xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision f41a0c441c3fe43e79ebeb75584dbb5bfa83e5cd)
1338ec570SDarrick J. Wong /*
2338ec570SDarrick J. Wong  * Support for SATA devices on Serial Attached SCSI (SAS) controllers
3338ec570SDarrick J. Wong  *
4338ec570SDarrick J. Wong  * Copyright (C) 2006 IBM Corporation
5338ec570SDarrick J. Wong  *
6338ec570SDarrick J. Wong  * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
7338ec570SDarrick J. Wong  *
8338ec570SDarrick J. Wong  * This program is free software; you can redistribute it and/or
9338ec570SDarrick J. Wong  * modify it under the terms of the GNU General Public License as
10338ec570SDarrick J. Wong  * published by the Free Software Foundation; either version 2 of the
11338ec570SDarrick J. Wong  * License, or (at your option) any later version.
12338ec570SDarrick J. Wong  *
13338ec570SDarrick J. Wong  * This program is distributed in the hope that it will be useful, but
14338ec570SDarrick J. Wong  * WITHOUT ANY WARRANTY; without even the implied warranty of
15338ec570SDarrick J. Wong  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16338ec570SDarrick J. Wong  * General Public License for more details.
17338ec570SDarrick J. Wong  *
18338ec570SDarrick J. Wong  * You should have received a copy of the GNU General Public License
19338ec570SDarrick J. Wong  * along with this program; if not, write to the Free Software
20338ec570SDarrick J. Wong  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21338ec570SDarrick J. Wong  * USA
22338ec570SDarrick J. Wong  */
23338ec570SDarrick J. Wong 
24b9142174SJames Bottomley #include <linux/scatterlist.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2650824d6cSDan Williams #include <linux/async.h>
27b9142174SJames Bottomley 
28338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
29338ec570SDarrick J. Wong #include "sas_internal.h"
30338ec570SDarrick J. Wong #include <scsi/scsi_host.h>
31338ec570SDarrick J. Wong #include <scsi/scsi_device.h>
32338ec570SDarrick J. Wong #include <scsi/scsi_tcq.h>
33338ec570SDarrick J. Wong #include <scsi/scsi.h>
34338ec570SDarrick J. Wong #include <scsi/scsi_transport.h>
35338ec570SDarrick J. Wong #include <scsi/scsi_transport_sas.h>
36338ec570SDarrick J. Wong #include "../scsi_sas_internal.h"
373a2755afSDarrick J. Wong #include "../scsi_transport_api.h"
383a2755afSDarrick J. Wong #include <scsi/scsi_eh.h>
39338ec570SDarrick J. Wong 
40338ec570SDarrick J. Wong static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
41338ec570SDarrick J. Wong {
42338ec570SDarrick J. Wong 	/* Cheesy attempt to translate SAS errors into ATA.  Hah! */
43338ec570SDarrick J. Wong 
44338ec570SDarrick J. Wong 	/* transport error */
45338ec570SDarrick J. Wong 	if (ts->resp == SAS_TASK_UNDELIVERED)
46338ec570SDarrick J. Wong 		return AC_ERR_ATA_BUS;
47338ec570SDarrick J. Wong 
48338ec570SDarrick J. Wong 	/* ts->resp == SAS_TASK_COMPLETE */
49338ec570SDarrick J. Wong 	/* task delivered, what happened afterwards? */
50338ec570SDarrick J. Wong 	switch (ts->stat) {
51338ec570SDarrick J. Wong 		case SAS_DEV_NO_RESPONSE:
52338ec570SDarrick J. Wong 			return AC_ERR_TIMEOUT;
53338ec570SDarrick J. Wong 
54338ec570SDarrick J. Wong 		case SAS_INTERRUPTED:
55338ec570SDarrick J. Wong 		case SAS_PHY_DOWN:
56338ec570SDarrick J. Wong 		case SAS_NAK_R_ERR:
57338ec570SDarrick J. Wong 			return AC_ERR_ATA_BUS;
58338ec570SDarrick J. Wong 
59338ec570SDarrick J. Wong 
60338ec570SDarrick J. Wong 		case SAS_DATA_UNDERRUN:
61338ec570SDarrick J. Wong 			/*
62338ec570SDarrick J. Wong 			 * Some programs that use the taskfile interface
63338ec570SDarrick J. Wong 			 * (smartctl in particular) can cause underrun
64338ec570SDarrick J. Wong 			 * problems.  Ignore these errors, perhaps at our
65338ec570SDarrick J. Wong 			 * peril.
66338ec570SDarrick J. Wong 			 */
67338ec570SDarrick J. Wong 			return 0;
68338ec570SDarrick J. Wong 
69338ec570SDarrick J. Wong 		case SAS_DATA_OVERRUN:
70338ec570SDarrick J. Wong 		case SAS_QUEUE_FULL:
71338ec570SDarrick J. Wong 		case SAS_DEVICE_UNKNOWN:
72338ec570SDarrick J. Wong 		case SAS_SG_ERR:
73338ec570SDarrick J. Wong 			return AC_ERR_INVALID;
74338ec570SDarrick J. Wong 
75338ec570SDarrick J. Wong 		case SAS_OPEN_TO:
76338ec570SDarrick J. Wong 		case SAS_OPEN_REJECT:
77338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Saw error %d.  What to do?\n",
78cadbd4a5SHarvey Harrison 				    __func__, ts->stat);
79338ec570SDarrick J. Wong 			return AC_ERR_OTHER;
80338ec570SDarrick J. Wong 
8175c0b386SJames Bottomley 		case SAM_STAT_CHECK_CONDITION:
82338ec570SDarrick J. Wong 		case SAS_ABORTED_TASK:
83338ec570SDarrick J. Wong 			return AC_ERR_DEV;
84338ec570SDarrick J. Wong 
85338ec570SDarrick J. Wong 		case SAS_PROTO_RESPONSE:
86338ec570SDarrick J. Wong 			/* This means the ending_fis has the error
87338ec570SDarrick J. Wong 			 * value; return 0 here to collect it */
88338ec570SDarrick J. Wong 			return 0;
89338ec570SDarrick J. Wong 		default:
90338ec570SDarrick J. Wong 			return 0;
91338ec570SDarrick J. Wong 	}
92338ec570SDarrick J. Wong }
93338ec570SDarrick J. Wong 
94338ec570SDarrick J. Wong static void sas_ata_task_done(struct sas_task *task)
95338ec570SDarrick J. Wong {
96338ec570SDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
979095a64aSDan Williams 	struct domain_device *dev = task->dev;
98338ec570SDarrick J. Wong 	struct task_status_struct *stat = &task->task_status;
99338ec570SDarrick J. Wong 	struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
1009095a64aSDan Williams 	struct sas_ha_struct *sas_ha = dev->port->ha;
101338ec570SDarrick J. Wong 	enum ata_completion_errors ac;
1023eb7a51aSDarrick J. Wong 	unsigned long flags;
103bb650a1bSXiangliang Yu 	struct ata_link *link;
1043dff5721SDan Williams 	struct ata_port *ap;
105338ec570SDarrick J. Wong 
1069095a64aSDan Williams 	spin_lock_irqsave(&dev->done_lock, flags);
1079095a64aSDan Williams 	if (test_bit(SAS_HA_FROZEN, &sas_ha->state))
1089095a64aSDan Williams 		task = NULL;
1099095a64aSDan Williams 	else if (qc && qc->scsicmd)
1109095a64aSDan Williams 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
1119095a64aSDan Williams 	spin_unlock_irqrestore(&dev->done_lock, flags);
1129095a64aSDan Williams 
1139095a64aSDan Williams 	/* check if libsas-eh got to the task before us */
1149095a64aSDan Williams 	if (unlikely(!task))
1159095a64aSDan Williams 		return;
1169095a64aSDan Williams 
1171c50dc83SDarrick J. Wong 	if (!qc)
1181c50dc83SDarrick J. Wong 		goto qc_already_gone;
1191c50dc83SDarrick J. Wong 
1203dff5721SDan Williams 	ap = qc->ap;
1213dff5721SDan Williams 	link = &ap->link;
1221c50dc83SDarrick J. Wong 
1233dff5721SDan Williams 	spin_lock_irqsave(ap->lock, flags);
1243dff5721SDan Williams 	/* check if we lost the race with libata/sas_ata_post_internal() */
1253dff5721SDan Williams 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) {
1263dff5721SDan Williams 		spin_unlock_irqrestore(ap->lock, flags);
1273dff5721SDan Williams 		if (qc->scsicmd)
1283dff5721SDan Williams 			goto qc_already_gone;
1293dff5721SDan Williams 		else {
1303dff5721SDan Williams 			/* if eh is not involved and the port is frozen then the
1313dff5721SDan Williams 			 * ata internal abort process has taken responsibility
1323dff5721SDan Williams 			 * for this sas_task
1333dff5721SDan Williams 			 */
1343dff5721SDan Williams 			return;
1353dff5721SDan Williams 		}
1363dff5721SDan Williams 	}
1373dff5721SDan Williams 
13875c0b386SJames Bottomley 	if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
13975c0b386SJames Bottomley 	    ((stat->stat == SAM_STAT_CHECK_CONDITION &&
14075c0b386SJames Bottomley 	      dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
141338ec570SDarrick J. Wong 		ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
142bb650a1bSXiangliang Yu 
143bb650a1bSXiangliang Yu 		if (!link->sactive) {
144338ec570SDarrick J. Wong 			qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
145bb650a1bSXiangliang Yu 		} else {
146bb650a1bSXiangliang Yu 			link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
147bb650a1bSXiangliang Yu 			if (unlikely(link->eh_info.err_mask))
148bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
149bb650a1bSXiangliang Yu 		}
15075c0b386SJames Bottomley 	} else {
151338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
152338ec570SDarrick J. Wong 		if (ac) {
153cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: SAS error %x\n", __func__,
154338ec570SDarrick J. Wong 				    stat->stat);
155338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
156bb650a1bSXiangliang Yu 			if (!link->sactive) {
157338ec570SDarrick J. Wong 				qc->err_mask = ac;
158bb650a1bSXiangliang Yu 			} else {
159bb650a1bSXiangliang Yu 				link->eh_info.err_mask |= AC_ERR_DEV;
160bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
161bb650a1bSXiangliang Yu 			}
162bb650a1bSXiangliang Yu 
163338ec570SDarrick J. Wong 			dev->sata_dev.tf.feature = 0x04; /* status err */
164338ec570SDarrick J. Wong 			dev->sata_dev.tf.command = ATA_ERR;
165338ec570SDarrick J. Wong 		}
166338ec570SDarrick J. Wong 	}
167338ec570SDarrick J. Wong 
1681c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
169338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1703dff5721SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
1713eb7a51aSDarrick J. Wong 
1721c50dc83SDarrick J. Wong qc_already_gone:
173338ec570SDarrick J. Wong 	list_del_init(&task->list);
174338ec570SDarrick J. Wong 	sas_free_task(task);
175338ec570SDarrick J. Wong }
176338ec570SDarrick J. Wong 
177338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
178338ec570SDarrick J. Wong {
179312d3e56SDan Williams 	unsigned long flags;
180338ec570SDarrick J. Wong 	struct sas_task *task;
181312d3e56SDan Williams 	struct scatterlist *sg;
182312d3e56SDan Williams 	int ret = AC_ERR_SYSTEM;
183312d3e56SDan Williams 	unsigned int si, xfer = 0;
184312d3e56SDan Williams 	struct ata_port *ap = qc->ap;
185312d3e56SDan Williams 	struct domain_device *dev = ap->private_data;
186338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
187338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
188338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
189312d3e56SDan Williams 
190312d3e56SDan Williams 	/* TODO: audit callers to ensure they are ready for qc_issue to
191312d3e56SDan Williams 	 * unconditionally re-enable interrupts
192312d3e56SDan Williams 	 */
193312d3e56SDan Williams 	local_irq_save(flags);
194312d3e56SDan Williams 	spin_unlock(ap->lock);
195338ec570SDarrick J. Wong 
19656dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
197e139942dSDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state))
198312d3e56SDan Williams 		goto out;
19956dd2c06SDarrick J. Wong 
200338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
201338ec570SDarrick J. Wong 	if (!task)
202312d3e56SDan Williams 		goto out;
203338ec570SDarrick J. Wong 	task->dev = dev;
204338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
205338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
206338ec570SDarrick J. Wong 
207338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
208338ec570SDarrick J. Wong 	    qc->tf.command == ATA_CMD_FPDMA_READ) {
209338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
210338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
211338ec570SDarrick J. Wong 	}
212338ec570SDarrick J. Wong 
213110dd8f1SJames Bottomley 	ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
214338ec570SDarrick J. Wong 	task->uldd_task = qc;
215405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
216338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
217dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
218dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
219338ec570SDarrick J. Wong 	} else {
220ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
221338ec570SDarrick J. Wong 			xfer += sg->length;
222338ec570SDarrick J. Wong 
223338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
224ff2aeb1eSTejun Heo 		task->num_scatter = si;
225338ec570SDarrick J. Wong 	}
226338ec570SDarrick J. Wong 
227338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
228ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
229338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
230338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
2311c50dc83SDarrick J. Wong 	qc->lldd_task = task;
232338ec570SDarrick J. Wong 
233338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
234338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
235338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
236338ec570SDarrick J. Wong 		/* fall through */
2370dc36888STejun Heo 	case ATAPI_PROT_DMA:
238338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
239338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
240338ec570SDarrick J. Wong 		break;
241338ec570SDarrick J. Wong 	}
242338ec570SDarrick J. Wong 
243fe059f12SDarrick J. Wong 	if (qc->scsicmd)
244fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
245fe059f12SDarrick J. Wong 
246338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
247312d3e56SDan Williams 		ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
248338ec570SDarrick J. Wong 	else
249312d3e56SDan Williams 		ret = sas_queue_up(task);
250338ec570SDarrick J. Wong 
251338ec570SDarrick J. Wong 	/* Examine */
252312d3e56SDan Williams 	if (ret) {
253312d3e56SDan Williams 		SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
254338ec570SDarrick J. Wong 
255fe059f12SDarrick J. Wong 		if (qc->scsicmd)
256fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
257338ec570SDarrick J. Wong 		sas_free_task(task);
258312d3e56SDan Williams 		ret = AC_ERR_SYSTEM;
259338ec570SDarrick J. Wong 	}
260338ec570SDarrick J. Wong 
261312d3e56SDan Williams  out:
262312d3e56SDan Williams 	spin_lock(ap->lock);
263312d3e56SDan Williams 	local_irq_restore(flags);
264312d3e56SDan Williams 	return ret;
265338ec570SDarrick J. Wong }
266338ec570SDarrick J. Wong 
2674c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2684c9bf4e7STejun Heo {
2694c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2704c9bf4e7STejun Heo 
2714c9bf4e7STejun Heo 	memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
2724c9bf4e7STejun Heo 	return true;
2734c9bf4e7STejun Heo }
2744c9bf4e7STejun Heo 
27536a39947SDan Williams static struct sas_internal *dev_to_sas_internal(struct domain_device *dev)
27636a39947SDan Williams {
27736a39947SDan Williams 	return to_sas_internal(dev->port->ha->core.shost->transportt);
27836a39947SDan Williams }
27936a39947SDan Williams 
28036a39947SDan Williams static int smp_ata_check_ready(struct ata_link *link)
28136a39947SDan Williams {
28236a39947SDan Williams 	int res;
28336a39947SDan Williams 	u8 addr[8];
28436a39947SDan Williams 	struct ata_port *ap = link->ap;
28536a39947SDan Williams 	struct domain_device *dev = ap->private_data;
28636a39947SDan Williams 	struct domain_device *ex_dev = dev->parent;
287*f41a0c44SDan Williams 	struct sas_phy *phy = sas_get_local_phy(dev);
28836a39947SDan Williams 
28936a39947SDan Williams 	res = sas_get_phy_attached_sas_addr(ex_dev, phy->number, addr);
290*f41a0c44SDan Williams 	sas_put_local_phy(phy);
29136a39947SDan Williams 	/* break the wait early if the expander is unreachable,
29236a39947SDan Williams 	 * otherwise keep polling
29336a39947SDan Williams 	 */
29436a39947SDan Williams 	if (res == -ECOMM)
29536a39947SDan Williams 		return res;
29636a39947SDan Williams 	if (res != SMP_RESP_FUNC_ACC || SAS_ADDR(addr) == 0)
29736a39947SDan Williams 		return 0;
29836a39947SDan Williams 	else
29936a39947SDan Williams 		return 1;
30036a39947SDan Williams }
30136a39947SDan Williams 
30236a39947SDan Williams static int local_ata_check_ready(struct ata_link *link)
303338ec570SDarrick J. Wong {
30400dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
305338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
30636a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
307338ec570SDarrick J. Wong 
30836a39947SDan Williams 	if (i->dft->lldd_ata_check_ready)
30936a39947SDan Williams 		return i->dft->lldd_ata_check_ready(dev);
31036a39947SDan Williams 	else {
31136a39947SDan Williams 		/* lldd's that don't implement 'ready' checking get the
31236a39947SDan Williams 		 * old default behavior of not coordinating reset
31336a39947SDan Williams 		 * recovery with libata
31436a39947SDan Williams 		 */
31536a39947SDan Williams 		return 1;
31636a39947SDan Williams 	}
31700dd4998SJames Bottomley }
318338ec570SDarrick J. Wong 
31936a39947SDan Williams static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
32036a39947SDan Williams 			      unsigned long deadline)
32136a39947SDan Williams {
32236a39947SDan Williams 	int ret = 0, res;
323*f41a0c44SDan Williams 	struct sas_phy *phy;
32436a39947SDan Williams 	struct ata_port *ap = link->ap;
32536a39947SDan Williams 	int (*check_ready)(struct ata_link *link);
32636a39947SDan Williams 	struct domain_device *dev = ap->private_data;
32736a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
32836a39947SDan Williams 
32936a39947SDan Williams 	res = i->dft->lldd_I_T_nexus_reset(dev);
33036a39947SDan Williams 
33136a39947SDan Williams 	if (res != TMF_RESP_FUNC_COMPLETE)
33236a39947SDan Williams 		SAS_DPRINTK("%s: Unable to reset ata device?\n", __func__);
33336a39947SDan Williams 
334*f41a0c44SDan Williams 	phy = sas_get_local_phy(dev);
33536a39947SDan Williams 	if (scsi_is_sas_phy_local(phy))
33636a39947SDan Williams 		check_ready = local_ata_check_ready;
33736a39947SDan Williams 	else
33836a39947SDan Williams 		check_ready = smp_ata_check_ready;
339*f41a0c44SDan Williams 	sas_put_local_phy(phy);
34036a39947SDan Williams 
34136a39947SDan Williams 	ret = ata_wait_after_reset(link, deadline, check_ready);
34236a39947SDan Williams 	if (ret && ret != -EAGAIN)
34336a39947SDan Williams 		ata_link_err(link, "COMRESET failed (errno=%d)\n", ret);
34436a39947SDan Williams 
34536a39947SDan Williams 	/* XXX: if the class changes during the reset the upper layer
34636a39947SDan Williams 	 * should be informed, if the device has gone away we assume
34736a39947SDan Williams 	 * libsas will eventually delete it
34836a39947SDan Williams 	 */
349338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
350338ec570SDarrick J. Wong 	case ATA_COMMAND_SET:
35100dd4998SJames Bottomley 		*class = ATA_DEV_ATA;
352338ec570SDarrick J. Wong 		break;
353338ec570SDarrick J. Wong 	case ATAPI_COMMAND_SET:
35400dd4998SJames Bottomley 		*class = ATA_DEV_ATAPI;
355338ec570SDarrick J. Wong 		break;
356338ec570SDarrick J. Wong 	}
357338ec570SDarrick J. Wong 
358338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
35900dd4998SJames Bottomley 	return ret;
360338ec570SDarrick J. Wong }
361338ec570SDarrick J. Wong 
3621ca1e43eSDave Jiang static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
3631ca1e43eSDave Jiang 			       unsigned long deadline)
3641ca1e43eSDave Jiang {
3651ca1e43eSDave Jiang 	struct ata_port *ap = link->ap;
3661ca1e43eSDave Jiang 	struct domain_device *dev = ap->private_data;
36736a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
3681ca1e43eSDave Jiang 	int res = TMF_RESP_FUNC_FAILED;
3691ca1e43eSDave Jiang 	int ret = 0;
3701ca1e43eSDave Jiang 
3711ca1e43eSDave Jiang 	if (i->dft->lldd_ata_soft_reset)
3721ca1e43eSDave Jiang 		res = i->dft->lldd_ata_soft_reset(dev);
3731ca1e43eSDave Jiang 
3741ca1e43eSDave Jiang 	if (res != TMF_RESP_FUNC_COMPLETE) {
3751ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
3761ca1e43eSDave Jiang 		ret = -EAGAIN;
3771ca1e43eSDave Jiang 	}
3781ca1e43eSDave Jiang 
3791ca1e43eSDave Jiang 	switch (dev->sata_dev.command_set) {
3801ca1e43eSDave Jiang 	case ATA_COMMAND_SET:
3811ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Found ATA device.\n", __func__);
3821ca1e43eSDave Jiang 		*class = ATA_DEV_ATA;
3831ca1e43eSDave Jiang 		break;
3841ca1e43eSDave Jiang 	case ATAPI_COMMAND_SET:
3851ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
3861ca1e43eSDave Jiang 		*class = ATA_DEV_ATAPI;
3871ca1e43eSDave Jiang 		break;
3881ca1e43eSDave Jiang 	default:
3891ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
3901ca1e43eSDave Jiang 			    __func__, dev->sata_dev.command_set);
3911ca1e43eSDave Jiang 		*class = ATA_DEV_UNKNOWN;
3921ca1e43eSDave Jiang 		break;
3931ca1e43eSDave Jiang 	}
3941ca1e43eSDave Jiang 
3951ca1e43eSDave Jiang 	ap->cbl = ATA_CBL_SATA;
3961ca1e43eSDave Jiang 	return ret;
3971ca1e43eSDave Jiang }
3981ca1e43eSDave Jiang 
3993dff5721SDan Williams /*
4003dff5721SDan Williams  * notify the lldd to forget the sas_task for this internal ata command
4013dff5721SDan Williams  * that bypasses scsi-eh
4023dff5721SDan Williams  */
4033dff5721SDan Williams static void sas_ata_internal_abort(struct sas_task *task)
4043dff5721SDan Williams {
40536a39947SDan Williams 	struct sas_internal *si = dev_to_sas_internal(task->dev);
4063dff5721SDan Williams 	unsigned long flags;
4073dff5721SDan Williams 	int res;
4083dff5721SDan Williams 
4093dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4103dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
4113dff5721SDan Williams 	    task->task_state_flags & SAS_TASK_STATE_DONE) {
4123dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4133dff5721SDan Williams 		SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
4143dff5721SDan Williams 			    task);
4153dff5721SDan Williams 		goto out;
4163dff5721SDan Williams 	}
4173dff5721SDan Williams 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
4183dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4193dff5721SDan Williams 
4203dff5721SDan Williams 	res = si->dft->lldd_abort_task(task);
4213dff5721SDan Williams 
4223dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4233dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_DONE ||
4243dff5721SDan Williams 	    res == TMF_RESP_FUNC_COMPLETE) {
4253dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4263dff5721SDan Williams 		goto out;
4273dff5721SDan Williams 	}
4283dff5721SDan Williams 
4293dff5721SDan Williams 	/* XXX we are not prepared to deal with ->lldd_abort_task()
4303dff5721SDan Williams 	 * failures.  TODO: lldds need to unconditionally forget about
4313dff5721SDan Williams 	 * aborted ata tasks, otherwise we (likely) leak the sas task
4323dff5721SDan Williams 	 * here
4333dff5721SDan Williams 	 */
4343dff5721SDan Williams 	SAS_DPRINTK("%s: Task %p leaked.\n", __func__, task);
4353dff5721SDan Williams 
4363dff5721SDan Williams 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
4373dff5721SDan Williams 		task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
4383dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4393dff5721SDan Williams 
4403dff5721SDan Williams 	return;
4413dff5721SDan Williams  out:
4423dff5721SDan Williams 	list_del_init(&task->list);
4433dff5721SDan Williams 	sas_free_task(task);
4443dff5721SDan Williams }
4453dff5721SDan Williams 
446338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
447338ec570SDarrick J. Wong {
448338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
449338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
450338ec570SDarrick J. Wong 
4511c50dc83SDarrick J. Wong 	if (qc->err_mask) {
4521c50dc83SDarrick J. Wong 		/*
4533dff5721SDan Williams 		 * Find the sas_task and kill it.  By this point, libata
4543dff5721SDan Williams 		 * has decided to kill the qc and has frozen the port.
4553dff5721SDan Williams 		 * In this state sas_ata_task_done() will no longer free
4563dff5721SDan Williams 		 * the sas_task, so we need to notify the lldd (via
4573dff5721SDan Williams 		 * ->lldd_abort_task) that the task is dead and free it
4583dff5721SDan Williams 		 *  ourselves.
4591c50dc83SDarrick J. Wong 		 */
4601c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
4611c50dc83SDarrick J. Wong 
4621c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
4633a2cdf39SDan Williams 		if (!task)
4643a2cdf39SDan Williams 			return;
4651c50dc83SDarrick J. Wong 		task->uldd_task = NULL;
4663dff5721SDan Williams 		sas_ata_internal_abort(task);
4671c50dc83SDarrick J. Wong 	}
4681c50dc83SDarrick J. Wong }
469338ec570SDarrick J. Wong 
470b91bb296SDan Williams 
471b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
472b91bb296SDan Williams {
473b91bb296SDan Williams 	struct domain_device *dev = ap->private_data;
47436a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
475b91bb296SDan Williams 
476b91bb296SDan Williams 	if (i->dft->lldd_ata_set_dmamode)
477b91bb296SDan Williams 		i->dft->lldd_ata_set_dmamode(dev);
478b91bb296SDan Williams }
479b91bb296SDan Williams 
480338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
48100dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
4821ca1e43eSDave Jiang 	.softreset		= sas_ata_soft_reset,
48300dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
48400dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
48500dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
486338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
487f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
488338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
489338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
4904c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
491338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
492338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
493b91bb296SDan Williams 	.set_dmamode		= sas_ata_set_dmamode,
494338ec570SDarrick J. Wong };
495338ec570SDarrick J. Wong 
496338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
4979cbe056fSSergei Shtylyov 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
4980f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
4990f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
500338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
501338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
502338ec570SDarrick J. Wong };
503338ec570SDarrick J. Wong 
504338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
505338ec570SDarrick J. Wong 			       struct scsi_target *starget)
506338ec570SDarrick J. Wong {
507338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
508338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
509338ec570SDarrick J. Wong 	struct ata_port *ap;
510338ec570SDarrick J. Wong 
511338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
5121d1bbee6SJeff Garzik 		      ha->dev,
513338ec570SDarrick J. Wong 		      sata_port_info.flags,
514338ec570SDarrick J. Wong 		      &sas_sata_ops);
515338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
516338ec570SDarrick J. Wong 				&sata_port_info,
517338ec570SDarrick J. Wong 				shost);
518338ec570SDarrick J. Wong 	if (!ap) {
519338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
520338ec570SDarrick J. Wong 		return -ENODEV;
521338ec570SDarrick J. Wong 	}
522338ec570SDarrick J. Wong 
523338ec570SDarrick J. Wong 	ap->private_data = found_dev;
524338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
525338ec570SDarrick J. Wong 	ap->scsi_host = shost;
526338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
527338ec570SDarrick J. Wong 
528338ec570SDarrick J. Wong 	return 0;
529338ec570SDarrick J. Wong }
5303a2755afSDarrick J. Wong 
5313a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
5323a2755afSDarrick J. Wong {
5333a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
5343a2755afSDarrick J. Wong 	struct completion *waiting;
5353a2755afSDarrick J. Wong 
5363a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
5373a2755afSDarrick J. Wong 	if (qc->scsicmd) {
5381b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
5391b4d0d8eSJames Bottomley 		unsigned long flags;
5401b4d0d8eSJames Bottomley 
54170b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
542242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
54370b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
5443a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
5453a2755afSDarrick J. Wong 		return;
5463a2755afSDarrick J. Wong 	}
5473a2755afSDarrick J. Wong 
5483a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
5493a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
5503a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
5513a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
5523a2755afSDarrick J. Wong 	waiting = qc->private_data;
5533a2755afSDarrick J. Wong 	complete(waiting);
5543a2755afSDarrick J. Wong }
555b9142174SJames Bottomley 
556b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
557b9142174SJames Bottomley {
558b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
559b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
560b9142174SJames Bottomley 
561b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
562b9142174SJames Bottomley 	     fis->lbal         == 1 &&
563b9142174SJames Bottomley 	     fis->lbam         == 0 &&
564b9142174SJames Bottomley 	     fis->lbah         == 0 &&
565b9142174SJames Bottomley 	     fis->device       == 0)
566b9142174SJames Bottomley 	    ||
567b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
568b9142174SJames Bottomley 	     fis->lbal         == 0 &&
569b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
570b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
571b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
572b9142174SJames Bottomley 
573b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
574b9142174SJames Bottomley 
575b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
576b9142174SJames Bottomley 		  fis->lbal             == 1 &&
577b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
578b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
579b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
580b9142174SJames Bottomley 
581b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
582b9142174SJames Bottomley 
583b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
584b9142174SJames Bottomley 		  fis->lbal         == 1 &&
585b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
586b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
587b9142174SJames Bottomley 		  fis->device       == 0)
588b9142174SJames Bottomley 		||
589b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
590b9142174SJames Bottomley 		  fis->lbal             == 1 &&
591b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
592b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
593b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
594b9142174SJames Bottomley 
595b9142174SJames Bottomley 		/* Treat it as a superset? */
596b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
597b9142174SJames Bottomley }
598b9142174SJames Bottomley 
59987c8331fSDan Williams void sas_probe_sata(struct work_struct *work)
60087c8331fSDan Williams {
60187c8331fSDan Williams 	struct domain_device *dev, *n;
60287c8331fSDan Williams 	struct sas_discovery_event *ev =
60387c8331fSDan Williams 		container_of(work, struct sas_discovery_event, work);
60487c8331fSDan Williams 	struct asd_sas_port *port = ev->port;
60587c8331fSDan Williams 
60687c8331fSDan Williams 	clear_bit(DISCE_PROBE, &port->disc.pending);
60787c8331fSDan Williams 
60887c8331fSDan Williams 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
60987c8331fSDan Williams 		int err;
61087c8331fSDan Williams 
61187c8331fSDan Williams 		spin_lock_irq(&port->dev_list_lock);
61287c8331fSDan Williams 		list_add_tail(&dev->dev_list_node, &port->dev_list);
61387c8331fSDan Williams 		spin_unlock_irq(&port->dev_list_lock);
61487c8331fSDan Williams 
61587c8331fSDan Williams 		err = sas_rphy_add(dev->rphy);
61687c8331fSDan Williams 
61787c8331fSDan Williams 		if (err) {
61887c8331fSDan Williams 			SAS_DPRINTK("%s: for %s device %16llx returned %d\n",
61987c8331fSDan Williams 				    __func__, dev->parent ? "exp-attached" :
62087c8331fSDan Williams 							    "direct-attached",
62187c8331fSDan Williams 				    SAS_ADDR(dev->sas_addr), err);
62287c8331fSDan Williams 			sas_unregister_dev(port, dev);
62387c8331fSDan Williams 		} else
62487c8331fSDan Williams 			list_del_init(&dev->disco_list_node);
62587c8331fSDan Williams 	}
62687c8331fSDan Williams }
62787c8331fSDan Williams 
628b9142174SJames Bottomley /**
629b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
630b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
631b9142174SJames Bottomley  *
632b91bb296SDan Williams  * Devices directly attached to a HA port, have no parents.  All other
633b91bb296SDan Williams  * devices do, and should have their "parent" pointer set appropriately
634b91bb296SDan Williams  * before calling this function.
635b9142174SJames Bottomley  */
636b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
637b9142174SJames Bottomley {
638b9142174SJames Bottomley 	int res;
639b9142174SJames Bottomley 
640b91bb296SDan Williams 	if (dev->dev_type == SATA_PM)
641b91bb296SDan Williams 		return -ENODEV;
642b91bb296SDan Williams 
643b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
644b91bb296SDan Williams 	sas_fill_in_rphy(dev, dev->rphy);
64587c8331fSDan Williams 
64687c8331fSDan Williams 	res = sas_notify_lldd_dev_found(dev);
64787c8331fSDan Williams 	if (res)
64887c8331fSDan Williams 		return res;
64987c8331fSDan Williams 
65087c8331fSDan Williams 	sas_discover_event(dev->port, DISCE_PROBE);
651b91bb296SDan Williams 	return 0;
652b9142174SJames Bottomley }
65300dd4998SJames Bottomley 
65450824d6cSDan Williams static void async_sas_ata_eh(void *data, async_cookie_t cookie)
65550824d6cSDan Williams {
65650824d6cSDan Williams 	struct domain_device *dev = data;
65750824d6cSDan Williams 	struct ata_port *ap = dev->sata_dev.ap;
65850824d6cSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
65950824d6cSDan Williams 
66050824d6cSDan Williams 	ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
66150824d6cSDan Williams 	ata_scsi_port_error_handler(ha->core.shost, ap);
66250824d6cSDan Williams }
66350824d6cSDan Williams 
66400dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
66500dd4998SJames Bottomley {
66600dd4998SJames Bottomley 	struct scsi_device *sdev;
66787c8331fSDan Williams 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
66850824d6cSDan Williams 	LIST_HEAD(async);
66987c8331fSDan Williams 
67087c8331fSDan Williams 	/* it's ok to defer revalidation events during ata eh, these
67187c8331fSDan Williams 	 * disks are in one of three states:
67287c8331fSDan Williams 	 * 1/ present for initial domain discovery, and these
67387c8331fSDan Williams 	 *    resets will cause bcn flutters
67487c8331fSDan Williams 	 * 2/ hot removed, we'll discover that after eh fails
67587c8331fSDan Williams 	 * 3/ hot added after initial discovery, lost the race, and need
67687c8331fSDan Williams 	 *    to catch the next train.
67787c8331fSDan Williams 	 */
67887c8331fSDan Williams 	sas_disable_revalidation(sas_ha);
67900dd4998SJames Bottomley 
68000dd4998SJames Bottomley 	shost_for_each_device(sdev, shost) {
68100dd4998SJames Bottomley 		struct domain_device *ddev = sdev_to_domain_dev(sdev);
68200dd4998SJames Bottomley 
68300dd4998SJames Bottomley 		if (!dev_is_sata(ddev))
68400dd4998SJames Bottomley 			continue;
68500dd4998SJames Bottomley 
68650824d6cSDan Williams 		async_schedule_domain(async_sas_ata_eh, ddev, &async);
68700dd4998SJames Bottomley 	}
68850824d6cSDan Williams 	async_synchronize_full_domain(&async);
68987c8331fSDan Williams 
69087c8331fSDan Williams 	sas_enable_revalidation(sas_ha);
69100dd4998SJames Bottomley }
69200dd4998SJames Bottomley 
69300dd4998SJames Bottomley int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
69400dd4998SJames Bottomley 	       struct list_head *done_q)
69500dd4998SJames Bottomley {
69600dd4998SJames Bottomley 	int rtn = 0;
69700dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
69800dd4998SJames Bottomley 	struct ata_port *ap;
69900dd4998SJames Bottomley 
70000dd4998SJames Bottomley 	do {
70100dd4998SJames Bottomley 		LIST_HEAD(sata_q);
70200dd4998SJames Bottomley 
70300dd4998SJames Bottomley 		ap = NULL;
70400dd4998SJames Bottomley 
70500dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
70600dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
70700dd4998SJames Bottomley 
70800dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
70900dd4998SJames Bottomley 				continue;
71000dd4998SJames Bottomley 			if (ap && ap != ddev->sata_dev.ap)
71100dd4998SJames Bottomley 				continue;
71200dd4998SJames Bottomley 			ap = ddev->sata_dev.ap;
71300dd4998SJames Bottomley 			rtn = 1;
71400dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
71500dd4998SJames Bottomley 		}
71600dd4998SJames Bottomley 
71700dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
71800dd4998SJames Bottomley 			ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
71900dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
720a82058a7SJames Bottomley 			/*
721a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
722a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
723a82058a7SJames Bottomley 			 * about to go out of scope.
724a82058a7SJames Bottomley 			 *
725a82058a7SJames Bottomley 			 * This looks strange, since the commands are
726a82058a7SJames Bottomley 			 * now part of no list, but the next error
727a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
728a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
729a82058a7SJames Bottomley 			 * anyway from the ata tag array.
730a82058a7SJames Bottomley 			 */
731a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
732a82058a7SJames Bottomley 				list_del_init(sata_q.next);
73300dd4998SJames Bottomley 		}
73400dd4998SJames Bottomley 	} while (ap);
73500dd4998SJames Bottomley 
73600dd4998SJames Bottomley 	return rtn;
73700dd4998SJames Bottomley }
738b52df417SDan Williams 
739b52df417SDan Williams void sas_ata_schedule_reset(struct domain_device *dev)
740b52df417SDan Williams {
741b52df417SDan Williams 	struct ata_eh_info *ehi;
742b52df417SDan Williams 	struct ata_port *ap;
743b52df417SDan Williams 	unsigned long flags;
744b52df417SDan Williams 
745b52df417SDan Williams 	if (!dev_is_sata(dev))
746b52df417SDan Williams 		return;
747b52df417SDan Williams 
748b52df417SDan Williams 	ap = dev->sata_dev.ap;
749b52df417SDan Williams 	ehi = &ap->link.eh_info;
750b52df417SDan Williams 
751b52df417SDan Williams 	spin_lock_irqsave(ap->lock, flags);
752b52df417SDan Williams 	ehi->err_mask |= AC_ERR_TIMEOUT;
753b52df417SDan Williams 	ehi->action |= ATA_EH_RESET;
754b52df417SDan Williams 	ata_port_schedule_eh(ap);
755b52df417SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
756b52df417SDan Williams }
75781c757bcSDan Williams 
75881c757bcSDan Williams void sas_ata_wait_eh(struct domain_device *dev)
75981c757bcSDan Williams {
76081c757bcSDan Williams 	struct ata_port *ap;
76181c757bcSDan Williams 
76281c757bcSDan Williams 	if (!dev_is_sata(dev))
76381c757bcSDan Williams 		return;
76481c757bcSDan Williams 
76581c757bcSDan Williams 	ap = dev->sata_dev.ap;
76681c757bcSDan Williams 	ata_port_wait_eh(ap);
76781c757bcSDan Williams }
768