blob: f9d20c1e63caac304761fe9abe4f1eab772c75f3 [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
Dan Williams6f231dd2011-07-02 22:56:22 -070056#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "port.h"
58#include "request.h"
Dan Williamse2f8db52011-05-10 02:28:46 -070059
60#define SCIC_SDS_PORT_HARD_RESET_TIMEOUT (1000)
61#define SCU_DUMMY_INDEX (0xFFFF)
Dan Williams6f231dd2011-07-02 22:56:22 -070062
Dan Williamsc132f692012-01-03 23:26:08 -080063static struct device *sciport_to_dev(struct isci_port *iport)
64{
65 int i = iport->physical_port_index;
66 struct isci_port *table;
67 struct isci_host *ihost;
68
69 if (i == SCIC_SDS_DUMMY_PORT)
70 i = SCI_MAX_PORTS+1;
71
72 table = iport - i;
73 ihost = container_of(table, typeof(*ihost), ports[0]);
74
75 return &ihost->pdev->dev;
76}
77
Dan Williams89a73012011-06-30 19:14:33 -070078static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
Dan Williams6f231dd2011-07-02 22:56:22 -070079{
Dan Williamse2f8db52011-05-10 02:28:46 -070080 u8 index;
81
Dan Williams89a73012011-06-30 19:14:33 -070082 proto->all = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -070083 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -070084 struct isci_phy *iphy = iport->phy_table[index];
85
86 if (!iphy)
87 continue;
88 sci_phy_get_protocols(iphy, proto);
Dan Williamse2f8db52011-05-10 02:28:46 -070089 }
Dan Williams6f231dd2011-07-02 22:56:22 -070090}
91
Dan Williams89a73012011-06-30 19:14:33 -070092static u32 sci_port_get_phys(struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -070093{
Dan Williamse2f8db52011-05-10 02:28:46 -070094 u32 index;
95 u32 mask;
96
97 mask = 0;
Dan Williams89a73012011-06-30 19:14:33 -070098 for (index = 0; index < SCI_MAX_PHYS; index++)
99 if (iport->phy_table[index])
Dan Williamse2f8db52011-05-10 02:28:46 -0700100 mask |= (1 << index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700101
102 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700103}
104
Dan Williamse2f8db52011-05-10 02:28:46 -0700105/**
Dan Williams89a73012011-06-30 19:14:33 -0700106 * sci_port_get_properties() - This method simply returns the properties
Dan Williamse2f8db52011-05-10 02:28:46 -0700107 * regarding the port, such as: physical index, protocols, sas address, etc.
108 * @port: this parameter specifies the port for which to retrieve the physical
109 * index.
110 * @properties: This parameter specifies the properties structure into which to
111 * copy the requested information.
112 *
113 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
114 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
115 * value is returned if the specified port is not valid. When this value is
116 * returned, no data is copied to the properties output parameter.
117 */
Bartek Nowakowski7e629842012-01-04 01:33:20 -0800118enum sci_status sci_port_get_properties(struct isci_port *iport,
Dan Williams89a73012011-06-30 19:14:33 -0700119 struct sci_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700120{
Dan Williamsffe191c2011-06-29 13:09:25 -0700121 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamse2f8db52011-05-10 02:28:46 -0700122 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700123
Dan Williams89a73012011-06-30 19:14:33 -0700124 prop->index = iport->logical_port_index;
125 prop->phy_mask = sci_port_get_phys(iport);
126 sci_port_get_sas_address(iport, &prop->local.sas_address);
127 sci_port_get_protocols(iport, &prop->local.protocols);
128 sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700129
Dan Williamse2f8db52011-05-10 02:28:46 -0700130 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700131}
132
Dan Williams89a73012011-06-30 19:14:33 -0700133static void sci_port_bcn_enable(struct isci_port *iport)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700134{
Dan Williams85280952011-06-28 15:05:53 -0700135 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700136 u32 val;
137 int i;
138
Dan Williamsffe191c2011-06-29 13:09:25 -0700139 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
140 iphy = iport->phy_table[i];
Dan Williams85280952011-06-28 15:05:53 -0700141 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700142 continue;
Dan Williams85280952011-06-28 15:05:53 -0700143 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700144 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700145 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700146 }
147}
148
Dan Williamsffe191c2011-06-29 13:09:25 -0700149static void isci_port_bc_change_received(struct isci_host *ihost,
150 struct isci_port *iport,
151 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700152{
Dan Williams52d74632011-10-27 15:05:37 -0700153 dev_dbg(&ihost->pdev->dev,
154 "%s: isci_phy = %p, sas_phy = %p\n",
155 __func__, iphy, &iphy->sas_phy);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700156
Dan Williams52d74632011-10-27 15:05:37 -0700157 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
Dan Williams89a73012011-06-30 19:14:33 -0700158 sci_port_bcn_enable(iport);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700159}
160
Dan Williamse2f8db52011-05-10 02:28:46 -0700161static void isci_port_link_up(struct isci_host *isci_host,
Dan Williamsffe191c2011-06-29 13:09:25 -0700162 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700163 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700164{
165 unsigned long flags;
Dan Williams89a73012011-06-30 19:14:33 -0700166 struct sci_port_properties properties;
Dan Williams6f231dd2011-07-02 22:56:22 -0700167 unsigned long success = true;
168
Dan Williams6f231dd2011-07-02 22:56:22 -0700169 dev_dbg(&isci_host->pdev->dev,
170 "%s: isci_port = %p\n",
Dan Williamsffe191c2011-06-29 13:09:25 -0700171 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700172
Dan Williams85280952011-06-28 15:05:53 -0700173 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700174
Dan Williams89a73012011-06-30 19:14:33 -0700175 sci_port_get_properties(iport, &properties);
Dan Williams6f231dd2011-07-02 22:56:22 -0700176
Dan Williams85280952011-06-28 15:05:53 -0700177 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800178 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700179
Dan Williams85280952011-06-28 15:05:53 -0700180 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
181 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700182
183 /*
184 * For direct-attached SATA devices, the SCI core will
185 * automagically assign a SAS address to the end device
186 * for the purpose of creating a port. This SAS address
187 * will not be the same as assigned to the PHY and needs
Dan Williams89a73012011-06-30 19:14:33 -0700188 * to be obtained from struct sci_port_properties properties.
Dan Williams6f231dd2011-07-02 22:56:22 -0700189 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800190 attached_sas_address = properties.remote.sas_address.high;
191 attached_sas_address <<= 32;
192 attached_sas_address |= properties.remote.sas_address.low;
193 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700194
Dan Williams85280952011-06-28 15:05:53 -0700195 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800196 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams85280952011-06-28 15:05:53 -0700197 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
198 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
199 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700200
201 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700202 memcpy(iphy->sas_phy.attached_sas_addr,
203 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700204 } else {
205 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
206 success = false;
207 }
208
Dan Williams85280952011-06-28 15:05:53 -0700209 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800210
Dan Williams85280952011-06-28 15:05:53 -0700211 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
213 /* Notify libsas that we have an address frame, if indeed
214 * we've found an SSP, SMP, or STP target */
215 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700216 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700217 PORTE_BYTES_DMAED);
218}
219
220
221/**
222 * isci_port_link_down() - This function is called by the sci core when a link
223 * becomes inactive.
224 * @isci_host: This parameter specifies the isci host object.
225 * @phy: This parameter specifies the isci phy with the active link.
226 * @port: This parameter specifies the isci port with the active link.
227 *
228 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700229static void isci_port_link_down(struct isci_host *isci_host,
230 struct isci_phy *isci_phy,
231 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700232{
233 struct isci_remote_device *isci_device;
234
235 dev_dbg(&isci_host->pdev->dev,
236 "%s: isci_port = %p\n", __func__, isci_port);
237
238 if (isci_port) {
239
240 /* check to see if this is the last phy on this port. */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700241 if (isci_phy->sas_phy.port &&
242 isci_phy->sas_phy.port->num_phys == 1) {
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700243 /* change the state for all devices on this port. The
244 * next task sent to this device will be returned as
245 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
246 * remove the target
Dan Williams6f231dd2011-07-02 22:56:22 -0700247 */
248 list_for_each_entry(isci_device,
249 &isci_port->remote_dev_list,
250 node) {
251 dev_dbg(&isci_host->pdev->dev,
252 "%s: isci_device = %p\n",
253 __func__, isci_device);
Dan Williams209fae12011-06-13 17:39:44 -0700254 set_bit(IDEV_GONE, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700255 }
256 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700257 }
258
259 /* Notify libsas of the borken link, this will trigger calls to our
260 * isci_port_deformed and isci_dev_gone functions.
261 */
262 sas_phy_disconnected(&isci_phy->sas_phy);
263 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
264 PHYE_LOSS_OF_SIGNAL);
265
Dan Williams6f231dd2011-07-02 22:56:22 -0700266 dev_dbg(&isci_host->pdev->dev,
267 "%s: isci_port = %p - Done\n", __func__, isci_port);
268}
269
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700270static bool is_port_ready_state(enum sci_port_states state)
271{
272 switch (state) {
273 case SCI_PORT_READY:
274 case SCI_PORT_SUB_WAITING:
275 case SCI_PORT_SUB_OPERATIONAL:
276 case SCI_PORT_SUB_CONFIGURING:
277 return true;
278 default:
279 return false;
280 }
281}
282
283/* flag dummy rnc hanling when exiting a ready state */
284static void port_state_machine_change(struct isci_port *iport,
285 enum sci_port_states state)
286{
287 struct sci_base_state_machine *sm = &iport->sm;
288 enum sci_port_states old_state = sm->current_state_id;
289
290 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
291 iport->ready_exit = true;
292
293 sci_change_state(sm, state);
294 iport->ready_exit = false;
295}
296
Dan Williams6f231dd2011-07-02 22:56:22 -0700297/**
298 * isci_port_hard_reset_complete() - This function is called by the sci core
299 * when the hard reset complete notification has been received.
300 * @port: This parameter specifies the sci port with the active link.
301 * @completion_status: This parameter specifies the core status for the reset
302 * process.
303 *
304 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700305static void isci_port_hard_reset_complete(struct isci_port *isci_port,
306 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700307{
308 dev_dbg(&isci_port->isci_host->pdev->dev,
309 "%s: isci_port = %p, completion_status=%x\n",
310 __func__, isci_port, completion_status);
311
312 /* Save the status of the hard reset from the port. */
313 isci_port->hard_reset_status = completion_status;
314
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700315 if (completion_status != SCI_SUCCESS) {
316
317 /* The reset failed. The port state is now SCI_PORT_FAILED. */
318 if (isci_port->active_phy_mask == 0) {
319
320 /* Generate the link down now to the host, since it
321 * was intercepted by the hard reset state machine when
322 * it really happened.
323 */
324 isci_port_link_down(isci_port->isci_host,
325 &isci_port->isci_host->phys[
326 isci_port->last_active_phy],
327 isci_port);
328 }
329 /* Advance the port state so that link state changes will be
330 * noticed.
331 */
332 port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
333
334 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700335 complete_all(&isci_port->hard_reset_complete);
336}
Dan Williams4393aa42011-03-31 13:10:44 -0700337
Dan Williamse2f8db52011-05-10 02:28:46 -0700338/* This method will return a true value if the specified phy can be assigned to
339 * this port The following is a list of phys for each port that are allowed: -
340 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
341 * doesn't preclude all configurations. It merely ensures that a phy is part
342 * of the allowable set of phy identifiers for that port. For example, one
343 * could assign phy 3 to port 0 and no other phys. Please refer to
Dan Williams89a73012011-06-30 19:14:33 -0700344 * sci_port_is_phy_mask_valid() for information regarding whether the
Dan Williamse2f8db52011-05-10 02:28:46 -0700345 * phy_mask for a port can be supported. bool true if this is a valid phy
346 * assignment for the port false if this is not a valid phy assignment for the
347 * port
348 */
Dan Williams89a73012011-06-30 19:14:33 -0700349bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700350{
Dan Williams89a73012011-06-30 19:14:33 -0700351 struct isci_host *ihost = iport->owning_controller;
352 struct sci_user_parameters *user = &ihost->user_parameters;
353
Dan Williamse2f8db52011-05-10 02:28:46 -0700354 /* Initialize to invalid value. */
355 u32 existing_phy_index = SCI_MAX_PHYS;
356 u32 index;
357
Dan Williams89a73012011-06-30 19:14:33 -0700358 if ((iport->physical_port_index == 1) && (phy_index != 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700359 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700360
Dan Williams89a73012011-06-30 19:14:33 -0700361 if (iport->physical_port_index == 3 && phy_index != 3)
Dan Williamse2f8db52011-05-10 02:28:46 -0700362 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700363
Dan Williams89a73012011-06-30 19:14:33 -0700364 if (iport->physical_port_index == 2 &&
365 (phy_index == 0 || phy_index == 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700366 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700367
Dan Williams89a73012011-06-30 19:14:33 -0700368 for (index = 0; index < SCI_MAX_PHYS; index++)
369 if (iport->phy_table[index] && index != phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700370 existing_phy_index = index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700371
Dan Williams89a73012011-06-30 19:14:33 -0700372 /* Ensure that all of the phys in the port are capable of
373 * operating at the same maximum link rate.
374 */
375 if (existing_phy_index < SCI_MAX_PHYS &&
376 user->phys[phy_index].max_speed_generation !=
377 user->phys[existing_phy_index].max_speed_generation)
Dan Williamse2f8db52011-05-10 02:28:46 -0700378 return false;
379
380 return true;
381}
382
383/**
384 *
385 * @sci_port: This is the port object for which to determine if the phy mask
386 * can be supported.
387 *
388 * This method will return a true value if the port's phy mask can be supported
389 * by the SCU. The following is a list of valid PHY mask configurations for
390 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
391 * - Port 3 - [3] This method returns a boolean indication specifying if the
392 * phy mask can be supported. true if this is a valid phy assignment for the
393 * port false if this is not a valid phy assignment for the port
394 */
Dan Williams89a73012011-06-30 19:14:33 -0700395static bool sci_port_is_phy_mask_valid(
Dan Williamsffe191c2011-06-29 13:09:25 -0700396 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700397 u32 phy_mask)
398{
Dan Williamsffe191c2011-06-29 13:09:25 -0700399 if (iport->physical_port_index == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700400 if (((phy_mask & 0x0F) == 0x0F)
401 || ((phy_mask & 0x03) == 0x03)
402 || ((phy_mask & 0x01) == 0x01)
403 || (phy_mask == 0))
404 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700405 } else if (iport->physical_port_index == 1) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700406 if (((phy_mask & 0x02) == 0x02)
407 || (phy_mask == 0))
408 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700409 } else if (iport->physical_port_index == 2) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700410 if (((phy_mask & 0x0C) == 0x0C)
411 || ((phy_mask & 0x04) == 0x04)
412 || (phy_mask == 0))
413 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700414 } else if (iport->physical_port_index == 3) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700415 if (((phy_mask & 0x08) == 0x08)
416 || (phy_mask == 0))
417 return true;
418 }
419
420 return false;
421}
422
Dan Williams85280952011-06-28 15:05:53 -0700423/*
Dan Williamse2f8db52011-05-10 02:28:46 -0700424 * This method retrieves a currently active (i.e. connected) phy contained in
425 * the port. Currently, the lowest order phy that is connected is returned.
426 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
427 * returned if there are no currently active (i.e. connected to a remote end
Dan Williams89a73012011-06-30 19:14:33 -0700428 * point) phys contained in the port. All other values specify a struct sci_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700429 * object that is active in the port.
430 */
Dan Williams89a73012011-06-30 19:14:33 -0700431static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
Dan Williams85280952011-06-28 15:05:53 -0700432{
Dan Williamse2f8db52011-05-10 02:28:46 -0700433 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700434 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700435
436 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700437 /* Ensure that the phy is both part of the port and currently
438 * connected to the remote end-point.
439 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700440 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700441 if (iphy && sci_port_active_phy(iport, iphy))
Dan Williams85280952011-06-28 15:05:53 -0700442 return iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700443 }
444
445 return NULL;
446}
447
Dan Williams89a73012011-06-30 19:14:33 -0700448static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700449{
Dan Williams85280952011-06-28 15:05:53 -0700450 /* Check to see if we can add this phy to a port
Dan Williamse2f8db52011-05-10 02:28:46 -0700451 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700452 * not already have a phy assinged to the phy index.
453 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700454 if (!iport->phy_table[iphy->phy_index] &&
Dan Williams85280952011-06-28 15:05:53 -0700455 !phy_get_non_dummy_port(iphy) &&
Dan Williams89a73012011-06-30 19:14:33 -0700456 sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams85280952011-06-28 15:05:53 -0700457 /* Phy is being added in the stopped state so we are in MPC mode
458 * make logical port index = physical port index
459 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700460 iport->logical_port_index = iport->physical_port_index;
461 iport->phy_table[iphy->phy_index] = iphy;
Dan Williams89a73012011-06-30 19:14:33 -0700462 sci_phy_set_port(iphy, iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700463
464 return SCI_SUCCESS;
465 }
466
467 return SCI_FAILURE;
468}
469
Dan Williams89a73012011-06-30 19:14:33 -0700470static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700471{
472 /* Make sure that this phy is part of this port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700473 if (iport->phy_table[iphy->phy_index] == iphy &&
474 phy_get_non_dummy_port(iphy) == iport) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700475 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700476
477 /* Yep it is assigned to this port so remove it */
Dan Williams89a73012011-06-30 19:14:33 -0700478 sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
Dan Williamsffe191c2011-06-29 13:09:25 -0700479 iport->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -0700480 return SCI_SUCCESS;
481 }
482
483 return SCI_FAILURE;
484}
485
Dan Williams89a73012011-06-30 19:14:33 -0700486void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700487{
488 u32 index;
489
Dan Williams89a73012011-06-30 19:14:33 -0700490 sas->high = 0;
491 sas->low = 0;
492 for (index = 0; index < SCI_MAX_PHYS; index++)
493 if (iport->phy_table[index])
494 sci_phy_get_sas_address(iport->phy_table[index], sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700495}
496
Dan Williams89a73012011-06-30 19:14:33 -0700497void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700498{
Dan Williams85280952011-06-28 15:05:53 -0700499 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700500
501 /*
502 * Ensure that the phy is both part of the port and currently
503 * connected to the remote end-point.
504 */
Dan Williams89a73012011-06-30 19:14:33 -0700505 iphy = sci_port_get_a_connected_phy(iport);
Dan Williams85280952011-06-28 15:05:53 -0700506 if (iphy) {
507 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams89a73012011-06-30 19:14:33 -0700508 sci_phy_get_attached_sas_address(iphy, sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700509 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700510 sci_phy_get_sas_address(iphy, sas);
511 sas->low += iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700512 }
513 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700514 sas->high = 0;
515 sas->low = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700516 }
517}
518
519/**
Dan Williams89a73012011-06-30 19:14:33 -0700520 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
Dan Williamse2f8db52011-05-10 02:28:46 -0700521 *
522 * @sci_port: logical port on which we need to create the remote node context
523 * @rni: remote node index for this remote node context.
524 *
525 * This routine will construct a dummy remote node context data structure
526 * This structure will be posted to the hardware to work around a scheduler
527 * error in the hardware.
528 */
Dan Williams89a73012011-06-30 19:14:33 -0700529static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
Dan Williamse2f8db52011-05-10 02:28:46 -0700530{
531 union scu_remote_node_context *rnc;
532
Dan Williamsffe191c2011-06-29 13:09:25 -0700533 rnc = &iport->owning_controller->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -0700534
535 memset(rnc, 0, sizeof(union scu_remote_node_context));
536
537 rnc->ssp.remote_sas_address_hi = 0;
538 rnc->ssp.remote_sas_address_lo = 0;
539
540 rnc->ssp.remote_node_index = rni;
541 rnc->ssp.remote_node_port_width = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700542 rnc->ssp.logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700543
544 rnc->ssp.nexus_loss_timer_enable = false;
545 rnc->ssp.check_bit = false;
546 rnc->ssp.is_valid = true;
547 rnc->ssp.is_remote_node_context = true;
548 rnc->ssp.function_number = 0;
549 rnc->ssp.arbitration_wait_time = 0;
550}
551
Dan Williamsdd047c82011-06-09 11:06:58 -0700552/*
553 * construct a dummy task context data structure. This
Dan Williamse2f8db52011-05-10 02:28:46 -0700554 * structure will be posted to the hardwre to work around a scheduler error
555 * in the hardware.
Dan Williamse2f8db52011-05-10 02:28:46 -0700556 */
Dan Williams89a73012011-06-30 19:14:33 -0700557static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
Dan Williamse2f8db52011-05-10 02:28:46 -0700558{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700559 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700560 struct scu_task_context *task_context;
561
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700562 task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700563 memset(task_context, 0, sizeof(struct scu_task_context));
564
Dan Williamse2f8db52011-05-10 02:28:46 -0700565 task_context->initiator_request = 1;
566 task_context->connection_rate = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700567 task_context->logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700568 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700569 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700570 task_context->valid = SCU_TASK_CONTEXT_VALID;
571 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamsffe191c2011-06-29 13:09:25 -0700572 task_context->remote_node_index = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -0700573 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db52011-05-10 02:28:46 -0700574 task_context->task_phase = 0x01;
575}
576
Dan Williams89a73012011-06-30 19:14:33 -0700577static void sci_port_destroy_dummy_resources(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700578{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700579 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700580
Dan Williamsffe191c2011-06-29 13:09:25 -0700581 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700582 isci_free_tag(ihost, iport->reserved_tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700583
Dan Williamsffe191c2011-06-29 13:09:25 -0700584 if (iport->reserved_rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700585 sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
Dan Williamsffe191c2011-06-29 13:09:25 -0700586 1, iport->reserved_rni);
Dan Williamse2f8db52011-05-10 02:28:46 -0700587
Dan Williamsffe191c2011-06-29 13:09:25 -0700588 iport->reserved_rni = SCU_DUMMY_INDEX;
589 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -0700590}
591
Dan Williams89a73012011-06-30 19:14:33 -0700592void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
Dan Williamse2f8db52011-05-10 02:28:46 -0700593{
594 u8 index;
595
596 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700597 if (iport->active_phy_mask & (1 << index))
Dan Williams89a73012011-06-30 19:14:33 -0700598 sci_phy_setup_transport(iport->phy_table[index], device_id);
Dan Williamse2f8db52011-05-10 02:28:46 -0700599 }
600}
601
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800602static void sci_port_resume_phy(struct isci_port *iport, struct isci_phy *iphy)
603{
604 sci_phy_resume(iphy);
605 iport->enabled_phy_mask |= 1 << iphy->phy_index;
606}
607
608static void sci_port_activate_phy(struct isci_port *iport,
609 struct isci_phy *iphy,
610 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700611{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700612 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700613
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800614 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA && (flags & PF_RESUME))
Dan Williams89a73012011-06-30 19:14:33 -0700615 sci_phy_resume(iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700616
Dan Williamsffe191c2011-06-29 13:09:25 -0700617 iport->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700618
Dan Williams89a73012011-06-30 19:14:33 -0700619 sci_controller_clear_invalid_phy(ihost, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700620
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800621 if (flags & PF_NOTIFY)
Dan Williamsffe191c2011-06-29 13:09:25 -0700622 isci_port_link_up(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700623}
624
Dan Williams89a73012011-06-30 19:14:33 -0700625void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
626 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700627{
Dan Williams34a99152011-07-01 02:25:15 -0700628 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700629
Dan Williamsffe191c2011-06-29 13:09:25 -0700630 iport->active_phy_mask &= ~(1 << iphy->phy_index);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800631 iport->enabled_phy_mask &= ~(1 << iphy->phy_index);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700632 if (!iport->active_phy_mask)
633 iport->last_active_phy = iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700634
Dan Williams85280952011-06-28 15:05:53 -0700635 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db52011-05-10 02:28:46 -0700636
Marcin Tomczakd4ec1cf2012-01-04 01:33:15 -0800637 /* Re-assign the phy back to the LP as if it were a narrow port for APC
638 * mode. For MPC mode, the phy will remain in the port.
639 */
640 if (iport->owning_controller->oem_parameters.controller.mode_type ==
641 SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE)
642 writel(iphy->phy_index,
643 &iport->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700644
645 if (do_notify_user == true)
646 isci_port_link_down(ihost, iphy, iport);
647}
648
Dan Williams89a73012011-06-30 19:14:33 -0700649static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700650{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700651 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700652
653 /*
654 * Check to see if we have alreay reported this link as bad and if
655 * not go ahead and tell the SCI_USER that we have discovered an
656 * invalid link.
657 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700658 if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700659 ihost->invalid_phy_mask |= 1 << iphy->phy_index;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700660 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700661 }
662}
663
664/**
Dan Williams89a73012011-06-30 19:14:33 -0700665 * sci_port_general_link_up_handler - phy can be assigned to port?
666 * @sci_port: sci_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700667 * @sci_phy: This is the struct isci_phy object that has gone link up.
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800668 * @flags: PF_RESUME, PF_NOTIFY to sci_port_activate_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700669 *
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800670 * Determine if this phy can be assigned to this port . If the phy is
671 * not a valid PHY for this port then the function will notify the user.
672 * A PHY can only be part of a port if it's attached SAS ADDRESS is the
673 * same as all other PHYs in the same port.
Dan Williamse2f8db52011-05-10 02:28:46 -0700674 */
Dan Williams89a73012011-06-30 19:14:33 -0700675static void sci_port_general_link_up_handler(struct isci_port *iport,
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800676 struct isci_phy *iphy,
677 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700678{
679 struct sci_sas_address port_sas_address;
680 struct sci_sas_address phy_sas_address;
681
Dan Williams89a73012011-06-30 19:14:33 -0700682 sci_port_get_attached_sas_address(iport, &port_sas_address);
683 sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700684
685 /* If the SAS address of the new phy matches the SAS address of
686 * other phys in the port OR this is the first phy in the port,
687 * then activate the phy and allow it to be used for operations
688 * in this port.
689 */
690 if ((phy_sas_address.high == port_sas_address.high &&
691 phy_sas_address.low == port_sas_address.low) ||
Dan Williamsffe191c2011-06-29 13:09:25 -0700692 iport->active_phy_mask == 0) {
693 struct sci_base_state_machine *sm = &iport->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700694
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800695 sci_port_activate_phy(iport, iphy, flags);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000696 if (sm->current_state_id == SCI_PORT_RESETTING)
Dan Williamsffe191c2011-06-29 13:09:25 -0700697 port_state_machine_change(iport, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700698 } else
Dan Williams89a73012011-06-30 19:14:33 -0700699 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700700}
701
702
703
704/**
705 * This method returns false if the port only has a single phy object assigned.
706 * If there are no phys or more than one phy then the method will return
707 * true.
708 * @sci_port: The port for which the wide port condition is to be checked.
709 *
710 * bool true Is returned if this is a wide ported port. false Is returned if
711 * this is a narrow port.
712 */
Dan Williams89a73012011-06-30 19:14:33 -0700713static bool sci_port_is_wide(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700714{
715 u32 index;
716 u32 phy_count = 0;
717
718 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700719 if (iport->phy_table[index] != NULL) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700720 phy_count++;
721 }
722 }
723
724 return phy_count != 1;
725}
726
727/**
728 * This method is called by the PHY object when the link is detected. if the
729 * port wants the PHY to continue on to the link up state then the port
730 * layer must return true. If the port object returns false the phy object
731 * must halt its attempt to go link up.
732 * @sci_port: The port associated with the phy object.
733 * @sci_phy: The phy object that is trying to go link up.
734 *
735 * true if the phy object can continue to the link up condition. true Is
736 * returned if this phy can continue to the ready state. false Is returned if
737 * can not continue on to the ready state. This notification is in place for
738 * wide ports and direct attached phys. Since there are no wide ported SATA
739 * devices this could become an invalid port configuration.
740 */
Dan Williams89a73012011-06-30 19:14:33 -0700741bool sci_port_link_detected(
Dan Williamsffe191c2011-06-29 13:09:25 -0700742 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700743 struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700744{
Dan Williamsffe191c2011-06-29 13:09:25 -0700745 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800746 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA)) {
747 if (sci_port_is_wide(iport)) {
748 sci_port_invalid_link_up(iport, iphy);
749 return false;
750 } else {
751 struct isci_host *ihost = iport->owning_controller;
752 struct isci_port *dst_port = &(ihost->ports[iphy->phy_index]);
753 writel(iphy->phy_index,
754 &dst_port->port_pe_configuration_register[iphy->phy_index]);
755 }
Dan Williamse2f8db52011-05-10 02:28:46 -0700756 }
757
758 return true;
759}
760
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000761static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700762{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000763 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsffe191c2011-06-29 13:09:25 -0700764 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700765 struct isci_host *ihost = iport->owning_controller;
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000766 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700767 u32 current_state;
768
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000769 spin_lock_irqsave(&ihost->scic_lock, flags);
770
771 if (tmr->cancel)
772 goto done;
773
Dan Williamsffe191c2011-06-29 13:09:25 -0700774 current_state = iport->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700775
Edmund Nadolskie3013702011-06-02 00:10:43 +0000776 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000777 /* if the port is still in the resetting state then the timeout
778 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700779 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700780 port_state_machine_change(iport, SCI_PORT_FAILED);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000781 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000782 /* if the port is stopped then the start request failed In this
783 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700784 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700785 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700786 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
787 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -0700788 iport);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000789 } else if (current_state == SCI_PORT_STOPPING) {
Dan Williamsfca4ecb2012-01-03 23:26:15 -0800790 dev_dbg(sciport_to_dev(iport),
791 "%s: port%d: stop complete timeout\n",
792 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700793 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000794 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700795 * reporting a timeout this should not happen.
796 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700797 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700798 "%s: SCIC Port 0x%p is processing a timeout operation "
Dan Williamsffe191c2011-06-29 13:09:25 -0700799 "in state %d.\n", __func__, iport, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700800 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000801
802done:
803 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700804}
805
806/* --------------------------------------------------------------------------- */
807
808/**
809 * This function updates the hardwares VIIT entry for this port.
810 *
811 *
812 */
Dan Williams89a73012011-06-30 19:14:33 -0700813static void sci_port_update_viit_entry(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700814{
815 struct sci_sas_address sas_address;
816
Dan Williams89a73012011-06-30 19:14:33 -0700817 sci_port_get_sas_address(iport, &sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700818
819 writel(sas_address.high,
Dan Williamsffe191c2011-06-29 13:09:25 -0700820 &iport->viit_registers->initiator_sas_address_hi);
Dan Williamse2f8db52011-05-10 02:28:46 -0700821 writel(sas_address.low,
Dan Williamsffe191c2011-06-29 13:09:25 -0700822 &iport->viit_registers->initiator_sas_address_lo);
Dan Williamse2f8db52011-05-10 02:28:46 -0700823
824 /* This value get cleared just in case its not already cleared */
Dan Williamsffe191c2011-06-29 13:09:25 -0700825 writel(0, &iport->viit_registers->reserved);
Dan Williamse2f8db52011-05-10 02:28:46 -0700826
827 /* We are required to update the status register last */
828 writel(SCU_VIIT_ENTRY_ID_VIIT |
829 SCU_VIIT_IPPT_INITIATOR |
Dan Williamsffe191c2011-06-29 13:09:25 -0700830 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
Dan Williamse2f8db52011-05-10 02:28:46 -0700831 SCU_VIIT_STATUS_ALL_VALID,
Dan Williamsffe191c2011-06-29 13:09:25 -0700832 &iport->viit_registers->status);
Dan Williamse2f8db52011-05-10 02:28:46 -0700833}
834
Dan Williams89a73012011-06-30 19:14:33 -0700835enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700836{
837 u16 index;
Dan Williams85280952011-06-28 15:05:53 -0700838 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700839 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700840
841 /*
842 * Loop through all of the phys in this port and find the phy with the
843 * lowest maximum link rate. */
844 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700845 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700846 if (iphy && sci_port_active_phy(iport, iphy) &&
Dan Williams85280952011-06-28 15:05:53 -0700847 iphy->max_negotiated_speed < max_allowed_speed)
848 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db52011-05-10 02:28:46 -0700849 }
850
851 return max_allowed_speed;
852}
853
Dan Williams89a73012011-06-30 19:14:33 -0700854static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700855{
856 u32 pts_control_value;
857
Dan Williamsffe191c2011-06-29 13:09:25 -0700858 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700859 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700860 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700861}
862
863/**
Dan Williams89a73012011-06-30 19:14:33 -0700864 * sci_port_post_dummy_request() - post dummy/workaround request
Dan Williamse2f8db52011-05-10 02:28:46 -0700865 * @sci_port: port to post task
866 *
867 * Prevent the hardware scheduler from posting new requests to the front
868 * of the scheduler queue causing a starvation problem for currently
869 * ongoing requests.
870 *
871 */
Dan Williams89a73012011-06-30 19:14:33 -0700872static void sci_port_post_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700873{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700874 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700875 u16 tag = iport->reserved_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700876 struct scu_task_context *tc;
877 u32 command;
Dan Williamse2f8db52011-05-10 02:28:46 -0700878
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700879 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williams312e0c22011-06-28 13:47:09 -0700880 tc->abort = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700881
882 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700883 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700884 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700885
Dan Williams89a73012011-06-30 19:14:33 -0700886 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700887}
888
889/**
890 * This routine will abort the dummy request. This will alow the hardware to
891 * power down parts of the silicon to save power.
892 *
893 * @sci_port: The port on which the task must be aborted.
894 *
895 */
Dan Williams89a73012011-06-30 19:14:33 -0700896static void sci_port_abort_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700897{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700898 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700899 u16 tag = iport->reserved_tag;
Dan Williamse2f8db52011-05-10 02:28:46 -0700900 struct scu_task_context *tc;
901 u32 command;
902
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700903 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700904 tc->abort = 1;
905
906 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
Dan Williamsffe191c2011-06-29 13:09:25 -0700907 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700908 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700909
Dan Williams89a73012011-06-30 19:14:33 -0700910 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700911}
912
913/**
914 *
Dan Williamsffe191c2011-06-29 13:09:25 -0700915 * @sci_port: This is the struct isci_port object to resume.
Dan Williamse2f8db52011-05-10 02:28:46 -0700916 *
917 * This method will resume the port task scheduler for this port object. none
918 */
919static void
Dan Williams89a73012011-06-30 19:14:33 -0700920sci_port_resume_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700921{
922 u32 pts_control_value;
923
Dan Williamsffe191c2011-06-29 13:09:25 -0700924 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700925 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700926 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700927}
928
Dan Williams89a73012011-06-30 19:14:33 -0700929static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700930{
Dan Williamsffe191c2011-06-29 13:09:25 -0700931 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -0700932
Dan Williams89a73012011-06-30 19:14:33 -0700933 sci_port_suspend_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700934
Dan Williamsffe191c2011-06-29 13:09:25 -0700935 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700936
Dan Williamsffe191c2011-06-29 13:09:25 -0700937 if (iport->active_phy_mask != 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700938 /* At least one of the phys on the port is ready */
Dan Williamsffe191c2011-06-29 13:09:25 -0700939 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000940 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -0700941 }
942}
943
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800944static void scic_sds_port_ready_substate_waiting_exit(
945 struct sci_base_state_machine *sm)
946{
947 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
948 sci_port_resume_port_task_scheduler(iport);
949}
950
Dan Williams89a73012011-06-30 19:14:33 -0700951static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700952{
953 u32 index;
Dan Williamsffe191c2011-06-29 13:09:25 -0700954 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700955 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700956
Dan Williamsfca4ecb2012-01-03 23:26:15 -0800957 dev_dbg(&ihost->pdev->dev, "%s: port%d ready\n",
958 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700959
960 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700961 if (iport->phy_table[index]) {
962 writel(iport->physical_port_index,
963 &iport->port_pe_configuration_register[
964 iport->phy_table[index]->phy_index]);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800965 if (((iport->active_phy_mask^iport->enabled_phy_mask) & (1 << index)) != 0)
966 sci_port_resume_phy(iport, iport->phy_table[index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700967 }
968 }
969
Dan Williams89a73012011-06-30 19:14:33 -0700970 sci_port_update_viit_entry(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700971
Dan Williamse2f8db52011-05-10 02:28:46 -0700972 /*
973 * Post the dummy task for the port so the hardware can schedule
974 * io correctly
975 */
Dan Williams89a73012011-06-30 19:14:33 -0700976 sci_port_post_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700977}
978
Dan Williams89a73012011-06-30 19:14:33 -0700979static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000980{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700981 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700982 u8 phys_index = iport->physical_port_index;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000983 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -0700984 u16 rni = iport->reserved_rni;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000985 u32 command;
986
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700987 rnc = &ihost->remote_node_context_table[rni];
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000988
989 rnc->ssp.is_valid = false;
990
991 /* ensure the preceding tc abort request has reached the
992 * controller and give it ample time to act before posting the rnc
993 * invalidate
994 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700995 readl(&ihost->smu_registers->interrupt_status); /* flush */
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000996 udelay(10);
997
998 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
999 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1000
Dan Williams89a73012011-06-30 19:14:33 -07001001 sci_controller_post_request(ihost, command);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001002}
1003
Dan Williamse2f8db52011-05-10 02:28:46 -07001004/**
1005 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001006 * @object: This is the object which is cast to a struct isci_port object.
Dan Williamse2f8db52011-05-10 02:28:46 -07001007 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001008 * This method will perform the actions required by the struct isci_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001009 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001010 * the port not ready and suspends the port task scheduler. none
1011 */
Dan Williams89a73012011-06-30 19:14:33 -07001012static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001013{
Dan Williamsffe191c2011-06-29 13:09:25 -07001014 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001015 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001016
1017 /*
1018 * Kill the dummy task for this port if it has not yet posted
1019 * the hardware will treat this as a NOP and just return abort
1020 * complete.
1021 */
Dan Williams89a73012011-06-30 19:14:33 -07001022 sci_port_abort_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001023
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001024 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1025 __func__, iport->physical_port_index);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001026
Dan Williamsffe191c2011-06-29 13:09:25 -07001027 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001028 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001029}
1030
Dan Williams89a73012011-06-30 19:14:33 -07001031static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001032{
Dan Williamsffe191c2011-06-29 13:09:25 -07001033 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001034 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001035
Dan Williamsffe191c2011-06-29 13:09:25 -07001036 if (iport->active_phy_mask == 0) {
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001037 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1038 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -07001039
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001040 port_state_machine_change(iport, SCI_PORT_SUB_WAITING);
1041 } else
1042 port_state_machine_change(iport, SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001043}
1044
Dan Williams89a73012011-06-30 19:14:33 -07001045enum sci_status sci_port_start(struct isci_port *iport)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001046{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001047 struct isci_host *ihost = iport->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001048 enum sci_status status = SCI_SUCCESS;
Dan Williams89a73012011-06-30 19:14:33 -07001049 enum sci_port_states state;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001050 u32 phy_mask;
1051
Dan Williamsffe191c2011-06-29 13:09:25 -07001052 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001053 if (state != SCI_PORT_STOPPED) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001054 dev_warn(sciport_to_dev(iport),
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001055 "%s: in wrong state: %d\n", __func__, state);
1056 return SCI_FAILURE_INVALID_STATE;
1057 }
1058
Dan Williamsffe191c2011-06-29 13:09:25 -07001059 if (iport->assigned_device_count > 0) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001060 /* TODO This is a start failure operation because
1061 * there are still devices assigned to this port.
1062 * There must be no devices assigned to a port on a
1063 * start operation.
1064 */
1065 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1066 }
1067
Dan Williamsffe191c2011-06-29 13:09:25 -07001068 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
Dan Williams89a73012011-06-30 19:14:33 -07001069 u16 rni = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001070 &ihost->available_remote_nodes, 1);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001071
1072 if (rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -07001073 sci_port_construct_dummy_rnc(iport, rni);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001074 else
1075 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williamsffe191c2011-06-29 13:09:25 -07001076 iport->reserved_rni = rni;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001077 }
1078
Dan Williamsffe191c2011-06-29 13:09:25 -07001079 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williams312e0c22011-06-28 13:47:09 -07001080 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001081
Dan Williams312e0c22011-06-28 13:47:09 -07001082 tag = isci_alloc_tag(ihost);
1083 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001084 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001085 else
Dan Williams89a73012011-06-30 19:14:33 -07001086 sci_port_construct_dummy_task(iport, tag);
Dan Williamsffe191c2011-06-29 13:09:25 -07001087 iport->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001088 }
1089
1090 if (status == SCI_SUCCESS) {
Dan Williams89a73012011-06-30 19:14:33 -07001091 phy_mask = sci_port_get_phys(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001092
1093 /*
1094 * There are one or more phys assigned to this port. Make sure
1095 * the port's phy mask is in fact legal and supported by the
1096 * silicon.
1097 */
Dan Williams89a73012011-06-30 19:14:33 -07001098 if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001099 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001100 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001101
1102 return SCI_SUCCESS;
1103 }
1104 status = SCI_FAILURE;
1105 }
1106
1107 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -07001108 sci_port_destroy_dummy_resources(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001109
1110 return status;
1111}
1112
Dan Williams89a73012011-06-30 19:14:33 -07001113enum sci_status sci_port_stop(struct isci_port *iport)
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001114{
Dan Williams89a73012011-06-30 19:14:33 -07001115 enum sci_port_states state;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001116
Dan Williamsffe191c2011-06-29 13:09:25 -07001117 state = iport->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001118 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001119 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001120 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001121 case SCI_PORT_SUB_WAITING:
1122 case SCI_PORT_SUB_OPERATIONAL:
1123 case SCI_PORT_SUB_CONFIGURING:
1124 case SCI_PORT_RESETTING:
Dan Williamsffe191c2011-06-29 13:09:25 -07001125 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001126 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001127 return SCI_SUCCESS;
1128 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001129 dev_warn(sciport_to_dev(iport),
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001130 "%s: in wrong state: %d\n", __func__, state);
1131 return SCI_FAILURE_INVALID_STATE;
1132 }
1133}
1134
Dan Williams89a73012011-06-30 19:14:33 -07001135static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001136{
1137 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001138 struct isci_phy *iphy = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001139 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001140 u32 phy_index;
1141
Dan Williamsffe191c2011-06-29 13:09:25 -07001142 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001143 if (state != SCI_PORT_SUB_OPERATIONAL) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001144 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001145 "%s: in wrong state: %d\n", __func__, state);
1146 return SCI_FAILURE_INVALID_STATE;
1147 }
1148
1149 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001150 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001151 iphy = iport->phy_table[phy_index];
Dan Williams89a73012011-06-30 19:14:33 -07001152 if (iphy && !sci_port_active_phy(iport, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001153 /*
1154 * We found a phy but it is not ready select
1155 * different phy
1156 */
Dan Williams85280952011-06-28 15:05:53 -07001157 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001158 }
1159 }
1160
1161 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001162 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001163 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001164 status = sci_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001165
1166 if (status != SCI_SUCCESS)
1167 return status;
1168
Dan Williamsffe191c2011-06-29 13:09:25 -07001169 sci_mod_timer(&iport->timer, timeout);
1170 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001171
Dan Williamsffe191c2011-06-29 13:09:25 -07001172 port_state_machine_change(iport, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001173 return SCI_SUCCESS;
1174}
1175
1176/**
Dan Williams89a73012011-06-30 19:14:33 -07001177 * sci_port_add_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001178 * @sci_port: This parameter specifies the port in which the phy will be added.
1179 * @sci_phy: This parameter is the phy which is to be added to the port.
1180 *
1181 * This method will add a PHY to the selected port. This method returns an
1182 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1183 * status is a failure to add the phy to the port.
1184 */
Dan Williams89a73012011-06-30 19:14:33 -07001185enum sci_status sci_port_add_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001186 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001187{
1188 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001189 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001190
Dan Williamsffe191c2011-06-29 13:09:25 -07001191 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001192 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001193 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001194 struct sci_sas_address port_sas_address;
1195
1196 /* Read the port assigned SAS Address if there is one */
Dan Williams89a73012011-06-30 19:14:33 -07001197 sci_port_get_sas_address(iport, &port_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001198
1199 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1200 struct sci_sas_address phy_sas_address;
1201
1202 /* Make sure that the PHY SAS Address matches the SAS Address
1203 * for this port
1204 */
Dan Williams89a73012011-06-30 19:14:33 -07001205 sci_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001206
1207 if (port_sas_address.high != phy_sas_address.high ||
1208 port_sas_address.low != phy_sas_address.low)
1209 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1210 }
Dan Williams89a73012011-06-30 19:14:33 -07001211 return sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001212 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001213 case SCI_PORT_SUB_WAITING:
1214 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001215 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001216
1217 if (status != SCI_SUCCESS)
1218 return status;
1219
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001220 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Dan Williamsffe191c2011-06-29 13:09:25 -07001221 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1222 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001223
1224 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001225 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001226 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001227
1228 if (status != SCI_SUCCESS)
1229 return status;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001230 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001231
1232 /* Re-enter the configuring state since this may be the last phy in
1233 * the port.
1234 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001235 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001236 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001237 return SCI_SUCCESS;
1238 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001239 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001240 "%s: in wrong state: %d\n", __func__, state);
1241 return SCI_FAILURE_INVALID_STATE;
1242 }
1243}
1244
1245/**
Dan Williams89a73012011-06-30 19:14:33 -07001246 * sci_port_remove_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001247 * @sci_port: This parameter specifies the port in which the phy will be added.
1248 * @sci_phy: This parameter is the phy which is to be added to the port.
1249 *
1250 * This method will remove the PHY from the selected PORT. This method returns
1251 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1252 * other status is a failure to add the phy to the port.
1253 */
Dan Williams89a73012011-06-30 19:14:33 -07001254enum sci_status sci_port_remove_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001255 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001256{
1257 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001258 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001259
Dan Williamsffe191c2011-06-29 13:09:25 -07001260 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001261
1262 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001263 case SCI_PORT_STOPPED:
Dan Williams89a73012011-06-30 19:14:33 -07001264 return sci_port_clear_phy(iport, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001265 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001266 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001267 if (status != SCI_SUCCESS)
1268 return status;
1269
Dan Williams89a73012011-06-30 19:14:33 -07001270 sci_port_deactivate_phy(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001271 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1272 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001273 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001274 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001275 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001276 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001277
1278 if (status != SCI_SUCCESS)
1279 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001280 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001281
1282 /* Re-enter the configuring state since this may be the last phy in
1283 * the port
1284 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001285 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001286 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001287 return SCI_SUCCESS;
1288 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001289 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001290 "%s: in wrong state: %d\n", __func__, state);
1291 return SCI_FAILURE_INVALID_STATE;
1292 }
1293}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001294
Dan Williams89a73012011-06-30 19:14:33 -07001295enum sci_status sci_port_link_up(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001296 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001297{
Dan Williams89a73012011-06-30 19:14:33 -07001298 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001299
Dan Williamsffe191c2011-06-29 13:09:25 -07001300 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001301 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001302 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001303 /* Since this is the first phy going link up for the port we
1304 * can just enable it and continue
1305 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001306 sci_port_activate_phy(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001307
Dan Williamsffe191c2011-06-29 13:09:25 -07001308 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001309 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001310 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001311 case SCI_PORT_SUB_OPERATIONAL:
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001312 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001313 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001314 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001315 /* TODO We should make sure that the phy that has gone
1316 * link up is the same one on which we sent the reset. It is
1317 * possible that the phy on which we sent the reset is not the
1318 * one that has gone link up and we want to make sure that
1319 * phy being reset comes back. Consider the case where a
1320 * reset is sent but before the hardware processes the reset it
1321 * get a link up on the port because of a hot plug event.
1322 * because of the reset request this phy will go link down
1323 * almost immediately.
1324 */
1325
1326 /* In the resetting state we don't notify the user regarding
1327 * link up and link down notifications.
1328 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001329 sci_port_general_link_up_handler(iport, iphy, PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001330 return SCI_SUCCESS;
1331 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001332 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001333 "%s: in wrong state: %d\n", __func__, state);
1334 return SCI_FAILURE_INVALID_STATE;
1335 }
1336}
1337
Dan Williams89a73012011-06-30 19:14:33 -07001338enum sci_status sci_port_link_down(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001339 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001340{
Dan Williams89a73012011-06-30 19:14:33 -07001341 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001342
Dan Williamsffe191c2011-06-29 13:09:25 -07001343 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001344 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001345 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001346 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001347
1348 /* If there are no active phys left in the port, then
1349 * transition the port to the WAITING state until such time
1350 * as a phy goes link up
1351 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001352 if (iport->active_phy_mask == 0)
1353 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001354 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001355 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001356 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001357 /* In the resetting state we don't notify the user regarding
1358 * link up and link down notifications. */
Dan Williams89a73012011-06-30 19:14:33 -07001359 sci_port_deactivate_phy(iport, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001360 return SCI_SUCCESS;
1361 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001362 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001363 "%s: in wrong state: %d\n", __func__, state);
1364 return SCI_FAILURE_INVALID_STATE;
1365 }
1366}
1367
Dan Williams89a73012011-06-30 19:14:33 -07001368enum sci_status sci_port_start_io(struct isci_port *iport,
1369 struct isci_remote_device *idev,
1370 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001371{
Dan Williams89a73012011-06-30 19:14:33 -07001372 enum sci_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001373
Dan Williamsffe191c2011-06-29 13:09:25 -07001374 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001375 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001376 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001377 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001378 case SCI_PORT_SUB_OPERATIONAL:
Dan Williamsffe191c2011-06-29 13:09:25 -07001379 iport->started_request_count++;
Dan Williams68138202011-05-12 07:16:06 -07001380 return SCI_SUCCESS;
1381 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001382 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001383 "%s: in wrong state: %d\n", __func__, state);
1384 return SCI_FAILURE_INVALID_STATE;
1385 }
1386}
1387
Dan Williams89a73012011-06-30 19:14:33 -07001388enum sci_status sci_port_complete_io(struct isci_port *iport,
1389 struct isci_remote_device *idev,
1390 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001391{
Dan Williams89a73012011-06-30 19:14:33 -07001392 enum sci_port_states state;
Dan Williams68138202011-05-12 07:16:06 -07001393
Dan Williamsffe191c2011-06-29 13:09:25 -07001394 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001395 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001396 case SCI_PORT_STOPPED:
Dan Williamsffe191c2011-06-29 13:09:25 -07001397 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001398 "%s: in wrong state: %d\n", __func__, state);
1399 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001400 case SCI_PORT_STOPPING:
Dan Williams89a73012011-06-30 19:14:33 -07001401 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001402
Dan Williamsffe191c2011-06-29 13:09:25 -07001403 if (iport->started_request_count == 0)
1404 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001405 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001406 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001407 case SCI_PORT_READY:
1408 case SCI_PORT_RESETTING:
1409 case SCI_PORT_FAILED:
1410 case SCI_PORT_SUB_WAITING:
1411 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001412 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001413 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001414 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001415 sci_port_decrement_request_count(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001416 if (iport->started_request_count == 0) {
1417 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001418 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001419 }
1420 break;
1421 }
1422 return SCI_SUCCESS;
1423}
Dan Williamse2f8db52011-05-10 02:28:46 -07001424
Dan Williams89a73012011-06-30 19:14:33 -07001425static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001426{
1427 u32 pts_control_value;
1428
Dan Williams89a73012011-06-30 19:14:33 -07001429 /* enable the port task scheduler in a suspended state */
Dan Williamsffe191c2011-06-29 13:09:25 -07001430 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001431 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -07001432 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001433}
1434
Dan Williams89a73012011-06-30 19:14:33 -07001435static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001436{
1437 u32 pts_control_value;
1438
Dan Williamsffe191c2011-06-29 13:09:25 -07001439 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001440 pts_control_value &=
1441 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
Dan Williamsffe191c2011-06-29 13:09:25 -07001442 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001443}
1444
Dan Williams89a73012011-06-30 19:14:33 -07001445static void sci_port_post_dummy_remote_node(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001446{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001447 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001448 u8 phys_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -07001449 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001450 u16 rni = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -07001451 u32 command;
1452
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001453 rnc = &ihost->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -07001454 rnc->ssp.is_valid = true;
1455
1456 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1457 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1458
Dan Williams89a73012011-06-30 19:14:33 -07001459 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001460
1461 /* ensure hardware has seen the post rnc command and give it
1462 * ample time to act before sending the suspend
1463 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001464 readl(&ihost->smu_registers->interrupt_status); /* flush */
Dan Williamse2f8db52011-05-10 02:28:46 -07001465 udelay(10);
1466
1467 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1468 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1469
Dan Williams89a73012011-06-30 19:14:33 -07001470 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001471}
1472
Dan Williams89a73012011-06-30 19:14:33 -07001473static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001474{
Dan Williamsffe191c2011-06-29 13:09:25 -07001475 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001476
Dan Williamsffe191c2011-06-29 13:09:25 -07001477 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001478 /*
1479 * If we enter this state becasuse of a request to stop
1480 * the port then we want to disable the hardwares port
1481 * task scheduler. */
Dan Williams89a73012011-06-30 19:14:33 -07001482 sci_port_disable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001483 }
1484}
1485
Dan Williams89a73012011-06-30 19:14:33 -07001486static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001487{
Dan Williamsffe191c2011-06-29 13:09:25 -07001488 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001489
1490 /* Enable and suspend the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001491 sci_port_enable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001492}
1493
Dan Williams89a73012011-06-30 19:14:33 -07001494static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001495{
Dan Williamsffe191c2011-06-29 13:09:25 -07001496 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001497 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001498 u32 prev_state;
1499
Dan Williamsffe191c2011-06-29 13:09:25 -07001500 prev_state = iport->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001501 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001502 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1503 else
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001504 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1505 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -07001506
1507 /* Post and suspend the dummy remote node context for this port. */
Dan Williams89a73012011-06-30 19:14:33 -07001508 sci_port_post_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001509
1510 /* Start the ready substate machine */
Dan Williamsffe191c2011-06-29 13:09:25 -07001511 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001512 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001513}
1514
Dan Williams89a73012011-06-30 19:14:33 -07001515static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001516{
Dan Williamsffe191c2011-06-29 13:09:25 -07001517 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001518
Dan Williamsffe191c2011-06-29 13:09:25 -07001519 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001520}
1521
Dan Williams89a73012011-06-30 19:14:33 -07001522static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001523{
Dan Williamsffe191c2011-06-29 13:09:25 -07001524 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001525
Dan Williamsffe191c2011-06-29 13:09:25 -07001526 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001527
Dan Williams89a73012011-06-30 19:14:33 -07001528 sci_port_destroy_dummy_resources(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001529}
1530
Dan Williams89a73012011-06-30 19:14:33 -07001531static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001532{
Dan Williamsffe191c2011-06-29 13:09:25 -07001533 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001534
Dan Williamse2f8db52011-05-10 02:28:46 -07001535 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1536}
1537
1538/* --------------------------------------------------------------------------- */
1539
Dan Williams89a73012011-06-30 19:14:33 -07001540static const struct sci_base_state sci_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001541 [SCI_PORT_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001542 .enter_state = sci_port_stopped_state_enter,
1543 .exit_state = sci_port_stopped_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001544 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001545 [SCI_PORT_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001546 .exit_state = sci_port_stopping_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001547 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001548 [SCI_PORT_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001549 .enter_state = sci_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001550 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001551 [SCI_PORT_SUB_WAITING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001552 .enter_state = sci_port_ready_substate_waiting_enter,
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001553 .exit_state = scic_sds_port_ready_substate_waiting_exit,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001554 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001555 [SCI_PORT_SUB_OPERATIONAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001556 .enter_state = sci_port_ready_substate_operational_enter,
1557 .exit_state = sci_port_ready_substate_operational_exit
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001558 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001559 [SCI_PORT_SUB_CONFIGURING] = {
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001560 .enter_state = sci_port_ready_substate_configuring_enter
Dan Williamse2f8db52011-05-10 02:28:46 -07001561 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001562 [SCI_PORT_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001563 .exit_state = sci_port_resetting_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001564 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001565 [SCI_PORT_FAILED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001566 .enter_state = sci_port_failed_state_enter,
Dan Williamse2f8db52011-05-10 02:28:46 -07001567 }
1568};
1569
Dan Williams89a73012011-06-30 19:14:33 -07001570void sci_port_construct(struct isci_port *iport, u8 index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001571 struct isci_host *ihost)
Dan Williamse2f8db52011-05-10 02:28:46 -07001572{
Dan Williams89a73012011-06-30 19:14:33 -07001573 sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001574
Dan Williamsffe191c2011-06-29 13:09:25 -07001575 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1576 iport->physical_port_index = index;
1577 iport->active_phy_mask = 0;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001578 iport->enabled_phy_mask = 0;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001579 iport->last_active_phy = 0;
1580 iport->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001581
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001582 iport->owning_controller = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001583
Dan Williamsffe191c2011-06-29 13:09:25 -07001584 iport->started_request_count = 0;
1585 iport->assigned_device_count = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -07001586
Dan Williamsffe191c2011-06-29 13:09:25 -07001587 iport->reserved_rni = SCU_DUMMY_INDEX;
1588 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -07001589
Dan Williamsffe191c2011-06-29 13:09:25 -07001590 sci_init_timer(&iport->timer, port_timeout);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001591
Dan Williamsffe191c2011-06-29 13:09:25 -07001592 iport->port_task_scheduler_registers = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001593
1594 for (index = 0; index < SCI_MAX_PHYS; index++)
Dan Williamsffe191c2011-06-29 13:09:25 -07001595 iport->phy_table[index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001596}
1597
1598void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1599{
1600 INIT_LIST_HEAD(&iport->remote_dev_list);
1601 INIT_LIST_HEAD(&iport->domain_dev_list);
Dan Williamse2f8db52011-05-10 02:28:46 -07001602 iport->isci_host = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001603}
1604
Dan Williams89a73012011-06-30 19:14:33 -07001605void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -07001606{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001607 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001608
1609 /* notify the user. */
Dan Williamsffe191c2011-06-29 13:09:25 -07001610 isci_port_bc_change_received(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -07001611}
1612
Dan Williams4393aa42011-03-31 13:10:44 -07001613int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1614 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001615{
Dan Williams4393aa42011-03-31 13:10:44 -07001616 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001617 enum sci_status status;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001618 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001619
Dan Williams4393aa42011-03-31 13:10:44 -07001620 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1621 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001622
Dan Williams4393aa42011-03-31 13:10:44 -07001623 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001624
Dan Williams4393aa42011-03-31 13:10:44 -07001625 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001626
1627 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams89a73012011-06-30 19:14:33 -07001628 status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001629
Dan Williams4393aa42011-03-31 13:10:44 -07001630 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001631
1632 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -07001633 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001634
Dan Williams4393aa42011-03-31 13:10:44 -07001635 dev_dbg(&ihost->pdev->dev,
1636 "%s: iport = %p; hard reset completion\n",
1637 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001638
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001639 if (iport->hard_reset_status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001640 ret = TMF_RESP_FUNC_FAILED;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001641
1642 dev_err(&ihost->pdev->dev,
1643 "%s: iport = %p; hard reset failed (0x%x)\n",
1644 __func__, iport, iport->hard_reset_status);
1645 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001646 } else {
1647 ret = TMF_RESP_FUNC_FAILED;
1648
Dan Williams4393aa42011-03-31 13:10:44 -07001649 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001650 "%s: iport = %p; sci_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001651 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001652 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001653
1654 }
1655
1656 /* If the hard reset for the port has failed, consider this
1657 * the same as link failures on all phys in the port.
1658 */
1659 if (ret != TMF_RESP_FUNC_COMPLETE) {
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001660
Dan Williams4393aa42011-03-31 13:10:44 -07001661 dev_err(&ihost->pdev->dev,
1662 "%s: iport = %p; hard reset failed "
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001663 "(0x%x) - driving explicit link fail for all phys\n",
1664 __func__, iport, iport->hard_reset_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001665 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001666 return ret;
1667}
Dave Jiang09d7da12011-03-26 16:11:51 -07001668
Dan Williamse2f8db52011-05-10 02:28:46 -07001669void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001670{
Dan Williamsc132f692012-01-03 23:26:08 -08001671 struct isci_host *ihost = phy->ha->lldd_ha;
1672 struct isci_port *iport = phy->port->lldd_port;
1673 unsigned long flags;
1674 int i;
1675
1676 /* we got a port notification on a port that was subsequently
1677 * torn down and libsas is just now catching up
1678 */
1679 if (!iport)
1680 return;
1681
1682 spin_lock_irqsave(&ihost->scic_lock, flags);
1683 for (i = 0; i < SCI_MAX_PHYS; i++) {
1684 if (iport->active_phy_mask & 1 << i)
1685 break;
1686 }
1687 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1688
1689 if (i >= SCI_MAX_PHYS)
1690 dev_dbg(&ihost->pdev->dev, "%s: port: %ld\n",
1691 __func__, (long) (iport - &ihost->ports[0]));
Dan Williamse2f8db52011-05-10 02:28:46 -07001692}
1693
Dan Williamse2f8db52011-05-10 02:28:46 -07001694void isci_port_formed(struct asd_sas_phy *phy)
1695{
Dan Williamsc132f692012-01-03 23:26:08 -08001696 struct isci_host *ihost = phy->ha->lldd_ha;
1697 struct isci_phy *iphy = to_iphy(phy);
1698 struct asd_sas_port *port = phy->port;
1699 struct isci_port *iport;
1700 unsigned long flags;
1701 int i;
1702
1703 /* initial ports are formed as the driver is still initializing,
1704 * wait for that process to complete
1705 */
1706 wait_for_start(ihost);
1707
1708 spin_lock_irqsave(&ihost->scic_lock, flags);
1709 for (i = 0; i < SCI_MAX_PORTS; i++) {
1710 iport = &ihost->ports[i];
1711 if (iport->active_phy_mask & 1 << iphy->phy_index)
1712 break;
1713 }
1714 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1715
1716 if (i >= SCI_MAX_PORTS)
1717 iport = NULL;
1718
1719 port->lldd_port = iport;
Dave Jiang09d7da12011-03-26 16:11:51 -07001720}