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
, const u8
*scsicmd
);
48 static struct ata_device
*
49 ata_scsi_find_dev(struct ata_port
*ap
, const struct scsi_device
*scsidev
);
52 static void ata_scsi_invalid_field(struct scsi_cmnd
*cmd
,
53 void (*done
)(struct scsi_cmnd
*))
55 ata_scsi_set_sense(cmd
, ILLEGAL_REQUEST
, 0x24, 0x0);
56 /* "Invalid field in cbd" */
61 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
62 * @sdev: SCSI device for which BIOS geometry is to be determined
63 * @bdev: block device associated with @sdev
64 * @capacity: capacity of SCSI device
65 * @geom: location to which geometry will be output
67 * Generic bios head/sector/cylinder calculator
68 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
69 * mapping. Some situations may arise where the disk is not
70 * bootable if this is not used.
73 * Defined by the SCSI layer. We don't really care.
78 int ata_std_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
79 sector_t capacity
, int geom
[])
83 sector_div(capacity
, 255*63);
89 int ata_scsi_ioctl(struct scsi_device
*scsidev
, int cmd
, void __user
*arg
)
92 struct ata_device
*dev
;
93 int val
= -EINVAL
, rc
= -EINVAL
;
95 ap
= (struct ata_port
*) &scsidev
->host
->hostdata
[0];
99 dev
= ata_scsi_find_dev(ap
, scsidev
);
106 case ATA_IOC_GET_IO32
:
108 if (copy_to_user(arg
, &val
, 1))
112 case ATA_IOC_SET_IO32
:
113 val
= (unsigned long) arg
;
128 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
129 * @ap: ATA port to which the new command is attached
130 * @dev: ATA device to which the new command is attached
131 * @cmd: SCSI command that originated this ATA command
132 * @done: SCSI command completion function
134 * Obtain a reference to an unused ata_queued_cmd structure,
135 * which is the basic libata structure representing a single
136 * ATA command sent to the hardware.
138 * If a command was available, fill in the SCSI-specific
139 * portions of the structure with information on the
143 * spin_lock_irqsave(host_set lock)
146 * Command allocated, or %NULL if none available.
148 struct ata_queued_cmd
*ata_scsi_qc_new(struct ata_port
*ap
,
149 struct ata_device
*dev
,
150 struct scsi_cmnd
*cmd
,
151 void (*done
)(struct scsi_cmnd
*))
153 struct ata_queued_cmd
*qc
;
155 qc
= ata_qc_new_init(ap
, dev
);
161 qc
->sg
= (struct scatterlist
*) cmd
->request_buffer
;
162 qc
->n_elem
= cmd
->use_sg
;
168 cmd
->result
= (DID_OK
<< 16) | (QUEUE_FULL
<< 1);
176 * ata_to_sense_error - convert ATA error to SCSI error
177 * @qc: Command that we are erroring out
178 * @drv_stat: value contained in ATA status register
180 * Converts an ATA error into a SCSI error. While we are at it
181 * we decode and dump the ATA error for the user so that they
182 * have some idea what really happened at the non make-believe
186 * spin_lock_irqsave(host_set lock)
189 void ata_to_sense_error(struct ata_queued_cmd
*qc
, u8 drv_stat
)
191 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
193 /* Based on the 3ware driver translation table */
194 static unsigned char sense_table
[][4] = {
196 {0xd1, ABORTED_COMMAND
, 0x00, 0x00}, // Device busy Aborted command
198 {0xd0, ABORTED_COMMAND
, 0x00, 0x00}, // Device busy Aborted command
200 {0x61, HARDWARE_ERROR
, 0x00, 0x00}, // Device fault Hardware error
201 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
202 {0x84, ABORTED_COMMAND
, 0x47, 0x00}, // Data CRC error SCSI parity error
203 /* MC|ID|ABRT|TRK0|MARK */
204 {0x37, NOT_READY
, 0x04, 0x00}, // Unit offline Not ready
206 {0x09, NOT_READY
, 0x04, 0x00}, // Unrecovered disk error Not ready
207 /* Bad address mark */
208 {0x01, MEDIUM_ERROR
, 0x13, 0x00}, // Address mark not found Address mark not found for data field
210 {0x02, HARDWARE_ERROR
, 0x00, 0x00}, // Track 0 not found Hardware error
212 {0x04, ABORTED_COMMAND
, 0x00, 0x00}, // Aborted command Aborted command
213 /* Media change request */
214 {0x08, NOT_READY
, 0x04, 0x00}, // Media change request FIXME: faking offline
216 {0x10, ABORTED_COMMAND
, 0x14, 0x00}, // ID not found Recorded entity not found
218 {0x08, NOT_READY
, 0x04, 0x00}, // Media change FIXME: faking offline
220 {0x40, MEDIUM_ERROR
, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
221 /* BBD - block marked bad */
222 {0x80, MEDIUM_ERROR
, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
223 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
225 static unsigned char stat_table
[][4] = {
226 /* Must be first because BUSY means no other bits valid */
227 {0x80, ABORTED_COMMAND
, 0x47, 0x00}, // Busy, fake parity for now
228 {0x20, HARDWARE_ERROR
, 0x00, 0x00}, // Device fault
229 {0x08, ABORTED_COMMAND
, 0x47, 0x00}, // Timed out in xfer, fake parity for now
230 {0x04, RECOVERED_ERROR
, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
231 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
236 * Is this an error we can process/parse
239 if(drv_stat
& ATA_ERR
)
240 /* Read the err bits */
241 err
= ata_chk_err(qc
->ap
);
243 /* Display the ATA level error info */
245 printk(KERN_WARNING
"ata%u: status=0x%02x { ", qc
->ap
->id
, drv_stat
);
249 err
= 0; /* Data is not valid in this case */
252 if(drv_stat
& 0x40) printk("DriveReady ");
253 if(drv_stat
& 0x20) printk("DeviceFault ");
254 if(drv_stat
& 0x10) printk("SeekComplete ");
255 if(drv_stat
& 0x08) printk("DataRequest ");
256 if(drv_stat
& 0x04) printk("CorrectedError ");
257 if(drv_stat
& 0x02) printk("Index ");
258 if(drv_stat
& 0x01) printk("Error ");
264 printk(KERN_WARNING
"ata%u: error=0x%02x { ", qc
->ap
->id
, err
);
265 if(err
& 0x04) printk("DriveStatusError ");
273 if(err
& 0x40) printk("UncorrectableError ");
274 if(err
& 0x10) printk("SectorIdNotFound ");
275 if(err
& 0x02) printk("TrackZeroNotFound ");
276 if(err
& 0x01) printk("AddrMarkNotFound ");
279 /* Should we dump sector info here too ?? */
284 while(sense_table
[i
][0] != 0xFF)
286 /* Look for best matches first */
287 if((sense_table
[i
][0] & err
) == sense_table
[i
][0])
289 ata_scsi_set_sense(cmd
, sense_table
[i
][1] /* sk */,
290 sense_table
[i
][2] /* asc */,
291 sense_table
[i
][3] /* ascq */ );
296 /* No immediate match */
298 printk(KERN_DEBUG
"ata%u: no sense translation for 0x%02x\n", qc
->ap
->id
, err
);
301 /* Fall back to interpreting status bits */
302 while(stat_table
[i
][0] != 0xFF)
304 if(stat_table
[i
][0] & drv_stat
)
306 ata_scsi_set_sense(cmd
, sense_table
[i
][1] /* sk */,
307 sense_table
[i
][2] /* asc */,
308 sense_table
[i
][3] /* ascq */ );
314 printk(KERN_ERR
"ata%u: called with no error (%02X)!\n", qc
->ap
->id
, drv_stat
);
315 /* additional-sense-code[-qualifier] */
317 if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
) {
318 ata_scsi_set_sense(cmd
, MEDIUM_ERROR
, 0x11, 0x4);
319 /* "unrecovered read error" */
321 ata_scsi_set_sense(cmd
, MEDIUM_ERROR
, 0xc, 0x2);
322 /* "write error - auto-reallocation failed" */
327 * ata_scsi_slave_config - Set SCSI device attributes
328 * @sdev: SCSI device to examine
330 * This is called before we actually start reading
331 * and writing to the device, to configure certain
332 * SCSI mid-layer behaviors.
335 * Defined by SCSI layer. We don't really care.
338 int ata_scsi_slave_config(struct scsi_device
*sdev
)
340 sdev
->use_10_for_rw
= 1;
341 sdev
->use_10_for_ms
= 1;
343 blk_queue_max_phys_segments(sdev
->request_queue
, LIBATA_MAX_PRD
);
345 if (sdev
->id
< ATA_MAX_DEVICES
) {
347 struct ata_device
*dev
;
349 ap
= (struct ata_port
*) &sdev
->host
->hostdata
[0];
350 dev
= &ap
->device
[sdev
->id
];
352 /* TODO: 1024 is an arbitrary number, not the
353 * hardware maximum. This should be increased to
354 * 65534 when Jens Axboe's patch for dynamically
355 * determining max_sectors is merged.
357 if ((dev
->flags
& ATA_DFLAG_LBA48
) &&
358 ((dev
->flags
& ATA_DFLAG_LOCK_SECTORS
) == 0)) {
360 * do not overwrite sdev->host->max_sectors, since
361 * other drives on this host may not support LBA48
363 blk_queue_max_sectors(sdev
->request_queue
, 2048);
367 return 0; /* scsi layer doesn't check return value, sigh */
371 * ata_scsi_error - SCSI layer error handler callback
372 * @host: SCSI host on which error occurred
374 * Handles SCSI-layer-thrown error events.
377 * Inherited from SCSI layer (none, can sleep)
383 int ata_scsi_error(struct Scsi_Host
*host
)
389 ap
= (struct ata_port
*) &host
->hostdata
[0];
390 ap
->ops
->eng_timeout(ap
);
392 /* TODO: this is per-command; when queueing is supported
393 * this code will either change or move to a more
397 INIT_LIST_HEAD(&host
->eh_cmd_q
);
404 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
405 * @qc: Storage for translated ATA taskfile
406 * @scsicmd: SCSI command to translate
408 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
409 * (to start). Perhaps these commands should be preceded by
410 * CHECK POWER MODE to see what power mode the device is already in.
411 * [See SAT revision 5 at www.t10.org]
414 * spin_lock_irqsave(host_set lock)
417 * Zero on success, non-zero on error.
420 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd
*qc
,
423 struct ata_taskfile
*tf
= &qc
->tf
;
425 tf
->flags
|= ATA_TFLAG_DEVICE
| ATA_TFLAG_ISADDR
;
426 tf
->protocol
= ATA_PROT_NODATA
;
427 if (scsicmd
[1] & 0x1) {
428 ; /* ignore IMMED bit, violates sat-r05 */
430 if (scsicmd
[4] & 0x2)
431 goto invalid_fld
; /* LOEJ bit set not supported */
432 if (((scsicmd
[4] >> 4) & 0xf) != 0)
433 goto invalid_fld
; /* power conditions not supported */
434 if (scsicmd
[4] & 0x1) {
435 tf
->nsect
= 1; /* 1 sector, lba=0 */
437 if (qc
->dev
->flags
& ATA_DFLAG_LBA
) {
438 qc
->tf
.flags
|= ATA_TFLAG_LBA
;
443 tf
->device
|= ATA_LBA
;
446 tf
->lbal
= 0x1; /* sect */
447 tf
->lbam
= 0x0; /* cyl low */
448 tf
->lbah
= 0x0; /* cyl high */
451 tf
->command
= ATA_CMD_VERIFY
; /* READ VERIFY */
453 tf
->nsect
= 0; /* time period value (0 implies now) */
454 tf
->command
= ATA_CMD_STANDBY
;
455 /* Consider: ATA STANDBY IMMEDIATE command */
458 * Standby and Idle condition timers could be implemented but that
459 * would require libata to implement the Power condition mode page
460 * and allow the user to change it. Changing mode pages requires
461 * MODE SELECT to be implemented.
467 ata_scsi_set_sense(qc
->scsicmd
, ILLEGAL_REQUEST
, 0x24, 0x0);
468 /* "Invalid field in cbd" */
474 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
475 * @qc: Storage for translated ATA taskfile
476 * @scsicmd: SCSI command to translate (ignored)
478 * Sets up an ATA taskfile to issue FLUSH CACHE or
482 * spin_lock_irqsave(host_set lock)
485 * Zero on success, non-zero on error.
488 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd
*qc
, const u8
*scsicmd
)
490 struct ata_taskfile
*tf
= &qc
->tf
;
492 tf
->flags
|= ATA_TFLAG_DEVICE
;
493 tf
->protocol
= ATA_PROT_NODATA
;
495 if ((qc
->dev
->flags
& ATA_DFLAG_LBA48
) &&
496 (ata_id_has_flush_ext(qc
->dev
->id
)))
497 tf
->command
= ATA_CMD_FLUSH_EXT
;
499 tf
->command
= ATA_CMD_FLUSH
;
505 * scsi_6_lba_len - Get LBA and transfer length
506 * @scsicmd: SCSI command to translate
508 * Calculate LBA and transfer length for 6-byte commands.
512 * @plen: the transfer length
515 static void scsi_6_lba_len(const u8
*scsicmd
, u64
*plba
, u32
*plen
)
520 VPRINTK("six-byte command\n");
522 lba
|= ((u64
)scsicmd
[2]) << 8;
523 lba
|= ((u64
)scsicmd
[3]);
525 len
|= ((u32
)scsicmd
[4]);
532 * scsi_10_lba_len - Get LBA and transfer length
533 * @scsicmd: SCSI command to translate
535 * Calculate LBA and transfer length for 10-byte commands.
539 * @plen: the transfer length
542 static void scsi_10_lba_len(const u8
*scsicmd
, u64
*plba
, u32
*plen
)
547 VPRINTK("ten-byte command\n");
549 lba
|= ((u64
)scsicmd
[2]) << 24;
550 lba
|= ((u64
)scsicmd
[3]) << 16;
551 lba
|= ((u64
)scsicmd
[4]) << 8;
552 lba
|= ((u64
)scsicmd
[5]);
554 len
|= ((u32
)scsicmd
[7]) << 8;
555 len
|= ((u32
)scsicmd
[8]);
562 * scsi_16_lba_len - Get LBA and transfer length
563 * @scsicmd: SCSI command to translate
565 * Calculate LBA and transfer length for 16-byte commands.
569 * @plen: the transfer length
572 static void scsi_16_lba_len(const u8
*scsicmd
, u64
*plba
, u32
*plen
)
577 VPRINTK("sixteen-byte command\n");
579 lba
|= ((u64
)scsicmd
[2]) << 56;
580 lba
|= ((u64
)scsicmd
[3]) << 48;
581 lba
|= ((u64
)scsicmd
[4]) << 40;
582 lba
|= ((u64
)scsicmd
[5]) << 32;
583 lba
|= ((u64
)scsicmd
[6]) << 24;
584 lba
|= ((u64
)scsicmd
[7]) << 16;
585 lba
|= ((u64
)scsicmd
[8]) << 8;
586 lba
|= ((u64
)scsicmd
[9]);
588 len
|= ((u32
)scsicmd
[10]) << 24;
589 len
|= ((u32
)scsicmd
[11]) << 16;
590 len
|= ((u32
)scsicmd
[12]) << 8;
591 len
|= ((u32
)scsicmd
[13]);
598 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
599 * @qc: Storage for translated ATA taskfile
600 * @scsicmd: SCSI command to translate
602 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
605 * spin_lock_irqsave(host_set lock)
608 * Zero on success, non-zero on error.
611 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd
*qc
, const u8
*scsicmd
)
613 struct ata_taskfile
*tf
= &qc
->tf
;
614 struct ata_device
*dev
= qc
->dev
;
615 u64 dev_sectors
= qc
->dev
->n_sectors
;
619 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
620 tf
->protocol
= ATA_PROT_NODATA
;
622 if (scsicmd
[0] == VERIFY
)
623 scsi_10_lba_len(scsicmd
, &block
, &n_block
);
624 else if (scsicmd
[0] == VERIFY_16
)
625 scsi_16_lba_len(scsicmd
, &block
, &n_block
);
631 if (block
>= dev_sectors
)
633 if ((block
+ n_block
) > dev_sectors
)
636 if (dev
->flags
& ATA_DFLAG_LBA
) {
637 tf
->flags
|= ATA_TFLAG_LBA
;
639 if (dev
->flags
& ATA_DFLAG_LBA48
) {
640 if (n_block
> (64 * 1024))
644 tf
->flags
|= ATA_TFLAG_LBA48
;
645 tf
->command
= ATA_CMD_VERIFY_EXT
;
647 tf
->hob_nsect
= (n_block
>> 8) & 0xff;
649 tf
->hob_lbah
= (block
>> 40) & 0xff;
650 tf
->hob_lbam
= (block
>> 32) & 0xff;
651 tf
->hob_lbal
= (block
>> 24) & 0xff;
657 tf
->command
= ATA_CMD_VERIFY
;
659 tf
->device
|= (block
>> 24) & 0xf;
662 tf
->nsect
= n_block
& 0xff;
664 tf
->lbah
= (block
>> 16) & 0xff;
665 tf
->lbam
= (block
>> 8) & 0xff;
666 tf
->lbal
= block
& 0xff;
668 tf
->device
|= ATA_LBA
;
671 u32 sect
, head
, cyl
, track
;
676 /* Convert LBA to CHS */
677 track
= (u32
)block
/ dev
->sectors
;
678 cyl
= track
/ dev
->heads
;
679 head
= track
% dev
->heads
;
680 sect
= (u32
)block
% dev
->sectors
+ 1;
682 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
683 (u32
)block
, track
, cyl
, head
, sect
);
685 /* Check whether the converted CHS can fit.
689 if ((cyl
>> 16) || (head
>> 4) || (sect
>> 8) || (!sect
))
692 tf
->command
= ATA_CMD_VERIFY
;
693 tf
->nsect
= n_block
& 0xff; /* Sector count 0 means 256 sectors */
703 ata_scsi_set_sense(qc
->scsicmd
, ILLEGAL_REQUEST
, 0x24, 0x0);
704 /* "Invalid field in cbd" */
708 ata_scsi_set_sense(qc
->scsicmd
, ILLEGAL_REQUEST
, 0x21, 0x0);
709 /* "Logical Block Address out of range" */
713 qc
->scsicmd
->result
= SAM_STAT_GOOD
;
718 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
719 * @qc: Storage for translated ATA taskfile
720 * @scsicmd: SCSI command to translate
722 * Converts any of six SCSI read/write commands into the
723 * ATA counterpart, including starting sector (LBA),
724 * sector count, and taking into account the device's LBA48
727 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
728 * %WRITE_16 are currently supported.
731 * spin_lock_irqsave(host_set lock)
734 * Zero on success, non-zero on error.
737 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd
*qc
, const u8
*scsicmd
)
739 struct ata_taskfile
*tf
= &qc
->tf
;
740 struct ata_device
*dev
= qc
->dev
;
744 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
746 if (scsicmd
[0] == WRITE_10
|| scsicmd
[0] == WRITE_6
||
747 scsicmd
[0] == WRITE_16
)
748 tf
->flags
|= ATA_TFLAG_WRITE
;
750 /* Calculate the SCSI LBA and transfer length. */
751 switch (scsicmd
[0]) {
754 scsi_10_lba_len(scsicmd
, &block
, &n_block
);
758 scsi_6_lba_len(scsicmd
, &block
, &n_block
);
760 /* for 6-byte r/w commands, transfer length 0
761 * means 256 blocks of data, not 0 block.
768 scsi_16_lba_len(scsicmd
, &block
, &n_block
);
771 DPRINTK("no-byte command\n");
775 /* Check and compose ATA command */
777 /* For 10-byte and 16-byte SCSI R/W commands, transfer
778 * length 0 means transfer 0 block of data.
779 * However, for ATA R/W commands, sector count 0 means
780 * 256 or 65536 sectors, not 0 sectors as in SCSI.
784 if (dev
->flags
& ATA_DFLAG_LBA
) {
785 tf
->flags
|= ATA_TFLAG_LBA
;
787 if (dev
->flags
& ATA_DFLAG_LBA48
) {
788 /* The request -may- be too large for LBA48. */
789 if ((block
>> 48) || (n_block
> 65536))
793 tf
->flags
|= ATA_TFLAG_LBA48
;
795 tf
->hob_nsect
= (n_block
>> 8) & 0xff;
797 tf
->hob_lbah
= (block
>> 40) & 0xff;
798 tf
->hob_lbam
= (block
>> 32) & 0xff;
799 tf
->hob_lbal
= (block
>> 24) & 0xff;
803 /* The request -may- be too large for LBA28. */
804 if ((block
>> 28) || (n_block
> 256))
807 tf
->device
|= (block
>> 24) & 0xf;
810 ata_rwcmd_protocol(qc
);
813 tf
->nsect
= n_block
& 0xff;
815 tf
->lbah
= (block
>> 16) & 0xff;
816 tf
->lbam
= (block
>> 8) & 0xff;
817 tf
->lbal
= block
& 0xff;
819 tf
->device
|= ATA_LBA
;
822 u32 sect
, head
, cyl
, track
;
824 /* The request -may- be too large for CHS addressing. */
825 if ((block
>> 28) || (n_block
> 256))
828 ata_rwcmd_protocol(qc
);
830 /* Convert LBA to CHS */
831 track
= (u32
)block
/ dev
->sectors
;
832 cyl
= track
/ dev
->heads
;
833 head
= track
% dev
->heads
;
834 sect
= (u32
)block
% dev
->sectors
+ 1;
836 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
837 (u32
)block
, track
, cyl
, head
, sect
);
839 /* Check whether the converted CHS can fit.
843 if ((cyl
>> 16) || (head
>> 4) || (sect
>> 8) || (!sect
))
847 tf
->nsect
= n_block
& 0xff; /* Sector count 0 means 256 sectors */
857 ata_scsi_set_sense(qc
->scsicmd
, ILLEGAL_REQUEST
, 0x24, 0x0);
858 /* "Invalid field in cbd" */
862 ata_scsi_set_sense(qc
->scsicmd
, ILLEGAL_REQUEST
, 0x21, 0x0);
863 /* "Logical Block Address out of range" */
867 qc
->scsicmd
->result
= SAM_STAT_GOOD
;
871 static int ata_scsi_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
873 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
875 if (unlikely(drv_stat
& (ATA_ERR
| ATA_BUSY
| ATA_DRQ
)))
876 ata_to_sense_error(qc
, drv_stat
);
878 cmd
->result
= SAM_STAT_GOOD
;
886 * ata_scsi_translate - Translate then issue SCSI command to ATA device
887 * @ap: ATA port to which the command is addressed
888 * @dev: ATA device to which the command is addressed
889 * @cmd: SCSI command to execute
890 * @done: SCSI command completion function
891 * @xlat_func: Actor which translates @cmd to an ATA taskfile
893 * Our ->queuecommand() function has decided that the SCSI
894 * command issued can be directly translated into an ATA
895 * command, rather than handled internally.
897 * This function sets up an ata_queued_cmd structure for the
898 * SCSI command, and sends that ata_queued_cmd to the hardware.
900 * The xlat_func argument (actor) returns 0 if ready to execute
901 * ATA command, else 1 to finish translation. If 1 is returned
902 * then cmd->result (and possibly cmd->sense_buffer) are assumed
903 * to be set reflecting an error condition or clean (early)
907 * spin_lock_irqsave(host_set lock)
910 static void ata_scsi_translate(struct ata_port
*ap
, struct ata_device
*dev
,
911 struct scsi_cmnd
*cmd
,
912 void (*done
)(struct scsi_cmnd
*),
913 ata_xlat_func_t xlat_func
)
915 struct ata_queued_cmd
*qc
;
916 u8
*scsicmd
= cmd
->cmnd
;
920 qc
= ata_scsi_qc_new(ap
, dev
, cmd
, done
);
924 /* data is present; dma-map it */
925 if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
||
926 cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
927 if (unlikely(cmd
->request_bufflen
< 1)) {
928 printk(KERN_WARNING
"ata%u(%u): WARNING: zero len r/w req\n",
934 ata_sg_init(qc
, cmd
->request_buffer
, cmd
->use_sg
);
936 ata_sg_init_one(qc
, cmd
->request_buffer
,
937 cmd
->request_bufflen
);
939 qc
->dma_dir
= cmd
->sc_data_direction
;
942 qc
->complete_fn
= ata_scsi_qc_complete
;
944 if (xlat_func(qc
, scsicmd
))
947 /* select device, send command to hardware */
948 if (ata_qc_issue(qc
))
957 DPRINTK("EXIT - early finish (good or error)\n");
963 cmd
->result
= (DID_ERROR
<< 16);
965 DPRINTK("EXIT - internal\n");
970 * ata_scsi_rbuf_get - Map response buffer.
971 * @cmd: SCSI command containing buffer to be mapped.
972 * @buf_out: Pointer to mapped area.
974 * Maps buffer contained within SCSI command @cmd.
977 * spin_lock_irqsave(host_set lock)
980 * Length of response buffer.
983 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd
*cmd
, u8
**buf_out
)
989 struct scatterlist
*sg
;
991 sg
= (struct scatterlist
*) cmd
->request_buffer
;
992 buf
= kmap_atomic(sg
->page
, KM_USER0
) + sg
->offset
;
995 buf
= cmd
->request_buffer
;
996 buflen
= cmd
->request_bufflen
;
1004 * ata_scsi_rbuf_put - Unmap response buffer.
1005 * @cmd: SCSI command containing buffer to be unmapped.
1006 * @buf: buffer to unmap
1008 * Unmaps response buffer contained within @cmd.
1011 * spin_lock_irqsave(host_set lock)
1014 static inline void ata_scsi_rbuf_put(struct scsi_cmnd
*cmd
, u8
*buf
)
1017 struct scatterlist
*sg
;
1019 sg
= (struct scatterlist
*) cmd
->request_buffer
;
1020 kunmap_atomic(buf
- sg
->offset
, KM_USER0
);
1025 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1026 * @args: device IDENTIFY data / SCSI command of interest.
1027 * @actor: Callback hook for desired SCSI command simulator
1029 * Takes care of the hard work of simulating a SCSI command...
1030 * Mapping the response buffer, calling the command's handler,
1031 * and handling the handler's return value. This return value
1032 * indicates whether the handler wishes the SCSI command to be
1033 * completed successfully (0), or not (in which case cmd->result
1034 * and sense buffer are assumed to be set).
1037 * spin_lock_irqsave(host_set lock)
1040 void ata_scsi_rbuf_fill(struct ata_scsi_args
*args
,
1041 unsigned int (*actor
) (struct ata_scsi_args
*args
,
1042 u8
*rbuf
, unsigned int buflen
))
1045 unsigned int buflen
, rc
;
1046 struct scsi_cmnd
*cmd
= args
->cmd
;
1048 buflen
= ata_scsi_rbuf_get(cmd
, &rbuf
);
1049 memset(rbuf
, 0, buflen
);
1050 rc
= actor(args
, rbuf
, buflen
);
1051 ata_scsi_rbuf_put(cmd
, rbuf
);
1054 cmd
->result
= SAM_STAT_GOOD
;
1059 * ata_scsiop_inq_std - Simulate INQUIRY command
1060 * @args: device IDENTIFY data / SCSI command of interest.
1061 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1062 * @buflen: Response buffer length.
1064 * Returns standard device identification data associated
1065 * with non-EVPD INQUIRY command output.
1068 * spin_lock_irqsave(host_set lock)
1071 unsigned int ata_scsiop_inq_std(struct ata_scsi_args
*args
, u8
*rbuf
,
1072 unsigned int buflen
)
1077 0x5, /* claim SPC-3 version compatibility */
1082 /* set scsi removeable (RMB) bit per ata bit */
1083 if (ata_id_removeable(args
->id
))
1088 memcpy(rbuf
, hdr
, sizeof(hdr
));
1091 memcpy(&rbuf
[8], "ATA ", 8);
1092 ata_dev_id_string(args
->id
, &rbuf
[16], ATA_ID_PROD_OFS
, 16);
1093 ata_dev_id_string(args
->id
, &rbuf
[32], ATA_ID_FW_REV_OFS
, 4);
1094 if (rbuf
[32] == 0 || rbuf
[32] == ' ')
1095 memcpy(&rbuf
[32], "n/a ", 4);
1099 const u8 versions
[] = {
1100 0x60, /* SAM-3 (no version claimed) */
1103 0x20, /* SBC-2 (no version claimed) */
1106 0x60 /* SPC-3 (no version claimed) */
1109 memcpy(rbuf
+ 59, versions
, sizeof(versions
));
1116 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1117 * @args: device IDENTIFY data / SCSI command of interest.
1118 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1119 * @buflen: Response buffer length.
1121 * Returns list of inquiry EVPD pages available.
1124 * spin_lock_irqsave(host_set lock)
1127 unsigned int ata_scsiop_inq_00(struct ata_scsi_args
*args
, u8
*rbuf
,
1128 unsigned int buflen
)
1130 const u8 pages
[] = {
1131 0x00, /* page 0x00, this page */
1132 0x80, /* page 0x80, unit serial no page */
1133 0x83 /* page 0x83, device ident page */
1135 rbuf
[3] = sizeof(pages
); /* number of supported EVPD pages */
1138 memcpy(rbuf
+ 4, pages
, sizeof(pages
));
1144 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1145 * @args: device IDENTIFY data / SCSI command of interest.
1146 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1147 * @buflen: Response buffer length.
1149 * Returns ATA device serial number.
1152 * spin_lock_irqsave(host_set lock)
1155 unsigned int ata_scsiop_inq_80(struct ata_scsi_args
*args
, u8
*rbuf
,
1156 unsigned int buflen
)
1160 0x80, /* this page code */
1162 ATA_SERNO_LEN
, /* page len */
1164 memcpy(rbuf
, hdr
, sizeof(hdr
));
1166 if (buflen
> (ATA_SERNO_LEN
+ 4 - 1))
1167 ata_dev_id_string(args
->id
, (unsigned char *) &rbuf
[4],
1168 ATA_ID_SERNO_OFS
, ATA_SERNO_LEN
);
1173 static const char *inq_83_str
= "Linux ATA-SCSI simulator";
1176 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1177 * @args: device IDENTIFY data / SCSI command of interest.
1178 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1179 * @buflen: Response buffer length.
1181 * Returns device identification. Currently hardcoded to
1182 * return "Linux ATA-SCSI simulator".
1185 * spin_lock_irqsave(host_set lock)
1188 unsigned int ata_scsiop_inq_83(struct ata_scsi_args
*args
, u8
*rbuf
,
1189 unsigned int buflen
)
1191 rbuf
[1] = 0x83; /* this page code */
1192 rbuf
[3] = 4 + strlen(inq_83_str
); /* page len */
1194 /* our one and only identification descriptor (vendor-specific) */
1195 if (buflen
> (strlen(inq_83_str
) + 4 + 4 - 1)) {
1196 rbuf
[4 + 0] = 2; /* code set: ASCII */
1197 rbuf
[4 + 3] = strlen(inq_83_str
);
1198 memcpy(rbuf
+ 4 + 4, inq_83_str
, strlen(inq_83_str
));
1205 * ata_scsiop_noop - Command handler that simply returns success.
1206 * @args: device IDENTIFY data / SCSI command of interest.
1207 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1208 * @buflen: Response buffer length.
1210 * No operation. Simply returns success to caller, to indicate
1211 * that the caller should successfully complete this SCSI command.
1214 * spin_lock_irqsave(host_set lock)
1217 unsigned int ata_scsiop_noop(struct ata_scsi_args
*args
, u8
*rbuf
,
1218 unsigned int buflen
)
1225 * ata_msense_push - Push data onto MODE SENSE data output buffer
1226 * @ptr_io: (input/output) Location to store more output data
1227 * @last: End of output data buffer
1228 * @buf: Pointer to BLOB being added to output buffer
1229 * @buflen: Length of BLOB
1231 * Store MODE SENSE data on an output buffer.
1237 static void ata_msense_push(u8
**ptr_io
, const u8
*last
,
1238 const u8
*buf
, unsigned int buflen
)
1242 if ((ptr
+ buflen
- 1) > last
)
1245 memcpy(ptr
, buf
, buflen
);
1253 * ata_msense_caching - Simulate MODE SENSE caching info page
1254 * @id: device IDENTIFY data
1255 * @ptr_io: (input/output) Location to store more output data
1256 * @last: End of output data buffer
1258 * Generate a caching info page, which conditionally indicates
1259 * write caching to the SCSI layer, depending on device
1266 static unsigned int ata_msense_caching(u16
*id
, u8
**ptr_io
,
1270 0x8, /* page code */
1271 0x12, /* page length */
1272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1273 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1276 if (ata_id_wcache_enabled(id
))
1277 page
[2] |= (1 << 2); /* write cache enable */
1278 if (!ata_id_rahead_enabled(id
))
1279 page
[12] |= (1 << 5); /* disable read ahead */
1281 ata_msense_push(ptr_io
, last
, page
, sizeof(page
));
1282 return sizeof(page
);
1286 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1287 * @dev: Device associated with this MODE SENSE command
1288 * @ptr_io: (input/output) Location to store more output data
1289 * @last: End of output data buffer
1291 * Generate a generic MODE SENSE control mode page.
1297 static unsigned int ata_msense_ctl_mode(u8
**ptr_io
, const u8
*last
)
1299 const u8 page
[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1301 /* byte 2: set the descriptor format sense data bit (bit 2)
1302 * since we need to support returning this format for SAT
1303 * commands and any SCSI commands against a 48b LBA device.
1306 ata_msense_push(ptr_io
, last
, page
, sizeof(page
));
1307 return sizeof(page
);
1311 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1312 * @dev: Device associated with this MODE SENSE command
1313 * @ptr_io: (input/output) Location to store more output data
1314 * @last: End of output data buffer
1316 * Generate a generic MODE SENSE r/w error recovery page.
1322 static unsigned int ata_msense_rw_recovery(u8
**ptr_io
, const u8
*last
)
1325 0x1, /* page code */
1326 0xa, /* page length */
1327 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1328 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1331 ata_msense_push(ptr_io
, last
, page
, sizeof(page
));
1332 return sizeof(page
);
1336 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1337 * @args: device IDENTIFY data / SCSI command of interest.
1338 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1339 * @buflen: Response buffer length.
1341 * Simulate MODE SENSE commands.
1344 * spin_lock_irqsave(host_set lock)
1347 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args
*args
, u8
*rbuf
,
1348 unsigned int buflen
)
1350 u8
*scsicmd
= args
->cmd
->cmnd
, *p
, *last
;
1351 unsigned int page_control
, six_byte
, output_len
;
1355 six_byte
= (scsicmd
[0] == MODE_SENSE
);
1357 /* we only support saved and current values (which we treat
1358 * in the same manner)
1360 page_control
= scsicmd
[2] >> 6;
1361 switch (page_control
) {
1362 case 0: /* current */
1363 break; /* supported */
1365 goto saving_not_supp
;
1366 case 1: /* changeable */
1367 case 2: /* defaults */
1377 p
= rbuf
+ output_len
;
1378 last
= rbuf
+ buflen
- 1;
1380 switch(scsicmd
[2] & 0x3f) {
1381 case 0x01: /* r/w error recovery */
1382 output_len
+= ata_msense_rw_recovery(&p
, last
);
1385 case 0x08: /* caching */
1386 output_len
+= ata_msense_caching(args
->id
, &p
, last
);
1389 case 0x0a: { /* control mode */
1390 output_len
+= ata_msense_ctl_mode(&p
, last
);
1394 case 0x3f: /* all pages */
1395 output_len
+= ata_msense_rw_recovery(&p
, last
);
1396 output_len
+= ata_msense_caching(args
->id
, &p
, last
);
1397 output_len
+= ata_msense_ctl_mode(&p
, last
);
1400 default: /* invalid page code */
1406 rbuf
[0] = output_len
;
1409 rbuf
[0] = output_len
>> 8;
1410 rbuf
[1] = output_len
;
1416 ata_scsi_set_sense(args
->cmd
, ILLEGAL_REQUEST
, 0x24, 0x0);
1417 /* "Invalid field in cbd" */
1421 ata_scsi_set_sense(args
->cmd
, ILLEGAL_REQUEST
, 0x39, 0x0);
1422 /* "Saving parameters not supported" */
1427 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1428 * @args: device IDENTIFY data / SCSI command of interest.
1429 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1430 * @buflen: Response buffer length.
1432 * Simulate READ CAPACITY commands.
1435 * spin_lock_irqsave(host_set lock)
1438 unsigned int ata_scsiop_read_cap(struct ata_scsi_args
*args
, u8
*rbuf
,
1439 unsigned int buflen
)
1446 if (ata_id_has_lba(args
->id
)) {
1447 if (ata_id_has_lba48(args
->id
))
1448 n_sectors
= ata_id_u64(args
->id
, 100);
1450 n_sectors
= ata_id_u32(args
->id
, 60);
1452 /* CHS default translation */
1453 n_sectors
= args
->id
[1] * args
->id
[3] * args
->id
[6];
1455 if (ata_id_current_chs_valid(args
->id
))
1456 /* CHS current translation */
1457 n_sectors
= ata_id_u32(args
->id
, 57);
1460 n_sectors
--; /* ATA TotalUserSectors - 1 */
1462 if (args
->cmd
->cmnd
[0] == READ_CAPACITY
) {
1463 if( n_sectors
>= 0xffffffffULL
)
1464 tmp
= 0xffffffff ; /* Return max count on overflow */
1468 /* sector count, 32-bit */
1469 rbuf
[0] = tmp
>> (8 * 3);
1470 rbuf
[1] = tmp
>> (8 * 2);
1471 rbuf
[2] = tmp
>> (8 * 1);
1475 tmp
= ATA_SECT_SIZE
;
1480 /* sector count, 64-bit */
1481 tmp
= n_sectors
>> (8 * 4);
1482 rbuf
[2] = tmp
>> (8 * 3);
1483 rbuf
[3] = tmp
>> (8 * 2);
1484 rbuf
[4] = tmp
>> (8 * 1);
1487 rbuf
[6] = tmp
>> (8 * 3);
1488 rbuf
[7] = tmp
>> (8 * 2);
1489 rbuf
[8] = tmp
>> (8 * 1);
1493 tmp
= ATA_SECT_SIZE
;
1494 rbuf
[12] = tmp
>> 8;
1502 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1503 * @args: device IDENTIFY data / SCSI command of interest.
1504 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1505 * @buflen: Response buffer length.
1507 * Simulate REPORT LUNS command.
1510 * spin_lock_irqsave(host_set lock)
1513 unsigned int ata_scsiop_report_luns(struct ata_scsi_args
*args
, u8
*rbuf
,
1514 unsigned int buflen
)
1517 rbuf
[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1523 * ata_scsi_set_sense - Set SCSI sense data and status
1524 * @cmd: SCSI request to be handled
1525 * @sk: SCSI-defined sense key
1526 * @asc: SCSI-defined additional sense code
1527 * @ascq: SCSI-defined additional sense code qualifier
1529 * Helper function that builds a valid fixed format, current
1530 * response code and the given sense key (sk), additional sense
1531 * code (asc) and additional sense code qualifier (ascq) with
1532 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1533 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1539 void ata_scsi_set_sense(struct scsi_cmnd
*cmd
, u8 sk
, u8 asc
, u8 ascq
)
1541 cmd
->result
= (DRIVER_SENSE
<< 24) | SAM_STAT_CHECK_CONDITION
;
1543 cmd
->sense_buffer
[0] = 0x70; /* fixed format, current */
1544 cmd
->sense_buffer
[2] = sk
;
1545 cmd
->sense_buffer
[7] = 18 - 8; /* additional sense length */
1546 cmd
->sense_buffer
[12] = asc
;
1547 cmd
->sense_buffer
[13] = ascq
;
1551 * ata_scsi_badcmd - End a SCSI request with an error
1552 * @cmd: SCSI request to be handled
1553 * @done: SCSI command completion function
1554 * @asc: SCSI-defined additional sense code
1555 * @ascq: SCSI-defined additional sense code qualifier
1557 * Helper function that completes a SCSI command with
1558 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1559 * and the specified additional sense codes.
1562 * spin_lock_irqsave(host_set lock)
1565 void ata_scsi_badcmd(struct scsi_cmnd
*cmd
, void (*done
)(struct scsi_cmnd
*), u8 asc
, u8 ascq
)
1568 ata_scsi_set_sense(cmd
, ILLEGAL_REQUEST
, asc
, ascq
);
1573 void atapi_request_sense(struct ata_port
*ap
, struct ata_device
*dev
,
1574 struct scsi_cmnd
*cmd
)
1576 DECLARE_COMPLETION(wait
);
1577 struct ata_queued_cmd
*qc
;
1578 unsigned long flags
;
1581 DPRINTK("ATAPI request sense\n");
1583 qc
= ata_qc_new_init(ap
, dev
);
1586 /* FIXME: is this needed? */
1587 memset(cmd
->sense_buffer
, 0, sizeof(cmd
->sense_buffer
));
1589 ata_sg_init_one(qc
, cmd
->sense_buffer
, sizeof(cmd
->sense_buffer
));
1590 qc
->dma_dir
= DMA_FROM_DEVICE
;
1592 memset(&qc
->cdb
, 0, ap
->cdb_len
);
1593 qc
->cdb
[0] = REQUEST_SENSE
;
1594 qc
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
1596 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1597 qc
->tf
.command
= ATA_CMD_PACKET
;
1599 qc
->tf
.protocol
= ATA_PROT_ATAPI
;
1600 qc
->tf
.lbam
= (8 * 1024) & 0xff;
1601 qc
->tf
.lbah
= (8 * 1024) >> 8;
1602 qc
->nbytes
= SCSI_SENSE_BUFFERSIZE
;
1604 qc
->waiting
= &wait
;
1605 qc
->complete_fn
= ata_qc_complete_noop
;
1607 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1608 rc
= ata_qc_issue(qc
);
1609 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1612 ata_port_disable(ap
);
1614 wait_for_completion(&wait
);
1619 static int atapi_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
1621 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
1623 VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat
);
1625 if (unlikely(drv_stat
& (ATA_BUSY
| ATA_DRQ
)))
1626 ata_to_sense_error(qc
, drv_stat
);
1628 else if (unlikely(drv_stat
& ATA_ERR
)) {
1629 DPRINTK("request check condition\n");
1631 /* FIXME: command completion with check condition
1632 * but no sense causes the error handler to run,
1633 * which then issues REQUEST SENSE, fills in the sense
1634 * buffer, and completes the command (for the second
1635 * time). We need to issue REQUEST SENSE some other
1636 * way, to avoid completing the command twice.
1638 cmd
->result
= SAM_STAT_CHECK_CONDITION
;
1646 u8
*scsicmd
= cmd
->cmnd
;
1648 if (scsicmd
[0] == INQUIRY
) {
1650 unsigned int buflen
;
1652 buflen
= ata_scsi_rbuf_get(cmd
, &buf
);
1654 /* ATAPI devices typically report zero for their SCSI version,
1655 * and sometimes deviate from the spec WRT response data
1656 * format. If SCSI version is reported as zero like normal,
1657 * then we make the following fixups: 1) Fake MMC-5 version,
1658 * to indicate to the Linux scsi midlayer this is a modern
1659 * device. 2) Ensure response data format / ATAPI information
1660 * are always correct.
1662 /* FIXME: do we ever override EVPD pages and the like, with
1670 ata_scsi_rbuf_put(cmd
, buf
);
1673 cmd
->result
= SAM_STAT_GOOD
;
1680 * atapi_xlat - Initialize PACKET taskfile
1681 * @qc: command structure to be initialized
1682 * @scsicmd: SCSI CDB associated with this PACKET command
1685 * spin_lock_irqsave(host_set lock)
1688 * Zero on success, non-zero on failure.
1691 static unsigned int atapi_xlat(struct ata_queued_cmd
*qc
, const u8
*scsicmd
)
1693 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
1694 struct ata_device
*dev
= qc
->dev
;
1695 int using_pio
= (dev
->flags
& ATA_DFLAG_PIO
);
1696 int nodata
= (cmd
->sc_data_direction
== DMA_NONE
);
1699 /* Check whether ATAPI DMA is safe */
1700 if (ata_check_atapi_dma(qc
))
1703 memcpy(&qc
->cdb
, scsicmd
, qc
->ap
->cdb_len
);
1705 qc
->complete_fn
= atapi_qc_complete
;
1707 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1708 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1709 qc
->tf
.flags
|= ATA_TFLAG_WRITE
;
1710 DPRINTK("direction: write\n");
1713 qc
->tf
.command
= ATA_CMD_PACKET
;
1715 /* no data, or PIO data xfer */
1716 if (using_pio
|| nodata
) {
1718 qc
->tf
.protocol
= ATA_PROT_ATAPI_NODATA
;
1720 qc
->tf
.protocol
= ATA_PROT_ATAPI
;
1721 qc
->tf
.lbam
= (8 * 1024) & 0xff;
1722 qc
->tf
.lbah
= (8 * 1024) >> 8;
1727 qc
->tf
.protocol
= ATA_PROT_ATAPI_DMA
;
1728 qc
->tf
.feature
|= ATAPI_PKT_DMA
;
1730 #ifdef ATAPI_ENABLE_DMADIR
1731 /* some SATA bridges need us to indicate data xfer direction */
1732 if (cmd
->sc_data_direction
!= DMA_TO_DEVICE
)
1733 qc
->tf
.feature
|= ATAPI_DMADIR
;
1737 qc
->nbytes
= cmd
->bufflen
;
1743 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1744 * @ap: ATA port to which the device is attached
1745 * @scsidev: SCSI device from which we derive the ATA device
1747 * Given various information provided in struct scsi_cmnd,
1748 * map that onto an ATA bus, and using that mapping
1749 * determine which ata_device is associated with the
1750 * SCSI command to be sent.
1753 * spin_lock_irqsave(host_set lock)
1756 * Associated ATA device, or %NULL if not found.
1759 static struct ata_device
*
1760 ata_scsi_find_dev(struct ata_port
*ap
, const struct scsi_device
*scsidev
)
1762 struct ata_device
*dev
;
1764 /* skip commands not addressed to targets we simulate */
1765 if (likely(scsidev
->id
< ATA_MAX_DEVICES
))
1766 dev
= &ap
->device
[scsidev
->id
];
1770 if (unlikely((scsidev
->channel
!= 0) ||
1771 (scsidev
->lun
!= 0)))
1774 if (unlikely(!ata_dev_present(dev
)))
1777 if (!atapi_enabled
) {
1778 if (unlikely(dev
->class == ATA_DEV_ATAPI
))
1786 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1788 * @cmd: SCSI command opcode to consider
1790 * Look up the SCSI command given, and determine whether the
1791 * SCSI command is to be translated or simulated.
1794 * Pointer to translation function if possible, %NULL if not.
1797 static inline ata_xlat_func_t
ata_get_xlat_func(struct ata_device
*dev
, u8 cmd
)
1807 return ata_scsi_rw_xlat
;
1809 case SYNCHRONIZE_CACHE
:
1810 if (ata_try_flush_cache(dev
))
1811 return ata_scsi_flush_xlat
;
1816 return ata_scsi_verify_xlat
;
1818 return ata_scsi_start_stop_xlat
;
1825 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1826 * @ap: ATA port to which the command was being sent
1827 * @cmd: SCSI command to dump
1829 * Prints the contents of a SCSI command via printk().
1832 static inline void ata_scsi_dump_cdb(struct ata_port
*ap
,
1833 struct scsi_cmnd
*cmd
)
1836 struct scsi_device
*scsidev
= cmd
->device
;
1837 u8
*scsicmd
= cmd
->cmnd
;
1839 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1841 scsidev
->channel
, scsidev
->id
, scsidev
->lun
,
1842 scsicmd
[0], scsicmd
[1], scsicmd
[2], scsicmd
[3],
1843 scsicmd
[4], scsicmd
[5], scsicmd
[6], scsicmd
[7],
1849 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1850 * @cmd: SCSI command to be sent
1851 * @done: Completion function, called when command is complete
1853 * In some cases, this function translates SCSI commands into
1854 * ATA taskfiles, and queues the taskfiles to be sent to
1855 * hardware. In other cases, this function simulates a
1856 * SCSI device by evaluating and responding to certain
1857 * SCSI commands. This creates the overall effect of
1858 * ATA and ATAPI devices appearing as SCSI devices.
1861 * Releases scsi-layer-held lock, and obtains host_set lock.
1867 int ata_scsi_queuecmd(struct scsi_cmnd
*cmd
, void (*done
)(struct scsi_cmnd
*))
1869 struct ata_port
*ap
;
1870 struct ata_device
*dev
;
1871 struct scsi_device
*scsidev
= cmd
->device
;
1873 ap
= (struct ata_port
*) &scsidev
->host
->hostdata
[0];
1875 ata_scsi_dump_cdb(ap
, cmd
);
1877 dev
= ata_scsi_find_dev(ap
, scsidev
);
1878 if (unlikely(!dev
)) {
1879 cmd
->result
= (DID_BAD_TARGET
<< 16);
1884 if (dev
->class == ATA_DEV_ATA
) {
1885 ata_xlat_func_t xlat_func
= ata_get_xlat_func(dev
,
1889 ata_scsi_translate(ap
, dev
, cmd
, done
, xlat_func
);
1891 ata_scsi_simulate(dev
->id
, cmd
, done
);
1893 ata_scsi_translate(ap
, dev
, cmd
, done
, atapi_xlat
);
1900 * ata_scsi_simulate - simulate SCSI command on ATA device
1901 * @id: current IDENTIFY data for target device.
1902 * @cmd: SCSI command being sent to device.
1903 * @done: SCSI command completion function.
1905 * Interprets and directly executes a select list of SCSI commands
1906 * that can be handled internally.
1909 * spin_lock_irqsave(host_set lock)
1912 void ata_scsi_simulate(u16
*id
,
1913 struct scsi_cmnd
*cmd
,
1914 void (*done
)(struct scsi_cmnd
*))
1916 struct ata_scsi_args args
;
1917 const u8
*scsicmd
= cmd
->cmnd
;
1923 switch(scsicmd
[0]) {
1924 /* no-op's, complete with success */
1925 case SYNCHRONIZE_CACHE
:
1929 case TEST_UNIT_READY
:
1930 case FORMAT_UNIT
: /* FIXME: correct? */
1931 case SEND_DIAGNOSTIC
: /* FIXME: correct? */
1932 ata_scsi_rbuf_fill(&args
, ata_scsiop_noop
);
1936 if (scsicmd
[1] & 2) /* is CmdDt set? */
1937 ata_scsi_invalid_field(cmd
, done
);
1938 else if ((scsicmd
[1] & 1) == 0) /* is EVPD clear? */
1939 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_std
);
1940 else if (scsicmd
[2] == 0x00)
1941 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_00
);
1942 else if (scsicmd
[2] == 0x80)
1943 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_80
);
1944 else if (scsicmd
[2] == 0x83)
1945 ata_scsi_rbuf_fill(&args
, ata_scsiop_inq_83
);
1947 ata_scsi_invalid_field(cmd
, done
);
1952 ata_scsi_rbuf_fill(&args
, ata_scsiop_mode_sense
);
1955 case MODE_SELECT
: /* unconditionally return */
1956 case MODE_SELECT_10
: /* bad-field-in-cdb */
1957 ata_scsi_invalid_field(cmd
, done
);
1961 ata_scsi_rbuf_fill(&args
, ata_scsiop_read_cap
);
1964 case SERVICE_ACTION_IN
:
1965 if ((scsicmd
[1] & 0x1f) == SAI_READ_CAPACITY_16
)
1966 ata_scsi_rbuf_fill(&args
, ata_scsiop_read_cap
);
1968 ata_scsi_invalid_field(cmd
, done
);
1972 ata_scsi_rbuf_fill(&args
, ata_scsiop_report_luns
);
1975 /* mandantory commands we haven't implemented yet */
1978 /* all other commands */
1980 ata_scsi_set_sense(cmd
, ILLEGAL_REQUEST
, 0x20, 0x0);
1981 /* "Invalid command operation code" */
1987 void ata_scsi_scan_host(struct ata_port
*ap
)
1989 struct ata_device
*dev
;
1992 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1995 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1996 dev
= &ap
->device
[i
];
1998 if (ata_dev_present(dev
))
1999 scsi_scan_target(&ap
->host
->shost_gendev
, 0, i
, 0, 0);