Commit 3674cc49 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Bjorn Helgaas

PCI/portdrv: Use pci_irq_alloc_vectors()

Use pci_irq_alloc_vectors() and greatly simplify the code by managing the
vector number for the subservices directly.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarBjorn Helgaas <bhelgaas@google.com>
parent 862290f9
...@@ -43,53 +43,17 @@ static void release_pcie_device(struct device *dev) ...@@ -43,53 +43,17 @@ static void release_pcie_device(struct device *dev)
kfree(to_pcie_device(dev)); kfree(to_pcie_device(dev));
} }
/**
* pcie_port_msix_add_entry - add entry to given array of MSI-X entries
* @entries: Array of MSI-X entries
* @new_entry: Index of the entry to add to the array
* @nr_entries: Number of entries already in the array
*
* Return value: Position of the added entry in the array
*/
static int pcie_port_msix_add_entry(
struct msix_entry *entries, int new_entry, int nr_entries)
{
int j;
for (j = 0; j < nr_entries; j++)
if (entries[j].entry == new_entry)
return j;
entries[j].entry = new_entry;
return j;
}
/** /**
* pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port * pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port
* @dev: PCI Express port to handle * @dev: PCI Express port to handle
* @vectors: Array of interrupt vectors to populate * @irqs: Array of interrupt vectors to populate
* @mask: Bitmask of port capabilities returned by get_port_device_capability() * @mask: Bitmask of port capabilities returned by get_port_device_capability()
* *
* Return value: 0 on success, error code on failure * Return value: 0 on success, error code on failure
*/ */
static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask) static int pcie_port_enable_msix(struct pci_dev *dev, int *irqs, int mask)
{ {
struct msix_entry *msix_entries; int nr_entries, entry, nvec = 0;
int idx[PCIE_PORT_DEVICE_MAXSERVICES];
int nr_entries, status, pos, i, nvec;
u16 reg16;
u32 reg32;
nr_entries = pci_msix_vec_count(dev);
if (nr_entries < 0)
return nr_entries;
BUG_ON(!nr_entries);
if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES)
nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES;
msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL);
if (!msix_entries)
return -ENOMEM;
/* /*
* Allocate as many entries as the port wants, so that we can check * Allocate as many entries as the port wants, so that we can check
...@@ -97,20 +61,13 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask) ...@@ -97,20 +61,13 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
* equal to the number of entries this port actually uses, we'll happily * equal to the number of entries this port actually uses, we'll happily
* go through without any tricks. * go through without any tricks.
*/ */
for (i = 0; i < nr_entries; i++) nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSIX_ENTRIES,
msix_entries[i].entry = i; PCI_IRQ_MSIX);
if (nr_entries < 0)
status = pci_enable_msix_exact(dev, msix_entries, nr_entries); return nr_entries;
if (status)
goto Exit;
for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
idx[i] = -1;
status = -EIO;
nvec = 0;
if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) { if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
int entry; u16 reg16;
/* /*
* The code below follows the PCI Express Base Specification 2.0 * The code below follows the PCI Express Base Specification 2.0
...@@ -125,18 +82,16 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask) ...@@ -125,18 +82,16 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16); pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9; entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
if (entry >= nr_entries) if (entry >= nr_entries)
goto Error; goto out_free_irqs;
i = pcie_port_msix_add_entry(msix_entries, entry, nvec); irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, entry);
if (i == nvec) irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, entry);
nvec++;
idx[PCIE_PORT_SERVICE_PME_SHIFT] = i; nvec = max(nvec, entry + 1);
idx[PCIE_PORT_SERVICE_HP_SHIFT] = i;
} }
if (mask & PCIE_PORT_SERVICE_AER) { if (mask & PCIE_PORT_SERVICE_AER) {
int entry; u32 reg32, pos;
/* /*
* The code below follows Section 7.10.10 of the PCI Express * The code below follows Section 7.10.10 of the PCI Express
...@@ -151,13 +106,11 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask) ...@@ -151,13 +106,11 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32); pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
entry = reg32 >> 27; entry = reg32 >> 27;
if (entry >= nr_entries) if (entry >= nr_entries)
goto Error; goto out_free_irqs;
i = pcie_port_msix_add_entry(msix_entries, entry, nvec); irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, entry);
if (i == nvec)
nvec++;
idx[PCIE_PORT_SERVICE_AER_SHIFT] = i; nvec = max(nvec, entry + 1);
} }
/* /*
...@@ -165,41 +118,39 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask) ...@@ -165,41 +118,39 @@ static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
* what we have. Otherwise, the port has some extra entries not for the * what we have. Otherwise, the port has some extra entries not for the
* services we know and we need to work around that. * services we know and we need to work around that.
*/ */
if (nvec == nr_entries) { if (nvec != nr_entries) {
status = 0;
} else {
/* Drop the temporary MSI-X setup */ /* Drop the temporary MSI-X setup */
pci_disable_msix(dev); pci_free_irq_vectors(dev);
/* Now allocate the MSI-X vectors for real */ /* Now allocate the MSI-X vectors for real */
status = pci_enable_msix_exact(dev, msix_entries, nvec); nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
if (status) PCI_IRQ_MSIX);
goto Exit; if (nr_entries < 0)
return nr_entries;
} }
for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) return 0;
vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1;
Exit:
kfree(msix_entries);
return status;
Error: out_free_irqs:
pci_disable_msix(dev); pci_free_irq_vectors(dev);
goto Exit; return -EIO;
} }
/** /**
* init_service_irqs - initialize irqs for PCI Express port services * pcie_init_service_irqs - initialize irqs for PCI Express port services
* @dev: PCI Express port to handle * @dev: PCI Express port to handle
* @irqs: Array of irqs to populate * @irqs: Array of irqs to populate
* @mask: Bitmask of port capabilities returned by get_port_device_capability() * @mask: Bitmask of port capabilities returned by get_port_device_capability()
* *
* Return value: Interrupt mode associated with the port * Return value: Interrupt mode associated with the port
*/ */
static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask) static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
{ {
int i, irq = -1; unsigned flags = PCI_IRQ_LEGACY | PCI_IRQ_MSI;
int ret, i;
for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
irqs[i] = -1;
/* /*
* If MSI cannot be used for PCIe PME or hotplug, we have to use * If MSI cannot be used for PCIe PME or hotplug, we have to use
...@@ -207,41 +158,25 @@ static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask) ...@@ -207,41 +158,25 @@ static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
*/ */
if (((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) || if (((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) ||
((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())) { ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())) {
if (dev->irq) flags &= ~PCI_IRQ_MSI;
irq = dev->irq; } else {
goto no_msi;
}
/* Try to use MSI-X if supported */ /* Try to use MSI-X if supported */
if (!pcie_port_enable_msix(dev, irqs, mask)) if (!pcie_port_enable_msix(dev, irqs, mask))
return 0; return 0;
}
/* ret = pci_alloc_irq_vectors(dev, 1, 1, flags);
* We're not going to use MSI-X, so try MSI and fall back to INTx. if (ret < 0)
* If neither MSI/MSI-X nor INTx available, try other interrupt. On return -ENODEV;
* some platforms, root port doesn't support MSI/MSI-X/INTx in RC mode.
*/
if (!pci_enable_msi(dev) || dev->irq)
irq = dev->irq;
no_msi: for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) if (i != PCIE_PORT_SERVICE_VC_SHIFT)
irqs[i] = irq; irqs[i] = pci_irq_vector(dev, 0);
irqs[PCIE_PORT_SERVICE_VC_SHIFT] = -1; }
if (irq < 0)
return -ENODEV;
return 0; return 0;
} }
static void cleanup_service_irqs(struct pci_dev *dev)
{
if (dev->msix_enabled)
pci_disable_msix(dev);
else if (dev->msi_enabled)
pci_disable_msi(dev);
}
/** /**
* get_port_device_capability - discover capabilities of a PCI Express port * get_port_device_capability - discover capabilities of a PCI Express port
* @dev: PCI Express port to examine * @dev: PCI Express port to examine
...@@ -378,7 +313,7 @@ int pcie_port_device_register(struct pci_dev *dev) ...@@ -378,7 +313,7 @@ int pcie_port_device_register(struct pci_dev *dev)
* that can be used in the absence of irqs. Allow them to determine * that can be used in the absence of irqs. Allow them to determine
* if that is to be used. * if that is to be used.
*/ */
status = init_service_irqs(dev, irqs, capabilities); status = pcie_init_service_irqs(dev, irqs, capabilities);
if (status) { if (status) {
capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP; capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
if (!capabilities) if (!capabilities)
...@@ -401,7 +336,7 @@ int pcie_port_device_register(struct pci_dev *dev) ...@@ -401,7 +336,7 @@ int pcie_port_device_register(struct pci_dev *dev)
return 0; return 0;
error_cleanup_irqs: error_cleanup_irqs:
cleanup_service_irqs(dev); pci_free_irq_vectors(dev);
error_disable: error_disable:
pci_disable_device(dev); pci_disable_device(dev);
return status; return status;
...@@ -469,7 +404,7 @@ static int remove_iter(struct device *dev, void *data) ...@@ -469,7 +404,7 @@ static int remove_iter(struct device *dev, void *data)
void pcie_port_device_remove(struct pci_dev *dev) void pcie_port_device_remove(struct pci_dev *dev)
{ {
device_for_each_child(&dev->dev, NULL, remove_iter); device_for_each_child(&dev->dev, NULL, remove_iter);
cleanup_service_irqs(dev); pci_free_irq_vectors(dev);
pci_disable_device(dev); pci_disable_device(dev);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment