[PATCH] OSS: replace kmalloc()+memset() combos with kzalloc()
[safe/jmp/linux-2.6] / drivers / ata / libata-core.c
index 396493c..25d8d3f 100644 (file)
@@ -59,6 +59,9 @@
 
 #include "libata.h"
 
+#define DRV_VERSION    "2.10"  /* must be exactly four chars */
+
+
 /* debounce timing parameters in msecs { interval, duration, timeout } */
 const unsigned long sata_deb_timing_normal[]           = {   5,  100, 2000 };
 const unsigned long sata_deb_timing_hotplug[]          = {  25,  500, 2000 };
@@ -199,7 +202,8 @@ static const u8 ata_rw_cmds[] = {
 
 /**
  *     ata_rwcmd_protocol - set taskfile r/w commands and protocol
- *     @qc: command to examine and configure
+ *     @tf: command to examine and configure
+ *     @dev: device tf belongs to
  *
  *     Examine the device configuration and tf->flags to calculate
  *     the proper read/write commands and protocol to use.
@@ -207,10 +211,8 @@ static const u8 ata_rw_cmds[] = {
  *     LOCKING:
  *     caller.
  */
-int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
+static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
 {
-       struct ata_taskfile *tf = &qc->tf;
-       struct ata_device *dev = qc->dev;
        u8 cmd;
 
        int index, fua, lba48, write;
@@ -222,7 +224,7 @@ int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
        if (dev->flags & ATA_DFLAG_PIO) {
                tf->protocol = ATA_PROT_PIO;
                index = dev->multi_count ? 0 : 8;
-       } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
+       } else if (lba48 && (dev->ap->flags & ATA_FLAG_PIO_LBA48)) {
                /* Unable to use DMA due to host limitation */
                tf->protocol = ATA_PROT_PIO;
                index = dev->multi_count ? 0 : 8;
@@ -240,6 +242,174 @@ int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
 }
 
 /**
+ *     ata_tf_read_block - Read block address from ATA taskfile
+ *     @tf: ATA taskfile of interest
+ *     @dev: ATA device @tf belongs to
+ *
+ *     LOCKING:
+ *     None.
+ *
+ *     Read block address from @tf.  This function can handle all
+ *     three address formats - LBA, LBA48 and CHS.  tf->protocol and
+ *     flags select the address format to use.
+ *
+ *     RETURNS:
+ *     Block address read from @tf.
+ */
+u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
+{
+       u64 block = 0;
+
+       if (tf->flags & ATA_TFLAG_LBA) {
+               if (tf->flags & ATA_TFLAG_LBA48) {
+                       block |= (u64)tf->hob_lbah << 40;
+                       block |= (u64)tf->hob_lbam << 32;
+                       block |= tf->hob_lbal << 24;
+               } else
+                       block |= (tf->device & 0xf) << 24;
+
+               block |= tf->lbah << 16;
+               block |= tf->lbam << 8;
+               block |= tf->lbal;
+       } else {
+               u32 cyl, head, sect;
+
+               cyl = tf->lbam | (tf->lbah << 8);
+               head = tf->device & 0xf;
+               sect = tf->lbal;
+
+               block = (cyl * dev->heads + head) * dev->sectors + sect;
+       }
+
+       return block;
+}
+
+/**
+ *     ata_build_rw_tf - Build ATA taskfile for given read/write request
+ *     @tf: Target ATA taskfile
+ *     @dev: ATA device @tf belongs to
+ *     @block: Block address
+ *     @n_block: Number of blocks
+ *     @tf_flags: RW/FUA etc...
+ *     @tag: tag
+ *
+ *     LOCKING:
+ *     None.
+ *
+ *     Build ATA taskfile @tf for read/write request described by
+ *     @block, @n_block, @tf_flags and @tag on @dev.
+ *
+ *     RETURNS:
+ *
+ *     0 on success, -ERANGE if the request is too large for @dev,
+ *     -EINVAL if the request is invalid.
+ */
+int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
+                   u64 block, u32 n_block, unsigned int tf_flags,
+                   unsigned int tag)
+{
+       tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
+       tf->flags |= tf_flags;
+
+       if ((dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ_OFF |
+                          ATA_DFLAG_NCQ)) == ATA_DFLAG_NCQ &&
+           likely(tag != ATA_TAG_INTERNAL)) {
+               /* yay, NCQ */
+               if (!lba_48_ok(block, n_block))
+                       return -ERANGE;
+
+               tf->protocol = ATA_PROT_NCQ;
+               tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
+
+               if (tf->flags & ATA_TFLAG_WRITE)
+                       tf->command = ATA_CMD_FPDMA_WRITE;
+               else
+                       tf->command = ATA_CMD_FPDMA_READ;
+
+               tf->nsect = tag << 3;
+               tf->hob_feature = (n_block >> 8) & 0xff;
+               tf->feature = n_block & 0xff;
+
+               tf->hob_lbah = (block >> 40) & 0xff;
+               tf->hob_lbam = (block >> 32) & 0xff;
+               tf->hob_lbal = (block >> 24) & 0xff;
+               tf->lbah = (block >> 16) & 0xff;
+               tf->lbam = (block >> 8) & 0xff;
+               tf->lbal = block & 0xff;
+
+               tf->device = 1 << 6;
+               if (tf->flags & ATA_TFLAG_FUA)
+                       tf->device |= 1 << 7;
+       } else if (dev->flags & ATA_DFLAG_LBA) {
+               tf->flags |= ATA_TFLAG_LBA;
+
+               if (lba_28_ok(block, n_block)) {
+                       /* use LBA28 */
+                       tf->device |= (block >> 24) & 0xf;
+               } else if (lba_48_ok(block, n_block)) {
+                       if (!(dev->flags & ATA_DFLAG_LBA48))
+                               return -ERANGE;
+
+                       /* use LBA48 */
+                       tf->flags |= ATA_TFLAG_LBA48;
+
+                       tf->hob_nsect = (n_block >> 8) & 0xff;
+
+                       tf->hob_lbah = (block >> 40) & 0xff;
+                       tf->hob_lbam = (block >> 32) & 0xff;
+                       tf->hob_lbal = (block >> 24) & 0xff;
+               } else
+                       /* request too large even for LBA48 */
+                       return -ERANGE;
+
+               if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
+                       return -EINVAL;
+
+               tf->nsect = n_block & 0xff;
+
+               tf->lbah = (block >> 16) & 0xff;
+               tf->lbam = (block >> 8) & 0xff;
+               tf->lbal = block & 0xff;
+
+               tf->device |= ATA_LBA;
+       } else {
+               /* CHS */
+               u32 sect, head, cyl, track;
+
+               /* The request -may- be too large for CHS addressing. */
+               if (!lba_28_ok(block, n_block))
+                       return -ERANGE;
+
+               if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
+                       return -EINVAL;
+
+               /* Convert LBA to CHS */
+               track = (u32)block / dev->sectors;
+               cyl   = track / dev->heads;
+               head  = track % dev->heads;
+               sect  = (u32)block % dev->sectors + 1;
+
+               DPRINTK("block %u track %u cyl %u head %u sect %u\n",
+                       (u32)block, track, cyl, head, sect);
+
+               /* Check whether the converted CHS can fit.
+                  Cylinder: 0-65535
+                  Head: 0-15
+                  Sector: 1-255*/
+               if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
+                       return -ERANGE;
+
+               tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
+               tf->lbal = sect;
+               tf->lbam = cyl;
+               tf->lbah = cyl >> 8;
+               tf->device |= head;
+       }
+
+       return 0;
+}
+
+/**
  *     ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  *     @pio_mask: pio_mask
  *     @mwdma_mask: mwdma_mask
@@ -431,51 +601,7 @@ void ata_dev_disable(struct ata_device *dev)
 }
 
 /**
- *     ata_pio_devchk - PATA device presence detection
- *     @ap: ATA channel to examine
- *     @device: Device to examine (starting at zero)
- *
- *     This technique was originally described in
- *     Hale Landis's ATADRVR (www.ata-atapi.com), and
- *     later found its way into the ATA/ATAPI spec.
- *
- *     Write a pattern to the ATA shadow registers,
- *     and if a device is present, it will respond by
- *     correctly storing and echoing back the
- *     ATA shadow register contents.
- *
- *     LOCKING:
- *     caller.
- */
-
-static unsigned int ata_pio_devchk(struct ata_port *ap,
-                                  unsigned int device)
-{
-       struct ata_ioports *ioaddr = &ap->ioaddr;
-       u8 nsect, lbal;
-
-       ap->ops->dev_select(ap, device);
-
-       outb(0x55, ioaddr->nsect_addr);
-       outb(0xaa, ioaddr->lbal_addr);
-
-       outb(0xaa, ioaddr->nsect_addr);
-       outb(0x55, ioaddr->lbal_addr);
-
-       outb(0x55, ioaddr->nsect_addr);
-       outb(0xaa, ioaddr->lbal_addr);
-
-       nsect = inb(ioaddr->nsect_addr);
-       lbal = inb(ioaddr->lbal_addr);
-
-       if ((nsect == 0x55) && (lbal == 0xaa))
-               return 1;       /* we found a device */
-
-       return 0;               /* nothing found */
-}
-
-/**
- *     ata_mmio_devchk - PATA device presence detection
+ *     ata_devchk - PATA device presence detection
  *     @ap: ATA channel to examine
  *     @device: Device to examine (starting at zero)
  *
@@ -492,25 +618,24 @@ static unsigned int ata_pio_devchk(struct ata_port *ap,
  *     caller.
  */
 
-static unsigned int ata_mmio_devchk(struct ata_port *ap,
-                                   unsigned int device)
+static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
        u8 nsect, lbal;
 
        ap->ops->dev_select(ap, device);
 
-       writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
-       writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
+       iowrite8(0x55, ioaddr->nsect_addr);
+       iowrite8(0xaa, ioaddr->lbal_addr);
 
-       writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
-       writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
+       iowrite8(0xaa, ioaddr->nsect_addr);
+       iowrite8(0x55, ioaddr->lbal_addr);
 
-       writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
-       writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
+       iowrite8(0x55, ioaddr->nsect_addr);
+       iowrite8(0xaa, ioaddr->lbal_addr);
 
-       nsect = readb((void __iomem *) ioaddr->nsect_addr);
-       lbal = readb((void __iomem *) ioaddr->lbal_addr);
+       nsect = ioread8(ioaddr->nsect_addr);
+       lbal = ioread8(ioaddr->lbal_addr);
 
        if ((nsect == 0x55) && (lbal == 0xaa))
                return 1;       /* we found a device */
@@ -519,27 +644,6 @@ static unsigned int ata_mmio_devchk(struct ata_port *ap,
 }
 
 /**
- *     ata_devchk - PATA device presence detection
- *     @ap: ATA channel to examine
- *     @device: Device to examine (starting at zero)
- *
- *     Dispatch ATA device presence detection, depending
- *     on whether we are using PIO or MMIO to talk to the
- *     ATA shadow registers.
- *
- *     LOCKING:
- *     caller.
- */
-
-static unsigned int ata_devchk(struct ata_port *ap,
-                                   unsigned int device)
-{
-       if (ap->flags & ATA_FLAG_MMIO)
-               return ata_mmio_devchk(ap, device);
-       return ata_pio_devchk(ap, device);
-}
-
-/**
  *     ata_dev_classify - determine device type based on ATA-spec signature
  *     @tf: ATA taskfile register set for device to be identified
  *
@@ -756,11 +860,7 @@ void ata_std_dev_select (struct ata_port *ap, unsigned int device)
        else
                tmp = ATA_DEVICE_OBS | ATA_DEV1;
 
-       if (ap->flags & ATA_FLAG_MMIO) {
-               writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
-       } else {
-               outb(tmp, ap->ioaddr.device_addr);
-       }
+       iowrite8(tmp, ap->ioaddr.device_addr);
        ata_pause(ap);          /* needed; also flushes, for mmio */
 }
 
@@ -870,7 +970,11 @@ static unsigned int ata_id_xfermask(const u16 *id)
                 * the PIO timing number for the maximum. Turn it into
                 * a mask.
                 */
-               pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
+               u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
+               if (mode < 5)   /* Valid PIO range */
+                       pio_mask = (2 << mode) - 1;
+               else
+                       pio_mask = 1;
 
                /* But wait.. there's more. Design your standards by
                 * committee and you too can get a free iordy field to
@@ -910,7 +1014,7 @@ static unsigned int ata_id_xfermask(const u16 *id)
  *     ata_port_queue_task - Queue port_task
  *     @ap: The ata_port to queue port_task for
  *     @fn: workqueue function to be scheduled
- *     @data: data value to pass to workqueue function
+ *     @data: data for @fn to use
  *     @delay: delay time for workqueue function
  *
  *     Schedule @fn(@data) for execution after @delay jiffies using
@@ -925,7 +1029,7 @@ static unsigned int ata_id_xfermask(const u16 *id)
  *     LOCKING:
  *     Inherited from caller.
  */
-void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
+void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
                         unsigned long delay)
 {
        int rc;
@@ -933,12 +1037,10 @@ void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
        if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
                return;
 
-       PREPARE_WORK(&ap->port_task, fn, data);
+       PREPARE_DELAYED_WORK(&ap->port_task, fn);
+       ap->port_task_data = data;
 
-       if (!delay)
-               rc = queue_work(ata_wq, &ap->port_task);
-       else
-               rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
+       rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
 
        /* rc == 0 means that another user is using port task */
        WARN_ON(rc == 0);
@@ -987,7 +1089,7 @@ void ata_port_flush_task(struct ata_port *ap)
                ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
 }
 
-void ata_qc_complete_internal(struct ata_queued_cmd *qc)
+static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
 {
        struct completion *waiting = qc->private_data;
 
@@ -995,13 +1097,13 @@ void ata_qc_complete_internal(struct ata_queued_cmd *qc)
 }
 
 /**
- *     ata_exec_internal - execute libata internal command
+ *     ata_exec_internal_sg - execute libata internal command
  *     @dev: Device to which the command is sent
  *     @tf: Taskfile registers for the command and the result
  *     @cdb: CDB for packet command
  *     @dma_dir: Data tranfer direction of the command
- *     @buf: Data buffer of the command
- *     @buflen: Length of data buffer
+ *     @sg: sg list for the data buffer of the command
+ *     @n_elem: Number of sg entries
  *
  *     Executes libata internal command with timeout.  @tf contains
  *     command on entry and result on return.  Timeout and error
@@ -1015,9 +1117,10 @@ void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  *     RETURNS:
  *     Zero on success, AC_ERR_* mask on failure
  */
-unsigned ata_exec_internal(struct ata_device *dev,
-                          struct ata_taskfile *tf, const u8 *cdb,
-                          int dma_dir, void *buf, unsigned int buflen)
+unsigned ata_exec_internal_sg(struct ata_device *dev,
+                             struct ata_taskfile *tf, const u8 *cdb,
+                             int dma_dir, struct scatterlist *sg,
+                             unsigned int n_elem)
 {
        struct ata_port *ap = dev->ap;
        u8 command = tf->command;
@@ -1073,8 +1176,13 @@ unsigned ata_exec_internal(struct ata_device *dev,
        qc->flags |= ATA_QCFLAG_RESULT_TF;
        qc->dma_dir = dma_dir;
        if (dma_dir != DMA_NONE) {
-               ata_sg_init_one(qc, buf, buflen);
-               qc->nsect = buflen / ATA_SECT_SIZE;
+               unsigned int i, buflen = 0;
+
+               for (i = 0; i < n_elem; i++)
+                       buflen += sg[i].length;
+
+               ata_sg_init(qc, sg, n_elem);
+               qc->nbytes = buflen;
        }
 
        qc->private_data = &wait;
@@ -1116,7 +1224,7 @@ unsigned ata_exec_internal(struct ata_device *dev,
        if (ap->ops->post_internal_cmd)
                ap->ops->post_internal_cmd(qc);
 
-       if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
+       if ((qc->flags & ATA_QCFLAG_FAILED) && !qc->err_mask) {
                if (ata_msg_warn(ap))
                        ata_dev_printk(dev, KERN_WARNING,
                                "zero err_mask for failed "
@@ -1157,6 +1265,41 @@ unsigned ata_exec_internal(struct ata_device *dev,
 }
 
 /**
+ *     ata_exec_internal - execute libata internal command
+ *     @dev: Device to which the command is sent
+ *     @tf: Taskfile registers for the command and the result
+ *     @cdb: CDB for packet command
+ *     @dma_dir: Data tranfer direction of the command
+ *     @buf: Data buffer of the command
+ *     @buflen: Length of data buffer
+ *
+ *     Wrapper around ata_exec_internal_sg() which takes simple
+ *     buffer instead of sg list.
+ *
+ *     LOCKING:
+ *     None.  Should be called with kernel context, might sleep.
+ *
+ *     RETURNS:
+ *     Zero on success, AC_ERR_* mask on failure
+ */
+unsigned ata_exec_internal(struct ata_device *dev,
+                          struct ata_taskfile *tf, const u8 *cdb,
+                          int dma_dir, void *buf, unsigned int buflen)
+{
+       struct scatterlist *psg = NULL, sg;
+       unsigned int n_elem = 0;
+
+       if (dma_dir != DMA_NONE) {
+               WARN_ON(!buf);
+               sg_init_one(&sg, buf, buflen);
+               psg = &sg;
+               n_elem++;
+       }
+
+       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
+}
+
+/**
  *     ata_do_simple_cmd - execute simple internal command
  *     @dev: Device to which the command is sent
  *     @cmd: Opcode to execute
@@ -1220,7 +1363,7 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  *     ata_dev_read_id - Read ID data from the specified device
  *     @dev: target device
  *     @p_class: pointer to class of the target device (may be changed)
- *     @post_reset: is this read ID post-reset?
+ *     @flags: ATA_READID_* flags
  *     @id: buffer to read IDENTIFY data into
  *
  *     Read ID data from the specified device.  ATA_CMD_ID_ATA is
@@ -1235,7 +1378,7 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  *     0 on success, -errno otherwise.
  */
 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
-                   int post_reset, u16 *id)
+                   unsigned int flags, u16 *id)
 {
        struct ata_port *ap = dev->ap;
        unsigned int class = *p_class;
@@ -1267,10 +1410,17 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
        }
 
        tf.protocol = ATA_PROT_PIO;
+       tf.flags |= ATA_TFLAG_POLLING; /* for polling presence detection */
 
        err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
                                     id, sizeof(id[0]) * ATA_ID_WORDS);
        if (err_mask) {
+               if (err_mask & AC_ERR_NODEV_HINT) {
+                       DPRINTK("ata%u.%d: NODEV after polling detection\n",
+                               ap->id, dev->devno);
+                       return -ENOENT;
+               }
+
                rc = -EIO;
                reason = "I/O error";
                goto err_out;
@@ -1290,7 +1440,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                        goto err_out;
        }
 
-       if (post_reset && class == ATA_DEV_ATA) {
+       if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
                /*
                 * The exact sequence expected by certain pre-ATA4 drives is:
                 * SRST RESET
@@ -1310,7 +1460,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                        /* current CHS translation info (id[53-58]) might be
                         * changed. reread the identify device info.
                         */
-                       post_reset = 0;
+                       flags &= ~ATA_READID_POSTRESET;
                        goto retry;
                }
        }
@@ -1341,7 +1491,10 @@ static void ata_dev_config_ncq(struct ata_device *dev,
                desc[0] = '\0';
                return;
        }
-
+       if (ata_device_blacklisted(dev) & ATA_HORKAGE_NONCQ) {
+               snprintf(desc, desc_sz, "NCQ (not used)");
+               return;
+       }
        if (ap->flags & ATA_FLAG_NCQ) {
                hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
                dev->flags |= ATA_DFLAG_NCQ;
@@ -1370,7 +1523,6 @@ static void ata_set_port_max_cmd_len(struct ata_port *ap)
 /**
  *     ata_dev_configure - Configure the specified ATA/ATAPI device
  *     @dev: Target device to configure
- *     @print_info: Enable device info printout
  *
  *     Configure @dev according to @dev->id.  Generic and low-level
  *     driver specific fixups are also applied.
@@ -1381,12 +1533,15 @@ static void ata_set_port_max_cmd_len(struct ata_port *ap)
  *     RETURNS:
  *     0 on success, -errno otherwise
  */
-int ata_dev_configure(struct ata_device *dev, int print_info)
+int ata_dev_configure(struct ata_device *dev)
 {
        struct ata_port *ap = dev->ap;
+       int print_info = ap->eh_context.i.flags & ATA_EHI_PRINTINFO;
        const u16 *id = dev->id;
        unsigned int xfer_mask;
        char revbuf[7];         /* XYZ-99\0 */
+       char fwrevbuf[ATA_ID_FW_REV_LEN+1];
+       char modelbuf[ATA_ID_PROD_LEN+1];
        int rc;
 
        if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
@@ -1441,6 +1596,16 @@ int ata_dev_configure(struct ata_device *dev, int print_info)
 
                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;
+
                if (ata_id_has_lba(id)) {
                        const char *lba_desc;
                        char ncq_desc[20];
@@ -1450,19 +1615,26 @@ int ata_dev_configure(struct ata_device *dev, int print_info)
                        if (ata_id_has_lba48(id)) {
                                dev->flags |= ATA_DFLAG_LBA48;
                                lba_desc = "LBA48";
+
+                               if (dev->n_sectors >= (1UL << 28) &&
+                                   ata_id_has_flush_ext(id))
+                                       dev->flags |= ATA_DFLAG_FLUSH_EXT;
                        }
 
                        /* config NCQ */
                        ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
 
                        /* print device info to dmesg */
-                       if (ata_msg_drv(ap) && print_info)
-                               ata_dev_printk(dev, KERN_INFO, "%s, "
-                                       "max %s, %Lu sectors: %s %s\n",
-                                       revbuf,
-                                       ata_mode_string(xfer_mask),
+                       if (ata_msg_drv(ap) && print_info) {
+                               ata_dev_printk(dev, KERN_INFO,
+                                       "%s: %s, %s, max %s\n",
+                                       revbuf, modelbuf, fwrevbuf,
+                                       ata_mode_string(xfer_mask));
+                               ata_dev_printk(dev, KERN_INFO,
+                                       "%Lu sectors, multi %u: %s %s\n",
                                        (unsigned long long)dev->n_sectors,
-                                       lba_desc, ncq_desc);
+                                       dev->multi_count, lba_desc, ncq_desc);
+                       }
                } else {
                        /* CHS */
 
@@ -1479,22 +1651,17 @@ int ata_dev_configure(struct ata_device *dev, int print_info)
                        }
 
                        /* print device info to dmesg */
-                       if (ata_msg_drv(ap) && print_info)
-                               ata_dev_printk(dev, KERN_INFO, "%s, "
-                                       "max %s, %Lu sectors: CHS %u/%u/%u\n",
-                                       revbuf,
-                                       ata_mode_string(xfer_mask),
-                                       (unsigned long long)dev->n_sectors,
-                                       dev->cylinders, dev->heads,
-                                       dev->sectors);
-               }
-
-               if (dev->id[59] & 0x100) {
-                       dev->multi_count = dev->id[59] & 0xff;
-                       if (ata_msg_drv(ap) && print_info)
+                       if (ata_msg_drv(ap) && print_info) {
                                ata_dev_printk(dev, KERN_INFO,
-                                       "ata%u: dev %u multi count %u\n",
-                                       ap->id, dev->devno, dev->multi_count);
+                                       "%s: %s, %s, max %s\n",
+                                       revbuf, modelbuf, fwrevbuf,
+                                       ata_mode_string(xfer_mask));
+                               ata_dev_printk(dev, KERN_INFO, 
+                                       "%Lu sectors, multi %u, CHS %u/%u/%u\n",
+                                       (unsigned long long)dev->n_sectors,
+                                       dev->multi_count, dev->cylinders,
+                                       dev->heads, dev->sectors);
+                       }
                }
 
                dev->cdb_len = 16;
@@ -1526,6 +1693,11 @@ int ata_dev_configure(struct ata_device *dev, int print_info)
                                       cdb_intr_string);
        }
 
+       /* determine max_sectors */
+       dev->max_sectors = ATA_MAX_SECTORS;
+       if (dev->flags & ATA_DFLAG_LBA48)
+               dev->max_sectors = ATA_MAX_SECTORS_LBA48;
+
        if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
                /* Let the user know. We don't want to disallow opens for
                   rescue purposes, or in case the vendor is just a blithering
@@ -1627,11 +1799,14 @@ int ata_bus_probe(struct ata_port *ap)
                if (!ata_dev_enabled(dev))
                        continue;
 
-               rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
+               rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
+                                    dev->id);
                if (rc)
                        goto fail;
 
-               rc = ata_dev_configure(dev, 1);
+               ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
+               rc = ata_dev_configure(dev);
+               ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
                if (rc)
                        goto fail;
        }
@@ -2077,7 +2252,7 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
         * DMA cycle timing is slower/equal than the fastest PIO timing.
         */
 
-       if (speed > XFER_PIO_4) {
+       if (speed > XFER_PIO_6) {
                ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
                ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
        }
@@ -2149,6 +2324,7 @@ int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
 
 static int ata_dev_set_mode(struct ata_device *dev)
 {
+       struct ata_eh_context *ehc = &dev->ap->eh_context;
        unsigned int err_mask;
        int rc;
 
@@ -2157,13 +2333,19 @@ static int ata_dev_set_mode(struct ata_device *dev)
                dev->flags |= ATA_DFLAG_PIO;
 
        err_mask = ata_dev_set_xfermode(dev);
+       /* Old CFA may refuse this command, which is just fine */
+       if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
+               err_mask &= ~AC_ERR_DEV;
+
        if (err_mask) {
                ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
                               "(err_mask=0x%x)\n", err_mask);
                return -EIO;
        }
 
+       ehc->i.flags |= ATA_EHI_POST_SETMODE;
        rc = ata_dev_revalidate(dev, 0);
+       ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
        if (rc)
                return rc;
 
@@ -2196,18 +2378,8 @@ int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
        int i, rc = 0, used_dma = 0, found = 0;
 
        /* has private set_mode? */
-       if (ap->ops->set_mode) {
-               /* FIXME: make ->set_mode handle no device case and
-                * return error code and failing device on failure.
-                */
-               for (i = 0; i < ATA_MAX_DEVICES; i++) {
-                       if (ata_dev_ready(&ap->device[i])) {
-                               ap->ops->set_mode(ap);
-                               break;
-                       }
-               }
-               return 0;
-       }
+       if (ap->ops->set_mode)
+               return ap->ops->set_mode(ap, r_failed_dev);
 
        /* step 1: calculate xfer_mask */
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
@@ -2267,7 +2439,7 @@ int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
                dev = &ap->device[i];
 
-               /* don't udpate suspended devices' xfer mode */
+               /* don't update suspended devices' xfer mode */
                if (!ata_dev_ready(dev))
                        continue;
 
@@ -2321,11 +2493,14 @@ static inline void ata_tf_to_host(struct ata_port *ap,
  *     Sleep until ATA Status register bit BSY clears,
  *     or a timeout occurs.
  *
- *     LOCKING: None.
+ *     LOCKING:
+ *     Kernel thread context (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
  */
-
-unsigned int ata_busy_sleep (struct ata_port *ap,
-                            unsigned long tmout_pat, unsigned long tmout)
+int ata_busy_sleep(struct ata_port *ap,
+                  unsigned long tmout_pat, unsigned long tmout)
 {
        unsigned long timer_start, timeout;
        u8 status;
@@ -2333,25 +2508,32 @@ unsigned int ata_busy_sleep (struct ata_port *ap,
        status = ata_busy_wait(ap, ATA_BUSY, 300);
        timer_start = jiffies;
        timeout = timer_start + tmout_pat;
-       while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
+       while (status != 0xff && (status & ATA_BUSY) &&
+              time_before(jiffies, timeout)) {
                msleep(50);
                status = ata_busy_wait(ap, ATA_BUSY, 3);
        }
 
-       if (status & ATA_BUSY)
+       if (status != 0xff && (status & ATA_BUSY))
                ata_port_printk(ap, KERN_WARNING,
-                               "port is slow to respond, please be patient\n");
+                               "port is slow to respond, please be patient "
+                               "(Status 0x%x)\n", status);
 
        timeout = timer_start + tmout;
-       while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
+       while (status != 0xff && (status & ATA_BUSY) &&
+              time_before(jiffies, timeout)) {
                msleep(50);
                status = ata_chk_status(ap);
        }
 
+       if (status == 0xff)
+               return -ENODEV;
+
        if (status & ATA_BUSY) {
                ata_port_printk(ap, KERN_ERR, "port failed to respond "
-                               "(%lu secs)\n", tmout / HZ);
-               return 1;
+                               "(%lu secs, Status 0x%x)\n",
+                               tmout / HZ, status);
+               return -EBUSY;
        }
 
        return 0;
@@ -2378,13 +2560,8 @@ static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
                u8 nsect, lbal;
 
                ap->ops->dev_select(ap, 1);
-               if (ap->flags & ATA_FLAG_MMIO) {
-                       nsect = readb((void __iomem *) ioaddr->nsect_addr);
-                       lbal = readb((void __iomem *) ioaddr->lbal_addr);
-               } else {
-                       nsect = inb(ioaddr->nsect_addr);
-                       lbal = inb(ioaddr->lbal_addr);
-               }
+               nsect = ioread8(ioaddr->nsect_addr);
+               lbal = ioread8(ioaddr->lbal_addr);
                if ((nsect == 1) && (lbal == 1))
                        break;
                if (time_after(jiffies, timeout)) {
@@ -2412,19 +2589,11 @@ static unsigned int ata_bus_softreset(struct ata_port *ap,
        DPRINTK("ata%u: bus reset via SRST\n", ap->id);
 
        /* software reset.  causes dev0 to be selected */
-       if (ap->flags & ATA_FLAG_MMIO) {
-               writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
-               udelay(20);     /* FIXME: flush */
-               writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
-               udelay(20);     /* FIXME: flush */
-               writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
-       } else {
-               outb(ap->ctl, ioaddr->ctl_addr);
-               udelay(10);
-               outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
-               udelay(10);
-               outb(ap->ctl, ioaddr->ctl_addr);
-       }
+       iowrite8(ap->ctl, ioaddr->ctl_addr);
+       udelay(20);     /* FIXME: flush */
+       iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
+       udelay(20);     /* FIXME: flush */
+       iowrite8(ap->ctl, ioaddr->ctl_addr);
 
        /* spec mandates ">= 2ms" before checking status.
         * We wait 150ms, because that was the magic delay used for
@@ -2442,10 +2611,8 @@ static unsigned int ata_bus_softreset(struct ata_port *ap,
         * the bus shows 0xFF because the odd clown forgets the D7
         * pulldown resistor.
         */
-       if (ata_check_status(ap) == 0xFF) {
-               ata_port_printk(ap, KERN_ERR, "SRST failed (status 0xFF)\n");
-               return AC_ERR_OTHER;
-       }
+       if (ata_check_status(ap) == 0xFF)
+               return 0;
 
        ata_bus_post_reset(ap, devmask);
 
@@ -2511,8 +2678,7 @@ void ata_bus_reset(struct ata_port *ap)
                ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
 
        /* re-enable interrupts */
-       if (ap->ioaddr.ctl_addr)        /* FIXME: hack. create a hook instead */
-               ata_irq_on(ap);
+       ap->ops->irq_on(ap);
 
        /* is double-select really necessary? */
        if (ap->device[1].class != ATA_DEV_NONE)
@@ -2527,10 +2693,7 @@ void ata_bus_reset(struct ata_port *ap)
 
        if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
                /* set up device control for ATA_FLAG_SATA_RESET */
-               if (ap->flags & ATA_FLAG_MMIO)
-                       writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
-               else
-                       outb(ap->ctl, ioaddr->ctl_addr);
+               iowrite8(ap->ctl, ioaddr->ctl_addr);
        }
 
        DPRINTK("EXIT\n");
@@ -2771,9 +2934,9 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
 }
 
 /**
- *     sata_std_hardreset - reset host port via SATA phy reset
+ *     sata_port_hardreset - reset port via SATA phy reset
  *     @ap: port to reset
- *     @class: resulting class of attached device
+ *     @timing: timing parameters { interval, duratinon, timeout } in msec
  *
  *     SATA phy-reset host port using DET bits of SControl register.
  *
@@ -2783,10 +2946,8 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
+int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
 {
-       struct ata_eh_context *ehc = &ap->eh_context;
-       const unsigned long *timing = sata_ehc_deb_timing(ehc);
        u32 scontrol;
        int rc;
 
@@ -2799,24 +2960,24 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
                 * and Sil3124.
                 */
                if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
-                       return rc;
+                       goto out;
 
                scontrol = (scontrol & 0x0f0) | 0x304;
 
                if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
-                       return rc;
+                       goto out;
 
                sata_set_spd(ap);
        }
 
        /* issue phy wake/reset */
        if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
-               return rc;
+               goto out;
 
        scontrol = (scontrol & 0x0f0) | 0x301;
 
        if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
-               return rc;
+               goto out;
 
        /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
         * 10.4.2 says at least 1 ms.
@@ -2824,7 +2985,40 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
        msleep(1);
 
        /* bring phy back */
-       sata_phy_resume(ap, timing);
+       rc = sata_phy_resume(ap, timing);
+ out:
+       DPRINTK("EXIT, rc=%d\n", rc);
+       return rc;
+}
+
+/**
+ *     sata_std_hardreset - reset host port via SATA phy reset
+ *     @ap: port to reset
+ *     @class: resulting class of attached device
+ *
+ *     SATA phy-reset host port using DET bits of SControl register,
+ *     wait for !BSY and classify the attached device.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
+ */
+int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
+{
+       const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
+       int rc;
+
+       DPRINTK("ENTER\n");
+
+       /* do hardreset */
+       rc = sata_port_hardreset(ap, timing);
+       if (rc) {
+               ata_port_printk(ap, KERN_ERR,
+                               "COMRESET failed (errno=%d)\n", rc);
+               return rc;
+       }
 
        /* TODO: phy layer with polling, timeouts, etc. */
        if (ata_port_offline(ap)) {
@@ -2833,6 +3027,9 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
                return 0;
        }
 
+       /* 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)) {
                ata_port_printk(ap, KERN_ERR,
                                "COMRESET failed (device not ready)\n");
@@ -2873,11 +3070,8 @@ void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
                sata_scr_write(ap, SCR_ERROR, serror);
 
        /* re-enable interrupts */
-       if (!ap->ops->error_handler) {
-               /* FIXME: hack. create a hook instead */
-               if (ap->ioaddr.ctl_addr)
-                       ata_irq_on(ap);
-       }
+       if (!ap->ops->error_handler)
+               ap->ops->irq_on(ap);
 
        /* is double-select really necessary? */
        if (classes[0] != ATA_DEV_NONE)
@@ -2892,12 +3086,8 @@ void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
        }
 
        /* set up device control */
-       if (ap->ioaddr.ctl_addr) {
-               if (ap->flags & ATA_FLAG_MMIO)
-                       writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
-               else
-                       outb(ap->ctl, ap->ioaddr.ctl_addr);
-       }
+       if (ap->ioaddr.ctl_addr)
+               iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
 
        DPRINTK("EXIT\n");
 }
@@ -2922,7 +3112,8 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
                               const u16 *new_id)
 {
        const u16 *old_id = dev->id;
-       unsigned char model[2][41], serial[2][21];
+       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) {
@@ -2931,10 +3122,10 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
                return 0;
        }
 
-       ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
-       ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
-       ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
-       ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
+       ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
+       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])) {
@@ -2963,7 +3154,7 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
 /**
  *     ata_dev_revalidate - Revalidate ATA device
  *     @dev: device to revalidate
- *     @post_reset: is this revalidation after reset?
+ *     @readid_flags: read ID flags
  *
  *     Re-read IDENTIFY page and make sure @dev is still attached to
  *     the port.
@@ -2974,7 +3165,7 @@ 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, int post_reset)
+int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
 {
        unsigned int class = dev->class;
        u16 *id = (void *)dev->ap->sector_buf;
@@ -2986,7 +3177,7 @@ int ata_dev_revalidate(struct ata_device *dev, int post_reset)
        }
 
        /* read ID data */
-       rc = ata_dev_read_id(dev, &class, post_reset, id);
+       rc = ata_dev_read_id(dev, &class, readid_flags, id);
        if (rc)
                goto fail;
 
@@ -2999,7 +3190,7 @@ int ata_dev_revalidate(struct ata_device *dev, int post_reset)
        memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
 
        /* configure device according to the new ID */
-       rc = ata_dev_configure(dev, 0);
+       rc = ata_dev_configure(dev);
        if (rc == 0)
                return 0;
 
@@ -3008,58 +3199,80 @@ int ata_dev_revalidate(struct ata_device *dev, int post_reset)
        return rc;
 }
 
-static const char * const ata_dma_blacklist [] = {
-       "WDC AC11000H", NULL,
-       "WDC AC22100H", NULL,
-       "WDC AC32500H", NULL,
-       "WDC AC33100H", NULL,
-       "WDC AC31600H", NULL,
-       "WDC AC32100H", "24.09P07",
-       "WDC AC23200L", "21.10N21",
-       "Compaq CRD-8241B",  NULL,
-       "CRD-8400B", NULL,
-       "CRD-8480B", NULL,
-       "CRD-8482B", NULL,
-       "CRD-84", NULL,
-       "SanDisk SDP3B", NULL,
-       "SanDisk SDP3B-64", NULL,
-       "SANYO CD-ROM CRD", NULL,
-       "HITACHI CDR-8", 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,
-       "SAMSUNG CD-ROM SC-148C", 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"
+struct ata_blacklist_entry {
+       const char *model_num;
+       const char *model_rev;
+       unsigned long horkage;
 };
 
-static int ata_strim(char *s, size_t len)
+static const struct ata_blacklist_entry ata_device_blacklist [] = {
+       /* Devices with DMA related problems under Linux */
+       { "WDC AC11000H",       NULL,           ATA_HORKAGE_NODMA },
+       { "WDC AC22100H",       NULL,           ATA_HORKAGE_NODMA },
+       { "WDC AC32500H",       NULL,           ATA_HORKAGE_NODMA },
+       { "WDC AC33100H",       NULL,           ATA_HORKAGE_NODMA },
+       { "WDC AC31600H",       NULL,           ATA_HORKAGE_NODMA },
+       { "WDC AC32100H",       "24.09P07",     ATA_HORKAGE_NODMA },
+       { "WDC AC23200L",       "21.10N21",     ATA_HORKAGE_NODMA },
+       { "Compaq CRD-8241B",   NULL,           ATA_HORKAGE_NODMA },
+       { "CRD-8400B",          NULL,           ATA_HORKAGE_NODMA },
+       { "CRD-8480B",          NULL,           ATA_HORKAGE_NODMA },
+       { "CRD-8482B",          NULL,           ATA_HORKAGE_NODMA },
+       { "CRD-84",             NULL,           ATA_HORKAGE_NODMA },
+       { "SanDisk SDP3B",      NULL,           ATA_HORKAGE_NODMA },
+       { "SanDisk SDP3B-64",   NULL,           ATA_HORKAGE_NODMA },
+       { "SANYO CD-ROM CRD",   NULL,           ATA_HORKAGE_NODMA },
+       { "HITACHI CDR-8",      NULL,           ATA_HORKAGE_NODMA },
+       { "HITACHI CDR-8335",   NULL,           ATA_HORKAGE_NODMA },
+       { "HITACHI CDR-8435",   NULL,           ATA_HORKAGE_NODMA },
+       { "Toshiba CD-ROM XM-6202B", NULL,      ATA_HORKAGE_NODMA },
+       { "TOSHIBA CD-ROM XM-1702BC", NULL,     ATA_HORKAGE_NODMA },
+       { "CD-532E-A",          NULL,           ATA_HORKAGE_NODMA },
+       { "E-IDE CD-ROM CR-840",NULL,           ATA_HORKAGE_NODMA },
+       { "CD-ROM Drive/F5A",   NULL,           ATA_HORKAGE_NODMA },
+       { "WPI CDD-820",        NULL,           ATA_HORKAGE_NODMA },
+       { "SAMSUNG CD-ROM SC-148C", NULL,       ATA_HORKAGE_NODMA },
+       { "SAMSUNG CD-ROM SC",  NULL,           ATA_HORKAGE_NODMA },
+       { "SanDisk SDP3B-64",   NULL,           ATA_HORKAGE_NODMA },
+       { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
+       { "_NEC DV5800A",       NULL,           ATA_HORKAGE_NODMA },
+       { "SAMSUNG CD-ROM SN-124","N001",       ATA_HORKAGE_NODMA },
+
+       /* Devices we expect to fail diagnostics */
+
+       /* Devices where NCQ should be avoided */
+       /* NCQ is slow */
+        { "WDC WD740ADFD-00",   NULL,          ATA_HORKAGE_NONCQ },
+
+       /* Devices with NCQ limits */
+
+       /* End Marker */
+       { }
+};
+
+unsigned long ata_device_blacklisted(const struct ata_device *dev)
 {
-       len = strnlen(s, len);
+       unsigned char model_num[ATA_ID_PROD_LEN + 1];
+       unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
+       const struct ata_blacklist_entry *ad = ata_device_blacklist;
+
+       ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
+       ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
 
-       /* ATAPI specifies that empty space is blank-filled; remove blanks */
-       while ((len > 0) && (s[len - 1] == ' ')) {
-               len--;
-               s[len] = 0;
+       while (ad->model_num) {
+               if (!strcmp(ad->model_num, model_num)) {
+                       if (ad->model_rev == NULL)
+                               return ad->horkage;
+                       if (!strcmp(ad->model_rev, model_rev))
+                               return ad->horkage;
+               }
+               ad++;
        }
-       return len;
+       return 0;
 }
 
 static int ata_dma_blacklisted(const struct ata_device *dev)
 {
-       unsigned char model_num[40];
-       unsigned char model_rev[16];
-       unsigned int nlen, rlen;
-       int i;
-
        /* We don't support polling DMA.
         * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
         * if the LLDD handles only interrupts in the HSM_ST_LAST state.
@@ -3067,23 +3280,7 @@ static int ata_dma_blacklisted(const struct ata_device *dev)
        if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
            (dev->flags & ATA_DFLAG_CDB_INTR))
                return 1;
-
-       ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
-                         sizeof(model_num));
-       ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
-                         sizeof(model_rev));
-       nlen = ata_strim(model_num, sizeof(model_num));
-       rlen = ata_strim(model_rev, sizeof(model_rev));
-
-       for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
-               if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
-                       if (ata_dma_blacklist[i+1] == NULL)
-                               return 1;
-                       if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
-                               return 1;
-               }
-       }
-       return 0;
+       return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
 }
 
 /**
@@ -3113,6 +3310,13 @@ static void ata_dev_xfermask(struct ata_device *dev)
         */
        if (ap->cbl == ATA_CBL_PATA40)
                xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
+       /* Apply drive side cable rule. Unknown or 80 pin cables reported
+        * host side are checked drive side as well. Cases where we know a
+        * 40wire cable is used safely for 80 are not checked here.
+        */
+        if (ata_drive_40wire(dev->id) && (ap->cbl == ATA_CBL_PATA_UNK || ap->cbl == ATA_CBL_PATA80))
+               xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
+
 
        xfer_mask &= ata_pack_xfermask(dev->pio_mask,
                                       dev->mwdma_mask, dev->udma_mask);
@@ -3230,8 +3434,7 @@ static unsigned int ata_dev_init_params(struct ata_device *dev,
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-static void ata_sg_clean(struct ata_queued_cmd *qc)
+void ata_sg_clean(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct scatterlist *sg = qc->__sg;
@@ -3389,19 +3592,15 @@ void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
 
 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
 {
-       struct scatterlist *sg;
-
        qc->flags |= ATA_QCFLAG_SINGLE;
 
-       memset(&qc->sgent, 0, sizeof(qc->sgent));
        qc->__sg = &qc->sgent;
        qc->n_elem = 1;
        qc->orig_n_elem = 1;
        qc->buf_virt = buf;
        qc->nbytes = buflen;
 
-       sg = qc->__sg;
-       sg_init_one(sg, buf, buflen);
+       sg_init_one(&qc->sgent, buf, buflen);
 }
 
 /**
@@ -3604,53 +3803,7 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
 }
 
 /**
- *     ata_mmio_data_xfer - Transfer data by MMIO
- *     @adev: device for this I/O
- *     @buf: data buffer
- *     @buflen: buffer length
- *     @write_data: read/write
- *
- *     Transfer data from/to the device data register by MMIO.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-
-void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
-                       unsigned int buflen, int write_data)
-{
-       struct ata_port *ap = adev->ap;
-       unsigned int i;
-       unsigned int words = buflen >> 1;
-       u16 *buf16 = (u16 *) buf;
-       void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
-
-       /* Transfer multiple of 2 bytes */
-       if (write_data) {
-               for (i = 0; i < words; i++)
-                       writew(le16_to_cpu(buf16[i]), mmio);
-       } else {
-               for (i = 0; i < words; i++)
-                       buf16[i] = cpu_to_le16(readw(mmio));
-       }
-
-       /* Transfer trailing 1 byte, if any. */
-       if (unlikely(buflen & 0x01)) {
-               u16 align_buf[1] = { 0 };
-               unsigned char *trailing_buf = buf + buflen - 1;
-
-               if (write_data) {
-                       memcpy(align_buf, trailing_buf, 1);
-                       writew(le16_to_cpu(align_buf[0]), mmio);
-               } else {
-                       align_buf[0] = cpu_to_le16(readw(mmio));
-                       memcpy(trailing_buf, align_buf, 1);
-               }
-       }
-}
-
-/**
- *     ata_pio_data_xfer - Transfer data by PIO
+ *     ata_data_xfer - Transfer data by PIO
  *     @adev: device to target
  *     @buf: data buffer
  *     @buflen: buffer length
@@ -3661,18 +3814,17 @@ void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  *     LOCKING:
  *     Inherited from caller.
  */
-
-void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
-                      unsigned int buflen, int write_data)
+void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
+                  unsigned int buflen, int write_data)
 {
        struct ata_port *ap = adev->ap;
        unsigned int words = buflen >> 1;
 
        /* Transfer multiple of 2 bytes */
        if (write_data)
-               outsw(ap->ioaddr.data_addr, buf, words);
+               iowrite16_rep(ap->ioaddr.data_addr, buf, words);
        else
-               insw(ap->ioaddr.data_addr, buf, words);
+               ioread16_rep(ap->ioaddr.data_addr, buf, words);
 
        /* Transfer trailing 1 byte, if any. */
        if (unlikely(buflen & 0x01)) {
@@ -3681,16 +3833,16 @@ void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
 
                if (write_data) {
                        memcpy(align_buf, trailing_buf, 1);
-                       outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
+                       iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
                } else {
-                       align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
+                       align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
                        memcpy(trailing_buf, align_buf, 1);
                }
        }
 }
 
 /**
- *     ata_pio_data_xfer_noirq - Transfer data by PIO
+ *     ata_data_xfer_noirq - Transfer data by PIO
  *     @adev: device to target
  *     @buf: data buffer
  *     @buflen: buffer length
@@ -3702,13 +3854,12 @@ void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
  *     LOCKING:
  *     Inherited from caller.
  */
-
-void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
-                                   unsigned int buflen, int write_data)
+void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
+                        unsigned int buflen, int write_data)
 {
        unsigned long flags;
        local_irq_save(flags);
-       ata_pio_data_xfer(adev, buf, buflen, write_data);
+       ata_data_xfer(adev, buf, buflen, write_data);
        local_irq_restore(flags);
 }
 
@@ -3732,11 +3883,11 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
        unsigned int offset;
        unsigned char *buf;
 
-       if (qc->cursect == (qc->nsect - 1))
+       if (qc->curbytes == qc->nbytes - ATA_SECT_SIZE)
                ap->hsm_task_state = HSM_ST_LAST;
 
        page = sg[qc->cursg].page;
-       offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
+       offset = sg[qc->cursg].offset + qc->cursg_ofs;
 
        /* get the current page and offset */
        page = nth_page(page, (offset >> PAGE_SHIFT));
@@ -3761,10 +3912,10 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
                ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
        }
 
-       qc->cursect++;
-       qc->cursg_ofs++;
+       qc->curbytes += ATA_SECT_SIZE;
+       qc->cursg_ofs += ATA_SECT_SIZE;
 
-       if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
+       if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
                qc->cursg++;
                qc->cursg_ofs = 0;
        }
@@ -3789,7 +3940,8 @@ static void ata_pio_sectors(struct ata_queued_cmd *qc)
 
                WARN_ON(qc->dev->multi_count == 0);
 
-               nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
+               nsect = min((qc->nbytes - qc->curbytes) / ATA_SECT_SIZE,
+                           qc->dev->multi_count);
                while (nsect--)
                        ata_pio_sector(qc);
        } else
@@ -4030,7 +4182,7 @@ static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
                        qc = ata_qc_from_tag(ap, qc->tag);
                        if (qc) {
                                if (likely(!(qc->err_mask & AC_ERR_HSM))) {
-                                       ata_irq_on(ap);
+                                       ap->ops->irq_on(ap);
                                        ata_qc_complete(qc);
                                } else
                                        ata_port_freeze(ap);
@@ -4046,7 +4198,7 @@ static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
        } else {
                if (in_wq) {
                        spin_lock_irqsave(ap->lock, flags);
-                       ata_irq_on(ap);
+                       ap->ops->irq_on(ap);
                        ata_qc_complete(qc);
                        spin_unlock_irqrestore(ap->lock, flags);
                } else
@@ -4194,8 +4346,12 @@ fsm_start:
                                        /* device stops HSM for abort/error */
                                        qc->err_mask |= AC_ERR_DEV;
                                else
-                                       /* HSM violation. Let EH handle this */
-                                       qc->err_mask |= AC_ERR_HSM;
+                                       /* HSM violation. Let EH handle this.
+                                        * Phantom devices also trigger this
+                                        * condition.  Mark hint.
+                                        */
+                                       qc->err_mask |= AC_ERR_HSM |
+                                                       AC_ERR_NODEV_HINT;
 
                                ap->hsm_task_state = HSM_ST_ERR;
                                goto fsm_start;
@@ -4289,10 +4445,11 @@ fsm_start:
        return poll_next;
 }
 
-static void ata_pio_task(void *_data)
+static void ata_pio_task(struct work_struct *work)
 {
-       struct ata_queued_cmd *qc = _data;
-       struct ata_port *ap = qc->ap;
+       struct ata_port *ap =
+               container_of(work, struct ata_port, port_task.work);
+       struct ata_queued_cmd *qc = ap->port_task_data;
        u8 status;
        int poll_next;
 
@@ -4434,6 +4591,14 @@ void __ata_qc_complete(struct ata_queued_cmd *qc)
        qc->complete_fn(qc);
 }
 
+static void fill_result_tf(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+
+       ap->ops->tf_read(ap, &qc->result_tf);
+       qc->result_tf.flags = qc->tf.flags;
+}
+
 /**
  *     ata_qc_complete - Complete an active ATA command
  *     @qc: Command to complete
@@ -4471,7 +4636,7 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
                if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
                        if (!ata_tag_internal(qc->tag)) {
                                /* always fill result TF for failed qc */
-                               ap->ops->tf_read(ap, &qc->result_tf);
+                               fill_result_tf(qc);
                                ata_qc_schedule_eh(qc);
                                return;
                        }
@@ -4479,7 +4644,7 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
 
                /* read result TF if requested */
                if (qc->flags & ATA_QCFLAG_RESULT_TF)
-                       ap->ops->tf_read(ap, &qc->result_tf);
+                       fill_result_tf(qc);
 
                __ata_qc_complete(qc);
        } else {
@@ -4488,7 +4653,7 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
 
                /* read result TF if failed or requested */
                if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
-                       ap->ops->tf_read(ap, &qc->result_tf);
+                       fill_result_tf(qc);
 
                __ata_qc_complete(qc);
        }
@@ -4654,6 +4819,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
        if (ap->flags & ATA_FLAG_PIO_POLLING) {
                switch (qc->tf.protocol) {
                case ATA_PROT_PIO:
+               case ATA_PROT_NODATA:
                case ATA_PROT_ATAPI:
                case ATA_PROT_ATAPI_NODATA:
                        qc->tf.flags |= ATA_TFLAG_POLLING;
@@ -4668,6 +4834,14 @@ 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);
 
@@ -4776,6 +4950,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
 inline unsigned int ata_host_intr (struct ata_port *ap,
                                   struct ata_queued_cmd *qc)
 {
+       struct ata_eh_info *ehi = &ap->eh_info;
        u8 status, host_stat = 0;
 
        VPRINTK("ata%u: protocol %d task_state %d\n",
@@ -4836,6 +5011,11 @@ inline unsigned int ata_host_intr (struct ata_port *ap,
        ap->ops->irq_clear(ap);
 
        ata_hsm_move(ap, qc, status, 0);
+
+       if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
+                                      qc->tf.protocol == ATA_PROT_ATAPI_DMA))
+               ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
+
        return 1;       /* irq handled */
 
 idle_irq:
@@ -4843,7 +5023,7 @@ idle_irq:
 
 #ifdef ATA_IRQ_TRAP
        if ((ap->stats.idle_irq % 1000) == 0) {
-               ata_irq_ack(ap, 0); /* debug trap */
+               ap->ops->irq_ack(ap, 0); /* debug trap */
                ata_port_printk(ap, KERN_WARNING, "irq trap\n");
                return 1;
        }
@@ -4855,7 +5035,6 @@ idle_irq:
  *     ata_interrupt - Default ATA host interrupt handler
  *     @irq: irq line (unused)
  *     @dev_instance: pointer to our ata_host information structure
- *     @regs: unused
  *
  *     Default interrupt handler for PCI IDE devices.  Calls
  *     ata_host_intr() for each port that is not disabled.
@@ -4867,7 +5046,7 @@ idle_irq:
  *     IRQ_NONE or IRQ_HANDLED.
  */
 
-irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
+irqreturn_t ata_interrupt (int irq, void *dev_instance)
 {
        struct ata_host *host = dev_instance;
        unsigned int i;
@@ -5043,7 +5222,7 @@ int ata_flush_cache(struct ata_device *dev)
        if (!ata_try_flush_cache(dev))
                return 0;
 
-       if (ata_id_has_flush_ext(dev->id))
+       if (dev->flags & ATA_DFLAG_FLUSH_EXT)
                cmd = ATA_CMD_FLUSH_EXT;
        else
                cmd = ATA_CMD_FLUSH;
@@ -5185,54 +5364,25 @@ void ata_host_resume(struct ata_host *host)
  *     LOCKING:
  *     Inherited from caller.
  */
-
-int ata_port_start (struct ata_port *ap)
+int ata_port_start(struct ata_port *ap)
 {
        struct device *dev = ap->dev;
        int rc;
 
-       ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
+       ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
+                                     GFP_KERNEL);
        if (!ap->prd)
                return -ENOMEM;
 
        rc = ata_pad_alloc(ap, dev);
-       if (rc) {
-               dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
+       if (rc)
                return rc;
-       }
-
-       DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
 
+       DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
+               (unsigned long long)ap->prd_dma);
        return 0;
 }
 
-
-/**
- *     ata_port_stop - Undo ata_port_start()
- *     @ap: Port to shut down
- *
- *     Frees the PRD table.
- *
- *     May be used as the port_stop() entry in ata_port_operations.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-
-void ata_port_stop (struct ata_port *ap)
-{
-       struct device *dev = ap->dev;
-
-       dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
-       ata_pad_free(ap, dev);
-}
-
-void ata_host_stop (struct ata_host *host)
-{
-       if (host->mmio_base)
-               iounmap(host->mmio_base);
-}
-
 /**
  *     ata_dev_init - Initialize an ata_device structure
  *     @dev: Device structure to initialize
@@ -5315,9 +5465,9 @@ void ata_port_init(struct ata_port *ap, struct ata_host *host,
        ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
 #endif
 
-       INIT_WORK(&ap->port_task, NULL, NULL);
-       INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
-       INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
+       INIT_DELAYED_WORK(&ap->port_task, NULL);
+       INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
+       INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
        INIT_LIST_HEAD(&ap->eh_done_q);
        init_waitqueue_head(&ap->eh_wait_q);
 
@@ -5406,6 +5556,27 @@ static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
        return ap;
 }
 
+static void ata_host_release(struct device *gendev, void *res)
+{
+       struct ata_host *host = dev_get_drvdata(gendev);
+       int i;
+
+       for (i = 0; i < host->n_ports; i++) {
+               struct ata_port *ap = host->ports[i];
+
+               if (!ap)
+                       continue;
+
+               if (ap->ops->port_stop)
+                       ap->ops->port_stop(ap);
+
+               scsi_host_put(ap->scsi_host);
+       }
+
+       if (host->ops->host_stop)
+               host->ops->host_stop(host);
+}
+
 /**
  *     ata_sas_host_init - Initialize a host struct
  *     @host:  host to initialize
@@ -5453,22 +5624,28 @@ int ata_device_add(const struct ata_probe_ent *ent)
        int rc;
 
        DPRINTK("ENTER\n");
-       
+
        if (ent->irq == 0) {
                dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
                return 0;
        }
+
+       if (!devres_open_group(dev, ata_device_add, GFP_KERNEL))
+               return 0;
+
        /* alloc a container for our list of ATA ports (buses) */
-       host = kzalloc(sizeof(struct ata_host) +
-                      (ent->n_ports * sizeof(void *)), GFP_KERNEL);
+       host = devres_alloc(ata_host_release, sizeof(struct ata_host) +
+                           (ent->n_ports * sizeof(void *)), GFP_KERNEL);
        if (!host)
-               return 0;
+               goto err_out;
+       devres_add(dev, host);
+       dev_set_drvdata(dev, host);
 
        ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
        host->n_ports = ent->n_ports;
        host->irq = ent->irq;
        host->irq2 = ent->irq2;
-       host->mmio_base = ent->mmio_base;
+       host->iomap = ent->iomap;
        host->private_data = ent->private_data;
 
        /* register each port bound to this device */
@@ -5506,8 +5683,8 @@ int ata_device_add(const struct ata_probe_ent *ent)
                                (ap->pio_mask << ATA_SHIFT_PIO);
 
                /* print per-port info to dmesg */
-               ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
-                               "ctl 0x%lX bmdma 0x%lX irq %d\n",
+               ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%p "
+                               "ctl 0x%p bmdma 0x%p irq %d\n",
                                ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
                                ata_mode_string(xfer_mode_mask),
                                ap->ioaddr.cmd_addr,
@@ -5515,14 +5692,13 @@ int ata_device_add(const struct ata_probe_ent *ent)
                                ap->ioaddr.bmdma_addr,
                                irq_line);
 
-               ata_chk_status(ap);
-               host->ops->irq_clear(ap);
-               ata_eh_freeze_port(ap); /* freeze port before requesting IRQ */
+               /* freeze port before requesting IRQ */
+               ata_eh_freeze_port(ap);
        }
 
        /* obtain irq, that may be shared between channels */
-       rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
-                        DRV_NAME, host);
+       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);
@@ -5535,15 +5711,19 @@ int ata_device_add(const struct ata_probe_ent *ent)
                   so trap it now */
                BUG_ON(ent->irq == ent->irq2);
 
-               rc = request_irq(ent->irq2, ent->port_ops->irq_handler, ent->irq_flags,
-                        DRV_NAME, host);
+               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_free_irq;
+                       goto err_out;
                }
        }
 
+       /* resource acquisition complete */
+       devres_remove_group(dev, ata_device_add);
+
        /* perform each probe synchronously */
        DPRINTK("probe begin\n");
        for (i = 0; i < host->n_ports; i++) {
@@ -5612,24 +5792,13 @@ int ata_device_add(const struct ata_probe_ent *ent)
                ata_scsi_scan_host(ap);
        }
 
-       dev_set_drvdata(dev, host);
-
        VPRINTK("EXIT, returning %u\n", ent->n_ports);
        return ent->n_ports; /* success */
 
-err_out_free_irq:
-       free_irq(ent->irq, host);
-err_out:
-       for (i = 0; i < host->n_ports; i++) {
-               struct ata_port *ap = host->ports[i];
-               if (ap) {
-                       ap->ops->port_stop(ap);
-                       scsi_host_put(ap->scsi_host);
-               }
-       }
-
-       kfree(host);
-       VPRINTK("EXIT, returning 0\n");
+ err_out:
+       devres_release_group(dev, ata_device_add);
+       dev_set_drvdata(dev, NULL);
+       VPRINTK("EXIT, returning %d\n", rc);
        return 0;
 }
 
@@ -5692,76 +5861,20 @@ void ata_port_detach(struct ata_port *ap)
 }
 
 /**
- *     ata_host_remove - PCI layer callback for device removal
- *     @host: ATA host set that was removed
+ *     ata_host_detach - Detach all ports of an ATA host
+ *     @host: Host to detach
  *
- *     Unregister all objects associated with this host set. Free those
- *     objects.
+ *     Detach all ports of @host.
  *
  *     LOCKING:
- *     Inherited from calling layer (may sleep).
+ *     Kernel thread context (may sleep).
  */
-
-void ata_host_remove(struct ata_host *host)
+void ata_host_detach(struct ata_host *host)
 {
-       unsigned int i;
+       int i;
 
        for (i = 0; i < host->n_ports; i++)
                ata_port_detach(host->ports[i]);
-
-       free_irq(host->irq, host);
-       if (host->irq2)
-               free_irq(host->irq2, host);
-
-       for (i = 0; i < host->n_ports; i++) {
-               struct ata_port *ap = host->ports[i];
-
-               ata_scsi_release(ap->scsi_host);
-
-               if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
-                       struct ata_ioports *ioaddr = &ap->ioaddr;
-
-                       /* FIXME: Add -ac IDE pci mods to remove these special cases */
-                       if (ioaddr->cmd_addr == ATA_PRIMARY_CMD)
-                               release_region(ATA_PRIMARY_CMD, 8);
-                       else if (ioaddr->cmd_addr == ATA_SECONDARY_CMD)
-                               release_region(ATA_SECONDARY_CMD, 8);
-               }
-
-               scsi_host_put(ap->scsi_host);
-       }
-
-       if (host->ops->host_stop)
-               host->ops->host_stop(host);
-
-       kfree(host);
-}
-
-/**
- *     ata_scsi_release - SCSI layer callback hook for host unload
- *     @host: libata host to be unloaded
- *
- *     Performs all duties necessary to shut down a libata port...
- *     Kill port kthread, disable port, and release resources.
- *
- *     LOCKING:
- *     Inherited from SCSI layer.
- *
- *     RETURNS:
- *     One.
- */
-
-int ata_scsi_release(struct Scsi_Host *shost)
-{
-       struct ata_port *ap = ata_shost_to_port(shost);
-
-       DPRINTK("ENTER\n");
-
-       ap->ops->port_disable(ap);
-       ap->ops->port_stop(ap);
-
-       DPRINTK("EXIT\n");
-       return 1;
 }
 
 struct ata_probe_ent *
@@ -5769,7 +5882,11 @@ ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
 {
        struct ata_probe_ent *probe_ent;
 
-       probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
+       /* XXX - the following if can go away once all LLDs are managed */
+       if (!list_empty(&dev->devres_head))
+               probe_ent = devm_kzalloc(dev, sizeof(*probe_ent), GFP_KERNEL);
+       else
+               probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
        if (!probe_ent) {
                printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
                       kobject_name(&(dev->kobj)));
@@ -5785,6 +5902,7 @@ ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
        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;
 }
@@ -5818,37 +5936,23 @@ void ata_std_ports(struct ata_ioports *ioaddr)
 
 #ifdef CONFIG_PCI
 
-void ata_pci_host_stop (struct ata_host *host)
-{
-       struct pci_dev *pdev = to_pci_dev(host->dev);
-
-       pci_iounmap(pdev, host->mmio_base);
-}
-
 /**
  *     ata_pci_remove_one - PCI layer callback for device removal
  *     @pdev: PCI device that was removed
  *
- *     PCI layer indicates to libata via this hook that
- *     hot-unplug or module unload event has occurred.
- *     Handle this by unregistering all objects associated
- *     with this PCI device.  Free those objects.  Then finally
- *     release PCI resources and disable device.
+ *     PCI layer indicates to libata via this hook that hot-unplug or
+ *     module unload event has occurred.  Detach all ports.  Resource
+ *     release is handled via devres.
  *
  *     LOCKING:
  *     Inherited from PCI layer (may sleep).
  */
-
-void ata_pci_remove_one (struct pci_dev *pdev)
+void ata_pci_remove_one(struct pci_dev *pdev)
 {
        struct device *dev = pci_dev_to_dev(pdev);
        struct ata_host *host = dev_get_drvdata(dev);
 
-       ata_host_remove(host);
-
-       pci_release_regions(pdev);
-       pci_disable_device(pdev);
-       dev_set_drvdata(dev, NULL);
+       ata_host_detach(host);
 }
 
 /* move to PCI subsystem */
@@ -5895,12 +5999,22 @@ void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
        }
 }
 
-void ata_pci_device_do_resume(struct pci_dev *pdev)
+int ata_pci_device_do_resume(struct pci_dev *pdev)
 {
+       int rc;
+
        pci_set_power_state(pdev, PCI_D0);
        pci_restore_state(pdev);
-       pci_enable_device(pdev);
+
+       rc = pcim_enable_device(pdev);
+       if (rc) {
+               dev_printk(KERN_ERR, &pdev->dev,
+                          "failed to enable device after resume (%d)\n", rc);
+               return rc;
+       }
+
        pci_set_master(pdev);
+       return 0;
 }
 
 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
@@ -5920,10 +6034,12 @@ int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
 int ata_pci_device_resume(struct pci_dev *pdev)
 {
        struct ata_host *host = dev_get_drvdata(&pdev->dev);
+       int rc;
 
-       ata_pci_device_do_resume(pdev);
-       ata_host_resume(host);
-       return 0;
+       rc = ata_pci_device_do_resume(pdev);
+       if (rc == 0)
+               ata_host_resume(host);
+       return rc;
 }
 #endif /* CONFIG_PCI */
 
@@ -5951,7 +6067,7 @@ static void __exit ata_exit(void)
        destroy_workqueue(ata_aux_wq);
 }
 
-module_init(ata_init);
+subsys_initcall(ata_init);
 module_exit(ata_exit);
 
 static unsigned long ratelimit_time;
@@ -6069,8 +6185,7 @@ EXPORT_SYMBOL_GPL(ata_std_bios_param);
 EXPORT_SYMBOL_GPL(ata_std_ports);
 EXPORT_SYMBOL_GPL(ata_host_init);
 EXPORT_SYMBOL_GPL(ata_device_add);
-EXPORT_SYMBOL_GPL(ata_port_detach);
-EXPORT_SYMBOL_GPL(ata_host_remove);
+EXPORT_SYMBOL_GPL(ata_host_detach);
 EXPORT_SYMBOL_GPL(ata_sg_init);
 EXPORT_SYMBOL_GPL(ata_sg_init_one);
 EXPORT_SYMBOL_GPL(ata_hsm_move);
@@ -6087,12 +6202,9 @@ 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_port_stop);
-EXPORT_SYMBOL_GPL(ata_host_stop);
 EXPORT_SYMBOL_GPL(ata_interrupt);
-EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
-EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
-EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
+EXPORT_SYMBOL_GPL(ata_data_xfer);
+EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
 EXPORT_SYMBOL_GPL(ata_qc_prep);
 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
@@ -6114,9 +6226,9 @@ EXPORT_SYMBOL_GPL(__sata_phy_reset);
 EXPORT_SYMBOL_GPL(ata_bus_reset);
 EXPORT_SYMBOL_GPL(ata_std_prereset);
 EXPORT_SYMBOL_GPL(ata_std_softreset);
+EXPORT_SYMBOL_GPL(sata_port_hardreset);
 EXPORT_SYMBOL_GPL(sata_std_hardreset);
 EXPORT_SYMBOL_GPL(ata_std_postreset);
-EXPORT_SYMBOL_GPL(ata_dev_revalidate);
 EXPORT_SYMBOL_GPL(ata_dev_classify);
 EXPORT_SYMBOL_GPL(ata_dev_pair);
 EXPORT_SYMBOL_GPL(ata_port_disable);
@@ -6129,7 +6241,6 @@ EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
-EXPORT_SYMBOL_GPL(ata_scsi_release);
 EXPORT_SYMBOL_GPL(ata_host_intr);
 EXPORT_SYMBOL_GPL(sata_scr_valid);
 EXPORT_SYMBOL_GPL(sata_scr_read);
@@ -6141,6 +6252,7 @@ EXPORT_SYMBOL_GPL(ata_host_suspend);
 EXPORT_SYMBOL_GPL(ata_host_resume);
 EXPORT_SYMBOL_GPL(ata_id_string);
 EXPORT_SYMBOL_GPL(ata_id_c_string);
+EXPORT_SYMBOL_GPL(ata_device_blacklisted);
 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
 
 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
@@ -6149,7 +6261,6 @@ EXPORT_SYMBOL_GPL(ata_timing_merge);
 
 #ifdef CONFIG_PCI
 EXPORT_SYMBOL_GPL(pci_test_config_bits);
-EXPORT_SYMBOL_GPL(ata_pci_host_stop);
 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
 EXPORT_SYMBOL_GPL(ata_pci_init_one);
 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
@@ -6173,3 +6284,7 @@ EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
 EXPORT_SYMBOL_GPL(ata_do_eh);
+EXPORT_SYMBOL_GPL(ata_irq_on);
+EXPORT_SYMBOL_GPL(ata_dummy_irq_on);
+EXPORT_SYMBOL_GPL(ata_irq_ack);
+EXPORT_SYMBOL_GPL(ata_dummy_irq_ack);