[SCSI] libsas: check for 'gone' expanders in smp_execute_task()
[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
16ba7709
DW
250static enum sci_status isci_sata_management_task_request_build(struct isci_request *ireq)
251{
252 struct isci_tmf *isci_tmf;
253 enum sci_status status;
254
3b34c169 255 if (!test_bit(IREQ_TMF, &ireq->flags))
16ba7709
DW
256 return SCI_FAILURE;
257
258 isci_tmf = isci_request_access_tmf(ireq);
259
260 switch (isci_tmf->tmf_code) {
261
262 case isci_tmf_sata_srst_high:
263 case isci_tmf_sata_srst_low: {
264 struct host_to_dev_fis *fis = &ireq->stp.cmd;
265
266 memset(fis, 0, sizeof(*fis));
267
268 fis->fis_type = 0x27;
269 fis->flags &= ~0x80;
270 fis->flags &= 0xF0;
271 if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
272 fis->control |= ATA_SRST;
273 else
274 fis->control &= ~ATA_SRST;
275 break;
276 }
277 /* other management commnd go here... */
278 default:
279 return SCI_FAILURE;
280 }
281
282 /* core builds the protocol specific request
283 * based on the h2d fis.
284 */
285 status = sci_task_request_construct_sata(ireq);
286
287 return status;
288}
289
0d0cf14c 290static struct isci_request *isci_task_request_build(struct isci_host *ihost,
209fae14 291 struct isci_remote_device *idev,
312e0c24 292 u16 tag, struct isci_tmf *isci_tmf)
6f231dda 293{
6f231dda 294 enum sci_status status = SCI_FAILURE;
0d0cf14c 295 struct isci_request *ireq = NULL;
a1a113b0 296 struct domain_device *dev;
6f231dda 297
0d0cf14c 298 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
299 "%s: isci_tmf = %p\n", __func__, isci_tmf);
300
0d0cf14c 301 dev = idev->domain_dev;
6f231dda
DW
302
303 /* do common allocation and init of request object. */
db056250 304 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
0d0cf14c
DW
305 if (!ireq)
306 return NULL;
6f231dda
DW
307
308 /* let the core do it's construct. */
89a7301f 309 status = sci_task_request_construct(ihost, idev, tag,
5076a1a9 310 ireq);
6f231dda
DW
311
312 if (status != SCI_SUCCESS) {
0d0cf14c 313 dev_warn(&ihost->pdev->dev,
89a7301f 314 "%s: sci_task_request_construct failed - "
6f231dda
DW
315 "status = 0x%x\n",
316 __func__,
317 status);
db056250 318 return NULL;
6f231dda
DW
319 }
320
a1a113b0
DW
321 /* XXX convert to get this from task->tproto like other drivers */
322 if (dev->dev_type == SAS_END_DEV) {
6f231dda 323 isci_tmf->proto = SAS_PROTOCOL_SSP;
89a7301f 324 status = sci_task_request_construct_ssp(ireq);
6f231dda 325 if (status != SCI_SUCCESS)
db056250 326 return NULL;
6f231dda
DW
327 }
328
a1a113b0 329 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
6f231dda 330 isci_tmf->proto = SAS_PROTOCOL_SATA;
0d0cf14c 331 status = isci_sata_management_task_request_build(ireq);
6f231dda
DW
332
333 if (status != SCI_SUCCESS)
db056250 334 return NULL;
6f231dda 335 }
0d0cf14c 336 return ireq;
6f231dda
DW
337}
338
b343dff1
JS
339/**
340* isci_request_mark_zombie() - This function must be called with scic_lock held.
341*/
342static void isci_request_mark_zombie(struct isci_host *ihost, struct isci_request *ireq)
343{
344 struct completion *tmf_completion = NULL;
345 struct completion *req_completion;
346
347 /* Set the request state to "dead". */
348 ireq->status = dead;
349
350 req_completion = ireq->io_request_completion;
351 ireq->io_request_completion = NULL;
352
3b34c169 353 if (test_bit(IREQ_TMF, &ireq->flags)) {
b343dff1
JS
354 /* Break links with the TMF request. */
355 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
356
357 /* In the case where a task request is dying,
358 * the thread waiting on the complete will sit and
359 * timeout unless we wake it now. Since the TMF
360 * has a default error status, complete it here
361 * to wake the waiting thread.
362 */
363 if (tmf) {
364 tmf_completion = tmf->complete;
365 tmf->complete = NULL;
366 }
367 ireq->ttype_ptr.tmf_task_ptr = NULL;
368 dev_dbg(&ihost->pdev->dev, "%s: tmf_code %d, managed tag %#x\n",
369 __func__, tmf->tmf_code, tmf->io_tag);
3b34c169
JS
370 } else {
371 /* Break links with the sas_task - the callback is done
372 * elsewhere.
373 */
374 struct sas_task *task = isci_request_access_task(ireq);
375
376 if (task)
377 task->lldd_task = NULL;
378
379 ireq->ttype_ptr.io_task_ptr = NULL;
b343dff1
JS
380 }
381
382 dev_warn(&ihost->pdev->dev, "task context unrecoverable (tag: %#x)\n",
383 ireq->io_tag);
384
385 /* Don't force waiting threads to timeout. */
386 if (req_completion)
387 complete(req_completion);
388
389 if (tmf_completion != NULL)
390 complete(tmf_completion);
391}
392
16ba7709
DW
393static int isci_task_execute_tmf(struct isci_host *ihost,
394 struct isci_remote_device *idev,
395 struct isci_tmf *tmf, unsigned long timeout_ms)
6f231dda
DW
396{
397 DECLARE_COMPLETION_ONSTACK(completion);
467e855a 398 enum sci_task_status status = SCI_TASK_FAILURE;
0d0cf14c 399 struct isci_request *ireq;
6f231dda
DW
400 int ret = TMF_RESP_FUNC_FAILED;
401 unsigned long flags;
fd18388b 402 unsigned long timeleft;
312e0c24
DW
403 u16 tag;
404
405 spin_lock_irqsave(&ihost->scic_lock, flags);
406 tag = isci_alloc_tag(ihost);
407 spin_unlock_irqrestore(&ihost->scic_lock, flags);
408
409 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
410 return ret;
6f231dda
DW
411
412 /* sanity check, return TMF_RESP_FUNC_FAILED
413 * if the device is not there and ready.
414 */
78a6f06e
DW
415 if (!idev ||
416 (!test_bit(IDEV_IO_READY, &idev->flags) &&
417 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
0d0cf14c 418 dev_dbg(&ihost->pdev->dev,
78a6f06e 419 "%s: idev = %p not ready (%#lx)\n",
6f231dda 420 __func__,
78a6f06e 421 idev, idev ? idev->flags : 0);
312e0c24 422 goto err_tci;
6f231dda 423 } else
0d0cf14c 424 dev_dbg(&ihost->pdev->dev,
78a6f06e
DW
425 "%s: idev = %p\n",
426 __func__, idev);
6f231dda
DW
427
428 /* Assign the pointer to the TMF's completion kernel wait structure. */
429 tmf->complete = &completion;
b343dff1 430 tmf->status = SCI_FAILURE_TIMEOUT;
6f231dda 431
78a6f06e 432 ireq = isci_task_request_build(ihost, idev, tag, tmf);
312e0c24
DW
433 if (!ireq)
434 goto err_tci;
6f231dda 435
0d0cf14c 436 spin_lock_irqsave(&ihost->scic_lock, flags);
6f231dda
DW
437
438 /* start the TMF io. */
89a7301f 439 status = sci_controller_start_task(ihost, idev, ireq);
6f231dda 440
467e855a 441 if (status != SCI_TASK_SUCCESS) {
a8a0a133 442 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
443 "%s: start_io failed - status = 0x%x, request = %p\n",
444 __func__,
445 status,
0d0cf14c
DW
446 ireq);
447 spin_unlock_irqrestore(&ihost->scic_lock, flags);
db056250 448 goto err_tci;
6f231dda
DW
449 }
450
6f231dda
DW
451 if (tmf->cb_state_func != NULL)
452 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
453
0d0cf14c 454 isci_request_change_state(ireq, started);
6f231dda
DW
455
456 /* add the request to the remote device request list. */
78a6f06e 457 list_add(&ireq->dev_node, &idev->reqs_in_process);
6f231dda 458
0d0cf14c 459 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
460
461 /* Wait for the TMF to complete, or a timeout. */
fd18388b 462 timeleft = wait_for_completion_timeout(&completion,
086a0dab 463 msecs_to_jiffies(timeout_ms));
fd18388b
EN
464
465 if (timeleft == 0) {
b343dff1
JS
466 /* The TMF did not complete - this could be because
467 * of an unplug. Terminate the TMF request now.
468 */
0d0cf14c 469 spin_lock_irqsave(&ihost->scic_lock, flags);
fd18388b
EN
470
471 if (tmf->cb_state_func != NULL)
b343dff1
JS
472 tmf->cb_state_func(isci_tmf_timed_out, tmf,
473 tmf->cb_data);
fd18388b 474
b343dff1 475 sci_controller_terminate_request(ihost, idev, ireq);
fd18388b 476
0d0cf14c 477 spin_unlock_irqrestore(&ihost->scic_lock, flags);
086a0dab 478
b343dff1
JS
479 timeleft = wait_for_completion_timeout(
480 &completion,
481 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
482
483 if (!timeleft) {
484 /* Strange condition - the termination of the TMF
485 * request timed-out.
486 */
487 spin_lock_irqsave(&ihost->scic_lock, flags);
488
489 /* If the TMF status has not changed, kill it. */
490 if (tmf->status == SCI_FAILURE_TIMEOUT)
491 isci_request_mark_zombie(ihost, ireq);
492
493 spin_unlock_irqrestore(&ihost->scic_lock, flags);
494 }
fd18388b 495 }
6f231dda 496
27234ab4 497 isci_print_tmf(ihost, tmf);
6f231dda
DW
498
499 if (tmf->status == SCI_SUCCESS)
500 ret = TMF_RESP_FUNC_COMPLETE;
501 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
0d0cf14c 502 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
503 "%s: tmf.status == "
504 "SCI_FAILURE_IO_RESPONSE_VALID\n",
505 __func__);
506 ret = TMF_RESP_FUNC_COMPLETE;
507 }
508 /* Else - leave the default "failed" status alone. */
509
0d0cf14c 510 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
511 "%s: completed request = %p\n",
512 __func__,
0d0cf14c 513 ireq);
6f231dda 514
6f231dda 515 return ret;
312e0c24 516
312e0c24
DW
517 err_tci:
518 spin_lock_irqsave(&ihost->scic_lock, flags);
519 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
520 spin_unlock_irqrestore(&ihost->scic_lock, flags);
521
522 return ret;
6f231dda
DW
523}
524
16ba7709
DW
525static void isci_task_build_tmf(struct isci_tmf *tmf,
526 enum isci_tmf_function_codes code,
527 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
528 struct isci_tmf *,
529 void *),
530 void *cb_data)
6f231dda 531{
6f231dda
DW
532 memset(tmf, 0, sizeof(*tmf));
533
6f231dda 534 tmf->tmf_code = code;
6f231dda 535 tmf->cb_state_func = tmf_sent_cb;
c3f42feb
JS
536 tmf->cb_data = cb_data;
537}
1fad9e93 538
16ba7709
DW
539static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
540 enum isci_tmf_function_codes code,
541 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
542 struct isci_tmf *,
543 void *),
544 struct isci_request *old_request)
c3f42feb 545{
16ba7709 546 isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
c3f42feb 547 tmf->io_tag = old_request->io_tag;
6f231dda
DW
548}
549
6f231dda
DW
550/**
551 * isci_task_validate_request_to_abort() - This function checks the given I/O
552 * against the "started" state. If the request is still "started", it's
553 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
554 * BEFORE CALLING THIS FUNCTION.
555 * @isci_request: This parameter specifies the request object to control.
556 * @isci_host: This parameter specifies the ISCI host object
557 * @isci_device: This is the device to which the request is pending.
558 * @aborted_io_completion: This is a completion structure that will be added to
559 * the request in case it is changed to aborting; this completion is
560 * triggered when the request is fully completed.
561 *
562 * Either "started" on successful change of the task status to "aborted", or
563 * "unallocated" if the task cannot be controlled.
564 */
565static enum isci_request_status isci_task_validate_request_to_abort(
566 struct isci_request *isci_request,
567 struct isci_host *isci_host,
568 struct isci_remote_device *isci_device,
569 struct completion *aborted_io_completion)
570{
571 enum isci_request_status old_state = unallocated;
572
573 /* Only abort the task if it's in the
574 * device's request_in_process list
575 */
576 if (isci_request && !list_empty(&isci_request->dev_node)) {
577 old_state = isci_request_change_started_to_aborted(
578 isci_request, aborted_io_completion);
579
6f231dda
DW
580 }
581
582 return old_state;
583}
584
d6891682
JS
585static int isci_request_is_dealloc_managed(enum isci_request_status stat)
586{
587 switch (stat) {
588 case aborted:
589 case aborting:
590 case terminating:
591 case completed:
592 case dead:
593 return true;
594 default:
595 return false;
596 }
597}
598
6f231dda
DW
599/**
600 * isci_terminate_request_core() - This function will terminate the given
601 * request, and wait for it to complete. This function must only be called
602 * from a thread that can wait. Note that the request is terminated and
603 * completed (back to the host, if started there).
d9dcb4ba 604 * @ihost: This SCU.
78a6f06e 605 * @idev: The target.
6f231dda
DW
606 * @isci_request: The I/O request to be terminated.
607 *
6f231dda 608 */
d9dcb4ba
DW
609static void isci_terminate_request_core(struct isci_host *ihost,
610 struct isci_remote_device *idev,
611 struct isci_request *isci_request)
6f231dda 612{
cbb65c66 613 enum sci_status status = SCI_SUCCESS;
6f231dda
DW
614 bool was_terminated = false;
615 bool needs_cleanup_handling = false;
77c852f3
JS
616 unsigned long flags;
617 unsigned long termination_completed = 1;
618 struct completion *io_request_completion;
6f231dda 619
d9dcb4ba 620 dev_dbg(&ihost->pdev->dev,
6f231dda 621 "%s: device = %p; request = %p\n",
78a6f06e 622 __func__, idev, isci_request);
6f231dda 623
d9dcb4ba 624 spin_lock_irqsave(&ihost->scic_lock, flags);
4dc043c4 625
77c852f3
JS
626 io_request_completion = isci_request->io_request_completion;
627
4dc043c4 628 /* Note that we are not going to control
38d8879b
DW
629 * the target to abort the request.
630 */
631 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
4dc043c4 632
6f231dda
DW
633 /* Make sure the request wasn't just sitting around signalling
634 * device condition (if the request handle is NULL, then the
635 * request completed but needed additional handling here).
636 */
38d8879b 637 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
6f231dda 638 was_terminated = true;
cbb65c66 639 needs_cleanup_handling = true;
89a7301f 640 status = sci_controller_terminate_request(ihost,
d9dcb4ba
DW
641 idev,
642 isci_request);
6f231dda 643 }
d9dcb4ba 644 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
645
646 /*
647 * The only time the request to terminate will
648 * fail is when the io request is completed and
649 * being aborted.
650 */
4dc043c4 651 if (status != SCI_SUCCESS) {
a8a0a133 652 dev_dbg(&ihost->pdev->dev,
89a7301f 653 "%s: sci_controller_terminate_request"
6f231dda 654 " returned = 0x%x\n",
77c852f3
JS
655 __func__, status);
656
4dc043c4
JS
657 isci_request->io_request_completion = NULL;
658
659 } else {
6f231dda 660 if (was_terminated) {
d9dcb4ba 661 dev_dbg(&ihost->pdev->dev,
77c852f3
JS
662 "%s: before completion wait (%p/%p)\n",
663 __func__, isci_request, io_request_completion);
6f231dda
DW
664
665 /* Wait here for the request to complete. */
77c852f3 666 termination_completed
4dc043c4 667 = wait_for_completion_timeout(
77c852f3 668 io_request_completion,
b343dff1 669 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
4dc043c4 670
77c852f3
JS
671 if (!termination_completed) {
672
673 /* The request to terminate has timed out. */
b343dff1 674 spin_lock_irqsave(&ihost->scic_lock, flags);
77c852f3
JS
675
676 /* Check for state changes. */
b343dff1
JS
677 if (!test_bit(IREQ_TERMINATED,
678 &isci_request->flags)) {
77c852f3
JS
679
680 /* The best we can do is to have the
681 * request die a silent death if it
682 * ever really completes.
77c852f3 683 */
b343dff1
JS
684 isci_request_mark_zombie(ihost,
685 isci_request);
686 needs_cleanup_handling = true;
77c852f3
JS
687 } else
688 termination_completed = 1;
689
d9dcb4ba 690 spin_unlock_irqrestore(&ihost->scic_lock,
77c852f3 691 flags);
4dc043c4 692
77c852f3 693 if (!termination_completed) {
4dc043c4 694
a8a0a133 695 dev_dbg(&ihost->pdev->dev,
77c852f3
JS
696 "%s: *** Timeout waiting for "
697 "termination(%p/%p)\n",
698 __func__, io_request_completion,
699 isci_request);
4dc043c4 700
77c852f3
JS
701 /* The request can no longer be referenced
702 * safely since it may go away if the
703 * termination every really does complete.
704 */
705 isci_request = NULL;
706 }
707 }
708 if (termination_completed)
d9dcb4ba 709 dev_dbg(&ihost->pdev->dev,
77c852f3
JS
710 "%s: after completion wait (%p/%p)\n",
711 __func__, isci_request, io_request_completion);
4dc043c4 712 }
4dc043c4 713
77c852f3 714 if (termination_completed) {
cbb65c66 715
77c852f3 716 isci_request->io_request_completion = NULL;
6f231dda 717
77c852f3
JS
718 /* Peek at the status of the request. This will tell
719 * us if there was special handling on the request such that it
720 * needs to be detached and freed here.
721 */
722 spin_lock_irqsave(&isci_request->state_lock, flags);
d6891682
JS
723
724 needs_cleanup_handling
725 = isci_request_is_dealloc_managed(
726 isci_request->status);
727
77c852f3 728 spin_unlock_irqrestore(&isci_request->state_lock, flags);
6f231dda 729
77c852f3 730 }
db49c2d0
JS
731 if (needs_cleanup_handling) {
732
733 dev_dbg(&ihost->pdev->dev,
734 "%s: cleanup isci_device=%p, request=%p\n",
735 __func__, idev, isci_request);
736
737 if (isci_request != NULL) {
738 spin_lock_irqsave(&ihost->scic_lock, flags);
739 isci_free_tag(ihost, isci_request->io_tag);
740 isci_request_change_state(isci_request, unallocated);
741 list_del_init(&isci_request->dev_node);
742 spin_unlock_irqrestore(&ihost->scic_lock, flags);
743 }
744 }
a5fde225 745 }
6f231dda
DW
746}
747
748/**
749 * isci_terminate_pending_requests() - This function will change the all of the
750 * requests on the given device's state to "aborting", will terminate the
751 * requests, and wait for them to complete. This function must only be
752 * called from a thread that can wait. Note that the requests are all
753 * terminated and completed (back to the host, if started there).
754 * @isci_host: This parameter specifies SCU.
78a6f06e 755 * @idev: This parameter specifies the target.
6f231dda 756 *
6f231dda 757 */
980d3aeb
DW
758void isci_terminate_pending_requests(struct isci_host *ihost,
759 struct isci_remote_device *idev)
6f231dda 760{
980d3aeb 761 struct completion request_completion;
77c852f3 762 enum isci_request_status old_state;
980d3aeb
DW
763 unsigned long flags;
764 LIST_HEAD(list);
6f231dda 765
980d3aeb
DW
766 spin_lock_irqsave(&ihost->scic_lock, flags);
767 list_splice_init(&idev->reqs_in_process, &list);
6f231dda 768
980d3aeb
DW
769 /* assumes that isci_terminate_request_core deletes from the list */
770 while (!list_empty(&list)) {
771 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
6f231dda 772
980d3aeb
DW
773 /* Change state to "terminating" if it is currently
774 * "started".
775 */
776 old_state = isci_request_change_started_to_newstate(ireq,
777 &request_completion,
778 terminating);
779 switch (old_state) {
780 case started:
781 case completed:
782 case aborting:
783 break;
784 default:
785 /* termination in progress, or otherwise dispositioned.
786 * We know the request was on 'list' so should be safe
787 * to move it back to reqs_in_process
788 */
789 list_move(&ireq->dev_node, &idev->reqs_in_process);
790 ireq = NULL;
791 break;
792 }
4dc043c4 793
980d3aeb
DW
794 if (!ireq)
795 continue;
796 spin_unlock_irqrestore(&ihost->scic_lock, flags);
77c852f3 797
980d3aeb 798 init_completion(&request_completion);
77c852f3 799
980d3aeb
DW
800 dev_dbg(&ihost->pdev->dev,
801 "%s: idev=%p request=%p; task=%p old_state=%d\n",
802 __func__, idev, ireq,
3b34c169
JS
803 (!test_bit(IREQ_TMF, &ireq->flags)
804 ? isci_request_access_task(ireq)
805 : NULL),
980d3aeb
DW
806 old_state);
807
808 /* If the old_state is started:
809 * This request was not already being aborted. If it had been,
810 * then the aborting I/O (ie. the TMF request) would not be in
811 * the aborting state, and thus would be terminated here. Note
812 * that since the TMF completion's call to the kernel function
813 * "complete()" does not happen until the pending I/O request
814 * terminate fully completes, we do not have to implement a
815 * special wait here for already aborting requests - the
816 * termination of the TMF request will force the request
817 * to finish it's already started terminate.
818 *
819 * If old_state == completed:
820 * This request completed from the SCU hardware perspective
821 * and now just needs cleaning up in terms of freeing the
822 * request and potentially calling up to libsas.
823 *
824 * If old_state == aborting:
825 * This request has already gone through a TMF timeout, but may
826 * not have been terminated; needs cleaning up at least.
827 */
828 isci_terminate_request_core(ihost, idev, ireq);
829 spin_lock_irqsave(&ihost->scic_lock, flags);
4dc043c4 830 }
980d3aeb 831 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda
DW
832}
833
834/**
835 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
836 * Template functions.
837 * @lun: This parameter specifies the lun to be reset.
838 *
839 * status, zero indicates success.
840 */
841static int isci_task_send_lu_reset_sas(
842 struct isci_host *isci_host,
843 struct isci_remote_device *isci_device,
844 u8 *lun)
845{
846 struct isci_tmf tmf;
847 int ret = TMF_RESP_FUNC_FAILED;
848
849 dev_dbg(&isci_host->pdev->dev,
850 "%s: isci_host = %p, isci_device = %p\n",
851 __func__, isci_host, isci_device);
852 /* Send the LUN reset to the target. By the time the call returns,
853 * the TMF has fully exected in the target (in which case the return
854 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
855 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
856 */
209fae14 857 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
6f231dda
DW
858
859 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
209fae14 860 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
6f231dda
DW
861
862 if (ret == TMF_RESP_FUNC_COMPLETE)
863 dev_dbg(&isci_host->pdev->dev,
864 "%s: %p: TMF_LU_RESET passed\n",
865 __func__, isci_device);
866 else
867 dev_dbg(&isci_host->pdev->dev,
868 "%s: %p: TMF_LU_RESET failed (%x)\n",
869 __func__, isci_device, ret);
870
871 return ret;
872}
873
16ba7709
DW
874static int isci_task_send_lu_reset_sata(struct isci_host *ihost,
875 struct isci_remote_device *idev, u8 *lun)
876{
877 int ret = TMF_RESP_FUNC_FAILED;
878 struct isci_tmf tmf;
879
880 /* Send the soft reset to the target */
881 #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
882 isci_task_build_tmf(&tmf, isci_tmf_sata_srst_high, NULL, NULL);
883
884 ret = isci_task_execute_tmf(ihost, idev, &tmf, ISCI_SRST_TIMEOUT_MS);
885
886 if (ret != TMF_RESP_FUNC_COMPLETE) {
a8a0a133 887 dev_dbg(&ihost->pdev->dev,
16ba7709
DW
888 "%s: Assert SRST failed (%p) = %x",
889 __func__, idev, ret);
890
891 /* Return the failure so that the LUN reset is escalated
892 * to a target reset.
893 */
894 }
895 return ret;
896}
897
6f231dda
DW
898/**
899 * isci_task_lu_reset() - This function is one of the SAS Domain Template
900 * functions. This is one of the Task Management functoins called by libsas,
901 * to reset the given lun. Note the assumption that while this call is
902 * executing, no I/O will be sent by the host to the device.
903 * @lun: This parameter specifies the lun to be reset.
904 *
905 * status, zero indicates success.
906 */
4393aa4e 907int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
6f231dda 908{
4393aa4e 909 struct isci_host *isci_host = dev_to_ihost(domain_device);
209fae14
DW
910 struct isci_remote_device *isci_device;
911 unsigned long flags;
6f231dda 912 int ret;
6f231dda 913
209fae14
DW
914 spin_lock_irqsave(&isci_host->scic_lock, flags);
915 isci_device = isci_lookup_device(domain_device);
916 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
6f231dda 917
4393aa4e
DW
918 dev_dbg(&isci_host->pdev->dev,
919 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
6f231dda
DW
920 __func__, domain_device, isci_host, isci_device);
921
98145cb7
JS
922 if (!isci_device) {
923 /* If the device is gone, stop the escalations. */
924 dev_dbg(&isci_host->pdev->dev, "%s: No dev\n", __func__);
6f231dda 925
98145cb7 926 ret = TMF_RESP_FUNC_COMPLETE;
209fae14 927 goto out;
6f231dda 928 }
98145cb7 929 set_bit(IDEV_EH, &isci_device->flags);
6f231dda 930
6f231dda
DW
931 /* Send the task management part of the reset. */
932 if (sas_protocol_ata(domain_device->tproto)) {
4393aa4e 933 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
6f231dda
DW
934 } else
935 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
936
937 /* If the LUN reset worked, all the I/O can now be terminated. */
938 if (ret == TMF_RESP_FUNC_COMPLETE)
939 /* Terminate all I/O now. */
940 isci_terminate_pending_requests(isci_host,
980d3aeb 941 isci_device);
6f231dda 942
209fae14
DW
943 out:
944 isci_put_device(isci_device);
6f231dda
DW
945 return ret;
946}
947
948
949/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
950int isci_task_clear_nexus_port(struct asd_sas_port *port)
951{
952 return TMF_RESP_FUNC_FAILED;
953}
954
955
956
957int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
958{
959 return TMF_RESP_FUNC_FAILED;
960}
961
6f231dda
DW
962/* Task Management Functions. Must be called from process context. */
963
964/**
965 * isci_abort_task_process_cb() - This is a helper function for the abort task
966 * TMF command. It manages the request state with respect to the successful
967 * transmission / completion of the abort task request.
968 * @cb_state: This parameter specifies when this function was called - after
969 * the TMF request has been started and after it has timed-out.
970 * @tmf: This parameter specifies the TMF in progress.
971 *
972 *
973 */
974static void isci_abort_task_process_cb(
975 enum isci_tmf_cb_state cb_state,
976 struct isci_tmf *tmf,
977 void *cb_data)
978{
979 struct isci_request *old_request;
980
981 old_request = (struct isci_request *)cb_data;
982
983 dev_dbg(&old_request->isci_host->pdev->dev,
984 "%s: tmf=%p, old_request=%p\n",
985 __func__, tmf, old_request);
986
987 switch (cb_state) {
988
989 case isci_tmf_started:
990 /* The TMF has been started. Nothing to do here, since the
991 * request state was already set to "aborted" by the abort
992 * task function.
993 */
6cb4d6b3
BB
994 if ((old_request->status != aborted)
995 && (old_request->status != completed))
a8a0a133 996 dev_dbg(&old_request->isci_host->pdev->dev,
6cb4d6b3
BB
997 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
998 __func__, old_request->status, tmf, old_request);
6f231dda
DW
999 break;
1000
1001 case isci_tmf_timed_out:
1002
1003 /* Set the task's state to "aborting", since the abort task
1004 * function thread set it to "aborted" (above) in anticipation
1005 * of the task management request working correctly. Since the
1006 * timeout has now fired, the TMF request failed. We set the
1007 * state such that the request completion will indicate the
1008 * device is no longer present.
1009 */
1010 isci_request_change_state(old_request, aborting);
1011 break;
1012
1013 default:
a8a0a133 1014 dev_dbg(&old_request->isci_host->pdev->dev,
6f231dda
DW
1015 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1016 __func__, cb_state, tmf, old_request);
1017 break;
1018 }
1019}
1020
1021/**
1022 * isci_task_abort_task() - This function is one of the SAS Domain Template
1023 * functions. This function is called by libsas to abort a specified task.
1024 * @task: This parameter specifies the SAS task to abort.
1025 *
1026 * status, zero indicates success.
1027 */
1028int isci_task_abort_task(struct sas_task *task)
1029{
4393aa4e 1030 struct isci_host *isci_host = dev_to_ihost(task->dev);
6f231dda 1031 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
a5fde225
JS
1032 struct isci_request *old_request = NULL;
1033 enum isci_request_status old_state;
6f231dda 1034 struct isci_remote_device *isci_device = NULL;
a5fde225
JS
1035 struct isci_tmf tmf;
1036 int ret = TMF_RESP_FUNC_FAILED;
1037 unsigned long flags;
98145cb7 1038 int perform_termination = 0;
6f231dda
DW
1039
1040 /* Get the isci_request reference from the task. Note that
1041 * this check does not depend on the pending request list
1042 * in the device, because tasks driving resets may land here
1043 * after completion in the core.
1044 */
209fae14
DW
1045 spin_lock_irqsave(&isci_host->scic_lock, flags);
1046 spin_lock(&task->task_state_lock);
1047
1048 old_request = task->lldd_task;
1049
1050 /* If task is already done, the request isn't valid */
1051 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
1052 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
1053 old_request)
1054 isci_device = isci_lookup_device(task->dev);
1055
1056 spin_unlock(&task->task_state_lock);
1057 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
6f231dda
DW
1058
1059 dev_dbg(&isci_host->pdev->dev,
98145cb7
JS
1060 "%s: dev = %p, task = %p, old_request == %p\n",
1061 __func__, isci_device, task, old_request);
6f231dda 1062
98145cb7
JS
1063 if (isci_device)
1064 set_bit(IDEV_EH, &isci_device->flags);
6f231dda 1065
98145cb7
JS
1066 /* Device reset conditions signalled in task_state_flags are the
1067 * responsbility of libsas to observe at the start of the error
1068 * handler thread.
6f231dda 1069 */
98145cb7
JS
1070 if (!isci_device || !old_request) {
1071 /* The request has already completed and there
1072 * is nothing to do here other than to set the task
1073 * done bit, and indicate that the task abort function
1074 * was sucessful.
1075 */
1076 spin_lock_irqsave(&task->task_state_lock, flags);
1077 task->task_state_flags |= SAS_TASK_STATE_DONE;
1078 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1079 SAS_TASK_STATE_PENDING);
1080 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda 1081
98145cb7 1082 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 1083
98145cb7
JS
1084 dev_dbg(&isci_host->pdev->dev,
1085 "%s: abort task not needed for %p\n",
1086 __func__, task);
209fae14 1087 goto out;
a5ec7f86 1088 }
6f231dda
DW
1089
1090 spin_lock_irqsave(&isci_host->scic_lock, flags);
1091
f219f010 1092 /* Check the request status and change to "aborted" if currently
a5fde225 1093 * "starting"; if true then set the I/O kernel completion
6f231dda
DW
1094 * struct that will be triggered when the request completes.
1095 */
a5fde225
JS
1096 old_state = isci_task_validate_request_to_abort(
1097 old_request, isci_host, isci_device,
1098 &aborted_io_completion);
f219f010
JS
1099 if ((old_state != started) &&
1100 (old_state != completed) &&
1101 (old_state != aborting)) {
6f231dda
DW
1102
1103 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1104
a5fde225
JS
1105 /* The request was already being handled by someone else (because
1106 * they got to set the state away from started).
1107 */
1108 dev_dbg(&isci_host->pdev->dev,
1109 "%s: device = %p; old_request %p already being aborted\n",
1110 __func__,
1111 isci_device, old_request);
209fae14
DW
1112 ret = TMF_RESP_FUNC_COMPLETE;
1113 goto out;
6f231dda 1114 }
38d8879b 1115 if (task->task_proto == SAS_PROTOCOL_SMP ||
98145cb7 1116 sas_protocol_ata(task->task_proto) ||
38d8879b 1117 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
6f231dda
DW
1118
1119 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1120
a5fde225 1121 dev_dbg(&isci_host->pdev->dev,
98145cb7 1122 "%s: %s request"
a5fde225 1123 " or complete_in_target (%d), thus no TMF\n",
98145cb7
JS
1124 __func__,
1125 ((task->task_proto == SAS_PROTOCOL_SMP)
1126 ? "SMP"
1127 : (sas_protocol_ata(task->task_proto)
1128 ? "SATA/STP"
1129 : "<other>")
1130 ),
38d8879b 1131 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
a5fde225 1132
98145cb7
JS
1133 if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
1134 spin_lock_irqsave(&task->task_state_lock, flags);
1135 task->task_state_flags |= SAS_TASK_STATE_DONE;
1136 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1137 SAS_TASK_STATE_PENDING);
1138 spin_unlock_irqrestore(&task->task_state_lock, flags);
1139 ret = TMF_RESP_FUNC_COMPLETE;
1140 } else {
1141 spin_lock_irqsave(&task->task_state_lock, flags);
1142 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1143 SAS_TASK_STATE_PENDING);
1144 spin_unlock_irqrestore(&task->task_state_lock, flags);
1145 }
6f231dda 1146
98145cb7
JS
1147 /* STP and SMP devices are not sent a TMF, but the
1148 * outstanding I/O request is terminated below. This is
1149 * because SATA/STP and SMP discovery path timeouts directly
1150 * call the abort task interface for cleanup.
6f231dda 1151 */
98145cb7
JS
1152 perform_termination = 1;
1153
6f231dda
DW
1154 } else {
1155 /* Fill in the tmf stucture */
209fae14 1156 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
c3f42feb
JS
1157 isci_abort_task_process_cb,
1158 old_request);
6f231dda 1159
6f231dda
DW
1160 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1161
98145cb7 1162 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
209fae14 1163 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
6f231dda
DW
1164 ISCI_ABORT_TASK_TIMEOUT_MS);
1165
98145cb7
JS
1166 if (ret == TMF_RESP_FUNC_COMPLETE)
1167 perform_termination = 1;
1168 else
a8a0a133 1169 dev_dbg(&isci_host->pdev->dev,
98145cb7 1170 "%s: isci_task_send_tmf failed\n", __func__);
6f231dda 1171 }
98145cb7 1172 if (perform_termination) {
38d8879b 1173 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
a5fde225 1174
77c852f3
JS
1175 /* Clean up the request on our side, and wait for the aborted
1176 * I/O to complete.
1177 */
98145cb7
JS
1178 isci_terminate_request_core(isci_host, isci_device,
1179 old_request);
a5fde225 1180 }
6f231dda 1181
a5fde225
JS
1182 /* Make sure we do not leave a reference to aborted_io_completion */
1183 old_request->io_request_completion = NULL;
209fae14
DW
1184 out:
1185 isci_put_device(isci_device);
6f231dda
DW
1186 return ret;
1187}
1188
1189/**
1190 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1191 * functions. This is one of the Task Management functoins called by libsas,
1192 * to abort all task for the given lun.
1193 * @d_device: This parameter specifies the domain device associated with this
1194 * request.
1195 * @lun: This parameter specifies the lun associated with this request.
1196 *
1197 * status, zero indicates success.
1198 */
1199int isci_task_abort_task_set(
1200 struct domain_device *d_device,
1201 u8 *lun)
1202{
1203 return TMF_RESP_FUNC_FAILED;
1204}
1205
1206
1207/**
1208 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1209 * functions. This is one of the Task Management functoins called by libsas.
1210 * @d_device: This parameter specifies the domain device associated with this
1211 * request.
1212 * @lun: This parameter specifies the lun associated with this request.
1213 *
1214 * status, zero indicates success.
1215 */
1216int isci_task_clear_aca(
1217 struct domain_device *d_device,
1218 u8 *lun)
1219{
1220 return TMF_RESP_FUNC_FAILED;
1221}
1222
1223
1224
1225/**
1226 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1227 * functions. This is one of the Task Management functoins called by libsas.
1228 * @d_device: This parameter specifies the domain device associated with this
1229 * request.
1230 * @lun: This parameter specifies the lun associated with this request.
1231 *
1232 * status, zero indicates success.
1233 */
1234int isci_task_clear_task_set(
1235 struct domain_device *d_device,
1236 u8 *lun)
1237{
1238 return TMF_RESP_FUNC_FAILED;
1239}
1240
1241
1242/**
1243 * isci_task_query_task() - This function is implemented to cause libsas to
1244 * correctly escalate the failed abort to a LUN or target reset (this is
1245 * because sas_scsi_find_task libsas function does not correctly interpret
1246 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1247 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1248 * returned, libsas will turn this into a target reset
1249 * @task: This parameter specifies the sas task being queried.
1250 * @lun: This parameter specifies the lun associated with this request.
1251 *
1252 * status, zero indicates success.
1253 */
1254int isci_task_query_task(
1255 struct sas_task *task)
1256{
1257 /* See if there is a pending device reset for this device. */
1258 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1259 return TMF_RESP_FUNC_FAILED;
1260 else
1261 return TMF_RESP_FUNC_SUCC;
1262}
1263
af5ae893 1264/*
6f231dda
DW
1265 * isci_task_request_complete() - This function is called by the sci core when
1266 * an task request completes.
af5ae893
DJ
1267 * @ihost: This parameter specifies the ISCI host object
1268 * @ireq: This parameter is the completed isci_request object.
6f231dda
DW
1269 * @completion_status: This parameter specifies the completion status from the
1270 * sci core.
1271 *
1272 * none.
1273 */
af5ae893
DJ
1274void
1275isci_task_request_complete(struct isci_host *ihost,
1276 struct isci_request *ireq,
1277 enum sci_task_status completion_status)
6f231dda 1278{
af5ae893 1279 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
b343dff1
JS
1280 struct completion *tmf_complete = NULL;
1281 struct completion *request_complete = ireq->io_request_completion;
6f231dda 1282
af5ae893 1283 dev_dbg(&ihost->pdev->dev,
6f231dda 1284 "%s: request = %p, status=%d\n",
af5ae893 1285 __func__, ireq, completion_status);
6f231dda 1286
8d2c65c0 1287 isci_request_change_state(ireq, completed);
6f231dda 1288
38d8879b 1289 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
6f231dda 1290
b343dff1
JS
1291 if (tmf) {
1292 tmf->status = completion_status;
1293
1294 if (tmf->proto == SAS_PROTOCOL_SSP) {
1295 memcpy(&tmf->resp.resp_iu,
1296 &ireq->ssp.rsp,
1297 SSP_RESP_IU_MAX_SIZE);
1298 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
1299 memcpy(&tmf->resp.d2h_fis,
1300 &ireq->stp.rsp,
1301 sizeof(struct dev_to_host_fis));
1302 }
1303 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1304 tmf_complete = tmf->complete;
6f231dda 1305 }
89a7301f 1306 sci_controller_complete_io(ihost, ireq->target_device, ireq);
67ea838d 1307 /* set the 'terminated' flag handle to make sure it cannot be terminated
6f231dda
DW
1308 * or completed again.
1309 */
38d8879b 1310 set_bit(IREQ_TERMINATED, &ireq->flags);
6f231dda 1311
d6891682
JS
1312 /* As soon as something is in the terminate path, deallocation is
1313 * managed there. Note that the final non-managed state of a task
1314 * request is "completed".
1315 */
1316 if ((ireq->status == completed) ||
1317 !isci_request_is_dealloc_managed(ireq->status)) {
1318 isci_request_change_state(ireq, unallocated);
1319 isci_free_tag(ihost, ireq->io_tag);
1320 list_del_init(&ireq->dev_node);
1321 }
6f231dda 1322
b343dff1
JS
1323 /* "request_complete" is set if the task was being terminated. */
1324 if (request_complete)
1325 complete(request_complete);
1326
6f231dda 1327 /* The task management part completes last. */
b343dff1
JS
1328 if (tmf_complete)
1329 complete(tmf_complete);
6f231dda
DW
1330}
1331
209fae14 1332static int isci_reset_device(struct isci_host *ihost,
bc6f387d 1333 struct isci_remote_device *idev)
6f231dda 1334{
209fae14 1335 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
6f231dda 1336 enum sci_status status;
d06b487b
DW
1337 unsigned long flags;
1338 int rc;
6f231dda 1339
d06b487b 1340 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
6f231dda 1341
d06b487b 1342 spin_lock_irqsave(&ihost->scic_lock, flags);
89a7301f 1343 status = sci_remote_device_reset(idev);
6f231dda 1344 if (status != SCI_SUCCESS) {
d06b487b 1345 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1346
a8a0a133 1347 dev_dbg(&ihost->pdev->dev,
89a7301f 1348 "%s: sci_remote_device_reset(%p) returned %d!\n",
d06b487b 1349 __func__, idev, status);
6f231dda
DW
1350
1351 return TMF_RESP_FUNC_FAILED;
1352 }
d06b487b 1353 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1354
bc6f387d 1355 rc = sas_phy_reset(phy, true);
6f231dda 1356
d06b487b
DW
1357 /* Terminate in-progress I/O now. */
1358 isci_remote_device_nuke_requests(ihost, idev);
6f231dda 1359
ff717ab0 1360 /* Since all pending TCs have been cleaned, resume the RNC. */
d06b487b 1361 spin_lock_irqsave(&ihost->scic_lock, flags);
89a7301f 1362 status = sci_remote_device_reset_complete(idev);
d06b487b 1363 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1364
d06b487b 1365 if (status != SCI_SUCCESS) {
a8a0a133 1366 dev_dbg(&ihost->pdev->dev,
89a7301f 1367 "%s: sci_remote_device_reset_complete(%p) "
d06b487b 1368 "returned %d!\n", __func__, idev, status);
6f231dda
DW
1369 }
1370
d06b487b 1371 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
6f231dda 1372
d06b487b
DW
1373 return rc;
1374}
6f231dda 1375
d06b487b
DW
1376int isci_task_I_T_nexus_reset(struct domain_device *dev)
1377{
1378 struct isci_host *ihost = dev_to_ihost(dev);
d06b487b
DW
1379 struct isci_remote_device *idev;
1380 unsigned long flags;
bc6f387d 1381 int ret;
d06b487b 1382
d06b487b 1383 spin_lock_irqsave(&ihost->scic_lock, flags);
209fae14 1384 idev = isci_lookup_device(dev);
d06b487b
DW
1385 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1386
209fae14
DW
1387 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1388 ret = TMF_RESP_FUNC_COMPLETE;
1389 goto out;
1390 }
d06b487b 1391
bc6f387d 1392 ret = isci_reset_device(ihost, idev);
209fae14
DW
1393 out:
1394 isci_put_device(idev);
1395 return ret;
d06b487b
DW
1396}
1397
1398int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1399{
1400 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
209fae14
DW
1401 struct isci_host *ihost = dev_to_ihost(dev);
1402 struct isci_remote_device *idev;
209fae14 1403 unsigned long flags;
bc6f387d 1404 int ret;
6f231dda 1405
209fae14
DW
1406 spin_lock_irqsave(&ihost->scic_lock, flags);
1407 idev = isci_lookup_device(dev);
1408 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1409
1410 if (!idev) {
1411 ret = TMF_RESP_FUNC_COMPLETE;
1412 goto out;
1413 }
1414
bc6f387d 1415 ret = isci_reset_device(ihost, idev);
209fae14
DW
1416 out:
1417 isci_put_device(idev);
1418 return ret;
6f231dda 1419}
This page took 0.232039 seconds and 5 git commands to generate.