Merge branch 'master'
[pandora-kernel.git] / drivers / scsi / libata-scsi.c
index 5574520..957a9ca 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>
@@ -130,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:
@@ -146,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;
@@ -154,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]) {
@@ -171,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;
@@ -193,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;
        }
@@ -206,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);
 
@@ -216,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:
@@ -230,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;
@@ -249,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;
 }
 
@@ -415,6 +398,7 @@ void ata_dump_status(unsigned id, struct ata_taskfile *tf)
 
 /**
  *     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
@@ -985,9 +969,13 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *sc
        if (dev->flags & ATA_DFLAG_LBA) {
                tf->flags |= ATA_TFLAG_LBA;
 
-               if (dev->flags & ATA_DFLAG_LBA48) {
-                       if (n_block > (64 * 1024))
-                               goto invalid_fld;
+               if (lba_28_ok(block, n_block)) {
+                       /* use LBA28 */
+                       tf->command = ATA_CMD_VERIFY;
+                       tf->device |= (block >> 24) & 0xf;
+               } else if (lba_48_ok(block, n_block)) {
+                       if (!(dev->flags & ATA_DFLAG_LBA48))
+                               goto out_of_range;
 
                        /* use LBA48 */
                        tf->flags |= ATA_TFLAG_LBA48;
@@ -998,15 +986,9 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *sc
                        tf->hob_lbah = (block >> 40) & 0xff;
                        tf->hob_lbam = (block >> 32) & 0xff;
                        tf->hob_lbal = (block >> 24) & 0xff;
-               } else {
-                       if (n_block > 256)
-                               goto invalid_fld;
-
-                       /* use LBA28 */
-                       tf->command = ATA_CMD_VERIFY;
-
-                       tf->device |= (block >> 24) & 0xf;
-               }
+               } else
+                       /* request too large even for LBA48 */
+                       goto out_of_range;
 
                tf->nsect = n_block & 0xff;
 
@@ -1019,8 +1001,8 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *sc
                /* CHS */
                u32 sect, head, cyl, track;
 
-               if (n_block > 256)
-                       goto invalid_fld;
+               if (!lba_28_ok(block, n_block))
+                       goto out_of_range;
 
                /* Convert LBA to CHS */
                track = (u32)block / dev->sectors;
@@ -1127,15 +1109,19 @@ 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;
 
        if (dev->flags & ATA_DFLAG_LBA) {
                tf->flags |= ATA_TFLAG_LBA;
 
-               if (dev->flags & ATA_DFLAG_LBA48) {
-                       /* The request -may- be too large for LBA48. */
-                       if ((block >> 48) || (n_block > 65536))
+               if (lba_28_ok(block, n_block)) {
+                       /* use LBA28 */
+                       tf->device |= (block >> 24) & 0xf;
+               } else if (lba_48_ok(block, n_block)) {
+                       if (!(dev->flags & ATA_DFLAG_LBA48))
                                goto out_of_range;
 
                        /* use LBA48 */
@@ -1146,15 +1132,9 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                        tf->hob_lbah = (block >> 40) & 0xff;
                        tf->hob_lbam = (block >> 32) & 0xff;
                        tf->hob_lbal = (block >> 24) & 0xff;
-               } else { 
-                       /* use LBA28 */
-
-                       /* The request -may- be too large for LBA28. */
-                       if ((block >> 28) || (n_block > 256))
-                               goto out_of_range;
-
-                       tf->device |= (block >> 24) & 0xf;
-               }
+               } else
+                       /* request too large even for LBA48 */
+                       goto out_of_range;
 
                ata_rwcmd_protocol(qc);
 
@@ -1171,7 +1151,7 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                u32 sect, head, cyl, track;
 
                /* The request -may- be too large for CHS addressing. */
-               if ((block >> 28) || (n_block > 256))
+               if (!lba_28_ok(block, n_block))
                        goto out_of_range;
 
                ata_rwcmd_protocol(qc);
@@ -1969,22 +1949,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,unsigned int err_mask)
 {
-       DECLARE_COMPLETION(wait);
-       struct ata_queued_cmd *qc;
-       unsigned long flags;
-       int rc;
+       if (err_mask && ((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;
 
@@ -1995,22 +1997,20 @@ 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))
+               ata_qc_complete(qc, AC_ERR_OTHER);
 
        DPRINTK("EXIT\n");
 }
@@ -2022,19 +2022,8 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
        VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
 
        if (unlikely(err_mask & AC_ERR_DEV)) {
-               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);
-
+               atapi_request_sense(qc);
                return 1;
        }
 
@@ -2230,7 +2219,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.
  *
@@ -2290,6 +2279,12 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, const 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,
@@ -2415,8 +2410,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);
 
@@ -2439,6 +2438,8 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
                ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
 
 out_unlock:
+       spin_unlock(&ap->host_set->lock);
+       spin_lock(shost->host_lock);
        return 0;
 }