xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision 00dd4998a60599d98b4d6635820a1fbeafa5b021)
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 
74df64d3caSJames Bottomley 		case SAM_STAT_CHECK_CONDITION:
75338ec570SDarrick J. Wong 		case SAS_OPEN_TO:
76338ec570SDarrick J. Wong 		case SAS_OPEN_REJECT:
77338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Saw error %d.  What to do?\n",
78cadbd4a5SHarvey Harrison 				    __func__, ts->stat);
79338ec570SDarrick J. Wong 			return AC_ERR_OTHER;
80338ec570SDarrick J. Wong 
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;
102338ec570SDarrick J. Wong 
1031c50dc83SDarrick J. Wong 	if (!qc)
1041c50dc83SDarrick J. Wong 		goto qc_already_gone;
1051c50dc83SDarrick J. Wong 
1061c50dc83SDarrick J. Wong 	dev = qc->ap->private_data;
1073a2755afSDarrick J. Wong 	sas_ha = dev->port->ha;
1081c50dc83SDarrick J. Wong 
1093eb7a51aSDarrick J. Wong 	spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
110df64d3caSJames Bottomley 	if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD) {
111338ec570SDarrick J. Wong 		ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
112338ec570SDarrick J. Wong 		qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
113338ec570SDarrick J. Wong 		dev->sata_dev.sstatus = resp->sstatus;
114338ec570SDarrick J. Wong 		dev->sata_dev.serror = resp->serror;
115338ec570SDarrick J. Wong 		dev->sata_dev.scontrol = resp->scontrol;
116338ec570SDarrick J. Wong 	} else if (stat->stat != SAM_STAT_GOOD) {
117338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
118338ec570SDarrick J. Wong 		if (ac) {
119cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: SAS error %x\n", __func__,
120338ec570SDarrick J. Wong 				    stat->stat);
121338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
122338ec570SDarrick J. Wong 			qc->err_mask = ac;
123338ec570SDarrick J. Wong 			dev->sata_dev.tf.feature = 0x04; /* status err */
124338ec570SDarrick J. Wong 			dev->sata_dev.tf.command = ATA_ERR;
125338ec570SDarrick J. Wong 		}
126338ec570SDarrick J. Wong 	}
127338ec570SDarrick J. Wong 
1281c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
129fe059f12SDarrick J. Wong 	if (qc->scsicmd)
130fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
131338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1323eb7a51aSDarrick J. Wong 	spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
1333eb7a51aSDarrick J. Wong 
1343a2755afSDarrick J. Wong 	/*
1353a2755afSDarrick J. Wong 	 * If the sas_task has an ata qc, a scsi_cmnd and the aborted
1363a2755afSDarrick J. Wong 	 * flag is set, then we must have come in via the libsas EH
1373a2755afSDarrick J. Wong 	 * functions.  When we exit this function, we need to put the
1383a2755afSDarrick J. Wong 	 * scsi_cmnd on the list of finished errors.  The ata_qc_complete
1393a2755afSDarrick J. Wong 	 * call cleans up the libata side of things but we're protected
1403a2755afSDarrick J. Wong 	 * from the scsi_cmnd going away because the scsi_cmnd is owned
1413a2755afSDarrick J. Wong 	 * by the EH, making libata's call to scsi_done a NOP.
1423a2755afSDarrick J. Wong 	 */
1433a2755afSDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
1443a2755afSDarrick J. Wong 	if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
1453a2755afSDarrick J. Wong 		scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
1463a2755afSDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1473a2755afSDarrick J. Wong 
1481c50dc83SDarrick J. Wong qc_already_gone:
149338ec570SDarrick J. Wong 	list_del_init(&task->list);
150338ec570SDarrick J. Wong 	sas_free_task(task);
151338ec570SDarrick J. Wong }
152338ec570SDarrick J. Wong 
153338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
154338ec570SDarrick J. Wong {
15535a7f2f6SDarrick J. Wong 	int res;
156338ec570SDarrick J. Wong 	struct sas_task *task;
157338ec570SDarrick J. Wong 	struct domain_device *dev = qc->ap->private_data;
158338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
159338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
160338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
161338ec570SDarrick J. Wong 	struct scatterlist *sg;
162338ec570SDarrick J. Wong 	unsigned int xfer = 0;
163ff2aeb1eSTejun Heo 	unsigned int si;
164338ec570SDarrick J. Wong 
16556dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
16656dd2c06SDarrick J. Wong 	if (dev->gone)
16756dd2c06SDarrick J. Wong 		return AC_ERR_SYSTEM;
16856dd2c06SDarrick J. Wong 
169338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
170338ec570SDarrick J. Wong 	if (!task)
17135a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
172338ec570SDarrick J. Wong 	task->dev = dev;
173338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
174338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
175338ec570SDarrick J. Wong 
176338ec570SDarrick J. Wong 	if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
177338ec570SDarrick J. Wong 	    qc->tf.command == ATA_CMD_FPDMA_READ) {
178338ec570SDarrick J. Wong 		/* Need to zero out the tag libata assigned us */
179338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
180338ec570SDarrick J. Wong 	}
181338ec570SDarrick J. Wong 
182110dd8f1SJames Bottomley 	ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
183338ec570SDarrick J. Wong 	task->uldd_task = qc;
184405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
185338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
186dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
187dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
188338ec570SDarrick J. Wong 	} else {
189ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
190338ec570SDarrick J. Wong 			xfer += sg->length;
191338ec570SDarrick J. Wong 
192338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
193ff2aeb1eSTejun Heo 		task->num_scatter = si;
194338ec570SDarrick J. Wong 	}
195338ec570SDarrick J. Wong 
196338ec570SDarrick J. Wong 	task->data_dir = qc->dma_dir;
197ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
198338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
199338ec570SDarrick J. Wong 	task->task_state_flags = SAS_TASK_STATE_PENDING;
2001c50dc83SDarrick J. Wong 	qc->lldd_task = task;
201338ec570SDarrick J. Wong 
202338ec570SDarrick J. Wong 	switch (qc->tf.protocol) {
203338ec570SDarrick J. Wong 	case ATA_PROT_NCQ:
204338ec570SDarrick J. Wong 		task->ata_task.use_ncq = 1;
205338ec570SDarrick J. Wong 		/* fall through */
2060dc36888STejun Heo 	case ATAPI_PROT_DMA:
207338ec570SDarrick J. Wong 	case ATA_PROT_DMA:
208338ec570SDarrick J. Wong 		task->ata_task.dma_xfer = 1;
209338ec570SDarrick J. Wong 		break;
210338ec570SDarrick J. Wong 	}
211338ec570SDarrick J. Wong 
212fe059f12SDarrick J. Wong 	if (qc->scsicmd)
213fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
214fe059f12SDarrick J. Wong 
215338ec570SDarrick J. Wong 	if (sas_ha->lldd_max_execute_num < 2)
216338ec570SDarrick J. Wong 		res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
217338ec570SDarrick J. Wong 	else
218338ec570SDarrick J. Wong 		res = sas_queue_up(task);
219338ec570SDarrick J. Wong 
220338ec570SDarrick J. Wong 	/* Examine */
221338ec570SDarrick J. Wong 	if (res) {
222338ec570SDarrick J. Wong 		SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
223338ec570SDarrick J. Wong 
224fe059f12SDarrick J. Wong 		if (qc->scsicmd)
225fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
226338ec570SDarrick J. Wong 		sas_free_task(task);
22735a7f2f6SDarrick J. Wong 		return AC_ERR_SYSTEM;
228338ec570SDarrick J. Wong 	}
229338ec570SDarrick J. Wong 
23035a7f2f6SDarrick J. Wong 	return 0;
231338ec570SDarrick J. Wong }
232338ec570SDarrick J. Wong 
2334c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2344c9bf4e7STejun Heo {
2354c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2364c9bf4e7STejun Heo 
2374c9bf4e7STejun Heo 	memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
2384c9bf4e7STejun Heo 	return true;
2394c9bf4e7STejun Heo }
2404c9bf4e7STejun Heo 
241*00dd4998SJames Bottomley static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
242*00dd4998SJames Bottomley 			       unsigned long deadline)
243338ec570SDarrick J. Wong {
244*00dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
245338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
246338ec570SDarrick J. Wong 	struct sas_internal *i =
247338ec570SDarrick J. Wong 		to_sas_internal(dev->port->ha->core.shost->transportt);
248a29c0515SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
249*00dd4998SJames Bottomley 	int ret = 0;
250338ec570SDarrick J. Wong 
251338ec570SDarrick J. Wong 	if (i->dft->lldd_I_T_nexus_reset)
252338ec570SDarrick J. Wong 		res = i->dft->lldd_I_T_nexus_reset(dev);
253338ec570SDarrick J. Wong 
254*00dd4998SJames Bottomley 	if (res != TMF_RESP_FUNC_COMPLETE) {
255cadbd4a5SHarvey Harrison 		SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
256*00dd4998SJames Bottomley 		ret = -EAGAIN;
257*00dd4998SJames Bottomley 	}
258338ec570SDarrick J. Wong 
259338ec570SDarrick J. Wong 	switch (dev->sata_dev.command_set) {
260338ec570SDarrick J. Wong 		case ATA_COMMAND_SET:
261cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: Found ATA device.\n", __func__);
262*00dd4998SJames Bottomley 			*class = ATA_DEV_ATA;
263338ec570SDarrick J. Wong 			break;
264338ec570SDarrick J. Wong 		case ATAPI_COMMAND_SET:
265cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
266*00dd4998SJames Bottomley 			*class = ATA_DEV_ATAPI;
267338ec570SDarrick J. Wong 			break;
268338ec570SDarrick J. Wong 		default:
269338ec570SDarrick J. Wong 			SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
270cadbd4a5SHarvey Harrison 				    __func__,
271338ec570SDarrick J. Wong 				    dev->sata_dev.command_set);
272*00dd4998SJames Bottomley 			*class = ATA_DEV_UNKNOWN;
273338ec570SDarrick J. Wong 			break;
274338ec570SDarrick J. Wong 	}
275338ec570SDarrick J. Wong 
276338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
277*00dd4998SJames Bottomley 	return ret;
278338ec570SDarrick J. Wong }
279338ec570SDarrick J. Wong 
280338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
281338ec570SDarrick J. Wong {
282338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
283338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
284338ec570SDarrick J. Wong 
2851c50dc83SDarrick J. Wong 	if (qc->err_mask) {
2861c50dc83SDarrick J. Wong 		/*
2871c50dc83SDarrick J. Wong 		 * Find the sas_task and kill it.  By this point,
2881c50dc83SDarrick J. Wong 		 * libata has decided to kill the qc, so we needn't
2891c50dc83SDarrick J. Wong 		 * bother with sas_ata_task_done.  But we still
2901c50dc83SDarrick J. Wong 		 * ought to abort the task.
2911c50dc83SDarrick J. Wong 		 */
2921c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
2933a2755afSDarrick J. Wong 		unsigned long flags;
2941c50dc83SDarrick J. Wong 
2951c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
2961c50dc83SDarrick J. Wong 		if (task) {
2973a2755afSDarrick J. Wong 			/* Should this be a AT(API) device reset? */
2983a2755afSDarrick J. Wong 			spin_lock_irqsave(&task->task_state_lock, flags);
2993a2755afSDarrick J. Wong 			task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3003a2755afSDarrick J. Wong 			spin_unlock_irqrestore(&task->task_state_lock, flags);
3013a2755afSDarrick J. Wong 
3021c50dc83SDarrick J. Wong 			task->uldd_task = NULL;
3031c50dc83SDarrick J. Wong 			__sas_task_abort(task);
3041c50dc83SDarrick J. Wong 		}
3051c50dc83SDarrick J. Wong 	}
306338ec570SDarrick J. Wong }
307338ec570SDarrick J. Wong 
30882ef04fbSTejun Heo static int sas_ata_scr_write(struct ata_link *link, unsigned int sc_reg_in,
309338ec570SDarrick J. Wong 			      u32 val)
310338ec570SDarrick J. Wong {
31182ef04fbSTejun Heo 	struct domain_device *dev = link->ap->private_data;
312338ec570SDarrick J. Wong 
313cadbd4a5SHarvey Harrison 	SAS_DPRINTK("STUB %s\n", __func__);
314338ec570SDarrick J. Wong 	switch (sc_reg_in) {
315338ec570SDarrick J. Wong 		case SCR_STATUS:
316338ec570SDarrick J. Wong 			dev->sata_dev.sstatus = val;
317338ec570SDarrick J. Wong 			break;
318338ec570SDarrick J. Wong 		case SCR_CONTROL:
319338ec570SDarrick J. Wong 			dev->sata_dev.scontrol = val;
320338ec570SDarrick J. Wong 			break;
321338ec570SDarrick J. Wong 		case SCR_ERROR:
322338ec570SDarrick J. Wong 			dev->sata_dev.serror = val;
323338ec570SDarrick J. Wong 			break;
324338ec570SDarrick J. Wong 		case SCR_ACTIVE:
3259af5c9c9STejun Heo 			dev->sata_dev.ap->link.sactive = val;
326338ec570SDarrick J. Wong 			break;
327110dd8f1SJames Bottomley 		default:
328110dd8f1SJames Bottomley 			return -EINVAL;
329338ec570SDarrick J. Wong 	}
330110dd8f1SJames Bottomley 	return 0;
331338ec570SDarrick J. Wong }
332338ec570SDarrick J. Wong 
33382ef04fbSTejun Heo static int sas_ata_scr_read(struct ata_link *link, unsigned int sc_reg_in,
334110dd8f1SJames Bottomley 			    u32 *val)
335338ec570SDarrick J. Wong {
33682ef04fbSTejun Heo 	struct domain_device *dev = link->ap->private_data;
337338ec570SDarrick J. Wong 
338cadbd4a5SHarvey Harrison 	SAS_DPRINTK("STUB %s\n", __func__);
339338ec570SDarrick J. Wong 	switch (sc_reg_in) {
340338ec570SDarrick J. Wong 		case SCR_STATUS:
341110dd8f1SJames Bottomley 			*val = dev->sata_dev.sstatus;
342110dd8f1SJames Bottomley 			return 0;
343338ec570SDarrick J. Wong 		case SCR_CONTROL:
344110dd8f1SJames Bottomley 			*val = dev->sata_dev.scontrol;
345110dd8f1SJames Bottomley 			return 0;
346338ec570SDarrick J. Wong 		case SCR_ERROR:
347110dd8f1SJames Bottomley 			*val = dev->sata_dev.serror;
348110dd8f1SJames Bottomley 			return 0;
349338ec570SDarrick J. Wong 		case SCR_ACTIVE:
3509af5c9c9STejun Heo 			*val = dev->sata_dev.ap->link.sactive;
351110dd8f1SJames Bottomley 			return 0;
352338ec570SDarrick J. Wong 		default:
353110dd8f1SJames Bottomley 			return -EINVAL;
354338ec570SDarrick J. Wong 	}
355338ec570SDarrick J. Wong }
356338ec570SDarrick J. Wong 
357338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
358*00dd4998SJames Bottomley 	.prereset		= ata_std_prereset,
359*00dd4998SJames Bottomley 	.softreset		= NULL,
360*00dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
361*00dd4998SJames Bottomley 	.postreset		= ata_std_postreset,
362*00dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
363338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
364f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
365338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
366338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
3674c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
368338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
369338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
370338ec570SDarrick J. Wong 	.scr_read		= sas_ata_scr_read,
371338ec570SDarrick J. Wong 	.scr_write		= sas_ata_scr_write
372338ec570SDarrick J. Wong };
373338ec570SDarrick J. Wong 
374338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
375338ec570SDarrick J. Wong 	.flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
376338ec570SDarrick J. Wong 		ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
377338ec570SDarrick J. Wong 	.pio_mask = 0x1f, /* PIO0-4 */
378338ec570SDarrick J. Wong 	.mwdma_mask = 0x07, /* MWDMA0-2 */
379338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
380338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
381338ec570SDarrick J. Wong };
382338ec570SDarrick J. Wong 
383338ec570SDarrick J. Wong int sas_ata_init_host_and_port(struct domain_device *found_dev,
384338ec570SDarrick J. Wong 			       struct scsi_target *starget)
385338ec570SDarrick J. Wong {
386338ec570SDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
387338ec570SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
388338ec570SDarrick J. Wong 	struct ata_port *ap;
389338ec570SDarrick J. Wong 
390338ec570SDarrick J. Wong 	ata_host_init(&found_dev->sata_dev.ata_host,
3911d1bbee6SJeff Garzik 		      ha->dev,
392338ec570SDarrick J. Wong 		      sata_port_info.flags,
393338ec570SDarrick J. Wong 		      &sas_sata_ops);
394338ec570SDarrick J. Wong 	ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
395338ec570SDarrick J. Wong 				&sata_port_info,
396338ec570SDarrick J. Wong 				shost);
397338ec570SDarrick J. Wong 	if (!ap) {
398338ec570SDarrick J. Wong 		SAS_DPRINTK("ata_sas_port_alloc failed.\n");
399338ec570SDarrick J. Wong 		return -ENODEV;
400338ec570SDarrick J. Wong 	}
401338ec570SDarrick J. Wong 
402338ec570SDarrick J. Wong 	ap->private_data = found_dev;
403338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
404338ec570SDarrick J. Wong 	ap->scsi_host = shost;
405338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
406338ec570SDarrick J. Wong 
407338ec570SDarrick J. Wong 	return 0;
408338ec570SDarrick J. Wong }
4093a2755afSDarrick J. Wong 
4103a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
4113a2755afSDarrick J. Wong {
4123a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
4133a2755afSDarrick J. Wong 	struct completion *waiting;
4143a2755afSDarrick J. Wong 
4153a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
4163a2755afSDarrick J. Wong 	if (qc->scsicmd) {
4171b4d0d8eSJames Bottomley 		struct request_queue *q = qc->scsicmd->device->request_queue;
4181b4d0d8eSJames Bottomley 		unsigned long flags;
4191b4d0d8eSJames Bottomley 
42070b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
421242f9dcbSJens Axboe 		blk_abort_request(qc->scsicmd->request);
42270b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
4233a2755afSDarrick J. Wong 		scsi_schedule_eh(qc->scsicmd->device->host);
4243a2755afSDarrick J. Wong 		return;
4253a2755afSDarrick J. Wong 	}
4263a2755afSDarrick J. Wong 
4273a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
4283a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4293a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
4303a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
4313a2755afSDarrick J. Wong 	waiting = qc->private_data;
4323a2755afSDarrick J. Wong 	complete(waiting);
4333a2755afSDarrick J. Wong }
434b9142174SJames Bottomley 
435b9142174SJames Bottomley static void sas_task_timedout(unsigned long _task)
436b9142174SJames Bottomley {
437b9142174SJames Bottomley 	struct sas_task *task = (void *) _task;
438b9142174SJames Bottomley 	unsigned long flags;
439b9142174SJames Bottomley 
440b9142174SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
441b9142174SJames Bottomley 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
442b9142174SJames Bottomley 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
443b9142174SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
444b9142174SJames Bottomley 
445b9142174SJames Bottomley 	complete(&task->completion);
446b9142174SJames Bottomley }
447b9142174SJames Bottomley 
448b9142174SJames Bottomley static void sas_disc_task_done(struct sas_task *task)
449b9142174SJames Bottomley {
450b9142174SJames Bottomley 	if (!del_timer(&task->timer))
451b9142174SJames Bottomley 		return;
452b9142174SJames Bottomley 	complete(&task->completion);
453b9142174SJames Bottomley }
454b9142174SJames Bottomley 
455b9142174SJames Bottomley #define SAS_DEV_TIMEOUT 10
456b9142174SJames Bottomley 
457b9142174SJames Bottomley /**
458b9142174SJames Bottomley  * sas_execute_task -- Basic task processing for discovery
459b9142174SJames Bottomley  * @task: the task to be executed
460b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
461b9142174SJames Bottomley  * @size: size of @buffer
4621d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
463b9142174SJames Bottomley  */
464b9142174SJames Bottomley static int sas_execute_task(struct sas_task *task, void *buffer, int size,
4651d1bbee6SJeff Garzik 			    enum dma_data_direction dma_dir)
466b9142174SJames Bottomley {
467b9142174SJames Bottomley 	int res = 0;
468b9142174SJames Bottomley 	struct scatterlist *scatter = NULL;
469b9142174SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
470b9142174SJames Bottomley 	int num_scatter = 0;
471b9142174SJames Bottomley 	int retries = 0;
472b9142174SJames Bottomley 	struct sas_internal *i =
473b9142174SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
474b9142174SJames Bottomley 
4751d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
476b9142174SJames Bottomley 		scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
477b9142174SJames Bottomley 		if (!scatter)
478b9142174SJames Bottomley 			goto out;
479b9142174SJames Bottomley 
480b9142174SJames Bottomley 		sg_init_one(scatter, buffer, size);
481b9142174SJames Bottomley 		num_scatter = 1;
482b9142174SJames Bottomley 	}
483b9142174SJames Bottomley 
484b9142174SJames Bottomley 	task->task_proto = task->dev->tproto;
485b9142174SJames Bottomley 	task->scatter = scatter;
486b9142174SJames Bottomley 	task->num_scatter = num_scatter;
487b9142174SJames Bottomley 	task->total_xfer_len = size;
4881d1bbee6SJeff Garzik 	task->data_dir = dma_dir;
489b9142174SJames Bottomley 	task->task_done = sas_disc_task_done;
4901d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE &&
491b9142174SJames Bottomley 	    sas_protocol_ata(task->task_proto)) {
4921d1bbee6SJeff Garzik 		task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
493b9142174SJames Bottomley 					       task->scatter,
494b9142174SJames Bottomley 					       task->num_scatter,
495b9142174SJames Bottomley 					       task->data_dir);
496b9142174SJames Bottomley 	}
497b9142174SJames Bottomley 
498b9142174SJames Bottomley 	for (retries = 0; retries < 5; retries++) {
499b9142174SJames Bottomley 		task->task_state_flags = SAS_TASK_STATE_PENDING;
500b9142174SJames Bottomley 		init_completion(&task->completion);
501b9142174SJames Bottomley 
502b9142174SJames Bottomley 		task->timer.data = (unsigned long) task;
503b9142174SJames Bottomley 		task->timer.function = sas_task_timedout;
504b9142174SJames Bottomley 		task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
505b9142174SJames Bottomley 		add_timer(&task->timer);
506b9142174SJames Bottomley 
507b9142174SJames Bottomley 		res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
508b9142174SJames Bottomley 		if (res) {
509b9142174SJames Bottomley 			del_timer(&task->timer);
510b9142174SJames Bottomley 			SAS_DPRINTK("executing SAS discovery task failed:%d\n",
511b9142174SJames Bottomley 				    res);
512b9142174SJames Bottomley 			goto ex_err;
513b9142174SJames Bottomley 		}
514b9142174SJames Bottomley 		wait_for_completion(&task->completion);
51532e8ae36SJames Bottomley 		res = -ECOMM;
516b9142174SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
517b9142174SJames Bottomley 			int res2;
518b9142174SJames Bottomley 			SAS_DPRINTK("task aborted, flags:0x%x\n",
519b9142174SJames Bottomley 				    task->task_state_flags);
520b9142174SJames Bottomley 			res2 = i->dft->lldd_abort_task(task);
521b9142174SJames Bottomley 			SAS_DPRINTK("came back from abort task\n");
522b9142174SJames Bottomley 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
523b9142174SJames Bottomley 				if (res2 == TMF_RESP_FUNC_COMPLETE)
524b9142174SJames Bottomley 					continue; /* Retry the task */
525b9142174SJames Bottomley 				else
526b9142174SJames Bottomley 					goto ex_err;
527b9142174SJames Bottomley 			}
528b9142174SJames Bottomley 		}
529df64d3caSJames Bottomley 		if (task->task_status.stat == SAM_STAT_BUSY ||
530df64d3caSJames Bottomley 			   task->task_status.stat == SAM_STAT_TASK_SET_FULL ||
531b9142174SJames Bottomley 			   task->task_status.stat == SAS_QUEUE_FULL) {
532b9142174SJames Bottomley 			SAS_DPRINTK("task: q busy, sleeping...\n");
533b9142174SJames Bottomley 			schedule_timeout_interruptible(HZ);
534df64d3caSJames Bottomley 		} else if (task->task_status.stat == SAM_STAT_CHECK_CONDITION) {
535b9142174SJames Bottomley 			struct scsi_sense_hdr shdr;
536b9142174SJames Bottomley 
537b9142174SJames Bottomley 			if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
538b9142174SJames Bottomley 						  &shdr)) {
539b9142174SJames Bottomley 				SAS_DPRINTK("couldn't normalize sense\n");
540b9142174SJames Bottomley 				continue;
541b9142174SJames Bottomley 			}
542b9142174SJames Bottomley 			if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
543b9142174SJames Bottomley 			    (shdr.sense_key == 2 && shdr.asc == 4 &&
544b9142174SJames Bottomley 			     shdr.ascq == 1)) {
545b9142174SJames Bottomley 				SAS_DPRINTK("device %016llx LUN: %016llx "
546b9142174SJames Bottomley 					    "powering up or not ready yet, "
547b9142174SJames Bottomley 					    "sleeping...\n",
548b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
549b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN));
550b9142174SJames Bottomley 
551b9142174SJames Bottomley 				schedule_timeout_interruptible(5*HZ);
552b9142174SJames Bottomley 			} else if (shdr.sense_key == 1) {
553b9142174SJames Bottomley 				res = 0;
554b9142174SJames Bottomley 				break;
555b9142174SJames Bottomley 			} else if (shdr.sense_key == 5) {
556b9142174SJames Bottomley 				break;
557b9142174SJames Bottomley 			} else {
558b9142174SJames Bottomley 				SAS_DPRINTK("dev %016llx LUN: %016llx "
559b9142174SJames Bottomley 					    "sense key:0x%x ASC:0x%x ASCQ:0x%x"
560b9142174SJames Bottomley 					    "\n",
561b9142174SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr),
562b9142174SJames Bottomley 					    SAS_ADDR(task->ssp_task.LUN),
563b9142174SJames Bottomley 					    shdr.sense_key,
564b9142174SJames Bottomley 					    shdr.asc, shdr.ascq);
565b9142174SJames Bottomley 			}
566b9142174SJames Bottomley 		} else if (task->task_status.resp != SAS_TASK_COMPLETE ||
567df64d3caSJames Bottomley 			   task->task_status.stat != SAM_STAT_GOOD) {
568b9142174SJames Bottomley 			SAS_DPRINTK("task finished with resp:0x%x, "
569b9142174SJames Bottomley 				    "stat:0x%x\n",
570b9142174SJames Bottomley 				    task->task_status.resp,
571b9142174SJames Bottomley 				    task->task_status.stat);
572b9142174SJames Bottomley 			goto ex_err;
573b9142174SJames Bottomley 		} else {
574b9142174SJames Bottomley 			res = 0;
575b9142174SJames Bottomley 			break;
576b9142174SJames Bottomley 		}
577b9142174SJames Bottomley 	}
578b9142174SJames Bottomley ex_err:
5791d1bbee6SJeff Garzik 	if (dma_dir != DMA_NONE) {
580b9142174SJames Bottomley 		if (sas_protocol_ata(task->task_proto))
5811d1bbee6SJeff Garzik 			dma_unmap_sg(task->dev->port->ha->dev,
582b9142174SJames Bottomley 				     task->scatter, task->num_scatter,
583b9142174SJames Bottomley 				     task->data_dir);
584b9142174SJames Bottomley 		kfree(scatter);
585b9142174SJames Bottomley 	}
586b9142174SJames Bottomley out:
587b9142174SJames Bottomley 	return res;
588b9142174SJames Bottomley }
589b9142174SJames Bottomley 
590b9142174SJames Bottomley /* ---------- SATA ---------- */
591b9142174SJames Bottomley 
592b9142174SJames Bottomley static void sas_get_ata_command_set(struct domain_device *dev)
593b9142174SJames Bottomley {
594b9142174SJames Bottomley 	struct dev_to_host_fis *fis =
595b9142174SJames Bottomley 		(struct dev_to_host_fis *) dev->frame_rcvd;
596b9142174SJames Bottomley 
597b9142174SJames Bottomley 	if ((fis->sector_count == 1 && /* ATA */
598b9142174SJames Bottomley 	     fis->lbal         == 1 &&
599b9142174SJames Bottomley 	     fis->lbam         == 0 &&
600b9142174SJames Bottomley 	     fis->lbah         == 0 &&
601b9142174SJames Bottomley 	     fis->device       == 0)
602b9142174SJames Bottomley 	    ||
603b9142174SJames Bottomley 	    (fis->sector_count == 0 && /* CE-ATA (mATA) */
604b9142174SJames Bottomley 	     fis->lbal         == 0 &&
605b9142174SJames Bottomley 	     fis->lbam         == 0xCE &&
606b9142174SJames Bottomley 	     fis->lbah         == 0xAA &&
607b9142174SJames Bottomley 	     (fis->device & ~0x10) == 0))
608b9142174SJames Bottomley 
609b9142174SJames Bottomley 		dev->sata_dev.command_set = ATA_COMMAND_SET;
610b9142174SJames Bottomley 
611b9142174SJames Bottomley 	else if ((fis->interrupt_reason == 1 &&	/* ATAPI */
612b9142174SJames Bottomley 		  fis->lbal             == 1 &&
613b9142174SJames Bottomley 		  fis->byte_count_low   == 0x14 &&
614b9142174SJames Bottomley 		  fis->byte_count_high  == 0xEB &&
615b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
616b9142174SJames Bottomley 
617b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
618b9142174SJames Bottomley 
619b9142174SJames Bottomley 	else if ((fis->sector_count == 1 && /* SEMB */
620b9142174SJames Bottomley 		  fis->lbal         == 1 &&
621b9142174SJames Bottomley 		  fis->lbam         == 0x3C &&
622b9142174SJames Bottomley 		  fis->lbah         == 0xC3 &&
623b9142174SJames Bottomley 		  fis->device       == 0)
624b9142174SJames Bottomley 		||
625b9142174SJames Bottomley 		 (fis->interrupt_reason == 1 &&	/* SATA PM */
626b9142174SJames Bottomley 		  fis->lbal             == 1 &&
627b9142174SJames Bottomley 		  fis->byte_count_low   == 0x69 &&
628b9142174SJames Bottomley 		  fis->byte_count_high  == 0x96 &&
629b9142174SJames Bottomley 		  (fis->device & ~0x10) == 0))
630b9142174SJames Bottomley 
631b9142174SJames Bottomley 		/* Treat it as a superset? */
632b9142174SJames Bottomley 		dev->sata_dev.command_set = ATAPI_COMMAND_SET;
633b9142174SJames Bottomley }
634b9142174SJames Bottomley 
635b9142174SJames Bottomley /**
636b9142174SJames Bottomley  * sas_issue_ata_cmd -- Basic SATA command processing for discovery
637b9142174SJames Bottomley  * @dev: the device to send the command to
638b9142174SJames Bottomley  * @command: the command register
639b9142174SJames Bottomley  * @features: the features register
640b9142174SJames Bottomley  * @buffer: pointer to buffer to do I/O
641b9142174SJames Bottomley  * @size: size of @buffer
6421d1bbee6SJeff Garzik  * @dma_dir: DMA direction.  DMA_xxx
643b9142174SJames Bottomley  */
644b9142174SJames Bottomley static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
645b9142174SJames Bottomley 			     u8 features, void *buffer, int size,
6461d1bbee6SJeff Garzik 			     enum dma_data_direction dma_dir)
647b9142174SJames Bottomley {
648b9142174SJames Bottomley 	int res = 0;
649b9142174SJames Bottomley 	struct sas_task *task;
650b9142174SJames Bottomley 	struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
651b9142174SJames Bottomley 		&dev->frame_rcvd[0];
652b9142174SJames Bottomley 
653b9142174SJames Bottomley 	res = -ENOMEM;
654b9142174SJames Bottomley 	task = sas_alloc_task(GFP_KERNEL);
655b9142174SJames Bottomley 	if (!task)
656b9142174SJames Bottomley 		goto out;
657b9142174SJames Bottomley 
658b9142174SJames Bottomley 	task->dev = dev;
659b9142174SJames Bottomley 
660b9142174SJames Bottomley 	task->ata_task.fis.fis_type = 0x27;
661b9142174SJames Bottomley 	task->ata_task.fis.command = command;
662b9142174SJames Bottomley 	task->ata_task.fis.features = features;
663b9142174SJames Bottomley 	task->ata_task.fis.device = d2h_fis->device;
664b9142174SJames Bottomley 	task->ata_task.retry_count = 1;
665b9142174SJames Bottomley 
6661d1bbee6SJeff Garzik 	res = sas_execute_task(task, buffer, size, dma_dir);
667b9142174SJames Bottomley 
668b9142174SJames Bottomley 	sas_free_task(task);
669b9142174SJames Bottomley out:
670b9142174SJames Bottomley 	return res;
671b9142174SJames Bottomley }
672b9142174SJames Bottomley 
673b9142174SJames Bottomley #define ATA_IDENTIFY_DEV         0xEC
674b9142174SJames Bottomley #define ATA_IDENTIFY_PACKET_DEV  0xA1
675b9142174SJames Bottomley #define ATA_SET_FEATURES         0xEF
676b9142174SJames Bottomley #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
677b9142174SJames Bottomley 
678b9142174SJames Bottomley /**
679b9142174SJames Bottomley  * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
680b9142174SJames Bottomley  * @dev: STP/SATA device of interest (ATA/ATAPI)
681b9142174SJames Bottomley  *
682b9142174SJames Bottomley  * The LLDD has already been notified of this device, so that we can
683b9142174SJames Bottomley  * send FISes to it.  Here we try to get IDENTIFY DEVICE or IDENTIFY
684b9142174SJames Bottomley  * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
685b9142174SJames Bottomley  * performance for this device.
686b9142174SJames Bottomley  */
687b9142174SJames Bottomley static int sas_discover_sata_dev(struct domain_device *dev)
688b9142174SJames Bottomley {
689b9142174SJames Bottomley 	int     res;
690b9142174SJames Bottomley 	__le16  *identify_x;
691b9142174SJames Bottomley 	u8      command;
692b9142174SJames Bottomley 
693b9142174SJames Bottomley 	identify_x = kzalloc(512, GFP_KERNEL);
694b9142174SJames Bottomley 	if (!identify_x)
695b9142174SJames Bottomley 		return -ENOMEM;
696b9142174SJames Bottomley 
697b9142174SJames Bottomley 	if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
698b9142174SJames Bottomley 		dev->sata_dev.identify_device = identify_x;
699b9142174SJames Bottomley 		command = ATA_IDENTIFY_DEV;
700b9142174SJames Bottomley 	} else {
701b9142174SJames Bottomley 		dev->sata_dev.identify_packet_device = identify_x;
702b9142174SJames Bottomley 		command = ATA_IDENTIFY_PACKET_DEV;
703b9142174SJames Bottomley 	}
704b9142174SJames Bottomley 
705b9142174SJames Bottomley 	res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
7061d1bbee6SJeff Garzik 				DMA_FROM_DEVICE);
707b9142174SJames Bottomley 	if (res)
708b9142174SJames Bottomley 		goto out_err;
709b9142174SJames Bottomley 
710b9142174SJames Bottomley 	/* lives on the media? */
711b9142174SJames Bottomley 	if (le16_to_cpu(identify_x[0]) & 4) {
712b9142174SJames Bottomley 		/* incomplete response */
713b9142174SJames Bottomley 		SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
714b9142174SJames Bottomley 			    "dev %llx\n", SAS_ADDR(dev->sas_addr));
71517b7a8deSAl Viro 		if (!(identify_x[83] & cpu_to_le16(1<<6)))
716b9142174SJames Bottomley 			goto cont1;
717b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
718b9142174SJames Bottomley 					ATA_FEATURE_PUP_STBY_SPIN_UP,
7191d1bbee6SJeff Garzik 					NULL, 0, DMA_NONE);
720b9142174SJames Bottomley 		if (res)
721b9142174SJames Bottomley 			goto cont1;
722b9142174SJames Bottomley 
723b9142174SJames Bottomley 		schedule_timeout_interruptible(5*HZ); /* More time? */
724b9142174SJames Bottomley 		res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
7251d1bbee6SJeff Garzik 					DMA_FROM_DEVICE);
726b9142174SJames Bottomley 		if (res)
727b9142174SJames Bottomley 			goto out_err;
728b9142174SJames Bottomley 	}
729b9142174SJames Bottomley cont1:
730b9142174SJames Bottomley 	/* XXX Hint: register this SATA device with SATL.
731b9142174SJames Bottomley 	   When this returns, dev->sata_dev->lu is alive and
732b9142174SJames Bottomley 	   present.
733b9142174SJames Bottomley 	sas_satl_register_dev(dev);
734b9142174SJames Bottomley 	*/
735b9142174SJames Bottomley 
736b9142174SJames Bottomley 	sas_fill_in_rphy(dev, dev->rphy);
737b9142174SJames Bottomley 
738b9142174SJames Bottomley 	return 0;
739b9142174SJames Bottomley out_err:
740b9142174SJames Bottomley 	dev->sata_dev.identify_packet_device = NULL;
741b9142174SJames Bottomley 	dev->sata_dev.identify_device = NULL;
742b9142174SJames Bottomley 	kfree(identify_x);
743b9142174SJames Bottomley 	return res;
744b9142174SJames Bottomley }
745b9142174SJames Bottomley 
746b9142174SJames Bottomley static int sas_discover_sata_pm(struct domain_device *dev)
747b9142174SJames Bottomley {
748b9142174SJames Bottomley 	return -ENODEV;
749b9142174SJames Bottomley }
750b9142174SJames Bottomley 
751b9142174SJames Bottomley /**
752b9142174SJames Bottomley  * sas_discover_sata -- discover an STP/SATA domain device
753b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
754b9142174SJames Bottomley  *
755b9142174SJames Bottomley  * First we notify the LLDD of this device, so we can send frames to
756b9142174SJames Bottomley  * it.  Then depending on the type of device we call the appropriate
757b9142174SJames Bottomley  * discover functions.  Once device discover is done, we notify the
758b9142174SJames Bottomley  * LLDD so that it can fine-tune its parameters for the device, by
759b9142174SJames Bottomley  * removing it and then adding it.  That is, the second time around,
760b9142174SJames Bottomley  * the driver would have certain fields, that it is looking at, set.
761b9142174SJames Bottomley  * Finally we initialize the kobj so that the device can be added to
762b9142174SJames Bottomley  * the system at registration time.  Devices directly attached to a HA
763b9142174SJames Bottomley  * port, have no parents.  All other devices do, and should have their
764b9142174SJames Bottomley  * "parent" pointer set appropriately before calling this function.
765b9142174SJames Bottomley  */
766b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
767b9142174SJames Bottomley {
768b9142174SJames Bottomley 	int res;
769b9142174SJames Bottomley 
770b9142174SJames Bottomley 	sas_get_ata_command_set(dev);
771b9142174SJames Bottomley 
772b9142174SJames Bottomley 	res = sas_notify_lldd_dev_found(dev);
773b9142174SJames Bottomley 	if (res)
774b9142174SJames Bottomley 		return res;
775b9142174SJames Bottomley 
776b9142174SJames Bottomley 	switch (dev->dev_type) {
777b9142174SJames Bottomley 	case SATA_DEV:
778b9142174SJames Bottomley 		res = sas_discover_sata_dev(dev);
779b9142174SJames Bottomley 		break;
780b9142174SJames Bottomley 	case SATA_PM:
781b9142174SJames Bottomley 		res = sas_discover_sata_pm(dev);
782b9142174SJames Bottomley 		break;
783b9142174SJames Bottomley 	default:
784b9142174SJames Bottomley 		break;
785b9142174SJames Bottomley 	}
786b9142174SJames Bottomley 	sas_notify_lldd_dev_gone(dev);
787b9142174SJames Bottomley 	if (!res) {
788b9142174SJames Bottomley 		sas_notify_lldd_dev_found(dev);
789b9142174SJames Bottomley 		res = sas_rphy_add(dev->rphy);
790b9142174SJames Bottomley 	}
791b9142174SJames Bottomley 
792b9142174SJames Bottomley 	return res;
793b9142174SJames Bottomley }
794*00dd4998SJames Bottomley 
795*00dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
796*00dd4998SJames Bottomley {
797*00dd4998SJames Bottomley 	struct scsi_device *sdev;
798*00dd4998SJames Bottomley 
799*00dd4998SJames Bottomley 	shost_for_each_device(sdev, shost) {
800*00dd4998SJames Bottomley 		struct domain_device *ddev = sdev_to_domain_dev(sdev);
801*00dd4998SJames Bottomley 		struct ata_port *ap = ddev->sata_dev.ap;
802*00dd4998SJames Bottomley 
803*00dd4998SJames Bottomley 		if (!dev_is_sata(ddev))
804*00dd4998SJames Bottomley 			continue;
805*00dd4998SJames Bottomley 
806*00dd4998SJames Bottomley 		ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
807*00dd4998SJames Bottomley 		ata_scsi_port_error_handler(shost, ap);
808*00dd4998SJames Bottomley 	}
809*00dd4998SJames Bottomley }
810*00dd4998SJames Bottomley 
811*00dd4998SJames Bottomley int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,
812*00dd4998SJames Bottomley 		      enum blk_eh_timer_return *rtn)
813*00dd4998SJames Bottomley {
814*00dd4998SJames Bottomley 	struct domain_device *ddev = cmd_to_domain_dev(cmd);
815*00dd4998SJames Bottomley 
816*00dd4998SJames Bottomley 	if (!dev_is_sata(ddev) || task)
817*00dd4998SJames Bottomley 		return 0;
818*00dd4998SJames Bottomley 
819*00dd4998SJames Bottomley 	/* we're a sata device with no task, so this must be a libata
820*00dd4998SJames Bottomley 	 * eh timeout.  Ideally should hook into libata timeout
821*00dd4998SJames Bottomley 	 * handling, but there's no point, it just wants to activate
822*00dd4998SJames Bottomley 	 * the eh thread */
823*00dd4998SJames Bottomley 	*rtn = BLK_EH_NOT_HANDLED;
824*00dd4998SJames Bottomley 	return 1;
825*00dd4998SJames Bottomley }
826*00dd4998SJames Bottomley 
827*00dd4998SJames Bottomley int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
828*00dd4998SJames Bottomley 	       struct list_head *done_q)
829*00dd4998SJames Bottomley {
830*00dd4998SJames Bottomley 	int rtn = 0;
831*00dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
832*00dd4998SJames Bottomley 	struct ata_port *ap;
833*00dd4998SJames Bottomley 
834*00dd4998SJames Bottomley 	do {
835*00dd4998SJames Bottomley 		LIST_HEAD(sata_q);
836*00dd4998SJames Bottomley 
837*00dd4998SJames Bottomley 		ap = NULL;
838*00dd4998SJames Bottomley 
839*00dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
840*00dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
841*00dd4998SJames Bottomley 
842*00dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
843*00dd4998SJames Bottomley 				continue;
844*00dd4998SJames Bottomley 			if(ap && ap != ddev->sata_dev.ap)
845*00dd4998SJames Bottomley 				continue;
846*00dd4998SJames Bottomley 			ap = ddev->sata_dev.ap;
847*00dd4998SJames Bottomley 			rtn = 1;
848*00dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
849*00dd4998SJames Bottomley 		}
850*00dd4998SJames Bottomley 
851*00dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
852*00dd4998SJames Bottomley 			ata_port_printk(ap, KERN_DEBUG,"sas eh calling libata cmd error handler\n");
853*00dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
854*00dd4998SJames Bottomley 		}
855*00dd4998SJames Bottomley 	} while (ap);
856*00dd4998SJames Bottomley 
857*00dd4998SJames Bottomley 	return rtn;
858*00dd4998SJames Bottomley }
859