1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Nexenta Systems, Inc.
* Copyright 2017 Joyent, Inc.
* Copyright 2019 Western Digital Corporation.
* Copyright 2021 Oxide Computer Company
*/
/*
* nvmeadm -- NVMe administration utility
*
* nvmeadm [-v] [-d] [-h] <command> [<ctl>[/<ns>][,...]] [args]
* commands: list
* identify
* get-logpage <logpage name>
* get-features <feature>[,...]
* format ...
* secure-erase ...
* detach ...
* attach ...
* get-param ...
* set-param ...
* load-firmware ...
* commit-firmware ...
* activate-firmware ...
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <ctype.h>
#include <err.h>
#include <sys/sunddi.h>
#include <libdevinfo.h>
#include <sys/nvme.h>
#include "nvmeadm.h"
typedef struct nvme_process_arg nvme_process_arg_t;
typedef struct nvme_feature nvme_feature_t;
typedef struct nvmeadm_cmd nvmeadm_cmd_t;
struct nvme_process_arg {
int npa_argc;
char **npa_argv;
char *npa_name;
char *npa_nsid;
int npa_found;
boolean_t npa_isns;
const nvmeadm_cmd_t *npa_cmd;
di_node_t npa_node;
di_minor_t npa_minor;
char *npa_path;
char *npa_dsk;
nvme_identify_ctrl_t *npa_idctl;
nvme_identify_nsid_t *npa_idns;
nvme_version_t *npa_version;
};
struct nvme_feature {
char *f_name;
char *f_short;
uint8_t f_feature;
size_t f_bufsize;
uint_t f_getflags;
int (*f_get)(int, const nvme_feature_t *, const nvme_process_arg_t *);
void (*f_print)(uint64_t, void *, size_t, nvme_identify_ctrl_t *,
nvme_version_t *);
};
#define NVMEADM_CTRL 1
#define NVMEADM_NS 2
#define NVMEADM_BOTH (NVMEADM_CTRL | NVMEADM_NS)
struct nvmeadm_cmd {
char *c_name;
char *c_desc;
char *c_flagdesc;
int (*c_func)(int, const nvme_process_arg_t *);
void (*c_usage)(const char *);
boolean_t c_multi;
};
static void usage(const nvmeadm_cmd_t *);
static void nvme_walk(nvme_process_arg_t *, di_node_t);
static boolean_t nvme_match(nvme_process_arg_t *);
static int nvme_process(di_node_t, di_minor_t, void *);
static int do_list(int, const nvme_process_arg_t *);
static int do_identify(int, const nvme_process_arg_t *);
static int do_get_logpage_error(int, const nvme_process_arg_t *);
static int do_get_logpage_health(int, const nvme_process_arg_t *);
static int do_get_logpage_fwslot(int, const nvme_process_arg_t *);
static int do_get_logpage(int, const nvme_process_arg_t *);
static int do_get_feat_common(int, const nvme_feature_t *,
const nvme_process_arg_t *);
static int do_get_feat_intr_vect(int, const nvme_feature_t *,
const nvme_process_arg_t *);
static int do_get_feat_temp_thresh(int, const nvme_feature_t *,
const nvme_process_arg_t *);
static int do_get_features(int, const nvme_process_arg_t *);
static int do_format(int, const nvme_process_arg_t *);
static int do_secure_erase(int, const nvme_process_arg_t *);
static int do_attach_detach(int, const nvme_process_arg_t *);
static int do_firmware_load(int, const nvme_process_arg_t *);
static int do_firmware_commit(int, const nvme_process_arg_t *);
static int do_firmware_activate(int, const nvme_process_arg_t *);
static void usage_list(const char *);
static void usage_identify(const char *);
static void usage_get_logpage(const char *);
static void usage_get_features(const char *);
static void usage_format(const char *);
static void usage_secure_erase(const char *);
static void usage_attach_detach(const char *);
static void usage_firmware_load(const char *);
static void usage_firmware_commit(const char *);
static void usage_firmware_activate(const char *);
int verbose;
int debug;
static int exitcode;
static const nvmeadm_cmd_t nvmeadm_cmds[] = {
{
"list",
"list controllers and namespaces",
NULL,
do_list, usage_list, B_TRUE
},
{
"identify",
"identify controllers and/or namespaces",
NULL,
do_identify, usage_identify, B_TRUE
},
{
"get-logpage",
"get a log page from controllers and/or namespaces",
NULL,
do_get_logpage, usage_get_logpage, B_TRUE
},
{
"get-features",
"get features from controllers and/or namespaces",
NULL,
do_get_features, usage_get_features, B_TRUE
},
{
"format",
"format namespace(s) of a controller",
NULL,
do_format, usage_format, B_FALSE
},
{
"secure-erase",
"secure erase namespace(s) of a controller",
" -c Do a cryptographic erase.",
do_secure_erase, usage_secure_erase, B_FALSE
},
{
"detach",
"detach blkdev(7d) from namespace(s) of a controller",
NULL,
do_attach_detach, usage_attach_detach, B_FALSE
},
{
"attach",
"attach blkdev(7d) to namespace(s) of a controller",
NULL,
do_attach_detach, usage_attach_detach, B_FALSE
},
{
"load-firmware",
"load firmware to a controller",
NULL,
do_firmware_load, usage_firmware_load, B_FALSE
},
{
"commit-firmware",
"commit downloaded firmware to a slot of a controller",
NULL,
do_firmware_commit, usage_firmware_commit, B_FALSE
},
{
"activate-firmware",
"activate a firmware slot of a controller",
NULL,
do_firmware_activate, usage_firmware_activate, B_FALSE
},
{
NULL, NULL, NULL,
NULL, NULL, B_FALSE
}
};
static const nvme_feature_t features[] = {
{ "Arbitration", "",
NVME_FEAT_ARBITRATION, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_arbitration },
{ "Power Management", "",
NVME_FEAT_POWER_MGMT, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_power_mgmt },
{ "LBA Range Type", "range",
NVME_FEAT_LBA_RANGE, NVME_LBA_RANGE_BUFSIZE, NVMEADM_NS,
do_get_feat_common, nvme_print_feat_lba_range },
{ "Temperature Threshold", "",
NVME_FEAT_TEMPERATURE, 0, NVMEADM_CTRL,
do_get_feat_temp_thresh, nvme_print_feat_temperature },
{ "Error Recovery", "",
NVME_FEAT_ERROR, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_error },
{ "Volatile Write Cache", "cache",
NVME_FEAT_WRITE_CACHE, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_write_cache },
{ "Number of Queues", "queues",
NVME_FEAT_NQUEUES, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_nqueues },
{ "Interrupt Coalescing", "coalescing",
NVME_FEAT_INTR_COAL, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_intr_coal },
{ "Interrupt Vector Configuration", "vector",
NVME_FEAT_INTR_VECT, 0, NVMEADM_CTRL,
do_get_feat_intr_vect, nvme_print_feat_intr_vect },
{ "Write Atomicity", "atomicity",
NVME_FEAT_WRITE_ATOM, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_write_atom },
{ "Asynchronous Event Configuration", "event",
NVME_FEAT_ASYNC_EVENT, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_async_event },
{ "Autonomous Power State Transition", "",
NVME_FEAT_AUTO_PST, NVME_AUTO_PST_BUFSIZE, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_auto_pst },
{ "Software Progress Marker", "progress",
NVME_FEAT_PROGRESS, 0, NVMEADM_CTRL,
do_get_feat_common, nvme_print_feat_progress },
{ NULL, NULL, 0, 0, B_FALSE, NULL }
};
int
main(int argc, char **argv)
{
int c;
extern int optind;
const nvmeadm_cmd_t *cmd;
di_node_t node;
nvme_process_arg_t npa = { 0 };
int help = 0;
char *tmp, *lasts = NULL;
while ((c = getopt(argc, argv, "dhv")) != -1) {
switch (c) {
case 'd':
debug++;
break;
case 'v':
verbose++;
break;
case 'h':
help++;
break;
case '?':
usage(NULL);
exit(-1);
}
}
if (optind == argc) {
usage(NULL);
if (help)
exit(0);
else
exit(-1);
}
/* Look up the specified command in the command table. */
for (cmd = &nvmeadm_cmds[0]; cmd->c_name != NULL; cmd++)
if (strcmp(cmd->c_name, argv[optind]) == 0)
break;
if (cmd->c_name == NULL) {
usage(NULL);
exit(-1);
}
if (help) {
usage(cmd);
exit(0);
}
npa.npa_cmd = cmd;
optind++;
/*
* All commands but "list" require a ctl/ns argument.
*/
if ((optind == argc || (strncmp(argv[optind], "nvme", 4) != 0)) &&
cmd->c_func != do_list) {
warnx("missing controller/namespace name");
usage(cmd);
exit(-1);
}
/* Store the remaining arguments for use by the command. */
npa.npa_argc = argc - optind - 1;
npa.npa_argv = &argv[optind + 1];
/*
* Make sure we're not running commands on multiple controllers that
* aren't allowed to do that.
*/
if (argv[optind] != NULL && strchr(argv[optind], ',') != NULL &&
cmd->c_multi == B_FALSE) {
warnx("%s not allowed on multiple controllers",
cmd->c_name);
usage(cmd);
exit(-1);
}
/*
* Get controller/namespace arguments and run command.
*/
npa.npa_name = strtok_r(argv[optind], ",", &lasts);
do {
if (npa.npa_name != NULL) {
tmp = strchr(npa.npa_name, '/');
if (tmp != NULL) {
*tmp++ = '\0';
npa.npa_nsid = tmp;
npa.npa_isns = B_TRUE;
}
}
if ((node = di_init("/", DINFOSUBTREE | DINFOMINOR)) == NULL)
err(-1, "failed to initialize libdevinfo");
nvme_walk(&npa, node);
di_fini(node);
if (npa.npa_found == 0) {
if (npa.npa_name != NULL) {
warnx("%s%.*s%.*s: no such controller or "
"namespace", npa.npa_name,
npa.npa_isns ? -1 : 0, "/",
npa.npa_isns ? -1 : 0, npa.npa_nsid);
} else {
warnx("no controllers found");
}
exitcode--;
}
npa.npa_found = 0;
npa.npa_name = strtok_r(NULL, ",", &lasts);
} while (npa.npa_name != NULL);
exit(exitcode);
}
static void
usage(const nvmeadm_cmd_t *cmd)
{
(void) fprintf(stderr, "usage:\n");
(void) fprintf(stderr, " %s -h %s\n", getprogname(),
cmd != NULL ? cmd->c_name : "[<command>]");
(void) fprintf(stderr, " %s [-dv] ", getprogname());
if (cmd != NULL) {
cmd->c_usage(cmd->c_name);
} else {
(void) fprintf(stderr,
"<command> <ctl>[/<ns>][,...] [<args>]\n");
(void) fprintf(stderr,
"\n Manage NVMe controllers and namespaces.\n");
(void) fprintf(stderr, "\ncommands:\n");
for (cmd = &nvmeadm_cmds[0]; cmd->c_name != NULL; cmd++)
(void) fprintf(stderr, " %-18s - %s\n",
cmd->c_name, cmd->c_desc);
}
(void) fprintf(stderr, "\nflags:\n"
" -h print usage information\n"
" -d print information useful for debugging %s\n"
" -v print verbose information\n", getprogname());
if (cmd != NULL && cmd->c_flagdesc != NULL)
(void) fprintf(stderr, "%s\n", cmd->c_flagdesc);
}
static boolean_t
nvme_match(nvme_process_arg_t *npa)
{
char *name;
char *nsid = NULL;
if (npa->npa_name == NULL)
return (B_TRUE);
if (asprintf(&name, "%s%d", di_driver_name(npa->npa_node),
di_instance(npa->npa_node)) < 0)
err(-1, "nvme_match()");
if (strcmp(name, npa->npa_name) != 0) {
free(name);
return (B_FALSE);
}
free(name);
if (npa->npa_isns) {
if (npa->npa_nsid == NULL)
return (B_TRUE);
nsid = di_minor_name(npa->npa_minor);
if (nsid == NULL || strcmp(npa->npa_nsid, nsid) != 0)
return (B_FALSE);
}
return (B_TRUE);
}
char *
nvme_dskname(const nvme_process_arg_t *npa)
{
char *path = NULL;
di_node_t child;
di_dim_t dim;
char *addr;
dim = di_dim_init();
for (child = di_child_node(npa->npa_node);
child != DI_NODE_NIL;
child = di_sibling_node(child)) {
addr = di_bus_addr(child);
if (addr == NULL)
continue;
if (addr[0] == 'w')
addr++;
if (strncasecmp(addr, di_minor_name(npa->npa_minor),
strchrnul(addr, ',') - addr) != 0)
continue;
path = di_dim_path_dev(dim, di_driver_name(child),
di_instance(child), "c");
/*
* Error out if we didn't get a path, or if it's too short for
* the following operations to be safe.
*/
if (path == NULL || strlen(path) < 2)
goto fail;
/* Chop off 's0' and get everything past the last '/' */
path[strlen(path) - 2] = '\0';
path = strrchr(path, '/');
if (path == NULL)
goto fail;
path++;
break;
}
di_dim_fini(dim);
return (path);
fail:
err(-1, "nvme_dskname");
}
static int
nvme_process(di_node_t node, di_minor_t minor, void *arg)
{
nvme_process_arg_t *npa = arg;
int fd;
npa->npa_node = node;
npa->npa_minor = minor;
if (!nvme_match(npa))
return (DI_WALK_CONTINUE);
if ((fd = nvme_open(minor)) < 0)
return (DI_WALK_CONTINUE);
npa->npa_found++;
npa->npa_path = di_devfs_path(node);
if (npa->npa_path == NULL)
goto out;
npa->npa_version = nvme_version(fd);
if (npa->npa_version == NULL)
goto out;
npa->npa_idctl = nvme_identify_ctrl(fd);
if (npa->npa_idctl == NULL)
goto out;
npa->npa_idns = nvme_identify_nsid(fd);
if (npa->npa_idns == NULL)
goto out;
if (npa->npa_isns)
npa->npa_dsk = nvme_dskname(npa);
exitcode += npa->npa_cmd->c_func(fd, npa);
out:
di_devfs_path_free(npa->npa_path);
free(npa->npa_dsk);
free(npa->npa_version);
free(npa->npa_idctl);
free(npa->npa_idns);
npa->npa_version = NULL;
npa->npa_idctl = NULL;
npa->npa_idns = NULL;
nvme_close(fd);
return (DI_WALK_CONTINUE);
}
static void
nvme_walk(nvme_process_arg_t *npa, di_node_t node)
{
char *minor_nodetype = DDI_NT_NVME_NEXUS;
if (npa->npa_isns)
minor_nodetype = DDI_NT_NVME_ATTACHMENT_POINT;
(void) di_walk_minor(node, minor_nodetype, 0, npa, nvme_process);
}
static void
usage_list(const char *c_name)
{
(void) fprintf(stderr, "%s [<ctl>[/<ns>][,...]\n\n"
" List NVMe controllers and their namespaces. If no "
"controllers and/or name-\n spaces are specified, all "
"controllers and namespaces in the system will be\n "
"listed.\n", c_name);
}
static int
do_list_nsid(int fd, const nvme_process_arg_t *npa)
{
_NOTE(ARGUNUSED(fd));
const uint_t format = npa->npa_idns->id_flbas.lba_format;
const uint_t bshift = npa->npa_idns->id_lbaf[format].lbaf_lbads;
/*
* Some devices have extra namespaces with illegal block sizes and
* zero blocks. Don't list them when verbose operation isn't requested.
*/
if ((bshift < 9 || npa->npa_idns->id_nsize == 0) && verbose == 0)
return (0);
(void) printf(" %s/%s (%s): ", npa->npa_name,
di_minor_name(npa->npa_minor),
npa->npa_dsk != NULL ? npa->npa_dsk : "unattached");
nvme_print_nsid_summary(npa->npa_idns);
return (0);
}
static int
do_list(int fd, const nvme_process_arg_t *npa)
{
_NOTE(ARGUNUSED(fd));
nvme_process_arg_t ns_npa = { 0 };
nvmeadm_cmd_t cmd = { 0 };
char *name;
if (asprintf(&name, "%s%d", di_driver_name(npa->npa_node),
di_instance(npa->npa_node)) < 0)
err(-1, "do_list()");
(void) printf("%s: ", name);
nvme_print_ctrl_summary(npa->npa_idctl, npa->npa_version);
ns_npa.npa_name = name;
ns_npa.npa_isns = B_TRUE;
ns_npa.npa_nsid = npa->npa_nsid;
cmd = *(npa->npa_cmd);
cmd.c_func = do_list_nsid;
ns_npa.npa_cmd = &cmd;
nvme_walk(&ns_npa, npa->npa_node);
free(name);
return (exitcode);
}
static void
usage_identify(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl>[/<ns>][,...]\n\n"
" Print detailed information about the specified NVMe "
"controllers and/or name-\n spaces.\n", c_name);
}
static int
do_identify(int fd, const nvme_process_arg_t *npa)
{
if (!npa->npa_isns) {
nvme_capabilities_t *cap;
cap = nvme_capabilities(fd);
if (cap == NULL)
return (-1);
(void) printf("%s: ", npa->npa_name);
nvme_print_identify_ctrl(npa->npa_idctl, cap,
npa->npa_version);
free(cap);
} else {
(void) printf("%s/%s: ", npa->npa_name,
di_minor_name(npa->npa_minor));
nvme_print_identify_nsid(npa->npa_idns,
npa->npa_version);
}
return (0);
}
static void
usage_get_logpage(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl>[/<ns>][,...] <logpage>\n\n"
" Print the specified log page of the specified NVMe "
"controllers and/or name-\n spaces. Supported log pages "
"are error, health, and firmware.\n", c_name);
}
static int
do_get_logpage_error(int fd, const nvme_process_arg_t *npa)
{
int nlog = npa->npa_idctl->id_elpe + 1;
size_t bufsize = sizeof (nvme_error_log_entry_t) * nlog;
nvme_error_log_entry_t *elog;
if (npa->npa_isns)
errx(-1, "Error Log not available on a per-namespace basis");
elog = nvme_get_logpage(fd, NVME_LOGPAGE_ERROR, &bufsize);
if (elog == NULL)
return (-1);
nlog = bufsize / sizeof (nvme_error_log_entry_t);
(void) printf("%s: ", npa->npa_name);
nvme_print_error_log(nlog, elog, npa->npa_version);
free(elog);
return (0);
}
static int
do_get_logpage_health(int fd, const nvme_process_arg_t *npa)
{
size_t bufsize = sizeof (nvme_health_log_t);
nvme_health_log_t *hlog;
if (npa->npa_isns) {
if (npa->npa_idctl->id_lpa.lp_smart == 0)
errx(-1, "SMART/Health information not available "
"on a per-namespace basis on this controller");
}
hlog = nvme_get_logpage(fd, NVME_LOGPAGE_HEALTH, &bufsize);
if (hlog == NULL)
return (-1);
(void) printf("%s: ", npa->npa_name);
nvme_print_health_log(hlog, npa->npa_idctl, npa->npa_version);
free(hlog);
return (0);
}
static int
do_get_logpage_fwslot(int fd, const nvme_process_arg_t *npa)
{
size_t bufsize = sizeof (nvme_fwslot_log_t);
nvme_fwslot_log_t *fwlog;
if (npa->npa_isns)
errx(-1, "Firmware Slot information not available on a "
"per-namespace basis");
fwlog = nvme_get_logpage(fd, NVME_LOGPAGE_FWSLOT, &bufsize);
if (fwlog == NULL)
return (-1);
(void) printf("%s: ", npa->npa_name);
nvme_print_fwslot_log(fwlog);
free(fwlog);
return (0);
}
static int
do_get_logpage(int fd, const nvme_process_arg_t *npa)
{
int ret = 0;
int (*func)(int, const nvme_process_arg_t *);
if (npa->npa_argc < 1) {
warnx("missing logpage name");
usage(npa->npa_cmd);
exit(-1);
}
if (strcmp(npa->npa_argv[0], "error") == 0)
func = do_get_logpage_error;
else if (strcmp(npa->npa_argv[0], "health") == 0)
func = do_get_logpage_health;
else if (strcmp(npa->npa_argv[0], "firmware") == 0)
func = do_get_logpage_fwslot;
else
errx(-1, "invalid log page: %s", npa->npa_argv[0]);
ret = func(fd, npa);
return (ret);
}
static void
usage_get_features(const char *c_name)
{
const nvme_feature_t *feat;
(void) fprintf(stderr, "%s <ctl>[/<ns>][,...] [<feature>[,...]]\n\n"
" Print the specified features of the specified NVMe controllers "
"and/or\n namespaces. Supported features are:\n\n", c_name);
(void) fprintf(stderr, " %-35s %-14s %s\n",
"FEATURE NAME", "SHORT NAME", "CONTROLLER/NAMESPACE");
for (feat = &features[0]; feat->f_feature != 0; feat++) {
char *type;
if ((feat->f_getflags & NVMEADM_BOTH) == NVMEADM_BOTH)
type = "both";
else if ((feat->f_getflags & NVMEADM_CTRL) != 0)
type = "controller only";
else
type = "namespace only";
(void) fprintf(stderr, " %-35s %-14s %s\n",
feat->f_name, feat->f_short, type);
}
}
static int
do_get_feat_common(int fd, const nvme_feature_t *feat,
const nvme_process_arg_t *npa)
{
void *buf = NULL;
size_t bufsize = feat->f_bufsize;
uint64_t res;
if (nvme_get_feature(fd, feat->f_feature, 0, &res, &bufsize, &buf)
== B_FALSE)
return (EINVAL);
nvme_print(2, feat->f_name, -1, NULL);
feat->f_print(res, buf, bufsize, npa->npa_idctl, npa->npa_version);
free(buf);
return (0);
}
static int
do_get_feat_temp_thresh_one(int fd, const nvme_feature_t *feat,
const char *label, uint16_t tmpsel, uint16_t thsel,
const nvme_process_arg_t *npa)
{
uint64_t res;
void *buf = NULL;
size_t bufsize = feat->f_bufsize;
nvme_temp_threshold_t tt;
tt.r = 0;
tt.b.tt_tmpsel = tmpsel;
tt.b.tt_thsel = thsel;
if (!nvme_get_feature(fd, feat->f_feature, tt.r, &res, &bufsize,
&buf)) {
return (EINVAL);
}
feat->f_print(res, (void *)label, 0, npa->npa_idctl, npa->npa_version);
free(buf);
return (0);
}
/*
* In NVMe 1.2, the specification allowed for up to 8 sensors to be on the
* device and changed the main device to have a composite temperature sensor. As
* a result, there is a set of thresholds for each sensor. In addition, they
* added both an over-temperature and under-temperature threshold. Since most
* devices don't actually implement all the sensors, we get the health page and
* see which sensors have a non-zero value to determine how to proceed.
*/
static int
do_get_feat_temp_thresh(int fd, const nvme_feature_t *feat,
const nvme_process_arg_t *npa)
{
int ret;
size_t bufsize = sizeof (nvme_health_log_t);
nvme_health_log_t *hlog;
nvme_print(2, feat->f_name, -1, NULL);
if ((ret = do_get_feat_temp_thresh_one(fd, feat,
"Composite Over Temp. Threshold", 0, NVME_TEMP_THRESH_OVER,
npa)) != 0) {
return (ret);
}
if (!nvme_version_check(npa->npa_version, 1, 2)) {
return (0);
}
if ((ret = do_get_feat_temp_thresh_one(fd, feat,
"Composite Under Temp. Threshold", 0, NVME_TEMP_THRESH_UNDER,
npa)) != 0) {
return (ret);
}
hlog = nvme_get_logpage(fd, NVME_LOGPAGE_HEALTH, &bufsize);
if (hlog == NULL) {
warnx("failed to get health log page, unable to get "
"thresholds for additional sensors");
return (0);
}
if (hlog->hl_temp_sensor_1 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 1 Over Temp. Threshold", 1,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 1 Under Temp. Threshold", 1,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_2 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 2 Over Temp. Threshold", 2,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 2 Under Temp. Threshold", 2,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_3 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 3 Over Temp. Threshold", 3,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 3 Under Temp. Threshold", 3,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_4 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 4 Over Temp. Threshold", 4,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 4 Under Temp. Threshold", 4,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_5 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 5 Over Temp. Threshold", 5,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 5 Under Temp. Threshold", 5,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_6 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 6 Over Temp. Threshold", 6,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 6 Under Temp. Threshold", 6,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_7 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 7 Over Temp. Threshold", 7,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 7 Under Temp. Threshold", 7,
NVME_TEMP_THRESH_UNDER, npa);
}
if (hlog->hl_temp_sensor_8 != 0) {
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 8 Over Temp. Threshold", 8,
NVME_TEMP_THRESH_OVER, npa);
(void) do_get_feat_temp_thresh_one(fd, feat,
"Temp. Sensor 8 Under Temp. Threshold", 8,
NVME_TEMP_THRESH_UNDER, npa);
}
free(hlog);
return (0);
}
static int
do_get_feat_intr_vect(int fd, const nvme_feature_t *feat,
const nvme_process_arg_t *npa)
{
uint64_t res;
uint64_t arg;
int intr_cnt;
intr_cnt = nvme_intr_cnt(fd);
if (intr_cnt == -1)
return (EINVAL);
nvme_print(2, feat->f_name, -1, NULL);
for (arg = 0; arg < intr_cnt; arg++) {
if (nvme_get_feature(fd, feat->f_feature, arg, &res, NULL, NULL)
== B_FALSE)
return (EINVAL);
feat->f_print(res, NULL, 0, npa->npa_idctl, npa->npa_version);
}
return (0);
}
static int
do_get_features(int fd, const nvme_process_arg_t *npa)
{
const nvme_feature_t *feat;
char *f, *flist, *lasts;
boolean_t header_printed = B_FALSE;
if (npa->npa_argc > 1)
errx(-1, "unexpected arguments");
/*
* No feature list given, print all supported features.
*/
if (npa->npa_argc == 0) {
(void) printf("%s: Get Features\n", npa->npa_name);
for (feat = &features[0]; feat->f_feature != 0; feat++) {
if ((npa->npa_isns &&
(feat->f_getflags & NVMEADM_NS) == 0) ||
(!npa->npa_isns &&
(feat->f_getflags & NVMEADM_CTRL) == 0))
continue;
(void) feat->f_get(fd, feat, npa);
}
return (0);
}
/*
* Process feature list.
*/
flist = strdup(npa->npa_argv[0]);
if (flist == NULL)
err(-1, "do_get_features");
for (f = strtok_r(flist, ",", &lasts);
f != NULL;
f = strtok_r(NULL, ",", &lasts)) {
while (isspace(*f))
f++;
for (feat = &features[0]; feat->f_feature != 0; feat++) {
if (strncasecmp(feat->f_name, f, strlen(f)) == 0 ||
strncasecmp(feat->f_short, f, strlen(f)) == 0)
break;
}
if (feat->f_feature == 0) {
warnx("unknown feature %s", f);
continue;
}
if ((npa->npa_isns &&
(feat->f_getflags & NVMEADM_NS) == 0) ||
(!npa->npa_isns &&
(feat->f_getflags & NVMEADM_CTRL) == 0)) {
warnx("feature %s %s supported for namespaces",
feat->f_name, (feat->f_getflags & NVMEADM_NS) != 0 ?
"only" : "not");
continue;
}
if (!header_printed) {
(void) printf("%s: Get Features\n", npa->npa_name);
header_printed = B_TRUE;
}
if (feat->f_get(fd, feat, npa) != 0) {
warnx("unsupported feature: %s", feat->f_name);
continue;
}
}
free(flist);
return (0);
}
static int
do_format_common(int fd, const nvme_process_arg_t *npa, unsigned long lbaf,
unsigned long ses)
{
nvme_process_arg_t ns_npa = { 0 };
nvmeadm_cmd_t cmd = { 0 };
cmd = *(npa->npa_cmd);
cmd.c_func = do_attach_detach;
cmd.c_name = "detach";
ns_npa = *npa;
ns_npa.npa_cmd = &cmd;
if (do_attach_detach(fd, &ns_npa) != 0)
return (exitcode);
if (nvme_format_nvm(fd, lbaf, ses) == B_FALSE) {
warn("%s failed", npa->npa_cmd->c_name);
exitcode += -1;
}
cmd.c_name = "attach";
exitcode += do_attach_detach(fd, &ns_npa);
return (exitcode);
}
static void
usage_format(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl>[/<ns>] [<lba-format>]\n\n"
" Format one or all namespaces of the specified NVMe "
"controller. Supported LBA\n formats can be queried with "
"the \"%s identify\" command on the namespace\n to be "
"formatted.\n", c_name, getprogname());
}
static int
do_format(int fd, const nvme_process_arg_t *npa)
{
unsigned long lbaf;
if (npa->npa_idctl->id_oacs.oa_format == 0)
errx(-1, "%s not supported", npa->npa_cmd->c_name);
if (npa->npa_isns && npa->npa_idctl->id_fna.fn_format != 0)
errx(-1, "%s not supported on individual namespace",
npa->npa_cmd->c_name);
if (npa->npa_argc > 0) {
errno = 0;
lbaf = strtoul(npa->npa_argv[0], NULL, 10);
if (errno != 0 || lbaf > NVME_FRMT_MAX_LBAF)
errx(-1, "invalid LBA format %d", lbaf + 1);
if (npa->npa_idns->id_lbaf[lbaf].lbaf_ms != 0)
errx(-1, "LBA formats with metadata not supported");
} else {
lbaf = npa->npa_idns->id_flbas.lba_format;
}
return (do_format_common(fd, npa, lbaf, 0));
}
static void
usage_secure_erase(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl>[/<ns>] [-c]\n\n"
" Secure-Erase one or all namespaces of the specified "
"NVMe controller.\n", c_name);
}
static int
do_secure_erase(int fd, const nvme_process_arg_t *npa)
{
unsigned long lbaf;
uint8_t ses = NVME_FRMT_SES_USER;
if (npa->npa_idctl->id_oacs.oa_format == 0)
errx(-1, "%s not supported", npa->npa_cmd->c_name);
if (npa->npa_isns && npa->npa_idctl->id_fna.fn_sec_erase != 0)
errx(-1, "%s not supported on individual namespace",
npa->npa_cmd->c_name);
if (npa->npa_argc > 0) {
if (strcmp(npa->npa_argv[0], "-c") == 0)
ses = NVME_FRMT_SES_CRYPTO;
else
usage(npa->npa_cmd);
}
if (ses == NVME_FRMT_SES_CRYPTO &&
npa->npa_idctl->id_fna.fn_crypt_erase == 0)
errx(-1, "cryptographic %s not supported",
npa->npa_cmd->c_name);
lbaf = npa->npa_idns->id_flbas.lba_format;
return (do_format_common(fd, npa, lbaf, ses));
}
static void
usage_attach_detach(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl>[/<ns>]\n\n"
" %c%s blkdev(7d) %s one or all namespaces of the "
"specified NVMe controller.\n",
c_name, toupper(c_name[0]), &c_name[1],
c_name[0] == 'd' ? "from" : "to");
}
static int
do_attach_detach(int fd, const nvme_process_arg_t *npa)
{
char *c_name = npa->npa_cmd->c_name;
if (!npa->npa_isns) {
nvme_process_arg_t ns_npa = { 0 };
ns_npa.npa_name = npa->npa_name;
ns_npa.npa_isns = B_TRUE;
ns_npa.npa_cmd = npa->npa_cmd;
nvme_walk(&ns_npa, npa->npa_node);
return (exitcode);
} else {
if ((c_name[0] == 'd' ? nvme_detach : nvme_attach)(fd)
== B_FALSE) {
warn("%s failed", c_name);
return (-1);
}
}
return (0);
}
static void
usage_firmware_load(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl> <image file> [<offset>]\n\n"
" Load firmware <image file> to offset <offset>.\n"
" The firmware needs to be committed to a slot using "
"\"nvmeadm commit-firmware\"\n command.\n", c_name);
}
/*
* Read exactly len bytes, or until eof.
*/
static ssize_t
read_block(int fd, char *buf, size_t len)
{
size_t remain;
ssize_t bytes;
remain = len;
while (remain > 0) {
bytes = read(fd, buf, remain);
if (bytes == 0)
break;
if (bytes < 0) {
if (errno == EINTR)
continue;
return (-1);
}
buf += bytes;
remain -= bytes;
}
return (len - remain);
}
/*
* Convert a string to a valid firmware upload offset (in bytes).
*/
static offset_t
get_fw_offsetb(char *str)
{
longlong_t offsetb;
char *valend;
errno = 0;
offsetb = strtoll(str, &valend, 0);
if (errno != 0 || *valend != '\0' || offsetb < 0 ||
offsetb > NVME_FW_OFFSETB_MAX)
errx(-1, "Offset must be numeric and in the range of 0 to %llu",
NVME_FW_OFFSETB_MAX);
if ((offsetb & NVME_DWORD_MASK) != 0)
errx(-1, "Offset must be multiple of %d", NVME_DWORD_SIZE);
return ((offset_t)offsetb);
}
#define FIRMWARE_READ_BLKSIZE (64 * 1024) /* 64K */
static int
do_firmware_load(int fd, const nvme_process_arg_t *npa)
{
int fw_fd;
ssize_t len;
offset_t offset = 0;
size_t size;
char buf[FIRMWARE_READ_BLKSIZE];
if (npa->npa_argc > 2)
errx(-1, "Too many arguments");
if (npa->npa_argc == 0)
errx(-1, "Requires firmware file name, and an "
"optional offset");
if (npa->npa_argc == 2)
offset = get_fw_offsetb(npa->npa_argv[1]);
fw_fd = open(npa->npa_argv[0], O_RDONLY);
if (fw_fd < 0)
errx(-1, "Failed to open \"%s\": %s", npa->npa_argv[0],
strerror(errno));
size = 0;
do {
len = read_block(fw_fd, buf, sizeof (buf));
if (len < 0)
errx(-1, "Error reading \"%s\": %s", npa->npa_argv[0],
strerror(errno));
if (len == 0)
break;
if (!nvme_firmware_load(fd, buf, len, offset))
errx(-1, "Error loading \"%s\": %s", npa->npa_argv[0],
strerror(errno));
offset += len;
size += len;
} while (len == sizeof (buf));
(void) close(fw_fd);
if (verbose)
(void) printf("%zu bytes downloaded.\n", size);
return (0);
}
/*
* Convert str to a valid firmware slot number.
*/
static uint_t
get_slot_number(char *str)
{
longlong_t slot;
char *valend;
errno = 0;
slot = strtoll(str, &valend, 0);
if (errno != 0 || *valend != '\0' ||
slot < NVME_FW_SLOT_MIN || slot > NVME_FW_SLOT_MAX)
errx(-1, "Slot must be numeric and in the range of %d to %d",
NVME_FW_SLOT_MIN, NVME_FW_SLOT_MAX);
return ((uint_t)slot);
}
static void
usage_firmware_commit(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl> <slot>\n\n"
" Commit previously downloaded firmware to slot <slot>.\n"
" The firmware is only activated after a "
"\"nvmeadm activate-firmware\" command.\n", c_name);
}
static int
do_firmware_commit(int fd, const nvme_process_arg_t *npa)
{
uint_t slot;
uint16_t sct, sc;
if (npa->npa_argc > 1)
errx(-1, "Too many arguments");
if (npa->npa_argc == 0)
errx(-1, "Firmware slot number is required");
slot = get_slot_number(npa->npa_argv[0]);
if (!nvme_firmware_commit(fd, slot, NVME_FWC_SAVE, &sct, &sc))
errx(-1, "Failed to commit firmware to slot %u: %s",
slot, nvme_str_error(sct, sc));
if (verbose)
(void) printf("Firmware committed to slot %u.\n", slot);
return (0);
}
static void
usage_firmware_activate(const char *c_name)
{
(void) fprintf(stderr, "%s <ctl> <slot>\n\n"
" Activate firmware in slot <slot>.\n"
" The firmware will be in use after the next system reset.\n",
c_name);
}
static int
do_firmware_activate(int fd, const nvme_process_arg_t *npa)
{
uint_t slot;
uint16_t sct, sc;
if (npa->npa_argc > 1)
errx(-1, "Too many arguments");
if (npa->npa_argc == 0)
errx(-1, "Firmware slot number is required");
slot = get_slot_number(npa->npa_argv[0]);
if (!nvme_firmware_commit(fd, slot, NVME_FWC_ACTIVATE, &sct, &sc))
errx(-1, "Failed to activate slot %u: %s", slot,
nvme_str_error(sct, sc));
if (verbose)
printf("Slot %u activated: %s.\n", slot,
nvme_str_error(sct, sc));
return (0);
}
/*
* While the NVME_VERSION_ATLEAST macro exists, specifying a version of 1.0
* causes GCC to helpfully flag the -Wtype-limits warning because a uint_t is
* always >= 0. In many cases it's useful to always indicate what version
* something was added in to simplify code (e.g. nvmeadm_print_bit) and we'd
* rather just say it's version 1.0 rather than making folks realize that a
* hardcoded true is equivalent. Therefore we have this function which can't
* trigger this warning today (and adds a minor amount of type safety). If GCC
* or clang get smart enough to see through this, then we'll have to just
* disable the warning for the single minor comparison (and reformat this a bit
* to minimize the impact).
*/
boolean_t
nvme_version_check(nvme_version_t *vers, uint_t major, uint_t minor)
{
if (vers->v_major > major) {
return (B_TRUE);
}
return (vers->v_major == major && vers->v_minor >= minor);
}
|