blob: 0ae0990b39dd45766c7a97d634f69b190d9e96db [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
189 /* @todo Provide a way to write this register correctly */
Dan Williams2e5da882012-01-04 01:32:34 -0800190 writel(0x02108421, &llr->afe_lookup_table_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700191
192 llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
Dan Williams89a73012011-06-30 19:14:33 -0700193 (u8)ihost->user_parameters.no_outbound_task_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700194
James Bottomleya5ec7f862011-07-03 14:14:45 -0500195 switch (phy_user->max_speed_generation) {
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700196 case SCIC_SDS_PARM_GEN3_SPEED:
197 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
198 break;
199 case SCIC_SDS_PARM_GEN2_SPEED:
200 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
201 break;
202 default:
203 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
204 break;
205 }
206 llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
Dan Williams2e5da882012-01-04 01:32:34 -0800207 writel(llctl, &llr->link_layer_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700208
Dan Williams2e5da882012-01-04 01:32:34 -0800209 sp_timeouts = readl(&llr->sas_phy_timeouts);
Marcin Tomczak985af6f2011-07-29 17:16:50 -0700210
211 /* Clear the default 0x36 (54us) RATE_CHANGE timeout value. */
212 sp_timeouts &= ~SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0xFF);
213
214 /* Set RATE_CHANGE timeout value to 0x3B (59us). This ensures SCU can
215 * lock with 3Gb drive when SCU max rate is set to 1.5Gb.
216 */
217 sp_timeouts |= SCU_SAS_PHYTOV_GEN_VAL(RATE_CHANGE, 0x3B);
218
Dan Williams2e5da882012-01-04 01:32:34 -0800219 writel(sp_timeouts, &llr->sas_phy_timeouts);
Marcin Tomczak985af6f2011-07-29 17:16:50 -0700220
Dan Williamsdc00c8b2011-07-01 11:41:21 -0700221 if (is_a2(ihost->pdev)) {
Dan Williams2e5da882012-01-04 01:32:34 -0800222 /* Program the max ARB time for the PHY to 700us so we
223 * inter-operate with the PMC expander which shuts down
224 * PHYs if the expander PHY generates too many breaks.
225 * This time value will guarantee that the initiator PHY
226 * will generate the break.
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700227 */
228 writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
Dan Williams2e5da882012-01-04 01:32:34 -0800229 &llr->maximum_arbitration_wait_timer_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700230 }
231
Dan Williams2e5da882012-01-04 01:32:34 -0800232 /* Disable link layer hang detection, rely on the OS timeout for
233 * I/O timeouts.
234 */
235 writel(0, &llr->link_layer_hang_detection_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700236
237 /* We can exit the initial state to the stopped state */
Dan Williams85280952011-06-28 15:05:53 -0700238 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700239
240 return SCI_SUCCESS;
241}
242
Edmund Nadolskia628d472011-05-19 11:59:36 +0000243static void phy_sata_timeout(unsigned long data)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700244{
Edmund Nadolskia628d472011-05-19 11:59:36 +0000245 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williams85280952011-06-28 15:05:53 -0700246 struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700247 struct isci_host *ihost = iphy->owning_port->owning_controller;
Edmund Nadolskia628d472011-05-19 11:59:36 +0000248 unsigned long flags;
249
250 spin_lock_irqsave(&ihost->scic_lock, flags);
251
252 if (tmr->cancel)
253 goto done;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700254
Dan Williams85280952011-06-28 15:05:53 -0700255 dev_dbg(sciphy_to_dev(iphy),
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700256 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
257 "timeout.\n",
258 __func__,
Dan Williams85280952011-06-28 15:05:53 -0700259 iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700260
Dan Williams85280952011-06-28 15:05:53 -0700261 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Edmund Nadolskia628d472011-05-19 11:59:36 +0000262done:
263 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700264}
265
266/**
267 * This method returns the port currently containing this phy. If the phy is
268 * currently contained by the dummy port, then the phy is considered to not
269 * be part of a port.
270 * @sci_phy: This parameter specifies the phy for which to retrieve the
271 * containing port.
272 *
273 * This method returns a handle to a port that contains the supplied phy.
274 * NULL This value is returned if the phy is not part of a real
275 * port (i.e. it's contained in the dummy port). !NULL All other
276 * values indicate a handle/pointer to the port containing the phy.
277 */
Dan Williams34a99152011-07-01 02:25:15 -0700278struct isci_port *phy_get_non_dummy_port(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700279{
Dan Williams34a99152011-07-01 02:25:15 -0700280 struct isci_port *iport = iphy->owning_port;
281
282 if (iport->physical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700283 return NULL;
284
Dan Williams85280952011-06-28 15:05:53 -0700285 return iphy->owning_port;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700286}
287
288/**
289 * This method will assign a port to the phy object.
Dan Williams85280952011-06-28 15:05:53 -0700290 * @out]: iphy This parameter specifies the phy for which to assign a port
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700291 * object.
292 *
293 *
294 */
Dan Williams89a73012011-06-30 19:14:33 -0700295void sci_phy_set_port(
Dan Williams85280952011-06-28 15:05:53 -0700296 struct isci_phy *iphy,
Dan Williamsffe191c2011-06-29 13:09:25 -0700297 struct isci_port *iport)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700298{
Dan Williamsffe191c2011-06-29 13:09:25 -0700299 iphy->owning_port = iport;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700300
Dan Williams85280952011-06-28 15:05:53 -0700301 if (iphy->bcn_received_while_port_unassigned) {
302 iphy->bcn_received_while_port_unassigned = false;
Dan Williams89a73012011-06-30 19:14:33 -0700303 sci_port_broadcast_change_received(iphy->owning_port, iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700304 }
305}
306
Dan Williams89a73012011-06-30 19:14:33 -0700307enum sci_status sci_phy_initialize(struct isci_phy *iphy,
308 struct scu_transport_layer_registers __iomem *tl,
309 struct scu_link_layer_registers __iomem *ll)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700310{
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700311 /* Perfrom the initialization of the TL hardware */
Dan Williams89a73012011-06-30 19:14:33 -0700312 sci_phy_transport_layer_initialization(iphy, tl);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700313
314 /* Perofrm the initialization of the PE hardware */
Dan Williams89a73012011-06-30 19:14:33 -0700315 sci_phy_link_layer_initialization(iphy, ll);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700316
Dan Williams89a73012011-06-30 19:14:33 -0700317 /* There is nothing that needs to be done in this state just
318 * transition to the stopped state
319 */
Dan Williams85280952011-06-28 15:05:53 -0700320 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700321
322 return SCI_SUCCESS;
323}
324
325/**
326 * This method assigns the direct attached device ID for this phy.
327 *
Dan Williams85280952011-06-28 15:05:53 -0700328 * @iphy The phy for which the direct attached device id is to
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700329 * be assigned.
330 * @device_id The direct attached device ID to assign to the phy.
331 * This will either be the RNi for the device or an invalid RNi if there
332 * is no current device assigned to the phy.
333 */
Dan Williams89a73012011-06-30 19:14:33 -0700334void sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700335{
336 u32 tl_control;
337
Dan Williams85280952011-06-28 15:05:53 -0700338 writel(device_id, &iphy->transport_layer_registers->stp_rni);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700339
340 /*
341 * The read should guarantee that the first write gets posted
342 * before the next write
343 */
Dan Williams85280952011-06-28 15:05:53 -0700344 tl_control = readl(&iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700345 tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
Dan Williams85280952011-06-28 15:05:53 -0700346 writel(tl_control, &iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700347}
348
Dan Williams89a73012011-06-30 19:14:33 -0700349static void sci_phy_suspend(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700350{
351 u32 scu_sas_pcfg_value;
352
353 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700354 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700355 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
356 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700357 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700358
Dan Williams89a73012011-06-30 19:14:33 -0700359 sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700360}
361
Dan Williams89a73012011-06-30 19:14:33 -0700362void sci_phy_resume(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700363{
364 u32 scu_sas_pcfg_value;
365
366 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700367 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700368 scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
369 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700370 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700371}
372
Dan Williams89a73012011-06-30 19:14:33 -0700373void sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700374{
Dan Williams89a73012011-06-30 19:14:33 -0700375 sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
376 sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700377}
378
Dan Williams89a73012011-06-30 19:14:33 -0700379void sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700380{
381 struct sas_identify_frame *iaf;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700382
383 iaf = &iphy->frame_rcvd.iaf;
Dan Williams89a73012011-06-30 19:14:33 -0700384 memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700385}
386
Dan Williams89a73012011-06-30 19:14:33 -0700387void sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700388{
Dan Williams89a73012011-06-30 19:14:33 -0700389 proto->all = readl(&iphy->link_layer_registers->transmit_identification);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700390}
391
Dan Williams89a73012011-06-30 19:14:33 -0700392enum sci_status sci_phy_start(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700393{
Dan Williams89a73012011-06-30 19:14:33 -0700394 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams966699b2011-05-12 03:44:24 -0700395
Edmund Nadolskie3013702011-06-02 00:10:43 +0000396 if (state != SCI_PHY_STOPPED) {
Dan Williams85280952011-06-28 15:05:53 -0700397 dev_dbg(sciphy_to_dev(iphy),
Dan Williams966699b2011-05-12 03:44:24 -0700398 "%s: in wrong state: %d\n", __func__, state);
399 return SCI_FAILURE_INVALID_STATE;
400 }
401
Dan Williams85280952011-06-28 15:05:53 -0700402 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams966699b2011-05-12 03:44:24 -0700403 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700404}
405
Dan Williams89a73012011-06-30 19:14:33 -0700406enum sci_status sci_phy_stop(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700407{
Dan Williams89a73012011-06-30 19:14:33 -0700408 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams93153232011-05-12 04:01:03 -0700409
410 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000411 case SCI_PHY_SUB_INITIAL:
412 case SCI_PHY_SUB_AWAIT_OSSP_EN:
413 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
414 case SCI_PHY_SUB_AWAIT_SAS_POWER:
415 case SCI_PHY_SUB_AWAIT_SATA_POWER:
416 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
417 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
418 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
419 case SCI_PHY_SUB_FINAL:
420 case SCI_PHY_READY:
Dan Williams93153232011-05-12 04:01:03 -0700421 break;
422 default:
Dan Williams85280952011-06-28 15:05:53 -0700423 dev_dbg(sciphy_to_dev(iphy),
Dan Williams93153232011-05-12 04:01:03 -0700424 "%s: in wrong state: %d\n", __func__, state);
425 return SCI_FAILURE_INVALID_STATE;
426 }
427
Dan Williams85280952011-06-28 15:05:53 -0700428 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williams93153232011-05-12 04:01:03 -0700429 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700430}
431
Dan Williams89a73012011-06-30 19:14:33 -0700432enum sci_status sci_phy_reset(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700433{
Dan Williams89a73012011-06-30 19:14:33 -0700434 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams0cf36fa2011-05-12 04:02:07 -0700435
Edmund Nadolskie3013702011-06-02 00:10:43 +0000436 if (state != SCI_PHY_READY) {
Dan Williams85280952011-06-28 15:05:53 -0700437 dev_dbg(sciphy_to_dev(iphy),
Dan Williams0cf36fa2011-05-12 04:02:07 -0700438 "%s: in wrong state: %d\n", __func__, state);
439 return SCI_FAILURE_INVALID_STATE;
440 }
441
Dan Williams85280952011-06-28 15:05:53 -0700442 sci_change_state(&iphy->sm, SCI_PHY_RESETTING);
Dan Williams0cf36fa2011-05-12 04:02:07 -0700443 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700444}
445
Dan Williams89a73012011-06-30 19:14:33 -0700446enum sci_status sci_phy_consume_power_handler(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700447{
Dan Williams89a73012011-06-30 19:14:33 -0700448 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams5b1d4af2011-05-12 04:51:41 -0700449
450 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000451 case SCI_PHY_SUB_AWAIT_SAS_POWER: {
Dan Williams5b1d4af2011-05-12 04:51:41 -0700452 u32 enable_spinup;
453
Dan Williams85280952011-06-28 15:05:53 -0700454 enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700455 enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
Dan Williams85280952011-06-28 15:05:53 -0700456 writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700457
458 /* Change state to the final state this substate machine has run to completion */
Dan Williams85280952011-06-28 15:05:53 -0700459 sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700460
461 return SCI_SUCCESS;
462 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000463 case SCI_PHY_SUB_AWAIT_SATA_POWER: {
Dan Williams5b1d4af2011-05-12 04:51:41 -0700464 u32 scu_sas_pcfg_value;
465
466 /* Release the spinup hold state and reset the OOB state machine */
467 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700468 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700469 scu_sas_pcfg_value &=
470 ~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
471 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
472 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700473 &iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700474
475 /* Now restart the OOB operation */
476 scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
477 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
478 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700479 &iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700480
481 /* Change state to the final state this substate machine has run to completion */
Dan Williams85280952011-06-28 15:05:53 -0700482 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700483
484 return SCI_SUCCESS;
485 }
486 default:
Dan Williams85280952011-06-28 15:05:53 -0700487 dev_dbg(sciphy_to_dev(iphy),
Dan Williams5b1d4af2011-05-12 04:51:41 -0700488 "%s: in wrong state: %d\n", __func__, state);
489 return SCI_FAILURE_INVALID_STATE;
490 }
Dan Williams23506a62011-05-12 04:27:29 -0700491}
492
Dan Williams89a73012011-06-30 19:14:33 -0700493static void sci_phy_start_sas_link_training(struct isci_phy *iphy)
Dan Williams23506a62011-05-12 04:27:29 -0700494{
Dan Williams89a73012011-06-30 19:14:33 -0700495 /* continue the link training for the phy as if it were a SAS PHY
496 * instead of a SATA PHY. This is done because the completion queue had a SAS
497 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
498 */
Dan Williams23506a62011-05-12 04:27:29 -0700499 u32 phy_control;
500
Dan Williams89a73012011-06-30 19:14:33 -0700501 phy_control = readl(&iphy->link_layer_registers->phy_configuration);
Dan Williams23506a62011-05-12 04:27:29 -0700502 phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
503 writel(phy_control,
Dan Williams89a73012011-06-30 19:14:33 -0700504 &iphy->link_layer_registers->phy_configuration);
Dan Williams23506a62011-05-12 04:27:29 -0700505
Dan Williams85280952011-06-28 15:05:53 -0700506 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700507
Dan Williams85280952011-06-28 15:05:53 -0700508 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
Dan Williams23506a62011-05-12 04:27:29 -0700509}
510
Dan Williams89a73012011-06-30 19:14:33 -0700511static void sci_phy_start_sata_link_training(struct isci_phy *iphy)
Dan Williams23506a62011-05-12 04:27:29 -0700512{
Dan Williams89a73012011-06-30 19:14:33 -0700513 /* This method continues the link training for the phy as if it were a SATA PHY
514 * instead of a SAS PHY. This is done because the completion queue had a SATA
515 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
516 */
Dan Williams85280952011-06-28 15:05:53 -0700517 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
Dan Williams23506a62011-05-12 04:27:29 -0700518
Dan Williams85280952011-06-28 15:05:53 -0700519 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
Dan Williams23506a62011-05-12 04:27:29 -0700520}
521
522/**
Dan Williams89a73012011-06-30 19:14:33 -0700523 * sci_phy_complete_link_training - perform processing common to
Dan Williams23506a62011-05-12 04:27:29 -0700524 * all protocols upon completion of link training.
525 * @sci_phy: This parameter specifies the phy object for which link training
526 * has completed.
527 * @max_link_rate: This parameter specifies the maximum link rate to be
528 * associated with this phy.
529 * @next_state: This parameter specifies the next state for the phy's starting
530 * sub-state machine.
531 *
532 */
Dan Williams89a73012011-06-30 19:14:33 -0700533static void sci_phy_complete_link_training(struct isci_phy *iphy,
534 enum sas_linkrate max_link_rate,
535 u32 next_state)
Dan Williams23506a62011-05-12 04:27:29 -0700536{
Dan Williams85280952011-06-28 15:05:53 -0700537 iphy->max_negotiated_speed = max_link_rate;
Dan Williams23506a62011-05-12 04:27:29 -0700538
Dan Williams85280952011-06-28 15:05:53 -0700539 sci_change_state(&iphy->sm, next_state);
Dan Williams23506a62011-05-12 04:27:29 -0700540}
541
Dan Williams89a73012011-06-30 19:14:33 -0700542enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
Dan Williams23506a62011-05-12 04:27:29 -0700543{
Dan Williams89a73012011-06-30 19:14:33 -0700544 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams23506a62011-05-12 04:27:29 -0700545
546 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000547 case SCI_PHY_SUB_AWAIT_OSSP_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700548 switch (scu_get_event_code(event_code)) {
549 case SCU_EVENT_SAS_PHY_DETECTED:
Dan Williams89a73012011-06-30 19:14:33 -0700550 sci_phy_start_sas_link_training(iphy);
Dan Williams85280952011-06-28 15:05:53 -0700551 iphy->is_in_link_training = true;
Dan Williams23506a62011-05-12 04:27:29 -0700552 break;
553 case SCU_EVENT_SATA_SPINUP_HOLD:
Dan Williams89a73012011-06-30 19:14:33 -0700554 sci_phy_start_sata_link_training(iphy);
Dan Williams85280952011-06-28 15:05:53 -0700555 iphy->is_in_link_training = true;
Dan Williams23506a62011-05-12 04:27:29 -0700556 break;
557 default:
Dan Williams85280952011-06-28 15:05:53 -0700558 dev_dbg(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700559 "%s: PHY starting substate machine received "
560 "unexpected event_code %x\n",
561 __func__,
562 event_code);
563 return SCI_FAILURE;
564 }
565 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000566 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700567 switch (scu_get_event_code(event_code)) {
568 case SCU_EVENT_SAS_PHY_DETECTED:
569 /*
570 * Why is this being reported again by the controller?
571 * We would re-enter this state so just stay here */
572 break;
573 case SCU_EVENT_SAS_15:
574 case SCU_EVENT_SAS_15_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700575 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
576 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700577 break;
578 case SCU_EVENT_SAS_30:
579 case SCU_EVENT_SAS_30_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700580 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
581 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700582 break;
583 case SCU_EVENT_SAS_60:
584 case SCU_EVENT_SAS_60_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700585 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
586 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700587 break;
588 case SCU_EVENT_SATA_SPINUP_HOLD:
589 /*
590 * We were doing SAS PHY link training and received a SATA PHY event
591 * continue OOB/SN as if this were a SATA PHY */
Dan Williams89a73012011-06-30 19:14:33 -0700592 sci_phy_start_sata_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700593 break;
594 case SCU_EVENT_LINK_FAILURE:
595 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700596 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700597 break;
598 default:
Dan Williams85280952011-06-28 15:05:53 -0700599 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700600 "%s: PHY starting substate machine received "
601 "unexpected event_code %x\n",
602 __func__, event_code);
603
604 return SCI_FAILURE;
605 break;
606 }
607 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000608 case SCI_PHY_SUB_AWAIT_IAF_UF:
Dan Williams23506a62011-05-12 04:27:29 -0700609 switch (scu_get_event_code(event_code)) {
610 case SCU_EVENT_SAS_PHY_DETECTED:
611 /* Backup the state machine */
Dan Williams89a73012011-06-30 19:14:33 -0700612 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700613 break;
614 case SCU_EVENT_SATA_SPINUP_HOLD:
615 /* We were doing SAS PHY link training and received a
616 * SATA PHY event continue OOB/SN as if this were a
617 * SATA PHY
618 */
Dan Williams89a73012011-06-30 19:14:33 -0700619 sci_phy_start_sata_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700620 break;
621 case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
622 case SCU_EVENT_LINK_FAILURE:
623 case SCU_EVENT_HARD_RESET_RECEIVED:
624 /* Start the oob/sn state machine over again */
Dan Williams85280952011-06-28 15:05:53 -0700625 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700626 break;
627 default:
Dan Williams85280952011-06-28 15:05:53 -0700628 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700629 "%s: PHY starting substate machine received "
630 "unexpected event_code %x\n",
631 __func__, event_code);
632 return SCI_FAILURE;
633 }
634 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000635 case SCI_PHY_SUB_AWAIT_SAS_POWER:
Dan Williams23506a62011-05-12 04:27:29 -0700636 switch (scu_get_event_code(event_code)) {
637 case SCU_EVENT_LINK_FAILURE:
638 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700639 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700640 break;
641 default:
Dan Williams85280952011-06-28 15:05:53 -0700642 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700643 "%s: PHY starting substate machine received unexpected "
644 "event_code %x\n",
645 __func__,
646 event_code);
647 return SCI_FAILURE;
648 }
649 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000650 case SCI_PHY_SUB_AWAIT_SATA_POWER:
Dan Williams23506a62011-05-12 04:27:29 -0700651 switch (scu_get_event_code(event_code)) {
652 case SCU_EVENT_LINK_FAILURE:
653 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700654 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700655 break;
656 case SCU_EVENT_SATA_SPINUP_HOLD:
657 /* These events are received every 10ms and are
658 * expected while in this state
659 */
660 break;
661
662 case SCU_EVENT_SAS_PHY_DETECTED:
663 /* There has been a change in the phy type before OOB/SN for the
664 * SATA finished start down the SAS link traning path.
665 */
Dan Williams89a73012011-06-30 19:14:33 -0700666 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700667 break;
668
669 default:
Dan Williams85280952011-06-28 15:05:53 -0700670 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700671 "%s: PHY starting substate machine received "
672 "unexpected event_code %x\n",
673 __func__, event_code);
674
675 return SCI_FAILURE;
676 }
677 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000678 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700679 switch (scu_get_event_code(event_code)) {
680 case SCU_EVENT_LINK_FAILURE:
681 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700682 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700683 break;
684 case SCU_EVENT_SATA_SPINUP_HOLD:
685 /* These events might be received since we dont know how many may be in
686 * the completion queue while waiting for power
687 */
688 break;
689 case SCU_EVENT_SATA_PHY_DETECTED:
Dan Williams85280952011-06-28 15:05:53 -0700690 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
Dan Williams23506a62011-05-12 04:27:29 -0700691
692 /* We have received the SATA PHY notification change state */
Dan Williams85280952011-06-28 15:05:53 -0700693 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700694 break;
695 case SCU_EVENT_SAS_PHY_DETECTED:
696 /* There has been a change in the phy type before OOB/SN for the
697 * SATA finished start down the SAS link traning path.
698 */
Dan Williams89a73012011-06-30 19:14:33 -0700699 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700700 break;
701 default:
Dan Williams85280952011-06-28 15:05:53 -0700702 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700703 "%s: PHY starting substate machine received "
704 "unexpected event_code %x\n",
705 __func__,
706 event_code);
707
Justin P. Mattock69932482011-07-26 23:06:29 -0700708 return SCI_FAILURE;
Dan Williams23506a62011-05-12 04:27:29 -0700709 }
710 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000711 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700712 switch (scu_get_event_code(event_code)) {
713 case SCU_EVENT_SATA_PHY_DETECTED:
714 /*
715 * The hardware reports multiple SATA PHY detected events
716 * ignore the extras */
717 break;
718 case SCU_EVENT_SATA_15:
719 case SCU_EVENT_SATA_15_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700720 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
721 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700722 break;
723 case SCU_EVENT_SATA_30:
724 case SCU_EVENT_SATA_30_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700725 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
726 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700727 break;
728 case SCU_EVENT_SATA_60:
729 case SCU_EVENT_SATA_60_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700730 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
731 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700732 break;
733 case SCU_EVENT_LINK_FAILURE:
734 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700735 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700736 break;
737 case SCU_EVENT_SAS_PHY_DETECTED:
738 /*
739 * There has been a change in the phy type before OOB/SN for the
740 * SATA finished start down the SAS link traning path. */
Dan Williams89a73012011-06-30 19:14:33 -0700741 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700742 break;
743 default:
Dan Williams85280952011-06-28 15:05:53 -0700744 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700745 "%s: PHY starting substate machine received "
746 "unexpected event_code %x\n",
747 __func__, event_code);
748
749 return SCI_FAILURE;
750 }
751
752 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000753 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
Dan Williams23506a62011-05-12 04:27:29 -0700754 switch (scu_get_event_code(event_code)) {
755 case SCU_EVENT_SATA_PHY_DETECTED:
756 /* Backup the state machine */
Dan Williams85280952011-06-28 15:05:53 -0700757 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700758 break;
759
760 case SCU_EVENT_LINK_FAILURE:
761 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700762 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700763 break;
764
765 default:
Dan Williams85280952011-06-28 15:05:53 -0700766 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700767 "%s: PHY starting substate machine received "
768 "unexpected event_code %x\n",
769 __func__,
770 event_code);
771
772 return SCI_FAILURE;
773 }
774 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000775 case SCI_PHY_READY:
Dan Williams23506a62011-05-12 04:27:29 -0700776 switch (scu_get_event_code(event_code)) {
777 case SCU_EVENT_LINK_FAILURE:
778 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700779 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700780 break;
781 case SCU_EVENT_BROADCAST_CHANGE:
782 /* Broadcast change received. Notify the port. */
Dan Williams85280952011-06-28 15:05:53 -0700783 if (phy_get_non_dummy_port(iphy) != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700784 sci_port_broadcast_change_received(iphy->owning_port, iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700785 else
Dan Williams85280952011-06-28 15:05:53 -0700786 iphy->bcn_received_while_port_unassigned = true;
Dan Williams23506a62011-05-12 04:27:29 -0700787 break;
788 default:
Dan Williams85280952011-06-28 15:05:53 -0700789 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700790 "%sP SCIC PHY 0x%p ready state machine received "
791 "unexpected event_code %x\n",
Dan Williams85280952011-06-28 15:05:53 -0700792 __func__, iphy, event_code);
Dan Williams23506a62011-05-12 04:27:29 -0700793 return SCI_FAILURE_INVALID_STATE;
794 }
795 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000796 case SCI_PHY_RESETTING:
Dan Williams23506a62011-05-12 04:27:29 -0700797 switch (scu_get_event_code(event_code)) {
798 case SCU_EVENT_HARD_RESET_TRANSMITTED:
799 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700800 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700801 break;
802 default:
Dan Williams85280952011-06-28 15:05:53 -0700803 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700804 "%s: SCIC PHY 0x%p resetting state machine received "
805 "unexpected event_code %x\n",
Dan Williams85280952011-06-28 15:05:53 -0700806 __func__, iphy, event_code);
Dan Williams23506a62011-05-12 04:27:29 -0700807
808 return SCI_FAILURE_INVALID_STATE;
809 break;
810 }
811 return SCI_SUCCESS;
812 default:
Dan Williams85280952011-06-28 15:05:53 -0700813 dev_dbg(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700814 "%s: in wrong state: %d\n", __func__, state);
815 return SCI_FAILURE_INVALID_STATE;
816 }
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700817}
818
Dan Williams89a73012011-06-30 19:14:33 -0700819enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700820{
Dan Williams89a73012011-06-30 19:14:33 -0700821 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700822 struct isci_host *ihost = iphy->owning_port->owning_controller;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700823 enum sci_status result;
Dan Williams4cffe132011-06-23 23:44:52 -0700824 unsigned long flags;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700825
826 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000827 case SCI_PHY_SUB_AWAIT_IAF_UF: {
Dan Williamsc4441ab2011-05-12 04:17:51 -0700828 u32 *frame_words;
829 struct sas_identify_frame iaf;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700830
Dan Williams89a73012011-06-30 19:14:33 -0700831 result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
832 frame_index,
833 (void **)&frame_words);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700834
835 if (result != SCI_SUCCESS)
836 return result;
837
838 sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
839 if (iaf.frame_type == 0) {
840 u32 state;
841
Dan Williams4cffe132011-06-23 23:44:52 -0700842 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700843 memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
Dan Williams4cffe132011-06-23 23:44:52 -0700844 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700845 if (iaf.smp_tport) {
846 /* We got the IAF for an expander PHY go to the final
847 * state since there are no power requirements for
848 * expander phys.
849 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000850 state = SCI_PHY_SUB_FINAL;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700851 } else {
852 /* We got the IAF we can now go to the await spinup
853 * semaphore state
854 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000855 state = SCI_PHY_SUB_AWAIT_SAS_POWER;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700856 }
Dan Williams85280952011-06-28 15:05:53 -0700857 sci_change_state(&iphy->sm, state);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700858 result = SCI_SUCCESS;
859 } else
Dan Williams85280952011-06-28 15:05:53 -0700860 dev_warn(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700861 "%s: PHY starting substate machine received "
862 "unexpected frame id %x\n",
863 __func__, frame_index);
864
Dan Williams89a73012011-06-30 19:14:33 -0700865 sci_controller_release_frame(ihost, frame_index);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700866 return result;
867 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000868 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
Dan Williamsc4441ab2011-05-12 04:17:51 -0700869 struct dev_to_host_fis *frame_header;
870 u32 *fis_frame_data;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700871
Dan Williams34a99152011-07-01 02:25:15 -0700872 result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
873 frame_index,
874 (void **)&frame_header);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700875
876 if (result != SCI_SUCCESS)
877 return result;
878
879 if ((frame_header->fis_type == FIS_REGD2H) &&
880 !(frame_header->status & ATA_BUSY)) {
Dan Williams89a73012011-06-30 19:14:33 -0700881 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
882 frame_index,
883 (void **)&fis_frame_data);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700884
Dan Williams4cffe132011-06-23 23:44:52 -0700885 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -0700886 sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
887 frame_header,
888 fis_frame_data);
Dan Williams4cffe132011-06-23 23:44:52 -0700889 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700890
891 /* got IAF we can now go to the await spinup semaphore state */
Dan Williams85280952011-06-28 15:05:53 -0700892 sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700893
894 result = SCI_SUCCESS;
895 } else
Dan Williams85280952011-06-28 15:05:53 -0700896 dev_warn(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700897 "%s: PHY starting substate machine received "
898 "unexpected frame id %x\n",
899 __func__, frame_index);
900
901 /* Regardless of the result we are done with this frame with it */
Dan Williams89a73012011-06-30 19:14:33 -0700902 sci_controller_release_frame(ihost, frame_index);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700903
904 return result;
905 }
906 default:
Dan Williams85280952011-06-28 15:05:53 -0700907 dev_dbg(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700908 "%s: in wrong state: %d\n", __func__, state);
909 return SCI_FAILURE_INVALID_STATE;
910 }
Dan Williams5076a1a2011-06-27 14:57:03 -0700911
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700912}
913
Dan Williams89a73012011-06-30 19:14:33 -0700914static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000915{
Dan Williams85280952011-06-28 15:05:53 -0700916 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000917
Adam Gruchala4a33c522011-05-10 23:54:23 +0000918 /* This is just an temporary state go off to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700919 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000920}
921
Dan Williams89a73012011-06-30 19:14:33 -0700922static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000923{
Dan Williams85280952011-06-28 15:05:53 -0700924 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700925 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000926
Dan Williams89a73012011-06-30 19:14:33 -0700927 sci_controller_power_control_queue_insert(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000928}
929
Dan Williams89a73012011-06-30 19:14:33 -0700930static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000931{
Dan Williams85280952011-06-28 15:05:53 -0700932 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700933 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000934
Dan Williams89a73012011-06-30 19:14:33 -0700935 sci_controller_power_control_queue_remove(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000936}
937
Dan Williams89a73012011-06-30 19:14:33 -0700938static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000939{
Dan Williams85280952011-06-28 15:05:53 -0700940 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700941 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000942
Dan Williams89a73012011-06-30 19:14:33 -0700943 sci_controller_power_control_queue_insert(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000944}
945
Dan Williams89a73012011-06-30 19:14:33 -0700946static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000947{
Dan Williams85280952011-06-28 15:05:53 -0700948 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700949 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000950
Dan Williams89a73012011-06-30 19:14:33 -0700951 sci_controller_power_control_queue_remove(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000952}
953
Dan Williams89a73012011-06-30 19:14:33 -0700954static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000955{
Dan Williams85280952011-06-28 15:05:53 -0700956 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000957
Dan Williams85280952011-06-28 15:05:53 -0700958 sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000959}
960
Dan Williams89a73012011-06-30 19:14:33 -0700961static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000962{
Dan Williams85280952011-06-28 15:05:53 -0700963 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000964
Dan Williams85280952011-06-28 15:05:53 -0700965 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000966}
967
Dan Williams89a73012011-06-30 19:14:33 -0700968static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000969{
Dan Williams85280952011-06-28 15:05:53 -0700970 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000971
Dan Williams85280952011-06-28 15:05:53 -0700972 sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000973}
974
Dan Williams89a73012011-06-30 19:14:33 -0700975static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000976{
Dan Williams85280952011-06-28 15:05:53 -0700977 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000978
Dan Williams85280952011-06-28 15:05:53 -0700979 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000980}
981
Dan Williams89a73012011-06-30 19:14:33 -0700982static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000983{
Dan Williams85280952011-06-28 15:05:53 -0700984 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000985
Dan Williams89a73012011-06-30 19:14:33 -0700986 if (sci_port_link_detected(iphy->owning_port, iphy)) {
Adam Gruchala4a33c522011-05-10 23:54:23 +0000987
Adam Gruchala4a33c522011-05-10 23:54:23 +0000988 /*
989 * Clear the PE suspend condition so we can actually
990 * receive SIG FIS
991 * The hardware will not respond to the XRDY until the PE
992 * suspend condition is cleared.
993 */
Dan Williams89a73012011-06-30 19:14:33 -0700994 sci_phy_resume(iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000995
Dan Williams85280952011-06-28 15:05:53 -0700996 sci_mod_timer(&iphy->sata_timer,
Edmund Nadolskia628d472011-05-19 11:59:36 +0000997 SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000998 } else
Dan Williams85280952011-06-28 15:05:53 -0700999 iphy->is_in_link_training = false;
Adam Gruchala4a33c522011-05-10 23:54:23 +00001000}
1001
Dan Williams89a73012011-06-30 19:14:33 -07001002static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +00001003{
Dan Williams85280952011-06-28 15:05:53 -07001004 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001005
Dan Williams85280952011-06-28 15:05:53 -07001006 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001007}
1008
Dan Williams89a73012011-06-30 19:14:33 -07001009static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +00001010{
Dan Williams85280952011-06-28 15:05:53 -07001011 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001012
Adam Gruchala4a33c522011-05-10 23:54:23 +00001013 /* State machine has run to completion so exit out and change
1014 * the base state machine to the ready state
1015 */
Dan Williams85280952011-06-28 15:05:53 -07001016 sci_change_state(&iphy->sm, SCI_PHY_READY);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001017}
1018
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001019/**
1020 *
Dan Williams85280952011-06-28 15:05:53 -07001021 * @sci_phy: This is the struct isci_phy object to stop.
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001022 *
Dan Williams85280952011-06-28 15:05:53 -07001023 * This method will stop the struct isci_phy object. This does not reset the
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001024 * protocol engine it just suspends it and places it in a state where it will
1025 * not cause the end device to power up. none
1026 */
1027static void scu_link_layer_stop_protocol_engine(
Dan Williams85280952011-06-28 15:05:53 -07001028 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001029{
1030 u32 scu_sas_pcfg_value;
1031 u32 enable_spinup_value;
1032
1033 /* Suspend the protocol engine and place it in a sata spinup hold state */
1034 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -07001035 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001036 scu_sas_pcfg_value |=
1037 (SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1038 SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
1039 SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
1040 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -07001041 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001042
1043 /* Disable the notify enable spinup primitives */
Dan Williams85280952011-06-28 15:05:53 -07001044 enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001045 enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
Dan Williams85280952011-06-28 15:05:53 -07001046 writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001047}
1048
1049/**
1050 *
1051 *
Dan Williams85280952011-06-28 15:05:53 -07001052 * This method will start the OOB/SN state machine for this struct isci_phy object.
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001053 */
1054static void scu_link_layer_start_oob(
Dan Williams85280952011-06-28 15:05:53 -07001055 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001056{
1057 u32 scu_sas_pcfg_value;
1058
1059 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -07001060 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001061 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1062 scu_sas_pcfg_value &=
1063 ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1064 SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
1065 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -07001066 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001067}
1068
1069/**
1070 *
1071 *
1072 * This method will transmit a hard reset request on the specified phy. The SCU
1073 * hardware requires that we reset the OOB state machine and set the hard reset
1074 * bit in the phy configuration register. We then must start OOB over with the
1075 * hard reset bit set.
1076 */
1077static void scu_link_layer_tx_hard_reset(
Dan Williams85280952011-06-28 15:05:53 -07001078 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001079{
1080 u32 phy_configuration_value;
1081
1082 /*
1083 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
1084 * to the starting state. */
1085 phy_configuration_value =
Dan Williams85280952011-06-28 15:05:53 -07001086 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001087 phy_configuration_value |=
1088 (SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
1089 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
1090 writel(phy_configuration_value,
Dan Williams85280952011-06-28 15:05:53 -07001091 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001092
1093 /* Now take the OOB state machine out of reset */
1094 phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1095 phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
1096 writel(phy_configuration_value,
Dan Williams85280952011-06-28 15:05:53 -07001097 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001098}
1099
Dan Williams89a73012011-06-30 19:14:33 -07001100static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001101{
Dan Williams85280952011-06-28 15:05:53 -07001102 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001103 struct isci_port *iport = iphy->owning_port;
1104 struct isci_host *ihost = iport->owning_controller;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001105
1106 /*
1107 * @todo We need to get to the controller to place this PE in a
1108 * reset state
1109 */
Dan Williams85280952011-06-28 15:05:53 -07001110 sci_del_timer(&iphy->sata_timer);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001111
Dan Williams85280952011-06-28 15:05:53 -07001112 scu_link_layer_stop_protocol_engine(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001113
Dan Williams85280952011-06-28 15:05:53 -07001114 if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
Dan Williams34a99152011-07-01 02:25:15 -07001115 sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001116}
1117
Dan Williams89a73012011-06-30 19:14:33 -07001118static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001119{
Dan Williams85280952011-06-28 15:05:53 -07001120 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001121 struct isci_port *iport = iphy->owning_port;
1122 struct isci_host *ihost = iport->owning_controller;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001123
Dan Williams85280952011-06-28 15:05:53 -07001124 scu_link_layer_stop_protocol_engine(iphy);
1125 scu_link_layer_start_oob(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001126
1127 /* We don't know what kind of phy we are going to be just yet */
Dan Williams85280952011-06-28 15:05:53 -07001128 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
1129 iphy->bcn_received_while_port_unassigned = false;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001130
Dan Williams85280952011-06-28 15:05:53 -07001131 if (iphy->sm.previous_state_id == SCI_PHY_READY)
Dan Williams34a99152011-07-01 02:25:15 -07001132 sci_controller_link_down(ihost, phy_get_non_dummy_port(iphy), iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001133
Dan Williams85280952011-06-28 15:05:53 -07001134 sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001135}
1136
Dan Williams89a73012011-06-30 19:14:33 -07001137static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001138{
Dan Williams85280952011-06-28 15:05:53 -07001139 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001140 struct isci_port *iport = iphy->owning_port;
1141 struct isci_host *ihost = iport->owning_controller;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001142
Dan Williams34a99152011-07-01 02:25:15 -07001143 sci_controller_link_up(ihost, phy_get_non_dummy_port(iphy), iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001144}
1145
Dan Williams89a73012011-06-30 19:14:33 -07001146static void sci_phy_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001147{
Dan Williams85280952011-06-28 15:05:53 -07001148 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001149
Dan Williams89a73012011-06-30 19:14:33 -07001150 sci_phy_suspend(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001151}
1152
Dan Williams89a73012011-06-30 19:14:33 -07001153static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001154{
Dan Williams85280952011-06-28 15:05:53 -07001155 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001156
Dan Williams5b1d4af2011-05-12 04:51:41 -07001157 /* The phy is being reset, therefore deactivate it from the port. In
1158 * the resetting state we don't notify the user regarding link up and
1159 * link down notifications
1160 */
Dan Williams89a73012011-06-30 19:14:33 -07001161 sci_port_deactivate_phy(iphy->owning_port, iphy, false);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001162
Dan Williams85280952011-06-28 15:05:53 -07001163 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
1164 scu_link_layer_tx_hard_reset(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001165 } else {
Dan Williams5b1d4af2011-05-12 04:51:41 -07001166 /* The SCU does not need to have a discrete reset state so
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001167 * just go back to the starting state.
1168 */
Dan Williams85280952011-06-28 15:05:53 -07001169 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001170 }
1171}
1172
Dan Williams89a73012011-06-30 19:14:33 -07001173static const struct sci_base_state sci_phy_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001174 [SCI_PHY_INITIAL] = { },
1175 [SCI_PHY_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001176 .enter_state = sci_phy_stopped_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001177 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001178 [SCI_PHY_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001179 .enter_state = sci_phy_starting_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001180 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001181 [SCI_PHY_SUB_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001182 .enter_state = sci_phy_starting_initial_substate_enter,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001183 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001184 [SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
1185 [SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
1186 [SCI_PHY_SUB_AWAIT_IAF_UF] = { },
1187 [SCI_PHY_SUB_AWAIT_SAS_POWER] = {
Dan Williams89a73012011-06-30 19:14:33 -07001188 .enter_state = sci_phy_starting_await_sas_power_substate_enter,
1189 .exit_state = sci_phy_starting_await_sas_power_substate_exit,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001190 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001191 [SCI_PHY_SUB_AWAIT_SATA_POWER] = {
Dan Williams89a73012011-06-30 19:14:33 -07001192 .enter_state = sci_phy_starting_await_sata_power_substate_enter,
1193 .exit_state = sci_phy_starting_await_sata_power_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001194 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001195 [SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
Dan Williams89a73012011-06-30 19:14:33 -07001196 .enter_state = sci_phy_starting_await_sata_phy_substate_enter,
1197 .exit_state = sci_phy_starting_await_sata_phy_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001198 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001199 [SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
Dan Williams89a73012011-06-30 19:14:33 -07001200 .enter_state = sci_phy_starting_await_sata_speed_substate_enter,
1201 .exit_state = sci_phy_starting_await_sata_speed_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001202 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001203 [SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
Dan Williams89a73012011-06-30 19:14:33 -07001204 .enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
1205 .exit_state = sci_phy_starting_await_sig_fis_uf_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001206 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001207 [SCI_PHY_SUB_FINAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001208 .enter_state = sci_phy_starting_final_substate_enter,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001209 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001210 [SCI_PHY_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001211 .enter_state = sci_phy_ready_state_enter,
1212 .exit_state = sci_phy_ready_state_exit,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001213 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001214 [SCI_PHY_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001215 .enter_state = sci_phy_resetting_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001216 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001217 [SCI_PHY_FINAL] = { },
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001218};
1219
Dan Williams89a73012011-06-30 19:14:33 -07001220void sci_phy_construct(struct isci_phy *iphy,
Dan Williamsffe191c2011-06-29 13:09:25 -07001221 struct isci_port *iport, u8 phy_index)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001222{
Dan Williams89a73012011-06-30 19:14:33 -07001223 sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001224
1225 /* Copy the rest of the input data to our locals */
Dan Williamsffe191c2011-06-29 13:09:25 -07001226 iphy->owning_port = iport;
Dan Williams85280952011-06-28 15:05:53 -07001227 iphy->phy_index = phy_index;
1228 iphy->bcn_received_while_port_unassigned = false;
1229 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
1230 iphy->link_layer_registers = NULL;
1231 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Edmund Nadolskia628d472011-05-19 11:59:36 +00001232
1233 /* Create the SIGNATURE FIS Timeout timer for this phy */
Dan Williams85280952011-06-28 15:05:53 -07001234 sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001235}
Dan Williams6f231dd2011-07-02 22:56:22 -07001236
Dan Williams4b339812011-05-06 17:36:38 -07001237void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
Dan Williams6f231dd2011-07-02 22:56:22 -07001238{
Dan Williams89a73012011-06-30 19:14:33 -07001239 struct sci_oem_params *oem = &ihost->oem_parameters;
Dan Williams4b339812011-05-06 17:36:38 -07001240 u64 sci_sas_addr;
1241 __be64 sas_addr;
Dan Williams6f231dd2011-07-02 22:56:22 -07001242
Dan Williams89a73012011-06-30 19:14:33 -07001243 sci_sas_addr = oem->phys[index].sas_address.high;
Dan Williams4b339812011-05-06 17:36:38 -07001244 sci_sas_addr <<= 32;
Dan Williams89a73012011-06-30 19:14:33 -07001245 sci_sas_addr |= oem->phys[index].sas_address.low;
Dan Williams4b339812011-05-06 17:36:38 -07001246 sas_addr = cpu_to_be64(sci_sas_addr);
1247 memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
Dan Williams6f231dd2011-07-02 22:56:22 -07001248
Dan Williams4b339812011-05-06 17:36:38 -07001249 iphy->isci_port = NULL;
1250 iphy->sas_phy.enabled = 0;
1251 iphy->sas_phy.id = index;
1252 iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
1253 iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
1254 iphy->sas_phy.ha = &ihost->sas_ha;
1255 iphy->sas_phy.lldd_phy = iphy;
1256 iphy->sas_phy.enabled = 1;
1257 iphy->sas_phy.class = SAS;
1258 iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
1259 iphy->sas_phy.tproto = 0;
1260 iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
1261 iphy->sas_phy.role = PHY_ROLE_INITIATOR;
1262 iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
1263 iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
1264 memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
Dan Williams6f231dd2011-07-02 22:56:22 -07001265}
1266
1267
1268/**
1269 * isci_phy_control() - This function is one of the SAS Domain Template
1270 * functions. This is a phy management function.
1271 * @phy: This parameter specifies the sphy being controlled.
1272 * @func: This parameter specifies the phy control function being invoked.
1273 * @buf: This parameter is specific to the phy function being invoked.
1274 *
1275 * status, zero indicates success.
1276 */
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001277int isci_phy_control(struct asd_sas_phy *sas_phy,
1278 enum phy_func func,
1279 void *buf)
Dan Williams6f231dd2011-07-02 22:56:22 -07001280{
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001281 int ret = 0;
1282 struct isci_phy *iphy = sas_phy->lldd_phy;
1283 struct isci_port *iport = iphy->isci_port;
1284 struct isci_host *ihost = sas_phy->ha->lldd_ha;
1285 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001286
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001287 dev_dbg(&ihost->pdev->dev,
1288 "%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
1289 __func__, sas_phy, func, buf, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001290
1291 switch (func) {
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001292 case PHY_FUNC_DISABLE:
1293 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001294 sci_phy_stop(iphy);
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001295 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1296 break;
1297
Dan Williams6f231dd2011-07-02 22:56:22 -07001298 case PHY_FUNC_LINK_RESET:
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001299 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001300 sci_phy_stop(iphy);
1301 sci_phy_start(iphy);
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001302 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1303 break;
1304
1305 case PHY_FUNC_HARD_RESET:
1306 if (!iport)
1307 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001308
1309 /* Perform the port reset. */
Dan Williams4393aa42011-03-31 13:10:44 -07001310 ret = isci_port_perform_hard_reset(ihost, iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -07001311
1312 break;
Dan Williamsac013ed12011-09-28 18:48:02 -07001313 case PHY_FUNC_GET_EVENTS: {
1314 struct scu_link_layer_registers __iomem *r;
1315 struct sas_phy *phy = sas_phy->phy;
1316
1317 r = iphy->link_layer_registers;
1318 phy->running_disparity_error_count = readl(&r->running_disparity_error_count);
1319 phy->loss_of_dword_sync_count = readl(&r->loss_of_sync_error_count);
1320 phy->phy_reset_problem_count = readl(&r->phy_reset_problem_count);
1321 phy->invalid_dword_count = readl(&r->invalid_dword_counter);
1322 break;
1323 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001324
Dan Williams6f231dd2011-07-02 22:56:22 -07001325 default:
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001326 dev_dbg(&ihost->pdev->dev,
1327 "%s: phy %p; func %d NOT IMPLEMENTED!\n",
1328 __func__, sas_phy, func);
1329 ret = -ENOSYS;
Dan Williams6f231dd2011-07-02 22:56:22 -07001330 break;
1331 }
1332 return ret;
1333}