Skip to content

Commit ef90bc1

Browse files
committed
examples/mi-mctp: Add subcommand support
A future change will introduce further mi commands, and admin channel commands, so we'll want a way to invoke mi-mctp to perform these additional commands. This change introduces an <action> argument, which currently defaults to the existing 'info' action. Signed-off-by: Jeremy Kerr <[email protected]>
1 parent 9bd757c commit ef90bc1

1 file changed

Lines changed: 66 additions & 26 deletions

File tree

examples/mi-mctp.c

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <err.h>
1414
#include <stdio.h>
1515
#include <stdlib.h>
16+
#include <string.h>
1617

1718
#include <libnvme-mi.h>
1819

@@ -79,36 +80,17 @@ static int show_port(nvme_mi_ep_t ep, int portid)
7980
return 0;
8081
}
8182

82-
int main(int argc, char **argv)
83+
int do_info(nvme_mi_ep_t ep)
8384
{
8485
struct nvme_mi_nvm_ss_health_status ss_health;
8586
struct nvme_mi_read_nvm_ss_info ss_info;
86-
nvme_root_t root;
87-
nvme_mi_ep_t ep;
88-
uint8_t eid;
89-
int net;
90-
int rc;
91-
int i;
92-
93-
if (argc != 3) {
94-
fprintf(stderr, "usage: %s <net> <eid>\n", argv[0]);
95-
return EXIT_FAILURE;
96-
}
97-
98-
net = atoi(argv[1]);
99-
eid = atoi(argv[2]) & 0xff;
100-
101-
root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL);
102-
if (!root)
103-
err(EXIT_FAILURE, "can't create NVMe root");
104-
105-
ep = nvme_mi_open_mctp(root, net, eid);
106-
if (!ep)
107-
err(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid);
87+
int i, rc;
10888

10989
rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
110-
if (rc)
111-
err(EXIT_FAILURE, "can't perform Read MI Data operation");
90+
if (rc) {
91+
warn("can't perform Read MI Data operation");
92+
return -1;
93+
}
11294

11395
printf("NVMe MI subsys info:\n");
11496
printf(" num ports: %d\n", ss_info.nump + 1);
@@ -130,11 +112,69 @@ int main(int argc, char **argv)
130112
printf(" drive life used: %d%%\n", ss_health.pdlu);
131113
printf(" controller status: 0x%04x\n", le16_to_cpu(ss_health.pdlu));
132114

115+
return 0;
116+
}
117+
118+
enum action {
119+
ACTION_INFO,
120+
};
121+
122+
int main(int argc, char **argv)
123+
{
124+
enum action action;
125+
nvme_root_t root;
126+
nvme_mi_ep_t ep;
127+
uint8_t eid;
128+
int rc, net;
129+
130+
if (argc < 3) {
131+
fprintf(stderr,
132+
"usage: %s <net> <eid> [action] [action args]\n",
133+
argv[0]);
134+
fprintf(stderr, "where action is:\n"
135+
" info\n");
136+
return EXIT_FAILURE;
137+
}
138+
139+
net = atoi(argv[1]);
140+
eid = atoi(argv[2]) & 0xff;
141+
argv += 2;
142+
argc -= 2;
143+
144+
if (argc == 1) {
145+
action = ACTION_INFO;
146+
} else {
147+
char *action_str = argv[1];
148+
argc--;
149+
argv++;
150+
151+
if (!strcmp(action_str, "info")) {
152+
action = ACTION_INFO;
153+
} else {
154+
fprintf(stderr, "invalid action '%s'\n", action_str);
155+
return EXIT_FAILURE;
156+
}
157+
}
158+
159+
root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL);
160+
if (!root)
161+
err(EXIT_FAILURE, "can't create NVMe root");
162+
163+
ep = nvme_mi_open_mctp(root, net, eid);
164+
if (!ep)
165+
err(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid);
166+
167+
switch (action) {
168+
case ACTION_INFO:
169+
rc = do_info(ep);
170+
break;
171+
}
172+
133173
nvme_mi_close(ep);
134174

135175
nvme_mi_free_root(root);
136176

137-
return EXIT_SUCCESS;
177+
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
138178
}
139179

140180

0 commit comments

Comments
 (0)