Commit 4ce22e0f authored by Changpeng Liu's avatar Changpeng Liu
Browse files

nvme: use asynchronous probe API in spdk_nvme_probe()



Change-Id: I64a7aa548a82f2698f8c16611bf939c9eb377348
Signed-off-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/447022


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent 59746336
Loading
Loading
Loading
Loading
+19 −32
Original line number Diff line number Diff line
@@ -469,26 +469,14 @@ static int
nvme_init_controllers(struct spdk_nvme_probe_ctx *probe_ctx)
{
	int rc = 0;
	struct spdk_nvme_ctrlr *ctrlr, *ctrlr_tmp;

	if (!spdk_process_is_primary() && probe_ctx->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
		return 0;
	}

	/* Initialize all new controllers in the probe context init_ctrlrs list in parallel. */
	while (!TAILQ_EMPTY(&probe_ctx->init_ctrlrs)) {
		TAILQ_FOREACH_SAFE(ctrlr, &probe_ctx->init_ctrlrs, tailq, ctrlr_tmp) {
			rc = nvme_ctrlr_poll_internal(ctrlr, probe_ctx);
			if (rc) {
				break;
			}
	while (true) {
		rc = spdk_nvme_probe_poll_async(probe_ctx);
		if (rc != -EAGAIN) {
			return rc;
		}
	}

	nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
	g_spdk_nvme_driver->initialized = true;
	nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);

	return rc;
}

@@ -606,14 +594,8 @@ 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,
		spdk_nvme_remove_cb remove_cb)
{
	int rc;
	struct spdk_nvme_transport_id trid_pcie;
	struct spdk_nvme_probe_ctx probe_ctx;

	rc = nvme_driver_init();
	if (rc != 0) {
		return rc;
	}
	struct spdk_nvme_probe_ctx *probe_ctx;

	if (trid == NULL) {
		memset(&trid_pcie, 0, sizeof(trid_pcie));
@@ -621,17 +603,18 @@ spdk_nvme_probe(const struct spdk_nvme_transport_id *trid, void *cb_ctx,
		trid = &trid_pcie;
	}

	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) {
		return rc;
	probe_ctx = spdk_nvme_probe_async(trid, cb_ctx, probe_cb,
					  attach_cb, remove_cb);
	if (!probe_ctx) {
		SPDK_ERRLOG("Create probe context failed\n");
		return -1;
	}

	/*
	 * Keep going even if one or more nvme_attach() calls failed,
	 *  but maintain the value of rc to signal errors when we return.
	 */
	return nvme_init_controllers(&probe_ctx);
	return nvme_init_controllers(probe_ctx);
}

static bool
@@ -658,7 +641,7 @@ spdk_nvme_connect(const struct spdk_nvme_transport_id *trid,
	struct spdk_nvme_ctrlr_connect_opts *user_connect_opts = NULL;
	struct spdk_nvme_ctrlr *ctrlr = NULL;
	spdk_nvme_probe_cb probe_cb = NULL;
	struct spdk_nvme_probe_ctx probe_ctx;
	struct spdk_nvme_probe_ctx *probe_ctx;

	if (trid == NULL) {
		SPDK_ERRLOG("No transport ID specified\n");
@@ -677,10 +660,14 @@ spdk_nvme_connect(const struct spdk_nvme_transport_id *trid,
		probe_cb = spdk_nvme_connect_probe_cb;
	}

	spdk_nvme_probe_ctx_init(&probe_ctx, trid, user_connect_opts, probe_cb, NULL, NULL);
	spdk_nvme_probe_internal(&probe_ctx, true);
	probe_ctx = calloc(1, sizeof(*probe_ctx));
	if (!probe_ctx) {
		return NULL;
	}
	spdk_nvme_probe_ctx_init(probe_ctx, trid, user_connect_opts, probe_cb, NULL, NULL);
	spdk_nvme_probe_internal(probe_ctx, true);

	rc = nvme_init_controllers(&probe_ctx);
	rc = nvme_init_controllers(probe_ctx);
	if (rc != 0) {
		return NULL;
	}
+39 −25
Original line number Diff line number Diff line
@@ -256,6 +256,18 @@ test_spdk_nvme_connect(void)
	CU_ASSERT(ret_ctrlr == NULL);
}

static struct spdk_nvme_probe_ctx *
test_nvme_init_get_probe_ctx(void)
{
	struct spdk_nvme_probe_ctx *probe_ctx;

	probe_ctx = calloc(1, sizeof(*probe_ctx));
	SPDK_CU_ASSERT_FATAL(probe_ctx != NULL);
	TAILQ_INIT(&probe_ctx->init_ctrlrs);

	return probe_ctx;
}

static void
test_nvme_init_controllers(void)
{
@@ -263,17 +275,16 @@ test_nvme_init_controllers(void)
	struct nvme_driver test_driver;
	void *cb_ctx = NULL;
	spdk_nvme_attach_cb attach_cb = dummy_attach_cb;
	struct spdk_nvme_probe_ctx probe_ctx;
	struct spdk_nvme_ctrlr ctrlr;
	struct spdk_nvme_probe_ctx *probe_ctx;
	struct spdk_nvme_ctrlr *ctrlr;
	pthread_mutexattr_t attr;

	g_spdk_nvme_driver = &test_driver;
	memset(&ctrlr, 0, sizeof(struct spdk_nvme_ctrlr));
	ctrlr.trid.trtype = SPDK_NVME_TRANSPORT_PCIE;
	ctrlr = calloc(1, sizeof(*ctrlr));
	SPDK_CU_ASSERT_FATAL(ctrlr != NULL);
	ctrlr->trid.trtype = SPDK_NVME_TRANSPORT_PCIE;
	CU_ASSERT(pthread_mutexattr_init(&attr) == 0);
	CU_ASSERT(pthread_mutex_init(&test_driver.lock, &attr) == 0);
	TAILQ_INIT(&probe_ctx.init_ctrlrs);
	TAILQ_INSERT_TAIL(&probe_ctx.init_ctrlrs, &ctrlr, tailq);
	TAILQ_INIT(&test_driver.shared_attached_ctrlrs);

	/*
@@ -284,13 +295,14 @@ test_nvme_init_controllers(void)
	MOCK_SET(spdk_process_is_primary, 1);
	g_spdk_nvme_driver->initialized = false;
	ut_destruct_called = false;
	probe_ctx.cb_ctx = cb_ctx;
	probe_ctx.attach_cb = attach_cb;
	probe_ctx.trid.trtype = SPDK_NVME_TRANSPORT_PCIE;
	rc = nvme_init_controllers(&probe_ctx);
	probe_ctx = test_nvme_init_get_probe_ctx();
	TAILQ_INSERT_TAIL(&probe_ctx->init_ctrlrs, ctrlr, tailq);
	probe_ctx->cb_ctx = cb_ctx;
	probe_ctx->attach_cb = attach_cb;
	probe_ctx->trid.trtype = SPDK_NVME_TRANSPORT_PCIE;
	rc = nvme_init_controllers(probe_ctx);
	CU_ASSERT(rc != 0);
	CU_ASSERT(g_spdk_nvme_driver->initialized == true);
	CU_ASSERT(TAILQ_EMPTY(&probe_ctx.init_ctrlrs));
	CU_ASSERT(ut_destruct_called == true);

	/*
@@ -298,32 +310,34 @@ test_nvme_init_controllers(void)
	 * forward by setting the ctrl state so that it can be moved
	 * the shared_attached_ctrlrs list.
	 */
	TAILQ_INSERT_TAIL(&probe_ctx.init_ctrlrs, &ctrlr, tailq);
	ctrlr.state = NVME_CTRLR_STATE_READY;
	probe_ctx = test_nvme_init_get_probe_ctx();
	TAILQ_INSERT_TAIL(&probe_ctx->init_ctrlrs, ctrlr, tailq);
	ctrlr->state = NVME_CTRLR_STATE_READY;
	MOCK_SET(nvme_ctrlr_process_init, 0);
	rc = nvme_init_controllers(&probe_ctx);
	rc = nvme_init_controllers(probe_ctx);
	CU_ASSERT(rc == 0);
	CU_ASSERT(ut_attach_cb_called == true);
	CU_ASSERT(TAILQ_EMPTY(&probe_ctx.init_ctrlrs));
	CU_ASSERT(TAILQ_EMPTY(&g_nvme_attached_ctrlrs));
	CU_ASSERT(TAILQ_FIRST(&g_spdk_nvme_driver->shared_attached_ctrlrs) == &ctrlr);
	TAILQ_REMOVE(&g_spdk_nvme_driver->shared_attached_ctrlrs, &ctrlr, tailq);
	CU_ASSERT(TAILQ_FIRST(&g_spdk_nvme_driver->shared_attached_ctrlrs) == ctrlr);
	TAILQ_REMOVE(&g_spdk_nvme_driver->shared_attached_ctrlrs, ctrlr, tailq);

	/*
	 * Non-PCIe controllers should be added to the per-process list, not the shared list.
	 */
	memset(&ctrlr, 0, sizeof(struct spdk_nvme_ctrlr));
	ctrlr.trid.trtype = SPDK_NVME_TRANSPORT_RDMA;
	TAILQ_INSERT_TAIL(&probe_ctx.init_ctrlrs, &ctrlr, tailq);
	ctrlr.state = NVME_CTRLR_STATE_READY;
	memset(ctrlr, 0, sizeof(struct spdk_nvme_ctrlr));
	ctrlr->trid.trtype = SPDK_NVME_TRANSPORT_RDMA;
	probe_ctx = test_nvme_init_get_probe_ctx();
	TAILQ_INSERT_TAIL(&probe_ctx->init_ctrlrs, ctrlr, tailq);
	ctrlr->state = NVME_CTRLR_STATE_READY;
	MOCK_SET(nvme_ctrlr_process_init, 0);
	rc = nvme_init_controllers(&probe_ctx);
	rc = nvme_init_controllers(probe_ctx);
	CU_ASSERT(rc == 0);
	CU_ASSERT(ut_attach_cb_called == true);
	CU_ASSERT(TAILQ_EMPTY(&probe_ctx.init_ctrlrs));
	CU_ASSERT(TAILQ_EMPTY(&g_spdk_nvme_driver->shared_attached_ctrlrs));
	CU_ASSERT(TAILQ_FIRST(&g_nvme_attached_ctrlrs) == &ctrlr);
	TAILQ_REMOVE(&g_nvme_attached_ctrlrs, &ctrlr, tailq);
	CU_ASSERT(TAILQ_FIRST(&g_nvme_attached_ctrlrs) == ctrlr);
	TAILQ_REMOVE(&g_nvme_attached_ctrlrs, ctrlr, tailq);
	free(ctrlr);
	CU_ASSERT(TAILQ_EMPTY(&g_nvme_attached_ctrlrs));

	g_spdk_nvme_driver = NULL;
	pthread_mutexattr_destroy(&attr);