libata-acpi: fix _GTF command protocol for ATAPI devices
[powerpc.git] / drivers / ata / libata-sff.c
index d551fa1..d211db6 100644 (file)
@@ -532,101 +532,6 @@ static int ata_resources_present(struct pci_dev *pdev, int port)
        return 1;
 }
 
-/**
- *     ata_pci_init_native_mode - Initialize native-mode driver
- *     @pdev:  pci device to be initialized
- *     @port:  array[2] of pointers to port info structures.
- *     @ports: bitmap of ports present
- *
- *     Utility function which allocates and initializes an
- *     ata_probe_ent structure for a standard dual-port
- *     PIO-based IDE controller.  The returned ata_probe_ent
- *     structure can be passed to ata_device_add().  The returned
- *     ata_probe_ent structure should then be freed with kfree().
- *
- *     The caller need only pass the address of the primary port, the
- *     secondary will be deduced automatically. If the device has non
- *     standard secondary port mappings this function can be called twice,
- *     once for each interface.
- */
-
-struct ata_probe_ent *
-ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
-{
-       struct ata_probe_ent *probe_ent;
-       int i;
-       void __iomem * const *iomap;
-
-       /* Discard disabled ports.  Some controllers show their unused
-        * channels this way.  Disabled ports will be made dummy.
-        */
-       if (ata_resources_present(pdev, 0) == 0)
-               ports &= ~ATA_PORT_PRIMARY;
-       if (ata_resources_present(pdev, 1) == 0)
-               ports &= ~ATA_PORT_SECONDARY;
-
-       if (!ports) {
-               dev_printk(KERN_ERR, &pdev->dev, "no available port\n");
-               return NULL;
-       }
-
-       /* iomap BARs */
-       for (i = 0; i < 4; i++) {
-               if (!(ports & (1 << (i / 2))))
-                       continue;
-               if (pcim_iomap(pdev, i, 0) == NULL) {
-                       dev_printk(KERN_ERR, &pdev->dev,
-                                  "failed to iomap PCI BAR %d\n", i);
-                       return NULL;
-               }
-       }
-
-       pcim_iomap(pdev, 4, 0); /* may fail */
-       iomap = pcim_iomap_table(pdev);
-
-       /* alloc and init probe_ent */
-       probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
-       if (!probe_ent)
-               return NULL;
-
-       probe_ent->n_ports = 2;
-       probe_ent->irq = pdev->irq;
-       probe_ent->irq_flags = IRQF_SHARED;
-
-       if (ports & ATA_PORT_PRIMARY) {
-               probe_ent->port[0].cmd_addr = iomap[0];
-               probe_ent->port[0].altstatus_addr =
-               probe_ent->port[0].ctl_addr = (void __iomem *)
-                       ((unsigned long)iomap[1] | ATA_PCI_CTL_OFS);
-               if (iomap[4]) {
-                       if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                           (ioread8(iomap[4] + 2) & 0x80))
-                               probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
-                       probe_ent->port[0].bmdma_addr = iomap[4];
-               }
-               ata_std_ports(&probe_ent->port[0]);
-       } else
-               probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
-
-       if (ports & ATA_PORT_SECONDARY) {
-               probe_ent->port[1].cmd_addr = iomap[2];
-               probe_ent->port[1].altstatus_addr =
-               probe_ent->port[1].ctl_addr = (void __iomem *)
-                       ((unsigned long)iomap[3] | ATA_PCI_CTL_OFS);
-               if (iomap[4]) {
-                       if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                           (ioread8(iomap[4] + 10) & 0x80))
-                               probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
-                       probe_ent->port[1].bmdma_addr = iomap[4] + 8;
-               }
-               ata_std_ports(&probe_ent->port[1]);
-               probe_ent->pinfo2 = port[1];
-       } else
-               probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
-
-       return probe_ent;
-}
-
 /**
  *     ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
  *     @host: target ATA host
@@ -661,15 +566,14 @@ static int ata_pci_init_bmdma(struct ata_host *host)
        }
        host->iomap = pcim_iomap_table(pdev);
 
-       for (i = 0; i < 2; i++) {
+       for (i = 0; i < host->n_ports; i++) {
                struct ata_port *ap = host->ports[i];
-               struct ata_ioports *ioaddr = &ap->ioaddr;
                void __iomem *bmdma = host->iomap[4] + 8 * i;
 
                if (ata_port_is_dummy(ap))
                        continue;
 
-               ioaddr->bmdma_addr = bmdma;
+               ap->ioaddr.bmdma_addr = bmdma;
                if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
                    (ioread8(bmdma + 2) & 0x80))
                        host->flags |= ATA_HOST_SIMPLEX;
@@ -742,6 +646,70 @@ int ata_pci_init_native_host(struct ata_host *host, unsigned int port_mask)
        return 0;
 }
 
+/**
+ *     ata_pci_prepare_native_host - helper to prepare native PCI ATA host
+ *     @pdev: target PCI device
+ *     @ppi: array of port_info
+ *     @n_ports: number of ports to allocate
+ *     @r_host: out argument for the initialized ATA host
+ *
+ *     Helper to allocate ATA host for @pdev, acquire all native PCI
+ *     resources and initialize it accordingly in one go.
+ *
+ *     LOCKING:
+ *     Inherited from calling layer (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
+ */
+int ata_pci_prepare_native_host(struct pci_dev *pdev,
+                               const struct ata_port_info * const * ppi,
+                               int n_ports, struct ata_host **r_host)
+{
+       struct ata_host *host;
+       unsigned int port_mask;
+       int rc;
+
+       if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
+               return -ENOMEM;
+
+       host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
+       if (!host) {
+               dev_printk(KERN_ERR, &pdev->dev,
+                          "failed to allocate ATA host\n");
+               rc = -ENOMEM;
+               goto err_out;
+       }
+
+       port_mask = ATA_PORT_PRIMARY;
+       if (n_ports > 1)
+               port_mask |= ATA_PORT_SECONDARY;
+
+       rc = ata_pci_init_native_host(host, port_mask);
+       if (rc)
+               goto err_out;
+
+       /* init DMA related stuff */
+       rc = ata_pci_init_bmdma(host);
+       if (rc)
+               goto err_bmdma;
+
+       devres_remove_group(&pdev->dev, NULL);
+       *r_host = host;
+       return 0;
+
+ err_bmdma:
+       /* This is necessary because PCI and iomap resources are
+        * merged and releasing the top group won't release the
+        * acquired resources if some of those have been acquired
+        * before entering this function.
+        */
+       pcim_iounmap_regions(pdev, 0xf);
+ err_out:
+       devres_release_group(&pdev->dev, NULL);
+       return rc;
+}
+
 struct ata_legacy_devres {
        unsigned int    mask;
        unsigned long   cmd_port[2];
@@ -907,7 +875,7 @@ static int ata_request_legacy_irqs(struct ata_host *host,
        legacy_dr = devres_find(host->dev, ata_legacy_release, NULL, NULL);
        BUG_ON(!legacy_dr);
 
-       for (i = 0; i < 2; i++) {
+       for (i = 0; i < host->n_ports; i++) {
                unsigned int irq;
 
                /* FIXME: ATA_*_IRQ() should take generic device not pci_dev */
@@ -995,10 +963,7 @@ int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
        BUG_ON(n_ports < 1 || n_ports > 2);
 
        port[0] = port_info[0];
-       if (n_ports > 1)
-               port[1] = port_info[1];
-       else
-               port[1] = port[0];
+       port[1] = (n_ports > 1) ? port_info[1] : NULL;
 
        /* FIXME: Really for ATA it isn't safe because the device may be
           multi-purpose and we want to leave it alone if it was already
@@ -1033,7 +998,7 @@ int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
        }
 
        /* alloc and init host */
-       host = ata_host_alloc_pinfo(dev, port, 2);
+       host = ata_host_alloc_pinfo(dev, port, n_ports);
        if (!host) {
                dev_printk(KERN_ERR, &pdev->dev,
                           "failed to allocate ATA host\n");