diff --git a/CHANGELOG.md b/CHANGELOG.md index 9653b4da1..7fa456457 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ ### nvme -Added asynchronous probe support. New APIs spdk_nvme_probe_ctx_init(), spdk_nvme_probe_async() -and spdk_nvme_probe_poll_async() were added to enable this feature. +Added asynchronous probe support. New APIs spdk_nvme_probe_async() and +spdk_nvme_probe_poll_async() were added to enable this feature. ### raid diff --git a/include/spdk/nvme.h b/include/spdk/nvme.h index 9467570cb..4cfd2b3bc 100644 --- a/include/spdk/nvme.h +++ b/include/spdk/nvme.h @@ -566,19 +566,14 @@ struct spdk_nvme_ctrlr *spdk_nvme_connect(const struct spdk_nvme_transport_id *t const struct spdk_nvme_ctrlr_opts *opts, size_t opts_size); -struct spdk_nvme_probe_ctx { - struct spdk_nvme_transport_id trid; - void *cb_ctx; - spdk_nvme_probe_cb probe_cb; - spdk_nvme_attach_cb attach_cb; - spdk_nvme_remove_cb remove_cb; - TAILQ_HEAD(, spdk_nvme_ctrlr) init_ctrlrs; -}; +struct spdk_nvme_probe_ctx; /** - * Initialize a context to track the probe result based on transport ID. + * Probe and add controllers to the probe context list. + * + * Users must call spdk_nvme_probe_poll_async() to initialize + * controllers in the probe context list to the READY state. * - * \param probe_ctx Context used to track probe actions. * \param trid The transport ID indicating which bus to enumerate. If the trtype * is PCIe or trid is NULL, this will scan the local PCIe bus. If the trtype is * RDMA, the traddr and trsvcid must point at the location of an NVMe-oF discovery @@ -592,25 +587,13 @@ struct spdk_nvme_probe_ctx { * spdk_nvme_probe() call but are no longer attached to the system. Optional; * specify NULL if removal notices are not desired. * + * \return probe context on success, NULL on failure. */ -void spdk_nvme_probe_ctx_init(struct spdk_nvme_probe_ctx *probe_ctx, - const struct spdk_nvme_transport_id *trid, - void *cb_ctx, - spdk_nvme_probe_cb probe_cb, - spdk_nvme_attach_cb attach_cb, - spdk_nvme_remove_cb remove_cb); - -/** - * Probe and add controllers to the probe context list. - * - * Users must call spdk_nvme_probe_poll_async() to initialize - * controllers in the probe context list to the READY state. - * - * \param probe_ctx Context used to track probe actions. - * - * \return 0 on success, -1 on failure. - */ -int spdk_nvme_probe_async(struct spdk_nvme_probe_ctx *probe_ctx); +struct spdk_nvme_probe_ctx *spdk_nvme_probe_async(const struct spdk_nvme_transport_id *trid, + void *cb_ctx, + spdk_nvme_probe_cb probe_cb, + spdk_nvme_attach_cb attach_cb, + spdk_nvme_remove_cb remove_cb); /** * Start controllers in the context list. diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index 59d9d571e..4bb172de9 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -585,6 +585,22 @@ spdk_nvme_probe_internal(struct spdk_nvme_probe_ctx *probe_ctx, return 0; } +static void +spdk_nvme_probe_ctx_init(struct spdk_nvme_probe_ctx *probe_ctx, + const struct spdk_nvme_transport_id *trid, + void *cb_ctx, + spdk_nvme_probe_cb probe_cb, + spdk_nvme_attach_cb attach_cb, + spdk_nvme_remove_cb remove_cb) +{ + probe_ctx->trid = *trid; + probe_ctx->cb_ctx = cb_ctx; + probe_ctx->probe_cb = probe_cb; + probe_ctx->attach_cb = attach_cb; + probe_ctx->remove_cb = remove_cb; + TAILQ_INIT(&probe_ctx->init_ctrlrs); +} + int spdk_nvme_probe(const struct spdk_nvme_transport_id *trid, void *cb_ctx, spdk_nvme_probe_cb probe_cb, spdk_nvme_attach_cb attach_cb, @@ -1046,33 +1062,34 @@ spdk_nvme_prchk_flags_str(uint32_t prchk_flags) } } -void -spdk_nvme_probe_ctx_init(struct spdk_nvme_probe_ctx *probe_ctx, - const struct spdk_nvme_transport_id *trid, - void *cb_ctx, - spdk_nvme_probe_cb probe_cb, - spdk_nvme_attach_cb attach_cb, - spdk_nvme_remove_cb remove_cb) -{ - probe_ctx->trid = *trid; - probe_ctx->cb_ctx = cb_ctx; - probe_ctx->probe_cb = probe_cb; - probe_ctx->attach_cb = attach_cb; - probe_ctx->remove_cb = remove_cb; - TAILQ_INIT(&probe_ctx->init_ctrlrs); -} - -int -spdk_nvme_probe_async(struct spdk_nvme_probe_ctx *probe_ctx) +struct spdk_nvme_probe_ctx * +spdk_nvme_probe_async(const struct spdk_nvme_transport_id *trid, + void *cb_ctx, + spdk_nvme_probe_cb probe_cb, + spdk_nvme_attach_cb attach_cb, + spdk_nvme_remove_cb remove_cb) { int rc; + struct spdk_nvme_probe_ctx *probe_ctx; rc = nvme_driver_init(); if (rc != 0) { - return rc; + return NULL; } - return spdk_nvme_probe_internal(probe_ctx, false); + probe_ctx = calloc(1, sizeof(*probe_ctx)); + if (!probe_ctx) { + return NULL; + } + + spdk_nvme_probe_ctx_init(probe_ctx, trid, cb_ctx, probe_cb, attach_cb, remove_cb); + rc = spdk_nvme_probe_internal(probe_ctx, false); + if (rc != 0) { + free(probe_ctx); + return NULL; + } + + return probe_ctx; } bool diff --git a/lib/nvme/nvme_internal.h b/lib/nvme/nvme_internal.h index 1bdaa8697..cb59f3016 100644 --- a/lib/nvme/nvme_internal.h +++ b/lib/nvme/nvme_internal.h @@ -691,6 +691,15 @@ struct spdk_nvme_ctrlr { uint32_t outstanding_aborts; }; +struct spdk_nvme_probe_ctx { + struct spdk_nvme_transport_id trid; + void *cb_ctx; + spdk_nvme_probe_cb probe_cb; + spdk_nvme_attach_cb attach_cb; + spdk_nvme_remove_cb remove_cb; + TAILQ_HEAD(, spdk_nvme_ctrlr) init_ctrlrs; +}; + struct nvme_driver { pthread_mutex_t lock;