xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision bb650a1bef73a8c1fd076fae4c4f0701cf3b5f25)
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;
102*bb650a1bSXiangliang 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;
109*bb650a1bSXiangliang 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);
116*bb650a1bSXiangliang Yu 
117*bb650a1bSXiangliang Yu 		if (!link->sactive) {
118338ec570SDarrick J. Wong 			qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
119*bb650a1bSXiangliang Yu 		} else {
120*bb650a1bSXiangliang Yu 			link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
121*bb650a1bSXiangliang Yu 			if (unlikely(link->eh_info.err_mask))
122*bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
123*bb650a1bSXiangliang Yu 		}
124*bb650a1bSXiangliang Yu 
125338ec570SDarrick J. Wong 		dev->sata_dev.sstatus = resp->sstatus;
126338ec570SDarrick J. Wong 		dev->sata_dev.serror = resp->serror;
127338ec570SDarrick J. Wong 		dev->sata_dev.scontrol = resp->scontrol;
12875c0b386SJames Bottomley 	} else {
129338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
130338ec570SDarrick J. Wong 		if (ac) {
131cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: SAS error %x\n", __func__,
132338ec570SDarrick J. Wong 				    stat->stat);
133338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
134*bb650a1bSXiangliang Yu 			if (!link->sactive) {
135338ec570SDarrick J. Wong 				qc->err_mask = ac;
136*bb650a1bSXiangliang Yu 			} else {
137*bb650a1bSXiangliang Yu 				link->eh_info.err_mask |= AC_ERR_DEV;
138*bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
139*bb650a1bSXiangliang Yu 			}
140*bb650a1bSXiangliang Yu 
141338ec570SDarrick J. Wong 			dev->sata_dev.tf.feature = 0x04; /* status err */
142338ec570SDarrick J. Wong 			dev->sata_dev.tf.command = ATA_ERR;
143338ec570SDarrick J. Wong 		}
144338ec570SDarrick J. Wong 	}
145338ec570SDarrick J. Wong 
1461c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
147fe059f12SDarrick J. Wong 	if (qc->scsicmd)
148fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
149338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1503eb7a51aSDarrick J. Wong 	spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
1513eb7a51aSDarrick J. Wong 
1523a2755afSDarrick J. Wong 	/*
1533a2755afSDarrick J. Wong 	 * If the sas_task has an ata qc, a scsi_cmnd and the aborted
1543a2755afSDarrick J. Wong 	 * flag is set, then we must have come in via the libsas EH
1553a2755afSDarrick J. Wong 	 * functions.  When we exit this function, we need to put the
1563a2755afSDarrick J. Wong 	 * scsi_cmnd on the list of finished errors.  The ata_qc_complete
1573a2755afSDarrick J. Wong 	 * call cleans up the libata side of things but we're protected
1583a2755afSDarrick J. Wong 	 * from the scsi_cmnd going away because the scsi_cmnd is owned
1593a2755afSDarrick J. Wong 	 * by the EH, making libata's call to scsi_done a NOP.
1603a2755afSDarrick J. Wong 	 */
1613a2755afSDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
1623a2755afSDarrick J. Wong 	if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
1633a2755afSDarrick J. Wong 		scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
1643a2755afSDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1653a2755afSDarrick J. Wong 
1661c50dc83SDarrick J. Wong qc_already_gone:
167338ec570SDarrick J. Wong 	list_del_init(&task->list);
168338ec570SDarrick J. Wong 	sas_free_task(task);
169338ec570SDarrick J. Wong }
170338ec570SDarrick J. Wong 
171338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
172338ec570SDarrick J. Wong {
17335a7f2f6SDarrick J. Wong 	int res;
174338ec570SDarrick J. Wong 	struct sas_task *task;
175338ec570SDarrick J. Wong 	struct domain_device *dev = qc->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);
179338ec570SDarrick J. Wong 	struct scatterlist *sg;
180338ec570SDarrick J. Wong 	unsigned int xfer = 0;
181ff2aeb1eSTejun Heo 	unsigned int si;
182338ec570SDarrick J. Wong 
18356dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
18456dd2c06SDarrick J. Wong 	if (dev->gone)
18556dd2c06SDarrick J. Wong 		return AC_ERR_SYSTEM;
18656dd2c06SDarrick J. Wong 
187338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
188338ec570SDarrick J. Wong 	if (!task)
18935a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
190338ec570SDarrick J. Wong 	task->dev = dev;
191338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
192338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
193338ec570SDarrick J. Wong 
194338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
195338ec570SDarrick J. Wong 	    qc->tf.command == ATA_CMD_FPDMA_READ) {
196338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
197338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
198338ec570SDarrick J. Wong 	}
199338ec570SDarrick J. Wong 
200110dd8f1SJames Bottomley 	ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
201338ec570SDarrick J. Wong 	task->uldd_task = qc;
202405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
203338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
204dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
205dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
206338ec570SDarrick J. Wong 	} else {
207ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
208338ec570SDarrick J. Wong 			xfer += sg->length;
209338ec570SDarrick J. Wong 
210338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
211ff2aeb1eSTejun Heo 		task->num_scatter = si;
212338ec570SDarrick J. Wong 	}
213338ec570SDarrick J. Wong 
214338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
215ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
216338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
217338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
2181c50dc83SDarrick J. Wong 	qc->lldd_task = task;
219338ec570SDarrick J. Wong 
220338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
221338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
222338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
223338ec570SDarrick J. Wong 		/* fall through */
2240dc36888STejun Heo 	case ATAPI_PROT_DMA:
225338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
226338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
227338ec570SDarrick J. Wong 		break;
228338ec570SDarrick J. Wong 	}
229338ec570SDarrick J. Wong 
230fe059f12SDarrick J. Wong 	if (qc->scsicmd)
231fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
232fe059f12SDarrick J. Wong 
233338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
234338ec570SDarrick J. Wong 		res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
235338ec570SDarrick J. Wong 	else
236338ec570SDarrick J. Wong 		res = sas_queue_up(task);
237338ec570SDarrick J. Wong 
238338ec570SDarrick J. Wong 	/* Examine */
239338ec570SDarrick J. Wong 	if (res) {
240338ec570SDarrick J. Wong 		SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
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);
24535a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
246338ec570SDarrick J. Wong 	}
247338ec570SDarrick J. Wong 
24835a7f2f6SDarrick J. Wong 	return 0;
249338ec570SDarrick J. Wong }
250338ec570SDarrick J. Wong 
2514c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2524c9bf4e7STejun Heo {
2534c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2544c9bf4e7STejun Heo 
2554c9bf4e7STejun Heo 	memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
2564c9bf4e7STejun Heo 	return true;
2574c9bf4e7STejun Heo }
2584c9bf4e7STejun Heo 
25900dd4998SJames Bottomley static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
26000dd4998SJames Bottomley 			       unsigned long deadline)
261338ec570SDarrick J. Wong {
26200dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
263338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
264338ec570SDarrick J. Wong 	struct sas_internal *i =
265338ec570SDarrick J. Wong 		to_sas_internal(dev->port->ha->core.shost->transportt);
266a29c0515SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
26700dd4998SJames Bottomley 	int ret = 0;
268338ec570SDarrick J. Wong 
269338ec570SDarrick J. Wong 	if (i->dft->lldd_I_T_nexus_reset)
270338ec570SDarrick J. Wong 		res = i->dft->lldd_I_T_nexus_reset(dev);
271338ec570SDarrick J. Wong 
27200dd4998SJames Bottomley 	if (res != TMF_RESP_FUNC_COMPLETE) {
273cadbd4a5SHarvey Harrison 		SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
27400dd4998SJames Bottomley 		ret = -EAGAIN;
27500dd4998SJames Bottomley 	}
276338ec570SDarrick J. Wong 
277338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
278338ec570SDarrick J. Wong 		case ATA_COMMAND_SET:
279cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: Found ATA device.\n", __func__);
28000dd4998SJames Bottomley 			*class = ATA_DEV_ATA;
281338ec570SDarrick J. Wong 			break;
282338ec570SDarrick J. Wong 		case ATAPI_COMMAND_SET:
283cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
28400dd4998SJames Bottomley 			*class = ATA_DEV_ATAPI;
285338ec570SDarrick J. Wong 			break;
286338ec570SDarrick J. Wong 		default:
287338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
288cadbd4a5SHarvey Harrison 				    __func__,
289338ec570SDarrick J. Wong 				    dev->sata_dev.command_set);
29000dd4998SJames Bottomley 			*class = ATA_DEV_UNKNOWN;
291338ec570SDarrick J. Wong 			break;
292338ec570SDarrick J. Wong 	}
293338ec570SDarrick J. Wong 
294338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
29500dd4998SJames Bottomley 	return ret;
296338ec570SDarrick J. Wong }
297338ec570SDarrick J. Wong 
298338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
299338ec570SDarrick J. Wong {
300338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
301338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
302338ec570SDarrick J. Wong 
3031c50dc83SDarrick J. Wong 	if (qc->err_mask) {
3041c50dc83SDarrick J. Wong 		/*
3051c50dc83SDarrick J. Wong 		 * Find the sas_task and kill it.  By this point,
3061c50dc83SDarrick J. Wong 		 * libata has decided to kill the qc, so we needn't
3071c50dc83SDarrick J. Wong 		 * bother with sas_ata_task_done.  But we still
3081c50dc83SDarrick J. Wong 		 * ought to abort the task.
3091c50dc83SDarrick J. Wong 		 */
3101c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
3113a2755afSDarrick J. Wong 		unsigned long flags;
3121c50dc83SDarrick J. Wong 
3131c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
3141c50dc83SDarrick J. Wong 		if (task) {
3153a2755afSDarrick J. Wong 			/* Should this be a AT(API) device reset? */
3163a2755afSDarrick J. Wong 			spin_lock_irqsave(&task->task_state_lock, flags);
3173a2755afSDarrick J. Wong 			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3183a2755afSDarrick J. Wong 			spin_unlock_irqrestore(&task->task_state_lock, flags);
3193a2755afSDarrick J. Wong 
3201c50dc83SDarrick J. Wong 			task->uldd_task = NULL;
3211c50dc83SDarrick J. Wong 			__sas_task_abort(task);
3221c50dc83SDarrick J. Wong 		}
3231c50dc83SDarrick J. Wong 	}
324338ec570SDarrick J. Wong }
325338ec570SDarrick J. Wong 
326338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
32700dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
32800dd4998SJames Bottomley 	.softreset		= NULL,
32900dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
33000dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
33100dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
332338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
333f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
334338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
335338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
3364c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
337338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
338338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
339338ec570SDarrick J. Wong };
340338ec570SDarrick J. Wong 
341338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
3429cbe056fSSergei Shtylyov 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
3430f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
3440f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
345338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
346338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
347338ec570SDarrick J. Wong };
348338ec570SDarrick J. Wong 
349338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
350338ec570SDarrick J. Wong 			       struct scsi_target *starget)
351338ec570SDarrick J. Wong {
352338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
353338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
354338ec570SDarrick J. Wong 	struct ata_port *ap;
355338ec570SDarrick J. Wong 
356338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
3571d1bbee6SJeff Garzik 		      ha->dev,
358338ec570SDarrick J. Wong 		      sata_port_info.flags,
359338ec570SDarrick J. Wong 		      &sas_sata_ops);
360338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
361338ec570SDarrick J. Wong 				&sata_port_info,
362338ec570SDarrick J. Wong 				shost);
363338ec570SDarrick J. Wong 	if (!ap) {
364338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
365338ec570SDarrick J. Wong 		return -ENODEV;
366338ec570SDarrick J. Wong 	}
367338ec570SDarrick J. Wong 
368338ec570SDarrick J. Wong 	ap->private_data = found_dev;
369338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
370338ec570SDarrick J. Wong 	ap->scsi_host = shost;
371338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
372338ec570SDarrick J. Wong 
373338ec570SDarrick J. Wong 	return 0;
374338ec570SDarrick J. Wong }
3753a2755afSDarrick J. Wong 
3763a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
3773a2755afSDarrick J. Wong {
3783a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
3793a2755afSDarrick J. Wong 	struct completion *waiting;
3803a2755afSDarrick J. Wong 
3813a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
3823a2755afSDarrick J. Wong 	if (qc->scsicmd) {
3831b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
3841b4d0d8eSJames Bottomley 		unsigned long flags;
3851b4d0d8eSJames Bottomley 
38670b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
387242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
38870b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
3893a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
3903a2755afSDarrick J. Wong 		return;
3913a2755afSDarrick J. Wong 	}
3923a2755afSDarrick J. Wong 
3933a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
3943a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
3953a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
3963a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
3973a2755afSDarrick J. Wong 	waiting = qc->private_data;
3983a2755afSDarrick J. Wong 	complete(waiting);
3993a2755afSDarrick J. Wong }
400b9142174SJames Bottomley 
401b9142174SJames Bottomley static void sas_task_timedout(unsigned long _task)
402b9142174SJames Bottomley {
403b9142174SJames Bottomley 	struct sas_task *task = (void *) _task;
404b9142174SJames Bottomley 	unsigned long flags;
405b9142174SJames Bottomley 
406b9142174SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
407b9142174SJames Bottomley 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
408b9142174SJames Bottomley 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
409b9142174SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
410b9142174SJames Bottomley 
411b9142174SJames Bottomley 	complete(&task->completion);
412b9142174SJames Bottomley }
413b9142174SJames Bottomley 
414b9142174SJames Bottomley static void sas_disc_task_done(struct sas_task *task)
415b9142174SJames Bottomley {
416b9142174SJames Bottomley 	if (!del_timer(&task->timer))
417b9142174SJames Bottomley 		return;
418b9142174SJames Bottomley 	complete(&task->completion);
419b9142174SJames Bottomley }
420b9142174SJames Bottomley 
421b9142174SJames Bottomley #define SAS_DEV_TIMEOUT 10
422b9142174SJames Bottomley 
423b9142174SJames Bottomley /**
424b9142174SJames Bottomley  * sas_execute_task -- Basic task processing for discovery
425b9142174SJames Bottomley  * @task: the task to be executed
426b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
427b9142174SJames Bottomley  * @size: size of @buffer
4281d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
429b9142174SJames Bottomley  */
430b9142174SJames Bottomley static int sas_execute_task(struct sas_task *task, void *buffer, int size,
4311d1bbee6SJeff Garzik 			    enum dma_data_direction dma_dir)
432b9142174SJames Bottomley {
433b9142174SJames Bottomley 	int res = 0;
434b9142174SJames Bottomley 	struct scatterlist *scatter = NULL;
435b9142174SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
436b9142174SJames Bottomley 	int num_scatter = 0;
437b9142174SJames Bottomley 	int retries = 0;
438b9142174SJames Bottomley 	struct sas_internal *i =
439b9142174SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
440b9142174SJames Bottomley 
4411d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
442b9142174SJames Bottomley 		scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
443b9142174SJames Bottomley 		if (!scatter)
444b9142174SJames Bottomley 			goto out;
445b9142174SJames Bottomley 
446b9142174SJames Bottomley 		sg_init_one(scatter, buffer, size);
447b9142174SJames Bottomley 		num_scatter = 1;
448b9142174SJames Bottomley 	}
449b9142174SJames Bottomley 
450b9142174SJames Bottomley 	task->task_proto = task->dev->tproto;
451b9142174SJames Bottomley 	task->scatter = scatter;
452b9142174SJames Bottomley 	task->num_scatter = num_scatter;
453b9142174SJames Bottomley 	task->total_xfer_len = size;
4541d1bbee6SJeff Garzik 	task->data_dir = dma_dir;
455b9142174SJames Bottomley 	task->task_done = sas_disc_task_done;
4561d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE &&
457b9142174SJames Bottomley 	    sas_protocol_ata(task->task_proto)) {
4581d1bbee6SJeff Garzik 		task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
459b9142174SJames Bottomley 					       task->scatter,
460b9142174SJames Bottomley 					       task->num_scatter,
461b9142174SJames Bottomley 					       task->data_dir);
462b9142174SJames Bottomley 	}
463b9142174SJames Bottomley 
464b9142174SJames Bottomley 	for (retries = 0; retries < 5; retries++) {
465b9142174SJames Bottomley 		task->task_state_flags = SAS_TASK_STATE_PENDING;
466b9142174SJames Bottomley 		init_completion(&task->completion);
467b9142174SJames Bottomley 
468b9142174SJames Bottomley 		task->timer.data = (unsigned long) task;
469b9142174SJames Bottomley 		task->timer.function = sas_task_timedout;
470b9142174SJames Bottomley 		task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
471b9142174SJames Bottomley 		add_timer(&task->timer);
472b9142174SJames Bottomley 
473b9142174SJames Bottomley 		res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
474b9142174SJames Bottomley 		if (res) {
475b9142174SJames Bottomley 			del_timer(&task->timer);
476b9142174SJames Bottomley 			SAS_DPRINTK("executing SAS discovery task failed:%d\n",
477b9142174SJames Bottomley 				    res);
478b9142174SJames Bottomley 			goto ex_err;
479b9142174SJames Bottomley 		}
480b9142174SJames Bottomley 		wait_for_completion(&task->completion);
48132e8ae36SJames Bottomley 		res = -ECOMM;
482b9142174SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
483b9142174SJames Bottomley 			int res2;
484b9142174SJames Bottomley 			SAS_DPRINTK("task aborted, flags:0x%x\n",
485b9142174SJames Bottomley 				    task->task_state_flags);
486b9142174SJames Bottomley 			res2 = i->dft->lldd_abort_task(task);
487b9142174SJames Bottomley 			SAS_DPRINTK("came back from abort task\n");
488b9142174SJames Bottomley 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
489b9142174SJames Bottomley 				if (res2 == TMF_RESP_FUNC_COMPLETE)
490b9142174SJames Bottomley 					continue; /* Retry the task */
491b9142174SJames Bottomley 				else
492b9142174SJames Bottomley 					goto ex_err;
493b9142174SJames Bottomley 			}
494b9142174SJames Bottomley 		}
495df64d3caSJames Bottomley 		if (task->task_status.stat == SAM_STAT_BUSY ||
496df64d3caSJames Bottomley 			   task->task_status.stat == SAM_STAT_TASK_SET_FULL ||
497b9142174SJames Bottomley 			   task->task_status.stat == SAS_QUEUE_FULL) {
498b9142174SJames Bottomley 			SAS_DPRINTK("task: q busy, sleeping...\n");
499b9142174SJames Bottomley 			schedule_timeout_interruptible(HZ);
500df64d3caSJames Bottomley 		} else if (task->task_status.stat == SAM_STAT_CHECK_CONDITION) {
501b9142174SJames Bottomley 			struct scsi_sense_hdr shdr;
502b9142174SJames Bottomley 
503b9142174SJames Bottomley 			if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
504b9142174SJames Bottomley 						  &shdr)) {
505b9142174SJames Bottomley 				SAS_DPRINTK("couldn't normalize sense\n");
506b9142174SJames Bottomley 				continue;
507b9142174SJames Bottomley 			}
508b9142174SJames Bottomley 			if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
509b9142174SJames Bottomley 			    (shdr.sense_key == 2 && shdr.asc == 4 &&
510b9142174SJames Bottomley 			     shdr.ascq == 1)) {
511b9142174SJames Bottomley 				SAS_DPRINTK("device %016llx LUN: %016llx "
512b9142174SJames Bottomley 					    "powering up or not ready yet, "
513b9142174SJames Bottomley 					    "sleeping...\n",
514b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
515b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN));
516b9142174SJames Bottomley 
517b9142174SJames Bottomley 				schedule_timeout_interruptible(5*HZ);
518b9142174SJames Bottomley 			} else if (shdr.sense_key == 1) {
519b9142174SJames Bottomley 				res = 0;
520b9142174SJames Bottomley 				break;
521b9142174SJames Bottomley 			} else if (shdr.sense_key == 5) {
522b9142174SJames Bottomley 				break;
523b9142174SJames Bottomley 			} else {
524b9142174SJames Bottomley 				SAS_DPRINTK("dev %016llx LUN: %016llx "
525b9142174SJames Bottomley 					    "sense key:0x%x ASC:0x%x ASCQ:0x%x"
526b9142174SJames Bottomley 					    "\n",
527b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
528b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN),
529b9142174SJames Bottomley 					    shdr.sense_key,
530b9142174SJames Bottomley 					    shdr.asc, shdr.ascq);
531b9142174SJames Bottomley 			}
532b9142174SJames Bottomley 		} else if (task->task_status.resp != SAS_TASK_COMPLETE ||
533df64d3caSJames Bottomley 			   task->task_status.stat != SAM_STAT_GOOD) {
534b9142174SJames Bottomley 			SAS_DPRINTK("task finished with resp:0x%x, "
535b9142174SJames Bottomley 				    "stat:0x%x\n",
536b9142174SJames Bottomley 				    task->task_status.resp,
537b9142174SJames Bottomley 				    task->task_status.stat);
538b9142174SJames Bottomley 			goto ex_err;
539b9142174SJames Bottomley 		} else {
540b9142174SJames Bottomley 			res = 0;
541b9142174SJames Bottomley 			break;
542b9142174SJames Bottomley 		}
543b9142174SJames Bottomley 	}
544b9142174SJames Bottomley ex_err:
5451d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
546b9142174SJames Bottomley 		if (sas_protocol_ata(task->task_proto))
5471d1bbee6SJeff Garzik 			dma_unmap_sg(task->dev->port->ha->dev,
548b9142174SJames Bottomley 				     task->scatter, task->num_scatter,
549b9142174SJames Bottomley 				     task->data_dir);
550b9142174SJames Bottomley 		kfree(scatter);
551b9142174SJames Bottomley 	}
552b9142174SJames Bottomley out:
553b9142174SJames Bottomley 	return res;
554b9142174SJames Bottomley }
555b9142174SJames Bottomley 
556b9142174SJames Bottomley /* ---------- SATA ---------- */
557b9142174SJames Bottomley 
558b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
559b9142174SJames Bottomley {
560b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
561b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
562b9142174SJames Bottomley 
563b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
564b9142174SJames Bottomley 	     fis->lbal         == 1 &&
565b9142174SJames Bottomley 	     fis->lbam         == 0 &&
566b9142174SJames Bottomley 	     fis->lbah         == 0 &&
567b9142174SJames Bottomley 	     fis->device       == 0)
568b9142174SJames Bottomley 	    ||
569b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
570b9142174SJames Bottomley 	     fis->lbal         == 0 &&
571b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
572b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
573b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
574b9142174SJames Bottomley 
575b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
576b9142174SJames Bottomley 
577b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
578b9142174SJames Bottomley 		  fis->lbal             == 1 &&
579b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
580b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
581b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
582b9142174SJames Bottomley 
583b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
584b9142174SJames Bottomley 
585b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
586b9142174SJames Bottomley 		  fis->lbal         == 1 &&
587b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
588b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
589b9142174SJames Bottomley 		  fis->device       == 0)
590b9142174SJames Bottomley 		||
591b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
592b9142174SJames Bottomley 		  fis->lbal             == 1 &&
593b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
594b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
595b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
596b9142174SJames Bottomley 
597b9142174SJames Bottomley 		/* Treat it as a superset? */
598b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
599b9142174SJames Bottomley }
600b9142174SJames Bottomley 
601b9142174SJames Bottomley /**
602b9142174SJames Bottomley  * sas_issue_ata_cmd -- Basic SATA command processing for discovery
603b9142174SJames Bottomley  * @dev: the device to send the command to
604b9142174SJames Bottomley  * @command: the command register
605b9142174SJames Bottomley  * @features: the features register
606b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
607b9142174SJames Bottomley  * @size: size of @buffer
6081d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
609b9142174SJames Bottomley  */
610b9142174SJames Bottomley static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
611b9142174SJames Bottomley 			     u8 features, void *buffer, int size,
6121d1bbee6SJeff Garzik 			     enum dma_data_direction dma_dir)
613b9142174SJames Bottomley {
614b9142174SJames Bottomley 	int res = 0;
615b9142174SJames Bottomley 	struct sas_task *task;
616b9142174SJames Bottomley 	struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
617b9142174SJames Bottomley 		&dev->frame_rcvd[0];
618b9142174SJames Bottomley 
619b9142174SJames Bottomley 	res = -ENOMEM;
620b9142174SJames Bottomley 	task = sas_alloc_task(GFP_KERNEL);
621b9142174SJames Bottomley 	if (!task)
622b9142174SJames Bottomley 		goto out;
623b9142174SJames Bottomley 
624b9142174SJames Bottomley 	task->dev = dev;
625b9142174SJames Bottomley 
626b9142174SJames Bottomley 	task->ata_task.fis.fis_type = 0x27;
627b9142174SJames Bottomley 	task->ata_task.fis.command = command;
628b9142174SJames Bottomley 	task->ata_task.fis.features = features;
629b9142174SJames Bottomley 	task->ata_task.fis.device = d2h_fis->device;
630b9142174SJames Bottomley 	task->ata_task.retry_count = 1;
631b9142174SJames Bottomley 
6321d1bbee6SJeff Garzik 	res = sas_execute_task(task, buffer, size, dma_dir);
633b9142174SJames Bottomley 
634b9142174SJames Bottomley 	sas_free_task(task);
635b9142174SJames Bottomley out:
636b9142174SJames Bottomley 	return res;
637b9142174SJames Bottomley }
638b9142174SJames Bottomley 
639b9142174SJames Bottomley #define ATA_IDENTIFY_DEV         0xEC
640b9142174SJames Bottomley #define ATA_IDENTIFY_PACKET_DEV  0xA1
641b9142174SJames Bottomley #define ATA_SET_FEATURES         0xEF
642b9142174SJames Bottomley #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
643b9142174SJames Bottomley 
644b9142174SJames Bottomley /**
645b9142174SJames Bottomley  * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
646b9142174SJames Bottomley  * @dev: STP/SATA device of interest (ATA/ATAPI)
647b9142174SJames Bottomley  *
648b9142174SJames Bottomley  * The LLDD has already been notified of this device, so that we can
649b9142174SJames Bottomley  * send FISes to it.  Here we try to get IDENTIFY DEVICE or IDENTIFY
650b9142174SJames Bottomley  * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
651b9142174SJames Bottomley  * performance for this device.
652b9142174SJames Bottomley  */
653b9142174SJames Bottomley static int sas_discover_sata_dev(struct domain_device *dev)
654b9142174SJames Bottomley {
655b9142174SJames Bottomley 	int     res;
656b9142174SJames Bottomley 	__le16  *identify_x;
657b9142174SJames Bottomley 	u8      command;
658b9142174SJames Bottomley 
659b9142174SJames Bottomley 	identify_x = kzalloc(512, GFP_KERNEL);
660b9142174SJames Bottomley 	if (!identify_x)
661b9142174SJames Bottomley 		return -ENOMEM;
662b9142174SJames Bottomley 
663b9142174SJames Bottomley 	if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
664b9142174SJames Bottomley 		dev->sata_dev.identify_device = identify_x;
665b9142174SJames Bottomley 		command = ATA_IDENTIFY_DEV;
666b9142174SJames Bottomley 	} else {
667b9142174SJames Bottomley 		dev->sata_dev.identify_packet_device = identify_x;
668b9142174SJames Bottomley 		command = ATA_IDENTIFY_PACKET_DEV;
669b9142174SJames Bottomley 	}
670b9142174SJames Bottomley 
671b9142174SJames Bottomley 	res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
6721d1bbee6SJeff Garzik 				DMA_FROM_DEVICE);
673b9142174SJames Bottomley 	if (res)
674b9142174SJames Bottomley 		goto out_err;
675b9142174SJames Bottomley 
676b9142174SJames Bottomley 	/* lives on the media? */
677b9142174SJames Bottomley 	if (le16_to_cpu(identify_x[0]) & 4) {
678b9142174SJames Bottomley 		/* incomplete response */
679b9142174SJames Bottomley 		SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
680b9142174SJames Bottomley 			    "dev %llx\n", SAS_ADDR(dev->sas_addr));
68117b7a8deSAl Viro 		if (!(identify_x[83] & cpu_to_le16(1<<6)))
682b9142174SJames Bottomley 			goto cont1;
683b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
684b9142174SJames Bottomley 					ATA_FEATURE_PUP_STBY_SPIN_UP,
6851d1bbee6SJeff Garzik 					NULL, 0, DMA_NONE);
686b9142174SJames Bottomley 		if (res)
687b9142174SJames Bottomley 			goto cont1;
688b9142174SJames Bottomley 
689b9142174SJames Bottomley 		schedule_timeout_interruptible(5*HZ); /* More time? */
690b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
6911d1bbee6SJeff Garzik 					DMA_FROM_DEVICE);
692b9142174SJames Bottomley 		if (res)
693b9142174SJames Bottomley 			goto out_err;
694b9142174SJames Bottomley 	}
695b9142174SJames Bottomley cont1:
696b9142174SJames Bottomley 	/* XXX Hint: register this SATA device with SATL.
697b9142174SJames Bottomley 	   When this returns, dev->sata_dev->lu is alive and
698b9142174SJames Bottomley 	   present.
699b9142174SJames Bottomley 	sas_satl_register_dev(dev);
700b9142174SJames Bottomley 	*/
701b9142174SJames Bottomley 
702b9142174SJames Bottomley 	sas_fill_in_rphy(dev, dev->rphy);
703b9142174SJames Bottomley 
704b9142174SJames Bottomley 	return 0;
705b9142174SJames Bottomley out_err:
706b9142174SJames Bottomley 	dev->sata_dev.identify_packet_device = NULL;
707b9142174SJames Bottomley 	dev->sata_dev.identify_device = NULL;
708b9142174SJames Bottomley 	kfree(identify_x);
709b9142174SJames Bottomley 	return res;
710b9142174SJames Bottomley }
711b9142174SJames Bottomley 
712b9142174SJames Bottomley static int sas_discover_sata_pm(struct domain_device *dev)
713b9142174SJames Bottomley {
714b9142174SJames Bottomley 	return -ENODEV;
715b9142174SJames Bottomley }
716b9142174SJames Bottomley 
717b9142174SJames Bottomley /**
718b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
719b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
720b9142174SJames Bottomley  *
721b9142174SJames Bottomley  * First we notify the LLDD of this device, so we can send frames to
722b9142174SJames Bottomley  * it.  Then depending on the type of device we call the appropriate
723b9142174SJames Bottomley  * discover functions.  Once device discover is done, we notify the
724b9142174SJames Bottomley  * LLDD so that it can fine-tune its parameters for the device, by
725b9142174SJames Bottomley  * removing it and then adding it.  That is, the second time around,
726b9142174SJames Bottomley  * the driver would have certain fields, that it is looking at, set.
727b9142174SJames Bottomley  * Finally we initialize the kobj so that the device can be added to
728b9142174SJames Bottomley  * the system at registration time.  Devices directly attached to a HA
729b9142174SJames Bottomley  * port, have no parents.  All other devices do, and should have their
730b9142174SJames Bottomley  * "parent" pointer set appropriately before calling this function.
731b9142174SJames Bottomley  */
732b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
733b9142174SJames Bottomley {
734b9142174SJames Bottomley 	int res;
735b9142174SJames Bottomley 
736b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
737b9142174SJames Bottomley 
738b9142174SJames Bottomley 	res = sas_notify_lldd_dev_found(dev);
739b9142174SJames Bottomley 	if (res)
740b9142174SJames Bottomley 		return res;
741b9142174SJames Bottomley 
742b9142174SJames Bottomley 	switch (dev->dev_type) {
743b9142174SJames Bottomley 	case SATA_DEV:
744b9142174SJames Bottomley 		res = sas_discover_sata_dev(dev);
745b9142174SJames Bottomley 		break;
746b9142174SJames Bottomley 	case SATA_PM:
747b9142174SJames Bottomley 		res = sas_discover_sata_pm(dev);
748b9142174SJames Bottomley 		break;
749b9142174SJames Bottomley 	default:
750b9142174SJames Bottomley 		break;
751b9142174SJames Bottomley 	}
752b9142174SJames Bottomley 	sas_notify_lldd_dev_gone(dev);
753b9142174SJames Bottomley 	if (!res) {
754b9142174SJames Bottomley 		sas_notify_lldd_dev_found(dev);
755b9142174SJames Bottomley 		res = sas_rphy_add(dev->rphy);
756b9142174SJames Bottomley 	}
757b9142174SJames Bottomley 
758b9142174SJames Bottomley 	return res;
759b9142174SJames Bottomley }
76000dd4998SJames Bottomley 
76100dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
76200dd4998SJames Bottomley {
76300dd4998SJames Bottomley 	struct scsi_device *sdev;
76400dd4998SJames Bottomley 
76500dd4998SJames Bottomley 	shost_for_each_device(sdev, shost) {
76600dd4998SJames Bottomley 		struct domain_device *ddev = sdev_to_domain_dev(sdev);
76700dd4998SJames Bottomley 		struct ata_port *ap = ddev->sata_dev.ap;
76800dd4998SJames Bottomley 
76900dd4998SJames Bottomley 		if (!dev_is_sata(ddev))
77000dd4998SJames Bottomley 			continue;
77100dd4998SJames Bottomley 
77200dd4998SJames Bottomley 		ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
77300dd4998SJames Bottomley 		ata_scsi_port_error_handler(shost, ap);
77400dd4998SJames Bottomley 	}
77500dd4998SJames Bottomley }
77600dd4998SJames Bottomley 
77700dd4998SJames Bottomley int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,
77800dd4998SJames Bottomley 		      enum blk_eh_timer_return *rtn)
77900dd4998SJames Bottomley {
78000dd4998SJames Bottomley 	struct domain_device *ddev = cmd_to_domain_dev(cmd);
78100dd4998SJames Bottomley 
78200dd4998SJames Bottomley 	if (!dev_is_sata(ddev) || task)
78300dd4998SJames Bottomley 		return 0;
78400dd4998SJames Bottomley 
78500dd4998SJames Bottomley 	/* we're a sata device with no task, so this must be a libata
78600dd4998SJames Bottomley 	 * eh timeout.  Ideally should hook into libata timeout
78700dd4998SJames Bottomley 	 * handling, but there's no point, it just wants to activate
78800dd4998SJames Bottomley 	 * the eh thread */
78900dd4998SJames Bottomley 	*rtn = BLK_EH_NOT_HANDLED;
79000dd4998SJames Bottomley 	return 1;
79100dd4998SJames Bottomley }
79200dd4998SJames Bottomley 
79300dd4998SJames Bottomley int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
79400dd4998SJames Bottomley 	       struct list_head *done_q)
79500dd4998SJames Bottomley {
79600dd4998SJames Bottomley 	int rtn = 0;
79700dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
79800dd4998SJames Bottomley 	struct ata_port *ap;
79900dd4998SJames Bottomley 
80000dd4998SJames Bottomley 	do {
80100dd4998SJames Bottomley 		LIST_HEAD(sata_q);
80200dd4998SJames Bottomley 
80300dd4998SJames Bottomley 		ap = NULL;
80400dd4998SJames Bottomley 
80500dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
80600dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
80700dd4998SJames Bottomley 
80800dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
80900dd4998SJames Bottomley 				continue;
81000dd4998SJames Bottomley 			if (ap && ap != ddev->sata_dev.ap)
81100dd4998SJames Bottomley 				continue;
81200dd4998SJames Bottomley 			ap = ddev->sata_dev.ap;
81300dd4998SJames Bottomley 			rtn = 1;
81400dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
81500dd4998SJames Bottomley 		}
81600dd4998SJames Bottomley 
81700dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
81800dd4998SJames Bottomley 			ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
81900dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
820a82058a7SJames Bottomley 			/*
821a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
822a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
823a82058a7SJames Bottomley 			 * about to go out of scope.
824a82058a7SJames Bottomley 			 *
825a82058a7SJames Bottomley 			 * This looks strange, since the commands are
826a82058a7SJames Bottomley 			 * now part of no list, but the next error
827a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
828a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
829a82058a7SJames Bottomley 			 * anyway from the ata tag array.
830a82058a7SJames Bottomley 			 */
831a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
832a82058a7SJames Bottomley 				list_del_init(sata_q.next);
83300dd4998SJames Bottomley 		}
83400dd4998SJames Bottomley 	} while (ap);
83500dd4998SJames Bottomley 
83600dd4998SJames Bottomley 	return rtn;
83700dd4998SJames Bottomley }
838