[PATCH] libata: ata_scsi_queuecmd cleanup
[linux-2.6/zen-sources.git] / drivers / scsi / libata-scsi.c
blob8192ddd62974e892f4a4f058f873f8b5b19cbab0
1 /*
2 * libata-scsi.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
36 #include <linux/kernel.h>
37 #include <linux/blkdev.h>
38 #include <linux/spinlock.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_request.h>
44 #include <scsi/scsi_transport.h>
45 #include <linux/libata.h>
46 #include <linux/hdreg.h>
47 #include <asm/uaccess.h>
49 #include "libata.h"
51 #define SECTOR_SIZE 512
53 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
54 static struct ata_device *
55 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
56 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd);
58 #define RW_RECOVERY_MPAGE 0x1
59 #define RW_RECOVERY_MPAGE_LEN 12
60 #define CACHE_MPAGE 0x8
61 #define CACHE_MPAGE_LEN 20
62 #define CONTROL_MPAGE 0xa
63 #define CONTROL_MPAGE_LEN 12
64 #define ALL_MPAGES 0x3f
65 #define ALL_SUB_MPAGES 0xff
68 static const u8 def_rw_recovery_mpage[] = {
69 RW_RECOVERY_MPAGE,
70 RW_RECOVERY_MPAGE_LEN - 2,
71 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
72 (1 << 6), /* ARRE (auto read reallocation) */
73 0, /* read retry count */
74 0, 0, 0, 0,
75 0, /* write retry count */
76 0, 0, 0
79 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
80 CACHE_MPAGE,
81 CACHE_MPAGE_LEN - 2,
82 0, /* contains WCE, needs to be 0 for logic */
83 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, /* contains DRA, needs to be 0 for logic */
85 0, 0, 0, 0, 0, 0, 0
88 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
89 CONTROL_MPAGE,
90 CONTROL_MPAGE_LEN - 2,
91 2, /* DSENSE=0, GLTSD=1 */
92 0, /* [QAM+QERR may be 1, see 05-359r1] */
93 0, 0, 0, 0, 0xff, 0xff,
94 0, 30 /* extended self test time, see 05-359r1 */
98 * libata transport template. libata doesn't do real transport stuff.
99 * It just needs the eh_timed_out hook.
101 struct scsi_transport_template ata_scsi_transport_template = {
102 .eh_timed_out = ata_scsi_timed_out,
106 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
107 void (*done)(struct scsi_cmnd *))
109 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
110 /* "Invalid field in cbd" */
111 done(cmd);
115 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
116 * @sdev: SCSI device for which BIOS geometry is to be determined
117 * @bdev: block device associated with @sdev
118 * @capacity: capacity of SCSI device
119 * @geom: location to which geometry will be output
121 * Generic bios head/sector/cylinder calculator
122 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
123 * mapping. Some situations may arise where the disk is not
124 * bootable if this is not used.
126 * LOCKING:
127 * Defined by the SCSI layer. We don't really care.
129 * RETURNS:
130 * Zero.
132 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
133 sector_t capacity, int geom[])
135 geom[0] = 255;
136 geom[1] = 63;
137 sector_div(capacity, 255*63);
138 geom[2] = capacity;
140 return 0;
144 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
145 * @scsidev: Device to which we are issuing command
146 * @arg: User provided data for issuing command
148 * LOCKING:
149 * Defined by the SCSI layer. We don't really care.
151 * RETURNS:
152 * Zero on success, negative errno on error.
155 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
157 int rc = 0;
158 u8 scsi_cmd[MAX_COMMAND_SIZE];
159 u8 args[4], *argbuf = NULL;
160 int argsize = 0;
161 struct scsi_sense_hdr sshdr;
162 enum dma_data_direction data_dir;
164 if (arg == NULL)
165 return -EINVAL;
167 if (copy_from_user(args, arg, sizeof(args)))
168 return -EFAULT;
170 memset(scsi_cmd, 0, sizeof(scsi_cmd));
172 if (args[3]) {
173 argsize = SECTOR_SIZE * args[3];
174 argbuf = kmalloc(argsize, GFP_KERNEL);
175 if (argbuf == NULL) {
176 rc = -ENOMEM;
177 goto error;
180 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
181 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
182 block count in sector count field */
183 data_dir = DMA_FROM_DEVICE;
184 } else {
185 scsi_cmd[1] = (3 << 1); /* Non-data */
186 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
187 data_dir = DMA_NONE;
190 scsi_cmd[0] = ATA_16;
192 scsi_cmd[4] = args[2];
193 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
194 scsi_cmd[6] = args[3];
195 scsi_cmd[8] = args[1];
196 scsi_cmd[10] = 0x4f;
197 scsi_cmd[12] = 0xc2;
198 } else {
199 scsi_cmd[6] = args[1];
201 scsi_cmd[14] = args[0];
203 /* Good values for timeout and retries? Values below
204 from scsi_ioctl_send_command() for default case... */
205 if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
206 &sshdr, (10*HZ), 5)) {
207 rc = -EIO;
208 goto error;
211 /* Need code to retrieve data from check condition? */
213 if ((argbuf)
214 && copy_to_user(arg + sizeof(args), argbuf, argsize))
215 rc = -EFAULT;
216 error:
217 if (argbuf)
218 kfree(argbuf);
220 return rc;
224 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
225 * @scsidev: Device to which we are issuing command
226 * @arg: User provided data for issuing command
228 * LOCKING:
229 * Defined by the SCSI layer. We don't really care.
231 * RETURNS:
232 * Zero on success, negative errno on error.
234 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
236 int rc = 0;
237 u8 scsi_cmd[MAX_COMMAND_SIZE];
238 u8 args[7];
239 struct scsi_sense_hdr sshdr;
241 if (arg == NULL)
242 return -EINVAL;
244 if (copy_from_user(args, arg, sizeof(args)))
245 return -EFAULT;
247 memset(scsi_cmd, 0, sizeof(scsi_cmd));
248 scsi_cmd[0] = ATA_16;
249 scsi_cmd[1] = (3 << 1); /* Non-data */
250 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
251 scsi_cmd[4] = args[1];
252 scsi_cmd[6] = args[2];
253 scsi_cmd[8] = args[3];
254 scsi_cmd[10] = args[4];
255 scsi_cmd[12] = args[5];
256 scsi_cmd[14] = args[0];
258 /* Good values for timeout and retries? Values below
259 from scsi_ioctl_send_command() for default case... */
260 if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
261 (10*HZ), 5))
262 rc = -EIO;
264 /* Need code to retrieve data from check condition? */
265 return rc;
268 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
270 struct ata_port *ap;
271 struct ata_device *dev;
272 int val = -EINVAL, rc = -EINVAL;
274 ap = (struct ata_port *) &scsidev->host->hostdata[0];
275 if (!ap)
276 goto out;
278 dev = ata_scsi_find_dev(ap, scsidev);
279 if (!dev) {
280 rc = -ENODEV;
281 goto out;
284 switch (cmd) {
285 case ATA_IOC_GET_IO32:
286 val = 0;
287 if (copy_to_user(arg, &val, 1))
288 return -EFAULT;
289 return 0;
291 case ATA_IOC_SET_IO32:
292 val = (unsigned long) arg;
293 if (val != 0)
294 return -EINVAL;
295 return 0;
297 case HDIO_DRIVE_CMD:
298 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
299 return -EACCES;
300 return ata_cmd_ioctl(scsidev, arg);
302 case HDIO_DRIVE_TASK:
303 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
304 return -EACCES;
305 return ata_task_ioctl(scsidev, arg);
307 default:
308 rc = -ENOTTY;
309 break;
312 out:
313 return rc;
317 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
318 * @ap: ATA port to which the new command is attached
319 * @dev: ATA device to which the new command is attached
320 * @cmd: SCSI command that originated this ATA command
321 * @done: SCSI command completion function
323 * Obtain a reference to an unused ata_queued_cmd structure,
324 * which is the basic libata structure representing a single
325 * ATA command sent to the hardware.
327 * If a command was available, fill in the SCSI-specific
328 * portions of the structure with information on the
329 * current command.
331 * LOCKING:
332 * spin_lock_irqsave(host_set lock)
334 * RETURNS:
335 * Command allocated, or %NULL if none available.
337 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
338 struct ata_device *dev,
339 struct scsi_cmnd *cmd,
340 void (*done)(struct scsi_cmnd *))
342 struct ata_queued_cmd *qc;
344 qc = ata_qc_new_init(ap, dev);
345 if (qc) {
346 qc->scsicmd = cmd;
347 qc->scsidone = done;
349 if (cmd->use_sg) {
350 qc->__sg = (struct scatterlist *) cmd->request_buffer;
351 qc->n_elem = cmd->use_sg;
352 } else {
353 qc->__sg = &qc->sgent;
354 qc->n_elem = 1;
356 } else {
357 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
358 done(cmd);
361 return qc;
365 * ata_dump_status - user friendly display of error info
366 * @id: id of the port in question
367 * @tf: ptr to filled out taskfile
369 * Decode and dump the ATA error/status registers for the user so
370 * that they have some idea what really happened at the non
371 * make-believe layer.
373 * LOCKING:
374 * inherited from caller
376 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
378 u8 stat = tf->command, err = tf->feature;
380 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
381 if (stat & ATA_BUSY) {
382 printk("Busy }\n"); /* Data is not valid in this case */
383 } else {
384 if (stat & 0x40) printk("DriveReady ");
385 if (stat & 0x20) printk("DeviceFault ");
386 if (stat & 0x10) printk("SeekComplete ");
387 if (stat & 0x08) printk("DataRequest ");
388 if (stat & 0x04) printk("CorrectedError ");
389 if (stat & 0x02) printk("Index ");
390 if (stat & 0x01) printk("Error ");
391 printk("}\n");
393 if (err) {
394 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
395 if (err & 0x04) printk("DriveStatusError ");
396 if (err & 0x80) {
397 if (err & 0x04) printk("BadCRC ");
398 else printk("Sector ");
400 if (err & 0x40) printk("UncorrectableError ");
401 if (err & 0x10) printk("SectorIdNotFound ");
402 if (err & 0x02) printk("TrackZeroNotFound ");
403 if (err & 0x01) printk("AddrMarkNotFound ");
404 printk("}\n");
409 int ata_scsi_device_resume(struct scsi_device *sdev)
411 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
412 struct ata_device *dev = &ap->device[sdev->id];
414 return ata_device_resume(ap, dev);
417 int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
419 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
420 struct ata_device *dev = &ap->device[sdev->id];
422 return ata_device_suspend(ap, dev, state);
426 * ata_to_sense_error - convert ATA error to SCSI error
427 * @id: ATA device number
428 * @drv_stat: value contained in ATA status register
429 * @drv_err: value contained in ATA error register
430 * @sk: the sense key we'll fill out
431 * @asc: the additional sense code we'll fill out
432 * @ascq: the additional sense code qualifier we'll fill out
434 * Converts an ATA error into a SCSI error. Fill out pointers to
435 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
436 * format sense blocks.
438 * LOCKING:
439 * spin_lock_irqsave(host_set lock)
441 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
442 u8 *ascq)
444 int i;
446 /* Based on the 3ware driver translation table */
447 static const unsigned char sense_table[][4] = {
448 /* BBD|ECC|ID|MAR */
449 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
450 /* BBD|ECC|ID */
451 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
452 /* ECC|MC|MARK */
453 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
454 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
455 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
456 /* MC|ID|ABRT|TRK0|MARK */
457 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
458 /* MCR|MARK */
459 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
460 /* Bad address mark */
461 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
462 /* TRK0 */
463 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
464 /* Abort & !ICRC */
465 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
466 /* Media change request */
467 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
468 /* SRV */
469 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
470 /* Media change */
471 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
472 /* ECC */
473 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
474 /* BBD - block marked bad */
475 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
476 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
478 static const unsigned char stat_table[][4] = {
479 /* Must be first because BUSY means no other bits valid */
480 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
481 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
482 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
483 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
484 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
488 * Is this an error we can process/parse
490 if (drv_stat & ATA_BUSY) {
491 drv_err = 0; /* Ignore the err bits, they're invalid */
494 if (drv_err) {
495 /* Look for drv_err */
496 for (i = 0; sense_table[i][0] != 0xFF; i++) {
497 /* Look for best matches first */
498 if ((sense_table[i][0] & drv_err) ==
499 sense_table[i][0]) {
500 *sk = sense_table[i][1];
501 *asc = sense_table[i][2];
502 *ascq = sense_table[i][3];
503 goto translate_done;
506 /* No immediate match */
507 printk(KERN_WARNING "ata%u: no sense translation for "
508 "error 0x%02x\n", id, drv_err);
511 /* Fall back to interpreting status bits */
512 for (i = 0; stat_table[i][0] != 0xFF; i++) {
513 if (stat_table[i][0] & drv_stat) {
514 *sk = stat_table[i][1];
515 *asc = stat_table[i][2];
516 *ascq = stat_table[i][3];
517 goto translate_done;
520 /* No error? Undecoded? */
521 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
522 id, drv_stat);
524 /* We need a sensible error return here, which is tricky, and one
525 that won't cause people to do things like return a disk wrongly */
526 *sk = ABORTED_COMMAND;
527 *asc = 0x00;
528 *ascq = 0x00;
530 translate_done:
531 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
532 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
533 *sk, *asc, *ascq);
534 return;
538 * ata_gen_ata_desc_sense - Generate check condition sense block.
539 * @qc: Command that completed.
541 * This function is specific to the ATA descriptor format sense
542 * block specified for the ATA pass through commands. Regardless
543 * of whether the command errored or not, return a sense
544 * block. Copy all controller registers into the sense
545 * block. Clear sense key, ASC & ASCQ if there is no error.
547 * LOCKING:
548 * spin_lock_irqsave(host_set lock)
550 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
552 struct scsi_cmnd *cmd = qc->scsicmd;
553 struct ata_taskfile *tf = &qc->tf;
554 unsigned char *sb = cmd->sense_buffer;
555 unsigned char *desc = sb + 8;
557 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
559 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
562 * Read the controller registers.
564 WARN_ON(qc->ap->ops->tf_read == NULL);
565 qc->ap->ops->tf_read(qc->ap, tf);
568 * Use ata_to_sense_error() to map status register bits
569 * onto sense key, asc & ascq.
571 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
572 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
573 &sb[1], &sb[2], &sb[3]);
574 sb[1] &= 0x0f;
578 * Sense data is current and format is descriptor.
580 sb[0] = 0x72;
582 desc[0] = 0x09;
585 * Set length of additional sense data.
586 * Since we only populate descriptor 0, the total
587 * length is the same (fixed) length as descriptor 0.
589 desc[1] = sb[7] = 14;
592 * Copy registers into sense buffer.
594 desc[2] = 0x00;
595 desc[3] = tf->feature; /* == error reg */
596 desc[5] = tf->nsect;
597 desc[7] = tf->lbal;
598 desc[9] = tf->lbam;
599 desc[11] = tf->lbah;
600 desc[12] = tf->device;
601 desc[13] = tf->command; /* == status reg */
604 * Fill in Extend bit, and the high order bytes
605 * if applicable.
607 if (tf->flags & ATA_TFLAG_LBA48) {
608 desc[2] |= 0x01;
609 desc[4] = tf->hob_nsect;
610 desc[6] = tf->hob_lbal;
611 desc[8] = tf->hob_lbam;
612 desc[10] = tf->hob_lbah;
617 * ata_gen_fixed_sense - generate a SCSI fixed sense block
618 * @qc: Command that we are erroring out
620 * Leverage ata_to_sense_error() to give us the codes. Fit our
621 * LBA in here if there's room.
623 * LOCKING:
624 * inherited from caller
626 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
628 struct scsi_cmnd *cmd = qc->scsicmd;
629 struct ata_taskfile *tf = &qc->tf;
630 unsigned char *sb = cmd->sense_buffer;
632 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
634 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
637 * Read the controller registers.
639 WARN_ON(qc->ap->ops->tf_read == NULL);
640 qc->ap->ops->tf_read(qc->ap, tf);
643 * Use ata_to_sense_error() to map status register bits
644 * onto sense key, asc & ascq.
646 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
647 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
648 &sb[2], &sb[12], &sb[13]);
649 sb[2] &= 0x0f;
652 sb[0] = 0x70;
653 sb[7] = 0x0a;
655 if (tf->flags & ATA_TFLAG_LBA48) {
656 /* TODO: find solution for LBA48 descriptors */
659 else if (tf->flags & ATA_TFLAG_LBA) {
660 /* A small (28b) LBA will fit in the 32b info field */
661 sb[0] |= 0x80; /* set valid bit */
662 sb[3] = tf->device & 0x0f;
663 sb[4] = tf->lbah;
664 sb[5] = tf->lbam;
665 sb[6] = tf->lbal;
668 else {
669 /* TODO: C/H/S */
673 static void ata_scsi_sdev_config(struct scsi_device *sdev)
675 sdev->use_10_for_rw = 1;
676 sdev->use_10_for_ms = 1;
679 static void ata_scsi_dev_config(struct scsi_device *sdev,
680 struct ata_device *dev)
682 unsigned int max_sectors;
684 /* TODO: 2048 is an arbitrary number, not the
685 * hardware maximum. This should be increased to
686 * 65534 when Jens Axboe's patch for dynamically
687 * determining max_sectors is merged.
689 max_sectors = ATA_MAX_SECTORS;
690 if (dev->flags & ATA_DFLAG_LBA48)
691 max_sectors = 2048;
692 if (dev->max_sectors)
693 max_sectors = dev->max_sectors;
695 blk_queue_max_sectors(sdev->request_queue, max_sectors);
698 * SATA DMA transfers must be multiples of 4 byte, so
699 * we need to pad ATAPI transfers using an extra sg.
700 * Decrement max hw segments accordingly.
702 if (dev->class == ATA_DEV_ATAPI) {
703 request_queue_t *q = sdev->request_queue;
704 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
709 * ata_scsi_slave_config - Set SCSI device attributes
710 * @sdev: SCSI device to examine
712 * This is called before we actually start reading
713 * and writing to the device, to configure certain
714 * SCSI mid-layer behaviors.
716 * LOCKING:
717 * Defined by SCSI layer. We don't really care.
720 int ata_scsi_slave_config(struct scsi_device *sdev)
722 ata_scsi_sdev_config(sdev);
724 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
726 if (sdev->id < ATA_MAX_DEVICES) {
727 struct ata_port *ap;
728 struct ata_device *dev;
730 ap = (struct ata_port *) &sdev->host->hostdata[0];
731 dev = &ap->device[sdev->id];
733 ata_scsi_dev_config(sdev, dev);
736 return 0; /* scsi layer doesn't check return value, sigh */
740 * ata_scsi_timed_out - SCSI layer time out callback
741 * @cmd: timed out SCSI command
743 * Handles SCSI layer timeout. We race with normal completion of
744 * the qc for @cmd. If the qc is already gone, we lose and let
745 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
746 * timed out and EH should be invoked. Prevent ata_qc_complete()
747 * from finishing it by setting EH_SCHEDULED and return
748 * EH_NOT_HANDLED.
750 * LOCKING:
751 * Called from timer context
753 * RETURNS:
754 * EH_HANDLED or EH_NOT_HANDLED
756 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
758 struct Scsi_Host *host = cmd->device->host;
759 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
760 unsigned long flags;
761 struct ata_queued_cmd *qc;
762 enum scsi_eh_timer_return ret = EH_HANDLED;
764 DPRINTK("ENTER\n");
766 spin_lock_irqsave(&ap->host_set->lock, flags);
767 qc = ata_qc_from_tag(ap, ap->active_tag);
768 if (qc) {
769 WARN_ON(qc->scsicmd != cmd);
770 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
771 qc->err_mask |= AC_ERR_TIMEOUT;
772 ret = EH_NOT_HANDLED;
774 spin_unlock_irqrestore(&ap->host_set->lock, flags);
776 DPRINTK("EXIT, ret=%d\n", ret);
777 return ret;
781 * ata_scsi_error - SCSI layer error handler callback
782 * @host: SCSI host on which error occurred
784 * Handles SCSI-layer-thrown error events.
786 * LOCKING:
787 * Inherited from SCSI layer (none, can sleep)
789 * RETURNS:
790 * Zero.
793 int ata_scsi_error(struct Scsi_Host *host)
795 struct ata_port *ap;
796 unsigned long flags;
798 DPRINTK("ENTER\n");
800 ap = (struct ata_port *) &host->hostdata[0];
802 spin_lock_irqsave(&ap->host_set->lock, flags);
803 WARN_ON(ap->flags & ATA_FLAG_IN_EH);
804 ap->flags |= ATA_FLAG_IN_EH;
805 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
806 spin_unlock_irqrestore(&ap->host_set->lock, flags);
808 ata_port_flush_task(ap);
810 ap->ops->eng_timeout(ap);
812 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
814 scsi_eh_flush_done_q(&ap->eh_done_q);
816 spin_lock_irqsave(&ap->host_set->lock, flags);
817 ap->flags &= ~ATA_FLAG_IN_EH;
818 spin_unlock_irqrestore(&ap->host_set->lock, flags);
820 DPRINTK("EXIT\n");
821 return 0;
824 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
826 /* nada */
829 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
831 struct ata_port *ap = qc->ap;
832 struct scsi_cmnd *scmd = qc->scsicmd;
833 unsigned long flags;
835 spin_lock_irqsave(&ap->host_set->lock, flags);
836 qc->scsidone = ata_eh_scsidone;
837 __ata_qc_complete(qc);
838 WARN_ON(ata_tag_valid(qc->tag));
839 spin_unlock_irqrestore(&ap->host_set->lock, flags);
841 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
845 * ata_eh_qc_complete - Complete an active ATA command from EH
846 * @qc: Command to complete
848 * Indicate to the mid and upper layers that an ATA command has
849 * completed. To be used from EH.
851 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
853 struct scsi_cmnd *scmd = qc->scsicmd;
854 scmd->retries = scmd->allowed;
855 __ata_eh_qc_complete(qc);
859 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
860 * @qc: Command to retry
862 * Indicate to the mid and upper layers that an ATA command
863 * should be retried. To be used from EH.
865 * SCSI midlayer limits the number of retries to scmd->allowed.
866 * This function might need to adjust scmd->retries for commands
867 * which get retried due to unrelated NCQ failures.
869 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
871 __ata_eh_qc_complete(qc);
875 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
876 * @qc: Storage for translated ATA taskfile
877 * @scsicmd: SCSI command to translate
879 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
880 * (to start). Perhaps these commands should be preceded by
881 * CHECK POWER MODE to see what power mode the device is already in.
882 * [See SAT revision 5 at www.t10.org]
884 * LOCKING:
885 * spin_lock_irqsave(host_set lock)
887 * RETURNS:
888 * Zero on success, non-zero on error.
891 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
892 const u8 *scsicmd)
894 struct ata_taskfile *tf = &qc->tf;
896 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
897 tf->protocol = ATA_PROT_NODATA;
898 if (scsicmd[1] & 0x1) {
899 ; /* ignore IMMED bit, violates sat-r05 */
901 if (scsicmd[4] & 0x2)
902 goto invalid_fld; /* LOEJ bit set not supported */
903 if (((scsicmd[4] >> 4) & 0xf) != 0)
904 goto invalid_fld; /* power conditions not supported */
905 if (scsicmd[4] & 0x1) {
906 tf->nsect = 1; /* 1 sector, lba=0 */
908 if (qc->dev->flags & ATA_DFLAG_LBA) {
909 qc->tf.flags |= ATA_TFLAG_LBA;
911 tf->lbah = 0x0;
912 tf->lbam = 0x0;
913 tf->lbal = 0x0;
914 tf->device |= ATA_LBA;
915 } else {
916 /* CHS */
917 tf->lbal = 0x1; /* sect */
918 tf->lbam = 0x0; /* cyl low */
919 tf->lbah = 0x0; /* cyl high */
922 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
923 } else {
924 tf->nsect = 0; /* time period value (0 implies now) */
925 tf->command = ATA_CMD_STANDBY;
926 /* Consider: ATA STANDBY IMMEDIATE command */
929 * Standby and Idle condition timers could be implemented but that
930 * would require libata to implement the Power condition mode page
931 * and allow the user to change it. Changing mode pages requires
932 * MODE SELECT to be implemented.
935 return 0;
937 invalid_fld:
938 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
939 /* "Invalid field in cbd" */
940 return 1;
945 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
946 * @qc: Storage for translated ATA taskfile
947 * @scsicmd: SCSI command to translate (ignored)
949 * Sets up an ATA taskfile to issue FLUSH CACHE or
950 * FLUSH CACHE EXT.
952 * LOCKING:
953 * spin_lock_irqsave(host_set lock)
955 * RETURNS:
956 * Zero on success, non-zero on error.
959 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
961 struct ata_taskfile *tf = &qc->tf;
963 tf->flags |= ATA_TFLAG_DEVICE;
964 tf->protocol = ATA_PROT_NODATA;
966 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
967 (ata_id_has_flush_ext(qc->dev->id)))
968 tf->command = ATA_CMD_FLUSH_EXT;
969 else
970 tf->command = ATA_CMD_FLUSH;
972 return 0;
976 * scsi_6_lba_len - Get LBA and transfer length
977 * @scsicmd: SCSI command to translate
979 * Calculate LBA and transfer length for 6-byte commands.
981 * RETURNS:
982 * @plba: the LBA
983 * @plen: the transfer length
986 static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
988 u64 lba = 0;
989 u32 len = 0;
991 VPRINTK("six-byte command\n");
993 lba |= ((u64)scsicmd[2]) << 8;
994 lba |= ((u64)scsicmd[3]);
996 len |= ((u32)scsicmd[4]);
998 *plba = lba;
999 *plen = len;
1003 * scsi_10_lba_len - Get LBA and transfer length
1004 * @scsicmd: SCSI command to translate
1006 * Calculate LBA and transfer length for 10-byte commands.
1008 * RETURNS:
1009 * @plba: the LBA
1010 * @plen: the transfer length
1013 static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
1015 u64 lba = 0;
1016 u32 len = 0;
1018 VPRINTK("ten-byte command\n");
1020 lba |= ((u64)scsicmd[2]) << 24;
1021 lba |= ((u64)scsicmd[3]) << 16;
1022 lba |= ((u64)scsicmd[4]) << 8;
1023 lba |= ((u64)scsicmd[5]);
1025 len |= ((u32)scsicmd[7]) << 8;
1026 len |= ((u32)scsicmd[8]);
1028 *plba = lba;
1029 *plen = len;
1033 * scsi_16_lba_len - Get LBA and transfer length
1034 * @scsicmd: SCSI command to translate
1036 * Calculate LBA and transfer length for 16-byte commands.
1038 * RETURNS:
1039 * @plba: the LBA
1040 * @plen: the transfer length
1043 static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
1045 u64 lba = 0;
1046 u32 len = 0;
1048 VPRINTK("sixteen-byte command\n");
1050 lba |= ((u64)scsicmd[2]) << 56;
1051 lba |= ((u64)scsicmd[3]) << 48;
1052 lba |= ((u64)scsicmd[4]) << 40;
1053 lba |= ((u64)scsicmd[5]) << 32;
1054 lba |= ((u64)scsicmd[6]) << 24;
1055 lba |= ((u64)scsicmd[7]) << 16;
1056 lba |= ((u64)scsicmd[8]) << 8;
1057 lba |= ((u64)scsicmd[9]);
1059 len |= ((u32)scsicmd[10]) << 24;
1060 len |= ((u32)scsicmd[11]) << 16;
1061 len |= ((u32)scsicmd[12]) << 8;
1062 len |= ((u32)scsicmd[13]);
1064 *plba = lba;
1065 *plen = len;
1069 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1070 * @qc: Storage for translated ATA taskfile
1071 * @scsicmd: SCSI command to translate
1073 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1075 * LOCKING:
1076 * spin_lock_irqsave(host_set lock)
1078 * RETURNS:
1079 * Zero on success, non-zero on error.
1082 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1084 struct ata_taskfile *tf = &qc->tf;
1085 struct ata_device *dev = qc->dev;
1086 u64 dev_sectors = qc->dev->n_sectors;
1087 u64 block;
1088 u32 n_block;
1090 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1091 tf->protocol = ATA_PROT_NODATA;
1093 if (scsicmd[0] == VERIFY)
1094 scsi_10_lba_len(scsicmd, &block, &n_block);
1095 else if (scsicmd[0] == VERIFY_16)
1096 scsi_16_lba_len(scsicmd, &block, &n_block);
1097 else
1098 goto invalid_fld;
1100 if (!n_block)
1101 goto nothing_to_do;
1102 if (block >= dev_sectors)
1103 goto out_of_range;
1104 if ((block + n_block) > dev_sectors)
1105 goto out_of_range;
1107 if (dev->flags & ATA_DFLAG_LBA) {
1108 tf->flags |= ATA_TFLAG_LBA;
1110 if (lba_28_ok(block, n_block)) {
1111 /* use LBA28 */
1112 tf->command = ATA_CMD_VERIFY;
1113 tf->device |= (block >> 24) & 0xf;
1114 } else if (lba_48_ok(block, n_block)) {
1115 if (!(dev->flags & ATA_DFLAG_LBA48))
1116 goto out_of_range;
1118 /* use LBA48 */
1119 tf->flags |= ATA_TFLAG_LBA48;
1120 tf->command = ATA_CMD_VERIFY_EXT;
1122 tf->hob_nsect = (n_block >> 8) & 0xff;
1124 tf->hob_lbah = (block >> 40) & 0xff;
1125 tf->hob_lbam = (block >> 32) & 0xff;
1126 tf->hob_lbal = (block >> 24) & 0xff;
1127 } else
1128 /* request too large even for LBA48 */
1129 goto out_of_range;
1131 tf->nsect = n_block & 0xff;
1133 tf->lbah = (block >> 16) & 0xff;
1134 tf->lbam = (block >> 8) & 0xff;
1135 tf->lbal = block & 0xff;
1137 tf->device |= ATA_LBA;
1138 } else {
1139 /* CHS */
1140 u32 sect, head, cyl, track;
1142 if (!lba_28_ok(block, n_block))
1143 goto out_of_range;
1145 /* Convert LBA to CHS */
1146 track = (u32)block / dev->sectors;
1147 cyl = track / dev->heads;
1148 head = track % dev->heads;
1149 sect = (u32)block % dev->sectors + 1;
1151 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1152 (u32)block, track, cyl, head, sect);
1154 /* Check whether the converted CHS can fit.
1155 Cylinder: 0-65535
1156 Head: 0-15
1157 Sector: 1-255*/
1158 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1159 goto out_of_range;
1161 tf->command = ATA_CMD_VERIFY;
1162 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1163 tf->lbal = sect;
1164 tf->lbam = cyl;
1165 tf->lbah = cyl >> 8;
1166 tf->device |= head;
1169 return 0;
1171 invalid_fld:
1172 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1173 /* "Invalid field in cbd" */
1174 return 1;
1176 out_of_range:
1177 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1178 /* "Logical Block Address out of range" */
1179 return 1;
1181 nothing_to_do:
1182 qc->scsicmd->result = SAM_STAT_GOOD;
1183 return 1;
1187 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1188 * @qc: Storage for translated ATA taskfile
1189 * @scsicmd: SCSI command to translate
1191 * Converts any of six SCSI read/write commands into the
1192 * ATA counterpart, including starting sector (LBA),
1193 * sector count, and taking into account the device's LBA48
1194 * support.
1196 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1197 * %WRITE_16 are currently supported.
1199 * LOCKING:
1200 * spin_lock_irqsave(host_set lock)
1202 * RETURNS:
1203 * Zero on success, non-zero on error.
1206 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1208 struct ata_taskfile *tf = &qc->tf;
1209 struct ata_device *dev = qc->dev;
1210 u64 block;
1211 u32 n_block;
1213 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1215 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1216 scsicmd[0] == WRITE_16)
1217 tf->flags |= ATA_TFLAG_WRITE;
1219 /* Calculate the SCSI LBA, transfer length and FUA. */
1220 switch (scsicmd[0]) {
1221 case READ_10:
1222 case WRITE_10:
1223 scsi_10_lba_len(scsicmd, &block, &n_block);
1224 if (unlikely(scsicmd[1] & (1 << 3)))
1225 tf->flags |= ATA_TFLAG_FUA;
1226 break;
1227 case READ_6:
1228 case WRITE_6:
1229 scsi_6_lba_len(scsicmd, &block, &n_block);
1231 /* for 6-byte r/w commands, transfer length 0
1232 * means 256 blocks of data, not 0 block.
1234 if (!n_block)
1235 n_block = 256;
1236 break;
1237 case READ_16:
1238 case WRITE_16:
1239 scsi_16_lba_len(scsicmd, &block, &n_block);
1240 if (unlikely(scsicmd[1] & (1 << 3)))
1241 tf->flags |= ATA_TFLAG_FUA;
1242 break;
1243 default:
1244 DPRINTK("no-byte command\n");
1245 goto invalid_fld;
1248 /* Check and compose ATA command */
1249 if (!n_block)
1250 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1251 * length 0 means transfer 0 block of data.
1252 * However, for ATA R/W commands, sector count 0 means
1253 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1255 * WARNING: one or two older ATA drives treat 0 as 0...
1257 goto nothing_to_do;
1259 if (dev->flags & ATA_DFLAG_LBA) {
1260 tf->flags |= ATA_TFLAG_LBA;
1262 if (lba_28_ok(block, n_block)) {
1263 /* use LBA28 */
1264 tf->device |= (block >> 24) & 0xf;
1265 } else if (lba_48_ok(block, n_block)) {
1266 if (!(dev->flags & ATA_DFLAG_LBA48))
1267 goto out_of_range;
1269 /* use LBA48 */
1270 tf->flags |= ATA_TFLAG_LBA48;
1272 tf->hob_nsect = (n_block >> 8) & 0xff;
1274 tf->hob_lbah = (block >> 40) & 0xff;
1275 tf->hob_lbam = (block >> 32) & 0xff;
1276 tf->hob_lbal = (block >> 24) & 0xff;
1277 } else
1278 /* request too large even for LBA48 */
1279 goto out_of_range;
1281 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1282 goto invalid_fld;
1284 qc->nsect = n_block;
1285 tf->nsect = n_block & 0xff;
1287 tf->lbah = (block >> 16) & 0xff;
1288 tf->lbam = (block >> 8) & 0xff;
1289 tf->lbal = block & 0xff;
1291 tf->device |= ATA_LBA;
1292 } else {
1293 /* CHS */
1294 u32 sect, head, cyl, track;
1296 /* The request -may- be too large for CHS addressing. */
1297 if (!lba_28_ok(block, n_block))
1298 goto out_of_range;
1300 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1301 goto invalid_fld;
1303 /* Convert LBA to CHS */
1304 track = (u32)block / dev->sectors;
1305 cyl = track / dev->heads;
1306 head = track % dev->heads;
1307 sect = (u32)block % dev->sectors + 1;
1309 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1310 (u32)block, track, cyl, head, sect);
1312 /* Check whether the converted CHS can fit.
1313 Cylinder: 0-65535
1314 Head: 0-15
1315 Sector: 1-255*/
1316 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1317 goto out_of_range;
1319 qc->nsect = n_block;
1320 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1321 tf->lbal = sect;
1322 tf->lbam = cyl;
1323 tf->lbah = cyl >> 8;
1324 tf->device |= head;
1327 return 0;
1329 invalid_fld:
1330 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1331 /* "Invalid field in cbd" */
1332 return 1;
1334 out_of_range:
1335 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1336 /* "Logical Block Address out of range" */
1337 return 1;
1339 nothing_to_do:
1340 qc->scsicmd->result = SAM_STAT_GOOD;
1341 return 1;
1344 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1346 struct scsi_cmnd *cmd = qc->scsicmd;
1347 u8 *cdb = cmd->cmnd;
1348 int need_sense = (qc->err_mask != 0);
1350 /* For ATA pass thru (SAT) commands, generate a sense block if
1351 * user mandated it or if there's an error. Note that if we
1352 * generate because the user forced us to, a check condition
1353 * is generated and the ATA register values are returned
1354 * whether the command completed successfully or not. If there
1355 * was no error, SK, ASC and ASCQ will all be zero.
1357 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1358 ((cdb[2] & 0x20) || need_sense)) {
1359 ata_gen_ata_desc_sense(qc);
1360 } else {
1361 if (!need_sense) {
1362 cmd->result = SAM_STAT_GOOD;
1363 } else {
1364 /* TODO: decide which descriptor format to use
1365 * for 48b LBA devices and call that here
1366 * instead of the fixed desc, which is only
1367 * good for smaller LBA (and maybe CHS?)
1368 * devices.
1370 ata_gen_fixed_sense(qc);
1374 if (need_sense) {
1375 /* The ata_gen_..._sense routines fill in tf */
1376 ata_dump_status(qc->ap->id, &qc->tf);
1379 qc->scsidone(cmd);
1381 ata_qc_free(qc);
1385 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1386 * @ap: ATA port to which the command is addressed
1387 * @dev: ATA device to which the command is addressed
1388 * @cmd: SCSI command to execute
1389 * @done: SCSI command completion function
1390 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1392 * Our ->queuecommand() function has decided that the SCSI
1393 * command issued can be directly translated into an ATA
1394 * command, rather than handled internally.
1396 * This function sets up an ata_queued_cmd structure for the
1397 * SCSI command, and sends that ata_queued_cmd to the hardware.
1399 * The xlat_func argument (actor) returns 0 if ready to execute
1400 * ATA command, else 1 to finish translation. If 1 is returned
1401 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1402 * to be set reflecting an error condition or clean (early)
1403 * termination.
1405 * LOCKING:
1406 * spin_lock_irqsave(host_set lock)
1409 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1410 struct scsi_cmnd *cmd,
1411 void (*done)(struct scsi_cmnd *),
1412 ata_xlat_func_t xlat_func)
1414 struct ata_queued_cmd *qc;
1415 u8 *scsicmd = cmd->cmnd;
1417 VPRINTK("ENTER\n");
1419 qc = ata_scsi_qc_new(ap, dev, cmd, done);
1420 if (!qc)
1421 goto err_mem;
1423 /* data is present; dma-map it */
1424 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1425 cmd->sc_data_direction == DMA_TO_DEVICE) {
1426 if (unlikely(cmd->request_bufflen < 1)) {
1427 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1428 ap->id, dev->devno);
1429 goto err_did;
1432 if (cmd->use_sg)
1433 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1434 else
1435 ata_sg_init_one(qc, cmd->request_buffer,
1436 cmd->request_bufflen);
1438 qc->dma_dir = cmd->sc_data_direction;
1441 qc->complete_fn = ata_scsi_qc_complete;
1443 if (xlat_func(qc, scsicmd))
1444 goto early_finish;
1446 /* select device, send command to hardware */
1447 qc->err_mask = ata_qc_issue(qc);
1448 if (qc->err_mask)
1449 ata_qc_complete(qc);
1451 VPRINTK("EXIT\n");
1452 return;
1454 early_finish:
1455 ata_qc_free(qc);
1456 done(cmd);
1457 DPRINTK("EXIT - early finish (good or error)\n");
1458 return;
1460 err_did:
1461 ata_qc_free(qc);
1462 err_mem:
1463 cmd->result = (DID_ERROR << 16);
1464 done(cmd);
1465 DPRINTK("EXIT - internal\n");
1466 return;
1470 * ata_scsi_rbuf_get - Map response buffer.
1471 * @cmd: SCSI command containing buffer to be mapped.
1472 * @buf_out: Pointer to mapped area.
1474 * Maps buffer contained within SCSI command @cmd.
1476 * LOCKING:
1477 * spin_lock_irqsave(host_set lock)
1479 * RETURNS:
1480 * Length of response buffer.
1483 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1485 u8 *buf;
1486 unsigned int buflen;
1488 if (cmd->use_sg) {
1489 struct scatterlist *sg;
1491 sg = (struct scatterlist *) cmd->request_buffer;
1492 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1493 buflen = sg->length;
1494 } else {
1495 buf = cmd->request_buffer;
1496 buflen = cmd->request_bufflen;
1499 *buf_out = buf;
1500 return buflen;
1504 * ata_scsi_rbuf_put - Unmap response buffer.
1505 * @cmd: SCSI command containing buffer to be unmapped.
1506 * @buf: buffer to unmap
1508 * Unmaps response buffer contained within @cmd.
1510 * LOCKING:
1511 * spin_lock_irqsave(host_set lock)
1514 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1516 if (cmd->use_sg) {
1517 struct scatterlist *sg;
1519 sg = (struct scatterlist *) cmd->request_buffer;
1520 kunmap_atomic(buf - sg->offset, KM_USER0);
1525 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1526 * @args: device IDENTIFY data / SCSI command of interest.
1527 * @actor: Callback hook for desired SCSI command simulator
1529 * Takes care of the hard work of simulating a SCSI command...
1530 * Mapping the response buffer, calling the command's handler,
1531 * and handling the handler's return value. This return value
1532 * indicates whether the handler wishes the SCSI command to be
1533 * completed successfully (0), or not (in which case cmd->result
1534 * and sense buffer are assumed to be set).
1536 * LOCKING:
1537 * spin_lock_irqsave(host_set lock)
1540 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1541 unsigned int (*actor) (struct ata_scsi_args *args,
1542 u8 *rbuf, unsigned int buflen))
1544 u8 *rbuf;
1545 unsigned int buflen, rc;
1546 struct scsi_cmnd *cmd = args->cmd;
1548 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1549 memset(rbuf, 0, buflen);
1550 rc = actor(args, rbuf, buflen);
1551 ata_scsi_rbuf_put(cmd, rbuf);
1553 if (rc == 0)
1554 cmd->result = SAM_STAT_GOOD;
1555 args->done(cmd);
1559 * ata_scsiop_inq_std - Simulate INQUIRY command
1560 * @args: device IDENTIFY data / SCSI command of interest.
1561 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1562 * @buflen: Response buffer length.
1564 * Returns standard device identification data associated
1565 * with non-VPD INQUIRY command output.
1567 * LOCKING:
1568 * spin_lock_irqsave(host_set lock)
1571 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1572 unsigned int buflen)
1574 u8 hdr[] = {
1575 TYPE_DISK,
1577 0x5, /* claim SPC-3 version compatibility */
1579 95 - 4
1582 /* set scsi removeable (RMB) bit per ata bit */
1583 if (ata_id_removeable(args->id))
1584 hdr[1] |= (1 << 7);
1586 VPRINTK("ENTER\n");
1588 memcpy(rbuf, hdr, sizeof(hdr));
1590 if (buflen > 35) {
1591 memcpy(&rbuf[8], "ATA ", 8);
1592 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1593 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1594 if (rbuf[32] == 0 || rbuf[32] == ' ')
1595 memcpy(&rbuf[32], "n/a ", 4);
1598 if (buflen > 63) {
1599 const u8 versions[] = {
1600 0x60, /* SAM-3 (no version claimed) */
1602 0x03,
1603 0x20, /* SBC-2 (no version claimed) */
1605 0x02,
1606 0x60 /* SPC-3 (no version claimed) */
1609 memcpy(rbuf + 59, versions, sizeof(versions));
1612 return 0;
1616 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1617 * @args: device IDENTIFY data / SCSI command of interest.
1618 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1619 * @buflen: Response buffer length.
1621 * Returns list of inquiry VPD pages available.
1623 * LOCKING:
1624 * spin_lock_irqsave(host_set lock)
1627 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1628 unsigned int buflen)
1630 const u8 pages[] = {
1631 0x00, /* page 0x00, this page */
1632 0x80, /* page 0x80, unit serial no page */
1633 0x83 /* page 0x83, device ident page */
1635 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
1637 if (buflen > 6)
1638 memcpy(rbuf + 4, pages, sizeof(pages));
1640 return 0;
1644 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
1645 * @args: device IDENTIFY data / SCSI command of interest.
1646 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1647 * @buflen: Response buffer length.
1649 * Returns ATA device serial number.
1651 * LOCKING:
1652 * spin_lock_irqsave(host_set lock)
1655 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1656 unsigned int buflen)
1658 const u8 hdr[] = {
1660 0x80, /* this page code */
1662 ATA_SERNO_LEN, /* page len */
1664 memcpy(rbuf, hdr, sizeof(hdr));
1666 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1667 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1668 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1670 return 0;
1674 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
1675 * @args: device IDENTIFY data / SCSI command of interest.
1676 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1677 * @buflen: Response buffer length.
1679 * Yields two logical unit device identification designators:
1680 * - vendor specific ASCII containing the ATA serial number
1681 * - SAT defined "t10 vendor id based" containing ASCII vendor
1682 * name ("ATA "), model and serial numbers.
1684 * LOCKING:
1685 * spin_lock_irqsave(host_set lock)
1688 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1689 unsigned int buflen)
1691 int num;
1692 const int sat_model_serial_desc_len = 68;
1693 const int ata_model_byte_len = 40;
1695 rbuf[1] = 0x83; /* this page code */
1696 num = 4;
1698 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1699 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1700 rbuf[num + 0] = 2;
1701 rbuf[num + 3] = ATA_SERNO_LEN;
1702 num += 4;
1703 ata_id_string(args->id, (unsigned char *) rbuf + num,
1704 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1705 num += ATA_SERNO_LEN;
1707 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1708 /* SAT defined lu model and serial numbers descriptor */
1709 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
1710 rbuf[num + 0] = 2;
1711 rbuf[num + 1] = 1;
1712 rbuf[num + 3] = sat_model_serial_desc_len;
1713 num += 4;
1714 memcpy(rbuf + num, "ATA ", 8);
1715 num += 8;
1716 ata_id_string(args->id, (unsigned char *) rbuf + num,
1717 ATA_ID_PROD_OFS, ata_model_byte_len);
1718 num += ata_model_byte_len;
1719 ata_id_string(args->id, (unsigned char *) rbuf + num,
1720 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1721 num += ATA_SERNO_LEN;
1723 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
1724 return 0;
1728 * ata_scsiop_noop - Command handler that simply returns success.
1729 * @args: device IDENTIFY data / SCSI command of interest.
1730 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1731 * @buflen: Response buffer length.
1733 * No operation. Simply returns success to caller, to indicate
1734 * that the caller should successfully complete this SCSI command.
1736 * LOCKING:
1737 * spin_lock_irqsave(host_set lock)
1740 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1741 unsigned int buflen)
1743 VPRINTK("ENTER\n");
1744 return 0;
1748 * ata_msense_push - Push data onto MODE SENSE data output buffer
1749 * @ptr_io: (input/output) Location to store more output data
1750 * @last: End of output data buffer
1751 * @buf: Pointer to BLOB being added to output buffer
1752 * @buflen: Length of BLOB
1754 * Store MODE SENSE data on an output buffer.
1756 * LOCKING:
1757 * None.
1760 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1761 const u8 *buf, unsigned int buflen)
1763 u8 *ptr = *ptr_io;
1765 if ((ptr + buflen - 1) > last)
1766 return;
1768 memcpy(ptr, buf, buflen);
1770 ptr += buflen;
1772 *ptr_io = ptr;
1776 * ata_msense_caching - Simulate MODE SENSE caching info page
1777 * @id: device IDENTIFY data
1778 * @ptr_io: (input/output) Location to store more output data
1779 * @last: End of output data buffer
1781 * Generate a caching info page, which conditionally indicates
1782 * write caching to the SCSI layer, depending on device
1783 * capabilities.
1785 * LOCKING:
1786 * None.
1789 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1790 const u8 *last)
1792 u8 page[CACHE_MPAGE_LEN];
1794 memcpy(page, def_cache_mpage, sizeof(page));
1795 if (ata_id_wcache_enabled(id))
1796 page[2] |= (1 << 2); /* write cache enable */
1797 if (!ata_id_rahead_enabled(id))
1798 page[12] |= (1 << 5); /* disable read ahead */
1800 ata_msense_push(ptr_io, last, page, sizeof(page));
1801 return sizeof(page);
1805 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1806 * @dev: Device associated with this MODE SENSE command
1807 * @ptr_io: (input/output) Location to store more output data
1808 * @last: End of output data buffer
1810 * Generate a generic MODE SENSE control mode page.
1812 * LOCKING:
1813 * None.
1816 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1818 ata_msense_push(ptr_io, last, def_control_mpage,
1819 sizeof(def_control_mpage));
1820 return sizeof(def_control_mpage);
1824 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1825 * @dev: Device associated with this MODE SENSE command
1826 * @ptr_io: (input/output) Location to store more output data
1827 * @last: End of output data buffer
1829 * Generate a generic MODE SENSE r/w error recovery page.
1831 * LOCKING:
1832 * None.
1835 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1838 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1839 sizeof(def_rw_recovery_mpage));
1840 return sizeof(def_rw_recovery_mpage);
1844 * We can turn this into a real blacklist if it's needed, for now just
1845 * blacklist any Maxtor BANC1G10 revision firmware
1847 static int ata_dev_supports_fua(u16 *id)
1849 unsigned char model[41], fw[9];
1851 if (!libata_fua)
1852 return 0;
1853 if (!ata_id_has_fua(id))
1854 return 0;
1856 ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1857 ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
1859 if (strcmp(model, "Maxtor"))
1860 return 1;
1861 if (strcmp(fw, "BANC1G10"))
1862 return 1;
1864 return 0; /* blacklisted */
1868 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1869 * @args: device IDENTIFY data / SCSI command of interest.
1870 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1871 * @buflen: Response buffer length.
1873 * Simulate MODE SENSE commands. Assume this is invoked for direct
1874 * access devices (e.g. disks) only. There should be no block
1875 * descriptor for other device types.
1877 * LOCKING:
1878 * spin_lock_irqsave(host_set lock)
1881 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1882 unsigned int buflen)
1884 struct ata_device *dev = args->dev;
1885 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1886 const u8 sat_blk_desc[] = {
1887 0, 0, 0, 0, /* number of blocks: sat unspecified */
1889 0, 0x2, 0x0 /* block length: 512 bytes */
1891 u8 pg, spg;
1892 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
1893 u8 dpofua;
1895 VPRINTK("ENTER\n");
1897 six_byte = (scsicmd[0] == MODE_SENSE);
1898 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1900 * LLBA bit in msense(10) ignored (compliant)
1903 page_control = scsicmd[2] >> 6;
1904 switch (page_control) {
1905 case 0: /* current */
1906 break; /* supported */
1907 case 3: /* saved */
1908 goto saving_not_supp;
1909 case 1: /* changeable */
1910 case 2: /* defaults */
1911 default:
1912 goto invalid_fld;
1915 if (six_byte) {
1916 output_len = 4 + (ebd ? 8 : 0);
1917 alloc_len = scsicmd[4];
1918 } else {
1919 output_len = 8 + (ebd ? 8 : 0);
1920 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1922 minlen = (alloc_len < buflen) ? alloc_len : buflen;
1924 p = rbuf + output_len;
1925 last = rbuf + minlen - 1;
1927 pg = scsicmd[2] & 0x3f;
1928 spg = scsicmd[3];
1930 * No mode subpages supported (yet) but asking for _all_
1931 * subpages may be valid
1933 if (spg && (spg != ALL_SUB_MPAGES))
1934 goto invalid_fld;
1936 switch(pg) {
1937 case RW_RECOVERY_MPAGE:
1938 output_len += ata_msense_rw_recovery(&p, last);
1939 break;
1941 case CACHE_MPAGE:
1942 output_len += ata_msense_caching(args->id, &p, last);
1943 break;
1945 case CONTROL_MPAGE: {
1946 output_len += ata_msense_ctl_mode(&p, last);
1947 break;
1950 case ALL_MPAGES:
1951 output_len += ata_msense_rw_recovery(&p, last);
1952 output_len += ata_msense_caching(args->id, &p, last);
1953 output_len += ata_msense_ctl_mode(&p, last);
1954 break;
1956 default: /* invalid page code */
1957 goto invalid_fld;
1960 if (minlen < 1)
1961 return 0;
1963 dpofua = 0;
1964 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
1965 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1966 dpofua = 1 << 4;
1968 if (six_byte) {
1969 output_len--;
1970 rbuf[0] = output_len;
1971 if (minlen > 2)
1972 rbuf[2] |= dpofua;
1973 if (ebd) {
1974 if (minlen > 3)
1975 rbuf[3] = sizeof(sat_blk_desc);
1976 if (minlen > 11)
1977 memcpy(rbuf + 4, sat_blk_desc,
1978 sizeof(sat_blk_desc));
1980 } else {
1981 output_len -= 2;
1982 rbuf[0] = output_len >> 8;
1983 if (minlen > 1)
1984 rbuf[1] = output_len;
1985 if (minlen > 3)
1986 rbuf[3] |= dpofua;
1987 if (ebd) {
1988 if (minlen > 7)
1989 rbuf[7] = sizeof(sat_blk_desc);
1990 if (minlen > 15)
1991 memcpy(rbuf + 8, sat_blk_desc,
1992 sizeof(sat_blk_desc));
1995 return 0;
1997 invalid_fld:
1998 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1999 /* "Invalid field in cbd" */
2000 return 1;
2002 saving_not_supp:
2003 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2004 /* "Saving parameters not supported" */
2005 return 1;
2009 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2010 * @args: device IDENTIFY data / SCSI command of interest.
2011 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2012 * @buflen: Response buffer length.
2014 * Simulate READ CAPACITY commands.
2016 * LOCKING:
2017 * spin_lock_irqsave(host_set lock)
2020 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2021 unsigned int buflen)
2023 u64 n_sectors;
2024 u32 tmp;
2026 VPRINTK("ENTER\n");
2028 if (ata_id_has_lba(args->id)) {
2029 if (ata_id_has_lba48(args->id))
2030 n_sectors = ata_id_u64(args->id, 100);
2031 else
2032 n_sectors = ata_id_u32(args->id, 60);
2033 } else {
2034 /* CHS default translation */
2035 n_sectors = args->id[1] * args->id[3] * args->id[6];
2037 if (ata_id_current_chs_valid(args->id))
2038 /* CHS current translation */
2039 n_sectors = ata_id_u32(args->id, 57);
2042 n_sectors--; /* ATA TotalUserSectors - 1 */
2044 if (args->cmd->cmnd[0] == READ_CAPACITY) {
2045 if( n_sectors >= 0xffffffffULL )
2046 tmp = 0xffffffff ; /* Return max count on overflow */
2047 else
2048 tmp = n_sectors ;
2050 /* sector count, 32-bit */
2051 rbuf[0] = tmp >> (8 * 3);
2052 rbuf[1] = tmp >> (8 * 2);
2053 rbuf[2] = tmp >> (8 * 1);
2054 rbuf[3] = tmp;
2056 /* sector size */
2057 tmp = ATA_SECT_SIZE;
2058 rbuf[6] = tmp >> 8;
2059 rbuf[7] = tmp;
2061 } else {
2062 /* sector count, 64-bit */
2063 tmp = n_sectors >> (8 * 4);
2064 rbuf[2] = tmp >> (8 * 3);
2065 rbuf[3] = tmp >> (8 * 2);
2066 rbuf[4] = tmp >> (8 * 1);
2067 rbuf[5] = tmp;
2068 tmp = n_sectors;
2069 rbuf[6] = tmp >> (8 * 3);
2070 rbuf[7] = tmp >> (8 * 2);
2071 rbuf[8] = tmp >> (8 * 1);
2072 rbuf[9] = tmp;
2074 /* sector size */
2075 tmp = ATA_SECT_SIZE;
2076 rbuf[12] = tmp >> 8;
2077 rbuf[13] = tmp;
2080 return 0;
2084 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2085 * @args: device IDENTIFY data / SCSI command of interest.
2086 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2087 * @buflen: Response buffer length.
2089 * Simulate REPORT LUNS command.
2091 * LOCKING:
2092 * spin_lock_irqsave(host_set lock)
2095 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2096 unsigned int buflen)
2098 VPRINTK("ENTER\n");
2099 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2101 return 0;
2105 * ata_scsi_set_sense - Set SCSI sense data and status
2106 * @cmd: SCSI request to be handled
2107 * @sk: SCSI-defined sense key
2108 * @asc: SCSI-defined additional sense code
2109 * @ascq: SCSI-defined additional sense code qualifier
2111 * Helper function that builds a valid fixed format, current
2112 * response code and the given sense key (sk), additional sense
2113 * code (asc) and additional sense code qualifier (ascq) with
2114 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2115 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2117 * LOCKING:
2118 * Not required
2121 void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2123 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2125 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
2126 cmd->sense_buffer[2] = sk;
2127 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
2128 cmd->sense_buffer[12] = asc;
2129 cmd->sense_buffer[13] = ascq;
2133 * ata_scsi_badcmd - End a SCSI request with an error
2134 * @cmd: SCSI request to be handled
2135 * @done: SCSI command completion function
2136 * @asc: SCSI-defined additional sense code
2137 * @ascq: SCSI-defined additional sense code qualifier
2139 * Helper function that completes a SCSI command with
2140 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2141 * and the specified additional sense codes.
2143 * LOCKING:
2144 * spin_lock_irqsave(host_set lock)
2147 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2149 DPRINTK("ENTER\n");
2150 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
2152 done(cmd);
2155 static void atapi_sense_complete(struct ata_queued_cmd *qc)
2157 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0))
2158 /* FIXME: not quite right; we don't want the
2159 * translation of taskfile registers into
2160 * a sense descriptors, since that's only
2161 * correct for ATA, not ATAPI
2163 ata_gen_ata_desc_sense(qc);
2165 qc->scsidone(qc->scsicmd);
2166 ata_qc_free(qc);
2169 /* is it pointless to prefer PIO for "safety reasons"? */
2170 static inline int ata_pio_use_silly(struct ata_port *ap)
2172 return (ap->flags & ATA_FLAG_PIO_DMA);
2175 static void atapi_request_sense(struct ata_queued_cmd *qc)
2177 struct ata_port *ap = qc->ap;
2178 struct scsi_cmnd *cmd = qc->scsicmd;
2180 DPRINTK("ATAPI request sense\n");
2182 /* FIXME: is this needed? */
2183 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2185 ap->ops->tf_read(ap, &qc->tf);
2187 /* fill these in, for the case where they are -not- overwritten */
2188 cmd->sense_buffer[0] = 0x70;
2189 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2191 ata_qc_reinit(qc);
2193 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2194 qc->dma_dir = DMA_FROM_DEVICE;
2196 memset(&qc->cdb, 0, qc->dev->cdb_len);
2197 qc->cdb[0] = REQUEST_SENSE;
2198 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2200 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2201 qc->tf.command = ATA_CMD_PACKET;
2203 if (ata_pio_use_silly(ap)) {
2204 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2205 qc->tf.feature |= ATAPI_PKT_DMA;
2206 } else {
2207 qc->tf.protocol = ATA_PROT_ATAPI;
2208 qc->tf.lbam = (8 * 1024) & 0xff;
2209 qc->tf.lbah = (8 * 1024) >> 8;
2211 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2213 qc->complete_fn = atapi_sense_complete;
2215 qc->err_mask = ata_qc_issue(qc);
2216 if (qc->err_mask)
2217 ata_qc_complete(qc);
2219 DPRINTK("EXIT\n");
2222 static void atapi_qc_complete(struct ata_queued_cmd *qc)
2224 struct scsi_cmnd *cmd = qc->scsicmd;
2225 unsigned int err_mask = qc->err_mask;
2227 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2229 if (unlikely(err_mask & AC_ERR_DEV)) {
2230 cmd->result = SAM_STAT_CHECK_CONDITION;
2231 atapi_request_sense(qc);
2232 return;
2235 else if (unlikely(err_mask))
2236 /* FIXME: not quite right; we don't want the
2237 * translation of taskfile registers into
2238 * a sense descriptors, since that's only
2239 * correct for ATA, not ATAPI
2241 ata_gen_ata_desc_sense(qc);
2243 else {
2244 u8 *scsicmd = cmd->cmnd;
2246 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
2247 u8 *buf = NULL;
2248 unsigned int buflen;
2250 buflen = ata_scsi_rbuf_get(cmd, &buf);
2252 /* ATAPI devices typically report zero for their SCSI version,
2253 * and sometimes deviate from the spec WRT response data
2254 * format. If SCSI version is reported as zero like normal,
2255 * then we make the following fixups: 1) Fake MMC-5 version,
2256 * to indicate to the Linux scsi midlayer this is a modern
2257 * device. 2) Ensure response data format / ATAPI information
2258 * are always correct.
2260 if (buf[2] == 0) {
2261 buf[2] = 0x5;
2262 buf[3] = 0x32;
2265 ata_scsi_rbuf_put(cmd, buf);
2268 cmd->result = SAM_STAT_GOOD;
2271 qc->scsidone(cmd);
2272 ata_qc_free(qc);
2275 * atapi_xlat - Initialize PACKET taskfile
2276 * @qc: command structure to be initialized
2277 * @scsicmd: SCSI CDB associated with this PACKET command
2279 * LOCKING:
2280 * spin_lock_irqsave(host_set lock)
2282 * RETURNS:
2283 * Zero on success, non-zero on failure.
2286 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2288 struct scsi_cmnd *cmd = qc->scsicmd;
2289 struct ata_device *dev = qc->dev;
2290 int using_pio = (dev->flags & ATA_DFLAG_PIO);
2291 int nodata = (cmd->sc_data_direction == DMA_NONE);
2293 if (!using_pio)
2294 /* Check whether ATAPI DMA is safe */
2295 if (ata_check_atapi_dma(qc))
2296 using_pio = 1;
2298 memcpy(&qc->cdb, scsicmd, dev->cdb_len);
2300 qc->complete_fn = atapi_qc_complete;
2302 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2303 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2304 qc->tf.flags |= ATA_TFLAG_WRITE;
2305 DPRINTK("direction: write\n");
2308 qc->tf.command = ATA_CMD_PACKET;
2310 /* no data, or PIO data xfer */
2311 if (using_pio || nodata) {
2312 if (nodata)
2313 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2314 else
2315 qc->tf.protocol = ATA_PROT_ATAPI;
2316 qc->tf.lbam = (8 * 1024) & 0xff;
2317 qc->tf.lbah = (8 * 1024) >> 8;
2320 /* DMA data xfer */
2321 else {
2322 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2323 qc->tf.feature |= ATAPI_PKT_DMA;
2325 #ifdef ATAPI_ENABLE_DMADIR
2326 /* some SATA bridges need us to indicate data xfer direction */
2327 if (cmd->sc_data_direction != DMA_TO_DEVICE)
2328 qc->tf.feature |= ATAPI_DMADIR;
2329 #endif
2332 qc->nbytes = cmd->bufflen;
2334 return 0;
2338 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2339 * @ap: ATA port to which the device is attached
2340 * @scsidev: SCSI device from which we derive the ATA device
2342 * Given various information provided in struct scsi_cmnd,
2343 * map that onto an ATA bus, and using that mapping
2344 * determine which ata_device is associated with the
2345 * SCSI command to be sent.
2347 * LOCKING:
2348 * spin_lock_irqsave(host_set lock)
2350 * RETURNS:
2351 * Associated ATA device, or %NULL if not found.
2354 static struct ata_device *
2355 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2357 struct ata_device *dev;
2359 /* skip commands not addressed to targets we simulate */
2360 if (likely(scsidev->id < ATA_MAX_DEVICES))
2361 dev = &ap->device[scsidev->id];
2362 else
2363 return NULL;
2365 if (unlikely((scsidev->channel != 0) ||
2366 (scsidev->lun != 0)))
2367 return NULL;
2369 if (unlikely(!ata_dev_present(dev)))
2370 return NULL;
2372 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2373 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2374 printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n",
2375 ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled");
2376 return NULL;
2380 return dev;
2384 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2385 * @byte1: Byte 1 from pass-thru CDB.
2387 * RETURNS:
2388 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2390 static u8
2391 ata_scsi_map_proto(u8 byte1)
2393 switch((byte1 & 0x1e) >> 1) {
2394 case 3: /* Non-data */
2395 return ATA_PROT_NODATA;
2397 case 6: /* DMA */
2398 return ATA_PROT_DMA;
2400 case 4: /* PIO Data-in */
2401 case 5: /* PIO Data-out */
2402 return ATA_PROT_PIO;
2404 case 10: /* Device Reset */
2405 case 0: /* Hard Reset */
2406 case 1: /* SRST */
2407 case 2: /* Bus Idle */
2408 case 7: /* Packet */
2409 case 8: /* DMA Queued */
2410 case 9: /* Device Diagnostic */
2411 case 11: /* UDMA Data-in */
2412 case 12: /* UDMA Data-Out */
2413 case 13: /* FPDMA */
2414 default: /* Reserved */
2415 break;
2418 return ATA_PROT_UNKNOWN;
2422 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2423 * @qc: command structure to be initialized
2424 * @scsicmd: SCSI command to convert
2426 * Handles either 12 or 16-byte versions of the CDB.
2428 * RETURNS:
2429 * Zero on success, non-zero on failure.
2431 static unsigned int
2432 ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2434 struct ata_taskfile *tf = &(qc->tf);
2435 struct scsi_cmnd *cmd = qc->scsicmd;
2437 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
2438 goto invalid_fld;
2440 if (scsicmd[1] & 0xe0)
2441 /* PIO multi not supported yet */
2442 goto invalid_fld;
2445 * 12 and 16 byte CDBs use different offsets to
2446 * provide the various register values.
2448 if (scsicmd[0] == ATA_16) {
2450 * 16-byte CDB - may contain extended commands.
2452 * If that is the case, copy the upper byte register values.
2454 if (scsicmd[1] & 0x01) {
2455 tf->hob_feature = scsicmd[3];
2456 tf->hob_nsect = scsicmd[5];
2457 tf->hob_lbal = scsicmd[7];
2458 tf->hob_lbam = scsicmd[9];
2459 tf->hob_lbah = scsicmd[11];
2460 tf->flags |= ATA_TFLAG_LBA48;
2461 } else
2462 tf->flags &= ~ATA_TFLAG_LBA48;
2465 * Always copy low byte, device and command registers.
2467 tf->feature = scsicmd[4];
2468 tf->nsect = scsicmd[6];
2469 tf->lbal = scsicmd[8];
2470 tf->lbam = scsicmd[10];
2471 tf->lbah = scsicmd[12];
2472 tf->device = scsicmd[13];
2473 tf->command = scsicmd[14];
2474 } else {
2476 * 12-byte CDB - incapable of extended commands.
2478 tf->flags &= ~ATA_TFLAG_LBA48;
2480 tf->feature = scsicmd[3];
2481 tf->nsect = scsicmd[4];
2482 tf->lbal = scsicmd[5];
2483 tf->lbam = scsicmd[6];
2484 tf->lbah = scsicmd[7];
2485 tf->device = scsicmd[8];
2486 tf->command = scsicmd[9];
2489 * If slave is possible, enforce correct master/slave bit
2491 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2492 tf->device = qc->dev->devno ?
2493 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
2496 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2497 * SET_FEATURES - XFER MODE must be preceded/succeeded
2498 * by an update to hardware-specific registers for each
2499 * controller (i.e. the reason for ->set_piomode(),
2500 * ->set_dmamode(), and ->post_set_mode() hooks).
2502 if ((tf->command == ATA_CMD_SET_FEATURES)
2503 && (tf->feature == SETFEATURES_XFER))
2504 goto invalid_fld;
2507 * Set flags so that all registers will be written,
2508 * and pass on write indication (used for PIO/DMA
2509 * setup.)
2511 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2513 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2514 tf->flags |= ATA_TFLAG_WRITE;
2517 * Set transfer length.
2519 * TODO: find out if we need to do more here to
2520 * cover scatter/gather case.
2522 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2524 return 0;
2526 invalid_fld:
2527 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2528 /* "Invalid field in cdb" */
2529 return 1;
2533 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2534 * @dev: ATA device
2535 * @cmd: SCSI command opcode to consider
2537 * Look up the SCSI command given, and determine whether the
2538 * SCSI command is to be translated or simulated.
2540 * RETURNS:
2541 * Pointer to translation function if possible, %NULL if not.
2544 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2546 switch (cmd) {
2547 case READ_6:
2548 case READ_10:
2549 case READ_16:
2551 case WRITE_6:
2552 case WRITE_10:
2553 case WRITE_16:
2554 return ata_scsi_rw_xlat;
2556 case SYNCHRONIZE_CACHE:
2557 if (ata_try_flush_cache(dev))
2558 return ata_scsi_flush_xlat;
2559 break;
2561 case VERIFY:
2562 case VERIFY_16:
2563 return ata_scsi_verify_xlat;
2565 case ATA_12:
2566 case ATA_16:
2567 return ata_scsi_pass_thru;
2569 case START_STOP:
2570 return ata_scsi_start_stop_xlat;
2573 return NULL;
2577 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2578 * @ap: ATA port to which the command was being sent
2579 * @cmd: SCSI command to dump
2581 * Prints the contents of a SCSI command via printk().
2584 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2585 struct scsi_cmnd *cmd)
2587 #ifdef ATA_DEBUG
2588 struct scsi_device *scsidev = cmd->device;
2589 u8 *scsicmd = cmd->cmnd;
2591 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2592 ap->id,
2593 scsidev->channel, scsidev->id, scsidev->lun,
2594 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2595 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2596 scsicmd[8]);
2597 #endif
2600 static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
2601 struct ata_port *ap, struct ata_device *dev)
2603 if (dev->class == ATA_DEV_ATA) {
2604 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2605 cmd->cmnd[0]);
2607 if (xlat_func)
2608 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
2609 else
2610 ata_scsi_simulate(ap, dev, cmd, done);
2611 } else
2612 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
2616 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2617 * @cmd: SCSI command to be sent
2618 * @done: Completion function, called when command is complete
2620 * In some cases, this function translates SCSI commands into
2621 * ATA taskfiles, and queues the taskfiles to be sent to
2622 * hardware. In other cases, this function simulates a
2623 * SCSI device by evaluating and responding to certain
2624 * SCSI commands. This creates the overall effect of
2625 * ATA and ATAPI devices appearing as SCSI devices.
2627 * LOCKING:
2628 * Releases scsi-layer-held lock, and obtains host_set lock.
2630 * RETURNS:
2631 * Zero.
2634 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2636 struct ata_port *ap;
2637 struct ata_device *dev;
2638 struct scsi_device *scsidev = cmd->device;
2639 struct Scsi_Host *shost = scsidev->host;
2641 ap = (struct ata_port *) &shost->hostdata[0];
2643 spin_unlock(shost->host_lock);
2644 spin_lock(&ap->host_set->lock);
2646 ata_scsi_dump_cdb(ap, cmd);
2648 dev = ata_scsi_find_dev(ap, scsidev);
2649 if (likely(dev))
2650 __ata_scsi_queuecmd(cmd, done, ap, dev);
2651 else {
2652 cmd->result = (DID_BAD_TARGET << 16);
2653 done(cmd);
2656 spin_unlock(&ap->host_set->lock);
2657 spin_lock(shost->host_lock);
2658 return 0;
2662 * ata_scsi_simulate - simulate SCSI command on ATA device
2663 * @ap: port the device is connected to
2664 * @dev: the target device
2665 * @cmd: SCSI command being sent to device.
2666 * @done: SCSI command completion function.
2668 * Interprets and directly executes a select list of SCSI commands
2669 * that can be handled internally.
2671 * LOCKING:
2672 * spin_lock_irqsave(host_set lock)
2675 void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
2676 struct scsi_cmnd *cmd,
2677 void (*done)(struct scsi_cmnd *))
2679 struct ata_scsi_args args;
2680 const u8 *scsicmd = cmd->cmnd;
2682 args.ap = ap;
2683 args.dev = dev;
2684 args.id = dev->id;
2685 args.cmd = cmd;
2686 args.done = done;
2688 switch(scsicmd[0]) {
2689 /* no-op's, complete with success */
2690 case SYNCHRONIZE_CACHE:
2691 case REZERO_UNIT:
2692 case SEEK_6:
2693 case SEEK_10:
2694 case TEST_UNIT_READY:
2695 case FORMAT_UNIT: /* FIXME: correct? */
2696 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2697 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2698 break;
2700 case INQUIRY:
2701 if (scsicmd[1] & 2) /* is CmdDt set? */
2702 ata_scsi_invalid_field(cmd, done);
2703 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2704 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2705 else if (scsicmd[2] == 0x00)
2706 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2707 else if (scsicmd[2] == 0x80)
2708 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2709 else if (scsicmd[2] == 0x83)
2710 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2711 else
2712 ata_scsi_invalid_field(cmd, done);
2713 break;
2715 case MODE_SENSE:
2716 case MODE_SENSE_10:
2717 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2718 break;
2720 case MODE_SELECT: /* unconditionally return */
2721 case MODE_SELECT_10: /* bad-field-in-cdb */
2722 ata_scsi_invalid_field(cmd, done);
2723 break;
2725 case READ_CAPACITY:
2726 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2727 break;
2729 case SERVICE_ACTION_IN:
2730 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2731 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2732 else
2733 ata_scsi_invalid_field(cmd, done);
2734 break;
2736 case REPORT_LUNS:
2737 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2738 break;
2740 /* mandatory commands we haven't implemented yet */
2741 case REQUEST_SENSE:
2743 /* all other commands */
2744 default:
2745 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2746 /* "Invalid command operation code" */
2747 done(cmd);
2748 break;
2752 void ata_scsi_scan_host(struct ata_port *ap)
2754 struct ata_device *dev;
2755 unsigned int i;
2757 if (ap->flags & ATA_FLAG_PORT_DISABLED)
2758 return;
2760 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2761 dev = &ap->device[i];
2763 if (ata_dev_present(dev))
2764 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);