[PATCH] libata: ata_dev_init_params() fixes
[sfrench/cifs-2.6.git] / drivers / scsi / libata-core.c
index 0314abd97f2dad381847b640376a0a0f1e622fe6..21b0ed583b8a7273e5391c8d221c634d031fb578 100644 (file)
 #include "libata.h"
 
 static unsigned int ata_dev_init_params(struct ata_port *ap,
-                                       struct ata_device *dev);
+                                       struct ata_device *dev,
+                                       u16 heads,
+                                       u16 sectors);
 static void ata_set_mode(struct ata_port *ap);
-static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
-static unsigned int ata_dev_xfermask(struct ata_port *ap,
-                                    struct ata_device *dev);
+static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
+                                        struct ata_device *dev);
+static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
 
 static unsigned int ata_unique_id = 1;
 static struct workqueue_struct *ata_wq;
@@ -190,7 +192,7 @@ static const u8 ata_rw_cmds[] = {
  *     ata_rwcmd_protocol - set taskfile r/w commands and protocol
  *     @qc: command to examine and configure
  *
- *     Examine the device configuration and tf->flags to calculate 
+ *     Examine the device configuration and tf->flags to calculate
  *     the proper read/write commands and protocol to use.
  *
  *     LOCKING:
@@ -203,7 +205,7 @@ int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
        u8 cmd;
 
        int index, fua, lba48, write;
+
        fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
        lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
        write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
@@ -252,6 +254,29 @@ static unsigned int ata_pack_xfermask(unsigned int pio_mask,
                ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
 }
 
+/**
+ *     ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
+ *     @xfer_mask: xfer_mask to unpack
+ *     @pio_mask: resulting pio_mask
+ *     @mwdma_mask: resulting mwdma_mask
+ *     @udma_mask: resulting udma_mask
+ *
+ *     Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
+ *     Any NULL distination masks will be ignored.
+ */
+static void ata_unpack_xfermask(unsigned int xfer_mask,
+                               unsigned int *pio_mask,
+                               unsigned int *mwdma_mask,
+                               unsigned int *udma_mask)
+{
+       if (pio_mask)
+               *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
+       if (mwdma_mask)
+               *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
+       if (udma_mask)
+               *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
+}
+
 static const struct ata_xfer_ent {
        unsigned int shift, bits;
        u8 base;
@@ -372,6 +397,15 @@ static const char *ata_mode_string(unsigned int xfer_mask)
        return "<n/a>";
 }
 
+static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
+{
+       if (ata_dev_present(dev)) {
+               printk(KERN_WARNING "ata%u: dev %u disabled\n",
+                      ap->id, dev->devno);
+               dev->class++;
+       }
+}
+
 /**
  *     ata_pio_devchk - PATA device presence detection
  *     @ap: ATA channel to examine
@@ -987,6 +1021,22 @@ ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
 
        ata_qc_free(qc);
 
+       /* XXX - Some LLDDs (sata_mv) disable port on command failure.
+        * Until those drivers are fixed, we detect the condition
+        * here, fail the command with AC_ERR_SYSTEM and reenable the
+        * port.
+        *
+        * Note that this doesn't change any behavior as internal
+        * command failure results in disabling the device in the
+        * higher layer for LLDDs without new reset/EH callbacks.
+        *
+        * Kill the following code as soon as those drivers are fixed.
+        */
+       if (ap->flags & ATA_FLAG_PORT_DISABLED) {
+               err_mask |= AC_ERR_SYSTEM;
+               ata_port_probe(ap);
+       }
+
        return err_mask;
 }
 
@@ -1007,7 +1057,7 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
                return 0;
        if (speed > 2)
                return 1;
-               
+
        /* If we have no drive specific rule, then PIO 2 is non IORDY */
 
        if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
@@ -1033,9 +1083,8 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  *
  *     Read ID data from the specified device.  ATA_CMD_ID_ATA is
  *     performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
- *     devices.  This function also takes care of EDD signature
- *     misreporting (to be removed once EDD support is gone) and
- *     issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
+ *     devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
+ *     for pre-ATA4 drives.
  *
  *     LOCKING:
  *     Kernel thread context (may sleep)
@@ -1047,7 +1096,6 @@ static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
                           unsigned int *p_class, int post_reset, u16 **p_id)
 {
        unsigned int class = *p_class;
-       unsigned int using_edd;
        struct ata_taskfile tf;
        unsigned int err_mask = 0;
        u16 *id;
@@ -1056,12 +1104,6 @@ static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
 
        DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
 
-       if (ap->ops->probe_reset ||
-           ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
-               using_edd = 0;
-       else
-               using_edd = 1;
-
        ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
 
        id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
@@ -1091,39 +1133,16 @@ static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
 
        err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
                                     id, sizeof(id[0]) * ATA_ID_WORDS);
-
        if (err_mask) {
                rc = -EIO;
                reason = "I/O error";
-
-               if (err_mask & ~AC_ERR_DEV)
-                       goto err_out;
-
-               /*
-                * arg!  EDD works for all test cases, but seems to return
-                * the ATA signature for some ATAPI devices.  Until the
-                * reason for this is found and fixed, we fix up the mess
-                * here.  If IDENTIFY DEVICE returns command aborted
-                * (as ATAPI devices do), then we issue an
-                * IDENTIFY PACKET DEVICE.
-                *
-                * ATA software reset (SRST, the default) does not appear
-                * to have this problem.
-                */
-               if ((using_edd) && (class == ATA_DEV_ATA)) {
-                       u8 err = tf.feature;
-                       if (err & ATA_ABORTED) {
-                               class = ATA_DEV_ATAPI;
-                               goto retry;
-                       }
-               }
                goto err_out;
        }
 
        swap_buf_le16(id, ATA_ID_WORDS);
 
        /* sanity check */
-       if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
+       if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
                rc = -EINVAL;
                reason = "device reports illegal type";
                goto err_out;
@@ -1139,7 +1158,7 @@ static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
                 * Some drives were very specific about that exact sequence.
                 */
                if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
-                       err_mask = ata_dev_init_params(ap, dev);
+                       err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
                        if (err_mask) {
                                rc = -EIO;
                                reason = "INIT_DEV_PARAMS failed";
@@ -1305,7 +1324,7 @@ static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
                if (print_info)
                        printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
                               ap->id, dev->devno);
-               ap->udma_mask &= ATA_UDMA5;
+               dev->udma_mask &= ATA_UDMA5;
                dev->max_sectors = ATA_MAX_SECTORS;
        }
 
@@ -1316,8 +1335,6 @@ static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
        return 0;
 
 err_out_nosup:
-       printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
-              ap->id, dev->devno);
        DPRINTK("EXIT, err\n");
        return rc;
 }
@@ -1384,7 +1401,7 @@ static int ata_bus_probe(struct ata_port *ap)
                }
 
                if (ata_dev_configure(ap, dev, 1)) {
-                       dev->class++;   /* disable device */
+                       ata_dev_disable(ap, dev);
                        continue;
                }
 
@@ -1394,7 +1411,11 @@ static int ata_bus_probe(struct ata_port *ap)
        if (!found)
                goto err_out_disable;
 
-       ata_set_mode(ap);
+       if (ap->ops->set_mode)
+               ap->ops->set_mode(ap);
+       else
+               ata_set_mode(ap);
+
        if (ap->flags & ATA_FLAG_PORT_DISABLED)
                goto err_out_disable;
 
@@ -1529,6 +1550,23 @@ void sata_phy_reset(struct ata_port *ap)
        ata_bus_reset(ap);
 }
 
+/**
+ *     ata_dev_pair            -       return other device on cable
+ *     @ap: port
+ *     @adev: device
+ *
+ *     Obtain the other device on the same cable, or if none is
+ *     present NULL is returned
+ */
+
+struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
+{
+       struct ata_device *pair = &ap->device[1 - adev->devno];
+       if (!ata_dev_present(pair))
+               return NULL;
+       return pair;
+}
+
 /**
  *     ata_port_disable - Disable port.
  *     @ap: Port to be disabled.
@@ -1557,7 +1595,7 @@ void ata_port_disable(struct ata_port *ap)
  * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  * for PIO 5, which is a nonstandard extension and UDMA6, which
- * is currently supported only by Maxtor drives. 
+ * is currently supported only by Maxtor drives.
  */
 
 static const struct ata_timing ata_timing[] = {
@@ -1572,11 +1610,11 @@ static const struct ata_timing ata_timing[] = {
        { XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
 
 /*     { XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
-                                          
+
        { XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
        { XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
        { XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
-                                          
+
        { XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
        { XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
        { XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
@@ -1629,7 +1667,7 @@ static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
        for (t = ata_timing; t->mode != speed; t++)
                if (t->mode == 0xFF)
                        return NULL;
-       return t; 
+       return t;
 }
 
 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
@@ -1639,7 +1677,7 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
        struct ata_timing p;
 
        /*
-        * Find the mode. 
+        * Find the mode.
         */
 
        if (!(s = ata_timing_find_mode(speed)))
@@ -1697,20 +1735,28 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
        return 0;
 }
 
-static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
+static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
 {
-       if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
-               return;
+       unsigned int err_mask;
+       int rc;
 
        if (dev->xfer_shift == ATA_SHIFT_PIO)
                dev->flags |= ATA_DFLAG_PIO;
 
-       ata_dev_set_xfermode(ap, dev);
+       err_mask = ata_dev_set_xfermode(ap, dev);
+       if (err_mask) {
+               printk(KERN_ERR
+                      "ata%u: failed to set xfermode (err_mask=0x%x)\n",
+                      ap->id, err_mask);
+               return -EIO;
+       }
 
-       if (ata_dev_revalidate(ap, dev, 0)) {
-               printk(KERN_ERR "ata%u: failed to revalidate after set "
-                      "xfermode, disabled\n", ap->id);
-               ata_port_disable(ap);
+       rc = ata_dev_revalidate(ap, dev, 0);
+       if (rc) {
+               printk(KERN_ERR
+                      "ata%u: failed to revalidate after set xfermode\n",
+                      ap->id);
+               return rc;
        }
 
        DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
@@ -1719,6 +1765,7 @@ static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
        printk(KERN_INFO "ata%u: dev %u configured for %s\n",
               ap->id, dev->devno,
               ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
+       return 0;
 }
 
 static int ata_host_set_pio(struct ata_port *ap)
@@ -1773,21 +1820,27 @@ static void ata_host_set_dma(struct ata_port *ap)
  */
 static void ata_set_mode(struct ata_port *ap)
 {
-       int i, rc;
+       int i, rc, used_dma = 0;
 
        /* step 1: calculate xfer_mask */
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
                struct ata_device *dev = &ap->device[i];
-               unsigned int xfer_mask;
+               unsigned int pio_mask, dma_mask;
 
                if (!ata_dev_present(dev))
                        continue;
 
-               xfer_mask = ata_dev_xfermask(ap, dev);
+               ata_dev_xfermask(ap, dev);
 
-               dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
-               dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
-                                                               ATA_MASK_UDMA));
+               /* TODO: let LLDD filter dev->*_mask here */
+
+               pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
+               dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+               dev->pio_mode = ata_xfer_mask2mode(pio_mask);
+               dev->dma_mode = ata_xfer_mask2mode(dma_mask);
+
+               if (dev->dma_mode)
+                       used_dma = 1;
        }
 
        /* step 2: always set host PIO timings */
@@ -1799,12 +1852,27 @@ static void ata_set_mode(struct ata_port *ap)
        ata_host_set_dma(ap);
 
        /* step 4: update devices' xfer mode */
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               ata_dev_set_mode(ap, &ap->device[i]);
+       for (i = 0; i < ATA_MAX_DEVICES; i++) {
+               struct ata_device *dev = &ap->device[i];
 
-       if (ap->flags & ATA_FLAG_PORT_DISABLED)
-               return;
+               if (!ata_dev_present(dev))
+                       continue;
 
+               if (ata_dev_set_mode(ap, dev))
+                       goto err_out;
+       }
+
+       /*
+        *      Record simplex status. If we selected DMA then the other
+        *      host channels are not permitted to do so.
+        */
+
+       if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
+               ap->host_set->simplex_claimed = 1;
+
+       /*
+        *      Chip specific finalisation
+        */
        if (ap->ops->post_set_mode)
                ap->ops->post_set_mode(ap);
 
@@ -1926,45 +1994,6 @@ static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
                ap->ops->dev_select(ap, 0);
 }
 
-/**
- *     ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
- *     @ap: Port to reset and probe
- *
- *     Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
- *     probe the bus.  Not often used these days.
- *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *     Obtains host_set lock.
- *
- */
-
-static unsigned int ata_bus_edd(struct ata_port *ap)
-{
-       struct ata_taskfile tf;
-       unsigned long flags;
-
-       /* set up execute-device-diag (bus reset) taskfile */
-       /* also, take interrupts to a known state (disabled) */
-       DPRINTK("execute-device-diag\n");
-       ata_tf_init(ap, &tf, 0);
-       tf.ctl |= ATA_NIEN;
-       tf.command = ATA_CMD_EDD;
-       tf.protocol = ATA_PROT_NODATA;
-
-       /* do bus reset */
-       spin_lock_irqsave(&ap->host_set->lock, flags);
-       ata_tf_to_host(ap, &tf);
-       spin_unlock_irqrestore(&ap->host_set->lock, flags);
-
-       /* spec says at least 2ms.  but who knows with those
-        * crazy ATAPI devices...
-        */
-       msleep(150);
-
-       return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
-}
-
 static unsigned int ata_bus_softreset(struct ata_port *ap,
                                      unsigned int devmask)
 {
@@ -1999,13 +2028,12 @@ static unsigned int ata_bus_softreset(struct ata_port *ap,
         */
        msleep(150);
 
-       
-       /* Before we perform post reset processing we want to see if 
-          the bus shows 0xFF because the odd clown forgets the D7 pulldown
-          resistor */
-       
+       /* 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)
-               return 1;       /* Positive is failure for some reason */
+               return AC_ERR_OTHER;
 
        ata_bus_post_reset(ap, devmask);
 
@@ -2037,7 +2065,7 @@ void ata_bus_reset(struct ata_port *ap)
        struct ata_ioports *ioaddr = &ap->ioaddr;
        unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
        u8 err;
-       unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
+       unsigned int dev0, dev1 = 0, devmask = 0;
 
        DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
 
@@ -2060,18 +2088,8 @@ void ata_bus_reset(struct ata_port *ap)
 
        /* issue bus reset */
        if (ap->flags & ATA_FLAG_SRST)
-               rc = ata_bus_softreset(ap, devmask);
-       else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
-               /* set up device control */
-               if (ap->flags & ATA_FLAG_MMIO)
-                       writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
-               else
-                       outb(ap->ctl, ioaddr->ctl_addr);
-               rc = ata_bus_edd(ap);
-       }
-
-       if (rc)
-               goto err_out;
+               if (ata_bus_softreset(ap, devmask))
+                       goto err_out;
 
        /*
         * determine by signature whether we have ATA or ATAPI devices
@@ -2144,9 +2162,9 @@ static int sata_phy_resume(struct ata_port *ap)
  *     so makes reset sequence different from the original
  *     ->phy_reset implementation and Jeff nervous.  :-P
  */
-extern void ata_std_probeinit(struct ata_port *ap)
+void ata_std_probeinit(struct ata_port *ap)
 {
-       if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
+       if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
                sata_phy_resume(ap);
                if (sata_dev_present(ap))
                        ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
@@ -2572,22 +2590,22 @@ static const char * const ata_dma_blacklist [] = {
        "SanDisk SDP3B-64", NULL,
        "SANYO CD-ROM CRD", NULL,
        "HITACHI CDR-8", NULL,
-       "HITACHI CDR-8335", NULL, 
+       "HITACHI CDR-8335", NULL,
        "HITACHI CDR-8435", NULL,
-       "Toshiba CD-ROM XM-6202B", NULL, 
-       "TOSHIBA CD-ROM XM-1702BC", NULL, 
-       "CD-532E-A", NULL, 
-       "E-IDE CD-ROM CR-840", NULL, 
-       "CD-ROM Drive/F5A", NULL, 
-       "WPI CDD-820", NULL, 
+       "Toshiba CD-ROM XM-6202B", NULL,
+       "TOSHIBA CD-ROM XM-1702BC", NULL,
+       "CD-532E-A", NULL,
+       "E-IDE CD-ROM CR-840", NULL,
+       "CD-ROM Drive/F5A", NULL,
+       "WPI CDD-820", NULL,
        "SAMSUNG CD-ROM SC-148C", NULL,
-       "SAMSUNG CD-ROM SC", NULL, 
+       "SAMSUNG CD-ROM SC", NULL,
        "SanDisk SDP3B-64", NULL,
        "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
        "_NEC DV5800A", NULL,
        "SAMSUNG CD-ROM SN-124", "N001"
 };
+
 static int ata_strim(char *s, size_t len)
 {
        len = strnlen(s, len);
@@ -2630,40 +2648,56 @@ static int ata_dma_blacklisted(const struct ata_device *dev)
  *     @ap: Port on which the device to compute xfermask for resides
  *     @dev: Device to compute xfermask for
  *
- *     Compute supported xfermask of @dev.  This function is
- *     responsible for applying all known limits including host
- *     controller limits, device blacklist, etc...
+ *     Compute supported xfermask of @dev and store it in
+ *     dev->*_mask.  This function is responsible for applying all
+ *     known limits including host controller limits, device
+ *     blacklist, etc...
+ *
+ *     FIXME: The current implementation limits all transfer modes to
+ *     the fastest of the lowested device on the port.  This is not
+ *     required on most controllers.
  *
  *     LOCKING:
  *     None.
- *
- *     RETURNS:
- *     Computed xfermask.
  */
-static unsigned int ata_dev_xfermask(struct ata_port *ap,
-                                    struct ata_device *dev)
+static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
 {
+       struct ata_host_set *hs = ap->host_set;
        unsigned long xfer_mask;
        int i;
 
        xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
                                      ap->udma_mask);
 
-       /* use port-wide xfermask for now */
+       /* FIXME: Use port-wide xfermask for now */
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
                struct ata_device *d = &ap->device[i];
                if (!ata_dev_present(d))
                        continue;
+               xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
+                                              d->udma_mask);
                xfer_mask &= ata_id_xfermask(d->id);
                if (ata_dma_blacklisted(d))
                        xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
+               /* Apply cable rule here. Don't apply it early because when
+                  we handle hot plug the cable type can itself change */
+               if (ap->cbl == ATA_CBL_PATA40)
+                       xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
        }
 
        if (ata_dma_blacklisted(dev))
                printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
                       "disabling DMA\n", ap->id, dev->devno);
 
-       return xfer_mask;
+       if (hs->flags & ATA_HOST_SIMPLEX) {
+               if (hs->simplex_claimed)
+                       xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
+       }
+       if (ap->ops->mode_filter)
+               xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
+
+       ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
+                           &dev->udma_mask);
 }
 
 /**
@@ -2676,11 +2710,16 @@ static unsigned int ata_dev_xfermask(struct ata_port *ap,
  *
  *     LOCKING:
  *     PCI/etc. bus probe sem.
+ *
+ *     RETURNS:
+ *     0 on success, AC_ERR_* mask otherwise.
  */
 
-static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
+static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
+                                        struct ata_device *dev)
 {
        struct ata_taskfile tf;
+       unsigned int err_mask;
 
        /* set up set-features taskfile */
        DPRINTK("set features - xfer mode\n");
@@ -2692,13 +2731,10 @@ static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = dev->xfer_mode;
 
-       if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
-               printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
-                      ap->id);
-               ata_port_disable(ap);
-       }
+       err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
 
-       DPRINTK("EXIT\n");
+       DPRINTK("EXIT, err_mask=%x\n", err_mask);
+       return err_mask;
 }
 
 /**
@@ -2714,16 +2750,16 @@ static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
  */
 
 static unsigned int ata_dev_init_params(struct ata_port *ap,
-                                       struct ata_device *dev)
+                                       struct ata_device *dev,
+                                       u16 heads,
+                                       u16 sectors)
 {
        struct ata_taskfile tf;
        unsigned int err_mask;
-       u16 sectors = dev->id[6];
-       u16 heads   = dev->id[3];
 
        /* Number of sectors per track 1-255. Number of heads 1-16 */
        if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
-               return 0;
+               return AC_ERR_INVALID;
 
        /* set up init dev params taskfile */
        DPRINTK("init dev params \n");
@@ -2775,7 +2811,7 @@ static void ata_sg_clean(struct ata_queued_cmd *qc)
 
        if (qc->flags & ATA_QCFLAG_SG) {
                if (qc->n_elem)
-                       dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
+                       dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
                /* restore last sg */
                sg[qc->orig_n_elem - 1].length += qc->pad_len;
                if (pad_buf) {
@@ -2786,7 +2822,7 @@ static void ata_sg_clean(struct ata_queued_cmd *qc)
                }
        } else {
                if (qc->n_elem)
-                       dma_unmap_single(ap->host_set->dev,
+                       dma_unmap_single(ap->dev,
                                sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
                                dir);
                /* restore sg */
@@ -2997,7 +3033,7 @@ static int ata_sg_setup_one(struct ata_queued_cmd *qc)
                goto skip_map;
        }
 
-       dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
+       dma_address = dma_map_single(ap->dev, qc->buf_virt,
                                     sg->length, dir);
        if (dma_mapping_error(dma_address)) {
                /* restore sg */
@@ -3085,7 +3121,7 @@ static int ata_sg_setup(struct ata_queued_cmd *qc)
        }
 
        dir = qc->dma_dir;
-       n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
+       n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
        if (n_elem < 1) {
                /* restore last sg */
                lsg->length += qc->pad_len;
@@ -3616,7 +3652,7 @@ static void ata_pio_error(struct ata_port *ap)
        if (qc->tf.command != ATA_CMD_PACKET)
                printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
 
-       /* make sure qc->err_mask is available to 
+       /* make sure qc->err_mask is available to
         * know what's wrong and recover
         */
        WARN_ON(qc->err_mask == 0);
@@ -4064,240 +4100,6 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
        return 0;
 }
 
-/**
- *     ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
-       u8 dmactl;
-       void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
-
-       /* load PRD table addr. */
-       mb();   /* make sure PRD table writes are visible to controller */
-       writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
-
-       /* specify data direction, triple-check start bit is clear */
-       dmactl = readb(mmio + ATA_DMA_CMD);
-       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
-       if (!rw)
-               dmactl |= ATA_DMA_WR;
-       writeb(dmactl, mmio + ATA_DMA_CMD);
-
-       /* issue r/w command */
-       ap->ops->exec_command(ap, &qc->tf);
-}
-
-/**
- *     ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
-       u8 dmactl;
-
-       /* start host DMA transaction */
-       dmactl = readb(mmio + ATA_DMA_CMD);
-       writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
-
-       /* Strictly, one may wish to issue a readb() here, to
-        * flush the mmio write.  However, control also passes
-        * to the hardware at this point, and it will interrupt
-        * us when we are to resume control.  So, in effect,
-        * we don't care when the mmio write flushes.
-        * Further, a read of the DMA status register _immediately_
-        * following the write may not be what certain flaky hardware
-        * is expected, so I think it is best to not add a readb()
-        * without first all the MMIO ATA cards/mobos.
-        * Or maybe I'm just being paranoid.
-        */
-}
-
-/**
- *     ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
- *     @qc: Info associated with this ATA transaction.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
-       u8 dmactl;
-
-       /* load PRD table addr. */
-       outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
-
-       /* specify data direction, triple-check start bit is clear */
-       dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
-       if (!rw)
-               dmactl |= ATA_DMA_WR;
-       outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-
-       /* issue r/w command */
-       ap->ops->exec_command(ap, &qc->tf);
-}
-
-/**
- *     ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
- *     @qc: Info associated with this ATA transaction.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       u8 dmactl;
-
-       /* start host DMA transaction */
-       dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       outb(dmactl | ATA_DMA_START,
-            ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-}
-
-
-/**
- *     ata_bmdma_start - Start a PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
- *
- *     Writes the ATA_DMA_START flag to the DMA command register.
- *
- *     May be used as the bmdma_start() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-void ata_bmdma_start(struct ata_queued_cmd *qc)
-{
-       if (qc->ap->flags & ATA_FLAG_MMIO)
-               ata_bmdma_start_mmio(qc);
-       else
-               ata_bmdma_start_pio(qc);
-}
-
-
-/**
- *     ata_bmdma_setup - Set up PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
- *
- *     Writes address of PRD table to device's PRD Table Address
- *     register, sets the DMA control register, and calls
- *     ops->exec_command() to start the transfer.
- *
- *     May be used as the bmdma_setup() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-void ata_bmdma_setup(struct ata_queued_cmd *qc)
-{
-       if (qc->ap->flags & ATA_FLAG_MMIO)
-               ata_bmdma_setup_mmio(qc);
-       else
-               ata_bmdma_setup_pio(qc);
-}
-
-
-/**
- *     ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
- *     @ap: Port associated with this ATA transaction.
- *
- *     Clear interrupt and error flags in DMA status register.
- *
- *     May be used as the irq_clear() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-void ata_bmdma_irq_clear(struct ata_port *ap)
-{
-       if (!ap->ioaddr.bmdma_addr)
-               return;
-
-       if (ap->flags & ATA_FLAG_MMIO) {
-               void __iomem *mmio =
-                     ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
-               writeb(readb(mmio), mmio);
-       } else {
-               unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
-               outb(inb(addr), addr);
-       }
-}
-
-
-/**
- *     ata_bmdma_status - Read PCI IDE BMDMA status
- *     @ap: Port associated with this ATA transaction.
- *
- *     Read and return BMDMA status register.
- *
- *     May be used as the bmdma_status() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-u8 ata_bmdma_status(struct ata_port *ap)
-{
-       u8 host_stat;
-       if (ap->flags & ATA_FLAG_MMIO) {
-               void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
-               host_stat = readb(mmio + ATA_DMA_STATUS);
-       } else
-               host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
-       return host_stat;
-}
-
-
-/**
- *     ata_bmdma_stop - Stop PCI IDE BMDMA transfer
- *     @qc: Command we are ending DMA for
- *
- *     Clears the ATA_DMA_START flag in the dma control register
- *
- *     May be used as the bmdma_stop() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host_set lock)
- */
-
-void ata_bmdma_stop(struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       if (ap->flags & ATA_FLAG_MMIO) {
-               void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
-
-               /* clear start/stop bit */
-               writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
-                       mmio + ATA_DMA_CMD);
-       } else {
-               /* clear start/stop bit */
-               outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
-                       ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       }
-
-       /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
-       ata_altstatus(ap);        /* dummy read */
-}
-
 /**
  *     ata_host_intr - Handle host interrupt for given (port, task)
  *     @ap: Port on which interrupt arrived (possibly...)
@@ -4506,14 +4308,15 @@ int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
  *     Flush the cache on the drive, if appropriate, then issue a
  *     standbynow command.
  */
-int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
+int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
 {
        if (!ata_dev_present(dev))
                return 0;
        if (dev->class == ATA_DEV_ATA)
                ata_flush_cache(ap, dev);
 
-       ata_standby_drive(ap, dev);
+       if (state.event != PM_EVENT_FREEZE)
+               ata_standby_drive(ap, dev);
        ap->flags |= ATA_FLAG_SUSPENDED;
        return 0;
 }
@@ -4533,7 +4336,7 @@ int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
 
 int ata_port_start (struct ata_port *ap)
 {
-       struct device *dev = ap->host_set->dev;
+       struct device *dev = ap->dev;
        int rc;
 
        ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
@@ -4566,7 +4369,7 @@ int ata_port_start (struct ata_port *ap)
 
 void ata_port_stop (struct ata_port *ap)
 {
-       struct device *dev = ap->host_set->dev;
+       struct device *dev = ap->dev;
 
        dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
        ata_pad_free(ap, dev);
@@ -4632,6 +4435,7 @@ static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
        ap->host = host;
        ap->ctl = ATA_DEVCTL_OBS;
        ap->host_set = host_set;
+       ap->dev = ent->dev;
        ap->port_no = port_no;
        ap->hard_port_no =
                ent->legacy_mode ? ent->hard_port_no : port_no;
@@ -4647,8 +4451,13 @@ static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
        INIT_WORK(&ap->port_task, NULL, NULL);
        INIT_LIST_HEAD(&ap->eh_done_q);
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               ap->device[i].devno = i;
+       for (i = 0; i < ATA_MAX_DEVICES; i++) {
+               struct ata_device *dev = &ap->device[i];
+               dev->devno = i;
+               dev->pio_mask = UINT_MAX;
+               dev->mwdma_mask = UINT_MAX;
+               dev->udma_mask = UINT_MAX;
+       }
 
 #ifdef ATA_IRQ_TRAP
        ap->stats.unhandled_irq = 1;
@@ -4682,6 +4491,14 @@ static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
        int rc;
 
        DPRINTK("ENTER\n");
+
+       if (!ent->port_ops->probe_reset &&
+           !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
+               printk(KERN_ERR "ata%u: no reset mechanism available\n",
+                      port_no);
+               return NULL;
+       }
+
        host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
        if (!host)
                return NULL;
@@ -4742,6 +4559,7 @@ int ata_device_add(const struct ata_probe_ent *ent)
        host_set->mmio_base = ent->mmio_base;
        host_set->private_data = ent->private_data;
        host_set->ops = ent->port_ops;
+       host_set->flags = ent->host_set_flags;
 
        /* register each port bound to this device */
        for (i = 0; i < ent->n_ports; i++) {
@@ -4842,7 +4660,7 @@ err_free_ret:
  *     ata_host_set_remove - PCI layer callback for device removal
  *     @host_set: ATA host set that was removed
  *
- *     Unregister all objects associated with this host set. Free those 
+ *     Unregister all objects associated with this host set. Free those
  *     objects.
  *
  *     LOCKING:
@@ -5114,6 +4932,8 @@ EXPORT_SYMBOL_GPL(ata_std_postreset);
 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
+EXPORT_SYMBOL_GPL(ata_dev_classify);
+EXPORT_SYMBOL_GPL(ata_dev_pair);
 EXPORT_SYMBOL_GPL(ata_port_disable);
 EXPORT_SYMBOL_GPL(ata_ratelimit);
 EXPORT_SYMBOL_GPL(ata_busy_sleep);
@@ -5124,7 +4944,6 @@ EXPORT_SYMBOL_GPL(ata_scsi_error);
 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
 EXPORT_SYMBOL_GPL(ata_scsi_release);
 EXPORT_SYMBOL_GPL(ata_host_intr);
-EXPORT_SYMBOL_GPL(ata_dev_classify);
 EXPORT_SYMBOL_GPL(ata_id_string);
 EXPORT_SYMBOL_GPL(ata_id_c_string);
 EXPORT_SYMBOL_GPL(ata_scsi_simulate);