]> www.infradead.org Git - users/hch/misc.git/commitdiff
PCI: Refactor extended capability search into PCI_FIND_NEXT_EXT_CAP()
authorHans Zhang <18255117159@163.com>
Wed, 13 Aug 2025 14:45:26 +0000 (22:45 +0800)
committerBjorn Helgaas <bhelgaas@google.com>
Thu, 14 Aug 2025 20:03:45 +0000 (15:03 -0500)
Move the extended Capability search logic into a PCI_FIND_NEXT_EXT_CAP()
macro that accepts a config space accessor function as an argument. This
enables controller drivers to perform Capability discovery using their
early access mechanisms prior to full device initialization while sharing
the Capability search code.

Convert the existing PCI core extended Capability search implementation to
use PCI_FIND_NEXT_EXT_CAP().

Signed-off-by: Hans Zhang <18255117159@163.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Tested-by: Niklas Schnelle <schnelle@linux.ibm.com>
Link: https://patch.msgid.link/20250813144529.303548-4-18255117159@163.com
drivers/pci/pci.c
drivers/pci/pci.h

index ac2658d946ea8bff6560b9e09f580450c14fab05..e698278229f2599039e8111ce1256952ce6bc2f6 100644 (file)
@@ -527,42 +527,11 @@ EXPORT_SYMBOL(pci_bus_find_capability);
  */
 u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 start, int cap)
 {
-       u32 header;
-       int ttl;
-       u16 pos = PCI_CFG_SPACE_SIZE;
-
-       /* minimum 8 bytes per capability */
-       ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
-
        if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
                return 0;
 
-       if (start)
-               pos = start;
-
-       if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
-               return 0;
-
-       /*
-        * If we have no capabilities, this is indicated by cap ID,
-        * cap version and next pointer all being 0.
-        */
-       if (header == 0)
-               return 0;
-
-       while (ttl-- > 0) {
-               if (PCI_EXT_CAP_ID(header) == cap && pos != start)
-                       return pos;
-
-               pos = PCI_EXT_CAP_NEXT(header);
-               if (pos < PCI_CFG_SPACE_SIZE)
-                       break;
-
-               if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
-                       break;
-       }
-
-       return 0;
+       return PCI_FIND_NEXT_EXT_CAP(pci_bus_read_config, start, cap,
+                                    dev->bus, dev->devfn);
 }
 EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
 
index 81580987509f2a2f432b3319af6537e7d93378d8..7fb44faf2c44a6a0b729076f219f04ecdcc4935f 100644 (file)
@@ -133,6 +133,46 @@ bool pcie_cap_has_rtctl(const struct pci_dev *dev);
        __found_pos;                                                    \
 })
 
+/* Extended Capability finder */
+/**
+ * PCI_FIND_NEXT_EXT_CAP - Find a PCI extended capability
+ * @read_cfg: Function pointer for reading PCI config space
+ * @start: Starting position to begin search (0 for initial search)
+ * @cap: Extended capability ID to find
+ * @args: Arguments to pass to read_cfg function
+ *
+ * Search the extended capability list in PCI config space to find @cap.
+ * Implements TTL protection against infinite loops using a calculated
+ * maximum search count.
+ *
+ * Return: Position of the capability if found, 0 otherwise.
+ */
+#define PCI_FIND_NEXT_EXT_CAP(read_cfg, start, cap, args...)           \
+({                                                                     \
+       u16 __pos = (start) ?: PCI_CFG_SPACE_SIZE;                      \
+       u16 __found_pos = 0;                                            \
+       int __ttl, __ret;                                               \
+       u32 __header;                                                   \
+                                                                       \
+       __ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;      \
+       while (__ttl-- > 0 && __pos >= PCI_CFG_SPACE_SIZE) {            \
+               __ret = read_cfg##_dword(args, __pos, &__header);       \
+               if (__ret != PCIBIOS_SUCCESSFUL)                        \
+                       break;                                          \
+                                                                       \
+               if (__header == 0)                                      \
+                       break;                                          \
+                                                                       \
+               if (PCI_EXT_CAP_ID(__header) == (cap) && __pos != start) {\
+                       __found_pos = __pos;                            \
+                       break;                                          \
+               }                                                       \
+                                                                       \
+               __pos = PCI_EXT_CAP_NEXT(__header);                     \
+       }                                                               \
+       __found_pos;                                                    \
+})
+
 /* Functions internal to the PCI core code */
 
 #ifdef CONFIG_DMI