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
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)
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>
41 #include <scsi/scsi_host.h>
42 #include <linux/libata.h>
43 #include <linux/hdreg.h>
44 #include <asm/uaccess.h>
48 #define SECTOR_SIZE 512
50 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
51 static struct ata_device *
52 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
56 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
57 * @sdev: SCSI device for which BIOS geometry is to be determined
58 * @bdev: block device associated with @sdev
59 * @capacity: capacity of SCSI device
60 * @geom: location to which geometry will be output
62 * Generic bios head/sector/cylinder calculator
63 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
64 * mapping. Some situations may arise where the disk is not
65 * bootable if this is not used.
68 * Defined by the SCSI layer. We don't really care.
73 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
74 sector_t capacity, int geom[])
78 sector_div(capacity, 255*63);
85 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
86 * @dev: Device to whom we are issuing command
87 * @arg: User provided data for issuing command
90 * Defined by the SCSI layer. We don't really care.
93 * Zero on success, negative errno on error.
96 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
99 u8 scsi_cmd[MAX_COMMAND_SIZE];
100 u8 args[4], *argbuf = NULL;
102 struct scsi_request *sreq;
104 if (NULL == (void *)arg)
107 if (copy_from_user(args, arg, sizeof(args)))
110 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
114 memset(scsi_cmd, 0, sizeof(scsi_cmd));
117 argsize = SECTOR_SIZE * args[3];
118 argbuf = kmalloc(argsize, GFP_KERNEL);
122 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
123 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
124 block count in sector count field */
125 sreq->sr_data_direction = DMA_FROM_DEVICE;
127 scsi_cmd[1] = (3 << 1); /* Non-data */
128 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
129 sreq->sr_data_direction = DMA_NONE;
132 scsi_cmd[0] = ATA_16;
134 scsi_cmd[4] = args[2];
135 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
136 scsi_cmd[6] = args[3];
137 scsi_cmd[8] = args[1];
141 scsi_cmd[6] = args[1];
143 scsi_cmd[14] = args[0];
145 /* Good values for timeout and retries? Values below
146 from scsi_ioctl_send_command() for default case... */
147 scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
149 if (sreq->sr_result) {
154 /* Need code to retrieve data from check condition? */
157 && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
160 scsi_release_request(sreq);
169 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
170 * @dev: Device to whom we are issuing command
171 * @arg: User provided data for issuing command
174 * Defined by the SCSI layer. We don't really care.
177 * Zero on success, negative errno on error.
179 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
182 u8 scsi_cmd[MAX_COMMAND_SIZE];
184 struct scsi_request *sreq;
186 if (NULL == (void *)arg)
189 if (copy_from_user(args, arg, sizeof(args)))
192 memset(scsi_cmd, 0, sizeof(scsi_cmd));
193 scsi_cmd[0] = ATA_16;
194 scsi_cmd[1] = (3 << 1); /* Non-data */
195 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
196 scsi_cmd[4] = args[1];
197 scsi_cmd[6] = args[2];
198 scsi_cmd[8] = args[3];
199 scsi_cmd[10] = args[4];
200 scsi_cmd[12] = args[5];
201 scsi_cmd[14] = args[0];
203 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
209 sreq->sr_data_direction = DMA_NONE;
210 /* Good values for timeout and retries? Values below
211 from scsi_ioctl_send_command() for default case... */
212 scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
214 if (sreq->sr_result) {
219 /* Need code to retrieve data from check condition? */
222 scsi_release_request(sreq);
226 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
229 struct ata_device *dev;
230 int val = -EINVAL, rc = -EINVAL;
232 ap = (struct ata_port *) &scsidev->host->hostdata[0];
236 dev = ata_scsi_find_dev(ap, scsidev);
243 case ATA_IOC_GET_IO32:
245 if (copy_to_user(arg, &val, 1))
249 case ATA_IOC_SET_IO32:
250 val = (unsigned long) arg;
256 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
258 return ata_cmd_ioctl(scsidev, arg);
260 case HDIO_DRIVE_TASK:
261 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
263 return ata_task_ioctl(scsidev, arg);
275 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
276 * @ap: ATA port to which the new command is attached
277 * @dev: ATA device to which the new command is attached
278 * @cmd: SCSI command that originated this ATA command
279 * @done: SCSI command completion function
281 * Obtain a reference to an unused ata_queued_cmd structure,
282 * which is the basic libata structure representing a single
283 * ATA command sent to the hardware.
285 * If a command was available, fill in the SCSI-specific
286 * portions of the structure with information on the
290 * spin_lock_irqsave(host_set lock)
293 * Command allocated, or %NULL if none available.
295 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
296 struct ata_device *dev,
297 struct scsi_cmnd *cmd,
298 void (*done)(struct scsi_cmnd *))
300 struct ata_queued_cmd *qc;
302 qc = ata_qc_new_init(ap, dev);
308 qc->sg = (struct scatterlist *) cmd->request_buffer;
309 qc->n_elem = cmd->use_sg;
315 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
323 * ata_dump_status - user friendly display of error info
324 * @id: id of the port in question
325 * @tf: ptr to filled out taskfile
327 * Decode and dump the ATA error/status registers for the user so
328 * that they have some idea what really happened at the non
329 * make-believe layer.
332 * inherited from caller
334 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
336 u8 stat = tf->command, err = tf->feature;
338 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
339 if (stat & ATA_BUSY) {
340 printk("Busy }\n"); /* Data is not valid in this case */
342 if (stat & 0x40) printk("DriveReady ");
343 if (stat & 0x20) printk("DeviceFault ");
344 if (stat & 0x10) printk("SeekComplete ");
345 if (stat & 0x08) printk("DataRequest ");
346 if (stat & 0x04) printk("CorrectedError ");
347 if (stat & 0x02) printk("Index ");
348 if (stat & 0x01) printk("Error ");
352 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
353 if (err & 0x04) printk("DriveStatusError ");
355 if (err & 0x04) printk("BadCRC ");
356 else printk("Sector ");
358 if (err & 0x40) printk("UncorrectableError ");
359 if (err & 0x10) printk("SectorIdNotFound ");
360 if (err & 0x02) printk("TrackZeroNotFound ");
361 if (err & 0x01) printk("AddrMarkNotFound ");
368 * ata_to_sense_error - convert ATA error to SCSI error
369 * @drv_stat: value contained in ATA status register
370 * @drv_err: value contained in ATA error register
371 * @sk: the sense key we'll fill out
372 * @asc: the additional sense code we'll fill out
373 * @ascq: the additional sense code qualifier we'll fill out
375 * Converts an ATA error into a SCSI error. Fill out pointers to
376 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
377 * format sense blocks.
380 * spin_lock_irqsave(host_set lock)
382 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
386 /* Based on the 3ware driver translation table */
387 static unsigned char sense_table[][4] = {
389 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
391 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
393 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
394 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
395 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
396 /* MC|ID|ABRT|TRK0|MARK */
397 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
399 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
400 /* Bad address mark */
401 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
403 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
405 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
406 /* Media change request */
407 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
409 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
411 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
413 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
414 /* BBD - block marked bad */
415 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
416 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
418 static unsigned char stat_table[][4] = {
419 /* Must be first because BUSY means no other bits valid */
420 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
421 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
422 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
423 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
424 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
428 * Is this an error we can process/parse
430 if (drv_stat & ATA_BUSY) {
431 drv_err = 0; /* Ignore the err bits, they're invalid */
435 /* Look for drv_err */
436 for (i = 0; sense_table[i][0] != 0xFF; i++) {
437 /* Look for best matches first */
438 if ((sense_table[i][0] & drv_err) ==
440 *sk = sense_table[i][1];
441 *asc = sense_table[i][2];
442 *ascq = sense_table[i][3];
446 /* No immediate match */
447 printk(KERN_WARNING "ata%u: no sense translation for "
448 "error 0x%02x\n", id, drv_err);
451 /* Fall back to interpreting status bits */
452 for (i = 0; stat_table[i][0] != 0xFF; i++) {
453 if (stat_table[i][0] & drv_stat) {
454 *sk = stat_table[i][1];
455 *asc = stat_table[i][2];
456 *ascq = stat_table[i][3];
460 /* No error? Undecoded? */
461 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
464 /* For our last chance pick, use medium read error because
465 * it's much more common than an ATA drive telling you a write
469 *asc = 0x11; /* "unrecovered read error" */
470 *ascq = 0x04; /* "auto-reallocation failed" */
473 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
474 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
480 * ata_gen_ata_desc_sense - Generate check condition sense block.
481 * @qc: Command that completed.
483 * This function is specific to the ATA descriptor format sense
484 * block specified for the ATA pass through commands. Regardless
485 * of whether the command errored or not, return a sense
486 * block. Copy all controller registers into the sense
487 * block. Clear sense key, ASC & ASCQ if there is no error.
490 * spin_lock_irqsave(host_set lock)
492 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
494 struct scsi_cmnd *cmd = qc->scsicmd;
495 struct ata_taskfile *tf = &qc->tf;
496 unsigned char *sb = cmd->sense_buffer;
497 unsigned char *desc = sb + 8;
499 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
501 cmd->result = SAM_STAT_CHECK_CONDITION;
504 * Read the controller registers.
506 assert(NULL != qc->ap->ops->tf_read);
507 qc->ap->ops->tf_read(qc->ap, tf);
510 * Use ata_to_sense_error() to map status register bits
511 * onto sense key, asc & ascq.
513 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
514 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
515 &sb[1], &sb[2], &sb[3]);
520 * Sense data is current and format is descriptor.
527 * Set length of additional sense data.
528 * Since we only populate descriptor 0, the total
529 * length is the same (fixed) length as descriptor 0.
531 desc[1] = sb[7] = 14;
534 * Copy registers into sense buffer.
537 desc[3] = tf->feature; /* == error reg */
542 desc[12] = tf->device;
543 desc[13] = tf->command; /* == status reg */
546 * Fill in Extend bit, and the high order bytes
549 if (tf->flags & ATA_TFLAG_LBA48) {
551 desc[4] = tf->hob_nsect;
552 desc[6] = tf->hob_lbal;
553 desc[8] = tf->hob_lbam;
554 desc[10] = tf->hob_lbah;
559 * ata_gen_fixed_sense - generate a SCSI fixed sense block
560 * @qc: Command that we are erroring out
562 * Leverage ata_to_sense_error() to give us the codes. Fit our
563 * LBA in here if there's room.
566 * inherited from caller
568 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
570 struct scsi_cmnd *cmd = qc->scsicmd;
571 struct ata_taskfile *tf = &qc->tf;
572 unsigned char *sb = cmd->sense_buffer;
574 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
576 cmd->result = SAM_STAT_CHECK_CONDITION;
579 * Read the controller registers.
581 assert(NULL != qc->ap->ops->tf_read);
582 qc->ap->ops->tf_read(qc->ap, tf);
585 * Use ata_to_sense_error() to map status register bits
586 * onto sense key, asc & ascq.
588 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
589 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
590 &sb[2], &sb[12], &sb[13]);
597 #if 0 /* when C/H/S support is merged */
598 if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
600 if (!(tf->flags & ATA_TFLAG_LBA48)) {
601 /* A small (28b) LBA will fit in the 32b info field */
602 sb[0] |= 0x80; /* set valid bit */
603 sb[3] = tf->device & 0x0f;
611 * ata_scsi_slave_config - Set SCSI device attributes
612 * @sdev: SCSI device to examine
614 * This is called before we actually start reading
615 * and writing to the device, to configure certain
616 * SCSI mid-layer behaviors.
619 * Defined by SCSI layer. We don't really care.
622 int ata_scsi_slave_config(struct scsi_device *sdev)
624 sdev->use_10_for_rw = 1;
625 sdev->use_10_for_ms = 1;
627 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
629 if (sdev->id < ATA_MAX_DEVICES) {
631 struct ata_device *dev;
633 ap = (struct ata_port *) &sdev->host->hostdata[0];
634 dev = &ap->device[sdev->id];
636 /* TODO: 1024 is an arbitrary number, not the
637 * hardware maximum. This should be increased to
638 * 65534 when Jens Axboe's patch for dynamically
639 * determining max_sectors is merged.
641 if ((dev->flags & ATA_DFLAG_LBA48) &&
642 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
644 * do not overwrite sdev->host->max_sectors, since
645 * other drives on this host may not support LBA48
647 blk_queue_max_sectors(sdev->request_queue, 2048);
651 return 0; /* scsi layer doesn't check return value, sigh */
655 * ata_scsi_error - SCSI layer error handler callback
656 * @host: SCSI host on which error occurred
658 * Handles SCSI-layer-thrown error events.
661 * Inherited from SCSI layer (none, can sleep)
667 int ata_scsi_error(struct Scsi_Host *host)
673 ap = (struct ata_port *) &host->hostdata[0];
674 ap->ops->eng_timeout(ap);
676 /* TODO: this is per-command; when queueing is supported
677 * this code will either change or move to a more
681 INIT_LIST_HEAD(&host->eh_cmd_q);
688 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
689 * @qc: Storage for translated ATA taskfile
690 * @scsicmd: SCSI command to translate
692 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
693 * (to start). Perhaps these commands should be preceded by
694 * CHECK POWER MODE to see what power mode the device is already in.
695 * [See SAT revision 5 at www.t10.org]
698 * spin_lock_irqsave(host_set lock)
701 * Zero on success, non-zero on error.
704 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
707 struct ata_taskfile *tf = &qc->tf;
709 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
710 tf->protocol = ATA_PROT_NODATA;
711 if (scsicmd[1] & 0x1) {
712 ; /* ignore IMMED bit, violates sat-r05 */
714 if (scsicmd[4] & 0x2)
715 return 1; /* LOEJ bit set not supported */
716 if (((scsicmd[4] >> 4) & 0xf) != 0)
717 return 1; /* power conditions not supported */
718 if (scsicmd[4] & 0x1) {
719 tf->nsect = 1; /* 1 sector, lba=0 */
723 tf->device |= ATA_LBA;
724 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
726 tf->nsect = 0; /* time period value (0 implies now) */
727 tf->command = ATA_CMD_STANDBY;
728 /* Consider: ATA STANDBY IMMEDIATE command */
731 * Standby and Idle condition timers could be implemented but that
732 * would require libata to implement the Power condition mode page
733 * and allow the user to change it. Changing mode pages requires
734 * MODE SELECT to be implemented.
742 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
743 * @qc: Storage for translated ATA taskfile
744 * @scsicmd: SCSI command to translate (ignored)
746 * Sets up an ATA taskfile to issue FLUSH CACHE or
750 * spin_lock_irqsave(host_set lock)
753 * Zero on success, non-zero on error.
756 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
758 struct ata_taskfile *tf = &qc->tf;
760 tf->flags |= ATA_TFLAG_DEVICE;
761 tf->protocol = ATA_PROT_NODATA;
763 if ((tf->flags & ATA_TFLAG_LBA48) &&
764 (ata_id_has_flush_ext(qc->dev->id)))
765 tf->command = ATA_CMD_FLUSH_EXT;
767 tf->command = ATA_CMD_FLUSH;
773 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
774 * @qc: Storage for translated ATA taskfile
775 * @scsicmd: SCSI command to translate
777 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
780 * spin_lock_irqsave(host_set lock)
783 * Zero on success, non-zero on error.
786 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
788 struct ata_taskfile *tf = &qc->tf;
789 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
790 u64 dev_sectors = qc->dev->n_sectors;
794 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
795 tf->protocol = ATA_PROT_NODATA;
796 tf->device |= ATA_LBA;
798 if (scsicmd[0] == VERIFY) {
799 sect |= ((u64)scsicmd[2]) << 24;
800 sect |= ((u64)scsicmd[3]) << 16;
801 sect |= ((u64)scsicmd[4]) << 8;
802 sect |= ((u64)scsicmd[5]);
804 n_sect |= ((u32)scsicmd[7]) << 8;
805 n_sect |= ((u32)scsicmd[8]);
808 else if (scsicmd[0] == VERIFY_16) {
809 sect |= ((u64)scsicmd[2]) << 56;
810 sect |= ((u64)scsicmd[3]) << 48;
811 sect |= ((u64)scsicmd[4]) << 40;
812 sect |= ((u64)scsicmd[5]) << 32;
813 sect |= ((u64)scsicmd[6]) << 24;
814 sect |= ((u64)scsicmd[7]) << 16;
815 sect |= ((u64)scsicmd[8]) << 8;
816 sect |= ((u64)scsicmd[9]);
818 n_sect |= ((u32)scsicmd[10]) << 24;
819 n_sect |= ((u32)scsicmd[11]) << 16;
820 n_sect |= ((u32)scsicmd[12]) << 8;
821 n_sect |= ((u32)scsicmd[13]);
829 if (sect >= dev_sectors)
831 if ((sect + n_sect) > dev_sectors)
834 if (n_sect > (64 * 1024))
842 tf->command = ATA_CMD_VERIFY_EXT;
844 tf->hob_nsect = (n_sect >> 8) & 0xff;
846 tf->hob_lbah = (sect >> 40) & 0xff;
847 tf->hob_lbam = (sect >> 32) & 0xff;
848 tf->hob_lbal = (sect >> 24) & 0xff;
850 tf->command = ATA_CMD_VERIFY;
852 tf->device |= (sect >> 24) & 0xf;
855 tf->nsect = n_sect & 0xff;
857 tf->lbah = (sect >> 16) & 0xff;
858 tf->lbam = (sect >> 8) & 0xff;
859 tf->lbal = sect & 0xff;
865 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
866 * @qc: Storage for translated ATA taskfile
867 * @scsicmd: SCSI command to translate
869 * Converts any of six SCSI read/write commands into the
870 * ATA counterpart, including starting sector (LBA),
871 * sector count, and taking into account the device's LBA48
874 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
875 * %WRITE_16 are currently supported.
878 * spin_lock_irqsave(host_set lock)
881 * Zero on success, non-zero on error.
884 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
886 struct ata_taskfile *tf = &qc->tf;
887 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
889 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
890 tf->protocol = qc->dev->xfer_protocol;
891 tf->device |= ATA_LBA;
893 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
894 scsicmd[0] == READ_16) {
895 tf->command = qc->dev->read_cmd;
897 tf->command = qc->dev->write_cmd;
898 tf->flags |= ATA_TFLAG_WRITE;
901 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
903 tf->hob_nsect = scsicmd[7];
904 tf->hob_lbal = scsicmd[2];
906 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
909 /* if we don't support LBA48 addressing, the request
910 * -may- be too large. */
911 if ((scsicmd[2] & 0xf0) || scsicmd[7])
914 /* stores LBA27:24 in lower 4 bits of device reg */
915 tf->device |= scsicmd[2];
917 qc->nsect = scsicmd[8];
920 tf->nsect = scsicmd[8];
921 tf->lbal = scsicmd[5];
922 tf->lbam = scsicmd[4];
923 tf->lbah = scsicmd[3];
925 VPRINTK("ten-byte command\n");
926 if (qc->nsect == 0) /* we don't support length==0 cmds */
931 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
932 qc->nsect = tf->nsect = scsicmd[4];
939 tf->lbal = scsicmd[3];
940 tf->lbam = scsicmd[2];
941 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
943 VPRINTK("six-byte command\n");
947 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
948 /* rule out impossible LBAs and sector counts */
949 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
953 tf->hob_nsect = scsicmd[12];
954 tf->hob_lbal = scsicmd[6];
955 tf->hob_lbam = scsicmd[5];
956 tf->hob_lbah = scsicmd[4];
958 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
961 /* once again, filter out impossible non-zero values */
962 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
966 /* stores LBA27:24 in lower 4 bits of device reg */
967 tf->device |= scsicmd[6];
969 qc->nsect = scsicmd[13];
972 tf->nsect = scsicmd[13];
973 tf->lbal = scsicmd[9];
974 tf->lbam = scsicmd[8];
975 tf->lbah = scsicmd[7];
977 VPRINTK("sixteen-byte command\n");
978 if (qc->nsect == 0) /* we don't support length==0 cmds */
983 DPRINTK("no-byte command\n");
987 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
989 struct scsi_cmnd *cmd = qc->scsicmd;
990 int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
992 /* For ATA pass thru (SAT) commands, generate a sense block if
993 * user mandated it or if there's an error. Note that if we
994 * generate because the user forced us to, a check condition
995 * is generated and the ATA register values are returned
996 * whether the command completed successfully or not. If there
997 * was no error, SK, ASC and ASCQ will all be zero.
999 if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
1000 ((cmd->cmnd[2] & 0x20) || need_sense)) {
1001 ata_gen_ata_desc_sense(qc);
1004 cmd->result = SAM_STAT_GOOD;
1006 /* TODO: decide which descriptor format to use
1007 * for 48b LBA devices and call that here
1008 * instead of the fixed desc, which is only
1009 * good for smaller LBA (and maybe CHS?)
1012 ata_gen_fixed_sense(qc);
1017 /* The ata_gen_..._sense routines fill in tf */
1018 ata_dump_status(qc->ap->id, &qc->tf);
1027 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1028 * @ap: ATA port to which the command is addressed
1029 * @dev: ATA device to which the command is addressed
1030 * @cmd: SCSI command to execute
1031 * @done: SCSI command completion function
1032 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1034 * Our ->queuecommand() function has decided that the SCSI
1035 * command issued can be directly translated into an ATA
1036 * command, rather than handled internally.
1038 * This function sets up an ata_queued_cmd structure for the
1039 * SCSI command, and sends that ata_queued_cmd to the hardware.
1042 * spin_lock_irqsave(host_set lock)
1045 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1046 struct scsi_cmnd *cmd,
1047 void (*done)(struct scsi_cmnd *),
1048 ata_xlat_func_t xlat_func)
1050 struct ata_queued_cmd *qc;
1051 u8 *scsicmd = cmd->cmnd;
1055 qc = ata_scsi_qc_new(ap, dev, cmd, done);
1059 /* data is present; dma-map it */
1060 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1061 cmd->sc_data_direction == DMA_TO_DEVICE) {
1062 if (unlikely(cmd->request_bufflen < 1)) {
1063 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1064 ap->id, dev->devno);
1069 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1071 ata_sg_init_one(qc, cmd->request_buffer,
1072 cmd->request_bufflen);
1074 qc->dma_dir = cmd->sc_data_direction;
1077 qc->complete_fn = ata_scsi_qc_complete;
1079 if (xlat_func(qc, scsicmd))
1081 /* select device, send command to hardware */
1082 if (ata_qc_issue(qc))
1090 ata_bad_cdb(cmd, done);
1091 DPRINTK("EXIT - badcmd\n");
1095 * ata_scsi_rbuf_get - Map response buffer.
1096 * @cmd: SCSI command containing buffer to be mapped.
1097 * @buf_out: Pointer to mapped area.
1099 * Maps buffer contained within SCSI command @cmd.
1102 * spin_lock_irqsave(host_set lock)
1105 * Length of response buffer.
1108 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1111 unsigned int buflen;
1114 struct scatterlist *sg;
1116 sg = (struct scatterlist *) cmd->request_buffer;
1117 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1118 buflen = sg->length;
1120 buf = cmd->request_buffer;
1121 buflen = cmd->request_bufflen;
1129 * ata_scsi_rbuf_put - Unmap response buffer.
1130 * @cmd: SCSI command containing buffer to be unmapped.
1131 * @buf: buffer to unmap
1133 * Unmaps response buffer contained within @cmd.
1136 * spin_lock_irqsave(host_set lock)
1139 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1142 struct scatterlist *sg;
1144 sg = (struct scatterlist *) cmd->request_buffer;
1145 kunmap_atomic(buf - sg->offset, KM_USER0);
1150 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1151 * @args: device IDENTIFY data / SCSI command of interest.
1152 * @actor: Callback hook for desired SCSI command simulator
1154 * Takes care of the hard work of simulating a SCSI command...
1155 * Mapping the response buffer, calling the command's handler,
1156 * and handling the handler's return value. This return value
1157 * indicates whether the handler wishes the SCSI command to be
1158 * completed successfully, or not.
1161 * spin_lock_irqsave(host_set lock)
1164 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1165 unsigned int (*actor) (struct ata_scsi_args *args,
1166 u8 *rbuf, unsigned int buflen))
1169 unsigned int buflen, rc;
1170 struct scsi_cmnd *cmd = args->cmd;
1172 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1173 memset(rbuf, 0, buflen);
1174 rc = actor(args, rbuf, buflen);
1175 ata_scsi_rbuf_put(cmd, rbuf);
1178 ata_bad_cdb(cmd, args->done);
1180 cmd->result = SAM_STAT_GOOD;
1186 * ata_scsiop_inq_std - Simulate INQUIRY command
1187 * @args: device IDENTIFY data / SCSI command of interest.
1188 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1189 * @buflen: Response buffer length.
1191 * Returns standard device identification data associated
1192 * with non-EVPD INQUIRY command output.
1195 * spin_lock_irqsave(host_set lock)
1198 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1199 unsigned int buflen)
1204 0x5, /* claim SPC-3 version compatibility */
1209 /* set scsi removeable (RMB) bit per ata bit */
1210 if (ata_id_removeable(args->id))
1215 memcpy(rbuf, hdr, sizeof(hdr));
1218 memcpy(&rbuf[8], "ATA ", 8);
1219 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1220 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1221 if (rbuf[32] == 0 || rbuf[32] == ' ')
1222 memcpy(&rbuf[32], "n/a ", 4);
1226 const u8 versions[] = {
1227 0x60, /* SAM-3 (no version claimed) */
1230 0x20, /* SBC-2 (no version claimed) */
1233 0x60 /* SPC-3 (no version claimed) */
1236 memcpy(rbuf + 59, versions, sizeof(versions));
1243 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1244 * @args: device IDENTIFY data / SCSI command of interest.
1245 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1246 * @buflen: Response buffer length.
1248 * Returns list of inquiry EVPD pages available.
1251 * spin_lock_irqsave(host_set lock)
1254 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1255 unsigned int buflen)
1257 const u8 pages[] = {
1258 0x00, /* page 0x00, this page */
1259 0x80, /* page 0x80, unit serial no page */
1260 0x83 /* page 0x83, device ident page */
1262 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
1265 memcpy(rbuf + 4, pages, sizeof(pages));
1271 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1272 * @args: device IDENTIFY data / SCSI command of interest.
1273 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1274 * @buflen: Response buffer length.
1276 * Returns ATA device serial number.
1279 * spin_lock_irqsave(host_set lock)
1282 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1283 unsigned int buflen)
1287 0x80, /* this page code */
1289 ATA_SERNO_LEN, /* page len */
1291 memcpy(rbuf, hdr, sizeof(hdr));
1293 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1294 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1295 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1300 static const char *inq_83_str = "Linux ATA-SCSI simulator";
1303 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1304 * @args: device IDENTIFY data / SCSI command of interest.
1305 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1306 * @buflen: Response buffer length.
1308 * Returns device identification. Currently hardcoded to
1309 * return "Linux ATA-SCSI simulator".
1312 * spin_lock_irqsave(host_set lock)
1315 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1316 unsigned int buflen)
1318 rbuf[1] = 0x83; /* this page code */
1319 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
1321 /* our one and only identification descriptor (vendor-specific) */
1322 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1323 rbuf[4 + 0] = 2; /* code set: ASCII */
1324 rbuf[4 + 3] = strlen(inq_83_str);
1325 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1332 * ata_scsiop_noop - Command handler that simply returns success.
1333 * @args: device IDENTIFY data / SCSI command of interest.
1334 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1335 * @buflen: Response buffer length.
1337 * No operation. Simply returns success to caller, to indicate
1338 * that the caller should successfully complete this SCSI command.
1341 * spin_lock_irqsave(host_set lock)
1344 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1345 unsigned int buflen)
1352 * ata_msense_push - Push data onto MODE SENSE data output buffer
1353 * @ptr_io: (input/output) Location to store more output data
1354 * @last: End of output data buffer
1355 * @buf: Pointer to BLOB being added to output buffer
1356 * @buflen: Length of BLOB
1358 * Store MODE SENSE data on an output buffer.
1364 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1365 const u8 *buf, unsigned int buflen)
1369 if ((ptr + buflen - 1) > last)
1372 memcpy(ptr, buf, buflen);
1380 * ata_msense_caching - Simulate MODE SENSE caching info page
1381 * @id: device IDENTIFY data
1382 * @ptr_io: (input/output) Location to store more output data
1383 * @last: End of output data buffer
1385 * Generate a caching info page, which conditionally indicates
1386 * write caching to the SCSI layer, depending on device
1393 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1397 0x8, /* page code */
1398 0x12, /* page length */
1399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1400 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1403 if (ata_id_wcache_enabled(id))
1404 page[2] |= (1 << 2); /* write cache enable */
1405 if (!ata_id_rahead_enabled(id))
1406 page[12] |= (1 << 5); /* disable read ahead */
1408 ata_msense_push(ptr_io, last, page, sizeof(page));
1409 return sizeof(page);
1413 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1414 * @dev: Device associated with this MODE SENSE command
1415 * @ptr_io: (input/output) Location to store more output data
1416 * @last: End of output data buffer
1418 * Generate a generic MODE SENSE control mode page.
1424 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1426 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1428 /* byte 2: set the descriptor format sense data bit (bit 2)
1429 * since we need to support returning this format for SAT
1430 * commands and any SCSI commands against a 48b LBA device.
1433 ata_msense_push(ptr_io, last, page, sizeof(page));
1434 return sizeof(page);
1438 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1439 * @dev: Device associated with this MODE SENSE command
1440 * @ptr_io: (input/output) Location to store more output data
1441 * @last: End of output data buffer
1443 * Generate a generic MODE SENSE r/w error recovery page.
1449 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1452 0x1, /* page code */
1453 0xa, /* page length */
1454 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1455 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1458 ata_msense_push(ptr_io, last, page, sizeof(page));
1459 return sizeof(page);
1463 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1464 * @args: device IDENTIFY data / SCSI command of interest.
1465 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1466 * @buflen: Response buffer length.
1468 * Simulate MODE SENSE commands.
1471 * spin_lock_irqsave(host_set lock)
1474 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1475 unsigned int buflen)
1477 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1478 unsigned int page_control, six_byte, output_len;
1482 six_byte = (scsicmd[0] == MODE_SENSE);
1484 /* we only support saved and current values (which we treat
1485 * in the same manner)
1487 page_control = scsicmd[2] >> 6;
1488 if ((page_control != 0) && (page_control != 3))
1496 p = rbuf + output_len;
1497 last = rbuf + buflen - 1;
1499 switch(scsicmd[2] & 0x3f) {
1500 case 0x01: /* r/w error recovery */
1501 output_len += ata_msense_rw_recovery(&p, last);
1504 case 0x08: /* caching */
1505 output_len += ata_msense_caching(args->id, &p, last);
1508 case 0x0a: { /* control mode */
1509 output_len += ata_msense_ctl_mode(&p, last);
1513 case 0x3f: /* all pages */
1514 output_len += ata_msense_rw_recovery(&p, last);
1515 output_len += ata_msense_caching(args->id, &p, last);
1516 output_len += ata_msense_ctl_mode(&p, last);
1519 default: /* invalid page code */
1525 rbuf[0] = output_len;
1528 rbuf[0] = output_len >> 8;
1529 rbuf[1] = output_len;
1536 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1537 * @args: device IDENTIFY data / SCSI command of interest.
1538 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1539 * @buflen: Response buffer length.
1541 * Simulate READ CAPACITY commands.
1544 * spin_lock_irqsave(host_set lock)
1547 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1548 unsigned int buflen)
1555 if (ata_id_has_lba48(args->id))
1556 n_sectors = ata_id_u64(args->id, 100);
1558 n_sectors = ata_id_u32(args->id, 60);
1559 n_sectors--; /* ATA TotalUserSectors - 1 */
1561 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1562 if( n_sectors >= 0xffffffffULL )
1563 tmp = 0xffffffff ; /* Return max count on overflow */
1567 /* sector count, 32-bit */
1568 rbuf[0] = tmp >> (8 * 3);
1569 rbuf[1] = tmp >> (8 * 2);
1570 rbuf[2] = tmp >> (8 * 1);
1574 tmp = ATA_SECT_SIZE;
1579 /* sector count, 64-bit */
1580 tmp = n_sectors >> (8 * 4);
1581 rbuf[2] = tmp >> (8 * 3);
1582 rbuf[3] = tmp >> (8 * 2);
1583 rbuf[4] = tmp >> (8 * 1);
1586 rbuf[6] = tmp >> (8 * 3);
1587 rbuf[7] = tmp >> (8 * 2);
1588 rbuf[8] = tmp >> (8 * 1);
1592 tmp = ATA_SECT_SIZE;
1593 rbuf[12] = tmp >> 8;
1601 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1602 * @args: device IDENTIFY data / SCSI command of interest.
1603 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1604 * @buflen: Response buffer length.
1606 * Simulate REPORT LUNS command.
1609 * spin_lock_irqsave(host_set lock)
1612 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1613 unsigned int buflen)
1616 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1622 * ata_scsi_badcmd - End a SCSI request with an error
1623 * @cmd: SCSI request to be handled
1624 * @done: SCSI command completion function
1625 * @asc: SCSI-defined additional sense code
1626 * @ascq: SCSI-defined additional sense code qualifier
1628 * Helper function that completes a SCSI command with
1629 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1630 * and the specified additional sense codes.
1633 * spin_lock_irqsave(host_set lock)
1636 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1639 cmd->result = SAM_STAT_CHECK_CONDITION;
1641 cmd->sense_buffer[0] = 0x70;
1642 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1643 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1644 cmd->sense_buffer[12] = asc;
1645 cmd->sense_buffer[13] = ascq;
1650 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1652 struct scsi_cmnd *cmd = qc->scsicmd;
1654 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1655 DPRINTK("request check condition\n");
1657 cmd->result = SAM_STAT_CHECK_CONDITION;
1663 u8 *scsicmd = cmd->cmnd;
1665 if (scsicmd[0] == INQUIRY) {
1667 unsigned int buflen;
1669 buflen = ata_scsi_rbuf_get(cmd, &buf);
1671 buf[3] = (buf[3] & 0xf0) | 2;
1672 ata_scsi_rbuf_put(cmd, buf);
1674 cmd->result = SAM_STAT_GOOD;
1682 * atapi_xlat - Initialize PACKET taskfile
1683 * @qc: command structure to be initialized
1684 * @scsicmd: SCSI CDB associated with this PACKET command
1687 * spin_lock_irqsave(host_set lock)
1690 * Zero on success, non-zero on failure.
1693 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1695 struct scsi_cmnd *cmd = qc->scsicmd;
1696 struct ata_device *dev = qc->dev;
1697 int using_pio = (dev->flags & ATA_DFLAG_PIO);
1698 int nodata = (cmd->sc_data_direction == DMA_NONE);
1701 /* Check whether ATAPI DMA is safe */
1702 if (ata_check_atapi_dma(qc))
1705 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1707 qc->complete_fn = atapi_qc_complete;
1709 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1710 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1711 qc->tf.flags |= ATA_TFLAG_WRITE;
1712 DPRINTK("direction: write\n");
1715 qc->tf.command = ATA_CMD_PACKET;
1717 /* no data, or PIO data xfer */
1718 if (using_pio || nodata) {
1720 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1722 qc->tf.protocol = ATA_PROT_ATAPI;
1723 qc->tf.lbam = (8 * 1024) & 0xff;
1724 qc->tf.lbah = (8 * 1024) >> 8;
1729 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1730 qc->tf.feature |= ATAPI_PKT_DMA;
1732 #ifdef ATAPI_ENABLE_DMADIR
1733 /* some SATA bridges need us to indicate data xfer direction */
1734 if (cmd->sc_data_direction != DMA_TO_DEVICE)
1735 qc->tf.feature |= ATAPI_DMADIR;
1739 qc->nbytes = cmd->bufflen;
1745 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1746 * @ap: ATA port to which the device is attached
1747 * @scsidev: SCSI device from which we derive the ATA device
1749 * Given various information provided in struct scsi_cmnd,
1750 * map that onto an ATA bus, and using that mapping
1751 * determine which ata_device is associated with the
1752 * SCSI command to be sent.
1755 * spin_lock_irqsave(host_set lock)
1758 * Associated ATA device, or %NULL if not found.
1761 static struct ata_device *
1762 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1764 struct ata_device *dev;
1766 /* skip commands not addressed to targets we simulate */
1767 if (likely(scsidev->id < ATA_MAX_DEVICES))
1768 dev = &ap->device[scsidev->id];
1772 if (unlikely((scsidev->channel != 0) ||
1773 (scsidev->lun != 0)))
1776 if (unlikely(!ata_dev_present(dev)))
1779 if (!atapi_enabled) {
1780 if (unlikely(dev->class == ATA_DEV_ATAPI))
1788 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
1789 * @byte1: Byte 1 from pass-thru CDB.
1792 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
1795 ata_scsi_map_proto(u8 byte1)
1797 switch((byte1 & 0x1e) >> 1) {
1798 case 3: /* Non-data */
1799 return ATA_PROT_NODATA;
1802 return ATA_PROT_DMA;
1804 case 4: /* PIO Data-in */
1805 case 5: /* PIO Data-out */
1807 return ATA_PROT_PIO_MULT;
1809 return ATA_PROT_PIO;
1811 case 10: /* Device Reset */
1812 case 0: /* Hard Reset */
1814 case 2: /* Bus Idle */
1815 case 7: /* Packet */
1816 case 8: /* DMA Queued */
1817 case 9: /* Device Diagnostic */
1818 case 11: /* UDMA Data-in */
1819 case 12: /* UDMA Data-Out */
1820 case 13: /* FPDMA */
1821 default: /* Reserved */
1825 return ATA_PROT_UNKNOWN;
1829 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
1830 * @qc: command structure to be initialized
1831 * @cmd: SCSI command to convert
1833 * Handles either 12 or 16-byte versions of the CDB.
1836 * Zero on success, non-zero on failure.
1839 ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
1841 struct ata_taskfile *tf = &(qc->tf);
1842 struct scsi_cmnd *cmd = qc->scsicmd;
1844 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
1848 * 12 and 16 byte CDBs use different offsets to
1849 * provide the various register values.
1851 if (scsicmd[0] == ATA_16) {
1853 * 16-byte CDB - may contain extended commands.
1855 * If that is the case, copy the upper byte register values.
1857 if (scsicmd[1] & 0x01) {
1858 tf->hob_feature = scsicmd[3];
1859 tf->hob_nsect = scsicmd[5];
1860 tf->hob_lbal = scsicmd[7];
1861 tf->hob_lbam = scsicmd[9];
1862 tf->hob_lbah = scsicmd[11];
1863 tf->flags |= ATA_TFLAG_LBA48;
1865 tf->flags &= ~ATA_TFLAG_LBA48;
1868 * Always copy low byte, device and command registers.
1870 tf->feature = scsicmd[4];
1871 tf->nsect = scsicmd[6];
1872 tf->lbal = scsicmd[8];
1873 tf->lbam = scsicmd[10];
1874 tf->lbah = scsicmd[12];
1875 tf->device = scsicmd[13];
1876 tf->command = scsicmd[14];
1879 * 12-byte CDB - incapable of extended commands.
1881 tf->flags &= ~ATA_TFLAG_LBA48;
1883 tf->feature = scsicmd[3];
1884 tf->nsect = scsicmd[4];
1885 tf->lbal = scsicmd[5];
1886 tf->lbam = scsicmd[6];
1887 tf->lbah = scsicmd[7];
1888 tf->device = scsicmd[8];
1889 tf->command = scsicmd[9];
1893 * Filter SET_FEATURES - XFER MODE command -- otherwise,
1894 * SET_FEATURES - XFER MODE must be preceded/succeeded
1895 * by an update to hardware-specific registers for each
1896 * controller (i.e. the reason for ->set_piomode(),
1897 * ->set_dmamode(), and ->post_set_mode() hooks).
1899 if ((tf->command == ATA_CMD_SET_FEATURES)
1900 && (tf->feature == SETFEATURES_XFER))
1904 * Set flags so that all registers will be written,
1905 * and pass on write indication (used for PIO/DMA
1908 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
1910 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1911 tf->flags |= ATA_TFLAG_WRITE;
1914 * Set transfer length.
1916 * TODO: find out if we need to do more here to
1917 * cover scatter/gather case.
1919 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
1925 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1927 * @cmd: SCSI command opcode to consider
1929 * Look up the SCSI command given, and determine whether the
1930 * SCSI command is to be translated or simulated.
1933 * Pointer to translation function if possible, %NULL if not.
1936 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1946 return ata_scsi_rw_xlat;
1948 case SYNCHRONIZE_CACHE:
1949 if (ata_try_flush_cache(dev))
1950 return ata_scsi_flush_xlat;
1955 return ata_scsi_verify_xlat;
1959 return ata_scsi_pass_thru;
1962 return ata_scsi_start_stop_xlat;
1969 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1970 * @ap: ATA port to which the command was being sent
1971 * @cmd: SCSI command to dump
1973 * Prints the contents of a SCSI command via printk().
1976 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1977 struct scsi_cmnd *cmd)
1980 struct scsi_device *scsidev = cmd->device;
1981 u8 *scsicmd = cmd->cmnd;
1983 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1985 scsidev->channel, scsidev->id, scsidev->lun,
1986 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1987 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1993 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1994 * @cmd: SCSI command to be sent
1995 * @done: Completion function, called when command is complete
1997 * In some cases, this function translates SCSI commands into
1998 * ATA taskfiles, and queues the taskfiles to be sent to
1999 * hardware. In other cases, this function simulates a
2000 * SCSI device by evaluating and responding to certain
2001 * SCSI commands. This creates the overall effect of
2002 * ATA and ATAPI devices appearing as SCSI devices.
2005 * Releases scsi-layer-held lock, and obtains host_set lock.
2011 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2013 struct ata_port *ap;
2014 struct ata_device *dev;
2015 struct scsi_device *scsidev = cmd->device;
2017 ap = (struct ata_port *) &scsidev->host->hostdata[0];
2019 ata_scsi_dump_cdb(ap, cmd);
2021 dev = ata_scsi_find_dev(ap, scsidev);
2022 if (unlikely(!dev)) {
2023 cmd->result = (DID_BAD_TARGET << 16);
2028 if (dev->class == ATA_DEV_ATA) {
2029 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2033 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
2035 ata_scsi_simulate(dev->id, cmd, done);
2037 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
2044 * ata_scsi_simulate - simulate SCSI command on ATA device
2045 * @id: current IDENTIFY data for target device.
2046 * @cmd: SCSI command being sent to device.
2047 * @done: SCSI command completion function.
2049 * Interprets and directly executes a select list of SCSI commands
2050 * that can be handled internally.
2053 * spin_lock_irqsave(host_set lock)
2056 void ata_scsi_simulate(u16 *id,
2057 struct scsi_cmnd *cmd,
2058 void (*done)(struct scsi_cmnd *))
2060 struct ata_scsi_args args;
2061 u8 *scsicmd = cmd->cmnd;
2067 switch(scsicmd[0]) {
2068 /* no-op's, complete with success */
2069 case SYNCHRONIZE_CACHE:
2073 case TEST_UNIT_READY:
2074 case FORMAT_UNIT: /* FIXME: correct? */
2075 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2076 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2080 if (scsicmd[1] & 2) /* is CmdDt set? */
2081 ata_bad_cdb(cmd, done);
2082 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2083 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2084 else if (scsicmd[2] == 0x00)
2085 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2086 else if (scsicmd[2] == 0x80)
2087 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2088 else if (scsicmd[2] == 0x83)
2089 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2091 ata_bad_cdb(cmd, done);
2096 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2099 case MODE_SELECT: /* unconditionally return */
2100 case MODE_SELECT_10: /* bad-field-in-cdb */
2101 ata_bad_cdb(cmd, done);
2105 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2108 case SERVICE_ACTION_IN:
2109 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2110 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2112 ata_bad_cdb(cmd, done);
2116 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2119 /* mandatory commands we haven't implemented yet */
2122 /* all other commands */
2124 ata_bad_scsiop(cmd, done);