blob: a4dd5c91508cc379b2a52ea0414e6508f720ce02 [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 Williamsd7a0ccd2012-02-10 01:18:44 -080063#undef C
64#define C(a) (#a)
65const char *port_state_name(enum sci_port_states state)
66{
67 static const char * const strings[] = PORT_STATES;
68
69 return strings[state];
70}
71#undef C
72
Dan Williamsc132f692012-01-03 23:26:08 -080073static struct device *sciport_to_dev(struct isci_port *iport)
74{
75 int i = iport->physical_port_index;
76 struct isci_port *table;
77 struct isci_host *ihost;
78
79 if (i == SCIC_SDS_DUMMY_PORT)
80 i = SCI_MAX_PORTS+1;
81
82 table = iport - i;
83 ihost = container_of(table, typeof(*ihost), ports[0]);
84
85 return &ihost->pdev->dev;
86}
87
Dan Williams89a73012011-06-30 19:14:33 -070088static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
Dan Williams6f231dd2011-07-02 22:56:22 -070089{
Dan Williamse2f8db52011-05-10 02:28:46 -070090 u8 index;
91
Dan Williams89a73012011-06-30 19:14:33 -070092 proto->all = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -070093 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -070094 struct isci_phy *iphy = iport->phy_table[index];
95
96 if (!iphy)
97 continue;
98 sci_phy_get_protocols(iphy, proto);
Dan Williamse2f8db52011-05-10 02:28:46 -070099 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700100}
101
Dan Williams89a73012011-06-30 19:14:33 -0700102static u32 sci_port_get_phys(struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -0700103{
Dan Williamse2f8db52011-05-10 02:28:46 -0700104 u32 index;
105 u32 mask;
106
107 mask = 0;
Dan Williams89a73012011-06-30 19:14:33 -0700108 for (index = 0; index < SCI_MAX_PHYS; index++)
109 if (iport->phy_table[index])
Dan Williamse2f8db52011-05-10 02:28:46 -0700110 mask |= (1 << index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700111
112 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700113}
114
Dan Williamse2f8db52011-05-10 02:28:46 -0700115/**
Dan Williams89a73012011-06-30 19:14:33 -0700116 * sci_port_get_properties() - This method simply returns the properties
Dan Williamse2f8db52011-05-10 02:28:46 -0700117 * regarding the port, such as: physical index, protocols, sas address, etc.
118 * @port: this parameter specifies the port for which to retrieve the physical
119 * index.
120 * @properties: This parameter specifies the properties structure into which to
121 * copy the requested information.
122 *
123 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
124 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
125 * value is returned if the specified port is not valid. When this value is
126 * returned, no data is copied to the properties output parameter.
127 */
Bartek Nowakowski7e629842012-01-04 01:33:20 -0800128enum sci_status sci_port_get_properties(struct isci_port *iport,
Dan Williams89a73012011-06-30 19:14:33 -0700129 struct sci_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700130{
Dan Williamsffe191c2011-06-29 13:09:25 -0700131 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamse2f8db52011-05-10 02:28:46 -0700132 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700133
Dan Williams89a73012011-06-30 19:14:33 -0700134 prop->index = iport->logical_port_index;
135 prop->phy_mask = sci_port_get_phys(iport);
136 sci_port_get_sas_address(iport, &prop->local.sas_address);
137 sci_port_get_protocols(iport, &prop->local.protocols);
138 sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700139
Dan Williamse2f8db52011-05-10 02:28:46 -0700140 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700141}
142
Dan Williams89a73012011-06-30 19:14:33 -0700143static void sci_port_bcn_enable(struct isci_port *iport)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700144{
Dan Williams85280952011-06-28 15:05:53 -0700145 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700146 u32 val;
147 int i;
148
Dan Williamsffe191c2011-06-29 13:09:25 -0700149 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
150 iphy = iport->phy_table[i];
Dan Williams85280952011-06-28 15:05:53 -0700151 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700152 continue;
Dan Williams85280952011-06-28 15:05:53 -0700153 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700154 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700155 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700156 }
157}
158
Dan Williamsffe191c2011-06-29 13:09:25 -0700159static void isci_port_bc_change_received(struct isci_host *ihost,
160 struct isci_port *iport,
161 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700162{
Dan Williams52d74632011-10-27 15:05:37 -0700163 dev_dbg(&ihost->pdev->dev,
164 "%s: isci_phy = %p, sas_phy = %p\n",
165 __func__, iphy, &iphy->sas_phy);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700166
Dan Williams52d74632011-10-27 15:05:37 -0700167 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
Dan Williams89a73012011-06-30 19:14:33 -0700168 sci_port_bcn_enable(iport);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700169}
170
Dan Williamse2f8db52011-05-10 02:28:46 -0700171static void isci_port_link_up(struct isci_host *isci_host,
Dan Williamsffe191c2011-06-29 13:09:25 -0700172 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700173 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700174{
175 unsigned long flags;
Dan Williams89a73012011-06-30 19:14:33 -0700176 struct sci_port_properties properties;
Dan Williams6f231dd2011-07-02 22:56:22 -0700177 unsigned long success = true;
178
Dan Williams6f231dd2011-07-02 22:56:22 -0700179 dev_dbg(&isci_host->pdev->dev,
180 "%s: isci_port = %p\n",
Dan Williamsffe191c2011-06-29 13:09:25 -0700181 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700182
Dan Williams85280952011-06-28 15:05:53 -0700183 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700184
Dan Williams89a73012011-06-30 19:14:33 -0700185 sci_port_get_properties(iport, &properties);
Dan Williams6f231dd2011-07-02 22:56:22 -0700186
Dan Williamsc79dd802012-02-01 00:44:14 -0800187 if (iphy->protocol == SAS_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800188 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700189
Dan Williams85280952011-06-28 15:05:53 -0700190 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
191 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700192
193 /*
194 * For direct-attached SATA devices, the SCI core will
195 * automagically assign a SAS address to the end device
196 * for the purpose of creating a port. This SAS address
197 * will not be the same as assigned to the PHY and needs
Dan Williams89a73012011-06-30 19:14:33 -0700198 * to be obtained from struct sci_port_properties properties.
Dan Williams6f231dd2011-07-02 22:56:22 -0700199 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800200 attached_sas_address = properties.remote.sas_address.high;
201 attached_sas_address <<= 32;
202 attached_sas_address |= properties.remote.sas_address.low;
203 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700204
Dan Williams85280952011-06-28 15:05:53 -0700205 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800206 &attached_sas_address, sizeof(attached_sas_address));
Dan Williamsc79dd802012-02-01 00:44:14 -0800207 } else if (iphy->protocol == SAS_PROTOCOL_SSP) {
Dan Williams85280952011-06-28 15:05:53 -0700208 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
209 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700210
211 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700212 memcpy(iphy->sas_phy.attached_sas_addr,
213 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700214 } else {
Masanari Iida59e13d42012-04-25 00:24:16 +0900215 dev_err(&isci_host->pdev->dev, "%s: unknown target\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700216 success = false;
217 }
218
Dan Williams85280952011-06-28 15:05:53 -0700219 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800220
Dan Williams85280952011-06-28 15:05:53 -0700221 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700222
223 /* Notify libsas that we have an address frame, if indeed
224 * we've found an SSP, SMP, or STP target */
225 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700226 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700227 PORTE_BYTES_DMAED);
228}
229
230
231/**
232 * isci_port_link_down() - This function is called by the sci core when a link
233 * becomes inactive.
234 * @isci_host: This parameter specifies the isci host object.
235 * @phy: This parameter specifies the isci phy with the active link.
236 * @port: This parameter specifies the isci port with the active link.
237 *
238 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700239static void isci_port_link_down(struct isci_host *isci_host,
240 struct isci_phy *isci_phy,
241 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700242{
Jeff Skirvin397497d2012-03-10 05:46:46 +0000243 struct isci_remote_device *isci_device;
244
Dan Williams6f231dd2011-07-02 22:56:22 -0700245 dev_dbg(&isci_host->pdev->dev,
246 "%s: isci_port = %p\n", __func__, isci_port);
247
Jeff Skirvin397497d2012-03-10 05:46:46 +0000248 if (isci_port) {
249
250 /* check to see if this is the last phy on this port. */
251 if (isci_phy->sas_phy.port &&
252 isci_phy->sas_phy.port->num_phys == 1) {
253 /* change the state for all devices on this port. The
254 * next task sent to this device will be returned as
255 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
256 * remove the target
257 */
258 list_for_each_entry(isci_device,
259 &isci_port->remote_dev_list,
260 node) {
261 dev_dbg(&isci_host->pdev->dev,
262 "%s: isci_device = %p\n",
263 __func__, isci_device);
264 set_bit(IDEV_GONE, &isci_device->flags);
265 }
266 }
267 }
268
Dan Williams6f231dd2011-07-02 22:56:22 -0700269 /* Notify libsas of the borken link, this will trigger calls to our
270 * isci_port_deformed and isci_dev_gone functions.
271 */
272 sas_phy_disconnected(&isci_phy->sas_phy);
273 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
274 PHYE_LOSS_OF_SIGNAL);
275
Dan Williams6f231dd2011-07-02 22:56:22 -0700276 dev_dbg(&isci_host->pdev->dev,
277 "%s: isci_port = %p - Done\n", __func__, isci_port);
278}
279
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700280static bool is_port_ready_state(enum sci_port_states state)
281{
282 switch (state) {
283 case SCI_PORT_READY:
284 case SCI_PORT_SUB_WAITING:
285 case SCI_PORT_SUB_OPERATIONAL:
286 case SCI_PORT_SUB_CONFIGURING:
287 return true;
288 default:
289 return false;
290 }
291}
292
293/* flag dummy rnc hanling when exiting a ready state */
294static void port_state_machine_change(struct isci_port *iport,
295 enum sci_port_states state)
296{
297 struct sci_base_state_machine *sm = &iport->sm;
298 enum sci_port_states old_state = sm->current_state_id;
299
300 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
301 iport->ready_exit = true;
302
303 sci_change_state(sm, state);
304 iport->ready_exit = false;
305}
306
Dan Williams6f231dd2011-07-02 22:56:22 -0700307/**
308 * isci_port_hard_reset_complete() - This function is called by the sci core
309 * when the hard reset complete notification has been received.
310 * @port: This parameter specifies the sci port with the active link.
311 * @completion_status: This parameter specifies the core status for the reset
312 * process.
313 *
314 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700315static void isci_port_hard_reset_complete(struct isci_port *isci_port,
316 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700317{
Dan Williams92776992011-11-30 11:57:34 -0800318 struct isci_host *ihost = isci_port->owning_controller;
319
320 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700321 "%s: isci_port = %p, completion_status=%x\n",
322 __func__, isci_port, completion_status);
323
324 /* Save the status of the hard reset from the port. */
325 isci_port->hard_reset_status = completion_status;
326
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700327 if (completion_status != SCI_SUCCESS) {
328
329 /* The reset failed. The port state is now SCI_PORT_FAILED. */
330 if (isci_port->active_phy_mask == 0) {
Dan Williams92776992011-11-30 11:57:34 -0800331 int phy_idx = isci_port->last_active_phy;
332 struct isci_phy *iphy = &ihost->phys[phy_idx];
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700333
334 /* Generate the link down now to the host, since it
335 * was intercepted by the hard reset state machine when
336 * it really happened.
337 */
Dan Williams92776992011-11-30 11:57:34 -0800338 isci_port_link_down(ihost, iphy, isci_port);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700339 }
340 /* Advance the port state so that link state changes will be
Dan Williams92776992011-11-30 11:57:34 -0800341 * noticed.
342 */
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700343 port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
344
345 }
Dan Williams92776992011-11-30 11:57:34 -0800346 clear_bit(IPORT_RESET_PENDING, &isci_port->state);
347 wake_up(&ihost->eventq);
348
Dan Williams6f231dd2011-07-02 22:56:22 -0700349}
Dan Williams4393aa42011-03-31 13:10:44 -0700350
Dan Williamse2f8db52011-05-10 02:28:46 -0700351/* This method will return a true value if the specified phy can be assigned to
352 * this port The following is a list of phys for each port that are allowed: -
353 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
354 * doesn't preclude all configurations. It merely ensures that a phy is part
355 * of the allowable set of phy identifiers for that port. For example, one
356 * could assign phy 3 to port 0 and no other phys. Please refer to
Dan Williams89a73012011-06-30 19:14:33 -0700357 * sci_port_is_phy_mask_valid() for information regarding whether the
Dan Williamse2f8db52011-05-10 02:28:46 -0700358 * phy_mask for a port can be supported. bool true if this is a valid phy
359 * assignment for the port false if this is not a valid phy assignment for the
360 * port
361 */
Dan Williams89a73012011-06-30 19:14:33 -0700362bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700363{
Dan Williams89a73012011-06-30 19:14:33 -0700364 struct isci_host *ihost = iport->owning_controller;
365 struct sci_user_parameters *user = &ihost->user_parameters;
366
Dan Williamse2f8db52011-05-10 02:28:46 -0700367 /* Initialize to invalid value. */
368 u32 existing_phy_index = SCI_MAX_PHYS;
369 u32 index;
370
Dan Williams89a73012011-06-30 19:14:33 -0700371 if ((iport->physical_port_index == 1) && (phy_index != 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700372 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700373
Dan Williams89a73012011-06-30 19:14:33 -0700374 if (iport->physical_port_index == 3 && phy_index != 3)
Dan Williamse2f8db52011-05-10 02:28:46 -0700375 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700376
Dan Williams89a73012011-06-30 19:14:33 -0700377 if (iport->physical_port_index == 2 &&
378 (phy_index == 0 || phy_index == 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700379 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700380
Dan Williams89a73012011-06-30 19:14:33 -0700381 for (index = 0; index < SCI_MAX_PHYS; index++)
382 if (iport->phy_table[index] && index != phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700383 existing_phy_index = index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700384
Dan Williams89a73012011-06-30 19:14:33 -0700385 /* Ensure that all of the phys in the port are capable of
386 * operating at the same maximum link rate.
387 */
388 if (existing_phy_index < SCI_MAX_PHYS &&
389 user->phys[phy_index].max_speed_generation !=
390 user->phys[existing_phy_index].max_speed_generation)
Dan Williamse2f8db52011-05-10 02:28:46 -0700391 return false;
392
393 return true;
394}
395
396/**
397 *
398 * @sci_port: This is the port object for which to determine if the phy mask
399 * can be supported.
400 *
401 * This method will return a true value if the port's phy mask can be supported
402 * by the SCU. The following is a list of valid PHY mask configurations for
403 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
404 * - Port 3 - [3] This method returns a boolean indication specifying if the
405 * phy mask can be supported. true if this is a valid phy assignment for the
406 * port false if this is not a valid phy assignment for the port
407 */
Dan Williams89a73012011-06-30 19:14:33 -0700408static bool sci_port_is_phy_mask_valid(
Dan Williamsffe191c2011-06-29 13:09:25 -0700409 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700410 u32 phy_mask)
411{
Dan Williamsffe191c2011-06-29 13:09:25 -0700412 if (iport->physical_port_index == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700413 if (((phy_mask & 0x0F) == 0x0F)
414 || ((phy_mask & 0x03) == 0x03)
415 || ((phy_mask & 0x01) == 0x01)
416 || (phy_mask == 0))
417 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700418 } else if (iport->physical_port_index == 1) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700419 if (((phy_mask & 0x02) == 0x02)
420 || (phy_mask == 0))
421 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700422 } else if (iport->physical_port_index == 2) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700423 if (((phy_mask & 0x0C) == 0x0C)
424 || ((phy_mask & 0x04) == 0x04)
425 || (phy_mask == 0))
426 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700427 } else if (iport->physical_port_index == 3) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700428 if (((phy_mask & 0x08) == 0x08)
429 || (phy_mask == 0))
430 return true;
431 }
432
433 return false;
434}
435
Dan Williams85280952011-06-28 15:05:53 -0700436/*
Dan Williamse2f8db52011-05-10 02:28:46 -0700437 * This method retrieves a currently active (i.e. connected) phy contained in
438 * the port. Currently, the lowest order phy that is connected is returned.
439 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
440 * returned if there are no currently active (i.e. connected to a remote end
Dan Williams89a73012011-06-30 19:14:33 -0700441 * point) phys contained in the port. All other values specify a struct sci_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700442 * object that is active in the port.
443 */
Dan Williams89a73012011-06-30 19:14:33 -0700444static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
Dan Williams85280952011-06-28 15:05:53 -0700445{
Dan Williamse2f8db52011-05-10 02:28:46 -0700446 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700447 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700448
449 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700450 /* Ensure that the phy is both part of the port and currently
451 * connected to the remote end-point.
452 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700453 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700454 if (iphy && sci_port_active_phy(iport, iphy))
Dan Williams85280952011-06-28 15:05:53 -0700455 return iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700456 }
457
458 return NULL;
459}
460
Dan Williams89a73012011-06-30 19:14:33 -0700461static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700462{
Dan Williams85280952011-06-28 15:05:53 -0700463 /* Check to see if we can add this phy to a port
Dan Williamse2f8db52011-05-10 02:28:46 -0700464 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700465 * not already have a phy assinged to the phy index.
466 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700467 if (!iport->phy_table[iphy->phy_index] &&
Dan Williams85280952011-06-28 15:05:53 -0700468 !phy_get_non_dummy_port(iphy) &&
Dan Williams89a73012011-06-30 19:14:33 -0700469 sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams85280952011-06-28 15:05:53 -0700470 /* Phy is being added in the stopped state so we are in MPC mode
471 * make logical port index = physical port index
472 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700473 iport->logical_port_index = iport->physical_port_index;
474 iport->phy_table[iphy->phy_index] = iphy;
Dan Williams89a73012011-06-30 19:14:33 -0700475 sci_phy_set_port(iphy, iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700476
477 return SCI_SUCCESS;
478 }
479
480 return SCI_FAILURE;
481}
482
Dan Williams89a73012011-06-30 19:14:33 -0700483static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700484{
485 /* Make sure that this phy is part of this port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700486 if (iport->phy_table[iphy->phy_index] == iphy &&
487 phy_get_non_dummy_port(iphy) == iport) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700488 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700489
490 /* Yep it is assigned to this port so remove it */
Dan Williams89a73012011-06-30 19:14:33 -0700491 sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
Dan Williamsffe191c2011-06-29 13:09:25 -0700492 iport->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -0700493 return SCI_SUCCESS;
494 }
495
496 return SCI_FAILURE;
497}
498
Dan Williams89a73012011-06-30 19:14:33 -0700499void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700500{
501 u32 index;
502
Dan Williams89a73012011-06-30 19:14:33 -0700503 sas->high = 0;
504 sas->low = 0;
505 for (index = 0; index < SCI_MAX_PHYS; index++)
506 if (iport->phy_table[index])
507 sci_phy_get_sas_address(iport->phy_table[index], sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700508}
509
Dan Williams89a73012011-06-30 19:14:33 -0700510void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700511{
Dan Williams85280952011-06-28 15:05:53 -0700512 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700513
514 /*
515 * Ensure that the phy is both part of the port and currently
516 * connected to the remote end-point.
517 */
Dan Williams89a73012011-06-30 19:14:33 -0700518 iphy = sci_port_get_a_connected_phy(iport);
Dan Williams85280952011-06-28 15:05:53 -0700519 if (iphy) {
Dan Williamsc79dd802012-02-01 00:44:14 -0800520 if (iphy->protocol != SAS_PROTOCOL_SATA) {
Dan Williams89a73012011-06-30 19:14:33 -0700521 sci_phy_get_attached_sas_address(iphy, sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700522 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700523 sci_phy_get_sas_address(iphy, sas);
524 sas->low += iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700525 }
526 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700527 sas->high = 0;
528 sas->low = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700529 }
530}
531
532/**
Dan Williams89a73012011-06-30 19:14:33 -0700533 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
Dan Williamse2f8db52011-05-10 02:28:46 -0700534 *
535 * @sci_port: logical port on which we need to create the remote node context
536 * @rni: remote node index for this remote node context.
537 *
538 * This routine will construct a dummy remote node context data structure
539 * This structure will be posted to the hardware to work around a scheduler
540 * error in the hardware.
541 */
Dan Williams89a73012011-06-30 19:14:33 -0700542static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
Dan Williamse2f8db52011-05-10 02:28:46 -0700543{
544 union scu_remote_node_context *rnc;
545
Dan Williamsffe191c2011-06-29 13:09:25 -0700546 rnc = &iport->owning_controller->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -0700547
548 memset(rnc, 0, sizeof(union scu_remote_node_context));
549
550 rnc->ssp.remote_sas_address_hi = 0;
551 rnc->ssp.remote_sas_address_lo = 0;
552
553 rnc->ssp.remote_node_index = rni;
554 rnc->ssp.remote_node_port_width = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700555 rnc->ssp.logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700556
557 rnc->ssp.nexus_loss_timer_enable = false;
558 rnc->ssp.check_bit = false;
559 rnc->ssp.is_valid = true;
560 rnc->ssp.is_remote_node_context = true;
561 rnc->ssp.function_number = 0;
562 rnc->ssp.arbitration_wait_time = 0;
563}
564
Dan Williamsdd047c82011-06-09 11:06:58 -0700565/*
566 * construct a dummy task context data structure. This
Dan Williamse2f8db52011-05-10 02:28:46 -0700567 * structure will be posted to the hardwre to work around a scheduler error
568 * in the hardware.
Dan Williamse2f8db52011-05-10 02:28:46 -0700569 */
Dan Williams89a73012011-06-30 19:14:33 -0700570static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
Dan Williamse2f8db52011-05-10 02:28:46 -0700571{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700572 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700573 struct scu_task_context *task_context;
574
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700575 task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700576 memset(task_context, 0, sizeof(struct scu_task_context));
577
Dan Williamse2f8db52011-05-10 02:28:46 -0700578 task_context->initiator_request = 1;
579 task_context->connection_rate = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700580 task_context->logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700581 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700582 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700583 task_context->valid = SCU_TASK_CONTEXT_VALID;
584 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamsffe191c2011-06-29 13:09:25 -0700585 task_context->remote_node_index = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -0700586 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db52011-05-10 02:28:46 -0700587 task_context->task_phase = 0x01;
588}
589
Dan Williams89a73012011-06-30 19:14:33 -0700590static void sci_port_destroy_dummy_resources(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700591{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700592 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700593
Dan Williamsffe191c2011-06-29 13:09:25 -0700594 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700595 isci_free_tag(ihost, iport->reserved_tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700596
Dan Williamsffe191c2011-06-29 13:09:25 -0700597 if (iport->reserved_rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700598 sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
Dan Williamsffe191c2011-06-29 13:09:25 -0700599 1, iport->reserved_rni);
Dan Williamse2f8db52011-05-10 02:28:46 -0700600
Dan Williamsffe191c2011-06-29 13:09:25 -0700601 iport->reserved_rni = SCU_DUMMY_INDEX;
602 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -0700603}
604
Dan Williams89a73012011-06-30 19:14:33 -0700605void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
Dan Williamse2f8db52011-05-10 02:28:46 -0700606{
607 u8 index;
608
609 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700610 if (iport->active_phy_mask & (1 << index))
Dan Williams89a73012011-06-30 19:14:33 -0700611 sci_phy_setup_transport(iport->phy_table[index], device_id);
Dan Williamse2f8db52011-05-10 02:28:46 -0700612 }
613}
614
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800615static void sci_port_resume_phy(struct isci_port *iport, struct isci_phy *iphy)
616{
617 sci_phy_resume(iphy);
618 iport->enabled_phy_mask |= 1 << iphy->phy_index;
619}
620
621static void sci_port_activate_phy(struct isci_port *iport,
622 struct isci_phy *iphy,
623 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700624{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700625 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700626
Dan Williamsc79dd802012-02-01 00:44:14 -0800627 if (iphy->protocol != SAS_PROTOCOL_SATA && (flags & PF_RESUME))
Dan Williams89a73012011-06-30 19:14:33 -0700628 sci_phy_resume(iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700629
Dan Williamsffe191c2011-06-29 13:09:25 -0700630 iport->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700631
Dan Williams89a73012011-06-30 19:14:33 -0700632 sci_controller_clear_invalid_phy(ihost, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700633
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800634 if (flags & PF_NOTIFY)
Dan Williamsffe191c2011-06-29 13:09:25 -0700635 isci_port_link_up(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700636}
637
Dan Williams89a73012011-06-30 19:14:33 -0700638void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
639 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700640{
Dan Williams34a99152011-07-01 02:25:15 -0700641 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700642
Dan Williamsffe191c2011-06-29 13:09:25 -0700643 iport->active_phy_mask &= ~(1 << iphy->phy_index);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800644 iport->enabled_phy_mask &= ~(1 << iphy->phy_index);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700645 if (!iport->active_phy_mask)
646 iport->last_active_phy = iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700647
Dan Williams85280952011-06-28 15:05:53 -0700648 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db52011-05-10 02:28:46 -0700649
Marcin Tomczakd4ec1cf2012-01-04 01:33:15 -0800650 /* Re-assign the phy back to the LP as if it were a narrow port for APC
651 * mode. For MPC mode, the phy will remain in the port.
652 */
653 if (iport->owning_controller->oem_parameters.controller.mode_type ==
654 SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE)
655 writel(iphy->phy_index,
656 &iport->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700657
658 if (do_notify_user == true)
659 isci_port_link_down(ihost, iphy, iport);
660}
661
Dan Williams89a73012011-06-30 19:14:33 -0700662static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700663{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700664 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700665
666 /*
667 * Check to see if we have alreay reported this link as bad and if
668 * not go ahead and tell the SCI_USER that we have discovered an
669 * invalid link.
670 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700671 if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700672 ihost->invalid_phy_mask |= 1 << iphy->phy_index;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700673 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700674 }
675}
676
677/**
Dan Williams89a73012011-06-30 19:14:33 -0700678 * sci_port_general_link_up_handler - phy can be assigned to port?
679 * @sci_port: sci_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700680 * @sci_phy: This is the struct isci_phy object that has gone link up.
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800681 * @flags: PF_RESUME, PF_NOTIFY to sci_port_activate_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700682 *
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800683 * Determine if this phy can be assigned to this port . If the phy is
684 * not a valid PHY for this port then the function will notify the user.
685 * A PHY can only be part of a port if it's attached SAS ADDRESS is the
686 * same as all other PHYs in the same port.
Dan Williamse2f8db52011-05-10 02:28:46 -0700687 */
Dan Williams89a73012011-06-30 19:14:33 -0700688static void sci_port_general_link_up_handler(struct isci_port *iport,
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800689 struct isci_phy *iphy,
690 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700691{
692 struct sci_sas_address port_sas_address;
693 struct sci_sas_address phy_sas_address;
694
Dan Williams89a73012011-06-30 19:14:33 -0700695 sci_port_get_attached_sas_address(iport, &port_sas_address);
696 sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700697
698 /* If the SAS address of the new phy matches the SAS address of
699 * other phys in the port OR this is the first phy in the port,
700 * then activate the phy and allow it to be used for operations
701 * in this port.
702 */
703 if ((phy_sas_address.high == port_sas_address.high &&
704 phy_sas_address.low == port_sas_address.low) ||
Dan Williamsffe191c2011-06-29 13:09:25 -0700705 iport->active_phy_mask == 0) {
706 struct sci_base_state_machine *sm = &iport->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700707
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800708 sci_port_activate_phy(iport, iphy, flags);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000709 if (sm->current_state_id == SCI_PORT_RESETTING)
Dan Williamsffe191c2011-06-29 13:09:25 -0700710 port_state_machine_change(iport, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700711 } else
Dan Williams89a73012011-06-30 19:14:33 -0700712 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700713}
714
715
716
717/**
718 * This method returns false if the port only has a single phy object assigned.
719 * If there are no phys or more than one phy then the method will return
720 * true.
721 * @sci_port: The port for which the wide port condition is to be checked.
722 *
723 * bool true Is returned if this is a wide ported port. false Is returned if
724 * this is a narrow port.
725 */
Dan Williams89a73012011-06-30 19:14:33 -0700726static bool sci_port_is_wide(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700727{
728 u32 index;
729 u32 phy_count = 0;
730
731 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700732 if (iport->phy_table[index] != NULL) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700733 phy_count++;
734 }
735 }
736
737 return phy_count != 1;
738}
739
740/**
741 * This method is called by the PHY object when the link is detected. if the
742 * port wants the PHY to continue on to the link up state then the port
743 * layer must return true. If the port object returns false the phy object
744 * must halt its attempt to go link up.
745 * @sci_port: The port associated with the phy object.
746 * @sci_phy: The phy object that is trying to go link up.
747 *
748 * true if the phy object can continue to the link up condition. true Is
749 * returned if this phy can continue to the ready state. false Is returned if
750 * can not continue on to the ready state. This notification is in place for
751 * wide ports and direct attached phys. Since there are no wide ported SATA
752 * devices this could become an invalid port configuration.
753 */
Dan Williamsc79dd802012-02-01 00:44:14 -0800754bool sci_port_link_detected(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700755{
Dan Williamsffe191c2011-06-29 13:09:25 -0700756 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Dan Williamsc79dd802012-02-01 00:44:14 -0800757 (iphy->protocol == SAS_PROTOCOL_SATA)) {
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800758 if (sci_port_is_wide(iport)) {
759 sci_port_invalid_link_up(iport, iphy);
760 return false;
761 } else {
762 struct isci_host *ihost = iport->owning_controller;
763 struct isci_port *dst_port = &(ihost->ports[iphy->phy_index]);
764 writel(iphy->phy_index,
765 &dst_port->port_pe_configuration_register[iphy->phy_index]);
766 }
Dan Williamse2f8db52011-05-10 02:28:46 -0700767 }
768
769 return true;
770}
771
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000772static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700773{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000774 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsffe191c2011-06-29 13:09:25 -0700775 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700776 struct isci_host *ihost = iport->owning_controller;
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000777 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700778 u32 current_state;
779
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000780 spin_lock_irqsave(&ihost->scic_lock, flags);
781
782 if (tmr->cancel)
783 goto done;
784
Dan Williamsffe191c2011-06-29 13:09:25 -0700785 current_state = iport->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700786
Edmund Nadolskie3013702011-06-02 00:10:43 +0000787 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000788 /* if the port is still in the resetting state then the timeout
789 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700790 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700791 port_state_machine_change(iport, SCI_PORT_FAILED);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000792 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000793 /* if the port is stopped then the start request failed In this
794 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700795 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700796 dev_err(sciport_to_dev(iport),
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900797 "%s: SCIC Port 0x%p failed to stop before timeout.\n",
Dan Williamse2f8db52011-05-10 02:28:46 -0700798 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -0700799 iport);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000800 } else if (current_state == SCI_PORT_STOPPING) {
Dan Williamsfca4ecb2012-01-03 23:26:15 -0800801 dev_dbg(sciport_to_dev(iport),
802 "%s: port%d: stop complete timeout\n",
803 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700804 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000805 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700806 * reporting a timeout this should not happen.
807 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700808 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700809 "%s: SCIC Port 0x%p is processing a timeout operation "
Dan Williamsffe191c2011-06-29 13:09:25 -0700810 "in state %d.\n", __func__, iport, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700811 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000812
813done:
814 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700815}
816
817/* --------------------------------------------------------------------------- */
818
819/**
820 * This function updates the hardwares VIIT entry for this port.
821 *
822 *
823 */
Dan Williams89a73012011-06-30 19:14:33 -0700824static void sci_port_update_viit_entry(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700825{
826 struct sci_sas_address sas_address;
827
Dan Williams89a73012011-06-30 19:14:33 -0700828 sci_port_get_sas_address(iport, &sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700829
830 writel(sas_address.high,
Dan Williamsffe191c2011-06-29 13:09:25 -0700831 &iport->viit_registers->initiator_sas_address_hi);
Dan Williamse2f8db52011-05-10 02:28:46 -0700832 writel(sas_address.low,
Dan Williamsffe191c2011-06-29 13:09:25 -0700833 &iport->viit_registers->initiator_sas_address_lo);
Dan Williamse2f8db52011-05-10 02:28:46 -0700834
835 /* This value get cleared just in case its not already cleared */
Dan Williamsffe191c2011-06-29 13:09:25 -0700836 writel(0, &iport->viit_registers->reserved);
Dan Williamse2f8db52011-05-10 02:28:46 -0700837
838 /* We are required to update the status register last */
839 writel(SCU_VIIT_ENTRY_ID_VIIT |
840 SCU_VIIT_IPPT_INITIATOR |
Dan Williamsffe191c2011-06-29 13:09:25 -0700841 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
Dan Williamse2f8db52011-05-10 02:28:46 -0700842 SCU_VIIT_STATUS_ALL_VALID,
Dan Williamsffe191c2011-06-29 13:09:25 -0700843 &iport->viit_registers->status);
Dan Williamse2f8db52011-05-10 02:28:46 -0700844}
845
Dan Williams89a73012011-06-30 19:14:33 -0700846enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700847{
848 u16 index;
Dan Williams85280952011-06-28 15:05:53 -0700849 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700850 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700851
852 /*
853 * Loop through all of the phys in this port and find the phy with the
854 * lowest maximum link rate. */
855 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700856 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700857 if (iphy && sci_port_active_phy(iport, iphy) &&
Dan Williams85280952011-06-28 15:05:53 -0700858 iphy->max_negotiated_speed < max_allowed_speed)
859 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db52011-05-10 02:28:46 -0700860 }
861
862 return max_allowed_speed;
863}
864
Dan Williams89a73012011-06-30 19:14:33 -0700865static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700866{
867 u32 pts_control_value;
868
Dan Williamsffe191c2011-06-29 13:09:25 -0700869 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700870 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700871 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700872}
873
874/**
Dan Williams89a73012011-06-30 19:14:33 -0700875 * sci_port_post_dummy_request() - post dummy/workaround request
Dan Williamse2f8db52011-05-10 02:28:46 -0700876 * @sci_port: port to post task
877 *
878 * Prevent the hardware scheduler from posting new requests to the front
879 * of the scheduler queue causing a starvation problem for currently
880 * ongoing requests.
881 *
882 */
Dan Williams89a73012011-06-30 19:14:33 -0700883static void sci_port_post_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700884{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700885 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700886 u16 tag = iport->reserved_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700887 struct scu_task_context *tc;
888 u32 command;
Dan Williamse2f8db52011-05-10 02:28:46 -0700889
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700890 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williams312e0c22011-06-28 13:47:09 -0700891 tc->abort = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700892
893 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700894 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700895 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700896
Dan Williams89a73012011-06-30 19:14:33 -0700897 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700898}
899
900/**
901 * This routine will abort the dummy request. This will alow the hardware to
902 * power down parts of the silicon to save power.
903 *
904 * @sci_port: The port on which the task must be aborted.
905 *
906 */
Dan Williams89a73012011-06-30 19:14:33 -0700907static void sci_port_abort_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700908{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700909 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700910 u16 tag = iport->reserved_tag;
Dan Williamse2f8db52011-05-10 02:28:46 -0700911 struct scu_task_context *tc;
912 u32 command;
913
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700914 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700915 tc->abort = 1;
916
917 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
Dan Williamsffe191c2011-06-29 13:09:25 -0700918 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700919 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700920
Dan Williams89a73012011-06-30 19:14:33 -0700921 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700922}
923
924/**
925 *
Dan Williamsffe191c2011-06-29 13:09:25 -0700926 * @sci_port: This is the struct isci_port object to resume.
Dan Williamse2f8db52011-05-10 02:28:46 -0700927 *
928 * This method will resume the port task scheduler for this port object. none
929 */
930static void
Dan Williams89a73012011-06-30 19:14:33 -0700931sci_port_resume_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700932{
933 u32 pts_control_value;
934
Dan Williamsffe191c2011-06-29 13:09:25 -0700935 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700936 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700937 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700938}
939
Dan Williams89a73012011-06-30 19:14:33 -0700940static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700941{
Dan Williamsffe191c2011-06-29 13:09:25 -0700942 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -0700943
Dan Williams89a73012011-06-30 19:14:33 -0700944 sci_port_suspend_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700945
Dan Williamsffe191c2011-06-29 13:09:25 -0700946 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700947
Dan Williamsffe191c2011-06-29 13:09:25 -0700948 if (iport->active_phy_mask != 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700949 /* At least one of the phys on the port is ready */
Dan Williamsffe191c2011-06-29 13:09:25 -0700950 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000951 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -0700952 }
953}
954
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800955static void scic_sds_port_ready_substate_waiting_exit(
956 struct sci_base_state_machine *sm)
957{
958 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
959 sci_port_resume_port_task_scheduler(iport);
960}
961
Dan Williams89a73012011-06-30 19:14:33 -0700962static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700963{
964 u32 index;
Dan Williamsffe191c2011-06-29 13:09:25 -0700965 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700966 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700967
Dan Williamsfca4ecb2012-01-03 23:26:15 -0800968 dev_dbg(&ihost->pdev->dev, "%s: port%d ready\n",
969 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700970
971 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700972 if (iport->phy_table[index]) {
973 writel(iport->physical_port_index,
974 &iport->port_pe_configuration_register[
975 iport->phy_table[index]->phy_index]);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800976 if (((iport->active_phy_mask^iport->enabled_phy_mask) & (1 << index)) != 0)
977 sci_port_resume_phy(iport, iport->phy_table[index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700978 }
979 }
980
Dan Williams89a73012011-06-30 19:14:33 -0700981 sci_port_update_viit_entry(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700982
Dan Williamse2f8db52011-05-10 02:28:46 -0700983 /*
984 * Post the dummy task for the port so the hardware can schedule
985 * io correctly
986 */
Dan Williams89a73012011-06-30 19:14:33 -0700987 sci_port_post_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700988}
989
Dan Williams89a73012011-06-30 19:14:33 -0700990static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000991{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700992 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700993 u8 phys_index = iport->physical_port_index;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000994 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -0700995 u16 rni = iport->reserved_rni;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000996 u32 command;
997
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700998 rnc = &ihost->remote_node_context_table[rni];
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000999
1000 rnc->ssp.is_valid = false;
1001
1002 /* ensure the preceding tc abort request has reached the
1003 * controller and give it ample time to act before posting the rnc
1004 * invalidate
1005 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001006 readl(&ihost->smu_registers->interrupt_status); /* flush */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001007 udelay(10);
1008
1009 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1010 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1011
Dan Williams89a73012011-06-30 19:14:33 -07001012 sci_controller_post_request(ihost, command);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001013}
1014
Dan Williamse2f8db52011-05-10 02:28:46 -07001015/**
1016 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001017 * @object: This is the object which is cast to a struct isci_port object.
Dan Williamse2f8db52011-05-10 02:28:46 -07001018 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001019 * This method will perform the actions required by the struct isci_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001020 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001021 * the port not ready and suspends the port task scheduler. none
1022 */
Dan Williams89a73012011-06-30 19:14:33 -07001023static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001024{
Dan Williamsffe191c2011-06-29 13:09:25 -07001025 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001026 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001027
1028 /*
1029 * Kill the dummy task for this port if it has not yet posted
1030 * the hardware will treat this as a NOP and just return abort
1031 * complete.
1032 */
Dan Williams89a73012011-06-30 19:14:33 -07001033 sci_port_abort_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001034
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001035 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1036 __func__, iport->physical_port_index);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001037
Dan Williamsffe191c2011-06-29 13:09:25 -07001038 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001039 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001040}
1041
Dan Williams89a73012011-06-30 19:14:33 -07001042static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001043{
Dan Williamsffe191c2011-06-29 13:09:25 -07001044 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001045 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001046
Dan Williamsffe191c2011-06-29 13:09:25 -07001047 if (iport->active_phy_mask == 0) {
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001048 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1049 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -07001050
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001051 port_state_machine_change(iport, SCI_PORT_SUB_WAITING);
1052 } else
1053 port_state_machine_change(iport, SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001054}
1055
Dan Williams89a73012011-06-30 19:14:33 -07001056enum sci_status sci_port_start(struct isci_port *iport)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001057{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001058 struct isci_host *ihost = iport->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001059 enum sci_status status = SCI_SUCCESS;
Dan Williams89a73012011-06-30 19:14:33 -07001060 enum sci_port_states state;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001061 u32 phy_mask;
1062
Dan Williamsffe191c2011-06-29 13:09:25 -07001063 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001064 if (state != SCI_PORT_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001065 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1066 __func__, port_state_name(state));
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001067 return SCI_FAILURE_INVALID_STATE;
1068 }
1069
Dan Williamsffe191c2011-06-29 13:09:25 -07001070 if (iport->assigned_device_count > 0) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001071 /* TODO This is a start failure operation because
1072 * there are still devices assigned to this port.
1073 * There must be no devices assigned to a port on a
1074 * start operation.
1075 */
1076 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1077 }
1078
Dan Williamsffe191c2011-06-29 13:09:25 -07001079 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
Dan Williams89a73012011-06-30 19:14:33 -07001080 u16 rni = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001081 &ihost->available_remote_nodes, 1);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001082
1083 if (rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -07001084 sci_port_construct_dummy_rnc(iport, rni);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001085 else
1086 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williamsffe191c2011-06-29 13:09:25 -07001087 iport->reserved_rni = rni;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001088 }
1089
Dan Williamsffe191c2011-06-29 13:09:25 -07001090 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williams312e0c22011-06-28 13:47:09 -07001091 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001092
Dan Williams312e0c22011-06-28 13:47:09 -07001093 tag = isci_alloc_tag(ihost);
1094 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001095 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001096 else
Dan Williams89a73012011-06-30 19:14:33 -07001097 sci_port_construct_dummy_task(iport, tag);
Dan Williamsffe191c2011-06-29 13:09:25 -07001098 iport->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001099 }
1100
1101 if (status == SCI_SUCCESS) {
Dan Williams89a73012011-06-30 19:14:33 -07001102 phy_mask = sci_port_get_phys(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001103
1104 /*
1105 * There are one or more phys assigned to this port. Make sure
1106 * the port's phy mask is in fact legal and supported by the
1107 * silicon.
1108 */
Dan Williams89a73012011-06-30 19:14:33 -07001109 if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001110 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001111 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001112
1113 return SCI_SUCCESS;
1114 }
1115 status = SCI_FAILURE;
1116 }
1117
1118 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -07001119 sci_port_destroy_dummy_resources(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001120
1121 return status;
1122}
1123
Dan Williams89a73012011-06-30 19:14:33 -07001124enum sci_status sci_port_stop(struct isci_port *iport)
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001125{
Dan Williams89a73012011-06-30 19:14:33 -07001126 enum sci_port_states state;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001127
Dan Williamsffe191c2011-06-29 13:09:25 -07001128 state = iport->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001129 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001130 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001131 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001132 case SCI_PORT_SUB_WAITING:
1133 case SCI_PORT_SUB_OPERATIONAL:
1134 case SCI_PORT_SUB_CONFIGURING:
1135 case SCI_PORT_RESETTING:
Dan Williamsffe191c2011-06-29 13:09:25 -07001136 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001137 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001138 return SCI_SUCCESS;
1139 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001140 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1141 __func__, port_state_name(state));
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001142 return SCI_FAILURE_INVALID_STATE;
1143 }
1144}
1145
Dan Williams89a73012011-06-30 19:14:33 -07001146static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001147{
1148 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001149 struct isci_phy *iphy = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001150 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001151 u32 phy_index;
1152
Dan Williamsffe191c2011-06-29 13:09:25 -07001153 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001154 if (state != SCI_PORT_SUB_OPERATIONAL) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001155 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1156 __func__, port_state_name(state));
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001157 return SCI_FAILURE_INVALID_STATE;
1158 }
1159
1160 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001161 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001162 iphy = iport->phy_table[phy_index];
Dan Williams89a73012011-06-30 19:14:33 -07001163 if (iphy && !sci_port_active_phy(iport, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001164 /*
1165 * We found a phy but it is not ready select
1166 * different phy
1167 */
Dan Williams85280952011-06-28 15:05:53 -07001168 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001169 }
1170 }
1171
1172 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001173 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001174 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001175 status = sci_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001176
1177 if (status != SCI_SUCCESS)
1178 return status;
1179
Dan Williamsffe191c2011-06-29 13:09:25 -07001180 sci_mod_timer(&iport->timer, timeout);
1181 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001182
Dan Williamsffe191c2011-06-29 13:09:25 -07001183 port_state_machine_change(iport, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001184 return SCI_SUCCESS;
1185}
1186
1187/**
Dan Williams89a73012011-06-30 19:14:33 -07001188 * sci_port_add_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001189 * @sci_port: This parameter specifies the port in which the phy will be added.
1190 * @sci_phy: This parameter is the phy which is to be added to the port.
1191 *
1192 * This method will add a PHY to the selected port. This method returns an
1193 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1194 * status is a failure to add the phy to the port.
1195 */
Dan Williams89a73012011-06-30 19:14:33 -07001196enum sci_status sci_port_add_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001197 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001198{
1199 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001200 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001201
Maciej Trela08e73be2012-03-12 23:29:30 +00001202 sci_port_bcn_enable(iport);
1203
Dan Williamsffe191c2011-06-29 13:09:25 -07001204 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001205 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001206 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001207 struct sci_sas_address port_sas_address;
1208
1209 /* Read the port assigned SAS Address if there is one */
Dan Williams89a73012011-06-30 19:14:33 -07001210 sci_port_get_sas_address(iport, &port_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001211
1212 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1213 struct sci_sas_address phy_sas_address;
1214
1215 /* Make sure that the PHY SAS Address matches the SAS Address
1216 * for this port
1217 */
Dan Williams89a73012011-06-30 19:14:33 -07001218 sci_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001219
1220 if (port_sas_address.high != phy_sas_address.high ||
1221 port_sas_address.low != phy_sas_address.low)
1222 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1223 }
Dan Williams89a73012011-06-30 19:14:33 -07001224 return sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001225 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001226 case SCI_PORT_SUB_WAITING:
1227 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001228 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001229
1230 if (status != SCI_SUCCESS)
1231 return status;
1232
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001233 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Dan Williamsffe191c2011-06-29 13:09:25 -07001234 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1235 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001236
1237 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001238 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001239 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001240
1241 if (status != SCI_SUCCESS)
1242 return status;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001243 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001244
1245 /* Re-enter the configuring state since this may be the last phy in
1246 * the port.
1247 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001248 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001249 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001250 return SCI_SUCCESS;
1251 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001252 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1253 __func__, port_state_name(state));
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001254 return SCI_FAILURE_INVALID_STATE;
1255 }
1256}
1257
1258/**
Dan Williams89a73012011-06-30 19:14:33 -07001259 * sci_port_remove_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001260 * @sci_port: This parameter specifies the port in which the phy will be added.
1261 * @sci_phy: This parameter is the phy which is to be added to the port.
1262 *
1263 * This method will remove the PHY from the selected PORT. This method returns
1264 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1265 * other status is a failure to add the phy to the port.
1266 */
Dan Williams89a73012011-06-30 19:14:33 -07001267enum sci_status sci_port_remove_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001268 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001269{
1270 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001271 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001272
Dan Williamsffe191c2011-06-29 13:09:25 -07001273 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001274
1275 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001276 case SCI_PORT_STOPPED:
Dan Williams89a73012011-06-30 19:14:33 -07001277 return sci_port_clear_phy(iport, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001278 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001279 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001280 if (status != SCI_SUCCESS)
1281 return status;
1282
Dan Williams89a73012011-06-30 19:14:33 -07001283 sci_port_deactivate_phy(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001284 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1285 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001286 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001287 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001288 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001289 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001290
1291 if (status != SCI_SUCCESS)
1292 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001293 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001294
1295 /* Re-enter the configuring state since this may be the last phy in
1296 * the port
1297 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001298 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001299 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001300 return SCI_SUCCESS;
1301 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001302 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1303 __func__, port_state_name(state));
Piotr Sawicki051266c2011-05-12 19:10:14 +00001304 return SCI_FAILURE_INVALID_STATE;
1305 }
1306}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001307
Dan Williams89a73012011-06-30 19:14:33 -07001308enum sci_status sci_port_link_up(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001309 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001310{
Dan Williams89a73012011-06-30 19:14:33 -07001311 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001312
Dan Williamsffe191c2011-06-29 13:09:25 -07001313 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001314 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001315 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001316 /* Since this is the first phy going link up for the port we
1317 * can just enable it and continue
1318 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001319 sci_port_activate_phy(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001320
Dan Williamsffe191c2011-06-29 13:09:25 -07001321 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001322 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001323 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001324 case SCI_PORT_SUB_OPERATIONAL:
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001325 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001326 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001327 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001328 /* TODO We should make sure that the phy that has gone
1329 * link up is the same one on which we sent the reset. It is
1330 * possible that the phy on which we sent the reset is not the
1331 * one that has gone link up and we want to make sure that
1332 * phy being reset comes back. Consider the case where a
1333 * reset is sent but before the hardware processes the reset it
1334 * get a link up on the port because of a hot plug event.
1335 * because of the reset request this phy will go link down
1336 * almost immediately.
1337 */
1338
1339 /* In the resetting state we don't notify the user regarding
1340 * link up and link down notifications.
1341 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001342 sci_port_general_link_up_handler(iport, iphy, PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001343 return SCI_SUCCESS;
1344 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001345 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1346 __func__, port_state_name(state));
Piotr Sawicki051266c2011-05-12 19:10:14 +00001347 return SCI_FAILURE_INVALID_STATE;
1348 }
1349}
1350
Dan Williams89a73012011-06-30 19:14:33 -07001351enum sci_status sci_port_link_down(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001352 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001353{
Dan Williams89a73012011-06-30 19:14:33 -07001354 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001355
Dan Williamsffe191c2011-06-29 13:09:25 -07001356 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001357 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001358 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001359 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001360
1361 /* If there are no active phys left in the port, then
1362 * transition the port to the WAITING state until such time
1363 * as a phy goes link up
1364 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001365 if (iport->active_phy_mask == 0)
1366 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001367 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001368 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001369 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001370 /* In the resetting state we don't notify the user regarding
1371 * link up and link down notifications. */
Dan Williams89a73012011-06-30 19:14:33 -07001372 sci_port_deactivate_phy(iport, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001373 return SCI_SUCCESS;
1374 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001375 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1376 __func__, port_state_name(state));
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001377 return SCI_FAILURE_INVALID_STATE;
1378 }
1379}
1380
Dan Williams89a73012011-06-30 19:14:33 -07001381enum sci_status sci_port_start_io(struct isci_port *iport,
1382 struct isci_remote_device *idev,
1383 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001384{
Dan Williams89a73012011-06-30 19:14:33 -07001385 enum sci_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001386
Dan Williamsffe191c2011-06-29 13:09:25 -07001387 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001388 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001389 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001390 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001391 case SCI_PORT_SUB_OPERATIONAL:
Dan Williamsffe191c2011-06-29 13:09:25 -07001392 iport->started_request_count++;
Dan Williams68138202011-05-12 07:16:06 -07001393 return SCI_SUCCESS;
1394 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001395 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1396 __func__, port_state_name(state));
Dan Williams68138202011-05-12 07:16:06 -07001397 return SCI_FAILURE_INVALID_STATE;
1398 }
1399}
1400
Dan Williams89a73012011-06-30 19:14:33 -07001401enum sci_status sci_port_complete_io(struct isci_port *iport,
1402 struct isci_remote_device *idev,
1403 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001404{
Dan Williams89a73012011-06-30 19:14:33 -07001405 enum sci_port_states state;
Dan Williams68138202011-05-12 07:16:06 -07001406
Dan Williamsffe191c2011-06-29 13:09:25 -07001407 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001408 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001409 case SCI_PORT_STOPPED:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001410 dev_warn(sciport_to_dev(iport), "%s: in wrong state: %s\n",
1411 __func__, port_state_name(state));
Dan Williams68138202011-05-12 07:16:06 -07001412 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001413 case SCI_PORT_STOPPING:
Dan Williams89a73012011-06-30 19:14:33 -07001414 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001415
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_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001419 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001420 case SCI_PORT_READY:
1421 case SCI_PORT_RESETTING:
1422 case SCI_PORT_FAILED:
1423 case SCI_PORT_SUB_WAITING:
1424 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001425 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001426 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001427 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001428 sci_port_decrement_request_count(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001429 if (iport->started_request_count == 0) {
1430 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001431 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001432 }
1433 break;
1434 }
1435 return SCI_SUCCESS;
1436}
Dan Williamse2f8db52011-05-10 02:28:46 -07001437
Dan Williams89a73012011-06-30 19:14:33 -07001438static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001439{
1440 u32 pts_control_value;
1441
Dan Williams89a73012011-06-30 19:14:33 -07001442 /* enable the port task scheduler in a suspended state */
Dan Williamsffe191c2011-06-29 13:09:25 -07001443 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001444 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -07001445 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001446}
1447
Dan Williams89a73012011-06-30 19:14:33 -07001448static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001449{
1450 u32 pts_control_value;
1451
Dan Williamsffe191c2011-06-29 13:09:25 -07001452 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001453 pts_control_value &=
1454 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
Dan Williamsffe191c2011-06-29 13:09:25 -07001455 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001456}
1457
Dan Williams89a73012011-06-30 19:14:33 -07001458static void sci_port_post_dummy_remote_node(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001459{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001460 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001461 u8 phys_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -07001462 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001463 u16 rni = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -07001464 u32 command;
1465
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001466 rnc = &ihost->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -07001467 rnc->ssp.is_valid = true;
1468
1469 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1470 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1471
Dan Williams89a73012011-06-30 19:14:33 -07001472 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001473
1474 /* ensure hardware has seen the post rnc command and give it
1475 * ample time to act before sending the suspend
1476 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001477 readl(&ihost->smu_registers->interrupt_status); /* flush */
Dan Williamse2f8db52011-05-10 02:28:46 -07001478 udelay(10);
1479
1480 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1481 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1482
Dan Williams89a73012011-06-30 19:14:33 -07001483 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001484}
1485
Dan Williams89a73012011-06-30 19:14:33 -07001486static void sci_port_stopped_state_enter(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
Dan Williamsffe191c2011-06-29 13:09:25 -07001490 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001491 /*
1492 * If we enter this state becasuse of a request to stop
1493 * the port then we want to disable the hardwares port
1494 * task scheduler. */
Dan Williams89a73012011-06-30 19:14:33 -07001495 sci_port_disable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001496 }
1497}
1498
Dan Williams89a73012011-06-30 19:14:33 -07001499static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001500{
Dan Williamsffe191c2011-06-29 13:09:25 -07001501 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001502
1503 /* Enable and suspend the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001504 sci_port_enable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001505}
1506
Dan Williams89a73012011-06-30 19:14:33 -07001507static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001508{
Dan Williamsffe191c2011-06-29 13:09:25 -07001509 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001510 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001511 u32 prev_state;
1512
Dan Williamsffe191c2011-06-29 13:09:25 -07001513 prev_state = iport->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001514 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001515 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1516 else
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001517 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1518 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -07001519
1520 /* Post and suspend the dummy remote node context for this port. */
Dan Williams89a73012011-06-30 19:14:33 -07001521 sci_port_post_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001522
1523 /* Start the ready substate machine */
Dan Williamsffe191c2011-06-29 13:09:25 -07001524 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001525 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001526}
1527
Dan Williams89a73012011-06-30 19:14:33 -07001528static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001529{
Dan Williamsffe191c2011-06-29 13:09:25 -07001530 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001531
Dan Williamsffe191c2011-06-29 13:09:25 -07001532 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001533}
1534
Dan Williams89a73012011-06-30 19:14:33 -07001535static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001536{
Dan Williamsffe191c2011-06-29 13:09:25 -07001537 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001538
Dan Williamsffe191c2011-06-29 13:09:25 -07001539 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001540
Dan Williams89a73012011-06-30 19:14:33 -07001541 sci_port_destroy_dummy_resources(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001542}
1543
Dan Williams89a73012011-06-30 19:14:33 -07001544static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001545{
Dan Williamsffe191c2011-06-29 13:09:25 -07001546 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001547
Dan Williamse2f8db52011-05-10 02:28:46 -07001548 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1549}
1550
Jeff Skirvin6f488442012-03-08 22:41:48 -08001551void sci_port_set_hang_detection_timeout(struct isci_port *iport, u32 timeout)
1552{
1553 int phy_index;
1554 u32 phy_mask = iport->active_phy_mask;
1555
1556 if (timeout)
1557 ++iport->hang_detect_users;
1558 else if (iport->hang_detect_users > 1)
1559 --iport->hang_detect_users;
1560 else
1561 iport->hang_detect_users = 0;
1562
1563 if (timeout || (iport->hang_detect_users == 0)) {
1564 for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
1565 if ((phy_mask >> phy_index) & 1) {
1566 writel(timeout,
1567 &iport->phy_table[phy_index]
1568 ->link_layer_registers
1569 ->link_layer_hang_detection_timeout);
1570 }
1571 }
1572 }
1573}
Dan Williamse2f8db52011-05-10 02:28:46 -07001574/* --------------------------------------------------------------------------- */
1575
Dan Williams89a73012011-06-30 19:14:33 -07001576static const struct sci_base_state sci_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001577 [SCI_PORT_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001578 .enter_state = sci_port_stopped_state_enter,
1579 .exit_state = sci_port_stopped_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001580 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001581 [SCI_PORT_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001582 .exit_state = sci_port_stopping_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001583 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001584 [SCI_PORT_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001585 .enter_state = sci_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001586 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001587 [SCI_PORT_SUB_WAITING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001588 .enter_state = sci_port_ready_substate_waiting_enter,
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001589 .exit_state = scic_sds_port_ready_substate_waiting_exit,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001590 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001591 [SCI_PORT_SUB_OPERATIONAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001592 .enter_state = sci_port_ready_substate_operational_enter,
1593 .exit_state = sci_port_ready_substate_operational_exit
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001594 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001595 [SCI_PORT_SUB_CONFIGURING] = {
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001596 .enter_state = sci_port_ready_substate_configuring_enter
Dan Williamse2f8db52011-05-10 02:28:46 -07001597 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001598 [SCI_PORT_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001599 .exit_state = sci_port_resetting_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001600 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001601 [SCI_PORT_FAILED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001602 .enter_state = sci_port_failed_state_enter,
Dan Williamse2f8db52011-05-10 02:28:46 -07001603 }
1604};
1605
Dan Williams89a73012011-06-30 19:14:33 -07001606void sci_port_construct(struct isci_port *iport, u8 index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001607 struct isci_host *ihost)
Dan Williamse2f8db52011-05-10 02:28:46 -07001608{
Dan Williams89a73012011-06-30 19:14:33 -07001609 sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001610
Dan Williamsffe191c2011-06-29 13:09:25 -07001611 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1612 iport->physical_port_index = index;
1613 iport->active_phy_mask = 0;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001614 iport->enabled_phy_mask = 0;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001615 iport->last_active_phy = 0;
1616 iport->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001617
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001618 iport->owning_controller = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001619
Dan Williamsffe191c2011-06-29 13:09:25 -07001620 iport->started_request_count = 0;
1621 iport->assigned_device_count = 0;
Jeff Skirvin6f488442012-03-08 22:41:48 -08001622 iport->hang_detect_users = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -07001623
Dan Williamsffe191c2011-06-29 13:09:25 -07001624 iport->reserved_rni = SCU_DUMMY_INDEX;
1625 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -07001626
Dan Williamsffe191c2011-06-29 13:09:25 -07001627 sci_init_timer(&iport->timer, port_timeout);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001628
Dan Williamsffe191c2011-06-29 13:09:25 -07001629 iport->port_task_scheduler_registers = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001630
1631 for (index = 0; index < SCI_MAX_PHYS; index++)
Dan Williamsffe191c2011-06-29 13:09:25 -07001632 iport->phy_table[index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001633}
1634
Dan Williams89a73012011-06-30 19:14:33 -07001635void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -07001636{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001637 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001638
1639 /* notify the user. */
Dan Williamsffe191c2011-06-29 13:09:25 -07001640 isci_port_bc_change_received(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -07001641}
1642
Dan Williams92776992011-11-30 11:57:34 -08001643static void wait_port_reset(struct isci_host *ihost, struct isci_port *iport)
1644{
1645 wait_event(ihost->eventq, !test_bit(IPORT_RESET_PENDING, &iport->state));
1646}
1647
Dan Williams4393aa42011-03-31 13:10:44 -07001648int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1649 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001650{
Dan Williams4393aa42011-03-31 13:10:44 -07001651 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001652 enum sci_status status;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001653 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001654
Dan Williams4393aa42011-03-31 13:10:44 -07001655 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1656 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001657
Dan Williams4393aa42011-03-31 13:10:44 -07001658 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams92776992011-11-30 11:57:34 -08001659 set_bit(IPORT_RESET_PENDING, &iport->state);
Dan Williams6f231dd2011-07-02 22:56:22 -07001660
1661 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams89a73012011-06-30 19:14:33 -07001662 status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001663
Dan Williams4393aa42011-03-31 13:10:44 -07001664 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001665
1666 if (status == SCI_SUCCESS) {
Dan Williams92776992011-11-30 11:57:34 -08001667 wait_port_reset(ihost, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001668
Dan Williams4393aa42011-03-31 13:10:44 -07001669 dev_dbg(&ihost->pdev->dev,
1670 "%s: iport = %p; hard reset completion\n",
1671 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001672
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001673 if (iport->hard_reset_status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001674 ret = TMF_RESP_FUNC_FAILED;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001675
1676 dev_err(&ihost->pdev->dev,
1677 "%s: iport = %p; hard reset failed (0x%x)\n",
1678 __func__, iport, iport->hard_reset_status);
1679 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001680 } else {
Dan Williams92776992011-11-30 11:57:34 -08001681 clear_bit(IPORT_RESET_PENDING, &iport->state);
1682 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001683 ret = TMF_RESP_FUNC_FAILED;
1684
Dan Williams4393aa42011-03-31 13:10:44 -07001685 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001686 "%s: iport = %p; sci_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001687 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001688 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001689
1690 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001691 return ret;
1692}
Dave Jiang09d7da12011-03-26 16:11:51 -07001693
Dan Williams687833a2011-11-17 18:01:38 -08001694int isci_ata_check_ready(struct domain_device *dev)
1695{
1696 struct isci_port *iport = dev->port->lldd_port;
1697 struct isci_host *ihost = dev_to_ihost(dev);
1698 struct isci_remote_device *idev;
1699 unsigned long flags;
1700 int rc = 0;
1701
1702 spin_lock_irqsave(&ihost->scic_lock, flags);
1703 idev = isci_lookup_device(dev);
1704 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1705
1706 if (!idev)
1707 goto out;
1708
1709 if (test_bit(IPORT_RESET_PENDING, &iport->state))
1710 goto out;
1711
1712 rc = !!iport->active_phy_mask;
1713 out:
1714 isci_put_device(idev);
1715
1716 return rc;
1717}
1718
Dan Williamse2f8db52011-05-10 02:28:46 -07001719void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001720{
Dan Williamsc132f692012-01-03 23:26:08 -08001721 struct isci_host *ihost = phy->ha->lldd_ha;
1722 struct isci_port *iport = phy->port->lldd_port;
1723 unsigned long flags;
1724 int i;
1725
1726 /* we got a port notification on a port that was subsequently
1727 * torn down and libsas is just now catching up
1728 */
1729 if (!iport)
1730 return;
1731
1732 spin_lock_irqsave(&ihost->scic_lock, flags);
1733 for (i = 0; i < SCI_MAX_PHYS; i++) {
1734 if (iport->active_phy_mask & 1 << i)
1735 break;
1736 }
1737 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1738
1739 if (i >= SCI_MAX_PHYS)
1740 dev_dbg(&ihost->pdev->dev, "%s: port: %ld\n",
1741 __func__, (long) (iport - &ihost->ports[0]));
Dan Williamse2f8db52011-05-10 02:28:46 -07001742}
1743
Dan Williamse2f8db52011-05-10 02:28:46 -07001744void isci_port_formed(struct asd_sas_phy *phy)
1745{
Dan Williamsc132f692012-01-03 23:26:08 -08001746 struct isci_host *ihost = phy->ha->lldd_ha;
1747 struct isci_phy *iphy = to_iphy(phy);
1748 struct asd_sas_port *port = phy->port;
Jeff Skirvin6f488442012-03-08 22:41:48 -08001749 struct isci_port *iport = NULL;
Dan Williamsc132f692012-01-03 23:26:08 -08001750 unsigned long flags;
1751 int i;
1752
1753 /* initial ports are formed as the driver is still initializing,
1754 * wait for that process to complete
1755 */
1756 wait_for_start(ihost);
1757
1758 spin_lock_irqsave(&ihost->scic_lock, flags);
1759 for (i = 0; i < SCI_MAX_PORTS; i++) {
1760 iport = &ihost->ports[i];
1761 if (iport->active_phy_mask & 1 << iphy->phy_index)
1762 break;
1763 }
1764 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1765
1766 if (i >= SCI_MAX_PORTS)
1767 iport = NULL;
1768
1769 port->lldd_port = iport;
Dave Jiang09d7da12011-03-26 16:11:51 -07001770}