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