diff mbox series

[1/2] PCI: Add a pci_dev_depth() helper function

Message ID 20211129121934.4963-1-hdegoede@redhat.com (mailing list archive)
State Superseded
Delegated to: Bjorn Helgaas
Headers show
Series [1/2] PCI: Add a pci_dev_depth() helper function | expand

Commit Message

Hans de Goede Nov. 29, 2021, 12:19 p.m. UTC
Add a pci_dev_depth() helper function, which returns the depth
of a device in the PCI hierarchy.

This is useful to have for lockdep annotations for dealing with
nested locked when traversing the hierarchy.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 include/linux/pci.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
diff mbox series

Patch

diff --git a/include/linux/pci.h b/include/linux/pci.h
index 7d825637d7ca..6ad78cc67aa8 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -691,6 +691,26 @@  static inline bool pci_is_bridge(struct pci_dev *dev)
 		dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
 }
 
+/**
+ * pci_dev_depth - Return the depth of a device in the PCI hierarchy
+ * @dev: PCI device
+ *
+ * Return the depth (number of parent busses above) the device in
+ * the PCI hierarchy.
+ */
+static inline int pci_dev_depth(struct pci_dev *dev)
+{
+	struct pci_bus *bus = dev->bus;
+	int depth = 0;
+
+	while (bus->parent) {
+		depth++;
+		bus = bus->parent;
+	}
+
+	return depth;
+}
+
 #define for_each_pci_bridge(dev, bus)				\
 	list_for_each_entry(dev, &bus->devices, bus_list)	\
 		if (!pci_is_bridge(dev)) {} else