2 libata-scsi.c - helper library for ATA
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
7 The contents of this file are subject to the Open
8 Software License version 1.1 that can be found at
9 http://www.opensource.org/licenses/osl-1.1.txt and is included herein
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU General Public License version 2 (the "GPL") as distributed
14 in the kernel source COPYING file, in which case the provisions of
15 the GPL are applicable instead of the above. If you wish to allow
16 the use of your version of this file only under the terms of the
17 GPL and not to allow others to use your version of this file under
18 the OSL, indicate your decision by deleting the provisions above and
19 replace them with the notice and other provisions required by the GPL.
20 If you do not delete the provisions above, a recipient may use your
21 version of this file under either the OSL or the GPL.
25 #include <linux/kernel.h>
26 #include <linux/blkdev.h>
27 #include <linux/spinlock.h>
28 #include <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <linux/libata.h>
32 #include <linux/hdreg.h>
33 #include <asm/uaccess.h>
37 #define SECTOR_SIZE 512
39 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
40 static struct ata_device *
41 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
45 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
46 * @sdev: SCSI device for which BIOS geometry is to be determined
47 * @bdev: block device associated with @sdev
48 * @capacity: capacity of SCSI device
49 * @geom: location to which geometry will be output
51 * Generic bios head/sector/cylinder calculator
52 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
53 * mapping. Some situations may arise where the disk is not
54 * bootable if this is not used.
57 * Defined by the SCSI layer. We don't really care.
62 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
63 sector_t capacity, int geom[])
67 sector_div(capacity, 255*63);
74 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
75 * @dev: Device to whom we are issuing command
76 * @arg: User provided data for issuing command
79 * Defined by the SCSI layer. We don't really care.
82 * Zero on success, negative errno on error.
85 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
88 u8 scsi_cmd[MAX_COMMAND_SIZE];
89 u8 args[4], *argbuf = NULL;
91 struct scsi_request *sreq;
93 if (NULL == (void *)arg)
96 if (copy_from_user(args, arg, sizeof(args)))
99 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
103 memset(scsi_cmd, 0, sizeof(scsi_cmd));
106 argsize = SECTOR_SIZE * args[3];
107 argbuf = kmalloc(argsize, GFP_KERNEL);
111 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
112 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
113 block count in sector count field */
114 sreq->sr_data_direction = DMA_FROM_DEVICE;
116 scsi_cmd[1] = (3 << 1); /* Non-data */
117 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
118 sreq->sr_data_direction = DMA_NONE;
121 scsi_cmd[0] = ATA_16;
123 scsi_cmd[4] = args[2];
124 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
125 scsi_cmd[6] = args[3];
126 scsi_cmd[8] = args[1];
130 scsi_cmd[6] = args[1];
132 scsi_cmd[14] = args[0];
134 /* Good values for timeout and retries? Values below
135 from scsi_ioctl_send_command() for default case... */
136 scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
138 if (sreq->sr_result) {
143 /* Need code to retrieve data from check condition? */
146 && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
149 scsi_release_request(sreq);
158 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
159 * @dev: Device to whom we are issuing command
160 * @arg: User provided data for issuing command
163 * Defined by the SCSI layer. We don't really care.
166 * Zero on success, negative errno on error.
168 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
171 u8 scsi_cmd[MAX_COMMAND_SIZE];
173 struct scsi_request *sreq;
175 if (NULL == (void *)arg)
178 if (copy_from_user(args, arg, sizeof(args)))
181 memset(scsi_cmd, 0, sizeof(scsi_cmd));
182 scsi_cmd[0] = ATA_16;
183 scsi_cmd[1] = (3 << 1); /* Non-data */
184 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
185 scsi_cmd[4] = args[1];
186 scsi_cmd[6] = args[2];
187 scsi_cmd[8] = args[3];
188 scsi_cmd[10] = args[4];
189 scsi_cmd[12] = args[5];
190 scsi_cmd[14] = args[0];
192 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
198 sreq->sr_data_direction = DMA_NONE;
199 /* Good values for timeout and retries? Values below
200 from scsi_ioctl_send_command() for default case... */
201 scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
203 if (sreq->sr_result) {
208 /* Need code to retrieve data from check condition? */
211 scsi_release_request(sreq);
215 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
218 struct ata_device *dev;
219 int val = -EINVAL, rc = -EINVAL;
221 ap = (struct ata_port *) &scsidev->host->hostdata[0];
225 dev = ata_scsi_find_dev(ap, scsidev);
232 case ATA_IOC_GET_IO32:
234 if (copy_to_user(arg, &val, 1))
238 case ATA_IOC_SET_IO32:
239 val = (unsigned long) arg;
245 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
247 return ata_cmd_ioctl(scsidev, arg);
249 case HDIO_DRIVE_TASK:
250 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
252 return ata_task_ioctl(scsidev, arg);
264 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
265 * @ap: ATA port to which the new command is attached
266 * @dev: ATA device to which the new command is attached
267 * @cmd: SCSI command that originated this ATA command
268 * @done: SCSI command completion function
270 * Obtain a reference to an unused ata_queued_cmd structure,
271 * which is the basic libata structure representing a single
272 * ATA command sent to the hardware.
274 * If a command was available, fill in the SCSI-specific
275 * portions of the structure with information on the
279 * spin_lock_irqsave(host_set lock)
282 * Command allocated, or %NULL if none available.
284 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
285 struct ata_device *dev,
286 struct scsi_cmnd *cmd,
287 void (*done)(struct scsi_cmnd *))
289 struct ata_queued_cmd *qc;
291 qc = ata_qc_new_init(ap, dev);
297 qc->sg = (struct scatterlist *) cmd->request_buffer;
298 qc->n_elem = cmd->use_sg;
304 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
312 * ata_dump_status - user friendly display of error info
313 * @id: id of the port in question
314 * @tf: ptr to filled out taskfile
316 * Decode and dump the ATA error/status registers for the user so
317 * that they have some idea what really happened at the non
318 * make-believe layer.
321 * inherited from caller
323 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
325 u8 stat = tf->command, err = tf->feature;
327 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
328 if (stat & ATA_BUSY) {
329 printk("Busy }\n"); /* Data is not valid in this case */
331 if (stat & 0x40) printk("DriveReady ");
332 if (stat & 0x20) printk("DeviceFault ");
333 if (stat & 0x10) printk("SeekComplete ");
334 if (stat & 0x08) printk("DataRequest ");
335 if (stat & 0x04) printk("CorrectedError ");
336 if (stat & 0x02) printk("Index ");
337 if (stat & 0x01) printk("Error ");
341 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
342 if (err & 0x04) printk("DriveStatusError ");
344 if (err & 0x04) printk("BadCRC ");
345 else printk("Sector ");
347 if (err & 0x40) printk("UncorrectableError ");
348 if (err & 0x10) printk("SectorIdNotFound ");
349 if (err & 0x02) printk("TrackZeroNotFound ");
350 if (err & 0x01) printk("AddrMarkNotFound ");
357 * ata_to_sense_error - convert ATA error to SCSI error
358 * @drv_stat: value contained in ATA status register
359 * @drv_err: value contained in ATA error register
360 * @sk: the sense key we'll fill out
361 * @asc: the additional sense code we'll fill out
362 * @ascq: the additional sense code qualifier we'll fill out
364 * Converts an ATA error into a SCSI error. Fill out pointers to
365 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
366 * format sense blocks.
369 * spin_lock_irqsave(host_set lock)
371 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
375 /* Based on the 3ware driver translation table */
376 static unsigned char sense_table[][4] = {
378 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
380 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
382 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
383 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
384 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
385 /* MC|ID|ABRT|TRK0|MARK */
386 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
388 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
389 /* Bad address mark */
390 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
392 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
394 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
395 /* Media change request */
396 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
398 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
400 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
402 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
403 /* BBD - block marked bad */
404 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
405 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
407 static unsigned char stat_table[][4] = {
408 /* Must be first because BUSY means no other bits valid */
409 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
410 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
411 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
412 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
413 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
417 * Is this an error we can process/parse
419 if (drv_stat & ATA_BUSY) {
420 drv_err = 0; /* Ignore the err bits, they're invalid */
424 /* Look for drv_err */
425 for (i = 0; sense_table[i][0] != 0xFF; i++) {
426 /* Look for best matches first */
427 if ((sense_table[i][0] & drv_err) ==
429 *sk = sense_table[i][1];
430 *asc = sense_table[i][2];
431 *ascq = sense_table[i][3];
435 /* No immediate match */
436 printk(KERN_WARNING "ata%u: no sense translation for "
437 "error 0x%02x\n", id, drv_err);
440 /* Fall back to interpreting status bits */
441 for (i = 0; stat_table[i][0] != 0xFF; i++) {
442 if (stat_table[i][0] & drv_stat) {
443 *sk = stat_table[i][1];
444 *asc = stat_table[i][2];
445 *ascq = stat_table[i][3];
449 /* No error? Undecoded? */
450 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
453 /* For our last chance pick, use medium read error because
454 * it's much more common than an ATA drive telling you a write
458 *asc = 0x11; /* "unrecovered read error" */
459 *ascq = 0x04; /* "auto-reallocation failed" */
462 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
463 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
469 * ata_gen_ata_desc_sense - Generate check condition sense block.
470 * @qc: Command that completed.
472 * This function is specific to the ATA descriptor format sense
473 * block specified for the ATA pass through commands. Regardless
474 * of whether the command errored or not, return a sense
475 * block. Copy all controller registers into the sense
476 * block. Clear sense key, ASC & ASCQ if there is no error.
479 * spin_lock_irqsave(host_set lock)
481 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
483 struct scsi_cmnd *cmd = qc->scsicmd;
484 struct ata_taskfile *tf = &qc->tf;
485 unsigned char *sb = cmd->sense_buffer;
486 unsigned char *desc = sb + 8;
488 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
490 cmd->result = SAM_STAT_CHECK_CONDITION;
493 * Read the controller registers.
495 assert(NULL != qc->ap->ops->tf_read);
496 qc->ap->ops->tf_read(qc->ap, tf);
499 * Use ata_to_sense_error() to map status register bits
500 * onto sense key, asc & ascq.
502 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
503 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
504 &sb[1], &sb[2], &sb[3]);
509 * Sense data is current and format is descriptor.
516 * Set length of additional sense data.
517 * Since we only populate descriptor 0, the total
518 * length is the same (fixed) length as descriptor 0.
520 desc[1] = sb[7] = 14;
523 * Copy registers into sense buffer.
526 desc[3] = tf->feature; /* == error reg */
531 desc[12] = tf->device;
532 desc[13] = tf->command; /* == status reg */
535 * Fill in Extend bit, and the high order bytes
538 if (tf->flags & ATA_TFLAG_LBA48) {
540 desc[4] = tf->hob_nsect;
541 desc[6] = tf->hob_lbal;
542 desc[8] = tf->hob_lbam;
543 desc[10] = tf->hob_lbah;
548 * ata_gen_fixed_sense - generate a SCSI fixed sense block
549 * @qc: Command that we are erroring out
551 * Leverage ata_to_sense_error() to give us the codes. Fit our
552 * LBA in here if there's room.
555 * inherited from caller
557 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
559 struct scsi_cmnd *cmd = qc->scsicmd;
560 struct ata_taskfile *tf = &qc->tf;
561 unsigned char *sb = cmd->sense_buffer;
563 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
565 cmd->result = SAM_STAT_CHECK_CONDITION;
568 * Read the controller registers.
570 assert(NULL != qc->ap->ops->tf_read);
571 qc->ap->ops->tf_read(qc->ap, tf);
574 * Use ata_to_sense_error() to map status register bits
575 * onto sense key, asc & ascq.
577 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
578 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
579 &sb[2], &sb[12], &sb[13]);
585 if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
586 /* A small (28b) LBA will fit in the 32b info field */
587 sb[0] |= 0x80; /* set valid bit */
588 sb[3] = tf->device & 0x0f;
596 * ata_scsi_slave_config - Set SCSI device attributes
597 * @sdev: SCSI device to examine
599 * This is called before we actually start reading
600 * and writing to the device, to configure certain
601 * SCSI mid-layer behaviors.
604 * Defined by SCSI layer. We don't really care.
607 int ata_scsi_slave_config(struct scsi_device *sdev)
609 sdev->use_10_for_rw = 1;
610 sdev->use_10_for_ms = 1;
612 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
614 if (sdev->id < ATA_MAX_DEVICES) {
616 struct ata_device *dev;
618 ap = (struct ata_port *) &sdev->host->hostdata[0];
619 dev = &ap->device[sdev->id];
621 /* TODO: 1024 is an arbitrary number, not the
622 * hardware maximum. This should be increased to
623 * 65534 when Jens Axboe's patch for dynamically
624 * determining max_sectors is merged.
626 if ((dev->flags & ATA_DFLAG_LBA48) &&
627 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
629 * do not overwrite sdev->host->max_sectors, since
630 * other drives on this host may not support LBA48
632 blk_queue_max_sectors(sdev->request_queue, 2048);
636 return 0; /* scsi layer doesn't check return value, sigh */
640 * ata_scsi_error - SCSI layer error handler callback
641 * @host: SCSI host on which error occurred
643 * Handles SCSI-layer-thrown error events.
646 * Inherited from SCSI layer (none, can sleep)
652 int ata_scsi_error(struct Scsi_Host *host)
658 ap = (struct ata_port *) &host->hostdata[0];
659 ap->ops->eng_timeout(ap);
661 /* TODO: this is per-command; when queueing is supported
662 * this code will either change or move to a more
672 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
673 * @qc: Storage for translated ATA taskfile
674 * @scsicmd: SCSI command to translate (ignored)
676 * Sets up an ATA taskfile to issue FLUSH CACHE or
680 * spin_lock_irqsave(host_set lock)
683 * Zero on success, non-zero on error.
686 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
688 struct ata_taskfile *tf = &qc->tf;
690 tf->flags |= ATA_TFLAG_DEVICE;
691 tf->protocol = ATA_PROT_NODATA;
693 if ((tf->flags & ATA_TFLAG_LBA48) &&
694 (ata_id_has_flush_ext(qc->dev->id)))
695 tf->command = ATA_CMD_FLUSH_EXT;
697 tf->command = ATA_CMD_FLUSH;
703 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
704 * @qc: Storage for translated ATA taskfile
705 * @scsicmd: SCSI command to translate
707 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
710 * spin_lock_irqsave(host_set lock)
713 * Zero on success, non-zero on error.
716 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
718 struct ata_taskfile *tf = &qc->tf;
719 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
720 u64 dev_sectors = qc->dev->n_sectors;
724 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
725 tf->protocol = ATA_PROT_NODATA;
726 tf->device |= ATA_LBA;
728 if (scsicmd[0] == VERIFY) {
729 sect |= ((u64)scsicmd[2]) << 24;
730 sect |= ((u64)scsicmd[3]) << 16;
731 sect |= ((u64)scsicmd[4]) << 8;
732 sect |= ((u64)scsicmd[5]);
734 n_sect |= ((u32)scsicmd[7]) << 8;
735 n_sect |= ((u32)scsicmd[8]);
738 else if (scsicmd[0] == VERIFY_16) {
739 sect |= ((u64)scsicmd[2]) << 56;
740 sect |= ((u64)scsicmd[3]) << 48;
741 sect |= ((u64)scsicmd[4]) << 40;
742 sect |= ((u64)scsicmd[5]) << 32;
743 sect |= ((u64)scsicmd[6]) << 24;
744 sect |= ((u64)scsicmd[7]) << 16;
745 sect |= ((u64)scsicmd[8]) << 8;
746 sect |= ((u64)scsicmd[9]);
748 n_sect |= ((u32)scsicmd[10]) << 24;
749 n_sect |= ((u32)scsicmd[11]) << 16;
750 n_sect |= ((u32)scsicmd[12]) << 8;
751 n_sect |= ((u32)scsicmd[13]);
759 if (sect >= dev_sectors)
761 if ((sect + n_sect) > dev_sectors)
764 if (n_sect > (64 * 1024))
772 tf->command = ATA_CMD_VERIFY_EXT;
774 tf->hob_nsect = (n_sect >> 8) & 0xff;
776 tf->hob_lbah = (sect >> 40) & 0xff;
777 tf->hob_lbam = (sect >> 32) & 0xff;
778 tf->hob_lbal = (sect >> 24) & 0xff;
780 tf->command = ATA_CMD_VERIFY;
782 tf->device |= (sect >> 24) & 0xf;
785 tf->nsect = n_sect & 0xff;
787 tf->lbah = (sect >> 16) & 0xff;
788 tf->lbam = (sect >> 8) & 0xff;
789 tf->lbal = sect & 0xff;
795 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
796 * @qc: Storage for translated ATA taskfile
797 * @scsicmd: SCSI command to translate
799 * Converts any of six SCSI read/write commands into the
800 * ATA counterpart, including starting sector (LBA),
801 * sector count, and taking into account the device's LBA48
804 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
805 * %WRITE_16 are currently supported.
808 * spin_lock_irqsave(host_set lock)
811 * Zero on success, non-zero on error.
814 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
816 struct ata_taskfile *tf = &qc->tf;
817 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
819 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
820 tf->protocol = qc->dev->xfer_protocol;
821 tf->device |= ATA_LBA;
823 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
824 scsicmd[0] == READ_16) {
825 tf->command = qc->dev->read_cmd;
827 tf->command = qc->dev->write_cmd;
828 tf->flags |= ATA_TFLAG_WRITE;
831 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
833 tf->hob_nsect = scsicmd[7];
834 tf->hob_lbal = scsicmd[2];
836 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
839 /* if we don't support LBA48 addressing, the request
840 * -may- be too large. */
841 if ((scsicmd[2] & 0xf0) || scsicmd[7])
844 /* stores LBA27:24 in lower 4 bits of device reg */
845 tf->device |= scsicmd[2];
847 qc->nsect = scsicmd[8];
850 tf->nsect = scsicmd[8];
851 tf->lbal = scsicmd[5];
852 tf->lbam = scsicmd[4];
853 tf->lbah = scsicmd[3];
855 VPRINTK("ten-byte command\n");
859 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
860 qc->nsect = tf->nsect = scsicmd[4];
861 tf->lbal = scsicmd[3];
862 tf->lbam = scsicmd[2];
863 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
865 VPRINTK("six-byte command\n");
869 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
870 /* rule out impossible LBAs and sector counts */
871 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
875 tf->hob_nsect = scsicmd[12];
876 tf->hob_lbal = scsicmd[6];
877 tf->hob_lbam = scsicmd[5];
878 tf->hob_lbah = scsicmd[4];
880 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
883 /* once again, filter out impossible non-zero values */
884 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
888 /* stores LBA27:24 in lower 4 bits of device reg */
889 tf->device |= scsicmd[6];
891 qc->nsect = scsicmd[13];
894 tf->nsect = scsicmd[13];
895 tf->lbal = scsicmd[9];
896 tf->lbam = scsicmd[8];
897 tf->lbah = scsicmd[7];
899 VPRINTK("sixteen-byte command\n");
903 DPRINTK("no-byte command\n");
907 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
909 struct scsi_cmnd *cmd = qc->scsicmd;
910 int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
912 /* For ATA pass thru (SAT) commands, generate a sense block if
913 * user mandated it or if there's an error. Note that if we
914 * generate because the user forced us to, a check condition
915 * is generated and the ATA register values are returned
916 * whether the command completed successfully or not. If there
917 * was no error, SK, ASC and ASCQ will all be zero.
919 if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
920 ((cmd->cmnd[2] & 0x20) || need_sense)) {
921 ata_gen_ata_desc_sense(qc);
924 cmd->result = SAM_STAT_GOOD;
926 /* TODO: decide which descriptor format to use
927 * for 48b LBA devices and call that here
928 * instead of the fixed desc, which is only
929 * good for smaller LBA (and maybe CHS?)
932 ata_gen_fixed_sense(qc);
937 /* The ata_gen_..._sense routines fill in tf */
938 ata_dump_status(qc->ap->id, &qc->tf);
947 * ata_scsi_translate - Translate then issue SCSI command to ATA device
948 * @ap: ATA port to which the command is addressed
949 * @dev: ATA device to which the command is addressed
950 * @cmd: SCSI command to execute
951 * @done: SCSI command completion function
952 * @xlat_func: Actor which translates @cmd to an ATA taskfile
954 * Our ->queuecommand() function has decided that the SCSI
955 * command issued can be directly translated into an ATA
956 * command, rather than handled internally.
958 * This function sets up an ata_queued_cmd structure for the
959 * SCSI command, and sends that ata_queued_cmd to the hardware.
962 * spin_lock_irqsave(host_set lock)
965 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
966 struct scsi_cmnd *cmd,
967 void (*done)(struct scsi_cmnd *),
968 ata_xlat_func_t xlat_func)
970 struct ata_queued_cmd *qc;
971 u8 *scsicmd = cmd->cmnd;
975 qc = ata_scsi_qc_new(ap, dev, cmd, done);
979 /* data is present; dma-map it */
980 if (cmd->sc_data_direction == SCSI_DATA_READ ||
981 cmd->sc_data_direction == SCSI_DATA_WRITE) {
982 if (unlikely(cmd->request_bufflen < 1)) {
983 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
989 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
991 ata_sg_init_one(qc, cmd->request_buffer,
992 cmd->request_bufflen);
994 qc->dma_dir = cmd->sc_data_direction;
997 qc->complete_fn = ata_scsi_qc_complete;
999 if (xlat_func(qc, scsicmd))
1001 /* select device, send command to hardware */
1002 if (ata_qc_issue(qc))
1010 ata_bad_cdb(cmd, done);
1011 DPRINTK("EXIT - badcmd\n");
1015 * ata_scsi_rbuf_get - Map response buffer.
1016 * @cmd: SCSI command containing buffer to be mapped.
1017 * @buf_out: Pointer to mapped area.
1019 * Maps buffer contained within SCSI command @cmd.
1022 * spin_lock_irqsave(host_set lock)
1025 * Length of response buffer.
1028 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1031 unsigned int buflen;
1034 struct scatterlist *sg;
1036 sg = (struct scatterlist *) cmd->request_buffer;
1037 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1038 buflen = sg->length;
1040 buf = cmd->request_buffer;
1041 buflen = cmd->request_bufflen;
1049 * ata_scsi_rbuf_put - Unmap response buffer.
1050 * @cmd: SCSI command containing buffer to be unmapped.
1051 * @buf: buffer to unmap
1053 * Unmaps response buffer contained within @cmd.
1056 * spin_lock_irqsave(host_set lock)
1059 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1062 struct scatterlist *sg;
1064 sg = (struct scatterlist *) cmd->request_buffer;
1065 kunmap_atomic(buf - sg->offset, KM_USER0);
1070 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1071 * @args: device IDENTIFY data / SCSI command of interest.
1072 * @actor: Callback hook for desired SCSI command simulator
1074 * Takes care of the hard work of simulating a SCSI command...
1075 * Mapping the response buffer, calling the command's handler,
1076 * and handling the handler's return value. This return value
1077 * indicates whether the handler wishes the SCSI command to be
1078 * completed successfully, or not.
1081 * spin_lock_irqsave(host_set lock)
1084 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1085 unsigned int (*actor) (struct ata_scsi_args *args,
1086 u8 *rbuf, unsigned int buflen))
1089 unsigned int buflen, rc;
1090 struct scsi_cmnd *cmd = args->cmd;
1092 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1093 memset(rbuf, 0, buflen);
1094 rc = actor(args, rbuf, buflen);
1095 ata_scsi_rbuf_put(cmd, rbuf);
1098 ata_bad_cdb(cmd, args->done);
1100 cmd->result = SAM_STAT_GOOD;
1106 * ata_scsiop_inq_std - Simulate INQUIRY command
1107 * @args: device IDENTIFY data / SCSI command of interest.
1108 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1109 * @buflen: Response buffer length.
1111 * Returns standard device identification data associated
1112 * with non-EVPD INQUIRY command output.
1115 * spin_lock_irqsave(host_set lock)
1118 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1119 unsigned int buflen)
1124 0x5, /* claim SPC-3 version compatibility */
1129 /* set scsi removeable (RMB) bit per ata bit */
1130 if (ata_id_removeable(args->id))
1135 memcpy(rbuf, hdr, sizeof(hdr));
1138 memcpy(&rbuf[8], "ATA ", 8);
1139 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1140 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1141 if (rbuf[32] == 0 || rbuf[32] == ' ')
1142 memcpy(&rbuf[32], "n/a ", 4);
1146 const u8 versions[] = {
1147 0x60, /* SAM-3 (no version claimed) */
1150 0x20, /* SBC-2 (no version claimed) */
1153 0x60 /* SPC-3 (no version claimed) */
1156 memcpy(rbuf + 59, versions, sizeof(versions));
1163 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1164 * @args: device IDENTIFY data / SCSI command of interest.
1165 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1166 * @buflen: Response buffer length.
1168 * Returns list of inquiry EVPD pages available.
1171 * spin_lock_irqsave(host_set lock)
1174 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1175 unsigned int buflen)
1177 const u8 pages[] = {
1178 0x00, /* page 0x00, this page */
1179 0x80, /* page 0x80, unit serial no page */
1180 0x83 /* page 0x83, device ident page */
1182 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
1185 memcpy(rbuf + 4, pages, sizeof(pages));
1191 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1192 * @args: device IDENTIFY data / SCSI command of interest.
1193 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1194 * @buflen: Response buffer length.
1196 * Returns ATA device serial number.
1199 * spin_lock_irqsave(host_set lock)
1202 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1203 unsigned int buflen)
1207 0x80, /* this page code */
1209 ATA_SERNO_LEN, /* page len */
1211 memcpy(rbuf, hdr, sizeof(hdr));
1213 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1214 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1215 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1220 static const char *inq_83_str = "Linux ATA-SCSI simulator";
1223 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1224 * @args: device IDENTIFY data / SCSI command of interest.
1225 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1226 * @buflen: Response buffer length.
1228 * Returns device identification. Currently hardcoded to
1229 * return "Linux ATA-SCSI simulator".
1232 * spin_lock_irqsave(host_set lock)
1235 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1236 unsigned int buflen)
1238 rbuf[1] = 0x83; /* this page code */
1239 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
1241 /* our one and only identification descriptor (vendor-specific) */
1242 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1243 rbuf[4 + 0] = 2; /* code set: ASCII */
1244 rbuf[4 + 3] = strlen(inq_83_str);
1245 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1252 * ata_scsiop_noop - Command handler that simply returns success.
1253 * @args: device IDENTIFY data / SCSI command of interest.
1254 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1255 * @buflen: Response buffer length.
1257 * No operation. Simply returns success to caller, to indicate
1258 * that the caller should successfully complete this SCSI command.
1261 * spin_lock_irqsave(host_set lock)
1264 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1265 unsigned int buflen)
1272 * ata_msense_push - Push data onto MODE SENSE data output buffer
1273 * @ptr_io: (input/output) Location to store more output data
1274 * @last: End of output data buffer
1275 * @buf: Pointer to BLOB being added to output buffer
1276 * @buflen: Length of BLOB
1278 * Store MODE SENSE data on an output buffer.
1284 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1285 const u8 *buf, unsigned int buflen)
1289 if ((ptr + buflen - 1) > last)
1292 memcpy(ptr, buf, buflen);
1300 * ata_msense_caching - Simulate MODE SENSE caching info page
1301 * @id: device IDENTIFY data
1302 * @ptr_io: (input/output) Location to store more output data
1303 * @last: End of output data buffer
1305 * Generate a caching info page, which conditionally indicates
1306 * write caching to the SCSI layer, depending on device
1313 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1317 0x8, /* page code */
1318 0x12, /* page length */
1319 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1320 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1323 if (ata_id_wcache_enabled(id))
1324 page[2] |= (1 << 2); /* write cache enable */
1325 if (!ata_id_rahead_enabled(id))
1326 page[12] |= (1 << 5); /* disable read ahead */
1328 ata_msense_push(ptr_io, last, page, sizeof(page));
1329 return sizeof(page);
1333 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1334 * @dev: Device associated with this MODE SENSE command
1335 * @ptr_io: (input/output) Location to store more output data
1336 * @last: End of output data buffer
1338 * Generate a generic MODE SENSE control mode page.
1344 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1346 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1348 /* byte 2: set the descriptor format sense data bit (bit 2)
1349 * since we need to support returning this format for SAT
1350 * commands and any SCSI commands against a 48b LBA device.
1353 ata_msense_push(ptr_io, last, page, sizeof(page));
1354 return sizeof(page);
1358 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1359 * @dev: Device associated with this MODE SENSE command
1360 * @ptr_io: (input/output) Location to store more output data
1361 * @last: End of output data buffer
1363 * Generate a generic MODE SENSE r/w error recovery page.
1369 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1372 0x1, /* page code */
1373 0xa, /* page length */
1374 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1375 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1378 ata_msense_push(ptr_io, last, page, sizeof(page));
1379 return sizeof(page);
1383 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1384 * @args: device IDENTIFY data / SCSI command of interest.
1385 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1386 * @buflen: Response buffer length.
1388 * Simulate MODE SENSE commands.
1391 * spin_lock_irqsave(host_set lock)
1394 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1395 unsigned int buflen)
1397 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1398 unsigned int page_control, six_byte, output_len;
1402 six_byte = (scsicmd[0] == MODE_SENSE);
1404 /* we only support saved and current values (which we treat
1405 * in the same manner)
1407 page_control = scsicmd[2] >> 6;
1408 if ((page_control != 0) && (page_control != 3))
1416 p = rbuf + output_len;
1417 last = rbuf + buflen - 1;
1419 switch(scsicmd[2] & 0x3f) {
1420 case 0x01: /* r/w error recovery */
1421 output_len += ata_msense_rw_recovery(&p, last);
1424 case 0x08: /* caching */
1425 output_len += ata_msense_caching(args->id, &p, last);
1428 case 0x0a: { /* control mode */
1429 output_len += ata_msense_ctl_mode(&p, last);
1433 case 0x3f: /* all pages */
1434 output_len += ata_msense_rw_recovery(&p, last);
1435 output_len += ata_msense_caching(args->id, &p, last);
1436 output_len += ata_msense_ctl_mode(&p, last);
1439 default: /* invalid page code */
1445 rbuf[0] = output_len;
1448 rbuf[0] = output_len >> 8;
1449 rbuf[1] = output_len;
1456 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1457 * @args: device IDENTIFY data / SCSI command of interest.
1458 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1459 * @buflen: Response buffer length.
1461 * Simulate READ CAPACITY commands.
1464 * spin_lock_irqsave(host_set lock)
1467 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1468 unsigned int buflen)
1475 if (ata_id_has_lba48(args->id))
1476 n_sectors = ata_id_u64(args->id, 100);
1478 n_sectors = ata_id_u32(args->id, 60);
1479 n_sectors--; /* ATA TotalUserSectors - 1 */
1481 tmp = n_sectors; /* note: truncates, if lba48 */
1482 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1483 /* sector count, 32-bit */
1484 rbuf[0] = tmp >> (8 * 3);
1485 rbuf[1] = tmp >> (8 * 2);
1486 rbuf[2] = tmp >> (8 * 1);
1490 tmp = ATA_SECT_SIZE;
1495 /* sector count, 64-bit */
1496 rbuf[2] = n_sectors >> (8 * 7);
1497 rbuf[3] = n_sectors >> (8 * 6);
1498 rbuf[4] = n_sectors >> (8 * 5);
1499 rbuf[5] = n_sectors >> (8 * 4);
1500 rbuf[6] = tmp >> (8 * 3);
1501 rbuf[7] = tmp >> (8 * 2);
1502 rbuf[8] = tmp >> (8 * 1);
1506 tmp = ATA_SECT_SIZE;
1507 rbuf[12] = tmp >> 8;
1515 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1516 * @args: device IDENTIFY data / SCSI command of interest.
1517 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1518 * @buflen: Response buffer length.
1520 * Simulate REPORT LUNS command.
1523 * spin_lock_irqsave(host_set lock)
1526 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1527 unsigned int buflen)
1530 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1536 * ata_scsi_badcmd - End a SCSI request with an error
1537 * @cmd: SCSI request to be handled
1538 * @done: SCSI command completion function
1539 * @asc: SCSI-defined additional sense code
1540 * @ascq: SCSI-defined additional sense code qualifier
1542 * Helper function that completes a SCSI command with
1543 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1544 * and the specified additional sense codes.
1547 * spin_lock_irqsave(host_set lock)
1550 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1553 cmd->result = SAM_STAT_CHECK_CONDITION;
1555 cmd->sense_buffer[0] = 0x70;
1556 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1557 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1558 cmd->sense_buffer[12] = asc;
1559 cmd->sense_buffer[13] = ascq;
1564 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1566 struct scsi_cmnd *cmd = qc->scsicmd;
1568 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1569 DPRINTK("request check condition\n");
1571 cmd->result = SAM_STAT_CHECK_CONDITION;
1577 u8 *scsicmd = cmd->cmnd;
1579 if (scsicmd[0] == INQUIRY) {
1581 unsigned int buflen;
1583 buflen = ata_scsi_rbuf_get(cmd, &buf);
1585 buf[3] = (buf[3] & 0xf0) | 2;
1586 ata_scsi_rbuf_put(cmd, buf);
1588 cmd->result = SAM_STAT_GOOD;
1596 * atapi_xlat - Initialize PACKET taskfile
1597 * @qc: command structure to be initialized
1598 * @scsicmd: SCSI CDB associated with this PACKET command
1601 * spin_lock_irqsave(host_set lock)
1604 * Zero on success, non-zero on failure.
1607 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1609 struct scsi_cmnd *cmd = qc->scsicmd;
1610 struct ata_device *dev = qc->dev;
1611 int using_pio = (dev->flags & ATA_DFLAG_PIO);
1612 int nodata = (cmd->sc_data_direction == SCSI_DATA_NONE);
1615 /* Check whether ATAPI DMA is safe */
1616 if (ata_check_atapi_dma(qc))
1619 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1621 qc->complete_fn = atapi_qc_complete;
1623 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1624 if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
1625 qc->tf.flags |= ATA_TFLAG_WRITE;
1626 DPRINTK("direction: write\n");
1629 qc->tf.command = ATA_CMD_PACKET;
1631 /* no data, or PIO data xfer */
1632 if (using_pio || nodata) {
1634 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1636 qc->tf.protocol = ATA_PROT_ATAPI;
1637 qc->tf.lbam = (8 * 1024) & 0xff;
1638 qc->tf.lbah = (8 * 1024) >> 8;
1643 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1644 qc->tf.feature |= ATAPI_PKT_DMA;
1646 #ifdef ATAPI_ENABLE_DMADIR
1647 /* some SATA bridges need us to indicate data xfer direction */
1648 if (cmd->sc_data_direction != SCSI_DATA_WRITE)
1649 qc->tf.feature |= ATAPI_DMADIR;
1653 qc->nbytes = cmd->bufflen;
1659 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1660 * @ap: ATA port to which the device is attached
1661 * @scsidev: SCSI device from which we derive the ATA device
1663 * Given various information provided in struct scsi_cmnd,
1664 * map that onto an ATA bus, and using that mapping
1665 * determine which ata_device is associated with the
1666 * SCSI command to be sent.
1669 * spin_lock_irqsave(host_set lock)
1672 * Associated ATA device, or %NULL if not found.
1675 static struct ata_device *
1676 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1678 struct ata_device *dev;
1680 /* skip commands not addressed to targets we simulate */
1681 if (likely(scsidev->id < ATA_MAX_DEVICES))
1682 dev = &ap->device[scsidev->id];
1686 if (unlikely((scsidev->channel != 0) ||
1687 (scsidev->lun != 0)))
1690 if (unlikely(!ata_dev_present(dev)))
1693 #ifndef ATA_ENABLE_ATAPI
1694 if (unlikely(dev->class == ATA_DEV_ATAPI))
1702 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
1703 * @byte1: Byte 1 from pass-thru CDB.
1706 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
1709 ata_scsi_map_proto(u8 byte1)
1711 switch((byte1 & 0x1e) >> 1) {
1712 case 3: /* Non-data */
1713 return ATA_PROT_NODATA;
1716 return ATA_PROT_DMA;
1718 case 4: /* PIO Data-in */
1719 case 5: /* PIO Data-out */
1721 return ATA_PROT_PIO_MULT;
1723 return ATA_PROT_PIO;
1725 case 10: /* Device Reset */
1726 case 0: /* Hard Reset */
1728 case 2: /* Bus Idle */
1729 case 7: /* Packet */
1730 case 8: /* DMA Queued */
1731 case 9: /* Device Diagnostic */
1732 case 11: /* UDMA Data-in */
1733 case 12: /* UDMA Data-Out */
1734 case 13: /* FPDMA */
1735 default: /* Reserved */
1739 return ATA_PROT_UNKNOWN;
1743 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
1744 * @qc: command structure to be initialized
1745 * @cmd: SCSI command to convert
1747 * Handles either 12 or 16-byte versions of the CDB.
1750 * Zero on success, non-zero on failure.
1753 ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
1755 struct ata_taskfile *tf = &(qc->tf);
1756 struct scsi_cmnd *cmd = qc->scsicmd;
1758 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
1762 * 12 and 16 byte CDBs use different offsets to
1763 * provide the various register values.
1765 if (scsicmd[0] == ATA_16) {
1767 * 16-byte CDB - may contain extended commands.
1769 * If that is the case, copy the upper byte register values.
1771 if (scsicmd[1] & 0x01) {
1772 tf->hob_feature = scsicmd[3];
1773 tf->hob_nsect = scsicmd[5];
1774 tf->hob_lbal = scsicmd[7];
1775 tf->hob_lbam = scsicmd[9];
1776 tf->hob_lbah = scsicmd[11];
1777 tf->flags |= ATA_TFLAG_LBA48;
1779 tf->flags &= ~ATA_TFLAG_LBA48;
1782 * Always copy low byte, device and command registers.
1784 tf->feature = scsicmd[4];
1785 tf->nsect = scsicmd[6];
1786 tf->lbal = scsicmd[8];
1787 tf->lbam = scsicmd[10];
1788 tf->lbah = scsicmd[12];
1789 tf->device = scsicmd[13];
1790 tf->command = scsicmd[14];
1793 * 12-byte CDB - incapable of extended commands.
1795 tf->flags &= ~ATA_TFLAG_LBA48;
1797 tf->feature = scsicmd[3];
1798 tf->nsect = scsicmd[4];
1799 tf->lbal = scsicmd[5];
1800 tf->lbam = scsicmd[6];
1801 tf->lbah = scsicmd[7];
1802 tf->device = scsicmd[8];
1803 tf->command = scsicmd[9];
1807 * Filter SET_FEATURES - XFER MODE command -- otherwise,
1808 * SET_FEATURES - XFER MODE must be preceded/succeeded
1809 * by an update to hardware-specific registers for each
1810 * controller (i.e. the reason for ->set_piomode(),
1811 * ->set_dmamode(), and ->post_set_mode() hooks).
1813 if ((tf->command == ATA_CMD_SET_FEATURES)
1814 && (tf->feature == SETFEATURES_XFER))
1818 * Set flags so that all registers will be written,
1819 * and pass on write indication (used for PIO/DMA
1822 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
1824 if (cmd->sc_data_direction == SCSI_DATA_WRITE)
1825 tf->flags |= ATA_TFLAG_WRITE;
1828 * Set transfer length.
1830 * TODO: find out if we need to do more here to
1831 * cover scatter/gather case.
1833 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
1839 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1841 * @cmd: SCSI command opcode to consider
1843 * Look up the SCSI command given, and determine whether the
1844 * SCSI command is to be translated or simulated.
1847 * Pointer to translation function if possible, %NULL if not.
1850 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1860 return ata_scsi_rw_xlat;
1862 case SYNCHRONIZE_CACHE:
1863 if (ata_try_flush_cache(dev))
1864 return ata_scsi_flush_xlat;
1869 return ata_scsi_verify_xlat;
1873 return ata_scsi_pass_thru;
1880 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1881 * @ap: ATA port to which the command was being sent
1882 * @cmd: SCSI command to dump
1884 * Prints the contents of a SCSI command via printk().
1887 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1888 struct scsi_cmnd *cmd)
1891 struct scsi_device *scsidev = cmd->device;
1892 u8 *scsicmd = cmd->cmnd;
1894 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1896 scsidev->channel, scsidev->id, scsidev->lun,
1897 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1898 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1904 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1905 * @cmd: SCSI command to be sent
1906 * @done: Completion function, called when command is complete
1908 * In some cases, this function translates SCSI commands into
1909 * ATA taskfiles, and queues the taskfiles to be sent to
1910 * hardware. In other cases, this function simulates a
1911 * SCSI device by evaluating and responding to certain
1912 * SCSI commands. This creates the overall effect of
1913 * ATA and ATAPI devices appearing as SCSI devices.
1916 * Releases scsi-layer-held lock, and obtains host_set lock.
1922 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1924 struct ata_port *ap;
1925 struct ata_device *dev;
1926 struct scsi_device *scsidev = cmd->device;
1928 ap = (struct ata_port *) &scsidev->host->hostdata[0];
1930 ata_scsi_dump_cdb(ap, cmd);
1932 dev = ata_scsi_find_dev(ap, scsidev);
1933 if (unlikely(!dev)) {
1934 cmd->result = (DID_BAD_TARGET << 16);
1939 if (dev->class == ATA_DEV_ATA) {
1940 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1944 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1946 ata_scsi_simulate(dev->id, cmd, done);
1948 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1955 * ata_scsi_simulate - simulate SCSI command on ATA device
1956 * @id: current IDENTIFY data for target device.
1957 * @cmd: SCSI command being sent to device.
1958 * @done: SCSI command completion function.
1960 * Interprets and directly executes a select list of SCSI commands
1961 * that can be handled internally.
1964 * spin_lock_irqsave(host_set lock)
1967 void ata_scsi_simulate(u16 *id,
1968 struct scsi_cmnd *cmd,
1969 void (*done)(struct scsi_cmnd *))
1971 struct ata_scsi_args args;
1972 u8 *scsicmd = cmd->cmnd;
1978 switch(scsicmd[0]) {
1979 /* no-op's, complete with success */
1980 case SYNCHRONIZE_CACHE:
1984 case TEST_UNIT_READY:
1985 case FORMAT_UNIT: /* FIXME: correct? */
1986 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1987 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1991 if (scsicmd[1] & 2) /* is CmdDt set? */
1992 ata_bad_cdb(cmd, done);
1993 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1994 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1995 else if (scsicmd[2] == 0x00)
1996 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1997 else if (scsicmd[2] == 0x80)
1998 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1999 else if (scsicmd[2] == 0x83)
2000 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2002 ata_bad_cdb(cmd, done);
2007 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2010 case MODE_SELECT: /* unconditionally return */
2011 case MODE_SELECT_10: /* bad-field-in-cdb */
2012 ata_bad_cdb(cmd, done);
2016 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2019 case SERVICE_ACTION_IN:
2020 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2021 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2023 ata_bad_cdb(cmd, done);
2027 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2030 /* mandatory commands we haven't implemented yet */
2033 /* all other commands */
2035 ata_bad_scsiop(cmd, done);