include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / ata / libata-scsi.c
index f802dbc..0088cde 100644 (file)
@@ -33,6 +33,7 @@
  *
  */
 
+#include <linux/slab.h>
 #include <linux/kernel.h>
 #include <linux/blkdev.h>
 #include <linux/spinlock.h>
 #include <linux/libata.h>
 #include <linux/hdreg.h>
 #include <linux/uaccess.h>
+#include <linux/suspend.h>
+#include <asm/unaligned.h>
 
 #include "libata.h"
 
-#define SECTOR_SIZE    512
+#define SECTOR_SIZE            512
+#define ATA_SCSI_RBUF_SIZE     4096
+
+static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
+static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
 
 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
 
@@ -131,10 +138,11 @@ static const char *ata_scsi_lpm_get(enum link_pm policy)
        return NULL;
 }
 
-static ssize_t ata_scsi_lpm_put(struct class_device *class_dev,
-       const char *buf, size_t count)
+static ssize_t ata_scsi_lpm_put(struct device *dev,
+                               struct device_attribute *attr,
+                               const char *buf, size_t count)
 {
-       struct Scsi_Host *shost = class_to_shost(class_dev);
+       struct Scsi_Host *shost = class_to_shost(dev);
        struct ata_port *ap = ata_shost_to_port(shost);
        enum link_pm policy = 0;
        int i;
@@ -148,8 +156,7 @@ static ssize_t ata_scsi_lpm_put(struct class_device *class_dev,
         */
        for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
                const int len = strlen(link_pm_policy[i].name);
-               if (strncmp(link_pm_policy[i].name, buf, len) == 0 &&
-                  buf[len] == '\n') {
+               if (strncmp(link_pm_policy[i].name, buf, len) == 0) {
                        policy = link_pm_policy[i].value;
                        break;
                }
@@ -162,9 +169,9 @@ static ssize_t ata_scsi_lpm_put(struct class_device *class_dev,
 }
 
 static ssize_t
-ata_scsi_lpm_show(struct class_device *class_dev, char *buf)
+ata_scsi_lpm_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
-       struct Scsi_Host *shost = class_to_shost(class_dev);
+       struct Scsi_Host *shost = class_to_shost(dev);
        struct ata_port *ap = ata_shost_to_port(shost);
        const char *policy =
                ata_scsi_lpm_get(ap->pm_policy);
@@ -174,9 +181,201 @@ ata_scsi_lpm_show(struct class_device *class_dev, char *buf)
 
        return snprintf(buf, 23, "%s\n", policy);
 }
-CLASS_DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
+DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
                ata_scsi_lpm_show, ata_scsi_lpm_put);
-EXPORT_SYMBOL_GPL(class_device_attr_link_power_management_policy);
+EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
+
+static ssize_t ata_scsi_park_show(struct device *device,
+                                 struct device_attribute *attr, char *buf)
+{
+       struct scsi_device *sdev = to_scsi_device(device);
+       struct ata_port *ap;
+       struct ata_link *link;
+       struct ata_device *dev;
+       unsigned long flags, now;
+       unsigned int uninitialized_var(msecs);
+       int rc = 0;
+
+       ap = ata_shost_to_port(sdev->host);
+
+       spin_lock_irqsave(ap->lock, flags);
+       dev = ata_scsi_find_dev(ap, sdev);
+       if (!dev) {
+               rc = -ENODEV;
+               goto unlock;
+       }
+       if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
+               rc = -EOPNOTSUPP;
+               goto unlock;
+       }
+
+       link = dev->link;
+       now = jiffies;
+       if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
+           link->eh_context.unloaded_mask & (1 << dev->devno) &&
+           time_after(dev->unpark_deadline, now))
+               msecs = jiffies_to_msecs(dev->unpark_deadline - now);
+       else
+               msecs = 0;
+
+unlock:
+       spin_unlock_irq(ap->lock);
+
+       return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
+}
+
+static ssize_t ata_scsi_park_store(struct device *device,
+                                  struct device_attribute *attr,
+                                  const char *buf, size_t len)
+{
+       struct scsi_device *sdev = to_scsi_device(device);
+       struct ata_port *ap;
+       struct ata_device *dev;
+       long int input;
+       unsigned long flags;
+       int rc;
+
+       rc = strict_strtol(buf, 10, &input);
+       if (rc || input < -2)
+               return -EINVAL;
+       if (input > ATA_TMOUT_MAX_PARK) {
+               rc = -EOVERFLOW;
+               input = ATA_TMOUT_MAX_PARK;
+       }
+
+       ap = ata_shost_to_port(sdev->host);
+
+       spin_lock_irqsave(ap->lock, flags);
+       dev = ata_scsi_find_dev(ap, sdev);
+       if (unlikely(!dev)) {
+               rc = -ENODEV;
+               goto unlock;
+       }
+       if (dev->class != ATA_DEV_ATA) {
+               rc = -EOPNOTSUPP;
+               goto unlock;
+       }
+
+       if (input >= 0) {
+               if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
+                       rc = -EOPNOTSUPP;
+                       goto unlock;
+               }
+
+               dev->unpark_deadline = ata_deadline(jiffies, input);
+               dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
+               ata_port_schedule_eh(ap);
+               complete(&ap->park_req_pending);
+       } else {
+               switch (input) {
+               case -1:
+                       dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
+                       break;
+               case -2:
+                       dev->flags |= ATA_DFLAG_NO_UNLOAD;
+                       break;
+               }
+       }
+unlock:
+       spin_unlock_irqrestore(ap->lock, flags);
+
+       return rc ? rc : len;
+}
+DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
+           ata_scsi_park_show, ata_scsi_park_store);
+EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
+
+static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
+{
+       cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
+
+       scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
+}
+
+static ssize_t
+ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
+                         const char *buf, size_t count)
+{
+       struct Scsi_Host *shost = class_to_shost(dev);
+       struct ata_port *ap = ata_shost_to_port(shost);
+       if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
+               return ap->ops->em_store(ap, buf, count);
+       return -EINVAL;
+}
+
+static ssize_t
+ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
+                        char *buf)
+{
+       struct Scsi_Host *shost = class_to_shost(dev);
+       struct ata_port *ap = ata_shost_to_port(shost);
+
+       if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
+               return ap->ops->em_show(ap, buf);
+       return -EINVAL;
+}
+DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
+               ata_scsi_em_message_show, ata_scsi_em_message_store);
+EXPORT_SYMBOL_GPL(dev_attr_em_message);
+
+static ssize_t
+ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct Scsi_Host *shost = class_to_shost(dev);
+       struct ata_port *ap = ata_shost_to_port(shost);
+
+       return snprintf(buf, 23, "%d\n", ap->em_message_type);
+}
+DEVICE_ATTR(em_message_type, S_IRUGO,
+                 ata_scsi_em_message_type_show, NULL);
+EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
+
+static ssize_t
+ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
+               char *buf)
+{
+       struct scsi_device *sdev = to_scsi_device(dev);
+       struct ata_port *ap = ata_shost_to_port(sdev->host);
+       struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
+
+       if (ap->ops->sw_activity_show && (ap->flags & ATA_FLAG_SW_ACTIVITY))
+               return ap->ops->sw_activity_show(atadev, buf);
+       return -EINVAL;
+}
+
+static ssize_t
+ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
+       const char *buf, size_t count)
+{
+       struct scsi_device *sdev = to_scsi_device(dev);
+       struct ata_port *ap = ata_shost_to_port(sdev->host);
+       struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
+       enum sw_activity val;
+       int rc;
+
+       if (ap->ops->sw_activity_store && (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
+               val = simple_strtoul(buf, NULL, 0);
+               switch (val) {
+               case OFF: case BLINK_ON: case BLINK_OFF:
+                       rc = ap->ops->sw_activity_store(atadev, val);
+                       if (!rc)
+                               return count;
+                       else
+                               return rc;
+               }
+       }
+       return -EINVAL;
+}
+DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
+                       ata_scsi_activity_store);
+EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
+
+struct device_attribute *ata_common_sdev_attrs[] = {
+       &dev_attr_unload_heads,
+       NULL
+};
+EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
 
 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
                                   void (*done)(struct scsi_cmnd *))
@@ -217,6 +416,7 @@ int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
 
 /**
  *     ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
+ *     @ap: target port
  *     @sdev: SCSI device to get identify data for
  *     @arg: User buffer area for identify data
  *
@@ -226,9 +426,9 @@ int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  *     RETURNS:
  *     Zero on success, negative errno on error.
  */
-static int ata_get_identity(struct scsi_device *sdev, void __user *arg)
+static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
+                           void __user *arg)
 {
-       struct ata_port *ap = ata_shost_to_port(sdev->host);
        struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
        u16 __user *dst = arg;
        char buf[40];
@@ -307,7 +507,7 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
        scsi_cmd[0] = ATA_16;
 
        scsi_cmd[4] = args[2];
-       if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
+       if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
                scsi_cmd[6]  = args[3];
                scsi_cmd[8]  = args[1];
                scsi_cmd[10] = 0x4f;
@@ -320,7 +520,7 @@ 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... */
        cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
-                                 sensebuf, (10*HZ), 5, 0);
+                                 sensebuf, (10*HZ), 5, 0, NULL);
 
        if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
                u8 *desc = sensebuf + 8;
@@ -406,7 +606,7 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
        /* Good values for timeout and retries?  Values below
           from scsi_ioctl_send_command() for default case... */
        cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
-                               sensebuf, (10*HZ), 5, 0);
+                               sensebuf, (10*HZ), 5, 0, NULL);
 
        if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
                u8 *desc = sensebuf + 8;
@@ -448,25 +648,48 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
        return rc;
 }
 
-int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
+static int ata_ioc32(struct ata_port *ap)
+{
+       if (ap->flags & ATA_FLAG_PIO_DMA)
+               return 1;
+       if (ap->pflags & ATA_PFLAG_PIO32)
+               return 1;
+       return 0;
+}
+
+int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
+                    int cmd, void __user *arg)
 {
        int val = -EINVAL, rc = -EINVAL;
+       unsigned long flags;
 
        switch (cmd) {
        case ATA_IOC_GET_IO32:
-               val = 0;
+               spin_lock_irqsave(ap->lock, flags);
+               val = ata_ioc32(ap);
+               spin_unlock_irqrestore(ap->lock, flags);
                if (copy_to_user(arg, &val, 1))
                        return -EFAULT;
                return 0;
 
        case ATA_IOC_SET_IO32:
                val = (unsigned long) arg;
-               if (val != 0)
-                       return -EINVAL;
-               return 0;
+               rc = 0;
+               spin_lock_irqsave(ap->lock, flags);
+               if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
+                       if (val)
+                               ap->pflags |= ATA_PFLAG_PIO32;
+                       else
+                               ap->pflags &= ~ATA_PFLAG_PIO32;
+               } else {
+                       if (val != ata_ioc32(ap))
+                               rc = -EINVAL;
+               }
+               spin_unlock_irqrestore(ap->lock, flags);
+               return rc;
 
        case HDIO_GET_IDENTITY:
-               return ata_get_identity(scsidev, arg);
+               return ata_get_identity(ap, scsidev, arg);
 
        case HDIO_DRIVE_CMD:
                if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
@@ -485,6 +708,14 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
 
        return rc;
 }
+EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
+
+int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
+{
+       return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
+                               scsidev, cmd, arg);
+}
+EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
 
 /**
  *     ata_scsi_qc_new - acquire new ata_queued_cmd reference
@@ -517,7 +748,7 @@ static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
                qc->scsicmd = cmd;
                qc->scsidone = done;
 
-               qc->__sg = scsi_sglist(cmd);
+               qc->sg = scsi_sglist(cmd);
                qc->n_elem = scsi_sg_count(cmd);
        } else {
                cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
@@ -527,6 +758,14 @@ static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
        return qc;
 }
 
+static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
+{
+       struct scsi_cmnd *scmd = qc->scsicmd;
+
+       qc->extrabytes = scmd->request->extra_len;
+       qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
+}
+
 /**
  *     ata_dump_status - user friendly display of error info
  *     @id: id of the port in question
@@ -826,23 +1065,66 @@ static void ata_scsi_sdev_config(struct scsi_device *sdev)
        sdev->max_device_blocked = 1;
 }
 
-static void ata_scsi_dev_config(struct scsi_device *sdev,
-                               struct ata_device *dev)
+/**
+ *     atapi_drain_needed - Check whether data transfer may overflow
+ *     @rq: request to be checked
+ *
+ *     ATAPI commands which transfer variable length data to host
+ *     might overflow due to application error or hardare bug.  This
+ *     function checks whether overflow should be drained and ignored
+ *     for @request.
+ *
+ *     LOCKING:
+ *     None.
+ *
+ *     RETURNS:
+ *     1 if ; otherwise, 0.
+ */
+static int atapi_drain_needed(struct request *rq)
 {
+       if (likely(!blk_pc_request(rq)))
+               return 0;
+
+       if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_RW))
+               return 0;
+
+       return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
+}
+
+static int ata_scsi_dev_config(struct scsi_device *sdev,
+                              struct ata_device *dev)
+{
+       if (!ata_id_has_unload(dev->id))
+               dev->flags |= ATA_DFLAG_NO_UNLOAD;
+
        /* configure max sectors */
-       blk_queue_max_sectors(sdev->request_queue, dev->max_sectors);
+       blk_queue_max_hw_sectors(sdev->request_queue, dev->max_sectors);
 
-       /* 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) {
                struct request_queue *q = sdev->request_queue;
-               blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
-       }
+               void *buf;
+
+               /* set the min alignment and padding */
+               blk_queue_update_dma_alignment(sdev->request_queue,
+                                              ATA_DMA_PAD_SZ - 1);
+               blk_queue_update_dma_pad(sdev->request_queue,
+                                        ATA_DMA_PAD_SZ - 1);
+
+               /* configure draining */
+               buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
+               if (!buf) {
+                       ata_dev_printk(dev, KERN_ERR,
+                                      "drain buffer allocation failed\n");
+                       return -ENOMEM;
+               }
 
-       if (dev->class == ATA_DEV_ATA)
+               blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
+       } else {
+               /* ATA devices must be sector aligned */
+               blk_queue_update_dma_alignment(sdev->request_queue,
+                                              ATA_SECT_SIZE - 1);
                sdev->manage_start_stop = 1;
+       }
 
        if (dev->flags & ATA_DFLAG_AN)
                set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
@@ -854,6 +1136,8 @@ static void ata_scsi_dev_config(struct scsi_device *sdev,
                depth = min(ATA_MAX_QUEUE - 1, depth);
                scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
        }
+
+       return 0;
 }
 
 /**
@@ -872,13 +1156,14 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
 {
        struct ata_port *ap = ata_shost_to_port(sdev->host);
        struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
+       int rc = 0;
 
        ata_scsi_sdev_config(sdev);
 
        if (dev)
-               ata_scsi_dev_config(sdev, dev);
+               rc = ata_scsi_dev_config(sdev, dev);
 
-       return 0;       /* scsi layer doesn't check return value, sigh */
+       return rc;
 }
 
 /**
@@ -898,6 +1183,7 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
 void ata_scsi_slave_destroy(struct scsi_device *sdev)
 {
        struct ata_port *ap = ata_shost_to_port(sdev->host);
+       struct request_queue *q = sdev->request_queue;
        unsigned long flags;
        struct ata_device *dev;
 
@@ -913,12 +1199,17 @@ void ata_scsi_slave_destroy(struct scsi_device *sdev)
                ata_port_schedule_eh(ap);
        }
        spin_unlock_irqrestore(ap->lock, flags);
+
+       kfree(q->dma_drain_buffer);
+       q->dma_drain_buffer = NULL;
+       q->dma_drain_size = 0;
 }
 
 /**
  *     ata_scsi_change_queue_depth - SCSI callback for queue depth config
  *     @sdev: SCSI device to configure queue depth for
  *     @queue_depth: new queue depth
+ *     @reason: calling context
  *
  *     This is libata standard hostt->change_queue_depth callback.
  *     SCSI will call into this callback when user tries to set queue
@@ -930,12 +1221,16 @@ void ata_scsi_slave_destroy(struct scsi_device *sdev)
  *     RETURNS:
  *     Newly configured queue depth.
  */
-int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
+int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth,
+                               int reason)
 {
        struct ata_port *ap = ata_shost_to_port(sdev->host);
        struct ata_device *dev;
        unsigned long flags;
 
+       if (reason != SCSI_QDEPTH_DEFAULT)
+               return -EOPNOTSUPP;
+
        if (queue_depth < 1 || queue_depth == sdev->queue_depth)
                return sdev->queue_depth;
 
@@ -964,23 +1259,6 @@ int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
        return queue_depth;
 }
 
-/* XXX: for spindown warning */
-static void ata_delayed_done_timerfn(unsigned long arg)
-{
-       struct scsi_cmnd *scmd = (void *)arg;
-
-       scmd->scsi_done(scmd);
-}
-
-/* XXX: for spindown warning */
-static void ata_delayed_done(struct scsi_cmnd *scmd)
-{
-       static struct timer_list timer;
-
-       setup_timer(&timer, ata_delayed_done_timerfn, (unsigned long)scmd);
-       mod_timer(&timer, jiffies + 5 * HZ);
-}
-
 /**
  *     ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  *     @qc: Storage for translated ATA taskfile
@@ -1015,12 +1293,6 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
        if (((cdb[4] >> 4) & 0xf) != 0)
                goto invalid_fld;       /* power conditions not supported */
 
-       if (qc->dev->horkage & ATA_HORKAGE_SKIP_PM) {
-               /* the device lacks PM support, finish without doing anything */
-               scmd->result = SAM_STAT_GOOD;
-               return 1;
-       }
-
        if (cdb[4] & 0x1) {
                tf->nsect = 1;  /* 1 sector, lba=0 */
 
@@ -1040,32 +1312,16 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
 
                tf->command = ATA_CMD_VERIFY;   /* READ VERIFY */
        } else {
-               /* XXX: This is for backward compatibility, will be
-                * removed.  Read Documentation/feature-removal-schedule.txt
-                * for more info.
+               /* Some odd clown BIOSen issue spindown on power off (ACPI S4
+                * or S5) causing some drives to spin up and down again.
                 */
-               if ((qc->dev->flags & ATA_DFLAG_SPUNDOWN) &&
-                   (system_state == SYSTEM_HALT ||
-                    system_state == SYSTEM_POWER_OFF)) {
-                       static unsigned long warned;
-
-                       if (!test_and_set_bit(0, &warned)) {
-                               ata_dev_printk(qc->dev, KERN_WARNING,
-                                       "DISK MIGHT NOT BE SPUN DOWN PROPERLY. "
-                                       "UPDATE SHUTDOWN UTILITY\n");
-                               ata_dev_printk(qc->dev, KERN_WARNING,
-                                       "For more info, visit "
-                                       "http://linux-ata.org/shutdown.html\n");
-
-                               /* ->scsi_done is not used, use it for
-                                * delayed completion.
-                                */
-                               scmd->scsi_done = qc->scsidone;
-                               qc->scsidone = ata_delayed_done;
-                       }
-                       scmd->result = SAM_STAT_GOOD;
-                       return 1;
-               }
+               if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
+                   system_state == SYSTEM_POWER_OFF)
+                       goto skip;
+
+               if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
+                    system_entering_hibernation())
+                       goto skip;
 
                /* Issue ATA STANDBY IMMEDIATE command */
                tf->command = ATA_CMD_STANDBYNOW1;
@@ -1080,10 +1336,13 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
 
        return 0;
 
-invalid_fld:
+ invalid_fld:
        ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
        /* "Invalid field in cbd" */
        return 1;
+ skip:
+       scmd->result = SAM_STAT_GOOD;
+       return 1;
 }
 
 
@@ -1464,14 +1723,6 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
                }
        }
 
-       /* XXX: track spindown state for spindown skipping and warning */
-       if (unlikely(qc->tf.command == ATA_CMD_STANDBY ||
-                    qc->tf.command == ATA_CMD_STANDBYNOW1))
-               qc->dev->flags |= ATA_DFLAG_SPUNDOWN;
-       else if (likely(system_state != SYSTEM_HALT &&
-                       system_state != SYSTEM_POWER_OFF))
-               qc->dev->flags &= ~ATA_DFLAG_SPUNDOWN;
-
        if (need_sense && !ap->ops->error_handler)
                ata_dump_status(ap->print_id, &qc->result_tf);
 
@@ -1577,52 +1828,48 @@ defer:
 /**
  *     ata_scsi_rbuf_get - Map response buffer.
  *     @cmd: SCSI command containing buffer to be mapped.
- *     @buf_out: Pointer to mapped area.
+ *     @flags: unsigned long variable to store irq enable status
+ *     @copy_in: copy in from user buffer
  *
- *     Maps buffer contained within SCSI command @cmd.
+ *     Prepare buffer for simulated SCSI commands.
  *
  *     LOCKING:
- *     spin_lock_irqsave(host lock)
+ *     spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  *
  *     RETURNS:
- *     Length of response buffer.
+ *     Pointer to response buffer.
  */
-
-static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
+static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
+                              unsigned long *flags)
 {
-       u8 *buf;
-       unsigned int buflen;
-
-       struct scatterlist *sg = scsi_sglist(cmd);
+       spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
 
-       if (sg) {
-               buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
-               buflen = sg->length;
-       } else {
-               buf = NULL;
-               buflen = 0;
-       }
-
-       *buf_out = buf;
-       return buflen;
+       memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
+       if (copy_in)
+               sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
+                                 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
+       return ata_scsi_rbuf;
 }
 
 /**
  *     ata_scsi_rbuf_put - Unmap response buffer.
  *     @cmd: SCSI command containing buffer to be unmapped.
- *     @buf: buffer to unmap
+ *     @copy_out: copy out result
+ *     @flags: @flags passed to ata_scsi_rbuf_get()
  *
- *     Unmaps response buffer contained within @cmd.
+ *     Returns rbuf buffer.  The result is copied to @cmd's buffer if
+ *     @copy_back is true.
  *
  *     LOCKING:
- *     spin_lock_irqsave(host lock)
+ *     Unlocks ata_scsi_rbuf_lock.
  */
-
-static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
+static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
+                                    unsigned long *flags)
 {
-       struct scatterlist *sg = scsi_sglist(cmd);
-       if (sg)
-               kunmap_atomic(buf - sg->offset, KM_IRQ0);
+       if (copy_out)
+               sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
+                                   ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
+       spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
 }
 
 /**
@@ -1640,19 +1887,17 @@ static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
-                       unsigned int (*actor) (struct ata_scsi_args *args,
-                                              u8 *rbuf, unsigned int buflen))
+static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
+               unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
 {
        u8 *rbuf;
-       unsigned int buflen, rc;
+       unsigned int rc;
        struct scsi_cmnd *cmd = args->cmd;
+       unsigned long flags;
 
-       buflen = ata_scsi_rbuf_get(cmd, &rbuf);
-       memset(rbuf, 0, buflen);
-       rc = actor(args, rbuf, buflen);
-       ata_scsi_rbuf_put(cmd, rbuf);
+       rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
+       rc = actor(args, rbuf);
+       ata_scsi_rbuf_put(cmd, rc == 0, &flags);
 
        if (rc == 0)
                cmd->result = SAM_STAT_GOOD;
@@ -1660,26 +1905,9 @@ void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
 }
 
 /**
- *     ATA_SCSI_RBUF_SET - helper to set values in SCSI response buffer
- *     @idx: byte index into SCSI response buffer
- *     @val: value to set
- *
- *     To be used by SCSI command simulator functions.  This macros
- *     expects two local variables, u8 *rbuf and unsigned int buflen,
- *     are in scope.
- *
- *     LOCKING:
- *     None.
- */
-#define ATA_SCSI_RBUF_SET(idx, val) do { \
-               if ((idx) < buflen) rbuf[(idx)] = (u8)(val); \
-       } while (0)
-
-/**
  *     ata_scsiop_inq_std - Simulate INQUIRY command
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Returns standard device identification data associated
  *     with non-VPD INQUIRY command output.
@@ -1687,10 +1915,17 @@ void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
-                              unsigned int buflen)
+static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
 {
+       const u8 versions[] = {
+               0x60,   /* SAM-3 (no version claimed) */
+
+               0x03,
+               0x20,   /* SBC-2 (no version claimed) */
+
+               0x02,
+               0x60    /* SPC-3 (no version claimed) */
+       };
        u8 hdr[] = {
                TYPE_DISK,
                0,
@@ -1699,35 +1934,21 @@ unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
                95 - 4
        };
 
+       VPRINTK("ENTER\n");
+
        /* set scsi removeable (RMB) bit per ata bit */
        if (ata_id_removeable(args->id))
                hdr[1] |= (1 << 7);
 
-       VPRINTK("ENTER\n");
-
        memcpy(rbuf, hdr, sizeof(hdr));
+       memcpy(&rbuf[8], "ATA     ", 8);
+       ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
+       ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
 
-       if (buflen > 35) {
-               memcpy(&rbuf[8], "ATA     ", 8);
-               ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
-               ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
-               if (rbuf[32] == 0 || rbuf[32] == ' ')
-                       memcpy(&rbuf[32], "n/a ", 4);
-       }
-
-       if (buflen > 63) {
-               const u8 versions[] = {
-                       0x60,   /* SAM-3 (no version claimed) */
-
-                       0x03,
-                       0x20,   /* SBC-2 (no version claimed) */
+       if (rbuf[32] == 0 || rbuf[32] == ' ')
+               memcpy(&rbuf[32], "n/a ", 4);
 
-                       0x02,
-                       0x60    /* SPC-3 (no version claimed) */
-               };
-
-               memcpy(rbuf + 59, versions, sizeof(versions));
-       }
+       memcpy(rbuf + 59, versions, sizeof(versions));
 
        return 0;
 }
@@ -1736,27 +1957,25 @@ unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  *     ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Returns list of inquiry VPD pages available.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
-                             unsigned int buflen)
+static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
 {
        const u8 pages[] = {
                0x00,   /* page 0x00, this page */
                0x80,   /* page 0x80, unit serial no page */
-               0x83    /* page 0x83, device ident page */
+               0x83,   /* page 0x83, device ident page */
+               0x89,   /* page 0x89, ata info page */
+               0xb0,   /* page 0xb0, block limits page */
+               0xb1,   /* page 0xb1, block device characteristics page */
        };
-       rbuf[3] = sizeof(pages);        /* number of supported VPD pages */
-
-       if (buflen > 6)
-               memcpy(rbuf + 4, pages, sizeof(pages));
 
+       rbuf[3] = sizeof(pages);        /* number of supported VPD pages */
+       memcpy(rbuf + 4, pages, sizeof(pages));
        return 0;
 }
 
@@ -1764,16 +1983,13 @@ unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  *     ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Returns ATA device serial number.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
-                             unsigned int buflen)
+static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
 {
        const u8 hdr[] = {
                0,
@@ -1781,12 +1997,10 @@ unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
                0,
                ATA_ID_SERNO_LEN,       /* page len */
        };
-       memcpy(rbuf, hdr, sizeof(hdr));
-
-       if (buflen > (ATA_ID_SERNO_LEN + 4 - 1))
-               ata_id_string(args->id, (unsigned char *) &rbuf[4],
-                             ATA_ID_SERNO, ATA_ID_SERNO_LEN);
 
+       memcpy(rbuf, hdr, sizeof(hdr));
+       ata_id_string(args->id, (unsigned char *) &rbuf[4],
+                     ATA_ID_SERNO, ATA_ID_SERNO_LEN);
        return 0;
 }
 
@@ -1794,7 +2008,6 @@ unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  *     ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Yields two logical unit device identification designators:
  *      - vendor specific ASCII containing the ATA serial number
@@ -1804,41 +2017,37 @@ unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
-                             unsigned int buflen)
+static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
 {
-       int num;
        const int sat_model_serial_desc_len = 68;
+       int num;
 
        rbuf[1] = 0x83;                 /* this page code */
        num = 4;
 
-       if (buflen > (ATA_ID_SERNO_LEN + num + 3)) {
-               /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
-               rbuf[num + 0] = 2;
-               rbuf[num + 3] = ATA_ID_SERNO_LEN;
-               num += 4;
-               ata_id_string(args->id, (unsigned char *) rbuf + num,
-                             ATA_ID_SERNO, ATA_ID_SERNO_LEN);
-               num += ATA_ID_SERNO_LEN;
-       }
-       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 + 3] = sat_model_serial_desc_len;
-               num += 4;
-               memcpy(rbuf + num, "ATA     ", 8);
-               num += 8;
-               ata_id_string(args->id, (unsigned char *) rbuf + num,
-                             ATA_ID_PROD, ATA_ID_PROD_LEN);
-               num += ATA_ID_PROD_LEN;
-               ata_id_string(args->id, (unsigned char *) rbuf + num,
-                             ATA_ID_SERNO, ATA_ID_SERNO_LEN);
-               num += ATA_ID_SERNO_LEN;
-       }
+       /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
+       rbuf[num + 0] = 2;
+       rbuf[num + 3] = ATA_ID_SERNO_LEN;
+       num += 4;
+       ata_id_string(args->id, (unsigned char *) rbuf + num,
+                     ATA_ID_SERNO, ATA_ID_SERNO_LEN);
+       num += ATA_ID_SERNO_LEN;
+
+       /* 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 + 3] = sat_model_serial_desc_len;
+       num += 4;
+       memcpy(rbuf + num, "ATA     ", 8);
+       num += 8;
+       ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
+                     ATA_ID_PROD_LEN);
+       num += ATA_ID_PROD_LEN;
+       ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
+                     ATA_ID_SERNO_LEN);
+       num += ATA_ID_SERNO_LEN;
+
        rbuf[3] = num - 4;    /* page len (assume less than 256 bytes) */
        return 0;
 }
@@ -1847,35 +2056,26 @@ unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  *     ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Yields SAT-specified ATA VPD page.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
-                             unsigned int buflen)
+static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
 {
-       u8 pbuf[60];
        struct ata_taskfile tf;
-       unsigned int i;
-
-       if (!buflen)
-               return 0;
 
-       memset(&pbuf, 0, sizeof(pbuf));
        memset(&tf, 0, sizeof(tf));
 
-       pbuf[1] = 0x89;                 /* our page code */
-       pbuf[2] = (0x238 >> 8);         /* page size fixed at 238h */
-       pbuf[3] = (0x238 & 0xff);
+       rbuf[1] = 0x89;                 /* our page code */
+       rbuf[2] = (0x238 >> 8);         /* page size fixed at 238h */
+       rbuf[3] = (0x238 & 0xff);
 
-       memcpy(&pbuf[8], "linux   ", 8);
-       memcpy(&pbuf[16], "libata          ", 16);
-       memcpy(&pbuf[32], DRV_VERSION, 4);
-       ata_id_string(args->id, &pbuf[32], ATA_ID_FW_REV, 4);
+       memcpy(&rbuf[8], "linux   ", 8);
+       memcpy(&rbuf[16], "libata          ", 16);
+       memcpy(&rbuf[32], DRV_VERSION, 4);
+       ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
 
        /* we don't store the ATA device signature, so we fake it */
 
@@ -1883,19 +2083,63 @@ unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
        tf.lbal = 0x1;
        tf.nsect = 0x1;
 
-       ata_tf_to_fis(&tf, 0, 1, &pbuf[36]);    /* TODO: PMP? */
-       pbuf[36] = 0x34;                /* force D2H Reg FIS (34h) */
+       ata_tf_to_fis(&tf, 0, 1, &rbuf[36]);    /* TODO: PMP? */
+       rbuf[36] = 0x34;                /* force D2H Reg FIS (34h) */
 
-       pbuf[56] = ATA_CMD_ID_ATA;
+       rbuf[56] = ATA_CMD_ID_ATA;
 
-       i = min(buflen, 60U);
-       memcpy(rbuf, &pbuf[0], i);
-       buflen -= i;
+       memcpy(&rbuf[60], &args->id[0], 512);
+       return 0;
+}
 
-       if (!buflen)
-               return 0;
+static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
+{
+       u32 min_io_sectors;
+
+       rbuf[1] = 0xb0;
+       rbuf[3] = 0x3c;         /* required VPD size with unmap support */
+
+       /*
+        * Optimal transfer length granularity.
+        *
+        * This is always one physical block, but for disks with a smaller
+        * logical than physical sector size we need to figure out what the
+        * latter is.
+        */
+       if (ata_id_has_large_logical_sectors(args->id))
+               min_io_sectors = ata_id_logical_per_physical_sectors(args->id);
+       else
+               min_io_sectors = 1;
+       put_unaligned_be16(min_io_sectors, &rbuf[6]);
+
+       /*
+        * Optimal unmap granularity.
+        *
+        * The ATA spec doesn't even know about a granularity or alignment
+        * for the TRIM command.  We can leave away most of the unmap related
+        * VPD page entries, but we have specifify a granularity to signal
+        * that we support some form of unmap - in thise case via WRITE SAME
+        * with the unmap bit set.
+        */
+       if (ata_id_has_trim(args->id)) {
+               put_unaligned_be32(65535 * 512 / 8, &rbuf[20]);
+               put_unaligned_be32(1, &rbuf[28]);
+       }
+
+       return 0;
+}
+
+static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
+{
+       int form_factor = ata_id_form_factor(args->id);
+       int media_rotation_rate = ata_id_rotation_rate(args->id);
+
+       rbuf[1] = 0xb1;
+       rbuf[3] = 0x3c;
+       rbuf[4] = media_rotation_rate >> 8;
+       rbuf[5] = media_rotation_rate;
+       rbuf[7] = form_factor;
 
-       memcpy(&rbuf[60], &args->id[0], min(buflen, 512U));
        return 0;
 }
 
@@ -1903,7 +2147,6 @@ unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
  *     ata_scsiop_noop - Command handler that simply returns success.
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     No operation.  Simply returns success to caller, to indicate
  *     that the caller should successfully complete this SCSI command.
@@ -1911,47 +2154,16 @@ unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
-                           unsigned int buflen)
+static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
 {
        VPRINTK("ENTER\n");
        return 0;
 }
 
 /**
- *     ata_msense_push - Push data onto MODE SENSE data output buffer
- *     @ptr_io: (input/output) Location to store more output data
- *     @last: End of output data buffer
- *     @buf: Pointer to BLOB being added to output buffer
- *     @buflen: Length of BLOB
- *
- *     Store MODE SENSE data on an output buffer.
- *
- *     LOCKING:
- *     None.
- */
-
-static void ata_msense_push(u8 **ptr_io, const u8 *last,
-                           const u8 *buf, unsigned int buflen)
-{
-       u8 *ptr = *ptr_io;
-
-       if ((ptr + buflen - 1) > last)
-               return;
-
-       memcpy(ptr, buf, buflen);
-
-       ptr += buflen;
-
-       *ptr_io = ptr;
-}
-
-/**
  *     ata_msense_caching - Simulate MODE SENSE caching info page
  *     @id: device IDENTIFY data
- *     @ptr_io: (input/output) Location to store more output data
- *     @last: End of output data buffer
+ *     @buf: output buffer
  *
  *     Generate a caching info page, which conditionally indicates
  *     write caching to the SCSI layer, depending on device
@@ -1960,58 +2172,43 @@ static void ata_msense_push(u8 **ptr_io, const u8 *last,
  *     LOCKING:
  *     None.
  */
-
-static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
-                                      const u8 *last)
+static unsigned int ata_msense_caching(u16 *id, u8 *buf)
 {
-       u8 page[CACHE_MPAGE_LEN];
-
-       memcpy(page, def_cache_mpage, sizeof(page));
+       memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage));
        if (ata_id_wcache_enabled(id))
-               page[2] |= (1 << 2);    /* write cache enable */
+               buf[2] |= (1 << 2);     /* write cache enable */
        if (!ata_id_rahead_enabled(id))
-               page[12] |= (1 << 5);   /* disable read ahead */
-
-       ata_msense_push(ptr_io, last, page, sizeof(page));
-       return sizeof(page);
+               buf[12] |= (1 << 5);    /* disable read ahead */
+       return sizeof(def_cache_mpage);
 }
 
 /**
  *     ata_msense_ctl_mode - Simulate MODE SENSE control mode page
- *     @dev: Device associated with this MODE SENSE command
- *     @ptr_io: (input/output) Location to store more output data
- *     @last: End of output data buffer
+ *     @buf: output buffer
  *
  *     Generate a generic MODE SENSE control mode page.
  *
  *     LOCKING:
  *     None.
  */
-
-static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
+static unsigned int ata_msense_ctl_mode(u8 *buf)
 {
-       ata_msense_push(ptr_io, last, def_control_mpage,
-                       sizeof(def_control_mpage));
+       memcpy(buf, def_control_mpage, sizeof(def_control_mpage));
        return sizeof(def_control_mpage);
 }
 
 /**
  *     ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
- *     @dev: Device associated with this MODE SENSE command
- *     @ptr_io: (input/output) Location to store more output data
- *     @last: End of output data buffer
+ *     @buf: output buffer
  *
  *     Generate a generic MODE SENSE r/w error recovery page.
  *
  *     LOCKING:
  *     None.
  */
-
-static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
+static unsigned int ata_msense_rw_recovery(u8 *buf)
 {
-
-       ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
-                       sizeof(def_rw_recovery_mpage));
+       memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage));
        return sizeof(def_rw_recovery_mpage);
 }
 
@@ -2043,7 +2240,6 @@ static int ata_dev_supports_fua(u16 *id)
  *     ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Simulate MODE SENSE commands. Assume this is invoked for direct
  *     access devices (e.g. disks) only. There should be no block
@@ -2052,19 +2248,17 @@ static int ata_dev_supports_fua(u16 *id)
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
-                                 unsigned int buflen)
+static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
 {
        struct ata_device *dev = args->dev;
-       u8 *scsicmd = args->cmd->cmnd, *p, *last;
+       u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
        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;
+       unsigned int ebd, page_control, six_byte;
        u8 dpofua;
 
        VPRINTK("ENTER\n");
@@ -2087,17 +2281,10 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
                goto invalid_fld;
        }
 
-       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 + minlen - 1;
+       if (six_byte)
+               p += 4 + (ebd ? 8 : 0);
+       else
+               p += 8 + (ebd ? 8 : 0);
 
        pg = scsicmd[2] & 0x3f;
        spg = scsicmd[3];
@@ -2110,61 +2297,48 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
 
        switch(pg) {
        case RW_RECOVERY_MPAGE:
-               output_len += ata_msense_rw_recovery(&p, last);
+               p += ata_msense_rw_recovery(p);
                break;
 
        case CACHE_MPAGE:
-               output_len += ata_msense_caching(args->id, &p, last);
+               p += ata_msense_caching(args->id, p);
                break;
 
-       case CONTROL_MPAGE: {
-               output_len += ata_msense_ctl_mode(&p, last);
+       case CONTROL_MPAGE:
+               p += ata_msense_ctl_mode(p);
                break;
-               }
 
        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);
+               p += ata_msense_rw_recovery(p);
+               p += ata_msense_caching(args->id, p);
+               p += ata_msense_ctl_mode(p);
                break;
 
        default:                /* invalid page code */
                goto invalid_fld;
        }
 
-       if (minlen < 1)
-               return 0;
-
        dpofua = 0;
        if (ata_dev_supports_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;
+               rbuf[0] = p - rbuf - 1;
+               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));
+                       rbuf[3] = sizeof(sat_blk_desc);
+                       memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
                }
        } else {
-               output_len -= 2;
+               unsigned int output_len = p - rbuf - 2;
+
                rbuf[0] = output_len >> 8;
-               if (minlen > 1)
-                       rbuf[1] = output_len;
-               if (minlen > 3)
-                       rbuf[3] |= dpofua;
+               rbuf[1] = output_len;
+               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));
+                       rbuf[7] = sizeof(sat_blk_desc);
+                       memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
                }
        }
        return 0;
@@ -2184,17 +2358,31 @@ saving_not_supp:
  *     ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Simulate READ CAPACITY commands.
  *
  *     LOCKING:
  *     None.
  */
-unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
-                                unsigned int buflen)
+static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
 {
-       u64 last_lba = args->dev->n_sectors - 1; /* LBA of the last block */
+       struct ata_device *dev = args->dev;
+       u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
+       u8 log_per_phys = 0;
+       u16 lowest_aligned = 0;
+       u16 word_106 = dev->id[106];
+       u16 word_209 = dev->id[209];
+
+       if ((word_106 & 0xc000) == 0x4000) {
+               /* Number and offset of logical sectors per physical sector */
+               if (word_106 & (1 << 13))
+                       log_per_phys = word_106 & 0xf;
+               if ((word_209 & 0xc000) == 0x4000) {
+                       u16 first = dev->id[209] & 0x3fff;
+                       if (first > 0)
+                               lowest_aligned = (1 << log_per_phys) - first;
+               }
+       }
 
        VPRINTK("ENTER\n");
 
@@ -2203,28 +2391,40 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
                        last_lba = 0xffffffff;
 
                /* sector count, 32-bit */
-               ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 3));
-               ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 2));
-               ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 1));
-               ATA_SCSI_RBUF_SET(3, last_lba);
+               rbuf[0] = last_lba >> (8 * 3);
+               rbuf[1] = last_lba >> (8 * 2);
+               rbuf[2] = last_lba >> (8 * 1);
+               rbuf[3] = last_lba;
 
                /* sector size */
-               ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8);
-               ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE);
+               rbuf[6] = ATA_SECT_SIZE >> 8;
+               rbuf[7] = ATA_SECT_SIZE & 0xff;
        } else {
                /* sector count, 64-bit */
-               ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7));
-               ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 6));
-               ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 5));
-               ATA_SCSI_RBUF_SET(3, last_lba >> (8 * 4));
-               ATA_SCSI_RBUF_SET(4, last_lba >> (8 * 3));
-               ATA_SCSI_RBUF_SET(5, last_lba >> (8 * 2));
-               ATA_SCSI_RBUF_SET(6, last_lba >> (8 * 1));
-               ATA_SCSI_RBUF_SET(7, last_lba);
+               rbuf[0] = last_lba >> (8 * 7);
+               rbuf[1] = last_lba >> (8 * 6);
+               rbuf[2] = last_lba >> (8 * 5);
+               rbuf[3] = last_lba >> (8 * 4);
+               rbuf[4] = last_lba >> (8 * 3);
+               rbuf[5] = last_lba >> (8 * 2);
+               rbuf[6] = last_lba >> (8 * 1);
+               rbuf[7] = last_lba;
 
                /* sector size */
-               ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8);
-               ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE);
+               rbuf[10] = ATA_SECT_SIZE >> 8;
+               rbuf[11] = ATA_SECT_SIZE & 0xff;
+
+               rbuf[12] = 0;
+               rbuf[13] = log_per_phys;
+               rbuf[14] = (lowest_aligned >> 8) & 0x3f;
+               rbuf[15] = lowest_aligned;
+
+               if (ata_id_has_trim(args->id)) {
+                       rbuf[14] |= 0x80; /* TPE */
+
+                       if (ata_id_has_zero_after_trim(args->id))
+                               rbuf[14] |= 0x40; /* TPRZ */
+               }
        }
 
        return 0;
@@ -2234,16 +2434,13 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  *     ata_scsiop_report_luns - Simulate REPORT LUNS command
  *     @args: device IDENTIFY data / SCSI command of interest.
  *     @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
- *     @buflen: Response buffer length.
  *
  *     Simulate REPORT LUNS command.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
-                                  unsigned int buflen)
+static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
 {
        VPRINTK("ENTER\n");
        rbuf[3] = 8;    /* just one lun, LUN 0, size 8 bytes */
@@ -2251,57 +2448,6 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
        return 0;
 }
 
-/**
- *     ata_scsi_set_sense - Set SCSI sense data and status
- *     @cmd: SCSI request to be handled
- *     @sk: SCSI-defined sense key
- *     @asc: SCSI-defined additional sense code
- *     @ascq: SCSI-defined additional sense code qualifier
- *
- *     Helper function that builds a valid fixed format, current
- *     response code and the given sense key (sk), additional sense
- *     code (asc) and additional sense code qualifier (ascq) with
- *     a SCSI command status of %SAM_STAT_CHECK_CONDITION and
- *     DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
- *
- *     LOCKING:
- *     Not required
- */
-
-void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
-{
-       cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
-
-       cmd->sense_buffer[0] = 0x70;    /* fixed format, current */
-       cmd->sense_buffer[2] = sk;
-       cmd->sense_buffer[7] = 18 - 8;  /* additional sense length */
-       cmd->sense_buffer[12] = asc;
-       cmd->sense_buffer[13] = ascq;
-}
-
-/**
- *     ata_scsi_badcmd - End a SCSI request with an error
- *     @cmd: SCSI request to be handled
- *     @done: SCSI command completion function
- *     @asc: SCSI-defined additional sense code
- *     @ascq: SCSI-defined additional sense code qualifier
- *
- *     Helper function that completes a SCSI command with
- *     %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
- *     and the specified additional sense codes.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-
-void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
-{
-       DPRINTK("ENTER\n");
-       ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
-
-       done(cmd);
-}
-
 static void atapi_sense_complete(struct ata_queued_cmd *qc)
 {
        if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
@@ -2331,9 +2477,12 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
        DPRINTK("ATAPI request sense\n");
 
        /* FIXME: is this needed? */
-       memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
+       memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
 
-       ap->ops->tf_read(ap, &qc->tf);
+#ifdef CONFIG_ATA_SFF
+       if (ap->ops->sff_tf_read)
+               ap->ops->sff_tf_read(ap, &qc->tf);
+#endif
 
        /* fill these in, for the case where they are -not- overwritten */
        cmd->sense_buffer[0] = 0x70;
@@ -2341,7 +2490,9 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
 
        ata_qc_reinit(qc);
 
-       ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
+       /* setup sg table and init transfer direction */
+       sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
+       ata_sg_init(qc, &qc->sgent, 1);
        qc->dma_dir = DMA_FROM_DEVICE;
 
        memset(&qc->cdb, 0, qc->dev->cdb_len);
@@ -2352,10 +2503,10 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
        qc->tf.command = ATA_CMD_PACKET;
 
        if (ata_pio_use_silly(ap)) {
-               qc->tf.protocol = ATA_PROT_ATAPI_DMA;
+               qc->tf.protocol = ATAPI_PROT_DMA;
                qc->tf.feature |= ATAPI_PKT_DMA;
        } else {
-               qc->tf.protocol = ATA_PROT_ATAPI;
+               qc->tf.protocol = ATAPI_PROT_PIO;
                qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
                qc->tf.lbah = 0;
        }
@@ -2423,10 +2574,10 @@ static void atapi_qc_complete(struct ata_queued_cmd *qc)
                u8 *scsicmd = cmd->cmnd;
 
                if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
-                       u8 *buf = NULL;
-                       unsigned int buflen;
+                       unsigned long flags;
+                       u8 *buf;
 
-                       buflen = ata_scsi_rbuf_get(cmd, &buf);
+                       buf = ata_scsi_rbuf_get(cmd, true, &flags);
 
        /* ATAPI devices typically report zero for their SCSI version,
         * and sometimes deviate from the spec WRT response data
@@ -2441,7 +2592,7 @@ static void atapi_qc_complete(struct ata_queued_cmd *qc)
                                buf[3] = 0x32;
                        }
 
-                       ata_scsi_rbuf_put(cmd, buf);
+                       ata_scsi_rbuf_put(cmd, true, &flags);
                }
 
                cmd->result = SAM_STAT_GOOD;
@@ -2464,8 +2615,8 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
 {
        struct scsi_cmnd *scmd = qc->scsicmd;
        struct ata_device *dev = qc->dev;
-       int using_pio = (dev->flags & ATA_DFLAG_PIO);
        int nodata = (scmd->sc_data_direction == DMA_NONE);
+       int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
        unsigned int nbytes;
 
        memset(qc->cdb, 0, dev->cdb_len);
@@ -2480,10 +2631,10 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
        }
 
        qc->tf.command = ATA_CMD_PACKET;
-       qc->nbytes = scsi_bufflen(scmd);
+       ata_qc_set_pc_nbytes(qc);
 
        /* check whether ATAPI DMA is safe */
-       if (!using_pio && ata_check_atapi_dma(qc))
+       if (!nodata && !using_pio && atapi_check_dma(qc))
                using_pio = 1;
 
        /* Some controller variants snoop this value for Packet
@@ -2491,7 +2642,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
         * want to set it properly, and for DMA where it is
         * effectively meaningless.
         */
-       nbytes = min(qc->nbytes, (unsigned int)63 * 1024);
+       nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
 
        /* Most ATAPI devices which honor transfer chunk size don't
         * behave according to the spec when odd chunk size which
@@ -2523,18 +2674,17 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
        qc->tf.lbam = (nbytes & 0xFF);
        qc->tf.lbah = (nbytes >> 8);
 
-       if (using_pio || nodata) {
-               /* no data, or PIO data xfer */
-               if (nodata)
-                       qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
-               else
-                       qc->tf.protocol = ATA_PROT_ATAPI;
-       } else {
+       if (nodata)
+               qc->tf.protocol = ATAPI_PROT_NODATA;
+       else if (using_pio)
+               qc->tf.protocol = ATAPI_PROT_PIO;
+       else {
                /* DMA data xfer */
-               qc->tf.protocol = ATA_PROT_ATAPI_DMA;
+               qc->tf.protocol = ATAPI_PROT_DMA;
                qc->tf.feature |= ATAPI_PKT_DMA;
 
-               if (atapi_dmadir && (scmd->sc_data_direction != DMA_TO_DEVICE))
+               if ((dev->flags & ATA_DFLAG_DMADIR) &&
+                   (scmd->sc_data_direction != DMA_TO_DEVICE))
                        /* some SATA bridges need us to indicate data xfer direction */
                        qc->tf.feature |= ATAPI_DMADIR;
        }
@@ -2547,7 +2697,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
 
 static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
 {
-       if (ap->nr_pmp_links == 0) {
+       if (!sata_pmp_attached(ap)) {
                if (likely(devno < ata_link_max_devices(&ap->link)))
                        return &ap->link.device[devno];
        } else {
@@ -2564,7 +2714,7 @@ static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
        int devno;
 
        /* skip commands not addressed to targets we simulate */
-       if (ap->nr_pmp_links == 0) {
+       if (!sata_pmp_attached(ap)) {
                if (unlikely(scsidev->channel || scsidev->lun))
                        return NULL;
                devno = scsidev->id;
@@ -2578,36 +2728,6 @@ static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
 }
 
 /**
- *     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 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->link->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
  *     @scsidev: SCSI device from which we derive the ATA device
@@ -2628,7 +2748,7 @@ 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_scsi_dev_enabled(dev)))
+       if (unlikely(!dev || !ata_dev_enabled(dev)))
                return NULL;
 
        return dev;
@@ -2691,28 +2811,6 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
                goto invalid_fld;
 
        /*
-        * Filter TPM commands by default. These provide an
-        * essentially uncontrolled encrypted "back door" between
-        * applications and the disk. Set libata.allow_tpm=1 if you
-        * have a real reason for wanting to use them. This ensures
-        * that installed software cannot easily mess stuff up without
-        * user intent. DVR type users will probably ship with this enabled
-        * for movie content management.
-        *
-        * Note that for ATA8 we can issue a DCS change and DCS freeze lock
-        * for this and should do in future but that it is not sufficient as
-        * DCS is an optional feature set. Thus we also do the software filter
-        * so that we comply with the TC consortium stated goal that the user
-        * can turn off TC features of their system.
-        */
-       if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
-               goto invalid_fld;
-
-       /* We may not issue DMA commands if no DMA mode is set */
-       if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
-               goto invalid_fld;
-
-       /*
         * 12 and 16 byte CDBs use different offsets to
         * provide the various register values.
         */
@@ -2761,6 +2859,41 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
        tf->device = dev->devno ?
                tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
 
+       /* READ/WRITE LONG use a non-standard sect_size */
+       qc->sect_size = ATA_SECT_SIZE;
+       switch (tf->command) {
+       case ATA_CMD_READ_LONG:
+       case ATA_CMD_READ_LONG_ONCE:
+       case ATA_CMD_WRITE_LONG:
+       case ATA_CMD_WRITE_LONG_ONCE:
+               if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
+                       goto invalid_fld;
+               qc->sect_size = scsi_bufflen(scmd);
+       }
+
+       /*
+        * Set flags so that all registers will be written, pass on
+        * write indication (used for PIO/DMA setup), result TF is
+        * copied back and we don't whine too much about its failure.
+        */
+       tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
+       if (scmd->sc_data_direction == DMA_TO_DEVICE)
+               tf->flags |= ATA_TFLAG_WRITE;
+
+       qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
+
+       /*
+        * Set transfer length.
+        *
+        * TODO: find out if we need to do more here to
+        *       cover scatter/gather case.
+        */
+       ata_qc_set_pc_nbytes(qc);
+
+       /* We may not issue DMA commands if no DMA mode is set */
+       if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
+               goto invalid_fld;
+
        /* sanity check for pio multi commands */
        if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
                goto invalid_fld;
@@ -2777,18 +2910,6 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
                                       multi_count);
        }
 
-       /* READ/WRITE LONG use a non-standard sect_size */
-       qc->sect_size = ATA_SECT_SIZE;
-       switch (tf->command) {
-       case ATA_CMD_READ_LONG:
-       case ATA_CMD_READ_LONG_ONCE:
-       case ATA_CMD_WRITE_LONG:
-       case ATA_CMD_WRITE_LONG_ONCE:
-               if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
-                       goto invalid_fld;
-               qc->sect_size = scsi_bufflen(scmd);
-       }
-
        /*
         * Filter SET_FEATURES - XFER MODE command -- otherwise,
         * SET_FEATURES - XFER MODE must be preceded/succeeded
@@ -2796,30 +2917,79 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
         * controller (i.e. the reason for ->set_piomode(),
         * ->set_dmamode(), and ->post_set_mode() hooks).
         */
-       if ((tf->command == ATA_CMD_SET_FEATURES)
-        && (tf->feature == SETFEATURES_XFER))
+       if (tf->command == ATA_CMD_SET_FEATURES &&
+           tf->feature == SETFEATURES_XFER)
                goto invalid_fld;
 
        /*
-        * Set flags so that all registers will be written,
-        * and pass on write indication (used for PIO/DMA
-        * setup.)
+        * Filter TPM commands by default. These provide an
+        * essentially uncontrolled encrypted "back door" between
+        * applications and the disk. Set libata.allow_tpm=1 if you
+        * have a real reason for wanting to use them. This ensures
+        * that installed software cannot easily mess stuff up without
+        * user intent. DVR type users will probably ship with this enabled
+        * for movie content management.
+        *
+        * Note that for ATA8 we can issue a DCS change and DCS freeze lock
+        * for this and should do in future but that it is not sufficient as
+        * DCS is an optional feature set. Thus we also do the software filter
+        * so that we comply with the TC consortium stated goal that the user
+        * can turn off TC features of their system.
         */
-       tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
+       if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
+               goto invalid_fld;
 
-       if (scmd->sc_data_direction == DMA_TO_DEVICE)
-               tf->flags |= ATA_TFLAG_WRITE;
+       return 0;
+
+ invalid_fld:
+       ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
+       /* "Invalid field in cdb" */
+       return 1;
+}
+
+static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
+{
+       struct ata_taskfile *tf = &qc->tf;
+       struct scsi_cmnd *scmd = qc->scsicmd;
+       struct ata_device *dev = qc->dev;
+       const u8 *cdb = scmd->cmnd;
+       u64 block;
+       u32 n_block;
+       u32 size;
+       void *buf;
+
+       /* we may not issue DMA commands if no DMA mode is set */
+       if (unlikely(!dev->dma_mode))
+               goto invalid_fld;
+
+       if (unlikely(scmd->cmd_len < 16))
+               goto invalid_fld;
+       scsi_16_lba_len(cdb, &block, &n_block);
+
+       /* for now we only support WRITE SAME with the unmap bit set */
+       if (unlikely(!(cdb[1] & 0x8)))
+               goto invalid_fld;
 
        /*
-        * Set transfer length.
-        *
-        * TODO: find out if we need to do more here to
-        *       cover scatter/gather case.
+        * WRITE SAME always has a sector sized buffer as payload, this
+        * should never be a multiple entry S/G list.
         */
-       qc->nbytes = scsi_bufflen(scmd);
+       if (!scsi_sg_count(scmd))
+               goto invalid_fld;
 
-       /* request result TF and be quiet about device error */
-       qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
+       buf = page_address(sg_page(scsi_sglist(scmd)));
+       size = ata_set_lba_range_entries(buf, 512, block, n_block);
+
+       tf->protocol = ATA_PROT_DMA;
+       tf->hob_feature = 0;
+       tf->feature = ATA_DSM_TRIM;
+       tf->hob_nsect = (size / 512) >> 8;
+       tf->nsect = size / 512;
+       tf->command = ATA_CMD_DSM;
+       tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
+                    ATA_TFLAG_WRITE;
+
+       ata_qc_set_pc_nbytes(qc);
 
        return 0;
 
@@ -2853,6 +3023,9 @@ static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
        case WRITE_16:
                return ata_scsi_rw_xlat;
 
+       case WRITE_SAME_16:
+               return ata_scsi_write_same_xlat;
+
        case SYNCHRONIZE_CACHE:
                if (ata_try_flush_cache(dev))
                        return ata_scsi_flush_xlat;
@@ -3042,6 +3215,12 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
                case 0x89:
                        ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
                        break;
+               case 0xb0:
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
+                       break;
+               case 0xb1:
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
+                       break;
                default:
                        ata_scsi_invalid_field(cmd, done);
                        break;
@@ -3170,12 +3349,12 @@ void ata_scsi_scan_host(struct ata_port *ap, int sync)
                return;
 
  repeat:
-       ata_port_for_each_link(link, ap) {
-               ata_link_for_each_dev(dev, link) {
+       ata_for_each_link(link, ap, EDGE) {
+               ata_for_each_dev(dev, link, ENABLED) {
                        struct scsi_device *sdev;
                        int channel = 0, id = 0;
 
-                       if (!ata_dev_enabled(dev) || dev->sdev)
+                       if (dev->sdev)
                                continue;
 
                        if (ata_is_host_link(link))
@@ -3196,9 +3375,9 @@ void ata_scsi_scan_host(struct ata_port *ap, int sync)
         * failure occurred, scan would have failed silently.  Check
         * whether all devices are attached.
         */
-       ata_port_for_each_link(link, ap) {
-               ata_link_for_each_dev(dev, link) {
-                       if (ata_dev_enabled(dev) && !dev->sdev)
+       ata_for_each_link(link, ap, EDGE) {
+               ata_for_each_dev(dev, link, ENABLED) {
+                       if (!dev->sdev)
                                goto exit_loop;
                }
        }
@@ -3310,7 +3489,7 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
 
        if (sdev) {
                ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
-                              sdev->sdev_gendev.bus_id);
+                              dev_name(&sdev->sdev_gendev));
 
                scsi_remove_device(sdev);
                scsi_device_put(sdev);
@@ -3322,7 +3501,7 @@ static void ata_scsi_handle_link_detach(struct ata_link *link)
        struct ata_port *ap = link->ap;
        struct ata_device *dev;
 
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ALL) {
                unsigned long flags;
 
                if (!(dev->flags & ATA_DFLAG_DETACHED))
@@ -3422,7 +3601,7 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
        if (lun != SCAN_WILD_CARD && lun)
                return -EINVAL;
 
-       if (ap->nr_pmp_links == 0) {
+       if (!sata_pmp_attached(ap)) {
                if (channel != SCAN_WILD_CARD && channel)
                        return -EINVAL;
                devno = id;
@@ -3437,10 +3616,10 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
        if (devno == SCAN_WILD_CARD) {
                struct ata_link *link;
 
-               ata_port_for_each_link(link, ap) {
+               ata_for_each_link(link, ap, EDGE) {
                        struct ata_eh_info *ehi = &link->eh_info;
-                       ehi->probe_mask |= (1 << ata_link_max_devices(link)) - 1;
-                       ehi->action |= ATA_EH_SOFTRESET;
+                       ehi->probe_mask |= ATA_ALL_DEVICES;
+                       ehi->action |= ATA_EH_RESET;
                }
        } else {
                struct ata_device *dev = ata_find_dev(ap, devno);
@@ -3448,8 +3627,7 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
                if (dev) {
                        struct ata_eh_info *ehi = &dev->link->eh_info;
                        ehi->probe_mask |= 1 << dev->devno;
-                       ehi->action |= ATA_EH_SOFTRESET;
-                       ehi->flags |= ATA_EHI_RESUME_LINK;
+                       ehi->action |= ATA_EH_RESET;
                } else
                        rc = -EINVAL;
        }
@@ -3486,11 +3664,11 @@ void ata_scsi_dev_rescan(struct work_struct *work)
 
        spin_lock_irqsave(ap->lock, flags);
 
-       ata_port_for_each_link(link, ap) {
-               ata_link_for_each_dev(dev, link) {
+       ata_for_each_link(link, ap, EDGE) {
+               ata_for_each_dev(dev, link, ENABLED) {
                        struct scsi_device *sdev = dev->sdev;
 
-                       if (!ata_dev_enabled(dev) || !sdev)
+                       if (!sdev)
                                continue;
                        if (scsi_device_get(sdev))
                                continue;
@@ -3546,7 +3724,7 @@ EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  *     @ap: Port to initialize
  *
  *     Called just after data structures for each port are
- *     initialized.  Allocates DMA pad.
+ *     initialized.
  *
  *     May be used as the port_start() entry in ata_port_operations.
  *
@@ -3555,7 +3733,7 @@ EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  */
 int ata_sas_port_start(struct ata_port *ap)
 {
-       return ata_pad_alloc(ap, ap->dev);
+       return 0;
 }
 EXPORT_SYMBOL_GPL(ata_sas_port_start);
 
@@ -3563,8 +3741,6 @@ EXPORT_SYMBOL_GPL(ata_sas_port_start);
  *     ata_port_stop - Undo ata_sas_port_start()
  *     @ap: Port to shut down
  *
- *     Frees the DMA pad.
- *
  *     May be used as the port_stop() entry in ata_port_operations.
  *
  *     LOCKING:
@@ -3573,7 +3749,6 @@ EXPORT_SYMBOL_GPL(ata_sas_port_start);
 
 void ata_sas_port_stop(struct ata_port *ap)
 {
-       ata_pad_free(ap, ap->dev);
 }
 EXPORT_SYMBOL_GPL(ata_sas_port_stop);
 
@@ -3650,7 +3825,7 @@ int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
 
        ata_scsi_dump_cdb(ap, cmd);
 
-       if (likely(ata_scsi_dev_enabled(ap->link.device)))
+       if (likely(ata_dev_enabled(ap->link.device)))
                rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
        else {
                cmd->result = (DID_BAD_TARGET << 16);