blob: c650d3003c22eb72289c754f85aa50a91ca99ffa [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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"
Dan Williamsce2b3262011-05-08 15:49:15 -070057#include "host.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "phy.h"
Dan Williamsd35bc1b2011-05-10 02:28:45 -070059#include "scu_event_codes.h"
Dan Williamse2f8db52011-05-10 02:28:46 -070060#include "probe_roms.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070061
Dan Williamsd35bc1b2011-05-10 02:28:45 -070062/* Maximum arbitration wait time in micro-seconds */
63#define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME (700)
64
Dan Williams85280952011-06-28 15:05:53 -070065enum sas_linkrate sci_phy_linkrate(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -070066{
Dan Williams85280952011-06-28 15:05:53 -070067 return iphy->max_negotiated_speed;
Dan Williamsd35bc1b2011-05-10 02:28:45 -070068}
69
Dan Williams89a73012011-06-30 19:14:33 -070070static enum sci_status
71sci_phy_transport_layer_initialization(struct isci_phy *iphy,
72 struct scu_transport_layer_registers __iomem *reg)
Dan Williamsd35bc1b2011-05-10 02:28:45 -070073{
74 u32 tl_control;
75
Dan Williams89a73012011-06-30 19:14:33 -070076 iphy->transport_layer_registers = reg;
Dan Williamsd35bc1b2011-05-10 02:28:45 -070077
78 writel(SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX,
Dan Williams85280952011-06-28 15:05:53 -070079 &iphy->transport_layer_registers->stp_rni);
Dan Williamsd35bc1b2011-05-10 02:28:45 -070080
81 /*
82 * Hardware team recommends that we enable the STP prefetch for all
83 * transports
84 */
Dan Williams85280952011-06-28 15:05:53 -070085 tl_control = readl(&iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -070086 tl_control |= SCU_TLCR_GEN_BIT(STP_WRITE_DATA_PREFETCH);
Dan Williams85280952011-06-28 15:05:53 -070087 writel(tl_control, &iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -070088
89 return SCI_SUCCESS;
90}
91
Dan Williamsd35bc1b2011-05-10 02:28:45 -070092static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -070093sci_phy_link_layer_initialization(struct isci_phy *iphy,
Dan Williams2e5da882012-01-04 01:32:34 -080094 struct scu_link_layer_registers __iomem *llr)
Dan Williamsd35bc1b2011-05-10 02:28:45 -070095{
Dan Williams89a73012011-06-30 19:14:33 -070096 struct isci_host *ihost = iphy->owning_port->owning_controller;
Dan Williams2e5da882012-01-04 01:32:34 -080097 struct sci_phy_user_params *phy_user;
98 struct sci_phy_oem_params *phy_oem;
Dan Williams85280952011-06-28 15:05:53 -070099 int phy_idx = iphy->phy_index;
Dan Williams89a73012011-06-30 19:14:33 -0700100 struct sci_phy_cap phy_cap;
Dan Williams2e5da882012-01-04 01:32:34 -0800101 u32 phy_configuration;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700102 u32 parity_check = 0;
103 u32 parity_count = 0;
104 u32 llctl, link_rate;
105 u32 clksm_value = 0;
Marcin Tomczak985af6f2011-07-29 17:16:50 -0700106 u32 sp_timeouts = 0;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700107
Dan Williams2e5da882012-01-04 01:32:34 -0800108 phy_user = &ihost->user_parameters.phys[phy_idx];
109 phy_oem = &ihost->oem_parameters.phys[phy_idx];
110 iphy->link_layer_registers = llr;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700111
112 /* Set our IDENTIFY frame data */
113 #define SCI_END_DEVICE 0x01
114
115 writel(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR) |
116 SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR) |
117 SCU_SAS_TIID_GEN_BIT(STP_INITIATOR) |
118 SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST) |
119 SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE),
Dan Williams2e5da882012-01-04 01:32:34 -0800120 &llr->transmit_identification);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700121
122 /* Write the device SAS Address */
Dan Williams2e5da882012-01-04 01:32:34 -0800123 writel(0xFEDCBA98, &llr->sas_device_name_high);
124 writel(phy_idx, &llr->sas_device_name_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700125
126 /* Write the source SAS Address */
Dan Williams2e5da882012-01-04 01:32:34 -0800127 writel(phy_oem->sas_address.high, &llr->source_sas_address_high);
128 writel(phy_oem->sas_address.low, &llr->source_sas_address_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700129
130 /* Clear and Set the PHY Identifier */
Dan Williams2e5da882012-01-04 01:32:34 -0800131 writel(0, &llr->identify_frame_phy_id);
132 writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx), &llr->identify_frame_phy_id);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700133
134 /* Change the initial state of the phy configuration register */
Dan Williams2e5da882012-01-04 01:32:34 -0800135 phy_configuration = readl(&llr->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700136
137 /* Hold OOB state machine in reset */
138 phy_configuration |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
Dan Williams2e5da882012-01-04 01:32:34 -0800139 writel(phy_configuration, &llr->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700140
141 /* Configure the SNW capabilities */
142 phy_cap.all = 0;
143 phy_cap.start = 1;
144 phy_cap.gen3_no_ssc = 1;
145 phy_cap.gen2_no_ssc = 1;
146 phy_cap.gen1_no_ssc = 1;
Dan Williams89a73012011-06-30 19:14:33 -0700147 if (ihost->oem_parameters.controller.do_enable_ssc == true) {
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700148 phy_cap.gen3_ssc = 1;
149 phy_cap.gen2_ssc = 1;
150 phy_cap.gen1_ssc = 1;
151 }
152
Dan Williams2e5da882012-01-04 01:32:34 -0800153 /* The SAS specification indicates that the phy_capabilities that
154 * are transmitted shall have an even parity. Calculate the parity.
155 */
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700156 parity_check = phy_cap.all;
157 while (parity_check != 0) {
158 if (parity_check & 0x1)
159 parity_count++;
160 parity_check >>= 1;
161 }
162
Dan Williams2e5da882012-01-04 01:32:34 -0800163 /* If parity indicates there are an odd number of bits set, then
164 * set the parity bit to 1 in the phy capabilities.
165 */
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700166 if ((parity_count % 2) != 0)
167 phy_cap.parity = 1;
168
Dan Williams2e5da882012-01-04 01:32:34 -0800169 writel(phy_cap.all, &llr->phy_capabilities);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700170
171 /* Set the enable spinup period but disable the ability to send
172 * notify enable spinup
173 */
174 writel(SCU_ENSPINUP_GEN_VAL(COUNT,
175 phy_user->notify_enable_spin_up_insertion_frequency),
Dan Williams2e5da882012-01-04 01:32:34 -0800176 &llr->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700177
178 /* Write the ALIGN Insertion Ferequency for connected phy and
179 * inpendent of connected state
180 */
181 clksm_value = SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(CONNECTED,
182 phy_user->in_connection_align_insertion_frequency);
183
184 clksm_value |= SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(GENERAL,
185 phy_user->align_insertion_frequency);
186
Dan Williams2e5da882012-01-04 01:32:34 -0800187 writel(clksm_value, &llr->clock_skew_management);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700188
Jeff Skirvinafd13a12012-01-04 01:32:39 -0800189 if (is_c0(ihost->pdev) || is_c1(ihost->pdev)) {
190 writel(0x04210400, &llr->afe_lookup_table_control);
191 writel(0x020A7C05, &llr->sas_primitive_timeout);
192 } else
193 writel(0x02108421, &llr->afe_lookup_table_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700194
195 llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
Dan Williams89a73012011-06-30 19:14:33 -0700196 (u8)ihost->user_parameters.no_outbound_task_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700197
James Bottomleya5ec7f862011-07-03 14:14:45 -0500198 switch (phy_user->max_speed_generation) {
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700199 case SCIC_SDS_PARM_GEN3_SPEED:
200 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
201 break;
202 case SCIC_SDS_PARM_GEN2_SPEED:
203 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
204 break;
205 default:
206 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
207 break;
208 }
209 llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
Dan Williams2e5da882012-01-04 01:32:34 -0800210 writel(llctl, &llr->link_layer_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700211
Dan Williams2e5da882012-01-04 01:32:34 -0800212 sp_timeouts = readl(&llr->sas_phy_timeouts);
Marcin Tomczak985af6f2011-07-29 17:16:50 -0700213
214 /* Clear the default 0x36 (54us) RATE_CHANGE timeout value. */
215 sp_timeouts &= ~SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0xFF);
216
217 /* Set RATE_CHANGE timeout value to 0x3B (59us). This ensures SCU can
218 * lock with 3Gb drive when SCU max rate is set to 1.5Gb.
219 */
220 sp_timeouts |= SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0x3B);
221
Dan Williams2e5da882012-01-04 01:32:34 -0800222 writel(sp_timeouts, &llr->sas_phy_timeouts);
Marcin Tomczak985af6f2011-07-29 17:16:50 -0700223
Dan Williamsdc00c8b2011-07-01 11:41:21 -0700224 if (is_a2(ihost->pdev)) {
Dan Williams2e5da882012-01-04 01:32:34 -0800225 /* Program the max ARB time for the PHY to 700us so we
226 * inter-operate with the PMC expander which shuts down
227 * PHYs if the expander PHY generates too many breaks.
228 * This time value will guarantee that the initiator PHY
229 * will generate the break.
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700230 */
231 writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
Dan Williams2e5da882012-01-04 01:32:34 -0800232 &llr->maximum_arbitration_wait_timer_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700233 }
234
Dan Williams2e5da882012-01-04 01:32:34 -0800235 /* Disable link layer hang detection, rely on the OS timeout for
236 * I/O timeouts.
237 */
238 writel(0, &llr->link_layer_hang_detection_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700239
240 /* We can exit the initial state to the stopped state */
Dan Williams85280952011-06-28 15:05:53 -0700241 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700242
243 return SCI_SUCCESS;
244}
245
Edmund Nadolskia628d472011-05-19 11:59:36 +0000246static void phy_sata_timeout(unsigned long data)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700247{
Edmund Nadolskia628d472011-05-19 11:59:36 +0000248 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williams85280952011-06-28 15:05:53 -0700249 struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700250 struct isci_host *ihost = iphy->owning_port->owning_controller;
Edmund Nadolskia628d472011-05-19 11:59:36 +0000251 unsigned long flags;
252
253 spin_lock_irqsave(&ihost->scic_lock, flags);
254
255 if (tmr->cancel)
256 goto done;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700257
Dan Williams85280952011-06-28 15:05:53 -0700258 dev_dbg(sciphy_to_dev(iphy),
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700259 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
260 "timeout.\n",
261 __func__,
Dan Williams85280952011-06-28 15:05:53 -0700262 iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700263
Dan Williams85280952011-06-28 15:05:53 -0700264 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Edmund Nadolskia628d472011-05-19 11:59:36 +0000265done:
266 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700267}
268
269/**
270 * This method returns the port currently containing this phy. If the phy is
271 * currently contained by the dummy port, then the phy is considered to not
272 * be part of a port.
273 * @sci_phy: This parameter specifies the phy for which to retrieve the
274 * containing port.
275 *
276 * This method returns a handle to a port that contains the supplied phy.
277 * NULL This value is returned if the phy is not part of a real
278 * port (i.e. it's contained in the dummy port). !NULL All other
279 * values indicate a handle/pointer to the port containing the phy.
280 */
Dan Williams34a99152011-07-01 02:25:15 -0700281struct isci_port *phy_get_non_dummy_port(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700282{
Dan Williams34a99152011-07-01 02:25:15 -0700283 struct isci_port *iport = iphy->owning_port;
284
285 if (iport->physical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700286 return NULL;
287
Dan Williams85280952011-06-28 15:05:53 -0700288 return iphy->owning_port;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700289}
290
291/**
292 * This method will assign a port to the phy object.
Dan Williams85280952011-06-28 15:05:53 -0700293 * @out]: iphy This parameter specifies the phy for which to assign a port
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700294 * object.
295 *
296 *
297 */
Dan Williams89a73012011-06-30 19:14:33 -0700298void sci_phy_set_port(
Dan Williams85280952011-06-28 15:05:53 -0700299 struct isci_phy *iphy,
Dan Williamsffe191c2011-06-29 13:09:25 -0700300 struct isci_port *iport)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700301{
Dan Williamsffe191c2011-06-29 13:09:25 -0700302 iphy->owning_port = iport;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700303
Dan Williams85280952011-06-28 15:05:53 -0700304 if (iphy->bcn_received_while_port_unassigned) {
305 iphy->bcn_received_while_port_unassigned = false;
Dan Williams89a73012011-06-30 19:14:33 -0700306 sci_port_broadcast_change_received(iphy->owning_port, iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700307 }
308}
309
Dan Williams89a73012011-06-30 19:14:33 -0700310enum sci_status sci_phy_initialize(struct isci_phy *iphy,
311 struct scu_transport_layer_registers __iomem *tl,
312 struct scu_link_layer_registers __iomem *ll)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700313{
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700314 /* Perfrom the initialization of the TL hardware */
Dan Williams89a73012011-06-30 19:14:33 -0700315 sci_phy_transport_layer_initialization(iphy, tl);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700316
317 /* Perofrm the initialization of the PE hardware */
Dan Williams89a73012011-06-30 19:14:33 -0700318 sci_phy_link_layer_initialization(iphy, ll);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700319
Dan Williams89a73012011-06-30 19:14:33 -0700320 /* There is nothing that needs to be done in this state just
321 * transition to the stopped state
322 */
Dan Williams85280952011-06-28 15:05:53 -0700323 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700324
325 return SCI_SUCCESS;
326}
327
328/**
329 * This method assigns the direct attached device ID for this phy.
330 *
Dan Williams85280952011-06-28 15:05:53 -0700331 * @iphy The phy for which the direct attached device id is to
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700332 * be assigned.
333 * @device_id The direct attached device ID to assign to the phy.
334 * This will either be the RNi for the device or an invalid RNi if there
335 * is no current device assigned to the phy.
336 */
Dan Williams89a73012011-06-30 19:14:33 -0700337void sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700338{
339 u32 tl_control;
340
Dan Williams85280952011-06-28 15:05:53 -0700341 writel(device_id, &iphy->transport_layer_registers->stp_rni);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700342
343 /*
344 * The read should guarantee that the first write gets posted
345 * before the next write
346 */
Dan Williams85280952011-06-28 15:05:53 -0700347 tl_control = readl(&iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700348 tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
Dan Williams85280952011-06-28 15:05:53 -0700349 writel(tl_control, &iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700350}
351
Dan Williams89a73012011-06-30 19:14:33 -0700352static void sci_phy_suspend(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700353{
354 u32 scu_sas_pcfg_value;
355
356 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700357 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700358 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
359 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700360 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700361
Dan Williams89a73012011-06-30 19:14:33 -0700362 sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700363}
364
Dan Williams89a73012011-06-30 19:14:33 -0700365void sci_phy_resume(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700366{
367 u32 scu_sas_pcfg_value;
368
369 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700370 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700371 scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
372 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700373 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700374}
375
Dan Williams89a73012011-06-30 19:14:33 -0700376void sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700377{
Dan Williams89a73012011-06-30 19:14:33 -0700378 sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
379 sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700380}
381
Dan Williams89a73012011-06-30 19:14:33 -0700382void sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700383{
384 struct sas_identify_frame *iaf;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700385
386 iaf = &iphy->frame_rcvd.iaf;
Dan Williams89a73012011-06-30 19:14:33 -0700387 memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700388}
389
Dan Williams89a73012011-06-30 19:14:33 -0700390void sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700391{
Dan Williams89a73012011-06-30 19:14:33 -0700392 proto->all = readl(&iphy->link_layer_registers->transmit_identification);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700393}
394
Dan Williams89a73012011-06-30 19:14:33 -0700395enum sci_status sci_phy_start(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700396{
Dan Williams89a73012011-06-30 19:14:33 -0700397 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams966699b2011-05-12 03:44:24 -0700398
Edmund Nadolskie3013702011-06-02 00:10:43 +0000399 if (state != SCI_PHY_STOPPED) {
Dan Williams85280952011-06-28 15:05:53 -0700400 dev_dbg(sciphy_to_dev(iphy),
Dan Williams966699b2011-05-12 03:44:24 -0700401 "%s: in wrong state: %d\n", __func__, state);
402 return SCI_FAILURE_INVALID_STATE;
403 }
404
Dan Williams85280952011-06-28 15:05:53 -0700405 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams966699b2011-05-12 03:44:24 -0700406 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700407}
408
Dan Williams89a73012011-06-30 19:14:33 -0700409enum sci_status sci_phy_stop(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700410{
Dan Williams89a73012011-06-30 19:14:33 -0700411 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams93153232011-05-12 04:01:03 -0700412
413 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000414 case SCI_PHY_SUB_INITIAL:
415 case SCI_PHY_SUB_AWAIT_OSSP_EN:
416 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
417 case SCI_PHY_SUB_AWAIT_SAS_POWER:
418 case SCI_PHY_SUB_AWAIT_SATA_POWER:
419 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
420 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
421 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
422 case SCI_PHY_SUB_FINAL:
423 case SCI_PHY_READY:
Dan Williams93153232011-05-12 04:01:03 -0700424 break;
425 default:
Dan Williams85280952011-06-28 15:05:53 -0700426 dev_dbg(sciphy_to_dev(iphy),
Dan Williams93153232011-05-12 04:01:03 -0700427 "%s: in wrong state: %d\n", __func__, state);
428 return SCI_FAILURE_INVALID_STATE;
429 }
430
Dan Williams85280952011-06-28 15:05:53 -0700431 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williams93153232011-05-12 04:01:03 -0700432 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700433}
434
Dan Williams89a73012011-06-30 19:14:33 -0700435enum sci_status sci_phy_reset(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700436{
Dan Williams89a73012011-06-30 19:14:33 -0700437 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams0cf36fa2011-05-12 04:02:07 -0700438
Edmund Nadolskie3013702011-06-02 00:10:43 +0000439 if (state != SCI_PHY_READY) {
Dan Williams85280952011-06-28 15:05:53 -0700440 dev_dbg(sciphy_to_dev(iphy),
Dan Williams0cf36fa2011-05-12 04:02:07 -0700441 "%s: in wrong state: %d\n", __func__, state);
442 return SCI_FAILURE_INVALID_STATE;
443 }
444
Dan Williams85280952011-06-28 15:05:53 -0700445 sci_change_state(&iphy->sm, SCI_PHY_RESETTING);
Dan Williams0cf36fa2011-05-12 04:02:07 -0700446 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700447}
448
Dan Williams89a73012011-06-30 19:14:33 -0700449enum sci_status sci_phy_consume_power_handler(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700450{
Dan Williams89a73012011-06-30 19:14:33 -0700451 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams5b1d4af2011-05-12 04:51:41 -0700452
453 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000454 case SCI_PHY_SUB_AWAIT_SAS_POWER: {
Dan Williams5b1d4af2011-05-12 04:51:41 -0700455 u32 enable_spinup;
456
Dan Williams85280952011-06-28 15:05:53 -0700457 enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700458 enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
Dan Williams85280952011-06-28 15:05:53 -0700459 writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700460
461 /* Change state to the final state this substate machine has run to completion */
Dan Williams85280952011-06-28 15:05:53 -0700462 sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700463
464 return SCI_SUCCESS;
465 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000466 case SCI_PHY_SUB_AWAIT_SATA_POWER: {
Dan Williams5b1d4af2011-05-12 04:51:41 -0700467 u32 scu_sas_pcfg_value;
468
469 /* Release the spinup hold state and reset the OOB state machine */
470 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700471 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700472 scu_sas_pcfg_value &=
473 ~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
474 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
475 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700476 &iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700477
478 /* Now restart the OOB operation */
479 scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
480 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
481 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700482 &iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700483
484 /* Change state to the final state this substate machine has run to completion */
Dan Williams85280952011-06-28 15:05:53 -0700485 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700486
487 return SCI_SUCCESS;
488 }
489 default:
Dan Williams85280952011-06-28 15:05:53 -0700490 dev_dbg(sciphy_to_dev(iphy),
Dan Williams5b1d4af2011-05-12 04:51:41 -0700491 "%s: in wrong state: %d\n", __func__, state);
492 return SCI_FAILURE_INVALID_STATE;
493 }
Dan Williams23506a62011-05-12 04:27:29 -0700494}
495
Dan Williams89a73012011-06-30 19:14:33 -0700496static void sci_phy_start_sas_link_training(struct isci_phy *iphy)
Dan Williams23506a62011-05-12 04:27:29 -0700497{
Dan Williams89a73012011-06-30 19:14:33 -0700498 /* continue the link training for the phy as if it were a SAS PHY
499 * instead of a SATA PHY. This is done because the completion queue had a SAS
500 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
501 */
Dan Williams23506a62011-05-12 04:27:29 -0700502 u32 phy_control;
503
Dan Williams89a73012011-06-30 19:14:33 -0700504 phy_control = readl(&iphy->link_layer_registers->phy_configuration);
Dan Williams23506a62011-05-12 04:27:29 -0700505 phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
506 writel(phy_control,
Dan Williams89a73012011-06-30 19:14:33 -0700507 &iphy->link_layer_registers->phy_configuration);
Dan Williams23506a62011-05-12 04:27:29 -0700508
Dan Williams85280952011-06-28 15:05:53 -0700509 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700510
Dan Williams85280952011-06-28 15:05:53 -0700511 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
Dan Williams23506a62011-05-12 04:27:29 -0700512}
513
Dan Williams89a73012011-06-30 19:14:33 -0700514static void sci_phy_start_sata_link_training(struct isci_phy *iphy)
Dan Williams23506a62011-05-12 04:27:29 -0700515{
Dan Williams89a73012011-06-30 19:14:33 -0700516 /* This method continues the link training for the phy as if it were a SATA PHY
517 * instead of a SAS PHY. This is done because the completion queue had a SATA
518 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
519 */
Dan Williams85280952011-06-28 15:05:53 -0700520 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
Dan Williams23506a62011-05-12 04:27:29 -0700521
Dan Williams85280952011-06-28 15:05:53 -0700522 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
Dan Williams23506a62011-05-12 04:27:29 -0700523}
524
525/**
Dan Williams89a73012011-06-30 19:14:33 -0700526 * sci_phy_complete_link_training - perform processing common to
Dan Williams23506a62011-05-12 04:27:29 -0700527 * all protocols upon completion of link training.
528 * @sci_phy: This parameter specifies the phy object for which link training
529 * has completed.
530 * @max_link_rate: This parameter specifies the maximum link rate to be
531 * associated with this phy.
532 * @next_state: This parameter specifies the next state for the phy's starting
533 * sub-state machine.
534 *
535 */
Dan Williams89a73012011-06-30 19:14:33 -0700536static void sci_phy_complete_link_training(struct isci_phy *iphy,
537 enum sas_linkrate max_link_rate,
538 u32 next_state)
Dan Williams23506a62011-05-12 04:27:29 -0700539{
Dan Williams85280952011-06-28 15:05:53 -0700540 iphy->max_negotiated_speed = max_link_rate;
Dan Williams23506a62011-05-12 04:27:29 -0700541
Dan Williams85280952011-06-28 15:05:53 -0700542 sci_change_state(&iphy->sm, next_state);
Dan Williams23506a62011-05-12 04:27:29 -0700543}
544
Dan Williams89a73012011-06-30 19:14:33 -0700545enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
Dan Williams23506a62011-05-12 04:27:29 -0700546{
Dan Williams89a73012011-06-30 19:14:33 -0700547 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams23506a62011-05-12 04:27:29 -0700548
549 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000550 case SCI_PHY_SUB_AWAIT_OSSP_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700551 switch (scu_get_event_code(event_code)) {
552 case SCU_EVENT_SAS_PHY_DETECTED:
Dan Williams89a73012011-06-30 19:14:33 -0700553 sci_phy_start_sas_link_training(iphy);
Dan Williams85280952011-06-28 15:05:53 -0700554 iphy->is_in_link_training = true;
Dan Williams23506a62011-05-12 04:27:29 -0700555 break;
556 case SCU_EVENT_SATA_SPINUP_HOLD:
Dan Williams89a73012011-06-30 19:14:33 -0700557 sci_phy_start_sata_link_training(iphy);
Dan Williams85280952011-06-28 15:05:53 -0700558 iphy->is_in_link_training = true;
Dan Williams23506a62011-05-12 04:27:29 -0700559 break;
560 default:
Dan Williams85280952011-06-28 15:05:53 -0700561 dev_dbg(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700562 "%s: PHY starting substate machine received "
563 "unexpected event_code %x\n",
564 __func__,
565 event_code);
566 return SCI_FAILURE;
567 }
568 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000569 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700570 switch (scu_get_event_code(event_code)) {
571 case SCU_EVENT_SAS_PHY_DETECTED:
572 /*
573 * Why is this being reported again by the controller?
574 * We would re-enter this state so just stay here */
575 break;
576 case SCU_EVENT_SAS_15:
577 case SCU_EVENT_SAS_15_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700578 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
579 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700580 break;
581 case SCU_EVENT_SAS_30:
582 case SCU_EVENT_SAS_30_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700583 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
584 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700585 break;
586 case SCU_EVENT_SAS_60:
587 case SCU_EVENT_SAS_60_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700588 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
589 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700590 break;
591 case SCU_EVENT_SATA_SPINUP_HOLD:
592 /*
593 * We were doing SAS PHY link training and received a SATA PHY event
594 * continue OOB/SN as if this were a SATA PHY */
Dan Williams89a73012011-06-30 19:14:33 -0700595 sci_phy_start_sata_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700596 break;
597 case SCU_EVENT_LINK_FAILURE:
598 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700599 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700600 break;
601 default:
Dan Williams85280952011-06-28 15:05:53 -0700602 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700603 "%s: PHY starting substate machine received "
604 "unexpected event_code %x\n",
605 __func__, event_code);
606
607 return SCI_FAILURE;
608 break;
609 }
610 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000611 case SCI_PHY_SUB_AWAIT_IAF_UF:
Dan Williams23506a62011-05-12 04:27:29 -0700612 switch (scu_get_event_code(event_code)) {
613 case SCU_EVENT_SAS_PHY_DETECTED:
614 /* Backup the state machine */
Dan Williams89a73012011-06-30 19:14:33 -0700615 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700616 break;
617 case SCU_EVENT_SATA_SPINUP_HOLD:
618 /* We were doing SAS PHY link training and received a
619 * SATA PHY event continue OOB/SN as if this were a
620 * SATA PHY
621 */
Dan Williams89a73012011-06-30 19:14:33 -0700622 sci_phy_start_sata_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700623 break;
624 case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
625 case SCU_EVENT_LINK_FAILURE:
626 case SCU_EVENT_HARD_RESET_RECEIVED:
627 /* Start the oob/sn state machine over again */
Dan Williams85280952011-06-28 15:05:53 -0700628 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700629 break;
630 default:
Dan Williams85280952011-06-28 15:05:53 -0700631 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700632 "%s: PHY starting substate machine received "
633 "unexpected event_code %x\n",
634 __func__, event_code);
635 return SCI_FAILURE;
636 }
637 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000638 case SCI_PHY_SUB_AWAIT_SAS_POWER:
Dan Williams23506a62011-05-12 04:27:29 -0700639 switch (scu_get_event_code(event_code)) {
640 case SCU_EVENT_LINK_FAILURE:
641 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700642 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700643 break;
644 default:
Dan Williams85280952011-06-28 15:05:53 -0700645 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700646 "%s: PHY starting substate machine received unexpected "
647 "event_code %x\n",
648 __func__,
649 event_code);
650 return SCI_FAILURE;
651 }
652 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000653 case SCI_PHY_SUB_AWAIT_SATA_POWER:
Dan Williams23506a62011-05-12 04:27:29 -0700654 switch (scu_get_event_code(event_code)) {
655 case SCU_EVENT_LINK_FAILURE:
656 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700657 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700658 break;
659 case SCU_EVENT_SATA_SPINUP_HOLD:
660 /* These events are received every 10ms and are
661 * expected while in this state
662 */
663 break;
664
665 case SCU_EVENT_SAS_PHY_DETECTED:
666 /* There has been a change in the phy type before OOB/SN for the
667 * SATA finished start down the SAS link traning path.
668 */
Dan Williams89a73012011-06-30 19:14:33 -0700669 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700670 break;
671
672 default:
Dan Williams85280952011-06-28 15:05:53 -0700673 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700674 "%s: PHY starting substate machine received "
675 "unexpected event_code %x\n",
676 __func__, event_code);
677
678 return SCI_FAILURE;
679 }
680 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000681 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700682 switch (scu_get_event_code(event_code)) {
683 case SCU_EVENT_LINK_FAILURE:
684 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700685 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700686 break;
687 case SCU_EVENT_SATA_SPINUP_HOLD:
688 /* These events might be received since we dont know how many may be in
689 * the completion queue while waiting for power
690 */
691 break;
692 case SCU_EVENT_SATA_PHY_DETECTED:
Dan Williams85280952011-06-28 15:05:53 -0700693 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
Dan Williams23506a62011-05-12 04:27:29 -0700694
695 /* We have received the SATA PHY notification change state */
Dan Williams85280952011-06-28 15:05:53 -0700696 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700697 break;
698 case SCU_EVENT_SAS_PHY_DETECTED:
699 /* There has been a change in the phy type before OOB/SN for the
700 * SATA finished start down the SAS link traning path.
701 */
Dan Williams89a73012011-06-30 19:14:33 -0700702 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700703 break;
704 default:
Dan Williams85280952011-06-28 15:05:53 -0700705 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700706 "%s: PHY starting substate machine received "
707 "unexpected event_code %x\n",
708 __func__,
709 event_code);
710
Justin P. Mattock69932482011-07-26 23:06:29 -0700711 return SCI_FAILURE;
Dan Williams23506a62011-05-12 04:27:29 -0700712 }
713 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000714 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700715 switch (scu_get_event_code(event_code)) {
716 case SCU_EVENT_SATA_PHY_DETECTED:
717 /*
718 * The hardware reports multiple SATA PHY detected events
719 * ignore the extras */
720 break;
721 case SCU_EVENT_SATA_15:
722 case SCU_EVENT_SATA_15_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700723 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
724 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700725 break;
726 case SCU_EVENT_SATA_30:
727 case SCU_EVENT_SATA_30_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700728 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
729 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700730 break;
731 case SCU_EVENT_SATA_60:
732 case SCU_EVENT_SATA_60_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700733 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
734 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700735 break;
736 case SCU_EVENT_LINK_FAILURE:
737 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700738 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700739 break;
740 case SCU_EVENT_SAS_PHY_DETECTED:
741 /*
742 * There has been a change in the phy type before OOB/SN for the
743 * SATA finished start down the SAS link traning path. */
Dan Williams89a73012011-06-30 19:14:33 -0700744 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700745 break;
746 default:
Dan Williams85280952011-06-28 15:05:53 -0700747 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700748 "%s: PHY starting substate machine received "
749 "unexpected event_code %x\n",
750 __func__, event_code);
751
752 return SCI_FAILURE;
753 }
754
755 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000756 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
Dan Williams23506a62011-05-12 04:27:29 -0700757 switch (scu_get_event_code(event_code)) {
758 case SCU_EVENT_SATA_PHY_DETECTED:
759 /* Backup the state machine */
Dan Williams85280952011-06-28 15:05:53 -0700760 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700761 break;
762
763 case SCU_EVENT_LINK_FAILURE:
764 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700765 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700766 break;
767
768 default:
Dan Williams85280952011-06-28 15:05:53 -0700769 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700770 "%s: PHY starting substate machine received "
771 "unexpected event_code %x\n",
772 __func__,
773 event_code);
774
775 return SCI_FAILURE;
776 }
777 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000778 case SCI_PHY_READY:
Dan Williams23506a62011-05-12 04:27:29 -0700779 switch (scu_get_event_code(event_code)) {
780 case SCU_EVENT_LINK_FAILURE:
781 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700782 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700783 break;
784 case SCU_EVENT_BROADCAST_CHANGE:
785 /* Broadcast change received. Notify the port. */
Dan Williams85280952011-06-28 15:05:53 -0700786 if (phy_get_non_dummy_port(iphy) != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700787 sci_port_broadcast_change_received(iphy->owning_port, iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700788 else
Dan Williams85280952011-06-28 15:05:53 -0700789 iphy->bcn_received_while_port_unassigned = true;
Dan Williams23506a62011-05-12 04:27:29 -0700790 break;
791 default:
Dan Williams85280952011-06-28 15:05:53 -0700792 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700793 "%sP SCIC PHY 0x%p ready state machine received "
794 "unexpected event_code %x\n",
Dan Williams85280952011-06-28 15:05:53 -0700795 __func__, iphy, event_code);
Dan Williams23506a62011-05-12 04:27:29 -0700796 return SCI_FAILURE_INVALID_STATE;
797 }
798 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000799 case SCI_PHY_RESETTING:
Dan Williams23506a62011-05-12 04:27:29 -0700800 switch (scu_get_event_code(event_code)) {
801 case SCU_EVENT_HARD_RESET_TRANSMITTED:
802 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700803 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700804 break;
805 default:
Dan Williams85280952011-06-28 15:05:53 -0700806 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700807 "%s: SCIC PHY 0x%p resetting state machine received "
808 "unexpected event_code %x\n",
Dan Williams85280952011-06-28 15:05:53 -0700809 __func__, iphy, event_code);
Dan Williams23506a62011-05-12 04:27:29 -0700810
811 return SCI_FAILURE_INVALID_STATE;
812 break;
813 }
814 return SCI_SUCCESS;
815 default:
Dan Williams85280952011-06-28 15:05:53 -0700816 dev_dbg(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700817 "%s: in wrong state: %d\n", __func__, state);
818 return SCI_FAILURE_INVALID_STATE;
819 }
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700820}
821
Dan Williams89a73012011-06-30 19:14:33 -0700822enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700823{
Dan Williams89a73012011-06-30 19:14:33 -0700824 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700825 struct isci_host *ihost = iphy->owning_port->owning_controller;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700826 enum sci_status result;
Dan Williams4cffe13e2011-06-23 23:44:52 -0700827 unsigned long flags;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700828
829 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000830 case SCI_PHY_SUB_AWAIT_IAF_UF: {
Dan Williamsc4441ab2011-05-12 04:17:51 -0700831 u32 *frame_words;
832 struct sas_identify_frame iaf;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700833
Dan Williams89a73012011-06-30 19:14:33 -0700834 result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
835 frame_index,
836 (void **)&frame_words);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700837
838 if (result != SCI_SUCCESS)
839 return result;
840
841 sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
842 if (iaf.frame_type == 0) {
843 u32 state;
844
Dan Williams4cffe13e2011-06-23 23:44:52 -0700845 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700846 memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
Dan Williams4cffe13e2011-06-23 23:44:52 -0700847 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700848 if (iaf.smp_tport) {
849 /* We got the IAF for an expander PHY go to the final
850 * state since there are no power requirements for
851 * expander phys.
852 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000853 state = SCI_PHY_SUB_FINAL;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700854 } else {
855 /* We got the IAF we can now go to the await spinup
856 * semaphore state
857 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000858 state = SCI_PHY_SUB_AWAIT_SAS_POWER;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700859 }
Dan Williams85280952011-06-28 15:05:53 -0700860 sci_change_state(&iphy->sm, state);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700861 result = SCI_SUCCESS;
862 } else
Dan Williams85280952011-06-28 15:05:53 -0700863 dev_warn(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700864 "%s: PHY starting substate machine received "
865 "unexpected frame id %x\n",
866 __func__, frame_index);
867
Dan Williams89a73012011-06-30 19:14:33 -0700868 sci_controller_release_frame(ihost, frame_index);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700869 return result;
870 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000871 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
Dan Williamsc4441ab2011-05-12 04:17:51 -0700872 struct dev_to_host_fis *frame_header;
873 u32 *fis_frame_data;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700874
Dan Williams34a99152011-07-01 02:25:15 -0700875 result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
876 frame_index,
877 (void **)&frame_header);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700878
879 if (result != SCI_SUCCESS)
880 return result;
881
882 if ((frame_header->fis_type == FIS_REGD2H) &&
883 !(frame_header->status & ATA_BUSY)) {
Dan Williams89a73012011-06-30 19:14:33 -0700884 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
885 frame_index,
886 (void **)&fis_frame_data);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700887
Dan Williams4cffe13e2011-06-23 23:44:52 -0700888 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -0700889 sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
890 frame_header,
891 fis_frame_data);
Dan Williams4cffe13e2011-06-23 23:44:52 -0700892 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700893
894 /* got IAF we can now go to the await spinup semaphore state */
Dan Williams85280952011-06-28 15:05:53 -0700895 sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700896
897 result = SCI_SUCCESS;
898 } else
Dan Williams85280952011-06-28 15:05:53 -0700899 dev_warn(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700900 "%s: PHY starting substate machine received "
901 "unexpected frame id %x\n",
902 __func__, frame_index);
903
904 /* Regardless of the result we are done with this frame with it */
Dan Williams89a73012011-06-30 19:14:33 -0700905 sci_controller_release_frame(ihost, frame_index);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700906
907 return result;
908 }
909 default:
Dan Williams85280952011-06-28 15:05:53 -0700910 dev_dbg(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700911 "%s: in wrong state: %d\n", __func__, state);
912 return SCI_FAILURE_INVALID_STATE;
913 }
Dan Williams5076a1a2011-06-27 14:57:03 -0700914
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700915}
916
Dan Williams89a73012011-06-30 19:14:33 -0700917static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000918{
Dan Williams85280952011-06-28 15:05:53 -0700919 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000920
Adam Gruchala4a33c522011-05-10 23:54:23 +0000921 /* This is just an temporary state go off to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700922 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000923}
924
Dan Williams89a73012011-06-30 19:14:33 -0700925static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000926{
Dan Williams85280952011-06-28 15:05:53 -0700927 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700928 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000929
Dan Williams89a73012011-06-30 19:14:33 -0700930 sci_controller_power_control_queue_insert(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000931}
932
Dan Williams89a73012011-06-30 19:14:33 -0700933static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000934{
Dan Williams85280952011-06-28 15:05:53 -0700935 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700936 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000937
Dan Williams89a73012011-06-30 19:14:33 -0700938 sci_controller_power_control_queue_remove(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000939}
940
Dan Williams89a73012011-06-30 19:14:33 -0700941static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000942{
Dan Williams85280952011-06-28 15:05:53 -0700943 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700944 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000945
Dan Williams89a73012011-06-30 19:14:33 -0700946 sci_controller_power_control_queue_insert(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000947}
948
Dan Williams89a73012011-06-30 19:14:33 -0700949static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000950{
Dan Williams85280952011-06-28 15:05:53 -0700951 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700952 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000953
Dan Williams89a73012011-06-30 19:14:33 -0700954 sci_controller_power_control_queue_remove(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000955}
956
Dan Williams89a73012011-06-30 19:14:33 -0700957static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000958{
Dan Williams85280952011-06-28 15:05:53 -0700959 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000960
Dan Williams85280952011-06-28 15:05:53 -0700961 sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000962}
963
Dan Williams89a73012011-06-30 19:14:33 -0700964static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000965{
Dan Williams85280952011-06-28 15:05:53 -0700966 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000967
Dan Williams85280952011-06-28 15:05:53 -0700968 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000969}
970
Dan Williams89a73012011-06-30 19:14:33 -0700971static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000972{
Dan Williams85280952011-06-28 15:05:53 -0700973 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000974
Dan Williams85280952011-06-28 15:05:53 -0700975 sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000976}
977
Dan Williams89a73012011-06-30 19:14:33 -0700978static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000979{
Dan Williams85280952011-06-28 15:05:53 -0700980 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000981
Dan Williams85280952011-06-28 15:05:53 -0700982 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000983}
984
Dan Williams89a73012011-06-30 19:14:33 -0700985static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000986{
Dan Williams85280952011-06-28 15:05:53 -0700987 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000988
Dan Williams89a73012011-06-30 19:14:33 -0700989 if (sci_port_link_detected(iphy->owning_port, iphy)) {
Adam Gruchala4a33c522011-05-10 23:54:23 +0000990
Adam Gruchala4a33c522011-05-10 23:54:23 +0000991 /*
992 * Clear the PE suspend condition so we can actually
993 * receive SIG FIS
994 * The hardware will not respond to the XRDY until the PE
995 * suspend condition is cleared.
996 */
Dan Williams89a73012011-06-30 19:14:33 -0700997 sci_phy_resume(iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000998
Dan Williams85280952011-06-28 15:05:53 -0700999 sci_mod_timer(&iphy->sata_timer,
Edmund Nadolskia628d472011-05-19 11:59:36 +00001000 SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001001 } else
Dan Williams85280952011-06-28 15:05:53 -07001002 iphy->is_in_link_training = false;
Adam Gruchala4a33c522011-05-10 23:54:23 +00001003}
1004
Dan Williams89a73012011-06-30 19:14:33 -07001005static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +00001006{
Dan Williams85280952011-06-28 15:05:53 -07001007 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001008
Dan Williams85280952011-06-28 15:05:53 -07001009 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001010}
1011
Dan Williams89a73012011-06-30 19:14:33 -07001012static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +00001013{
Dan Williams85280952011-06-28 15:05:53 -07001014 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001015
Adam Gruchala4a33c522011-05-10 23:54:23 +00001016 /* State machine has run to completion so exit out and change
1017 * the base state machine to the ready state
1018 */
Dan Williams85280952011-06-28 15:05:53 -07001019 sci_change_state(&iphy->sm, SCI_PHY_READY);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001020}
1021
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001022/**
1023 *
Dan Williams85280952011-06-28 15:05:53 -07001024 * @sci_phy: This is the struct isci_phy object to stop.
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001025 *
Dan Williams85280952011-06-28 15:05:53 -07001026 * This method will stop the struct isci_phy object. This does not reset the
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001027 * protocol engine it just suspends it and places it in a state where it will
1028 * not cause the end device to power up. none
1029 */
1030static void scu_link_layer_stop_protocol_engine(
Dan Williams85280952011-06-28 15:05:53 -07001031 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001032{
1033 u32 scu_sas_pcfg_value;
1034 u32 enable_spinup_value;
1035
1036 /* Suspend the protocol engine and place it in a sata spinup hold state */
1037 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -07001038 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001039 scu_sas_pcfg_value |=
1040 (SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1041 SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
1042 SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
1043 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -07001044 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001045
1046 /* Disable the notify enable spinup primitives */
Dan Williams85280952011-06-28 15:05:53 -07001047 enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001048 enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
Dan Williams85280952011-06-28 15:05:53 -07001049 writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001050}
1051
1052/**
1053 *
1054 *
Dan Williams85280952011-06-28 15:05:53 -07001055 * This method will start the OOB/SN state machine for this struct isci_phy object.
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001056 */
1057static void scu_link_layer_start_oob(
Dan Williams85280952011-06-28 15:05:53 -07001058 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001059{
1060 u32 scu_sas_pcfg_value;
1061
1062 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -07001063 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001064 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1065 scu_sas_pcfg_value &=
1066 ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1067 SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
1068 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -07001069 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001070}
1071
1072/**
1073 *
1074 *
1075 * This method will transmit a hard reset request on the specified phy. The SCU
1076 * hardware requires that we reset the OOB state machine and set the hard reset
1077 * bit in the phy configuration register. We then must start OOB over with the
1078 * hard reset bit set.
1079 */
1080static void scu_link_layer_tx_hard_reset(
Dan Williams85280952011-06-28 15:05:53 -07001081 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001082{
1083 u32 phy_configuration_value;
1084
1085 /*
1086 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
1087 * to the starting state. */
1088 phy_configuration_value =
Dan Williams85280952011-06-28 15:05:53 -07001089 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001090 phy_configuration_value |=
1091 (SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
1092 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
1093 writel(phy_configuration_value,
Dan Williams85280952011-06-28 15:05:53 -07001094 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001095
1096 /* Now take the OOB state machine out of reset */
1097 phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1098 phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
1099 writel(phy_configuration_value,
Dan Williams85280952011-06-28 15:05:53 -07001100 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001101}
1102
Dan Williams89a73012011-06-30 19:14:33 -07001103static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001104{
Dan Williams85280952011-06-28 15:05:53 -07001105 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001106 struct isci_port *iport = iphy->owning_port;
1107 struct isci_host *ihost = iport->owning_controller;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001108
1109 /*
1110 * @todo We need to get to the controller to place this PE in a
1111 * reset state
1112 */
Dan Williams85280952011-06-28 15:05:53 -07001113 sci_del_timer(&iphy->sata_timer);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001114
Dan Williams85280952011-06-28 15:05:53 -07001115 scu_link_layer_stop_protocol_engine(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001116
Dan Williams85280952011-06-28 15:05:53 -07001117 if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
Dan Williams34a99152011-07-01 02:25:15 -07001118 sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001119}
1120
Dan Williams89a73012011-06-30 19:14:33 -07001121static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001122{
Dan Williams85280952011-06-28 15:05:53 -07001123 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001124 struct isci_port *iport = iphy->owning_port;
1125 struct isci_host *ihost = iport->owning_controller;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001126
Dan Williams85280952011-06-28 15:05:53 -07001127 scu_link_layer_stop_protocol_engine(iphy);
1128 scu_link_layer_start_oob(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001129
1130 /* We don't know what kind of phy we are going to be just yet */
Dan Williams85280952011-06-28 15:05:53 -07001131 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
1132 iphy->bcn_received_while_port_unassigned = false;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001133
Dan Williams85280952011-06-28 15:05:53 -07001134 if (iphy->sm.previous_state_id == SCI_PHY_READY)
Dan Williams34a99152011-07-01 02:25:15 -07001135 sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001136
Dan Williams85280952011-06-28 15:05:53 -07001137 sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001138}
1139
Dan Williams89a73012011-06-30 19:14:33 -07001140static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001141{
Dan Williams85280952011-06-28 15:05:53 -07001142 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001143 struct isci_port *iport = iphy->owning_port;
1144 struct isci_host *ihost = iport->owning_controller;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001145
Dan Williams34a99152011-07-01 02:25:15 -07001146 sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001147}
1148
Dan Williams89a73012011-06-30 19:14:33 -07001149static void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001150{
Dan Williams85280952011-06-28 15:05:53 -07001151 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001152
Dan Williams89a73012011-06-30 19:14:33 -07001153 sci_phy_suspend(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001154}
1155
Dan Williams89a73012011-06-30 19:14:33 -07001156static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001157{
Dan Williams85280952011-06-28 15:05:53 -07001158 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001159
Dan Williams5b1d4af2011-05-12 04:51:41 -07001160 /* The phy is being reset, therefore deactivate it from the port. In
1161 * the resetting state we don't notify the user regarding link up and
1162 * link down notifications
1163 */
Dan Williams89a73012011-06-30 19:14:33 -07001164 sci_port_deactivate_phy(iphy->owning_port, iphy, false);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001165
Dan Williams85280952011-06-28 15:05:53 -07001166 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
1167 scu_link_layer_tx_hard_reset(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001168 } else {
Dan Williams5b1d4af2011-05-12 04:51:41 -07001169 /* The SCU does not need to have a discrete reset state so
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001170 * just go back to the starting state.
1171 */
Dan Williams85280952011-06-28 15:05:53 -07001172 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001173 }
1174}
1175
Dan Williams89a73012011-06-30 19:14:33 -07001176static const struct sci_base_state sci_phy_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001177 [SCI_PHY_INITIAL] = { },
1178 [SCI_PHY_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001179 .enter_state = sci_phy_stopped_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001180 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001181 [SCI_PHY_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001182 .enter_state = sci_phy_starting_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001183 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001184 [SCI_PHY_SUB_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001185 .enter_state = sci_phy_starting_initial_substate_enter,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001186 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001187 [SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
1188 [SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
1189 [SCI_PHY_SUB_AWAIT_IAF_UF] = { },
1190 [SCI_PHY_SUB_AWAIT_SAS_POWER] = {
Dan Williams89a73012011-06-30 19:14:33 -07001191 .enter_state = sci_phy_starting_await_sas_power_substate_enter,
1192 .exit_state = sci_phy_starting_await_sas_power_substate_exit,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001193 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001194 [SCI_PHY_SUB_AWAIT_SATA_POWER] = {
Dan Williams89a73012011-06-30 19:14:33 -07001195 .enter_state = sci_phy_starting_await_sata_power_substate_enter,
1196 .exit_state = sci_phy_starting_await_sata_power_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001197 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001198 [SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
Dan Williams89a73012011-06-30 19:14:33 -07001199 .enter_state = sci_phy_starting_await_sata_phy_substate_enter,
1200 .exit_state = sci_phy_starting_await_sata_phy_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001201 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001202 [SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
Dan Williams89a73012011-06-30 19:14:33 -07001203 .enter_state = sci_phy_starting_await_sata_speed_substate_enter,
1204 .exit_state = sci_phy_starting_await_sata_speed_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001205 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001206 [SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
Dan Williams89a73012011-06-30 19:14:33 -07001207 .enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
1208 .exit_state = sci_phy_starting_await_sig_fis_uf_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001209 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001210 [SCI_PHY_SUB_FINAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001211 .enter_state = sci_phy_starting_final_substate_enter,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001212 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001213 [SCI_PHY_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001214 .enter_state = sci_phy_ready_state_enter,
1215 .exit_state = sci_phy_ready_state_exit,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001216 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001217 [SCI_PHY_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001218 .enter_state = sci_phy_resetting_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001219 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001220 [SCI_PHY_FINAL] = { },
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001221};
1222
Dan Williams89a73012011-06-30 19:14:33 -07001223void sci_phy_construct(struct isci_phy *iphy,
Dan Williamsffe191c2011-06-29 13:09:25 -07001224 struct isci_port *iport, u8 phy_index)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001225{
Dan Williams89a73012011-06-30 19:14:33 -07001226 sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001227
1228 /* Copy the rest of the input data to our locals */
Dan Williamsffe191c2011-06-29 13:09:25 -07001229 iphy->owning_port = iport;
Dan Williams85280952011-06-28 15:05:53 -07001230 iphy->phy_index = phy_index;
1231 iphy->bcn_received_while_port_unassigned = false;
1232 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
1233 iphy->link_layer_registers = NULL;
1234 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Edmund Nadolskia628d472011-05-19 11:59:36 +00001235
1236 /* Create the SIGNATURE FIS Timeout timer for this phy */
Dan Williams85280952011-06-28 15:05:53 -07001237 sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001238}
Dan Williams6f231dd2011-07-02 22:56:22 -07001239
Dan Williams4b339812011-05-06 17:36:38 -07001240void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
Dan Williams6f231dd2011-07-02 22:56:22 -07001241{
Dan Williams89a73012011-06-30 19:14:33 -07001242 struct sci_oem_params *oem = &ihost->oem_parameters;
Dan Williams4b339812011-05-06 17:36:38 -07001243 u64 sci_sas_addr;
1244 __be64 sas_addr;
Dan Williams6f231dd2011-07-02 22:56:22 -07001245
Dan Williams89a73012011-06-30 19:14:33 -07001246 sci_sas_addr = oem->phys[index].sas_address.high;
Dan Williams4b339812011-05-06 17:36:38 -07001247 sci_sas_addr <<= 32;
Dan Williams89a73012011-06-30 19:14:33 -07001248 sci_sas_addr |= oem->phys[index].sas_address.low;
Dan Williams4b339812011-05-06 17:36:38 -07001249 sas_addr = cpu_to_be64(sci_sas_addr);
1250 memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
Dan Williams6f231dd2011-07-02 22:56:22 -07001251
Dan Williams4b339812011-05-06 17:36:38 -07001252 iphy->isci_port = NULL;
1253 iphy->sas_phy.enabled = 0;
1254 iphy->sas_phy.id = index;
1255 iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
1256 iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
1257 iphy->sas_phy.ha = &ihost->sas_ha;
1258 iphy->sas_phy.lldd_phy = iphy;
1259 iphy->sas_phy.enabled = 1;
1260 iphy->sas_phy.class = SAS;
1261 iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
1262 iphy->sas_phy.tproto = 0;
1263 iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
1264 iphy->sas_phy.role = PHY_ROLE_INITIATOR;
1265 iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
1266 iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
1267 memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
Dan Williams6f231dd2011-07-02 22:56:22 -07001268}
1269
1270
1271/**
1272 * isci_phy_control() - This function is one of the SAS Domain Template
1273 * functions. This is a phy management function.
1274 * @phy: This parameter specifies the sphy being controlled.
1275 * @func: This parameter specifies the phy control function being invoked.
1276 * @buf: This parameter is specific to the phy function being invoked.
1277 *
1278 * status, zero indicates success.
1279 */
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001280int isci_phy_control(struct asd_sas_phy *sas_phy,
1281 enum phy_func func,
1282 void *buf)
Dan Williams6f231dd2011-07-02 22:56:22 -07001283{
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001284 int ret = 0;
1285 struct isci_phy *iphy = sas_phy->lldd_phy;
1286 struct isci_port *iport = iphy->isci_port;
1287 struct isci_host *ihost = sas_phy->ha->lldd_ha;
1288 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001289
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001290 dev_dbg(&ihost->pdev->dev,
1291 "%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
1292 __func__, sas_phy, func, buf, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001293
1294 switch (func) {
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001295 case PHY_FUNC_DISABLE:
1296 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001297 sci_phy_stop(iphy);
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001298 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1299 break;
1300
Dan Williams6f231dd2011-07-02 22:56:22 -07001301 case PHY_FUNC_LINK_RESET:
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001302 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001303 sci_phy_stop(iphy);
1304 sci_phy_start(iphy);
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001305 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1306 break;
1307
1308 case PHY_FUNC_HARD_RESET:
1309 if (!iport)
1310 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001311
1312 /* Perform the port reset. */
Dan Williams4393aa42011-03-31 13:10:44 -07001313 ret = isci_port_perform_hard_reset(ihost, iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -07001314
1315 break;
Dan Williamsac013ed12011-09-28 18:48:02 -07001316 case PHY_FUNC_GET_EVENTS: {
1317 struct scu_link_layer_registers __iomem *r;
1318 struct sas_phy *phy = sas_phy->phy;
1319
1320 r = iphy->link_layer_registers;
1321 phy->running_disparity_error_count = readl(&r->running_disparity_error_count);
1322 phy->loss_of_dword_sync_count = readl(&r->loss_of_sync_error_count);
1323 phy->phy_reset_problem_count = readl(&r->phy_reset_problem_count);
1324 phy->invalid_dword_count = readl(&r->invalid_dword_counter);
1325 break;
1326 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001327
Dan Williams6f231dd2011-07-02 22:56:22 -07001328 default:
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001329 dev_dbg(&ihost->pdev->dev,
1330 "%s: phy %p; func %d NOT IMPLEMENTED!\n",
1331 __func__, sas_phy, func);
1332 ret = -ENOSYS;
Dan Williams6f231dd2011-07-02 22:56:22 -07001333 break;
1334 }
1335 return ret;
1336}