blob: ac879745ef8007ab2a4973aff26e3c950e8f1dba [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)
Dan Williams50a92d92012-02-29 01:07:56 -080060#define SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION (1000)
Dan Williams6f231dd2011-07-02 22:56:22 -070061
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 Williams89a73012011-06-30 19:14:33 -0700115static struct isci_port *sci_port_configuration_agent_find_port(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700116 struct isci_host *ihost,
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 Williams89a73012011-06-30 19:14:33 -0700130 sci_phy_get_sas_address(iphy, &phy_sas_address);
131 sci_phy_get_attached_sas_address(iphy, &phy_attached_device_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700132
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700133 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700134 struct isci_port *iport = &ihost->ports[i];
Dan Williams6f231dd2011-07-02 22:56:22 -0700135
Dan Williams89a73012011-06-30 19:14:33 -0700136 sci_port_get_sas_address(iport, &port_sas_address);
137 sci_port_get_attached_sas_address(iport, &port_attached_device_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700138
Dan Williamse5313812011-05-07 10:11:43 -0700139 if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
140 sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
Dan Williamsffe191c2011-06-29 13:09:25 -0700141 return iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700142 }
143
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700144 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700145}
146
147/**
148 *
149 * @controller: This is the controller object that contains the port agent
150 * @port_agent: This is the port configruation agent for the controller.
151 *
152 * This routine will validate the port configuration is correct for the SCU
153 * hardware. The SCU hardware allows for port configurations as follows. LP0
154 * -> (PE0), (PE0, PE1), (PE0, PE1, PE2, PE3) LP1 -> (PE1) LP2 -> (PE2), (PE2,
155 * PE3) LP3 -> (PE3) enum sci_status SCI_SUCCESS the port configuration is valid for
156 * this port configuration agent. SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
157 * the port configuration is not valid for this port configuration agent.
158 */
Dan Williams89a73012011-06-30 19:14:33 -0700159static enum sci_status sci_port_configuration_agent_validate_ports(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700160 struct isci_host *ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700161 struct sci_port_configuration_agent *port_agent)
Dan Williams6f231dd2011-07-02 22:56:22 -0700162{
163 struct sci_sas_address first_address;
164 struct sci_sas_address second_address;
165
166 /*
167 * Sanity check the max ranges for all the phys the max index
168 * is always equal to the port range index */
Dan Williams4b339812011-05-06 17:36:38 -0700169 if (port_agent->phy_valid_port_range[0].max_index != 0 ||
170 port_agent->phy_valid_port_range[1].max_index != 1 ||
171 port_agent->phy_valid_port_range[2].max_index != 2 ||
172 port_agent->phy_valid_port_range[3].max_index != 3)
Dan Williams6f231dd2011-07-02 22:56:22 -0700173 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
Dan Williams6f231dd2011-07-02 22:56:22 -0700174
175 /*
176 * This is a request to configure a single x4 port or at least attempt
177 * to make all the phys into a single port */
Dan Williams4b339812011-05-06 17:36:38 -0700178 if (port_agent->phy_valid_port_range[0].min_index == 0 &&
179 port_agent->phy_valid_port_range[1].min_index == 0 &&
180 port_agent->phy_valid_port_range[2].min_index == 0 &&
181 port_agent->phy_valid_port_range[3].min_index == 0)
Dan Williams6f231dd2011-07-02 22:56:22 -0700182 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700183
184 /*
185 * This is a degenerate case where phy 1 and phy 2 are assigned
186 * to the same port this is explicitly disallowed by the hardware
187 * unless they are part of the same x4 port and this condition was
188 * already checked above. */
189 if (port_agent->phy_valid_port_range[2].min_index == 1) {
190 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
191 }
192
193 /*
194 * PE0 and PE3 can never have the same SAS Address unless they
195 * are part of the same x4 wide port and we have already checked
196 * for this condition. */
Dan Williams89a73012011-06-30 19:14:33 -0700197 sci_phy_get_sas_address(&ihost->phys[0], &first_address);
198 sci_phy_get_sas_address(&ihost->phys[3], &second_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700199
200 if (sci_sas_address_compare(first_address, second_address) == 0) {
201 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
202 }
203
204 /*
205 * PE0 and PE1 are configured into a 2x1 ports make sure that the
206 * SAS Address for PE0 and PE2 are different since they can not be
207 * part of the same port. */
Dan Williams4b339812011-05-06 17:36:38 -0700208 if (port_agent->phy_valid_port_range[0].min_index == 0 &&
209 port_agent->phy_valid_port_range[1].min_index == 1) {
Dan Williams89a73012011-06-30 19:14:33 -0700210 sci_phy_get_sas_address(&ihost->phys[0], &first_address);
211 sci_phy_get_sas_address(&ihost->phys[2], &second_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
213 if (sci_sas_address_compare(first_address, second_address) == 0) {
214 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
215 }
216 }
217
218 /*
219 * PE2 and PE3 are configured into a 2x1 ports make sure that the
220 * SAS Address for PE1 and PE3 are different since they can not be
221 * part of the same port. */
Dan Williams4b339812011-05-06 17:36:38 -0700222 if (port_agent->phy_valid_port_range[2].min_index == 2 &&
223 port_agent->phy_valid_port_range[3].min_index == 3) {
Dan Williams89a73012011-06-30 19:14:33 -0700224 sci_phy_get_sas_address(&ihost->phys[1], &first_address);
225 sci_phy_get_sas_address(&ihost->phys[3], &second_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700226
227 if (sci_sas_address_compare(first_address, second_address) == 0) {
228 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
229 }
230 }
231
232 return SCI_SUCCESS;
233}
234
235/*
236 * ******************************************************************************
237 * Manual port configuration agent routines
238 * ****************************************************************************** */
239
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700240/* verify all of the phys in the same port are using the same SAS address */
241static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700242sci_mpc_agent_validate_phy_configuration(struct isci_host *ihost,
243 struct sci_port_configuration_agent *port_agent)
Dan Williams6f231dd2011-07-02 22:56:22 -0700244{
245 u32 phy_mask;
246 u32 assigned_phy_mask;
247 struct sci_sas_address sas_address;
248 struct sci_sas_address phy_assigned_address;
249 u8 port_index;
250 u8 phy_index;
251
252 assigned_phy_mask = 0;
253 sas_address.high = 0;
254 sas_address.low = 0;
255
256 for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
Dan Williams89a73012011-06-30 19:14:33 -0700257 phy_mask = ihost->oem_parameters.ports[port_index].phy_mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700258
Dan Williams4b339812011-05-06 17:36:38 -0700259 if (!phy_mask)
260 continue;
261 /*
262 * Make sure that one or more of the phys were not already assinged to
263 * a different port. */
264 if ((phy_mask & ~assigned_phy_mask) == 0) {
265 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
266 }
267
268 /* Find the starting phy index for this round through the loop */
269 for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
270 if ((phy_mask & (1 << phy_index)) == 0)
271 continue;
Dan Williams89a73012011-06-30 19:14:33 -0700272 sci_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700273 &sas_address);
274
Dan Williams6f231dd2011-07-02 22:56:22 -0700275 /*
Dan Williams4b339812011-05-06 17:36:38 -0700276 * The phy_index can be used as the starting point for the
277 * port range since the hardware starts all logical ports
278 * the same as the PE index. */
279 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
280 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
281
282 if (phy_index != port_index) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700283 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
284 }
285
Dan Williams4b339812011-05-06 17:36:38 -0700286 break;
Dan Williams6f231dd2011-07-02 22:56:22 -0700287 }
Dan Williams4b339812011-05-06 17:36:38 -0700288
289 /*
290 * See how many additional phys are being added to this logical port.
291 * Note: We have not moved the current phy_index so we will actually
292 * compare the startting phy with itself.
293 * This is expected and required to add the phy to the port. */
294 while (phy_index < SCI_MAX_PHYS) {
295 if ((phy_mask & (1 << phy_index)) == 0)
296 continue;
Dan Williams89a73012011-06-30 19:14:33 -0700297 sci_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700298 &phy_assigned_address);
299
300 if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
301 /*
302 * The phy mask specified that this phy is part of the same port
303 * as the starting phy and it is not so fail this configuration */
304 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
305 }
306
307 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
308 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
309
Dan Williams89a73012011-06-30 19:14:33 -0700310 sci_port_add_phy(&ihost->ports[port_index],
Dan Williams85280952011-06-28 15:05:53 -0700311 &ihost->phys[phy_index]);
Dan Williams4b339812011-05-06 17:36:38 -0700312
313 assigned_phy_mask |= (1 << phy_index);
Xinghai Yu80aebef2013-07-17 10:54:01 +0800314 phy_index++;
Dan Williams4b339812011-05-06 17:36:38 -0700315 }
316
Dan Williams6f231dd2011-07-02 22:56:22 -0700317 }
318
Dan Williams89a73012011-06-30 19:14:33 -0700319 return sci_port_configuration_agent_validate_ports(ihost, port_agent);
Dan Williams6f231dd2011-07-02 22:56:22 -0700320}
321
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700322static void mpc_agent_timeout(unsigned long data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700323{
324 u8 index;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700325 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williams89a73012011-06-30 19:14:33 -0700326 struct sci_port_configuration_agent *port_agent;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700327 struct isci_host *ihost;
328 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700329 u16 configure_phy_mask;
330
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700331 port_agent = container_of(tmr, typeof(*port_agent), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700332 ihost = container_of(port_agent, typeof(*ihost), port_agent);
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700333
334 spin_lock_irqsave(&ihost->scic_lock, flags);
335
336 if (tmr->cancel)
337 goto done;
338
Dan Williams6f231dd2011-07-02 22:56:22 -0700339 port_agent->timer_pending = false;
340
341 /* Find the mask of phys that are reported read but as yet unconfigured into a port */
342 configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
343
344 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700345 struct isci_phy *iphy = &ihost->phys[index];
Dan Williams4b339812011-05-06 17:36:38 -0700346
Dan Williams6f231dd2011-07-02 22:56:22 -0700347 if (configure_phy_mask & (1 << index)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700348 port_agent->link_up_handler(ihost, port_agent,
Dan Williams85280952011-06-28 15:05:53 -0700349 phy_get_non_dummy_port(iphy),
350 iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700351 }
352 }
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700353
354done:
355 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700356}
357
Dan Williams89a73012011-06-30 19:14:33 -0700358static void sci_mpc_agent_link_up(struct isci_host *ihost,
359 struct sci_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700360 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700361 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700362{
Dan Williamsffe191c2011-06-29 13:09:25 -0700363 /* If the port is NULL then the phy was not assigned to a port.
364 * This is because the phy was not given the same SAS Address as
365 * the other PHYs in the port.
Dan Williams85280952011-06-28 15:05:53 -0700366 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700367 if (!iport)
368 return;
Dan Williams6f231dd2011-07-02 22:56:22 -0700369
Dan Williams34a99152011-07-01 02:25:15 -0700370 port_agent->phy_ready_mask |= (1 << iphy->phy_index);
Dan Williams89a73012011-06-30 19:14:33 -0700371 sci_port_link_up(iport, iphy);
Dan Williams34a99152011-07-01 02:25:15 -0700372 if ((iport->active_phy_mask & (1 << iphy->phy_index)))
373 port_agent->phy_configured_mask |= (1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700374}
375
376/**
377 *
378 * @controller: This is the controller object that receives the link down
379 * notification.
380 * @port: This is the port object associated with the phy. If the is no
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700381 * associated port this is an NULL. The port is an invalid
Dan Williams6f231dd2011-07-02 22:56:22 -0700382 * handle only if the phy was never port of this port. This happens when
383 * the phy is not broadcasting the same SAS address as the other phys in the
384 * assigned port.
385 * @phy: This is the phy object which has gone link down.
386 *
Dave Jiang09d7da12011-03-26 16:11:51 -0700387 * This function handles the manual port configuration link down notifications.
Dan Williams6f231dd2011-07-02 22:56:22 -0700388 * Since all ports and phys are associated at initialization time we just turn
389 * around and notifiy the port object of the link down event. If this PHY is
390 * not associated with a port there is no action taken. Is it possible to get a
391 * link down notification from a phy that has no assocoated port?
392 */
Dan Williams89a73012011-06-30 19:14:33 -0700393static void sci_mpc_agent_link_down(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700394 struct isci_host *ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700395 struct sci_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700396 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700397 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700398{
Dan Williamsffe191c2011-06-29 13:09:25 -0700399 if (iport != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700400 /*
Dave Jiang09d7da12011-03-26 16:11:51 -0700401 * If we can form a new port from the remainder of the phys
402 * then we want to start the timer to allow the SCI User to
403 * cleanup old devices and rediscover the port before
404 * rebuilding the port with the phys that remain in the ready
405 * state.
406 */
Dan Williams34a99152011-07-01 02:25:15 -0700407 port_agent->phy_ready_mask &= ~(1 << iphy->phy_index);
408 port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700409
410 /*
Dave Jiang09d7da12011-03-26 16:11:51 -0700411 * Check to see if there are more phys waiting to be
412 * configured into a port. If there are allow the SCI User
413 * to tear down this port, if necessary, and then reconstruct
414 * the port after the timeout.
415 */
416 if ((port_agent->phy_configured_mask == 0x0000) &&
417 (port_agent->phy_ready_mask != 0x0000) &&
418 !port_agent->timer_pending) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700419 port_agent->timer_pending = true;
420
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700421 sci_mod_timer(&port_agent->timer,
422 SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -0700423 }
424
Dan Williams89a73012011-06-30 19:14:33 -0700425 sci_port_link_down(iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700426 }
427}
428
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700429/* verify phys are assigned a valid SAS address for automatic port
430 * configuration mode.
Dan Williams6f231dd2011-07-02 22:56:22 -0700431 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700432static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700433sci_apc_agent_validate_phy_configuration(struct isci_host *ihost,
434 struct sci_port_configuration_agent *port_agent)
Dan Williams6f231dd2011-07-02 22:56:22 -0700435{
436 u8 phy_index;
437 u8 port_index;
438 struct sci_sas_address sas_address;
439 struct sci_sas_address phy_assigned_address;
440
441 phy_index = 0;
442
443 while (phy_index < SCI_MAX_PHYS) {
444 port_index = phy_index;
445
446 /* Get the assigned SAS Address for the first PHY on the controller. */
Dan Williams89a73012011-06-30 19:14:33 -0700447 sci_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700448 &sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700449
450 while (++phy_index < SCI_MAX_PHYS) {
Dan Williams89a73012011-06-30 19:14:33 -0700451 sci_phy_get_sas_address(&ihost->phys[phy_index],
Dan Williams4b339812011-05-06 17:36:38 -0700452 &phy_assigned_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700453
454 /* Verify each of the SAS address are all the same for every PHY */
455 if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
456 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
457 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
458 } else {
459 port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
460 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
461 break;
462 }
463 }
464 }
465
Dan Williams89a73012011-06-30 19:14:33 -0700466 return sci_port_configuration_agent_validate_ports(ihost, port_agent);
Dan Williams6f231dd2011-07-02 22:56:22 -0700467}
468
Marcin Tomczakbe778342012-01-04 01:33:31 -0800469/*
470 * This routine will restart the automatic port configuration timeout
471 * timer for the next time period. This could be caused by either a link
472 * down event or a link up event where we can not yet tell to which a phy
473 * belongs.
474 */
Dan Williams50a92d92012-02-29 01:07:56 -0800475static void sci_apc_agent_start_timer(struct sci_port_configuration_agent *port_agent,
476 u32 timeout)
Marcin Tomczakbe778342012-01-04 01:33:31 -0800477{
Marcin Tomczakbe778342012-01-04 01:33:31 -0800478 port_agent->timer_pending = true;
479 sci_mod_timer(&port_agent->timer, timeout);
480}
481
Dan Williams89a73012011-06-30 19:14:33 -0700482static void sci_apc_agent_configure_ports(struct isci_host *ihost,
483 struct sci_port_configuration_agent *port_agent,
Dan Williams85280952011-06-28 15:05:53 -0700484 struct isci_phy *iphy,
485 bool start_timer)
Dan Williams6f231dd2011-07-02 22:56:22 -0700486{
487 u8 port_index;
488 enum sci_status status;
Dan Williamsffe191c2011-06-29 13:09:25 -0700489 struct isci_port *iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700490 enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
491
Dan Williams89a73012011-06-30 19:14:33 -0700492 iport = sci_port_configuration_agent_find_port(ihost, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700493
Dan Williamsffe191c2011-06-29 13:09:25 -0700494 if (iport) {
Dan Williams89a73012011-06-30 19:14:33 -0700495 if (sci_port_is_valid_phy_assignment(iport, iphy->phy_index))
Dan Williams6f231dd2011-07-02 22:56:22 -0700496 apc_activity = SCIC_SDS_APC_ADD_PHY;
497 else
498 apc_activity = SCIC_SDS_APC_SKIP_PHY;
499 } else {
500 /*
501 * There is no matching Port for this PHY so lets search through the
502 * Ports and see if we can add the PHY to its own port or maybe start
503 * the timer and wait to see if a wider port can be made.
504 *
505 * Note the break when we reach the condition of the port id == phy id */
Dan Williamsffe191c2011-06-29 13:09:25 -0700506 for (port_index = port_agent->phy_valid_port_range[iphy->phy_index].min_index;
507 port_index <= port_agent->phy_valid_port_range[iphy->phy_index].max_index;
508 port_index++) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700509
Dan Williamsffe191c2011-06-29 13:09:25 -0700510 iport = &ihost->ports[port_index];
Dan Williams6f231dd2011-07-02 22:56:22 -0700511
512 /* First we must make sure that this PHY can be added to this Port. */
Dan Williams89a73012011-06-30 19:14:33 -0700513 if (sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700514 /*
515 * Port contains a PHY with a greater PHY ID than the current
516 * PHY that has gone link up. This phy can not be part of any
517 * port so skip it and move on. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700518 if (iport->active_phy_mask > (1 << iphy->phy_index)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700519 apc_activity = SCIC_SDS_APC_SKIP_PHY;
520 break;
521 }
522
523 /*
524 * We have reached the end of our Port list and have not found
525 * any reason why we should not either add the PHY to the port
526 * or wait for more phys to become active. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700527 if (iport->physical_port_index == iphy->phy_index) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700528 /*
529 * The Port either has no active PHYs.
530 * Consider that if the port had any active PHYs we would have
531 * or active PHYs with
532 * a lower PHY Id than this PHY. */
533 if (apc_activity != SCIC_SDS_APC_START_TIMER) {
534 apc_activity = SCIC_SDS_APC_ADD_PHY;
535 }
536
537 break;
538 }
539
540 /*
541 * The current Port has no active PHYs and this PHY could be part
542 * of this Port. Since we dont know as yet setup to start the
543 * timer and see if there is a better configuration. */
Dan Williamsffe191c2011-06-29 13:09:25 -0700544 if (iport->active_phy_mask == 0) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700545 apc_activity = SCIC_SDS_APC_START_TIMER;
546 }
Dan Williamsffe191c2011-06-29 13:09:25 -0700547 } else if (iport->active_phy_mask != 0) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700548 /*
549 * The Port has an active phy and the current Phy can not
550 * participate in this port so skip the PHY and see if
551 * there is a better configuration. */
552 apc_activity = SCIC_SDS_APC_SKIP_PHY;
553 }
554 }
555 }
556
557 /*
558 * Check to see if the start timer operations should instead map to an
559 * add phy operation. This is caused because we have been waiting to
560 * add a phy to a port but could not becuase the automatic port
561 * configuration engine had a choice of possible ports for the phy.
562 * Since we have gone through a timeout we are going to restrict the
563 * choice to the smallest possible port. */
564 if (
565 (start_timer == false)
566 && (apc_activity == SCIC_SDS_APC_START_TIMER)
567 ) {
568 apc_activity = SCIC_SDS_APC_ADD_PHY;
569 }
570
571 switch (apc_activity) {
572 case SCIC_SDS_APC_ADD_PHY:
Dan Williams89a73012011-06-30 19:14:33 -0700573 status = sci_port_add_phy(iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700574
575 if (status == SCI_SUCCESS) {
Dan Williams85280952011-06-28 15:05:53 -0700576 port_agent->phy_configured_mask |= (1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700577 }
578 break;
579
580 case SCIC_SDS_APC_START_TIMER:
Marcin Tomczakbe778342012-01-04 01:33:31 -0800581 sci_apc_agent_start_timer(port_agent,
582 SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
Dan Williams6f231dd2011-07-02 22:56:22 -0700583 break;
584
585 case SCIC_SDS_APC_SKIP_PHY:
586 default:
587 /* do nothing the PHY can not be made part of a port at this time. */
588 break;
589 }
590}
591
592/**
Dan Williams89a73012011-06-30 19:14:33 -0700593 * sci_apc_agent_link_up - handle apc link up events
Piotr Sawickib3824292011-02-23 00:09:14 -0800594 * @scic: This is the controller object that receives the link up
Dan Williams6f231dd2011-07-02 22:56:22 -0700595 * notification.
Piotr Sawickib3824292011-02-23 00:09:14 -0800596 * @sci_port: This is the port object associated with the phy. If the is no
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700597 * associated port this is an NULL.
Piotr Sawickib3824292011-02-23 00:09:14 -0800598 * @sci_phy: This is the phy object which has gone link up.
Dan Williams6f231dd2011-07-02 22:56:22 -0700599 *
600 * This method handles the automatic port configuration for link up
601 * notifications. Is it possible to get a link down notification from a phy
602 * that has no assocoated port?
603 */
Dan Williams89a73012011-06-30 19:14:33 -0700604static void sci_apc_agent_link_up(struct isci_host *ihost,
605 struct sci_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700606 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700607 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700608{
Dan Williams85280952011-06-28 15:05:53 -0700609 u8 phy_index = iphy->phy_index;
Dan Williams6f231dd2011-07-02 22:56:22 -0700610
Dan Williamsffe191c2011-06-29 13:09:25 -0700611 if (!iport) {
Piotr Sawickib3824292011-02-23 00:09:14 -0800612 /* the phy is not the part of this port */
613 port_agent->phy_ready_mask |= 1 << phy_index;
Marcin Tomczakbe778342012-01-04 01:33:31 -0800614 sci_apc_agent_start_timer(port_agent,
615 SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
Piotr Sawickib3824292011-02-23 00:09:14 -0800616 } else {
617 /* the phy is already the part of the port */
Piotr Sawickib3824292011-02-23 00:09:14 -0800618 port_agent->phy_ready_mask |= 1 << phy_index;
Dan Williams89a73012011-06-30 19:14:33 -0700619 sci_port_link_up(iport, iphy);
Piotr Sawickib3824292011-02-23 00:09:14 -0800620 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700621}
622
623/**
624 *
625 * @controller: This is the controller object that receives the link down
626 * notification.
Dan Williamsffe191c2011-06-29 13:09:25 -0700627 * @iport: This is the port object associated with the phy. If the is no
Edmund Nadolskia7e536c2011-02-08 09:28:42 -0700628 * associated port this is an NULL.
Dan Williamsffe191c2011-06-29 13:09:25 -0700629 * @iphy: This is the phy object which has gone link down.
Dan Williams6f231dd2011-07-02 22:56:22 -0700630 *
631 * This method handles the automatic port configuration link down
632 * notifications. not associated with a port there is no action taken. Is it
633 * possible to get a link down notification from a phy that has no assocoated
634 * port?
635 */
Dan Williams89a73012011-06-30 19:14:33 -0700636static void sci_apc_agent_link_down(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700637 struct isci_host *ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700638 struct sci_port_configuration_agent *port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -0700639 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700640 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700641{
Dan Williams34a99152011-07-01 02:25:15 -0700642 port_agent->phy_ready_mask &= ~(1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700643
Dan Williamsffe191c2011-06-29 13:09:25 -0700644 if (!iport)
645 return;
646 if (port_agent->phy_configured_mask & (1 << iphy->phy_index)) {
647 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700648
Dan Williams89a73012011-06-30 19:14:33 -0700649 status = sci_port_remove_phy(iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700650
Dan Williamsffe191c2011-06-29 13:09:25 -0700651 if (status == SCI_SUCCESS)
652 port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
Dan Williams6f231dd2011-07-02 22:56:22 -0700653 }
654}
655
Dan Williams4b339812011-05-06 17:36:38 -0700656/* configure the phys into ports when the timer fires */
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700657static void apc_agent_timeout(unsigned long data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700658{
659 u32 index;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700660 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williams89a73012011-06-30 19:14:33 -0700661 struct sci_port_configuration_agent *port_agent;
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700662 struct isci_host *ihost;
663 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700664 u16 configure_phy_mask;
665
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700666 port_agent = container_of(tmr, typeof(*port_agent), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700667 ihost = container_of(port_agent, typeof(*ihost), port_agent);
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700668
669 spin_lock_irqsave(&ihost->scic_lock, flags);
670
671 if (tmr->cancel)
672 goto done;
Dan Williams6f231dd2011-07-02 22:56:22 -0700673
674 port_agent->timer_pending = false;
675
676 configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
677
Dan Williams4b339812011-05-06 17:36:38 -0700678 if (!configure_phy_mask)
Jeff Skirvin983d3fd2011-09-28 18:35:32 -0700679 goto done;
Dan Williams4b339812011-05-06 17:36:38 -0700680
681 for (index = 0; index < SCI_MAX_PHYS; index++) {
682 if ((configure_phy_mask & (1 << index)) == 0)
683 continue;
684
Dan Williams89a73012011-06-30 19:14:33 -0700685 sci_apc_agent_configure_ports(ihost, port_agent,
Dan Williams85280952011-06-28 15:05:53 -0700686 &ihost->phys[index], false);
Dan Williams6f231dd2011-07-02 22:56:22 -0700687 }
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700688
Dan Williams50a92d92012-02-29 01:07:56 -0800689 if (is_controller_start_complete(ihost))
690 sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
691
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700692done:
693 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700694}
695
696/*
697 * ******************************************************************************
698 * Public port configuration agent routines
699 * ****************************************************************************** */
700
701/**
702 *
703 *
704 * This method will construct the port configuration agent for operation. This
705 * call is universal for both manual port configuration and automatic port
706 * configuration modes.
707 */
Dan Williams89a73012011-06-30 19:14:33 -0700708void sci_port_configuration_agent_construct(
709 struct sci_port_configuration_agent *port_agent)
Dan Williams6f231dd2011-07-02 22:56:22 -0700710{
711 u32 index;
712
713 port_agent->phy_configured_mask = 0x00;
714 port_agent->phy_ready_mask = 0x00;
715
716 port_agent->link_up_handler = NULL;
717 port_agent->link_down_handler = NULL;
718
719 port_agent->timer_pending = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700720
721 for (index = 0; index < SCI_MAX_PORTS; index++) {
722 port_agent->phy_valid_port_range[index].min_index = 0;
723 port_agent->phy_valid_port_range[index].max_index = 0;
724 }
725}
726
Dan Williams50a92d92012-02-29 01:07:56 -0800727bool is_port_config_apc(struct isci_host *ihost)
728{
729 return ihost->port_agent.link_up_handler == sci_apc_agent_link_up;
730}
731
Dan Williams89a73012011-06-30 19:14:33 -0700732enum sci_status sci_port_configuration_agent_initialize(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700733 struct isci_host *ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700734 struct sci_port_configuration_agent *port_agent)
Dan Williams6f231dd2011-07-02 22:56:22 -0700735{
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700736 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -0700737 enum sci_port_configuration_mode mode;
Dan Williams6f231dd2011-07-02 22:56:22 -0700738
Dan Williams89a73012011-06-30 19:14:33 -0700739 mode = ihost->oem_parameters.controller.mode_type;
Dan Williams6f231dd2011-07-02 22:56:22 -0700740
741 if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams89a73012011-06-30 19:14:33 -0700742 status = sci_mpc_agent_validate_phy_configuration(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700743 ihost, port_agent);
Dan Williams6f231dd2011-07-02 22:56:22 -0700744
Dan Williams89a73012011-06-30 19:14:33 -0700745 port_agent->link_up_handler = sci_mpc_agent_link_up;
746 port_agent->link_down_handler = sci_mpc_agent_link_down;
Dan Williams6f231dd2011-07-02 22:56:22 -0700747
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700748 sci_init_timer(&port_agent->timer, mpc_agent_timeout);
Dan Williams6f231dd2011-07-02 22:56:22 -0700749 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700750 status = sci_apc_agent_validate_phy_configuration(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700751 ihost, port_agent);
Dan Williams6f231dd2011-07-02 22:56:22 -0700752
Dan Williams89a73012011-06-30 19:14:33 -0700753 port_agent->link_up_handler = sci_apc_agent_link_up;
754 port_agent->link_down_handler = sci_apc_agent_link_down;
Dan Williams6f231dd2011-07-02 22:56:22 -0700755
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -0700756 sci_init_timer(&port_agent->timer, apc_agent_timeout);
Dan Williams6f231dd2011-07-02 22:56:22 -0700757 }
758
759 return status;
760}