Merge branch 'alpm' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/libata-dev
[safe/jmp/linux-2.6] / drivers / ata / libata-core.c
index d696999..63035d7 100644 (file)
 #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>
 
@@ -68,7 +68,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,7 +94,7 @@ 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)");
 
@@ -619,6 +620,177 @@ void ata_dev_disable(struct ata_device *dev)
        }
 }
 
+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;
+
+               /* disable DIPM */
+               if (ata_dev_enabled(dev) && (dev->flags & ATA_DFLAG_DIPM))
+                       err_mask = ata_dev_set_feature(dev,
+                                       SETFEATURES_SATA_DISABLE, SATA_DIPM);
+               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;
+
+               /* disable DIPM */
+               if (ata_dev_enabled(dev) && (dev->flags & ATA_DFLAG_DIPM))
+                       err_mask = ata_dev_set_feature(dev,
+                                       SETFEATURES_SATA_DISABLE, SATA_DIPM);
+               break;
+       }
+
+       /* FIXME: handle SET FEATURES failure */
+       (void) err_mask;
+
+       return 0;
+}
+
+/**
+ *     ata_dev_enable_pm - enable SATA interface power management
+ *     @device - device to enable ipm for
+ *     @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 */
+}
+
+/**
+ *     ata_dev_disable_pm - disable SATA interface power management
+ *     @device - device to enable ipm for
+ *
+ *     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);
+}
+
+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);
+}
+
+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);
+       }
+}
+
+
 /**
  *     ata_devchk - PATA device presence detection
  *     @ap: ATA channel to examine
@@ -713,7 +885,7 @@ unsigned int ata_dev_classify(const struct ata_taskfile *tf)
        }
 
        if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
-               printk("ata: SEMB device ignored\n");
+               printk(KERN_INFO "ata: SEMB device ignored\n");
                return ATA_DEV_SEMB_UNSUP; /* not yet */
        }
 
@@ -939,7 +1111,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;
 }
@@ -1151,7 +1323,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)
 {
 }
 
@@ -1171,7 +1343,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;
 
@@ -1292,7 +1464,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;
 
@@ -1392,7 +1564,7 @@ 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)
  *
@@ -1410,7 +1582,7 @@ 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,
+                             int dma_dir, struct scatterlist *sgl,
                              unsigned int n_elem, unsigned long timeout)
 {
        struct ata_link *link = dev->link;
@@ -1472,11 +1644,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;
        }
 
@@ -1692,7 +1865,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)
@@ -1798,13 +1971,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, 0);
+               err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
                if (err_mask && id[2] != 0x738c) {
                        rc = -EIO;
                        reason = "SPINUP failed";
@@ -1978,9 +2145,8 @@ 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));
+               } else
+                       snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
 
                dev->n_sectors = ata_id_n_sectors(id);
 
@@ -2075,7 +2241,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 "
@@ -2105,11 +2272,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,
@@ -2130,6 +2304,13 @@ int ata_dev_configure(struct ata_device *dev)
                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);
 
@@ -2223,6 +2404,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);
 
@@ -2238,12 +2438,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 */
@@ -2666,8 +2860,8 @@ static const struct ata_timing ata_timing[] = {
        { 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)
 {
@@ -2694,7 +2888,7 @@ 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_timingata_timing_find_mode(unsigned short speed)
+static const struct ata_timing *ata_timing_find_mode(unsigned short speed)
 {
        const struct ata_timing *t;
 
@@ -2726,10 +2920,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);
@@ -2875,14 +3069,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);
@@ -3112,6 +3316,55 @@ 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.
+        */
+       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
@@ -3217,8 +3470,6 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
                             unsigned long deadline)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
-       struct ata_device *dev;
-       int i = 0;
 
        DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
 
@@ -3229,42 +3480,14 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
        udelay(20);     /* FIXME: flush */
        iowrite8(ap->ctl, ioaddr->ctl_addr);
 
-       /* If we issued an SRST then an ATA drive (not ATAPI)
-        * may have changed configuration and be in PIO0 timing. If
-        * we did 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.
-        */
-
-       ata_link_for_each_dev(dev, &ap->link)
-               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 revalidating */
-       if (ap->ops->set_piomode) {
-               ata_link_for_each_dev(dev, &ap->link)
-                       if (devmask & (1 << i++))
-                               ap->ops->set_piomode(ap, dev);
-       }
-
-       /* 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);
@@ -3685,8 +3908,8 @@ 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
@@ -3942,11 +4165,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 },
@@ -3958,7 +4178,7 @@ 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 },
        /* http://thread.gmane.org/gmane.linux.ide/14907 */
        { "FUJITSU MHT2060BH",  NULL,           ATA_HORKAGE_NONCQ },
        /* NCQ is broken */
@@ -3978,6 +4198,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "HTS541612J9SA00",    "SBDIC7JP",     ATA_HORKAGE_NONCQ, },
        { "HDT722516DLA380",    "V43OA96A",     ATA_HORKAGE_NONCQ, },
        { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
+       { "Hitachi HTS542525K9SA00", "BBFOC31P", ATA_HORKAGE_NONCQ, },
        { "WDC WD740ADFD-00NLR1", NULL,         ATA_HORKAGE_NONCQ, },
        { "WDC WD3200AAJS-00RYA0", "12.01B01",  ATA_HORKAGE_NONCQ, },
        { "FUJITSU MHV2080BH",  "00840028",     ATA_HORKAGE_NONCQ, },
@@ -3988,6 +4209,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "ST3160812AS",        "3.ADJ",        ATA_HORKAGE_NONCQ, },
        { "ST980813AS",         "3.ADB",        ATA_HORKAGE_NONCQ, },
        { "SAMSUNG HD401LJ",    "ZZ100-15",     ATA_HORKAGE_NONCQ, },
+       { "Maxtor 7V300F0",     "VA111900",     ATA_HORKAGE_NONCQ, },
 
        /* devices which puke on READ_NATIVE_MAX */
        { "HDS724040KLSA80",    "KFAOA20N",     ATA_HORKAGE_BROKEN_HPA, },
@@ -4003,7 +4225,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { }
 };
 
-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;
@@ -4014,8 +4236,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);
 }
@@ -4099,7 +4327,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");
@@ -4121,11 +4349,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_drive_40wire(dev->id) &&
+                   (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);
                }
@@ -4171,15 +4399,14 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        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.
@@ -4187,7 +4414,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;
@@ -4200,7 +4428,7 @@ 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, 0);
 
@@ -4286,10 +4514,10 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
                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;
+               sg_last(sg, qc->orig_n_elem)->length += qc->pad_len;
                if (pad_buf) {
                        struct scatterlist *psg = &qc->pad_sgent;
-                       void *addr = kmap_atomic(psg->page, KM_IRQ0);
+                       void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
                        memcpy(addr + psg->offset, pad_buf, qc->pad_len);
                        kunmap_atomic(addr, KM_IRQ0);
                }
@@ -4388,7 +4616,7 @@ static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
                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.
                 */
@@ -4541,6 +4769,7 @@ void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
        qc->orig_n_elem = 1;
        qc->buf_virt = buf;
        qc->nbytes = buflen;
+       qc->cursg = qc->__sg;
 
        sg_init_one(&qc->sgent, buf, buflen);
 }
@@ -4566,6 +4795,7 @@ void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
        qc->__sg = sg;
        qc->n_elem = n_elem;
        qc->orig_n_elem = n_elem;
+       qc->cursg = qc->__sg;
 }
 
 /**
@@ -4655,7 +4885,7 @@ 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];
+       struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
        int n_elem, pre_n_elem, dir, trim_sg = 0;
 
        VPRINTK("ENTER, ata%u\n", ap->print_id);
@@ -4677,11 +4907,12 @@ static int ata_sg_setup(struct ata_queued_cmd *qc)
                 * 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);
+               sg_init_table(psg, 1);
+               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(psg->page, KM_IRQ0);
+                       void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
                        memcpy(pad_buf, addr + psg->offset, qc->pad_len);
                        kunmap_atomic(addr, KM_IRQ0);
                }
@@ -4819,7 +5050,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;
@@ -4828,8 +5058,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));
@@ -4857,8 +5087,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;
        }
 }
@@ -4944,16 +5174,18 @@ static void __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 scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
        struct ata_port *ap = qc->ap;
        struct page *page;
        unsigned char *buf;
        unsigned int offset, count;
+       int no_more_sg = 0;
 
        if (qc->curbytes + bytes >= qc->nbytes)
                ap->hsm_task_state = HSM_ST_LAST;
 
 next_sg:
-       if (unlikely(qc->cursg >= qc->n_elem)) {
+       if (unlikely(no_more_sg)) {
                /*
                 * The end of qc->sg is reached and the device expects
                 * more data to transfer. In order not to overrun qc->sg
@@ -4970,15 +5202,15 @@ next_sg:
                                       "%u bytes trailing data\n", bytes);
 
                for (i = 0; i < words; i++)
-                       ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
+                       ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write);
 
                ap->hsm_task_state = HSM_ST_LAST;
                return;
        }
 
-       sg = &qc->__sg[qc->cursg];
+       sg = qc->cursg;
 
-       page = sg->page;
+       page = sg_page(sg);
        offset = sg->offset + qc->cursg_ofs;
 
        /* get the current page and offset */
@@ -5015,7 +5247,10 @@ next_sg:
        qc->cursg_ofs += count;
 
        if (qc->cursg_ofs == sg->length) {
-               qc->cursg++;
+               if (qc->cursg == lsg)
+                       no_more_sg = 1;
+
+               qc->cursg = sg_next(qc->cursg);
                qc->cursg_ofs = 0;
        }
 
@@ -5578,6 +5813,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))
@@ -5596,6 +5834,27 @@ 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;
+               }
+
                __ata_qc_complete(qc);
        } else {
                if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
@@ -5733,6 +5992,14 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
                qc->flags &= ~ATA_QCFLAG_DMAMAP;
        }
 
+       /* 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);
 
        qc->err_mask |= ap->ops->qc_issue(qc);
@@ -5895,8 +6162,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;
@@ -5996,7 +6263,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;
@@ -6199,7 +6466,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) {
@@ -6280,6 +6547,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;
@@ -6302,6 +6575,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
 
@@ -6844,6 +7120,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;
@@ -6905,7 +7182,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;
@@ -7240,7 +7517,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);
@@ -7310,6 +7586,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);