2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2019 Intel Corporation.
|
2019-10-01 10:55:40 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
|
|
|
#include "spdk/nvme.h"
|
|
|
|
#include "spdk/env.h"
|
|
|
|
#include "spdk/string.h"
|
|
|
|
|
|
|
|
struct ctrlr_entry {
|
2020-09-28 01:48:34 +00:00
|
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
|
|
TAILQ_ENTRY(ctrlr_entry) link;
|
|
|
|
char name[1024];
|
2019-10-01 10:55:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ns_entry {
|
|
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
|
|
struct spdk_nvme_ns *ns;
|
2020-09-28 01:48:34 +00:00
|
|
|
TAILQ_ENTRY(ns_entry) link;
|
2019-10-01 10:55:40 +00:00
|
|
|
struct spdk_nvme_qpair *qpair;
|
|
|
|
};
|
|
|
|
|
2020-09-28 01:48:34 +00:00
|
|
|
static TAILQ_HEAD(, ctrlr_entry) g_controllers = TAILQ_HEAD_INITIALIZER(g_controllers);
|
|
|
|
static TAILQ_HEAD(, ns_entry) g_namespaces = TAILQ_HEAD_INITIALIZER(g_namespaces);
|
2019-10-01 10:55:40 +00:00
|
|
|
static int g_startup_time = 0;
|
|
|
|
|
|
|
|
static bool
|
|
|
|
probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
|
|
|
|
struct spdk_nvme_ctrlr_opts *opts)
|
|
|
|
{
|
|
|
|
printf("Attaching to %s\n", trid->traddr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
|
|
|
|
struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct ctrlr_entry *entry;
|
|
|
|
const struct spdk_nvme_ctrlr_data *cdata;
|
|
|
|
|
|
|
|
entry = malloc(sizeof(struct ctrlr_entry));
|
|
|
|
if (entry == NULL) {
|
|
|
|
perror("ctrlr_entry malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Attached to %s\n", trid->traddr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* spdk_nvme_ctrlr is the logical abstraction in SPDK for an NVMe
|
|
|
|
* controller. During initialization, the IDENTIFY data for the
|
|
|
|
* controller is read using an NVMe admin command, and that data
|
|
|
|
* can be retrieved using spdk_nvme_ctrlr_get_data() to get
|
|
|
|
* detailed information on the controller. Refer to the NVMe
|
|
|
|
* specification for more details on IDENTIFY for NVMe controllers.
|
|
|
|
*/
|
|
|
|
cdata = spdk_nvme_ctrlr_get_data(ctrlr);
|
|
|
|
|
|
|
|
snprintf(entry->name, sizeof(entry->name), "%-20.20s (%-20.20s)", cdata->mn, cdata->sn);
|
|
|
|
|
|
|
|
entry->ctrlr = ctrlr;
|
2020-09-28 01:48:34 +00:00
|
|
|
TAILQ_INSERT_TAIL(&g_controllers, entry, link);
|
2019-10-01 10:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
2020-09-28 01:48:34 +00:00
|
|
|
struct ns_entry *ns_entry, *tmp_ns_entry;
|
|
|
|
struct ctrlr_entry *ctrlr_entry, *tmp_ctrlr_entry;
|
2020-10-16 00:02:22 +00:00
|
|
|
struct spdk_nvme_detach_ctx *detach_ctx = NULL;
|
2019-10-01 10:55:40 +00:00
|
|
|
|
2020-09-28 01:48:34 +00:00
|
|
|
TAILQ_FOREACH_SAFE(ns_entry, &g_namespaces, link, tmp_ns_entry) {
|
|
|
|
TAILQ_REMOVE(&g_namespaces, ns_entry, link);
|
2019-10-01 10:55:40 +00:00
|
|
|
free(ns_entry);
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:48:34 +00:00
|
|
|
TAILQ_FOREACH_SAFE(ctrlr_entry, &g_controllers, link, tmp_ctrlr_entry) {
|
|
|
|
TAILQ_REMOVE(&g_controllers, ctrlr_entry, link);
|
2020-10-16 00:02:22 +00:00
|
|
|
spdk_nvme_detach_async(ctrlr_entry->ctrlr, &detach_ctx);
|
2019-10-01 10:55:40 +00:00
|
|
|
free(ctrlr_entry);
|
|
|
|
}
|
2020-10-16 00:02:22 +00:00
|
|
|
|
2021-06-24 20:30:55 +00:00
|
|
|
if (detach_ctx) {
|
|
|
|
spdk_nvme_detach_poll(detach_ctx);
|
2020-10-16 00:02:22 +00:00
|
|
|
}
|
2019-10-01 10:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(const char *program_name)
|
|
|
|
{
|
|
|
|
printf("%s [options]", program_name);
|
|
|
|
printf("\n");
|
|
|
|
printf("options:\n");
|
|
|
|
printf(" -t The maximum time needed for startup. The unit is us. The value should be bigger than 0.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int op;
|
|
|
|
|
|
|
|
while ((op = getopt(argc, argv, "t:")) != -1) {
|
|
|
|
switch (op) {
|
|
|
|
case 't':
|
|
|
|
g_startup_time = spdk_strtol(optarg, 10);
|
|
|
|
if (g_startup_time < 0) {
|
|
|
|
fprintf(stderr, "Invalid nvme startup time\n");
|
|
|
|
return g_startup_time;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-22 21:35:04 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2019-10-01 10:55:40 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct spdk_env_opts opts;
|
|
|
|
uint64_t start_tsc, end_tsc, tsc_diff;
|
|
|
|
float time_used_in_usec;
|
|
|
|
|
|
|
|
rc = parse_args(argc, argv);
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_startup_time == 0) {
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_tsc = spdk_get_ticks();
|
|
|
|
/*
|
|
|
|
* SPDK relies on an abstraction around the local environment
|
|
|
|
* named env that handles memory allocation and PCI device operations.
|
|
|
|
* This library must be initialized first.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
spdk_env_opts_init(&opts);
|
|
|
|
opts.name = "startup";
|
|
|
|
opts.shm_id = 0;
|
|
|
|
if (spdk_env_init(&opts) < 0) {
|
|
|
|
fprintf(stderr, "Unable to initialize SPDK env\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Initializing NVMe Controllers\n");
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the SPDK NVMe enumeration process. probe_cb will be called
|
|
|
|
* for each NVMe controller found, giving our application a choice on
|
|
|
|
* whether to attach to each controller. attach_cb will then be
|
|
|
|
* called for each controller after the SPDK NVMe driver has completed
|
|
|
|
* initializing the controller we chose to attach.
|
|
|
|
*/
|
|
|
|
rc = spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL);
|
|
|
|
if (rc != 0) {
|
|
|
|
fprintf(stderr, "spdk_nvme_probe() failed\n");
|
|
|
|
cleanup();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:48:34 +00:00
|
|
|
if (TAILQ_EMPTY(&g_controllers)) {
|
2019-10-01 10:55:40 +00:00
|
|
|
fprintf(stderr, "no NVMe controllers found\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_tsc = spdk_get_ticks();
|
|
|
|
tsc_diff = end_tsc - start_tsc;
|
|
|
|
time_used_in_usec = ((float)tsc_diff) * 1000 * 1000 / spdk_get_ticks_hz();
|
|
|
|
printf("Initialization complete.\n");
|
|
|
|
printf("Time used:%-16.3f(us).\n", time_used_in_usec);
|
|
|
|
if (time_used_in_usec > g_startup_time) {
|
|
|
|
fprintf(stderr, "Too long time for initialization.\n");
|
|
|
|
cleanup();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
return 0;
|
|
|
|
}
|