git: 68f8fa4ada24 - stable/15 - LinuxKPI: pci: implement for_each_pci_dev() and improve pci_get_device()

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Mon, 15 Sep 2025 19:11:05 UTC
The branch stable/15 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=68f8fa4ada24aaf53a2d463b53259439dfb32146

commit 68f8fa4ada24aaf53a2d463b53259439dfb32146
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2025-09-04 20:20:15 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2025-09-15 14:50:49 +0000

    LinuxKPI: pci: implement for_each_pci_dev() and improve pci_get_device()
    
    Implement for_each_pci_dev() needed by a wireless driver update.
    For that also improve pci_get_device() and add the functionality to
    support the odev argument to start searching from that.
    
    Sponsored by:   The FreeBSD Foundation (intially)
    Reviewed by:    dumbbell
    Differential Revision:  https://reviews.freebsd.org/D52066
    
    (cherry picked from commit 910cf345d0ee9a5d72856a1ba35382eb4f0db951)
---
 sys/compat/linuxkpi/common/include/linux/pci.h |  3 +++
 sys/compat/linuxkpi/common/src/linux_pci.c     | 12 +++++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/pci.h b/sys/compat/linuxkpi/common/include/linux/pci.h
index 3fd4191b9917..df29af87f160 100644
--- a/sys/compat/linuxkpi/common/include/linux/pci.h
+++ b/sys/compat/linuxkpi/common/include/linux/pci.h
@@ -1445,6 +1445,9 @@ linuxkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev)
 	return (lkpi_pci_get_device(vendor, device, odev));
 }
 
+#define	for_each_pci_dev(_pdev)						\
+    while ((_pdev = linuxkpi_pci_get_device(PCI_ANY_ID, PCI_ANY_ID, _pdev)) != NULL)
+
 /* This is a FreeBSD extension so we can use bus_*(). */
 static inline void
 linuxkpi_pcim_want_to_use_bus_functions(struct pci_dev *pdev)
diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index d5bbbea1eb2c..00d4a25e86ed 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -289,12 +289,18 @@ lkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev)
 {
 	struct pci_dev *pdev, *found;
 
-	KASSERT(odev == NULL, ("%s: odev argument not yet supported\n", __func__));
-
 	found = NULL;
 	spin_lock(&pci_lock);
 	list_for_each_entry(pdev, &pci_devices, links) {
-		if (pdev->vendor == vendor && pdev->device == device) {
+		/* Walk until we find odev. */
+		if (odev != NULL) {
+			if (pdev == odev)
+				odev = NULL;
+			continue;
+		}
+
+		if ((pdev->vendor == vendor || vendor == PCI_ANY_ID) &&
+		    (pdev->device == device || device == PCI_ANY_ID)) {
 			found = pdev;
 			break;
 		}