]> err.no Git - linux-2.6/blobdiff - drivers/scsi/libata-scsi.c
[PATCH] libata-ncq: pass ata_scsi_translate() return value to SCSI midlayer
[linux-2.6] / drivers / scsi / libata-scsi.c
index a1259b242b8ec9cf41fa72dd21474e62c576484b..96517ca021e334b592c1f8c63f1798444d0fa1d4 100644 (file)
@@ -53,7 +53,6 @@
 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
 static struct ata_device *
 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
-enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd);
 
 #define RW_RECOVERY_MPAGE 0x1
 #define RW_RECOVERY_MPAGE_LEN 12
@@ -99,6 +98,7 @@ static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  * It just needs the eh_timed_out hook.
  */
 struct scsi_transport_template ata_scsi_transport_template = {
+       .eh_strategy_handler    = ata_scsi_error,
        .eh_timed_out           = ata_scsi_timed_out,
 };
 
@@ -256,7 +256,7 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
        scsi_cmd[14] = args[0];
 
        /* Good values for timeout and retries?  Values below
-          from scsi_ioctl_send_command() for default case... */        
+          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;
@@ -267,20 +267,8 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
 
 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
 {
-       struct ata_port *ap;
-       struct ata_device *dev;
        int val = -EINVAL, rc = -EINVAL;
 
-       ap = (struct ata_port *) &scsidev->host->hostdata[0];
-       if (!ap)
-               goto out;
-
-       dev = ata_scsi_find_dev(ap, scsidev);
-       if (!dev) {
-               rc = -ENODEV;
-               goto out;
-       }
-
        switch (cmd) {
        case ATA_IOC_GET_IO32:
                val = 0;
@@ -309,13 +297,11 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
                break;
        }
 
-out:
        return rc;
 }
 
 /**
  *     ata_scsi_qc_new - acquire new ata_queued_cmd reference
- *     @ap: ATA port to which the new command is attached
  *     @dev: ATA device to which the new command is attached
  *     @cmd: SCSI command that originated this ATA command
  *     @done: SCSI command completion function
@@ -334,14 +320,13 @@ out:
  *     RETURNS:
  *     Command allocated, or %NULL if none available.
  */
-struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
-                                      struct ata_device *dev,
+struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
                                       struct scsi_cmnd *cmd,
                                       void (*done)(struct scsi_cmnd *))
 {
        struct ata_queued_cmd *qc;
 
-       qc = ata_qc_new_init(ap, dev);
+       qc = ata_qc_new_init(dev);
        if (qc) {
                qc->scsicmd = cmd;
                qc->scsidone = done;
@@ -408,18 +393,18 @@ 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_port *ap = ata_shost_to_port(sdev->host);
        struct ata_device *dev = &ap->device[sdev->id];
 
-       return ata_device_resume(ap, dev);
+       return ata_device_resume(dev);
 }
 
-int ata_scsi_device_suspend(struct scsi_device *sdev)
+int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
 {
-       struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
+       struct ata_port *ap = ata_shost_to_port(sdev->host);
        struct ata_device *dev = &ap->device[sdev->id];
 
-       return ata_device_suspend(ap, dev);
+       return ata_device_suspend(dev, state);
 }
 
 /**
@@ -430,6 +415,7 @@ int ata_scsi_device_suspend(struct scsi_device *sdev)
  *     @sk: the sense key we'll fill out
  *     @asc: the additional sense code we'll fill out
  *     @ascq: the additional sense code qualifier we'll fill out
+ *     @verbose: be verbose
  *
  *     Converts an ATA error into a SCSI error.  Fill out pointers to
  *     SK, ASC, and ASCQ bytes for later use in fixed or descriptor
@@ -438,8 +424,8 @@ int ata_scsi_device_suspend(struct scsi_device *sdev)
  *     LOCKING:
  *     spin_lock_irqsave(host_set lock)
  */
-void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc, 
-                       u8 *ascq)
+void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
+                       u8 *ascq, int verbose)
 {
        int i;
 
@@ -495,7 +481,7 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
                /* Look for drv_err */
                for (i = 0; sense_table[i][0] != 0xFF; i++) {
                        /* Look for best matches first */
-                       if ((sense_table[i][0] & drv_err) == 
+                       if ((sense_table[i][0] & drv_err) ==
                            sense_table[i][0]) {
                                *sk = sense_table[i][1];
                                *asc = sense_table[i][2];
@@ -504,8 +490,9 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
                        }
                }
                /* No immediate match */
-               printk(KERN_WARNING "ata%u: no sense translation for "
-                      "error 0x%02x\n", id, drv_err);
+               if (verbose)
+                       printk(KERN_WARNING "ata%u: no sense translation for "
+                              "error 0x%02x\n", id, drv_err);
        }
 
        /* Fall back to interpreting status bits */
@@ -518,8 +505,9 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
                }
        }
        /* No error?  Undecoded? */
-       printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n", 
-              id, drv_stat);
+       if (verbose)
+               printk(KERN_WARNING "ata%u: no sense translation for "
+                      "status: 0x%02x\n", id, drv_stat);
 
        /* We need a sensible error return here, which is tricky, and one
           that won't cause people to do things like return a disk wrongly */
@@ -528,9 +516,10 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
        *ascq = 0x00;
 
  translate_done:
-       printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
-              "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
-              *sk, *asc, *ascq);
+       if (verbose)
+               printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
+                      "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
+                      id, drv_stat, drv_err, *sk, *asc, *ascq);
        return;
 }
 
@@ -550,27 +539,23 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
 {
        struct scsi_cmnd *cmd = qc->scsicmd;
-       struct ata_taskfile *tf = &qc->tf;
+       struct ata_taskfile *tf = &qc->result_tf;
        unsigned char *sb = cmd->sense_buffer;
        unsigned char *desc = sb + 8;
+       int verbose = qc->ap->ops->error_handler == NULL;
 
        memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
 
        cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
 
-       /*
-        * Read the controller registers.
-        */
-       WARN_ON(qc->ap->ops->tf_read == NULL);
-       qc->ap->ops->tf_read(qc->ap, tf);
-
        /*
         * Use ata_to_sense_error() to map status register bits
         * onto sense key, asc & ascq.
         */
-       if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
+       if (qc->err_mask ||
+           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], &sb[2], &sb[3], verbose);
                sb[1] &= 0x0f;
        }
 
@@ -626,26 +611,22 @@ void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
 {
        struct scsi_cmnd *cmd = qc->scsicmd;
-       struct ata_taskfile *tf = &qc->tf;
+       struct ata_taskfile *tf = &qc->result_tf;
        unsigned char *sb = cmd->sense_buffer;
+       int verbose = qc->ap->ops->error_handler == NULL;
 
        memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
 
        cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
 
-       /*
-        * Read the controller registers.
-        */
-       WARN_ON(qc->ap->ops->tf_read == NULL);
-       qc->ap->ops->tf_read(qc->ap, tf);
-
        /*
         * Use ata_to_sense_error() to map status register bits
         * onto sense key, asc & ascq.
         */
-       if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
+       if (qc->err_mask ||
+           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], &sb[12], &sb[13], verbose);
                sb[2] &= 0x0f;
        }
 
@@ -727,7 +708,7 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
                struct ata_port *ap;
                struct ata_device *dev;
 
-               ap = (struct ata_port *) &sdev->host->hostdata[0];
+               ap = ata_shost_to_port(sdev->host);
                dev = &ap->device[sdev->id];
 
                ata_scsi_dev_config(sdev, dev);
@@ -736,141 +717,6 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
        return 0;       /* scsi layer doesn't check return value, sigh */
 }
 
-/**
- *     ata_scsi_timed_out - SCSI layer time out callback
- *     @cmd: timed out SCSI command
- *
- *     Handles SCSI layer timeout.  We race with normal completion of
- *     the qc for @cmd.  If the qc is already gone, we lose and let
- *     the scsi command finish (EH_HANDLED).  Otherwise, the qc has
- *     timed out and EH should be invoked.  Prevent ata_qc_complete()
- *     from finishing it by setting EH_SCHEDULED and return
- *     EH_NOT_HANDLED.
- *
- *     LOCKING:
- *     Called from timer context
- *
- *     RETURNS:
- *     EH_HANDLED or EH_NOT_HANDLED
- */
-enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
-{
-       struct Scsi_Host *host = cmd->device->host;
-       struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
-       unsigned long flags;
-       struct ata_queued_cmd *qc;
-       enum scsi_eh_timer_return ret = EH_HANDLED;
-
-       DPRINTK("ENTER\n");
-
-       spin_lock_irqsave(&ap->host_set->lock, flags);
-       qc = ata_qc_from_tag(ap, ap->active_tag);
-       if (qc) {
-               WARN_ON(qc->scsicmd != cmd);
-               qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
-               qc->err_mask |= AC_ERR_TIMEOUT;
-               ret = EH_NOT_HANDLED;
-       }
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
-
-       DPRINTK("EXIT, ret=%d\n", ret);
-       return ret;
-}
-
-/**
- *     ata_scsi_error - SCSI layer error handler callback
- *     @host: SCSI host on which error occurred
- *
- *     Handles SCSI-layer-thrown error events.
- *
- *     LOCKING:
- *     Inherited from SCSI layer (none, can sleep)
- *
- *     RETURNS:
- *     Zero.
- */
-
-int ata_scsi_error(struct Scsi_Host *host)
-{
-       struct ata_port *ap;
-       unsigned long flags;
-
-       DPRINTK("ENTER\n");
-
-       ap = (struct ata_port *) &host->hostdata[0];
-
-       spin_lock_irqsave(&ap->host_set->lock, flags);
-       WARN_ON(ap->flags & ATA_FLAG_IN_EH);
-       ap->flags |= ATA_FLAG_IN_EH;
-       WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
-
-       ata_port_flush_task(ap);
-
-       ap->ops->eng_timeout(ap);
-
-       WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
-
-       scsi_eh_flush_done_q(&ap->eh_done_q);
-
-       spin_lock_irqsave(&ap->host_set->lock, flags);
-       ap->flags &= ~ATA_FLAG_IN_EH;
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
-
-       DPRINTK("EXIT\n");
-       return 0;
-}
-
-static void ata_eh_scsidone(struct scsi_cmnd *scmd)
-{
-       /* nada */
-}
-
-static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       struct scsi_cmnd *scmd = qc->scsicmd;
-       unsigned long flags;
-
-       spin_lock_irqsave(&ap->host_set->lock, flags);
-       qc->scsidone = ata_eh_scsidone;
-       __ata_qc_complete(qc);
-       WARN_ON(ata_tag_valid(qc->tag));
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
-
-       scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
-}
-
-/**
- *     ata_eh_qc_complete - Complete an active ATA command from EH
- *     @qc: Command to complete
- *
- *     Indicate to the mid and upper layers that an ATA command has
- *     completed.  To be used from EH.
- */
-void ata_eh_qc_complete(struct ata_queued_cmd *qc)
-{
-       struct scsi_cmnd *scmd = qc->scsicmd;
-       scmd->retries = scmd->allowed;
-       __ata_eh_qc_complete(qc);
-}
-
-/**
- *     ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
- *     @qc: Command to retry
- *
- *     Indicate to the mid and upper layers that an ATA command
- *     should be retried.  To be used from EH.
- *
- *     SCSI midlayer limits the number of retries to scmd->allowed.
- *     This function might need to adjust scmd->retries for commands
- *     which get retried due to unrelated NCQ failures.
- */
-void ata_eh_qc_retry(struct ata_queued_cmd *qc)
-{
-       __ata_eh_qc_complete(qc);
-}
-
 /**
  *     ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  *     @qc: Storage for translated ATA taskfile
@@ -906,7 +752,7 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
                tf->nsect = 1;  /* 1 sector, lba=0 */
 
                if (qc->dev->flags & ATA_DFLAG_LBA) {
-                       qc->tf.flags |= ATA_TFLAG_LBA;
+                       tf->flags |= ATA_TFLAG_LBA;
 
                        tf->lbah = 0x0;
                        tf->lbam = 0x0;
@@ -1150,14 +996,14 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *sc
 
                DPRINTK("block %u track %u cyl %u head %u sect %u\n",
                        (u32)block, track, cyl, head, sect);
-               
-               /* Check whether the converted CHS can fit. 
-                  Cylinder: 0-65535 
+
+               /* Check whether the converted CHS can fit.
+                  Cylinder: 0-65535
                   Head: 0-15
                   Sector: 1-255*/
-               if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) 
+               if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
                        goto out_of_range;
-               
+
                tf->command = ATA_CMD_VERIFY;
                tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
                tf->lbal = sect;
@@ -1210,6 +1056,7 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
        u64 block;
        u32 n_block;
 
+       qc->flags |= ATA_QCFLAG_IO;
        tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
 
        if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
@@ -1289,7 +1136,7 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                tf->lbal = block & 0xff;
 
                tf->device |= ATA_LBA;
-       } else { 
+       } else {
                /* CHS */
                u32 sect, head, cyl, track;
 
@@ -1309,8 +1156,8 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
                DPRINTK("block %u track %u cyl %u head %u sect %u\n",
                        (u32)block, track, cyl, head, sect);
 
-               /* Check whether the converted CHS can fit. 
-                  Cylinder: 0-65535 
+               /* Check whether the converted CHS can fit.
+                  Cylinder: 0-65535
                   Head: 0-15
                   Sector: 1-255*/
                if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
@@ -1371,10 +1218,8 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
                }
        }
 
-       if (need_sense) {
-               /* The ata_gen_..._sense routines fill in tf */
-               ata_dump_status(qc->ap->id, &qc->tf);
-       }
+       if (need_sense && !qc->ap->ops->error_handler)
+               ata_dump_status(qc->ap->id, &qc->result_tf);
 
        qc->scsidone(cmd);
 
@@ -1383,7 +1228,6 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
 
 /**
  *     ata_scsi_translate - Translate then issue SCSI command to ATA device
- *     @ap: ATA port to which the command is addressed
  *     @dev: ATA device to which the command is addressed
  *     @cmd: SCSI command to execute
  *     @done: SCSI command completion function
@@ -1404,10 +1248,12 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  *
  *     LOCKING:
  *     spin_lock_irqsave(host_set lock)
+ *
+ *     RETURNS:
+ *     0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
+ *     needs to be deferred.
  */
-
-static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
-                             struct scsi_cmnd *cmd,
+static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
                              void (*done)(struct scsi_cmnd *),
                              ata_xlat_func_t xlat_func)
 {
@@ -1416,7 +1262,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
 
        VPRINTK("ENTER\n");
 
-       qc = ata_scsi_qc_new(ap, dev, cmd, done);
+       qc = ata_scsi_qc_new(dev, cmd, done);
        if (!qc)
                goto err_mem;
 
@@ -1424,8 +1270,8 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
        if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
            cmd->sc_data_direction == DMA_TO_DEVICE) {
                if (unlikely(cmd->request_bufflen < 1)) {
-                       printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
-                              ap->id, dev->devno);
+                       ata_dev_printk(dev, KERN_WARNING,
+                                      "WARNING: zero len r/w req\n");
                        goto err_did;
                }
 
@@ -1444,18 +1290,16 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
                goto early_finish;
 
        /* select device, send command to hardware */
-       qc->err_mask = ata_qc_issue(qc);
-       if (qc->err_mask)
-               ata_qc_complete(qc);
+       ata_qc_issue(qc);
 
        VPRINTK("EXIT\n");
-       return;
+       return 0;
 
 early_finish:
         ata_qc_free(qc);
        done(cmd);
        DPRINTK("EXIT - early finish (good or error)\n");
-       return;
+       return 0;
 
 err_did:
        ata_qc_free(qc);
@@ -1463,7 +1307,7 @@ err_mem:
        cmd->result = (DID_ERROR << 16);
        done(cmd);
        DPRINTK("EXIT - internal\n");
-       return;
+       return 0;
 }
 
 /**
@@ -1697,7 +1541,7 @@ unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
 
        if (buflen > (ATA_SERNO_LEN + num + 3)) {
                /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
-               rbuf[num + 0] = 2;      
+               rbuf[num + 0] = 2;
                rbuf[num + 3] = ATA_SERNO_LEN;
                num += 4;
                ata_id_string(args->id, (unsigned char *) rbuf + num,
@@ -1707,8 +1551,8 @@ unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
        if (buflen > (sat_model_serial_desc_len + num + 3)) {
                /* SAT defined lu model and serial numbers descriptor */
                /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
-               rbuf[num + 0] = 2;      
-               rbuf[num + 1] = 1;      
+               rbuf[num + 0] = 2;
+               rbuf[num + 1] = 1;
                rbuf[num + 3] = sat_model_serial_desc_len;
                num += 4;
                memcpy(rbuf + num, "ATA     ", 8);
@@ -2154,13 +1998,14 @@ void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8
 
 static void atapi_sense_complete(struct ata_queued_cmd *qc)
 {
-       if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0))
+       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);
+       }
 
        qc->scsidone(qc->scsicmd);
        ata_qc_free(qc);
@@ -2212,9 +2057,7 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
 
        qc->complete_fn = atapi_sense_complete;
 
-       qc->err_mask = ata_qc_issue(qc);
-       if (qc->err_mask)
-               ata_qc_complete(qc);
+       ata_qc_issue(qc);
 
        DPRINTK("EXIT\n");
 }
@@ -2226,21 +2069,38 @@ static void atapi_qc_complete(struct ata_queued_cmd *qc)
 
        VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
 
+       /* handle completion from new EH */
+       if (unlikely(qc->ap->ops->error_handler &&
+                    (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
+
+               if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
+                       /* 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);
+               }
+
+               qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
+               qc->scsidone(cmd);
+               ata_qc_free(qc);
+               return;
+       }
+
+       /* successful completion or old EH failure path */
        if (unlikely(err_mask & AC_ERR_DEV)) {
                cmd->result = SAM_STAT_CHECK_CONDITION;
                atapi_request_sense(qc);
                return;
-       }
-
-       else if (unlikely(err_mask))
+       } 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
                 * correct for ATA, not ATAPI
                 */
                ata_gen_ata_desc_sense(qc);
-
-       else {
+       } else {
                u8 *scsicmd = cmd->cmnd;
 
                if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
@@ -2322,11 +2182,9 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
                qc->tf.protocol = ATA_PROT_ATAPI_DMA;
                qc->tf.feature |= ATAPI_PKT_DMA;
 
-#ifdef ATAPI_ENABLE_DMADIR
-               /* some SATA bridges need us to indicate data xfer direction */
-               if (cmd->sc_data_direction != DMA_TO_DEVICE)
+               if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
+                       /* some SATA bridges need us to indicate data xfer direction */
                        qc->tf.feature |= ATAPI_DMADIR;
-#endif
        }
 
        qc->nbytes = cmd->bufflen;
@@ -2366,13 +2224,14 @@ ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
                     (scsidev->lun != 0)))
                return NULL;
 
-       if (unlikely(!ata_dev_present(dev)))
+       if (unlikely(!ata_dev_enabled(dev)))
                return NULL;
 
        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");
+                       ata_dev_printk(dev, KERN_WARNING,
+                               "WARNING: ATAPI is %s, device ignored.\n",
+                               atapi_enabled ? "not supported with this driver" : "disabled");
                        return NULL;
                }
        }
@@ -2521,6 +2380,9 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
         */
        qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
 
+       /* request result TF */
+       qc->flags |= ATA_QCFLAG_RESULT_TF;
+
        return 0;
 
  invalid_fld:
@@ -2597,6 +2459,26 @@ static inline void ata_scsi_dump_cdb(struct ata_port *ap,
 #endif
 }
 
+static inline int __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
+                                     void (*done)(struct scsi_cmnd *),
+                                     struct ata_device *dev)
+{
+       int rc = 0;
+
+       if (dev->class == ATA_DEV_ATA) {
+               ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
+                                                             cmd->cmnd[0]);
+
+               if (xlat_func)
+                       rc = ata_scsi_translate(dev, cmd, done, xlat_func);
+               else
+                       ata_scsi_simulate(dev, cmd, done);
+       } else
+               rc = ata_scsi_translate(dev, cmd, done, atapi_xlat);
+
+       return rc;
+}
+
 /**
  *     ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  *     @cmd: SCSI command to be sent
@@ -2613,17 +2495,18 @@ static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  *     Releases scsi-layer-held lock, and obtains host_set lock.
  *
  *     RETURNS:
- *     Zero.
+ *     Return value from __ata_scsi_queuecmd() if @cmd can be queued,
+ *     0 otherwise.
  */
-
 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;
+       int rc = 0;
 
-       ap = (struct ata_port *) &shost->hostdata[0];
+       ap = ata_shost_to_port(shost);
 
        spin_unlock(shost->host_lock);
        spin_lock(&ap->host_set->lock);
@@ -2631,32 +2514,20 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
        ata_scsi_dump_cdb(ap, cmd);
 
        dev = ata_scsi_find_dev(ap, scsidev);
-       if (unlikely(!dev)) {
+       if (likely(dev))
+               rc = __ata_scsi_queuecmd(cmd, done, dev);
+       else {
                cmd->result = (DID_BAD_TARGET << 16);
                done(cmd);
-               goto out_unlock;
        }
 
-       if (dev->class == ATA_DEV_ATA) {
-               ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
-                                                             cmd->cmnd[0]);
-
-               if (xlat_func)
-                       ata_scsi_translate(ap, dev, cmd, done, xlat_func);
-               else
-                       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;
+       return rc;
 }
 
 /**
  *     ata_scsi_simulate - simulate SCSI command on ATA device
- *     @ap: port the device is connected to
  *     @dev: the target device
  *     @cmd: SCSI command being sent to device.
  *     @done: SCSI command completion function.
@@ -2668,14 +2539,12 @@ out_unlock:
  *     spin_lock_irqsave(host_set lock)
  */
 
-void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
-                     struct scsi_cmnd *cmd,
+void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
                      void (*done)(struct scsi_cmnd *))
 {
        struct ata_scsi_args args;
        const u8 *scsicmd = cmd->cmnd;
 
-       args.ap = ap;
        args.dev = dev;
        args.id = dev->id;
        args.cmd = cmd;
@@ -2750,14 +2619,37 @@ void ata_scsi_scan_host(struct ata_port *ap)
        struct ata_device *dev;
        unsigned int i;
 
-       if (ap->flags & ATA_FLAG_PORT_DISABLED)
+       if (ap->flags & ATA_FLAG_DISABLED)
                return;
 
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
                dev = &ap->device[i];
 
-               if (ata_dev_present(dev))
+               if (ata_dev_enabled(dev))
                        scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
        }
 }
 
+/**
+ *     ata_schedule_scsi_eh - schedule EH for SCSI host
+ *     @shost: SCSI host to invoke error handling on.
+ *
+ *     Schedule SCSI EH without scmd.  This is a hack.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ **/
+void ata_schedule_scsi_eh(struct Scsi_Host *shost)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(shost->host_lock, flags);
+
+       if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
+           scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
+               shost->host_eh_scheduled++;
+               scsi_eh_wakeup(shost);
+       }
+
+       spin_unlock_irqrestore(shost->host_lock, flags);
+}