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 "scic_task_request.h"
58 #include "scic_remote_device.h"
59 #include "scic_io_request.h"
60 #include "scic_sds_remote_device.h"
61 #include "scic_sds_remote_node_context.h"
69 * isci_task_execute_task() - This function is one of the SAS Domain Template
70 * functions. This function is called by libsas to send a task down to
72 * @task: This parameter specifies the SAS task to send.
73 * @num: This parameter specifies the number of tasks to queue.
74 * @gfp_flags: This parameter specifies the context of this call.
76 * status, zero indicates success.
78 int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
80 struct isci_host *isci_host;
81 struct isci_request *request = NULL;
82 struct isci_remote_device *device;
85 enum sci_status status;
88 dev_dbg(task->dev->port->ha->dev, "%s: num=%d\n", __func__, num);
90 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
92 isci_task_complete_for_upper_layer(
95 SAM_STAT_TASK_ABORTED,
96 isci_perform_normal_io_completion
99 return 0; /* The I/O was accepted (and failed). */
101 if ((task->dev == NULL) || (task->dev->port == NULL)) {
103 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi midlayer
104 * removes the target.
106 isci_task_complete_for_upper_layer(
108 SAS_TASK_UNDELIVERED,
110 isci_perform_normal_io_completion
112 return 0; /* The I/O was accepted (and failed). */
114 isci_host = isci_host_from_sas_ha(task->dev->port->ha);
116 /* Check if we have room for more tasks */
117 ret = isci_host_can_queue(isci_host, num);
120 dev_warn(task->dev->port->ha->dev, "%s: queue full\n", __func__);
125 dev_dbg(task->dev->port->ha->dev,
126 "task = %p, num = %d; dev = %p; cmd = %p\n",
127 task, num, task->dev, task->uldd_task);
129 if ((task->dev == NULL) || (task->dev->port == NULL)) {
130 dev_warn(task->dev->port->ha->dev,
131 "%s: task %p's port or dev == NULL!\n",
134 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi
135 * midlayer removes the target.
137 isci_task_complete_for_upper_layer(
139 SAS_TASK_UNDELIVERED,
141 isci_perform_normal_io_completion
143 /* We don't have a valid host reference, so we
144 * can't control the host queueing condition.
149 device = isci_dev_from_domain_dev(task->dev);
151 isci_host = isci_host_from_sas_ha(task->dev->port->ha);
153 if (device && device->status == isci_ready) {
155 /* Forces a retry from scsi mid layer. */
156 dev_warn(task->dev->port->ha->dev,
157 "%s: task %p: isci_host->status = %d, "
161 isci_host_get_state(isci_host),
165 dev_dbg(task->dev->port->ha->dev,
166 "%s: device->status = 0x%x\n",
167 __func__, device->status);
169 /* Indicate QUEUE_FULL so that the scsi midlayer
172 isci_task_complete_for_upper_layer(
176 isci_perform_normal_io_completion
178 isci_host_can_dequeue(isci_host, 1);
180 /* the device is going down... */
181 else if (!device || device->status != isci_ready_for_io) {
183 dev_dbg(task->dev->port->ha->dev,
184 "%s: task %p: isci_host->status = %d, "
188 isci_host_get_state(isci_host),
192 dev_dbg(task->dev->port->ha->dev,
193 "%s: device->status = 0x%x\n",
194 __func__, device->status);
196 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi
197 * midlayer removes the target.
199 isci_task_complete_for_upper_layer(
201 SAS_TASK_UNDELIVERED,
203 isci_perform_normal_io_completion
205 isci_host_can_dequeue(isci_host, 1);
208 /* build and send the request. */
209 status = isci_request_execute(isci_host, task, &request,
212 if (status == SCI_SUCCESS) {
213 spin_lock_irqsave(&task->task_state_lock, flags);
214 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
215 spin_unlock_irqrestore(&task->task_state_lock, flags);
217 /* Indicate QUEUE_FULL so that the scsi
218 * midlayer retries. if the request
219 * failed for remote device reasons,
220 * it gets returned as
221 * SAS_TASK_UNDELIVERED next time
224 isci_task_complete_for_upper_layer(
228 isci_perform_normal_io_completion
230 isci_host_can_dequeue(isci_host, 1);
233 task = list_entry(task->list.next, struct sas_task, list);
241 * isci_task_request_build() - This function builds the task request object.
242 * @isci_host: This parameter specifies the ISCI host object
243 * @request: This parameter points to the isci_request object allocated in the
244 * request construct function.
245 * @tmf: This parameter is the task management struct to be built
247 * SCI_SUCCESS on successfull completion, or specific failure code.
249 static enum sci_status isci_task_request_build(
250 struct isci_host *isci_host,
251 struct isci_request **isci_request,
252 struct isci_tmf *isci_tmf)
254 struct scic_sds_remote_device *sci_device;
255 enum sci_status status = SCI_FAILURE;
256 struct isci_request *request;
257 struct isci_remote_device *isci_device;
258 /* struct sci_sas_identify_address_frame_protocols dev_protocols; */
259 struct smp_discover_response_protocols dev_protocols;
262 dev_dbg(&isci_host->pdev->dev,
263 "%s: isci_tmf = %p\n", __func__, isci_tmf);
265 isci_device = isci_tmf->device;
266 sci_device = to_sci_dev(isci_device);
268 /* do common allocation and init of request object. */
269 status = isci_request_alloc_tmf(
277 if (status != SCI_SUCCESS)
280 /* let the core do it's construct. */
281 status = scic_task_request_construct(
282 isci_host->core_controller,
284 SCI_CONTROLLER_INVALID_IO_TAG,
286 request->sci_request_mem_ptr,
287 &request->sci_request_handle
290 if (status != SCI_SUCCESS) {
291 dev_warn(&isci_host->pdev->dev,
292 "%s: scic_task_request_construct failed - "
299 sci_object_set_association(
300 request->sci_request_handle,
304 scic_remote_device_get_protocols(
309 /* let the core do it's protocol
310 * specific construction.
312 if (dev_protocols.u.bits.attached_ssp_target) {
314 isci_tmf->proto = SAS_PROTOCOL_SSP;
315 status = scic_task_request_construct_ssp(
316 request->sci_request_handle
318 if (status != SCI_SUCCESS)
322 if (dev_protocols.u.bits.attached_stp_target) {
324 isci_tmf->proto = SAS_PROTOCOL_SATA;
325 status = isci_sata_management_task_request_build(request);
327 if (status != SCI_SUCCESS)
335 /* release the dma memory if we fail. */
336 isci_request_free(isci_host, request);
340 *isci_request = request;
345 * isci_tmf_timeout_cb() - This function is called as a kernel callback when
346 * the timeout period for the TMF has expired.
350 static void isci_tmf_timeout_cb(void *tmf_request_arg)
352 struct isci_request *request = (struct isci_request *)tmf_request_arg;
353 struct isci_tmf *tmf = isci_request_access_tmf(request);
354 enum sci_status status;
356 BUG_ON(request->ttype != tmf_task);
358 /* This task management request has timed-out. Terminate the request
359 * so that the request eventually completes to the requestor in the
360 * request completion callback path.
362 /* Note - the timer callback function itself has provided spinlock
363 * exclusion from the start and completion paths. No need to take
364 * the request->isci_host->scic_lock here.
367 if (tmf->timeout_timer != NULL) {
368 /* Call the users callback, if any. */
369 if (tmf->cb_state_func != NULL)
370 tmf->cb_state_func(isci_tmf_timed_out, tmf,
373 /* Terminate the TMF transmit request. */
374 status = scic_controller_terminate_request(
375 request->isci_host->core_controller,
376 to_sci_dev(request->isci_device),
377 request->sci_request_handle
380 dev_dbg(&request->isci_host->pdev->dev,
381 "%s: tmf_request = %p; tmf = %p; status = %d\n",
382 __func__, request, tmf, status);
384 dev_dbg(&request->isci_host->pdev->dev,
385 "%s: timer already canceled! "
386 "tmf_request = %p; tmf = %p\n",
387 __func__, request, tmf);
389 /* No need to unlock since the caller to this callback is doing it for
391 * request->isci_host->scic_lock
396 * isci_task_execute_tmf() - This function builds and sends a task request,
397 * then waits for the completion.
398 * @isci_host: This parameter specifies the ISCI host object
399 * @tmf: This parameter is the pointer to the task management structure for
401 * @timeout_ms: This parameter specifies the timeout period for the task
402 * management request.
404 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
405 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
407 int isci_task_execute_tmf(
408 struct isci_host *isci_host,
409 struct isci_tmf *tmf,
410 unsigned long timeout_ms)
412 DECLARE_COMPLETION_ONSTACK(completion);
413 enum sci_status status = SCI_FAILURE;
414 struct scic_sds_remote_device *sci_device;
415 struct isci_remote_device *isci_device = tmf->device;
416 struct isci_request *request;
417 int ret = TMF_RESP_FUNC_FAILED;
420 /* sanity check, return TMF_RESP_FUNC_FAILED
421 * if the device is not there and ready.
423 if (!isci_device || isci_device->status != isci_ready_for_io) {
424 dev_dbg(&isci_host->pdev->dev,
425 "%s: isci_device = %p not ready (%d)\n",
427 isci_device, isci_device->status);
428 return TMF_RESP_FUNC_FAILED;
430 dev_dbg(&isci_host->pdev->dev,
431 "%s: isci_device = %p\n",
432 __func__, isci_device);
434 sci_device = to_sci_dev(isci_device);
436 /* Assign the pointer to the TMF's completion kernel wait structure. */
437 tmf->complete = &completion;
439 isci_task_request_build(
446 dev_warn(&isci_host->pdev->dev,
447 "%s: isci_task_request_build failed\n",
449 return TMF_RESP_FUNC_FAILED;
452 /* Allocate the TMF timeout timer. */
453 spin_lock_irqsave(&isci_host->scic_lock, flags);
454 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
456 /* Start the timer. */
457 if (tmf->timeout_timer)
458 isci_timer_start(tmf->timeout_timer, timeout_ms);
460 dev_warn(&isci_host->pdev->dev,
461 "%s: isci_timer_create failed!!!!\n",
464 /* start the TMF io. */
465 status = scic_controller_start_task(
466 isci_host->core_controller,
468 request->sci_request_handle,
469 SCI_CONTROLLER_INVALID_IO_TAG
472 if (status != SCI_SUCCESS) {
473 dev_warn(&isci_host->pdev->dev,
474 "%s: start_io failed - status = 0x%x, request = %p\n",
478 goto cleanup_request;
481 /* Call the users callback, if any. */
482 if (tmf->cb_state_func != NULL)
483 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
485 /* Change the state of the TMF-bearing request to "started". */
486 isci_request_change_state(request, started);
488 /* add the request to the remote device request list. */
489 list_add(&request->dev_node, &isci_device->reqs_in_process);
491 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
493 /* Wait for the TMF to complete, or a timeout. */
494 wait_for_completion(&completion);
498 if (tmf->status == SCI_SUCCESS)
499 ret = TMF_RESP_FUNC_COMPLETE;
500 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
501 dev_dbg(&isci_host->pdev->dev,
503 "SCI_FAILURE_IO_RESPONSE_VALID\n",
505 ret = TMF_RESP_FUNC_COMPLETE;
507 /* Else - leave the default "failed" status alone. */
509 dev_dbg(&isci_host->pdev->dev,
510 "%s: completed request = %p\n",
514 if (request->io_request_completion != NULL) {
516 /* The fact that this is non-NULL for a TMF request
517 * means there is a thread waiting for this TMF to
520 complete(request->io_request_completion);
523 spin_lock_irqsave(&isci_host->scic_lock, flags);
527 /* Clean up the timer if needed. */
528 if (tmf->timeout_timer) {
529 isci_del_timer(isci_host, tmf->timeout_timer);
530 tmf->timeout_timer = NULL;
533 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
535 isci_request_free(isci_host, request);
540 void isci_task_build_tmf(
541 struct isci_tmf *tmf,
542 struct isci_remote_device *isci_device,
543 enum isci_tmf_function_codes code,
544 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
549 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
550 "%s: isci_device = %p\n", __func__, isci_device);
552 memset(tmf, 0, sizeof(*tmf));
554 tmf->device = isci_device;
555 tmf->tmf_code = code;
556 tmf->timeout_timer = NULL;
557 tmf->cb_state_func = tmf_sent_cb;
558 tmf->cb_data = cb_data;
561 static struct isci_request *isci_task_get_request_from_task(
562 struct sas_task *task,
563 struct isci_host **isci_host,
564 struct isci_remote_device **isci_device)
567 struct isci_request *request = NULL;
570 spin_lock_irqsave(&task->task_state_lock, flags);
572 request = task->lldd_task;
574 /* If task is already done, the request isn't valid */
575 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
576 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
579 if (isci_host != NULL)
580 *isci_host = request->isci_host;
582 if (isci_device != NULL)
583 *isci_device = request->isci_device;
586 spin_unlock_irqrestore(&task->task_state_lock, flags);
592 * isci_task_validate_request_to_abort() - This function checks the given I/O
593 * against the "started" state. If the request is still "started", it's
594 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
595 * BEFORE CALLING THIS FUNCTION.
596 * @isci_request: This parameter specifies the request object to control.
597 * @isci_host: This parameter specifies the ISCI host object
598 * @isci_device: This is the device to which the request is pending.
599 * @aborted_io_completion: This is a completion structure that will be added to
600 * the request in case it is changed to aborting; this completion is
601 * triggered when the request is fully completed.
603 * Either "started" on successful change of the task status to "aborted", or
604 * "unallocated" if the task cannot be controlled.
606 static enum isci_request_status isci_task_validate_request_to_abort(
607 struct isci_request *isci_request,
608 struct isci_host *isci_host,
609 struct isci_remote_device *isci_device,
610 struct completion *aborted_io_completion)
612 enum isci_request_status old_state = unallocated;
614 /* Only abort the task if it's in the
615 * device's request_in_process list
617 if (isci_request && !list_empty(&isci_request->dev_node)) {
618 old_state = isci_request_change_started_to_aborted(
619 isci_request, aborted_io_completion);
626 static void isci_request_cleanup_completed_loiterer(
627 struct isci_host *isci_host,
628 struct isci_remote_device *isci_device,
629 struct isci_request *isci_request)
631 struct sas_task *task;
634 task = (isci_request->ttype == io_task)
635 ? isci_request_access_task(isci_request)
638 dev_dbg(&isci_host->pdev->dev,
639 "%s: isci_device=%p, request=%p, task=%p\n",
640 __func__, isci_device, isci_request, task);
642 spin_lock_irqsave(&isci_host->scic_lock, flags);
643 list_del_init(&isci_request->dev_node);
644 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
648 spin_lock_irqsave(&task->task_state_lock, flags);
649 task->lldd_task = NULL;
651 isci_set_task_doneflags(task);
653 /* If this task is not in the abort path, call task_done. */
654 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
656 spin_unlock_irqrestore(&task->task_state_lock, flags);
657 task->task_done(task);
659 spin_unlock_irqrestore(&task->task_state_lock, flags);
661 isci_request_free(isci_host, isci_request);
664 * isci_terminate_request_core() - This function will terminate the given
665 * request, and wait for it to complete. This function must only be called
666 * from a thread that can wait. Note that the request is terminated and
667 * completed (back to the host, if started there).
668 * @isci_host: This SCU.
669 * @isci_device: The target.
670 * @isci_request: The I/O request to be terminated.
674 static void isci_terminate_request_core(
675 struct isci_host *isci_host,
676 struct isci_remote_device *isci_device,
677 struct isci_request *isci_request,
678 struct completion *request_completion)
680 enum sci_status status = SCI_SUCCESS;
681 bool was_terminated = false;
682 bool needs_cleanup_handling = false;
683 enum isci_request_status request_status;
686 dev_dbg(&isci_host->pdev->dev,
687 "%s: device = %p; request = %p\n",
688 __func__, isci_device, isci_request);
690 /* Peek at the current status of the request. This will tell
691 * us if there was special handling on the request such that it
692 * needs to be detached and freed here.
694 spin_lock_irqsave(&isci_request->state_lock, flags);
695 request_status = isci_request_get_state(isci_request);
697 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
698 && ((request_status == aborted)
699 || (request_status == aborting)
700 || (request_status == terminating)
701 || (request_status == completed)
705 /* The completion routine won't free a request in
706 * the aborted/aborting/terminating state, so we do
709 needs_cleanup_handling = true;
711 spin_unlock_irqrestore(&isci_request->state_lock, flags);
713 spin_lock_irqsave(&isci_host->scic_lock, flags);
714 /* Make sure the request wasn't just sitting around signalling
715 * device condition (if the request handle is NULL, then the
716 * request completed but needed additional handling here).
718 if (isci_request->sci_request_handle != NULL) {
719 was_terminated = true;
720 status = scic_controller_terminate_request(
721 isci_host->core_controller,
722 to_sci_dev(isci_device),
723 isci_request->sci_request_handle
726 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
729 * The only time the request to terminate will
730 * fail is when the io request is completed and
733 if (status != SCI_SUCCESS)
734 dev_err(&isci_host->pdev->dev,
735 "%s: scic_controller_terminate_request"
736 " returned = 0x%x\n",
740 if (was_terminated) {
741 dev_dbg(&isci_host->pdev->dev,
742 "%s: before completion wait (%p)\n",
746 /* Wait here for the request to complete. */
747 wait_for_completion(request_completion);
749 dev_dbg(&isci_host->pdev->dev,
750 "%s: after completion wait (%p)\n",
755 if (needs_cleanup_handling)
756 isci_request_cleanup_completed_loiterer(
757 isci_host, isci_device, isci_request
761 static void isci_terminate_request(
762 struct isci_host *isci_host,
763 struct isci_remote_device *isci_device,
764 struct isci_request *isci_request,
765 enum isci_request_status new_request_state)
767 enum isci_request_status old_state;
769 DECLARE_COMPLETION_ONSTACK(request_completion);
772 spin_lock_irqsave(&isci_host->scic_lock, flags);
774 /* Change state to "new_request_state" if it is currently "started" */
775 old_state = isci_request_change_started_to_newstate(
781 if ((old_state == started) || (old_state == completed)) {
783 /* If the old_state is started:
784 * This request was not already being aborted. If it had been,
785 * then the aborting I/O (ie. the TMF request) would not be in
786 * the aborting state, and thus would be terminated here. Note
787 * that since the TMF completion's call to the kernel function
788 * "complete()" does not happen until the pending I/O request
789 * terminate fully completes, we do not have to implement a
790 * special wait here for already aborting requests - the
791 * termination of the TMF request will force the request
792 * to finish it's already started terminate.
794 * If old_state == completed:
795 * This request completed from the SCU hardware perspective
796 * and now just needs cleaning up in terms of freeing the
797 * request and potentially calling up to libsas.
799 isci_terminate_request_core(isci_host, isci_device,
800 isci_request, &request_completion);
805 * isci_terminate_pending_requests() - This function will change the all of the
806 * requests on the given device's state to "aborting", will terminate the
807 * requests, and wait for them to complete. This function must only be
808 * called from a thread that can wait. Note that the requests are all
809 * terminated and completed (back to the host, if started there).
810 * @isci_host: This parameter specifies SCU.
811 * @isci_device: This parameter specifies the target.
815 void isci_terminate_pending_requests(
816 struct isci_host *isci_host,
817 struct isci_remote_device *isci_device,
818 enum isci_request_status new_request_state)
820 struct isci_request *isci_request;
821 struct sas_task *task;
825 dev_dbg(&isci_host->pdev->dev,
826 "%s: isci_device = %p (new request state = %d)\n",
827 __func__, isci_device, new_request_state);
829 #define ISCI_TERMINATE_SHOW_PENDING_REQUESTS
830 #ifdef ISCI_TERMINATE_SHOW_PENDING_REQUESTS
832 struct isci_request *request;
834 /* Only abort the task if it's in the
835 * device's request_in_process list
837 list_for_each_entry(request,
838 &isci_device->reqs_in_process,
840 dev_dbg(&isci_host->pdev->dev,
841 "%s: isci_device = %p; request is on "
842 "reqs_in_process list: %p\n",
843 __func__, isci_device, request);
845 #endif /* ISCI_TERMINATE_SHOW_PENDING_REQUESTS */
847 /* Clean up all pending requests. */
849 spin_lock_irqsave(&isci_host->scic_lock, flags);
851 if (list_empty(&isci_device->reqs_in_process)) {
854 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
856 dev_dbg(&isci_host->pdev->dev,
857 "%s: isci_device = %p; done.\n",
858 __func__, isci_device);
860 /* The list was not empty - grab the first request. */
861 isci_request = list_first_entry(
862 &isci_device->reqs_in_process,
863 struct isci_request, dev_node
865 /* Note that we are not expecting to have to control
866 * the target to abort the request.
868 isci_request->complete_in_target = true;
870 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
872 /* Get the libsas task reference. */
873 task = isci_request_access_task(isci_request);
875 dev_dbg(&isci_host->pdev->dev,
876 "%s: isci_device=%p request=%p; task=%p\n",
877 __func__, isci_device, isci_request, task);
879 /* Mark all still pending I/O with the selected next
882 isci_terminate_request(isci_host, isci_device,
883 isci_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(
940 struct domain_device *domain_device,
943 struct isci_host *isci_host = NULL;
944 struct isci_remote_device *isci_device = NULL;
946 bool device_stopping = false;
948 if (domain_device == NULL) {
949 pr_warn("%s: domain_device == NULL\n", __func__);
950 return TMF_RESP_FUNC_FAILED;
953 isci_device = isci_dev_from_domain_dev(domain_device);
955 if (domain_device->port != NULL)
956 isci_host = isci_host_from_sas_ha(domain_device->port->ha);
958 pr_debug("%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
959 __func__, domain_device, isci_host, isci_device);
961 if (isci_device != NULL)
962 device_stopping = (isci_device->status == isci_stopping)
963 || (isci_device->status == isci_stopped);
965 /* If there is a device reset pending on any request in the
966 * device's list, fail this LUN reset request in order to
967 * escalate to the device reset.
969 if ((isci_device == NULL) ||
970 (isci_host == NULL) ||
971 ((isci_host != NULL) &&
972 (isci_device != NULL) &&
974 (isci_device_is_reset_pending(isci_host, isci_device))))) {
975 dev_warn(&isci_host->pdev->dev,
976 "%s: No dev (%p), no host (%p), or "
977 "RESET PENDING: domain_device=%p\n",
978 __func__, isci_device, isci_host, domain_device);
979 return TMF_RESP_FUNC_FAILED;
982 /* Send the task management part of the reset. */
983 if (sas_protocol_ata(domain_device->tproto)) {
984 ret = isci_task_send_lu_reset_sata(
985 isci_host, isci_device, lun
988 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
990 /* If the LUN reset worked, all the I/O can now be terminated. */
991 if (ret == TMF_RESP_FUNC_COMPLETE)
992 /* Terminate all I/O now. */
993 isci_terminate_pending_requests(isci_host,
1001 /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1002 int isci_task_clear_nexus_port(struct asd_sas_port *port)
1004 return TMF_RESP_FUNC_FAILED;
1009 int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1011 return TMF_RESP_FUNC_FAILED;
1014 int isci_task_I_T_nexus_reset(struct domain_device *dev)
1016 return TMF_RESP_FUNC_FAILED;
1020 /* Task Management Functions. Must be called from process context. */
1023 * isci_abort_task_process_cb() - This is a helper function for the abort task
1024 * TMF command. It manages the request state with respect to the successful
1025 * transmission / completion of the abort task request.
1026 * @cb_state: This parameter specifies when this function was called - after
1027 * the TMF request has been started and after it has timed-out.
1028 * @tmf: This parameter specifies the TMF in progress.
1032 static void isci_abort_task_process_cb(
1033 enum isci_tmf_cb_state cb_state,
1034 struct isci_tmf *tmf,
1037 struct isci_request *old_request;
1039 old_request = (struct isci_request *)cb_data;
1041 dev_dbg(&old_request->isci_host->pdev->dev,
1042 "%s: tmf=%p, old_request=%p\n",
1043 __func__, tmf, old_request);
1047 case isci_tmf_started:
1048 /* The TMF has been started. Nothing to do here, since the
1049 * request state was already set to "aborted" by the abort
1052 BUG_ON(old_request->status != aborted);
1055 case isci_tmf_timed_out:
1057 /* Set the task's state to "aborting", since the abort task
1058 * function thread set it to "aborted" (above) in anticipation
1059 * of the task management request working correctly. Since the
1060 * timeout has now fired, the TMF request failed. We set the
1061 * state such that the request completion will indicate the
1062 * device is no longer present.
1064 isci_request_change_state(old_request, aborting);
1068 dev_err(&old_request->isci_host->pdev->dev,
1069 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1070 __func__, cb_state, tmf, old_request);
1076 * isci_task_abort_task() - This function is one of the SAS Domain Template
1077 * functions. This function is called by libsas to abort a specified task.
1078 * @task: This parameter specifies the SAS task to abort.
1080 * status, zero indicates success.
1082 int isci_task_abort_task(struct sas_task *task)
1084 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
1085 struct isci_request *old_request = NULL;
1086 enum isci_request_status old_state;
1087 struct isci_remote_device *isci_device = NULL;
1088 struct isci_host *isci_host = NULL;
1089 struct isci_tmf tmf;
1090 int ret = TMF_RESP_FUNC_FAILED;
1091 unsigned long flags;
1092 bool any_dev_reset = false;
1093 bool device_stopping;
1095 /* Get the isci_request reference from the task. Note that
1096 * this check does not depend on the pending request list
1097 * in the device, because tasks driving resets may land here
1098 * after completion in the core.
1100 old_request = isci_task_get_request_from_task(task, &isci_host,
1103 dev_dbg(&isci_host->pdev->dev,
1104 "%s: task = %p\n", __func__, task);
1106 /* Check if the device has been / is currently being removed.
1107 * If so, no task management will be done, and the I/O will
1110 device_stopping = (isci_device->status == isci_stopping)
1111 || (isci_device->status == isci_stopped);
1113 /* This version of the driver will fail abort requests for
1114 * SATA/STP. Failing the abort request this way will cause the
1115 * SCSI error handler thread to escalate to LUN reset
1117 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1118 dev_warn(&isci_host->pdev->dev,
1119 " task %p is for a STP/SATA device;"
1120 " returning TMF_RESP_FUNC_FAILED\n"
1121 " to cause a LUN reset...\n", task);
1122 return TMF_RESP_FUNC_FAILED;
1125 dev_dbg(&isci_host->pdev->dev,
1126 "%s: old_request == %p\n", __func__, old_request);
1128 if (!device_stopping)
1129 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1131 spin_lock_irqsave(&task->task_state_lock, flags);
1133 /* Don't do resets to stopping devices. */
1134 if (device_stopping) {
1136 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1137 any_dev_reset = false;
1139 } else /* See if there is a pending device reset for this device. */
1140 any_dev_reset = any_dev_reset
1141 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
1143 /* If the extraction of the request reference from the task
1144 * failed, then the request has been completed (or if there is a
1145 * pending reset then this abort request function must be failed
1146 * in order to escalate to the target reset).
1148 if ((old_request == NULL) || any_dev_reset) {
1150 /* If the device reset task flag is set, fail the task
1151 * management request. Otherwise, the original request
1154 if (any_dev_reset) {
1156 /* Turn off the task's DONE to make sure this
1157 * task is escalated to a target reset.
1159 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1161 /* Make the reset happen as soon as possible. */
1162 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1164 spin_unlock_irqrestore(&task->task_state_lock, flags);
1166 /* Fail the task management request in order to
1167 * escalate to the target reset.
1169 ret = TMF_RESP_FUNC_FAILED;
1171 dev_dbg(&isci_host->pdev->dev,
1172 "%s: Failing task abort in order to "
1173 "escalate to target reset because\n"
1174 "SAS_TASK_NEED_DEV_RESET is set for "
1175 "task %p on dev %p\n",
1176 __func__, task, isci_device);
1180 /* The request has already completed and there
1181 * is nothing to do here other than to set the task
1182 * done bit, and indicate that the task abort function
1185 isci_set_task_doneflags(task);
1187 spin_unlock_irqrestore(&task->task_state_lock, flags);
1189 ret = TMF_RESP_FUNC_COMPLETE;
1191 dev_dbg(&isci_host->pdev->dev,
1192 "%s: abort task not needed for %p\n",
1199 spin_unlock_irqrestore(&task->task_state_lock, flags);
1201 spin_lock_irqsave(&isci_host->scic_lock, flags);
1203 /* Check the request status and change to "aborting" if currently
1204 * "starting"; if true then set the I/O kernel completion
1205 * struct that will be triggered when the request completes.
1207 old_state = isci_task_validate_request_to_abort(
1208 old_request, isci_host, isci_device,
1209 &aborted_io_completion);
1210 if ((old_state != started) && (old_state != completed)) {
1212 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1214 /* The request was already being handled by someone else (because
1215 * they got to set the state away from started).
1217 dev_dbg(&isci_host->pdev->dev,
1218 "%s: device = %p; old_request %p already being aborted\n",
1220 isci_device, old_request);
1222 return TMF_RESP_FUNC_COMPLETE;
1224 if ((task->task_proto == SAS_PROTOCOL_SMP)
1226 || old_request->complete_in_target
1229 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1231 dev_dbg(&isci_host->pdev->dev,
1232 "%s: SMP request (%d)"
1233 " or device is stopping (%d)"
1234 " or complete_in_target (%d), thus no TMF\n",
1235 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1236 device_stopping, old_request->complete_in_target);
1238 /* Set the state on the task. */
1239 isci_task_all_done(task);
1241 ret = TMF_RESP_FUNC_COMPLETE;
1243 /* Stopping and SMP devices are not sent a TMF, and are not
1244 * reset, but the outstanding I/O request is terminated below.
1247 /* Fill in the tmf stucture */
1248 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_task_abort,
1249 isci_abort_task_process_cb, old_request);
1251 tmf.io_tag = scic_io_request_get_io_tag(
1252 old_request->sci_request_handle
1255 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1257 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1258 ret = isci_task_execute_tmf(isci_host, &tmf,
1259 ISCI_ABORT_TASK_TIMEOUT_MS);
1261 if (ret != TMF_RESP_FUNC_COMPLETE)
1262 dev_err(&isci_host->pdev->dev,
1263 "%s: isci_task_send_tmf failed\n",
1266 if (ret == TMF_RESP_FUNC_COMPLETE) {
1267 old_request->complete_in_target = true;
1269 /* Clean up the request on our side, and wait for the aborted I/O to
1272 isci_terminate_request_core(isci_host, isci_device, old_request,
1273 &aborted_io_completion);
1276 /* Make sure we do not leave a reference to aborted_io_completion */
1277 old_request->io_request_completion = NULL;
1282 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1283 * functions. This is one of the Task Management functoins called by libsas,
1284 * to abort all task for the given lun.
1285 * @d_device: This parameter specifies the domain device associated with this
1287 * @lun: This parameter specifies the lun associated with this request.
1289 * status, zero indicates success.
1291 int isci_task_abort_task_set(
1292 struct domain_device *d_device,
1295 return TMF_RESP_FUNC_FAILED;
1300 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1301 * functions. This is one of the Task Management functoins called by libsas.
1302 * @d_device: This parameter specifies the domain device associated with this
1304 * @lun: This parameter specifies the lun associated with this request.
1306 * status, zero indicates success.
1308 int isci_task_clear_aca(
1309 struct domain_device *d_device,
1312 return TMF_RESP_FUNC_FAILED;
1318 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1319 * functions. This is one of the Task Management functoins called by libsas.
1320 * @d_device: This parameter specifies the domain device associated with this
1322 * @lun: This parameter specifies the lun associated with this request.
1324 * status, zero indicates success.
1326 int isci_task_clear_task_set(
1327 struct domain_device *d_device,
1330 return TMF_RESP_FUNC_FAILED;
1335 * isci_task_query_task() - This function is implemented to cause libsas to
1336 * correctly escalate the failed abort to a LUN or target reset (this is
1337 * because sas_scsi_find_task libsas function does not correctly interpret
1338 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1339 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1340 * returned, libsas will turn this into a target reset
1341 * @task: This parameter specifies the sas task being queried.
1342 * @lun: This parameter specifies the lun associated with this request.
1344 * status, zero indicates success.
1346 int isci_task_query_task(
1347 struct sas_task *task)
1349 /* See if there is a pending device reset for this device. */
1350 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1351 return TMF_RESP_FUNC_FAILED;
1353 return TMF_RESP_FUNC_SUCC;
1357 * isci_task_request_complete() - This function is called by the sci core when
1358 * an task request completes.
1359 * @isci_host: This parameter specifies the ISCI host object
1360 * @request: This parameter is the completed isci_request object.
1361 * @completion_status: This parameter specifies the completion status from the
1366 void isci_task_request_complete(
1367 struct isci_host *isci_host,
1368 struct isci_request *request,
1369 enum sci_task_status completion_status)
1371 struct isci_remote_device *isci_device = request->isci_device;
1372 enum isci_request_status old_state;
1373 struct isci_tmf *tmf = isci_request_access_tmf(request);
1374 struct completion *tmf_complete;
1376 dev_dbg(&isci_host->pdev->dev,
1377 "%s: request = %p, status=%d\n",
1378 __func__, request, completion_status);
1380 old_state = isci_request_change_state(request, completed);
1382 tmf->status = completion_status;
1383 request->complete_in_target = true;
1385 if (SAS_PROTOCOL_SSP == tmf->proto) {
1387 memcpy(&tmf->resp.resp_iu,
1388 scic_io_request_get_response_iu_address(
1389 request->sci_request_handle
1391 sizeof(struct sci_ssp_response_iu));
1393 } else if (SAS_PROTOCOL_SATA == tmf->proto) {
1395 memcpy(&tmf->resp.d2h_fis,
1396 scic_stp_io_request_get_d2h_reg_address(
1397 request->sci_request_handle
1399 sizeof(struct sata_fis_reg_d2h)
1403 /* Manage the timer if it is still running. */
1404 if (tmf->timeout_timer) {
1405 isci_del_timer(isci_host, tmf->timeout_timer);
1406 tmf->timeout_timer = NULL;
1409 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1410 tmf_complete = tmf->complete;
1412 scic_controller_complete_task(
1413 isci_host->core_controller,
1414 to_sci_dev(isci_device),
1415 request->sci_request_handle
1417 /* NULL the request handle to make sure it cannot be terminated
1418 * or completed again.
1420 request->sci_request_handle = NULL;
1422 isci_request_change_state(request, unallocated);
1423 list_del_init(&request->dev_node);
1425 /* The task management part completes last. */
1426 complete(tmf_complete);
1431 * isci_task_ssp_request_get_lun() - This function is called by the sci core to
1432 * retrieve the lun for a given task request.
1433 * @request: This parameter is the isci_request object.
1435 * lun for specified task request.
1437 u32 isci_task_ssp_request_get_lun(struct isci_request *request)
1439 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1441 dev_dbg(&request->isci_host->pdev->dev,
1442 "%s: lun = %d\n", __func__, isci_tmf->lun[0]);
1443 /* @todo: build lun from array of bytes to 32 bit */
1444 return isci_tmf->lun[0];
1448 * isci_task_ssp_request_get_function() - This function is called by the sci
1449 * core to retrieve the function for a given task request.
1450 * @request: This parameter is the isci_request object.
1452 * function code for specified task request.
1454 u8 isci_task_ssp_request_get_function(struct isci_request *request)
1456 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1458 dev_dbg(&request->isci_host->pdev->dev,
1459 "%s: func = %d\n", __func__, isci_tmf->tmf_code);
1461 return isci_tmf->tmf_code;
1465 * isci_task_ssp_request_get_io_tag_to_manage() - This function is called by
1466 * the sci core to retrieve the io tag for a given task request.
1467 * @request: This parameter is the isci_request object.
1469 * io tag for specified task request.
1471 u16 isci_task_ssp_request_get_io_tag_to_manage(struct isci_request *request)
1473 u16 io_tag = SCI_CONTROLLER_INVALID_IO_TAG;
1475 if (tmf_task == request->ttype) {
1476 struct isci_tmf *tmf = isci_request_access_tmf(request);
1477 io_tag = tmf->io_tag;
1480 dev_dbg(&request->isci_host->pdev->dev,
1481 "%s: request = %p, io_tag = %d\n",
1482 __func__, request, io_tag);
1488 * isci_task_ssp_request_get_response_data_address() - This function is called
1489 * by the sci core to retrieve the response data address for a given task
1491 * @request: This parameter is the isci_request object.
1493 * response data address for specified task request.
1495 void *isci_task_ssp_request_get_response_data_address(
1496 struct isci_request *request)
1498 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1500 return &isci_tmf->resp.resp_iu;
1504 * isci_task_ssp_request_get_response_data_length() - This function is called
1505 * by the sci core to retrieve the response data length for a given task
1507 * @request: This parameter is the isci_request object.
1509 * response data length for specified task request.
1511 u32 isci_task_ssp_request_get_response_data_length(
1512 struct isci_request *request)
1514 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1516 return sizeof(isci_tmf->resp.resp_iu);
1520 * isci_bus_reset_handler() - This function performs a target reset of the
1521 * device referenced by "cmd'. This function is exported through the
1522 * "struct scsi_host_template" structure such that it is called when an I/O
1523 * recovery process has escalated to a target reset. Note that this function
1524 * is called from the scsi error handler event thread, so may block on calls.
1525 * @scsi_cmd: This parameter specifies the target to be reset.
1527 * SUCCESS if the reset process was successful, else FAILED.
1529 int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1531 unsigned long flags = 0;
1532 struct isci_host *isci_host = NULL;
1533 enum sci_status status;
1535 struct isci_remote_device *isci_dev
1536 = isci_dev_from_domain_dev(
1537 sdev_to_domain_dev(cmd->device));
1539 dev_dbg(&cmd->device->sdev_gendev,
1540 "%s: cmd %p, isci_dev %p\n",
1541 __func__, cmd, isci_dev);
1544 dev_warn(&cmd->device->sdev_gendev,
1545 "%s: isci_dev is GONE!\n",
1548 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1551 if (isci_dev->isci_port != NULL)
1552 isci_host = isci_dev->isci_port->isci_host;
1554 if (isci_host != NULL)
1555 spin_lock_irqsave(&isci_host->scic_lock, flags);
1557 status = scic_remote_device_reset(to_sci_dev(isci_dev));
1558 if (status != SCI_SUCCESS) {
1560 if (isci_host != NULL)
1561 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1563 scmd_printk(KERN_WARNING, cmd,
1564 "%s: scic_remote_device_reset(%p) returned %d!\n",
1565 __func__, isci_dev, status);
1567 return TMF_RESP_FUNC_FAILED;
1569 if (isci_host != NULL)
1570 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1572 /* Make sure all pending requests are able to be fully terminated. */
1573 isci_device_clear_reset_pending(isci_dev);
1575 /* Terminate in-progress I/O now. */
1576 isci_remote_device_nuke_requests(isci_dev);
1578 /* Call into the libsas default handler (which calls sas_phy_reset). */
1579 base_status = sas_eh_bus_reset_handler(cmd);
1581 if (base_status != SUCCESS) {
1583 /* There can be cases where the resets to individual devices
1584 * behind an expander will fail because of an unplug of the
1587 scmd_printk(KERN_WARNING, cmd,
1588 "%s: sas_eh_bus_reset_handler(%p) returned %d!\n",
1589 __func__, cmd, base_status);
1592 /* WHAT TO DO HERE IF sas_phy_reset FAILS? */
1594 if (isci_host != NULL)
1595 spin_lock_irqsave(&isci_host->scic_lock, flags);
1596 status = scic_remote_device_reset_complete(to_sci_dev(isci_dev));
1598 if (isci_host != NULL)
1599 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1601 if (status != SCI_SUCCESS) {
1602 scmd_printk(KERN_WARNING, cmd,
1603 "%s: scic_remote_device_reset_complete(%p) "
1605 __func__, isci_dev, status);
1607 /* WHAT TO DO HERE IF scic_remote_device_reset_complete FAILS? */
1609 dev_dbg(&cmd->device->sdev_gendev,
1610 "%s: cmd %p, isci_dev %p complete.\n",
1611 __func__, cmd, isci_dev);
1613 return TMF_RESP_FUNC_COMPLETE;