[PATCH] libata-ncq: pass ata_scsi_translate() return value to SCSI midlayer
[cascardo/linux.git] / drivers / scsi / libata-scsi.c
index fcbf64e..96517ca 100644 (file)
@@ -415,6 +415,7 @@ int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
  *     @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
@@ -424,7 +425,7 @@ int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
  *     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)
+                       u8 *ascq, int verbose)
 {
        int i;
 
@@ -489,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 */
@@ -503,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 */
@@ -513,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;
 }
 
@@ -538,6 +542,7 @@ void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
        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);
 
@@ -550,7 +555,7 @@ void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
        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;
        }
 
@@ -608,6 +613,7 @@ void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
        struct scsi_cmnd *cmd = qc->scsicmd;
        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);
 
@@ -620,7 +626,7 @@ void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
        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;
        }
 
@@ -1212,7 +1218,7 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
                }
        }
 
-       if (need_sense)
+       if (need_sense && !qc->ap->ops->error_handler)
                ata_dump_status(qc->ap->id, &qc->result_tf);
 
        qc->scsidone(cmd);
@@ -1242,11 +1248,14 @@ 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_device *dev, struct scsi_cmnd *cmd,
-                              void (*done)(struct scsi_cmnd *),
-                              ata_xlat_func_t xlat_func)
+static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
+                             void (*done)(struct scsi_cmnd *),
+                             ata_xlat_func_t xlat_func)
 {
        struct ata_queued_cmd *qc;
        u8 *scsicmd = cmd->cmnd;
@@ -1261,8 +1270,8 @@ static void ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
        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",
-                              dev->ap->id, dev->devno);
+                       ata_dev_printk(dev, KERN_WARNING,
+                                      "WARNING: zero len r/w req\n");
                        goto err_did;
                }
 
@@ -1284,13 +1293,13 @@ static void ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
        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);
@@ -1298,7 +1307,7 @@ err_mem:
        cmd->result = (DID_ERROR << 16);
        done(cmd);
        DPRINTK("EXIT - internal\n");
-       return;
+       return 0;
 }
 
 /**
@@ -2060,6 +2069,26 @@ 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);
@@ -2200,8 +2229,9 @@ ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
 
        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;
                }
        }
@@ -2429,20 +2459,24 @@ static inline void ata_scsi_dump_cdb(struct ata_port *ap,
 #endif
 }
 
-static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
-                                      void (*done)(struct scsi_cmnd *),
-                                      struct ata_device *dev)
+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)
-                       ata_scsi_translate(dev, cmd, done, xlat_func);
+                       rc = ata_scsi_translate(dev, cmd, done, xlat_func);
                else
                        ata_scsi_simulate(dev, cmd, done);
        } else
-               ata_scsi_translate(dev, cmd, done, atapi_xlat);
+               rc = ata_scsi_translate(dev, cmd, done, atapi_xlat);
+
+       return rc;
 }
 
 /**
@@ -2461,15 +2495,16 @@ static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
  *     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 = ata_shost_to_port(shost);
 
@@ -2480,7 +2515,7 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
 
        dev = ata_scsi_find_dev(ap, scsidev);
        if (likely(dev))
-               __ata_scsi_queuecmd(cmd, done, dev);
+               rc = __ata_scsi_queuecmd(cmd, done, dev);
        else {
                cmd->result = (DID_BAD_TARGET << 16);
                done(cmd);
@@ -2488,7 +2523,7 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
 
        spin_unlock(&ap->host_set->lock);
        spin_lock(shost->host_lock);
-       return 0;
+       return rc;
 }
 
 /**
@@ -2595,3 +2630,26 @@ void ata_scsi_scan_host(struct ata_port *ap)
        }
 }
 
+/**
+ *     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);
+}