diff --git a/include/spdk/env.h b/include/spdk/env.h index 656f5a12c1..6c0fec9e6c 100644 --- a/include/spdk/env.h +++ b/include/spdk/env.h @@ -173,6 +173,9 @@ uint16_t spdk_pci_device_get_domain(struct spdk_pci_device *dev); uint8_t spdk_pci_device_get_bus(struct spdk_pci_device *dev); uint8_t spdk_pci_device_get_dev(struct spdk_pci_device *dev); uint8_t spdk_pci_device_get_func(struct spdk_pci_device *dev); + +struct spdk_pci_addr spdk_pci_device_get_addr(struct spdk_pci_device *dev); + uint16_t spdk_pci_device_get_vendor_id(struct spdk_pci_device *dev); uint16_t spdk_pci_device_get_device_id(struct spdk_pci_device *dev); uint16_t spdk_pci_device_get_subvendor_id(struct spdk_pci_device *dev); @@ -189,7 +192,12 @@ int spdk_pci_device_cfg_write16(struct spdk_pci_device *dev, uint16_t value, uin int spdk_pci_device_cfg_read32(struct spdk_pci_device *dev, uint32_t *value, uint32_t offset); int spdk_pci_device_cfg_write32(struct spdk_pci_device *dev, uint32_t value, uint32_t offset); -bool spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr); +/** + * Compare two PCI addresses. + * + * \return 0 if a1 == a2, less than 0 if a1 < a2, greater than 0 if a1 > a2 + */ +int spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2); #ifdef __cplusplus } diff --git a/lib/bdev/nvme/blockdev_nvme.c b/lib/bdev/nvme/blockdev_nvme.c index 17ad87b870..44a92bf77f 100644 --- a/lib/bdev/nvme/blockdev_nvme.c +++ b/lib/bdev/nvme/blockdev_nvme.c @@ -414,10 +414,12 @@ blockdev_nvme_exist(struct nvme_probe_ctx *ctx) { int i; struct nvme_device *nvme_dev; + struct spdk_pci_addr dev_addr; for (i = 0; i < ctx->num_whitelist_controllers; i++) { TAILQ_FOREACH(nvme_dev, &g_nvme_devices, tailq) { - if (spdk_pci_device_compare_addr(nvme_dev->pci_dev, &ctx->whitelist[i])) { + dev_addr = spdk_pci_device_get_addr(nvme_dev->pci_dev); + if (spdk_pci_addr_compare(&dev_addr, &ctx->whitelist[i]) == 0) { return true; } } diff --git a/lib/env/pci.c b/lib/env/pci.c index 45505fcc3a..24ed242649 100644 --- a/lib/env/pci.c +++ b/lib/env/pci.c @@ -394,13 +394,41 @@ spdk_pci_device_get_serial_number(struct spdk_pci_device *dev, char *sn, size_t return -1; } -bool -spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr) +struct spdk_pci_addr +spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev) { - return ((spdk_pci_device_get_domain(dev) == addr->domain) && - (spdk_pci_device_get_bus(dev) == addr->bus) && - (spdk_pci_device_get_dev(dev) == addr->dev) && - (spdk_pci_device_get_func(dev) == addr->func)); + struct spdk_pci_addr pci_addr; + + pci_addr.domain = spdk_pci_device_get_domain(pci_dev); + pci_addr.bus = spdk_pci_device_get_bus(pci_dev); + pci_addr.dev = spdk_pci_device_get_dev(pci_dev); + pci_addr.func = spdk_pci_device_get_func(pci_dev); + + return pci_addr; +} + +int +spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2) +{ + if (a1->domain > a2->domain) { + return 1; + } else if (a1->domain < a2->domain) { + return -1; + } else if (a1->bus > a2->bus) { + return 1; + } else if (a1->bus < a2->bus) { + return -1; + } else if (a1->dev > a2->dev) { + return 1; + } else if (a1->dev < a2->dev) { + return -1; + } else if (a1->func > a2->func) { + return 1; + } else if (a1->func < a2->func) { + return -1; + } + + return 0; } #ifdef __linux__ diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index 77eb5be723..0c5e36d999 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -229,6 +229,9 @@ nvme_enum_cb(void *ctx, struct spdk_pci_device *pci_dev) struct nvme_enum_ctx *enum_ctx = ctx; struct spdk_nvme_ctrlr *ctrlr; struct spdk_nvme_ctrlr_opts opts; + struct spdk_pci_addr dev_addr; + + dev_addr = spdk_pci_device_get_addr(pci_dev); /* Verify that this controller is not already attached */ TAILQ_FOREACH(ctrlr, &g_spdk_nvme_driver->attached_ctrlrs, tailq) { @@ -236,7 +239,7 @@ nvme_enum_cb(void *ctx, struct spdk_pci_device *pci_dev) * different per each process, we compare by BDF to determine whether it is the * same controller. */ - if (spdk_pci_device_compare_addr(pci_dev, &ctrlr->pci_addr)) { + if (spdk_pci_addr_compare(&dev_addr, &ctrlr->pci_addr) == 0) { return 0; } } diff --git a/test/lib/nvme/unit/nvme_c/nvme_ut.c b/test/lib/nvme/unit/nvme_c/nvme_ut.c index 1adfa19dab..8c41346f11 100644 --- a/test/lib/nvme/unit/nvme_c/nvme_ut.c +++ b/test/lib/nvme/unit/nvme_c/nvme_ut.c @@ -73,8 +73,17 @@ spdk_nvme_ctrlr_opts_set_defaults(struct spdk_nvme_ctrlr_opts *opts) memset(opts, 0, sizeof(*opts)); } -bool -spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr) +struct spdk_pci_addr +spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev) +{ + struct spdk_pci_addr pci_addr; + + memset(&pci_addr, 0, sizeof(pci_addr)); + return pci_addr; +} + +int +spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2) { return true; } diff --git a/test/lib/nvme/unit/nvme_ns_cmd_c/nvme_ns_cmd_ut.c b/test/lib/nvme/unit/nvme_ns_cmd_c/nvme_ns_cmd_ut.c index 6f27134dd7..2a9b2c650f 100644 --- a/test/lib/nvme/unit/nvme_ns_cmd_c/nvme_ns_cmd_ut.c +++ b/test/lib/nvme/unit/nvme_ns_cmd_c/nvme_ns_cmd_ut.c @@ -77,8 +77,17 @@ nvme_ctrlr_start(struct spdk_nvme_ctrlr *ctrlr) return 0; } -bool -spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr) +struct spdk_pci_addr +spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev) +{ + struct spdk_pci_addr pci_addr; + + memset(&pci_addr, 0, sizeof(pci_addr)); + return pci_addr; +} + +int +spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2) { return true; }