git: 6dc813301a17 - main - sys: Use is_pci_device instead of direct comparisons to devclasses

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Mon, 18 May 2026 18:53:31 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=6dc813301a173e2e1993c3064df162e6218c1231

commit 6dc813301a173e2e1993c3064df162e6218c1231
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-05-18 18:52:23 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-05-18 18:52:23 +0000

    sys: Use is_pci_device instead of direct comparisons to devclasses
    
    Reviewed by:    bz
    Sponsored by:   Chelsio Communications
    Differential Revision:  https://reviews.freebsd.org/D56997
---
 sys/arm64/cavium/thunder_pcie_pem.c            |  6 +----
 sys/arm64/iommu/smmu.c                         |  6 ++---
 sys/compat/linuxkpi/common/include/linux/pci.h |  6 ++---
 sys/dev/ata/ata-all.c                          |  3 +--
 sys/dev/bge/if_bge.c                           | 13 +++--------
 sys/dev/ichwd/ichwd.c                          |  5 ++--
 sys/dev/iommu/busdma_iommu.c                   | 32 +++++++-------------------
 sys/dev/pci/pci.c                              | 10 ++------
 sys/dev/pci/vga_pci.c                          |  6 ++---
 sys/dev/vnic/thunder_bgx_fdt.c                 |  9 ++------
 sys/x86/iommu/amd_drv.c                        |  3 +--
 sys/x86/iommu/intel_drv.c                      | 13 +++--------
 12 files changed, 29 insertions(+), 83 deletions(-)

diff --git a/sys/arm64/cavium/thunder_pcie_pem.c b/sys/arm64/cavium/thunder_pcie_pem.c
index 04b892b91c56..092f11241e29 100644
--- a/sys/arm64/cavium/thunder_pcie_pem.c
+++ b/sys/arm64/cavium/thunder_pcie_pem.c
@@ -760,8 +760,6 @@ thunder_pem_attach(device_t dev)
 {
 	struct resource_map_request req;
 	struct resource_map map;
-	devclass_t pci_class;
-	device_t parent;
 	struct thunder_pem_softc *sc;
 	int error;
 	int rid;
@@ -773,9 +771,7 @@ thunder_pem_attach(device_t dev)
 	sc->dev = dev;
 
 	/* Allocate memory for resource */
-	pci_class = devclass_find("pci");
-	parent = device_get_parent(dev);
-	if (device_get_devclass(parent) == pci_class)
+	if (is_pci_device(dev))
 		rid = PCIR_BAR(0);
 	else
 		rid = RID_PEM_SPACE;
diff --git a/sys/arm64/iommu/smmu.c b/sys/arm64/iommu/smmu.c
index a84ef4ae347e..265f1e56f892 100644
--- a/sys/arm64/iommu/smmu.c
+++ b/sys/arm64/iommu/smmu.c
@@ -1825,7 +1825,6 @@ smmu_ctx_init(device_t dev, struct iommu_ctx *ioctx)
 	struct iommu_domain *iodom;
 	struct smmu_softc *sc;
 	struct smmu_ctx *ctx;
-	devclass_t pci_class;
 	u_int sid;
 	int err;
 
@@ -1836,8 +1835,7 @@ smmu_ctx_init(device_t dev, struct iommu_ctx *ioctx)
 	domain = ctx->domain;
 	iodom = (struct iommu_domain *)domain;
 
-	pci_class = devclass_find("pci");
-	if (device_get_devclass(device_get_parent(ctx->dev)) == pci_class) {
+	if (is_pci_device(ctx->dev)) {
 		err = smmu_pci_get_sid(ctx->dev, NULL, &sid);
 		if (err)
 			return (err);
@@ -1863,7 +1861,7 @@ smmu_ctx_init(device_t dev, struct iommu_ctx *ioctx)
 
 	smmu_init_ste(sc, domain->cd, ctx->sid, ctx->bypass);
 
-	if (device_get_devclass(device_get_parent(ctx->dev)) == pci_class)
+	if (is_pci_device((ctx->dev))
 		if (iommu_is_buswide_ctx(iodom->iommu, pci_get_bus(ctx->dev)))
 			smmu_set_buswide(dev, domain, ctx);
 
diff --git a/sys/compat/linuxkpi/common/include/linux/pci.h b/sys/compat/linuxkpi/common/include/linux/pci.h
index a6ddc02d1947..56b9c6e05f27 100644
--- a/sys/compat/linuxkpi/common/include/linux/pci.h
+++ b/sys/compat/linuxkpi/common/include/linux/pci.h
@@ -399,8 +399,7 @@ int _lkpi_pci_enable_msi_range(struct pci_dev *pdev, int minvec, int maxvec);
 static inline bool
 dev_is_pci(struct device *dev)
 {
-	return (device_get_devclass(device_get_parent(dev->bsddev)) ==
-	    devclass_find("pci"));
+	return (is_pci_device(dev->bsddev));
 }
 
 static inline uint16_t
@@ -556,8 +555,7 @@ pci_upstream_bridge(struct pci_dev *pdev)
 		bridge = device_get_parent(bridge);
 		if (bridge == NULL)
 			goto done;
-		if (device_get_devclass(device_get_parent(bridge)) !=
-		    devclass_find("pci"))
+		if (!is_pci_device(bridge))
 			goto done;
 
 		/*
diff --git a/sys/dev/ata/ata-all.c b/sys/dev/ata/ata-all.c
index 2e77c0f6478e..85fe40aa4584 100644
--- a/sys/dev/ata/ata-all.c
+++ b/sys/dev/ata/ata-all.c
@@ -1176,8 +1176,7 @@ ataaction(struct cam_sim *sim, union ccb *ccb)
 		cpi->protocol = PROTO_ATA;
 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
 		cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
-		if (device_get_devclass(device_get_parent(parent)) ==
-		    devclass_find("pci")) {
+		if (is_pci_device(parent)) {
 			cpi->hba_vendor = pci_get_vendor(parent);
 			cpi->hba_device = pci_get_device(parent);
 			cpi->hba_subvendor = pci_get_subvendor(parent);
diff --git a/sys/dev/bge/if_bge.c b/sys/dev/bge/if_bge.c
index 2feb19bff677..3551f10b2ae3 100644
--- a/sys/dev/bge/if_bge.c
+++ b/sys/dev/bge/if_bge.c
@@ -3202,20 +3202,13 @@ bge_mbox_reorder(struct bge_softc *sc)
 	} mbox_reorder_lists[] = {
 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
 	};
-	devclass_t pci, pcib;
-	device_t bus, dev;
+	device_t dev;
 	int i;
 
-	pci = devclass_find("pci");
-	pcib = devclass_find("pcib");
 	dev = sc->bge_dev;
-	bus = device_get_parent(dev);
 	for (;;) {
-		dev = device_get_parent(bus);
-		bus = device_get_parent(dev);
-		if (device_get_devclass(dev) != pcib)
-			break;
-		if (device_get_devclass(bus) != pci)
+		dev = device_get_parent(device_get_parent(dev));
+		if (!is_pci_device(dev))
 			break;
 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
 			if (pci_get_vendor(dev) ==
diff --git a/sys/dev/ichwd/ichwd.c b/sys/dev/ichwd/ichwd.c
index 5481553cc175..b0c953e58956 100644
--- a/sys/dev/ichwd/ichwd.c
+++ b/sys/dev/ichwd/ichwd.c
@@ -561,13 +561,12 @@ static device_t
 ichwd_find_ich_lpc_bridge(device_t isa, struct ichwd_device **id_p)
 {
 	struct ichwd_device *id;
-	device_t isab, pci;
+	device_t isab;
 	uint16_t devid;
 
 	/* Check whether parent ISA bridge looks familiar. */
 	isab = device_get_parent(isa);
-	pci = device_get_parent(isab);
-	if (pci == NULL || device_get_devclass(pci) != devclass_find("pci"))
+	if (!is_pci_device(isab))
 		return (NULL);
 	if (pci_get_vendor(isab) != VENDORID_INTEL)
 		return (NULL);
diff --git a/sys/dev/iommu/busdma_iommu.c b/sys/dev/iommu/busdma_iommu.c
index 82f73d469585..3852429c4a8a 100644
--- a/sys/dev/iommu/busdma_iommu.c
+++ b/sys/dev/iommu/busdma_iommu.c
@@ -117,17 +117,14 @@ iommu_bus_dma_is_dev_disabled(int domain, int bus, int slot, int func)
 int
 iommu_get_requester(device_t dev, device_t *requesterp, uint16_t *rid)
 {
-	devclass_t pci_class;
 	device_t l, pci, pcib, pcip, pcibp, requester;
 	int cap_offset;
 	uint16_t pcie_flags;
 	bool bridge_is_pcie;
 
-	pci_class = devclass_find("pci");
 	l = requester = dev;
 
-	pci = device_get_parent(dev);
-	if (pci == NULL || device_get_devclass(pci) != pci_class) {
+	if (!is_pci_device(dev)) {
 		*rid = 0;	/* XXXKIB: Could be ACPI HID */
 		*requesterp = NULL;
 		return (ENOTTY);
@@ -141,29 +138,18 @@ iommu_get_requester(device_t dev, device_t *requesterp, uint16_t *rid)
 	 * unit.
 	 */
 	for (;;) {
-		pci = device_get_parent(l);
-		if (pci == NULL) {
+		if (!is_pci_device(l)) {
 			if (bootverbose) {
 				printf(
-			"iommu_get_requester(%s): NULL parent for %s\n",
+			"iommu_get_requester(%s): non-pci ancestor %s\n",
 				    device_get_name(dev), device_get_name(l));
 			}
 			*rid = 0;
 			*requesterp = NULL;
 			return (ENXIO);
 		}
-		if (device_get_devclass(pci) != pci_class) {
-			if (bootverbose) {
-				printf(
-			"iommu_get_requester(%s): non-pci parent %s for %s\n",
-				    device_get_name(dev), device_get_name(pci),
-				    device_get_name(l));
-			}
-			*rid = 0;
-			*requesterp = NULL;
-			return (ENXIO);
-		}
 
+		pci = device_get_parent(l);
 		pcib = device_get_parent(pci);
 		if (pcib == NULL) {
 			if (bootverbose) {
@@ -182,10 +168,8 @@ iommu_get_requester(device_t dev, device_t *requesterp, uint16_t *rid)
 		 * port, and the requester ID won't be translated
 		 * further.
 		 */
-		pcip = device_get_parent(pcib);
-		if (device_get_devclass(pcip) != pci_class)
+		if (!is_pci_device(pcib))
 			break;
-		pcibp = device_get_parent(pcip);
 
 		if (pci_find_cap(l, PCIY_EXPRESS, &cap_offset) == 0) {
 			/*
@@ -212,6 +196,8 @@ iommu_get_requester(device_t dev, device_t *requesterp, uint16_t *rid)
 			 * PCI bridge, then we know pcib is actually a
 			 * PCIe/PCI bridge.
 			 */
+			pcip = device_get_parent(pcib);
+			pcibp = device_get_parent(pcip);
 			if (!bridge_is_pcie && pci_find_cap(pcibp,
 			    PCIY_EXPRESS, &cap_offset) == 0) {
 				pcie_flags = pci_read_config(pcibp,
@@ -337,11 +323,9 @@ bool
 bus_dma_iommu_set_buswide(device_t dev)
 {
 	struct iommu_unit *unit;
-	device_t parent;
 	u_int busno, slot, func;
 
-	parent = device_get_parent(dev);
-	if (device_get_devclass(parent) != devclass_find("pci"))
+	if (!is_pci_device(dev))
 		return (false);
 	unit = iommu_find(dev, bootverbose);
 	if (unit == NULL)
diff --git a/sys/dev/pci/pci.c b/sys/dev/pci/pci.c
index adf3daea66fd..034f34e2dde2 100644
--- a/sys/dev/pci/pci.c
+++ b/sys/dev/pci/pci.c
@@ -6535,11 +6535,9 @@ device_t
 pci_find_pcie_root_port(device_t dev)
 {
 	struct pci_devinfo *dinfo;
-	devclass_t pci_class;
 	device_t pcib, bus;
 
-	pci_class = devclass_find("pci");
-	KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
+	KASSERT(is_pci_device(dev),
 	    ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
 
 	/*
@@ -6555,11 +6553,7 @@ pci_find_pcie_root_port(device_t dev)
 		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
 		    device_get_nameunit(bus)));
 
-		/*
-		 * pcib's parent must be a PCI bus for this to be a
-		 * PCI-PCI bridge.
-		 */
-		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
+		if (!is_pci_device(pcib))
 			return (NULL);
 
 		dinfo = device_get_ivars(pcib);
diff --git a/sys/dev/pci/vga_pci.c b/sys/dev/pci/vga_pci.c
index 39b2fcab67b4..72c93e201d96 100644
--- a/sys/dev/pci/vga_pci.c
+++ b/sys/dev/pci/vga_pci.c
@@ -111,8 +111,7 @@ vga_pci_is_boot_display(device_t dev)
 	 */
 
 	pcib = device_get_parent(device_get_parent(dev));
-	if (device_get_devclass(device_get_parent(pcib)) ==
-	    devclass_find("pci")) {
+	if (is_pci_device(pcib)) {
 		/*
 		 * The parent bridge is a PCI-to-PCI bridge: check the
 		 * value of the "VGA Enable" bit.
@@ -186,8 +185,7 @@ vga_pci_map_bios(device_t dev, size_t *size)
 #endif
 
 	pcib = device_get_parent(device_get_parent(dev));
-	if (device_get_devclass(device_get_parent(pcib)) ==
-	    devclass_find("pci")) {
+	if (is_pci_device(pcib)) {
 		/*
 		 * The parent bridge is a PCI-to-PCI bridge: check the
 		 * value of the "VGA Enable" bit.
diff --git a/sys/dev/vnic/thunder_bgx_fdt.c b/sys/dev/vnic/thunder_bgx_fdt.c
index 20a5b54f7448..d8c5459f2914 100644
--- a/sys/dev/vnic/thunder_bgx_fdt.c
+++ b/sys/dev/vnic/thunder_bgx_fdt.c
@@ -285,11 +285,9 @@ bgx_fdt_traverse_nodes(uint8_t unit, phandle_t start, char *name,
 static device_t
 bgx_find_root_pcib(device_t dev)
 {
-	devclass_t pci_class;
 	device_t pcib, bus;
 
-	pci_class = devclass_find("pci");
-	KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
+	KASSERT(is_pci_device(dev),
 	    ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
 
 	/* Walk the bridge hierarchy until we find a non-PCI device */
@@ -298,9 +296,6 @@ bgx_find_root_pcib(device_t dev)
 		KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
 		    device_get_nameunit(dev)));
 
-		if (device_get_devclass(bus) != pci_class)
-			return (NULL);
-
 		pcib = device_get_parent(bus);
 		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
 		    device_get_nameunit(bus)));
@@ -309,7 +304,7 @@ bgx_find_root_pcib(device_t dev)
 		 * If the parent of this PCIB is not PCI
 		 * then we found our root PCIB.
 		 */
-		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
+		if (!is_pci_device(pcib))
 			return (pcib);
 
 		dev = pcib;
diff --git a/sys/x86/iommu/amd_drv.c b/sys/x86/iommu/amd_drv.c
index fc9770e1a32b..d45c576856eb 100644
--- a/sys/x86/iommu/amd_drv.c
+++ b/sys/x86/iommu/amd_drv.c
@@ -894,8 +894,7 @@ amdiommu_find_unit(device_t dev, struct amdiommu_unit **unitp, uint16_t *ridp,
 	if (!amdiommu_enable)
 		return (ENXIO);
 
-	if (device_get_devclass(device_get_parent(dev)) !=
-	    devclass_find("pci"))
+	if (!is_pci_device(dev))
 		return (ENXIO);
 
 	bzero(&ifu, sizeof(ifu));
diff --git a/sys/x86/iommu/intel_drv.c b/sys/x86/iommu/intel_drv.c
index 28db14bba95b..185ed9c1a628 100644
--- a/sys/x86/iommu/intel_drv.c
+++ b/sys/x86/iommu/intel_drv.c
@@ -605,16 +605,13 @@ dmar_print_path(int busno, int depth, const ACPI_DMAR_PCI_PATH *path)
 int
 dmar_dev_depth(device_t child)
 {
-	devclass_t pci_class;
 	device_t bus, pcib;
 	int depth;
 
-	pci_class = devclass_find("pci");
 	for (depth = 1; ; depth++) {
 		bus = device_get_parent(child);
 		pcib = device_get_parent(bus);
-		if (device_get_devclass(device_get_parent(pcib)) !=
-		    pci_class)
+		if (!is_pci_device(pcib))
 			return (depth);
 		child = pcib;
 	}
@@ -623,19 +620,16 @@ dmar_dev_depth(device_t child)
 void
 dmar_dev_path(device_t child, int *busno, void *path1, int depth)
 {
-	devclass_t pci_class;
 	device_t bus, pcib;
 	ACPI_DMAR_PCI_PATH *path;
 
-	pci_class = devclass_find("pci");
 	path = path1;
 	for (depth--; depth != -1; depth--) {
 		path[depth].Device = pci_get_slot(child);
 		path[depth].Function = pci_get_function(child);
 		bus = device_get_parent(child);
 		pcib = device_get_parent(bus);
-		if (device_get_devclass(device_get_parent(pcib)) !=
-		    pci_class) {
+		if (!is_pci_device(pcib)) {
 			/* reached a host bridge */
 			*busno = pcib_get_bus(bus);
 			return;
@@ -765,8 +759,7 @@ dmar_find(device_t dev, bool verbose)
 	/*
 	 * This function can only handle PCI(e) devices.
 	 */
-	if (device_get_devclass(device_get_parent(dev)) !=
-	    devclass_find("pci"))
+	if (!is_pci_device(dev))
 		return (NULL);
 
 	dev_domain = pci_get_domain(dev);