blob: be9f0e0be4fffae3b3044158f14ca2019a17fc75 [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
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -080075static enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
76 enum sci_remote_node_suspension_reasons reason)
Jeff Skirvin23ec2aa2012-03-08 22:41:52 -080077{
Jeff Skirvin447bfbc2012-03-08 22:41:59 -080078 return sci_remote_node_context_suspend(&idev->rnc, reason,
79 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
Jeff Skirvin23ec2aa2012-03-08 22:41:52 -080080}
81
Dan Williamsab2e8f72011-04-27 16:32:45 -070082/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070083 * isci_remote_device_ready() - This function is called by the ihost when the
Dan Williamsab2e8f72011-04-27 16:32:45 -070084 * remote device is ready. We mark the isci device as ready and signal the
85 * waiting proccess.
86 * @ihost: our valid isci_host
87 * @idev: remote device
88 *
89 */
90static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
91{
92 dev_dbg(&ihost->pdev->dev,
93 "%s: idev = %p\n", __func__, idev);
94
Jeff Skirvin9274f452011-06-23 17:09:02 -070095 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -070096 set_bit(IDEV_IO_READY, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -070097 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
98 wake_up(&ihost->eventq);
99}
100
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800101static enum sci_status sci_remote_device_terminate_req(
102 struct isci_host *ihost,
103 struct isci_remote_device *idev,
104 int check_abort,
105 struct isci_request *ireq)
Jeff Skirvin5b6bf222012-03-08 22:41:51 -0800106{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800107 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
108 (ireq->target_device != idev) ||
109 (check_abort && !test_bit(IREQ_PENDING_ABORT, &ireq->flags)))
110 return SCI_SUCCESS;
111
Jeff Skirvin621120c2012-03-08 22:42:03 -0800112 dev_dbg(&ihost->pdev->dev,
113 "%s: idev=%p; flags=%lx; req=%p; req target=%p\n",
114 __func__, idev, idev->flags, ireq, ireq->target_device);
115
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800116 set_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
117
118 return sci_controller_terminate_request(ihost, idev, ireq);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -0800119}
120
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800121static enum sci_status sci_remote_device_terminate_reqs_checkabort(
122 struct isci_remote_device *idev,
123 int chk)
Jeff Skirvin726980d2012-03-08 22:41:50 -0800124{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800125 struct isci_host *ihost = idev->owning_port->owning_controller;
126 enum sci_status status = SCI_SUCCESS;
127 u32 i;
128
129 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
130 struct isci_request *ireq = ihost->reqs[i];
131 enum sci_status s;
132
133 s = sci_remote_device_terminate_req(ihost, idev, chk, ireq);
134 if (s != SCI_SUCCESS)
135 status = s;
136 }
137 return status;
138}
139
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800140static bool isci_compare_suspendcount(
141 struct isci_remote_device *idev,
142 u32 localcount)
143{
144 smp_rmb();
145 return localcount != idev->rnc.suspend_count;
146}
147
Jeff Skirvin033d19d2012-03-08 22:42:03 -0800148static bool isci_check_reqterm(
149 struct isci_host *ihost,
150 struct isci_remote_device *idev,
151 struct isci_request *ireq,
152 u32 localcount)
153{
154 unsigned long flags;
155 bool res;
156
157 spin_lock_irqsave(&ihost->scic_lock, flags);
158 res = isci_compare_suspendcount(idev, localcount)
159 && !test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
160 spin_unlock_irqrestore(&ihost->scic_lock, flags);
161
162 return res;
163}
164
165static bool isci_check_devempty(
166 struct isci_host *ihost,
167 struct isci_remote_device *idev,
168 u32 localcount)
169{
170 unsigned long flags;
171 bool res;
172
173 spin_lock_irqsave(&ihost->scic_lock, flags);
174 res = isci_compare_suspendcount(idev, localcount)
175 && idev->started_request_count == 0;
176 spin_unlock_irqrestore(&ihost->scic_lock, flags);
177
178 return res;
179}
180
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800181enum sci_status isci_remote_device_terminate_requests(
182 struct isci_host *ihost,
183 struct isci_remote_device *idev,
184 struct isci_request *ireq)
185{
186 enum sci_status status = SCI_SUCCESS;
Jeff Skirvin726980d2012-03-08 22:41:50 -0800187 unsigned long flags;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800188 u32 rnc_suspend_count;
Jeff Skirvin726980d2012-03-08 22:41:50 -0800189
190 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800191
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800192 if (isci_get_device(idev) == NULL) {
193 dev_dbg(&ihost->pdev->dev, "%s: failed isci_get_device(idev=%p)\n",
194 __func__, idev);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800195 spin_unlock_irqrestore(&ihost->scic_lock, flags);
196 status = SCI_FAILURE;
197 } else {
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800198 /* If already suspended, don't wait for another suspension. */
199 smp_rmb();
200 rnc_suspend_count
201 = sci_remote_node_context_is_suspended(&idev->rnc)
202 ? 0 : idev->rnc.suspend_count;
203
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800204 dev_dbg(&ihost->pdev->dev,
205 "%s: idev=%p, ireq=%p; started_request_count=%d, "
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800206 "rnc_suspend_count=%d, rnc.suspend_count=%d"
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800207 "about to wait\n",
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800208 __func__, idev, ireq, idev->started_request_count,
209 rnc_suspend_count, idev->rnc.suspend_count);
Jeff Skirvin8c731882012-03-08 22:42:06 -0800210
211 #define MAX_SUSPEND_MSECS 10000
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800212 if (ireq) {
213 /* Terminate a specific TC. */
Jeff Skirvin621120c2012-03-08 22:42:03 -0800214 set_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800215 sci_remote_device_terminate_req(ihost, idev, 0, ireq);
216 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin8c731882012-03-08 22:42:06 -0800217 if (!wait_event_timeout(ihost->eventq,
218 isci_check_reqterm(ihost, idev, ireq,
219 rnc_suspend_count),
220 msecs_to_jiffies(MAX_SUSPEND_MSECS))) {
221
222 dev_warn(&ihost->pdev->dev, "%s host%d timeout single\n",
223 __func__, ihost->id);
224 dev_dbg(&ihost->pdev->dev,
225 "%s: ******* Timeout waiting for "
226 "suspend; idev=%p, current state %s; "
227 "started_request_count=%d, flags=%lx\n\t"
228 "rnc_suspend_count=%d, rnc.suspend_count=%d "
229 "RNC: current state %s, current "
230 "suspend_type %x dest state %d;\n"
231 "ireq=%p, ireq->flags = %lx\n",
232 __func__, idev,
233 dev_state_name(idev->sm.current_state_id),
234 idev->started_request_count, idev->flags,
235 rnc_suspend_count, idev->rnc.suspend_count,
236 rnc_state_name(idev->rnc.sm.current_state_id),
237 idev->rnc.suspend_type,
238 idev->rnc.destination_state,
239 ireq, ireq->flags);
240 }
Jeff Skirvin621120c2012-03-08 22:42:03 -0800241 clear_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags);
242 isci_free_tag(ihost, ireq->io_tag);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800243 } else {
244 /* Terminate all TCs. */
245 sci_remote_device_terminate_requests(idev);
246 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin8c731882012-03-08 22:42:06 -0800247 if (!wait_event_timeout(ihost->eventq,
248 isci_check_devempty(ihost, idev,
249 rnc_suspend_count),
250 msecs_to_jiffies(MAX_SUSPEND_MSECS))) {
251
252 dev_warn(&ihost->pdev->dev, "%s host%d timeout all\n",
253 __func__, ihost->id);
254 dev_dbg(&ihost->pdev->dev,
255 "%s: ******* Timeout waiting for "
256 "suspend; idev=%p, current state %s; "
257 "started_request_count=%d, flags=%lx\n\t"
258 "rnc_suspend_count=%d, "
259 "RNC: current state %s, "
260 "rnc.suspend_count=%d, current "
261 "suspend_type %x dest state %d\n",
262 __func__, idev,
263 dev_state_name(idev->sm.current_state_id),
264 idev->started_request_count, idev->flags,
265 rnc_suspend_count,
266 rnc_state_name(idev->rnc.sm.current_state_id),
267 idev->rnc.suspend_count,
268 idev->rnc.suspend_type,
269 idev->rnc.destination_state);
270 }
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800271 }
272 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, wait done\n",
273 __func__, idev);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800274 isci_put_device(idev);
275 }
276 return status;
277}
278
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800279/**
280* isci_remote_device_not_ready() - This function is called by the ihost when
281* the remote device is not ready. We mark the isci device as ready (not
282* "ready_for_io") and signal the waiting proccess.
283* @isci_host: This parameter specifies the isci host object.
284* @isci_device: This parameter specifies the remote device
285*
286* sci_lock is held on entrance to this function.
287*/
288static void isci_remote_device_not_ready(struct isci_host *ihost,
289 struct isci_remote_device *idev,
290 u32 reason)
291{
292 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin59e35392012-03-08 22:41:54 -0800293 "%s: isci_device = %p; reason = %d\n", __func__, idev, reason);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800294
295 switch (reason) {
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800296 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
297 set_bit(IDEV_IO_NCQERROR, &idev->flags);
298
299 /* Suspend the remote device so the I/O can be terminated. */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800300 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800301
302 /* Kill all outstanding requests for the device. */
303 sci_remote_device_terminate_requests(idev);
304
305 /* Fall through into the default case... */
306 default:
307 clear_bit(IDEV_IO_READY, &idev->flags);
308 break;
309 }
310}
311
Dan Williamsec575662011-05-01 14:19:25 -0700312/* called once the remote node context is ready to be freed.
313 * The remote device can now report that its stop operation is complete. none
314 */
315static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700316{
Dan Williams78a6f062011-06-30 16:31:37 -0700317 struct isci_remote_device *idev = _dev;
Dan Williamsec575662011-05-01 14:19:25 -0700318
Dan Williams78a6f062011-06-30 16:31:37 -0700319 BUG_ON(idev->started_request_count != 0);
320 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700321}
322
Jeff Skirvin726980d2012-03-08 22:41:50 -0800323enum sci_status sci_remote_device_terminate_requests(
324 struct isci_remote_device *idev)
325{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800326 return sci_remote_device_terminate_reqs_checkabort(idev, 0);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800327}
328
Dan Williams89a73012011-06-30 19:14:33 -0700329enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
Dan Williamsec575662011-05-01 14:19:25 -0700330 u32 timeout)
331{
Dan Williams78a6f062011-06-30 16:31:37 -0700332 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700333 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsec575662011-05-01 14:19:25 -0700334
335 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000336 case SCI_DEV_INITIAL:
337 case SCI_DEV_FAILED:
338 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700339 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800340 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
341 __func__, dev_state_name(state));
Dan Williamsec575662011-05-01 14:19:25 -0700342 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000343 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700344 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000345 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700346 /* device not started so there had better be no requests */
Dan Williams78a6f062011-06-30 16:31:37 -0700347 BUG_ON(idev->started_request_count != 0);
Dan Williams89a73012011-06-30 19:14:33 -0700348 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700349 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700350 /* Transition to the stopping state and wait for the
351 * remote node to complete being posted and invalidated.
352 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000353 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700354 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000355 case SCI_DEV_READY:
356 case SCI_STP_DEV_IDLE:
357 case SCI_STP_DEV_CMD:
358 case SCI_STP_DEV_NCQ:
359 case SCI_STP_DEV_NCQ_ERROR:
360 case SCI_STP_DEV_AWAIT_RESET:
361 case SCI_SMP_DEV_IDLE:
362 case SCI_SMP_DEV_CMD:
363 sci_change_state(sm, SCI_DEV_STOPPING);
Jeff Skirvin83884012012-03-08 22:41:52 -0800364 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700365 sci_remote_node_context_destruct(&idev->rnc,
Jeff Skirvin83884012012-03-08 22:41:52 -0800366 rnc_destruct_done,
367 idev);
368 else {
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800369 sci_remote_device_suspend(
370 idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
Jeff Skirvin83884012012-03-08 22:41:52 -0800371 sci_remote_device_terminate_requests(idev);
372 }
373 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000374 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700375 /* All requests should have been terminated, but if there is an
376 * attempt to stop a device already in the stopping state, then
377 * try again to terminate.
378 */
Dan Williams89a73012011-06-30 19:14:33 -0700379 return sci_remote_device_terminate_requests(idev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000380 case SCI_DEV_RESETTING:
381 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700382 return SCI_SUCCESS;
383 }
Dan Williams88f3b622011-04-22 19:18:03 -0700384}
Dan Williams6f231dd2011-07-02 22:56:22 -0700385
Dan Williams89a73012011-06-30 19:14:33 -0700386enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700387{
Dan Williams78a6f062011-06-30 16:31:37 -0700388 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700389 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700390
391 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000392 case SCI_DEV_INITIAL:
393 case SCI_DEV_STOPPED:
394 case SCI_DEV_STARTING:
395 case SCI_SMP_DEV_IDLE:
396 case SCI_SMP_DEV_CMD:
397 case SCI_DEV_STOPPING:
398 case SCI_DEV_FAILED:
399 case SCI_DEV_RESETTING:
400 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700401 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800402 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
403 __func__, dev_state_name(state));
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700404 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000405 case SCI_DEV_READY:
406 case SCI_STP_DEV_IDLE:
407 case SCI_STP_DEV_CMD:
408 case SCI_STP_DEV_NCQ:
409 case SCI_STP_DEV_NCQ_ERROR:
410 case SCI_STP_DEV_AWAIT_RESET:
411 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700412 return SCI_SUCCESS;
413 }
Dan Williams88f3b622011-04-22 19:18:03 -0700414}
415
Dan Williams89a73012011-06-30 19:14:33 -0700416enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700417{
Dan Williams78a6f062011-06-30 16:31:37 -0700418 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700419 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700420
Edmund Nadolskie3013702011-06-02 00:10:43 +0000421 if (state != SCI_DEV_RESETTING) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800422 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
423 __func__, dev_state_name(state));
Dan Williams81515182011-05-01 14:53:00 -0700424 return SCI_FAILURE_INVALID_STATE;
425 }
426
Edmund Nadolskie3013702011-06-02 00:10:43 +0000427 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700428 return SCI_SUCCESS;
429}
Dan Williams88f3b622011-04-22 19:18:03 -0700430
Dan Williams89a73012011-06-30 19:14:33 -0700431enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
Dan Williams01bec772011-05-01 16:51:11 -0700432 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700433{
Dan Williams78a6f062011-06-30 16:31:37 -0700434 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700435 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700436 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams01bec772011-05-01 16:51:11 -0700437 enum sci_status status;
438
439 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000440 case SCI_DEV_INITIAL:
441 case SCI_DEV_STOPPED:
442 case SCI_DEV_STARTING:
443 case SCI_STP_DEV_IDLE:
444 case SCI_SMP_DEV_IDLE:
445 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700446 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800447 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
448 __func__, dev_state_name(state));
Dan Williams01bec772011-05-01 16:51:11 -0700449 /* Return the frame back to the controller */
Dan Williams89a73012011-06-30 19:14:33 -0700450 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700451 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000452 case SCI_DEV_READY:
453 case SCI_STP_DEV_NCQ_ERROR:
454 case SCI_STP_DEV_AWAIT_RESET:
455 case SCI_DEV_STOPPING:
456 case SCI_DEV_FAILED:
457 case SCI_DEV_RESETTING: {
Dan Williams5076a1a2011-06-27 14:57:03 -0700458 struct isci_request *ireq;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700459 struct ssp_frame_hdr hdr;
460 void *frame_header;
461 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700462
Dan Williams89a73012011-06-30 19:14:33 -0700463 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700464 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700465 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700466 if (status != SCI_SUCCESS)
467 return status;
468
Dave Jiang2d9c2242011-05-04 18:45:05 -0700469 word_cnt = sizeof(hdr) / sizeof(u32);
470 sci_swab32_cpy(&hdr, frame_header, word_cnt);
471
Dan Williams89a73012011-06-30 19:14:33 -0700472 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
Dan Williams78a6f062011-06-30 16:31:37 -0700473 if (ireq && ireq->target_device == idev) {
Dan Williams01bec772011-05-01 16:51:11 -0700474 /* The IO request is now in charge of releasing the frame */
Dan Williams89a73012011-06-30 19:14:33 -0700475 status = sci_io_request_frame_handler(ireq, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700476 } else {
477 /* We could not map this tag to a valid IO
478 * request Just toss the frame and continue
479 */
Dan Williams89a73012011-06-30 19:14:33 -0700480 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700481 }
482 break;
483 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000484 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700485 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700486
Dan Williams89a73012011-06-30 19:14:33 -0700487 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700488 frame_index,
489 (void **)&hdr);
490 if (status != SCI_SUCCESS)
491 return status;
492
Dave Jiange76d6182011-05-04 15:02:03 -0700493 if (hdr->fis_type == FIS_SETDEVBITS &&
494 (hdr->status & ATA_ERR)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700495 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Dan Williams01bec772011-05-01 16:51:11 -0700496
497 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000498 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700499 } else if (hdr->fis_type == FIS_REGD2H &&
500 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700501 /*
502 * Some devices return D2H FIS when an NCQ error is detected.
503 * Treat this like an SDB error FIS ready reason.
504 */
Dan Williams78a6f062011-06-30 16:31:37 -0700505 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
506 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700507 } else
508 status = SCI_FAILURE;
509
Dan Williams89a73012011-06-30 19:14:33 -0700510 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700511 break;
512 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000513 case SCI_STP_DEV_CMD:
514 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700515 /* The device does not process any UF received from the hardware while
516 * in this state. All unsolicited frames are forwarded to the io request
517 * object.
518 */
Dan Williams89a73012011-06-30 19:14:33 -0700519 status = sci_io_request_frame_handler(idev->working_request, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700520 break;
521 }
522
523 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700524}
525
Dan Williams78a6f062011-06-30 16:31:37 -0700526static bool is_remote_device_ready(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700527{
Dan Williamse6225712011-05-01 16:26:09 -0700528
Dan Williams78a6f062011-06-30 16:31:37 -0700529 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700530 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700531
532 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000533 case SCI_DEV_READY:
534 case SCI_STP_DEV_IDLE:
535 case SCI_STP_DEV_CMD:
536 case SCI_STP_DEV_NCQ:
537 case SCI_STP_DEV_NCQ_ERROR:
538 case SCI_STP_DEV_AWAIT_RESET:
539 case SCI_SMP_DEV_IDLE:
540 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700541 return true;
542 default:
543 return false;
544 }
545}
546
Dan Williamsb50102d2011-09-30 18:52:19 -0700547/*
548 * called once the remote node context has transisitioned to a ready
549 * state (after suspending RX and/or TX due to early D2H fis)
550 */
551static void atapi_remote_device_resume_done(void *_dev)
552{
553 struct isci_remote_device *idev = _dev;
554 struct isci_request *ireq = idev->working_request;
555
556 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
557}
558
Dan Williams89a73012011-06-30 19:14:33 -0700559enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
Dan Williamse6225712011-05-01 16:26:09 -0700560 u32 event_code)
561{
Dan Williamse6225712011-05-01 16:26:09 -0700562 enum sci_status status;
563
564 switch (scu_get_event_type(event_code)) {
565 case SCU_EVENT_TYPE_RNC_OPS_MISC:
566 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
567 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
Dan Williams89a73012011-06-30 19:14:33 -0700568 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
Dan Williamse6225712011-05-01 16:26:09 -0700569 break;
570 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
571 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
572 status = SCI_SUCCESS;
573
574 /* Suspend the associated RNC */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800575 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
Dan Williamse6225712011-05-01 16:26:09 -0700576
Dan Williams78a6f062011-06-30 16:31:37 -0700577 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700578 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700579 __func__, idev, event_code,
580 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700581 ? "I_T_Nexus_Timeout event"
582 : "I_T_Nexus_Timeout event in wrong state");
583
584 break;
585 }
586 /* Else, fall through and treat as unhandled... */
587 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700588 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700589 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700590 __func__, idev, event_code,
591 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700592 ? "unexpected event"
593 : "unexpected event in wrong state");
594 status = SCI_FAILURE_INVALID_STATE;
595 break;
596 }
597
598 if (status != SCI_SUCCESS)
599 return status;
600
Dan Williamse6225712011-05-01 16:26:09 -0700601 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700602}
603
Dan Williams89a73012011-06-30 19:14:33 -0700604static void sci_remote_device_start_request(struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700605 struct isci_request *ireq,
Dan Williams18606552011-05-01 14:57:11 -0700606 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700607{
Dan Williams78a6f062011-06-30 16:31:37 -0700608 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700609
610 /* cleanup requests that failed after starting on the port */
611 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -0700612 sci_port_complete_io(iport, idev, ireq);
Dan Williams209fae12011-06-13 17:39:44 -0700613 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700614 kref_get(&idev->kref);
Dan Williams34a99152011-07-01 02:25:15 -0700615 idev->started_request_count++;
Dan Williams209fae12011-06-13 17:39:44 -0700616 }
Dan Williams18606552011-05-01 14:57:11 -0700617}
618
Dan Williams89a73012011-06-30 19:14:33 -0700619enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700620 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700621 struct isci_request *ireq)
Dan Williams18606552011-05-01 14:57:11 -0700622{
Dan Williams78a6f062011-06-30 16:31:37 -0700623 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700624 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700625 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700626 enum sci_status status;
627
628 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000629 case SCI_DEV_INITIAL:
630 case SCI_DEV_STOPPED:
631 case SCI_DEV_STARTING:
632 case SCI_STP_DEV_NCQ_ERROR:
633 case SCI_DEV_STOPPING:
634 case SCI_DEV_FAILED:
635 case SCI_DEV_RESETTING:
636 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700637 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800638 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
639 __func__, dev_state_name(state));
Dan Williams18606552011-05-01 14:57:11 -0700640 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000641 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700642 /* attempt to start an io request for this device object. The remote
643 * device object will issue the start request for the io and if
644 * successful it will start the request for the port object then
645 * increment its own request count.
646 */
Dan Williams89a73012011-06-30 19:14:33 -0700647 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700648 if (status != SCI_SUCCESS)
649 return status;
650
Dan Williams89a73012011-06-30 19:14:33 -0700651 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700652 if (status != SCI_SUCCESS)
653 break;
654
Dan Williams89a73012011-06-30 19:14:33 -0700655 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700656 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000657 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700658 /* handle the start io operation for a sata device that is in
659 * the command idle state. - Evalute the type of IO request to
660 * be started - If its an NCQ request change to NCQ substate -
661 * If its any other command change to the CMD substate
662 *
663 * If this is a softreset we may want to have a different
664 * substate.
665 */
Dan Williams89a73012011-06-30 19:14:33 -0700666 enum sci_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700667 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700668
Dan Williams89a73012011-06-30 19:14:33 -0700669 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700670 if (status != SCI_SUCCESS)
671 return status;
672
Dan Williams89a73012011-06-30 19:14:33 -0700673 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700674 if (status != SCI_SUCCESS)
675 break;
676
Dan Williams89a73012011-06-30 19:14:33 -0700677 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700678 if (status != SCI_SUCCESS)
679 break;
680
Dave Jiange76d6182011-05-04 15:02:03 -0700681 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000682 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700683 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700684 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000685 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700686 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000687 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700688 break;
689 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000690 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700691 struct sas_task *task = isci_request_access_task(ireq);
692
693 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700694 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700695 if (status != SCI_SUCCESS)
696 return status;
697
Dan Williams89a73012011-06-30 19:14:33 -0700698 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700699 if (status != SCI_SUCCESS)
700 break;
701
Dan Williams89a73012011-06-30 19:14:33 -0700702 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700703 } else
704 return SCI_FAILURE_INVALID_STATE;
705 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700706 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000707 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700708 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000709 case SCI_SMP_DEV_IDLE:
Dan Williams89a73012011-06-30 19:14:33 -0700710 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700711 if (status != SCI_SUCCESS)
712 return status;
713
Dan Williams89a73012011-06-30 19:14:33 -0700714 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700715 if (status != SCI_SUCCESS)
716 break;
717
Dan Williams89a73012011-06-30 19:14:33 -0700718 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700719 if (status != SCI_SUCCESS)
720 break;
721
Dan Williams78a6f062011-06-30 16:31:37 -0700722 idev->working_request = ireq;
723 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700724 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000725 case SCI_STP_DEV_CMD:
726 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700727 /* device is already handling a command it can not accept new commands
728 * until this one is complete.
729 */
730 return SCI_FAILURE_INVALID_STATE;
731 }
732
Dan Williams89a73012011-06-30 19:14:33 -0700733 sci_remote_device_start_request(idev, ireq, status);
Dan Williams18606552011-05-01 14:57:11 -0700734 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700735}
736
Dan Williamsffe191c2011-06-29 13:09:25 -0700737static enum sci_status common_complete_io(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700738 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700739 struct isci_request *ireq)
Dan Williams88f3b622011-04-22 19:18:03 -0700740{
Dan Williams10a09e62011-05-01 15:33:43 -0700741 enum sci_status status;
742
Dan Williams89a73012011-06-30 19:14:33 -0700743 status = sci_request_complete(ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700744 if (status != SCI_SUCCESS)
745 return status;
746
Dan Williams89a73012011-06-30 19:14:33 -0700747 status = sci_port_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700748 if (status != SCI_SUCCESS)
749 return status;
750
Dan Williams89a73012011-06-30 19:14:33 -0700751 sci_remote_device_decrement_request_count(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700752 return status;
753}
754
Dan Williams89a73012011-06-30 19:14:33 -0700755enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700756 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700757 struct isci_request *ireq)
Dan Williams10a09e62011-05-01 15:33:43 -0700758{
Dan Williams78a6f062011-06-30 16:31:37 -0700759 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700760 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700761 struct isci_port *iport = idev->owning_port;
Dan Williams10a09e62011-05-01 15:33:43 -0700762 enum sci_status status;
763
764 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000765 case SCI_DEV_INITIAL:
766 case SCI_DEV_STOPPED:
767 case SCI_DEV_STARTING:
768 case SCI_STP_DEV_IDLE:
769 case SCI_SMP_DEV_IDLE:
770 case SCI_DEV_FAILED:
771 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700772 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800773 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
774 __func__, dev_state_name(state));
Dan Williams10a09e62011-05-01 15:33:43 -0700775 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000776 case SCI_DEV_READY:
777 case SCI_STP_DEV_AWAIT_RESET:
778 case SCI_DEV_RESETTING:
Dan Williams78a6f062011-06-30 16:31:37 -0700779 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700780 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000781 case SCI_STP_DEV_CMD:
782 case SCI_STP_DEV_NCQ:
783 case SCI_STP_DEV_NCQ_ERROR:
Dan Williamsb50102d2011-09-30 18:52:19 -0700784 case SCI_STP_DEV_ATAPI_ERROR:
Dan Williams78a6f062011-06-30 16:31:37 -0700785 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700786 if (status != SCI_SUCCESS)
787 break;
788
Dan Williams5076a1a2011-06-27 14:57:03 -0700789 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams10a09e62011-05-01 15:33:43 -0700790 /* This request causes hardware error, device needs to be Lun Reset.
791 * So here we force the state machine to IDLE state so the rest IOs
792 * can reach RNC state handler, these IOs will be completed by RNC with
793 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
794 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000795 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams34a99152011-07-01 02:25:15 -0700796 } else if (idev->started_request_count == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000797 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700798 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000799 case SCI_SMP_DEV_CMD:
Dan Williams78a6f062011-06-30 16:31:37 -0700800 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700801 if (status != SCI_SUCCESS)
802 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000803 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700804 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000805 case SCI_DEV_STOPPING:
Dan Williams78a6f062011-06-30 16:31:37 -0700806 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700807 if (status != SCI_SUCCESS)
808 break;
809
Dan Williams34a99152011-07-01 02:25:15 -0700810 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700811 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams34a99152011-07-01 02:25:15 -0700812 rnc_destruct_done,
813 idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700814 break;
815 }
816
817 if (status != SCI_SUCCESS)
Dan Williams78a6f062011-06-30 16:31:37 -0700818 dev_err(scirdev_to_dev(idev),
Dan Williams10a09e62011-05-01 15:33:43 -0700819 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
Dan Williamsffe191c2011-06-29 13:09:25 -0700820 "could not complete\n", __func__, iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700821 idev, ireq, status);
Dan Williams209fae12011-06-13 17:39:44 -0700822 else
Dan Williams78a6f062011-06-30 16:31:37 -0700823 isci_put_device(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700824
825 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700826}
827
Dan Williams89a73012011-06-30 19:14:33 -0700828static void sci_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700829{
Dan Williams78a6f062011-06-30 16:31:37 -0700830 struct isci_remote_device *idev = dev;
Dan Williams84b9b022011-05-01 15:53:25 -0700831
832 /* we need to check if this request is still valid to continue. */
Dan Williams78a6f062011-06-30 16:31:37 -0700833 if (idev->working_request)
Dan Williams89a73012011-06-30 19:14:33 -0700834 sci_controller_continue_io(idev->working_request);
Dan Williams84b9b022011-05-01 15:53:25 -0700835}
836
Dan Williams89a73012011-06-30 19:14:33 -0700837enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700838 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700839 struct isci_request *ireq)
Dan Williams84b9b022011-05-01 15:53:25 -0700840{
Dan Williams78a6f062011-06-30 16:31:37 -0700841 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700842 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700843 struct isci_port *iport = idev->owning_port;
Dan Williams84b9b022011-05-01 15:53:25 -0700844 enum sci_status status;
845
846 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000847 case SCI_DEV_INITIAL:
848 case SCI_DEV_STOPPED:
849 case SCI_DEV_STARTING:
850 case SCI_SMP_DEV_IDLE:
851 case SCI_SMP_DEV_CMD:
852 case SCI_DEV_STOPPING:
853 case SCI_DEV_FAILED:
854 case SCI_DEV_RESETTING:
855 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700856 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800857 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
858 __func__, dev_state_name(state));
Dan Williams84b9b022011-05-01 15:53:25 -0700859 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000860 case SCI_STP_DEV_IDLE:
861 case SCI_STP_DEV_CMD:
862 case SCI_STP_DEV_NCQ:
863 case SCI_STP_DEV_NCQ_ERROR:
864 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams89a73012011-06-30 19:14:33 -0700865 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700866 if (status != SCI_SUCCESS)
867 return status;
868
Dan Williams89a73012011-06-30 19:14:33 -0700869 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700870 if (status != SCI_SUCCESS)
871 goto out;
872
873 /* Note: If the remote device state is not IDLE this will
874 * replace the request that probably resulted in the task
875 * management request.
876 */
Dan Williams78a6f062011-06-30 16:31:37 -0700877 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000878 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700879
880 /* The remote node context must cleanup the TCi to NCQ mapping
881 * table. The only way to do this correctly is to either write
882 * to the TLCR register or to invalidate and repost the RNC. In
883 * either case the remote node context state machine will take
884 * the correct action when the remote node context is suspended
885 * and later resumed.
886 */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800887 sci_remote_device_suspend(idev,
888 SCI_SW_SUSPEND_LINKHANG_DETECT);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800889
890 status = sci_remote_node_context_start_task(&idev->rnc, ireq,
891 sci_remote_device_continue_request, idev);
Dan Williams84b9b022011-05-01 15:53:25 -0700892
893 out:
Dan Williams89a73012011-06-30 19:14:33 -0700894 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700895 /* We need to let the controller start request handler know that
896 * it can't post TC yet. We will provide a callback function to
897 * post TC when RNC gets resumed.
898 */
899 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000900 case SCI_DEV_READY:
Dan Williams89a73012011-06-30 19:14:33 -0700901 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700902 if (status != SCI_SUCCESS)
903 return status;
904
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800905 /* Resume the RNC as needed: */
906 status = sci_remote_node_context_start_task(&idev->rnc, ireq,
907 NULL, NULL);
Dan Williams84b9b022011-05-01 15:53:25 -0700908 if (status != SCI_SUCCESS)
909 break;
910
Dan Williams89a73012011-06-30 19:14:33 -0700911 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700912 break;
913 }
Dan Williams89a73012011-06-30 19:14:33 -0700914 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700915
916 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700917}
918
Dan Williams34a99152011-07-01 02:25:15 -0700919void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
Dan Williams88f3b622011-04-22 19:18:03 -0700920{
Dan Williams34a99152011-07-01 02:25:15 -0700921 struct isci_port *iport = idev->owning_port;
Dan Williams88f3b622011-04-22 19:18:03 -0700922 u32 context;
923
Dan Williams34a99152011-07-01 02:25:15 -0700924 context = request |
925 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
926 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
927 idev->rnc.remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -0700928
Dan Williams34a99152011-07-01 02:25:15 -0700929 sci_controller_post_request(iport->owning_controller, context);
Dan Williams88f3b622011-04-22 19:18:03 -0700930}
931
Dan Williamsab2e8f72011-04-27 16:32:45 -0700932/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700933 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700934 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700935 */
Dan Williamseb229672011-05-01 14:05:57 -0700936static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700937{
Dan Williams78a6f062011-06-30 16:31:37 -0700938 struct isci_remote_device *idev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700939
Dan Williams78a6f062011-06-30 16:31:37 -0700940 if (is_remote_device_ready(idev))
Dan Williamse6225712011-05-01 16:26:09 -0700941 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700942
Dan Williamse6225712011-05-01 16:26:09 -0700943 /* go 'ready' if we are not already in a ready state */
Dan Williams78a6f062011-06-30 16:31:37 -0700944 sci_change_state(&idev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700945}
946
Dan Williams89a73012011-06-30 19:14:33 -0700947static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700948{
Dan Williams78a6f062011-06-30 16:31:37 -0700949 struct isci_remote_device *idev = _dev;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700950 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700951
952 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
953 * As a result, avoid sending the ready notification.
954 */
Dan Williams78a6f062011-06-30 16:31:37 -0700955 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700956 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700957}
958
Dan Williams89a73012011-06-30 19:14:33 -0700959static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700960{
Dan Williams78a6f062011-06-30 16:31:37 -0700961 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700962
Dan Williams88f3b622011-04-22 19:18:03 -0700963 /* Initial state is a transitional state to the stopped state */
Dan Williams78a6f062011-06-30 16:31:37 -0700964 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700965}
966
967/**
Dan Williams89a73012011-06-30 19:14:33 -0700968 * sci_remote_device_destruct() - free remote node context and destruct
Dan Williams88f3b622011-04-22 19:18:03 -0700969 * @remote_device: This parameter specifies the remote device to be destructed.
970 *
971 * Remote device objects are a limited resource. As such, they must be
972 * protected. Thus calls to construct and destruct are mutually exclusive and
973 * non-reentrant. The return value shall indicate if the device was
974 * successfully destructed or if some failure occurred. enum sci_status This value
975 * is returned if the device is successfully destructed.
976 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
977 * device isn't valid (e.g. it's already been destoryed, the handle isn't
978 * valid, etc.).
979 */
Dan Williams89a73012011-06-30 19:14:33 -0700980static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700981{
Dan Williams78a6f062011-06-30 16:31:37 -0700982 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700983 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700984 struct isci_host *ihost;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700985
Edmund Nadolskie3013702011-06-02 00:10:43 +0000986 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800987 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
988 __func__, dev_state_name(state));
Dan Williamsb8d82f62011-05-01 14:38:26 -0700989 return SCI_FAILURE_INVALID_STATE;
990 }
991
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700992 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700993 sci_controller_free_remote_node_context(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700994 idev->rnc.remote_node_index);
995 idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000996 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -0700997
998 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700999}
1000
Dan Williams6f231dd2011-07-02 22:56:22 -07001001/**
1002 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001003 * @ihost: This parameter specifies the isci host object.
1004 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001005 *
1006 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001007static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001008{
Dan Williamsd9c37392011-03-03 17:59:32 -08001009 dev_dbg(&ihost->pdev->dev,
1010 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001011
1012 /* There should not be any outstanding io's. All paths to
1013 * here should go through isci_remote_device_nuke_requests.
1014 * If we hit this condition, we will need a way to complete
1015 * io requests in process */
Jeff Skirvind6b2a0e2012-03-08 22:41:57 -08001016 BUG_ON(idev->started_request_count > 0);
Dan Williams6f231dd2011-07-02 22:56:22 -07001017
Dan Williams89a73012011-06-30 19:14:33 -07001018 sci_remote_device_destruct(idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001019 list_del_init(&idev->node);
Dan Williams209fae12011-06-13 17:39:44 -07001020 isci_put_device(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001021}
1022
Dan Williams89a73012011-06-30 19:14:33 -07001023static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001024{
Dan Williams78a6f062011-06-30 16:31:37 -07001025 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001026 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -07001027 u32 prev_state;
1028
Dan Williams88f3b622011-04-22 19:18:03 -07001029 /* If we are entering from the stopping state let the SCI User know that
1030 * the stop operation has completed.
1031 */
Dan Williams78a6f062011-06-30 16:31:37 -07001032 prev_state = idev->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001033 if (prev_state == SCI_DEV_STOPPING)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001034 isci_remote_device_deconstruct(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001035
Dan Williams89a73012011-06-30 19:14:33 -07001036 sci_controller_remote_device_stopped(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001037}
1038
Dan Williams89a73012011-06-30 19:14:33 -07001039static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001040{
Dan Williams78a6f062011-06-30 16:31:37 -07001041 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001042 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -07001043
Dan Williams88f3b622011-04-22 19:18:03 -07001044 isci_remote_device_not_ready(ihost, idev,
1045 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1046}
1047
Dan Williams89a73012011-06-30 19:14:33 -07001048static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001049{
Dan Williams78a6f062011-06-30 16:31:37 -07001050 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001051 struct isci_host *ihost = idev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001052 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001053
Dan Williamsab2e8f72011-04-27 16:32:45 -07001054 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Dan Williams78a6f062011-06-30 16:31:37 -07001055 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001056 } else if (dev_is_expander(dev)) {
Dan Williams78a6f062011-06-30 16:31:37 -07001057 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001058 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001059 isci_remote_device_ready(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001060}
1061
Dan Williams89a73012011-06-30 19:14:33 -07001062static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001063{
Dan Williams78a6f062011-06-30 16:31:37 -07001064 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1065 struct domain_device *dev = idev->domain_dev;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001066
1067 if (dev->dev_type == SAS_END_DEV) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001068 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -07001069
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001070 isci_remote_device_not_ready(ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001071 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1072 }
1073}
1074
Dan Williams89a73012011-06-30 19:14:33 -07001075static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001076{
Dan Williams78a6f062011-06-30 16:31:37 -07001077 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001078 struct isci_host *ihost = idev->owning_port->owning_controller;
1079
1080 dev_dbg(&ihost->pdev->dev,
1081 "%s: isci_device = %p\n", __func__, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001082
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -08001083 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
Dan Williams88f3b622011-04-22 19:18:03 -07001084}
1085
Dan Williams89a73012011-06-30 19:14:33 -07001086static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001087{
Dan Williams78a6f062011-06-30 16:31:37 -07001088 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001089 struct isci_host *ihost = idev->owning_port->owning_controller;
1090
1091 dev_dbg(&ihost->pdev->dev,
1092 "%s: isci_device = %p\n", __func__, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001093
Dan Williams89a73012011-06-30 19:14:33 -07001094 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -07001095}
1096
Dan Williams89a73012011-06-30 19:14:33 -07001097static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001098{
Dan Williams78a6f062011-06-30 16:31:37 -07001099 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001100
Dan Williams78a6f062011-06-30 16:31:37 -07001101 idev->working_request = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001102 if (sci_remote_node_context_is_ready(&idev->rnc)) {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001103 /*
1104 * Since the RNC is ready, it's alright to finish completion
1105 * processing (e.g. signal the remote device is ready). */
Dan Williams89a73012011-06-30 19:14:33 -07001106 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001107 } else {
Dan Williams89a73012011-06-30 19:14:33 -07001108 sci_remote_node_context_resume(&idev->rnc,
1109 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
Dan Williams78a6f062011-06-30 16:31:37 -07001110 idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001111 }
1112}
1113
Dan Williams89a73012011-06-30 19:14:33 -07001114static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001115{
Dan Williams78a6f062011-06-30 16:31:37 -07001116 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001117 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001118
Dan Williams78a6f062011-06-30 16:31:37 -07001119 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001120
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001121 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001122 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1123}
1124
Dan Williams89a73012011-06-30 19:14:33 -07001125static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001126{
Dan Williams78a6f062011-06-30 16:31:37 -07001127 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001128 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001129
Dan Williams78a6f062011-06-30 16:31:37 -07001130 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001131 isci_remote_device_not_ready(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -07001132 idev->not_ready_reason);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001133}
1134
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001135static void sci_stp_remote_device_atapi_error_substate_enter(
1136 struct sci_base_state_machine *sm)
1137{
1138 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1139
1140 /* This state is entered when an I/O is decoded with an error
1141 * condition. By this point the RNC expected suspension state is set.
1142 * The error conditions suspend the device, so unsuspend here if
1143 * possible.
1144 */
1145 sci_remote_node_context_resume(&idev->rnc,
1146 atapi_remote_device_resume_done,
1147 idev);
1148}
1149
Dan Williams89a73012011-06-30 19:14:33 -07001150static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001151{
Dan Williams78a6f062011-06-30 16:31:37 -07001152 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001153 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001154
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001155 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001156}
1157
Dan Williams89a73012011-06-30 19:14:33 -07001158static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001159{
Dan Williams78a6f062011-06-30 16:31:37 -07001160 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001161 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001162
Dan Williams78a6f062011-06-30 16:31:37 -07001163 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001165 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001166 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1167}
1168
Dan Williams89a73012011-06-30 19:14:33 -07001169static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001170{
Dan Williams78a6f062011-06-30 16:31:37 -07001171 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001172
Dan Williams78a6f062011-06-30 16:31:37 -07001173 idev->working_request = NULL;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001174}
Dan Williams88f3b622011-04-22 19:18:03 -07001175
Dan Williams89a73012011-06-30 19:14:33 -07001176static const struct sci_base_state sci_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001177 [SCI_DEV_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001178 .enter_state = sci_remote_device_initial_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001179 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001180 [SCI_DEV_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001181 .enter_state = sci_remote_device_stopped_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001182 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001183 [SCI_DEV_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001184 .enter_state = sci_remote_device_starting_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001185 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001186 [SCI_DEV_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001187 .enter_state = sci_remote_device_ready_state_enter,
1188 .exit_state = sci_remote_device_ready_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001189 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001190 [SCI_STP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001191 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001192 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001193 [SCI_STP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001194 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001195 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001196 [SCI_STP_DEV_NCQ] = { },
1197 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williams89a73012011-06-30 19:14:33 -07001198 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001199 },
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001200 [SCI_STP_DEV_ATAPI_ERROR] = {
1201 .enter_state = sci_stp_remote_device_atapi_error_substate_enter,
1202 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001203 [SCI_STP_DEV_AWAIT_RESET] = { },
1204 [SCI_SMP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001205 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001206 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001207 [SCI_SMP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001208 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1209 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001210 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001211 [SCI_DEV_STOPPING] = { },
1212 [SCI_DEV_FAILED] = { },
1213 [SCI_DEV_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001214 .enter_state = sci_remote_device_resetting_state_enter,
1215 .exit_state = sci_remote_device_resetting_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001216 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001217 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001218};
1219
1220/**
Dan Williams89a73012011-06-30 19:14:33 -07001221 * sci_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001222 * @sci_port: SAS/SATA port through which this device is accessed.
1223 * @sci_dev: remote device to construct
1224 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001225 * This routine just performs benign initialization and does not
1226 * allocate the remote_node_context which is left to
Dan Williams89a73012011-06-30 19:14:33 -07001227 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
Dan Williamsb87ee302011-04-25 11:48:29 -07001228 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001229 */
Dan Williams89a73012011-06-30 19:14:33 -07001230static void sci_remote_device_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001231 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001232{
Dan Williams78a6f062011-06-30 16:31:37 -07001233 idev->owning_port = iport;
1234 idev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001235
Dan Williams89a73012011-06-30 19:14:33 -07001236 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001237
Dan Williams89a73012011-06-30 19:14:33 -07001238 sci_remote_node_context_construct(&idev->rnc,
Dan Williams88f3b622011-04-22 19:18:03 -07001239 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001240}
1241
1242/**
Dan Williams89a73012011-06-30 19:14:33 -07001243 * sci_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001244 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001245 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1246 * the device is known to the SCI Core since it is contained in the
Dan Williams89a73012011-06-30 19:14:33 -07001247 * sci_phy object. Remote node context(s) is/are a global resource
1248 * allocated by this routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001249 *
1250 * Returns:
1251 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1252 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1253 * sata-only controller instance.
1254 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001255 */
Dan Williams89a73012011-06-30 19:14:33 -07001256static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001257 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001258{
1259 enum sci_status status;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001260 struct sci_port_properties properties;
Dan Williams88f3b622011-04-22 19:18:03 -07001261
Dan Williams89a73012011-06-30 19:14:33 -07001262 sci_remote_device_construct(iport, idev);
Dan Williamsb87ee302011-04-25 11:48:29 -07001263
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001264 sci_port_get_properties(iport, &properties);
1265 /* Get accurate port width from port's phy mask for a DA device. */
1266 idev->device_port_width = hweight32(properties.phy_mask);
1267
Dan Williams89a73012011-06-30 19:14:33 -07001268 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams11cc5182012-02-01 00:23:10 -08001269 idev,
1270 &idev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001271
Dan Williamsa1a113b2011-04-21 18:44:45 -07001272 if (status != SCI_SUCCESS)
1273 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001274
Dan Williams89a73012011-06-30 19:14:33 -07001275 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
Dan Williams88f3b622011-04-22 19:18:03 -07001276
Dan Williamsa1a113b2011-04-21 18:44:45 -07001277 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001278}
1279
Dan Williams88f3b622011-04-22 19:18:03 -07001280/**
Dan Williams89a73012011-06-30 19:14:33 -07001281 * sci_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001282 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001283 * Remote node context(s) is/are a global resource allocated by this
Dan Williams89a73012011-06-30 19:14:33 -07001284 * routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001285 *
1286 * Returns:
1287 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1288 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1289 * sata-only controller instance.
1290 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001291 */
Dan Williams89a73012011-06-30 19:14:33 -07001292static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001293 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001294{
Dan Williams78a6f062011-06-30 16:31:37 -07001295 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001296 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001297
Dan Williams89a73012011-06-30 19:14:33 -07001298 sci_remote_device_construct(iport, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001299
Dan Williams89a73012011-06-30 19:14:33 -07001300 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001301 idev,
1302 &idev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001303 if (status != SCI_SUCCESS)
1304 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001305
Dan Williams11cc5182012-02-01 00:23:10 -08001306 /* For SAS-2 the physical link rate is actually a logical link
Dan Williamsa1a113b2011-04-21 18:44:45 -07001307 * rate that incorporates multiplexing. The SCU doesn't
1308 * incorporate multiplexing and for the purposes of the
1309 * connection the logical link rate is that same as the
1310 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
Dan Williams11cc5182012-02-01 00:23:10 -08001311 * one another, so this code works for both situations.
1312 */
Dan Williams89a73012011-06-30 19:14:33 -07001313 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
Dan Williams00d680e2011-04-25 14:29:29 -07001314 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001315
Dan Williamsa1a113b2011-04-21 18:44:45 -07001316 /* / @todo Should I assign the port width by reading all of the phys on the port? */
Dan Williams78a6f062011-06-30 16:31:37 -07001317 idev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001318
Dan Williamsab2e8f72011-04-27 16:32:45 -07001319 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001320}
1321
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001322enum sci_status sci_remote_device_resume(
1323 struct isci_remote_device *idev,
1324 scics_sds_remote_node_context_callback cb_fn,
1325 void *cb_p)
1326{
1327 enum sci_status status;
1328
1329 status = sci_remote_node_context_resume(&idev->rnc, cb_fn, cb_p);
1330 if (status != SCI_SUCCESS)
1331 dev_dbg(scirdev_to_dev(idev), "%s: failed to resume: %d\n",
1332 __func__, status);
1333 return status;
1334}
1335
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001336static void isci_remote_device_resume_from_abort_complete(void *cbparam)
1337{
1338 struct isci_remote_device *idev = cbparam;
1339 struct isci_host *ihost = idev->owning_port->owning_controller;
1340 scics_sds_remote_node_context_callback abort_resume_cb =
1341 idev->abort_resume_cb;
1342
1343 dev_dbg(scirdev_to_dev(idev), "%s: passing-along resume: %p\n",
1344 __func__, abort_resume_cb);
1345
1346 if (abort_resume_cb != NULL) {
1347 idev->abort_resume_cb = NULL;
1348 abort_resume_cb(idev->abort_resume_cbparam);
1349 }
1350 clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
1351 wake_up(&ihost->eventq);
1352}
1353
1354
1355void isci_remote_device_wait_for_resume_from_abort(
1356 struct isci_host *ihost,
1357 struct isci_remote_device *idev)
1358{
1359 dev_dbg(scirdev_to_dev(idev), "%s: starting resume wait: %p\n",
1360 __func__, idev);
1361
Jeff Skirvin8c731882012-03-08 22:42:06 -08001362 #define MAX_RESUME_MSECS 10000
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001363 if (!wait_event_timeout(ihost->eventq,
1364 (!test_bit(IDEV_ABORT_PATH_RESUME_PENDING,
1365 &idev->flags)
1366 || test_bit(IDEV_STOP_PENDING, &idev->flags)),
1367 msecs_to_jiffies(MAX_RESUME_MSECS))) {
1368
1369 dev_warn(scirdev_to_dev(idev), "%s: #### Timeout waiting for "
1370 "resume: %p\n", __func__, idev);
1371 }
1372 clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
1373
1374 dev_dbg(scirdev_to_dev(idev), "%s: resume wait done: %p\n",
1375 __func__, idev);
1376}
1377
Jeff Skirvin31a38ef2012-03-08 22:42:01 -08001378enum sci_status isci_remote_device_resume_from_abort(
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001379 struct isci_host *ihost,
Jeff Skirvin31a38ef2012-03-08 22:42:01 -08001380 struct isci_remote_device *idev)
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001381{
1382 unsigned long flags;
1383 enum sci_status status;
1384
1385 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin31a38ef2012-03-08 22:42:01 -08001386 /* Preserve any current resume callbacks, for instance from other
1387 * resumptions.
1388 */
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001389 idev->abort_resume_cb = idev->rnc.user_callback;
1390 idev->abort_resume_cbparam = idev->rnc.user_cookie;
1391 set_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
Jeff Skirvin0c3ce382012-03-08 22:42:02 -08001392 clear_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags);
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001393 status = sci_remote_device_resume(
1394 idev, isci_remote_device_resume_from_abort_complete,
1395 idev);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001396 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001397 isci_remote_device_wait_for_resume_from_abort(ihost, idev);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001398 return status;
1399}
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001400
Dan Williams88f3b622011-04-22 19:18:03 -07001401/**
Dan Williams89a73012011-06-30 19:14:33 -07001402 * sci_remote_device_start() - This method will start the supplied remote
Dan Williams88f3b622011-04-22 19:18:03 -07001403 * device. This method enables normal IO requests to flow through to the
1404 * remote device.
1405 * @remote_device: This parameter specifies the device to be started.
1406 * @timeout: This parameter specifies the number of milliseconds in which the
1407 * start operation should complete.
1408 *
1409 * An indication of whether the device was successfully started. SCI_SUCCESS
1410 * This value is returned if the device was successfully started.
1411 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1412 * the device when there have been no phys added to it.
1413 */
Dan Williams89a73012011-06-30 19:14:33 -07001414static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
Jeff Skirvin726980d2012-03-08 22:41:50 -08001415 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001416{
Dan Williams78a6f062011-06-30 16:31:37 -07001417 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -07001418 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamseb229672011-05-01 14:05:57 -07001419 enum sci_status status;
1420
Edmund Nadolskie3013702011-06-02 00:10:43 +00001421 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001422 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
1423 __func__, dev_state_name(state));
Dan Williamseb229672011-05-01 14:05:57 -07001424 return SCI_FAILURE_INVALID_STATE;
1425 }
1426
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001427 status = sci_remote_device_resume(idev, remote_device_resume_done,
1428 idev);
Dan Williamseb229672011-05-01 14:05:57 -07001429 if (status != SCI_SUCCESS)
1430 return status;
1431
Edmund Nadolskie3013702011-06-02 00:10:43 +00001432 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001433
1434 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001435}
Dan Williams6f231dd2011-07-02 22:56:22 -07001436
Dan Williams00d680e2011-04-25 14:29:29 -07001437static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1438 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001439{
Dan Williams00d680e2011-04-25 14:29:29 -07001440 struct isci_host *ihost = iport->isci_host;
1441 struct domain_device *dev = idev->domain_dev;
1442 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001443
Dan Williams00d680e2011-04-25 14:29:29 -07001444 if (dev->parent && dev_is_expander(dev->parent))
Dan Williams89a73012011-06-30 19:14:33 -07001445 status = sci_remote_device_ea_construct(iport, idev);
Dan Williams00d680e2011-04-25 14:29:29 -07001446 else
Dan Williams89a73012011-06-30 19:14:33 -07001447 status = sci_remote_device_da_construct(iport, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001448
1449 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001450 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1451 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001452
1453 return status;
1454 }
1455
Dan Williams6f231dd2011-07-02 22:56:22 -07001456 /* start the device. */
Dan Williams89a73012011-06-30 19:14:33 -07001457 status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001458
Dan Williams00d680e2011-04-25 14:29:29 -07001459 if (status != SCI_SUCCESS)
1460 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1461 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001462
1463 return status;
1464}
1465
Dan Williams6f231dd2011-07-02 22:56:22 -07001466/**
1467 * This function builds the isci_remote_device when a libsas dev_found message
1468 * is received.
1469 * @isci_host: This parameter specifies the isci host object.
1470 * @port: This parameter specifies the isci_port conected to this device.
1471 *
1472 * pointer to new isci_remote_device.
1473 */
1474static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001475isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001476{
Dan Williamsd9c37392011-03-03 17:59:32 -08001477 struct isci_remote_device *idev;
1478 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001479
Dan Williamsd9c37392011-03-03 17:59:32 -08001480 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001481 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001482 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1483 break;
1484 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001485
Dan Williamsd9c37392011-03-03 17:59:32 -08001486 if (i >= SCI_MAX_REMOTE_DEVICES) {
1487 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001488 return NULL;
1489 }
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001490 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1491 return NULL;
1492
Dan Williamsd9c37392011-03-03 17:59:32 -08001493 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001494}
Dan Williams6f231dd2011-07-02 22:56:22 -07001495
Dan Williams209fae12011-06-13 17:39:44 -07001496void isci_remote_device_release(struct kref *kref)
1497{
1498 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1499 struct isci_host *ihost = idev->isci_port->isci_host;
1500
1501 idev->domain_dev = NULL;
1502 idev->isci_port = NULL;
1503 clear_bit(IDEV_START_PENDING, &idev->flags);
1504 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -07001505 clear_bit(IDEV_IO_READY, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001506 clear_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001507 smp_mb__before_clear_bit();
1508 clear_bit(IDEV_ALLOCATED, &idev->flags);
1509 wake_up(&ihost->eventq);
1510}
1511
Dan Williams6f231dd2011-07-02 22:56:22 -07001512/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001513 * isci_remote_device_stop() - This function is called internally to stop the
1514 * remote device.
1515 * @isci_host: This parameter specifies the isci host object.
1516 * @isci_device: This parameter specifies the remote device.
1517 *
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001518 * The status of the ihost request to stop.
Dan Williams6f231dd2011-07-02 22:56:22 -07001519 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001520enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001521{
1522 enum sci_status status;
1523 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001524
Dan Williams6ad31fe2011-03-04 12:10:29 -08001525 dev_dbg(&ihost->pdev->dev,
1526 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001527
Dan Williams209fae12011-06-13 17:39:44 -07001528 spin_lock_irqsave(&ihost->scic_lock, flags);
1529 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1530 set_bit(IDEV_GONE, &idev->flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001531
Dan Williams6ad31fe2011-03-04 12:10:29 -08001532 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams89a73012011-06-30 19:14:33 -07001533 status = sci_remote_device_stop(idev, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001534 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001535
1536 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001537 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1538 /* nothing to wait for */;
1539 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001540 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001541
Jeff Skirvin83884012012-03-08 22:41:52 -08001542 dev_dbg(&ihost->pdev->dev,
1543 "%s: isci_device = %p, waiting done.\n", __func__, idev);
1544
Dan Williams6f231dd2011-07-02 22:56:22 -07001545 return status;
1546}
1547
1548/**
1549 * isci_remote_device_gone() - This function is called by libsas when a domain
1550 * device is removed.
1551 * @domain_device: This parameter specifies the libsas domain device.
1552 *
1553 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001554void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001555{
Dan Williams4393aa42011-03-31 13:10:44 -07001556 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001557 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001558
Dan Williams6ad31fe2011-03-04 12:10:29 -08001559 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001560 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001561 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001562
Dan Williams6ad31fe2011-03-04 12:10:29 -08001563 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001564}
1565
1566
1567/**
1568 * isci_remote_device_found() - This function is called by libsas when a remote
1569 * device is discovered. A remote device object is created and started. the
1570 * function then sleeps until the sci core device started message is
1571 * received.
1572 * @domain_device: This parameter specifies the libsas domain device.
1573 *
1574 * status, zero indicates success.
1575 */
Dan Williamsc132f692012-01-03 23:26:08 -08001576int isci_remote_device_found(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001577{
Dan Williamsc132f692012-01-03 23:26:08 -08001578 struct isci_host *isci_host = dev_to_ihost(dev);
1579 struct isci_port *isci_port = dev->port->lldd_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001580 struct isci_remote_device *isci_device;
1581 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001582
Dan Williams6f231dd2011-07-02 22:56:22 -07001583 dev_dbg(&isci_host->pdev->dev,
Dan Williamsc132f692012-01-03 23:26:08 -08001584 "%s: domain_device = %p\n", __func__, dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001585
Dan Williamsc132f692012-01-03 23:26:08 -08001586 if (!isci_port)
1587 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001588
Dan Williams6f231dd2011-07-02 22:56:22 -07001589 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001590 if (!isci_device)
1591 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001592
Dan Williams209fae12011-06-13 17:39:44 -07001593 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001594 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001595
1596 spin_lock_irq(&isci_host->scic_lock);
Dan Williamsc132f692012-01-03 23:26:08 -08001597 isci_device->domain_dev = dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001598 isci_device->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001599 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1600
Dan Williams6ad31fe2011-03-04 12:10:29 -08001601 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001602 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001603
Dan Williams6f231dd2011-07-02 22:56:22 -07001604 dev_dbg(&isci_host->pdev->dev,
1605 "%s: isci_device = %p\n",
1606 __func__, isci_device);
1607
Dan Williams209fae12011-06-13 17:39:44 -07001608 if (status == SCI_SUCCESS) {
1609 /* device came up, advertise it to the world */
Dan Williamsc132f692012-01-03 23:26:08 -08001610 dev->lldd_dev = isci_device;
Dan Williams209fae12011-06-13 17:39:44 -07001611 } else
1612 isci_put_device(isci_device);
1613 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001614
Dan Williams6ad31fe2011-03-04 12:10:29 -08001615 /* wait for the device ready callback. */
1616 wait_for_device_start(isci_host, isci_device);
1617
Dan Williams209fae12011-06-13 17:39:44 -07001618 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001619}
Jeff Skirvin726980d2012-03-08 22:41:50 -08001620
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001621enum sci_status isci_remote_device_suspend_terminate(
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001622 struct isci_host *ihost,
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001623 struct isci_remote_device *idev,
1624 struct isci_request *ireq)
Jeff Skirvin726980d2012-03-08 22:41:50 -08001625{
Jeff Skirvin726980d2012-03-08 22:41:50 -08001626 unsigned long flags;
1627 enum sci_status status;
1628
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001629 /* Put the device into suspension. */
Jeff Skirvin726980d2012-03-08 22:41:50 -08001630 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin0c3ce382012-03-08 22:42:02 -08001631 set_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags);
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -08001632 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
Jeff Skirvin726980d2012-03-08 22:41:50 -08001633 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001634
1635 /* Terminate and wait for the completions. */
1636 status = isci_remote_device_terminate_requests(ihost, idev, ireq);
1637 if (status != SCI_SUCCESS)
Jeff Skirvin726980d2012-03-08 22:41:50 -08001638 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001639 "%s: isci_remote_device_terminate_requests(%p) "
1640 "returned %d!\n",
Jeff Skirvin726980d2012-03-08 22:41:50 -08001641 __func__, idev, status);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001642
1643 /* NOTE: RNC resumption is left to the caller! */
Jeff Skirvin726980d2012-03-08 22:41:50 -08001644 return status;
1645}
1646
1647int isci_remote_device_is_safe_to_abort(
1648 struct isci_remote_device *idev)
1649{
1650 return sci_remote_node_context_is_safe_to_abort(&idev->rnc);
1651}
1652
1653enum sci_status sci_remote_device_abort_requests_pending_abort(
1654 struct isci_remote_device *idev)
1655{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001656 return sci_remote_device_terminate_reqs_checkabort(idev, 1);
Jeff Skirvin726980d2012-03-08 22:41:50 -08001657}
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001658
1659enum sci_status isci_remote_device_reset_complete(
1660 struct isci_host *ihost,
1661 struct isci_remote_device *idev)
1662{
1663 unsigned long flags;
1664 enum sci_status status;
1665
1666 spin_lock_irqsave(&ihost->scic_lock, flags);
1667 status = sci_remote_device_reset_complete(idev);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001668 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1669
1670 return status;
1671}
1672
Jeff Skirvin9608b642012-03-08 22:42:00 -08001673void isci_dev_set_hang_detection_timeout(
1674 struct isci_remote_device *idev,
1675 u32 timeout)
1676{
1677 if (dev_is_sata(idev->domain_dev)) {
1678 if (timeout) {
1679 if (test_and_set_bit(IDEV_RNC_LLHANG_ENABLED,
1680 &idev->flags))
1681 return; /* Already enabled. */
1682 } else if (!test_and_clear_bit(IDEV_RNC_LLHANG_ENABLED,
1683 &idev->flags))
1684 return; /* Not enabled. */
1685
1686 sci_port_set_hang_detection_timeout(idev->owning_port,
1687 timeout);
1688 }
1689}