]> Pileus Git - ~andy/linux/blob - drivers/scsi/isci/core/scic_sds_port.c
isci: Removed sci_base_object from scic_sds_controller.
[~andy/linux] / drivers / scsi / isci / core / scic_sds_port.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 "intel_sas.h"
57 #include "scic_controller.h"
58 #include "scic_phy.h"
59 #include "scic_port.h"
60 #include "scic_sds_controller.h"
61 #include "scic_sds_phy.h"
62 #include "scic_sds_port.h"
63 #include "remote_device.h"
64 #include "remote_node_context.h"
65 #include "scic_sds_request.h"
66 #include "sci_environment.h"
67 #include "scu_registers.h"
68
69 #define SCIC_SDS_PORT_MIN_TIMER_COUNT  (SCI_MAX_PORTS)
70 #define SCIC_SDS_PORT_MAX_TIMER_COUNT  (SCI_MAX_PORTS)
71
72 #define SCIC_SDS_PORT_HARD_RESET_TIMEOUT  (1000)
73 #define SCU_DUMMY_INDEX    (0xFFFF)
74
75
76 /**
77  *
78  * @sci_port: This is the port object to which the phy is being assigned.
79  * @phy_index: This is the phy index that is being assigned to the port.
80  *
81  * This method will return a true value if the specified phy can be assigned to
82  * this port The following is a list of phys for each port that are allowed: -
83  * Port 0 - 3 2 1 0 - Port 1 -     1 - Port 2 - 3 2 - Port 3 - 3 This method
84  * doesn't preclude all configurations.  It merely ensures that a phy is part
85  * of the allowable set of phy identifiers for that port.  For example, one
86  * could assign phy 3 to port 0 and no other phys.  Please refer to
87  * scic_sds_port_is_phy_mask_valid() for information regarding whether the
88  * phy_mask for a port can be supported. bool true if this is a valid phy
89  * assignment for the port false if this is not a valid phy assignment for the
90  * port
91  */
92 bool scic_sds_port_is_valid_phy_assignment(
93         struct scic_sds_port *sci_port,
94         u32 phy_index)
95 {
96         /* Initialize to invalid value. */
97         u32 existing_phy_index = SCI_MAX_PHYS;
98         u32 index;
99
100         if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
101                 return false;
102         }
103
104         if (sci_port->physical_port_index == 3 && phy_index != 3) {
105                 return false;
106         }
107
108         if (
109                 (sci_port->physical_port_index == 2)
110                 && ((phy_index == 0) || (phy_index == 1))
111                 ) {
112                 return false;
113         }
114
115         for (index = 0; index < SCI_MAX_PHYS; index++) {
116                 if ((sci_port->phy_table[index] != NULL)
117                     && (index != phy_index)) {
118                         existing_phy_index = index;
119                 }
120         }
121
122         /*
123          * Ensure that all of the phys in the port are capable of
124          * operating at the same maximum link rate. */
125         if (
126                 (existing_phy_index < SCI_MAX_PHYS)
127                 && (sci_port->owning_controller->user_parameters.sds1.phys[
128                             phy_index].max_speed_generation !=
129                     sci_port->owning_controller->user_parameters.sds1.phys[
130                             existing_phy_index].max_speed_generation)
131                 )
132                 return false;
133
134         return true;
135 }
136
137 /**
138  * This method requests a list (mask) of the phys contained in the supplied SAS
139  *    port.
140  * @sci_port: a handle corresponding to the SAS port for which to return the
141  *    phy mask.
142  *
143  * Return a bit mask indicating which phys are a part of this port. Each bit
144  * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
145  */
146 static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
147 {
148         u32 index;
149         u32 mask;
150
151         mask = 0;
152
153         for (index = 0; index < SCI_MAX_PHYS; index++) {
154                 if (sci_port->phy_table[index] != NULL) {
155                         mask |= (1 << index);
156                 }
157         }
158
159         return mask;
160 }
161
162 /**
163  *
164  * @sci_port: This is the port object for which to determine if the phy mask
165  *    can be supported.
166  *
167  * This method will return a true value if the port's phy mask can be supported
168  * by the SCU. The following is a list of valid PHY mask configurations for
169  * each port: - Port 0 - [[3  2] 1] 0 - Port 1 -        [1] - Port 2 - [[3] 2]
170  * - Port 3 -  [3] This method returns a boolean indication specifying if the
171  * phy mask can be supported. true if this is a valid phy assignment for the
172  * port false if this is not a valid phy assignment for the port
173  */
174 static bool scic_sds_port_is_phy_mask_valid(
175         struct scic_sds_port *sci_port,
176         u32 phy_mask)
177 {
178         if (sci_port->physical_port_index == 0) {
179                 if (((phy_mask & 0x0F) == 0x0F)
180                     || ((phy_mask & 0x03) == 0x03)
181                     || ((phy_mask & 0x01) == 0x01)
182                     || (phy_mask == 0))
183                         return true;
184         } else if (sci_port->physical_port_index == 1) {
185                 if (((phy_mask & 0x02) == 0x02)
186                     || (phy_mask == 0))
187                         return true;
188         } else if (sci_port->physical_port_index == 2) {
189                 if (((phy_mask & 0x0C) == 0x0C)
190                     || ((phy_mask & 0x04) == 0x04)
191                     || (phy_mask == 0))
192                         return true;
193         } else if (sci_port->physical_port_index == 3) {
194                 if (((phy_mask & 0x08) == 0x08)
195                     || (phy_mask == 0))
196                         return true;
197         }
198
199         return false;
200 }
201
202 /**
203  *
204  * @sci_port: This parameter specifies the port from which to return a
205  *    connected phy.
206  *
207  * This method retrieves a currently active (i.e. connected) phy contained in
208  * the port.  Currently, the lowest order phy that is connected is returned.
209  * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
210  * returned if there are no currently active (i.e. connected to a remote end
211  * point) phys contained in the port. All other values specify a struct scic_sds_phy
212  * object that is active in the port.
213  */
214 static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
215         struct scic_sds_port *sci_port
216         ) {
217         u32 index;
218         struct scic_sds_phy *phy;
219
220         for (index = 0; index < SCI_MAX_PHYS; index++) {
221                 /*
222                  * Ensure that the phy is both part of the port and currently
223                  * connected to the remote end-point. */
224                 phy = sci_port->phy_table[index];
225                 if (
226                         (phy != NULL)
227                         && scic_sds_port_active_phy(sci_port, phy)
228                         ) {
229                         return phy;
230                 }
231         }
232
233         return NULL;
234 }
235
236 /**
237  * scic_sds_port_set_phy() -
238  * @out]: port The port object to which the phy assignement is being made.
239  * @out]: phy The phy which is being assigned to the port.
240  *
241  * This method attempts to make the assignment of the phy to the port. If
242  * successful the phy is assigned to the ports phy table. bool true if the phy
243  * assignment can be made. false if the phy assignement can not be made. This
244  * is a functional test that only fails if the phy is currently assigned to a
245  * different port.
246  */
247 static enum sci_status scic_sds_port_set_phy(
248         struct scic_sds_port *port,
249         struct scic_sds_phy *phy)
250 {
251         /*
252          * Check to see if we can add this phy to a port
253          * that means that the phy is not part of a port and that the port does
254          * not already have a phy assinged to the phy index. */
255         if (
256                 (port->phy_table[phy->phy_index] == NULL)
257                 && (scic_sds_phy_get_port(phy) == NULL)
258                 && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
259                 ) {
260                 /*
261                  * Phy is being added in the stopped state so we are in MPC mode
262                  * make logical port index = physical port index */
263                 port->logical_port_index = port->physical_port_index;
264                 port->phy_table[phy->phy_index] = phy;
265                 scic_sds_phy_set_port(phy, port);
266
267                 return SCI_SUCCESS;
268         }
269
270         return SCI_FAILURE;
271 }
272
273 /**
274  * scic_sds_port_clear_phy() -
275  * @out]: port The port from which the phy is being cleared.
276  * @out]: phy The phy being cleared from the port.
277  *
278  * This method will clear the phy assigned to this port.  This method fails if
279  * this phy is not currently assinged to this port. bool true if the phy is
280  * removed from the port. false if this phy is not assined to this port.
281  */
282 static enum sci_status scic_sds_port_clear_phy(
283         struct scic_sds_port *port,
284         struct scic_sds_phy *phy)
285 {
286         /* Make sure that this phy is part of this port */
287         if (
288                 (port->phy_table[phy->phy_index] == phy)
289                 && (scic_sds_phy_get_port(phy) == port)
290                 ) {
291                 /* Yep it is assigned to this port so remove it */
292                 scic_sds_phy_set_port(
293                         phy,
294                         &scic_sds_port_get_controller(port)->port_table[SCI_MAX_PORTS]
295                         );
296
297                 port->phy_table[phy->phy_index] = NULL;
298
299                 return SCI_SUCCESS;
300         }
301
302         return SCI_FAILURE;
303 }
304
305 /**
306  * scic_sds_port_add_phy() -
307  * @sci_port: This parameter specifies the port in which the phy will be added.
308  * @sci_phy: This parameter is the phy which is to be added to the port.
309  *
310  * This method will add a PHY to the selected port. This method returns an
311  * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other status
312  * is failre to add the phy to the port.
313  */
314 enum sci_status scic_sds_port_add_phy(
315         struct scic_sds_port *sci_port,
316         struct scic_sds_phy *sci_phy)
317 {
318         return sci_port->state_handlers->add_phy_handler(
319                        sci_port, sci_phy);
320 }
321
322
323 /**
324  * scic_sds_port_remove_phy() -
325  * @sci_port: This parameter specifies the port in which the phy will be added.
326  * @sci_phy: This parameter is the phy which is to be added to the port.
327  *
328  * This method will remove the PHY from the selected PORT. This method returns
329  * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any other
330  * status is failre to add the phy to the port.
331  */
332 enum sci_status scic_sds_port_remove_phy(
333         struct scic_sds_port *sci_port,
334         struct scic_sds_phy *sci_phy)
335 {
336         return sci_port->state_handlers->remove_phy_handler(
337                        sci_port, sci_phy);
338 }
339
340 /**
341  * This method requests the SAS address for the supplied SAS port from the SCI
342  *    implementation.
343  * @sci_port: a handle corresponding to the SAS port for which to return the
344  *    SAS address.
345  * @sas_address: This parameter specifies a pointer to a SAS address structure
346  *    into which the core will copy the SAS address for the port.
347  *
348  */
349 void scic_sds_port_get_sas_address(
350         struct scic_sds_port *sci_port,
351         struct sci_sas_address *sas_address)
352 {
353         u32 index;
354
355         sas_address->high = 0;
356         sas_address->low  = 0;
357
358         for (index = 0; index < SCI_MAX_PHYS; index++) {
359                 if (sci_port->phy_table[index] != NULL) {
360                         scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
361                 }
362         }
363 }
364
365 /**
366  * This method will indicate which protocols are supported by this port.
367  * @sci_port: a handle corresponding to the SAS port for which to return the
368  *    supported protocols.
369  * @protocols: This parameter specifies a pointer to an IAF protocol field
370  *    structure into which the core will copy the protocol values for the port.
371  *     The values are returned as part of a bit mask in order to allow for
372  *    multi-protocol support.
373  *
374  */
375 static void scic_sds_port_get_protocols(
376         struct scic_sds_port *sci_port,
377         struct sci_sas_identify_address_frame_protocols *protocols)
378 {
379         u8 index;
380
381         protocols->u.all = 0;
382
383         for (index = 0; index < SCI_MAX_PHYS; index++) {
384                 if (sci_port->phy_table[index] != NULL) {
385                         scic_sds_phy_get_protocols(sci_port->phy_table[index], protocols);
386                 }
387         }
388 }
389
390 /**
391  * This method requests the SAS address for the device directly attached to
392  *    this SAS port.
393  * @sci_port: a handle corresponding to the SAS port for which to return the
394  *    SAS address.
395  * @sas_address: This parameter specifies a pointer to a SAS address structure
396  *    into which the core will copy the SAS address for the device directly
397  *    attached to the port.
398  *
399  */
400 void scic_sds_port_get_attached_sas_address(
401         struct scic_sds_port *sci_port,
402         struct sci_sas_address *sas_address)
403 {
404         struct sci_sas_identify_address_frame_protocols protocols;
405         struct scic_sds_phy *phy;
406
407         /*
408          * Ensure that the phy is both part of the port and currently
409          * connected to the remote end-point. */
410         phy = scic_sds_port_get_a_connected_phy(sci_port);
411         if (phy != NULL) {
412                 scic_sds_phy_get_attached_phy_protocols(phy, &protocols);
413
414                 if (!protocols.u.bits.stp_target) {
415                         scic_sds_phy_get_attached_sas_address(phy, sas_address);
416                 } else {
417                         scic_sds_phy_get_sas_address(phy, sas_address);
418                         sas_address->low += phy->phy_index;
419                 }
420         } else {
421                 sas_address->high = 0;
422                 sas_address->low  = 0;
423         }
424 }
425
426 /**
427  * This method will indicate which protocols are supported by this remote
428  *    device.
429  * @sci_port: a handle corresponding to the SAS port for which to return the
430  *    supported protocols.
431  * @protocols: This parameter specifies a pointer to an IAF protocol field
432  *    structure into which the core will copy the protocol values for the port.
433  *     The values are returned as part of a bit mask in order to allow for
434  *    multi-protocol support.
435  *
436  */
437 static void scic_sds_port_get_attached_protocols(
438         struct scic_sds_port *sci_port,
439         struct sci_sas_identify_address_frame_protocols *protocols)
440 {
441         struct scic_sds_phy *phy;
442
443         /*
444          * Ensure that the phy is both part of the port and currently
445          * connected to the remote end-point. */
446         phy = scic_sds_port_get_a_connected_phy(sci_port);
447         if (phy != NULL)
448                 scic_sds_phy_get_attached_phy_protocols(phy, protocols);
449         else
450                 protocols->u.all = 0;
451 }
452
453 /**
454  * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
455  *
456  * @sci_port: logical port on which we need to create the remote node context
457  * @rni: remote node index for this remote node context.
458  *
459  * This routine will construct a dummy remote node context data structure
460  * This structure will be posted to the hardware to work around a scheduler
461  * error in the hardware.
462  */
463 static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
464 {
465         union scu_remote_node_context *rnc;
466
467         rnc = &sci_port->owning_controller->remote_node_context_table[rni];
468
469         memset(rnc, 0, sizeof(union scu_remote_node_context));
470
471         rnc->ssp.remote_sas_address_hi = 0;
472         rnc->ssp.remote_sas_address_lo = 0;
473
474         rnc->ssp.remote_node_index = rni;
475         rnc->ssp.remote_node_port_width = 1;
476         rnc->ssp.logical_port_index = sci_port->physical_port_index;
477
478         rnc->ssp.nexus_loss_timer_enable = false;
479         rnc->ssp.check_bit = false;
480         rnc->ssp.is_valid = true;
481         rnc->ssp.is_remote_node_context = true;
482         rnc->ssp.function_number = 0;
483         rnc->ssp.arbitration_wait_time = 0;
484 }
485
486 /**
487  * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
488  * @sci_port The logical port on which we need to create the
489  *            remote node context.
490  *            context.
491  * @tci The remote node index for this remote node context.
492  *
493  * This routine will construct a dummy task context data structure.  This
494  * structure will be posted to the hardwre to work around a scheduler error
495  * in the hardware.
496  *
497  */
498 static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
499 {
500         struct scu_task_context *task_context;
501
502         task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
503
504         memset(task_context, 0, sizeof(struct scu_task_context));
505
506         task_context->abort = 0;
507         task_context->priority = 0;
508         task_context->initiator_request = 1;
509         task_context->connection_rate = 1;
510         task_context->protocol_engine_index = 0;
511         task_context->logical_port_index = sci_port->physical_port_index;
512         task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
513         task_context->task_index = scic_sds_io_tag_get_index(tci);
514         task_context->valid = SCU_TASK_CONTEXT_VALID;
515         task_context->context_type = SCU_TASK_CONTEXT_TYPE;
516
517         task_context->remote_node_index = sci_port->reserved_rni;
518         task_context->command_code = 0;
519
520         task_context->link_layer_control = 0;
521         task_context->do_not_dma_ssp_good_response = 1;
522         task_context->strict_ordering = 0;
523         task_context->control_frame = 0;
524         task_context->timeout_enable = 0;
525         task_context->block_guard_enable = 0;
526
527         task_context->address_modifier = 0;
528
529         task_context->task_phase = 0x01;
530 }
531
532 static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
533 {
534         struct scic_sds_controller *scic = sci_port->owning_controller;
535
536         if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
537                 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
538
539         if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
540                 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
541                                                                      1, sci_port->reserved_rni);
542
543         sci_port->reserved_rni = SCU_DUMMY_INDEX;
544         sci_port->reserved_tci = SCU_DUMMY_INDEX;
545 }
546
547 /**
548  * This method performs initialization of the supplied port. Initialization
549  *    includes: - state machine initialization - member variable initialization
550  *    - configuring the phy_mask
551  * @sci_port:
552  * @transport_layer_registers:
553  * @port_task_scheduler_registers:
554  * @port_configuration_regsiter:
555  *
556  * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
557  * if the phy being added to the port
558  */
559 enum sci_status scic_sds_port_initialize(
560         struct scic_sds_port *sci_port,
561         void __iomem *port_task_scheduler_registers,
562         void __iomem *port_configuration_regsiter,
563         void __iomem *viit_registers)
564 {
565         sci_port->port_task_scheduler_registers  = port_task_scheduler_registers;
566         sci_port->port_pe_configuration_register = port_configuration_regsiter;
567         sci_port->viit_registers                 = viit_registers;
568
569         return SCI_SUCCESS;
570 }
571
572 /**
573  * scic_port_get_properties() - This method simply returns the properties
574  *    regarding the port, such as: physical index, protocols, sas address, etc.
575  * @port: this parameter specifies the port for which to retrieve the physical
576  *    index.
577  * @properties: This parameter specifies the properties structure into which to
578  *    copy the requested information.
579  *
580  * Indicate if the user specified a valid port. SCI_SUCCESS This value is
581  * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
582  * value is returned if the specified port is not valid.  When this value is
583  * returned, no data is copied to the properties output parameter.
584  */
585 enum sci_status scic_port_get_properties(
586         struct scic_sds_port *port,
587         struct scic_port_properties *prop)
588 {
589         if ((port == NULL) ||
590             (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
591                 return SCI_FAILURE_INVALID_PORT;
592
593         prop->index    = port->logical_port_index;
594         prop->phy_mask = scic_sds_port_get_phys(port);
595         scic_sds_port_get_sas_address(port, &prop->local.sas_address);
596         scic_sds_port_get_protocols(port, &prop->local.protocols);
597         scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
598         scic_sds_port_get_attached_protocols(port, &prop->remote.protocols);
599
600         return SCI_SUCCESS;
601 }
602
603 /**
604  * scic_port_hard_reset() - perform port hard reset
605  * @port: a handle corresponding to the SAS port to be hard reset.
606  * @reset_timeout: This parameter specifies the number of milliseconds in which
607  *    the port reset operation should complete.
608  *
609  * The SCI User callback in scic_user_callbacks_t will only be called once for
610  * each phy in the SAS Port at completion of the hard reset sequence. Return a
611  * status indicating whether the hard reset started successfully. SCI_SUCCESS
612  * This value is returned if the hard reset operation started successfully.
613  */
614 enum sci_status scic_port_hard_reset(
615         struct scic_sds_port *port,
616         u32 reset_timeout)
617 {
618         return port->state_handlers->reset_handler(
619                        port, reset_timeout);
620 }
621
622 /**
623  * This method assigns the direct attached device ID for this port.
624  *
625  * @param[in] sci_port The port for which the direct attached device id is to
626  *       be assigned.
627  * @param[in] device_id The direct attached device ID to assign to the port.
628  *       This will be the RNi for the device
629  */
630 void scic_sds_port_setup_transports(
631         struct scic_sds_port *sci_port,
632         u32 device_id)
633 {
634         u8 index;
635
636         for (index = 0; index < SCI_MAX_PHYS; index++) {
637                 if (sci_port->active_phy_mask & (1 << index))
638                         scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
639         }
640 }
641
642 /**
643  *
644  * @sci_port: This is the port on which the phy should be enabled.
645  * @sci_phy: This is the specific phy which to enable.
646  * @do_notify_user: This parameter specifies whether to inform the user (via
647  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
648  *
649  * This function will activate the phy in the port.
650  * Activation includes: - adding
651  * the phy to the port - enabling the Protocol Engine in the silicon. -
652  * notifying the user that the link is up. none
653  */
654 static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
655                                        struct scic_sds_phy *sci_phy,
656                                        bool do_notify_user)
657 {
658         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
659         struct sci_sas_identify_address_frame_protocols protocols;
660         struct isci_host *ihost = scic->ihost;
661
662         scic_sds_phy_get_attached_phy_protocols(sci_phy, &protocols);
663
664         /* If this is sata port then the phy has already been resumed */
665         if (!protocols.u.bits.stp_target)
666                 scic_sds_phy_resume(sci_phy);
667
668         sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
669
670         scic_sds_controller_clear_invalid_phy(scic, sci_phy);
671
672         if (do_notify_user == true)
673                 isci_port_link_up(ihost, sci_port, sci_phy);
674 }
675
676 void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
677                                   struct scic_sds_phy *sci_phy,
678                                   bool do_notify_user)
679 {
680         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
681         struct isci_port *iport = sci_object_get_association(sci_port);
682         struct isci_host *ihost = scic->ihost;
683         struct isci_phy *iphy = sci_object_get_association(sci_phy);
684
685         sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
686
687         sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
688
689         /* Re-assign the phy back to the LP as if it were a narrow port */
690         writel(sci_phy->phy_index,
691                 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
692
693         if (do_notify_user == true)
694                 isci_port_link_down(ihost, iphy, iport);
695 }
696
697 /**
698  *
699  * @sci_port: This is the port on which the phy should be disabled.
700  * @sci_phy: This is the specific phy which to disabled.
701  *
702  * This function will disable the phy and report that the phy is not valid for
703  * this port object. None
704  */
705 static void scic_sds_port_invalid_link_up(
706         struct scic_sds_port *sci_port,
707         struct scic_sds_phy *sci_phy)
708 {
709         struct scic_sds_controller *scic =
710                 scic_sds_port_get_controller(sci_port);
711
712         /*
713          * Check to see if we have alreay reported this link as bad and if
714          * not go ahead and tell the SCI_USER that we have discovered an
715          * invalid link.
716          */
717         if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
718                 scic_sds_controller_set_invalid_phy(scic, sci_phy);
719                 isci_port_invalid_link_up(scic, sci_port, sci_phy);
720         }
721 }
722
723 /**
724  * scic_sds_port_general_link_up_handler - phy can be assigned to port?
725  * @sci_port: scic_sds_port object for which has a phy that has gone link up.
726  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
727  * @do_notify_user: This parameter specifies whether to inform the user (via
728  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
729  *
730  * Determine if this phy can be assigned to this
731  * port . If the phy is not a valid PHY for
732  * this port then the function will notify the user. A PHY can only be
733  * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
734  * the same port. none
735  */
736 static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
737                                                   struct scic_sds_phy *sci_phy,
738                                                   bool do_notify_user)
739 {
740         struct sci_sas_address port_sas_address;
741         struct sci_sas_address phy_sas_address;
742
743         scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
744         scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
745
746         /* If the SAS address of the new phy matches the SAS address of
747          * other phys in the port OR this is the first phy in the port,
748          * then activate the phy and allow it to be used for operations
749          * in this port.
750          */
751         if ((phy_sas_address.high == port_sas_address.high &&
752              phy_sas_address.low  == port_sas_address.low) ||
753             sci_port->active_phy_mask == 0) {
754                 struct sci_base_state_machine *sm = &sci_port->state_machine;
755
756                 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
757                 if (sm->current_state_id == SCI_BASE_PORT_STATE_RESETTING)
758                         sci_base_state_machine_change_state(sm, SCI_BASE_PORT_STATE_READY);
759         } else
760                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
761 }
762
763
764
765 /**
766  * This method returns false if the port only has a single phy object assigned.
767  *     If there are no phys or more than one phy then the method will return
768  *    true.
769  * @sci_port: The port for which the wide port condition is to be checked.
770  *
771  * bool true Is returned if this is a wide ported port. false Is returned if
772  * this is a narrow port.
773  */
774 static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
775 {
776         u32 index;
777         u32 phy_count = 0;
778
779         for (index = 0; index < SCI_MAX_PHYS; index++) {
780                 if (sci_port->phy_table[index] != NULL) {
781                         phy_count++;
782                 }
783         }
784
785         return phy_count != 1;
786 }
787
788 /**
789  * This method is called by the PHY object when the link is detected. if the
790  *    port wants the PHY to continue on to the link up state then the port
791  *    layer must return true.  If the port object returns false the phy object
792  *    must halt its attempt to go link up.
793  * @sci_port: The port associated with the phy object.
794  * @sci_phy: The phy object that is trying to go link up.
795  *
796  * true if the phy object can continue to the link up condition. true Is
797  * returned if this phy can continue to the ready state. false Is returned if
798  * can not continue on to the ready state. This notification is in place for
799  * wide ports and direct attached phys.  Since there are no wide ported SATA
800  * devices this could become an invalid port configuration.
801  */
802 bool scic_sds_port_link_detected(
803         struct scic_sds_port *sci_port,
804         struct scic_sds_phy *sci_phy)
805 {
806         struct sci_sas_identify_address_frame_protocols protocols;
807
808         scic_sds_phy_get_attached_phy_protocols(sci_phy, &protocols);
809
810         if (
811                 (sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT)
812                 && (protocols.u.bits.stp_target)
813                 && scic_sds_port_is_wide(sci_port)
814                 ) {
815                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
816
817                 return false;
818         }
819
820         return true;
821 }
822
823 /**
824  * This method is the entry point for the phy to inform the port that it is now
825  *    in a ready state
826  * @sci_port:
827  *
828  *
829  */
830 void scic_sds_port_link_up(
831         struct scic_sds_port *sci_port,
832         struct scic_sds_phy *sci_phy)
833 {
834         sci_phy->is_in_link_training = false;
835
836         sci_port->state_handlers->link_up_handler(sci_port, sci_phy);
837 }
838
839 /**
840  * This method is the entry point for the phy to inform the port that it is no
841  *    longer in a ready state
842  * @sci_port:
843  *
844  *
845  */
846 void scic_sds_port_link_down(
847         struct scic_sds_port *sci_port,
848         struct scic_sds_phy *sci_phy)
849 {
850         sci_port->state_handlers->link_down_handler(sci_port, sci_phy);
851 }
852
853 /**
854  * This method is called to start an IO request on this port.
855  * @sci_port:
856  * @sci_dev:
857  * @sci_req:
858  *
859  * enum sci_status
860  */
861 enum sci_status scic_sds_port_start_io(
862         struct scic_sds_port *sci_port,
863         struct scic_sds_remote_device *sci_dev,
864         struct scic_sds_request *sci_req)
865 {
866         return sci_port->state_handlers->start_io_handler(
867                        sci_port, sci_dev, sci_req);
868 }
869
870 /**
871  * This method is called to complete an IO request to the port.
872  * @sci_port:
873  * @sci_dev:
874  * @sci_req:
875  *
876  * enum sci_status
877  */
878 enum sci_status scic_sds_port_complete_io(
879         struct scic_sds_port *sci_port,
880         struct scic_sds_remote_device *sci_dev,
881         struct scic_sds_request *sci_req)
882 {
883         return sci_port->state_handlers->complete_io_handler(
884                        sci_port, sci_dev, sci_req);
885 }
886
887 /**
888  * This method is provided to timeout requests for port operations. Mostly its
889  *    for the port reset operation.
890  *
891  *
892  */
893 static void scic_sds_port_timeout_handler(void *port)
894 {
895         struct scic_sds_port *sci_port = port;
896         u32 current_state;
897
898         current_state = sci_base_state_machine_get_state(
899                 &sci_port->state_machine);
900
901         if (current_state == SCI_BASE_PORT_STATE_RESETTING) {
902                 /*
903                  * if the port is still in the resetting state then the
904                  * timeout fired before the reset completed.
905                  */
906                 sci_base_state_machine_change_state(
907                         &sci_port->state_machine,
908                         SCI_BASE_PORT_STATE_FAILED);
909         } else if (current_state == SCI_BASE_PORT_STATE_STOPPED) {
910                 /*
911                  * if the port is stopped then the start request failed
912                  * In this case stay in the stopped state.
913                  */
914                 dev_err(sciport_to_dev(sci_port),
915                         "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
916                         __func__,
917                         sci_port);
918         } else if (current_state == SCI_BASE_PORT_STATE_STOPPING) {
919                 /*
920                  * if the port is still stopping then the stop has not
921                  * completed
922                  */
923                 isci_port_stop_complete(
924                                 scic_sds_port_get_controller(sci_port),
925                                 sci_port,
926                                 SCI_FAILURE_TIMEOUT);
927         } else {
928                 /*
929                  * The port is in the ready state and we have a timer
930                  * reporting a timeout this should not happen.
931                  */
932                 dev_err(sciport_to_dev(sci_port),
933                         "%s: SCIC Port 0x%p is processing a timeout operation "
934                         "in state %d.\n",
935                         __func__,
936                         sci_port,
937                         current_state);
938         }
939 }
940
941 /* --------------------------------------------------------------------------- */
942
943 /**
944  * This function updates the hardwares VIIT entry for this port.
945  *
946  *
947  */
948 static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
949 {
950         struct sci_sas_address sas_address;
951
952         scic_sds_port_get_sas_address(sci_port, &sas_address);
953
954         writel(sas_address.high,
955                 &sci_port->viit_registers->initiator_sas_address_hi);
956         writel(sas_address.low,
957                 &sci_port->viit_registers->initiator_sas_address_lo);
958
959         /* This value get cleared just in case its not already cleared */
960         writel(0, &sci_port->viit_registers->reserved);
961
962         /* We are required to update the status register last */
963         writel(SCU_VIIT_ENTRY_ID_VIIT |
964                SCU_VIIT_IPPT_INITIATOR |
965                ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
966                SCU_VIIT_STATUS_ALL_VALID,
967                &sci_port->viit_registers->status);
968 }
969
970 /**
971  * This method returns the maximum allowed speed for data transfers on this
972  *    port.  This maximum allowed speed evaluates to the maximum speed of the
973  *    slowest phy in the port.
974  * @sci_port: This parameter specifies the port for which to retrieve the
975  *    maximum allowed speed.
976  *
977  * This method returns the maximum negotiated speed of the slowest phy in the
978  * port.
979  */
980 enum sas_linkrate scic_sds_port_get_max_allowed_speed(
981         struct scic_sds_port *sci_port)
982 {
983         u16 index;
984         enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
985         struct scic_sds_phy *phy = NULL;
986
987         /*
988          * Loop through all of the phys in this port and find the phy with the
989          * lowest maximum link rate. */
990         for (index = 0; index < SCI_MAX_PHYS; index++) {
991                 phy = sci_port->phy_table[index];
992                 if (
993                         (phy != NULL)
994                         && (scic_sds_port_active_phy(sci_port, phy) == true)
995                         && (phy->max_negotiated_speed < max_allowed_speed)
996                         )
997                         max_allowed_speed = phy->max_negotiated_speed;
998         }
999
1000         return max_allowed_speed;
1001 }
1002
1003
1004 /**
1005  * This method passes the event to core user.
1006  * @sci_port: The port that a BCN happens.
1007  * @sci_phy: The phy that receives BCN.
1008  *
1009  */
1010 void scic_sds_port_broadcast_change_received(
1011         struct scic_sds_port *sci_port,
1012         struct scic_sds_phy *sci_phy)
1013 {
1014         struct scic_sds_controller *scic = sci_port->owning_controller;
1015         struct isci_host *ihost = scic->ihost;
1016
1017         /* notify the user. */
1018         isci_port_bc_change_received(ihost, sci_port, sci_phy);
1019 }
1020
1021
1022 /**
1023  * This API methhod enables the broadcast change notification from underneath
1024  *    hardware.
1025  * @sci_port: The port that a BCN had been disabled from.
1026  *
1027  */
1028 void scic_port_enable_broadcast_change_notification(
1029         struct scic_sds_port *port)
1030 {
1031         struct scic_sds_phy *phy;
1032         u32 register_value;
1033         u8 index;
1034
1035         /* Loop through all of the phys to enable BCN. */
1036         for (index = 0; index < SCI_MAX_PHYS; index++) {
1037                 phy = port->phy_table[index];
1038                 if (phy != NULL) {
1039                         register_value =
1040                                 readl(&phy->link_layer_registers->link_layer_control);
1041
1042                         /* clear the bit by writing 1. */
1043                         writel(register_value,
1044                                 &phy->link_layer_registers->link_layer_control);
1045                 }
1046         }
1047 }
1048
1049 /*
1050  * ****************************************************************************
1051  * *  READY SUBSTATE HANDLERS
1052  * **************************************************************************** */
1053
1054 /*
1055  * This method is the general ready state stop handler for the struct scic_sds_port
1056  * object.  This function will transition the ready substate machine to its
1057  * final state. enum sci_status SCI_SUCCESS
1058  */
1059 static enum sci_status scic_sds_port_ready_substate_stop_handler(
1060         struct scic_sds_port *port)
1061 {
1062         sci_base_state_machine_change_state(
1063                 &port->state_machine,
1064                 SCI_BASE_PORT_STATE_STOPPING
1065                 );
1066
1067         return SCI_SUCCESS;
1068 }
1069
1070 /*
1071  * This method is the general ready substate complete io handler for the
1072  * struct scic_sds_port object.  This function decrments the outstanding request count
1073  * for this port object. enum sci_status SCI_SUCCESS
1074  */
1075 static enum sci_status scic_sds_port_ready_substate_complete_io_handler(
1076         struct scic_sds_port *port,
1077         struct scic_sds_remote_device *device,
1078         struct scic_sds_request *io_request)
1079 {
1080         scic_sds_port_decrement_request_count(port);
1081
1082         return SCI_SUCCESS;
1083 }
1084
1085 static enum sci_status scic_sds_port_ready_substate_add_phy_handler(
1086         struct scic_sds_port *port,
1087         struct scic_sds_phy *phy)
1088 {
1089         enum sci_status status;
1090
1091         status = scic_sds_port_set_phy(port, phy);
1092
1093         if (status == SCI_SUCCESS) {
1094                 scic_sds_port_general_link_up_handler(port, phy, true);
1095
1096                 port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1097
1098                 sci_base_state_machine_change_state(
1099                         &port->ready_substate_machine,
1100                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1101                         );
1102         }
1103
1104         return status;
1105 }
1106
1107
1108 static enum sci_status scic_sds_port_ready_substate_remove_phy_handler(
1109         struct scic_sds_port *port,
1110         struct scic_sds_phy *phy)
1111 {
1112         enum sci_status status;
1113
1114         status = scic_sds_port_clear_phy(port, phy);
1115
1116         if (status == SCI_SUCCESS) {
1117                 scic_sds_port_deactivate_phy(port, phy, true);
1118
1119                 port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1120
1121                 sci_base_state_machine_change_state(
1122                         &port->ready_substate_machine,
1123                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1124                         );
1125         }
1126
1127         return status;
1128 }
1129
1130 /*
1131  * ****************************************************************************
1132  * *  READY SUBSTATE WAITING HANDLERS
1133  * **************************************************************************** */
1134
1135 /**
1136  *
1137  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1138  *    gone link up.
1139  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1140  *
1141  * This method is the ready waiting substate link up handler for the
1142  * struct scic_sds_port object.  This methos will report the link up condition for
1143  * this port and will transition to the ready operational substate. none
1144  */
1145 static void scic_sds_port_ready_waiting_substate_link_up_handler(
1146         struct scic_sds_port *sci_port,
1147         struct scic_sds_phy *sci_phy)
1148 {
1149         /*
1150          * Since this is the first phy going link up for the port we can just enable
1151          * it and continue. */
1152         scic_sds_port_activate_phy(sci_port, sci_phy, true);
1153
1154         sci_base_state_machine_change_state(
1155                 &sci_port->ready_substate_machine,
1156                 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
1157                 );
1158 }
1159
1160 /*
1161  * This method is the ready waiting substate start io handler for the
1162  * struct scic_sds_port object. The port object can not accept new requests so the
1163  * request is failed. enum sci_status SCI_FAILURE_INVALID_STATE
1164  */
1165 static enum sci_status scic_sds_port_ready_waiting_substate_start_io_handler(
1166         struct scic_sds_port *port,
1167         struct scic_sds_remote_device *device,
1168         struct scic_sds_request *io_request)
1169 {
1170         return SCI_FAILURE_INVALID_STATE;
1171 }
1172
1173 /*
1174  * ****************************************************************************
1175  * *  READY SUBSTATE OPERATIONAL HANDLERS
1176  * **************************************************************************** */
1177
1178 /*
1179  * This method will casue the port to reset. enum sci_status SCI_SUCCESS
1180  */
1181 static enum
1182 sci_status scic_sds_port_ready_operational_substate_reset_handler(
1183                 struct scic_sds_port *port,
1184                 u32 timeout)
1185 {
1186         enum sci_status status = SCI_FAILURE_INVALID_PHY;
1187         u32 phy_index;
1188         struct scic_sds_phy *selected_phy = NULL;
1189
1190
1191         /* Select a phy on which we can send the hard reset request. */
1192         for (phy_index = 0;
1193              (phy_index < SCI_MAX_PHYS) && (selected_phy == NULL);
1194              phy_index++) {
1195                 selected_phy = port->phy_table[phy_index];
1196
1197                 if ((selected_phy != NULL) &&
1198                     !scic_sds_port_active_phy(port, selected_phy)) {
1199                         /*
1200                          * We found a phy but it is not ready select
1201                          * different phy
1202                          */
1203                         selected_phy = NULL;
1204                 }
1205         }
1206
1207         /* If we have a phy then go ahead and start the reset procedure */
1208         if (selected_phy != NULL) {
1209                 status = scic_sds_phy_reset(selected_phy);
1210
1211                 if (status == SCI_SUCCESS) {
1212                         isci_timer_start(port->timer_handle, timeout);
1213                         port->not_ready_reason =
1214                                 SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1215
1216                         sci_base_state_machine_change_state(
1217                                         &port->state_machine,
1218                                         SCI_BASE_PORT_STATE_RESETTING);
1219                 }
1220         }
1221
1222         return status;
1223 }
1224
1225 /**
1226  * scic_sds_port_ready_operational_substate_link_up_handler() -
1227  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1228  *    gone link up.
1229  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1230  *
1231  * This method is the ready operational substate link up handler for the
1232  * struct scic_sds_port object. This function notifies the SCI User that the phy has
1233  * gone link up. none
1234  */
1235 static void scic_sds_port_ready_operational_substate_link_up_handler(
1236         struct scic_sds_port *sci_port,
1237         struct scic_sds_phy *sci_phy)
1238 {
1239         scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1240 }
1241
1242 /**
1243  * scic_sds_port_ready_operational_substate_link_down_handler() -
1244  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1245  *    gone link down.
1246  * @sci_phy: This is the struct scic_sds_phy object that has gone link down.
1247  *
1248  * This method is the ready operational substate link down handler for the
1249  * struct scic_sds_port object. This function notifies the SCI User that the phy has
1250  * gone link down and if this is the last phy in the port the port will change
1251  * state to the ready waiting substate. none
1252  */
1253 static void scic_sds_port_ready_operational_substate_link_down_handler(
1254         struct scic_sds_port *sci_port,
1255         struct scic_sds_phy *sci_phy)
1256 {
1257         scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1258
1259         /*
1260          * If there are no active phys left in the port, then transition
1261          * the port to the WAITING state until such time as a phy goes
1262          * link up. */
1263         if (sci_port->active_phy_mask == 0)
1264                 sci_base_state_machine_change_state(&sci_port->ready_substate_machine,
1265                                                     SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1266 }
1267
1268 /*
1269  * This method is the ready operational substate start io handler for the
1270  * struct scic_sds_port object.  This function incremetns the outstanding request
1271  * count for this port object. enum sci_status SCI_SUCCESS
1272  */
1273 static enum sci_status scic_sds_port_ready_operational_substate_start_io_handler(
1274         struct scic_sds_port *port,
1275         struct scic_sds_remote_device *device,
1276         struct scic_sds_request *io_request)
1277 {
1278         scic_sds_port_increment_request_count(port);
1279
1280         return SCI_SUCCESS;
1281 }
1282
1283 /*
1284  * ****************************************************************************
1285  * *  READY SUBSTATE OPERATIONAL HANDLERS
1286  * **************************************************************************** */
1287
1288 /*
1289  * This is the default method for a port add phy request.  It will report a
1290  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
1291  */
1292 static enum sci_status scic_sds_port_ready_configuring_substate_add_phy_handler(
1293         struct scic_sds_port *port,
1294         struct scic_sds_phy *phy)
1295 {
1296         enum sci_status status;
1297
1298         status = scic_sds_port_set_phy(port, phy);
1299
1300         if (status == SCI_SUCCESS) {
1301                 scic_sds_port_general_link_up_handler(port, phy, true);
1302
1303                 /*
1304                  * Re-enter the configuring state since this may be the last phy in
1305                  * the port. */
1306                 sci_base_state_machine_change_state(
1307                         &port->ready_substate_machine,
1308                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1309                         );
1310         }
1311
1312         return status;
1313 }
1314
1315 /*
1316  * This is the default method for a port remove phy request.  It will report a
1317  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
1318  */
1319 static enum sci_status scic_sds_port_ready_configuring_substate_remove_phy_handler(
1320         struct scic_sds_port *port,
1321         struct scic_sds_phy *phy)
1322 {
1323         enum sci_status status;
1324
1325         status = scic_sds_port_clear_phy(port, phy);
1326
1327         if (status == SCI_SUCCESS) {
1328                 scic_sds_port_deactivate_phy(port, phy, true);
1329
1330                 /*
1331                  * Re-enter the configuring state since this may be the last phy in
1332                  * the port. */
1333                 sci_base_state_machine_change_state(
1334                         &port->ready_substate_machine,
1335                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1336                         );
1337         }
1338
1339         return status;
1340 }
1341
1342 /**
1343  * scic_sds_port_ready_configuring_substate_complete_io_handler() -
1344  * @port: This is the port that is being requested to complete the io request.
1345  * @device: This is the device on which the io is completing.
1346  *
1347  * This method will decrement the outstanding request count for this port. If
1348  * the request count goes to 0 then the port can be reprogrammed with its new
1349  * phy data.
1350  */
1351 static enum sci_status
1352 scic_sds_port_ready_configuring_substate_complete_io_handler(
1353         struct scic_sds_port *port,
1354         struct scic_sds_remote_device *device,
1355         struct scic_sds_request *io_request)
1356 {
1357         scic_sds_port_decrement_request_count(port);
1358
1359         if (port->started_request_count == 0) {
1360                 sci_base_state_machine_change_state(
1361                         &port->ready_substate_machine,
1362                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
1363                         );
1364         }
1365
1366         return SCI_SUCCESS;
1367 }
1368
1369 static enum sci_status default_port_handler(struct scic_sds_port *sci_port,
1370                                             const char *func)
1371 {
1372         dev_warn(sciport_to_dev(sci_port),
1373                  "%s: in wrong state: %d\n", func,
1374                  sci_base_state_machine_get_state(&sci_port->state_machine));
1375         return SCI_FAILURE_INVALID_STATE;
1376 }
1377
1378 static enum sci_status
1379 scic_sds_port_default_start_handler(struct scic_sds_port *sci_port)
1380 {
1381         return default_port_handler(sci_port, __func__);
1382 }
1383
1384 static enum sci_status
1385 scic_sds_port_default_stop_handler(struct scic_sds_port *sci_port)
1386 {
1387         return default_port_handler(sci_port, __func__);
1388 }
1389
1390 static enum sci_status
1391 scic_sds_port_default_destruct_handler(struct scic_sds_port *sci_port)
1392 {
1393         return default_port_handler(sci_port, __func__);
1394 }
1395
1396 static enum sci_status
1397 scic_sds_port_default_reset_handler(struct scic_sds_port *sci_port,
1398                                     u32 timeout)
1399 {
1400         return default_port_handler(sci_port, __func__);
1401 }
1402
1403 static enum sci_status
1404 scic_sds_port_default_add_phy_handler(struct scic_sds_port *sci_port,
1405                                       struct scic_sds_phy *base_phy)
1406 {
1407         return default_port_handler(sci_port, __func__);
1408 }
1409
1410 static enum sci_status
1411 scic_sds_port_default_remove_phy_handler(struct scic_sds_port *sci_port,
1412                                          struct scic_sds_phy *base_phy)
1413 {
1414         return default_port_handler(sci_port, __func__);
1415 }
1416
1417 /*
1418  * This is the default method for a port unsolicited frame request.  It will
1419  * report a warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE Is it even
1420  * possible to receive an unsolicited frame directed to a port object?  It
1421  * seems possible if we implementing virtual functions but until then?
1422  */
1423 static enum sci_status
1424 scic_sds_port_default_frame_handler(struct scic_sds_port *sci_port,
1425                                     u32 frame_index)
1426 {
1427         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
1428
1429         default_port_handler(sci_port, __func__);
1430         scic_sds_controller_release_frame(scic, frame_index);
1431
1432         return SCI_FAILURE_INVALID_STATE;
1433 }
1434
1435 static enum sci_status scic_sds_port_default_event_handler(struct scic_sds_port *sci_port,
1436                                                     u32 event_code)
1437 {
1438         return default_port_handler(sci_port, __func__);
1439 }
1440
1441 static void scic_sds_port_default_link_up_handler(struct scic_sds_port *sci_port,
1442                                            struct scic_sds_phy *sci_phy)
1443 {
1444         default_port_handler(sci_port, __func__);
1445 }
1446
1447 static void scic_sds_port_default_link_down_handler(struct scic_sds_port *sci_port,
1448                                              struct scic_sds_phy *sci_phy)
1449 {
1450         default_port_handler(sci_port, __func__);
1451 }
1452
1453 static enum sci_status scic_sds_port_default_start_io_handler(struct scic_sds_port *sci_port,
1454                                                        struct scic_sds_remote_device *sci_dev,
1455                                                        struct scic_sds_request *sci_req)
1456 {
1457         return default_port_handler(sci_port, __func__);
1458 }
1459
1460 static enum sci_status scic_sds_port_default_complete_io_handler(struct scic_sds_port *sci_port,
1461                                                                  struct scic_sds_remote_device *sci_dev,
1462                                                                  struct scic_sds_request *sci_req)
1463 {
1464         return default_port_handler(sci_port, __func__);
1465 }
1466
1467
1468
1469 static struct scic_sds_port_state_handler
1470 scic_sds_port_ready_substate_handler_table[SCIC_SDS_PORT_READY_MAX_SUBSTATES] = {
1471         {
1472                 /* SCIC_SDS_PORT_READY_SUBSTATE_WAITING */
1473                 scic_sds_port_default_start_handler,
1474                 scic_sds_port_ready_substate_stop_handler,
1475                 scic_sds_port_default_destruct_handler,
1476                 scic_sds_port_default_reset_handler,
1477                 scic_sds_port_ready_substate_add_phy_handler,
1478                 scic_sds_port_default_remove_phy_handler,
1479                 scic_sds_port_default_frame_handler,
1480                 scic_sds_port_default_event_handler,
1481                 scic_sds_port_ready_waiting_substate_link_up_handler,
1482                 scic_sds_port_default_link_down_handler,
1483                 scic_sds_port_ready_waiting_substate_start_io_handler,
1484                 scic_sds_port_ready_substate_complete_io_handler,
1485         },
1486
1487         {
1488                 /* SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL */
1489                 scic_sds_port_default_start_handler,
1490                 scic_sds_port_ready_substate_stop_handler,
1491                 scic_sds_port_default_destruct_handler,
1492                 scic_sds_port_ready_operational_substate_reset_handler,
1493                 scic_sds_port_ready_substate_add_phy_handler,
1494                 scic_sds_port_ready_substate_remove_phy_handler,
1495                 scic_sds_port_default_frame_handler,
1496                 scic_sds_port_default_event_handler,
1497                 scic_sds_port_ready_operational_substate_link_up_handler,
1498                 scic_sds_port_ready_operational_substate_link_down_handler,
1499                 scic_sds_port_ready_operational_substate_start_io_handler,
1500                 scic_sds_port_ready_substate_complete_io_handler,
1501         },
1502
1503         {
1504                 /* SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING */
1505                 scic_sds_port_default_start_handler,
1506                 scic_sds_port_ready_substate_stop_handler,
1507                 scic_sds_port_default_destruct_handler,
1508                 scic_sds_port_default_reset_handler,
1509                 scic_sds_port_ready_configuring_substate_add_phy_handler,
1510                 scic_sds_port_ready_configuring_substate_remove_phy_handler,
1511                 scic_sds_port_default_frame_handler,
1512                 scic_sds_port_default_event_handler,
1513                 scic_sds_port_default_link_up_handler,
1514                 scic_sds_port_default_link_down_handler,
1515                 scic_sds_port_default_start_io_handler,
1516                 scic_sds_port_ready_configuring_substate_complete_io_handler
1517         }
1518 };
1519
1520 /**
1521  * scic_sds_port_set_ready_state_handlers() -
1522  *
1523  * This macro sets the port ready substate handlers.
1524  */
1525 #define scic_sds_port_set_ready_state_handlers(port, state_id) \
1526         scic_sds_port_set_state_handlers(\
1527                 port, &scic_sds_port_ready_substate_handler_table[(state_id)] \
1528                 )
1529
1530 /*
1531  * ******************************************************************************
1532  * *  PORT STATE PRIVATE METHODS
1533  * ****************************************************************************** */
1534
1535 /**
1536  *
1537  * @sci_port: This is the struct scic_sds_port object to suspend.
1538  *
1539  * This method will susped the port task scheduler for this port object. none
1540  */
1541 static void
1542 scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1543 {
1544         u32 pts_control_value;
1545
1546         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1547         pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1548         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1549 }
1550
1551 /**
1552  * scic_sds_port_post_dummy_request() - post dummy/workaround request
1553  * @sci_port: port to post task
1554  *
1555  * Prevent the hardware scheduler from posting new requests to the front
1556  * of the scheduler queue causing a starvation problem for currently
1557  * ongoing requests.
1558  *
1559  */
1560 static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1561 {
1562         u32 command;
1563         struct scu_task_context *task_context;
1564         struct scic_sds_controller *scic = sci_port->owning_controller;
1565         u16 tci = sci_port->reserved_tci;
1566
1567         task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1568
1569         task_context->abort = 0;
1570
1571         command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1572                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1573                   tci;
1574
1575         scic_sds_controller_post_request(scic, command);
1576 }
1577
1578 /**
1579  * This routine will abort the dummy request.  This will alow the hardware to
1580  * power down parts of the silicon to save power.
1581  *
1582  * @sci_port: The port on which the task must be aborted.
1583  *
1584  */
1585 static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1586 {
1587         struct scic_sds_controller *scic = sci_port->owning_controller;
1588         u16 tci = sci_port->reserved_tci;
1589         struct scu_task_context *tc;
1590         u32 command;
1591
1592         tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1593
1594         tc->abort = 1;
1595
1596         command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1597                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1598                   tci;
1599
1600         scic_sds_controller_post_request(scic, command);
1601 }
1602
1603 /**
1604  *
1605  * @sci_port: This is the struct scic_sds_port object to resume.
1606  *
1607  * This method will resume the port task scheduler for this port object. none
1608  */
1609 static void
1610 scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1611 {
1612         u32 pts_control_value;
1613
1614         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1615         pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1616         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1617 }
1618
1619 /*
1620  * ******************************************************************************
1621  * *  PORT READY SUBSTATE METHODS
1622  * ****************************************************************************** */
1623
1624 /**
1625  *
1626  * @object: This is the object which is cast to a struct scic_sds_port object.
1627  *
1628  * This method will perform the actions required by the struct scic_sds_port on
1629  * entering the SCIC_SDS_PORT_READY_SUBSTATE_WAITING. This function checks the
1630  * port for any ready phys.  If there is at least one phy in a ready state then
1631  * the port transitions to the ready operational substate. none
1632  */
1633 static void scic_sds_port_ready_substate_waiting_enter(void *object)
1634 {
1635         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1636
1637         scic_sds_port_set_ready_state_handlers(
1638                 sci_port, SCIC_SDS_PORT_READY_SUBSTATE_WAITING
1639                 );
1640
1641         scic_sds_port_suspend_port_task_scheduler(sci_port);
1642
1643         sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1644
1645         if (sci_port->active_phy_mask != 0) {
1646                 /* At least one of the phys on the port is ready */
1647                 sci_base_state_machine_change_state(
1648                         &sci_port->ready_substate_machine,
1649                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
1650                         );
1651         }
1652 }
1653
1654 /**
1655  *
1656  * @object: This is the object which is cast to a struct scic_sds_port object.
1657  *
1658  * This function will perform the actions required by the struct scic_sds_port
1659  * on entering the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function sets
1660  * the state handlers for the port object, notifies the SCI User that the port
1661  * is ready, and resumes port operations. none
1662  */
1663 static void scic_sds_port_ready_substate_operational_enter(void *object)
1664 {
1665         u32 index;
1666         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1667         struct scic_sds_controller *scic =
1668                 scic_sds_port_get_controller(sci_port);
1669         struct isci_host *ihost = scic->ihost;
1670         struct isci_port *iport = sci_object_get_association(sci_port);
1671
1672         scic_sds_port_set_ready_state_handlers(
1673                         sci_port,
1674                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1675
1676         isci_port_ready(ihost, iport);
1677
1678         for (index = 0; index < SCI_MAX_PHYS; index++) {
1679                 if (sci_port->phy_table[index]) {
1680                         writel(sci_port->physical_port_index,
1681                                 &sci_port->port_pe_configuration_register[
1682                                         sci_port->phy_table[index]->phy_index]);
1683                 }
1684         }
1685
1686         scic_sds_port_update_viit_entry(sci_port);
1687
1688         scic_sds_port_resume_port_task_scheduler(sci_port);
1689
1690         /*
1691          * Post the dummy task for the port so the hardware can schedule
1692          * io correctly
1693          */
1694         scic_sds_port_post_dummy_request(sci_port);
1695 }
1696
1697 /**
1698  *
1699  * @object: This is the object which is cast to a struct scic_sds_port object.
1700  *
1701  * This method will perform the actions required by the struct scic_sds_port on
1702  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1703  * the port not ready and suspends the port task scheduler. none
1704  */
1705 static void scic_sds_port_ready_substate_operational_exit(void *object)
1706 {
1707         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1708         struct scic_sds_controller *scic =
1709                 scic_sds_port_get_controller(sci_port);
1710         struct isci_host *ihost = scic->ihost;
1711         struct isci_port *iport = sci_object_get_association(sci_port);
1712
1713         /*
1714          * Kill the dummy task for this port if it has not yet posted
1715          * the hardware will treat this as a NOP and just return abort
1716          * complete.
1717          */
1718         scic_sds_port_abort_dummy_request(sci_port);
1719
1720         isci_port_not_ready(ihost, iport);
1721 }
1722
1723 /*
1724  * ******************************************************************************
1725  * *  PORT READY CONFIGURING METHODS
1726  * ****************************************************************************** */
1727
1728 /**
1729  * scic_sds_port_ready_substate_configuring_enter() -
1730  * @object: This is the object which is cast to a struct scic_sds_port object.
1731  *
1732  * This method will perform the actions required by the struct scic_sds_port on
1733  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1734  * the port not ready and suspends the port task scheduler. none
1735  */
1736 static void scic_sds_port_ready_substate_configuring_enter(void *object)
1737 {
1738         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1739         struct scic_sds_controller *scic =
1740                 scic_sds_port_get_controller(sci_port);
1741         struct isci_host *ihost = scic->ihost;
1742         struct isci_port *iport = sci_object_get_association(sci_port);
1743
1744         scic_sds_port_set_ready_state_handlers(
1745                         sci_port,
1746                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1747
1748         if (sci_port->active_phy_mask == 0) {
1749                 isci_port_not_ready(ihost, iport);
1750
1751                 sci_base_state_machine_change_state(
1752                                 &sci_port->ready_substate_machine,
1753                                 SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1754         } else if (sci_port->started_request_count == 0)
1755                 sci_base_state_machine_change_state(
1756                                 &sci_port->ready_substate_machine,
1757                                 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1758 }
1759
1760 static void scic_sds_port_ready_substate_configuring_exit(void *object)
1761 {
1762         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
1763
1764         scic_sds_port_suspend_port_task_scheduler(sci_port);
1765 }
1766
1767 /* --------------------------------------------------------------------------- */
1768
1769 static const struct sci_base_state scic_sds_port_ready_substate_table[] = {
1770         [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
1771                 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1772         },
1773         [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
1774                 .enter_state = scic_sds_port_ready_substate_operational_enter,
1775                 .exit_state  = scic_sds_port_ready_substate_operational_exit
1776         },
1777         [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
1778                 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1779                 .exit_state  = scic_sds_port_ready_substate_configuring_exit
1780         },
1781 };
1782
1783 /**
1784  *
1785  * @port: This is the struct scic_sds_port object on which the io request count will
1786  *    be decremented.
1787  * @device: This is the struct scic_sds_remote_device object to which the io request
1788  *    is being directed.  This parameter is not required to complete this
1789  *    operation.
1790  * @io_request: This is the request that is being completed on this port
1791  *    object.  This parameter is not required to complete this operation.
1792  *
1793  * This is a general complete io request handler for the struct scic_sds_port object.
1794  * enum sci_status SCI_SUCCESS
1795  */
1796 static enum sci_status scic_sds_port_general_complete_io_handler(
1797         struct scic_sds_port *port,
1798         struct scic_sds_remote_device *device,
1799         struct scic_sds_request *io_request)
1800 {
1801         scic_sds_port_decrement_request_count(port);
1802
1803         return SCI_SUCCESS;
1804 }
1805
1806 /**
1807  * scic_sds_port_stopped_state_start_handler() - stop a port from "started"
1808  *
1809  * @port: This is the struct scic_sds_port object which is cast into a
1810  * struct scic_sds_port object.
1811  *
1812  * This function takes the struct scic_sds_port from a stopped state and
1813  * attempts to start it.  To start a port it must have no assiged devices and
1814  * it must have at least one phy assigned to it.  If those conditions are
1815  * met then the port can transition to the ready state.
1816  * enum sci_status
1817  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
1818  * This struct scic_sds_port object could not be started because the port
1819  * configuration is not valid.
1820  * SCI_SUCCESS
1821  * the start request is successful and the struct scic_sds_port object
1822  * has transitioned to the SCI_BASE_PORT_STATE_READY.
1823  */
1824 static enum sci_status
1825 scic_sds_port_stopped_state_start_handler(struct scic_sds_port *sci_port)
1826 {
1827         struct scic_sds_controller *scic = sci_port->owning_controller;
1828         struct isci_host *ihost = scic->ihost;
1829         enum sci_status status = SCI_SUCCESS;
1830         u32 phy_mask;
1831
1832         if (sci_port->assigned_device_count > 0) {
1833                 /*
1834                  * @todo This is a start failure operation because
1835                  * there are still devices assigned to this port.
1836                  * There must be no devices assigned to a port on a
1837                  * start operation.
1838                  */
1839                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1840         }
1841
1842         sci_port->timer_handle =
1843                 isci_timer_create(ihost,
1844                                   sci_port,
1845                                   scic_sds_port_timeout_handler);
1846
1847         if (!sci_port->timer_handle)
1848                 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1849
1850         if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1851                 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1852                                 &scic->available_remote_nodes, 1);
1853
1854                 if (rni != SCU_DUMMY_INDEX)
1855                         scic_sds_port_construct_dummy_rnc(sci_port, rni);
1856                 else
1857                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1858                 sci_port->reserved_rni = rni;
1859         }
1860
1861         if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
1862                 /* Allocate a TCI and remove the sequence nibble */
1863                 u16 tci = scic_controller_allocate_io_tag(scic);
1864
1865                 if (tci != SCU_DUMMY_INDEX)
1866                         scic_sds_port_construct_dummy_task(sci_port, tci);
1867                 else
1868                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1869                 sci_port->reserved_tci = tci;
1870         }
1871
1872         if (status == SCI_SUCCESS) {
1873                 phy_mask = scic_sds_port_get_phys(sci_port);
1874
1875                 /*
1876                  * There are one or more phys assigned to this port.  Make sure
1877                  * the port's phy mask is in fact legal and supported by the
1878                  * silicon.
1879                  */
1880                 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1881                         sci_base_state_machine_change_state(
1882                                 &sci_port->state_machine,
1883                                 SCI_BASE_PORT_STATE_READY);
1884
1885                         return SCI_SUCCESS;
1886                 } else
1887                         status = SCI_FAILURE;
1888         }
1889
1890         if (status != SCI_SUCCESS)
1891                 scic_sds_port_destroy_dummy_resources(sci_port);
1892
1893         return status;
1894 }
1895
1896 /*
1897  * This method takes the struct scic_sds_port that is in a stopped state and handles a
1898  * stop request.  This function takes no action. enum sci_status SCI_SUCCESS the
1899  * stop request is successful as the struct scic_sds_port object is already stopped.
1900  */
1901 static enum sci_status scic_sds_port_stopped_state_stop_handler(
1902         struct scic_sds_port *port)
1903 {
1904         /* We are already stopped so there is nothing to do here */
1905         return SCI_SUCCESS;
1906 }
1907
1908 /*
1909  * This method takes the struct scic_sds_port that is in a stopped state and handles
1910  * the destruct request.  The stopped state is the only state in which the
1911  * struct scic_sds_port can be destroyed.  This function causes the port object to
1912  * transition to the SCI_BASE_PORT_STATE_FINAL. enum sci_status SCI_SUCCESS
1913  */
1914 static enum sci_status scic_sds_port_stopped_state_destruct_handler(
1915         struct scic_sds_port *port)
1916 {
1917         sci_base_state_machine_stop(&port->state_machine);
1918
1919         return SCI_SUCCESS;
1920 }
1921
1922 /*
1923  * This method takes the struct scic_sds_port that is in a stopped state and handles
1924  * the add phy request.  In MPC mode the only time a phy can be added to a port
1925  * is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
1926  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
1927  * be added to the port. SCI_SUCCESS if the phy is added to the port.
1928  */
1929 static enum sci_status scic_sds_port_stopped_state_add_phy_handler(
1930         struct scic_sds_port *port,
1931         struct scic_sds_phy *phy)
1932 {
1933         struct sci_sas_address port_sas_address;
1934
1935         /* Read the port assigned SAS Address if there is one */
1936         scic_sds_port_get_sas_address(port, &port_sas_address);
1937
1938         if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1939                 struct sci_sas_address phy_sas_address;
1940
1941                 /*
1942                  * Make sure that the PHY SAS Address matches the SAS Address
1943                  * for this port. */
1944                 scic_sds_phy_get_sas_address(phy, &phy_sas_address);
1945
1946                 if (
1947                         (port_sas_address.high != phy_sas_address.high)
1948                         || (port_sas_address.low  != phy_sas_address.low)
1949                         ) {
1950                         return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1951                 }
1952         }
1953
1954         return scic_sds_port_set_phy(port, phy);
1955 }
1956
1957 /*
1958  * This method takes the struct scic_sds_port that is in a stopped state and handles
1959  * the remove phy request.  In MPC mode the only time a phy can be removed from
1960  * a port is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
1961  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
1962  * be added to the port. SCI_SUCCESS if the phy is added to the port.
1963  */
1964 static enum sci_status scic_sds_port_stopped_state_remove_phy_handler(
1965         struct scic_sds_port *port,
1966         struct scic_sds_phy *phy)
1967 {
1968         return scic_sds_port_clear_phy(port, phy);
1969 }
1970
1971 /*
1972  * ****************************************************************************
1973  * *  READY STATE HANDLERS
1974  * **************************************************************************** */
1975
1976 /*
1977  * ****************************************************************************
1978  * *  RESETTING STATE HANDLERS
1979  * **************************************************************************** */
1980
1981 /*
1982  * ****************************************************************************
1983  * *  STOPPING STATE HANDLERS
1984  * **************************************************************************** */
1985
1986 /*
1987  * This method takes the struct scic_sds_port that is in a stopping state and handles
1988  * the complete io request. Should the request count reach 0 then the port
1989  * object will transition to the stopped state. enum sci_status SCI_SUCCESS
1990  */
1991 static enum sci_status scic_sds_port_stopping_state_complete_io_handler(
1992         struct scic_sds_port *sci_port,
1993         struct scic_sds_remote_device *device,
1994         struct scic_sds_request *io_request)
1995 {
1996         scic_sds_port_decrement_request_count(sci_port);
1997
1998         if (sci_port->started_request_count == 0) {
1999                 sci_base_state_machine_change_state(&sci_port->state_machine,
2000                                                     SCI_BASE_PORT_STATE_STOPPED);
2001         }
2002
2003         return SCI_SUCCESS;
2004 }
2005
2006 /*
2007  * ****************************************************************************
2008  * *  RESETTING STATE HANDLERS
2009  * **************************************************************************** */
2010
2011 /**
2012  *
2013  * @port: This is the port object which is being requested to stop.
2014  *
2015  * This method will stop a failed port.  This causes a transition to the
2016  * stopping state. enum sci_status SCI_SUCCESS
2017  */
2018 static enum sci_status scic_sds_port_reset_state_stop_handler(
2019         struct scic_sds_port *port)
2020 {
2021         sci_base_state_machine_change_state(
2022                 &port->state_machine,
2023                 SCI_BASE_PORT_STATE_STOPPING
2024                 );
2025
2026         return SCI_SUCCESS;
2027 }
2028
2029 /*
2030  * This method will transition a failed port to its ready state.  The port
2031  * failed because a hard reset request timed out but at some time later one or
2032  * more phys in the port became ready. enum sci_status SCI_SUCCESS
2033  */
2034 static void scic_sds_port_reset_state_link_up_handler(
2035         struct scic_sds_port *port,
2036         struct scic_sds_phy *phy)
2037 {
2038         /*
2039          * / @todo We should make sure that the phy that has gone link up is the same
2040          * /       one on which we sent the reset.  It is possible that the phy on
2041          * /       which we sent the reset is not the one that has gone link up and we
2042          * /       want to make sure that phy being reset comes back.  Consider the
2043          * /       case where a reset is sent but before the hardware processes the
2044          * /       reset it get a link up on the port because of a hot plug event.
2045          * /       because of the reset request this phy will go link down almost
2046          * /       immediately. */
2047
2048         /*
2049          * In the resetting state we don't notify the user regarding
2050          * link up and link down notifications. */
2051         scic_sds_port_general_link_up_handler(port, phy, false);
2052 }
2053
2054 /*
2055  * This method process link down notifications that occur during a port reset
2056  * operation. Link downs can occur during the reset operation. enum sci_status
2057  * SCI_SUCCESS
2058  */
2059 static void scic_sds_port_reset_state_link_down_handler(
2060         struct scic_sds_port *port,
2061         struct scic_sds_phy *phy)
2062 {
2063         /*
2064          * In the resetting state we don't notify the user regarding
2065          * link up and link down notifications. */
2066         scic_sds_port_deactivate_phy(port, phy, false);
2067 }
2068
2069 static struct scic_sds_port_state_handler
2070 scic_sds_port_state_handler_table[SCI_BASE_PORT_MAX_STATES] =
2071 {
2072         /* SCI_BASE_PORT_STATE_STOPPED */
2073         {
2074                 scic_sds_port_stopped_state_start_handler,
2075                 scic_sds_port_stopped_state_stop_handler,
2076                 scic_sds_port_stopped_state_destruct_handler,
2077                 scic_sds_port_default_reset_handler,
2078                 scic_sds_port_stopped_state_add_phy_handler,
2079                 scic_sds_port_stopped_state_remove_phy_handler,
2080                 scic_sds_port_default_frame_handler,
2081                 scic_sds_port_default_event_handler,
2082                 scic_sds_port_default_link_up_handler,
2083                 scic_sds_port_default_link_down_handler,
2084                 scic_sds_port_default_start_io_handler,
2085                 scic_sds_port_default_complete_io_handler
2086         },
2087         /* SCI_BASE_PORT_STATE_STOPPING */
2088         {
2089                 scic_sds_port_default_start_handler,
2090                 scic_sds_port_default_stop_handler,
2091                 scic_sds_port_default_destruct_handler,
2092                 scic_sds_port_default_reset_handler,
2093                 scic_sds_port_default_add_phy_handler,
2094                 scic_sds_port_default_remove_phy_handler,
2095                 scic_sds_port_default_frame_handler,
2096                 scic_sds_port_default_event_handler,
2097                 scic_sds_port_default_link_up_handler,
2098                 scic_sds_port_default_link_down_handler,
2099                 scic_sds_port_default_start_io_handler,
2100                 scic_sds_port_stopping_state_complete_io_handler
2101         },
2102         /* SCI_BASE_PORT_STATE_READY */
2103         {
2104                 scic_sds_port_default_start_handler,
2105                 scic_sds_port_default_stop_handler,
2106                 scic_sds_port_default_destruct_handler,
2107                 scic_sds_port_default_reset_handler,
2108                 scic_sds_port_default_add_phy_handler,
2109                 scic_sds_port_default_remove_phy_handler,
2110                 scic_sds_port_default_frame_handler,
2111                 scic_sds_port_default_event_handler,
2112                 scic_sds_port_default_link_up_handler,
2113                 scic_sds_port_default_link_down_handler,
2114                 scic_sds_port_default_start_io_handler,
2115                 scic_sds_port_general_complete_io_handler
2116         },
2117         /* SCI_BASE_PORT_STATE_RESETTING */
2118         {
2119                 scic_sds_port_default_start_handler,
2120                 scic_sds_port_reset_state_stop_handler,
2121                 scic_sds_port_default_destruct_handler,
2122                 scic_sds_port_default_reset_handler,
2123                 scic_sds_port_default_add_phy_handler,
2124                 scic_sds_port_default_remove_phy_handler,
2125                 scic_sds_port_default_frame_handler,
2126                 scic_sds_port_default_event_handler,
2127                 scic_sds_port_reset_state_link_up_handler,
2128                 scic_sds_port_reset_state_link_down_handler,
2129                 scic_sds_port_default_start_io_handler,
2130                 scic_sds_port_general_complete_io_handler
2131         },
2132         /* SCI_BASE_PORT_STATE_FAILED */
2133         {
2134                 scic_sds_port_default_start_handler,
2135                 scic_sds_port_default_stop_handler,
2136                 scic_sds_port_default_destruct_handler,
2137                 scic_sds_port_default_reset_handler,
2138                 scic_sds_port_default_add_phy_handler,
2139                 scic_sds_port_default_remove_phy_handler,
2140                 scic_sds_port_default_frame_handler,
2141                 scic_sds_port_default_event_handler,
2142                 scic_sds_port_default_link_up_handler,
2143                 scic_sds_port_default_link_down_handler,
2144                 scic_sds_port_default_start_io_handler,
2145                 scic_sds_port_general_complete_io_handler
2146         }
2147 };
2148
2149 /*
2150  * ******************************************************************************
2151  * *  PORT STATE PRIVATE METHODS
2152  * ****************************************************************************** */
2153
2154 /**
2155  *
2156  * @sci_port: This is the port object which to suspend.
2157  *
2158  * This method will enable the SCU Port Task Scheduler for this port object but
2159  * will leave the port task scheduler in a suspended state. none
2160  */
2161 static void
2162 scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
2163 {
2164         u32 pts_control_value;
2165
2166         pts_control_value = readl(&port->port_task_scheduler_registers->control);
2167         pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
2168         writel(pts_control_value, &port->port_task_scheduler_registers->control);
2169 }
2170
2171 /**
2172  *
2173  * @sci_port: This is the port object which to resume.
2174  *
2175  * This method will disable the SCU port task scheduler for this port object.
2176  * none
2177  */
2178 static void
2179 scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
2180 {
2181         u32 pts_control_value;
2182
2183         pts_control_value = readl(&port->port_task_scheduler_registers->control);
2184         pts_control_value &=
2185                 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
2186         writel(pts_control_value, &port->port_task_scheduler_registers->control);
2187 }
2188
2189 static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
2190 {
2191         struct scic_sds_controller *scic = sci_port->owning_controller;
2192         u8 phys_index = sci_port->physical_port_index;
2193         union scu_remote_node_context *rnc;
2194         u16 rni = sci_port->reserved_rni;
2195         u32 command;
2196
2197         rnc = &scic->remote_node_context_table[rni];
2198         rnc->ssp.is_valid = true;
2199
2200         command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
2201                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2202
2203         scic_sds_controller_post_request(scic, command);
2204
2205         /* ensure hardware has seen the post rnc command and give it
2206          * ample time to act before sending the suspend
2207          */
2208         readl(&scic->smu_registers->interrupt_status); /* flush */
2209         udelay(10);
2210
2211         command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
2212                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2213
2214         scic_sds_controller_post_request(scic, command);
2215 }
2216
2217 static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
2218 {
2219         struct scic_sds_controller *scic = sci_port->owning_controller;
2220         u8 phys_index = sci_port->physical_port_index;
2221         union scu_remote_node_context *rnc;
2222         u16 rni = sci_port->reserved_rni;
2223         u32 command;
2224
2225         rnc = &scic->remote_node_context_table[rni];
2226
2227         rnc->ssp.is_valid = false;
2228
2229         /* ensure the preceding tc abort request has reached the
2230          * controller and give it ample time to act before posting the rnc
2231          * invalidate
2232          */
2233         readl(&scic->smu_registers->interrupt_status); /* flush */
2234         udelay(10);
2235
2236         command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
2237                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2238
2239         scic_sds_controller_post_request(scic, command);
2240 }
2241
2242 /*
2243  * ******************************************************************************
2244  * *  PORT STATE METHODS
2245  * ****************************************************************************** */
2246
2247 /**
2248  *
2249  * @object: This is the object which is cast to a struct scic_sds_port object.
2250  *
2251  * This method will perform the actions required by the struct scic_sds_port on
2252  * entering the SCI_BASE_PORT_STATE_STOPPED. This function sets the stopped
2253  * state handlers for the struct scic_sds_port object and disables the port task
2254  * scheduler in the hardware. none
2255  */
2256 static void scic_sds_port_stopped_state_enter(void *object)
2257 {
2258         struct scic_sds_port *sci_port;
2259
2260         sci_port = (struct scic_sds_port *)object;
2261
2262         scic_sds_port_set_base_state_handlers(
2263                 sci_port, SCI_BASE_PORT_STATE_STOPPED
2264                 );
2265
2266         if (
2267                 SCI_BASE_PORT_STATE_STOPPING
2268                 == sci_port->state_machine.previous_state_id
2269                 ) {
2270                 /*
2271                  * If we enter this state becasuse of a request to stop
2272                  * the port then we want to disable the hardwares port
2273                  * task scheduler. */
2274                 scic_sds_port_disable_port_task_scheduler(sci_port);
2275         }
2276 }
2277
2278 /**
2279  *
2280  * @object: This is the object which is cast to a struct scic_sds_port object.
2281  *
2282  * This method will perform the actions required by the struct scic_sds_port on
2283  * exiting the SCI_BASE_STATE_STOPPED. This function enables the SCU hardware
2284  * port task scheduler. none
2285  */
2286 static void scic_sds_port_stopped_state_exit(void *object)
2287 {
2288         struct scic_sds_port *sci_port;
2289
2290         sci_port = (struct scic_sds_port *)object;
2291
2292         /* Enable and suspend the port task scheduler */
2293         scic_sds_port_enable_port_task_scheduler(sci_port);
2294 }
2295
2296 /**
2297  * scic_sds_port_ready_state_enter -
2298  * @object: This is the object which is cast to a struct scic_sds_port object.
2299  *
2300  * This method will perform the actions required by the struct scic_sds_port on
2301  * entering the SCI_BASE_PORT_STATE_READY. This function sets the ready state
2302  * handlers for the struct scic_sds_port object, reports the port object as
2303  * not ready and starts the ready substate machine. none
2304  */
2305 static void scic_sds_port_ready_state_enter(void *object)
2306 {
2307         struct scic_sds_controller *scic;
2308         struct scic_sds_port *sci_port;
2309         struct isci_port *iport;
2310         struct isci_host *ihost;
2311         u32 prev_state;
2312
2313         sci_port = container_of(object, typeof(*sci_port), parent);
2314         scic = scic_sds_port_get_controller(sci_port);
2315         ihost = scic->ihost;
2316         iport = sci_object_get_association(sci_port);
2317
2318         /* Put the ready state handlers in place though they will not be there long */
2319         scic_sds_port_set_base_state_handlers(sci_port, SCI_BASE_PORT_STATE_READY);
2320
2321         prev_state = sci_port->state_machine.previous_state_id;
2322         if (prev_state  == SCI_BASE_PORT_STATE_RESETTING)
2323                 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
2324         else
2325                 isci_port_not_ready(ihost, iport);
2326
2327         /* Post and suspend the dummy remote node context for this port. */
2328         scic_sds_port_post_dummy_remote_node(sci_port);
2329
2330         /* Start the ready substate machine */
2331         sci_base_state_machine_start(&sci_port->ready_substate_machine);
2332 }
2333
2334 static void scic_sds_port_ready_state_exit(void *object)
2335 {
2336         struct scic_sds_port *sci_port;
2337
2338         sci_port = container_of(object, typeof(*sci_port), parent);
2339         sci_base_state_machine_stop(&sci_port->ready_substate_machine);
2340         scic_sds_port_invalidate_dummy_remote_node(sci_port);
2341 }
2342
2343 /**
2344  *
2345  * @object: This is the object which is cast to a struct scic_sds_port object.
2346  *
2347  * This method will perform the actions required by the struct scic_sds_port on
2348  * entering the SCI_BASE_PORT_STATE_RESETTING. This function sets the resetting
2349  * state handlers for the struct scic_sds_port object. none
2350  */
2351 static void scic_sds_port_resetting_state_enter(void *object)
2352 {
2353         struct scic_sds_port *sci_port;
2354
2355         sci_port = (struct scic_sds_port *)object;
2356
2357         scic_sds_port_set_base_state_handlers(
2358                 sci_port, SCI_BASE_PORT_STATE_RESETTING
2359                 );
2360 }
2361
2362 /**
2363  *
2364  * @object: This is the object which is cast to a struct scic_sds_port object.
2365  *
2366  * This function will perform the actions required by the
2367  * struct scic_sds_port on
2368  * exiting the SCI_BASE_STATE_RESETTING. This function does nothing. none
2369  */
2370 static inline void scic_sds_port_resetting_state_exit(void *object)
2371 {
2372         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
2373
2374         isci_timer_stop(sci_port->timer_handle);
2375 }
2376
2377 /**
2378  *
2379  * @object: This is the void object which is cast to a
2380  * struct scic_sds_port object.
2381  *
2382  * This method will perform the actions required by the struct scic_sds_port on
2383  * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
2384  * state handlers for the struct scic_sds_port object. none
2385  */
2386 static void scic_sds_port_stopping_state_enter(void *object)
2387 {
2388         struct scic_sds_port *sci_port;
2389
2390         sci_port = (struct scic_sds_port *)object;
2391
2392         scic_sds_port_set_base_state_handlers(
2393                 sci_port, SCI_BASE_PORT_STATE_STOPPING
2394                 );
2395 }
2396
2397 /**
2398  *
2399  * @object: This is the object which is cast to a struct scic_sds_port object.
2400  *
2401  * This function will perform the actions required by the
2402  * struct scic_sds_port on
2403  * exiting the SCI_BASE_STATE_STOPPING. This function does nothing. none
2404  */
2405 static inline void
2406 scic_sds_port_stopping_state_exit(void *object)
2407 {
2408         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
2409
2410         isci_timer_stop(sci_port->timer_handle);
2411
2412         scic_sds_port_destroy_dummy_resources(sci_port);
2413 }
2414
2415 /**
2416  *
2417  * @object: This is the object which is cast to a struct scic_sds_port object.
2418  *
2419  * This function will perform the actions required by the
2420  * struct scic_sds_port on
2421  * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
2422  * state handlers for the struct scic_sds_port object. none
2423  */
2424 static void scic_sds_port_failed_state_enter(void *object)
2425 {
2426         struct scic_sds_port *sci_port = (struct scic_sds_port *)object;
2427         struct isci_port *iport = sci_object_get_association(sci_port);
2428
2429         scic_sds_port_set_base_state_handlers(sci_port,
2430                                               SCI_BASE_PORT_STATE_FAILED);
2431
2432         isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
2433 }
2434
2435 /* --------------------------------------------------------------------------- */
2436
2437 static const struct sci_base_state scic_sds_port_state_table[] = {
2438         [SCI_BASE_PORT_STATE_STOPPED] = {
2439                 .enter_state = scic_sds_port_stopped_state_enter,
2440                 .exit_state  = scic_sds_port_stopped_state_exit
2441         },
2442         [SCI_BASE_PORT_STATE_STOPPING] = {
2443                 .enter_state = scic_sds_port_stopping_state_enter,
2444                 .exit_state  = scic_sds_port_stopping_state_exit
2445         },
2446         [SCI_BASE_PORT_STATE_READY] = {
2447                 .enter_state = scic_sds_port_ready_state_enter,
2448                 .exit_state  = scic_sds_port_ready_state_exit
2449         },
2450         [SCI_BASE_PORT_STATE_RESETTING] = {
2451                 .enter_state = scic_sds_port_resetting_state_enter,
2452                 .exit_state  = scic_sds_port_resetting_state_exit
2453         },
2454         [SCI_BASE_PORT_STATE_FAILED] = {
2455                 .enter_state = scic_sds_port_failed_state_enter,
2456         }
2457 };
2458
2459 void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 port_index,
2460                              struct scic_sds_controller *scic)
2461 {
2462         u32 index;
2463
2464         sci_port->parent.private = NULL;
2465         sci_base_state_machine_construct(&sci_port->state_machine,
2466                                          &sci_port->parent,
2467                                          scic_sds_port_state_table,
2468                                          SCI_BASE_PORT_STATE_STOPPED);
2469
2470         sci_base_state_machine_start(&sci_port->state_machine);
2471
2472         sci_base_state_machine_construct(&sci_port->ready_substate_machine,
2473                                          &sci_port->parent,
2474                                          scic_sds_port_ready_substate_table,
2475                                          SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
2476
2477         sci_port->logical_port_index  = SCIC_SDS_DUMMY_PORT;
2478         sci_port->physical_port_index = port_index;
2479         sci_port->active_phy_mask     = 0;
2480
2481         sci_port->owning_controller = scic;
2482
2483         sci_port->started_request_count = 0;
2484         sci_port->assigned_device_count = 0;
2485
2486         sci_port->reserved_rni = SCU_DUMMY_INDEX;
2487         sci_port->reserved_tci = SCU_DUMMY_INDEX;
2488
2489         sci_port->timer_handle = NULL;
2490
2491         sci_port->port_task_scheduler_registers = NULL;
2492
2493         for (index = 0; index < SCI_MAX_PHYS; index++)
2494                 sci_port->phy_table[index] = NULL;
2495 }