blob: bfeb87905aaf1bc280e0c63b763a87761e0f4211 [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 Williamse5313812011-05-07 10:11:43 -070063static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
64{
65 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -070066
Dan Williamse5313812011-05-07 10:11:43 -070067 dev_dbg(&iport->isci_host->pdev->dev,
68 "%s: iport = %p, state = 0x%x\n",
69 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -070070
Dan Williamse5313812011-05-07 10:11:43 -070071 /* XXX pointless lock */
72 spin_lock_irqsave(&iport->state_lock, flags);
73 iport->status = status;
74 spin_unlock_irqrestore(&iport->state_lock, flags);
75}
Dan Williams6f231dd2011-07-02 22:56:22 -070076
Dan Williams89a73012011-06-30 19:14:33 -070077static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
Dan Williams6f231dd2011-07-02 22:56:22 -070078{
Dan Williamse2f8db52011-05-10 02:28:46 -070079 u8 index;
80
Dan Williams89a73012011-06-30 19:14:33 -070081 proto->all = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -070082 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -070083 struct isci_phy *iphy = iport->phy_table[index];
84
85 if (!iphy)
86 continue;
87 sci_phy_get_protocols(iphy, proto);
Dan Williamse2f8db52011-05-10 02:28:46 -070088 }
Dan Williams6f231dd2011-07-02 22:56:22 -070089}
90
Dan Williams89a73012011-06-30 19:14:33 -070091static u32 sci_port_get_phys(struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -070092{
Dan Williamse2f8db52011-05-10 02:28:46 -070093 u32 index;
94 u32 mask;
95
96 mask = 0;
Dan Williams89a73012011-06-30 19:14:33 -070097 for (index = 0; index < SCI_MAX_PHYS; index++)
98 if (iport->phy_table[index])
Dan Williamse2f8db52011-05-10 02:28:46 -070099 mask |= (1 << index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700100
101 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700102}
103
Dan Williamse2f8db52011-05-10 02:28:46 -0700104/**
Dan Williams89a73012011-06-30 19:14:33 -0700105 * sci_port_get_properties() - This method simply returns the properties
Dan Williamse2f8db52011-05-10 02:28:46 -0700106 * regarding the port, such as: physical index, protocols, sas address, etc.
107 * @port: this parameter specifies the port for which to retrieve the physical
108 * index.
109 * @properties: This parameter specifies the properties structure into which to
110 * copy the requested information.
111 *
112 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
113 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
114 * value is returned if the specified port is not valid. When this value is
115 * returned, no data is copied to the properties output parameter.
116 */
Dan Williams89a73012011-06-30 19:14:33 -0700117static enum sci_status sci_port_get_properties(struct isci_port *iport,
118 struct sci_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700119{
Dan Williamsffe191c2011-06-29 13:09:25 -0700120 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamse2f8db52011-05-10 02:28:46 -0700121 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700122
Dan Williams89a73012011-06-30 19:14:33 -0700123 prop->index = iport->logical_port_index;
124 prop->phy_mask = sci_port_get_phys(iport);
125 sci_port_get_sas_address(iport, &prop->local.sas_address);
126 sci_port_get_protocols(iport, &prop->local.protocols);
127 sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700128
Dan Williamse2f8db52011-05-10 02:28:46 -0700129 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700130}
131
Dan Williams89a73012011-06-30 19:14:33 -0700132static void sci_port_bcn_enable(struct isci_port *iport)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700133{
Dan Williams85280952011-06-28 15:05:53 -0700134 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700135 u32 val;
136 int i;
137
Dan Williamsffe191c2011-06-29 13:09:25 -0700138 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
139 iphy = iport->phy_table[i];
Dan Williams85280952011-06-28 15:05:53 -0700140 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700141 continue;
Dan Williams85280952011-06-28 15:05:53 -0700142 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700143 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700144 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700145 }
146}
147
Dan Williams89a73012011-06-30 19:14:33 -0700148/* called under sci_lock to stabilize phy:port associations */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700149void isci_port_bcn_enable(struct isci_host *ihost, struct isci_port *iport)
150{
151 int i;
152
153 clear_bit(IPORT_BCN_BLOCKED, &iport->flags);
154 wake_up(&ihost->eventq);
155
156 if (!test_and_clear_bit(IPORT_BCN_PENDING, &iport->flags))
157 return;
158
Dan Williamsffe191c2011-06-29 13:09:25 -0700159 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
160 struct isci_phy *iphy = iport->phy_table[i];
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700161
Dan Williams85280952011-06-28 15:05:53 -0700162 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700163 continue;
164
165 ihost->sas_ha.notify_port_event(&iphy->sas_phy,
166 PORTE_BROADCAST_RCVD);
167 break;
168 }
169}
170
Dan Williamsffe191c2011-06-29 13:09:25 -0700171static void isci_port_bc_change_received(struct isci_host *ihost,
172 struct isci_port *iport,
173 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700174{
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700175 if (iport && test_bit(IPORT_BCN_BLOCKED, &iport->flags)) {
176 dev_dbg(&ihost->pdev->dev,
177 "%s: disabled BCN; isci_phy = %p, sas_phy = %p\n",
178 __func__, iphy, &iphy->sas_phy);
179 set_bit(IPORT_BCN_PENDING, &iport->flags);
180 atomic_inc(&iport->event);
181 wake_up(&ihost->eventq);
182 } else {
183 dev_dbg(&ihost->pdev->dev,
184 "%s: isci_phy = %p, sas_phy = %p\n",
185 __func__, iphy, &iphy->sas_phy);
186
187 ihost->sas_ha.notify_port_event(&iphy->sas_phy,
188 PORTE_BROADCAST_RCVD);
189 }
Dan Williams89a73012011-06-30 19:14:33 -0700190 sci_port_bcn_enable(iport);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700191}
192
Dan Williamse2f8db52011-05-10 02:28:46 -0700193static void isci_port_link_up(struct isci_host *isci_host,
Dan Williamsffe191c2011-06-29 13:09:25 -0700194 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700195 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700196{
197 unsigned long flags;
Dan Williams89a73012011-06-30 19:14:33 -0700198 struct sci_port_properties properties;
Dan Williams6f231dd2011-07-02 22:56:22 -0700199 unsigned long success = true;
200
Dan Williams85280952011-06-28 15:05:53 -0700201 BUG_ON(iphy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700202
Dan Williamsffe191c2011-06-29 13:09:25 -0700203 iphy->isci_port = iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700204
205 dev_dbg(&isci_host->pdev->dev,
206 "%s: isci_port = %p\n",
Dan Williamsffe191c2011-06-29 13:09:25 -0700207 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700208
Dan Williams85280952011-06-28 15:05:53 -0700209 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700210
Dan Williams85280952011-06-28 15:05:53 -0700211 isci_port_change_state(iphy->isci_port, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
Dan Williams89a73012011-06-30 19:14:33 -0700213 sci_port_get_properties(iport, &properties);
Dan Williams6f231dd2011-07-02 22:56:22 -0700214
Dan Williams85280952011-06-28 15:05:53 -0700215 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800216 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700217
Dan Williams85280952011-06-28 15:05:53 -0700218 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
219 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700220
221 /*
222 * For direct-attached SATA devices, the SCI core will
223 * automagically assign a SAS address to the end device
224 * for the purpose of creating a port. This SAS address
225 * will not be the same as assigned to the PHY and needs
Dan Williams89a73012011-06-30 19:14:33 -0700226 * to be obtained from struct sci_port_properties properties.
Dan Williams6f231dd2011-07-02 22:56:22 -0700227 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800228 attached_sas_address = properties.remote.sas_address.high;
229 attached_sas_address <<= 32;
230 attached_sas_address |= properties.remote.sas_address.low;
231 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700232
Dan Williams85280952011-06-28 15:05:53 -0700233 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800234 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams85280952011-06-28 15:05:53 -0700235 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
236 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
237 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700238
239 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700240 memcpy(iphy->sas_phy.attached_sas_addr,
241 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700242 } else {
243 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
244 success = false;
245 }
246
Dan Williams85280952011-06-28 15:05:53 -0700247 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800248
Dan Williams85280952011-06-28 15:05:53 -0700249 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700250
251 /* Notify libsas that we have an address frame, if indeed
252 * we've found an SSP, SMP, or STP target */
253 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700254 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700255 PORTE_BYTES_DMAED);
256}
257
258
259/**
260 * isci_port_link_down() - This function is called by the sci core when a link
261 * becomes inactive.
262 * @isci_host: This parameter specifies the isci host object.
263 * @phy: This parameter specifies the isci phy with the active link.
264 * @port: This parameter specifies the isci port with the active link.
265 *
266 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700267static void isci_port_link_down(struct isci_host *isci_host,
268 struct isci_phy *isci_phy,
269 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700270{
271 struct isci_remote_device *isci_device;
272
273 dev_dbg(&isci_host->pdev->dev,
274 "%s: isci_port = %p\n", __func__, isci_port);
275
276 if (isci_port) {
277
278 /* check to see if this is the last phy on this port. */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700279 if (isci_phy->sas_phy.port &&
280 isci_phy->sas_phy.port->num_phys == 1) {
281 atomic_inc(&isci_port->event);
282 isci_port_bcn_enable(isci_host, isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -0700283
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700284 /* change the state for all devices on this port. The
285 * next task sent to this device will be returned as
286 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
287 * remove the target
Dan Williams6f231dd2011-07-02 22:56:22 -0700288 */
289 list_for_each_entry(isci_device,
290 &isci_port->remote_dev_list,
291 node) {
292 dev_dbg(&isci_host->pdev->dev,
293 "%s: isci_device = %p\n",
294 __func__, isci_device);
Dan Williams209fae12011-06-13 17:39:44 -0700295 set_bit(IDEV_GONE, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700296 }
Jeff Skirvincdd05f02011-09-28 18:47:56 -0700297 isci_port_change_state(isci_port, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -0700298 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700299 }
300
301 /* Notify libsas of the borken link, this will trigger calls to our
302 * isci_port_deformed and isci_dev_gone functions.
303 */
304 sas_phy_disconnected(&isci_phy->sas_phy);
305 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
306 PHYE_LOSS_OF_SIGNAL);
307
308 isci_phy->isci_port = NULL;
309
310 dev_dbg(&isci_host->pdev->dev,
311 "%s: isci_port = %p - Done\n", __func__, isci_port);
312}
313
314
315/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700316 * isci_port_ready() - This function is called by the sci core when a link
317 * becomes ready.
318 * @isci_host: This parameter specifies the isci host object.
319 * @port: This parameter specifies the sci port with the active link.
320 *
321 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700322static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700323{
324 dev_dbg(&isci_host->pdev->dev,
325 "%s: isci_port = %p\n", __func__, isci_port);
326
327 complete_all(&isci_port->start_complete);
328 isci_port_change_state(isci_port, isci_ready);
329 return;
330}
331
332/**
333 * isci_port_not_ready() - This function is called by the sci core when a link
334 * is not ready. All remote devices on this link will be removed if they are
335 * in the stopping state.
336 * @isci_host: This parameter specifies the isci host object.
337 * @port: This parameter specifies the sci port with the active link.
338 *
339 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700340static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700341{
342 dev_dbg(&isci_host->pdev->dev,
343 "%s: isci_port = %p\n", __func__, isci_port);
344}
345
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700346static void isci_port_stop_complete(struct isci_host *ihost,
Dan Williamsffe191c2011-06-29 13:09:25 -0700347 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700348 enum sci_status completion_status)
349{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700350 dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700351}
352
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700353
354static bool is_port_ready_state(enum sci_port_states state)
355{
356 switch (state) {
357 case SCI_PORT_READY:
358 case SCI_PORT_SUB_WAITING:
359 case SCI_PORT_SUB_OPERATIONAL:
360 case SCI_PORT_SUB_CONFIGURING:
361 return true;
362 default:
363 return false;
364 }
365}
366
367/* flag dummy rnc hanling when exiting a ready state */
368static void port_state_machine_change(struct isci_port *iport,
369 enum sci_port_states state)
370{
371 struct sci_base_state_machine *sm = &iport->sm;
372 enum sci_port_states old_state = sm->current_state_id;
373
374 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
375 iport->ready_exit = true;
376
377 sci_change_state(sm, state);
378 iport->ready_exit = false;
379}
380
Dan Williams6f231dd2011-07-02 22:56:22 -0700381/**
382 * isci_port_hard_reset_complete() - This function is called by the sci core
383 * when the hard reset complete notification has been received.
384 * @port: This parameter specifies the sci port with the active link.
385 * @completion_status: This parameter specifies the core status for the reset
386 * process.
387 *
388 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700389static void isci_port_hard_reset_complete(struct isci_port *isci_port,
390 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700391{
392 dev_dbg(&isci_port->isci_host->pdev->dev,
393 "%s: isci_port = %p, completion_status=%x\n",
394 __func__, isci_port, completion_status);
395
396 /* Save the status of the hard reset from the port. */
397 isci_port->hard_reset_status = completion_status;
398
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700399 if (completion_status != SCI_SUCCESS) {
400
401 /* The reset failed. The port state is now SCI_PORT_FAILED. */
402 if (isci_port->active_phy_mask == 0) {
403
404 /* Generate the link down now to the host, since it
405 * was intercepted by the hard reset state machine when
406 * it really happened.
407 */
408 isci_port_link_down(isci_port->isci_host,
409 &isci_port->isci_host->phys[
410 isci_port->last_active_phy],
411 isci_port);
412 }
413 /* Advance the port state so that link state changes will be
414 * noticed.
415 */
416 port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
417
418 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700419 complete_all(&isci_port->hard_reset_complete);
420}
Dan Williams4393aa42011-03-31 13:10:44 -0700421
Dan Williamse2f8db52011-05-10 02:28:46 -0700422/* This method will return a true value if the specified phy can be assigned to
423 * this port The following is a list of phys for each port that are allowed: -
424 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
425 * doesn't preclude all configurations. It merely ensures that a phy is part
426 * of the allowable set of phy identifiers for that port. For example, one
427 * could assign phy 3 to port 0 and no other phys. Please refer to
Dan Williams89a73012011-06-30 19:14:33 -0700428 * sci_port_is_phy_mask_valid() for information regarding whether the
Dan Williamse2f8db52011-05-10 02:28:46 -0700429 * phy_mask for a port can be supported. bool true if this is a valid phy
430 * assignment for the port false if this is not a valid phy assignment for the
431 * port
432 */
Dan Williams89a73012011-06-30 19:14:33 -0700433bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700434{
Dan Williams89a73012011-06-30 19:14:33 -0700435 struct isci_host *ihost = iport->owning_controller;
436 struct sci_user_parameters *user = &ihost->user_parameters;
437
Dan Williamse2f8db52011-05-10 02:28:46 -0700438 /* Initialize to invalid value. */
439 u32 existing_phy_index = SCI_MAX_PHYS;
440 u32 index;
441
Dan Williams89a73012011-06-30 19:14:33 -0700442 if ((iport->physical_port_index == 1) && (phy_index != 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700443 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700444
Dan Williams89a73012011-06-30 19:14:33 -0700445 if (iport->physical_port_index == 3 && phy_index != 3)
Dan Williamse2f8db52011-05-10 02:28:46 -0700446 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700447
Dan Williams89a73012011-06-30 19:14:33 -0700448 if (iport->physical_port_index == 2 &&
449 (phy_index == 0 || phy_index == 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700450 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700451
Dan Williams89a73012011-06-30 19:14:33 -0700452 for (index = 0; index < SCI_MAX_PHYS; index++)
453 if (iport->phy_table[index] && index != phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700454 existing_phy_index = index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700455
Dan Williams89a73012011-06-30 19:14:33 -0700456 /* Ensure that all of the phys in the port are capable of
457 * operating at the same maximum link rate.
458 */
459 if (existing_phy_index < SCI_MAX_PHYS &&
460 user->phys[phy_index].max_speed_generation !=
461 user->phys[existing_phy_index].max_speed_generation)
Dan Williamse2f8db52011-05-10 02:28:46 -0700462 return false;
463
464 return true;
465}
466
467/**
468 *
469 * @sci_port: This is the port object for which to determine if the phy mask
470 * can be supported.
471 *
472 * This method will return a true value if the port's phy mask can be supported
473 * by the SCU. The following is a list of valid PHY mask configurations for
474 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
475 * - Port 3 - [3] This method returns a boolean indication specifying if the
476 * phy mask can be supported. true if this is a valid phy assignment for the
477 * port false if this is not a valid phy assignment for the port
478 */
Dan Williams89a73012011-06-30 19:14:33 -0700479static bool sci_port_is_phy_mask_valid(
Dan Williamsffe191c2011-06-29 13:09:25 -0700480 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700481 u32 phy_mask)
482{
Dan Williamsffe191c2011-06-29 13:09:25 -0700483 if (iport->physical_port_index == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700484 if (((phy_mask & 0x0F) == 0x0F)
485 || ((phy_mask & 0x03) == 0x03)
486 || ((phy_mask & 0x01) == 0x01)
487 || (phy_mask == 0))
488 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700489 } else if (iport->physical_port_index == 1) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700490 if (((phy_mask & 0x02) == 0x02)
491 || (phy_mask == 0))
492 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700493 } else if (iport->physical_port_index == 2) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700494 if (((phy_mask & 0x0C) == 0x0C)
495 || ((phy_mask & 0x04) == 0x04)
496 || (phy_mask == 0))
497 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700498 } else if (iport->physical_port_index == 3) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700499 if (((phy_mask & 0x08) == 0x08)
500 || (phy_mask == 0))
501 return true;
502 }
503
504 return false;
505}
506
Dan Williams85280952011-06-28 15:05:53 -0700507/*
Dan Williamse2f8db52011-05-10 02:28:46 -0700508 * This method retrieves a currently active (i.e. connected) phy contained in
509 * the port. Currently, the lowest order phy that is connected is returned.
510 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
511 * returned if there are no currently active (i.e. connected to a remote end
Dan Williams89a73012011-06-30 19:14:33 -0700512 * point) phys contained in the port. All other values specify a struct sci_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700513 * object that is active in the port.
514 */
Dan Williams89a73012011-06-30 19:14:33 -0700515static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
Dan Williams85280952011-06-28 15:05:53 -0700516{
Dan Williamse2f8db52011-05-10 02:28:46 -0700517 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700518 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700519
520 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700521 /* Ensure that the phy is both part of the port and currently
522 * connected to the remote end-point.
523 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700524 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700525 if (iphy && sci_port_active_phy(iport, iphy))
Dan Williams85280952011-06-28 15:05:53 -0700526 return iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700527 }
528
529 return NULL;
530}
531
Dan Williams89a73012011-06-30 19:14:33 -0700532static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700533{
Dan Williams85280952011-06-28 15:05:53 -0700534 /* Check to see if we can add this phy to a port
Dan Williamse2f8db52011-05-10 02:28:46 -0700535 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700536 * not already have a phy assinged to the phy index.
537 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700538 if (!iport->phy_table[iphy->phy_index] &&
Dan Williams85280952011-06-28 15:05:53 -0700539 !phy_get_non_dummy_port(iphy) &&
Dan Williams89a73012011-06-30 19:14:33 -0700540 sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams85280952011-06-28 15:05:53 -0700541 /* Phy is being added in the stopped state so we are in MPC mode
542 * make logical port index = physical port index
543 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700544 iport->logical_port_index = iport->physical_port_index;
545 iport->phy_table[iphy->phy_index] = iphy;
Dan Williams89a73012011-06-30 19:14:33 -0700546 sci_phy_set_port(iphy, iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700547
548 return SCI_SUCCESS;
549 }
550
551 return SCI_FAILURE;
552}
553
Dan Williams89a73012011-06-30 19:14:33 -0700554static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700555{
556 /* Make sure that this phy is part of this port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700557 if (iport->phy_table[iphy->phy_index] == iphy &&
558 phy_get_non_dummy_port(iphy) == iport) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700559 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700560
561 /* Yep it is assigned to this port so remove it */
Dan Williams89a73012011-06-30 19:14:33 -0700562 sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
Dan Williamsffe191c2011-06-29 13:09:25 -0700563 iport->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -0700564 return SCI_SUCCESS;
565 }
566
567 return SCI_FAILURE;
568}
569
Dan Williams89a73012011-06-30 19:14:33 -0700570void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700571{
572 u32 index;
573
Dan Williams89a73012011-06-30 19:14:33 -0700574 sas->high = 0;
575 sas->low = 0;
576 for (index = 0; index < SCI_MAX_PHYS; index++)
577 if (iport->phy_table[index])
578 sci_phy_get_sas_address(iport->phy_table[index], sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700579}
580
Dan Williams89a73012011-06-30 19:14:33 -0700581void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700582{
Dan Williams85280952011-06-28 15:05:53 -0700583 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700584
585 /*
586 * Ensure that the phy is both part of the port and currently
587 * connected to the remote end-point.
588 */
Dan Williams89a73012011-06-30 19:14:33 -0700589 iphy = sci_port_get_a_connected_phy(iport);
Dan Williams85280952011-06-28 15:05:53 -0700590 if (iphy) {
591 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams89a73012011-06-30 19:14:33 -0700592 sci_phy_get_attached_sas_address(iphy, sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700593 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700594 sci_phy_get_sas_address(iphy, sas);
595 sas->low += iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700596 }
597 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700598 sas->high = 0;
599 sas->low = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700600 }
601}
602
603/**
Dan Williams89a73012011-06-30 19:14:33 -0700604 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
Dan Williamse2f8db52011-05-10 02:28:46 -0700605 *
606 * @sci_port: logical port on which we need to create the remote node context
607 * @rni: remote node index for this remote node context.
608 *
609 * This routine will construct a dummy remote node context data structure
610 * This structure will be posted to the hardware to work around a scheduler
611 * error in the hardware.
612 */
Dan Williams89a73012011-06-30 19:14:33 -0700613static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
Dan Williamse2f8db52011-05-10 02:28:46 -0700614{
615 union scu_remote_node_context *rnc;
616
Dan Williamsffe191c2011-06-29 13:09:25 -0700617 rnc = &iport->owning_controller->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -0700618
619 memset(rnc, 0, sizeof(union scu_remote_node_context));
620
621 rnc->ssp.remote_sas_address_hi = 0;
622 rnc->ssp.remote_sas_address_lo = 0;
623
624 rnc->ssp.remote_node_index = rni;
625 rnc->ssp.remote_node_port_width = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700626 rnc->ssp.logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700627
628 rnc->ssp.nexus_loss_timer_enable = false;
629 rnc->ssp.check_bit = false;
630 rnc->ssp.is_valid = true;
631 rnc->ssp.is_remote_node_context = true;
632 rnc->ssp.function_number = 0;
633 rnc->ssp.arbitration_wait_time = 0;
634}
635
Dan Williamsdd047c82011-06-09 11:06:58 -0700636/*
637 * construct a dummy task context data structure. This
Dan Williamse2f8db52011-05-10 02:28:46 -0700638 * structure will be posted to the hardwre to work around a scheduler error
639 * in the hardware.
Dan Williamse2f8db52011-05-10 02:28:46 -0700640 */
Dan Williams89a73012011-06-30 19:14:33 -0700641static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
Dan Williamse2f8db52011-05-10 02:28:46 -0700642{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700643 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700644 struct scu_task_context *task_context;
645
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700646 task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700647 memset(task_context, 0, sizeof(struct scu_task_context));
648
Dan Williamse2f8db52011-05-10 02:28:46 -0700649 task_context->initiator_request = 1;
650 task_context->connection_rate = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700651 task_context->logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700652 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700653 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700654 task_context->valid = SCU_TASK_CONTEXT_VALID;
655 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamsffe191c2011-06-29 13:09:25 -0700656 task_context->remote_node_index = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -0700657 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db52011-05-10 02:28:46 -0700658 task_context->task_phase = 0x01;
659}
660
Dan Williams89a73012011-06-30 19:14:33 -0700661static void sci_port_destroy_dummy_resources(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700662{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700663 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700664
Dan Williamsffe191c2011-06-29 13:09:25 -0700665 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700666 isci_free_tag(ihost, iport->reserved_tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700667
Dan Williamsffe191c2011-06-29 13:09:25 -0700668 if (iport->reserved_rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700669 sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
Dan Williamsffe191c2011-06-29 13:09:25 -0700670 1, iport->reserved_rni);
Dan Williamse2f8db52011-05-10 02:28:46 -0700671
Dan Williamsffe191c2011-06-29 13:09:25 -0700672 iport->reserved_rni = SCU_DUMMY_INDEX;
673 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -0700674}
675
Dan Williams89a73012011-06-30 19:14:33 -0700676void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
Dan Williamse2f8db52011-05-10 02:28:46 -0700677{
678 u8 index;
679
680 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700681 if (iport->active_phy_mask & (1 << index))
Dan Williams89a73012011-06-30 19:14:33 -0700682 sci_phy_setup_transport(iport->phy_table[index], device_id);
Dan Williamse2f8db52011-05-10 02:28:46 -0700683 }
684}
685
Dan Williams89a73012011-06-30 19:14:33 -0700686static void sci_port_activate_phy(struct isci_port *iport, struct isci_phy *iphy,
687 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700688{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700689 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700690
Dan Williams85280952011-06-28 15:05:53 -0700691 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
Dan Williams89a73012011-06-30 19:14:33 -0700692 sci_phy_resume(iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700693
Dan Williamsffe191c2011-06-29 13:09:25 -0700694 iport->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700695
Dan Williams89a73012011-06-30 19:14:33 -0700696 sci_controller_clear_invalid_phy(ihost, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700697
698 if (do_notify_user == true)
Dan Williamsffe191c2011-06-29 13:09:25 -0700699 isci_port_link_up(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700700}
701
Dan Williams89a73012011-06-30 19:14:33 -0700702void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
703 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700704{
Dan Williams34a99152011-07-01 02:25:15 -0700705 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700706
Dan Williamsffe191c2011-06-29 13:09:25 -0700707 iport->active_phy_mask &= ~(1 << iphy->phy_index);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700708 if (!iport->active_phy_mask)
709 iport->last_active_phy = iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700710
Dan Williams85280952011-06-28 15:05:53 -0700711 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db52011-05-10 02:28:46 -0700712
713 /* Re-assign the phy back to the LP as if it were a narrow port */
Dan Williams85280952011-06-28 15:05:53 -0700714 writel(iphy->phy_index,
Dan Williamsffe191c2011-06-29 13:09:25 -0700715 &iport->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700716
717 if (do_notify_user == true)
718 isci_port_link_down(ihost, iphy, iport);
719}
720
Dan Williams89a73012011-06-30 19:14:33 -0700721static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700722{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700723 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700724
725 /*
726 * Check to see if we have alreay reported this link as bad and if
727 * not go ahead and tell the SCI_USER that we have discovered an
728 * invalid link.
729 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700730 if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700731 ihost->invalid_phy_mask |= 1 << iphy->phy_index;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700732 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700733 }
734}
735
736/**
Dan Williams89a73012011-06-30 19:14:33 -0700737 * sci_port_general_link_up_handler - phy can be assigned to port?
738 * @sci_port: sci_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700739 * @sci_phy: This is the struct isci_phy object that has gone link up.
Dan Williamse2f8db52011-05-10 02:28:46 -0700740 * @do_notify_user: This parameter specifies whether to inform the user (via
Dan Williams89a73012011-06-30 19:14:33 -0700741 * sci_port_link_up()) as to the fact that a new phy as become ready.
Dan Williamse2f8db52011-05-10 02:28:46 -0700742 *
743 * Determine if this phy can be assigned to this
744 * port . If the phy is not a valid PHY for
745 * this port then the function will notify the user. A PHY can only be
746 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
747 * the same port. none
748 */
Dan Williams89a73012011-06-30 19:14:33 -0700749static void sci_port_general_link_up_handler(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700750 struct isci_phy *iphy,
Dan Williamse2f8db52011-05-10 02:28:46 -0700751 bool do_notify_user)
752{
753 struct sci_sas_address port_sas_address;
754 struct sci_sas_address phy_sas_address;
755
Dan Williams89a73012011-06-30 19:14:33 -0700756 sci_port_get_attached_sas_address(iport, &port_sas_address);
757 sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700758
759 /* If the SAS address of the new phy matches the SAS address of
760 * other phys in the port OR this is the first phy in the port,
761 * then activate the phy and allow it to be used for operations
762 * in this port.
763 */
764 if ((phy_sas_address.high == port_sas_address.high &&
765 phy_sas_address.low == port_sas_address.low) ||
Dan Williamsffe191c2011-06-29 13:09:25 -0700766 iport->active_phy_mask == 0) {
767 struct sci_base_state_machine *sm = &iport->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700768
Dan Williams89a73012011-06-30 19:14:33 -0700769 sci_port_activate_phy(iport, iphy, do_notify_user);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000770 if (sm->current_state_id == SCI_PORT_RESETTING)
Dan Williamsffe191c2011-06-29 13:09:25 -0700771 port_state_machine_change(iport, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700772 } else
Dan Williams89a73012011-06-30 19:14:33 -0700773 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700774}
775
776
777
778/**
779 * This method returns false if the port only has a single phy object assigned.
780 * If there are no phys or more than one phy then the method will return
781 * true.
782 * @sci_port: The port for which the wide port condition is to be checked.
783 *
784 * bool true Is returned if this is a wide ported port. false Is returned if
785 * this is a narrow port.
786 */
Dan Williams89a73012011-06-30 19:14:33 -0700787static bool sci_port_is_wide(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700788{
789 u32 index;
790 u32 phy_count = 0;
791
792 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700793 if (iport->phy_table[index] != NULL) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700794 phy_count++;
795 }
796 }
797
798 return phy_count != 1;
799}
800
801/**
802 * This method is called by the PHY object when the link is detected. if the
803 * port wants the PHY to continue on to the link up state then the port
804 * layer must return true. If the port object returns false the phy object
805 * must halt its attempt to go link up.
806 * @sci_port: The port associated with the phy object.
807 * @sci_phy: The phy object that is trying to go link up.
808 *
809 * true if the phy object can continue to the link up condition. true Is
810 * returned if this phy can continue to the ready state. false Is returned if
811 * can not continue on to the ready state. This notification is in place for
812 * wide ports and direct attached phys. Since there are no wide ported SATA
813 * devices this could become an invalid port configuration.
814 */
Dan Williams89a73012011-06-30 19:14:33 -0700815bool sci_port_link_detected(
Dan Williamsffe191c2011-06-29 13:09:25 -0700816 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700817 struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700818{
Dan Williamsffe191c2011-06-29 13:09:25 -0700819 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Dan Williams85280952011-06-28 15:05:53 -0700820 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
Dan Williams89a73012011-06-30 19:14:33 -0700821 sci_port_is_wide(iport)) {
822 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700823
824 return false;
825 }
826
827 return true;
828}
829
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000830static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700831{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000832 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsffe191c2011-06-29 13:09:25 -0700833 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700834 struct isci_host *ihost = iport->owning_controller;
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000835 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700836 u32 current_state;
837
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000838 spin_lock_irqsave(&ihost->scic_lock, flags);
839
840 if (tmr->cancel)
841 goto done;
842
Dan Williamsffe191c2011-06-29 13:09:25 -0700843 current_state = iport->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700844
Edmund Nadolskie3013702011-06-02 00:10:43 +0000845 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000846 /* if the port is still in the resetting state then the timeout
847 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700848 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700849 port_state_machine_change(iport, SCI_PORT_FAILED);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000850 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000851 /* if the port is stopped then the start request failed In this
852 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700853 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700854 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700855 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
856 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -0700857 iport);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000858 } else if (current_state == SCI_PORT_STOPPING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000859 /* if the port is still stopping then the stop has not completed */
Dan Williamsffe191c2011-06-29 13:09:25 -0700860 isci_port_stop_complete(iport->owning_controller,
861 iport,
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000862 SCI_FAILURE_TIMEOUT);
Dan Williamse2f8db52011-05-10 02:28:46 -0700863 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000864 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700865 * reporting a timeout this should not happen.
866 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700867 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700868 "%s: SCIC Port 0x%p is processing a timeout operation "
Dan Williamsffe191c2011-06-29 13:09:25 -0700869 "in state %d.\n", __func__, iport, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700870 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000871
872done:
873 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700874}
875
876/* --------------------------------------------------------------------------- */
877
878/**
879 * This function updates the hardwares VIIT entry for this port.
880 *
881 *
882 */
Dan Williams89a73012011-06-30 19:14:33 -0700883static void sci_port_update_viit_entry(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700884{
885 struct sci_sas_address sas_address;
886
Dan Williams89a73012011-06-30 19:14:33 -0700887 sci_port_get_sas_address(iport, &sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700888
889 writel(sas_address.high,
Dan Williamsffe191c2011-06-29 13:09:25 -0700890 &iport->viit_registers->initiator_sas_address_hi);
Dan Williamse2f8db52011-05-10 02:28:46 -0700891 writel(sas_address.low,
Dan Williamsffe191c2011-06-29 13:09:25 -0700892 &iport->viit_registers->initiator_sas_address_lo);
Dan Williamse2f8db52011-05-10 02:28:46 -0700893
894 /* This value get cleared just in case its not already cleared */
Dan Williamsffe191c2011-06-29 13:09:25 -0700895 writel(0, &iport->viit_registers->reserved);
Dan Williamse2f8db52011-05-10 02:28:46 -0700896
897 /* We are required to update the status register last */
898 writel(SCU_VIIT_ENTRY_ID_VIIT |
899 SCU_VIIT_IPPT_INITIATOR |
Dan Williamsffe191c2011-06-29 13:09:25 -0700900 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
Dan Williamse2f8db52011-05-10 02:28:46 -0700901 SCU_VIIT_STATUS_ALL_VALID,
Dan Williamsffe191c2011-06-29 13:09:25 -0700902 &iport->viit_registers->status);
Dan Williamse2f8db52011-05-10 02:28:46 -0700903}
904
Dan Williams89a73012011-06-30 19:14:33 -0700905enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700906{
907 u16 index;
Dan Williams85280952011-06-28 15:05:53 -0700908 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700909 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700910
911 /*
912 * Loop through all of the phys in this port and find the phy with the
913 * lowest maximum link rate. */
914 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700915 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700916 if (iphy && sci_port_active_phy(iport, iphy) &&
Dan Williams85280952011-06-28 15:05:53 -0700917 iphy->max_negotiated_speed < max_allowed_speed)
918 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db52011-05-10 02:28:46 -0700919 }
920
921 return max_allowed_speed;
922}
923
Dan Williams89a73012011-06-30 19:14:33 -0700924static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700925{
926 u32 pts_control_value;
927
Dan Williamsffe191c2011-06-29 13:09:25 -0700928 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700929 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700930 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700931}
932
933/**
Dan Williams89a73012011-06-30 19:14:33 -0700934 * sci_port_post_dummy_request() - post dummy/workaround request
Dan Williamse2f8db52011-05-10 02:28:46 -0700935 * @sci_port: port to post task
936 *
937 * Prevent the hardware scheduler from posting new requests to the front
938 * of the scheduler queue causing a starvation problem for currently
939 * ongoing requests.
940 *
941 */
Dan Williams89a73012011-06-30 19:14:33 -0700942static void sci_port_post_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700943{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700944 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700945 u16 tag = iport->reserved_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700946 struct scu_task_context *tc;
947 u32 command;
Dan Williamse2f8db52011-05-10 02:28:46 -0700948
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700949 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williams312e0c22011-06-28 13:47:09 -0700950 tc->abort = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700951
952 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700953 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700954 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700955
Dan Williams89a73012011-06-30 19:14:33 -0700956 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700957}
958
959/**
960 * This routine will abort the dummy request. This will alow the hardware to
961 * power down parts of the silicon to save power.
962 *
963 * @sci_port: The port on which the task must be aborted.
964 *
965 */
Dan Williams89a73012011-06-30 19:14:33 -0700966static void sci_port_abort_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700967{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700968 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700969 u16 tag = iport->reserved_tag;
Dan Williamse2f8db52011-05-10 02:28:46 -0700970 struct scu_task_context *tc;
971 u32 command;
972
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700973 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700974 tc->abort = 1;
975
976 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
Dan Williamsffe191c2011-06-29 13:09:25 -0700977 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700978 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700979
Dan Williams89a73012011-06-30 19:14:33 -0700980 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700981}
982
983/**
984 *
Dan Williamsffe191c2011-06-29 13:09:25 -0700985 * @sci_port: This is the struct isci_port object to resume.
Dan Williamse2f8db52011-05-10 02:28:46 -0700986 *
987 * This method will resume the port task scheduler for this port object. none
988 */
989static void
Dan Williams89a73012011-06-30 19:14:33 -0700990sci_port_resume_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700991{
992 u32 pts_control_value;
993
Dan Williamsffe191c2011-06-29 13:09:25 -0700994 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700995 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700996 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700997}
998
Dan Williams89a73012011-06-30 19:14:33 -0700999static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001000{
Dan Williamsffe191c2011-06-29 13:09:25 -07001001 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001002
Dan Williams89a73012011-06-30 19:14:33 -07001003 sci_port_suspend_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001004
Dan Williamsffe191c2011-06-29 13:09:25 -07001005 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
Dan Williamse2f8db52011-05-10 02:28:46 -07001006
Dan Williamsffe191c2011-06-29 13:09:25 -07001007 if (iport->active_phy_mask != 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001008 /* At least one of the phys on the port is ready */
Dan Williamsffe191c2011-06-29 13:09:25 -07001009 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001010 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001011 }
1012}
1013
Dan Williams89a73012011-06-30 19:14:33 -07001014static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001015{
1016 u32 index;
Dan Williamsffe191c2011-06-29 13:09:25 -07001017 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001018 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001019
Dan Williamse2f8db52011-05-10 02:28:46 -07001020 isci_port_ready(ihost, iport);
1021
1022 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001023 if (iport->phy_table[index]) {
1024 writel(iport->physical_port_index,
1025 &iport->port_pe_configuration_register[
1026 iport->phy_table[index]->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -07001027 }
1028 }
1029
Dan Williams89a73012011-06-30 19:14:33 -07001030 sci_port_update_viit_entry(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001031
Dan Williams89a73012011-06-30 19:14:33 -07001032 sci_port_resume_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001033
1034 /*
1035 * Post the dummy task for the port so the hardware can schedule
1036 * io correctly
1037 */
Dan Williams89a73012011-06-30 19:14:33 -07001038 sci_port_post_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001039}
1040
Dan Williams89a73012011-06-30 19:14:33 -07001041static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001042{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001043 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001044 u8 phys_index = iport->physical_port_index;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001045 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001046 u16 rni = iport->reserved_rni;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001047 u32 command;
1048
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001049 rnc = &ihost->remote_node_context_table[rni];
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001050
1051 rnc->ssp.is_valid = false;
1052
1053 /* ensure the preceding tc abort request has reached the
1054 * controller and give it ample time to act before posting the rnc
1055 * invalidate
1056 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001057 readl(&ihost->smu_registers->interrupt_status); /* flush */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001058 udelay(10);
1059
1060 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1061 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1062
Dan Williams89a73012011-06-30 19:14:33 -07001063 sci_controller_post_request(ihost, command);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001064}
1065
Dan Williamse2f8db52011-05-10 02:28:46 -07001066/**
1067 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001068 * @object: This is the object which is cast to a struct isci_port object.
Dan Williamse2f8db52011-05-10 02:28:46 -07001069 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001070 * This method will perform the actions required by the struct isci_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001071 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001072 * the port not ready and suspends the port task scheduler. none
1073 */
Dan Williams89a73012011-06-30 19:14:33 -07001074static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001075{
Dan Williamsffe191c2011-06-29 13:09:25 -07001076 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001077 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001078
1079 /*
1080 * Kill the dummy task for this port if it has not yet posted
1081 * the hardware will treat this as a NOP and just return abort
1082 * complete.
1083 */
Dan Williams89a73012011-06-30 19:14:33 -07001084 sci_port_abort_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001085
1086 isci_port_not_ready(ihost, iport);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001087
Dan Williamsffe191c2011-06-29 13:09:25 -07001088 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001089 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001090}
1091
Dan Williams89a73012011-06-30 19:14:33 -07001092static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001093{
Dan Williamsffe191c2011-06-29 13:09:25 -07001094 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001095 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001096
Dan Williamsffe191c2011-06-29 13:09:25 -07001097 if (iport->active_phy_mask == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001098 isci_port_not_ready(ihost, iport);
1099
Dan Williamsffe191c2011-06-29 13:09:25 -07001100 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001101 SCI_PORT_SUB_WAITING);
Dan Williamsffe191c2011-06-29 13:09:25 -07001102 } else if (iport->started_request_count == 0)
1103 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001104 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001105}
1106
Dan Williams89a73012011-06-30 19:14:33 -07001107static void sci_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001108{
Dan Williamsffe191c2011-06-29 13:09:25 -07001109 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001110
Dan Williams89a73012011-06-30 19:14:33 -07001111 sci_port_suspend_port_task_scheduler(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001112 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001113 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001114}
1115
Dan Williams89a73012011-06-30 19:14:33 -07001116enum sci_status sci_port_start(struct isci_port *iport)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001117{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001118 struct isci_host *ihost = iport->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001119 enum sci_status status = SCI_SUCCESS;
Dan Williams89a73012011-06-30 19:14:33 -07001120 enum sci_port_states state;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001121 u32 phy_mask;
1122
Dan Williamsffe191c2011-06-29 13:09:25 -07001123 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001124 if (state != SCI_PORT_STOPPED) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001125 dev_warn(sciport_to_dev(iport),
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001126 "%s: in wrong state: %d\n", __func__, state);
1127 return SCI_FAILURE_INVALID_STATE;
1128 }
1129
Dan Williamsffe191c2011-06-29 13:09:25 -07001130 if (iport->assigned_device_count > 0) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001131 /* TODO This is a start failure operation because
1132 * there are still devices assigned to this port.
1133 * There must be no devices assigned to a port on a
1134 * start operation.
1135 */
1136 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1137 }
1138
Dan Williamsffe191c2011-06-29 13:09:25 -07001139 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
Dan Williams89a73012011-06-30 19:14:33 -07001140 u16 rni = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001141 &ihost->available_remote_nodes, 1);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001142
1143 if (rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -07001144 sci_port_construct_dummy_rnc(iport, rni);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001145 else
1146 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williamsffe191c2011-06-29 13:09:25 -07001147 iport->reserved_rni = rni;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001148 }
1149
Dan Williamsffe191c2011-06-29 13:09:25 -07001150 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williams312e0c22011-06-28 13:47:09 -07001151 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001152
Dan Williams312e0c22011-06-28 13:47:09 -07001153 tag = isci_alloc_tag(ihost);
1154 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001155 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001156 else
Dan Williams89a73012011-06-30 19:14:33 -07001157 sci_port_construct_dummy_task(iport, tag);
Dan Williamsffe191c2011-06-29 13:09:25 -07001158 iport->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001159 }
1160
1161 if (status == SCI_SUCCESS) {
Dan Williams89a73012011-06-30 19:14:33 -07001162 phy_mask = sci_port_get_phys(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001163
1164 /*
1165 * There are one or more phys assigned to this port. Make sure
1166 * the port's phy mask is in fact legal and supported by the
1167 * silicon.
1168 */
Dan Williams89a73012011-06-30 19:14:33 -07001169 if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001170 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001171 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001172
1173 return SCI_SUCCESS;
1174 }
1175 status = SCI_FAILURE;
1176 }
1177
1178 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -07001179 sci_port_destroy_dummy_resources(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001180
1181 return status;
1182}
1183
Dan Williams89a73012011-06-30 19:14:33 -07001184enum sci_status sci_port_stop(struct isci_port *iport)
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001185{
Dan Williams89a73012011-06-30 19:14:33 -07001186 enum sci_port_states state;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001187
Dan Williamsffe191c2011-06-29 13:09:25 -07001188 state = iport->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001189 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001190 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001191 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001192 case SCI_PORT_SUB_WAITING:
1193 case SCI_PORT_SUB_OPERATIONAL:
1194 case SCI_PORT_SUB_CONFIGURING:
1195 case SCI_PORT_RESETTING:
Dan Williamsffe191c2011-06-29 13:09:25 -07001196 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001197 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001198 return SCI_SUCCESS;
1199 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001200 dev_warn(sciport_to_dev(iport),
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001201 "%s: in wrong state: %d\n", __func__, state);
1202 return SCI_FAILURE_INVALID_STATE;
1203 }
1204}
1205
Dan Williams89a73012011-06-30 19:14:33 -07001206static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001207{
1208 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001209 struct isci_phy *iphy = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001210 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001211 u32 phy_index;
1212
Dan Williamsffe191c2011-06-29 13:09:25 -07001213 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001214 if (state != SCI_PORT_SUB_OPERATIONAL) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001215 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001216 "%s: in wrong state: %d\n", __func__, state);
1217 return SCI_FAILURE_INVALID_STATE;
1218 }
1219
1220 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001221 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001222 iphy = iport->phy_table[phy_index];
Dan Williams89a73012011-06-30 19:14:33 -07001223 if (iphy && !sci_port_active_phy(iport, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001224 /*
1225 * We found a phy but it is not ready select
1226 * different phy
1227 */
Dan Williams85280952011-06-28 15:05:53 -07001228 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001229 }
1230 }
1231
1232 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001233 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001234 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001235 status = sci_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001236
1237 if (status != SCI_SUCCESS)
1238 return status;
1239
Dan Williamsffe191c2011-06-29 13:09:25 -07001240 sci_mod_timer(&iport->timer, timeout);
1241 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001242
Dan Williamsffe191c2011-06-29 13:09:25 -07001243 port_state_machine_change(iport, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001244 return SCI_SUCCESS;
1245}
1246
1247/**
Dan Williams89a73012011-06-30 19:14:33 -07001248 * sci_port_add_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001249 * @sci_port: This parameter specifies the port in which the phy will be added.
1250 * @sci_phy: This parameter is the phy which is to be added to the port.
1251 *
1252 * This method will add a PHY to the selected port. This method returns an
1253 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1254 * status is a failure to add the phy to the port.
1255 */
Dan Williams89a73012011-06-30 19:14:33 -07001256enum sci_status sci_port_add_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001257 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001258{
1259 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001260 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001261
Dan Williamsffe191c2011-06-29 13:09:25 -07001262 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001263 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001264 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001265 struct sci_sas_address port_sas_address;
1266
1267 /* Read the port assigned SAS Address if there is one */
Dan Williams89a73012011-06-30 19:14:33 -07001268 sci_port_get_sas_address(iport, &port_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001269
1270 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1271 struct sci_sas_address phy_sas_address;
1272
1273 /* Make sure that the PHY SAS Address matches the SAS Address
1274 * for this port
1275 */
Dan Williams89a73012011-06-30 19:14:33 -07001276 sci_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001277
1278 if (port_sas_address.high != phy_sas_address.high ||
1279 port_sas_address.low != phy_sas_address.low)
1280 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1281 }
Dan Williams89a73012011-06-30 19:14:33 -07001282 return sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001283 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001284 case SCI_PORT_SUB_WAITING:
1285 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001286 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001287
1288 if (status != SCI_SUCCESS)
1289 return status;
1290
Dan Williams89a73012011-06-30 19:14:33 -07001291 sci_port_general_link_up_handler(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001292 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1293 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001294
1295 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001296 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001297 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001298
1299 if (status != SCI_SUCCESS)
1300 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001301 sci_port_general_link_up_handler(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001302
1303 /* Re-enter the configuring state since this may be the last phy in
1304 * the port.
1305 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001306 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001307 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001308 return SCI_SUCCESS;
1309 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001310 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001311 "%s: in wrong state: %d\n", __func__, state);
1312 return SCI_FAILURE_INVALID_STATE;
1313 }
1314}
1315
1316/**
Dan Williams89a73012011-06-30 19:14:33 -07001317 * sci_port_remove_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001318 * @sci_port: This parameter specifies the port in which the phy will be added.
1319 * @sci_phy: This parameter is the phy which is to be added to the port.
1320 *
1321 * This method will remove the PHY from the selected PORT. This method returns
1322 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1323 * other status is a failure to add the phy to the port.
1324 */
Dan Williams89a73012011-06-30 19:14:33 -07001325enum sci_status sci_port_remove_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001326 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001327{
1328 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001329 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001330
Dan Williamsffe191c2011-06-29 13:09:25 -07001331 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001332
1333 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001334 case SCI_PORT_STOPPED:
Dan Williams89a73012011-06-30 19:14:33 -07001335 return sci_port_clear_phy(iport, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001336 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001337 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001338 if (status != SCI_SUCCESS)
1339 return status;
1340
Dan Williams89a73012011-06-30 19:14:33 -07001341 sci_port_deactivate_phy(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001342 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1343 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001344 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001345 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001346 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001347 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001348
1349 if (status != SCI_SUCCESS)
1350 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001351 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001352
1353 /* Re-enter the configuring state since this may be the last phy in
1354 * the port
1355 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001356 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001357 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001358 return SCI_SUCCESS;
1359 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001360 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001361 "%s: in wrong state: %d\n", __func__, state);
1362 return SCI_FAILURE_INVALID_STATE;
1363 }
1364}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001365
Dan Williams89a73012011-06-30 19:14:33 -07001366enum sci_status sci_port_link_up(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001367 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001368{
Dan Williams89a73012011-06-30 19:14:33 -07001369 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001370
Dan Williamsffe191c2011-06-29 13:09:25 -07001371 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001372 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001373 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001374 /* Since this is the first phy going link up for the port we
1375 * can just enable it and continue
1376 */
Dan Williams89a73012011-06-30 19:14:33 -07001377 sci_port_activate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001378
Dan Williamsffe191c2011-06-29 13:09:25 -07001379 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001380 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001381 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001382 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001383 sci_port_general_link_up_handler(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001384 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001385 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001386 /* TODO We should make sure that the phy that has gone
1387 * link up is the same one on which we sent the reset. It is
1388 * possible that the phy on which we sent the reset is not the
1389 * one that has gone link up and we want to make sure that
1390 * phy being reset comes back. Consider the case where a
1391 * reset is sent but before the hardware processes the reset it
1392 * get a link up on the port because of a hot plug event.
1393 * because of the reset request this phy will go link down
1394 * almost immediately.
1395 */
1396
1397 /* In the resetting state we don't notify the user regarding
1398 * link up and link down notifications.
1399 */
Dan Williams89a73012011-06-30 19:14:33 -07001400 sci_port_general_link_up_handler(iport, iphy, false);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001401 return SCI_SUCCESS;
1402 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001403 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001404 "%s: in wrong state: %d\n", __func__, state);
1405 return SCI_FAILURE_INVALID_STATE;
1406 }
1407}
1408
Dan Williams89a73012011-06-30 19:14:33 -07001409enum sci_status sci_port_link_down(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001410 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001411{
Dan Williams89a73012011-06-30 19:14:33 -07001412 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001413
Dan Williamsffe191c2011-06-29 13:09:25 -07001414 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001415 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001416 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001417 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001418
1419 /* If there are no active phys left in the port, then
1420 * transition the port to the WAITING state until such time
1421 * as a phy goes link up
1422 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001423 if (iport->active_phy_mask == 0)
1424 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001425 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001426 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001427 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001428 /* In the resetting state we don't notify the user regarding
1429 * link up and link down notifications. */
Dan Williams89a73012011-06-30 19:14:33 -07001430 sci_port_deactivate_phy(iport, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001431 return SCI_SUCCESS;
1432 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001433 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001434 "%s: in wrong state: %d\n", __func__, state);
1435 return SCI_FAILURE_INVALID_STATE;
1436 }
1437}
1438
Dan Williams89a73012011-06-30 19:14:33 -07001439enum sci_status sci_port_start_io(struct isci_port *iport,
1440 struct isci_remote_device *idev,
1441 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001442{
Dan Williams89a73012011-06-30 19:14:33 -07001443 enum sci_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001444
Dan Williamsffe191c2011-06-29 13:09:25 -07001445 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001446 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001447 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001448 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001449 case SCI_PORT_SUB_OPERATIONAL:
Dan Williamsffe191c2011-06-29 13:09:25 -07001450 iport->started_request_count++;
Dan Williams68138202011-05-12 07:16:06 -07001451 return SCI_SUCCESS;
1452 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001453 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001454 "%s: in wrong state: %d\n", __func__, state);
1455 return SCI_FAILURE_INVALID_STATE;
1456 }
1457}
1458
Dan Williams89a73012011-06-30 19:14:33 -07001459enum sci_status sci_port_complete_io(struct isci_port *iport,
1460 struct isci_remote_device *idev,
1461 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001462{
Dan Williams89a73012011-06-30 19:14:33 -07001463 enum sci_port_states state;
Dan Williams68138202011-05-12 07:16:06 -07001464
Dan Williamsffe191c2011-06-29 13:09:25 -07001465 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001466 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001467 case SCI_PORT_STOPPED:
Dan Williamsffe191c2011-06-29 13:09:25 -07001468 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001469 "%s: in wrong state: %d\n", __func__, state);
1470 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001471 case SCI_PORT_STOPPING:
Dan Williams89a73012011-06-30 19:14:33 -07001472 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001473
Dan Williamsffe191c2011-06-29 13:09:25 -07001474 if (iport->started_request_count == 0)
1475 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001476 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001477 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001478 case SCI_PORT_READY:
1479 case SCI_PORT_RESETTING:
1480 case SCI_PORT_FAILED:
1481 case SCI_PORT_SUB_WAITING:
1482 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001483 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001484 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001485 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001486 sci_port_decrement_request_count(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001487 if (iport->started_request_count == 0) {
1488 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001489 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001490 }
1491 break;
1492 }
1493 return SCI_SUCCESS;
1494}
Dan Williamse2f8db52011-05-10 02:28:46 -07001495
Dan Williams89a73012011-06-30 19:14:33 -07001496static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001497{
1498 u32 pts_control_value;
1499
Dan Williams89a73012011-06-30 19:14:33 -07001500 /* enable the port task scheduler in a suspended state */
Dan Williamsffe191c2011-06-29 13:09:25 -07001501 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001502 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -07001503 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001504}
1505
Dan Williams89a73012011-06-30 19:14:33 -07001506static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001507{
1508 u32 pts_control_value;
1509
Dan Williamsffe191c2011-06-29 13:09:25 -07001510 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001511 pts_control_value &=
1512 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
Dan Williamsffe191c2011-06-29 13:09:25 -07001513 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001514}
1515
Dan Williams89a73012011-06-30 19:14:33 -07001516static void sci_port_post_dummy_remote_node(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001517{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001518 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001519 u8 phys_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -07001520 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001521 u16 rni = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -07001522 u32 command;
1523
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001524 rnc = &ihost->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -07001525 rnc->ssp.is_valid = true;
1526
1527 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1528 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1529
Dan Williams89a73012011-06-30 19:14:33 -07001530 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001531
1532 /* ensure hardware has seen the post rnc command and give it
1533 * ample time to act before sending the suspend
1534 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001535 readl(&ihost->smu_registers->interrupt_status); /* flush */
Dan Williamse2f8db52011-05-10 02:28:46 -07001536 udelay(10);
1537
1538 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1539 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1540
Dan Williams89a73012011-06-30 19:14:33 -07001541 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001542}
1543
Dan Williams89a73012011-06-30 19:14:33 -07001544static void sci_port_stopped_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 Williamsffe191c2011-06-29 13:09:25 -07001548 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001549 /*
1550 * If we enter this state becasuse of a request to stop
1551 * the port then we want to disable the hardwares port
1552 * task scheduler. */
Dan Williams89a73012011-06-30 19:14:33 -07001553 sci_port_disable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001554 }
1555}
1556
Dan Williams89a73012011-06-30 19:14:33 -07001557static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001558{
Dan Williamsffe191c2011-06-29 13:09:25 -07001559 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001560
1561 /* Enable and suspend the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001562 sci_port_enable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001563}
1564
Dan Williams89a73012011-06-30 19:14:33 -07001565static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001566{
Dan Williamsffe191c2011-06-29 13:09:25 -07001567 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001568 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001569 u32 prev_state;
1570
Dan Williamsffe191c2011-06-29 13:09:25 -07001571 prev_state = iport->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001572 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001573 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1574 else
1575 isci_port_not_ready(ihost, iport);
1576
1577 /* Post and suspend the dummy remote node context for this port. */
Dan Williams89a73012011-06-30 19:14:33 -07001578 sci_port_post_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001579
1580 /* Start the ready substate machine */
Dan Williamsffe191c2011-06-29 13:09:25 -07001581 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001582 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001583}
1584
Dan Williams89a73012011-06-30 19:14:33 -07001585static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001586{
Dan Williamsffe191c2011-06-29 13:09:25 -07001587 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001588
Dan Williamsffe191c2011-06-29 13:09:25 -07001589 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001590}
1591
Dan Williams89a73012011-06-30 19:14:33 -07001592static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001593{
Dan Williamsffe191c2011-06-29 13:09:25 -07001594 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001595
Dan Williamsffe191c2011-06-29 13:09:25 -07001596 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001597
Dan Williams89a73012011-06-30 19:14:33 -07001598 sci_port_destroy_dummy_resources(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001599}
1600
Dan Williams89a73012011-06-30 19:14:33 -07001601static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001602{
Dan Williamsffe191c2011-06-29 13:09:25 -07001603 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001604
Dan Williamse2f8db52011-05-10 02:28:46 -07001605 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1606}
1607
1608/* --------------------------------------------------------------------------- */
1609
Dan Williams89a73012011-06-30 19:14:33 -07001610static const struct sci_base_state sci_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001611 [SCI_PORT_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001612 .enter_state = sci_port_stopped_state_enter,
1613 .exit_state = sci_port_stopped_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001614 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001615 [SCI_PORT_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001616 .exit_state = sci_port_stopping_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001617 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001618 [SCI_PORT_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001619 .enter_state = sci_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001620 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001621 [SCI_PORT_SUB_WAITING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001622 .enter_state = sci_port_ready_substate_waiting_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001623 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001624 [SCI_PORT_SUB_OPERATIONAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001625 .enter_state = sci_port_ready_substate_operational_enter,
1626 .exit_state = sci_port_ready_substate_operational_exit
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001627 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001628 [SCI_PORT_SUB_CONFIGURING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001629 .enter_state = sci_port_ready_substate_configuring_enter,
1630 .exit_state = sci_port_ready_substate_configuring_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001631 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001632 [SCI_PORT_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001633 .exit_state = sci_port_resetting_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001634 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001635 [SCI_PORT_FAILED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001636 .enter_state = sci_port_failed_state_enter,
Dan Williamse2f8db52011-05-10 02:28:46 -07001637 }
1638};
1639
Dan Williams89a73012011-06-30 19:14:33 -07001640void sci_port_construct(struct isci_port *iport, u8 index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001641 struct isci_host *ihost)
Dan Williamse2f8db52011-05-10 02:28:46 -07001642{
Dan Williams89a73012011-06-30 19:14:33 -07001643 sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001644
Dan Williamsffe191c2011-06-29 13:09:25 -07001645 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1646 iport->physical_port_index = index;
1647 iport->active_phy_mask = 0;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001648 iport->last_active_phy = 0;
1649 iport->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001650
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001651 iport->owning_controller = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001652
Dan Williamsffe191c2011-06-29 13:09:25 -07001653 iport->started_request_count = 0;
1654 iport->assigned_device_count = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -07001655
Dan Williamsffe191c2011-06-29 13:09:25 -07001656 iport->reserved_rni = SCU_DUMMY_INDEX;
1657 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -07001658
Dan Williamsffe191c2011-06-29 13:09:25 -07001659 sci_init_timer(&iport->timer, port_timeout);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001660
Dan Williamsffe191c2011-06-29 13:09:25 -07001661 iport->port_task_scheduler_registers = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001662
1663 for (index = 0; index < SCI_MAX_PHYS; index++)
Dan Williamsffe191c2011-06-29 13:09:25 -07001664 iport->phy_table[index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001665}
1666
1667void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1668{
1669 INIT_LIST_HEAD(&iport->remote_dev_list);
1670 INIT_LIST_HEAD(&iport->domain_dev_list);
1671 spin_lock_init(&iport->state_lock);
1672 init_completion(&iport->start_complete);
1673 iport->isci_host = ihost;
1674 isci_port_change_state(iport, isci_freed);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001675 atomic_set(&iport->event, 0);
Dan Williamse2f8db52011-05-10 02:28:46 -07001676}
1677
1678/**
1679 * isci_port_get_state() - This function gets the status of the port object.
1680 * @isci_port: This parameter points to the isci_port object
1681 *
1682 * status of the object as a isci_status enum.
1683 */
1684enum isci_status isci_port_get_state(
1685 struct isci_port *isci_port)
1686{
1687 return isci_port->status;
1688}
1689
Dan Williams89a73012011-06-30 19:14:33 -07001690void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -07001691{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001692 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001693
1694 /* notify the user. */
Dan Williamsffe191c2011-06-29 13:09:25 -07001695 isci_port_bc_change_received(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -07001696}
1697
Dan Williams4393aa42011-03-31 13:10:44 -07001698int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1699 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001700{
Dan Williams4393aa42011-03-31 13:10:44 -07001701 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001702 enum sci_status status;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001703 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001704
Dan Williams4393aa42011-03-31 13:10:44 -07001705 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1706 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001707
Dan Williams4393aa42011-03-31 13:10:44 -07001708 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001709
Dan Williams4393aa42011-03-31 13:10:44 -07001710 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001711
1712 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams89a73012011-06-30 19:14:33 -07001713 status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001714
Dan Williams4393aa42011-03-31 13:10:44 -07001715 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001716
1717 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -07001718 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001719
Dan Williams4393aa42011-03-31 13:10:44 -07001720 dev_dbg(&ihost->pdev->dev,
1721 "%s: iport = %p; hard reset completion\n",
1722 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001723
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001724 if (iport->hard_reset_status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001725 ret = TMF_RESP_FUNC_FAILED;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001726
1727 dev_err(&ihost->pdev->dev,
1728 "%s: iport = %p; hard reset failed (0x%x)\n",
1729 __func__, iport, iport->hard_reset_status);
1730 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001731 } else {
1732 ret = TMF_RESP_FUNC_FAILED;
1733
Dan Williams4393aa42011-03-31 13:10:44 -07001734 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001735 "%s: iport = %p; sci_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001736 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001737 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001738
1739 }
1740
1741 /* If the hard reset for the port has failed, consider this
1742 * the same as link failures on all phys in the port.
1743 */
1744 if (ret != TMF_RESP_FUNC_COMPLETE) {
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001745
Dan Williams4393aa42011-03-31 13:10:44 -07001746 dev_err(&ihost->pdev->dev,
1747 "%s: iport = %p; hard reset failed "
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001748 "(0x%x) - driving explicit link fail for all phys\n",
1749 __func__, iport, iport->hard_reset_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001750 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001751 return ret;
1752}
Dave Jiang09d7da12011-03-26 16:11:51 -07001753
Dan Williamse2f8db52011-05-10 02:28:46 -07001754/**
1755 * isci_port_deformed() - This function is called by libsas when a port becomes
1756 * inactive.
1757 * @phy: This parameter specifies the libsas phy with the inactive port.
1758 *
1759 */
1760void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001761{
Dan Williamse2f8db52011-05-10 02:28:46 -07001762 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1763}
1764
1765/**
1766 * isci_port_formed() - This function is called by libsas when a port becomes
1767 * active.
1768 * @phy: This parameter specifies the libsas phy with the active port.
1769 *
1770 */
1771void isci_port_formed(struct asd_sas_phy *phy)
1772{
1773 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
Dave Jiang09d7da12011-03-26 16:11:51 -07001774}