Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[safe/jmp/linux-2.6] / drivers / ata / sata_vsc.c
index e654b99..8b2a278 100644 (file)
 #include <linux/libata.h>
 
 #define DRV_NAME       "sata_vsc"
-#define DRV_VERSION    "2.0"
+#define DRV_VERSION    "2.3"
 
 enum {
+       VSC_MMIO_BAR                    = 0,
+
        /* Interrupt register offsets (from chip base address) */
        VSC_SATA_INT_STAT_OFFSET        = 0x00,
        VSC_SATA_INT_MASK_OFFSET        = 0x04,
@@ -96,25 +98,45 @@ enum {
                              VSC_SATA_INT_PHY_CHANGE),
 };
 
-
-#define is_vsc_sata_int_err(port_idx, int_status) \
-        (int_status & (VSC_SATA_INT_ERROR << (8 * port_idx)))
+static int vsc_sata_scr_read(struct ata_link *link,
+                            unsigned int sc_reg, u32 *val)
+{
+       if (sc_reg > SCR_CONTROL)
+               return -EINVAL;
+       *val = readl(link->ap->ioaddr.scr_addr + (sc_reg * 4));
+       return 0;
+}
 
 
-static u32 vsc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg)
+static int vsc_sata_scr_write(struct ata_link *link,
+                             unsigned int sc_reg, u32 val)
 {
        if (sc_reg > SCR_CONTROL)
-               return 0xffffffffU;
-       return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
+               return -EINVAL;
+       writel(val, link->ap->ioaddr.scr_addr + (sc_reg * 4));
+       return 0;
 }
 
 
-static void vsc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg,
-                              u32 val)
+static void vsc_freeze(struct ata_port *ap)
 {
-       if (sc_reg > SCR_CONTROL)
-               return;
-       writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
+       void __iomem *mask_addr;
+
+       mask_addr = ap->host->iomap[VSC_MMIO_BAR] +
+               VSC_SATA_INT_MASK_OFFSET + ap->port_no;
+
+       writeb(0, mask_addr);
+}
+
+
+static void vsc_thaw(struct ata_port *ap)
+{
+       void __iomem *mask_addr;
+
+       mask_addr = ap->host->iomap[VSC_MMIO_BAR] +
+               VSC_SATA_INT_MASK_OFFSET + ap->port_no;
+
+       writeb(0xff, mask_addr);
 }
 
 
@@ -123,7 +145,7 @@ static void vsc_intr_mask_update(struct ata_port *ap, u8 ctl)
        void __iomem *mask_addr;
        u8 mask;
 
-       mask_addr = ap->host->mmio_base +
+       mask_addr = ap->host->iomap[VSC_MMIO_BAR] +
                VSC_SATA_INT_MASK_OFFSET + ap->port_no;
        mask = readb(mask_addr);
        if (ctl & ATA_NIEN)
@@ -142,18 +164,24 @@ static void vsc_sata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
        /*
         * The only thing the ctl register is used for is SRST.
         * That is not enabled or disabled via tf_load.
-        * However, if ATA_NIEN is changed, then we need to change the interrupt register.
+        * However, if ATA_NIEN is changed, then we need to change
+        * the interrupt register.
         */
        if ((tf->ctl & ATA_NIEN) != (ap->last_ctl & ATA_NIEN)) {
                ap->last_ctl = tf->ctl;
                vsc_intr_mask_update(ap, tf->ctl & ATA_NIEN);
        }
        if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
-               writew(tf->feature | (((u16)tf->hob_feature) << 8), ioaddr->feature_addr);
-               writew(tf->nsect | (((u16)tf->hob_nsect) << 8), ioaddr->nsect_addr);
-               writew(tf->lbal | (((u16)tf->hob_lbal) << 8), ioaddr->lbal_addr);
-               writew(tf->lbam | (((u16)tf->hob_lbam) << 8), ioaddr->lbam_addr);
-               writew(tf->lbah | (((u16)tf->hob_lbah) << 8), ioaddr->lbah_addr);
+               writew(tf->feature | (((u16)tf->hob_feature) << 8),
+                      ioaddr->feature_addr);
+               writew(tf->nsect | (((u16)tf->hob_nsect) << 8),
+                      ioaddr->nsect_addr);
+               writew(tf->lbal | (((u16)tf->hob_lbal) << 8),
+                      ioaddr->lbal_addr);
+               writew(tf->lbam | (((u16)tf->hob_lbam) << 8),
+                      ioaddr->lbam_addr);
+               writew(tf->lbah | (((u16)tf->hob_lbah) << 8),
+                      ioaddr->lbah_addr);
        } else if (is_addr) {
                writew(tf->feature, ioaddr->feature_addr);
                writew(tf->nsect, ioaddr->nsect_addr);
@@ -174,7 +202,7 @@ static void vsc_sata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
        struct ata_ioports *ioaddr = &ap->ioaddr;
        u16 nsect, lbal, lbam, lbah, feature;
 
-       tf->command = ata_check_status(ap);
+       tf->command = ata_sff_check_status(ap);
        tf->device = readw(ioaddr->device_addr);
        feature = readw(ioaddr->error_addr);
        nsect = readw(ioaddr->nsect_addr);
@@ -194,123 +222,105 @@ static void vsc_sata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
                tf->hob_lbal = lbal >> 8;
                tf->hob_lbam = lbam >> 8;
                tf->hob_lbah = lbah >> 8;
-        }
+       }
+}
+
+static inline void vsc_error_intr(u8 port_status, struct ata_port *ap)
+{
+       if (port_status & (VSC_SATA_INT_PHY_CHANGE | VSC_SATA_INT_ERROR_M))
+               ata_port_freeze(ap);
+       else
+               ata_port_abort(ap);
 }
 
+static void vsc_port_intr(u8 port_status, struct ata_port *ap)
+{
+       struct ata_queued_cmd *qc;
+       int handled = 0;
+
+       if (unlikely(port_status & VSC_SATA_INT_ERROR)) {
+               vsc_error_intr(port_status, ap);
+               return;
+       }
+
+       qc = ata_qc_from_tag(ap, ap->link.active_tag);
+       if (qc && likely(!(qc->tf.flags & ATA_TFLAG_POLLING)))
+               handled = ata_sff_host_intr(ap, qc);
+
+       /* We received an interrupt during a polled command,
+        * or some other spurious condition.  Interrupt reporting
+        * with this hardware is fairly reliable so it is safe to
+        * simply clear the interrupt
+        */
+       if (unlikely(!handled))
+               ap->ops->sff_check_status(ap);
+}
 
 /*
  * vsc_sata_interrupt
  *
- * Read the interrupt register and process for the devices that have them pending.
+ * Read the interrupt register and process for the devices that have
+ * them pending.
  */
-static irqreturn_t vsc_sata_interrupt (int irq, void *dev_instance)
+static irqreturn_t vsc_sata_interrupt(int irq, void *dev_instance)
 {
        struct ata_host *host = dev_instance;
        unsigned int i;
        unsigned int handled = 0;
-       u32 int_status;
+       u32 status;
 
-       spin_lock(&host->lock);
-
-       int_status = readl(host->mmio_base + VSC_SATA_INT_STAT_OFFSET);
+       status = readl(host->iomap[VSC_MMIO_BAR] + VSC_SATA_INT_STAT_OFFSET);
 
-       for (i = 0; i < host->n_ports; i++) {
-               if (int_status & ((u32) 0xFF << (8 * i))) {
-                       struct ata_port *ap;
+       if (unlikely(status == 0xffffffff || status == 0)) {
+               if (status)
+                       dev_printk(KERN_ERR, host->dev,
+                               ": IRQ status == 0xffffffff, "
+                               "PCI fault or device removal?\n");
+               goto out;
+       }
 
-                       ap = host->ports[i];
+       spin_lock(&host->lock);
 
-                       if (is_vsc_sata_int_err(i, int_status)) {
-                               u32 err_status;
-                               printk(KERN_DEBUG "%s: ignoring interrupt(s)\n", __FUNCTION__);
-                               err_status = ap ? vsc_sata_scr_read(ap, SCR_ERROR) : 0;
-                               vsc_sata_scr_write(ap, SCR_ERROR, err_status);
-                               handled++;
-                       }
+       for (i = 0; i < host->n_ports; i++) {
+               u8 port_status = (status >> (8 * i)) & 0xff;
+               if (port_status) {
+                       struct ata_port *ap = host->ports[i];
 
                        if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
-                               struct ata_queued_cmd *qc;
-
-                               qc = ata_qc_from_tag(ap, ap->active_tag);
-                               if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
-                                       handled += ata_host_intr(ap, qc);
-                               else if (is_vsc_sata_int_err(i, int_status)) {
-                                       /*
-                                        * On some chips (i.e. Intel 31244), an error
-                                        * interrupt will sneak in at initialization
-                                        * time (phy state changes).  Clearing the SCR
-                                        * error register is not required, but it prevents
-                                        * the phy state change interrupts from recurring
-                                        * later.
-                                        */
-                                       u32 err_status;
-                                       err_status = vsc_sata_scr_read(ap, SCR_ERROR);
-                                       printk(KERN_DEBUG "%s: clearing interrupt, "
-                                              "status %x; sata err status %x\n",
-                                              __FUNCTION__,
-                                              int_status, err_status);
-                                       vsc_sata_scr_write(ap, SCR_ERROR, err_status);
-                                       /* Clear interrupt status */
-                                       ata_chk_status(ap);
-                                       handled++;
-                               }
-                       }
+                               vsc_port_intr(port_status, ap);
+                               handled++;
+                       } else
+                               dev_printk(KERN_ERR, host->dev,
+                                       "interrupt from disabled port %d\n", i);
                }
        }
 
        spin_unlock(&host->lock);
-
+out:
        return IRQ_RETVAL(handled);
 }
 
 
 static struct scsi_host_template vsc_sata_sht = {
-       .module                 = THIS_MODULE,
-       .name                   = DRV_NAME,
-       .ioctl                  = ata_scsi_ioctl,
-       .queuecommand           = ata_scsi_queuecmd,
-       .can_queue              = ATA_DEF_QUEUE,
-       .this_id                = ATA_SHT_THIS_ID,
-       .sg_tablesize           = LIBATA_MAX_PRD,
-       .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
-       .emulated               = ATA_SHT_EMULATED,
-       .use_clustering         = ATA_SHT_USE_CLUSTERING,
-       .proc_name              = DRV_NAME,
-       .dma_boundary           = ATA_DMA_BOUNDARY,
-       .slave_configure        = ata_scsi_slave_config,
-       .slave_destroy          = ata_scsi_slave_destroy,
-       .bios_param             = ata_std_bios_param,
+       ATA_BMDMA_SHT(DRV_NAME),
 };
 
 
-static const struct ata_port_operations vsc_sata_ops = {
-       .port_disable           = ata_port_disable,
-       .tf_load                = vsc_sata_tf_load,
-       .tf_read                = vsc_sata_tf_read,
-       .exec_command           = ata_exec_command,
-       .check_status           = ata_check_status,
-       .dev_select             = ata_std_dev_select,
-       .bmdma_setup            = ata_bmdma_setup,
-       .bmdma_start            = ata_bmdma_start,
-       .bmdma_stop             = ata_bmdma_stop,
-       .bmdma_status           = ata_bmdma_status,
-       .qc_prep                = ata_qc_prep,
-       .qc_issue               = ata_qc_issue_prot,
-       .data_xfer              = ata_mmio_data_xfer,
-       .freeze                 = ata_bmdma_freeze,
-       .thaw                   = ata_bmdma_thaw,
-       .error_handler          = ata_bmdma_error_handler,
-       .post_internal_cmd      = ata_bmdma_post_internal_cmd,
-       .irq_handler            = vsc_sata_interrupt,
-       .irq_clear              = ata_bmdma_irq_clear,
+static struct ata_port_operations vsc_sata_ops = {
+       .inherits               = &ata_bmdma_port_ops,
+       /* The IRQ handling is not quite standard SFF behaviour so we
+          cannot use the default lost interrupt handler */
+       .lost_interrupt         = ATA_OP_NULL,
+       .sff_tf_load            = vsc_sata_tf_load,
+       .sff_tf_read            = vsc_sata_tf_read,
+       .freeze                 = vsc_freeze,
+       .thaw                   = vsc_thaw,
        .scr_read               = vsc_sata_scr_read,
        .scr_write              = vsc_sata_scr_write,
-       .port_start             = ata_port_start,
-       .port_stop              = ata_port_stop,
-       .host_stop              = ata_pci_host_stop,
 };
 
-static void __devinit vsc_sata_setup_port(struct ata_ioports *port, unsigned long base)
+static void __devinit vsc_sata_setup_port(struct ata_ioports *port,
+                                         void __iomem *base)
 {
        port->cmd_addr          = base + VSC_SATA_TF_CMD_OFFSET;
        port->data_addr         = base + VSC_SATA_TF_DATA_OFFSET;
@@ -332,90 +342,80 @@ static void __devinit vsc_sata_setup_port(struct ata_ioports *port, unsigned lon
 }
 
 
-static int __devinit vsc_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
+static int __devinit vsc_sata_init_one(struct pci_dev *pdev,
+                                      const struct pci_device_id *ent)
 {
+       static const struct ata_port_info pi = {
+               .flags          = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+                                 ATA_FLAG_MMIO,
+               .pio_mask       = ATA_PIO4,
+               .mwdma_mask     = ATA_MWDMA2,
+               .udma_mask      = ATA_UDMA6,
+               .port_ops       = &vsc_sata_ops,
+       };
+       const struct ata_port_info *ppi[] = { &pi, NULL };
        static int printed_version;
-       struct ata_probe_ent *probe_ent = NULL;
-       unsigned long base;
-       int pci_dev_busy = 0;
+       struct ata_host *host;
        void __iomem *mmio_base;
-       int rc;
+       int i, rc;
+       u8 cls;
 
        if (!printed_version++)
                dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
 
-       rc = pci_enable_device(pdev);
+       /* allocate host */
+       host = ata_host_alloc_pinfo(&pdev->dev, ppi, 4);
+       if (!host)
+               return -ENOMEM;
+
+       rc = pcim_enable_device(pdev);
        if (rc)
                return rc;
 
-       /*
-        * Check if we have needed resource mapped.
-        */
-       if (pci_resource_len(pdev, 0) == 0) {
-               rc = -ENODEV;
-               goto err_out;
-       }
+       /* check if we have needed resource mapped */
+       if (pci_resource_len(pdev, 0) == 0)
+               return -ENODEV;
+
+       /* map IO regions and intialize host accordingly */
+       rc = pcim_iomap_regions(pdev, 1 << VSC_MMIO_BAR, DRV_NAME);
+       if (rc == -EBUSY)
+               pcim_pin_device(pdev);
+       if (rc)
+               return rc;
+       host->iomap = pcim_iomap_table(pdev);
 
-       rc = pci_request_regions(pdev, DRV_NAME);
-       if (rc) {
-               pci_dev_busy = 1;
-               goto err_out;
+       mmio_base = host->iomap[VSC_MMIO_BAR];
+
+       for (i = 0; i < host->n_ports; i++) {
+               struct ata_port *ap = host->ports[i];
+               unsigned int offset = (i + 1) * VSC_SATA_PORT_OFFSET;
+
+               vsc_sata_setup_port(&ap->ioaddr, mmio_base + offset);
+
+               ata_port_pbar_desc(ap, VSC_MMIO_BAR, -1, "mmio");
+               ata_port_pbar_desc(ap, VSC_MMIO_BAR, offset, "port");
        }
 
        /*
         * Use 32 bit DMA mask, because 64 bit address support is poor.
         */
-       rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
+       rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
        if (rc)
-               goto err_out_regions;
-       rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
+               return rc;
+       rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
        if (rc)
-               goto err_out_regions;
-
-       probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
-       if (probe_ent == NULL) {
-               rc = -ENOMEM;
-               goto err_out_regions;
-       }
-       memset(probe_ent, 0, sizeof(*probe_ent));
-       probe_ent->dev = pci_dev_to_dev(pdev);
-       INIT_LIST_HEAD(&probe_ent->node);
-
-       mmio_base = pci_iomap(pdev, 0, 0);
-       if (mmio_base == NULL) {
-               rc = -ENOMEM;
-               goto err_out_free_ent;
-       }
-       base = (unsigned long) mmio_base;
+               return rc;
 
        /*
-        * Due to a bug in the chip, the default cache line size can't be used
-        */
-       pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x80);
-
-       probe_ent->sht = &vsc_sata_sht;
-       probe_ent->port_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
-                               ATA_FLAG_MMIO;
-       probe_ent->port_ops = &vsc_sata_ops;
-       probe_ent->n_ports = 4;
-       probe_ent->irq = pdev->irq;
-       probe_ent->irq_flags = IRQF_SHARED;
-       probe_ent->mmio_base = mmio_base;
-
-       /* We don't care much about the PIO/UDMA masks, but the core won't like us
-        * if we don't fill these
+        * Due to a bug in the chip, the default cache line size can't be
+        * used (unless the default is non-zero).
         */
-       probe_ent->pio_mask = 0x1f;
-       probe_ent->mwdma_mask = 0x07;
-       probe_ent->udma_mask = 0x7f;
+       pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cls);
+       if (cls == 0x00)
+               pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x80);
 
-       /* We have 4 ports per PCI function */
-       vsc_sata_setup_port(&probe_ent->port[0], base + 1 * VSC_SATA_PORT_OFFSET);
-       vsc_sata_setup_port(&probe_ent->port[1], base + 2 * VSC_SATA_PORT_OFFSET);
-       vsc_sata_setup_port(&probe_ent->port[2], base + 3 * VSC_SATA_PORT_OFFSET);
-       vsc_sata_setup_port(&probe_ent->port[3], base + 4 * VSC_SATA_PORT_OFFSET);
-
-       pci_set_master(pdev);
+       if (pci_enable_msi(pdev) == 0)
+               pci_intx(pdev, 0);
 
        /*
         * Config offset 0x98 is "Extended Control and Status Register 0"
@@ -425,20 +425,9 @@ static int __devinit vsc_sata_init_one (struct pci_dev *pdev, const struct pci_d
         */
        pci_write_config_dword(pdev, 0x98, 0);
 
-       /* FIXME: check ata_device_add return value */
-       ata_device_add(probe_ent);
-       kfree(probe_ent);
-
-       return 0;
-
-err_out_free_ent:
-       kfree(probe_ent);
-err_out_regions:
-       pci_release_regions(pdev);
-err_out:
-       if (!pci_dev_busy)
-               pci_disable_device(pdev);
-       return rc;
+       pci_set_master(pdev);
+       return ata_host_activate(host, pdev->irq, vsc_sata_interrupt,
+                                IRQF_SHARED, &vsc_sata_sht);
 }
 
 static const struct pci_device_id vsc_sata_pci_tbl[] = {