Print this page
8480 nvmeadm should ignore 0-sized namespaces by default
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/nvmeadm/nvmeadm.c
+++ new/usr/src/cmd/nvmeadm/nvmeadm.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2016 Nexenta Systems, Inc.
14 14 */
15 15
16 16 /*
17 17 * nvmeadm -- NVMe administration utility
18 18 *
19 19 * nvmeadm [-v] [-d] [-h] <command> [<ctl>[/<ns>][,...]] [args]
20 20 * commands: list
21 21 * identify
22 22 * get-logpage <logpage name>
23 23 * get-features <feature>[,...]
24 24 * format ...
25 25 * secure-erase ...
26 26 * detach ...
27 27 * attach ...
28 28 * get-param ...
29 29 * set-param ...
30 30 * load-firmware ...
31 31 * activate-firmware ...
32 32 * write-uncorrectable ...
33 33 * compare ...
34 34 * compare-and-write ...
35 35 */
36 36
37 37 #include <stdio.h>
38 38 #include <stdlib.h>
39 39 #include <strings.h>
40 40 #include <ctype.h>
41 41 #include <err.h>
42 42 #include <sys/sunddi.h>
43 43 #include <libdevinfo.h>
44 44
45 45 #include <sys/nvme.h>
46 46
47 47 #include "nvmeadm.h"
48 48
49 49 typedef struct nvme_process_arg nvme_process_arg_t;
50 50 typedef struct nvme_feature nvme_feature_t;
51 51 typedef struct nvmeadm_cmd nvmeadm_cmd_t;
52 52
53 53 struct nvme_process_arg {
54 54 int npa_argc;
55 55 char **npa_argv;
56 56 char *npa_name;
57 57 uint32_t npa_nsid;
58 58 boolean_t npa_isns;
59 59 const nvmeadm_cmd_t *npa_cmd;
60 60 di_node_t npa_node;
61 61 di_minor_t npa_minor;
62 62 char *npa_path;
63 63 char *npa_dsk;
64 64 nvme_identify_ctrl_t *npa_idctl;
65 65 nvme_identify_nsid_t *npa_idns;
66 66 nvme_version_t *npa_version;
67 67 };
68 68
69 69 struct nvme_feature {
70 70 char *f_name;
71 71 char *f_short;
72 72 uint8_t f_feature;
73 73 size_t f_bufsize;
74 74 uint_t f_getflags;
75 75 int (*f_get)(int, const nvme_feature_t *, nvme_identify_ctrl_t *);
76 76 void (*f_print)(uint64_t, void *, size_t, nvme_identify_ctrl_t *);
77 77 };
78 78
79 79 #define NVMEADM_CTRL 1
80 80 #define NVMEADM_NS 2
81 81 #define NVMEADM_BOTH (NVMEADM_CTRL | NVMEADM_NS)
82 82
83 83 struct nvmeadm_cmd {
84 84 char *c_name;
85 85 char *c_desc;
86 86 char *c_flagdesc;
87 87 int (*c_func)(int, const nvme_process_arg_t *);
88 88 void (*c_usage)(const char *);
89 89 boolean_t c_multi;
90 90 };
91 91
92 92
93 93 static void usage(const nvmeadm_cmd_t *);
94 94 static void nvme_walk(nvme_process_arg_t *, di_node_t);
95 95 static boolean_t nvme_match(nvme_process_arg_t *);
96 96
97 97 static int nvme_process(di_node_t, di_minor_t, void *);
98 98
99 99 static int do_list(int, const nvme_process_arg_t *);
100 100 static int do_identify(int, const nvme_process_arg_t *);
101 101 static int do_get_logpage_error(int, const nvme_process_arg_t *);
102 102 static int do_get_logpage_health(int, const nvme_process_arg_t *);
103 103 static int do_get_logpage_fwslot(int, const nvme_process_arg_t *);
104 104 static int do_get_logpage(int, const nvme_process_arg_t *);
105 105 static int do_get_feat_common(int, const nvme_feature_t *,
106 106 nvme_identify_ctrl_t *);
107 107 static int do_get_feat_intr_vect(int, const nvme_feature_t *,
108 108 nvme_identify_ctrl_t *);
109 109 static int do_get_features(int, const nvme_process_arg_t *);
110 110 static int do_format(int, const nvme_process_arg_t *);
111 111 static int do_secure_erase(int, const nvme_process_arg_t *);
112 112 static int do_attach_detach(int, const nvme_process_arg_t *);
113 113
114 114 static void usage_list(const char *);
115 115 static void usage_identify(const char *);
116 116 static void usage_get_logpage(const char *);
117 117 static void usage_get_features(const char *);
118 118 static void usage_format(const char *);
119 119 static void usage_secure_erase(const char *);
120 120 static void usage_attach_detach(const char *);
121 121
122 122 int verbose;
123 123 int debug;
124 124 int found;
125 125 static int exitcode;
126 126
127 127 static const nvmeadm_cmd_t nvmeadm_cmds[] = {
128 128 {
129 129 "list",
130 130 "list controllers and namespaces",
131 131 NULL,
132 132 do_list, usage_list, B_TRUE
133 133 },
134 134 {
135 135 "identify",
136 136 "identify controllers and/or namespaces",
137 137 NULL,
138 138 do_identify, usage_identify, B_TRUE
139 139 },
140 140 {
141 141 "get-logpage",
142 142 "get a log page from controllers and/or namespaces",
143 143 NULL,
144 144 do_get_logpage, usage_get_logpage, B_TRUE
145 145 },
146 146 {
147 147 "get-features",
148 148 "get features from controllers and/or namespaces",
149 149 NULL,
150 150 do_get_features, usage_get_features, B_TRUE
151 151 },
152 152 {
153 153 "format",
154 154 "format namespace(s) of a controller",
155 155 NULL,
156 156 do_format, usage_format, B_FALSE
157 157 },
158 158 {
159 159 "secure-erase",
160 160 "secure erase namespace(s) of a controller",
161 161 " -c Do a cryptographic erase.",
162 162 do_secure_erase, usage_secure_erase, B_FALSE
163 163 },
164 164 {
165 165 "detach",
166 166 "detach blkdev(7d) from namespace(s) of a controller",
167 167 NULL,
168 168 do_attach_detach, usage_attach_detach, B_FALSE
169 169 },
170 170 {
171 171 "attach",
172 172 "attach blkdev(7d) to namespace(s) of a controller",
173 173 NULL,
174 174 do_attach_detach, usage_attach_detach, B_FALSE
175 175 },
176 176 {
177 177 NULL, NULL, NULL,
178 178 NULL, NULL, B_FALSE
179 179 }
180 180 };
181 181
182 182 static const nvme_feature_t features[] = {
183 183 { "Arbitration", "",
184 184 NVME_FEAT_ARBITRATION, 0, NVMEADM_CTRL,
185 185 do_get_feat_common, nvme_print_feat_arbitration },
186 186 { "Power Management", "",
187 187 NVME_FEAT_POWER_MGMT, 0, NVMEADM_CTRL,
188 188 do_get_feat_common, nvme_print_feat_power_mgmt },
189 189 { "LBA Range Type", "range",
190 190 NVME_FEAT_LBA_RANGE, NVME_LBA_RANGE_BUFSIZE, NVMEADM_NS,
191 191 do_get_feat_common, nvme_print_feat_lba_range },
192 192 { "Temperature Threshold", "",
193 193 NVME_FEAT_TEMPERATURE, 0, NVMEADM_CTRL,
194 194 do_get_feat_common, nvme_print_feat_temperature },
195 195 { "Error Recovery", "",
196 196 NVME_FEAT_ERROR, 0, NVMEADM_CTRL,
197 197 do_get_feat_common, nvme_print_feat_error },
198 198 { "Volatile Write Cache", "cache",
199 199 NVME_FEAT_WRITE_CACHE, 0, NVMEADM_CTRL,
200 200 do_get_feat_common, nvme_print_feat_write_cache },
201 201 { "Number of Queues", "queues",
202 202 NVME_FEAT_NQUEUES, 0, NVMEADM_CTRL,
203 203 do_get_feat_common, nvme_print_feat_nqueues },
204 204 { "Interrupt Coalescing", "coalescing",
205 205 NVME_FEAT_INTR_COAL, 0, NVMEADM_CTRL,
206 206 do_get_feat_common, nvme_print_feat_intr_coal },
207 207 { "Interrupt Vector Configuration", "vector",
208 208 NVME_FEAT_INTR_VECT, 0, NVMEADM_CTRL,
209 209 do_get_feat_intr_vect, nvme_print_feat_intr_vect },
210 210 { "Write Atomicity", "atomicity",
211 211 NVME_FEAT_WRITE_ATOM, 0, NVMEADM_CTRL,
212 212 do_get_feat_common, nvme_print_feat_write_atom },
213 213 { "Asynchronous Event Configuration", "event",
214 214 NVME_FEAT_ASYNC_EVENT, 0, NVMEADM_CTRL,
215 215 do_get_feat_common, nvme_print_feat_async_event },
216 216 { "Autonomous Power State Transition", "",
217 217 NVME_FEAT_AUTO_PST, NVME_AUTO_PST_BUFSIZE, NVMEADM_CTRL,
218 218 do_get_feat_common, nvme_print_feat_auto_pst },
219 219 { "Software Progress Marker", "progress",
220 220 NVME_FEAT_PROGRESS, 0, NVMEADM_CTRL,
221 221 do_get_feat_common, nvme_print_feat_progress },
222 222 { NULL, NULL, 0, 0, B_FALSE, NULL }
223 223 };
224 224
225 225
226 226 int
227 227 main(int argc, char **argv)
228 228 {
229 229 int c;
230 230 extern int optind;
231 231 const nvmeadm_cmd_t *cmd;
232 232 di_node_t node;
233 233 nvme_process_arg_t npa = { 0 };
234 234 int help = 0;
235 235 char *tmp, *lasts = NULL;
236 236
237 237 while ((c = getopt(argc, argv, "dhv")) != -1) {
238 238 switch (c) {
239 239 case 'd':
240 240 debug++;
241 241 break;
242 242 case 'v':
243 243 verbose++;
244 244 break;
245 245 case 'h':
246 246 help++;
247 247 break;
248 248 case '?':
249 249 usage(NULL);
250 250 exit(-1);
251 251 }
252 252 }
253 253
254 254 if (optind == argc) {
255 255 usage(NULL);
256 256 if (help)
257 257 exit(0);
258 258 else
259 259 exit(-1);
260 260 }
261 261
262 262 /* Look up the specified command in the command table. */
263 263 for (cmd = &nvmeadm_cmds[0]; cmd->c_name != NULL; cmd++)
264 264 if (strcmp(cmd->c_name, argv[optind]) == 0)
265 265 break;
266 266
267 267 if (cmd->c_name == NULL) {
268 268 usage(NULL);
269 269 exit(-1);
270 270 }
271 271
272 272 if (help) {
273 273 usage(cmd);
274 274 exit(0);
275 275 }
276 276
277 277 npa.npa_cmd = cmd;
278 278
279 279 optind++;
280 280
281 281 /*
282 282 * All commands but "list" require a ctl/ns argument.
283 283 */
284 284 if ((optind == argc || (strncmp(argv[optind], "nvme", 4) != 0)) &&
285 285 cmd->c_func != do_list) {
286 286 warnx("missing controller/namespace name");
287 287 usage(cmd);
288 288 exit(-1);
289 289 }
290 290
291 291
292 292 /* Store the remaining arguments for use by the command. */
293 293 npa.npa_argc = argc - optind - 1;
294 294 npa.npa_argv = &argv[optind + 1];
295 295
296 296 /*
297 297 * Make sure we're not running commands on multiple controllers that
298 298 * aren't allowed to do that.
299 299 */
300 300 if (argv[optind] != NULL && strchr(argv[optind], ',') != NULL &&
301 301 cmd->c_multi == B_FALSE) {
302 302 warnx("%s not allowed on multiple controllers",
303 303 cmd->c_name);
304 304 usage(cmd);
305 305 exit(-1);
306 306 }
307 307
308 308 /*
309 309 * Get controller/namespace arguments and run command.
310 310 */
311 311 npa.npa_name = strtok_r(argv[optind], ",", &lasts);
312 312 do {
313 313 if (npa.npa_name != NULL) {
314 314 tmp = strchr(npa.npa_name, '/');
315 315 if (tmp != NULL) {
316 316 unsigned long nsid;
317 317 *tmp++ = '\0';
318 318 errno = 0;
319 319 nsid = strtoul(tmp, NULL, 10);
320 320 if (nsid >= UINT32_MAX || errno != 0) {
321 321 warn("invalid namespace %s", tmp);
322 322 exitcode--;
323 323 continue;
324 324 }
325 325 if (nsid == 0) {
326 326 warnx("invalid namespace %s", tmp);
327 327 exitcode--;
328 328 continue;
329 329 }
330 330 npa.npa_nsid = nsid;
331 331 npa.npa_isns = B_TRUE;
332 332 }
333 333 }
334 334
335 335 if ((node = di_init("/", DINFOSUBTREE | DINFOMINOR)) == NULL)
336 336 err(-1, "failed to initialize libdevinfo");
337 337 nvme_walk(&npa, node);
338 338 di_fini(node);
339 339
340 340 if (found == 0) {
341 341 if (npa.npa_name != NULL) {
342 342 warnx("%s%.*s%.*d: no such controller or "
343 343 "namespace", npa.npa_name,
344 344 npa.npa_nsid > 0 ? -1 : 0, "/",
345 345 npa.npa_nsid > 0 ? -1 : 0, npa.npa_nsid);
346 346 } else {
347 347 warnx("no controllers found");
348 348 }
349 349 exitcode--;
350 350 }
351 351 found = 0;
352 352 npa.npa_name = strtok_r(NULL, ",", &lasts);
353 353 } while (npa.npa_name != NULL);
354 354
355 355 exit(exitcode);
356 356 }
357 357
358 358 static void
359 359 usage(const nvmeadm_cmd_t *cmd)
360 360 {
361 361 (void) fprintf(stderr, "usage:\n");
362 362 (void) fprintf(stderr, " %s -h %s\n", getprogname(),
363 363 cmd != NULL ? cmd->c_name : "[<command>]");
364 364 (void) fprintf(stderr, " %s [-dv] ", getprogname());
365 365
366 366 if (cmd != NULL) {
367 367 cmd->c_usage(cmd->c_name);
368 368 } else {
369 369 (void) fprintf(stderr,
370 370 "<command> <ctl>[/<ns>][,...] [<args>]\n");
371 371 (void) fprintf(stderr,
372 372 "\n Manage NVMe controllers and namespaces.\n");
373 373 (void) fprintf(stderr, "\ncommands:\n");
374 374
375 375 for (cmd = &nvmeadm_cmds[0]; cmd->c_name != NULL; cmd++)
376 376 (void) fprintf(stderr, " %-15s - %s\n",
377 377 cmd->c_name, cmd->c_desc);
378 378 }
379 379 (void) fprintf(stderr, "\nflags:\n"
380 380 " -h print usage information\n"
381 381 " -d print information useful for debugging %s\n"
382 382 " -v print verbose information\n", getprogname());
383 383 if (cmd != NULL && cmd->c_flagdesc != NULL)
384 384 (void) fprintf(stderr, "%s\n", cmd->c_flagdesc);
385 385 }
386 386
387 387 static boolean_t
388 388 nvme_match(nvme_process_arg_t *npa)
389 389 {
390 390 char *name;
391 391 uint32_t nsid = 0;
392 392
393 393 if (npa->npa_name == NULL)
394 394 return (B_TRUE);
395 395
396 396 if (asprintf(&name, "%s%d", di_driver_name(npa->npa_node),
397 397 di_instance(npa->npa_node)) < 0)
398 398 err(-1, "nvme_match()");
399 399
400 400 if (strcmp(name, npa->npa_name) != 0) {
401 401 free(name);
402 402 return (B_FALSE);
403 403 }
404 404
405 405 free(name);
406 406
407 407 if (npa->npa_isns) {
408 408 if (npa->npa_nsid == 0)
409 409 return (B_TRUE);
410 410 nsid = strtoul(di_minor_name(npa->npa_minor), NULL, 10);
411 411 }
412 412
413 413 if (npa->npa_isns && npa->npa_nsid != nsid)
414 414 return (B_FALSE);
415 415
416 416 return (B_TRUE);
417 417 }
418 418
419 419 char *
420 420 nvme_dskname(const nvme_process_arg_t *npa)
421 421 {
422 422 char *path = NULL;
423 423 di_node_t child;
424 424 di_dim_t dim;
425 425 char *addr;
426 426
427 427 dim = di_dim_init();
428 428
429 429 for (child = di_child_node(npa->npa_node);
430 430 child != DI_NODE_NIL;
431 431 child = di_sibling_node(child)) {
432 432 addr = di_bus_addr(child);
433 433 if (addr == NULL)
434 434 continue;
435 435
436 436 if (addr[0] == 'w')
437 437 addr++;
438 438
439 439 if (strncasecmp(addr, di_minor_name(npa->npa_minor),
440 440 strchrnul(addr, ',') - addr) != 0)
441 441 continue;
442 442
443 443 path = di_dim_path_dev(dim, di_driver_name(child),
444 444 di_instance(child), "c");
445 445
446 446 if (path != NULL) {
447 447 path[strlen(path) - 2] = '\0';
448 448 path = strrchr(path, '/') + 1;
449 449 if (path != NULL) {
450 450 path = strdup(path);
451 451 if (path == NULL)
452 452 err(-1, "nvme_dskname");
453 453 }
454 454 }
455 455
456 456 break;
457 457 }
458 458
459 459 di_dim_fini(dim);
460 460 return (path);
461 461 }
462 462
463 463 static int
464 464 nvme_process(di_node_t node, di_minor_t minor, void *arg)
465 465 {
466 466 nvme_process_arg_t *npa = arg;
467 467 int fd;
468 468
469 469 npa->npa_node = node;
470 470 npa->npa_minor = minor;
471 471
472 472 if (!nvme_match(npa))
473 473 return (DI_WALK_CONTINUE);
474 474
475 475 if ((fd = nvme_open(minor)) < 0)
476 476 return (DI_WALK_CONTINUE);
477 477
478 478 found++;
479 479
480 480 npa->npa_path = di_devfs_path(node);
481 481 if (npa->npa_path == NULL)
482 482 goto out;
483 483
484 484 npa->npa_version = nvme_version(fd);
485 485 if (npa->npa_version == NULL)
486 486 goto out;
487 487
488 488 npa->npa_idctl = nvme_identify_ctrl(fd);
489 489 if (npa->npa_idctl == NULL)
490 490 goto out;
491 491
492 492 npa->npa_idns = nvme_identify_nsid(fd);
493 493 if (npa->npa_idns == NULL)
494 494 goto out;
495 495
496 496 if (npa->npa_isns)
497 497 npa->npa_dsk = nvme_dskname(npa);
498 498
499 499 exitcode += npa->npa_cmd->c_func(fd, npa);
500 500
501 501 out:
502 502 di_devfs_path_free(npa->npa_path);
503 503 free(npa->npa_dsk);
504 504 free(npa->npa_version);
505 505 free(npa->npa_idctl);
506 506 free(npa->npa_idns);
507 507
508 508 npa->npa_version = NULL;
509 509 npa->npa_idctl = NULL;
510 510 npa->npa_idns = NULL;
511 511
512 512 nvme_close(fd);
513 513
514 514 return (DI_WALK_CONTINUE);
515 515 }
516 516
517 517 static void
518 518 nvme_walk(nvme_process_arg_t *npa, di_node_t node)
519 519 {
520 520 char *minor_nodetype = DDI_NT_NVME_NEXUS;
521 521
522 522 if (npa->npa_isns)
523 523 minor_nodetype = DDI_NT_NVME_ATTACHMENT_POINT;
524 524
525 525 (void) di_walk_minor(node, minor_nodetype, 0, npa, nvme_process);
526 526 }
527 527
528 528 static void
529 529 usage_list(const char *c_name)
530 530 {
531 531 (void) fprintf(stderr, "%s [<ctl>[/<ns>][,...]\n\n"
↓ open down ↓ |
531 lines elided |
↑ open up ↑ |
532 532 " List NVMe controllers and their namespaces. If no "
533 533 "controllers and/or name-\n spaces are specified, all "
534 534 "controllers and namespaces in the system will be\n "
535 535 "listed.\n", c_name);
536 536 }
537 537
538 538 static int
539 539 do_list_nsid(int fd, const nvme_process_arg_t *npa)
540 540 {
541 541 _NOTE(ARGUNUSED(fd));
542 + const uint_t format = npa->npa_idns->id_flbas.lba_format;
543 + const uint_t bshift = npa->npa_idns->id_lbaf[format].lbaf_lbads;
542 544
545 + /*
546 + * Some devices have extra namespaces with illegal block sizes and
547 + * zero blocks. Don't list them when verbose operation isn't requested.
548 + */
549 + if ((bshift < 9 || npa->npa_idns->id_nsize == 0) && verbose == 0)
550 + return (0);
551 +
543 552 (void) printf(" %s/%s (%s): ", npa->npa_name,
544 553 di_minor_name(npa->npa_minor),
545 554 npa->npa_dsk != NULL ? npa->npa_dsk : "unattached");
546 555 nvme_print_nsid_summary(npa->npa_idns);
547 556
548 557 return (0);
549 558 }
550 559
551 560 static int
552 561 do_list(int fd, const nvme_process_arg_t *npa)
553 562 {
554 563 _NOTE(ARGUNUSED(fd));
555 564
556 565 nvme_process_arg_t ns_npa = { 0 };
557 566 nvmeadm_cmd_t cmd = { 0 };
558 567 char *name;
559 568
560 569 if (asprintf(&name, "%s%d", di_driver_name(npa->npa_node),
561 570 di_instance(npa->npa_node)) < 0)
562 571 err(-1, "do_list()");
563 572
564 573 (void) printf("%s: ", name);
565 574 nvme_print_ctrl_summary(npa->npa_idctl, npa->npa_version);
566 575
567 576 ns_npa.npa_name = name;
568 577 ns_npa.npa_isns = B_TRUE;
569 578 ns_npa.npa_nsid = npa->npa_nsid;
570 579 cmd = *(npa->npa_cmd);
571 580 cmd.c_func = do_list_nsid;
572 581 ns_npa.npa_cmd = &cmd;
573 582
574 583 nvme_walk(&ns_npa, npa->npa_node);
575 584
576 585 free(name);
577 586
578 587 return (exitcode);
579 588 }
580 589
581 590 static void
582 591 usage_identify(const char *c_name)
583 592 {
584 593 (void) fprintf(stderr, "%s <ctl>[/<ns>][,...]\n\n"
585 594 " Print detailed information about the specified NVMe "
586 595 "controllers and/or name-\n spaces.\n", c_name);
587 596 }
588 597
589 598 static int
590 599 do_identify(int fd, const nvme_process_arg_t *npa)
591 600 {
592 601 if (npa->npa_nsid == 0) {
593 602 nvme_capabilities_t *cap;
594 603
595 604 cap = nvme_capabilities(fd);
596 605 if (cap == NULL)
597 606 return (-1);
598 607
599 608 (void) printf("%s: ", npa->npa_name);
600 609 nvme_print_identify_ctrl(npa->npa_idctl, cap,
601 610 npa->npa_version);
602 611
603 612 free(cap);
604 613 } else {
605 614 (void) printf("%s/%s: ", npa->npa_name,
606 615 di_minor_name(npa->npa_minor));
607 616 nvme_print_identify_nsid(npa->npa_idns,
608 617 npa->npa_version);
609 618 }
610 619
611 620 return (0);
612 621 }
613 622
614 623 static void
615 624 usage_get_logpage(const char *c_name)
616 625 {
617 626 (void) fprintf(stderr, "%s <ctl>[/<ns>][,...] <logpage>\n\n"
618 627 " Print the specified log page of the specified NVMe "
619 628 "controllers and/or name-\n spaces. Supported log pages "
620 629 "are error, health, and firmware.\n", c_name);
621 630 }
622 631
623 632 static int
624 633 do_get_logpage_error(int fd, const nvme_process_arg_t *npa)
625 634 {
626 635 int nlog = npa->npa_idctl->id_elpe + 1;
627 636 size_t bufsize = sizeof (nvme_error_log_entry_t) * nlog;
628 637 nvme_error_log_entry_t *elog;
629 638
630 639 if (npa->npa_nsid != 0)
631 640 errx(-1, "Error Log not available on a per-namespace basis");
632 641
633 642 elog = nvme_get_logpage(fd, NVME_LOGPAGE_ERROR, &bufsize);
634 643
635 644 if (elog == NULL)
636 645 return (-1);
637 646
638 647 nlog = bufsize / sizeof (nvme_error_log_entry_t);
639 648
640 649 (void) printf("%s: ", npa->npa_name);
641 650 nvme_print_error_log(nlog, elog);
642 651
643 652 free(elog);
644 653
645 654 return (0);
646 655 }
647 656
648 657 static int
649 658 do_get_logpage_health(int fd, const nvme_process_arg_t *npa)
650 659 {
651 660 size_t bufsize = sizeof (nvme_health_log_t);
652 661 nvme_health_log_t *hlog;
653 662
654 663 if (npa->npa_nsid != 0) {
655 664 if (npa->npa_idctl->id_lpa.lp_smart == 0)
656 665 errx(-1, "SMART/Health information not available "
657 666 "on a per-namespace basis on this controller");
658 667 }
659 668
660 669 hlog = nvme_get_logpage(fd, NVME_LOGPAGE_HEALTH, &bufsize);
661 670
662 671 if (hlog == NULL)
663 672 return (-1);
664 673
665 674 (void) printf("%s: ", npa->npa_name);
666 675 nvme_print_health_log(hlog, npa->npa_idctl);
667 676
668 677 free(hlog);
669 678
670 679 return (0);
671 680 }
672 681
673 682 static int
674 683 do_get_logpage_fwslot(int fd, const nvme_process_arg_t *npa)
675 684 {
676 685 size_t bufsize = sizeof (nvme_fwslot_log_t);
677 686 nvme_fwslot_log_t *fwlog;
678 687
679 688 if (npa->npa_nsid != 0)
680 689 errx(-1, "Firmware Slot information not available on a "
681 690 "per-namespace basis");
682 691
683 692 fwlog = nvme_get_logpage(fd, NVME_LOGPAGE_FWSLOT, &bufsize);
684 693
685 694 if (fwlog == NULL)
686 695 return (-1);
687 696
688 697 (void) printf("%s: ", npa->npa_name);
689 698 nvme_print_fwslot_log(fwlog);
690 699
691 700 free(fwlog);
692 701
693 702 return (0);
694 703 }
695 704
696 705 static int
697 706 do_get_logpage(int fd, const nvme_process_arg_t *npa)
698 707 {
699 708 int ret = 0;
700 709 int (*func)(int, const nvme_process_arg_t *);
701 710
702 711 if (npa->npa_argc < 1) {
703 712 warnx("missing logpage name");
704 713 usage(npa->npa_cmd);
705 714 exit(-1);
706 715 }
707 716
708 717 if (strcmp(npa->npa_argv[0], "error") == 0)
709 718 func = do_get_logpage_error;
710 719 else if (strcmp(npa->npa_argv[0], "health") == 0)
711 720 func = do_get_logpage_health;
712 721 else if (strcmp(npa->npa_argv[0], "firmware") == 0)
713 722 func = do_get_logpage_fwslot;
714 723 else
715 724 errx(-1, "invalid log page: %s", npa->npa_argv[0]);
716 725
717 726 ret = func(fd, npa);
718 727 return (ret);
719 728 }
720 729
721 730 static void
722 731 usage_get_features(const char *c_name)
723 732 {
724 733 const nvme_feature_t *feat;
725 734
726 735 (void) fprintf(stderr, "%s <ctl>[/<ns>][,...] [<feature>[,...]]\n\n"
727 736 " Print the specified features of the specified NVMe controllers "
728 737 "and/or\n namespaces. Supported features are:\n\n", c_name);
729 738 (void) fprintf(stderr, " %-35s %-14s %s\n",
730 739 "FEATURE NAME", "SHORT NAME", "CONTROLLER/NAMESPACE");
731 740 for (feat = &features[0]; feat->f_feature != 0; feat++) {
732 741 char *type;
733 742
734 743 if ((feat->f_getflags & NVMEADM_BOTH) == NVMEADM_BOTH)
735 744 type = "both";
736 745 else if ((feat->f_getflags & NVMEADM_CTRL) != 0)
737 746 type = "controller only";
738 747 else
739 748 type = "namespace only";
740 749
741 750 (void) fprintf(stderr, " %-35s %-14s %s\n",
742 751 feat->f_name, feat->f_short, type);
743 752 }
744 753
745 754 }
746 755
747 756 static int
748 757 do_get_feat_common(int fd, const nvme_feature_t *feat,
749 758 nvme_identify_ctrl_t *idctl)
750 759 {
751 760 void *buf = NULL;
752 761 size_t bufsize = feat->f_bufsize;
753 762 uint64_t res;
754 763
755 764 if (nvme_get_feature(fd, feat->f_feature, 0, &res, &bufsize, &buf)
756 765 == B_FALSE)
757 766 return (EINVAL);
758 767
759 768 nvme_print(2, feat->f_name, -1, NULL);
760 769 feat->f_print(res, buf, bufsize, idctl);
761 770 free(buf);
762 771
763 772 return (0);
764 773 }
765 774
766 775 static int
767 776 do_get_feat_intr_vect(int fd, const nvme_feature_t *feat,
768 777 nvme_identify_ctrl_t *idctl)
769 778 {
770 779 uint64_t res;
771 780 uint64_t arg;
772 781 int intr_cnt;
773 782
774 783 intr_cnt = nvme_intr_cnt(fd);
775 784
776 785 if (intr_cnt == -1)
777 786 return (EINVAL);
778 787
779 788 nvme_print(2, feat->f_name, -1, NULL);
780 789
781 790 for (arg = 0; arg < intr_cnt; arg++) {
782 791 if (nvme_get_feature(fd, feat->f_feature, arg, &res, NULL, NULL)
783 792 == B_FALSE)
784 793 return (EINVAL);
785 794
786 795 feat->f_print(res, NULL, 0, idctl);
787 796 }
788 797
789 798 return (0);
790 799 }
791 800
792 801 static int
793 802 do_get_features(int fd, const nvme_process_arg_t *npa)
794 803 {
795 804 const nvme_feature_t *feat;
796 805 char *f, *flist, *lasts;
797 806 boolean_t header_printed = B_FALSE;
798 807
799 808 if (npa->npa_argc > 1)
800 809 errx(-1, "unexpected arguments");
801 810
802 811 /*
803 812 * No feature list given, print all supported features.
804 813 */
805 814 if (npa->npa_argc == 0) {
806 815 (void) printf("%s: Get Features\n", npa->npa_name);
807 816 for (feat = &features[0]; feat->f_feature != 0; feat++) {
808 817 if ((npa->npa_nsid != 0 &&
809 818 (feat->f_getflags & NVMEADM_NS) == 0) ||
810 819 (npa->npa_nsid == 0 &&
811 820 (feat->f_getflags & NVMEADM_CTRL) == 0))
812 821 continue;
813 822
814 823 (void) feat->f_get(fd, feat, npa->npa_idctl);
815 824 }
816 825
817 826 return (0);
818 827 }
819 828
820 829 /*
821 830 * Process feature list.
822 831 */
823 832 flist = strdup(npa->npa_argv[0]);
824 833 if (flist == NULL)
825 834 err(-1, "do_get_features");
826 835
827 836 for (f = strtok_r(flist, ",", &lasts);
828 837 f != NULL;
829 838 f = strtok_r(NULL, ",", &lasts)) {
830 839 while (isspace(*f))
831 840 f++;
832 841
833 842 for (feat = &features[0]; feat->f_feature != 0; feat++) {
834 843 if (strncasecmp(feat->f_name, f, strlen(f)) == 0 ||
835 844 strncasecmp(feat->f_short, f, strlen(f)) == 0)
836 845 break;
837 846 }
838 847
839 848 if (feat->f_feature == 0) {
840 849 warnx("unknown feature %s", f);
841 850 continue;
842 851 }
843 852
844 853 if ((npa->npa_nsid != 0 &&
845 854 (feat->f_getflags & NVMEADM_NS) == 0) ||
846 855 (npa->npa_nsid == 0 &&
847 856 (feat->f_getflags & NVMEADM_CTRL) == 0)) {
848 857 warnx("feature %s %s supported for namespaces",
849 858 feat->f_name, (feat->f_getflags & NVMEADM_NS) != 0 ?
850 859 "only" : "not");
851 860 continue;
852 861 }
853 862
854 863 if (!header_printed) {
855 864 (void) printf("%s: Get Features\n", npa->npa_name);
856 865 header_printed = B_TRUE;
857 866 }
858 867
859 868 if (feat->f_get(fd, feat, npa->npa_idctl) != 0) {
860 869 warnx("unsupported feature: %s", feat->f_name);
861 870 continue;
862 871 }
863 872 }
864 873
865 874 free(flist);
866 875 return (0);
867 876 }
868 877
869 878 static int
870 879 do_format_common(int fd, const nvme_process_arg_t *npa, unsigned long lbaf,
871 880 unsigned long ses)
872 881 {
873 882 nvme_process_arg_t ns_npa = { 0 };
874 883 nvmeadm_cmd_t cmd = { 0 };
875 884
876 885 cmd = *(npa->npa_cmd);
877 886 cmd.c_func = do_attach_detach;
878 887 cmd.c_name = "detach";
879 888 ns_npa = *npa;
880 889 ns_npa.npa_cmd = &cmd;
881 890
882 891 if (do_attach_detach(fd, &ns_npa) != 0)
883 892 return (exitcode);
884 893 if (nvme_format_nvm(fd, lbaf, ses) == B_FALSE) {
885 894 warn("%s failed", npa->npa_cmd->c_name);
886 895 exitcode += -1;
887 896 }
888 897 cmd.c_name = "attach";
889 898 exitcode += do_attach_detach(fd, &ns_npa);
890 899
891 900 return (exitcode);
892 901 }
893 902
894 903 static void
895 904 usage_format(const char *c_name)
896 905 {
897 906 (void) fprintf(stderr, "%s <ctl>[/<ns>] [<lba-format>]\n\n"
898 907 " Format one or all namespaces of the specified NVMe "
899 908 "controller. Supported LBA\n formats can be queried with "
900 909 "the \"%s identify\" command on the namespace\n to be "
901 910 "formatted.\n", c_name, getprogname());
902 911 }
903 912
904 913 static int
905 914 do_format(int fd, const nvme_process_arg_t *npa)
906 915 {
907 916 unsigned long lbaf;
908 917
909 918 if (npa->npa_idctl->id_oacs.oa_format == 0)
910 919 errx(-1, "%s not supported", npa->npa_cmd->c_name);
911 920
912 921 if (npa->npa_isns && npa->npa_idctl->id_fna.fn_format != 0)
913 922 errx(-1, "%s not supported on individual namespace",
914 923 npa->npa_cmd->c_name);
915 924
916 925
917 926 if (npa->npa_argc > 0) {
918 927 errno = 0;
919 928 lbaf = strtoul(npa->npa_argv[0], NULL, 10);
920 929
921 930 if (errno != 0 || lbaf > NVME_FRMT_MAX_LBAF)
922 931 errx(-1, "invalid LBA format %d", lbaf + 1);
923 932
924 933 if (npa->npa_idns->id_lbaf[lbaf].lbaf_ms != 0)
925 934 errx(-1, "LBA formats with metadata not supported");
926 935 } else {
927 936 lbaf = npa->npa_idns->id_flbas.lba_format;
928 937 }
929 938
930 939 return (do_format_common(fd, npa, lbaf, 0));
931 940 }
932 941
933 942 static void
934 943 usage_secure_erase(const char *c_name)
935 944 {
936 945 (void) fprintf(stderr, "%s <ctl>[/<ns>] [-c]\n\n"
937 946 " Secure-Erase one or all namespaces of the specified "
938 947 "NVMe controller.\n", c_name);
939 948 }
940 949
941 950 static int
942 951 do_secure_erase(int fd, const nvme_process_arg_t *npa)
943 952 {
944 953 unsigned long lbaf;
945 954 uint8_t ses = NVME_FRMT_SES_USER;
946 955
947 956 if (npa->npa_idctl->id_oacs.oa_format == 0)
948 957 errx(-1, "%s not supported", npa->npa_cmd->c_name);
949 958
950 959 if (npa->npa_isns && npa->npa_idctl->id_fna.fn_sec_erase != 0)
951 960 errx(-1, "%s not supported on individual namespace",
952 961 npa->npa_cmd->c_name);
953 962
954 963 if (npa->npa_argc > 0) {
955 964 if (strcmp(npa->npa_argv[0], "-c") == 0)
956 965 ses = NVME_FRMT_SES_CRYPTO;
957 966 else
958 967 usage(npa->npa_cmd);
959 968 }
960 969
961 970 if (ses == NVME_FRMT_SES_CRYPTO &&
962 971 npa->npa_idctl->id_fna.fn_crypt_erase == 0)
963 972 errx(-1, "cryptographic %s not supported",
964 973 npa->npa_cmd->c_name);
965 974
966 975 lbaf = npa->npa_idns->id_flbas.lba_format;
967 976
968 977 return (do_format_common(fd, npa, lbaf, ses));
969 978 }
970 979
971 980 static void
972 981 usage_attach_detach(const char *c_name)
973 982 {
974 983 (void) fprintf(stderr, "%s <ctl>[/<ns>]\n\n"
975 984 " %c%s blkdev(7d) %s one or all namespaces of the "
976 985 "specified NVMe controller.\n",
977 986 c_name, toupper(c_name[0]), &c_name[1],
978 987 c_name[0] == 'd' ? "from" : "to");
979 988 }
980 989
981 990 static int
982 991 do_attach_detach(int fd, const nvme_process_arg_t *npa)
983 992 {
984 993 char *c_name = npa->npa_cmd->c_name;
985 994
986 995 if (!npa->npa_isns) {
987 996 nvme_process_arg_t ns_npa = { 0 };
988 997
989 998 ns_npa.npa_name = npa->npa_name;
990 999 ns_npa.npa_isns = B_TRUE;
991 1000 ns_npa.npa_cmd = npa->npa_cmd;
992 1001
993 1002 nvme_walk(&ns_npa, npa->npa_node);
994 1003
995 1004 return (exitcode);
996 1005 } else {
997 1006 if ((c_name[0] == 'd' ? nvme_detach : nvme_attach)(fd)
998 1007 == B_FALSE) {
999 1008 warn("%s failed", c_name);
1000 1009 return (-1);
1001 1010 }
1002 1011 }
1003 1012
1004 1013 return (0);
1005 1014 }
↓ open down ↓ |
453 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX