Skip to content

Commit 833c1f0

Browse files
committed
mi: replace nvme_mi_*global_context with nvme_*_global_context
Merge these two function together and avoid code duplication. Signed-off-by: Daniel Wagner <[email protected]>
1 parent ef61562 commit 833c1f0

10 files changed

Lines changed: 41 additions & 105 deletions

File tree

libnvme/examples/mi-mctp-ae.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int main(int argc, char **argv)
109109
return EXIT_FAILURE;
110110
}
111111

112-
ctx = nvme_mi_create_global_ctx(stderr, DEFAULT_LOGLEVEL);
112+
ctx = nvme_create_global_ctx(stderr, DEFAULT_LOGLEVEL);
113113
if (!ctx)
114114
err(EXIT_FAILURE, "can't create NVMe root");
115115

@@ -173,7 +173,7 @@ int main(int argc, char **argv)
173173
//Cleanup
174174
nvme_mi_aem_disable(ep);
175175
nvme_mi_close(ep);
176-
nvme_mi_free_global_ctx(ctx);
176+
nvme_free_global_ctx(ctx);
177177

178178
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
179179
}

libnvme/examples/mi-mctp-csi-test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ int main(int argc, char **argv)
237237
return EXIT_FAILURE;
238238
}
239239

240-
ctx = nvme_mi_create_global_ctx(stderr, DEFAULT_LOGLEVEL);
240+
ctx = nvme_create_global_ctx(stderr, DEFAULT_LOGLEVEL);
241241
if (!ctx)
242242
err(EXIT_FAILURE, "can't create NVMe root");
243243

244244
rc = do_action_endpoint(action, ctx, net, eid, argc, argv);
245-
nvme_mi_free_global_ctx(ctx);
245+
nvme_free_global_ctx(ctx);
246246

247247
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
248248
}

libnvme/examples/mi-mctp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,9 @@ int main(int argc, char **argv)
846846
printf("---\n");
847847
free(desc);
848848
}
849-
nvme_mi_free_global_ctx(ctx);
849+
nvme_free_global_ctx(ctx);
850850
} else {
851-
ctx = nvme_mi_create_global_ctx(stderr, DEFAULT_LOGLEVEL);
851+
ctx = nvme_create_global_ctx(stderr, DEFAULT_LOGLEVEL);
852852
if (!ctx)
853853
err(EXIT_FAILURE, "can't create NVMe root");
854854

@@ -857,7 +857,7 @@ int main(int argc, char **argv)
857857
errx(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid);
858858
rc = do_action_endpoint(action, ep, argc, argv);
859859
nvme_mi_close(ep);
860-
nvme_mi_free_global_ctx(ctx);
860+
nvme_free_global_ctx(ctx);
861861
}
862862

863863
return rc ? EXIT_FAILURE : EXIT_SUCCESS;

libnvme/src/libnvme.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ LIBNVME_2_0 {
157157
nvme_mi_scan_ep;
158158
nvme_mi_scan_mctp;
159159
nvme_mi_set_csi;
160-
nvme_mi_set_probe_enabled;
161160
nvme_mi_status_to_string;
162161
nvme_mi_submit_entry;
163162
nvme_mi_submit_exit;
@@ -229,6 +228,7 @@ LIBNVME_2_0 {
229228
nvme_set_ioctl_probing;
230229
nvme_set_keyring;
231230
nvme_set_logging_level;
231+
nvme_set_probe_enabled;
232232
nvme_set_property;
233233
nvme_set_root;
234234
nvme_skip_namespaces;

libnvme/src/nvme/lib.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
#include <fcntl.h>
1616
#include <sys/ioctl.h>
1717

18+
static bool nvme_mi_probe_enabled_default(void)
19+
{
20+
char *val;
21+
22+
val = getenv("LIBNVME_MI_PROBE_ENABLED");
23+
if (!val)
24+
return true;
25+
26+
return strcmp(val, "0") &&
27+
strcasecmp(val, "false") &&
28+
strncasecmp(val, "disable", 7);
29+
30+
}
31+
1832
struct nvme_global_ctx *nvme_create_global_ctx(FILE *fp, int log_level)
1933
{
2034
struct nvme_global_ctx *ctx;
@@ -40,13 +54,15 @@ struct nvme_global_ctx *nvme_create_global_ctx(FILE *fp, int log_level)
4054
list_head_init(&ctx->endpoints);
4155

4256
ctx->ioctl_probing = true;
57+
ctx->mi_probe_enabled = nvme_mi_probe_enabled_default();
4358

4459
return ctx;
4560
}
4661

4762
void nvme_free_global_ctx(struct nvme_global_ctx *ctx)
4863
{
4964
struct nvme_host *h, *_h;
65+
nvme_mi_ep_t ep, tmp;
5066

5167
if (!ctx)
5268
return;
@@ -57,6 +73,8 @@ void nvme_free_global_ctx(struct nvme_global_ctx *ctx)
5773
free(ctx->options);
5874
nvme_for_each_host_safe(ctx, h, _h)
5975
__nvme_free_host(h);
76+
nvme_mi_for_each_endpoint_safe(ctx, ep, tmp)
77+
nvme_mi_close(ep);
6078
free(ctx->config_file);
6179
free(ctx->application);
6280
free(ctx);

libnvme/src/nvme/lib.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ void nvme_transport_handle_set_decide_retry(struct nvme_transport_handle *hdl,
202202
bool (*decide_retry)(struct nvme_transport_handle *hdl,
203203
struct nvme_passthru_cmd *cmd, int err));
204204

205+
/**
206+
* nvme_set_probe_enabled() - enable/disable the probe for new MI endpoints
207+
* @ctx: &struct nvme_global_ctx object
208+
* @enabled: whether to probe new endpoints
209+
*
210+
* Controls whether newly-created endpoints are probed for quirks on creation.
211+
* Defaults to enabled, which results in some initial messaging with the
212+
* endpoint to determine model-specific details.
213+
*/
214+
void nvme_set_probe_enabled(struct nvme_global_ctx *ctx, bool enabled);
215+
205216
/**
206217
* nvme_set_dry_run() - Set global dry run state
207218
* @ctx: struct nvme_global_ctx object

libnvme/src/nvme/mi.c

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@ static int nvme_mi_get_async_message(nvme_mi_ep_t ep,
4343
static const int default_timeout = 1000; /* milliseconds; endpoints may
4444
override */
4545

46-
static bool nvme_mi_probe_enabled_default(void)
47-
{
48-
char *val;
49-
50-
val = getenv("LIBNVME_MI_PROBE_ENABLED");
51-
if (!val)
52-
return true;
53-
54-
return strcmp(val, "0") &&
55-
strcasecmp(val, "false") &&
56-
strncasecmp(val, "disable", 7);
57-
58-
}
59-
6046
static int parse_devname(const char *dev, unsigned int *net, uint8_t *eid,
6147
unsigned int *ctrl)
6248
{
@@ -113,53 +99,6 @@ void __nvme_transport_handle_close_mi(struct nvme_transport_handle *hdl)
11399
free(hdl);
114100
}
115101

116-
/* MI-equivalent of nvme_create_root, but avoids clashing symbol names
117-
* when linking against both libnvme and libnvme-mi.
118-
*/
119-
struct nvme_global_ctx *nvme_mi_create_global_ctx(FILE *fp, int log_level)
120-
{
121-
struct nvme_global_ctx *ctx;
122-
int fd;
123-
124-
ctx = calloc(1, sizeof(*ctx));
125-
if (!ctx)
126-
return NULL;
127-
128-
if (fp) {
129-
fd = fileno(fp);
130-
if (fd < 0) {
131-
free(ctx);
132-
return NULL;
133-
}
134-
} else
135-
fd = STDERR_FILENO;
136-
137-
ctx->log.fd = fd;
138-
ctx->log.level = log_level;
139-
140-
ctx->mi_probe_enabled = nvme_mi_probe_enabled_default();
141-
142-
list_head_init(&ctx->hosts);
143-
list_head_init(&ctx->endpoints);
144-
145-
return ctx;
146-
}
147-
148-
void nvme_mi_free_global_ctx(struct nvme_global_ctx *ctx)
149-
{
150-
nvme_mi_ep_t ep, tmp;
151-
152-
nvme_mi_for_each_endpoint_safe(ctx, ep, tmp)
153-
nvme_mi_close(ep);
154-
155-
free(ctx);
156-
}
157-
158-
void nvme_mi_set_probe_enabled(struct nvme_global_ctx *ctx, bool enabled)
159-
{
160-
ctx->mi_probe_enabled = enabled;
161-
}
162-
163102
static void nvme_mi_record_resp_time(struct nvme_mi_ep *ep)
164103
{
165104
int rc;

libnvme/src/nvme/mi.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -651,38 +651,6 @@ struct nvme_mi_control_resp {
651651
*/
652652
const char *nvme_mi_status_to_string(int status);
653653

654-
/**
655-
* nvme_mi_create_global_ctx() - Create top-level MI (ctx) handle.
656-
* @fp: File descriptor for logging messages
657-
* @log_level: Logging level to use
658-
*
659-
* Create the top-level (library) handle for creating subsequent endpoint
660-
* objects. Similar to nvme_create_global_ctx(), but we provide this to
661-
* allow linking without the core libnvme.
662-
*
663-
* Return: new nvme_global_ctx object, or NULL on failure.
664-
*
665-
* See &nvme_create_global_ctx.
666-
*/
667-
struct nvme_global_ctx *nvme_mi_create_global_ctx(FILE *fp, int log_level);
668-
669-
/**
670-
* nvme_mi_free_global_ctx() - Free nvme_global_ctx object.
671-
* @ctx: &struct nvme_global_ctx object
672-
*/
673-
void nvme_mi_free_global_ctx(struct nvme_global_ctx *ctx);
674-
675-
/**
676-
* nvme_mi_set_probe_enabled() - enable/disable the probe for new endpoints
677-
* @ctx: &struct nvme_global_ctx object
678-
* @enabled: whether to probe new endpoints
679-
*
680-
* Controls whether newly-created endpoints are probed for quirks on creation.
681-
* Defaults to enabled, which results in some initial messaging with the
682-
* endpoint to determine model-specific details.
683-
*/
684-
void nvme_mi_set_probe_enabled(struct nvme_global_ctx *ctx, bool enabled);
685-
686654
/* Top level management object: NVMe-MI Management Endpoint */
687655
struct nvme_mi_ep;
688656

libnvme/test/mi-mctp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ int main(void)
14611461

14621462
__nvme_mi_mctp_set_ops(&ops);
14631463

1464-
ctx = nvme_mi_create_global_ctx(fd, DEFAULT_LOGLEVEL);
1464+
ctx = nvme_create_global_ctx(fd, DEFAULT_LOGLEVEL);
14651465
assert(ctx);
14661466

14671467
ep = nvme_mi_open_mctp(ctx, 0, 0);
@@ -1473,7 +1473,7 @@ int main(void)
14731473
}
14741474

14751475
nvme_mi_close(ep);
1476-
nvme_mi_free_global_ctx(ctx);
1476+
nvme_free_global_ctx(ctx);
14771477

14781478
test_close_log(fd);
14791479

libnvme/test/mi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ int main(void)
21312131

21322132
fd = test_setup_log();
21332133

2134-
ctx = nvme_mi_create_global_ctx(fd, DEFAULT_LOGLEVEL);
2134+
ctx = nvme_create_global_ctx(fd, DEFAULT_LOGLEVEL);
21352135
assert(ctx);
21362136

21372137
ep = nvme_mi_open_test(ctx);
@@ -2142,7 +2142,7 @@ int main(void)
21422142
}
21432143

21442144
nvme_mi_close(ep);
2145-
nvme_mi_free_global_ctx(ctx);
2145+
nvme_free_global_ctx(ctx);
21462146

21472147
test_close_log(fd);
21482148

0 commit comments

Comments
 (0)