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