blob: 8f501b0a81d6e7842a9d9caaaae1d228b996f2ab [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 */
Dave Jiang2d9c2242011-05-04 18:45:05 -070055#include <scsi/sas.h>
Bartek Nowakowski7e629842012-01-04 01:33:20 -080056#include <linux/bitops.h>
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "remote_node_context.h"
Dan Williams88f3b622011-04-22 19:18:03 -070062#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063#include "task.h"
64
Dan Williamsd7a0ccd2012-02-10 01:18:44 -080065#undef C
66#define C(a) (#a)
67const char *dev_state_name(enum sci_remote_device_states state)
68{
69 static const char * const strings[] = REMOTE_DEV_STATES;
70
71 return strings[state];
72}
73#undef C
74
Dan Williamsab2e8f72011-04-27 16:32:45 -070075/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070076 * isci_remote_device_not_ready() - This function is called by the ihost when
Dan Williamsab2e8f72011-04-27 16:32:45 -070077 * the remote device is not ready. We mark the isci device as ready (not
78 * "ready_for_io") and signal the waiting proccess.
79 * @isci_host: This parameter specifies the isci host object.
80 * @isci_device: This parameter specifies the remote device
81 *
Dan Williams89a73012011-06-30 19:14:33 -070082 * sci_lock is held on entrance to this function.
Dan Williamsab2e8f72011-04-27 16:32:45 -070083 */
84static void isci_remote_device_not_ready(struct isci_host *ihost,
85 struct isci_remote_device *idev, u32 reason)
86{
James Bottomleya5ec7f862011-07-03 14:14:45 -050087 struct isci_request *ireq;
Jeff Skirvin9274f452011-06-23 17:09:02 -070088
Dan Williamsab2e8f72011-04-27 16:32:45 -070089 dev_dbg(&ihost->pdev->dev,
90 "%s: isci_device = %p\n", __func__, idev);
91
Jeff Skirvin9274f452011-06-23 17:09:02 -070092 switch (reason) {
93 case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
Dan Williams209fae12011-06-13 17:39:44 -070094 set_bit(IDEV_GONE, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -070095 break;
96 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
97 set_bit(IDEV_IO_NCQERROR, &idev->flags);
98
99 /* Kill all outstanding requests for the device. */
100 list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
101
102 dev_dbg(&ihost->pdev->dev,
103 "%s: isci_device = %p request = %p\n",
104 __func__, idev, ireq);
105
Dan Williams89a73012011-06-30 19:14:33 -0700106 sci_controller_terminate_request(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700107 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700108 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700109 }
110 /* Fall through into the default case... */
111 default:
Dan Williamsf2088262011-06-16 11:26:12 -0700112 clear_bit(IDEV_IO_READY, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700113 break;
114 }
Dan Williamsab2e8f72011-04-27 16:32:45 -0700115}
116
117/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700118 * isci_remote_device_ready() - This function is called by the ihost when the
Dan Williamsab2e8f72011-04-27 16:32:45 -0700119 * remote device is ready. We mark the isci device as ready and signal the
120 * waiting proccess.
121 * @ihost: our valid isci_host
122 * @idev: remote device
123 *
124 */
125static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
126{
127 dev_dbg(&ihost->pdev->dev,
128 "%s: idev = %p\n", __func__, idev);
129
Jeff Skirvin9274f452011-06-23 17:09:02 -0700130 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -0700131 set_bit(IDEV_IO_READY, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700132 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
133 wake_up(&ihost->eventq);
134}
135
Dan Williamsec575662011-05-01 14:19:25 -0700136/* called once the remote node context is ready to be freed.
137 * The remote device can now report that its stop operation is complete. none
138 */
139static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700140{
Dan Williams78a6f062011-06-30 16:31:37 -0700141 struct isci_remote_device *idev = _dev;
Dan Williamsec575662011-05-01 14:19:25 -0700142
Dan Williams78a6f062011-06-30 16:31:37 -0700143 BUG_ON(idev->started_request_count != 0);
144 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700145}
146
Dan Williams89a73012011-06-30 19:14:33 -0700147static enum sci_status sci_remote_device_terminate_requests(struct isci_remote_device *idev)
Dan Williamsec575662011-05-01 14:19:25 -0700148{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700149 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsec575662011-05-01 14:19:25 -0700150 enum sci_status status = SCI_SUCCESS;
Dan Williams76802ce2011-06-29 09:45:48 -0700151 u32 i;
Dan Williamsec575662011-05-01 14:19:25 -0700152
Dan Williams76802ce2011-06-29 09:45:48 -0700153 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
Dan Williamsdb056252011-06-17 14:18:39 -0700154 struct isci_request *ireq = ihost->reqs[i];
Dan Williamsec575662011-05-01 14:19:25 -0700155 enum sci_status s;
156
Dan Williamsdb056252011-06-17 14:18:39 -0700157 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
Dan Williams78a6f062011-06-30 16:31:37 -0700158 ireq->target_device != idev)
Dan Williamsec575662011-05-01 14:19:25 -0700159 continue;
Dan Williamsdb056252011-06-17 14:18:39 -0700160
Dan Williams89a73012011-06-30 19:14:33 -0700161 s = sci_controller_terminate_request(ihost, idev, ireq);
Dan Williamsec575662011-05-01 14:19:25 -0700162 if (s != SCI_SUCCESS)
163 status = s;
164 }
165
166 return status;
167}
168
Dan Williams89a73012011-06-30 19:14:33 -0700169enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
Dan Williamsec575662011-05-01 14:19:25 -0700170 u32 timeout)
171{
Dan Williams78a6f062011-06-30 16:31:37 -0700172 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700173 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsec575662011-05-01 14:19:25 -0700174
175 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000176 case SCI_DEV_INITIAL:
177 case SCI_DEV_FAILED:
178 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700179 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800180 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
181 __func__, dev_state_name(state));
Dan Williamsec575662011-05-01 14:19:25 -0700182 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000183 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700184 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000185 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700186 /* device not started so there had better be no requests */
Dan Williams78a6f062011-06-30 16:31:37 -0700187 BUG_ON(idev->started_request_count != 0);
Dan Williams89a73012011-06-30 19:14:33 -0700188 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700189 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700190 /* Transition to the stopping state and wait for the
191 * remote node to complete being posted and invalidated.
192 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000193 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700194 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000195 case SCI_DEV_READY:
196 case SCI_STP_DEV_IDLE:
197 case SCI_STP_DEV_CMD:
198 case SCI_STP_DEV_NCQ:
199 case SCI_STP_DEV_NCQ_ERROR:
200 case SCI_STP_DEV_AWAIT_RESET:
201 case SCI_SMP_DEV_IDLE:
202 case SCI_SMP_DEV_CMD:
203 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williams78a6f062011-06-30 16:31:37 -0700204 if (idev->started_request_count == 0) {
Dan Williams89a73012011-06-30 19:14:33 -0700205 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700206 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700207 return SCI_SUCCESS;
208 } else
Dan Williams89a73012011-06-30 19:14:33 -0700209 return sci_remote_device_terminate_requests(idev);
Dan Williamsec575662011-05-01 14:19:25 -0700210 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000211 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700212 /* All requests should have been terminated, but if there is an
213 * attempt to stop a device already in the stopping state, then
214 * try again to terminate.
215 */
Dan Williams89a73012011-06-30 19:14:33 -0700216 return sci_remote_device_terminate_requests(idev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000217 case SCI_DEV_RESETTING:
218 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700219 return SCI_SUCCESS;
220 }
Dan Williams88f3b622011-04-22 19:18:03 -0700221}
Dan Williams6f231dd2011-07-02 22:56:22 -0700222
Dan Williams89a73012011-06-30 19:14:33 -0700223enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700224{
Dan Williams78a6f062011-06-30 16:31:37 -0700225 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700226 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700227
228 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000229 case SCI_DEV_INITIAL:
230 case SCI_DEV_STOPPED:
231 case SCI_DEV_STARTING:
232 case SCI_SMP_DEV_IDLE:
233 case SCI_SMP_DEV_CMD:
234 case SCI_DEV_STOPPING:
235 case SCI_DEV_FAILED:
236 case SCI_DEV_RESETTING:
237 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700238 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800239 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
240 __func__, dev_state_name(state));
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700241 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000242 case SCI_DEV_READY:
243 case SCI_STP_DEV_IDLE:
244 case SCI_STP_DEV_CMD:
245 case SCI_STP_DEV_NCQ:
246 case SCI_STP_DEV_NCQ_ERROR:
247 case SCI_STP_DEV_AWAIT_RESET:
248 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700249 return SCI_SUCCESS;
250 }
Dan Williams88f3b622011-04-22 19:18:03 -0700251}
252
Dan Williams89a73012011-06-30 19:14:33 -0700253enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700254{
Dan Williams78a6f062011-06-30 16:31:37 -0700255 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700256 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700257
Edmund Nadolskie3013702011-06-02 00:10:43 +0000258 if (state != SCI_DEV_RESETTING) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800259 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
260 __func__, dev_state_name(state));
Dan Williams81515182011-05-01 14:53:00 -0700261 return SCI_FAILURE_INVALID_STATE;
262 }
263
Edmund Nadolskie3013702011-06-02 00:10:43 +0000264 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700265 return SCI_SUCCESS;
266}
Dan Williams88f3b622011-04-22 19:18:03 -0700267
Dan Williams89a73012011-06-30 19:14:33 -0700268enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
Dan Williams323f0ec2011-05-01 16:15:47 -0700269 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700270{
Dan Williams78a6f062011-06-30 16:31:37 -0700271 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700272 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams323f0ec2011-05-01 16:15:47 -0700273
Edmund Nadolskie3013702011-06-02 00:10:43 +0000274 if (state != SCI_STP_DEV_CMD) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800275 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
276 __func__, dev_state_name(state));
Dan Williams323f0ec2011-05-01 16:15:47 -0700277 return SCI_FAILURE_INVALID_STATE;
278 }
279
Dan Williams89a73012011-06-30 19:14:33 -0700280 return sci_remote_node_context_suspend(&idev->rnc,
Dan Williams323f0ec2011-05-01 16:15:47 -0700281 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700282}
283
Dan Williams89a73012011-06-30 19:14:33 -0700284enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
Dan Williams01bec772011-05-01 16:51:11 -0700285 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700286{
Dan Williams78a6f062011-06-30 16:31:37 -0700287 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700288 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700289 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams01bec772011-05-01 16:51:11 -0700290 enum sci_status status;
291
292 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000293 case SCI_DEV_INITIAL:
294 case SCI_DEV_STOPPED:
295 case SCI_DEV_STARTING:
296 case SCI_STP_DEV_IDLE:
297 case SCI_SMP_DEV_IDLE:
298 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700299 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800300 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
301 __func__, dev_state_name(state));
Dan Williams01bec772011-05-01 16:51:11 -0700302 /* Return the frame back to the controller */
Dan Williams89a73012011-06-30 19:14:33 -0700303 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700304 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000305 case SCI_DEV_READY:
306 case SCI_STP_DEV_NCQ_ERROR:
307 case SCI_STP_DEV_AWAIT_RESET:
308 case SCI_DEV_STOPPING:
309 case SCI_DEV_FAILED:
310 case SCI_DEV_RESETTING: {
Dan Williams5076a1a2011-06-27 14:57:03 -0700311 struct isci_request *ireq;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700312 struct ssp_frame_hdr hdr;
313 void *frame_header;
314 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700315
Dan Williams89a73012011-06-30 19:14:33 -0700316 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700317 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700318 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700319 if (status != SCI_SUCCESS)
320 return status;
321
Dave Jiang2d9c2242011-05-04 18:45:05 -0700322 word_cnt = sizeof(hdr) / sizeof(u32);
323 sci_swab32_cpy(&hdr, frame_header, word_cnt);
324
Dan Williams89a73012011-06-30 19:14:33 -0700325 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
Dan Williams78a6f062011-06-30 16:31:37 -0700326 if (ireq && ireq->target_device == idev) {
Dan Williams01bec772011-05-01 16:51:11 -0700327 /* The IO request is now in charge of releasing the frame */
Dan Williams89a73012011-06-30 19:14:33 -0700328 status = sci_io_request_frame_handler(ireq, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700329 } else {
330 /* We could not map this tag to a valid IO
331 * request Just toss the frame and continue
332 */
Dan Williams89a73012011-06-30 19:14:33 -0700333 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700334 }
335 break;
336 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000337 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700338 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700339
Dan Williams89a73012011-06-30 19:14:33 -0700340 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700341 frame_index,
342 (void **)&hdr);
343 if (status != SCI_SUCCESS)
344 return status;
345
Dave Jiange76d6182011-05-04 15:02:03 -0700346 if (hdr->fis_type == FIS_SETDEVBITS &&
347 (hdr->status & ATA_ERR)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700348 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Dan Williams01bec772011-05-01 16:51:11 -0700349
350 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000351 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700352 } else if (hdr->fis_type == FIS_REGD2H &&
353 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700354 /*
355 * Some devices return D2H FIS when an NCQ error is detected.
356 * Treat this like an SDB error FIS ready reason.
357 */
Dan Williams78a6f062011-06-30 16:31:37 -0700358 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
359 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700360 } else
361 status = SCI_FAILURE;
362
Dan Williams89a73012011-06-30 19:14:33 -0700363 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700364 break;
365 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000366 case SCI_STP_DEV_CMD:
367 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700368 /* The device does not process any UF received from the hardware while
369 * in this state. All unsolicited frames are forwarded to the io request
370 * object.
371 */
Dan Williams89a73012011-06-30 19:14:33 -0700372 status = sci_io_request_frame_handler(idev->working_request, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700373 break;
374 }
375
376 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700377}
378
Dan Williams78a6f062011-06-30 16:31:37 -0700379static bool is_remote_device_ready(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700380{
Dan Williamse6225712011-05-01 16:26:09 -0700381
Dan Williams78a6f062011-06-30 16:31:37 -0700382 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700383 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700384
385 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000386 case SCI_DEV_READY:
387 case SCI_STP_DEV_IDLE:
388 case SCI_STP_DEV_CMD:
389 case SCI_STP_DEV_NCQ:
390 case SCI_STP_DEV_NCQ_ERROR:
391 case SCI_STP_DEV_AWAIT_RESET:
392 case SCI_SMP_DEV_IDLE:
393 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700394 return true;
395 default:
396 return false;
397 }
398}
399
Dan Williamsb50102d2011-09-30 18:52:19 -0700400/*
401 * called once the remote node context has transisitioned to a ready
402 * state (after suspending RX and/or TX due to early D2H fis)
403 */
404static void atapi_remote_device_resume_done(void *_dev)
405{
406 struct isci_remote_device *idev = _dev;
407 struct isci_request *ireq = idev->working_request;
408
409 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
410}
411
Dan Williams89a73012011-06-30 19:14:33 -0700412enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
Dan Williamse6225712011-05-01 16:26:09 -0700413 u32 event_code)
414{
Dan Williams78a6f062011-06-30 16:31:37 -0700415 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700416 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700417 enum sci_status status;
418
419 switch (scu_get_event_type(event_code)) {
420 case SCU_EVENT_TYPE_RNC_OPS_MISC:
421 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
422 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
Dan Williams89a73012011-06-30 19:14:33 -0700423 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
Dan Williamse6225712011-05-01 16:26:09 -0700424 break;
425 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
426 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
427 status = SCI_SUCCESS;
428
429 /* Suspend the associated RNC */
Dan Williams89a73012011-06-30 19:14:33 -0700430 sci_remote_node_context_suspend(&idev->rnc,
Dan Williamse6225712011-05-01 16:26:09 -0700431 SCI_SOFTWARE_SUSPENSION,
432 NULL, NULL);
433
Dan Williams78a6f062011-06-30 16:31:37 -0700434 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700435 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700436 __func__, idev, event_code,
437 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700438 ? "I_T_Nexus_Timeout event"
439 : "I_T_Nexus_Timeout event in wrong state");
440
441 break;
442 }
443 /* Else, fall through and treat as unhandled... */
444 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700445 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700446 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700447 __func__, idev, event_code,
448 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700449 ? "unexpected event"
450 : "unexpected event in wrong state");
451 status = SCI_FAILURE_INVALID_STATE;
452 break;
453 }
454
455 if (status != SCI_SUCCESS)
456 return status;
457
Dan Williamsb50102d2011-09-30 18:52:19 -0700458 if (state == SCI_STP_DEV_ATAPI_ERROR) {
459 /* For ATAPI error state resume the RNC right away. */
460 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
461 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX) {
462 return sci_remote_node_context_resume(&idev->rnc,
463 atapi_remote_device_resume_done,
464 idev);
465 }
466 }
467
Edmund Nadolskie3013702011-06-02 00:10:43 +0000468 if (state == SCI_STP_DEV_IDLE) {
Dan Williamse6225712011-05-01 16:26:09 -0700469
470 /* We pick up suspension events to handle specifically to this
471 * state. We resume the RNC right away.
472 */
473 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
474 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
Dan Williams89a73012011-06-30 19:14:33 -0700475 status = sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williamse6225712011-05-01 16:26:09 -0700476 }
477
478 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700479}
480
Dan Williams89a73012011-06-30 19:14:33 -0700481static void sci_remote_device_start_request(struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700482 struct isci_request *ireq,
Dan Williams18606552011-05-01 14:57:11 -0700483 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700484{
Dan Williams78a6f062011-06-30 16:31:37 -0700485 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700486
487 /* cleanup requests that failed after starting on the port */
488 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -0700489 sci_port_complete_io(iport, idev, ireq);
Dan Williams209fae12011-06-13 17:39:44 -0700490 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700491 kref_get(&idev->kref);
Dan Williams34a99152011-07-01 02:25:15 -0700492 idev->started_request_count++;
Dan Williams209fae12011-06-13 17:39:44 -0700493 }
Dan Williams18606552011-05-01 14:57:11 -0700494}
495
Dan Williams89a73012011-06-30 19:14:33 -0700496enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700497 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700498 struct isci_request *ireq)
Dan Williams18606552011-05-01 14:57:11 -0700499{
Dan Williams78a6f062011-06-30 16:31:37 -0700500 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700501 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700502 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700503 enum sci_status status;
504
505 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000506 case SCI_DEV_INITIAL:
507 case SCI_DEV_STOPPED:
508 case SCI_DEV_STARTING:
509 case SCI_STP_DEV_NCQ_ERROR:
510 case SCI_DEV_STOPPING:
511 case SCI_DEV_FAILED:
512 case SCI_DEV_RESETTING:
513 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700514 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800515 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
516 __func__, dev_state_name(state));
Dan Williams18606552011-05-01 14:57:11 -0700517 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000518 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700519 /* attempt to start an io request for this device object. The remote
520 * device object will issue the start request for the io and if
521 * successful it will start the request for the port object then
522 * increment its own request count.
523 */
Dan Williams89a73012011-06-30 19:14:33 -0700524 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700525 if (status != SCI_SUCCESS)
526 return status;
527
Dan Williams89a73012011-06-30 19:14:33 -0700528 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700529 if (status != SCI_SUCCESS)
530 break;
531
Dan Williams89a73012011-06-30 19:14:33 -0700532 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700533 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000534 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700535 /* handle the start io operation for a sata device that is in
536 * the command idle state. - Evalute the type of IO request to
537 * be started - If its an NCQ request change to NCQ substate -
538 * If its any other command change to the CMD substate
539 *
540 * If this is a softreset we may want to have a different
541 * substate.
542 */
Dan Williams89a73012011-06-30 19:14:33 -0700543 enum sci_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700544 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700545
Dan Williams89a73012011-06-30 19:14:33 -0700546 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700547 if (status != SCI_SUCCESS)
548 return status;
549
Dan Williams89a73012011-06-30 19:14:33 -0700550 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700551 if (status != SCI_SUCCESS)
552 break;
553
Dan Williams89a73012011-06-30 19:14:33 -0700554 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700555 if (status != SCI_SUCCESS)
556 break;
557
Dave Jiange76d6182011-05-04 15:02:03 -0700558 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000559 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700560 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700561 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000562 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700563 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000564 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700565 break;
566 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000567 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700568 struct sas_task *task = isci_request_access_task(ireq);
569
570 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700571 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700572 if (status != SCI_SUCCESS)
573 return status;
574
Dan Williams89a73012011-06-30 19:14:33 -0700575 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700576 if (status != SCI_SUCCESS)
577 break;
578
Dan Williams89a73012011-06-30 19:14:33 -0700579 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700580 } else
581 return SCI_FAILURE_INVALID_STATE;
582 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700583 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000584 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700585 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000586 case SCI_SMP_DEV_IDLE:
Dan Williams89a73012011-06-30 19:14:33 -0700587 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700588 if (status != SCI_SUCCESS)
589 return status;
590
Dan Williams89a73012011-06-30 19:14:33 -0700591 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700592 if (status != SCI_SUCCESS)
593 break;
594
Dan Williams89a73012011-06-30 19:14:33 -0700595 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700596 if (status != SCI_SUCCESS)
597 break;
598
Dan Williams78a6f062011-06-30 16:31:37 -0700599 idev->working_request = ireq;
600 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700601 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000602 case SCI_STP_DEV_CMD:
603 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700604 /* device is already handling a command it can not accept new commands
605 * until this one is complete.
606 */
607 return SCI_FAILURE_INVALID_STATE;
608 }
609
Dan Williams89a73012011-06-30 19:14:33 -0700610 sci_remote_device_start_request(idev, ireq, status);
Dan Williams18606552011-05-01 14:57:11 -0700611 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700612}
613
Dan Williamsffe191c2011-06-29 13:09:25 -0700614static enum sci_status common_complete_io(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700615 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700616 struct isci_request *ireq)
Dan Williams88f3b622011-04-22 19:18:03 -0700617{
Dan Williams10a09e62011-05-01 15:33:43 -0700618 enum sci_status status;
619
Dan Williams89a73012011-06-30 19:14:33 -0700620 status = sci_request_complete(ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700621 if (status != SCI_SUCCESS)
622 return status;
623
Dan Williams89a73012011-06-30 19:14:33 -0700624 status = sci_port_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700625 if (status != SCI_SUCCESS)
626 return status;
627
Dan Williams89a73012011-06-30 19:14:33 -0700628 sci_remote_device_decrement_request_count(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700629 return status;
630}
631
Dan Williams89a73012011-06-30 19:14:33 -0700632enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700633 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700634 struct isci_request *ireq)
Dan Williams10a09e62011-05-01 15:33:43 -0700635{
Dan Williams78a6f062011-06-30 16:31:37 -0700636 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700637 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700638 struct isci_port *iport = idev->owning_port;
Dan Williams10a09e62011-05-01 15:33:43 -0700639 enum sci_status status;
640
641 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000642 case SCI_DEV_INITIAL:
643 case SCI_DEV_STOPPED:
644 case SCI_DEV_STARTING:
645 case SCI_STP_DEV_IDLE:
646 case SCI_SMP_DEV_IDLE:
647 case SCI_DEV_FAILED:
648 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700649 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800650 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
651 __func__, dev_state_name(state));
Dan Williams10a09e62011-05-01 15:33:43 -0700652 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000653 case SCI_DEV_READY:
654 case SCI_STP_DEV_AWAIT_RESET:
655 case SCI_DEV_RESETTING:
Dan Williams78a6f062011-06-30 16:31:37 -0700656 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700657 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000658 case SCI_STP_DEV_CMD:
659 case SCI_STP_DEV_NCQ:
660 case SCI_STP_DEV_NCQ_ERROR:
Dan Williamsb50102d2011-09-30 18:52:19 -0700661 case SCI_STP_DEV_ATAPI_ERROR:
Dan Williams78a6f062011-06-30 16:31:37 -0700662 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700663 if (status != SCI_SUCCESS)
664 break;
665
Dan Williams5076a1a2011-06-27 14:57:03 -0700666 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams10a09e62011-05-01 15:33:43 -0700667 /* This request causes hardware error, device needs to be Lun Reset.
668 * So here we force the state machine to IDLE state so the rest IOs
669 * can reach RNC state handler, these IOs will be completed by RNC with
670 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
671 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000672 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams34a99152011-07-01 02:25:15 -0700673 } else if (idev->started_request_count == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000674 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700675 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000676 case SCI_SMP_DEV_CMD:
Dan Williams78a6f062011-06-30 16:31:37 -0700677 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700678 if (status != SCI_SUCCESS)
679 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000680 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700681 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000682 case SCI_DEV_STOPPING:
Dan Williams78a6f062011-06-30 16:31:37 -0700683 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700684 if (status != SCI_SUCCESS)
685 break;
686
Dan Williams34a99152011-07-01 02:25:15 -0700687 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700688 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams34a99152011-07-01 02:25:15 -0700689 rnc_destruct_done,
690 idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700691 break;
692 }
693
694 if (status != SCI_SUCCESS)
Dan Williams78a6f062011-06-30 16:31:37 -0700695 dev_err(scirdev_to_dev(idev),
Dan Williams10a09e62011-05-01 15:33:43 -0700696 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
Dan Williamsffe191c2011-06-29 13:09:25 -0700697 "could not complete\n", __func__, iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700698 idev, ireq, status);
Dan Williams209fae12011-06-13 17:39:44 -0700699 else
Dan Williams78a6f062011-06-30 16:31:37 -0700700 isci_put_device(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700701
702 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700703}
704
Dan Williams89a73012011-06-30 19:14:33 -0700705static void sci_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700706{
Dan Williams78a6f062011-06-30 16:31:37 -0700707 struct isci_remote_device *idev = dev;
Dan Williams84b9b022011-05-01 15:53:25 -0700708
709 /* we need to check if this request is still valid to continue. */
Dan Williams78a6f062011-06-30 16:31:37 -0700710 if (idev->working_request)
Dan Williams89a73012011-06-30 19:14:33 -0700711 sci_controller_continue_io(idev->working_request);
Dan Williams84b9b022011-05-01 15:53:25 -0700712}
713
Dan Williams89a73012011-06-30 19:14:33 -0700714enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700715 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700716 struct isci_request *ireq)
Dan Williams84b9b022011-05-01 15:53:25 -0700717{
Dan Williams78a6f062011-06-30 16:31:37 -0700718 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700719 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700720 struct isci_port *iport = idev->owning_port;
Dan Williams84b9b022011-05-01 15:53:25 -0700721 enum sci_status status;
722
723 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000724 case SCI_DEV_INITIAL:
725 case SCI_DEV_STOPPED:
726 case SCI_DEV_STARTING:
727 case SCI_SMP_DEV_IDLE:
728 case SCI_SMP_DEV_CMD:
729 case SCI_DEV_STOPPING:
730 case SCI_DEV_FAILED:
731 case SCI_DEV_RESETTING:
732 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700733 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800734 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
735 __func__, dev_state_name(state));
Dan Williams84b9b022011-05-01 15:53:25 -0700736 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000737 case SCI_STP_DEV_IDLE:
738 case SCI_STP_DEV_CMD:
739 case SCI_STP_DEV_NCQ:
740 case SCI_STP_DEV_NCQ_ERROR:
741 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams89a73012011-06-30 19:14:33 -0700742 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700743 if (status != SCI_SUCCESS)
744 return status;
745
Dan Williams89a73012011-06-30 19:14:33 -0700746 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700747 if (status != SCI_SUCCESS)
748 goto out;
749
Dan Williams89a73012011-06-30 19:14:33 -0700750 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700751 if (status != SCI_SUCCESS)
752 goto out;
753
754 /* Note: If the remote device state is not IDLE this will
755 * replace the request that probably resulted in the task
756 * management request.
757 */
Dan Williams78a6f062011-06-30 16:31:37 -0700758 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000759 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700760
761 /* The remote node context must cleanup the TCi to NCQ mapping
762 * table. The only way to do this correctly is to either write
763 * to the TLCR register or to invalidate and repost the RNC. In
764 * either case the remote node context state machine will take
765 * the correct action when the remote node context is suspended
766 * and later resumed.
767 */
Dan Williams89a73012011-06-30 19:14:33 -0700768 sci_remote_node_context_suspend(&idev->rnc,
Dan Williams84b9b022011-05-01 15:53:25 -0700769 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
Dan Williams89a73012011-06-30 19:14:33 -0700770 sci_remote_node_context_resume(&idev->rnc,
771 sci_remote_device_continue_request,
Dan Williams78a6f062011-06-30 16:31:37 -0700772 idev);
Dan Williams84b9b022011-05-01 15:53:25 -0700773
774 out:
Dan Williams89a73012011-06-30 19:14:33 -0700775 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700776 /* We need to let the controller start request handler know that
777 * it can't post TC yet. We will provide a callback function to
778 * post TC when RNC gets resumed.
779 */
780 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000781 case SCI_DEV_READY:
Dan Williams89a73012011-06-30 19:14:33 -0700782 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700783 if (status != SCI_SUCCESS)
784 return status;
785
Dan Williams89a73012011-06-30 19:14:33 -0700786 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700787 if (status != SCI_SUCCESS)
788 break;
789
Dan Williams89a73012011-06-30 19:14:33 -0700790 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700791 break;
792 }
Dan Williams89a73012011-06-30 19:14:33 -0700793 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700794
795 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700796}
797
Dan Williams34a99152011-07-01 02:25:15 -0700798void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
Dan Williams88f3b622011-04-22 19:18:03 -0700799{
Dan Williams34a99152011-07-01 02:25:15 -0700800 struct isci_port *iport = idev->owning_port;
Dan Williams88f3b622011-04-22 19:18:03 -0700801 u32 context;
802
Dan Williams34a99152011-07-01 02:25:15 -0700803 context = request |
804 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
805 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
806 idev->rnc.remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -0700807
Dan Williams34a99152011-07-01 02:25:15 -0700808 sci_controller_post_request(iport->owning_controller, context);
Dan Williams88f3b622011-04-22 19:18:03 -0700809}
810
Dan Williamsab2e8f72011-04-27 16:32:45 -0700811/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700812 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700813 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700814 */
Dan Williamseb229672011-05-01 14:05:57 -0700815static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700816{
Dan Williams78a6f062011-06-30 16:31:37 -0700817 struct isci_remote_device *idev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700818
Dan Williams78a6f062011-06-30 16:31:37 -0700819 if (is_remote_device_ready(idev))
Dan Williamse6225712011-05-01 16:26:09 -0700820 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700821
Dan Williamse6225712011-05-01 16:26:09 -0700822 /* go 'ready' if we are not already in a ready state */
Dan Williams78a6f062011-06-30 16:31:37 -0700823 sci_change_state(&idev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700824}
825
Dan Williams89a73012011-06-30 19:14:33 -0700826static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700827{
Dan Williams78a6f062011-06-30 16:31:37 -0700828 struct isci_remote_device *idev = _dev;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700829 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700830
831 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
832 * As a result, avoid sending the ready notification.
833 */
Dan Williams78a6f062011-06-30 16:31:37 -0700834 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700835 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700836}
837
Dan Williams89a73012011-06-30 19:14:33 -0700838static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700839{
Dan Williams78a6f062011-06-30 16:31:37 -0700840 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700841
Dan Williams88f3b622011-04-22 19:18:03 -0700842 /* Initial state is a transitional state to the stopped state */
Dan Williams78a6f062011-06-30 16:31:37 -0700843 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700844}
845
846/**
Dan Williams89a73012011-06-30 19:14:33 -0700847 * sci_remote_device_destruct() - free remote node context and destruct
Dan Williams88f3b622011-04-22 19:18:03 -0700848 * @remote_device: This parameter specifies the remote device to be destructed.
849 *
850 * Remote device objects are a limited resource. As such, they must be
851 * protected. Thus calls to construct and destruct are mutually exclusive and
852 * non-reentrant. The return value shall indicate if the device was
853 * successfully destructed or if some failure occurred. enum sci_status This value
854 * is returned if the device is successfully destructed.
855 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
856 * device isn't valid (e.g. it's already been destoryed, the handle isn't
857 * valid, etc.).
858 */
Dan Williams89a73012011-06-30 19:14:33 -0700859static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700860{
Dan Williams78a6f062011-06-30 16:31:37 -0700861 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700862 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700863 struct isci_host *ihost;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700864
Edmund Nadolskie3013702011-06-02 00:10:43 +0000865 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800866 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
867 __func__, dev_state_name(state));
Dan Williamsb8d82f62011-05-01 14:38:26 -0700868 return SCI_FAILURE_INVALID_STATE;
869 }
870
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700871 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700872 sci_controller_free_remote_node_context(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700873 idev->rnc.remote_node_index);
874 idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000875 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -0700876
877 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700878}
879
Dan Williams6f231dd2011-07-02 22:56:22 -0700880/**
881 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800882 * @ihost: This parameter specifies the isci host object.
883 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700884 *
885 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800886static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700887{
Dan Williamsd9c37392011-03-03 17:59:32 -0800888 dev_dbg(&ihost->pdev->dev,
889 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700890
891 /* There should not be any outstanding io's. All paths to
892 * here should go through isci_remote_device_nuke_requests.
893 * If we hit this condition, we will need a way to complete
894 * io requests in process */
Dan Williams209fae12011-06-13 17:39:44 -0700895 BUG_ON(!list_empty(&idev->reqs_in_process));
Dan Williams6f231dd2011-07-02 22:56:22 -0700896
Dan Williams89a73012011-06-30 19:14:33 -0700897 sci_remote_device_destruct(idev);
Dan Williamsd9c37392011-03-03 17:59:32 -0800898 list_del_init(&idev->node);
Dan Williams209fae12011-06-13 17:39:44 -0700899 isci_put_device(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700900}
901
Dan Williams89a73012011-06-30 19:14:33 -0700902static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700903{
Dan Williams78a6f062011-06-30 16:31:37 -0700904 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700905 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700906 u32 prev_state;
907
Dan Williams88f3b622011-04-22 19:18:03 -0700908 /* If we are entering from the stopping state let the SCI User know that
909 * the stop operation has completed.
910 */
Dan Williams78a6f062011-06-30 16:31:37 -0700911 prev_state = idev->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000912 if (prev_state == SCI_DEV_STOPPING)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700913 isci_remote_device_deconstruct(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700914
Dan Williams89a73012011-06-30 19:14:33 -0700915 sci_controller_remote_device_stopped(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700916}
917
Dan Williams89a73012011-06-30 19:14:33 -0700918static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700919{
Dan Williams78a6f062011-06-30 16:31:37 -0700920 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700921 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700922
Dan Williams88f3b622011-04-22 19:18:03 -0700923 isci_remote_device_not_ready(ihost, idev,
924 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
925}
926
Dan Williams89a73012011-06-30 19:14:33 -0700927static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700928{
Dan Williams78a6f062011-06-30 16:31:37 -0700929 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700930 struct isci_host *ihost = idev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000931 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700932
Dan Williamsab2e8f72011-04-27 16:32:45 -0700933 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700934 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700935 } else if (dev_is_expander(dev)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700936 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700937 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700938 isci_remote_device_ready(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700939}
940
Dan Williams89a73012011-06-30 19:14:33 -0700941static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700942{
Dan Williams78a6f062011-06-30 16:31:37 -0700943 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
944 struct domain_device *dev = idev->domain_dev;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700945
946 if (dev->dev_type == SAS_END_DEV) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700947 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700948
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700949 isci_remote_device_not_ready(ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700950 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
951 }
952}
953
Dan Williams89a73012011-06-30 19:14:33 -0700954static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700955{
Dan Williams78a6f062011-06-30 16:31:37 -0700956 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700957
Dan Williams89a73012011-06-30 19:14:33 -0700958 sci_remote_node_context_suspend(
Dan Williams78a6f062011-06-30 16:31:37 -0700959 &idev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700960}
961
Dan Williams89a73012011-06-30 19:14:33 -0700962static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700963{
Dan Williams78a6f062011-06-30 16:31:37 -0700964 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700965
Dan Williams89a73012011-06-30 19:14:33 -0700966 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700967}
968
Dan Williams89a73012011-06-30 19:14:33 -0700969static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700970{
Dan Williams78a6f062011-06-30 16:31:37 -0700971 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700972
Dan Williams78a6f062011-06-30 16:31:37 -0700973 idev->working_request = NULL;
Dan Williams89a73012011-06-30 19:14:33 -0700974 if (sci_remote_node_context_is_ready(&idev->rnc)) {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700975 /*
976 * Since the RNC is ready, it's alright to finish completion
977 * processing (e.g. signal the remote device is ready). */
Dan Williams89a73012011-06-30 19:14:33 -0700978 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700979 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700980 sci_remote_node_context_resume(&idev->rnc,
981 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
Dan Williams78a6f062011-06-30 16:31:37 -0700982 idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700983 }
984}
985
Dan Williams89a73012011-06-30 19:14:33 -0700986static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700987{
Dan Williams78a6f062011-06-30 16:31:37 -0700988 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700989 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700990
Dan Williams78a6f062011-06-30 16:31:37 -0700991 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700992
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700993 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700994 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
995}
996
Dan Williams89a73012011-06-30 19:14:33 -0700997static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700998{
Dan Williams78a6f062011-06-30 16:31:37 -0700999 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001000 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001001
Dan Williams78a6f062011-06-30 16:31:37 -07001002 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001003 isci_remote_device_not_ready(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -07001004 idev->not_ready_reason);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001005}
1006
Dan Williams89a73012011-06-30 19:14:33 -07001007static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001008{
Dan Williams78a6f062011-06-30 16:31:37 -07001009 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001010 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001011
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001012 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001013}
1014
Dan Williams89a73012011-06-30 19:14:33 -07001015static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001016{
Dan Williams78a6f062011-06-30 16:31:37 -07001017 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001018 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001019
Dan Williams78a6f062011-06-30 16:31:37 -07001020 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001021
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001022 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001023 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1024}
1025
Dan Williams89a73012011-06-30 19:14:33 -07001026static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001027{
Dan Williams78a6f062011-06-30 16:31:37 -07001028 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001029
Dan Williams78a6f062011-06-30 16:31:37 -07001030 idev->working_request = NULL;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001031}
Dan Williams88f3b622011-04-22 19:18:03 -07001032
Dan Williams89a73012011-06-30 19:14:33 -07001033static const struct sci_base_state sci_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001034 [SCI_DEV_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001035 .enter_state = sci_remote_device_initial_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001036 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001037 [SCI_DEV_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001038 .enter_state = sci_remote_device_stopped_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001039 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001040 [SCI_DEV_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001041 .enter_state = sci_remote_device_starting_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001042 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001043 [SCI_DEV_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001044 .enter_state = sci_remote_device_ready_state_enter,
1045 .exit_state = sci_remote_device_ready_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001046 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001047 [SCI_STP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001048 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001049 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001050 [SCI_STP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001051 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001052 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001053 [SCI_STP_DEV_NCQ] = { },
1054 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williams89a73012011-06-30 19:14:33 -07001055 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001056 },
Dan Williamsb50102d2011-09-30 18:52:19 -07001057 [SCI_STP_DEV_ATAPI_ERROR] = { },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001058 [SCI_STP_DEV_AWAIT_RESET] = { },
1059 [SCI_SMP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001060 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001061 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001062 [SCI_SMP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001063 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1064 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001065 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001066 [SCI_DEV_STOPPING] = { },
1067 [SCI_DEV_FAILED] = { },
1068 [SCI_DEV_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001069 .enter_state = sci_remote_device_resetting_state_enter,
1070 .exit_state = sci_remote_device_resetting_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001071 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001072 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001073};
1074
1075/**
Dan Williams89a73012011-06-30 19:14:33 -07001076 * sci_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001077 * @sci_port: SAS/SATA port through which this device is accessed.
1078 * @sci_dev: remote device to construct
1079 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001080 * This routine just performs benign initialization and does not
1081 * allocate the remote_node_context which is left to
Dan Williams89a73012011-06-30 19:14:33 -07001082 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
Dan Williamsb87ee302011-04-25 11:48:29 -07001083 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001084 */
Dan Williams89a73012011-06-30 19:14:33 -07001085static void sci_remote_device_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001086 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001087{
Dan Williams78a6f062011-06-30 16:31:37 -07001088 idev->owning_port = iport;
1089 idev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001090
Dan Williams89a73012011-06-30 19:14:33 -07001091 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001092
Dan Williams89a73012011-06-30 19:14:33 -07001093 sci_remote_node_context_construct(&idev->rnc,
Dan Williams88f3b622011-04-22 19:18:03 -07001094 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001095}
1096
1097/**
Dan Williams89a73012011-06-30 19:14:33 -07001098 * sci_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001099 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001100 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1101 * the device is known to the SCI Core since it is contained in the
Dan Williams89a73012011-06-30 19:14:33 -07001102 * sci_phy object. Remote node context(s) is/are a global resource
1103 * allocated by this routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001104 *
1105 * Returns:
1106 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1107 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1108 * sata-only controller instance.
1109 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001110 */
Dan Williams89a73012011-06-30 19:14:33 -07001111static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001112 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001113{
1114 enum sci_status status;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001115 struct sci_port_properties properties;
Dan Williams78a6f062011-06-30 16:31:37 -07001116 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001117
Dan Williams89a73012011-06-30 19:14:33 -07001118 sci_remote_device_construct(iport, idev);
Dan Williamsb87ee302011-04-25 11:48:29 -07001119
Dan Williams88f3b622011-04-22 19:18:03 -07001120 /*
1121 * This information is request to determine how many remote node context
1122 * entries will be needed to store the remote node.
1123 */
Dan Williams78a6f062011-06-30 16:31:37 -07001124 idev->is_direct_attached = true;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001125
1126 sci_port_get_properties(iport, &properties);
1127 /* Get accurate port width from port's phy mask for a DA device. */
1128 idev->device_port_width = hweight32(properties.phy_mask);
1129
Dan Williams89a73012011-06-30 19:14:33 -07001130 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001131 idev,
1132 &idev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001133
Dan Williamsa1a113b2011-04-21 18:44:45 -07001134 if (status != SCI_SUCCESS)
1135 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001136
Dan Williamsab2e8f72011-04-27 16:32:45 -07001137 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1138 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1139 /* pass */;
1140 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001141 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001142
Dan Williams89a73012011-06-30 19:14:33 -07001143 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
Dan Williams88f3b622011-04-22 19:18:03 -07001144
Dan Williamsa1a113b2011-04-21 18:44:45 -07001145 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001146}
1147
Dan Williams88f3b622011-04-22 19:18:03 -07001148/**
Dan Williams89a73012011-06-30 19:14:33 -07001149 * sci_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001150 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001151 * Remote node context(s) is/are a global resource allocated by this
Dan Williams89a73012011-06-30 19:14:33 -07001152 * routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001153 *
1154 * Returns:
1155 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1156 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1157 * sata-only controller instance.
1158 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001159 */
Dan Williams89a73012011-06-30 19:14:33 -07001160static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001161 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001162{
Dan Williams78a6f062011-06-30 16:31:37 -07001163 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001164 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001165
Dan Williams89a73012011-06-30 19:14:33 -07001166 sci_remote_device_construct(iport, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001167
Dan Williams89a73012011-06-30 19:14:33 -07001168 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001169 idev,
1170 &idev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001171 if (status != SCI_SUCCESS)
1172 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001173
Dan Williamsab2e8f72011-04-27 16:32:45 -07001174 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1175 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1176 /* pass */;
1177 else
1178 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001179
Dan Williamsa1a113b2011-04-21 18:44:45 -07001180 /*
1181 * For SAS-2 the physical link rate is actually a logical link
1182 * rate that incorporates multiplexing. The SCU doesn't
1183 * incorporate multiplexing and for the purposes of the
1184 * connection the logical link rate is that same as the
1185 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1186 * one another, so this code works for both situations. */
Dan Williams89a73012011-06-30 19:14:33 -07001187 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
Dan Williams00d680e2011-04-25 14:29:29 -07001188 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001189
Dan Williamsa1a113b2011-04-21 18:44:45 -07001190 /* / @todo Should I assign the port width by reading all of the phys on the port? */
Dan Williams78a6f062011-06-30 16:31:37 -07001191 idev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001192
Dan Williamsab2e8f72011-04-27 16:32:45 -07001193 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001194}
1195
1196/**
Dan Williams89a73012011-06-30 19:14:33 -07001197 * sci_remote_device_start() - This method will start the supplied remote
Dan Williams88f3b622011-04-22 19:18:03 -07001198 * device. This method enables normal IO requests to flow through to the
1199 * remote device.
1200 * @remote_device: This parameter specifies the device to be started.
1201 * @timeout: This parameter specifies the number of milliseconds in which the
1202 * start operation should complete.
1203 *
1204 * An indication of whether the device was successfully started. SCI_SUCCESS
1205 * This value is returned if the device was successfully started.
1206 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1207 * the device when there have been no phys added to it.
1208 */
Dan Williams89a73012011-06-30 19:14:33 -07001209static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
Dan Williamseb229672011-05-01 14:05:57 -07001210 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001211{
Dan Williams78a6f062011-06-30 16:31:37 -07001212 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -07001213 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamseb229672011-05-01 14:05:57 -07001214 enum sci_status status;
1215
Edmund Nadolskie3013702011-06-02 00:10:43 +00001216 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001217 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
1218 __func__, dev_state_name(state));
Dan Williamseb229672011-05-01 14:05:57 -07001219 return SCI_FAILURE_INVALID_STATE;
1220 }
1221
Dan Williams89a73012011-06-30 19:14:33 -07001222 status = sci_remote_node_context_resume(&idev->rnc,
Dan Williamseb229672011-05-01 14:05:57 -07001223 remote_device_resume_done,
Dan Williams78a6f062011-06-30 16:31:37 -07001224 idev);
Dan Williamseb229672011-05-01 14:05:57 -07001225 if (status != SCI_SUCCESS)
1226 return status;
1227
Edmund Nadolskie3013702011-06-02 00:10:43 +00001228 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001229
1230 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001231}
Dan Williams6f231dd2011-07-02 22:56:22 -07001232
Dan Williams00d680e2011-04-25 14:29:29 -07001233static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1234 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001235{
Dan Williams00d680e2011-04-25 14:29:29 -07001236 struct isci_host *ihost = iport->isci_host;
1237 struct domain_device *dev = idev->domain_dev;
1238 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001239
Dan Williams00d680e2011-04-25 14:29:29 -07001240 if (dev->parent && dev_is_expander(dev->parent))
Dan Williams89a73012011-06-30 19:14:33 -07001241 status = sci_remote_device_ea_construct(iport, idev);
Dan Williams00d680e2011-04-25 14:29:29 -07001242 else
Dan Williams89a73012011-06-30 19:14:33 -07001243 status = sci_remote_device_da_construct(iport, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001244
1245 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001246 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1247 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001248
1249 return status;
1250 }
1251
Dan Williams6f231dd2011-07-02 22:56:22 -07001252 /* start the device. */
Dan Williams89a73012011-06-30 19:14:33 -07001253 status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001254
Dan Williams00d680e2011-04-25 14:29:29 -07001255 if (status != SCI_SUCCESS)
1256 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1257 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001258
1259 return status;
1260}
1261
Dan Williams4393aa42011-03-31 13:10:44 -07001262void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001263{
1264 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001265
Dan Williams4393aa42011-03-31 13:10:44 -07001266 dev_dbg(&ihost->pdev->dev,
1267 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001268
1269 /* Cleanup all requests pending for this device. */
Dan Williams980d3ae2011-06-20 15:11:22 -07001270 isci_terminate_pending_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001271
Dan Williams4393aa42011-03-31 13:10:44 -07001272 dev_dbg(&ihost->pdev->dev,
1273 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001274}
1275
Dan Williams6f231dd2011-07-02 22:56:22 -07001276/**
1277 * This function builds the isci_remote_device when a libsas dev_found message
1278 * is received.
1279 * @isci_host: This parameter specifies the isci host object.
1280 * @port: This parameter specifies the isci_port conected to this device.
1281 *
1282 * pointer to new isci_remote_device.
1283 */
1284static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001285isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001286{
Dan Williamsd9c37392011-03-03 17:59:32 -08001287 struct isci_remote_device *idev;
1288 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001289
Dan Williamsd9c37392011-03-03 17:59:32 -08001290 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001291 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001292 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1293 break;
1294 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001295
Dan Williamsd9c37392011-03-03 17:59:32 -08001296 if (i >= SCI_MAX_REMOTE_DEVICES) {
1297 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001298 return NULL;
1299 }
1300
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001301 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1302 return NULL;
1303
1304 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1305 return NULL;
1306
Dan Williamsd9c37392011-03-03 17:59:32 -08001307 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001308}
Dan Williams6f231dd2011-07-02 22:56:22 -07001309
Dan Williams209fae12011-06-13 17:39:44 -07001310void isci_remote_device_release(struct kref *kref)
1311{
1312 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1313 struct isci_host *ihost = idev->isci_port->isci_host;
1314
1315 idev->domain_dev = NULL;
1316 idev->isci_port = NULL;
1317 clear_bit(IDEV_START_PENDING, &idev->flags);
1318 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -07001319 clear_bit(IDEV_IO_READY, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001320 clear_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001321 smp_mb__before_clear_bit();
1322 clear_bit(IDEV_ALLOCATED, &idev->flags);
1323 wake_up(&ihost->eventq);
1324}
1325
Dan Williams6f231dd2011-07-02 22:56:22 -07001326/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001327 * isci_remote_device_stop() - This function is called internally to stop the
1328 * remote device.
1329 * @isci_host: This parameter specifies the isci host object.
1330 * @isci_device: This parameter specifies the remote device.
1331 *
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001332 * The status of the ihost request to stop.
Dan Williams6f231dd2011-07-02 22:56:22 -07001333 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001334enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001335{
1336 enum sci_status status;
1337 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001338
Dan Williams6ad31fe2011-03-04 12:10:29 -08001339 dev_dbg(&ihost->pdev->dev,
1340 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001341
Dan Williams209fae12011-06-13 17:39:44 -07001342 spin_lock_irqsave(&ihost->scic_lock, flags);
1343 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1344 set_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001345 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001346
1347 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001348 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001349
Dan Williams6ad31fe2011-03-04 12:10:29 -08001350 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001351
Dan Williams6ad31fe2011-03-04 12:10:29 -08001352 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001353 status = sci_remote_device_stop(idev, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001354 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001355
1356 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001357 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1358 /* nothing to wait for */;
1359 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001360 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001361
Dan Williams6f231dd2011-07-02 22:56:22 -07001362 return status;
1363}
1364
1365/**
1366 * isci_remote_device_gone() - This function is called by libsas when a domain
1367 * device is removed.
1368 * @domain_device: This parameter specifies the libsas domain device.
1369 *
1370 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001371void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001372{
Dan Williams4393aa42011-03-31 13:10:44 -07001373 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001374 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001375
Dan Williams6ad31fe2011-03-04 12:10:29 -08001376 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001377 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001378 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001379
Dan Williams6ad31fe2011-03-04 12:10:29 -08001380 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001381}
1382
1383
1384/**
1385 * isci_remote_device_found() - This function is called by libsas when a remote
1386 * device is discovered. A remote device object is created and started. the
1387 * function then sleeps until the sci core device started message is
1388 * received.
1389 * @domain_device: This parameter specifies the libsas domain device.
1390 *
1391 * status, zero indicates success.
1392 */
Dan Williamsc132f692012-01-03 23:26:08 -08001393int isci_remote_device_found(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001394{
Dan Williamsc132f692012-01-03 23:26:08 -08001395 struct isci_host *isci_host = dev_to_ihost(dev);
1396 struct isci_port *isci_port = dev->port->lldd_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001397 struct isci_remote_device *isci_device;
1398 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001399
Dan Williams6f231dd2011-07-02 22:56:22 -07001400 dev_dbg(&isci_host->pdev->dev,
Dan Williamsc132f692012-01-03 23:26:08 -08001401 "%s: domain_device = %p\n", __func__, dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001402
Dan Williamsc132f692012-01-03 23:26:08 -08001403 if (!isci_port)
1404 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001405
Dan Williams6f231dd2011-07-02 22:56:22 -07001406 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001407 if (!isci_device)
1408 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001409
Dan Williams209fae12011-06-13 17:39:44 -07001410 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001411 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001412
1413 spin_lock_irq(&isci_host->scic_lock);
Dan Williamsc132f692012-01-03 23:26:08 -08001414 isci_device->domain_dev = dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001415 isci_device->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001416 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1417
Dan Williams6ad31fe2011-03-04 12:10:29 -08001418 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001419 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001420
Dan Williams6f231dd2011-07-02 22:56:22 -07001421 dev_dbg(&isci_host->pdev->dev,
1422 "%s: isci_device = %p\n",
1423 __func__, isci_device);
1424
Dan Williams209fae12011-06-13 17:39:44 -07001425 if (status == SCI_SUCCESS) {
1426 /* device came up, advertise it to the world */
Dan Williamsc132f692012-01-03 23:26:08 -08001427 dev->lldd_dev = isci_device;
Dan Williams209fae12011-06-13 17:39:44 -07001428 } else
1429 isci_put_device(isci_device);
1430 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001431
Dan Williams6ad31fe2011-03-04 12:10:29 -08001432 /* wait for the device ready callback. */
1433 wait_for_device_start(isci_host, isci_device);
1434
Dan Williams209fae12011-06-13 17:39:44 -07001435 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001436}