]> err.no Git - linux-2.6/blobdiff - drivers/ata/libata-core.c
Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / ata / libata-core.c
index 8b08e7bdd24d59727bf3d7e3a94266a27e22e0d5..629eadbd0ec09c17f538ace9ffd7921161679eac 100644 (file)
@@ -97,13 +97,17 @@ static int ata_ignore_hpa = 0;
 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
 
+static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
+module_param_named(dma, libata_dma_mask, int, 0444);
+MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
+
 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
 module_param(ata_probe_timeout, int, 0444);
 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
 
-int libata_noacpi = 1;
+int libata_noacpi = 0;
 module_param_named(noacpi, libata_noacpi, int, 0444);
-MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
+MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
 
 MODULE_AUTHOR("Jeff Garzik");
 MODULE_DESCRIPTION("Library module for ATA devices");
@@ -670,29 +674,49 @@ static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  *     None.
  *
  *     RETURNS:
- *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
- *     the event of failure.
+ *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
+ *     %ATA_DEV_UNKNOWN the event of failure.
  */
-
 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
 {
        /* Apple's open source Darwin code hints that some devices only
         * put a proper signature into the LBA mid/high registers,
         * So, we only check those.  It's sufficient for uniqueness.
+        *
+        * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
+        * signatures for ATA and ATAPI devices attached on SerialATA,
+        * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
+        * spec has never mentioned about using different signatures
+        * for ATA/ATAPI devices.  Then, Serial ATA II: Port
+        * Multiplier specification began to use 0x69/0x96 to identify
+        * port multpliers and 0x3c/0xc3 to identify SEMB device.
+        * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
+        * 0x69/0x96 shortly and described them as reserved for
+        * SerialATA.
+        *
+        * We follow the current spec and consider that 0x69/0x96
+        * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
         */
-
-       if (((tf->lbam == 0) && (tf->lbah == 0)) ||
-           ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
+       if ((tf->lbam == 0) && (tf->lbah == 0)) {
                DPRINTK("found ATA device by sig\n");
                return ATA_DEV_ATA;
        }
 
-       if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
-           ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
+       if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
                DPRINTK("found ATAPI device by sig\n");
                return ATA_DEV_ATAPI;
        }
 
+       if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
+               DPRINTK("found PMP device by sig\n");
+               return ATA_DEV_PMP;
+       }
+
+       if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
+               printk("ata: SEMB device ignored\n");
+               return ATA_DEV_SEMB_UNSUP; /* not yet */
+       }
+
        DPRINTK("unknown device\n");
        return ATA_DEV_UNKNOWN;
 }
@@ -902,7 +926,7 @@ static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
        tf.protocol |= ATA_PROT_NODATA;
        tf.device |= ATA_LBA;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_WARNING, "failed to read native "
                               "max address (err_mask=0x%x)\n", err_mask);
@@ -915,7 +939,8 @@ static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
                *max_sectors = ata_tf_to_lba48(&tf);
        else
                *max_sectors = ata_tf_to_lba(&tf);
-
+        if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
+               (*max_sectors)--;
        return 0;
 }
 
@@ -950,9 +975,12 @@ static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
                tf.hob_lbal = (new_sectors >> 24) & 0xff;
                tf.hob_lbam = (new_sectors >> 32) & 0xff;
                tf.hob_lbah = (new_sectors >> 40) & 0xff;
-       } else
+       } else {
                tf.command = ATA_CMD_SET_MAX;
 
+               tf.device |= (new_sectors >> 24) & 0xf;
+       }
+
        tf.protocol |= ATA_PROT_NODATA;
        tf.device |= ATA_LBA;
 
@@ -960,7 +988,7 @@ static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
        tf.lbam = (new_sectors >> 8) & 0xff;
        tf.lbah = (new_sectors >> 16) & 0xff;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_WARNING, "failed to set "
                               "max address (err_mask=0x%x)\n", err_mask);
@@ -1364,8 +1392,9 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  *     @tf: Taskfile registers for the command and the result
  *     @cdb: CDB for packet command
  *     @dma_dir: Data tranfer direction of the command
- *     @sg: sg list for the data buffer of the command
+ *     @sgl: sg list for the data buffer of the command
  *     @n_elem: Number of sg entries
+ *     @timeout: Timeout in msecs (0 for default)
  *
  *     Executes libata internal command with timeout.  @tf contains
  *     command on entry and result on return.  Timeout and error
@@ -1381,8 +1410,8 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  */
 unsigned ata_exec_internal_sg(struct ata_device *dev,
                              struct ata_taskfile *tf, const u8 *cdb,
-                             int dma_dir, struct scatterlist *sg,
-                             unsigned int n_elem)
+                             int dma_dir, struct scatterlist *sgl,
+                             unsigned int n_elem, unsigned long timeout)
 {
        struct ata_link *link = dev->link;
        struct ata_port *ap = link->ap;
@@ -1443,11 +1472,12 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
        qc->dma_dir = dma_dir;
        if (dma_dir != DMA_NONE) {
                unsigned int i, buflen = 0;
+               struct scatterlist *sg;
 
-               for (i = 0; i < n_elem; i++)
-                       buflen += sg[i].length;
+               for_each_sg(sgl, sg, n_elem, i)
+                       buflen += sg->length;
 
-               ata_sg_init(qc, sg, n_elem);
+               ata_sg_init(qc, sgl, n_elem);
                qc->nbytes = buflen;
        }
 
@@ -1458,7 +1488,10 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
 
        spin_unlock_irqrestore(ap->lock, flags);
 
-       rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
+       if (!timeout)
+               timeout = ata_probe_timeout * 1000 / HZ;
+
+       rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
 
        ata_port_flush_task(ap);
 
@@ -1543,6 +1576,7 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
  *     @dma_dir: Data tranfer direction of the command
  *     @buf: Data buffer of the command
  *     @buflen: Length of data buffer
+ *     @timeout: Timeout in msecs (0 for default)
  *
  *     Wrapper around ata_exec_internal_sg() which takes simple
  *     buffer instead of sg list.
@@ -1555,7 +1589,8 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
  */
 unsigned ata_exec_internal(struct ata_device *dev,
                           struct ata_taskfile *tf, const u8 *cdb,
-                          int dma_dir, void *buf, unsigned int buflen)
+                          int dma_dir, void *buf, unsigned int buflen,
+                          unsigned long timeout)
 {
        struct scatterlist *psg = NULL, sg;
        unsigned int n_elem = 0;
@@ -1567,7 +1602,8 @@ unsigned ata_exec_internal(struct ata_device *dev,
                n_elem++;
        }
 
-       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
+       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
+                                   timeout);
 }
 
 /**
@@ -1594,7 +1630,7 @@ unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
        tf.flags |= ATA_TFLAG_DEVICE;
        tf.protocol = ATA_PROT_NODATA;
 
-       return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 }
 
 /**
@@ -1709,7 +1745,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
        tf.flags |= ATA_TFLAG_POLLING;
 
        err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
-                                    id, sizeof(id[0]) * ATA_ID_WORDS);
+                                    id, sizeof(id[0]) * ATA_ID_WORDS, 0);
        if (err_mask) {
                if (err_mask & AC_ERR_NODEV_HINT) {
                        DPRINTK("ata%u.%d: NODEV after polling detection\n",
@@ -1768,7 +1804,8 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                tf.feature = SETFEATURES_SPINUP;
                tf.protocol = ATA_PROT_NODATA;
                tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
-               err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+               err_mask = ata_exec_internal(dev, &tf, NULL,
+                                            DMA_NONE, NULL, 0, 0);
                if (err_mask && id[2] != 0x738c) {
                        rc = -EIO;
                        reason = "SPINUP failed";
@@ -2893,14 +2930,27 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
        /* step 1: calculate xfer_mask */
        ata_link_for_each_dev(dev, link) {
                unsigned int pio_mask, dma_mask;
+               unsigned int mode_mask;
 
                if (!ata_dev_enabled(dev))
                        continue;
 
+               mode_mask = ATA_DMA_MASK_ATA;
+               if (dev->class == ATA_DEV_ATAPI)
+                       mode_mask = ATA_DMA_MASK_ATAPI;
+               else if (ata_id_is_cfa(dev->id))
+                       mode_mask = ATA_DMA_MASK_CFA;
+
                ata_dev_xfermask(dev);
 
                pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
                dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+
+               if (libata_dma_mask & mode_mask)
+                       dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+               else
+                       dma_mask = 0;
+
                dev->pio_mode = ata_xfer_mask2mode(pio_mask);
                dev->dma_mode = ata_xfer_mask2mode(dma_mask);
 
@@ -3168,6 +3218,8 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
                             unsigned long deadline)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
+       struct ata_device *dev;
+       int i = 0;
 
        DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
 
@@ -3178,6 +3230,25 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
        udelay(20);     /* FIXME: flush */
        iowrite8(ap->ctl, ioaddr->ctl_addr);
 
+       /* If we issued an SRST then an ATA drive (not ATAPI)
+        * may have changed configuration and be in PIO0 timing. If
+        * we did a hard reset (or are coming from power on) this is
+        * true for ATA or ATAPI. Until we've set a suitable controller
+        * mode we should not touch the bus as we may be talking too fast.
+        */
+
+       ata_link_for_each_dev(dev, &ap->link)
+               dev->pio_mode = XFER_PIO_0;
+
+       /* If the controller has a pio mode setup function then use
+          it to set the chipset to rights. Don't touch the DMA setup
+          as that will be dealt with when revalidating */
+       if (ap->ops->set_piomode) {
+               ata_link_for_each_dev(dev, &ap->link)
+                       if (devmask & (1 << i++))
+                               ap->ops->set_piomode(ap, dev);
+       }
+
        /* spec mandates ">= 2ms" before checking status.
         * We wait 150ms, because that was the magic delay used for
         * ATAPI devices in Hale Landis's ATADRVR, for the period of time
@@ -3422,6 +3493,12 @@ int ata_std_prereset(struct ata_link *link, unsigned long deadline)
            (link->flags & ATA_LFLAG_HRST_TO_RESUME))
                ehc->i.action |= ATA_EH_HARDRESET;
 
+       /* Some PMPs don't work with only SRST, force hardreset if PMP
+        * is supported.
+        */
+       if (ap->flags & ATA_FLAG_PMP)
+               ehc->i.action |= ATA_EH_HARDRESET;
+
        /* if we're about to do hardreset, nothing more to do */
        if (ehc->i.action & ATA_EH_HARDRESET)
                return 0;
@@ -3612,6 +3689,16 @@ int sata_std_hardreset(struct ata_link *link, unsigned int *class,
        /* wait a while before checking status, see SRST for more info */
        msleep(150);
 
+       /* If PMP is supported, we have to do follow-up SRST.  Note
+        * that some PMPs don't send D2H Reg FIS after hardreset at
+        * all if the first port is empty.  Wait for it just for a
+        * second and request follow-up SRST.
+        */
+       if (ap->flags & ATA_FLAG_PMP) {
+               ata_wait_ready(ap, jiffies + HZ);
+               return -EAGAIN;
+       }
+
        rc = ata_wait_ready(ap, deadline);
        /* link occupied, -ENODEV too is an error */
        if (rc) {
@@ -3861,6 +3948,9 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "IOMEGA  ZIP 250       ATAPI", NULL,  ATA_HORKAGE_NODMA }, /* temporary fix */
        { "IOMEGA  ZIP 250       ATAPI       Floppy",
                                NULL,           ATA_HORKAGE_NODMA },
+       /* Odd clown on sil3726/4726 PMPs */
+       { "Config  Disk",       NULL,           ATA_HORKAGE_NODMA |
+                                               ATA_HORKAGE_SKIP_PM },
 
        /* Weird ATAPI devices */
        { "TORiSAN DVD-ROM DRD-N216", NULL,     ATA_HORKAGE_MAX_SEC_128 },
@@ -3875,8 +3965,9 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        /* NCQ is broken */
        { "Maxtor *",           "BANC*",        ATA_HORKAGE_NONCQ },
        { "Maxtor 7V300F0",     "VA111630",     ATA_HORKAGE_NONCQ },
-       { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
-         ATA_HORKAGE_NONCQ },
+       { "HITACHI HDS7250SASUN500G*", NULL,    ATA_HORKAGE_NONCQ },
+       { "HITACHI HDS7225SBSUN250G*", NULL,    ATA_HORKAGE_NONCQ },
+       { "ST380817AS",         "3.42",         ATA_HORKAGE_NONCQ },
 
        /* Blacklist entries taken from Silicon Image 3124/3132
           Windows driver .inf file - also several Linux problem reports */
@@ -3886,11 +3977,17 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        /* Drives which do spurious command completion */
        { "HTS541680J9SA00",    "SB2IC7EP",     ATA_HORKAGE_NONCQ, },
        { "HTS541612J9SA00",    "SBDIC7JP",     ATA_HORKAGE_NONCQ, },
+       { "HDT722516DLA380",    "V43OA96A",     ATA_HORKAGE_NONCQ, },
        { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
        { "WDC WD740ADFD-00NLR1", NULL,         ATA_HORKAGE_NONCQ, },
+       { "WDC WD3200AAJS-00RYA0", "12.01B01",  ATA_HORKAGE_NONCQ, },
        { "FUJITSU MHV2080BH",  "00840028",     ATA_HORKAGE_NONCQ, },
+       { "ST9120822AS",        "3.CLF",        ATA_HORKAGE_NONCQ, },
        { "ST9160821AS",        "3.CLF",        ATA_HORKAGE_NONCQ, },
-       { "ST3160812AS",        "3.AD",         ATA_HORKAGE_NONCQ, },
+       { "ST9160821AS",        "3.ALD",        ATA_HORKAGE_NONCQ, },
+       { "ST9160821AS",        "3.CCD",        ATA_HORKAGE_NONCQ, },
+       { "ST3160812AS",        "3.ADJ",        ATA_HORKAGE_NONCQ, },
+       { "ST980813AS",         "3.ADB",        ATA_HORKAGE_NONCQ, },
        { "SAMSUNG HD401LJ",    "ZZ100-15",     ATA_HORKAGE_NONCQ, },
 
        /* devices which puke on READ_NATIVE_MAX */
@@ -3899,6 +3996,10 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
        { "MAXTOR 6L080L4",     "A93.0500",     ATA_HORKAGE_BROKEN_HPA },
 
+       /* Devices which report 1 sector over size HPA */
+       { "ST340823A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
+       { "ST320413A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
+
        /* End Marker */
        { }
 };
@@ -3914,8 +4015,14 @@ int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
        p = strchr(patt, wildchar);
        if (p && ((*(p + 1)) == 0))
                len = p - patt;
-       else
+       else {
                len = strlen(name);
+               if (!len) {
+                       if (!*patt)
+                               return 0;
+                       return -1;
+               }
+       }
 
        return strncmp(patt, name, len);
 }
@@ -4066,7 +4173,7 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = dev->xfer_mode;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 
        DPRINTK("EXIT, err_mask=%x\n", err_mask);
        return err_mask;
@@ -4102,7 +4209,7 @@ static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = SATA_AN;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 
        DPRINTK("EXIT, err_mask=%x\n", err_mask);
        return err_mask;
@@ -4140,7 +4247,7 @@ static unsigned int ata_dev_init_params(struct ata_device *dev,
        tf.nsect = sectors;
        tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        /* A clean abort indicates an original or just out of spec drive
           and we should continue as we issue the setup based on the
           drive reported working geometry */
@@ -4186,7 +4293,7 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
                if (qc->n_elem)
                        dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
                /* restore last sg */
-               sg[qc->orig_n_elem - 1].length += qc->pad_len;
+               sg_last(sg, qc->orig_n_elem)->length += qc->pad_len;
                if (pad_buf) {
                        struct scatterlist *psg = &qc->pad_sgent;
                        void *addr = kmap_atomic(psg->page, KM_IRQ0);
@@ -4441,6 +4548,7 @@ void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
        qc->orig_n_elem = 1;
        qc->buf_virt = buf;
        qc->nbytes = buflen;
+       qc->cursg = qc->__sg;
 
        sg_init_one(&qc->sgent, buf, buflen);
 }
@@ -4466,6 +4574,7 @@ void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
        qc->__sg = sg;
        qc->n_elem = n_elem;
        qc->orig_n_elem = n_elem;
+       qc->cursg = qc->__sg;
 }
 
 /**
@@ -4555,7 +4664,7 @@ static int ata_sg_setup(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct scatterlist *sg = qc->__sg;
-       struct scatterlist *lsg = &sg[qc->n_elem - 1];
+       struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
        int n_elem, pre_n_elem, dir, trim_sg = 0;
 
        VPRINTK("ENTER, ata%u\n", ap->print_id);
@@ -4719,7 +4828,6 @@ void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
 static void ata_pio_sector(struct ata_queued_cmd *qc)
 {
        int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
-       struct scatterlist *sg = qc->__sg;
        struct ata_port *ap = qc->ap;
        struct page *page;
        unsigned int offset;
@@ -4728,8 +4836,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
        if (qc->curbytes == qc->nbytes - qc->sect_size)
                ap->hsm_task_state = HSM_ST_LAST;
 
-       page = sg[qc->cursg].page;
-       offset = sg[qc->cursg].offset + qc->cursg_ofs;
+       page = qc->cursg->page;
+       offset = qc->cursg->offset + qc->cursg_ofs;
 
        /* get the current page and offset */
        page = nth_page(page, (offset >> PAGE_SHIFT));
@@ -4757,8 +4865,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
        qc->curbytes += qc->sect_size;
        qc->cursg_ofs += qc->sect_size;
 
-       if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
-               qc->cursg++;
+       if (qc->cursg_ofs == qc->cursg->length) {
+               qc->cursg = sg_next(qc->cursg);
                qc->cursg_ofs = 0;
        }
 }
@@ -4844,16 +4952,18 @@ static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
 {
        int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
        struct scatterlist *sg = qc->__sg;
+       struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
        struct ata_port *ap = qc->ap;
        struct page *page;
        unsigned char *buf;
        unsigned int offset, count;
+       int no_more_sg = 0;
 
        if (qc->curbytes + bytes >= qc->nbytes)
                ap->hsm_task_state = HSM_ST_LAST;
 
 next_sg:
-       if (unlikely(qc->cursg >= qc->n_elem)) {
+       if (unlikely(no_more_sg)) {
                /*
                 * The end of qc->sg is reached and the device expects
                 * more data to transfer. In order not to overrun qc->sg
@@ -4876,7 +4986,7 @@ next_sg:
                return;
        }
 
-       sg = &qc->__sg[qc->cursg];
+       sg = qc->cursg;
 
        page = sg->page;
        offset = sg->offset + qc->cursg_ofs;
@@ -4915,7 +5025,10 @@ next_sg:
        qc->cursg_ofs += count;
 
        if (qc->cursg_ofs == sg->length) {
-               qc->cursg++;
+               if (qc->cursg == lsg)
+                       no_more_sg = 1;
+
+               qc->cursg = sg_next(qc->cursg);
                qc->cursg_ofs = 0;
        }
 
@@ -5952,22 +6065,26 @@ int sata_scr_valid(struct ata_link *link)
  *     @val: Place to store read value
  *
  *     Read SCR register @reg of @link into *@val.  This function is
- *     guaranteed to succeed if the cable type of the port is SATA
- *     and the port implements ->scr_read.
+ *     guaranteed to succeed if @link is ap->link, the cable type of
+ *     the port is SATA and the port implements ->scr_read.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
 {
-       struct ata_port *ap = link->ap;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
+
+               if (sata_scr_valid(link))
+                       return ap->ops->scr_read(ap, reg, val);
+               return -EOPNOTSUPP;
+       }
 
-       if (sata_scr_valid(link))
-               return ap->ops->scr_read(ap, reg, val);
-       return -EOPNOTSUPP;
+       return sata_pmp_scr_read(link, reg, val);
 }
 
 /**
@@ -5977,22 +6094,26 @@ int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  *     @val: value to write
  *
  *     Write @val to SCR register @reg of @link.  This function is
- *     guaranteed to succeed if the cable type of the port is SATA
- *     and the port implements ->scr_read.
+ *     guaranteed to succeed if @link is ap->link, the cable type of
+ *     the port is SATA and the port implements ->scr_read.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_write(struct ata_link *link, int reg, u32 val)
 {
-       struct ata_port *ap = link->ap;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
 
-       if (sata_scr_valid(link))
-               return ap->ops->scr_write(ap, reg, val);
-       return -EOPNOTSUPP;
+               if (sata_scr_valid(link))
+                       return ap->ops->scr_write(ap, reg, val);
+               return -EOPNOTSUPP;
+       }
+
+       return sata_pmp_scr_write(link, reg, val);
 }
 
 /**
@@ -6005,23 +6126,27 @@ int sata_scr_write(struct ata_link *link, int reg, u32 val)
  *     function performs flush after writing to the register.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
 {
-       struct ata_port *ap = link->ap;
-       int rc;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
+               int rc;
 
-       if (sata_scr_valid(link)) {
-               rc = ap->ops->scr_write(ap, reg, val);
-               if (rc == 0)
-                       rc = ap->ops->scr_read(ap, reg, &val);
-               return rc;
+               if (sata_scr_valid(link)) {
+                       rc = ap->ops->scr_write(ap, reg, val);
+                       if (rc == 0)
+                               rc = ap->ops->scr_read(ap, reg, &val);
+                       return rc;
+               }
+               return -EOPNOTSUPP;
        }
-       return -EOPNOTSUPP;
+
+       return sata_pmp_scr_write(link, reg, val);
 }
 
 /**
@@ -6410,6 +6535,7 @@ static void ata_host_release(struct device *gendev, void *res)
                if (ap->scsi_host)
                        scsi_host_put(ap->scsi_host);
 
+               kfree(ap->pmp_link);
                kfree(ap);
                host->ports[i] = NULL;
        }
@@ -6669,13 +6795,14 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
                                              ap->udma_mask);
 
-               if (!ata_port_is_dummy(ap))
+               if (!ata_port_is_dummy(ap)) {
                        ata_port_printk(ap, KERN_INFO,
                                        "%cATA max %s %s\n",
                                        (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
                                        ata_mode_string(xfer_mask),
                                        ap->link.eh_info.desc);
-               else
+                       ata_ehi_clear_desc(&ap->link.eh_info);
+               } else
                        ata_port_printk(ap, KERN_INFO, "DUMMY\n");
        }
 
@@ -6894,7 +7021,7 @@ void ata_std_ports(struct ata_ioports *ioaddr)
  */
 void ata_pci_remove_one(struct pci_dev *pdev)
 {
-       struct device *dev = pci_dev_to_dev(pdev);
+       struct device *dev = &pdev->dev;
        struct ata_host *host = dev_get_drvdata(dev);
 
        ata_host_detach(host);
@@ -7240,6 +7367,12 @@ EXPORT_SYMBOL_GPL(ata_pci_default_filter);
 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
 #endif /* CONFIG_PCI */
 
+EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
+EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
+EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
+EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
+EXPORT_SYMBOL_GPL(sata_pmp_do_eh);
+
 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);