blob: 5cb0a2ae5924a62a574c38af49060ee0fe76b44b [file] [log] [blame]
Darrick J. Wong338ec572006-10-18 14:43:37 -07001/*
2 * Support for SATA devices on Serial Attached SCSI (SAS) controllers
3 *
4 * Copyright (C) 2006 IBM Corporation
5 *
6 * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 */
23
James Bottomleyb9142172007-07-22 13:15:55 -050024#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
James Bottomleyb9142172007-07-22 13:15:55 -050026
Darrick J. Wong338ec572006-10-18 14:43:37 -070027#include <scsi/sas_ata.h>
28#include "sas_internal.h"
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_tcq.h>
32#include <scsi/scsi.h>
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_sas.h>
35#include "../scsi_sas_internal.h"
Darrick J. Wong3a2755a2007-01-30 01:18:58 -080036#include "../scsi_transport_api.h"
37#include <scsi/scsi_eh.h>
Darrick J. Wong338ec572006-10-18 14:43:37 -070038
39static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
40{
41 /* Cheesy attempt to translate SAS errors into ATA. Hah! */
42
43 /* transport error */
44 if (ts->resp == SAS_TASK_UNDELIVERED)
45 return AC_ERR_ATA_BUS;
46
47 /* ts->resp == SAS_TASK_COMPLETE */
48 /* task delivered, what happened afterwards? */
49 switch (ts->stat) {
50 case SAS_DEV_NO_RESPONSE:
51 return AC_ERR_TIMEOUT;
52
53 case SAS_INTERRUPTED:
54 case SAS_PHY_DOWN:
55 case SAS_NAK_R_ERR:
56 return AC_ERR_ATA_BUS;
57
58
59 case SAS_DATA_UNDERRUN:
60 /*
61 * Some programs that use the taskfile interface
62 * (smartctl in particular) can cause underrun
63 * problems. Ignore these errors, perhaps at our
64 * peril.
65 */
66 return 0;
67
68 case SAS_DATA_OVERRUN:
69 case SAS_QUEUE_FULL:
70 case SAS_DEVICE_UNKNOWN:
71 case SAS_SG_ERR:
72 return AC_ERR_INVALID;
73
Darrick J. Wong338ec572006-10-18 14:43:37 -070074 case SAS_OPEN_TO:
75 case SAS_OPEN_REJECT:
76 SAS_DPRINTK("%s: Saw error %d. What to do?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070077 __func__, ts->stat);
Darrick J. Wong338ec572006-10-18 14:43:37 -070078 return AC_ERR_OTHER;
79
James Bottomley75c0b382011-01-23 08:16:24 -060080 case SAM_STAT_CHECK_CONDITION:
Darrick J. Wong338ec572006-10-18 14:43:37 -070081 case SAS_ABORTED_TASK:
82 return AC_ERR_DEV;
83
84 case SAS_PROTO_RESPONSE:
85 /* This means the ending_fis has the error
86 * value; return 0 here to collect it */
87 return 0;
88 default:
89 return 0;
90 }
91}
92
93static void sas_ata_task_done(struct sas_task *task)
94{
95 struct ata_queued_cmd *qc = task->uldd_task;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -080096 struct domain_device *dev;
Darrick J. Wong338ec572006-10-18 14:43:37 -070097 struct task_status_struct *stat = &task->task_status;
98 struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -080099 struct sas_ha_struct *sas_ha;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700100 enum ata_completion_errors ac;
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800101 unsigned long flags;
Xiangliang Yubb650a12011-05-08 19:27:01 +0800102 struct ata_link *link;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700103
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800104 if (!qc)
105 goto qc_already_gone;
106
107 dev = qc->ap->private_data;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800108 sas_ha = dev->port->ha;
Xiangliang Yubb650a12011-05-08 19:27:01 +0800109 link = &dev->sata_dev.ap->link;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800110
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800111 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
James Bottomley75c0b382011-01-23 08:16:24 -0600112 if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
113 ((stat->stat == SAM_STAT_CHECK_CONDITION &&
114 dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700115 ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
Xiangliang Yubb650a12011-05-08 19:27:01 +0800116
117 if (!link->sactive) {
118 qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
119 } else {
120 link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
121 if (unlikely(link->eh_info.err_mask))
122 qc->flags |= ATA_QCFLAG_FAILED;
123 }
James Bottomley75c0b382011-01-23 08:16:24 -0600124 } else {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700125 ac = sas_to_ata_err(stat);
126 if (ac) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700127 SAS_DPRINTK("%s: SAS error %x\n", __func__,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700128 stat->stat);
129 /* We saw a SAS error. Send a vague error. */
Xiangliang Yubb650a12011-05-08 19:27:01 +0800130 if (!link->sactive) {
131 qc->err_mask = ac;
132 } else {
133 link->eh_info.err_mask |= AC_ERR_DEV;
134 qc->flags |= ATA_QCFLAG_FAILED;
135 }
136
Darrick J. Wong338ec572006-10-18 14:43:37 -0700137 dev->sata_dev.tf.feature = 0x04; /* status err */
138 dev->sata_dev.tf.command = ATA_ERR;
139 }
140 }
141
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800142 qc->lldd_task = NULL;
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800143 if (qc->scsicmd)
144 ASSIGN_SAS_TASK(qc->scsicmd, NULL);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700145 ata_qc_complete(qc);
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800146 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
147
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800148qc_already_gone:
Darrick J. Wong338ec572006-10-18 14:43:37 -0700149 list_del_init(&task->list);
150 sas_free_task(task);
151}
152
153static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
154{
Dan Williams312d3e52011-11-17 17:59:50 -0800155 unsigned long flags;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700156 struct sas_task *task;
Dan Williams312d3e52011-11-17 17:59:50 -0800157 struct scatterlist *sg;
158 int ret = AC_ERR_SYSTEM;
159 unsigned int si, xfer = 0;
160 struct ata_port *ap = qc->ap;
161 struct domain_device *dev = ap->private_data;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700162 struct sas_ha_struct *sas_ha = dev->port->ha;
163 struct Scsi_Host *host = sas_ha->core.shost;
164 struct sas_internal *i = to_sas_internal(host->transportt);
Dan Williams312d3e52011-11-17 17:59:50 -0800165
166 /* TODO: audit callers to ensure they are ready for qc_issue to
167 * unconditionally re-enable interrupts
168 */
169 local_irq_save(flags);
170 spin_unlock(ap->lock);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700171
Darrick J. Wong56dd2c02010-10-01 13:55:47 -0700172 /* If the device fell off, no sense in issuing commands */
Dan Williamse1399422012-01-07 08:52:39 +0000173 if (test_bit(SAS_DEV_GONE, &dev->state))
Dan Williams312d3e52011-11-17 17:59:50 -0800174 goto out;
Darrick J. Wong56dd2c02010-10-01 13:55:47 -0700175
Darrick J. Wong338ec572006-10-18 14:43:37 -0700176 task = sas_alloc_task(GFP_ATOMIC);
177 if (!task)
Dan Williams312d3e52011-11-17 17:59:50 -0800178 goto out;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700179 task->dev = dev;
180 task->task_proto = SAS_PROTOCOL_STP;
181 task->task_done = sas_ata_task_done;
182
183 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
184 qc->tf.command == ATA_CMD_FPDMA_READ) {
185 /* Need to zero out the tag libata assigned us */
186 qc->tf.nsect = 0;
187 }
188
James Bottomley110dd8f2007-07-20 13:11:44 -0500189 ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700190 task->uldd_task = qc;
Tejun Heo405e66b2007-11-27 19:28:53 +0900191 if (ata_is_atapi(qc->tf.protocol)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700192 memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
James Bottomleydde20202008-02-19 11:36:56 +0100193 task->total_xfer_len = qc->nbytes;
194 task->num_scatter = qc->n_elem;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700195 } else {
Tejun Heoff2aeb12007-12-05 16:43:11 +0900196 for_each_sg(qc->sg, sg, qc->n_elem, si)
Darrick J. Wong338ec572006-10-18 14:43:37 -0700197 xfer += sg->length;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700198
199 task->total_xfer_len = xfer;
Tejun Heoff2aeb12007-12-05 16:43:11 +0900200 task->num_scatter = si;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700201 }
202
203 task->data_dir = qc->dma_dir;
Tejun Heoff2aeb12007-12-05 16:43:11 +0900204 task->scatter = qc->sg;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700205 task->ata_task.retry_count = 1;
206 task->task_state_flags = SAS_TASK_STATE_PENDING;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800207 qc->lldd_task = task;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700208
209 switch (qc->tf.protocol) {
210 case ATA_PROT_NCQ:
211 task->ata_task.use_ncq = 1;
212 /* fall through */
Tejun Heo0dc36882007-12-18 16:34:43 -0500213 case ATAPI_PROT_DMA:
Darrick J. Wong338ec572006-10-18 14:43:37 -0700214 case ATA_PROT_DMA:
215 task->ata_task.dma_xfer = 1;
216 break;
217 }
218
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800219 if (qc->scsicmd)
220 ASSIGN_SAS_TASK(qc->scsicmd, task);
221
Darrick J. Wong338ec572006-10-18 14:43:37 -0700222 if (sas_ha->lldd_max_execute_num < 2)
Dan Williams312d3e52011-11-17 17:59:50 -0800223 ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700224 else
Dan Williams312d3e52011-11-17 17:59:50 -0800225 ret = sas_queue_up(task);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700226
227 /* Examine */
Dan Williams312d3e52011-11-17 17:59:50 -0800228 if (ret) {
229 SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700230
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800231 if (qc->scsicmd)
232 ASSIGN_SAS_TASK(qc->scsicmd, NULL);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700233 sas_free_task(task);
Dan Williams312d3e52011-11-17 17:59:50 -0800234 ret = AC_ERR_SYSTEM;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700235 }
236
Dan Williams312d3e52011-11-17 17:59:50 -0800237 out:
238 spin_lock(ap->lock);
239 local_irq_restore(flags);
240 return ret;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700241}
242
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900243static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
244{
245 struct domain_device *dev = qc->ap->private_data;
246
247 memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
248 return true;
249}
250
James Bottomley00dd4992011-01-23 09:44:12 -0600251static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
252 unsigned long deadline)
Darrick J. Wong338ec572006-10-18 14:43:37 -0700253{
James Bottomley00dd4992011-01-23 09:44:12 -0600254 struct ata_port *ap = link->ap;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700255 struct domain_device *dev = ap->private_data;
256 struct sas_internal *i =
257 to_sas_internal(dev->port->ha->core.shost->transportt);
James Bottomleya29c0512008-02-23 23:38:44 -0600258 int res = TMF_RESP_FUNC_FAILED;
James Bottomley00dd4992011-01-23 09:44:12 -0600259 int ret = 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700260
261 if (i->dft->lldd_I_T_nexus_reset)
262 res = i->dft->lldd_I_T_nexus_reset(dev);
263
James Bottomley00dd4992011-01-23 09:44:12 -0600264 if (res != TMF_RESP_FUNC_COMPLETE) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700265 SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
James Bottomley00dd4992011-01-23 09:44:12 -0600266 ret = -EAGAIN;
267 }
Darrick J. Wong338ec572006-10-18 14:43:37 -0700268
269 switch (dev->sata_dev.command_set) {
270 case ATA_COMMAND_SET:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700271 SAS_DPRINTK("%s: Found ATA device.\n", __func__);
James Bottomley00dd4992011-01-23 09:44:12 -0600272 *class = ATA_DEV_ATA;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700273 break;
274 case ATAPI_COMMAND_SET:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700275 SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
James Bottomley00dd4992011-01-23 09:44:12 -0600276 *class = ATA_DEV_ATAPI;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700277 break;
278 default:
279 SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700280 __func__,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700281 dev->sata_dev.command_set);
James Bottomley00dd4992011-01-23 09:44:12 -0600282 *class = ATA_DEV_UNKNOWN;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700283 break;
284 }
285
286 ap->cbl = ATA_CBL_SATA;
James Bottomley00dd4992011-01-23 09:44:12 -0600287 return ret;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700288}
289
Dave Jiang1ca1e432011-05-24 13:18:04 -0700290static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
291 unsigned long deadline)
292{
293 struct ata_port *ap = link->ap;
294 struct domain_device *dev = ap->private_data;
295 struct sas_internal *i =
296 to_sas_internal(dev->port->ha->core.shost->transportt);
297 int res = TMF_RESP_FUNC_FAILED;
298 int ret = 0;
299
300 if (i->dft->lldd_ata_soft_reset)
301 res = i->dft->lldd_ata_soft_reset(dev);
302
303 if (res != TMF_RESP_FUNC_COMPLETE) {
304 SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
305 ret = -EAGAIN;
306 }
307
308 switch (dev->sata_dev.command_set) {
309 case ATA_COMMAND_SET:
310 SAS_DPRINTK("%s: Found ATA device.\n", __func__);
311 *class = ATA_DEV_ATA;
312 break;
313 case ATAPI_COMMAND_SET:
314 SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
315 *class = ATA_DEV_ATAPI;
316 break;
317 default:
318 SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
319 __func__, dev->sata_dev.command_set);
320 *class = ATA_DEV_UNKNOWN;
321 break;
322 }
323
324 ap->cbl = ATA_CBL_SATA;
325 return ret;
326}
327
Darrick J. Wong338ec572006-10-18 14:43:37 -0700328static void sas_ata_post_internal(struct ata_queued_cmd *qc)
329{
330 if (qc->flags & ATA_QCFLAG_FAILED)
331 qc->err_mask |= AC_ERR_OTHER;
332
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800333 if (qc->err_mask) {
334 /*
335 * Find the sas_task and kill it. By this point,
336 * libata has decided to kill the qc, so we needn't
337 * bother with sas_ata_task_done. But we still
338 * ought to abort the task.
339 */
340 struct sas_task *task = qc->lldd_task;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800341 unsigned long flags;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800342
343 qc->lldd_task = NULL;
344 if (task) {
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800345 /* Should this be a AT(API) device reset? */
346 spin_lock_irqsave(&task->task_state_lock, flags);
347 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
348 spin_unlock_irqrestore(&task->task_state_lock, flags);
349
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800350 task->uldd_task = NULL;
351 __sas_task_abort(task);
352 }
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800353 }
Darrick J. Wong338ec572006-10-18 14:43:37 -0700354}
355
Dan Williamsb91bb292011-11-17 17:59:52 -0800356
357static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
358{
359 struct domain_device *dev = ap->private_data;
360 struct sas_internal *i =
361 to_sas_internal(dev->port->ha->core.shost->transportt);
362
363 if (i->dft->lldd_ata_set_dmamode)
364 i->dft->lldd_ata_set_dmamode(dev);
365}
366
Darrick J. Wong338ec572006-10-18 14:43:37 -0700367static struct ata_port_operations sas_sata_ops = {
James Bottomley00dd4992011-01-23 09:44:12 -0600368 .prereset = ata_std_prereset,
Dave Jiang1ca1e432011-05-24 13:18:04 -0700369 .softreset = sas_ata_soft_reset,
James Bottomley00dd4992011-01-23 09:44:12 -0600370 .hardreset = sas_ata_hard_reset,
371 .postreset = ata_std_postreset,
372 .error_handler = ata_std_error_handler,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700373 .post_internal_cmd = sas_ata_post_internal,
David Milburnf0ad30d2010-09-03 17:13:03 -0500374 .qc_defer = ata_std_qc_defer,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700375 .qc_prep = ata_noop_qc_prep,
376 .qc_issue = sas_ata_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900377 .qc_fill_rtf = sas_ata_qc_fill_rtf,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700378 .port_start = ata_sas_port_start,
379 .port_stop = ata_sas_port_stop,
Dan Williamsb91bb292011-11-17 17:59:52 -0800380 .set_dmamode = sas_ata_set_dmamode,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700381};
382
383static struct ata_port_info sata_port_info = {
Sergei Shtylyov9cbe0562011-02-04 22:05:48 +0300384 .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
Sergei Shtylyov0f2e0332011-01-21 20:32:01 +0300385 .pio_mask = ATA_PIO4,
386 .mwdma_mask = ATA_MWDMA2,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700387 .udma_mask = ATA_UDMA6,
388 .port_ops = &sas_sata_ops
389};
390
391int sas_ata_init_host_and_port(struct domain_device *found_dev,
392 struct scsi_target *starget)
393{
394 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
395 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
396 struct ata_port *ap;
397
398 ata_host_init(&found_dev->sata_dev.ata_host,
Jeff Garzik1d1bbee2007-07-26 09:28:37 -0400399 ha->dev,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700400 sata_port_info.flags,
401 &sas_sata_ops);
402 ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
403 &sata_port_info,
404 shost);
405 if (!ap) {
406 SAS_DPRINTK("ata_sas_port_alloc failed.\n");
407 return -ENODEV;
408 }
409
410 ap->private_data = found_dev;
411 ap->cbl = ATA_CBL_SATA;
412 ap->scsi_host = shost;
413 found_dev->sata_dev.ap = ap;
414
415 return 0;
416}
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800417
418void sas_ata_task_abort(struct sas_task *task)
419{
420 struct ata_queued_cmd *qc = task->uldd_task;
421 struct completion *waiting;
422
423 /* Bounce SCSI-initiated commands to the SCSI EH */
424 if (qc->scsicmd) {
James Bottomley1b4d0d82010-05-13 09:31:54 -0500425 struct request_queue *q = qc->scsicmd->device->request_queue;
426 unsigned long flags;
427
Tejun Heo70b25f82010-04-15 09:00:08 +0900428 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700429 blk_abort_request(qc->scsicmd->request);
Tejun Heo70b25f82010-04-15 09:00:08 +0900430 spin_unlock_irqrestore(q->queue_lock, flags);
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800431 scsi_schedule_eh(qc->scsicmd->device->host);
432 return;
433 }
434
435 /* Internal command, fake a timeout and complete. */
436 qc->flags &= ~ATA_QCFLAG_ACTIVE;
437 qc->flags |= ATA_QCFLAG_FAILED;
438 qc->err_mask |= AC_ERR_TIMEOUT;
439 waiting = qc->private_data;
440 complete(waiting);
441}
James Bottomleyb9142172007-07-22 13:15:55 -0500442
James Bottomleyb9142172007-07-22 13:15:55 -0500443static void sas_get_ata_command_set(struct domain_device *dev)
444{
445 struct dev_to_host_fis *fis =
446 (struct dev_to_host_fis *) dev->frame_rcvd;
447
448 if ((fis->sector_count == 1 && /* ATA */
449 fis->lbal == 1 &&
450 fis->lbam == 0 &&
451 fis->lbah == 0 &&
452 fis->device == 0)
453 ||
454 (fis->sector_count == 0 && /* CE-ATA (mATA) */
455 fis->lbal == 0 &&
456 fis->lbam == 0xCE &&
457 fis->lbah == 0xAA &&
458 (fis->device & ~0x10) == 0))
459
460 dev->sata_dev.command_set = ATA_COMMAND_SET;
461
462 else if ((fis->interrupt_reason == 1 && /* ATAPI */
463 fis->lbal == 1 &&
464 fis->byte_count_low == 0x14 &&
465 fis->byte_count_high == 0xEB &&
466 (fis->device & ~0x10) == 0))
467
468 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
469
470 else if ((fis->sector_count == 1 && /* SEMB */
471 fis->lbal == 1 &&
472 fis->lbam == 0x3C &&
473 fis->lbah == 0xC3 &&
474 fis->device == 0)
475 ||
476 (fis->interrupt_reason == 1 && /* SATA PM */
477 fis->lbal == 1 &&
478 fis->byte_count_low == 0x69 &&
479 fis->byte_count_high == 0x96 &&
480 (fis->device & ~0x10) == 0))
481
482 /* Treat it as a superset? */
483 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
484}
485
Dan Williams87c83312011-11-17 17:59:51 -0800486void sas_probe_sata(struct work_struct *work)
487{
488 struct domain_device *dev, *n;
489 struct sas_discovery_event *ev =
490 container_of(work, struct sas_discovery_event, work);
491 struct asd_sas_port *port = ev->port;
492
493 clear_bit(DISCE_PROBE, &port->disc.pending);
494
495 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
496 int err;
497
498 spin_lock_irq(&port->dev_list_lock);
499 list_add_tail(&dev->dev_list_node, &port->dev_list);
500 spin_unlock_irq(&port->dev_list_lock);
501
502 err = sas_rphy_add(dev->rphy);
503
504 if (err) {
505 SAS_DPRINTK("%s: for %s device %16llx returned %d\n",
506 __func__, dev->parent ? "exp-attached" :
507 "direct-attached",
508 SAS_ADDR(dev->sas_addr), err);
509 sas_unregister_dev(port, dev);
510 } else
511 list_del_init(&dev->disco_list_node);
512 }
513}
514
James Bottomleyb9142172007-07-22 13:15:55 -0500515/**
516 * sas_discover_sata -- discover an STP/SATA domain device
517 * @dev: pointer to struct domain_device of interest
518 *
Dan Williamsb91bb292011-11-17 17:59:52 -0800519 * Devices directly attached to a HA port, have no parents. All other
520 * devices do, and should have their "parent" pointer set appropriately
521 * before calling this function.
James Bottomleyb9142172007-07-22 13:15:55 -0500522 */
523int sas_discover_sata(struct domain_device *dev)
524{
525 int res;
526
Dan Williamsb91bb292011-11-17 17:59:52 -0800527 if (dev->dev_type == SATA_PM)
528 return -ENODEV;
529
James Bottomleyb9142172007-07-22 13:15:55 -0500530 sas_get_ata_command_set(dev);
Dan Williamsb91bb292011-11-17 17:59:52 -0800531 sas_fill_in_rphy(dev, dev->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800532
533 res = sas_notify_lldd_dev_found(dev);
534 if (res)
535 return res;
536
537 sas_discover_event(dev->port, DISCE_PROBE);
Dan Williamsb91bb292011-11-17 17:59:52 -0800538 return 0;
James Bottomleyb9142172007-07-22 13:15:55 -0500539}
James Bottomley00dd4992011-01-23 09:44:12 -0600540
541void sas_ata_strategy_handler(struct Scsi_Host *shost)
542{
543 struct scsi_device *sdev;
Dan Williams87c83312011-11-17 17:59:51 -0800544 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
545
546 /* it's ok to defer revalidation events during ata eh, these
547 * disks are in one of three states:
548 * 1/ present for initial domain discovery, and these
549 * resets will cause bcn flutters
550 * 2/ hot removed, we'll discover that after eh fails
551 * 3/ hot added after initial discovery, lost the race, and need
552 * to catch the next train.
553 */
554 sas_disable_revalidation(sas_ha);
James Bottomley00dd4992011-01-23 09:44:12 -0600555
556 shost_for_each_device(sdev, shost) {
557 struct domain_device *ddev = sdev_to_domain_dev(sdev);
558 struct ata_port *ap = ddev->sata_dev.ap;
559
560 if (!dev_is_sata(ddev))
561 continue;
James Bottomleyc2991902011-01-23 09:44:12 -0600562
James Bottomley00dd4992011-01-23 09:44:12 -0600563 ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
564 ata_scsi_port_error_handler(shost, ap);
565 }
Dan Williams87c83312011-11-17 17:59:51 -0800566
567 sas_enable_revalidation(sas_ha);
James Bottomley00dd4992011-01-23 09:44:12 -0600568}
569
570int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,
571 enum blk_eh_timer_return *rtn)
572{
573 struct domain_device *ddev = cmd_to_domain_dev(cmd);
574
575 if (!dev_is_sata(ddev) || task)
576 return 0;
577
578 /* we're a sata device with no task, so this must be a libata
579 * eh timeout. Ideally should hook into libata timeout
580 * handling, but there's no point, it just wants to activate
581 * the eh thread */
582 *rtn = BLK_EH_NOT_HANDLED;
583 return 1;
584}
585
586int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
587 struct list_head *done_q)
588{
589 int rtn = 0;
590 struct scsi_cmnd *cmd, *n;
591 struct ata_port *ap;
592
593 do {
594 LIST_HEAD(sata_q);
595
596 ap = NULL;
James Bottomleyc2991902011-01-23 09:44:12 -0600597
James Bottomley00dd4992011-01-23 09:44:12 -0600598 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
599 struct domain_device *ddev = cmd_to_domain_dev(cmd);
600
601 if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
602 continue;
James Bottomleyc2991902011-01-23 09:44:12 -0600603 if (ap && ap != ddev->sata_dev.ap)
James Bottomley00dd4992011-01-23 09:44:12 -0600604 continue;
605 ap = ddev->sata_dev.ap;
606 rtn = 1;
607 list_move(&cmd->eh_entry, &sata_q);
608 }
609
610 if (!list_empty(&sata_q)) {
James Bottomleyc2991902011-01-23 09:44:12 -0600611 ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
James Bottomley00dd4992011-01-23 09:44:12 -0600612 ata_scsi_cmd_error_handler(shost, ap, &sata_q);
James Bottomleya82058a2011-03-10 17:13:18 -0600613 /*
614 * ata's error handler may leave the cmd on the list
615 * so make sure they don't remain on a stack list
616 * about to go out of scope.
617 *
618 * This looks strange, since the commands are
619 * now part of no list, but the next error
620 * action will be ata_port_error_handler()
621 * which takes no list and sweeps them up
622 * anyway from the ata tag array.
623 */
624 while (!list_empty(&sata_q))
625 list_del_init(sata_q.next);
James Bottomley00dd4992011-01-23 09:44:12 -0600626 }
627 } while (ap);
628
629 return rtn;
630}