blob: 0df9f713f487c497f35771c45cf1dbbbfaa4e4d2 [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,
94 struct scu_link_layer_registers __iomem *reg)
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 Williams85280952011-06-28 15:05:53 -070097 int phy_idx = iphy->phy_index;
Dan Williams89a73012011-06-30 19:14:33 -070098 struct sci_phy_user_params *phy_user = &ihost->user_parameters.phys[phy_idx];
Dan Williamsd35bc1b2011-05-10 02:28:45 -070099 struct sci_phy_oem_params *phy_oem =
Dan Williams89a73012011-06-30 19:14:33 -0700100 &ihost->oem_parameters.phys[phy_idx];
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700101 u32 phy_configuration;
Dan Williams89a73012011-06-30 19:14:33 -0700102 struct sci_phy_cap phy_cap;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700103 u32 parity_check = 0;
104 u32 parity_count = 0;
105 u32 llctl, link_rate;
106 u32 clksm_value = 0;
107
Dan Williams89a73012011-06-30 19:14:33 -0700108 iphy->link_layer_registers = reg;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700109
110 /* Set our IDENTIFY frame data */
111 #define SCI_END_DEVICE 0x01
112
113 writel(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR) |
114 SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR) |
115 SCU_SAS_TIID_GEN_BIT(STP_INITIATOR) |
116 SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST) |
117 SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE),
Dan Williams85280952011-06-28 15:05:53 -0700118 &iphy->link_layer_registers->transmit_identification);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700119
120 /* Write the device SAS Address */
121 writel(0xFEDCBA98,
Dan Williams85280952011-06-28 15:05:53 -0700122 &iphy->link_layer_registers->sas_device_name_high);
123 writel(phy_idx, &iphy->link_layer_registers->sas_device_name_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700124
125 /* Write the source SAS Address */
126 writel(phy_oem->sas_address.high,
Dan Williams85280952011-06-28 15:05:53 -0700127 &iphy->link_layer_registers->source_sas_address_high);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700128 writel(phy_oem->sas_address.low,
Dan Williams85280952011-06-28 15:05:53 -0700129 &iphy->link_layer_registers->source_sas_address_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700130
131 /* Clear and Set the PHY Identifier */
Dan Williams85280952011-06-28 15:05:53 -0700132 writel(0, &iphy->link_layer_registers->identify_frame_phy_id);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700133 writel(SCU_SAS_TIPID_GEN_VALUE(ID, phy_idx),
Dan Williams85280952011-06-28 15:05:53 -0700134 &iphy->link_layer_registers->identify_frame_phy_id);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700135
136 /* Change the initial state of the phy configuration register */
137 phy_configuration =
Dan Williams85280952011-06-28 15:05:53 -0700138 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700139
140 /* Hold OOB state machine in reset */
141 phy_configuration |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
142 writel(phy_configuration,
Dan Williams85280952011-06-28 15:05:53 -0700143 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700144
145 /* Configure the SNW capabilities */
146 phy_cap.all = 0;
147 phy_cap.start = 1;
148 phy_cap.gen3_no_ssc = 1;
149 phy_cap.gen2_no_ssc = 1;
150 phy_cap.gen1_no_ssc = 1;
Dan Williams89a73012011-06-30 19:14:33 -0700151 if (ihost->oem_parameters.controller.do_enable_ssc == true) {
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700152 phy_cap.gen3_ssc = 1;
153 phy_cap.gen2_ssc = 1;
154 phy_cap.gen1_ssc = 1;
155 }
156
157 /*
158 * The SAS specification indicates that the phy_capabilities that
159 * are transmitted shall have an even parity. Calculate the parity. */
160 parity_check = phy_cap.all;
161 while (parity_check != 0) {
162 if (parity_check & 0x1)
163 parity_count++;
164 parity_check >>= 1;
165 }
166
167 /*
168 * If parity indicates there are an odd number of bits set, then
169 * set the parity bit to 1 in the phy capabilities. */
170 if ((parity_count % 2) != 0)
171 phy_cap.parity = 1;
172
Dan Williams85280952011-06-28 15:05:53 -0700173 writel(phy_cap.all, &iphy->link_layer_registers->phy_capabilities);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700174
175 /* Set the enable spinup period but disable the ability to send
176 * notify enable spinup
177 */
178 writel(SCU_ENSPINUP_GEN_VAL(COUNT,
179 phy_user->notify_enable_spin_up_insertion_frequency),
Dan Williams85280952011-06-28 15:05:53 -0700180 &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700181
182 /* Write the ALIGN Insertion Ferequency for connected phy and
183 * inpendent of connected state
184 */
185 clksm_value = SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(CONNECTED,
186 phy_user->in_connection_align_insertion_frequency);
187
188 clksm_value |= SCU_ALIGN_INSERTION_FREQUENCY_GEN_VAL(GENERAL,
189 phy_user->align_insertion_frequency);
190
Dan Williams85280952011-06-28 15:05:53 -0700191 writel(clksm_value, &iphy->link_layer_registers->clock_skew_management);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700192
193 /* @todo Provide a way to write this register correctly */
194 writel(0x02108421,
Dan Williams85280952011-06-28 15:05:53 -0700195 &iphy->link_layer_registers->afe_lookup_table_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700196
197 llctl = SCU_SAS_LLCTL_GEN_VAL(NO_OUTBOUND_TASK_TIMEOUT,
Dan Williams89a73012011-06-30 19:14:33 -0700198 (u8)ihost->user_parameters.no_outbound_task_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700199
200 switch(phy_user->max_speed_generation) {
201 case SCIC_SDS_PARM_GEN3_SPEED:
202 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3;
203 break;
204 case SCIC_SDS_PARM_GEN2_SPEED:
205 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2;
206 break;
207 default:
208 link_rate = SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1;
209 break;
210 }
211 llctl |= SCU_SAS_LLCTL_GEN_VAL(MAX_LINK_RATE, link_rate);
Dan Williams85280952011-06-28 15:05:53 -0700212 writel(llctl, &iphy->link_layer_registers->link_layer_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700213
214 if (is_a0() || is_a2()) {
215 /* Program the max ARB time for the PHY to 700us so we inter-operate with
216 * the PMC expander which shuts down PHYs if the expander PHY generates too
217 * many breaks. This time value will guarantee that the initiator PHY will
218 * generate the break.
219 */
220 writel(SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME,
Dan Williams85280952011-06-28 15:05:53 -0700221 &iphy->link_layer_registers->maximum_arbitration_wait_timer_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700222 }
223
Jeff Skirvin9b917982011-06-20 14:09:31 -0700224 /* Disable link layer hang detection, rely on the OS timeout for I/O timeouts. */
Dan Williams85280952011-06-28 15:05:53 -0700225 writel(0, &iphy->link_layer_registers->link_layer_hang_detection_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700226
227 /* We can exit the initial state to the stopped state */
Dan Williams85280952011-06-28 15:05:53 -0700228 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700229
230 return SCI_SUCCESS;
231}
232
Edmund Nadolskia628d472011-05-19 11:59:36 +0000233static void phy_sata_timeout(unsigned long data)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700234{
Edmund Nadolskia628d472011-05-19 11:59:36 +0000235 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williams85280952011-06-28 15:05:53 -0700236 struct isci_phy *iphy = container_of(tmr, typeof(*iphy), sata_timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700237 struct isci_host *ihost = iphy->owning_port->owning_controller;
Edmund Nadolskia628d472011-05-19 11:59:36 +0000238 unsigned long flags;
239
240 spin_lock_irqsave(&ihost->scic_lock, flags);
241
242 if (tmr->cancel)
243 goto done;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700244
Dan Williams85280952011-06-28 15:05:53 -0700245 dev_dbg(sciphy_to_dev(iphy),
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700246 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
247 "timeout.\n",
248 __func__,
Dan Williams85280952011-06-28 15:05:53 -0700249 iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700250
Dan Williams85280952011-06-28 15:05:53 -0700251 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Edmund Nadolskia628d472011-05-19 11:59:36 +0000252done:
253 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700254}
255
256/**
257 * This method returns the port currently containing this phy. If the phy is
258 * currently contained by the dummy port, then the phy is considered to not
259 * be part of a port.
260 * @sci_phy: This parameter specifies the phy for which to retrieve the
261 * containing port.
262 *
263 * This method returns a handle to a port that contains the supplied phy.
264 * NULL This value is returned if the phy is not part of a real
265 * port (i.e. it's contained in the dummy port). !NULL All other
266 * values indicate a handle/pointer to the port containing the phy.
267 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700268struct isci_port *phy_get_non_dummy_port(
Dan Williams85280952011-06-28 15:05:53 -0700269 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700270{
Dan Williams89a73012011-06-30 19:14:33 -0700271 if (sci_port_get_index(iphy->owning_port) == SCIC_SDS_DUMMY_PORT)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700272 return NULL;
273
Dan Williams85280952011-06-28 15:05:53 -0700274 return iphy->owning_port;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700275}
276
277/**
278 * This method will assign a port to the phy object.
Dan Williams85280952011-06-28 15:05:53 -0700279 * @out]: iphy This parameter specifies the phy for which to assign a port
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700280 * object.
281 *
282 *
283 */
Dan Williams89a73012011-06-30 19:14:33 -0700284void sci_phy_set_port(
Dan Williams85280952011-06-28 15:05:53 -0700285 struct isci_phy *iphy,
Dan Williamsffe191c2011-06-29 13:09:25 -0700286 struct isci_port *iport)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700287{
Dan Williamsffe191c2011-06-29 13:09:25 -0700288 iphy->owning_port = iport;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700289
Dan Williams85280952011-06-28 15:05:53 -0700290 if (iphy->bcn_received_while_port_unassigned) {
291 iphy->bcn_received_while_port_unassigned = false;
Dan Williams89a73012011-06-30 19:14:33 -0700292 sci_port_broadcast_change_received(iphy->owning_port, iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700293 }
294}
295
Dan Williams89a73012011-06-30 19:14:33 -0700296enum sci_status sci_phy_initialize(struct isci_phy *iphy,
297 struct scu_transport_layer_registers __iomem *tl,
298 struct scu_link_layer_registers __iomem *ll)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700299{
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700300 /* Perfrom the initialization of the TL hardware */
Dan Williams89a73012011-06-30 19:14:33 -0700301 sci_phy_transport_layer_initialization(iphy, tl);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700302
303 /* Perofrm the initialization of the PE hardware */
Dan Williams89a73012011-06-30 19:14:33 -0700304 sci_phy_link_layer_initialization(iphy, ll);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700305
Dan Williams89a73012011-06-30 19:14:33 -0700306 /* There is nothing that needs to be done in this state just
307 * transition to the stopped state
308 */
Dan Williams85280952011-06-28 15:05:53 -0700309 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700310
311 return SCI_SUCCESS;
312}
313
314/**
315 * This method assigns the direct attached device ID for this phy.
316 *
Dan Williams85280952011-06-28 15:05:53 -0700317 * @iphy The phy for which the direct attached device id is to
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700318 * be assigned.
319 * @device_id The direct attached device ID to assign to the phy.
320 * This will either be the RNi for the device or an invalid RNi if there
321 * is no current device assigned to the phy.
322 */
Dan Williams89a73012011-06-30 19:14:33 -0700323void sci_phy_setup_transport(struct isci_phy *iphy, u32 device_id)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700324{
325 u32 tl_control;
326
Dan Williams85280952011-06-28 15:05:53 -0700327 writel(device_id, &iphy->transport_layer_registers->stp_rni);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700328
329 /*
330 * The read should guarantee that the first write gets posted
331 * before the next write
332 */
Dan Williams85280952011-06-28 15:05:53 -0700333 tl_control = readl(&iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700334 tl_control |= SCU_TLCR_GEN_BIT(CLEAR_TCI_NCQ_MAPPING_TABLE);
Dan Williams85280952011-06-28 15:05:53 -0700335 writel(tl_control, &iphy->transport_layer_registers->control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700336}
337
Dan Williams89a73012011-06-30 19:14:33 -0700338static void sci_phy_suspend(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700339{
340 u32 scu_sas_pcfg_value;
341
342 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700343 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700344 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
345 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700346 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700347
Dan Williams89a73012011-06-30 19:14:33 -0700348 sci_phy_setup_transport(iphy, SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700349}
350
Dan Williams89a73012011-06-30 19:14:33 -0700351void sci_phy_resume(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700352{
353 u32 scu_sas_pcfg_value;
354
355 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700356 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700357 scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);
358 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700359 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700360}
361
Dan Williams89a73012011-06-30 19:14:33 -0700362void sci_phy_get_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700363{
Dan Williams89a73012011-06-30 19:14:33 -0700364 sas->high = readl(&iphy->link_layer_registers->source_sas_address_high);
365 sas->low = readl(&iphy->link_layer_registers->source_sas_address_low);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700366}
367
Dan Williams89a73012011-06-30 19:14:33 -0700368void sci_phy_get_attached_sas_address(struct isci_phy *iphy, struct sci_sas_address *sas)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700369{
370 struct sas_identify_frame *iaf;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700371
372 iaf = &iphy->frame_rcvd.iaf;
Dan Williams89a73012011-06-30 19:14:33 -0700373 memcpy(sas, iaf->sas_addr, SAS_ADDR_SIZE);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700374}
375
Dan Williams89a73012011-06-30 19:14:33 -0700376void sci_phy_get_protocols(struct isci_phy *iphy, struct sci_phy_proto *proto)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700377{
Dan Williams89a73012011-06-30 19:14:33 -0700378 proto->all = readl(&iphy->link_layer_registers->transmit_identification);
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700379}
380
Dan Williams89a73012011-06-30 19:14:33 -0700381enum sci_status sci_phy_start(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700382{
Dan Williams89a73012011-06-30 19:14:33 -0700383 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams966699b2011-05-12 03:44:24 -0700384
Edmund Nadolskie3013702011-06-02 00:10:43 +0000385 if (state != SCI_PHY_STOPPED) {
Dan Williams85280952011-06-28 15:05:53 -0700386 dev_dbg(sciphy_to_dev(iphy),
Dan Williams966699b2011-05-12 03:44:24 -0700387 "%s: in wrong state: %d\n", __func__, state);
388 return SCI_FAILURE_INVALID_STATE;
389 }
390
Dan Williams85280952011-06-28 15:05:53 -0700391 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams966699b2011-05-12 03:44:24 -0700392 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700393}
394
Dan Williams89a73012011-06-30 19:14:33 -0700395enum sci_status sci_phy_stop(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 Williams93153232011-05-12 04:01:03 -0700398
399 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000400 case SCI_PHY_SUB_INITIAL:
401 case SCI_PHY_SUB_AWAIT_OSSP_EN:
402 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
403 case SCI_PHY_SUB_AWAIT_SAS_POWER:
404 case SCI_PHY_SUB_AWAIT_SATA_POWER:
405 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
406 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
407 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
408 case SCI_PHY_SUB_FINAL:
409 case SCI_PHY_READY:
Dan Williams93153232011-05-12 04:01:03 -0700410 break;
411 default:
Dan Williams85280952011-06-28 15:05:53 -0700412 dev_dbg(sciphy_to_dev(iphy),
Dan Williams93153232011-05-12 04:01:03 -0700413 "%s: in wrong state: %d\n", __func__, state);
414 return SCI_FAILURE_INVALID_STATE;
415 }
416
Dan Williams85280952011-06-28 15:05:53 -0700417 sci_change_state(&iphy->sm, SCI_PHY_STOPPED);
Dan Williams93153232011-05-12 04:01:03 -0700418 return SCI_SUCCESS;
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700419}
420
Dan Williams89a73012011-06-30 19:14:33 -0700421enum sci_status sci_phy_reset(struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700422{
Dan Williams89a73012011-06-30 19:14:33 -0700423 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams0cf36fa2011-05-12 04:02:07 -0700424
Edmund Nadolskie3013702011-06-02 00:10:43 +0000425 if (state != SCI_PHY_READY) {
Dan Williams85280952011-06-28 15:05:53 -0700426 dev_dbg(sciphy_to_dev(iphy),
Dan Williams0cf36fa2011-05-12 04:02:07 -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_RESETTING);
Dan Williams0cf36fa2011-05-12 04:02:07 -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_consume_power_handler(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 Williams5b1d4af2011-05-12 04:51:41 -0700438
439 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000440 case SCI_PHY_SUB_AWAIT_SAS_POWER: {
Dan Williams5b1d4af2011-05-12 04:51:41 -0700441 u32 enable_spinup;
442
Dan Williams85280952011-06-28 15:05:53 -0700443 enable_spinup = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700444 enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
Dan Williams85280952011-06-28 15:05:53 -0700445 writel(enable_spinup, &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700446
447 /* Change state to the final state this substate machine has run to completion */
Dan Williams85280952011-06-28 15:05:53 -0700448 sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700449
450 return SCI_SUCCESS;
451 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000452 case SCI_PHY_SUB_AWAIT_SATA_POWER: {
Dan Williams5b1d4af2011-05-12 04:51:41 -0700453 u32 scu_sas_pcfg_value;
454
455 /* Release the spinup hold state and reset the OOB state machine */
456 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -0700457 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700458 scu_sas_pcfg_value &=
459 ~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
460 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
461 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700462 &iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700463
464 /* Now restart the OOB operation */
465 scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
466 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
467 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -0700468 &iphy->link_layer_registers->phy_configuration);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700469
470 /* Change state to the final state this substate machine has run to completion */
Dan Williams85280952011-06-28 15:05:53 -0700471 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_PHY_EN);
Dan Williams5b1d4af2011-05-12 04:51:41 -0700472
473 return SCI_SUCCESS;
474 }
475 default:
Dan Williams85280952011-06-28 15:05:53 -0700476 dev_dbg(sciphy_to_dev(iphy),
Dan Williams5b1d4af2011-05-12 04:51:41 -0700477 "%s: in wrong state: %d\n", __func__, state);
478 return SCI_FAILURE_INVALID_STATE;
479 }
Dan Williams23506a62011-05-12 04:27:29 -0700480}
481
Dan Williams89a73012011-06-30 19:14:33 -0700482static void sci_phy_start_sas_link_training(struct isci_phy *iphy)
Dan Williams23506a62011-05-12 04:27:29 -0700483{
Dan Williams89a73012011-06-30 19:14:33 -0700484 /* continue the link training for the phy as if it were a SAS PHY
485 * instead of a SATA PHY. This is done because the completion queue had a SAS
486 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
487 */
Dan Williams23506a62011-05-12 04:27:29 -0700488 u32 phy_control;
489
Dan Williams89a73012011-06-30 19:14:33 -0700490 phy_control = readl(&iphy->link_layer_registers->phy_configuration);
Dan Williams23506a62011-05-12 04:27:29 -0700491 phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
492 writel(phy_control,
Dan Williams89a73012011-06-30 19:14:33 -0700493 &iphy->link_layer_registers->phy_configuration);
Dan Williams23506a62011-05-12 04:27:29 -0700494
Dan Williams85280952011-06-28 15:05:53 -0700495 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SAS_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700496
Dan Williams85280952011-06-28 15:05:53 -0700497 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
Dan Williams23506a62011-05-12 04:27:29 -0700498}
499
Dan Williams89a73012011-06-30 19:14:33 -0700500static void sci_phy_start_sata_link_training(struct isci_phy *iphy)
Dan Williams23506a62011-05-12 04:27:29 -0700501{
Dan Williams89a73012011-06-30 19:14:33 -0700502 /* This method continues the link training for the phy as if it were a SATA PHY
503 * instead of a SAS PHY. This is done because the completion queue had a SATA
504 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
505 */
Dan Williams85280952011-06-28 15:05:53 -0700506 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_POWER);
Dan Williams23506a62011-05-12 04:27:29 -0700507
Dan Williams85280952011-06-28 15:05:53 -0700508 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
Dan Williams23506a62011-05-12 04:27:29 -0700509}
510
511/**
Dan Williams89a73012011-06-30 19:14:33 -0700512 * sci_phy_complete_link_training - perform processing common to
Dan Williams23506a62011-05-12 04:27:29 -0700513 * all protocols upon completion of link training.
514 * @sci_phy: This parameter specifies the phy object for which link training
515 * has completed.
516 * @max_link_rate: This parameter specifies the maximum link rate to be
517 * associated with this phy.
518 * @next_state: This parameter specifies the next state for the phy's starting
519 * sub-state machine.
520 *
521 */
Dan Williams89a73012011-06-30 19:14:33 -0700522static void sci_phy_complete_link_training(struct isci_phy *iphy,
523 enum sas_linkrate max_link_rate,
524 u32 next_state)
Dan Williams23506a62011-05-12 04:27:29 -0700525{
Dan Williams85280952011-06-28 15:05:53 -0700526 iphy->max_negotiated_speed = max_link_rate;
Dan Williams23506a62011-05-12 04:27:29 -0700527
Dan Williams85280952011-06-28 15:05:53 -0700528 sci_change_state(&iphy->sm, next_state);
Dan Williams23506a62011-05-12 04:27:29 -0700529}
530
Dan Williams89a73012011-06-30 19:14:33 -0700531enum sci_status sci_phy_event_handler(struct isci_phy *iphy, u32 event_code)
Dan Williams23506a62011-05-12 04:27:29 -0700532{
Dan Williams89a73012011-06-30 19:14:33 -0700533 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williams23506a62011-05-12 04:27:29 -0700534
535 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000536 case SCI_PHY_SUB_AWAIT_OSSP_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700537 switch (scu_get_event_code(event_code)) {
538 case SCU_EVENT_SAS_PHY_DETECTED:
Dan Williams89a73012011-06-30 19:14:33 -0700539 sci_phy_start_sas_link_training(iphy);
Dan Williams85280952011-06-28 15:05:53 -0700540 iphy->is_in_link_training = true;
Dan Williams23506a62011-05-12 04:27:29 -0700541 break;
542 case SCU_EVENT_SATA_SPINUP_HOLD:
Dan Williams89a73012011-06-30 19:14:33 -0700543 sci_phy_start_sata_link_training(iphy);
Dan Williams85280952011-06-28 15:05:53 -0700544 iphy->is_in_link_training = true;
Dan Williams23506a62011-05-12 04:27:29 -0700545 break;
546 default:
Dan Williams85280952011-06-28 15:05:53 -0700547 dev_dbg(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700548 "%s: PHY starting substate machine received "
549 "unexpected event_code %x\n",
550 __func__,
551 event_code);
552 return SCI_FAILURE;
553 }
554 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000555 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700556 switch (scu_get_event_code(event_code)) {
557 case SCU_EVENT_SAS_PHY_DETECTED:
558 /*
559 * Why is this being reported again by the controller?
560 * We would re-enter this state so just stay here */
561 break;
562 case SCU_EVENT_SAS_15:
563 case SCU_EVENT_SAS_15_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700564 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
565 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700566 break;
567 case SCU_EVENT_SAS_30:
568 case SCU_EVENT_SAS_30_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700569 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
570 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700571 break;
572 case SCU_EVENT_SAS_60:
573 case SCU_EVENT_SAS_60_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700574 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
575 SCI_PHY_SUB_AWAIT_IAF_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700576 break;
577 case SCU_EVENT_SATA_SPINUP_HOLD:
578 /*
579 * We were doing SAS PHY link training and received a SATA PHY event
580 * continue OOB/SN as if this were a SATA PHY */
Dan Williams89a73012011-06-30 19:14:33 -0700581 sci_phy_start_sata_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700582 break;
583 case SCU_EVENT_LINK_FAILURE:
584 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700585 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700586 break;
587 default:
Dan Williams85280952011-06-28 15:05:53 -0700588 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700589 "%s: PHY starting substate machine received "
590 "unexpected event_code %x\n",
591 __func__, event_code);
592
593 return SCI_FAILURE;
594 break;
595 }
596 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000597 case SCI_PHY_SUB_AWAIT_IAF_UF:
Dan Williams23506a62011-05-12 04:27:29 -0700598 switch (scu_get_event_code(event_code)) {
599 case SCU_EVENT_SAS_PHY_DETECTED:
600 /* Backup the state machine */
Dan Williams89a73012011-06-30 19:14:33 -0700601 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700602 break;
603 case SCU_EVENT_SATA_SPINUP_HOLD:
604 /* We were doing SAS PHY link training and received a
605 * SATA PHY event continue OOB/SN as if this were a
606 * SATA PHY
607 */
Dan Williams89a73012011-06-30 19:14:33 -0700608 sci_phy_start_sata_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700609 break;
610 case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
611 case SCU_EVENT_LINK_FAILURE:
612 case SCU_EVENT_HARD_RESET_RECEIVED:
613 /* Start the oob/sn state machine over again */
Dan Williams85280952011-06-28 15:05:53 -0700614 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700615 break;
616 default:
Dan Williams85280952011-06-28 15:05:53 -0700617 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700618 "%s: PHY starting substate machine received "
619 "unexpected event_code %x\n",
620 __func__, event_code);
621 return SCI_FAILURE;
622 }
623 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000624 case SCI_PHY_SUB_AWAIT_SAS_POWER:
Dan Williams23506a62011-05-12 04:27:29 -0700625 switch (scu_get_event_code(event_code)) {
626 case SCU_EVENT_LINK_FAILURE:
627 /* Link failure change state back to the starting state */
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 unexpected "
633 "event_code %x\n",
634 __func__,
635 event_code);
636 return SCI_FAILURE;
637 }
638 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000639 case SCI_PHY_SUB_AWAIT_SATA_POWER:
Dan Williams23506a62011-05-12 04:27:29 -0700640 switch (scu_get_event_code(event_code)) {
641 case SCU_EVENT_LINK_FAILURE:
642 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700643 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700644 break;
645 case SCU_EVENT_SATA_SPINUP_HOLD:
646 /* These events are received every 10ms and are
647 * expected while in this state
648 */
649 break;
650
651 case SCU_EVENT_SAS_PHY_DETECTED:
652 /* There has been a change in the phy type before OOB/SN for the
653 * SATA finished start down the SAS link traning path.
654 */
Dan Williams89a73012011-06-30 19:14:33 -0700655 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700656 break;
657
658 default:
Dan Williams85280952011-06-28 15:05:53 -0700659 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700660 "%s: PHY starting substate machine received "
661 "unexpected event_code %x\n",
662 __func__, event_code);
663
664 return SCI_FAILURE;
665 }
666 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000667 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700668 switch (scu_get_event_code(event_code)) {
669 case SCU_EVENT_LINK_FAILURE:
670 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700671 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700672 break;
673 case SCU_EVENT_SATA_SPINUP_HOLD:
674 /* These events might be received since we dont know how many may be in
675 * the completion queue while waiting for power
676 */
677 break;
678 case SCU_EVENT_SATA_PHY_DETECTED:
Dan Williams85280952011-06-28 15:05:53 -0700679 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
Dan Williams23506a62011-05-12 04:27:29 -0700680
681 /* We have received the SATA PHY notification change state */
Dan Williams85280952011-06-28 15:05:53 -0700682 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700683 break;
684 case SCU_EVENT_SAS_PHY_DETECTED:
685 /* There has been a change in the phy type before OOB/SN for the
686 * SATA finished start down the SAS link traning path.
687 */
Dan Williams89a73012011-06-30 19:14:33 -0700688 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700689 break;
690 default:
Dan Williams85280952011-06-28 15:05:53 -0700691 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700692 "%s: PHY starting substate machine received "
693 "unexpected event_code %x\n",
694 __func__,
695 event_code);
696
697 return SCI_FAILURE;;
698 }
699 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000700 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
Dan Williams23506a62011-05-12 04:27:29 -0700701 switch (scu_get_event_code(event_code)) {
702 case SCU_EVENT_SATA_PHY_DETECTED:
703 /*
704 * The hardware reports multiple SATA PHY detected events
705 * ignore the extras */
706 break;
707 case SCU_EVENT_SATA_15:
708 case SCU_EVENT_SATA_15_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700709 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_1_5_GBPS,
710 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700711 break;
712 case SCU_EVENT_SATA_30:
713 case SCU_EVENT_SATA_30_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700714 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_3_0_GBPS,
715 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700716 break;
717 case SCU_EVENT_SATA_60:
718 case SCU_EVENT_SATA_60_SSC:
Dan Williams89a73012011-06-30 19:14:33 -0700719 sci_phy_complete_link_training(iphy, SAS_LINK_RATE_6_0_GBPS,
720 SCI_PHY_SUB_AWAIT_SIG_FIS_UF);
Dan Williams23506a62011-05-12 04:27:29 -0700721 break;
722 case SCU_EVENT_LINK_FAILURE:
723 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700724 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700725 break;
726 case SCU_EVENT_SAS_PHY_DETECTED:
727 /*
728 * There has been a change in the phy type before OOB/SN for the
729 * SATA finished start down the SAS link traning path. */
Dan Williams89a73012011-06-30 19:14:33 -0700730 sci_phy_start_sas_link_training(iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700731 break;
732 default:
Dan Williams85280952011-06-28 15:05:53 -0700733 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700734 "%s: PHY starting substate machine received "
735 "unexpected event_code %x\n",
736 __func__, event_code);
737
738 return SCI_FAILURE;
739 }
740
741 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000742 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
Dan Williams23506a62011-05-12 04:27:29 -0700743 switch (scu_get_event_code(event_code)) {
744 case SCU_EVENT_SATA_PHY_DETECTED:
745 /* Backup the state machine */
Dan Williams85280952011-06-28 15:05:53 -0700746 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_SATA_SPEED_EN);
Dan Williams23506a62011-05-12 04:27:29 -0700747 break;
748
749 case SCU_EVENT_LINK_FAILURE:
750 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700751 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700752 break;
753
754 default:
Dan Williams85280952011-06-28 15:05:53 -0700755 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700756 "%s: PHY starting substate machine received "
757 "unexpected event_code %x\n",
758 __func__,
759 event_code);
760
761 return SCI_FAILURE;
762 }
763 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000764 case SCI_PHY_READY:
Dan Williams23506a62011-05-12 04:27:29 -0700765 switch (scu_get_event_code(event_code)) {
766 case SCU_EVENT_LINK_FAILURE:
767 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700768 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williams23506a62011-05-12 04:27:29 -0700769 break;
770 case SCU_EVENT_BROADCAST_CHANGE:
771 /* Broadcast change received. Notify the port. */
Dan Williams85280952011-06-28 15:05:53 -0700772 if (phy_get_non_dummy_port(iphy) != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700773 sci_port_broadcast_change_received(iphy->owning_port, iphy);
Dan Williams23506a62011-05-12 04:27:29 -0700774 else
Dan Williams85280952011-06-28 15:05:53 -0700775 iphy->bcn_received_while_port_unassigned = true;
Dan Williams23506a62011-05-12 04:27:29 -0700776 break;
777 default:
Dan Williams85280952011-06-28 15:05:53 -0700778 dev_warn(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700779 "%sP SCIC PHY 0x%p ready state machine received "
780 "unexpected event_code %x\n",
Dan Williams85280952011-06-28 15:05:53 -0700781 __func__, iphy, event_code);
Dan Williams23506a62011-05-12 04:27:29 -0700782 return SCI_FAILURE_INVALID_STATE;
783 }
784 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000785 case SCI_PHY_RESETTING:
Dan Williams23506a62011-05-12 04:27:29 -0700786 switch (scu_get_event_code(event_code)) {
787 case SCU_EVENT_HARD_RESET_TRANSMITTED:
788 /* Link failure change state back to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700789 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
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 "%s: SCIC PHY 0x%p resetting 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
797 return SCI_FAILURE_INVALID_STATE;
798 break;
799 }
800 return SCI_SUCCESS;
801 default:
Dan Williams85280952011-06-28 15:05:53 -0700802 dev_dbg(sciphy_to_dev(iphy),
Dan Williams23506a62011-05-12 04:27:29 -0700803 "%s: in wrong state: %d\n", __func__, state);
804 return SCI_FAILURE_INVALID_STATE;
805 }
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700806}
807
Dan Williams89a73012011-06-30 19:14:33 -0700808enum sci_status sci_phy_frame_handler(struct isci_phy *iphy, u32 frame_index)
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700809{
Dan Williams89a73012011-06-30 19:14:33 -0700810 enum sci_phy_states state = iphy->sm.current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700811 struct isci_host *ihost = iphy->owning_port->owning_controller;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700812 enum sci_status result;
Dan Williams4cffe132011-06-23 23:44:52 -0700813 unsigned long flags;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700814
815 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000816 case SCI_PHY_SUB_AWAIT_IAF_UF: {
Dan Williamsc4441ab2011-05-12 04:17:51 -0700817 u32 *frame_words;
818 struct sas_identify_frame iaf;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700819
Dan Williams89a73012011-06-30 19:14:33 -0700820 result = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
821 frame_index,
822 (void **)&frame_words);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700823
824 if (result != SCI_SUCCESS)
825 return result;
826
827 sci_swab32_cpy(&iaf, frame_words, sizeof(iaf) / sizeof(u32));
828 if (iaf.frame_type == 0) {
829 u32 state;
830
Dan Williams4cffe132011-06-23 23:44:52 -0700831 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700832 memcpy(&iphy->frame_rcvd.iaf, &iaf, sizeof(iaf));
Dan Williams4cffe132011-06-23 23:44:52 -0700833 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700834 if (iaf.smp_tport) {
835 /* We got the IAF for an expander PHY go to the final
836 * state since there are no power requirements for
837 * expander phys.
838 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000839 state = SCI_PHY_SUB_FINAL;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700840 } else {
841 /* We got the IAF we can now go to the await spinup
842 * semaphore state
843 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000844 state = SCI_PHY_SUB_AWAIT_SAS_POWER;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700845 }
Dan Williams85280952011-06-28 15:05:53 -0700846 sci_change_state(&iphy->sm, state);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700847 result = SCI_SUCCESS;
848 } else
Dan Williams85280952011-06-28 15:05:53 -0700849 dev_warn(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700850 "%s: PHY starting substate machine received "
851 "unexpected frame id %x\n",
852 __func__, frame_index);
853
Dan Williams89a73012011-06-30 19:14:33 -0700854 sci_controller_release_frame(ihost, frame_index);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700855 return result;
856 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000857 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: {
Dan Williamsc4441ab2011-05-12 04:17:51 -0700858 struct dev_to_host_fis *frame_header;
859 u32 *fis_frame_data;
Dan Williamsc4441ab2011-05-12 04:17:51 -0700860
Dan Williams89a73012011-06-30 19:14:33 -0700861 result = sci_unsolicited_frame_control_get_header(
862 &(sci_phy_get_controller(iphy)->uf_control),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700863 frame_index,
864 (void **)&frame_header);
865
866 if (result != SCI_SUCCESS)
867 return result;
868
869 if ((frame_header->fis_type == FIS_REGD2H) &&
870 !(frame_header->status & ATA_BUSY)) {
Dan Williams89a73012011-06-30 19:14:33 -0700871 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
872 frame_index,
873 (void **)&fis_frame_data);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700874
Dan Williams4cffe132011-06-23 23:44:52 -0700875 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -0700876 sci_controller_copy_sata_response(&iphy->frame_rcvd.fis,
877 frame_header,
878 fis_frame_data);
Dan Williams4cffe132011-06-23 23:44:52 -0700879 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700880
881 /* got IAF we can now go to the await spinup semaphore state */
Dan Williams85280952011-06-28 15:05:53 -0700882 sci_change_state(&iphy->sm, SCI_PHY_SUB_FINAL);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700883
884 result = SCI_SUCCESS;
885 } else
Dan Williams85280952011-06-28 15:05:53 -0700886 dev_warn(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700887 "%s: PHY starting substate machine received "
888 "unexpected frame id %x\n",
889 __func__, frame_index);
890
891 /* Regardless of the result we are done with this frame with it */
Dan Williams89a73012011-06-30 19:14:33 -0700892 sci_controller_release_frame(ihost, frame_index);
Dan Williamsc4441ab2011-05-12 04:17:51 -0700893
894 return result;
895 }
896 default:
Dan Williams85280952011-06-28 15:05:53 -0700897 dev_dbg(sciphy_to_dev(iphy),
Dan Williamsc4441ab2011-05-12 04:17:51 -0700898 "%s: in wrong state: %d\n", __func__, state);
899 return SCI_FAILURE_INVALID_STATE;
900 }
Dan Williams5076a1a2011-06-27 14:57:03 -0700901
Dan Williamsd35bc1b2011-05-10 02:28:45 -0700902}
903
Dan Williams89a73012011-06-30 19:14:33 -0700904static void sci_phy_starting_initial_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000905{
Dan Williams85280952011-06-28 15:05:53 -0700906 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000907
Adam Gruchala4a33c522011-05-10 23:54:23 +0000908 /* This is just an temporary state go off to the starting state */
Dan Williams85280952011-06-28 15:05:53 -0700909 sci_change_state(&iphy->sm, SCI_PHY_SUB_AWAIT_OSSP_EN);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000910}
911
Dan Williams89a73012011-06-30 19:14:33 -0700912static void sci_phy_starting_await_sas_power_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000913{
Dan Williams85280952011-06-28 15:05:53 -0700914 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700915 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000916
Dan Williams89a73012011-06-30 19:14:33 -0700917 sci_controller_power_control_queue_insert(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000918}
919
Dan Williams89a73012011-06-30 19:14:33 -0700920static void sci_phy_starting_await_sas_power_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000921{
Dan Williams85280952011-06-28 15:05:53 -0700922 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700923 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000924
Dan Williams89a73012011-06-30 19:14:33 -0700925 sci_controller_power_control_queue_remove(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000926}
927
Dan Williams89a73012011-06-30 19:14:33 -0700928static void sci_phy_starting_await_sata_power_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000929{
Dan Williams85280952011-06-28 15:05:53 -0700930 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700931 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000932
Dan Williams89a73012011-06-30 19:14:33 -0700933 sci_controller_power_control_queue_insert(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000934}
935
Dan Williams89a73012011-06-30 19:14:33 -0700936static void sci_phy_starting_await_sata_power_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000937{
Dan Williams85280952011-06-28 15:05:53 -0700938 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700939 struct isci_host *ihost = iphy->owning_port->owning_controller;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000940
Dan Williams89a73012011-06-30 19:14:33 -0700941 sci_controller_power_control_queue_remove(ihost, iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000942}
943
Dan Williams89a73012011-06-30 19:14:33 -0700944static void sci_phy_starting_await_sata_phy_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000945{
Dan Williams85280952011-06-28 15:05:53 -0700946 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000947
Dan Williams85280952011-06-28 15:05:53 -0700948 sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000949}
950
Dan Williams89a73012011-06-30 19:14:33 -0700951static void sci_phy_starting_await_sata_phy_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000952{
Dan Williams85280952011-06-28 15:05:53 -0700953 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000954
Dan Williams85280952011-06-28 15:05:53 -0700955 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000956}
957
Dan Williams89a73012011-06-30 19:14:33 -0700958static void sci_phy_starting_await_sata_speed_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000959{
Dan Williams85280952011-06-28 15:05:53 -0700960 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000961
Dan Williams85280952011-06-28 15:05:53 -0700962 sci_mod_timer(&iphy->sata_timer, SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000963}
964
Dan Williams89a73012011-06-30 19:14:33 -0700965static void sci_phy_starting_await_sata_speed_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000966{
Dan Williams85280952011-06-28 15:05:53 -0700967 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000968
Dan Williams85280952011-06-28 15:05:53 -0700969 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000970}
971
Dan Williams89a73012011-06-30 19:14:33 -0700972static void sci_phy_starting_await_sig_fis_uf_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000973{
Dan Williams85280952011-06-28 15:05:53 -0700974 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000975
Dan Williams89a73012011-06-30 19:14:33 -0700976 if (sci_port_link_detected(iphy->owning_port, iphy)) {
Adam Gruchala4a33c522011-05-10 23:54:23 +0000977
Adam Gruchala4a33c522011-05-10 23:54:23 +0000978 /*
979 * Clear the PE suspend condition so we can actually
980 * receive SIG FIS
981 * The hardware will not respond to the XRDY until the PE
982 * suspend condition is cleared.
983 */
Dan Williams89a73012011-06-30 19:14:33 -0700984 sci_phy_resume(iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000985
Dan Williams85280952011-06-28 15:05:53 -0700986 sci_mod_timer(&iphy->sata_timer,
Edmund Nadolskia628d472011-05-19 11:59:36 +0000987 SCIC_SDS_SIGNATURE_FIS_TIMEOUT);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000988 } else
Dan Williams85280952011-06-28 15:05:53 -0700989 iphy->is_in_link_training = false;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000990}
991
Dan Williams89a73012011-06-30 19:14:33 -0700992static void sci_phy_starting_await_sig_fis_uf_substate_exit(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000993{
Dan Williams85280952011-06-28 15:05:53 -0700994 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000995
Dan Williams85280952011-06-28 15:05:53 -0700996 sci_del_timer(&iphy->sata_timer);
Adam Gruchala4a33c522011-05-10 23:54:23 +0000997}
998
Dan Williams89a73012011-06-30 19:14:33 -0700999static void sci_phy_starting_final_substate_enter(struct sci_base_state_machine *sm)
Adam Gruchala4a33c522011-05-10 23:54:23 +00001000{
Dan Williams85280952011-06-28 15:05:53 -07001001 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001002
Adam Gruchala4a33c522011-05-10 23:54:23 +00001003 /* State machine has run to completion so exit out and change
1004 * the base state machine to the ready state
1005 */
Dan Williams85280952011-06-28 15:05:53 -07001006 sci_change_state(&iphy->sm, SCI_PHY_READY);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001007}
1008
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001009/**
1010 *
Dan Williams85280952011-06-28 15:05:53 -07001011 * @sci_phy: This is the struct isci_phy object to stop.
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001012 *
Dan Williams85280952011-06-28 15:05:53 -07001013 * This method will stop the struct isci_phy object. This does not reset the
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001014 * protocol engine it just suspends it and places it in a state where it will
1015 * not cause the end device to power up. none
1016 */
1017static void scu_link_layer_stop_protocol_engine(
Dan Williams85280952011-06-28 15:05:53 -07001018 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001019{
1020 u32 scu_sas_pcfg_value;
1021 u32 enable_spinup_value;
1022
1023 /* Suspend the protocol engine and place it in a sata spinup hold state */
1024 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -07001025 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001026 scu_sas_pcfg_value |=
1027 (SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1028 SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE) |
1029 SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD));
1030 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -07001031 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001032
1033 /* Disable the notify enable spinup primitives */
Dan Williams85280952011-06-28 15:05:53 -07001034 enable_spinup_value = readl(&iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001035 enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
Dan Williams85280952011-06-28 15:05:53 -07001036 writel(enable_spinup_value, &iphy->link_layer_registers->notify_enable_spinup_control);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001037}
1038
1039/**
1040 *
1041 *
Dan Williams85280952011-06-28 15:05:53 -07001042 * This method will start the OOB/SN state machine for this struct isci_phy object.
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001043 */
1044static void scu_link_layer_start_oob(
Dan Williams85280952011-06-28 15:05:53 -07001045 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001046{
1047 u32 scu_sas_pcfg_value;
1048
1049 scu_sas_pcfg_value =
Dan Williams85280952011-06-28 15:05:53 -07001050 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001051 scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1052 scu_sas_pcfg_value &=
1053 ~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) |
1054 SCU_SAS_PCFG_GEN_BIT(HARD_RESET));
1055 writel(scu_sas_pcfg_value,
Dan Williams85280952011-06-28 15:05:53 -07001056 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001057}
1058
1059/**
1060 *
1061 *
1062 * This method will transmit a hard reset request on the specified phy. The SCU
1063 * hardware requires that we reset the OOB state machine and set the hard reset
1064 * bit in the phy configuration register. We then must start OOB over with the
1065 * hard reset bit set.
1066 */
1067static void scu_link_layer_tx_hard_reset(
Dan Williams85280952011-06-28 15:05:53 -07001068 struct isci_phy *iphy)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001069{
1070 u32 phy_configuration_value;
1071
1072 /*
1073 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
1074 * to the starting state. */
1075 phy_configuration_value =
Dan Williams85280952011-06-28 15:05:53 -07001076 readl(&iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001077 phy_configuration_value |=
1078 (SCU_SAS_PCFG_GEN_BIT(HARD_RESET) |
1079 SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
1080 writel(phy_configuration_value,
Dan Williams85280952011-06-28 15:05:53 -07001081 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001082
1083 /* Now take the OOB state machine out of reset */
1084 phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
1085 phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
1086 writel(phy_configuration_value,
Dan Williams85280952011-06-28 15:05:53 -07001087 &iphy->link_layer_registers->phy_configuration);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001088}
1089
Dan Williams89a73012011-06-30 19:14:33 -07001090static void sci_phy_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001091{
Dan Williams85280952011-06-28 15:05:53 -07001092 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001093
1094 /*
1095 * @todo We need to get to the controller to place this PE in a
1096 * reset state
1097 */
Dan Williams85280952011-06-28 15:05:53 -07001098 sci_del_timer(&iphy->sata_timer);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001099
Dan Williams85280952011-06-28 15:05:53 -07001100 scu_link_layer_stop_protocol_engine(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001101
Dan Williams85280952011-06-28 15:05:53 -07001102 if (iphy->sm.previous_state_id != SCI_PHY_INITIAL)
Dan Williams89a73012011-06-30 19:14:33 -07001103 sci_controller_link_down(sci_phy_get_controller(iphy),
Dan Williams85280952011-06-28 15:05:53 -07001104 phy_get_non_dummy_port(iphy),
1105 iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001106}
1107
Dan Williams89a73012011-06-30 19:14:33 -07001108static void sci_phy_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001109{
Dan Williams85280952011-06-28 15:05:53 -07001110 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001111
Dan Williams85280952011-06-28 15:05:53 -07001112 scu_link_layer_stop_protocol_engine(iphy);
1113 scu_link_layer_start_oob(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001114
1115 /* We don't know what kind of phy we are going to be just yet */
Dan Williams85280952011-06-28 15:05:53 -07001116 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
1117 iphy->bcn_received_while_port_unassigned = false;
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001118
Dan Williams85280952011-06-28 15:05:53 -07001119 if (iphy->sm.previous_state_id == SCI_PHY_READY)
Dan Williams89a73012011-06-30 19:14:33 -07001120 sci_controller_link_down(sci_phy_get_controller(iphy),
Dan Williams85280952011-06-28 15:05:53 -07001121 phy_get_non_dummy_port(iphy),
1122 iphy);
Adam Gruchala4a33c522011-05-10 23:54:23 +00001123
Dan Williams85280952011-06-28 15:05:53 -07001124 sci_change_state(&iphy->sm, SCI_PHY_SUB_INITIAL);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001125}
1126
Dan Williams89a73012011-06-30 19:14:33 -07001127static void sci_phy_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001128{
Dan Williams85280952011-06-28 15:05:53 -07001129 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001130
Dan Williams89a73012011-06-30 19:14:33 -07001131 sci_controller_link_up(sci_phy_get_controller(iphy),
Dan Williams85280952011-06-28 15:05:53 -07001132 phy_get_non_dummy_port(iphy),
1133 iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001134
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001135}
1136
Dan Williams89a73012011-06-30 19:14:33 -07001137static void sci_phy_ready_state_exit(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 Williamsd35bc1b2011-05-10 02:28:45 -07001140
Dan Williams89a73012011-06-30 19:14:33 -07001141 sci_phy_suspend(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001142}
1143
Dan Williams89a73012011-06-30 19:14:33 -07001144static void sci_phy_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001145{
Dan Williams85280952011-06-28 15:05:53 -07001146 struct isci_phy *iphy = container_of(sm, typeof(*iphy), sm);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001147
Dan Williams5b1d4af2011-05-12 04:51:41 -07001148 /* The phy is being reset, therefore deactivate it from the port. In
1149 * the resetting state we don't notify the user regarding link up and
1150 * link down notifications
1151 */
Dan Williams89a73012011-06-30 19:14:33 -07001152 sci_port_deactivate_phy(iphy->owning_port, iphy, false);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001153
Dan Williams85280952011-06-28 15:05:53 -07001154 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
1155 scu_link_layer_tx_hard_reset(iphy);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001156 } else {
Dan Williams5b1d4af2011-05-12 04:51:41 -07001157 /* The SCU does not need to have a discrete reset state so
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001158 * just go back to the starting state.
1159 */
Dan Williams85280952011-06-28 15:05:53 -07001160 sci_change_state(&iphy->sm, SCI_PHY_STARTING);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001161 }
1162}
1163
Dan Williams89a73012011-06-30 19:14:33 -07001164static const struct sci_base_state sci_phy_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001165 [SCI_PHY_INITIAL] = { },
1166 [SCI_PHY_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001167 .enter_state = sci_phy_stopped_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001168 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001169 [SCI_PHY_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001170 .enter_state = sci_phy_starting_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001171 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001172 [SCI_PHY_SUB_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001173 .enter_state = sci_phy_starting_initial_substate_enter,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001174 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001175 [SCI_PHY_SUB_AWAIT_OSSP_EN] = { },
1176 [SCI_PHY_SUB_AWAIT_SAS_SPEED_EN] = { },
1177 [SCI_PHY_SUB_AWAIT_IAF_UF] = { },
1178 [SCI_PHY_SUB_AWAIT_SAS_POWER] = {
Dan Williams89a73012011-06-30 19:14:33 -07001179 .enter_state = sci_phy_starting_await_sas_power_substate_enter,
1180 .exit_state = sci_phy_starting_await_sas_power_substate_exit,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001181 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001182 [SCI_PHY_SUB_AWAIT_SATA_POWER] = {
Dan Williams89a73012011-06-30 19:14:33 -07001183 .enter_state = sci_phy_starting_await_sata_power_substate_enter,
1184 .exit_state = sci_phy_starting_await_sata_power_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001185 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001186 [SCI_PHY_SUB_AWAIT_SATA_PHY_EN] = {
Dan Williams89a73012011-06-30 19:14:33 -07001187 .enter_state = sci_phy_starting_await_sata_phy_substate_enter,
1188 .exit_state = sci_phy_starting_await_sata_phy_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001189 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001190 [SCI_PHY_SUB_AWAIT_SATA_SPEED_EN] = {
Dan Williams89a73012011-06-30 19:14:33 -07001191 .enter_state = sci_phy_starting_await_sata_speed_substate_enter,
1192 .exit_state = sci_phy_starting_await_sata_speed_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001193 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001194 [SCI_PHY_SUB_AWAIT_SIG_FIS_UF] = {
Dan Williams89a73012011-06-30 19:14:33 -07001195 .enter_state = sci_phy_starting_await_sig_fis_uf_substate_enter,
1196 .exit_state = sci_phy_starting_await_sig_fis_uf_substate_exit
Adam Gruchala4a33c522011-05-10 23:54:23 +00001197 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001198 [SCI_PHY_SUB_FINAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001199 .enter_state = sci_phy_starting_final_substate_enter,
Adam Gruchala4a33c522011-05-10 23:54:23 +00001200 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001201 [SCI_PHY_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001202 .enter_state = sci_phy_ready_state_enter,
1203 .exit_state = sci_phy_ready_state_exit,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001204 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001205 [SCI_PHY_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001206 .enter_state = sci_phy_resetting_state_enter,
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001207 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001208 [SCI_PHY_FINAL] = { },
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001209};
1210
Dan Williams89a73012011-06-30 19:14:33 -07001211void sci_phy_construct(struct isci_phy *iphy,
Dan Williamsffe191c2011-06-29 13:09:25 -07001212 struct isci_port *iport, u8 phy_index)
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001213{
Dan Williams89a73012011-06-30 19:14:33 -07001214 sci_init_sm(&iphy->sm, sci_phy_state_table, SCI_PHY_INITIAL);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001215
1216 /* Copy the rest of the input data to our locals */
Dan Williamsffe191c2011-06-29 13:09:25 -07001217 iphy->owning_port = iport;
Dan Williams85280952011-06-28 15:05:53 -07001218 iphy->phy_index = phy_index;
1219 iphy->bcn_received_while_port_unassigned = false;
1220 iphy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
1221 iphy->link_layer_registers = NULL;
1222 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Edmund Nadolskia628d472011-05-19 11:59:36 +00001223
1224 /* Create the SIGNATURE FIS Timeout timer for this phy */
Dan Williams85280952011-06-28 15:05:53 -07001225 sci_init_timer(&iphy->sata_timer, phy_sata_timeout);
Dan Williamsd35bc1b2011-05-10 02:28:45 -07001226}
Dan Williams6f231dd2011-07-02 22:56:22 -07001227
Dan Williams4b339812011-05-06 17:36:38 -07001228void isci_phy_init(struct isci_phy *iphy, struct isci_host *ihost, int index)
Dan Williams6f231dd2011-07-02 22:56:22 -07001229{
Dan Williams89a73012011-06-30 19:14:33 -07001230 struct sci_oem_params *oem = &ihost->oem_parameters;
Dan Williams4b339812011-05-06 17:36:38 -07001231 u64 sci_sas_addr;
1232 __be64 sas_addr;
Dan Williams6f231dd2011-07-02 22:56:22 -07001233
Dan Williams89a73012011-06-30 19:14:33 -07001234 sci_sas_addr = oem->phys[index].sas_address.high;
Dan Williams4b339812011-05-06 17:36:38 -07001235 sci_sas_addr <<= 32;
Dan Williams89a73012011-06-30 19:14:33 -07001236 sci_sas_addr |= oem->phys[index].sas_address.low;
Dan Williams4b339812011-05-06 17:36:38 -07001237 sas_addr = cpu_to_be64(sci_sas_addr);
1238 memcpy(iphy->sas_addr, &sas_addr, sizeof(sas_addr));
Dan Williams6f231dd2011-07-02 22:56:22 -07001239
Dan Williams4b339812011-05-06 17:36:38 -07001240 iphy->isci_port = NULL;
1241 iphy->sas_phy.enabled = 0;
1242 iphy->sas_phy.id = index;
1243 iphy->sas_phy.sas_addr = &iphy->sas_addr[0];
1244 iphy->sas_phy.frame_rcvd = (u8 *)&iphy->frame_rcvd;
1245 iphy->sas_phy.ha = &ihost->sas_ha;
1246 iphy->sas_phy.lldd_phy = iphy;
1247 iphy->sas_phy.enabled = 1;
1248 iphy->sas_phy.class = SAS;
1249 iphy->sas_phy.iproto = SAS_PROTOCOL_ALL;
1250 iphy->sas_phy.tproto = 0;
1251 iphy->sas_phy.type = PHY_TYPE_PHYSICAL;
1252 iphy->sas_phy.role = PHY_ROLE_INITIATOR;
1253 iphy->sas_phy.oob_mode = OOB_NOT_CONNECTED;
1254 iphy->sas_phy.linkrate = SAS_LINK_RATE_UNKNOWN;
1255 memset(&iphy->frame_rcvd, 0, sizeof(iphy->frame_rcvd));
Dan Williams6f231dd2011-07-02 22:56:22 -07001256}
1257
1258
1259/**
1260 * isci_phy_control() - This function is one of the SAS Domain Template
1261 * functions. This is a phy management function.
1262 * @phy: This parameter specifies the sphy being controlled.
1263 * @func: This parameter specifies the phy control function being invoked.
1264 * @buf: This parameter is specific to the phy function being invoked.
1265 *
1266 * status, zero indicates success.
1267 */
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001268int isci_phy_control(struct asd_sas_phy *sas_phy,
1269 enum phy_func func,
1270 void *buf)
Dan Williams6f231dd2011-07-02 22:56:22 -07001271{
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001272 int ret = 0;
1273 struct isci_phy *iphy = sas_phy->lldd_phy;
1274 struct isci_port *iport = iphy->isci_port;
1275 struct isci_host *ihost = sas_phy->ha->lldd_ha;
1276 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001277
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001278 dev_dbg(&ihost->pdev->dev,
1279 "%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
1280 __func__, sas_phy, func, buf, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001281
1282 switch (func) {
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001283 case PHY_FUNC_DISABLE:
1284 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001285 sci_phy_stop(iphy);
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001286 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1287 break;
1288
Dan Williams6f231dd2011-07-02 22:56:22 -07001289 case PHY_FUNC_LINK_RESET:
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001290 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001291 sci_phy_stop(iphy);
1292 sci_phy_start(iphy);
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001293 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1294 break;
1295
1296 case PHY_FUNC_HARD_RESET:
1297 if (!iport)
1298 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001299
1300 /* Perform the port reset. */
Dan Williams4393aa42011-03-31 13:10:44 -07001301 ret = isci_port_perform_hard_reset(ihost, iport, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -07001302
1303 break;
1304
Dan Williams6f231dd2011-07-02 22:56:22 -07001305 default:
Dave Jiang4d07f7f2011-03-02 12:31:24 -08001306 dev_dbg(&ihost->pdev->dev,
1307 "%s: phy %p; func %d NOT IMPLEMENTED!\n",
1308 __func__, sas_phy, func);
1309 ret = -ENOSYS;
Dan Williams6f231dd2011-07-02 22:56:22 -07001310 break;
1311 }
1312 return ret;
1313}