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