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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
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.
56 #include <linux/completion.h>
57 #include <linux/irqflags.h>
59 #include "remote_device.h"
60 #include "remote_node_context.h"
67 * isci_task_refuse() - complete the request to the upper layer driver in
68 * the case where an I/O needs to be completed back in the submit path.
69 * @ihost: host on which the the request was queued
70 * @task: request to complete
71 * @response: response code for the completed task.
72 * @status: status code for the completed task.
75 static void isci_task_refuse(struct isci_host
*ihost
, struct sas_task
*task
,
76 enum service_response response
,
77 enum exec_status status
)
80 enum isci_completion_selection disposition
;
82 disposition
= isci_perform_normal_io_completion
;
83 disposition
= isci_task_set_completion_status(task
, response
, status
,
86 /* Tasks aborted specifically by a call to the lldd_abort_task
87 * function should not be completed to the host in the regular path.
89 switch (disposition
) {
90 case isci_perform_normal_io_completion
:
91 /* Normal notification (task_done) */
92 dev_dbg(&ihost
->pdev
->dev
,
93 "%s: Normal - task = %p, response=%d, "
95 __func__
, task
, response
, status
);
97 task
->lldd_task
= NULL
;
99 isci_execpath_callback(ihost
, task
, task
->task_done
);
102 case isci_perform_aborted_io_completion
:
103 /* No notification because this request is already in the
106 dev_warn(&ihost
->pdev
->dev
,
107 "%s: Aborted - task = %p, response=%d, "
109 __func__
, task
, response
, status
);
112 case isci_perform_error_io_completion
:
113 /* Use sas_task_abort */
114 dev_warn(&ihost
->pdev
->dev
,
115 "%s: Error - task = %p, response=%d, "
117 __func__
, task
, response
, status
);
119 isci_execpath_callback(ihost
, task
, sas_task_abort
);
123 dev_warn(&ihost
->pdev
->dev
,
124 "%s: isci task notification default case!",
126 sas_task_abort(task
);
131 #define for_each_sas_task(num, task) \
132 for (; num > 0; num--,\
133 task = list_entry(task->list.next, struct sas_task, list))
136 * isci_task_execute_task() - This function is one of the SAS Domain Template
137 * functions. This function is called by libsas to send a task down to
139 * @task: This parameter specifies the SAS task to send.
140 * @num: This parameter specifies the number of tasks to queue.
141 * @gfp_flags: This parameter specifies the context of this call.
143 * status, zero indicates success.
145 int isci_task_execute_task(struct sas_task
*task
, int num
, gfp_t gfp_flags
)
147 struct isci_host
*ihost
= dev_to_ihost(task
->dev
);
148 struct isci_request
*request
= NULL
;
149 struct isci_remote_device
*device
;
152 enum sci_status status
;
153 enum isci_status device_status
;
155 dev_dbg(&ihost
->pdev
->dev
, "%s: num=%d\n", __func__
, num
);
157 /* Check if we have room for more tasks */
158 ret
= isci_host_can_queue(ihost
, num
);
161 dev_warn(&ihost
->pdev
->dev
, "%s: queue full\n", __func__
);
165 for_each_sas_task(num
, task
) {
166 dev_dbg(&ihost
->pdev
->dev
,
167 "task = %p, num = %d; dev = %p; cmd = %p\n",
168 task
, num
, task
->dev
, task
->uldd_task
);
170 device
= task
->dev
->lldd_dev
;
173 device_status
= device
->status
;
175 device_status
= isci_freed
;
177 /* From this point onward, any process that needs to guarantee
178 * that there is no kernel I/O being started will have to wait
179 * for the quiesce spinlock.
182 if (device_status
!= isci_ready_for_io
) {
184 /* Forces a retry from scsi mid layer. */
185 dev_dbg(&ihost
->pdev
->dev
,
186 "%s: task %p: isci_host->status = %d, "
187 "device = %p; device_status = 0x%x\n\n",
190 isci_host_get_state(ihost
),
194 if (device_status
== isci_ready
) {
195 /* Indicate QUEUE_FULL so that the scsi midlayer
198 isci_task_refuse(ihost
, task
,
202 /* Else, the device is going down. */
203 isci_task_refuse(ihost
, task
,
204 SAS_TASK_UNDELIVERED
,
207 isci_host_can_dequeue(ihost
, 1);
209 /* There is a device and it's ready for I/O. */
210 spin_lock_irqsave(&task
->task_state_lock
, flags
);
212 if (task
->task_state_flags
& SAS_TASK_STATE_ABORTED
) {
214 spin_unlock_irqrestore(&task
->task_state_lock
,
217 isci_task_refuse(ihost
, task
,
218 SAS_TASK_UNDELIVERED
,
219 SAM_STAT_TASK_ABORTED
);
221 /* The I/O was aborted. */
224 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
225 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
227 /* build and send the request. */
228 status
= isci_request_execute(ihost
, task
, &request
,
231 if (status
!= SCI_SUCCESS
) {
233 spin_lock_irqsave(&task
->task_state_lock
, flags
);
234 /* Did not really start this command. */
235 task
->task_state_flags
&= ~SAS_TASK_AT_INITIATOR
;
236 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
238 /* Indicate QUEUE_FULL so that the scsi
239 * midlayer retries. if the request
240 * failed for remote device reasons,
241 * it gets returned as
242 * SAS_TASK_UNDELIVERED next time
245 isci_task_refuse(ihost
, task
,
248 isci_host_can_dequeue(ihost
, 1);
259 * isci_task_request_build() - This function builds the task request object.
260 * @isci_host: This parameter specifies the ISCI host object
261 * @request: This parameter points to the isci_request object allocated in the
262 * request construct function.
263 * @tmf: This parameter is the task management struct to be built
265 * SCI_SUCCESS on successfull completion, or specific failure code.
267 static enum sci_status
isci_task_request_build(
268 struct isci_host
*isci_host
,
269 struct isci_request
**isci_request
,
270 struct isci_tmf
*isci_tmf
)
272 struct scic_sds_remote_device
*sci_device
;
273 enum sci_status status
= SCI_FAILURE
;
274 struct isci_request
*request
= NULL
;
275 struct isci_remote_device
*isci_device
;
276 struct domain_device
*dev
;
278 dev_dbg(&isci_host
->pdev
->dev
,
279 "%s: isci_tmf = %p\n", __func__
, isci_tmf
);
281 isci_device
= isci_tmf
->device
;
282 sci_device
= &isci_device
->sci
;
283 dev
= isci_device
->domain_dev
;
285 /* do common allocation and init of request object. */
286 status
= isci_request_alloc_tmf(
294 if (status
!= SCI_SUCCESS
)
297 /* let the core do it's construct. */
298 status
= scic_task_request_construct(&isci_host
->sci
, sci_device
,
299 SCI_CONTROLLER_INVALID_IO_TAG
,
302 if (status
!= SCI_SUCCESS
) {
303 dev_warn(&isci_host
->pdev
->dev
,
304 "%s: scic_task_request_construct failed - "
311 /* XXX convert to get this from task->tproto like other drivers */
312 if (dev
->dev_type
== SAS_END_DEV
) {
313 isci_tmf
->proto
= SAS_PROTOCOL_SSP
;
314 status
= scic_task_request_construct_ssp(&request
->sci
);
315 if (status
!= SCI_SUCCESS
)
319 if (dev
->dev_type
== SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_STP
)) {
320 isci_tmf
->proto
= SAS_PROTOCOL_SATA
;
321 status
= isci_sata_management_task_request_build(request
);
323 if (status
!= SCI_SUCCESS
)
331 /* release the dma memory if we fail. */
332 isci_request_free(isci_host
, request
);
336 *isci_request
= request
;
341 * isci_task_execute_tmf() - This function builds and sends a task request,
342 * then waits for the completion.
343 * @isci_host: This parameter specifies the ISCI host object
344 * @tmf: This parameter is the pointer to the task management structure for
346 * @timeout_ms: This parameter specifies the timeout period for the task
347 * management request.
349 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
350 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
352 int isci_task_execute_tmf(
353 struct isci_host
*isci_host
,
354 struct isci_tmf
*tmf
,
355 unsigned long timeout_ms
)
357 DECLARE_COMPLETION_ONSTACK(completion
);
358 enum sci_task_status status
= SCI_TASK_FAILURE
;
359 struct scic_sds_remote_device
*sci_device
;
360 struct isci_remote_device
*isci_device
= tmf
->device
;
361 struct isci_request
*request
;
362 int ret
= TMF_RESP_FUNC_FAILED
;
364 unsigned long timeleft
;
366 /* sanity check, return TMF_RESP_FUNC_FAILED
367 * if the device is not there and ready.
369 if (!isci_device
|| isci_device
->status
!= isci_ready_for_io
) {
370 dev_dbg(&isci_host
->pdev
->dev
,
371 "%s: isci_device = %p not ready (%d)\n",
373 isci_device
, isci_device
->status
);
374 return TMF_RESP_FUNC_FAILED
;
376 dev_dbg(&isci_host
->pdev
->dev
,
377 "%s: isci_device = %p\n",
378 __func__
, isci_device
);
380 sci_device
= &isci_device
->sci
;
382 /* Assign the pointer to the TMF's completion kernel wait structure. */
383 tmf
->complete
= &completion
;
385 isci_task_request_build(
392 dev_warn(&isci_host
->pdev
->dev
,
393 "%s: isci_task_request_build failed\n",
395 return TMF_RESP_FUNC_FAILED
;
398 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
400 /* start the TMF io. */
401 status
= scic_controller_start_task(
405 SCI_CONTROLLER_INVALID_IO_TAG
);
407 if (status
!= SCI_TASK_SUCCESS
) {
408 dev_warn(&isci_host
->pdev
->dev
,
409 "%s: start_io failed - status = 0x%x, request = %p\n",
413 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
414 goto cleanup_request
;
417 if (tmf
->cb_state_func
!= NULL
)
418 tmf
->cb_state_func(isci_tmf_started
, tmf
, tmf
->cb_data
);
420 isci_request_change_state(request
, started
);
422 /* add the request to the remote device request list. */
423 list_add(&request
->dev_node
, &isci_device
->reqs_in_process
);
425 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
427 /* Wait for the TMF to complete, or a timeout. */
428 timeleft
= wait_for_completion_timeout(&completion
,
429 jiffies
+ msecs_to_jiffies(timeout_ms
));
432 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
434 if (tmf
->cb_state_func
!= NULL
)
435 tmf
->cb_state_func(isci_tmf_timed_out
, tmf
, tmf
->cb_data
);
437 status
= scic_controller_terminate_request(
438 &request
->isci_host
->sci
,
439 &request
->isci_device
->sci
,
442 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
447 if (tmf
->status
== SCI_SUCCESS
)
448 ret
= TMF_RESP_FUNC_COMPLETE
;
449 else if (tmf
->status
== SCI_FAILURE_IO_RESPONSE_VALID
) {
450 dev_dbg(&isci_host
->pdev
->dev
,
452 "SCI_FAILURE_IO_RESPONSE_VALID\n",
454 ret
= TMF_RESP_FUNC_COMPLETE
;
456 /* Else - leave the default "failed" status alone. */
458 dev_dbg(&isci_host
->pdev
->dev
,
459 "%s: completed request = %p\n",
463 if (request
->io_request_completion
!= NULL
) {
464 /* A thread is waiting for this TMF to finish. */
465 complete(request
->io_request_completion
);
469 isci_request_free(isci_host
, request
);
473 void isci_task_build_tmf(
474 struct isci_tmf
*tmf
,
475 struct isci_remote_device
*isci_device
,
476 enum isci_tmf_function_codes code
,
477 void (*tmf_sent_cb
)(enum isci_tmf_cb_state
,
482 dev_dbg(&isci_device
->isci_port
->isci_host
->pdev
->dev
,
483 "%s: isci_device = %p\n", __func__
, isci_device
);
485 memset(tmf
, 0, sizeof(*tmf
));
487 tmf
->device
= isci_device
;
488 tmf
->tmf_code
= code
;
490 tmf
->cb_state_func
= tmf_sent_cb
;
491 tmf
->cb_data
= cb_data
;
494 static void isci_task_build_abort_task_tmf(
495 struct isci_tmf
*tmf
,
496 struct isci_remote_device
*isci_device
,
497 enum isci_tmf_function_codes code
,
498 void (*tmf_sent_cb
)(enum isci_tmf_cb_state
,
501 struct isci_request
*old_request
)
503 isci_task_build_tmf(tmf
, isci_device
, code
, tmf_sent_cb
,
504 (void *)old_request
);
505 tmf
->io_tag
= old_request
->io_tag
;
508 static struct isci_request
*isci_task_get_request_from_task(
509 struct sas_task
*task
,
510 struct isci_remote_device
**isci_device
)
513 struct isci_request
*request
= NULL
;
516 spin_lock_irqsave(&task
->task_state_lock
, flags
);
518 request
= task
->lldd_task
;
520 /* If task is already done, the request isn't valid */
521 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
) &&
522 (task
->task_state_flags
& SAS_TASK_AT_INITIATOR
) &&
525 if (isci_device
!= NULL
)
526 *isci_device
= request
->isci_device
;
529 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
535 * isci_task_validate_request_to_abort() - This function checks the given I/O
536 * against the "started" state. If the request is still "started", it's
537 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
538 * BEFORE CALLING THIS FUNCTION.
539 * @isci_request: This parameter specifies the request object to control.
540 * @isci_host: This parameter specifies the ISCI host object
541 * @isci_device: This is the device to which the request is pending.
542 * @aborted_io_completion: This is a completion structure that will be added to
543 * the request in case it is changed to aborting; this completion is
544 * triggered when the request is fully completed.
546 * Either "started" on successful change of the task status to "aborted", or
547 * "unallocated" if the task cannot be controlled.
549 static enum isci_request_status
isci_task_validate_request_to_abort(
550 struct isci_request
*isci_request
,
551 struct isci_host
*isci_host
,
552 struct isci_remote_device
*isci_device
,
553 struct completion
*aborted_io_completion
)
555 enum isci_request_status old_state
= unallocated
;
557 /* Only abort the task if it's in the
558 * device's request_in_process list
560 if (isci_request
&& !list_empty(&isci_request
->dev_node
)) {
561 old_state
= isci_request_change_started_to_aborted(
562 isci_request
, aborted_io_completion
);
569 static void isci_request_cleanup_completed_loiterer(
570 struct isci_host
*isci_host
,
571 struct isci_remote_device
*isci_device
,
572 struct isci_request
*isci_request
)
574 struct sas_task
*task
;
577 task
= (isci_request
->ttype
== io_task
)
578 ? isci_request_access_task(isci_request
)
581 dev_dbg(&isci_host
->pdev
->dev
,
582 "%s: isci_device=%p, request=%p, task=%p\n",
583 __func__
, isci_device
, isci_request
, task
);
585 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
586 list_del_init(&isci_request
->dev_node
);
587 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
591 spin_lock_irqsave(&task
->task_state_lock
, flags
);
592 task
->lldd_task
= NULL
;
594 isci_set_task_doneflags(task
);
596 /* If this task is not in the abort path, call task_done. */
597 if (!(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
599 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
600 task
->task_done(task
);
602 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
604 isci_request_free(isci_host
, isci_request
);
608 * @isci_termination_timed_out(): this function will deal with a request for
609 * which the wait for termination has timed-out.
611 * @isci_host This SCU.
612 * @isci_request The I/O request being terminated.
615 isci_termination_timed_out(
616 struct isci_host
* host
,
617 struct isci_request
* request
620 unsigned long state_flags
;
622 dev_warn(&host
->pdev
->dev
,
623 "%s: host = %p; request = %p\n",
624 __func__
, host
, request
);
626 /* At this point, the request to terminate
627 * has timed out. The best we can do is to
628 * have the request die a silent death
629 * if it ever completes.
631 spin_lock_irqsave(&request
->state_lock
, state_flags
);
633 if (request
->status
== started
) {
635 /* Set the request state to "dead",
636 * and clear the task pointer so that an actual
637 * completion event callback doesn't do
640 request
->status
= dead
;
642 /* Clear the timeout completion event pointer.*/
643 request
->io_request_completion
= NULL
;
645 if (request
->ttype
== io_task
) {
647 /* Break links with the sas_task. */
648 if (request
->ttype_ptr
.io_task_ptr
!= NULL
) {
650 request
->ttype_ptr
.io_task_ptr
->lldd_task
= NULL
;
651 request
->ttype_ptr
.io_task_ptr
= NULL
;
655 spin_unlock_irqrestore(&request
->state_lock
, state_flags
);
660 * isci_terminate_request_core() - This function will terminate the given
661 * request, and wait for it to complete. This function must only be called
662 * from a thread that can wait. Note that the request is terminated and
663 * completed (back to the host, if started there).
664 * @isci_host: This SCU.
665 * @isci_device: The target.
666 * @isci_request: The I/O request to be terminated.
670 static void isci_terminate_request_core(
671 struct isci_host
*isci_host
,
672 struct isci_remote_device
*isci_device
,
673 struct isci_request
*isci_request
)
675 enum sci_status status
= SCI_SUCCESS
;
676 bool was_terminated
= false;
677 bool needs_cleanup_handling
= false;
678 enum isci_request_status request_status
;
680 unsigned long timeout_remaining
;
683 dev_dbg(&isci_host
->pdev
->dev
,
684 "%s: device = %p; request = %p\n",
685 __func__
, isci_device
, isci_request
);
687 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
689 /* Note that we are not going to control
690 * the target to abort the request.
692 isci_request
->complete_in_target
= true;
694 /* Make sure the request wasn't just sitting around signalling
695 * device condition (if the request handle is NULL, then the
696 * request completed but needed additional handling here).
698 if (!isci_request
->terminated
) {
699 was_terminated
= true;
700 needs_cleanup_handling
= true;
701 status
= scic_controller_terminate_request(
706 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
709 * The only time the request to terminate will
710 * fail is when the io request is completed and
713 if (status
!= SCI_SUCCESS
) {
714 dev_err(&isci_host
->pdev
->dev
,
715 "%s: scic_controller_terminate_request"
716 " returned = 0x%x\n",
719 /* Clear the completion pointer from the request. */
720 isci_request
->io_request_completion
= NULL
;
723 if (was_terminated
) {
724 dev_dbg(&isci_host
->pdev
->dev
,
725 "%s: before completion wait (%p)\n",
727 isci_request
->io_request_completion
);
729 /* Wait here for the request to complete. */
730 #define TERMINATION_TIMEOUT_MSEC 50
732 = wait_for_completion_timeout(
733 isci_request
->io_request_completion
,
734 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC
));
736 if (!timeout_remaining
) {
738 isci_termination_timed_out(isci_host
,
741 dev_err(&isci_host
->pdev
->dev
,
742 "%s: *** Timeout waiting for "
743 "termination(%p/%p)\n",
745 isci_request
->io_request_completion
,
749 dev_dbg(&isci_host
->pdev
->dev
,
750 "%s: after completion wait (%p)\n",
752 isci_request
->io_request_completion
);
754 /* Clear the completion pointer from the request. */
755 isci_request
->io_request_completion
= NULL
;
757 /* Peek at the status of the request. This will tell
758 * us if there was special handling on the request such that it
759 * needs to be detached and freed here.
761 spin_lock_irqsave(&isci_request
->state_lock
, flags
);
762 request_status
= isci_request_get_state(isci_request
);
764 if ((isci_request
->ttype
== io_task
) /* TMFs are in their own thread */
765 && ((request_status
== aborted
)
766 || (request_status
== aborting
)
767 || (request_status
== terminating
)
768 || (request_status
== completed
)
769 || (request_status
== dead
)
773 /* The completion routine won't free a request in
774 * the aborted/aborting/etc. states, so we do
777 needs_cleanup_handling
= true;
779 spin_unlock_irqrestore(&isci_request
->state_lock
, flags
);
781 if (needs_cleanup_handling
)
782 isci_request_cleanup_completed_loiterer(
783 isci_host
, isci_device
, isci_request
788 static void isci_terminate_request(
789 struct isci_host
*isci_host
,
790 struct isci_remote_device
*isci_device
,
791 struct isci_request
*isci_request
,
792 enum isci_request_status new_request_state
)
794 enum isci_request_status old_state
;
795 DECLARE_COMPLETION_ONSTACK(request_completion
);
797 /* Change state to "new_request_state" if it is currently "started" */
798 old_state
= isci_request_change_started_to_newstate(
804 if ((old_state
== started
) ||
805 (old_state
== completed
) ||
806 (old_state
== aborting
)) {
808 /* If the old_state is started:
809 * This request was not already being aborted. If it had been,
810 * then the aborting I/O (ie. the TMF request) would not be in
811 * the aborting state, and thus would be terminated here. Note
812 * that since the TMF completion's call to the kernel function
813 * "complete()" does not happen until the pending I/O request
814 * terminate fully completes, we do not have to implement a
815 * special wait here for already aborting requests - the
816 * termination of the TMF request will force the request
817 * to finish it's already started terminate.
819 * If old_state == completed:
820 * This request completed from the SCU hardware perspective
821 * and now just needs cleaning up in terms of freeing the
822 * request and potentially calling up to libsas.
824 * If old_state == aborting:
825 * This request has already gone through a TMF timeout, but may
826 * not have been terminated; needs cleaning up at least.
828 isci_terminate_request_core(isci_host
, isci_device
,
834 * isci_terminate_pending_requests() - This function will change the all of the
835 * requests on the given device's state to "aborting", will terminate the
836 * requests, and wait for them to complete. This function must only be
837 * called from a thread that can wait. Note that the requests are all
838 * terminated and completed (back to the host, if started there).
839 * @isci_host: This parameter specifies SCU.
840 * @isci_device: This parameter specifies the target.
844 void isci_terminate_pending_requests(
845 struct isci_host
*isci_host
,
846 struct isci_remote_device
*isci_device
,
847 enum isci_request_status new_request_state
)
849 struct isci_request
*request
;
850 struct isci_request
*next_request
;
852 struct list_head aborted_request_list
;
854 INIT_LIST_HEAD(&aborted_request_list
);
856 dev_dbg(&isci_host
->pdev
->dev
,
857 "%s: isci_device = %p (new request state = %d)\n",
858 __func__
, isci_device
, new_request_state
);
860 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
862 /* Move all of the pending requests off of the device list. */
863 list_splice_init(&isci_device
->reqs_in_process
,
864 &aborted_request_list
);
866 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
868 /* Iterate through the now-local list. */
869 list_for_each_entry_safe(request
, next_request
,
870 &aborted_request_list
, dev_node
) {
872 dev_warn(&isci_host
->pdev
->dev
,
873 "%s: isci_device=%p request=%p; task=%p\n",
875 isci_device
, request
,
876 ((request
->ttype
== io_task
)
877 ? isci_request_access_task(request
)
880 /* Mark all still pending I/O with the selected next
881 * state, terminate and free it.
883 isci_terminate_request(isci_host
, isci_device
,
884 request
, new_request_state
890 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
891 * Template functions.
892 * @lun: This parameter specifies the lun to be reset.
894 * status, zero indicates success.
896 static int isci_task_send_lu_reset_sas(
897 struct isci_host
*isci_host
,
898 struct isci_remote_device
*isci_device
,
902 int ret
= TMF_RESP_FUNC_FAILED
;
904 dev_dbg(&isci_host
->pdev
->dev
,
905 "%s: isci_host = %p, isci_device = %p\n",
906 __func__
, isci_host
, isci_device
);
907 /* Send the LUN reset to the target. By the time the call returns,
908 * the TMF has fully exected in the target (in which case the return
909 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
910 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
912 isci_task_build_tmf(&tmf
, isci_device
, isci_tmf_ssp_lun_reset
, NULL
,
915 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
916 ret
= isci_task_execute_tmf(isci_host
, &tmf
, ISCI_LU_RESET_TIMEOUT_MS
);
918 if (ret
== TMF_RESP_FUNC_COMPLETE
)
919 dev_dbg(&isci_host
->pdev
->dev
,
920 "%s: %p: TMF_LU_RESET passed\n",
921 __func__
, isci_device
);
923 dev_dbg(&isci_host
->pdev
->dev
,
924 "%s: %p: TMF_LU_RESET failed (%x)\n",
925 __func__
, isci_device
, ret
);
931 * isci_task_lu_reset() - This function is one of the SAS Domain Template
932 * functions. This is one of the Task Management functoins called by libsas,
933 * to reset the given lun. Note the assumption that while this call is
934 * executing, no I/O will be sent by the host to the device.
935 * @lun: This parameter specifies the lun to be reset.
937 * status, zero indicates success.
939 int isci_task_lu_reset(struct domain_device
*domain_device
, u8
*lun
)
941 struct isci_host
*isci_host
= dev_to_ihost(domain_device
);
942 struct isci_remote_device
*isci_device
= NULL
;
944 bool device_stopping
= false;
946 isci_device
= domain_device
->lldd_dev
;
948 dev_dbg(&isci_host
->pdev
->dev
,
949 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
950 __func__
, domain_device
, isci_host
, isci_device
);
952 if (isci_device
!= NULL
) {
953 device_stopping
= (isci_device
->status
== isci_stopping
)
954 || (isci_device
->status
== isci_stopped
);
955 set_bit(IDEV_EH
, &isci_device
->flags
);
958 /* If there is a device reset pending on any request in the
959 * device's list, fail this LUN reset request in order to
960 * escalate to the device reset.
962 if (!isci_device
|| device_stopping
||
963 isci_device_is_reset_pending(isci_host
, isci_device
)) {
964 dev_warn(&isci_host
->pdev
->dev
,
965 "%s: No dev (%p), or "
966 "RESET PENDING: domain_device=%p\n",
967 __func__
, isci_device
, domain_device
);
968 return TMF_RESP_FUNC_FAILED
;
971 /* Send the task management part of the reset. */
972 if (sas_protocol_ata(domain_device
->tproto
)) {
973 ret
= isci_task_send_lu_reset_sata(isci_host
, isci_device
, lun
);
975 ret
= isci_task_send_lu_reset_sas(isci_host
, isci_device
, lun
);
977 /* If the LUN reset worked, all the I/O can now be terminated. */
978 if (ret
== TMF_RESP_FUNC_COMPLETE
)
979 /* Terminate all I/O now. */
980 isci_terminate_pending_requests(isci_host
,
988 /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
989 int isci_task_clear_nexus_port(struct asd_sas_port
*port
)
991 return TMF_RESP_FUNC_FAILED
;
996 int isci_task_clear_nexus_ha(struct sas_ha_struct
*ha
)
998 return TMF_RESP_FUNC_FAILED
;
1001 /* Task Management Functions. Must be called from process context. */
1004 * isci_abort_task_process_cb() - This is a helper function for the abort task
1005 * TMF command. It manages the request state with respect to the successful
1006 * transmission / completion of the abort task request.
1007 * @cb_state: This parameter specifies when this function was called - after
1008 * the TMF request has been started and after it has timed-out.
1009 * @tmf: This parameter specifies the TMF in progress.
1013 static void isci_abort_task_process_cb(
1014 enum isci_tmf_cb_state cb_state
,
1015 struct isci_tmf
*tmf
,
1018 struct isci_request
*old_request
;
1020 old_request
= (struct isci_request
*)cb_data
;
1022 dev_dbg(&old_request
->isci_host
->pdev
->dev
,
1023 "%s: tmf=%p, old_request=%p\n",
1024 __func__
, tmf
, old_request
);
1028 case isci_tmf_started
:
1029 /* The TMF has been started. Nothing to do here, since the
1030 * request state was already set to "aborted" by the abort
1033 if ((old_request
->status
!= aborted
)
1034 && (old_request
->status
!= completed
))
1035 dev_err(&old_request
->isci_host
->pdev
->dev
,
1036 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
1037 __func__
, old_request
->status
, tmf
, old_request
);
1040 case isci_tmf_timed_out
:
1042 /* Set the task's state to "aborting", since the abort task
1043 * function thread set it to "aborted" (above) in anticipation
1044 * of the task management request working correctly. Since the
1045 * timeout has now fired, the TMF request failed. We set the
1046 * state such that the request completion will indicate the
1047 * device is no longer present.
1049 isci_request_change_state(old_request
, aborting
);
1053 dev_err(&old_request
->isci_host
->pdev
->dev
,
1054 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1055 __func__
, cb_state
, tmf
, old_request
);
1061 * isci_task_abort_task() - This function is one of the SAS Domain Template
1062 * functions. This function is called by libsas to abort a specified task.
1063 * @task: This parameter specifies the SAS task to abort.
1065 * status, zero indicates success.
1067 int isci_task_abort_task(struct sas_task
*task
)
1069 struct isci_host
*isci_host
= dev_to_ihost(task
->dev
);
1070 DECLARE_COMPLETION_ONSTACK(aborted_io_completion
);
1071 struct isci_request
*old_request
= NULL
;
1072 enum isci_request_status old_state
;
1073 struct isci_remote_device
*isci_device
= NULL
;
1074 struct isci_tmf tmf
;
1075 int ret
= TMF_RESP_FUNC_FAILED
;
1076 unsigned long flags
;
1077 bool any_dev_reset
= false;
1078 bool device_stopping
;
1080 /* Get the isci_request reference from the task. Note that
1081 * this check does not depend on the pending request list
1082 * in the device, because tasks driving resets may land here
1083 * after completion in the core.
1085 old_request
= isci_task_get_request_from_task(task
, &isci_device
);
1087 dev_dbg(&isci_host
->pdev
->dev
,
1088 "%s: task = %p\n", __func__
, task
);
1090 /* Check if the device has been / is currently being removed.
1091 * If so, no task management will be done, and the I/O will
1094 device_stopping
= (isci_device
->status
== isci_stopping
)
1095 || (isci_device
->status
== isci_stopped
);
1097 /* XXX need to fix device lookup lifetime (needs to be done
1098 * under scic_lock, among other things...), but for now assume
1099 * the device is available like the above code
1101 set_bit(IDEV_EH
, &isci_device
->flags
);
1103 /* This version of the driver will fail abort requests for
1104 * SATA/STP. Failing the abort request this way will cause the
1105 * SCSI error handler thread to escalate to LUN reset
1107 if (sas_protocol_ata(task
->task_proto
) && !device_stopping
) {
1108 dev_warn(&isci_host
->pdev
->dev
,
1109 " task %p is for a STP/SATA device;"
1110 " returning TMF_RESP_FUNC_FAILED\n"
1111 " to cause a LUN reset...\n", task
);
1112 return TMF_RESP_FUNC_FAILED
;
1115 dev_dbg(&isci_host
->pdev
->dev
,
1116 "%s: old_request == %p\n", __func__
, old_request
);
1118 if (!device_stopping
)
1119 any_dev_reset
= isci_device_is_reset_pending(isci_host
,isci_device
);
1121 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1123 /* Don't do resets to stopping devices. */
1124 if (device_stopping
) {
1126 task
->task_state_flags
&= ~SAS_TASK_NEED_DEV_RESET
;
1127 any_dev_reset
= false;
1129 } else /* See if there is a pending device reset for this device. */
1130 any_dev_reset
= any_dev_reset
1131 || (task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
);
1133 /* If the extraction of the request reference from the task
1134 * failed, then the request has been completed (or if there is a
1135 * pending reset then this abort request function must be failed
1136 * in order to escalate to the target reset).
1138 if ((old_request
== NULL
) || any_dev_reset
) {
1140 /* If the device reset task flag is set, fail the task
1141 * management request. Otherwise, the original request
1144 if (any_dev_reset
) {
1146 /* Turn off the task's DONE to make sure this
1147 * task is escalated to a target reset.
1149 task
->task_state_flags
&= ~SAS_TASK_STATE_DONE
;
1151 /* Make the reset happen as soon as possible. */
1152 task
->task_state_flags
|= SAS_TASK_NEED_DEV_RESET
;
1154 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1156 /* Fail the task management request in order to
1157 * escalate to the target reset.
1159 ret
= TMF_RESP_FUNC_FAILED
;
1161 dev_dbg(&isci_host
->pdev
->dev
,
1162 "%s: Failing task abort in order to "
1163 "escalate to target reset because\n"
1164 "SAS_TASK_NEED_DEV_RESET is set for "
1165 "task %p on dev %p\n",
1166 __func__
, task
, isci_device
);
1170 /* The request has already completed and there
1171 * is nothing to do here other than to set the task
1172 * done bit, and indicate that the task abort function
1175 isci_set_task_doneflags(task
);
1177 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1179 ret
= TMF_RESP_FUNC_COMPLETE
;
1181 dev_dbg(&isci_host
->pdev
->dev
,
1182 "%s: abort task not needed for %p\n",
1189 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1191 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
1193 /* Check the request status and change to "aborted" if currently
1194 * "starting"; if true then set the I/O kernel completion
1195 * struct that will be triggered when the request completes.
1197 old_state
= isci_task_validate_request_to_abort(
1198 old_request
, isci_host
, isci_device
,
1199 &aborted_io_completion
);
1200 if ((old_state
!= started
) &&
1201 (old_state
!= completed
) &&
1202 (old_state
!= aborting
)) {
1204 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
1206 /* The request was already being handled by someone else (because
1207 * they got to set the state away from started).
1209 dev_dbg(&isci_host
->pdev
->dev
,
1210 "%s: device = %p; old_request %p already being aborted\n",
1212 isci_device
, old_request
);
1214 return TMF_RESP_FUNC_COMPLETE
;
1216 if ((task
->task_proto
== SAS_PROTOCOL_SMP
)
1218 || old_request
->complete_in_target
1221 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
1223 dev_dbg(&isci_host
->pdev
->dev
,
1224 "%s: SMP request (%d)"
1225 " or device is stopping (%d)"
1226 " or complete_in_target (%d), thus no TMF\n",
1227 __func__
, (task
->task_proto
== SAS_PROTOCOL_SMP
),
1228 device_stopping
, old_request
->complete_in_target
);
1230 /* Set the state on the task. */
1231 isci_task_all_done(task
);
1233 ret
= TMF_RESP_FUNC_COMPLETE
;
1235 /* Stopping and SMP devices are not sent a TMF, and are not
1236 * reset, but the outstanding I/O request is terminated below.
1239 /* Fill in the tmf stucture */
1240 isci_task_build_abort_task_tmf(&tmf
, isci_device
,
1241 isci_tmf_ssp_task_abort
,
1242 isci_abort_task_process_cb
,
1245 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
1247 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1248 ret
= isci_task_execute_tmf(isci_host
, &tmf
,
1249 ISCI_ABORT_TASK_TIMEOUT_MS
);
1251 if (ret
!= TMF_RESP_FUNC_COMPLETE
)
1252 dev_err(&isci_host
->pdev
->dev
,
1253 "%s: isci_task_send_tmf failed\n",
1256 if (ret
== TMF_RESP_FUNC_COMPLETE
) {
1257 old_request
->complete_in_target
= true;
1259 /* Clean up the request on our side, and wait for the aborted I/O to
1262 isci_terminate_request_core(isci_host
, isci_device
, old_request
);
1265 /* Make sure we do not leave a reference to aborted_io_completion */
1266 old_request
->io_request_completion
= NULL
;
1271 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1272 * functions. This is one of the Task Management functoins called by libsas,
1273 * to abort all task for the given lun.
1274 * @d_device: This parameter specifies the domain device associated with this
1276 * @lun: This parameter specifies the lun associated with this request.
1278 * status, zero indicates success.
1280 int isci_task_abort_task_set(
1281 struct domain_device
*d_device
,
1284 return TMF_RESP_FUNC_FAILED
;
1289 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1290 * functions. This is one of the Task Management functoins called by libsas.
1291 * @d_device: This parameter specifies the domain device associated with this
1293 * @lun: This parameter specifies the lun associated with this request.
1295 * status, zero indicates success.
1297 int isci_task_clear_aca(
1298 struct domain_device
*d_device
,
1301 return TMF_RESP_FUNC_FAILED
;
1307 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1308 * functions. This is one of the Task Management functoins called by libsas.
1309 * @d_device: This parameter specifies the domain device associated with this
1311 * @lun: This parameter specifies the lun associated with this request.
1313 * status, zero indicates success.
1315 int isci_task_clear_task_set(
1316 struct domain_device
*d_device
,
1319 return TMF_RESP_FUNC_FAILED
;
1324 * isci_task_query_task() - This function is implemented to cause libsas to
1325 * correctly escalate the failed abort to a LUN or target reset (this is
1326 * because sas_scsi_find_task libsas function does not correctly interpret
1327 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1328 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1329 * returned, libsas will turn this into a target reset
1330 * @task: This parameter specifies the sas task being queried.
1331 * @lun: This parameter specifies the lun associated with this request.
1333 * status, zero indicates success.
1335 int isci_task_query_task(
1336 struct sas_task
*task
)
1338 /* See if there is a pending device reset for this device. */
1339 if (task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
)
1340 return TMF_RESP_FUNC_FAILED
;
1342 return TMF_RESP_FUNC_SUCC
;
1346 * isci_task_request_complete() - This function is called by the sci core when
1347 * an task request completes.
1348 * @ihost: This parameter specifies the ISCI host object
1349 * @ireq: This parameter is the completed isci_request object.
1350 * @completion_status: This parameter specifies the completion status from the
1356 isci_task_request_complete(struct isci_host
*ihost
,
1357 struct isci_request
*ireq
,
1358 enum sci_task_status completion_status
)
1360 struct isci_remote_device
*idev
= ireq
->isci_device
;
1361 enum isci_request_status old_state
;
1362 struct isci_tmf
*tmf
= isci_request_access_tmf(ireq
);
1363 struct completion
*tmf_complete
;
1364 struct scic_sds_request
*sci_req
= &ireq
->sci
;
1366 dev_dbg(&ihost
->pdev
->dev
,
1367 "%s: request = %p, status=%d\n",
1368 __func__
, ireq
, completion_status
);
1370 old_state
= isci_request_change_state(ireq
, completed
);
1372 tmf
->status
= completion_status
;
1373 ireq
->complete_in_target
= true;
1375 if (tmf
->proto
== SAS_PROTOCOL_SSP
) {
1376 memcpy(&tmf
->resp
.resp_iu
,
1378 SSP_RESP_IU_MAX_SIZE
);
1379 } else if (tmf
->proto
== SAS_PROTOCOL_SATA
) {
1380 memcpy(&tmf
->resp
.d2h_fis
,
1382 sizeof(struct dev_to_host_fis
));
1385 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1386 tmf_complete
= tmf
->complete
;
1388 scic_controller_complete_io(&ihost
->sci
, &idev
->sci
, &ireq
->sci
);
1389 /* set the 'terminated' flag handle to make sure it cannot be terminated
1390 * or completed again.
1392 ireq
->terminated
= true;;
1394 isci_request_change_state(ireq
, unallocated
);
1395 list_del_init(&ireq
->dev_node
);
1397 /* The task management part completes last. */
1398 complete(tmf_complete
);
1401 static int isci_reset_device(struct domain_device
*dev
, int hard_reset
)
1403 struct isci_remote_device
*idev
= dev
->lldd_dev
;
1404 struct sas_phy
*phy
= sas_find_local_phy(dev
);
1405 struct isci_host
*ihost
= dev_to_ihost(dev
);
1406 enum sci_status status
;
1407 unsigned long flags
;
1410 dev_dbg(&ihost
->pdev
->dev
, "%s: idev %p\n", __func__
, idev
);
1413 dev_warn(&ihost
->pdev
->dev
,
1414 "%s: idev is GONE!\n",
1417 return TMF_RESP_FUNC_COMPLETE
; /* Nothing to reset. */
1420 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1421 status
= scic_remote_device_reset(&idev
->sci
);
1422 if (status
!= SCI_SUCCESS
) {
1423 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
1425 dev_warn(&ihost
->pdev
->dev
,
1426 "%s: scic_remote_device_reset(%p) returned %d!\n",
1427 __func__
, idev
, status
);
1429 return TMF_RESP_FUNC_FAILED
;
1431 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
1433 /* Make sure all pending requests are able to be fully terminated. */
1434 isci_device_clear_reset_pending(ihost
, idev
);
1436 rc
= sas_phy_reset(phy
, hard_reset
);
1437 msleep(2000); /* just like mvsas */
1439 /* Terminate in-progress I/O now. */
1440 isci_remote_device_nuke_requests(ihost
, idev
);
1442 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1443 status
= scic_remote_device_reset_complete(&idev
->sci
);
1444 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
1446 if (status
!= SCI_SUCCESS
) {
1447 dev_warn(&ihost
->pdev
->dev
,
1448 "%s: scic_remote_device_reset_complete(%p) "
1449 "returned %d!\n", __func__
, idev
, status
);
1452 dev_dbg(&ihost
->pdev
->dev
, "%s: idev %p complete.\n", __func__
, idev
);
1457 int isci_task_I_T_nexus_reset(struct domain_device
*dev
)
1459 struct isci_host
*ihost
= dev_to_ihost(dev
);
1460 int ret
= TMF_RESP_FUNC_FAILED
, hard_reset
= 1;
1461 struct isci_remote_device
*idev
;
1462 unsigned long flags
;
1464 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1465 * being too paranoid, or is mvsas busted?!
1467 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1468 idev
= dev
->lldd_dev
;
1469 if (!idev
|| !test_bit(IDEV_EH
, &idev
->flags
))
1470 ret
= TMF_RESP_FUNC_COMPLETE
;
1471 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
1473 if (ret
== TMF_RESP_FUNC_COMPLETE
)
1476 if (dev
->dev_type
== SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_STP
))
1479 return isci_reset_device(dev
, hard_reset
);
1482 int isci_bus_reset_handler(struct scsi_cmnd
*cmd
)
1484 struct domain_device
*dev
= sdev_to_domain_dev(cmd
->device
);
1487 if (dev
->dev_type
== SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_STP
))
1490 return isci_reset_device(dev
, hard_reset
);