Skip to contentSkip to conversation

PCI: Use header type defines in pci_setup_device()

PATCH In review Ilpo Järvinen 3 participants 7 messages Jun 10, 2025 – Aug 6, 2025 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 PCI: Use header type defines in pci_setup_device()

Replace literals with PCI_HEADER_TYPE_* defines in pci_setup_device().

---
 drivers/pci/probe.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +2 -2
index 4b8693ec9e4c..c00634e5a2ed 100644
@@ -1985,8 +1985,8 @@ int pci_setup_device(struct pci_dev *dev)
 	dev->sysdata = dev->bus->sysdata;
 	dev->dev.parent = dev->bus->bridge;
 	dev->dev.bus = &pci_bus_type;
-	dev->hdr_type = hdr_type & 0x7f;
-	dev->multifunction = !!(hdr_type & 0x80);
+	dev->hdr_type = FIELD_GET(PCI_HEADER_TYPE_MASK, hdr_type);
+	dev->multifunction = FIELD_GET(PCI_HEADER_TYPE_MFD, hdr_type);
 	dev->error_state = pci_channel_io_normal;
 	set_pcie_port_type(dev);
 

base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494

Signature · 1 line
2.39.5
#1 lore raw

PATCH 2/3 PCI: Cleanup early_dump_pci_device()

Convert 256 to PCI_CFG_SPACE_SIZE and 4 to sizeof(u32) and avoid i / 4 construct by changing the iteration.

---
 drivers/pci/probe.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pci/probe.c +5 -4
index c00634e5a2ed..f08e754c404b 100644
@@ -3,6 +3,7 @@
  * PCI detection and setup code
  */
 
+#include <linux/array_size.h>
 #include <linux/kernel.h>
 #include <linux/delay.h>
 #include <linux/init.h>
@@ -1912,16 +1913,16 @@ static int pci_intx_mask_broken(struct pci_dev *dev)
 
 static void early_dump_pci_device(struct pci_dev *pdev)
 {
-	u32 value[256 / 4];
+	u32 value[PCI_CFG_SPACE_SIZE / sizeof(u32)];
 	int i;
 
 	pci_info(pdev, "config space:\n");
 
-	for (i = 0; i < 256; i += 4)
-		pci_read_config_dword(pdev, i, &value[i / 4]);
+	for (i = 0; i < ARRAY_SIZE(value); i++)
+		pci_read_config_dword(pdev, i * sizeof(u32), &value[i]);
 
 	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
-		       value, 256, false);
+		       value, ARRAY_SIZE(value) * sizeof(u32), false);
 }
 
 static const char *pci_type_str(struct pci_dev *dev)
Signature · 1 line
2.39.5
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] PCI: Cleanup early_dump_pci_device()

Quoted 5 lines · Ilpo Järvinen

Convert 256 to PCI_CFG_SPACE_SIZE and 4 to sizeof(u32) and avoid i / 4 construct by changing the iteration.

---

Quoted 36 lines
  drivers/pci/probe.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pci/probe.c +5 -4
index c00634e5a2ed..f08e754c404b 100644
@@ -3,6 +3,7 @@
   * PCI detection and setup code
   */
 
+#include <linux/array_size.h>
  #include <linux/kernel.h>
  #include <linux/delay.h>
  #include <linux/init.h>
@@ -1912,16 +1913,16 @@ static int pci_intx_mask_broken(struct pci_dev *dev)
 
  static void early_dump_pci_device(struct pci_dev *pdev)
  {
-	u32 value[256 / 4];
+	u32 value[PCI_CFG_SPACE_SIZE / sizeof(u32)];
  	int i;
 
  	pci_info(pdev, "config space:\n");
 
-	for (i = 0; i < 256; i += 4)
-		pci_read_config_dword(pdev, i, &value[i / 4]);
+	for (i = 0; i < ARRAY_SIZE(value); i++)
+		pci_read_config_dword(pdev, i * sizeof(u32), &value[i]);
 
  	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
-		       value, 256, false);
+		       value, ARRAY_SIZE(value) * sizeof(u32), false);
  }
 
  static const char *pci_type_str(struct pci_dev *dev)
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#3 lore raw

PATCH 3/3 PCI: Cleanup pci_scan_child_bus_extend() loop

In reply to Ilpo Järvinen

pci_scan_child_bus_extend() open-codes device number iteration in the for loop. Convert to use PCI_DEVFN() and add PCI_MAX_NR_DEVS (there seems to be no pre-existing defines for this purpose).

---
 drivers/pci/pci.h   | 1 +
 drivers/pci/probe.c | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
drivers/pci/pci.h +1 -0
index 12215ee72afb..caa6e02a9aea 100644
@@ -8,6 +8,7 @@ struct pcie_tlp_log;
 
 /* Number of possible devfns: 0.0 to 1f.7 inclusive */
 #define MAX_NR_DEVFNS 256
+#define PCI_MAX_NR_DEVS	32
 
 #define MAX_NR_LANES 16
 
drivers/pci/probe.c +3 -3
index f08e754c404b..963cab481327 100644
@@ -3029,14 +3029,14 @@ static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
 {
 	unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
 	unsigned int start = bus->busn_res.start;
-	unsigned int devfn, cmax, max = start;
+	unsigned int devnr, cmax, max = start;
 	struct pci_dev *dev;
 
 	dev_dbg(&bus->dev, "scanning bus\n");
 
 	/* Go find them, Rover! */
-	for (devfn = 0; devfn < 256; devfn += 8)
-		pci_scan_slot(bus, devfn);
+	for (devnr = 0; devnr < PCI_MAX_NR_DEVS; devnr++)
+		pci_scan_slot(bus, PCI_DEVFN(devnr, 0));
 
 	/* Reserve buses for SR-IOV capability */
 	used_buses = pci_iov_bus_range(bus);
Signature · 1 line
2.39.5
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] PCI: Cleanup pci_scan_child_bus_extend() loop

Quoted 6 lines · Ilpo Järvinen

pci_scan_child_bus_extend() open-codes device number iteration in the for loop. Convert to use PCI_DEVFN() and add PCI_MAX_NR_DEVS (there seems to be no pre-existing defines for this purpose).

---

Quoted 38 lines
  drivers/pci/pci.h   | 1 +
  drivers/pci/probe.c | 6 +++---
  2 files changed, 4 insertions(+), 3 deletions(-)
drivers/pci/pci.h +1 -0
index 12215ee72afb..caa6e02a9aea 100644
@@ -8,6 +8,7 @@ struct pcie_tlp_log;
 
  /* Number of possible devfns: 0.0 to 1f.7 inclusive */
  #define MAX_NR_DEVFNS 256
+#define PCI_MAX_NR_DEVS	32
 
  #define MAX_NR_LANES 16
 
drivers/pci/probe.c +3 -3
index f08e754c404b..963cab481327 100644
@@ -3029,14 +3029,14 @@ static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
  {
  	unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
  	unsigned int start = bus->busn_res.start;
-	unsigned int devfn, cmax, max = start;
+	unsigned int devnr, cmax, max = start;
  	struct pci_dev *dev;
 
  	dev_dbg(&bus->dev, "scanning bus\n");
 
  	/* Go find them, Rover! */
-	for (devfn = 0; devfn < 256; devfn += 8)
-		pci_scan_slot(bus, devfn);
+	for (devnr = 0; devnr < PCI_MAX_NR_DEVS; devnr++)
+		pci_scan_slot(bus, PCI_DEVFN(devnr, 0));
 
  	/* Reserve buses for SR-IOV capability */
  	used_buses = pci_iov_bus_range(bus);
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Reviewed-by on v1 1/3
#5 lore raw
In reply to Ilpo Järvinen
Quoted 4 lines · Ilpo Järvinen

Replace literals with PCI_HEADER_TYPE_* defines in pci_setup_device().

---

Quoted 20 lines
  drivers/pci/probe.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +2 -2
index 4b8693ec9e4c..c00634e5a2ed 100644
@@ -1985,8 +1985,8 @@ int pci_setup_device(struct pci_dev *dev)
  	dev->sysdata = dev->bus->sysdata;
  	dev->dev.parent = dev->bus->bridge;
  	dev->dev.bus = &pci_bus_type;
-	dev->hdr_type = hdr_type & 0x7f;
-	dev->multifunction = !!(hdr_type & 0x80);
+	dev->hdr_type = FIELD_GET(PCI_HEADER_TYPE_MASK, hdr_type);
+	dev->multifunction = FIELD_GET(PCI_HEADER_TYPE_MFD, hdr_type);
  	dev->error_state = pci_channel_io_normal;
  	set_pcie_port_type(dev);
 

base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#6 lore raw
In reply to Ilpo Järvinen
Quoted 3 lines · Ilpo Järvinen

Replace literals with PCI_HEADER_TYPE_* defines in pci_setup_device().

All three applied to pci/enumeration for v6.18, thanks!

Quoted 24 lines
---
 drivers/pci/probe.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +2 -2
index 4b8693ec9e4c..c00634e5a2ed 100644
@@ -1985,8 +1985,8 @@ int pci_setup_device(struct pci_dev *dev)
 	dev->sysdata = dev->bus->sysdata;
 	dev->dev.parent = dev->bus->bridge;
 	dev->dev.bus = &pci_bus_type;
-	dev->hdr_type = hdr_type & 0x7f;
-	dev->multifunction = !!(hdr_type & 0x80);
+	dev->hdr_type = FIELD_GET(PCI_HEADER_TYPE_MASK, hdr_type);
+	dev->multifunction = FIELD_GET(PCI_HEADER_TYPE_MFD, hdr_type);
 	dev->error_state = pci_channel_io_normal;
 	set_pcie_port_type(dev);
 

base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494

Signature · 1 line
2.39.5