xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision ff2aeb1eb64c8a4770a6304f9addbae9f9828646)
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>
25b9142174SJames Bottomley 
26338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
27338ec570SDarrick J. Wong #include "sas_internal.h"
28338ec570SDarrick J. Wong #include <scsi/scsi_host.h>
29338ec570SDarrick J. Wong #include <scsi/scsi_device.h>
30338ec570SDarrick J. Wong #include <scsi/scsi_tcq.h>
31338ec570SDarrick J. Wong #include <scsi/scsi.h>
32338ec570SDarrick J. Wong #include <scsi/scsi_transport.h>
33338ec570SDarrick J. Wong #include <scsi/scsi_transport_sas.h>
34338ec570SDarrick J. Wong #include "../scsi_sas_internal.h"
353a2755afSDarrick J. Wong #include "../scsi_transport_api.h"
363a2755afSDarrick J. Wong #include <scsi/scsi_eh.h>
37338ec570SDarrick J. Wong 
38338ec570SDarrick J. Wong static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
39338ec570SDarrick J. Wong {
40338ec570SDarrick J. Wong 	/* Cheesy attempt to translate SAS errors into ATA.  Hah! */
41338ec570SDarrick J. Wong 
42338ec570SDarrick J. Wong 	/* transport error */
43338ec570SDarrick J. Wong 	if (ts->resp == SAS_TASK_UNDELIVERED)
44338ec570SDarrick J. Wong 		return AC_ERR_ATA_BUS;
45338ec570SDarrick J. Wong 
46338ec570SDarrick J. Wong 	/* ts->resp == SAS_TASK_COMPLETE */
47338ec570SDarrick J. Wong 	/* task delivered, what happened afterwards? */
48338ec570SDarrick J. Wong 	switch (ts->stat) {
49338ec570SDarrick J. Wong 		case SAS_DEV_NO_RESPONSE:
50338ec570SDarrick J. Wong 			return AC_ERR_TIMEOUT;
51338ec570SDarrick J. Wong 
52338ec570SDarrick J. Wong 		case SAS_INTERRUPTED:
53338ec570SDarrick J. Wong 		case SAS_PHY_DOWN:
54338ec570SDarrick J. Wong 		case SAS_NAK_R_ERR:
55338ec570SDarrick J. Wong 			return AC_ERR_ATA_BUS;
56338ec570SDarrick J. Wong 
57338ec570SDarrick J. Wong 
58338ec570SDarrick J. Wong 		case SAS_DATA_UNDERRUN:
59338ec570SDarrick J. Wong 			/*
60338ec570SDarrick J. Wong 			 * Some programs that use the taskfile interface
61338ec570SDarrick J. Wong 			 * (smartctl in particular) can cause underrun
62338ec570SDarrick J. Wong 			 * problems.  Ignore these errors, perhaps at our
63338ec570SDarrick J. Wong 			 * peril.
64338ec570SDarrick J. Wong 			 */
65338ec570SDarrick J. Wong 			return 0;
66338ec570SDarrick J. Wong 
67338ec570SDarrick J. Wong 		case SAS_DATA_OVERRUN:
68338ec570SDarrick J. Wong 		case SAS_QUEUE_FULL:
69338ec570SDarrick J. Wong 		case SAS_DEVICE_UNKNOWN:
70338ec570SDarrick J. Wong 		case SAS_SG_ERR:
71338ec570SDarrick J. Wong 			return AC_ERR_INVALID;
72338ec570SDarrick J. Wong 
73338ec570SDarrick J. Wong 		case SAM_CHECK_COND:
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",
77338ec570SDarrick J. Wong 				    __FUNCTION__, ts->stat);
78338ec570SDarrick J. Wong 			return AC_ERR_OTHER;
79338ec570SDarrick J. Wong 
80338ec570SDarrick J. Wong 		case SAS_ABORTED_TASK:
81338ec570SDarrick J. Wong 			return AC_ERR_DEV;
82338ec570SDarrick J. Wong 
83338ec570SDarrick J. Wong 		case SAS_PROTO_RESPONSE:
84338ec570SDarrick J. Wong 			/* This means the ending_fis has the error
85338ec570SDarrick J. Wong 			 * value; return 0 here to collect it */
86338ec570SDarrick J. Wong 			return 0;
87338ec570SDarrick J. Wong 		default:
88338ec570SDarrick J. Wong 			return 0;
89338ec570SDarrick J. Wong 	}
90338ec570SDarrick J. Wong }
91338ec570SDarrick J. Wong 
92338ec570SDarrick J. Wong static void sas_ata_task_done(struct sas_task *task)
93338ec570SDarrick J. Wong {
94338ec570SDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
951c50dc83SDarrick J. Wong 	struct domain_device *dev;
96338ec570SDarrick J. Wong 	struct task_status_struct *stat = &task->task_status;
97338ec570SDarrick J. Wong 	struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
983a2755afSDarrick J. Wong 	struct sas_ha_struct *sas_ha;
99338ec570SDarrick J. Wong 	enum ata_completion_errors ac;
1003eb7a51aSDarrick J. Wong 	unsigned long flags;
101338ec570SDarrick J. Wong 
1021c50dc83SDarrick J. Wong 	if (!qc)
1031c50dc83SDarrick J. Wong 		goto qc_already_gone;
1041c50dc83SDarrick J. Wong 
1051c50dc83SDarrick J. Wong 	dev = qc->ap->private_data;
1063a2755afSDarrick J. Wong 	sas_ha = dev->port->ha;
1071c50dc83SDarrick J. Wong 
1083eb7a51aSDarrick J. Wong 	spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
109d97db63fSDarrick J. Wong 	if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_GOOD) {
110338ec570SDarrick J. Wong 		ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
111338ec570SDarrick J. Wong 		qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
112338ec570SDarrick J. Wong 		dev->sata_dev.sstatus = resp->sstatus;
113338ec570SDarrick J. Wong 		dev->sata_dev.serror = resp->serror;
114338ec570SDarrick J. Wong 		dev->sata_dev.scontrol = resp->scontrol;
115338ec570SDarrick J. Wong 	} else if (stat->stat != SAM_STAT_GOOD) {
116338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
117338ec570SDarrick J. Wong 		if (ac) {
118338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__,
119338ec570SDarrick J. Wong 				    stat->stat);
120338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
121338ec570SDarrick J. Wong 			qc->err_mask = ac;
122338ec570SDarrick J. Wong 			dev->sata_dev.tf.feature = 0x04; /* status err */
123338ec570SDarrick J. Wong 			dev->sata_dev.tf.command = ATA_ERR;
124338ec570SDarrick J. Wong 		}
125338ec570SDarrick J. Wong 	}
126338ec570SDarrick J. Wong 
1271c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
128fe059f12SDarrick J. Wong 	if (qc->scsicmd)
129fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
130338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1313eb7a51aSDarrick J. Wong 	spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
1323eb7a51aSDarrick J. Wong 
1333a2755afSDarrick J. Wong 	/*
1343a2755afSDarrick J. Wong 	 * If the sas_task has an ata qc, a scsi_cmnd and the aborted
1353a2755afSDarrick J. Wong 	 * flag is set, then we must have come in via the libsas EH
1363a2755afSDarrick J. Wong 	 * functions.  When we exit this function, we need to put the
1373a2755afSDarrick J. Wong 	 * scsi_cmnd on the list of finished errors.  The ata_qc_complete
1383a2755afSDarrick J. Wong 	 * call cleans up the libata side of things but we're protected
1393a2755afSDarrick J. Wong 	 * from the scsi_cmnd going away because the scsi_cmnd is owned
1403a2755afSDarrick J. Wong 	 * by the EH, making libata's call to scsi_done a NOP.
1413a2755afSDarrick J. Wong 	 */
1423a2755afSDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
1433a2755afSDarrick J. Wong 	if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
1443a2755afSDarrick J. Wong 		scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
1453a2755afSDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1463a2755afSDarrick J. Wong 
1471c50dc83SDarrick J. Wong qc_already_gone:
148338ec570SDarrick J. Wong 	list_del_init(&task->list);
149338ec570SDarrick J. Wong 	sas_free_task(task);
150338ec570SDarrick J. Wong }
151338ec570SDarrick J. Wong 
152338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
153338ec570SDarrick J. Wong {
15435a7f2f6SDarrick J. Wong 	int res;
155338ec570SDarrick J. Wong 	struct sas_task *task;
156338ec570SDarrick J. Wong 	struct domain_device *dev = qc->ap->private_data;
157338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
158338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
159338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
160338ec570SDarrick J. Wong 	struct scatterlist *sg;
161338ec570SDarrick J. Wong 	unsigned int xfer = 0;
162*ff2aeb1eSTejun Heo 	unsigned int si;
163338ec570SDarrick J. Wong 
164338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
165338ec570SDarrick J. Wong 	if (!task)
16635a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
167338ec570SDarrick J. Wong 	task->dev = dev;
168338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
169338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
170338ec570SDarrick J. Wong 
171338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
172338ec570SDarrick J. Wong 	    qc->tf.command == ATA_CMD_FPDMA_READ) {
173338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
174338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
175338ec570SDarrick J. Wong 	}
176338ec570SDarrick J. Wong 
177110dd8f1SJames Bottomley 	ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
178338ec570SDarrick J. Wong 	task->uldd_task = qc;
179405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
180338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
181338ec570SDarrick J. Wong 		task->total_xfer_len = qc->nbytes + qc->pad_len;
182338ec570SDarrick J. Wong 		task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
183338ec570SDarrick J. Wong 	} else {
184*ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
185338ec570SDarrick J. Wong 			xfer += sg->length;
186338ec570SDarrick J. Wong 
187338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
188*ff2aeb1eSTejun Heo 		task->num_scatter = si;
189338ec570SDarrick J. Wong 	}
190338ec570SDarrick J. Wong 
191338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
192*ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
193338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
194338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
1951c50dc83SDarrick J. Wong 	qc->lldd_task = task;
196338ec570SDarrick J. Wong 
197338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
198338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
199338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
200338ec570SDarrick J. Wong 		/* fall through */
2010dc36888STejun Heo 	case ATAPI_PROT_DMA:
202338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
203338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
204338ec570SDarrick J. Wong 		break;
205338ec570SDarrick J. Wong 	}
206338ec570SDarrick J. Wong 
207fe059f12SDarrick J. Wong 	if (qc->scsicmd)
208fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
209fe059f12SDarrick J. Wong 
210338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
211338ec570SDarrick J. Wong 		res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
212338ec570SDarrick J. Wong 	else
213338ec570SDarrick J. Wong 		res = sas_queue_up(task);
214338ec570SDarrick J. Wong 
215338ec570SDarrick J. Wong 	/* Examine */
216338ec570SDarrick J. Wong 	if (res) {
217338ec570SDarrick J. Wong 		SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
218338ec570SDarrick J. Wong 
219fe059f12SDarrick J. Wong 		if (qc->scsicmd)
220fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
221338ec570SDarrick J. Wong 		sas_free_task(task);
22235a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
223338ec570SDarrick J. Wong 	}
224338ec570SDarrick J. Wong 
22535a7f2f6SDarrick J. Wong 	return 0;
226338ec570SDarrick J. Wong }
227338ec570SDarrick J. Wong 
228338ec570SDarrick J. Wong static u8 sas_ata_check_status(struct ata_port *ap)
229338ec570SDarrick J. Wong {
230338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
231338ec570SDarrick J. Wong 	return dev->sata_dev.tf.command;
232338ec570SDarrick J. Wong }
233338ec570SDarrick J. Wong 
234338ec570SDarrick J. Wong static void sas_ata_phy_reset(struct ata_port *ap)
235338ec570SDarrick J. Wong {
236338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
237338ec570SDarrick J. Wong 	struct sas_internal *i =
238338ec570SDarrick J. Wong 		to_sas_internal(dev->port->ha->core.shost->transportt);
239338ec570SDarrick J. Wong 	int res = 0;
240338ec570SDarrick J. Wong 
241338ec570SDarrick J. Wong 	if (i->dft->lldd_I_T_nexus_reset)
242338ec570SDarrick J. Wong 		res = i->dft->lldd_I_T_nexus_reset(dev);
243338ec570SDarrick J. Wong 
244338ec570SDarrick J. Wong 	if (res)
245338ec570SDarrick J. Wong 		SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
246338ec570SDarrick J. Wong 
247338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
248338ec570SDarrick J. Wong 		case ATA_COMMAND_SET:
249338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
2509af5c9c9STejun Heo 			ap->link.device[0].class = ATA_DEV_ATA;
251338ec570SDarrick J. Wong 			break;
252338ec570SDarrick J. Wong 		case ATAPI_COMMAND_SET:
253338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
2549af5c9c9STejun Heo 			ap->link.device[0].class = ATA_DEV_ATAPI;
255338ec570SDarrick J. Wong 			break;
256338ec570SDarrick J. Wong 		default:
257338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
258338ec570SDarrick J. Wong 				    __FUNCTION__,
259338ec570SDarrick J. Wong 				    dev->sata_dev.command_set);
2609af5c9c9STejun Heo 			ap->link.device[0].class = ATA_DEV_UNKNOWN;
261338ec570SDarrick J. Wong 			break;
262338ec570SDarrick J. Wong 	}
263338ec570SDarrick J. Wong 
264338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
265338ec570SDarrick J. Wong }
266338ec570SDarrick J. Wong 
267338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
268338ec570SDarrick J. Wong {
269338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
270338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
271338ec570SDarrick J. Wong 
2721c50dc83SDarrick J. Wong 	if (qc->err_mask) {
2731c50dc83SDarrick J. Wong 		/*
2741c50dc83SDarrick J. Wong 		 * Find the sas_task and kill it.  By this point,
2751c50dc83SDarrick J. Wong 		 * libata has decided to kill the qc, so we needn't
2761c50dc83SDarrick J. Wong 		 * bother with sas_ata_task_done.  But we still
2771c50dc83SDarrick J. Wong 		 * ought to abort the task.
2781c50dc83SDarrick J. Wong 		 */
2791c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
2803a2755afSDarrick J. Wong 		unsigned long flags;
2811c50dc83SDarrick J. Wong 
2821c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
2831c50dc83SDarrick J. Wong 		if (task) {
2843a2755afSDarrick J. Wong 			/* Should this be a AT(API) device reset? */
2853a2755afSDarrick J. Wong 			spin_lock_irqsave(&task->task_state_lock, flags);
2863a2755afSDarrick J. Wong 			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2873a2755afSDarrick J. Wong 			spin_unlock_irqrestore(&task->task_state_lock, flags);
2883a2755afSDarrick J. Wong 
2891c50dc83SDarrick J. Wong 			task->uldd_task = NULL;
2901c50dc83SDarrick J. Wong 			__sas_task_abort(task);
2911c50dc83SDarrick J. Wong 		}
2921c50dc83SDarrick J. Wong 	}
293338ec570SDarrick J. Wong }
294338ec570SDarrick J. Wong 
295338ec570SDarrick J. Wong static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
296338ec570SDarrick J. Wong {
297338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
298338ec570SDarrick J. Wong 	memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
299338ec570SDarrick J. Wong }
300338ec570SDarrick J. Wong 
301110dd8f1SJames Bottomley static int sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
302338ec570SDarrick J. Wong 			      u32 val)
303338ec570SDarrick J. Wong {
304338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
305338ec570SDarrick J. Wong 
306338ec570SDarrick J. Wong 	SAS_DPRINTK("STUB %s\n", __FUNCTION__);
307338ec570SDarrick J. Wong 	switch (sc_reg_in) {
308338ec570SDarrick J. Wong 		case SCR_STATUS:
309338ec570SDarrick J. Wong 			dev->sata_dev.sstatus = val;
310338ec570SDarrick J. Wong 			break;
311338ec570SDarrick J. Wong 		case SCR_CONTROL:
312338ec570SDarrick J. Wong 			dev->sata_dev.scontrol = val;
313338ec570SDarrick J. Wong 			break;
314338ec570SDarrick J. Wong 		case SCR_ERROR:
315338ec570SDarrick J. Wong 			dev->sata_dev.serror = val;
316338ec570SDarrick J. Wong 			break;
317338ec570SDarrick J. Wong 		case SCR_ACTIVE:
3189af5c9c9STejun Heo 			dev->sata_dev.ap->link.sactive = val;
319338ec570SDarrick J. Wong 			break;
320110dd8f1SJames Bottomley 		default:
321110dd8f1SJames Bottomley 			return -EINVAL;
322338ec570SDarrick J. Wong 	}
323110dd8f1SJames Bottomley 	return 0;
324338ec570SDarrick J. Wong }
325338ec570SDarrick J. Wong 
326110dd8f1SJames Bottomley static int sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
327110dd8f1SJames Bottomley 			    u32 *val)
328338ec570SDarrick J. Wong {
329338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
330338ec570SDarrick J. Wong 
331338ec570SDarrick J. Wong 	SAS_DPRINTK("STUB %s\n", __FUNCTION__);
332338ec570SDarrick J. Wong 	switch (sc_reg_in) {
333338ec570SDarrick J. Wong 		case SCR_STATUS:
334110dd8f1SJames Bottomley 			*val = dev->sata_dev.sstatus;
335110dd8f1SJames Bottomley 			return 0;
336338ec570SDarrick J. Wong 		case SCR_CONTROL:
337110dd8f1SJames Bottomley 			*val = dev->sata_dev.scontrol;
338110dd8f1SJames Bottomley 			return 0;
339338ec570SDarrick J. Wong 		case SCR_ERROR:
340110dd8f1SJames Bottomley 			*val = dev->sata_dev.serror;
341110dd8f1SJames Bottomley 			return 0;
342338ec570SDarrick J. Wong 		case SCR_ACTIVE:
3439af5c9c9STejun Heo 			*val = dev->sata_dev.ap->link.sactive;
344110dd8f1SJames Bottomley 			return 0;
345338ec570SDarrick J. Wong 		default:
346110dd8f1SJames Bottomley 			return -EINVAL;
347338ec570SDarrick J. Wong 	}
348338ec570SDarrick J. Wong }
349338ec570SDarrick J. Wong 
350338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
351338ec570SDarrick J. Wong 	.check_status		= sas_ata_check_status,
352338ec570SDarrick J. Wong 	.check_altstatus	= sas_ata_check_status,
353338ec570SDarrick J. Wong 	.dev_select		= ata_noop_dev_select,
354338ec570SDarrick J. Wong 	.phy_reset		= sas_ata_phy_reset,
355338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
356338ec570SDarrick J. Wong 	.tf_read		= sas_ata_tf_read,
357338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
358338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
359338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
360338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
361338ec570SDarrick J. Wong 	.scr_read		= sas_ata_scr_read,
362338ec570SDarrick J. Wong 	.scr_write		= sas_ata_scr_write
363338ec570SDarrick J. Wong };
364338ec570SDarrick J. Wong 
365338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
366338ec570SDarrick J. Wong 	.flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
367338ec570SDarrick J. Wong 		ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
368338ec570SDarrick J. Wong 	.pio_mask = 0x1f, /* PIO0-4 */
369338ec570SDarrick J. Wong 	.mwdma_mask = 0x07, /* MWDMA0-2 */
370338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
371338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
372338ec570SDarrick J. Wong };
373338ec570SDarrick J. Wong 
374338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
375338ec570SDarrick J. Wong 			       struct scsi_target *starget)
376338ec570SDarrick J. Wong {
377338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
378338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
379338ec570SDarrick J. Wong 	struct ata_port *ap;
380338ec570SDarrick J. Wong 
381338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
3821d1bbee6SJeff Garzik 		      ha->dev,
383338ec570SDarrick J. Wong 		      sata_port_info.flags,
384338ec570SDarrick J. Wong 		      &sas_sata_ops);
385338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
386338ec570SDarrick J. Wong 				&sata_port_info,
387338ec570SDarrick J. Wong 				shost);
388338ec570SDarrick J. Wong 	if (!ap) {
389338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
390338ec570SDarrick J. Wong 		return -ENODEV;
391338ec570SDarrick J. Wong 	}
392338ec570SDarrick J. Wong 
393338ec570SDarrick J. Wong 	ap->private_data = found_dev;
394338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
395338ec570SDarrick J. Wong 	ap->scsi_host = shost;
396338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
397338ec570SDarrick J. Wong 
398338ec570SDarrick J. Wong 	return 0;
399338ec570SDarrick J. Wong }
4003a2755afSDarrick J. Wong 
4013a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
4023a2755afSDarrick J. Wong {
4033a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
4043a2755afSDarrick J. Wong 	struct completion *waiting;
4053a2755afSDarrick J. Wong 
4063a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
4073a2755afSDarrick J. Wong 	if (qc->scsicmd) {
4083a2755afSDarrick J. Wong 		scsi_req_abort_cmd(qc->scsicmd);
4093a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
4103a2755afSDarrick J. Wong 		return;
4113a2755afSDarrick J. Wong 	}
4123a2755afSDarrick J. Wong 
4133a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
4143a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4153a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
4163a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
4173a2755afSDarrick J. Wong 	waiting = qc->private_data;
4183a2755afSDarrick J. Wong 	complete(waiting);
4193a2755afSDarrick J. Wong }
420b9142174SJames Bottomley 
421b9142174SJames Bottomley static void sas_task_timedout(unsigned long _task)
422b9142174SJames Bottomley {
423b9142174SJames Bottomley 	struct sas_task *task = (void *) _task;
424b9142174SJames Bottomley 	unsigned long flags;
425b9142174SJames Bottomley 
426b9142174SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
427b9142174SJames Bottomley 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
428b9142174SJames Bottomley 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
429b9142174SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
430b9142174SJames Bottomley 
431b9142174SJames Bottomley 	complete(&task->completion);
432b9142174SJames Bottomley }
433b9142174SJames Bottomley 
434b9142174SJames Bottomley static void sas_disc_task_done(struct sas_task *task)
435b9142174SJames Bottomley {
436b9142174SJames Bottomley 	if (!del_timer(&task->timer))
437b9142174SJames Bottomley 		return;
438b9142174SJames Bottomley 	complete(&task->completion);
439b9142174SJames Bottomley }
440b9142174SJames Bottomley 
441b9142174SJames Bottomley #define SAS_DEV_TIMEOUT 10
442b9142174SJames Bottomley 
443b9142174SJames Bottomley /**
444b9142174SJames Bottomley  * sas_execute_task -- Basic task processing for discovery
445b9142174SJames Bottomley  * @task: the task to be executed
446b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
447b9142174SJames Bottomley  * @size: size of @buffer
4481d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
449b9142174SJames Bottomley  */
450b9142174SJames Bottomley static int sas_execute_task(struct sas_task *task, void *buffer, int size,
4511d1bbee6SJeff Garzik 			    enum dma_data_direction dma_dir)
452b9142174SJames Bottomley {
453b9142174SJames Bottomley 	int res = 0;
454b9142174SJames Bottomley 	struct scatterlist *scatter = NULL;
455b9142174SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
456b9142174SJames Bottomley 	int num_scatter = 0;
457b9142174SJames Bottomley 	int retries = 0;
458b9142174SJames Bottomley 	struct sas_internal *i =
459b9142174SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
460b9142174SJames Bottomley 
4611d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
462b9142174SJames Bottomley 		scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
463b9142174SJames Bottomley 		if (!scatter)
464b9142174SJames Bottomley 			goto out;
465b9142174SJames Bottomley 
466b9142174SJames Bottomley 		sg_init_one(scatter, buffer, size);
467b9142174SJames Bottomley 		num_scatter = 1;
468b9142174SJames Bottomley 	}
469b9142174SJames Bottomley 
470b9142174SJames Bottomley 	task->task_proto = task->dev->tproto;
471b9142174SJames Bottomley 	task->scatter = scatter;
472b9142174SJames Bottomley 	task->num_scatter = num_scatter;
473b9142174SJames Bottomley 	task->total_xfer_len = size;
4741d1bbee6SJeff Garzik 	task->data_dir = dma_dir;
475b9142174SJames Bottomley 	task->task_done = sas_disc_task_done;
4761d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE &&
477b9142174SJames Bottomley 	    sas_protocol_ata(task->task_proto)) {
4781d1bbee6SJeff Garzik 		task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
479b9142174SJames Bottomley 					       task->scatter,
480b9142174SJames Bottomley 					       task->num_scatter,
481b9142174SJames Bottomley 					       task->data_dir);
482b9142174SJames Bottomley 	}
483b9142174SJames Bottomley 
484b9142174SJames Bottomley 	for (retries = 0; retries < 5; retries++) {
485b9142174SJames Bottomley 		task->task_state_flags = SAS_TASK_STATE_PENDING;
486b9142174SJames Bottomley 		init_completion(&task->completion);
487b9142174SJames Bottomley 
488b9142174SJames Bottomley 		task->timer.data = (unsigned long) task;
489b9142174SJames Bottomley 		task->timer.function = sas_task_timedout;
490b9142174SJames Bottomley 		task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
491b9142174SJames Bottomley 		add_timer(&task->timer);
492b9142174SJames Bottomley 
493b9142174SJames Bottomley 		res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
494b9142174SJames Bottomley 		if (res) {
495b9142174SJames Bottomley 			del_timer(&task->timer);
496b9142174SJames Bottomley 			SAS_DPRINTK("executing SAS discovery task failed:%d\n",
497b9142174SJames Bottomley 				    res);
498b9142174SJames Bottomley 			goto ex_err;
499b9142174SJames Bottomley 		}
500b9142174SJames Bottomley 		wait_for_completion(&task->completion);
501b9142174SJames Bottomley 		res = -ETASK;
502b9142174SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
503b9142174SJames Bottomley 			int res2;
504b9142174SJames Bottomley 			SAS_DPRINTK("task aborted, flags:0x%x\n",
505b9142174SJames Bottomley 				    task->task_state_flags);
506b9142174SJames Bottomley 			res2 = i->dft->lldd_abort_task(task);
507b9142174SJames Bottomley 			SAS_DPRINTK("came back from abort task\n");
508b9142174SJames Bottomley 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
509b9142174SJames Bottomley 				if (res2 == TMF_RESP_FUNC_COMPLETE)
510b9142174SJames Bottomley 					continue; /* Retry the task */
511b9142174SJames Bottomley 				else
512b9142174SJames Bottomley 					goto ex_err;
513b9142174SJames Bottomley 			}
514b9142174SJames Bottomley 		}
515b9142174SJames Bottomley 		if (task->task_status.stat == SAM_BUSY ||
516b9142174SJames Bottomley 			   task->task_status.stat == SAM_TASK_SET_FULL ||
517b9142174SJames Bottomley 			   task->task_status.stat == SAS_QUEUE_FULL) {
518b9142174SJames Bottomley 			SAS_DPRINTK("task: q busy, sleeping...\n");
519b9142174SJames Bottomley 			schedule_timeout_interruptible(HZ);
520b9142174SJames Bottomley 		} else if (task->task_status.stat == SAM_CHECK_COND) {
521b9142174SJames Bottomley 			struct scsi_sense_hdr shdr;
522b9142174SJames Bottomley 
523b9142174SJames Bottomley 			if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
524b9142174SJames Bottomley 						  &shdr)) {
525b9142174SJames Bottomley 				SAS_DPRINTK("couldn't normalize sense\n");
526b9142174SJames Bottomley 				continue;
527b9142174SJames Bottomley 			}
528b9142174SJames Bottomley 			if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
529b9142174SJames Bottomley 			    (shdr.sense_key == 2 && shdr.asc == 4 &&
530b9142174SJames Bottomley 			     shdr.ascq == 1)) {
531b9142174SJames Bottomley 				SAS_DPRINTK("device %016llx LUN: %016llx "
532b9142174SJames Bottomley 					    "powering up or not ready yet, "
533b9142174SJames Bottomley 					    "sleeping...\n",
534b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
535b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN));
536b9142174SJames Bottomley 
537b9142174SJames Bottomley 				schedule_timeout_interruptible(5*HZ);
538b9142174SJames Bottomley 			} else if (shdr.sense_key == 1) {
539b9142174SJames Bottomley 				res = 0;
540b9142174SJames Bottomley 				break;
541b9142174SJames Bottomley 			} else if (shdr.sense_key == 5) {
542b9142174SJames Bottomley 				break;
543b9142174SJames Bottomley 			} else {
544b9142174SJames Bottomley 				SAS_DPRINTK("dev %016llx LUN: %016llx "
545b9142174SJames Bottomley 					    "sense key:0x%x ASC:0x%x ASCQ:0x%x"
546b9142174SJames Bottomley 					    "\n",
547b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
548b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN),
549b9142174SJames Bottomley 					    shdr.sense_key,
550b9142174SJames Bottomley 					    shdr.asc, shdr.ascq);
551b9142174SJames Bottomley 			}
552b9142174SJames Bottomley 		} else if (task->task_status.resp != SAS_TASK_COMPLETE ||
553b9142174SJames Bottomley 			   task->task_status.stat != SAM_GOOD) {
554b9142174SJames Bottomley 			SAS_DPRINTK("task finished with resp:0x%x, "
555b9142174SJames Bottomley 				    "stat:0x%x\n",
556b9142174SJames Bottomley 				    task->task_status.resp,
557b9142174SJames Bottomley 				    task->task_status.stat);
558b9142174SJames Bottomley 			goto ex_err;
559b9142174SJames Bottomley 		} else {
560b9142174SJames Bottomley 			res = 0;
561b9142174SJames Bottomley 			break;
562b9142174SJames Bottomley 		}
563b9142174SJames Bottomley 	}
564b9142174SJames Bottomley ex_err:
5651d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
566b9142174SJames Bottomley 		if (sas_protocol_ata(task->task_proto))
5671d1bbee6SJeff Garzik 			dma_unmap_sg(task->dev->port->ha->dev,
568b9142174SJames Bottomley 				     task->scatter, task->num_scatter,
569b9142174SJames Bottomley 				     task->data_dir);
570b9142174SJames Bottomley 		kfree(scatter);
571b9142174SJames Bottomley 	}
572b9142174SJames Bottomley out:
573b9142174SJames Bottomley 	return res;
574b9142174SJames Bottomley }
575b9142174SJames Bottomley 
576b9142174SJames Bottomley /* ---------- SATA ---------- */
577b9142174SJames Bottomley 
578b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
579b9142174SJames Bottomley {
580b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
581b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
582b9142174SJames Bottomley 
583b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
584b9142174SJames Bottomley 	     fis->lbal         == 1 &&
585b9142174SJames Bottomley 	     fis->lbam         == 0 &&
586b9142174SJames Bottomley 	     fis->lbah         == 0 &&
587b9142174SJames Bottomley 	     fis->device       == 0)
588b9142174SJames Bottomley 	    ||
589b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
590b9142174SJames Bottomley 	     fis->lbal         == 0 &&
591b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
592b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
593b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
594b9142174SJames Bottomley 
595b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
596b9142174SJames Bottomley 
597b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
598b9142174SJames Bottomley 		  fis->lbal             == 1 &&
599b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
600b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
601b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
602b9142174SJames Bottomley 
603b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
604b9142174SJames Bottomley 
605b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
606b9142174SJames Bottomley 		  fis->lbal         == 1 &&
607b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
608b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
609b9142174SJames Bottomley 		  fis->device       == 0)
610b9142174SJames Bottomley 		||
611b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
612b9142174SJames Bottomley 		  fis->lbal             == 1 &&
613b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
614b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
615b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
616b9142174SJames Bottomley 
617b9142174SJames Bottomley 		/* Treat it as a superset? */
618b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
619b9142174SJames Bottomley }
620b9142174SJames Bottomley 
621b9142174SJames Bottomley /**
622b9142174SJames Bottomley  * sas_issue_ata_cmd -- Basic SATA command processing for discovery
623b9142174SJames Bottomley  * @dev: the device to send the command to
624b9142174SJames Bottomley  * @command: the command register
625b9142174SJames Bottomley  * @features: the features register
626b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
627b9142174SJames Bottomley  * @size: size of @buffer
6281d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
629b9142174SJames Bottomley  */
630b9142174SJames Bottomley static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
631b9142174SJames Bottomley 			     u8 features, void *buffer, int size,
6321d1bbee6SJeff Garzik 			     enum dma_data_direction dma_dir)
633b9142174SJames Bottomley {
634b9142174SJames Bottomley 	int res = 0;
635b9142174SJames Bottomley 	struct sas_task *task;
636b9142174SJames Bottomley 	struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
637b9142174SJames Bottomley 		&dev->frame_rcvd[0];
638b9142174SJames Bottomley 
639b9142174SJames Bottomley 	res = -ENOMEM;
640b9142174SJames Bottomley 	task = sas_alloc_task(GFP_KERNEL);
641b9142174SJames Bottomley 	if (!task)
642b9142174SJames Bottomley 		goto out;
643b9142174SJames Bottomley 
644b9142174SJames Bottomley 	task->dev = dev;
645b9142174SJames Bottomley 
646b9142174SJames Bottomley 	task->ata_task.fis.fis_type = 0x27;
647b9142174SJames Bottomley 	task->ata_task.fis.command = command;
648b9142174SJames Bottomley 	task->ata_task.fis.features = features;
649b9142174SJames Bottomley 	task->ata_task.fis.device = d2h_fis->device;
650b9142174SJames Bottomley 	task->ata_task.retry_count = 1;
651b9142174SJames Bottomley 
6521d1bbee6SJeff Garzik 	res = sas_execute_task(task, buffer, size, dma_dir);
653b9142174SJames Bottomley 
654b9142174SJames Bottomley 	sas_free_task(task);
655b9142174SJames Bottomley out:
656b9142174SJames Bottomley 	return res;
657b9142174SJames Bottomley }
658b9142174SJames Bottomley 
659b9142174SJames Bottomley static void sas_sata_propagate_sas_addr(struct domain_device *dev)
660b9142174SJames Bottomley {
661b9142174SJames Bottomley 	unsigned long flags;
662b9142174SJames Bottomley 	struct asd_sas_port *port = dev->port;
663b9142174SJames Bottomley 	struct asd_sas_phy  *phy;
664b9142174SJames Bottomley 
665b9142174SJames Bottomley 	BUG_ON(dev->parent);
666b9142174SJames Bottomley 
667b9142174SJames Bottomley 	memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
668b9142174SJames Bottomley 	spin_lock_irqsave(&port->phy_list_lock, flags);
669b9142174SJames Bottomley 	list_for_each_entry(phy, &port->phy_list, port_phy_el)
670b9142174SJames Bottomley 		memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
671b9142174SJames Bottomley 	spin_unlock_irqrestore(&port->phy_list_lock, flags);
672b9142174SJames Bottomley }
673b9142174SJames Bottomley 
674b9142174SJames Bottomley #define ATA_IDENTIFY_DEV         0xEC
675b9142174SJames Bottomley #define ATA_IDENTIFY_PACKET_DEV  0xA1
676b9142174SJames Bottomley #define ATA_SET_FEATURES         0xEF
677b9142174SJames Bottomley #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
678b9142174SJames Bottomley 
679b9142174SJames Bottomley /**
680b9142174SJames Bottomley  * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
681b9142174SJames Bottomley  * @dev: STP/SATA device of interest (ATA/ATAPI)
682b9142174SJames Bottomley  *
683b9142174SJames Bottomley  * The LLDD has already been notified of this device, so that we can
684b9142174SJames Bottomley  * send FISes to it.  Here we try to get IDENTIFY DEVICE or IDENTIFY
685b9142174SJames Bottomley  * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
686b9142174SJames Bottomley  * performance for this device.
687b9142174SJames Bottomley  */
688b9142174SJames Bottomley static int sas_discover_sata_dev(struct domain_device *dev)
689b9142174SJames Bottomley {
690b9142174SJames Bottomley 	int     res;
691b9142174SJames Bottomley 	__le16  *identify_x;
692b9142174SJames Bottomley 	u8      command;
693b9142174SJames Bottomley 
694b9142174SJames Bottomley 	identify_x = kzalloc(512, GFP_KERNEL);
695b9142174SJames Bottomley 	if (!identify_x)
696b9142174SJames Bottomley 		return -ENOMEM;
697b9142174SJames Bottomley 
698b9142174SJames Bottomley 	if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
699b9142174SJames Bottomley 		dev->sata_dev.identify_device = identify_x;
700b9142174SJames Bottomley 		command = ATA_IDENTIFY_DEV;
701b9142174SJames Bottomley 	} else {
702b9142174SJames Bottomley 		dev->sata_dev.identify_packet_device = identify_x;
703b9142174SJames Bottomley 		command = ATA_IDENTIFY_PACKET_DEV;
704b9142174SJames Bottomley 	}
705b9142174SJames Bottomley 
706b9142174SJames Bottomley 	res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
7071d1bbee6SJeff Garzik 				DMA_FROM_DEVICE);
708b9142174SJames Bottomley 	if (res)
709b9142174SJames Bottomley 		goto out_err;
710b9142174SJames Bottomley 
711b9142174SJames Bottomley 	/* lives on the media? */
712b9142174SJames Bottomley 	if (le16_to_cpu(identify_x[0]) & 4) {
713b9142174SJames Bottomley 		/* incomplete response */
714b9142174SJames Bottomley 		SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
715b9142174SJames Bottomley 			    "dev %llx\n", SAS_ADDR(dev->sas_addr));
716b9142174SJames Bottomley 		if (!le16_to_cpu(identify_x[83] & (1<<6)))
717b9142174SJames Bottomley 			goto cont1;
718b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
719b9142174SJames Bottomley 					ATA_FEATURE_PUP_STBY_SPIN_UP,
7201d1bbee6SJeff Garzik 					NULL, 0, DMA_NONE);
721b9142174SJames Bottomley 		if (res)
722b9142174SJames Bottomley 			goto cont1;
723b9142174SJames Bottomley 
724b9142174SJames Bottomley 		schedule_timeout_interruptible(5*HZ); /* More time? */
725b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
7261d1bbee6SJeff Garzik 					DMA_FROM_DEVICE);
727b9142174SJames Bottomley 		if (res)
728b9142174SJames Bottomley 			goto out_err;
729b9142174SJames Bottomley 	}
730b9142174SJames Bottomley cont1:
731b9142174SJames Bottomley 	/* Get WWN */
732b9142174SJames Bottomley 	if (dev->port->oob_mode != SATA_OOB_MODE) {
733b9142174SJames Bottomley 		memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
734b9142174SJames Bottomley 		       SAS_ADDR_SIZE);
735b9142174SJames Bottomley 	} else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
736b9142174SJames Bottomley 		   (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
737b9142174SJames Bottomley 		   == 0x5000) {
738b9142174SJames Bottomley 		int i;
739b9142174SJames Bottomley 
740b9142174SJames Bottomley 		for (i = 0; i < 4; i++) {
741b9142174SJames Bottomley 			dev->sas_addr[2*i] =
742b9142174SJames Bottomley 	     (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
743b9142174SJames Bottomley 			dev->sas_addr[2*i+1] =
744b9142174SJames Bottomley 	      le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
745b9142174SJames Bottomley 		}
746b9142174SJames Bottomley 	}
747b9142174SJames Bottomley 	sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
748b9142174SJames Bottomley 	if (!dev->parent)
749b9142174SJames Bottomley 		sas_sata_propagate_sas_addr(dev);
750b9142174SJames Bottomley 
751b9142174SJames Bottomley 	/* XXX Hint: register this SATA device with SATL.
752b9142174SJames Bottomley 	   When this returns, dev->sata_dev->lu is alive and
753b9142174SJames Bottomley 	   present.
754b9142174SJames Bottomley 	sas_satl_register_dev(dev);
755b9142174SJames Bottomley 	*/
756b9142174SJames Bottomley 
757b9142174SJames Bottomley 	sas_fill_in_rphy(dev, dev->rphy);
758b9142174SJames Bottomley 
759b9142174SJames Bottomley 	return 0;
760b9142174SJames Bottomley out_err:
761b9142174SJames Bottomley 	dev->sata_dev.identify_packet_device = NULL;
762b9142174SJames Bottomley 	dev->sata_dev.identify_device = NULL;
763b9142174SJames Bottomley 	kfree(identify_x);
764b9142174SJames Bottomley 	return res;
765b9142174SJames Bottomley }
766b9142174SJames Bottomley 
767b9142174SJames Bottomley static int sas_discover_sata_pm(struct domain_device *dev)
768b9142174SJames Bottomley {
769b9142174SJames Bottomley 	return -ENODEV;
770b9142174SJames Bottomley }
771b9142174SJames Bottomley 
772b9142174SJames Bottomley /**
773b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
774b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
775b9142174SJames Bottomley  *
776b9142174SJames Bottomley  * First we notify the LLDD of this device, so we can send frames to
777b9142174SJames Bottomley  * it.  Then depending on the type of device we call the appropriate
778b9142174SJames Bottomley  * discover functions.  Once device discover is done, we notify the
779b9142174SJames Bottomley  * LLDD so that it can fine-tune its parameters for the device, by
780b9142174SJames Bottomley  * removing it and then adding it.  That is, the second time around,
781b9142174SJames Bottomley  * the driver would have certain fields, that it is looking at, set.
782b9142174SJames Bottomley  * Finally we initialize the kobj so that the device can be added to
783b9142174SJames Bottomley  * the system at registration time.  Devices directly attached to a HA
784b9142174SJames Bottomley  * port, have no parents.  All other devices do, and should have their
785b9142174SJames Bottomley  * "parent" pointer set appropriately before calling this function.
786b9142174SJames Bottomley  */
787b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
788b9142174SJames Bottomley {
789b9142174SJames Bottomley 	int res;
790b9142174SJames Bottomley 
791b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
792b9142174SJames Bottomley 
793b9142174SJames Bottomley 	res = sas_notify_lldd_dev_found(dev);
794b9142174SJames Bottomley 	if (res)
795b9142174SJames Bottomley 		return res;
796b9142174SJames Bottomley 
797b9142174SJames Bottomley 	switch (dev->dev_type) {
798b9142174SJames Bottomley 	case SATA_DEV:
799b9142174SJames Bottomley 		res = sas_discover_sata_dev(dev);
800b9142174SJames Bottomley 		break;
801b9142174SJames Bottomley 	case SATA_PM:
802b9142174SJames Bottomley 		res = sas_discover_sata_pm(dev);
803b9142174SJames Bottomley 		break;
804b9142174SJames Bottomley 	default:
805b9142174SJames Bottomley 		break;
806b9142174SJames Bottomley 	}
807b9142174SJames Bottomley 	sas_notify_lldd_dev_gone(dev);
808b9142174SJames Bottomley 	if (!res) {
809b9142174SJames Bottomley 		sas_notify_lldd_dev_found(dev);
810b9142174SJames Bottomley 		res = sas_rphy_add(dev->rphy);
811b9142174SJames Bottomley 	}
812b9142174SJames Bottomley 
813b9142174SJames Bottomley 	return res;
814b9142174SJames Bottomley }
815