diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index 963f0f129..7f159290c 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -228,31 +228,16 @@ nvme_mutex_init_shared(pthread_mutex_t *mtx) return rc; } -static int -nvme_enum_cb(enum spdk_nvme_transport_type type, struct nvme_enum_usr_ctx *enum_ctx, - struct spdk_nvme_probe_info *probe_info, void *devhandle) +int +nvme_probe_one(enum spdk_nvme_transport_type type, spdk_nvme_probe_cb probe_cb, void *cb_ctx, + struct spdk_nvme_probe_info *probe_info, void *devhandle) { struct spdk_nvme_ctrlr *ctrlr; struct spdk_nvme_ctrlr_opts opts; - /* Verify that this controller is not already attached */ - TAILQ_FOREACH(ctrlr, &g_spdk_nvme_driver->attached_ctrlrs, tailq) { - /* NOTE: In the case like multi-process environment where the device handle is - * different per each process, we compare by BDF to determine whether it is the - * same controller. - */ - if (type == SPDK_NVME_TRANSPORT_PCIE) { - if (spdk_pci_addr_compare(&probe_info->pci_addr, &ctrlr->probe_info.pci_addr) == 0) { - return 0; - } - } - - /* Todo: need to differentiate the NVMe over fabrics to avoid duplicated connection */ - } - spdk_nvme_ctrlr_opts_set_defaults(&opts); - if (enum_ctx->probe_cb(enum_ctx->cb_ctx, probe_info, &opts)) { + if (probe_cb(cb_ctx, probe_info, &opts)) { ctrlr = nvme_attach(type, devhandle); if (ctrlr == NULL) { SPDK_ERRLOG("nvme_attach() failed\n"); @@ -274,7 +259,6 @@ _spdk_nvme_probe(const struct spdk_nvme_discover_info *info, void *cb_ctx, spdk_nvme_remove_cb remove_cb) { int rc, start_rc; - struct nvme_enum_ctx enum_ctx; struct spdk_nvme_ctrlr *ctrlr, *ctrlr_tmp; enum spdk_nvme_transport_type type; @@ -296,16 +280,13 @@ _spdk_nvme_probe(const struct spdk_nvme_discover_info *info, void *cb_ctx, } } - enum_ctx.usr_ctx.probe_cb = probe_cb; - enum_ctx.usr_ctx.cb_ctx = cb_ctx; - enum_ctx.enum_cb = nvme_enum_cb; if (!info) { type = SPDK_NVME_TRANSPORT_PCIE; } else { type = info->type; } - rc = nvme_transport_ctrlr_scan(type, &enum_ctx, (void *)info); + rc = nvme_transport_ctrlr_scan(type, probe_cb, cb_ctx, (void *)info); /* * Keep going even if one or more nvme_attach() calls failed, diff --git a/lib/nvme/nvme_internal.h b/lib/nvme/nvme_internal.h index b3f83fcd2..4da63f015 100644 --- a/lib/nvme/nvme_internal.h +++ b/lib/nvme/nvme_internal.h @@ -480,6 +480,9 @@ void nvme_completion_poll_cb(void *arg, const struct spdk_nvme_cpl *cpl); int nvme_ctrlr_add_process(struct spdk_nvme_ctrlr *ctrlr, void *devhandle); void nvme_ctrlr_free_processes(struct spdk_nvme_ctrlr *ctrlr); +int nvme_probe_one(enum spdk_nvme_transport_type type, spdk_nvme_probe_cb probe_cb, void *cb_ctx, + struct spdk_nvme_probe_info *probe_info, void *devhandle); + int nvme_ctrlr_construct(struct spdk_nvme_ctrlr *ctrlr); void nvme_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr); int nvme_ctrlr_process_init(struct spdk_nvme_ctrlr *ctrlr); @@ -524,25 +527,11 @@ void nvme_qpair_print_command(struct spdk_nvme_qpair *qpair, struct spdk_nvme_cm void nvme_qpair_print_completion(struct spdk_nvme_qpair *qpair, struct spdk_nvme_cpl *cpl); struct spdk_nvme_ctrlr *nvme_attach(enum spdk_nvme_transport_type transport, void *devhandle); -struct nvme_enum_usr_ctx { - spdk_nvme_probe_cb probe_cb; - void *cb_ctx; -}; - -typedef int (*nvme_ctrlr_enum_cb)(enum spdk_nvme_transport_type type, - struct nvme_enum_usr_ctx *enum_usr_ctx, - struct spdk_nvme_probe_info *probe_info, void *devhandle); - -struct nvme_enum_ctx { - struct nvme_enum_usr_ctx usr_ctx; - nvme_ctrlr_enum_cb enum_cb; -}; - /* Transport specific functions */ #define DECLARE_TRANSPORT(name) \ struct spdk_nvme_ctrlr *nvme_ ## name ## _ctrlr_construct(enum spdk_nvme_transport_type transport, void *devhandle); \ int nvme_ ## name ## _ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr); \ - int nvme_ ## name ## _ctrlr_scan(enum spdk_nvme_transport_type transport, struct nvme_enum_ctx *enum_ctx, void *devhandle); \ + int nvme_ ## name ## _ctrlr_scan(enum spdk_nvme_transport_type transport, spdk_nvme_probe_cb probe_cb, void *cb_ctx, void *devhandle); \ int nvme_ ## name ## _ctrlr_enable(struct spdk_nvme_ctrlr *ctrlr); \ int nvme_ ## name ## _ctrlr_get_pci_id(struct spdk_nvme_ctrlr *ctrlr, struct spdk_pci_id *pci_id); \ int nvme_ ## name ## _ctrlr_set_reg_4(struct spdk_nvme_ctrlr *ctrlr, uint32_t offset, uint32_t value); \ diff --git a/lib/nvme/nvme_pcie.c b/lib/nvme/nvme_pcie.c index 09f45eac4..4946e7822 100644 --- a/lib/nvme/nvme_pcie.c +++ b/lib/nvme/nvme_pcie.c @@ -68,6 +68,10 @@ */ #define NVME_MAX_XFER_SIZE NVME_MAX_PRP_LIST_ENTRIES * PAGE_SIZE +struct nvme_pcie_enum_ctx { + spdk_nvme_probe_cb probe_cb; + void *cb_ctx; +}; /* PCIe transport extensions for spdk_nvme_ctrlr */ struct nvme_pcie_ctrlr { @@ -431,20 +435,37 @@ static int pcie_nvme_enum_cb(void *ctx, struct spdk_pci_device *pci_dev) { struct spdk_nvme_probe_info probe_info = {}; - struct nvme_enum_ctx *enum_ctx = ctx; + struct nvme_pcie_enum_ctx *enum_ctx = ctx; + struct spdk_nvme_ctrlr *ctrlr; probe_info.pci_addr = spdk_pci_device_get_addr(pci_dev); probe_info.pci_id = spdk_pci_device_get_id(pci_dev); - return enum_ctx->enum_cb(SPDK_NVME_TRANSPORT_PCIE, &enum_ctx->usr_ctx, &probe_info, - (void *)pci_dev); + /* Verify that this controller is not already attached */ + TAILQ_FOREACH(ctrlr, &g_spdk_nvme_driver->attached_ctrlrs, tailq) { + /* NOTE: In the case like multi-process environment where the device handle is + * different per each process, we compare by BDF to determine whether it is the + * same controller. + */ + if (spdk_pci_addr_compare(&probe_info.pci_addr, &ctrlr->probe_info.pci_addr) == 0) { + return 0; + } + } + + return nvme_probe_one(SPDK_NVME_TRANSPORT_PCIE, enum_ctx->probe_cb, enum_ctx->cb_ctx, + &probe_info, pci_dev); } int nvme_pcie_ctrlr_scan(enum spdk_nvme_transport_type transport, - struct nvme_enum_ctx *enum_ctx, void *devhandle) + spdk_nvme_probe_cb probe_cb, void *cb_ctx, void *devhandle) { - return spdk_pci_enumerate(SPDK_PCI_DEVICE_NVME, pcie_nvme_enum_cb, enum_ctx); + struct nvme_pcie_enum_ctx enum_ctx; + + enum_ctx.probe_cb = probe_cb; + enum_ctx.cb_ctx = cb_ctx; + + return spdk_pci_enumerate(SPDK_PCI_DEVICE_NVME, pcie_nvme_enum_cb, &enum_ctx); } struct spdk_nvme_ctrlr *nvme_pcie_ctrlr_construct(enum spdk_nvme_transport_type transport, diff --git a/lib/nvme/nvme_rdma.c b/lib/nvme/nvme_rdma.c index 76debae71..0178cb6fb 100644 --- a/lib/nvme/nvme_rdma.c +++ b/lib/nvme/nvme_rdma.c @@ -1138,7 +1138,7 @@ nvme_fabrics_get_log_discovery_page(struct spdk_nvme_ctrlr *ctrlr, /* This function must only be called while holding g_spdk_nvme_driver->lock */ int nvme_rdma_ctrlr_scan(enum spdk_nvme_transport_type transport, - struct nvme_enum_ctx *enum_ctx, void *devhandle) + spdk_nvme_probe_cb probe_cb, void *cb_ctx, void *devhandle) { struct spdk_nvme_discover_info *info = (struct spdk_nvme_discover_info *)devhandle; struct spdk_nvme_probe_info probe_info; @@ -1170,7 +1170,8 @@ nvme_rdma_ctrlr_scan(enum spdk_nvme_transport_type transport, discover_info.trsvcid = probe_info.trsvcid = (const char *)log_page->entries[i].trsvcid; SPDK_NOTICELOG("nqn=%s, traddr=%s, trsvcid=%s\n", discover_info.nqn, discover_info.traddr, discover_info.trsvcid); - enum_ctx->enum_cb(info->type, &enum_ctx->usr_ctx, &probe_info, (void *)&discover_info); + /* Todo: need to differentiate the NVMe over fabrics to avoid duplicated connection */ + nvme_probe_one(info->type, probe_cb, cb_ctx, &probe_info, (void *)&discover_info); } nvme_ctrlr_destruct(discovery_ctrlr); diff --git a/lib/nvme/nvme_transport.c b/lib/nvme/nvme_transport.c index c48cdd113..bec85dce7 100644 --- a/lib/nvme/nvme_transport.c +++ b/lib/nvme/nvme_transport.c @@ -74,9 +74,9 @@ struct spdk_nvme_ctrlr *nvme_transport_ctrlr_construct(enum spdk_nvme_transport_ int nvme_transport_ctrlr_scan(enum spdk_nvme_transport_type transport, - struct nvme_enum_ctx *enum_ctx, void *devhandle) + spdk_nvme_probe_cb probe_cb, void *cb_ctx, void *devhandle) { - NVME_TRANSPORT_CALL(transport, ctrlr_scan, (transport, enum_ctx, devhandle)); + NVME_TRANSPORT_CALL(transport, ctrlr_scan, (transport, probe_cb, cb_ctx, devhandle)); } int diff --git a/test/lib/nvme/unit/nvme_c/nvme_ut.c b/test/lib/nvme/unit/nvme_c/nvme_ut.c index 3878d1214..d7ffa3c3f 100644 --- a/test/lib/nvme/unit/nvme_c/nvme_ut.c +++ b/test/lib/nvme/unit/nvme_c/nvme_ut.c @@ -65,7 +65,7 @@ struct spdk_nvme_ctrlr *nvme_transport_ctrlr_construct(enum spdk_nvme_transport_ int nvme_transport_ctrlr_scan(enum spdk_nvme_transport_type transport, - struct nvme_enum_ctx *enum_ctx, void *devhandle) + spdk_nvme_probe_cb probe_cb, void *cb_ctx, void *devhandle) { return 0; } 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 aad487dc6..23e06137c 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 @@ -156,7 +156,7 @@ nvme_ctrlr_get_ref_count(struct spdk_nvme_ctrlr *ctrlr) int nvme_transport_ctrlr_scan(enum spdk_nvme_transport_type transport, - struct nvme_enum_ctx *enum_ctx, void *devhandle) + spdk_nvme_probe_cb probe_cb, void *cb_ctx, void *devhandle) { return 0; }