Skip to contentSkip to conversation

PCI/VPD: Add helper pci_get_func0_dev

PATCH In review Heiner Kallweit 2 participants 2 messages Apr 16, 2021 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/VPD: Add helper pci_get_func0_dev

The combined use of the PCI_DEVFN() and PCI_SLOT() macros in several places is unnecessarily complex. Use a simplified version and add a helper for it.

---
 drivers/pci/vpd.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/pci/vpd.c +9 -5
index 42f762ab0..60573f27a 100644
@@ -28,6 +28,12 @@ struct pci_vpd {
 	unsigned int	valid:1;
 };
 
+static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
+{
+	/* bits 2:0 in devfn is the device function */
+	return pci_get_slot(dev->bus, dev->devfn & 0xf8);
+}
+
 /**
  * pci_read_vpd - Read one entry from Vital Product Data
  * @dev:	pci device struct
@@ -305,8 +311,7 @@ static const struct pci_vpd_ops pci_vpd_ops = {
 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
 			       void *arg)
 {
-	struct pci_dev *tdev = pci_get_slot(dev->bus,
-					    PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+	struct pci_dev *tdev = pci_get_func0_dev(dev);
 	ssize_t ret;
 
 	if (!tdev)
@@ -320,8 +325,7 @@ static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
 				const void *arg)
 {
-	struct pci_dev *tdev = pci_get_slot(dev->bus,
-					    PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+	struct pci_dev *tdev = pci_get_func0_dev(dev);
 	ssize_t ret;
 
 	if (!tdev)
@@ -414,7 +418,7 @@ static void quirk_f0_vpd_link(struct pci_dev *dev)
 	if (!PCI_FUNC(dev->devfn))
 		return;
 
-	f0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+	f0 = pci_get_func0_dev(dev);
 	if (!f0)
 		return;
 
Signature · 1 line
2.31.1
#1 lore raw
Quoted 5 lines · Heiner Kallweit

The combined use of the PCI_DEVFN() and PCI_SLOT() macros in several places is unnecessarily complex. Use a simplified version and add a helper for it.

Applied with pci_get_func0_dev() as follows to pci/vpd for v5.13. It's true that "devfn & 0xf8" is simpler, but it also exposes implementation details that need not be exposed here.

diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c index b23bbab6802d..03a02af5a6a7 100644 --- a/drivers/pci/vpd.c
+++ b/drivers/pci/vpd.c

  @@ -28,6 +28,11 @@ struct pci_vpd {
	  unsigned int	valid:1;

};

  +static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
  +{
  +	return pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  +}
  +
Quoted 53 lines
---
 drivers/pci/vpd.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/pci/vpd.c +9 -5
index 42f762ab0..60573f27a 100644
@@ -28,6 +28,12 @@ struct pci_vpd {
 	unsigned int	valid:1;
 };
 
+static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
+{
+	/* bits 2:0 in devfn is the device function */
+	return pci_get_slot(dev->bus, dev->devfn & 0xf8);
+}
+
 /**
  * pci_read_vpd - Read one entry from Vital Product Data
  * @dev:	pci device struct
@@ -305,8 +311,7 @@ static const struct pci_vpd_ops pci_vpd_ops = {
 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
 			       void *arg)
 {
-	struct pci_dev *tdev = pci_get_slot(dev->bus,
-					    PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+	struct pci_dev *tdev = pci_get_func0_dev(dev);
 	ssize_t ret;
 
 	if (!tdev)
@@ -320,8 +325,7 @@ static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
 				const void *arg)
 {
-	struct pci_dev *tdev = pci_get_slot(dev->bus,
-					    PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+	struct pci_dev *tdev = pci_get_func0_dev(dev);
 	ssize_t ret;
 
 	if (!tdev)
@@ -414,7 +418,7 @@ static void quirk_f0_vpd_link(struct pci_dev *dev)
 	if (!PCI_FUNC(dev->devfn))
 		return;
 
-	f0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+	f0 = pci_get_func0_dev(dev);
 	if (!f0)
 		return;
 
Signature · 1 line
2.31.1