xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision b91421749a1840148d8c81637c03c0ace3f35269)
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 
24*b9142174SJames Bottomley #include <linux/scatterlist.h>
25*b9142174SJames 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 num = 0;
162338ec570SDarrick J. Wong 	unsigned int xfer = 0;
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;
179338ec570SDarrick J. Wong 	if (is_atapi_taskfile(&qc->tf)) {
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 {
184338ec570SDarrick J. Wong 		ata_for_each_sg(sg, qc) {
185338ec570SDarrick J. Wong 			num++;
186338ec570SDarrick J. Wong 			xfer += sg->length;
187338ec570SDarrick J. Wong 		}
188338ec570SDarrick J. Wong 
189338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
190338ec570SDarrick J. Wong 		task->num_scatter = num;
191338ec570SDarrick J. Wong 	}
192338ec570SDarrick J. Wong 
193338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
194338ec570SDarrick J. Wong 	task->scatter = qc->__sg;
195338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
196338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
1971c50dc83SDarrick J. Wong 	qc->lldd_task = task;
198338ec570SDarrick J. Wong 
199338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
200338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
201338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
202338ec570SDarrick J. Wong 		/* fall through */
203338ec570SDarrick J. Wong 	case ATA_PROT_ATAPI_DMA:
204338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
205338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
206338ec570SDarrick J. Wong 		break;
207338ec570SDarrick J. Wong 	}
208338ec570SDarrick J. Wong 
209fe059f12SDarrick J. Wong 	if (qc->scsicmd)
210fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
211fe059f12SDarrick J. Wong 
212338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
213338ec570SDarrick J. Wong 		res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
214338ec570SDarrick J. Wong 	else
215338ec570SDarrick J. Wong 		res = sas_queue_up(task);
216338ec570SDarrick J. Wong 
217338ec570SDarrick J. Wong 	/* Examine */
218338ec570SDarrick J. Wong 	if (res) {
219338ec570SDarrick J. Wong 		SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
220338ec570SDarrick J. Wong 
221fe059f12SDarrick J. Wong 		if (qc->scsicmd)
222fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
223338ec570SDarrick J. Wong 		sas_free_task(task);
22435a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
225338ec570SDarrick J. Wong 	}
226338ec570SDarrick J. Wong 
22735a7f2f6SDarrick J. Wong 	return 0;
228338ec570SDarrick J. Wong }
229338ec570SDarrick J. Wong 
230338ec570SDarrick J. Wong static u8 sas_ata_check_status(struct ata_port *ap)
231338ec570SDarrick J. Wong {
232338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
233338ec570SDarrick J. Wong 	return dev->sata_dev.tf.command;
234338ec570SDarrick J. Wong }
235338ec570SDarrick J. Wong 
236338ec570SDarrick J. Wong static void sas_ata_phy_reset(struct ata_port *ap)
237338ec570SDarrick J. Wong {
238338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
239338ec570SDarrick J. Wong 	struct sas_internal *i =
240338ec570SDarrick J. Wong 		to_sas_internal(dev->port->ha->core.shost->transportt);
241338ec570SDarrick J. Wong 	int res = 0;
242338ec570SDarrick J. Wong 
243338ec570SDarrick J. Wong 	if (i->dft->lldd_I_T_nexus_reset)
244338ec570SDarrick J. Wong 		res = i->dft->lldd_I_T_nexus_reset(dev);
245338ec570SDarrick J. Wong 
246338ec570SDarrick J. Wong 	if (res)
247338ec570SDarrick J. Wong 		SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
248338ec570SDarrick J. Wong 
249338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
250338ec570SDarrick J. Wong 		case ATA_COMMAND_SET:
251338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
252338ec570SDarrick J. Wong 			ap->device[0].class = ATA_DEV_ATA;
253338ec570SDarrick J. Wong 			break;
254338ec570SDarrick J. Wong 		case ATAPI_COMMAND_SET:
255338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
256338ec570SDarrick J. Wong 			ap->device[0].class = ATA_DEV_ATAPI;
257338ec570SDarrick J. Wong 			break;
258338ec570SDarrick J. Wong 		default:
259338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
260338ec570SDarrick J. Wong 				    __FUNCTION__,
261338ec570SDarrick J. Wong 				    dev->sata_dev.command_set);
2625986c3d3SDarrick J. Wong 			ap->device[0].class = ATA_DEV_UNKNOWN;
263338ec570SDarrick J. Wong 			break;
264338ec570SDarrick J. Wong 	}
265338ec570SDarrick J. Wong 
266338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
267338ec570SDarrick J. Wong }
268338ec570SDarrick J. Wong 
269338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
270338ec570SDarrick J. Wong {
271338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
272338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
273338ec570SDarrick J. Wong 
2741c50dc83SDarrick J. Wong 	if (qc->err_mask) {
2751c50dc83SDarrick J. Wong 		/*
2761c50dc83SDarrick J. Wong 		 * Find the sas_task and kill it.  By this point,
2771c50dc83SDarrick J. Wong 		 * libata has decided to kill the qc, so we needn't
2781c50dc83SDarrick J. Wong 		 * bother with sas_ata_task_done.  But we still
2791c50dc83SDarrick J. Wong 		 * ought to abort the task.
2801c50dc83SDarrick J. Wong 		 */
2811c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
2823a2755afSDarrick J. Wong 		unsigned long flags;
2831c50dc83SDarrick J. Wong 
2841c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
2851c50dc83SDarrick J. Wong 		if (task) {
2863a2755afSDarrick J. Wong 			/* Should this be a AT(API) device reset? */
2873a2755afSDarrick J. Wong 			spin_lock_irqsave(&task->task_state_lock, flags);
2883a2755afSDarrick J. Wong 			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
2893a2755afSDarrick J. Wong 			spin_unlock_irqrestore(&task->task_state_lock, flags);
2903a2755afSDarrick J. Wong 
2911c50dc83SDarrick J. Wong 			task->uldd_task = NULL;
2921c50dc83SDarrick J. Wong 			__sas_task_abort(task);
2931c50dc83SDarrick J. Wong 		}
2941c50dc83SDarrick J. Wong 	}
295338ec570SDarrick J. Wong }
296338ec570SDarrick J. Wong 
297338ec570SDarrick J. Wong static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
298338ec570SDarrick J. Wong {
299338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
300338ec570SDarrick J. Wong 	memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
301338ec570SDarrick J. Wong }
302338ec570SDarrick J. Wong 
303110dd8f1SJames Bottomley static int sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
304338ec570SDarrick J. Wong 			      u32 val)
305338ec570SDarrick J. Wong {
306338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
307338ec570SDarrick J. Wong 
308338ec570SDarrick J. Wong 	SAS_DPRINTK("STUB %s\n", __FUNCTION__);
309338ec570SDarrick J. Wong 	switch (sc_reg_in) {
310338ec570SDarrick J. Wong 		case SCR_STATUS:
311338ec570SDarrick J. Wong 			dev->sata_dev.sstatus = val;
312338ec570SDarrick J. Wong 			break;
313338ec570SDarrick J. Wong 		case SCR_CONTROL:
314338ec570SDarrick J. Wong 			dev->sata_dev.scontrol = val;
315338ec570SDarrick J. Wong 			break;
316338ec570SDarrick J. Wong 		case SCR_ERROR:
317338ec570SDarrick J. Wong 			dev->sata_dev.serror = val;
318338ec570SDarrick J. Wong 			break;
319338ec570SDarrick J. Wong 		case SCR_ACTIVE:
320338ec570SDarrick J. Wong 			dev->sata_dev.ap->sactive = val;
321338ec570SDarrick J. Wong 			break;
322110dd8f1SJames Bottomley 		default:
323110dd8f1SJames Bottomley 			return -EINVAL;
324338ec570SDarrick J. Wong 	}
325110dd8f1SJames Bottomley 	return 0;
326338ec570SDarrick J. Wong }
327338ec570SDarrick J. Wong 
328110dd8f1SJames Bottomley static int sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
329110dd8f1SJames Bottomley 			    u32 *val)
330338ec570SDarrick J. Wong {
331338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
332338ec570SDarrick J. Wong 
333338ec570SDarrick J. Wong 	SAS_DPRINTK("STUB %s\n", __FUNCTION__);
334338ec570SDarrick J. Wong 	switch (sc_reg_in) {
335338ec570SDarrick J. Wong 		case SCR_STATUS:
336110dd8f1SJames Bottomley 			*val = dev->sata_dev.sstatus;
337110dd8f1SJames Bottomley 			return 0;
338338ec570SDarrick J. Wong 		case SCR_CONTROL:
339110dd8f1SJames Bottomley 			*val = dev->sata_dev.scontrol;
340110dd8f1SJames Bottomley 			return 0;
341338ec570SDarrick J. Wong 		case SCR_ERROR:
342110dd8f1SJames Bottomley 			*val = dev->sata_dev.serror;
343110dd8f1SJames Bottomley 			return 0;
344338ec570SDarrick J. Wong 		case SCR_ACTIVE:
345110dd8f1SJames Bottomley 			*val = dev->sata_dev.ap->sactive;
346110dd8f1SJames Bottomley 			return 0;
347338ec570SDarrick J. Wong 		default:
348110dd8f1SJames Bottomley 			return -EINVAL;
349338ec570SDarrick J. Wong 	}
350338ec570SDarrick J. Wong }
351338ec570SDarrick J. Wong 
352338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
353338ec570SDarrick J. Wong 	.port_disable		= ata_port_disable,
354338ec570SDarrick J. Wong 	.check_status		= sas_ata_check_status,
355338ec570SDarrick J. Wong 	.check_altstatus	= sas_ata_check_status,
356338ec570SDarrick J. Wong 	.dev_select		= ata_noop_dev_select,
357338ec570SDarrick J. Wong 	.phy_reset		= sas_ata_phy_reset,
358338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
359338ec570SDarrick J. Wong 	.tf_read		= sas_ata_tf_read,
360338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
361338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
362338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
363338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
364338ec570SDarrick J. Wong 	.scr_read		= sas_ata_scr_read,
365338ec570SDarrick J. Wong 	.scr_write		= sas_ata_scr_write
366338ec570SDarrick J. Wong };
367338ec570SDarrick J. Wong 
368338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
369338ec570SDarrick J. Wong 	.flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
370338ec570SDarrick J. Wong 		ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
371338ec570SDarrick J. Wong 	.pio_mask = 0x1f, /* PIO0-4 */
372338ec570SDarrick J. Wong 	.mwdma_mask = 0x07, /* MWDMA0-2 */
373338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
374338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
375338ec570SDarrick J. Wong };
376338ec570SDarrick J. Wong 
377338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
378338ec570SDarrick J. Wong 			       struct scsi_target *starget)
379338ec570SDarrick J. Wong {
380338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
381338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
382338ec570SDarrick J. Wong 	struct ata_port *ap;
383338ec570SDarrick J. Wong 
384338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
385338ec570SDarrick J. Wong 		      &ha->pcidev->dev,
386338ec570SDarrick J. Wong 		      sata_port_info.flags,
387338ec570SDarrick J. Wong 		      &sas_sata_ops);
388338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
389338ec570SDarrick J. Wong 				&sata_port_info,
390338ec570SDarrick J. Wong 				shost);
391338ec570SDarrick J. Wong 	if (!ap) {
392338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
393338ec570SDarrick J. Wong 		return -ENODEV;
394338ec570SDarrick J. Wong 	}
395338ec570SDarrick J. Wong 
396338ec570SDarrick J. Wong 	ap->private_data = found_dev;
397338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
398338ec570SDarrick J. Wong 	ap->scsi_host = shost;
399338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
400338ec570SDarrick J. Wong 
401338ec570SDarrick J. Wong 	return 0;
402338ec570SDarrick J. Wong }
4033a2755afSDarrick J. Wong 
4043a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
4053a2755afSDarrick J. Wong {
4063a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
4073a2755afSDarrick J. Wong 	struct completion *waiting;
4083a2755afSDarrick J. Wong 
4093a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
4103a2755afSDarrick J. Wong 	if (qc->scsicmd) {
4113a2755afSDarrick J. Wong 		scsi_req_abort_cmd(qc->scsicmd);
4123a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
4133a2755afSDarrick J. Wong 		return;
4143a2755afSDarrick J. Wong 	}
4153a2755afSDarrick J. Wong 
4163a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
4173a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4183a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
4193a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
4203a2755afSDarrick J. Wong 	waiting = qc->private_data;
4213a2755afSDarrick J. Wong 	complete(waiting);
4223a2755afSDarrick J. Wong }
423*b9142174SJames Bottomley 
424*b9142174SJames Bottomley static void sas_task_timedout(unsigned long _task)
425*b9142174SJames Bottomley {
426*b9142174SJames Bottomley 	struct sas_task *task = (void *) _task;
427*b9142174SJames Bottomley 	unsigned long flags;
428*b9142174SJames Bottomley 
429*b9142174SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
430*b9142174SJames Bottomley 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
431*b9142174SJames Bottomley 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
432*b9142174SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
433*b9142174SJames Bottomley 
434*b9142174SJames Bottomley 	complete(&task->completion);
435*b9142174SJames Bottomley }
436*b9142174SJames Bottomley 
437*b9142174SJames Bottomley static void sas_disc_task_done(struct sas_task *task)
438*b9142174SJames Bottomley {
439*b9142174SJames Bottomley 	if (!del_timer(&task->timer))
440*b9142174SJames Bottomley 		return;
441*b9142174SJames Bottomley 	complete(&task->completion);
442*b9142174SJames Bottomley }
443*b9142174SJames Bottomley 
444*b9142174SJames Bottomley #define SAS_DEV_TIMEOUT 10
445*b9142174SJames Bottomley 
446*b9142174SJames Bottomley /**
447*b9142174SJames Bottomley  * sas_execute_task -- Basic task processing for discovery
448*b9142174SJames Bottomley  * @task: the task to be executed
449*b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
450*b9142174SJames Bottomley  * @size: size of @buffer
451*b9142174SJames Bottomley  * @pci_dma_dir: PCI_DMA_...
452*b9142174SJames Bottomley  */
453*b9142174SJames Bottomley static int sas_execute_task(struct sas_task *task, void *buffer, int size,
454*b9142174SJames Bottomley 			    int pci_dma_dir)
455*b9142174SJames Bottomley {
456*b9142174SJames Bottomley 	int res = 0;
457*b9142174SJames Bottomley 	struct scatterlist *scatter = NULL;
458*b9142174SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
459*b9142174SJames Bottomley 	int num_scatter = 0;
460*b9142174SJames Bottomley 	int retries = 0;
461*b9142174SJames Bottomley 	struct sas_internal *i =
462*b9142174SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
463*b9142174SJames Bottomley 
464*b9142174SJames Bottomley 	if (pci_dma_dir != PCI_DMA_NONE) {
465*b9142174SJames Bottomley 		scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
466*b9142174SJames Bottomley 		if (!scatter)
467*b9142174SJames Bottomley 			goto out;
468*b9142174SJames Bottomley 
469*b9142174SJames Bottomley 		sg_init_one(scatter, buffer, size);
470*b9142174SJames Bottomley 		num_scatter = 1;
471*b9142174SJames Bottomley 	}
472*b9142174SJames Bottomley 
473*b9142174SJames Bottomley 	task->task_proto = task->dev->tproto;
474*b9142174SJames Bottomley 	task->scatter = scatter;
475*b9142174SJames Bottomley 	task->num_scatter = num_scatter;
476*b9142174SJames Bottomley 	task->total_xfer_len = size;
477*b9142174SJames Bottomley 	task->data_dir = pci_dma_dir;
478*b9142174SJames Bottomley 	task->task_done = sas_disc_task_done;
479*b9142174SJames Bottomley 	if (pci_dma_dir != PCI_DMA_NONE &&
480*b9142174SJames Bottomley 	    sas_protocol_ata(task->task_proto)) {
481*b9142174SJames Bottomley 		task->num_scatter = pci_map_sg(task->dev->port->ha->pcidev,
482*b9142174SJames Bottomley 					       task->scatter,
483*b9142174SJames Bottomley 					       task->num_scatter,
484*b9142174SJames Bottomley 					       task->data_dir);
485*b9142174SJames Bottomley 	}
486*b9142174SJames Bottomley 
487*b9142174SJames Bottomley 	for (retries = 0; retries < 5; retries++) {
488*b9142174SJames Bottomley 		task->task_state_flags = SAS_TASK_STATE_PENDING;
489*b9142174SJames Bottomley 		init_completion(&task->completion);
490*b9142174SJames Bottomley 
491*b9142174SJames Bottomley 		task->timer.data = (unsigned long) task;
492*b9142174SJames Bottomley 		task->timer.function = sas_task_timedout;
493*b9142174SJames Bottomley 		task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
494*b9142174SJames Bottomley 		add_timer(&task->timer);
495*b9142174SJames Bottomley 
496*b9142174SJames Bottomley 		res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
497*b9142174SJames Bottomley 		if (res) {
498*b9142174SJames Bottomley 			del_timer(&task->timer);
499*b9142174SJames Bottomley 			SAS_DPRINTK("executing SAS discovery task failed:%d\n",
500*b9142174SJames Bottomley 				    res);
501*b9142174SJames Bottomley 			goto ex_err;
502*b9142174SJames Bottomley 		}
503*b9142174SJames Bottomley 		wait_for_completion(&task->completion);
504*b9142174SJames Bottomley 		res = -ETASK;
505*b9142174SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
506*b9142174SJames Bottomley 			int res2;
507*b9142174SJames Bottomley 			SAS_DPRINTK("task aborted, flags:0x%x\n",
508*b9142174SJames Bottomley 				    task->task_state_flags);
509*b9142174SJames Bottomley 			res2 = i->dft->lldd_abort_task(task);
510*b9142174SJames Bottomley 			SAS_DPRINTK("came back from abort task\n");
511*b9142174SJames Bottomley 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
512*b9142174SJames Bottomley 				if (res2 == TMF_RESP_FUNC_COMPLETE)
513*b9142174SJames Bottomley 					continue; /* Retry the task */
514*b9142174SJames Bottomley 				else
515*b9142174SJames Bottomley 					goto ex_err;
516*b9142174SJames Bottomley 			}
517*b9142174SJames Bottomley 		}
518*b9142174SJames Bottomley 		if (task->task_status.stat == SAM_BUSY ||
519*b9142174SJames Bottomley 			   task->task_status.stat == SAM_TASK_SET_FULL ||
520*b9142174SJames Bottomley 			   task->task_status.stat == SAS_QUEUE_FULL) {
521*b9142174SJames Bottomley 			SAS_DPRINTK("task: q busy, sleeping...\n");
522*b9142174SJames Bottomley 			schedule_timeout_interruptible(HZ);
523*b9142174SJames Bottomley 		} else if (task->task_status.stat == SAM_CHECK_COND) {
524*b9142174SJames Bottomley 			struct scsi_sense_hdr shdr;
525*b9142174SJames Bottomley 
526*b9142174SJames Bottomley 			if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
527*b9142174SJames Bottomley 						  &shdr)) {
528*b9142174SJames Bottomley 				SAS_DPRINTK("couldn't normalize sense\n");
529*b9142174SJames Bottomley 				continue;
530*b9142174SJames Bottomley 			}
531*b9142174SJames Bottomley 			if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
532*b9142174SJames Bottomley 			    (shdr.sense_key == 2 && shdr.asc == 4 &&
533*b9142174SJames Bottomley 			     shdr.ascq == 1)) {
534*b9142174SJames Bottomley 				SAS_DPRINTK("device %016llx LUN: %016llx "
535*b9142174SJames Bottomley 					    "powering up or not ready yet, "
536*b9142174SJames Bottomley 					    "sleeping...\n",
537*b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
538*b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN));
539*b9142174SJames Bottomley 
540*b9142174SJames Bottomley 				schedule_timeout_interruptible(5*HZ);
541*b9142174SJames Bottomley 			} else if (shdr.sense_key == 1) {
542*b9142174SJames Bottomley 				res = 0;
543*b9142174SJames Bottomley 				break;
544*b9142174SJames Bottomley 			} else if (shdr.sense_key == 5) {
545*b9142174SJames Bottomley 				break;
546*b9142174SJames Bottomley 			} else {
547*b9142174SJames Bottomley 				SAS_DPRINTK("dev %016llx LUN: %016llx "
548*b9142174SJames Bottomley 					    "sense key:0x%x ASC:0x%x ASCQ:0x%x"
549*b9142174SJames Bottomley 					    "\n",
550*b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
551*b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN),
552*b9142174SJames Bottomley 					    shdr.sense_key,
553*b9142174SJames Bottomley 					    shdr.asc, shdr.ascq);
554*b9142174SJames Bottomley 			}
555*b9142174SJames Bottomley 		} else if (task->task_status.resp != SAS_TASK_COMPLETE ||
556*b9142174SJames Bottomley 			   task->task_status.stat != SAM_GOOD) {
557*b9142174SJames Bottomley 			SAS_DPRINTK("task finished with resp:0x%x, "
558*b9142174SJames Bottomley 				    "stat:0x%x\n",
559*b9142174SJames Bottomley 				    task->task_status.resp,
560*b9142174SJames Bottomley 				    task->task_status.stat);
561*b9142174SJames Bottomley 			goto ex_err;
562*b9142174SJames Bottomley 		} else {
563*b9142174SJames Bottomley 			res = 0;
564*b9142174SJames Bottomley 			break;
565*b9142174SJames Bottomley 		}
566*b9142174SJames Bottomley 	}
567*b9142174SJames Bottomley ex_err:
568*b9142174SJames Bottomley 	if (pci_dma_dir != PCI_DMA_NONE) {
569*b9142174SJames Bottomley 		if (sas_protocol_ata(task->task_proto))
570*b9142174SJames Bottomley 			pci_unmap_sg(task->dev->port->ha->pcidev,
571*b9142174SJames Bottomley 				     task->scatter, task->num_scatter,
572*b9142174SJames Bottomley 				     task->data_dir);
573*b9142174SJames Bottomley 		kfree(scatter);
574*b9142174SJames Bottomley 	}
575*b9142174SJames Bottomley out:
576*b9142174SJames Bottomley 	return res;
577*b9142174SJames Bottomley }
578*b9142174SJames Bottomley 
579*b9142174SJames Bottomley /* ---------- SATA ---------- */
580*b9142174SJames Bottomley 
581*b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
582*b9142174SJames Bottomley {
583*b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
584*b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
585*b9142174SJames Bottomley 
586*b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
587*b9142174SJames Bottomley 	     fis->lbal         == 1 &&
588*b9142174SJames Bottomley 	     fis->lbam         == 0 &&
589*b9142174SJames Bottomley 	     fis->lbah         == 0 &&
590*b9142174SJames Bottomley 	     fis->device       == 0)
591*b9142174SJames Bottomley 	    ||
592*b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
593*b9142174SJames Bottomley 	     fis->lbal         == 0 &&
594*b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
595*b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
596*b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
597*b9142174SJames Bottomley 
598*b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
599*b9142174SJames Bottomley 
600*b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
601*b9142174SJames Bottomley 		  fis->lbal             == 1 &&
602*b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
603*b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
604*b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
605*b9142174SJames Bottomley 
606*b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
607*b9142174SJames Bottomley 
608*b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
609*b9142174SJames Bottomley 		  fis->lbal         == 1 &&
610*b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
611*b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
612*b9142174SJames Bottomley 		  fis->device       == 0)
613*b9142174SJames Bottomley 		||
614*b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
615*b9142174SJames Bottomley 		  fis->lbal             == 1 &&
616*b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
617*b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
618*b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
619*b9142174SJames Bottomley 
620*b9142174SJames Bottomley 		/* Treat it as a superset? */
621*b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
622*b9142174SJames Bottomley }
623*b9142174SJames Bottomley 
624*b9142174SJames Bottomley /**
625*b9142174SJames Bottomley  * sas_issue_ata_cmd -- Basic SATA command processing for discovery
626*b9142174SJames Bottomley  * @dev: the device to send the command to
627*b9142174SJames Bottomley  * @command: the command register
628*b9142174SJames Bottomley  * @features: the features register
629*b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
630*b9142174SJames Bottomley  * @size: size of @buffer
631*b9142174SJames Bottomley  * @pci_dma_dir: PCI_DMA_...
632*b9142174SJames Bottomley  */
633*b9142174SJames Bottomley static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
634*b9142174SJames Bottomley 			     u8 features, void *buffer, int size,
635*b9142174SJames Bottomley 			     int pci_dma_dir)
636*b9142174SJames Bottomley {
637*b9142174SJames Bottomley 	int res = 0;
638*b9142174SJames Bottomley 	struct sas_task *task;
639*b9142174SJames Bottomley 	struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
640*b9142174SJames Bottomley 		&dev->frame_rcvd[0];
641*b9142174SJames Bottomley 
642*b9142174SJames Bottomley 	res = -ENOMEM;
643*b9142174SJames Bottomley 	task = sas_alloc_task(GFP_KERNEL);
644*b9142174SJames Bottomley 	if (!task)
645*b9142174SJames Bottomley 		goto out;
646*b9142174SJames Bottomley 
647*b9142174SJames Bottomley 	task->dev = dev;
648*b9142174SJames Bottomley 
649*b9142174SJames Bottomley 	task->ata_task.fis.fis_type = 0x27;
650*b9142174SJames Bottomley 	task->ata_task.fis.command = command;
651*b9142174SJames Bottomley 	task->ata_task.fis.features = features;
652*b9142174SJames Bottomley 	task->ata_task.fis.device = d2h_fis->device;
653*b9142174SJames Bottomley 	task->ata_task.retry_count = 1;
654*b9142174SJames Bottomley 
655*b9142174SJames Bottomley 	res = sas_execute_task(task, buffer, size, pci_dma_dir);
656*b9142174SJames Bottomley 
657*b9142174SJames Bottomley 	sas_free_task(task);
658*b9142174SJames Bottomley out:
659*b9142174SJames Bottomley 	return res;
660*b9142174SJames Bottomley }
661*b9142174SJames Bottomley 
662*b9142174SJames Bottomley static void sas_sata_propagate_sas_addr(struct domain_device *dev)
663*b9142174SJames Bottomley {
664*b9142174SJames Bottomley 	unsigned long flags;
665*b9142174SJames Bottomley 	struct asd_sas_port *port = dev->port;
666*b9142174SJames Bottomley 	struct asd_sas_phy  *phy;
667*b9142174SJames Bottomley 
668*b9142174SJames Bottomley 	BUG_ON(dev->parent);
669*b9142174SJames Bottomley 
670*b9142174SJames Bottomley 	memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
671*b9142174SJames Bottomley 	spin_lock_irqsave(&port->phy_list_lock, flags);
672*b9142174SJames Bottomley 	list_for_each_entry(phy, &port->phy_list, port_phy_el)
673*b9142174SJames Bottomley 		memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
674*b9142174SJames Bottomley 	spin_unlock_irqrestore(&port->phy_list_lock, flags);
675*b9142174SJames Bottomley }
676*b9142174SJames Bottomley 
677*b9142174SJames Bottomley #define ATA_IDENTIFY_DEV         0xEC
678*b9142174SJames Bottomley #define ATA_IDENTIFY_PACKET_DEV  0xA1
679*b9142174SJames Bottomley #define ATA_SET_FEATURES         0xEF
680*b9142174SJames Bottomley #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
681*b9142174SJames Bottomley 
682*b9142174SJames Bottomley /**
683*b9142174SJames Bottomley  * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
684*b9142174SJames Bottomley  * @dev: STP/SATA device of interest (ATA/ATAPI)
685*b9142174SJames Bottomley  *
686*b9142174SJames Bottomley  * The LLDD has already been notified of this device, so that we can
687*b9142174SJames Bottomley  * send FISes to it.  Here we try to get IDENTIFY DEVICE or IDENTIFY
688*b9142174SJames Bottomley  * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
689*b9142174SJames Bottomley  * performance for this device.
690*b9142174SJames Bottomley  */
691*b9142174SJames Bottomley static int sas_discover_sata_dev(struct domain_device *dev)
692*b9142174SJames Bottomley {
693*b9142174SJames Bottomley 	int     res;
694*b9142174SJames Bottomley 	__le16  *identify_x;
695*b9142174SJames Bottomley 	u8      command;
696*b9142174SJames Bottomley 
697*b9142174SJames Bottomley 	identify_x = kzalloc(512, GFP_KERNEL);
698*b9142174SJames Bottomley 	if (!identify_x)
699*b9142174SJames Bottomley 		return -ENOMEM;
700*b9142174SJames Bottomley 
701*b9142174SJames Bottomley 	if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
702*b9142174SJames Bottomley 		dev->sata_dev.identify_device = identify_x;
703*b9142174SJames Bottomley 		command = ATA_IDENTIFY_DEV;
704*b9142174SJames Bottomley 	} else {
705*b9142174SJames Bottomley 		dev->sata_dev.identify_packet_device = identify_x;
706*b9142174SJames Bottomley 		command = ATA_IDENTIFY_PACKET_DEV;
707*b9142174SJames Bottomley 	}
708*b9142174SJames Bottomley 
709*b9142174SJames Bottomley 	res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
710*b9142174SJames Bottomley 				PCI_DMA_FROMDEVICE);
711*b9142174SJames Bottomley 	if (res)
712*b9142174SJames Bottomley 		goto out_err;
713*b9142174SJames Bottomley 
714*b9142174SJames Bottomley 	/* lives on the media? */
715*b9142174SJames Bottomley 	if (le16_to_cpu(identify_x[0]) & 4) {
716*b9142174SJames Bottomley 		/* incomplete response */
717*b9142174SJames Bottomley 		SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
718*b9142174SJames Bottomley 			    "dev %llx\n", SAS_ADDR(dev->sas_addr));
719*b9142174SJames Bottomley 		if (!le16_to_cpu(identify_x[83] & (1<<6)))
720*b9142174SJames Bottomley 			goto cont1;
721*b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
722*b9142174SJames Bottomley 					ATA_FEATURE_PUP_STBY_SPIN_UP,
723*b9142174SJames Bottomley 					NULL, 0, PCI_DMA_NONE);
724*b9142174SJames Bottomley 		if (res)
725*b9142174SJames Bottomley 			goto cont1;
726*b9142174SJames Bottomley 
727*b9142174SJames Bottomley 		schedule_timeout_interruptible(5*HZ); /* More time? */
728*b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
729*b9142174SJames Bottomley 					PCI_DMA_FROMDEVICE);
730*b9142174SJames Bottomley 		if (res)
731*b9142174SJames Bottomley 			goto out_err;
732*b9142174SJames Bottomley 	}
733*b9142174SJames Bottomley cont1:
734*b9142174SJames Bottomley 	/* Get WWN */
735*b9142174SJames Bottomley 	if (dev->port->oob_mode != SATA_OOB_MODE) {
736*b9142174SJames Bottomley 		memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
737*b9142174SJames Bottomley 		       SAS_ADDR_SIZE);
738*b9142174SJames Bottomley 	} else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
739*b9142174SJames Bottomley 		   (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
740*b9142174SJames Bottomley 		   == 0x5000) {
741*b9142174SJames Bottomley 		int i;
742*b9142174SJames Bottomley 
743*b9142174SJames Bottomley 		for (i = 0; i < 4; i++) {
744*b9142174SJames Bottomley 			dev->sas_addr[2*i] =
745*b9142174SJames Bottomley 	     (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
746*b9142174SJames Bottomley 			dev->sas_addr[2*i+1] =
747*b9142174SJames Bottomley 	      le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
748*b9142174SJames Bottomley 		}
749*b9142174SJames Bottomley 	}
750*b9142174SJames Bottomley 	sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
751*b9142174SJames Bottomley 	if (!dev->parent)
752*b9142174SJames Bottomley 		sas_sata_propagate_sas_addr(dev);
753*b9142174SJames Bottomley 
754*b9142174SJames Bottomley 	/* XXX Hint: register this SATA device with SATL.
755*b9142174SJames Bottomley 	   When this returns, dev->sata_dev->lu is alive and
756*b9142174SJames Bottomley 	   present.
757*b9142174SJames Bottomley 	sas_satl_register_dev(dev);
758*b9142174SJames Bottomley 	*/
759*b9142174SJames Bottomley 
760*b9142174SJames Bottomley 	sas_fill_in_rphy(dev, dev->rphy);
761*b9142174SJames Bottomley 
762*b9142174SJames Bottomley 	return 0;
763*b9142174SJames Bottomley out_err:
764*b9142174SJames Bottomley 	dev->sata_dev.identify_packet_device = NULL;
765*b9142174SJames Bottomley 	dev->sata_dev.identify_device = NULL;
766*b9142174SJames Bottomley 	kfree(identify_x);
767*b9142174SJames Bottomley 	return res;
768*b9142174SJames Bottomley }
769*b9142174SJames Bottomley 
770*b9142174SJames Bottomley static int sas_discover_sata_pm(struct domain_device *dev)
771*b9142174SJames Bottomley {
772*b9142174SJames Bottomley 	return -ENODEV;
773*b9142174SJames Bottomley }
774*b9142174SJames Bottomley 
775*b9142174SJames Bottomley /**
776*b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
777*b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
778*b9142174SJames Bottomley  *
779*b9142174SJames Bottomley  * First we notify the LLDD of this device, so we can send frames to
780*b9142174SJames Bottomley  * it.  Then depending on the type of device we call the appropriate
781*b9142174SJames Bottomley  * discover functions.  Once device discover is done, we notify the
782*b9142174SJames Bottomley  * LLDD so that it can fine-tune its parameters for the device, by
783*b9142174SJames Bottomley  * removing it and then adding it.  That is, the second time around,
784*b9142174SJames Bottomley  * the driver would have certain fields, that it is looking at, set.
785*b9142174SJames Bottomley  * Finally we initialize the kobj so that the device can be added to
786*b9142174SJames Bottomley  * the system at registration time.  Devices directly attached to a HA
787*b9142174SJames Bottomley  * port, have no parents.  All other devices do, and should have their
788*b9142174SJames Bottomley  * "parent" pointer set appropriately before calling this function.
789*b9142174SJames Bottomley  */
790*b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
791*b9142174SJames Bottomley {
792*b9142174SJames Bottomley 	int res;
793*b9142174SJames Bottomley 
794*b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
795*b9142174SJames Bottomley 
796*b9142174SJames Bottomley 	res = sas_notify_lldd_dev_found(dev);
797*b9142174SJames Bottomley 	if (res)
798*b9142174SJames Bottomley 		return res;
799*b9142174SJames Bottomley 
800*b9142174SJames Bottomley 	switch (dev->dev_type) {
801*b9142174SJames Bottomley 	case SATA_DEV:
802*b9142174SJames Bottomley 		res = sas_discover_sata_dev(dev);
803*b9142174SJames Bottomley 		break;
804*b9142174SJames Bottomley 	case SATA_PM:
805*b9142174SJames Bottomley 		res = sas_discover_sata_pm(dev);
806*b9142174SJames Bottomley 		break;
807*b9142174SJames Bottomley 	default:
808*b9142174SJames Bottomley 		break;
809*b9142174SJames Bottomley 	}
810*b9142174SJames Bottomley 	sas_notify_lldd_dev_gone(dev);
811*b9142174SJames Bottomley 	if (!res) {
812*b9142174SJames Bottomley 		sas_notify_lldd_dev_found(dev);
813*b9142174SJames Bottomley 		res = sas_rphy_add(dev->rphy);
814*b9142174SJames Bottomley 	}
815*b9142174SJames Bottomley 
816*b9142174SJames Bottomley 	return res;
817*b9142174SJames Bottomley }
818