Skip to contentSkip to conversation

Refactor pci_is_brdige() to simplify code

PATCH Applied Yijing Wang 2 participants 15 messages May 4, 2014 – May 27, 2014

Subsystems (8)
x86 PowerPC ACPI PCI +4 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover Refactor pci_is_brdige() to simplify code

v1->v2: Add comments for new pci_is_bridge().

This patchset rename the current pci_is_bridge() to pci_has_subordinate(), and introduce a new pci_is_bridge() which determine pci bridge by check dev->hdr_type. The new one is more accurate. PCI Spec define the pci device is a bridge by the dev->hdr_type = 0x01 || 0x02.

There is no guarantee that a PCI bridge must attached a subordinate bus. When PCI bridge is created but before the scan child bus, it has no subordinate bus. Also users can remove the pci bus using interface pci_remove_bus() in remove.c.

So use new pci_is_bridge() check if the PCI device is bridge is better choice. If user want check PCI bridge whether has a subordinate bus, pci_has_subordinate() is a candidate.

Yijing Wang (13):
PCI: rename pci_is_bridge() to pci_has_subordinate()
PCI: Introduce new pci_is_bridge() helper function
PCI: Use new pci_is_bridge() to simplify code x86/PCI: Use new pci_is_bridge() to simplify code IA64/PCI: Use new pci_is_bridge() to simplify code powerpc/PCI: Use new pci_is_bridge() to simplify code sparc/PCI: Use new pci_is_bridge() to simplify code PCI, rpaphp: Use new pci_is_bridge() to simplify code PCI, shpchp: Use new pci_is_bridge() to simplify code PCI, cpcihp: Use new pci_is_bridge() to simplify code PCI, acpiphp: Use new pci_is_bridge() to simplify code PCI, pcmcia: Use new pci_is_bridge() to simplify code PCI, pciehp: Use new pci_is_bridge() to simplify code

 arch/ia64/pci/fixup.c                  |    4 +---
 arch/powerpc/kernel/pci-hotplug.c      |    3 +--
 arch/powerpc/kernel/pci_of_scan.c      |    3 +--
 arch/sparc/kernel/pci.c                |    3 +--
 arch/x86/pci/fixup.c                   |    4 +---
 drivers/pci/hotplug/acpiphp_glue.c     |    3 +--
 drivers/pci/hotplug/cpci_hotplug_pci.c |    3 +--
 drivers/pci/hotplug/pciehp_pci.c       |    3 +--
 drivers/pci/hotplug/rpadlpar_core.c    |    3 +--
 drivers/pci/hotplug/shpchp_pci.c       |    3 +--
 drivers/pci/pci-acpi.c                 |    8 +-------
 drivers/pci/pci-driver.c               |    8 ++++----
 drivers/pci/pci.h                      |    2 +-
 drivers/pci/probe.c                    |    3 +--
 drivers/pci/setup-bus.c                |    4 +---
 drivers/pcmcia/cardbus.c               |    3 +--
 include/linux/pci.h                    |   13 +++++++++++++
 17 files changed, 32 insertions(+), 41 deletions(-)
#1 lore raw

PATCH v2 1/13 PCI: rename pci_is_bridge() to pci_has_subordinate()

pci_is_bridge() which seems to determine whether device is a bridge, returns true only when the subordinate bus exists. This confuses people. PCI device is a bridge means its header type(bit 0 through 6) is 0x1(PCI bridge) or 0x2(CardBus bridge). Rename the current pci_is_bridge() helper function to pci_has_subordinate().

---
 drivers/pci/pci-driver.c |    8 ++++----
 drivers/pci/pci.h        |    2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/pci/pci-driver.c +4 -4
index d911e0c..b7850cb 100644
@@ -580,14 +580,14 @@ static void pci_pm_default_resume(struct pci_dev *pci_dev)
 {
 	pci_fixup_device(pci_fixup_resume, pci_dev);
 
-	if (!pci_is_bridge(pci_dev))
+	if (!pci_has_subordinate(pci_dev))
 		pci_enable_wake(pci_dev, PCI_D0, false);
 }
 
 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
 {
 	/* Disable non-bridge devices without PM support */
-	if (!pci_is_bridge(pci_dev))
+	if (!pci_has_subordinate(pci_dev))
 		pci_disable_enabled_device(pci_dev);
 }
 
@@ -717,7 +717,7 @@ static int pci_pm_suspend_noirq(struct device *dev)
 
 	if (!pci_dev->state_saved) {
 		pci_save_state(pci_dev);
-		if (!pci_is_bridge(pci_dev))
+		if (!pci_has_subordinate(pci_dev))
 			pci_prepare_to_sleep(pci_dev);
 	}
 
@@ -971,7 +971,7 @@ static int pci_pm_poweroff_noirq(struct device *dev)
 			return error;
 	}
 
-	if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
+	if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev))
 		pci_prepare_to_sleep(pci_dev);
 
 	/*
drivers/pci/pci.h +1 -1
index 6bd0822..65108fc 100644
@@ -77,7 +77,7 @@ static inline void pci_wakeup_event(struct pci_dev *dev)
 	pm_wakeup_event(&dev->dev, 100);
 }
 
-static inline bool pci_is_bridge(struct pci_dev *pci_dev)
+static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
 {
 	return !!(pci_dev->subordinate);
 }
Signature · 1 line
1.7.1
#2 lore raw

PATCH v2 2/13 PCI: Introduce new pci_is_bridge() helper function

In reply to Yijing Wang

PCIe Spec define the PCI bridge is the PCI device which header type(bit 0 through 6) is 0x1(PCI bridge) or 0x2(CardBus bridge).

---
 include/linux/pci.h |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
include/linux/pci.h +13 -0
index aab57b4..9f5f89e 100644
@@ -477,6 +477,19 @@ static inline bool pci_is_root_bus(struct pci_bus *pbus)
 	return !(pbus->parent);
 }
 
+/**
+ * pci_is_bridge - check if the PCI device is a bridge
+ * @dev: PCI device
+ *
+ * Return true if the PCI device is bridge whether it has subordinate
+ * or not.
+ */
+static inline bool pci_is_bridge(struct pci_dev *dev)
+{
+	return dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
+		dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
+}
+
 static inline struct pci_dev *pci_upstream_bridge(struct pci_dev *dev)
 {
 	dev = pci_physfn(dev);
Signature · 1 line
1.7.1
#3 lore raw

PATCH v2 3/13 PCI: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pci/pci-acpi.c  |    8 +-------
 drivers/pci/probe.c     |    3 +--
 drivers/pci/setup-bus.c |    4 +---
 3 files changed, 3 insertions(+), 12 deletions(-)
drivers/pci/pci-acpi.c +1 -7
index f49abef..ca4927b 100644
@@ -309,13 +309,7 @@ static struct acpi_device *acpi_pci_find_companion(struct device *dev)
 	bool check_children;
 	u64 addr;
 
-	/*
-	 * pci_is_bridge() is not suitable here, because pci_dev->subordinate
-	 * is set only after acpi_pci_find_device() has been called for the
-	 * given device.
-	 */
-	check_children = pci_dev->hdr_type = PCI_HEADER_TYPE_BRIDGE
-			|| pci_dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
+	check_children = pci_is_bridge(pci_dev);
 	/* Please ref to ACPI spec for the syntax of _ADR */
 	addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
 	return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
drivers/pci/probe.c +1 -2
index ef09f5f..f831dd8 100644
@@ -1670,8 +1670,7 @@ unsigned int pci_scan_child_bus(struct pci_bus *bus)
 
 	for (pass=0; pass < 2; pass++)
 		list_for_each_entry(dev, &bus->devices, bus_list) {
-			if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-			    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS)
+			if (pci_is_bridge(dev))
 				max = pci_scan_bridge(bus, dev, max, pass);
 		}
 
drivers/pci/setup-bus.c +1 -3
index 138bdd6..e399d00 100644
@@ -1629,9 +1629,7 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
 
 	down_read(&pci_bus_sem);
 	list_for_each_entry(dev, &bus->devices, bus_list)
-		if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-		    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS)
-			if (dev->subordinate)
+		if (pci_is_bridge(dev) && pci_has_subordinate(dev))
 				__pci_bus_size_bridges(dev->subordinate,
 							 &add_list);
 	up_read(&pci_bus_sem);
Signature · 1 line
1.7.1
#4 lore raw

PATCH v2 4/13 x86/PCI: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 arch/x86/pci/fixup.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
arch/x86/pci/fixup.c +1 -3
index 94ae9ae..e5f000c 100644
@@ -337,9 +337,7 @@ static void pci_fixup_video(struct pci_dev *pdev)
 		 * type BRIDGE, or CARDBUS. Host to PCI controllers use
 		 * PCI header type NORMAL.
 		 */
-		if (bridge
-		    && ((bridge->hdr_type = PCI_HEADER_TYPE_BRIDGE)
-		       || (bridge->hdr_type = PCI_HEADER_TYPE_CARDBUS))) {
+		if (bridge && (pci_is_bridge(bridge))) {
 			pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
 						&config);
 			if (!(config & PCI_BRIDGE_CTL_VGA))
Signature · 1 line
1.7.1
#5 lore raw

PATCH v2 5/13 IA64/PCI: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 arch/ia64/pci/fixup.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
arch/ia64/pci/fixup.c +1 -3
index eee069a..1fe9aa5 100644
@@ -49,9 +49,7 @@ static void pci_fixup_video(struct pci_dev *pdev)
 		 * type BRIDGE, or CARDBUS. Host to PCI controllers use
 		 * PCI header type NORMAL.
 		 */
-		if (bridge
-		    &&((bridge->hdr_type = PCI_HEADER_TYPE_BRIDGE)
-		       ||(bridge->hdr_type = PCI_HEADER_TYPE_CARDBUS))) {
+		if (bridge && (pci_is_bridge(bridge))) {
 			pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
 						&config);
 			if (!(config & PCI_BRIDGE_CTL_VGA))
Signature · 1 line
1.7.1
#6 lore raw

PATCH v2 6/13 powerpc/PCI: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 arch/powerpc/kernel/pci-hotplug.c |    3 +--
 arch/powerpc/kernel/pci_of_scan.c |    3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/kernel/pci-hotplug.c +1 -2
index c1e17ae..5b78917 100644
@@ -98,8 +98,7 @@ void pcibios_add_pci_devices(struct pci_bus * bus)
 		max = bus->busn_res.start;
 		for (pass = 0; pass < 2; pass++) {
 			list_for_each_entry(dev, &bus->devices, bus_list) {
-				if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-				    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS)
+				if (pci_is_bridge(dev))
 					max = pci_scan_bridge(bus, dev,
 							      max, pass);
 			}
arch/powerpc/kernel/pci_of_scan.c +1 -2
index 83c26d8..059e244 100644
@@ -362,8 +362,7 @@ static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
 
 	/* Now scan child busses */
 	list_for_each_entry(dev, &bus->devices, bus_list) {
-		if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-		    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS) {
+		if (pci_is_bridge(dev)) {
 			of_scan_pci_bridge(dev);
 		}
 	}
Signature · 1 line
1.7.1
#7 lore raw

PATCH v2 7/13 sparc/PCI: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 arch/sparc/kernel/pci.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
arch/sparc/kernel/pci.c +1 -2
index 1555bbc..857ad77 100644
@@ -543,8 +543,7 @@ static void pci_of_scan_bus(struct pci_pbm_info *pbm,
 			printk("PCI: dev header type: %x\n",
 			       dev->hdr_type);
 
-		if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-		    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS)
+		if (pci_is_bridge(dev))
 			of_scan_pci_bridge(pbm, child, dev);
 	}
 }
Signature · 1 line
1.7.1
#8 lore raw

PATCH v2 8/13 PCI, rpaphp: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pci/hotplug/rpadlpar_core.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/pci/hotplug/rpadlpar_core.c +1 -2
index 4fcdeed..7660232 100644
@@ -157,8 +157,7 @@ static void dlpar_pci_add_bus(struct device_node *dn)
 	}
 
 	/* Scan below the new bridge */
-	if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-	    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS)
+	if (pci_is_bridge(dev))
 		of_scan_pci_bridge(dev);
 
 	/* Map IO space for child bus, which may or may not succeed */
Signature · 1 line
1.7.1
#9 lore raw

PATCH v2 9/13 PCI, shpchp: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pci/hotplug/shpchp_pci.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/pci/hotplug/shpchp_pci.c +1 -2
index 2bf69fe..ea8ad31 100644
@@ -64,8 +64,7 @@ int __ref shpchp_configure_device(struct slot *p_slot)
 	list_for_each_entry(dev, &parent->devices, bus_list) {
 		if (PCI_SLOT(dev->devfn) != p_slot->device)
 			continue;
-		if ((dev->hdr_type = PCI_HEADER_TYPE_BRIDGE) ||
-		    (dev->hdr_type = PCI_HEADER_TYPE_CARDBUS))
+		if (pci_is_bridge(dev))
 			pci_hp_add_bridge(dev);
 	}
 
Signature · 1 line
1.7.1
#10 lore raw

PATCH v2 10/13 PCI, cpcihp: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pci/hotplug/cpci_hotplug_pci.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/pci/hotplug/cpci_hotplug_pci.c +1 -2
index 8c14648..9843371 100644
@@ -289,8 +289,7 @@ int __ref cpci_configure_slot(struct slot *slot)
 	list_for_each_entry(dev, &parent->devices, bus_list)
 		if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn))
 			continue;
-		if ((dev->hdr_type = PCI_HEADER_TYPE_BRIDGE) ||
-		    (dev->hdr_type = PCI_HEADER_TYPE_CARDBUS))
+		if (pci_is_bridge(dev))
 			pci_hp_add_bridge(dev);
 
 
Signature · 1 line
1.7.1
#11 lore raw

PATCH v2 11/13 PCI, acpiphp: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pci/hotplug/acpiphp_glue.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +1 -2
index bccc27e..f1f9bd1 100644
@@ -516,8 +516,7 @@ static void __ref enable_slot(struct acpiphp_slot *slot)
 			if (PCI_SLOT(dev->devfn) != slot->device)
 				continue;
 
-			if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-			    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS) {
+			if (pci_is_bridge(dev)) {
 				max = pci_scan_bridge(bus, dev, max, pass);
 				if (pass && dev->subordinate) {
 					check_hotplug_bridge(slot, dev);
Signature · 1 line
1.7.1
#12 lore raw

PATCH v2 12/13 PCI, pcmcia: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pcmcia/cardbus.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/pcmcia/cardbus.c +1 -2
index 8bde619..4fe4cc4 100644
@@ -78,8 +78,7 @@ int __ref cb_alloc(struct pcmcia_socket *s)
 	max = bus->busn_res.start;
 	for (pass = 0; pass < 2; pass++)
 		list_for_each_entry(dev, &bus->devices, bus_list)
-			if (dev->hdr_type = PCI_HEADER_TYPE_BRIDGE ||
-			    dev->hdr_type = PCI_HEADER_TYPE_CARDBUS)
+			if (pci_is_bridge(dev))
 				max = pci_scan_bridge(bus, dev, max, pass);
 
 	/*
Signature · 1 line
1.7.1
#13 lore raw

PATCH v2 13/13 PCI, pciehp: Use new pci_is_bridge() to simplify code

In reply to Yijing Wang

Now we can use new pci_is_bridge() helper function to simplify code.

---
 drivers/pci/hotplug/pciehp_pci.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/pci/hotplug/pciehp_pci.c +1 -2
index 1b53306..b6cb1df 100644
@@ -62,8 +62,7 @@ int pciehp_configure_device(struct slot *p_slot)
 	}
 
 	list_for_each_entry(dev, &parent->devices, bus_list)
-		if ((dev->hdr_type = PCI_HEADER_TYPE_BRIDGE) ||
-				(dev->hdr_type = PCI_HEADER_TYPE_CARDBUS))
+		if (pci_is_bridge(dev))
 			pci_hp_add_bridge(dev);
 
 	pci_assign_unassigned_bridge_resources(bridge);
Signature · 1 line
1.7.1
maintainer Applied
#14 lore raw
In reply to Yijing Wang
Quoted 42 lines · Yijing Wang

v1->v2: Add comments for new pci_is_bridge().

This patchset rename the current pci_is_bridge() to pci_has_subordinate(), and introduce a new pci_is_bridge() which determine pci bridge by check dev->hdr_type. The new one is more accurate. PCI Spec define the pci device is a bridge by the dev->hdr_type = 0x01 || 0x02.

There is no guarantee that a PCI bridge must attached a subordinate bus. When PCI bridge is created but before the scan child bus, it has no subordinate bus. Also users can remove the pci bus using interface pci_remove_bus() in remove.c.

So use new pci_is_bridge() check if the PCI device is bridge is better choice. If user want check PCI bridge whether has a subordinate bus, pci_has_subordinate() is a candidate.

Yijing Wang (13):
PCI: rename pci_is_bridge() to pci_has_subordinate()
PCI: Introduce new pci_is_bridge() helper function
PCI: Use new pci_is_bridge() to simplify code x86/PCI: Use new pci_is_bridge() to simplify code IA64/PCI: Use new pci_is_bridge() to simplify code powerpc/PCI: Use new pci_is_bridge() to simplify code sparc/PCI: Use new pci_is_bridge() to simplify code PCI, rpaphp: Use new pci_is_bridge() to simplify code PCI, shpchp: Use new pci_is_bridge() to simplify code PCI, cpcihp: Use new pci_is_bridge() to simplify code PCI, acpiphp: Use new pci_is_bridge() to simplify code PCI, pcmcia: Use new pci_is_bridge() to simplify code PCI, pciehp: Use new pci_is_bridge() to simplify code

 arch/ia64/pci/fixup.c                  |    4 +---
 arch/powerpc/kernel/pci-hotplug.c      |    3 +--
 arch/powerpc/kernel/pci_of_scan.c      |    3 +--
 arch/sparc/kernel/pci.c                |    3 +--
 arch/x86/pci/fixup.c                   |    4 +---
 drivers/pci/hotplug/acpiphp_glue.c     |    3 +--
 drivers/pci/hotplug/cpci_hotplug_pci.c |    3 +--
 drivers/pci/hotplug/pciehp_pci.c       |    3 +--
 drivers/pci/hotplug/rpadlpar_core.c    |    3 +--
 drivers/pci/hotplug/shpchp_pci.c       |    3 +--
Quoted 8 lines
 drivers/pci/pci-acpi.c                 |    8 +-------
 drivers/pci/pci-driver.c               |    8 ++++----
 drivers/pci/pci.h                      |    2 +-
 drivers/pci/probe.c                    |    3 +--
 drivers/pci/setup-bus.c                |    4 +---
 drivers/pcmcia/cardbus.c               |    3 +--
 include/linux/pci.h                    |   13 +++++++++++++
 17 files changed, 32 insertions(+), 41 deletions(-)

Applied to pci/pci_is_bridge for v3.16, thanks,

Bjorn