1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris */
52230557Sjimharris
53230557Sjimharris#include <sys/cdefs.h>
54230557Sjimharris__FBSDID("$FreeBSD$");
55230557Sjimharris
56230557Sjimharris/**
57230557Sjimharris * @file
58230557Sjimharris *
59230557Sjimharris * @brief This file contains the methods for the SCIF_SAS_SMP_REMMOTE object's
60230557Sjimharris *           clear affiliation activity.
61230557Sjimharris */
62230557Sjimharris#include <dev/isci/scil/sci_controller.h>
63230557Sjimharris#include <dev/isci/scil/scif_sas_controller.h>
64230557Sjimharris#include <dev/isci/scil/scif_sas_remote_device.h>
65230557Sjimharris#include <dev/isci/scil/scif_sas_logger.h>
66230557Sjimharris
67230557Sjimharris#include <dev/isci/scil/scif_sas_smp_remote_device.h>
68230557Sjimharris#include <dev/isci/scil/scif_sas_smp_io_request.h>
69230557Sjimharris#include <dev/isci/scil/intel_sas.h>
70230557Sjimharris#include <dev/isci/scil/scic_io_request.h>
71230557Sjimharris#include <dev/isci/scil/scic_remote_device.h>
72230557Sjimharris#include <dev/isci/scil/scif_sas_smp_phy.h>
73230557Sjimharris
74230557Sjimharris//******************************************************************************
75230557Sjimharris//* P R I V A T E   M E T H O D S
76230557Sjimharris//******************************************************************************
77230557Sjimharris
78230557Sjimharris/**
79230557Sjimharris * @brief This method finds the next smp phy (from the anchor_phy) that link to
80230557Sjimharris *           a SATA end device.
81230557Sjimharris *
82230557Sjimharris * @param[in] fw_device the framework SMP device that is clearing affiliation for
83230557Sjimharris *               its remote SATA devices'
84230557Sjimharris *
85230557Sjimharris * @return SCIF_SAS_SMP_PHY_T a smp phy, to which clear affiliation phy control command
86230557Sjimharris *            is to be sent.
87230557Sjimharris */
88230557Sjimharrisstatic
89230557SjimharrisSCIF_SAS_SMP_PHY_T * scif_sas_smp_remote_device_find_next_smp_phy_link_to_sata(
90230557Sjimharris   SCIF_SAS_SMP_PHY_T * anchor_phy
91230557Sjimharris)
92230557Sjimharris{
93230557Sjimharris   SCI_FAST_LIST_ELEMENT_T  * element = &anchor_phy->list_element;
94230557Sjimharris   SCIF_SAS_SMP_PHY_T * curr_smp_phy = NULL;
95230557Sjimharris
96230557Sjimharris   while (element != NULL)
97230557Sjimharris   {
98230557Sjimharris      curr_smp_phy = (SCIF_SAS_SMP_PHY_T*) sci_fast_list_get_object(element);
99230557Sjimharris      element = sci_fast_list_get_next(element);
100230557Sjimharris
101230557Sjimharris      if (curr_smp_phy->attached_device_type == SMP_END_DEVICE_ONLY
102230557Sjimharris          && curr_smp_phy->u.end_device != NULL)
103230557Sjimharris      {
104230557Sjimharris         SMP_DISCOVER_RESPONSE_PROTOCOLS_T  dev_protocols;
105230557Sjimharris         scic_remote_device_get_protocols(
106230557Sjimharris            curr_smp_phy->u.end_device->core_object, &dev_protocols);
107230557Sjimharris
108230557Sjimharris         if (dev_protocols.u.bits.attached_stp_target)
109230557Sjimharris            return curr_smp_phy;
110230557Sjimharris      }
111230557Sjimharris   }
112230557Sjimharris
113230557Sjimharris   return NULL;
114230557Sjimharris}
115230557Sjimharris
116230557Sjimharris//******************************************************************************
117230557Sjimharris//* P U B L I C   M E T H O D S
118230557Sjimharris//******************************************************************************
119230557Sjimharris
120230557Sjimharris/**
121230557Sjimharris * @brief This method starts the clear affiliation activity for a
122230557Sjimharris *           smp device's remote SATA devices.
123230557Sjimharris *
124230557Sjimharris * @param[in] fw_device the framework SMP device that is clearing affiliation for
125230557Sjimharris *               its remote SATA devices.
126230557Sjimharris *
127230557Sjimharris * @return none
128230557Sjimharris */
129230557Sjimharrisvoid scif_sas_smp_remote_device_start_clear_affiliation(
130230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device
131230557Sjimharris)
132230557Sjimharris{
133230557Sjimharris   SCIF_SAS_SMP_REMOTE_DEVICE_T * smp_device =
134230557Sjimharris      &fw_device->protocol_device.smp_device;
135230557Sjimharris
136230557Sjimharris   SCIF_SAS_SMP_PHY_T * phy_to_clear_affiliation = NULL;
137230557Sjimharris
138230557Sjimharris   if (smp_device->smp_phy_list.list_head != NULL)
139230557Sjimharris   {
140230557Sjimharris      phy_to_clear_affiliation =
141230557Sjimharris         scif_sas_smp_remote_device_find_next_smp_phy_link_to_sata(
142230557Sjimharris            (SCIF_SAS_SMP_PHY_T *)smp_device->smp_phy_list.list_head->object
143230557Sjimharris         );
144230557Sjimharris   }
145230557Sjimharris
146230557Sjimharris   if (phy_to_clear_affiliation != NULL)
147230557Sjimharris   {
148230557Sjimharris      smp_device->curr_clear_affiliation_phy = phy_to_clear_affiliation;
149230557Sjimharris
150230557Sjimharris      //set current activity
151230557Sjimharris      fw_device->protocol_device.smp_device.current_activity =
152230557Sjimharris         SCIF_SAS_SMP_REMOTE_DEVICE_ACTIVITY_CLEAR_AFFILIATION;
153230557Sjimharris
154230557Sjimharris      //Set current_smp_request to PHY CONTROL.
155230557Sjimharris      fw_device->protocol_device.smp_device.current_smp_request =
156230557Sjimharris         SMP_FUNCTION_PHY_CONTROL;
157230557Sjimharris
158230557Sjimharris      //reset discover_to_start flag.
159230557Sjimharris      fw_device->protocol_device.smp_device.scheduled_activity =
160230557Sjimharris         SCIF_SAS_SMP_REMOTE_DEVICE_ACTIVITY_NONE;
161230557Sjimharris
162230557Sjimharris      //build PHY Control (clear affiliation) to the phy.
163230557Sjimharris      scif_sas_smp_request_construct_phy_control(
164230557Sjimharris         fw_device->domain->controller,
165230557Sjimharris         fw_device,
166230557Sjimharris         PHY_OPERATION_CLEAR_AFFILIATION,
167230557Sjimharris         phy_to_clear_affiliation->phy_identifier,
168230557Sjimharris         NULL,
169230557Sjimharris         NULL
170230557Sjimharris      );
171230557Sjimharris
172230557Sjimharris      //issue DPC to start this request.
173230557Sjimharris      scif_cb_start_internal_io_task_schedule(
174230557Sjimharris         fw_device->domain->controller,
175230557Sjimharris         scif_sas_controller_start_high_priority_io,
176230557Sjimharris         fw_device->domain->controller
177230557Sjimharris      );
178230557Sjimharris   }
179230557Sjimharris   else
180230557Sjimharris      scif_sas_smp_remote_device_finish_clear_affiliation(fw_device);
181230557Sjimharris}
182230557Sjimharris
183230557Sjimharris
184230557Sjimharris/**
185230557Sjimharris * @brief This method continues the clear affiliation activity for a
186230557Sjimharris *           smp device's remote SATA devices.
187230557Sjimharris *
188230557Sjimharris * @param[in] fw_device the framework SMP device that is clearing affiliation for
189230557Sjimharris *               its remote SATA devices.
190230557Sjimharris *
191230557Sjimharris * @return none
192230557Sjimharris */
193230557Sjimharrisvoid scif_sas_smp_remote_device_continue_clear_affiliation(
194230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device
195230557Sjimharris)
196230557Sjimharris{
197230557Sjimharris   SCIF_SAS_SMP_REMOTE_DEVICE_T * smp_device =
198230557Sjimharris      &fw_device->protocol_device.smp_device;
199230557Sjimharris
200230557Sjimharris   //search from next immediate smp phy.
201230557Sjimharris   SCIF_SAS_SMP_PHY_T * phy_to_clear_affiliation = NULL;
202230557Sjimharris
203230557Sjimharris   if (smp_device->curr_clear_affiliation_phy->list_element.next != NULL)
204230557Sjimharris   {
205230557Sjimharris      phy_to_clear_affiliation =
206230557Sjimharris         scif_sas_smp_remote_device_find_next_smp_phy_link_to_sata(
207230557Sjimharris            smp_device->curr_clear_affiliation_phy->list_element.next->object
208230557Sjimharris         );
209230557Sjimharris   }
210230557Sjimharris
211230557Sjimharris   if (phy_to_clear_affiliation != NULL)
212230557Sjimharris   {
213230557Sjimharris      smp_device->curr_clear_affiliation_phy = phy_to_clear_affiliation;
214230557Sjimharris
215230557Sjimharris      //build PHY Control (clear affiliation) to the phy.
216230557Sjimharris      scif_sas_smp_request_construct_phy_control(
217230557Sjimharris         fw_device->domain->controller,
218230557Sjimharris         fw_device,
219230557Sjimharris         PHY_OPERATION_CLEAR_AFFILIATION,
220230557Sjimharris         phy_to_clear_affiliation->phy_identifier,
221230557Sjimharris         NULL,
222230557Sjimharris         NULL
223230557Sjimharris      );
224230557Sjimharris   }
225230557Sjimharris   else
226230557Sjimharris      scif_sas_smp_remote_device_finish_clear_affiliation(fw_device);
227230557Sjimharris}
228230557Sjimharris
229230557Sjimharris
230230557Sjimharris/**
231230557Sjimharris * @brief This method finishes the clear affiliation activity for a
232230557Sjimharris *           smp device's remote SATA devices. It then notify the domain it fihishes
233230557Sjimharris *           the clear affiliation activity.
234230557Sjimharris *
235230557Sjimharris * @param[in] fw_device the framework SMP device that is clearing affiliation for
236230557Sjimharris *               its remote SATA devices.
237230557Sjimharris *
238230557Sjimharris * @return none
239230557Sjimharris */
240230557Sjimharrisvoid scif_sas_smp_remote_device_finish_clear_affiliation(
241230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device
242230557Sjimharris)
243230557Sjimharris{
244230557Sjimharris   SCIF_SAS_DOMAIN_T * fw_domain = fw_device->domain;
245230557Sjimharris
246230557Sjimharris   scif_sas_smp_remote_device_clear(fw_device);
247230557Sjimharris
248230557Sjimharris   //let domain continue to clear affiliation on other smp devices.
249230557Sjimharris   scif_sas_domain_continue_clear_affiliation(fw_domain);
250230557Sjimharris}
251230557Sjimharris
252230557Sjimharris
253