xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision d214d81e883b6fc6f11cc772ff88585565d45cce)
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>
2743a5ab15SDan Williams #include <linux/export.h>
28b9142174SJames Bottomley 
29338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
30338ec570SDarrick J. Wong #include "sas_internal.h"
31338ec570SDarrick J. Wong #include <scsi/scsi_host.h>
32338ec570SDarrick J. Wong #include <scsi/scsi_device.h>
33338ec570SDarrick J. Wong #include <scsi/scsi_tcq.h>
34338ec570SDarrick J. Wong #include <scsi/scsi.h>
35338ec570SDarrick J. Wong #include <scsi/scsi_transport.h>
36338ec570SDarrick J. Wong #include <scsi/scsi_transport_sas.h>
37338ec570SDarrick J. Wong #include "../scsi_sas_internal.h"
383a2755afSDarrick J. Wong #include "../scsi_transport_api.h"
393a2755afSDarrick J. Wong #include <scsi/scsi_eh.h>
40338ec570SDarrick J. Wong 
41338ec570SDarrick J. Wong static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
42338ec570SDarrick J. Wong {
43338ec570SDarrick J. Wong 	/* Cheesy attempt to translate SAS errors into ATA.  Hah! */
44338ec570SDarrick J. Wong 
45338ec570SDarrick J. Wong 	/* transport error */
46338ec570SDarrick J. Wong 	if (ts->resp == SAS_TASK_UNDELIVERED)
47338ec570SDarrick J. Wong 		return AC_ERR_ATA_BUS;
48338ec570SDarrick J. Wong 
49338ec570SDarrick J. Wong 	/* ts->resp == SAS_TASK_COMPLETE */
50338ec570SDarrick J. Wong 	/* task delivered, what happened afterwards? */
51338ec570SDarrick J. Wong 	switch (ts->stat) {
52338ec570SDarrick J. Wong 		case SAS_DEV_NO_RESPONSE:
53338ec570SDarrick J. Wong 			return AC_ERR_TIMEOUT;
54338ec570SDarrick J. Wong 
55338ec570SDarrick J. Wong 		case SAS_INTERRUPTED:
56338ec570SDarrick J. Wong 		case SAS_PHY_DOWN:
57338ec570SDarrick J. Wong 		case SAS_NAK_R_ERR:
58338ec570SDarrick J. Wong 			return AC_ERR_ATA_BUS;
59338ec570SDarrick J. Wong 
60338ec570SDarrick J. Wong 
61338ec570SDarrick J. Wong 		case SAS_DATA_UNDERRUN:
62338ec570SDarrick J. Wong 			/*
63338ec570SDarrick J. Wong 			 * Some programs that use the taskfile interface
64338ec570SDarrick J. Wong 			 * (smartctl in particular) can cause underrun
65338ec570SDarrick J. Wong 			 * problems.  Ignore these errors, perhaps at our
66338ec570SDarrick J. Wong 			 * peril.
67338ec570SDarrick J. Wong 			 */
68338ec570SDarrick J. Wong 			return 0;
69338ec570SDarrick J. Wong 
70338ec570SDarrick J. Wong 		case SAS_DATA_OVERRUN:
71338ec570SDarrick J. Wong 		case SAS_QUEUE_FULL:
72338ec570SDarrick J. Wong 		case SAS_DEVICE_UNKNOWN:
73338ec570SDarrick J. Wong 		case SAS_SG_ERR:
74338ec570SDarrick J. Wong 			return AC_ERR_INVALID;
75338ec570SDarrick J. Wong 
76338ec570SDarrick J. Wong 		case SAS_OPEN_TO:
77338ec570SDarrick J. Wong 		case SAS_OPEN_REJECT:
78338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Saw error %d.  What to do?\n",
79cadbd4a5SHarvey Harrison 				    __func__, ts->stat);
80338ec570SDarrick J. Wong 			return AC_ERR_OTHER;
81338ec570SDarrick J. Wong 
8275c0b386SJames Bottomley 		case SAM_STAT_CHECK_CONDITION:
83338ec570SDarrick J. Wong 		case SAS_ABORTED_TASK:
84338ec570SDarrick J. Wong 			return AC_ERR_DEV;
85338ec570SDarrick J. Wong 
86338ec570SDarrick J. Wong 		case SAS_PROTO_RESPONSE:
87338ec570SDarrick J. Wong 			/* This means the ending_fis has the error
88338ec570SDarrick J. Wong 			 * value; return 0 here to collect it */
89338ec570SDarrick J. Wong 			return 0;
90338ec570SDarrick J. Wong 		default:
91338ec570SDarrick J. Wong 			return 0;
92338ec570SDarrick J. Wong 	}
93338ec570SDarrick J. Wong }
94338ec570SDarrick J. Wong 
95338ec570SDarrick J. Wong static void sas_ata_task_done(struct sas_task *task)
96338ec570SDarrick J. Wong {
97338ec570SDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
989095a64aSDan Williams 	struct domain_device *dev = task->dev;
99338ec570SDarrick J. Wong 	struct task_status_struct *stat = &task->task_status;
100338ec570SDarrick J. Wong 	struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
1019095a64aSDan Williams 	struct sas_ha_struct *sas_ha = dev->port->ha;
102338ec570SDarrick J. Wong 	enum ata_completion_errors ac;
1033eb7a51aSDarrick J. Wong 	unsigned long flags;
104bb650a1bSXiangliang Yu 	struct ata_link *link;
1053dff5721SDan Williams 	struct ata_port *ap;
106338ec570SDarrick J. Wong 
1079095a64aSDan Williams 	spin_lock_irqsave(&dev->done_lock, flags);
1089095a64aSDan Williams 	if (test_bit(SAS_HA_FROZEN, &sas_ha->state))
1099095a64aSDan Williams 		task = NULL;
1109095a64aSDan Williams 	else if (qc && qc->scsicmd)
1119095a64aSDan Williams 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
1129095a64aSDan Williams 	spin_unlock_irqrestore(&dev->done_lock, flags);
1139095a64aSDan Williams 
1149095a64aSDan Williams 	/* check if libsas-eh got to the task before us */
1159095a64aSDan Williams 	if (unlikely(!task))
1169095a64aSDan Williams 		return;
1179095a64aSDan Williams 
1181c50dc83SDarrick J. Wong 	if (!qc)
1191c50dc83SDarrick J. Wong 		goto qc_already_gone;
1201c50dc83SDarrick J. Wong 
1213dff5721SDan Williams 	ap = qc->ap;
1223dff5721SDan Williams 	link = &ap->link;
1231c50dc83SDarrick J. Wong 
1243dff5721SDan Williams 	spin_lock_irqsave(ap->lock, flags);
1253dff5721SDan Williams 	/* check if we lost the race with libata/sas_ata_post_internal() */
1263dff5721SDan Williams 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) {
1273dff5721SDan Williams 		spin_unlock_irqrestore(ap->lock, flags);
1283dff5721SDan Williams 		if (qc->scsicmd)
1293dff5721SDan Williams 			goto qc_already_gone;
1303dff5721SDan Williams 		else {
1313dff5721SDan Williams 			/* if eh is not involved and the port is frozen then the
1323dff5721SDan Williams 			 * ata internal abort process has taken responsibility
1333dff5721SDan Williams 			 * for this sas_task
1343dff5721SDan Williams 			 */
1353dff5721SDan Williams 			return;
1363dff5721SDan Williams 		}
1373dff5721SDan Williams 	}
1383dff5721SDan Williams 
13975c0b386SJames Bottomley 	if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
14075c0b386SJames Bottomley 	    ((stat->stat == SAM_STAT_CHECK_CONDITION &&
14175c0b386SJames Bottomley 	      dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
142338ec570SDarrick J. Wong 		ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
143bb650a1bSXiangliang Yu 
144bb650a1bSXiangliang Yu 		if (!link->sactive) {
145338ec570SDarrick J. Wong 			qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
146bb650a1bSXiangliang Yu 		} else {
147bb650a1bSXiangliang Yu 			link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
148bb650a1bSXiangliang Yu 			if (unlikely(link->eh_info.err_mask))
149bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
150bb650a1bSXiangliang Yu 		}
15175c0b386SJames Bottomley 	} else {
152338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
153338ec570SDarrick J. Wong 		if (ac) {
154cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: SAS error %x\n", __func__,
155338ec570SDarrick J. Wong 				    stat->stat);
156338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
157bb650a1bSXiangliang Yu 			if (!link->sactive) {
158338ec570SDarrick J. Wong 				qc->err_mask = ac;
159bb650a1bSXiangliang Yu 			} else {
160bb650a1bSXiangliang Yu 				link->eh_info.err_mask |= AC_ERR_DEV;
161bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
162bb650a1bSXiangliang Yu 			}
163bb650a1bSXiangliang Yu 
164338ec570SDarrick J. Wong 			dev->sata_dev.tf.feature = 0x04; /* status err */
165338ec570SDarrick J. Wong 			dev->sata_dev.tf.command = ATA_ERR;
166338ec570SDarrick J. Wong 		}
167338ec570SDarrick J. Wong 	}
168338ec570SDarrick J. Wong 
1691c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
170338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1713dff5721SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
1723eb7a51aSDarrick J. Wong 
1731c50dc83SDarrick J. Wong qc_already_gone:
174338ec570SDarrick J. Wong 	list_del_init(&task->list);
175338ec570SDarrick J. Wong 	sas_free_task(task);
176338ec570SDarrick J. Wong }
177338ec570SDarrick J. Wong 
178338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
179338ec570SDarrick J. Wong {
180312d3e56SDan Williams 	unsigned long flags;
181338ec570SDarrick J. Wong 	struct sas_task *task;
182312d3e56SDan Williams 	struct scatterlist *sg;
183312d3e56SDan Williams 	int ret = AC_ERR_SYSTEM;
184312d3e56SDan Williams 	unsigned int si, xfer = 0;
185312d3e56SDan Williams 	struct ata_port *ap = qc->ap;
186312d3e56SDan Williams 	struct domain_device *dev = ap->private_data;
187338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
188338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
189338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
190312d3e56SDan Williams 
191312d3e56SDan Williams 	/* TODO: audit callers to ensure they are ready for qc_issue to
192312d3e56SDan Williams 	 * unconditionally re-enable interrupts
193312d3e56SDan Williams 	 */
194312d3e56SDan Williams 	local_irq_save(flags);
195312d3e56SDan Williams 	spin_unlock(ap->lock);
196338ec570SDarrick J. Wong 
19756dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
198e139942dSDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state))
199312d3e56SDan Williams 		goto out;
20056dd2c06SDarrick J. Wong 
201338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
202338ec570SDarrick J. Wong 	if (!task)
203312d3e56SDan Williams 		goto out;
204338ec570SDarrick J. Wong 	task->dev = dev;
205338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
206338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
207338ec570SDarrick J. Wong 
208338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
209338ec570SDarrick J. Wong 	    qc->tf.command == ATA_CMD_FPDMA_READ) {
210338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
211338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
212338ec570SDarrick J. Wong 	}
213338ec570SDarrick J. Wong 
214110dd8f1SJames Bottomley 	ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
215338ec570SDarrick J. Wong 	task->uldd_task = qc;
216405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
217338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
218dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
219dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
220338ec570SDarrick J. Wong 	} else {
221ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
222338ec570SDarrick J. Wong 			xfer += sg->length;
223338ec570SDarrick J. Wong 
224338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
225ff2aeb1eSTejun Heo 		task->num_scatter = si;
226338ec570SDarrick J. Wong 	}
227338ec570SDarrick J. Wong 
228338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
229ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
230338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
231338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
2321c50dc83SDarrick J. Wong 	qc->lldd_task = task;
233338ec570SDarrick J. Wong 
234338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
235338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
236338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
237338ec570SDarrick J. Wong 		/* fall through */
2380dc36888STejun Heo 	case ATAPI_PROT_DMA:
239338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
240338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
241338ec570SDarrick J. Wong 		break;
242338ec570SDarrick J. Wong 	}
243338ec570SDarrick J. Wong 
244fe059f12SDarrick J. Wong 	if (qc->scsicmd)
245fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
246fe059f12SDarrick J. Wong 
247338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
248312d3e56SDan Williams 		ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
249338ec570SDarrick J. Wong 	else
250312d3e56SDan Williams 		ret = sas_queue_up(task);
251338ec570SDarrick J. Wong 
252338ec570SDarrick J. Wong 	/* Examine */
253312d3e56SDan Williams 	if (ret) {
254312d3e56SDan Williams 		SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
255338ec570SDarrick J. Wong 
256fe059f12SDarrick J. Wong 		if (qc->scsicmd)
257fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
258338ec570SDarrick J. Wong 		sas_free_task(task);
259312d3e56SDan Williams 		ret = AC_ERR_SYSTEM;
260338ec570SDarrick J. Wong 	}
261338ec570SDarrick J. Wong 
262312d3e56SDan Williams  out:
263312d3e56SDan Williams 	spin_lock(ap->lock);
264312d3e56SDan Williams 	local_irq_restore(flags);
265312d3e56SDan Williams 	return ret;
266338ec570SDarrick J. Wong }
267338ec570SDarrick J. Wong 
2684c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2694c9bf4e7STejun Heo {
2704c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2714c9bf4e7STejun Heo 
2724c9bf4e7STejun Heo 	memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
2734c9bf4e7STejun Heo 	return true;
2744c9bf4e7STejun Heo }
2754c9bf4e7STejun Heo 
27636a39947SDan Williams static struct sas_internal *dev_to_sas_internal(struct domain_device *dev)
27736a39947SDan Williams {
27836a39947SDan Williams 	return to_sas_internal(dev->port->ha->core.shost->transportt);
27936a39947SDan Williams }
28036a39947SDan Williams 
28136a39947SDan Williams static int smp_ata_check_ready(struct ata_link *link)
28236a39947SDan Williams {
28336a39947SDan Williams 	int res;
28436a39947SDan Williams 	u8 addr[8];
28536a39947SDan Williams 	struct ata_port *ap = link->ap;
28636a39947SDan Williams 	struct domain_device *dev = ap->private_data;
28736a39947SDan Williams 	struct domain_device *ex_dev = dev->parent;
288f41a0c44SDan Williams 	struct sas_phy *phy = sas_get_local_phy(dev);
28936a39947SDan Williams 
29036a39947SDan Williams 	res = sas_get_phy_attached_sas_addr(ex_dev, phy->number, addr);
291f41a0c44SDan Williams 	sas_put_local_phy(phy);
29236a39947SDan Williams 	/* break the wait early if the expander is unreachable,
29336a39947SDan Williams 	 * otherwise keep polling
29436a39947SDan Williams 	 */
29536a39947SDan Williams 	if (res == -ECOMM)
29636a39947SDan Williams 		return res;
29736a39947SDan Williams 	if (res != SMP_RESP_FUNC_ACC || SAS_ADDR(addr) == 0)
29836a39947SDan Williams 		return 0;
29936a39947SDan Williams 	else
30036a39947SDan Williams 		return 1;
30136a39947SDan Williams }
30236a39947SDan Williams 
30336a39947SDan Williams static int local_ata_check_ready(struct ata_link *link)
304338ec570SDarrick J. Wong {
30500dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
306338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
30736a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
308338ec570SDarrick J. Wong 
30936a39947SDan Williams 	if (i->dft->lldd_ata_check_ready)
31036a39947SDan Williams 		return i->dft->lldd_ata_check_ready(dev);
31136a39947SDan Williams 	else {
31236a39947SDan Williams 		/* lldd's that don't implement 'ready' checking get the
31336a39947SDan Williams 		 * old default behavior of not coordinating reset
31436a39947SDan Williams 		 * recovery with libata
31536a39947SDan Williams 		 */
31636a39947SDan Williams 		return 1;
31736a39947SDan Williams 	}
31800dd4998SJames Bottomley }
319338ec570SDarrick J. Wong 
320*d214d81eSDan Williams static int sas_ata_printk(const char *level, const struct domain_device *ddev,
321*d214d81eSDan Williams 			  const char *fmt, ...)
322*d214d81eSDan Williams {
323*d214d81eSDan Williams 	struct ata_port *ap = ddev->sata_dev.ap;
324*d214d81eSDan Williams 	struct device *dev = &ddev->rphy->dev;
325*d214d81eSDan Williams 	struct va_format vaf;
326*d214d81eSDan Williams 	va_list args;
327*d214d81eSDan Williams 	int r;
328*d214d81eSDan Williams 
329*d214d81eSDan Williams 	va_start(args, fmt);
330*d214d81eSDan Williams 
331*d214d81eSDan Williams 	vaf.fmt = fmt;
332*d214d81eSDan Williams 	vaf.va = &args;
333*d214d81eSDan Williams 
334*d214d81eSDan Williams 	r = printk("%ssas: ata%u: %s: %pV",
335*d214d81eSDan Williams 		   level, ap->print_id, dev_name(dev), &vaf);
336*d214d81eSDan Williams 
337*d214d81eSDan Williams 	va_end(args);
338*d214d81eSDan Williams 
339*d214d81eSDan Williams 	return r;
340*d214d81eSDan Williams }
341*d214d81eSDan Williams 
34236a39947SDan Williams static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
34336a39947SDan Williams 			      unsigned long deadline)
34436a39947SDan Williams {
34536a39947SDan Williams 	int ret = 0, res;
346f41a0c44SDan Williams 	struct sas_phy *phy;
34736a39947SDan Williams 	struct ata_port *ap = link->ap;
34836a39947SDan Williams 	int (*check_ready)(struct ata_link *link);
34936a39947SDan Williams 	struct domain_device *dev = ap->private_data;
35036a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
35136a39947SDan Williams 
352cb48d672SDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state))
353cb48d672SDan Williams 		return -ENODEV;
354cb48d672SDan Williams 
35536a39947SDan Williams 	res = i->dft->lldd_I_T_nexus_reset(dev);
35636a39947SDan Williams 
35736a39947SDan Williams 	if (res != TMF_RESP_FUNC_COMPLETE)
358*d214d81eSDan Williams 		sas_ata_printk(KERN_DEBUG, dev, "Unable to reset ata device?\n");
35936a39947SDan Williams 
360f41a0c44SDan Williams 	phy = sas_get_local_phy(dev);
36136a39947SDan Williams 	if (scsi_is_sas_phy_local(phy))
36236a39947SDan Williams 		check_ready = local_ata_check_ready;
36336a39947SDan Williams 	else
36436a39947SDan Williams 		check_ready = smp_ata_check_ready;
365f41a0c44SDan Williams 	sas_put_local_phy(phy);
36636a39947SDan Williams 
36736a39947SDan Williams 	ret = ata_wait_after_reset(link, deadline, check_ready);
36836a39947SDan Williams 	if (ret && ret != -EAGAIN)
369*d214d81eSDan Williams 		sas_ata_printk(KERN_ERR, dev, "reset failed (errno=%d)\n", ret);
37036a39947SDan Williams 
37136a39947SDan Williams 	/* XXX: if the class changes during the reset the upper layer
37236a39947SDan Williams 	 * should be informed, if the device has gone away we assume
37336a39947SDan Williams 	 * libsas will eventually delete it
37436a39947SDan Williams 	 */
375338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
376338ec570SDarrick J. Wong 	case ATA_COMMAND_SET:
37700dd4998SJames Bottomley 		*class = ATA_DEV_ATA;
378338ec570SDarrick J. Wong 		break;
379338ec570SDarrick J. Wong 	case ATAPI_COMMAND_SET:
38000dd4998SJames Bottomley 		*class = ATA_DEV_ATAPI;
381338ec570SDarrick J. Wong 		break;
382338ec570SDarrick J. Wong 	}
383338ec570SDarrick J. Wong 
384338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
38500dd4998SJames Bottomley 	return ret;
386338ec570SDarrick J. Wong }
387338ec570SDarrick J. Wong 
3881ca1e43eSDave Jiang static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
3891ca1e43eSDave Jiang 			       unsigned long deadline)
3901ca1e43eSDave Jiang {
3911ca1e43eSDave Jiang 	struct ata_port *ap = link->ap;
3921ca1e43eSDave Jiang 	struct domain_device *dev = ap->private_data;
39336a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
3941ca1e43eSDave Jiang 	int res = TMF_RESP_FUNC_FAILED;
3951ca1e43eSDave Jiang 	int ret = 0;
3961ca1e43eSDave Jiang 
3971ca1e43eSDave Jiang 	if (i->dft->lldd_ata_soft_reset)
3981ca1e43eSDave Jiang 		res = i->dft->lldd_ata_soft_reset(dev);
3991ca1e43eSDave Jiang 
4001ca1e43eSDave Jiang 	if (res != TMF_RESP_FUNC_COMPLETE) {
4011ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
4021ca1e43eSDave Jiang 		ret = -EAGAIN;
4031ca1e43eSDave Jiang 	}
4041ca1e43eSDave Jiang 
4051ca1e43eSDave Jiang 	switch (dev->sata_dev.command_set) {
4061ca1e43eSDave Jiang 	case ATA_COMMAND_SET:
4071ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Found ATA device.\n", __func__);
4081ca1e43eSDave Jiang 		*class = ATA_DEV_ATA;
4091ca1e43eSDave Jiang 		break;
4101ca1e43eSDave Jiang 	case ATAPI_COMMAND_SET:
4111ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
4121ca1e43eSDave Jiang 		*class = ATA_DEV_ATAPI;
4131ca1e43eSDave Jiang 		break;
4141ca1e43eSDave Jiang 	default:
4151ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
4161ca1e43eSDave Jiang 			    __func__, dev->sata_dev.command_set);
4171ca1e43eSDave Jiang 		*class = ATA_DEV_UNKNOWN;
4181ca1e43eSDave Jiang 		break;
4191ca1e43eSDave Jiang 	}
4201ca1e43eSDave Jiang 
4211ca1e43eSDave Jiang 	ap->cbl = ATA_CBL_SATA;
4221ca1e43eSDave Jiang 	return ret;
4231ca1e43eSDave Jiang }
4241ca1e43eSDave Jiang 
4253dff5721SDan Williams /*
4263dff5721SDan Williams  * notify the lldd to forget the sas_task for this internal ata command
4273dff5721SDan Williams  * that bypasses scsi-eh
4283dff5721SDan Williams  */
4293dff5721SDan Williams static void sas_ata_internal_abort(struct sas_task *task)
4303dff5721SDan Williams {
43136a39947SDan Williams 	struct sas_internal *si = dev_to_sas_internal(task->dev);
4323dff5721SDan Williams 	unsigned long flags;
4333dff5721SDan Williams 	int res;
4343dff5721SDan Williams 
4353dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4363dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
4373dff5721SDan Williams 	    task->task_state_flags & SAS_TASK_STATE_DONE) {
4383dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4393dff5721SDan Williams 		SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
4403dff5721SDan Williams 			    task);
4413dff5721SDan Williams 		goto out;
4423dff5721SDan Williams 	}
4433dff5721SDan Williams 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
4443dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4453dff5721SDan Williams 
4463dff5721SDan Williams 	res = si->dft->lldd_abort_task(task);
4473dff5721SDan Williams 
4483dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4493dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_DONE ||
4503dff5721SDan Williams 	    res == TMF_RESP_FUNC_COMPLETE) {
4513dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4523dff5721SDan Williams 		goto out;
4533dff5721SDan Williams 	}
4543dff5721SDan Williams 
4553dff5721SDan Williams 	/* XXX we are not prepared to deal with ->lldd_abort_task()
4563dff5721SDan Williams 	 * failures.  TODO: lldds need to unconditionally forget about
4573dff5721SDan Williams 	 * aborted ata tasks, otherwise we (likely) leak the sas task
4583dff5721SDan Williams 	 * here
4593dff5721SDan Williams 	 */
4603dff5721SDan Williams 	SAS_DPRINTK("%s: Task %p leaked.\n", __func__, task);
4613dff5721SDan Williams 
4623dff5721SDan Williams 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
4633dff5721SDan Williams 		task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
4643dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4653dff5721SDan Williams 
4663dff5721SDan Williams 	return;
4673dff5721SDan Williams  out:
4683dff5721SDan Williams 	list_del_init(&task->list);
4693dff5721SDan Williams 	sas_free_task(task);
4703dff5721SDan Williams }
4713dff5721SDan Williams 
472338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
473338ec570SDarrick J. Wong {
474338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
475338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
476338ec570SDarrick J. Wong 
4771c50dc83SDarrick J. Wong 	if (qc->err_mask) {
4781c50dc83SDarrick J. Wong 		/*
4793dff5721SDan Williams 		 * Find the sas_task and kill it.  By this point, libata
4803dff5721SDan Williams 		 * has decided to kill the qc and has frozen the port.
4813dff5721SDan Williams 		 * In this state sas_ata_task_done() will no longer free
4823dff5721SDan Williams 		 * the sas_task, so we need to notify the lldd (via
4833dff5721SDan Williams 		 * ->lldd_abort_task) that the task is dead and free it
4843dff5721SDan Williams 		 *  ourselves.
4851c50dc83SDarrick J. Wong 		 */
4861c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
4871c50dc83SDarrick J. Wong 
4881c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
4893a2cdf39SDan Williams 		if (!task)
4903a2cdf39SDan Williams 			return;
4911c50dc83SDarrick J. Wong 		task->uldd_task = NULL;
4923dff5721SDan Williams 		sas_ata_internal_abort(task);
4931c50dc83SDarrick J. Wong 	}
4941c50dc83SDarrick J. Wong }
495338ec570SDarrick J. Wong 
496b91bb296SDan Williams 
497b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
498b91bb296SDan Williams {
499b91bb296SDan Williams 	struct domain_device *dev = ap->private_data;
50036a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
501b91bb296SDan Williams 
502b91bb296SDan Williams 	if (i->dft->lldd_ata_set_dmamode)
503b91bb296SDan Williams 		i->dft->lldd_ata_set_dmamode(dev);
504b91bb296SDan Williams }
505b91bb296SDan Williams 
506338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
50700dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
5081ca1e43eSDave Jiang 	.softreset		= sas_ata_soft_reset,
50900dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
51000dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
51100dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
512338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
513f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
514338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
515338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
5164c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
517338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
518338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
519b91bb296SDan Williams 	.set_dmamode		= sas_ata_set_dmamode,
520338ec570SDarrick J. Wong };
521338ec570SDarrick J. Wong 
522338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
5239cbe056fSSergei Shtylyov 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
5240f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
5250f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
526338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
527338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
528338ec570SDarrick J. Wong };
529338ec570SDarrick J. Wong 
530338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
531338ec570SDarrick J. Wong 			       struct scsi_target *starget)
532338ec570SDarrick J. Wong {
533338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
534338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
535338ec570SDarrick J. Wong 	struct ata_port *ap;
536338ec570SDarrick J. Wong 
537338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
5381d1bbee6SJeff Garzik 		      ha->dev,
539338ec570SDarrick J. Wong 		      sata_port_info.flags,
540338ec570SDarrick J. Wong 		      &sas_sata_ops);
541338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
542338ec570SDarrick J. Wong 				&sata_port_info,
543338ec570SDarrick J. Wong 				shost);
544338ec570SDarrick J. Wong 	if (!ap) {
545338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
546338ec570SDarrick J. Wong 		return -ENODEV;
547338ec570SDarrick J. Wong 	}
548338ec570SDarrick J. Wong 
549338ec570SDarrick J. Wong 	ap->private_data = found_dev;
550338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
551338ec570SDarrick J. Wong 	ap->scsi_host = shost;
552338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
553338ec570SDarrick J. Wong 
554338ec570SDarrick J. Wong 	return 0;
555338ec570SDarrick J. Wong }
5563a2755afSDarrick J. Wong 
5573a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
5583a2755afSDarrick J. Wong {
5593a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
5603a2755afSDarrick J. Wong 	struct completion *waiting;
5613a2755afSDarrick J. Wong 
5623a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
5633a2755afSDarrick J. Wong 	if (qc->scsicmd) {
5641b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
5651b4d0d8eSJames Bottomley 		unsigned long flags;
5661b4d0d8eSJames Bottomley 
56770b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
568242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
56970b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
5703a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
5713a2755afSDarrick J. Wong 		return;
5723a2755afSDarrick J. Wong 	}
5733a2755afSDarrick J. Wong 
5743a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
5753a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
5763a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
5773a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
5783a2755afSDarrick J. Wong 	waiting = qc->private_data;
5793a2755afSDarrick J. Wong 	complete(waiting);
5803a2755afSDarrick J. Wong }
581b9142174SJames Bottomley 
582b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
583b9142174SJames Bottomley {
584b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
585b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
586b9142174SJames Bottomley 
587b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
588b9142174SJames Bottomley 	     fis->lbal         == 1 &&
589b9142174SJames Bottomley 	     fis->lbam         == 0 &&
590b9142174SJames Bottomley 	     fis->lbah         == 0 &&
591b9142174SJames Bottomley 	     fis->device       == 0)
592b9142174SJames Bottomley 	    ||
593b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
594b9142174SJames Bottomley 	     fis->lbal         == 0 &&
595b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
596b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
597b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
598b9142174SJames Bottomley 
599b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
600b9142174SJames Bottomley 
601b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
602b9142174SJames Bottomley 		  fis->lbal             == 1 &&
603b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
604b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
605b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
606b9142174SJames Bottomley 
607b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
608b9142174SJames Bottomley 
609b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
610b9142174SJames Bottomley 		  fis->lbal         == 1 &&
611b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
612b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
613b9142174SJames Bottomley 		  fis->device       == 0)
614b9142174SJames Bottomley 		||
615b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
616b9142174SJames Bottomley 		  fis->lbal             == 1 &&
617b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
618b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
619b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
620b9142174SJames Bottomley 
621b9142174SJames Bottomley 		/* Treat it as a superset? */
622b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
623b9142174SJames Bottomley }
624b9142174SJames Bottomley 
62587c8331fSDan Williams void sas_probe_sata(struct work_struct *work)
62687c8331fSDan Williams {
62787c8331fSDan Williams 	struct domain_device *dev, *n;
62887c8331fSDan Williams 	struct sas_discovery_event *ev =
62987c8331fSDan Williams 		container_of(work, struct sas_discovery_event, work);
63087c8331fSDan Williams 	struct asd_sas_port *port = ev->port;
63187c8331fSDan Williams 
63287c8331fSDan Williams 	clear_bit(DISCE_PROBE, &port->disc.pending);
63387c8331fSDan Williams 
63487c8331fSDan Williams 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
63587c8331fSDan Williams 		int err;
63687c8331fSDan Williams 
63787c8331fSDan Williams 		spin_lock_irq(&port->dev_list_lock);
63887c8331fSDan Williams 		list_add_tail(&dev->dev_list_node, &port->dev_list);
63987c8331fSDan Williams 		spin_unlock_irq(&port->dev_list_lock);
64087c8331fSDan Williams 
64187c8331fSDan Williams 		err = sas_rphy_add(dev->rphy);
64287c8331fSDan Williams 
64387c8331fSDan Williams 		if (err) {
64487c8331fSDan Williams 			SAS_DPRINTK("%s: for %s device %16llx returned %d\n",
64587c8331fSDan Williams 				    __func__, dev->parent ? "exp-attached" :
64687c8331fSDan Williams 							    "direct-attached",
64787c8331fSDan Williams 				    SAS_ADDR(dev->sas_addr), err);
64887c8331fSDan Williams 			sas_unregister_dev(port, dev);
64987c8331fSDan Williams 		} else
65087c8331fSDan Williams 			list_del_init(&dev->disco_list_node);
65187c8331fSDan Williams 	}
65287c8331fSDan Williams }
65387c8331fSDan Williams 
654b9142174SJames Bottomley /**
655b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
656b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
657b9142174SJames Bottomley  *
658b91bb296SDan Williams  * Devices directly attached to a HA port, have no parents.  All other
659b91bb296SDan Williams  * devices do, and should have their "parent" pointer set appropriately
660b91bb296SDan Williams  * before calling this function.
661b9142174SJames Bottomley  */
662b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
663b9142174SJames Bottomley {
664b9142174SJames Bottomley 	int res;
665b9142174SJames Bottomley 
666b91bb296SDan Williams 	if (dev->dev_type == SATA_PM)
667b91bb296SDan Williams 		return -ENODEV;
668b91bb296SDan Williams 
669b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
670b91bb296SDan Williams 	sas_fill_in_rphy(dev, dev->rphy);
67187c8331fSDan Williams 
67287c8331fSDan Williams 	res = sas_notify_lldd_dev_found(dev);
67387c8331fSDan Williams 	if (res)
67487c8331fSDan Williams 		return res;
67587c8331fSDan Williams 
67687c8331fSDan Williams 	sas_discover_event(dev->port, DISCE_PROBE);
677b91bb296SDan Williams 	return 0;
678b9142174SJames Bottomley }
67900dd4998SJames Bottomley 
68050824d6cSDan Williams static void async_sas_ata_eh(void *data, async_cookie_t cookie)
68150824d6cSDan Williams {
68250824d6cSDan Williams 	struct domain_device *dev = data;
68350824d6cSDan Williams 	struct ata_port *ap = dev->sata_dev.ap;
68450824d6cSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
68550824d6cSDan Williams 
6868abda4d2SDan Williams 	/* hold a reference over eh since we may be racing with final
6878abda4d2SDan Williams 	 * remove once all commands are completed
6888abda4d2SDan Williams 	 */
6898abda4d2SDan Williams 	kref_get(&dev->kref);
690*d214d81eSDan Williams 	sas_ata_printk(KERN_DEBUG, dev, "dev error handler\n");
69150824d6cSDan Williams 	ata_scsi_port_error_handler(ha->core.shost, ap);
6928abda4d2SDan Williams 	sas_put_device(dev);
69350824d6cSDan Williams }
69450824d6cSDan Williams 
69500dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
69600dd4998SJames Bottomley {
69700dd4998SJames Bottomley 	struct scsi_device *sdev;
69887c8331fSDan Williams 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
69950824d6cSDan Williams 	LIST_HEAD(async);
70087c8331fSDan Williams 
70187c8331fSDan Williams 	/* it's ok to defer revalidation events during ata eh, these
70287c8331fSDan Williams 	 * disks are in one of three states:
70387c8331fSDan Williams 	 * 1/ present for initial domain discovery, and these
70487c8331fSDan Williams 	 *    resets will cause bcn flutters
70587c8331fSDan Williams 	 * 2/ hot removed, we'll discover that after eh fails
70687c8331fSDan Williams 	 * 3/ hot added after initial discovery, lost the race, and need
70787c8331fSDan Williams 	 *    to catch the next train.
70887c8331fSDan Williams 	 */
70987c8331fSDan Williams 	sas_disable_revalidation(sas_ha);
71000dd4998SJames Bottomley 
71100dd4998SJames Bottomley 	shost_for_each_device(sdev, shost) {
71200dd4998SJames Bottomley 		struct domain_device *ddev = sdev_to_domain_dev(sdev);
71300dd4998SJames Bottomley 
71400dd4998SJames Bottomley 		if (!dev_is_sata(ddev))
71500dd4998SJames Bottomley 			continue;
71600dd4998SJames Bottomley 
71750824d6cSDan Williams 		async_schedule_domain(async_sas_ata_eh, ddev, &async);
71800dd4998SJames Bottomley 	}
71950824d6cSDan Williams 	async_synchronize_full_domain(&async);
72087c8331fSDan Williams 
72187c8331fSDan Williams 	sas_enable_revalidation(sas_ha);
72200dd4998SJames Bottomley }
72300dd4998SJames Bottomley 
724d230ce69SDan Williams void sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
72500dd4998SJames Bottomley 		struct list_head *done_q)
72600dd4998SJames Bottomley {
72700dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
728*d214d81eSDan Williams 	struct domain_device *eh_dev;
72900dd4998SJames Bottomley 
73000dd4998SJames Bottomley 	do {
73100dd4998SJames Bottomley 		LIST_HEAD(sata_q);
732*d214d81eSDan Williams 		eh_dev = NULL;
73300dd4998SJames Bottomley 
73400dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
73500dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
73600dd4998SJames Bottomley 
73700dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
73800dd4998SJames Bottomley 				continue;
739*d214d81eSDan Williams 			if (eh_dev && eh_dev != ddev)
74000dd4998SJames Bottomley 				continue;
741*d214d81eSDan Williams 			eh_dev = ddev;
74200dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
74300dd4998SJames Bottomley 		}
74400dd4998SJames Bottomley 
74500dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
746*d214d81eSDan Williams 			struct ata_port *ap = eh_dev->sata_dev.ap;
747*d214d81eSDan Williams 
748*d214d81eSDan Williams 			sas_ata_printk(KERN_DEBUG, eh_dev, "cmd error handler\n");
74900dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
750a82058a7SJames Bottomley 			/*
751a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
752a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
753a82058a7SJames Bottomley 			 * about to go out of scope.
754a82058a7SJames Bottomley 			 *
755a82058a7SJames Bottomley 			 * This looks strange, since the commands are
756a82058a7SJames Bottomley 			 * now part of no list, but the next error
757a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
758a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
759a82058a7SJames Bottomley 			 * anyway from the ata tag array.
760a82058a7SJames Bottomley 			 */
761a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
762a82058a7SJames Bottomley 				list_del_init(sata_q.next);
76300dd4998SJames Bottomley 		}
764*d214d81eSDan Williams 	} while (eh_dev);
76500dd4998SJames Bottomley }
766b52df417SDan Williams 
767b52df417SDan Williams void sas_ata_schedule_reset(struct domain_device *dev)
768b52df417SDan Williams {
769b52df417SDan Williams 	struct ata_eh_info *ehi;
770b52df417SDan Williams 	struct ata_port *ap;
771b52df417SDan Williams 	unsigned long flags;
772b52df417SDan Williams 
773b52df417SDan Williams 	if (!dev_is_sata(dev))
774b52df417SDan Williams 		return;
775b52df417SDan Williams 
776b52df417SDan Williams 	ap = dev->sata_dev.ap;
777b52df417SDan Williams 	ehi = &ap->link.eh_info;
778b52df417SDan Williams 
779b52df417SDan Williams 	spin_lock_irqsave(ap->lock, flags);
780b52df417SDan Williams 	ehi->err_mask |= AC_ERR_TIMEOUT;
781b52df417SDan Williams 	ehi->action |= ATA_EH_RESET;
782b52df417SDan Williams 	ata_port_schedule_eh(ap);
783b52df417SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
784b52df417SDan Williams }
78543a5ab15SDan Williams EXPORT_SYMBOL_GPL(sas_ata_schedule_reset);
78681c757bcSDan Williams 
78781c757bcSDan Williams void sas_ata_wait_eh(struct domain_device *dev)
78881c757bcSDan Williams {
78981c757bcSDan Williams 	struct ata_port *ap;
79081c757bcSDan Williams 
79181c757bcSDan Williams 	if (!dev_is_sata(dev))
79281c757bcSDan Williams 		return;
79381c757bcSDan Williams 
79481c757bcSDan Williams 	ap = dev->sata_dev.ap;
79581c757bcSDan Williams 	ata_port_wait_eh(ap);
79681c757bcSDan Williams }
797