libata: convert to chained sg
[safe/jmp/linux-2.6] / drivers / ata / libata-core.c
index 9e7f55b..e998028 100644 (file)
  *  Hardware documentation available from http://www.t13.org/ and
  *  http://www.sata-io.org/
  *
+ *  Standards documents from:
+ *     http://www.t13.org (ATA standards, PCI DMA IDE spec)
+ *     http://www.t10.org (SCSI MMC - for ATAPI MMC)
+ *     http://www.sata-io.org (SATA)
+ *     http://www.compactflash.org (CF)
+ *     http://www.qic.org (QIC157 - Tape and DSC)
+ *     http://www.ce-ata.org (CE-ATA: not supported)
+ *
  */
 
 #include <linux/kernel.h>
 #include <linux/workqueue.h>
 #include <linux/jiffies.h>
 #include <linux/scatterlist.h>
+#include <linux/io.h>
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_host.h>
 #include <linux/libata.h>
-#include <asm/io.h>
 #include <asm/semaphore.h>
 #include <asm/byteorder.h>
+#include <linux/cdrom.h>
 
 #include "libata.h"
 
@@ -68,7 +77,8 @@ const unsigned long sata_deb_timing_long[]            = { 100, 2000, 5000 };
 static unsigned int ata_dev_init_params(struct ata_device *dev,
                                        u16 heads, u16 sectors);
 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
-static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable);
+static unsigned int ata_dev_set_feature(struct ata_device *dev,
+                                       u8 enable, u8 feature);
 static void ata_dev_xfermask(struct ata_device *dev);
 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
 
@@ -93,17 +103,25 @@ int libata_fua = 0;
 module_param_named(fua, libata_fua, int, 0444);
 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
 
-static int ata_ignore_hpa = 0;
+static int ata_ignore_hpa;
 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
 
+static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
+module_param_named(dma, libata_dma_mask, int, 0444);
+MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
+
 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
 module_param(ata_probe_timeout, int, 0444);
 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
 
-int libata_noacpi = 1;
+int libata_noacpi = 0;
 module_param_named(noacpi, libata_noacpi, int, 0444);
-MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
+MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
+
+int libata_allow_tpm = 0;
+module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
+MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
 
 MODULE_AUTHOR("Jeff Garzik");
 MODULE_DESCRIPTION("Library module for ATA devices");
@@ -436,9 +454,9 @@ int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
  *     RETURNS:
  *     Packed xfer_mask.
  */
-static unsigned int ata_pack_xfermask(unsigned int pio_mask,
-                                     unsigned int mwdma_mask,
-                                     unsigned int udma_mask)
+unsigned long ata_pack_xfermask(unsigned long pio_mask,
+                               unsigned long mwdma_mask,
+                               unsigned long udma_mask)
 {
        return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
                ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
@@ -455,10 +473,8 @@ static unsigned int ata_pack_xfermask(unsigned int pio_mask,
  *     Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  *     Any NULL distination masks will be ignored.
  */
-static void ata_unpack_xfermask(unsigned int xfer_mask,
-                               unsigned int *pio_mask,
-                               unsigned int *mwdma_mask,
-                               unsigned int *udma_mask)
+void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
+                        unsigned long *mwdma_mask, unsigned long *udma_mask)
 {
        if (pio_mask)
                *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
@@ -472,9 +488,9 @@ static const struct ata_xfer_ent {
        int shift, bits;
        u8 base;
 } ata_xfer_tbl[] = {
-       { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
-       { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
-       { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
+       { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
+       { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
+       { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
        { -1, },
 };
 
@@ -489,9 +505,9 @@ static const struct ata_xfer_ent {
  *     None.
  *
  *     RETURNS:
- *     Matching XFER_* value, 0 if no match found.
+ *     Matching XFER_* value, 0xff if no match found.
  */
-static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
+u8 ata_xfer_mask2mode(unsigned long xfer_mask)
 {
        int highbit = fls(xfer_mask) - 1;
        const struct ata_xfer_ent *ent;
@@ -499,7 +515,7 @@ static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
        for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
                if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
                        return ent->base + highbit - ent->shift;
-       return 0;
+       return 0xff;
 }
 
 /**
@@ -514,13 +530,14 @@ static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
  *     RETURNS:
  *     Matching xfer_mask, 0 if no match found.
  */
-static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
+unsigned long ata_xfer_mode2mask(u8 xfer_mode)
 {
        const struct ata_xfer_ent *ent;
 
        for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
                if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
-                       return 1 << (ent->shift + xfer_mode - ent->base);
+                       return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
+                               & ~((1 << ent->shift) - 1);
        return 0;
 }
 
@@ -536,7 +553,7 @@ static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
  *     RETURNS:
  *     Matching xfer_shift, -1 if no match found.
  */
-static int ata_xfer_mode2shift(unsigned int xfer_mode)
+int ata_xfer_mode2shift(unsigned long xfer_mode)
 {
        const struct ata_xfer_ent *ent;
 
@@ -560,7 +577,7 @@ static int ata_xfer_mode2shift(unsigned int xfer_mode)
  *     Constant C string representing highest speed listed in
  *     @mode_mask, or the constant C string "<n/a>".
  */
-static const char *ata_mode_string(unsigned int xfer_mask)
+const char *ata_mode_string(unsigned long xfer_mask)
 {
        static const char * const xfer_mode_str[] = {
                "PIO0",
@@ -609,12 +626,190 @@ void ata_dev_disable(struct ata_device *dev)
        if (ata_dev_enabled(dev)) {
                if (ata_msg_drv(dev->link->ap))
                        ata_dev_printk(dev, KERN_WARNING, "disabled\n");
+               ata_acpi_on_disable(dev);
                ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
                                             ATA_DNXFER_QUIET);
                dev->class++;
        }
 }
 
+static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
+{
+       struct ata_link *link = dev->link;
+       struct ata_port *ap = link->ap;
+       u32 scontrol;
+       unsigned int err_mask;
+       int rc;
+
+       /*
+        * disallow DIPM for drivers which haven't set
+        * ATA_FLAG_IPM.  This is because when DIPM is enabled,
+        * phy ready will be set in the interrupt status on
+        * state changes, which will cause some drivers to
+        * think there are errors - additionally drivers will
+        * need to disable hot plug.
+        */
+       if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
+               ap->pm_policy = NOT_AVAILABLE;
+               return -EINVAL;
+       }
+
+       /*
+        * For DIPM, we will only enable it for the
+        * min_power setting.
+        *
+        * Why?  Because Disks are too stupid to know that
+        * If the host rejects a request to go to SLUMBER
+        * they should retry at PARTIAL, and instead it
+        * just would give up.  So, for medium_power to
+        * work at all, we need to only allow HIPM.
+        */
+       rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
+       if (rc)
+               return rc;
+
+       switch (policy) {
+       case MIN_POWER:
+               /* no restrictions on IPM transitions */
+               scontrol &= ~(0x3 << 8);
+               rc = sata_scr_write(link, SCR_CONTROL, scontrol);
+               if (rc)
+                       return rc;
+
+               /* enable DIPM */
+               if (dev->flags & ATA_DFLAG_DIPM)
+                       err_mask = ata_dev_set_feature(dev,
+                                       SETFEATURES_SATA_ENABLE, SATA_DIPM);
+               break;
+       case MEDIUM_POWER:
+               /* allow IPM to PARTIAL */
+               scontrol &= ~(0x1 << 8);
+               scontrol |= (0x2 << 8);
+               rc = sata_scr_write(link, SCR_CONTROL, scontrol);
+               if (rc)
+                       return rc;
+
+               /*
+                * we don't have to disable DIPM since IPM flags
+                * disallow transitions to SLUMBER, which effectively
+                * disable DIPM if it does not support PARTIAL
+                */
+               break;
+       case NOT_AVAILABLE:
+       case MAX_PERFORMANCE:
+               /* disable all IPM transitions */
+               scontrol |= (0x3 << 8);
+               rc = sata_scr_write(link, SCR_CONTROL, scontrol);
+               if (rc)
+                       return rc;
+
+               /*
+                * we don't have to disable DIPM since IPM flags
+                * disallow all transitions which effectively
+                * disable DIPM anyway.
+                */
+               break;
+       }
+
+       /* FIXME: handle SET FEATURES failure */
+       (void) err_mask;
+
+       return 0;
+}
+
+/**
+ *     ata_dev_enable_pm - enable SATA interface power management
+ *     @dev:  device to enable power management
+ *     @policy: the link power management policy
+ *
+ *     Enable SATA Interface power management.  This will enable
+ *     Device Interface Power Management (DIPM) for min_power
+ *     policy, and then call driver specific callbacks for
+ *     enabling Host Initiated Power management.
+ *
+ *     Locking: Caller.
+ *     Returns: -EINVAL if IPM is not supported, 0 otherwise.
+ */
+void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
+{
+       int rc = 0;
+       struct ata_port *ap = dev->link->ap;
+
+       /* set HIPM first, then DIPM */
+       if (ap->ops->enable_pm)
+               rc = ap->ops->enable_pm(ap, policy);
+       if (rc)
+               goto enable_pm_out;
+       rc = ata_dev_set_dipm(dev, policy);
+
+enable_pm_out:
+       if (rc)
+               ap->pm_policy = MAX_PERFORMANCE;
+       else
+               ap->pm_policy = policy;
+       return /* rc */;        /* hopefully we can use 'rc' eventually */
+}
+
+#ifdef CONFIG_PM
+/**
+ *     ata_dev_disable_pm - disable SATA interface power management
+ *     @dev: device to disable power management
+ *
+ *     Disable SATA Interface power management.  This will disable
+ *     Device Interface Power Management (DIPM) without changing
+ *     policy,  call driver specific callbacks for disabling Host
+ *     Initiated Power management.
+ *
+ *     Locking: Caller.
+ *     Returns: void
+ */
+static void ata_dev_disable_pm(struct ata_device *dev)
+{
+       struct ata_port *ap = dev->link->ap;
+
+       ata_dev_set_dipm(dev, MAX_PERFORMANCE);
+       if (ap->ops->disable_pm)
+               ap->ops->disable_pm(ap);
+}
+#endif /* CONFIG_PM */
+
+void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
+{
+       ap->pm_policy = policy;
+       ap->link.eh_info.action |= ATA_EHI_LPM;
+       ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
+       ata_port_schedule_eh(ap);
+}
+
+#ifdef CONFIG_PM
+static void ata_lpm_enable(struct ata_host *host)
+{
+       struct ata_link *link;
+       struct ata_port *ap;
+       struct ata_device *dev;
+       int i;
+
+       for (i = 0; i < host->n_ports; i++) {
+               ap = host->ports[i];
+               ata_port_for_each_link(link, ap) {
+                       ata_link_for_each_dev(dev, link)
+                               ata_dev_disable_pm(dev);
+               }
+       }
+}
+
+static void ata_lpm_disable(struct ata_host *host)
+{
+       int i;
+
+       for (i = 0; i < host->n_ports; i++) {
+               struct ata_port *ap = host->ports[i];
+               ata_lpm_schedule(ap, ap->pm_policy);
+       }
+}
+#endif /* CONFIG_PM */
+
+
 /**
  *     ata_devchk - PATA device presence detection
  *     @ap: ATA channel to examine
@@ -670,29 +865,49 @@ static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  *     None.
  *
  *     RETURNS:
- *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
- *     the event of failure.
+ *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
+ *     %ATA_DEV_UNKNOWN the event of failure.
  */
-
 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
 {
        /* Apple's open source Darwin code hints that some devices only
         * put a proper signature into the LBA mid/high registers,
         * So, we only check those.  It's sufficient for uniqueness.
+        *
+        * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
+        * signatures for ATA and ATAPI devices attached on SerialATA,
+        * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
+        * spec has never mentioned about using different signatures
+        * for ATA/ATAPI devices.  Then, Serial ATA II: Port
+        * Multiplier specification began to use 0x69/0x96 to identify
+        * port multpliers and 0x3c/0xc3 to identify SEMB device.
+        * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
+        * 0x69/0x96 shortly and described them as reserved for
+        * SerialATA.
+        *
+        * We follow the current spec and consider that 0x69/0x96
+        * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
         */
-
-       if (((tf->lbam == 0) && (tf->lbah == 0)) ||
-           ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
+       if ((tf->lbam == 0) && (tf->lbah == 0)) {
                DPRINTK("found ATA device by sig\n");
                return ATA_DEV_ATA;
        }
 
-       if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
-           ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
+       if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
                DPRINTK("found ATAPI device by sig\n");
                return ATA_DEV_ATAPI;
        }
 
+       if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
+               DPRINTK("found PMP device by sig\n");
+               return ATA_DEV_PMP;
+       }
+
+       if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
+               printk(KERN_INFO "ata: SEMB device ignored\n");
+               return ATA_DEV_SEMB_UNSUP; /* not yet */
+       }
+
        DPRINTK("unknown device\n");
        return ATA_DEV_UNKNOWN;
 }
@@ -902,7 +1117,7 @@ static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
        tf.protocol |= ATA_PROT_NODATA;
        tf.device |= ATA_LBA;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_WARNING, "failed to read native "
                               "max address (err_mask=0x%x)\n", err_mask);
@@ -915,7 +1130,7 @@ static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
                *max_sectors = ata_tf_to_lba48(&tf);
        else
                *max_sectors = ata_tf_to_lba(&tf);
-        if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
+       if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
                (*max_sectors)--;
        return 0;
 }
@@ -964,7 +1179,7 @@ static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
        tf.lbam = (new_sectors >> 8) & 0xff;
        tf.lbah = (new_sectors >> 16) & 0xff;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_WARNING, "failed to set "
                               "max address (err_mask=0x%x)\n", err_mask);
@@ -1074,48 +1289,6 @@ static int ata_hpa_resize(struct ata_device *dev)
 }
 
 /**
- *     ata_id_to_dma_mode      -       Identify DMA mode from id block
- *     @dev: device to identify
- *     @unknown: mode to assume if we cannot tell
- *
- *     Set up the timing values for the device based upon the identify
- *     reported values for the DMA mode. This function is used by drivers
- *     which rely upon firmware configured modes, but wish to report the
- *     mode correctly when possible.
- *
- *     In addition we emit similarly formatted messages to the default
- *     ata_dev_set_mode handler, in order to provide consistency of
- *     presentation.
- */
-
-void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
-{
-       unsigned int mask;
-       u8 mode;
-
-       /* Pack the DMA modes */
-       mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
-       if (dev->id[53] & 0x04)
-               mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
-
-       /* Select the mode in use */
-       mode = ata_xfer_mask2mode(mask);
-
-       if (mode != 0) {
-               ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
-                      ata_mode_string(mask));
-       } else {
-               /* SWDMA perhaps ? */
-               mode = unknown;
-               ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
-       }
-
-       /* Configure the device reporting */
-       dev->xfer_mode = mode;
-       dev->xfer_shift = ata_xfer_mode2shift(mode);
-}
-
-/**
  *     ata_noop_dev_select - Select device 0/1 on ATA bus
  *     @ap: ATA channel to manipulate
  *     @device: ATA device (numbered from zero) to select
@@ -1127,7 +1300,7 @@ void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
  *     LOCKING:
  *     caller.
  */
-void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
+void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
 {
 }
 
@@ -1147,7 +1320,7 @@ void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
  *     caller.
  */
 
-void ata_std_dev_select (struct ata_port *ap, unsigned int device)
+void ata_std_dev_select(struct ata_port *ap, unsigned int device)
 {
        u8 tmp;
 
@@ -1252,9 +1425,9 @@ static inline void ata_dump_id(const u16 *id)
  *     RETURNS:
  *     Computed xfermask
  */
-static unsigned int ata_id_xfermask(const u16 *id)
+unsigned long ata_id_xfermask(const u16 *id)
 {
-       unsigned int pio_mask, mwdma_mask, udma_mask;
+       unsigned long pio_mask, mwdma_mask, udma_mask;
 
        /* Usual case. Word 53 indicates word 64 is valid */
        if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
@@ -1268,7 +1441,7 @@ static unsigned int ata_id_xfermask(const u16 *id)
                 */
                u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
                if (mode < 5)   /* Valid PIO range */
-                       pio_mask = (2 << mode) - 1;
+                       pio_mask = (2 << mode) - 1;
                else
                        pio_mask = 1;
 
@@ -1368,8 +1541,9 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  *     @tf: Taskfile registers for the command and the result
  *     @cdb: CDB for packet command
  *     @dma_dir: Data tranfer direction of the command
- *     @sg: sg list for the data buffer of the command
+ *     @sgl: sg list for the data buffer of the command
  *     @n_elem: Number of sg entries
+ *     @timeout: Timeout in msecs (0 for default)
  *
  *     Executes libata internal command with timeout.  @tf contains
  *     command on entry and result on return.  Timeout and error
@@ -1385,8 +1559,8 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  */
 unsigned ata_exec_internal_sg(struct ata_device *dev,
                              struct ata_taskfile *tf, const u8 *cdb,
-                             int dma_dir, struct scatterlist *sg,
-                             unsigned int n_elem)
+                             int dma_dir, struct scatterlist *sgl,
+                             unsigned int n_elem, unsigned long timeout)
 {
        struct ata_link *link = dev->link;
        struct ata_port *ap = link->ap;
@@ -1447,11 +1621,12 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
        qc->dma_dir = dma_dir;
        if (dma_dir != DMA_NONE) {
                unsigned int i, buflen = 0;
+               struct scatterlist *sg;
 
-               for (i = 0; i < n_elem; i++)
-                       buflen += sg[i].length;
+               for_each_sg(sgl, sg, n_elem, i)
+                       buflen += sg->length;
 
-               ata_sg_init(qc, sg, n_elem);
+               ata_sg_init(qc, sgl, n_elem);
                qc->nbytes = buflen;
        }
 
@@ -1462,7 +1637,10 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
 
        spin_unlock_irqrestore(ap->lock, flags);
 
-       rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
+       if (!timeout)
+               timeout = ata_probe_timeout * 1000 / HZ;
+
+       rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
 
        ata_port_flush_task(ap);
 
@@ -1547,6 +1725,7 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
  *     @dma_dir: Data tranfer direction of the command
  *     @buf: Data buffer of the command
  *     @buflen: Length of data buffer
+ *     @timeout: Timeout in msecs (0 for default)
  *
  *     Wrapper around ata_exec_internal_sg() which takes simple
  *     buffer instead of sg list.
@@ -1559,7 +1738,8 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
  */
 unsigned ata_exec_internal(struct ata_device *dev,
                           struct ata_taskfile *tf, const u8 *cdb,
-                          int dma_dir, void *buf, unsigned int buflen)
+                          int dma_dir, void *buf, unsigned int buflen,
+                          unsigned long timeout)
 {
        struct scatterlist *psg = NULL, sg;
        unsigned int n_elem = 0;
@@ -1571,7 +1751,8 @@ unsigned ata_exec_internal(struct ata_device *dev,
                n_elem++;
        }
 
-       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
+       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
+                                   timeout);
 }
 
 /**
@@ -1598,7 +1779,7 @@ unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
        tf.flags |= ATA_TFLAG_DEVICE;
        tf.protocol = ATA_PROT_NODATA;
 
-       return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 }
 
 /**
@@ -1661,7 +1842,7 @@ static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
  *     for pre-ATA4 drives.
  *
  *     FIXME: ATA_CMD_ID_ATA is optional for early drives and right
- *     now we abort if we hit that case. 
+ *     now we abort if we hit that case.
  *
  *     LOCKING:
  *     Kernel thread context (may sleep)
@@ -1713,7 +1894,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
        tf.flags |= ATA_TFLAG_POLLING;
 
        err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
-                                    id, sizeof(id[0]) * ATA_ID_WORDS);
+                                    id, sizeof(id[0]) * ATA_ID_WORDS, 0);
        if (err_mask) {
                if (err_mask & AC_ERR_NODEV_HINT) {
                        DPRINTK("ata%u.%d: NODEV after polling detection\n",
@@ -1767,12 +1948,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                 * SET_FEATURES spin-up subcommand before it will accept
                 * anything other than the original IDENTIFY command.
                 */
-               ata_tf_init(dev, &tf);
-               tf.command = ATA_CMD_SET_FEATURES;
-               tf.feature = SETFEATURES_SPINUP;
-               tf.protocol = ATA_PROT_NODATA;
-               tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
-               err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+               err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
                if (err_mask && id[2] != 0x738c) {
                        rc = -EIO;
                        reason = "SPINUP failed";
@@ -1875,7 +2051,7 @@ int ata_dev_configure(struct ata_device *dev)
        struct ata_eh_context *ehc = &dev->link->eh_context;
        int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
        const u16 *id = dev->id;
-       unsigned int xfer_mask;
+       unsigned long xfer_mask;
        char revbuf[7];         /* XYZ-99\0 */
        char fwrevbuf[ATA_ID_FW_REV_LEN+1];
        char modelbuf[ATA_ID_PROD_LEN+1];
@@ -1946,9 +2122,14 @@ int ata_dev_configure(struct ata_device *dev)
                                               "supports DRM functions and may "
                                               "not be fully accessable.\n");
                        snprintf(revbuf, 7, "CFA");
+               } else {
+                       snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
+                       /* Warn the user if the device has TPM extensions */
+                       if (ata_id_has_tpm(id))
+                               ata_dev_printk(dev, KERN_WARNING,
+                                              "supports DRM functions and may "
+                                              "not be fully accessable.\n");
                }
-               else
-                       snprintf(revbuf, 7, "ATA-%d",  ata_id_major_version(id));
 
                dev->n_sectors = ata_id_n_sectors(id);
 
@@ -2043,7 +2224,8 @@ int ata_dev_configure(struct ata_device *dev)
                        unsigned int err_mask;
 
                        /* issue SET feature command to turn this on */
-                       err_mask = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
+                       err_mask = ata_dev_set_feature(dev,
+                                       SETFEATURES_SATA_ENABLE, SATA_AN);
                        if (err_mask)
                                ata_dev_printk(dev, KERN_ERR,
                                        "failed to enable ATAPI AN "
@@ -2073,11 +2255,18 @@ int ata_dev_configure(struct ata_device *dev)
        if (dev->flags & ATA_DFLAG_LBA48)
                dev->max_sectors = ATA_MAX_SECTORS_LBA48;
 
+       if (!(dev->horkage & ATA_HORKAGE_IPM)) {
+               if (ata_id_has_hipm(dev->id))
+                       dev->flags |= ATA_DFLAG_HIPM;
+               if (ata_id_has_dipm(dev->id))
+                       dev->flags |= ATA_DFLAG_DIPM;
+       }
+
        if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
                /* Let the user know. We don't want to disallow opens for
                   rescue purposes, or in case the vendor is just a blithering
                   idiot */
-                if (print_info) {
+               if (print_info) {
                        ata_dev_printk(dev, KERN_WARNING,
 "Drive reports diagnostics failure. This may indicate a drive\n");
                        ata_dev_printk(dev, KERN_WARNING,
@@ -2094,10 +2283,23 @@ int ata_dev_configure(struct ata_device *dev)
                dev->max_sectors = ATA_MAX_SECTORS;
        }
 
+       if ((dev->class == ATA_DEV_ATAPI) &&
+           (atapi_command_packet_set(id) == TYPE_TAPE)) {
+               dev->max_sectors = ATA_MAX_SECTORS_TAPE;
+               dev->horkage |= ATA_HORKAGE_STUCK_ERR;
+       }
+
        if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
                dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
                                         dev->max_sectors);
 
+       if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
+               dev->horkage |= ATA_HORKAGE_IPM;
+
+               /* reset link pm_policy for this port to no pm */
+               ap->pm_policy = MAX_PERFORMANCE;
+       }
+
        if (ap->ops->dev_config)
                ap->ops->dev_config(dev);
 
@@ -2152,6 +2354,18 @@ int ata_cable_unknown(struct ata_port *ap)
 }
 
 /**
+ *     ata_cable_ignore        -       return ignored PATA cable.
+ *     @ap: port
+ *
+ *     Helper method for drivers which don't use cable type to limit
+ *     transfer mode.
+ */
+int ata_cable_ignore(struct ata_port *ap)
+{
+       return ATA_CBL_PATA_IGN;
+}
+
+/**
  *     ata_cable_sata  -       return SATA cable type
  *     @ap: port
  *
@@ -2191,6 +2405,25 @@ int ata_bus_probe(struct ata_port *ap)
                tries[dev->devno] = ATA_PROBE_MAX_TRIES;
 
  retry:
+       ata_link_for_each_dev(dev, &ap->link) {
+               /* If we issue an SRST then an ATA drive (not ATAPI)
+                * may change configuration and be in PIO0 timing. If
+                * we do a hard reset (or are coming from power on)
+                * this is true for ATA or ATAPI. Until we've set a
+                * suitable controller mode we should not touch the
+                * bus as we may be talking too fast.
+                */
+               dev->pio_mode = XFER_PIO_0;
+
+               /* If the controller has a pio mode setup function
+                * then use it to set the chipset to rights. Don't
+                * touch the DMA setup as that will be dealt with when
+                * configuring devices.
+                */
+               if (ap->ops->set_piomode)
+                       ap->ops->set_piomode(ap, dev);
+       }
+
        /* reset and determine device classes */
        ap->ops->phy_reset(ap);
 
@@ -2206,12 +2439,6 @@ int ata_bus_probe(struct ata_port *ap)
 
        ata_port_probe(ap);
 
-       /* after the reset the device state is PIO 0 and the controller
-          state is undefined. Record the mode */
-
-       ata_link_for_each_dev(dev, &ap->link)
-               dev->pio_mode = XFER_PIO_0;
-
        /* read IDENTIFY page and configure devices. We have to do the identify
           specific sequence bass-ackwards so that PDIAG- is released by
           the slave device */
@@ -2345,81 +2572,6 @@ void sata_print_link_status(struct ata_link *link)
 }
 
 /**
- *     __sata_phy_reset - Wake/reset a low-level SATA PHY
- *     @ap: SATA port associated with target SATA PHY.
- *
- *     This function issues commands to standard SATA Sxxx
- *     PHY registers, to wake up the phy (and device), and
- *     clear any reset condition.
- *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *
- */
-void __sata_phy_reset(struct ata_port *ap)
-{
-       struct ata_link *link = &ap->link;
-       unsigned long timeout = jiffies + (HZ * 5);
-       u32 sstatus;
-
-       if (ap->flags & ATA_FLAG_SATA_RESET) {
-               /* issue phy wake/reset */
-               sata_scr_write_flush(link, SCR_CONTROL, 0x301);
-               /* Couldn't find anything in SATA I/II specs, but
-                * AHCI-1.1 10.4.2 says at least 1 ms. */
-               mdelay(1);
-       }
-       /* phy wake/clear reset */
-       sata_scr_write_flush(link, SCR_CONTROL, 0x300);
-
-       /* wait for phy to become ready, if necessary */
-       do {
-               msleep(200);
-               sata_scr_read(link, SCR_STATUS, &sstatus);
-               if ((sstatus & 0xf) != 1)
-                       break;
-       } while (time_before(jiffies, timeout));
-
-       /* print link status */
-       sata_print_link_status(link);
-
-       /* TODO: phy layer with polling, timeouts, etc. */
-       if (!ata_link_offline(link))
-               ata_port_probe(ap);
-       else
-               ata_port_disable(ap);
-
-       if (ap->flags & ATA_FLAG_DISABLED)
-               return;
-
-       if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
-               ata_port_disable(ap);
-               return;
-       }
-
-       ap->cbl = ATA_CBL_SATA;
-}
-
-/**
- *     sata_phy_reset - Reset SATA bus.
- *     @ap: SATA port associated with target SATA PHY.
- *
- *     This function resets the SATA bus, and then probes
- *     the bus for devices.
- *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *
- */
-void sata_phy_reset(struct ata_port *ap)
-{
-       __sata_phy_reset(ap);
-       if (ap->flags & ATA_FLAG_DISABLED)
-               return;
-       ata_bus_reset(ap);
-}
-
-/**
  *     ata_dev_pair            -       return other device on cable
  *     @adev: device
  *
@@ -2517,17 +2669,27 @@ int sata_down_spd_limit(struct ata_link *link)
 
 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
 {
-       u32 spd, limit;
+       struct ata_link *host_link = &link->ap->link;
+       u32 limit, target, spd;
+
+       limit = link->sata_spd_limit;
+
+       /* Don't configure downstream link faster than upstream link.
+        * It doesn't speed up anything and some PMPs choke on such
+        * configuration.
+        */
+       if (!ata_is_host_link(link) && host_link->sata_spd)
+               limit &= (1 << host_link->sata_spd) - 1;
 
-       if (link->sata_spd_limit == UINT_MAX)
-               limit = 0;
+       if (limit == UINT_MAX)
+               target = 0;
        else
-               limit = fls(link->sata_spd_limit);
+               target = fls(limit);
 
        spd = (*scontrol >> 4) & 0xf;
-       *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
+       *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
 
-       return spd != limit;
+       return spd != target;
 }
 
 /**
@@ -2550,7 +2712,7 @@ int sata_set_spd_needed(struct ata_link *link)
        u32 scontrol;
 
        if (sata_scr_read(link, SCR_CONTROL, &scontrol))
-               return 0;
+               return 1;
 
        return __sata_set_spd_needed(link, &scontrol);
 }
@@ -2598,44 +2760,39 @@ int sata_set_spd(struct ata_link *link)
  */
 
 static const struct ata_timing ata_timing[] = {
+/*     { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
+       { XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
+       { XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
+       { XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
+       { XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
+       { XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
+       { XFER_PIO_5,     15,  65,  25, 100,  65,  25, 100,   0 },
+       { XFER_PIO_6,     10,  55,  20,  80,  55,  20,  80,   0 },
 
-       { XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
-       { XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
-       { XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
-       { XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
+       { XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
+       { XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
+       { XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
 
-       { XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20,  80,   0 },
+       { XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
+       { XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
+       { XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
        { XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 100,   0 },
-       { XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
-       { XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
-       { XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
+       { XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20,  80,   0 },
 
 /*     { XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
-
-       { XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
-       { XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
-       { XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
-
-       { XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
-       { XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
-       { XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
-
-       { XFER_PIO_6,     10,  55,  20,  80,  55,  20,  80,   0 },
-       { XFER_PIO_5,     15,  65,  25, 100,  65,  25, 100,   0 },
-       { XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
-       { XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
-
-       { XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
-       { XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
-       { XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
-
-/*     { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
+       { XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
+       { XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
+       { XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
+       { XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
+       { XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
+       { XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
+       { XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
 
        { 0xFF }
 };
 
-#define ENOUGH(v,unit)         (((v)-1)/(unit)+1)
-#define EZ(v,unit)             ((v)?ENOUGH(v,unit):0)
+#define ENOUGH(v, unit)                (((v)-1)/(unit)+1)
+#define EZ(v, unit)            ((v)?ENOUGH(v, unit):0)
 
 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
 {
@@ -2662,14 +2819,16 @@ void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
        if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
 }
 
-static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
+const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
 {
-       const struct ata_timing *t;
+       const struct ata_timing *t = ata_timing;
+
+       while (xfer_mode > t->mode)
+               t++;
 
-       for (t = ata_timing; t->mode != speed; t++)
-               if (t->mode == 0xFF)
-                       return NULL;
-       return t;
+       if (xfer_mode == t->mode)
+               return t;
+       return NULL;
 }
 
 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
@@ -2694,10 +2853,10 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
 
        if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
                memset(&p, 0, sizeof(p));
-               if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
+               if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
                        if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
                                            else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
-               } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
+               } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
                        p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
                }
                ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
@@ -2744,6 +2903,57 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
 }
 
 /**
+ *     ata_timing_cycle2mode - find xfer mode for the specified cycle duration
+ *     @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
+ *     @cycle: cycle duration in ns
+ *
+ *     Return matching xfer mode for @cycle.  The returned mode is of
+ *     the transfer type specified by @xfer_shift.  If @cycle is too
+ *     slow for @xfer_shift, 0xff is returned.  If @cycle is faster
+ *     than the fastest known mode, the fasted mode is returned.
+ *
+ *     LOCKING:
+ *     None.
+ *
+ *     RETURNS:
+ *     Matching xfer_mode, 0xff if no match found.
+ */
+u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
+{
+       u8 base_mode = 0xff, last_mode = 0xff;
+       const struct ata_xfer_ent *ent;
+       const struct ata_timing *t;
+
+       for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
+               if (ent->shift == xfer_shift)
+                       base_mode = ent->base;
+
+       for (t = ata_timing_find_mode(base_mode);
+            t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
+               unsigned short this_cycle;
+
+               switch (xfer_shift) {
+               case ATA_SHIFT_PIO:
+               case ATA_SHIFT_MWDMA:
+                       this_cycle = t->cycle;
+                       break;
+               case ATA_SHIFT_UDMA:
+                       this_cycle = t->udma;
+                       break;
+               default:
+                       return 0xff;
+               }
+
+               if (cycle > this_cycle)
+                       break;
+
+               last_mode = t->mode;
+       }
+
+       return last_mode;
+}
+
+/**
  *     ata_down_xfermask_limit - adjust dev xfer masks downward
  *     @dev: Device to adjust xfer masks
  *     @sel: ATA_DNXFER_* selector
@@ -2761,8 +2971,8 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
 {
        char buf[32];
-       unsigned int orig_mask, xfer_mask;
-       unsigned int pio_mask, mwdma_mask, udma_mask;
+       unsigned long orig_mask, xfer_mask;
+       unsigned long pio_mask, mwdma_mask, udma_mask;
        int quiet, highbit;
 
        quiet = !!(sel & ATA_DNXFER_QUIET);
@@ -2843,14 +3053,24 @@ static int ata_dev_set_mode(struct ata_device *dev)
                dev->flags |= ATA_DFLAG_PIO;
 
        err_mask = ata_dev_set_xfermode(dev);
+
        /* Old CFA may refuse this command, which is just fine */
        if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
-               err_mask &= ~AC_ERR_DEV;
+               err_mask &= ~AC_ERR_DEV;
+
        /* Some very old devices and some bad newer ones fail any kind of
           SET_XFERMODE request but support PIO0-2 timings and no IORDY */
        if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
                        dev->pio_mode <= XFER_PIO_2)
                err_mask &= ~AC_ERR_DEV;
+
+       /* Early MWDMA devices do DMA but don't allow DMA mode setting.
+          Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
+       if (dev->xfer_shift == ATA_SHIFT_MWDMA && 
+           dev->dma_mode == XFER_MW_DMA_0 &&
+           (dev->id[63] >> 8) & 1)
+               err_mask &= ~AC_ERR_DEV;
+
        if (err_mask) {
                ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
                               "(err_mask=0x%x)\n", err_mask);
@@ -2896,20 +3116,33 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
 
        /* step 1: calculate xfer_mask */
        ata_link_for_each_dev(dev, link) {
-               unsigned int pio_mask, dma_mask;
+               unsigned long pio_mask, dma_mask;
+               unsigned int mode_mask;
 
                if (!ata_dev_enabled(dev))
                        continue;
 
+               mode_mask = ATA_DMA_MASK_ATA;
+               if (dev->class == ATA_DEV_ATAPI)
+                       mode_mask = ATA_DMA_MASK_ATAPI;
+               else if (ata_id_is_cfa(dev->id))
+                       mode_mask = ATA_DMA_MASK_CFA;
+
                ata_dev_xfermask(dev);
 
                pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
                dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+
+               if (libata_dma_mask & mode_mask)
+                       dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+               else
+                       dma_mask = 0;
+
                dev->pio_mode = ata_xfer_mask2mode(pio_mask);
                dev->dma_mode = ata_xfer_mask2mode(dma_mask);
 
                found = 1;
-               if (dev->dma_mode)
+               if (dev->dma_mode != 0xff)
                        used_dma = 1;
        }
        if (!found)
@@ -2920,7 +3153,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
                if (!ata_dev_enabled(dev))
                        continue;
 
-               if (!dev->pio_mode) {
+               if (dev->pio_mode == 0xff) {
                        ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
                        rc = -EINVAL;
                        goto out;
@@ -2934,7 +3167,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
 
        /* step 3: set host DMA timings */
        ata_link_for_each_dev(dev, link) {
-               if (!ata_dev_enabled(dev) || !dev->dma_mode)
+               if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
                        continue;
 
                dev->xfer_mode = dev->dma_mode;
@@ -2967,31 +3200,6 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
 }
 
 /**
- *     ata_set_mode - Program timings and issue SET FEATURES - XFER
- *     @link: link on which timings will be programmed
- *     @r_failed_dev: out paramter for failed device
- *
- *     Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
- *     ata_set_mode() fails, pointer to the failing device is
- *     returned in @r_failed_dev.
- *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *
- *     RETURNS:
- *     0 on success, negative errno otherwise
- */
-int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
-{
-       struct ata_port *ap = link->ap;
-
-       /* has private set_mode? */
-       if (ap->ops->set_mode)
-               return ap->ops->set_mode(link, r_failed_dev);
-       return ata_do_set_mode(link, r_failed_dev);
-}
-
-/**
  *     ata_tf_to_host - issue ATA taskfile to host controller
  *     @ap: port to which command is being issued
  *     @tf: ATA taskfile register set
@@ -3067,6 +3275,61 @@ int ata_busy_sleep(struct ata_port *ap,
 }
 
 /**
+ *     ata_wait_after_reset - wait before checking status after reset
+ *     @ap: port containing status register to be polled
+ *     @deadline: deadline jiffies for the operation
+ *
+ *     After reset, we need to pause a while before reading status.
+ *     Also, certain combination of controller and device report 0xff
+ *     for some duration (e.g. until SATA PHY is up and running)
+ *     which is interpreted as empty port in ATA world.  This
+ *     function also waits for such devices to get out of 0xff
+ *     status.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep).
+ */
+void ata_wait_after_reset(struct ata_port *ap, unsigned long deadline)
+{
+       unsigned long until = jiffies + ATA_TMOUT_FF_WAIT;
+
+       if (time_before(until, deadline))
+               deadline = until;
+
+       /* Spec mandates ">= 2ms" before checking status.  We wait
+        * 150ms, because that was the magic delay used for ATAPI
+        * devices in Hale Landis's ATADRVR, for the period of time
+        * between when the ATA command register is written, and then
+        * status is checked.  Because waiting for "a while" before
+        * checking status is fine, post SRST, we perform this magic
+        * delay here as well.
+        *
+        * Old drivers/ide uses the 2mS rule and then waits for ready.
+        */
+       msleep(150);
+
+       /* Wait for 0xff to clear.  Some SATA devices take a long time
+        * to clear 0xff after reset.  For example, HHD424020F7SV00
+        * iVDR needs >= 800ms while.  Quantum GoVault needs even more
+        * than that.
+        *
+        * Note that some PATA controllers (pata_ali) explode if
+        * status register is read more than once when there's no
+        * device attached.
+        */
+       if (ap->flags & ATA_FLAG_SATA) {
+               while (1) {
+                       u8 status = ata_chk_status(ap);
+
+                       if (status != 0xff || time_after(jiffies, deadline))
+                               return;
+
+                       msleep(50);
+               }
+       }
+}
+
+/**
  *     ata_wait_ready - sleep until BSY clears, or timeout
  *     @ap: port containing status register to be polled
  *     @deadline: deadline jiffies for the operation
@@ -3182,23 +3445,14 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
        udelay(20);     /* FIXME: flush */
        iowrite8(ap->ctl, ioaddr->ctl_addr);
 
-       /* spec mandates ">= 2ms" before checking status.
-        * We wait 150ms, because that was the magic delay used for
-        * ATAPI devices in Hale Landis's ATADRVR, for the period of time
-        * between when the ATA command register is written, and then
-        * status is checked.  Because waiting for "a while" before
-        * checking status is fine, post SRST, we perform this magic
-        * delay here as well.
-        *
-        * Old drivers/ide uses the 2mS rule and then waits for ready
-        */
-       msleep(150);
+       /* wait a while before checking status */
+       ata_wait_after_reset(ap, deadline);
 
        /* Before we perform post reset processing we want to see if
         * the bus shows 0xFF because the odd clown forgets the D7
         * pulldown resistor.
         */
-       if (ata_check_status(ap) == 0xFF)
+       if (ata_chk_status(ap) == 0xFF)
                return -ENODEV;
 
        return ata_bus_post_reset(ap, devmask, deadline);
@@ -3426,6 +3680,12 @@ int ata_std_prereset(struct ata_link *link, unsigned long deadline)
            (link->flags & ATA_LFLAG_HRST_TO_RESUME))
                ehc->i.action |= ATA_EH_HARDRESET;
 
+       /* Some PMPs don't work with only SRST, force hardreset if PMP
+        * is supported.
+        */
+       if (ap->flags & ATA_FLAG_PMP)
+               ehc->i.action |= ATA_EH_HARDRESET;
+
        /* if we're about to do hardreset, nothing more to do */
        if (ehc->i.action & ATA_EH_HARDRESET)
                return 0;
@@ -3613,8 +3873,18 @@ int sata_std_hardreset(struct ata_link *link, unsigned int *class,
                return 0;
        }
 
-       /* wait a while before checking status, see SRST for more info */
-       msleep(150);
+       /* wait a while before checking status */
+       ata_wait_after_reset(ap, deadline);
+
+       /* If PMP is supported, we have to do follow-up SRST.  Note
+        * that some PMPs don't send D2H Reg FIS after hardreset at
+        * all if the first port is empty.  Wait for it just for a
+        * second and request follow-up SRST.
+        */
+       if (ap->flags & ATA_FLAG_PMP) {
+               ata_wait_ready(ap, jiffies + HZ);
+               return -EAGAIN;
+       }
 
        rc = ata_wait_ready(ap, deadline);
        /* link occupied, -ENODEV too is an error */
@@ -3657,6 +3927,7 @@ void ata_std_postreset(struct ata_link *link, unsigned int *classes)
        /* clear SError */
        if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
                sata_scr_write(link, SCR_ERROR, serror);
+       link->eh_info.serror = 0;
 
        /* is double-select really necessary? */
        if (classes[0] != ATA_DEV_NONE)
@@ -3860,11 +4131,8 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "SAMSUNG CD-ROM SC",  NULL,           ATA_HORKAGE_NODMA },
        { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
        { "_NEC DV5800A",       NULL,           ATA_HORKAGE_NODMA },
-       { "SAMSUNG CD-ROM SN-124","N001",       ATA_HORKAGE_NODMA },
+       { "SAMSUNG CD-ROM SN-124", "N001",      ATA_HORKAGE_NODMA },
        { "Seagate STT20000A", NULL,            ATA_HORKAGE_NODMA },
-       { "IOMEGA  ZIP 250       ATAPI", NULL,  ATA_HORKAGE_NODMA }, /* temporary fix */
-       { "IOMEGA  ZIP 250       ATAPI       Floppy",
-                               NULL,           ATA_HORKAGE_NODMA },
        /* Odd clown on sil3726/4726 PMPs */
        { "Config  Disk",       NULL,           ATA_HORKAGE_NODMA |
                                                ATA_HORKAGE_SKIP_PM },
@@ -3876,7 +4144,8 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
 
        /* Devices where NCQ should be avoided */
        /* NCQ is slow */
-        { "WDC WD740ADFD-00",   NULL,          ATA_HORKAGE_NONCQ },
+       { "WDC WD740ADFD-00",   NULL,           ATA_HORKAGE_NONCQ },
+       { "WDC WD740ADFD-00NLR1", NULL,         ATA_HORKAGE_NONCQ, },
        /* http://thread.gmane.org/gmane.linux.ide/14907 */
        { "FUJITSU MHT2060BH",  NULL,           ATA_HORKAGE_NONCQ },
        /* NCQ is broken */
@@ -3884,24 +4153,14 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "Maxtor 7V300F0",     "VA111630",     ATA_HORKAGE_NONCQ },
        { "HITACHI HDS7250SASUN500G*", NULL,    ATA_HORKAGE_NONCQ },
        { "HITACHI HDS7225SBSUN250G*", NULL,    ATA_HORKAGE_NONCQ },
+       { "ST380817AS",         "3.42",         ATA_HORKAGE_NONCQ },
+       { "ST3160023AS",        "3.42",         ATA_HORKAGE_NONCQ },
 
        /* Blacklist entries taken from Silicon Image 3124/3132
           Windows driver .inf file - also several Linux problem reports */
        { "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
        { "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
        { "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
-       /* Drives which do spurious command completion */
-       { "HTS541680J9SA00",    "SB2IC7EP",     ATA_HORKAGE_NONCQ, },
-       { "HTS541612J9SA00",    "SBDIC7JP",     ATA_HORKAGE_NONCQ, },
-       { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
-       { "WDC WD740ADFD-00NLR1", NULL,         ATA_HORKAGE_NONCQ, },
-       { "WDC WD3200AAJS-00RYA0", "12.01B01",  ATA_HORKAGE_NONCQ, },
-       { "FUJITSU MHV2080BH",  "00840028",     ATA_HORKAGE_NONCQ, },
-       { "ST9120822AS",        "3.CLF",        ATA_HORKAGE_NONCQ, },
-       { "ST9160821AS",        "3.CLF",        ATA_HORKAGE_NONCQ, },
-       { "ST3160812AS",        "3.ADJ",        ATA_HORKAGE_NONCQ, },
-       { "ST980813AS",         "3.ADB",        ATA_HORKAGE_NONCQ, },
-       { "SAMSUNG HD401LJ",    "ZZ100-15",     ATA_HORKAGE_NONCQ, },
 
        /* devices which puke on READ_NATIVE_MAX */
        { "HDS724040KLSA80",    "KFAOA20N",     ATA_HORKAGE_BROKEN_HPA, },
@@ -3913,11 +4172,18 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "ST340823A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
        { "ST320413A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
 
+       /* Devices which get the IVB wrong */
+       { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
+       { "TSSTcorp CDDVDW SH-S202J", "SB00",     ATA_HORKAGE_IVB, },
+       { "TSSTcorp CDDVDW SH-S202J", "SB01",     ATA_HORKAGE_IVB, },
+       { "TSSTcorp CDDVDW SH-S202N", "SB00",     ATA_HORKAGE_IVB, },
+       { "TSSTcorp CDDVDW SH-S202N", "SB01",     ATA_HORKAGE_IVB, },
+
        /* End Marker */
        { }
 };
 
-int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
+static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
 {
        const char *p;
        int len;
@@ -3928,8 +4194,14 @@ int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
        p = strchr(patt, wildchar);
        if (p && ((*(p + 1)) == 0))
                len = p - patt;
-       else
+       else {
                len = strlen(name);
+               if (!len) {
+                       if (!*patt)
+                               return 0;
+                       return -1;
+               }
+       }
 
        return strncmp(patt, name, len);
 }
@@ -3968,6 +4240,21 @@ static int ata_dma_blacklisted(const struct ata_device *dev)
 }
 
 /**
+ *     ata_is_40wire           -       check drive side detection
+ *     @dev: device
+ *
+ *     Perform drive side detection decoding, allowing for device vendors
+ *     who can't follow the documentation.
+ */
+
+static int ata_is_40wire(struct ata_device *dev)
+{
+       if (dev->horkage & ATA_HORKAGE_IVB)
+               return ata_drive_40wire_relaxed(dev->id);
+       return ata_drive_40wire(dev->id);
+}
+
+/**
  *     ata_dev_xfermask - Compute supported xfermask of the given device
  *     @dev: Device to compute xfermask for
  *
@@ -4013,7 +4300,7 @@ static void ata_dev_xfermask(struct ata_device *dev)
        }
 
        if ((host->flags & ATA_HOST_SIMPLEX) &&
-            host->simplex_claimed && host->simplex_claimed != ap) {
+           host->simplex_claimed && host->simplex_claimed != ap) {
                xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
                ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
                               "other device, disabling DMA\n");
@@ -4035,11 +4322,11 @@ static void ata_dev_xfermask(struct ata_device *dev)
         */
        if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
                /* UDMA/44 or higher would be available */
-               if((ap->cbl == ATA_CBL_PATA40) ||
-                   (ata_drive_40wire(dev->id) &&
-                    (ap->cbl == ATA_CBL_PATA_UNK ||
-                     ap->cbl == ATA_CBL_PATA80))) {
-                       ata_dev_printk(dev, KERN_WARNING,
+               if ((ap->cbl == ATA_CBL_PATA40) ||
+                   (ata_is_40wire(dev) &&
+                   (ap->cbl == ATA_CBL_PATA_UNK ||
+                    ap->cbl == ATA_CBL_PATA80))) {
+                       ata_dev_printk(dev, KERN_WARNING,
                                 "limited to UDMA/33 due to 40-wire cable\n");
                        xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
                }
@@ -4078,22 +4365,28 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        tf.feature = SETFEATURES_XFER;
        tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
        tf.protocol = ATA_PROT_NODATA;
-       tf.nsect = dev->xfer_mode;
+       /* If we are using IORDY we must send the mode setting command */
+       if (ata_pio_need_iordy(dev))
+               tf.nsect = dev->xfer_mode;
+       /* If the device has IORDY and the controller does not - turn it off */
+       else if (ata_id_has_iordy(dev->id))
+               tf.nsect = 0x01;
+       else /* In the ancient relic department - skip all of this */
+               return 0;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 
        DPRINTK("EXIT, err_mask=%x\n", err_mask);
        return err_mask;
 }
-
 /**
- *     ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
+ *     ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
  *     @dev: Device to which command will be sent
  *     @enable: Whether to enable or disable the feature
+ *     @feature: The sector count represents the feature to set
  *
  *     Issue SET FEATURES - SATA FEATURES command to device @dev
- *     on port @ap with sector count set to indicate Asynchronous
- *     Notification feature
+ *     on port @ap with sector count
  *
  *     LOCKING:
  *     PCI/etc. bus probe sem.
@@ -4101,7 +4394,8 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  *     RETURNS:
  *     0 on success, AC_ERR_* mask otherwise.
  */
-static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
+static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
+                                       u8 feature)
 {
        struct ata_taskfile tf;
        unsigned int err_mask;
@@ -4114,9 +4408,9 @@ static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
        tf.feature = enable;
        tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
        tf.protocol = ATA_PROT_NODATA;
-       tf.nsect = SATA_AN;
+       tf.nsect = feature;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 
        DPRINTK("EXIT, err_mask=%x\n", err_mask);
        return err_mask;
@@ -4154,7 +4448,7 @@ static unsigned int ata_dev_init_params(struct ata_device *dev,
        tf.nsect = sectors;
        tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        /* A clean abort indicates an original or just out of spec drive
           and we should continue as we issue the setup based on the
           drive reported working geometry */
@@ -4177,17 +4471,13 @@ static unsigned int ata_dev_init_params(struct ata_device *dev,
 void ata_sg_clean(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       struct scatterlist *sg = qc->__sg;
+       struct scatterlist *sg = qc->sg;
        int dir = qc->dma_dir;
        void *pad_buf = NULL;
 
-       WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
        WARN_ON(sg == NULL);
 
-       if (qc->flags & ATA_QCFLAG_SINGLE)
-               WARN_ON(qc->n_elem > 1);
-
-       VPRINTK("unmapping %u sg elements\n", qc->n_elem);
+       VPRINTK("unmapping %u sg elements\n", qc->mapped_n_elem);
 
        /* if we padded the buffer out to 32-bit bound, and data
         * xfer direction is from-device, we must copy from the
@@ -4196,31 +4486,20 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
        if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
                pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
 
-       if (qc->flags & ATA_QCFLAG_SG) {
-               if (qc->n_elem)
-                       dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
-               /* restore last sg */
-               sg[qc->orig_n_elem - 1].length += qc->pad_len;
-               if (pad_buf) {
-                       struct scatterlist *psg = &qc->pad_sgent;
-                       void *addr = kmap_atomic(psg->page, KM_IRQ0);
-                       memcpy(addr + psg->offset, pad_buf, qc->pad_len);
-                       kunmap_atomic(addr, KM_IRQ0);
-               }
-       } else {
-               if (qc->n_elem)
-                       dma_unmap_single(ap->dev,
-                               sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
-                               dir);
-               /* restore sg */
-               sg->length += qc->pad_len;
-               if (pad_buf)
-                       memcpy(qc->buf_virt + sg->length - qc->pad_len,
-                              pad_buf, qc->pad_len);
+       if (qc->mapped_n_elem)
+               dma_unmap_sg(ap->dev, sg, qc->mapped_n_elem, dir);
+       /* restore last sg */
+       if (qc->last_sg)
+               *qc->last_sg = qc->saved_last_sg;
+       if (pad_buf) {
+               struct scatterlist *psg = &qc->extra_sg[1];
+               void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
+               memcpy(addr + psg->offset, pad_buf, qc->pad_len);
+               kunmap_atomic(addr, KM_IRQ0);
        }
 
        qc->flags &= ~ATA_QCFLAG_DMAMAP;
-       qc->__sg = NULL;
+       qc->sg = NULL;
 }
 
 /**
@@ -4238,13 +4517,10 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct scatterlist *sg;
-       unsigned int idx;
+       unsigned int si, pi;
 
-       WARN_ON(qc->__sg == NULL);
-       WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
-
-       idx = 0;
-       ata_for_each_sg(sg, qc) {
+       pi = 0;
+       for_each_sg(qc->sg, sg, qc->n_elem, si) {
                u32 addr, offset;
                u32 sg_len, len;
 
@@ -4261,18 +4537,17 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
                        if ((offset + sg_len) > 0x10000)
                                len = 0x10000 - offset;
 
-                       ap->prd[idx].addr = cpu_to_le32(addr);
-                       ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
-                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
+                       ap->prd[pi].addr = cpu_to_le32(addr);
+                       ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
+                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
 
-                       idx++;
+                       pi++;
                        sg_len -= len;
                        addr += len;
                }
        }
 
-       if (idx)
-               ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
+       ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
 }
 
 /**
@@ -4292,17 +4567,14 @@ static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct scatterlist *sg;
-       unsigned int idx;
-
-       WARN_ON(qc->__sg == NULL);
-       WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
+       unsigned int si, pi;
 
-       idx = 0;
-       ata_for_each_sg(sg, qc) {
+       pi = 0;
+       for_each_sg(qc->sg, sg, qc->n_elem, si) {
                u32 addr, offset;
                u32 sg_len, len, blen;
 
-               /* determine if physical DMA addr spans 64K boundary.
+               /* determine if physical DMA addr spans 64K boundary.
                 * Note h/w doesn't support 64-bit, so we unconditionally
                 * truncate dma_addr_t to u32.
                 */
@@ -4316,25 +4588,24 @@ static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
                                len = 0x10000 - offset;
 
                        blen = len & 0xffff;
-                       ap->prd[idx].addr = cpu_to_le32(addr);
+                       ap->prd[pi].addr = cpu_to_le32(addr);
                        if (blen == 0) {
                           /* Some PATA chipsets like the CS5530 can't
                              cope with 0x0000 meaning 64K as the spec says */
-                               ap->prd[idx].flags_len = cpu_to_le32(0x8000);
+                               ap->prd[pi].flags_len = cpu_to_le32(0x8000);
                                blen = 0x8000;
-                               ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
+                               ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
                        }
-                       ap->prd[idx].flags_len = cpu_to_le32(blen);
-                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
+                       ap->prd[pi].flags_len = cpu_to_le32(blen);
+                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
 
-                       idx++;
+                       pi++;
                        sg_len -= len;
                        addr += len;
                }
        }
 
-       if (idx)
-               ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
+       ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
 }
 
 /**
@@ -4368,6 +4639,43 @@ int ata_check_atapi_dma(struct ata_queued_cmd *qc)
 }
 
 /**
+ *     atapi_qc_may_overflow - Check whether data transfer may overflow
+ *     @qc: ATA command in question
+ *
+ *     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 @qc.
+ *
+ *     LOCKING:
+ *     None.
+ *
+ *     RETURNS:
+ *     1 if @qc may overflow; otherwise, 0.
+ */
+static int atapi_qc_may_overflow(struct ata_queued_cmd *qc)
+{
+       if (qc->tf.protocol != ATAPI_PROT_PIO &&
+           qc->tf.protocol != ATAPI_PROT_DMA)
+               return 0;
+
+       if (qc->tf.flags & ATA_TFLAG_WRITE)
+               return 0;
+
+       switch (qc->cdb[0]) {
+       case READ_10:
+       case READ_12:
+       case WRITE_10:
+       case WRITE_12:
+       case GPCMD_READ_CD:
+       case GPCMD_READ_CD_MSF:
+               return 0;
+       }
+
+       return 1;
+}
+
+/**
  *     ata_std_qc_defer - Check whether a qc needs to be deferred
  *     @qc: ATA command in question
  *
@@ -4434,32 +4742,6 @@ void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
 
 /**
- *     ata_sg_init_one - Associate command with memory buffer
- *     @qc: Command to be associated
- *     @buf: Memory buffer
- *     @buflen: Length of memory buffer, in bytes.
- *
- *     Initialize the data-related elements of queued_cmd @qc
- *     to point to a single memory buffer, @buf of byte length @buflen.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-
-void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
-{
-       qc->flags |= ATA_QCFLAG_SINGLE;
-
-       qc->__sg = &qc->sgent;
-       qc->n_elem = 1;
-       qc->orig_n_elem = 1;
-       qc->buf_virt = buf;
-       qc->nbytes = buflen;
-
-       sg_init_one(&qc->sgent, buf, buflen);
-}
-
-/**
  *     ata_sg_init - Associate command with scatter-gather table.
  *     @qc: Command to be associated
  *     @sg: Scatter-gather table.
@@ -4472,83 +4754,100 @@ void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
                 unsigned int n_elem)
 {
-       qc->flags |= ATA_QCFLAG_SG;
-       qc->__sg = sg;
+       qc->sg = sg;
        qc->n_elem = n_elem;
-       qc->orig_n_elem = n_elem;
+       qc->cursg = qc->sg;
 }
 
-/**
- *     ata_sg_setup_one - DMA-map the memory buffer associated with a command.
- *     @qc: Command with memory buffer to be mapped.
- *
- *     DMA-map the memory buffer associated with queued_cmd @qc.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- *
- *     RETURNS:
- *     Zero on success, negative on error.
- */
-
-static int ata_sg_setup_one(struct ata_queued_cmd *qc)
+static unsigned int ata_sg_setup_extra(struct ata_queued_cmd *qc,
+                                      unsigned int *n_elem_extra)
 {
        struct ata_port *ap = qc->ap;
-       int dir = qc->dma_dir;
-       struct scatterlist *sg = qc->__sg;
-       dma_addr_t dma_address;
-       int trim_sg = 0;
+       unsigned int n_elem = qc->n_elem;
+       struct scatterlist *lsg, *copy_lsg = NULL, *tsg = NULL, *esg = NULL;
+
+       *n_elem_extra = 0;
+
+       /* needs padding? */
+       qc->pad_len = qc->nbytes & 3;
+
+       if (likely(!qc->pad_len))
+               return n_elem;
+
+       /* locate last sg and save it */
+       lsg = sg_last(qc->sg, n_elem);
+       qc->last_sg = lsg;
+       qc->saved_last_sg = *lsg;
+
+       sg_init_table(qc->extra_sg, ARRAY_SIZE(qc->extra_sg));
 
-       /* we must lengthen transfers to end on a 32-bit boundary */
-       qc->pad_len = sg->length & 3;
        if (qc->pad_len) {
+               struct scatterlist *psg = &qc->extra_sg[1];
                void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
-               struct scatterlist *psg = &qc->pad_sgent;
+               unsigned int offset;
 
                WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
 
                memset(pad_buf, 0, ATA_DMA_PAD_SZ);
 
-               if (qc->tf.flags & ATA_TFLAG_WRITE)
-                       memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
-                              qc->pad_len);
+               /* psg->page/offset are used to copy to-be-written
+                * data in this function or read data in ata_sg_clean.
+                */
+               offset = lsg->offset + lsg->length - qc->pad_len;
+               sg_set_page(psg, nth_page(sg_page(lsg), offset >> PAGE_SHIFT),
+                           qc->pad_len, offset_in_page(offset));
+
+               if (qc->tf.flags & ATA_TFLAG_WRITE) {
+                       void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
+                       memcpy(pad_buf, addr + psg->offset, qc->pad_len);
+                       kunmap_atomic(addr, KM_IRQ0);
+               }
 
                sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
                sg_dma_len(psg) = ATA_DMA_PAD_SZ;
-               /* trim sg */
-               sg->length -= qc->pad_len;
-               if (sg->length == 0)
-                       trim_sg = 1;
 
-               DPRINTK("padding done, sg->length=%u pad_len=%u\n",
-                       sg->length, qc->pad_len);
-       }
+               /* Trim the last sg entry and chain the original and
+                * padding sg lists.
+                *
+                * Because chaining consumes one sg entry, one extra
+                * sg entry is allocated and the last sg entry is
+                * copied to it if the length isn't zero after padded
+                * amount is removed.
+                *
+                * If the last sg entry is completely replaced by
+                * padding sg entry, the first sg entry is skipped
+                * while chaining.
+                */
+               lsg->length -= qc->pad_len;
+               if (lsg->length) {
+                       copy_lsg = &qc->extra_sg[0];
+                       tsg = &qc->extra_sg[0];
+               } else {
+                       n_elem--;
+                       tsg = &qc->extra_sg[1];
+               }
 
-       if (trim_sg) {
-               qc->n_elem--;
-               goto skip_map;
-       }
+               esg = &qc->extra_sg[1];
 
-       dma_address = dma_map_single(ap->dev, qc->buf_virt,
-                                    sg->length, dir);
-       if (dma_mapping_error(dma_address)) {
-               /* restore sg */
-               sg->length += qc->pad_len;
-               return -1;
+               (*n_elem_extra)++;
        }
 
-       sg_dma_address(sg) = dma_address;
-       sg_dma_len(sg) = sg->length;
+       if (copy_lsg)
+               sg_set_page(copy_lsg, sg_page(lsg), lsg->length, lsg->offset);
 
-skip_map:
-       DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
-               qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
+       sg_chain(lsg, 1, tsg);
+       sg_mark_end(esg);
 
-       return 0;
+       /* sglist can't start with chaining sg entry, fast forward */
+       if (qc->sg == lsg) {
+               qc->sg = tsg;
+               qc->cursg = tsg;
+       }
+
+       return n_elem;
 }
 
 /**
@@ -4564,74 +4863,30 @@ skip_map:
  *     Zero on success, negative on error.
  *
  */
-
 static int ata_sg_setup(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       struct scatterlist *sg = qc->__sg;
-       struct scatterlist *lsg = &sg[qc->n_elem - 1];
-       int n_elem, pre_n_elem, dir, trim_sg = 0;
+       unsigned int n_elem, n_elem_extra;
 
        VPRINTK("ENTER, ata%u\n", ap->print_id);
-       WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
-
-       /* we must lengthen transfers to end on a 32-bit boundary */
-       qc->pad_len = lsg->length & 3;
-       if (qc->pad_len) {
-               void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
-               struct scatterlist *psg = &qc->pad_sgent;
-               unsigned int offset;
-
-               WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
-
-               memset(pad_buf, 0, ATA_DMA_PAD_SZ);
 
-               /*
-                * psg->page/offset are used to copy to-be-written
-                * data in this function or read data in ata_sg_clean.
-                */
-               offset = lsg->offset + lsg->length - qc->pad_len;
-               psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
-               psg->offset = offset_in_page(offset);
+       n_elem = ata_sg_setup_extra(qc, &n_elem_extra);
 
-               if (qc->tf.flags & ATA_TFLAG_WRITE) {
-                       void *addr = kmap_atomic(psg->page, KM_IRQ0);
-                       memcpy(pad_buf, addr + psg->offset, qc->pad_len);
-                       kunmap_atomic(addr, KM_IRQ0);
+       if (n_elem) {
+               n_elem = dma_map_sg(ap->dev, qc->sg, n_elem, qc->dma_dir);
+               if (n_elem < 1) {
+                       /* restore last sg */
+                       if (qc->last_sg)
+                               *qc->last_sg = qc->saved_last_sg;
+                       return -1;
                }
-
-               sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
-               sg_dma_len(psg) = ATA_DMA_PAD_SZ;
-               /* trim last sg */
-               lsg->length -= qc->pad_len;
-               if (lsg->length == 0)
-                       trim_sg = 1;
-
-               DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
-                       qc->n_elem - 1, lsg->length, qc->pad_len);
-       }
-
-       pre_n_elem = qc->n_elem;
-       if (trim_sg && pre_n_elem)
-               pre_n_elem--;
-
-       if (!pre_n_elem) {
-               n_elem = 0;
-               goto skip_map;
+               DPRINTK("%d sg elements mapped\n", n_elem);
        }
 
-       dir = qc->dma_dir;
-       n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
-       if (n_elem < 1) {
-               /* restore last sg */
-               lsg->length += qc->pad_len;
-               return -1;
-       }
-
-       DPRINTK("%d sg elements mapped\n", n_elem);
+       qc->n_elem = qc->mapped_n_elem = n_elem;
+       qc->n_elem += n_elem_extra;
 
-skip_map:
-       qc->n_elem = n_elem;
+       qc->flags |= ATA_QCFLAG_DMAMAP;
 
        return 0;
 }
@@ -4660,7 +4915,7 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
 
 /**
  *     ata_data_xfer - Transfer data by PIO
- *     @adev: device to target
+ *     @dev: device to target
  *     @buf: data buffer
  *     @buflen: buffer length
  *     @write_data: read/write
@@ -4669,37 +4924,44 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
  *
  *     LOCKING:
  *     Inherited from caller.
+ *
+ *     RETURNS:
+ *     Bytes consumed.
  */
-void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
-                  unsigned int buflen, int write_data)
+unsigned int ata_data_xfer(struct ata_device *dev, unsigned char *buf,
+                          unsigned int buflen, int rw)
 {
-       struct ata_port *ap = adev->link->ap;
+       struct ata_port *ap = dev->link->ap;
+       void __iomem *data_addr = ap->ioaddr.data_addr;
        unsigned int words = buflen >> 1;
 
        /* Transfer multiple of 2 bytes */
-       if (write_data)
-               iowrite16_rep(ap->ioaddr.data_addr, buf, words);
+       if (rw == READ)
+               ioread16_rep(data_addr, buf, words);
        else
-               ioread16_rep(ap->ioaddr.data_addr, buf, words);
+               iowrite16_rep(data_addr, buf, words);
 
        /* Transfer trailing 1 byte, if any. */
        if (unlikely(buflen & 0x01)) {
                u16 align_buf[1] = { 0 };
                unsigned char *trailing_buf = buf + buflen - 1;
 
-               if (write_data) {
-                       memcpy(align_buf, trailing_buf, 1);
-                       iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
-               } else {
-                       align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
+               if (rw == READ) {
+                       align_buf[0] = cpu_to_le16(ioread16(data_addr));
                        memcpy(trailing_buf, align_buf, 1);
+               } else {
+                       memcpy(align_buf, trailing_buf, 1);
+                       iowrite16(le16_to_cpu(align_buf[0]), data_addr);
                }
+               words++;
        }
+
+       return words << 1;
 }
 
 /**
  *     ata_data_xfer_noirq - Transfer data by PIO
- *     @adev: device to target
+ *     @dev: device to target
  *     @buf: data buffer
  *     @buflen: buffer length
  *     @write_data: read/write
@@ -4709,14 +4971,21 @@ void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
  *
  *     LOCKING:
  *     Inherited from caller.
+ *
+ *     RETURNS:
+ *     Bytes consumed.
  */
-void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
-                        unsigned int buflen, int write_data)
+unsigned int ata_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
+                                unsigned int buflen, int rw)
 {
        unsigned long flags;
+       unsigned int consumed;
+
        local_irq_save(flags);
-       ata_data_xfer(adev, buf, buflen, write_data);
+       consumed = ata_data_xfer(dev, buf, buflen, rw);
        local_irq_restore(flags);
+
+       return consumed;
 }
 
 
@@ -4733,7 +5002,6 @@ void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
 static void ata_pio_sector(struct ata_queued_cmd *qc)
 {
        int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
-       struct scatterlist *sg = qc->__sg;
        struct ata_port *ap = qc->ap;
        struct page *page;
        unsigned int offset;
@@ -4742,8 +5010,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
        if (qc->curbytes == qc->nbytes - qc->sect_size)
                ap->hsm_task_state = HSM_ST_LAST;
 
-       page = sg[qc->cursg].page;
-       offset = sg[qc->cursg].offset + qc->cursg_ofs;
+       page = sg_page(qc->cursg);
+       offset = qc->cursg->offset + qc->cursg_ofs;
 
        /* get the current page and offset */
        page = nth_page(page, (offset >> PAGE_SHIFT));
@@ -4771,8 +5039,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
        qc->curbytes += qc->sect_size;
        qc->cursg_ofs += qc->sect_size;
 
-       if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
-               qc->cursg++;
+       if (qc->cursg_ofs == qc->cursg->length) {
+               qc->cursg = sg_next(qc->cursg);
                qc->cursg_ofs = 0;
        }
 }
@@ -4828,13 +5096,13 @@ static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
        ata_altstatus(ap); /* flush */
 
        switch (qc->tf.protocol) {
-       case ATA_PROT_ATAPI:
+       case ATAPI_PROT_PIO:
                ap->hsm_task_state = HSM_ST;
                break;
-       case ATA_PROT_ATAPI_NODATA:
+       case ATAPI_PROT_NODATA:
                ap->hsm_task_state = HSM_ST_LAST;
                break;
-       case ATA_PROT_ATAPI_DMA:
+       case ATAPI_PROT_DMA:
                ap->hsm_task_state = HSM_ST_LAST;
                /* initiate bmdma */
                ap->ops->bmdma_start(qc);
@@ -4853,21 +5121,19 @@ static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  *     Inherited from caller.
  *
  */
-
-static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
+static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
 {
        int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
-       struct scatterlist *sg = qc->__sg;
        struct ata_port *ap = qc->ap;
+       struct ata_eh_info *ehi = &qc->dev->link->eh_info;
+       struct scatterlist *sg;
        struct page *page;
        unsigned char *buf;
        unsigned int offset, count;
 
-       if (qc->curbytes + bytes >= qc->nbytes)
-               ap->hsm_task_state = HSM_ST_LAST;
-
 next_sg:
-       if (unlikely(qc->cursg >= qc->n_elem)) {
+       sg = qc->cursg;
+       if (unlikely(!sg)) {
                /*
                 * The end of qc->sg is reached and the device expects
                 * more data to transfer. In order not to overrun qc->sg
@@ -4876,23 +5142,30 @@ next_sg:
                 *    - for write case, padding zero data to the device
                 */
                u16 pad_buf[1] = { 0 };
-               unsigned int words = bytes >> 1;
                unsigned int i;
 
-               if (words) /* warning if bytes > 1 */
-                       ata_dev_printk(qc->dev, KERN_WARNING,
-                                      "%u bytes trailing data\n", bytes);
+               if (bytes > qc->curbytes - qc->nbytes + ATAPI_MAX_DRAIN) {
+                       ata_ehi_push_desc(ehi, "too much trailing data "
+                                         "buf=%u cur=%u bytes=%u",
+                                         qc->nbytes, qc->curbytes, bytes);
+                       return -1;
+               }
+
+                /* overflow is exptected for misc ATAPI commands */
+               if (bytes && !atapi_qc_may_overflow(qc))
+                       ata_dev_printk(qc->dev, KERN_WARNING, "ATAPI %u bytes "
+                                      "trailing data (cdb=%02x nbytes=%u)\n",
+                                      bytes, qc->cdb[0], qc->nbytes);
 
-               for (i = 0; i < words; i++)
-                       ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
+               for (i = 0; i < (bytes + 1) / 2; i++)
+                       ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write);
 
-               ap->hsm_task_state = HSM_ST_LAST;
-               return;
-       }
+               qc->curbytes += bytes;
 
-       sg = &qc->__sg[qc->cursg];
+               return 0;
+       }
 
-       page = sg->page;
+       page = sg_page(sg);
        offset = sg->offset + qc->cursg_ofs;
 
        /* get the current page and offset */
@@ -4925,16 +5198,20 @@ next_sg:
        }
 
        bytes -= count;
+       if ((count & 1) && bytes)
+               bytes--;
        qc->curbytes += count;
        qc->cursg_ofs += count;
 
        if (qc->cursg_ofs == sg->length) {
-               qc->cursg++;
+               qc->cursg = sg_next(qc->cursg);
                qc->cursg_ofs = 0;
        }
 
        if (bytes)
                goto next_sg;
+
+       return 0;
 }
 
 /**
@@ -4967,17 +5244,21 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
        bytes = (bc_hi << 8) | bc_lo;
 
        /* shall be cleared to zero, indicating xfer of data */
-       if (ireason & (1 << 0))
+       if (unlikely(ireason & (1 << 0)))
                goto err_out;
 
        /* make sure transfer direction matches expected */
        i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
-       if (do_write != i_write)
+       if (unlikely(do_write != i_write))
+               goto err_out;
+
+       if (unlikely(!bytes))
                goto err_out;
 
        VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
 
-       __atapi_pio_bytes(qc, bytes);
+       if (__atapi_pio_bytes(qc, bytes))
+               goto err_out;
        ata_altstatus(ap); /* flush */
 
        return;
@@ -5007,7 +5288,7 @@ static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *q
                    (qc->tf.flags & ATA_TFLAG_WRITE))
                    return 1;
 
-               if (is_atapi_taskfile(&qc->tf) &&
+               if (ata_is_atapi(qc->tf.protocol) &&
                    !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
                        return 1;
        }
@@ -5124,11 +5405,19 @@ fsm_start:
                 * let the EH abort the command or reset the device.
                 */
                if (unlikely(status & (ATA_ERR | ATA_DF))) {
-                       ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
-                                       "error, dev_stat 0x%X\n", status);
-                       qc->err_mask |= AC_ERR_HSM;
-                       ap->hsm_task_state = HSM_ST_ERR;
-                       goto fsm_start;
+                       /* Some ATAPI tape drives forget to clear the ERR bit
+                        * when doing the next command (mostly request sense).
+                        * We ignore ERR here to workaround and proceed sending
+                        * the CDB.
+                        */
+                       if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
+                               ata_port_printk(ap, KERN_WARNING,
+                                               "DRQ=1 with device error, "
+                                               "dev_stat 0x%X\n", status);
+                               qc->err_mask |= AC_ERR_HSM;
+                               ap->hsm_task_state = HSM_ST_ERR;
+                               goto fsm_start;
+                       }
                }
 
                /* Send the CDB (atapi) or the first data block (ata pio out).
@@ -5164,7 +5453,7 @@ fsm_start:
 
        case HSM_ST:
                /* complete command or read/write the data register */
-               if (qc->tf.protocol == ATA_PROT_ATAPI) {
+               if (qc->tf.protocol == ATAPI_PROT_PIO) {
                        /* ATAPI PIO protocol */
                        if ((status & ATA_DRQ) == 0) {
                                /* No more data to transfer or device error.
@@ -5463,6 +5752,22 @@ static void fill_result_tf(struct ata_queued_cmd *qc)
        ap->ops->tf_read(ap, &qc->result_tf);
 }
 
+static void ata_verify_xfer(struct ata_queued_cmd *qc)
+{
+       struct ata_device *dev = qc->dev;
+
+       if (ata_tag_internal(qc->tag))
+               return;
+
+       if (ata_is_nodata(qc->tf.protocol))
+               return;
+
+       if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
+               return;
+
+       dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
+}
+
 /**
  *     ata_qc_complete - Complete an active ATA command
  *     @qc: Command to complete
@@ -5492,6 +5797,9 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
         * taken care of.
         */
        if (ap->ops->error_handler) {
+               struct ata_device *dev = qc->dev;
+               struct ata_eh_info *ehi = &dev->link->eh_info;
+
                WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
 
                if (unlikely(qc->err_mask))
@@ -5510,6 +5818,30 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
                if (qc->flags & ATA_QCFLAG_RESULT_TF)
                        fill_result_tf(qc);
 
+               /* Some commands need post-processing after successful
+                * completion.
+                */
+               switch (qc->tf.command) {
+               case ATA_CMD_SET_FEATURES:
+                       if (qc->tf.feature != SETFEATURES_WC_ON &&
+                           qc->tf.feature != SETFEATURES_WC_OFF)
+                               break;
+                       /* fall through */
+               case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
+               case ATA_CMD_SET_MULTI: /* multi_count changed */
+                       /* revalidate device */
+                       ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
+                       ata_port_schedule_eh(ap);
+                       break;
+
+               case ATA_CMD_SLEEP:
+                       dev->flags |= ATA_DFLAG_SLEEPING;
+                       break;
+               }
+
+               if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
+                       ata_verify_xfer(qc);
+
                __ata_qc_complete(qc);
        } else {
                if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
@@ -5572,30 +5904,6 @@ int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
        return nr_done;
 }
 
-static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-
-       switch (qc->tf.protocol) {
-       case ATA_PROT_NCQ:
-       case ATA_PROT_DMA:
-       case ATA_PROT_ATAPI_DMA:
-               return 1;
-
-       case ATA_PROT_ATAPI:
-       case ATA_PROT_PIO:
-               if (ap->flags & ATA_FLAG_PIO_DMA)
-                       return 1;
-
-               /* fall through */
-
-       default:
-               return 0;
-       }
-
-       /* never reached */
-}
-
 /**
  *     ata_qc_issue - issue taskfile to device
  *     @qc: command to issue to device
@@ -5612,6 +5920,7 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct ata_link *link = qc->dev->link;
+       u8 prot = qc->tf.protocol;
 
        /* Make sure only one non-NCQ command is outstanding.  The
         * check is skipped for old EH because it reuses active qc to
@@ -5619,7 +5928,7 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
         */
        WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
 
-       if (qc->tf.protocol == ATA_PROT_NCQ) {
+       if (ata_is_ncq(prot)) {
                WARN_ON(link->sactive & (1 << qc->tag));
 
                if (!link->sactive)
@@ -5635,16 +5944,22 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
        qc->flags |= ATA_QCFLAG_ACTIVE;
        ap->qc_active |= 1 << qc->tag;
 
-       if (ata_should_dma_map(qc)) {
-               if (qc->flags & ATA_QCFLAG_SG) {
-                       if (ata_sg_setup(qc))
-                               goto sg_err;
-               } else if (qc->flags & ATA_QCFLAG_SINGLE) {
-                       if (ata_sg_setup_one(qc))
-                               goto sg_err;
-               }
-       } else {
-               qc->flags &= ~ATA_QCFLAG_DMAMAP;
+       /* We guarantee to LLDs that they will have at least one
+        * non-zero sg if the command is a data command.
+        */
+       BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
+
+       if (ata_is_dma(prot) || (ata_is_pio(prot) &&
+                                (ap->flags & ATA_FLAG_PIO_DMA)))
+               if (ata_sg_setup(qc))
+                       goto sg_err;
+
+       /* if device is sleeping, schedule softreset and abort the link */
+       if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
+               link->eh_info.action |= ATA_EH_SOFTRESET;
+               ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
+               ata_link_abort(link);
+               return;
        }
 
        ap->ops->qc_prep(qc);
@@ -5655,7 +5970,6 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
        return;
 
 sg_err:
-       qc->flags &= ~ATA_QCFLAG_DMAMAP;
        qc->err_mask |= AC_ERR_SYSTEM;
 err:
        ata_qc_complete(qc);
@@ -5690,11 +6004,11 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
                switch (qc->tf.protocol) {
                case ATA_PROT_PIO:
                case ATA_PROT_NODATA:
-               case ATA_PROT_ATAPI:
-               case ATA_PROT_ATAPI_NODATA:
+               case ATAPI_PROT_PIO:
+               case ATAPI_PROT_NODATA:
                        qc->tf.flags |= ATA_TFLAG_POLLING;
                        break;
-               case ATA_PROT_ATAPI_DMA:
+               case ATAPI_PROT_DMA:
                        if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
                                /* see ata_dma_blacklisted() */
                                BUG();
@@ -5758,8 +6072,8 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
 
                break;
 
-       case ATA_PROT_ATAPI:
-       case ATA_PROT_ATAPI_NODATA:
+       case ATAPI_PROT_PIO:
+       case ATAPI_PROT_NODATA:
                if (qc->tf.flags & ATA_TFLAG_POLLING)
                        ata_qc_set_polling(qc);
 
@@ -5773,7 +6087,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
                        ata_port_queue_task(ap, ata_pio_task, qc, 0);
                break;
 
-       case ATA_PROT_ATAPI_DMA:
+       case ATAPI_PROT_DMA:
                WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
 
                ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
@@ -5809,8 +6123,8 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  *     One if interrupt was handled, zero if not (shared irq).
  */
 
-inline unsigned int ata_host_intr (struct ata_port *ap,
-                                  struct ata_queued_cmd *qc)
+inline unsigned int ata_host_intr(struct ata_port *ap,
+                                 struct ata_queued_cmd *qc)
 {
        struct ata_eh_info *ehi = &ap->link.eh_info;
        u8 status, host_stat = 0;
@@ -5826,15 +6140,15 @@ inline unsigned int ata_host_intr (struct ata_port *ap,
                 */
 
                /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
-                * The flag was turned on only for atapi devices.
-                * No need to check is_atapi_taskfile(&qc->tf) again.
+                * The flag was turned on only for atapi devices.  No
+                * need to check ata_is_atapi(qc->tf.protocol) again.
                 */
                if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
                        goto idle_irq;
                break;
        case HSM_ST_LAST:
                if (qc->tf.protocol == ATA_PROT_DMA ||
-                   qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
+                   qc->tf.protocol == ATAPI_PROT_DMA) {
                        /* check status of DMA engine */
                        host_stat = ap->ops->bmdma_status(ap);
                        VPRINTK("ata%u: host_stat 0x%X\n",
@@ -5876,7 +6190,7 @@ inline unsigned int ata_host_intr (struct ata_port *ap,
        ata_hsm_move(ap, qc, status, 0);
 
        if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
-                                      qc->tf.protocol == ATA_PROT_ATAPI_DMA))
+                                      qc->tf.protocol == ATAPI_PROT_DMA))
                ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
 
        return 1;       /* irq handled */
@@ -5910,7 +6224,7 @@ idle_irq:
  *     IRQ_NONE or IRQ_HANDLED.
  */
 
-irqreturn_t ata_interrupt (int irq, void *dev_instance)
+irqreturn_t ata_interrupt(int irq, void *dev_instance)
 {
        struct ata_host *host = dev_instance;
        unsigned int i;
@@ -5966,22 +6280,26 @@ int sata_scr_valid(struct ata_link *link)
  *     @val: Place to store read value
  *
  *     Read SCR register @reg of @link into *@val.  This function is
- *     guaranteed to succeed if the cable type of the port is SATA
- *     and the port implements ->scr_read.
+ *     guaranteed to succeed if @link is ap->link, the cable type of
+ *     the port is SATA and the port implements ->scr_read.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
 {
-       struct ata_port *ap = link->ap;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
 
-       if (sata_scr_valid(link))
-               return ap->ops->scr_read(ap, reg, val);
-       return -EOPNOTSUPP;
+               if (sata_scr_valid(link))
+                       return ap->ops->scr_read(ap, reg, val);
+               return -EOPNOTSUPP;
+       }
+
+       return sata_pmp_scr_read(link, reg, val);
 }
 
 /**
@@ -5991,22 +6309,26 @@ int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  *     @val: value to write
  *
  *     Write @val to SCR register @reg of @link.  This function is
- *     guaranteed to succeed if the cable type of the port is SATA
- *     and the port implements ->scr_read.
+ *     guaranteed to succeed if @link is ap->link, the cable type of
+ *     the port is SATA and the port implements ->scr_read.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_write(struct ata_link *link, int reg, u32 val)
 {
-       struct ata_port *ap = link->ap;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
+
+               if (sata_scr_valid(link))
+                       return ap->ops->scr_write(ap, reg, val);
+               return -EOPNOTSUPP;
+       }
 
-       if (sata_scr_valid(link))
-               return ap->ops->scr_write(ap, reg, val);
-       return -EOPNOTSUPP;
+       return sata_pmp_scr_write(link, reg, val);
 }
 
 /**
@@ -6019,23 +6341,27 @@ int sata_scr_write(struct ata_link *link, int reg, u32 val)
  *     function performs flush after writing to the register.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
 {
-       struct ata_port *ap = link->ap;
-       int rc;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
+               int rc;
 
-       if (sata_scr_valid(link)) {
-               rc = ap->ops->scr_write(ap, reg, val);
-               if (rc == 0)
-                       rc = ap->ops->scr_read(ap, reg, &val);
-               return rc;
+               if (sata_scr_valid(link)) {
+                       rc = ap->ops->scr_write(ap, reg, val);
+                       if (rc == 0)
+                               rc = ap->ops->scr_read(ap, reg, &val);
+                       return rc;
+               }
+               return -EOPNOTSUPP;
        }
-       return -EOPNOTSUPP;
+
+       return sata_pmp_scr_write(link, reg, val);
 }
 
 /**
@@ -6101,7 +6427,7 @@ int ata_flush_cache(struct ata_device *dev)
 
        /* This is wrong. On a failed flush we get back the LBA of the lost
           sector and we should (assuming it wasn't aborted as unknown) issue
-          a further flush command to continue the writeback until it 
+          a further flush command to continue the writeback until it
           does not error */
        err_mask = ata_do_simple_cmd(dev, cmd);
        if (err_mask) {
@@ -6182,6 +6508,12 @@ int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
 {
        int rc;
 
+       /*
+        * disable link pm on all ports before requesting
+        * any pm activity
+        */
+       ata_lpm_enable(host);
+
        rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
        if (rc == 0)
                host->dev->power.power_state = mesg;
@@ -6204,6 +6536,9 @@ void ata_host_resume(struct ata_host *host)
        ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
                            ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
        host->dev->power.power_state = PMSG_ON;
+
+       /* reenable link pm */
+       ata_lpm_disable(host);
 }
 #endif
 
@@ -6408,22 +6743,10 @@ static void ata_host_release(struct device *gendev, void *res)
                if (!ap)
                        continue;
 
-               if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
-                       ap->ops->port_stop(ap);
-       }
-
-       if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
-               host->ops->host_stop(host);
-
-       for (i = 0; i < host->n_ports; i++) {
-               struct ata_port *ap = host->ports[i];
-
-               if (!ap)
-                       continue;
-
                if (ap->scsi_host)
                        scsi_host_put(ap->scsi_host);
 
+               kfree(ap->pmp_link);
                kfree(ap);
                host->ports[i] = NULL;
        }
@@ -6546,6 +6869,24 @@ struct ata_host *ata_host_alloc_pinfo(struct device *dev,
        return host;
 }
 
+static void ata_host_stop(struct device *gendev, void *res)
+{
+       struct ata_host *host = dev_get_drvdata(gendev);
+       int i;
+
+       WARN_ON(!(host->flags & ATA_HOST_STARTED));
+
+       for (i = 0; i < host->n_ports; i++) {
+               struct ata_port *ap = host->ports[i];
+
+               if (ap->ops->port_stop)
+                       ap->ops->port_stop(ap);
+       }
+
+       if (host->ops->host_stop)
+               host->ops->host_stop(host);
+}
+
 /**
  *     ata_host_start - start and freeze ports of an ATA host
  *     @host: ATA host to start ports for
@@ -6564,6 +6905,8 @@ struct ata_host *ata_host_alloc_pinfo(struct device *dev,
  */
 int ata_host_start(struct ata_host *host)
 {
+       int have_stop = 0;
+       void *start_dr = NULL;
        int i, rc;
 
        if (host->flags & ATA_HOST_STARTED)
@@ -6575,18 +6918,37 @@ int ata_host_start(struct ata_host *host)
                if (!host->ops && !ata_port_is_dummy(ap))
                        host->ops = ap->ops;
 
+               if (ap->ops->port_stop)
+                       have_stop = 1;
+       }
+
+       if (host->ops->host_stop)
+               have_stop = 1;
+
+       if (have_stop) {
+               start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
+               if (!start_dr)
+                       return -ENOMEM;
+       }
+
+       for (i = 0; i < host->n_ports; i++) {
+               struct ata_port *ap = host->ports[i];
+
                if (ap->ops->port_start) {
                        rc = ap->ops->port_start(ap);
                        if (rc) {
-                               ata_port_printk(ap, KERN_ERR, "failed to "
-                                               "start port (errno=%d)\n", rc);
+                               if (rc != -ENODEV)
+                                       dev_printk(KERN_ERR, host->dev,
+                                               "failed to start port %d "
+                                               "(errno=%d)\n", i, rc);
                                goto err_out;
                        }
                }
-
                ata_eh_freeze_port(ap);
        }
 
+       if (start_dr)
+               devres_add(host->dev, start_dr);
        host->flags |= ATA_HOST_STARTED;
        return 0;
 
@@ -6597,6 +6959,7 @@ int ata_host_start(struct ata_host *host)
                if (ap->ops->port_stop)
                        ap->ops->port_stop(ap);
        }
+       devres_free(start_dr);
        return rc;
 }
 
@@ -6683,13 +7046,14 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
                                              ap->udma_mask);
 
-               if (!ata_port_is_dummy(ap))
+               if (!ata_port_is_dummy(ap)) {
                        ata_port_printk(ap, KERN_INFO,
                                        "%cATA max %s %s\n",
                                        (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
                                        ata_mode_string(xfer_mask),
                                        ap->link.eh_info.desc);
-               else
+                       ata_ehi_clear_desc(&ap->link.eh_info);
+               } else
                        ata_port_printk(ap, KERN_INFO, "DUMMY\n");
        }
 
@@ -6744,6 +7108,7 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                struct ata_port *ap = host->ports[i];
 
                ata_scsi_scan_host(ap, 1);
+               ata_lpm_schedule(ap, ap->pm_policy);
        }
 
        return 0;
@@ -6762,6 +7127,10 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
  *     request IRQ and register it.  This helper takes necessasry
  *     arguments and performs the three steps in one go.
  *
+ *     An invalid IRQ skips the IRQ registration and expects the host to
+ *     have set polling mode on the port. In this case, @irq_handler
+ *     should be NULL.
+ *
  *     LOCKING:
  *     Inherited from calling layer (may sleep).
  *
@@ -6778,6 +7147,12 @@ int ata_host_activate(struct ata_host *host, int irq,
        if (rc)
                return rc;
 
+       /* Special case for polling mode */
+       if (!irq) {
+               WARN_ON(irq_handler);
+               return ata_host_register(host, sht);
+       }
+
        rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
                              dev_driver_string(host->dev), host);
        if (rc)
@@ -6805,7 +7180,7 @@ int ata_host_activate(struct ata_host *host, int irq,
  *     LOCKING:
  *     Kernel thread context (may sleep).
  */
-void ata_port_detach(struct ata_port *ap)
+static void ata_port_detach(struct ata_port *ap)
 {
        unsigned long flags;
        struct ata_link *link;
@@ -6821,18 +7196,14 @@ void ata_port_detach(struct ata_port *ap)
 
        ata_port_wait_eh(ap);
 
-       /* EH is now guaranteed to see UNLOADING, so no new device
-        * will be attached.  Disable all existing devices.
+       /* EH is now guaranteed to see UNLOADING - EH context belongs
+        * to us.  Disable all existing devices.
         */
-       spin_lock_irqsave(ap->lock, flags);
-
        ata_port_for_each_link(link, ap) {
                ata_link_for_each_dev(dev, link)
                        ata_dev_disable(dev);
        }
 
-       spin_unlock_irqrestore(ap->lock, flags);
-
        /* Final freeze & EH.  All in-flight commands are aborted.  EH
         * will be skipped and retrials will be terminated with bad
         * target.
@@ -6864,6 +7235,9 @@ void ata_host_detach(struct ata_host *host)
 
        for (i = 0; i < host->n_ports; i++)
                ata_port_detach(host->ports[i]);
+
+       /* the host is dead now, dissociate ACPI */
+       ata_acpi_dissociate(host);
 }
 
 /**
@@ -6908,7 +7282,7 @@ void ata_std_ports(struct ata_ioports *ioaddr)
  */
 void ata_pci_remove_one(struct pci_dev *pdev)
 {
-       struct device *dev = pci_dev_to_dev(pdev);
+       struct device *dev = &pdev->dev;
        struct ata_host *host = dev_get_drvdata(dev);
 
        ata_host_detach(host);
@@ -7140,7 +7514,6 @@ const struct ata_port_info ata_dummy_port_info = {
  * likely to change as new drivers are added and updated.
  * Do not depend on ABI/API stability.
  */
-
 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
@@ -7156,7 +7529,6 @@ EXPORT_SYMBOL_GPL(ata_host_register);
 EXPORT_SYMBOL_GPL(ata_host_activate);
 EXPORT_SYMBOL_GPL(ata_host_detach);
 EXPORT_SYMBOL_GPL(ata_sg_init);
-EXPORT_SYMBOL_GPL(ata_sg_init_one);
 EXPORT_SYMBOL_GPL(ata_hsm_move);
 EXPORT_SYMBOL_GPL(ata_qc_complete);
 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
@@ -7168,6 +7540,13 @@ EXPORT_SYMBOL_GPL(ata_std_dev_select);
 EXPORT_SYMBOL_GPL(sata_print_link_status);
 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
+EXPORT_SYMBOL_GPL(ata_pack_xfermask);
+EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
+EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
+EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
+EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
+EXPORT_SYMBOL_GPL(ata_mode_string);
+EXPORT_SYMBOL_GPL(ata_id_xfermask);
 EXPORT_SYMBOL_GPL(ata_check_status);
 EXPORT_SYMBOL_GPL(ata_altstatus);
 EXPORT_SYMBOL_GPL(ata_exec_command);
@@ -7196,8 +7575,6 @@ EXPORT_SYMBOL_GPL(ata_dev_disable);
 EXPORT_SYMBOL_GPL(sata_set_spd);
 EXPORT_SYMBOL_GPL(sata_link_debounce);
 EXPORT_SYMBOL_GPL(sata_link_resume);
-EXPORT_SYMBOL_GPL(sata_phy_reset);
-EXPORT_SYMBOL_GPL(__sata_phy_reset);
 EXPORT_SYMBOL_GPL(ata_bus_reset);
 EXPORT_SYMBOL_GPL(ata_std_prereset);
 EXPORT_SYMBOL_GPL(ata_std_softreset);
@@ -7210,6 +7587,7 @@ EXPORT_SYMBOL_GPL(ata_port_disable);
 EXPORT_SYMBOL_GPL(ata_ratelimit);
 EXPORT_SYMBOL_GPL(ata_wait_register);
 EXPORT_SYMBOL_GPL(ata_busy_sleep);
+EXPORT_SYMBOL_GPL(ata_wait_after_reset);
 EXPORT_SYMBOL_GPL(ata_wait_ready);
 EXPORT_SYMBOL_GPL(ata_port_queue_task);
 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
@@ -7230,12 +7608,13 @@ EXPORT_SYMBOL_GPL(ata_host_resume);
 #endif /* CONFIG_PM */
 EXPORT_SYMBOL_GPL(ata_id_string);
 EXPORT_SYMBOL_GPL(ata_id_c_string);
-EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
 
 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
+EXPORT_SYMBOL_GPL(ata_timing_find_mode);
 EXPORT_SYMBOL_GPL(ata_timing_compute);
 EXPORT_SYMBOL_GPL(ata_timing_merge);
+EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
 
 #ifdef CONFIG_PCI
 EXPORT_SYMBOL_GPL(pci_test_config_bits);
@@ -7254,9 +7633,7 @@ EXPORT_SYMBOL_GPL(ata_pci_default_filter);
 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
 #endif /* CONFIG_PCI */
 
-EXPORT_SYMBOL_GPL(sata_pmp_read_init_tf);
-EXPORT_SYMBOL_GPL(sata_pmp_read_val);
-EXPORT_SYMBOL_GPL(sata_pmp_write_init_tf);
+EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
 EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
 EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
 EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
@@ -7269,7 +7646,6 @@ EXPORT_SYMBOL_GPL(ata_port_desc);
 #ifdef CONFIG_PCI
 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
 #endif /* CONFIG_PCI */
-EXPORT_SYMBOL_GPL(ata_eng_timeout);
 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
 EXPORT_SYMBOL_GPL(ata_link_abort);
 EXPORT_SYMBOL_GPL(ata_port_abort);
@@ -7286,4 +7662,5 @@ EXPORT_SYMBOL_GPL(ata_dev_try_classify);
 EXPORT_SYMBOL_GPL(ata_cable_40wire);
 EXPORT_SYMBOL_GPL(ata_cable_80wire);
 EXPORT_SYMBOL_GPL(ata_cable_unknown);
+EXPORT_SYMBOL_GPL(ata_cable_ignore);
 EXPORT_SYMBOL_GPL(ata_cable_sata);