ide-cd: replace C code with call to ARRAY_SIZE() macro
[safe/jmp/linux-2.6] / drivers / ata / libata-core.c
index 8d3ae66..981b397 100644 (file)
@@ -59,7 +59,7 @@
 
 #include "libata.h"
 
-#define DRV_VERSION    "2.20"  /* must be exactly four chars */
+#define DRV_VERSION    "2.21"  /* must be exactly four chars */
 
 
 /* debounce timing parameters in msecs { interval, duration, timeout } */
@@ -89,6 +89,10 @@ 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;
+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 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)");
@@ -596,8 +600,9 @@ static const char *sata_spd_string(unsigned int spd)
 
 void ata_dev_disable(struct ata_device *dev)
 {
-       if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
-               ata_dev_printk(dev, KERN_WARNING, "disabled\n");
+       if (ata_dev_enabled(dev)) {
+               if (ata_msg_drv(dev->ap))
+                       ata_dev_printk(dev, KERN_WARNING, "disabled\n");
                ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
                                             ATA_DNXFER_QUIET);
                dev->class++;
@@ -808,6 +813,206 @@ void ata_id_c_string(const u16 *id, unsigned char *s,
        *p = '\0';
 }
 
+static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
+{
+       u64 sectors = 0;
+
+       sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
+       sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
+       sectors |= (tf->hob_lbal & 0xff) << 24;
+       sectors |= (tf->lbah & 0xff) << 16;
+       sectors |= (tf->lbam & 0xff) << 8;
+       sectors |= (tf->lbal & 0xff);
+
+       return ++sectors;
+}
+
+static u64 ata_tf_to_lba(struct ata_taskfile *tf)
+{
+       u64 sectors = 0;
+
+       sectors |= (tf->device & 0x0f) << 24;
+       sectors |= (tf->lbah & 0xff) << 16;
+       sectors |= (tf->lbam & 0xff) << 8;
+       sectors |= (tf->lbal & 0xff);
+
+       return ++sectors;
+}
+
+/**
+ *     ata_read_native_max_address_ext -       LBA48 native max query
+ *     @dev: Device to query
+ *
+ *     Perform an LBA48 size query upon the device in question. Return the
+ *     actual LBA48 size or zero if the command fails.
+ */
+
+static u64 ata_read_native_max_address_ext(struct ata_device *dev)
+{
+       unsigned int err;
+       struct ata_taskfile tf;
+
+       ata_tf_init(dev, &tf);
+
+       tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
+       tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | ATA_TFLAG_ISADDR;
+       tf.protocol |= ATA_PROT_NODATA;
+       tf.device |= 0x40;
+
+       err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       if (err)
+               return 0;
+
+       return ata_tf_to_lba48(&tf);
+}
+
+/**
+ *     ata_read_native_max_address     -       LBA28 native max query
+ *     @dev: Device to query
+ *
+ *     Performa an LBA28 size query upon the device in question. Return the
+ *     actual LBA28 size or zero if the command fails.
+ */
+
+static u64 ata_read_native_max_address(struct ata_device *dev)
+{
+       unsigned int err;
+       struct ata_taskfile tf;
+
+       ata_tf_init(dev, &tf);
+
+       tf.command = ATA_CMD_READ_NATIVE_MAX;
+       tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
+       tf.protocol |= ATA_PROT_NODATA;
+       tf.device |= 0x40;
+
+       err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       if (err)
+               return 0;
+
+       return ata_tf_to_lba(&tf);
+}
+
+/**
+ *     ata_set_native_max_address_ext  -       LBA48 native max set
+ *     @dev: Device to query
+ *     @new_sectors: new max sectors value to set for the device
+ *
+ *     Perform an LBA48 size set max upon the device in question. Return the
+ *     actual LBA48 size or zero if the command fails.
+ */
+
+static u64 ata_set_native_max_address_ext(struct ata_device *dev, u64 new_sectors)
+{
+       unsigned int err;
+       struct ata_taskfile tf;
+
+       new_sectors--;
+
+       ata_tf_init(dev, &tf);
+
+       tf.command = ATA_CMD_SET_MAX_EXT;
+       tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | ATA_TFLAG_ISADDR;
+       tf.protocol |= ATA_PROT_NODATA;
+       tf.device |= 0x40;
+
+       tf.lbal = (new_sectors >> 0) & 0xff;
+       tf.lbam = (new_sectors >> 8) & 0xff;
+       tf.lbah = (new_sectors >> 16) & 0xff;
+
+       tf.hob_lbal = (new_sectors >> 24) & 0xff;
+       tf.hob_lbam = (new_sectors >> 32) & 0xff;
+       tf.hob_lbah = (new_sectors >> 40) & 0xff;
+
+       err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       if (err)
+               return 0;
+
+       return ata_tf_to_lba48(&tf);
+}
+
+/**
+ *     ata_set_native_max_address      -       LBA28 native max set
+ *     @dev: Device to query
+ *     @new_sectors: new max sectors value to set for the device
+ *
+ *     Perform an LBA28 size set max upon the device in question. Return the
+ *     actual LBA28 size or zero if the command fails.
+ */
+
+static u64 ata_set_native_max_address(struct ata_device *dev, u64 new_sectors)
+{
+       unsigned int err;
+       struct ata_taskfile tf;
+
+       new_sectors--;
+
+       ata_tf_init(dev, &tf);
+
+       tf.command = ATA_CMD_SET_MAX;
+       tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
+       tf.protocol |= ATA_PROT_NODATA;
+
+       tf.lbal = (new_sectors >> 0) & 0xff;
+       tf.lbam = (new_sectors >> 8) & 0xff;
+       tf.lbah = (new_sectors >> 16) & 0xff;
+       tf.device |= ((new_sectors >> 24) & 0x0f) | 0x40;
+
+       err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       if (err)
+               return 0;
+
+       return ata_tf_to_lba(&tf);
+}
+
+/**
+ *     ata_hpa_resize          -       Resize a device with an HPA set
+ *     @dev: Device to resize
+ *
+ *     Read the size of an LBA28 or LBA48 disk with HPA features and resize
+ *     it if required to the full size of the media. The caller must check
+ *     the drive has the HPA feature set enabled.
+ */
+
+static u64 ata_hpa_resize(struct ata_device *dev)
+{
+       u64 sectors = dev->n_sectors;
+       u64 hpa_sectors;
+
+       if (ata_id_has_lba48(dev->id))
+               hpa_sectors = ata_read_native_max_address_ext(dev);
+       else
+               hpa_sectors = ata_read_native_max_address(dev);
+
+       if (hpa_sectors > sectors) {
+               ata_dev_printk(dev, KERN_INFO,
+                       "Host Protected Area detected:\n"
+                       "\tcurrent size: %lld sectors\n"
+                       "\tnative size: %lld sectors\n",
+                       (long long)sectors, (long long)hpa_sectors);
+
+               if (ata_ignore_hpa) {
+                       if (ata_id_has_lba48(dev->id))
+                               hpa_sectors = ata_set_native_max_address_ext(dev, hpa_sectors);
+                       else
+                               hpa_sectors = ata_set_native_max_address(dev,
+                                                               hpa_sectors);
+
+                       if (hpa_sectors) {
+                               ata_dev_printk(dev, KERN_INFO, "native size "
+                                       "increased to %lld sectors\n",
+                                       (long long)hpa_sectors);
+                               return hpa_sectors;
+                       }
+               }
+       } else if (hpa_sectors < sectors)
+               ata_dev_printk(dev, KERN_WARNING, "%s 1: hpa sectors (%lld) "
+                              "is smaller than sectors (%lld)\n", __FUNCTION__,
+                              (long long)hpa_sectors, (long long)sectors);
+
+       return sectors;
+}
+
 static u64 ata_id_n_sectors(const u16 *id)
 {
        if (ata_id_has_lba(id)) {
@@ -1113,7 +1318,7 @@ void ata_port_flush_task(struct ata_port *ap)
        spin_unlock_irqrestore(ap->lock, flags);
 
        DPRINTK("flush #1\n");
-       flush_workqueue(ata_wq);
+       cancel_work_sync(&ap->port_task.work); /* akpm: seems unneeded */
 
        /*
         * At this point, if a task is running, it's guaranteed to see
@@ -1124,7 +1329,7 @@ void ata_port_flush_task(struct ata_port *ap)
                if (ata_msg_ctl(ap))
                        ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
                                        __FUNCTION__);
-               flush_workqueue(ata_wq);
+               cancel_work_sync(&ap->port_task.work);
        }
 
        spin_lock_irqsave(ap->lock, flags);
@@ -1383,7 +1588,7 @@ unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  *     Check if the current speed of the device requires IORDY. Used
  *     by various controllers for chip configuration.
  */
+
 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
 {
        /* Controller doesn't support  IORDY. Probably a pointless check
@@ -1406,7 +1611,7 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  *     Compute the highest mode possible if we are not using iordy. Return
  *     -1 if no iordy mode is available.
  */
+
 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
 {
        /* If we have no drive specific rule, then PIO 2 is non IORDY */
@@ -1449,13 +1654,13 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
        struct ata_taskfile tf;
        unsigned int err_mask = 0;
        const char *reason;
+       int may_fallback = 1, tried_spinup = 0;
        int rc;
 
        if (ata_msg_ctl(ap))
                ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
 
        ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
-
  retry:
        ata_tf_init(dev, &tf);
 
@@ -1493,16 +1698,36 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                        return -ENOENT;
                }
 
+               /* Device or controller might have reported the wrong
+                * device class.  Give a shot at the other IDENTIFY if
+                * the current one is aborted by the device.
+                */
+               if (may_fallback &&
+                   (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
+                       may_fallback = 0;
+
+                       if (class == ATA_DEV_ATA)
+                               class = ATA_DEV_ATAPI;
+                       else
+                               class = ATA_DEV_ATA;
+                       goto retry;
+               }
+
                rc = -EIO;
                reason = "I/O error";
                goto err_out;
        }
 
+       /* Falling back doesn't make sense if ID data was read
+        * successfully at least once.
+        */
+       may_fallback = 0;
+
        swap_buf_le16(id, ATA_ID_WORDS);
 
        /* sanity check */
        rc = -EINVAL;
-       reason = "device reports illegal type";
+       reason = "device reports invalid type";
 
        if (class == ATA_DEV_ATA) {
                if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
@@ -1512,6 +1737,32 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                        goto err_out;
        }
 
+       if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
+               tried_spinup = 1;
+               /*
+                * Drive powered-up in standby mode, and requires a specific
+                * 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);
+               if (err_mask) {
+                       rc = -EIO;
+                       reason = "SPINUP failed";
+                       goto err_out;
+               }
+               /*
+                * If the drive initially returned incomplete IDENTIFY info,
+                * we now must reissue the IDENTIFY command.
+                */
+               if (id[2] == 0x37c8)
+                       goto retry;
+       }
+
        if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
                /*
                 * The exact sequence expected by certain pre-ATA4 drives is:
@@ -1612,7 +1863,7 @@ int ata_dev_configure(struct ata_device *dev)
                ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
 
        /* set _SDD */
-       rc = ata_acpi_push_id(ap, dev->devno);
+       rc = ata_acpi_push_id(dev);
        if (rc) {
                ata_dev_printk(dev, KERN_WARNING, "failed to set _SDD(%d)\n",
                        rc);
@@ -1649,6 +1900,13 @@ int ata_dev_configure(struct ata_device *dev)
        if (ata_msg_probe(ap))
                ata_dump_id(id);
 
+       /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
+       ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
+                       sizeof(fwrevbuf));
+
+       ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
+                       sizeof(modelbuf));
+
        /* ATA-specific feature tests */
        if (dev->class == ATA_DEV_ATA) {
                if (ata_id_is_cfa(id)) {
@@ -1663,13 +1921,6 @@ int ata_dev_configure(struct ata_device *dev)
 
                dev->n_sectors = ata_id_n_sectors(id);
 
-               /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
-               ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
-                               sizeof(fwrevbuf));
-
-               ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
-                               sizeof(modelbuf));
-
                if (dev->id[59] & 0x100)
                        dev->multi_count = dev->id[59] & 0xff;
 
@@ -1688,6 +1939,9 @@ int ata_dev_configure(struct ata_device *dev)
                                        dev->flags |= ATA_DFLAG_FLUSH_EXT;
                        }
 
+                       if (ata_id_hpa_enabled(dev->id))
+                               dev->n_sectors = ata_hpa_resize(dev);
+
                        /* config NCQ */
                        ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
 
@@ -1755,7 +2009,9 @@ int ata_dev_configure(struct ata_device *dev)
 
                /* print device info to dmesg */
                if (ata_msg_drv(ap) && print_info)
-                       ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
+                       ata_dev_printk(dev, KERN_INFO,
+                                      "ATAPI: %s, %s, max %s%s\n",
+                                      modelbuf, fwrevbuf,
                                       ata_mode_string(xfer_mask),
                                       cdb_intr_string);
        }
@@ -1790,10 +2046,6 @@ int ata_dev_configure(struct ata_device *dev)
                dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
                                         dev->max_sectors);
 
-       /* limit ATAPI DMA to R/W commands only */
-       if (ata_device_blacklisted(dev) & ATA_HORKAGE_DMA_RW_ONLY)
-               dev->horkage |= ATA_HORKAGE_DMA_RW_ONLY;
-
        if (ap->ops->dev_config)
                ap->ops->dev_config(dev);
 
@@ -2410,6 +2662,12 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
                t->recover = t->cycle - t->active;
        }
 
+       /* In a few cases quantisation may produce enough errors to
+          leave t->cycle too low for the sum of active and recovery
+          if so we must correct this */
+       if (t->active + t->recover > t->cycle)
+               t->cycle = t->active + t->recover;
+
        return 0;
 }
 
@@ -2619,7 +2877,7 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
                dev = &ap->device[i];
 
                /* don't update suspended devices' xfer mode */
-               if (!ata_dev_ready(dev))
+               if (!ata_dev_enabled(dev))
                        continue;
 
                rc = ata_dev_set_mode(dev);
@@ -2633,9 +2891,6 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
        if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
                ap->host->simplex_claimed = ap;
 
-       /* step5: chip specific finalisation */
-       if (ap->ops->post_set_mode)
-               ap->ops->post_set_mode(ap);
  out:
        if (rc)
                *r_failed_dev = dev;
@@ -2740,39 +2995,97 @@ int ata_busy_sleep(struct ata_port *ap,
        return 0;
 }
 
-static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
+/**
+ *     ata_wait_ready - sleep until BSY clears, or timeout
+ *     @ap: port containing status register to be polled
+ *     @deadline: deadline jiffies for the operation
+ *
+ *     Sleep until ATA Status register bit BSY clears, or timeout
+ *     occurs.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
+ */
+int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
+{
+       unsigned long start = jiffies;
+       int warned = 0;
+
+       while (1) {
+               u8 status = ata_chk_status(ap);
+               unsigned long now = jiffies;
+
+               if (!(status & ATA_BUSY))
+                       return 0;
+               if (!ata_port_online(ap) && status == 0xff)
+                       return -ENODEV;
+               if (time_after(now, deadline))
+                       return -EBUSY;
+
+               if (!warned && time_after(now, start + 5 * HZ) &&
+                   (deadline - now > 3 * HZ)) {
+                       ata_port_printk(ap, KERN_WARNING,
+                               "port is slow to respond, please be patient "
+                               "(Status 0x%x)\n", status);
+                       warned = 1;
+               }
+
+               msleep(50);
+       }
+}
+
+static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
+                             unsigned long deadline)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
        unsigned int dev0 = devmask & (1 << 0);
        unsigned int dev1 = devmask & (1 << 1);
-       unsigned long timeout;
+       int rc, ret = 0;
 
        /* if device 0 was found in ata_devchk, wait for its
         * BSY bit to clear
         */
-       if (dev0)
-               ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
+       if (dev0) {
+               rc = ata_wait_ready(ap, deadline);
+               if (rc) {
+                       if (rc != -ENODEV)
+                               return rc;
+                       ret = rc;
+               }
+       }
 
-       /* if device 1 was found in ata_devchk, wait for
-        * register access, then wait for BSY to clear
+       /* if device 1 was found in ata_devchk, wait for register
+        * access briefly, then wait for BSY to clear.
         */
-       timeout = jiffies + ATA_TMOUT_BOOT;
-       while (dev1) {
-               u8 nsect, lbal;
+       if (dev1) {
+               int i;
 
                ap->ops->dev_select(ap, 1);
-               nsect = ioread8(ioaddr->nsect_addr);
-               lbal = ioread8(ioaddr->lbal_addr);
-               if ((nsect == 1) && (lbal == 1))
-                       break;
-               if (time_after(jiffies, timeout)) {
-                       dev1 = 0;
-                       break;
+
+               /* Wait for register access.  Some ATAPI devices fail
+                * to set nsect/lbal after reset, so don't waste too
+                * much time on it.  We're gonna wait for !BSY anyway.
+                */
+               for (i = 0; i < 2; i++) {
+                       u8 nsect, lbal;
+
+                       nsect = ioread8(ioaddr->nsect_addr);
+                       lbal = ioread8(ioaddr->lbal_addr);
+                       if ((nsect == 1) && (lbal == 1))
+                               break;
+                       msleep(50);     /* give drive a breather */
+               }
+
+               rc = ata_wait_ready(ap, deadline);
+               if (rc) {
+                       if (rc != -ENODEV)
+                               return rc;
+                       ret = rc;
                }
-               msleep(50);     /* give drive a breather */
        }
-       if (dev1)
-               ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
 
        /* is all this really necessary? */
        ap->ops->dev_select(ap, 0);
@@ -2780,10 +3093,12 @@ static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
                ap->ops->dev_select(ap, 1);
        if (dev0)
                ap->ops->dev_select(ap, 0);
+
+       return ret;
 }
 
-static unsigned int ata_bus_softreset(struct ata_port *ap,
-                                     unsigned int devmask)
+static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
+                            unsigned long deadline)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
 
@@ -2813,11 +3128,9 @@ static unsigned int ata_bus_softreset(struct ata_port *ap,
         * pulldown resistor.
         */
        if (ata_check_status(ap) == 0xFF)
-               return 0;
-
-       ata_bus_post_reset(ap, devmask);
+               return -ENODEV;
 
-       return 0;
+       return ata_bus_post_reset(ap, devmask, deadline);
 }
 
 /**
@@ -2846,6 +3159,7 @@ void ata_bus_reset(struct ata_port *ap)
        unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
        u8 err;
        unsigned int dev0, dev1 = 0, devmask = 0;
+       int rc;
 
        DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
 
@@ -2867,9 +3181,11 @@ void ata_bus_reset(struct ata_port *ap)
        ap->ops->dev_select(ap, 0);
 
        /* issue bus reset */
-       if (ap->flags & ATA_FLAG_SRST)
-               if (ata_bus_softreset(ap, devmask))
+       if (ap->flags & ATA_FLAG_SRST) {
+               rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
+               if (rc && rc != -ENODEV)
                        goto err_out;
+       }
 
        /*
         * determine by signature whether we have ATA or ATAPI devices
@@ -2911,29 +3227,37 @@ err_out:
  *     sata_phy_debounce - debounce SATA phy status
  *     @ap: ATA port to debounce SATA phy status for
  *     @params: timing parameters { interval, duratinon, timeout } in msec
+ *     @deadline: deadline jiffies for the operation
  *
  *     Make sure SStatus of @ap reaches stable state, determined by
  *     holding the same value where DET is not 1 for @duration polled
  *     every @interval, before @timeout.  Timeout constraints the
- *     beginning of the stable state.  Because, after hot unplugging,
- *     DET gets stuck at 1 on some controllers, this functions waits
+ *     beginning of the stable state.  Because DET gets stuck at 1 on
+ *     some controllers after hot unplugging, this functions waits
  *     until timeout then returns 0 if DET is stable at 1.
  *
+ *     @timeout is further limited by @deadline.  The sooner of the
+ *     two is used.
+ *
  *     LOCKING:
  *     Kernel thread context (may sleep)
  *
  *     RETURNS:
  *     0 on success, -errno on failure.
  */
-int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
+int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
+                     unsigned long deadline)
 {
        unsigned long interval_msec = params[0];
-       unsigned long duration = params[1] * HZ / 1000;
-       unsigned long timeout = jiffies + params[2] * HZ / 1000;
-       unsigned long last_jiffies;
+       unsigned long duration = msecs_to_jiffies(params[1]);
+       unsigned long last_jiffies, t;
        u32 last, cur;
        int rc;
 
+       t = jiffies + msecs_to_jiffies(params[2]);
+       if (time_before(t, deadline))
+               deadline = t;
+
        if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
                return rc;
        cur &= 0xf;
@@ -2949,7 +3273,7 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
 
                /* DET stable? */
                if (cur == last) {
-                       if (cur == 1 && time_before(jiffies, timeout))
+                       if (cur == 1 && time_before(jiffies, deadline))
                                continue;
                        if (time_after(jiffies, last_jiffies + duration))
                                return 0;
@@ -2960,8 +3284,8 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
                last = cur;
                last_jiffies = jiffies;
 
-               /* check timeout */
-               if (time_after(jiffies, timeout))
+               /* check deadline */
+               if (time_after(jiffies, deadline))
                        return -EBUSY;
        }
 }
@@ -2970,6 +3294,7 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  *     sata_phy_resume - resume SATA phy
  *     @ap: ATA port to resume SATA phy for
  *     @params: timing parameters { interval, duratinon, timeout } in msec
+ *     @deadline: deadline jiffies for the operation
  *
  *     Resume SATA phy of @ap and debounce it.
  *
@@ -2979,7 +3304,8 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  *     RETURNS:
  *     0 on success, -errno on failure.
  */
-int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
+int sata_phy_resume(struct ata_port *ap, const unsigned long *params,
+                   unsigned long deadline)
 {
        u32 scontrol;
        int rc;
@@ -2997,43 +3323,19 @@ int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
         */
        msleep(200);
 
-       return sata_phy_debounce(ap, params);
-}
-
-static void ata_wait_spinup(struct ata_port *ap)
-{
-       struct ata_eh_context *ehc = &ap->eh_context;
-       unsigned long end, secs;
-       int rc;
-
-       /* first, debounce phy if SATA */
-       if (ap->cbl == ATA_CBL_SATA) {
-               rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
-
-               /* if debounced successfully and offline, no need to wait */
-               if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
-                       return;
-       }
-
-       /* okay, let's give the drive time to spin up */
-       end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
-       secs = ((end - jiffies) + HZ - 1) / HZ;
-
-       if (time_after(jiffies, end))
-               return;
-
-       if (secs > 5)
-               ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
-                               "(%lu secs)\n", secs);
-
-       schedule_timeout_uninterruptible(end - jiffies);
+       return sata_phy_debounce(ap, params, deadline);
 }
 
 /**
  *     ata_std_prereset - prepare for reset
  *     @ap: ATA port to be reset
+ *     @deadline: deadline jiffies for the operation
  *
- *     @ap is about to be reset.  Initialize it.
+ *     @ap is about to be reset.  Initialize it.  Failure from
+ *     prereset makes libata abort whole reset sequence and give up
+ *     that port, so prereset should be best-effort.  It does its
+ *     best to prepare for reset sequence but if things go wrong, it
+ *     should just whine, not fail.
  *
  *     LOCKING:
  *     Kernel thread context (may sleep)
@@ -3041,41 +3343,41 @@ static void ata_wait_spinup(struct ata_port *ap)
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int ata_std_prereset(struct ata_port *ap)
+int ata_std_prereset(struct ata_port *ap, unsigned long deadline)
 {
        struct ata_eh_context *ehc = &ap->eh_context;
        const unsigned long *timing = sata_ehc_deb_timing(ehc);
        int rc;
 
-       /* handle link resume & hotplug spinup */
+       /* handle link resume */
        if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
            (ap->flags & ATA_FLAG_HRST_TO_RESUME))
                ehc->i.action |= ATA_EH_HARDRESET;
 
-       if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
-           (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
-               ata_wait_spinup(ap);
-
        /* if we're about to do hardreset, nothing more to do */
        if (ehc->i.action & ATA_EH_HARDRESET)
                return 0;
 
        /* if SATA, resume phy */
        if (ap->cbl == ATA_CBL_SATA) {
-               rc = sata_phy_resume(ap, timing);
-               if (rc && rc != -EOPNOTSUPP) {
-                       /* phy resume failed */
+               rc = sata_phy_resume(ap, timing, deadline);
+               /* whine about phy resume failure but proceed */
+               if (rc && rc != -EOPNOTSUPP)
                        ata_port_printk(ap, KERN_WARNING, "failed to resume "
                                        "link for reset (errno=%d)\n", rc);
-                       return rc;
-               }
        }
 
        /* Wait for !BSY if the controller can wait for the first D2H
         * Reg FIS and we don't know that no device is attached.
         */
-       if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
-               ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
+       if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap)) {
+               rc = ata_wait_ready(ap, deadline);
+               if (rc && rc != -ENODEV) {
+                       ata_port_printk(ap, KERN_WARNING, "device not ready "
+                                       "(errno=%d), forcing hardreset\n", rc);
+                       ehc->i.action |= ATA_EH_HARDRESET;
+               }
+       }
 
        return 0;
 }
@@ -3084,6 +3386,7 @@ int ata_std_prereset(struct ata_port *ap)
  *     ata_std_softreset - reset host port via ATA SRST
  *     @ap: port to reset
  *     @classes: resulting classes of attached devices
+ *     @deadline: deadline jiffies for the operation
  *
  *     Reset host port using ATA SRST.
  *
@@ -3093,10 +3396,12 @@ int ata_std_prereset(struct ata_port *ap)
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
+int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
+                     unsigned long deadline)
 {
        unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
-       unsigned int devmask = 0, err_mask;
+       unsigned int devmask = 0;
+       int rc;
        u8 err;
 
        DPRINTK("ENTER\n");
@@ -3117,11 +3422,11 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
 
        /* issue bus reset */
        DPRINTK("about to softreset, devmask=%x\n", devmask);
-       err_mask = ata_bus_softreset(ap, devmask);
-       if (err_mask) {
-               ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
-                               err_mask);
-               return -EIO;
+       rc = ata_bus_softreset(ap, devmask, deadline);
+       /* if link is occupied, -ENODEV too is an error */
+       if (rc && (rc != -ENODEV || sata_scr_valid(ap))) {
+               ata_port_printk(ap, KERN_ERR, "SRST failed (errno=%d)\n", rc);
+               return rc;
        }
 
        /* determine by signature whether we have ATA or ATAPI devices */
@@ -3138,6 +3443,7 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  *     sata_port_hardreset - reset port via SATA phy reset
  *     @ap: port to reset
  *     @timing: timing parameters { interval, duratinon, timeout } in msec
+ *     @deadline: deadline jiffies for the operation
  *
  *     SATA phy-reset host port using DET bits of SControl register.
  *
@@ -3147,7 +3453,8 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
+int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
+                       unsigned long deadline)
 {
        u32 scontrol;
        int rc;
@@ -3186,7 +3493,7 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
        msleep(1);
 
        /* bring phy back */
-       rc = sata_phy_resume(ap, timing);
+       rc = sata_phy_resume(ap, timing, deadline);
  out:
        DPRINTK("EXIT, rc=%d\n", rc);
        return rc;
@@ -3196,6 +3503,7 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
  *     sata_std_hardreset - reset host port via SATA phy reset
  *     @ap: port to reset
  *     @class: resulting class of attached device
+ *     @deadline: deadline jiffies for the operation
  *
  *     SATA phy-reset host port using DET bits of SControl register,
  *     wait for !BSY and classify the attached device.
@@ -3206,7 +3514,8 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
+int sata_std_hardreset(struct ata_port *ap, unsigned int *class,
+                      unsigned long deadline)
 {
        const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
        int rc;
@@ -3214,7 +3523,7 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
        DPRINTK("ENTER\n");
 
        /* do hardreset */
-       rc = sata_port_hardreset(ap, timing);
+       rc = sata_port_hardreset(ap, timing, deadline);
        if (rc) {
                ata_port_printk(ap, KERN_ERR,
                                "COMRESET failed (errno=%d)\n", rc);
@@ -3231,10 +3540,12 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
        /* wait a while before checking status, see SRST for more info */
        msleep(150);
 
-       if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
+       rc = ata_wait_ready(ap, deadline);
+       /* link occupied, -ENODEV too is an error */
+       if (rc) {
                ata_port_printk(ap, KERN_ERR,
-                               "COMRESET failed (device not ready)\n");
-               return -EIO;
+                               "COMRESET failed (errno=%d)\n", rc);
+               return rc;
        }
 
        ap->ops->dev_select(ap, 0);     /* probably unnecessary */
@@ -3315,7 +3626,6 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
        const u16 *old_id = dev->id;
        unsigned char model[2][ATA_ID_PROD_LEN + 1];
        unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
-       u64 new_n_sectors;
 
        if (dev->class != new_class) {
                ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
@@ -3327,7 +3637,6 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
        ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
        ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
        ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
-       new_n_sectors = ata_id_n_sectors(new_id);
 
        if (strcmp(model[0], model[1])) {
                ata_dev_printk(dev, KERN_INFO, "model number mismatch "
@@ -3341,20 +3650,12 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
                return 0;
        }
 
-       if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
-               ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
-                              "%llu != %llu\n",
-                              (unsigned long long)dev->n_sectors,
-                              (unsigned long long)new_n_sectors);
-               return 0;
-       }
-
        return 1;
 }
 
 /**
- *     ata_dev_revalidate - Revalidate ATA device
- *     @dev: device to revalidate
+ *     ata_dev_reread_id - Re-read IDENTIFY data
+ *     @dev: target ATA device
  *     @readid_flags: read ID flags
  *
  *     Re-read IDENTIFY page and make sure @dev is still attached to
@@ -3366,34 +3667,68 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  *     RETURNS:
  *     0 on success, negative errno otherwise
  */
-int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
+int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
 {
        unsigned int class = dev->class;
        u16 *id = (void *)dev->ap->sector_buf;
        int rc;
 
-       if (!ata_dev_enabled(dev)) {
-               rc = -ENODEV;
-               goto fail;
-       }
-
        /* read ID data */
        rc = ata_dev_read_id(dev, &class, readid_flags, id);
        if (rc)
-               goto fail;
+               return rc;
 
        /* is the device still there? */
-       if (!ata_dev_same_device(dev, class, id)) {
-               rc = -ENODEV;
-               goto fail;
-       }
+       if (!ata_dev_same_device(dev, class, id))
+               return -ENODEV;
 
        memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
+       return 0;
+}
+
+/**
+ *     ata_dev_revalidate - Revalidate ATA device
+ *     @dev: device to revalidate
+ *     @readid_flags: read ID flags
+ *
+ *     Re-read IDENTIFY page, make sure @dev is still attached to the
+ *     port and reconfigure it according to the new IDENTIFY page.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ *
+ *     RETURNS:
+ *     0 on success, negative errno otherwise
+ */
+int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
+{
+       u64 n_sectors = dev->n_sectors;
+       int rc;
+
+       if (!ata_dev_enabled(dev))
+               return -ENODEV;
+
+       /* re-read ID */
+       rc = ata_dev_reread_id(dev, readid_flags);
+       if (rc)
+               goto fail;
 
        /* configure device according to the new ID */
        rc = ata_dev_configure(dev);
-       if (rc == 0)
-               return 0;
+       if (rc)
+               goto fail;
+
+       /* verify n_sectors hasn't changed */
+       if (dev->class == ATA_DEV_ATA && dev->n_sectors != n_sectors) {
+               ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
+                              "%llu != %llu\n",
+                              (unsigned long long)n_sectors,
+                              (unsigned long long)dev->n_sectors);
+               rc = -ENODEV;
+               goto fail;
+       }
+
+       return 0;
 
  fail:
        ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
@@ -3437,10 +3772,11 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "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 },
+       { "Seagate STT20000A", NULL,            ATA_HORKAGE_NODMA },
+       { "IOMEGA  ZIP 250       ATAPI", NULL,  ATA_HORKAGE_NODMA }, /* temporary fix */
 
        /* Weird ATAPI devices */
-       { "TORiSAN DVD-ROM DRD-N216", NULL,     ATA_HORKAGE_MAX_SEC_128 |
-                                               ATA_HORKAGE_DMA_RW_ONLY },
+       { "TORiSAN DVD-ROM DRD-N216", NULL,     ATA_HORKAGE_MAX_SEC_128 },
 
        /* Devices we expect to fail diagnostics */
 
@@ -3451,6 +3787,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "FUJITSU MHT2060BH",  NULL,           ATA_HORKAGE_NONCQ },
        /* NCQ is broken */
        { "Maxtor 6L250S0",     "BANC1G10",     ATA_HORKAGE_NONCQ },
+       { "Maxtor 6B200M0",     "BANC1B10",     ATA_HORKAGE_NONCQ },
        /* NCQ hard hangs device under heavier load, needs hard power cycle */
        { "Maxtor 6B250S0",     "BANC1B70",     ATA_HORKAGE_NONCQ },
        /* Blacklist entries taken from Silicon Image 3124/3132
@@ -3458,6 +3795,11 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "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, },
 
        /* Devices with NCQ limits */
 
@@ -3600,10 +3942,13 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        /* set up set-features taskfile */
        DPRINTK("set features - xfer mode\n");
 
+       /* Some controllers and ATAPI devices show flaky interrupt
+        * behavior after setting xfer mode.  Use polling instead.
+        */
        ata_tf_init(dev, &tf);
        tf.command = ATA_CMD_SET_FEATURES;
        tf.feature = SETFEATURES_XFER;
-       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
+       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = dev->xfer_mode;
 
@@ -3760,6 +4105,7 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
        if (idx)
                ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
 }
+
 /**
  *     ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  *     @qc: Metadata associated with taskfile to check
@@ -3777,33 +4123,19 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       int rc = 0; /* Assume ATAPI DMA is OK by default */
-
-       /* some drives can only do ATAPI DMA on read/write */
-       if (unlikely(qc->dev->horkage & ATA_HORKAGE_DMA_RW_ONLY)) {
-               struct scsi_cmnd *cmd = qc->scsicmd;
-               u8 *scsicmd = cmd->cmnd;
-
-               switch (scsicmd[0]) {
-               case READ_10:
-               case WRITE_10:
-               case READ_12:
-               case WRITE_12:
-               case READ_6:
-               case WRITE_6:
-                       /* atapi dma maybe ok */
-                       break;
-               default:
-                       /* turn off atapi dma */
-                       return 1;
-               }
-       }
+
+       /* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
+        * few ATAPI devices choke on such DMA requests.
+        */
+       if (unlikely(qc->nbytes & 15))
+               return 1;
 
        if (ap->ops->check_atapi_dma)
-               rc = ap->ops->check_atapi_dma(qc);
+               return ap->ops->check_atapi_dma(qc);
 
-       return rc;
+       return 0;
 }
+
 /**
  *     ata_qc_prep - Prepare taskfile for submission
  *     @qc: Metadata associated with taskfile to be prepared
@@ -4450,8 +4782,6 @@ static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
                } else
                        ata_qc_complete(qc);
        }
-
-       ata_altstatus(ap); /* flush */
 }
 
 /**
@@ -5081,14 +5411,6 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
                }
        }
 
-       /* Some controllers show flaky interrupt behavior after
-        * setting xfer mode.  Use polling instead.
-        */
-       if (unlikely(qc->tf.command == ATA_CMD_SET_FEATURES &&
-                    qc->tf.feature == SETFEATURES_XFER) &&
-           (ap->flags & ATA_FLAG_SETXFER_POLLING))
-               qc->tf.flags |= ATA_TFLAG_POLLING;
-
        /* select the device */
        ata_dev_select(ap, qc->dev->devno, 1, 0);
 
@@ -5549,37 +5871,11 @@ static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  */
 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
 {
-       int i, j, rc;
+       int rc;
 
        rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
-       if (rc)
-               goto fail;
-
-       /* EH is quiescent now.  Fail if we have any ready device.
-        * This happens if hotplug occurs between completion of device
-        * suspension and here.
-        */
-       for (i = 0; i < host->n_ports; i++) {
-               struct ata_port *ap = host->ports[i];
-
-               for (j = 0; j < ATA_MAX_DEVICES; j++) {
-                       struct ata_device *dev = &ap->device[j];
-
-                       if (ata_dev_ready(dev)) {
-                               ata_port_printk(ap, KERN_WARNING,
-                                               "suspend failed, device %d "
-                                               "still active\n", dev->devno);
-                               rc = -EBUSY;
-                               goto fail;
-                       }
-               }
-       }
-
-       host->dev->power.power_state = mesg;
-       return 0;
-
- fail:
-       ata_host_resume(host);
+       if (rc == 0)
+               host->dev->power.power_state = mesg;
        return rc;
 }
 
@@ -5688,6 +5984,7 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
        if (!ap)
                return NULL;
 
+       ap->pflags |= ATA_PFLAG_INITIALIZING;
        ap->lock = &host->lock;
        ap->flags = ATA_FLAG_DISABLED;
        ap->print_id = -1;
@@ -6010,7 +6307,8 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                /* init sata_spd_limit to the current value */
                if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
                        int spd = (scontrol >> 4) & 0xf;
-                       ap->hw_sata_spd_limit &= (1 << spd) - 1;
+                       if (spd)
+                               ap->hw_sata_spd_limit &= (1 << spd) - 1;
                }
                ap->sata_spd_limit = ap->hw_sata_spd_limit;
 
@@ -6056,6 +6354,7 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                        ehi->action |= ATA_EH_SOFTRESET;
                        ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
 
+                       ap->pflags &= ~ATA_PFLAG_INITIALIZING;
                        ap->pflags |= ATA_PFLAG_LOADING;
                        ata_port_schedule_eh(ap);
 
@@ -6124,6 +6423,9 @@ int ata_host_activate(struct ata_host *host, int irq,
        if (rc)
                return rc;
 
+       /* Used to print device info at probe */
+       host->irq = irq;
+
        rc = ata_host_register(host, sht);
        /* if failed, just free the IRQ and leave ports alone */
        if (rc)
@@ -6133,131 +6435,6 @@ int ata_host_activate(struct ata_host *host, int irq,
 }
 
 /**
- *     ata_device_add - Register hardware device with ATA and SCSI layers
- *     @ent: Probe information describing hardware device to be registered
- *
- *     This function processes the information provided in the probe
- *     information struct @ent, allocates the necessary ATA and SCSI
- *     host information structures, initializes them, and registers
- *     everything with requisite kernel subsystems.
- *
- *     This function requests irqs, probes the ATA bus, and probes
- *     the SCSI bus.
- *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *
- *     RETURNS:
- *     Number of ports registered.  Zero on error (no ports registered).
- */
-int ata_device_add(const struct ata_probe_ent *ent)
-{
-       unsigned int i;
-       struct device *dev = ent->dev;
-       struct ata_host *host;
-       int rc;
-
-       DPRINTK("ENTER\n");
-
-       if (ent->irq == 0) {
-               dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
-               return 0;
-       }
-
-       if (!ent->port_ops->error_handler &&
-           !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
-               dev_printk(KERN_ERR, dev, "no reset mechanism available\n");
-               return 0;
-       }
-
-       if (!devres_open_group(dev, ata_device_add, GFP_KERNEL))
-               return 0;
-
-       /* allocate host */
-       host = ata_host_alloc(dev, ent->n_ports);
-
-       host->irq = ent->irq;
-       host->irq2 = ent->irq2;
-       host->iomap = ent->iomap;
-       host->private_data = ent->private_data;
-       host->ops = ent->port_ops;
-       host->flags = ent->_host_flags;
-
-       for (i = 0; i < host->n_ports; i++) {
-               struct ata_port *ap = host->ports[i];
-
-               /* dummy? */
-               if (ent->dummy_port_mask & (1 << i)) {
-                       ap->ops = &ata_dummy_port_ops;
-                       continue;
-               }
-
-               if (ap->port_no == 1 && ent->pinfo2) {
-                       ap->pio_mask = ent->pinfo2->pio_mask;
-                       ap->mwdma_mask = ent->pinfo2->mwdma_mask;
-                       ap->udma_mask = ent->pinfo2->udma_mask;
-                       ap->flags |= ent->pinfo2->flags;
-                       ap->ops = ent->pinfo2->port_ops;
-               } else {
-                       ap->pio_mask = ent->pio_mask;
-                       ap->mwdma_mask = ent->mwdma_mask;
-                       ap->udma_mask = ent->udma_mask;
-                       ap->flags |= ent->port_flags;
-                       ap->ops = ent->port_ops;
-               }
-
-               memcpy(&ap->ioaddr, &ent->port[ap->port_no],
-                      sizeof(struct ata_ioports));
-       }
-
-       /* start and freeze ports before requesting IRQ */
-       rc = ata_host_start(host);
-       if (rc)
-               goto err_out;
-
-       /* obtain irq, that may be shared between channels */
-       rc = devm_request_irq(dev, ent->irq, ent->port_ops->irq_handler,
-                             ent->irq_flags, DRV_NAME, host);
-       if (rc) {
-               dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
-                          ent->irq, rc);
-               goto err_out;
-       }
-
-       /* do we have a second IRQ for the other channel, eg legacy mode */
-       if (ent->irq2) {
-               /* We will get weird core code crashes later if this is true
-                  so trap it now */
-               BUG_ON(ent->irq == ent->irq2);
-
-               rc = devm_request_irq(dev, ent->irq2,
-                               ent->port_ops->irq_handler, ent->irq_flags,
-                               DRV_NAME, host);
-               if (rc) {
-                       dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
-                                  ent->irq2, rc);
-                       goto err_out;
-               }
-       }
-
-       /* resource acquisition complete */
-       devres_remove_group(dev, ata_device_add);
-
-       /* register */
-       rc = ata_host_register(host, ent->sht);
-       if (rc)
-               goto err_out;
-
-       VPRINTK("EXIT, returning %u\n", host->n_ports);
-       return host->n_ports; /* success */
-
- err_out:
-       devres_release_group(dev, ata_device_add);
-       VPRINTK("EXIT, returning 0\n");
-       return 0;
-}
-
-/**
  *     ata_port_detach - Detach ATA port in prepration of device removal
  *     @ap: ATA port to be detached
  *
@@ -6306,9 +6483,9 @@ void ata_port_detach(struct ata_port *ap)
        /* Flush hotplug task.  The sequence is similar to
         * ata_port_flush_task().
         */
-       flush_workqueue(ata_aux_wq);
+       cancel_work_sync(&ap->hotplug_task.work); /* akpm: why? */
        cancel_delayed_work(&ap->hotplug_task);
-       flush_workqueue(ata_aux_wq);
+       cancel_work_sync(&ap->hotplug_task.work);
 
  skip_eh:
        /* remove the associated SCSI host */
@@ -6332,32 +6509,6 @@ void ata_host_detach(struct ata_host *host)
                ata_port_detach(host->ports[i]);
 }
 
-struct ata_probe_ent *
-ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
-{
-       struct ata_probe_ent *probe_ent;
-
-       probe_ent = devm_kzalloc(dev, sizeof(*probe_ent), GFP_KERNEL);
-       if (!probe_ent) {
-               printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
-                      kobject_name(&(dev->kobj)));
-               return NULL;
-       }
-
-       INIT_LIST_HEAD(&probe_ent->node);
-       probe_ent->dev = dev;
-
-       probe_ent->sht = port->sht;
-       probe_ent->port_flags = port->flags;
-       probe_ent->pio_mask = port->pio_mask;
-       probe_ent->mwdma_mask = port->mwdma_mask;
-       probe_ent->udma_mask = port->udma_mask;
-       probe_ent->port_ops = port->port_ops;
-       probe_ent->private_data = port->private_data;
-
-       return probe_ent;
-}
-
 /**
  *     ata_std_ports - initialize ioaddr with standard port offsets.
  *     @ioaddr: IO address structure to be initialized
@@ -6623,6 +6774,10 @@ const struct ata_port_operations ata_dummy_port_ops = {
        .port_stop              = ata_dummy_noret,
 };
 
+const struct ata_port_info ata_dummy_port_info = {
+       .port_ops               = &ata_dummy_port_ops,
+};
+
 /*
  * libata is essentially a library of internal helper functions for
  * low-level ATA host controller drivers.  As such, the API/ABI is
@@ -6634,6 +6789,7 @@ EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
+EXPORT_SYMBOL_GPL(ata_dummy_port_info);
 EXPORT_SYMBOL_GPL(ata_std_bios_param);
 EXPORT_SYMBOL_GPL(ata_std_ports);
 EXPORT_SYMBOL_GPL(ata_host_init);
@@ -6642,7 +6798,6 @@ EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
 EXPORT_SYMBOL_GPL(ata_host_start);
 EXPORT_SYMBOL_GPL(ata_host_register);
 EXPORT_SYMBOL_GPL(ata_host_activate);
-EXPORT_SYMBOL_GPL(ata_device_add);
 EXPORT_SYMBOL_GPL(ata_host_detach);
 EXPORT_SYMBOL_GPL(ata_sg_init);
 EXPORT_SYMBOL_GPL(ata_sg_init_one);
@@ -6661,6 +6816,7 @@ EXPORT_SYMBOL_GPL(ata_check_status);
 EXPORT_SYMBOL_GPL(ata_altstatus);
 EXPORT_SYMBOL_GPL(ata_exec_command);
 EXPORT_SYMBOL_GPL(ata_port_start);
+EXPORT_SYMBOL_GPL(ata_sff_port_start);
 EXPORT_SYMBOL_GPL(ata_interrupt);
 EXPORT_SYMBOL_GPL(ata_do_set_mode);
 EXPORT_SYMBOL_GPL(ata_data_xfer);
@@ -6696,6 +6852,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_ready);
 EXPORT_SYMBOL_GPL(ata_port_queue_task);
 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
@@ -6725,8 +6882,9 @@ EXPORT_SYMBOL_GPL(ata_timing_merge);
 
 #ifdef CONFIG_PCI
 EXPORT_SYMBOL_GPL(pci_test_config_bits);
-EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
 EXPORT_SYMBOL_GPL(ata_pci_init_native_host);
+EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
+EXPORT_SYMBOL_GPL(ata_pci_prepare_native_host);
 EXPORT_SYMBOL_GPL(ata_pci_init_one);
 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
 #ifdef CONFIG_PM
@@ -6739,11 +6897,6 @@ EXPORT_SYMBOL_GPL(ata_pci_default_filter);
 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
 #endif /* CONFIG_PCI */
 
-#ifdef CONFIG_PM
-EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
-EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
-#endif /* CONFIG_PM */
-
 EXPORT_SYMBOL_GPL(ata_eng_timeout);
 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
 EXPORT_SYMBOL_GPL(ata_port_abort);