2 * libata-scsi.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
36 #include <linux/kernel.h>
37 #include <linux/blkdev.h>
38 #include <linux/spinlock.h>
39 #include <scsi/scsi.h>
41 #include <scsi/scsi_host.h>
42 #include <linux/libata.h>
43 #include <asm/uaccess.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
);
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.
65 * Defined by the SCSI layer. We don't really care.
70 int ata_std_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
71 sector_t capacity
, int geom
[])
75 sector_div(capacity
, 255*63);
81 int ata_scsi_ioctl(struct scsi_device
*scsidev
, int cmd
, void __user
*arg
)
84 struct ata_device
*dev
;
85 int val
= -EINVAL
, rc
= -EINVAL
;
87 ap
= (struct ata_port
*) &scsidev
->host
->hostdata
[0];
91 dev
= ata_scsi_find_dev(ap
, scsidev
);
98 case ATA_IOC_GET_IO32
:
100 if (copy_to_user(arg
, &val
, 1))
104 case ATA_IOC_SET_IO32
:
105 val
= (unsigned long) arg
;
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
135 * spin_lock_irqsave(host_set lock)
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
);
153 qc
->sg
= (struct scatterlist
*) cmd
->request_buffer
;
154 qc
->n_elem
= cmd
->use_sg
;
160 cmd
->result
= (DID_OK
<< 16) | (QUEUE_FULL
<< 1);
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
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
;
185 unsigned char *sb
= cmd
->sense_buffer
;
186 /* Based on the 3ware driver translation table */
187 static unsigned char sense_table
[][4] = {
189 {0xd1, ABORTED_COMMAND
, 0x00, 0x00}, // Device busy Aborted command
191 {0xd0, ABORTED_COMMAND
, 0x00, 0x00}, // Device busy Aborted command
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
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
203 {0x02, HARDWARE_ERROR
, 0x00, 0x00}, // Track 0 not found Hardware error
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
209 {0x10, ABORTED_COMMAND
, 0x14, 0x00}, // ID not found Recorded entity not found
211 {0x08, NOT_READY
, 0x04, 0x00}, // Media change FIXME: faking offline
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
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
);
244 err
= 0; /* Data is not valid in this case */
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 ");
259 printk(KERN_WARNING
"ata%u: error=0x%02x { ", qc
->ap
->id
, err
);
260 if(err
& 0x04) printk("DriveStatusError ");
268 if(err
& 0x40) printk("UncorrectableError ");
269 if(err
& 0x10) printk("SectorIdNotFound ");
270 if(err
& 0x02) printk("TrackZeroNotFound ");
271 if(err
& 0x01) printk("AddrMarkNotFound ");
274 /* Should we dump sector info here too ?? */
279 while(sense_table
[i
][0] != 0xFF)
281 /* Look for best matches first */
282 if((sense_table
[i
][0] & err
) == sense_table
[i
][0])
285 sb
[2] = sense_table
[i
][1];
287 sb
[12] = sense_table
[i
][2];
288 sb
[13] = sense_table
[i
][3];
293 /* No immediate match */
295 printk(KERN_DEBUG
"ata%u: no sense translation for 0x%02x\n", qc
->ap
->id
, err
);
298 /* Fall back to interpreting status bits */
299 while(stat_table
[i
][0] != 0xFF)
301 if(stat_table
[i
][0] & drv_stat
)
304 sb
[2] = stat_table
[i
][1];
306 sb
[12] = stat_table
[i
][2];
307 sb
[13] = stat_table
[i
][3];
313 printk(KERN_ERR
"ata%u: called with no error (%02X)!\n", qc
->ap
->id
, drv_stat
);
314 /* additional-sense-code[-qualifier] */
317 sb
[2] = MEDIUM_ERROR
;
319 if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
) {
320 sb
[12] = 0x11; /* "unrecovered read error" */
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.
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
) {
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.
379 * Inherited from SCSI layer (none, can sleep)
385 int ata_scsi_error(struct Scsi_Host
*host
)
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
399 INIT_LIST_HEAD(&host
->eh_cmd_q
);
406 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
407 * @qc: Storage for translated ATA taskfile
408 * @scsicmd: SCSI command to translate (ignored)
410 * Sets up an ATA taskfile to issue FLUSH CACHE or
414 * spin_lock_irqsave(host_set lock)
417 * Zero on success, non-zero on error.
420 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd
*qc
, u8
*scsicmd
)
422 struct ata_taskfile
*tf
= &qc
->tf
;
424 tf
->flags
|= ATA_TFLAG_DEVICE
;
425 tf
->protocol
= ATA_PROT_NODATA
;
427 if ((tf
->flags
& ATA_TFLAG_LBA48
) &&
428 (ata_id_has_flush_ext(qc
->dev
->id
)))
429 tf
->command
= ATA_CMD_FLUSH_EXT
;
431 tf
->command
= ATA_CMD_FLUSH
;
437 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
438 * @qc: Storage for translated ATA taskfile
439 * @scsicmd: SCSI command to translate
441 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
444 * spin_lock_irqsave(host_set lock)
447 * Zero on success, non-zero on error.
450 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd
*qc
, u8
*scsicmd
)
452 struct ata_taskfile
*tf
= &qc
->tf
;
453 unsigned int lba48
= tf
->flags
& ATA_TFLAG_LBA48
;
454 u64 dev_sectors
= qc
->dev
->n_sectors
;
458 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
459 tf
->protocol
= ATA_PROT_NODATA
;
460 tf
->device
|= ATA_LBA
;
462 if (scsicmd
[0] == VERIFY
) {
463 sect
|= ((u64
)scsicmd
[2]) << 24;
464 sect
|= ((u64
)scsicmd
[3]) << 16;
465 sect
|= ((u64
)scsicmd
[4]) << 8;
466 sect
|= ((u64
)scsicmd
[5]);
468 n_sect
|= ((u32
)scsicmd
[7]) << 8;
469 n_sect
|= ((u32
)scsicmd
[8]);
472 else if (scsicmd
[0] == VERIFY_16
) {
473 sect
|= ((u64
)scsicmd
[2]) << 56;
474 sect
|= ((u64
)scsicmd
[3]) << 48;
475 sect
|= ((u64
)scsicmd
[4]) << 40;
476 sect
|= ((u64
)scsicmd
[5]) << 32;
477 sect
|= ((u64
)scsicmd
[6]) << 24;
478 sect
|= ((u64
)scsicmd
[7]) << 16;
479 sect
|= ((u64
)scsicmd
[8]) << 8;
480 sect
|= ((u64
)scsicmd
[9]);
482 n_sect
|= ((u32
)scsicmd
[10]) << 24;
483 n_sect
|= ((u32
)scsicmd
[11]) << 16;
484 n_sect
|= ((u32
)scsicmd
[12]) << 8;
485 n_sect
|= ((u32
)scsicmd
[13]);
493 if (sect
>= dev_sectors
)
495 if ((sect
+ n_sect
) > dev_sectors
)
498 if (n_sect
> (64 * 1024))
506 tf
->command
= ATA_CMD_VERIFY_EXT
;
508 tf
->hob_nsect
= (n_sect
>> 8) & 0xff;
510 tf
->hob_lbah
= (sect
>> 40) & 0xff;
511 tf
->hob_lbam
= (sect
>> 32) & 0xff;
512 tf
->hob_lbal
= (sect
>> 24) & 0xff;
514 tf
->command
= ATA_CMD_VERIFY
;
516 tf
->device
|= (sect
>> 24) & 0xf;
519 tf
->nsect
= n_sect
& 0xff;
521 tf
->lbah
= (sect
>> 16) & 0xff;
522 tf
->lbam
= (sect
>> 8) & 0xff;
523 tf
->lbal
= sect
& 0xff;
529 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
530 * @qc: Storage for translated ATA taskfile
531 * @scsicmd: SCSI command to translate
533 * Converts any of six SCSI read/write commands into the
534 * ATA counterpart, including starting sector (LBA),
535 * sector count, and taking into account the device's LBA48
538 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
539 * %WRITE_16 are currently supported.
542 * spin_lock_irqsave(host_set lock)
545 * Zero on success, non-zero on error.
548 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd
*qc
, u8
*scsicmd
)
550 struct ata_taskfile
*tf
= &qc
->tf
;
551 unsigned int lba48
= tf
->flags
& ATA_TFLAG_LBA48
;
553 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
554 tf
->protocol
= qc
->dev
->xfer_protocol
;
555 tf
->device
|= ATA_LBA
;
557 if (scsicmd
[0] == READ_10
|| scsicmd
[0] == READ_6
||
558 scsicmd
[0] == READ_16
) {
559 tf
->command
= qc
->dev
->read_cmd
;
561 tf
->command
= qc
->dev
->write_cmd
;
562 tf
->flags
|= ATA_TFLAG_WRITE
;
565 if (scsicmd
[0] == READ_10
|| scsicmd
[0] == WRITE_10
) {
567 tf
->hob_nsect
= scsicmd
[7];
568 tf
->hob_lbal
= scsicmd
[2];
570 qc
->nsect
= ((unsigned int)scsicmd
[7] << 8) |
573 /* if we don't support LBA48 addressing, the request
574 * -may- be too large. */
575 if ((scsicmd
[2] & 0xf0) || scsicmd
[7])
578 /* stores LBA27:24 in lower 4 bits of device reg */
579 tf
->device
|= scsicmd
[2];
581 qc
->nsect
= scsicmd
[8];
584 tf
->nsect
= scsicmd
[8];
585 tf
->lbal
= scsicmd
[5];
586 tf
->lbam
= scsicmd
[4];
587 tf
->lbah
= scsicmd
[3];
589 VPRINTK("ten-byte command\n");
593 if (scsicmd
[0] == READ_6
|| scsicmd
[0] == WRITE_6
) {
594 qc
->nsect
= tf
->nsect
= scsicmd
[4];
595 tf
->lbal
= scsicmd
[3];
596 tf
->lbam
= scsicmd
[2];
597 tf
->lbah
= scsicmd
[1] & 0x1f; /* mask out reserved bits */
599 VPRINTK("six-byte command\n");
603 if (scsicmd
[0] == READ_16
|| scsicmd
[0] == WRITE_16
) {
604 /* rule out impossible LBAs and sector counts */
605 if (scsicmd
[2] || scsicmd
[3] || scsicmd
[10] || scsicmd
[11])
609 tf
->hob_nsect
= scsicmd
[12];
610 tf
->hob_lbal
= scsicmd
[6];
611 tf
->hob_lbam
= scsicmd
[5];
612 tf
->hob_lbah
= scsicmd
[4];
614 qc
->nsect
= ((unsigned int)scsicmd
[12] << 8) |
617 /* once again, filter out impossible non-zero values */
618 if (scsicmd
[4] || scsicmd
[5] || scsicmd
[12] ||
622 /* stores LBA27:24 in lower 4 bits of device reg */
623 tf
->device
|= scsicmd
[6];
625 qc
->nsect
= scsicmd
[13];
628 tf
->nsect
= scsicmd
[13];
629 tf
->lbal
= scsicmd
[9];
630 tf
->lbam
= scsicmd
[8];
631 tf
->lbah
= scsicmd
[7];
633 VPRINTK("sixteen-byte command\n");
637 DPRINTK("no-byte command\n");
641 static int ata_scsi_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
643 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
645 if (unlikely(drv_stat
& (ATA_ERR
| ATA_BUSY
| ATA_DRQ
)))
646 ata_to_sense_error(qc
, drv_stat
);
648 cmd
->result
= SAM_STAT_GOOD
;
656 * ata_scsi_translate - Translate then issue SCSI command to ATA device
657 * @ap: ATA port to which the command is addressed
658 * @dev: ATA device to which the command is addressed
659 * @cmd: SCSI command to execute
660 * @done: SCSI command completion function
661 * @xlat_func: Actor which translates @cmd to an ATA taskfile
663 * Our ->queuecommand() function has decided that the SCSI
664 * command issued can be directly translated into an ATA
665 * command, rather than handled internally.
667 * This function sets up an ata_queued_cmd structure for the
668 * SCSI command, and sends that ata_queued_cmd to the hardware.
671 * spin_lock_irqsave(host_set lock)
674 static void ata_scsi_translate(struct ata_port
*ap
, struct ata_device
*dev
,
675 struct scsi_cmnd
*cmd
,
676 void (*done
)(struct scsi_cmnd
*),
677 ata_xlat_func_t xlat_func
)
679 struct ata_queued_cmd
*qc
;
680 u8
*scsicmd
= cmd
->cmnd
;
684 qc
= ata_scsi_qc_new(ap
, dev
, cmd
, done
);
688 /* data is present; dma-map it */
689 if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
||
690 cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
691 if (unlikely(cmd
->request_bufflen
< 1)) {
692 printk(KERN_WARNING
"ata%u(%u): WARNING: zero len r/w req\n",
698 ata_sg_init(qc
, cmd
->request_buffer
, cmd
->use_sg
);
700 ata_sg_init_one(qc
, cmd
->request_buffer
,
701 cmd
->request_bufflen
);
703 qc
->dma_dir
= cmd
->sc_data_direction
;
706 qc
->complete_fn
= ata_scsi_qc_complete
;
708 if (xlat_func(qc
, scsicmd
))
711 /* select device, send command to hardware */
712 if (ata_qc_issue(qc
))
720 ata_bad_cdb(cmd
, done
);
721 DPRINTK("EXIT - badcmd\n");
725 * ata_scsi_rbuf_get - Map response buffer.
726 * @cmd: SCSI command containing buffer to be mapped.
727 * @buf_out: Pointer to mapped area.
729 * Maps buffer contained within SCSI command @cmd.
732 * spin_lock_irqsave(host_set lock)
735 * Length of response buffer.
738 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd
*cmd
, u8
**buf_out
)
744 struct scatterlist
*sg
;
746 sg
= (struct scatterlist
*) cmd
->request_buffer
;
747 buf
= kmap_atomic(sg
->page
, KM_USER0
) + sg
->offset
;
750 buf
= cmd
->request_buffer
;
751 buflen
= cmd
->request_bufflen
;
759 * ata_scsi_rbuf_put - Unmap response buffer.
760 * @cmd: SCSI command containing buffer to be unmapped.
761 * @buf: buffer to unmap
763 * Unmaps response buffer contained within @cmd.
766 * spin_lock_irqsave(host_set lock)
769 static inline void ata_scsi_rbuf_put(struct scsi_cmnd
*cmd
, u8
*buf
)
772 struct scatterlist
*sg
;
774 sg
= (struct scatterlist
*) cmd
->request_buffer
;
775 kunmap_atomic(buf
- sg
->offset
, KM_USER0
);
780 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
781 * @args: device IDENTIFY data / SCSI command of interest.
782 * @actor: Callback hook for desired SCSI command simulator
784 * Takes care of the hard work of simulating a SCSI command...
785 * Mapping the response buffer, calling the command's handler,
786 * and handling the handler's return value. This return value
787 * indicates whether the handler wishes the SCSI command to be
788 * completed successfully, or not.
791 * spin_lock_irqsave(host_set lock)
794 void ata_scsi_rbuf_fill(struct ata_scsi_args
*args
,
795 unsigned int (*actor
) (struct ata_scsi_args
*args
,
796 u8
*rbuf
, unsigned int buflen
))
799 unsigned int buflen
, rc
;
800 struct scsi_cmnd
*cmd
= args
->cmd
;
802 buflen
= ata_scsi_rbuf_get(cmd
, &rbuf
);
803 memset(rbuf
, 0, buflen
);
804 rc
= actor(args
, rbuf
, buflen
);
805 ata_scsi_rbuf_put(cmd
, rbuf
);
808 ata_bad_cdb(cmd
, args
->done
);
810 cmd
->result
= SAM_STAT_GOOD
;
816 * ata_scsiop_inq_std - Simulate INQUIRY command
817 * @args: device IDENTIFY data / SCSI command of interest.
818 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
819 * @buflen: Response buffer length.
821 * Returns standard device identification data associated
822 * with non-EVPD INQUIRY command output.
825 * spin_lock_irqsave(host_set lock)
828 unsigned int ata_scsiop_inq_std(struct ata_scsi_args
*args
, u8
*rbuf
,
834 0x5, /* claim SPC-3 version compatibility */
839 /* set scsi removeable (RMB) bit per ata bit */
840 if (ata_id_removeable(args
->id
))
845 memcpy(rbuf
, hdr
, sizeof(hdr
));
848 memcpy(&rbuf
[8], "ATA ", 8);
849 ata_dev_id_string(args
->id
, &rbuf
[16], ATA_ID_PROD_OFS
, 16);
850 ata_dev_id_string(args
->id
, &rbuf
[32], ATA_ID_FW_REV_OFS
, 4);
851 if (rbuf
[32] == 0 || rbuf
[32] == ' ')
852 memcpy(&rbuf
[32], "n/a ", 4);
856 const u8 versions
[] = {
857 0x60, /* SAM-3 (no version claimed) */
860 0x20, /* SBC-2 (no version claimed) */
863 0x60 /* SPC-3 (no version claimed) */
866 memcpy(rbuf
+ 59, versions
, sizeof(versions
));
873 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
874 * @args: device IDENTIFY data / SCSI command of interest.
875 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
876 * @buflen: Response buffer length.
878 * Returns list of inquiry EVPD pages available.
881 * spin_lock_irqsave(host_set lock)
884 unsigned int ata_scsiop_inq_00(struct ata_scsi_args
*args
, u8
*rbuf
,
888 0x00, /* page 0x00, this page */
889 0x80, /* page 0x80, unit serial no page */
890 0x83 /* page 0x83, device ident page */
892 rbuf
[3] = sizeof(pages
); /* number of supported EVPD pages */
895 memcpy(rbuf
+ 4, pages
, sizeof(pages
));
901 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
902 * @args: device IDENTIFY data / SCSI command of interest.
903 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
904 * @buflen: Response buffer length.
906 * Returns ATA device serial number.
909 * spin_lock_irqsave(host_set lock)
912 unsigned int ata_scsiop_inq_80(struct ata_scsi_args
*args
, u8
*rbuf
,
917 0x80, /* this page code */
919 ATA_SERNO_LEN
, /* page len */
921 memcpy(rbuf
, hdr
, sizeof(hdr
));
923 if (buflen
> (ATA_SERNO_LEN
+ 4 - 1))
924 ata_dev_id_string(args
->id
, (unsigned char *) &rbuf
[4],
925 ATA_ID_SERNO_OFS
, ATA_SERNO_LEN
);
930 static const char *inq_83_str
= "Linux ATA-SCSI simulator";
933 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
934 * @args: device IDENTIFY data / SCSI command of interest.
935 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
936 * @buflen: Response buffer length.
938 * Returns device identification. Currently hardcoded to
939 * return "Linux ATA-SCSI simulator".
942 * spin_lock_irqsave(host_set lock)
945 unsigned int ata_scsiop_inq_83(struct ata_scsi_args
*args
, u8
*rbuf
,
948 rbuf
[1] = 0x83; /* this page code */
949 rbuf
[3] = 4 + strlen(inq_83_str
); /* page len */
951 /* our one and only identification descriptor (vendor-specific) */
952 if (buflen
> (strlen(inq_83_str
) + 4 + 4 - 1)) {
953 rbuf
[4 + 0] = 2; /* code set: ASCII */
954 rbuf
[4 + 3] = strlen(inq_83_str
);
955 memcpy(rbuf
+ 4 + 4, inq_83_str
, strlen(inq_83_str
));
962 * ata_scsiop_noop - Command handler that simply returns success.
963 * @args: device IDENTIFY data / SCSI command of interest.
964 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
965 * @buflen: Response buffer length.
967 * No operation. Simply returns success to caller, to indicate
968 * that the caller should successfully complete this SCSI command.
971 * spin_lock_irqsave(host_set lock)
974 unsigned int ata_scsiop_noop(struct ata_scsi_args
*args
, u8
*rbuf
,
982 * ata_msense_push - Push data onto MODE SENSE data output buffer
983 * @ptr_io: (input/output) Location to store more output data
984 * @last: End of output data buffer
985 * @buf: Pointer to BLOB being added to output buffer
986 * @buflen: Length of BLOB
988 * Store MODE SENSE data on an output buffer.
994 static void ata_msense_push(u8
**ptr_io
, const u8
*last
,
995 const u8
*buf
, unsigned int buflen
)
999 if ((ptr
+ buflen
- 1) > last
)
1002 memcpy(ptr
, buf
, buflen
);
1010 * ata_msense_caching - Simulate MODE SENSE caching info page
1011 * @id: device IDENTIFY data
1012 * @ptr_io: (input/output) Location to store more output data
1013 * @last: End of output data buffer
1015 * Generate a caching info page, which conditionally indicates
1016 * write caching to the SCSI layer, depending on device
1023 static unsigned int ata_msense_caching(u16
*id
, u8
**ptr_io
,
1027 0x8, /* page code */
1028 0x12, /* page length */
1029 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1030 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1033 if (ata_id_wcache_enabled(id
))
1034 page
[2] |= (1 << 2); /* write cache enable */
1035 if (!ata_id_rahead_enabled(id
))
1036 page
[12] |= (1 << 5); /* disable read ahead */
1038 ata_msense_push(ptr_io
, last
, page
, sizeof(page
));
1039 return sizeof(page
);
1043 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1044 * @dev: Device associated with this MODE SENSE command
1045 * @ptr_io: (input/output) Location to store more output data
1046 * @last: End of output data buffer
1048 * Generate a generic MODE SENSE control mode page.
1054 static unsigned int ata_msense_ctl_mode(u8
**ptr_io
, const u8
*last
)
1056 const u8 page
[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1058 /* byte 2: set the descriptor format sense data bit (bit 2)
1059 * since we need to support returning this format for SAT
1060 * commands and any SCSI commands against a 48b LBA device.
1063 ata_msense_push(ptr_io
, last
, page
, sizeof(page
));
1064 return sizeof(page
);
1068 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1069 * @dev: Device associated with this MODE SENSE command
1070 * @ptr_io: (input/output) Location to store more output data
1071 * @last: End of output data buffer
1073 * Generate a generic MODE SENSE r/w error recovery page.
1079 static unsigned int ata_msense_rw_recovery(u8
**ptr_io
, const u8
*last
)
1082 0x1, /* page code */
1083 0xa, /* page length */
1084 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1085 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1088 ata_msense_push(ptr_io
, last
, page
, sizeof(page
));
1089 return sizeof(page
);
1093 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1094 * @args: device IDENTIFY data / SCSI command of interest.
1095 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1096 * @buflen: Response buffer length.
1098 * Simulate MODE SENSE commands.
1101 * spin_lock_irqsave(host_set lock)
1104 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args
*args
, u8
*rbuf
,
1105 unsigned int buflen
)
1107 u8
*scsicmd
= args
->cmd
->cmnd
, *p
, *last
;
1108 unsigned int page_control
, six_byte
, output_len
;
1112 six_byte
= (scsicmd
[0] == MODE_SENSE
);
1114 /* we only support saved and current values (which we treat
1115 * in the same manner)
1117 page_control
= scsicmd
[2] >> 6;
1118 if ((page_control
!= 0) && (page_control
!= 3))
1126 p
= rbuf
+ output_len
;
1127 last
= rbuf
+ buflen
- 1;
1129 switch(scsicmd
[2] & 0x3f) {
1130 case 0x01: /* r/w error recovery */
1131 output_len
+= ata_msense_rw_recovery(&p
, last
);
1134 case 0x08: /* caching */
1135 output_len
+= ata_msense_caching(args
->id
, &p
, last
);
1138 case 0x0a: { /* control mode */
1139 output_len
+= ata_msense_ctl_mode(&p
, last
);
1143 case 0x3f: /* all pages */
1144 output_len
+= ata_msense_rw_recovery(&p
, last
);
1145 output_len
+= ata_msense_caching(args
->id
, &p
, last
);
1146 output_len
+= ata_msense_ctl_mode(&p
, last
);
1149 default: /* invalid page code */
1155 rbuf
[0] = output_len
;
1158 rbuf
[0] = output_len
>> 8;
1159 rbuf
[1] = output_len
;
1166 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1167 * @args: device IDENTIFY data / SCSI command of interest.
1168 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1169 * @buflen: Response buffer length.
1171 * Simulate READ CAPACITY commands.
1174 * spin_lock_irqsave(host_set lock)
1177 unsigned int ata_scsiop_read_cap(struct ata_scsi_args
*args
, u8
*rbuf
,
1178 unsigned int buflen
)
1185 if (ata_id_has_lba48(args
->id
))
1186 n_sectors
= ata_id_u64(args
->id
, 100);
1188 n_sectors
= ata_id_u32(args
->id
, 60);
1189 n_sectors
--; /* ATA TotalUserSectors - 1 */
1191 if (args
->cmd
->cmnd
[0] == READ_CAPACITY
) {
1192 if( n_sectors
>= 0xffffffffULL
)
1193 tmp
= 0xffffffff ; /* Return max count on overflow */
1197 /* sector count, 32-bit */
1198 rbuf
[0] = tmp
>> (8 * 3);
1199 rbuf
[1] = tmp
>> (8 * 2);
1200 rbuf
[2] = tmp
>> (8 * 1);
1204 tmp
= ATA_SECT_SIZE
;
1209 /* sector count, 64-bit */
1210 tmp
= n_sectors
>> (8 * 4);
1211 rbuf
[2] = tmp
>> (8 * 3);
1212 rbuf
[3] = tmp
>> (8 * 2);
1213 rbuf
[4] = tmp
>> (8 * 1);
1216 rbuf
[6] = tmp
>> (8 * 3);
1217 rbuf
[7] = tmp
>> (8 * 2);
1218 rbuf
[8] = tmp
>> (8 * 1);
1222 tmp
= ATA_SECT_SIZE
;
1223 rbuf
[12] = tmp
>> 8;
1231 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1232 * @args: device IDENTIFY data / SCSI command of interest.
1233 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1234 * @buflen: Response buffer length.
1236 * Simulate REPORT LUNS command.
1239 * spin_lock_irqsave(host_set lock)
1242 unsigned int ata_scsiop_report_luns(struct ata_scsi_args
*args
, u8
*rbuf
,
1243 unsigned int buflen
)
1246 rbuf
[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1252 * ata_scsi_badcmd - End a SCSI request with an error
1253 * @cmd: SCSI request to be handled
1254 * @done: SCSI command completion function
1255 * @asc: SCSI-defined additional sense code
1256 * @ascq: SCSI-defined additional sense code qualifier
1258 * Helper function that completes a SCSI command with
1259 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1260 * and the specified additional sense codes.
1263 * spin_lock_irqsave(host_set lock)
1266 void ata_scsi_badcmd(struct scsi_cmnd
*cmd
, void (*done
)(struct scsi_cmnd
*), u8 asc
, u8 ascq
)
1269 cmd
->result
= SAM_STAT_CHECK_CONDITION
;
1271 cmd
->sense_buffer
[0] = 0x70;
1272 cmd
->sense_buffer
[2] = ILLEGAL_REQUEST
;
1273 cmd
->sense_buffer
[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1274 cmd
->sense_buffer
[12] = asc
;
1275 cmd
->sense_buffer
[13] = ascq
;
1280 static int atapi_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
1282 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
1284 if (unlikely(drv_stat
& (ATA_ERR
| ATA_BUSY
| ATA_DRQ
))) {
1285 DPRINTK("request check condition\n");
1287 cmd
->result
= SAM_STAT_CHECK_CONDITION
;
1293 u8
*scsicmd
= cmd
->cmnd
;
1295 if (scsicmd
[0] == INQUIRY
) {
1297 unsigned int buflen
;
1299 buflen
= ata_scsi_rbuf_get(cmd
, &buf
);
1301 buf
[3] = (buf
[3] & 0xf0) | 2;
1302 ata_scsi_rbuf_put(cmd
, buf
);
1304 cmd
->result
= SAM_STAT_GOOD
;
1312 * atapi_xlat - Initialize PACKET taskfile
1313 * @qc: command structure to be initialized
1314 * @scsicmd: SCSI CDB associated with this PACKET command
1317 * spin_lock_irqsave(host_set lock)
1320 * Zero on success, non-zero on failure.
1323 static unsigned int atapi_xlat(struct ata_queued_cmd
*qc
, u8
*scsicmd
)
1325 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
1326 struct ata_device
*dev
= qc
->dev
;
1327 int using_pio
= (dev
->flags
& ATA_DFLAG_PIO
);
1328 int nodata
= (cmd
->sc_data_direction
== DMA_NONE
);
1331 /* Check whether ATAPI DMA is safe */
1332 if (ata_check_atapi_dma(qc
))
1335 memcpy(&qc
->cdb
, scsicmd
, qc
->ap
->cdb_len
);
1337 qc
->complete_fn
= atapi_qc_complete
;
1339 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1340 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1341 qc
->tf
.flags
|= ATA_TFLAG_WRITE
;
1342 DPRINTK("direction: write\n");
1345 qc
->tf
.command
= ATA_CMD_PACKET
;
1347 /* no data, or PIO data xfer */
1348 if (using_pio
|| nodata
) {
1350 qc
->tf
.protocol
= ATA_PROT_ATAPI_NODATA
;
1352 qc
->tf
.protocol
= ATA_PROT_ATAPI
;
1353 qc
->tf
.lbam
= (8 * 1024) & 0xff;
1354 qc
->tf
.lbah
= (8 * 1024) >> 8;
1359 qc
->tf
.protocol
= ATA_PROT_ATAPI_DMA
;
1360 qc
->tf
.feature
|= ATAPI_PKT_DMA
;
1362 #ifdef ATAPI_ENABLE_DMADIR
1363 /* some SATA bridges need us to indicate data xfer direction */
1364 if (cmd
->sc_data_direction
!= DMA_TO_DEVICE
)
1365 qc
->tf
.feature
|= ATAPI_DMADIR
;
1369 qc
->nbytes
= cmd
->bufflen
;
1375 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1376 * @ap: ATA port to which the device is attached
1377 * @scsidev: SCSI device from which we derive the ATA device
1379 * Given various information provided in struct scsi_cmnd,
1380 * map that onto an ATA bus, and using that mapping
1381 * determine which ata_device is associated with the
1382 * SCSI command to be sent.
1385 * spin_lock_irqsave(host_set lock)
1388 * Associated ATA device, or %NULL if not found.
1391 static struct ata_device
*
1392 ata_scsi_find_dev(struct ata_port
*ap
, struct scsi_device
*scsidev
)
1394 struct ata_device
*dev
;
1396 /* skip commands not addressed to targets we simulate */
1397 if (likely(scsidev
->id
< ATA_MAX_DEVICES
))
1398 dev
= &ap
->device
[scsidev
->id
];
1402 if (unlikely((scsidev
->channel
!= 0) ||
1403 (scsidev
->lun
!= 0)))
1406 if (unlikely(!ata_dev_present(dev
)))
1409 #ifndef ATA_ENABLE_ATAPI
1410 if (unlikely(dev
->class == ATA_DEV_ATAPI
))
1418 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1420 * @cmd: SCSI command opcode to consider
1422 * Look up the SCSI command given, and determine whether the
1423 * SCSI command is to be translated or simulated.
1426 * Pointer to translation function if possible, %NULL if not.
1429 static inline ata_xlat_func_t
ata_get_xlat_func(struct ata_device
*dev
, u8 cmd
)
1439 return ata_scsi_rw_xlat
;
1441 case SYNCHRONIZE_CACHE
:
1442 if (ata_try_flush_cache(dev
))
1443 return ata_scsi_flush_xlat
;
1448 return ata_scsi_verify_xlat
;
1455 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1456 * @ap: ATA port to which the command was being sent
1457 * @cmd: SCSI command to dump
1459 * Prints the contents of a SCSI command via printk().
1462 static inline void ata_scsi_dump_cdb(struct ata_port
*ap
,
1463 struct scsi_cmnd
*cmd
)
1466 struct scsi_device
*scsidev
= cmd
->device
;
1467 u8
*scsicmd
= cmd
->cmnd
;
1469 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1471 scsidev
->channel
, scsidev
->id
, scsidev
->lun
,
1472 scsicmd
[0], scsicmd
[1], scsicmd
[2], scsicmd
[3],
1473 scsicmd
[4], scsicmd
[5], scsicmd
[6], scsicmd
[7],
1479 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1480 * @cmd: SCSI command to be sent
1481 * @done: Completion function, called when command is complete
1483 * In some cases, this function translates SCSI commands into
1484 * ATA taskfiles, and queues the taskfiles to be sent to
1485 * hardware. In other cases, this function simulates a
1486 * SCSI device by evaluating and responding to certain
1487 * SCSI commands. This creates the overall effect of
1488 * ATA and ATAPI devices appearing as SCSI devices.
1491 * Releases scsi-layer-held lock, and obtains host_set lock.
1497 int ata_scsi_queuecmd(struct scsi_cmnd
*cmd
, void (*done
)(struct scsi_cmnd
*))
1499 struct ata_port
*ap
;
1500 struct ata_device
*dev
;
1501 struct scsi_device
*scsidev
= cmd
->device
;
1503 ap
= (struct ata_port
*) &scsidev
->host
->hostdata
[0];
1505 ata_scsi_dump_cdb(ap
, cmd
);
1507 dev
= ata_scsi_find_dev(ap
, scsidev
);
1508 if (unlikely(!dev
)) {
1509 cmd
->result
= (DID_BAD_TARGET
<< 16);
1514 if (dev
->class == ATA_DEV_ATA
) {
1515 ata_xlat_func_t xlat_func
= ata_get_xlat_func(dev
,
1519 ata_scsi_translate(ap
, dev
, cmd
, done
, xlat_func
);
1521 ata_scsi_simulate(dev
->id
, cmd
, done
);
1523 ata_scsi_translate(ap
, dev
, cmd
, done
, atapi_xlat
);
1530 * ata_scsi_simulate - simulate SCSI command on ATA device
1531 * @id: current IDENTIFY data for target device.
1532 * @cmd: SCSI command being sent to device.
1533 * @done: SCSI command completion function.
1535 * Interprets and directly executes a select list of SCSI commands
1536 * that can be handled internally.
1539 * spin_lock_irqsave(host_set lock)
1542 void ata_scsi_simulate(u16
*id
,
1543 struct scsi_cmnd
*cmd
,
1544 void (*done
)(struct scsi_cmnd
*))
1546 struct ata_scsi_args args
;
1547 u8
*scsicmd
= cmd
->cmnd
;
1553 switch(scsicmd
[0]) {
1554 /* no-op's, complete with success */
1555 case SYNCHRONIZE_CACHE
:
1559 case TEST_UNIT_READY
:
1560 case FORMAT_UNIT
: /* FIXME: correct? */
1561 case SEND_DIAGNOSTIC
: /* FIXME: correct? */
1562 ata_scsi_rbuf_fill(&args
, ata_scsiop_noop
);
1566 if (scsicmd
[1] & 2) /* is CmdDt set? */
1567 ata_bad_cdb(cmd
, done
);
1568 else if ((scsicmd
[1] & 1) == 0) /* is EVPD clear? */
1569 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_std
);
1570 else if (scsicmd
[2] == 0x00)
1571 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_00
);
1572 else if (scsicmd
[2] == 0x80)
1573 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_80
);
1574 else if (scsicmd
[2] == 0x83)
1575 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_83
);
1577 ata_bad_cdb(cmd
, done
);
1582 ata_scsi_rbuf_fill(&args
, ata_scsiop_mode_sense
);
1585 case MODE_SELECT
: /* unconditionally return */
1586 case MODE_SELECT_10
: /* bad-field-in-cdb */
1587 ata_bad_cdb(cmd
, done
);
1591 ata_scsi_rbuf_fill(&args
, ata_scsiop_read_cap
);
1594 case SERVICE_ACTION_IN
:
1595 if ((scsicmd
[1] & 0x1f) == SAI_READ_CAPACITY_16
)
1596 ata_scsi_rbuf_fill(&args
, ata_scsiop_read_cap
);
1598 ata_bad_cdb(cmd
, done
);
1602 ata_scsi_rbuf_fill(&args
, ata_scsiop_report_luns
);
1605 /* mandantory commands we haven't implemented yet */
1608 /* all other commands */
1610 ata_bad_scsiop(cmd
, done
);