]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/isci/port_config.c
ca76f493c30d3f715e32a7efcec0ab45b20ffd3b
[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 /**
332  *
333  *
334  * This timer routine is used to allow the SCI User to rediscover or change
335  * device objects before a new series of link up notifications because a link
336  * down has allowed a better port configuration.
337  */
338 static void scic_sds_mpc_agent_timeout_handler(void *object)
339 {
340         u8 index;
341         struct scic_sds_controller *scic = object;
342         struct isci_host *ihost = scic_to_ihost(scic);
343         struct scic_sds_port_configuration_agent *port_agent = &scic->port_agent;
344         u16 configure_phy_mask;
345
346         port_agent->timer_pending = false;
347
348         /* Find the mask of phys that are reported read but as yet unconfigured into a port */
349         configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
350
351         for (index = 0; index < SCI_MAX_PHYS; index++) {
352                 struct scic_sds_phy *sci_phy = &ihost->phys[index].sci;
353
354                 if (configure_phy_mask & (1 << index)) {
355                         port_agent->link_up_handler(scic, port_agent,
356                                                     phy_get_non_dummy_port(sci_phy),
357                                                     sci_phy);
358                 }
359         }
360 }
361
362 /**
363  *
364  * @controller: This is the controller object that receives the link up
365  *    notification.
366  * @port: This is the port object associated with the phy.  If the is no
367  *    associated port this is an NULL.
368  * @phy: This is the phy object which has gone ready.
369  *
370  * This method handles the manual port configuration link up notifications.
371  * Since all ports and phys are associate at initialization time we just turn
372  * around and notifiy the port object that there is a link up.  If this PHY is
373  * not associated with a port there is no action taken. Is it possible to get a
374  * link up notification from a phy that has no assocoated port?
375  */
376 static void scic_sds_mpc_agent_link_up(
377         struct scic_sds_controller *controller,
378         struct scic_sds_port_configuration_agent *port_agent,
379         struct scic_sds_port *port,
380         struct scic_sds_phy *phy)
381 {
382         /*
383          * If the port has an invalid handle then the phy was not assigned to
384          * a port.  This is because the phy was not given the same SAS Address
385          * as the other PHYs in the port. */
386         if (port != NULL) {
387                 port_agent->phy_ready_mask |= (1 << scic_sds_phy_get_index(phy));
388
389                 scic_sds_port_link_up(port, phy);
390
391                 if ((port->active_phy_mask & (1 << scic_sds_phy_get_index(phy))) != 0) {
392                         port_agent->phy_configured_mask |= (1 << scic_sds_phy_get_index(phy));
393                 }
394         }
395 }
396
397 /**
398  *
399  * @controller: This is the controller object that receives the link down
400  *    notification.
401  * @port: This is the port object associated with the phy.  If the is no
402  *    associated port this is an NULL.  The port is an invalid
403  *    handle only if the phy was never port of this port.  This happens when
404  *    the phy is not broadcasting the same SAS address as the other phys in the
405  *    assigned port.
406  * @phy: This is the phy object which has gone link down.
407  *
408  * This function handles the manual port configuration link down notifications.
409  * Since all ports and phys are associated at initialization time we just turn
410  * around and notifiy the port object of the link down event.  If this PHY is
411  * not associated with a port there is no action taken. Is it possible to get a
412  * link down notification from a phy that has no assocoated port?
413  */
414 static void scic_sds_mpc_agent_link_down(
415         struct scic_sds_controller *scic,
416         struct scic_sds_port_configuration_agent *port_agent,
417         struct scic_sds_port *sci_port,
418         struct scic_sds_phy *sci_phy)
419 {
420         if (sci_port != NULL) {
421                 /*
422                  * If we can form a new port from the remainder of the phys
423                  * then we want to start the timer to allow the SCI User to
424                  * cleanup old devices and rediscover the port before
425                  * rebuilding the port with the phys that remain in the ready
426                  * state.
427                  */
428                 port_agent->phy_ready_mask &=
429                         ~(1 << scic_sds_phy_get_index(sci_phy));
430                 port_agent->phy_configured_mask &=
431                         ~(1 << scic_sds_phy_get_index(sci_phy));
432
433                 /*
434                  * Check to see if there are more phys waiting to be
435                  * configured into a port. If there are allow the SCI User
436                  * to tear down this port, if necessary, and then reconstruct
437                  * the port after the timeout.
438                  */
439                 if ((port_agent->phy_configured_mask == 0x0000) &&
440                     (port_agent->phy_ready_mask != 0x0000) &&
441                     !port_agent->timer_pending) {
442                         port_agent->timer_pending = true;
443
444                         isci_timer_start(port_agent->timer,
445                                          SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
446                 }
447
448                 scic_sds_port_link_down(sci_port, sci_phy);
449         }
450 }
451
452 /*
453  * ******************************************************************************
454  * Automatic port configuration agent routines
455  * ****************************************************************************** */
456
457 /**
458  *
459  *
460  * This routine will verify that the phys are assigned a valid SAS address for
461  * automatic port configuration mode.
462  */
463 static enum sci_status scic_sds_apc_agent_validate_phy_configuration(
464         struct scic_sds_controller *controller,
465         struct scic_sds_port_configuration_agent *port_agent)
466 {
467         u8 phy_index;
468         u8 port_index;
469         struct sci_sas_address sas_address;
470         struct sci_sas_address phy_assigned_address;
471         struct isci_host *ihost = scic_to_ihost(controller);
472
473         phy_index = 0;
474
475         while (phy_index < SCI_MAX_PHYS) {
476                 port_index = phy_index;
477
478                 /* Get the assigned SAS Address for the first PHY on the controller. */
479                 scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
480                                             &sas_address);
481
482                 while (++phy_index < SCI_MAX_PHYS) {
483                         scic_sds_phy_get_sas_address(&ihost->phys[phy_index].sci,
484                                                      &phy_assigned_address);
485
486                         /* Verify each of the SAS address are all the same for every PHY */
487                         if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
488                                 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
489                                 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
490                         } else {
491                                 port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
492                                 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
493                                 break;
494                         }
495                 }
496         }
497
498         return scic_sds_port_configuration_agent_validate_ports(controller, port_agent);
499 }
500
501 /**
502  *
503  * @controller: This is the controller that to which the port agent is assigned.
504  * @port_agent: This is the port agent that is requesting the timer start
505  *    operation.
506  * @phy: This is the phy that has caused the timer operation to be scheduled.
507  *
508  * This routine will restart the automatic port configuration timeout timer for
509  * the next time period.  This could be caused by either a link down event or a
510  * link up event where we can not yet tell to which port a phy belongs.
511  */
512 static inline void scic_sds_apc_agent_start_timer(
513         struct scic_sds_controller *scic,
514         struct scic_sds_port_configuration_agent *port_agent,
515         struct scic_sds_phy *sci_phy,
516         u32 timeout)
517 {
518         if (port_agent->timer_pending)
519                 isci_timer_stop(port_agent->timer);
520
521         port_agent->timer_pending = true;
522
523         isci_timer_start(port_agent->timer, timeout);
524 }
525
526 /**
527  *
528  * @controller: This is the controller object that receives the link up
529  *    notification.
530  * @phy: This is the phy object which has gone link up.
531  *
532  * This method handles the automatic port configuration for link up
533  * notifications.
534  */
535 static void scic_sds_apc_agent_configure_ports(
536         struct scic_sds_controller *controller,
537         struct scic_sds_port_configuration_agent *port_agent,
538         struct scic_sds_phy *phy,
539         bool start_timer)
540 {
541         u8 port_index;
542         enum sci_status status;
543         struct scic_sds_port *port;
544         enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
545         struct isci_host *ihost = scic_to_ihost(controller);
546
547         port = scic_sds_port_configuration_agent_find_port(controller, phy);
548
549         if (port != NULL) {
550                 if (scic_sds_port_is_valid_phy_assignment(port, phy->phy_index))
551                         apc_activity = SCIC_SDS_APC_ADD_PHY;
552                 else
553                         apc_activity = SCIC_SDS_APC_SKIP_PHY;
554         } else {
555                 /*
556                  * There is no matching Port for this PHY so lets search through the
557                  * Ports and see if we can add the PHY to its own port or maybe start
558                  * the timer and wait to see if a wider port can be made.
559                  *
560                  * Note the break when we reach the condition of the port id == phy id */
561                 for (
562                         port_index = port_agent->phy_valid_port_range[phy->phy_index].min_index;
563                         port_index <= port_agent->phy_valid_port_range[phy->phy_index].max_index;
564                         port_index++
565                         ) {
566
567                         port = &ihost->ports[port_index].sci;
568
569                         /* First we must make sure that this PHY can be added to this Port. */
570                         if (scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)) {
571                                 /*
572                                  * Port contains a PHY with a greater PHY ID than the current
573                                  * PHY that has gone link up.  This phy can not be part of any
574                                  * port so skip it and move on. */
575                                 if (port->active_phy_mask > (1 << phy->phy_index)) {
576                                         apc_activity = SCIC_SDS_APC_SKIP_PHY;
577                                         break;
578                                 }
579
580                                 /*
581                                  * We have reached the end of our Port list and have not found
582                                  * any reason why we should not either add the PHY to the port
583                                  * or wait for more phys to become active. */
584                                 if (port->physical_port_index == phy->phy_index) {
585                                         /*
586                                          * The Port either has no active PHYs.
587                                          * Consider that if the port had any active PHYs we would have
588                                          * or active PHYs with
589                                          * a lower PHY Id than this PHY. */
590                                         if (apc_activity != SCIC_SDS_APC_START_TIMER) {
591                                                 apc_activity = SCIC_SDS_APC_ADD_PHY;
592                                         }
593
594                                         break;
595                                 }
596
597                                 /*
598                                  * The current Port has no active PHYs and this PHY could be part
599                                  * of this Port.  Since we dont know as yet setup to start the
600                                  * timer and see if there is a better configuration. */
601                                 if (port->active_phy_mask == 0) {
602                                         apc_activity = SCIC_SDS_APC_START_TIMER;
603                                 }
604                         } else if (port->active_phy_mask != 0) {
605                                 /*
606                                  * The Port has an active phy and the current Phy can not
607                                  * participate in this port so skip the PHY and see if
608                                  * there is a better configuration. */
609                                 apc_activity = SCIC_SDS_APC_SKIP_PHY;
610                         }
611                 }
612         }
613
614         /*
615          * Check to see if the start timer operations should instead map to an
616          * add phy operation.  This is caused because we have been waiting to
617          * add a phy to a port but could not becuase the automatic port
618          * configuration engine had a choice of possible ports for the phy.
619          * Since we have gone through a timeout we are going to restrict the
620          * choice to the smallest possible port. */
621         if (
622                 (start_timer == false)
623                 && (apc_activity == SCIC_SDS_APC_START_TIMER)
624                 ) {
625                 apc_activity = SCIC_SDS_APC_ADD_PHY;
626         }
627
628         switch (apc_activity) {
629         case SCIC_SDS_APC_ADD_PHY:
630                 status = scic_sds_port_add_phy(port, phy);
631
632                 if (status == SCI_SUCCESS) {
633                         port_agent->phy_configured_mask |= (1 << phy->phy_index);
634                 }
635                 break;
636
637         case SCIC_SDS_APC_START_TIMER:
638                 scic_sds_apc_agent_start_timer(
639                         controller, port_agent, phy, SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION
640                         );
641                 break;
642
643         case SCIC_SDS_APC_SKIP_PHY:
644         default:
645                 /* do nothing the PHY can not be made part of a port at this time. */
646                 break;
647         }
648 }
649
650 /**
651  * scic_sds_apc_agent_link_up - handle apc link up events
652  * @scic: This is the controller object that receives the link up
653  *    notification.
654  * @sci_port: This is the port object associated with the phy.  If the is no
655  *    associated port this is an NULL.
656  * @sci_phy: This is the phy object which has gone link up.
657  *
658  * This method handles the automatic port configuration for link up
659  * notifications. Is it possible to get a link down notification from a phy
660  * that has no assocoated port?
661  */
662 static void scic_sds_apc_agent_link_up(struct scic_sds_controller *scic,
663                                        struct scic_sds_port_configuration_agent *port_agent,
664                                        struct scic_sds_port *sci_port,
665                                        struct scic_sds_phy *sci_phy)
666 {
667         u8 phy_index  = sci_phy->phy_index;
668
669         if (!sci_port) {
670                 /* the phy is not the part of this port */
671                 port_agent->phy_ready_mask |= 1 << phy_index;
672                 scic_sds_apc_agent_configure_ports(scic, port_agent, sci_phy, true);
673         } else {
674                 /* the phy is already the part of the port */
675                 u32 port_state = sci_port->state_machine.current_state_id;
676
677                 /* if the PORT'S state is resetting then the link up is from
678                  * port hard reset in this case, we need to tell the port
679                  * that link up is recieved
680                  */
681                 BUG_ON(port_state != SCI_BASE_PORT_STATE_RESETTING);
682                 port_agent->phy_ready_mask |= 1 << phy_index;
683                 scic_sds_port_link_up(sci_port, sci_phy);
684         }
685 }
686
687 /**
688  *
689  * @controller: This is the controller object that receives the link down
690  *    notification.
691  * @port: This is the port object associated with the phy.  If the is no
692  *    associated port this is an NULL.
693  * @phy: This is the phy object which has gone link down.
694  *
695  * This method handles the automatic port configuration link down
696  * notifications. not associated with a port there is no action taken. Is it
697  * possible to get a link down notification from a phy that has no assocoated
698  * port?
699  */
700 static void scic_sds_apc_agent_link_down(
701         struct scic_sds_controller *controller,
702         struct scic_sds_port_configuration_agent *port_agent,
703         struct scic_sds_port *port,
704         struct scic_sds_phy *phy)
705 {
706         port_agent->phy_ready_mask &= ~(1 << scic_sds_phy_get_index(phy));
707
708         if (port != NULL) {
709                 if (port_agent->phy_configured_mask & (1 << phy->phy_index)) {
710                         enum sci_status status;
711
712                         status = scic_sds_port_remove_phy(port, phy);
713
714                         if (status == SCI_SUCCESS) {
715                                 port_agent->phy_configured_mask &= ~(1 << phy->phy_index);
716                         }
717                 }
718         }
719 }
720
721 /* configure the phys into ports when the timer fires */
722 static void scic_sds_apc_agent_timeout_handler(void *object)
723 {
724         u32 index;
725         struct scic_sds_port_configuration_agent *port_agent;
726         struct scic_sds_controller *scic = object;
727         struct isci_host *ihost = scic_to_ihost(scic);
728         u16 configure_phy_mask;
729
730         port_agent = scic_sds_controller_get_port_configuration_agent(scic);
731
732         port_agent->timer_pending = false;
733
734         configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
735
736         if (!configure_phy_mask)
737                 return;
738
739         for (index = 0; index < SCI_MAX_PHYS; index++) {
740                 if ((configure_phy_mask & (1 << index)) == 0)
741                         continue;
742
743                 scic_sds_apc_agent_configure_ports(scic, port_agent,
744                                                    &ihost->phys[index].sci, false);
745         }
746 }
747
748 /*
749  * ******************************************************************************
750  * Public port configuration agent routines
751  * ****************************************************************************** */
752
753 /**
754  *
755  *
756  * This method will construct the port configuration agent for operation. This
757  * call is universal for both manual port configuration and automatic port
758  * configuration modes.
759  */
760 void scic_sds_port_configuration_agent_construct(
761         struct scic_sds_port_configuration_agent *port_agent)
762 {
763         u32 index;
764
765         port_agent->phy_configured_mask = 0x00;
766         port_agent->phy_ready_mask = 0x00;
767
768         port_agent->link_up_handler = NULL;
769         port_agent->link_down_handler = NULL;
770
771         port_agent->timer_pending = false;
772         port_agent->timer = NULL;
773
774         for (index = 0; index < SCI_MAX_PORTS; index++) {
775                 port_agent->phy_valid_port_range[index].min_index = 0;
776                 port_agent->phy_valid_port_range[index].max_index = 0;
777         }
778 }
779
780 enum sci_status scic_sds_port_configuration_agent_initialize(
781         struct scic_sds_controller *scic,
782         struct scic_sds_port_configuration_agent *port_agent)
783 {
784         enum sci_status status = SCI_SUCCESS;
785         enum scic_port_configuration_mode mode;
786         struct isci_host *ihost = scic_to_ihost(scic);
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                 port_agent->timer = isci_timer_create(
798                                 ihost,
799                                 scic,
800                                 scic_sds_mpc_agent_timeout_handler);
801         } else {
802                 status = scic_sds_apc_agent_validate_phy_configuration(
803                                 scic, port_agent);
804
805                 port_agent->link_up_handler = scic_sds_apc_agent_link_up;
806                 port_agent->link_down_handler = scic_sds_apc_agent_link_down;
807
808                 port_agent->timer = isci_timer_create(
809                                 ihost,
810                                 scic,
811                                 scic_sds_apc_agent_timeout_handler);
812         }
813
814         /* Make sure we have actually gotten a timer */
815         if ((status == SCI_SUCCESS) && (port_agent->timer == NULL)) {
816                 dev_err(scic_to_dev(scic),
817                         "%s: Controller 0x%p automatic port configuration "
818                         "agent could not get timer.\n",
819                         __func__,
820                         scic);
821
822                 status = SCI_FAILURE;
823         }
824
825         return status;
826 }