blob: 4beca66728b47c9d72eb59d74f4757589c8c8522 [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>
Dan Williams50824d62011-12-04 01:06:24 -080026#include <linux/async.h>
James Bottomleyb9142172007-07-22 13:15:55 -050027
Darrick J. Wong338ec572006-10-18 14:43:37 -070028#include <scsi/sas_ata.h>
29#include "sas_internal.h"
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi.h>
34#include <scsi/scsi_transport.h>
35#include <scsi/scsi_transport_sas.h>
36#include "../scsi_sas_internal.h"
Darrick J. Wong3a2755a2007-01-30 01:18:58 -080037#include "../scsi_transport_api.h"
38#include <scsi/scsi_eh.h>
Darrick J. Wong338ec572006-10-18 14:43:37 -070039
40static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
41{
42 /* Cheesy attempt to translate SAS errors into ATA. Hah! */
43
44 /* transport error */
45 if (ts->resp == SAS_TASK_UNDELIVERED)
46 return AC_ERR_ATA_BUS;
47
48 /* ts->resp == SAS_TASK_COMPLETE */
49 /* task delivered, what happened afterwards? */
50 switch (ts->stat) {
51 case SAS_DEV_NO_RESPONSE:
52 return AC_ERR_TIMEOUT;
53
54 case SAS_INTERRUPTED:
55 case SAS_PHY_DOWN:
56 case SAS_NAK_R_ERR:
57 return AC_ERR_ATA_BUS;
58
59
60 case SAS_DATA_UNDERRUN:
61 /*
62 * Some programs that use the taskfile interface
63 * (smartctl in particular) can cause underrun
64 * problems. Ignore these errors, perhaps at our
65 * peril.
66 */
67 return 0;
68
69 case SAS_DATA_OVERRUN:
70 case SAS_QUEUE_FULL:
71 case SAS_DEVICE_UNKNOWN:
72 case SAS_SG_ERR:
73 return AC_ERR_INVALID;
74
Darrick J. Wong338ec572006-10-18 14:43:37 -070075 case SAS_OPEN_TO:
76 case SAS_OPEN_REJECT:
77 SAS_DPRINTK("%s: Saw error %d. What to do?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070078 __func__, ts->stat);
Darrick J. Wong338ec572006-10-18 14:43:37 -070079 return AC_ERR_OTHER;
80
James Bottomley75c0b382011-01-23 08:16:24 -060081 case SAM_STAT_CHECK_CONDITION:
Darrick J. Wong338ec572006-10-18 14:43:37 -070082 case SAS_ABORTED_TASK:
83 return AC_ERR_DEV;
84
85 case SAS_PROTO_RESPONSE:
86 /* This means the ending_fis has the error
87 * value; return 0 here to collect it */
88 return 0;
89 default:
90 return 0;
91 }
92}
93
94static void sas_ata_task_done(struct sas_task *task)
95{
96 struct ata_queued_cmd *qc = task->uldd_task;
Dan Williams9095a642011-11-28 11:29:20 -080097 struct domain_device *dev = task->dev;
Darrick J. Wong338ec572006-10-18 14:43:37 -070098 struct task_status_struct *stat = &task->task_status;
99 struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
Dan Williams9095a642011-11-28 11:29:20 -0800100 struct sas_ha_struct *sas_ha = dev->port->ha;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700101 enum ata_completion_errors ac;
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800102 unsigned long flags;
Xiangliang Yubb650a12011-05-08 19:27:01 +0800103 struct ata_link *link;
Dan Williams3dff5722011-11-28 12:08:22 -0800104 struct ata_port *ap;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700105
Dan Williams9095a642011-11-28 11:29:20 -0800106 spin_lock_irqsave(&dev->done_lock, flags);
107 if (test_bit(SAS_HA_FROZEN, &sas_ha->state))
108 task = NULL;
109 else if (qc && qc->scsicmd)
110 ASSIGN_SAS_TASK(qc->scsicmd, NULL);
111 spin_unlock_irqrestore(&dev->done_lock, flags);
112
113 /* check if libsas-eh got to the task before us */
114 if (unlikely(!task))
115 return;
116
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800117 if (!qc)
118 goto qc_already_gone;
119
Dan Williams3dff5722011-11-28 12:08:22 -0800120 ap = qc->ap;
Dan Williams3dff5722011-11-28 12:08:22 -0800121 link = &ap->link;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800122
Dan Williams3dff5722011-11-28 12:08:22 -0800123 spin_lock_irqsave(ap->lock, flags);
124 /* check if we lost the race with libata/sas_ata_post_internal() */
125 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) {
126 spin_unlock_irqrestore(ap->lock, flags);
127 if (qc->scsicmd)
128 goto qc_already_gone;
129 else {
130 /* if eh is not involved and the port is frozen then the
131 * ata internal abort process has taken responsibility
132 * for this sas_task
133 */
134 return;
135 }
136 }
137
James Bottomley75c0b382011-01-23 08:16:24 -0600138 if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
139 ((stat->stat == SAM_STAT_CHECK_CONDITION &&
140 dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700141 ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
Xiangliang Yubb650a12011-05-08 19:27:01 +0800142
143 if (!link->sactive) {
144 qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
145 } else {
146 link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
147 if (unlikely(link->eh_info.err_mask))
148 qc->flags |= ATA_QCFLAG_FAILED;
149 }
James Bottomley75c0b382011-01-23 08:16:24 -0600150 } else {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700151 ac = sas_to_ata_err(stat);
152 if (ac) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700153 SAS_DPRINTK("%s: SAS error %x\n", __func__,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700154 stat->stat);
155 /* We saw a SAS error. Send a vague error. */
Xiangliang Yubb650a12011-05-08 19:27:01 +0800156 if (!link->sactive) {
157 qc->err_mask = ac;
158 } else {
159 link->eh_info.err_mask |= AC_ERR_DEV;
160 qc->flags |= ATA_QCFLAG_FAILED;
161 }
162
Darrick J. Wong338ec572006-10-18 14:43:37 -0700163 dev->sata_dev.tf.feature = 0x04; /* status err */
164 dev->sata_dev.tf.command = ATA_ERR;
165 }
166 }
167
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800168 qc->lldd_task = NULL;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700169 ata_qc_complete(qc);
Dan Williams3dff5722011-11-28 12:08:22 -0800170 spin_unlock_irqrestore(ap->lock, flags);
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800171
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800172qc_already_gone:
Darrick J. Wong338ec572006-10-18 14:43:37 -0700173 list_del_init(&task->list);
174 sas_free_task(task);
175}
176
177static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
178{
Dan Williams312d3e52011-11-17 17:59:50 -0800179 unsigned long flags;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700180 struct sas_task *task;
Dan Williams312d3e52011-11-17 17:59:50 -0800181 struct scatterlist *sg;
182 int ret = AC_ERR_SYSTEM;
183 unsigned int si, xfer = 0;
184 struct ata_port *ap = qc->ap;
185 struct domain_device *dev = ap->private_data;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700186 struct sas_ha_struct *sas_ha = dev->port->ha;
187 struct Scsi_Host *host = sas_ha->core.shost;
188 struct sas_internal *i = to_sas_internal(host->transportt);
Dan Williams312d3e52011-11-17 17:59:50 -0800189
190 /* TODO: audit callers to ensure they are ready for qc_issue to
191 * unconditionally re-enable interrupts
192 */
193 local_irq_save(flags);
194 spin_unlock(ap->lock);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700195
Darrick J. Wong56dd2c02010-10-01 13:55:47 -0700196 /* If the device fell off, no sense in issuing commands */
Dan Williamse1399422012-01-07 08:52:39 +0000197 if (test_bit(SAS_DEV_GONE, &dev->state))
Dan Williams312d3e52011-11-17 17:59:50 -0800198 goto out;
Darrick J. Wong56dd2c02010-10-01 13:55:47 -0700199
Darrick J. Wong338ec572006-10-18 14:43:37 -0700200 task = sas_alloc_task(GFP_ATOMIC);
201 if (!task)
Dan Williams312d3e52011-11-17 17:59:50 -0800202 goto out;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700203 task->dev = dev;
204 task->task_proto = SAS_PROTOCOL_STP;
205 task->task_done = sas_ata_task_done;
206
207 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
208 qc->tf.command == ATA_CMD_FPDMA_READ) {
209 /* Need to zero out the tag libata assigned us */
210 qc->tf.nsect = 0;
211 }
212
James Bottomley110dd8f2007-07-20 13:11:44 -0500213 ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700214 task->uldd_task = qc;
Tejun Heo405e66b2007-11-27 19:28:53 +0900215 if (ata_is_atapi(qc->tf.protocol)) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700216 memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
James Bottomleydde20202008-02-19 11:36:56 +0100217 task->total_xfer_len = qc->nbytes;
218 task->num_scatter = qc->n_elem;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700219 } else {
Tejun Heoff2aeb12007-12-05 16:43:11 +0900220 for_each_sg(qc->sg, sg, qc->n_elem, si)
Darrick J. Wong338ec572006-10-18 14:43:37 -0700221 xfer += sg->length;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700222
223 task->total_xfer_len = xfer;
Tejun Heoff2aeb12007-12-05 16:43:11 +0900224 task->num_scatter = si;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700225 }
226
227 task->data_dir = qc->dma_dir;
Tejun Heoff2aeb12007-12-05 16:43:11 +0900228 task->scatter = qc->sg;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700229 task->ata_task.retry_count = 1;
230 task->task_state_flags = SAS_TASK_STATE_PENDING;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800231 qc->lldd_task = task;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700232
233 switch (qc->tf.protocol) {
234 case ATA_PROT_NCQ:
235 task->ata_task.use_ncq = 1;
236 /* fall through */
Tejun Heo0dc36882007-12-18 16:34:43 -0500237 case ATAPI_PROT_DMA:
Darrick J. Wong338ec572006-10-18 14:43:37 -0700238 case ATA_PROT_DMA:
239 task->ata_task.dma_xfer = 1;
240 break;
241 }
242
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800243 if (qc->scsicmd)
244 ASSIGN_SAS_TASK(qc->scsicmd, task);
245
Darrick J. Wong338ec572006-10-18 14:43:37 -0700246 if (sas_ha->lldd_max_execute_num < 2)
Dan Williams312d3e52011-11-17 17:59:50 -0800247 ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700248 else
Dan Williams312d3e52011-11-17 17:59:50 -0800249 ret = sas_queue_up(task);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700250
251 /* Examine */
Dan Williams312d3e52011-11-17 17:59:50 -0800252 if (ret) {
253 SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700254
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800255 if (qc->scsicmd)
256 ASSIGN_SAS_TASK(qc->scsicmd, NULL);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700257 sas_free_task(task);
Dan Williams312d3e52011-11-17 17:59:50 -0800258 ret = AC_ERR_SYSTEM;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700259 }
260
Dan Williams312d3e52011-11-17 17:59:50 -0800261 out:
262 spin_lock(ap->lock);
263 local_irq_restore(flags);
264 return ret;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700265}
266
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900267static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
268{
269 struct domain_device *dev = qc->ap->private_data;
270
271 memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
272 return true;
273}
274
James Bottomley00dd4992011-01-23 09:44:12 -0600275static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
276 unsigned long deadline)
Darrick J. Wong338ec572006-10-18 14:43:37 -0700277{
James Bottomley00dd4992011-01-23 09:44:12 -0600278 struct ata_port *ap = link->ap;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700279 struct domain_device *dev = ap->private_data;
280 struct sas_internal *i =
281 to_sas_internal(dev->port->ha->core.shost->transportt);
James Bottomleya29c0512008-02-23 23:38:44 -0600282 int res = TMF_RESP_FUNC_FAILED;
James Bottomley00dd4992011-01-23 09:44:12 -0600283 int ret = 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700284
285 if (i->dft->lldd_I_T_nexus_reset)
286 res = i->dft->lldd_I_T_nexus_reset(dev);
287
James Bottomley00dd4992011-01-23 09:44:12 -0600288 if (res != TMF_RESP_FUNC_COMPLETE) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700289 SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
James Bottomley00dd4992011-01-23 09:44:12 -0600290 ret = -EAGAIN;
291 }
Darrick J. Wong338ec572006-10-18 14:43:37 -0700292
293 switch (dev->sata_dev.command_set) {
294 case ATA_COMMAND_SET:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700295 SAS_DPRINTK("%s: Found ATA device.\n", __func__);
James Bottomley00dd4992011-01-23 09:44:12 -0600296 *class = ATA_DEV_ATA;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700297 break;
298 case ATAPI_COMMAND_SET:
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700299 SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
James Bottomley00dd4992011-01-23 09:44:12 -0600300 *class = ATA_DEV_ATAPI;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700301 break;
302 default:
303 SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700304 __func__,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700305 dev->sata_dev.command_set);
James Bottomley00dd4992011-01-23 09:44:12 -0600306 *class = ATA_DEV_UNKNOWN;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700307 break;
308 }
309
310 ap->cbl = ATA_CBL_SATA;
James Bottomley00dd4992011-01-23 09:44:12 -0600311 return ret;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700312}
313
Dave Jiang1ca1e432011-05-24 13:18:04 -0700314static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
315 unsigned long deadline)
316{
317 struct ata_port *ap = link->ap;
318 struct domain_device *dev = ap->private_data;
319 struct sas_internal *i =
320 to_sas_internal(dev->port->ha->core.shost->transportt);
321 int res = TMF_RESP_FUNC_FAILED;
322 int ret = 0;
323
324 if (i->dft->lldd_ata_soft_reset)
325 res = i->dft->lldd_ata_soft_reset(dev);
326
327 if (res != TMF_RESP_FUNC_COMPLETE) {
328 SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
329 ret = -EAGAIN;
330 }
331
332 switch (dev->sata_dev.command_set) {
333 case ATA_COMMAND_SET:
334 SAS_DPRINTK("%s: Found ATA device.\n", __func__);
335 *class = ATA_DEV_ATA;
336 break;
337 case ATAPI_COMMAND_SET:
338 SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
339 *class = ATA_DEV_ATAPI;
340 break;
341 default:
342 SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
343 __func__, dev->sata_dev.command_set);
344 *class = ATA_DEV_UNKNOWN;
345 break;
346 }
347
348 ap->cbl = ATA_CBL_SATA;
349 return ret;
350}
351
Dan Williams3dff5722011-11-28 12:08:22 -0800352/*
353 * notify the lldd to forget the sas_task for this internal ata command
354 * that bypasses scsi-eh
355 */
356static void sas_ata_internal_abort(struct sas_task *task)
357{
358 struct sas_internal *si =
359 to_sas_internal(task->dev->port->ha->core.shost->transportt);
360 unsigned long flags;
361 int res;
362
363 spin_lock_irqsave(&task->task_state_lock, flags);
364 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
365 task->task_state_flags & SAS_TASK_STATE_DONE) {
366 spin_unlock_irqrestore(&task->task_state_lock, flags);
367 SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
368 task);
369 goto out;
370 }
371 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
372 spin_unlock_irqrestore(&task->task_state_lock, flags);
373
374 res = si->dft->lldd_abort_task(task);
375
376 spin_lock_irqsave(&task->task_state_lock, flags);
377 if (task->task_state_flags & SAS_TASK_STATE_DONE ||
378 res == TMF_RESP_FUNC_COMPLETE) {
379 spin_unlock_irqrestore(&task->task_state_lock, flags);
380 goto out;
381 }
382
383 /* XXX we are not prepared to deal with ->lldd_abort_task()
384 * failures. TODO: lldds need to unconditionally forget about
385 * aborted ata tasks, otherwise we (likely) leak the sas task
386 * here
387 */
388 SAS_DPRINTK("%s: Task %p leaked.\n", __func__, task);
389
390 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
391 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
392 spin_unlock_irqrestore(&task->task_state_lock, flags);
393
394 return;
395 out:
396 list_del_init(&task->list);
397 sas_free_task(task);
398}
399
Darrick J. Wong338ec572006-10-18 14:43:37 -0700400static void sas_ata_post_internal(struct ata_queued_cmd *qc)
401{
402 if (qc->flags & ATA_QCFLAG_FAILED)
403 qc->err_mask |= AC_ERR_OTHER;
404
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800405 if (qc->err_mask) {
406 /*
Dan Williams3dff5722011-11-28 12:08:22 -0800407 * Find the sas_task and kill it. By this point, libata
408 * has decided to kill the qc and has frozen the port.
409 * In this state sas_ata_task_done() will no longer free
410 * the sas_task, so we need to notify the lldd (via
411 * ->lldd_abort_task) that the task is dead and free it
412 * ourselves.
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800413 */
414 struct sas_task *task = qc->lldd_task;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800415
416 qc->lldd_task = NULL;
Dan Williams3a2cdf32011-11-29 14:54:28 -0800417 if (!task)
418 return;
419 task->uldd_task = NULL;
420 sas_ata_internal_abort(task);
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800421 }
Darrick J. Wong338ec572006-10-18 14:43:37 -0700422}
423
Dan Williamsb91bb292011-11-17 17:59:52 -0800424
425static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
426{
427 struct domain_device *dev = ap->private_data;
428 struct sas_internal *i =
429 to_sas_internal(dev->port->ha->core.shost->transportt);
430
431 if (i->dft->lldd_ata_set_dmamode)
432 i->dft->lldd_ata_set_dmamode(dev);
433}
434
Darrick J. Wong338ec572006-10-18 14:43:37 -0700435static struct ata_port_operations sas_sata_ops = {
James Bottomley00dd4992011-01-23 09:44:12 -0600436 .prereset = ata_std_prereset,
Dave Jiang1ca1e432011-05-24 13:18:04 -0700437 .softreset = sas_ata_soft_reset,
James Bottomley00dd4992011-01-23 09:44:12 -0600438 .hardreset = sas_ata_hard_reset,
439 .postreset = ata_std_postreset,
440 .error_handler = ata_std_error_handler,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700441 .post_internal_cmd = sas_ata_post_internal,
David Milburnf0ad30d2010-09-03 17:13:03 -0500442 .qc_defer = ata_std_qc_defer,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700443 .qc_prep = ata_noop_qc_prep,
444 .qc_issue = sas_ata_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900445 .qc_fill_rtf = sas_ata_qc_fill_rtf,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700446 .port_start = ata_sas_port_start,
447 .port_stop = ata_sas_port_stop,
Dan Williamsb91bb292011-11-17 17:59:52 -0800448 .set_dmamode = sas_ata_set_dmamode,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700449};
450
451static struct ata_port_info sata_port_info = {
Sergei Shtylyov9cbe0562011-02-04 22:05:48 +0300452 .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
Sergei Shtylyov0f2e0332011-01-21 20:32:01 +0300453 .pio_mask = ATA_PIO4,
454 .mwdma_mask = ATA_MWDMA2,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700455 .udma_mask = ATA_UDMA6,
456 .port_ops = &sas_sata_ops
457};
458
459int sas_ata_init_host_and_port(struct domain_device *found_dev,
460 struct scsi_target *starget)
461{
462 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
463 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
464 struct ata_port *ap;
465
466 ata_host_init(&found_dev->sata_dev.ata_host,
Jeff Garzik1d1bbee2007-07-26 09:28:37 -0400467 ha->dev,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700468 sata_port_info.flags,
469 &sas_sata_ops);
470 ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
471 &sata_port_info,
472 shost);
473 if (!ap) {
474 SAS_DPRINTK("ata_sas_port_alloc failed.\n");
475 return -ENODEV;
476 }
477
478 ap->private_data = found_dev;
479 ap->cbl = ATA_CBL_SATA;
480 ap->scsi_host = shost;
481 found_dev->sata_dev.ap = ap;
482
483 return 0;
484}
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800485
486void sas_ata_task_abort(struct sas_task *task)
487{
488 struct ata_queued_cmd *qc = task->uldd_task;
489 struct completion *waiting;
490
491 /* Bounce SCSI-initiated commands to the SCSI EH */
492 if (qc->scsicmd) {
James Bottomley1b4d0d82010-05-13 09:31:54 -0500493 struct request_queue *q = qc->scsicmd->device->request_queue;
494 unsigned long flags;
495
Tejun Heo70b25f82010-04-15 09:00:08 +0900496 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700497 blk_abort_request(qc->scsicmd->request);
Tejun Heo70b25f82010-04-15 09:00:08 +0900498 spin_unlock_irqrestore(q->queue_lock, flags);
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800499 scsi_schedule_eh(qc->scsicmd->device->host);
500 return;
501 }
502
503 /* Internal command, fake a timeout and complete. */
504 qc->flags &= ~ATA_QCFLAG_ACTIVE;
505 qc->flags |= ATA_QCFLAG_FAILED;
506 qc->err_mask |= AC_ERR_TIMEOUT;
507 waiting = qc->private_data;
508 complete(waiting);
509}
James Bottomleyb9142172007-07-22 13:15:55 -0500510
James Bottomleyb9142172007-07-22 13:15:55 -0500511static void sas_get_ata_command_set(struct domain_device *dev)
512{
513 struct dev_to_host_fis *fis =
514 (struct dev_to_host_fis *) dev->frame_rcvd;
515
516 if ((fis->sector_count == 1 && /* ATA */
517 fis->lbal == 1 &&
518 fis->lbam == 0 &&
519 fis->lbah == 0 &&
520 fis->device == 0)
521 ||
522 (fis->sector_count == 0 && /* CE-ATA (mATA) */
523 fis->lbal == 0 &&
524 fis->lbam == 0xCE &&
525 fis->lbah == 0xAA &&
526 (fis->device & ~0x10) == 0))
527
528 dev->sata_dev.command_set = ATA_COMMAND_SET;
529
530 else if ((fis->interrupt_reason == 1 && /* ATAPI */
531 fis->lbal == 1 &&
532 fis->byte_count_low == 0x14 &&
533 fis->byte_count_high == 0xEB &&
534 (fis->device & ~0x10) == 0))
535
536 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
537
538 else if ((fis->sector_count == 1 && /* SEMB */
539 fis->lbal == 1 &&
540 fis->lbam == 0x3C &&
541 fis->lbah == 0xC3 &&
542 fis->device == 0)
543 ||
544 (fis->interrupt_reason == 1 && /* SATA PM */
545 fis->lbal == 1 &&
546 fis->byte_count_low == 0x69 &&
547 fis->byte_count_high == 0x96 &&
548 (fis->device & ~0x10) == 0))
549
550 /* Treat it as a superset? */
551 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
552}
553
Dan Williams87c83312011-11-17 17:59:51 -0800554void sas_probe_sata(struct work_struct *work)
555{
556 struct domain_device *dev, *n;
557 struct sas_discovery_event *ev =
558 container_of(work, struct sas_discovery_event, work);
559 struct asd_sas_port *port = ev->port;
560
561 clear_bit(DISCE_PROBE, &port->disc.pending);
562
563 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
564 int err;
565
566 spin_lock_irq(&port->dev_list_lock);
567 list_add_tail(&dev->dev_list_node, &port->dev_list);
568 spin_unlock_irq(&port->dev_list_lock);
569
570 err = sas_rphy_add(dev->rphy);
571
572 if (err) {
573 SAS_DPRINTK("%s: for %s device %16llx returned %d\n",
574 __func__, dev->parent ? "exp-attached" :
575 "direct-attached",
576 SAS_ADDR(dev->sas_addr), err);
577 sas_unregister_dev(port, dev);
578 } else
579 list_del_init(&dev->disco_list_node);
580 }
581}
582
James Bottomleyb9142172007-07-22 13:15:55 -0500583/**
584 * sas_discover_sata -- discover an STP/SATA domain device
585 * @dev: pointer to struct domain_device of interest
586 *
Dan Williamsb91bb292011-11-17 17:59:52 -0800587 * Devices directly attached to a HA port, have no parents. All other
588 * devices do, and should have their "parent" pointer set appropriately
589 * before calling this function.
James Bottomleyb9142172007-07-22 13:15:55 -0500590 */
591int sas_discover_sata(struct domain_device *dev)
592{
593 int res;
594
Dan Williamsb91bb292011-11-17 17:59:52 -0800595 if (dev->dev_type == SATA_PM)
596 return -ENODEV;
597
James Bottomleyb9142172007-07-22 13:15:55 -0500598 sas_get_ata_command_set(dev);
Dan Williamsb91bb292011-11-17 17:59:52 -0800599 sas_fill_in_rphy(dev, dev->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800600
601 res = sas_notify_lldd_dev_found(dev);
602 if (res)
603 return res;
604
605 sas_discover_event(dev->port, DISCE_PROBE);
Dan Williamsb91bb292011-11-17 17:59:52 -0800606 return 0;
James Bottomleyb9142172007-07-22 13:15:55 -0500607}
James Bottomley00dd4992011-01-23 09:44:12 -0600608
Dan Williams50824d62011-12-04 01:06:24 -0800609static void async_sas_ata_eh(void *data, async_cookie_t cookie)
610{
611 struct domain_device *dev = data;
612 struct ata_port *ap = dev->sata_dev.ap;
613 struct sas_ha_struct *ha = dev->port->ha;
614
615 ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
616 ata_scsi_port_error_handler(ha->core.shost, ap);
617}
618
James Bottomley00dd4992011-01-23 09:44:12 -0600619void sas_ata_strategy_handler(struct Scsi_Host *shost)
620{
621 struct scsi_device *sdev;
Dan Williams87c83312011-11-17 17:59:51 -0800622 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
Dan Williams50824d62011-12-04 01:06:24 -0800623 LIST_HEAD(async);
Dan Williams87c83312011-11-17 17:59:51 -0800624
625 /* it's ok to defer revalidation events during ata eh, these
626 * disks are in one of three states:
627 * 1/ present for initial domain discovery, and these
628 * resets will cause bcn flutters
629 * 2/ hot removed, we'll discover that after eh fails
630 * 3/ hot added after initial discovery, lost the race, and need
631 * to catch the next train.
632 */
633 sas_disable_revalidation(sas_ha);
James Bottomley00dd4992011-01-23 09:44:12 -0600634
635 shost_for_each_device(sdev, shost) {
636 struct domain_device *ddev = sdev_to_domain_dev(sdev);
James Bottomley00dd4992011-01-23 09:44:12 -0600637
638 if (!dev_is_sata(ddev))
639 continue;
James Bottomleyc2991902011-01-23 09:44:12 -0600640
Dan Williams50824d62011-12-04 01:06:24 -0800641 async_schedule_domain(async_sas_ata_eh, ddev, &async);
James Bottomley00dd4992011-01-23 09:44:12 -0600642 }
Dan Williams50824d62011-12-04 01:06:24 -0800643 async_synchronize_full_domain(&async);
Dan Williams87c83312011-11-17 17:59:51 -0800644
645 sas_enable_revalidation(sas_ha);
James Bottomley00dd4992011-01-23 09:44:12 -0600646}
647
James Bottomley00dd4992011-01-23 09:44:12 -0600648int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
649 struct list_head *done_q)
650{
651 int rtn = 0;
652 struct scsi_cmnd *cmd, *n;
653 struct ata_port *ap;
654
655 do {
656 LIST_HEAD(sata_q);
657
658 ap = NULL;
James Bottomleyc2991902011-01-23 09:44:12 -0600659
James Bottomley00dd4992011-01-23 09:44:12 -0600660 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
661 struct domain_device *ddev = cmd_to_domain_dev(cmd);
662
663 if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
664 continue;
James Bottomleyc2991902011-01-23 09:44:12 -0600665 if (ap && ap != ddev->sata_dev.ap)
James Bottomley00dd4992011-01-23 09:44:12 -0600666 continue;
667 ap = ddev->sata_dev.ap;
668 rtn = 1;
669 list_move(&cmd->eh_entry, &sata_q);
670 }
671
672 if (!list_empty(&sata_q)) {
James Bottomleyc2991902011-01-23 09:44:12 -0600673 ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
James Bottomley00dd4992011-01-23 09:44:12 -0600674 ata_scsi_cmd_error_handler(shost, ap, &sata_q);
James Bottomleya82058a2011-03-10 17:13:18 -0600675 /*
676 * ata's error handler may leave the cmd on the list
677 * so make sure they don't remain on a stack list
678 * about to go out of scope.
679 *
680 * This looks strange, since the commands are
681 * now part of no list, but the next error
682 * action will be ata_port_error_handler()
683 * which takes no list and sweeps them up
684 * anyway from the ata tag array.
685 */
686 while (!list_empty(&sata_q))
687 list_del_init(sata_q.next);
James Bottomley00dd4992011-01-23 09:44:12 -0600688 }
689 } while (ap);
690
691 return rtn;
692}
Dan Williamsb52df412011-11-30 23:23:33 -0800693
694void sas_ata_schedule_reset(struct domain_device *dev)
695{
696 struct ata_eh_info *ehi;
697 struct ata_port *ap;
698 unsigned long flags;
699
700 if (!dev_is_sata(dev))
701 return;
702
703 ap = dev->sata_dev.ap;
704 ehi = &ap->link.eh_info;
705
706 spin_lock_irqsave(ap->lock, flags);
707 ehi->err_mask |= AC_ERR_TIMEOUT;
708 ehi->action |= ATA_EH_RESET;
709 ata_port_schedule_eh(ap);
710 spin_unlock_irqrestore(ap->lock, flags);
711}
Dan Williams81c757b2011-12-02 16:07:01 -0800712
713void sas_ata_wait_eh(struct domain_device *dev)
714{
715 struct ata_port *ap;
716
717 if (!dev_is_sata(dev))
718 return;
719
720 ap = dev->sata_dev.ap;
721 ata_port_wait_eh(ap);
722}