2 libata-scsi.c - helper library for ATA
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
7 The contents of this file are subject to the Open
8 Software License version 1.1 that can be found at
9 http://www.opensource.org/licenses/osl-1.1.txt and is included herein
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU General Public License version 2 (the "GPL") as distributed
14 in the kernel source COPYING file, in which case the provisions of
15 the GPL are applicable instead of the above. If you wish to allow
16 the use of your version of this file only under the terms of the
17 GPL and not to allow others to use your version of this file under
18 the OSL, indicate your decision by deleting the provisions above and
19 replace them with the notice and other provisions required by the GPL.
20 If you do not delete the provisions above, a recipient may use your
21 version of this file under either the OSL or the GPL.
25 #include <linux/kernel.h>
26 #include <linux/blkdev.h>
27 #include <linux/spinlock.h>
28 #include <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <linux/libata.h>
35 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
36 static void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
37 struct scsi_cmnd *cmd,
38 void (*done)(struct scsi_cmnd *));
42 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
43 * @sdev: SCSI device for which BIOS geometry is to be determined
44 * @bdev: block device associated with @sdev
45 * @capacity: capacity of SCSI device
46 * @geom: location to which geometry will be output
48 * Generic bios head/sector/cylinder calculator
49 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
50 * mapping. Some situations may arise where the disk is not
51 * bootable if this is not used.
54 * Defined by the SCSI layer. We don't really care.
59 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
60 sector_t capacity, int geom[])
64 sector_div(capacity, 255*63);
72 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
73 * @ap: ATA port to which the new command is attached
74 * @dev: ATA device to which the new command is attached
75 * @cmd: SCSI command that originated this ATA command
76 * @done: SCSI command completion function
78 * Obtain a reference to an unused ata_queued_cmd structure,
79 * which is the basic libata structure representing a single
80 * ATA command sent to the hardware.
82 * If a command was available, fill in the SCSI-specific
83 * portions of the structure with information on the
87 * spin_lock_irqsave(host_set lock)
90 * Command allocated, or %NULL if none available.
92 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
93 struct ata_device *dev,
94 struct scsi_cmnd *cmd,
95 void (*done)(struct scsi_cmnd *))
97 struct ata_queued_cmd *qc;
99 qc = ata_qc_new_init(ap, dev);
105 qc->sg = (struct scatterlist *) cmd->request_buffer;
106 qc->n_elem = cmd->use_sg;
112 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
120 * ata_to_sense_error - convert ATA error to SCSI error
121 * @qc: Command that we are erroring out
123 * Converts an ATA error into a SCSI error.
125 * Right now, this routine is laughably primitive. We
126 * don't even examine what ATA told us, we just look at
127 * the command data direction, and return a fatal SCSI
128 * sense error based on that.
131 * spin_lock_irqsave(host_set lock)
134 void ata_to_sense_error(struct ata_queued_cmd *qc)
136 struct scsi_cmnd *cmd = qc->scsicmd;
138 cmd->result = SAM_STAT_CHECK_CONDITION;
140 cmd->sense_buffer[0] = 0x70;
141 cmd->sense_buffer[2] = MEDIUM_ERROR;
142 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
144 /* additional-sense-code[-qualifier] */
145 if (cmd->sc_data_direction == SCSI_DATA_READ) {
146 cmd->sense_buffer[12] = 0x11; /* "unrecovered read error" */
147 cmd->sense_buffer[13] = 0x04;
149 cmd->sense_buffer[12] = 0x0C; /* "write error - */
150 cmd->sense_buffer[13] = 0x02; /* auto-reallocation failed" */
155 * ata_scsi_slave_config - Set SCSI device attributes
156 * @sdev: SCSI device to examine
158 * This is called before we actually start reading
159 * and writing to the device, to configure certain
160 * SCSI mid-layer behaviors.
163 * Defined by SCSI layer. We don't really care.
166 int ata_scsi_slave_config(struct scsi_device *sdev)
168 sdev->use_10_for_rw = 1;
169 sdev->use_10_for_ms = 1;
171 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
173 if (sdev->id < ATA_MAX_DEVICES) {
175 struct ata_device *dev;
177 ap = (struct ata_port *) &sdev->host->hostdata[0];
178 dev = &ap->device[sdev->id];
180 /* TODO: 1024 is an arbitrary number, not the
181 * hardware maximum. This should be increased to
182 * 65534 when Jens Axboe's patch for dynamically
183 * determining max_sectors is merged.
185 if ((dev->flags & ATA_DFLAG_LBA48) &&
186 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
187 sdev->host->max_sectors = 2048;
188 blk_queue_max_sectors(sdev->request_queue, 2048);
192 return 0; /* scsi layer doesn't check return value, sigh */
196 * ata_scsi_error - SCSI layer error handler callback
197 * @host: SCSI host on which error occurred
199 * Handles SCSI-layer-thrown error events.
202 * Inherited from SCSI layer (none, can sleep)
208 int ata_scsi_error(struct Scsi_Host *host)
214 ap = (struct ata_port *) &host->hostdata[0];
215 ap->ops->eng_timeout(ap);
222 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
223 * @qc: Storage for translated ATA taskfile
224 * @scsicmd: SCSI command to translate
226 * Converts any of six SCSI read/write commands into the
227 * ATA counterpart, including starting sector (LBA),
228 * sector count, and taking into account the device's LBA48
231 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
232 * %WRITE_16 are currently supported.
235 * spin_lock_irqsave(host_set lock)
238 * Zero on success, non-zero on error.
241 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
243 struct ata_taskfile *tf = &qc->tf;
244 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
246 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
251 tf->protocol = qc->dev->xfer_protocol;
252 tf->device |= ATA_LBA;
254 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
255 scsicmd[0] == READ_16) {
256 tf->command = qc->dev->read_cmd;
258 tf->command = qc->dev->write_cmd;
259 tf->flags |= ATA_TFLAG_WRITE;
262 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
264 tf->hob_nsect = scsicmd[7];
265 tf->hob_lbal = scsicmd[2];
267 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
270 /* if we don't support LBA48 addressing, the request
271 * -may- be too large. */
272 if ((scsicmd[2] & 0xf0) || scsicmd[7])
275 /* stores LBA27:24 in lower 4 bits of device reg */
276 tf->device |= scsicmd[2];
278 qc->nsect = scsicmd[8];
281 tf->nsect = scsicmd[8];
282 tf->lbal = scsicmd[5];
283 tf->lbam = scsicmd[4];
284 tf->lbah = scsicmd[3];
286 VPRINTK("ten-byte command\n");
290 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
291 qc->nsect = tf->nsect = scsicmd[4];
292 tf->lbal = scsicmd[3];
293 tf->lbam = scsicmd[2];
294 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
296 VPRINTK("six-byte command\n");
300 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
301 /* rule out impossible LBAs and sector counts */
302 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
306 tf->hob_nsect = scsicmd[12];
307 tf->hob_lbal = scsicmd[6];
308 tf->hob_lbam = scsicmd[5];
309 tf->hob_lbah = scsicmd[4];
311 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
314 /* once again, filter out impossible non-zero values */
315 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
319 /* stores LBA27:24 in lower 4 bits of device reg */
320 tf->device |= scsicmd[2];
322 qc->nsect = scsicmd[13];
325 tf->nsect = scsicmd[13];
326 tf->lbal = scsicmd[9];
327 tf->lbam = scsicmd[8];
328 tf->lbah = scsicmd[7];
330 VPRINTK("sixteen-byte command\n");
334 DPRINTK("no-byte command\n");
338 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
340 struct scsi_cmnd *cmd = qc->scsicmd;
342 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
343 if (is_atapi_taskfile(&qc->tf))
344 cmd->result = SAM_STAT_CHECK_CONDITION;
346 ata_to_sense_error(qc);
348 cmd->result = SAM_STAT_GOOD;
357 * ata_scsi_translate - Translate then issue SCSI command to ATA device
358 * @ap: ATA port to which the command is addressed
359 * @dev: ATA device to which the command is addressed
360 * @cmd: SCSI command to execute
361 * @done: SCSI command completion function
362 * @xlat_func: Actor which translates @cmd to an ATA taskfile
364 * Our ->queuecommand() function has decided that the SCSI
365 * command issued can be directly translated into an ATA
366 * command, rather than handled internally.
368 * This function sets up an ata_queued_cmd structure for the
369 * SCSI command, and sends that ata_queued_cmd to the hardware.
372 * spin_lock_irqsave(host_set lock)
375 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
376 struct scsi_cmnd *cmd,
377 void (*done)(struct scsi_cmnd *),
378 ata_xlat_func_t xlat_func)
380 struct ata_queued_cmd *qc;
381 u8 *scsicmd = cmd->cmnd;
385 qc = ata_scsi_qc_new(ap, dev, cmd, done);
389 /* data is present; dma-map it */
390 if (cmd->sc_data_direction == SCSI_DATA_READ ||
391 cmd->sc_data_direction == SCSI_DATA_WRITE) {
392 if (unlikely(cmd->request_bufflen < 1)) {
393 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
399 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
401 ata_sg_init_one(qc, cmd->request_buffer,
402 cmd->request_bufflen);
404 qc->pci_dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
407 qc->complete_fn = ata_scsi_qc_complete;
409 if (xlat_func(qc, scsicmd))
412 /* select device, send command to hardware */
413 if (ata_qc_issue(qc))
420 ata_bad_cdb(cmd, done);
421 DPRINTK("EXIT - badcmd\n");
425 * ata_scsi_rbuf_get - Map response buffer.
426 * @cmd: SCSI command containing buffer to be mapped.
427 * @buf_out: Pointer to mapped area.
429 * Maps buffer contained within SCSI command @cmd.
432 * spin_lock_irqsave(host_set lock)
435 * Length of response buffer.
438 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
444 struct scatterlist *sg;
446 sg = (struct scatterlist *) cmd->request_buffer;
447 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
450 buf = cmd->request_buffer;
451 buflen = cmd->request_bufflen;
454 memset(buf, 0, buflen);
460 * ata_scsi_rbuf_put - Unmap response buffer.
461 * @cmd: SCSI command containing buffer to be unmapped.
463 * Unmaps response buffer contained within @cmd.
466 * spin_lock_irqsave(host_set lock)
469 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd)
472 struct scatterlist *sg;
474 sg = (struct scatterlist *) cmd->request_buffer;
475 kunmap_atomic(sg->page, KM_USER0);
480 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
481 * @args: Port / device / SCSI command of interest.
482 * @actor: Callback hook for desired SCSI command simulator
484 * Takes care of the hard work of simulating a SCSI command...
485 * Mapping the response buffer, calling the command's handler,
486 * and handling the handler's return value. This return value
487 * indicates whether the handler wishes the SCSI command to be
488 * completed successfully, or not.
491 * spin_lock_irqsave(host_set lock)
494 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
495 unsigned int (*actor) (struct ata_scsi_args *args,
496 u8 *rbuf, unsigned int buflen))
499 unsigned int buflen, rc;
500 struct scsi_cmnd *cmd = args->cmd;
502 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
503 rc = actor(args, rbuf, buflen);
504 ata_scsi_rbuf_put(cmd);
507 ata_bad_cdb(cmd, args->done);
509 cmd->result = SAM_STAT_GOOD;
515 * ata_scsiop_inq_std - Simulate INQUIRY command
516 * @args: Port / device / SCSI command of interest.
517 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
518 * @buflen: Response buffer length.
520 * Returns standard device identification data associated
521 * with non-EVPD INQUIRY command output.
524 * spin_lock_irqsave(host_set lock)
527 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
530 struct ata_device *dev = args->dev;
535 0x5, /* claim SPC-3 version compatibility */
540 /* set scsi removeable (RMB) bit per ata bit */
541 if (ata_id_removeable(dev))
546 memcpy(rbuf, hdr, sizeof(hdr));
549 memcpy(&rbuf[8], "ATA ", 8);
550 ata_dev_id_string(dev, &rbuf[16], ATA_ID_PROD_OFS, 16);
551 ata_dev_id_string(dev, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
552 if (rbuf[32] == 0 || rbuf[32] == ' ')
553 memcpy(&rbuf[32], "n/a ", 4);
557 const u8 versions[] = {
558 0x60, /* SAM-3 (no version claimed) */
561 0x20, /* SBC-2 (no version claimed) */
564 0x60 /* SPC-3 (no version claimed) */
567 memcpy(rbuf + 59, versions, sizeof(versions));
574 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
575 * @args: Port / device / SCSI command of interest.
576 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
577 * @buflen: Response buffer length.
579 * Returns list of inquiry EVPD pages available.
582 * spin_lock_irqsave(host_set lock)
585 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
589 0x00, /* page 0x00, this page */
590 0x80, /* page 0x80, unit serial no page */
591 0x83 /* page 0x83, device ident page */
593 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
596 memcpy(rbuf + 4, pages, sizeof(pages));
602 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
603 * @args: Port / device / SCSI command of interest.
604 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
605 * @buflen: Response buffer length.
607 * Returns ATA device serial number.
610 * spin_lock_irqsave(host_set lock)
613 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
618 0x80, /* this page code */
620 ATA_SERNO_LEN, /* page len */
622 memcpy(rbuf, hdr, sizeof(hdr));
624 if (buflen > (ATA_SERNO_LEN + 4))
625 ata_dev_id_string(args->dev, (unsigned char *) &rbuf[4],
626 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
631 static const char *inq_83_str = "Linux ATA-SCSI simulator";
634 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
635 * @args: Port / device / SCSI command of interest.
636 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
637 * @buflen: Response buffer length.
639 * Returns device identification. Currently hardcoded to
640 * return "Linux ATA-SCSI simulator".
643 * spin_lock_irqsave(host_set lock)
646 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
649 rbuf[1] = 0x83; /* this page code */
650 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
652 /* our one and only identification descriptor (vendor-specific) */
653 if (buflen > (strlen(inq_83_str) + 4 + 4)) {
654 rbuf[4 + 0] = 2; /* code set: ASCII */
655 rbuf[4 + 3] = strlen(inq_83_str);
656 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
664 * @args: Port / device / SCSI command of interest.
665 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
666 * @buflen: Response buffer length.
668 * No operation. Simply returns success to caller, to indicate
669 * that the caller should successfully complete this SCSI command.
672 * spin_lock_irqsave(host_set lock)
675 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
683 * ata_msense_push - Push data onto MODE SENSE data output buffer
684 * @ptr_io: (input/output) Location to store more output data
685 * @last: End of output data buffer
686 * @buf: Pointer to BLOB being added to output buffer
687 * @buflen: Length of BLOB
689 * Store MODE SENSE data on an output buffer.
695 static void ata_msense_push(u8 **ptr_io, const u8 *last,
696 const u8 *buf, unsigned int buflen)
700 if ((ptr + buflen - 1) > last)
703 memcpy(ptr, buf, buflen);
711 * ata_msense_caching - Simulate MODE SENSE caching info page
712 * @dev: Device associated with this MODE SENSE command
713 * @ptr_io: (input/output) Location to store more output data
714 * @last: End of output data buffer
716 * Generate a caching info page, which conditionally indicates
717 * write caching to the SCSI layer, depending on device
724 static unsigned int ata_msense_caching(struct ata_device *dev, u8 **ptr_io,
729 0x12, /* page length */
730 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
731 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
734 if (ata_id_wcache_enabled(dev))
735 page[2] |= (1 << 2); /* write cache enable */
736 if (!ata_id_rahead_enabled(dev))
737 page[12] |= (1 << 5); /* disable read ahead */
739 ata_msense_push(ptr_io, last, page, sizeof(page));
744 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
745 * @dev: Device associated with this MODE SENSE command
746 * @ptr_io: (input/output) Location to store more output data
747 * @last: End of output data buffer
749 * Generate a generic MODE SENSE control mode page.
755 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
757 const u8 page[] = {0xa, 0xa, 2, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
759 ata_msense_push(ptr_io, last, page, sizeof(page));
764 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
765 * @dev: Device associated with this MODE SENSE command
766 * @ptr_io: (input/output) Location to store more output data
767 * @last: End of output data buffer
769 * Generate a generic MODE SENSE r/w error recovery page.
775 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
779 0xa, /* page length */
780 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
781 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
784 ata_msense_push(ptr_io, last, page, sizeof(page));
789 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
790 * @args: Port / device / SCSI command of interest.
791 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
792 * @buflen: Response buffer length.
794 * Simulate MODE SENSE commands.
797 * spin_lock_irqsave(host_set lock)
800 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
803 u8 *scsicmd = args->cmd->cmnd, *p, *last;
804 struct ata_device *dev = args->dev;
805 unsigned int page_control, six_byte, output_len;
809 six_byte = (scsicmd[0] == MODE_SENSE);
811 /* we only support saved and current values (which we treat
812 * in the same manner)
814 page_control = scsicmd[2] >> 6;
815 if ((page_control != 0) && (page_control != 3))
823 p = rbuf + output_len;
824 last = rbuf + buflen - 1;
826 switch(scsicmd[2] & 0x3f) {
827 case 0x01: /* r/w error recovery */
828 output_len += ata_msense_rw_recovery(&p, last);
831 case 0x08: /* caching */
832 output_len += ata_msense_caching(dev, &p, last);
835 case 0x0a: { /* control mode */
836 output_len += ata_msense_ctl_mode(&p, last);
840 case 0x3f: /* all pages */
841 output_len += ata_msense_rw_recovery(&p, last);
842 output_len += ata_msense_caching(dev, &p, last);
843 output_len += ata_msense_ctl_mode(&p, last);
846 default: /* invalid page code */
852 rbuf[0] = output_len;
855 rbuf[0] = output_len >> 8;
856 rbuf[1] = output_len;
863 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
864 * @args: Port / device / SCSI command of interest.
865 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
866 * @buflen: Response buffer length.
868 * Simulate READ CAPACITY commands.
871 * spin_lock_irqsave(host_set lock)
874 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
877 u64 n_sectors = args->dev->n_sectors;
882 n_sectors--; /* ATA TotalUserSectors - 1 */
884 tmp = n_sectors; /* note: truncates, if lba48 */
885 if (args->cmd->cmnd[0] == READ_CAPACITY) {
886 /* sector count, 32-bit */
887 rbuf[0] = tmp >> (8 * 3);
888 rbuf[1] = tmp >> (8 * 2);
889 rbuf[2] = tmp >> (8 * 1);
898 /* sector count, 64-bit */
899 rbuf[2] = n_sectors >> (8 * 7);
900 rbuf[3] = n_sectors >> (8 * 6);
901 rbuf[4] = n_sectors >> (8 * 5);
902 rbuf[5] = n_sectors >> (8 * 4);
903 rbuf[6] = tmp >> (8 * 3);
904 rbuf[7] = tmp >> (8 * 2);
905 rbuf[8] = tmp >> (8 * 1);
918 * ata_scsiop_report_luns - Simulate REPORT LUNS command
919 * @args: Port / device / SCSI command of interest.
920 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
921 * @buflen: Response buffer length.
923 * Simulate REPORT LUNS command.
926 * spin_lock_irqsave(host_set lock)
929 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
933 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
939 * ata_scsi_badcmd - End a SCSI request with an error
940 * @cmd: SCSI request to be handled
941 * @done: SCSI command completion function
942 * @asc: SCSI-defined additional sense code
943 * @ascq: SCSI-defined additional sense code qualifier
945 * Helper function that completes a SCSI command with
946 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
947 * and the specified additional sense codes.
950 * spin_lock_irqsave(host_set lock)
953 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
956 cmd->result = SAM_STAT_CHECK_CONDITION;
958 cmd->sense_buffer[0] = 0x70;
959 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
960 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
961 cmd->sense_buffer[12] = asc;
962 cmd->sense_buffer[13] = ascq;
968 * atapi_xlat - Initialize PACKET taskfile
969 * @qc: command structure to be initialized
970 * @scsicmd: SCSI CDB associated with this PACKET command
973 * spin_lock_irqsave(host_set lock)
976 * Zero on success, non-zero on failure.
979 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
981 struct scsi_cmnd *cmd = qc->scsicmd;
983 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
984 if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
985 qc->tf.flags |= ATA_TFLAG_WRITE;
986 DPRINTK("direction: write\n");
989 qc->tf.command = ATA_CMD_PACKET;
991 /* no data - interrupt-driven */
992 if (cmd->sc_data_direction == SCSI_DATA_NONE)
993 qc->tf.protocol = ATA_PROT_ATAPI;
995 /* PIO data xfer - polling */
996 else if ((qc->flags & ATA_QCFLAG_DMA) == 0) {
997 ata_qc_set_polling(qc);
998 qc->tf.protocol = ATA_PROT_ATAPI;
999 qc->tf.lbam = (8 * 1024) & 0xff;
1000 qc->tf.lbah = (8 * 1024) >> 8;
1002 /* DMA data xfer - interrupt-driven */
1004 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1005 qc->tf.feature |= ATAPI_PKT_DMA;
1007 #ifdef ATAPI_ENABLE_DMADIR
1008 /* some SATA bridges need us to indicate data xfer direction */
1009 if (cmd->sc_data_direction != SCSI_DATA_WRITE)
1010 qc->tf.feature |= ATAPI_DMADIR;
1018 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1019 * @ap: ATA port to which the device is attached
1020 * @cmd: SCSI command to be sent to the device
1022 * Given various information provided in struct scsi_cmnd,
1023 * map that onto an ATA bus, and using that mapping
1024 * determine which ata_device is associated with the
1025 * SCSI command to be sent.
1028 * spin_lock_irqsave(host_set lock)
1031 * Associated ATA device, or %NULL if not found.
1034 static inline struct ata_device *
1035 ata_scsi_find_dev(struct ata_port *ap, struct scsi_cmnd *cmd)
1037 struct ata_device *dev;
1039 /* skip commands not addressed to targets we simulate */
1040 if (likely(cmd->device->id < ATA_MAX_DEVICES))
1041 dev = &ap->device[cmd->device->id];
1045 if (unlikely((cmd->device->channel != 0) ||
1046 (cmd->device->lun != 0)))
1049 if (unlikely(!ata_dev_present(dev)))
1052 #ifndef ATA_ENABLE_ATAPI
1053 if (unlikely(dev->class == ATA_DEV_ATAPI))
1061 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1062 * @cmd: SCSI command opcode to consider
1064 * Look up the SCSI command given, and determine whether the
1065 * SCSI command is to be translated or simulated.
1068 * Pointer to translation function if possible, %NULL if not.
1071 static inline ata_xlat_func_t ata_get_xlat_func(u8 cmd)
1081 return ata_scsi_rw_xlat;
1088 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1089 * @ap: ATA port to which the command was being sent
1090 * @cmd: SCSI command to dump
1092 * Prints the contents of a SCSI command via printk().
1095 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1096 struct scsi_cmnd *cmd)
1099 u8 *scsicmd = cmd->cmnd;
1101 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1103 cmd->device->channel, cmd->device->id, cmd->device->lun,
1104 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1105 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1111 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1112 * @cmd: SCSI command to be sent
1113 * @done: Completion function, called when command is complete
1115 * In some cases, this function translates SCSI commands into
1116 * ATA taskfiles, and queues the taskfiles to be sent to
1117 * hardware. In other cases, this function simulates a
1118 * SCSI device by evaluating and responding to certain
1119 * SCSI commands. This creates the overall effect of
1120 * ATA and ATAPI devices appearing as SCSI devices.
1123 * Releases scsi-layer-held lock, and obtains host_set lock.
1129 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1131 struct ata_port *ap;
1132 struct ata_device *dev;
1134 ap = (struct ata_port *) &cmd->device->host->hostdata[0];
1136 ata_scsi_dump_cdb(ap, cmd);
1138 dev = ata_scsi_find_dev(ap, cmd);
1139 if (unlikely(!dev)) {
1140 cmd->result = (DID_BAD_TARGET << 16);
1145 if (dev->class == ATA_DEV_ATA) {
1146 ata_xlat_func_t xlat_func = ata_get_xlat_func(cmd->cmnd[0]);
1149 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1151 ata_scsi_simulate(ap, dev, cmd, done);
1153 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1160 * ata_scsi_simulate - simulate SCSI command on ATA device
1161 * @ap: Port to which ATA device is attached.
1162 * @dev: Target device for CDB.
1163 * @cmd: SCSI command being sent to device.
1164 * @done: SCSI command completion function.
1166 * Interprets and directly executes a select list of SCSI commands
1167 * that can be handled internally.
1170 * spin_lock_irqsave(host_set lock)
1173 static void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
1174 struct scsi_cmnd *cmd,
1175 void (*done)(struct scsi_cmnd *))
1177 struct ata_scsi_args args;
1178 u8 *scsicmd = cmd->cmnd;
1185 switch(scsicmd[0]) {
1186 /* no-op's, complete with success */
1187 case SYNCHRONIZE_CACHE: /* FIXME: temporary */
1191 case TEST_UNIT_READY:
1192 case FORMAT_UNIT: /* FIXME: correct? */
1193 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1194 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1198 if (scsicmd[1] & 2) /* is CmdDt set? */
1199 ata_bad_cdb(cmd, done);
1200 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1201 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1202 else if (scsicmd[2] == 0x00)
1203 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1204 else if (scsicmd[2] == 0x80)
1205 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1206 else if (scsicmd[2] == 0x83)
1207 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1209 ata_bad_cdb(cmd, done);
1214 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1217 case MODE_SELECT: /* unconditionally return */
1218 case MODE_SELECT_10: /* bad-field-in-cdb */
1219 ata_bad_cdb(cmd, done);
1223 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1226 case SERVICE_ACTION_IN:
1227 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1228 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1230 ata_bad_cdb(cmd, done);
1234 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1237 /* mandantory commands we haven't implemented yet */
1240 /* all other commands */
1242 ata_bad_scsiop(cmd, done);