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>
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;
170 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
172 return 0; /* scsi layer doesn't check return value, sigh */
176 * ata_scsi_error - SCSI layer error handler callback
177 * @host: SCSI host on which error occurred
179 * Handles SCSI-layer-thrown error events.
182 * Inherited from SCSI layer (none, can sleep)
188 int ata_scsi_error(struct Scsi_Host *host)
194 ap = (struct ata_port *) &host->hostdata[0];
195 ap->ops->eng_timeout(ap);
202 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
203 * @qc: Storage for translated ATA taskfile
204 * @scsicmd: SCSI command to translate
206 * Converts any of six SCSI read/write commands into the
207 * ATA counterpart, including starting sector (LBA),
208 * sector count, and taking into account the device's LBA48
211 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
212 * %WRITE_16 are currently supported.
215 * spin_lock_irqsave(host_set lock)
218 * Zero on success, non-zero on error.
221 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
223 struct ata_taskfile *tf = &qc->tf;
224 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
226 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
231 tf->protocol = qc->dev->xfer_protocol;
232 tf->device |= ATA_LBA;
234 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
235 scsicmd[0] == READ_16) {
236 tf->command = qc->dev->read_cmd;
238 tf->command = qc->dev->write_cmd;
239 tf->flags |= ATA_TFLAG_WRITE;
242 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
244 tf->hob_nsect = scsicmd[7];
245 tf->hob_lbal = scsicmd[2];
247 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
250 /* if we don't support LBA48 addressing, the request
251 * -may- be too large. */
252 if ((scsicmd[2] & 0xf0) || scsicmd[7])
255 /* stores LBA27:24 in lower 4 bits of device reg */
256 tf->device |= scsicmd[2];
258 qc->nsect = scsicmd[8];
261 tf->nsect = scsicmd[8];
262 tf->lbal = scsicmd[5];
263 tf->lbam = scsicmd[4];
264 tf->lbah = scsicmd[3];
266 VPRINTK("ten-byte command\n");
270 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
271 qc->nsect = tf->nsect = scsicmd[4];
272 tf->lbal = scsicmd[3];
273 tf->lbam = scsicmd[2];
274 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
276 VPRINTK("six-byte command\n");
280 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
281 /* rule out impossible LBAs and sector counts */
282 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
286 tf->hob_nsect = scsicmd[12];
287 tf->hob_lbal = scsicmd[6];
288 tf->hob_lbam = scsicmd[5];
289 tf->hob_lbah = scsicmd[4];
291 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
294 /* once again, filter out impossible non-zero values */
295 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
299 /* stores LBA27:24 in lower 4 bits of device reg */
300 tf->device |= scsicmd[2];
302 qc->nsect = scsicmd[13];
305 tf->nsect = scsicmd[13];
306 tf->lbal = scsicmd[9];
307 tf->lbam = scsicmd[8];
308 tf->lbah = scsicmd[7];
310 VPRINTK("sixteen-byte command\n");
314 DPRINTK("no-byte command\n");
319 * ata_scsi_translate - Translate then issue SCSI command to ATA device
320 * @ap: ATA port to which the command is addressed
321 * @dev: ATA device to which the command is addressed
322 * @cmd: SCSI command to execute
323 * @done: SCSI command completion function
325 * Our ->queuecommand() function has decided that the SCSI
326 * command issued can be directly translated into an ATA
327 * command, rather than handled internally.
329 * This function sets up an ata_queued_cmd structure for the
330 * SCSI command, and sends that ata_queued_cmd to the hardware.
333 * spin_lock_irqsave(host_set lock)
336 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
337 struct scsi_cmnd *cmd,
338 void (*done)(struct scsi_cmnd *),
339 ata_xlat_func_t xlat_func)
341 struct ata_queued_cmd *qc;
342 u8 *scsicmd = cmd->cmnd;
346 if (unlikely(cmd->request_bufflen < 1)) {
347 printk(KERN_WARNING "ata%u(%u): empty request buffer\n",
352 qc = ata_scsi_qc_new(ap, dev, cmd, done);
356 if (cmd->sc_data_direction == SCSI_DATA_READ ||
357 cmd->sc_data_direction == SCSI_DATA_WRITE)
358 qc->flags |= ATA_QCFLAG_SG; /* data is present; dma-map it */
360 if (xlat_func(qc, scsicmd))
363 /* select device, send command to hardware */
364 if (ata_qc_issue(qc))
371 ata_bad_cdb(cmd, done);
372 DPRINTK("EXIT - badcmd\n");
376 * ata_scsi_rbuf_get - Map response buffer.
377 * @cmd: SCSI command containing buffer to be mapped.
378 * @buf_out: Pointer to mapped area.
380 * Maps buffer contained within SCSI command @cmd.
383 * spin_lock_irqsave(host_set lock)
386 * Length of response buffer.
389 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
395 struct scatterlist *sg;
397 sg = (struct scatterlist *) cmd->request_buffer;
398 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
401 buf = cmd->request_buffer;
402 buflen = cmd->request_bufflen;
405 memset(buf, 0, buflen);
411 * ata_scsi_rbuf_put - Unmap response buffer.
412 * @cmd: SCSI command containing buffer to be unmapped.
414 * Unmaps response buffer contained within @cmd.
417 * spin_lock_irqsave(host_set lock)
420 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd)
423 struct scatterlist *sg;
425 sg = (struct scatterlist *) cmd->request_buffer;
426 kunmap_atomic(sg->page, KM_USER0);
431 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
432 * @args: Port / device / SCSI command of interest.
433 * @actor: Callback hook for desired SCSI command simulator
435 * Takes care of the hard work of simulating a SCSI command...
436 * Mapping the response buffer, calling the command's handler,
437 * and handling the handler's return value. This return value
438 * indicates whether the handler wishes the SCSI command to be
439 * completed successfully, or not.
442 * spin_lock_irqsave(host_set lock)
445 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
446 unsigned int (*actor) (struct ata_scsi_args *args,
447 u8 *rbuf, unsigned int buflen))
450 unsigned int buflen, rc;
451 struct scsi_cmnd *cmd = args->cmd;
453 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
454 rc = actor(args, rbuf, buflen);
455 ata_scsi_rbuf_put(cmd);
458 ata_bad_cdb(cmd, args->done);
460 cmd->result = SAM_STAT_GOOD;
466 * ata_scsiop_inq_std - Simulate INQUIRY command
467 * @args: Port / device / SCSI command of interest.
468 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
469 * @buflen: Response buffer length.
471 * Returns standard device identification data associated
472 * with non-EVPD INQUIRY command output.
475 * spin_lock_irqsave(host_set lock)
478 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
484 0x5, /* claim SPC-3 version compatibility */
491 memcpy(rbuf, hdr, sizeof(hdr));
494 memcpy(&rbuf[8], args->dev->vendor, 8);
495 memcpy(&rbuf[16], args->dev->product, 16);
496 memcpy(&rbuf[32], DRV_VERSION, 4);
500 const u8 versions[] = {
501 0x60, /* SAM-3 (no version claimed) */
504 0x20, /* SBC-2 (no version claimed) */
507 0x60 /* SPC-3 (no version claimed) */
510 memcpy(rbuf + 59, versions, sizeof(versions));
517 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
518 * @args: Port / device / SCSI command of interest.
519 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
520 * @buflen: Response buffer length.
522 * Returns list of inquiry EVPD pages available.
525 * spin_lock_irqsave(host_set lock)
528 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
532 0x00, /* page 0x00, this page */
533 0x80, /* page 0x80, unit serial no page */
534 0x83 /* page 0x83, device ident page */
536 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
539 memcpy(rbuf + 4, pages, sizeof(pages));
545 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
546 * @args: Port / device / SCSI command of interest.
547 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
548 * @buflen: Response buffer length.
550 * Returns ATA device serial number.
553 * spin_lock_irqsave(host_set lock)
556 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
561 0x80, /* this page code */
563 ATA_SERNO_LEN, /* page len */
565 memcpy(rbuf, hdr, sizeof(hdr));
567 if (buflen > (ATA_SERNO_LEN + 4))
568 ata_dev_id_string(args->dev, (unsigned char *) &rbuf[4],
569 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
574 static const char *inq_83_str = "Linux ATA-SCSI simulator";
577 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
578 * @args: Port / device / SCSI command of interest.
579 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
580 * @buflen: Response buffer length.
582 * Returns device identification. Currently hardcoded to
583 * return "Linux ATA-SCSI simulator".
586 * spin_lock_irqsave(host_set lock)
589 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
592 rbuf[1] = 0x83; /* this page code */
593 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
595 /* our one and only identification descriptor (vendor-specific) */
596 if (buflen > (strlen(inq_83_str) + 4 + 4)) {
597 rbuf[4 + 0] = 2; /* code set: ASCII */
598 rbuf[4 + 3] = strlen(inq_83_str);
599 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
607 * @args: Port / device / SCSI command of interest.
608 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
609 * @buflen: Response buffer length.
611 * No operation. Simply returns success to caller, to indicate
612 * that the caller should successfully complete this SCSI command.
615 * spin_lock_irqsave(host_set lock)
618 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
626 * ata_msense_push - Push data onto MODE SENSE data output buffer
627 * @ptr_io: (input/output) Location to store more output data
628 * @last: End of output data buffer
629 * @buf: Pointer to BLOB being added to output buffer
630 * @buflen: Length of BLOB
632 * Store MODE SENSE data on an output buffer.
638 static void ata_msense_push(u8 **ptr_io, const u8 *last,
639 const u8 *buf, unsigned int buflen)
643 if ((ptr + buflen - 1) > last)
646 memcpy(ptr, buf, buflen);
654 * ata_msense_caching - Simulate MODE SENSE caching info page
655 * @dev: Device associated with this MODE SENSE command
656 * @ptr_io: (input/output) Location to store more output data
657 * @last: End of output data buffer
659 * Generate a caching info page, which conditionally indicates
660 * write caching to the SCSI layer, depending on device
667 static unsigned int ata_msense_caching(struct ata_device *dev, u8 **ptr_io,
670 u8 page[7] = { 0xf, 0, 0x10, 0, 0x8, 0xa, 0 };
671 if (dev->flags & ATA_DFLAG_WCACHE)
674 ata_msense_push(ptr_io, last, page, sizeof(page));
679 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
680 * @dev: Device associated with this MODE SENSE command
681 * @ptr_io: (input/output) Location to store more output data
682 * @last: End of output data buffer
684 * Generate a generic MODE SENSE control mode page.
690 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
692 const u8 page[] = {0xa, 0xa, 2, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
694 ata_msense_push(ptr_io, last, page, sizeof(page));
699 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
700 * @args: Port / device / SCSI command of interest.
701 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
702 * @buflen: Response buffer length.
704 * Simulate MODE SENSE commands.
707 * spin_lock_irqsave(host_set lock)
710 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
713 u8 *scsicmd = args->cmd->cmnd, *p, *last;
714 struct ata_device *dev = args->dev;
715 unsigned int page_control, six_byte, output_len;
719 six_byte = (scsicmd[0] == MODE_SENSE);
721 /* we only support saved and current values (which we treat
722 * in the same manner)
724 page_control = scsicmd[2] >> 6;
725 if ((page_control != 0) && (page_control != 3))
733 p = rbuf + output_len;
734 last = rbuf + buflen - 1;
736 switch(scsicmd[2] & 0x3f) {
737 case 0x08: /* caching */
738 output_len += ata_msense_caching(dev, &p, last);
741 case 0x0a: { /* control mode */
742 output_len += ata_msense_ctl_mode(&p, last);
746 case 0x3f: /* all pages */
747 output_len += ata_msense_caching(dev, &p, last);
748 output_len += ata_msense_ctl_mode(&p, last);
751 default: /* invalid page code */
757 rbuf[0] = output_len;
760 rbuf[0] = output_len >> 8;
761 rbuf[1] = output_len;
768 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
769 * @args: Port / device / SCSI command of interest.
770 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
771 * @buflen: Response buffer length.
773 * Simulate READ CAPACITY commands.
776 * spin_lock_irqsave(host_set lock)
779 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
782 u64 n_sectors = args->dev->n_sectors;
787 n_sectors--; /* one off */
789 tmp = n_sectors; /* note: truncates, if lba48 */
790 if (args->cmd->cmnd[0] == READ_CAPACITY) {
791 rbuf[0] = tmp >> (8 * 3);
792 rbuf[1] = tmp >> (8 * 2);
793 rbuf[2] = tmp >> (8 * 1);
801 rbuf[2] = n_sectors >> (8 * 7);
802 rbuf[3] = n_sectors >> (8 * 6);
803 rbuf[4] = n_sectors >> (8 * 5);
804 rbuf[5] = n_sectors >> (8 * 4);
805 rbuf[6] = tmp >> (8 * 3);
806 rbuf[7] = tmp >> (8 * 2);
807 rbuf[8] = tmp >> (8 * 1);
819 * ata_scsiop_report_luns - Simulate REPORT LUNS command
820 * @args: Port / device / SCSI command of interest.
821 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
822 * @buflen: Response buffer length.
824 * Simulate REPORT LUNS command.
827 * spin_lock_irqsave(host_set lock)
830 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
834 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
840 * ata_scsi_badcmd - End a SCSI request with an error
841 * @cmd: SCSI request to be handled
842 * @done: SCSI command completion function
843 * @asc: SCSI-defined additional sense code
844 * @ascq: SCSI-defined additional sense code qualifier
846 * Helper function that completes a SCSI command with
847 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
848 * and the specified additional sense codes.
851 * spin_lock_irqsave(host_set lock)
854 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
857 cmd->result = SAM_STAT_CHECK_CONDITION;
859 cmd->sense_buffer[0] = 0x70;
860 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
861 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
862 cmd->sense_buffer[12] = asc;
863 cmd->sense_buffer[13] = ascq;
869 * atapi_scsi_queuecmd - Send CDB to ATAPI device
870 * @ap: Port to which ATAPI device is attached.
871 * @dev: Target device for CDB.
872 * @cmd: SCSI command being sent to device.
873 * @done: SCSI command completion function.
875 * Sends CDB to ATAPI device. If the Linux SCSI layer sends a
876 * non-data command, then this function handles the command
877 * directly, via polling. Otherwise, the bmdma engine is started.
880 * spin_lock_irqsave(host_set lock)
883 static void atapi_scsi_queuecmd(struct ata_port *ap, struct ata_device *dev,
884 struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
886 struct ata_queued_cmd *qc;
887 u8 *scsicmd = cmd->cmnd, status;
888 unsigned int doing_dma = 0;
890 VPRINTK("ENTER, drv_stat = 0x%x\n", ata_chk_status(ap));
892 if (cmd->sc_data_direction == SCSI_DATA_UNKNOWN) {
893 DPRINTK("unknown data, scsicmd 0x%x\n", scsicmd[0]);
894 ata_bad_cdb(cmd, done);
903 DPRINTK("read6/write6/modesel/modesense trap\n");
904 ata_bad_scsiop(cmd, done);
912 qc = ata_scsi_qc_new(ap, dev, cmd, done);
914 printk(KERN_ERR "ata%u: command queue empty\n", ap->id);
918 qc->flags |= ATA_QCFLAG_ATAPI;
920 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
921 if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
922 qc->tf.flags |= ATA_TFLAG_WRITE;
923 DPRINTK("direction: write\n");
926 qc->tf.command = ATA_CMD_PACKET;
928 /* set up SG table */
929 if (cmd->sc_data_direction == SCSI_DATA_NONE) {
930 ap->active_tag = qc->tag;
931 qc->flags |= ATA_QCFLAG_ACTIVE | ATA_QCFLAG_POLL;
932 qc->tf.protocol = ATA_PROT_ATAPI;
934 ata_dev_select(ap, dev->devno, 1, 0);
936 DPRINTK("direction: none\n");
937 qc->tf.ctl |= ATA_NIEN; /* disable interrupts */
938 ata_tf_to_host_nolock(ap, &qc->tf);
940 qc->flags |= ATA_QCFLAG_SG; /* data is present; dma-map it */
941 qc->tf.feature = ATAPI_PKT_DMA;
942 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
946 /* select device, send command to hardware */
947 if (ata_qc_issue(qc))
951 status = ata_busy_wait(ap, ATA_BUSY, 1000);
952 if (status & ATA_BUSY) {
953 ata_thread_wake(ap, THR_PACKET);
956 if ((status & ATA_DRQ) == 0)
959 /* FIXME: mmio-ize */
960 DPRINTK("writing cdb\n");
961 outsl(ap->ioaddr.data_addr, scsicmd, ap->host->max_cmd_len / 4);
964 ata_thread_wake(ap, THR_PACKET);
971 ata_irq_on(ap); /* re-enable interrupts */
972 ata_bad_cdb(cmd, done);
973 DPRINTK("EXIT - badcmd\n");
977 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
978 * @ap: ATA port to which the device is attached
979 * @cmd: SCSI command to be sent to the device
981 * Given various information provided in struct scsi_cmnd,
982 * map that onto an ATA bus, and using that mapping
983 * determine which ata_device is associated with the
984 * SCSI command to be sent.
987 * spin_lock_irqsave(host_set lock)
990 * Associated ATA device, or %NULL if not found.
993 static inline struct ata_device *
994 ata_scsi_find_dev(struct ata_port *ap, struct scsi_cmnd *cmd)
996 struct ata_device *dev;
998 /* skip commands not addressed to targets we simulate */
999 if (likely(cmd->device->id < ATA_MAX_DEVICES))
1000 dev = &ap->device[cmd->device->id];
1004 if (unlikely((cmd->device->channel != 0) ||
1005 (cmd->device->lun != 0)))
1008 if (unlikely(!ata_dev_present(dev)))
1011 #ifndef ATA_ENABLE_ATAPI
1012 if (unlikely(dev->class == ATA_DEV_ATAPI))
1020 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1021 * @cmd: SCSI command opcode to consider
1023 * Look up the SCSI command given, and determine whether the
1024 * SCSI command is to be translated or simulated.
1027 * Pointer to translation function if possible, %NULL if not.
1030 static inline ata_xlat_func_t ata_get_xlat_func(u8 cmd)
1040 return ata_scsi_rw_xlat;
1047 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1048 * @ap: ATA port to which the command was being sent
1049 * @cmd: SCSI command to dump
1051 * Prints the contents of a SCSI command via printk().
1054 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1055 struct scsi_cmnd *cmd)
1058 u8 *scsicmd = cmd->cmnd;
1060 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1062 cmd->device->channel, cmd->device->id, cmd->device->lun,
1063 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1064 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1070 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1071 * @cmd: SCSI command to be sent
1072 * @done: Completion function, called when command is complete
1074 * In some cases, this function translates SCSI commands into
1075 * ATA taskfiles, and queues the taskfiles to be sent to
1076 * hardware. In other cases, this function simulates a
1077 * SCSI device by evaluating and responding to certain
1078 * SCSI commands. This creates the overall effect of
1079 * ATA and ATAPI devices appearing as SCSI devices.
1082 * Releases scsi-layer-held lock, and obtains host_set lock.
1088 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1090 struct ata_port *ap;
1091 struct ata_device *dev;
1093 ap = (struct ata_port *) &cmd->device->host->hostdata[0];
1095 ata_scsi_dump_cdb(ap, cmd);
1097 dev = ata_scsi_find_dev(ap, cmd);
1098 if (unlikely(!dev)) {
1099 cmd->result = (DID_BAD_TARGET << 16);
1104 if (dev->class == ATA_DEV_ATA) {
1105 ata_xlat_func_t xlat_func = ata_get_xlat_func(cmd->cmnd[0]);
1108 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1110 ata_scsi_simulate(ap, dev, cmd, done);
1112 atapi_scsi_queuecmd(ap, dev, cmd, done);
1119 * ata_scsi_simulate - simulate SCSI command on ATA device
1120 * @ap: Port to which ATA device is attached.
1121 * @dev: Target device for CDB.
1122 * @cmd: SCSI command being sent to device.
1123 * @done: SCSI command completion function.
1125 * Interprets and directly executes a select list of SCSI commands
1126 * that can be handled internally.
1129 * spin_lock_irqsave(host_set lock)
1132 static void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
1133 struct scsi_cmnd *cmd,
1134 void (*done)(struct scsi_cmnd *))
1136 struct ata_scsi_args args;
1137 u8 *scsicmd = cmd->cmnd;
1144 switch(scsicmd[0]) {
1145 case TEST_UNIT_READY: /* FIXME: correct? */
1146 case FORMAT_UNIT: /* FIXME: correct? */
1147 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1148 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1152 if (scsicmd[1] & 2) /* is CmdDt set? */
1153 ata_bad_cdb(cmd, done);
1154 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1155 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1156 else if (scsicmd[2] == 0x00)
1157 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1158 else if (scsicmd[2] == 0x80)
1159 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1160 else if (scsicmd[2] == 0x83)
1161 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1163 ata_bad_cdb(cmd, done);
1168 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1171 case MODE_SELECT: /* unconditionally return */
1172 case MODE_SELECT_10: /* bad-field-in-cdb */
1173 ata_bad_cdb(cmd, done);
1177 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1180 case SERVICE_ACTION_IN:
1181 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1182 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1184 ata_bad_cdb(cmd, done);
1188 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1191 /* mandantory commands we haven't implemented yet */
1194 /* all other commands */
1196 ata_bad_scsiop(cmd, done);