intelfb: add preliminary i2c support
[safe/jmp/linux-2.6] / drivers / scsi / sata_mv.c
index f695d0b..4a71578 100644 (file)
@@ -2,6 +2,7 @@
  * sata_mv.c - Marvell SATA support
  *
  * Copyright 2005: EMC Corporation, all rights reserved.
+ * Copyright 2005 Red Hat, Inc.  All rights reserved.
  *
  * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  *
@@ -36,7 +37,7 @@
 #include <asm/io.h>
 
 #define DRV_NAME       "sata_mv"
-#define DRV_VERSION    "0.25"
+#define DRV_VERSION    "0.7"
 
 enum {
        /* BAR's are enumerated in terms of pci_resource_start() terms */
@@ -49,6 +50,12 @@ enum {
 
        MV_PCI_REG_BASE         = 0,
        MV_IRQ_COAL_REG_BASE    = 0x18000,      /* 6xxx part only */
+       MV_IRQ_COAL_CAUSE               = (MV_IRQ_COAL_REG_BASE + 0x08),
+       MV_IRQ_COAL_CAUSE_LO            = (MV_IRQ_COAL_REG_BASE + 0x88),
+       MV_IRQ_COAL_CAUSE_HI            = (MV_IRQ_COAL_REG_BASE + 0x8c),
+       MV_IRQ_COAL_THRESHOLD           = (MV_IRQ_COAL_REG_BASE + 0xcc),
+       MV_IRQ_COAL_TIME_THRESHOLD      = (MV_IRQ_COAL_REG_BASE + 0xd0),
+
        MV_SATAHC0_REG_BASE     = 0x20000,
        MV_FLASH_CTL            = 0x1046c,
        MV_GPIO_PORT_CTL        = 0x104f0,
@@ -85,7 +92,8 @@ enum {
        MV_FLAG_DUAL_HC         = (1 << 30),  /* two SATA Host Controllers */
        MV_FLAG_IRQ_COALESCE    = (1 << 29),  /* IRQ coalescing capability */
        MV_COMMON_FLAGS         = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
-                                  ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO),
+                                  ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
+                                  ATA_FLAG_NO_ATAPI | ATA_FLAG_PIO_POLLING),
        MV_6XXX_FLAGS           = MV_FLAG_IRQ_COALESCE,
 
        CRQB_FLAG_READ          = (1 << 0),
@@ -158,6 +166,9 @@ enum {
        PHY_MODE3               = 0x310,
        PHY_MODE4               = 0x314,
        PHY_MODE2               = 0x330,
+       MV5_PHY_MODE            = 0x74,
+       MV5_LT_MODE             = 0x30,
+       MV5_PHY_CTL             = 0x0C,
        SATA_INTERFACE_CTL      = 0x050,
 
        MV_M2_PREAMP_MASK       = 0x7e0,
@@ -214,6 +225,7 @@ enum {
        EDMA_DS                 = (1 << 1),
        ATA_RST                 = (1 << 2),
 
+       EDMA_IORDY_TMOUT        = 0x34,
        EDMA_ARB_CFG            = 0x38,
 
        /* Host private flags (hp_flags) */
@@ -222,14 +234,20 @@ enum {
        MV_HP_ERRATA_50XXB2     = (1 << 2),
        MV_HP_ERRATA_60X1B2     = (1 << 3),
        MV_HP_ERRATA_60X1C0     = (1 << 4),
-       MV_HP_50XX              = (1 << 5),
+       MV_HP_ERRATA_XX42A0     = (1 << 5),
+       MV_HP_50XX              = (1 << 6),
+       MV_HP_GEN_IIE           = (1 << 7),
 
        /* Port private flags (pp_flags) */
        MV_PP_FLAG_EDMA_EN      = (1 << 0),
        MV_PP_FLAG_EDMA_DS_ACT  = (1 << 1),
 };
 
+#define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
 #define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
+#define IS_GEN_I(hpriv) IS_50XX(hpriv)
+#define IS_GEN_II(hpriv) IS_60XX(hpriv)
+#define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
 
 enum {
        /* Our DMA boundary is determined by an ePRD being unable to handle
@@ -248,29 +266,39 @@ enum chip_type {
        chip_5080,
        chip_604x,
        chip_608x,
+       chip_6042,
+       chip_7042,
 };
 
 /* Command ReQuest Block: 32B */
 struct mv_crqb {
-       u32                     sg_addr;
-       u32                     sg_addr_hi;
-       u16                     ctrl_flags;
-       u16                     ata_cmd[11];
+       __le32                  sg_addr;
+       __le32                  sg_addr_hi;
+       __le16                  ctrl_flags;
+       __le16                  ata_cmd[11];
+};
+
+struct mv_crqb_iie {
+       __le32                  addr;
+       __le32                  addr_hi;
+       __le32                  flags;
+       __le32                  len;
+       __le32                  ata_cmd[4];
 };
 
 /* Command ResPonse Block: 8B */
 struct mv_crpb {
-       u16                     id;
-       u16                     flags;
-       u32                     tmstmp;
+       __le16                  id;
+       __le16                  flags;
+       __le32                  tmstmp;
 };
 
 /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
 struct mv_sg {
-       u32                     addr;
-       u32                     flags_size;
-       u32                     addr_hi;
-       u32                     reserved;
+       __le32                  addr;
+       __le32                  flags_size;
+       __le32                  addr_hi;
+       __le32                  reserved;
 };
 
 struct mv_port_priv {
@@ -280,9 +308,6 @@ struct mv_port_priv {
        dma_addr_t              crpb_dma;
        struct mv_sg            *sg_tbl;
        dma_addr_t              sg_tbl_dma;
-
-       unsigned                req_producer;           /* cp of req_in_ptr */
-       unsigned                rsp_consumer;           /* cp of rsp_out_ptr */
        u32                     pp_flags;
 };
 
@@ -293,11 +318,13 @@ struct mv_port_signal {
 
 struct mv_host_priv;
 struct mv_hw_ops {
-       void (*phy_errata)(struct ata_port *ap);
+       void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
+                          unsigned int port);
        void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
        void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
                           void __iomem *mmio);
-       int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio);
+       int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
+                       unsigned int n_hc);
        void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
        void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
 };
@@ -311,54 +338,63 @@ struct mv_host_priv {
 static void mv_irq_clear(struct ata_port *ap);
 static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
 static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
+static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
+static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
 static void mv_phy_reset(struct ata_port *ap);
+static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
 static void mv_host_stop(struct ata_host_set *host_set);
 static int mv_port_start(struct ata_port *ap);
 static void mv_port_stop(struct ata_port *ap);
 static void mv_qc_prep(struct ata_queued_cmd *qc);
-static int mv_qc_issue(struct ata_queued_cmd *qc);
+static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
+static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
 static irqreturn_t mv_interrupt(int irq, void *dev_instance,
                                struct pt_regs *regs);
 static void mv_eng_timeout(struct ata_port *ap);
 static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
 
-static void mv5_phy_errata(struct ata_port *ap);
+static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
+                          unsigned int port);
 static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
 static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
                           void __iomem *mmio);
-static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio);
+static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
+                       unsigned int n_hc);
 static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
 static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
 
-static void mv6_phy_errata(struct ata_port *ap);
+static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
+                          unsigned int port);
 static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
 static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
                           void __iomem *mmio);
-static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio);
+static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
+                       unsigned int n_hc);
 static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
 static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
+static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
+                            unsigned int port_no);
+static void mv_stop_and_reset(struct ata_port *ap);
 
 static struct scsi_host_template mv_sht = {
        .module                 = THIS_MODULE,
        .name                   = DRV_NAME,
        .ioctl                  = ata_scsi_ioctl,
        .queuecommand           = ata_scsi_queuecmd,
-       .eh_strategy_handler    = ata_scsi_error,
        .can_queue              = MV_USE_Q_DEPTH,
        .this_id                = ATA_SHT_THIS_ID,
-       .sg_tablesize           = MV_MAX_SG_CT,
-       .max_sectors            = ATA_MAX_SECTORS,
+       .sg_tablesize           = MV_MAX_SG_CT / 2,
        .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
        .emulated               = ATA_SHT_EMULATED,
        .use_clustering         = ATA_SHT_USE_CLUSTERING,
        .proc_name              = DRV_NAME,
        .dma_boundary           = MV_DMA_BOUNDARY,
        .slave_configure        = ata_scsi_slave_config,
+       .slave_destroy          = ata_scsi_slave_destroy,
        .bios_param             = ata_std_bios_param,
-       .ordered_flush          = 1,
 };
 
-static const struct ata_port_operations mv_ops = {
+static const struct ata_port_operations mv5_ops = {
        .port_disable           = ata_port_disable,
 
        .tf_load                = ata_tf_load,
@@ -371,6 +407,62 @@ static const struct ata_port_operations mv_ops = {
 
        .qc_prep                = mv_qc_prep,
        .qc_issue               = mv_qc_issue,
+       .data_xfer              = ata_mmio_data_xfer,
+
+       .eng_timeout            = mv_eng_timeout,
+
+       .irq_handler            = mv_interrupt,
+       .irq_clear              = mv_irq_clear,
+
+       .scr_read               = mv5_scr_read,
+       .scr_write              = mv5_scr_write,
+
+       .port_start             = mv_port_start,
+       .port_stop              = mv_port_stop,
+       .host_stop              = mv_host_stop,
+};
+
+static const struct ata_port_operations mv6_ops = {
+       .port_disable           = ata_port_disable,
+
+       .tf_load                = ata_tf_load,
+       .tf_read                = ata_tf_read,
+       .check_status           = ata_check_status,
+       .exec_command           = ata_exec_command,
+       .dev_select             = ata_std_dev_select,
+
+       .phy_reset              = mv_phy_reset,
+
+       .qc_prep                = mv_qc_prep,
+       .qc_issue               = mv_qc_issue,
+       .data_xfer              = ata_mmio_data_xfer,
+
+       .eng_timeout            = mv_eng_timeout,
+
+       .irq_handler            = mv_interrupt,
+       .irq_clear              = mv_irq_clear,
+
+       .scr_read               = mv_scr_read,
+       .scr_write              = mv_scr_write,
+
+       .port_start             = mv_port_start,
+       .port_stop              = mv_port_stop,
+       .host_stop              = mv_host_stop,
+};
+
+static const struct ata_port_operations mv_iie_ops = {
+       .port_disable           = ata_port_disable,
+
+       .tf_load                = ata_tf_load,
+       .tf_read                = ata_tf_read,
+       .check_status           = ata_check_status,
+       .exec_command           = ata_exec_command,
+       .dev_select             = ata_std_dev_select,
+
+       .phy_reset              = mv_phy_reset,
+
+       .qc_prep                = mv_qc_prep_iie,
+       .qc_issue               = mv_qc_issue,
 
        .eng_timeout            = mv_eng_timeout,
 
@@ -385,34 +477,34 @@ static const struct ata_port_operations mv_ops = {
        .host_stop              = mv_host_stop,
 };
 
-static struct ata_port_info mv_port_info[] = {
+static const struct ata_port_info mv_port_info[] = {
        {  /* chip_504x */
                .sht            = &mv_sht,
                .host_flags     = MV_COMMON_FLAGS,
                .pio_mask       = 0x1f, /* pio0-4 */
-               .udma_mask      = 0,    /* 0x7f (udma0-6 disabled for now) */
-               .port_ops       = &mv_ops,
+               .udma_mask      = 0x7f, /* udma0-6 */
+               .port_ops       = &mv5_ops,
        },
        {  /* chip_508x */
                .sht            = &mv_sht,
                .host_flags     = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
                .pio_mask       = 0x1f, /* pio0-4 */
-               .udma_mask      = 0,    /* 0x7f (udma0-6 disabled for now) */
-               .port_ops       = &mv_ops,
+               .udma_mask      = 0x7f, /* udma0-6 */
+               .port_ops       = &mv5_ops,
        },
        {  /* chip_5080 */
                .sht            = &mv_sht,
                .host_flags     = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
                .pio_mask       = 0x1f, /* pio0-4 */
-               .udma_mask      = 0,    /* 0x7f (udma0-6 disabled for now) */
-               .port_ops       = &mv_ops,
+               .udma_mask      = 0x7f, /* udma0-6 */
+               .port_ops       = &mv5_ops,
        },
        {  /* chip_604x */
                .sht            = &mv_sht,
                .host_flags     = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
                .pio_mask       = 0x1f, /* pio0-4 */
                .udma_mask      = 0x7f, /* udma0-6 */
-               .port_ops       = &mv_ops,
+               .port_ops       = &mv6_ops,
        },
        {  /* chip_608x */
                .sht            = &mv_sht,
@@ -420,20 +512,34 @@ static struct ata_port_info mv_port_info[] = {
                                   MV_FLAG_DUAL_HC),
                .pio_mask       = 0x1f, /* pio0-4 */
                .udma_mask      = 0x7f, /* udma0-6 */
-               .port_ops       = &mv_ops,
+               .port_ops       = &mv6_ops,
+       },
+       {  /* chip_6042 */
+               .sht            = &mv_sht,
+               .host_flags     = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
+               .pio_mask       = 0x1f, /* pio0-4 */
+               .udma_mask      = 0x7f, /* udma0-6 */
+               .port_ops       = &mv_iie_ops,
+       },
+       {  /* chip_7042 */
+               .sht            = &mv_sht,
+               .host_flags     = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
+                                  MV_FLAG_DUAL_HC),
+               .pio_mask       = 0x1f, /* pio0-4 */
+               .udma_mask      = 0x7f, /* udma0-6 */
+               .port_ops       = &mv_iie_ops,
        },
 };
 
 static const struct pci_device_id mv_pci_tbl[] = {
-#if 0 /* unusably broken right now */
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_5080},
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
-#endif
 
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
+       {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6042), 0, 0, chip_6042},
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
        {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
 
@@ -467,6 +573,12 @@ static const struct mv_hw_ops mv6xxx_ops = {
 };
 
 /*
+ * module options
+ */
+static int msi;              /* Use PCI msi; either zero (off, default) or non-zero */
+
+
+/*
  * Functions
  */
 
@@ -481,11 +593,27 @@ static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
        return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
 }
 
+static inline unsigned int mv_hc_from_port(unsigned int port)
+{
+       return port >> MV_PORT_HC_SHIFT;
+}
+
+static inline unsigned int mv_hardport_from_port(unsigned int port)
+{
+       return port & MV_PORT_MASK;
+}
+
+static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
+                                                unsigned int port)
+{
+       return mv_hc_base(base, mv_hc_from_port(port));
+}
+
 static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
 {
-       return (mv_hc_base(base, port >> MV_PORT_HC_SHIFT) +
+       return  mv_hc_base_from_port(base, port) +
                MV_SATAHC_ARBTR_REG_SZ +
-               ((port & MV_PORT_MASK) * MV_PORT_REG_SZ));
+               (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
 }
 
 static inline void __iomem *mv_ap_base(struct ata_port *ap)
@@ -507,8 +635,8 @@ static void mv_irq_clear(struct ata_port *ap)
  *      @base: port base address
  *      @pp: port private data
  *
- *      Verify the local cache of the eDMA state is accurate with an
- *      assert.
+ *      Verify the local cache of the eDMA state is accurate with a
+ *      WARN_ON.
  *
  *      LOCKING:
  *      Inherited from caller.
@@ -519,15 +647,15 @@ static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
                writelfl(EDMA_EN, base + EDMA_CMD_OFS);
                pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
        }
-       assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
+       WARN_ON(!(EDMA_EN & readl(base + EDMA_CMD_OFS)));
 }
 
 /**
  *      mv_stop_dma - Disable eDMA engine
  *      @ap: ATA channel to manipulate
  *
- *      Verify the local cache of the eDMA state is accurate with an
- *      assert.
+ *      Verify the local cache of the eDMA state is accurate with a
+ *      WARN_ON.
  *
  *      LOCKING:
  *      Inherited from caller.
@@ -545,7 +673,7 @@ static void mv_stop_dma(struct ata_port *ap)
                writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
                pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
        } else {
-               assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
+               WARN_ON(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS));
        }
 
        /* now properly wait for the eDMA to stop */
@@ -558,7 +686,7 @@ static void mv_stop_dma(struct ata_port *ap)
        }
 
        if (EDMA_EN & reg) {
-               printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
+               ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
                /* FIXME: Consider doing a reset here to recover */
        }
 }
@@ -625,7 +753,7 @@ static void mv_dump_all_regs(void __iomem *mmio_base, int port,
        mv_dump_mem(mmio_base+0xf00, 0x4);
        mv_dump_mem(mmio_base+0x1d00, 0x6c);
        for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
-               hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
+               hc_base = mv_hc_base(mmio_base, hc);
                DPRINTK("HC regs (HC %i):\n", hc);
                mv_dump_mem(hc_base, 0x1c);
        }
@@ -708,6 +836,33 @@ static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
        dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
 }
 
+static void mv_edma_cfg(struct mv_host_priv *hpriv, void __iomem *port_mmio)
+{
+       u32 cfg = readl(port_mmio + EDMA_CFG_OFS);
+
+       /* set up non-NCQ EDMA configuration */
+       cfg &= ~0x1f;           /* clear queue depth */
+       cfg &= ~EDMA_CFG_NCQ;   /* clear NCQ mode */
+       cfg &= ~(1 << 9);       /* disable equeue */
+
+       if (IS_GEN_I(hpriv))
+               cfg |= (1 << 8);        /* enab config burst size mask */
+
+       else if (IS_GEN_II(hpriv))
+               cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
+
+       else if (IS_GEN_IIE(hpriv)) {
+               cfg |= (1 << 23);       /* dis RX PM port mask */
+               cfg &= ~(1 << 16);      /* dis FIS-based switching (for now) */
+               cfg &= ~(1 << 19);      /* dis 128-entry queue (for now?) */
+               cfg |= (1 << 18);       /* enab early completion */
+               cfg |= (1 << 17);       /* enab host q cache */
+               cfg |= (1 << 22);       /* enab cutthrough */
+       }
+
+       writelfl(cfg, port_mmio + EDMA_CFG_OFS);
+}
+
 /**
  *      mv_port_start - Port specific init/start routine.
  *      @ap: ATA channel to manipulate
@@ -721,6 +876,7 @@ static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
 static int mv_port_start(struct ata_port *ap)
 {
        struct device *dev = ap->host_set->dev;
+       struct mv_host_priv *hpriv = ap->host_set->private_data;
        struct mv_port_priv *pp;
        void __iomem *port_mmio = mv_ap_base(ap);
        void *mem;
@@ -764,22 +920,29 @@ static int mv_port_start(struct ata_port *ap)
        pp->sg_tbl = mem;
        pp->sg_tbl_dma = mem_dma;
 
-       writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT |
-                EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
+       mv_edma_cfg(hpriv, port_mmio);
 
        writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
        writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
                 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
 
-       writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
-       writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
+       if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
+               writelfl(pp->crqb_dma & 0xffffffff,
+                        port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
+       else
+               writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
 
        writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
+
+       if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
+               writelfl(pp->crpb_dma & 0xffffffff,
+                        port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
+       else
+               writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
+
        writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
                 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
 
-       pp->req_producer = pp->rsp_consumer = 0;
-
        /* Don't turn on EDMA here...do it before DMA commands only.  Else
         * we'll be unable to send non-data, PIO, etc due to restricted access
         * to shadow regs.
@@ -836,33 +999,43 @@ static void mv_fill_sg(struct ata_queued_cmd *qc)
        struct scatterlist *sg;
 
        ata_for_each_sg(sg, qc) {
-               u32 sg_len;
                dma_addr_t addr;
+               u32 sg_len, len, offset;
 
                addr = sg_dma_address(sg);
                sg_len = sg_dma_len(sg);
 
-               pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
-               pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
-               assert(0 == (sg_len & ~MV_DMA_BOUNDARY));
-               pp->sg_tbl[i].flags_size = cpu_to_le32(sg_len);
-               if (ata_sg_is_last(sg, qc))
-                       pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
+               while (sg_len) {
+                       offset = addr & MV_DMA_BOUNDARY;
+                       len = sg_len;
+                       if ((offset + sg_len) > 0x10000)
+                               len = 0x10000 - offset;
+
+                       pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
+                       pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
+                       pp->sg_tbl[i].flags_size = cpu_to_le32(len & 0xffff);
 
-               i++;
+                       sg_len -= len;
+                       addr += len;
+
+                       if (!sg_len && ata_sg_is_last(sg, qc))
+                               pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
+
+                       i++;
+               }
        }
 }
 
-static inline unsigned mv_inc_q_index(unsigned *index)
+static inline unsigned mv_inc_q_index(unsigned index)
 {
-       *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
-       return *index;
+       return (index + 1) & MV_MAX_Q_DEPTH_MASK;
 }
 
-static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
+static inline void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
 {
-       *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
+       u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
                (last ? CRQB_CMD_LAST : 0);
+       *cmdw = cpu_to_le16(tmp);
 }
 
 /**
@@ -881,34 +1054,32 @@ static void mv_qc_prep(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct mv_port_priv *pp = ap->private_data;
-       u16 *cw;
+       __le16 *cw;
        struct ata_taskfile *tf;
        u16 flags = 0;
+       unsigned in_index;
 
-       if (ATA_PROT_DMA != qc->tf.protocol) {
+       if (ATA_PROT_DMA != qc->tf.protocol)
                return;
-       }
-
-       /* the req producer index should be the same as we remember it */
-       assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
-                EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
-              pp->req_producer);
 
        /* Fill in command request block
         */
-       if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
+       if (!(qc->tf.flags & ATA_TFLAG_WRITE))
                flags |= CRQB_FLAG_READ;
-       }
-       assert(MV_MAX_Q_DEPTH > qc->tag);
+       WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
        flags |= qc->tag << CRQB_TAG_SHIFT;
 
-       pp->crqb[pp->req_producer].sg_addr =
+       /* get current queue index from hardware */
+       in_index = (readl(mv_ap_base(ap) + EDMA_REQ_Q_IN_PTR_OFS)
+                       >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
+
+       pp->crqb[in_index].sg_addr =
                cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
-       pp->crqb[pp->req_producer].sg_addr_hi =
+       pp->crqb[in_index].sg_addr_hi =
                cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
-       pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
+       pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
 
-       cw = &pp->crqb[pp->req_producer].ata_cmd[0];
+       cw = &pp->crqb[in_index].ata_cmd[0];
        tf = &qc->tf;
 
        /* Sadly, the CRQB cannot accomodate all registers--there are
@@ -922,6 +1093,7 @@ static void mv_qc_prep(struct ata_queued_cmd *qc)
        case ATA_CMD_READ_EXT:
        case ATA_CMD_WRITE:
        case ATA_CMD_WRITE_EXT:
+       case ATA_CMD_WRITE_FUA_EXT:
                mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
                break;
 #ifdef LIBATA_NCQ              /* FIXME: remove this line when NCQ added */
@@ -953,9 +1125,76 @@ static void mv_qc_prep(struct ata_queued_cmd *qc)
        mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
        mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);    /* last */
 
-       if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
+       if (!(qc->flags & ATA_QCFLAG_DMAMAP))
+               return;
+       mv_fill_sg(qc);
+}
+
+/**
+ *      mv_qc_prep_iie - Host specific command preparation.
+ *      @qc: queued command to prepare
+ *
+ *      This routine simply redirects to the general purpose routine
+ *      if command is not DMA.  Else, it handles prep of the CRQB
+ *      (command request block), does some sanity checking, and calls
+ *      the SG load routine.
+ *
+ *      LOCKING:
+ *      Inherited from caller.
+ */
+static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       struct mv_port_priv *pp = ap->private_data;
+       struct mv_crqb_iie *crqb;
+       struct ata_taskfile *tf;
+       unsigned in_index;
+       u32 flags = 0;
+
+       if (ATA_PROT_DMA != qc->tf.protocol)
+               return;
+
+       /* Fill in Gen IIE command request block
+        */
+       if (!(qc->tf.flags & ATA_TFLAG_WRITE))
+               flags |= CRQB_FLAG_READ;
+
+       WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
+       flags |= qc->tag << CRQB_TAG_SHIFT;
+
+       /* get current queue index from hardware */
+       in_index = (readl(mv_ap_base(ap) + EDMA_REQ_Q_IN_PTR_OFS)
+                       >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
+
+       crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
+       crqb->addr = cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
+       crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
+       crqb->flags = cpu_to_le32(flags);
+
+       tf = &qc->tf;
+       crqb->ata_cmd[0] = cpu_to_le32(
+                       (tf->command << 16) |
+                       (tf->feature << 24)
+               );
+       crqb->ata_cmd[1] = cpu_to_le32(
+                       (tf->lbal << 0) |
+                       (tf->lbam << 8) |
+                       (tf->lbah << 16) |
+                       (tf->device << 24)
+               );
+       crqb->ata_cmd[2] = cpu_to_le32(
+                       (tf->hob_lbal << 0) |
+                       (tf->hob_lbam << 8) |
+                       (tf->hob_lbah << 16) |
+                       (tf->hob_feature << 24)
+               );
+       crqb->ata_cmd[3] = cpu_to_le32(
+                       (tf->nsect << 0) |
+                       (tf->hob_nsect << 8)
+               );
+
+       if (!(qc->flags & ATA_QCFLAG_DMAMAP))
                return;
-       }
        mv_fill_sg(qc);
 }
 
@@ -971,10 +1210,11 @@ static void mv_qc_prep(struct ata_queued_cmd *qc)
  *      LOCKING:
  *      Inherited from caller.
  */
-static int mv_qc_issue(struct ata_queued_cmd *qc)
+static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
 {
        void __iomem *port_mmio = mv_ap_base(qc->ap);
        struct mv_port_priv *pp = qc->ap->private_data;
+       unsigned in_index;
        u32 in_ptr;
 
        if (ATA_PROT_DMA != qc->tf.protocol) {
@@ -986,23 +1226,20 @@ static int mv_qc_issue(struct ata_queued_cmd *qc)
                return ata_qc_issue_prot(qc);
        }
 
-       in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
+       in_ptr   = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
+       in_index = (in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
 
-       /* the req producer index should be the same as we remember it */
-       assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
-              pp->req_producer);
        /* until we do queuing, the queue should be empty at this point */
-       assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
-              ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
-                EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
+       WARN_ON(in_index != ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
+               >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
 
-       mv_inc_q_index(&pp->req_producer);      /* now incr producer index */
+       in_index = mv_inc_q_index(in_index);    /* now incr producer index */
 
        mv_start_dma(port_mmio, pp);
 
        /* and write the request in pointer to kick the EDMA to life */
        in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
-       in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
+       in_ptr |= in_index << EDMA_REQ_Q_PTR_SHIFT;
        writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
 
        return 0;
@@ -1014,7 +1251,7 @@ static int mv_qc_issue(struct ata_queued_cmd *qc)
  *
  *      This routine is for use when the port is in DMA mode, when it
  *      will be using the CRPB (command response block) method of
- *      returning command completion information.  We assert indices
+ *      returning command completion information.  We check indices
  *      are good, grab status, and bump the response consumer index to
  *      prove that we're up to date.
  *
@@ -1025,34 +1262,36 @@ static u8 mv_get_crpb_status(struct ata_port *ap)
 {
        void __iomem *port_mmio = mv_ap_base(ap);
        struct mv_port_priv *pp = ap->private_data;
+       unsigned out_index;
        u32 out_ptr;
+       u8 ata_status;
 
-       out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
+       out_ptr   = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
+       out_index = (out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
 
-       /* the response consumer index should be the same as we remember it */
-       assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
-              pp->rsp_consumer);
+       ata_status = le16_to_cpu(pp->crpb[out_index].flags)
+                                       >> CRPB_FLAG_STATUS_SHIFT;
 
        /* increment our consumer index... */
-       pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
+       out_index = mv_inc_q_index(out_index);
 
        /* and, until we do NCQ, there should only be 1 CRPB waiting */
-       assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
-                EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
-              pp->rsp_consumer);
+       WARN_ON(out_index != ((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
+               >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
 
        /* write out our inc'd consumer index so EDMA knows we're caught up */
        out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
-       out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
+       out_ptr |= out_index << EDMA_RSP_Q_PTR_SHIFT;
        writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
 
        /* Return ATA status register for completed CRPB */
-       return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
+       return ata_status;
 }
 
 /**
  *      mv_err_intr - Handle error interrupts on the port
  *      @ap: ATA channel to manipulate
+ *      @reset_allowed: bool: 0 == don't trigger from reset here
  *
  *      In most cases, just clear the interrupt and move on.  However,
  *      some cases require an eDMA reset, which is done right before
@@ -1063,7 +1302,7 @@ static u8 mv_get_crpb_status(struct ata_port *ap)
  *      LOCKING:
  *      Inherited from caller.
  */
-static void mv_err_intr(struct ata_port *ap)
+static void mv_err_intr(struct ata_port *ap, int reset_allowed)
 {
        void __iomem *port_mmio = mv_ap_base(ap);
        u32 edma_err_cause, serr = 0;
@@ -1071,8 +1310,8 @@ static void mv_err_intr(struct ata_port *ap)
        edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
 
        if (EDMA_ERR_SERR & edma_err_cause) {
-               serr = scr_read(ap, SCR_ERROR);
-               scr_write_flush(ap, SCR_ERROR, serr);
+               sata_scr_read(ap, SCR_ERROR, &serr);
+               sata_scr_write_flush(ap, SCR_ERROR, serr);
        }
        if (EDMA_ERR_SELF_DIS & edma_err_cause) {
                struct mv_port_priv *pp = ap->private_data;
@@ -1085,9 +1324,8 @@ static void mv_err_intr(struct ata_port *ap)
        writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
 
        /* check for fatal here and recover if needed */
-       if (EDMA_ERR_FATAL & edma_err_cause) {
-               mv_phy_reset(ap);
-       }
+       if (reset_allowed && (EDMA_ERR_FATAL & edma_err_cause))
+               mv_stop_and_reset(ap);
 }
 
 /**
@@ -1111,12 +1349,10 @@ static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
 {
        void __iomem *mmio = host_set->mmio_base;
        void __iomem *hc_mmio = mv_hc_base(mmio, hc);
-       struct ata_port *ap;
        struct ata_queued_cmd *qc;
        u32 hc_irq_cause;
        int shift, port, port0, hard_port, handled;
        unsigned int err_mask;
-       u8 ata_status = 0;
 
        if (hc == 0) {
                port0 = 0;
@@ -1134,23 +1370,40 @@ static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
                hc,relevant,hc_irq_cause);
 
        for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
-               ap = host_set->ports[port];
-               hard_port = port & MV_PORT_MASK;        /* range 0-3 */
+               u8 ata_status = 0;
+               struct ata_port *ap = host_set->ports[port];
+               struct mv_port_priv *pp = ap->private_data;
+
+               hard_port = mv_hardport_from_port(port); /* range 0..3 */
                handled = 0;    /* ensure ata_status is set if handled++ */
 
-               if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
-                       /* new CRPB on the queue; just one at a time until NCQ
-                        */
-                       ata_status = mv_get_crpb_status(ap);
-                       handled++;
-               } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
-                       /* received ATA IRQ; read the status reg to clear INTRQ
-                        */
-                       ata_status = readb((void __iomem *)
+               /* Note that DEV_IRQ might happen spuriously during EDMA,
+                * and should be ignored in such cases.
+                * The cause of this is still under investigation.
+                */
+               if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
+                       /* EDMA: check for response queue interrupt */
+                       if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
+                               ata_status = mv_get_crpb_status(ap);
+                               handled = 1;
+                       }
+               } else {
+                       /* PIO: check for device (drive) interrupt */
+                       if ((DEV_IRQ << hard_port) & hc_irq_cause) {
+                               ata_status = readb((void __iomem *)
                                           ap->ioaddr.status_addr);
-                       handled++;
+                               handled = 1;
+                               /* ignore spurious intr if drive still BUSY */
+                               if (ata_status & ATA_BUSY) {
+                                       ata_status = 0;
+                                       handled = 0;
+                               }
+                       }
                }
 
+               if (ap && (ap->flags & ATA_FLAG_DISABLED))
+                       continue;
+
                err_mask = ac_err_mask(ata_status);
 
                shift = port << 1;              /* (port * 2) */
@@ -1158,18 +1411,21 @@ static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
                        shift++;        /* skip bit 8 in the HC Main IRQ reg */
                }
                if ((PORT0_ERR << shift) & relevant) {
-                       mv_err_intr(ap);
+                       mv_err_intr(ap, 1);
                        err_mask |= AC_ERR_OTHER;
-                       handled++;
+                       handled = 1;
                }
 
-               if (handled && ap) {
+               if (handled) {
                        qc = ata_qc_from_tag(ap, ap->active_tag);
-                       if (NULL != qc) {
+                       if (qc && (qc->flags & ATA_QCFLAG_ACTIVE)) {
                                VPRINTK("port %u IRQ found for qc, "
                                        "ata_status 0x%x\n", port,ata_status);
                                /* mark qc status appropriately */
-                               ata_qc_complete(qc, err_mask);
+                               if (!(qc->tf.flags & ATA_TFLAG_POLLING)) {
+                                       qc->err_mask |= err_mask;
+                                       ata_qc_complete(qc);
+                               }
                        }
                }
        }
@@ -1197,6 +1453,7 @@ static irqreturn_t mv_interrupt(int irq, void *dev_instance,
        struct ata_host_set *host_set = dev_instance;
        unsigned int hc, handled = 0, n_hcs;
        void __iomem *mmio = host_set->mmio_base;
+       struct mv_host_priv *hpriv;
        u32 irq_stat;
 
        irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
@@ -1218,6 +1475,17 @@ static irqreturn_t mv_interrupt(int irq, void *dev_instance,
                        handled++;
                }
        }
+
+       hpriv = host_set->private_data;
+       if (IS_60XX(hpriv)) {
+               /* deal with the interrupt coalescing bits */
+               if (irq_stat & (TRAN_LO_DONE | TRAN_HI_DONE | PORTS_0_7_COAL_DONE)) {
+                       writelfl(0, mmio + MV_IRQ_COAL_CAUSE_LO);
+                       writelfl(0, mmio + MV_IRQ_COAL_CAUSE_HI);
+                       writelfl(0, mmio + MV_IRQ_COAL_CAUSE);
+               }
+       }
+
        if (PCI_ERR & irq_stat) {
                printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
                       readl(mmio + PCI_IRQ_CAUSE_OFS));
@@ -1233,6 +1501,51 @@ static irqreturn_t mv_interrupt(int irq, void *dev_instance,
        return IRQ_RETVAL(handled);
 }
 
+static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
+{
+       void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
+       unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
+
+       return hc_mmio + ofs;
+}
+
+static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
+{
+       unsigned int ofs;
+
+       switch (sc_reg_in) {
+       case SCR_STATUS:
+       case SCR_ERROR:
+       case SCR_CONTROL:
+               ofs = sc_reg_in * sizeof(u32);
+               break;
+       default:
+               ofs = 0xffffffffU;
+               break;
+       }
+       return ofs;
+}
+
+static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
+{
+       void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
+       unsigned int ofs = mv5_scr_offset(sc_reg_in);
+
+       if (ofs != 0xffffffffU)
+               return readl(mmio + ofs);
+       else
+               return (u32) ofs;
+}
+
+static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
+{
+       void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
+       unsigned int ofs = mv5_scr_offset(sc_reg_in);
+
+       if (ofs != 0xffffffffU)
+               writelfl(val, mmio + ofs);
+}
+
 static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
 {
        u8 rev_id;
@@ -1259,7 +1572,13 @@ static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
 static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
                           void __iomem *mmio)
 {
-       /* FIXME */
+       void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
+       u32 tmp;
+
+       tmp = readl(phy_mmio + MV5_PHY_MODE);
+
+       hpriv->signal[idx].pre = tmp & 0x1800;  /* bits 12:11 */
+       hpriv->signal[idx].amps = tmp & 0xe0;   /* bits 7:5 */
 }
 
 static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
@@ -1275,15 +1594,93 @@ static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
        writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
 }
 
-static void mv5_phy_errata(struct ata_port *ap)
+static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
+                          unsigned int port)
+{
+       void __iomem *phy_mmio = mv5_phy_base(mmio, port);
+       const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
+       u32 tmp;
+       int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
+
+       if (fix_apm_sq) {
+               tmp = readl(phy_mmio + MV5_LT_MODE);
+               tmp |= (1 << 19);
+               writel(tmp, phy_mmio + MV5_LT_MODE);
+
+               tmp = readl(phy_mmio + MV5_PHY_CTL);
+               tmp &= ~0x3;
+               tmp |= 0x1;
+               writel(tmp, phy_mmio + MV5_PHY_CTL);
+       }
+
+       tmp = readl(phy_mmio + MV5_PHY_MODE);
+       tmp &= ~mask;
+       tmp |= hpriv->signal[port].pre;
+       tmp |= hpriv->signal[port].amps;
+       writel(tmp, phy_mmio + MV5_PHY_MODE);
+}
+
+
+#undef ZERO
+#define ZERO(reg) writel(0, port_mmio + (reg))
+static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
+                            unsigned int port)
 {
-       /* FIXME */
+       void __iomem *port_mmio = mv_port_base(mmio, port);
+
+       writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
+
+       mv_channel_reset(hpriv, mmio, port);
+
+       ZERO(0x028);    /* command */
+       writel(0x11f, port_mmio + EDMA_CFG_OFS);
+       ZERO(0x004);    /* timer */
+       ZERO(0x008);    /* irq err cause */
+       ZERO(0x00c);    /* irq err mask */
+       ZERO(0x010);    /* rq bah */
+       ZERO(0x014);    /* rq inp */
+       ZERO(0x018);    /* rq outp */
+       ZERO(0x01c);    /* respq bah */
+       ZERO(0x024);    /* respq outp */
+       ZERO(0x020);    /* respq inp */
+       ZERO(0x02c);    /* test control */
+       writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
 }
+#undef ZERO
 
-static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio)
+#define ZERO(reg) writel(0, hc_mmio + (reg))
+static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
+                       unsigned int hc)
 {
-       /* FIXME */
-       return 1;
+       void __iomem *hc_mmio = mv_hc_base(mmio, hc);
+       u32 tmp;
+
+       ZERO(0x00c);
+       ZERO(0x010);
+       ZERO(0x014);
+       ZERO(0x018);
+
+       tmp = readl(hc_mmio + 0x20);
+       tmp &= 0x1c1c1c1c;
+       tmp |= 0x03030303;
+       writel(tmp, hc_mmio + 0x20);
+}
+#undef ZERO
+
+static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
+                       unsigned int n_hc)
+{
+       unsigned int hc, port;
+
+       for (hc = 0; hc < n_hc; hc++) {
+               for (port = 0; port < MV_PORTS_PER_HC; port++)
+                       mv5_reset_hc_port(hpriv, mmio,
+                                         (hc * MV_PORTS_PER_HC) + port);
+
+               mv5_reset_one_hc(hpriv, mmio, hc);
+       }
+
+       return 0;
 }
 
 #undef ZERO
@@ -1331,7 +1728,8 @@ static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  *      LOCKING:
  *      Inherited from caller.
  */
-static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio)
+static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
+                       unsigned int n_hc)
 {
        void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
        int i, rc = 0;
@@ -1411,11 +1809,12 @@ static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
        writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
 }
 
-static void mv6_phy_errata(struct ata_port *ap)
+static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
+                          unsigned int port)
 {
-       struct mv_host_priv *hpriv = ap->host_set->private_data;
+       void __iomem *port_mmio = mv_port_base(mmio, port);
+
        u32 hp_flags = hpriv->hp_flags;
-       void __iomem *port_mmio = mv_ap_base(ap);
        int fix_phy_mode2 =
                hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
        int fix_phy_mode4 =
@@ -1463,15 +1862,68 @@ static void mv6_phy_errata(struct ata_port *ap)
        m2 = readl(port_mmio + PHY_MODE2);
 
        m2 &= ~MV_M2_PREAMP_MASK;
-       m2 |= hpriv->signal[ap->port_no].amps;
-       m2 |= hpriv->signal[ap->port_no].pre;
+       m2 |= hpriv->signal[port].amps;
+       m2 |= hpriv->signal[port].pre;
        m2 &= ~(1 << 16);
 
+       /* according to mvSata 3.6.1, some IIE values are fixed */
+       if (IS_GEN_IIE(hpriv)) {
+               m2 &= ~0xC30FF01F;
+               m2 |= 0x0000900F;
+       }
+
        writel(m2, port_mmio + PHY_MODE2);
 }
 
+static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
+                            unsigned int port_no)
+{
+       void __iomem *port_mmio = mv_port_base(mmio, port_no);
+
+       writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
+
+       if (IS_60XX(hpriv)) {
+               u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
+               ifctl |= (1 << 7);              /* enable gen2i speed */
+               ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
+               writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
+       }
+
+       udelay(25);             /* allow reset propagation */
+
+       /* Spec never mentions clearing the bit.  Marvell's driver does
+        * clear the bit, however.
+        */
+       writelfl(0, port_mmio + EDMA_CMD_OFS);
+
+       hpriv->ops->phy_errata(hpriv, mmio, port_no);
+
+       if (IS_50XX(hpriv))
+               mdelay(1);
+}
+
+static void mv_stop_and_reset(struct ata_port *ap)
+{
+       struct mv_host_priv *hpriv = ap->host_set->private_data;
+       void __iomem *mmio = ap->host_set->mmio_base;
+
+       mv_stop_dma(ap);
+
+       mv_channel_reset(hpriv, mmio, ap->port_no);
+
+       __mv_phy_reset(ap, 0);
+}
+
+static inline void __msleep(unsigned int msec, int can_sleep)
+{
+       if (can_sleep)
+               msleep(msec);
+       else
+               mdelay(msec);
+}
+
 /**
- *      mv_phy_reset - Perform eDMA reset followed by COMRESET
+ *      __mv_phy_reset - Perform eDMA reset followed by COMRESET
  *      @ap: ATA channel to manipulate
  *
  *      Part of this is taken from __sata_phy_reset and modified to
@@ -1481,7 +1933,7 @@ static void mv6_phy_errata(struct ata_port *ap)
  *      Inherited from caller.  This is coded to safe to call at
  *      interrupt level, i.e. it does not sleep.
  */
-static void mv_phy_reset(struct ata_port *ap)
+static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
 {
        struct mv_port_priv *pp = ap->private_data;
        struct mv_host_priv *hpriv = ap->host_set->private_data;
@@ -1489,66 +1941,76 @@ static void mv_phy_reset(struct ata_port *ap)
        struct ata_taskfile tf;
        struct ata_device *dev = &ap->device[0];
        unsigned long timeout;
+       int retry = 5;
+       u32 sstatus;
 
        VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
 
-       mv_stop_dma(ap);
-
-       writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
-
-       if (IS_60XX(hpriv)) {
-               u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
-               ifctl |= (1 << 12) | (1 << 7);
-               writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
-       }
-
-       udelay(25);             /* allow reset propagation */
-
-       /* Spec never mentions clearing the bit.  Marvell's driver does
-        * clear the bit, however.
-        */
-       writelfl(0, port_mmio + EDMA_CMD_OFS);
-
-       hpriv->ops->phy_errata(ap);
-
        DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
                "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
                mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
 
-       /* proceed to init communications via the scr_control reg */
-       scr_write_flush(ap, SCR_CONTROL, 0x301);
-       mdelay(1);
-       scr_write_flush(ap, SCR_CONTROL, 0x300);
-       timeout = jiffies + (HZ * 1);
+       /* Issue COMRESET via SControl */
+comreset_retry:
+       sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
+       __msleep(1, can_sleep);
+
+       sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
+       __msleep(20, can_sleep);
+
+       timeout = jiffies + msecs_to_jiffies(200);
        do {
-               mdelay(10);
-               if ((scr_read(ap, SCR_STATUS) & 0xf) != 1)
+               sata_scr_read(ap, SCR_STATUS, &sstatus);
+               sstatus &= 0x3;
+               if ((sstatus == 3) || (sstatus == 0))
                        break;
+
+               __msleep(1, can_sleep);
        } while (time_before(jiffies, timeout));
 
-       mv_scr_write(ap, SCR_ERROR, mv_scr_read(ap, SCR_ERROR));
+       /* work around errata */
+       if (IS_60XX(hpriv) &&
+           (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
+           (retry-- > 0))
+               goto comreset_retry;
 
        DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
                "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
                mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
 
-       if (sata_dev_present(ap)) {
+       if (ata_port_online(ap)) {
                ata_port_probe(ap);
        } else {
-               printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
-                      ap->id, scr_read(ap, SCR_STATUS));
+               sata_scr_read(ap, SCR_STATUS, &sstatus);
+               ata_port_printk(ap, KERN_INFO,
+                               "no device found (phy stat %08x)\n", sstatus);
                ata_port_disable(ap);
                return;
        }
        ap->cbl = ATA_CBL_SATA;
 
+       /* even after SStatus reflects that device is ready,
+        * it seems to take a while for link to be fully
+        * established (and thus Status no longer 0x80/0x7F),
+        * so we poll a bit for that, here.
+        */
+       retry = 20;
+       while (1) {
+               u8 drv_stat = ata_check_status(ap);
+               if ((drv_stat != 0x80) && (drv_stat != 0x7f))
+                       break;
+               __msleep(500, can_sleep);
+               if (retry-- <= 0)
+                       break;
+       }
+
        tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
        tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
        tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
        tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
 
        dev->class = ata_dev_classify(&tf);
-       if (!ata_dev_present(dev)) {
+       if (!ata_dev_enabled(dev)) {
                VPRINTK("Port disabled post-sig: No device present.\n");
                ata_port_disable(ap);
        }
@@ -1560,6 +2022,11 @@ static void mv_phy_reset(struct ata_port *ap)
        VPRINTK("EXIT\n");
 }
 
+static void mv_phy_reset(struct ata_port *ap)
+{
+       __mv_phy_reset(ap, 1);
+}
+
 /**
  *      mv_eng_timeout - Routine called by libata when SCSI times out I/O
  *      @ap: ATA channel to manipulate
@@ -1575,7 +2042,7 @@ static void mv_eng_timeout(struct ata_port *ap)
        struct ata_queued_cmd *qc;
        unsigned long flags;
 
-       printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
+       ata_port_printk(ap, KERN_ERR, "Entering mv_eng_timeout\n");
        DPRINTK("All regs @ start of eng_timeout\n");
        mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
                         to_pci_dev(ap->host_set->dev));
@@ -1585,23 +2052,15 @@ static void mv_eng_timeout(struct ata_port *ap)
               ap->host_set->mmio_base, ap, qc, qc->scsicmd,
               &qc->scsicmd->cmnd);
 
-       mv_err_intr(ap);
-       mv_phy_reset(ap);
+       spin_lock_irqsave(&ap->host_set->lock, flags);
+       mv_err_intr(ap, 0);
+       mv_stop_and_reset(ap);
+       spin_unlock_irqrestore(&ap->host_set->lock, flags);
 
-       if (!qc) {
-               printk(KERN_ERR "ata%u: BUG: timeout without command\n",
-                      ap->id);
-       } else {
-               /* hack alert!  We cannot use the supplied completion
-                * function from inside the ->eh_strategy_handler() thread.
-                * libata is the only user of ->eh_strategy_handler() in
-                * any kernel, so the default scsi_done() assumes it is
-                * not being called from the SCSI EH.
-                */
-               spin_lock_irqsave(&ap->host_set->lock, flags);
-               qc->scsidone = scsi_finish_command;
-               ata_qc_complete(qc, AC_ERR_OTHER);
-               spin_unlock_irqrestore(&ap->host_set->lock, flags);
+       WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
+       if (qc->flags & ATA_QCFLAG_ACTIVE) {
+               qc->err_mask |= AC_ERR_TIMEOUT;
+               ata_eh_qc_complete(qc);
        }
 }
 
@@ -1721,6 +2180,27 @@ static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
                }
                break;
 
+       case chip_7042:
+       case chip_6042:
+               hpriv->ops = &mv6xxx_ops;
+
+               hp_flags |= MV_HP_GEN_IIE;
+
+               switch (rev_id) {
+               case 0x0:
+                       hp_flags |= MV_HP_ERRATA_XX42A0;
+                       break;
+               case 0x1:
+                       hp_flags |= MV_HP_ERRATA_60X1C0;
+                       break;
+               default:
+                       dev_printk(KERN_WARNING, &pdev->dev,
+                          "Applying 60X1C0 workarounds to unknown rev\n");
+                       hp_flags |= MV_HP_ERRATA_60X1C0;
+                       break;
+               }
+               break;
+
        default:
                printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
                return 1;
@@ -1747,7 +2227,6 @@ static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
 {
        int rc = 0, n_hc, port, hc;
        void __iomem *mmio = probe_ent->mmio_base;
-       void __iomem *port_mmio;
        struct mv_host_priv *hpriv = probe_ent->private_data;
 
        /* global interrupt mask */
@@ -1763,7 +2242,7 @@ static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
        for (port = 0; port < probe_ent->n_ports; port++)
                hpriv->ops->read_preamp(hpriv, port, mmio);
 
-       rc = hpriv->ops->reset_hc(hpriv, mmio);
+       rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
        if (rc)
                goto done;
 
@@ -1772,7 +2251,20 @@ static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
        hpriv->ops->enable_leds(hpriv, mmio);
 
        for (port = 0; port < probe_ent->n_ports; port++) {
-               port_mmio = mv_port_base(mmio, port);
+               if (IS_60XX(hpriv)) {
+                       void __iomem *port_mmio = mv_port_base(mmio, port);
+
+                       u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
+                       ifctl |= (1 << 7);              /* enable gen2i speed */
+                       ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
+                       writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
+               }
+
+               hpriv->ops->phy_errata(hpriv, mmio, port);
+       }
+
+       for (port = 0; port < probe_ent->n_ports; port++) {
+               void __iomem *port_mmio = mv_port_base(mmio, port);
                mv_port_init(&probe_ent->port[port], port_mmio);
        }
 
@@ -1865,6 +2357,7 @@ static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (rc) {
                return rc;
        }
+       pci_set_master(pdev);
 
        rc = pci_request_regions(pdev, DRV_NAME);
        if (rc) {
@@ -1913,7 +2406,7 @@ static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
        }
 
        /* Enable interrupts */
-       if (pci_enable_msi(pdev) == 0) {
+       if (msi && pci_enable_msi(pdev) == 0) {
                hpriv->hp_flags |= MV_HP_FLAG_MSI;
        } else {
                pci_intx(pdev, 1);
@@ -1968,5 +2461,8 @@ MODULE_LICENSE("GPL");
 MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
 MODULE_VERSION(DRV_VERSION);
 
+module_param(msi, int, 0444);
+MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
+
 module_init(mv_init);
 module_exit(mv_exit);