DVB (2390): Adds a time-delay to IR remote button presses for av7110 ir input,
[safe/jmp/linux-2.6] / drivers / scsi / libata-scsi.c
index b761bd1..cfbceb5 100644 (file)
 #include <linux/blkdev.h>
 #include <linux/spinlock.h>
 #include <scsi/scsi.h>
-#include "scsi.h"
 #include <scsi/scsi_host.h>
+#include <scsi/scsi_eh.h>
+#include <scsi/scsi_device.h>
+#include <scsi/scsi_request.h>
 #include <linux/libata.h>
 #include <linux/hdreg.h>
 #include <asm/uaccess.h>
@@ -51,6 +53,45 @@ typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scs
 static struct ata_device *
 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
 
+#define RW_RECOVERY_MPAGE 0x1
+#define RW_RECOVERY_MPAGE_LEN 12
+#define CACHE_MPAGE 0x8
+#define CACHE_MPAGE_LEN 20
+#define CONTROL_MPAGE 0xa
+#define CONTROL_MPAGE_LEN 12
+#define ALL_MPAGES 0x3f
+#define ALL_SUB_MPAGES 0xff
+
+
+static const u8 def_rw_recovery_mpage[] = {
+       RW_RECOVERY_MPAGE,
+       RW_RECOVERY_MPAGE_LEN - 2,
+       (1 << 7) |      /* AWRE, sat-r06 say it shall be 0 */
+           (1 << 6),   /* ARRE (auto read reallocation) */
+       0,              /* read retry count */
+       0, 0, 0, 0,
+       0,              /* write retry count */
+       0, 0, 0
+};
+
+static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
+       CACHE_MPAGE,
+       CACHE_MPAGE_LEN - 2,
+       0,              /* contains WCE, needs to be 0 for logic */
+       0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0,              /* contains DRA, needs to be 0 for logic */
+       0, 0, 0, 0, 0, 0, 0
+};
+
+static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
+       CONTROL_MPAGE,
+       CONTROL_MPAGE_LEN - 2,
+       2,      /* DSENSE=0, GLTSD=1 */
+       0,      /* [QAM+QERR may be 1, see 05-359r1] */
+       0, 0, 0, 0, 0xff, 0xff,
+       0, 30   /* extended self test time, see 05-359r1 */
+};
+
 
 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
                                   void (*done)(struct scsi_cmnd *))
@@ -91,7 +132,7 @@ int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
 
 /**
  *     ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
- *     @dev: Device to whom we are issuing command
+ *     @scsidev: Device to which we are issuing command
  *     @arg: User provided data for issuing command
  *
  *     LOCKING:
@@ -107,7 +148,8 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
        u8 scsi_cmd[MAX_COMMAND_SIZE];
        u8 args[4], *argbuf = NULL;
        int argsize = 0;
-       struct scsi_request *sreq;
+       struct scsi_sense_hdr sshdr;
+       enum dma_data_direction data_dir;
 
        if (NULL == (void *)arg)
                return -EINVAL;
@@ -115,10 +157,6 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
        if (copy_from_user(args, arg, sizeof(args)))
                return -EFAULT;
 
-       sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
-       if (!sreq)
-               return -EINTR;
-
        memset(scsi_cmd, 0, sizeof(scsi_cmd));
 
        if (args[3]) {
@@ -132,11 +170,11 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
                scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
                scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
                                            block count in sector count field */
-               sreq->sr_data_direction = DMA_FROM_DEVICE;
+               data_dir = DMA_FROM_DEVICE;
        } else {
                scsi_cmd[1]  = (3 << 1); /* Non-data */
                /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
-               sreq->sr_data_direction = DMA_NONE;
+               data_dir = DMA_NONE;
        }
 
        scsi_cmd[0] = ATA_16;
@@ -154,9 +192,8 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
 
        /* Good values for timeout and retries?  Values below
           from scsi_ioctl_send_command() for default case... */
-       scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
-
-       if (sreq->sr_result) {
+       if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
+                            &sshdr, (10*HZ), 5)) {
                rc = -EIO;
                goto error;
        }
@@ -167,8 +204,6 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
         && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
                rc = -EFAULT;
 error:
-       scsi_release_request(sreq);
-
        if (argbuf)
                kfree(argbuf);
 
@@ -177,7 +212,7 @@ error:
 
 /**
  *     ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
- *     @dev: Device to whom we are issuing command
+ *     @scsidev: Device to which we are issuing command
  *     @arg: User provided data for issuing command
  *
  *     LOCKING:
@@ -191,7 +226,7 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
        int rc = 0;
        u8 scsi_cmd[MAX_COMMAND_SIZE];
        u8 args[7];
-       struct scsi_request *sreq;
+       struct scsi_sense_hdr sshdr;
 
        if (NULL == (void *)arg)
                return -EINVAL;
@@ -210,26 +245,13 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
        scsi_cmd[12] = args[5];
        scsi_cmd[14] = args[0];
 
-       sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
-       if (!sreq) {
-               rc = -EINTR;
-               goto error;
-       }
-
-       sreq->sr_data_direction = DMA_NONE;
        /* Good values for timeout and retries?  Values below
-          from scsi_ioctl_send_command() for default case... */
-       scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
-
-       if (sreq->sr_result) {
+          from scsi_ioctl_send_command() for default case... */        
+       if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
+                            (10*HZ), 5))
                rc = -EIO;
-               goto error;
-       }
 
        /* Need code to retrieve data from check condition? */
-
-error:
-       scsi_release_request(sreq);
        return rc;
 }
 
@@ -315,10 +337,10 @@ struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
                qc->scsidone = done;
 
                if (cmd->use_sg) {
-                       qc->sg = (struct scatterlist *) cmd->request_buffer;
+                       qc->__sg = (struct scatterlist *) cmd->request_buffer;
                        qc->n_elem = cmd->use_sg;
                } else {
-                       qc->sg = &qc->sgent;
+                       qc->__sg = &qc->sgent;
                        qc->n_elem = 1;
                }
        } else {
@@ -374,8 +396,25 @@ void ata_dump_status(unsigned id, struct ata_taskfile *tf)
        }
 }
 
+int ata_scsi_device_resume(struct scsi_device *sdev)
+{
+       struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
+       struct ata_device *dev = &ap->device[sdev->id];
+
+       return ata_device_resume(ap, dev);
+}
+
+int ata_scsi_device_suspend(struct scsi_device *sdev)
+{
+       struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
+       struct ata_device *dev = &ap->device[sdev->id];
+
+       return ata_device_suspend(ap, dev);
+}
+
 /**
  *     ata_to_sense_error - convert ATA error to SCSI error
+ *     @id: ATA device number
  *     @drv_stat: value contained in ATA status register
  *     @drv_err: value contained in ATA error register
  *     @sk: the sense key we'll fill out
@@ -395,7 +434,7 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
        int i;
 
        /* Based on the 3ware driver translation table */
-       static unsigned char sense_table[][4] = {
+       static const unsigned char sense_table[][4] = {
                /* BBD|ECC|ID|MAR */
                {0xd1,          ABORTED_COMMAND, 0x00, 0x00},   // Device busy                  Aborted command
                /* BBD|ECC|ID */
@@ -426,7 +465,7 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
                {0x80,          MEDIUM_ERROR, 0x11, 0x04},      // Block marked bad               Medium error, unrecovered read error
                {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
        };
-       static unsigned char stat_table[][4] = {
+       static const unsigned char stat_table[][4] = {
                /* Must be first because BUSY means no other bits valid */
                {0x80,          ABORTED_COMMAND, 0x47, 0x00},   // Busy, fake parity for now
                {0x20,          HARDWARE_ERROR,  0x00, 0x00},   // Device fault
@@ -521,7 +560,7 @@ void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
         * Use ata_to_sense_error() to map status register bits
         * onto sense key, asc & ascq.
         */
-       if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
+       if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
                ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
                                   &sb[1], &sb[2], &sb[3]);
                sb[1] &= 0x0f;
@@ -596,7 +635,7 @@ void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
         * Use ata_to_sense_error() to map status register bits
         * onto sense key, asc & ascq.
         */
-       if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
+       if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
                ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
                                   &sb[2], &sb[12], &sb[13]);
                sb[2] &= 0x0f;
@@ -605,7 +644,11 @@ void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
        sb[0] = 0x70;
        sb[7] = 0x0a;
 
-       if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
+       if (tf->flags & ATA_TFLAG_LBA48) {
+               /* TODO: find solution for LBA48 descriptors */
+       }
+
+       else if (tf->flags & ATA_TFLAG_LBA) {
                /* A small (28b) LBA will fit in the 32b info field */
                sb[0] |= 0x80;          /* set valid bit */
                sb[3] = tf->device & 0x0f;
@@ -613,6 +656,10 @@ void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
                sb[5] = tf->lbam;
                sb[6] = tf->lbal;
        }
+
+       else {
+               /* TODO: C/H/S */
+       }
 }
 
 /**
@@ -654,6 +701,16 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
                         */
                        blk_queue_max_sectors(sdev->request_queue, 2048);
                }
+
+               /*
+                * SATA DMA transfers must be multiples of 4 byte, so
+                * we need to pad ATAPI transfers using an extra sg.
+                * Decrement max hw segments accordingly.
+                */
+               if (dev->class == ATA_DEV_ATAPI) {
+                       request_queue_t *q = sdev->request_queue;
+                       blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
+               }
        }
 
        return 0;       /* scsi layer doesn't check return value, sigh */
@@ -1039,11 +1096,13 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
            scsicmd[0] == WRITE_16)
                tf->flags |= ATA_TFLAG_WRITE;
 
-       /* Calculate the SCSI LBA and transfer length. */
+       /* Calculate the SCSI LBA, transfer length and FUA. */
        switch (scsicmd[0]) {
        case READ_10:
        case WRITE_10:
                scsi_10_lba_len(scsicmd, &block, &n_block);
+               if (unlikely(scsicmd[1] & (1 << 3)))
+                       tf->flags |= ATA_TFLAG_FUA;
                break;
        case READ_6:
        case WRITE_6:
@@ -1058,6 +1117,8 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
        case READ_16:
        case WRITE_16:
                scsi_16_lba_len(scsicmd, &block, &n_block);
+               if (unlikely(scsicmd[1] & (1 << 3)))
+                       tf->flags |= ATA_TFLAG_FUA;
                break;
        default:
                DPRINTK("no-byte command\n");
@@ -1070,6 +1131,8 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                 * length 0 means transfer 0 block of data.
                 * However, for ATA R/W commands, sector count 0 means
                 * 256 or 65536 sectors, not 0 sectors as in SCSI.
+                *
+                * WARNING: one or two older ATA drives treat 0 as 0...
                 */
                goto nothing_to_do;
 
@@ -1099,7 +1162,8 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                        tf->device |= (block >> 24) & 0xf;
                }
 
-               ata_rwcmd_protocol(qc);
+               if (unlikely(ata_rwcmd_protocol(qc) < 0))
+                       goto invalid_fld;
 
                qc->nsect = n_block;
                tf->nsect = n_block & 0xff;
@@ -1117,7 +1181,8 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                if ((block >> 28) || (n_block > 256))
                        goto out_of_range;
 
-               ata_rwcmd_protocol(qc);
+               if (unlikely(ata_rwcmd_protocol(qc) < 0))
+                       goto invalid_fld;
 
                /* Convert LBA to CHS */
                track = (u32)block / dev->sectors;
@@ -1160,10 +1225,11 @@ nothing_to_do:
        return 1;
 }
 
-static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
+static int ata_scsi_qc_complete(struct ata_queued_cmd *qc)
 {
        struct scsi_cmnd *cmd = qc->scsicmd;
-       int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
+       u8 *cdb = cmd->cmnd;
+       int need_sense = (qc->err_mask != 0);
 
        /* For ATA pass thru (SAT) commands, generate a sense block if
         * user mandated it or if there's an error.  Note that if we
@@ -1172,8 +1238,8 @@ static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
         * whether the command completed successfully or not. If there
         * was no error, SK, ASC and ASCQ will all be zero.
         */
-       if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
-           ((cmd->cmnd[2] & 0x20) || need_sense)) {
+       if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
+           ((cdb[2] & 0x20) || need_sense)) {
                ata_gen_ata_desc_sense(qc);
        } else {
                if (!need_sense) {
@@ -1487,7 +1553,7 @@ unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
        return 0;
 }
 
-static const char *inq_83_str = "Linux ATA-SCSI simulator";
+static const char * const inq_83_str = "Linux ATA-SCSI simulator";
 
 /**
  *     ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
@@ -1583,13 +1649,9 @@ static void ata_msense_push(u8 **ptr_io, const u8 *last,
 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
                                       const u8 *last)
 {
-       u8 page[] = {
-               0x8,                            /* page code */
-               0x12,                           /* page length */
-               0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 10 zeroes */
-               0, 0, 0, 0, 0, 0, 0, 0          /* 8 zeroes */
-       };
+       u8 page[CACHE_MPAGE_LEN];
 
+       memcpy(page, def_cache_mpage, sizeof(page));
        if (ata_id_wcache_enabled(id))
                page[2] |= (1 << 2);    /* write cache enable */
        if (!ata_id_rahead_enabled(id))
@@ -1613,15 +1675,9 @@ static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
 
 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
 {
-       const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
-
-       /* byte 2: set the descriptor format sense data bit (bit 2)
-        * since we need to support returning this format for SAT
-        * commands and any SCSI commands against a 48b LBA device.
-        */
-
-       ata_msense_push(ptr_io, last, page, sizeof(page));
-       return sizeof(page);
+       ata_msense_push(ptr_io, last, def_control_mpage,
+                       sizeof(def_control_mpage));
+       return sizeof(def_control_mpage);
 }
 
 /**
@@ -1638,15 +1694,10 @@ static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
 
 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
 {
-       const u8 page[] = {
-               0x1,                      /* page code */
-               0xa,                      /* page length */
-               (1 << 7) | (1 << 6),      /* note auto r/w reallocation */
-               0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
-       };
 
-       ata_msense_push(ptr_io, last, page, sizeof(page));
-       return sizeof(page);
+       ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
+                       sizeof(def_rw_recovery_mpage));
+       return sizeof(def_rw_recovery_mpage);
 }
 
 /**
@@ -1655,7 +1706,9 @@ static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  *     @buflen: Response buffer length.
  *
- *     Simulate MODE SENSE commands.
+ *     Simulate MODE SENSE commands. Assume this is invoked for direct
+ *     access devices (e.g. disks) only. There should be no block
+ *     descriptor for other device types.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host_set lock)
@@ -1664,16 +1717,25 @@ static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
                                  unsigned int buflen)
 {
+       struct ata_device *dev = args->dev;
        u8 *scsicmd = args->cmd->cmnd, *p, *last;
-       unsigned int page_control, six_byte, output_len;
+       const u8 sat_blk_desc[] = {
+               0, 0, 0, 0,     /* number of blocks: sat unspecified */
+               0,
+               0, 0x2, 0x0     /* block length: 512 bytes */
+       };
+       u8 pg, spg;
+       unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
+       u8 dpofua;
 
        VPRINTK("ENTER\n");
 
        six_byte = (scsicmd[0] == MODE_SENSE);
-
-       /* we only support saved and current values (which we treat
-        * in the same manner)
+       ebd = !(scsicmd[1] & 0x8);      /* dbd bit inverted == edb */
+       /*
+        * LLBA bit in msense(10) ignored (compliant)
         */
+
        page_control = scsicmd[2] >> 6;
        switch (page_control) {
        case 0: /* current */
@@ -1686,29 +1748,42 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
                goto invalid_fld;
        }
 
-       if (six_byte)
-               output_len = 4;
-       else
-               output_len = 8;
+       if (six_byte) {
+               output_len = 4 + (ebd ? 8 : 0);
+               alloc_len = scsicmd[4];
+       } else {
+               output_len = 8 + (ebd ? 8 : 0);
+               alloc_len = (scsicmd[7] << 8) + scsicmd[8];
+       }
+       minlen = (alloc_len < buflen) ? alloc_len : buflen;
 
        p = rbuf + output_len;
-       last = rbuf + buflen - 1;
+       last = rbuf + minlen - 1;
 
-       switch(scsicmd[2] & 0x3f) {
-       case 0x01:              /* r/w error recovery */
+       pg = scsicmd[2] & 0x3f;
+       spg = scsicmd[3];
+       /*
+        * No mode subpages supported (yet) but asking for _all_
+        * subpages may be valid
+        */
+       if (spg && (spg != ALL_SUB_MPAGES))
+               goto invalid_fld;
+
+       switch(pg) {
+       case RW_RECOVERY_MPAGE:
                output_len += ata_msense_rw_recovery(&p, last);
                break;
 
-       case 0x08:              /* caching */
+       case CACHE_MPAGE:
                output_len += ata_msense_caching(args->id, &p, last);
                break;
 
-       case 0x0a: {            /* control mode */
+       case CONTROL_MPAGE: {
                output_len += ata_msense_ctl_mode(&p, last);
                break;
                }
 
-       case 0x3f:              /* all pages */
+       case ALL_MPAGES:
                output_len += ata_msense_rw_recovery(&p, last);
                output_len += ata_msense_caching(args->id, &p, last);
                output_len += ata_msense_ctl_mode(&p, last);
@@ -1718,15 +1793,41 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
                goto invalid_fld;
        }
 
+       if (minlen < 1)
+               return 0;
+
+       dpofua = 0;
+       if (ata_id_has_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
+           (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
+               dpofua = 1 << 4;
+
        if (six_byte) {
                output_len--;
                rbuf[0] = output_len;
+               if (minlen > 2)
+                       rbuf[2] |= dpofua;
+               if (ebd) {
+                       if (minlen > 3)
+                               rbuf[3] = sizeof(sat_blk_desc);
+                       if (minlen > 11)
+                               memcpy(rbuf + 4, sat_blk_desc,
+                                      sizeof(sat_blk_desc));
+               }
        } else {
                output_len -= 2;
                rbuf[0] = output_len >> 8;
-               rbuf[1] = output_len;
+               if (minlen > 1)
+                       rbuf[1] = output_len;
+               if (minlen > 3)
+                       rbuf[3] |= dpofua;
+               if (ebd) {
+                       if (minlen > 7)
+                               rbuf[7] = sizeof(sat_blk_desc);
+                       if (minlen > 15)
+                               memcpy(rbuf + 8, sat_blk_desc,
+                                      sizeof(sat_blk_desc));
+               }
        }
-
        return 0;
 
 invalid_fld:
@@ -1887,22 +1988,44 @@ void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8
        done(cmd);
 }
 
-void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
-                        struct scsi_cmnd *cmd)
+static int atapi_sense_complete(struct ata_queued_cmd *qc)
 {
-       DECLARE_COMPLETION(wait);
-       struct ata_queued_cmd *qc;
-       unsigned long flags;
-       int rc;
+       if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0))
+               /* FIXME: not quite right; we don't want the
+                * translation of taskfile registers into
+                * a sense descriptors, since that's only
+                * correct for ATA, not ATAPI
+                */
+               ata_gen_ata_desc_sense(qc);
 
-       DPRINTK("ATAPI request sense\n");
+       qc->scsidone(qc->scsicmd);
+       return 0;
+}
 
-       qc = ata_qc_new_init(ap, dev);
-       BUG_ON(qc == NULL);
+/* is it pointless to prefer PIO for "safety reasons"? */
+static inline int ata_pio_use_silly(struct ata_port *ap)
+{
+       return (ap->flags & ATA_FLAG_PIO_DMA);
+}
+
+static void atapi_request_sense(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       struct scsi_cmnd *cmd = qc->scsicmd;
+
+       DPRINTK("ATAPI request sense\n");
 
        /* FIXME: is this needed? */
        memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
 
+       ap->ops->tf_read(ap, &qc->tf);
+
+       /* fill these in, for the case where they are -not- overwritten */
+       cmd->sense_buffer[0] = 0x70;
+       cmd->sense_buffer[2] = qc->tf.feature >> 4;
+
+       ata_qc_reinit(qc);
+
        ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
        qc->dma_dir = DMA_FROM_DEVICE;
 
@@ -1913,33 +2036,40 @@ void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
        qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
        qc->tf.command = ATA_CMD_PACKET;
 
-       qc->tf.protocol = ATA_PROT_ATAPI;
-       qc->tf.lbam = (8 * 1024) & 0xff;
-       qc->tf.lbah = (8 * 1024) >> 8;
+       if (ata_pio_use_silly(ap)) {
+               qc->tf.protocol = ATA_PROT_ATAPI_DMA;
+               qc->tf.feature |= ATAPI_PKT_DMA;
+       } else {
+               qc->tf.protocol = ATA_PROT_ATAPI;
+               qc->tf.lbam = (8 * 1024) & 0xff;
+               qc->tf.lbah = (8 * 1024) >> 8;
+       }
        qc->nbytes = SCSI_SENSE_BUFFERSIZE;
 
-       qc->waiting = &wait;
-       qc->complete_fn = ata_qc_complete_noop;
+       qc->complete_fn = atapi_sense_complete;
 
-       spin_lock_irqsave(&ap->host_set->lock, flags);
-       rc = ata_qc_issue(qc);
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
-
-       if (rc)
-               ata_port_disable(ap);
-       else
-               wait_for_completion(&wait);
+       if (ata_qc_issue(qc)) {
+               qc->err_mask |= AC_ERR_OTHER;
+               ata_qc_complete(qc);
+       }
 
        DPRINTK("EXIT\n");
 }
 
-static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
+static int atapi_qc_complete(struct ata_queued_cmd *qc)
 {
        struct scsi_cmnd *cmd = qc->scsicmd;
+       unsigned int err_mask = qc->err_mask;
+
+       VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
 
-       VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat);
+       if (unlikely(err_mask & AC_ERR_DEV)) {
+               cmd->result = SAM_STAT_CHECK_CONDITION;
+               atapi_request_sense(qc);
+               return 1;
+       }
 
-       if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ)))
+       else if (unlikely(err_mask))
                /* FIXME: not quite right; we don't want the
                 * translation of taskfile registers into
                 * a sense descriptors, since that's only
@@ -1947,27 +2077,10 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
                 */
                ata_gen_ata_desc_sense(qc);
 
-       else if (unlikely(drv_stat & ATA_ERR)) {
-               DPRINTK("request check condition\n");
-
-               /* FIXME: command completion with check condition
-                * but no sense causes the error handler to run,
-                * which then issues REQUEST SENSE, fills in the sense 
-                * buffer, and completes the command (for the second
-                * time).  We need to issue REQUEST SENSE some other
-                * way, to avoid completing the command twice.
-                */
-               cmd->result = SAM_STAT_CHECK_CONDITION;
-
-               qc->scsidone(cmd);
-
-               return 1;
-       }
-
        else {
                u8 *scsicmd = cmd->cmnd;
 
-               if (scsicmd[0] == INQUIRY) {
+               if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
                        u8 *buf = NULL;
                        unsigned int buflen;
 
@@ -1981,9 +2094,6 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
         * device.  2) Ensure response data format / ATAPI information
         * are always correct.
         */
-       /* FIXME: do we ever override EVPD pages and the like, with
-        * this code?
-        */
                        if (buf[2] == 0) {
                                buf[2] = 0x5;
                                buf[3] = 0x32;
@@ -2096,9 +2206,12 @@ ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
        if (unlikely(!ata_dev_present(dev)))
                return NULL;
 
-       if (!atapi_enabled) {
-               if (unlikely(dev->class == ATA_DEV_ATAPI))
+       if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
+               if (unlikely(dev->class == ATA_DEV_ATAPI)) {
+                       printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n",
+                              ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled");
                        return NULL;
+               }
        }
 
        return dev;
@@ -2148,7 +2261,7 @@ ata_scsi_map_proto(u8 byte1)
 /**
  *     ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  *     @qc: command structure to be initialized
- *     @cmd: SCSI command to convert
+ *     @scsicmd: SCSI command to convert
  *
  *     Handles either 12 or 16-byte versions of the CDB.
  *
@@ -2156,13 +2269,13 @@ ata_scsi_map_proto(u8 byte1)
  *     Zero on success, non-zero on failure.
  */
 static unsigned int
-ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
+ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
 {
        struct ata_taskfile *tf = &(qc->tf);
        struct scsi_cmnd *cmd = qc->scsicmd;
 
        if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
-               return 1;
+               goto invalid_fld;
 
        /*
         * 12 and 16 byte CDBs use different offsets to
@@ -2208,6 +2321,12 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
                tf->device = scsicmd[8];
                tf->command = scsicmd[9];
        }
+       /*
+        * If slave is possible, enforce correct master/slave bit
+       */
+       if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
+               tf->device = qc->dev->devno ?
+                       tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
 
        /*
         * Filter SET_FEATURES - XFER MODE command -- otherwise,
@@ -2218,7 +2337,7 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
         */
        if ((tf->command == ATA_CMD_SET_FEATURES)
         && (tf->feature == SETFEATURES_XFER))
-               return 1;
+               goto invalid_fld;
 
        /*
         * Set flags so that all registers will be written,
@@ -2239,6 +2358,11 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
        qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
 
        return 0;
+
+ invalid_fld:
+       ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
+       /* "Invalid field in cdb" */
+       return 1;
 }
 
 /**
@@ -2333,8 +2457,12 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
        struct ata_port *ap;
        struct ata_device *dev;
        struct scsi_device *scsidev = cmd->device;
+       struct Scsi_Host *shost = scsidev->host;
 
-       ap = (struct ata_port *) &scsidev->host->hostdata[0];
+       ap = (struct ata_port *) &shost->hostdata[0];
+
+       spin_unlock(shost->host_lock);
+       spin_lock(&ap->host_set->lock);
 
        ata_scsi_dump_cdb(ap, cmd);
 
@@ -2352,11 +2480,13 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
                if (xlat_func)
                        ata_scsi_translate(ap, dev, cmd, done, xlat_func);
                else
-                       ata_scsi_simulate(dev->id, cmd, done);
+                       ata_scsi_simulate(ap, dev, cmd, done);
        } else
                ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
 
 out_unlock:
+       spin_unlock(&ap->host_set->lock);
+       spin_lock(shost->host_lock);
        return 0;
 }
 
@@ -2373,14 +2503,16 @@ out_unlock:
  *     spin_lock_irqsave(host_set lock)
  */
 
-void ata_scsi_simulate(u16 *id,
+void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
                      struct scsi_cmnd *cmd,
                      void (*done)(struct scsi_cmnd *))
 {
        struct ata_scsi_args args;
        const u8 *scsicmd = cmd->cmnd;
 
-       args.id = id;
+       args.ap = ap;
+       args.dev = dev;
+       args.id = dev->id;
        args.cmd = cmd;
        args.done = done;