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