forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme.c
3568 lines (3272 loc) · 91.3 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>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 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 "linux/nvme.h"
#include "src/suffix.h"
#define min(x, y) (x) > (y) ? (y) : (x)
#define FORMAT_TIMEOUT 120000 // 120 seconds
static int fd;
static struct stat nvme_stat;
static const char *devicename;
#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 detached namespace from the controller", delete_ns) \
ENTRY(ATTACH_NS, "attach-ns", "Attaches a created 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, show results", 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", "Returns entries from generic log in raw fmt", get_log) \
ENTRY(GET_FW_LOG, "fw-log", "Retrieve FW Log in either hex or raw fmt", get_fw_log) \
ENTRY(GET_SMART_LOG, "smart-log", "Retrieve SMART log, show in hex or raw fmt", 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 entries from Error Log in hex or raw fmt", get_error_log) \
ENTRY(GET_FEATURE, "get-feature", "Read and display controller's specified feature 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", "Obtain results of one or more previous same-protocol Security Sends", 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(FLUSH, "flush", "Submit a Flush command, return results", flush) \
ENTRY(COMPARE, "compare", "Submit a Comapre 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(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 void usage(char *cmd)
{
fprintf(stdout, "usage: %s <command> [<device>] [<args>]\n", cmd);
}
static void command_help(struct option *opts, int num_opts, char *name)
{
fprintf(stderr, "Usage: nvme %s /dev/nvmeX", name);
for (int j = 0; j < num_opts; ++j) {
if (opts[j].name != 0) {
if (opts[j].has_arg == no_argument)
fprintf(stderr, " [ --%s | -%s ]",
opts[j].name, (char *)&opts[j].val);
else
fprintf(stderr, " [ --%s= | -%s ] ARG",
opts[j].name, (char *)&opts[j].val);
}
if (j % 2 == 0)
fprintf(stderr, "\n");
}
fprintf(stderr, "\nAlso try 'man nvme-%s'\n", name);
}
static void general_help()
{
unsigned i;
usage("nvme");
printf("\n");
printf("'<device>' / '/dev/nvmeX' may be either an NVMe character "\
"device (ex: /dev/nvme0)\n or an nvme block device (ex: /d"\
"ev/nvme0n1)\n\n");
printf("The following are all implemented sub-commands:\n");
for (i = 0; i < NUM_COMMANDS; i++)
printf(" %-*s %s\n", 15, commands[i].name, commands[i].help);
printf("\n");
printf("Try 'nvme help <command>' for more information on a specific command.\n");
}
static int help(int argc, char **argv)
{
struct command *c;
if (argc == 1)
general_help();
else {
for (unsigned i = 0; i < NUM_COMMANDS; i++) {
c = &commands[i];
if (strcmp(c->name, argv[1])) continue;
exit(execlp("man", "man", c->man, (char *) NULL));
}
fprintf(stderr, "no man entry for NVMe sub-command '%s'\n", argv[1]);
}
return 0;
}
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 = 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 void show_error_log(struct nvme_error_log_page *err_log, int entries)
{
int i;
printf("Error Log Entries for device: %s entries: %d\n", devicename,
entries);
printf(".................\n");
for (i = 0; i < entries; i++) {
printf(" Entry[%2d] \n", i);
printf(".................\n");
printf("error_count : %"PRIu64"\n", (uint64_t)le64toh(err_log[i].error_count));
printf("sqid : %d\n", err_log[i].sqid);
printf("cmdid : %#x\n", err_log[i].cmdid);
printf("status_field : %#x\n", err_log[i].status_field);
printf("parm_err_loc : %#x\n", err_log[i].parm_error_location);
printf("lba : %#"PRIx64"\n",(uint64_t)le64toh(err_log[i].lba));
printf("nsid : %d\n", err_log[i].nsid);
printf("vs : %d\n", err_log[i].vs);
printf(".................\n");
}
}
static void show_nvme_resv_report(struct nvme_reservation_status *status)
{
int i, regctl;
regctl = status->regctl[0] | (status->regctl[1] << 8);
printf("\nNVME Reservation status:\n\n");
printf("gen : %d\n", le32toh(status->gen));
printf("regctl : %d\n", regctl);
printf("rtype : %d\n", status->rtype);
printf("ptpls : %d\n", status->ptpls);
for (i = 0; i < regctl; i++) {
printf("regctl[%d] :\n", i);
printf(" cntlid : %x\n", le16toh(status->regctl_ds[i].cntlid));
printf(" rcsts : %x\n", status->regctl_ds[i].rcsts);
printf(" hostid : %"PRIx64"\n", (uint64_t)le64toh(status->regctl_ds[i].hostid));
printf(" rkey : %"PRIx64"\n", (uint64_t)le64toh(status->regctl_ds[i].rkey));
}
printf("\n");
}
static char *fw_to_string(__u64 fw)
{
static char ret[9];
char *c = (char *)&fw;
int i;
for (i = 0; i < 8; i++)
ret[i] = c[i] >= '!' && c[i] <= '~' ? c[i] : '.';
ret[i] = '\0';
return ret;
}
static void show_fw_log(struct nvme_firmware_log_page *fw_log)
{
int i;
printf("Firmware Log for device: %s\n", devicename);
printf("afi : %#x\n", fw_log->afi);
for (i = 0; i < 7; i++)
if (fw_log->frs[i])
printf("frs%d : %#016"PRIx64" (%s)\n", i + 1, (uint64_t)fw_log->frs[i],
fw_to_string(fw_log->frs[i]));
}
static long double int128_to_double(__u8 *data)
{
int i;
long double result = 0;
for (i = 0; i < 16; i++) {
result *= 256;
result += data[15 - i];
}
return result;
}
static unsigned long int48_to_long(__u8 *data)
{
int i;
long result = 0;
for (i = 0; i < 6; i++) {
result *= 256;
result += data[5 - i];
}
return result;
}
static void show_smart_log(struct nvme_smart_log *smart, unsigned int nsid)
{
/* convert temperature from Kelvin to Celsius */
unsigned int temperature = ((smart->temperature[1] << 8) |
smart->temperature[0]) - 273;
printf("Smart Log for NVME device: %s namespace-id: %x\n", devicename, nsid);
printf("critical_warning : %#x\n", smart->critical_warning);
printf("temperature : %u C\n", temperature);
printf("available_spare : %u%%\n", smart->avail_spare);
printf("available_spare_threshold : %u%%\n", smart->spare_thresh);
printf("percentage_used : %u%%\n", smart->percent_used);
printf("data_units_read : %'.0Lf\n",
int128_to_double(smart->data_units_read));
printf("data_units_written : %'.0Lf\n",
int128_to_double(smart->data_units_written));
printf("host_read_commands : %'.0Lf\n",
int128_to_double(smart->host_reads));
printf("host_write_commands : %'.0Lf\n",
int128_to_double(smart->host_writes));
printf("controller_busy_time : %'.0Lf\n",
int128_to_double(smart->ctrl_busy_time));
printf("power_cycles : %'.0Lf\n",
int128_to_double(smart->power_cycles));
printf("power_on_hours : %'.0Lf\n",
int128_to_double(smart->power_on_hours));
printf("unsafe_shutdowns : %'.0Lf\n",
int128_to_double(smart->unsafe_shutdowns));
printf("media_errors : %'.0Lf\n",
int128_to_double(smart->media_errors));
printf("num_err_log_entries : %'.0Lf\n",
int128_to_double(smart->num_err_log_entries));
}
static void show_additional_smart_log(struct nvme_additional_smart_log *smart, unsigned int nsid)
{
printf("Additional Smart Log for NVME device: %s namespace-id: %x\n", devicename, nsid);
printf("key normalized raw\n");
printf("program_fail_count : %3d%% %lu\n",
smart->program_fail_cnt.norm,
int48_to_long(smart->program_fail_cnt.raw));
printf("erase_fail_count : %3d%% %lu\n",
smart->erase_fail_cnt.norm,
int48_to_long(smart->erase_fail_cnt.raw));
printf("wear_leveling : %3d%% min: %u, max: %u, avg: %u\n",
smart->wear_leveling_cnt.norm,
smart->wear_leveling_cnt.wear_level.min,
smart->wear_leveling_cnt.wear_level.max,
smart->wear_leveling_cnt.wear_level.avg);
printf("end_to_end_error_detection_count: %3d%% %lu\n",
smart->e2e_err_cnt.norm,
int48_to_long(smart->e2e_err_cnt.raw));
printf("crc_error_count : %3d%% %lu\n",
smart->crc_err_cnt.norm,
int48_to_long(smart->crc_err_cnt.raw));
printf("timed_workload_media_wear : %3d%% %.3f%%\n",
smart->timed_workload_media_wear.norm,
((float)int48_to_long(smart->timed_workload_media_wear.raw)) / 1024);
printf("timed_workload_host_reads : %3d%% %lu%%\n",
smart->timed_workload_host_reads.norm,
int48_to_long(smart->timed_workload_host_reads.raw));
printf("timed_workload_timer : %3d%% %lu min\n",
smart->timed_workload_timer.norm,
int48_to_long(smart->timed_workload_timer.raw));
printf("thermal_throttle_status : %3d%% %u%%, cnt: %u\n",
smart->thermal_throttle_status.norm,
smart->thermal_throttle_status.thermal_throttle.pct,
smart->thermal_throttle_status.thermal_throttle.count);
printf("retry_buffer_overflow_count : %3d%% %lu\n",
smart->retry_buffer_overflow_cnt.norm,
int48_to_long(smart->retry_buffer_overflow_cnt.raw));
printf("pll_lock_loss_count : %3d%% %lu\n",
smart->pll_lock_loss_cnt.norm,
int48_to_long(smart->pll_lock_loss_cnt.raw));
printf("nand_bytes_written : %3d%% sectors: %lu\n",
smart->nand_bytes_written.norm,
int48_to_long(smart->nand_bytes_written.raw));
printf("host_bytes_written : %3d%% sectors: %lu\n",
smart->host_bytes_written.norm,
int48_to_long(smart->host_bytes_written.raw));
}
char* nvme_feature_to_string(int feature)
{
switch (feature)
{
case NVME_FEAT_ARBITRATION: return "Arbitration";
case NVME_FEAT_POWER_MGMT: return "Power Management";
case NVME_FEAT_LBA_RANGE: return "LBA Range";
case NVME_FEAT_TEMP_THRESH: return "Temperature Threshold";
case NVME_FEAT_ERR_RECOVERY: return "Error Recovery";
case NVME_FEAT_VOLATILE_WC: return "Volatile Write Cache";
case NVME_FEAT_NUM_QUEUES: return "Number of Queues";
case NVME_FEAT_IRQ_COALESCE: return "IRQ Coalescing";
case NVME_FEAT_IRQ_CONFIG: return "IRQ Configuration";
case NVME_FEAT_WRITE_ATOMIC: return "Write Atomicity";
case NVME_FEAT_ASYNC_EVENT: return "Async Event";
case NVME_FEAT_SW_PROGRESS: return "Software Progress";
default: return "Unknown";
}
}
static const char *nvme_status_to_string(__u32 status)
{
switch (status & 0x3ff) {
case NVME_SC_SUCCESS: return "SUCCESS";
case NVME_SC_INVALID_OPCODE: return "INVALID_OPCODE";
case NVME_SC_INVALID_FIELD: return "INVALID_FIELD";
case NVME_SC_CMDID_CONFLICT: return "CMDID_CONFLICT";
case NVME_SC_DATA_XFER_ERROR: return "DATA_XFER_ERROR";
case NVME_SC_POWER_LOSS: return "POWER_LOSS";
case NVME_SC_INTERNAL: return "INTERNAL";
case NVME_SC_ABORT_REQ: return "ABORT_REQ";
case NVME_SC_ABORT_QUEUE: return "ABORT_QUEUE";
case NVME_SC_FUSED_FAIL: return "FUSED_FAIL";
case NVME_SC_FUSED_MISSING: return "FUSED_MISSING";
case NVME_SC_INVALID_NS: return "INVALID_NS";
case NVME_SC_CMD_SEQ_ERROR: return "CMD_SEQ_ERROR";
case NVME_SC_LBA_RANGE: return "LBA_RANGE";
case NVME_SC_CAP_EXCEEDED: return "CAP_EXCEEDED";
case NVME_SC_NS_NOT_READY: return "NS_NOT_READY";
case NVME_SC_CQ_INVALID: return "CQ_INVALID";
case NVME_SC_QID_INVALID: return "QID_INVALID";
case NVME_SC_QUEUE_SIZE: return "QUEUE_SIZE";
case NVME_SC_ABORT_LIMIT: return "ABORT_LIMIT";
case NVME_SC_ABORT_MISSING: return "ABORT_MISSING";
case NVME_SC_ASYNC_LIMIT: return "ASYNC_LIMIT";
case NVME_SC_FIRMWARE_SLOT: return "FIRMWARE_SLOT";
case NVME_SC_FIRMWARE_IMAGE: return "FIRMWARE_IMAGE";
case NVME_SC_INVALID_VECTOR: return "INVALID_VECTOR";
case NVME_SC_INVALID_LOG_PAGE: return "INVALID_LOG_PAGE";
case NVME_SC_INVALID_FORMAT: return "INVALID_FORMAT";
case NVME_SC_BAD_ATTRIBUTES: return "BAD_ATTRIBUTES";
case NVME_SC_WRITE_FAULT: return "WRITE_FAULT";
case NVME_SC_READ_ERROR: return "READ_ERROR";
case NVME_SC_GUARD_CHECK: return "GUARD_CHECK";
case NVME_SC_APPTAG_CHECK: return "APPTAG_CHECK";
case NVME_SC_REFTAG_CHECK: return "REFTAG_CHECK";
case NVME_SC_COMPARE_FAILED: return "COMPARE_FAILED";
case NVME_SC_ACCESS_DENIED: return "ACCESS_DENIED";
default: return "Unknown";
}
}
static int identify(int namespace, void *ptr, __u32 cns)
{
struct nvme_admin_cmd cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = nvme_admin_identify;
cmd.nsid = namespace;
cmd.addr = (unsigned long)ptr;
cmd.data_len = 4096;
cmd.cdw10 = cns;
return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
}
static int nvme_get_log(void *log_addr, __u32 data_len, __u32 dw10, __u32 nsid)
{
struct nvme_admin_cmd cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = nvme_admin_get_log_page;
cmd.addr = (unsigned long)log_addr;
cmd.data_len = data_len;
cmd.cdw10 = dw10;
cmd.nsid = nsid;
return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
}
static void d_raw(unsigned char *buf, unsigned len)
{
unsigned i;
for (i = 0; i < len; i++)
putchar(*(buf+i));
}
static void d(unsigned char *buf, int len, int width, int group)
{
int i, offset = 0, line_done = 0;
char ascii[width + 1];
printf(" ");
for (i = 0; i <= 15; i++)
printf("%3x", i);
for (i = 0; i < len; i++) {
line_done = 0;
if (i % width == 0)
fprintf(stdout, "\n%04x:", offset);
if (i % group == 0)
fprintf(stdout, " %02x", buf[i]);
else
fprintf(stdout, "%02x", buf[i]);
ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
if (((i + 1) % width) == 0) {
ascii[i % width + 1] = '\0';
fprintf(stdout, " \"%.*s\"", width, ascii);
offset += width;
line_done = 1;
}
}
if (!line_done) {
unsigned b = width - (i % width);
ascii[i % width + 1] = '\0';
fprintf(stdout, " %*s \"%.*s\"",
2 * b + b / group + (b % group ? 1 : 0), "",
width, ascii);
}
fprintf(stdout, "\n");
}
static void show_nvme_id_ctrl_cmic(__u8 cmic)
{
__u8 rsvd = (cmic & 0xF8) >> 3;
__u8 sriov = (cmic & 0x4) >> 2;
__u8 mctl = (cmic & 0x2) >> 1;
__u8 mp = cmic & 0x1;
if (rsvd)
printf(" [7:3] : %#x\tReserved\n", rsvd);
printf(" [2:2] : %#x\t%s\n", sriov, sriov ? "SR-IOV" : "PCI");
printf(" [1:1] : %#x\t%s Controller\n",
mctl, mctl ? "Multi" : "Single");
printf(" [0:0] : %#x\t%s Port\n", mp, mp ? "Multi" : "Single");
printf("\n");
}
static void show_nvme_id_ctrl_oaes(__le32 oaes)
{
__le32 rsvd0 = (oaes & 0xFFFFFE00) >> 9;
__le32 nace = (oaes & 0x100) >> 8;
__le32 rsvd1 = oaes & 0xFF;
if (rsvd0)
printf(" [31:9] : %#x\tReserved\n", rsvd0);
printf(" [8:8] : %#x\tNamespace Attribute Changed Event %sSupported\n",
nace, nace ? "" : "Not ");
if (rsvd1)
printf(" [7:0] : %#x\tReserved\n", rsvd1);
printf("\n");
}
static void show_nvme_id_ctrl_oacs(__le16 oacs)
{
__le16 rsvd = (oacs & 0xFFF0) >> 4;
__le16 nsm = (oacs & 0x8) >> 3;
__le16 fwc = (oacs & 0x4) >> 2;
__le16 fmt = (oacs & 0x2) >> 1;
__le16 sec = oacs & 0x1;
if (rsvd)
printf(" [15:4] : %#x\tReserved\n", rsvd);
printf(" [3:3] : %#x\tNS Management and Attachment %sSupported\n",
nsm, nsm ? "" : "Not ");
printf(" [2:2] : %#x\tFW Commit and Download %sSupported\n",
fwc, fwc ? "" : "Not ");
printf(" [1:1] : %#x\tFormat NVM %sSupported\n",
fmt, fmt ? "" : "Not ");
printf(" [0:0] : %#x\tSec. Send and Receive %sSupported\n",
sec, sec ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl_frmw(__u8 frmw)
{
__u8 rsvd = (frmw & 0xE0) >> 5;
__u8 fawr = (frmw & 0x10) >> 4;
__u8 nfws = (frmw & 0xE) >> 1;
__u8 s1ro = frmw & 0x1;
if (rsvd)
printf(" [7:5] : %#x\tReserved\n", rsvd);
printf(" [4:4] : %#x\tFirmware Activate Without Reset %sSupported\n",
fawr, fawr ? "" : "Not ");
printf(" [3:1] : %#x\tNumber of Firmware Slots\n", nfws);
printf(" [0:0] : %#x\tFirmware Slot 1 Read%s\n",
s1ro, s1ro ? "-Only" : "/Write");
printf("\n");
}
static void show_nvme_id_ctrl_lpa(__u8 lpa)
{
__u8 rsvd = (lpa & 0xFC) >> 2;
__u8 celp = (lpa & 0x2) >> 1;
__u8 smlp = lpa & 0x1;
if (rsvd)
printf(" [7:2] : %#x\tReserved\n", rsvd);
printf(" [1:1] : %#x\tCommand Effects Log Page %sSupported\n",
celp, celp ? "" : "Not ");
printf(" [0:0] : %#x\tSMART/Health Log Page per NS %sSupported\n",
smlp, smlp ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl_avscc(__u8 avscc)
{
__u8 rsvd = (avscc & 0xFE) >> 1;
__u8 fmt = avscc & 0x1;
if (rsvd)
printf(" [7:1] : %#x\tReserved\n", rsvd);
printf(" [0:0] : %#x\tAdmin Vendor Specific Commands uses %s Format\n",
fmt, fmt ? "NVMe" : "Vendor Specific");
printf("\n");
}
static void show_nvme_id_ctrl_apsta(__u8 apsta)
{
__u8 rsvd = (apsta & 0xFE) >> 1;
__u8 apst = apsta & 0x1;
if (rsvd)
printf(" [7:1] : %#x\tReserved\n", rsvd);
printf(" [0:0] : %#x\tAutonomous Power State Transitions %sSupported\n",
apst, apst ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl_rpmbs(__le32 rpmbs)
{
__le32 asz = (rpmbs & 0xFF000000) >> 24;
__le32 tsz = (rpmbs & 0xFF0000) >> 16;
__le32 rsvd = (rpmbs & 0xFFC0) >> 6;
__le32 auth = (rpmbs & 0x38) >> 3;
__le32 rpmb = rpmbs & 0x3;
printf(" [31:24]: %#x\tAccess Size\n", asz);
printf(" [23:16]: %#x\tTotal Size\n", tsz);
if (rsvd)
printf(" [15:6] : %#x\tReserved\n", rsvd);
printf(" [5:3] : %#x\tAuthentication Method\n", auth);
printf(" [2:0] : %#x\tNumber of RPMB Units\n", rpmb);
printf("\n");
}
static void show_nvme_id_ctrl_sqes(__u8 sqes)
{
__u8 msqes = (sqes & 0xF0) >> 4;
__u8 rsqes = sqes & 0xF;
printf(" [7:4] : %#x\tMax SQ Entry Size (%d)\n", msqes, 1 << msqes);
printf(" [3:0] : %#x\tMin SQ Entry Size (%d)\n", rsqes, 1 << rsqes);
printf("\n");
}
static void show_nvme_id_ctrl_cqes(__u8 cqes)
{
__u8 mcqes = (cqes & 0xF0) >> 4;
__u8 rcqes = cqes & 0xF;
printf(" [7:4] : %#x\tMax CQ Entry Size (%d)\n", mcqes, 1 << mcqes);
printf(" [3:0] : %#x\tMin CQ Entry Size (%d)\n", rcqes, 1 << rcqes);
printf("\n");
}
static void show_nvme_id_ctrl_oncs(__le16 oncs)
{
__le16 rsvd = (oncs & 0xFFC0) >> 6;
__le16 resv = (oncs & 0x20) >> 5;
__le16 save = (oncs & 0x10) >> 4;
__le16 wzro = (oncs & 0x8) >> 3;
__le16 dsms = (oncs & 0x4) >> 2;
__le16 wunc = (oncs & 0x2) >> 1;
__le16 cmp = oncs & 0x1;
if (rsvd)
printf(" [15:6] : %#x\tReserved\n", rsvd);
printf(" [5:5] : %#x\tReservations %sSupported\n",
resv, resv ? "" : "Not ");
printf(" [4:4] : %#x\tSave and Select %sSupported\n",
save, save ? "" : "Not ");
printf(" [3:3] : %#x\tWrite Zeroes %sSupported\n",
wzro, wzro ? "" : "Not ");
printf(" [2:2] : %#x\tData Set Management %sSupported\n",
dsms, dsms ? "" : "Not ");
printf(" [1:1] : %#x\tWrite Uncorrectable %sSupported\n",
wunc, wunc ? "" : "Not ");
printf(" [0:0] : %#x\tCompare %sSupported\n",
cmp, cmp ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl_fuses(__le16 fuses)
{
__le16 rsvd = (fuses & 0xFE) >> 1;
__le16 cmpw = fuses & 0x1;
if (rsvd)
printf(" [15:1] : %#x\tReserved\n", rsvd);
printf(" [0:0] : %#x\tFused Compare and Write %sSupported\n",
cmpw, cmpw ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl_fna(__u8 fna)
{
__u8 rsvd = (fna & 0xF8) >> 3;
__u8 cese = (fna & 0x4) >> 2;
__u8 cens = (fna & 0x2) >> 1;
__u8 fmns = fna & 0x1;
if (rsvd)
printf(" [7:3] : %#x\tReserved\n", rsvd);
printf(" [2:2] : %#x\tCrypto Erase %sSupported as part of Secure Erase\n",
cese, cese ? "" : "Not ");
printf(" [1:1] : %#x\tCrypto Erase Applies to %s Namespace(s)\n",
cens, cens ? "All" : "Single");
printf(" [0:0] : %#x\tFormat Applies to %s Namespace(s)\n",
fmns, fmns ? "All" : "Single");
printf("\n");
}
static void show_nvme_id_ctrl_vwc(__u8 vwc)
{
__u8 rsvd = (vwc & 0xFE) >> 1;
__u8 vwcp = vwc & 0x1;
if (rsvd)
printf(" [7:3] : %#x\tReserved\n", rsvd);
printf(" [0:0] : %#x\tVolatile Write Cache %sPresent\n",
vwcp, vwcp ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl_nvscc(__u8 nvscc)
{
__u8 rsvd = (nvscc & 0xFE) >> 1;
__u8 fmt = nvscc & 0x1;
if (rsvd)
printf(" [7:1] : %#x\tReserved\n", rsvd);
printf(" [0:0] : %#x\tNVM Vendor Specific Commands uses %s Format\n",
fmt, fmt ? "NVMe" : "Vendor Specific");
printf("\n");
}
static void show_nvme_id_ctrl_sgls(__le32 sgls)
{
__le32 rsvd0 = (sgls & 0xFFF80000) >> 19;
__le32 sglltb = (sgls & 0x40000) >> 18;
__le32 bacmdb = (sgls & 0x20000) >> 17;
__le32 bbs = (sgls & 0x10000) >> 16;
__le32 rsvd1 = (sgls & 0xFFFE) >> 1;
__le32 sglsp = sgls & 0x1;
if (rsvd0)
printf(" [31:19]: %#x\tReserved\n", rsvd0);
if (sglsp || (!sglsp && sglltb))
printf(" [18:18]: %#x\tSGL Length Larger than Buffer %sSupported\n",
sglltb, sglltb ? "" : "Not ");
if (sglsp || (!sglsp && bacmdb))
printf(" [17:17]: %#x\tByte-Aligned Contig. MD Buffer %sSupported\n",
bacmdb, bacmdb ? "" : "Not ");
if (sglsp || (!sglsp && bbs))
printf(" [16:16]: %#x\tSGL Bit-Bucket %sSupported\n",
bbs, bbs ? "" : "Not ");
if (rsvd1)
printf(" [15:1] : %#x\tReserved\n", rsvd1);
printf(" [0:0] : %#x\tScatter-Gather Lists %sSupported\n",
sglsp, sglsp ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ctrl(struct nvme_id_ctrl *ctrl, int vs, int human)
{
int i;
char sn[sizeof(ctrl->sn) + 1];
char mn[sizeof(ctrl->mn) + 1];
char fr[sizeof(ctrl->fr) + 1];
memcpy(sn, ctrl->sn, sizeof(ctrl->sn));
memcpy(mn, ctrl->mn, sizeof(ctrl->mn));
memcpy(fr, ctrl->fr, sizeof(ctrl->fr));
sn[sizeof(ctrl->sn)] = '\0';
mn[sizeof(ctrl->mn)] = '\0';
fr[sizeof(ctrl->fr)] = '\0';
printf("NVME Identify Controller:\n");
printf("vid : %#x\n", ctrl->vid);
printf("ssvid : %#x\n", ctrl->ssvid);
printf("sn : %s\n", sn);
printf("mn : %s\n", mn);
printf("fr : %s\n", fr);
printf("rab : %d\n", ctrl->rab);
printf("ieee : %02x%02x%02x\n",
ctrl->ieee[0], ctrl->ieee[1], ctrl->ieee[2]);
printf("cmic : %#x\n", ctrl->cmic);
if (human)
show_nvme_id_ctrl_cmic(ctrl->cmic);
printf("mdts : %d\n", ctrl->mdts);
printf("cntlid : %x\n", ctrl->cntlid);
printf("ver : %x\n", ctrl->ver);
printf("rtd3r : %x\n", ctrl->rtd3r);
printf("rtd3e : %x\n", ctrl->rtd3e);
printf("oaes : %#x\n", ctrl->oaes);
if (human)
show_nvme_id_ctrl_oaes(ctrl->oaes);
printf("oacs : %#x\n", ctrl->oacs);
if (human)
show_nvme_id_ctrl_oacs(ctrl->oacs);
printf("acl : %d\n", ctrl->acl);
printf("aerl : %d\n", ctrl->aerl);
printf("frmw : %#x\n", ctrl->frmw);
if (human)
show_nvme_id_ctrl_frmw(ctrl->frmw);
printf("lpa : %#x\n", ctrl->lpa);
if (human)
show_nvme_id_ctrl_lpa(ctrl->lpa);
printf("elpe : %d\n", ctrl->elpe);
printf("npss : %d\n", ctrl->npss);
printf("avscc : %#x\n", ctrl->avscc);
if (human)
show_nvme_id_ctrl_avscc(ctrl->avscc);
printf("apsta : %#x\n", ctrl->apsta);
if (human)
show_nvme_id_ctrl_apsta(ctrl->apsta);
printf("wctemp : %d\n", ctrl->wctemp);
printf("cctemp : %d\n", ctrl->cctemp);
printf("mtfa : %d\n", ctrl->mtfa);
printf("hmmin : %d\n", ctrl->hmmin);
printf("tnvmcap : %.0Lf\n", int128_to_double(ctrl->tnvmcap));
printf("unvmcap : %.0Lf\n", int128_to_double(ctrl->unvmcap));
printf("rpmbs : %#x\n", ctrl->rpmbs);
if (human)
show_nvme_id_ctrl_rpmbs(ctrl->rpmbs);
printf("sqes : %#x\n", ctrl->sqes);
if (human)
show_nvme_id_ctrl_sqes(ctrl->sqes);
printf("cqes : %#x\n", ctrl->cqes);
if (human)
show_nvme_id_ctrl_cqes(ctrl->cqes);
printf("nn : %d\n", ctrl->nn);
printf("oncs : %#x\n", ctrl->oncs);
if (human)
show_nvme_id_ctrl_oncs(ctrl->oncs);
printf("fuses : %#x\n", ctrl->fuses);
if (human)
show_nvme_id_ctrl_fuses(ctrl->fuses);
printf("fna : %#x\n", ctrl->fna);
if (human)
show_nvme_id_ctrl_fna(ctrl->fna);
printf("vwc : %#x\n", ctrl->vwc);
if (human)
show_nvme_id_ctrl_vwc(ctrl->vwc);
printf("awun : %d\n", ctrl->awun);
printf("awupf : %d\n", ctrl->awupf);
printf("nvscc : %d\n", ctrl->nvscc);
if (human)
show_nvme_id_ctrl_nvscc(ctrl->nvscc);
printf("acwu : %d\n", ctrl->acwu);
printf("sgls : %x\n", ctrl->sgls);
if (human)
show_nvme_id_ctrl_sgls(ctrl->sgls);
for (i = 0; i <= ctrl->npss; i++) {
printf("ps %4d : mp:%d flags:%x enlat:%d exlat:%d rrt:%d rrl:%d\n"
" rwt:%d rwl:%d idlp:%d ips:%x actp:%x ap flags:%x\n",
i, ctrl->psd[i].max_power, ctrl->psd[i].flags,
ctrl->psd[i].entry_lat, ctrl->psd[i].exit_lat,
ctrl->psd[i].read_tput, ctrl->psd[i].read_lat,
ctrl->psd[i].write_tput, ctrl->psd[i].write_lat,
ctrl->psd[i].idle_power, ctrl->psd[i].idle_scale,
ctrl->psd[i].active_power, ctrl->psd[i].active_work_scale);
}
if (vs) {
printf("vs[]:\n");
d(ctrl->vs, sizeof(ctrl->vs), 16, 1);
}
}
static void show_lba_range(struct nvme_lba_range_type *lbrt, int nr_ranges)
{
int i, j;
for (i = 0; i < nr_ranges; i++) {
printf("type : %#x\n", lbrt[i].type);
printf("attributes : %#x\n", lbrt[i].attributes);
printf("slba : %#"PRIx64"\n", (uint64_t)(lbrt[i].slba));
printf("nlb : %#"PRIx64"\n", (uint64_t)(lbrt[i].nlb));
printf("guid : ");
for (j = 0; j < 16; j++)
printf("%02x", lbrt[i].guid[j]);
printf("\n");
}
}
static void show_nvme_id_ns_nsfeat(__u8 nsfeat)
{
__u8 rsvd = (nsfeat & 0xF8) >> 3;
__u8 dulbe = (nsfeat & 0x4) >> 2;
__u8 na = (nsfeat & 0x2) >> 1;
__u8 thin = nsfeat & 0x1;
if (rsvd)
printf(" [7:3] : %#x\tReserved\n", rsvd);
printf(" [2:2] : %#x\tDeallocated or Unwritten Logical Block error %sSupported\n",
dulbe, dulbe ? "" : "Not ");
printf(" [1:1] : %#x\tNamespace uses %s\n",
na, na ? "NAWUN, NAWUPF, and NACWU" : "AWUN, AWUPF, and ACWU");
printf(" [0:0] : %#x\tThin Provisioning %sSupported\n",
thin, thin ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ns_flbas(__u8 flbas)
{
__u8 rsvd = (flbas & 0xE0) >> 5;
__u8 mdedata = (flbas & 0x10) >> 4;
__u8 lbaf = flbas & 0xF;
if (rsvd)
printf(" [7:5] : %#x\tReserved\n", rsvd);
printf(" [4:4] : %#x\tMetadata Transferred %s\n",
mdedata, mdedata ? "at End of Data LBA" : "in Separate Contiguous Buffer");
printf(" [3:0] : %#x\tCurrent LBA Format Selected\n", lbaf);
printf("\n");
}
static void show_nvme_id_ns_mc(__u8 mc)
{
__u8 rsvd = (mc & 0xFC) >> 2;
__u8 mdp = (mc & 0x2) >> 1;
__u8 extdlba = mc & 0x1;
if (rsvd)
printf(" [7:2] : %#x\tReserved\n", rsvd);
printf(" [1:1] : %#x\tMetadata Pointer %sSupported\n",
mdp, mdp ? "" : "Not ");
printf(" [0:0] : %#x\tMetadata as Part of Extended Data LBA %sSupported\n",
extdlba, extdlba ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ns_dpc(__u8 dpc)
{
__u8 rsvd = (dpc & 0xE0) >> 5;
__u8 pil8 = (dpc & 0x10) >> 4;
__u8 pif8 = (dpc & 0x8) >> 3;
__u8 pit3 = (dpc & 0x4) >> 2;
__u8 pit2 = (dpc & 0x2) >> 1;
__u8 pit1 = dpc & 0x1;
if (rsvd)
printf(" [7:5] : %#x\tReserved\n", rsvd);
printf(" [4:4] : %#x\tProtection Information Transferred as Last 8 Bytes of Metadata %sSupported\n",
pil8, pil8 ? "" : "Not ");
printf(" [3:3] : %#x\tProtection Information Transferred as First 8 Bytes of Metadata %sSupported\n",
pif8, pif8 ? "" : "Not ");
printf(" [2:2] : %#x\tProtection Information Type 3 %sSupported\n",
pit3, pit3 ? "" : "Not ");
printf(" [1:1] : %#x\tProtection Information Type 2 %sSupported\n",
pit2, pit2 ? "" : "Not ");
printf(" [0:0] : %#x\tProtection Information Type 1 %sSupported\n",
pit1, pit1 ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ns_dps(__u8 dps)
{
__u8 rsvd = (dps & 0xF0) >> 4;
__u8 pif8 = (dps & 0x8) >> 3;
__u8 pit = dps & 0x7;
if (rsvd)
printf(" [7:4] : %#x\tReserved\n", rsvd);
printf(" [3:3] : %#x\tProtection Information is Transferred as %s 8 Bytes of Metadata\n",
pif8, pif8 ? "First" : "Last");
printf(" [2:0] : %#x\tProtection Information %s\n", pit,
pit == 3 ? "Type 3 Enabled" :
pit == 2 ? "Type 2 Enabled" :
pit == 1 ? "Type 1 Enabled" :
pit == 0 ? "Disabled" : "Reserved Enabled");
printf("\n");
}
static void show_nvme_id_ns_nmic(__u8 nmic)
{
__u8 rsvd = (nmic & 0xFE) >> 1;
__u8 mp = nmic & 0x1;
if (rsvd)
printf(" [7:1] : %#x\tReserved\n", rsvd);
printf(" [0:0] : %#x\tNamespace Multipath %sCapable\n",
mp, mp ? "" : "Not ");
printf("\n");
}
static void show_nvme_id_ns_rescap(__u8 rescap)
{
__u8 rsvd = (rescap & 0x80) >> 7;
__u8 eaar = (rescap & 0x40) >> 6;
__u8 wear = (rescap & 0x20) >> 5;
__u8 earo = (rescap & 0x10) >> 4;
__u8 wero = (rescap & 0x8) >> 3;
__u8 ea = (rescap & 0x4) >> 2;
__u8 we = (rescap & 0x2) >> 1;
__u8 ptpl = rescap & 0x1;
if (rsvd)
printf(" [7:7] : %#x\tReserved\n", rsvd);
printf(" [6:6] : %#x\tExclusive Access - All Registrants %sSupported\n",