]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/isci/port_config.c
1cde7b9165f41780b1fd2f705bb35ed1b68b252f
[karo-tx-linux.git] / drivers / scsi / isci / port_config.c
1 /*
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
56 #include "host.h"
57 #include "timers.h"
58
59 #define SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT    (10)
60 #define SCIC_SDS_APC_RECONFIGURATION_TIMEOUT    (10)
61 #define SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION  (100)
62
63 enum SCIC_SDS_APC_ACTIVITY {
64         SCIC_SDS_APC_SKIP_PHY,
65         SCIC_SDS_APC_ADD_PHY,
66         SCIC_SDS_APC_START_TIMER,
67
68         SCIC_SDS_APC_ACTIVITY_MAX
69 };
70
71 /*
72  * ******************************************************************************
73  * General port configuration agent routines
74  * ****************************************************************************** */
75
76 /**
77  *
78  * @address_one: A SAS Address to be compared.
79  * @address_two: A SAS Address to be compared.
80  *
81  * Compare the two SAS Address and if SAS Address One is greater than SAS
82  * Address Two then return > 0 else if SAS Address One is less than SAS Address
83  * Two return < 0 Otherwise they are the same return 0 A signed value of x > 0
84  * > y where x is returned for Address One > Address Two y is returned for
85  * Address One < Address Two 0 is returned ofr Address One = Address Two
86  */
87 static s32 sci_sas_address_compare(
88         struct sci_sas_address address_one,
89         struct sci_sas_address address_two)
90 {
91         if (address_one.high > address_two.high) {
92                 return 1;
93         } else if (address_one.high < address_two.high) {
94                 return -1;
95         } else if (address_one.low > address_two.low) {
96                 return 1;
97         } else if (address_one.low < address_two.low) {
98                 return -1;
99         }
100
101         /* The two SAS Address must be identical */
102         return 0;
103 }
104
105 /**
106  *
107  * @controller: The controller object used for the port search.
108  * @phy: The phy object to match.
109  *
110  * This routine will find a matching port for the phy.  This means that the
111  * port and phy both have the same broadcast sas address and same received sas
112  * address. The port address or the NULL if there is no matching
113  * port. port address if the port can be found to match the phy.
114  * NULL if there is no matching port for the phy.
115  */
116 static struct scic_sds_port *scic_sds_port_configuration_agent_find_port(
117         struct scic_sds_controller *scic,
118         struct scic_sds_phy *phy)
119 {
120         u8 i;
121         struct sci_sas_address port_sas_address;
122         struct sci_sas_address port_attached_device_address;
123         struct sci_sas_address phy_sas_address;
124         struct sci_sas_address phy_attached_device_address;
125
126         /*
127          * Since this phy can be a member of a wide port check to see if one or
128          * more phys match the sent and received SAS address as this phy in which
129          * case it should participate in the same port.
130          */
131         scic_sds_phy_get_sas_address(phy, &phy_sas_address);
132         scic_sds_phy_get_attached_sas_address(phy, &phy_attached_device_address);
133
134         for (i = 0; i < scic->logical_port_entries; i++) {
135                 struct isci_host *ihost = scic_to_ihost(scic);
136                 struct scic_sds_port *sci_port = &ihost->ports[i].sci;
137
138                 scic_sds_port_get_sas_address(sci_port, &port_sas_address);
139                 scic_sds_port_get_attached_sas_address(sci_port, &port_attached_device_address);
140
141                 if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
142                     sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
143                         return sci_port;
144         }
145
146         return NULL;
147 }
148
149 /**
150  *
151  * @controller: This is the controller object that contains the port agent
152  * @port_agent: This is the port configruation agent for the controller.
153  *
154  * This routine will validate the port configuration is correct for the SCU
155  * hardware.  The SCU hardware allows for port configurations as follows. LP0
156  * -> (PE0), (PE0, PE1), (PE0, PE1, PE2, PE3) LP1 -> (PE1) LP2 -> (PE2), (PE2,
157  * PE3) LP3 -> (PE3) enum sci_status SCI_SUCCESS the port configuration is valid for
158  * this port configuration agent. SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
159  * the port configuration is not valid for this port configuration agent.
160  */
161 static enum sci_status scic_sds_port_configuration_agent_validate_ports(
162         struct scic_sds_controller *controller,
163         struct scic_sds_port_configuration_agent *port_agent)
164 {
165         struct isci_host *ihost = scic_to_ihost(controller);
166         struct sci_sas_address first_address;
167         struct sci_sas_address second_address;
168
169         /*
170          * Sanity check the max ranges for all the phys the max index
171          * is always equal to the port range index */
172         if (port_agent->phy_valid_port_range[0].max_index != 0 ||
173             port_agent->phy_valid_port_range[1].max_index != 1 ||
174             port_agent->phy_valid_port_range[2].max_index != 2 ||
175             port_agent->phy_valid_port_range[3].max_index != 3)
176                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
177
178         /*
179          * This is a request to configure a single x4 port or at least attempt
180          * to make all the phys into a single port */
181         if (port_agent->phy_valid_port_range[0].min_index == 0 &&
182             port_agent->phy_valid_port_range[1].min_index == 0 &&
183             port_agent->phy_valid_port_range[2].min_index == 0 &&
184             port_agent->phy_valid_port_range[3].min_index == 0)
185                 return SCI_SUCCESS;
186
187         /*
188          * This is a degenerate case where phy 1 and phy 2 are assigned
189          * to the same port this is explicitly disallowed by the hardware
190          * unless they are part of the same x4 port and this condition was
191          * already checked above. */
192         if (port_agent->phy_valid_port_range[2].min_index == 1) {
193                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
194         }
195
196         /*
197          * PE0 and PE3 can never have the same SAS Address unless they
198          * are part of the same x4 wide port and we have already checked
199          * for this condition. */
200         scic_sds_phy_get_sas_address(&ihost->phys[0].sci, &first_address);
201         scic_sds_phy_get_sas_address(&ihost->phys[3].sci, &second_address);
202
203         if (sci_sas_address_compare(first_address, second_address) == 0) {
204                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
205         }
206
207         /*
208          * PE0 and PE1 are configured into a 2x1 ports make sure that the
209          * SAS Address for PE0 and PE2 are different since they can not be
210          * part of the same port. */
211         if (port_agent->phy_valid_port_range[0].min_index == 0 &&
212             port_agent->phy_valid_port_range[1].min_index == 1) {
213                 scic_sds_phy_get_sas_address(&ihost->phys[0].sci, &first_address);
214                 scic_sds_phy_get_sas_address(&ihost->phys[2].sci, &second_address);
215
216                 if (sci_sas_address_compare(first_address, second_address) == 0) {
217                         return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
218                 }
219         }
220
221         /*
222          * PE2 and PE3 are configured into a 2x1 ports make sure that the
223          * SAS Address for PE1 and PE3 are different since they can not be
224          * part of the same port. */
225         if (port_agent->phy_valid_port_range[2].min_index == 2 &&
226             port_agent->phy_valid_port_range[3].min_index == 3) {
227                 scic_sds_phy_get_sas_address(&ihost->phys[1].sci, &first_address);
228                 scic_sds_phy_get_sas_address(&ihost->phys[3].sci, &second_address);
229
230                 if (sci_sas_address_compare(first_address, second_address) == 0) {
231                         return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
232                 }
233         }
234
235         return SCI_SUCCESS;
236 }
237
238 /*
239  * ******************************************************************************
240  * Manual port configuration agent routines
241  * ****************************************************************************** */
242
243 /**
244  *
245  *
246  * This routine will verify that all of the phys in the same port are using the
247  * same SAS address.
248  */
249 static enum sci_status scic_sds_mpc_agent_validate_phy_configuration(
250         struct scic_sds_controller *controller,
251         struct scic_sds_port_configuration_agent *port_agent)
252 {
253         struct isci_host *ihost = scic_to_ihost(controller);
254         u32 phy_mask;
255         u32 assigned_phy_mask;
256         struct sci_sas_address sas_address;
257         struct sci_sas_address phy_assigned_address;
258         u8 port_index;
259         u8 phy_index;
260
261         assigned_phy_mask = 0;
262         sas_address.high = 0;
263         sas_address.low = 0;
264
265         for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
266                 phy_mask = controller->oem_parameters.sds1.ports[port_index].phy_mask;
267
268                 if (!phy_mask)
269                         continue;
270                 /*
271                  * Make sure that one or more of the phys were not already assinged to
272                  * a different port. */
273                 if ((phy_mask & ~assigned_phy_mask) == 0) {
274                         return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
275                 }
276
277                 /* Find the starting phy index for this round through the loop */
278                 for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
279                         if ((phy_mask & (1 << phy_index)) == 0)
280                                 continue;
281                         scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
282                                                      &sas_address);
283
284                         /*
285                          * The phy_index can be used as the starting point for the
286                          * port range since the hardware starts all logical ports
287                          * the same as the PE index. */
288                         port_agent->phy_valid_port_range[phy_index].min_index = port_index;
289                         port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
290
291                         if (phy_index != port_index) {
292                                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
293                         }
294
295                         break;
296                 }
297
298                 /*
299                  * See how many additional phys are being added to this logical port.
300                  * Note: We have not moved the current phy_index so we will actually
301                  *       compare the startting phy with itself.
302                  *       This is expected and required to add the phy to the port. */
303                 while (phy_index < SCI_MAX_PHYS) {
304                         if ((phy_mask & (1 << phy_index)) == 0)
305                                 continue;
306                         scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
307                                                      &phy_assigned_address);
308
309                         if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
310                                 /*
311                                  * The phy mask specified that this phy is part of the same port
312                                  * as the starting phy and it is not so fail this configuration */
313                                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
314                         }
315
316                         port_agent->phy_valid_port_range[phy_index].min_index = port_index;
317                         port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
318
319                         scic_sds_port_add_phy(&ihost->ports[port_index].sci,
320                                               &ihost->phys[phy_index].sci);
321
322                         assigned_phy_mask |= (1 << phy_index);
323                 }
324
325                 phy_index++;
326         }
327
328         return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
329 }
330
331 static void mpc_agent_timeout(unsigned long data)
332 {
333         u8 index;
334         struct sci_timer *tmr = (struct sci_timer *)data;
335         struct scic_sds_port_configuration_agent *port_agent;
336         struct scic_sds_controller *scic;
337         struct isci_host *ihost;
338         unsigned long flags;
339         u16 configure_phy_mask;
340
341         port_agent = container_of(tmr, typeof(*port_agent), timer);
342         scic = container_of(port_agent, typeof(*scic), port_agent);
343         ihost = scic_to_ihost(scic);
344
345         spin_lock_irqsave(&ihost->scic_lock, flags);
346
347         if (tmr->cancel)
348                 goto done;
349
350         port_agent->timer_pending = false;
351
352         /* Find the mask of phys that are reported read but as yet unconfigured into a port */
353         configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
354
355         for (index = 0; index < SCI_MAX_PHYS; index++) {
356                 struct scic_sds_phy *sci_phy = &ihost->phys[index].sci;
357
358                 if (configure_phy_mask & (1 << index)) {
359                         port_agent->link_up_handler(scic, port_agent,
360                                                     phy_get_non_dummy_port(sci_phy),
361                                                     sci_phy);
362                 }
363         }
364
365 done:
366         spin_unlock_irqrestore(&ihost->scic_lock, flags);
367 }
368
369 /**
370  *
371  * @controller: This is the controller object that receives the link up
372  *    notification.
373  * @port: This is the port object associated with the phy.  If the is no
374  *    associated port this is an NULL.
375  * @phy: This is the phy object which has gone ready.
376  *
377  * This method handles the manual port configuration link up notifications.
378  * Since all ports and phys are associate at initialization time we just turn
379  * around and notifiy the port object that there is a link up.  If this PHY is
380  * not associated with a port there is no action taken. Is it possible to get a
381  * link up notification from a phy that has no assocoated port?
382  */
383 static void scic_sds_mpc_agent_link_up(
384         struct scic_sds_controller *controller,
385         struct scic_sds_port_configuration_agent *port_agent,
386         struct scic_sds_port *port,
387         struct scic_sds_phy *phy)
388 {
389         /*
390          * If the port has an invalid handle then the phy was not assigned to
391          * a port.  This is because the phy was not given the same SAS Address
392          * as the other PHYs in the port. */
393         if (port != NULL) {
394                 port_agent->phy_ready_mask |= (1 << scic_sds_phy_get_index(phy));
395
396                 scic_sds_port_link_up(port, phy);
397
398                 if ((port->active_phy_mask & (1 << scic_sds_phy_get_index(phy))) != 0) {
399                         port_agent->phy_configured_mask |= (1 << scic_sds_phy_get_index(phy));
400                 }
401         }
402 }
403
404 /**
405  *
406  * @controller: This is the controller object that receives the link down
407  *    notification.
408  * @port: This is the port object associated with the phy.  If the is no
409  *    associated port this is an NULL.  The port is an invalid
410  *    handle only if the phy was never port of this port.  This happens when
411  *    the phy is not broadcasting the same SAS address as the other phys in the
412  *    assigned port.
413  * @phy: This is the phy object which has gone link down.
414  *
415  * This function handles the manual port configuration link down notifications.
416  * Since all ports and phys are associated at initialization time we just turn
417  * around and notifiy the port object of the link down event.  If this PHY is
418  * not associated with a port there is no action taken. Is it possible to get a
419  * link down notification from a phy that has no assocoated port?
420  */
421 static void scic_sds_mpc_agent_link_down(
422         struct scic_sds_controller *scic,
423         struct scic_sds_port_configuration_agent *port_agent,
424         struct scic_sds_port *sci_port,
425         struct scic_sds_phy *sci_phy)
426 {
427         if (sci_port != NULL) {
428                 /*
429                  * If we can form a new port from the remainder of the phys
430                  * then we want to start the timer to allow the SCI User to
431                  * cleanup old devices and rediscover the port before
432                  * rebuilding the port with the phys that remain in the ready
433                  * state.
434                  */
435                 port_agent->phy_ready_mask &=
436                         ~(1 << scic_sds_phy_get_index(sci_phy));
437                 port_agent->phy_configured_mask &=
438                         ~(1 << scic_sds_phy_get_index(sci_phy));
439
440                 /*
441                  * Check to see if there are more phys waiting to be
442                  * configured into a port. If there are allow the SCI User
443                  * to tear down this port, if necessary, and then reconstruct
444                  * the port after the timeout.
445                  */
446                 if ((port_agent->phy_configured_mask == 0x0000) &&
447                     (port_agent->phy_ready_mask != 0x0000) &&
448                     !port_agent->timer_pending) {
449                         port_agent->timer_pending = true;
450
451                         sci_mod_timer(&port_agent->timer,
452                                       SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
453                 }
454
455                 scic_sds_port_link_down(sci_port, sci_phy);
456         }
457 }
458
459 /*
460  * ******************************************************************************
461  * Automatic port configuration agent routines
462  * ****************************************************************************** */
463
464 /**
465  *
466  *
467  * This routine will verify that the phys are assigned a valid SAS address for
468  * automatic port configuration mode.
469  */
470 static enum sci_status scic_sds_apc_agent_validate_phy_configuration(
471         struct scic_sds_controller *controller,
472         struct scic_sds_port_configuration_agent *port_agent)
473 {
474         u8 phy_index;
475         u8 port_index;
476         struct sci_sas_address sas_address;
477         struct sci_sas_address phy_assigned_address;
478         struct isci_host *ihost = scic_to_ihost(controller);
479
480         phy_index = 0;
481
482         while (phy_index < SCI_MAX_PHYS) {
483                 port_index = phy_index;
484
485                 /* Get the assigned SAS Address for the first PHY on the controller. */
486                 scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
487                                             &sas_address);
488
489                 while (++phy_index < SCI_MAX_PHYS) {
490                         scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
491                                                      &phy_assigned_address);
492
493                         /* Verify each of the SAS address are all the same for every PHY */
494                         if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
495                                 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
496                                 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
497                         } else {
498                                 port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
499                                 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
500                                 break;
501                         }
502                 }
503         }
504
505         return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
506 }
507
508 /**
509  *
510  * @controller: This is the controller object that receives the link up
511  *    notification.
512  * @phy: This is the phy object which has gone link up.
513  *
514  * This method handles the automatic port configuration for link up
515  * notifications.
516  */
517 static void scic_sds_apc_agent_configure_ports(
518         struct scic_sds_controller *controller,
519         struct scic_sds_port_configuration_agent *port_agent,
520         struct scic_sds_phy *phy,
521         bool start_timer)
522 {
523         u8 port_index;
524         enum sci_status status;
525         struct scic_sds_port *port;
526         enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
527         struct isci_host *ihost = scic_to_ihost(controller);
528
529         port = scic_sds_port_configuration_agent_find_port(controller, phy);
530
531         if (port != NULL) {
532                 if (scic_sds_port_is_valid_phy_assignment(port, phy->phy_index))
533                         apc_activity = SCIC_SDS_APC_ADD_PHY;
534                 else
535                         apc_activity = SCIC_SDS_APC_SKIP_PHY;
536         } else {
537                 /*
538                  * There is no matching Port for this PHY so lets search through the
539                  * Ports and see if we can add the PHY to its own port or maybe start
540                  * the timer and wait to see if a wider port can be made.
541                  *
542                  * Note the break when we reach the condition of the port id == phy id */
543                 for (
544                         port_index = port_agent->phy_valid_port_range[phy->phy_index].min_index;
545                         port_index <= port_agent->phy_valid_port_range[phy->phy_index].max_index;
546                         port_index++
547                         ) {
548
549                         port = &ihost->ports[port_index].sci;
550
551                         /* First we must make sure that this PHY can be added to this Port. */
552                         if (scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)) {
553                                 /*
554                                  * Port contains a PHY with a greater PHY ID than the current
555                                  * PHY that has gone link up.  This phy can not be part of any
556                                  * port so skip it and move on. */
557                                 if (port->active_phy_mask > (1 << phy->phy_index)) {
558                                         apc_activity = SCIC_SDS_APC_SKIP_PHY;
559                                         break;
560                                 }
561
562                                 /*
563                                  * We have reached the end of our Port list and have not found
564                                  * any reason why we should not either add the PHY to the port
565                                  * or wait for more phys to become active. */
566                                 if (port->physical_port_index == phy->phy_index) {
567                                         /*
568                                          * The Port either has no active PHYs.
569                                          * Consider that if the port had any active PHYs we would have
570                                          * or active PHYs with
571                                          * a lower PHY Id than this PHY. */
572                                         if (apc_activity != SCIC_SDS_APC_START_TIMER) {
573                                                 apc_activity = SCIC_SDS_APC_ADD_PHY;
574                                         }
575
576                                         break;
577                                 }
578
579                                 /*
580                                  * The current Port has no active PHYs and this PHY could be part
581                                  * of this Port.  Since we dont know as yet setup to start the
582                                  * timer and see if there is a better configuration. */
583                                 if (port->active_phy_mask == 0) {
584                                         apc_activity = SCIC_SDS_APC_START_TIMER;
585                                 }
586                         } else if (port->active_phy_mask != 0) {
587                                 /*
588                                  * The Port has an active phy and the current Phy can not
589                                  * participate in this port so skip the PHY and see if
590                                  * there is a better configuration. */
591                                 apc_activity = SCIC_SDS_APC_SKIP_PHY;
592                         }
593                 }
594         }
595
596         /*
597          * Check to see if the start timer operations should instead map to an
598          * add phy operation.  This is caused because we have been waiting to
599          * add a phy to a port but could not becuase the automatic port
600          * configuration engine had a choice of possible ports for the phy.
601          * Since we have gone through a timeout we are going to restrict the
602          * choice to the smallest possible port. */
603         if (
604                 (start_timer == false)
605                 && (apc_activity == SCIC_SDS_APC_START_TIMER)
606                 ) {
607                 apc_activity = SCIC_SDS_APC_ADD_PHY;
608         }
609
610         switch (apc_activity) {
611         case SCIC_SDS_APC_ADD_PHY:
612                 status = scic_sds_port_add_phy(port, phy);
613
614                 if (status == SCI_SUCCESS) {
615                         port_agent->phy_configured_mask |= (1 << phy->phy_index);
616                 }
617                 break;
618
619         case SCIC_SDS_APC_START_TIMER:
620                 /*
621                  * This can occur for either a link down event, or a link
622                  * up event where we cannot yet tell the port to which a
623                  * phy belongs.
624                  */
625                 if (port_agent->timer_pending)
626                         sci_del_timer(&port_agent->timer);
627
628                 port_agent->timer_pending = true;
629                 sci_mod_timer(&port_agent->timer,
630                               SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
631                 break;
632
633         case SCIC_SDS_APC_SKIP_PHY:
634         default:
635                 /* do nothing the PHY can not be made part of a port at this time. */
636                 break;
637         }
638 }
639
640 /**
641  * scic_sds_apc_agent_link_up - handle apc link up events
642  * @scic: This is the controller object that receives the link up
643  *    notification.
644  * @sci_port: This is the port object associated with the phy.  If the is no
645  *    associated port this is an NULL.
646  * @sci_phy: This is the phy object which has gone link up.
647  *
648  * This method handles the automatic port configuration for link up
649  * notifications. Is it possible to get a link down notification from a phy
650  * that has no assocoated port?
651  */
652 static void scic_sds_apc_agent_link_up(struct scic_sds_controller *scic,
653                                        struct scic_sds_port_configuration_agent *port_agent,
654                                        struct scic_sds_port *sci_port,
655                                        struct scic_sds_phy *sci_phy)
656 {
657         u8 phy_index  = sci_phy->phy_index;
658
659         if (!sci_port) {
660                 /* the phy is not the part of this port */
661                 port_agent->phy_ready_mask |= 1 << phy_index;
662                 scic_sds_apc_agent_configure_ports(scic, port_agent, sci_phy, true);
663         } else {
664                 /* the phy is already the part of the port */
665                 u32 port_state = sci_port->state_machine.current_state_id;
666
667                 /* if the PORT'S state is resetting then the link up is from
668                  * port hard reset in this case, we need to tell the port
669                  * that link up is recieved
670                  */
671                 BUG_ON(port_state != SCI_BASE_PORT_STATE_RESETTING);
672                 port_agent->phy_ready_mask |= 1 << phy_index;
673                 scic_sds_port_link_up(sci_port, sci_phy);
674         }
675 }
676
677 /**
678  *
679  * @controller: This is the controller object that receives the link down
680  *    notification.
681  * @port: This is the port object associated with the phy.  If the is no
682  *    associated port this is an NULL.
683  * @phy: This is the phy object which has gone link down.
684  *
685  * This method handles the automatic port configuration link down
686  * notifications. not associated with a port there is no action taken. Is it
687  * possible to get a link down notification from a phy that has no assocoated
688  * port?
689  */
690 static void scic_sds_apc_agent_link_down(
691         struct scic_sds_controller *controller,
692         struct scic_sds_port_configuration_agent *port_agent,
693         struct scic_sds_port *port,
694         struct scic_sds_phy *phy)
695 {
696         port_agent->phy_ready_mask &= ~(1 << scic_sds_phy_get_index(phy));
697
698         if (port != NULL) {
699                 if (port_agent->phy_configured_mask & (1 << phy->phy_index)) {
700                         enum sci_status status;
701
702                         status = scic_sds_port_remove_phy(port, phy);
703
704                         if (status == SCI_SUCCESS) {
705                                 port_agent->phy_configured_mask &= ~(1 << phy->phy_index);
706                         }
707                 }
708         }
709 }
710
711 /* configure the phys into ports when the timer fires */
712 static void apc_agent_timeout(unsigned long data)
713 {
714         u32 index;
715         struct sci_timer *tmr = (struct sci_timer *)data;
716         struct scic_sds_port_configuration_agent *port_agent;
717         struct scic_sds_controller *scic;
718         struct isci_host *ihost;
719         unsigned long flags;
720         u16 configure_phy_mask;
721
722         port_agent = container_of(tmr, typeof(*port_agent), timer);
723         scic = container_of(port_agent, typeof(*scic), port_agent);
724         ihost = scic_to_ihost(scic);
725
726         spin_lock_irqsave(&ihost->scic_lock, flags);
727
728         if (tmr->cancel)
729                 goto done;
730
731         port_agent->timer_pending = false;
732
733         configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
734
735         if (!configure_phy_mask)
736                 return;
737
738         for (index = 0; index < SCI_MAX_PHYS; index++) {
739                 if ((configure_phy_mask & (1 << index)) == 0)
740                         continue;
741
742                 scic_sds_apc_agent_configure_ports(scic, port_agent,
743                                                    &ihost->phys[index].sci, false);
744         }
745
746 done:
747         spin_unlock_irqrestore(&ihost->scic_lock, flags);
748 }
749
750 /*
751  * ******************************************************************************
752  * Public port configuration agent routines
753  * ****************************************************************************** */
754
755 /**
756  *
757  *
758  * This method will construct the port configuration agent for operation. This
759  * call is universal for both manual port configuration and automatic port
760  * configuration modes.
761  */
762 void scic_sds_port_configuration_agent_construct(
763         struct scic_sds_port_configuration_agent *port_agent)
764 {
765         u32 index;
766
767         port_agent->phy_configured_mask = 0x00;
768         port_agent->phy_ready_mask = 0x00;
769
770         port_agent->link_up_handler = NULL;
771         port_agent->link_down_handler = NULL;
772
773         port_agent->timer_pending = false;
774
775         for (index = 0; index < SCI_MAX_PORTS; index++) {
776                 port_agent->phy_valid_port_range[index].min_index = 0;
777                 port_agent->phy_valid_port_range[index].max_index = 0;
778         }
779 }
780
781 enum sci_status scic_sds_port_configuration_agent_initialize(
782         struct scic_sds_controller *scic,
783         struct scic_sds_port_configuration_agent *port_agent)
784 {
785         enum sci_status status;
786         enum scic_port_configuration_mode mode;
787
788         mode = scic->oem_parameters.sds1.controller.mode_type;
789
790         if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
791                 status = scic_sds_mpc_agent_validate_phy_configuration(
792                                 scic, port_agent);
793
794                 port_agent->link_up_handler = scic_sds_mpc_agent_link_up;
795                 port_agent->link_down_handler = scic_sds_mpc_agent_link_down;
796
797                 sci_init_timer(&port_agent->timer, mpc_agent_timeout);
798         } else {
799                 status = scic_sds_apc_agent_validate_phy_configuration(
800                                 scic, port_agent);
801
802                 port_agent->link_up_handler = scic_sds_apc_agent_link_up;
803                 port_agent->link_down_handler = scic_sds_apc_agent_link_down;
804
805                 sci_init_timer(&port_agent->timer, apc_agent_timeout);
806         }
807
808         return status;
809 }