Currently, setup gets device info as part of setup, while the separate get device info API also calls setup if the fd is zero. Untangle these two APIs and make each do one thing, and adjust all existing callers.
Signed-off-by: Anatoly Burakov <[email protected]> --- drivers/bus/cdx/cdx_vfio.c | 12 ++++++++-- drivers/bus/pci/linux/pci_vfio.c | 18 ++++++++++---- drivers/bus/platform/platform.c | 9 ++++++- drivers/crypto/bcmfs/bcmfs_vfio.c | 8 ++++++- lib/eal/include/dev_vfio.h | 23 +++++++----------- lib/eal/linux/eal_vfio.c | 40 +++++++++---------------------- 6 files changed, 58 insertions(+), 52 deletions(-) diff --git a/drivers/bus/cdx/cdx_vfio.c b/drivers/bus/cdx/cdx_vfio.c index 02a34d6f186..531e4ccc8d0 100644 --- a/drivers/bus/cdx/cdx_vfio.c +++ b/drivers/bus/cdx/cdx_vfio.c @@ -401,10 +401,14 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev) return -1; ret = dev_vfio_setup_device(RTE_CDX_BUS_DEVICES_PATH, dev_name, - &vfio_dev_fd, &device_info); + &vfio_dev_fd); if (ret) return ret; + ret = dev_vfio_get_device_info(vfio_dev_fd, &device_info); + if (ret) + goto err_vfio_dev_fd; + /* allocate vfio_res and get region info */ vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0); if (vfio_res == NULL) { @@ -510,10 +514,14 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev) } ret = dev_vfio_setup_device(RTE_CDX_BUS_DEVICES_PATH, dev_name, - &vfio_dev_fd, &device_info); + &vfio_dev_fd); if (ret) return ret; + ret = dev_vfio_get_device_info(vfio_dev_fd, &device_info); + if (ret) + goto err_vfio_dev_fd; + /* map MMIO regions */ maps = vfio_res->maps; diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c index 3b967d62c89..a4bb288916b 100644 --- a/drivers/bus/pci/linux/pci_vfio.c +++ b/drivers/bus/pci/linux/pci_vfio.c @@ -753,10 +753,14 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev) loc->domain, loc->bus, loc->devid, loc->function); ret = dev_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, - &vfio_dev_fd, &device_info); + &vfio_dev_fd); if (ret) return ret; + ret = dev_vfio_get_device_info(vfio_dev_fd, &device_info); + if (ret) + goto err_vfio_dev_fd; + if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd)) goto err_vfio_dev_fd; @@ -962,10 +966,14 @@ pci_vfio_map_resource_secondary(struct rte_pci_device *dev) } ret = dev_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, - &vfio_dev_fd, &device_info); + &vfio_dev_fd); if (ret) return ret; + ret = dev_vfio_get_device_info(vfio_dev_fd, &device_info); + if (ret) + goto err_vfio_dev_fd; + ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info); if (ret) goto err_vfio_dev_fd; @@ -1195,12 +1203,14 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, if (vfio_dev_fd < 0) { return -1; } else if (vfio_dev_fd == 0) { - if (dev_vfio_get_device_info(rte_pci_get_sysfs_path(), pci_addr, - &vfio_dev_fd, &device_info) != 0) + if (dev_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, + &vfio_dev_fd) != 0) return -1; /* save vfio_dev_fd so it can be used during release */ if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd) != 0) return -1; + if (dev_vfio_get_device_info(vfio_dev_fd, &device_info) != 0) + return -1; if (pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info) != 0) return -1; diff --git a/drivers/bus/platform/platform.c b/drivers/bus/platform/platform.c index 9d7df9be803..950306e2d90 100644 --- a/drivers/bus/platform/platform.c +++ b/drivers/bus/platform/platform.c @@ -292,12 +292,19 @@ device_setup(struct rte_platform_device *pdev) const char *name = pdev->name; int ret; - ret = dev_vfio_setup_device(PLATFORM_BUS_DEVICES_PATH, name, &pdev->dev_fd, &dev_info); + ret = dev_vfio_setup_device(PLATFORM_BUS_DEVICES_PATH, name, &pdev->dev_fd); if (ret) { PLATFORM_LOG_LINE(ERR, "failed to setup %s", name); return -ENODEV; } + ret = dev_vfio_get_device_info(pdev->dev_fd, &dev_info); + if (ret) { + PLATFORM_LOG_LINE(ERR, "failed to get device info for %s", name); + ret = -ENODEV; + goto out; + } + /* This is an extra check to confirm that platform device was initialized * by a kernel vfio-platform driver. On kernels that predate vfio-platform * driver this flag obviously does not exist. In such scenarios this diff --git a/drivers/crypto/bcmfs/bcmfs_vfio.c b/drivers/crypto/bcmfs/bcmfs_vfio.c index 053af0d227b..ad244d97569 100644 --- a/drivers/crypto/bcmfs/bcmfs_vfio.c +++ b/drivers/crypto/bcmfs/bcmfs_vfio.c @@ -25,12 +25,18 @@ vfio_map_dev_obj(const char *path, const char *dev_obj, struct vfio_device_info d_info = { .argsz = sizeof(d_info) }; struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) }; - ret = dev_vfio_setup_device(path, dev_obj, dev_fd, &d_info); + ret = dev_vfio_setup_device(path, dev_obj, dev_fd); if (ret) { BCMFS_LOG(ERR, "VFIO Setting for device failed"); return ret; } + ret = dev_vfio_get_device_info(*dev_fd, &d_info); + if (ret) { + BCMFS_LOG(ERR, "VFIO Getting device info failed"); + goto map_failed; + } + /* getting device region info*/ ret = ioctl(*dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info); if (ret < 0) { diff --git a/lib/eal/include/dev_vfio.h b/lib/eal/include/dev_vfio.h index 871bebb43b1..b7f91ca96b8 100644 --- a/lib/eal/include/dev_vfio.h +++ b/lib/eal/include/dev_vfio.h @@ -66,10 +66,7 @@ enum dev_vfio_module { * device location. * * @param vfio_dev_fd - * VFIO fd. - * - * @param device_info - * Device information. + * Pointer to VFIO fd, will be set to the opened device fd on success. * * @return * 0 on success. @@ -78,7 +75,7 @@ enum dev_vfio_module { */ __rte_internal int dev_vfio_setup_device(const char *sysfs_base, const char *dev_addr, - int *vfio_dev_fd, struct vfio_device_info *device_info); + int *vfio_dev_fd); /** * @internal @@ -217,19 +214,16 @@ dev_vfio_get_group_num(const char *sysfs_base, * @internal * Get device information. * + * This function retrieves VFIO device information from an already opened + * device. The device must be opened with `dev_vfio_setup_device()` first. + * * This function is only relevant to Linux and will return an error on BSD. * - * @param sysfs_base - * sysfs path prefix. - * - * @param dev_addr - * device location. - * * @param vfio_dev_fd - * VFIO fd. + * VFIO device fd (must be a valid, already opened fd). * * @param device_info - * Device information. + * Pointer to device information structure to be filled. * * @return * 0 on success. @@ -237,8 +231,7 @@ dev_vfio_get_group_num(const char *sysfs_base, */ __rte_internal int -dev_vfio_get_device_info(const char *sysfs_base, const char *dev_addr, - int *vfio_dev_fd, struct vfio_device_info *device_info); +dev_vfio_get_device_info(int vfio_dev_fd, struct vfio_device_info *device_info); /** * @internal diff --git a/lib/eal/linux/eal_vfio.c b/lib/eal/linux/eal_vfio.c index 6ea040dd2e3..0acc423ce28 100644 --- a/lib/eal/linux/eal_vfio.c +++ b/lib/eal/linux/eal_vfio.c @@ -816,7 +816,7 @@ dev_vfio_clear_group(int vfio_group_fd) RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_setup_device) int dev_vfio_setup_device(const char *sysfs_base, const char *dev_addr, - int *vfio_dev_fd, struct vfio_device_info *device_info) + int *vfio_dev_fd) { struct vfio_group_status group_status = { .argsz = sizeof(group_status) @@ -1038,7 +1038,7 @@ dev_vfio_setup_device(const char *sysfs_base, const char *dev_addr, *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev); if (*vfio_dev_fd >= 0) - goto dev_get_info; + goto out; } /* get a file descriptor for the device */ @@ -1055,18 +1055,8 @@ dev_vfio_setup_device(const char *sysfs_base, const char *dev_addr, return -1; } - /* test and setup the device */ -dev_get_info: - ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info); - if (ret) { - EAL_LOG(ERR, "%s cannot get device info, " - "error %i (%s)", dev_addr, errno, - strerror(errno)); - close(*vfio_dev_fd); - close(vfio_group_fd); - dev_vfio_clear_group(vfio_group_fd); - return -1; - } + /* device is now set up */ +out: vfio_group_device_get(vfio_group_fd); return 0; @@ -1297,8 +1287,7 @@ vfio_set_iommu_type(int vfio_container_fd) RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_device_info) int -dev_vfio_get_device_info(const char *sysfs_base, const char *dev_addr, - int *vfio_dev_fd, struct vfio_device_info *device_info) +dev_vfio_get_device_info(int vfio_dev_fd, struct vfio_device_info *device_info) { int ret; @@ -1307,21 +1296,14 @@ dev_vfio_get_device_info(const char *sysfs_base, const char *dev_addr, return -1; } - if (device_info == NULL || *vfio_dev_fd < 0) + if (device_info == NULL || vfio_dev_fd < 0) return -1; - if (*vfio_dev_fd == 0) { - ret = dev_vfio_setup_device(sysfs_base, dev_addr, - vfio_dev_fd, device_info); - if (ret) - return -1; - } else { - ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info); - if (ret) { - EAL_LOG(ERR, "%s cannot get device info, error %i (%s)", - dev_addr, errno, strerror(errno)); - return -1; - } + ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info); + if (ret) { + EAL_LOG(ERR, "Cannot get device info, error %i (%s)", + errno, strerror(errno)); + return -1; } return 0; -- 2.52.0

