xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision b91bb296188118eea9fdc6093cfcf76bbe8589ba)
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>
26b9142174SJames Bottomley 
27338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
28338ec570SDarrick J. Wong #include "sas_internal.h"
29338ec570SDarrick J. Wong #include <scsi/scsi_host.h>
30338ec570SDarrick J. Wong #include <scsi/scsi_device.h>
31338ec570SDarrick J. Wong #include <scsi/scsi_tcq.h>
32338ec570SDarrick J. Wong #include <scsi/scsi.h>
33338ec570SDarrick J. Wong #include <scsi/scsi_transport.h>
34338ec570SDarrick J. Wong #include <scsi/scsi_transport_sas.h>
35338ec570SDarrick J. Wong #include "../scsi_sas_internal.h"
363a2755afSDarrick J. Wong #include "../scsi_transport_api.h"
373a2755afSDarrick J. Wong #include <scsi/scsi_eh.h>
38338ec570SDarrick J. Wong 
39338ec570SDarrick J. Wong static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
40338ec570SDarrick J. Wong {
41338ec570SDarrick J. Wong 	/* Cheesy attempt to translate SAS errors into ATA.  Hah! */
42338ec570SDarrick J. Wong 
43338ec570SDarrick J. Wong 	/* transport error */
44338ec570SDarrick J. Wong 	if (ts->resp == SAS_TASK_UNDELIVERED)
45338ec570SDarrick J. Wong 		return AC_ERR_ATA_BUS;
46338ec570SDarrick J. Wong 
47338ec570SDarrick J. Wong 	/* ts->resp == SAS_TASK_COMPLETE */
48338ec570SDarrick J. Wong 	/* task delivered, what happened afterwards? */
49338ec570SDarrick J. Wong 	switch (ts->stat) {
50338ec570SDarrick J. Wong 		case SAS_DEV_NO_RESPONSE:
51338ec570SDarrick J. Wong 			return AC_ERR_TIMEOUT;
52338ec570SDarrick J. Wong 
53338ec570SDarrick J. Wong 		case SAS_INTERRUPTED:
54338ec570SDarrick J. Wong 		case SAS_PHY_DOWN:
55338ec570SDarrick J. Wong 		case SAS_NAK_R_ERR:
56338ec570SDarrick J. Wong 			return AC_ERR_ATA_BUS;
57338ec570SDarrick J. Wong 
58338ec570SDarrick J. Wong 
59338ec570SDarrick J. Wong 		case SAS_DATA_UNDERRUN:
60338ec570SDarrick J. Wong 			/*
61338ec570SDarrick J. Wong 			 * Some programs that use the taskfile interface
62338ec570SDarrick J. Wong 			 * (smartctl in particular) can cause underrun
63338ec570SDarrick J. Wong 			 * problems.  Ignore these errors, perhaps at our
64338ec570SDarrick J. Wong 			 * peril.
65338ec570SDarrick J. Wong 			 */
66338ec570SDarrick J. Wong 			return 0;
67338ec570SDarrick J. Wong 
68338ec570SDarrick J. Wong 		case SAS_DATA_OVERRUN:
69338ec570SDarrick J. Wong 		case SAS_QUEUE_FULL:
70338ec570SDarrick J. Wong 		case SAS_DEVICE_UNKNOWN:
71338ec570SDarrick J. Wong 		case SAS_SG_ERR:
72338ec570SDarrick J. Wong 			return AC_ERR_INVALID;
73338ec570SDarrick J. Wong 
74338ec570SDarrick J. Wong 		case SAS_OPEN_TO:
75338ec570SDarrick J. Wong 		case SAS_OPEN_REJECT:
76338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Saw error %d.  What to do?\n",
77cadbd4a5SHarvey Harrison 				    __func__, ts->stat);
78338ec570SDarrick J. Wong 			return AC_ERR_OTHER;
79338ec570SDarrick J. Wong 
8075c0b386SJames Bottomley 		case SAM_STAT_CHECK_CONDITION:
81338ec570SDarrick J. Wong 		case SAS_ABORTED_TASK:
82338ec570SDarrick J. Wong 			return AC_ERR_DEV;
83338ec570SDarrick J. Wong 
84338ec570SDarrick J. Wong 		case SAS_PROTO_RESPONSE:
85338ec570SDarrick J. Wong 			/* This means the ending_fis has the error
86338ec570SDarrick J. Wong 			 * value; return 0 here to collect it */
87338ec570SDarrick J. Wong 			return 0;
88338ec570SDarrick J. Wong 		default:
89338ec570SDarrick J. Wong 			return 0;
90338ec570SDarrick J. Wong 	}
91338ec570SDarrick J. Wong }
92338ec570SDarrick J. Wong 
93338ec570SDarrick J. Wong static void sas_ata_task_done(struct sas_task *task)
94338ec570SDarrick J. Wong {
95338ec570SDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
961c50dc83SDarrick J. Wong 	struct domain_device *dev;
97338ec570SDarrick J. Wong 	struct task_status_struct *stat = &task->task_status;
98338ec570SDarrick J. Wong 	struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
993a2755afSDarrick J. Wong 	struct sas_ha_struct *sas_ha;
100338ec570SDarrick J. Wong 	enum ata_completion_errors ac;
1013eb7a51aSDarrick J. Wong 	unsigned long flags;
102bb650a1bSXiangliang Yu 	struct ata_link *link;
103338ec570SDarrick J. Wong 
1041c50dc83SDarrick J. Wong 	if (!qc)
1051c50dc83SDarrick J. Wong 		goto qc_already_gone;
1061c50dc83SDarrick J. Wong 
1071c50dc83SDarrick J. Wong 	dev = qc->ap->private_data;
1083a2755afSDarrick J. Wong 	sas_ha = dev->port->ha;
109bb650a1bSXiangliang Yu 	link = &dev->sata_dev.ap->link;
1101c50dc83SDarrick J. Wong 
1113eb7a51aSDarrick J. Wong 	spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
11275c0b386SJames Bottomley 	if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
11375c0b386SJames Bottomley 	    ((stat->stat == SAM_STAT_CHECK_CONDITION &&
11475c0b386SJames Bottomley 	      dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
115338ec570SDarrick J. Wong 		ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
116bb650a1bSXiangliang Yu 
117bb650a1bSXiangliang Yu 		if (!link->sactive) {
118338ec570SDarrick J. Wong 			qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
119bb650a1bSXiangliang Yu 		} else {
120bb650a1bSXiangliang Yu 			link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
121bb650a1bSXiangliang Yu 			if (unlikely(link->eh_info.err_mask))
122bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
123bb650a1bSXiangliang Yu 		}
12475c0b386SJames Bottomley 	} else {
125338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
126338ec570SDarrick J. Wong 		if (ac) {
127cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: SAS error %x\n", __func__,
128338ec570SDarrick J. Wong 				    stat->stat);
129338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
130bb650a1bSXiangliang Yu 			if (!link->sactive) {
131338ec570SDarrick J. Wong 				qc->err_mask = ac;
132bb650a1bSXiangliang Yu 			} else {
133bb650a1bSXiangliang Yu 				link->eh_info.err_mask |= AC_ERR_DEV;
134bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
135bb650a1bSXiangliang Yu 			}
136bb650a1bSXiangliang Yu 
137338ec570SDarrick J. Wong 			dev->sata_dev.tf.feature = 0x04; /* status err */
138338ec570SDarrick J. Wong 			dev->sata_dev.tf.command = ATA_ERR;
139338ec570SDarrick J. Wong 		}
140338ec570SDarrick J. Wong 	}
141338ec570SDarrick J. Wong 
1421c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
143fe059f12SDarrick J. Wong 	if (qc->scsicmd)
144fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
145338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1463eb7a51aSDarrick J. Wong 	spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
1473eb7a51aSDarrick J. Wong 
1483a2755afSDarrick J. Wong 	/*
1493a2755afSDarrick J. Wong 	 * If the sas_task has an ata qc, a scsi_cmnd and the aborted
1503a2755afSDarrick J. Wong 	 * flag is set, then we must have come in via the libsas EH
1513a2755afSDarrick J. Wong 	 * functions.  When we exit this function, we need to put the
1523a2755afSDarrick J. Wong 	 * scsi_cmnd on the list of finished errors.  The ata_qc_complete
1533a2755afSDarrick J. Wong 	 * call cleans up the libata side of things but we're protected
1543a2755afSDarrick J. Wong 	 * from the scsi_cmnd going away because the scsi_cmnd is owned
1553a2755afSDarrick J. Wong 	 * by the EH, making libata's call to scsi_done a NOP.
1563a2755afSDarrick J. Wong 	 */
1573a2755afSDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
1583a2755afSDarrick J. Wong 	if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
1593a2755afSDarrick J. Wong 		scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
1603a2755afSDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1613a2755afSDarrick J. Wong 
1621c50dc83SDarrick J. Wong qc_already_gone:
163338ec570SDarrick J. Wong 	list_del_init(&task->list);
164338ec570SDarrick J. Wong 	sas_free_task(task);
165338ec570SDarrick J. Wong }
166338ec570SDarrick J. Wong 
167338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
168338ec570SDarrick J. Wong {
169312d3e56SDan Williams 	unsigned long flags;
170338ec570SDarrick J. Wong 	struct sas_task *task;
171312d3e56SDan Williams 	struct scatterlist *sg;
172312d3e56SDan Williams 	int ret = AC_ERR_SYSTEM;
173312d3e56SDan Williams 	unsigned int si, xfer = 0;
174312d3e56SDan Williams 	struct ata_port *ap = qc->ap;
175312d3e56SDan Williams 	struct domain_device *dev = ap->private_data;
176338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
177338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
178338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
179312d3e56SDan Williams 
180312d3e56SDan Williams 	/* TODO: audit callers to ensure they are ready for qc_issue to
181312d3e56SDan Williams 	 * unconditionally re-enable interrupts
182312d3e56SDan Williams 	 */
183312d3e56SDan Williams 	local_irq_save(flags);
184312d3e56SDan Williams 	spin_unlock(ap->lock);
185338ec570SDarrick J. Wong 
18656dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
187e139942dSDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state))
188312d3e56SDan Williams 		goto out;
18956dd2c06SDarrick J. Wong 
190338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
191338ec570SDarrick J. Wong 	if (!task)
192312d3e56SDan Williams 		goto out;
193338ec570SDarrick J. Wong 	task->dev = dev;
194338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
195338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
196338ec570SDarrick J. Wong 
197338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
198338ec570SDarrick J. Wong 	    qc->tf.command == ATA_CMD_FPDMA_READ) {
199338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
200338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
201338ec570SDarrick J. Wong 	}
202338ec570SDarrick J. Wong 
203110dd8f1SJames Bottomley 	ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
204338ec570SDarrick J. Wong 	task->uldd_task = qc;
205405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
206338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
207dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
208dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
209338ec570SDarrick J. Wong 	} else {
210ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
211338ec570SDarrick J. Wong 			xfer += sg->length;
212338ec570SDarrick J. Wong 
213338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
214ff2aeb1eSTejun Heo 		task->num_scatter = si;
215338ec570SDarrick J. Wong 	}
216338ec570SDarrick J. Wong 
217338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
218ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
219338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
220338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
2211c50dc83SDarrick J. Wong 	qc->lldd_task = task;
222338ec570SDarrick J. Wong 
223338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
224338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
225338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
226338ec570SDarrick J. Wong 		/* fall through */
2270dc36888STejun Heo 	case ATAPI_PROT_DMA:
228338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
229338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
230338ec570SDarrick J. Wong 		break;
231338ec570SDarrick J. Wong 	}
232338ec570SDarrick J. Wong 
233fe059f12SDarrick J. Wong 	if (qc->scsicmd)
234fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
235fe059f12SDarrick J. Wong 
236338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
237312d3e56SDan Williams 		ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
238338ec570SDarrick J. Wong 	else
239312d3e56SDan Williams 		ret = sas_queue_up(task);
240338ec570SDarrick J. Wong 
241338ec570SDarrick J. Wong 	/* Examine */
242312d3e56SDan Williams 	if (ret) {
243312d3e56SDan Williams 		SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
244338ec570SDarrick J. Wong 
245fe059f12SDarrick J. Wong 		if (qc->scsicmd)
246fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
247338ec570SDarrick J. Wong 		sas_free_task(task);
248312d3e56SDan Williams 		ret = AC_ERR_SYSTEM;
249338ec570SDarrick J. Wong 	}
250338ec570SDarrick J. Wong 
251312d3e56SDan Williams  out:
252312d3e56SDan Williams 	spin_lock(ap->lock);
253312d3e56SDan Williams 	local_irq_restore(flags);
254312d3e56SDan Williams 	return ret;
255338ec570SDarrick J. Wong }
256338ec570SDarrick J. Wong 
2574c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2584c9bf4e7STejun Heo {
2594c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2604c9bf4e7STejun Heo 
2614c9bf4e7STejun Heo 	memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
2624c9bf4e7STejun Heo 	return true;
2634c9bf4e7STejun Heo }
2644c9bf4e7STejun Heo 
26500dd4998SJames Bottomley static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
26600dd4998SJames Bottomley 			       unsigned long deadline)
267338ec570SDarrick J. Wong {
26800dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
269338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
270338ec570SDarrick J. Wong 	struct sas_internal *i =
271338ec570SDarrick J. Wong 		to_sas_internal(dev->port->ha->core.shost->transportt);
272a29c0515SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
27300dd4998SJames Bottomley 	int ret = 0;
274338ec570SDarrick J. Wong 
275338ec570SDarrick J. Wong 	if (i->dft->lldd_I_T_nexus_reset)
276338ec570SDarrick J. Wong 		res = i->dft->lldd_I_T_nexus_reset(dev);
277338ec570SDarrick J. Wong 
27800dd4998SJames Bottomley 	if (res != TMF_RESP_FUNC_COMPLETE) {
279cadbd4a5SHarvey Harrison 		SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
28000dd4998SJames Bottomley 		ret = -EAGAIN;
28100dd4998SJames Bottomley 	}
282338ec570SDarrick J. Wong 
283338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
284338ec570SDarrick J. Wong 		case ATA_COMMAND_SET:
285cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: Found ATA device.\n", __func__);
28600dd4998SJames Bottomley 			*class = ATA_DEV_ATA;
287338ec570SDarrick J. Wong 			break;
288338ec570SDarrick J. Wong 		case ATAPI_COMMAND_SET:
289cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
29000dd4998SJames Bottomley 			*class = ATA_DEV_ATAPI;
291338ec570SDarrick J. Wong 			break;
292338ec570SDarrick J. Wong 		default:
293338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
294cadbd4a5SHarvey Harrison 				    __func__,
295338ec570SDarrick J. Wong 				    dev->sata_dev.command_set);
29600dd4998SJames Bottomley 			*class = ATA_DEV_UNKNOWN;
297338ec570SDarrick J. Wong 			break;
298338ec570SDarrick J. Wong 	}
299338ec570SDarrick J. Wong 
300338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
30100dd4998SJames Bottomley 	return ret;
302338ec570SDarrick J. Wong }
303338ec570SDarrick J. Wong 
3041ca1e43eSDave Jiang static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
3051ca1e43eSDave Jiang 			       unsigned long deadline)
3061ca1e43eSDave Jiang {
3071ca1e43eSDave Jiang 	struct ata_port *ap = link->ap;
3081ca1e43eSDave Jiang 	struct domain_device *dev = ap->private_data;
3091ca1e43eSDave Jiang 	struct sas_internal *i =
3101ca1e43eSDave Jiang 		to_sas_internal(dev->port->ha->core.shost->transportt);
3111ca1e43eSDave Jiang 	int res = TMF_RESP_FUNC_FAILED;
3121ca1e43eSDave Jiang 	int ret = 0;
3131ca1e43eSDave Jiang 
3141ca1e43eSDave Jiang 	if (i->dft->lldd_ata_soft_reset)
3151ca1e43eSDave Jiang 		res = i->dft->lldd_ata_soft_reset(dev);
3161ca1e43eSDave Jiang 
3171ca1e43eSDave Jiang 	if (res != TMF_RESP_FUNC_COMPLETE) {
3181ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
3191ca1e43eSDave Jiang 		ret = -EAGAIN;
3201ca1e43eSDave Jiang 	}
3211ca1e43eSDave Jiang 
3221ca1e43eSDave Jiang 	switch (dev->sata_dev.command_set) {
3231ca1e43eSDave Jiang 	case ATA_COMMAND_SET:
3241ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Found ATA device.\n", __func__);
3251ca1e43eSDave Jiang 		*class = ATA_DEV_ATA;
3261ca1e43eSDave Jiang 		break;
3271ca1e43eSDave Jiang 	case ATAPI_COMMAND_SET:
3281ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
3291ca1e43eSDave Jiang 		*class = ATA_DEV_ATAPI;
3301ca1e43eSDave Jiang 		break;
3311ca1e43eSDave Jiang 	default:
3321ca1e43eSDave Jiang 		SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
3331ca1e43eSDave Jiang 			    __func__, dev->sata_dev.command_set);
3341ca1e43eSDave Jiang 		*class = ATA_DEV_UNKNOWN;
3351ca1e43eSDave Jiang 		break;
3361ca1e43eSDave Jiang 	}
3371ca1e43eSDave Jiang 
3381ca1e43eSDave Jiang 	ap->cbl = ATA_CBL_SATA;
3391ca1e43eSDave Jiang 	return ret;
3401ca1e43eSDave Jiang }
3411ca1e43eSDave Jiang 
342338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
343338ec570SDarrick J. Wong {
344338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
345338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
346338ec570SDarrick J. Wong 
3471c50dc83SDarrick J. Wong 	if (qc->err_mask) {
3481c50dc83SDarrick J. Wong 		/*
3491c50dc83SDarrick J. Wong 		 * Find the sas_task and kill it.  By this point,
3501c50dc83SDarrick J. Wong 		 * libata has decided to kill the qc, so we needn't
3511c50dc83SDarrick J. Wong 		 * bother with sas_ata_task_done.  But we still
3521c50dc83SDarrick J. Wong 		 * ought to abort the task.
3531c50dc83SDarrick J. Wong 		 */
3541c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
3553a2755afSDarrick J. Wong 		unsigned long flags;
3561c50dc83SDarrick J. Wong 
3571c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
3581c50dc83SDarrick J. Wong 		if (task) {
3593a2755afSDarrick J. Wong 			/* Should this be a AT(API) device reset? */
3603a2755afSDarrick J. Wong 			spin_lock_irqsave(&task->task_state_lock, flags);
3613a2755afSDarrick J. Wong 			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3623a2755afSDarrick J. Wong 			spin_unlock_irqrestore(&task->task_state_lock, flags);
3633a2755afSDarrick J. Wong 
3641c50dc83SDarrick J. Wong 			task->uldd_task = NULL;
3651c50dc83SDarrick J. Wong 			__sas_task_abort(task);
3661c50dc83SDarrick J. Wong 		}
3671c50dc83SDarrick J. Wong 	}
368338ec570SDarrick J. Wong }
369338ec570SDarrick J. Wong 
370*b91bb296SDan Williams 
371*b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
372*b91bb296SDan Williams {
373*b91bb296SDan Williams 	struct domain_device *dev = ap->private_data;
374*b91bb296SDan Williams 	struct sas_internal *i =
375*b91bb296SDan Williams 		to_sas_internal(dev->port->ha->core.shost->transportt);
376*b91bb296SDan Williams 
377*b91bb296SDan Williams 	if (i->dft->lldd_ata_set_dmamode)
378*b91bb296SDan Williams 		i->dft->lldd_ata_set_dmamode(dev);
379*b91bb296SDan Williams }
380*b91bb296SDan Williams 
381338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
38200dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
3831ca1e43eSDave Jiang 	.softreset		= sas_ata_soft_reset,
38400dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
38500dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
38600dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
387338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
388f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
389338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
390338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
3914c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
392338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
393338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
394*b91bb296SDan Williams 	.set_dmamode		= sas_ata_set_dmamode,
395338ec570SDarrick J. Wong };
396338ec570SDarrick J. Wong 
397338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
3989cbe056fSSergei Shtylyov 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
3990f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
4000f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
401338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
402338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
403338ec570SDarrick J. Wong };
404338ec570SDarrick J. Wong 
405338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
406338ec570SDarrick J. Wong 			       struct scsi_target *starget)
407338ec570SDarrick J. Wong {
408338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
409338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
410338ec570SDarrick J. Wong 	struct ata_port *ap;
411338ec570SDarrick J. Wong 
412338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
4131d1bbee6SJeff Garzik 		      ha->dev,
414338ec570SDarrick J. Wong 		      sata_port_info.flags,
415338ec570SDarrick J. Wong 		      &sas_sata_ops);
416338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
417338ec570SDarrick J. Wong 				&sata_port_info,
418338ec570SDarrick J. Wong 				shost);
419338ec570SDarrick J. Wong 	if (!ap) {
420338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
421338ec570SDarrick J. Wong 		return -ENODEV;
422338ec570SDarrick J. Wong 	}
423338ec570SDarrick J. Wong 
424338ec570SDarrick J. Wong 	ap->private_data = found_dev;
425338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
426338ec570SDarrick J. Wong 	ap->scsi_host = shost;
427338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
428338ec570SDarrick J. Wong 
429338ec570SDarrick J. Wong 	return 0;
430338ec570SDarrick J. Wong }
4313a2755afSDarrick J. Wong 
4323a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
4333a2755afSDarrick J. Wong {
4343a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
4353a2755afSDarrick J. Wong 	struct completion *waiting;
4363a2755afSDarrick J. Wong 
4373a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
4383a2755afSDarrick J. Wong 	if (qc->scsicmd) {
4391b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
4401b4d0d8eSJames Bottomley 		unsigned long flags;
4411b4d0d8eSJames Bottomley 
44270b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
443242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
44470b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
4453a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
4463a2755afSDarrick J. Wong 		return;
4473a2755afSDarrick J. Wong 	}
4483a2755afSDarrick J. Wong 
4493a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
4503a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4513a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
4523a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
4533a2755afSDarrick J. Wong 	waiting = qc->private_data;
4543a2755afSDarrick J. Wong 	complete(waiting);
4553a2755afSDarrick J. Wong }
456b9142174SJames Bottomley 
457b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
458b9142174SJames Bottomley {
459b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
460b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
461b9142174SJames Bottomley 
462b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
463b9142174SJames Bottomley 	     fis->lbal         == 1 &&
464b9142174SJames Bottomley 	     fis->lbam         == 0 &&
465b9142174SJames Bottomley 	     fis->lbah         == 0 &&
466b9142174SJames Bottomley 	     fis->device       == 0)
467b9142174SJames Bottomley 	    ||
468b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
469b9142174SJames Bottomley 	     fis->lbal         == 0 &&
470b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
471b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
472b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
473b9142174SJames Bottomley 
474b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
475b9142174SJames Bottomley 
476b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
477b9142174SJames Bottomley 		  fis->lbal             == 1 &&
478b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
479b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
480b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
481b9142174SJames Bottomley 
482b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
483b9142174SJames Bottomley 
484b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
485b9142174SJames Bottomley 		  fis->lbal         == 1 &&
486b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
487b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
488b9142174SJames Bottomley 		  fis->device       == 0)
489b9142174SJames Bottomley 		||
490b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
491b9142174SJames Bottomley 		  fis->lbal             == 1 &&
492b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
493b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
494b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
495b9142174SJames Bottomley 
496b9142174SJames Bottomley 		/* Treat it as a superset? */
497b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
498b9142174SJames Bottomley }
499b9142174SJames Bottomley 
50087c8331fSDan Williams void sas_probe_sata(struct work_struct *work)
50187c8331fSDan Williams {
50287c8331fSDan Williams 	struct domain_device *dev, *n;
50387c8331fSDan Williams 	struct sas_discovery_event *ev =
50487c8331fSDan Williams 		container_of(work, struct sas_discovery_event, work);
50587c8331fSDan Williams 	struct asd_sas_port *port = ev->port;
50687c8331fSDan Williams 
50787c8331fSDan Williams 	clear_bit(DISCE_PROBE, &port->disc.pending);
50887c8331fSDan Williams 
50987c8331fSDan Williams 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
51087c8331fSDan Williams 		int err;
51187c8331fSDan Williams 
51287c8331fSDan Williams 		spin_lock_irq(&port->dev_list_lock);
51387c8331fSDan Williams 		list_add_tail(&dev->dev_list_node, &port->dev_list);
51487c8331fSDan Williams 		spin_unlock_irq(&port->dev_list_lock);
51587c8331fSDan Williams 
51687c8331fSDan Williams 		err = sas_rphy_add(dev->rphy);
51787c8331fSDan Williams 
51887c8331fSDan Williams 		if (err) {
51987c8331fSDan Williams 			SAS_DPRINTK("%s: for %s device %16llx returned %d\n",
52087c8331fSDan Williams 				    __func__, dev->parent ? "exp-attached" :
52187c8331fSDan Williams 							    "direct-attached",
52287c8331fSDan Williams 				    SAS_ADDR(dev->sas_addr), err);
52387c8331fSDan Williams 			sas_unregister_dev(port, dev);
52487c8331fSDan Williams 		} else
52587c8331fSDan Williams 			list_del_init(&dev->disco_list_node);
52687c8331fSDan Williams 	}
52787c8331fSDan Williams }
52887c8331fSDan Williams 
529b9142174SJames Bottomley /**
530b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
531b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
532b9142174SJames Bottomley  *
533*b91bb296SDan Williams  * Devices directly attached to a HA port, have no parents.  All other
534*b91bb296SDan Williams  * devices do, and should have their "parent" pointer set appropriately
535*b91bb296SDan Williams  * before calling this function.
536b9142174SJames Bottomley  */
537b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
538b9142174SJames Bottomley {
539b9142174SJames Bottomley 	int res;
540b9142174SJames Bottomley 
541*b91bb296SDan Williams 	if (dev->dev_type == SATA_PM)
542*b91bb296SDan Williams 		return -ENODEV;
543*b91bb296SDan Williams 
544b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
545*b91bb296SDan Williams 	sas_fill_in_rphy(dev, dev->rphy);
54687c8331fSDan Williams 
54787c8331fSDan Williams 	res = sas_notify_lldd_dev_found(dev);
54887c8331fSDan Williams 	if (res)
54987c8331fSDan Williams 		return res;
55087c8331fSDan Williams 
55187c8331fSDan Williams 	sas_discover_event(dev->port, DISCE_PROBE);
552*b91bb296SDan Williams 	return 0;
553b9142174SJames Bottomley }
55400dd4998SJames Bottomley 
55500dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
55600dd4998SJames Bottomley {
55700dd4998SJames Bottomley 	struct scsi_device *sdev;
55887c8331fSDan Williams 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
55987c8331fSDan Williams 
56087c8331fSDan Williams 	/* it's ok to defer revalidation events during ata eh, these
56187c8331fSDan Williams 	 * disks are in one of three states:
56287c8331fSDan Williams 	 * 1/ present for initial domain discovery, and these
56387c8331fSDan Williams 	 *    resets will cause bcn flutters
56487c8331fSDan Williams 	 * 2/ hot removed, we'll discover that after eh fails
56587c8331fSDan Williams 	 * 3/ hot added after initial discovery, lost the race, and need
56687c8331fSDan Williams 	 *    to catch the next train.
56787c8331fSDan Williams 	 */
56887c8331fSDan Williams 	sas_disable_revalidation(sas_ha);
56900dd4998SJames Bottomley 
57000dd4998SJames Bottomley 	shost_for_each_device(sdev, shost) {
57100dd4998SJames Bottomley 		struct domain_device *ddev = sdev_to_domain_dev(sdev);
57200dd4998SJames Bottomley 		struct ata_port *ap = ddev->sata_dev.ap;
57300dd4998SJames Bottomley 
57400dd4998SJames Bottomley 		if (!dev_is_sata(ddev))
57500dd4998SJames Bottomley 			continue;
57600dd4998SJames Bottomley 
57700dd4998SJames Bottomley 		ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
57800dd4998SJames Bottomley 		ata_scsi_port_error_handler(shost, ap);
57900dd4998SJames Bottomley 	}
58087c8331fSDan Williams 
58187c8331fSDan Williams 	sas_enable_revalidation(sas_ha);
58200dd4998SJames Bottomley }
58300dd4998SJames Bottomley 
58400dd4998SJames Bottomley int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,
58500dd4998SJames Bottomley 		      enum blk_eh_timer_return *rtn)
58600dd4998SJames Bottomley {
58700dd4998SJames Bottomley 	struct domain_device *ddev = cmd_to_domain_dev(cmd);
58800dd4998SJames Bottomley 
58900dd4998SJames Bottomley 	if (!dev_is_sata(ddev) || task)
59000dd4998SJames Bottomley 		return 0;
59100dd4998SJames Bottomley 
59200dd4998SJames Bottomley 	/* we're a sata device with no task, so this must be a libata
59300dd4998SJames Bottomley 	 * eh timeout.  Ideally should hook into libata timeout
59400dd4998SJames Bottomley 	 * handling, but there's no point, it just wants to activate
59500dd4998SJames Bottomley 	 * the eh thread */
59600dd4998SJames Bottomley 	*rtn = BLK_EH_NOT_HANDLED;
59700dd4998SJames Bottomley 	return 1;
59800dd4998SJames Bottomley }
59900dd4998SJames Bottomley 
60000dd4998SJames Bottomley int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
60100dd4998SJames Bottomley 	       struct list_head *done_q)
60200dd4998SJames Bottomley {
60300dd4998SJames Bottomley 	int rtn = 0;
60400dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
60500dd4998SJames Bottomley 	struct ata_port *ap;
60600dd4998SJames Bottomley 
60700dd4998SJames Bottomley 	do {
60800dd4998SJames Bottomley 		LIST_HEAD(sata_q);
60900dd4998SJames Bottomley 
61000dd4998SJames Bottomley 		ap = NULL;
61100dd4998SJames Bottomley 
61200dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
61300dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
61400dd4998SJames Bottomley 
61500dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
61600dd4998SJames Bottomley 				continue;
61700dd4998SJames Bottomley 			if (ap && ap != ddev->sata_dev.ap)
61800dd4998SJames Bottomley 				continue;
61900dd4998SJames Bottomley 			ap = ddev->sata_dev.ap;
62000dd4998SJames Bottomley 			rtn = 1;
62100dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
62200dd4998SJames Bottomley 		}
62300dd4998SJames Bottomley 
62400dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
62500dd4998SJames Bottomley 			ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
62600dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
627a82058a7SJames Bottomley 			/*
628a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
629a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
630a82058a7SJames Bottomley 			 * about to go out of scope.
631a82058a7SJames Bottomley 			 *
632a82058a7SJames Bottomley 			 * This looks strange, since the commands are
633a82058a7SJames Bottomley 			 * now part of no list, but the next error
634a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
635a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
636a82058a7SJames Bottomley 			 * anyway from the ata tag array.
637a82058a7SJames Bottomley 			 */
638a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
639a82058a7SJames Bottomley 				list_del_init(sata_q.next);
64000dd4998SJames Bottomley 		}
64100dd4998SJames Bottomley 	} while (ap);
64200dd4998SJames Bottomley 
64300dd4998SJames Bottomley 	return rtn;
64400dd4998SJames Bottomley }
645