isci: Intel(R) C600 Series Chipset Storage Control Unit Driver
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / isci / core / scic_sds_phy.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_ata.h"
57 #include "intel_sata.h"
58 #include "sci_base_state.h"
59 #include "sci_base_state_machine.h"
60 #include "scic_phy.h"
61 #include "scic_sds_controller.h"
62 #include "scic_sds_phy.h"
63 #include "scic_sds_phy_registers.h"
64 #include "scic_sds_port.h"
65 #include "scic_user_callback.h"
66 #include "sci_environment.h"
67 #include "sci_util.h"
68 #include "scu_event_codes.h"
69
70 #define SCIC_SDS_PHY_MIN_TIMER_COUNT  (SCI_MAX_PHYS)
71 #define SCIC_SDS_PHY_MAX_TIMER_COUNT  (SCI_MAX_PHYS)
72
73 /* Maximum arbitration wait time in micro-seconds */
74 #define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME  (700)
75
76 /*
77  * *****************************************************************************
78  * * SCIC SDS PHY Internal Methods
79  * ***************************************************************************** */
80
81 /**
82  * This method will initialize the phy link layer registers
83  * @this_phy:
84  * @link_layer_registers:
85  *
86  * enum sci_status
87  */
88 static enum sci_status scic_sds_phy_link_layer_initialization(
89         struct scic_sds_phy *this_phy,
90         struct scu_link_layer_registers *link_layer_registers)
91 {
92         u32 phy_configuration;
93         struct sas_capabilities phy_capabilities;
94         u32 parity_check = 0;
95         u32 parity_count = 0;
96         u32 link_layer_control;
97
98         this_phy->link_layer_registers = link_layer_registers;
99
100         /* Set our IDENTIFY frame data */
101    #define SCI_END_DEVICE 0x01
102
103         SCU_SAS_TIID_WRITE(
104                 this_phy,
105                 (SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR)
106                  | SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR)
107                  | SCU_SAS_TIID_GEN_BIT(STP_INITIATOR)
108                  | SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST)
109                  | SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE))
110                 );
111
112         /* Write the device SAS Address */
113         SCU_SAS_TIDNH_WRITE(this_phy, 0xFEDCBA98);
114         SCU_SAS_TIDNL_WRITE(this_phy, this_phy->phy_index);
115
116         /* Write the source SAS Address */
117         SCU_SAS_TISSAH_WRITE(
118                 this_phy,
119                 this_phy->owning_port->owning_controller->oem_parameters.sds1.phys[
120                         this_phy->phy_index].sas_address.high
121                 );
122         SCU_SAS_TISSAL_WRITE(
123                 this_phy,
124                 this_phy->owning_port->owning_controller->oem_parameters.sds1.phys[
125                         this_phy->phy_index].sas_address.low
126                 );
127
128         /* Clear and Set the PHY Identifier */
129         SCU_SAS_TIPID_WRITE(this_phy, 0x00000000);
130         SCU_SAS_TIPID_WRITE(this_phy, SCU_SAS_TIPID_GEN_VALUE(ID, this_phy->phy_index));
131
132         /* Change the initial state of the phy configuration register */
133         phy_configuration = SCU_SAS_PCFG_READ(this_phy);
134
135         /* Hold OOB state machine in reset */
136         phy_configuration |=  SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
137         SCU_SAS_PCFG_WRITE(this_phy, phy_configuration);
138
139         /* Configure the SNW capabilities */
140         phy_capabilities.u.all = 0;
141         phy_capabilities.u.bits.start                      = 1;
142         phy_capabilities.u.bits.gen3_without_ssc_supported = 1;
143         phy_capabilities.u.bits.gen2_without_ssc_supported = 1;
144         phy_capabilities.u.bits.gen1_without_ssc_supported = 1;
145         if (this_phy->owning_port->owning_controller->oem_parameters.sds1.
146             controller.do_enable_ssc == true) {
147                 phy_capabilities.u.bits.gen3_with_ssc_supported = 1;
148                 phy_capabilities.u.bits.gen2_with_ssc_supported = 1;
149                 phy_capabilities.u.bits.gen1_with_ssc_supported = 1;
150         }
151
152         /*
153          * The SAS specification indicates that the phy_capabilities that
154          * are transmitted shall have an even parity.  Calculate the parity. */
155         parity_check = phy_capabilities.u.all;
156         while (parity_check != 0) {
157                 if (parity_check & 0x1)
158                         parity_count++;
159                 parity_check >>= 1;
160         }
161
162         /*
163          * If parity indicates there are an odd number of bits set, then
164          * set the parity bit to 1 in the phy capabilities. */
165         if ((parity_count % 2) != 0)
166                 phy_capabilities.u.bits.parity = 1;
167
168         SCU_SAS_PHYCAP_WRITE(this_phy, phy_capabilities.u.all);
169
170         /* Set the enable spinup period but disable the ability to send notify enable spinup */
171         SCU_SAS_ENSPINUP_WRITE(this_phy, SCU_ENSPINUP_GEN_VAL(COUNT, 0x33));
172
173 #if defined(CONFIG_PBG_HBA_A0) || defined(CONFIG_PBG_HBA_A2) || defined(CONFIG_PBG_HBA_BETA)
174         /* / @todo Provide a way to write this register correctly */
175         scu_link_layer_register_write(this_phy, afe_lookup_table_control, 0x02108421);
176 #else
177         /* / @todo Provide a way to write this register correctly */
178         scu_link_layer_register_write(this_phy, afe_lookup_table_control, 0x0e739ce7);
179 #endif
180
181         link_layer_control = SCU_SAS_LLCTL_GEN_VAL(
182                 NO_OUTBOUND_TASK_TIMEOUT,
183                 (u8)this_phy->owning_port->owning_controller->
184                 user_parameters.sds1.no_outbound_task_timeout
185                 );
186
187 /* #define COMPILED_MAX_LINK_RATE SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1 */
188 /* #define COMPILED_MAX_LINK_RATE SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2 */
189 #define COMPILED_MAX_LINK_RATE SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3
190
191         if (this_phy->owning_port->owning_controller->user_parameters.sds1.
192             phys[this_phy->phy_index].max_speed_generation == SCIC_SDS_PARM_GEN3_SPEED) {
193                 link_layer_control |= SCU_SAS_LLCTL_GEN_VAL(
194                         MAX_LINK_RATE, COMPILED_MAX_LINK_RATE
195                         );
196         } else if (this_phy->owning_port->owning_controller->user_parameters.sds1.
197                    phys[this_phy->phy_index].max_speed_generation == SCIC_SDS_PARM_GEN2_SPEED) {
198                 link_layer_control |= SCU_SAS_LLCTL_GEN_VAL(
199                         MAX_LINK_RATE,
200                         min(
201                                 SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2,
202                                 COMPILED_MAX_LINK_RATE)
203                         );
204         } else {
205                 link_layer_control |= SCU_SAS_LLCTL_GEN_VAL(
206                         MAX_LINK_RATE,
207                         min(
208                                 SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1,
209                                 COMPILED_MAX_LINK_RATE)
210                         );
211         }
212
213         scu_link_layer_register_write(
214                 this_phy, link_layer_control, link_layer_control
215                 );
216
217         /*
218          * Program the max ARB time for the PHY to 700us so we inter-operate with
219          * the PMC expander which shuts down PHYs if the expander PHY generates too
220          * many breaks.  This time value will guarantee that the initiator PHY will
221          * generate the break. */
222 #if defined(CONFIG_PBG_HBA_A0) || defined(CONFIG_PBG_HBA_A2)
223         scu_link_layer_register_write(
224                 this_phy,
225                 maximum_arbitration_wait_timer_timeout,
226                 SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME
227                 );
228 #endif  /* defined(CONFIG_PBG_HBA_A0) || defined(CONFIG_PBG_HBA_A2) */
229
230         /*
231          * Set the link layer hang detection to 500ms (0x1F4) from its default
232          * value of 128ms.  Max value is 511 ms. */
233         scu_link_layer_register_write(
234                 this_phy, link_layer_hang_detection_timeout, 0x1F4
235                 );
236
237         /* We can exit the initial state to the stopped state */
238         sci_base_state_machine_change_state(
239                 scic_sds_phy_get_base_state_machine(this_phy),
240                 SCI_BASE_PHY_STATE_STOPPED
241                 );
242
243         return SCI_SUCCESS;
244 }
245
246 /**
247  * This function will handle the sata SIGNATURE FIS timeout condition.  It will
248  * restart the starting substate machine since we dont know what has actually
249  * happening.
250  */
251 static void scic_sds_phy_sata_timeout(void *phy)
252 {
253         struct scic_sds_phy *sci_phy = phy;
254
255         dev_dbg(sciphy_to_dev(sci_phy),
256                  "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
257                  "timeout.\n",
258                  __func__,
259                  sci_phy);
260
261         sci_base_state_machine_stop(
262                 scic_sds_phy_get_starting_substate_machine(sci_phy));
263
264         sci_base_state_machine_change_state(
265                 scic_sds_phy_get_base_state_machine(sci_phy),
266                 SCI_BASE_PHY_STATE_STARTING
267                 );
268 }
269
270 /*
271  * *****************************************************************************
272  * * SCIC SDS PHY External Methods
273  * ***************************************************************************** */
274
275 /**
276  * This method returns the object size for a phy object.
277  *
278  * u32
279  */
280
281 /**
282  * This method returns the minimum number of timers required for a phy object.
283  *
284  * u32
285  */
286
287 /**
288  * This method returns the maximum number of timers required for a phy object.
289  *
290  * u32
291  */
292
293 #ifdef SCIC_DEBUG_ENABLED
294 /**
295  * scic_sds_phy_observe_state_change() -
296  * @our_observer:
297  *
298  * Debug code to record the state transitions in the phy
299  */
300 void scic_sds_phy_observe_state_change(
301         struct sci_base_observer *our_observer,
302         struct sci_base_subject *the_subject)
303 {
304         struct scic_sds_phy *this_phy;
305         struct sci_base_state_machine *the_state_machine;
306
307         u8 transition_requestor;
308         u32 base_state_id;
309         u32 starting_substate_id;
310
311         the_state_machine = (struct sci_base_state_machine *)the_subject;
312         this_phy = (struct scic_sds_phy *)the_state_machine->state_machine_owner;
313
314         if (the_state_machine == &this_phy->parent.state_machine) {
315                 transition_requestor = 0x01;
316         } else if (the_state_machine == &this_phy->starting_substate_machine) {
317                 transition_requestor = 0x02;
318         } else {
319                 transition_requestor = 0xFF;
320         }
321
322         base_state_id =
323                 sci_base_state_machine_get_state(&this_phy->parent.state_machine);
324         starting_substate_id =
325                 sci_base_state_machine_get_state(&this_phy->starting_substate_machine);
326
327         this_phy->state_record.state_transition_table[
328                 this_phy->state_record.index++] = ((transition_requestor << 24)
329                                                    | ((u8)base_state_id << 8)
330                                                    | ((u8)starting_substate_id));
331
332         this_phy->state_record.index =
333                 this_phy->state_record.index & (MAX_STATE_TRANSITION_RECORD - 1);
334
335 }
336 #endif /* SCIC_DEBUG_ENABLED */
337
338 #ifdef SCIC_DEBUG_ENABLED
339 /**
340  * scic_sds_phy_initialize_state_recording() -
341  *
342  * This method initializes the state record debug information for the phy
343  * object. The state machines for the phy object must be constructed before
344  * this function is called.
345  */
346 void scic_sds_phy_initialize_state_recording(
347         struct scic_sds_phy *this_phy)
348 {
349         this_phy->state_record.index = 0;
350
351         sci_base_observer_initialize(
352                 &this_phy->state_record.base_state_observer,
353                 scic_sds_phy_observe_state_change,
354                 &this_phy->parent.state_machine.parent
355                 );
356
357         sci_base_observer_initialize(
358                 &this_phy->state_record.starting_state_observer,
359                 scic_sds_phy_observe_state_change,
360                 &this_phy->starting_substate_machine.parent
361                 );
362 }
363 #endif /* SCIC_DEBUG_ENABLED */
364
365 /**
366  * This method will construct the struct scic_sds_phy object
367  * @this_phy:
368  * @owning_port:
369  * @phy_index:
370  *
371  */
372 void scic_sds_phy_construct(
373         struct scic_sds_phy *this_phy,
374         struct scic_sds_port *owning_port,
375         u8 phy_index)
376 {
377         /*
378          * Call the base constructor first
379          */
380         sci_base_phy_construct(
381                 &this_phy->parent,
382                 scic_sds_phy_state_table
383                 );
384
385         /* Copy the rest of the input data to our locals */
386         this_phy->owning_port = owning_port;
387         this_phy->phy_index = phy_index;
388         this_phy->bcn_received_while_port_unassigned = false;
389         this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
390         this_phy->link_layer_registers = NULL;
391         this_phy->max_negotiated_speed = SCI_SAS_NO_LINK_RATE;
392
393         /* Clear out the identification buffer data */
394         memset(&this_phy->phy_type, 0, sizeof(this_phy->phy_type));
395
396         /* Initialize the the substate machines */
397         sci_base_state_machine_construct(
398                 &this_phy->starting_substate_machine,
399                 &this_phy->parent.parent,
400                 scic_sds_phy_starting_substates,
401                 SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL
402                 );
403
404    #ifdef SCIC_DEBUG_ENABLED
405         scic_sds_phy_initialize_state_recording(this_phy);
406    #endif /* SCIC_DEBUG_ENABLED */
407 }
408
409 /**
410  * This method returns the port currently containing this phy. If the phy is
411  *    currently contained by the dummy port, then the phy is considered to not
412  *    be part of a port.
413  * @this_phy: This parameter specifies the phy for which to retrieve the
414  *    containing port.
415  *
416  * This method returns a handle to a port that contains the supplied phy.
417  * SCI_INVALID_HANDLE This value is returned if the phy is not part of a real
418  * port (i.e. it's contained in the dummy port). !SCI_INVALID_HANDLE All other
419  * values indicate a handle/pointer to the port containing the phy.
420  */
421 struct scic_sds_port *scic_sds_phy_get_port(
422         struct scic_sds_phy *this_phy)
423 {
424         if (scic_sds_port_get_index(this_phy->owning_port) == SCIC_SDS_DUMMY_PORT)
425                 return SCI_INVALID_HANDLE;
426
427         return this_phy->owning_port;
428 }
429
430 /**
431  * This method will assign a port to the phy object.
432  * @out]: this_phy This parameter specifies the phy for which to assign a port
433  *    object.
434  *
435  *
436  */
437 void scic_sds_phy_set_port(
438         struct scic_sds_phy *this_phy,
439         struct scic_sds_port *the_port)
440 {
441         this_phy->owning_port = the_port;
442
443         if (this_phy->bcn_received_while_port_unassigned) {
444                 this_phy->bcn_received_while_port_unassigned = false;
445                 scic_sds_port_broadcast_change_received(this_phy->owning_port, this_phy);
446         }
447 }
448
449 /**
450  * This method will initialize the constructed phy
451  * @sci_phy:
452  * @link_layer_registers:
453  *
454  * enum sci_status
455  */
456 enum sci_status scic_sds_phy_initialize(
457         struct scic_sds_phy *sci_phy,
458         struct scu_link_layer_registers *link_layer_registers)
459 {
460         /* Create the SIGNATURE FIS Timeout timer for this phy */
461         sci_phy->sata_timeout_timer = scic_cb_timer_create(
462                 scic_sds_phy_get_controller(sci_phy),
463                 scic_sds_phy_sata_timeout,
464                 sci_phy
465                 );
466
467         /* Perofrm the initialization of the PE hardware */
468         scic_sds_phy_link_layer_initialization(sci_phy, link_layer_registers);
469
470         /*
471          * There is nothing that needs to be done in this state just
472          * transition to the stopped state. */
473         sci_base_state_machine_change_state(
474                 scic_sds_phy_get_base_state_machine(sci_phy),
475                 SCI_BASE_PHY_STATE_STOPPED
476                 );
477
478         return SCI_SUCCESS;
479 }
480
481
482 /**
483  *
484  * @this_phy: The phy object to be suspended.
485  *
486  * This function will perform the register reads/writes to suspend the SCU
487  * hardware protocol engine. none
488  */
489 void scic_sds_phy_suspend(
490         struct scic_sds_phy *this_phy)
491 {
492         u32 scu_sas_pcfg_value;
493
494         scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);
495
496         scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
497
498         SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
499 }
500
501 /**
502  *
503  * @this_phy: The phy object to resume.
504  *
505  * This function will perform the register reads/writes required to resume the
506  * SCU hardware protocol engine. none
507  */
508 void scic_sds_phy_resume(
509         struct scic_sds_phy *this_phy)
510 {
511         u32 scu_sas_pcfg_value;
512
513         scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);
514
515         scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
516
517         SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
518 }
519
520 /**
521  * This method returns the local sas address assigned to this phy.
522  * @this_phy: This parameter specifies the phy for which to retrieve the local
523  *    SAS address.
524  * @sas_address: This parameter specifies the location into which to copy the
525  *    local SAS address.
526  *
527  */
528 void scic_sds_phy_get_sas_address(
529         struct scic_sds_phy *this_phy,
530         struct sci_sas_address *sas_address)
531 {
532         sas_address->high = SCU_SAS_TISSAH_READ(this_phy);
533         sas_address->low  = SCU_SAS_TISSAL_READ(this_phy);
534 }
535
536 /**
537  * This method returns the remote end-point (i.e. attached) sas address
538  *    assigned to this phy.
539  * @this_phy: This parameter specifies the phy for which to retrieve the remote
540  *    end-point SAS address.
541  * @sas_address: This parameter specifies the location into which to copy the
542  *    remote end-point SAS address.
543  *
544  */
545 void scic_sds_phy_get_attached_sas_address(
546         struct scic_sds_phy *this_phy,
547         struct sci_sas_address *sas_address)
548 {
549         sas_address->high
550                 = this_phy->phy_type.sas.identify_address_frame_buffer.sas_address.high;
551         sas_address->low
552                 = this_phy->phy_type.sas.identify_address_frame_buffer.sas_address.low;
553 }
554
555 /**
556  * This method returns the supported protocols assigned to this phy
557  * @this_phy:
558  *
559  *
560  */
561 void scic_sds_phy_get_protocols(
562         struct scic_sds_phy *this_phy,
563         struct sci_sas_identify_address_frame_protocols *protocols)
564 {
565         protocols->u.all = (u16)(SCU_SAS_TIID_READ(this_phy) & 0x0000FFFF);
566 }
567
568 /**
569  *
570  * @this_phy: The parameter is the phy object for which the attached phy
571  *    protcols are to be returned.
572  *
573  * This method returns the supported protocols for the attached phy.  If this
574  * is a SAS phy the protocols are returned from the identify address frame. If
575  * this is a SATA phy then protocols are made up and the target phy is an STP
576  * target phy. The caller will get the entire set of bits for the protocol
577  * value.
578  */
579 void scic_sds_phy_get_attached_phy_protocols(
580         struct scic_sds_phy *this_phy,
581         struct sci_sas_identify_address_frame_protocols *protocols)
582 {
583         protocols->u.all = 0;
584
585         if (this_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
586                 protocols->u.all =
587                         this_phy->phy_type.sas.identify_address_frame_buffer.protocols.u.all;
588         } else if (this_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
589                 protocols->u.bits.stp_target = 1;
590         }
591 }
592
593 /*
594  * *****************************************************************************
595  * * SCIC SDS PHY Handler Redirects
596  * ***************************************************************************** */
597
598 /**
599  * This method will attempt to start the phy object. This request is only valid
600  *    when the phy is in the stopped state
601  * @this_phy:
602  *
603  * enum sci_status
604  */
605 enum sci_status scic_sds_phy_start(
606         struct scic_sds_phy *this_phy)
607 {
608         return this_phy->state_handlers->parent.start_handler(&this_phy->parent);
609 }
610
611 /**
612  * This method will attempt to stop the phy object.
613  * @this_phy:
614  *
615  * enum sci_status SCI_SUCCESS if the phy is going to stop SCI_INVALID_STATE if the
616  * phy is not in a valid state to stop
617  */
618 enum sci_status scic_sds_phy_stop(
619         struct scic_sds_phy *this_phy)
620 {
621         return this_phy->state_handlers->parent.stop_handler(&this_phy->parent);
622 }
623
624 /**
625  * This method will attempt to reset the phy.  This request is only valid when
626  *    the phy is in an ready state
627  * @this_phy:
628  *
629  * enum sci_status
630  */
631 enum sci_status scic_sds_phy_reset(
632         struct scic_sds_phy *this_phy)
633 {
634         return this_phy->state_handlers->parent.reset_handler(
635                        &this_phy->parent
636                        );
637 }
638
639 /**
640  * This method will process the event code received.
641  * @this_phy:
642  * @event_code:
643  *
644  * enum sci_status
645  */
646 enum sci_status scic_sds_phy_event_handler(
647         struct scic_sds_phy *this_phy,
648         u32 event_code)
649 {
650         return this_phy->state_handlers->event_handler(this_phy, event_code);
651 }
652
653 /**
654  * This method will process the frame index received.
655  * @this_phy:
656  * @frame_index:
657  *
658  * enum sci_status
659  */
660 enum sci_status scic_sds_phy_frame_handler(
661         struct scic_sds_phy *this_phy,
662         u32 frame_index)
663 {
664         return this_phy->state_handlers->frame_handler(this_phy, frame_index);
665 }
666
667 /**
668  * This method will give the phy permission to consume power
669  * @this_phy:
670  *
671  * enum sci_status
672  */
673 enum sci_status scic_sds_phy_consume_power_handler(
674         struct scic_sds_phy *this_phy)
675 {
676         return this_phy->state_handlers->consume_power_handler(this_phy);
677 }
678
679 /*
680  * *****************************************************************************
681  * * SCIC PHY Public Methods
682  * ***************************************************************************** */
683
684
685 enum sci_status scic_sas_phy_get_properties(
686         struct scic_sds_phy *sci_phy,
687         struct scic_sas_phy_properties *properties)
688 {
689         if (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
690                 memcpy(
691                         &properties->received_iaf,
692                         &sci_phy->phy_type.sas.identify_address_frame_buffer,
693                         sizeof(struct sci_sas_identify_address_frame)
694                         );
695
696                 properties->received_capabilities.u.all
697                         = SCU_SAS_RECPHYCAP_READ(sci_phy);
698
699                 return SCI_SUCCESS;
700         }
701
702         return SCI_FAILURE;
703 }
704
705
706 enum sci_status scic_sata_phy_get_properties(
707         struct scic_sds_phy *sci_phy,
708         struct scic_sata_phy_properties *properties)
709 {
710         if (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
711                 memcpy(
712                         &properties->signature_fis,
713                         &sci_phy->phy_type.sata.signature_fis_buffer,
714                         sizeof(struct sata_fis_reg_d2h)
715                         );
716
717                 /* / @todo add support for port selectors. */
718                 properties->is_port_selector_present = false;
719
720                 return SCI_SUCCESS;
721         }
722
723         return SCI_FAILURE;
724 }
725
726 /*
727  * *****************************************************************************
728  * * SCIC SDS PHY HELPER FUNCTIONS
729  * ***************************************************************************** */
730
731
732 /**
733  *
734  * @this_phy: The phy object that received SAS PHY DETECTED.
735  *
736  * This method continues the link training for the phy as if it were a SAS PHY
737  * instead of a SATA PHY. This is done because the completion queue had a SAS
738  * PHY DETECTED event when the state machine was expecting a SATA PHY event.
739  * none
740  */
741 static void scic_sds_phy_start_sas_link_training(
742         struct scic_sds_phy *this_phy)
743 {
744         u32 phy_control;
745
746         phy_control = SCU_SAS_PCFG_READ(this_phy);
747         phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
748         SCU_SAS_PCFG_WRITE(this_phy, phy_control);
749
750         sci_base_state_machine_change_state(
751                 &this_phy->starting_substate_machine,
752                 SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN
753                 );
754
755         this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
756 }
757
758 /**
759  *
760  * @this_phy: The phy object that received a SATA SPINUP HOLD event
761  *
762  * This method continues the link training for the phy as if it were a SATA PHY
763  * instead of a SAS PHY.  This is done because the completion queue had a SATA
764  * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
765  */
766 static void scic_sds_phy_start_sata_link_training(
767         struct scic_sds_phy *this_phy)
768 {
769         sci_base_state_machine_change_state(
770                 &this_phy->starting_substate_machine,
771                 SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER
772                 );
773
774         this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
775 }
776
777 /**
778  * This method performs processing common to all protocols upon completion of
779  *    link training.
780  * @this_phy: This parameter specifies the phy object for which link training
781  *    has completed.
782  * @max_link_rate: This parameter specifies the maximum link rate to be
783  *    associated with this phy.
784  * @next_state: This parameter specifies the next state for the phy's starting
785  *    sub-state machine.
786  *
787  */
788 static void scic_sds_phy_complete_link_training(
789         struct scic_sds_phy *this_phy,
790         enum sci_sas_link_rate max_link_rate,
791         u32 next_state)
792 {
793         this_phy->max_negotiated_speed = max_link_rate;
794
795         sci_base_state_machine_change_state(
796                 scic_sds_phy_get_starting_substate_machine(this_phy), next_state
797                 );
798 }
799
800 /**
801  *
802  * @this_phy: The struct scic_sds_phy object to restart.
803  *
804  * This method restarts the struct scic_sds_phy objects base state machine in the
805  * starting state from any starting substate. none
806  */
807 static void scic_sds_phy_restart_starting_state(
808         struct scic_sds_phy *this_phy)
809 {
810         /* Stop the current substate machine */
811         sci_base_state_machine_stop(
812                 scic_sds_phy_get_starting_substate_machine(this_phy)
813                 );
814
815         /* Re-enter the base state machine starting state */
816         sci_base_state_machine_change_state(
817                 scic_sds_phy_get_base_state_machine(this_phy),
818                 SCI_BASE_PHY_STATE_STARTING
819                 );
820 }
821
822 /*
823  * *****************************************************************************
824  * * SCIC SDS PHY EVENT_HANDLERS
825  * ***************************************************************************** */
826
827 /**
828  *
829  * @phy: This struct scic_sds_phy object which has received an event.
830  * @event_code: This is the event code which the phy object is to decode.
831  *
832  * This method is called when an event notification is received for the phy
833  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SPEED_EN. -
834  * decode the event - sas phy detected causes a state transition to the wait
835  * for speed event notification. - any other events log a warning message and
836  * set a failure status enum sci_status SCI_SUCCESS on any valid event notification
837  * SCI_FAILURE on any unexpected event notifation
838  */
839 static enum sci_status scic_sds_phy_starting_substate_await_ossp_event_handler(
840         struct scic_sds_phy *this_phy,
841         u32 event_code)
842 {
843         u32 result = SCI_SUCCESS;
844
845         switch (scu_get_event_code(event_code)) {
846         case SCU_EVENT_SAS_PHY_DETECTED:
847                 scic_sds_phy_start_sas_link_training(this_phy);
848                 this_phy->is_in_link_training = true;
849                 break;
850
851         case SCU_EVENT_SATA_SPINUP_HOLD:
852                 scic_sds_phy_start_sata_link_training(this_phy);
853                 this_phy->is_in_link_training = true;
854                 break;
855
856         default:
857                 dev_warn(sciphy_to_dev(this_phy),
858                          "%s: PHY starting substate machine received "
859                          "unexpected event_code %x\n",
860                          __func__,
861                          event_code);
862
863                 result = SCI_FAILURE;
864                 break;
865         }
866
867         return result;
868 }
869
870 /**
871  *
872  * @phy: This struct scic_sds_phy object which has received an event.
873  * @event_code: This is the event code which the phy object is to decode.
874  *
875  * This method is called when an event notification is received for the phy
876  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SPEED_EN. -
877  * decode the event - sas phy detected returns us back to this state. - speed
878  * event detected causes a state transition to the wait for iaf. - identify
879  * timeout is an un-expected event and the state machine is restarted. - link
880  * failure events restart the starting state machine - any other events log a
881  * warning message and set a failure status enum sci_status SCI_SUCCESS on any valid
882  * event notification SCI_FAILURE on any unexpected event notifation
883  */
884 static enum sci_status scic_sds_phy_starting_substate_await_sas_phy_speed_event_handler(
885         struct scic_sds_phy *this_phy,
886         u32 event_code)
887 {
888         u32 result = SCI_SUCCESS;
889
890         switch (scu_get_event_code(event_code)) {
891         case SCU_EVENT_SAS_PHY_DETECTED:
892                 /*
893                  * Why is this being reported again by the controller?
894                  * We would re-enter this state so just stay here */
895                 break;
896
897         case SCU_EVENT_SAS_15:
898         case SCU_EVENT_SAS_15_SSC:
899                 scic_sds_phy_complete_link_training(
900                         this_phy, SCI_SAS_150_GB, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
901                         );
902                 break;
903
904         case SCU_EVENT_SAS_30:
905         case SCU_EVENT_SAS_30_SSC:
906                 scic_sds_phy_complete_link_training(
907                         this_phy, SCI_SAS_300_GB, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
908                         );
909                 break;
910
911         case SCU_EVENT_SAS_60:
912         case SCU_EVENT_SAS_60_SSC:
913                 scic_sds_phy_complete_link_training(
914                         this_phy, SCI_SAS_600_GB, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
915                         );
916                 break;
917
918         case SCU_EVENT_SATA_SPINUP_HOLD:
919                 /*
920                  * We were doing SAS PHY link training and received a SATA PHY event
921                  * continue OOB/SN as if this were a SATA PHY */
922                 scic_sds_phy_start_sata_link_training(this_phy);
923                 break;
924
925         case SCU_EVENT_LINK_FAILURE:
926                 /* Link failure change state back to the starting state */
927                 scic_sds_phy_restart_starting_state(this_phy);
928                 break;
929
930         default:
931                 dev_warn(sciphy_to_dev(this_phy),
932                          "%s: PHY starting substate machine received "
933                          "unexpected event_code %x\n",
934                          __func__,
935                          event_code);
936
937                 result = SCI_FAILURE;
938                 break;
939         }
940
941         return result;
942 }
943
944 /**
945  *
946  * @phy: This struct scic_sds_phy object which has received an event.
947  * @event_code: This is the event code which the phy object is to decode.
948  *
949  * This method is called when an event notification is received for the phy
950  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF. -
951  * decode the event - sas phy detected event backs up the state machine to the
952  * await speed notification. - identify timeout is an un-expected event and the
953  * state machine is restarted. - link failure events restart the starting state
954  * machine - any other events log a warning message and set a failure status
955  * enum sci_status SCI_SUCCESS on any valid event notification SCI_FAILURE on any
956  * unexpected event notifation
957  */
958 static enum sci_status scic_sds_phy_starting_substate_await_iaf_uf_event_handler(
959         struct scic_sds_phy *this_phy,
960         u32 event_code)
961 {
962         u32 result = SCI_SUCCESS;
963
964         switch (scu_get_event_code(event_code)) {
965         case SCU_EVENT_SAS_PHY_DETECTED:
966                 /* Backup the state machine */
967                 scic_sds_phy_start_sas_link_training(this_phy);
968                 break;
969
970         case SCU_EVENT_SATA_SPINUP_HOLD:
971                 /*
972                  * We were doing SAS PHY link training and received a SATA PHY event
973                  * continue OOB/SN as if this were a SATA PHY */
974                 scic_sds_phy_start_sata_link_training(this_phy);
975                 break;
976
977         case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
978         case SCU_EVENT_LINK_FAILURE:
979         case SCU_EVENT_HARD_RESET_RECEIVED:
980                 /* Start the oob/sn state machine over again */
981                 scic_sds_phy_restart_starting_state(this_phy);
982                 break;
983
984         default:
985                 dev_warn(sciphy_to_dev(this_phy),
986                          "%s: PHY starting substate machine received "
987                          "unexpected event_code %x\n",
988                          __func__,
989                          event_code);
990
991                 result = SCI_FAILURE;
992                 break;
993         }
994
995         return result;
996 }
997
998 /**
999  *
1000  * @phy: This struct scic_sds_phy object which has received an event.
1001  * @event_code: This is the event code which the phy object is to decode.
1002  *
1003  * This method is called when an event notification is received for the phy
1004  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_POWER. -
1005  * decode the event - link failure events restart the starting state machine -
1006  * any other events log a warning message and set a failure status enum sci_status
1007  * SCI_SUCCESS on a link failure event SCI_FAILURE on any unexpected event
1008  * notifation
1009  */
1010 static enum sci_status scic_sds_phy_starting_substate_await_sas_power_event_handler(
1011         struct scic_sds_phy *this_phy,
1012         u32 event_code)
1013 {
1014         u32 result = SCI_SUCCESS;
1015
1016         switch (scu_get_event_code(event_code)) {
1017         case SCU_EVENT_LINK_FAILURE:
1018                 /* Link failure change state back to the starting state */
1019                 scic_sds_phy_restart_starting_state(this_phy);
1020                 break;
1021
1022         default:
1023                 dev_warn(sciphy_to_dev(this_phy),
1024                         "%s: PHY starting substate machine received unexpected "
1025                         "event_code %x\n",
1026                         __func__,
1027                         event_code);
1028
1029                 result = SCI_FAILURE;
1030                 break;
1031         }
1032
1033         return result;
1034 }
1035
1036 /**
1037  *
1038  * @phy: This struct scic_sds_phy object which has received an event.
1039  * @event_code: This is the event code which the phy object is to decode.
1040  *
1041  * This method is called when an event notification is received for the phy
1042  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER. -
1043  * decode the event - link failure events restart the starting state machine -
1044  * sata spinup hold events are ignored since they are expected - any other
1045  * events log a warning message and set a failure status enum sci_status SCI_SUCCESS
1046  * on a link failure event SCI_FAILURE on any unexpected event notifation
1047  */
1048 static enum sci_status scic_sds_phy_starting_substate_await_sata_power_event_handler(
1049         struct scic_sds_phy *this_phy,
1050         u32 event_code)
1051 {
1052         u32 result = SCI_SUCCESS;
1053
1054         switch (scu_get_event_code(event_code)) {
1055         case SCU_EVENT_LINK_FAILURE:
1056                 /* Link failure change state back to the starting state */
1057                 scic_sds_phy_restart_starting_state(this_phy);
1058                 break;
1059
1060         case SCU_EVENT_SATA_SPINUP_HOLD:
1061                 /* These events are received every 10ms and are expected while in this state */
1062                 break;
1063
1064         case SCU_EVENT_SAS_PHY_DETECTED:
1065                 /*
1066                  * There has been a change in the phy type before OOB/SN for the
1067                  * SATA finished start down the SAS link traning path. */
1068                 scic_sds_phy_start_sas_link_training(this_phy);
1069                 break;
1070
1071         default:
1072                 dev_warn(sciphy_to_dev(this_phy),
1073                          "%s: PHY starting substate machine received "
1074                          "unexpected event_code %x\n",
1075                          __func__,
1076                          event_code);
1077
1078                 result = SCI_FAILURE;
1079                 break;
1080         }
1081
1082         return result;
1083 }
1084
1085 /**
1086  *
1087  * @phy: This struct scic_sds_phy object which has received an event.
1088  * @event_code: This is the event code which the phy object is to decode.
1089  *
1090  * This method is called when an event notification is received for the phy
1091  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN. -
1092  * decode the event - link failure events restart the starting state machine -
1093  * sata spinup hold events are ignored since they are expected - sata phy
1094  * detected event change to the wait speed event - any other events log a
1095  * warning message and set a failure status enum sci_status SCI_SUCCESS on a link
1096  * failure event SCI_FAILURE on any unexpected event notifation
1097  */
1098 static enum sci_status scic_sds_phy_starting_substate_await_sata_phy_event_handler(
1099         struct scic_sds_phy *this_phy,
1100         u32 event_code)
1101 {
1102         u32 result = SCI_SUCCESS;
1103
1104         switch (scu_get_event_code(event_code)) {
1105         case SCU_EVENT_LINK_FAILURE:
1106                 /* Link failure change state back to the starting state */
1107                 scic_sds_phy_restart_starting_state(this_phy);
1108                 break;
1109
1110         case SCU_EVENT_SATA_SPINUP_HOLD:
1111                 /*
1112                  * These events might be received since we dont know how many may be in
1113                  * the completion queue while waiting for power */
1114                 break;
1115
1116         case SCU_EVENT_SATA_PHY_DETECTED:
1117                 this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
1118
1119                 /* We have received the SATA PHY notification change state */
1120                 sci_base_state_machine_change_state(
1121                         scic_sds_phy_get_starting_substate_machine(this_phy),
1122                         SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN
1123                         );
1124                 break;
1125
1126         case SCU_EVENT_SAS_PHY_DETECTED:
1127                 /*
1128                  * There has been a change in the phy type before OOB/SN for the
1129                  * SATA finished start down the SAS link traning path. */
1130                 scic_sds_phy_start_sas_link_training(this_phy);
1131                 break;
1132
1133         default:
1134                 dev_warn(sciphy_to_dev(this_phy),
1135                          "%s: PHY starting substate machine received "
1136                          "unexpected event_code %x\n",
1137                          __func__,
1138                          event_code);
1139
1140                 result = SCI_FAILURE;
1141                 break;
1142         }
1143
1144         return result;
1145 }
1146
1147 /**
1148  *
1149  * @phy: This struct scic_sds_phy object which has received an event.
1150  * @event_code: This is the event code which the phy object is to decode.
1151  *
1152  * This method is called when an event notification is received for the phy
1153  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN.
1154  * - decode the event - sata phy detected returns us back to this state. -
1155  * speed event detected causes a state transition to the wait for signature. -
1156  * link failure events restart the starting state machine - any other events
1157  * log a warning message and set a failure status enum sci_status SCI_SUCCESS on any
1158  * valid event notification SCI_FAILURE on any unexpected event notifation
1159  */
1160 static enum sci_status scic_sds_phy_starting_substate_await_sata_speed_event_handler(
1161         struct scic_sds_phy *this_phy,
1162         u32 event_code)
1163 {
1164         u32 result = SCI_SUCCESS;
1165
1166         switch (scu_get_event_code(event_code)) {
1167         case SCU_EVENT_SATA_PHY_DETECTED:
1168                 /*
1169                  * The hardware reports multiple SATA PHY detected events
1170                  * ignore the extras */
1171                 break;
1172
1173         case SCU_EVENT_SATA_15:
1174         case SCU_EVENT_SATA_15_SSC:
1175                 scic_sds_phy_complete_link_training(
1176                         this_phy,
1177                         SCI_SAS_150_GB,
1178                         SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
1179                         );
1180                 break;
1181
1182         case SCU_EVENT_SATA_30:
1183         case SCU_EVENT_SATA_30_SSC:
1184                 scic_sds_phy_complete_link_training(
1185                         this_phy,
1186                         SCI_SAS_300_GB,
1187                         SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
1188                         );
1189                 break;
1190
1191         case SCU_EVENT_SATA_60:
1192         case SCU_EVENT_SATA_60_SSC:
1193                 scic_sds_phy_complete_link_training(
1194                         this_phy,
1195                         SCI_SAS_600_GB,
1196                         SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
1197                         );
1198                 break;
1199
1200         case SCU_EVENT_LINK_FAILURE:
1201                 /* Link failure change state back to the starting state */
1202                 scic_sds_phy_restart_starting_state(this_phy);
1203                 break;
1204
1205         case SCU_EVENT_SAS_PHY_DETECTED:
1206                 /*
1207                  * There has been a change in the phy type before OOB/SN for the
1208                  * SATA finished start down the SAS link traning path. */
1209                 scic_sds_phy_start_sas_link_training(this_phy);
1210                 break;
1211
1212         default:
1213                 dev_warn(sciphy_to_dev(this_phy),
1214                          "%s: PHY starting substate machine received "
1215                          "unexpected event_code %x\n",
1216                          __func__,
1217                          event_code);
1218
1219                 result = SCI_FAILURE;
1220                 break;
1221         }
1222
1223         return result;
1224 }
1225
1226 /**
1227  *
1228  * @phy: This struct scic_sds_phy object which has received an event.
1229  * @event_code: This is the event code which the phy object is to decode.
1230  *
1231  * This method is called when an event notification is received for the phy
1232  * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. -
1233  * decode the event - sas phy detected event backs up the state machine to the
1234  * await speed notification. - identify timeout is an un-expected event and the
1235  * state machine is restarted. - link failure events restart the starting state
1236  * machine - any other events log a warning message and set a failure status
1237  * enum sci_status SCI_SUCCESS on any valid event notification SCI_FAILURE on any
1238  * unexpected event notifation
1239  */
1240 static enum sci_status scic_sds_phy_starting_substate_await_sig_fis_event_handler(
1241         struct scic_sds_phy *this_phy,
1242         u32 event_code)
1243 {
1244         u32 result = SCI_SUCCESS;
1245
1246         switch (scu_get_event_code(event_code)) {
1247         case SCU_EVENT_SATA_PHY_DETECTED:
1248                 /* Backup the state machine */
1249                 sci_base_state_machine_change_state(
1250                         scic_sds_phy_get_starting_substate_machine(this_phy),
1251                         SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN
1252                         );
1253                 break;
1254
1255         case SCU_EVENT_LINK_FAILURE:
1256                 /* Link failure change state back to the starting state */
1257                 scic_sds_phy_restart_starting_state(this_phy);
1258                 break;
1259
1260         default:
1261                 dev_warn(sciphy_to_dev(this_phy),
1262                          "%s: PHY starting substate machine received "
1263                          "unexpected event_code %x\n",
1264                          __func__,
1265                          event_code);
1266
1267                 result = SCI_FAILURE;
1268                 break;
1269         }
1270
1271         return result;
1272 }
1273
1274
1275 /*
1276  * *****************************************************************************
1277  * *  SCIC SDS PHY FRAME_HANDLERS
1278  * ***************************************************************************** */
1279
1280 /**
1281  *
1282  * @phy: This is struct scic_sds_phy object which is being requested to decode the
1283  *    frame data.
1284  * @frame_index: This is the index of the unsolicited frame which was received
1285  *    for this phy.
1286  *
1287  * This method decodes the unsolicited frame when the struct scic_sds_phy is in the
1288  * SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF. - Get the UF Header - If the UF
1289  * is an IAF - Copy IAF data to local phy object IAF data buffer. - Change
1290  * starting substate to wait power. - else - log warning message of unexpected
1291  * unsolicted frame - release frame buffer enum sci_status SCI_SUCCESS
1292  */
1293 static enum sci_status scic_sds_phy_starting_substate_await_iaf_uf_frame_handler(
1294         struct scic_sds_phy *this_phy,
1295         u32 frame_index)
1296 {
1297         enum sci_status result;
1298         u32 *frame_words;
1299         struct sci_sas_identify_address_frame *identify_frame;
1300
1301         result = scic_sds_unsolicited_frame_control_get_header(
1302                 &(scic_sds_phy_get_controller(this_phy)->uf_control),
1303                 frame_index,
1304                 (void **)&frame_words);
1305
1306         if (result != SCI_SUCCESS) {
1307                 return result;
1308         }
1309
1310         frame_words[0] = SCIC_SWAP_DWORD(frame_words[0]);
1311         identify_frame = (struct sci_sas_identify_address_frame *)frame_words;
1312
1313         if (identify_frame->address_frame_type == 0) {
1314                 /*
1315                  * Byte swap the rest of the frame so we can make
1316                  * a copy of the buffer */
1317                 frame_words[1] = SCIC_SWAP_DWORD(frame_words[1]);
1318                 frame_words[2] = SCIC_SWAP_DWORD(frame_words[2]);
1319                 frame_words[3] = SCIC_SWAP_DWORD(frame_words[3]);
1320                 frame_words[4] = SCIC_SWAP_DWORD(frame_words[4]);
1321                 frame_words[5] = SCIC_SWAP_DWORD(frame_words[5]);
1322
1323                 memcpy(
1324                         &this_phy->phy_type.sas.identify_address_frame_buffer,
1325                         identify_frame,
1326                         sizeof(struct sci_sas_identify_address_frame)
1327                         );
1328
1329                 if (identify_frame->protocols.u.bits.smp_target) {
1330                         /*
1331                          * We got the IAF for an expander PHY go to the final state since
1332                          * there are no power requirements for expander phys. */
1333                         sci_base_state_machine_change_state(
1334                                 scic_sds_phy_get_starting_substate_machine(this_phy),
1335                                 SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
1336                                 );
1337                 } else {
1338                         /* We got the IAF we can now go to the await spinup semaphore state */
1339                         sci_base_state_machine_change_state(
1340                                 scic_sds_phy_get_starting_substate_machine(this_phy),
1341                                 SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER
1342                                 );
1343                 }
1344
1345                 result = SCI_SUCCESS;
1346         } else
1347                 dev_warn(sciphy_to_dev(this_phy),
1348                         "%s: PHY starting substate machine received "
1349                         "unexpected frame id %x\n",
1350                         __func__,
1351                         frame_index);
1352
1353         /* Regardless of the result release this frame since we are done with it */
1354         scic_sds_controller_release_frame(
1355                 scic_sds_phy_get_controller(this_phy), frame_index
1356                 );
1357
1358         return result;
1359 }
1360
1361 /**
1362  *
1363  * @phy: This is struct scic_sds_phy object which is being requested to decode the
1364  *    frame data.
1365  * @frame_index: This is the index of the unsolicited frame which was received
1366  *    for this phy.
1367  *
1368  * This method decodes the unsolicited frame when the struct scic_sds_phy is in the
1369  * SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. - Get the UF Header - If
1370  * the UF is an SIGNATURE FIS - Copy IAF data to local phy object SIGNATURE FIS
1371  * data buffer. - else - log warning message of unexpected unsolicted frame -
1372  * release frame buffer enum sci_status SCI_SUCCESS Must decode the SIGNATURE FIS
1373  * data
1374  */
1375 static enum sci_status scic_sds_phy_starting_substate_await_sig_fis_frame_handler(
1376         struct scic_sds_phy *this_phy,
1377         u32 frame_index)
1378 {
1379         enum sci_status result;
1380         u32 *frame_words;
1381         struct sata_fis_header *fis_frame_header;
1382         u32 *fis_frame_data;
1383
1384         result = scic_sds_unsolicited_frame_control_get_header(
1385                 &(scic_sds_phy_get_controller(this_phy)->uf_control),
1386                 frame_index,
1387                 (void **)&frame_words);
1388
1389         if (result != SCI_SUCCESS) {
1390                 return result;
1391         }
1392
1393         fis_frame_header = (struct sata_fis_header *)frame_words;
1394
1395         if (
1396                 (fis_frame_header->fis_type == SATA_FIS_TYPE_REGD2H)
1397                 && !(fis_frame_header->status & ATA_STATUS_REG_BSY_BIT)
1398                 ) {
1399                 scic_sds_unsolicited_frame_control_get_buffer(
1400                         &(scic_sds_phy_get_controller(this_phy)->uf_control),
1401                         frame_index,
1402                         (void **)&fis_frame_data
1403                         );
1404
1405                 scic_sds_controller_copy_sata_response(
1406                         &this_phy->phy_type.sata.signature_fis_buffer,
1407                         frame_words,
1408                         fis_frame_data
1409                         );
1410
1411                 /* We got the IAF we can now go to the await spinup semaphore state */
1412                 sci_base_state_machine_change_state(
1413                         scic_sds_phy_get_starting_substate_machine(this_phy),
1414                         SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
1415                         );
1416
1417                 result = SCI_SUCCESS;
1418         } else
1419                 dev_warn(sciphy_to_dev(this_phy),
1420                          "%s: PHY starting substate machine received "
1421                          "unexpected frame id %x\n",
1422                          __func__,
1423                          frame_index);
1424
1425         /* Regardless of the result release this frame since we are done with it */
1426         scic_sds_controller_release_frame(
1427                 scic_sds_phy_get_controller(this_phy), frame_index
1428                 );
1429
1430         return result;
1431 }
1432
1433 /*
1434  * *****************************************************************************
1435  * * SCIC SDS PHY POWER_HANDLERS
1436  * ***************************************************************************** */
1437
1438 /**
1439  *
1440  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
1441  *    object.
1442  *
1443  * This method is called by the struct scic_sds_controller when the phy object is
1444  * granted power. - The notify enable spinups are turned on for this phy object
1445  * - The phy state machine is transitioned to the
1446  * SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL. enum sci_status SCI_SUCCESS
1447  */
1448 static enum sci_status scic_sds_phy_starting_substate_await_sas_power_consume_power_handler(
1449         struct scic_sds_phy *this_phy)
1450 {
1451         u32 enable_spinup;
1452
1453         enable_spinup = SCU_SAS_ENSPINUP_READ(this_phy);
1454         enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
1455         SCU_SAS_ENSPINUP_WRITE(this_phy, enable_spinup);
1456
1457         /* Change state to the final state this substate machine has run to completion */
1458         sci_base_state_machine_change_state(
1459                 scic_sds_phy_get_starting_substate_machine(this_phy),
1460                 SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
1461                 );
1462
1463         return SCI_SUCCESS;
1464 }
1465
1466 /**
1467  *
1468  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
1469  *    object.
1470  *
1471  * This method is called by the struct scic_sds_controller when the phy object is
1472  * granted power. - The phy state machine is transitioned to the
1473  * SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN. enum sci_status SCI_SUCCESS
1474  */
1475 static enum sci_status scic_sds_phy_starting_substate_await_sata_power_consume_power_handler(
1476         struct scic_sds_phy *this_phy)
1477 {
1478         u32 scu_sas_pcfg_value;
1479
1480         /* Release the spinup hold state and reset the OOB state machine */
1481         scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);
1482         scu_sas_pcfg_value &=
1483                 ~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
1484         scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
1485         SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
1486
1487         /* Now restart the OOB operation */
1488         scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
1489         scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1490         SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
1491
1492         /* Change state to the final state this substate machine has run to completion */
1493         sci_base_state_machine_change_state(
1494                 scic_sds_phy_get_starting_substate_machine(this_phy),
1495                 SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN
1496                 );
1497
1498         return SCI_SUCCESS;
1499 }
1500
1501 /* --------------------------------------------------------------------------- */
1502
1503 struct scic_sds_phy_state_handler
1504 scic_sds_phy_starting_substate_handler_table[SCIC_SDS_PHY_STARTING_MAX_SUBSTATES] =
1505 {
1506         /* SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL */
1507         {
1508                 {
1509                         scic_sds_phy_default_start_handler,
1510                         scic_sds_phy_default_stop_handler,
1511                         scic_sds_phy_default_reset_handler,
1512                         scic_sds_phy_default_destroy_handler
1513                 },
1514                 scic_sds_phy_default_frame_handler,
1515                 scic_sds_phy_default_event_handler,
1516                 scic_sds_phy_default_consume_power_handler
1517         },
1518         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN */
1519         {
1520                 {
1521                         scic_sds_phy_default_start_handler,
1522                         scic_sds_phy_default_stop_handler,
1523                         scic_sds_phy_default_reset_handler,
1524                         scic_sds_phy_default_destroy_handler
1525                 },
1526                 scic_sds_phy_default_frame_handler,
1527                 scic_sds_phy_starting_substate_await_ossp_event_handler,
1528                 scic_sds_phy_default_consume_power_handler
1529         },
1530         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN */
1531         {
1532                 {
1533                         scic_sds_phy_default_start_handler,
1534                         scic_sds_phy_default_stop_handler,
1535                         scic_sds_phy_default_reset_handler,
1536                         scic_sds_phy_default_destroy_handler
1537                 },
1538                 scic_sds_phy_default_frame_handler,
1539                 scic_sds_phy_starting_substate_await_sas_phy_speed_event_handler,
1540                 scic_sds_phy_default_consume_power_handler
1541         },
1542         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF */
1543         {
1544                 {
1545                         scic_sds_phy_default_start_handler,
1546                         scic_sds_phy_default_stop_handler,
1547                         scic_sds_phy_default_reset_handler,
1548                         scic_sds_phy_default_destroy_handler
1549                 },
1550                 scic_sds_phy_starting_substate_await_iaf_uf_frame_handler,
1551                 scic_sds_phy_starting_substate_await_iaf_uf_event_handler,
1552                 scic_sds_phy_default_consume_power_handler
1553         },
1554         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER */
1555         {
1556                 {
1557                         scic_sds_phy_default_start_handler,
1558                         scic_sds_phy_default_stop_handler,
1559                         scic_sds_phy_default_reset_handler,
1560                         scic_sds_phy_default_destroy_handler
1561                 },
1562                 scic_sds_phy_default_frame_handler,
1563                 scic_sds_phy_starting_substate_await_sas_power_event_handler,
1564                 scic_sds_phy_starting_substate_await_sas_power_consume_power_handler
1565         },
1566         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER, */
1567         {
1568                 {
1569                         scic_sds_phy_default_start_handler,
1570                         scic_sds_phy_default_stop_handler,
1571                         scic_sds_phy_default_reset_handler,
1572                         scic_sds_phy_default_destroy_handler
1573                 },
1574                 scic_sds_phy_default_frame_handler,
1575                 scic_sds_phy_starting_substate_await_sata_power_event_handler,
1576                 scic_sds_phy_starting_substate_await_sata_power_consume_power_handler
1577         },
1578         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN, */
1579         {
1580                 {
1581                         scic_sds_phy_default_start_handler,
1582                         scic_sds_phy_default_stop_handler,
1583                         scic_sds_phy_default_reset_handler,
1584                         scic_sds_phy_default_destroy_handler
1585                 },
1586                 scic_sds_phy_default_frame_handler,
1587                 scic_sds_phy_starting_substate_await_sata_phy_event_handler,
1588                 scic_sds_phy_default_consume_power_handler
1589         },
1590         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN, */
1591         {
1592                 {
1593                         scic_sds_phy_default_start_handler,
1594                         scic_sds_phy_default_stop_handler,
1595                         scic_sds_phy_default_reset_handler,
1596                         scic_sds_phy_default_destroy_handler
1597                 },
1598                 scic_sds_phy_default_frame_handler,
1599                 scic_sds_phy_starting_substate_await_sata_speed_event_handler,
1600                 scic_sds_phy_default_consume_power_handler
1601         },
1602         /* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF, */
1603         {
1604                 {
1605                         scic_sds_phy_default_start_handler,
1606                         scic_sds_phy_default_stop_handler,
1607                         scic_sds_phy_default_reset_handler,
1608                         scic_sds_phy_default_destroy_handler
1609                 },
1610                 scic_sds_phy_starting_substate_await_sig_fis_frame_handler,
1611                 scic_sds_phy_starting_substate_await_sig_fis_event_handler,
1612                 scic_sds_phy_default_consume_power_handler
1613         },
1614         /* SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL */
1615         {
1616                 {
1617                         scic_sds_phy_default_start_handler,
1618                         scic_sds_phy_default_stop_handler,
1619                         scic_sds_phy_default_reset_handler,
1620                         scic_sds_phy_default_destroy_handler
1621                 },
1622                 scic_sds_phy_default_frame_handler,
1623                 scic_sds_phy_default_event_handler,
1624                 scic_sds_phy_default_consume_power_handler
1625         }
1626 };
1627
1628 /**
1629  * scic_sds_phy_set_starting_substate_handlers() -
1630  *
1631  * This macro sets the starting substate handlers by state_id
1632  */
1633 #define scic_sds_phy_set_starting_substate_handlers(phy, state_id) \
1634         scic_sds_phy_set_state_handlers(\
1635                 (phy), \
1636                 &scic_sds_phy_starting_substate_handler_table[(state_id)] \
1637                 )
1638
1639 /*
1640  * ****************************************************************************
1641  * *  PHY STARTING SUBSTATE METHODS
1642  * **************************************************************************** */
1643
1644 /**
1645  *
1646  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1647  *
1648  * This method will perform the actions required by the struct scic_sds_phy on
1649  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL. - The initial state
1650  * handlers are put in place for the struct scic_sds_phy object. - The state is
1651  * changed to the wait phy type event notification. none
1652  */
1653 static void scic_sds_phy_starting_initial_substate_enter(
1654         struct sci_base_object *object)
1655 {
1656         struct scic_sds_phy *this_phy;
1657
1658         this_phy = (struct scic_sds_phy *)object;
1659
1660         scic_sds_phy_set_starting_substate_handlers(
1661                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL);
1662
1663         /* This is just an temporary state go off to the starting state */
1664         sci_base_state_machine_change_state(
1665                 scic_sds_phy_get_starting_substate_machine(this_phy),
1666                 SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN
1667                 );
1668 }
1669
1670 /**
1671  *
1672  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1673  *
1674  * This method will perform the actions required by the struct scic_sds_phy on
1675  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_PHY_TYPE_EN. - Set the
1676  * struct scic_sds_phy object state handlers for this state. none
1677  */
1678 static void scic_sds_phy_starting_await_ossp_en_substate_enter(
1679         struct sci_base_object *object)
1680 {
1681         struct scic_sds_phy *this_phy;
1682
1683         this_phy = (struct scic_sds_phy *)object;
1684
1685         scic_sds_phy_set_starting_substate_handlers(
1686                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN
1687                 );
1688 }
1689
1690 /**
1691  *
1692  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1693  *
1694  * This method will perform the actions required by the struct scic_sds_phy on
1695  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SPEED_EN. - Set the
1696  * struct scic_sds_phy object state handlers for this state. none
1697  */
1698 static void scic_sds_phy_starting_await_sas_speed_en_substate_enter(
1699         struct sci_base_object *object)
1700 {
1701         struct scic_sds_phy *this_phy;
1702
1703         this_phy = (struct scic_sds_phy *)object;
1704
1705         scic_sds_phy_set_starting_substate_handlers(
1706                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN
1707                 );
1708 }
1709
1710 /**
1711  *
1712  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1713  *
1714  * This method will perform the actions required by the struct scic_sds_phy on
1715  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF. - Set the
1716  * struct scic_sds_phy object state handlers for this state. none
1717  */
1718 static void scic_sds_phy_starting_await_iaf_uf_substate_enter(
1719         struct sci_base_object *object)
1720 {
1721         struct scic_sds_phy *this_phy;
1722
1723         this_phy = (struct scic_sds_phy *)object;
1724
1725         scic_sds_phy_set_starting_substate_handlers(
1726                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
1727                 );
1728 }
1729
1730 /**
1731  *
1732  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1733  *
1734  * This method will perform the actions required by the struct scic_sds_phy on
1735  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER. - Set the
1736  * struct scic_sds_phy object state handlers for this state. - Add this phy object to
1737  * the power control queue none
1738  */
1739 static void scic_sds_phy_starting_await_sas_power_substate_enter(
1740         struct sci_base_object *object)
1741 {
1742         struct scic_sds_phy *this_phy;
1743
1744         this_phy = (struct scic_sds_phy *)object;
1745
1746         scic_sds_phy_set_starting_substate_handlers(
1747                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER
1748                 );
1749
1750         scic_sds_controller_power_control_queue_insert(
1751                 scic_sds_phy_get_controller(this_phy),
1752                 this_phy
1753                 );
1754 }
1755
1756 /**
1757  *
1758  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1759  *
1760  * This method will perform the actions required by the struct scic_sds_phy on exiting
1761  * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER. - Remove the
1762  * struct scic_sds_phy object from the power control queue. none
1763  */
1764 static void scic_sds_phy_starting_await_sas_power_substate_exit(
1765         struct sci_base_object *object)
1766 {
1767         struct scic_sds_phy *this_phy;
1768
1769         this_phy = (struct scic_sds_phy *)object;
1770
1771         scic_sds_controller_power_control_queue_remove(
1772                 scic_sds_phy_get_controller(this_phy), this_phy
1773                 );
1774 }
1775
1776 /**
1777  *
1778  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1779  *
1780  * This method will perform the actions required by the struct scic_sds_phy on
1781  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER. - Set the
1782  * struct scic_sds_phy object state handlers for this state. - Add this phy object to
1783  * the power control queue none
1784  */
1785 static void scic_sds_phy_starting_await_sata_power_substate_enter(
1786         struct sci_base_object *object)
1787 {
1788         struct scic_sds_phy *this_phy;
1789
1790         this_phy = (struct scic_sds_phy *)object;
1791
1792         scic_sds_phy_set_starting_substate_handlers(
1793                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER
1794                 );
1795
1796         scic_sds_controller_power_control_queue_insert(
1797                 scic_sds_phy_get_controller(this_phy),
1798                 this_phy
1799                 );
1800 }
1801
1802 /**
1803  *
1804  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1805  *
1806  * This method will perform the actions required by the struct scic_sds_phy on exiting
1807  * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER. - Remove the
1808  * struct scic_sds_phy object from the power control queue. none
1809  */
1810 static void scic_sds_phy_starting_await_sata_power_substate_exit(
1811         struct sci_base_object *object)
1812 {
1813         struct scic_sds_phy *this_phy;
1814
1815         this_phy = (struct scic_sds_phy *)object;
1816
1817         scic_sds_controller_power_control_queue_remove(
1818                 scic_sds_phy_get_controller(this_phy),
1819                 this_phy
1820                 );
1821 }
1822
1823 /**
1824  *
1825  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1826  *
1827  * This method will perform the actions required by the struct scic_sds_phy on
1828  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN. - Set the
1829  * struct scic_sds_phy object state handlers for this state. none
1830  */
1831 static void scic_sds_phy_starting_await_sata_phy_substate_enter(
1832         struct sci_base_object *object)
1833 {
1834         struct scic_sds_phy *this_phy;
1835
1836         this_phy = (struct scic_sds_phy *)object;
1837
1838         scic_sds_phy_set_starting_substate_handlers(
1839                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN
1840                 );
1841
1842         scic_cb_timer_start(
1843                 scic_sds_phy_get_controller(this_phy),
1844                 this_phy->sata_timeout_timer,
1845                 SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT
1846                 );
1847 }
1848
1849 /**
1850  *
1851  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1852  *
1853  * This method will perform the actions required by the struct scic_sds_phy on exiting
1854  * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN. - stop the timer
1855  * that was started on entry to await sata phy event notification none
1856  */
1857 static void scic_sds_phy_starting_await_sata_phy_substate_exit(
1858         struct sci_base_object *object)
1859 {
1860         struct scic_sds_phy *this_phy;
1861
1862         this_phy = (struct scic_sds_phy *)object;
1863
1864         scic_cb_timer_stop(
1865                 scic_sds_phy_get_controller(this_phy),
1866                 this_phy->sata_timeout_timer
1867                 );
1868 }
1869
1870 /**
1871  *
1872  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1873  *
1874  * This method will perform the actions required by the struct scic_sds_phy on
1875  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN. - Set the
1876  * struct scic_sds_phy object state handlers for this state. none
1877  */
1878 static void scic_sds_phy_starting_await_sata_speed_substate_enter(
1879         struct sci_base_object *object)
1880 {
1881         struct scic_sds_phy *this_phy;
1882
1883         this_phy = (struct scic_sds_phy *)object;
1884
1885         scic_sds_phy_set_starting_substate_handlers(
1886                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN
1887                 );
1888
1889         scic_cb_timer_start(
1890                 scic_sds_phy_get_controller(this_phy),
1891                 this_phy->sata_timeout_timer,
1892                 SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT
1893                 );
1894 }
1895
1896 /**
1897  *
1898  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1899  *
1900  * This method will perform the actions required by the struct scic_sds_phy on exiting
1901  * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN. - stop the timer
1902  * that was started on entry to await sata phy event notification none
1903  */
1904 static void scic_sds_phy_starting_await_sata_speed_substate_exit(
1905         struct sci_base_object *object)
1906 {
1907         struct scic_sds_phy *this_phy;
1908
1909         this_phy = (struct scic_sds_phy *)object;
1910
1911         scic_cb_timer_stop(
1912                 scic_sds_phy_get_controller(this_phy),
1913                 this_phy->sata_timeout_timer
1914                 );
1915 }
1916
1917 /**
1918  *
1919  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1920  *
1921  * This method will perform the actions required by the struct scic_sds_phy on
1922  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. - Set the
1923  * struct scic_sds_phy object state handlers for this state. - Start the SIGNATURE FIS
1924  * timeout timer none
1925  */
1926 static void scic_sds_phy_starting_await_sig_fis_uf_substate_enter(
1927         struct sci_base_object *object)
1928 {
1929         bool continue_to_ready_state;
1930         struct scic_sds_phy *this_phy;
1931
1932         this_phy = (struct scic_sds_phy *)object;
1933
1934         scic_sds_phy_set_starting_substate_handlers(
1935                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
1936                 );
1937
1938         continue_to_ready_state = scic_sds_port_link_detected(
1939                 this_phy->owning_port,
1940                 this_phy
1941                 );
1942
1943         if (continue_to_ready_state) {
1944                 /*
1945                  * Clear the PE suspend condition so we can actually receive SIG FIS
1946                  * The hardware will not respond to the XRDY until the PE suspend
1947                  * condition is cleared. */
1948                 scic_sds_phy_resume(this_phy);
1949
1950                 scic_cb_timer_start(
1951                         scic_sds_phy_get_controller(this_phy),
1952                         this_phy->sata_timeout_timer,
1953                         SCIC_SDS_SIGNATURE_FIS_TIMEOUT
1954                         );
1955         } else {
1956                 this_phy->is_in_link_training = false;
1957         }
1958 }
1959
1960 /**
1961  *
1962  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1963  *
1964  * This method will perform the actions required by the struct scic_sds_phy on exiting
1965  * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. - Stop the SIGNATURE
1966  * FIS timeout timer. none
1967  */
1968 static void scic_sds_phy_starting_await_sig_fis_uf_substate_exit(
1969         struct sci_base_object *object)
1970 {
1971         struct scic_sds_phy *this_phy;
1972
1973         this_phy = (struct scic_sds_phy *)object;
1974
1975         scic_cb_timer_stop(
1976                 scic_sds_phy_get_controller(this_phy),
1977                 this_phy->sata_timeout_timer
1978                 );
1979 }
1980
1981 /**
1982  *
1983  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
1984  *
1985  * This method will perform the actions required by the struct scic_sds_phy on
1986  * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL. - Set the struct scic_sds_phy
1987  * object state handlers for this state. - Change base state machine to the
1988  * ready state. none
1989  */
1990 static void scic_sds_phy_starting_final_substate_enter(
1991         struct sci_base_object *object)
1992 {
1993         struct scic_sds_phy *this_phy;
1994
1995         this_phy = (struct scic_sds_phy *)object;
1996
1997         scic_sds_phy_set_starting_substate_handlers(
1998                 this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
1999                 );
2000
2001         /*
2002          * State machine has run to completion so exit out and change
2003          * the base state machine to the ready state */
2004         sci_base_state_machine_change_state(
2005                 scic_sds_phy_get_base_state_machine(this_phy),
2006                 SCI_BASE_PHY_STATE_READY);
2007 }
2008
2009 /* --------------------------------------------------------------------------- */
2010
2011 const struct sci_base_state scic_sds_phy_starting_substates[] = {
2012         [SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL] = {
2013                 .enter_state = scic_sds_phy_starting_initial_substate_enter,
2014         },
2015         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN] = {
2016                 .enter_state = scic_sds_phy_starting_await_ossp_en_substate_enter,
2017         },
2018         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN] = {
2019                 .enter_state = scic_sds_phy_starting_await_sas_speed_en_substate_enter,
2020         },
2021         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF] = {
2022                 .enter_state = scic_sds_phy_starting_await_iaf_uf_substate_enter,
2023         },
2024         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER] = {
2025                 .enter_state = scic_sds_phy_starting_await_sas_power_substate_enter,
2026                 .exit_state  = scic_sds_phy_starting_await_sas_power_substate_exit,
2027         },
2028         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER] = {
2029                 .enter_state = scic_sds_phy_starting_await_sata_power_substate_enter,
2030                 .exit_state  = scic_sds_phy_starting_await_sata_power_substate_exit
2031         },
2032         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN] = {
2033                 .enter_state = scic_sds_phy_starting_await_sata_phy_substate_enter,
2034                 .exit_state  = scic_sds_phy_starting_await_sata_phy_substate_exit
2035         },
2036         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN] = {
2037                 .enter_state = scic_sds_phy_starting_await_sata_speed_substate_enter,
2038                 .exit_state  = scic_sds_phy_starting_await_sata_speed_substate_exit
2039         },
2040         [SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF] = {
2041                 .enter_state = scic_sds_phy_starting_await_sig_fis_uf_substate_enter,
2042                 .exit_state  = scic_sds_phy_starting_await_sig_fis_uf_substate_exit
2043         },
2044         [SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL] = {
2045                 .enter_state = scic_sds_phy_starting_final_substate_enter,
2046         }
2047 };
2048
2049 /*
2050  * ***************************************************************************
2051  * *  DEFAULT HANDLERS
2052  * *************************************************************************** */
2053
2054 /**
2055  *
2056  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2057  *    object.
2058  *
2059  * This is the default method for phy a start request.  It will report a
2060  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2061  */
2062 enum sci_status scic_sds_phy_default_start_handler(
2063         struct sci_base_phy *phy)
2064 {
2065         struct scic_sds_phy *this_phy;
2066
2067         this_phy = (struct scic_sds_phy *)phy;
2068
2069         dev_warn(sciphy_to_dev(this_phy),
2070                  "%s: SCIC Phy 0x%p requested to start from invalid "
2071                  "state %d\n",
2072                  __func__,
2073                  this_phy,
2074                  sci_base_state_machine_get_state(
2075                          &this_phy->parent.state_machine));
2076
2077         return SCI_FAILURE_INVALID_STATE;
2078
2079 }
2080
2081 /**
2082  *
2083  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2084  *    object.
2085  *
2086  * This is the default method for phy a stop request.  It will report a warning
2087  * and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2088  */
2089 enum sci_status scic_sds_phy_default_stop_handler(
2090         struct sci_base_phy *phy)
2091 {
2092         struct scic_sds_phy *this_phy;
2093
2094         this_phy = (struct scic_sds_phy *)phy;
2095
2096         dev_warn(sciphy_to_dev(this_phy),
2097                  "%s: SCIC Phy 0x%p requested to stop from invalid "
2098                  "state %d\n",
2099                  __func__,
2100                  this_phy,
2101                  sci_base_state_machine_get_state(
2102                          &this_phy->parent.state_machine));
2103
2104         return SCI_FAILURE_INVALID_STATE;
2105 }
2106
2107 /**
2108  *
2109  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2110  *    object.
2111  *
2112  * This is the default method for phy a reset request.  It will report a
2113  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2114  */
2115 enum sci_status scic_sds_phy_default_reset_handler(
2116         struct sci_base_phy *phy)
2117 {
2118         struct scic_sds_phy *this_phy;
2119
2120         this_phy = (struct scic_sds_phy *)phy;
2121
2122         dev_warn(sciphy_to_dev(this_phy),
2123                  "%s: SCIC Phy 0x%p requested to reset from invalid state "
2124                  "%d\n",
2125                  __func__,
2126                  this_phy,
2127                  sci_base_state_machine_get_state(
2128                          &this_phy->parent.state_machine));
2129
2130         return SCI_FAILURE_INVALID_STATE;
2131 }
2132
2133 /**
2134  *
2135  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2136  *    object.
2137  *
2138  * This is the default method for phy a destruct request.  It will report a
2139  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2140  */
2141 enum sci_status scic_sds_phy_default_destroy_handler(
2142         struct sci_base_phy *phy)
2143 {
2144         struct scic_sds_phy *this_phy;
2145
2146         this_phy = (struct scic_sds_phy *)phy;
2147
2148         /* / @todo Implement something for the default */
2149         dev_warn(sciphy_to_dev(this_phy),
2150                  "%s: SCIC Phy 0x%p requested to destroy from invalid "
2151                  "state %d\n",
2152                  __func__,
2153                  this_phy,
2154                  sci_base_state_machine_get_state(
2155                          &this_phy->parent.state_machine));
2156
2157         return SCI_FAILURE_INVALID_STATE;
2158 }
2159
2160 /**
2161  *
2162  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2163  *    object.
2164  * @frame_index: This is the frame index that was received from the SCU
2165  *    hardware.
2166  *
2167  * This is the default method for a phy frame handling request.  It will report
2168  * a warning, release the frame and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2169  */
2170 enum sci_status scic_sds_phy_default_frame_handler(
2171         struct scic_sds_phy *this_phy,
2172         u32 frame_index)
2173 {
2174         dev_warn(sciphy_to_dev(this_phy),
2175                  "%s: SCIC Phy 0x%p received unexpected frame data %d "
2176                  "while in state %d\n",
2177                  __func__,
2178                  this_phy,
2179                  frame_index,
2180                  sci_base_state_machine_get_state(
2181                          &this_phy->parent.state_machine));
2182
2183         scic_sds_controller_release_frame(
2184                 scic_sds_phy_get_controller(this_phy), frame_index);
2185
2186         return SCI_FAILURE_INVALID_STATE;
2187 }
2188
2189 /**
2190  *
2191  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2192  *    object.
2193  * @event_code: This is the event code that was received from the SCU hardware.
2194  *
2195  * This is the default method for a phy event handler.  It will report a
2196  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2197  */
2198 enum sci_status scic_sds_phy_default_event_handler(
2199         struct scic_sds_phy *this_phy,
2200         u32 event_code)
2201 {
2202         dev_warn(sciphy_to_dev(this_phy),
2203                 "%s: SCIC Phy 0x%p received unexpected event status %x "
2204                 "while in state %d\n",
2205                 __func__,
2206                 this_phy,
2207                 event_code,
2208                 sci_base_state_machine_get_state(
2209                         &this_phy->parent.state_machine));
2210
2211         return SCI_FAILURE_INVALID_STATE;
2212 }
2213
2214 /**
2215  *
2216  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2217  *    object.
2218  *
2219  * This is the default method for a phy consume power handler.  It will report
2220  * a warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
2221  */
2222 enum sci_status scic_sds_phy_default_consume_power_handler(
2223         struct scic_sds_phy *this_phy)
2224 {
2225         dev_warn(sciphy_to_dev(this_phy),
2226                  "%s: SCIC Phy 0x%p given unexpected permission to consume "
2227                  "power while in state %d\n",
2228                  __func__,
2229                  this_phy,
2230                  sci_base_state_machine_get_state(
2231                          &this_phy->parent.state_machine));
2232
2233         return SCI_FAILURE_INVALID_STATE;
2234 }
2235
2236 /*
2237  * ******************************************************************************
2238  * * PHY STOPPED STATE HANDLERS
2239  * ****************************************************************************** */
2240
2241 /**
2242  *
2243  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2244  *    object.
2245  *
2246  * This method takes the struct scic_sds_phy from a stopped state and attempts to
2247  * start it. - The phy state machine is transitioned to the
2248  * SCI_BASE_PHY_STATE_STARTING. enum sci_status SCI_SUCCESS
2249  */
2250 static enum sci_status scic_sds_phy_stopped_state_start_handler(
2251         struct sci_base_phy *phy)
2252 {
2253         struct scic_sds_phy *this_phy;
2254
2255         this_phy = (struct scic_sds_phy *)phy;
2256
2257         sci_base_state_machine_change_state(
2258                 scic_sds_phy_get_base_state_machine(this_phy),
2259                 SCI_BASE_PHY_STATE_STARTING
2260                 );
2261
2262         return SCI_SUCCESS;
2263 }
2264
2265 /**
2266  *
2267  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2268  *    object.
2269  *
2270  * This method takes the struct scic_sds_phy from a stopped state and destroys it. -
2271  * This function takes no action. Shouldnt this function transition the
2272  * struct sci_base_phy::state_machine to the SCI_BASE_PHY_STATE_FINAL? enum sci_status
2273  * SCI_SUCCESS
2274  */
2275 static enum sci_status scic_sds_phy_stopped_state_destroy_handler(
2276         struct sci_base_phy *phy)
2277 {
2278         struct scic_sds_phy *this_phy;
2279
2280         this_phy = (struct scic_sds_phy *)phy;
2281
2282         /* / @todo what do we actually need to do here? */
2283         return SCI_SUCCESS;
2284 }
2285
2286 /*
2287  * ******************************************************************************
2288  * * PHY STARTING STATE HANDLERS
2289  * ****************************************************************************** */
2290
2291 /* All of these state handlers are mapped to the starting sub-state machine */
2292
2293 /*
2294  * ******************************************************************************
2295  * * PHY READY STATE HANDLERS
2296  * ****************************************************************************** */
2297
2298 /**
2299  *
2300  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2301  *    object.
2302  *
2303  * This method takes the struct scic_sds_phy from a ready state and attempts to stop
2304  * it. - The phy state machine is transitioned to the
2305  * SCI_BASE_PHY_STATE_STOPPED. enum sci_status SCI_SUCCESS
2306  */
2307 static enum sci_status scic_sds_phy_ready_state_stop_handler(
2308         struct sci_base_phy *phy)
2309 {
2310         struct scic_sds_phy *this_phy;
2311
2312         this_phy = (struct scic_sds_phy *)phy;
2313
2314         sci_base_state_machine_change_state(
2315                 scic_sds_phy_get_base_state_machine(this_phy),
2316                 SCI_BASE_PHY_STATE_STOPPED
2317                 );
2318
2319         return SCI_SUCCESS;
2320 }
2321
2322 /**
2323  *
2324  * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
2325  *    object.
2326  *
2327  * This method takes the struct scic_sds_phy from a ready state and attempts to reset
2328  * it. - The phy state machine is transitioned to the
2329  * SCI_BASE_PHY_STATE_STARTING. enum sci_status SCI_SUCCESS
2330  */
2331 static enum sci_status scic_sds_phy_ready_state_reset_handler(
2332         struct sci_base_phy *phy)
2333 {
2334         struct scic_sds_phy *this_phy;
2335
2336         this_phy = (struct scic_sds_phy *)phy;
2337
2338         sci_base_state_machine_change_state(
2339                 scic_sds_phy_get_base_state_machine(this_phy),
2340                 SCI_BASE_PHY_STATE_RESETTING
2341                 );
2342
2343         return SCI_SUCCESS;
2344 }
2345
2346 /**
2347  *
2348  * @phy: This is the struct scic_sds_phy object which has received the event.
2349  *
2350  * This method request the struct scic_sds_phy handle the received event.  The only
2351  * event that we are interested in while in the ready state is the link failure
2352  * event. - decoded event is a link failure - transition the struct scic_sds_phy back
2353  * to the SCI_BASE_PHY_STATE_STARTING state. - any other event recived will
2354  * report a warning message enum sci_status SCI_SUCCESS if the event received is a
2355  * link failure SCI_FAILURE_INVALID_STATE for any other event received.
2356  */
2357 static enum sci_status scic_sds_phy_ready_state_event_handler(
2358         struct scic_sds_phy *this_phy,
2359         u32 event_code)
2360 {
2361         enum sci_status result = SCI_FAILURE;
2362
2363         switch (scu_get_event_code(event_code)) {
2364         case SCU_EVENT_LINK_FAILURE:
2365                 /* Link failure change state back to the starting state */
2366                 sci_base_state_machine_change_state(
2367                         scic_sds_phy_get_base_state_machine(this_phy),
2368                         SCI_BASE_PHY_STATE_STARTING
2369                         );
2370
2371                 result = SCI_SUCCESS;
2372                 break;
2373
2374         case SCU_EVENT_BROADCAST_CHANGE:
2375                 /* Broadcast change received. Notify the port. */
2376                 if (scic_sds_phy_get_port(this_phy) != SCI_INVALID_HANDLE)
2377                         scic_sds_port_broadcast_change_received(this_phy->owning_port, this_phy);
2378                 else
2379                         this_phy->bcn_received_while_port_unassigned = true;
2380                 break;
2381
2382         default:
2383                 dev_warn(sciphy_to_dev(this_phy),
2384                          "%sP SCIC PHY 0x%p ready state machine received "
2385                          "unexpected event_code %x\n",
2386                          __func__,
2387                          this_phy,
2388                          event_code);
2389
2390                 result = SCI_FAILURE_INVALID_STATE;
2391                 break;
2392         }
2393
2394         return result;
2395 }
2396
2397 /* --------------------------------------------------------------------------- */
2398
2399 /**
2400  *
2401  * @this_phy: This is the struct scic_sds_phy object which is receiving the event.
2402  * @event_code: This is the event code to be processed.
2403  *
2404  * This is the resetting state event handler. enum sci_status
2405  * SCI_FAILURE_INVALID_STATE
2406  */
2407 static enum sci_status scic_sds_phy_resetting_state_event_handler(
2408         struct scic_sds_phy *this_phy,
2409         u32 event_code)
2410 {
2411         enum sci_status result = SCI_FAILURE;
2412
2413         switch (scu_get_event_code(event_code)) {
2414         case SCU_EVENT_HARD_RESET_TRANSMITTED:
2415                 /* Link failure change state back to the starting state */
2416                 sci_base_state_machine_change_state(
2417                         scic_sds_phy_get_base_state_machine(this_phy),
2418                         SCI_BASE_PHY_STATE_STARTING
2419                         );
2420
2421                 result = SCI_SUCCESS;
2422                 break;
2423
2424         default:
2425                 dev_warn(sciphy_to_dev(this_phy),
2426                          "%s: SCIC PHY 0x%p resetting state machine received "
2427                          "unexpected event_code %x\n",
2428                          __func__,
2429                          this_phy,
2430                          event_code);
2431
2432                 result = SCI_FAILURE_INVALID_STATE;
2433                 break;
2434         }
2435
2436         return result;
2437 }
2438
2439 /* --------------------------------------------------------------------------- */
2440
2441 struct scic_sds_phy_state_handler
2442 scic_sds_phy_state_handler_table[SCI_BASE_PHY_MAX_STATES] =
2443 {
2444         /* SCI_BASE_PHY_STATE_INITIAL */
2445         {
2446                 {
2447                         scic_sds_phy_default_start_handler,
2448                         scic_sds_phy_default_stop_handler,
2449                         scic_sds_phy_default_reset_handler,
2450                         scic_sds_phy_default_destroy_handler
2451                 },
2452                 scic_sds_phy_default_frame_handler,
2453                 scic_sds_phy_default_event_handler,
2454                 scic_sds_phy_default_consume_power_handler
2455         },
2456         /* SCI_BASE_PHY_STATE_STOPPED */
2457         {
2458                 {
2459                         scic_sds_phy_stopped_state_start_handler,
2460                         scic_sds_phy_default_stop_handler,
2461                         scic_sds_phy_default_reset_handler,
2462                         scic_sds_phy_stopped_state_destroy_handler
2463                 },
2464                 scic_sds_phy_default_frame_handler,
2465                 scic_sds_phy_default_event_handler,
2466                 scic_sds_phy_default_consume_power_handler
2467         },
2468         /* SCI_BASE_PHY_STATE_STARTING */
2469         {
2470                 {
2471                         scic_sds_phy_default_start_handler,
2472                         scic_sds_phy_default_stop_handler,
2473                         scic_sds_phy_default_reset_handler,
2474                         scic_sds_phy_default_destroy_handler
2475                 },
2476                 scic_sds_phy_default_frame_handler,
2477                 scic_sds_phy_default_event_handler,
2478                 scic_sds_phy_default_consume_power_handler
2479         },
2480         /* SCI_BASE_PHY_STATE_READY */
2481         {
2482                 {
2483                         scic_sds_phy_default_start_handler,
2484                         scic_sds_phy_ready_state_stop_handler,
2485                         scic_sds_phy_ready_state_reset_handler,
2486                         scic_sds_phy_default_destroy_handler
2487                 },
2488                 scic_sds_phy_default_frame_handler,
2489                 scic_sds_phy_ready_state_event_handler,
2490                 scic_sds_phy_default_consume_power_handler
2491         },
2492         /* SCI_BASE_PHY_STATE_RESETTING */
2493         {
2494                 {
2495                         scic_sds_phy_default_start_handler,
2496                         scic_sds_phy_default_stop_handler,
2497                         scic_sds_phy_default_reset_handler,
2498                         scic_sds_phy_default_destroy_handler
2499                 },
2500                 scic_sds_phy_default_frame_handler,
2501                 scic_sds_phy_resetting_state_event_handler,
2502                 scic_sds_phy_default_consume_power_handler
2503         },
2504         /* SCI_BASE_PHY_STATE_FINAL */
2505         {
2506                 {
2507                         scic_sds_phy_default_start_handler,
2508                         scic_sds_phy_default_stop_handler,
2509                         scic_sds_phy_default_reset_handler,
2510                         scic_sds_phy_default_destroy_handler
2511                 },
2512                 scic_sds_phy_default_frame_handler,
2513                 scic_sds_phy_default_event_handler,
2514                 scic_sds_phy_default_consume_power_handler
2515         }
2516 };
2517
2518 /*
2519  * ****************************************************************************
2520  * *  PHY STATE PRIVATE METHODS
2521  * **************************************************************************** */
2522
2523 /**
2524  *
2525  * @this_phy: This is the struct scic_sds_phy object to stop.
2526  *
2527  * This method will stop the struct scic_sds_phy object. This does not reset the
2528  * protocol engine it just suspends it and places it in a state where it will
2529  * not cause the end device to power up. none
2530  */
2531 static void scu_link_layer_stop_protocol_engine(
2532         struct scic_sds_phy *this_phy)
2533 {
2534         u32 scu_sas_pcfg_value;
2535         u32 enable_spinup_value;
2536
2537         /* Suspend the protocol engine and place it in a sata spinup hold state */
2538         scu_sas_pcfg_value  = SCU_SAS_PCFG_READ(this_phy);
2539         scu_sas_pcfg_value |= (
2540                 SCU_SAS_PCFG_GEN_BIT(OOB_RESET)
2541                 | SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE)
2542                 | SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD)
2543                 );
2544         SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
2545
2546         /* Disable the notify enable spinup primitives */
2547         enable_spinup_value = SCU_SAS_ENSPINUP_READ(this_phy);
2548         enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
2549         SCU_SAS_ENSPINUP_WRITE(this_phy, enable_spinup_value);
2550 }
2551
2552 /**
2553  *
2554  *
2555  * This method will start the OOB/SN state machine for this struct scic_sds_phy object.
2556  */
2557 static void scu_link_layer_start_oob(
2558         struct scic_sds_phy *this_phy)
2559 {
2560         u32 scu_sas_pcfg_value;
2561
2562         scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);
2563         scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
2564         scu_sas_pcfg_value &=
2565                 ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) | SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
2566
2567         SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
2568 }
2569
2570 /**
2571  *
2572  *
2573  * This method will transmit a hard reset request on the specified phy. The SCU
2574  * hardware requires that we reset the OOB state machine and set the hard reset
2575  * bit in the phy configuration register. We then must start OOB over with the
2576  * hard reset bit set.
2577  */
2578 static void scu_link_layer_tx_hard_reset(
2579         struct scic_sds_phy *this_phy)
2580 {
2581         u32 phy_configuration_value;
2582
2583         /*
2584          * SAS Phys must wait for the HARD_RESET_TX event notification to transition
2585          * to the starting state. */
2586         phy_configuration_value = SCU_SAS_PCFG_READ(this_phy);
2587         phy_configuration_value |=
2588                 (SCU_SAS_PCFG_GEN_BIT(HARD_RESET) | SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
2589         SCU_SAS_PCFG_WRITE(this_phy, phy_configuration_value);
2590
2591         /* Now take the OOB state machine out of reset */
2592         phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
2593         phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
2594         SCU_SAS_PCFG_WRITE(this_phy, phy_configuration_value);
2595 }
2596
2597 /*
2598  * ****************************************************************************
2599  * *  PHY BASE STATE METHODS
2600  * **************************************************************************** */
2601
2602 /**
2603  *
2604  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2605  *
2606  * This method will perform the actions required by the struct scic_sds_phy on
2607  * entering the SCI_BASE_PHY_STATE_INITIAL. - This function sets the state
2608  * handlers for the phy object base state machine initial state. none
2609  */
2610 static void scic_sds_phy_initial_state_enter(
2611         struct sci_base_object *object)
2612 {
2613         struct scic_sds_phy *this_phy;
2614
2615         this_phy = (struct scic_sds_phy *)object;
2616
2617         scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_STOPPED);
2618 }
2619
2620 /**
2621  *
2622  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2623  *
2624  * This method will perform the actions required by the struct scic_sds_phy on
2625  * entering the SCI_BASE_PHY_STATE_INITIAL. - This function sets the state
2626  * handlers for the phy object base state machine initial state. - The SCU
2627  * hardware is requested to stop the protocol engine. none
2628  */
2629 static void scic_sds_phy_stopped_state_enter(
2630         struct sci_base_object *object)
2631 {
2632         struct scic_sds_phy *this_phy;
2633
2634         this_phy = (struct scic_sds_phy *)object;
2635
2636         /* / @todo We need to get to the controller to place this PE in a reset state */
2637
2638         scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_STOPPED);
2639
2640         scu_link_layer_stop_protocol_engine(this_phy);
2641 }
2642
2643 /**
2644  *
2645  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2646  *
2647  * This method will perform the actions required by the struct scic_sds_phy on
2648  * entering the SCI_BASE_PHY_STATE_STARTING. - This function sets the state
2649  * handlers for the phy object base state machine starting state. - The SCU
2650  * hardware is requested to start OOB/SN on this protocl engine. - The phy
2651  * starting substate machine is started. - If the previous state was the ready
2652  * state then the struct scic_sds_controller is informed that the phy has gone link
2653  * down. none
2654  */
2655 static void scic_sds_phy_starting_state_enter(
2656         struct sci_base_object *object)
2657 {
2658         struct scic_sds_phy *this_phy;
2659
2660         this_phy = (struct scic_sds_phy *)object;
2661
2662         scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_STARTING);
2663
2664         scu_link_layer_stop_protocol_engine(this_phy);
2665         scu_link_layer_start_oob(this_phy);
2666
2667         /* We don't know what kind of phy we are going to be just yet */
2668         this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
2669         this_phy->bcn_received_while_port_unassigned = false;
2670
2671         /* Change over to the starting substate machine to continue */
2672         sci_base_state_machine_start(&this_phy->starting_substate_machine);
2673
2674         if (this_phy->parent.state_machine.previous_state_id
2675             == SCI_BASE_PHY_STATE_READY) {
2676                 scic_sds_controller_link_down(
2677                         scic_sds_phy_get_controller(this_phy),
2678                         scic_sds_phy_get_port(this_phy),
2679                         this_phy
2680                         );
2681         }
2682 }
2683
2684 /**
2685  *
2686  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2687  *
2688  * This method will perform the actions required by the struct scic_sds_phy on
2689  * entering the SCI_BASE_PHY_STATE_READY. - This function sets the state
2690  * handlers for the phy object base state machine ready state. - The SCU
2691  * hardware protocol engine is resumed. - The struct scic_sds_controller is informed
2692  * that the phy object has gone link up. none
2693  */
2694 static void scic_sds_phy_ready_state_enter(
2695         struct sci_base_object *object)
2696 {
2697         struct scic_sds_phy *this_phy;
2698
2699         this_phy = (struct scic_sds_phy *)object;
2700
2701         scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_READY);
2702
2703         scic_sds_controller_link_up(
2704                 scic_sds_phy_get_controller(this_phy),
2705                 scic_sds_phy_get_port(this_phy),
2706                 this_phy
2707                 );
2708 }
2709
2710 /**
2711  *
2712  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2713  *
2714  * This method will perform the actions required by the struct scic_sds_phy on exiting
2715  * the SCI_BASE_PHY_STATE_INITIAL. This function suspends the SCU hardware
2716  * protocol engine represented by this struct scic_sds_phy object. none
2717  */
2718 static void scic_sds_phy_ready_state_exit(
2719         struct sci_base_object *object)
2720 {
2721         struct scic_sds_phy *this_phy;
2722
2723         this_phy = (struct scic_sds_phy *)object;
2724
2725         scic_sds_phy_suspend(this_phy);
2726 }
2727
2728 /**
2729  *
2730  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2731  *
2732  * This method will perform the actions required by the struct scic_sds_phy on
2733  * entering the SCI_BASE_PHY_STATE_RESETTING. - This function sets the state
2734  * handlers for the phy object base state machine resetting state. none
2735  */
2736 static void scic_sds_phy_resetting_state_enter(
2737         struct sci_base_object *object)
2738 {
2739         struct scic_sds_phy *this_phy;
2740
2741         this_phy = (struct scic_sds_phy *)object;
2742
2743         scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_RESETTING);
2744
2745         /*
2746          * The phy is being reset, therefore deactivate it from the port.
2747          * In the resetting state we don't notify the user regarding
2748          * link up and link down notifications. */
2749         scic_sds_port_deactivate_phy(this_phy->owning_port, this_phy, false);
2750
2751         if (this_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
2752                 scu_link_layer_tx_hard_reset(this_phy);
2753         } else {
2754                 /*
2755                  * The SCU does not need to have a descrete reset state so just go back to
2756                  * the starting state. */
2757                 sci_base_state_machine_change_state(
2758                         &this_phy->parent.state_machine,
2759                         SCI_BASE_PHY_STATE_STARTING
2760                         );
2761         }
2762 }
2763
2764 /**
2765  *
2766  * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
2767  *
2768  * This method will perform the actions required by the struct scic_sds_phy on
2769  * entering the SCI_BASE_PHY_STATE_FINAL. - This function sets the state
2770  * handlers for the phy object base state machine final state. none
2771  */
2772 static void scic_sds_phy_final_state_enter(
2773         struct sci_base_object *object)
2774 {
2775         struct scic_sds_phy *this_phy;
2776
2777         this_phy = (struct scic_sds_phy *)object;
2778
2779         scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_FINAL);
2780
2781         /* Nothing to do here */
2782 }
2783
2784 /* --------------------------------------------------------------------------- */
2785
2786 const struct sci_base_state scic_sds_phy_state_table[] = {
2787         [SCI_BASE_PHY_STATE_INITIAL] = {
2788                 .enter_state = scic_sds_phy_initial_state_enter,
2789         },
2790         [SCI_BASE_PHY_STATE_STOPPED] = {
2791                 .enter_state = scic_sds_phy_stopped_state_enter,
2792         },
2793         [SCI_BASE_PHY_STATE_STARTING] = {
2794                 .enter_state = scic_sds_phy_starting_state_enter,
2795         },
2796         [SCI_BASE_PHY_STATE_READY] = {
2797                 .enter_state = scic_sds_phy_ready_state_enter,
2798                 .exit_state = scic_sds_phy_ready_state_exit,
2799         },
2800         [SCI_BASE_PHY_STATE_RESETTING] = {
2801                 .enter_state = scic_sds_phy_resetting_state_enter,
2802         },
2803         [SCI_BASE_PHY_STATE_FINAL] = {
2804                 .enter_state = scic_sds_phy_final_state_enter,
2805         },
2806 };
2807