forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme.c
2700 lines (2407 loc) · 88.7 KB
/
nvme.c
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
/*
* nvme.c -- NVM-Express command line utility.
*
* Copyright (c) 2014-2015, Intel Corporation.
*
* Written by Keith Busch <keith.busch@intel.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* This program uses NVMe IOCTLs to run native nvme commands to a device.
*/
#include <endian.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#ifdef LIBUDEV_EXISTS
#include <libudev.h>
#endif
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "nvme-print.h"
#include "nvme-ioctl.h"
#include "src/argconfig.h"
#include "src/suffix.h"
#define array_len(x) ((size_t)(sizeof(x) / sizeof(x[0])))
#define min(x, y) (x) > (y) ? (y) : (x)
#define max(x, y) (x) > (y) ? (x) : (y)
static int fd;
static struct stat nvme_stat;
static const char *devicename;
static const char nvme_version_string[] = NVME_VERSION;
#define COMMAND_LIST \
ENTRY(LIST, "list", "List all NVMe devices and namespaces on machine", list) \
ENTRY(ID_CTRL, "id-ctrl", "Send NVMe Identify Controller", id_ctrl) \
ENTRY(ID_NS, "id-ns", "Send NVMe Identify Namespace, display structure", id_ns) \
ENTRY(LIST_NS, "list-ns", "Send NVMe Identify List, display structure", list_ns) \
ENTRY(CREATE_NS, "create-ns", "Creates a namespace with the provided parameters", create_ns) \
ENTRY(DELETE_NS, "delete-ns", "Deletes a namespace from the controller", delete_ns) \
ENTRY(ATTACH_NS, "attach-ns", "Attaches a namespace to requested controller(s)", attach_ns) \
ENTRY(DETACH_NS, "detach-ns", "Detaches a namespace from requested controller(s)", detach_ns) \
ENTRY(LIST_CTRL, "list-ctrl", "Send NVMe Identify Controller List, display structure", list_ctrl) \
ENTRY(GET_NS_ID, "get-ns-id", "Retrieve the namespace ID of opened block device", get_ns_id) \
ENTRY(GET_LOG, "get-log", "Generic NVMe get log, returns log in raw format", get_log) \
ENTRY(GET_FW_LOG, "fw-log", "Retrieve FW Log, show it", get_fw_log) \
ENTRY(GET_SMART_LOG, "smart-log", "Retrieve SMART Log, show it", get_smart_log) \
ENTRY(GET_ADDITIONAL_SMART_LOG, "smart-log-add", "Retrieve additional SMART Log, show it", get_additional_smart_log) \
ENTRY(GET_ERR_LOG, "error-log", "Retrieve Error Log, show it", get_error_log) \
ENTRY(GET_FEATURE, "get-feature", "Get feature and show the resulting value", get_feature) \
ENTRY(SET_FEATURE, "set-feature", "Set a feature and show the resulting value", set_feature) \
ENTRY(FORMAT, "format", "Format namespace with new block format", format) \
ENTRY(FW_ACTIVATE, "fw-activate", "Activate new firmware slot", fw_activate) \
ENTRY(FW_DOWNLOAD, "fw-download", "Download new firmware", fw_download) \
ENTRY(ADMIN_PASSTHRU, "admin-passthru", "Submit arbitrary admin command, return results", admin_passthru) \
ENTRY(IO_PASSTHRU, "io-passthru", "Submit an arbitrary IO command, return results", io_passthru) \
ENTRY(SECURITY_SEND, "security-send", "Submit a Security Send command, return results", sec_send) \
ENTRY(SECURITY_RECV, "security-recv", "Submit a Security Receive command, return results", sec_recv) \
ENTRY(RESV_ACQUIRE, "resv-acquire", "Submit a Reservation Acquire, return results", resv_acquire) \
ENTRY(RESV_REGISTER, "resv-register", "Submit a Reservation Register, return results", resv_register) \
ENTRY(RESV_RELEASE, "resv-release", "Submit a Reservation Release, return results", resv_release) \
ENTRY(RESV_REPORT, "resv-report", "Submit a Reservation Report, return results", resv_report) \
ENTRY(DSM, "dsm", "Submit a Data Set Management command, return results", dsm) \
ENTRY(FLUSH, "flush", "Submit a Flush command, return results", flush) \
ENTRY(COMPARE, "compare", "Submit a Compare command, return results", compare) \
ENTRY(READ_CMD, "read", "Submit a read command, return results", read_cmd) \
ENTRY(WRITE_CMD, "write", "Submit a write command, return results", write_cmd) \
ENTRY(REGISTERS, "show-regs", "Shows the controller registers. Requires admin character device", show_registers) \
ENTRY(VERSION, "version", "Shows the program version", version) \
ENTRY(HELP, "help", "Display this help", help)
#define ENTRY(i, n, h, f) \
static int f(int argc, char **argv);
COMMAND_LIST
#undef ENTRY
enum {
#define ENTRY(i, n, h, f) i,
COMMAND_LIST
#undef ENTRY
NUM_COMMANDS
};
struct command {
char *name;
char *help;
char *path;
char *man;
int (*fn)(int argc, char **argv);
};
struct command commands[] = {
#define ENTRY(i, n, h, f)\
{ \
.name = n, \
.help = h, \
.fn = f, \
.path = "Documentation/nvme-"n".1", \
.man = "nvme-"n, \
},
COMMAND_LIST
#undef ENTRY
};
static unsigned long long elapsed_utime(struct timeval start_time,
struct timeval end_time)
{
unsigned long long ret = (end_time.tv_sec - start_time.tv_sec)*1000000 +
(end_time.tv_usec - start_time.tv_usec);
return ret;
}
static void open_dev(const char *dev)
{
int err;
devicename = basename(dev);
fd = open(dev, O_RDONLY);
if (fd < 0)
goto perror;
err = fstat(fd, &nvme_stat);
if (err < 0)
goto perror;
if (!S_ISCHR(nvme_stat.st_mode) && !S_ISBLK(nvme_stat.st_mode)) {
fprintf(stderr, "%s is not a block or character device\n", dev);
exit(ENODEV);
}
return;
perror:
perror(dev);
exit(errno);
}
static void get_dev(int optind, int argc, char **argv)
{
if (optind >= argc) {
errno = EINVAL;
perror(argv[0]);
exit(errno);
}
open_dev((const char *)argv[optind]);
}
static int get_smart_log(int argc, char **argv)
{
struct nvme_smart_log smart_log;
const char *desc = "smart-log: retrieve SMART log for the given "\
"device (or optionally, namespace) in either hex-dump "\
"(default) or binary format.";
const char *namespace = "(optional) desired namespace";
const char *raw = "output in binary format";
int err;
struct config {
__u32 namespace_id;
__u8 raw_binary;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0xffffffff,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace},
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
err = nvme_smart_log(fd, cfg.namespace_id, &smart_log);
if (!err) {
if (!cfg.raw_binary)
show_smart_log(&smart_log, cfg.namespace_id, devicename);
else
d_raw((unsigned char *)&smart_log, sizeof(smart_log));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static int get_additional_smart_log(int argc, char **argv)
{
struct nvme_additional_smart_log smart_log;
int err;
char *desc = "Get additional smart log (optionally, "\
"for the specified namspace), and show it.";
const char *namespace = "(optional) desired namespace";
const char *raw = "dump output in binary format";
struct config {
__u32 namespace_id;
__u8 raw_binary;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0xffffffff,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace},
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
err = nvme_intel_smart_log(fd, cfg.namespace_id, &smart_log);
if (!err) {
if (!cfg.raw_binary)
show_intel_smart_log(&smart_log, cfg.namespace_id, devicename);
else
d_raw((unsigned char *)&smart_log, sizeof(smart_log));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static int get_error_log(int argc, char **argv)
{
const char *desc = "error-log: retrieve specified number of "\
"error log entries from a given device (or "\
"namespace) in either hex-dump (default) or binary format.";
const char *namespace_id = "desired namespace";
const char *log_entries = "number of entries to retrieve";
const char *raw_binary = "dump in binary format";
struct nvme_id_ctrl ctrl;
int err;
struct config {
__u32 namespace_id;
__u32 log_entries;
__u8 raw_binary;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0xffffffff,
.log_entries = 64,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"log-entries", "NUM", CFG_POSITIVE, &defaults.log_entries, required_argument, log_entries},
{"e", "NUM", CFG_POSITIVE, &defaults.log_entries, required_argument, log_entries},
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
if (!cfg.log_entries) {
fprintf(stderr, "non-zero log-entries is required param\n");
return EINVAL;
}
err = nvme_identify_ctrl(fd, &ctrl);
cfg.log_entries = min(cfg.log_entries, ctrl.elpe + 1);
if (err) {
fprintf(stderr, "could not identify controller\n");
return ENODEV;
} else {
struct nvme_error_log_page err_log[cfg.log_entries];
err = nvme_error_log(fd, cfg.namespace_id, cfg.log_entries, err_log);
if (!err) {
if (!cfg.raw_binary)
show_error_log(err_log, cfg.log_entries, devicename);
else
d_raw((unsigned char *)err_log, sizeof(err_log));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
}
return err;
}
static int get_fw_log(int argc, char **argv)
{
const char *desc = "fw-log: retrieve the firmware log for the "\
"specified device in either hex-dump (default) or binary "\
"format.";
const char *raw_binary = "use binary output";
int err;
struct nvme_firmware_log_page fw_log;
struct config {
__u8 raw_binary;
};
struct config cfg;
const struct config defaults = {
};
const struct argconfig_commandline_options command_line_options[] = {
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
err = nvme_fw_log(fd, &fw_log);
if (!err) {
if (!cfg.raw_binary)
show_fw_log(&fw_log, devicename);
else
d_raw((unsigned char *)&fw_log, sizeof(fw_log));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("fw log");
return err;
}
static int get_log(int argc, char **argv)
{
const char *desc = "get-log: retrieve desired number of bytes "\
"from a given log on a specified device in either "\
"hex-dump (default) or binary format";
const char *namespace_id = "desired namespace";
const char *log_id = "name of log to retrieve";
const char *log_len = "how many bytes to retrieve";
const char *raw_binary = "output in raw format";
int err;
struct config {
__u32 namespace_id;
__u32 log_id;
__u32 log_len;
__u8 raw_binary;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0xffffffff,
.log_id = 0,
.log_len = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"log-id", "NUM", CFG_POSITIVE, &defaults.log_id, required_argument, log_id},
{"i", "NUM", CFG_POSITIVE, &defaults.log_id, required_argument, log_id},
{"log-len", "NUM", CFG_POSITIVE, &defaults.log_len, required_argument, log_len},
{"l", "NUM", CFG_POSITIVE, &defaults.log_len, required_argument, log_len},
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
if (!cfg.log_len) {
fprintf(stderr, "non-zero log-len is required param\n");
return EINVAL;
} else {
unsigned char log[cfg.log_len];
err = nvme_log(fd, cfg.namespace_id, cfg.log_id, cfg.log_len, log);
if (!err) {
if (!cfg.raw_binary) {
printf("Device:%s log-id:%d namespace-id:%#x\n",
devicename, cfg.log_id,
cfg.namespace_id);
d(log, cfg.log_len, 16, 1);
} else
d_raw((unsigned char *)log, cfg.log_len);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
}
static int list_ctrl(int argc, char **argv)
{
const char *desc = "list-ctrl: show controller information for the "\
"given device (and optionally, namespace)";
const char *controller = "controller to display";
const char *namespace_id = "optional namespace attached to controller";
int err, i;
struct nvme_controller_list *cntlist;
struct config {
__u16 cntid;
__u32 namespace_id;
};
struct config cfg;
const struct config defaults = {
.cntid = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"cntid", "NUM", CFG_SHORT, &defaults.cntid, required_argument, controller},
{"c", "NUM", CFG_SHORT, &defaults.cntid, required_argument, controller},
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
if (posix_memalign((void *)&cntlist, getpagesize(), 0x1000))
return ENOMEM;
err = nvme_identify_ctrl_list(fd, cfg.namespace_id, cfg.cntid, cntlist);
if (!err) {
for (i = 0; i < (min(cntlist->num, 2048)); i++)
printf("[%4u]:%#x\n", i, cntlist->identifier[i]);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) cntid:%d\n",
nvme_status_to_string(err), err, cfg.cntid);
return err;
}
static int list_ns(int argc, char **argv)
{
const char *desc = "list-ns: for the specified device, show the "\
"namespace list (optionally starting with a given namespace)";
const char *namespace_id = "namespace to start after";
const char *all = "show all namespaces in the subsystem, whether attached or inactive";
int err, i;
__u32 ns_list[1024];
struct config {
__u32 namespace_id;
__u8 all;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"all", "", CFG_NONE, &defaults.all, no_argument, all},
{"a", "", CFG_NONE, &defaults.all, no_argument, all},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
err = nvme_identify_ns_list(fd, cfg.namespace_id, !!cfg.all, ns_list);
if (!err) {
for (i = 0; i < 1024; i++)
if (ns_list[i])
printf("[%4u]:%#x\n", i, ns_list[i]);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, cfg.namespace_id);
return err;
}
static int delete_ns(int argc, char **argv)
{
const char *desc = "delete-ns: delete the given namespace by "\
"sending a namespace management command to "\
"the given device. All controllers should be detached from "\
"the namespace prior to namespace deletion. A namespace ID "\
"becomes inactive when that namespace is detached (or, if "\
"the namespace is not already inactive, once deleted).";
const char *namespace_id = "namespace to delete";
int err;
struct config {
__u32 namespace_id;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
if (!cfg.namespace_id) {
fprintf(stderr, "%s: namespace-id parameter required\n",
commands[DELETE_NS].name);
return EINVAL;
}
get_dev(1, argc, argv);
err = nvme_ns_delete(fd, cfg.namespace_id);
if (!err)
printf("%s: Success, deleted nsid:%d\n", commands[DELETE_NS].name,
cfg.namespace_id);
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
fprintf(stderr, "system error:(%x)\n", err);
return err;
}
static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc)
{
char *name = commands[attach ? ATTACH_NS : DETACH_NS].name;
int err, num, i, list[2048];
__u16 ctrlist[2048];
const char *namespace_id = "namespace to attach";
const char *cont = "optional comma-sep controllers list";
struct config {
char *cntlist;
__u32 namespace_id;
};
struct config cfg;
const struct config defaults = {
.cntlist = "",
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"controllers", "LIST", CFG_STRING, &defaults.cntlist, required_argument, cont},
{"c", "LIST", CFG_STRING, &defaults.cntlist, required_argument, cont},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
if (!cfg.namespace_id) {
fprintf(stderr, "%s: namespace-id parameter required\n",
name);
return EINVAL;
}
num = argconfig_parse_comma_sep_array(cfg.cntlist,
list, 2047);
for (i = 0; i < num; i++)
ctrlist[i] = ((uint16_t)list[i]);
get_dev(1, argc, argv);
if (attach)
err = nvme_ns_attach_ctrls(fd, cfg.namespace_id, num, ctrlist);
else
err = nvme_ns_detach_ctrls(fd, cfg.namespace_id, num, ctrlist);
if (!err)
printf("%s: Success, nsid:%d\n", name, cfg.namespace_id);
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
fprintf(stderr, "system error:(%x)\n", err);
return err;
}
static int attach_ns(int argc, char **argv)
{
const char *desc = "attach-ns: attach the given namespace to the "\
"given controller or comma-sep list of controllers. ID of the "\
"given namespace becomes active upon attachment to a "\
"controller. A namespace must be attached to a controller "\
"before IO commands may be directed to that namespace.";
return nvme_attach_ns(argc, argv, 1, desc);
}
static int detach_ns(int argc, char **argv)
{
const char *desc = "detach-ns: detach the given namespace from the "\
"given controller; de-activates the given namespace's ID. A "\
"namespace must be attached to a controller before IO "\
"commands may be directed to that namespace.";
return nvme_attach_ns(argc, argv, 0, desc);
}
static int create_ns(int argc, char **argv)
{
const char *desc = "create-ns: send a namespace management command "\
"to the specified device to create a namespace with the given "\
"parameters. The next available namespace ID is used for the "\
"create operation. Note that create-ns does not attach the "\
"namespace to a controller, the attach-ns command is needed.";
int err = 0;
__u32 nsid;
struct config {
__u64 nsze;
__u64 ncap;
__u8 flbas;
__u8 dps;
__u8 nmic;
};
struct config cfg;
const struct config defaults = {
};
const char *nsze = "size of ns";
const char *ncap = "capacity of ns";
const char *flbas = "FLBA size";
const char *dps = "data protection capabilities";
const char *nmic = "multipath and sharing capabilities";
const struct argconfig_commandline_options command_line_options[] = {
{"nsze", "NUM", CFG_LONG_SUFFIX, &defaults.nsze, required_argument, nsze},
{"s", "NUM", CFG_LONG_SUFFIX, &defaults.nsze, required_argument, nsze},
{"ncap", "NUM", CFG_LONG_SUFFIX, &defaults.ncap, required_argument, ncap},
{"c", "NUM", CFG_LONG_SUFFIX, &defaults.ncap, required_argument, ncap},
{"flbas", "NUM", CFG_BYTE, &defaults.flbas, required_argument, flbas},
{"f", "NUM", CFG_BYTE, &defaults.flbas, required_argument, flbas},
{"dps", "NUM", CFG_BYTE, &defaults.dps, required_argument, dps},
{"d", "NUM", CFG_BYTE, &defaults.dps, required_argument, dps},
{"nmic", "NUM", CFG_BYTE, &defaults.nmic, required_argument, nmic},
{"m", "NUM", CFG_BYTE, &defaults.nmic, required_argument, nmic},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
get_dev(1, argc, argv);
err = nvme_ns_create(fd, cfg.nsze, cfg.ncap, cfg.flbas, cfg.dps, cfg.nmic, &nsid);
if (!err)
printf("%s: Success, created nsid:%d\n", commands[CREATE_NS].name,
nsid);
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static char *nvme_char_from_block(char *block)
{
char slen[16];
unsigned len;
if (strncmp("nvme", block, 4)) {
fprintf(stderr,"Device %s is not a nvme device.", block);
exit(-1);
}
sscanf(block,"nvme%d", &len);
sprintf(slen,"%d", len);
block[4+strlen(slen)] = 0;
return block;
}
static void get_registers(struct nvme_bar **bar, unsigned char_only)
{
int pci_fd;
char *base, path[512];
void *membase;
if (char_only && !S_ISCHR(nvme_stat.st_mode)) {
fprintf(stderr, "%s is not a character device\n", devicename);
exit(ENODEV);
}
base = nvme_char_from_block(basename(devicename));
sprintf(path, "/sys/class/nvme/%s/device/resource0", base);
pci_fd = open(path, O_RDONLY);
if (pci_fd < 0) {
sprintf(path, "/sys/class/misc/%s/device/resource0", base);
pci_fd = open(path, O_RDONLY);
}
if (pci_fd < 0) {
fprintf(stderr, "%s did not find a pci resource\n", devicename);
exit(ENODEV);
}
membase = mmap(0, getpagesize(), PROT_READ, MAP_SHARED, pci_fd, 0);
if (!membase) {
fprintf(stderr, "%s failed to map\n", devicename);
exit(ENODEV);
}
*bar = membase;
}
struct list_item {
char node[1024];
struct nvme_id_ctrl ctrl;
int nsid;
struct nvme_id_ns ns;
unsigned block;
__le32 ver;
};
#ifdef LIBUDEV_EXISTS
/* For pre NVMe 1.2 devices we must get the version from the BAR, not the
* ctrl_id.*/
static void get_version(struct list_item* list_item)
{
struct nvme_bar *bar;
list_item->ver = list_item->ctrl.ver;
if (list_item->ctrl.ver)
return;
get_registers(&bar, 0);
list_item->ver = bar->vs;
}
static void print_list_item(struct list_item list_item)
{
double nsze = list_item.ns.nsze;
double nuse = list_item.ns.nuse;
long long int lba = list_item.ns.lbaf[(list_item.ns.flbas & 0x0f)].ds;
lba = (1 << lba);
nsze *= lba;
nuse *= lba;
const char *s_suffix = suffix_si_get(&nsze);
const char *u_suffix = suffix_si_get(&nuse);
const char *l_suffix = suffix_binary_get(&lba);
char usage[128];
sprintf(usage,"%6.2f %2sB / %6.2f %2sB", nuse, u_suffix,
nsze, s_suffix);
char format[128];
sprintf(format,"%3.0f %2sB + %2d B", (double)lba, l_suffix,
list_item.ns.lbaf[(list_item.ns.flbas & 0x0f)].ms);
char version[128];
sprintf(version,"%d.%d", (list_item.ver >> 16),
(list_item.ver >> 8) & 0xff);
fprintf(stdout, "%-16s %-20.20s %-8s %-8d %-26s %-16s %-.8s\n", list_item.node,
list_item.ctrl.mn, version, list_item.nsid, usage, format, list_item.ctrl.fr);
}
static void print_list_items(struct list_item *list_items, unsigned len)
{
unsigned i;
fprintf(stdout,"%-16s %-20s %-8s %-8s %-26s %-16s %-8s\n",
"Node","Model","Version","Namepace", "Usage", "Format", "FW Rev");
fprintf(stdout,"%-16s %-20s %-8s %-8s %-26s %-16s %-8s\n",
"----------------","--------------------","--------","--------",
"--------------------------","----------------","--------");
for (i = 0 ; i < len ; i++)
print_list_item(list_items[i]);
}
#else
static int list(int argc, char **argv)
{
fprintf(stderr,"nvme-list: libudev not detected, install and rebuild.\n");
return -1;
}
#endif
#ifdef LIBUDEV_EXISTS
#define MAX_LIST_ITEMS 256
static int list(int argc, char **argv)
{
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
struct list_item list_items[MAX_LIST_ITEMS];
unsigned count=0;
udev = udev_new();
if (!udev) {
perror("nvme-list: Cannot create udev context.");
return errno;
}
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "block");
udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
int err;
const char *path, *node;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
node = udev_device_get_devnode(dev);
if (strstr(node,"nvme")!=NULL){
open_dev(node);
err = nvme_identify_ctrl(fd, &list_items[count].ctrl);
if (err > 0)
return err;
list_items[count].nsid = nvme_get_nsid(fd);
err = nvme_identify_ns(fd, list_items[count].nsid,
0, &list_items[count].ns);
if (err > 0)
return err;
strcpy(list_items[count].node, node);
list_items[count].block = S_ISBLK(nvme_stat.st_mode);
get_version(&list_items[count]);
count++;
}
}
udev_enumerate_unref(enumerate);
udev_unref(udev);
if (count)
print_list_items(list_items, count);
else
fprintf(stdout,"No NVMe devices detected.\n");
return 0;
}
#endif
static int id_ctrl(int argc, char **argv)
{
const char *desc = "id-ctrl: send an Identify Controller command to "\
"the given device and report information about the specified "\
"controller in human-readable or "\
"binary format. Can also return binary vendor-specific "\
"controller attributes.";
const char *vendor_specific = "dump binary vendor infos";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
int err;
unsigned int flags = 0;
struct nvme_id_ctrl ctrl;
struct config {
__u8 vendor_specific;
__u8 raw_binary;
__u8 human_readable;
};
struct config cfg;
const struct config defaults = {
};
const struct argconfig_commandline_options command_line_options[] = {
{"vendor-specific", "", CFG_NONE, &defaults.vendor_specific, no_argument, vendor_specific},
{"v", "", CFG_NONE, &defaults.vendor_specific, no_argument, vendor_specific},
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"human-readable", "", CFG_NONE, &defaults.human_readable, no_argument, human_readable},
{"H", "", CFG_NONE, &defaults.human_readable, no_argument, human_readable},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
if (cfg.vendor_specific)
flags |= VS;
if (cfg.human_readable)
flags |= HUMAN;
get_dev(1, argc, argv);
err = nvme_identify_ctrl(fd, &ctrl);
if (!err) {
if (cfg.raw_binary) {
d_raw((unsigned char *)&ctrl, sizeof(ctrl));
} else {
printf("NVME Identify Controller:\n");
show_nvme_id_ctrl(&ctrl, flags);
}
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static int id_ns(int argc, char **argv)
{
const char *desc = "id-ns: send an Identify Namespace command to the "\
"given device, returns properties of the specified namespace "\
"in either human-readable or binary format. Can also return "\
"binary vendor-specific namespace attributes.";
const char *vendor_specific = "dump binary vendor infos";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
const char *namespace_id = "name of desired namespace";
struct nvme_id_ns ns;
int err;
unsigned int flags = 0;
struct config {
__u32 namespace_id;
__u8 vendor_specific;
__u8 raw_binary;
__u8 human_readable;
};
struct config cfg;
const struct config defaults = {
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"n", "NUM", CFG_POSITIVE, &defaults.namespace_id, required_argument, namespace_id},
{"vendor-specific", "", CFG_NONE, &defaults.vendor_specific, no_argument, vendor_specific},
{"v", "", CFG_NONE, &defaults.vendor_specific, no_argument, vendor_specific},
{"raw-binary", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"b", "", CFG_NONE, &defaults.raw_binary, no_argument, raw_binary},
{"human-readable", "", CFG_NONE, &defaults.human_readable, no_argument, human_readable},
{"H", "", CFG_NONE, &defaults.human_readable, no_argument, human_readable},
{0}
};
argconfig_parse(argc, argv, desc, command_line_options,
&defaults, &cfg, sizeof(cfg));
if (cfg.vendor_specific)
flags |= VS;
if (cfg.human_readable)
flags |= HUMAN;
get_dev(1, argc, argv);
if (!cfg.namespace_id) {
if (!S_ISBLK(nvme_stat.st_mode)) {
fprintf(stderr,
"%s: non-block device requires namespace-id param\n",
devicename);
exit(ENOTBLK);
}
cfg.namespace_id = nvme_get_nsid(fd);
if (cfg.namespace_id <= 0) {
perror(devicename);
exit(errno);
}
}