xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision 2da11d4262639dc0e2fabc6a70886db57af25c43)
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 &&
1411cbd772dSHannes Reinecke 	      dev->sata_dev.class == ATA_DEV_ATAPI))) {
1426ef1b512SDan Williams 		memcpy(dev->sata_dev.fis, resp->ending_fis, ATA_RESP_FIS_SIZE);
143bb650a1bSXiangliang Yu 
144bb650a1bSXiangliang Yu 		if (!link->sactive) {
1456ef1b512SDan Williams 			qc->err_mask |= ac_err_mask(dev->sata_dev.fis[2]);
146bb650a1bSXiangliang Yu 		} else {
1476ef1b512SDan Williams 			link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.fis[2]);
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 
1646ef1b512SDan Williams 			dev->sata_dev.fis[3] = 0x04; /* status err */
1656ef1b512SDan Williams 			dev->sata_dev.fis[2] = 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 	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 {
179338ec570SDarrick J. Wong 	struct sas_task *task;
180312d3e56SDan Williams 	struct scatterlist *sg;
181312d3e56SDan Williams 	int ret = AC_ERR_SYSTEM;
182312d3e56SDan Williams 	unsigned int si, xfer = 0;
183312d3e56SDan Williams 	struct ata_port *ap = qc->ap;
184312d3e56SDan Williams 	struct domain_device *dev = ap->private_data;
185338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
186338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
187338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
188312d3e56SDan Williams 
189*2da11d42SSebastian Andrzej Siewior 	/* TODO: we should try to remove that unlock */
190312d3e56SDan Williams 	spin_unlock(ap->lock);
191338ec570SDarrick J. Wong 
19256dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
193e139942dSDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state))
194312d3e56SDan Williams 		goto out;
19556dd2c06SDarrick J. Wong 
196338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
197338ec570SDarrick J. Wong 	if (!task)
198312d3e56SDan Williams 		goto out;
199338ec570SDarrick J. Wong 	task->dev = dev;
200338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
201338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
202338ec570SDarrick J. Wong 
203338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
204ef026b18SHannes Reinecke 	    qc->tf.command == ATA_CMD_FPDMA_READ ||
205ef026b18SHannes Reinecke 	    qc->tf.command == ATA_CMD_FPDMA_RECV ||
206661ce1f0SHannes Reinecke 	    qc->tf.command == ATA_CMD_FPDMA_SEND ||
207661ce1f0SHannes Reinecke 	    qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
208338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
209338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
210338ec570SDarrick J. Wong 	}
211338ec570SDarrick J. Wong 
212ae5fbae0SDan Williams 	ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *)&task->ata_task.fis);
213338ec570SDarrick J. Wong 	task->uldd_task = qc;
214405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
215338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
216dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
217dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
218338ec570SDarrick J. Wong 	} else {
219ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
2209702c67cSJohn Garry 			xfer += sg_dma_len(sg);
221338ec570SDarrick J. Wong 
222338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
223ff2aeb1eSTejun Heo 		task->num_scatter = si;
224338ec570SDarrick J. Wong 	}
225338ec570SDarrick J. Wong 
226338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
227ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
228338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
229338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
2301c50dc83SDarrick J. Wong 	qc->lldd_task = task;
231338ec570SDarrick J. Wong 
232b38d4d85SHannes Reinecke 	task->ata_task.use_ncq = ata_is_ncq(qc->tf.protocol);
233b38d4d85SHannes Reinecke 	task->ata_task.dma_xfer = ata_is_dma(qc->tf.protocol);
234338ec570SDarrick J. Wong 
235fe059f12SDarrick J. Wong 	if (qc->scsicmd)
236fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
237fe059f12SDarrick J. Wong 
23879855d17SChristoph Hellwig 	ret = i->dft->lldd_execute_task(task, GFP_ATOMIC);
239312d3e56SDan Williams 	if (ret) {
240312d3e56SDan Williams 		SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
241338ec570SDarrick J. Wong 
242fe059f12SDarrick J. Wong 		if (qc->scsicmd)
243fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
244338ec570SDarrick J. Wong 		sas_free_task(task);
245354a086dSWei Fang 		qc->lldd_task = NULL;
246312d3e56SDan Williams 		ret = AC_ERR_SYSTEM;
247338ec570SDarrick J. Wong 	}
248338ec570SDarrick J. Wong 
249312d3e56SDan Williams  out:
250312d3e56SDan Williams 	spin_lock(ap->lock);
251312d3e56SDan Williams 	return ret;
252338ec570SDarrick J. Wong }
253338ec570SDarrick J. Wong 
2544c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2554c9bf4e7STejun Heo {
2564c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2574c9bf4e7STejun Heo 
2586ef1b512SDan Williams 	ata_tf_from_fis(dev->sata_dev.fis, &qc->result_tf);
2594c9bf4e7STejun Heo 	return true;
2604c9bf4e7STejun Heo }
2614c9bf4e7STejun Heo 
26236a39947SDan Williams static struct sas_internal *dev_to_sas_internal(struct domain_device *dev)
26336a39947SDan Williams {
26436a39947SDan Williams 	return to_sas_internal(dev->port->ha->core.shost->transportt);
26536a39947SDan Williams }
26636a39947SDan Williams 
2671cbd772dSHannes Reinecke static int sas_get_ata_command_set(struct domain_device *dev);
268354cf829SDan Williams 
269354cf829SDan Williams int sas_get_ata_info(struct domain_device *dev, struct ex_phy *phy)
270354cf829SDan Williams {
271354cf829SDan Williams 	if (phy->attached_tproto & SAS_PROTOCOL_STP)
272354cf829SDan Williams 		dev->tproto = phy->attached_tproto;
273354cf829SDan Williams 	if (phy->attached_sata_dev)
274aa9f8328SJames Bottomley 		dev->tproto |= SAS_SATA_DEV;
275354cf829SDan Williams 
276aa9f8328SJames Bottomley 	if (phy->attached_dev_type == SAS_SATA_PENDING)
277aa9f8328SJames Bottomley 		dev->dev_type = SAS_SATA_PENDING;
278354cf829SDan Williams 	else {
279354cf829SDan Williams 		int res;
280354cf829SDan Williams 
281aa9f8328SJames Bottomley 		dev->dev_type = SAS_SATA_DEV;
282354cf829SDan Williams 		res = sas_get_report_phy_sata(dev->parent, phy->phy_id,
283354cf829SDan Williams 					      &dev->sata_dev.rps_resp);
284354cf829SDan Williams 		if (res) {
285354cf829SDan Williams 			SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
286354cf829SDan Williams 				    "0x%x\n", SAS_ADDR(dev->parent->sas_addr),
287354cf829SDan Williams 				    phy->phy_id, res);
288354cf829SDan Williams 			return res;
289354cf829SDan Williams 		}
290354cf829SDan Williams 		memcpy(dev->frame_rcvd, &dev->sata_dev.rps_resp.rps.fis,
291354cf829SDan Williams 		       sizeof(struct dev_to_host_fis));
2921cbd772dSHannes Reinecke 		dev->sata_dev.class = sas_get_ata_command_set(dev);
293354cf829SDan Williams 	}
294354cf829SDan Williams 	return 0;
295354cf829SDan Williams }
296354cf829SDan Williams 
297354cf829SDan Williams static int sas_ata_clear_pending(struct domain_device *dev, struct ex_phy *phy)
298354cf829SDan Williams {
299354cf829SDan Williams 	int res;
300354cf829SDan Williams 
301354cf829SDan Williams 	/* we weren't pending, so successfully end the reset sequence now */
302aa9f8328SJames Bottomley 	if (dev->dev_type != SAS_SATA_PENDING)
303354cf829SDan Williams 		return 1;
304354cf829SDan Williams 
305354cf829SDan Williams 	/* hmmm, if this succeeds do we need to repost the domain_device to the
306354cf829SDan Williams 	 * lldd so it can pick up new parameters?
307354cf829SDan Williams 	 */
308354cf829SDan Williams 	res = sas_get_ata_info(dev, phy);
309354cf829SDan Williams 	if (res)
310354cf829SDan Williams 		return 0; /* retry */
311354cf829SDan Williams 	else
312354cf829SDan Williams 		return 1;
313354cf829SDan Williams }
314354cf829SDan Williams 
31536a39947SDan Williams static int smp_ata_check_ready(struct ata_link *link)
31636a39947SDan Williams {
31736a39947SDan Williams 	int res;
31836a39947SDan Williams 	struct ata_port *ap = link->ap;
31936a39947SDan Williams 	struct domain_device *dev = ap->private_data;
32036a39947SDan Williams 	struct domain_device *ex_dev = dev->parent;
321f41a0c44SDan Williams 	struct sas_phy *phy = sas_get_local_phy(dev);
322354cf829SDan Williams 	struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy->number];
32336a39947SDan Williams 
324354cf829SDan Williams 	res = sas_ex_phy_discover(ex_dev, phy->number);
325f41a0c44SDan Williams 	sas_put_local_phy(phy);
326354cf829SDan Williams 
32736a39947SDan Williams 	/* break the wait early if the expander is unreachable,
32836a39947SDan Williams 	 * otherwise keep polling
32936a39947SDan Williams 	 */
33036a39947SDan Williams 	if (res == -ECOMM)
33136a39947SDan Williams 		return res;
332354cf829SDan Williams 	if (res != SMP_RESP_FUNC_ACC)
33336a39947SDan Williams 		return 0;
334354cf829SDan Williams 
335354cf829SDan Williams 	switch (ex_phy->attached_dev_type) {
336aa9f8328SJames Bottomley 	case SAS_SATA_PENDING:
337354cf829SDan Williams 		return 0;
338aa9f8328SJames Bottomley 	case SAS_END_DEVICE:
339354cf829SDan Williams 		if (ex_phy->attached_sata_dev)
340354cf829SDan Williams 			return sas_ata_clear_pending(dev, ex_phy);
341e656f0d0SBart Van Assche 		/* fall through */
342354cf829SDan Williams 	default:
343354cf829SDan Williams 		return -ENODEV;
344354cf829SDan Williams 	}
34536a39947SDan Williams }
34636a39947SDan Williams 
34736a39947SDan Williams static int local_ata_check_ready(struct ata_link *link)
348338ec570SDarrick J. Wong {
34900dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
350338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
35136a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
352338ec570SDarrick J. Wong 
35336a39947SDan Williams 	if (i->dft->lldd_ata_check_ready)
35436a39947SDan Williams 		return i->dft->lldd_ata_check_ready(dev);
35536a39947SDan Williams 	else {
35636a39947SDan Williams 		/* lldd's that don't implement 'ready' checking get the
35736a39947SDan Williams 		 * old default behavior of not coordinating reset
35836a39947SDan Williams 		 * recovery with libata
35936a39947SDan Williams 		 */
36036a39947SDan Williams 		return 1;
36136a39947SDan Williams 	}
36200dd4998SJames Bottomley }
363338ec570SDarrick J. Wong 
364d214d81eSDan Williams static int sas_ata_printk(const char *level, const struct domain_device *ddev,
365d214d81eSDan Williams 			  const char *fmt, ...)
366d214d81eSDan Williams {
367d214d81eSDan Williams 	struct ata_port *ap = ddev->sata_dev.ap;
368d214d81eSDan Williams 	struct device *dev = &ddev->rphy->dev;
369d214d81eSDan Williams 	struct va_format vaf;
370d214d81eSDan Williams 	va_list args;
371d214d81eSDan Williams 	int r;
372d214d81eSDan Williams 
373d214d81eSDan Williams 	va_start(args, fmt);
374d214d81eSDan Williams 
375d214d81eSDan Williams 	vaf.fmt = fmt;
376d214d81eSDan Williams 	vaf.va = &args;
377d214d81eSDan Williams 
378d214d81eSDan Williams 	r = printk("%ssas: ata%u: %s: %pV",
379d214d81eSDan Williams 		   level, ap->print_id, dev_name(dev), &vaf);
380d214d81eSDan Williams 
381d214d81eSDan Williams 	va_end(args);
382d214d81eSDan Williams 
383d214d81eSDan Williams 	return r;
384d214d81eSDan Williams }
385d214d81eSDan Williams 
38636a39947SDan Williams static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
38736a39947SDan Williams 			      unsigned long deadline)
38836a39947SDan Williams {
38936a39947SDan Williams 	int ret = 0, res;
390f41a0c44SDan Williams 	struct sas_phy *phy;
39136a39947SDan Williams 	struct ata_port *ap = link->ap;
39236a39947SDan Williams 	int (*check_ready)(struct ata_link *link);
39336a39947SDan Williams 	struct domain_device *dev = ap->private_data;
39436a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
39536a39947SDan Williams 
39636a39947SDan Williams 	res = i->dft->lldd_I_T_nexus_reset(dev);
39726a2e68fSDan Williams 	if (res == -ENODEV)
39826a2e68fSDan Williams 		return res;
39936a39947SDan Williams 
40036a39947SDan Williams 	if (res != TMF_RESP_FUNC_COMPLETE)
401d214d81eSDan Williams 		sas_ata_printk(KERN_DEBUG, dev, "Unable to reset ata device?\n");
40236a39947SDan Williams 
403f41a0c44SDan Williams 	phy = sas_get_local_phy(dev);
40436a39947SDan Williams 	if (scsi_is_sas_phy_local(phy))
40536a39947SDan Williams 		check_ready = local_ata_check_ready;
40636a39947SDan Williams 	else
40736a39947SDan Williams 		check_ready = smp_ata_check_ready;
408f41a0c44SDan Williams 	sas_put_local_phy(phy);
40936a39947SDan Williams 
41036a39947SDan Williams 	ret = ata_wait_after_reset(link, deadline, check_ready);
41136a39947SDan Williams 	if (ret && ret != -EAGAIN)
412d214d81eSDan Williams 		sas_ata_printk(KERN_ERR, dev, "reset failed (errno=%d)\n", ret);
41336a39947SDan Williams 
4141cbd772dSHannes Reinecke 	*class = dev->sata_dev.class;
415338ec570SDarrick J. Wong 
416338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
41700dd4998SJames Bottomley 	return ret;
418338ec570SDarrick J. Wong }
419338ec570SDarrick J. Wong 
4203dff5721SDan Williams /*
4213dff5721SDan Williams  * notify the lldd to forget the sas_task for this internal ata command
4223dff5721SDan Williams  * that bypasses scsi-eh
4233dff5721SDan Williams  */
4243dff5721SDan Williams static void sas_ata_internal_abort(struct sas_task *task)
4253dff5721SDan Williams {
42636a39947SDan Williams 	struct sas_internal *si = dev_to_sas_internal(task->dev);
4273dff5721SDan Williams 	unsigned long flags;
4283dff5721SDan Williams 	int res;
4293dff5721SDan Williams 
4303dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4313dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
4323dff5721SDan Williams 	    task->task_state_flags & SAS_TASK_STATE_DONE) {
4333dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4343dff5721SDan Williams 		SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
4353dff5721SDan Williams 			    task);
4363dff5721SDan Williams 		goto out;
4373dff5721SDan Williams 	}
4383dff5721SDan Williams 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
4393dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4403dff5721SDan Williams 
4413dff5721SDan Williams 	res = si->dft->lldd_abort_task(task);
4423dff5721SDan Williams 
4433dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4443dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_DONE ||
4453dff5721SDan Williams 	    res == TMF_RESP_FUNC_COMPLETE) {
4463dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4473dff5721SDan Williams 		goto out;
4483dff5721SDan Williams 	}
4493dff5721SDan Williams 
4503dff5721SDan Williams 	/* XXX we are not prepared to deal with ->lldd_abort_task()
4513dff5721SDan Williams 	 * failures.  TODO: lldds need to unconditionally forget about
4523dff5721SDan Williams 	 * aborted ata tasks, otherwise we (likely) leak the sas task
4533dff5721SDan Williams 	 * here
4543dff5721SDan Williams 	 */
4553dff5721SDan Williams 	SAS_DPRINTK("%s: Task %p leaked.\n", __func__, task);
4563dff5721SDan Williams 
4573dff5721SDan Williams 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
4583dff5721SDan Williams 		task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
4593dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4603dff5721SDan Williams 
4613dff5721SDan Williams 	return;
4623dff5721SDan Williams  out:
4633dff5721SDan Williams 	sas_free_task(task);
4643dff5721SDan Williams }
4653dff5721SDan Williams 
466338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
467338ec570SDarrick J. Wong {
468338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
469338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
470338ec570SDarrick J. Wong 
4711c50dc83SDarrick J. Wong 	if (qc->err_mask) {
4721c50dc83SDarrick J. Wong 		/*
4733dff5721SDan Williams 		 * Find the sas_task and kill it.  By this point, libata
4743dff5721SDan Williams 		 * has decided to kill the qc and has frozen the port.
4753dff5721SDan Williams 		 * In this state sas_ata_task_done() will no longer free
4763dff5721SDan Williams 		 * the sas_task, so we need to notify the lldd (via
4773dff5721SDan Williams 		 * ->lldd_abort_task) that the task is dead and free it
4783dff5721SDan Williams 		 *  ourselves.
4791c50dc83SDarrick J. Wong 		 */
4801c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
4811c50dc83SDarrick J. Wong 
4821c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
4833a2cdf39SDan Williams 		if (!task)
4843a2cdf39SDan Williams 			return;
4851c50dc83SDarrick J. Wong 		task->uldd_task = NULL;
4863dff5721SDan Williams 		sas_ata_internal_abort(task);
4871c50dc83SDarrick J. Wong 	}
4881c50dc83SDarrick J. Wong }
489338ec570SDarrick J. Wong 
490b91bb296SDan Williams 
491b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
492b91bb296SDan Williams {
493b91bb296SDan Williams 	struct domain_device *dev = ap->private_data;
49436a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
495b91bb296SDan Williams 
496b91bb296SDan Williams 	if (i->dft->lldd_ata_set_dmamode)
497b91bb296SDan Williams 		i->dft->lldd_ata_set_dmamode(dev);
498b91bb296SDan Williams }
499b91bb296SDan Williams 
500e4a9c373SDan Williams static void sas_ata_sched_eh(struct ata_port *ap)
501e4a9c373SDan Williams {
502e4a9c373SDan Williams 	struct domain_device *dev = ap->private_data;
503e4a9c373SDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
504e4a9c373SDan Williams 	unsigned long flags;
505e4a9c373SDan Williams 
506e4a9c373SDan Williams 	spin_lock_irqsave(&ha->lock, flags);
507e4a9c373SDan Williams 	if (!test_and_set_bit(SAS_DEV_EH_PENDING, &dev->state))
508e4a9c373SDan Williams 		ha->eh_active++;
509e4a9c373SDan Williams 	ata_std_sched_eh(ap);
510e4a9c373SDan Williams 	spin_unlock_irqrestore(&ha->lock, flags);
511e4a9c373SDan Williams }
512e4a9c373SDan Williams 
513e4a9c373SDan Williams void sas_ata_end_eh(struct ata_port *ap)
514e4a9c373SDan Williams {
515e4a9c373SDan Williams 	struct domain_device *dev = ap->private_data;
516e4a9c373SDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
517e4a9c373SDan Williams 	unsigned long flags;
518e4a9c373SDan Williams 
519e4a9c373SDan Williams 	spin_lock_irqsave(&ha->lock, flags);
520e4a9c373SDan Williams 	if (test_and_clear_bit(SAS_DEV_EH_PENDING, &dev->state))
521e4a9c373SDan Williams 		ha->eh_active--;
522e4a9c373SDan Williams 	spin_unlock_irqrestore(&ha->lock, flags);
523e4a9c373SDan Williams }
524e4a9c373SDan Williams 
525338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
52600dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
52700dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
52800dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
52900dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
530338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
531f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
532338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
533338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
5344c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
535338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
536338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
537b91bb296SDan Williams 	.set_dmamode		= sas_ata_set_dmamode,
538e4a9c373SDan Williams 	.sched_eh		= sas_ata_sched_eh,
539e4a9c373SDan Williams 	.end_eh			= sas_ata_end_eh,
540338ec570SDarrick J. Wong };
541338ec570SDarrick J. Wong 
542338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
5435067c046SShaohua Li 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ |
544ef026b18SHannes Reinecke 		 ATA_FLAG_SAS_HOST | ATA_FLAG_FPDMA_AUX,
5450f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
5460f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
547338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
548338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
549338ec570SDarrick J. Wong };
550338ec570SDarrick J. Wong 
551b2024459SDan Williams int sas_ata_init(struct domain_device *found_dev)
552338ec570SDarrick J. Wong {
5539508a66fSDan Williams 	struct sas_ha_struct *ha = found_dev->port->ha;
5549508a66fSDan Williams 	struct Scsi_Host *shost = ha->core.shost;
555338ec570SDarrick J. Wong 	struct ata_port *ap;
556b2024459SDan Williams 	int rc;
557338ec570SDarrick J. Wong 
5588d8e7d13SDan Williams 	ata_host_init(&found_dev->sata_dev.ata_host, ha->dev, &sas_sata_ops);
559338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
560338ec570SDarrick J. Wong 				&sata_port_info,
561338ec570SDarrick J. Wong 				shost);
562338ec570SDarrick J. Wong 	if (!ap) {
563338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
564338ec570SDarrick J. Wong 		return -ENODEV;
565338ec570SDarrick J. Wong 	}
566338ec570SDarrick J. Wong 
567338ec570SDarrick J. Wong 	ap->private_data = found_dev;
568338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
569338ec570SDarrick J. Wong 	ap->scsi_host = shost;
570b2024459SDan Williams 	rc = ata_sas_port_init(ap);
571b2024459SDan Williams 	if (rc) {
572b2024459SDan Williams 		ata_sas_port_destroy(ap);
573b2024459SDan Williams 		return rc;
574b2024459SDan Williams 	}
575b6240a4dSJason Yan 	rc = ata_sas_tport_add(found_dev->sata_dev.ata_host.dev, ap);
576b6240a4dSJason Yan 	if (rc) {
577b6240a4dSJason Yan 		ata_sas_port_destroy(ap);
578b6240a4dSJason Yan 		return rc;
579b6240a4dSJason Yan 	}
580338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
581338ec570SDarrick J. Wong 
582338ec570SDarrick J. Wong 	return 0;
583338ec570SDarrick J. Wong }
5843a2755afSDarrick J. Wong 
5853a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
5863a2755afSDarrick J. Wong {
5873a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
5883a2755afSDarrick J. Wong 	struct completion *waiting;
5893a2755afSDarrick J. Wong 
5903a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
5913a2755afSDarrick J. Wong 	if (qc->scsicmd) {
5921b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
5931b4d0d8eSJames Bottomley 		unsigned long flags;
5941b4d0d8eSJames Bottomley 
59570b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
596242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
59770b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
5983a2755afSDarrick J. Wong 		return;
5993a2755afSDarrick J. Wong 	}
6003a2755afSDarrick J. Wong 
6013a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
6023a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
6033a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
6043a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
6053a2755afSDarrick J. Wong 	waiting = qc->private_data;
6063a2755afSDarrick J. Wong 	complete(waiting);
6073a2755afSDarrick J. Wong }
608b9142174SJames Bottomley 
6091cbd772dSHannes Reinecke static int sas_get_ata_command_set(struct domain_device *dev)
610b9142174SJames Bottomley {
611b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
612b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
6131cbd772dSHannes Reinecke 	struct ata_taskfile tf;
614b9142174SJames Bottomley 
615aa9f8328SJames Bottomley 	if (dev->dev_type == SAS_SATA_PENDING)
6161cbd772dSHannes Reinecke 		return ATA_DEV_UNKNOWN;
617354cf829SDan Williams 
6181cbd772dSHannes Reinecke 	ata_tf_from_fis((const u8 *)fis, &tf);
619b9142174SJames Bottomley 
6201cbd772dSHannes Reinecke 	return ata_dev_classify(&tf);
621b9142174SJames Bottomley }
622b9142174SJames Bottomley 
6239508a66fSDan Williams void sas_probe_sata(struct asd_sas_port *port)
6249508a66fSDan Williams {
6259508a66fSDan Williams 	struct domain_device *dev, *n;
6269508a66fSDan Williams 
6279508a66fSDan Williams 	mutex_lock(&port->ha->disco_mutex);
628b2024459SDan Williams 	list_for_each_entry(dev, &port->disco_list, disco_list_node) {
6299508a66fSDan Williams 		if (!dev_is_sata(dev))
6309508a66fSDan Williams 			continue;
6319508a66fSDan Williams 
632b2024459SDan Williams 		ata_sas_async_probe(dev->sata_dev.ap);
6339508a66fSDan Williams 	}
6349508a66fSDan Williams 	mutex_unlock(&port->ha->disco_mutex);
6359508a66fSDan Williams 
6369508a66fSDan Williams 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
6379508a66fSDan Williams 		if (!dev_is_sata(dev))
6389508a66fSDan Williams 			continue;
6399508a66fSDan Williams 
6409508a66fSDan Williams 		sas_ata_wait_eh(dev);
6419508a66fSDan Williams 
6429508a66fSDan Williams 		/* if libata could not bring the link up, don't surface
6439508a66fSDan Williams 		 * the device
6449508a66fSDan Williams 		 */
6459508a66fSDan Williams 		if (ata_dev_disabled(sas_to_ata_dev(dev)))
6469508a66fSDan Williams 			sas_fail_probe(dev, __func__, -ENODEV);
6479508a66fSDan Williams 	}
648303694eeSDan Williams 
649303694eeSDan Williams }
650303694eeSDan Williams 
651bc6e7c4bSDan Williams static void sas_ata_flush_pm_eh(struct asd_sas_port *port, const char *func)
652303694eeSDan Williams {
653303694eeSDan Williams 	struct domain_device *dev, *n;
654303694eeSDan Williams 
655303694eeSDan Williams 	list_for_each_entry_safe(dev, n, &port->dev_list, dev_list_node) {
656303694eeSDan Williams 		if (!dev_is_sata(dev))
657303694eeSDan Williams 			continue;
658303694eeSDan Williams 
659303694eeSDan Williams 		sas_ata_wait_eh(dev);
660303694eeSDan Williams 
661303694eeSDan Williams 		/* if libata failed to power manage the device, tear it down */
662303694eeSDan Williams 		if (ata_dev_disabled(sas_to_ata_dev(dev)))
663303694eeSDan Williams 			sas_fail_probe(dev, func, -ENODEV);
664303694eeSDan Williams 	}
665303694eeSDan Williams }
666303694eeSDan Williams 
667303694eeSDan Williams void sas_suspend_sata(struct asd_sas_port *port)
668303694eeSDan Williams {
669303694eeSDan Williams 	struct domain_device *dev;
670303694eeSDan Williams 
671303694eeSDan Williams 	mutex_lock(&port->ha->disco_mutex);
672303694eeSDan Williams 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
673303694eeSDan Williams 		struct sata_device *sata;
674303694eeSDan Williams 
675303694eeSDan Williams 		if (!dev_is_sata(dev))
676303694eeSDan Williams 			continue;
677303694eeSDan Williams 
678303694eeSDan Williams 		sata = &dev->sata_dev;
679303694eeSDan Williams 		if (sata->ap->pm_mesg.event == PM_EVENT_SUSPEND)
680303694eeSDan Williams 			continue;
681303694eeSDan Williams 
682bc6e7c4bSDan Williams 		ata_sas_port_suspend(sata->ap);
683303694eeSDan Williams 	}
684303694eeSDan Williams 	mutex_unlock(&port->ha->disco_mutex);
685303694eeSDan Williams 
686bc6e7c4bSDan Williams 	sas_ata_flush_pm_eh(port, __func__);
687303694eeSDan Williams }
688303694eeSDan Williams 
689303694eeSDan Williams void sas_resume_sata(struct asd_sas_port *port)
690303694eeSDan Williams {
691303694eeSDan Williams 	struct domain_device *dev;
692303694eeSDan Williams 
693303694eeSDan Williams 	mutex_lock(&port->ha->disco_mutex);
694303694eeSDan Williams 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
695303694eeSDan Williams 		struct sata_device *sata;
696303694eeSDan Williams 
697303694eeSDan Williams 		if (!dev_is_sata(dev))
698303694eeSDan Williams 			continue;
699303694eeSDan Williams 
700303694eeSDan Williams 		sata = &dev->sata_dev;
701303694eeSDan Williams 		if (sata->ap->pm_mesg.event == PM_EVENT_ON)
702303694eeSDan Williams 			continue;
703303694eeSDan Williams 
704bc6e7c4bSDan Williams 		ata_sas_port_resume(sata->ap);
705303694eeSDan Williams 	}
706303694eeSDan Williams 	mutex_unlock(&port->ha->disco_mutex);
707303694eeSDan Williams 
708bc6e7c4bSDan Williams 	sas_ata_flush_pm_eh(port, __func__);
7099508a66fSDan Williams }
7109508a66fSDan Williams 
711b9142174SJames Bottomley /**
712121246aeSBart Van Assche  * sas_discover_sata - discover an STP/SATA domain device
713b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
714b9142174SJames Bottomley  *
715b91bb296SDan Williams  * Devices directly attached to a HA port, have no parents.  All other
716b91bb296SDan Williams  * devices do, and should have their "parent" pointer set appropriately
717b91bb296SDan Williams  * before calling this function.
718b9142174SJames Bottomley  */
719b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
720b9142174SJames Bottomley {
721b9142174SJames Bottomley 	int res;
722b9142174SJames Bottomley 
723aa9f8328SJames Bottomley 	if (dev->dev_type == SAS_SATA_PM)
724b91bb296SDan Williams 		return -ENODEV;
725b91bb296SDan Williams 
7261cbd772dSHannes Reinecke 	dev->sata_dev.class = sas_get_ata_command_set(dev);
727b91bb296SDan Williams 	sas_fill_in_rphy(dev, dev->rphy);
72887c8331fSDan Williams 
72987c8331fSDan Williams 	res = sas_notify_lldd_dev_found(dev);
73087c8331fSDan Williams 	if (res)
73187c8331fSDan Williams 		return res;
73287c8331fSDan Williams 
733b91bb296SDan Williams 	return 0;
734b9142174SJames Bottomley }
73500dd4998SJames Bottomley 
73650824d6cSDan Williams static void async_sas_ata_eh(void *data, async_cookie_t cookie)
73750824d6cSDan Williams {
73850824d6cSDan Williams 	struct domain_device *dev = data;
73950824d6cSDan Williams 	struct ata_port *ap = dev->sata_dev.ap;
74050824d6cSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
74150824d6cSDan Williams 
742d214d81eSDan Williams 	sas_ata_printk(KERN_DEBUG, dev, "dev error handler\n");
74350824d6cSDan Williams 	ata_scsi_port_error_handler(ha->core.shost, ap);
7448abda4d2SDan Williams 	sas_put_device(dev);
74550824d6cSDan Williams }
74650824d6cSDan Williams 
74700dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
74800dd4998SJames Bottomley {
74987c8331fSDan Williams 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
7502955b47dSDan Williams 	ASYNC_DOMAIN_EXCLUSIVE(async);
7519508a66fSDan Williams 	int i;
75287c8331fSDan Williams 
75387c8331fSDan Williams 	/* it's ok to defer revalidation events during ata eh, these
75487c8331fSDan Williams 	 * disks are in one of three states:
75587c8331fSDan Williams 	 * 1/ present for initial domain discovery, and these
75687c8331fSDan Williams 	 *    resets will cause bcn flutters
75787c8331fSDan Williams 	 * 2/ hot removed, we'll discover that after eh fails
75887c8331fSDan Williams 	 * 3/ hot added after initial discovery, lost the race, and need
75987c8331fSDan Williams 	 *    to catch the next train.
76087c8331fSDan Williams 	 */
76187c8331fSDan Williams 	sas_disable_revalidation(sas_ha);
76200dd4998SJames Bottomley 
7639508a66fSDan Williams 	spin_lock_irq(&sas_ha->phy_port_lock);
7649508a66fSDan Williams 	for (i = 0; i < sas_ha->num_phys; i++) {
7659508a66fSDan Williams 		struct asd_sas_port *port = sas_ha->sas_port[i];
7669508a66fSDan Williams 		struct domain_device *dev;
76700dd4998SJames Bottomley 
7689508a66fSDan Williams 		spin_lock(&port->dev_list_lock);
7699508a66fSDan Williams 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
770b2024459SDan Williams 			if (!dev_is_sata(dev))
77100dd4998SJames Bottomley 				continue;
772e4a9c373SDan Williams 
773e4a9c373SDan Williams 			/* hold a reference over eh since we may be
774e4a9c373SDan Williams 			 * racing with final remove once all commands
775e4a9c373SDan Williams 			 * are completed
776e4a9c373SDan Williams 			 */
777e4a9c373SDan Williams 			kref_get(&dev->kref);
778e4a9c373SDan Williams 
7799508a66fSDan Williams 			async_schedule_domain(async_sas_ata_eh, dev, &async);
78000dd4998SJames Bottomley 		}
7819508a66fSDan Williams 		spin_unlock(&port->dev_list_lock);
7829508a66fSDan Williams 	}
7839508a66fSDan Williams 	spin_unlock_irq(&sas_ha->phy_port_lock);
7849508a66fSDan Williams 
78550824d6cSDan Williams 	async_synchronize_full_domain(&async);
78687c8331fSDan Williams 
78787c8331fSDan Williams 	sas_enable_revalidation(sas_ha);
78800dd4998SJames Bottomley }
78900dd4998SJames Bottomley 
790d230ce69SDan Williams void sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
79100dd4998SJames Bottomley 		struct list_head *done_q)
79200dd4998SJames Bottomley {
79300dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
794d214d81eSDan Williams 	struct domain_device *eh_dev;
79500dd4998SJames Bottomley 
79600dd4998SJames Bottomley 	do {
79700dd4998SJames Bottomley 		LIST_HEAD(sata_q);
798d214d81eSDan Williams 		eh_dev = NULL;
79900dd4998SJames Bottomley 
80000dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
80100dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
80200dd4998SJames Bottomley 
80300dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
80400dd4998SJames Bottomley 				continue;
805d214d81eSDan Williams 			if (eh_dev && eh_dev != ddev)
80600dd4998SJames Bottomley 				continue;
807d214d81eSDan Williams 			eh_dev = ddev;
80800dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
80900dd4998SJames Bottomley 		}
81000dd4998SJames Bottomley 
81100dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
812d214d81eSDan Williams 			struct ata_port *ap = eh_dev->sata_dev.ap;
813d214d81eSDan Williams 
814d214d81eSDan Williams 			sas_ata_printk(KERN_DEBUG, eh_dev, "cmd error handler\n");
81500dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
816a82058a7SJames Bottomley 			/*
817a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
818a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
819a82058a7SJames Bottomley 			 * about to go out of scope.
820a82058a7SJames Bottomley 			 *
821a82058a7SJames Bottomley 			 * This looks strange, since the commands are
822a82058a7SJames Bottomley 			 * now part of no list, but the next error
823a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
824a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
825a82058a7SJames Bottomley 			 * anyway from the ata tag array.
826a82058a7SJames Bottomley 			 */
827a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
828a82058a7SJames Bottomley 				list_del_init(sata_q.next);
82900dd4998SJames Bottomley 		}
830d214d81eSDan Williams 	} while (eh_dev);
83100dd4998SJames Bottomley }
832b52df417SDan Williams 
833b52df417SDan Williams void sas_ata_schedule_reset(struct domain_device *dev)
834b52df417SDan Williams {
835b52df417SDan Williams 	struct ata_eh_info *ehi;
836b52df417SDan Williams 	struct ata_port *ap;
837b52df417SDan Williams 	unsigned long flags;
838b52df417SDan Williams 
839b52df417SDan Williams 	if (!dev_is_sata(dev))
840b52df417SDan Williams 		return;
841b52df417SDan Williams 
842b52df417SDan Williams 	ap = dev->sata_dev.ap;
843b52df417SDan Williams 	ehi = &ap->link.eh_info;
844b52df417SDan Williams 
845b52df417SDan Williams 	spin_lock_irqsave(ap->lock, flags);
846b52df417SDan Williams 	ehi->err_mask |= AC_ERR_TIMEOUT;
847b52df417SDan Williams 	ehi->action |= ATA_EH_RESET;
848b52df417SDan Williams 	ata_port_schedule_eh(ap);
849b52df417SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
850b52df417SDan Williams }
85143a5ab15SDan Williams EXPORT_SYMBOL_GPL(sas_ata_schedule_reset);
85281c757bcSDan Williams 
85381c757bcSDan Williams void sas_ata_wait_eh(struct domain_device *dev)
85481c757bcSDan Williams {
85581c757bcSDan Williams 	struct ata_port *ap;
85681c757bcSDan Williams 
85781c757bcSDan Williams 	if (!dev_is_sata(dev))
85881c757bcSDan Williams 		return;
85981c757bcSDan Williams 
86081c757bcSDan Williams 	ap = dev->sata_dev.ap;
86181c757bcSDan Williams 	ata_port_wait_eh(ap);
86281c757bcSDan Williams }
863