of: Always use 'struct device.of_node' to get device node pointer.
[safe/jmp/linux-2.6] / drivers / ata / sata_fsl.c
index 922d7b2..e3339e2 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>
 
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_cmnd.h>
@@ -34,8 +35,7 @@ enum {
 
        SATA_FSL_HOST_FLAGS     = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
                                ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
-                               ATA_FLAG_NCQ),
-       SATA_FSL_HOST_LFLAGS    = ATA_LFLAG_SKIP_D2H_BSY,
+                               ATA_FLAG_PMP | ATA_FLAG_NCQ | ATA_FLAG_AN),
 
        SATA_FSL_MAX_CMDS       = SATA_FSL_QUEUE_DEPTH,
        SATA_FSL_CMD_HDR_SIZE   = 16,   /* 4 DWORDS */
@@ -44,9 +44,9 @@ enum {
        /*
         * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
         * chained indirect PRDEs upto a max count of 63.
-        * We are allocating an array of 63 PRDEs contigiously, but PRDE#15 will
+        * We are allocating an array of 63 PRDEs contiguously, but PRDE#15 will
         * be setup as an indirect descriptor, pointing to it's next
-        * (contigious) PRDE. Though chained indirect PRDE arrays are
+        * (contiguous) PRDE. Though chained indirect PRDE arrays are
         * supported,it will be more efficient to use a direct PRDT and
         * a single chain/link to indirect PRDE array/PRDT.
         */
@@ -133,7 +133,7 @@ enum {
        INT_ON_SINGL_DEVICE_ERR = (1 << 1),
        INT_ON_CMD_COMPLETE = 1,
 
-       INT_ON_ERROR = INT_ON_FATAL_ERR |
+       INT_ON_ERROR = INT_ON_FATAL_ERR | INT_ON_SNOTIFY_UPDATE |
            INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
 
        /*
@@ -154,7 +154,7 @@ enum {
        IE_ON_CMD_COMPLETE = 1,
 
        DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
-           IE_ON_SIGNATURE_UPDATE |
+           IE_ON_SIGNATURE_UPDATE | IE_ON_SNOTIFY_UPDATE |
            IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
 
        EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
@@ -206,6 +206,7 @@ struct cmdhdr_tbl_entry {
  * Description information bitdefs
  */
 enum {
+       CMD_DESC_RES = (1 << 11),
        VENDOR_SPECIFIC_BIST = (1 << 10),
        CMD_DESC_SNOOP_ENABLE = (1 << 9),
        FPDMA_QUEUED_CMD = (1 << 8),
@@ -245,17 +246,6 @@ struct sata_fsl_port_priv {
        dma_addr_t cmdslot_paddr;
        struct command_desc *cmdentry;
        dma_addr_t cmdentry_paddr;
-
-       /*
-        * SATA FSL controller has a Status FIS which should contain the
-        * received D2H FIS & taskfile registers. This SFIS is present in
-        * the command descriptor, and to have a ready reference to it,
-        * we are caching it here, quite similar to what is done in H/W on
-        * AHCI compliant devices by copying taskfile fields to a 32-bit
-        * register.
-        */
-
-       struct ata_taskfile tf;
 };
 
 /*
@@ -325,7 +315,7 @@ static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
        u32 ttl_dwords = 0;
 
        /*
-        * NOTE : direct & indirect prdt's are contigiously allocated
+        * NOTE : direct & indirect prdt's are contiguously allocated
         */
        struct prde *prd = (struct prde *)&((struct command_desc *)
                                            cmd_desc)->prdt;
@@ -335,7 +325,7 @@ static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
        dma_addr_t indirect_ext_segment_paddr;
        unsigned int si;
 
-       VPRINTK("SATA FSL : cd = 0x%x, prd = 0x%x\n", cmd_desc, prd);
+       VPRINTK("SATA FSL : cd = 0x%p, prd = 0x%p\n", cmd_desc, prd);
 
        indirect_ext_segment_paddr = cmd_desc_paddr +
            SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
@@ -344,19 +334,20 @@ static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
                dma_addr_t sg_addr = sg_dma_address(sg);
                u32 sg_len = sg_dma_len(sg);
 
-               VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%x, sg_len = %d\n",
-                       sg_addr, sg_len);
+               VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%llx, sg_len = %d\n",
+                       (unsigned long long)sg_addr, sg_len);
 
                /* warn if each s/g element is not dword aligned */
                if (sg_addr & 0x03)
                        ata_port_printk(qc->ap, KERN_ERR,
-                                       "s/g addr unaligned : 0x%x\n", sg_addr);
+                                       "s/g addr unaligned : 0x%llx\n",
+                                       (unsigned long long)sg_addr);
                if (sg_len & 0x03)
                        ata_port_printk(qc->ap, KERN_ERR,
                                        "s/g len unaligned : 0x%x\n", sg_len);
 
-               if ((num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1)) &&
-                   (qc->n_iter + 1 != qc->n_elem)) {
+               if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
+                   sg_next(sg) != NULL) {
                        VPRINTK("setting indirect prde\n");
                        prd_ptr_to_indirect_ext = prd;
                        prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
@@ -399,7 +390,7 @@ static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
        void __iomem *hcr_base = host_priv->hcr_base;
        unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
        struct command_desc *cd;
-       u32 desc_info = CMD_DESC_SNOOP_ENABLE;
+       u32 desc_info = CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE;
        u32 num_prde = 0;
        u32 ttl_dwords = 0;
        dma_addr_t cd_paddr;
@@ -407,7 +398,7 @@ static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
        cd = (struct command_desc *)pp->cmdentry + tag;
        cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
 
-       ata_tf_to_fis(&qc->tf, 0, 1, (u8 *) &cd->cfis);
+       ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *) &cd->cfis);
 
        VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
                cd->cfis[0], cd->cfis[1], cd->cfis[2]);
@@ -450,6 +441,8 @@ static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
                ioread32(CA + hcr_base),
                ioread32(CE + hcr_base), ioread32(CC + hcr_base));
 
+       iowrite32(qc->dev->link->pmp, CQPMP + hcr_base);
+
        /* Simply queue command to the controller/device */
        iowrite32(1 << tag, CQ + hcr_base);
 
@@ -459,15 +452,30 @@ static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
        VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
                ioread32(CE + hcr_base),
                ioread32(DE + hcr_base),
-               ioread32(CC + hcr_base), ioread32(COMMANDSTAT + csr_base));
+               ioread32(CC + hcr_base),
+               ioread32(COMMANDSTAT + host_priv->csr_base));
 
        return 0;
 }
 
-static int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
-                              u32 val)
+static bool sata_fsl_qc_fill_rtf(struct ata_queued_cmd *qc)
 {
-       struct sata_fsl_host_priv *host_priv = ap->host->private_data;
+       struct sata_fsl_port_priv *pp = qc->ap->private_data;
+       struct sata_fsl_host_priv *host_priv = qc->ap->host->private_data;
+       void __iomem *hcr_base = host_priv->hcr_base;
+       unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
+       struct command_desc *cd;
+
+       cd = pp->cmdentry + tag;
+
+       ata_tf_from_fis(cd->sfis, &qc->result_tf);
+       return true;
+}
+
+static int sata_fsl_scr_write(struct ata_link *link,
+                             unsigned int sc_reg_in, u32 val)
+{
+       struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
        void __iomem *ssr_base = host_priv->ssr_base;
        unsigned int sc_reg;
 
@@ -488,10 +496,10 @@ static int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
        return 0;
 }
 
-static int sata_fsl_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
-                       u32 *val)
+static int sata_fsl_scr_read(struct ata_link *link,
+                            unsigned int sc_reg_in, u32 *val)
 {
-       struct sata_fsl_host_priv *host_priv = ap->host->private_data;
+       struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
        void __iomem *ssr_base = host_priv->ssr_base;
        unsigned int sc_reg;
 
@@ -522,7 +530,8 @@ static void sata_fsl_freeze(struct ata_port *ap)
                ioread32(CQ + hcr_base),
                ioread32(CA + hcr_base),
                ioread32(CE + hcr_base), ioread32(DE + hcr_base));
-       VPRINTK("CmdStat = 0x%x\n", ioread32(csr_base + COMMANDSTAT));
+       VPRINTK("CmdStat = 0x%x\n",
+               ioread32(host_priv->csr_base + COMMANDSTAT));
 
        /* disable interrupts on the controller/port */
        temp = ioread32(hcr_base + HCONTROL);
@@ -554,43 +563,36 @@ static void sata_fsl_thaw(struct ata_port *ap)
                ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
 }
 
-/*
- * NOTE : 1st D2H FIS from device does not update sfis in command descriptor.
- */
-static inline void sata_fsl_cache_taskfile_from_d2h_fis(struct ata_queued_cmd
-                                                       *qc,
-                                                       struct ata_port *ap)
+static void sata_fsl_pmp_attach(struct ata_port *ap)
 {
-       struct sata_fsl_port_priv *pp = ap->private_data;
        struct sata_fsl_host_priv *host_priv = ap->host->private_data;
        void __iomem *hcr_base = host_priv->hcr_base;
-       unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
-       struct command_desc *cd;
-
-       cd = pp->cmdentry + tag;
+       u32 temp;
 
-       ata_tf_from_fis(cd->sfis, &pp->tf);
+       temp = ioread32(hcr_base + HCONTROL);
+       iowrite32((temp | HCONTROL_PMP_ATTACHED), hcr_base + HCONTROL);
 }
 
-static u8 sata_fsl_check_status(struct ata_port *ap)
+static void sata_fsl_pmp_detach(struct ata_port *ap)
 {
-       struct sata_fsl_port_priv *pp = ap->private_data;
+       struct sata_fsl_host_priv *host_priv = ap->host->private_data;
+       void __iomem *hcr_base = host_priv->hcr_base;
+       u32 temp;
 
-       return pp->tf.command;
-}
+       temp = ioread32(hcr_base + HCONTROL);
+       temp &= ~HCONTROL_PMP_ATTACHED;
+       iowrite32(temp, hcr_base + HCONTROL);
 
-static void sata_fsl_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
-{
-       struct sata_fsl_port_priv *pp = ap->private_data;
+       /* enable interrupts on the controller/port */
+       temp = ioread32(hcr_base + HCONTROL);
+       iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
 
-       *tf = pp->tf;
 }
 
 static int sata_fsl_port_start(struct ata_port *ap)
 {
        struct device *dev = ap->host->dev;
        struct sata_fsl_port_priv *pp;
-       int retval;
        void *mem;
        dma_addr_t mem_dma;
        struct sata_fsl_host_priv *host_priv = ap->host->private_data;
@@ -601,21 +603,9 @@ static int sata_fsl_port_start(struct ata_port *ap)
        if (!pp)
                return -ENOMEM;
 
-       /*
-        * allocate per command dma alignment pad buffer, which is used
-        * internally by libATA to ensure that all transfers ending on
-        * unaligned boundaries are padded, to align on Dword boundaries
-        */
-       retval = ata_pad_alloc(ap, dev);
-       if (retval) {
-               kfree(pp);
-               return retval;
-       }
-
        mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
                                 GFP_KERNEL);
        if (!mem) {
-               ata_pad_free(ap, dev);
                kfree(pp);
                return -ENOMEM;
        }
@@ -658,12 +648,12 @@ static int sata_fsl_port_start(struct ata_port *ap)
         * Workaround for 8315DS board 3gbps link-up issue,
         * currently limit SATA port to GEN1 speed
         */
-       sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
+       sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
        temp &= ~(0xF << 4);
        temp |= (0x1 << 4);
-       sata_fsl_scr_write(ap, SCR_CONTROL, temp);
+       sata_fsl_scr_write(&ap->link, SCR_CONTROL, temp);
 
-       sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
+       sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
        dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
                        temp);
 #endif
@@ -694,7 +684,6 @@ static void sata_fsl_port_stop(struct ata_port *ap)
        dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
                          pp->cmdslot, pp->cmdslot_paddr);
 
-       ata_pad_free(ap, dev);
        kfree(pp);
 }
 
@@ -719,21 +708,17 @@ static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
        return ata_dev_classify(&tf);
 }
 
-static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
-                             unsigned long deadline)
+static int sata_fsl_hardreset(struct ata_link *link, unsigned int *class,
+                                       unsigned long deadline)
 {
        struct ata_port *ap = link->ap;
-       struct sata_fsl_port_priv *pp = ap->private_data;
        struct sata_fsl_host_priv *host_priv = ap->host->private_data;
        void __iomem *hcr_base = host_priv->hcr_base;
        u32 temp;
-       struct ata_taskfile tf;
-       u8 *cfis;
-       u32 Serror;
        int i = 0;
        unsigned long start_jiffies;
 
-       DPRINTK("in xx_softreset\n");
+       DPRINTK("in xx_hardreset\n");
 
 try_offline_again:
        /*
@@ -748,7 +733,7 @@ try_offline_again:
 
        if (temp & ONLINE) {
                ata_port_printk(ap, KERN_ERR,
-                               "Softreset failed, not off-lined %d\n", i);
+                               "Hardreset failed, not off-lined %d\n", i);
 
                /*
                 * Try to offline controller atleast twice
@@ -760,7 +745,7 @@ try_offline_again:
                        goto try_offline_again;
        }
 
-       DPRINTK("softreset, controller off-lined\n");
+       DPRINTK("hardreset, controller off-lined\n");
        VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
        VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
 
@@ -778,17 +763,18 @@ try_offline_again:
 
        temp = ioread32(hcr_base + HCONTROL);
        temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
+       temp |= HCONTROL_PMP_ATTACHED;
        iowrite32(temp, hcr_base + HCONTROL);
 
        temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, 0, 1, 500);
 
        if (!(temp & ONLINE)) {
                ata_port_printk(ap, KERN_ERR,
-                               "Softreset failed, not on-lined\n");
+                               "Hardreset failed, not on-lined\n");
                goto err;
        }
 
-       DPRINTK("softreset, controller off-lined & on-lined\n");
+       DPRINTK("hardreset, controller off-lined & on-lined\n");
        VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
        VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
 
@@ -803,7 +789,8 @@ try_offline_again:
                ata_port_printk(ap, KERN_WARNING,
                                "No Device OR PHYRDY change,Hstatus = 0x%x\n",
                                ioread32(hcr_base + HSTATUS));
-               goto err;
+               *class = ATA_DEV_NONE;
+               return 0;
        }
 
        /*
@@ -815,11 +802,45 @@ try_offline_again:
 
        if ((temp & 0xFF) != 0x18) {
                ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
-               goto err;
+               *class = ATA_DEV_NONE;
+               goto do_followup_srst;
        } else {
                ata_port_printk(ap, KERN_INFO,
                                "Signature Update detected @ %d msecs\n",
                                jiffies_to_msecs(jiffies - start_jiffies));
+               *class = sata_fsl_dev_classify(ap);
+               return 0;
+       }
+
+do_followup_srst:
+       /*
+        * request libATA to perform follow-up softreset
+        */
+       return -EAGAIN;
+
+err:
+       return -EIO;
+}
+
+static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
+                                       unsigned long deadline)
+{
+       struct ata_port *ap = link->ap;
+       struct sata_fsl_port_priv *pp = ap->private_data;
+       struct sata_fsl_host_priv *host_priv = ap->host->private_data;
+       void __iomem *hcr_base = host_priv->hcr_base;
+       int pmp = sata_srst_pmp(link);
+       u32 temp;
+       struct ata_taskfile tf;
+       u8 *cfis;
+       u32 Serror;
+
+       DPRINTK("in xx_softreset\n");
+
+       if (ata_link_offline(link)) {
+               DPRINTK("PHY reports no device\n");
+               *class = ATA_DEV_NONE;
+               return 0;
        }
 
        /*
@@ -837,10 +858,10 @@ try_offline_again:
 
        /* device reset/SRST is a control register update FIS, uses tag0 */
        sata_fsl_setup_cmd_hdr_entry(pp, 0,
-                                    SRST_CMD | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
+               SRST_CMD | CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
 
        tf.ctl |= ATA_SRST;     /* setup SRST bit in taskfile control reg */
-       ata_tf_to_fis(&tf, 0, 0, cfis);
+       ata_tf_to_fis(&tf, pmp, 0, cfis);
 
        DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
                cfis[0], cfis[1], cfis[2], cfis[3]);
@@ -855,6 +876,8 @@ try_offline_again:
                ioread32(CA + hcr_base), ioread32(CC + hcr_base));
 
        iowrite32(0xFFFF, CC + hcr_base);
+       if (pmp != SATA_PMP_CTRL_PORT)
+               iowrite32(pmp, CQPMP + hcr_base);
        iowrite32(1, CQ + hcr_base);
 
        temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
@@ -865,7 +888,7 @@ try_offline_again:
                        ioread32(CQ + hcr_base),
                        ioread32(CA + hcr_base), ioread32(CC + hcr_base));
 
-               sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
+               sata_fsl_scr_read(&ap->link, SCR_ERROR, &Serror);
 
                DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
                DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
@@ -883,11 +906,14 @@ try_offline_again:
         * using ATA signature D2H register FIS to the host controller.
         */
 
-       sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
+       sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE,
+                                     0, 0, 5);
 
        tf.ctl &= ~ATA_SRST;    /* 2nd H2D Ctl. register FIS */
-       ata_tf_to_fis(&tf, 0, 0, cfis);
+       ata_tf_to_fis(&tf, pmp, 0, cfis);
 
+       if (pmp != SATA_PMP_CTRL_PORT)
+               iowrite32(pmp, CQPMP + hcr_base);
        iowrite32(1, CQ + hcr_base);
        msleep(150);            /* ?? */
 
@@ -928,10 +954,8 @@ static void sata_fsl_error_handler(struct ata_port *ap)
 {
 
        DPRINTK("in xx_error_handler\n");
+       sata_pmp_error_handler(ap);
 
-       /* perform recovery */
-       ata_do_eh(ap, ata_std_prereset, sata_fsl_softreset, sata_std_hardreset,
-                 ata_std_postreset);
 }
 
 static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
@@ -945,40 +969,58 @@ static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
        }
 }
 
-static void sata_fsl_irq_clear(struct ata_port *ap)
-{
-       /* unused */
-}
-
 static void sata_fsl_error_intr(struct ata_port *ap)
 {
-       struct ata_link *link = &ap->link;
-       struct ata_eh_info *ehi = &link->eh_info;
        struct sata_fsl_host_priv *host_priv = ap->host->private_data;
        void __iomem *hcr_base = host_priv->hcr_base;
-       u32 hstatus, dereg, cereg = 0, SError = 0;
+       u32 hstatus, dereg=0, cereg = 0, SError = 0;
        unsigned int err_mask = 0, action = 0;
-       struct ata_queued_cmd *qc;
-       int freeze = 0;
+       int freeze = 0, abort=0;
+       struct ata_link *link = NULL;
+       struct ata_queued_cmd *qc = NULL;
+       struct ata_eh_info *ehi;
 
        hstatus = ioread32(hcr_base + HSTATUS);
        cereg = ioread32(hcr_base + CE);
 
+       /* first, analyze and record host port events */
+       link = &ap->link;
+       ehi = &link->eh_info;
        ata_ehi_clear_desc(ehi);
 
        /*
         * Handle & Clear SError
         */
 
-       sata_fsl_scr_read(ap, SCR_ERROR, &SError);
-       if (unlikely(SError & 0xFFFF0000)) {
-               sata_fsl_scr_write(ap, SCR_ERROR, SError);
-               err_mask |= AC_ERR_ATA_BUS;
-       }
+       sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
+       if (unlikely(SError & 0xFFFF0000))
+               sata_fsl_scr_write(&ap->link, SCR_ERROR, SError);
 
        DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
                hstatus, cereg, ioread32(hcr_base + DE), SError);
 
+       /* handle fatal errors */
+       if (hstatus & FATAL_ERROR_DECODE) {
+               ehi->err_mask |= AC_ERR_ATA_BUS;
+               ehi->action |= ATA_EH_SOFTRESET;
+
+               freeze = 1;
+       }
+
+       /* Handle SDB FIS receive & notify update */
+       if (hstatus & INT_ON_SNOTIFY_UPDATE)
+               sata_async_notification(ap);
+
+       /* Handle PHYRDY change notification */
+       if (hstatus & INT_ON_PHYRDY_CHG) {
+               DPRINTK("SATA FSL: PHYRDY change indication\n");
+
+               /* Setup a soft-reset EH action */
+               ata_ehi_hotplugged(ehi);
+               ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
+               freeze = 1;
+       }
+
        /* handle single device errors */
        if (cereg) {
                /*
@@ -990,67 +1032,68 @@ static void sata_fsl_error_intr(struct ata_port *ap)
                 * clearing the device error, we can issue commands to the
                 * device to interrogate it to find the source of the error.
                 */
-               dereg = ioread32(hcr_base + DE);
-               iowrite32(dereg, hcr_base + DE);
-               iowrite32(cereg, hcr_base + CE);
+               abort = 1;
 
                DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
                        ioread32(hcr_base + CE), ioread32(hcr_base + DE));
-               /*
-                * We should consider this as non fatal error, and TF must
-                * be updated as done below.
-                */
 
-               err_mask |= AC_ERR_DEV;
-       }
-
-       /* handle fatal errors */
-       if (hstatus & FATAL_ERROR_DECODE) {
-               err_mask |= AC_ERR_ATA_BUS;
-               action |= ATA_EH_SOFTRESET;
-               /* how will fatal error interrupts be completed ?? */
-               freeze = 1;
-       }
-
-       /* Handle PHYRDY change notification */
-       if (hstatus & INT_ON_PHYRDY_CHG) {
-               DPRINTK("SATA FSL: PHYRDY change indication\n");
-
-               /* Setup a soft-reset EH action */
-               ata_ehi_hotplugged(ehi);
-               freeze = 1;
+               /* find out the offending link and qc */
+               if (ap->nr_pmp_links) {
+                       dereg = ioread32(hcr_base + DE);
+                       iowrite32(dereg, hcr_base + DE);
+                       iowrite32(cereg, hcr_base + CE);
+
+                       if (dereg < ap->nr_pmp_links) {
+                               link = &ap->pmp_link[dereg];
+                               ehi = &link->eh_info;
+                               qc = ata_qc_from_tag(ap, link->active_tag);
+                               /*
+                                * We should consider this as non fatal error,
+                                 * and TF must be updated as done below.
+                                */
+
+                               err_mask |= AC_ERR_DEV;
+
+                       } else {
+                               err_mask |= AC_ERR_HSM;
+                               action |= ATA_EH_HARDRESET;
+                               freeze = 1;
+                       }
+               } else {
+                       dereg = ioread32(hcr_base + DE);
+                       iowrite32(dereg, hcr_base + DE);
+                       iowrite32(cereg, hcr_base + CE);
+
+                       qc = ata_qc_from_tag(ap, link->active_tag);
+                       /*
+                        * We should consider this as non fatal error,
+                         * and TF must be updated as done below.
+                       */
+                       err_mask |= AC_ERR_DEV;
+               }
        }
 
        /* record error info */
-       qc = ata_qc_from_tag(ap, link->active_tag);
-
-       if (qc) {
-               sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
+       if (qc)
                qc->err_mask |= err_mask;
-       else
+       else
                ehi->err_mask |= err_mask;
 
        ehi->action |= action;
-       ehi->serror |= SError;
 
        /* freeze or abort */
        if (freeze)
                ata_port_freeze(ap);
-       else
-               ata_port_abort(ap);
-}
-
-static void sata_fsl_qc_complete(struct ata_queued_cmd *qc)
-{
-       if (qc->flags & ATA_QCFLAG_RESULT_TF) {
-               DPRINTK("xx_qc_complete called\n");
-               sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
+       else if (abort) {
+               if (qc)
+                       ata_link_abort(qc->dev->link);
+               else
+                       ata_port_abort(ap);
        }
 }
 
 static void sata_fsl_host_intr(struct ata_port *ap)
 {
-       struct ata_link *link = &ap->link;
        struct sata_fsl_host_priv *host_priv = ap->host->private_data;
        void __iomem *hcr_base = host_priv->hcr_base;
        u32 hstatus, qc_active = 0;
@@ -1059,12 +1102,11 @@ static void sata_fsl_host_intr(struct ata_port *ap)
 
        hstatus = ioread32(hcr_base + HSTATUS);
 
-       sata_fsl_scr_read(ap, SCR_ERROR, &SError);
+       sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
 
        if (unlikely(SError & 0xFFFF0000)) {
                DPRINTK("serror @host_intr : 0x%x\n", SError);
                sata_fsl_error_intr(ap);
-
        }
 
        if (unlikely(hstatus & INT_ON_ERROR)) {
@@ -1073,10 +1115,19 @@ static void sata_fsl_host_intr(struct ata_port *ap)
                return;
        }
 
-       if (link->sactive) {    /* only true for NCQ commands */
+       /* Read command completed register */
+       qc_active = ioread32(hcr_base + CC);
+
+       VPRINTK("Status of all queues :\n");
+       VPRINTK("qc_active/CC = 0x%x, CA = 0x%x, CE=0x%x,CQ=0x%x,apqa=0x%x\n",
+               qc_active,
+               ioread32(hcr_base + CA),
+               ioread32(hcr_base + CE),
+               ioread32(hcr_base + CQ),
+               ap->qc_active);
+
+       if (qc_active & ap->qc_active) {
                int i;
-               /* Read command completed register */
-               qc_active = ioread32(hcr_base + CC);
                /* clear CC bit, this will also complete the interrupt */
                iowrite32(qc_active, hcr_base + CC);
 
@@ -1089,7 +1140,6 @@ static void sata_fsl_host_intr(struct ata_port *ap)
                        if (qc_active & (1 << i)) {
                                qc = ata_qc_from_tag(ap, i);
                                if (qc) {
-                                       sata_fsl_qc_complete(qc);
                                        ata_qc_complete(qc);
                                }
                                DPRINTK
@@ -1100,21 +1150,21 @@ static void sata_fsl_host_intr(struct ata_port *ap)
                }
                return;
 
-       } else if (ap->qc_active) {
+       } else if ((ap->qc_active & (1 << ATA_TAG_INTERNAL))) {
                iowrite32(1, hcr_base + CC);
-               qc = ata_qc_from_tag(ap, link->active_tag);
+               qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
 
-               DPRINTK("completing non-ncq cmd, tag=%d,CC=0x%x\n",
-                       link->active_tag, ioread32(hcr_base + CC));
+               DPRINTK("completing non-ncq cmd, CC=0x%x\n",
+                        ioread32(hcr_base + CC));
 
                if (qc) {
-                       sata_fsl_qc_complete(qc);
                        ata_qc_complete(qc);
                }
        } else {
                /* Spurious Interrupt!! */
                DPRINTK("spurious interrupt!!, CC = 0x%x\n",
                        ioread32(hcr_base + CC));
+               iowrite32(qc_active, hcr_base + CC);
                return;
        }
 }
@@ -1190,9 +1240,6 @@ static int sata_fsl_init_controller(struct ata_host *host)
        iowrite32(0x00000FFFF, hcr_base + CE);
        iowrite32(0x00000FFFF, hcr_base + DE);
 
-       /* initially assuming no Port multiplier, set CQPMP to 0 */
-       iowrite32(0x0, hcr_base + CQPMP);
-
        /*
         * host controller will be brought on-line, during xx_port_start()
         * callback, that should also initiate the OOB, COMINIT sequence
@@ -1208,53 +1255,43 @@ static int sata_fsl_init_controller(struct ata_host *host)
  * scsi mid-layer and libata interface structures
  */
 static struct scsi_host_template sata_fsl_sht = {
-       .module = THIS_MODULE,
-       .name = "sata_fsl",
-       .ioctl = ata_scsi_ioctl,
-       .queuecommand = ata_scsi_queuecmd,
-       .change_queue_depth = ata_scsi_change_queue_depth,
+       ATA_NCQ_SHT("sata_fsl"),
        .can_queue = SATA_FSL_QUEUE_DEPTH,
-       .this_id = ATA_SHT_THIS_ID,
        .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
-       .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
-       .emulated = ATA_SHT_EMULATED,
-       .use_clustering = ATA_SHT_USE_CLUSTERING,
-       .proc_name = "sata_fsl",
        .dma_boundary = ATA_DMA_BOUNDARY,
-       .slave_configure = ata_scsi_slave_config,
-       .slave_destroy = ata_scsi_slave_destroy,
-       .bios_param = ata_std_bios_param,
 };
 
-static const struct ata_port_operations sata_fsl_ops = {
-       .check_status = sata_fsl_check_status,
-       .check_altstatus = sata_fsl_check_status,
-       .dev_select = ata_noop_dev_select,
-
-       .tf_read = sata_fsl_tf_read,
+static struct ata_port_operations sata_fsl_ops = {
+       .inherits               = &sata_pmp_port_ops,
 
+       .qc_defer = ata_std_qc_defer,
        .qc_prep = sata_fsl_qc_prep,
        .qc_issue = sata_fsl_qc_issue,
-       .irq_clear = sata_fsl_irq_clear,
+       .qc_fill_rtf = sata_fsl_qc_fill_rtf,
 
        .scr_read = sata_fsl_scr_read,
        .scr_write = sata_fsl_scr_write,
 
        .freeze = sata_fsl_freeze,
        .thaw = sata_fsl_thaw,
+       .softreset = sata_fsl_softreset,
+       .hardreset = sata_fsl_hardreset,
+       .pmp_softreset = sata_fsl_softreset,
        .error_handler = sata_fsl_error_handler,
        .post_internal_cmd = sata_fsl_post_internal_cmd,
 
        .port_start = sata_fsl_port_start,
        .port_stop = sata_fsl_port_stop,
+
+       .pmp_attach = sata_fsl_pmp_attach,
+       .pmp_detach = sata_fsl_pmp_detach,
 };
 
 static const struct ata_port_info sata_fsl_port_info[] = {
        {
         .flags = SATA_FSL_HOST_FLAGS,
-        .link_flags = SATA_FSL_HOST_LFLAGS,
-        .pio_mask = 0x1f,      /* pio 0-4 */
-        .udma_mask = 0x7f,     /* udma 0-6 */
+        .pio_mask = ATA_PIO4,
+        .udma_mask = ATA_UDMA6,
         .port_ops = &sata_fsl_ops,
         },
 };
@@ -1262,12 +1299,11 @@ static const struct ata_port_info sata_fsl_port_info[] = {
 static int sata_fsl_probe(struct of_device *ofdev,
                        const struct of_device_id *match)
 {
-       int retval = 0;
+       int retval = -ENXIO;
        void __iomem *hcr_base = NULL;
        void __iomem *ssr_base = NULL;
        void __iomem *csr_base = NULL;
        struct sata_fsl_host_priv *host_priv = NULL;
-       struct resource *r;
        int irq;
        struct ata_host *host;
 
@@ -1277,9 +1313,7 @@ static int sata_fsl_probe(struct of_device *ofdev,
        dev_printk(KERN_INFO, &ofdev->dev,
                   "Sata FSL Platform/CSB Driver init\n");
 
-       r = kmalloc(sizeof(struct resource), GFP_KERNEL);
-
-       hcr_base = of_iomap(ofdev->node, 0);
+       hcr_base = of_iomap(ofdev->dev.of_node, 0);
        if (!hcr_base)
                goto error_exit_with_cleanup;
 
@@ -1298,7 +1332,7 @@ static int sata_fsl_probe(struct of_device *ofdev,
        host_priv->ssr_base = ssr_base;
        host_priv->csr_base = csr_base;
 
-       irq = irq_of_parse_and_map(ofdev->node, 0);
+       irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
        if (irq < 0) {
                dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
                goto error_exit_with_cleanup;
@@ -1311,11 +1345,6 @@ static int sata_fsl_probe(struct of_device *ofdev,
        /* host->iomap is not used currently */
        host->private_data = host_priv;
 
-       /* setup port(s) */
-
-       host->ports[0]->ioaddr.cmd_addr = host_priv->hcr_base;
-       host->ports[0]->ioaddr.scr_addr = host_priv->ssr_base;
-
        /* initialize host controller */
        sata_fsl_init_controller(host);
 
@@ -1357,12 +1386,40 @@ static int sata_fsl_remove(struct of_device *ofdev)
        return 0;
 }
 
+#ifdef CONFIG_PM
+static int sata_fsl_suspend(struct of_device *op, pm_message_t state)
+{
+       struct ata_host *host = dev_get_drvdata(&op->dev);
+       return ata_host_suspend(host, state);
+}
+
+static int sata_fsl_resume(struct of_device *op)
+{
+       struct ata_host *host = dev_get_drvdata(&op->dev);
+       struct sata_fsl_host_priv *host_priv = host->private_data;
+       int ret;
+       void __iomem *hcr_base = host_priv->hcr_base;
+       struct ata_port *ap = host->ports[0];
+       struct sata_fsl_port_priv *pp = ap->private_data;
+
+       ret = sata_fsl_init_controller(host);
+       if (ret) {
+               dev_printk(KERN_ERR, &op->dev,
+                       "Error initialize hardware\n");
+               return ret;
+       }
+
+       /* Recovery the CHBA register in host controller cmd register set */
+       iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
+
+       ata_host_resume(host);
+       return 0;
+}
+#endif
+
 static struct of_device_id fsl_sata_match[] = {
        {
-               .compatible = "fsl,mpc8315-sata",
-       },
-       {
-               .compatible = "fsl,mpc8379-sata",
+               .compatible = "fsl,pq-sata",
        },
        {},
 };
@@ -1374,6 +1431,10 @@ static struct of_platform_driver fsl_sata_driver = {
        .match_table    = fsl_sata_match,
        .probe          = sata_fsl_probe,
        .remove         = sata_fsl_remove,
+#ifdef CONFIG_PM
+       .suspend        = sata_fsl_suspend,
+       .resume         = sata_fsl_resume,
+#endif
 };
 
 static int __init sata_fsl_init(void)