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 <asm/uaccess.h>
36 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
37 static struct ata_device *
38 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
42 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
43 * @sdev: SCSI device for which BIOS geometry is to be determined
44 * @bdev: block device associated with @sdev
45 * @capacity: capacity of SCSI device
46 * @geom: location to which geometry will be output
48 * Generic bios head/sector/cylinder calculator
49 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
50 * mapping. Some situations may arise where the disk is not
51 * bootable if this is not used.
54 * Defined by the SCSI layer. We don't really care.
59 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
60 sector_t capacity, int geom[])
64 sector_div(capacity, 255*63);
70 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
73 struct ata_device *dev;
74 int val = -EINVAL, rc = -EINVAL;
76 ap = (struct ata_port *) &scsidev->host->hostdata[0];
80 dev = ata_scsi_find_dev(ap, scsidev);
87 case ATA_IOC_GET_IO32:
89 if (copy_to_user(arg, &val, 1))
93 case ATA_IOC_SET_IO32:
94 val = (unsigned long) arg;
109 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
110 * @ap: ATA port to which the new command is attached
111 * @dev: ATA device to which the new command is attached
112 * @cmd: SCSI command that originated this ATA command
113 * @done: SCSI command completion function
115 * Obtain a reference to an unused ata_queued_cmd structure,
116 * which is the basic libata structure representing a single
117 * ATA command sent to the hardware.
119 * If a command was available, fill in the SCSI-specific
120 * portions of the structure with information on the
124 * spin_lock_irqsave(host_set lock)
127 * Command allocated, or %NULL if none available.
129 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
130 struct ata_device *dev,
131 struct scsi_cmnd *cmd,
132 void (*done)(struct scsi_cmnd *))
134 struct ata_queued_cmd *qc;
136 qc = ata_qc_new_init(ap, dev);
142 qc->sg = (struct scatterlist *) cmd->request_buffer;
143 qc->n_elem = cmd->use_sg;
149 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
157 * ata_to_sense_error - convert ATA error to SCSI error
158 * @qc: Command that we are erroring out
160 * Converts an ATA error into a SCSI error. While we are at it
161 * we decode and dump the ATA error for the user so that they
162 * have some idea what really happened at the non make-believe
166 * spin_lock_irqsave(host_set lock)
169 void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
171 struct scsi_cmnd *cmd = qc->scsicmd;
173 unsigned char *sb = cmd->sense_buffer;
174 /* Based on the 3ware driver translation table */
175 static unsigned char sense_table[][4] = {
177 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
179 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
181 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
182 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
183 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
184 /* MC|ID|ABRT|TRK0|MARK */
185 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
187 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
188 /* Bad address mark */
189 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
191 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
193 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
194 /* Media change request */
195 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
197 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
199 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
201 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
202 /* BBD - block marked bad */
203 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
204 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
206 static unsigned char stat_table[][4] = {
207 /* Must be first because BUSY means no other bits valid */
208 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
209 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
210 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
211 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
212 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
216 cmd->result = SAM_STAT_CHECK_CONDITION;
219 * Is this an error we can process/parse
222 if(drv_stat & ATA_ERR)
223 /* Read the err bits */
224 err = ata_chk_err(qc->ap);
226 /* Display the ATA level error info */
228 printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
232 err = 0; /* Data is not valid in this case */
235 if(drv_stat & 0x40) printk("DriveReady ");
236 if(drv_stat & 0x20) printk("DeviceFault ");
237 if(drv_stat & 0x10) printk("SeekComplete ");
238 if(drv_stat & 0x08) printk("DataRequest ");
239 if(drv_stat & 0x04) printk("CorrectedError ");
240 if(drv_stat & 0x02) printk("Index ");
241 if(drv_stat & 0x01) printk("Error ");
247 printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err);
248 if(err & 0x04) printk("DriveStatusError ");
256 if(err & 0x40) printk("UncorrectableError ");
257 if(err & 0x10) printk("SectorIdNotFound ");
258 if(err & 0x02) printk("TrackZeroNotFound ");
259 if(err & 0x01) printk("AddrMarkNotFound ");
262 /* Should we dump sector info here too ?? */
267 while(sense_table[i][0] != 0xFF)
269 /* Look for best matches first */
270 if((sense_table[i][0] & err) == sense_table[i][0])
273 sb[2] = sense_table[i][1];
275 sb[12] = sense_table[i][2];
276 sb[13] = sense_table[i][3];
281 /* No immediate match */
283 printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
285 /* Fall back to interpreting status bits */
286 while(stat_table[i][0] != 0xFF)
288 if(stat_table[i][0] & drv_stat)
291 sb[2] = stat_table[i][1];
293 sb[12] = stat_table[i][2];
294 sb[13] = stat_table[i][3];
300 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
301 /* additional-sense-code[-qualifier] */
304 sb[2] = MEDIUM_ERROR;
306 if (cmd->sc_data_direction == SCSI_DATA_READ) {
307 sb[12] = 0x11; /* "unrecovered read error" */
310 sb[12] = 0x0C; /* "write error - */
311 sb[13] = 0x02; /* auto-reallocation failed" */
316 * ata_scsi_slave_config - Set SCSI device attributes
317 * @sdev: SCSI device to examine
319 * This is called before we actually start reading
320 * and writing to the device, to configure certain
321 * SCSI mid-layer behaviors.
324 * Defined by SCSI layer. We don't really care.
327 int ata_scsi_slave_config(struct scsi_device *sdev)
329 sdev->use_10_for_rw = 1;
330 sdev->use_10_for_ms = 1;
332 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
334 if (sdev->id < ATA_MAX_DEVICES) {
336 struct ata_device *dev;
338 ap = (struct ata_port *) &sdev->host->hostdata[0];
339 dev = &ap->device[sdev->id];
341 /* TODO: 1024 is an arbitrary number, not the
342 * hardware maximum. This should be increased to
343 * 65534 when Jens Axboe's patch for dynamically
344 * determining max_sectors is merged.
346 if ((dev->flags & ATA_DFLAG_LBA48) &&
347 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
348 sdev->host->max_sectors = 2048;
349 blk_queue_max_sectors(sdev->request_queue, 2048);
353 return 0; /* scsi layer doesn't check return value, sigh */
357 * ata_scsi_error - SCSI layer error handler callback
358 * @host: SCSI host on which error occurred
360 * Handles SCSI-layer-thrown error events.
363 * Inherited from SCSI layer (none, can sleep)
369 int ata_scsi_error(struct Scsi_Host *host)
375 ap = (struct ata_port *) &host->hostdata[0];
376 ap->ops->eng_timeout(ap);
378 /* TODO: this is per-command; when queueing is supported
379 * this code will either change or move to a more
389 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
390 * @qc: Storage for translated ATA taskfile
391 * @scsicmd: SCSI command to translate (ignored)
393 * Sets up an ATA taskfile to issue FLUSH CACHE or
397 * spin_lock_irqsave(host_set lock)
400 * Zero on success, non-zero on error.
403 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
405 struct ata_taskfile *tf = &qc->tf;
407 tf->flags |= ATA_TFLAG_DEVICE;
408 tf->protocol = ATA_PROT_NODATA;
410 if ((tf->flags & ATA_TFLAG_LBA48) &&
411 (ata_id_has_flush_ext(qc->dev->id)))
412 tf->command = ATA_CMD_FLUSH_EXT;
414 tf->command = ATA_CMD_FLUSH;
420 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
421 * @qc: Storage for translated ATA taskfile
422 * @scsicmd: SCSI command to translate
424 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
427 * spin_lock_irqsave(host_set lock)
430 * Zero on success, non-zero on error.
433 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
435 struct ata_taskfile *tf = &qc->tf;
436 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
437 u64 dev_sectors = qc->dev->n_sectors;
441 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
442 tf->protocol = ATA_PROT_NODATA;
443 tf->device |= ATA_LBA;
445 if (scsicmd[0] == VERIFY) {
446 sect |= ((u64)scsicmd[2]) << 24;
447 sect |= ((u64)scsicmd[3]) << 16;
448 sect |= ((u64)scsicmd[4]) << 8;
449 sect |= ((u64)scsicmd[5]);
451 n_sect |= ((u32)scsicmd[7]) << 8;
452 n_sect |= ((u32)scsicmd[8]);
455 else if (scsicmd[0] == VERIFY_16) {
456 sect |= ((u64)scsicmd[2]) << 56;
457 sect |= ((u64)scsicmd[3]) << 48;
458 sect |= ((u64)scsicmd[4]) << 40;
459 sect |= ((u64)scsicmd[5]) << 32;
460 sect |= ((u64)scsicmd[6]) << 24;
461 sect |= ((u64)scsicmd[7]) << 16;
462 sect |= ((u64)scsicmd[8]) << 8;
463 sect |= ((u64)scsicmd[9]);
465 n_sect |= ((u32)scsicmd[10]) << 24;
466 n_sect |= ((u32)scsicmd[11]) << 16;
467 n_sect |= ((u32)scsicmd[12]) << 8;
468 n_sect |= ((u32)scsicmd[13]);
476 if (sect >= dev_sectors)
478 if ((sect + n_sect) > dev_sectors)
481 if (n_sect > (64 * 1024))
489 tf->hob_nsect = (n_sect >> 8) & 0xff;
491 tf->hob_lbah = (sect >> 40) & 0xff;
492 tf->hob_lbam = (sect >> 32) & 0xff;
493 tf->hob_lbal = (sect >> 24) & 0xff;
495 tf->device |= (sect >> 24) & 0xf;
497 tf->nsect = n_sect & 0xff;
499 tf->hob_lbah = (sect >> 16) & 0xff;
500 tf->hob_lbam = (sect >> 8) & 0xff;
501 tf->hob_lbal = sect & 0xff;
507 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
508 * @qc: Storage for translated ATA taskfile
509 * @scsicmd: SCSI command to translate
511 * Converts any of six SCSI read/write commands into the
512 * ATA counterpart, including starting sector (LBA),
513 * sector count, and taking into account the device's LBA48
516 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
517 * %WRITE_16 are currently supported.
520 * spin_lock_irqsave(host_set lock)
523 * Zero on success, non-zero on error.
526 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
528 struct ata_taskfile *tf = &qc->tf;
529 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
531 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
532 tf->protocol = qc->dev->xfer_protocol;
533 tf->device |= ATA_LBA;
535 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
536 scsicmd[0] == READ_16) {
537 tf->command = qc->dev->read_cmd;
539 tf->command = qc->dev->write_cmd;
540 tf->flags |= ATA_TFLAG_WRITE;
543 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
545 tf->hob_nsect = scsicmd[7];
546 tf->hob_lbal = scsicmd[2];
548 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
551 /* if we don't support LBA48 addressing, the request
552 * -may- be too large. */
553 if ((scsicmd[2] & 0xf0) || scsicmd[7])
556 /* stores LBA27:24 in lower 4 bits of device reg */
557 tf->device |= scsicmd[2];
559 qc->nsect = scsicmd[8];
562 tf->nsect = scsicmd[8];
563 tf->lbal = scsicmd[5];
564 tf->lbam = scsicmd[4];
565 tf->lbah = scsicmd[3];
567 VPRINTK("ten-byte command\n");
571 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
572 qc->nsect = tf->nsect = scsicmd[4];
573 tf->lbal = scsicmd[3];
574 tf->lbam = scsicmd[2];
575 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
577 VPRINTK("six-byte command\n");
581 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
582 /* rule out impossible LBAs and sector counts */
583 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
587 tf->hob_nsect = scsicmd[12];
588 tf->hob_lbal = scsicmd[6];
589 tf->hob_lbam = scsicmd[5];
590 tf->hob_lbah = scsicmd[4];
592 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
595 /* once again, filter out impossible non-zero values */
596 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
600 /* stores LBA27:24 in lower 4 bits of device reg */
601 tf->device |= scsicmd[2];
603 qc->nsect = scsicmd[13];
606 tf->nsect = scsicmd[13];
607 tf->lbal = scsicmd[9];
608 tf->lbam = scsicmd[8];
609 tf->lbah = scsicmd[7];
611 VPRINTK("sixteen-byte command\n");
615 DPRINTK("no-byte command\n");
619 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
621 struct scsi_cmnd *cmd = qc->scsicmd;
623 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
624 ata_to_sense_error(qc, drv_stat);
626 cmd->result = SAM_STAT_GOOD;
634 * ata_scsi_translate - Translate then issue SCSI command to ATA device
635 * @ap: ATA port to which the command is addressed
636 * @dev: ATA device to which the command is addressed
637 * @cmd: SCSI command to execute
638 * @done: SCSI command completion function
639 * @xlat_func: Actor which translates @cmd to an ATA taskfile
641 * Our ->queuecommand() function has decided that the SCSI
642 * command issued can be directly translated into an ATA
643 * command, rather than handled internally.
645 * This function sets up an ata_queued_cmd structure for the
646 * SCSI command, and sends that ata_queued_cmd to the hardware.
649 * spin_lock_irqsave(host_set lock)
652 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
653 struct scsi_cmnd *cmd,
654 void (*done)(struct scsi_cmnd *),
655 ata_xlat_func_t xlat_func)
657 struct ata_queued_cmd *qc;
658 u8 *scsicmd = cmd->cmnd;
662 qc = ata_scsi_qc_new(ap, dev, cmd, done);
666 /* data is present; dma-map it */
667 if (cmd->sc_data_direction == SCSI_DATA_READ ||
668 cmd->sc_data_direction == SCSI_DATA_WRITE) {
669 if (unlikely(cmd->request_bufflen < 1)) {
670 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
676 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
678 ata_sg_init_one(qc, cmd->request_buffer,
679 cmd->request_bufflen);
681 qc->pci_dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
684 qc->complete_fn = ata_scsi_qc_complete;
686 if (xlat_func(qc, scsicmd))
689 /* select device, send command to hardware */
690 if (ata_qc_issue(qc))
697 ata_bad_cdb(cmd, done);
698 DPRINTK("EXIT - badcmd\n");
702 * ata_scsi_rbuf_get - Map response buffer.
703 * @cmd: SCSI command containing buffer to be mapped.
704 * @buf_out: Pointer to mapped area.
706 * Maps buffer contained within SCSI command @cmd.
709 * spin_lock_irqsave(host_set lock)
712 * Length of response buffer.
715 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
721 struct scatterlist *sg;
723 sg = (struct scatterlist *) cmd->request_buffer;
724 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
727 buf = cmd->request_buffer;
728 buflen = cmd->request_bufflen;
736 * ata_scsi_rbuf_put - Unmap response buffer.
737 * @cmd: SCSI command containing buffer to be unmapped.
739 * Unmaps response buffer contained within @cmd.
742 * spin_lock_irqsave(host_set lock)
745 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
748 struct scatterlist *sg;
750 sg = (struct scatterlist *) cmd->request_buffer;
751 kunmap_atomic(buf - sg->offset, KM_USER0);
756 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
757 * @args: device IDENTIFY data / SCSI command of interest.
758 * @actor: Callback hook for desired SCSI command simulator
760 * Takes care of the hard work of simulating a SCSI command...
761 * Mapping the response buffer, calling the command's handler,
762 * and handling the handler's return value. This return value
763 * indicates whether the handler wishes the SCSI command to be
764 * completed successfully, or not.
767 * spin_lock_irqsave(host_set lock)
770 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
771 unsigned int (*actor) (struct ata_scsi_args *args,
772 u8 *rbuf, unsigned int buflen))
775 unsigned int buflen, rc;
776 struct scsi_cmnd *cmd = args->cmd;
778 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
779 memset(rbuf, 0, buflen);
780 rc = actor(args, rbuf, buflen);
781 ata_scsi_rbuf_put(cmd, rbuf);
784 ata_bad_cdb(cmd, args->done);
786 cmd->result = SAM_STAT_GOOD;
792 * ata_scsiop_inq_std - Simulate INQUIRY command
793 * @args: device IDENTIFY data / SCSI command of interest.
794 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
795 * @buflen: Response buffer length.
797 * Returns standard device identification data associated
798 * with non-EVPD INQUIRY command output.
801 * spin_lock_irqsave(host_set lock)
804 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
810 0x5, /* claim SPC-3 version compatibility */
815 /* set scsi removeable (RMB) bit per ata bit */
816 if (ata_id_removeable(args->id))
821 memcpy(rbuf, hdr, sizeof(hdr));
824 memcpy(&rbuf[8], "ATA ", 8);
825 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
826 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
827 if (rbuf[32] == 0 || rbuf[32] == ' ')
828 memcpy(&rbuf[32], "n/a ", 4);
832 const u8 versions[] = {
833 0x60, /* SAM-3 (no version claimed) */
836 0x20, /* SBC-2 (no version claimed) */
839 0x60 /* SPC-3 (no version claimed) */
842 memcpy(rbuf + 59, versions, sizeof(versions));
849 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
850 * @args: device IDENTIFY data / SCSI command of interest.
851 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
852 * @buflen: Response buffer length.
854 * Returns list of inquiry EVPD pages available.
857 * spin_lock_irqsave(host_set lock)
860 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
864 0x00, /* page 0x00, this page */
865 0x80, /* page 0x80, unit serial no page */
866 0x83 /* page 0x83, device ident page */
868 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
871 memcpy(rbuf + 4, pages, sizeof(pages));
877 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
878 * @args: device IDENTIFY data / SCSI command of interest.
879 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
880 * @buflen: Response buffer length.
882 * Returns ATA device serial number.
885 * spin_lock_irqsave(host_set lock)
888 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
893 0x80, /* this page code */
895 ATA_SERNO_LEN, /* page len */
897 memcpy(rbuf, hdr, sizeof(hdr));
899 if (buflen > (ATA_SERNO_LEN + 4 - 1))
900 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
901 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
906 static const char *inq_83_str = "Linux ATA-SCSI simulator";
909 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
910 * @args: device IDENTIFY data / SCSI command of interest.
911 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
912 * @buflen: Response buffer length.
914 * Returns device identification. Currently hardcoded to
915 * return "Linux ATA-SCSI simulator".
918 * spin_lock_irqsave(host_set lock)
921 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
924 rbuf[1] = 0x83; /* this page code */
925 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
927 /* our one and only identification descriptor (vendor-specific) */
928 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
929 rbuf[4 + 0] = 2; /* code set: ASCII */
930 rbuf[4 + 3] = strlen(inq_83_str);
931 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
939 * @args: device IDENTIFY data / SCSI command of interest.
940 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
941 * @buflen: Response buffer length.
943 * No operation. Simply returns success to caller, to indicate
944 * that the caller should successfully complete this SCSI command.
947 * spin_lock_irqsave(host_set lock)
950 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
958 * ata_msense_push - Push data onto MODE SENSE data output buffer
959 * @ptr_io: (input/output) Location to store more output data
960 * @last: End of output data buffer
961 * @buf: Pointer to BLOB being added to output buffer
962 * @buflen: Length of BLOB
964 * Store MODE SENSE data on an output buffer.
970 static void ata_msense_push(u8 **ptr_io, const u8 *last,
971 const u8 *buf, unsigned int buflen)
975 if ((ptr + buflen - 1) > last)
978 memcpy(ptr, buf, buflen);
986 * ata_msense_caching - Simulate MODE SENSE caching info page
987 * @id: device IDENTIFY data
988 * @ptr_io: (input/output) Location to store more output data
989 * @last: End of output data buffer
991 * Generate a caching info page, which conditionally indicates
992 * write caching to the SCSI layer, depending on device
999 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1003 0x8, /* page code */
1004 0x12, /* page length */
1005 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1006 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1009 if (ata_id_wcache_enabled(id))
1010 page[2] |= (1 << 2); /* write cache enable */
1011 if (!ata_id_rahead_enabled(id))
1012 page[12] |= (1 << 5); /* disable read ahead */
1014 ata_msense_push(ptr_io, last, page, sizeof(page));
1015 return sizeof(page);
1019 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1020 * @dev: Device associated with this MODE SENSE command
1021 * @ptr_io: (input/output) Location to store more output data
1022 * @last: End of output data buffer
1024 * Generate a generic MODE SENSE control mode page.
1030 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1032 const u8 page[] = {0xa, 0xa, 2, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1034 ata_msense_push(ptr_io, last, page, sizeof(page));
1035 return sizeof(page);
1039 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1040 * @dev: Device associated with this MODE SENSE command
1041 * @ptr_io: (input/output) Location to store more output data
1042 * @last: End of output data buffer
1044 * Generate a generic MODE SENSE r/w error recovery page.
1050 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1053 0x1, /* page code */
1054 0xa, /* page length */
1055 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1056 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1059 ata_msense_push(ptr_io, last, page, sizeof(page));
1060 return sizeof(page);
1064 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1065 * @args: device IDENTIFY data / SCSI command of interest.
1066 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1067 * @buflen: Response buffer length.
1069 * Simulate MODE SENSE commands.
1072 * spin_lock_irqsave(host_set lock)
1075 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1076 unsigned int buflen)
1078 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1079 unsigned int page_control, six_byte, output_len;
1083 six_byte = (scsicmd[0] == MODE_SENSE);
1085 /* we only support saved and current values (which we treat
1086 * in the same manner)
1088 page_control = scsicmd[2] >> 6;
1089 if ((page_control != 0) && (page_control != 3))
1097 p = rbuf + output_len;
1098 last = rbuf + buflen - 1;
1100 switch(scsicmd[2] & 0x3f) {
1101 case 0x01: /* r/w error recovery */
1102 output_len += ata_msense_rw_recovery(&p, last);
1105 case 0x08: /* caching */
1106 output_len += ata_msense_caching(args->id, &p, last);
1109 case 0x0a: { /* control mode */
1110 output_len += ata_msense_ctl_mode(&p, last);
1114 case 0x3f: /* all pages */
1115 output_len += ata_msense_rw_recovery(&p, last);
1116 output_len += ata_msense_caching(args->id, &p, last);
1117 output_len += ata_msense_ctl_mode(&p, last);
1120 default: /* invalid page code */
1126 rbuf[0] = output_len;
1129 rbuf[0] = output_len >> 8;
1130 rbuf[1] = output_len;
1137 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1138 * @args: device IDENTIFY data / SCSI command of interest.
1139 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1140 * @buflen: Response buffer length.
1142 * Simulate READ CAPACITY commands.
1145 * spin_lock_irqsave(host_set lock)
1148 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1149 unsigned int buflen)
1156 if (ata_id_has_lba48(args->id))
1157 n_sectors = ata_id_u64(args->id, 100);
1159 n_sectors = ata_id_u32(args->id, 60);
1160 n_sectors--; /* ATA TotalUserSectors - 1 */
1162 tmp = n_sectors; /* note: truncates, if lba48 */
1163 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1164 /* sector count, 32-bit */
1165 rbuf[0] = tmp >> (8 * 3);
1166 rbuf[1] = tmp >> (8 * 2);
1167 rbuf[2] = tmp >> (8 * 1);
1171 tmp = ATA_SECT_SIZE;
1176 /* sector count, 64-bit */
1177 rbuf[2] = n_sectors >> (8 * 7);
1178 rbuf[3] = n_sectors >> (8 * 6);
1179 rbuf[4] = n_sectors >> (8 * 5);
1180 rbuf[5] = n_sectors >> (8 * 4);
1181 rbuf[6] = tmp >> (8 * 3);
1182 rbuf[7] = tmp >> (8 * 2);
1183 rbuf[8] = tmp >> (8 * 1);
1187 tmp = ATA_SECT_SIZE;
1188 rbuf[12] = tmp >> 8;
1196 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1197 * @args: device IDENTIFY data / SCSI command of interest.
1198 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1199 * @buflen: Response buffer length.
1201 * Simulate REPORT LUNS command.
1204 * spin_lock_irqsave(host_set lock)
1207 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1208 unsigned int buflen)
1211 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1217 * ata_scsi_badcmd - End a SCSI request with an error
1218 * @cmd: SCSI request to be handled
1219 * @done: SCSI command completion function
1220 * @asc: SCSI-defined additional sense code
1221 * @ascq: SCSI-defined additional sense code qualifier
1223 * Helper function that completes a SCSI command with
1224 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1225 * and the specified additional sense codes.
1228 * spin_lock_irqsave(host_set lock)
1231 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1234 cmd->result = SAM_STAT_CHECK_CONDITION;
1236 cmd->sense_buffer[0] = 0x70;
1237 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1238 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1239 cmd->sense_buffer[12] = asc;
1240 cmd->sense_buffer[13] = ascq;
1245 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1247 struct scsi_cmnd *cmd = qc->scsicmd;
1249 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1250 DPRINTK("request check condition\n");
1252 cmd->result = SAM_STAT_CHECK_CONDITION;
1258 u8 *scsicmd = cmd->cmnd;
1260 if (scsicmd[0] == INQUIRY) {
1262 unsigned int buflen;
1264 buflen = ata_scsi_rbuf_get(cmd, &buf);
1266 buf[3] = (buf[3] & 0xf0) | 2;
1267 ata_scsi_rbuf_put(cmd, buf);
1269 cmd->result = SAM_STAT_GOOD;
1277 * atapi_xlat - Initialize PACKET taskfile
1278 * @qc: command structure to be initialized
1279 * @scsicmd: SCSI CDB associated with this PACKET command
1282 * spin_lock_irqsave(host_set lock)
1285 * Zero on success, non-zero on failure.
1288 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1290 struct scsi_cmnd *cmd = qc->scsicmd;
1291 struct ata_device *dev = qc->dev;
1292 int using_pio = (dev->flags & ATA_DFLAG_PIO);
1293 int nodata = (cmd->sc_data_direction == SCSI_DATA_NONE);
1295 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1297 qc->complete_fn = atapi_qc_complete;
1299 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1300 if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
1301 qc->tf.flags |= ATA_TFLAG_WRITE;
1302 DPRINTK("direction: write\n");
1305 qc->tf.command = ATA_CMD_PACKET;
1307 /* no data, or PIO data xfer */
1308 if (using_pio || nodata) {
1310 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1312 qc->tf.protocol = ATA_PROT_ATAPI;
1313 qc->tf.lbam = (8 * 1024) & 0xff;
1314 qc->tf.lbah = (8 * 1024) >> 8;
1319 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1320 qc->tf.feature |= ATAPI_PKT_DMA;
1322 #ifdef ATAPI_ENABLE_DMADIR
1323 /* some SATA bridges need us to indicate data xfer direction */
1324 if (cmd->sc_data_direction != SCSI_DATA_WRITE)
1325 qc->tf.feature |= ATAPI_DMADIR;
1329 qc->nbytes = cmd->bufflen;
1335 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1336 * @ap: ATA port to which the device is attached
1337 * @cmd: SCSI command to be sent to the device
1339 * Given various information provided in struct scsi_cmnd,
1340 * map that onto an ATA bus, and using that mapping
1341 * determine which ata_device is associated with the
1342 * SCSI command to be sent.
1345 * spin_lock_irqsave(host_set lock)
1348 * Associated ATA device, or %NULL if not found.
1351 static struct ata_device *
1352 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1354 struct ata_device *dev;
1356 /* skip commands not addressed to targets we simulate */
1357 if (likely(scsidev->id < ATA_MAX_DEVICES))
1358 dev = &ap->device[scsidev->id];
1362 if (unlikely((scsidev->channel != 0) ||
1363 (scsidev->lun != 0)))
1366 if (unlikely(!ata_dev_present(dev)))
1369 #ifndef ATA_ENABLE_ATAPI
1370 if (unlikely(dev->class == ATA_DEV_ATAPI))
1378 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1380 * @cmd: SCSI command opcode to consider
1382 * Look up the SCSI command given, and determine whether the
1383 * SCSI command is to be translated or simulated.
1386 * Pointer to translation function if possible, %NULL if not.
1389 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1399 return ata_scsi_rw_xlat;
1401 case SYNCHRONIZE_CACHE:
1402 if (ata_try_flush_cache(dev))
1403 return ata_scsi_flush_xlat;
1408 return ata_scsi_verify_xlat;
1415 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1416 * @ap: ATA port to which the command was being sent
1417 * @cmd: SCSI command to dump
1419 * Prints the contents of a SCSI command via printk().
1422 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1423 struct scsi_cmnd *cmd)
1426 struct scsi_device *scsidev = cmd->device;
1427 u8 *scsicmd = cmd->cmnd;
1429 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1431 scsidev->channel, scsidev->id, scsidev->lun,
1432 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1433 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1439 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1440 * @cmd: SCSI command to be sent
1441 * @done: Completion function, called when command is complete
1443 * In some cases, this function translates SCSI commands into
1444 * ATA taskfiles, and queues the taskfiles to be sent to
1445 * hardware. In other cases, this function simulates a
1446 * SCSI device by evaluating and responding to certain
1447 * SCSI commands. This creates the overall effect of
1448 * ATA and ATAPI devices appearing as SCSI devices.
1451 * Releases scsi-layer-held lock, and obtains host_set lock.
1457 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1459 struct ata_port *ap;
1460 struct ata_device *dev;
1461 struct scsi_device *scsidev = cmd->device;
1463 ap = (struct ata_port *) &scsidev->host->hostdata[0];
1465 ata_scsi_dump_cdb(ap, cmd);
1467 dev = ata_scsi_find_dev(ap, scsidev);
1468 if (unlikely(!dev)) {
1469 cmd->result = (DID_BAD_TARGET << 16);
1474 if (dev->class == ATA_DEV_ATA) {
1475 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1479 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1481 ata_scsi_simulate(dev->id, cmd, done);
1483 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1490 * ata_scsi_simulate - simulate SCSI command on ATA device
1491 * @id: current IDENTIFY data for target device.
1492 * @cmd: SCSI command being sent to device.
1493 * @done: SCSI command completion function.
1495 * Interprets and directly executes a select list of SCSI commands
1496 * that can be handled internally.
1499 * spin_lock_irqsave(host_set lock)
1502 void ata_scsi_simulate(u16 *id,
1503 struct scsi_cmnd *cmd,
1504 void (*done)(struct scsi_cmnd *))
1506 struct ata_scsi_args args;
1507 u8 *scsicmd = cmd->cmnd;
1513 switch(scsicmd[0]) {
1514 /* no-op's, complete with success */
1515 case SYNCHRONIZE_CACHE:
1519 case TEST_UNIT_READY:
1520 case FORMAT_UNIT: /* FIXME: correct? */
1521 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1522 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1526 if (scsicmd[1] & 2) /* is CmdDt set? */
1527 ata_bad_cdb(cmd, done);
1528 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1529 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1530 else if (scsicmd[2] == 0x00)
1531 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1532 else if (scsicmd[2] == 0x80)
1533 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1534 else if (scsicmd[2] == 0x83)
1535 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1537 ata_bad_cdb(cmd, done);
1542 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1545 case MODE_SELECT: /* unconditionally return */
1546 case MODE_SELECT_10: /* bad-field-in-cdb */
1547 ata_bad_cdb(cmd, done);
1551 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1554 case SERVICE_ACTION_IN:
1555 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1556 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1558 ata_bad_cdb(cmd, done);
1562 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1565 /* mandantory commands we haven't implemented yet */
1568 /* all other commands */
1570 ata_bad_scsiop(cmd, done);