]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/scsi/libata-bmdma.c
Merge tag 'r8169-20060920-00' of git://electric-eye.fr.zoreil.com/home/romieu/linux...
[linux-2.6-omap-h63xx.git] / drivers / scsi / libata-bmdma.c
index 96b4d2160df8e0e0fca628a3e3a7bf495239d62a..9ce221f25954426af69ce124bb11a4beaf0feff2 100644 (file)
@@ -32,7 +32,6 @@
  *
  */
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/pci.h>
 #include <linux/libata.h>
@@ -418,6 +417,385 @@ u8 ata_altstatus(struct ata_port *ap)
        return inb(ap->ioaddr.altstatus_addr);
 }
 
+/**
+ *     ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
+       u8 dmactl;
+       void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
+
+       /* load PRD table addr. */
+       mb();   /* make sure PRD table writes are visible to controller */
+       writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
+
+       /* specify data direction, triple-check start bit is clear */
+       dmactl = readb(mmio + ATA_DMA_CMD);
+       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
+       if (!rw)
+               dmactl |= ATA_DMA_WR;
+       writeb(dmactl, mmio + ATA_DMA_CMD);
+
+       /* issue r/w command */
+       ap->ops->exec_command(ap, &qc->tf);
+}
+
+/**
+ *     ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
+       u8 dmactl;
+
+       /* start host DMA transaction */
+       dmactl = readb(mmio + ATA_DMA_CMD);
+       writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
+
+       /* Strictly, one may wish to issue a readb() here, to
+        * flush the mmio write.  However, control also passes
+        * to the hardware at this point, and it will interrupt
+        * us when we are to resume control.  So, in effect,
+        * we don't care when the mmio write flushes.
+        * Further, a read of the DMA status register _immediately_
+        * following the write may not be what certain flaky hardware
+        * is expected, so I think it is best to not add a readb()
+        * without first all the MMIO ATA cards/mobos.
+        * Or maybe I'm just being paranoid.
+        */
+}
+
+/**
+ *     ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
+       u8 dmactl;
+
+       /* load PRD table addr. */
+       outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
+
+       /* specify data direction, triple-check start bit is clear */
+       dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
+       if (!rw)
+               dmactl |= ATA_DMA_WR;
+       outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+
+       /* issue r/w command */
+       ap->ops->exec_command(ap, &qc->tf);
+}
+
+/**
+ *     ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       u8 dmactl;
+
+       /* start host DMA transaction */
+       dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+       outb(dmactl | ATA_DMA_START,
+            ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+}
+
+
+/**
+ *     ata_bmdma_start - Start a PCI IDE BMDMA transaction
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     Writes the ATA_DMA_START flag to the DMA command register.
+ *
+ *     May be used as the bmdma_start() entry in ata_port_operations.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+void ata_bmdma_start(struct ata_queued_cmd *qc)
+{
+       if (qc->ap->flags & ATA_FLAG_MMIO)
+               ata_bmdma_start_mmio(qc);
+       else
+               ata_bmdma_start_pio(qc);
+}
+
+
+/**
+ *     ata_bmdma_setup - Set up PCI IDE BMDMA transaction
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     Writes address of PRD table to device's PRD Table Address
+ *     register, sets the DMA control register, and calls
+ *     ops->exec_command() to start the transfer.
+ *
+ *     May be used as the bmdma_setup() entry in ata_port_operations.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+void ata_bmdma_setup(struct ata_queued_cmd *qc)
+{
+       if (qc->ap->flags & ATA_FLAG_MMIO)
+               ata_bmdma_setup_mmio(qc);
+       else
+               ata_bmdma_setup_pio(qc);
+}
+
+
+/**
+ *     ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
+ *     @ap: Port associated with this ATA transaction.
+ *
+ *     Clear interrupt and error flags in DMA status register.
+ *
+ *     May be used as the irq_clear() entry in ata_port_operations.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+void ata_bmdma_irq_clear(struct ata_port *ap)
+{
+       if (!ap->ioaddr.bmdma_addr)
+               return;
+
+       if (ap->flags & ATA_FLAG_MMIO) {
+               void __iomem *mmio =
+                     ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
+               writeb(readb(mmio), mmio);
+       } else {
+               unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
+               outb(inb(addr), addr);
+       }
+}
+
+
+/**
+ *     ata_bmdma_status - Read PCI IDE BMDMA status
+ *     @ap: Port associated with this ATA transaction.
+ *
+ *     Read and return BMDMA status register.
+ *
+ *     May be used as the bmdma_status() entry in ata_port_operations.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+u8 ata_bmdma_status(struct ata_port *ap)
+{
+       u8 host_stat;
+       if (ap->flags & ATA_FLAG_MMIO) {
+               void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
+               host_stat = readb(mmio + ATA_DMA_STATUS);
+       } else
+               host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
+       return host_stat;
+}
+
+
+/**
+ *     ata_bmdma_stop - Stop PCI IDE BMDMA transfer
+ *     @qc: Command we are ending DMA for
+ *
+ *     Clears the ATA_DMA_START flag in the dma control register
+ *
+ *     May be used as the bmdma_stop() entry in ata_port_operations.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host_set lock)
+ */
+
+void ata_bmdma_stop(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       if (ap->flags & ATA_FLAG_MMIO) {
+               void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
+
+               /* clear start/stop bit */
+               writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
+                       mmio + ATA_DMA_CMD);
+       } else {
+               /* clear start/stop bit */
+               outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
+                       ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+       }
+
+       /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
+       ata_altstatus(ap);        /* dummy read */
+}
+
+/**
+ *     ata_bmdma_freeze - Freeze BMDMA controller port
+ *     @ap: port to freeze
+ *
+ *     Freeze BMDMA controller port.
+ *
+ *     LOCKING:
+ *     Inherited from caller.
+ */
+void ata_bmdma_freeze(struct ata_port *ap)
+{
+       struct ata_ioports *ioaddr = &ap->ioaddr;
+
+       ap->ctl |= ATA_NIEN;
+       ap->last_ctl = ap->ctl;
+
+       if (ap->flags & ATA_FLAG_MMIO)
+               writeb(ap->ctl, (void __iomem *)ioaddr->ctl_addr);
+       else
+               outb(ap->ctl, ioaddr->ctl_addr);
+}
+
+/**
+ *     ata_bmdma_thaw - Thaw BMDMA controller port
+ *     @ap: port to thaw
+ *
+ *     Thaw BMDMA controller port.
+ *
+ *     LOCKING:
+ *     Inherited from caller.
+ */
+void ata_bmdma_thaw(struct ata_port *ap)
+{
+       /* clear & re-enable interrupts */
+       ata_chk_status(ap);
+       ap->ops->irq_clear(ap);
+       if (ap->ioaddr.ctl_addr)        /* FIXME: hack. create a hook instead */
+               ata_irq_on(ap);
+}
+
+/**
+ *     ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
+ *     @ap: port to handle error for
+ *     @prereset: prereset method (can be NULL)
+ *     @softreset: softreset method (can be NULL)
+ *     @hardreset: hardreset method (can be NULL)
+ *     @postreset: postreset method (can be NULL)
+ *
+ *     Handle error for ATA BMDMA controller.  It can handle both
+ *     PATA and SATA controllers.  Many controllers should be able to
+ *     use this EH as-is or with some added handling before and
+ *     after.
+ *
+ *     This function is intended to be used for constructing
+ *     ->error_handler callback by low level drivers.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ */
+void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
+                       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
+                       ata_postreset_fn_t postreset)
+{
+       struct ata_eh_context *ehc = &ap->eh_context;
+       struct ata_queued_cmd *qc;
+       unsigned long flags;
+       int thaw = 0;
+
+       qc = __ata_qc_from_tag(ap, ap->active_tag);
+       if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
+               qc = NULL;
+
+       /* reset PIO HSM and stop DMA engine */
+       spin_lock_irqsave(ap->lock, flags);
+
+       ap->hsm_task_state = HSM_ST_IDLE;
+
+       if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
+                  qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
+               u8 host_stat;
+
+               host_stat = ata_bmdma_status(ap);
+
+               ata_ehi_push_desc(&ehc->i, "BMDMA stat 0x%x", host_stat);
+
+               /* BMDMA controllers indicate host bus error by
+                * setting DMA_ERR bit and timing out.  As it wasn't
+                * really a timeout event, adjust error mask and
+                * cancel frozen state.
+                */
+               if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
+                       qc->err_mask = AC_ERR_HOST_BUS;
+                       thaw = 1;
+               }
+
+               ap->ops->bmdma_stop(qc);
+       }
+
+       ata_altstatus(ap);
+       ata_chk_status(ap);
+       ap->ops->irq_clear(ap);
+
+       spin_unlock_irqrestore(ap->lock, flags);
+
+       if (thaw)
+               ata_eh_thaw_port(ap);
+
+       /* PIO and DMA engines have been stopped, perform recovery */
+       ata_do_eh(ap, prereset, softreset, hardreset, postreset);
+}
+
+/**
+ *     ata_bmdma_error_handler - Stock error handler for BMDMA controller
+ *     @ap: port to handle error for
+ *
+ *     Stock error handler for BMDMA controller.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ */
+void ata_bmdma_error_handler(struct ata_port *ap)
+{
+       ata_reset_fn_t hardreset;
+
+       hardreset = NULL;
+       if (sata_scr_valid(ap))
+               hardreset = sata_std_hardreset;
+
+       ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
+                          ata_std_postreset);
+}
+
+/**
+ *     ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
+ *                                   BMDMA controller
+ *     @qc: internal command to clean up
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ */
+void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
+{
+       ata_bmdma_stop(qc);
+}
+
 #ifdef CONFIG_PCI
 static struct ata_probe_ent *
 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
@@ -469,12 +847,13 @@ ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int
        struct ata_probe_ent *probe_ent =
                ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
        int p = 0;
+       unsigned long bmdma;
 
        if (!probe_ent)
                return NULL;
 
        probe_ent->irq = pdev->irq;
-       probe_ent->irq_flags = SA_SHIRQ;
+       probe_ent->irq_flags = IRQF_SHARED;
        probe_ent->private_data = port[0]->private_data;
 
        if (ports & ATA_PORT_PRIMARY) {
@@ -482,7 +861,12 @@ ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int
                probe_ent->port[p].altstatus_addr =
                probe_ent->port[p].ctl_addr =
                        pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
-               probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
+               bmdma = pci_resource_start(pdev, 4);
+               if (bmdma) {
+                       if (inb(bmdma + 2) & 0x80)
+                               probe_ent->host_set_flags |= ATA_HOST_SIMPLEX;
+                       probe_ent->port[p].bmdma_addr = bmdma;
+               }
                ata_std_ports(&probe_ent->port[p]);
                p++;
        }
@@ -492,7 +876,13 @@ ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int
                probe_ent->port[p].altstatus_addr =
                probe_ent->port[p].ctl_addr =
                        pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
-               probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
+               bmdma = pci_resource_start(pdev, 4);
+               if (bmdma) {
+                       bmdma += 8;
+                       if(inb(bmdma + 2) & 0x80)
+                       probe_ent->host_set_flags |= ATA_HOST_SIMPLEX;
+                       probe_ent->port[p].bmdma_addr = bmdma;
+               }
                ata_std_ports(&probe_ent->port[p]);
                p++;
        }
@@ -506,6 +896,7 @@ static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
                                struct ata_port_info *port, int port_num)
 {
        struct ata_probe_ent *probe_ent;
+       unsigned long bmdma;
 
        probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
        if (!probe_ent)
@@ -532,8 +923,13 @@ static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
                        break;
        }
 
-       probe_ent->port[0].bmdma_addr =
-               pci_resource_start(pdev, 4) + 8 * port_num;
+       bmdma = pci_resource_start(pdev, 4);
+       if (bmdma != 0) {
+               bmdma += 8 * port_num;
+               probe_ent->port[0].bmdma_addr = bmdma;
+               if (inb(bmdma + 2) & 0x80)
+                       probe_ent->host_set_flags |= ATA_HOST_SIMPLEX;
+       }
        ata_std_ports(&probe_ent->port[0]);
 
        return probe_ent;
@@ -678,10 +1074,21 @@ int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
 
        /* FIXME: check ata_device_add return */
        if (legacy_mode) {
-               if (legacy_mode & (1 << 0))
+               struct device *dev = &pdev->dev;
+               struct ata_host_set *host_set = NULL;
+
+               if (legacy_mode & (1 << 0)) {
                        ata_device_add(probe_ent);
-               if (legacy_mode & (1 << 1))
+                       host_set = dev_get_drvdata(dev);
+               }
+
+               if (legacy_mode & (1 << 1)) {
                        ata_device_add(probe_ent2);
+                       if (host_set) {
+                               host_set->next = dev_get_drvdata(dev);
+                               dev_set_drvdata(dev, host_set);
+                       }
+               }
        } else
                ata_device_add(probe_ent);
 
@@ -707,7 +1114,7 @@ err_out:
  *     @pdev: PCI device
  *
  *     Some PCI ATA devices report simplex mode but in fact can be told to
- *     enter non simplex mode. This implements the neccessary logic to 
+ *     enter non simplex mode. This implements the neccessary logic to
  *     perform the task on such devices. Calling it on other devices will
  *     have -undefined- behaviour.
  */
@@ -732,7 +1139,7 @@ unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_devic
 {
        /* Filter out DMA modes if the device has been configured by
           the BIOS as PIO only */
-          
+
        if (ap->ioaddr.bmdma_addr == 0)
                xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
        return xfer_mask;