diff --git a/include/spdk/env.h b/include/spdk/env.h index 2e17d26c2..38e69eb45 100644 --- a/include/spdk/env.h +++ b/include/spdk/env.h @@ -1149,8 +1149,10 @@ int spdk_pci_addr_fmt(char *bdf, size_t sz, const struct spdk_pci_addr *addr); * * \param drv driver that will be able to attach the device * \param dev fully initialized PCI device struct + * + * \return 0 on success, negative errno otherwise. */ -void spdk_pci_hook_device(struct spdk_pci_driver *drv, struct spdk_pci_device *dev); +int spdk_pci_hook_device(struct spdk_pci_driver *drv, struct spdk_pci_device *dev); /** * Un-hook a custom PCI device from the PCI layer. The device must not be attached. diff --git a/lib/env_dpdk/Makefile b/lib/env_dpdk/Makefile index 97e5bca13..70b8f6b2b 100644 --- a/lib/env_dpdk/Makefile +++ b/lib/env_dpdk/Makefile @@ -6,8 +6,8 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -SO_VER := 9 -SO_MINOR := 1 +SO_VER := 10 +SO_MINOR := 0 CFLAGS += $(ENV_CFLAGS) C_SRCS = env.c memory.c pci.c init.c threads.c diff --git a/lib/env_dpdk/pci.c b/lib/env_dpdk/pci.c index 16fa2de94..a3fbb6567 100644 --- a/lib/env_dpdk/pci.c +++ b/lib/env_dpdk/pci.c @@ -1147,15 +1147,29 @@ spdk_pci_addr_fmt(char *bdf, size_t sz, const struct spdk_pci_addr *addr) return -1; } -void +int spdk_pci_hook_device(struct spdk_pci_driver *drv, struct spdk_pci_device *dev) { + int rc; + assert(dev->map_bar != NULL); assert(dev->unmap_bar != NULL); assert(dev->cfg_read != NULL); assert(dev->cfg_write != NULL); dev->internal.driver = drv; + + if (drv->cb_fn != NULL) { + rc = drv->cb_fn(drv->cb_arg, dev); + if (rc != 0) { + return -ECANCELED; + } + + dev->internal.attached = true; + } + TAILQ_INSERT_TAIL(&g_pci_devices, dev, internal.tailq); + + return 0; } void diff --git a/lib/vmd/vmd.c b/lib/vmd/vmd.c index bae171c4b..3c8b49021 100644 --- a/lib/vmd/vmd.c +++ b/lib/vmd/vmd.c @@ -6,6 +6,7 @@ #include "vmd_internal.h" #include "spdk/stdinc.h" +#include "spdk/string.h" #include "spdk/likely.h" static unsigned char *device_type[] = { @@ -896,6 +897,7 @@ vmd_init_end_device(struct vmd_pci_device *dev) struct vmd_pci_bus *bus = dev->bus; struct vmd_adapter *vmd; uint8_t bdf[32]; + int rc; if (!vmd_assign_base_addrs(dev)) { SPDK_ERRLOG("Failed to allocate BARs for device: %p\n", dev); @@ -909,7 +911,12 @@ vmd_init_end_device(struct vmd_pci_device *dev) spdk_pci_addr_fmt(bdf, sizeof(bdf), &dev->pci.addr); SPDK_INFOLOG(vmd, "Initializing NVMe device at %s\n", bdf); dev->pci.parent = dev->bus->vmd->pci; - spdk_pci_hook_device(spdk_pci_nvme_get_driver(), &dev->pci); + + rc = spdk_pci_hook_device(spdk_pci_nvme_get_driver(), &dev->pci); + if (rc != 0) { + SPDK_ERRLOG("Failed to hook device %s: %s\n", bdf, spdk_strerror(-rc)); + return -1; + } vmd = bus->vmd; vmd->target[vmd->nvme_count] = dev; diff --git a/mk/spdk.lib_deps.mk b/mk/spdk.lib_deps.mk index 705318e83..4be41fe9c 100644 --- a/mk/spdk.lib_deps.mk +++ b/mk/spdk.lib_deps.mk @@ -22,7 +22,7 @@ DEPDIRS-ioat := log DEPDIRS-idxd := log util DEPDIRS-sock := log $(JSON_LIBS) DEPDIRS-util := log -DEPDIRS-vmd := log +DEPDIRS-vmd := log util DEPDIRS-dma := log DEPDIRS-trace_parser := log ifeq ($(CONFIG_VFIO_USER),y) diff --git a/test/env/pci/pci_ut.c b/test/env/pci/pci_ut.c index 8ebb317f0..eb4e8c309 100644 --- a/test/env/pci/pci_ut.c +++ b/test/env/pci/pci_ut.c @@ -125,7 +125,8 @@ pci_hook_test(void) ut_dev.pci.cfg_write = ut_cfg_write; /* hook the device into the PCI layer */ - spdk_pci_hook_device(&ut_pci_driver, &ut_dev.pci); + rc = spdk_pci_hook_device(&ut_pci_driver, &ut_dev.pci); + CU_ASSERT_EQUAL(rc, 0); /* try to attach a device with the matching driver and bdf */ rc = spdk_pci_device_attach(&ut_pci_driver, ut_enum_cb, NULL, &ut_dev.pci.addr);