blob: bb62d2a252179d864bc2513f8b6335993fc389b2 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
Dan Williamscc9203b2011-05-08 17:34:44 -070056#include "host.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057
58#define SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT (10)
59#define SCIC_SDS_APC_RECONFIGURATION_TIMEOUT (10)
60#define SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION (100)
61
62enum SCIC_SDS_APC_ACTIVITY {
63 SCIC_SDS_APC_SKIP_PHY,
64 SCIC_SDS_APC_ADD_PHY,
65 SCIC_SDS_APC_START_TIMER,
66
67 SCIC_SDS_APC_ACTIVITY_MAX
68};
69
70/*
71 * ******************************************************************************
72 * General port configuration agent routines
73 * ****************************************************************************** */
74
75/**
76 *
77 * @address_one: A SAS Address to be compared.
78 * @address_two: A SAS Address to be compared.
79 *
80 * Compare the two SAS Address and if SAS Address One is greater than SAS
81 * Address Two then return > 0 else if SAS Address One is less than SAS Address
82 * Two return < 0 Otherwise they are the same return 0 A signed value of x > 0
83 * > y where x is returned for Address One > Address Two y is returned for
84 * Address One < Address Two 0 is returned ofr Address One = Address Two
85 */
86static s32 sci_sas_address_compare(
87 struct sci_sas_address address_one,
88 struct sci_sas_address address_two)
89{
90 if (address_one.high > address_two.high) {
91 return 1;
92 } else if (address_one.high < address_two.high) {
93 return -1;
94 } else if (address_one.low > address_two.low) {
95 return 1;
96 } else if (address_one.low < address_two.low) {
97 return -1;
98 }
99
100 /* The two SAS Address must be identical */
101 return 0;
102}
103
104/**
105 *
106 * @controller: The controller object used for the port search.
107 * @phy: The phy object to match.
108 *
109 * This routine will find a matching port for the phy. This means that the
110 * port and phy both have the same broadcast sas address and same received sas
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700111 * address. The port address or the NULL if there is no matching
Dan Williams6f231dd2011-07-02 22:56:22 -0700112 * port. port address if the port can be found to match the phy.
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700113 * NULL if there is no matching port for the phy.
Dan Williams6f231dd2011-07-02 22:56:22 -0700114 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700115static struct isci_port *scic_sds_port_configuration_agent_find_port(
Edmund Nadolskied30c272011-05-05 01:11:43 +0000116 struct scic_sds_controller *scic,
Dan Williams85280952011-06-28 15:05:53 -0700117 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700118{
Edmund Nadolskied30c272011-05-05 01:11:43 +0000119 u8 i;
Dan Williams6f231dd2011-07-02 22:56:22 -0700120 struct sci_sas_address port_sas_address;
121 struct sci_sas_address port_attached_device_address;
122 struct sci_sas_address phy_sas_address;
123 struct sci_sas_address phy_attached_device_address;
124
125 /*
126 * Since this phy can be a member of a wide port check to see if one or
127 * more phys match the sent and received SAS address as this phy in which
Edmund Nadolskied30c272011-05-05 01:11:43 +0000128 * case it should participate in the same port.
129 */
Dan Williams85280952011-06-28 15:05:53 -0700130 scic_sds_phy_get_sas_address(iphy, &phy_sas_address);
131 scic_sds_phy_get_attached_sas_address(iphy, &phy_attached_device_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700132
Edmund Nadolskied30c272011-05-05 01:11:43 +0000133 for (i = 0; i < scic->logical_port_entries; i++) {
Dan Williamse5313812011-05-07 10:11:43 -0700134 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamsffe191c2011-06-29 13:09:25 -0700135 struct isci_port *iport = &ihost->ports[i];
Dan Williams6f231dd2011-07-02 22:56:22 -0700136
Dan Williamsffe191c2011-06-29 13:09:25 -0700137 scic_sds_port_get_sas_address(iport, &port_sas_address);
138 scic_sds_port_get_attached_sas_address(iport, &port_attached_device_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700139
Dan Williamse5313812011-05-07 10:11:43 -0700140 if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
141 sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
Dan Williamsffe191c2011-06-29 13:09:25 -0700142 return iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700143 }
144
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700145 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700146}
147
148/**
149 *
150 * @controller: This is the controller object that contains the port agent
151 * @port_agent: This is the port configruation agent for the controller.
152 *
153 * This routine will validate the port configuration is correct for the SCU
154 * hardware. The SCU hardware allows for port configurations as follows. LP0
155 * -> (PE0), (PE0, PE1), (PE0, PE1, PE2, PE3) LP1 -> (PE1) LP2 -> (PE2), (PE2,
156 * PE3) LP3 -> (PE3) enum sci_status SCI_SUCCESS the port configuration is valid for
157 * this port configuration agent. SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
158 * the port configuration is not valid for this port configuration agent.
159 */
160static enum sci_status scic_sds_port_configuration_agent_validate_ports(
161 struct scic_sds_controller *controller,
162 struct scic_sds_port_configuration_agent *port_agent)
163{
Dan Williams4b339812011-05-06 17:36:38 -0700164 struct isci_host *ihost = scic_to_ihost(controller);
Dan Williams6f231dd2011-07-02 22:56:22 -0700165 struct sci_sas_address first_address;
166 struct sci_sas_address second_address;
167
168 /*
169 * Sanity check the max ranges for all the phys the max index
170 * is always equal to the port range index */
Dan Williams4b339812011-05-06 17:36:38 -0700171 if (port_agent->phy_valid_port_range[0].max_index != 0 ||
172 port_agent->phy_valid_port_range[1].max_index != 1 ||
173 port_agent->phy_valid_port_range[2].max_index != 2 ||
174 port_agent->phy_valid_port_range[3].max_index != 3)
Dan Williams6f231dd2011-07-02 22:56:22 -0700175 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
Dan Williams6f231dd2011-07-02 22:56:22 -0700176
177 /*
178 * This is a request to configure a single x4 port or at least attempt
179 * to make all the phys into a single port */
Dan Williams4b339812011-05-06 17:36:38 -0700180 if (port_agent->phy_valid_port_range[0].min_index == 0 &&
181 port_agent->phy_valid_port_range[1].min_index == 0 &&
182 port_agent->phy_valid_port_range[2].min_index == 0 &&
183 port_agent->phy_valid_port_range[3].min_index == 0)
Dan Williams6f231dd2011-07-02 22:56:22 -0700184 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700185
186 /*
187 * This is a degenerate case where phy 1 and phy 2 are assigned
188 * to the same port this is explicitly disallowed by the hardware
189 * unless they are part of the same x4 port and this condition was
190 * already checked above. */
191 if (port_agent->phy_valid_port_range[2].min_index == 1) {
192 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
193 }
194
195 /*
196 * PE0 and PE3 can never have the same SAS Address unless they
197 * are part of the same x4 wide port and we have already checked
198 * for this condition. */
Dan Williams85280952011-06-28 15:05:53 -0700199 scic_sds_phy_get_sas_address(&ihost->phys[0], &first_address);
200 scic_sds_phy_get_sas_address(&ihost->phys[3], &second_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700201
202 if (sci_sas_address_compare(first_address, second_address) == 0) {
203 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
204 }
205
206 /*
207 * PE0 and PE1 are configured into a 2x1 ports make sure that the
208 * SAS Address for PE0 and PE2 are different since they can not be
209 * part of the same port. */
Dan Williams4b339812011-05-06 17:36:38 -0700210 if (port_agent->phy_valid_port_range[0].min_index == 0 &&
211 port_agent->phy_valid_port_range[1].min_index == 1) {
Dan Williams85280952011-06-28 15:05:53 -0700212 scic_sds_phy_get_sas_address(&ihost->phys[0], &first_address);
213 scic_sds_phy_get_sas_address(&ihost->phys[2], &second_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700214
215 if (sci_sas_address_compare(first_address, second_address) == 0) {
216 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
217 }
218 }
219
220 /*
221 * PE2 and PE3 are configured into a 2x1 ports make sure that the
222 * SAS Address for PE1 and PE3 are different since they can not be
223 * part of the same port. */
Dan Williams4b339812011-05-06 17:36:38 -0700224 if (port_agent->phy_valid_port_range[2].min_index == 2 &&
225 port_agent->phy_valid_port_range[3].min_index == 3) {
Dan Williams85280952011-06-28 15:05:53 -0700226 scic_sds_phy_get_sas_address(&ihost->phys[1], &first_address);
227 scic_sds_phy_get_sas_address(&ihost->phys[3], &second_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700228
229 if (sci_sas_address_compare(first_address, second_address) == 0) {
230 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
231 }
232 }
233
234 return SCI_SUCCESS;
235}
236
237/*
238 * ******************************************************************************
239 * Manual port configuration agent routines
240 * ****************************************************************************** */
241
242/**
243 *
244 *
245 * This routine will verify that all of the phys in the same port are using the
246 * same SAS address.
247 */
248static enum sci_status scic_sds_mpc_agent_validate_phy_configuration(
249 struct scic_sds_controller *controller,
250 struct scic_sds_port_configuration_agent *port_agent)
251{
Dan Williams4b339812011-05-06 17:36:38 -0700252 struct isci_host *ihost = scic_to_ihost(controller);
Dan Williams6f231dd2011-07-02 22:56:22 -0700253 u32 phy_mask;
254 u32 assigned_phy_mask;
255 struct sci_sas_address sas_address;
256 struct sci_sas_address phy_assigned_address;
257 u8 port_index;
258 u8 phy_index;
259
260 assigned_phy_mask = 0;
261 sas_address.high = 0;
262 sas_address.low = 0;
263
264 for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
265 phy_mask = controller->oem_parameters.sds1.ports[port_index].phy_mask;
266
Dan Williams4b339812011-05-06 17:36:38 -0700267 if (!phy_mask)
268 continue;
269 /*
270 * Make sure that one or more of the phys were not already assinged to
271 * a different port. */
272 if ((phy_mask & ~assigned_phy_mask) == 0) {
273 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
274 }
275
276 /* Find the starting phy index for this round through the loop */
277 for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
278 if ((phy_mask & (1 << phy_index)) == 0)
279 continue;
Dan Williams85280952011-06-28 15:05:53 -0700280 scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700281 &sas_address);
282
Dan Williams6f231dd2011-07-02 22:56:22 -0700283 /*
Dan Williams4b339812011-05-06 17:36:38 -0700284 * The phy_index can be used as the starting point for the
285 * port range since the hardware starts all logical ports
286 * the same as the PE index. */
287 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
288 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
289
290 if (phy_index != port_index) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700291 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
292 }
293
Dan Williams4b339812011-05-06 17:36:38 -0700294 break;
Dan Williams6f231dd2011-07-02 22:56:22 -0700295 }
Dan Williams4b339812011-05-06 17:36:38 -0700296
297 /*
298 * See how many additional phys are being added to this logical port.
299 * Note: We have not moved the current phy_index so we will actually
300 * compare the startting phy with itself.
301 * This is expected and required to add the phy to the port. */
302 while (phy_index < SCI_MAX_PHYS) {
303 if ((phy_mask & (1 << phy_index)) == 0)
304 continue;
Dan Williams85280952011-06-28 15:05:53 -0700305 scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700306 &phy_assigned_address);
307
308 if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
309 /*
310 * The phy mask specified that this phy is part of the same port
311 * as the starting phy and it is not so fail this configuration */
312 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
313 }
314
315 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
316 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
317
Dan Williamsffe191c2011-06-29 13:09:25 -0700318 scic_sds_port_add_phy(&ihost->ports[port_index],
Dan Williams85280952011-06-28 15:05:53 -0700319 &ihost->phys[phy_index]);
Dan Williams4b339812011-05-06 17:36:38 -0700320
321 assigned_phy_mask |= (1 << phy_index);
322 }
323
324 phy_index++;
Dan Williams6f231dd2011-07-02 22:56:22 -0700325 }
326
327 return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
328}
329
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700330static void mpc_agent_timeout(unsigned long data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700331{
332 u8 index;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700333 struct sci_timer *tmr = (struct sci_timer *)data;
334 struct scic_sds_port_configuration_agent *port_agent;
335 struct scic_sds_controller *scic;
336 struct isci_host *ihost;
337 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700338 u16 configure_phy_mask;
339
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700340 port_agent = container_of(tmr, typeof(*port_agent), timer);
341 scic = container_of(port_agent, typeof(*scic), port_agent);
342 ihost = scic_to_ihost(scic);
343
344 spin_lock_irqsave(&ihost->scic_lock, flags);
345
346 if (tmr->cancel)
347 goto done;
348
Dan Williams6f231dd2011-07-02 22:56:22 -0700349 port_agent->timer_pending = false;
350
351 /* Find the mask of phys that are reported read but as yet unconfigured into a port */
352 configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
353
354 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700355 struct isci_phy *iphy = &ihost->phys[index];
Dan Williams4b339812011-05-06 17:36:38 -0700356
Dan Williams6f231dd2011-07-02 22:56:22 -0700357 if (configure_phy_mask & (1 << index)) {
Dan Williams4b339812011-05-06 17:36:38 -0700358 port_agent->link_up_handler(scic, port_agent,
Dan Williams85280952011-06-28 15:05:53 -0700359 phy_get_non_dummy_port(iphy),
360 iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700361 }
362 }
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700363
364done:
365 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700366}
367
Dan Williams85280952011-06-28 15:05:53 -0700368static void scic_sds_mpc_agent_link_up(struct scic_sds_controller *controller,
369 struct scic_sds_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700370 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700371 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700372{
Dan Williamsffe191c2011-06-29 13:09:25 -0700373 /* If the port is NULL then the phy was not assigned to a port.
374 * This is because the phy was not given the same SAS Address as
375 * the other PHYs in the port.
Dan Williams85280952011-06-28 15:05:53 -0700376 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700377 if (!iport)
378 return;
Dan Williams6f231dd2011-07-02 22:56:22 -0700379
Dan Williamsffe191c2011-06-29 13:09:25 -0700380 port_agent->phy_ready_mask |= (1 << scic_sds_phy_get_index(iphy));
381 scic_sds_port_link_up(iport, iphy);
382 if ((iport->active_phy_mask & (1 << scic_sds_phy_get_index(iphy))))
383 port_agent->phy_configured_mask |= (1 << scic_sds_phy_get_index(iphy));
Dan Williams6f231dd2011-07-02 22:56:22 -0700384}
385
386/**
387 *
388 * @controller: This is the controller object that receives the link down
389 * notification.
390 * @port: This is the port object associated with the phy. If the is no
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700391 * associated port this is an NULL. The port is an invalid
Dan Williams6f231dd2011-07-02 22:56:22 -0700392 * handle only if the phy was never port of this port. This happens when
393 * the phy is not broadcasting the same SAS address as the other phys in the
394 * assigned port.
395 * @phy: This is the phy object which has gone link down.
396 *
Dave Jiang09d7da12011-03-26 16:11:51 -0700397 * This function handles the manual port configuration link down notifications.
Dan Williams6f231dd2011-07-02 22:56:22 -0700398 * Since all ports and phys are associated at initialization time we just turn
399 * around and notifiy the port object of the link down event. If this PHY is
400 * not associated with a port there is no action taken. Is it possible to get a
401 * link down notification from a phy that has no assocoated port?
402 */
403static void scic_sds_mpc_agent_link_down(
Dave Jiang09d7da12011-03-26 16:11:51 -0700404 struct scic_sds_controller *scic,
Dan Williams6f231dd2011-07-02 22:56:22 -0700405 struct scic_sds_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700406 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700407 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700408{
Dan Williamsffe191c2011-06-29 13:09:25 -0700409 if (iport != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700410 /*
Dave Jiang09d7da12011-03-26 16:11:51 -0700411 * If we can form a new port from the remainder of the phys
412 * then we want to start the timer to allow the SCI User to
413 * cleanup old devices and rediscover the port before
414 * rebuilding the port with the phys that remain in the ready
415 * state.
416 */
417 port_agent->phy_ready_mask &=
Dan Williams85280952011-06-28 15:05:53 -0700418 ~(1 << scic_sds_phy_get_index(iphy));
Dave Jiang09d7da12011-03-26 16:11:51 -0700419 port_agent->phy_configured_mask &=
Dan Williams85280952011-06-28 15:05:53 -0700420 ~(1 << scic_sds_phy_get_index(iphy));
Dan Williams6f231dd2011-07-02 22:56:22 -0700421
422 /*
Dave Jiang09d7da12011-03-26 16:11:51 -0700423 * Check to see if there are more phys waiting to be
424 * configured into a port. If there are allow the SCI User
425 * to tear down this port, if necessary, and then reconstruct
426 * the port after the timeout.
427 */
428 if ((port_agent->phy_configured_mask == 0x0000) &&
429 (port_agent->phy_ready_mask != 0x0000) &&
430 !port_agent->timer_pending) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700431 port_agent->timer_pending = true;
432
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700433 sci_mod_timer(&port_agent->timer,
434 SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -0700435 }
436
Dan Williamsffe191c2011-06-29 13:09:25 -0700437 scic_sds_port_link_down(iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700438 }
439}
440
441/*
442 * ******************************************************************************
443 * Automatic port configuration agent routines
444 * ****************************************************************************** */
445
446/**
447 *
448 *
449 * This routine will verify that the phys are assigned a valid SAS address for
450 * automatic port configuration mode.
451 */
452static enum sci_status scic_sds_apc_agent_validate_phy_configuration(
453 struct scic_sds_controller *controller,
454 struct scic_sds_port_configuration_agent *port_agent)
455{
456 u8 phy_index;
457 u8 port_index;
458 struct sci_sas_address sas_address;
459 struct sci_sas_address phy_assigned_address;
Dan Williams4b339812011-05-06 17:36:38 -0700460 struct isci_host *ihost = scic_to_ihost(controller);
Dan Williams6f231dd2011-07-02 22:56:22 -0700461
462 phy_index = 0;
463
464 while (phy_index < SCI_MAX_PHYS) {
465 port_index = phy_index;
466
467 /* Get the assigned SAS Address for the first PHY on the controller. */
Dan Williams85280952011-06-28 15:05:53 -0700468 scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700469 &sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700470
471 while (++phy_index < SCI_MAX_PHYS) {
Dan Williams85280952011-06-28 15:05:53 -0700472 scic_sds_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700473 &phy_assigned_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700474
475 /* Verify each of the SAS address are all the same for every PHY */
476 if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
477 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
478 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
479 } else {
480 port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
481 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
482 break;
483 }
484 }
485 }
486
487 return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
488}
489
Dan Williams85280952011-06-28 15:05:53 -0700490static void scic_sds_apc_agent_configure_ports(struct scic_sds_controller *controller,
491 struct scic_sds_port_configuration_agent *port_agent,
492 struct isci_phy *iphy,
493 bool start_timer)
Dan Williams6f231dd2011-07-02 22:56:22 -0700494{
495 u8 port_index;
496 enum sci_status status;
Dan Williamsffe191c2011-06-29 13:09:25 -0700497 struct isci_port *iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700498 enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
Dan Williamse5313812011-05-07 10:11:43 -0700499 struct isci_host *ihost = scic_to_ihost(controller);
Dan Williams6f231dd2011-07-02 22:56:22 -0700500
Dan Williamsffe191c2011-06-29 13:09:25 -0700501 iport = scic_sds_port_configuration_agent_find_port(controller, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700502
Dan Williamsffe191c2011-06-29 13:09:25 -0700503 if (iport) {
504 if (scic_sds_port_is_valid_phy_assignment(iport, iphy->phy_index))
Dan Williams6f231dd2011-07-02 22:56:22 -0700505 apc_activity = SCIC_SDS_APC_ADD_PHY;
506 else
507 apc_activity = SCIC_SDS_APC_SKIP_PHY;
508 } else {
509 /*
510 * There is no matching Port for this PHY so lets search through the
511 * Ports and see if we can add the PHY to its own port or maybe start
512 * the timer and wait to see if a wider port can be made.
513 *
514 * Note the break when we reach the condition of the port id == phy id */
Dan Williamsffe191c2011-06-29 13:09:25 -0700515 for (port_index = port_agent->phy_valid_port_range[iphy->phy_index].min_index;
516 port_index <= port_agent->phy_valid_port_range[iphy->phy_index].max_index;
517 port_index++) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700518
Dan Williamsffe191c2011-06-29 13:09:25 -0700519 iport = &ihost->ports[port_index];
Dan Williams6f231dd2011-07-02 22:56:22 -0700520
521 /* First we must make sure that this PHY can be added to this Port. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700522 if (scic_sds_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700523 /*
524 * Port contains a PHY with a greater PHY ID than the current
525 * PHY that has gone link up. This phy can not be part of any
526 * port so skip it and move on. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700527 if (iport->active_phy_mask > (1 << iphy->phy_index)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700528 apc_activity = SCIC_SDS_APC_SKIP_PHY;
529 break;
530 }
531
532 /*
533 * We have reached the end of our Port list and have not found
534 * any reason why we should not either add the PHY to the port
535 * or wait for more phys to become active. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700536 if (iport->physical_port_index == iphy->phy_index) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700537 /*
538 * The Port either has no active PHYs.
539 * Consider that if the port had any active PHYs we would have
540 * or active PHYs with
541 * a lower PHY Id than this PHY. */
542 if (apc_activity != SCIC_SDS_APC_START_TIMER) {
543 apc_activity = SCIC_SDS_APC_ADD_PHY;
544 }
545
546 break;
547 }
548
549 /*
550 * The current Port has no active PHYs and this PHY could be part
551 * of this Port. Since we dont know as yet setup to start the
552 * timer and see if there is a better configuration. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700553 if (iport->active_phy_mask == 0) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700554 apc_activity = SCIC_SDS_APC_START_TIMER;
555 }
Dan Williamsffe191c2011-06-29 13:09:25 -0700556 } else if (iport->active_phy_mask != 0) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700557 /*
558 * The Port has an active phy and the current Phy can not
559 * participate in this port so skip the PHY and see if
560 * there is a better configuration. */
561 apc_activity = SCIC_SDS_APC_SKIP_PHY;
562 }
563 }
564 }
565
566 /*
567 * Check to see if the start timer operations should instead map to an
568 * add phy operation. This is caused because we have been waiting to
569 * add a phy to a port but could not becuase the automatic port
570 * configuration engine had a choice of possible ports for the phy.
571 * Since we have gone through a timeout we are going to restrict the
572 * choice to the smallest possible port. */
573 if (
574 (start_timer == false)
575 && (apc_activity == SCIC_SDS_APC_START_TIMER)
576 ) {
577 apc_activity = SCIC_SDS_APC_ADD_PHY;
578 }
579
580 switch (apc_activity) {
581 case SCIC_SDS_APC_ADD_PHY:
Dan Williamsffe191c2011-06-29 13:09:25 -0700582 status = scic_sds_port_add_phy(iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700583
584 if (status == SCI_SUCCESS) {
Dan Williams85280952011-06-28 15:05:53 -0700585 port_agent->phy_configured_mask |= (1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700586 }
587 break;
588
589 case SCIC_SDS_APC_START_TIMER:
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700590 /*
591 * This can occur for either a link down event, or a link
592 * up event where we cannot yet tell the port to which a
593 * phy belongs.
594 */
595 if (port_agent->timer_pending)
596 sci_del_timer(&port_agent->timer);
597
598 port_agent->timer_pending = true;
599 sci_mod_timer(&port_agent->timer,
600 SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
Dan Williams6f231dd2011-07-02 22:56:22 -0700601 break;
602
603 case SCIC_SDS_APC_SKIP_PHY:
604 default:
605 /* do nothing the PHY can not be made part of a port at this time. */
606 break;
607 }
608}
609
610/**
Piotr Sawickib3824292011-02-23 00:09:14 -0800611 * scic_sds_apc_agent_link_up - handle apc link up events
612 * @scic: This is the controller object that receives the link up
Dan Williams6f231dd2011-07-02 22:56:22 -0700613 * notification.
Piotr Sawickib3824292011-02-23 00:09:14 -0800614 * @sci_port: This is the port object associated with the phy. If the is no
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700615 * associated port this is an NULL.
Piotr Sawickib3824292011-02-23 00:09:14 -0800616 * @sci_phy: This is the phy object which has gone link up.
Dan Williams6f231dd2011-07-02 22:56:22 -0700617 *
618 * This method handles the automatic port configuration for link up
619 * notifications. Is it possible to get a link down notification from a phy
620 * that has no assocoated port?
621 */
Piotr Sawickib3824292011-02-23 00:09:14 -0800622static void scic_sds_apc_agent_link_up(struct scic_sds_controller *scic,
623 struct scic_sds_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700624 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700625 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700626{
Dan Williams85280952011-06-28 15:05:53 -0700627 u8 phy_index = iphy->phy_index;
Dan Williams6f231dd2011-07-02 22:56:22 -0700628
Dan Williamsffe191c2011-06-29 13:09:25 -0700629 if (!iport) {
Piotr Sawickib3824292011-02-23 00:09:14 -0800630 /* the phy is not the part of this port */
631 port_agent->phy_ready_mask |= 1 << phy_index;
Dan Williams85280952011-06-28 15:05:53 -0700632 scic_sds_apc_agent_configure_ports(scic, port_agent, iphy, true);
Piotr Sawickib3824292011-02-23 00:09:14 -0800633 } else {
634 /* the phy is already the part of the port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700635 u32 port_state = iport->sm.current_state_id;
Dan Williams6f231dd2011-07-02 22:56:22 -0700636
Piotr Sawickib3824292011-02-23 00:09:14 -0800637 /* if the PORT'S state is resetting then the link up is from
638 * port hard reset in this case, we need to tell the port
639 * that link up is recieved
640 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000641 BUG_ON(port_state != SCI_PORT_RESETTING);
Piotr Sawickib3824292011-02-23 00:09:14 -0800642 port_agent->phy_ready_mask |= 1 << phy_index;
Dan Williamsffe191c2011-06-29 13:09:25 -0700643 scic_sds_port_link_up(iport, iphy);
Piotr Sawickib3824292011-02-23 00:09:14 -0800644 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700645}
646
647/**
648 *
649 * @controller: This is the controller object that receives the link down
650 * notification.
Dan Williamsffe191c2011-06-29 13:09:25 -0700651 * @iport: This is the port object associated with the phy. If the is no
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700652 * associated port this is an NULL.
Dan Williamsffe191c2011-06-29 13:09:25 -0700653 * @iphy: This is the phy object which has gone link down.
Dan Williams6f231dd2011-07-02 22:56:22 -0700654 *
655 * This method handles the automatic port configuration link down
656 * notifications. not associated with a port there is no action taken. Is it
657 * possible to get a link down notification from a phy that has no assocoated
658 * port?
659 */
660static void scic_sds_apc_agent_link_down(
661 struct scic_sds_controller *controller,
662 struct scic_sds_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700663 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700664 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700665{
Dan Williams85280952011-06-28 15:05:53 -0700666 port_agent->phy_ready_mask &= ~(1 << scic_sds_phy_get_index(iphy));
Dan Williams6f231dd2011-07-02 22:56:22 -0700667
Dan Williamsffe191c2011-06-29 13:09:25 -0700668 if (!iport)
669 return;
670 if (port_agent->phy_configured_mask & (1 << iphy->phy_index)) {
671 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700672
Dan Williamsffe191c2011-06-29 13:09:25 -0700673 status = scic_sds_port_remove_phy(iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700674
Dan Williamsffe191c2011-06-29 13:09:25 -0700675 if (status == SCI_SUCCESS)
676 port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700677 }
678}
679
Dan Williams4b339812011-05-06 17:36:38 -0700680/* configure the phys into ports when the timer fires */
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700681static void apc_agent_timeout(unsigned long data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700682{
683 u32 index;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700684 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700685 struct scic_sds_port_configuration_agent *port_agent;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700686 struct scic_sds_controller *scic;
687 struct isci_host *ihost;
688 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700689 u16 configure_phy_mask;
690
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700691 port_agent = container_of(tmr, typeof(*port_agent), timer);
692 scic = container_of(port_agent, typeof(*scic), port_agent);
693 ihost = scic_to_ihost(scic);
694
695 spin_lock_irqsave(&ihost->scic_lock, flags);
696
697 if (tmr->cancel)
698 goto done;
Dan Williams6f231dd2011-07-02 22:56:22 -0700699
700 port_agent->timer_pending = false;
701
702 configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
703
Dan Williams4b339812011-05-06 17:36:38 -0700704 if (!configure_phy_mask)
705 return;
706
707 for (index = 0; index < SCI_MAX_PHYS; index++) {
708 if ((configure_phy_mask & (1 << index)) == 0)
709 continue;
710
711 scic_sds_apc_agent_configure_ports(scic, port_agent,
Dan Williams85280952011-06-28 15:05:53 -0700712 &ihost->phys[index], false);
Dan Williams6f231dd2011-07-02 22:56:22 -0700713 }
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700714
715done:
716 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700717}
718
719/*
720 * ******************************************************************************
721 * Public port configuration agent routines
722 * ****************************************************************************** */
723
724/**
725 *
726 *
727 * This method will construct the port configuration agent for operation. This
728 * call is universal for both manual port configuration and automatic port
729 * configuration modes.
730 */
731void scic_sds_port_configuration_agent_construct(
732 struct scic_sds_port_configuration_agent *port_agent)
733{
734 u32 index;
735
736 port_agent->phy_configured_mask = 0x00;
737 port_agent->phy_ready_mask = 0x00;
738
739 port_agent->link_up_handler = NULL;
740 port_agent->link_down_handler = NULL;
741
742 port_agent->timer_pending = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700743
744 for (index = 0; index < SCI_MAX_PORTS; index++) {
745 port_agent->phy_valid_port_range[index].min_index = 0;
746 port_agent->phy_valid_port_range[index].max_index = 0;
747 }
748}
749
Dan Williams6f231dd2011-07-02 22:56:22 -0700750enum sci_status scic_sds_port_configuration_agent_initialize(
Dave Jiang09d7da12011-03-26 16:11:51 -0700751 struct scic_sds_controller *scic,
Dan Williams6f231dd2011-07-02 22:56:22 -0700752 struct scic_sds_port_configuration_agent *port_agent)
753{
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700754 enum sci_status status;
Dave Jiangde728b72011-03-26 17:14:07 -0700755 enum scic_port_configuration_mode mode;
Dan Williams6f231dd2011-07-02 22:56:22 -0700756
Dave Jiang09d7da12011-03-26 16:11:51 -0700757 mode = scic->oem_parameters.sds1.controller.mode_type;
Dan Williams6f231dd2011-07-02 22:56:22 -0700758
759 if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dave Jiang09d7da12011-03-26 16:11:51 -0700760 status = scic_sds_mpc_agent_validate_phy_configuration(
761 scic, port_agent);
Dan Williams6f231dd2011-07-02 22:56:22 -0700762
763 port_agent->link_up_handler = scic_sds_mpc_agent_link_up;
764 port_agent->link_down_handler = scic_sds_mpc_agent_link_down;
765
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700766 sci_init_timer(&port_agent->timer, mpc_agent_timeout);
Dan Williams6f231dd2011-07-02 22:56:22 -0700767 } else {
Dave Jiang09d7da12011-03-26 16:11:51 -0700768 status = scic_sds_apc_agent_validate_phy_configuration(
769 scic, port_agent);
Dan Williams6f231dd2011-07-02 22:56:22 -0700770
771 port_agent->link_up_handler = scic_sds_apc_agent_link_up;
772 port_agent->link_down_handler = scic_sds_apc_agent_link_down;
773
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700774 sci_init_timer(&port_agent->timer, apc_agent_timeout);
Dan Williams6f231dd2011-07-02 22:56:22 -0700775 }
776
777 return status;
778}