Merge ../linux-2.6
[pandora-kernel.git] / drivers / scsi / libata-scsi.c
index 32c1df6..e92c31d 100644 (file)
@@ -38,9 +38,9 @@
 #include <linux/spinlock.h>
 #include <scsi/scsi.h>
 #include <scsi/scsi_host.h>
+#include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_eh.h>
 #include <scsi/scsi_device.h>
-#include <scsi/scsi_request.h>
 #include <scsi/scsi_tcq.h>
 #include <scsi/scsi_transport.h>
 #include <linux/libata.h>
@@ -222,9 +222,7 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
         && copy_to_user(arg + sizeof(args), argbuf, argsize))
                rc = -EFAULT;
 error:
-       if (argbuf)
-               kfree(argbuf);
-
+       kfree(argbuf);
        return rc;
 }
 
@@ -399,20 +397,129 @@ void ata_dump_status(unsigned id, struct ata_taskfile *tf)
        }
 }
 
-int ata_scsi_device_resume(struct scsi_device *sdev)
+/**
+ *     ata_scsi_device_suspend - suspend ATA device associated with sdev
+ *     @sdev: the SCSI device to suspend
+ *     @state: target power management state
+ *
+ *     Request suspend EH action on the ATA device associated with
+ *     @sdev and wait for the operation to complete.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
+ */
+int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
 {
        struct ata_port *ap = ata_shost_to_port(sdev->host);
-       struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
+       struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
+       unsigned long flags;
+       unsigned int action;
+       int rc = 0;
+
+       if (!dev)
+               goto out;
+
+       spin_lock_irqsave(ap->lock, flags);
+
+       /* wait for the previous resume to complete */
+       while (dev->flags & ATA_DFLAG_SUSPENDED) {
+               spin_unlock_irqrestore(ap->lock, flags);
+               ata_port_wait_eh(ap);
+               spin_lock_irqsave(ap->lock, flags);
+       }
+
+       /* if @sdev is already detached, nothing to do */
+       if (sdev->sdev_state == SDEV_OFFLINE ||
+           sdev->sdev_state == SDEV_CANCEL || sdev->sdev_state == SDEV_DEL)
+               goto out_unlock;
+
+       /* request suspend */
+       action = ATA_EH_SUSPEND;
+       if (state.event != PM_EVENT_SUSPEND)
+               action |= ATA_EH_PM_FREEZE;
+       ap->eh_info.dev_action[dev->devno] |= action;
+       ap->eh_info.flags |= ATA_EHI_QUIET;
+       ata_port_schedule_eh(ap);
 
-       return ata_device_resume(dev);
+       spin_unlock_irqrestore(ap->lock, flags);
+
+       /* wait for EH to do the job */
+       ata_port_wait_eh(ap);
+
+       spin_lock_irqsave(ap->lock, flags);
+
+       /* If @sdev is still attached but the associated ATA device
+        * isn't suspended, the operation failed.
+        */
+       if (sdev->sdev_state != SDEV_OFFLINE &&
+           sdev->sdev_state != SDEV_CANCEL && sdev->sdev_state != SDEV_DEL &&
+           !(dev->flags & ATA_DFLAG_SUSPENDED))
+               rc = -EIO;
+
+ out_unlock:
+       spin_unlock_irqrestore(ap->lock, flags);
+ out:
+       if (rc == 0)
+               sdev->sdev_gendev.power.power_state = state;
+       return rc;
 }
 
-int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
+/**
+ *     ata_scsi_device_resume - resume ATA device associated with sdev
+ *     @sdev: the SCSI device to resume
+ *
+ *     Request resume EH action on the ATA device associated with
+ *     @sdev and return immediately.  This enables parallel
+ *     wakeup/spinup of devices.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep).
+ *
+ *     RETURNS:
+ *     0.
+ */
+int ata_scsi_device_resume(struct scsi_device *sdev)
 {
        struct ata_port *ap = ata_shost_to_port(sdev->host);
-       struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
+       struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
+       struct ata_eh_info *ehi = &ap->eh_info;
+       unsigned long flags;
+       unsigned int action;
 
-       return ata_device_suspend(dev, state);
+       if (!dev)
+               goto out;
+
+       spin_lock_irqsave(ap->lock, flags);
+
+       /* if @sdev is already detached, nothing to do */
+       if (sdev->sdev_state == SDEV_OFFLINE ||
+           sdev->sdev_state == SDEV_CANCEL || sdev->sdev_state == SDEV_DEL)
+               goto out_unlock;
+
+       /* request resume */
+       action = ATA_EH_RESUME;
+       if (sdev->sdev_gendev.power.power_state.event == PM_EVENT_SUSPEND)
+               __ata_ehi_hotplugged(ehi);
+       else
+               action |= ATA_EH_PM_FREEZE | ATA_EH_SOFTRESET;
+       ehi->dev_action[dev->devno] |= action;
+
+       /* We don't want autopsy and verbose EH messages.  Disable
+        * those if we're the only device on this link.
+        */
+       if (ata_port_max_devices(ap) == 1)
+               ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
+
+       ata_port_schedule_eh(ap);
+
+ out_unlock:
+       spin_unlock_irqrestore(ap->lock, flags);
+ out:
+       sdev->sdev_gendev.power.power_state = PMSG_ON;
+       return 0;
 }
 
 /**
@@ -752,7 +859,7 @@ void ata_scsi_slave_destroy(struct scsi_device *sdev)
        if (!ap->ops->error_handler)
                return;
 
-       spin_lock_irqsave(&ap->host_set->lock, flags);
+       spin_lock_irqsave(ap->lock, flags);
        dev = __ata_scsi_find_dev(ap, sdev);
        if (dev && dev->sdev) {
                /* SCSI device already in CANCEL state, no need to offline it */
@@ -760,7 +867,7 @@ void ata_scsi_slave_destroy(struct scsi_device *sdev)
                dev->flags |= ATA_DFLAG_DETACH;
                ata_port_schedule_eh(ap);
        }
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
+       spin_unlock_irqrestore(ap->lock, flags);
 }
 
 /**
@@ -2246,6 +2353,19 @@ static void atapi_qc_complete(struct ata_queued_cmd *qc)
                        ata_gen_ata_desc_sense(qc);
                }
 
+               /* SCSI EH automatically locks door if sdev->locked is
+                * set.  Sometimes door lock request continues to
+                * fail, for example, when no media is present.  This
+                * creates a loop - SCSI EH issues door lock which
+                * fails and gets invoked again to acquire sense data
+                * for the failed command.
+                *
+                * If door lock fails, always clear sdev->locked to
+                * avoid this infinite loop.
+                */
+               if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
+                       qc->dev->sdev->locked = 0;
+
                qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
                qc->scsidone(cmd);
                ata_qc_free(qc);
@@ -2351,7 +2471,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
                        qc->tf.feature |= ATAPI_DMADIR;
        }
 
-       qc->nbytes = cmd->bufflen;
+       qc->nbytes = cmd->request_bufflen;
 
        return 0;
 }
@@ -2373,6 +2493,36 @@ static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
        return ata_find_dev(ap, scsidev->id);
 }
 
+/**
+ *     ata_scsi_dev_enabled - determine if device is enabled
+ *     @dev: ATA device
+ *
+ *     Determine if commands should be sent to the specified device.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ *
+ *     RETURNS:
+ *     0 if commands are not allowed / 1 if commands are allowed
+ */
+
+static int ata_scsi_dev_enabled(struct ata_device *dev)
+{
+       if (unlikely(!ata_dev_enabled(dev)))
+               return 0;
+
+       if (!atapi_enabled || (dev->ap->flags & ATA_FLAG_NO_ATAPI)) {
+               if (unlikely(dev->class == ATA_DEV_ATAPI)) {
+                       ata_dev_printk(dev, KERN_WARNING,
+                                      "WARNING: ATAPI is %s, device ignored.\n",
+                                      atapi_enabled ? "not supported with this driver" : "disabled");
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
 /**
  *     ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  *     @ap: ATA port to which the device is attached
@@ -2394,18 +2544,9 @@ ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
 {
        struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
 
-       if (unlikely(!dev || !ata_dev_enabled(dev)))
+       if (unlikely(!dev || !ata_scsi_dev_enabled(dev)))
                return NULL;
 
-       if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
-               if (unlikely(dev->class == ATA_DEV_ATAPI)) {
-                       ata_dev_printk(dev, KERN_WARNING,
-                               "WARNING: ATAPI is %s, device ignored.\n",
-                               atapi_enabled ? "not supported with this driver" : "disabled");
-                       return NULL;
-               }
-       }
-
        return dev;
 }
 
@@ -2553,7 +2694,7 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
         * TODO: find out if we need to do more here to
         *       cover scatter/gather case.
         */
-       qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
+       qc->nsect = cmd->request_bufflen / ATA_SECT_SIZE;
 
        /* request result TF */
        qc->flags |= ATA_QCFLAG_RESULT_TF;
@@ -2684,7 +2825,7 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
        ap = ata_shost_to_port(shost);
 
        spin_unlock(shost->host_lock);
-       spin_lock(&ap->host_set->lock);
+       spin_lock(ap->lock);
 
        ata_scsi_dump_cdb(ap, cmd);
 
@@ -2696,7 +2837,7 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
                done(cmd);
        }
 
-       spin_unlock(&ap->host_set->lock);
+       spin_unlock(ap->lock);
        spin_lock(shost->host_lock);
        return rc;
 }
@@ -2858,7 +2999,7 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
         * increments reference counts regardless of device state.
         */
        mutex_lock(&ap->host->scan_mutex);
-       spin_lock_irqsave(&ap->host_set->lock, flags);
+       spin_lock_irqsave(ap->lock, flags);
 
        /* clearing dev->sdev is protected by host_set lock */
        sdev = dev->sdev;
@@ -2882,7 +3023,7 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
                }
        }
 
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
+       spin_unlock_irqrestore(ap->lock, flags);
        mutex_unlock(&ap->host->scan_mutex);
 
        if (sdev) {
@@ -2911,7 +3052,7 @@ void ata_scsi_hotplug(void *data)
        struct ata_port *ap = data;
        int i;
 
-       if (ap->flags & ATA_FLAG_UNLOADING) {
+       if (ap->pflags & ATA_PFLAG_UNLOADING) {
                DPRINTK("ENTER/EXIT - unloading\n");
                return;
        }
@@ -2926,9 +3067,9 @@ void ata_scsi_hotplug(void *data)
                if (!(dev->flags & ATA_DFLAG_DETACHED))
                        continue;
 
-               spin_lock_irqsave(&ap->host_set->lock, flags);
+               spin_lock_irqsave(ap->lock, flags);
                dev->flags &= ~ATA_DFLAG_DETACHED;
-               spin_unlock_irqrestore(&ap->host_set->lock, flags);
+               spin_unlock_irqrestore(ap->lock, flags);
 
                ata_scsi_remove_dev(dev);
        }
@@ -2981,7 +3122,7 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
            (lun != SCAN_WILD_CARD && lun != 0))
                return -EINVAL;
 
-       spin_lock_irqsave(&ap->host_set->lock, flags);
+       spin_lock_irqsave(ap->lock, flags);
 
        if (id == SCAN_WILD_CARD) {
                ap->eh_info.probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
@@ -2992,6 +3133,7 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
                if (dev) {
                        ap->eh_info.probe_mask |= 1 << dev->devno;
                        ap->eh_info.action |= ATA_EH_SOFTRESET;
+                       ap->eh_info.flags |= ATA_EHI_RESUME_LINK;
                } else
                        rc = -EINVAL;
        }
@@ -2999,7 +3141,7 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
        if (rc == 0)
                ata_port_schedule_eh(ap);
 
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
+       spin_unlock_irqrestore(ap->lock, flags);
 
        return rc;
 }