Skip to content

Commit 4d909bf

Browse files
Hans Zhangbjorn-helgaas
authored andcommitted
PCI: Refactor extended capability search into PCI_FIND_NEXT_EXT_CAP()
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
1 parent 3c02084 commit 4d909bf

2 files changed

Lines changed: 42 additions & 33 deletions

File tree

drivers/pci/pci.c

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -527,42 +527,11 @@ EXPORT_SYMBOL(pci_bus_find_capability);
527527
*/
528528
u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 start, int cap)
529529
{
530-
u32 header;
531-
int ttl;
532-
u16 pos = PCI_CFG_SPACE_SIZE;
533-
534-
/* minimum 8 bytes per capability */
535-
ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
536-
537530
if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
538531
return 0;
539532

540-
if (start)
541-
pos = start;
542-
543-
if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
544-
return 0;
545-
546-
/*
547-
* If we have no capabilities, this is indicated by cap ID,
548-
* cap version and next pointer all being 0.
549-
*/
550-
if (header == 0)
551-
return 0;
552-
553-
while (ttl-- > 0) {
554-
if (PCI_EXT_CAP_ID(header) == cap && pos != start)
555-
return pos;
556-
557-
pos = PCI_EXT_CAP_NEXT(header);
558-
if (pos < PCI_CFG_SPACE_SIZE)
559-
break;
560-
561-
if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
562-
break;
563-
}
564-
565-
return 0;
533+
return PCI_FIND_NEXT_EXT_CAP(pci_bus_read_config, start, cap,
534+
dev->bus, dev->devfn);
566535
}
567536
EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
568537

drivers/pci/pci.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,46 @@ bool pcie_cap_has_rtctl(const struct pci_dev *dev);
133133
__found_pos; \
134134
})
135135

136+
/* Extended Capability finder */
137+
/**
138+
* PCI_FIND_NEXT_EXT_CAP - Find a PCI extended capability
139+
* @read_cfg: Function pointer for reading PCI config space
140+
* @start: Starting position to begin search (0 for initial search)
141+
* @cap: Extended capability ID to find
142+
* @args: Arguments to pass to read_cfg function
143+
*
144+
* Search the extended capability list in PCI config space to find @cap.
145+
* Implements TTL protection against infinite loops using a calculated
146+
* maximum search count.
147+
*
148+
* Return: Position of the capability if found, 0 otherwise.
149+
*/
150+
#define PCI_FIND_NEXT_EXT_CAP(read_cfg, start, cap, args...) \
151+
({ \
152+
u16 __pos = (start) ?: PCI_CFG_SPACE_SIZE; \
153+
u16 __found_pos = 0; \
154+
int __ttl, __ret; \
155+
u32 __header; \
156+
\
157+
__ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; \
158+
while (__ttl-- > 0 && __pos >= PCI_CFG_SPACE_SIZE) { \
159+
__ret = read_cfg##_dword(args, __pos, &__header); \
160+
if (__ret != PCIBIOS_SUCCESSFUL) \
161+
break; \
162+
\
163+
if (__header == 0) \
164+
break; \
165+
\
166+
if (PCI_EXT_CAP_ID(__header) == (cap) && __pos != start) {\
167+
__found_pos = __pos; \
168+
break; \
169+
} \
170+
\
171+
__pos = PCI_EXT_CAP_NEXT(__header); \
172+
} \
173+
__found_pos; \
174+
})
175+
136176
/* Functions internal to the PCI core code */
137177

138178
#ifdef CONFIG_DMI

0 commit comments

Comments
 (0)