isci: Escalate to I_T_Nexus_Reset when the device is gone.
[deliverable/linux.git] / drivers / scsi / isci / task.c
CommitLineData
6f231dda
DW
1/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#include <linux/completion.h>
50e7f9b5 57#include <linux/irqflags.h>
af5ae893 58#include "sas.h"
61aaff49 59#include <scsi/libsas.h>
88f3b62a
DW
60#include "remote_device.h"
61#include "remote_node_context.h"
6f231dda
DW
62#include "isci.h"
63#include "request.h"
6f231dda 64#include "task.h"
312e0c24 65#include "host.h"
af5ae893 66
50e7f9b5 67/**
1077a574
DW
68* isci_task_refuse() - complete the request to the upper layer driver in
69* the case where an I/O needs to be completed back in the submit path.
70* @ihost: host on which the the request was queued
71* @task: request to complete
72* @response: response code for the completed task.
73* @status: status code for the completed task.
50e7f9b5 74*
50e7f9b5 75*/
1077a574
DW
76static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
79
50e7f9b5 80{
1077a574 81 enum isci_completion_selection disposition;
50e7f9b5 82
1077a574
DW
83 disposition = isci_perform_normal_io_completion;
84 disposition = isci_task_set_completion_status(task, response, status,
85 disposition);
50e7f9b5
DW
86
87 /* Tasks aborted specifically by a call to the lldd_abort_task
1077a574
DW
88 * function should not be completed to the host in the regular path.
89 */
90 switch (disposition) {
a5ec7f86
JB
91 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
93 dev_dbg(&ihost->pdev->dev,
94 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
96 __func__, task, response, status);
50e7f9b5 97
a5ec7f86 98 task->lldd_task = NULL;
312d3e56 99 task->task_done(task);
a5ec7f86 100 break;
50e7f9b5 101
a5ec7f86
JB
102 case isci_perform_aborted_io_completion:
103 /*
104 * No notification because this request is already in the
105 * abort path.
106 */
107 dev_dbg(&ihost->pdev->dev,
108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
110 __func__, task, response, status);
111 break;
50e7f9b5 112
a5ec7f86
JB
113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
115 dev_dbg(&ihost->pdev->dev,
116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
118 __func__, task, response, status);
312d3e56 119 sas_task_abort(task);
a5ec7f86 120 break;
50e7f9b5 121
a5ec7f86
JB
122 default:
123 dev_dbg(&ihost->pdev->dev,
124 "%s: isci task notification default case!",
125 __func__);
126 sas_task_abort(task);
127 break;
50e7f9b5
DW
128 }
129}
6f231dda 130
1077a574
DW
131#define for_each_sas_task(num, task) \
132 for (; num > 0; num--,\
133 task = list_entry(task->list.next, struct sas_task, list))
134
9274f45e
JS
135
136static inline int isci_device_io_ready(struct isci_remote_device *idev,
137 struct sas_task *task)
138{
139 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
140 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
141 isci_task_is_ncq_recovery(task))
142 : 0;
143}
6f231dda
DW
144/**
145 * isci_task_execute_task() - This function is one of the SAS Domain Template
146 * functions. This function is called by libsas to send a task down to
147 * hardware.
148 * @task: This parameter specifies the SAS task to send.
149 * @num: This parameter specifies the number of tasks to queue.
150 * @gfp_flags: This parameter specifies the context of this call.
151 *
152 * status, zero indicates success.
153 */
154int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
155{
4393aa4e 156 struct isci_host *ihost = dev_to_ihost(task->dev);
209fae14 157 struct isci_remote_device *idev;
6f231dda 158 unsigned long flags;
f2088267 159 bool io_ready;
312e0c24 160 u16 tag;
6f231dda 161
1077a574 162 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
6f231dda 163
1077a574 164 for_each_sas_task(num, task) {
312e0c24
DW
165 enum sci_status status = SCI_FAILURE;
166
209fae14
DW
167 spin_lock_irqsave(&ihost->scic_lock, flags);
168 idev = isci_lookup_device(task->dev);
9274f45e 169 io_ready = isci_device_io_ready(idev, task);
312e0c24 170 tag = isci_alloc_tag(ihost);
209fae14 171 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 172
f2088267
DW
173 dev_dbg(&ihost->pdev->dev,
174 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
175 task, num, task->dev, idev, idev ? idev->flags : 0,
176 task->uldd_task);
6f231dda 177
f2088267
DW
178 if (!idev) {
179 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
180 SAS_DEVICE_UNKNOWN);
312e0c24 181 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
f2088267
DW
182 /* Indicate QUEUE_FULL so that the scsi midlayer
183 * retries.
184 */
185 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
186 SAS_QUEUE_FULL);
f0846c68
JS
187 } else {
188 /* There is a device and it's ready for I/O. */
189 spin_lock_irqsave(&task->task_state_lock, flags);
6f231dda 190
f0846c68 191 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
f2088267 192 /* The I/O was aborted. */
f0846c68
JS
193 spin_unlock_irqrestore(&task->task_state_lock,
194 flags);
6f231dda 195
1077a574
DW
196 isci_task_refuse(ihost, task,
197 SAS_TASK_UNDELIVERED,
198 SAM_STAT_TASK_ABORTED);
f0846c68 199 } else {
6f231dda
DW
200 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
201 spin_unlock_irqrestore(&task->task_state_lock, flags);
f0846c68
JS
202
203 /* build and send the request. */
db056250 204 status = isci_request_execute(ihost, idev, task, tag);
f0846c68
JS
205
206 if (status != SCI_SUCCESS) {
207
208 spin_lock_irqsave(&task->task_state_lock, flags);
209 /* Did not really start this command. */
210 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
211 spin_unlock_irqrestore(&task->task_state_lock, flags);
212
c2cb8a5f
JS
213 if (test_bit(IDEV_GONE, &idev->flags)) {
214
215 /* Indicate that the device
216 * is gone.
217 */
218 isci_task_refuse(ihost, task,
219 SAS_TASK_UNDELIVERED,
220 SAS_DEVICE_UNKNOWN);
221 } else {
222 /* Indicate QUEUE_FULL so that
223 * the scsi midlayer retries.
224 * If the request failed for
225 * remote device reasons, it
226 * gets returned as
227 * SAS_TASK_UNDELIVERED next
228 * time through.
229 */
230 isci_task_refuse(ihost, task,
231 SAS_TASK_COMPLETE,
232 SAS_QUEUE_FULL);
233 }
f0846c68 234 }
6f231dda
DW
235 }
236 }
312e0c24
DW
237 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
238 spin_lock_irqsave(&ihost->scic_lock, flags);
239 /* command never hit the device, so just free
240 * the tci and skip the sequence increment
241 */
242 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
243 spin_unlock_irqrestore(&ihost->scic_lock, flags);
244 }
209fae14 245 isci_put_device(idev);
1077a574 246 }
6f231dda
DW
247 return 0;
248}
249
0d0cf14c 250static struct isci_request *isci_task_request_build(struct isci_host *ihost,
209fae14 251 struct isci_remote_device *idev,
312e0c24 252 u16 tag, struct isci_tmf *isci_tmf)
6f231dda 253{
6f231dda 254 enum sci_status status = SCI_FAILURE;
0d0cf14c 255 struct isci_request *ireq = NULL;
a1a113b0 256 struct domain_device *dev;
6f231dda 257
0d0cf14c 258 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
259 "%s: isci_tmf = %p\n", __func__, isci_tmf);
260
0d0cf14c 261 dev = idev->domain_dev;
6f231dda
DW
262
263 /* do common allocation and init of request object. */
db056250 264 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
0d0cf14c
DW
265 if (!ireq)
266 return NULL;
6f231dda
DW
267
268 /* let the core do it's construct. */
89a7301f 269 status = sci_task_request_construct(ihost, idev, tag,
5076a1a9 270 ireq);
6f231dda
DW
271
272 if (status != SCI_SUCCESS) {
0d0cf14c 273 dev_warn(&ihost->pdev->dev,
89a7301f 274 "%s: sci_task_request_construct failed - "
6f231dda
DW
275 "status = 0x%x\n",
276 __func__,
277 status);
db056250 278 return NULL;
6f231dda
DW
279 }
280
a1a113b0
DW
281 /* XXX convert to get this from task->tproto like other drivers */
282 if (dev->dev_type == SAS_END_DEV) {
6f231dda 283 isci_tmf->proto = SAS_PROTOCOL_SSP;
89a7301f 284 status = sci_task_request_construct_ssp(ireq);
6f231dda 285 if (status != SCI_SUCCESS)
db056250 286 return NULL;
6f231dda
DW
287 }
288
0d0cf14c 289 return ireq;
6f231dda
DW
290}
291
b343dff1
JS
292/**
293* isci_request_mark_zombie() - This function must be called with scic_lock held.
294*/
295static void isci_request_mark_zombie(struct isci_host *ihost, struct isci_request *ireq)
296{
297 struct completion *tmf_completion = NULL;
298 struct completion *req_completion;
299
300 /* Set the request state to "dead". */
301 ireq->status = dead;
302
303 req_completion = ireq->io_request_completion;
304 ireq->io_request_completion = NULL;
305
3b34c169 306 if (test_bit(IREQ_TMF, &ireq->flags)) {
b343dff1
JS
307 /* Break links with the TMF request. */
308 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
309
310 /* In the case where a task request is dying,
311 * the thread waiting on the complete will sit and
312 * timeout unless we wake it now. Since the TMF
313 * has a default error status, complete it here
314 * to wake the waiting thread.
315 */
316 if (tmf) {
317 tmf_completion = tmf->complete;
318 tmf->complete = NULL;
319 }
320 ireq->ttype_ptr.tmf_task_ptr = NULL;
321 dev_dbg(&ihost->pdev->dev, "%s: tmf_code %d, managed tag %#x\n",
322 __func__, tmf->tmf_code, tmf->io_tag);
3b34c169
JS
323 } else {
324 /* Break links with the sas_task - the callback is done
325 * elsewhere.
326 */
327 struct sas_task *task = isci_request_access_task(ireq);
328
329 if (task)
330 task->lldd_task = NULL;
331
332 ireq->ttype_ptr.io_task_ptr = NULL;
b343dff1
JS
333 }
334
335 dev_warn(&ihost->pdev->dev, "task context unrecoverable (tag: %#x)\n",
336 ireq->io_tag);
337
338 /* Don't force waiting threads to timeout. */
339 if (req_completion)
340 complete(req_completion);
341
342 if (tmf_completion != NULL)
343 complete(tmf_completion);
344}
345
16ba7709
DW
346static int isci_task_execute_tmf(struct isci_host *ihost,
347 struct isci_remote_device *idev,
348 struct isci_tmf *tmf, unsigned long timeout_ms)
6f231dda
DW
349{
350 DECLARE_COMPLETION_ONSTACK(completion);
467e855a 351 enum sci_task_status status = SCI_TASK_FAILURE;
0d0cf14c 352 struct isci_request *ireq;
6f231dda
DW
353 int ret = TMF_RESP_FUNC_FAILED;
354 unsigned long flags;
fd18388b 355 unsigned long timeleft;
312e0c24
DW
356 u16 tag;
357
358 spin_lock_irqsave(&ihost->scic_lock, flags);
359 tag = isci_alloc_tag(ihost);
360 spin_unlock_irqrestore(&ihost->scic_lock, flags);
361
362 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
363 return ret;
6f231dda
DW
364
365 /* sanity check, return TMF_RESP_FUNC_FAILED
366 * if the device is not there and ready.
367 */
78a6f06e
DW
368 if (!idev ||
369 (!test_bit(IDEV_IO_READY, &idev->flags) &&
370 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
0d0cf14c 371 dev_dbg(&ihost->pdev->dev,
78a6f06e 372 "%s: idev = %p not ready (%#lx)\n",
6f231dda 373 __func__,
78a6f06e 374 idev, idev ? idev->flags : 0);
312e0c24 375 goto err_tci;
6f231dda 376 } else
0d0cf14c 377 dev_dbg(&ihost->pdev->dev,
78a6f06e
DW
378 "%s: idev = %p\n",
379 __func__, idev);
6f231dda
DW
380
381 /* Assign the pointer to the TMF's completion kernel wait structure. */
382 tmf->complete = &completion;
b343dff1 383 tmf->status = SCI_FAILURE_TIMEOUT;
6f231dda 384
78a6f06e 385 ireq = isci_task_request_build(ihost, idev, tag, tmf);
312e0c24
DW
386 if (!ireq)
387 goto err_tci;
6f231dda 388
0d0cf14c 389 spin_lock_irqsave(&ihost->scic_lock, flags);
6f231dda
DW
390
391 /* start the TMF io. */
89a7301f 392 status = sci_controller_start_task(ihost, idev, ireq);
6f231dda 393
467e855a 394 if (status != SCI_TASK_SUCCESS) {
a8a0a133 395 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
396 "%s: start_io failed - status = 0x%x, request = %p\n",
397 __func__,
398 status,
0d0cf14c
DW
399 ireq);
400 spin_unlock_irqrestore(&ihost->scic_lock, flags);
db056250 401 goto err_tci;
6f231dda
DW
402 }
403
6f231dda
DW
404 if (tmf->cb_state_func != NULL)
405 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
406
0d0cf14c 407 isci_request_change_state(ireq, started);
6f231dda
DW
408
409 /* add the request to the remote device request list. */
78a6f06e 410 list_add(&ireq->dev_node, &idev->reqs_in_process);
6f231dda 411
0d0cf14c 412 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
413
414 /* Wait for the TMF to complete, or a timeout. */
fd18388b 415 timeleft = wait_for_completion_timeout(&completion,
086a0dab 416 msecs_to_jiffies(timeout_ms));
fd18388b
EN
417
418 if (timeleft == 0) {
b343dff1
JS
419 /* The TMF did not complete - this could be because
420 * of an unplug. Terminate the TMF request now.
421 */
0d0cf14c 422 spin_lock_irqsave(&ihost->scic_lock, flags);
fd18388b
EN
423
424 if (tmf->cb_state_func != NULL)
b343dff1
JS
425 tmf->cb_state_func(isci_tmf_timed_out, tmf,
426 tmf->cb_data);
fd18388b 427
b343dff1 428 sci_controller_terminate_request(ihost, idev, ireq);
fd18388b 429
0d0cf14c 430 spin_unlock_irqrestore(&ihost->scic_lock, flags);
086a0dab 431
b343dff1
JS
432 timeleft = wait_for_completion_timeout(
433 &completion,
434 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
435
436 if (!timeleft) {
437 /* Strange condition - the termination of the TMF
438 * request timed-out.
439 */
440 spin_lock_irqsave(&ihost->scic_lock, flags);
441
442 /* If the TMF status has not changed, kill it. */
443 if (tmf->status == SCI_FAILURE_TIMEOUT)
444 isci_request_mark_zombie(ihost, ireq);
445
446 spin_unlock_irqrestore(&ihost->scic_lock, flags);
447 }
fd18388b 448 }
6f231dda 449
27234ab4 450 isci_print_tmf(ihost, tmf);
6f231dda
DW
451
452 if (tmf->status == SCI_SUCCESS)
453 ret = TMF_RESP_FUNC_COMPLETE;
454 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
0d0cf14c 455 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
456 "%s: tmf.status == "
457 "SCI_FAILURE_IO_RESPONSE_VALID\n",
458 __func__);
459 ret = TMF_RESP_FUNC_COMPLETE;
460 }
461 /* Else - leave the default "failed" status alone. */
462
0d0cf14c 463 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
464 "%s: completed request = %p\n",
465 __func__,
0d0cf14c 466 ireq);
6f231dda 467
6f231dda 468 return ret;
312e0c24 469
312e0c24
DW
470 err_tci:
471 spin_lock_irqsave(&ihost->scic_lock, flags);
472 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
473 spin_unlock_irqrestore(&ihost->scic_lock, flags);
474
475 return ret;
6f231dda
DW
476}
477
16ba7709
DW
478static void isci_task_build_tmf(struct isci_tmf *tmf,
479 enum isci_tmf_function_codes code,
480 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
481 struct isci_tmf *,
482 void *),
483 void *cb_data)
6f231dda 484{
6f231dda
DW
485 memset(tmf, 0, sizeof(*tmf));
486
6f231dda 487 tmf->tmf_code = code;
6f231dda 488 tmf->cb_state_func = tmf_sent_cb;
c3f42feb
JS
489 tmf->cb_data = cb_data;
490}
1fad9e93 491
16ba7709
DW
492static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
493 enum isci_tmf_function_codes code,
494 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
495 struct isci_tmf *,
496 void *),
497 struct isci_request *old_request)
c3f42feb 498{
16ba7709 499 isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
c3f42feb 500 tmf->io_tag = old_request->io_tag;
6f231dda
DW
501}
502
6f231dda
DW
503/**
504 * isci_task_validate_request_to_abort() - This function checks the given I/O
505 * against the "started" state. If the request is still "started", it's
506 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
507 * BEFORE CALLING THIS FUNCTION.
508 * @isci_request: This parameter specifies the request object to control.
509 * @isci_host: This parameter specifies the ISCI host object
510 * @isci_device: This is the device to which the request is pending.
511 * @aborted_io_completion: This is a completion structure that will be added to
512 * the request in case it is changed to aborting; this completion is
513 * triggered when the request is fully completed.
514 *
515 * Either "started" on successful change of the task status to "aborted", or
516 * "unallocated" if the task cannot be controlled.
517 */
518static enum isci_request_status isci_task_validate_request_to_abort(
519 struct isci_request *isci_request,
520 struct isci_host *isci_host,
521 struct isci_remote_device *isci_device,
522 struct completion *aborted_io_completion)
523{
524 enum isci_request_status old_state = unallocated;
525
526 /* Only abort the task if it's in the
527 * device's request_in_process list
528 */
529 if (isci_request && !list_empty(&isci_request->dev_node)) {
530 old_state = isci_request_change_started_to_aborted(
531 isci_request, aborted_io_completion);
532
6f231dda
DW
533 }
534
535 return old_state;
536}
537
d6891682
JS
538static int isci_request_is_dealloc_managed(enum isci_request_status stat)
539{
540 switch (stat) {
541 case aborted:
542 case aborting:
543 case terminating:
544 case completed:
545 case dead:
546 return true;
547 default:
548 return false;
549 }
550}
551
6f231dda
DW
552/**
553 * isci_terminate_request_core() - This function will terminate the given
554 * request, and wait for it to complete. This function must only be called
555 * from a thread that can wait. Note that the request is terminated and
556 * completed (back to the host, if started there).
d9dcb4ba 557 * @ihost: This SCU.
78a6f06e 558 * @idev: The target.
6f231dda
DW
559 * @isci_request: The I/O request to be terminated.
560 *
6f231dda 561 */
d9dcb4ba
DW
562static void isci_terminate_request_core(struct isci_host *ihost,
563 struct isci_remote_device *idev,
564 struct isci_request *isci_request)
6f231dda 565{
cbb65c66 566 enum sci_status status = SCI_SUCCESS;
6f231dda
DW
567 bool was_terminated = false;
568 bool needs_cleanup_handling = false;
77c852f3
JS
569 unsigned long flags;
570 unsigned long termination_completed = 1;
571 struct completion *io_request_completion;
6f231dda 572
d9dcb4ba 573 dev_dbg(&ihost->pdev->dev,
6f231dda 574 "%s: device = %p; request = %p\n",
78a6f06e 575 __func__, idev, isci_request);
6f231dda 576
d9dcb4ba 577 spin_lock_irqsave(&ihost->scic_lock, flags);
4dc043c4 578
77c852f3
JS
579 io_request_completion = isci_request->io_request_completion;
580
4dc043c4 581 /* Note that we are not going to control
38d8879b
DW
582 * the target to abort the request.
583 */
584 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
4dc043c4 585
6f231dda
DW
586 /* Make sure the request wasn't just sitting around signalling
587 * device condition (if the request handle is NULL, then the
588 * request completed but needed additional handling here).
589 */
38d8879b 590 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
6f231dda 591 was_terminated = true;
cbb65c66 592 needs_cleanup_handling = true;
89a7301f 593 status = sci_controller_terminate_request(ihost,
d9dcb4ba
DW
594 idev,
595 isci_request);
6f231dda 596 }
d9dcb4ba 597 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
598
599 /*
600 * The only time the request to terminate will
601 * fail is when the io request is completed and
602 * being aborted.
603 */
4dc043c4 604 if (status != SCI_SUCCESS) {
a8a0a133 605 dev_dbg(&ihost->pdev->dev,
89a7301f 606 "%s: sci_controller_terminate_request"
6f231dda 607 " returned = 0x%x\n",
77c852f3
JS
608 __func__, status);
609
4dc043c4
JS
610 isci_request->io_request_completion = NULL;
611
612 } else {
6f231dda 613 if (was_terminated) {
d9dcb4ba 614 dev_dbg(&ihost->pdev->dev,
77c852f3
JS
615 "%s: before completion wait (%p/%p)\n",
616 __func__, isci_request, io_request_completion);
6f231dda
DW
617
618 /* Wait here for the request to complete. */
77c852f3 619 termination_completed
4dc043c4 620 = wait_for_completion_timeout(
77c852f3 621 io_request_completion,
b343dff1 622 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
4dc043c4 623
77c852f3
JS
624 if (!termination_completed) {
625
626 /* The request to terminate has timed out. */
b343dff1 627 spin_lock_irqsave(&ihost->scic_lock, flags);
77c852f3
JS
628
629 /* Check for state changes. */
b343dff1
JS
630 if (!test_bit(IREQ_TERMINATED,
631 &isci_request->flags)) {
77c852f3
JS
632
633 /* The best we can do is to have the
634 * request die a silent death if it
635 * ever really completes.
77c852f3 636 */
b343dff1
JS
637 isci_request_mark_zombie(ihost,
638 isci_request);
639 needs_cleanup_handling = true;
77c852f3
JS
640 } else
641 termination_completed = 1;
642
d9dcb4ba 643 spin_unlock_irqrestore(&ihost->scic_lock,
77c852f3 644 flags);
4dc043c4 645
77c852f3 646 if (!termination_completed) {
4dc043c4 647
a8a0a133 648 dev_dbg(&ihost->pdev->dev,
77c852f3
JS
649 "%s: *** Timeout waiting for "
650 "termination(%p/%p)\n",
651 __func__, io_request_completion,
652 isci_request);
4dc043c4 653
77c852f3
JS
654 /* The request can no longer be referenced
655 * safely since it may go away if the
656 * termination every really does complete.
657 */
658 isci_request = NULL;
659 }
660 }
661 if (termination_completed)
d9dcb4ba 662 dev_dbg(&ihost->pdev->dev,
77c852f3
JS
663 "%s: after completion wait (%p/%p)\n",
664 __func__, isci_request, io_request_completion);
4dc043c4 665 }
4dc043c4 666
77c852f3 667 if (termination_completed) {
cbb65c66 668
77c852f3 669 isci_request->io_request_completion = NULL;
6f231dda 670
77c852f3
JS
671 /* Peek at the status of the request. This will tell
672 * us if there was special handling on the request such that it
673 * needs to be detached and freed here.
674 */
675 spin_lock_irqsave(&isci_request->state_lock, flags);
d6891682
JS
676
677 needs_cleanup_handling
678 = isci_request_is_dealloc_managed(
679 isci_request->status);
680
77c852f3 681 spin_unlock_irqrestore(&isci_request->state_lock, flags);
6f231dda 682
77c852f3 683 }
db49c2d0
JS
684 if (needs_cleanup_handling) {
685
686 dev_dbg(&ihost->pdev->dev,
687 "%s: cleanup isci_device=%p, request=%p\n",
688 __func__, idev, isci_request);
689
690 if (isci_request != NULL) {
691 spin_lock_irqsave(&ihost->scic_lock, flags);
692 isci_free_tag(ihost, isci_request->io_tag);
693 isci_request_change_state(isci_request, unallocated);
694 list_del_init(&isci_request->dev_node);
695 spin_unlock_irqrestore(&ihost->scic_lock, flags);
696 }
697 }
a5fde225 698 }
6f231dda
DW
699}
700
701/**
702 * isci_terminate_pending_requests() - This function will change the all of the
703 * requests on the given device's state to "aborting", will terminate the
704 * requests, and wait for them to complete. This function must only be
705 * called from a thread that can wait. Note that the requests are all
706 * terminated and completed (back to the host, if started there).
707 * @isci_host: This parameter specifies SCU.
78a6f06e 708 * @idev: This parameter specifies the target.
6f231dda 709 *
6f231dda 710 */
980d3aeb
DW
711void isci_terminate_pending_requests(struct isci_host *ihost,
712 struct isci_remote_device *idev)
6f231dda 713{
980d3aeb 714 struct completion request_completion;
77c852f3 715 enum isci_request_status old_state;
980d3aeb
DW
716 unsigned long flags;
717 LIST_HEAD(list);
6f231dda 718
5b6bf225
JS
719 isci_remote_device_suspend(ihost, idev);
720
980d3aeb
DW
721 spin_lock_irqsave(&ihost->scic_lock, flags);
722 list_splice_init(&idev->reqs_in_process, &list);
6f231dda 723
980d3aeb
DW
724 /* assumes that isci_terminate_request_core deletes from the list */
725 while (!list_empty(&list)) {
726 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
6f231dda 727
980d3aeb
DW
728 /* Change state to "terminating" if it is currently
729 * "started".
730 */
731 old_state = isci_request_change_started_to_newstate(ireq,
732 &request_completion,
733 terminating);
734 switch (old_state) {
735 case started:
736 case completed:
737 case aborting:
738 break;
739 default:
740 /* termination in progress, or otherwise dispositioned.
741 * We know the request was on 'list' so should be safe
742 * to move it back to reqs_in_process
743 */
744 list_move(&ireq->dev_node, &idev->reqs_in_process);
745 ireq = NULL;
746 break;
747 }
4dc043c4 748
980d3aeb
DW
749 if (!ireq)
750 continue;
751 spin_unlock_irqrestore(&ihost->scic_lock, flags);
77c852f3 752
980d3aeb 753 init_completion(&request_completion);
77c852f3 754
980d3aeb
DW
755 dev_dbg(&ihost->pdev->dev,
756 "%s: idev=%p request=%p; task=%p old_state=%d\n",
757 __func__, idev, ireq,
3b34c169
JS
758 (!test_bit(IREQ_TMF, &ireq->flags)
759 ? isci_request_access_task(ireq)
760 : NULL),
980d3aeb
DW
761 old_state);
762
763 /* If the old_state is started:
764 * This request was not already being aborted. If it had been,
765 * then the aborting I/O (ie. the TMF request) would not be in
766 * the aborting state, and thus would be terminated here. Note
767 * that since the TMF completion's call to the kernel function
768 * "complete()" does not happen until the pending I/O request
769 * terminate fully completes, we do not have to implement a
770 * special wait here for already aborting requests - the
771 * termination of the TMF request will force the request
772 * to finish it's already started terminate.
773 *
774 * If old_state == completed:
775 * This request completed from the SCU hardware perspective
776 * and now just needs cleaning up in terms of freeing the
777 * request and potentially calling up to libsas.
778 *
779 * If old_state == aborting:
780 * This request has already gone through a TMF timeout, but may
781 * not have been terminated; needs cleaning up at least.
782 */
783 isci_terminate_request_core(ihost, idev, ireq);
784 spin_lock_irqsave(&ihost->scic_lock, flags);
4dc043c4 785 }
980d3aeb 786 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
787}
788
789/**
790 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
791 * Template functions.
792 * @lun: This parameter specifies the lun to be reset.
793 *
794 * status, zero indicates success.
795 */
796static int isci_task_send_lu_reset_sas(
797 struct isci_host *isci_host,
798 struct isci_remote_device *isci_device,
799 u8 *lun)
800{
801 struct isci_tmf tmf;
802 int ret = TMF_RESP_FUNC_FAILED;
803
804 dev_dbg(&isci_host->pdev->dev,
805 "%s: isci_host = %p, isci_device = %p\n",
806 __func__, isci_host, isci_device);
807 /* Send the LUN reset to the target. By the time the call returns,
808 * the TMF has fully exected in the target (in which case the return
809 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
810 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
811 */
209fae14 812 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
6f231dda
DW
813
814 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
209fae14 815 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
6f231dda
DW
816
817 if (ret == TMF_RESP_FUNC_COMPLETE)
818 dev_dbg(&isci_host->pdev->dev,
819 "%s: %p: TMF_LU_RESET passed\n",
820 __func__, isci_device);
821 else
822 dev_dbg(&isci_host->pdev->dev,
823 "%s: %p: TMF_LU_RESET failed (%x)\n",
824 __func__, isci_device, ret);
825
826 return ret;
827}
828
43a5ab15 829int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
16ba7709 830{
5b6bf225 831 struct isci_host *ihost = dev_to_ihost(dev);
209fae14
DW
832 struct isci_remote_device *isci_device;
833 unsigned long flags;
6f231dda 834 int ret;
6f231dda 835
5b6bf225 836 spin_lock_irqsave(&ihost->scic_lock, flags);
43a5ab15 837 isci_device = isci_lookup_device(dev);
5b6bf225 838 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 839
5b6bf225 840 dev_dbg(&ihost->pdev->dev,
4393aa4e 841 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
5b6bf225 842 __func__, dev, ihost, isci_device);
6f231dda 843
98145cb7 844 if (!isci_device) {
d80ecd57 845 /* If the device is gone, escalate to I_T_Nexus_Reset. */
5b6bf225 846 dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
6f231dda 847
d80ecd57 848 ret = TMF_RESP_FUNC_FAILED;
209fae14 849 goto out;
6f231dda 850 }
5b6bf225
JS
851 if (isci_remote_device_suspend(ihost, isci_device) != SCI_SUCCESS) {
852 dev_dbg(&ihost->pdev->dev,
853 "%s: device = %p; failed to suspend\n",
854 __func__, isci_device);
855 ret = TMF_RESP_FUNC_FAILED;
856 goto out;
857 }
6f231dda 858
6f231dda 859 /* Send the task management part of the reset. */
43a5ab15
DW
860 if (dev_is_sata(dev)) {
861 sas_ata_schedule_reset(dev);
862 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 863 } else
5b6bf225 864 ret = isci_task_send_lu_reset_sas(ihost, isci_device, lun);
6f231dda
DW
865
866 /* If the LUN reset worked, all the I/O can now be terminated. */
5b6bf225 867 if (ret == TMF_RESP_FUNC_COMPLETE) {
6f231dda 868 /* Terminate all I/O now. */
5b6bf225
JS
869 isci_terminate_pending_requests(ihost, isci_device);
870 isci_remote_device_resume(ihost, isci_device, NULL, NULL);
871 }
209fae14
DW
872 out:
873 isci_put_device(isci_device);
6f231dda
DW
874 return ret;
875}
876
877
878/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
879int isci_task_clear_nexus_port(struct asd_sas_port *port)
880{
881 return TMF_RESP_FUNC_FAILED;
882}
883
884
885
886int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
887{
888 return TMF_RESP_FUNC_FAILED;
889}
890
6f231dda
DW
891/* Task Management Functions. Must be called from process context. */
892
893/**
894 * isci_abort_task_process_cb() - This is a helper function for the abort task
895 * TMF command. It manages the request state with respect to the successful
896 * transmission / completion of the abort task request.
897 * @cb_state: This parameter specifies when this function was called - after
898 * the TMF request has been started and after it has timed-out.
899 * @tmf: This parameter specifies the TMF in progress.
900 *
901 *
902 */
903static void isci_abort_task_process_cb(
904 enum isci_tmf_cb_state cb_state,
905 struct isci_tmf *tmf,
906 void *cb_data)
907{
908 struct isci_request *old_request;
909
910 old_request = (struct isci_request *)cb_data;
911
912 dev_dbg(&old_request->isci_host->pdev->dev,
913 "%s: tmf=%p, old_request=%p\n",
914 __func__, tmf, old_request);
915
916 switch (cb_state) {
917
918 case isci_tmf_started:
919 /* The TMF has been started. Nothing to do here, since the
920 * request state was already set to "aborted" by the abort
921 * task function.
922 */
6cb4d6b3
BB
923 if ((old_request->status != aborted)
924 && (old_request->status != completed))
a8a0a133 925 dev_dbg(&old_request->isci_host->pdev->dev,
6cb4d6b3
BB
926 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
927 __func__, old_request->status, tmf, old_request);
6f231dda
DW
928 break;
929
930 case isci_tmf_timed_out:
931
932 /* Set the task's state to "aborting", since the abort task
933 * function thread set it to "aborted" (above) in anticipation
934 * of the task management request working correctly. Since the
935 * timeout has now fired, the TMF request failed. We set the
936 * state such that the request completion will indicate the
937 * device is no longer present.
938 */
939 isci_request_change_state(old_request, aborting);
940 break;
941
942 default:
a8a0a133 943 dev_dbg(&old_request->isci_host->pdev->dev,
6f231dda
DW
944 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
945 __func__, cb_state, tmf, old_request);
946 break;
947 }
948}
949
950/**
951 * isci_task_abort_task() - This function is one of the SAS Domain Template
952 * functions. This function is called by libsas to abort a specified task.
953 * @task: This parameter specifies the SAS task to abort.
954 *
955 * status, zero indicates success.
956 */
957int isci_task_abort_task(struct sas_task *task)
958{
4393aa4e 959 struct isci_host *isci_host = dev_to_ihost(task->dev);
6f231dda 960 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
a5fde225
JS
961 struct isci_request *old_request = NULL;
962 enum isci_request_status old_state;
6f231dda 963 struct isci_remote_device *isci_device = NULL;
a5fde225
JS
964 struct isci_tmf tmf;
965 int ret = TMF_RESP_FUNC_FAILED;
966 unsigned long flags;
98145cb7 967 int perform_termination = 0;
6f231dda
DW
968
969 /* Get the isci_request reference from the task. Note that
970 * this check does not depend on the pending request list
971 * in the device, because tasks driving resets may land here
972 * after completion in the core.
973 */
209fae14
DW
974 spin_lock_irqsave(&isci_host->scic_lock, flags);
975 spin_lock(&task->task_state_lock);
976
977 old_request = task->lldd_task;
978
979 /* If task is already done, the request isn't valid */
980 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
981 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
982 old_request)
983 isci_device = isci_lookup_device(task->dev);
984
985 spin_unlock(&task->task_state_lock);
986 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
6f231dda 987
5b6bf225 988 dev_warn(&isci_host->pdev->dev,
98145cb7
JS
989 "%s: dev = %p, task = %p, old_request == %p\n",
990 __func__, isci_device, task, old_request);
6f231dda 991
98145cb7
JS
992 /* Device reset conditions signalled in task_state_flags are the
993 * responsbility of libsas to observe at the start of the error
994 * handler thread.
6f231dda 995 */
98145cb7
JS
996 if (!isci_device || !old_request) {
997 /* The request has already completed and there
998 * is nothing to do here other than to set the task
999 * done bit, and indicate that the task abort function
1000 * was sucessful.
1001 */
1002 spin_lock_irqsave(&task->task_state_lock, flags);
1003 task->task_state_flags |= SAS_TASK_STATE_DONE;
1004 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1005 SAS_TASK_STATE_PENDING);
1006 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda 1007
98145cb7 1008 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 1009
5b6bf225 1010 dev_warn(&isci_host->pdev->dev,
98145cb7
JS
1011 "%s: abort task not needed for %p\n",
1012 __func__, task);
209fae14 1013 goto out;
a5ec7f86 1014 }
6f231dda
DW
1015
1016 spin_lock_irqsave(&isci_host->scic_lock, flags);
1017
f219f010 1018 /* Check the request status and change to "aborted" if currently
a5fde225 1019 * "starting"; if true then set the I/O kernel completion
6f231dda
DW
1020 * struct that will be triggered when the request completes.
1021 */
a5fde225
JS
1022 old_state = isci_task_validate_request_to_abort(
1023 old_request, isci_host, isci_device,
1024 &aborted_io_completion);
f219f010
JS
1025 if ((old_state != started) &&
1026 (old_state != completed) &&
1027 (old_state != aborting)) {
6f231dda
DW
1028
1029 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1030
a5fde225
JS
1031 /* The request was already being handled by someone else (because
1032 * they got to set the state away from started).
1033 */
5b6bf225 1034 dev_warn(&isci_host->pdev->dev,
a5fde225
JS
1035 "%s: device = %p; old_request %p already being aborted\n",
1036 __func__,
1037 isci_device, old_request);
209fae14
DW
1038 ret = TMF_RESP_FUNC_COMPLETE;
1039 goto out;
6f231dda 1040 }
38d8879b 1041 if (task->task_proto == SAS_PROTOCOL_SMP ||
98145cb7 1042 sas_protocol_ata(task->task_proto) ||
38d8879b 1043 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
6f231dda
DW
1044
1045 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1046
5b6bf225 1047 dev_warn(&isci_host->pdev->dev,
98145cb7 1048 "%s: %s request"
a5fde225 1049 " or complete_in_target (%d), thus no TMF\n",
98145cb7
JS
1050 __func__,
1051 ((task->task_proto == SAS_PROTOCOL_SMP)
1052 ? "SMP"
1053 : (sas_protocol_ata(task->task_proto)
1054 ? "SATA/STP"
1055 : "<other>")
1056 ),
38d8879b 1057 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
a5fde225 1058
98145cb7
JS
1059 if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
1060 spin_lock_irqsave(&task->task_state_lock, flags);
1061 task->task_state_flags |= SAS_TASK_STATE_DONE;
1062 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1063 SAS_TASK_STATE_PENDING);
1064 spin_unlock_irqrestore(&task->task_state_lock, flags);
1065 ret = TMF_RESP_FUNC_COMPLETE;
1066 } else {
1067 spin_lock_irqsave(&task->task_state_lock, flags);
1068 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1069 SAS_TASK_STATE_PENDING);
1070 spin_unlock_irqrestore(&task->task_state_lock, flags);
1071 }
6f231dda 1072
98145cb7
JS
1073 /* STP and SMP devices are not sent a TMF, but the
1074 * outstanding I/O request is terminated below. This is
1075 * because SATA/STP and SMP discovery path timeouts directly
1076 * call the abort task interface for cleanup.
6f231dda 1077 */
98145cb7
JS
1078 perform_termination = 1;
1079
5b6bf225
JS
1080 if (isci_device && !test_bit(IDEV_GONE, &isci_device->flags) &&
1081 (isci_remote_device_suspend(isci_host, isci_device)
1082 != SCI_SUCCESS)) {
1083 dev_warn(&isci_host->pdev->dev,
1084 "%s: device = %p; failed to suspend\n",
1085 __func__, isci_device);
1086 goto out;
1087 }
1088
6f231dda
DW
1089 } else {
1090 /* Fill in the tmf stucture */
209fae14 1091 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
c3f42feb
JS
1092 isci_abort_task_process_cb,
1093 old_request);
6f231dda 1094
6f231dda
DW
1095 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1096
5b6bf225
JS
1097 if (isci_remote_device_suspend(isci_host, isci_device)
1098 != SCI_SUCCESS) {
1099 dev_warn(&isci_host->pdev->dev,
1100 "%s: device = %p; failed to suspend\n",
1101 __func__, isci_device);
1102 goto out;
1103 }
1104
98145cb7 1105 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
209fae14 1106 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
6f231dda
DW
1107 ISCI_ABORT_TASK_TIMEOUT_MS);
1108
98145cb7
JS
1109 if (ret == TMF_RESP_FUNC_COMPLETE)
1110 perform_termination = 1;
1111 else
5b6bf225 1112 dev_warn(&isci_host->pdev->dev,
98145cb7 1113 "%s: isci_task_send_tmf failed\n", __func__);
6f231dda 1114 }
98145cb7 1115 if (perform_termination) {
38d8879b 1116 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
a5fde225 1117
77c852f3
JS
1118 /* Clean up the request on our side, and wait for the aborted
1119 * I/O to complete.
1120 */
98145cb7
JS
1121 isci_terminate_request_core(isci_host, isci_device,
1122 old_request);
5b6bf225 1123 isci_remote_device_resume(isci_host, isci_device, NULL, NULL);
a5fde225 1124 }
6f231dda 1125
a5fde225
JS
1126 /* Make sure we do not leave a reference to aborted_io_completion */
1127 old_request->io_request_completion = NULL;
209fae14
DW
1128 out:
1129 isci_put_device(isci_device);
6f231dda
DW
1130 return ret;
1131}
1132
1133/**
1134 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1135 * functions. This is one of the Task Management functoins called by libsas,
1136 * to abort all task for the given lun.
1137 * @d_device: This parameter specifies the domain device associated with this
1138 * request.
1139 * @lun: This parameter specifies the lun associated with this request.
1140 *
1141 * status, zero indicates success.
1142 */
1143int isci_task_abort_task_set(
1144 struct domain_device *d_device,
1145 u8 *lun)
1146{
1147 return TMF_RESP_FUNC_FAILED;
1148}
1149
1150
1151/**
1152 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1153 * functions. This is one of the Task Management functoins called by libsas.
1154 * @d_device: This parameter specifies the domain device associated with this
1155 * request.
1156 * @lun: This parameter specifies the lun associated with this request.
1157 *
1158 * status, zero indicates success.
1159 */
1160int isci_task_clear_aca(
1161 struct domain_device *d_device,
1162 u8 *lun)
1163{
1164 return TMF_RESP_FUNC_FAILED;
1165}
1166
1167
1168
1169/**
1170 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1171 * functions. This is one of the Task Management functoins called by libsas.
1172 * @d_device: This parameter specifies the domain device associated with this
1173 * request.
1174 * @lun: This parameter specifies the lun associated with this request.
1175 *
1176 * status, zero indicates success.
1177 */
1178int isci_task_clear_task_set(
1179 struct domain_device *d_device,
1180 u8 *lun)
1181{
1182 return TMF_RESP_FUNC_FAILED;
1183}
1184
1185
1186/**
1187 * isci_task_query_task() - This function is implemented to cause libsas to
1188 * correctly escalate the failed abort to a LUN or target reset (this is
1189 * because sas_scsi_find_task libsas function does not correctly interpret
1190 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1191 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1192 * returned, libsas will turn this into a target reset
1193 * @task: This parameter specifies the sas task being queried.
1194 * @lun: This parameter specifies the lun associated with this request.
1195 *
1196 * status, zero indicates success.
1197 */
1198int isci_task_query_task(
1199 struct sas_task *task)
1200{
1201 /* See if there is a pending device reset for this device. */
1202 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1203 return TMF_RESP_FUNC_FAILED;
1204 else
1205 return TMF_RESP_FUNC_SUCC;
1206}
1207
af5ae893 1208/*
6f231dda
DW
1209 * isci_task_request_complete() - This function is called by the sci core when
1210 * an task request completes.
af5ae893
DJ
1211 * @ihost: This parameter specifies the ISCI host object
1212 * @ireq: This parameter is the completed isci_request object.
6f231dda
DW
1213 * @completion_status: This parameter specifies the completion status from the
1214 * sci core.
1215 *
1216 * none.
1217 */
af5ae893
DJ
1218void
1219isci_task_request_complete(struct isci_host *ihost,
1220 struct isci_request *ireq,
1221 enum sci_task_status completion_status)
6f231dda 1222{
af5ae893 1223 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
b343dff1
JS
1224 struct completion *tmf_complete = NULL;
1225 struct completion *request_complete = ireq->io_request_completion;
6f231dda 1226
af5ae893 1227 dev_dbg(&ihost->pdev->dev,
6f231dda 1228 "%s: request = %p, status=%d\n",
af5ae893 1229 __func__, ireq, completion_status);
6f231dda 1230
8d2c65c0 1231 isci_request_change_state(ireq, completed);
6f231dda 1232
38d8879b 1233 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
6f231dda 1234
b343dff1
JS
1235 if (tmf) {
1236 tmf->status = completion_status;
1237
1238 if (tmf->proto == SAS_PROTOCOL_SSP) {
1239 memcpy(&tmf->resp.resp_iu,
1240 &ireq->ssp.rsp,
1241 SSP_RESP_IU_MAX_SIZE);
1242 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
1243 memcpy(&tmf->resp.d2h_fis,
1244 &ireq->stp.rsp,
1245 sizeof(struct dev_to_host_fis));
1246 }
1247 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1248 tmf_complete = tmf->complete;
6f231dda 1249 }
89a7301f 1250 sci_controller_complete_io(ihost, ireq->target_device, ireq);
67ea838d 1251 /* set the 'terminated' flag handle to make sure it cannot be terminated
6f231dda
DW
1252 * or completed again.
1253 */
38d8879b 1254 set_bit(IREQ_TERMINATED, &ireq->flags);
6f231dda 1255
d6891682
JS
1256 /* As soon as something is in the terminate path, deallocation is
1257 * managed there. Note that the final non-managed state of a task
1258 * request is "completed".
1259 */
1260 if ((ireq->status == completed) ||
1261 !isci_request_is_dealloc_managed(ireq->status)) {
1262 isci_request_change_state(ireq, unallocated);
1263 isci_free_tag(ihost, ireq->io_tag);
1264 list_del_init(&ireq->dev_node);
1265 }
6f231dda 1266
b343dff1
JS
1267 /* "request_complete" is set if the task was being terminated. */
1268 if (request_complete)
1269 complete(request_complete);
1270
6f231dda 1271 /* The task management part completes last. */
b343dff1
JS
1272 if (tmf_complete)
1273 complete(tmf_complete);
6f231dda
DW
1274}
1275
209fae14 1276static int isci_reset_device(struct isci_host *ihost,
92776991 1277 struct domain_device *dev,
bc6f387d 1278 struct isci_remote_device *idev)
6f231dda 1279{
d06b487b 1280 int rc;
92776991
DW
1281 enum sci_status status;
1282 struct sas_phy *phy = sas_get_local_phy(dev);
1283 struct isci_port *iport = dev->port->lldd_port;
6f231dda 1284
d06b487b 1285 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
6f231dda 1286
5b6bf225 1287 if (isci_remote_device_reset(ihost, idev) != SCI_SUCCESS) {
f41a0c44
DW
1288 rc = TMF_RESP_FUNC_FAILED;
1289 goto out;
6f231dda 1290 }
6f231dda 1291
92776991
DW
1292 if (scsi_is_sas_phy_local(phy)) {
1293 struct isci_phy *iphy = &ihost->phys[phy->number];
1294
1295 rc = isci_port_perform_hard_reset(ihost, iport, iphy);
1296 } else
1297 rc = sas_phy_reset(phy, !dev_is_sata(dev));
6f231dda 1298
d06b487b
DW
1299 /* Terminate in-progress I/O now. */
1300 isci_remote_device_nuke_requests(ihost, idev);
6f231dda 1301
ff717ab0 1302 /* Since all pending TCs have been cleaned, resume the RNC. */
5b6bf225 1303 status = isci_remote_device_reset_complete(ihost, idev);
6f231dda 1304
5b6bf225 1305 if (status != SCI_SUCCESS)
a8a0a133 1306 dev_dbg(&ihost->pdev->dev,
5b6bf225 1307 "%s: isci_remote_device_reset_complete(%p) "
d06b487b 1308 "returned %d!\n", __func__, idev, status);
6f231dda 1309
d06b487b 1310 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
f41a0c44
DW
1311 out:
1312 sas_put_local_phy(phy);
d06b487b
DW
1313 return rc;
1314}
6f231dda 1315
d06b487b
DW
1316int isci_task_I_T_nexus_reset(struct domain_device *dev)
1317{
1318 struct isci_host *ihost = dev_to_ihost(dev);
d06b487b
DW
1319 struct isci_remote_device *idev;
1320 unsigned long flags;
bc6f387d 1321 int ret;
d06b487b 1322
d06b487b 1323 spin_lock_irqsave(&ihost->scic_lock, flags);
5b6bf225 1324 idev = isci_get_device(dev);
d06b487b
DW
1325 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1326
5a998328
DW
1327 if (!idev) {
1328 /* XXX: need to cleanup any ireqs targeting this
1329 * domain_device
1330 */
209fae14
DW
1331 ret = TMF_RESP_FUNC_COMPLETE;
1332 goto out;
1333 }
d06b487b 1334
92776991 1335 ret = isci_reset_device(ihost, dev, idev);
209fae14
DW
1336 out:
1337 isci_put_device(idev);
1338 return ret;
d06b487b 1339}
This page took 0.183826 seconds and 5 git commands to generate.