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