env/pci: call driver callback in pci_hook_device

Now that we have a attach_device() callback, the devices can be hooked
during spdk_pci_device_attach().  With DPDK, driver->cb_fn() is called
in pci_device_init(), so we need to do the same in
spdk_pci_hook_device().

Signed-off-by: Konrad Sztyber <konrad.sztyber@intel.com>
Change-Id: Iada8b83ce7592aa62561530192072a50ec3a904b
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/13884
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Tom Nabarro <tom.nabarro@intel.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
This commit is contained in:
Konrad Sztyber 2022-08-04 09:29:01 +02:00 committed by Tomasz Zawadzki
parent ac8b65bdd2
commit 4b08c07a62
6 changed files with 31 additions and 7 deletions

View File

@ -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 drv driver that will be able to attach the device
* \param dev fully initialized PCI device struct * \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. * Un-hook a custom PCI device from the PCI layer. The device must not be attached.

View File

@ -6,8 +6,8 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
SO_VER := 9 SO_VER := 10
SO_MINOR := 1 SO_MINOR := 0
CFLAGS += $(ENV_CFLAGS) CFLAGS += $(ENV_CFLAGS)
C_SRCS = env.c memory.c pci.c init.c threads.c C_SRCS = env.c memory.c pci.c init.c threads.c

View File

@ -1147,15 +1147,29 @@ spdk_pci_addr_fmt(char *bdf, size_t sz, const struct spdk_pci_addr *addr)
return -1; return -1;
} }
void int
spdk_pci_hook_device(struct spdk_pci_driver *drv, struct spdk_pci_device *dev) spdk_pci_hook_device(struct spdk_pci_driver *drv, struct spdk_pci_device *dev)
{ {
int rc;
assert(dev->map_bar != NULL); assert(dev->map_bar != NULL);
assert(dev->unmap_bar != NULL); assert(dev->unmap_bar != NULL);
assert(dev->cfg_read != NULL); assert(dev->cfg_read != NULL);
assert(dev->cfg_write != NULL); assert(dev->cfg_write != NULL);
dev->internal.driver = drv; 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); TAILQ_INSERT_TAIL(&g_pci_devices, dev, internal.tailq);
return 0;
} }
void void

View File

@ -6,6 +6,7 @@
#include "vmd_internal.h" #include "vmd_internal.h"
#include "spdk/stdinc.h" #include "spdk/stdinc.h"
#include "spdk/string.h"
#include "spdk/likely.h" #include "spdk/likely.h"
static unsigned char *device_type[] = { 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_pci_bus *bus = dev->bus;
struct vmd_adapter *vmd; struct vmd_adapter *vmd;
uint8_t bdf[32]; uint8_t bdf[32];
int rc;
if (!vmd_assign_base_addrs(dev)) { if (!vmd_assign_base_addrs(dev)) {
SPDK_ERRLOG("Failed to allocate BARs for device: %p\n", 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_pci_addr_fmt(bdf, sizeof(bdf), &dev->pci.addr);
SPDK_INFOLOG(vmd, "Initializing NVMe device at %s\n", bdf); SPDK_INFOLOG(vmd, "Initializing NVMe device at %s\n", bdf);
dev->pci.parent = dev->bus->vmd->pci; 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 = bus->vmd;
vmd->target[vmd->nvme_count] = dev; vmd->target[vmd->nvme_count] = dev;

View File

@ -22,7 +22,7 @@ DEPDIRS-ioat := log
DEPDIRS-idxd := log util DEPDIRS-idxd := log util
DEPDIRS-sock := log $(JSON_LIBS) DEPDIRS-sock := log $(JSON_LIBS)
DEPDIRS-util := log DEPDIRS-util := log
DEPDIRS-vmd := log DEPDIRS-vmd := log util
DEPDIRS-dma := log DEPDIRS-dma := log
DEPDIRS-trace_parser := log DEPDIRS-trace_parser := log
ifeq ($(CONFIG_VFIO_USER),y) ifeq ($(CONFIG_VFIO_USER),y)

View File

@ -125,7 +125,8 @@ pci_hook_test(void)
ut_dev.pci.cfg_write = ut_cfg_write; ut_dev.pci.cfg_write = ut_cfg_write;
/* hook the device into the PCI layer */ /* 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 */ /* 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); rc = spdk_pci_device_attach(&ut_pci_driver, ut_enum_cb, NULL, &ut_dev.pci.addr);