xref: /OK3568_Linux_fs/kernel/drivers/scsi/isci/phy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
3*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
19*4882a593Smuzhiyun  * along with this program; if not, write to the Free Software
20*4882a593Smuzhiyun  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21*4882a593Smuzhiyun  * The full GNU General Public License is included in this distribution
22*4882a593Smuzhiyun  * in the file called LICENSE.GPL.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * BSD LICENSE
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27*4882a593Smuzhiyun  * All rights reserved.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
30*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
31*4882a593Smuzhiyun  * are met:
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *   * Redistributions of source code must retain the above copyright
34*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer.
35*4882a593Smuzhiyun  *   * Redistributions in binary form must reproduce the above copyright
36*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer in
37*4882a593Smuzhiyun  *     the documentation and/or other materials provided with the
38*4882a593Smuzhiyun  *     distribution.
39*4882a593Smuzhiyun  *   * Neither the name of Intel Corporation nor the names of its
40*4882a593Smuzhiyun  *     contributors may be used to endorse or promote products derived
41*4882a593Smuzhiyun  *     from this software without specific prior written permission.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #include "isci.h"
57*4882a593Smuzhiyun #include "host.h"
58*4882a593Smuzhiyun #include "phy.h"
59*4882a593Smuzhiyun #include "scu_event_codes.h"
60*4882a593Smuzhiyun #include "probe_roms.h"
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #undef C
63*4882a593Smuzhiyun #define C(a) (#a)
phy_state_name(enum sci_phy_states state)64*4882a593Smuzhiyun static const char *phy_state_name(enum sci_phy_states state)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	static const char * const strings[] = PHY_STATES;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	return strings[state];
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun #undef C
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /* Maximum arbitration wait time in micro-seconds */
73*4882a593Smuzhiyun #define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME  (700)
74*4882a593Smuzhiyun 
sci_phy_linkrate(struct isci_phy * iphy)75*4882a593Smuzhiyun enum sas_linkrate sci_phy_linkrate(struct isci_phy *iphy)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	return iphy->max_negotiated_speed;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
phy_to_host(struct isci_phy * iphy)80*4882a593Smuzhiyun static struct isci_host *phy_to_host(struct isci_phy *iphy)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	struct isci_phy *table = iphy - iphy->phy_index;
83*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(table, typeof(*ihost), phys[0]);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return ihost;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
sciphy_to_dev(struct isci_phy * iphy)88*4882a593Smuzhiyun static struct device *sciphy_to_dev(struct isci_phy *iphy)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return &phy_to_host(iphy)->pdev->dev;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static enum sci_status
sci_phy_transport_layer_initialization(struct isci_phy * iphy,struct scu_transport_layer_registers __iomem * reg)94*4882a593Smuzhiyun sci_phy_transport_layer_initialization(struct isci_phy *iphy,
95*4882a593Smuzhiyun 				       struct scu_transport_layer_registers __iomem *reg)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	u32 tl_control;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	iphy->transport_layer_registers = reg;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	writel(SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX,
102*4882a593Smuzhiyun 		&iphy->transport_layer_registers->stp_rni);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/*
105*4882a593Smuzhiyun 	 * Hardware team recommends that we enable the STP prefetch for all
106*4882a593Smuzhiyun 	 * transports
107*4882a593Smuzhiyun 	 */
108*4882a593Smuzhiyun 	tl_control = readl(&iphy->transport_layer_registers->control);
109*4882a593Smuzhiyun 	tl_control |= SCU_TLCR_GEN_BIT(STP_WRITE_DATA_PREFETCH);
110*4882a593Smuzhiyun 	writel(tl_control, &iphy->transport_layer_registers->control);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	return SCI_SUCCESS;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static enum sci_status
sci_phy_link_layer_initialization(struct isci_phy * iphy,struct scu_link_layer_registers __iomem * llr)116*4882a593Smuzhiyun sci_phy_link_layer_initialization(struct isci_phy *iphy,
117*4882a593Smuzhiyun 				  struct scu_link_layer_registers __iomem *llr)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
120*4882a593Smuzhiyun 	struct sci_phy_user_params *phy_user;
121*4882a593Smuzhiyun 	struct sci_phy_oem_params *phy_oem;
122*4882a593Smuzhiyun 	int phy_idx = iphy->phy_index;
123*4882a593Smuzhiyun 	struct sci_phy_cap phy_cap;
124*4882a593Smuzhiyun 	u32 phy_configuration;
125*4882a593Smuzhiyun 	u32 parity_check = 0;
126*4882a593Smuzhiyun 	u32 parity_count = 0;
127*4882a593Smuzhiyun 	u32 llctl, link_rate;
128*4882a593Smuzhiyun 	u32 clksm_value = 0;
129*4882a593Smuzhiyun 	u32 sp_timeouts = 0;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	phy_user = &ihost->user_parameters.phys[phy_idx];
132*4882a593Smuzhiyun 	phy_oem = &ihost->oem_parameters.phys[phy_idx];
133*4882a593Smuzhiyun 	iphy->link_layer_registers = llr;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* Set our IDENTIFY frame data */
136*4882a593Smuzhiyun 	#define SCI_END_DEVICE 0x01
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	writel(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR) |
139*4882a593Smuzhiyun 	       SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR) |
140*4882a593Smuzhiyun 	       SCU_SAS_TIID_GEN_BIT(STP_INITIATOR) |
141*4882a593Smuzhiyun 	       SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST) |
142*4882a593Smuzhiyun 	       SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE),
143*4882a593Smuzhiyun 	       &llr->transmit_identification);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* Write the device SAS Address */
146*4882a593Smuzhiyun 	writel(0xFEDCBA98, &llr->sas_device_name_high);
147*4882a593Smuzhiyun 	writel(phy_idx, &llr->sas_device_name_low);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* Write the source SAS Address */
150*4882a593Smuzhiyun 	writel(phy_oem->sas_address.high, &llr->source_sas_address_high);
151*4882a593Smuzhiyun 	writel(phy_oem->sas_address.low, &llr->source_sas_address_low);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/* Clear and Set the PHY Identifier */
154*4882a593Smuzhiyun 	writel(0, &llr->identify_frame_phy_id);
155*4882a593Smuzhiyun 	writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx), &llr->identify_frame_phy_id);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	/* Change the initial state of the phy configuration register */
158*4882a593Smuzhiyun 	phy_configuration = readl(&llr->phy_configuration);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* Hold OOB state machine in reset */
161*4882a593Smuzhiyun 	phy_configuration |=  SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
162*4882a593Smuzhiyun 	writel(phy_configuration, &llr->phy_configuration);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* Configure the SNW capabilities */
165*4882a593Smuzhiyun 	phy_cap.all = 0;
166*4882a593Smuzhiyun 	phy_cap.start = 1;
167*4882a593Smuzhiyun 	phy_cap.gen3_no_ssc = 1;
168*4882a593Smuzhiyun 	phy_cap.gen2_no_ssc = 1;
169*4882a593Smuzhiyun 	phy_cap.gen1_no_ssc = 1;
170*4882a593Smuzhiyun 	if (ihost->oem_parameters.controller.do_enable_ssc) {
171*4882a593Smuzhiyun 		struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
172*4882a593Smuzhiyun 		struct scu_afe_transceiver __iomem *xcvr = &afe->scu_afe_xcvr[phy_idx];
173*4882a593Smuzhiyun 		struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
174*4882a593Smuzhiyun 		bool en_sas = false;
175*4882a593Smuzhiyun 		bool en_sata = false;
176*4882a593Smuzhiyun 		u32 sas_type = 0;
177*4882a593Smuzhiyun 		u32 sata_spread = 0x2;
178*4882a593Smuzhiyun 		u32 sas_spread = 0x2;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		phy_cap.gen3_ssc = 1;
181*4882a593Smuzhiyun 		phy_cap.gen2_ssc = 1;
182*4882a593Smuzhiyun 		phy_cap.gen1_ssc = 1;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		if (pci_info->orom->hdr.version < ISCI_ROM_VER_1_1)
185*4882a593Smuzhiyun 			en_sas = en_sata = true;
186*4882a593Smuzhiyun 		else {
187*4882a593Smuzhiyun 			sata_spread = ihost->oem_parameters.controller.ssc_sata_tx_spread_level;
188*4882a593Smuzhiyun 			sas_spread = ihost->oem_parameters.controller.ssc_sas_tx_spread_level;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 			if (sata_spread)
191*4882a593Smuzhiyun 				en_sata = true;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 			if (sas_spread) {
194*4882a593Smuzhiyun 				en_sas = true;
195*4882a593Smuzhiyun 				sas_type = ihost->oem_parameters.controller.ssc_sas_tx_type;
196*4882a593Smuzhiyun 			}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 		}
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		if (en_sas) {
201*4882a593Smuzhiyun 			u32 reg;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 			reg = readl(&xcvr->afe_xcvr_control0);
204*4882a593Smuzhiyun 			reg |= (0x00100000 | (sas_type << 19));
205*4882a593Smuzhiyun 			writel(reg, &xcvr->afe_xcvr_control0);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 			reg = readl(&xcvr->afe_tx_ssc_control);
208*4882a593Smuzhiyun 			reg |= sas_spread << 8;
209*4882a593Smuzhiyun 			writel(reg, &xcvr->afe_tx_ssc_control);
210*4882a593Smuzhiyun 		}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		if (en_sata) {
213*4882a593Smuzhiyun 			u32 reg;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 			reg = readl(&xcvr->afe_tx_ssc_control);
216*4882a593Smuzhiyun 			reg |= sata_spread;
217*4882a593Smuzhiyun 			writel(reg, &xcvr->afe_tx_ssc_control);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 			reg = readl(&llr->stp_control);
220*4882a593Smuzhiyun 			reg |= 1 << 12;
221*4882a593Smuzhiyun 			writel(reg, &llr->stp_control);
222*4882a593Smuzhiyun 		}
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* The SAS specification indicates that the phy_capabilities that
226*4882a593Smuzhiyun 	 * are transmitted shall have an even parity.  Calculate the parity.
227*4882a593Smuzhiyun 	 */
228*4882a593Smuzhiyun 	parity_check = phy_cap.all;
229*4882a593Smuzhiyun 	while (parity_check != 0) {
230*4882a593Smuzhiyun 		if (parity_check & 0x1)
231*4882a593Smuzhiyun 			parity_count++;
232*4882a593Smuzhiyun 		parity_check >>= 1;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/* If parity indicates there are an odd number of bits set, then
236*4882a593Smuzhiyun 	 * set the parity bit to 1 in the phy capabilities.
237*4882a593Smuzhiyun 	 */
238*4882a593Smuzhiyun 	if ((parity_count % 2) != 0)
239*4882a593Smuzhiyun 		phy_cap.parity = 1;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	writel(phy_cap.all, &llr->phy_capabilities);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/* Set the enable spinup period but disable the ability to send
244*4882a593Smuzhiyun 	 * notify enable spinup
245*4882a593Smuzhiyun 	 */
246*4882a593Smuzhiyun 	writel(SCU_ENSPINUP_GEN_VAL(COUNT,
247*4882a593Smuzhiyun 			phy_user->notify_enable_spin_up_insertion_frequency),
248*4882a593Smuzhiyun 		&llr->notify_enable_spinup_control);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/* Write the ALIGN Insertion Ferequency for connected phy and
251*4882a593Smuzhiyun 	 * inpendent of connected state
252*4882a593Smuzhiyun 	 */
253*4882a593Smuzhiyun 	clksm_value = SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(CONNECTED,
254*4882a593Smuzhiyun 			phy_user->in_connection_align_insertion_frequency);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	clksm_value |= SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(GENERAL,
257*4882a593Smuzhiyun 			phy_user->align_insertion_frequency);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	writel(clksm_value, &llr->clock_skew_management);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	if (is_c0(ihost->pdev) || is_c1(ihost->pdev)) {
262*4882a593Smuzhiyun 		writel(0x04210400, &llr->afe_lookup_table_control);
263*4882a593Smuzhiyun 		writel(0x020A7C05, &llr->sas_primitive_timeout);
264*4882a593Smuzhiyun 	} else
265*4882a593Smuzhiyun 		writel(0x02108421, &llr->afe_lookup_table_control);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
268*4882a593Smuzhiyun 		(u8)ihost->user_parameters.no_outbound_task_timeout);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	switch (phy_user->max_speed_generation) {
271*4882a593Smuzhiyun 	case SCIC_SDS_PARM_GEN3_SPEED:
272*4882a593Smuzhiyun 		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
273*4882a593Smuzhiyun 		break;
274*4882a593Smuzhiyun 	case SCIC_SDS_PARM_GEN2_SPEED:
275*4882a593Smuzhiyun 		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
276*4882a593Smuzhiyun 		break;
277*4882a593Smuzhiyun 	default:
278*4882a593Smuzhiyun 		link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
279*4882a593Smuzhiyun 		break;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 	llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
282*4882a593Smuzhiyun 	writel(llctl, &llr->link_layer_control);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	sp_timeouts = readl(&llr->sas_phy_timeouts);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/* Clear the default 0x36 (54us) RATE_CHANGE timeout value. */
287*4882a593Smuzhiyun 	sp_timeouts &= ~SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0xFF);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	/* Set RATE_CHANGE timeout value to 0x3B (59us).  This ensures SCU can
290*4882a593Smuzhiyun 	 * lock with 3Gb drive when SCU max rate is set to 1.5Gb.
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	sp_timeouts |= SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0x3B);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	writel(sp_timeouts, &llr->sas_phy_timeouts);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	if (is_a2(ihost->pdev)) {
297*4882a593Smuzhiyun 		/* Program the max ARB time for the PHY to 700us so we
298*4882a593Smuzhiyun 		 * inter-operate with the PMC expander which shuts down
299*4882a593Smuzhiyun 		 * PHYs if the expander PHY generates too many breaks.
300*4882a593Smuzhiyun 		 * This time value will guarantee that the initiator PHY
301*4882a593Smuzhiyun 		 * will generate the break.
302*4882a593Smuzhiyun 		 */
303*4882a593Smuzhiyun 		writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
304*4882a593Smuzhiyun 		       &llr->maximum_arbitration_wait_timer_timeout);
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	/* Disable link layer hang detection, rely on the OS timeout for
308*4882a593Smuzhiyun 	 * I/O timeouts.
309*4882a593Smuzhiyun 	 */
310*4882a593Smuzhiyun 	writel(0, &llr->link_layer_hang_detection_timeout);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	/* We can exit the initial state to the stopped state */
313*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	return SCI_SUCCESS;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
phy_sata_timeout(struct timer_list * t)318*4882a593Smuzhiyun static void phy_sata_timeout(struct timer_list *t)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	struct sci_timer *tmr = from_timer(tmr, t, timer);
321*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
322*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
323*4882a593Smuzhiyun 	unsigned long flags;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	spin_lock_irqsave(&ihost->scic_lock, flags);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	if (tmr->cancel)
328*4882a593Smuzhiyun 		goto done;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	dev_dbg(sciphy_to_dev(iphy),
331*4882a593Smuzhiyun 		 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
332*4882a593Smuzhiyun 		 "timeout.\n",
333*4882a593Smuzhiyun 		 __func__,
334*4882a593Smuzhiyun 		 iphy);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_STARTING);
337*4882a593Smuzhiyun done:
338*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun /**
342*4882a593Smuzhiyun  * This method returns the port currently containing this phy. If the phy is
343*4882a593Smuzhiyun  *    currently contained by the dummy port, then the phy is considered to not
344*4882a593Smuzhiyun  *    be part of a port.
345*4882a593Smuzhiyun  * @sci_phy: This parameter specifies the phy for which to retrieve the
346*4882a593Smuzhiyun  *    containing port.
347*4882a593Smuzhiyun  *
348*4882a593Smuzhiyun  * This method returns a handle to a port that contains the supplied phy.
349*4882a593Smuzhiyun  * NULL This value is returned if the phy is not part of a real
350*4882a593Smuzhiyun  * port (i.e. it's contained in the dummy port). !NULL All other
351*4882a593Smuzhiyun  * values indicate a handle/pointer to the port containing the phy.
352*4882a593Smuzhiyun  */
phy_get_non_dummy_port(struct isci_phy * iphy)353*4882a593Smuzhiyun struct isci_port *phy_get_non_dummy_port(struct isci_phy *iphy)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	struct isci_port *iport = iphy->owning_port;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (iport->physical_port_index == SCIC_SDS_DUMMY_PORT)
358*4882a593Smuzhiyun 		return NULL;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return iphy->owning_port;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun  * This method will assign a port to the phy object.
365*4882a593Smuzhiyun  * @out]: iphy This parameter specifies the phy for which to assign a port
366*4882a593Smuzhiyun  *    object.
367*4882a593Smuzhiyun  *
368*4882a593Smuzhiyun  *
369*4882a593Smuzhiyun  */
sci_phy_set_port(struct isci_phy * iphy,struct isci_port * iport)370*4882a593Smuzhiyun void sci_phy_set_port(
371*4882a593Smuzhiyun 	struct isci_phy *iphy,
372*4882a593Smuzhiyun 	struct isci_port *iport)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	iphy->owning_port = iport;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (iphy->bcn_received_while_port_unassigned) {
377*4882a593Smuzhiyun 		iphy->bcn_received_while_port_unassigned = false;
378*4882a593Smuzhiyun 		sci_port_broadcast_change_received(iphy->owning_port, iphy);
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
sci_phy_initialize(struct isci_phy * iphy,struct scu_transport_layer_registers __iomem * tl,struct scu_link_layer_registers __iomem * ll)382*4882a593Smuzhiyun enum sci_status sci_phy_initialize(struct isci_phy *iphy,
383*4882a593Smuzhiyun 				   struct scu_transport_layer_registers __iomem *tl,
384*4882a593Smuzhiyun 				   struct scu_link_layer_registers __iomem *ll)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	/* Perfrom the initialization of the TL hardware */
387*4882a593Smuzhiyun 	sci_phy_transport_layer_initialization(iphy, tl);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* Perofrm the initialization of the PE hardware */
390*4882a593Smuzhiyun 	sci_phy_link_layer_initialization(iphy, ll);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/* There is nothing that needs to be done in this state just
393*4882a593Smuzhiyun 	 * transition to the stopped state
394*4882a593Smuzhiyun 	 */
395*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	return SCI_SUCCESS;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun /**
401*4882a593Smuzhiyun  * This method assigns the direct attached device ID for this phy.
402*4882a593Smuzhiyun  *
403*4882a593Smuzhiyun  * @iphy The phy for which the direct attached device id is to
404*4882a593Smuzhiyun  *       be assigned.
405*4882a593Smuzhiyun  * @device_id The direct attached device ID to assign to the phy.
406*4882a593Smuzhiyun  *       This will either be the RNi for the device or an invalid RNi if there
407*4882a593Smuzhiyun  *       is no current device assigned to the phy.
408*4882a593Smuzhiyun  */
sci_phy_setup_transport(struct isci_phy * iphy,u32 device_id)409*4882a593Smuzhiyun void sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	u32 tl_control;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	writel(device_id, &iphy->transport_layer_registers->stp_rni);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	/*
416*4882a593Smuzhiyun 	 * The read should guarantee that the first write gets posted
417*4882a593Smuzhiyun 	 * before the next write
418*4882a593Smuzhiyun 	 */
419*4882a593Smuzhiyun 	tl_control = readl(&iphy->transport_layer_registers->control);
420*4882a593Smuzhiyun 	tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
421*4882a593Smuzhiyun 	writel(tl_control, &iphy->transport_layer_registers->control);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
sci_phy_suspend(struct isci_phy * iphy)424*4882a593Smuzhiyun static void sci_phy_suspend(struct isci_phy *iphy)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	u32 scu_sas_pcfg_value;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	scu_sas_pcfg_value =
429*4882a593Smuzhiyun 		readl(&iphy->link_layer_registers->phy_configuration);
430*4882a593Smuzhiyun 	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
431*4882a593Smuzhiyun 	writel(scu_sas_pcfg_value,
432*4882a593Smuzhiyun 		&iphy->link_layer_registers->phy_configuration);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
sci_phy_resume(struct isci_phy * iphy)437*4882a593Smuzhiyun void sci_phy_resume(struct isci_phy *iphy)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	u32 scu_sas_pcfg_value;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	scu_sas_pcfg_value =
442*4882a593Smuzhiyun 		readl(&iphy->link_layer_registers->phy_configuration);
443*4882a593Smuzhiyun 	scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
444*4882a593Smuzhiyun 	writel(scu_sas_pcfg_value,
445*4882a593Smuzhiyun 		&iphy->link_layer_registers->phy_configuration);
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
sci_phy_get_sas_address(struct isci_phy * iphy,struct sci_sas_address * sas)448*4882a593Smuzhiyun void sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
451*4882a593Smuzhiyun 	sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun 
sci_phy_get_attached_sas_address(struct isci_phy * iphy,struct sci_sas_address * sas)454*4882a593Smuzhiyun void sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun 	struct sas_identify_frame *iaf;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	iaf = &iphy->frame_rcvd.iaf;
459*4882a593Smuzhiyun 	memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun 
sci_phy_get_protocols(struct isci_phy * iphy,struct sci_phy_proto * proto)462*4882a593Smuzhiyun void sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	proto->all = readl(&iphy->link_layer_registers->transmit_identification);
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
sci_phy_start(struct isci_phy * iphy)467*4882a593Smuzhiyun enum sci_status sci_phy_start(struct isci_phy *iphy)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	enum sci_phy_states state = iphy->sm.current_state_id;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	if (state != SCI_PHY_STOPPED) {
472*4882a593Smuzhiyun 		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
473*4882a593Smuzhiyun 			__func__, phy_state_name(state));
474*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_STARTING);
478*4882a593Smuzhiyun 	return SCI_SUCCESS;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun 
sci_phy_stop(struct isci_phy * iphy)481*4882a593Smuzhiyun enum sci_status sci_phy_stop(struct isci_phy *iphy)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	enum sci_phy_states state = iphy->sm.current_state_id;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	switch (state) {
486*4882a593Smuzhiyun 	case SCI_PHY_SUB_INITIAL:
487*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_OSSP_EN:
488*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
489*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
490*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
491*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
492*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
493*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
494*4882a593Smuzhiyun 	case SCI_PHY_SUB_FINAL:
495*4882a593Smuzhiyun 	case SCI_PHY_READY:
496*4882a593Smuzhiyun 		break;
497*4882a593Smuzhiyun 	default:
498*4882a593Smuzhiyun 		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
499*4882a593Smuzhiyun 			__func__, phy_state_name(state));
500*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
504*4882a593Smuzhiyun 	return SCI_SUCCESS;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
sci_phy_reset(struct isci_phy * iphy)507*4882a593Smuzhiyun enum sci_status sci_phy_reset(struct isci_phy *iphy)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun 	enum sci_phy_states state = iphy->sm.current_state_id;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if (state != SCI_PHY_READY) {
512*4882a593Smuzhiyun 		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
513*4882a593Smuzhiyun 			__func__, phy_state_name(state));
514*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
515*4882a593Smuzhiyun 	}
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_RESETTING);
518*4882a593Smuzhiyun 	return SCI_SUCCESS;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
sci_phy_consume_power_handler(struct isci_phy * iphy)521*4882a593Smuzhiyun enum sci_status sci_phy_consume_power_handler(struct isci_phy *iphy)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	enum sci_phy_states state = iphy->sm.current_state_id;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	switch (state) {
526*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_POWER: {
527*4882a593Smuzhiyun 		u32 enable_spinup;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 		enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
530*4882a593Smuzhiyun 		enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
531*4882a593Smuzhiyun 		writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 		/* Change state to the final state this substate machine has run to completion */
534*4882a593Smuzhiyun 		sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 		return SCI_SUCCESS;
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_POWER: {
539*4882a593Smuzhiyun 		u32 scu_sas_pcfg_value;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		/* Release the spinup hold state and reset the OOB state machine */
542*4882a593Smuzhiyun 		scu_sas_pcfg_value =
543*4882a593Smuzhiyun 			readl(&iphy->link_layer_registers->phy_configuration);
544*4882a593Smuzhiyun 		scu_sas_pcfg_value &=
545*4882a593Smuzhiyun 			~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
546*4882a593Smuzhiyun 		scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
547*4882a593Smuzhiyun 		writel(scu_sas_pcfg_value,
548*4882a593Smuzhiyun 			&iphy->link_layer_registers->phy_configuration);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 		/* Now restart the OOB operation */
551*4882a593Smuzhiyun 		scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
552*4882a593Smuzhiyun 		scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
553*4882a593Smuzhiyun 		writel(scu_sas_pcfg_value,
554*4882a593Smuzhiyun 			&iphy->link_layer_registers->phy_configuration);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 		/* Change state to the final state this substate machine has run to completion */
557*4882a593Smuzhiyun 		sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 		return SCI_SUCCESS;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 	default:
562*4882a593Smuzhiyun 		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
563*4882a593Smuzhiyun 			__func__, phy_state_name(state));
564*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
565*4882a593Smuzhiyun 	}
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
sci_phy_start_sas_link_training(struct isci_phy * iphy)568*4882a593Smuzhiyun static void sci_phy_start_sas_link_training(struct isci_phy *iphy)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	/* continue the link training for the phy as if it were a SAS PHY
571*4882a593Smuzhiyun 	 * instead of a SATA PHY. This is done because the completion queue had a SAS
572*4882a593Smuzhiyun 	 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
573*4882a593Smuzhiyun 	 */
574*4882a593Smuzhiyun 	u32 phy_control;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	phy_control = readl(&iphy->link_layer_registers->phy_configuration);
577*4882a593Smuzhiyun 	phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
578*4882a593Smuzhiyun 	writel(phy_control,
579*4882a593Smuzhiyun 	       &iphy->link_layer_registers->phy_configuration);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	iphy->protocol = SAS_PROTOCOL_SSP;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
sci_phy_start_sata_link_training(struct isci_phy * iphy)586*4882a593Smuzhiyun static void sci_phy_start_sata_link_training(struct isci_phy *iphy)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun 	/* This method continues the link training for the phy as if it were a SATA PHY
589*4882a593Smuzhiyun 	 * instead of a SAS PHY.  This is done because the completion queue had a SATA
590*4882a593Smuzhiyun 	 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
591*4882a593Smuzhiyun 	 */
592*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	iphy->protocol = SAS_PROTOCOL_SATA;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun /**
598*4882a593Smuzhiyun  * sci_phy_complete_link_training - perform processing common to
599*4882a593Smuzhiyun  *    all protocols upon completion of link training.
600*4882a593Smuzhiyun  * @sci_phy: This parameter specifies the phy object for which link training
601*4882a593Smuzhiyun  *    has completed.
602*4882a593Smuzhiyun  * @max_link_rate: This parameter specifies the maximum link rate to be
603*4882a593Smuzhiyun  *    associated with this phy.
604*4882a593Smuzhiyun  * @next_state: This parameter specifies the next state for the phy's starting
605*4882a593Smuzhiyun  *    sub-state machine.
606*4882a593Smuzhiyun  *
607*4882a593Smuzhiyun  */
sci_phy_complete_link_training(struct isci_phy * iphy,enum sas_linkrate max_link_rate,u32 next_state)608*4882a593Smuzhiyun static void sci_phy_complete_link_training(struct isci_phy *iphy,
609*4882a593Smuzhiyun 					   enum sas_linkrate max_link_rate,
610*4882a593Smuzhiyun 					   u32 next_state)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	iphy->max_negotiated_speed = max_link_rate;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, next_state);
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
phy_event_name(u32 event_code)617*4882a593Smuzhiyun static const char *phy_event_name(u32 event_code)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun 	switch (scu_get_event_code(event_code)) {
620*4882a593Smuzhiyun 	case SCU_EVENT_PORT_SELECTOR_DETECTED:
621*4882a593Smuzhiyun 		return "port selector";
622*4882a593Smuzhiyun 	case SCU_EVENT_SENT_PORT_SELECTION:
623*4882a593Smuzhiyun 		return "port selection";
624*4882a593Smuzhiyun 	case SCU_EVENT_HARD_RESET_TRANSMITTED:
625*4882a593Smuzhiyun 		return "tx hard reset";
626*4882a593Smuzhiyun 	case SCU_EVENT_HARD_RESET_RECEIVED:
627*4882a593Smuzhiyun 		return "rx hard reset";
628*4882a593Smuzhiyun 	case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
629*4882a593Smuzhiyun 		return "identify timeout";
630*4882a593Smuzhiyun 	case SCU_EVENT_LINK_FAILURE:
631*4882a593Smuzhiyun 		return "link fail";
632*4882a593Smuzhiyun 	case SCU_EVENT_SATA_SPINUP_HOLD:
633*4882a593Smuzhiyun 		return "sata spinup hold";
634*4882a593Smuzhiyun 	case SCU_EVENT_SAS_15_SSC:
635*4882a593Smuzhiyun 	case SCU_EVENT_SAS_15:
636*4882a593Smuzhiyun 		return "sas 1.5";
637*4882a593Smuzhiyun 	case SCU_EVENT_SAS_30_SSC:
638*4882a593Smuzhiyun 	case SCU_EVENT_SAS_30:
639*4882a593Smuzhiyun 		return "sas 3.0";
640*4882a593Smuzhiyun 	case SCU_EVENT_SAS_60_SSC:
641*4882a593Smuzhiyun 	case SCU_EVENT_SAS_60:
642*4882a593Smuzhiyun 		return "sas 6.0";
643*4882a593Smuzhiyun 	case SCU_EVENT_SATA_15_SSC:
644*4882a593Smuzhiyun 	case SCU_EVENT_SATA_15:
645*4882a593Smuzhiyun 		return "sata 1.5";
646*4882a593Smuzhiyun 	case SCU_EVENT_SATA_30_SSC:
647*4882a593Smuzhiyun 	case SCU_EVENT_SATA_30:
648*4882a593Smuzhiyun 		return "sata 3.0";
649*4882a593Smuzhiyun 	case SCU_EVENT_SATA_60_SSC:
650*4882a593Smuzhiyun 	case SCU_EVENT_SATA_60:
651*4882a593Smuzhiyun 		return "sata 6.0";
652*4882a593Smuzhiyun 	case SCU_EVENT_SAS_PHY_DETECTED:
653*4882a593Smuzhiyun 		return "sas detect";
654*4882a593Smuzhiyun 	case SCU_EVENT_SATA_PHY_DETECTED:
655*4882a593Smuzhiyun 		return "sata detect";
656*4882a593Smuzhiyun 	default:
657*4882a593Smuzhiyun 		return "unknown";
658*4882a593Smuzhiyun 	}
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun #define phy_event_dbg(iphy, state, code) \
662*4882a593Smuzhiyun 	dev_dbg(sciphy_to_dev(iphy), "phy-%d:%d: %s event: %s (%x)\n", \
663*4882a593Smuzhiyun 		phy_to_host(iphy)->id, iphy->phy_index, \
664*4882a593Smuzhiyun 		phy_state_name(state), phy_event_name(code), code)
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun #define phy_event_warn(iphy, state, code) \
667*4882a593Smuzhiyun 	dev_warn(sciphy_to_dev(iphy), "phy-%d:%d: %s event: %s (%x)\n", \
668*4882a593Smuzhiyun 		phy_to_host(iphy)->id, iphy->phy_index, \
669*4882a593Smuzhiyun 		phy_state_name(state), phy_event_name(code), code)
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 
scu_link_layer_set_txcomsas_timeout(struct isci_phy * iphy,u32 timeout)672*4882a593Smuzhiyun static void scu_link_layer_set_txcomsas_timeout(struct isci_phy *iphy, u32 timeout)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun 	u32 val;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	/* Extend timeout */
677*4882a593Smuzhiyun 	val = readl(&iphy->link_layer_registers->transmit_comsas_signal);
678*4882a593Smuzhiyun 	val &= ~SCU_SAS_LLTXCOMSAS_GEN_VAL(NEGTIME, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_MASK);
679*4882a593Smuzhiyun 	val |= SCU_SAS_LLTXCOMSAS_GEN_VAL(NEGTIME, timeout);
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	writel(val, &iphy->link_layer_registers->transmit_comsas_signal);
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
sci_phy_event_handler(struct isci_phy * iphy,u32 event_code)684*4882a593Smuzhiyun enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	enum sci_phy_states state = iphy->sm.current_state_id;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	switch (state) {
689*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_OSSP_EN:
690*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
691*4882a593Smuzhiyun 		case SCU_EVENT_SAS_PHY_DETECTED:
692*4882a593Smuzhiyun 			sci_phy_start_sas_link_training(iphy);
693*4882a593Smuzhiyun 			iphy->is_in_link_training = true;
694*4882a593Smuzhiyun 			break;
695*4882a593Smuzhiyun 		case SCU_EVENT_SATA_SPINUP_HOLD:
696*4882a593Smuzhiyun 			sci_phy_start_sata_link_training(iphy);
697*4882a593Smuzhiyun 			iphy->is_in_link_training = true;
698*4882a593Smuzhiyun 			break;
699*4882a593Smuzhiyun 		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
700*4882a593Smuzhiyun 		       /* Extend timeout value */
701*4882a593Smuzhiyun 		       scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 		       /* Start the oob/sn state machine over again */
704*4882a593Smuzhiyun 		       sci_change_state(&iphy->sm, SCI_PHY_STARTING);
705*4882a593Smuzhiyun 		       break;
706*4882a593Smuzhiyun 		default:
707*4882a593Smuzhiyun 			phy_event_dbg(iphy, state, event_code);
708*4882a593Smuzhiyun 			return SCI_FAILURE;
709*4882a593Smuzhiyun 		}
710*4882a593Smuzhiyun 		return SCI_SUCCESS;
711*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
712*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
713*4882a593Smuzhiyun 		case SCU_EVENT_SAS_PHY_DETECTED:
714*4882a593Smuzhiyun 			/*
715*4882a593Smuzhiyun 			 * Why is this being reported again by the controller?
716*4882a593Smuzhiyun 			 * We would re-enter this state so just stay here */
717*4882a593Smuzhiyun 			break;
718*4882a593Smuzhiyun 		case SCU_EVENT_SAS_15:
719*4882a593Smuzhiyun 		case SCU_EVENT_SAS_15_SSC:
720*4882a593Smuzhiyun 			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
721*4882a593Smuzhiyun 						       SCI_PHY_SUB_AWAIT_IAF_UF);
722*4882a593Smuzhiyun 			break;
723*4882a593Smuzhiyun 		case SCU_EVENT_SAS_30:
724*4882a593Smuzhiyun 		case SCU_EVENT_SAS_30_SSC:
725*4882a593Smuzhiyun 			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
726*4882a593Smuzhiyun 						       SCI_PHY_SUB_AWAIT_IAF_UF);
727*4882a593Smuzhiyun 			break;
728*4882a593Smuzhiyun 		case SCU_EVENT_SAS_60:
729*4882a593Smuzhiyun 		case SCU_EVENT_SAS_60_SSC:
730*4882a593Smuzhiyun 			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
731*4882a593Smuzhiyun 						       SCI_PHY_SUB_AWAIT_IAF_UF);
732*4882a593Smuzhiyun 			break;
733*4882a593Smuzhiyun 		case SCU_EVENT_SATA_SPINUP_HOLD:
734*4882a593Smuzhiyun 			/*
735*4882a593Smuzhiyun 			 * We were doing SAS PHY link training and received a SATA PHY event
736*4882a593Smuzhiyun 			 * continue OOB/SN as if this were a SATA PHY */
737*4882a593Smuzhiyun 			sci_phy_start_sata_link_training(iphy);
738*4882a593Smuzhiyun 			break;
739*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
740*4882a593Smuzhiyun 			/* Change the timeout value to default */
741*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
744*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
745*4882a593Smuzhiyun 			break;
746*4882a593Smuzhiyun 		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
747*4882a593Smuzhiyun 		       /* Extend the timeout value */
748*4882a593Smuzhiyun 		       scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 		       /* Start the oob/sn state machine over again */
751*4882a593Smuzhiyun 		       sci_change_state(&iphy->sm, SCI_PHY_STARTING);
752*4882a593Smuzhiyun 		       break;
753*4882a593Smuzhiyun 		default:
754*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
755*4882a593Smuzhiyun 			return SCI_FAILURE;
756*4882a593Smuzhiyun 			break;
757*4882a593Smuzhiyun 		}
758*4882a593Smuzhiyun 		return SCI_SUCCESS;
759*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_IAF_UF:
760*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
761*4882a593Smuzhiyun 		case SCU_EVENT_SAS_PHY_DETECTED:
762*4882a593Smuzhiyun 			/* Backup the state machine */
763*4882a593Smuzhiyun 			sci_phy_start_sas_link_training(iphy);
764*4882a593Smuzhiyun 			break;
765*4882a593Smuzhiyun 		case SCU_EVENT_SATA_SPINUP_HOLD:
766*4882a593Smuzhiyun 			/* We were doing SAS PHY link training and received a
767*4882a593Smuzhiyun 			 * SATA PHY event continue OOB/SN as if this were a
768*4882a593Smuzhiyun 			 * SATA PHY
769*4882a593Smuzhiyun 			 */
770*4882a593Smuzhiyun 			sci_phy_start_sata_link_training(iphy);
771*4882a593Smuzhiyun 			break;
772*4882a593Smuzhiyun 		case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
773*4882a593Smuzhiyun 			/* Extend the timeout value */
774*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_EXTENDED);
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 			/* Start the oob/sn state machine over again */
777*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
778*4882a593Smuzhiyun 			break;
779*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
780*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
781*4882a593Smuzhiyun 			fallthrough;
782*4882a593Smuzhiyun 		case SCU_EVENT_HARD_RESET_RECEIVED:
783*4882a593Smuzhiyun 			/* Start the oob/sn state machine over again */
784*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
785*4882a593Smuzhiyun 			break;
786*4882a593Smuzhiyun 		default:
787*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
788*4882a593Smuzhiyun 			return SCI_FAILURE;
789*4882a593Smuzhiyun 		}
790*4882a593Smuzhiyun 		return SCI_SUCCESS;
791*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
792*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
793*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
794*4882a593Smuzhiyun 			/* Change the timeout value to default */
795*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
798*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
799*4882a593Smuzhiyun 			break;
800*4882a593Smuzhiyun 		default:
801*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
802*4882a593Smuzhiyun 			return SCI_FAILURE;
803*4882a593Smuzhiyun 		}
804*4882a593Smuzhiyun 		return SCI_SUCCESS;
805*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
806*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
807*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
808*4882a593Smuzhiyun 			/* Change the timeout value to default */
809*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
812*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
813*4882a593Smuzhiyun 			break;
814*4882a593Smuzhiyun 		case SCU_EVENT_SATA_SPINUP_HOLD:
815*4882a593Smuzhiyun 			/* These events are received every 10ms and are
816*4882a593Smuzhiyun 			 * expected while in this state
817*4882a593Smuzhiyun 			 */
818*4882a593Smuzhiyun 			break;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 		case SCU_EVENT_SAS_PHY_DETECTED:
821*4882a593Smuzhiyun 			/* There has been a change in the phy type before OOB/SN for the
822*4882a593Smuzhiyun 			 * SATA finished start down the SAS link traning path.
823*4882a593Smuzhiyun 			 */
824*4882a593Smuzhiyun 			sci_phy_start_sas_link_training(iphy);
825*4882a593Smuzhiyun 			break;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 		default:
828*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
829*4882a593Smuzhiyun 			return SCI_FAILURE;
830*4882a593Smuzhiyun 		}
831*4882a593Smuzhiyun 		return SCI_SUCCESS;
832*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
833*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
834*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
835*4882a593Smuzhiyun 			/* Change the timeout value to default */
836*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
839*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
840*4882a593Smuzhiyun 			break;
841*4882a593Smuzhiyun 		case SCU_EVENT_SATA_SPINUP_HOLD:
842*4882a593Smuzhiyun 			/* These events might be received since we dont know how many may be in
843*4882a593Smuzhiyun 			 * the completion queue while waiting for power
844*4882a593Smuzhiyun 			 */
845*4882a593Smuzhiyun 			break;
846*4882a593Smuzhiyun 		case SCU_EVENT_SATA_PHY_DETECTED:
847*4882a593Smuzhiyun 			iphy->protocol = SAS_PROTOCOL_SATA;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 			/* We have received the SATA PHY notification change state */
850*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
851*4882a593Smuzhiyun 			break;
852*4882a593Smuzhiyun 		case SCU_EVENT_SAS_PHY_DETECTED:
853*4882a593Smuzhiyun 			/* There has been a change in the phy type before OOB/SN for the
854*4882a593Smuzhiyun 			 * SATA finished start down the SAS link traning path.
855*4882a593Smuzhiyun 			 */
856*4882a593Smuzhiyun 			sci_phy_start_sas_link_training(iphy);
857*4882a593Smuzhiyun 			break;
858*4882a593Smuzhiyun 		default:
859*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
860*4882a593Smuzhiyun 			return SCI_FAILURE;
861*4882a593Smuzhiyun 		}
862*4882a593Smuzhiyun 		return SCI_SUCCESS;
863*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
864*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
865*4882a593Smuzhiyun 		case SCU_EVENT_SATA_PHY_DETECTED:
866*4882a593Smuzhiyun 			/*
867*4882a593Smuzhiyun 			 * The hardware reports multiple SATA PHY detected events
868*4882a593Smuzhiyun 			 * ignore the extras */
869*4882a593Smuzhiyun 			break;
870*4882a593Smuzhiyun 		case SCU_EVENT_SATA_15:
871*4882a593Smuzhiyun 		case SCU_EVENT_SATA_15_SSC:
872*4882a593Smuzhiyun 			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
873*4882a593Smuzhiyun 						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
874*4882a593Smuzhiyun 			break;
875*4882a593Smuzhiyun 		case SCU_EVENT_SATA_30:
876*4882a593Smuzhiyun 		case SCU_EVENT_SATA_30_SSC:
877*4882a593Smuzhiyun 			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
878*4882a593Smuzhiyun 						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
879*4882a593Smuzhiyun 			break;
880*4882a593Smuzhiyun 		case SCU_EVENT_SATA_60:
881*4882a593Smuzhiyun 		case SCU_EVENT_SATA_60_SSC:
882*4882a593Smuzhiyun 			sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
883*4882a593Smuzhiyun 						       SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
884*4882a593Smuzhiyun 			break;
885*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
886*4882a593Smuzhiyun 			/* Change the timeout value to default */
887*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
890*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
891*4882a593Smuzhiyun 			break;
892*4882a593Smuzhiyun 		case SCU_EVENT_SAS_PHY_DETECTED:
893*4882a593Smuzhiyun 			/*
894*4882a593Smuzhiyun 			 * There has been a change in the phy type before OOB/SN for the
895*4882a593Smuzhiyun 			 * SATA finished start down the SAS link traning path. */
896*4882a593Smuzhiyun 			sci_phy_start_sas_link_training(iphy);
897*4882a593Smuzhiyun 			break;
898*4882a593Smuzhiyun 		default:
899*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
900*4882a593Smuzhiyun 			return SCI_FAILURE;
901*4882a593Smuzhiyun 		}
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 		return SCI_SUCCESS;
904*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
905*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
906*4882a593Smuzhiyun 		case SCU_EVENT_SATA_PHY_DETECTED:
907*4882a593Smuzhiyun 			/* Backup the state machine */
908*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
909*4882a593Smuzhiyun 			break;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
912*4882a593Smuzhiyun 			/* Change the timeout value to default */
913*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
916*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
917*4882a593Smuzhiyun 			break;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 		default:
920*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
921*4882a593Smuzhiyun 			return SCI_FAILURE;
922*4882a593Smuzhiyun 		}
923*4882a593Smuzhiyun 		return SCI_SUCCESS;
924*4882a593Smuzhiyun 	case SCI_PHY_READY:
925*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
926*4882a593Smuzhiyun 		case SCU_EVENT_LINK_FAILURE:
927*4882a593Smuzhiyun 			/* Set default timeout */
928*4882a593Smuzhiyun 			scu_link_layer_set_txcomsas_timeout(iphy, SCU_SAS_LINK_LAYER_TXCOMSAS_NEGTIME_DEFAULT);
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
931*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
932*4882a593Smuzhiyun 			break;
933*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_CHANGE:
934*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_SES:
935*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_RESERVED0:
936*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_RESERVED1:
937*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_EXPANDER:
938*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_AEN:
939*4882a593Smuzhiyun 			/* Broadcast change received. Notify the port. */
940*4882a593Smuzhiyun 			if (phy_get_non_dummy_port(iphy) != NULL)
941*4882a593Smuzhiyun 				sci_port_broadcast_change_received(iphy->owning_port, iphy);
942*4882a593Smuzhiyun 			else
943*4882a593Smuzhiyun 				iphy->bcn_received_while_port_unassigned = true;
944*4882a593Smuzhiyun 			break;
945*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_RESERVED3:
946*4882a593Smuzhiyun 		case SCU_EVENT_BROADCAST_RESERVED4:
947*4882a593Smuzhiyun 		default:
948*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
949*4882a593Smuzhiyun 			return SCI_FAILURE_INVALID_STATE;
950*4882a593Smuzhiyun 		}
951*4882a593Smuzhiyun 		return SCI_SUCCESS;
952*4882a593Smuzhiyun 	case SCI_PHY_RESETTING:
953*4882a593Smuzhiyun 		switch (scu_get_event_code(event_code)) {
954*4882a593Smuzhiyun 		case SCU_EVENT_HARD_RESET_TRANSMITTED:
955*4882a593Smuzhiyun 			/* Link failure change state back to the starting state */
956*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_STARTING);
957*4882a593Smuzhiyun 			break;
958*4882a593Smuzhiyun 		default:
959*4882a593Smuzhiyun 			phy_event_warn(iphy, state, event_code);
960*4882a593Smuzhiyun 			return SCI_FAILURE_INVALID_STATE;
961*4882a593Smuzhiyun 			break;
962*4882a593Smuzhiyun 		}
963*4882a593Smuzhiyun 		return SCI_SUCCESS;
964*4882a593Smuzhiyun 	default:
965*4882a593Smuzhiyun 		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
966*4882a593Smuzhiyun 			__func__, phy_state_name(state));
967*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun 
sci_phy_frame_handler(struct isci_phy * iphy,u32 frame_index)971*4882a593Smuzhiyun enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun 	enum sci_phy_states state = iphy->sm.current_state_id;
974*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
975*4882a593Smuzhiyun 	enum sci_status result;
976*4882a593Smuzhiyun 	unsigned long flags;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	switch (state) {
979*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_IAF_UF: {
980*4882a593Smuzhiyun 		u32 *frame_words;
981*4882a593Smuzhiyun 		struct sas_identify_frame iaf;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 		result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
984*4882a593Smuzhiyun 								  frame_index,
985*4882a593Smuzhiyun 								  (void **)&frame_words);
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 		if (result != SCI_SUCCESS)
988*4882a593Smuzhiyun 			return result;
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 		sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
991*4882a593Smuzhiyun 		if (iaf.frame_type == 0) {
992*4882a593Smuzhiyun 			u32 state;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
995*4882a593Smuzhiyun 			memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
996*4882a593Smuzhiyun 			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
997*4882a593Smuzhiyun 			if (iaf.smp_tport) {
998*4882a593Smuzhiyun 				/* We got the IAF for an expander PHY go to the final
999*4882a593Smuzhiyun 				 * state since there are no power requirements for
1000*4882a593Smuzhiyun 				 * expander phys.
1001*4882a593Smuzhiyun 				 */
1002*4882a593Smuzhiyun 				state = SCI_PHY_SUB_FINAL;
1003*4882a593Smuzhiyun 			} else {
1004*4882a593Smuzhiyun 				/* We got the IAF we can now go to the await spinup
1005*4882a593Smuzhiyun 				 * semaphore state
1006*4882a593Smuzhiyun 				 */
1007*4882a593Smuzhiyun 				state = SCI_PHY_SUB_AWAIT_SAS_POWER;
1008*4882a593Smuzhiyun 			}
1009*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, state);
1010*4882a593Smuzhiyun 			result = SCI_SUCCESS;
1011*4882a593Smuzhiyun 		} else
1012*4882a593Smuzhiyun 			dev_warn(sciphy_to_dev(iphy),
1013*4882a593Smuzhiyun 				"%s: PHY starting substate machine received "
1014*4882a593Smuzhiyun 				"unexpected frame id %x\n",
1015*4882a593Smuzhiyun 				__func__, frame_index);
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 		sci_controller_release_frame(ihost, frame_index);
1018*4882a593Smuzhiyun 		return result;
1019*4882a593Smuzhiyun 	}
1020*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
1021*4882a593Smuzhiyun 		struct dev_to_host_fis *frame_header;
1022*4882a593Smuzhiyun 		u32 *fis_frame_data;
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 		result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1025*4882a593Smuzhiyun 								  frame_index,
1026*4882a593Smuzhiyun 								  (void **)&frame_header);
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 		if (result != SCI_SUCCESS)
1029*4882a593Smuzhiyun 			return result;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 		if ((frame_header->fis_type == FIS_REGD2H) &&
1032*4882a593Smuzhiyun 		    !(frame_header->status & ATA_BUSY)) {
1033*4882a593Smuzhiyun 			sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1034*4882a593Smuzhiyun 								 frame_index,
1035*4882a593Smuzhiyun 								 (void **)&fis_frame_data);
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 			spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
1038*4882a593Smuzhiyun 			sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
1039*4882a593Smuzhiyun 							  frame_header,
1040*4882a593Smuzhiyun 							  fis_frame_data);
1041*4882a593Smuzhiyun 			spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 			/* got IAF we can now go to the await spinup semaphore state */
1044*4882a593Smuzhiyun 			sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 			result = SCI_SUCCESS;
1047*4882a593Smuzhiyun 		} else
1048*4882a593Smuzhiyun 			dev_warn(sciphy_to_dev(iphy),
1049*4882a593Smuzhiyun 				 "%s: PHY starting substate machine received "
1050*4882a593Smuzhiyun 				 "unexpected frame id %x\n",
1051*4882a593Smuzhiyun 				 __func__, frame_index);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 		/* Regardless of the result we are done with this frame with it */
1054*4882a593Smuzhiyun 		sci_controller_release_frame(ihost, frame_index);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 		return result;
1057*4882a593Smuzhiyun 	}
1058*4882a593Smuzhiyun 	default:
1059*4882a593Smuzhiyun 		dev_dbg(sciphy_to_dev(iphy), "%s: in wrong state: %s\n",
1060*4882a593Smuzhiyun 			__func__, phy_state_name(state));
1061*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun 
sci_phy_starting_initial_substate_enter(struct sci_base_state_machine * sm)1066*4882a593Smuzhiyun static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	/* This is just an temporary state go off to the starting state */
1071*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun 
sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine * sm)1074*4882a593Smuzhiyun static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1077*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	sci_controller_power_control_queue_insert(ihost, iphy);
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun 
sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine * sm)1082*4882a593Smuzhiyun static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1085*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	sci_controller_power_control_queue_remove(ihost, iphy);
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun 
sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine * sm)1090*4882a593Smuzhiyun static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
1091*4882a593Smuzhiyun {
1092*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1093*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	sci_controller_power_control_queue_insert(ihost, iphy);
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun 
sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine * sm)1098*4882a593Smuzhiyun static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
1099*4882a593Smuzhiyun {
1100*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1101*4882a593Smuzhiyun 	struct isci_host *ihost = iphy->owning_port->owning_controller;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	sci_controller_power_control_queue_remove(ihost, iphy);
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine * sm)1106*4882a593Smuzhiyun static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
1107*4882a593Smuzhiyun {
1108*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun 
sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine * sm)1113*4882a593Smuzhiyun static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	sci_del_timer(&iphy->sata_timer);
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine * sm)1120*4882a593Smuzhiyun static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun 
sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine * sm)1127*4882a593Smuzhiyun static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	sci_del_timer(&iphy->sata_timer);
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun 
sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine * sm)1134*4882a593Smuzhiyun static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
1135*4882a593Smuzhiyun {
1136*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	if (sci_port_link_detected(iphy->owning_port, iphy)) {
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 		/*
1141*4882a593Smuzhiyun 		 * Clear the PE suspend condition so we can actually
1142*4882a593Smuzhiyun 		 * receive SIG FIS
1143*4882a593Smuzhiyun 		 * The hardware will not respond to the XRDY until the PE
1144*4882a593Smuzhiyun 		 * suspend condition is cleared.
1145*4882a593Smuzhiyun 		 */
1146*4882a593Smuzhiyun 		sci_phy_resume(iphy);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 		sci_mod_timer(&iphy->sata_timer,
1149*4882a593Smuzhiyun 			      SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
1150*4882a593Smuzhiyun 	} else
1151*4882a593Smuzhiyun 		iphy->is_in_link_training = false;
1152*4882a593Smuzhiyun }
1153*4882a593Smuzhiyun 
sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine * sm)1154*4882a593Smuzhiyun static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	sci_del_timer(&iphy->sata_timer);
1159*4882a593Smuzhiyun }
1160*4882a593Smuzhiyun 
sci_phy_starting_final_substate_enter(struct sci_base_state_machine * sm)1161*4882a593Smuzhiyun static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
1162*4882a593Smuzhiyun {
1163*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	/* State machine has run to completion so exit out and change
1166*4882a593Smuzhiyun 	 * the base state machine to the ready state
1167*4882a593Smuzhiyun 	 */
1168*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_READY);
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /**
1172*4882a593Smuzhiyun  *
1173*4882a593Smuzhiyun  * @sci_phy: This is the struct isci_phy object to stop.
1174*4882a593Smuzhiyun  *
1175*4882a593Smuzhiyun  * This method will stop the struct isci_phy object. This does not reset the
1176*4882a593Smuzhiyun  * protocol engine it just suspends it and places it in a state where it will
1177*4882a593Smuzhiyun  * not cause the end device to power up. none
1178*4882a593Smuzhiyun  */
scu_link_layer_stop_protocol_engine(struct isci_phy * iphy)1179*4882a593Smuzhiyun static void scu_link_layer_stop_protocol_engine(
1180*4882a593Smuzhiyun 	struct isci_phy *iphy)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun 	u32 scu_sas_pcfg_value;
1183*4882a593Smuzhiyun 	u32 enable_spinup_value;
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	/* Suspend the protocol engine and place it in a sata spinup hold state */
1186*4882a593Smuzhiyun 	scu_sas_pcfg_value =
1187*4882a593Smuzhiyun 		readl(&iphy->link_layer_registers->phy_configuration);
1188*4882a593Smuzhiyun 	scu_sas_pcfg_value |=
1189*4882a593Smuzhiyun 		(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1190*4882a593Smuzhiyun 		 SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
1191*4882a593Smuzhiyun 		 SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
1192*4882a593Smuzhiyun 	writel(scu_sas_pcfg_value,
1193*4882a593Smuzhiyun 	       &iphy->link_layer_registers->phy_configuration);
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	/* Disable the notify enable spinup primitives */
1196*4882a593Smuzhiyun 	enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
1197*4882a593Smuzhiyun 	enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
1198*4882a593Smuzhiyun 	writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
1199*4882a593Smuzhiyun }
1200*4882a593Smuzhiyun 
scu_link_layer_start_oob(struct isci_phy * iphy)1201*4882a593Smuzhiyun static void scu_link_layer_start_oob(struct isci_phy *iphy)
1202*4882a593Smuzhiyun {
1203*4882a593Smuzhiyun 	struct scu_link_layer_registers __iomem *ll = iphy->link_layer_registers;
1204*4882a593Smuzhiyun 	u32 val;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	/** Reset OOB sequence - start */
1207*4882a593Smuzhiyun 	val = readl(&ll->phy_configuration);
1208*4882a593Smuzhiyun 	val &= ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1209*4882a593Smuzhiyun 		 SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE) |
1210*4882a593Smuzhiyun 		 SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
1211*4882a593Smuzhiyun 	writel(val, &ll->phy_configuration);
1212*4882a593Smuzhiyun 	readl(&ll->phy_configuration); /* flush */
1213*4882a593Smuzhiyun 	/** Reset OOB sequence - end */
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	/** Start OOB sequence - start */
1216*4882a593Smuzhiyun 	val = readl(&ll->phy_configuration);
1217*4882a593Smuzhiyun 	val |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1218*4882a593Smuzhiyun 	writel(val, &ll->phy_configuration);
1219*4882a593Smuzhiyun 	readl(&ll->phy_configuration); /* flush */
1220*4882a593Smuzhiyun 	/** Start OOB sequence - end */
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun /**
1224*4882a593Smuzhiyun  *
1225*4882a593Smuzhiyun  *
1226*4882a593Smuzhiyun  * This method will transmit a hard reset request on the specified phy. The SCU
1227*4882a593Smuzhiyun  * hardware requires that we reset the OOB state machine and set the hard reset
1228*4882a593Smuzhiyun  * bit in the phy configuration register. We then must start OOB over with the
1229*4882a593Smuzhiyun  * hard reset bit set.
1230*4882a593Smuzhiyun  */
scu_link_layer_tx_hard_reset(struct isci_phy * iphy)1231*4882a593Smuzhiyun static void scu_link_layer_tx_hard_reset(
1232*4882a593Smuzhiyun 	struct isci_phy *iphy)
1233*4882a593Smuzhiyun {
1234*4882a593Smuzhiyun 	u32 phy_configuration_value;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	/*
1237*4882a593Smuzhiyun 	 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
1238*4882a593Smuzhiyun 	 * to the starting state. */
1239*4882a593Smuzhiyun 	phy_configuration_value =
1240*4882a593Smuzhiyun 		readl(&iphy->link_layer_registers->phy_configuration);
1241*4882a593Smuzhiyun 	phy_configuration_value &= ~(SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
1242*4882a593Smuzhiyun 	phy_configuration_value |=
1243*4882a593Smuzhiyun 		(SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
1244*4882a593Smuzhiyun 		 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
1245*4882a593Smuzhiyun 	writel(phy_configuration_value,
1246*4882a593Smuzhiyun 	       &iphy->link_layer_registers->phy_configuration);
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 	/* Now take the OOB state machine out of reset */
1249*4882a593Smuzhiyun 	phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1250*4882a593Smuzhiyun 	phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
1251*4882a593Smuzhiyun 	writel(phy_configuration_value,
1252*4882a593Smuzhiyun 	       &iphy->link_layer_registers->phy_configuration);
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun 
sci_phy_stopped_state_enter(struct sci_base_state_machine * sm)1255*4882a593Smuzhiyun static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1258*4882a593Smuzhiyun 	struct isci_port *iport = iphy->owning_port;
1259*4882a593Smuzhiyun 	struct isci_host *ihost = iport->owning_controller;
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 	/*
1262*4882a593Smuzhiyun 	 * @todo We need to get to the controller to place this PE in a
1263*4882a593Smuzhiyun 	 * reset state
1264*4882a593Smuzhiyun 	 */
1265*4882a593Smuzhiyun 	sci_del_timer(&iphy->sata_timer);
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	scu_link_layer_stop_protocol_engine(iphy);
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
1270*4882a593Smuzhiyun 		sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun 
sci_phy_starting_state_enter(struct sci_base_state_machine * sm)1273*4882a593Smuzhiyun static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1276*4882a593Smuzhiyun 	struct isci_port *iport = iphy->owning_port;
1277*4882a593Smuzhiyun 	struct isci_host *ihost = iport->owning_controller;
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	scu_link_layer_stop_protocol_engine(iphy);
1280*4882a593Smuzhiyun 	scu_link_layer_start_oob(iphy);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	/* We don't know what kind of phy we are going to be just yet */
1283*4882a593Smuzhiyun 	iphy->protocol = SAS_PROTOCOL_NONE;
1284*4882a593Smuzhiyun 	iphy->bcn_received_while_port_unassigned = false;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	if (iphy->sm.previous_state_id == SCI_PHY_READY)
1287*4882a593Smuzhiyun 		sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun 
sci_phy_ready_state_enter(struct sci_base_state_machine * sm)1292*4882a593Smuzhiyun static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
1293*4882a593Smuzhiyun {
1294*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1295*4882a593Smuzhiyun 	struct isci_port *iport = iphy->owning_port;
1296*4882a593Smuzhiyun 	struct isci_host *ihost = iport->owning_controller;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun 
sci_phy_ready_state_exit(struct sci_base_state_machine * sm)1301*4882a593Smuzhiyun static void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
1302*4882a593Smuzhiyun {
1303*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	sci_phy_suspend(iphy);
1306*4882a593Smuzhiyun }
1307*4882a593Smuzhiyun 
sci_phy_resetting_state_enter(struct sci_base_state_machine * sm)1308*4882a593Smuzhiyun static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
1309*4882a593Smuzhiyun {
1310*4882a593Smuzhiyun 	struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	/* The phy is being reset, therefore deactivate it from the port.  In
1313*4882a593Smuzhiyun 	 * the resetting state we don't notify the user regarding link up and
1314*4882a593Smuzhiyun 	 * link down notifications
1315*4882a593Smuzhiyun 	 */
1316*4882a593Smuzhiyun 	sci_port_deactivate_phy(iphy->owning_port, iphy, false);
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	if (iphy->protocol == SAS_PROTOCOL_SSP) {
1319*4882a593Smuzhiyun 		scu_link_layer_tx_hard_reset(iphy);
1320*4882a593Smuzhiyun 	} else {
1321*4882a593Smuzhiyun 		/* The SCU does not need to have a discrete reset state so
1322*4882a593Smuzhiyun 		 * just go back to the starting state.
1323*4882a593Smuzhiyun 		 */
1324*4882a593Smuzhiyun 		sci_change_state(&iphy->sm, SCI_PHY_STARTING);
1325*4882a593Smuzhiyun 	}
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun static const struct sci_base_state sci_phy_state_table[] = {
1329*4882a593Smuzhiyun 	[SCI_PHY_INITIAL] = { },
1330*4882a593Smuzhiyun 	[SCI_PHY_STOPPED] = {
1331*4882a593Smuzhiyun 		.enter_state = sci_phy_stopped_state_enter,
1332*4882a593Smuzhiyun 	},
1333*4882a593Smuzhiyun 	[SCI_PHY_STARTING] = {
1334*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_state_enter,
1335*4882a593Smuzhiyun 	},
1336*4882a593Smuzhiyun 	[SCI_PHY_SUB_INITIAL] = {
1337*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_initial_substate_enter,
1338*4882a593Smuzhiyun 	},
1339*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
1340*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
1341*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_IAF_UF] = { },
1342*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_SAS_POWER] = {
1343*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_await_sas_power_substate_enter,
1344*4882a593Smuzhiyun 		.exit_state  = sci_phy_starting_await_sas_power_substate_exit,
1345*4882a593Smuzhiyun 	},
1346*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_SATA_POWER] = {
1347*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_await_sata_power_substate_enter,
1348*4882a593Smuzhiyun 		.exit_state  = sci_phy_starting_await_sata_power_substate_exit
1349*4882a593Smuzhiyun 	},
1350*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
1351*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_await_sata_phy_substate_enter,
1352*4882a593Smuzhiyun 		.exit_state  = sci_phy_starting_await_sata_phy_substate_exit
1353*4882a593Smuzhiyun 	},
1354*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
1355*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_await_sata_speed_substate_enter,
1356*4882a593Smuzhiyun 		.exit_state  = sci_phy_starting_await_sata_speed_substate_exit
1357*4882a593Smuzhiyun 	},
1358*4882a593Smuzhiyun 	[SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
1359*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
1360*4882a593Smuzhiyun 		.exit_state  = sci_phy_starting_await_sig_fis_uf_substate_exit
1361*4882a593Smuzhiyun 	},
1362*4882a593Smuzhiyun 	[SCI_PHY_SUB_FINAL] = {
1363*4882a593Smuzhiyun 		.enter_state = sci_phy_starting_final_substate_enter,
1364*4882a593Smuzhiyun 	},
1365*4882a593Smuzhiyun 	[SCI_PHY_READY] = {
1366*4882a593Smuzhiyun 		.enter_state = sci_phy_ready_state_enter,
1367*4882a593Smuzhiyun 		.exit_state = sci_phy_ready_state_exit,
1368*4882a593Smuzhiyun 	},
1369*4882a593Smuzhiyun 	[SCI_PHY_RESETTING] = {
1370*4882a593Smuzhiyun 		.enter_state = sci_phy_resetting_state_enter,
1371*4882a593Smuzhiyun 	},
1372*4882a593Smuzhiyun 	[SCI_PHY_FINAL] = { },
1373*4882a593Smuzhiyun };
1374*4882a593Smuzhiyun 
sci_phy_construct(struct isci_phy * iphy,struct isci_port * iport,u8 phy_index)1375*4882a593Smuzhiyun void sci_phy_construct(struct isci_phy *iphy,
1376*4882a593Smuzhiyun 			    struct isci_port *iport, u8 phy_index)
1377*4882a593Smuzhiyun {
1378*4882a593Smuzhiyun 	sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	/* Copy the rest of the input data to our locals */
1381*4882a593Smuzhiyun 	iphy->owning_port = iport;
1382*4882a593Smuzhiyun 	iphy->phy_index = phy_index;
1383*4882a593Smuzhiyun 	iphy->bcn_received_while_port_unassigned = false;
1384*4882a593Smuzhiyun 	iphy->protocol = SAS_PROTOCOL_NONE;
1385*4882a593Smuzhiyun 	iphy->link_layer_registers = NULL;
1386*4882a593Smuzhiyun 	iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	/* Create the SIGNATURE FIS Timeout timer for this phy */
1389*4882a593Smuzhiyun 	sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun 
isci_phy_init(struct isci_phy * iphy,struct isci_host * ihost,int index)1392*4882a593Smuzhiyun void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun 	struct sci_oem_params *oem = &ihost->oem_parameters;
1395*4882a593Smuzhiyun 	u64 sci_sas_addr;
1396*4882a593Smuzhiyun 	__be64 sas_addr;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	sci_sas_addr = oem->phys[index].sas_address.high;
1399*4882a593Smuzhiyun 	sci_sas_addr <<= 32;
1400*4882a593Smuzhiyun 	sci_sas_addr |= oem->phys[index].sas_address.low;
1401*4882a593Smuzhiyun 	sas_addr = cpu_to_be64(sci_sas_addr);
1402*4882a593Smuzhiyun 	memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	iphy->sas_phy.enabled = 0;
1405*4882a593Smuzhiyun 	iphy->sas_phy.id = index;
1406*4882a593Smuzhiyun 	iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
1407*4882a593Smuzhiyun 	iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
1408*4882a593Smuzhiyun 	iphy->sas_phy.ha = &ihost->sas_ha;
1409*4882a593Smuzhiyun 	iphy->sas_phy.lldd_phy = iphy;
1410*4882a593Smuzhiyun 	iphy->sas_phy.enabled = 1;
1411*4882a593Smuzhiyun 	iphy->sas_phy.class = SAS;
1412*4882a593Smuzhiyun 	iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
1413*4882a593Smuzhiyun 	iphy->sas_phy.tproto = 0;
1414*4882a593Smuzhiyun 	iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
1415*4882a593Smuzhiyun 	iphy->sas_phy.role = PHY_ROLE_INITIATOR;
1416*4882a593Smuzhiyun 	iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
1417*4882a593Smuzhiyun 	iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
1418*4882a593Smuzhiyun 	memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun /**
1423*4882a593Smuzhiyun  * isci_phy_control() - This function is one of the SAS Domain Template
1424*4882a593Smuzhiyun  *    functions. This is a phy management function.
1425*4882a593Smuzhiyun  * @phy: This parameter specifies the sphy being controlled.
1426*4882a593Smuzhiyun  * @func: This parameter specifies the phy control function being invoked.
1427*4882a593Smuzhiyun  * @buf: This parameter is specific to the phy function being invoked.
1428*4882a593Smuzhiyun  *
1429*4882a593Smuzhiyun  * status, zero indicates success.
1430*4882a593Smuzhiyun  */
isci_phy_control(struct asd_sas_phy * sas_phy,enum phy_func func,void * buf)1431*4882a593Smuzhiyun int isci_phy_control(struct asd_sas_phy *sas_phy,
1432*4882a593Smuzhiyun 		     enum phy_func func,
1433*4882a593Smuzhiyun 		     void *buf)
1434*4882a593Smuzhiyun {
1435*4882a593Smuzhiyun 	int ret = 0;
1436*4882a593Smuzhiyun 	struct isci_phy *iphy = sas_phy->lldd_phy;
1437*4882a593Smuzhiyun 	struct asd_sas_port *port = sas_phy->port;
1438*4882a593Smuzhiyun 	struct isci_host *ihost = sas_phy->ha->lldd_ha;
1439*4882a593Smuzhiyun 	unsigned long flags;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	dev_dbg(&ihost->pdev->dev,
1442*4882a593Smuzhiyun 		"%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
1443*4882a593Smuzhiyun 		__func__, sas_phy, func, buf, iphy, port);
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	switch (func) {
1446*4882a593Smuzhiyun 	case PHY_FUNC_DISABLE:
1447*4882a593Smuzhiyun 		spin_lock_irqsave(&ihost->scic_lock, flags);
1448*4882a593Smuzhiyun 		scu_link_layer_start_oob(iphy);
1449*4882a593Smuzhiyun 		sci_phy_stop(iphy);
1450*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
1451*4882a593Smuzhiyun 		break;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	case PHY_FUNC_LINK_RESET:
1454*4882a593Smuzhiyun 		spin_lock_irqsave(&ihost->scic_lock, flags);
1455*4882a593Smuzhiyun 		scu_link_layer_start_oob(iphy);
1456*4882a593Smuzhiyun 		sci_phy_stop(iphy);
1457*4882a593Smuzhiyun 		sci_phy_start(iphy);
1458*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
1459*4882a593Smuzhiyun 		break;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	case PHY_FUNC_HARD_RESET:
1462*4882a593Smuzhiyun 		if (!port)
1463*4882a593Smuzhiyun 			return -ENODEV;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 		ret = isci_port_perform_hard_reset(ihost, port->lldd_port, iphy);
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 		break;
1468*4882a593Smuzhiyun 	case PHY_FUNC_GET_EVENTS: {
1469*4882a593Smuzhiyun 		struct scu_link_layer_registers __iomem *r;
1470*4882a593Smuzhiyun 		struct sas_phy *phy = sas_phy->phy;
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 		r = iphy->link_layer_registers;
1473*4882a593Smuzhiyun 		phy->running_disparity_error_count = readl(&r->running_disparity_error_count);
1474*4882a593Smuzhiyun 		phy->loss_of_dword_sync_count = readl(&r->loss_of_sync_error_count);
1475*4882a593Smuzhiyun 		phy->phy_reset_problem_count = readl(&r->phy_reset_problem_count);
1476*4882a593Smuzhiyun 		phy->invalid_dword_count = readl(&r->invalid_dword_counter);
1477*4882a593Smuzhiyun 		break;
1478*4882a593Smuzhiyun 	}
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	default:
1481*4882a593Smuzhiyun 		dev_dbg(&ihost->pdev->dev,
1482*4882a593Smuzhiyun 			   "%s: phy %p; func %d NOT IMPLEMENTED!\n",
1483*4882a593Smuzhiyun 			   __func__, sas_phy, func);
1484*4882a593Smuzhiyun 		ret = -ENOSYS;
1485*4882a593Smuzhiyun 		break;
1486*4882a593Smuzhiyun 	}
1487*4882a593Smuzhiyun 	return ret;
1488*4882a593Smuzhiyun }
1489