1f11c7f63SJim Harris /*-
2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3*718cf2ccSPedro F. Giffuni *
4f11c7f63SJim Harris * This file is provided under a dual BSD/GPLv2 license. When using or
5f11c7f63SJim Harris * redistributing this file, you may do so under either license.
6f11c7f63SJim Harris *
7f11c7f63SJim Harris * GPL LICENSE SUMMARY
8f11c7f63SJim Harris *
9f11c7f63SJim Harris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10f11c7f63SJim Harris *
11f11c7f63SJim Harris * This program is free software; you can redistribute it and/or modify
12f11c7f63SJim Harris * it under the terms of version 2 of the GNU General Public License as
13f11c7f63SJim Harris * published by the Free Software Foundation.
14f11c7f63SJim Harris *
15f11c7f63SJim Harris * This program is distributed in the hope that it will be useful, but
16f11c7f63SJim Harris * WITHOUT ANY WARRANTY; without even the implied warranty of
17f11c7f63SJim Harris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18f11c7f63SJim Harris * General Public License for more details.
19f11c7f63SJim Harris *
20f11c7f63SJim Harris * You should have received a copy of the GNU General Public License
21f11c7f63SJim Harris * along with this program; if not, write to the Free Software
22f11c7f63SJim Harris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23f11c7f63SJim Harris * The full GNU General Public License is included in this distribution
24f11c7f63SJim Harris * in the file called LICENSE.GPL.
25f11c7f63SJim Harris *
26f11c7f63SJim Harris * BSD LICENSE
27f11c7f63SJim Harris *
28f11c7f63SJim Harris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29f11c7f63SJim Harris * All rights reserved.
30f11c7f63SJim Harris *
31f11c7f63SJim Harris * Redistribution and use in source and binary forms, with or without
32f11c7f63SJim Harris * modification, are permitted provided that the following conditions
33f11c7f63SJim Harris * are met:
34f11c7f63SJim Harris *
35f11c7f63SJim Harris * * Redistributions of source code must retain the above copyright
36f11c7f63SJim Harris * notice, this list of conditions and the following disclaimer.
37f11c7f63SJim Harris * * Redistributions in binary form must reproduce the above copyright
38f11c7f63SJim Harris * notice, this list of conditions and the following disclaimer in
39f11c7f63SJim Harris * the documentation and/or other materials provided with the
40f11c7f63SJim Harris * distribution.
41f11c7f63SJim Harris *
42f11c7f63SJim Harris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43f11c7f63SJim Harris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44f11c7f63SJim Harris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45f11c7f63SJim Harris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46f11c7f63SJim Harris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47f11c7f63SJim Harris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48f11c7f63SJim Harris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49f11c7f63SJim Harris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50f11c7f63SJim Harris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51f11c7f63SJim Harris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52f11c7f63SJim Harris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53f11c7f63SJim Harris */
54f11c7f63SJim Harris
55f11c7f63SJim Harris #include <sys/cdefs.h>
56f11c7f63SJim Harris /**
57f11c7f63SJim Harris * @file
58f11c7f63SJim Harris *
59f11c7f63SJim Harris * @brief This file contains the implementation of the SCIF_SAS_SMP_PHY
60f11c7f63SJim Harris * object.
61f11c7f63SJim Harris */
62f11c7f63SJim Harris
63f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_controller.h>
64f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_smp_phy.h>
65f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_smp_remote_device.h>
66f11c7f63SJim Harris
67f11c7f63SJim Harris //******************************************************************************
68f11c7f63SJim Harris //*
69f11c7f63SJim Harris //* P U B L I C M E T H O D S
70f11c7f63SJim Harris //*
71f11c7f63SJim Harris //******************************************************************************
72f11c7f63SJim Harris
73f11c7f63SJim Harris /**
74f11c7f63SJim Harris * @brief This routine constructs a smp phy object for an expander phy and insert
75f11c7f63SJim Harris * to owning expander device's smp_phy_list.
76f11c7f63SJim Harris * @param[in] this_smp_phy The memory space to store a phy
77f11c7f63SJim Harris * @param[in] owning_device The smp remote device that owns this smp phy.
78f11c7f63SJim Harris * @param[in] expander_phy_id The expander phy id for this_smp_phy.
79f11c7f63SJim Harris * @return None
80f11c7f63SJim Harris */
scif_sas_smp_phy_construct(SCIF_SAS_SMP_PHY_T * this_smp_phy,SCIF_SAS_REMOTE_DEVICE_T * owning_device,U8 expander_phy_id)81f11c7f63SJim Harris void scif_sas_smp_phy_construct(
82f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy,
83f11c7f63SJim Harris SCIF_SAS_REMOTE_DEVICE_T * owning_device,
84f11c7f63SJim Harris U8 expander_phy_id
85f11c7f63SJim Harris )
86f11c7f63SJim Harris {
87f11c7f63SJim Harris memset(this_smp_phy, 0, sizeof(SCIF_SAS_SMP_PHY_T));
88f11c7f63SJim Harris
89f11c7f63SJim Harris this_smp_phy->phy_identifier = expander_phy_id;
90f11c7f63SJim Harris this_smp_phy->owning_device = owning_device;
91f11c7f63SJim Harris
92f11c7f63SJim Harris sci_fast_list_element_init((this_smp_phy), (&this_smp_phy->list_element));
93f11c7f63SJim Harris
94f11c7f63SJim Harris //insert to owning device's smp phy list.
95f11c7f63SJim Harris sci_fast_list_insert_tail(
96f11c7f63SJim Harris (&owning_device->protocol_device.smp_device.smp_phy_list),
97f11c7f63SJim Harris (&this_smp_phy->list_element)
98f11c7f63SJim Harris );
99f11c7f63SJim Harris }
100f11c7f63SJim Harris
101f11c7f63SJim Harris /**
102f11c7f63SJim Harris * @brief This routine destructs a smp phy object for an expander phy and free the smp
103f11c7f63SJim Harris * phy to controller's smp phy memory.
104f11c7f63SJim Harris * @param[in] this_smp_phy The smp phy to be destructed.
105f11c7f63SJim Harris *
106f11c7f63SJim Harris * @return None
107f11c7f63SJim Harris */
scif_sas_smp_phy_destruct(SCIF_SAS_SMP_PHY_T * this_smp_phy)108f11c7f63SJim Harris void scif_sas_smp_phy_destruct(
109f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy
110f11c7f63SJim Harris )
111f11c7f63SJim Harris {
112f11c7f63SJim Harris SCIF_SAS_REMOTE_DEVICE_T * owning_device = this_smp_phy->owning_device;
113f11c7f63SJim Harris SCIF_SAS_CONTROLLER_T * fw_controller = owning_device->domain->controller;
114f11c7f63SJim Harris
115f11c7f63SJim Harris if ( ( this_smp_phy->attached_device_type == SMP_EDGE_EXPANDER_DEVICE
116f11c7f63SJim Harris || this_smp_phy->attached_device_type == SMP_FANOUT_EXPANDER_DEVICE)
117f11c7f63SJim Harris && this_smp_phy->u.attached_phy != NULL )
118f11c7f63SJim Harris {
119f11c7f63SJim Harris //update the counterpart phy from the other smp phy list.
120f11c7f63SJim Harris this_smp_phy->u.attached_phy->attached_device_type = SMP_NO_DEVICE_ATTACHED;
121f11c7f63SJim Harris this_smp_phy->u.attached_phy->u.attached_phy = NULL;
122f11c7f63SJim Harris }
123f11c7f63SJim Harris
124f11c7f63SJim Harris //remove curr_smp_phy
125f11c7f63SJim Harris sci_fast_list_remove_element(&this_smp_phy->list_element);
126f11c7f63SJim Harris scif_sas_controller_free_smp_phy(fw_controller, this_smp_phy);
127f11c7f63SJim Harris }
128f11c7f63SJim Harris
129f11c7f63SJim Harris
130f11c7f63SJim Harris /**
131f11c7f63SJim Harris * @brief This routine save a smp phy information based on discover response.
132f11c7f63SJim Harris *
133f11c7f63SJim Harris * @param[in] this_smp_phy The memory space to store a phy
134f11c7f63SJim Harris * @param[in] attached_device A possible direct attached device to this phy.
135f11c7f63SJim Harris *
136f11c7f63SJim Harris * @param[in] discover_response The smp DISCOVER response for this_smp_phy.
137f11c7f63SJim Harris * @return None
138f11c7f63SJim Harris */
scif_sas_smp_phy_save_information(SCIF_SAS_SMP_PHY_T * this_smp_phy,SCIF_SAS_REMOTE_DEVICE_T * attached_device,SMP_RESPONSE_DISCOVER_T * discover_response)139f11c7f63SJim Harris void scif_sas_smp_phy_save_information(
140f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy,
141f11c7f63SJim Harris SCIF_SAS_REMOTE_DEVICE_T * attached_device,
142f11c7f63SJim Harris SMP_RESPONSE_DISCOVER_T * discover_response
143f11c7f63SJim Harris )
144f11c7f63SJim Harris {
145f11c7f63SJim Harris ASSERT (this_smp_phy->owning_device != NULL);
146f11c7f63SJim Harris ASSERT (this_smp_phy->phy_identifier == discover_response->phy_identifier);
147f11c7f63SJim Harris
148f11c7f63SJim Harris this_smp_phy->attached_device_type = (U8)discover_response->u2.sas1_1.attached_device_type;
149f11c7f63SJim Harris this_smp_phy->routing_attribute = (U8)discover_response->routing_attribute;
150f11c7f63SJim Harris this_smp_phy->attached_sas_address = discover_response->attached_sas_address;
151f11c7f63SJim Harris this_smp_phy->config_route_table_index_anchor = 0;
152f11c7f63SJim Harris
153f11c7f63SJim Harris if (this_smp_phy->attached_device_type != SMP_EDGE_EXPANDER_DEVICE
154f11c7f63SJim Harris && this_smp_phy->attached_device_type != SMP_FANOUT_EXPANDER_DEVICE)
155f11c7f63SJim Harris {
156f11c7f63SJim Harris //note, end_device field could be an end device, or a NULL value, but can't be expander device.
157f11c7f63SJim Harris this_smp_phy->u.end_device = attached_device;
158f11c7f63SJim Harris }
159f11c7f63SJim Harris else
160f11c7f63SJim Harris {
161f11c7f63SJim Harris //if attached device type is expander, we will set u.attached_phy later when the
162f11c7f63SJim Harris //the attached expander finish its discover on attached_phy.
163f11c7f63SJim Harris ;
164f11c7f63SJim Harris }
165f11c7f63SJim Harris }
166f11c7f63SJim Harris
167f11c7f63SJim Harris /**
168f11c7f63SJim Harris * @brief This routine constructs a smp phy object for an expander phy.
169f11c7f63SJim Harris * @param[in] this_smp_phy The memory space to store a phy
170f11c7f63SJim Harris * @param[in] owning_device The smp remote device that owns this smp phy.
171f11c7f63SJim Harris * @param[in] discover_response The smp DISCOVER response for this_smp_phy.
172f11c7f63SJim Harris *
173f11c7f63SJim Harris * @return Whether a smp phy has an attached phy and the pair of phy are set
174f11c7f63SJim Harris * set to each other as attached phy successfully.
175f11c7f63SJim Harris */
scif_sas_smp_phy_set_attached_phy(SCIF_SAS_SMP_PHY_T * this_smp_phy,U8 attached_phy_identifier,SCIF_SAS_REMOTE_DEVICE_T * attached_remote_device)176f11c7f63SJim Harris SCI_STATUS scif_sas_smp_phy_set_attached_phy(
177f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy,
178f11c7f63SJim Harris U8 attached_phy_identifier,
179f11c7f63SJim Harris SCIF_SAS_REMOTE_DEVICE_T * attached_remote_device
180f11c7f63SJim Harris )
181f11c7f63SJim Harris {
182f11c7f63SJim Harris //find the attached phy from its owning device by attached_phy_id.
183f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * attached_smp_phy =
184f11c7f63SJim Harris (SCIF_SAS_SMP_PHY_T *)scif_sas_smp_remote_device_find_smp_phy_by_id(
185f11c7f63SJim Harris attached_phy_identifier,
186f11c7f63SJim Harris &attached_remote_device->protocol_device.smp_device);
187f11c7f63SJim Harris
188f11c7f63SJim Harris if (attached_smp_phy != NULL)
189f11c7f63SJim Harris {
190f11c7f63SJim Harris this_smp_phy->u.attached_phy = attached_smp_phy;
191f11c7f63SJim Harris attached_smp_phy->u.attached_phy = this_smp_phy;
192f11c7f63SJim Harris
193f11c7f63SJim Harris return SCI_SUCCESS;
194f11c7f63SJim Harris }
195f11c7f63SJim Harris
196f11c7f63SJim Harris return SCI_FAILURE;
197f11c7f63SJim Harris }
198f11c7f63SJim Harris
199f11c7f63SJim Harris
200f11c7f63SJim Harris /**
201f11c7f63SJim Harris * @brief This method verify the routing attributes of a phy connection per
202f11c7f63SJim Harris * specification.
203f11c7f63SJim Harris *
204f11c7f63SJim Harris * @param[in] this_smp_phy One smp phy belongs to a smp phy connection.
205f11c7f63SJim Harris * @param[in] attached_smp_phy One smp phy belongs to a smp phy connection.
206f11c7f63SJim Harris *
207f11c7f63SJim Harris * @return Whether routing attributes of a phy connection is legal.
208f11c7f63SJim Harris * @retval SCI_SUCCESS indicates a good phy connection.
209f11c7f63SJim Harris * SCI_FAILURE indicates a illegal phy connection.
210f11c7f63SJim Harris */
scif_sas_smp_phy_verify_routing_attribute(SCIF_SAS_SMP_PHY_T * this_smp_phy,SCIF_SAS_SMP_PHY_T * attached_smp_phy)211f11c7f63SJim Harris SCI_STATUS scif_sas_smp_phy_verify_routing_attribute(
212f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy,
213f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * attached_smp_phy
214f11c7f63SJim Harris )
215f11c7f63SJim Harris {
216f11c7f63SJim Harris SCI_STATUS status = SCI_SUCCESS;
217f11c7f63SJim Harris
218f11c7f63SJim Harris //expander phy with direct routing attribute can only connect to
219f11c7f63SJim Harris //phy with direct routing attribute.
220f11c7f63SJim Harris if ( this_smp_phy->routing_attribute == DIRECT_ROUTING_ATTRIBUTE
221f11c7f63SJim Harris || attached_smp_phy->routing_attribute == DIRECT_ROUTING_ATTRIBUTE )
222f11c7f63SJim Harris {
223f11c7f63SJim Harris if ( (this_smp_phy->routing_attribute | attached_smp_phy->routing_attribute)
224f11c7f63SJim Harris != DIRECT_ROUTING_ATTRIBUTE )
225f11c7f63SJim Harris status = SCI_FAILURE;
226f11c7f63SJim Harris }
227f11c7f63SJim Harris
228f11c7f63SJim Harris if (this_smp_phy->routing_attribute == TABLE_ROUTING_ATTRIBUTE
229f11c7f63SJim Harris && attached_smp_phy->routing_attribute == TABLE_ROUTING_ATTRIBUTE)
230f11c7f63SJim Harris {
231f11c7f63SJim Harris if ( ! this_smp_phy->owning_device->protocol_device.smp_device.is_table_to_table_supported
232f11c7f63SJim Harris || !attached_smp_phy->owning_device->protocol_device.smp_device.is_table_to_table_supported )
233f11c7f63SJim Harris status = SCI_FAILURE;
234f11c7f63SJim Harris }
235f11c7f63SJim Harris
236f11c7f63SJim Harris return status;
237f11c7f63SJim Harris }
238f11c7f63SJim Harris
239f11c7f63SJim Harris
240f11c7f63SJim Harris /**
241f11c7f63SJim Harris * @brief This method find The next smp phy that is in the smp phy list and
242f11c7f63SJim Harris * resides in the same wide port as this_smp_phy.
243f11c7f63SJim Harris *
244f11c7f63SJim Harris * @param[in] this_smp_phy The smp phy whose neighbor phy that is in the same
245f11c7f63SJim Harris * same wide port is to be find.
246f11c7f63SJim Harris *
247f11c7f63SJim Harris * @return The next smp phy that is in the smp phy list and resides in the same
248f11c7f63SJim Harris * wide port as this_smp_phy.
249f11c7f63SJim Harris */
scif_sas_smp_phy_find_next_phy_in_wide_port(SCIF_SAS_SMP_PHY_T * this_smp_phy)250f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * scif_sas_smp_phy_find_next_phy_in_wide_port(
251f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy
252f11c7f63SJim Harris )
253f11c7f63SJim Harris {
254f11c7f63SJim Harris SCI_FAST_LIST_ELEMENT_T * next_phy_element = sci_fast_list_get_next(
255f11c7f63SJim Harris &(this_smp_phy->list_element) );
256f11c7f63SJim Harris
257f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * next_phy;
258f11c7f63SJim Harris
259f11c7f63SJim Harris while (next_phy_element != NULL)
260f11c7f63SJim Harris {
261f11c7f63SJim Harris next_phy = (SCIF_SAS_SMP_PHY_T *)sci_fast_list_get_object(next_phy_element);
262f11c7f63SJim Harris
263f11c7f63SJim Harris next_phy_element = sci_fast_list_get_next( &(next_phy->list_element));
264f11c7f63SJim Harris
265f11c7f63SJim Harris if (next_phy->attached_sas_address.high == this_smp_phy->attached_sas_address.high
266f11c7f63SJim Harris &&next_phy->attached_sas_address.low == this_smp_phy->attached_sas_address.low)
267f11c7f63SJim Harris return next_phy;
268f11c7f63SJim Harris }
269f11c7f63SJim Harris
270f11c7f63SJim Harris return NULL;
271f11c7f63SJim Harris }
272f11c7f63SJim Harris
273f11c7f63SJim Harris
274f11c7f63SJim Harris /**
275f11c7f63SJim Harris * @brief This method find the smp phy that resides in the middle of the same
276f11c7f63SJim Harris * wide port as this_smp_phy.
277f11c7f63SJim Harris *
278f11c7f63SJim Harris * @param[in] this_smp_phy The smp phy who is the lowest order phy in a wide
279f11c7f63SJim Harris * port .
280f11c7f63SJim Harris *
281f11c7f63SJim Harris * @return The next smp phy that is in the smp phy list and resides in the same
282f11c7f63SJim Harris * wide port as this_smp_phy.
283f11c7f63SJim Harris */
scif_sas_smp_phy_find_middle_phy_in_wide_port(SCIF_SAS_SMP_PHY_T * this_smp_phy)284f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * scif_sas_smp_phy_find_middle_phy_in_wide_port(
285f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy
286f11c7f63SJim Harris )
287f11c7f63SJim Harris {
288f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * next_phy =
289f11c7f63SJim Harris scif_sas_smp_phy_find_next_phy_in_wide_port(this_smp_phy);
290f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * middle_phy = this_smp_phy;
291f11c7f63SJim Harris
292f11c7f63SJim Harris //currently we assume a wide port could not be wider than X4. so the
293f11c7f63SJim Harris //second phy is always the correct answer for x2, x3 or x4 wide port.
294f11c7f63SJim Harris //For a narrow port, phy0 is the middle phy.
295f11c7f63SJim Harris if (next_phy != NULL)
296f11c7f63SJim Harris {
297f11c7f63SJim Harris middle_phy = next_phy;
298f11c7f63SJim Harris next_phy =
299f11c7f63SJim Harris scif_sas_smp_phy_find_next_phy_in_wide_port(next_phy);
300f11c7f63SJim Harris }
301f11c7f63SJim Harris
302f11c7f63SJim Harris if (next_phy != NULL)
303f11c7f63SJim Harris middle_phy = next_phy;
304f11c7f63SJim Harris
305f11c7f63SJim Harris return middle_phy;
306f11c7f63SJim Harris }
307f11c7f63SJim Harris
308f11c7f63SJim Harris
309f11c7f63SJim Harris /**
310f11c7f63SJim Harris * @brief This method find the smp phy that is the hishest order phy
311f11c7f63SJim Harris * in the same wide port as this_smp_phy.
312f11c7f63SJim Harris *
313f11c7f63SJim Harris * @param[in] this_smp_phy The smp phy who is the lowest order phy in a wide
314f11c7f63SJim Harris * port.
315f11c7f63SJim Harris *
316f11c7f63SJim Harris * @return The next smp phy that is in the smp phy list and resides in the same
317f11c7f63SJim Harris * wide port as this_smp_phy.
318f11c7f63SJim Harris */
scif_sas_smp_phy_find_highest_phy_in_wide_port(SCIF_SAS_SMP_PHY_T * this_smp_phy)319f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * scif_sas_smp_phy_find_highest_phy_in_wide_port(
320f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * this_smp_phy
321f11c7f63SJim Harris )
322f11c7f63SJim Harris {
323f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * next_phy =
324f11c7f63SJim Harris scif_sas_smp_phy_find_next_phy_in_wide_port(this_smp_phy);
325f11c7f63SJim Harris SCIF_SAS_SMP_PHY_T * highest_phy = this_smp_phy;
326f11c7f63SJim Harris
327f11c7f63SJim Harris while(next_phy != NULL )
328f11c7f63SJim Harris {
329f11c7f63SJim Harris highest_phy = next_phy;
330f11c7f63SJim Harris next_phy =
331f11c7f63SJim Harris scif_sas_smp_phy_find_next_phy_in_wide_port(next_phy);
332f11c7f63SJim Harris }
333f11c7f63SJim Harris
334f11c7f63SJim Harris return highest_phy;
335f11c7f63SJim Harris }
336