struct ata_queued_cmd *qc;
        unsigned int tag, preempted_tag;
        u32 preempted_sactive, preempted_qc_active;
+       int preempted_nr_active_links;
        DECLARE_COMPLETION_ONSTACK(wait);
        unsigned long flags;
        unsigned int err_mask;
        preempted_tag = link->active_tag;
        preempted_sactive = link->sactive;
        preempted_qc_active = ap->qc_active;
+       preempted_nr_active_links = ap->nr_active_links;
        link->active_tag = ATA_TAG_POISON;
        link->sactive = 0;
        ap->qc_active = 0;
+       ap->nr_active_links = 0;
 
        /* prepare & issue qc */
        qc->tf = *tf;
        link->active_tag = preempted_tag;
        link->sactive = preempted_sactive;
        ap->qc_active = preempted_qc_active;
+       ap->nr_active_links = preempted_nr_active_links;
 
        /* XXX - Some LLDDs (sata_mv) disable port on command failure.
         * Until those drivers are fixed, we detect the condition
                ata_sg_clean(qc);
 
        /* command should be marked inactive atomically with qc completion */
-       if (qc->tf.protocol == ATA_PROT_NCQ)
+       if (qc->tf.protocol == ATA_PROT_NCQ) {
                link->sactive &= ~(1 << qc->tag);
-       else
+               if (!link->sactive)
+                       ap->nr_active_links--;
+       } else {
                link->active_tag = ATA_TAG_POISON;
+               ap->nr_active_links--;
+       }
+
+       /* clear exclusive status */
+       if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
+                    ap->excl_link == link))
+               ap->excl_link = NULL;
 
        /* atapi: mark qc as inactive to prevent the interrupt handler
         * from completing the command twice later, before the error handler
 
        if (qc->tf.protocol == ATA_PROT_NCQ) {
                WARN_ON(link->sactive & (1 << qc->tag));
+
+               if (!link->sactive)
+                       ap->nr_active_links++;
                link->sactive |= 1 << qc->tag;
        } else {
                WARN_ON(link->sactive);
+
+               ap->nr_active_links++;
                link->active_tag = qc->tag;
        }
 
 
 
                ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
                ap->pflags &= ~ATA_PFLAG_EH_PENDING;
+               ap->excl_link = NULL;   /* don't maintain exclusion over EH */
 
                spin_unlock_irqrestore(ap->lock, flags);
 
                        }
                }
        }
+
+       /* make sure nr_active_links is zero after EH */
+       WARN_ON(ap->nr_active_links);
+       ap->nr_active_links = 0;
 }
 
 /**
 
        ATA_QCFLAG_DMAMAP       = ATA_QCFLAG_SG | ATA_QCFLAG_SINGLE,
        ATA_QCFLAG_IO           = (1 << 3), /* standard IO command */
        ATA_QCFLAG_RESULT_TF    = (1 << 4), /* result TF requested */
+       ATA_QCFLAG_CLEAR_EXCL   = (1 << 5), /* clear excl_link on completion */
 
        ATA_QCFLAG_FAILED       = (1 << 16), /* cmd failed and is owned by EH */
        ATA_QCFLAG_SENSE_VALID  = (1 << 17), /* sense data valid */
        struct ata_queued_cmd   qcmd[ATA_MAX_QUEUE];
        unsigned long           qc_allocated;
        unsigned int            qc_active;
+       int                     nr_active_links; /* #links with active qcs */
 
        struct ata_link         link;   /* host default link */
 
        int                     nr_pmp_links;   /* nr of available PMP links */
        struct ata_link         *pmp_link;      /* array of PMP links */
+       struct ata_link         *excl_link;     /* for PMP qc exclusion */
 
        struct ata_port_stats   stats;
        struct ata_host         *host;
        return 1;
 }
 
+static inline int ata_link_active(struct ata_link *link)
+{
+       return ata_tag_valid(link->active_tag) || link->sactive;
+}
+
 static inline struct ata_link *ata_port_first_link(struct ata_port *ap)
 {
        if (ap->nr_pmp_links)