Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / libata-scsi.c
blob4e5e54a1564b571edd07e6ac5d2ccfa100696b82
1 /*
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
10 by reference.
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>
29 #include "scsi.h"
30 #include <scsi/scsi_host.h>
31 #include <linux/libata.h>
32 #include <asm/uaccess.h>
34 #include "libata.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);
41 /**
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.
53 * LOCKING:
54 * Defined by the SCSI layer. We don't really care.
56 * RETURNS:
57 * Zero.
59 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
60 sector_t capacity, int geom[])
62 geom[0] = 255;
63 geom[1] = 63;
64 sector_div(capacity, 255*63);
65 geom[2] = capacity;
67 return 0;
70 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
72 struct ata_port *ap;
73 struct ata_device *dev;
74 int val = -EINVAL, rc = -EINVAL;
76 ap = (struct ata_port *) &scsidev->host->hostdata[0];
77 if (!ap)
78 goto out;
80 dev = ata_scsi_find_dev(ap, scsidev);
81 if (!dev) {
82 rc = -ENODEV;
83 goto out;
86 switch (cmd) {
87 case ATA_IOC_GET_IO32:
88 val = 0;
89 if (copy_to_user(arg, &val, 1))
90 return -EFAULT;
91 return 0;
93 case ATA_IOC_SET_IO32:
94 val = (unsigned long) arg;
95 if (val != 0)
96 return -EINVAL;
97 return 0;
99 default:
100 rc = -ENOTTY;
101 break;
104 out:
105 return rc;
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
121 * current command.
123 * LOCKING:
124 * spin_lock_irqsave(host_set lock)
126 * RETURNS:
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);
137 if (qc) {
138 qc->scsicmd = cmd;
139 qc->scsidone = done;
141 if (cmd->use_sg) {
142 qc->sg = (struct scatterlist *) cmd->request_buffer;
143 qc->n_elem = cmd->use_sg;
144 } else {
145 qc->sg = &qc->sgent;
146 qc->n_elem = 1;
148 } else {
149 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
150 done(cmd);
153 return qc;
157 * ata_to_sense_error - convert ATA error to SCSI error
158 * @qc: Command that we are erroring out
159 * @drv_stat: value contained in ATA status register
161 * Converts an ATA error into a SCSI error. While we are at it
162 * we decode and dump the ATA error for the user so that they
163 * have some idea what really happened at the non make-believe
164 * layer.
166 * LOCKING:
167 * spin_lock_irqsave(host_set lock)
170 void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
172 struct scsi_cmnd *cmd = qc->scsicmd;
173 u8 err = 0;
174 unsigned char *sb = cmd->sense_buffer;
175 /* Based on the 3ware driver translation table */
176 static unsigned char sense_table[][4] = {
177 /* BBD|ECC|ID|MAR */
178 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
179 /* BBD|ECC|ID */
180 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
181 /* ECC|MC|MARK */
182 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
183 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
184 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
185 /* MC|ID|ABRT|TRK0|MARK */
186 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
187 /* MCR|MARK */
188 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
189 /* Bad address mark */
190 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
191 /* TRK0 */
192 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
193 /* Abort & !ICRC */
194 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
195 /* Media change request */
196 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
197 /* SRV */
198 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
199 /* Media change */
200 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
201 /* ECC */
202 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
203 /* BBD - block marked bad */
204 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
205 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
207 static unsigned char stat_table[][4] = {
208 /* Must be first because BUSY means no other bits valid */
209 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
210 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
211 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
212 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
213 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
215 int i = 0;
217 cmd->result = SAM_STAT_CHECK_CONDITION;
220 * Is this an error we can process/parse
223 if(drv_stat & ATA_ERR)
224 /* Read the err bits */
225 err = ata_chk_err(qc->ap);
227 /* Display the ATA level error info */
229 printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
230 if(drv_stat & 0x80)
232 printk("Busy ");
233 err = 0; /* Data is not valid in this case */
235 else {
236 if(drv_stat & 0x40) printk("DriveReady ");
237 if(drv_stat & 0x20) printk("DeviceFault ");
238 if(drv_stat & 0x10) printk("SeekComplete ");
239 if(drv_stat & 0x08) printk("DataRequest ");
240 if(drv_stat & 0x04) printk("CorrectedError ");
241 if(drv_stat & 0x02) printk("Index ");
242 if(drv_stat & 0x01) printk("Error ");
244 printk("}\n");
246 if(err)
248 printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err);
249 if(err & 0x04) printk("DriveStatusError ");
250 if(err & 0x80)
252 if(err & 0x04)
253 printk("BadCRC ");
254 else
255 printk("Sector ");
257 if(err & 0x40) printk("UncorrectableError ");
258 if(err & 0x10) printk("SectorIdNotFound ");
259 if(err & 0x02) printk("TrackZeroNotFound ");
260 if(err & 0x01) printk("AddrMarkNotFound ");
261 printk("}\n");
263 /* Should we dump sector info here too ?? */
267 /* Look for err */
268 while(sense_table[i][0] != 0xFF)
270 /* Look for best matches first */
271 if((sense_table[i][0] & err) == sense_table[i][0])
273 sb[0] = 0x70;
274 sb[2] = sense_table[i][1];
275 sb[7] = 0x0a;
276 sb[12] = sense_table[i][2];
277 sb[13] = sense_table[i][3];
278 return;
280 i++;
282 /* No immediate match */
283 if(err)
284 printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
286 i = 0;
287 /* Fall back to interpreting status bits */
288 while(stat_table[i][0] != 0xFF)
290 if(stat_table[i][0] & drv_stat)
292 sb[0] = 0x70;
293 sb[2] = stat_table[i][1];
294 sb[7] = 0x0a;
295 sb[12] = stat_table[i][2];
296 sb[13] = stat_table[i][3];
297 return;
299 i++;
301 /* No error ?? */
302 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
303 /* additional-sense-code[-qualifier] */
305 sb[0] = 0x70;
306 sb[2] = MEDIUM_ERROR;
307 sb[7] = 0x0A;
308 if (cmd->sc_data_direction == SCSI_DATA_READ) {
309 sb[12] = 0x11; /* "unrecovered read error" */
310 sb[13] = 0x04;
311 } else {
312 sb[12] = 0x0C; /* "write error - */
313 sb[13] = 0x02; /* auto-reallocation failed" */
318 * ata_scsi_slave_config - Set SCSI device attributes
319 * @sdev: SCSI device to examine
321 * This is called before we actually start reading
322 * and writing to the device, to configure certain
323 * SCSI mid-layer behaviors.
325 * LOCKING:
326 * Defined by SCSI layer. We don't really care.
329 int ata_scsi_slave_config(struct scsi_device *sdev)
331 sdev->use_10_for_rw = 1;
332 sdev->use_10_for_ms = 1;
334 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
336 if (sdev->id < ATA_MAX_DEVICES) {
337 struct ata_port *ap;
338 struct ata_device *dev;
340 ap = (struct ata_port *) &sdev->host->hostdata[0];
341 dev = &ap->device[sdev->id];
343 /* TODO: 1024 is an arbitrary number, not the
344 * hardware maximum. This should be increased to
345 * 65534 when Jens Axboe's patch for dynamically
346 * determining max_sectors is merged.
348 if ((dev->flags & ATA_DFLAG_LBA48) &&
349 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
350 sdev->host->max_sectors = 2048;
351 blk_queue_max_sectors(sdev->request_queue, 2048);
355 return 0; /* scsi layer doesn't check return value, sigh */
359 * ata_scsi_error - SCSI layer error handler callback
360 * @host: SCSI host on which error occurred
362 * Handles SCSI-layer-thrown error events.
364 * LOCKING:
365 * Inherited from SCSI layer (none, can sleep)
367 * RETURNS:
368 * Zero.
371 int ata_scsi_error(struct Scsi_Host *host)
373 struct ata_port *ap;
375 DPRINTK("ENTER\n");
377 ap = (struct ata_port *) &host->hostdata[0];
378 ap->ops->eng_timeout(ap);
380 /* TODO: this is per-command; when queueing is supported
381 * this code will either change or move to a more
382 * appropriate place
384 host->host_failed--;
386 DPRINTK("EXIT\n");
387 return 0;
391 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
392 * @qc: Storage for translated ATA taskfile
393 * @scsicmd: SCSI command to translate (ignored)
395 * Sets up an ATA taskfile to issue FLUSH CACHE or
396 * FLUSH CACHE EXT.
398 * LOCKING:
399 * spin_lock_irqsave(host_set lock)
401 * RETURNS:
402 * Zero on success, non-zero on error.
405 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
407 struct ata_taskfile *tf = &qc->tf;
409 tf->flags |= ATA_TFLAG_DEVICE;
410 tf->protocol = ATA_PROT_NODATA;
412 if ((tf->flags & ATA_TFLAG_LBA48) &&
413 (ata_id_has_flush_ext(qc->dev->id)))
414 tf->command = ATA_CMD_FLUSH_EXT;
415 else
416 tf->command = ATA_CMD_FLUSH;
418 return 0;
422 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
423 * @qc: Storage for translated ATA taskfile
424 * @scsicmd: SCSI command to translate
426 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
428 * LOCKING:
429 * spin_lock_irqsave(host_set lock)
431 * RETURNS:
432 * Zero on success, non-zero on error.
435 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
437 struct ata_taskfile *tf = &qc->tf;
438 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
439 u64 dev_sectors = qc->dev->n_sectors;
440 u64 sect = 0;
441 u32 n_sect = 0;
443 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
444 tf->protocol = ATA_PROT_NODATA;
445 tf->device |= ATA_LBA;
447 if (scsicmd[0] == VERIFY) {
448 sect |= ((u64)scsicmd[2]) << 24;
449 sect |= ((u64)scsicmd[3]) << 16;
450 sect |= ((u64)scsicmd[4]) << 8;
451 sect |= ((u64)scsicmd[5]);
453 n_sect |= ((u32)scsicmd[7]) << 8;
454 n_sect |= ((u32)scsicmd[8]);
457 else if (scsicmd[0] == VERIFY_16) {
458 sect |= ((u64)scsicmd[2]) << 56;
459 sect |= ((u64)scsicmd[3]) << 48;
460 sect |= ((u64)scsicmd[4]) << 40;
461 sect |= ((u64)scsicmd[5]) << 32;
462 sect |= ((u64)scsicmd[6]) << 24;
463 sect |= ((u64)scsicmd[7]) << 16;
464 sect |= ((u64)scsicmd[8]) << 8;
465 sect |= ((u64)scsicmd[9]);
467 n_sect |= ((u32)scsicmd[10]) << 24;
468 n_sect |= ((u32)scsicmd[11]) << 16;
469 n_sect |= ((u32)scsicmd[12]) << 8;
470 n_sect |= ((u32)scsicmd[13]);
473 else
474 return 1;
476 if (!n_sect)
477 return 1;
478 if (sect >= dev_sectors)
479 return 1;
480 if ((sect + n_sect) > dev_sectors)
481 return 1;
482 if (lba48) {
483 if (n_sect > (64 * 1024))
484 return 1;
485 } else {
486 if (n_sect > 256)
487 return 1;
490 if (lba48) {
491 tf->command = ATA_CMD_VERIFY_EXT;
493 tf->hob_nsect = (n_sect >> 8) & 0xff;
495 tf->hob_lbah = (sect >> 40) & 0xff;
496 tf->hob_lbam = (sect >> 32) & 0xff;
497 tf->hob_lbal = (sect >> 24) & 0xff;
498 } else {
499 tf->command = ATA_CMD_VERIFY;
501 tf->device |= (sect >> 24) & 0xf;
504 tf->nsect = n_sect & 0xff;
506 tf->lbah = (sect >> 16) & 0xff;
507 tf->lbam = (sect >> 8) & 0xff;
508 tf->lbal = sect & 0xff;
510 return 0;
514 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
515 * @qc: Storage for translated ATA taskfile
516 * @scsicmd: SCSI command to translate
518 * Converts any of six SCSI read/write commands into the
519 * ATA counterpart, including starting sector (LBA),
520 * sector count, and taking into account the device's LBA48
521 * support.
523 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
524 * %WRITE_16 are currently supported.
526 * LOCKING:
527 * spin_lock_irqsave(host_set lock)
529 * RETURNS:
530 * Zero on success, non-zero on error.
533 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
535 struct ata_taskfile *tf = &qc->tf;
536 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
538 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
539 tf->protocol = qc->dev->xfer_protocol;
540 tf->device |= ATA_LBA;
542 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
543 scsicmd[0] == READ_16) {
544 tf->command = qc->dev->read_cmd;
545 } else {
546 tf->command = qc->dev->write_cmd;
547 tf->flags |= ATA_TFLAG_WRITE;
550 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
551 if (lba48) {
552 tf->hob_nsect = scsicmd[7];
553 tf->hob_lbal = scsicmd[2];
555 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
556 scsicmd[8];
557 } else {
558 /* if we don't support LBA48 addressing, the request
559 * -may- be too large. */
560 if ((scsicmd[2] & 0xf0) || scsicmd[7])
561 return 1;
563 /* stores LBA27:24 in lower 4 bits of device reg */
564 tf->device |= scsicmd[2];
566 qc->nsect = scsicmd[8];
569 tf->nsect = scsicmd[8];
570 tf->lbal = scsicmd[5];
571 tf->lbam = scsicmd[4];
572 tf->lbah = scsicmd[3];
574 VPRINTK("ten-byte command\n");
575 return 0;
578 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
579 qc->nsect = tf->nsect = scsicmd[4];
580 tf->lbal = scsicmd[3];
581 tf->lbam = scsicmd[2];
582 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
584 VPRINTK("six-byte command\n");
585 return 0;
588 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
589 /* rule out impossible LBAs and sector counts */
590 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
591 return 1;
593 if (lba48) {
594 tf->hob_nsect = scsicmd[12];
595 tf->hob_lbal = scsicmd[6];
596 tf->hob_lbam = scsicmd[5];
597 tf->hob_lbah = scsicmd[4];
599 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
600 scsicmd[13];
601 } else {
602 /* once again, filter out impossible non-zero values */
603 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
604 (scsicmd[6] & 0xf0))
605 return 1;
607 /* stores LBA27:24 in lower 4 bits of device reg */
608 tf->device |= scsicmd[6];
610 qc->nsect = scsicmd[13];
613 tf->nsect = scsicmd[13];
614 tf->lbal = scsicmd[9];
615 tf->lbam = scsicmd[8];
616 tf->lbah = scsicmd[7];
618 VPRINTK("sixteen-byte command\n");
619 return 0;
622 DPRINTK("no-byte command\n");
623 return 1;
626 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
628 struct scsi_cmnd *cmd = qc->scsicmd;
630 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
631 ata_to_sense_error(qc, drv_stat);
632 else
633 cmd->result = SAM_STAT_GOOD;
635 qc->scsidone(cmd);
637 return 0;
641 * ata_scsi_translate - Translate then issue SCSI command to ATA device
642 * @ap: ATA port to which the command is addressed
643 * @dev: ATA device to which the command is addressed
644 * @cmd: SCSI command to execute
645 * @done: SCSI command completion function
646 * @xlat_func: Actor which translates @cmd to an ATA taskfile
648 * Our ->queuecommand() function has decided that the SCSI
649 * command issued can be directly translated into an ATA
650 * command, rather than handled internally.
652 * This function sets up an ata_queued_cmd structure for the
653 * SCSI command, and sends that ata_queued_cmd to the hardware.
655 * LOCKING:
656 * spin_lock_irqsave(host_set lock)
659 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
660 struct scsi_cmnd *cmd,
661 void (*done)(struct scsi_cmnd *),
662 ata_xlat_func_t xlat_func)
664 struct ata_queued_cmd *qc;
665 u8 *scsicmd = cmd->cmnd;
667 VPRINTK("ENTER\n");
669 qc = ata_scsi_qc_new(ap, dev, cmd, done);
670 if (!qc)
671 return;
673 /* data is present; dma-map it */
674 if (cmd->sc_data_direction == SCSI_DATA_READ ||
675 cmd->sc_data_direction == SCSI_DATA_WRITE) {
676 if (unlikely(cmd->request_bufflen < 1)) {
677 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
678 ap->id, dev->devno);
679 goto err_out;
682 if (cmd->use_sg)
683 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
684 else
685 ata_sg_init_one(qc, cmd->request_buffer,
686 cmd->request_bufflen);
688 qc->dma_dir = cmd->sc_data_direction;
691 qc->complete_fn = ata_scsi_qc_complete;
693 if (xlat_func(qc, scsicmd))
694 goto err_out;
696 /* select device, send command to hardware */
697 if (ata_qc_issue(qc))
698 goto err_out;
700 VPRINTK("EXIT\n");
701 return;
703 err_out:
704 ata_qc_free(qc);
705 ata_bad_cdb(cmd, done);
706 DPRINTK("EXIT - badcmd\n");
710 * ata_scsi_rbuf_get - Map response buffer.
711 * @cmd: SCSI command containing buffer to be mapped.
712 * @buf_out: Pointer to mapped area.
714 * Maps buffer contained within SCSI command @cmd.
716 * LOCKING:
717 * spin_lock_irqsave(host_set lock)
719 * RETURNS:
720 * Length of response buffer.
723 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
725 u8 *buf;
726 unsigned int buflen;
728 if (cmd->use_sg) {
729 struct scatterlist *sg;
731 sg = (struct scatterlist *) cmd->request_buffer;
732 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
733 buflen = sg->length;
734 } else {
735 buf = cmd->request_buffer;
736 buflen = cmd->request_bufflen;
739 *buf_out = buf;
740 return buflen;
744 * ata_scsi_rbuf_put - Unmap response buffer.
745 * @cmd: SCSI command containing buffer to be unmapped.
746 * @buf: buffer to unmap
748 * Unmaps response buffer contained within @cmd.
750 * LOCKING:
751 * spin_lock_irqsave(host_set lock)
754 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
756 if (cmd->use_sg) {
757 struct scatterlist *sg;
759 sg = (struct scatterlist *) cmd->request_buffer;
760 kunmap_atomic(buf - sg->offset, KM_USER0);
765 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
766 * @args: device IDENTIFY data / SCSI command of interest.
767 * @actor: Callback hook for desired SCSI command simulator
769 * Takes care of the hard work of simulating a SCSI command...
770 * Mapping the response buffer, calling the command's handler,
771 * and handling the handler's return value. This return value
772 * indicates whether the handler wishes the SCSI command to be
773 * completed successfully, or not.
775 * LOCKING:
776 * spin_lock_irqsave(host_set lock)
779 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
780 unsigned int (*actor) (struct ata_scsi_args *args,
781 u8 *rbuf, unsigned int buflen))
783 u8 *rbuf;
784 unsigned int buflen, rc;
785 struct scsi_cmnd *cmd = args->cmd;
787 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
788 memset(rbuf, 0, buflen);
789 rc = actor(args, rbuf, buflen);
790 ata_scsi_rbuf_put(cmd, rbuf);
792 if (rc)
793 ata_bad_cdb(cmd, args->done);
794 else {
795 cmd->result = SAM_STAT_GOOD;
796 args->done(cmd);
801 * ata_scsiop_inq_std - Simulate INQUIRY command
802 * @args: device IDENTIFY data / SCSI command of interest.
803 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
804 * @buflen: Response buffer length.
806 * Returns standard device identification data associated
807 * with non-EVPD INQUIRY command output.
809 * LOCKING:
810 * spin_lock_irqsave(host_set lock)
813 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
814 unsigned int buflen)
816 u8 hdr[] = {
817 TYPE_DISK,
819 0x5, /* claim SPC-3 version compatibility */
821 95 - 4
824 /* set scsi removeable (RMB) bit per ata bit */
825 if (ata_id_removeable(args->id))
826 hdr[1] |= (1 << 7);
828 VPRINTK("ENTER\n");
830 memcpy(rbuf, hdr, sizeof(hdr));
832 if (buflen > 35) {
833 memcpy(&rbuf[8], "ATA ", 8);
834 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
835 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
836 if (rbuf[32] == 0 || rbuf[32] == ' ')
837 memcpy(&rbuf[32], "n/a ", 4);
840 if (buflen > 63) {
841 const u8 versions[] = {
842 0x60, /* SAM-3 (no version claimed) */
844 0x03,
845 0x20, /* SBC-2 (no version claimed) */
847 0x02,
848 0x60 /* SPC-3 (no version claimed) */
851 memcpy(rbuf + 59, versions, sizeof(versions));
854 return 0;
858 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
859 * @args: device IDENTIFY data / SCSI command of interest.
860 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
861 * @buflen: Response buffer length.
863 * Returns list of inquiry EVPD pages available.
865 * LOCKING:
866 * spin_lock_irqsave(host_set lock)
869 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
870 unsigned int buflen)
872 const u8 pages[] = {
873 0x00, /* page 0x00, this page */
874 0x80, /* page 0x80, unit serial no page */
875 0x83 /* page 0x83, device ident page */
877 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
879 if (buflen > 6)
880 memcpy(rbuf + 4, pages, sizeof(pages));
882 return 0;
886 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
887 * @args: device IDENTIFY data / SCSI command of interest.
888 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
889 * @buflen: Response buffer length.
891 * Returns ATA device serial number.
893 * LOCKING:
894 * spin_lock_irqsave(host_set lock)
897 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
898 unsigned int buflen)
900 const u8 hdr[] = {
902 0x80, /* this page code */
904 ATA_SERNO_LEN, /* page len */
906 memcpy(rbuf, hdr, sizeof(hdr));
908 if (buflen > (ATA_SERNO_LEN + 4 - 1))
909 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
910 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
912 return 0;
915 static const char *inq_83_str = "Linux ATA-SCSI simulator";
918 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
919 * @args: device IDENTIFY data / SCSI command of interest.
920 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
921 * @buflen: Response buffer length.
923 * Returns device identification. Currently hardcoded to
924 * return "Linux ATA-SCSI simulator".
926 * LOCKING:
927 * spin_lock_irqsave(host_set lock)
930 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
931 unsigned int buflen)
933 rbuf[1] = 0x83; /* this page code */
934 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
936 /* our one and only identification descriptor (vendor-specific) */
937 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
938 rbuf[4 + 0] = 2; /* code set: ASCII */
939 rbuf[4 + 3] = strlen(inq_83_str);
940 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
943 return 0;
947 * ata_scsiop_noop -
948 * @args: device IDENTIFY data / SCSI command of interest.
949 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
950 * @buflen: Response buffer length.
952 * No operation. Simply returns success to caller, to indicate
953 * that the caller should successfully complete this SCSI command.
955 * LOCKING:
956 * spin_lock_irqsave(host_set lock)
959 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
960 unsigned int buflen)
962 VPRINTK("ENTER\n");
963 return 0;
967 * ata_msense_push - Push data onto MODE SENSE data output buffer
968 * @ptr_io: (input/output) Location to store more output data
969 * @last: End of output data buffer
970 * @buf: Pointer to BLOB being added to output buffer
971 * @buflen: Length of BLOB
973 * Store MODE SENSE data on an output buffer.
975 * LOCKING:
976 * None.
979 static void ata_msense_push(u8 **ptr_io, const u8 *last,
980 const u8 *buf, unsigned int buflen)
982 u8 *ptr = *ptr_io;
984 if ((ptr + buflen - 1) > last)
985 return;
987 memcpy(ptr, buf, buflen);
989 ptr += buflen;
991 *ptr_io = ptr;
995 * ata_msense_caching - Simulate MODE SENSE caching info page
996 * @id: device IDENTIFY data
997 * @ptr_io: (input/output) Location to store more output data
998 * @last: End of output data buffer
1000 * Generate a caching info page, which conditionally indicates
1001 * write caching to the SCSI layer, depending on device
1002 * capabilities.
1004 * LOCKING:
1005 * None.
1008 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1009 const u8 *last)
1011 u8 page[] = {
1012 0x8, /* page code */
1013 0x12, /* page length */
1014 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1015 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1018 if (ata_id_wcache_enabled(id))
1019 page[2] |= (1 << 2); /* write cache enable */
1020 if (!ata_id_rahead_enabled(id))
1021 page[12] |= (1 << 5); /* disable read ahead */
1023 ata_msense_push(ptr_io, last, page, sizeof(page));
1024 return sizeof(page);
1028 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1029 * @dev: Device associated with this MODE SENSE command
1030 * @ptr_io: (input/output) Location to store more output data
1031 * @last: End of output data buffer
1033 * Generate a generic MODE SENSE control mode page.
1035 * LOCKING:
1036 * None.
1039 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1041 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1043 /* byte 2: set the descriptor format sense data bit (bit 2)
1044 * since we need to support returning this format for SAT
1045 * commands and any SCSI commands against a 48b LBA device.
1048 ata_msense_push(ptr_io, last, page, sizeof(page));
1049 return sizeof(page);
1053 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1054 * @dev: Device associated with this MODE SENSE command
1055 * @ptr_io: (input/output) Location to store more output data
1056 * @last: End of output data buffer
1058 * Generate a generic MODE SENSE r/w error recovery page.
1060 * LOCKING:
1061 * None.
1064 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1066 const u8 page[] = {
1067 0x1, /* page code */
1068 0xa, /* page length */
1069 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1070 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1073 ata_msense_push(ptr_io, last, page, sizeof(page));
1074 return sizeof(page);
1078 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1079 * @args: device IDENTIFY data / SCSI command of interest.
1080 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1081 * @buflen: Response buffer length.
1083 * Simulate MODE SENSE commands.
1085 * LOCKING:
1086 * spin_lock_irqsave(host_set lock)
1089 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1090 unsigned int buflen)
1092 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1093 unsigned int page_control, six_byte, output_len;
1095 VPRINTK("ENTER\n");
1097 six_byte = (scsicmd[0] == MODE_SENSE);
1099 /* we only support saved and current values (which we treat
1100 * in the same manner)
1102 page_control = scsicmd[2] >> 6;
1103 if ((page_control != 0) && (page_control != 3))
1104 return 1;
1106 if (six_byte)
1107 output_len = 4;
1108 else
1109 output_len = 8;
1111 p = rbuf + output_len;
1112 last = rbuf + buflen - 1;
1114 switch(scsicmd[2] & 0x3f) {
1115 case 0x01: /* r/w error recovery */
1116 output_len += ata_msense_rw_recovery(&p, last);
1117 break;
1119 case 0x08: /* caching */
1120 output_len += ata_msense_caching(args->id, &p, last);
1121 break;
1123 case 0x0a: { /* control mode */
1124 output_len += ata_msense_ctl_mode(&p, last);
1125 break;
1128 case 0x3f: /* all pages */
1129 output_len += ata_msense_rw_recovery(&p, last);
1130 output_len += ata_msense_caching(args->id, &p, last);
1131 output_len += ata_msense_ctl_mode(&p, last);
1132 break;
1134 default: /* invalid page code */
1135 return 1;
1138 if (six_byte) {
1139 output_len--;
1140 rbuf[0] = output_len;
1141 } else {
1142 output_len -= 2;
1143 rbuf[0] = output_len >> 8;
1144 rbuf[1] = output_len;
1147 return 0;
1151 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1152 * @args: device IDENTIFY data / SCSI command of interest.
1153 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1154 * @buflen: Response buffer length.
1156 * Simulate READ CAPACITY commands.
1158 * LOCKING:
1159 * spin_lock_irqsave(host_set lock)
1162 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1163 unsigned int buflen)
1165 u64 n_sectors;
1166 u32 tmp;
1168 VPRINTK("ENTER\n");
1170 if (ata_id_has_lba48(args->id))
1171 n_sectors = ata_id_u64(args->id, 100);
1172 else
1173 n_sectors = ata_id_u32(args->id, 60);
1174 n_sectors--; /* ATA TotalUserSectors - 1 */
1176 tmp = n_sectors; /* note: truncates, if lba48 */
1177 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1178 /* sector count, 32-bit */
1179 rbuf[0] = tmp >> (8 * 3);
1180 rbuf[1] = tmp >> (8 * 2);
1181 rbuf[2] = tmp >> (8 * 1);
1182 rbuf[3] = tmp;
1184 /* sector size */
1185 tmp = ATA_SECT_SIZE;
1186 rbuf[6] = tmp >> 8;
1187 rbuf[7] = tmp;
1189 } else {
1190 /* sector count, 64-bit */
1191 rbuf[2] = n_sectors >> (8 * 7);
1192 rbuf[3] = n_sectors >> (8 * 6);
1193 rbuf[4] = n_sectors >> (8 * 5);
1194 rbuf[5] = n_sectors >> (8 * 4);
1195 rbuf[6] = tmp >> (8 * 3);
1196 rbuf[7] = tmp >> (8 * 2);
1197 rbuf[8] = tmp >> (8 * 1);
1198 rbuf[9] = tmp;
1200 /* sector size */
1201 tmp = ATA_SECT_SIZE;
1202 rbuf[12] = tmp >> 8;
1203 rbuf[13] = tmp;
1206 return 0;
1210 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1211 * @args: device IDENTIFY data / SCSI command of interest.
1212 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1213 * @buflen: Response buffer length.
1215 * Simulate REPORT LUNS command.
1217 * LOCKING:
1218 * spin_lock_irqsave(host_set lock)
1221 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1222 unsigned int buflen)
1224 VPRINTK("ENTER\n");
1225 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1227 return 0;
1231 * ata_scsi_badcmd - End a SCSI request with an error
1232 * @cmd: SCSI request to be handled
1233 * @done: SCSI command completion function
1234 * @asc: SCSI-defined additional sense code
1235 * @ascq: SCSI-defined additional sense code qualifier
1237 * Helper function that completes a SCSI command with
1238 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1239 * and the specified additional sense codes.
1241 * LOCKING:
1242 * spin_lock_irqsave(host_set lock)
1245 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1247 DPRINTK("ENTER\n");
1248 cmd->result = SAM_STAT_CHECK_CONDITION;
1250 cmd->sense_buffer[0] = 0x70;
1251 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1252 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1253 cmd->sense_buffer[12] = asc;
1254 cmd->sense_buffer[13] = ascq;
1256 done(cmd);
1259 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1261 struct scsi_cmnd *cmd = qc->scsicmd;
1263 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1264 DPRINTK("request check condition\n");
1266 cmd->result = SAM_STAT_CHECK_CONDITION;
1268 qc->scsidone(cmd);
1270 return 1;
1271 } else {
1272 u8 *scsicmd = cmd->cmnd;
1274 if (scsicmd[0] == INQUIRY) {
1275 u8 *buf = NULL;
1276 unsigned int buflen;
1278 buflen = ata_scsi_rbuf_get(cmd, &buf);
1279 buf[2] = 0x5;
1280 buf[3] = (buf[3] & 0xf0) | 2;
1281 ata_scsi_rbuf_put(cmd, buf);
1283 cmd->result = SAM_STAT_GOOD;
1286 qc->scsidone(cmd);
1288 return 0;
1291 * atapi_xlat - Initialize PACKET taskfile
1292 * @qc: command structure to be initialized
1293 * @scsicmd: SCSI CDB associated with this PACKET command
1295 * LOCKING:
1296 * spin_lock_irqsave(host_set lock)
1298 * RETURNS:
1299 * Zero on success, non-zero on failure.
1302 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1304 struct scsi_cmnd *cmd = qc->scsicmd;
1305 struct ata_device *dev = qc->dev;
1306 int using_pio = (dev->flags & ATA_DFLAG_PIO);
1307 int nodata = (cmd->sc_data_direction == SCSI_DATA_NONE);
1309 if (!using_pio)
1310 /* Check whether ATAPI DMA is safe */
1311 if (ata_check_atapi_dma(qc))
1312 using_pio = 1;
1314 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1316 qc->complete_fn = atapi_qc_complete;
1318 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1319 if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
1320 qc->tf.flags |= ATA_TFLAG_WRITE;
1321 DPRINTK("direction: write\n");
1324 qc->tf.command = ATA_CMD_PACKET;
1326 /* no data, or PIO data xfer */
1327 if (using_pio || nodata) {
1328 if (nodata)
1329 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1330 else
1331 qc->tf.protocol = ATA_PROT_ATAPI;
1332 qc->tf.lbam = (8 * 1024) & 0xff;
1333 qc->tf.lbah = (8 * 1024) >> 8;
1336 /* DMA data xfer */
1337 else {
1338 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1339 qc->tf.feature |= ATAPI_PKT_DMA;
1341 #ifdef ATAPI_ENABLE_DMADIR
1342 /* some SATA bridges need us to indicate data xfer direction */
1343 if (cmd->sc_data_direction != SCSI_DATA_WRITE)
1344 qc->tf.feature |= ATAPI_DMADIR;
1345 #endif
1348 qc->nbytes = cmd->bufflen;
1350 return 0;
1354 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1355 * @ap: ATA port to which the device is attached
1356 * @scsidev: SCSI device from which we derive the ATA device
1358 * Given various information provided in struct scsi_cmnd,
1359 * map that onto an ATA bus, and using that mapping
1360 * determine which ata_device is associated with the
1361 * SCSI command to be sent.
1363 * LOCKING:
1364 * spin_lock_irqsave(host_set lock)
1366 * RETURNS:
1367 * Associated ATA device, or %NULL if not found.
1370 static struct ata_device *
1371 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1373 struct ata_device *dev;
1375 /* skip commands not addressed to targets we simulate */
1376 if (likely(scsidev->id < ATA_MAX_DEVICES))
1377 dev = &ap->device[scsidev->id];
1378 else
1379 return NULL;
1381 if (unlikely((scsidev->channel != 0) ||
1382 (scsidev->lun != 0)))
1383 return NULL;
1385 if (unlikely(!ata_dev_present(dev)))
1386 return NULL;
1388 #ifndef ATA_ENABLE_ATAPI
1389 if (unlikely(dev->class == ATA_DEV_ATAPI))
1390 return NULL;
1391 #endif
1393 return dev;
1397 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1398 * @dev: ATA device
1399 * @cmd: SCSI command opcode to consider
1401 * Look up the SCSI command given, and determine whether the
1402 * SCSI command is to be translated or simulated.
1404 * RETURNS:
1405 * Pointer to translation function if possible, %NULL if not.
1408 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1410 switch (cmd) {
1411 case READ_6:
1412 case READ_10:
1413 case READ_16:
1415 case WRITE_6:
1416 case WRITE_10:
1417 case WRITE_16:
1418 return ata_scsi_rw_xlat;
1420 case SYNCHRONIZE_CACHE:
1421 if (ata_try_flush_cache(dev))
1422 return ata_scsi_flush_xlat;
1423 break;
1425 case VERIFY:
1426 case VERIFY_16:
1427 return ata_scsi_verify_xlat;
1430 return NULL;
1434 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1435 * @ap: ATA port to which the command was being sent
1436 * @cmd: SCSI command to dump
1438 * Prints the contents of a SCSI command via printk().
1441 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1442 struct scsi_cmnd *cmd)
1444 #ifdef ATA_DEBUG
1445 struct scsi_device *scsidev = cmd->device;
1446 u8 *scsicmd = cmd->cmnd;
1448 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1449 ap->id,
1450 scsidev->channel, scsidev->id, scsidev->lun,
1451 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1452 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1453 scsicmd[8]);
1454 #endif
1458 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1459 * @cmd: SCSI command to be sent
1460 * @done: Completion function, called when command is complete
1462 * In some cases, this function translates SCSI commands into
1463 * ATA taskfiles, and queues the taskfiles to be sent to
1464 * hardware. In other cases, this function simulates a
1465 * SCSI device by evaluating and responding to certain
1466 * SCSI commands. This creates the overall effect of
1467 * ATA and ATAPI devices appearing as SCSI devices.
1469 * LOCKING:
1470 * Releases scsi-layer-held lock, and obtains host_set lock.
1472 * RETURNS:
1473 * Zero.
1476 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1478 struct ata_port *ap;
1479 struct ata_device *dev;
1480 struct scsi_device *scsidev = cmd->device;
1482 ap = (struct ata_port *) &scsidev->host->hostdata[0];
1484 ata_scsi_dump_cdb(ap, cmd);
1486 dev = ata_scsi_find_dev(ap, scsidev);
1487 if (unlikely(!dev)) {
1488 cmd->result = (DID_BAD_TARGET << 16);
1489 done(cmd);
1490 goto out_unlock;
1493 if (dev->class == ATA_DEV_ATA) {
1494 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1495 cmd->cmnd[0]);
1497 if (xlat_func)
1498 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1499 else
1500 ata_scsi_simulate(dev->id, cmd, done);
1501 } else
1502 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1504 out_unlock:
1505 return 0;
1509 * ata_scsi_simulate - simulate SCSI command on ATA device
1510 * @id: current IDENTIFY data for target device.
1511 * @cmd: SCSI command being sent to device.
1512 * @done: SCSI command completion function.
1514 * Interprets and directly executes a select list of SCSI commands
1515 * that can be handled internally.
1517 * LOCKING:
1518 * spin_lock_irqsave(host_set lock)
1521 void ata_scsi_simulate(u16 *id,
1522 struct scsi_cmnd *cmd,
1523 void (*done)(struct scsi_cmnd *))
1525 struct ata_scsi_args args;
1526 u8 *scsicmd = cmd->cmnd;
1528 args.id = id;
1529 args.cmd = cmd;
1530 args.done = done;
1532 switch(scsicmd[0]) {
1533 /* no-op's, complete with success */
1534 case SYNCHRONIZE_CACHE:
1535 case REZERO_UNIT:
1536 case SEEK_6:
1537 case SEEK_10:
1538 case TEST_UNIT_READY:
1539 case FORMAT_UNIT: /* FIXME: correct? */
1540 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1541 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1542 break;
1544 case INQUIRY:
1545 if (scsicmd[1] & 2) /* is CmdDt set? */
1546 ata_bad_cdb(cmd, done);
1547 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1548 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1549 else if (scsicmd[2] == 0x00)
1550 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1551 else if (scsicmd[2] == 0x80)
1552 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1553 else if (scsicmd[2] == 0x83)
1554 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1555 else
1556 ata_bad_cdb(cmd, done);
1557 break;
1559 case MODE_SENSE:
1560 case MODE_SENSE_10:
1561 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1562 break;
1564 case MODE_SELECT: /* unconditionally return */
1565 case MODE_SELECT_10: /* bad-field-in-cdb */
1566 ata_bad_cdb(cmd, done);
1567 break;
1569 case READ_CAPACITY:
1570 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1571 break;
1573 case SERVICE_ACTION_IN:
1574 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1575 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1576 else
1577 ata_bad_cdb(cmd, done);
1578 break;
1580 case REPORT_LUNS:
1581 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1582 break;
1584 /* mandantory commands we haven't implemented yet */
1585 case REQUEST_SENSE:
1587 /* all other commands */
1588 default:
1589 ata_bad_scsiop(cmd, done);
1590 break;