blob: 359391f5735ff2b9477e644e7b0ec7c0332f518a [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
24#include <scsi/sas_ata.h>
25#include "sas_internal.h"
26#include <scsi/scsi_host.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_tcq.h>
29#include <scsi/scsi.h>
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_sas.h>
32#include "../scsi_sas_internal.h"
Darrick J. Wong3a2755a2007-01-30 01:18:58 -080033#include "../scsi_transport_api.h"
34#include <scsi/scsi_eh.h>
Darrick J. Wong338ec572006-10-18 14:43:37 -070035
36static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
37{
38 /* Cheesy attempt to translate SAS errors into ATA. Hah! */
39
40 /* transport error */
41 if (ts->resp == SAS_TASK_UNDELIVERED)
42 return AC_ERR_ATA_BUS;
43
44 /* ts->resp == SAS_TASK_COMPLETE */
45 /* task delivered, what happened afterwards? */
46 switch (ts->stat) {
47 case SAS_DEV_NO_RESPONSE:
48 return AC_ERR_TIMEOUT;
49
50 case SAS_INTERRUPTED:
51 case SAS_PHY_DOWN:
52 case SAS_NAK_R_ERR:
53 return AC_ERR_ATA_BUS;
54
55
56 case SAS_DATA_UNDERRUN:
57 /*
58 * Some programs that use the taskfile interface
59 * (smartctl in particular) can cause underrun
60 * problems. Ignore these errors, perhaps at our
61 * peril.
62 */
63 return 0;
64
65 case SAS_DATA_OVERRUN:
66 case SAS_QUEUE_FULL:
67 case SAS_DEVICE_UNKNOWN:
68 case SAS_SG_ERR:
69 return AC_ERR_INVALID;
70
71 case SAM_CHECK_COND:
72 case SAS_OPEN_TO:
73 case SAS_OPEN_REJECT:
74 SAS_DPRINTK("%s: Saw error %d. What to do?\n",
75 __FUNCTION__, ts->stat);
76 return AC_ERR_OTHER;
77
78 case SAS_ABORTED_TASK:
79 return AC_ERR_DEV;
80
81 case SAS_PROTO_RESPONSE:
82 /* This means the ending_fis has the error
83 * value; return 0 here to collect it */
84 return 0;
85 default:
86 return 0;
87 }
88}
89
90static void sas_ata_task_done(struct sas_task *task)
91{
92 struct ata_queued_cmd *qc = task->uldd_task;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -080093 struct domain_device *dev;
Darrick J. Wong338ec572006-10-18 14:43:37 -070094 struct task_status_struct *stat = &task->task_status;
95 struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -080096 struct sas_ha_struct *sas_ha;
Darrick J. Wong338ec572006-10-18 14:43:37 -070097 enum ata_completion_errors ac;
Darrick J. Wong3eb7a512007-01-30 01:18:35 -080098 unsigned long flags;
Darrick J. Wong338ec572006-10-18 14:43:37 -070099
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800100 if (!qc)
101 goto qc_already_gone;
102
103 dev = qc->ap->private_data;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800104 sas_ha = dev->port->ha;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800105
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800106 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
Darrick J. Wongd97db632007-01-30 01:18:49 -0800107 if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_GOOD) {
Darrick J. Wong338ec572006-10-18 14:43:37 -0700108 ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
109 qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
110 dev->sata_dev.sstatus = resp->sstatus;
111 dev->sata_dev.serror = resp->serror;
112 dev->sata_dev.scontrol = resp->scontrol;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700113 } else if (stat->stat != SAM_STAT_GOOD) {
114 ac = sas_to_ata_err(stat);
115 if (ac) {
116 SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__,
117 stat->stat);
118 /* We saw a SAS error. Send a vague error. */
119 qc->err_mask = ac;
120 dev->sata_dev.tf.feature = 0x04; /* status err */
121 dev->sata_dev.tf.command = ATA_ERR;
122 }
123 }
124
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800125 qc->lldd_task = NULL;
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800126 if (qc->scsicmd)
127 ASSIGN_SAS_TASK(qc->scsicmd, NULL);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700128 ata_qc_complete(qc);
Darrick J. Wong3eb7a512007-01-30 01:18:35 -0800129 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
130
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800131 /*
132 * If the sas_task has an ata qc, a scsi_cmnd and the aborted
133 * flag is set, then we must have come in via the libsas EH
134 * functions. When we exit this function, we need to put the
135 * scsi_cmnd on the list of finished errors. The ata_qc_complete
136 * call cleans up the libata side of things but we're protected
137 * from the scsi_cmnd going away because the scsi_cmnd is owned
138 * by the EH, making libata's call to scsi_done a NOP.
139 */
140 spin_lock_irqsave(&task->task_state_lock, flags);
141 if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
142 scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
143 spin_unlock_irqrestore(&task->task_state_lock, flags);
144
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800145qc_already_gone:
Darrick J. Wong338ec572006-10-18 14:43:37 -0700146 list_del_init(&task->list);
147 sas_free_task(task);
148}
149
150static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
151{
Darrick J. Wong35a7f2f2007-01-30 01:18:38 -0800152 int res;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700153 struct sas_task *task;
154 struct domain_device *dev = qc->ap->private_data;
155 struct sas_ha_struct *sas_ha = dev->port->ha;
156 struct Scsi_Host *host = sas_ha->core.shost;
157 struct sas_internal *i = to_sas_internal(host->transportt);
158 struct scatterlist *sg;
159 unsigned int num = 0;
160 unsigned int xfer = 0;
161
162 task = sas_alloc_task(GFP_ATOMIC);
163 if (!task)
Darrick J. Wong35a7f2f2007-01-30 01:18:38 -0800164 return AC_ERR_SYSTEM;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700165 task->dev = dev;
166 task->task_proto = SAS_PROTOCOL_STP;
167 task->task_done = sas_ata_task_done;
168
169 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
170 qc->tf.command == ATA_CMD_FPDMA_READ) {
171 /* Need to zero out the tag libata assigned us */
172 qc->tf.nsect = 0;
173 }
174
James Bottomley110dd8f2007-07-20 13:11:44 -0500175 ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700176 task->uldd_task = qc;
177 if (is_atapi_taskfile(&qc->tf)) {
178 memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
179 task->total_xfer_len = qc->nbytes + qc->pad_len;
180 task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
181 } else {
182 ata_for_each_sg(sg, qc) {
183 num++;
184 xfer += sg->length;
185 }
186
187 task->total_xfer_len = xfer;
188 task->num_scatter = num;
189 }
190
191 task->data_dir = qc->dma_dir;
192 task->scatter = qc->__sg;
193 task->ata_task.retry_count = 1;
194 task->task_state_flags = SAS_TASK_STATE_PENDING;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800195 qc->lldd_task = task;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700196
197 switch (qc->tf.protocol) {
198 case ATA_PROT_NCQ:
199 task->ata_task.use_ncq = 1;
200 /* fall through */
201 case ATA_PROT_ATAPI_DMA:
202 case ATA_PROT_DMA:
203 task->ata_task.dma_xfer = 1;
204 break;
205 }
206
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800207 if (qc->scsicmd)
208 ASSIGN_SAS_TASK(qc->scsicmd, task);
209
Darrick J. Wong338ec572006-10-18 14:43:37 -0700210 if (sas_ha->lldd_max_execute_num < 2)
211 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
212 else
213 res = sas_queue_up(task);
214
215 /* Examine */
216 if (res) {
217 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
218
Darrick J. Wongfe059f12007-01-30 01:18:55 -0800219 if (qc->scsicmd)
220 ASSIGN_SAS_TASK(qc->scsicmd, NULL);
Darrick J. Wong338ec572006-10-18 14:43:37 -0700221 sas_free_task(task);
Darrick J. Wong35a7f2f2007-01-30 01:18:38 -0800222 return AC_ERR_SYSTEM;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700223 }
224
Darrick J. Wong35a7f2f2007-01-30 01:18:38 -0800225 return 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700226}
227
228static u8 sas_ata_check_status(struct ata_port *ap)
229{
230 struct domain_device *dev = ap->private_data;
231 return dev->sata_dev.tf.command;
232}
233
234static void sas_ata_phy_reset(struct ata_port *ap)
235{
236 struct domain_device *dev = ap->private_data;
237 struct sas_internal *i =
238 to_sas_internal(dev->port->ha->core.shost->transportt);
239 int res = 0;
240
241 if (i->dft->lldd_I_T_nexus_reset)
242 res = i->dft->lldd_I_T_nexus_reset(dev);
243
244 if (res)
245 SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
246
247 switch (dev->sata_dev.command_set) {
248 case ATA_COMMAND_SET:
249 SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
250 ap->device[0].class = ATA_DEV_ATA;
251 break;
252 case ATAPI_COMMAND_SET:
253 SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
254 ap->device[0].class = ATA_DEV_ATAPI;
255 break;
256 default:
257 SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
258 __FUNCTION__,
259 dev->sata_dev.command_set);
Darrick J. Wong5986c3d2007-01-30 01:18:52 -0800260 ap->device[0].class = ATA_DEV_UNKNOWN;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700261 break;
262 }
263
264 ap->cbl = ATA_CBL_SATA;
265}
266
267static void sas_ata_post_internal(struct ata_queued_cmd *qc)
268{
269 if (qc->flags & ATA_QCFLAG_FAILED)
270 qc->err_mask |= AC_ERR_OTHER;
271
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800272 if (qc->err_mask) {
273 /*
274 * Find the sas_task and kill it. By this point,
275 * libata has decided to kill the qc, so we needn't
276 * bother with sas_ata_task_done. But we still
277 * ought to abort the task.
278 */
279 struct sas_task *task = qc->lldd_task;
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800280 unsigned long flags;
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800281
282 qc->lldd_task = NULL;
283 if (task) {
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800284 /* Should this be a AT(API) device reset? */
285 spin_lock_irqsave(&task->task_state_lock, flags);
286 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
287 spin_unlock_irqrestore(&task->task_state_lock, flags);
288
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800289 task->uldd_task = NULL;
290 __sas_task_abort(task);
291 }
Darrick J. Wong1c50dc82007-01-30 01:18:41 -0800292 }
Darrick J. Wong338ec572006-10-18 14:43:37 -0700293}
294
295static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
296{
297 struct domain_device *dev = ap->private_data;
298 memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
299}
300
James Bottomley110dd8f2007-07-20 13:11:44 -0500301static int sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
Darrick J. Wong338ec572006-10-18 14:43:37 -0700302 u32 val)
303{
304 struct domain_device *dev = ap->private_data;
305
306 SAS_DPRINTK("STUB %s\n", __FUNCTION__);
307 switch (sc_reg_in) {
308 case SCR_STATUS:
309 dev->sata_dev.sstatus = val;
310 break;
311 case SCR_CONTROL:
312 dev->sata_dev.scontrol = val;
313 break;
314 case SCR_ERROR:
315 dev->sata_dev.serror = val;
316 break;
317 case SCR_ACTIVE:
318 dev->sata_dev.ap->sactive = val;
319 break;
James Bottomley110dd8f2007-07-20 13:11:44 -0500320 default:
321 return -EINVAL;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700322 }
James Bottomley110dd8f2007-07-20 13:11:44 -0500323 return 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700324}
325
James Bottomley110dd8f2007-07-20 13:11:44 -0500326static int sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
327 u32 *val)
Darrick J. Wong338ec572006-10-18 14:43:37 -0700328{
329 struct domain_device *dev = ap->private_data;
330
331 SAS_DPRINTK("STUB %s\n", __FUNCTION__);
332 switch (sc_reg_in) {
333 case SCR_STATUS:
James Bottomley110dd8f2007-07-20 13:11:44 -0500334 *val = dev->sata_dev.sstatus;
335 return 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700336 case SCR_CONTROL:
James Bottomley110dd8f2007-07-20 13:11:44 -0500337 *val = dev->sata_dev.scontrol;
338 return 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700339 case SCR_ERROR:
James Bottomley110dd8f2007-07-20 13:11:44 -0500340 *val = dev->sata_dev.serror;
341 return 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700342 case SCR_ACTIVE:
James Bottomley110dd8f2007-07-20 13:11:44 -0500343 *val = dev->sata_dev.ap->sactive;
344 return 0;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700345 default:
James Bottomley110dd8f2007-07-20 13:11:44 -0500346 return -EINVAL;
Darrick J. Wong338ec572006-10-18 14:43:37 -0700347 }
348}
349
350static struct ata_port_operations sas_sata_ops = {
351 .port_disable = ata_port_disable,
352 .check_status = sas_ata_check_status,
353 .check_altstatus = sas_ata_check_status,
354 .dev_select = ata_noop_dev_select,
355 .phy_reset = sas_ata_phy_reset,
356 .post_internal_cmd = sas_ata_post_internal,
357 .tf_read = sas_ata_tf_read,
358 .qc_prep = ata_noop_qc_prep,
359 .qc_issue = sas_ata_qc_issue,
360 .port_start = ata_sas_port_start,
361 .port_stop = ata_sas_port_stop,
362 .scr_read = sas_ata_scr_read,
363 .scr_write = sas_ata_scr_write
364};
365
366static struct ata_port_info sata_port_info = {
367 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
368 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
369 .pio_mask = 0x1f, /* PIO0-4 */
370 .mwdma_mask = 0x07, /* MWDMA0-2 */
371 .udma_mask = ATA_UDMA6,
372 .port_ops = &sas_sata_ops
373};
374
375int sas_ata_init_host_and_port(struct domain_device *found_dev,
376 struct scsi_target *starget)
377{
378 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
379 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
380 struct ata_port *ap;
381
382 ata_host_init(&found_dev->sata_dev.ata_host,
383 &ha->pcidev->dev,
384 sata_port_info.flags,
385 &sas_sata_ops);
386 ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
387 &sata_port_info,
388 shost);
389 if (!ap) {
390 SAS_DPRINTK("ata_sas_port_alloc failed.\n");
391 return -ENODEV;
392 }
393
394 ap->private_data = found_dev;
395 ap->cbl = ATA_CBL_SATA;
396 ap->scsi_host = shost;
397 found_dev->sata_dev.ap = ap;
398
399 return 0;
400}
Darrick J. Wong3a2755a2007-01-30 01:18:58 -0800401
402void sas_ata_task_abort(struct sas_task *task)
403{
404 struct ata_queued_cmd *qc = task->uldd_task;
405 struct completion *waiting;
406
407 /* Bounce SCSI-initiated commands to the SCSI EH */
408 if (qc->scsicmd) {
409 scsi_req_abort_cmd(qc->scsicmd);
410 scsi_schedule_eh(qc->scsicmd->device->host);
411 return;
412 }
413
414 /* Internal command, fake a timeout and complete. */
415 qc->flags &= ~ATA_QCFLAG_ACTIVE;
416 qc->flags |= ATA_QCFLAG_FAILED;
417 qc->err_mask |= AC_ERR_TIMEOUT;
418 waiting = qc->private_data;
419 complete(waiting);
420}