PCI: aerdrv: introduce default_downstream_reset_link
[safe/jmp/linux-2.6] / drivers / pci / msi.c
index a7f2a01..77b68ea 100644 (file)
@@ -16,9 +16,9 @@
 #include <linux/proc_fs.h>
 #include <linux/msi.h>
 #include <linux/smp.h>
-
-#include <asm/errno.h>
-#include <asm/io.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/slab.h>
 
 #include "pci.h"
 #include "msi.h"
@@ -390,7 +390,7 @@ static int msi_capability_init(struct pci_dev *dev, int nvec)
        u16 control;
        unsigned mask;
 
-       pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
+       pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
        msi_set_enable(dev, pos, 0);    /* Disable MSI during set up */
 
        pci_read_config_word(dev, msi_control_reg(pos), &control);
@@ -399,12 +399,12 @@ static int msi_capability_init(struct pci_dev *dev, int nvec)
        if (!entry)
                return -ENOMEM;
 
-       entry->msi_attrib.is_msix = 0;
-       entry->msi_attrib.is_64 = is_64bit_address(control);
-       entry->msi_attrib.entry_nr = 0;
-       entry->msi_attrib.maskbit = is_mask_bit_support(control);
-       entry->msi_attrib.default_irq = dev->irq;       /* Save IOAPIC IRQ */
-       entry->msi_attrib.pos = pos;
+       entry->msi_attrib.is_msix       = 0;
+       entry->msi_attrib.is_64         = is_64bit_address(control);
+       entry->msi_attrib.entry_nr      = 0;
+       entry->msi_attrib.maskbit       = is_mask_bit_support(control);
+       entry->msi_attrib.default_irq   = dev->irq;     /* Save IOAPIC IRQ */
+       entry->msi_attrib.pos           = pos;
 
        entry->mask_pos = msi_mask_reg(pos, entry->msi_attrib.is_64);
        /* All MSIs are unmasked by default, Mask them all */
@@ -432,44 +432,27 @@ static int msi_capability_init(struct pci_dev *dev, int nvec)
        return 0;
 }
 
-/**
- * msix_capability_init - configure device's MSI-X capability
- * @dev: pointer to the pci_dev data structure of MSI-X device function
- * @entries: pointer to an array of struct msix_entry entries
- * @nvec: number of @entries
- *
- * Setup the MSI-X capability structure of device function with a
- * single MSI-X irq. A return of zero indicates the successful setup of
- * requested MSI-X entries with allocated irqs or non-zero for otherwise.
- **/
-static int msix_capability_init(struct pci_dev *dev,
-                               struct msix_entry *entries, int nvec)
+static void __iomem *msix_map_region(struct pci_dev *dev, unsigned pos,
+                                                       unsigned nr_entries)
 {
-       struct msi_desc *entry;
-       int pos, i, j, nr_entries, ret;
        unsigned long phys_addr;
        u32 table_offset;
-       u16 control;
        u8 bir;
-       void __iomem *base;
-
-       pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
-       pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
 
-       /* Ensure MSI-X is disabled while it is set up */
-       control &= ~PCI_MSIX_FLAGS_ENABLE;
-       pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
-
-       /* Request & Map MSI-X table region */
-       nr_entries = multi_msix_capable(control);
-
-       pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
+       pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
        bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
        table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
-       phys_addr = pci_resource_start (dev, bir) + table_offset;
-       base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
-       if (base == NULL)
-               return -ENOMEM;
+       phys_addr = pci_resource_start(dev, bir) + table_offset;
+
+       return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
+}
+
+static int msix_setup_entries(struct pci_dev *dev, unsigned pos,
+                               void __iomem *base, struct msix_entry *entries,
+                               int nvec)
+{
+       struct msi_desc *entry;
+       int i;
 
        for (i = 0; i < nvec; i++) {
                entry = alloc_msi_entry(dev);
@@ -482,36 +465,73 @@ static int msix_capability_init(struct pci_dev *dev,
                        return -ENOMEM;
                }
 
-               j = entries[i].entry;
-               entry->msi_attrib.is_msix = 1;
-               entry->msi_attrib.is_64 = 1;
-               entry->msi_attrib.entry_nr = j;
-               entry->msi_attrib.default_irq = dev->irq;
-               entry->msi_attrib.pos = pos;
-               entry->mask_base = base;
+               entry->msi_attrib.is_msix       = 1;
+               entry->msi_attrib.is_64         = 1;
+               entry->msi_attrib.entry_nr      = entries[i].entry;
+               entry->msi_attrib.default_irq   = dev->irq;
+               entry->msi_attrib.pos           = pos;
+               entry->mask_base                = base;
 
                list_add_tail(&entry->list, &dev->msi_list);
        }
 
-       ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
-       if (ret < 0) {
-               /* If we had some success report the number of irqs
-                * we succeeded in setting up. */
-               int avail = 0;
-               list_for_each_entry(entry, &dev->msi_list, list) {
-                       if (entry->irq != 0) {
-                               avail++;
-                       }
-               }
+       return 0;
+}
 
-               if (avail != 0)
-                       ret = avail;
+static void msix_program_entries(struct pci_dev *dev,
+                                       struct msix_entry *entries)
+{
+       struct msi_desc *entry;
+       int i = 0;
+
+       list_for_each_entry(entry, &dev->msi_list, list) {
+               int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
+                                               PCI_MSIX_ENTRY_VECTOR_CTRL;
+
+               entries[i].vector = entry->irq;
+               set_irq_msi(entry->irq, entry);
+               entry->masked = readl(entry->mask_base + offset);
+               msix_mask_irq(entry, 1);
+               i++;
        }
+}
 
-       if (ret) {
-               free_msi_irqs(dev);
+/**
+ * msix_capability_init - configure device's MSI-X capability
+ * @dev: pointer to the pci_dev data structure of MSI-X device function
+ * @entries: pointer to an array of struct msix_entry entries
+ * @nvec: number of @entries
+ *
+ * Setup the MSI-X capability structure of device function with a
+ * single MSI-X irq. A return of zero indicates the successful setup of
+ * requested MSI-X entries with allocated irqs or non-zero for otherwise.
+ **/
+static int msix_capability_init(struct pci_dev *dev,
+                               struct msix_entry *entries, int nvec)
+{
+       int pos, ret;
+       u16 control;
+       void __iomem *base;
+
+       pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
+       pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
+
+       /* Ensure MSI-X is disabled while it is set up */
+       control &= ~PCI_MSIX_FLAGS_ENABLE;
+       pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
+
+       /* Request & Map MSI-X table region */
+       base = msix_map_region(dev, pos, multi_msix_capable(control));
+       if (!base)
+               return -ENOMEM;
+
+       ret = msix_setup_entries(dev, pos, base, entries, nvec);
+       if (ret)
                return ret;
-       }
+
+       ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
+       if (ret)
+               goto error;
 
        /*
         * Some devices require MSI-X to be enabled before we can touch the
@@ -521,16 +541,7 @@ static int msix_capability_init(struct pci_dev *dev,
        control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
        pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
 
-       i = 0;
-       list_for_each_entry(entry, &dev->msi_list, list) {
-               entries[i].vector = entry->irq;
-               set_irq_msi(entry->irq, entry);
-               j = entries[i].entry;
-               entry->masked = readl(base + j * PCI_MSIX_ENTRY_SIZE +
-                                               PCI_MSIX_ENTRY_VECTOR_CTRL);
-               msix_mask_irq(entry, 1);
-               i++;
-       }
+       msix_program_entries(dev, entries);
 
        /* Set MSI-X enabled bits and unmask the function */
        pci_intx_for_msi(dev, 0);
@@ -540,6 +551,27 @@ static int msix_capability_init(struct pci_dev *dev,
        pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
 
        return 0;
+
+error:
+       if (ret < 0) {
+               /*
+                * If we had some success, report the number of irqs
+                * we succeeded in setting up.
+                */
+               struct msi_desc *entry;
+               int avail = 0;
+
+               list_for_each_entry(entry, &dev->msi_list, list) {
+                       if (entry->irq != 0)
+                               avail++;
+               }
+               if (avail != 0)
+                       ret = avail;
+       }
+
+       free_msi_irqs(dev);
+
+       return ret;
 }
 
 /**
@@ -552,7 +584,7 @@ static int msix_capability_init(struct pci_dev *dev,
  * to determine if MSI/-X are supported for the device. If MSI/-X is
  * supported return 0, else return an error code.
  **/
-static int pci_msi_check_device(struct pci_devdev, int nvec, int type)
+static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
 {
        struct pci_bus *bus;
        int ret;
@@ -569,8 +601,9 @@ static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
        if (nvec < 1)
                return -ERANGE;
 
-       /* Any bridge which does NOT route MSI transactions from it's
-        * secondary bus to it's primary bus must set NO_MSI flag on
+       /*
+        * Any bridge which does NOT route MSI transactions from its
+        * secondary bus to its primary bus must set NO_MSI flag on
         * the secondary pci_bus.
         * We expect only arch-specific PCI host bus controller driver
         * or quirks for specific PCI bridges to be setting NO_MSI.
@@ -661,7 +694,7 @@ void pci_msi_shutdown(struct pci_dev *dev)
        dev->irq = desc->msi_attrib.default_irq;
 }
 
-void pci_disable_msi(struct pci_devdev)
+void pci_disable_msi(struct pci_dev *dev)
 {
        if (!pci_msi_enable || !dev || !dev->msi_enabled)
                return;
@@ -703,13 +736,13 @@ int pci_msix_table_size(struct pci_dev *dev)
  * of irqs or MSI-X vectors available. Driver should use the returned value to
  * re-send its request.
  **/
-int pci_enable_msix(struct pci_devdev, struct msix_entry *entries, int nvec)
+int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
 {
        int status, nr_entries;
        int i, j;
 
        if (!entries)
-               return -EINVAL;
+               return -EINVAL;
 
        status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
        if (status)
@@ -731,7 +764,7 @@ int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
        WARN_ON(!!dev->msix_enabled);
 
        /* Check whether driver already requested for MSI irq */
-       if (dev->msi_enabled) {
+       if (dev->msi_enabled) {
                dev_info(&dev->dev, "can't enable MSI-X "
                       "(MSI IRQ already assigned)\n");
                return -EINVAL;
@@ -741,7 +774,7 @@ int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
 }
 EXPORT_SYMBOL(pci_enable_msix);
 
-void pci_msix_shutdown(struct pci_devdev)
+void pci_msix_shutdown(struct pci_dev *dev)
 {
        struct msi_desc *entry;
 
@@ -759,7 +792,7 @@ void pci_msix_shutdown(struct pci_dev* dev)
        dev->msix_enabled = 0;
 }
 
-void pci_disable_msix(struct pci_devdev)
+void pci_disable_msix(struct pci_dev *dev)
 {
        if (!pci_msi_enable || !dev || !dev->msix_enabled)
                return;
@@ -778,10 +811,10 @@ EXPORT_SYMBOL(pci_disable_msix);
  * allocated for this device function, are reclaimed to unused state,
  * which may be used later on.
  **/
-void msi_remove_pci_irq_vectors(struct pci_devdev)
+void msi_remove_pci_irq_vectors(struct pci_dev *dev)
 {
        if (!pci_msi_enable || !dev)
-               return;
+               return;
 
        if (dev->msi_enabled || dev->msix_enabled)
                free_msi_irqs(dev);