isci: Handle timed-out request terminations correctly
[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"
64#include "sata.h"
65#include "task.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) {
50e7f9b5
DW
91 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
1077a574 93 dev_dbg(&ihost->pdev->dev,
ed8a72d1
JS
94 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
50e7f9b5
DW
96 __func__, task, response, status);
97
1077a574 98 task->lldd_task = NULL;
50e7f9b5 99
ed8a72d1 100 isci_execpath_callback(ihost, task, task->task_done);
50e7f9b5
DW
101 break;
102
103 case isci_perform_aborted_io_completion:
104 /* No notification because this request is already in the
105 * abort path.
106 */
1077a574 107 dev_warn(&ihost->pdev->dev,
ed8a72d1
JS
108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
50e7f9b5
DW
110 __func__, task, response, status);
111 break;
112
113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
1077a574 115 dev_warn(&ihost->pdev->dev,
ed8a72d1
JS
116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
50e7f9b5 118 __func__, task, response, status);
ed8a72d1
JS
119
120 isci_execpath_callback(ihost, task, sas_task_abort);
50e7f9b5
DW
121 break;
122
123 default:
1077a574 124 dev_warn(&ihost->pdev->dev,
50e7f9b5
DW
125 "%s: isci task notification default case!",
126 __func__);
127 sas_task_abort(task);
128 break;
129 }
130}
6f231dda 131
1077a574
DW
132#define for_each_sas_task(num, task) \
133 for (; num > 0; num--,\
134 task = list_entry(task->list.next, struct sas_task, list))
135
6f231dda
DW
136/**
137 * isci_task_execute_task() - This function is one of the SAS Domain Template
138 * functions. This function is called by libsas to send a task down to
139 * hardware.
140 * @task: This parameter specifies the SAS task to send.
141 * @num: This parameter specifies the number of tasks to queue.
142 * @gfp_flags: This parameter specifies the context of this call.
143 *
144 * status, zero indicates success.
145 */
146int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
147{
4393aa4e 148 struct isci_host *ihost = dev_to_ihost(task->dev);
6f231dda
DW
149 struct isci_request *request = NULL;
150 struct isci_remote_device *device;
151 unsigned long flags;
6f231dda
DW
152 int ret;
153 enum sci_status status;
f0846c68 154 enum isci_status device_status;
6f231dda 155
1077a574 156 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
6f231dda
DW
157
158 /* Check if we have room for more tasks */
1077a574 159 ret = isci_host_can_queue(ihost, num);
6f231dda
DW
160
161 if (ret) {
1077a574 162 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
6f231dda
DW
163 return ret;
164 }
165
1077a574
DW
166 for_each_sas_task(num, task) {
167 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
168 "task = %p, num = %d; dev = %p; cmd = %p\n",
169 task, num, task->dev, task->uldd_task);
170
4393aa4e 171 device = task->dev->lldd_dev;
6f231dda 172
f0846c68
JS
173 if (device)
174 device_status = device->status;
175 else
176 device_status = isci_freed;
177
178 /* From this point onward, any process that needs to guarantee
179 * that there is no kernel I/O being started will have to wait
180 * for the quiesce spinlock.
181 */
182
183 if (device_status != isci_ready_for_io) {
6f231dda
DW
184
185 /* Forces a retry from scsi mid layer. */
d2d61433
DJ
186 dev_dbg(&ihost->pdev->dev,
187 "%s: task %p: isci_host->status = %d, "
188 "device = %p; device_status = 0x%x\n\n",
189 __func__,
190 task,
191 isci_host_get_state(ihost),
192 device,
193 device_status);
6f231dda 194
f0846c68
JS
195 if (device_status == isci_ready) {
196 /* Indicate QUEUE_FULL so that the scsi midlayer
197 * retries.
198 */
1077a574
DW
199 isci_task_refuse(ihost, task,
200 SAS_TASK_COMPLETE,
201 SAS_QUEUE_FULL);
f0846c68
JS
202 } else {
203 /* Else, the device is going down. */
1077a574
DW
204 isci_task_refuse(ihost, task,
205 SAS_TASK_UNDELIVERED,
206 SAS_DEVICE_UNKNOWN);
f0846c68 207 }
1077a574 208 isci_host_can_dequeue(ihost, 1);
f0846c68
JS
209 } else {
210 /* There is a device and it's ready for I/O. */
211 spin_lock_irqsave(&task->task_state_lock, flags);
6f231dda 212
f0846c68 213 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
6f231dda 214
f0846c68
JS
215 spin_unlock_irqrestore(&task->task_state_lock,
216 flags);
6f231dda 217
1077a574
DW
218 isci_task_refuse(ihost, task,
219 SAS_TASK_UNDELIVERED,
220 SAM_STAT_TASK_ABORTED);
6f231dda 221
f0846c68 222 /* The I/O was aborted. */
6f231dda 223
f0846c68 224 } else {
6f231dda
DW
225 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
226 spin_unlock_irqrestore(&task->task_state_lock, flags);
f0846c68
JS
227
228 /* build and send the request. */
1077a574 229 status = isci_request_execute(ihost, task, &request,
f0846c68
JS
230 gfp_flags);
231
232 if (status != SCI_SUCCESS) {
233
234 spin_lock_irqsave(&task->task_state_lock, flags);
235 /* Did not really start this command. */
236 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
237 spin_unlock_irqrestore(&task->task_state_lock, flags);
238
239 /* Indicate QUEUE_FULL so that the scsi
240 * midlayer retries. if the request
241 * failed for remote device reasons,
242 * it gets returned as
243 * SAS_TASK_UNDELIVERED next time
244 * through.
245 */
1077a574
DW
246 isci_task_refuse(ihost, task,
247 SAS_TASK_COMPLETE,
248 SAS_QUEUE_FULL);
249 isci_host_can_dequeue(ihost, 1);
f0846c68 250 }
6f231dda
DW
251 }
252 }
1077a574 253 }
6f231dda
DW
254 return 0;
255}
256
257
258
259/**
260 * isci_task_request_build() - This function builds the task request object.
261 * @isci_host: This parameter specifies the ISCI host object
262 * @request: This parameter points to the isci_request object allocated in the
263 * request construct function.
264 * @tmf: This parameter is the task management struct to be built
265 *
266 * SCI_SUCCESS on successfull completion, or specific failure code.
267 */
268static enum sci_status isci_task_request_build(
269 struct isci_host *isci_host,
270 struct isci_request **isci_request,
271 struct isci_tmf *isci_tmf)
272{
273 struct scic_sds_remote_device *sci_device;
274 enum sci_status status = SCI_FAILURE;
6cb4d6b3 275 struct isci_request *request = NULL;
6f231dda 276 struct isci_remote_device *isci_device;
a1a113b0 277 struct domain_device *dev;
6f231dda
DW
278
279 dev_dbg(&isci_host->pdev->dev,
280 "%s: isci_tmf = %p\n", __func__, isci_tmf);
281
282 isci_device = isci_tmf->device;
57f20f4e 283 sci_device = &isci_device->sci;
a1a113b0 284 dev = isci_device->domain_dev;
6f231dda
DW
285
286 /* do common allocation and init of request object. */
287 status = isci_request_alloc_tmf(
288 isci_host,
289 isci_tmf,
290 &request,
291 isci_device,
292 GFP_ATOMIC
293 );
294
295 if (status != SCI_SUCCESS)
296 goto out;
297
298 /* let the core do it's construct. */
0d84366f
DW
299 status = scic_task_request_construct(&isci_host->sci, sci_device,
300 SCI_CONTROLLER_INVALID_IO_TAG,
67ea838d 301 &request->sci);
6f231dda
DW
302
303 if (status != SCI_SUCCESS) {
304 dev_warn(&isci_host->pdev->dev,
305 "%s: scic_task_request_construct failed - "
306 "status = 0x%x\n",
307 __func__,
308 status);
309 goto errout;
310 }
311
a1a113b0
DW
312 /* XXX convert to get this from task->tproto like other drivers */
313 if (dev->dev_type == SAS_END_DEV) {
6f231dda 314 isci_tmf->proto = SAS_PROTOCOL_SSP;
67ea838d 315 status = scic_task_request_construct_ssp(&request->sci);
6f231dda
DW
316 if (status != SCI_SUCCESS)
317 goto errout;
318 }
319
a1a113b0 320 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
6f231dda
DW
321 isci_tmf->proto = SAS_PROTOCOL_SATA;
322 status = isci_sata_management_task_request_build(request);
323
324 if (status != SCI_SUCCESS)
325 goto errout;
326 }
327
328 goto out;
329
330 errout:
331
332 /* release the dma memory if we fail. */
333 isci_request_free(isci_host, request);
334 request = NULL;
335
336 out:
337 *isci_request = request;
338 return status;
339}
340
6f231dda
DW
341/**
342 * isci_task_execute_tmf() - This function builds and sends a task request,
343 * then waits for the completion.
344 * @isci_host: This parameter specifies the ISCI host object
345 * @tmf: This parameter is the pointer to the task management structure for
346 * this request.
347 * @timeout_ms: This parameter specifies the timeout period for the task
348 * management request.
349 *
350 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
351 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
352 */
353int isci_task_execute_tmf(
354 struct isci_host *isci_host,
355 struct isci_tmf *tmf,
356 unsigned long timeout_ms)
357{
358 DECLARE_COMPLETION_ONSTACK(completion);
467e855a 359 enum sci_task_status status = SCI_TASK_FAILURE;
6f231dda
DW
360 struct scic_sds_remote_device *sci_device;
361 struct isci_remote_device *isci_device = tmf->device;
362 struct isci_request *request;
363 int ret = TMF_RESP_FUNC_FAILED;
364 unsigned long flags;
fd18388b 365 unsigned long timeleft;
6f231dda
DW
366
367 /* sanity check, return TMF_RESP_FUNC_FAILED
368 * if the device is not there and ready.
369 */
8acaec15 370 if (!isci_device || isci_device->status != isci_ready_for_io) {
6f231dda
DW
371 dev_dbg(&isci_host->pdev->dev,
372 "%s: isci_device = %p not ready (%d)\n",
373 __func__,
8acaec15 374 isci_device, isci_device->status);
6f231dda
DW
375 return TMF_RESP_FUNC_FAILED;
376 } else
377 dev_dbg(&isci_host->pdev->dev,
378 "%s: isci_device = %p\n",
379 __func__, isci_device);
380
57f20f4e 381 sci_device = &isci_device->sci;
6f231dda
DW
382
383 /* Assign the pointer to the TMF's completion kernel wait structure. */
384 tmf->complete = &completion;
385
386 isci_task_request_build(
387 isci_host,
388 &request,
389 tmf
390 );
391
392 if (!request) {
393 dev_warn(&isci_host->pdev->dev,
394 "%s: isci_task_request_build failed\n",
395 __func__);
396 return TMF_RESP_FUNC_FAILED;
397 }
398
6f231dda 399 spin_lock_irqsave(&isci_host->scic_lock, flags);
6f231dda
DW
400
401 /* start the TMF io. */
402 status = scic_controller_start_task(
cc3dbd0a 403 &isci_host->sci,
6f231dda 404 sci_device,
67ea838d
DW
405 &request->sci,
406 SCI_CONTROLLER_INVALID_IO_TAG);
6f231dda 407
467e855a 408 if (status != SCI_TASK_SUCCESS) {
6f231dda
DW
409 dev_warn(&isci_host->pdev->dev,
410 "%s: start_io failed - status = 0x%x, request = %p\n",
411 __func__,
412 status,
413 request);
fd18388b 414 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
6f231dda
DW
415 goto cleanup_request;
416 }
417
6f231dda
DW
418 if (tmf->cb_state_func != NULL)
419 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
420
6f231dda
DW
421 isci_request_change_state(request, started);
422
423 /* add the request to the remote device request list. */
424 list_add(&request->dev_node, &isci_device->reqs_in_process);
425
426 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
427
428 /* Wait for the TMF to complete, or a timeout. */
fd18388b
EN
429 timeleft = wait_for_completion_timeout(&completion,
430 jiffies + msecs_to_jiffies(timeout_ms));
431
432 if (timeleft == 0) {
433 spin_lock_irqsave(&isci_host->scic_lock, flags);
434
435 if (tmf->cb_state_func != NULL)
436 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
437
438 status = scic_controller_terminate_request(
439 &request->isci_host->sci,
440 &request->isci_device->sci,
441 &request->sci);
442
443 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
444 }
6f231dda
DW
445
446 isci_print_tmf(tmf);
447
448 if (tmf->status == SCI_SUCCESS)
449 ret = TMF_RESP_FUNC_COMPLETE;
450 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
451 dev_dbg(&isci_host->pdev->dev,
452 "%s: tmf.status == "
453 "SCI_FAILURE_IO_RESPONSE_VALID\n",
454 __func__);
455 ret = TMF_RESP_FUNC_COMPLETE;
456 }
457 /* Else - leave the default "failed" status alone. */
458
459 dev_dbg(&isci_host->pdev->dev,
460 "%s: completed request = %p\n",
461 __func__,
462 request);
463
464 if (request->io_request_completion != NULL) {
fd18388b 465 /* A thread is waiting for this TMF to finish. */
6f231dda
DW
466 complete(request->io_request_completion);
467 }
468
6f231dda 469 cleanup_request:
6f231dda 470 isci_request_free(isci_host, request);
6f231dda
DW
471 return ret;
472}
473
474void isci_task_build_tmf(
475 struct isci_tmf *tmf,
476 struct isci_remote_device *isci_device,
477 enum isci_tmf_function_codes code,
478 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
479 struct isci_tmf *,
480 void *),
c3f42feb 481 void *cb_data)
6f231dda
DW
482{
483 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
484 "%s: isci_device = %p\n", __func__, isci_device);
485
486 memset(tmf, 0, sizeof(*tmf));
487
488 tmf->device = isci_device;
489 tmf->tmf_code = code;
fd18388b 490
6f231dda 491 tmf->cb_state_func = tmf_sent_cb;
c3f42feb
JS
492 tmf->cb_data = cb_data;
493}
1fad9e93 494
35173d57 495static void isci_task_build_abort_task_tmf(
c3f42feb
JS
496 struct isci_tmf *tmf,
497 struct isci_remote_device *isci_device,
498 enum isci_tmf_function_codes code,
499 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
500 struct isci_tmf *,
501 void *),
502 struct isci_request *old_request)
503{
504 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
505 (void *)old_request);
506 tmf->io_tag = old_request->io_tag;
6f231dda
DW
507}
508
509static struct isci_request *isci_task_get_request_from_task(
510 struct sas_task *task,
6f231dda
DW
511 struct isci_remote_device **isci_device)
512{
513
514 struct isci_request *request = NULL;
515 unsigned long flags;
516
517 spin_lock_irqsave(&task->task_state_lock, flags);
518
519 request = task->lldd_task;
520
521 /* If task is already done, the request isn't valid */
522 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
523 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
524 (request != NULL)) {
525
6f231dda
DW
526 if (isci_device != NULL)
527 *isci_device = request->isci_device;
528 }
529
530 spin_unlock_irqrestore(&task->task_state_lock, flags);
531
532 return request;
533}
534
535/**
536 * isci_task_validate_request_to_abort() - This function checks the given I/O
537 * against the "started" state. If the request is still "started", it's
538 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
539 * BEFORE CALLING THIS FUNCTION.
540 * @isci_request: This parameter specifies the request object to control.
541 * @isci_host: This parameter specifies the ISCI host object
542 * @isci_device: This is the device to which the request is pending.
543 * @aborted_io_completion: This is a completion structure that will be added to
544 * the request in case it is changed to aborting; this completion is
545 * triggered when the request is fully completed.
546 *
547 * Either "started" on successful change of the task status to "aborted", or
548 * "unallocated" if the task cannot be controlled.
549 */
550static enum isci_request_status isci_task_validate_request_to_abort(
551 struct isci_request *isci_request,
552 struct isci_host *isci_host,
553 struct isci_remote_device *isci_device,
554 struct completion *aborted_io_completion)
555{
556 enum isci_request_status old_state = unallocated;
557
558 /* Only abort the task if it's in the
559 * device's request_in_process list
560 */
561 if (isci_request && !list_empty(&isci_request->dev_node)) {
562 old_state = isci_request_change_started_to_aborted(
563 isci_request, aborted_io_completion);
564
6f231dda
DW
565 }
566
567 return old_state;
568}
569
77c852f3
JS
570/**
571* isci_request_cleanup_completed_loiterer() - This function will take care of
572* the final cleanup on any request which has been explicitly terminated.
573* @isci_host: This parameter specifies the ISCI host object
574* @isci_device: This is the device to which the request is pending.
575* @isci_request: This parameter specifies the terminated request object.
576* @task: This parameter is the libsas I/O request.
577*/
6f231dda 578static void isci_request_cleanup_completed_loiterer(
77c852f3 579 struct isci_host *isci_host,
6f231dda 580 struct isci_remote_device *isci_device,
77c852f3
JS
581 struct isci_request *isci_request,
582 struct sas_task *task)
6f231dda 583{
77c852f3 584 unsigned long flags;
6f231dda
DW
585
586 dev_dbg(&isci_host->pdev->dev,
587 "%s: isci_device=%p, request=%p, task=%p\n",
18d3d72a 588 __func__, isci_device, isci_request, task);
6f231dda 589
a5fde225
JS
590 if (task != NULL) {
591
592 spin_lock_irqsave(&task->task_state_lock, flags);
593 task->lldd_task = NULL;
594
77c852f3
JS
595 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
596
a5fde225
JS
597 isci_set_task_doneflags(task);
598
599 /* If this task is not in the abort path, call task_done. */
600 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
601
602 spin_unlock_irqrestore(&task->task_state_lock, flags);
603 task->task_done(task);
604 } else
605 spin_unlock_irqrestore(&task->task_state_lock, flags);
606 }
4dc043c4 607
77c852f3
JS
608 if (isci_request != NULL) {
609 spin_lock_irqsave(&isci_host->scic_lock, flags);
610 list_del_init(&isci_request->dev_node);
611 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
4dc043c4 612
77c852f3 613 isci_request_free(isci_host, isci_request);
4dc043c4 614 }
4dc043c4
JS
615}
616
6f231dda
DW
617/**
618 * isci_terminate_request_core() - This function will terminate the given
619 * request, and wait for it to complete. This function must only be called
620 * from a thread that can wait. Note that the request is terminated and
621 * completed (back to the host, if started there).
622 * @isci_host: This SCU.
623 * @isci_device: The target.
624 * @isci_request: The I/O request to be terminated.
625 *
6f231dda
DW
626 */
627static void isci_terminate_request_core(
628 struct isci_host *isci_host,
629 struct isci_remote_device *isci_device,
cbb65c66 630 struct isci_request *isci_request)
6f231dda 631{
cbb65c66 632 enum sci_status status = SCI_SUCCESS;
6f231dda
DW
633 bool was_terminated = false;
634 bool needs_cleanup_handling = false;
635 enum isci_request_status request_status;
77c852f3
JS
636 unsigned long flags;
637 unsigned long termination_completed = 1;
638 struct completion *io_request_completion;
639 struct sas_task *task;
6f231dda
DW
640
641 dev_dbg(&isci_host->pdev->dev,
642 "%s: device = %p; request = %p\n",
643 __func__, isci_device, isci_request);
644
6f231dda 645 spin_lock_irqsave(&isci_host->scic_lock, flags);
4dc043c4 646
77c852f3
JS
647 io_request_completion = isci_request->io_request_completion;
648
649 task = (isci_request->ttype == io_task)
650 ? isci_request_access_task(isci_request)
651 : NULL;
652
4dc043c4
JS
653 /* Note that we are not going to control
654 * the target to abort the request.
655 */
656 isci_request->complete_in_target = true;
657
6f231dda
DW
658 /* Make sure the request wasn't just sitting around signalling
659 * device condition (if the request handle is NULL, then the
660 * request completed but needed additional handling here).
661 */
67ea838d 662 if (!isci_request->terminated) {
6f231dda 663 was_terminated = true;
cbb65c66 664 needs_cleanup_handling = true;
6f231dda 665 status = scic_controller_terminate_request(
cc3dbd0a 666 &isci_host->sci,
57f20f4e 667 &isci_device->sci,
67ea838d 668 &isci_request->sci);
6f231dda
DW
669 }
670 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
671
672 /*
673 * The only time the request to terminate will
674 * fail is when the io request is completed and
675 * being aborted.
676 */
4dc043c4 677 if (status != SCI_SUCCESS) {
6f231dda
DW
678 dev_err(&isci_host->pdev->dev,
679 "%s: scic_controller_terminate_request"
680 " returned = 0x%x\n",
77c852f3
JS
681 __func__, status);
682
4dc043c4
JS
683 isci_request->io_request_completion = NULL;
684
685 } else {
6f231dda
DW
686 if (was_terminated) {
687 dev_dbg(&isci_host->pdev->dev,
77c852f3
JS
688 "%s: before completion wait (%p/%p)\n",
689 __func__, isci_request, io_request_completion);
6f231dda
DW
690
691 /* Wait here for the request to complete. */
77c852f3
JS
692 #define TERMINATION_TIMEOUT_MSEC 500
693 termination_completed
4dc043c4 694 = wait_for_completion_timeout(
77c852f3 695 io_request_completion,
4dc043c4
JS
696 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
697
77c852f3
JS
698 if (!termination_completed) {
699
700 /* The request to terminate has timed out. */
701 spin_lock_irqsave(&isci_host->scic_lock,
702 flags);
703
704 /* Check for state changes. */
705 if (!isci_request->terminated) {
706
707 /* The best we can do is to have the
708 * request die a silent death if it
709 * ever really completes.
710 *
711 * Set the request state to "dead",
712 * and clear the task pointer so that
713 * an actual completion event callback
714 * doesn't do anything.
715 */
716 isci_request->status = dead;
717 isci_request->io_request_completion
718 = NULL;
719
720 if (isci_request->ttype == io_task) {
721
722 /* Break links with the
723 * sas_task.
724 */
725 isci_request->ttype_ptr.io_task_ptr
726 = NULL;
727 }
728 } else
729 termination_completed = 1;
730
731 spin_unlock_irqrestore(&isci_host->scic_lock,
732 flags);
4dc043c4 733
77c852f3 734 if (!termination_completed) {
4dc043c4 735
77c852f3
JS
736 dev_err(&isci_host->pdev->dev,
737 "%s: *** Timeout waiting for "
738 "termination(%p/%p)\n",
739 __func__, io_request_completion,
740 isci_request);
4dc043c4 741
77c852f3
JS
742 /* The request can no longer be referenced
743 * safely since it may go away if the
744 * termination every really does complete.
745 */
746 isci_request = NULL;
747 }
748 }
749 if (termination_completed)
4dc043c4 750 dev_dbg(&isci_host->pdev->dev,
77c852f3
JS
751 "%s: after completion wait (%p/%p)\n",
752 __func__, isci_request, io_request_completion);
4dc043c4 753 }
4dc043c4 754
77c852f3 755 if (termination_completed) {
cbb65c66 756
77c852f3 757 isci_request->io_request_completion = NULL;
6f231dda 758
77c852f3
JS
759 /* Peek at the status of the request. This will tell
760 * us if there was special handling on the request such that it
761 * needs to be detached and freed here.
762 */
763 spin_lock_irqsave(&isci_request->state_lock, flags);
764 request_status = isci_request_get_state(isci_request);
765
766 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
767 && ((request_status == aborted)
768 || (request_status == aborting)
769 || (request_status == terminating)
770 || (request_status == completed)
771 || (request_status == dead)
772 )
773 ) {
774
775 /* The completion routine won't free a request in
776 * the aborted/aborting/etc. states, so we do
777 * it here.
778 */
779 needs_cleanup_handling = true;
780 }
781 spin_unlock_irqrestore(&isci_request->state_lock, flags);
6f231dda 782
77c852f3
JS
783 }
784 if (needs_cleanup_handling)
785 isci_request_cleanup_completed_loiterer(
786 isci_host, isci_device, isci_request, task);
a5fde225 787 }
6f231dda
DW
788}
789
790/**
791 * isci_terminate_pending_requests() - This function will change the all of the
792 * requests on the given device's state to "aborting", will terminate the
793 * requests, and wait for them to complete. This function must only be
794 * called from a thread that can wait. Note that the requests are all
795 * terminated and completed (back to the host, if started there).
796 * @isci_host: This parameter specifies SCU.
797 * @isci_device: This parameter specifies the target.
798 *
799 *
800 */
801void isci_terminate_pending_requests(
802 struct isci_host *isci_host,
803 struct isci_remote_device *isci_device,
804 enum isci_request_status new_request_state)
805{
4dc043c4
JS
806 struct isci_request *request;
807 struct isci_request *next_request;
808 unsigned long flags;
77c852f3
JS
809 enum isci_request_status old_state;
810 DECLARE_COMPLETION_ONSTACK(request_completion);
6f231dda
DW
811
812 dev_dbg(&isci_host->pdev->dev,
813 "%s: isci_device = %p (new request state = %d)\n",
814 __func__, isci_device, new_request_state);
815
4dc043c4 816 spin_lock_irqsave(&isci_host->scic_lock, flags);
6f231dda 817
77c852f3 818 /* Iterate through the list. */
4dc043c4 819 list_for_each_entry_safe(request, next_request,
77c852f3 820 &isci_device->reqs_in_process, dev_node) {
6f231dda 821
77c852f3 822 init_completion(&request_completion);
4dc043c4 823
77c852f3
JS
824 /* Change state to "new_request_state" if it is currently
825 * "started".
4dc043c4 826 */
77c852f3
JS
827 old_state = isci_request_change_started_to_newstate(
828 request,
829 &request_completion,
830 new_request_state);
831
832 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
833
834 if ((old_state == started) ||
835 (old_state == completed) ||
836 (old_state == aborting)) {
837
838 dev_warn(&isci_host->pdev->dev,
839 "%s: isci_device=%p request=%p; task=%p "
840 "old_state=%d\n",
841 __func__,
842 isci_device, request,
843 ((request->ttype == io_task)
844 ? isci_request_access_task(request)
845 : NULL),
846 old_state);
847
848 /* If the old_state is started:
849 * This request was not already being aborted. If it had been,
850 * then the aborting I/O (ie. the TMF request) would not be in
851 * the aborting state, and thus would be terminated here. Note
852 * that since the TMF completion's call to the kernel function
853 * "complete()" does not happen until the pending I/O request
854 * terminate fully completes, we do not have to implement a
855 * special wait here for already aborting requests - the
856 * termination of the TMF request will force the request
857 * to finish it's already started terminate.
858 *
859 * If old_state == completed:
860 * This request completed from the SCU hardware perspective
861 * and now just needs cleaning up in terms of freeing the
862 * request and potentially calling up to libsas.
863 *
864 * If old_state == aborting:
865 * This request has already gone through a TMF timeout, but may
866 * not have been terminated; needs cleaning up at least.
867 */
868 isci_terminate_request_core(isci_host, isci_device,
869 request);
870 }
871 spin_lock_irqsave(&isci_host->scic_lock, flags);
4dc043c4 872 }
77c852f3 873 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
6f231dda
DW
874}
875
876/**
877 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
878 * Template functions.
879 * @lun: This parameter specifies the lun to be reset.
880 *
881 * status, zero indicates success.
882 */
883static int isci_task_send_lu_reset_sas(
884 struct isci_host *isci_host,
885 struct isci_remote_device *isci_device,
886 u8 *lun)
887{
888 struct isci_tmf tmf;
889 int ret = TMF_RESP_FUNC_FAILED;
890
891 dev_dbg(&isci_host->pdev->dev,
892 "%s: isci_host = %p, isci_device = %p\n",
893 __func__, isci_host, isci_device);
894 /* Send the LUN reset to the target. By the time the call returns,
895 * the TMF has fully exected in the target (in which case the return
896 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
897 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
898 */
899 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
900 NULL);
901
902 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
903 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
904
905 if (ret == TMF_RESP_FUNC_COMPLETE)
906 dev_dbg(&isci_host->pdev->dev,
907 "%s: %p: TMF_LU_RESET passed\n",
908 __func__, isci_device);
909 else
910 dev_dbg(&isci_host->pdev->dev,
911 "%s: %p: TMF_LU_RESET failed (%x)\n",
912 __func__, isci_device, ret);
913
914 return ret;
915}
916
917/**
918 * isci_task_lu_reset() - This function is one of the SAS Domain Template
919 * functions. This is one of the Task Management functoins called by libsas,
920 * to reset the given lun. Note the assumption that while this call is
921 * executing, no I/O will be sent by the host to the device.
922 * @lun: This parameter specifies the lun to be reset.
923 *
924 * status, zero indicates success.
925 */
4393aa4e 926int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
6f231dda 927{
4393aa4e 928 struct isci_host *isci_host = dev_to_ihost(domain_device);
6f231dda
DW
929 struct isci_remote_device *isci_device = NULL;
930 int ret;
931 bool device_stopping = false;
932
4393aa4e 933 isci_device = domain_device->lldd_dev;
6f231dda 934
4393aa4e
DW
935 dev_dbg(&isci_host->pdev->dev,
936 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
6f231dda
DW
937 __func__, domain_device, isci_host, isci_device);
938
d06b487b 939 if (isci_device != NULL) {
6f231dda
DW
940 device_stopping = (isci_device->status == isci_stopping)
941 || (isci_device->status == isci_stopped);
d06b487b
DW
942 set_bit(IDEV_EH, &isci_device->flags);
943 }
6f231dda
DW
944
945 /* If there is a device reset pending on any request in the
946 * device's list, fail this LUN reset request in order to
947 * escalate to the device reset.
948 */
4393aa4e
DW
949 if (!isci_device || device_stopping ||
950 isci_device_is_reset_pending(isci_host, isci_device)) {
6f231dda 951 dev_warn(&isci_host->pdev->dev,
4393aa4e 952 "%s: No dev (%p), or "
6f231dda 953 "RESET PENDING: domain_device=%p\n",
4393aa4e 954 __func__, isci_device, domain_device);
6f231dda
DW
955 return TMF_RESP_FUNC_FAILED;
956 }
957
6f231dda
DW
958 /* Send the task management part of the reset. */
959 if (sas_protocol_ata(domain_device->tproto)) {
4393aa4e 960 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
6f231dda
DW
961 } else
962 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
963
964 /* If the LUN reset worked, all the I/O can now be terminated. */
965 if (ret == TMF_RESP_FUNC_COMPLETE)
966 /* Terminate all I/O now. */
967 isci_terminate_pending_requests(isci_host,
968 isci_device,
969 terminating);
970
6f231dda
DW
971 return ret;
972}
973
974
975/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
976int isci_task_clear_nexus_port(struct asd_sas_port *port)
977{
978 return TMF_RESP_FUNC_FAILED;
979}
980
981
982
983int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
984{
985 return TMF_RESP_FUNC_FAILED;
986}
987
6f231dda
DW
988/* Task Management Functions. Must be called from process context. */
989
990/**
991 * isci_abort_task_process_cb() - This is a helper function for the abort task
992 * TMF command. It manages the request state with respect to the successful
993 * transmission / completion of the abort task request.
994 * @cb_state: This parameter specifies when this function was called - after
995 * the TMF request has been started and after it has timed-out.
996 * @tmf: This parameter specifies the TMF in progress.
997 *
998 *
999 */
1000static void isci_abort_task_process_cb(
1001 enum isci_tmf_cb_state cb_state,
1002 struct isci_tmf *tmf,
1003 void *cb_data)
1004{
1005 struct isci_request *old_request;
1006
1007 old_request = (struct isci_request *)cb_data;
1008
1009 dev_dbg(&old_request->isci_host->pdev->dev,
1010 "%s: tmf=%p, old_request=%p\n",
1011 __func__, tmf, old_request);
1012
1013 switch (cb_state) {
1014
1015 case isci_tmf_started:
1016 /* The TMF has been started. Nothing to do here, since the
1017 * request state was already set to "aborted" by the abort
1018 * task function.
1019 */
6cb4d6b3
BB
1020 if ((old_request->status != aborted)
1021 && (old_request->status != completed))
1022 dev_err(&old_request->isci_host->pdev->dev,
1023 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
1024 __func__, old_request->status, tmf, old_request);
6f231dda
DW
1025 break;
1026
1027 case isci_tmf_timed_out:
1028
1029 /* Set the task's state to "aborting", since the abort task
1030 * function thread set it to "aborted" (above) in anticipation
1031 * of the task management request working correctly. Since the
1032 * timeout has now fired, the TMF request failed. We set the
1033 * state such that the request completion will indicate the
1034 * device is no longer present.
1035 */
1036 isci_request_change_state(old_request, aborting);
1037 break;
1038
1039 default:
1040 dev_err(&old_request->isci_host->pdev->dev,
1041 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1042 __func__, cb_state, tmf, old_request);
1043 break;
1044 }
1045}
1046
1047/**
1048 * isci_task_abort_task() - This function is one of the SAS Domain Template
1049 * functions. This function is called by libsas to abort a specified task.
1050 * @task: This parameter specifies the SAS task to abort.
1051 *
1052 * status, zero indicates success.
1053 */
1054int isci_task_abort_task(struct sas_task *task)
1055{
4393aa4e 1056 struct isci_host *isci_host = dev_to_ihost(task->dev);
6f231dda 1057 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
a5fde225
JS
1058 struct isci_request *old_request = NULL;
1059 enum isci_request_status old_state;
6f231dda 1060 struct isci_remote_device *isci_device = NULL;
a5fde225
JS
1061 struct isci_tmf tmf;
1062 int ret = TMF_RESP_FUNC_FAILED;
1063 unsigned long flags;
1064 bool any_dev_reset = false;
1065 bool device_stopping;
6f231dda
DW
1066
1067 /* Get the isci_request reference from the task. Note that
1068 * this check does not depend on the pending request list
1069 * in the device, because tasks driving resets may land here
1070 * after completion in the core.
1071 */
4393aa4e 1072 old_request = isci_task_get_request_from_task(task, &isci_device);
6f231dda
DW
1073
1074 dev_dbg(&isci_host->pdev->dev,
1075 "%s: task = %p\n", __func__, task);
1076
1077 /* Check if the device has been / is currently being removed.
1078 * If so, no task management will be done, and the I/O will
1079 * be terminated.
1080 */
1081 device_stopping = (isci_device->status == isci_stopping)
1082 || (isci_device->status == isci_stopped);
1083
d06b487b
DW
1084 /* XXX need to fix device lookup lifetime (needs to be done
1085 * under scic_lock, among other things...), but for now assume
1086 * the device is available like the above code
1087 */
1088 set_bit(IDEV_EH, &isci_device->flags);
1089
6f231dda
DW
1090 /* This version of the driver will fail abort requests for
1091 * SATA/STP. Failing the abort request this way will cause the
1092 * SCSI error handler thread to escalate to LUN reset
1093 */
1094 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1095 dev_warn(&isci_host->pdev->dev,
1096 " task %p is for a STP/SATA device;"
1097 " returning TMF_RESP_FUNC_FAILED\n"
1098 " to cause a LUN reset...\n", task);
1099 return TMF_RESP_FUNC_FAILED;
1100 }
1101
1102 dev_dbg(&isci_host->pdev->dev,
1103 "%s: old_request == %p\n", __func__, old_request);
1104
a5fde225
JS
1105 if (!device_stopping)
1106 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1107
6f231dda
DW
1108 spin_lock_irqsave(&task->task_state_lock, flags);
1109
1110 /* Don't do resets to stopping devices. */
a5fde225 1111 if (device_stopping) {
6f231dda 1112
a5fde225
JS
1113 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1114 any_dev_reset = false;
6f231dda 1115
a5fde225 1116 } else /* See if there is a pending device reset for this device. */
6f231dda 1117 any_dev_reset = any_dev_reset
a5fde225 1118 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
6f231dda
DW
1119
1120 /* If the extraction of the request reference from the task
1121 * failed, then the request has been completed (or if there is a
1122 * pending reset then this abort request function must be failed
1123 * in order to escalate to the target reset).
1124 */
a5fde225 1125 if ((old_request == NULL) || any_dev_reset) {
6f231dda
DW
1126
1127 /* If the device reset task flag is set, fail the task
1128 * management request. Otherwise, the original request
1129 * has completed.
1130 */
1131 if (any_dev_reset) {
1132
1133 /* Turn off the task's DONE to make sure this
1134 * task is escalated to a target reset.
1135 */
1136 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1137
a5fde225
JS
1138 /* Make the reset happen as soon as possible. */
1139 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1140
1141 spin_unlock_irqrestore(&task->task_state_lock, flags);
1142
6f231dda
DW
1143 /* Fail the task management request in order to
1144 * escalate to the target reset.
1145 */
1146 ret = TMF_RESP_FUNC_FAILED;
1147
1148 dev_dbg(&isci_host->pdev->dev,
1149 "%s: Failing task abort in order to "
1150 "escalate to target reset because\n"
1151 "SAS_TASK_NEED_DEV_RESET is set for "
1152 "task %p on dev %p\n",
1153 __func__, task, isci_device);
1154
6f231dda 1155
a5fde225 1156 } else {
6f231dda
DW
1157 /* The request has already completed and there
1158 * is nothing to do here other than to set the task
1159 * done bit, and indicate that the task abort function
1160 * was sucessful.
1161 */
1162 isci_set_task_doneflags(task);
1163
a5fde225 1164 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda 1165
a5fde225 1166 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 1167
a5fde225
JS
1168 dev_dbg(&isci_host->pdev->dev,
1169 "%s: abort task not needed for %p\n",
1170 __func__, task);
6f231dda 1171 }
6f231dda
DW
1172
1173 return ret;
1174 }
a5fde225
JS
1175 else
1176 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda
DW
1177
1178 spin_lock_irqsave(&isci_host->scic_lock, flags);
1179
f219f010 1180 /* Check the request status and change to "aborted" if currently
a5fde225 1181 * "starting"; if true then set the I/O kernel completion
6f231dda
DW
1182 * struct that will be triggered when the request completes.
1183 */
a5fde225
JS
1184 old_state = isci_task_validate_request_to_abort(
1185 old_request, isci_host, isci_device,
1186 &aborted_io_completion);
f219f010
JS
1187 if ((old_state != started) &&
1188 (old_state != completed) &&
1189 (old_state != aborting)) {
6f231dda
DW
1190
1191 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1192
a5fde225
JS
1193 /* The request was already being handled by someone else (because
1194 * they got to set the state away from started).
1195 */
1196 dev_dbg(&isci_host->pdev->dev,
1197 "%s: device = %p; old_request %p already being aborted\n",
1198 __func__,
1199 isci_device, old_request);
6f231dda
DW
1200
1201 return TMF_RESP_FUNC_COMPLETE;
1202 }
a5fde225
JS
1203 if ((task->task_proto == SAS_PROTOCOL_SMP)
1204 || device_stopping
1205 || old_request->complete_in_target
1206 ) {
6f231dda
DW
1207
1208 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1209
a5fde225
JS
1210 dev_dbg(&isci_host->pdev->dev,
1211 "%s: SMP request (%d)"
1212 " or device is stopping (%d)"
1213 " or complete_in_target (%d), thus no TMF\n",
1214 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1215 device_stopping, old_request->complete_in_target);
1216
6f231dda
DW
1217 /* Set the state on the task. */
1218 isci_task_all_done(task);
1219
1220 ret = TMF_RESP_FUNC_COMPLETE;
1221
1222 /* Stopping and SMP devices are not sent a TMF, and are not
a5fde225 1223 * reset, but the outstanding I/O request is terminated below.
6f231dda 1224 */
6f231dda
DW
1225 } else {
1226 /* Fill in the tmf stucture */
c3f42feb
JS
1227 isci_task_build_abort_task_tmf(&tmf, isci_device,
1228 isci_tmf_ssp_task_abort,
1229 isci_abort_task_process_cb,
1230 old_request);
6f231dda 1231
6f231dda
DW
1232 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1233
1234 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1235 ret = isci_task_execute_tmf(isci_host, &tmf,
1236 ISCI_ABORT_TASK_TIMEOUT_MS);
1237
a5fde225 1238 if (ret != TMF_RESP_FUNC_COMPLETE)
6f231dda
DW
1239 dev_err(&isci_host->pdev->dev,
1240 "%s: isci_task_send_tmf failed\n",
1241 __func__);
1242 }
a5fde225
JS
1243 if (ret == TMF_RESP_FUNC_COMPLETE) {
1244 old_request->complete_in_target = true;
1245
77c852f3
JS
1246 /* Clean up the request on our side, and wait for the aborted
1247 * I/O to complete.
1248 */
cbb65c66 1249 isci_terminate_request_core(isci_host, isci_device, old_request);
a5fde225 1250 }
6f231dda 1251
a5fde225
JS
1252 /* Make sure we do not leave a reference to aborted_io_completion */
1253 old_request->io_request_completion = NULL;
6f231dda
DW
1254 return ret;
1255}
1256
1257/**
1258 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1259 * functions. This is one of the Task Management functoins called by libsas,
1260 * to abort all task for the given lun.
1261 * @d_device: This parameter specifies the domain device associated with this
1262 * request.
1263 * @lun: This parameter specifies the lun associated with this request.
1264 *
1265 * status, zero indicates success.
1266 */
1267int isci_task_abort_task_set(
1268 struct domain_device *d_device,
1269 u8 *lun)
1270{
1271 return TMF_RESP_FUNC_FAILED;
1272}
1273
1274
1275/**
1276 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1277 * functions. This is one of the Task Management functoins called by libsas.
1278 * @d_device: This parameter specifies the domain device associated with this
1279 * request.
1280 * @lun: This parameter specifies the lun associated with this request.
1281 *
1282 * status, zero indicates success.
1283 */
1284int isci_task_clear_aca(
1285 struct domain_device *d_device,
1286 u8 *lun)
1287{
1288 return TMF_RESP_FUNC_FAILED;
1289}
1290
1291
1292
1293/**
1294 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1295 * functions. This is one of the Task Management functoins called by libsas.
1296 * @d_device: This parameter specifies the domain device associated with this
1297 * request.
1298 * @lun: This parameter specifies the lun associated with this request.
1299 *
1300 * status, zero indicates success.
1301 */
1302int isci_task_clear_task_set(
1303 struct domain_device *d_device,
1304 u8 *lun)
1305{
1306 return TMF_RESP_FUNC_FAILED;
1307}
1308
1309
1310/**
1311 * isci_task_query_task() - This function is implemented to cause libsas to
1312 * correctly escalate the failed abort to a LUN or target reset (this is
1313 * because sas_scsi_find_task libsas function does not correctly interpret
1314 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1315 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1316 * returned, libsas will turn this into a target reset
1317 * @task: This parameter specifies the sas task being queried.
1318 * @lun: This parameter specifies the lun associated with this request.
1319 *
1320 * status, zero indicates success.
1321 */
1322int isci_task_query_task(
1323 struct sas_task *task)
1324{
1325 /* See if there is a pending device reset for this device. */
1326 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1327 return TMF_RESP_FUNC_FAILED;
1328 else
1329 return TMF_RESP_FUNC_SUCC;
1330}
1331
af5ae893 1332/*
6f231dda
DW
1333 * isci_task_request_complete() - This function is called by the sci core when
1334 * an task request completes.
af5ae893
DJ
1335 * @ihost: This parameter specifies the ISCI host object
1336 * @ireq: This parameter is the completed isci_request object.
6f231dda
DW
1337 * @completion_status: This parameter specifies the completion status from the
1338 * sci core.
1339 *
1340 * none.
1341 */
af5ae893
DJ
1342void
1343isci_task_request_complete(struct isci_host *ihost,
1344 struct isci_request *ireq,
1345 enum sci_task_status completion_status)
6f231dda 1346{
af5ae893 1347 struct isci_remote_device *idev = ireq->isci_device;
af5ae893 1348 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
6f231dda 1349 struct completion *tmf_complete;
67ea838d 1350 struct scic_sds_request *sci_req = &ireq->sci;
6f231dda 1351
af5ae893 1352 dev_dbg(&ihost->pdev->dev,
6f231dda 1353 "%s: request = %p, status=%d\n",
af5ae893 1354 __func__, ireq, completion_status);
6f231dda 1355
8d2c65c0 1356 isci_request_change_state(ireq, completed);
6f231dda
DW
1357
1358 tmf->status = completion_status;
af5ae893 1359 ireq->complete_in_target = true;
6f231dda 1360
af5ae893 1361 if (tmf->proto == SAS_PROTOCOL_SSP) {
6f231dda 1362 memcpy(&tmf->resp.resp_iu,
b7645818 1363 &sci_req->ssp.rsp,
af5ae893
DJ
1364 SSP_RESP_IU_MAX_SIZE);
1365 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
6f231dda 1366 memcpy(&tmf->resp.d2h_fis,
b7645818 1367 &sci_req->stp.rsp,
f2f30080 1368 sizeof(struct dev_to_host_fis));
6f231dda
DW
1369 }
1370
6f231dda
DW
1371 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1372 tmf_complete = tmf->complete;
1373
67ea838d
DW
1374 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1375 /* set the 'terminated' flag handle to make sure it cannot be terminated
6f231dda
DW
1376 * or completed again.
1377 */
67ea838d 1378 ireq->terminated = true;;
6f231dda 1379
af5ae893
DJ
1380 isci_request_change_state(ireq, unallocated);
1381 list_del_init(&ireq->dev_node);
6f231dda
DW
1382
1383 /* The task management part completes last. */
1384 complete(tmf_complete);
1385}
1386
61aaff49
JS
1387static void isci_smp_task_timedout(unsigned long _task)
1388{
1389 struct sas_task *task = (void *) _task;
1390 unsigned long flags;
1391
1392 spin_lock_irqsave(&task->task_state_lock, flags);
1393 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1394 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1395 spin_unlock_irqrestore(&task->task_state_lock, flags);
1396
1397 complete(&task->completion);
1398}
1399
1400static void isci_smp_task_done(struct sas_task *task)
1401{
1402 if (!del_timer(&task->timer))
1403 return;
1404 complete(&task->completion);
1405}
1406
1407static struct sas_task *isci_alloc_task(void)
1408{
1409 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1410
1411 if (task) {
1412 INIT_LIST_HEAD(&task->list);
1413 spin_lock_init(&task->task_state_lock);
1414 task->task_state_flags = SAS_TASK_STATE_PENDING;
1415 init_timer(&task->timer);
1416 init_completion(&task->completion);
1417 }
1418
1419 return task;
1420}
1421
1422static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1423{
1424 if (task) {
1425 BUG_ON(!list_empty(&task->list));
1426 kfree(task);
1427 }
1428}
1429
1430static int isci_smp_execute_task(struct isci_host *ihost,
1431 struct domain_device *dev, void *req,
1432 int req_size, void *resp, int resp_size)
1433{
1434 int res, retry;
1435 struct sas_task *task = NULL;
1436
1437 for (retry = 0; retry < 3; retry++) {
1438 task = isci_alloc_task();
1439 if (!task)
1440 return -ENOMEM;
1441
1442 task->dev = dev;
1443 task->task_proto = dev->tproto;
1444 sg_init_one(&task->smp_task.smp_req, req, req_size);
1445 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1446
1447 task->task_done = isci_smp_task_done;
1448
1449 task->timer.data = (unsigned long) task;
1450 task->timer.function = isci_smp_task_timedout;
1451 task->timer.expires = jiffies + 10*HZ;
1452 add_timer(&task->timer);
1453
1454 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1455
1456 if (res) {
1457 del_timer(&task->timer);
1458 dev_err(&ihost->pdev->dev,
1459 "%s: executing SMP task failed:%d\n",
1460 __func__, res);
1461 goto ex_err;
1462 }
1463
1464 wait_for_completion(&task->completion);
1465 res = -ECOMM;
1466 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1467 dev_err(&ihost->pdev->dev,
1468 "%s: smp task timed out or aborted\n",
1469 __func__);
1470 isci_task_abort_task(task);
1471 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1472 dev_err(&ihost->pdev->dev,
1473 "%s: SMP task aborted and not done\n",
1474 __func__);
1475 goto ex_err;
1476 }
1477 }
1478 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1479 task->task_status.stat == SAM_STAT_GOOD) {
1480 res = 0;
1481 break;
1482 }
1483 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1484 task->task_status.stat == SAS_DATA_UNDERRUN) {
1485 /* no error, but return the number of bytes of
1486 * underrun */
1487 res = task->task_status.residual;
1488 break;
1489 }
1490 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1491 task->task_status.stat == SAS_DATA_OVERRUN) {
1492 res = -EMSGSIZE;
1493 break;
1494 } else {
1495 dev_err(&ihost->pdev->dev,
1496 "%s: task to dev %016llx response: 0x%x "
1497 "status 0x%x\n", __func__,
1498 SAS_ADDR(dev->sas_addr),
1499 task->task_status.resp,
1500 task->task_status.stat);
1501 isci_free_task(ihost, task);
1502 task = NULL;
1503 }
1504 }
1505ex_err:
1506 BUG_ON(retry == 3 && task != NULL);
1507 isci_free_task(ihost, task);
1508 return res;
1509}
1510
1511#define DISCOVER_REQ_SIZE 16
1512#define DISCOVER_RESP_SIZE 56
1513
1514int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1515 struct domain_device *dev,
1516 int phy_id, int *adt)
1517{
1518 struct smp_resp *disc_resp;
1519 u8 *disc_req;
1520 int res;
1521
1522 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1523 if (!disc_resp)
1524 return -ENOMEM;
1525
1526 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1527 if (disc_req) {
1528 disc_req[0] = SMP_REQUEST;
1529 disc_req[1] = SMP_DISCOVER;
1530 disc_req[9] = phy_id;
1531 } else {
1532 kfree(disc_resp);
1533 return -ENOMEM;
1534 }
1535 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1536 disc_resp, DISCOVER_RESP_SIZE);
1537 if (!res) {
1538 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1539 res = disc_resp->result;
1540 else
1541 *adt = disc_resp->disc.attached_dev_type;
1542 }
1543 kfree(disc_req);
1544 kfree(disc_resp);
1545
1546 return res;
1547}
1548
1549static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1550{
1551 struct domain_device *dev = idev->domain_dev;
1552 struct isci_port *iport = idev->isci_port;
1553 struct isci_host *ihost = iport->isci_host;
1554 int res, iteration = 0, attached_device_type;
1555 #define STP_WAIT_MSECS 25000
1556 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1557 unsigned long deadline = jiffies + tmo;
1558 enum {
1559 SMP_PHYWAIT_PHYDOWN,
1560 SMP_PHYWAIT_PHYUP,
1561 SMP_PHYWAIT_DONE
1562 } phy_state = SMP_PHYWAIT_PHYDOWN;
1563
1564 /* While there is time, wait for the phy to go away and come back */
1565 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1566 int event = atomic_read(&iport->event);
1567
1568 ++iteration;
1569
1570 tmo = wait_event_timeout(ihost->eventq,
1571 event != atomic_read(&iport->event) ||
1572 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1573 tmo);
1574 /* link down, stop polling */
1575 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1576 break;
1577
1578 dev_dbg(&ihost->pdev->dev,
1579 "%s: iport %p, iteration %d,"
1580 " phase %d: time_remaining %lu, bcns = %d\n",
1581 __func__, iport, iteration, phy_state,
1582 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1583
1584 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1585 &attached_device_type);
1586 tmo = deadline - jiffies;
1587
1588 if (res) {
1589 dev_warn(&ihost->pdev->dev,
1590 "%s: iteration %d, phase %d:"
1591 " SMP error=%d, time_remaining=%lu\n",
1592 __func__, iteration, phy_state, res, tmo);
1593 break;
1594 }
1595 dev_dbg(&ihost->pdev->dev,
1596 "%s: iport %p, iteration %d,"
1597 " phase %d: time_remaining %lu, bcns = %d, "
1598 "attdevtype = %x\n",
1599 __func__, iport, iteration, phy_state,
1600 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1601 attached_device_type);
1602
1603 switch (phy_state) {
1604 case SMP_PHYWAIT_PHYDOWN:
1605 /* Has the device gone away? */
1606 if (!attached_device_type)
1607 phy_state = SMP_PHYWAIT_PHYUP;
1608
1609 break;
1610
1611 case SMP_PHYWAIT_PHYUP:
1612 /* Has the device come back? */
1613 if (attached_device_type)
1614 phy_state = SMP_PHYWAIT_DONE;
1615 break;
1616
1617 case SMP_PHYWAIT_DONE:
1618 break;
1619 }
1620
1621 }
1622 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1623}
1624
d06b487b 1625static int isci_reset_device(struct domain_device *dev, int hard_reset)
6f231dda 1626{
d06b487b
DW
1627 struct isci_remote_device *idev = dev->lldd_dev;
1628 struct sas_phy *phy = sas_find_local_phy(dev);
1629 struct isci_host *ihost = dev_to_ihost(dev);
61aaff49 1630 struct isci_port *iport = idev->isci_port;
6f231dda 1631 enum sci_status status;
d06b487b
DW
1632 unsigned long flags;
1633 int rc;
6f231dda 1634
d06b487b 1635 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
6f231dda 1636
d06b487b
DW
1637 if (!idev) {
1638 dev_warn(&ihost->pdev->dev,
1639 "%s: idev is GONE!\n",
6f231dda
DW
1640 __func__);
1641
1642 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1643 }
1644
d06b487b
DW
1645 spin_lock_irqsave(&ihost->scic_lock, flags);
1646 status = scic_remote_device_reset(&idev->sci);
6f231dda 1647 if (status != SCI_SUCCESS) {
d06b487b 1648 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1649
d06b487b
DW
1650 dev_warn(&ihost->pdev->dev,
1651 "%s: scic_remote_device_reset(%p) returned %d!\n",
1652 __func__, idev, status);
6f231dda
DW
1653
1654 return TMF_RESP_FUNC_FAILED;
1655 }
d06b487b 1656 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1657
6f231dda 1658 /* Make sure all pending requests are able to be fully terminated. */
d06b487b 1659 isci_device_clear_reset_pending(ihost, idev);
6f231dda 1660
61aaff49
JS
1661 /* If this is a device on an expander, disable BCN processing. */
1662 if (!scsi_is_sas_phy_local(phy))
1663 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1664
d06b487b 1665 rc = sas_phy_reset(phy, hard_reset);
6f231dda 1666
d06b487b
DW
1667 /* Terminate in-progress I/O now. */
1668 isci_remote_device_nuke_requests(ihost, idev);
6f231dda 1669
ff717ab0 1670 /* Since all pending TCs have been cleaned, resume the RNC. */
d06b487b
DW
1671 spin_lock_irqsave(&ihost->scic_lock, flags);
1672 status = scic_remote_device_reset_complete(&idev->sci);
1673 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 1674
61aaff49
JS
1675 /* If this is a device on an expander, bring the phy back up. */
1676 if (!scsi_is_sas_phy_local(phy)) {
1677 /* A phy reset will cause the device to go away then reappear.
1678 * Since libsas will take action on incoming BCNs (eg. remove
1679 * a device going through an SMP phy-control driven reset),
1680 * we need to wait until the phy comes back up before letting
1681 * discovery proceed in libsas.
1682 */
1683 isci_wait_for_smp_phy_reset(idev, phy->number);
1684
1685 spin_lock_irqsave(&ihost->scic_lock, flags);
1686 isci_port_bcn_enable(ihost, idev->isci_port);
1687 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1688 }
ff717ab0 1689
d06b487b
DW
1690 if (status != SCI_SUCCESS) {
1691 dev_warn(&ihost->pdev->dev,
1692 "%s: scic_remote_device_reset_complete(%p) "
1693 "returned %d!\n", __func__, idev, status);
6f231dda
DW
1694 }
1695
d06b487b 1696 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
6f231dda 1697
d06b487b
DW
1698 return rc;
1699}
6f231dda 1700
d06b487b
DW
1701int isci_task_I_T_nexus_reset(struct domain_device *dev)
1702{
1703 struct isci_host *ihost = dev_to_ihost(dev);
1704 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1705 struct isci_remote_device *idev;
1706 unsigned long flags;
1707
1708 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1709 * being too paranoid, or is mvsas busted?!
1710 */
1711 spin_lock_irqsave(&ihost->scic_lock, flags);
1712 idev = dev->lldd_dev;
1713 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1714 ret = TMF_RESP_FUNC_COMPLETE;
1715 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1716
1717 if (ret == TMF_RESP_FUNC_COMPLETE)
1718 return ret;
1719
1720 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1721 hard_reset = 0;
1722
1723 return isci_reset_device(dev, hard_reset);
1724}
1725
1726int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1727{
1728 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1729 int hard_reset = 1;
1730
1731 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1732 hard_reset = 0;
6f231dda 1733
d06b487b 1734 return isci_reset_device(dev, hard_reset);
6f231dda 1735}
This page took 0.122079 seconds and 5 git commands to generate.