2021-02-16 14:21:00

by Dejin Zheng

[permalink] [raw]
Subject: [PATCH v2 0/4] Introduce pcim_alloc_irq_vectors()

Introduce pcim_alloc_irq_vectors(), a device-managed version of
pci_alloc_irq_vectors(), In some i2c drivers, If pcim_enable_device()
has been called before, then pci_alloc_irq_vectors() is actually a
device-managed function. It is used as a device-managed function, So
replace it with pcim_alloc_irq_vectors().

Changelog
---------
v1 -> v2:
- Use pci_free_irq_vectors() to replace some code in
pcim_release().
- Modify some commit messages.

Andy and Krzysztof, thanks for your help!

Dejin Zheng (4):
PCI: Introduce pcim_alloc_irq_vectors()
Documentation: devres: Add pcim_alloc_irq_vectors()
i2c: designware: Use the correct name of device-managed function
i2c: thunderx: Use the correct name of device-managed function

.../driver-api/driver-model/devres.rst | 1 +
drivers/i2c/busses/i2c-designware-pcidrv.c | 2 +-
drivers/i2c/busses/i2c-thunderx-pcidrv.c | 2 +-
drivers/pci/pci.c | 33 ++++++++++++++++---
include/linux/pci.h | 3 ++
5 files changed, 35 insertions(+), 6 deletions(-)

--
2.25.0


2021-02-16 14:21:13

by Dejin Zheng

[permalink] [raw]
Subject: [PATCH v2 2/4] Documentation: devres: Add pcim_alloc_irq_vectors()

Add pcim_alloc_irq_vectors(), a device-managed version of
pci_alloc_irq_vectors(). introducing this function can simplify
the error handling path in many drivers.

Signed-off-by: Dejin Zheng <[email protected]>
---
v1 -> v2:
- Modify some commit messages.

Documentation/driver-api/driver-model/devres.rst | 1 +
1 file changed, 1 insertion(+)

diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index cd8b6e657b94..a52f65b6352f 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -380,6 +380,7 @@ PCI
devm_pci_alloc_host_bridge() : managed PCI host bridge allocation
devm_pci_remap_cfgspace() : ioremap PCI configuration space
devm_pci_remap_cfg_resource() : ioremap PCI configuration space resource
+ pcim_alloc_irq_vectors() : managed IRQ vectors allocation
pcim_enable_device() : after success, all PCI ops become managed
pcim_pin_device() : keep PCI device enabled after release

--
2.25.0

2021-02-16 14:21:38

by Dejin Zheng

[permalink] [raw]
Subject: [PATCH v2 1/4] PCI: Introduce pcim_alloc_irq_vectors()

Introduce pcim_alloc_irq_vectors(), a device-managed version of
pci_alloc_irq_vectors(). Introducing this function can simplify
the error handling path in many drivers.

Suggested-by: Andy Shevchenko <[email protected]>
Signed-off-by: Dejin Zheng <[email protected]>
---
v1 -> v2:
- Use pci_free_irq_vectors() to replace some code in
pcim_release().
- Modify some commit messages.

drivers/pci/pci.c | 33 +++++++++++++++++++++++++++++----
include/linux/pci.h | 3 +++
2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index b67c4327d307..db799d089c85 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -1969,10 +1969,7 @@ static void pcim_release(struct device *gendev, void *res)
struct pci_devres *this = res;
int i;

- if (dev->msi_enabled)
- pci_disable_msi(dev);
- if (dev->msix_enabled)
- pci_disable_msix(dev);
+ pci_free_irq_vectors(dev);

for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
if (this->region_mask & (1 << i))
@@ -2054,6 +2051,34 @@ void pcim_pin_device(struct pci_dev *pdev)
}
EXPORT_SYMBOL(pcim_pin_device);

+/**
+ * pcim_alloc_irq_vectors - a device-managed pci_alloc_irq_vectors()
+ * @dev: PCI device to operate on
+ * @min_vecs: minimum number of vectors required (must be >= 1)
+ * @max_vecs: maximum (desired) number of vectors
+ * @flags: flags or quirks for the allocation
+ *
+ * Return the number of vectors allocated, (which might be smaller than
+ * @max_vecs) if successful, or a negative error code on error. If less
+ * than @min_vecs interrupt vectors are available for @dev the function
+ * will fail with -ENOSPC.
+ *
+ * It depends on calling pcim_enable_device() to make IRQ resources
+ * manageable.
+ */
+int pcim_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
+ unsigned int max_vecs, unsigned int flags)
+{
+ struct pci_devres *dr;
+
+ dr = find_pci_dr(dev);
+ if (!dr || !dr->enabled)
+ return -EINVAL;
+
+ return pci_alloc_irq_vectors(dev, min_vecs, max_vecs, flags);
+}
+EXPORT_SYMBOL(pcim_alloc_irq_vectors);
+
/*
* pcibios_add_device - provide arch specific hooks when adding device dev
* @dev: the PCI device being added
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 86c799c97b77..d75ba85ddfc5 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1818,6 +1818,9 @@ pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
NULL);
}

+int pcim_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
+ unsigned int max_vecs, unsigned int flags);
+
/* Include architecture-dependent settings and functions */

#include <asm/pci.h>
--
2.25.0

2021-02-16 14:22:11

by Dejin Zheng

[permalink] [raw]
Subject: [PATCH v2 4/4] i2c: thunderx: Use the correct name of device-managed function

Use the new function pcim_alloc_irq_vectors() to allocate IRQ vectors,
the pcim_alloc_irq_vectors() function, an explicit device-managed version
of pci_alloc_irq_vectors(). If pcim_enable_device() has been called
before, then pci_alloc_irq_vectors() is actually a device-managed
function. It is used here as a device-managed function, So replace it
with pcim_alloc_irq_vectors().

Signed-off-by: Dejin Zheng <[email protected]>
---
v1 -> v2:
- Modify some commit messages.

drivers/i2c/busses/i2c-thunderx-pcidrv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-thunderx-pcidrv.c b/drivers/i2c/busses/i2c-thunderx-pcidrv.c
index 12c90aa0900e..63354e9fb726 100644
--- a/drivers/i2c/busses/i2c-thunderx-pcidrv.c
+++ b/drivers/i2c/busses/i2c-thunderx-pcidrv.c
@@ -192,7 +192,7 @@ static int thunder_i2c_probe_pci(struct pci_dev *pdev,
i2c->hlc_int_enable = thunder_i2c_hlc_int_enable;
i2c->hlc_int_disable = thunder_i2c_hlc_int_disable;

- ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSIX);
+ ret = pcim_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSIX);
if (ret < 0)
goto error;

--
2.25.0

2021-02-16 14:22:35

by Dejin Zheng

[permalink] [raw]
Subject: [PATCH v2 3/4] i2c: designware: Use the correct name of device-managed function

Use the new function pcim_alloc_irq_vectors() to allocate IRQ vectors,
the pcim_alloc_irq_vectors() function, an explicit device-managed version
of pci_alloc_irq_vectors(). If pcim_enable_device() has been called
before, then pci_alloc_irq_vectors() is actually a device-managed
function. It is used here as a device-managed function, So replace it
with pcim_alloc_irq_vectors().

Signed-off-by: Dejin Zheng <[email protected]>
---
v1 -> v2:
- Modify some commit messages.

drivers/i2c/busses/i2c-designware-pcidrv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
index 55c83a7a24f3..444533be49ee 100644
--- a/drivers/i2c/busses/i2c-designware-pcidrv.c
+++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
@@ -219,7 +219,7 @@ static int i2c_dw_pci_probe(struct pci_dev *pdev,
if (!dev)
return -ENOMEM;

- r = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
+ r = pcim_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
if (r < 0)
return r;

--
2.25.0

2021-02-16 14:44:47

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] i2c: designware: Use the correct name of device-managed function

On Tue, Feb 16, 2021 at 10:18:09PM +0800, Dejin Zheng wrote:
> Use the new function pcim_alloc_irq_vectors() to allocate IRQ vectors,
> the pcim_alloc_irq_vectors() function, an explicit device-managed version
> of pci_alloc_irq_vectors(). If pcim_enable_device() has been called
> before, then pci_alloc_irq_vectors() is actually a device-managed
> function. It is used here as a device-managed function, So replace it
> with pcim_alloc_irq_vectors().

...

> - r = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
> + r = pcim_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
> if (r < 0)
> return r;

It's good, but now why do we have pci_free_irq_vectors() in the same file?


--
With Best Regards,
Andy Shevchenko


2021-02-16 14:45:05

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/4] PCI: Introduce pcim_alloc_irq_vectors()

On Tue, Feb 16, 2021 at 10:18:07PM +0800, Dejin Zheng wrote:
> Introduce pcim_alloc_irq_vectors(), a device-managed version of
> pci_alloc_irq_vectors(). Introducing this function can simplify
> the error handling path in many drivers.

> - if (dev->msi_enabled)
> - pci_disable_msi(dev);
> - if (dev->msix_enabled)
> - pci_disable_msix(dev);
> + pci_free_irq_vectors(dev);

This change deserves a line in the commit message.

--
With Best Regards,
Andy Shevchenko


2021-02-16 16:16:55

by Dejin Zheng

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] i2c: designware: Use the correct name of device-managed function

On Tue, Feb 16, 2021 at 04:39:09PM +0200, Andy Shevchenko wrote:
> On Tue, Feb 16, 2021 at 10:18:09PM +0800, Dejin Zheng wrote:
> > Use the new function pcim_alloc_irq_vectors() to allocate IRQ vectors,
> > the pcim_alloc_irq_vectors() function, an explicit device-managed version
> > of pci_alloc_irq_vectors(). If pcim_enable_device() has been called
> > before, then pci_alloc_irq_vectors() is actually a device-managed
> > function. It is used here as a device-managed function, So replace it
> > with pcim_alloc_irq_vectors().
>
> ...
>
> > - r = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
> > + r = pcim_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
> > if (r < 0)
> > return r;
>
> It's good, but now why do we have pci_free_irq_vectors() in the same file?
>
Done. and thank you for your careful inspection.


>
> --
> With Best Regards,
> Andy Shevchenko
>
>