Merge /spare/repo/linux-2.6/
[linux-2.6/sactl.git] / drivers / scsi / libata-scsi.c
blob346eb36b1e31e0c5665269077e65ef4f11149644
1 /*
2 * libata-scsi.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
36 #include <linux/kernel.h>
37 #include <linux/blkdev.h>
38 #include <linux/spinlock.h>
39 #include <scsi/scsi.h>
40 #include "scsi.h"
41 #include <scsi/scsi_host.h>
42 #include <linux/libata.h>
43 #include <asm/uaccess.h>
45 #include "libata.h"
47 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
48 static struct ata_device *
49 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
52 /**
53 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
54 * @sdev: SCSI device for which BIOS geometry is to be determined
55 * @bdev: block device associated with @sdev
56 * @capacity: capacity of SCSI device
57 * @geom: location to which geometry will be output
59 * Generic bios head/sector/cylinder calculator
60 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
61 * mapping. Some situations may arise where the disk is not
62 * bootable if this is not used.
64 * LOCKING:
65 * Defined by the SCSI layer. We don't really care.
67 * RETURNS:
68 * Zero.
70 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
71 sector_t capacity, int geom[])
73 geom[0] = 255;
74 geom[1] = 63;
75 sector_div(capacity, 255*63);
76 geom[2] = capacity;
78 return 0;
81 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
83 struct ata_port *ap;
84 struct ata_device *dev;
85 int val = -EINVAL, rc = -EINVAL;
87 ap = (struct ata_port *) &scsidev->host->hostdata[0];
88 if (!ap)
89 goto out;
91 dev = ata_scsi_find_dev(ap, scsidev);
92 if (!dev) {
93 rc = -ENODEV;
94 goto out;
97 switch (cmd) {
98 case ATA_IOC_GET_IO32:
99 val = 0;
100 if (copy_to_user(arg, &val, 1))
101 return -EFAULT;
102 return 0;
104 case ATA_IOC_SET_IO32:
105 val = (unsigned long) arg;
106 if (val != 0)
107 return -EINVAL;
108 return 0;
110 default:
111 rc = -ENOTTY;
112 break;
115 out:
116 return rc;
120 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
121 * @ap: ATA port to which the new command is attached
122 * @dev: ATA device to which the new command is attached
123 * @cmd: SCSI command that originated this ATA command
124 * @done: SCSI command completion function
126 * Obtain a reference to an unused ata_queued_cmd structure,
127 * which is the basic libata structure representing a single
128 * ATA command sent to the hardware.
130 * If a command was available, fill in the SCSI-specific
131 * portions of the structure with information on the
132 * current command.
134 * LOCKING:
135 * spin_lock_irqsave(host_set lock)
137 * RETURNS:
138 * Command allocated, or %NULL if none available.
140 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
141 struct ata_device *dev,
142 struct scsi_cmnd *cmd,
143 void (*done)(struct scsi_cmnd *))
145 struct ata_queued_cmd *qc;
147 qc = ata_qc_new_init(ap, dev);
148 if (qc) {
149 qc->scsicmd = cmd;
150 qc->scsidone = done;
152 if (cmd->use_sg) {
153 qc->sg = (struct scatterlist *) cmd->request_buffer;
154 qc->n_elem = cmd->use_sg;
155 } else {
156 qc->sg = &qc->sgent;
157 qc->n_elem = 1;
159 } else {
160 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
161 done(cmd);
164 return qc;
168 * ata_to_sense_error - convert ATA error to SCSI error
169 * @qc: Command that we are erroring out
170 * @drv_stat: value contained in ATA status register
172 * Converts an ATA error into a SCSI error. While we are at it
173 * we decode and dump the ATA error for the user so that they
174 * have some idea what really happened at the non make-believe
175 * layer.
177 * LOCKING:
178 * spin_lock_irqsave(host_set lock)
181 void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
183 struct scsi_cmnd *cmd = qc->scsicmd;
184 u8 err = 0;
185 unsigned char *sb = cmd->sense_buffer;
186 /* Based on the 3ware driver translation table */
187 static unsigned char sense_table[][4] = {
188 /* BBD|ECC|ID|MAR */
189 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
190 /* BBD|ECC|ID */
191 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
192 /* ECC|MC|MARK */
193 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
194 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
195 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
196 /* MC|ID|ABRT|TRK0|MARK */
197 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
198 /* MCR|MARK */
199 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
200 /* Bad address mark */
201 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
202 /* TRK0 */
203 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
204 /* Abort & !ICRC */
205 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
206 /* Media change request */
207 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
208 /* SRV */
209 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
210 /* Media change */
211 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
212 /* ECC */
213 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
214 /* BBD - block marked bad */
215 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
216 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
218 static unsigned char stat_table[][4] = {
219 /* Must be first because BUSY means no other bits valid */
220 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
221 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
222 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
223 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
224 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
226 int i = 0;
228 cmd->result = SAM_STAT_CHECK_CONDITION;
231 * Is this an error we can process/parse
234 if(drv_stat & ATA_ERR)
235 /* Read the err bits */
236 err = ata_chk_err(qc->ap);
238 /* Display the ATA level error info */
240 printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
241 if(drv_stat & 0x80)
243 printk("Busy ");
244 err = 0; /* Data is not valid in this case */
246 else {
247 if(drv_stat & 0x40) printk("DriveReady ");
248 if(drv_stat & 0x20) printk("DeviceFault ");
249 if(drv_stat & 0x10) printk("SeekComplete ");
250 if(drv_stat & 0x08) printk("DataRequest ");
251 if(drv_stat & 0x04) printk("CorrectedError ");
252 if(drv_stat & 0x02) printk("Index ");
253 if(drv_stat & 0x01) printk("Error ");
255 printk("}\n");
257 if(err)
259 printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err);
260 if(err & 0x04) printk("DriveStatusError ");
261 if(err & 0x80)
263 if(err & 0x04)
264 printk("BadCRC ");
265 else
266 printk("Sector ");
268 if(err & 0x40) printk("UncorrectableError ");
269 if(err & 0x10) printk("SectorIdNotFound ");
270 if(err & 0x02) printk("TrackZeroNotFound ");
271 if(err & 0x01) printk("AddrMarkNotFound ");
272 printk("}\n");
274 /* Should we dump sector info here too ?? */
278 /* Look for err */
279 while(sense_table[i][0] != 0xFF)
281 /* Look for best matches first */
282 if((sense_table[i][0] & err) == sense_table[i][0])
284 sb[0] = 0x70;
285 sb[2] = sense_table[i][1];
286 sb[7] = 0x0a;
287 sb[12] = sense_table[i][2];
288 sb[13] = sense_table[i][3];
289 return;
291 i++;
293 /* No immediate match */
294 if(err)
295 printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
297 i = 0;
298 /* Fall back to interpreting status bits */
299 while(stat_table[i][0] != 0xFF)
301 if(stat_table[i][0] & drv_stat)
303 sb[0] = 0x70;
304 sb[2] = stat_table[i][1];
305 sb[7] = 0x0a;
306 sb[12] = stat_table[i][2];
307 sb[13] = stat_table[i][3];
308 return;
310 i++;
312 /* No error ?? */
313 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
314 /* additional-sense-code[-qualifier] */
316 sb[0] = 0x70;
317 sb[2] = MEDIUM_ERROR;
318 sb[7] = 0x0A;
319 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
320 sb[12] = 0x11; /* "unrecovered read error" */
321 sb[13] = 0x04;
322 } else {
323 sb[12] = 0x0C; /* "write error - */
324 sb[13] = 0x02; /* auto-reallocation failed" */
329 * ata_scsi_slave_config - Set SCSI device attributes
330 * @sdev: SCSI device to examine
332 * This is called before we actually start reading
333 * and writing to the device, to configure certain
334 * SCSI mid-layer behaviors.
336 * LOCKING:
337 * Defined by SCSI layer. We don't really care.
340 int ata_scsi_slave_config(struct scsi_device *sdev)
342 sdev->use_10_for_rw = 1;
343 sdev->use_10_for_ms = 1;
345 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
347 if (sdev->id < ATA_MAX_DEVICES) {
348 struct ata_port *ap;
349 struct ata_device *dev;
351 ap = (struct ata_port *) &sdev->host->hostdata[0];
352 dev = &ap->device[sdev->id];
354 /* TODO: 1024 is an arbitrary number, not the
355 * hardware maximum. This should be increased to
356 * 65534 when Jens Axboe's patch for dynamically
357 * determining max_sectors is merged.
359 if ((dev->flags & ATA_DFLAG_LBA48) &&
360 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
362 * do not overwrite sdev->host->max_sectors, since
363 * other drives on this host may not support LBA48
365 blk_queue_max_sectors(sdev->request_queue, 2048);
369 return 0; /* scsi layer doesn't check return value, sigh */
373 * ata_scsi_error - SCSI layer error handler callback
374 * @host: SCSI host on which error occurred
376 * Handles SCSI-layer-thrown error events.
378 * LOCKING:
379 * Inherited from SCSI layer (none, can sleep)
381 * RETURNS:
382 * Zero.
385 int ata_scsi_error(struct Scsi_Host *host)
387 struct ata_port *ap;
389 DPRINTK("ENTER\n");
391 ap = (struct ata_port *) &host->hostdata[0];
392 ap->ops->eng_timeout(ap);
394 /* TODO: this is per-command; when queueing is supported
395 * this code will either change or move to a more
396 * appropriate place
398 host->host_failed--;
399 INIT_LIST_HEAD(&host->eh_cmd_q);
401 DPRINTK("EXIT\n");
402 return 0;
406 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
407 * @qc: Storage for translated ATA taskfile
408 * @scsicmd: SCSI command to translate
410 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
411 * (to start). Perhaps these commands should be preceded by
412 * CHECK POWER MODE to see what power mode the device is already in.
413 * [See SAT revision 5 at www.t10.org]
415 * LOCKING:
416 * spin_lock_irqsave(host_set lock)
418 * RETURNS:
419 * Zero on success, non-zero on error.
422 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
423 u8 *scsicmd)
425 struct ata_taskfile *tf = &qc->tf;
427 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
428 tf->protocol = ATA_PROT_NODATA;
429 if (scsicmd[1] & 0x1) {
430 ; /* ignore IMMED bit, violates sat-r05 */
432 if (scsicmd[4] & 0x2)
433 return 1; /* LOEJ bit set not supported */
434 if (((scsicmd[4] >> 4) & 0xf) != 0)
435 return 1; /* power conditions not supported */
436 if (scsicmd[4] & 0x1) {
437 tf->nsect = 1; /* 1 sector, lba=0 */
438 tf->lbah = 0x0;
439 tf->lbam = 0x0;
440 tf->lbal = 0x0;
441 tf->device |= ATA_LBA;
442 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
443 } else {
444 tf->nsect = 0; /* time period value (0 implies now) */
445 tf->command = ATA_CMD_STANDBY;
446 /* Consider: ATA STANDBY IMMEDIATE command */
449 * Standby and Idle condition timers could be implemented but that
450 * would require libata to implement the Power condition mode page
451 * and allow the user to change it. Changing mode pages requires
452 * MODE SELECT to be implemented.
455 return 0;
460 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
461 * @qc: Storage for translated ATA taskfile
462 * @scsicmd: SCSI command to translate (ignored)
464 * Sets up an ATA taskfile to issue FLUSH CACHE or
465 * FLUSH CACHE EXT.
467 * LOCKING:
468 * spin_lock_irqsave(host_set lock)
470 * RETURNS:
471 * Zero on success, non-zero on error.
474 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
476 struct ata_taskfile *tf = &qc->tf;
478 tf->flags |= ATA_TFLAG_DEVICE;
479 tf->protocol = ATA_PROT_NODATA;
481 if ((tf->flags & ATA_TFLAG_LBA48) &&
482 (ata_id_has_flush_ext(qc->dev->id)))
483 tf->command = ATA_CMD_FLUSH_EXT;
484 else
485 tf->command = ATA_CMD_FLUSH;
487 return 0;
491 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
492 * @qc: Storage for translated ATA taskfile
493 * @scsicmd: SCSI command to translate
495 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
497 * LOCKING:
498 * spin_lock_irqsave(host_set lock)
500 * RETURNS:
501 * Zero on success, non-zero on error.
504 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
506 struct ata_taskfile *tf = &qc->tf;
507 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
508 u64 dev_sectors = qc->dev->n_sectors;
509 u64 sect = 0;
510 u32 n_sect = 0;
512 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
513 tf->protocol = ATA_PROT_NODATA;
514 tf->device |= ATA_LBA;
516 if (scsicmd[0] == VERIFY) {
517 sect |= ((u64)scsicmd[2]) << 24;
518 sect |= ((u64)scsicmd[3]) << 16;
519 sect |= ((u64)scsicmd[4]) << 8;
520 sect |= ((u64)scsicmd[5]);
522 n_sect |= ((u32)scsicmd[7]) << 8;
523 n_sect |= ((u32)scsicmd[8]);
526 else if (scsicmd[0] == VERIFY_16) {
527 sect |= ((u64)scsicmd[2]) << 56;
528 sect |= ((u64)scsicmd[3]) << 48;
529 sect |= ((u64)scsicmd[4]) << 40;
530 sect |= ((u64)scsicmd[5]) << 32;
531 sect |= ((u64)scsicmd[6]) << 24;
532 sect |= ((u64)scsicmd[7]) << 16;
533 sect |= ((u64)scsicmd[8]) << 8;
534 sect |= ((u64)scsicmd[9]);
536 n_sect |= ((u32)scsicmd[10]) << 24;
537 n_sect |= ((u32)scsicmd[11]) << 16;
538 n_sect |= ((u32)scsicmd[12]) << 8;
539 n_sect |= ((u32)scsicmd[13]);
542 else
543 return 1;
545 if (!n_sect)
546 return 1;
547 if (sect >= dev_sectors)
548 return 1;
549 if ((sect + n_sect) > dev_sectors)
550 return 1;
551 if (lba48) {
552 if (n_sect > (64 * 1024))
553 return 1;
554 } else {
555 if (n_sect > 256)
556 return 1;
559 if (lba48) {
560 tf->command = ATA_CMD_VERIFY_EXT;
562 tf->hob_nsect = (n_sect >> 8) & 0xff;
564 tf->hob_lbah = (sect >> 40) & 0xff;
565 tf->hob_lbam = (sect >> 32) & 0xff;
566 tf->hob_lbal = (sect >> 24) & 0xff;
567 } else {
568 tf->command = ATA_CMD_VERIFY;
570 tf->device |= (sect >> 24) & 0xf;
573 tf->nsect = n_sect & 0xff;
575 tf->lbah = (sect >> 16) & 0xff;
576 tf->lbam = (sect >> 8) & 0xff;
577 tf->lbal = sect & 0xff;
579 return 0;
583 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
584 * @qc: Storage for translated ATA taskfile
585 * @scsicmd: SCSI command to translate
587 * Converts any of six SCSI read/write commands into the
588 * ATA counterpart, including starting sector (LBA),
589 * sector count, and taking into account the device's LBA48
590 * support.
592 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
593 * %WRITE_16 are currently supported.
595 * LOCKING:
596 * spin_lock_irqsave(host_set lock)
598 * RETURNS:
599 * Zero on success, non-zero on error.
602 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
604 struct ata_taskfile *tf = &qc->tf;
605 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
607 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
608 tf->protocol = qc->dev->xfer_protocol;
609 tf->device |= ATA_LBA;
611 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
612 scsicmd[0] == READ_16) {
613 tf->command = qc->dev->read_cmd;
614 } else {
615 tf->command = qc->dev->write_cmd;
616 tf->flags |= ATA_TFLAG_WRITE;
619 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
620 if (lba48) {
621 tf->hob_nsect = scsicmd[7];
622 tf->hob_lbal = scsicmd[2];
624 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
625 scsicmd[8];
626 } else {
627 /* if we don't support LBA48 addressing, the request
628 * -may- be too large. */
629 if ((scsicmd[2] & 0xf0) || scsicmd[7])
630 return 1;
632 /* stores LBA27:24 in lower 4 bits of device reg */
633 tf->device |= scsicmd[2];
635 qc->nsect = scsicmd[8];
638 tf->nsect = scsicmd[8];
639 tf->lbal = scsicmd[5];
640 tf->lbam = scsicmd[4];
641 tf->lbah = scsicmd[3];
643 VPRINTK("ten-byte command\n");
644 if (qc->nsect == 0) /* we don't support length==0 cmds */
645 return 1;
646 return 0;
649 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
650 qc->nsect = tf->nsect = scsicmd[4];
651 if (!qc->nsect) {
652 qc->nsect = 256;
653 if (lba48)
654 tf->hob_nsect = 1;
657 tf->lbal = scsicmd[3];
658 tf->lbam = scsicmd[2];
659 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
661 VPRINTK("six-byte command\n");
662 return 0;
665 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
666 /* rule out impossible LBAs and sector counts */
667 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
668 return 1;
670 if (lba48) {
671 tf->hob_nsect = scsicmd[12];
672 tf->hob_lbal = scsicmd[6];
673 tf->hob_lbam = scsicmd[5];
674 tf->hob_lbah = scsicmd[4];
676 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
677 scsicmd[13];
678 } else {
679 /* once again, filter out impossible non-zero values */
680 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
681 (scsicmd[6] & 0xf0))
682 return 1;
684 /* stores LBA27:24 in lower 4 bits of device reg */
685 tf->device |= scsicmd[6];
687 qc->nsect = scsicmd[13];
690 tf->nsect = scsicmd[13];
691 tf->lbal = scsicmd[9];
692 tf->lbam = scsicmd[8];
693 tf->lbah = scsicmd[7];
695 VPRINTK("sixteen-byte command\n");
696 if (qc->nsect == 0) /* we don't support length==0 cmds */
697 return 1;
698 return 0;
701 DPRINTK("no-byte command\n");
702 return 1;
705 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
707 struct scsi_cmnd *cmd = qc->scsicmd;
709 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
710 ata_to_sense_error(qc, drv_stat);
711 else
712 cmd->result = SAM_STAT_GOOD;
714 qc->scsidone(cmd);
716 return 0;
720 * ata_scsi_translate - Translate then issue SCSI command to ATA device
721 * @ap: ATA port to which the command is addressed
722 * @dev: ATA device to which the command is addressed
723 * @cmd: SCSI command to execute
724 * @done: SCSI command completion function
725 * @xlat_func: Actor which translates @cmd to an ATA taskfile
727 * Our ->queuecommand() function has decided that the SCSI
728 * command issued can be directly translated into an ATA
729 * command, rather than handled internally.
731 * This function sets up an ata_queued_cmd structure for the
732 * SCSI command, and sends that ata_queued_cmd to the hardware.
734 * LOCKING:
735 * spin_lock_irqsave(host_set lock)
738 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
739 struct scsi_cmnd *cmd,
740 void (*done)(struct scsi_cmnd *),
741 ata_xlat_func_t xlat_func)
743 struct ata_queued_cmd *qc;
744 u8 *scsicmd = cmd->cmnd;
746 VPRINTK("ENTER\n");
748 qc = ata_scsi_qc_new(ap, dev, cmd, done);
749 if (!qc)
750 return;
752 /* data is present; dma-map it */
753 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
754 cmd->sc_data_direction == DMA_TO_DEVICE) {
755 if (unlikely(cmd->request_bufflen < 1)) {
756 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
757 ap->id, dev->devno);
758 goto err_out;
761 if (cmd->use_sg)
762 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
763 else
764 ata_sg_init_one(qc, cmd->request_buffer,
765 cmd->request_bufflen);
767 qc->dma_dir = cmd->sc_data_direction;
770 qc->complete_fn = ata_scsi_qc_complete;
772 if (xlat_func(qc, scsicmd))
773 goto err_out;
775 /* select device, send command to hardware */
776 if (ata_qc_issue(qc))
777 goto err_out;
779 VPRINTK("EXIT\n");
780 return;
782 err_out:
783 ata_qc_free(qc);
784 ata_bad_cdb(cmd, done);
785 DPRINTK("EXIT - badcmd\n");
789 * ata_scsi_rbuf_get - Map response buffer.
790 * @cmd: SCSI command containing buffer to be mapped.
791 * @buf_out: Pointer to mapped area.
793 * Maps buffer contained within SCSI command @cmd.
795 * LOCKING:
796 * spin_lock_irqsave(host_set lock)
798 * RETURNS:
799 * Length of response buffer.
802 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
804 u8 *buf;
805 unsigned int buflen;
807 if (cmd->use_sg) {
808 struct scatterlist *sg;
810 sg = (struct scatterlist *) cmd->request_buffer;
811 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
812 buflen = sg->length;
813 } else {
814 buf = cmd->request_buffer;
815 buflen = cmd->request_bufflen;
818 *buf_out = buf;
819 return buflen;
823 * ata_scsi_rbuf_put - Unmap response buffer.
824 * @cmd: SCSI command containing buffer to be unmapped.
825 * @buf: buffer to unmap
827 * Unmaps response buffer contained within @cmd.
829 * LOCKING:
830 * spin_lock_irqsave(host_set lock)
833 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
835 if (cmd->use_sg) {
836 struct scatterlist *sg;
838 sg = (struct scatterlist *) cmd->request_buffer;
839 kunmap_atomic(buf - sg->offset, KM_USER0);
844 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
845 * @args: device IDENTIFY data / SCSI command of interest.
846 * @actor: Callback hook for desired SCSI command simulator
848 * Takes care of the hard work of simulating a SCSI command...
849 * Mapping the response buffer, calling the command's handler,
850 * and handling the handler's return value. This return value
851 * indicates whether the handler wishes the SCSI command to be
852 * completed successfully, or not.
854 * LOCKING:
855 * spin_lock_irqsave(host_set lock)
858 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
859 unsigned int (*actor) (struct ata_scsi_args *args,
860 u8 *rbuf, unsigned int buflen))
862 u8 *rbuf;
863 unsigned int buflen, rc;
864 struct scsi_cmnd *cmd = args->cmd;
866 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
867 memset(rbuf, 0, buflen);
868 rc = actor(args, rbuf, buflen);
869 ata_scsi_rbuf_put(cmd, rbuf);
871 if (rc)
872 ata_bad_cdb(cmd, args->done);
873 else {
874 cmd->result = SAM_STAT_GOOD;
875 args->done(cmd);
880 * ata_scsiop_inq_std - Simulate INQUIRY command
881 * @args: device IDENTIFY data / SCSI command of interest.
882 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
883 * @buflen: Response buffer length.
885 * Returns standard device identification data associated
886 * with non-EVPD INQUIRY command output.
888 * LOCKING:
889 * spin_lock_irqsave(host_set lock)
892 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
893 unsigned int buflen)
895 u8 hdr[] = {
896 TYPE_DISK,
898 0x5, /* claim SPC-3 version compatibility */
900 95 - 4
903 /* set scsi removeable (RMB) bit per ata bit */
904 if (ata_id_removeable(args->id))
905 hdr[1] |= (1 << 7);
907 VPRINTK("ENTER\n");
909 memcpy(rbuf, hdr, sizeof(hdr));
911 if (buflen > 35) {
912 memcpy(&rbuf[8], "ATA ", 8);
913 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
914 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
915 if (rbuf[32] == 0 || rbuf[32] == ' ')
916 memcpy(&rbuf[32], "n/a ", 4);
919 if (buflen > 63) {
920 const u8 versions[] = {
921 0x60, /* SAM-3 (no version claimed) */
923 0x03,
924 0x20, /* SBC-2 (no version claimed) */
926 0x02,
927 0x60 /* SPC-3 (no version claimed) */
930 memcpy(rbuf + 59, versions, sizeof(versions));
933 return 0;
937 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
938 * @args: device IDENTIFY data / SCSI command of interest.
939 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
940 * @buflen: Response buffer length.
942 * Returns list of inquiry EVPD pages available.
944 * LOCKING:
945 * spin_lock_irqsave(host_set lock)
948 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
949 unsigned int buflen)
951 const u8 pages[] = {
952 0x00, /* page 0x00, this page */
953 0x80, /* page 0x80, unit serial no page */
954 0x83 /* page 0x83, device ident page */
956 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
958 if (buflen > 6)
959 memcpy(rbuf + 4, pages, sizeof(pages));
961 return 0;
965 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
966 * @args: device IDENTIFY data / SCSI command of interest.
967 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
968 * @buflen: Response buffer length.
970 * Returns ATA device serial number.
972 * LOCKING:
973 * spin_lock_irqsave(host_set lock)
976 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
977 unsigned int buflen)
979 const u8 hdr[] = {
981 0x80, /* this page code */
983 ATA_SERNO_LEN, /* page len */
985 memcpy(rbuf, hdr, sizeof(hdr));
987 if (buflen > (ATA_SERNO_LEN + 4 - 1))
988 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
989 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
991 return 0;
994 static const char *inq_83_str = "Linux ATA-SCSI simulator";
997 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
998 * @args: device IDENTIFY data / SCSI command of interest.
999 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1000 * @buflen: Response buffer length.
1002 * Returns device identification. Currently hardcoded to
1003 * return "Linux ATA-SCSI simulator".
1005 * LOCKING:
1006 * spin_lock_irqsave(host_set lock)
1009 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1010 unsigned int buflen)
1012 rbuf[1] = 0x83; /* this page code */
1013 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
1015 /* our one and only identification descriptor (vendor-specific) */
1016 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1017 rbuf[4 + 0] = 2; /* code set: ASCII */
1018 rbuf[4 + 3] = strlen(inq_83_str);
1019 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1022 return 0;
1026 * ata_scsiop_noop - Command handler that simply returns success.
1027 * @args: device IDENTIFY data / SCSI command of interest.
1028 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1029 * @buflen: Response buffer length.
1031 * No operation. Simply returns success to caller, to indicate
1032 * that the caller should successfully complete this SCSI command.
1034 * LOCKING:
1035 * spin_lock_irqsave(host_set lock)
1038 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1039 unsigned int buflen)
1041 VPRINTK("ENTER\n");
1042 return 0;
1046 * ata_msense_push - Push data onto MODE SENSE data output buffer
1047 * @ptr_io: (input/output) Location to store more output data
1048 * @last: End of output data buffer
1049 * @buf: Pointer to BLOB being added to output buffer
1050 * @buflen: Length of BLOB
1052 * Store MODE SENSE data on an output buffer.
1054 * LOCKING:
1055 * None.
1058 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1059 const u8 *buf, unsigned int buflen)
1061 u8 *ptr = *ptr_io;
1063 if ((ptr + buflen - 1) > last)
1064 return;
1066 memcpy(ptr, buf, buflen);
1068 ptr += buflen;
1070 *ptr_io = ptr;
1074 * ata_msense_caching - Simulate MODE SENSE caching info page
1075 * @id: device IDENTIFY data
1076 * @ptr_io: (input/output) Location to store more output data
1077 * @last: End of output data buffer
1079 * Generate a caching info page, which conditionally indicates
1080 * write caching to the SCSI layer, depending on device
1081 * capabilities.
1083 * LOCKING:
1084 * None.
1087 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1088 const u8 *last)
1090 u8 page[] = {
1091 0x8, /* page code */
1092 0x12, /* page length */
1093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1094 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1097 if (ata_id_wcache_enabled(id))
1098 page[2] |= (1 << 2); /* write cache enable */
1099 if (!ata_id_rahead_enabled(id))
1100 page[12] |= (1 << 5); /* disable read ahead */
1102 ata_msense_push(ptr_io, last, page, sizeof(page));
1103 return sizeof(page);
1107 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1108 * @dev: Device associated with this MODE SENSE command
1109 * @ptr_io: (input/output) Location to store more output data
1110 * @last: End of output data buffer
1112 * Generate a generic MODE SENSE control mode page.
1114 * LOCKING:
1115 * None.
1118 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1120 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1122 /* byte 2: set the descriptor format sense data bit (bit 2)
1123 * since we need to support returning this format for SAT
1124 * commands and any SCSI commands against a 48b LBA device.
1127 ata_msense_push(ptr_io, last, page, sizeof(page));
1128 return sizeof(page);
1132 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1133 * @dev: Device associated with this MODE SENSE command
1134 * @ptr_io: (input/output) Location to store more output data
1135 * @last: End of output data buffer
1137 * Generate a generic MODE SENSE r/w error recovery page.
1139 * LOCKING:
1140 * None.
1143 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1145 const u8 page[] = {
1146 0x1, /* page code */
1147 0xa, /* page length */
1148 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1149 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1152 ata_msense_push(ptr_io, last, page, sizeof(page));
1153 return sizeof(page);
1157 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1158 * @args: device IDENTIFY data / SCSI command of interest.
1159 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1160 * @buflen: Response buffer length.
1162 * Simulate MODE SENSE commands.
1164 * LOCKING:
1165 * spin_lock_irqsave(host_set lock)
1168 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1169 unsigned int buflen)
1171 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1172 unsigned int page_control, six_byte, output_len;
1174 VPRINTK("ENTER\n");
1176 six_byte = (scsicmd[0] == MODE_SENSE);
1178 /* we only support saved and current values (which we treat
1179 * in the same manner)
1181 page_control = scsicmd[2] >> 6;
1182 if ((page_control != 0) && (page_control != 3))
1183 return 1;
1185 if (six_byte)
1186 output_len = 4;
1187 else
1188 output_len = 8;
1190 p = rbuf + output_len;
1191 last = rbuf + buflen - 1;
1193 switch(scsicmd[2] & 0x3f) {
1194 case 0x01: /* r/w error recovery */
1195 output_len += ata_msense_rw_recovery(&p, last);
1196 break;
1198 case 0x08: /* caching */
1199 output_len += ata_msense_caching(args->id, &p, last);
1200 break;
1202 case 0x0a: { /* control mode */
1203 output_len += ata_msense_ctl_mode(&p, last);
1204 break;
1207 case 0x3f: /* all pages */
1208 output_len += ata_msense_rw_recovery(&p, last);
1209 output_len += ata_msense_caching(args->id, &p, last);
1210 output_len += ata_msense_ctl_mode(&p, last);
1211 break;
1213 default: /* invalid page code */
1214 return 1;
1217 if (six_byte) {
1218 output_len--;
1219 rbuf[0] = output_len;
1220 } else {
1221 output_len -= 2;
1222 rbuf[0] = output_len >> 8;
1223 rbuf[1] = output_len;
1226 return 0;
1230 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1231 * @args: device IDENTIFY data / SCSI command of interest.
1232 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1233 * @buflen: Response buffer length.
1235 * Simulate READ CAPACITY commands.
1237 * LOCKING:
1238 * spin_lock_irqsave(host_set lock)
1241 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1242 unsigned int buflen)
1244 u64 n_sectors;
1245 u32 tmp;
1247 VPRINTK("ENTER\n");
1249 if (ata_id_has_lba48(args->id))
1250 n_sectors = ata_id_u64(args->id, 100);
1251 else
1252 n_sectors = ata_id_u32(args->id, 60);
1253 n_sectors--; /* ATA TotalUserSectors - 1 */
1255 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1256 if( n_sectors >= 0xffffffffULL )
1257 tmp = 0xffffffff ; /* Return max count on overflow */
1258 else
1259 tmp = n_sectors ;
1261 /* sector count, 32-bit */
1262 rbuf[0] = tmp >> (8 * 3);
1263 rbuf[1] = tmp >> (8 * 2);
1264 rbuf[2] = tmp >> (8 * 1);
1265 rbuf[3] = tmp;
1267 /* sector size */
1268 tmp = ATA_SECT_SIZE;
1269 rbuf[6] = tmp >> 8;
1270 rbuf[7] = tmp;
1272 } else {
1273 /* sector count, 64-bit */
1274 tmp = n_sectors >> (8 * 4);
1275 rbuf[2] = tmp >> (8 * 3);
1276 rbuf[3] = tmp >> (8 * 2);
1277 rbuf[4] = tmp >> (8 * 1);
1278 rbuf[5] = tmp;
1279 tmp = n_sectors;
1280 rbuf[6] = tmp >> (8 * 3);
1281 rbuf[7] = tmp >> (8 * 2);
1282 rbuf[8] = tmp >> (8 * 1);
1283 rbuf[9] = tmp;
1285 /* sector size */
1286 tmp = ATA_SECT_SIZE;
1287 rbuf[12] = tmp >> 8;
1288 rbuf[13] = tmp;
1291 return 0;
1295 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1296 * @args: device IDENTIFY data / SCSI command of interest.
1297 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1298 * @buflen: Response buffer length.
1300 * Simulate REPORT LUNS command.
1302 * LOCKING:
1303 * spin_lock_irqsave(host_set lock)
1306 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1307 unsigned int buflen)
1309 VPRINTK("ENTER\n");
1310 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1312 return 0;
1316 * ata_scsi_badcmd - End a SCSI request with an error
1317 * @cmd: SCSI request to be handled
1318 * @done: SCSI command completion function
1319 * @asc: SCSI-defined additional sense code
1320 * @ascq: SCSI-defined additional sense code qualifier
1322 * Helper function that completes a SCSI command with
1323 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1324 * and the specified additional sense codes.
1326 * LOCKING:
1327 * spin_lock_irqsave(host_set lock)
1330 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1332 DPRINTK("ENTER\n");
1333 cmd->result = SAM_STAT_CHECK_CONDITION;
1335 cmd->sense_buffer[0] = 0x70;
1336 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1337 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1338 cmd->sense_buffer[12] = asc;
1339 cmd->sense_buffer[13] = ascq;
1341 done(cmd);
1344 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1346 struct scsi_cmnd *cmd = qc->scsicmd;
1348 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1349 DPRINTK("request check condition\n");
1351 cmd->result = SAM_STAT_CHECK_CONDITION;
1353 qc->scsidone(cmd);
1355 return 1;
1356 } else {
1357 u8 *scsicmd = cmd->cmnd;
1359 if (scsicmd[0] == INQUIRY) {
1360 u8 *buf = NULL;
1361 unsigned int buflen;
1363 buflen = ata_scsi_rbuf_get(cmd, &buf);
1364 buf[2] = 0x5;
1365 buf[3] = (buf[3] & 0xf0) | 2;
1366 ata_scsi_rbuf_put(cmd, buf);
1368 cmd->result = SAM_STAT_GOOD;
1371 qc->scsidone(cmd);
1373 return 0;
1376 * atapi_xlat - Initialize PACKET taskfile
1377 * @qc: command structure to be initialized
1378 * @scsicmd: SCSI CDB associated with this PACKET command
1380 * LOCKING:
1381 * spin_lock_irqsave(host_set lock)
1383 * RETURNS:
1384 * Zero on success, non-zero on failure.
1387 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1389 struct scsi_cmnd *cmd = qc->scsicmd;
1390 struct ata_device *dev = qc->dev;
1391 int using_pio = (dev->flags & ATA_DFLAG_PIO);
1392 int nodata = (cmd->sc_data_direction == DMA_NONE);
1394 if (!using_pio)
1395 /* Check whether ATAPI DMA is safe */
1396 if (ata_check_atapi_dma(qc))
1397 using_pio = 1;
1399 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1401 qc->complete_fn = atapi_qc_complete;
1403 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1404 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1405 qc->tf.flags |= ATA_TFLAG_WRITE;
1406 DPRINTK("direction: write\n");
1409 qc->tf.command = ATA_CMD_PACKET;
1411 /* no data, or PIO data xfer */
1412 if (using_pio || nodata) {
1413 if (nodata)
1414 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1415 else
1416 qc->tf.protocol = ATA_PROT_ATAPI;
1417 qc->tf.lbam = (8 * 1024) & 0xff;
1418 qc->tf.lbah = (8 * 1024) >> 8;
1421 /* DMA data xfer */
1422 else {
1423 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1424 qc->tf.feature |= ATAPI_PKT_DMA;
1426 #ifdef ATAPI_ENABLE_DMADIR
1427 /* some SATA bridges need us to indicate data xfer direction */
1428 if (cmd->sc_data_direction != DMA_TO_DEVICE)
1429 qc->tf.feature |= ATAPI_DMADIR;
1430 #endif
1433 qc->nbytes = cmd->bufflen;
1435 return 0;
1439 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1440 * @ap: ATA port to which the device is attached
1441 * @scsidev: SCSI device from which we derive the ATA device
1443 * Given various information provided in struct scsi_cmnd,
1444 * map that onto an ATA bus, and using that mapping
1445 * determine which ata_device is associated with the
1446 * SCSI command to be sent.
1448 * LOCKING:
1449 * spin_lock_irqsave(host_set lock)
1451 * RETURNS:
1452 * Associated ATA device, or %NULL if not found.
1455 static struct ata_device *
1456 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1458 struct ata_device *dev;
1460 /* skip commands not addressed to targets we simulate */
1461 if (likely(scsidev->id < ATA_MAX_DEVICES))
1462 dev = &ap->device[scsidev->id];
1463 else
1464 return NULL;
1466 if (unlikely((scsidev->channel != 0) ||
1467 (scsidev->lun != 0)))
1468 return NULL;
1470 if (unlikely(!ata_dev_present(dev)))
1471 return NULL;
1473 #ifndef ATA_ENABLE_ATAPI
1474 if (unlikely(dev->class == ATA_DEV_ATAPI))
1475 return NULL;
1476 #endif
1478 return dev;
1482 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1483 * @dev: ATA device
1484 * @cmd: SCSI command opcode to consider
1486 * Look up the SCSI command given, and determine whether the
1487 * SCSI command is to be translated or simulated.
1489 * RETURNS:
1490 * Pointer to translation function if possible, %NULL if not.
1493 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1495 switch (cmd) {
1496 case READ_6:
1497 case READ_10:
1498 case READ_16:
1500 case WRITE_6:
1501 case WRITE_10:
1502 case WRITE_16:
1503 return ata_scsi_rw_xlat;
1505 case SYNCHRONIZE_CACHE:
1506 if (ata_try_flush_cache(dev))
1507 return ata_scsi_flush_xlat;
1508 break;
1510 case VERIFY:
1511 case VERIFY_16:
1512 return ata_scsi_verify_xlat;
1513 case START_STOP:
1514 return ata_scsi_start_stop_xlat;
1517 return NULL;
1521 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1522 * @ap: ATA port to which the command was being sent
1523 * @cmd: SCSI command to dump
1525 * Prints the contents of a SCSI command via printk().
1528 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1529 struct scsi_cmnd *cmd)
1531 #ifdef ATA_DEBUG
1532 struct scsi_device *scsidev = cmd->device;
1533 u8 *scsicmd = cmd->cmnd;
1535 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1536 ap->id,
1537 scsidev->channel, scsidev->id, scsidev->lun,
1538 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1539 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1540 scsicmd[8]);
1541 #endif
1545 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1546 * @cmd: SCSI command to be sent
1547 * @done: Completion function, called when command is complete
1549 * In some cases, this function translates SCSI commands into
1550 * ATA taskfiles, and queues the taskfiles to be sent to
1551 * hardware. In other cases, this function simulates a
1552 * SCSI device by evaluating and responding to certain
1553 * SCSI commands. This creates the overall effect of
1554 * ATA and ATAPI devices appearing as SCSI devices.
1556 * LOCKING:
1557 * Releases scsi-layer-held lock, and obtains host_set lock.
1559 * RETURNS:
1560 * Zero.
1563 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1565 struct ata_port *ap;
1566 struct ata_device *dev;
1567 struct scsi_device *scsidev = cmd->device;
1569 ap = (struct ata_port *) &scsidev->host->hostdata[0];
1571 ata_scsi_dump_cdb(ap, cmd);
1573 dev = ata_scsi_find_dev(ap, scsidev);
1574 if (unlikely(!dev)) {
1575 cmd->result = (DID_BAD_TARGET << 16);
1576 done(cmd);
1577 goto out_unlock;
1580 if (dev->class == ATA_DEV_ATA) {
1581 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1582 cmd->cmnd[0]);
1584 if (xlat_func)
1585 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1586 else
1587 ata_scsi_simulate(dev->id, cmd, done);
1588 } else
1589 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1591 out_unlock:
1592 return 0;
1596 * ata_scsi_simulate - simulate SCSI command on ATA device
1597 * @id: current IDENTIFY data for target device.
1598 * @cmd: SCSI command being sent to device.
1599 * @done: SCSI command completion function.
1601 * Interprets and directly executes a select list of SCSI commands
1602 * that can be handled internally.
1604 * LOCKING:
1605 * spin_lock_irqsave(host_set lock)
1608 void ata_scsi_simulate(u16 *id,
1609 struct scsi_cmnd *cmd,
1610 void (*done)(struct scsi_cmnd *))
1612 struct ata_scsi_args args;
1613 u8 *scsicmd = cmd->cmnd;
1615 args.id = id;
1616 args.cmd = cmd;
1617 args.done = done;
1619 switch(scsicmd[0]) {
1620 /* no-op's, complete with success */
1621 case SYNCHRONIZE_CACHE:
1622 case REZERO_UNIT:
1623 case SEEK_6:
1624 case SEEK_10:
1625 case TEST_UNIT_READY:
1626 case FORMAT_UNIT: /* FIXME: correct? */
1627 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1628 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1629 break;
1631 case INQUIRY:
1632 if (scsicmd[1] & 2) /* is CmdDt set? */
1633 ata_bad_cdb(cmd, done);
1634 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1635 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1636 else if (scsicmd[2] == 0x00)
1637 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1638 else if (scsicmd[2] == 0x80)
1639 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1640 else if (scsicmd[2] == 0x83)
1641 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1642 else
1643 ata_bad_cdb(cmd, done);
1644 break;
1646 case MODE_SENSE:
1647 case MODE_SENSE_10:
1648 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1649 break;
1651 case MODE_SELECT: /* unconditionally return */
1652 case MODE_SELECT_10: /* bad-field-in-cdb */
1653 ata_bad_cdb(cmd, done);
1654 break;
1656 case READ_CAPACITY:
1657 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1658 break;
1660 case SERVICE_ACTION_IN:
1661 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1662 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1663 else
1664 ata_bad_cdb(cmd, done);
1665 break;
1667 case REPORT_LUNS:
1668 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1669 break;
1671 /* mandantory commands we haven't implemented yet */
1672 case REQUEST_SENSE:
1674 /* all other commands */
1675 default:
1676 ata_bad_scsiop(cmd, done);
1677 break;