162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
362306a36Sopenharmony_ci * redistributing this file, you may do so under either license.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * GPL LICENSE SUMMARY
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
1062306a36Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
1162306a36Sopenharmony_ci * published by the Free Software Foundation.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
1462306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
1562306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1662306a36Sopenharmony_ci * General Public License for more details.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
1962306a36Sopenharmony_ci * along with this program; if not, write to the Free Software
2062306a36Sopenharmony_ci * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
2162306a36Sopenharmony_ci * The full GNU General Public License is included in this distribution
2262306a36Sopenharmony_ci * in the file called LICENSE.GPL.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * BSD LICENSE
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
2762306a36Sopenharmony_ci * All rights reserved.
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
3062306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
3162306a36Sopenharmony_ci * are met:
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci *   * Redistributions of source code must retain the above copyright
3462306a36Sopenharmony_ci *     notice, this list of conditions and the following disclaimer.
3562306a36Sopenharmony_ci *   * Redistributions in binary form must reproduce the above copyright
3662306a36Sopenharmony_ci *     notice, this list of conditions and the following disclaimer in
3762306a36Sopenharmony_ci *     the documentation and/or other materials provided with the
3862306a36Sopenharmony_ci *     distribution.
3962306a36Sopenharmony_ci *   * Neither the name of Intel Corporation nor the names of its
4062306a36Sopenharmony_ci *     contributors may be used to endorse or promote products derived
4162306a36Sopenharmony_ci *     from this software without specific prior written permission.
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4462306a36Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4562306a36Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4662306a36Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4762306a36Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4862306a36Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4962306a36Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5062306a36Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5162306a36Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5262306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5362306a36Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#include <linux/completion.h>
5762306a36Sopenharmony_ci#include <linux/irqflags.h>
5862306a36Sopenharmony_ci#include "sas.h"
5962306a36Sopenharmony_ci#include <scsi/libsas.h>
6062306a36Sopenharmony_ci#include "remote_device.h"
6162306a36Sopenharmony_ci#include "remote_node_context.h"
6262306a36Sopenharmony_ci#include "isci.h"
6362306a36Sopenharmony_ci#include "request.h"
6462306a36Sopenharmony_ci#include "task.h"
6562306a36Sopenharmony_ci#include "host.h"
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/**
6862306a36Sopenharmony_ci* isci_task_refuse() - complete the request to the upper layer driver in
6962306a36Sopenharmony_ci*     the case where an I/O needs to be completed back in the submit path.
7062306a36Sopenharmony_ci* @ihost: host on which the the request was queued
7162306a36Sopenharmony_ci* @task: request to complete
7262306a36Sopenharmony_ci* @response: response code for the completed task.
7362306a36Sopenharmony_ci* @status: status code for the completed task.
7462306a36Sopenharmony_ci*
7562306a36Sopenharmony_ci*/
7662306a36Sopenharmony_cistatic void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
7762306a36Sopenharmony_ci			     enum service_response response,
7862306a36Sopenharmony_ci			     enum exec_status status)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	unsigned long flags;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	/* Normal notification (task_done) */
8462306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
8562306a36Sopenharmony_ci		__func__, task, response, status);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	spin_lock_irqsave(&task->task_state_lock, flags);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	task->task_status.resp = response;
9062306a36Sopenharmony_ci	task->task_status.stat = status;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/* Normal notification (task_done) */
9362306a36Sopenharmony_ci	task->task_state_flags |= SAS_TASK_STATE_DONE;
9462306a36Sopenharmony_ci	task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
9562306a36Sopenharmony_ci	task->lldd_task = NULL;
9662306a36Sopenharmony_ci	spin_unlock_irqrestore(&task->task_state_lock, flags);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	task->task_done(task);
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define for_each_sas_task(num, task) \
10262306a36Sopenharmony_ci	for (; num > 0; num--,\
10362306a36Sopenharmony_ci	     task = list_entry(task->list.next, struct sas_task, list))
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic inline int isci_device_io_ready(struct isci_remote_device *idev,
10762306a36Sopenharmony_ci				       struct sas_task *task)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
11062306a36Sopenharmony_ci		      (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
11162306a36Sopenharmony_ci		       isci_task_is_ncq_recovery(task))
11262306a36Sopenharmony_ci		    : 0;
11362306a36Sopenharmony_ci}
11462306a36Sopenharmony_ci/**
11562306a36Sopenharmony_ci * isci_task_execute_task() - This function is one of the SAS Domain Template
11662306a36Sopenharmony_ci *    functions. This function is called by libsas to send a task down to
11762306a36Sopenharmony_ci *    hardware.
11862306a36Sopenharmony_ci * @task: This parameter specifies the SAS task to send.
11962306a36Sopenharmony_ci * @gfp_flags: This parameter specifies the context of this call.
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci * status, zero indicates success.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_ciint isci_task_execute_task(struct sas_task *task, gfp_t gfp_flags)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	struct isci_host *ihost = dev_to_ihost(task->dev);
12662306a36Sopenharmony_ci	struct isci_remote_device *idev;
12762306a36Sopenharmony_ci	unsigned long flags;
12862306a36Sopenharmony_ci	enum sci_status status = SCI_FAILURE;
12962306a36Sopenharmony_ci	bool io_ready;
13062306a36Sopenharmony_ci	u16 tag;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
13362306a36Sopenharmony_ci	idev = isci_lookup_device(task->dev);
13462306a36Sopenharmony_ci	io_ready = isci_device_io_ready(idev, task);
13562306a36Sopenharmony_ci	tag = isci_alloc_tag(ihost);
13662306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev,
13962306a36Sopenharmony_ci		"task: %p, dev: %p idev: %p:%#lx cmd = %p\n",
14062306a36Sopenharmony_ci		task, task->dev, idev, idev ? idev->flags : 0,
14162306a36Sopenharmony_ci		task->uldd_task);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	if (!idev) {
14462306a36Sopenharmony_ci		isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
14562306a36Sopenharmony_ci				 SAS_DEVICE_UNKNOWN);
14662306a36Sopenharmony_ci	} else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
14762306a36Sopenharmony_ci		/* Indicate QUEUE_FULL so that the scsi midlayer
14862306a36Sopenharmony_ci		 * retries.
14962306a36Sopenharmony_ci		  */
15062306a36Sopenharmony_ci		isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
15162306a36Sopenharmony_ci				 SAS_QUEUE_FULL);
15262306a36Sopenharmony_ci	} else {
15362306a36Sopenharmony_ci		/* There is a device and it's ready for I/O. */
15462306a36Sopenharmony_ci		spin_lock_irqsave(&task->task_state_lock, flags);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
15762306a36Sopenharmony_ci			/* The I/O was aborted. */
15862306a36Sopenharmony_ci			spin_unlock_irqrestore(&task->task_state_lock, flags);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci			isci_task_refuse(ihost, task,
16162306a36Sopenharmony_ci					 SAS_TASK_UNDELIVERED,
16262306a36Sopenharmony_ci					 SAS_SAM_STAT_TASK_ABORTED);
16362306a36Sopenharmony_ci		} else {
16462306a36Sopenharmony_ci			struct isci_request *ireq;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci			/* do common allocation and init of request object. */
16762306a36Sopenharmony_ci			ireq = isci_io_request_from_tag(ihost, task, tag);
16862306a36Sopenharmony_ci			spin_unlock_irqrestore(&task->task_state_lock, flags);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci			/* build and send the request. */
17162306a36Sopenharmony_ci			/* do common allocation and init of request object. */
17262306a36Sopenharmony_ci			status = isci_request_execute(ihost, idev, task, ireq);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci			if (status != SCI_SUCCESS) {
17562306a36Sopenharmony_ci				if (test_bit(IDEV_GONE, &idev->flags)) {
17662306a36Sopenharmony_ci					/* Indicate that the device
17762306a36Sopenharmony_ci					 * is gone.
17862306a36Sopenharmony_ci					 */
17962306a36Sopenharmony_ci					isci_task_refuse(ihost, task,
18062306a36Sopenharmony_ci						SAS_TASK_UNDELIVERED,
18162306a36Sopenharmony_ci						SAS_DEVICE_UNKNOWN);
18262306a36Sopenharmony_ci				} else {
18362306a36Sopenharmony_ci					/* Indicate QUEUE_FULL so that
18462306a36Sopenharmony_ci					 * the scsi midlayer retries.
18562306a36Sopenharmony_ci					 * If the request failed for
18662306a36Sopenharmony_ci					 * remote device reasons, it
18762306a36Sopenharmony_ci					 * gets returned as
18862306a36Sopenharmony_ci					 * SAS_TASK_UNDELIVERED next
18962306a36Sopenharmony_ci					 * time through.
19062306a36Sopenharmony_ci					 */
19162306a36Sopenharmony_ci					isci_task_refuse(ihost, task,
19262306a36Sopenharmony_ci						SAS_TASK_COMPLETE,
19362306a36Sopenharmony_ci						SAS_QUEUE_FULL);
19462306a36Sopenharmony_ci				}
19562306a36Sopenharmony_ci			}
19662306a36Sopenharmony_ci		}
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
20062306a36Sopenharmony_ci		spin_lock_irqsave(&ihost->scic_lock, flags);
20162306a36Sopenharmony_ci		/* command never hit the device, so just free
20262306a36Sopenharmony_ci		 * the tci and skip the sequence increment
20362306a36Sopenharmony_ci		 */
20462306a36Sopenharmony_ci		isci_tci_free(ihost, ISCI_TAG_TCI(tag));
20562306a36Sopenharmony_ci		spin_unlock_irqrestore(&ihost->scic_lock, flags);
20662306a36Sopenharmony_ci	}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	isci_put_device(idev);
20962306a36Sopenharmony_ci	return 0;
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic struct isci_request *isci_task_request_build(struct isci_host *ihost,
21362306a36Sopenharmony_ci						    struct isci_remote_device *idev,
21462306a36Sopenharmony_ci						    u16 tag, struct isci_tmf *isci_tmf)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	enum sci_status status = SCI_FAILURE;
21762306a36Sopenharmony_ci	struct isci_request *ireq = NULL;
21862306a36Sopenharmony_ci	struct domain_device *dev;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev,
22162306a36Sopenharmony_ci		"%s: isci_tmf = %p\n", __func__, isci_tmf);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	dev = idev->domain_dev;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* do common allocation and init of request object. */
22662306a36Sopenharmony_ci	ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
22762306a36Sopenharmony_ci	if (!ireq)
22862306a36Sopenharmony_ci		return NULL;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	/* let the core do it's construct. */
23162306a36Sopenharmony_ci	status = sci_task_request_construct(ihost, idev, tag,
23262306a36Sopenharmony_ci					     ireq);
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	if (status != SCI_SUCCESS) {
23562306a36Sopenharmony_ci		dev_warn(&ihost->pdev->dev,
23662306a36Sopenharmony_ci			 "%s: sci_task_request_construct failed - "
23762306a36Sopenharmony_ci			 "status = 0x%x\n",
23862306a36Sopenharmony_ci			 __func__,
23962306a36Sopenharmony_ci			 status);
24062306a36Sopenharmony_ci		return NULL;
24162306a36Sopenharmony_ci	}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	/* XXX convert to get this from task->tproto like other drivers */
24462306a36Sopenharmony_ci	if (dev->dev_type == SAS_END_DEVICE) {
24562306a36Sopenharmony_ci		isci_tmf->proto = SAS_PROTOCOL_SSP;
24662306a36Sopenharmony_ci		status = sci_task_request_construct_ssp(ireq);
24762306a36Sopenharmony_ci		if (status != SCI_SUCCESS)
24862306a36Sopenharmony_ci			return NULL;
24962306a36Sopenharmony_ci	}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	return ireq;
25262306a36Sopenharmony_ci}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_cistatic int isci_task_execute_tmf(struct isci_host *ihost,
25562306a36Sopenharmony_ci				 struct isci_remote_device *idev,
25662306a36Sopenharmony_ci				 struct isci_tmf *tmf, unsigned long timeout_ms)
25762306a36Sopenharmony_ci{
25862306a36Sopenharmony_ci	DECLARE_COMPLETION_ONSTACK(completion);
25962306a36Sopenharmony_ci	enum sci_status status = SCI_FAILURE;
26062306a36Sopenharmony_ci	struct isci_request *ireq;
26162306a36Sopenharmony_ci	int ret = TMF_RESP_FUNC_FAILED;
26262306a36Sopenharmony_ci	unsigned long flags;
26362306a36Sopenharmony_ci	unsigned long timeleft;
26462306a36Sopenharmony_ci	u16 tag;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
26762306a36Sopenharmony_ci	tag = isci_alloc_tag(ihost);
26862306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
27162306a36Sopenharmony_ci		return ret;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* sanity check, return TMF_RESP_FUNC_FAILED
27462306a36Sopenharmony_ci	 * if the device is not there and ready.
27562306a36Sopenharmony_ci	 */
27662306a36Sopenharmony_ci	if (!idev ||
27762306a36Sopenharmony_ci	    (!test_bit(IDEV_IO_READY, &idev->flags) &&
27862306a36Sopenharmony_ci	     !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
27962306a36Sopenharmony_ci		dev_dbg(&ihost->pdev->dev,
28062306a36Sopenharmony_ci			"%s: idev = %p not ready (%#lx)\n",
28162306a36Sopenharmony_ci			__func__,
28262306a36Sopenharmony_ci			idev, idev ? idev->flags : 0);
28362306a36Sopenharmony_ci		goto err_tci;
28462306a36Sopenharmony_ci	} else
28562306a36Sopenharmony_ci		dev_dbg(&ihost->pdev->dev,
28662306a36Sopenharmony_ci			"%s: idev = %p\n",
28762306a36Sopenharmony_ci			__func__, idev);
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	/* Assign the pointer to the TMF's completion kernel wait structure. */
29062306a36Sopenharmony_ci	tmf->complete = &completion;
29162306a36Sopenharmony_ci	tmf->status = SCI_FAILURE_TIMEOUT;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	ireq = isci_task_request_build(ihost, idev, tag, tmf);
29462306a36Sopenharmony_ci	if (!ireq)
29562306a36Sopenharmony_ci		goto err_tci;
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	/* start the TMF io. */
30062306a36Sopenharmony_ci	status = sci_controller_start_task(ihost, idev, ireq);
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	if (status != SCI_SUCCESS) {
30362306a36Sopenharmony_ci		dev_dbg(&ihost->pdev->dev,
30462306a36Sopenharmony_ci			 "%s: start_io failed - status = 0x%x, request = %p\n",
30562306a36Sopenharmony_ci			 __func__,
30662306a36Sopenharmony_ci			 status,
30762306a36Sopenharmony_ci			 ireq);
30862306a36Sopenharmony_ci		spin_unlock_irqrestore(&ihost->scic_lock, flags);
30962306a36Sopenharmony_ci		goto err_tci;
31062306a36Sopenharmony_ci	}
31162306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	/* The RNC must be unsuspended before the TMF can get a response. */
31462306a36Sopenharmony_ci	isci_remote_device_resume_from_abort(ihost, idev);
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	/* Wait for the TMF to complete, or a timeout. */
31762306a36Sopenharmony_ci	timeleft = wait_for_completion_timeout(&completion,
31862306a36Sopenharmony_ci					       msecs_to_jiffies(timeout_ms));
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	if (timeleft == 0) {
32162306a36Sopenharmony_ci		/* The TMF did not complete - this could be because
32262306a36Sopenharmony_ci		 * of an unplug.  Terminate the TMF request now.
32362306a36Sopenharmony_ci		 */
32462306a36Sopenharmony_ci		isci_remote_device_suspend_terminate(ihost, idev, ireq);
32562306a36Sopenharmony_ci	}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	isci_print_tmf(ihost, tmf);
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	if (tmf->status == SCI_SUCCESS)
33062306a36Sopenharmony_ci		ret =  TMF_RESP_FUNC_COMPLETE;
33162306a36Sopenharmony_ci	else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
33262306a36Sopenharmony_ci		dev_dbg(&ihost->pdev->dev,
33362306a36Sopenharmony_ci			"%s: tmf.status == "
33462306a36Sopenharmony_ci			"SCI_FAILURE_IO_RESPONSE_VALID\n",
33562306a36Sopenharmony_ci			__func__);
33662306a36Sopenharmony_ci		ret =  TMF_RESP_FUNC_COMPLETE;
33762306a36Sopenharmony_ci	}
33862306a36Sopenharmony_ci	/* Else - leave the default "failed" status alone. */
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev,
34162306a36Sopenharmony_ci		"%s: completed request = %p\n",
34262306a36Sopenharmony_ci		__func__,
34362306a36Sopenharmony_ci		ireq);
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	return ret;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci err_tci:
34862306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
34962306a36Sopenharmony_ci	isci_tci_free(ihost, ISCI_TAG_TCI(tag));
35062306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	return ret;
35362306a36Sopenharmony_ci}
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_cistatic void isci_task_build_tmf(struct isci_tmf *tmf,
35662306a36Sopenharmony_ci				enum isci_tmf_function_codes code)
35762306a36Sopenharmony_ci{
35862306a36Sopenharmony_ci	memset(tmf, 0, sizeof(*tmf));
35962306a36Sopenharmony_ci	tmf->tmf_code = code;
36062306a36Sopenharmony_ci}
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_cistatic void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
36362306a36Sopenharmony_ci					   enum isci_tmf_function_codes code,
36462306a36Sopenharmony_ci					   struct isci_request *old_request)
36562306a36Sopenharmony_ci{
36662306a36Sopenharmony_ci	isci_task_build_tmf(tmf, code);
36762306a36Sopenharmony_ci	tmf->io_tag = old_request->io_tag;
36862306a36Sopenharmony_ci}
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci/*
37162306a36Sopenharmony_ci * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
37262306a36Sopenharmony_ci *    Template functions.
37362306a36Sopenharmony_ci * @lun: This parameter specifies the lun to be reset.
37462306a36Sopenharmony_ci *
37562306a36Sopenharmony_ci * status, zero indicates success.
37662306a36Sopenharmony_ci */
37762306a36Sopenharmony_cistatic int isci_task_send_lu_reset_sas(
37862306a36Sopenharmony_ci	struct isci_host *isci_host,
37962306a36Sopenharmony_ci	struct isci_remote_device *isci_device,
38062306a36Sopenharmony_ci	u8 *lun)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	struct isci_tmf tmf;
38362306a36Sopenharmony_ci	int ret = TMF_RESP_FUNC_FAILED;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	dev_dbg(&isci_host->pdev->dev,
38662306a36Sopenharmony_ci		"%s: isci_host = %p, isci_device = %p\n",
38762306a36Sopenharmony_ci		__func__, isci_host, isci_device);
38862306a36Sopenharmony_ci	/* Send the LUN reset to the target.  By the time the call returns,
38962306a36Sopenharmony_ci	 * the TMF has fully exected in the target (in which case the return
39062306a36Sopenharmony_ci	 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
39162306a36Sopenharmony_ci	 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
39262306a36Sopenharmony_ci	 */
39362306a36Sopenharmony_ci	isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci	#define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
39662306a36Sopenharmony_ci	ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	if (ret == TMF_RESP_FUNC_COMPLETE)
39962306a36Sopenharmony_ci		dev_dbg(&isci_host->pdev->dev,
40062306a36Sopenharmony_ci			"%s: %p: TMF_LU_RESET passed\n",
40162306a36Sopenharmony_ci			__func__, isci_device);
40262306a36Sopenharmony_ci	else
40362306a36Sopenharmony_ci		dev_dbg(&isci_host->pdev->dev,
40462306a36Sopenharmony_ci			"%s: %p: TMF_LU_RESET failed (%x)\n",
40562306a36Sopenharmony_ci			__func__, isci_device, ret);
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	return ret;
40862306a36Sopenharmony_ci}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ciint isci_task_lu_reset(struct domain_device *dev, u8 *lun)
41162306a36Sopenharmony_ci{
41262306a36Sopenharmony_ci	struct isci_host *ihost = dev_to_ihost(dev);
41362306a36Sopenharmony_ci	struct isci_remote_device *idev;
41462306a36Sopenharmony_ci	unsigned long flags;
41562306a36Sopenharmony_ci	int ret = TMF_RESP_FUNC_COMPLETE;
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
41862306a36Sopenharmony_ci	idev = isci_get_device(dev->lldd_dev);
41962306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev,
42262306a36Sopenharmony_ci		"%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
42362306a36Sopenharmony_ci		__func__, dev, ihost, idev);
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	if (!idev) {
42662306a36Sopenharmony_ci		/* If the device is gone, escalate to I_T_Nexus_Reset. */
42762306a36Sopenharmony_ci		dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci		ret = TMF_RESP_FUNC_FAILED;
43062306a36Sopenharmony_ci		goto out;
43162306a36Sopenharmony_ci	}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	/* Suspend the RNC, kill all TCs */
43462306a36Sopenharmony_ci	if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
43562306a36Sopenharmony_ci	    != SCI_SUCCESS) {
43662306a36Sopenharmony_ci		/* The suspend/terminate only fails if isci_get_device fails */
43762306a36Sopenharmony_ci		ret = TMF_RESP_FUNC_FAILED;
43862306a36Sopenharmony_ci		goto out;
43962306a36Sopenharmony_ci	}
44062306a36Sopenharmony_ci	/* All pending I/Os have been terminated and cleaned up. */
44162306a36Sopenharmony_ci	if (!test_bit(IDEV_GONE, &idev->flags)) {
44262306a36Sopenharmony_ci		if (dev_is_sata(dev))
44362306a36Sopenharmony_ci			sas_ata_schedule_reset(dev);
44462306a36Sopenharmony_ci		else
44562306a36Sopenharmony_ci			/* Send the task management part of the reset. */
44662306a36Sopenharmony_ci			ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
44762306a36Sopenharmony_ci	}
44862306a36Sopenharmony_ci out:
44962306a36Sopenharmony_ci	isci_put_device(idev);
45062306a36Sopenharmony_ci	return ret;
45162306a36Sopenharmony_ci}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci/*	 int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
45562306a36Sopenharmony_ciint isci_task_clear_nexus_port(struct asd_sas_port *port)
45662306a36Sopenharmony_ci{
45762306a36Sopenharmony_ci	return TMF_RESP_FUNC_FAILED;
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ciint isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
46362306a36Sopenharmony_ci{
46462306a36Sopenharmony_ci	return TMF_RESP_FUNC_FAILED;
46562306a36Sopenharmony_ci}
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci/* Task Management Functions. Must be called from process context.	 */
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci/**
47062306a36Sopenharmony_ci * isci_task_abort_task() - This function is one of the SAS Domain Template
47162306a36Sopenharmony_ci *    functions. This function is called by libsas to abort a specified task.
47262306a36Sopenharmony_ci * @task: This parameter specifies the SAS task to abort.
47362306a36Sopenharmony_ci *
47462306a36Sopenharmony_ci * status, zero indicates success.
47562306a36Sopenharmony_ci */
47662306a36Sopenharmony_ciint isci_task_abort_task(struct sas_task *task)
47762306a36Sopenharmony_ci{
47862306a36Sopenharmony_ci	struct isci_host *ihost = dev_to_ihost(task->dev);
47962306a36Sopenharmony_ci	DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
48062306a36Sopenharmony_ci	struct isci_request       *old_request = NULL;
48162306a36Sopenharmony_ci	struct isci_remote_device *idev = NULL;
48262306a36Sopenharmony_ci	struct isci_tmf           tmf;
48362306a36Sopenharmony_ci	int                       ret = TMF_RESP_FUNC_FAILED;
48462306a36Sopenharmony_ci	unsigned long             flags;
48562306a36Sopenharmony_ci	int                       target_done_already = 0;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	/* Get the isci_request reference from the task.  Note that
48862306a36Sopenharmony_ci	 * this check does not depend on the pending request list
48962306a36Sopenharmony_ci	 * in the device, because tasks driving resets may land here
49062306a36Sopenharmony_ci	 * after completion in the core.
49162306a36Sopenharmony_ci	 */
49262306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
49362306a36Sopenharmony_ci	spin_lock(&task->task_state_lock);
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	old_request = task->lldd_task;
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	/* If task is already done, the request isn't valid */
49862306a36Sopenharmony_ci	if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
49962306a36Sopenharmony_ci	    old_request) {
50062306a36Sopenharmony_ci		idev = isci_get_device(task->dev->lldd_dev);
50162306a36Sopenharmony_ci		target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
50262306a36Sopenharmony_ci					       &old_request->flags);
50362306a36Sopenharmony_ci	}
50462306a36Sopenharmony_ci	spin_unlock(&task->task_state_lock);
50562306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	dev_warn(&ihost->pdev->dev,
50862306a36Sopenharmony_ci		 "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
50962306a36Sopenharmony_ci		 __func__, idev,
51062306a36Sopenharmony_ci		 (dev_is_sata(task->dev) ? "STP/SATA"
51162306a36Sopenharmony_ci					 : ((dev_is_expander(task->dev->dev_type))
51262306a36Sopenharmony_ci						? "SMP"
51362306a36Sopenharmony_ci						: "SSP")),
51462306a36Sopenharmony_ci		 ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
51562306a36Sopenharmony_ci			   ? " IDEV_GONE"
51662306a36Sopenharmony_ci			   : "")
51762306a36Sopenharmony_ci			 : " <NULL>"),
51862306a36Sopenharmony_ci		 task, old_request);
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	/* Device reset conditions signalled in task_state_flags are the
52162306a36Sopenharmony_ci	 * responsbility of libsas to observe at the start of the error
52262306a36Sopenharmony_ci	 * handler thread.
52362306a36Sopenharmony_ci	 */
52462306a36Sopenharmony_ci	if (!idev || !old_request) {
52562306a36Sopenharmony_ci		/* The request has already completed and there
52662306a36Sopenharmony_ci		* is nothing to do here other than to set the task
52762306a36Sopenharmony_ci		* done bit, and indicate that the task abort function
52862306a36Sopenharmony_ci		* was successful.
52962306a36Sopenharmony_ci		*/
53062306a36Sopenharmony_ci		spin_lock_irqsave(&task->task_state_lock, flags);
53162306a36Sopenharmony_ci		task->task_state_flags |= SAS_TASK_STATE_DONE;
53262306a36Sopenharmony_ci		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
53362306a36Sopenharmony_ci		spin_unlock_irqrestore(&task->task_state_lock, flags);
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci		ret = TMF_RESP_FUNC_COMPLETE;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci		dev_warn(&ihost->pdev->dev,
53862306a36Sopenharmony_ci			 "%s: abort task not needed for %p\n",
53962306a36Sopenharmony_ci			 __func__, task);
54062306a36Sopenharmony_ci		goto out;
54162306a36Sopenharmony_ci	}
54262306a36Sopenharmony_ci	/* Suspend the RNC, kill the TC */
54362306a36Sopenharmony_ci	if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
54462306a36Sopenharmony_ci	    != SCI_SUCCESS) {
54562306a36Sopenharmony_ci		dev_warn(&ihost->pdev->dev,
54662306a36Sopenharmony_ci			 "%s: isci_remote_device_reset_terminate(dev=%p, "
54762306a36Sopenharmony_ci				 "req=%p, task=%p) failed\n",
54862306a36Sopenharmony_ci			 __func__, idev, old_request, task);
54962306a36Sopenharmony_ci		ret = TMF_RESP_FUNC_FAILED;
55062306a36Sopenharmony_ci		goto out;
55162306a36Sopenharmony_ci	}
55262306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	if (task->task_proto == SAS_PROTOCOL_SMP ||
55562306a36Sopenharmony_ci	    sas_protocol_ata(task->task_proto) ||
55662306a36Sopenharmony_ci	    target_done_already ||
55762306a36Sopenharmony_ci	    test_bit(IDEV_GONE, &idev->flags)) {
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci		spin_unlock_irqrestore(&ihost->scic_lock, flags);
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci		/* No task to send, so explicitly resume the device here */
56262306a36Sopenharmony_ci		isci_remote_device_resume_from_abort(ihost, idev);
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci		dev_warn(&ihost->pdev->dev,
56562306a36Sopenharmony_ci			 "%s: %s request"
56662306a36Sopenharmony_ci				 " or complete_in_target (%d), "
56762306a36Sopenharmony_ci				 "or IDEV_GONE (%d), thus no TMF\n",
56862306a36Sopenharmony_ci			 __func__,
56962306a36Sopenharmony_ci			 ((task->task_proto == SAS_PROTOCOL_SMP)
57062306a36Sopenharmony_ci			  ? "SMP"
57162306a36Sopenharmony_ci			  : (sas_protocol_ata(task->task_proto)
57262306a36Sopenharmony_ci				? "SATA/STP"
57362306a36Sopenharmony_ci				: "<other>")
57462306a36Sopenharmony_ci			  ),
57562306a36Sopenharmony_ci			 test_bit(IREQ_COMPLETE_IN_TARGET,
57662306a36Sopenharmony_ci				  &old_request->flags),
57762306a36Sopenharmony_ci			 test_bit(IDEV_GONE, &idev->flags));
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci		spin_lock_irqsave(&task->task_state_lock, flags);
58062306a36Sopenharmony_ci		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
58162306a36Sopenharmony_ci		task->task_state_flags |= SAS_TASK_STATE_DONE;
58262306a36Sopenharmony_ci		spin_unlock_irqrestore(&task->task_state_lock, flags);
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci		ret = TMF_RESP_FUNC_COMPLETE;
58562306a36Sopenharmony_ci	} else {
58662306a36Sopenharmony_ci		/* Fill in the tmf structure */
58762306a36Sopenharmony_ci		isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
58862306a36Sopenharmony_ci					       old_request);
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci		spin_unlock_irqrestore(&ihost->scic_lock, flags);
59162306a36Sopenharmony_ci
59262306a36Sopenharmony_ci		/* Send the task management request. */
59362306a36Sopenharmony_ci		#define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
59462306a36Sopenharmony_ci		ret = isci_task_execute_tmf(ihost, idev, &tmf,
59562306a36Sopenharmony_ci					    ISCI_ABORT_TASK_TIMEOUT_MS);
59662306a36Sopenharmony_ci	}
59762306a36Sopenharmony_ciout:
59862306a36Sopenharmony_ci	dev_warn(&ihost->pdev->dev,
59962306a36Sopenharmony_ci		 "%s: Done; dev = %p, task = %p , old_request == %p\n",
60062306a36Sopenharmony_ci		 __func__, idev, task, old_request);
60162306a36Sopenharmony_ci	isci_put_device(idev);
60262306a36Sopenharmony_ci	return ret;
60362306a36Sopenharmony_ci}
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci/**
60662306a36Sopenharmony_ci * isci_task_abort_task_set() - This function is one of the SAS Domain Template
60762306a36Sopenharmony_ci *    functions. This is one of the Task Management functoins called by libsas,
60862306a36Sopenharmony_ci *    to abort all task for the given lun.
60962306a36Sopenharmony_ci * @d_device: This parameter specifies the domain device associated with this
61062306a36Sopenharmony_ci *    request.
61162306a36Sopenharmony_ci * @lun: This parameter specifies the lun associated with this request.
61262306a36Sopenharmony_ci *
61362306a36Sopenharmony_ci * status, zero indicates success.
61462306a36Sopenharmony_ci */
61562306a36Sopenharmony_ciint isci_task_abort_task_set(
61662306a36Sopenharmony_ci	struct domain_device *d_device,
61762306a36Sopenharmony_ci	u8 *lun)
61862306a36Sopenharmony_ci{
61962306a36Sopenharmony_ci	return TMF_RESP_FUNC_FAILED;
62062306a36Sopenharmony_ci}
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci/**
62462306a36Sopenharmony_ci * isci_task_clear_task_set() - This function is one of the SAS Domain Template
62562306a36Sopenharmony_ci *    functions. This is one of the Task Management functoins called by libsas.
62662306a36Sopenharmony_ci * @d_device: This parameter specifies the domain device associated with this
62762306a36Sopenharmony_ci *    request.
62862306a36Sopenharmony_ci * @lun: This parameter specifies the lun	 associated with this request.
62962306a36Sopenharmony_ci *
63062306a36Sopenharmony_ci * status, zero indicates success.
63162306a36Sopenharmony_ci */
63262306a36Sopenharmony_ciint isci_task_clear_task_set(
63362306a36Sopenharmony_ci	struct domain_device *d_device,
63462306a36Sopenharmony_ci	u8 *lun)
63562306a36Sopenharmony_ci{
63662306a36Sopenharmony_ci	return TMF_RESP_FUNC_FAILED;
63762306a36Sopenharmony_ci}
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci/**
64162306a36Sopenharmony_ci * isci_task_query_task() - This function is implemented to cause libsas to
64262306a36Sopenharmony_ci *    correctly escalate the failed abort to a LUN or target reset (this is
64362306a36Sopenharmony_ci *    because sas_scsi_find_task libsas function does not correctly interpret
64462306a36Sopenharmony_ci *    all return codes from the abort task call).  When TMF_RESP_FUNC_SUCC is
64562306a36Sopenharmony_ci *    returned, libsas turns this into a LUN reset; when FUNC_FAILED is
64662306a36Sopenharmony_ci *    returned, libsas will turn this into a target reset
64762306a36Sopenharmony_ci * @task: This parameter specifies the sas task being queried.
64862306a36Sopenharmony_ci *
64962306a36Sopenharmony_ci * status, zero indicates success.
65062306a36Sopenharmony_ci */
65162306a36Sopenharmony_ciint isci_task_query_task(
65262306a36Sopenharmony_ci	struct sas_task *task)
65362306a36Sopenharmony_ci{
65462306a36Sopenharmony_ci	/* See if there is a pending device reset for this device. */
65562306a36Sopenharmony_ci	if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
65662306a36Sopenharmony_ci		return TMF_RESP_FUNC_FAILED;
65762306a36Sopenharmony_ci	else
65862306a36Sopenharmony_ci		return TMF_RESP_FUNC_SUCC;
65962306a36Sopenharmony_ci}
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci/*
66262306a36Sopenharmony_ci * isci_task_request_complete() - This function is called by the sci core when
66362306a36Sopenharmony_ci *    an task request completes.
66462306a36Sopenharmony_ci * @ihost: This parameter specifies the ISCI host object
66562306a36Sopenharmony_ci * @ireq: This parameter is the completed isci_request object.
66662306a36Sopenharmony_ci * @completion_status: This parameter specifies the completion status from the
66762306a36Sopenharmony_ci *    sci core.
66862306a36Sopenharmony_ci *
66962306a36Sopenharmony_ci * none.
67062306a36Sopenharmony_ci */
67162306a36Sopenharmony_civoid
67262306a36Sopenharmony_ciisci_task_request_complete(struct isci_host *ihost,
67362306a36Sopenharmony_ci			   struct isci_request *ireq,
67462306a36Sopenharmony_ci			   enum sci_task_status completion_status)
67562306a36Sopenharmony_ci{
67662306a36Sopenharmony_ci	struct isci_tmf *tmf = isci_request_access_tmf(ireq);
67762306a36Sopenharmony_ci	struct completion *tmf_complete = NULL;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev,
68062306a36Sopenharmony_ci		"%s: request = %p, status=%d\n",
68162306a36Sopenharmony_ci		__func__, ireq, completion_status);
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci	set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci	if (tmf) {
68662306a36Sopenharmony_ci		tmf->status = completion_status;
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ci		if (tmf->proto == SAS_PROTOCOL_SSP) {
68962306a36Sopenharmony_ci			memcpy(tmf->resp.rsp_buf,
69062306a36Sopenharmony_ci			       ireq->ssp.rsp_buf,
69162306a36Sopenharmony_ci			       SSP_RESP_IU_MAX_SIZE);
69262306a36Sopenharmony_ci		} else if (tmf->proto == SAS_PROTOCOL_SATA) {
69362306a36Sopenharmony_ci			memcpy(&tmf->resp.d2h_fis,
69462306a36Sopenharmony_ci			       &ireq->stp.rsp,
69562306a36Sopenharmony_ci			       sizeof(struct dev_to_host_fis));
69662306a36Sopenharmony_ci		}
69762306a36Sopenharmony_ci		/* PRINT_TMF( ((struct isci_tmf *)request->task)); */
69862306a36Sopenharmony_ci		tmf_complete = tmf->complete;
69962306a36Sopenharmony_ci	}
70062306a36Sopenharmony_ci	sci_controller_complete_io(ihost, ireq->target_device, ireq);
70162306a36Sopenharmony_ci	/* set the 'terminated' flag handle to make sure it cannot be terminated
70262306a36Sopenharmony_ci	 *  or completed again.
70362306a36Sopenharmony_ci	 */
70462306a36Sopenharmony_ci	set_bit(IREQ_TERMINATED, &ireq->flags);
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_ci	if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
70762306a36Sopenharmony_ci		wake_up_all(&ihost->eventq);
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
71062306a36Sopenharmony_ci		isci_free_tag(ihost, ireq->io_tag);
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_ci	/* The task management part completes last. */
71362306a36Sopenharmony_ci	if (tmf_complete)
71462306a36Sopenharmony_ci		complete(tmf_complete);
71562306a36Sopenharmony_ci}
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_cistatic int isci_reset_device(struct isci_host *ihost,
71862306a36Sopenharmony_ci			     struct domain_device *dev,
71962306a36Sopenharmony_ci			     struct isci_remote_device *idev)
72062306a36Sopenharmony_ci{
72162306a36Sopenharmony_ci	int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
72262306a36Sopenharmony_ci	struct sas_phy *phy = sas_get_local_phy(dev);
72362306a36Sopenharmony_ci	struct isci_port *iport = dev->port->lldd_port;
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci	/* Suspend the RNC, terminate all outstanding TCs. */
72862306a36Sopenharmony_ci	if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
72962306a36Sopenharmony_ci	    != SCI_SUCCESS) {
73062306a36Sopenharmony_ci		rc = TMF_RESP_FUNC_FAILED;
73162306a36Sopenharmony_ci		goto out;
73262306a36Sopenharmony_ci	}
73362306a36Sopenharmony_ci	/* Note that since the termination for outstanding requests succeeded,
73462306a36Sopenharmony_ci	 * this function will return success.  This is because the resets will
73562306a36Sopenharmony_ci	 * only fail if the device has been removed (ie. hotplug), and the
73662306a36Sopenharmony_ci	 * primary duty of this function is to cleanup tasks, so that is the
73762306a36Sopenharmony_ci	 * relevant status.
73862306a36Sopenharmony_ci	 */
73962306a36Sopenharmony_ci	if (!test_bit(IDEV_GONE, &idev->flags)) {
74062306a36Sopenharmony_ci		if (scsi_is_sas_phy_local(phy)) {
74162306a36Sopenharmony_ci			struct isci_phy *iphy = &ihost->phys[phy->number];
74262306a36Sopenharmony_ci
74362306a36Sopenharmony_ci			reset_stat = isci_port_perform_hard_reset(ihost, iport,
74462306a36Sopenharmony_ci								  iphy);
74562306a36Sopenharmony_ci		} else
74662306a36Sopenharmony_ci			reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
74762306a36Sopenharmony_ci	}
74862306a36Sopenharmony_ci	/* Explicitly resume the RNC here, since there was no task sent. */
74962306a36Sopenharmony_ci	isci_remote_device_resume_from_abort(ihost, idev);
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_ci	dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
75262306a36Sopenharmony_ci		__func__, idev, reset_stat);
75362306a36Sopenharmony_ci out:
75462306a36Sopenharmony_ci	sas_put_local_phy(phy);
75562306a36Sopenharmony_ci	return rc;
75662306a36Sopenharmony_ci}
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ciint isci_task_I_T_nexus_reset(struct domain_device *dev)
75962306a36Sopenharmony_ci{
76062306a36Sopenharmony_ci	struct isci_host *ihost = dev_to_ihost(dev);
76162306a36Sopenharmony_ci	struct isci_remote_device *idev;
76262306a36Sopenharmony_ci	unsigned long flags;
76362306a36Sopenharmony_ci	int ret;
76462306a36Sopenharmony_ci
76562306a36Sopenharmony_ci	spin_lock_irqsave(&ihost->scic_lock, flags);
76662306a36Sopenharmony_ci	idev = isci_get_device(dev->lldd_dev);
76762306a36Sopenharmony_ci	spin_unlock_irqrestore(&ihost->scic_lock, flags);
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	if (!idev) {
77062306a36Sopenharmony_ci		/* XXX: need to cleanup any ireqs targeting this
77162306a36Sopenharmony_ci		 * domain_device
77262306a36Sopenharmony_ci		 */
77362306a36Sopenharmony_ci		ret = -ENODEV;
77462306a36Sopenharmony_ci		goto out;
77562306a36Sopenharmony_ci	}
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci	ret = isci_reset_device(ihost, dev, idev);
77862306a36Sopenharmony_ci out:
77962306a36Sopenharmony_ci	isci_put_device(idev);
78062306a36Sopenharmony_ci	return ret;
78162306a36Sopenharmony_ci}
782