xref: /openbmc/linux/drivers/scsi/isci/task.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
16f231ddaSDan Williams /*
26f231ddaSDan Williams  * This file is provided under a dual BSD/GPLv2 license.  When using or
36f231ddaSDan Williams  * redistributing this file, you may do so under either license.
46f231ddaSDan Williams  *
56f231ddaSDan Williams  * GPL LICENSE SUMMARY
66f231ddaSDan Williams  *
76f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
86f231ddaSDan Williams  *
96f231ddaSDan Williams  * This program is free software; you can redistribute it and/or modify
106f231ddaSDan Williams  * it under the terms of version 2 of the GNU General Public License as
116f231ddaSDan Williams  * published by the Free Software Foundation.
126f231ddaSDan Williams  *
136f231ddaSDan Williams  * This program is distributed in the hope that it will be useful, but
146f231ddaSDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
156f231ddaSDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
166f231ddaSDan Williams  * General Public License for more details.
176f231ddaSDan Williams  *
186f231ddaSDan Williams  * You should have received a copy of the GNU General Public License
196f231ddaSDan Williams  * along with this program; if not, write to the Free Software
206f231ddaSDan Williams  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
216f231ddaSDan Williams  * The full GNU General Public License is included in this distribution
226f231ddaSDan Williams  * in the file called LICENSE.GPL.
236f231ddaSDan Williams  *
246f231ddaSDan Williams  * BSD LICENSE
256f231ddaSDan Williams  *
266f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
276f231ddaSDan Williams  * All rights reserved.
286f231ddaSDan Williams  *
296f231ddaSDan Williams  * Redistribution and use in source and binary forms, with or without
306f231ddaSDan Williams  * modification, are permitted provided that the following conditions
316f231ddaSDan Williams  * are met:
326f231ddaSDan Williams  *
336f231ddaSDan Williams  *   * Redistributions of source code must retain the above copyright
346f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer.
356f231ddaSDan Williams  *   * Redistributions in binary form must reproduce the above copyright
366f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer in
376f231ddaSDan Williams  *     the documentation and/or other materials provided with the
386f231ddaSDan Williams  *     distribution.
396f231ddaSDan Williams  *   * Neither the name of Intel Corporation nor the names of its
406f231ddaSDan Williams  *     contributors may be used to endorse or promote products derived
416f231ddaSDan Williams  *     from this software without specific prior written permission.
426f231ddaSDan Williams  *
436f231ddaSDan Williams  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
446f231ddaSDan Williams  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
456f231ddaSDan Williams  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
466f231ddaSDan Williams  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
476f231ddaSDan Williams  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
486f231ddaSDan Williams  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
496f231ddaSDan Williams  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
506f231ddaSDan Williams  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
516f231ddaSDan Williams  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
526f231ddaSDan Williams  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
536f231ddaSDan Williams  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
546f231ddaSDan Williams  */
556f231ddaSDan Williams 
566f231ddaSDan Williams #include <linux/completion.h>
5750e7f9b5SDan Williams #include <linux/irqflags.h>
58af5ae893SDave Jiang #include "sas.h"
5961aaff49SJeff Skirvin #include <scsi/libsas.h>
6088f3b62aSDan Williams #include "remote_device.h"
6188f3b62aSDan Williams #include "remote_node_context.h"
626f231ddaSDan Williams #include "isci.h"
636f231ddaSDan Williams #include "request.h"
646f231ddaSDan Williams #include "task.h"
65312e0c24SDan Williams #include "host.h"
66af5ae893SDave Jiang 
6750e7f9b5SDan Williams /**
681077a574SDan Williams * isci_task_refuse() - complete the request to the upper layer driver in
691077a574SDan Williams *     the case where an I/O needs to be completed back in the submit path.
701077a574SDan Williams * @ihost: host on which the the request was queued
711077a574SDan Williams * @task: request to complete
721077a574SDan Williams * @response: response code for the completed task.
731077a574SDan Williams * @status: status code for the completed task.
7450e7f9b5SDan Williams *
7550e7f9b5SDan Williams */
isci_task_refuse(struct isci_host * ihost,struct sas_task * task,enum service_response response,enum exec_status status)761077a574SDan Williams static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
7750e7f9b5SDan Williams 			     enum service_response response,
781077a574SDan Williams 			     enum exec_status status)
7950e7f9b5SDan Williams 
801077a574SDan Williams {
8114aaa9f0SJeff Skirvin 	unsigned long flags;
821077a574SDan Williams 
8350e7f9b5SDan Williams 	/* Normal notification (task_done) */
8414aaa9f0SJeff Skirvin 	dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
8550e7f9b5SDan Williams 		__func__, task, response, status);
8650e7f9b5SDan Williams 
8714aaa9f0SJeff Skirvin 	spin_lock_irqsave(&task->task_state_lock, flags);
8814aaa9f0SJeff Skirvin 
8914aaa9f0SJeff Skirvin 	task->task_status.resp = response;
9014aaa9f0SJeff Skirvin 	task->task_status.stat = status;
9114aaa9f0SJeff Skirvin 
9214aaa9f0SJeff Skirvin 	/* Normal notification (task_done) */
9314aaa9f0SJeff Skirvin 	task->task_state_flags |= SAS_TASK_STATE_DONE;
94*26fc0ea7SJohn Garry 	task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
951077a574SDan Williams 	task->lldd_task = NULL;
9614aaa9f0SJeff Skirvin 	spin_unlock_irqrestore(&task->task_state_lock, flags);
9714aaa9f0SJeff Skirvin 
98312d3e56SDan Williams 	task->task_done(task);
9950e7f9b5SDan Williams }
1006f231ddaSDan Williams 
1011077a574SDan Williams #define for_each_sas_task(num, task) \
1021077a574SDan Williams 	for (; num > 0; num--,\
1031077a574SDan Williams 	     task = list_entry(task->list.next, struct sas_task, list))
1041077a574SDan Williams 
1059274f45eSJeff Skirvin 
isci_device_io_ready(struct isci_remote_device * idev,struct sas_task * task)1069274f45eSJeff Skirvin static inline int isci_device_io_ready(struct isci_remote_device *idev,
1079274f45eSJeff Skirvin 				       struct sas_task *task)
1089274f45eSJeff Skirvin {
1099274f45eSJeff Skirvin 	return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
1109274f45eSJeff Skirvin 		      (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
1119274f45eSJeff Skirvin 		       isci_task_is_ncq_recovery(task))
1129274f45eSJeff Skirvin 		    : 0;
1139274f45eSJeff Skirvin }
1146f231ddaSDan Williams /**
1156f231ddaSDan Williams  * isci_task_execute_task() - This function is one of the SAS Domain Template
1166f231ddaSDan Williams  *    functions. This function is called by libsas to send a task down to
1176f231ddaSDan Williams  *    hardware.
1186f231ddaSDan Williams  * @task: This parameter specifies the SAS task to send.
1196f231ddaSDan Williams  * @gfp_flags: This parameter specifies the context of this call.
1206f231ddaSDan Williams  *
1216f231ddaSDan Williams  * status, zero indicates success.
1226f231ddaSDan Williams  */
isci_task_execute_task(struct sas_task * task,gfp_t gfp_flags)12379855d17SChristoph Hellwig int isci_task_execute_task(struct sas_task *task, gfp_t gfp_flags)
1246f231ddaSDan Williams {
1254393aa4eSDan Williams 	struct isci_host *ihost = dev_to_ihost(task->dev);
126209fae14SDan Williams 	struct isci_remote_device *idev;
127f2088267SDan Williams 	unsigned long flags;
12879855d17SChristoph Hellwig 	enum sci_status status = SCI_FAILURE;
129f2088267SDan Williams 	bool io_ready;
130312e0c24SDan Williams 	u16 tag;
1316f231ddaSDan Williams 
132209fae14SDan Williams 	spin_lock_irqsave(&ihost->scic_lock, flags);
133209fae14SDan Williams 	idev = isci_lookup_device(task->dev);
1349274f45eSJeff Skirvin 	io_ready = isci_device_io_ready(idev, task);
135312e0c24SDan Williams 	tag = isci_alloc_tag(ihost);
136209fae14SDan Williams 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1376f231ddaSDan Williams 
138d2d61433SDave Jiang 	dev_dbg(&ihost->pdev->dev,
13979855d17SChristoph Hellwig 		"task: %p, dev: %p idev: %p:%#lx cmd = %p\n",
14079855d17SChristoph Hellwig 		task, task->dev, idev, idev ? idev->flags : 0,
141f2088267SDan Williams 		task->uldd_task);
1426f231ddaSDan Williams 
143f2088267SDan Williams 	if (!idev) {
144f2088267SDan Williams 		isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
145f2088267SDan Williams 				 SAS_DEVICE_UNKNOWN);
146312e0c24SDan Williams 	} else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1476f231ddaSDan Williams 		/* Indicate QUEUE_FULL so that the scsi midlayer
1486f231ddaSDan Williams 		 * retries.
1496f231ddaSDan Williams 		  */
150f2088267SDan Williams 		isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
1511077a574SDan Williams 				 SAS_QUEUE_FULL);
152f0846c68SJeff Skirvin 	} else {
153f0846c68SJeff Skirvin 		/* There is a device and it's ready for I/O. */
154f0846c68SJeff Skirvin 		spin_lock_irqsave(&task->task_state_lock, flags);
155f0846c68SJeff Skirvin 
156f0846c68SJeff Skirvin 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
157f2088267SDan Williams 			/* The I/O was aborted. */
15879855d17SChristoph Hellwig 			spin_unlock_irqrestore(&task->task_state_lock, flags);
159f0846c68SJeff Skirvin 
1601077a574SDan Williams 			isci_task_refuse(ihost, task,
161f0846c68SJeff Skirvin 					 SAS_TASK_UNDELIVERED,
162d377f415SBart Van Assche 					 SAS_SAM_STAT_TASK_ABORTED);
1636f231ddaSDan Williams 		} else {
164c39d5aa4SJohn Garry 			struct isci_request *ireq;
165c39d5aa4SJohn Garry 
166c39d5aa4SJohn Garry 			/* do common allocation and init of request object. */
167c39d5aa4SJohn Garry 			ireq = isci_io_request_from_tag(ihost, task, tag);
168f0846c68SJeff Skirvin 			spin_unlock_irqrestore(&task->task_state_lock, flags);
169f0846c68SJeff Skirvin 
1706f231ddaSDan Williams 			/* build and send the request. */
171c39d5aa4SJohn Garry 			/* do common allocation and init of request object. */
172c39d5aa4SJohn Garry 			status = isci_request_execute(ihost, idev, task, ireq);
1736f231ddaSDan Williams 
174f0846c68SJeff Skirvin 			if (status != SCI_SUCCESS) {
175c2cb8a5fSJeff Skirvin 				if (test_bit(IDEV_GONE, &idev->flags)) {
176c2cb8a5fSJeff Skirvin 					/* Indicate that the device
177c2cb8a5fSJeff Skirvin 					 * is gone.
178c2cb8a5fSJeff Skirvin 					 */
179c2cb8a5fSJeff Skirvin 					isci_task_refuse(ihost, task,
180c2cb8a5fSJeff Skirvin 						SAS_TASK_UNDELIVERED,
181c2cb8a5fSJeff Skirvin 						SAS_DEVICE_UNKNOWN);
182c2cb8a5fSJeff Skirvin 				} else {
183c2cb8a5fSJeff Skirvin 					/* Indicate QUEUE_FULL so that
184c2cb8a5fSJeff Skirvin 					 * the scsi midlayer retries.
185c2cb8a5fSJeff Skirvin 					 * If the request failed for
186c2cb8a5fSJeff Skirvin 					 * remote device reasons, it
187c2cb8a5fSJeff Skirvin 					 * gets returned as
188c2cb8a5fSJeff Skirvin 					 * SAS_TASK_UNDELIVERED next
189c2cb8a5fSJeff Skirvin 					 * time through.
1906f231ddaSDan Williams 					 */
1911077a574SDan Williams 					isci_task_refuse(ihost, task,
1926f231ddaSDan Williams 						SAS_TASK_COMPLETE,
1931077a574SDan Williams 						SAS_QUEUE_FULL);
1946f231ddaSDan Williams 				}
1956f231ddaSDan Williams 			}
196f0846c68SJeff Skirvin 		}
197c2cb8a5fSJeff Skirvin 	}
19879855d17SChristoph Hellwig 
199312e0c24SDan Williams 	if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
200312e0c24SDan Williams 		spin_lock_irqsave(&ihost->scic_lock, flags);
201312e0c24SDan Williams 		/* command never hit the device, so just free
202312e0c24SDan Williams 		 * the tci and skip the sequence increment
203312e0c24SDan Williams 		 */
204312e0c24SDan Williams 		isci_tci_free(ihost, ISCI_TAG_TCI(tag));
205312e0c24SDan Williams 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
206312e0c24SDan Williams 	}
20779855d17SChristoph Hellwig 
208209fae14SDan Williams 	isci_put_device(idev);
2096f231ddaSDan Williams 	return 0;
2106f231ddaSDan Williams }
2116f231ddaSDan Williams 
isci_task_request_build(struct isci_host * ihost,struct isci_remote_device * idev,u16 tag,struct isci_tmf * isci_tmf)2120d0cf14cSDan Williams static struct isci_request *isci_task_request_build(struct isci_host *ihost,
213209fae14SDan Williams 						    struct isci_remote_device *idev,
214312e0c24SDan Williams 						    u16 tag, struct isci_tmf *isci_tmf)
2156f231ddaSDan Williams {
2166f231ddaSDan Williams 	enum sci_status status = SCI_FAILURE;
2170d0cf14cSDan Williams 	struct isci_request *ireq = NULL;
218a1a113b0SDan Williams 	struct domain_device *dev;
2196f231ddaSDan Williams 
2200d0cf14cSDan Williams 	dev_dbg(&ihost->pdev->dev,
2216f231ddaSDan Williams 		"%s: isci_tmf = %p\n", __func__, isci_tmf);
2226f231ddaSDan Williams 
2230d0cf14cSDan Williams 	dev = idev->domain_dev;
2246f231ddaSDan Williams 
2256f231ddaSDan Williams 	/* do common allocation and init of request object. */
226db056250SDan Williams 	ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
2270d0cf14cSDan Williams 	if (!ireq)
2280d0cf14cSDan Williams 		return NULL;
2296f231ddaSDan Williams 
2306f231ddaSDan Williams 	/* let the core do it's construct. */
23189a7301fSDan Williams 	status = sci_task_request_construct(ihost, idev, tag,
2325076a1a9SDan Williams 					     ireq);
2336f231ddaSDan Williams 
2346f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2350d0cf14cSDan Williams 		dev_warn(&ihost->pdev->dev,
23689a7301fSDan Williams 			 "%s: sci_task_request_construct failed - "
2376f231ddaSDan Williams 			 "status = 0x%x\n",
2386f231ddaSDan Williams 			 __func__,
2396f231ddaSDan Williams 			 status);
240db056250SDan Williams 		return NULL;
2416f231ddaSDan Williams 	}
2426f231ddaSDan Williams 
243a1a113b0SDan Williams 	/* XXX convert to get this from task->tproto like other drivers */
244aa9f8328SJames Bottomley 	if (dev->dev_type == SAS_END_DEVICE) {
2456f231ddaSDan Williams 		isci_tmf->proto = SAS_PROTOCOL_SSP;
24689a7301fSDan Williams 		status = sci_task_request_construct_ssp(ireq);
2476f231ddaSDan Williams 		if (status != SCI_SUCCESS)
248db056250SDan Williams 			return NULL;
2496f231ddaSDan Williams 	}
2506f231ddaSDan Williams 
2510d0cf14cSDan Williams 	return ireq;
2526f231ddaSDan Williams }
2536f231ddaSDan Williams 
isci_task_execute_tmf(struct isci_host * ihost,struct isci_remote_device * idev,struct isci_tmf * tmf,unsigned long timeout_ms)25416ba7709SDan Williams static int isci_task_execute_tmf(struct isci_host *ihost,
25578a6f06eSDan Williams 				 struct isci_remote_device *idev,
256209fae14SDan Williams 				 struct isci_tmf *tmf, unsigned long timeout_ms)
2576f231ddaSDan Williams {
2586f231ddaSDan Williams 	DECLARE_COMPLETION_ONSTACK(completion);
259362b5da3SNathan Chancellor 	enum sci_status status = SCI_FAILURE;
2600d0cf14cSDan Williams 	struct isci_request *ireq;
2616f231ddaSDan Williams 	int ret = TMF_RESP_FUNC_FAILED;
2626f231ddaSDan Williams 	unsigned long flags;
263fd18388bSEdmund Nadolski 	unsigned long timeleft;
264312e0c24SDan Williams 	u16 tag;
265312e0c24SDan Williams 
266312e0c24SDan Williams 	spin_lock_irqsave(&ihost->scic_lock, flags);
267312e0c24SDan Williams 	tag = isci_alloc_tag(ihost);
268312e0c24SDan Williams 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
269312e0c24SDan Williams 
270312e0c24SDan Williams 	if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
271312e0c24SDan Williams 		return ret;
2726f231ddaSDan Williams 
2736f231ddaSDan Williams 	/* sanity check, return TMF_RESP_FUNC_FAILED
2746f231ddaSDan Williams 	 * if the device is not there and ready.
2756f231ddaSDan Williams 	 */
27678a6f06eSDan Williams 	if (!idev ||
27778a6f06eSDan Williams 	    (!test_bit(IDEV_IO_READY, &idev->flags) &&
27878a6f06eSDan Williams 	     !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
2790d0cf14cSDan Williams 		dev_dbg(&ihost->pdev->dev,
28078a6f06eSDan Williams 			"%s: idev = %p not ready (%#lx)\n",
2816f231ddaSDan Williams 			__func__,
28278a6f06eSDan Williams 			idev, idev ? idev->flags : 0);
283312e0c24SDan Williams 		goto err_tci;
2846f231ddaSDan Williams 	} else
2850d0cf14cSDan Williams 		dev_dbg(&ihost->pdev->dev,
28678a6f06eSDan Williams 			"%s: idev = %p\n",
28778a6f06eSDan Williams 			__func__, idev);
2886f231ddaSDan Williams 
2896f231ddaSDan Williams 	/* Assign the pointer to the TMF's completion kernel wait structure. */
2906f231ddaSDan Williams 	tmf->complete = &completion;
291b343dff1SJeff Skirvin 	tmf->status = SCI_FAILURE_TIMEOUT;
2926f231ddaSDan Williams 
29378a6f06eSDan Williams 	ireq = isci_task_request_build(ihost, idev, tag, tmf);
294312e0c24SDan Williams 	if (!ireq)
295312e0c24SDan Williams 		goto err_tci;
2966f231ddaSDan Williams 
2970d0cf14cSDan Williams 	spin_lock_irqsave(&ihost->scic_lock, flags);
2986f231ddaSDan Williams 
2996f231ddaSDan Williams 	/* start the TMF io. */
30089a7301fSDan Williams 	status = sci_controller_start_task(ihost, idev, ireq);
3016f231ddaSDan Williams 
302362b5da3SNathan Chancellor 	if (status != SCI_SUCCESS) {
303a8a0a133SDan Williams 		dev_dbg(&ihost->pdev->dev,
3046f231ddaSDan Williams 			 "%s: start_io failed - status = 0x%x, request = %p\n",
3056f231ddaSDan Williams 			 __func__,
3066f231ddaSDan Williams 			 status,
3070d0cf14cSDan Williams 			 ireq);
3080d0cf14cSDan Williams 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
309db056250SDan Williams 		goto err_tci;
3106f231ddaSDan Williams 	}
3110d0cf14cSDan Williams 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
3126f231ddaSDan Williams 
31331a38ef0SJeff Skirvin 	/* The RNC must be unsuspended before the TMF can get a response. */
31431a38ef0SJeff Skirvin 	isci_remote_device_resume_from_abort(ihost, idev);
31531a38ef0SJeff Skirvin 
3166f231ddaSDan Williams 	/* Wait for the TMF to complete, or a timeout. */
317fd18388bSEdmund Nadolski 	timeleft = wait_for_completion_timeout(&completion,
318086a0dabSDan Williams 					       msecs_to_jiffies(timeout_ms));
319fd18388bSEdmund Nadolski 
320fd18388bSEdmund Nadolski 	if (timeleft == 0) {
321b343dff1SJeff Skirvin 		/* The TMF did not complete - this could be because
322b343dff1SJeff Skirvin 		 * of an unplug.  Terminate the TMF request now.
323b343dff1SJeff Skirvin 		 */
32414aaa9f0SJeff Skirvin 		isci_remote_device_suspend_terminate(ihost, idev, ireq);
325fd18388bSEdmund Nadolski 	}
3266f231ddaSDan Williams 
32727234ab4SMaciej Trela 	isci_print_tmf(ihost, tmf);
3286f231ddaSDan Williams 
3296f231ddaSDan Williams 	if (tmf->status == SCI_SUCCESS)
3306f231ddaSDan Williams 		ret =  TMF_RESP_FUNC_COMPLETE;
3316f231ddaSDan Williams 	else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
3320d0cf14cSDan Williams 		dev_dbg(&ihost->pdev->dev,
3336f231ddaSDan Williams 			"%s: tmf.status == "
3346f231ddaSDan Williams 			"SCI_FAILURE_IO_RESPONSE_VALID\n",
3356f231ddaSDan Williams 			__func__);
3366f231ddaSDan Williams 		ret =  TMF_RESP_FUNC_COMPLETE;
3376f231ddaSDan Williams 	}
3386f231ddaSDan Williams 	/* Else - leave the default "failed" status alone. */
3396f231ddaSDan Williams 
3400d0cf14cSDan Williams 	dev_dbg(&ihost->pdev->dev,
3416f231ddaSDan Williams 		"%s: completed request = %p\n",
3426f231ddaSDan Williams 		__func__,
3430d0cf14cSDan Williams 		ireq);
3446f231ddaSDan Williams 
3456f231ddaSDan Williams 	return ret;
346312e0c24SDan Williams 
347312e0c24SDan Williams  err_tci:
348312e0c24SDan Williams 	spin_lock_irqsave(&ihost->scic_lock, flags);
349312e0c24SDan Williams 	isci_tci_free(ihost, ISCI_TAG_TCI(tag));
350312e0c24SDan Williams 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
351312e0c24SDan Williams 
352312e0c24SDan Williams 	return ret;
3536f231ddaSDan Williams }
3546f231ddaSDan Williams 
isci_task_build_tmf(struct isci_tmf * tmf,enum isci_tmf_function_codes code)35516ba7709SDan Williams static void isci_task_build_tmf(struct isci_tmf *tmf,
35614aaa9f0SJeff Skirvin 				enum isci_tmf_function_codes code)
3576f231ddaSDan Williams {
3586f231ddaSDan Williams 	memset(tmf, 0, sizeof(*tmf));
3596f231ddaSDan Williams 	tmf->tmf_code = code;
360c3f42febSJeff Skirvin }
3611fad9e93SJeff Skirvin 
isci_task_build_abort_task_tmf(struct isci_tmf * tmf,enum isci_tmf_function_codes code,struct isci_request * old_request)36216ba7709SDan Williams static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
363c3f42febSJeff Skirvin 					   enum isci_tmf_function_codes code,
364c3f42febSJeff Skirvin 					   struct isci_request *old_request)
365c3f42febSJeff Skirvin {
36614aaa9f0SJeff Skirvin 	isci_task_build_tmf(tmf, code);
367c3f42febSJeff Skirvin 	tmf->io_tag = old_request->io_tag;
3686f231ddaSDan Williams }
3696f231ddaSDan Williams 
3700afdee03SLee Jones /*
3716f231ddaSDan Williams  * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
3726f231ddaSDan Williams  *    Template functions.
3736f231ddaSDan Williams  * @lun: This parameter specifies the lun to be reset.
3746f231ddaSDan Williams  *
3756f231ddaSDan Williams  * status, zero indicates success.
3766f231ddaSDan Williams  */
isci_task_send_lu_reset_sas(struct isci_host * isci_host,struct isci_remote_device * isci_device,u8 * lun)3776f231ddaSDan Williams static int isci_task_send_lu_reset_sas(
3786f231ddaSDan Williams 	struct isci_host *isci_host,
3796f231ddaSDan Williams 	struct isci_remote_device *isci_device,
3806f231ddaSDan Williams 	u8 *lun)
3816f231ddaSDan Williams {
3826f231ddaSDan Williams 	struct isci_tmf tmf;
3836f231ddaSDan Williams 	int ret = TMF_RESP_FUNC_FAILED;
3846f231ddaSDan Williams 
3856f231ddaSDan Williams 	dev_dbg(&isci_host->pdev->dev,
3866f231ddaSDan Williams 		"%s: isci_host = %p, isci_device = %p\n",
3876f231ddaSDan Williams 		__func__, isci_host, isci_device);
3886f231ddaSDan Williams 	/* Send the LUN reset to the target.  By the time the call returns,
3896f231ddaSDan Williams 	 * the TMF has fully exected in the target (in which case the return
3906f231ddaSDan Williams 	 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
3916f231ddaSDan Williams 	 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
3926f231ddaSDan Williams 	 */
39314aaa9f0SJeff Skirvin 	isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
3946f231ddaSDan Williams 
3956f231ddaSDan Williams 	#define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
396209fae14SDan Williams 	ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
3976f231ddaSDan Williams 
3986f231ddaSDan Williams 	if (ret == TMF_RESP_FUNC_COMPLETE)
3996f231ddaSDan Williams 		dev_dbg(&isci_host->pdev->dev,
4006f231ddaSDan Williams 			"%s: %p: TMF_LU_RESET passed\n",
4016f231ddaSDan Williams 			__func__, isci_device);
4026f231ddaSDan Williams 	else
4036f231ddaSDan Williams 		dev_dbg(&isci_host->pdev->dev,
4046f231ddaSDan Williams 			"%s: %p: TMF_LU_RESET failed (%x)\n",
4056f231ddaSDan Williams 			__func__, isci_device, ret);
4066f231ddaSDan Williams 
4076f231ddaSDan Williams 	return ret;
4086f231ddaSDan Williams }
4096f231ddaSDan Williams 
isci_task_lu_reset(struct domain_device * dev,u8 * lun)41043a5ab15SDan Williams int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
41116ba7709SDan Williams {
4125b6bf225SJeff Skirvin 	struct isci_host *ihost = dev_to_ihost(dev);
41314aaa9f0SJeff Skirvin 	struct isci_remote_device *idev;
414209fae14SDan Williams 	unsigned long flags;
415397497ddSJeff Skirvin 	int ret = TMF_RESP_FUNC_COMPLETE;
4166f231ddaSDan Williams 
4175b6bf225SJeff Skirvin 	spin_lock_irqsave(&ihost->scic_lock, flags);
41863732502SJeff Skirvin 	idev = isci_get_device(dev->lldd_dev);
4195b6bf225SJeff Skirvin 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
4206f231ddaSDan Williams 
4215b6bf225SJeff Skirvin 	dev_dbg(&ihost->pdev->dev,
4224393aa4eSDan Williams 		"%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
42314aaa9f0SJeff Skirvin 		__func__, dev, ihost, idev);
4246f231ddaSDan Williams 
42514aaa9f0SJeff Skirvin 	if (!idev) {
426d80ecd57SJeff Skirvin 		/* If the device is gone, escalate to I_T_Nexus_Reset. */
4275b6bf225SJeff Skirvin 		dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
4286f231ddaSDan Williams 
429d80ecd57SJeff Skirvin 		ret = TMF_RESP_FUNC_FAILED;
430209fae14SDan Williams 		goto out;
4316f231ddaSDan Williams 	}
4326f231ddaSDan Williams 
43308c031e4SJeff Skirvin 	/* Suspend the RNC, kill all TCs */
43408c031e4SJeff Skirvin 	if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
43508c031e4SJeff Skirvin 	    != SCI_SUCCESS) {
43608c031e4SJeff Skirvin 		/* The suspend/terminate only fails if isci_get_device fails */
43708c031e4SJeff Skirvin 		ret = TMF_RESP_FUNC_FAILED;
43808c031e4SJeff Skirvin 		goto out;
43908c031e4SJeff Skirvin 	}
44008c031e4SJeff Skirvin 	/* All pending I/Os have been terminated and cleaned up. */
441397497ddSJeff Skirvin 	if (!test_bit(IDEV_GONE, &idev->flags)) {
442397497ddSJeff Skirvin 		if (dev_is_sata(dev))
44343a5ab15SDan Williams 			sas_ata_schedule_reset(dev);
444397497ddSJeff Skirvin 		else
44514aaa9f0SJeff Skirvin 			/* Send the task management part of the reset. */
44614aaa9f0SJeff Skirvin 			ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
4475b6bf225SJeff Skirvin 	}
448209fae14SDan Williams  out:
44914aaa9f0SJeff Skirvin 	isci_put_device(idev);
4506f231ddaSDan Williams 	return ret;
4516f231ddaSDan Williams }
4526f231ddaSDan Williams 
4536f231ddaSDan Williams 
4546f231ddaSDan Williams /*	 int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
isci_task_clear_nexus_port(struct asd_sas_port * port)4556f231ddaSDan Williams int isci_task_clear_nexus_port(struct asd_sas_port *port)
4566f231ddaSDan Williams {
4576f231ddaSDan Williams 	return TMF_RESP_FUNC_FAILED;
4586f231ddaSDan Williams }
4596f231ddaSDan Williams 
4606f231ddaSDan Williams 
4616f231ddaSDan Williams 
isci_task_clear_nexus_ha(struct sas_ha_struct * ha)4626f231ddaSDan Williams int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
4636f231ddaSDan Williams {
4646f231ddaSDan Williams 	return TMF_RESP_FUNC_FAILED;
4656f231ddaSDan Williams }
4666f231ddaSDan Williams 
4676f231ddaSDan Williams /* Task Management Functions. Must be called from process context.	 */
4686f231ddaSDan Williams 
4696f231ddaSDan Williams /**
4706f231ddaSDan Williams  * isci_task_abort_task() - This function is one of the SAS Domain Template
4716f231ddaSDan Williams  *    functions. This function is called by libsas to abort a specified task.
4726f231ddaSDan Williams  * @task: This parameter specifies the SAS task to abort.
4736f231ddaSDan Williams  *
4746f231ddaSDan Williams  * status, zero indicates success.
4756f231ddaSDan Williams  */
isci_task_abort_task(struct sas_task * task)4766f231ddaSDan Williams int isci_task_abort_task(struct sas_task *task)
4776f231ddaSDan Williams {
47814aaa9f0SJeff Skirvin 	struct isci_host *ihost = dev_to_ihost(task->dev);
4796f231ddaSDan Williams 	DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
4806f231ddaSDan Williams 	struct isci_request       *old_request = NULL;
48114aaa9f0SJeff Skirvin 	struct isci_remote_device *idev = NULL;
4826f231ddaSDan Williams 	struct isci_tmf           tmf;
4836f231ddaSDan Williams 	int                       ret = TMF_RESP_FUNC_FAILED;
4846f231ddaSDan Williams 	unsigned long             flags;
48596f15f29SJeff Skirvin 	int                       target_done_already = 0;
4866f231ddaSDan Williams 
4876f231ddaSDan Williams 	/* Get the isci_request reference from the task.  Note that
4886f231ddaSDan Williams 	 * this check does not depend on the pending request list
4896f231ddaSDan Williams 	 * in the device, because tasks driving resets may land here
4906f231ddaSDan Williams 	 * after completion in the core.
4916f231ddaSDan Williams 	 */
49214aaa9f0SJeff Skirvin 	spin_lock_irqsave(&ihost->scic_lock, flags);
493209fae14SDan Williams 	spin_lock(&task->task_state_lock);
494209fae14SDan Williams 
495209fae14SDan Williams 	old_request = task->lldd_task;
496209fae14SDan Williams 
497209fae14SDan Williams 	/* If task is already done, the request isn't valid */
498209fae14SDan Williams 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
49996f15f29SJeff Skirvin 	    old_request) {
50063732502SJeff Skirvin 		idev = isci_get_device(task->dev->lldd_dev);
50196f15f29SJeff Skirvin 		target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
50296f15f29SJeff Skirvin 					       &old_request->flags);
50396f15f29SJeff Skirvin 	}
504209fae14SDan Williams 	spin_unlock(&task->task_state_lock);
50514aaa9f0SJeff Skirvin 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
5066f231ddaSDan Williams 
50714aaa9f0SJeff Skirvin 	dev_warn(&ihost->pdev->dev,
508397497ddSJeff Skirvin 		 "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
509397497ddSJeff Skirvin 		 __func__, idev,
510397497ddSJeff Skirvin 		 (dev_is_sata(task->dev) ? "STP/SATA"
511924a3541SJohn Garry 					 : ((dev_is_expander(task->dev->dev_type))
512397497ddSJeff Skirvin 						? "SMP"
513397497ddSJeff Skirvin 						: "SSP")),
514397497ddSJeff Skirvin 		 ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
515397497ddSJeff Skirvin 			   ? " IDEV_GONE"
516397497ddSJeff Skirvin 			   : "")
517397497ddSJeff Skirvin 			 : " <NULL>"),
518397497ddSJeff Skirvin 		 task, old_request);
5196f231ddaSDan Williams 
52098145cb7SJeff Skirvin 	/* Device reset conditions signalled in task_state_flags are the
52198145cb7SJeff Skirvin 	 * responsbility of libsas to observe at the start of the error
52298145cb7SJeff Skirvin 	 * handler thread.
5236f231ddaSDan Williams 	 */
52414aaa9f0SJeff Skirvin 	if (!idev || !old_request) {
5256f231ddaSDan Williams 		/* The request has already completed and there
5266f231ddaSDan Williams 		* is nothing to do here other than to set the task
5276f231ddaSDan Williams 		* done bit, and indicate that the task abort function
5284907cb7bSAnatol Pomozov 		* was successful.
5296f231ddaSDan Williams 		*/
53098145cb7SJeff Skirvin 		spin_lock_irqsave(&task->task_state_lock, flags);
53198145cb7SJeff Skirvin 		task->task_state_flags |= SAS_TASK_STATE_DONE;
532*26fc0ea7SJohn Garry 		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
5336f231ddaSDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
5346f231ddaSDan Williams 
535a5fde225SJeff Skirvin 		ret = TMF_RESP_FUNC_COMPLETE;
536a5fde225SJeff Skirvin 
53714aaa9f0SJeff Skirvin 		dev_warn(&ihost->pdev->dev,
538a5fde225SJeff Skirvin 			 "%s: abort task not needed for %p\n",
539a5fde225SJeff Skirvin 			 __func__, task);
540209fae14SDan Williams 		goto out;
541a5ec7f86SJames Bottomley 	}
54214aaa9f0SJeff Skirvin 	/* Suspend the RNC, kill the TC */
54314aaa9f0SJeff Skirvin 	if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
54414aaa9f0SJeff Skirvin 	    != SCI_SUCCESS) {
54514aaa9f0SJeff Skirvin 		dev_warn(&ihost->pdev->dev,
54614aaa9f0SJeff Skirvin 			 "%s: isci_remote_device_reset_terminate(dev=%p, "
54714aaa9f0SJeff Skirvin 				 "req=%p, task=%p) failed\n",
54814aaa9f0SJeff Skirvin 			 __func__, idev, old_request, task);
54914aaa9f0SJeff Skirvin 		ret = TMF_RESP_FUNC_FAILED;
550209fae14SDan Williams 		goto out;
5516f231ddaSDan Williams 	}
55214aaa9f0SJeff Skirvin 	spin_lock_irqsave(&ihost->scic_lock, flags);
55314aaa9f0SJeff Skirvin 
55438d8879bSDan Williams 	if (task->task_proto == SAS_PROTOCOL_SMP ||
55598145cb7SJeff Skirvin 	    sas_protocol_ata(task->task_proto) ||
55696f15f29SJeff Skirvin 	    target_done_already ||
557397497ddSJeff Skirvin 	    test_bit(IDEV_GONE, &idev->flags)) {
5586f231ddaSDan Williams 
55914aaa9f0SJeff Skirvin 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
56014aaa9f0SJeff Skirvin 
56131a38ef0SJeff Skirvin 		/* No task to send, so explicitly resume the device here */
56231a38ef0SJeff Skirvin 		isci_remote_device_resume_from_abort(ihost, idev);
56331a38ef0SJeff Skirvin 
56414aaa9f0SJeff Skirvin 		dev_warn(&ihost->pdev->dev,
56598145cb7SJeff Skirvin 			 "%s: %s request"
566397497ddSJeff Skirvin 				 " or complete_in_target (%d), "
567397497ddSJeff Skirvin 				 "or IDEV_GONE (%d), thus no TMF\n",
56898145cb7SJeff Skirvin 			 __func__,
56998145cb7SJeff Skirvin 			 ((task->task_proto == SAS_PROTOCOL_SMP)
57098145cb7SJeff Skirvin 			  ? "SMP"
57198145cb7SJeff Skirvin 			  : (sas_protocol_ata(task->task_proto)
57298145cb7SJeff Skirvin 				? "SATA/STP"
57398145cb7SJeff Skirvin 				: "<other>")
57498145cb7SJeff Skirvin 			  ),
57514aaa9f0SJeff Skirvin 			 test_bit(IREQ_COMPLETE_IN_TARGET,
576397497ddSJeff Skirvin 				  &old_request->flags),
577397497ddSJeff Skirvin 			 test_bit(IDEV_GONE, &idev->flags));
578a5fde225SJeff Skirvin 
57998145cb7SJeff Skirvin 		spin_lock_irqsave(&task->task_state_lock, flags);
580*26fc0ea7SJohn Garry 		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
58198145cb7SJeff Skirvin 		task->task_state_flags |= SAS_TASK_STATE_DONE;
58298145cb7SJeff Skirvin 		spin_unlock_irqrestore(&task->task_state_lock, flags);
58314aaa9f0SJeff Skirvin 
5846f231ddaSDan Williams 		ret = TMF_RESP_FUNC_COMPLETE;
58598145cb7SJeff Skirvin 	} else {
5864e210519SGeert Uytterhoeven 		/* Fill in the tmf structure */
587209fae14SDan Williams 		isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
588c3f42febSJeff Skirvin 					       old_request);
5896f231ddaSDan Williams 
59014aaa9f0SJeff Skirvin 		spin_unlock_irqrestore(&ihost->scic_lock, flags);
5916f231ddaSDan Williams 
59214aaa9f0SJeff Skirvin 		/* Send the task management request. */
59398145cb7SJeff Skirvin 		#define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
59414aaa9f0SJeff Skirvin 		ret = isci_task_execute_tmf(ihost, idev, &tmf,
5956f231ddaSDan Williams 					    ISCI_ABORT_TASK_TIMEOUT_MS);
596a5fde225SJeff Skirvin 	}
597209fae14SDan Williams out:
59863732502SJeff Skirvin 	dev_warn(&ihost->pdev->dev,
59963732502SJeff Skirvin 		 "%s: Done; dev = %p, task = %p , old_request == %p\n",
60063732502SJeff Skirvin 		 __func__, idev, task, old_request);
60114aaa9f0SJeff Skirvin 	isci_put_device(idev);
6026f231ddaSDan Williams 	return ret;
6036f231ddaSDan Williams }
6046f231ddaSDan Williams 
6056f231ddaSDan Williams /**
6066f231ddaSDan Williams  * isci_task_abort_task_set() - This function is one of the SAS Domain Template
6076f231ddaSDan Williams  *    functions. This is one of the Task Management functoins called by libsas,
6086f231ddaSDan Williams  *    to abort all task for the given lun.
6096f231ddaSDan Williams  * @d_device: This parameter specifies the domain device associated with this
6106f231ddaSDan Williams  *    request.
6116f231ddaSDan Williams  * @lun: This parameter specifies the lun associated with this request.
6126f231ddaSDan Williams  *
6136f231ddaSDan Williams  * status, zero indicates success.
6146f231ddaSDan Williams  */
isci_task_abort_task_set(struct domain_device * d_device,u8 * lun)6156f231ddaSDan Williams int isci_task_abort_task_set(
6166f231ddaSDan Williams 	struct domain_device *d_device,
6176f231ddaSDan Williams 	u8 *lun)
6186f231ddaSDan Williams {
6196f231ddaSDan Williams 	return TMF_RESP_FUNC_FAILED;
6206f231ddaSDan Williams }
6216f231ddaSDan Williams 
6226f231ddaSDan Williams 
6236f231ddaSDan Williams /**
6246f231ddaSDan Williams  * isci_task_clear_task_set() - This function is one of the SAS Domain Template
6256f231ddaSDan Williams  *    functions. This is one of the Task Management functoins called by libsas.
6266f231ddaSDan Williams  * @d_device: This parameter specifies the domain device associated with this
6276f231ddaSDan Williams  *    request.
6286f231ddaSDan Williams  * @lun: This parameter specifies the lun	 associated with this request.
6296f231ddaSDan Williams  *
6306f231ddaSDan Williams  * status, zero indicates success.
6316f231ddaSDan Williams  */
isci_task_clear_task_set(struct domain_device * d_device,u8 * lun)6326f231ddaSDan Williams int isci_task_clear_task_set(
6336f231ddaSDan Williams 	struct domain_device *d_device,
6346f231ddaSDan Williams 	u8 *lun)
6356f231ddaSDan Williams {
6366f231ddaSDan Williams 	return TMF_RESP_FUNC_FAILED;
6376f231ddaSDan Williams }
6386f231ddaSDan Williams 
6396f231ddaSDan Williams 
6406f231ddaSDan Williams /**
6416f231ddaSDan Williams  * isci_task_query_task() - This function is implemented to cause libsas to
6426f231ddaSDan Williams  *    correctly escalate the failed abort to a LUN or target reset (this is
6436f231ddaSDan Williams  *    because sas_scsi_find_task libsas function does not correctly interpret
6446f231ddaSDan Williams  *    all return codes from the abort task call).  When TMF_RESP_FUNC_SUCC is
6456f231ddaSDan Williams  *    returned, libsas turns this into a LUN reset; when FUNC_FAILED is
6466f231ddaSDan Williams  *    returned, libsas will turn this into a target reset
6476f231ddaSDan Williams  * @task: This parameter specifies the sas task being queried.
6486f231ddaSDan Williams  *
6496f231ddaSDan Williams  * status, zero indicates success.
6506f231ddaSDan Williams  */
isci_task_query_task(struct sas_task * task)6516f231ddaSDan Williams int isci_task_query_task(
6526f231ddaSDan Williams 	struct sas_task *task)
6536f231ddaSDan Williams {
6546f231ddaSDan Williams 	/* See if there is a pending device reset for this device. */
6556f231ddaSDan Williams 	if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
6566f231ddaSDan Williams 		return TMF_RESP_FUNC_FAILED;
6576f231ddaSDan Williams 	else
6586f231ddaSDan Williams 		return TMF_RESP_FUNC_SUCC;
6596f231ddaSDan Williams }
6606f231ddaSDan Williams 
661af5ae893SDave Jiang /*
6626f231ddaSDan Williams  * isci_task_request_complete() - This function is called by the sci core when
6636f231ddaSDan Williams  *    an task request completes.
664af5ae893SDave Jiang  * @ihost: This parameter specifies the ISCI host object
665af5ae893SDave Jiang  * @ireq: This parameter is the completed isci_request object.
6666f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6676f231ddaSDan Williams  *    sci core.
6686f231ddaSDan Williams  *
6696f231ddaSDan Williams  * none.
6706f231ddaSDan Williams  */
671af5ae893SDave Jiang void
isci_task_request_complete(struct isci_host * ihost,struct isci_request * ireq,enum sci_task_status completion_status)672af5ae893SDave Jiang isci_task_request_complete(struct isci_host *ihost,
673af5ae893SDave Jiang 			   struct isci_request *ireq,
6746f231ddaSDan Williams 			   enum sci_task_status completion_status)
6756f231ddaSDan Williams {
676af5ae893SDave Jiang 	struct isci_tmf *tmf = isci_request_access_tmf(ireq);
677b343dff1SJeff Skirvin 	struct completion *tmf_complete = NULL;
6786f231ddaSDan Williams 
679af5ae893SDave Jiang 	dev_dbg(&ihost->pdev->dev,
6806f231ddaSDan Williams 		"%s: request = %p, status=%d\n",
681af5ae893SDave Jiang 		__func__, ireq, completion_status);
6826f231ddaSDan Williams 
68338d8879bSDan Williams 	set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
6846f231ddaSDan Williams 
685b343dff1SJeff Skirvin 	if (tmf) {
686b343dff1SJeff Skirvin 		tmf->status = completion_status;
687b343dff1SJeff Skirvin 
688af5ae893SDave Jiang 		if (tmf->proto == SAS_PROTOCOL_SSP) {
6895250db63SKees Cook 			memcpy(tmf->resp.rsp_buf,
6905250db63SKees Cook 			       ireq->ssp.rsp_buf,
691af5ae893SDave Jiang 			       SSP_RESP_IU_MAX_SIZE);
692af5ae893SDave Jiang 		} else if (tmf->proto == SAS_PROTOCOL_SATA) {
6936f231ddaSDan Williams 			memcpy(&tmf->resp.d2h_fis,
6945076a1a9SDan Williams 			       &ireq->stp.rsp,
695f2f30080SDave Jiang 			       sizeof(struct dev_to_host_fis));
6966f231ddaSDan Williams 		}
6976f231ddaSDan Williams 		/* PRINT_TMF( ((struct isci_tmf *)request->task)); */
6986f231ddaSDan Williams 		tmf_complete = tmf->complete;
699b343dff1SJeff Skirvin 	}
70089a7301fSDan Williams 	sci_controller_complete_io(ihost, ireq->target_device, ireq);
70167ea838dSDan Williams 	/* set the 'terminated' flag handle to make sure it cannot be terminated
7026f231ddaSDan Williams 	 *  or completed again.
7036f231ddaSDan Williams 	 */
70438d8879bSDan Williams 	set_bit(IREQ_TERMINATED, &ireq->flags);
7056f231ddaSDan Williams 
7061db79b3eSJeff Skirvin 	if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
7071db79b3eSJeff Skirvin 		wake_up_all(&ihost->eventq);
7081db79b3eSJeff Skirvin 
709621120caSJeff Skirvin 	if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
710d6891682SJeff Skirvin 		isci_free_tag(ihost, ireq->io_tag);
711b343dff1SJeff Skirvin 
7126f231ddaSDan Williams 	/* The task management part completes last. */
713b343dff1SJeff Skirvin 	if (tmf_complete)
7146f231ddaSDan Williams 		complete(tmf_complete);
7156f231ddaSDan Williams }
7166f231ddaSDan Williams 
isci_reset_device(struct isci_host * ihost,struct domain_device * dev,struct isci_remote_device * idev)717209fae14SDan Williams static int isci_reset_device(struct isci_host *ihost,
71892776991SDan Williams 			     struct domain_device *dev,
719bc6f387dSJeff Skirvin 			     struct isci_remote_device *idev)
7206f231ddaSDan Williams {
721397497ddSJeff Skirvin 	int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
72292776991SDan Williams 	struct sas_phy *phy = sas_get_local_phy(dev);
72392776991SDan Williams 	struct isci_port *iport = dev->port->lldd_port;
7246f231ddaSDan Williams 
725d06b487bSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
7266f231ddaSDan Williams 
72714aaa9f0SJeff Skirvin 	/* Suspend the RNC, terminate all outstanding TCs. */
72814aaa9f0SJeff Skirvin 	if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
72914aaa9f0SJeff Skirvin 	    != SCI_SUCCESS) {
730f41a0c44SDan Williams 		rc = TMF_RESP_FUNC_FAILED;
731f41a0c44SDan Williams 		goto out;
7326f231ddaSDan Williams 	}
73314aaa9f0SJeff Skirvin 	/* Note that since the termination for outstanding requests succeeded,
73414aaa9f0SJeff Skirvin 	 * this function will return success.  This is because the resets will
73514aaa9f0SJeff Skirvin 	 * only fail if the device has been removed (ie. hotplug), and the
73614aaa9f0SJeff Skirvin 	 * primary duty of this function is to cleanup tasks, so that is the
73714aaa9f0SJeff Skirvin 	 * relevant status.
73814aaa9f0SJeff Skirvin 	 */
739397497ddSJeff Skirvin 	if (!test_bit(IDEV_GONE, &idev->flags)) {
74092776991SDan Williams 		if (scsi_is_sas_phy_local(phy)) {
74192776991SDan Williams 			struct isci_phy *iphy = &ihost->phys[phy->number];
74292776991SDan Williams 
743397497ddSJeff Skirvin 			reset_stat = isci_port_perform_hard_reset(ihost, iport,
744397497ddSJeff Skirvin 								  iphy);
74592776991SDan Williams 		} else
74614aaa9f0SJeff Skirvin 			reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
747397497ddSJeff Skirvin 	}
74814aaa9f0SJeff Skirvin 	/* Explicitly resume the RNC here, since there was no task sent. */
74931a38ef0SJeff Skirvin 	isci_remote_device_resume_from_abort(ihost, idev);
7506f231ddaSDan Williams 
75114aaa9f0SJeff Skirvin 	dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
75214aaa9f0SJeff Skirvin 		__func__, idev, reset_stat);
753f41a0c44SDan Williams  out:
754f41a0c44SDan Williams 	sas_put_local_phy(phy);
755d06b487bSDan Williams 	return rc;
756d06b487bSDan Williams }
757d06b487bSDan Williams 
isci_task_I_T_nexus_reset(struct domain_device * dev)758d06b487bSDan Williams int isci_task_I_T_nexus_reset(struct domain_device *dev)
759d06b487bSDan Williams {
760d06b487bSDan Williams 	struct isci_host *ihost = dev_to_ihost(dev);
761d06b487bSDan Williams 	struct isci_remote_device *idev;
762d06b487bSDan Williams 	unsigned long flags;
763bc6f387dSJeff Skirvin 	int ret;
764d06b487bSDan Williams 
765d06b487bSDan Williams 	spin_lock_irqsave(&ihost->scic_lock, flags);
76614aaa9f0SJeff Skirvin 	idev = isci_get_device(dev->lldd_dev);
767d06b487bSDan Williams 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
768d06b487bSDan Williams 
7695a998328SDan Williams 	if (!idev) {
7705a998328SDan Williams 		/* XXX: need to cleanup any ireqs targeting this
7715a998328SDan Williams 		 * domain_device
7725a998328SDan Williams 		 */
773ddfadd77SDan Williams 		ret = -ENODEV;
774209fae14SDan Williams 		goto out;
775209fae14SDan Williams 	}
776d06b487bSDan Williams 
77792776991SDan Williams 	ret = isci_reset_device(ihost, dev, idev);
778209fae14SDan Williams  out:
779209fae14SDan Williams 	isci_put_device(idev);
780209fae14SDan Williams 	return ret;
781d06b487bSDan Williams }
782