2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 #include <linux/completion.h>
57 #include <linux/irqflags.h>
59 #include <scsi/libsas.h>
60 #include "remote_device.h"
61 #include "remote_node_context.h"
68 * isci_task_refuse() - complete the request to the upper layer driver in
69 * the case where an I/O needs to be completed back in the submit path.
70 * @ihost: host on which the the request was queued
71 * @task: request to complete
72 * @response: response code for the completed task.
73 * @status: status code for the completed task.
76 static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
81 enum isci_completion_selection disposition;
83 disposition = isci_perform_normal_io_completion;
84 disposition = isci_task_set_completion_status(task, response, status,
87 /* Tasks aborted specifically by a call to the lldd_abort_task
88 * function should not be completed to the host in the regular path.
90 switch (disposition) {
91 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
93 dev_dbg(&ihost->pdev->dev,
94 "%s: Normal - task = %p, response=%d, "
96 __func__, task, response, status);
98 task->lldd_task = NULL;
100 isci_execpath_callback(ihost, task, task->task_done);
103 case isci_perform_aborted_io_completion:
104 /* No notification because this request is already in the
107 dev_warn(&ihost->pdev->dev,
108 "%s: Aborted - task = %p, response=%d, "
110 __func__, task, response, status);
113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
115 dev_warn(&ihost->pdev->dev,
116 "%s: Error - task = %p, response=%d, "
118 __func__, task, response, status);
120 isci_execpath_callback(ihost, task, sas_task_abort);
124 dev_warn(&ihost->pdev->dev,
125 "%s: isci task notification default case!",
127 sas_task_abort(task);
132 #define for_each_sas_task(num, task) \
133 for (; num > 0; num--,\
134 task = list_entry(task->list.next, struct sas_task, list))
137 * isci_task_execute_task() - This function is one of the SAS Domain Template
138 * functions. This function is called by libsas to send a task down to
140 * @task: This parameter specifies the SAS task to send.
141 * @num: This parameter specifies the number of tasks to queue.
142 * @gfp_flags: This parameter specifies the context of this call.
144 * status, zero indicates success.
146 int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
148 struct isci_host *ihost = dev_to_ihost(task->dev);
149 struct isci_remote_device *device;
152 enum sci_status status;
153 enum isci_status device_status;
155 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
157 /* Check if we have room for more tasks */
158 ret = isci_host_can_queue(ihost, num);
161 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
165 for_each_sas_task(num, task) {
166 dev_dbg(&ihost->pdev->dev,
167 "task = %p, num = %d; dev = %p; cmd = %p\n",
168 task, num, task->dev, task->uldd_task);
170 device = task->dev->lldd_dev;
173 device_status = device->status;
175 device_status = isci_freed;
177 /* From this point onward, any process that needs to guarantee
178 * that there is no kernel I/O being started will have to wait
179 * for the quiesce spinlock.
182 if (device_status != isci_ready_for_io) {
184 /* Forces a retry from scsi mid layer. */
185 dev_dbg(&ihost->pdev->dev,
186 "%s: task %p: isci_host->status = %d, "
187 "device = %p; device_status = 0x%x\n\n",
190 isci_host_get_state(ihost),
194 if (device_status == isci_ready) {
195 /* Indicate QUEUE_FULL so that the scsi midlayer
198 isci_task_refuse(ihost, task,
202 /* Else, the device is going down. */
203 isci_task_refuse(ihost, task,
204 SAS_TASK_UNDELIVERED,
207 isci_host_can_dequeue(ihost, 1);
209 /* There is a device and it's ready for I/O. */
210 spin_lock_irqsave(&task->task_state_lock, flags);
212 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
214 spin_unlock_irqrestore(&task->task_state_lock,
217 isci_task_refuse(ihost, task,
218 SAS_TASK_UNDELIVERED,
219 SAM_STAT_TASK_ABORTED);
221 /* The I/O was aborted. */
224 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
225 spin_unlock_irqrestore(&task->task_state_lock, flags);
227 /* build and send the request. */
228 status = isci_request_execute(ihost, task, gfp_flags);
230 if (status != SCI_SUCCESS) {
232 spin_lock_irqsave(&task->task_state_lock, flags);
233 /* Did not really start this command. */
234 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
235 spin_unlock_irqrestore(&task->task_state_lock, flags);
237 /* Indicate QUEUE_FULL so that the scsi
238 * midlayer retries. if the request
239 * failed for remote device reasons,
240 * it gets returned as
241 * SAS_TASK_UNDELIVERED next time
244 isci_task_refuse(ihost, task,
247 isci_host_can_dequeue(ihost, 1);
255 static struct isci_request *isci_task_request_build(struct isci_host *ihost,
256 struct isci_tmf *isci_tmf)
258 struct scic_sds_remote_device *sci_dev;
259 enum sci_status status = SCI_FAILURE;
260 struct isci_request *ireq = NULL;
261 struct isci_remote_device *idev;
262 struct domain_device *dev;
264 dev_dbg(&ihost->pdev->dev,
265 "%s: isci_tmf = %p\n", __func__, isci_tmf);
267 idev = isci_tmf->device;
268 sci_dev = &idev->sci;
269 dev = idev->domain_dev;
271 /* do common allocation and init of request object. */
272 ireq = isci_request_alloc_tmf(ihost, isci_tmf, idev, GFP_ATOMIC);
276 /* let the core do it's construct. */
277 status = scic_task_request_construct(&ihost->sci, sci_dev,
278 SCI_CONTROLLER_INVALID_IO_TAG,
281 if (status != SCI_SUCCESS) {
282 dev_warn(&ihost->pdev->dev,
283 "%s: scic_task_request_construct failed - "
290 /* XXX convert to get this from task->tproto like other drivers */
291 if (dev->dev_type == SAS_END_DEV) {
292 isci_tmf->proto = SAS_PROTOCOL_SSP;
293 status = scic_task_request_construct_ssp(&ireq->sci);
294 if (status != SCI_SUCCESS)
298 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
299 isci_tmf->proto = SAS_PROTOCOL_SATA;
300 status = isci_sata_management_task_request_build(ireq);
302 if (status != SCI_SUCCESS)
307 isci_request_free(ihost, ireq);
313 * isci_task_execute_tmf() - This function builds and sends a task request,
314 * then waits for the completion.
315 * @isci_host: This parameter specifies the ISCI host object
316 * @tmf: This parameter is the pointer to the task management structure for
318 * @timeout_ms: This parameter specifies the timeout period for the task
319 * management request.
321 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
322 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
324 int isci_task_execute_tmf(struct isci_host *ihost, struct isci_tmf *tmf,
325 unsigned long timeout_ms)
327 DECLARE_COMPLETION_ONSTACK(completion);
328 enum sci_task_status status = SCI_TASK_FAILURE;
329 struct scic_sds_remote_device *sci_device;
330 struct isci_remote_device *isci_device = tmf->device;
331 struct isci_request *ireq;
332 int ret = TMF_RESP_FUNC_FAILED;
334 unsigned long timeleft;
336 /* sanity check, return TMF_RESP_FUNC_FAILED
337 * if the device is not there and ready.
339 if (!isci_device || isci_device->status != isci_ready_for_io) {
340 dev_dbg(&ihost->pdev->dev,
341 "%s: isci_device = %p not ready (%d)\n",
343 isci_device, isci_device->status);
344 return TMF_RESP_FUNC_FAILED;
346 dev_dbg(&ihost->pdev->dev,
347 "%s: isci_device = %p\n",
348 __func__, isci_device);
350 sci_device = &isci_device->sci;
352 /* Assign the pointer to the TMF's completion kernel wait structure. */
353 tmf->complete = &completion;
355 ireq = isci_task_request_build(ihost, tmf);
357 dev_warn(&ihost->pdev->dev,
358 "%s: isci_task_request_build failed\n",
360 return TMF_RESP_FUNC_FAILED;
363 spin_lock_irqsave(&ihost->scic_lock, flags);
365 /* start the TMF io. */
366 status = scic_controller_start_task(
370 SCI_CONTROLLER_INVALID_IO_TAG);
372 if (status != SCI_TASK_SUCCESS) {
373 dev_warn(&ihost->pdev->dev,
374 "%s: start_io failed - status = 0x%x, request = %p\n",
378 spin_unlock_irqrestore(&ihost->scic_lock, flags);
379 goto cleanup_request;
382 if (tmf->cb_state_func != NULL)
383 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
385 isci_request_change_state(ireq, started);
387 /* add the request to the remote device request list. */
388 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
390 spin_unlock_irqrestore(&ihost->scic_lock, flags);
392 /* Wait for the TMF to complete, or a timeout. */
393 timeleft = wait_for_completion_timeout(&completion,
394 jiffies + msecs_to_jiffies(timeout_ms));
397 spin_lock_irqsave(&ihost->scic_lock, flags);
399 if (tmf->cb_state_func != NULL)
400 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
402 status = scic_controller_terminate_request(
403 &ireq->isci_host->sci,
404 &ireq->isci_device->sci,
407 spin_unlock_irqrestore(&ihost->scic_lock, flags);
412 if (tmf->status == SCI_SUCCESS)
413 ret = TMF_RESP_FUNC_COMPLETE;
414 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
415 dev_dbg(&ihost->pdev->dev,
417 "SCI_FAILURE_IO_RESPONSE_VALID\n",
419 ret = TMF_RESP_FUNC_COMPLETE;
421 /* Else - leave the default "failed" status alone. */
423 dev_dbg(&ihost->pdev->dev,
424 "%s: completed request = %p\n",
428 if (ireq->io_request_completion != NULL) {
429 /* A thread is waiting for this TMF to finish. */
430 complete(ireq->io_request_completion);
434 isci_request_free(ihost, ireq);
438 void isci_task_build_tmf(
439 struct isci_tmf *tmf,
440 struct isci_remote_device *isci_device,
441 enum isci_tmf_function_codes code,
442 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
447 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
448 "%s: isci_device = %p\n", __func__, isci_device);
450 memset(tmf, 0, sizeof(*tmf));
452 tmf->device = isci_device;
453 tmf->tmf_code = code;
455 tmf->cb_state_func = tmf_sent_cb;
456 tmf->cb_data = cb_data;
459 static void isci_task_build_abort_task_tmf(
460 struct isci_tmf *tmf,
461 struct isci_remote_device *isci_device,
462 enum isci_tmf_function_codes code,
463 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
466 struct isci_request *old_request)
468 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
469 (void *)old_request);
470 tmf->io_tag = old_request->io_tag;
473 static struct isci_request *isci_task_get_request_from_task(
474 struct sas_task *task,
475 struct isci_remote_device **isci_device)
478 struct isci_request *request = NULL;
481 spin_lock_irqsave(&task->task_state_lock, flags);
483 request = task->lldd_task;
485 /* If task is already done, the request isn't valid */
486 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
487 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
490 if (isci_device != NULL)
491 *isci_device = request->isci_device;
494 spin_unlock_irqrestore(&task->task_state_lock, flags);
500 * isci_task_validate_request_to_abort() - This function checks the given I/O
501 * against the "started" state. If the request is still "started", it's
502 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
503 * BEFORE CALLING THIS FUNCTION.
504 * @isci_request: This parameter specifies the request object to control.
505 * @isci_host: This parameter specifies the ISCI host object
506 * @isci_device: This is the device to which the request is pending.
507 * @aborted_io_completion: This is a completion structure that will be added to
508 * the request in case it is changed to aborting; this completion is
509 * triggered when the request is fully completed.
511 * Either "started" on successful change of the task status to "aborted", or
512 * "unallocated" if the task cannot be controlled.
514 static enum isci_request_status isci_task_validate_request_to_abort(
515 struct isci_request *isci_request,
516 struct isci_host *isci_host,
517 struct isci_remote_device *isci_device,
518 struct completion *aborted_io_completion)
520 enum isci_request_status old_state = unallocated;
522 /* Only abort the task if it's in the
523 * device's request_in_process list
525 if (isci_request && !list_empty(&isci_request->dev_node)) {
526 old_state = isci_request_change_started_to_aborted(
527 isci_request, aborted_io_completion);
535 * isci_request_cleanup_completed_loiterer() - This function will take care of
536 * the final cleanup on any request which has been explicitly terminated.
537 * @isci_host: This parameter specifies the ISCI host object
538 * @isci_device: This is the device to which the request is pending.
539 * @isci_request: This parameter specifies the terminated request object.
540 * @task: This parameter is the libsas I/O request.
542 static void isci_request_cleanup_completed_loiterer(
543 struct isci_host *isci_host,
544 struct isci_remote_device *isci_device,
545 struct isci_request *isci_request,
546 struct sas_task *task)
550 dev_dbg(&isci_host->pdev->dev,
551 "%s: isci_device=%p, request=%p, task=%p\n",
552 __func__, isci_device, isci_request, task);
556 spin_lock_irqsave(&task->task_state_lock, flags);
557 task->lldd_task = NULL;
559 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
561 isci_set_task_doneflags(task);
563 /* If this task is not in the abort path, call task_done. */
564 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
566 spin_unlock_irqrestore(&task->task_state_lock, flags);
567 task->task_done(task);
569 spin_unlock_irqrestore(&task->task_state_lock, flags);
572 if (isci_request != NULL) {
573 spin_lock_irqsave(&isci_host->scic_lock, flags);
574 list_del_init(&isci_request->dev_node);
575 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
577 isci_request_free(isci_host, isci_request);
582 * isci_terminate_request_core() - This function will terminate the given
583 * request, and wait for it to complete. This function must only be called
584 * from a thread that can wait. Note that the request is terminated and
585 * completed (back to the host, if started there).
586 * @isci_host: This SCU.
587 * @isci_device: The target.
588 * @isci_request: The I/O request to be terminated.
591 static void isci_terminate_request_core(
592 struct isci_host *isci_host,
593 struct isci_remote_device *isci_device,
594 struct isci_request *isci_request)
596 enum sci_status status = SCI_SUCCESS;
597 bool was_terminated = false;
598 bool needs_cleanup_handling = false;
599 enum isci_request_status request_status;
601 unsigned long termination_completed = 1;
602 struct completion *io_request_completion;
603 struct sas_task *task;
605 dev_dbg(&isci_host->pdev->dev,
606 "%s: device = %p; request = %p\n",
607 __func__, isci_device, isci_request);
609 spin_lock_irqsave(&isci_host->scic_lock, flags);
611 io_request_completion = isci_request->io_request_completion;
613 task = (isci_request->ttype == io_task)
614 ? isci_request_access_task(isci_request)
617 /* Note that we are not going to control
618 * the target to abort the request.
620 isci_request->complete_in_target = true;
622 /* Make sure the request wasn't just sitting around signalling
623 * device condition (if the request handle is NULL, then the
624 * request completed but needed additional handling here).
626 if (!isci_request->terminated) {
627 was_terminated = true;
628 needs_cleanup_handling = true;
629 status = scic_controller_terminate_request(
634 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
637 * The only time the request to terminate will
638 * fail is when the io request is completed and
641 if (status != SCI_SUCCESS) {
642 dev_err(&isci_host->pdev->dev,
643 "%s: scic_controller_terminate_request"
644 " returned = 0x%x\n",
647 isci_request->io_request_completion = NULL;
650 if (was_terminated) {
651 dev_dbg(&isci_host->pdev->dev,
652 "%s: before completion wait (%p/%p)\n",
653 __func__, isci_request, io_request_completion);
655 /* Wait here for the request to complete. */
656 #define TERMINATION_TIMEOUT_MSEC 500
657 termination_completed
658 = wait_for_completion_timeout(
659 io_request_completion,
660 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
662 if (!termination_completed) {
664 /* The request to terminate has timed out. */
665 spin_lock_irqsave(&isci_host->scic_lock,
668 /* Check for state changes. */
669 if (!isci_request->terminated) {
671 /* The best we can do is to have the
672 * request die a silent death if it
673 * ever really completes.
675 * Set the request state to "dead",
676 * and clear the task pointer so that
677 * an actual completion event callback
678 * doesn't do anything.
680 isci_request->status = dead;
681 isci_request->io_request_completion
684 if (isci_request->ttype == io_task) {
686 /* Break links with the
689 isci_request->ttype_ptr.io_task_ptr
693 termination_completed = 1;
695 spin_unlock_irqrestore(&isci_host->scic_lock,
698 if (!termination_completed) {
700 dev_err(&isci_host->pdev->dev,
701 "%s: *** Timeout waiting for "
702 "termination(%p/%p)\n",
703 __func__, io_request_completion,
706 /* The request can no longer be referenced
707 * safely since it may go away if the
708 * termination every really does complete.
713 if (termination_completed)
714 dev_dbg(&isci_host->pdev->dev,
715 "%s: after completion wait (%p/%p)\n",
716 __func__, isci_request, io_request_completion);
719 if (termination_completed) {
721 isci_request->io_request_completion = NULL;
723 /* Peek at the status of the request. This will tell
724 * us if there was special handling on the request such that it
725 * needs to be detached and freed here.
727 spin_lock_irqsave(&isci_request->state_lock, flags);
728 request_status = isci_request_get_state(isci_request);
730 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
731 && ((request_status == aborted)
732 || (request_status == aborting)
733 || (request_status == terminating)
734 || (request_status == completed)
735 || (request_status == dead)
739 /* The completion routine won't free a request in
740 * the aborted/aborting/etc. states, so we do
743 needs_cleanup_handling = true;
745 spin_unlock_irqrestore(&isci_request->state_lock, flags);
748 if (needs_cleanup_handling)
749 isci_request_cleanup_completed_loiterer(
750 isci_host, isci_device, isci_request, task);
755 * isci_terminate_pending_requests() - This function will change the all of the
756 * requests on the given device's state to "aborting", will terminate the
757 * requests, and wait for them to complete. This function must only be
758 * called from a thread that can wait. Note that the requests are all
759 * terminated and completed (back to the host, if started there).
760 * @isci_host: This parameter specifies SCU.
761 * @isci_device: This parameter specifies the target.
764 void isci_terminate_pending_requests(struct isci_host *ihost,
765 struct isci_remote_device *idev)
767 struct completion request_completion;
768 enum isci_request_status old_state;
772 spin_lock_irqsave(&ihost->scic_lock, flags);
773 list_splice_init(&idev->reqs_in_process, &list);
775 /* assumes that isci_terminate_request_core deletes from the list */
776 while (!list_empty(&list)) {
777 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
779 /* Change state to "terminating" if it is currently
782 old_state = isci_request_change_started_to_newstate(ireq,
791 /* termination in progress, or otherwise dispositioned.
792 * We know the request was on 'list' so should be safe
793 * to move it back to reqs_in_process
795 list_move(&ireq->dev_node, &idev->reqs_in_process);
802 spin_unlock_irqrestore(&ihost->scic_lock, flags);
804 init_completion(&request_completion);
806 dev_dbg(&ihost->pdev->dev,
807 "%s: idev=%p request=%p; task=%p old_state=%d\n",
808 __func__, idev, ireq,
809 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
812 /* If the old_state is started:
813 * This request was not already being aborted. If it had been,
814 * then the aborting I/O (ie. the TMF request) would not be in
815 * the aborting state, and thus would be terminated here. Note
816 * that since the TMF completion's call to the kernel function
817 * "complete()" does not happen until the pending I/O request
818 * terminate fully completes, we do not have to implement a
819 * special wait here for already aborting requests - the
820 * termination of the TMF request will force the request
821 * to finish it's already started terminate.
823 * If old_state == completed:
824 * This request completed from the SCU hardware perspective
825 * and now just needs cleaning up in terms of freeing the
826 * request and potentially calling up to libsas.
828 * If old_state == aborting:
829 * This request has already gone through a TMF timeout, but may
830 * not have been terminated; needs cleaning up at least.
832 isci_terminate_request_core(ihost, idev, ireq);
833 spin_lock_irqsave(&ihost->scic_lock, flags);
835 spin_unlock_irqrestore(&ihost->scic_lock, flags);
839 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
840 * Template functions.
841 * @lun: This parameter specifies the lun to be reset.
843 * status, zero indicates success.
845 static int isci_task_send_lu_reset_sas(
846 struct isci_host *isci_host,
847 struct isci_remote_device *isci_device,
851 int ret = TMF_RESP_FUNC_FAILED;
853 dev_dbg(&isci_host->pdev->dev,
854 "%s: isci_host = %p, isci_device = %p\n",
855 __func__, isci_host, isci_device);
856 /* Send the LUN reset to the target. By the time the call returns,
857 * the TMF has fully exected in the target (in which case the return
858 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
859 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
861 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
864 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
865 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
867 if (ret == TMF_RESP_FUNC_COMPLETE)
868 dev_dbg(&isci_host->pdev->dev,
869 "%s: %p: TMF_LU_RESET passed\n",
870 __func__, isci_device);
872 dev_dbg(&isci_host->pdev->dev,
873 "%s: %p: TMF_LU_RESET failed (%x)\n",
874 __func__, isci_device, ret);
880 * isci_task_lu_reset() - This function is one of the SAS Domain Template
881 * functions. This is one of the Task Management functoins called by libsas,
882 * to reset the given lun. Note the assumption that while this call is
883 * executing, no I/O will be sent by the host to the device.
884 * @lun: This parameter specifies the lun to be reset.
886 * status, zero indicates success.
888 int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
890 struct isci_host *isci_host = dev_to_ihost(domain_device);
891 struct isci_remote_device *isci_device = NULL;
893 bool device_stopping = false;
895 isci_device = domain_device->lldd_dev;
897 dev_dbg(&isci_host->pdev->dev,
898 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
899 __func__, domain_device, isci_host, isci_device);
901 if (isci_device != NULL) {
902 device_stopping = (isci_device->status == isci_stopping)
903 || (isci_device->status == isci_stopped);
904 set_bit(IDEV_EH, &isci_device->flags);
907 /* If there is a device reset pending on any request in the
908 * device's list, fail this LUN reset request in order to
909 * escalate to the device reset.
911 if (!isci_device || device_stopping ||
912 isci_device_is_reset_pending(isci_host, isci_device)) {
913 dev_warn(&isci_host->pdev->dev,
914 "%s: No dev (%p), or "
915 "RESET PENDING: domain_device=%p\n",
916 __func__, isci_device, domain_device);
917 return TMF_RESP_FUNC_FAILED;
920 /* Send the task management part of the reset. */
921 if (sas_protocol_ata(domain_device->tproto)) {
922 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
924 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
926 /* If the LUN reset worked, all the I/O can now be terminated. */
927 if (ret == TMF_RESP_FUNC_COMPLETE)
928 /* Terminate all I/O now. */
929 isci_terminate_pending_requests(isci_host,
936 /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
937 int isci_task_clear_nexus_port(struct asd_sas_port *port)
939 return TMF_RESP_FUNC_FAILED;
944 int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
946 return TMF_RESP_FUNC_FAILED;
949 /* Task Management Functions. Must be called from process context. */
952 * isci_abort_task_process_cb() - This is a helper function for the abort task
953 * TMF command. It manages the request state with respect to the successful
954 * transmission / completion of the abort task request.
955 * @cb_state: This parameter specifies when this function was called - after
956 * the TMF request has been started and after it has timed-out.
957 * @tmf: This parameter specifies the TMF in progress.
961 static void isci_abort_task_process_cb(
962 enum isci_tmf_cb_state cb_state,
963 struct isci_tmf *tmf,
966 struct isci_request *old_request;
968 old_request = (struct isci_request *)cb_data;
970 dev_dbg(&old_request->isci_host->pdev->dev,
971 "%s: tmf=%p, old_request=%p\n",
972 __func__, tmf, old_request);
976 case isci_tmf_started:
977 /* The TMF has been started. Nothing to do here, since the
978 * request state was already set to "aborted" by the abort
981 if ((old_request->status != aborted)
982 && (old_request->status != completed))
983 dev_err(&old_request->isci_host->pdev->dev,
984 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
985 __func__, old_request->status, tmf, old_request);
988 case isci_tmf_timed_out:
990 /* Set the task's state to "aborting", since the abort task
991 * function thread set it to "aborted" (above) in anticipation
992 * of the task management request working correctly. Since the
993 * timeout has now fired, the TMF request failed. We set the
994 * state such that the request completion will indicate the
995 * device is no longer present.
997 isci_request_change_state(old_request, aborting);
1001 dev_err(&old_request->isci_host->pdev->dev,
1002 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1003 __func__, cb_state, tmf, old_request);
1009 * isci_task_abort_task() - This function is one of the SAS Domain Template
1010 * functions. This function is called by libsas to abort a specified task.
1011 * @task: This parameter specifies the SAS task to abort.
1013 * status, zero indicates success.
1015 int isci_task_abort_task(struct sas_task *task)
1017 struct isci_host *isci_host = dev_to_ihost(task->dev);
1018 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
1019 struct isci_request *old_request = NULL;
1020 enum isci_request_status old_state;
1021 struct isci_remote_device *isci_device = NULL;
1022 struct isci_tmf tmf;
1023 int ret = TMF_RESP_FUNC_FAILED;
1024 unsigned long flags;
1025 bool any_dev_reset = false;
1026 bool device_stopping;
1028 /* Get the isci_request reference from the task. Note that
1029 * this check does not depend on the pending request list
1030 * in the device, because tasks driving resets may land here
1031 * after completion in the core.
1033 old_request = isci_task_get_request_from_task(task, &isci_device);
1035 dev_dbg(&isci_host->pdev->dev,
1036 "%s: task = %p\n", __func__, task);
1038 /* Check if the device has been / is currently being removed.
1039 * If so, no task management will be done, and the I/O will
1042 device_stopping = (isci_device->status == isci_stopping)
1043 || (isci_device->status == isci_stopped);
1045 /* XXX need to fix device lookup lifetime (needs to be done
1046 * under scic_lock, among other things...), but for now assume
1047 * the device is available like the above code
1049 set_bit(IDEV_EH, &isci_device->flags);
1051 /* This version of the driver will fail abort requests for
1052 * SATA/STP. Failing the abort request this way will cause the
1053 * SCSI error handler thread to escalate to LUN reset
1055 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1056 dev_warn(&isci_host->pdev->dev,
1057 " task %p is for a STP/SATA device;"
1058 " returning TMF_RESP_FUNC_FAILED\n"
1059 " to cause a LUN reset...\n", task);
1060 return TMF_RESP_FUNC_FAILED;
1063 dev_dbg(&isci_host->pdev->dev,
1064 "%s: old_request == %p\n", __func__, old_request);
1066 if (!device_stopping)
1067 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1069 spin_lock_irqsave(&task->task_state_lock, flags);
1071 /* Don't do resets to stopping devices. */
1072 if (device_stopping) {
1074 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1075 any_dev_reset = false;
1077 } else /* See if there is a pending device reset for this device. */
1078 any_dev_reset = any_dev_reset
1079 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
1081 /* If the extraction of the request reference from the task
1082 * failed, then the request has been completed (or if there is a
1083 * pending reset then this abort request function must be failed
1084 * in order to escalate to the target reset).
1086 if ((old_request == NULL) || any_dev_reset) {
1088 /* If the device reset task flag is set, fail the task
1089 * management request. Otherwise, the original request
1092 if (any_dev_reset) {
1094 /* Turn off the task's DONE to make sure this
1095 * task is escalated to a target reset.
1097 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1099 /* Make the reset happen as soon as possible. */
1100 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1102 spin_unlock_irqrestore(&task->task_state_lock, flags);
1104 /* Fail the task management request in order to
1105 * escalate to the target reset.
1107 ret = TMF_RESP_FUNC_FAILED;
1109 dev_dbg(&isci_host->pdev->dev,
1110 "%s: Failing task abort in order to "
1111 "escalate to target reset because\n"
1112 "SAS_TASK_NEED_DEV_RESET is set for "
1113 "task %p on dev %p\n",
1114 __func__, task, isci_device);
1118 /* The request has already completed and there
1119 * is nothing to do here other than to set the task
1120 * done bit, and indicate that the task abort function
1123 isci_set_task_doneflags(task);
1125 spin_unlock_irqrestore(&task->task_state_lock, flags);
1127 ret = TMF_RESP_FUNC_COMPLETE;
1129 dev_dbg(&isci_host->pdev->dev,
1130 "%s: abort task not needed for %p\n",
1137 spin_unlock_irqrestore(&task->task_state_lock, flags);
1139 spin_lock_irqsave(&isci_host->scic_lock, flags);
1141 /* Check the request status and change to "aborted" if currently
1142 * "starting"; if true then set the I/O kernel completion
1143 * struct that will be triggered when the request completes.
1145 old_state = isci_task_validate_request_to_abort(
1146 old_request, isci_host, isci_device,
1147 &aborted_io_completion);
1148 if ((old_state != started) &&
1149 (old_state != completed) &&
1150 (old_state != aborting)) {
1152 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1154 /* The request was already being handled by someone else (because
1155 * they got to set the state away from started).
1157 dev_dbg(&isci_host->pdev->dev,
1158 "%s: device = %p; old_request %p already being aborted\n",
1160 isci_device, old_request);
1162 return TMF_RESP_FUNC_COMPLETE;
1164 if ((task->task_proto == SAS_PROTOCOL_SMP)
1166 || old_request->complete_in_target
1169 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1171 dev_dbg(&isci_host->pdev->dev,
1172 "%s: SMP request (%d)"
1173 " or device is stopping (%d)"
1174 " or complete_in_target (%d), thus no TMF\n",
1175 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1176 device_stopping, old_request->complete_in_target);
1178 /* Set the state on the task. */
1179 isci_task_all_done(task);
1181 ret = TMF_RESP_FUNC_COMPLETE;
1183 /* Stopping and SMP devices are not sent a TMF, and are not
1184 * reset, but the outstanding I/O request is terminated below.
1187 /* Fill in the tmf stucture */
1188 isci_task_build_abort_task_tmf(&tmf, isci_device,
1189 isci_tmf_ssp_task_abort,
1190 isci_abort_task_process_cb,
1193 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1195 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1196 ret = isci_task_execute_tmf(isci_host, &tmf,
1197 ISCI_ABORT_TASK_TIMEOUT_MS);
1199 if (ret != TMF_RESP_FUNC_COMPLETE)
1200 dev_err(&isci_host->pdev->dev,
1201 "%s: isci_task_send_tmf failed\n",
1204 if (ret == TMF_RESP_FUNC_COMPLETE) {
1205 old_request->complete_in_target = true;
1207 /* Clean up the request on our side, and wait for the aborted
1210 isci_terminate_request_core(isci_host, isci_device, old_request);
1213 /* Make sure we do not leave a reference to aborted_io_completion */
1214 old_request->io_request_completion = NULL;
1219 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1220 * functions. This is one of the Task Management functoins called by libsas,
1221 * to abort all task for the given lun.
1222 * @d_device: This parameter specifies the domain device associated with this
1224 * @lun: This parameter specifies the lun associated with this request.
1226 * status, zero indicates success.
1228 int isci_task_abort_task_set(
1229 struct domain_device *d_device,
1232 return TMF_RESP_FUNC_FAILED;
1237 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1238 * functions. This is one of the Task Management functoins called by libsas.
1239 * @d_device: This parameter specifies the domain device associated with this
1241 * @lun: This parameter specifies the lun associated with this request.
1243 * status, zero indicates success.
1245 int isci_task_clear_aca(
1246 struct domain_device *d_device,
1249 return TMF_RESP_FUNC_FAILED;
1255 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1256 * functions. This is one of the Task Management functoins called by libsas.
1257 * @d_device: This parameter specifies the domain device associated with this
1259 * @lun: This parameter specifies the lun associated with this request.
1261 * status, zero indicates success.
1263 int isci_task_clear_task_set(
1264 struct domain_device *d_device,
1267 return TMF_RESP_FUNC_FAILED;
1272 * isci_task_query_task() - This function is implemented to cause libsas to
1273 * correctly escalate the failed abort to a LUN or target reset (this is
1274 * because sas_scsi_find_task libsas function does not correctly interpret
1275 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1276 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1277 * returned, libsas will turn this into a target reset
1278 * @task: This parameter specifies the sas task being queried.
1279 * @lun: This parameter specifies the lun associated with this request.
1281 * status, zero indicates success.
1283 int isci_task_query_task(
1284 struct sas_task *task)
1286 /* See if there is a pending device reset for this device. */
1287 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1288 return TMF_RESP_FUNC_FAILED;
1290 return TMF_RESP_FUNC_SUCC;
1294 * isci_task_request_complete() - This function is called by the sci core when
1295 * an task request completes.
1296 * @ihost: This parameter specifies the ISCI host object
1297 * @ireq: This parameter is the completed isci_request object.
1298 * @completion_status: This parameter specifies the completion status from the
1304 isci_task_request_complete(struct isci_host *ihost,
1305 struct isci_request *ireq,
1306 enum sci_task_status completion_status)
1308 struct isci_remote_device *idev = ireq->isci_device;
1309 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
1310 struct completion *tmf_complete;
1311 struct scic_sds_request *sci_req = &ireq->sci;
1313 dev_dbg(&ihost->pdev->dev,
1314 "%s: request = %p, status=%d\n",
1315 __func__, ireq, completion_status);
1317 isci_request_change_state(ireq, completed);
1319 tmf->status = completion_status;
1320 ireq->complete_in_target = true;
1322 if (tmf->proto == SAS_PROTOCOL_SSP) {
1323 memcpy(&tmf->resp.resp_iu,
1325 SSP_RESP_IU_MAX_SIZE);
1326 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
1327 memcpy(&tmf->resp.d2h_fis,
1329 sizeof(struct dev_to_host_fis));
1332 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1333 tmf_complete = tmf->complete;
1335 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1336 /* set the 'terminated' flag handle to make sure it cannot be terminated
1337 * or completed again.
1339 ireq->terminated = true;;
1341 isci_request_change_state(ireq, unallocated);
1342 list_del_init(&ireq->dev_node);
1344 /* The task management part completes last. */
1345 complete(tmf_complete);
1348 static void isci_smp_task_timedout(unsigned long _task)
1350 struct sas_task *task = (void *) _task;
1351 unsigned long flags;
1353 spin_lock_irqsave(&task->task_state_lock, flags);
1354 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1355 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1356 spin_unlock_irqrestore(&task->task_state_lock, flags);
1358 complete(&task->completion);
1361 static void isci_smp_task_done(struct sas_task *task)
1363 if (!del_timer(&task->timer))
1365 complete(&task->completion);
1368 static struct sas_task *isci_alloc_task(void)
1370 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1373 INIT_LIST_HEAD(&task->list);
1374 spin_lock_init(&task->task_state_lock);
1375 task->task_state_flags = SAS_TASK_STATE_PENDING;
1376 init_timer(&task->timer);
1377 init_completion(&task->completion);
1383 static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1386 BUG_ON(!list_empty(&task->list));
1391 static int isci_smp_execute_task(struct isci_host *ihost,
1392 struct domain_device *dev, void *req,
1393 int req_size, void *resp, int resp_size)
1396 struct sas_task *task = NULL;
1398 for (retry = 0; retry < 3; retry++) {
1399 task = isci_alloc_task();
1404 task->task_proto = dev->tproto;
1405 sg_init_one(&task->smp_task.smp_req, req, req_size);
1406 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1408 task->task_done = isci_smp_task_done;
1410 task->timer.data = (unsigned long) task;
1411 task->timer.function = isci_smp_task_timedout;
1412 task->timer.expires = jiffies + 10*HZ;
1413 add_timer(&task->timer);
1415 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1418 del_timer(&task->timer);
1419 dev_err(&ihost->pdev->dev,
1420 "%s: executing SMP task failed:%d\n",
1425 wait_for_completion(&task->completion);
1427 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1428 dev_err(&ihost->pdev->dev,
1429 "%s: smp task timed out or aborted\n",
1431 isci_task_abort_task(task);
1432 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1433 dev_err(&ihost->pdev->dev,
1434 "%s: SMP task aborted and not done\n",
1439 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1440 task->task_status.stat == SAM_STAT_GOOD) {
1444 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1445 task->task_status.stat == SAS_DATA_UNDERRUN) {
1446 /* no error, but return the number of bytes of
1448 res = task->task_status.residual;
1451 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1452 task->task_status.stat == SAS_DATA_OVERRUN) {
1456 dev_err(&ihost->pdev->dev,
1457 "%s: task to dev %016llx response: 0x%x "
1458 "status 0x%x\n", __func__,
1459 SAS_ADDR(dev->sas_addr),
1460 task->task_status.resp,
1461 task->task_status.stat);
1462 isci_free_task(ihost, task);
1467 BUG_ON(retry == 3 && task != NULL);
1468 isci_free_task(ihost, task);
1472 #define DISCOVER_REQ_SIZE 16
1473 #define DISCOVER_RESP_SIZE 56
1475 int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1476 struct domain_device *dev,
1477 int phy_id, int *adt)
1479 struct smp_resp *disc_resp;
1483 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1487 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1489 disc_req[0] = SMP_REQUEST;
1490 disc_req[1] = SMP_DISCOVER;
1491 disc_req[9] = phy_id;
1496 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1497 disc_resp, DISCOVER_RESP_SIZE);
1499 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1500 res = disc_resp->result;
1502 *adt = disc_resp->disc.attached_dev_type;
1510 static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1512 struct domain_device *dev = idev->domain_dev;
1513 struct isci_port *iport = idev->isci_port;
1514 struct isci_host *ihost = iport->isci_host;
1515 int res, iteration = 0, attached_device_type;
1516 #define STP_WAIT_MSECS 25000
1517 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1518 unsigned long deadline = jiffies + tmo;
1520 SMP_PHYWAIT_PHYDOWN,
1523 } phy_state = SMP_PHYWAIT_PHYDOWN;
1525 /* While there is time, wait for the phy to go away and come back */
1526 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1527 int event = atomic_read(&iport->event);
1531 tmo = wait_event_timeout(ihost->eventq,
1532 event != atomic_read(&iport->event) ||
1533 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1535 /* link down, stop polling */
1536 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1539 dev_dbg(&ihost->pdev->dev,
1540 "%s: iport %p, iteration %d,"
1541 " phase %d: time_remaining %lu, bcns = %d\n",
1542 __func__, iport, iteration, phy_state,
1543 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1545 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1546 &attached_device_type);
1547 tmo = deadline - jiffies;
1550 dev_warn(&ihost->pdev->dev,
1551 "%s: iteration %d, phase %d:"
1552 " SMP error=%d, time_remaining=%lu\n",
1553 __func__, iteration, phy_state, res, tmo);
1556 dev_dbg(&ihost->pdev->dev,
1557 "%s: iport %p, iteration %d,"
1558 " phase %d: time_remaining %lu, bcns = %d, "
1559 "attdevtype = %x\n",
1560 __func__, iport, iteration, phy_state,
1561 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1562 attached_device_type);
1564 switch (phy_state) {
1565 case SMP_PHYWAIT_PHYDOWN:
1566 /* Has the device gone away? */
1567 if (!attached_device_type)
1568 phy_state = SMP_PHYWAIT_PHYUP;
1572 case SMP_PHYWAIT_PHYUP:
1573 /* Has the device come back? */
1574 if (attached_device_type)
1575 phy_state = SMP_PHYWAIT_DONE;
1578 case SMP_PHYWAIT_DONE:
1583 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1586 static int isci_reset_device(struct domain_device *dev, int hard_reset)
1588 struct isci_remote_device *idev = dev->lldd_dev;
1589 struct sas_phy *phy = sas_find_local_phy(dev);
1590 struct isci_host *ihost = dev_to_ihost(dev);
1591 struct isci_port *iport = idev->isci_port;
1592 enum sci_status status;
1593 unsigned long flags;
1596 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
1599 dev_warn(&ihost->pdev->dev,
1600 "%s: idev is GONE!\n",
1603 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1606 spin_lock_irqsave(&ihost->scic_lock, flags);
1607 status = scic_remote_device_reset(&idev->sci);
1608 if (status != SCI_SUCCESS) {
1609 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1611 dev_warn(&ihost->pdev->dev,
1612 "%s: scic_remote_device_reset(%p) returned %d!\n",
1613 __func__, idev, status);
1615 return TMF_RESP_FUNC_FAILED;
1617 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1619 /* Make sure all pending requests are able to be fully terminated. */
1620 isci_device_clear_reset_pending(ihost, idev);
1622 /* If this is a device on an expander, disable BCN processing. */
1623 if (!scsi_is_sas_phy_local(phy))
1624 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1626 rc = sas_phy_reset(phy, hard_reset);
1628 /* Terminate in-progress I/O now. */
1629 isci_remote_device_nuke_requests(ihost, idev);
1631 /* Since all pending TCs have been cleaned, resume the RNC. */
1632 spin_lock_irqsave(&ihost->scic_lock, flags);
1633 status = scic_remote_device_reset_complete(&idev->sci);
1634 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1636 /* If this is a device on an expander, bring the phy back up. */
1637 if (!scsi_is_sas_phy_local(phy)) {
1638 /* A phy reset will cause the device to go away then reappear.
1639 * Since libsas will take action on incoming BCNs (eg. remove
1640 * a device going through an SMP phy-control driven reset),
1641 * we need to wait until the phy comes back up before letting
1642 * discovery proceed in libsas.
1644 isci_wait_for_smp_phy_reset(idev, phy->number);
1646 spin_lock_irqsave(&ihost->scic_lock, flags);
1647 isci_port_bcn_enable(ihost, idev->isci_port);
1648 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1651 if (status != SCI_SUCCESS) {
1652 dev_warn(&ihost->pdev->dev,
1653 "%s: scic_remote_device_reset_complete(%p) "
1654 "returned %d!\n", __func__, idev, status);
1657 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
1662 int isci_task_I_T_nexus_reset(struct domain_device *dev)
1664 struct isci_host *ihost = dev_to_ihost(dev);
1665 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1666 struct isci_remote_device *idev;
1667 unsigned long flags;
1669 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1670 * being too paranoid, or is mvsas busted?!
1672 spin_lock_irqsave(&ihost->scic_lock, flags);
1673 idev = dev->lldd_dev;
1674 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1675 ret = TMF_RESP_FUNC_COMPLETE;
1676 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1678 if (ret == TMF_RESP_FUNC_COMPLETE)
1681 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1684 return isci_reset_device(dev, hard_reset);
1687 int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1689 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1692 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1695 return isci_reset_device(dev, hard_reset);