tree-wide: fix assorted typos all over the place
[safe/jmp/linux-2.6] / drivers / ata / sata_fsl.c
index c47f2d4..0d9d2f2 100644 (file)
@@ -34,8 +34,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),
 
        SATA_FSL_MAX_CMDS       = SATA_FSL_QUEUE_DEPTH,
        SATA_FSL_CMD_HDR_SIZE   = 16,   /* 4 DWORDS */
@@ -44,9 +43,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.
         */
@@ -206,6 +205,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 +245,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;
 };
 
 /*
@@ -269,7 +258,7 @@ struct sata_fsl_host_priv {
 };
 
 static inline unsigned int sata_fsl_tag(unsigned int tag,
-                                       void __iomem * hcr_base)
+                                       void __iomem *hcr_base)
 {
        /* We let libATA core do actual (queue) tag allocation */
 
@@ -308,7 +297,7 @@ static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
        pp->cmdslot[tag].prde_fis_len =
            cpu_to_le32((num_prde << 16) | (fis_len << 2));
        pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
-       pp->cmdslot[tag].desc_info = cpu_to_le32((desc_info | (tag & 0x1F)));
+       pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
 
        VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
                pp->cmdslot[tag].cda,
@@ -318,14 +307,14 @@ static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
 }
 
 static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
-                                    u32 * ttl, dma_addr_t cmd_desc_paddr)
+                                    u32 *ttl, dma_addr_t cmd_desc_paddr)
 {
        struct scatterlist *sg;
        unsigned int num_prde = 0;
        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;
@@ -333,29 +322,31 @@ static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
        struct prde *prd_ptr_to_indirect_ext = NULL;
        unsigned indirect_ext_segment_sz = 0;
        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;
 
-       ata_for_each_sg(sg, qc) {
+       for_each_sg(qc->sg, sg, qc->n_elem, si) {
                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);
@@ -398,7 +389,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;
@@ -406,7 +397,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]);
@@ -417,7 +408,7 @@ static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
        }
 
        /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
-       if (is_atapi_taskfile(&qc->tf)) {
+       if (ata_is_atapi(qc->tf.protocol)) {
                desc_info |= ATAPI_CMD;
                memset((void *)&cd->acmd, 0, 32);
                memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
@@ -449,6 +440,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);
 
@@ -458,15 +451,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;
 
@@ -487,10 +495,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;
 
@@ -521,7 +529,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);
@@ -553,43 +562,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;
@@ -600,21 +602,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;
        }
@@ -652,18 +642,20 @@ static int sata_fsl_port_start(struct ata_port *ap)
        VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
        VPRINTK("CHBA  = 0x%x\n", ioread32(hcr_base + CHBA));
 
+#ifdef CONFIG_MPC8315_DS
        /*
         * 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
 
        return 0;
 }
@@ -691,7 +683,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);
 }
 
@@ -716,26 +707,35 @@ static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
        return ata_dev_classify(&tf);
 }
 
+static int sata_fsl_prereset(struct ata_link *link, unsigned long deadline)
+{
+       /* FIXME: Never skip softreset, sata_fsl_softreset() is
+        * combination of soft and hard resets.  sata_fsl_softreset()
+        * needs to be splitted into soft and hard resets.
+        */
+       return 0;
+}
+
 static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
-                             unsigned long deadline)
+                                       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;
        int i = 0;
-       struct ata_queued_cmd qc;
-       u8 *buf;
-       dma_addr_t dma_address;
-       struct scatterlist *sg;
        unsigned long start_jiffies;
 
        DPRINTK("in xx_softreset\n");
 
+       if (pmp != SATA_PMP_CTRL_PORT)
+               goto issue_srst;
+
 try_offline_again:
        /*
         * Force host controller to go off-line, aborting current operations
@@ -779,6 +779,7 @@ 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);
@@ -804,7 +805,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;
+               goto out;
        }
 
        /*
@@ -816,7 +818,8 @@ try_offline_again:
 
        if ((temp & 0xFF) != 0x18) {
                ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
-               goto err;
+               *class = ATA_DEV_NONE;
+               goto out;
        } else {
                ata_port_printk(ap, KERN_INFO,
                                "Signature Update detected @ %d msecs\n",
@@ -831,20 +834,18 @@ try_offline_again:
         * reached here, we can send a command to the target device
         */
 
-       if (link->sactive)
-               goto skip_srst_do_ncq_error_handling;
-
+issue_srst:
        DPRINTK("Sending SRST/device reset\n");
 
        ata_tf_init(link->device, &tf);
-       cfis = (u8 *) & pp->cmdentry->cfis;
+       cfis = (u8 *) &pp->cmdentry->cfis;
 
        /* 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]);
@@ -869,7 +870,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));
@@ -887,11 +888,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);            /* ?? */
 
@@ -901,69 +905,6 @@ try_offline_again:
         * command bit of the CCreg
         */
        iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
-       goto check_device_signature;
-
-skip_srst_do_ncq_error_handling:
-
-       VPRINTK("Sending read log ext(10h) command\n");
-
-       memset(&qc, 0, sizeof(struct ata_queued_cmd));
-       ata_tf_init(link->device, &tf);
-
-       tf.command = ATA_CMD_READ_LOG_EXT;
-       tf.lbal = ATA_LOG_SATA_NCQ;
-       tf.nsect = 1;
-       tf.hob_nsect = 0;
-       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
-       tf.protocol = ATA_PROT_PIO;
-
-       qc.tag = ATA_TAG_INTERNAL;
-       qc.scsicmd = NULL;
-       qc.ap = ap;
-       qc.dev = link->device;
-
-       qc.tf = tf;
-       qc.flags |= ATA_QCFLAG_RESULT_TF;
-       qc.dma_dir = DMA_FROM_DEVICE;
-
-       buf = ap->sector_buf;
-       ata_sg_init_one(&qc, buf, 1 * ATA_SECT_SIZE);
-
-       /*
-        * Need to DMA-map the memory buffer associated with the command
-        */
-
-       sg = qc.__sg;
-       dma_address = dma_map_single(ap->dev, qc.buf_virt,
-                                    sg->length, DMA_FROM_DEVICE);
-
-       sg_dma_address(sg) = dma_address;
-       sg_dma_len(sg) = sg->length;
-
-       VPRINTK("EH, addr = 0x%x, len = 0x%x\n", dma_address, sg->length);
-
-       sata_fsl_qc_prep(&qc);
-       sata_fsl_qc_issue(&qc);
-
-       temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
-       if (temp & 0x1) {
-               VPRINTK("READ_LOG_EXT_10H issue failed\n");
-
-               VPRINTK("READ_LOG@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
-                       ioread32(CQ + hcr_base),
-                       ioread32(CA + hcr_base), ioread32(CC + hcr_base));
-
-               sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
-
-               VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
-               VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
-               VPRINTK("Serror = 0x%x\n", Serror);
-               goto err;
-       }
-
-       iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
-
-      check_device_signature:
 
        DPRINTK("SATA FSL : Now checking device signature\n");
 
@@ -985,32 +926,19 @@ skip_srst_do_ncq_error_handling:
                VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
        }
 
+out:
        return 0;
 
 err:
        return -EIO;
 }
 
-static int sata_fsl_hardreset(struct ata_port *ap, unsigned int *class,
-                             unsigned long deadline)
-{
-       int retval;
-
-       retval = sata_std_hardreset(ap, class, deadline);
-
-       DPRINTK("SATA FSL : in xx_hardreset, retval = 0x%d\n", retval);
-
-       return retval;
-}
-
 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_fsl_hardreset,
-                 ata_std_postreset);
 }
 
 static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
@@ -1024,70 +952,54 @@ 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);
+       sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
        if (unlikely(SError & 0xFFFF0000)) {
-               sata_fsl_scr_write(ap, SCR_ERROR, SError);
-               err_mask |= AC_ERR_ATA_BUS;
+               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 single device errors */
-       if (cereg) {
-               /*
-                * clear the command error, also clears queue to the device
-                * in error, and we can (re)issue commands to this device.
-                * When a device is in error all commands queued into the
-                * host controller and at the device are considered aborted
-                * and the queue for that device is stopped. Now, after
-                * 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);
+       /* handle fatal errors */
+       if (hstatus & FATAL_ERROR_DECODE) {
+               ehi->err_mask |= AC_ERR_ATA_BUS;
+               ehi->action |= ATA_EH_SOFTRESET;
 
-               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.
+                * Ignore serror in case of fatal errors as we always want
+                * to do a soft-reset of the FSL SATA controller. Analyzing
+                * serror may cause libata to schedule a hard-reset action,
+                * and hard-reset currently does not do controller
+                * offline/online, causing command timeouts and leads to an
+                * un-recoverable state, hence make libATA ignore
+                * autopsy in case of fatal errors.
                 */
 
-               err_mask |= AC_ERR_DEV;
-       }
+               ehi->flags |= ATA_EHI_NO_AUTOPSY;
 
-       /* 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;
        }
 
@@ -1097,39 +1009,83 @@ static void sata_fsl_error_intr(struct ata_port *ap)
 
                /* Setup a soft-reset EH action */
                ata_ehi_hotplugged(ehi);
+               ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
                freeze = 1;
        }
 
-       /* record error info */
-       qc = ata_qc_from_tag(ap, link->active_tag);
+       /* handle single device errors */
+       if (cereg) {
+               /*
+                * clear the command error, also clears queue to the device
+                * in error, and we can (re)issue commands to this device.
+                * When a device is in error all commands queued into the
+                * host controller and at the device are considered aborted
+                * and the queue for that device is stopped. Now, after
+                * clearing the device error, we can issue commands to the
+                * device to interrogate it to find the source of the error.
+                */
+               abort = 1;
+
+               DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
+                       ioread32(hcr_base + CE), ioread32(hcr_base + DE));
 
+               /* 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 */
        if (qc) {
-               sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
                qc->err_mask |= err_mask;
        } 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;
@@ -1138,7 +1094,7 @@ 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);
@@ -1152,10 +1108,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);
 
@@ -1168,7 +1133,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
@@ -1179,21 +1143,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;
        }
 }
@@ -1269,9 +1233,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
@@ -1287,57 +1248,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,
-#ifdef CONFIG_PM
-       .suspend = ata_scsi_device_suspend,
-       .resume = ata_scsi_device_resume,
-#endif
 };
 
-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,
+       .prereset = sata_fsl_prereset,
+       .softreset = sata_fsl_softreset,
+       .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,
         },
 };
@@ -1345,12 +1292,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;
 
@@ -1360,8 +1306,6 @@ 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);
        if (!hcr_base)
                goto error_exit_with_cleanup;
@@ -1394,11 +1338,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);
 
@@ -1440,12 +1379,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",
        },
        {},
 };
@@ -1457,6 +1424,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)