xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision e139942d77a6e3ac83bc322e826668054a8601d6)
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 */
187*e139942dSDan 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 
370338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
37100dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
3721ca1e43eSDave Jiang 	.softreset		= sas_ata_soft_reset,
37300dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
37400dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
37500dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
376338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
377f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
378338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
379338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
3804c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
381338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
382338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
383338ec570SDarrick J. Wong };
384338ec570SDarrick J. Wong 
385338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
3869cbe056fSSergei Shtylyov 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
3870f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
3880f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
389338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
390338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
391338ec570SDarrick J. Wong };
392338ec570SDarrick J. Wong 
393338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
394338ec570SDarrick J. Wong 			       struct scsi_target *starget)
395338ec570SDarrick J. Wong {
396338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
397338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
398338ec570SDarrick J. Wong 	struct ata_port *ap;
399338ec570SDarrick J. Wong 
400338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
4011d1bbee6SJeff Garzik 		      ha->dev,
402338ec570SDarrick J. Wong 		      sata_port_info.flags,
403338ec570SDarrick J. Wong 		      &sas_sata_ops);
404338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
405338ec570SDarrick J. Wong 				&sata_port_info,
406338ec570SDarrick J. Wong 				shost);
407338ec570SDarrick J. Wong 	if (!ap) {
408338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
409338ec570SDarrick J. Wong 		return -ENODEV;
410338ec570SDarrick J. Wong 	}
411338ec570SDarrick J. Wong 
412338ec570SDarrick J. Wong 	ap->private_data = found_dev;
413338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
414338ec570SDarrick J. Wong 	ap->scsi_host = shost;
415338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
416338ec570SDarrick J. Wong 
417338ec570SDarrick J. Wong 	return 0;
418338ec570SDarrick J. Wong }
4193a2755afSDarrick J. Wong 
4203a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
4213a2755afSDarrick J. Wong {
4223a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
4233a2755afSDarrick J. Wong 	struct completion *waiting;
4243a2755afSDarrick J. Wong 
4253a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
4263a2755afSDarrick J. Wong 	if (qc->scsicmd) {
4271b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
4281b4d0d8eSJames Bottomley 		unsigned long flags;
4291b4d0d8eSJames Bottomley 
43070b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
431242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
43270b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
4333a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
4343a2755afSDarrick J. Wong 		return;
4353a2755afSDarrick J. Wong 	}
4363a2755afSDarrick J. Wong 
4373a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
4383a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4393a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
4403a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
4413a2755afSDarrick J. Wong 	waiting = qc->private_data;
4423a2755afSDarrick J. Wong 	complete(waiting);
4433a2755afSDarrick J. Wong }
444b9142174SJames Bottomley 
445b9142174SJames Bottomley static void sas_task_timedout(unsigned long _task)
446b9142174SJames Bottomley {
447b9142174SJames Bottomley 	struct sas_task *task = (void *) _task;
448b9142174SJames Bottomley 	unsigned long flags;
449b9142174SJames Bottomley 
450b9142174SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
451b9142174SJames Bottomley 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
452b9142174SJames Bottomley 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
453b9142174SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
454b9142174SJames Bottomley 
455b9142174SJames Bottomley 	complete(&task->completion);
456b9142174SJames Bottomley }
457b9142174SJames Bottomley 
458b9142174SJames Bottomley static void sas_disc_task_done(struct sas_task *task)
459b9142174SJames Bottomley {
460b9142174SJames Bottomley 	if (!del_timer(&task->timer))
461b9142174SJames Bottomley 		return;
462b9142174SJames Bottomley 	complete(&task->completion);
463b9142174SJames Bottomley }
464b9142174SJames Bottomley 
465b9142174SJames Bottomley #define SAS_DEV_TIMEOUT 10
466b9142174SJames Bottomley 
467b9142174SJames Bottomley /**
468b9142174SJames Bottomley  * sas_execute_task -- Basic task processing for discovery
469b9142174SJames Bottomley  * @task: the task to be executed
470b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
471b9142174SJames Bottomley  * @size: size of @buffer
4721d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
473b9142174SJames Bottomley  */
474b9142174SJames Bottomley static int sas_execute_task(struct sas_task *task, void *buffer, int size,
4751d1bbee6SJeff Garzik 			    enum dma_data_direction dma_dir)
476b9142174SJames Bottomley {
477b9142174SJames Bottomley 	int res = 0;
478b9142174SJames Bottomley 	struct scatterlist *scatter = NULL;
479b9142174SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
480b9142174SJames Bottomley 	int num_scatter = 0;
481b9142174SJames Bottomley 	int retries = 0;
482b9142174SJames Bottomley 	struct sas_internal *i =
483b9142174SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
484b9142174SJames Bottomley 
4851d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
486b9142174SJames Bottomley 		scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
487b9142174SJames Bottomley 		if (!scatter)
488b9142174SJames Bottomley 			goto out;
489b9142174SJames Bottomley 
490b9142174SJames Bottomley 		sg_init_one(scatter, buffer, size);
491b9142174SJames Bottomley 		num_scatter = 1;
492b9142174SJames Bottomley 	}
493b9142174SJames Bottomley 
494b9142174SJames Bottomley 	task->task_proto = task->dev->tproto;
495b9142174SJames Bottomley 	task->scatter = scatter;
496b9142174SJames Bottomley 	task->num_scatter = num_scatter;
497b9142174SJames Bottomley 	task->total_xfer_len = size;
4981d1bbee6SJeff Garzik 	task->data_dir = dma_dir;
499b9142174SJames Bottomley 	task->task_done = sas_disc_task_done;
5001d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE &&
501b9142174SJames Bottomley 	    sas_protocol_ata(task->task_proto)) {
5021d1bbee6SJeff Garzik 		task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
503b9142174SJames Bottomley 					       task->scatter,
504b9142174SJames Bottomley 					       task->num_scatter,
505b9142174SJames Bottomley 					       task->data_dir);
506b9142174SJames Bottomley 	}
507b9142174SJames Bottomley 
508b9142174SJames Bottomley 	for (retries = 0; retries < 5; retries++) {
509b9142174SJames Bottomley 		task->task_state_flags = SAS_TASK_STATE_PENDING;
510b9142174SJames Bottomley 		init_completion(&task->completion);
511b9142174SJames Bottomley 
512b9142174SJames Bottomley 		task->timer.data = (unsigned long) task;
513b9142174SJames Bottomley 		task->timer.function = sas_task_timedout;
514b9142174SJames Bottomley 		task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
515b9142174SJames Bottomley 		add_timer(&task->timer);
516b9142174SJames Bottomley 
517b9142174SJames Bottomley 		res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
518b9142174SJames Bottomley 		if (res) {
519b9142174SJames Bottomley 			del_timer(&task->timer);
520b9142174SJames Bottomley 			SAS_DPRINTK("executing SAS discovery task failed:%d\n",
521b9142174SJames Bottomley 				    res);
522b9142174SJames Bottomley 			goto ex_err;
523b9142174SJames Bottomley 		}
524b9142174SJames Bottomley 		wait_for_completion(&task->completion);
52532e8ae36SJames Bottomley 		res = -ECOMM;
526b9142174SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
527b9142174SJames Bottomley 			int res2;
528b9142174SJames Bottomley 			SAS_DPRINTK("task aborted, flags:0x%x\n",
529b9142174SJames Bottomley 				    task->task_state_flags);
530b9142174SJames Bottomley 			res2 = i->dft->lldd_abort_task(task);
531b9142174SJames Bottomley 			SAS_DPRINTK("came back from abort task\n");
532b9142174SJames Bottomley 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
533b9142174SJames Bottomley 				if (res2 == TMF_RESP_FUNC_COMPLETE)
534b9142174SJames Bottomley 					continue; /* Retry the task */
535b9142174SJames Bottomley 				else
536b9142174SJames Bottomley 					goto ex_err;
537b9142174SJames Bottomley 			}
538b9142174SJames Bottomley 		}
539df64d3caSJames Bottomley 		if (task->task_status.stat == SAM_STAT_BUSY ||
540df64d3caSJames Bottomley 			   task->task_status.stat == SAM_STAT_TASK_SET_FULL ||
541b9142174SJames Bottomley 			   task->task_status.stat == SAS_QUEUE_FULL) {
542b9142174SJames Bottomley 			SAS_DPRINTK("task: q busy, sleeping...\n");
543b9142174SJames Bottomley 			schedule_timeout_interruptible(HZ);
544df64d3caSJames Bottomley 		} else if (task->task_status.stat == SAM_STAT_CHECK_CONDITION) {
545b9142174SJames Bottomley 			struct scsi_sense_hdr shdr;
546b9142174SJames Bottomley 
547b9142174SJames Bottomley 			if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
548b9142174SJames Bottomley 						  &shdr)) {
549b9142174SJames Bottomley 				SAS_DPRINTK("couldn't normalize sense\n");
550b9142174SJames Bottomley 				continue;
551b9142174SJames Bottomley 			}
552b9142174SJames Bottomley 			if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
553b9142174SJames Bottomley 			    (shdr.sense_key == 2 && shdr.asc == 4 &&
554b9142174SJames Bottomley 			     shdr.ascq == 1)) {
555b9142174SJames Bottomley 				SAS_DPRINTK("device %016llx LUN: %016llx "
556b9142174SJames Bottomley 					    "powering up or not ready yet, "
557b9142174SJames Bottomley 					    "sleeping...\n",
558b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
559b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN));
560b9142174SJames Bottomley 
561b9142174SJames Bottomley 				schedule_timeout_interruptible(5*HZ);
562b9142174SJames Bottomley 			} else if (shdr.sense_key == 1) {
563b9142174SJames Bottomley 				res = 0;
564b9142174SJames Bottomley 				break;
565b9142174SJames Bottomley 			} else if (shdr.sense_key == 5) {
566b9142174SJames Bottomley 				break;
567b9142174SJames Bottomley 			} else {
568b9142174SJames Bottomley 				SAS_DPRINTK("dev %016llx LUN: %016llx "
569b9142174SJames Bottomley 					    "sense key:0x%x ASC:0x%x ASCQ:0x%x"
570b9142174SJames Bottomley 					    "\n",
571b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
572b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN),
573b9142174SJames Bottomley 					    shdr.sense_key,
574b9142174SJames Bottomley 					    shdr.asc, shdr.ascq);
575b9142174SJames Bottomley 			}
576b9142174SJames Bottomley 		} else if (task->task_status.resp != SAS_TASK_COMPLETE ||
577df64d3caSJames Bottomley 			   task->task_status.stat != SAM_STAT_GOOD) {
578b9142174SJames Bottomley 			SAS_DPRINTK("task finished with resp:0x%x, "
579b9142174SJames Bottomley 				    "stat:0x%x\n",
580b9142174SJames Bottomley 				    task->task_status.resp,
581b9142174SJames Bottomley 				    task->task_status.stat);
582b9142174SJames Bottomley 			goto ex_err;
583b9142174SJames Bottomley 		} else {
584b9142174SJames Bottomley 			res = 0;
585b9142174SJames Bottomley 			break;
586b9142174SJames Bottomley 		}
587b9142174SJames Bottomley 	}
588b9142174SJames Bottomley ex_err:
5891d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
590b9142174SJames Bottomley 		if (sas_protocol_ata(task->task_proto))
5911d1bbee6SJeff Garzik 			dma_unmap_sg(task->dev->port->ha->dev,
592b9142174SJames Bottomley 				     task->scatter, task->num_scatter,
593b9142174SJames Bottomley 				     task->data_dir);
594b9142174SJames Bottomley 		kfree(scatter);
595b9142174SJames Bottomley 	}
596b9142174SJames Bottomley out:
597b9142174SJames Bottomley 	return res;
598b9142174SJames Bottomley }
599b9142174SJames Bottomley 
600b9142174SJames Bottomley /* ---------- SATA ---------- */
601b9142174SJames Bottomley 
602b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
603b9142174SJames Bottomley {
604b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
605b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
606b9142174SJames Bottomley 
607b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
608b9142174SJames Bottomley 	     fis->lbal         == 1 &&
609b9142174SJames Bottomley 	     fis->lbam         == 0 &&
610b9142174SJames Bottomley 	     fis->lbah         == 0 &&
611b9142174SJames Bottomley 	     fis->device       == 0)
612b9142174SJames Bottomley 	    ||
613b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
614b9142174SJames Bottomley 	     fis->lbal         == 0 &&
615b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
616b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
617b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
618b9142174SJames Bottomley 
619b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
620b9142174SJames Bottomley 
621b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
622b9142174SJames Bottomley 		  fis->lbal             == 1 &&
623b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
624b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
625b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
626b9142174SJames Bottomley 
627b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
628b9142174SJames Bottomley 
629b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
630b9142174SJames Bottomley 		  fis->lbal         == 1 &&
631b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
632b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
633b9142174SJames Bottomley 		  fis->device       == 0)
634b9142174SJames Bottomley 		||
635b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
636b9142174SJames Bottomley 		  fis->lbal             == 1 &&
637b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
638b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
639b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
640b9142174SJames Bottomley 
641b9142174SJames Bottomley 		/* Treat it as a superset? */
642b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
643b9142174SJames Bottomley }
644b9142174SJames Bottomley 
645b9142174SJames Bottomley /**
646b9142174SJames Bottomley  * sas_issue_ata_cmd -- Basic SATA command processing for discovery
647b9142174SJames Bottomley  * @dev: the device to send the command to
648b9142174SJames Bottomley  * @command: the command register
649b9142174SJames Bottomley  * @features: the features register
650b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
651b9142174SJames Bottomley  * @size: size of @buffer
6521d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
653b9142174SJames Bottomley  */
654b9142174SJames Bottomley static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
655b9142174SJames Bottomley 			     u8 features, void *buffer, int size,
6561d1bbee6SJeff Garzik 			     enum dma_data_direction dma_dir)
657b9142174SJames Bottomley {
658b9142174SJames Bottomley 	int res = 0;
659b9142174SJames Bottomley 	struct sas_task *task;
660b9142174SJames Bottomley 	struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
661b9142174SJames Bottomley 		&dev->frame_rcvd[0];
662b9142174SJames Bottomley 
663b9142174SJames Bottomley 	res = -ENOMEM;
664b9142174SJames Bottomley 	task = sas_alloc_task(GFP_KERNEL);
665b9142174SJames Bottomley 	if (!task)
666b9142174SJames Bottomley 		goto out;
667b9142174SJames Bottomley 
668b9142174SJames Bottomley 	task->dev = dev;
669b9142174SJames Bottomley 
670b9142174SJames Bottomley 	task->ata_task.fis.fis_type = 0x27;
671b9142174SJames Bottomley 	task->ata_task.fis.command = command;
672b9142174SJames Bottomley 	task->ata_task.fis.features = features;
673b9142174SJames Bottomley 	task->ata_task.fis.device = d2h_fis->device;
674b9142174SJames Bottomley 	task->ata_task.retry_count = 1;
675b9142174SJames Bottomley 
6761d1bbee6SJeff Garzik 	res = sas_execute_task(task, buffer, size, dma_dir);
677b9142174SJames Bottomley 
678b9142174SJames Bottomley 	sas_free_task(task);
679b9142174SJames Bottomley out:
680b9142174SJames Bottomley 	return res;
681b9142174SJames Bottomley }
682b9142174SJames Bottomley 
683b9142174SJames Bottomley #define ATA_IDENTIFY_DEV         0xEC
684b9142174SJames Bottomley #define ATA_IDENTIFY_PACKET_DEV  0xA1
685b9142174SJames Bottomley #define ATA_SET_FEATURES         0xEF
686b9142174SJames Bottomley #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
687b9142174SJames Bottomley 
688b9142174SJames Bottomley /**
689b9142174SJames Bottomley  * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
690b9142174SJames Bottomley  * @dev: STP/SATA device of interest (ATA/ATAPI)
691b9142174SJames Bottomley  *
692b9142174SJames Bottomley  * The LLDD has already been notified of this device, so that we can
693b9142174SJames Bottomley  * send FISes to it.  Here we try to get IDENTIFY DEVICE or IDENTIFY
694b9142174SJames Bottomley  * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
695b9142174SJames Bottomley  * performance for this device.
696b9142174SJames Bottomley  */
697b9142174SJames Bottomley static int sas_discover_sata_dev(struct domain_device *dev)
698b9142174SJames Bottomley {
699b9142174SJames Bottomley 	int     res;
700b9142174SJames Bottomley 	__le16  *identify_x;
701b9142174SJames Bottomley 	u8      command;
702b9142174SJames Bottomley 
703b9142174SJames Bottomley 	identify_x = kzalloc(512, GFP_KERNEL);
704b9142174SJames Bottomley 	if (!identify_x)
705b9142174SJames Bottomley 		return -ENOMEM;
706b9142174SJames Bottomley 
707b9142174SJames Bottomley 	if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
708b9142174SJames Bottomley 		dev->sata_dev.identify_device = identify_x;
709b9142174SJames Bottomley 		command = ATA_IDENTIFY_DEV;
710b9142174SJames Bottomley 	} else {
711b9142174SJames Bottomley 		dev->sata_dev.identify_packet_device = identify_x;
712b9142174SJames Bottomley 		command = ATA_IDENTIFY_PACKET_DEV;
713b9142174SJames Bottomley 	}
714b9142174SJames Bottomley 
715b9142174SJames Bottomley 	res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
7161d1bbee6SJeff Garzik 				DMA_FROM_DEVICE);
717b9142174SJames Bottomley 	if (res)
718b9142174SJames Bottomley 		goto out_err;
719b9142174SJames Bottomley 
720b9142174SJames Bottomley 	/* lives on the media? */
721b9142174SJames Bottomley 	if (le16_to_cpu(identify_x[0]) & 4) {
722b9142174SJames Bottomley 		/* incomplete response */
723b9142174SJames Bottomley 		SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
724b9142174SJames Bottomley 			    "dev %llx\n", SAS_ADDR(dev->sas_addr));
72517b7a8deSAl Viro 		if (!(identify_x[83] & cpu_to_le16(1<<6)))
726b9142174SJames Bottomley 			goto cont1;
727b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
728b9142174SJames Bottomley 					ATA_FEATURE_PUP_STBY_SPIN_UP,
7291d1bbee6SJeff Garzik 					NULL, 0, DMA_NONE);
730b9142174SJames Bottomley 		if (res)
731b9142174SJames Bottomley 			goto cont1;
732b9142174SJames Bottomley 
733b9142174SJames Bottomley 		schedule_timeout_interruptible(5*HZ); /* More time? */
734b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
7351d1bbee6SJeff Garzik 					DMA_FROM_DEVICE);
736b9142174SJames Bottomley 		if (res)
737b9142174SJames Bottomley 			goto out_err;
738b9142174SJames Bottomley 	}
739b9142174SJames Bottomley cont1:
740b9142174SJames Bottomley 	/* XXX Hint: register this SATA device with SATL.
741b9142174SJames Bottomley 	   When this returns, dev->sata_dev->lu is alive and
742b9142174SJames Bottomley 	   present.
743b9142174SJames Bottomley 	sas_satl_register_dev(dev);
744b9142174SJames Bottomley 	*/
745b9142174SJames Bottomley 
746b9142174SJames Bottomley 	sas_fill_in_rphy(dev, dev->rphy);
747b9142174SJames Bottomley 
748b9142174SJames Bottomley 	return 0;
749b9142174SJames Bottomley out_err:
750b9142174SJames Bottomley 	dev->sata_dev.identify_packet_device = NULL;
751b9142174SJames Bottomley 	dev->sata_dev.identify_device = NULL;
752b9142174SJames Bottomley 	kfree(identify_x);
753b9142174SJames Bottomley 	return res;
754b9142174SJames Bottomley }
755b9142174SJames Bottomley 
756b9142174SJames Bottomley static int sas_discover_sata_pm(struct domain_device *dev)
757b9142174SJames Bottomley {
758b9142174SJames Bottomley 	return -ENODEV;
759b9142174SJames Bottomley }
760b9142174SJames Bottomley 
761b9142174SJames Bottomley /**
762b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
763b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
764b9142174SJames Bottomley  *
765b9142174SJames Bottomley  * First we notify the LLDD of this device, so we can send frames to
766b9142174SJames Bottomley  * it.  Then depending on the type of device we call the appropriate
767b9142174SJames Bottomley  * discover functions.  Once device discover is done, we notify the
768b9142174SJames Bottomley  * LLDD so that it can fine-tune its parameters for the device, by
769b9142174SJames Bottomley  * removing it and then adding it.  That is, the second time around,
770b9142174SJames Bottomley  * the driver would have certain fields, that it is looking at, set.
771b9142174SJames Bottomley  * Finally we initialize the kobj so that the device can be added to
772b9142174SJames Bottomley  * the system at registration time.  Devices directly attached to a HA
773b9142174SJames Bottomley  * port, have no parents.  All other devices do, and should have their
774b9142174SJames Bottomley  * "parent" pointer set appropriately before calling this function.
775b9142174SJames Bottomley  */
776b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
777b9142174SJames Bottomley {
778b9142174SJames Bottomley 	int res;
779b9142174SJames Bottomley 
780b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
781b9142174SJames Bottomley 
782b9142174SJames Bottomley 	res = sas_notify_lldd_dev_found(dev);
783b9142174SJames Bottomley 	if (res)
784b9142174SJames Bottomley 		return res;
785b9142174SJames Bottomley 
786b9142174SJames Bottomley 	switch (dev->dev_type) {
787b9142174SJames Bottomley 	case SATA_DEV:
788b9142174SJames Bottomley 		res = sas_discover_sata_dev(dev);
789b9142174SJames Bottomley 		break;
790b9142174SJames Bottomley 	case SATA_PM:
791b9142174SJames Bottomley 		res = sas_discover_sata_pm(dev);
792b9142174SJames Bottomley 		break;
793b9142174SJames Bottomley 	default:
794b9142174SJames Bottomley 		break;
795b9142174SJames Bottomley 	}
796b9142174SJames Bottomley 	sas_notify_lldd_dev_gone(dev);
797b9142174SJames Bottomley 	if (!res) {
798b9142174SJames Bottomley 		sas_notify_lldd_dev_found(dev);
799b9142174SJames Bottomley 		res = sas_rphy_add(dev->rphy);
800b9142174SJames Bottomley 	}
801b9142174SJames Bottomley 
802b9142174SJames Bottomley 	return res;
803b9142174SJames Bottomley }
80400dd4998SJames Bottomley 
80500dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
80600dd4998SJames Bottomley {
80700dd4998SJames Bottomley 	struct scsi_device *sdev;
80800dd4998SJames Bottomley 
80900dd4998SJames Bottomley 	shost_for_each_device(sdev, shost) {
81000dd4998SJames Bottomley 		struct domain_device *ddev = sdev_to_domain_dev(sdev);
81100dd4998SJames Bottomley 		struct ata_port *ap = ddev->sata_dev.ap;
81200dd4998SJames Bottomley 
81300dd4998SJames Bottomley 		if (!dev_is_sata(ddev))
81400dd4998SJames Bottomley 			continue;
81500dd4998SJames Bottomley 
81600dd4998SJames Bottomley 		ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
81700dd4998SJames Bottomley 		ata_scsi_port_error_handler(shost, ap);
81800dd4998SJames Bottomley 	}
81900dd4998SJames Bottomley }
82000dd4998SJames Bottomley 
82100dd4998SJames Bottomley int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,
82200dd4998SJames Bottomley 		      enum blk_eh_timer_return *rtn)
82300dd4998SJames Bottomley {
82400dd4998SJames Bottomley 	struct domain_device *ddev = cmd_to_domain_dev(cmd);
82500dd4998SJames Bottomley 
82600dd4998SJames Bottomley 	if (!dev_is_sata(ddev) || task)
82700dd4998SJames Bottomley 		return 0;
82800dd4998SJames Bottomley 
82900dd4998SJames Bottomley 	/* we're a sata device with no task, so this must be a libata
83000dd4998SJames Bottomley 	 * eh timeout.  Ideally should hook into libata timeout
83100dd4998SJames Bottomley 	 * handling, but there's no point, it just wants to activate
83200dd4998SJames Bottomley 	 * the eh thread */
83300dd4998SJames Bottomley 	*rtn = BLK_EH_NOT_HANDLED;
83400dd4998SJames Bottomley 	return 1;
83500dd4998SJames Bottomley }
83600dd4998SJames Bottomley 
83700dd4998SJames Bottomley int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
83800dd4998SJames Bottomley 	       struct list_head *done_q)
83900dd4998SJames Bottomley {
84000dd4998SJames Bottomley 	int rtn = 0;
84100dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
84200dd4998SJames Bottomley 	struct ata_port *ap;
84300dd4998SJames Bottomley 
84400dd4998SJames Bottomley 	do {
84500dd4998SJames Bottomley 		LIST_HEAD(sata_q);
84600dd4998SJames Bottomley 
84700dd4998SJames Bottomley 		ap = NULL;
84800dd4998SJames Bottomley 
84900dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
85000dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
85100dd4998SJames Bottomley 
85200dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
85300dd4998SJames Bottomley 				continue;
85400dd4998SJames Bottomley 			if (ap && ap != ddev->sata_dev.ap)
85500dd4998SJames Bottomley 				continue;
85600dd4998SJames Bottomley 			ap = ddev->sata_dev.ap;
85700dd4998SJames Bottomley 			rtn = 1;
85800dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
85900dd4998SJames Bottomley 		}
86000dd4998SJames Bottomley 
86100dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
86200dd4998SJames Bottomley 			ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
86300dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
864a82058a7SJames Bottomley 			/*
865a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
866a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
867a82058a7SJames Bottomley 			 * about to go out of scope.
868a82058a7SJames Bottomley 			 *
869a82058a7SJames Bottomley 			 * This looks strange, since the commands are
870a82058a7SJames Bottomley 			 * now part of no list, but the next error
871a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
872a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
873a82058a7SJames Bottomley 			 * anyway from the ata tag array.
874a82058a7SJames Bottomley 			 */
875a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
876a82058a7SJames Bottomley 				list_del_init(sata_q.next);
87700dd4998SJames Bottomley 		}
87800dd4998SJames Bottomley 	} while (ap);
87900dd4998SJames Bottomley 
88000dd4998SJames Bottomley 	return rtn;
88100dd4998SJames Bottomley }
882