isci: Properly handle requests in the "aborting" state.
[linux-2.6.git] / drivers / scsi / isci / task.c
blob338f08ec4d8eb13208e3fc13071dced470cdc0ff
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.
5 * GPL LICENSE SUMMARY
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.
24 * BSD LICENSE
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
31 * are met:
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.
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>
58 #include <scsi/sas_ata.h>
59 #include "scic_task_request.h"
60 #include "scic_remote_device.h"
61 #include "scic_io_request.h"
62 #include "scic_sds_remote_device.h"
63 #include "scic_sds_remote_node_context.h"
64 #include "isci.h"
65 #include "request.h"
66 #include "sata.h"
67 #include "task.h"
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.
78 static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
79 enum service_response response,
80 enum exec_status status)
83 enum isci_completion_selection disposition;
85 disposition = isci_perform_normal_io_completion;
86 disposition = isci_task_set_completion_status(task, response, status,
87 disposition);
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.
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, status=%d\n",
97 __func__, task, response, status);
99 task->lldd_task = NULL;
100 if (dev_is_sata(task->dev)) {
101 /* Since we are still in the submit path, and since
102 * libsas takes the host lock on behalf of SATA
103 * devices before I/O starts, we need to unlock
104 * before we can call back and report the I/O
105 * submission error.
107 unsigned long flags;
109 raw_local_irq_save(flags);
110 spin_unlock(ihost->shost->host_lock);
111 task->task_done(task);
112 spin_lock(ihost->shost->host_lock);
113 raw_local_irq_restore(flags);
114 } else
115 task->task_done(task);
116 break;
118 case isci_perform_aborted_io_completion:
119 /* No notification because this request is already in the
120 * abort path.
122 dev_warn(&ihost->pdev->dev,
123 "%s: Aborted - task = %p, response=%d, status=%d\n",
124 __func__, task, response, status);
125 break;
127 case isci_perform_error_io_completion:
128 /* Use sas_task_abort */
129 dev_warn(&ihost->pdev->dev,
130 "%s: Error - task = %p, response=%d, status=%d\n",
131 __func__, task, response, status);
132 sas_task_abort(task);
133 break;
135 default:
136 dev_warn(&ihost->pdev->dev,
137 "%s: isci task notification default case!",
138 __func__);
139 sas_task_abort(task);
140 break;
144 #define for_each_sas_task(num, task) \
145 for (; num > 0; num--,\
146 task = list_entry(task->list.next, struct sas_task, list))
149 * isci_task_execute_task() - This function is one of the SAS Domain Template
150 * functions. This function is called by libsas to send a task down to
151 * hardware.
152 * @task: This parameter specifies the SAS task to send.
153 * @num: This parameter specifies the number of tasks to queue.
154 * @gfp_flags: This parameter specifies the context of this call.
156 * status, zero indicates success.
158 int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
160 struct isci_host *ihost = task->dev->port->ha->lldd_ha;
161 struct isci_request *request = NULL;
162 struct isci_remote_device *device;
163 unsigned long flags;
164 int ret;
165 enum sci_status status;
166 enum isci_status device_status;
168 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
170 /* Check if we have room for more tasks */
171 ret = isci_host_can_queue(ihost, num);
173 if (ret) {
174 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
175 return ret;
178 for_each_sas_task(num, task) {
179 dev_dbg(&ihost->pdev->dev,
180 "task = %p, num = %d; dev = %p; cmd = %p\n",
181 task, num, task->dev, task->uldd_task);
183 device = isci_dev_from_domain_dev(task->dev);
185 if (device)
186 device_status = device->status;
187 else
188 device_status = isci_freed;
190 /* From this point onward, any process that needs to guarantee
191 * that there is no kernel I/O being started will have to wait
192 * for the quiesce spinlock.
195 if (device_status != isci_ready_for_io) {
197 /* Forces a retry from scsi mid layer. */
198 dev_warn(&ihost->pdev->dev,
199 "%s: task %p: isci_host->status = %d, "
200 "device = %p; device_status = 0x%x\n\n",
201 __func__,
202 task,
203 isci_host_get_state(ihost),
204 device, device_status);
206 if (device_status == isci_ready) {
207 /* Indicate QUEUE_FULL so that the scsi midlayer
208 * retries.
210 isci_task_refuse(ihost, task,
211 SAS_TASK_COMPLETE,
212 SAS_QUEUE_FULL);
213 } else {
214 /* Else, the device is going down. */
215 isci_task_refuse(ihost, task,
216 SAS_TASK_UNDELIVERED,
217 SAS_DEVICE_UNKNOWN);
219 isci_host_can_dequeue(ihost, 1);
220 } else {
221 /* There is a device and it's ready for I/O. */
222 spin_lock_irqsave(&task->task_state_lock, flags);
224 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
226 spin_unlock_irqrestore(&task->task_state_lock,
227 flags);
229 isci_task_refuse(ihost, task,
230 SAS_TASK_UNDELIVERED,
231 SAM_STAT_TASK_ABORTED);
233 /* The I/O was aborted. */
235 } else {
236 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
237 spin_unlock_irqrestore(&task->task_state_lock, flags);
239 /* build and send the request. */
240 status = isci_request_execute(ihost, task, &request,
241 gfp_flags);
243 if (status != SCI_SUCCESS) {
245 spin_lock_irqsave(&task->task_state_lock, flags);
246 /* Did not really start this command. */
247 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
248 spin_unlock_irqrestore(&task->task_state_lock, flags);
250 /* Indicate QUEUE_FULL so that the scsi
251 * midlayer retries. if the request
252 * failed for remote device reasons,
253 * it gets returned as
254 * SAS_TASK_UNDELIVERED next time
255 * through.
257 isci_task_refuse(ihost, task,
258 SAS_TASK_COMPLETE,
259 SAS_QUEUE_FULL);
260 isci_host_can_dequeue(ihost, 1);
265 return 0;
271 * isci_task_request_build() - This function builds the task request object.
272 * @isci_host: This parameter specifies the ISCI host object
273 * @request: This parameter points to the isci_request object allocated in the
274 * request construct function.
275 * @tmf: This parameter is the task management struct to be built
277 * SCI_SUCCESS on successfull completion, or specific failure code.
279 static enum sci_status isci_task_request_build(
280 struct isci_host *isci_host,
281 struct isci_request **isci_request,
282 struct isci_tmf *isci_tmf)
284 struct scic_sds_remote_device *sci_device;
285 enum sci_status status = SCI_FAILURE;
286 struct isci_request *request;
287 struct isci_remote_device *isci_device;
288 /* struct sci_sas_identify_address_frame_protocols dev_protocols; */
289 struct smp_discover_response_protocols dev_protocols;
292 dev_dbg(&isci_host->pdev->dev,
293 "%s: isci_tmf = %p\n", __func__, isci_tmf);
295 isci_device = isci_tmf->device;
296 sci_device = to_sci_dev(isci_device);
298 /* do common allocation and init of request object. */
299 status = isci_request_alloc_tmf(
300 isci_host,
301 isci_tmf,
302 &request,
303 isci_device,
304 GFP_ATOMIC
307 if (status != SCI_SUCCESS)
308 goto out;
310 /* let the core do it's construct. */
311 status = scic_task_request_construct(
312 isci_host->core_controller,
313 sci_device,
314 SCI_CONTROLLER_INVALID_IO_TAG,
315 request,
316 request->sci_request_mem_ptr,
317 &request->sci_request_handle
320 if (status != SCI_SUCCESS) {
321 dev_warn(&isci_host->pdev->dev,
322 "%s: scic_task_request_construct failed - "
323 "status = 0x%x\n",
324 __func__,
325 status);
326 goto errout;
329 sci_object_set_association(
330 request->sci_request_handle,
331 request
334 scic_remote_device_get_protocols(
335 sci_device,
336 &dev_protocols
339 /* let the core do it's protocol
340 * specific construction.
342 if (dev_protocols.u.bits.attached_ssp_target) {
344 isci_tmf->proto = SAS_PROTOCOL_SSP;
345 status = scic_task_request_construct_ssp(
346 request->sci_request_handle
348 if (status != SCI_SUCCESS)
349 goto errout;
352 if (dev_protocols.u.bits.attached_stp_target) {
354 isci_tmf->proto = SAS_PROTOCOL_SATA;
355 status = isci_sata_management_task_request_build(request);
357 if (status != SCI_SUCCESS)
358 goto errout;
361 goto out;
363 errout:
365 /* release the dma memory if we fail. */
366 isci_request_free(isci_host, request);
367 request = NULL;
369 out:
370 *isci_request = request;
371 return status;
375 * isci_tmf_timeout_cb() - This function is called as a kernel callback when
376 * the timeout period for the TMF has expired.
380 static void isci_tmf_timeout_cb(void *tmf_request_arg)
382 struct isci_request *request = (struct isci_request *)tmf_request_arg;
383 struct isci_tmf *tmf = isci_request_access_tmf(request);
384 enum sci_status status;
386 BUG_ON(request->ttype != tmf_task);
388 /* This task management request has timed-out. Terminate the request
389 * so that the request eventually completes to the requestor in the
390 * request completion callback path.
392 /* Note - the timer callback function itself has provided spinlock
393 * exclusion from the start and completion paths. No need to take
394 * the request->isci_host->scic_lock here.
397 if (tmf->timeout_timer != NULL) {
398 /* Call the users callback, if any. */
399 if (tmf->cb_state_func != NULL)
400 tmf->cb_state_func(isci_tmf_timed_out, tmf,
401 tmf->cb_data);
403 /* Terminate the TMF transmit request. */
404 status = scic_controller_terminate_request(
405 request->isci_host->core_controller,
406 to_sci_dev(request->isci_device),
407 request->sci_request_handle
410 dev_dbg(&request->isci_host->pdev->dev,
411 "%s: tmf_request = %p; tmf = %p; status = %d\n",
412 __func__, request, tmf, status);
413 } else
414 dev_dbg(&request->isci_host->pdev->dev,
415 "%s: timer already canceled! "
416 "tmf_request = %p; tmf = %p\n",
417 __func__, request, tmf);
419 /* No need to unlock since the caller to this callback is doing it for
420 * us.
421 * request->isci_host->scic_lock
426 * isci_task_execute_tmf() - This function builds and sends a task request,
427 * then waits for the completion.
428 * @isci_host: This parameter specifies the ISCI host object
429 * @tmf: This parameter is the pointer to the task management structure for
430 * this request.
431 * @timeout_ms: This parameter specifies the timeout period for the task
432 * management request.
434 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
435 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
437 int isci_task_execute_tmf(
438 struct isci_host *isci_host,
439 struct isci_tmf *tmf,
440 unsigned long timeout_ms)
442 DECLARE_COMPLETION_ONSTACK(completion);
443 enum sci_status status = SCI_FAILURE;
444 struct scic_sds_remote_device *sci_device;
445 struct isci_remote_device *isci_device = tmf->device;
446 struct isci_request *request;
447 int ret = TMF_RESP_FUNC_FAILED;
448 unsigned long flags;
450 /* sanity check, return TMF_RESP_FUNC_FAILED
451 * if the device is not there and ready.
453 if (!isci_device || isci_device->status != isci_ready_for_io) {
454 dev_dbg(&isci_host->pdev->dev,
455 "%s: isci_device = %p not ready (%d)\n",
456 __func__,
457 isci_device, isci_device->status);
458 return TMF_RESP_FUNC_FAILED;
459 } else
460 dev_dbg(&isci_host->pdev->dev,
461 "%s: isci_device = %p\n",
462 __func__, isci_device);
464 sci_device = to_sci_dev(isci_device);
466 /* Assign the pointer to the TMF's completion kernel wait structure. */
467 tmf->complete = &completion;
469 isci_task_request_build(
470 isci_host,
471 &request,
475 if (!request) {
476 dev_warn(&isci_host->pdev->dev,
477 "%s: isci_task_request_build failed\n",
478 __func__);
479 return TMF_RESP_FUNC_FAILED;
482 /* Allocate the TMF timeout timer. */
483 spin_lock_irqsave(&isci_host->scic_lock, flags);
484 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
486 /* Start the timer. */
487 if (tmf->timeout_timer)
488 isci_timer_start(tmf->timeout_timer, timeout_ms);
489 else
490 dev_warn(&isci_host->pdev->dev,
491 "%s: isci_timer_create failed!!!!\n",
492 __func__);
494 /* start the TMF io. */
495 status = scic_controller_start_task(
496 isci_host->core_controller,
497 sci_device,
498 request->sci_request_handle,
499 SCI_CONTROLLER_INVALID_IO_TAG
502 if (status != SCI_SUCCESS) {
503 dev_warn(&isci_host->pdev->dev,
504 "%s: start_io failed - status = 0x%x, request = %p\n",
505 __func__,
506 status,
507 request);
508 goto cleanup_request;
511 /* Call the users callback, if any. */
512 if (tmf->cb_state_func != NULL)
513 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
515 /* Change the state of the TMF-bearing request to "started". */
516 isci_request_change_state(request, started);
518 /* add the request to the remote device request list. */
519 list_add(&request->dev_node, &isci_device->reqs_in_process);
521 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
523 /* Wait for the TMF to complete, or a timeout. */
524 wait_for_completion(&completion);
526 isci_print_tmf(tmf);
528 if (tmf->status == SCI_SUCCESS)
529 ret = TMF_RESP_FUNC_COMPLETE;
530 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
531 dev_dbg(&isci_host->pdev->dev,
532 "%s: tmf.status == "
533 "SCI_FAILURE_IO_RESPONSE_VALID\n",
534 __func__);
535 ret = TMF_RESP_FUNC_COMPLETE;
537 /* Else - leave the default "failed" status alone. */
539 dev_dbg(&isci_host->pdev->dev,
540 "%s: completed request = %p\n",
541 __func__,
542 request);
544 if (request->io_request_completion != NULL) {
546 /* The fact that this is non-NULL for a TMF request
547 * means there is a thread waiting for this TMF to
548 * finish.
550 complete(request->io_request_completion);
553 spin_lock_irqsave(&isci_host->scic_lock, flags);
555 cleanup_request:
557 /* Clean up the timer if needed. */
558 if (tmf->timeout_timer) {
559 isci_del_timer(isci_host, tmf->timeout_timer);
560 tmf->timeout_timer = NULL;
563 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
565 isci_request_free(isci_host, request);
567 return ret;
570 void isci_task_build_tmf(
571 struct isci_tmf *tmf,
572 struct isci_remote_device *isci_device,
573 enum isci_tmf_function_codes code,
574 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
575 struct isci_tmf *,
576 void *),
577 void *cb_data)
579 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
580 "%s: isci_device = %p\n", __func__, isci_device);
582 memset(tmf, 0, sizeof(*tmf));
584 tmf->device = isci_device;
585 tmf->tmf_code = code;
586 tmf->timeout_timer = NULL;
587 tmf->cb_state_func = tmf_sent_cb;
588 tmf->cb_data = cb_data;
591 static void isci_task_build_abort_task_tmf(
592 struct isci_tmf *tmf,
593 struct isci_remote_device *isci_device,
594 enum isci_tmf_function_codes code,
595 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
596 struct isci_tmf *,
597 void *),
598 struct isci_request *old_request)
600 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
601 (void *)old_request);
602 tmf->io_tag = old_request->io_tag;
605 static struct isci_request *isci_task_get_request_from_task(
606 struct sas_task *task,
607 struct isci_host **isci_host,
608 struct isci_remote_device **isci_device)
611 struct isci_request *request = NULL;
612 unsigned long flags;
614 spin_lock_irqsave(&task->task_state_lock, flags);
616 request = task->lldd_task;
618 /* If task is already done, the request isn't valid */
619 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
620 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
621 (request != NULL)) {
623 if (isci_host != NULL)
624 *isci_host = request->isci_host;
626 if (isci_device != NULL)
627 *isci_device = request->isci_device;
630 spin_unlock_irqrestore(&task->task_state_lock, flags);
632 return request;
636 * isci_task_validate_request_to_abort() - This function checks the given I/O
637 * against the "started" state. If the request is still "started", it's
638 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
639 * BEFORE CALLING THIS FUNCTION.
640 * @isci_request: This parameter specifies the request object to control.
641 * @isci_host: This parameter specifies the ISCI host object
642 * @isci_device: This is the device to which the request is pending.
643 * @aborted_io_completion: This is a completion structure that will be added to
644 * the request in case it is changed to aborting; this completion is
645 * triggered when the request is fully completed.
647 * Either "started" on successful change of the task status to "aborted", or
648 * "unallocated" if the task cannot be controlled.
650 static enum isci_request_status isci_task_validate_request_to_abort(
651 struct isci_request *isci_request,
652 struct isci_host *isci_host,
653 struct isci_remote_device *isci_device,
654 struct completion *aborted_io_completion)
656 enum isci_request_status old_state = unallocated;
658 /* Only abort the task if it's in the
659 * device's request_in_process list
661 if (isci_request && !list_empty(&isci_request->dev_node)) {
662 old_state = isci_request_change_started_to_aborted(
663 isci_request, aborted_io_completion);
667 return old_state;
670 static void isci_request_cleanup_completed_loiterer(
671 struct isci_host *isci_host,
672 struct isci_remote_device *isci_device,
673 struct isci_request *isci_request)
675 struct sas_task *task;
676 unsigned long flags;
678 task = (isci_request->ttype == io_task)
679 ? isci_request_access_task(isci_request)
680 : NULL;
682 dev_dbg(&isci_host->pdev->dev,
683 "%s: isci_device=%p, request=%p, task=%p\n",
684 __func__, isci_device, isci_request, task);
686 spin_lock_irqsave(&isci_host->scic_lock, flags);
687 list_del_init(&isci_request->dev_node);
688 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
690 if (task != NULL) {
692 spin_lock_irqsave(&task->task_state_lock, flags);
693 task->lldd_task = NULL;
695 isci_set_task_doneflags(task);
697 /* If this task is not in the abort path, call task_done. */
698 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
700 spin_unlock_irqrestore(&task->task_state_lock, flags);
701 task->task_done(task);
702 } else
703 spin_unlock_irqrestore(&task->task_state_lock, flags);
705 isci_request_free(isci_host, isci_request);
709 * @isci_termination_timed_out(): this function will deal with a request for
710 * which the wait for termination has timed-out.
712 * @isci_host This SCU.
713 * @isci_request The I/O request being terminated.
715 static void
716 isci_termination_timed_out(
717 struct isci_host * host,
718 struct isci_request * request
721 unsigned long state_flags;
723 dev_warn(&host->pdev->dev,
724 "%s: host = %p; request = %p\n",
725 __func__, host, request);
727 /* At this point, the request to terminate
728 * has timed out. The best we can do is to
729 * have the request die a silent death
730 * if it ever completes.
732 spin_lock_irqsave(&request->state_lock, state_flags);
734 if (request->status == started) {
736 /* Set the request state to "dead",
737 * and clear the task pointer so that an actual
738 * completion event callback doesn't do
739 * anything.
741 request->status = dead;
743 /* Clear the timeout completion event pointer.*/
744 request->io_request_completion = NULL;
746 if (request->ttype == io_task) {
748 /* Break links with the sas_task. */
749 if (request->ttype_ptr.io_task_ptr != NULL) {
751 request->ttype_ptr.io_task_ptr->lldd_task = NULL;
752 request->ttype_ptr.io_task_ptr = NULL;
756 spin_unlock_irqrestore(&request->state_lock, state_flags);
761 * isci_terminate_request_core() - This function will terminate the given
762 * request, and wait for it to complete. This function must only be called
763 * from a thread that can wait. Note that the request is terminated and
764 * completed (back to the host, if started there).
765 * @isci_host: This SCU.
766 * @isci_device: The target.
767 * @isci_request: The I/O request to be terminated.
771 static void isci_terminate_request_core(
772 struct isci_host *isci_host,
773 struct isci_remote_device *isci_device,
774 struct isci_request *isci_request)
776 enum sci_status status = SCI_SUCCESS;
777 bool was_terminated = false;
778 bool needs_cleanup_handling = false;
779 enum isci_request_status request_status;
780 unsigned long flags;
781 unsigned long timeout_remaining;
784 dev_dbg(&isci_host->pdev->dev,
785 "%s: device = %p; request = %p\n",
786 __func__, isci_device, isci_request);
788 spin_lock_irqsave(&isci_host->scic_lock, flags);
790 /* Note that we are not going to control
791 * the target to abort the request.
793 isci_request->complete_in_target = true;
795 /* Make sure the request wasn't just sitting around signalling
796 * device condition (if the request handle is NULL, then the
797 * request completed but needed additional handling here).
799 if (isci_request->sci_request_handle != NULL) {
800 was_terminated = true;
801 needs_cleanup_handling = true;
802 status = scic_controller_terminate_request(
803 isci_host->core_controller,
804 to_sci_dev(isci_device),
805 isci_request->sci_request_handle
808 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
811 * The only time the request to terminate will
812 * fail is when the io request is completed and
813 * being aborted.
815 if (status != SCI_SUCCESS) {
816 dev_err(&isci_host->pdev->dev,
817 "%s: scic_controller_terminate_request"
818 " returned = 0x%x\n",
819 __func__,
820 status);
821 /* Clear the completion pointer from the request. */
822 isci_request->io_request_completion = NULL;
824 } else {
825 if (was_terminated) {
826 dev_dbg(&isci_host->pdev->dev,
827 "%s: before completion wait (%p)\n",
828 __func__,
829 isci_request->io_request_completion);
831 /* Wait here for the request to complete. */
832 #define TERMINATION_TIMEOUT_MSEC 50
833 timeout_remaining
834 = wait_for_completion_timeout(
835 isci_request->io_request_completion,
836 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
838 if (!timeout_remaining) {
840 isci_termination_timed_out(isci_host,
841 isci_request);
843 dev_err(&isci_host->pdev->dev,
844 "%s: *** Timeout waiting for "
845 "termination(%p/%p)\n",
846 __func__,
847 isci_request->io_request_completion,
848 isci_request);
850 } else
851 dev_dbg(&isci_host->pdev->dev,
852 "%s: after completion wait (%p)\n",
853 __func__,
854 isci_request->io_request_completion);
856 /* Clear the completion pointer from the request. */
857 isci_request->io_request_completion = NULL;
859 /* Peek at the status of the request. This will tell
860 * us if there was special handling on the request such that it
861 * needs to be detached and freed here.
863 spin_lock_irqsave(&isci_request->state_lock, flags);
864 request_status = isci_request_get_state(isci_request);
866 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
867 && ((request_status == aborted)
868 || (request_status == aborting)
869 || (request_status == terminating)
870 || (request_status == completed)
871 || (request_status == dead)
875 /* The completion routine won't free a request in
876 * the aborted/aborting/etc. states, so we do
877 * it here.
879 needs_cleanup_handling = true;
881 spin_unlock_irqrestore(&isci_request->state_lock, flags);
883 if (needs_cleanup_handling)
884 isci_request_cleanup_completed_loiterer(
885 isci_host, isci_device, isci_request
890 static void isci_terminate_request(
891 struct isci_host *isci_host,
892 struct isci_remote_device *isci_device,
893 struct isci_request *isci_request,
894 enum isci_request_status new_request_state)
896 enum isci_request_status old_state;
897 DECLARE_COMPLETION_ONSTACK(request_completion);
899 /* Change state to "new_request_state" if it is currently "started" */
900 old_state = isci_request_change_started_to_newstate(
901 isci_request,
902 &request_completion,
903 new_request_state
906 if ((old_state == started) ||
907 (old_state == completed) ||
908 (old_state == aborting)) {
910 /* If the old_state is started:
911 * This request was not already being aborted. If it had been,
912 * then the aborting I/O (ie. the TMF request) would not be in
913 * the aborting state, and thus would be terminated here. Note
914 * that since the TMF completion's call to the kernel function
915 * "complete()" does not happen until the pending I/O request
916 * terminate fully completes, we do not have to implement a
917 * special wait here for already aborting requests - the
918 * termination of the TMF request will force the request
919 * to finish it's already started terminate.
921 * If old_state == completed:
922 * This request completed from the SCU hardware perspective
923 * and now just needs cleaning up in terms of freeing the
924 * request and potentially calling up to libsas.
926 * If old_state == aborting:
927 * This request has already gone through a TMF timeout, but may
928 * not have been terminated; needs cleaning up at least.
930 isci_terminate_request_core(isci_host, isci_device,
931 isci_request);
936 * isci_terminate_pending_requests() - This function will change the all of the
937 * requests on the given device's state to "aborting", will terminate the
938 * requests, and wait for them to complete. This function must only be
939 * called from a thread that can wait. Note that the requests are all
940 * terminated and completed (back to the host, if started there).
941 * @isci_host: This parameter specifies SCU.
942 * @isci_device: This parameter specifies the target.
946 void isci_terminate_pending_requests(
947 struct isci_host *isci_host,
948 struct isci_remote_device *isci_device,
949 enum isci_request_status new_request_state)
951 struct isci_request *request;
952 struct isci_request *next_request;
953 unsigned long flags;
954 struct list_head aborted_request_list;
956 INIT_LIST_HEAD(&aborted_request_list);
958 dev_dbg(&isci_host->pdev->dev,
959 "%s: isci_device = %p (new request state = %d)\n",
960 __func__, isci_device, new_request_state);
962 spin_lock_irqsave(&isci_host->scic_lock, flags);
964 /* Move all of the pending requests off of the device list. */
965 list_splice_init(&isci_device->reqs_in_process,
966 &aborted_request_list);
968 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
970 /* Iterate through the now-local list. */
971 list_for_each_entry_safe(request, next_request,
972 &aborted_request_list, dev_node) {
974 dev_warn(&isci_host->pdev->dev,
975 "%s: isci_device=%p request=%p; task=%p\n",
976 __func__,
977 isci_device, request,
978 ((request->ttype == io_task)
979 ? isci_request_access_task(request)
980 : NULL));
982 /* Mark all still pending I/O with the selected next
983 * state, terminate and free it.
985 isci_terminate_request(isci_host, isci_device,
986 request, new_request_state
992 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
993 * Template functions.
994 * @lun: This parameter specifies the lun to be reset.
996 * status, zero indicates success.
998 static int isci_task_send_lu_reset_sas(
999 struct isci_host *isci_host,
1000 struct isci_remote_device *isci_device,
1001 u8 *lun)
1003 struct isci_tmf tmf;
1004 int ret = TMF_RESP_FUNC_FAILED;
1006 dev_dbg(&isci_host->pdev->dev,
1007 "%s: isci_host = %p, isci_device = %p\n",
1008 __func__, isci_host, isci_device);
1009 /* Send the LUN reset to the target. By the time the call returns,
1010 * the TMF has fully exected in the target (in which case the return
1011 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
1012 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
1014 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
1015 NULL);
1017 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
1018 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
1020 if (ret == TMF_RESP_FUNC_COMPLETE)
1021 dev_dbg(&isci_host->pdev->dev,
1022 "%s: %p: TMF_LU_RESET passed\n",
1023 __func__, isci_device);
1024 else
1025 dev_dbg(&isci_host->pdev->dev,
1026 "%s: %p: TMF_LU_RESET failed (%x)\n",
1027 __func__, isci_device, ret);
1029 return ret;
1033 * isci_task_lu_reset() - This function is one of the SAS Domain Template
1034 * functions. This is one of the Task Management functoins called by libsas,
1035 * to reset the given lun. Note the assumption that while this call is
1036 * executing, no I/O will be sent by the host to the device.
1037 * @lun: This parameter specifies the lun to be reset.
1039 * status, zero indicates success.
1041 int isci_task_lu_reset(
1042 struct domain_device *domain_device,
1043 u8 *lun)
1045 struct isci_host *isci_host = NULL;
1046 struct isci_remote_device *isci_device = NULL;
1047 int ret;
1048 bool device_stopping = false;
1050 if (domain_device == NULL) {
1051 pr_warn("%s: domain_device == NULL\n", __func__);
1052 return TMF_RESP_FUNC_FAILED;
1055 isci_device = isci_dev_from_domain_dev(domain_device);
1057 if (domain_device->port != NULL)
1058 isci_host = isci_host_from_sas_ha(domain_device->port->ha);
1060 pr_debug("%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
1061 __func__, domain_device, isci_host, isci_device);
1063 if (isci_device != NULL)
1064 device_stopping = (isci_device->status == isci_stopping)
1065 || (isci_device->status == isci_stopped);
1067 /* If there is a device reset pending on any request in the
1068 * device's list, fail this LUN reset request in order to
1069 * escalate to the device reset.
1071 if ((isci_device == NULL) ||
1072 (isci_host == NULL) ||
1073 ((isci_host != NULL) &&
1074 (isci_device != NULL) &&
1075 (device_stopping ||
1076 (isci_device_is_reset_pending(isci_host, isci_device))))) {
1077 dev_warn(&isci_host->pdev->dev,
1078 "%s: No dev (%p), no host (%p), or "
1079 "RESET PENDING: domain_device=%p\n",
1080 __func__, isci_device, isci_host, domain_device);
1081 return TMF_RESP_FUNC_FAILED;
1084 /* Send the task management part of the reset. */
1085 if (sas_protocol_ata(domain_device->tproto)) {
1086 ret = isci_task_send_lu_reset_sata(
1087 isci_host, isci_device, lun
1089 } else
1090 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
1092 /* If the LUN reset worked, all the I/O can now be terminated. */
1093 if (ret == TMF_RESP_FUNC_COMPLETE)
1094 /* Terminate all I/O now. */
1095 isci_terminate_pending_requests(isci_host,
1096 isci_device,
1097 terminating);
1099 return ret;
1103 /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1104 int isci_task_clear_nexus_port(struct asd_sas_port *port)
1106 return TMF_RESP_FUNC_FAILED;
1111 int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1113 return TMF_RESP_FUNC_FAILED;
1116 int isci_task_I_T_nexus_reset(struct domain_device *dev)
1118 return TMF_RESP_FUNC_FAILED;
1122 /* Task Management Functions. Must be called from process context. */
1125 * isci_abort_task_process_cb() - This is a helper function for the abort task
1126 * TMF command. It manages the request state with respect to the successful
1127 * transmission / completion of the abort task request.
1128 * @cb_state: This parameter specifies when this function was called - after
1129 * the TMF request has been started and after it has timed-out.
1130 * @tmf: This parameter specifies the TMF in progress.
1134 static void isci_abort_task_process_cb(
1135 enum isci_tmf_cb_state cb_state,
1136 struct isci_tmf *tmf,
1137 void *cb_data)
1139 struct isci_request *old_request;
1141 old_request = (struct isci_request *)cb_data;
1143 dev_dbg(&old_request->isci_host->pdev->dev,
1144 "%s: tmf=%p, old_request=%p\n",
1145 __func__, tmf, old_request);
1147 switch (cb_state) {
1149 case isci_tmf_started:
1150 /* The TMF has been started. Nothing to do here, since the
1151 * request state was already set to "aborted" by the abort
1152 * task function.
1154 BUG_ON((old_request->status != aborted)
1155 && (old_request->status != completed));
1156 break;
1158 case isci_tmf_timed_out:
1160 /* Set the task's state to "aborting", since the abort task
1161 * function thread set it to "aborted" (above) in anticipation
1162 * of the task management request working correctly. Since the
1163 * timeout has now fired, the TMF request failed. We set the
1164 * state such that the request completion will indicate the
1165 * device is no longer present.
1167 isci_request_change_state(old_request, aborting);
1168 break;
1170 default:
1171 dev_err(&old_request->isci_host->pdev->dev,
1172 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1173 __func__, cb_state, tmf, old_request);
1174 break;
1179 * isci_task_abort_task() - This function is one of the SAS Domain Template
1180 * functions. This function is called by libsas to abort a specified task.
1181 * @task: This parameter specifies the SAS task to abort.
1183 * status, zero indicates success.
1185 int isci_task_abort_task(struct sas_task *task)
1187 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
1188 struct isci_request *old_request = NULL;
1189 enum isci_request_status old_state;
1190 struct isci_remote_device *isci_device = NULL;
1191 struct isci_host *isci_host = NULL;
1192 struct isci_tmf tmf;
1193 int ret = TMF_RESP_FUNC_FAILED;
1194 unsigned long flags;
1195 bool any_dev_reset = false;
1196 bool device_stopping;
1198 /* Get the isci_request reference from the task. Note that
1199 * this check does not depend on the pending request list
1200 * in the device, because tasks driving resets may land here
1201 * after completion in the core.
1203 old_request = isci_task_get_request_from_task(task, &isci_host,
1204 &isci_device);
1206 dev_dbg(&isci_host->pdev->dev,
1207 "%s: task = %p\n", __func__, task);
1209 /* Check if the device has been / is currently being removed.
1210 * If so, no task management will be done, and the I/O will
1211 * be terminated.
1213 device_stopping = (isci_device->status == isci_stopping)
1214 || (isci_device->status == isci_stopped);
1216 /* This version of the driver will fail abort requests for
1217 * SATA/STP. Failing the abort request this way will cause the
1218 * SCSI error handler thread to escalate to LUN reset
1220 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1221 dev_warn(&isci_host->pdev->dev,
1222 " task %p is for a STP/SATA device;"
1223 " returning TMF_RESP_FUNC_FAILED\n"
1224 " to cause a LUN reset...\n", task);
1225 return TMF_RESP_FUNC_FAILED;
1228 dev_dbg(&isci_host->pdev->dev,
1229 "%s: old_request == %p\n", __func__, old_request);
1231 if (!device_stopping)
1232 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1234 spin_lock_irqsave(&task->task_state_lock, flags);
1236 /* Don't do resets to stopping devices. */
1237 if (device_stopping) {
1239 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1240 any_dev_reset = false;
1242 } else /* See if there is a pending device reset for this device. */
1243 any_dev_reset = any_dev_reset
1244 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
1246 /* If the extraction of the request reference from the task
1247 * failed, then the request has been completed (or if there is a
1248 * pending reset then this abort request function must be failed
1249 * in order to escalate to the target reset).
1251 if ((old_request == NULL) || any_dev_reset) {
1253 /* If the device reset task flag is set, fail the task
1254 * management request. Otherwise, the original request
1255 * has completed.
1257 if (any_dev_reset) {
1259 /* Turn off the task's DONE to make sure this
1260 * task is escalated to a target reset.
1262 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1264 /* Make the reset happen as soon as possible. */
1265 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1267 spin_unlock_irqrestore(&task->task_state_lock, flags);
1269 /* Fail the task management request in order to
1270 * escalate to the target reset.
1272 ret = TMF_RESP_FUNC_FAILED;
1274 dev_dbg(&isci_host->pdev->dev,
1275 "%s: Failing task abort in order to "
1276 "escalate to target reset because\n"
1277 "SAS_TASK_NEED_DEV_RESET is set for "
1278 "task %p on dev %p\n",
1279 __func__, task, isci_device);
1282 } else {
1283 /* The request has already completed and there
1284 * is nothing to do here other than to set the task
1285 * done bit, and indicate that the task abort function
1286 * was sucessful.
1288 isci_set_task_doneflags(task);
1290 spin_unlock_irqrestore(&task->task_state_lock, flags);
1292 ret = TMF_RESP_FUNC_COMPLETE;
1294 dev_dbg(&isci_host->pdev->dev,
1295 "%s: abort task not needed for %p\n",
1296 __func__, task);
1299 return ret;
1301 else
1302 spin_unlock_irqrestore(&task->task_state_lock, flags);
1304 spin_lock_irqsave(&isci_host->scic_lock, flags);
1306 /* Check the request status and change to "aborted" if currently
1307 * "starting"; if true then set the I/O kernel completion
1308 * struct that will be triggered when the request completes.
1310 old_state = isci_task_validate_request_to_abort(
1311 old_request, isci_host, isci_device,
1312 &aborted_io_completion);
1313 if ((old_state != started) &&
1314 (old_state != completed) &&
1315 (old_state != aborting)) {
1317 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1319 /* The request was already being handled by someone else (because
1320 * they got to set the state away from started).
1322 dev_dbg(&isci_host->pdev->dev,
1323 "%s: device = %p; old_request %p already being aborted\n",
1324 __func__,
1325 isci_device, old_request);
1327 return TMF_RESP_FUNC_COMPLETE;
1329 if ((task->task_proto == SAS_PROTOCOL_SMP)
1330 || device_stopping
1331 || old_request->complete_in_target
1334 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1336 dev_dbg(&isci_host->pdev->dev,
1337 "%s: SMP request (%d)"
1338 " or device is stopping (%d)"
1339 " or complete_in_target (%d), thus no TMF\n",
1340 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1341 device_stopping, old_request->complete_in_target);
1343 /* Set the state on the task. */
1344 isci_task_all_done(task);
1346 ret = TMF_RESP_FUNC_COMPLETE;
1348 /* Stopping and SMP devices are not sent a TMF, and are not
1349 * reset, but the outstanding I/O request is terminated below.
1351 } else {
1352 /* Fill in the tmf stucture */
1353 isci_task_build_abort_task_tmf(&tmf, isci_device,
1354 isci_tmf_ssp_task_abort,
1355 isci_abort_task_process_cb,
1356 old_request);
1358 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1360 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1361 ret = isci_task_execute_tmf(isci_host, &tmf,
1362 ISCI_ABORT_TASK_TIMEOUT_MS);
1364 if (ret != TMF_RESP_FUNC_COMPLETE)
1365 dev_err(&isci_host->pdev->dev,
1366 "%s: isci_task_send_tmf failed\n",
1367 __func__);
1369 if (ret == TMF_RESP_FUNC_COMPLETE) {
1370 old_request->complete_in_target = true;
1372 /* Clean up the request on our side, and wait for the aborted I/O to
1373 * complete.
1375 isci_terminate_request_core(isci_host, isci_device, old_request);
1378 /* Make sure we do not leave a reference to aborted_io_completion */
1379 old_request->io_request_completion = NULL;
1380 return ret;
1384 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1385 * functions. This is one of the Task Management functoins called by libsas,
1386 * to abort all task for the given lun.
1387 * @d_device: This parameter specifies the domain device associated with this
1388 * request.
1389 * @lun: This parameter specifies the lun associated with this request.
1391 * status, zero indicates success.
1393 int isci_task_abort_task_set(
1394 struct domain_device *d_device,
1395 u8 *lun)
1397 return TMF_RESP_FUNC_FAILED;
1402 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1403 * functions. This is one of the Task Management functoins called by libsas.
1404 * @d_device: This parameter specifies the domain device associated with this
1405 * request.
1406 * @lun: This parameter specifies the lun associated with this request.
1408 * status, zero indicates success.
1410 int isci_task_clear_aca(
1411 struct domain_device *d_device,
1412 u8 *lun)
1414 return TMF_RESP_FUNC_FAILED;
1420 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1421 * functions. This is one of the Task Management functoins called by libsas.
1422 * @d_device: This parameter specifies the domain device associated with this
1423 * request.
1424 * @lun: This parameter specifies the lun associated with this request.
1426 * status, zero indicates success.
1428 int isci_task_clear_task_set(
1429 struct domain_device *d_device,
1430 u8 *lun)
1432 return TMF_RESP_FUNC_FAILED;
1437 * isci_task_query_task() - This function is implemented to cause libsas to
1438 * correctly escalate the failed abort to a LUN or target reset (this is
1439 * because sas_scsi_find_task libsas function does not correctly interpret
1440 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1441 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1442 * returned, libsas will turn this into a target reset
1443 * @task: This parameter specifies the sas task being queried.
1444 * @lun: This parameter specifies the lun associated with this request.
1446 * status, zero indicates success.
1448 int isci_task_query_task(
1449 struct sas_task *task)
1451 /* See if there is a pending device reset for this device. */
1452 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1453 return TMF_RESP_FUNC_FAILED;
1454 else
1455 return TMF_RESP_FUNC_SUCC;
1459 * isci_task_request_complete() - This function is called by the sci core when
1460 * an task request completes.
1461 * @isci_host: This parameter specifies the ISCI host object
1462 * @request: This parameter is the completed isci_request object.
1463 * @completion_status: This parameter specifies the completion status from the
1464 * sci core.
1466 * none.
1468 void isci_task_request_complete(
1469 struct isci_host *isci_host,
1470 struct isci_request *request,
1471 enum sci_task_status completion_status)
1473 struct isci_remote_device *isci_device = request->isci_device;
1474 enum isci_request_status old_state;
1475 struct isci_tmf *tmf = isci_request_access_tmf(request);
1476 struct completion *tmf_complete;
1478 dev_dbg(&isci_host->pdev->dev,
1479 "%s: request = %p, status=%d\n",
1480 __func__, request, completion_status);
1482 old_state = isci_request_change_state(request, completed);
1484 tmf->status = completion_status;
1485 request->complete_in_target = true;
1487 if (SAS_PROTOCOL_SSP == tmf->proto) {
1489 memcpy(&tmf->resp.resp_iu,
1490 scic_io_request_get_response_iu_address(
1491 request->sci_request_handle
1493 sizeof(struct sci_ssp_response_iu));
1495 } else if (SAS_PROTOCOL_SATA == tmf->proto) {
1497 memcpy(&tmf->resp.d2h_fis,
1498 scic_stp_io_request_get_d2h_reg_address(
1499 request->sci_request_handle
1501 sizeof(struct sata_fis_reg_d2h)
1505 /* Manage the timer if it is still running. */
1506 if (tmf->timeout_timer) {
1507 isci_del_timer(isci_host, tmf->timeout_timer);
1508 tmf->timeout_timer = NULL;
1511 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1512 tmf_complete = tmf->complete;
1514 scic_controller_complete_task(
1515 isci_host->core_controller,
1516 to_sci_dev(isci_device),
1517 request->sci_request_handle
1519 /* NULL the request handle to make sure it cannot be terminated
1520 * or completed again.
1522 request->sci_request_handle = NULL;
1524 isci_request_change_state(request, unallocated);
1525 list_del_init(&request->dev_node);
1527 /* The task management part completes last. */
1528 complete(tmf_complete);
1533 * isci_task_ssp_request_get_lun() - This function is called by the sci core to
1534 * retrieve the lun for a given task request.
1535 * @request: This parameter is the isci_request object.
1537 * lun for specified task request.
1541 * isci_task_ssp_request_get_function() - This function is called by the sci
1542 * core to retrieve the function for a given task request.
1543 * @request: This parameter is the isci_request object.
1545 * function code for specified task request.
1547 u8 isci_task_ssp_request_get_function(struct isci_request *request)
1549 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1551 dev_dbg(&request->isci_host->pdev->dev,
1552 "%s: func = %d\n", __func__, isci_tmf->tmf_code);
1554 return isci_tmf->tmf_code;
1558 * isci_task_ssp_request_get_io_tag_to_manage() - This function is called by
1559 * the sci core to retrieve the io tag for a given task request.
1560 * @request: This parameter is the isci_request object.
1562 * io tag for specified task request.
1564 u16 isci_task_ssp_request_get_io_tag_to_manage(struct isci_request *request)
1566 u16 io_tag = SCI_CONTROLLER_INVALID_IO_TAG;
1568 if (tmf_task == request->ttype) {
1569 struct isci_tmf *tmf = isci_request_access_tmf(request);
1570 io_tag = tmf->io_tag;
1573 dev_dbg(&request->isci_host->pdev->dev,
1574 "%s: request = %p, io_tag = %d\n",
1575 __func__, request, io_tag);
1577 return io_tag;
1581 * isci_task_ssp_request_get_response_data_address() - This function is called
1582 * by the sci core to retrieve the response data address for a given task
1583 * request.
1584 * @request: This parameter is the isci_request object.
1586 * response data address for specified task request.
1588 void *isci_task_ssp_request_get_response_data_address(
1589 struct isci_request *request)
1591 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1593 return &isci_tmf->resp.resp_iu;
1597 * isci_task_ssp_request_get_response_data_length() - This function is called
1598 * by the sci core to retrieve the response data length for a given task
1599 * request.
1600 * @request: This parameter is the isci_request object.
1602 * response data length for specified task request.
1604 u32 isci_task_ssp_request_get_response_data_length(
1605 struct isci_request *request)
1607 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1609 return sizeof(isci_tmf->resp.resp_iu);
1613 * isci_bus_reset_handler() - This function performs a target reset of the
1614 * device referenced by "cmd'. This function is exported through the
1615 * "struct scsi_host_template" structure such that it is called when an I/O
1616 * recovery process has escalated to a target reset. Note that this function
1617 * is called from the scsi error handler event thread, so may block on calls.
1618 * @scsi_cmd: This parameter specifies the target to be reset.
1620 * SUCCESS if the reset process was successful, else FAILED.
1622 int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1624 unsigned long flags = 0;
1625 struct isci_host *isci_host = NULL;
1626 enum sci_status status;
1627 int base_status;
1628 struct isci_remote_device *isci_dev
1629 = isci_dev_from_domain_dev(
1630 sdev_to_domain_dev(cmd->device));
1632 dev_dbg(&cmd->device->sdev_gendev,
1633 "%s: cmd %p, isci_dev %p\n",
1634 __func__, cmd, isci_dev);
1636 if (!isci_dev) {
1637 dev_warn(&cmd->device->sdev_gendev,
1638 "%s: isci_dev is GONE!\n",
1639 __func__);
1641 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1644 if (isci_dev->isci_port != NULL)
1645 isci_host = isci_dev->isci_port->isci_host;
1647 if (isci_host != NULL)
1648 spin_lock_irqsave(&isci_host->scic_lock, flags);
1650 status = scic_remote_device_reset(to_sci_dev(isci_dev));
1651 if (status != SCI_SUCCESS) {
1653 if (isci_host != NULL)
1654 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1656 scmd_printk(KERN_WARNING, cmd,
1657 "%s: scic_remote_device_reset(%p) returned %d!\n",
1658 __func__, isci_dev, status);
1660 return TMF_RESP_FUNC_FAILED;
1662 if (isci_host != NULL)
1663 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1665 /* Make sure all pending requests are able to be fully terminated. */
1666 isci_device_clear_reset_pending(isci_dev);
1668 /* Terminate in-progress I/O now. */
1669 isci_remote_device_nuke_requests(isci_dev);
1671 /* Call into the libsas default handler (which calls sas_phy_reset). */
1672 base_status = sas_eh_bus_reset_handler(cmd);
1674 if (base_status != SUCCESS) {
1676 /* There can be cases where the resets to individual devices
1677 * behind an expander will fail because of an unplug of the
1678 * expander itself.
1680 scmd_printk(KERN_WARNING, cmd,
1681 "%s: sas_eh_bus_reset_handler(%p) returned %d!\n",
1682 __func__, cmd, base_status);
1685 /* WHAT TO DO HERE IF sas_phy_reset FAILS? */
1687 if (isci_host != NULL)
1688 spin_lock_irqsave(&isci_host->scic_lock, flags);
1689 status = scic_remote_device_reset_complete(to_sci_dev(isci_dev));
1691 if (isci_host != NULL)
1692 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1694 if (status != SCI_SUCCESS) {
1695 scmd_printk(KERN_WARNING, cmd,
1696 "%s: scic_remote_device_reset_complete(%p) "
1697 "returned %d!\n",
1698 __func__, isci_dev, status);
1700 /* WHAT TO DO HERE IF scic_remote_device_reset_complete FAILS? */
1702 dev_dbg(&cmd->device->sdev_gendev,
1703 "%s: cmd %p, isci_dev %p complete.\n",
1704 __func__, cmd, isci_dev);
1706 return TMF_RESP_FUNC_COMPLETE;