forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme-print.c
executable file
·5824 lines (5207 loc) · 198 KB
/
nvme-print.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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include "nvme-print.h"
#include "util/json.h"
#include "nvme-models.h"
#include "util/suffix.h"
#include "common.h"
static const uint8_t zero_uuid[16] = { 0 };
static const uint8_t invalid_uuid[16] = {[0 ... 15] = 0xff };
static const char dash[100] = {[0 ... 99] = '-'};
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 const char *nvme_ana_state_to_string(enum nvme_ana_state state)
{
switch (state) {
case NVME_ANA_OPTIMIZED:
return "optimized";
case NVME_ANA_NONOPTIMIZED:
return "non-optimized";
case NVME_ANA_INACCESSIBLE:
return "inaccessible";
case NVME_ANA_PERSISTENT_LOSS:
return "persistent-loss";
case NVME_ANA_CHANGE:
return "change";
}
return "invalid state";
}
static const char *nvme_cmd_to_string(int admin, __u8 opcode)
{
if (admin) {
switch (opcode) {
case nvme_admin_delete_sq: return "Delete I/O Submission Queue";
case nvme_admin_create_sq: return "Create I/O Submission Queue";
case nvme_admin_get_log_page: return "Get Log Page";
case nvme_admin_delete_cq: return "Delete I/O Completion Queue";
case nvme_admin_create_cq: return "Create I/O Completion Queue";
case nvme_admin_identify: return "Identify";
case nvme_admin_abort_cmd: return "Abort";
case nvme_admin_set_features: return "Set Features";
case nvme_admin_get_features: return "Get Features";
case nvme_admin_async_event: return "Asynchronous Event Request";
case nvme_admin_ns_mgmt: return "Namespace Management";
case nvme_admin_activate_fw: return "Firmware Commit";
case nvme_admin_download_fw: return "Firmware Image Download";
case nvme_admin_dev_self_test: return "Device Self-test";
case nvme_admin_ns_attach: return "Namespace Attachment";
case nvme_admin_keep_alive: return "Keep Alive";
case nvme_admin_directive_send: return "Directive Send";
case nvme_admin_directive_recv: return "Directive Receive";
case nvme_admin_virtual_mgmt: return "Virtualization Management";
case nvme_admin_nvme_mi_send: return "NVMEe-MI Send";
case nvme_admin_nvme_mi_recv: return "NVMEe-MI Receive";
case nvme_admin_dbbuf: return "Doorbell Buffer Config";
case nvme_admin_format_nvm: return "Format NVM";
case nvme_admin_security_send: return "Security Send";
case nvme_admin_security_recv: return "Security Receive";
case nvme_admin_sanitize_nvm: return "Sanitize";
case nvme_admin_get_lba_status: return "Get LBA Status";
}
} else {
switch (opcode) {
case nvme_cmd_flush: return "Flush";
case nvme_cmd_write: return "Write";
case nvme_cmd_read: return "Read";
case nvme_cmd_write_uncor: return "Write Uncorrectable";
case nvme_cmd_compare: return "Compare";
case nvme_cmd_write_zeroes: return "Write Zeroes";
case nvme_cmd_dsm: return "Dataset Management";
case nvme_cmd_resv_register: return "Reservation Register";
case nvme_cmd_resv_report: return "Reservation Report";
case nvme_cmd_resv_acquire: return "Reservation Acquire";
case nvme_cmd_resv_release: return "Reservation Release";
case nvme_cmd_verify: return "Verify";
case nvme_cmd_copy: return "Copy";
}
}
return "Unknown";
}
static const 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 const char *get_sanitize_log_sstat_status_str(__u16 status)
{
switch (status & NVME_SANITIZE_LOG_STATUS_MASK) {
case NVME_SANITIZE_LOG_NEVER_SANITIZED:
return "NVM Subsystem has never been sanitized.";
case NVME_SANITIZE_LOG_COMPLETED_SUCCESS:
return "Most Recent Sanitize Command Completed Successfully.";
case NVME_SANITIZE_LOG_IN_PROGESS:
return "Sanitize in Progress.";
case NVME_SANITIZE_LOG_COMPLETED_FAILED:
return "Most Recent Sanitize Command Failed.";
case NVME_SANITIZE_LOG_ND_COMPLETED_SUCCESS:
return "Most Recent Sanitize Command (No-Deallocate After Sanitize) Completed Successfully.";
default:
return "Unknown";
}
}
static void json_nvme_id_ns(struct nvme_id_ns *ns)
{
char nguid_buf[2 * sizeof(ns->nguid) + 1],
eui64_buf[2 * sizeof(ns->eui64) + 1];
char *nguid = nguid_buf, *eui64 = eui64_buf;
struct json_object *root;
struct json_array *lbafs;
int i;
long double nvmcap = int128_to_double(ns->nvmcap);
root = json_create_object();
json_object_add_value_uint(root, "nsze", le64_to_cpu(ns->nsze));
json_object_add_value_uint(root, "ncap", le64_to_cpu(ns->ncap));
json_object_add_value_uint(root, "nuse", le64_to_cpu(ns->nuse));
json_object_add_value_int(root, "nsfeat", ns->nsfeat);
json_object_add_value_int(root, "nlbaf", ns->nlbaf);
json_object_add_value_int(root, "flbas", ns->flbas);
json_object_add_value_int(root, "mc", ns->mc);
json_object_add_value_int(root, "dpc", ns->dpc);
json_object_add_value_int(root, "dps", ns->dps);
json_object_add_value_int(root, "nmic", ns->nmic);
json_object_add_value_int(root, "rescap", ns->rescap);
json_object_add_value_int(root, "fpi", ns->fpi);
json_object_add_value_int(root, "nawun", le16_to_cpu(ns->nawun));
json_object_add_value_int(root, "nawupf", le16_to_cpu(ns->nawupf));
json_object_add_value_int(root, "nacwu", le16_to_cpu(ns->nacwu));
json_object_add_value_int(root, "nabsn", le16_to_cpu(ns->nabsn));
json_object_add_value_int(root, "nabo", le16_to_cpu(ns->nabo));
json_object_add_value_int(root, "nabspf", le16_to_cpu(ns->nabspf));
json_object_add_value_int(root, "noiob", le16_to_cpu(ns->noiob));
json_object_add_value_float(root, "nvmcap", nvmcap);
json_object_add_value_int(root, "nsattr", ns->nsattr);
json_object_add_value_int(root, "nvmsetid", le16_to_cpu(ns->nvmsetid));
if (ns->nsfeat & 0x10) {
json_object_add_value_int(root, "npwg", le16_to_cpu(ns->npwg));
json_object_add_value_int(root, "npwa", le16_to_cpu(ns->npwa));
json_object_add_value_int(root, "npdg", le16_to_cpu(ns->npdg));
json_object_add_value_int(root, "npda", le16_to_cpu(ns->npda));
json_object_add_value_int(root, "nows", le16_to_cpu(ns->nows));
}
json_object_add_value_int(root, "mssrl", le16_to_cpu(ns->mssrl));
json_object_add_value_int(root, "mcl", le32_to_cpu(ns->mcl));
json_object_add_value_int(root, "msrc", ns->msrc);
json_object_add_value_int(root, "anagrpid", le32_to_cpu(ns->anagrpid));
json_object_add_value_int(root, "endgid", le16_to_cpu(ns->endgid));
memset(eui64, 0, sizeof(eui64_buf));
for (i = 0; i < sizeof(ns->eui64); i++)
eui64 += sprintf(eui64, "%02x", ns->eui64[i]);
memset(nguid, 0, sizeof(nguid_buf));
for (i = 0; i < sizeof(ns->nguid); i++)
nguid += sprintf(nguid, "%02x", ns->nguid[i]);
json_object_add_value_string(root, "eui64", eui64_buf);
json_object_add_value_string(root, "nguid", nguid_buf);
lbafs = json_create_array();
json_object_add_value_array(root, "lbafs", lbafs);
for (i = 0; i <= ns->nlbaf; i++) {
struct json_object *lbaf = json_create_object();
json_object_add_value_int(lbaf, "ms",
le16_to_cpu(ns->lbaf[i].ms));
json_object_add_value_int(lbaf, "ds", ns->lbaf[i].ds);
json_object_add_value_int(lbaf, "rp", ns->lbaf[i].rp);
json_array_add_value_object(lbafs, lbaf);
}
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_nvme_id_ctrl(struct nvme_id_ctrl *ctrl,
void (*vs)(__u8 *vs, struct json_object *root))
{
struct json_object *root;
struct json_array *psds;
long double tnvmcap = int128_to_double(ctrl->tnvmcap);
long double unvmcap = int128_to_double(ctrl->unvmcap);
char sn[sizeof(ctrl->sn) + 1], mn[sizeof(ctrl->mn) + 1],
fr[sizeof(ctrl->fr) + 1], subnqn[sizeof(ctrl->subnqn) + 1];
__u32 ieee = ctrl->ieee[2] << 16 | ctrl->ieee[1] << 8 | ctrl->ieee[0];
int i;
snprintf(sn, sizeof(sn), "%-.*s", (int)sizeof(ctrl->sn), ctrl->sn);
snprintf(mn, sizeof(mn), "%-.*s", (int)sizeof(ctrl->mn), ctrl->mn);
snprintf(fr, sizeof(fr), "%-.*s", (int)sizeof(ctrl->fr), ctrl->fr);
snprintf(subnqn, sizeof(subnqn), "%-.*s", (int)sizeof(ctrl->subnqn), ctrl->subnqn);
root = json_create_object();
json_object_add_value_int(root, "vid", le16_to_cpu(ctrl->vid));
json_object_add_value_int(root, "ssvid", le16_to_cpu(ctrl->ssvid));
json_object_add_value_string(root, "sn", sn);
json_object_add_value_string(root, "mn", mn);
json_object_add_value_string(root, "fr", fr);
json_object_add_value_int(root, "rab", ctrl->rab);
json_object_add_value_int(root, "ieee", ieee);
json_object_add_value_int(root, "cmic", ctrl->cmic);
json_object_add_value_int(root, "mdts", ctrl->mdts);
json_object_add_value_int(root, "cntlid", le16_to_cpu(ctrl->cntlid));
json_object_add_value_uint(root, "ver", le32_to_cpu(ctrl->ver));
json_object_add_value_uint(root, "rtd3r", le32_to_cpu(ctrl->rtd3r));
json_object_add_value_uint(root, "rtd3e", le32_to_cpu(ctrl->rtd3e));
json_object_add_value_uint(root, "oaes", le32_to_cpu(ctrl->oaes));
json_object_add_value_int(root, "ctratt", le32_to_cpu(ctrl->ctratt));
json_object_add_value_int(root, "rrls", le16_to_cpu(ctrl->rrls));
json_object_add_value_int(root, "crdt1", le16_to_cpu(ctrl->crdt1));
json_object_add_value_int(root, "crdt2", le16_to_cpu(ctrl->crdt2));
json_object_add_value_int(root, "crdt3", le16_to_cpu(ctrl->crdt3));
json_object_add_value_int(root, "oacs", le16_to_cpu(ctrl->oacs));
json_object_add_value_int(root, "acl", ctrl->acl);
json_object_add_value_int(root, "aerl", ctrl->aerl);
json_object_add_value_int(root, "frmw", ctrl->frmw);
json_object_add_value_int(root, "lpa", ctrl->lpa);
json_object_add_value_int(root, "elpe", ctrl->elpe);
json_object_add_value_int(root, "npss", ctrl->npss);
json_object_add_value_int(root, "avscc", ctrl->avscc);
json_object_add_value_int(root, "apsta", ctrl->apsta);
json_object_add_value_int(root, "wctemp", le16_to_cpu(ctrl->wctemp));
json_object_add_value_int(root, "cctemp", le16_to_cpu(ctrl->cctemp));
json_object_add_value_int(root, "mtfa", le16_to_cpu(ctrl->mtfa));
json_object_add_value_uint(root, "hmpre", le32_to_cpu(ctrl->hmpre));
json_object_add_value_uint(root, "hmmin", le32_to_cpu(ctrl->hmmin));
json_object_add_value_float(root, "tnvmcap", tnvmcap);
json_object_add_value_float(root, "unvmcap", unvmcap);
json_object_add_value_uint(root, "rpmbs", le32_to_cpu(ctrl->rpmbs));
json_object_add_value_int(root, "edstt", le16_to_cpu(ctrl->edstt));
json_object_add_value_int(root, "dsto", ctrl->dsto);
json_object_add_value_int(root, "fwug", ctrl->fwug);
json_object_add_value_int(root, "kas", le16_to_cpu(ctrl->kas));
json_object_add_value_int(root, "hctma", le16_to_cpu(ctrl->hctma));
json_object_add_value_int(root, "mntmt", le16_to_cpu(ctrl->mntmt));
json_object_add_value_int(root, "mxtmt", le16_to_cpu(ctrl->mxtmt));
json_object_add_value_int(root, "sanicap", le32_to_cpu(ctrl->sanicap));
json_object_add_value_int(root, "hmminds", le32_to_cpu(ctrl->hmminds));
json_object_add_value_int(root, "hmmaxd", le16_to_cpu(ctrl->hmmaxd));
json_object_add_value_int(root, "nsetidmax",
le16_to_cpu(ctrl->nsetidmax));
json_object_add_value_int(root, "anatt",ctrl->anatt);
json_object_add_value_int(root, "anacap", ctrl->anacap);
json_object_add_value_int(root, "anagrpmax",
le32_to_cpu(ctrl->anagrpmax));
json_object_add_value_int(root, "nanagrpid",
le32_to_cpu(ctrl->nanagrpid));
json_object_add_value_int(root, "sqes", ctrl->sqes);
json_object_add_value_int(root, "cqes", ctrl->cqes);
json_object_add_value_int(root, "maxcmd", le16_to_cpu(ctrl->maxcmd));
json_object_add_value_uint(root, "nn", le32_to_cpu(ctrl->nn));
json_object_add_value_int(root, "oncs", le16_to_cpu(ctrl->oncs));
json_object_add_value_int(root, "fuses", le16_to_cpu(ctrl->fuses));
json_object_add_value_int(root, "fna", ctrl->fna);
json_object_add_value_int(root, "vwc", ctrl->vwc);
json_object_add_value_int(root, "awun", le16_to_cpu(ctrl->awun));
json_object_add_value_int(root, "awupf", le16_to_cpu(ctrl->awupf));
json_object_add_value_int(root, "icsvscc", ctrl->icsvscc);
json_object_add_value_int(root, "nwpc", ctrl->nwpc);
json_object_add_value_int(root, "acwu", le16_to_cpu(ctrl->acwu));
json_object_add_value_int(root, "ocfs", le16_to_cpu(ctrl->ocfs));
json_object_add_value_int(root, "sgls", le32_to_cpu(ctrl->sgls));
if (strlen(subnqn))
json_object_add_value_string(root, "subnqn", subnqn);
json_object_add_value_int(root, "ioccsz", le32_to_cpu(ctrl->ioccsz));
json_object_add_value_int(root, "iorcsz", le32_to_cpu(ctrl->iorcsz));
json_object_add_value_int(root, "icdoff", le16_to_cpu(ctrl->icdoff));
json_object_add_value_int(root, "ctrattr", ctrl->ctrattr);
json_object_add_value_int(root, "msdbd", ctrl->msdbd);
psds = json_create_array();
json_object_add_value_array(root, "psds", psds);
for (i = 0; i <= ctrl->npss; i++) {
struct json_object *psd = json_create_object();
json_object_add_value_int(psd, "max_power",
le16_to_cpu(ctrl->psd[i].max_power));
json_object_add_value_int(psd, "flags", ctrl->psd[i].flags);
json_object_add_value_uint(psd, "entry_lat",
le32_to_cpu(ctrl->psd[i].entry_lat));
json_object_add_value_uint(psd, "exit_lat",
le32_to_cpu(ctrl->psd[i].exit_lat));
json_object_add_value_int(psd, "read_tput",
ctrl->psd[i].read_tput);
json_object_add_value_int(psd, "read_lat",
ctrl->psd[i].read_lat);
json_object_add_value_int(psd, "write_tput",
ctrl->psd[i].write_tput);
json_object_add_value_int(psd, "write_lat",
ctrl->psd[i].write_lat);
json_object_add_value_int(psd, "idle_power",
le16_to_cpu(ctrl->psd[i].idle_power));
json_object_add_value_int(psd, "idle_scale",
ctrl->psd[i].idle_scale);
json_object_add_value_int(psd, "active_power",
le16_to_cpu(ctrl->psd[i].active_power));
json_object_add_value_int(psd, "active_work_scale",
ctrl->psd[i].active_work_scale);
json_array_add_value_object(psds, psd);
}
if(vs)
vs(ctrl->vs, root);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_error_log(struct nvme_error_log_page *err_log, int entries)
{
struct json_object *root;
struct json_array *errors;
int i;
root = json_create_object();
errors = json_create_array();
json_object_add_value_array(root, "errors", errors);
for (i = 0; i < entries; i++) {
struct json_object *error = json_create_object();
json_object_add_value_uint(error, "error_count",
le64_to_cpu(err_log[i].error_count));
json_object_add_value_int(error, "sqid",
le16_to_cpu(err_log[i].sqid));
json_object_add_value_int(error, "cmdid",
le16_to_cpu(err_log[i].cmdid));
json_object_add_value_int(error, "status_field",
le16_to_cpu(err_log[i].status_field));
json_object_add_value_int(error, "parm_error_location",
le16_to_cpu(err_log[i].parm_error_location));
json_object_add_value_uint(error, "lba",
le64_to_cpu(err_log[i].lba));
json_object_add_value_uint(error, "nsid",
le32_to_cpu(err_log[i].nsid));
json_object_add_value_int(error, "vs", err_log[i].vs);
json_object_add_value_int(error, "trtype", err_log[i].trtype);
json_object_add_value_uint(error, "cs",
le64_to_cpu(err_log[i].cs));
json_object_add_value_int(error, "trtype_spec_info",
le16_to_cpu(err_log[i].trtype_spec_info));
json_array_add_value_object(errors, error);
}
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_nvme_resv_report(struct nvme_reservation_status *status,
int bytes, __u32 cdw11)
{
struct json_object *root;
struct json_array *rcs;
int i, j, regctl, entries;
regctl = status->regctl[0] | (status->regctl[1] << 8);
root = json_create_object();
json_object_add_value_int(root, "gen", le32_to_cpu(status->gen));
json_object_add_value_int(root, "rtype", status->rtype);
json_object_add_value_int(root, "regctl", regctl);
json_object_add_value_int(root, "ptpls", status->ptpls);
rcs = json_create_array();
/* check Extended Data Structure bit */
if ((cdw11 & 0x1) == 0) {
/*
* if status buffer was too small, don't loop past the end of
* the buffer
*/
entries = (bytes - 24) / 24;
if (entries < regctl)
regctl = entries;
json_object_add_value_array(root, "regctls", rcs);
for (i = 0; i < regctl; i++) {
struct json_object *rc = json_create_object();
json_object_add_value_int(rc, "cntlid",
le16_to_cpu(status->regctl_ds[i].cntlid));
json_object_add_value_int(rc, "rcsts",
status->regctl_ds[i].rcsts);
json_object_add_value_uint(rc, "hostid",
le64_to_cpu(status->regctl_ds[i].hostid));
json_object_add_value_uint(rc, "rkey",
le64_to_cpu(status->regctl_ds[i].rkey));
json_array_add_value_object(rcs, rc);
}
} else {
struct nvme_reservation_status_ext *ext_status = (struct nvme_reservation_status_ext *)status;
char hostid[33];
/* if status buffer was too small, don't loop past the end of the buffer */
entries = (bytes - 64) / 64;
if (entries < regctl)
regctl = entries;
json_object_add_value_array(root, "regctlext", rcs);
for (i = 0; i < regctl; i++) {
struct json_object *rc = json_create_object();
json_object_add_value_int(rc, "cntlid",
le16_to_cpu(ext_status->regctl_eds[i].cntlid));
json_object_add_value_int(rc, "rcsts",
ext_status->regctl_eds[i].rcsts);
json_object_add_value_uint(rc, "rkey",
le64_to_cpu(ext_status->regctl_eds[i].rkey));
for (j = 0; j < 16; j++)
sprintf(hostid + j * 2, "%02x",
ext_status->regctl_eds[i].hostid[j]);
json_object_add_value_string(rc, "hostid", hostid);
json_array_add_value_object(rcs, rc);
}
}
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_fw_log(struct nvme_firmware_log_page *fw_log, const char *devname)
{
struct json_object *root;
struct json_object *fwsi;
char fmt[21];
char str[32];
int i;
root = json_create_object();
fwsi = json_create_object();
json_object_add_value_int(fwsi, "Active Firmware Slot (afi)",
fw_log->afi);
for (i = 0; i < 7; i++) {
if (fw_log->frs[i]) {
snprintf(fmt, sizeof(fmt), "Firmware Rev Slot %d",
i + 1);
snprintf(str, sizeof(str), "%"PRIu64" (%s)",
(uint64_t)fw_log->frs[i],
fw_to_string(fw_log->frs[i]));
json_object_add_value_string(fwsi, fmt, str);
}
}
json_object_add_value_object(root, devname, fwsi);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_changed_ns_list_log(struct nvme_changed_ns_list_log *log,
const char *devname)
{
struct json_object *root;
struct json_object *nsi;
char fmt[32];
char str[32];
__u32 nsid;
int i;
if (log->log[0] == cpu_to_le32(0xffffffff))
return;
root = json_create_object();
nsi = json_create_object();
json_object_add_value_string(root, "Changed Namespace List Log",
devname);
for (i = 0; i < NVME_MAX_CHANGED_NAMESPACES; i++) {
nsid = le32_to_cpu(log->log[i]);
if (nsid == 0)
break;
snprintf(fmt, sizeof(fmt), "[%4u]", i + 1);
snprintf(str, sizeof(str), "%#x", nsid);
json_object_add_value_string(nsi, fmt, str);
}
json_object_add_value_object(root, devname, nsi);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_endurance_log(struct nvme_endurance_group_log *endurance_group,
__u16 group_id)
{
struct json_object *root;
long double endurance_estimate =
int128_to_double(endurance_group->endurance_estimate);
long double data_units_read =
int128_to_double(endurance_group->data_units_read);
long double data_units_written =
int128_to_double(endurance_group->data_units_written);
long double media_units_written =
int128_to_double(endurance_group->media_units_written);
long double host_read_cmds =
int128_to_double(endurance_group->host_read_cmds);
long double host_write_cmds =
int128_to_double(endurance_group->host_write_cmds);
long double media_data_integrity_err =
int128_to_double(endurance_group->media_data_integrity_err);
long double num_err_info_log_entries =
int128_to_double(endurance_group->num_err_info_log_entries);
root = json_create_object();
json_object_add_value_int(root, "critical_warning",
endurance_group->critical_warning);
json_object_add_value_int(root, "avl_spare",
endurance_group->avl_spare);
json_object_add_value_int(root, "avl_spare_threshold",
endurance_group->avl_spare_threshold);
json_object_add_value_int(root, "percent_used",
endurance_group->percent_used);
json_object_add_value_float(root, "endurance_estimate",
endurance_estimate);
json_object_add_value_float(root, "data_units_read", data_units_read);
json_object_add_value_float(root, "data_units_written",
data_units_written);
json_object_add_value_float(root, "mediate_write_commands",
media_units_written);
json_object_add_value_float(root, "host_read_cmds", host_read_cmds);
json_object_add_value_float(root, "host_write_cmds", host_write_cmds);
json_object_add_value_float(root, "media_data_integrity_err",
media_data_integrity_err);
json_object_add_value_float(root, "num_err_info_log_entries",
num_err_info_log_entries);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
enum nvme_print_flags flags)
{
int c, human = flags & VERBOSE;
struct json_object *root;
char key[21];
unsigned int temperature = ((smart->temperature[1] << 8) |
smart->temperature[0]);
long double data_units_read = int128_to_double(smart->data_units_read);
long double data_units_written = int128_to_double(smart->data_units_written);
long double host_read_commands = int128_to_double(smart->host_reads);
long double host_write_commands = int128_to_double(smart->host_writes);
long double controller_busy_time = int128_to_double(smart->ctrl_busy_time);
long double power_cycles = int128_to_double(smart->power_cycles);
long double power_on_hours = int128_to_double(smart->power_on_hours);
long double unsafe_shutdowns = int128_to_double(smart->unsafe_shutdowns);
long double media_errors = int128_to_double(smart->media_errors);
long double num_err_log_entries = int128_to_double(smart->num_err_log_entries);
root = json_create_object();
if (human) {
struct json_object *crt = json_create_object();
json_object_add_value_int(crt, "value", smart->critical_warning);
json_object_add_value_int(crt, "available_spare", smart->critical_warning & 0x01);
json_object_add_value_int(crt, "temp_threshold", (smart->critical_warning & 0x02) >> 1);
json_object_add_value_int(crt, "reliability_degraded", (smart->critical_warning & 0x04) >> 2);
json_object_add_value_int(crt, "ro", (smart->critical_warning & 0x08) >> 3);
json_object_add_value_int(crt, "vmbu_failed", (smart->critical_warning & 0x10) >> 4);
json_object_add_value_int(crt, "pmr_ro", (smart->critical_warning & 0x20) >> 5);
json_object_add_value_object(root, "critical_warning", crt);
} else
json_object_add_value_int(root, "critical_warning",
smart->critical_warning);
json_object_add_value_int(root, "temperature", temperature);
json_object_add_value_int(root, "avail_spare", smart->avail_spare);
json_object_add_value_int(root, "spare_thresh", smart->spare_thresh);
json_object_add_value_int(root, "percent_used", smart->percent_used);
json_object_add_value_int(root, "endurance_grp_critical_warning_summary",
smart->endu_grp_crit_warn_sumry);
json_object_add_value_float(root, "data_units_read", data_units_read);
json_object_add_value_float(root, "data_units_written",
data_units_written);
json_object_add_value_float(root, "host_read_commands",
host_read_commands);
json_object_add_value_float(root, "host_write_commands",
host_write_commands);
json_object_add_value_float(root, "controller_busy_time",
controller_busy_time);
json_object_add_value_float(root, "power_cycles", power_cycles);
json_object_add_value_float(root, "power_on_hours", power_on_hours);
json_object_add_value_float(root, "unsafe_shutdowns", unsafe_shutdowns);
json_object_add_value_float(root, "media_errors", media_errors);
json_object_add_value_float(root, "num_err_log_entries",
num_err_log_entries);
json_object_add_value_uint(root, "warning_temp_time",
le32_to_cpu(smart->warning_temp_time));
json_object_add_value_uint(root, "critical_comp_time",
le32_to_cpu(smart->critical_comp_time));
for (c=0; c < 8; c++) {
__s32 temp = le16_to_cpu(smart->temp_sensor[c]);
if (temp == 0)
continue;
sprintf(key, "temperature_sensor_%d",c+1);
json_object_add_value_int(root, key, temp);
}
json_object_add_value_uint(root, "thm_temp1_trans_count",
le32_to_cpu(smart->thm_temp1_trans_count));
json_object_add_value_uint(root, "thm_temp2_trans_count",
le32_to_cpu(smart->thm_temp2_trans_count));
json_object_add_value_uint(root, "thm_temp1_total_time",
le32_to_cpu(smart->thm_temp1_total_time));
json_object_add_value_uint(root, "thm_temp2_total_time",
le32_to_cpu(smart->thm_temp2_total_time));
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_ana_log(struct nvme_ana_rsp_hdr *ana_log, const char *devname)
{
int offset = sizeof(struct nvme_ana_rsp_hdr);
struct nvme_ana_rsp_hdr *hdr = ana_log;
struct nvme_ana_group_desc *ana_desc;
struct json_array *desc_list;
struct json_array *ns_list;
struct json_object *desc;
struct json_object *nsid;
struct json_object *root;
size_t nsid_buf_size;
void *base = ana_log;
__u32 nr_nsids;
int i, j;
root = json_create_object();
json_object_add_value_string(root,
"Asymmetric Namespace Access Log for NVMe device",
devname);
json_object_add_value_uint(root, "chgcnt",
le64_to_cpu(hdr->chgcnt));
json_object_add_value_uint(root, "ngrps", le16_to_cpu(hdr->ngrps));
desc_list = json_create_array();
for (i = 0; i < le16_to_cpu(ana_log->ngrps); i++) {
desc = json_create_object();
ana_desc = base + offset;
nr_nsids = le32_to_cpu(ana_desc->nnsids);
nsid_buf_size = nr_nsids * sizeof(__le32);
offset += sizeof(*ana_desc);
json_object_add_value_uint(desc, "grpid",
le32_to_cpu(ana_desc->grpid));
json_object_add_value_uint(desc, "nnsids",
le32_to_cpu(ana_desc->nnsids));
json_object_add_value_uint(desc, "chgcnt",
le64_to_cpu(ana_desc->chgcnt));
json_object_add_value_string(desc, "state",
nvme_ana_state_to_string(ana_desc->state));
ns_list = json_create_array();
for (j = 0; j < le32_to_cpu(ana_desc->nnsids); j++) {
nsid = json_create_object();
json_object_add_value_uint(nsid, "nsid",
le32_to_cpu(ana_desc->nsids[j]));
json_array_add_value_object(ns_list, nsid);
}
json_object_add_value_array(desc, "NSIDS", ns_list);
offset += nsid_buf_size;
json_array_add_value_object(desc_list, desc);
}
json_object_add_value_array(root, "ANA DESC LIST ", desc_list);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_self_test_log(struct nvme_self_test_log *self_test, __u8 dst_entries)
{
struct json_object *valid_attrs;
struct json_object *root;
struct json_array *valid;
int i;
__u32 num_entries;
root = json_create_object();
json_object_add_value_int(root, "Current Device Self-Test Operation",
self_test->crnt_dev_selftest_oprn);
json_object_add_value_int(root, "Current Device Self-Test Completion",
self_test->crnt_dev_selftest_compln);
valid = json_create_array();
num_entries = min(dst_entries, NVME_ST_REPORTS);
for (i = 0; i < num_entries; i++) {
valid_attrs = json_create_object();
json_object_add_value_int(valid_attrs, "Self test result",
self_test->result[i].dsts & 0xf);
if ((self_test->result[i].dsts & 0xf) == 0xf)
goto add;
json_object_add_value_int(valid_attrs, "Self test code",
self_test->result[i].dsts >> 4);
json_object_add_value_int(valid_attrs, "Segment number",
self_test->result[i].seg);
json_object_add_value_int(valid_attrs, "Valid Diagnostic Information",
self_test->result[i].vdi);
json_object_add_value_uint(valid_attrs, "Power on hours",
le64_to_cpu(self_test->result[i].poh));
if (self_test->result[i].vdi & NVME_ST_VALID_NSID)
json_object_add_value_int(valid_attrs, "Namespace Identifier",
le32_to_cpu(self_test->result[i].nsid));
if (self_test->result[i].vdi & NVME_ST_VALID_FLBA)
json_object_add_value_uint(valid_attrs, "Failing LBA",
le64_to_cpu(self_test->result[i].flba));
if (self_test->result[i].vdi & NVME_ST_VALID_SCT)
json_object_add_value_int(valid_attrs, "Status Code Type",
self_test->result[i].sct);
if (self_test->result[i].vdi & NVME_ST_VALID_SC)
json_object_add_value_int(valid_attrs, "Status Code",
self_test->result[i].sc);
json_object_add_value_int(valid_attrs, "Vendor Specific",
(self_test->result[i].vs[1] << 8) |
(self_test->result[i].vs[0]));
add:
json_array_add_value_object(valid, valid_attrs);
}
json_object_add_value_array(root, "List of Valid Reports", valid);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_effects_log(struct nvme_effects_log_page *effects_log)
{
struct json_object *root;
unsigned int opcode;
char key[128];
__u32 effect;
root = json_create_object();
for (opcode = 0; opcode < 256; opcode++) {
sprintf(key, "ACS%d (%s)", opcode,
nvme_cmd_to_string(1, opcode));
effect = le32_to_cpu(effects_log->acs[opcode]);
json_object_add_value_uint(root, key, effect);
}
for (opcode = 0; opcode < 256; opcode++) {
sprintf(key, "IOCS%d (%s)", opcode,
nvme_cmd_to_string(0, opcode));
effect = le32_to_cpu(effects_log->iocs[opcode]);
json_object_add_value_uint(root, key, effect);
}
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void json_sanitize_log(struct nvme_sanitize_log_page *sanitize_log,
const char *devname)
{
struct json_object *root;
struct json_object *dev;
struct json_object *sstat;
const char *status_str;
char str[128];
__u16 status = le16_to_cpu(sanitize_log->status);
root = json_create_object();
dev = json_create_object();
sstat = json_create_object();
json_object_add_value_int(dev, "sprog",
le16_to_cpu(sanitize_log->progress));
json_object_add_value_int(sstat, "global_erased",
(status & NVME_SANITIZE_LOG_GLOBAL_DATA_ERASED) >> 8);
json_object_add_value_int(sstat, "no_cmplted_passes",
(status & NVME_SANITIZE_LOG_NUM_CMPLTED_PASS_MASK) >> 3);
status_str = get_sanitize_log_sstat_status_str(status);
sprintf(str, "(%d) %s", status & NVME_SANITIZE_LOG_STATUS_MASK,
status_str);
json_object_add_value_string(sstat, "status", str);
json_object_add_value_object(dev, "sstat", sstat);
json_object_add_value_uint(dev, "cdw10_info",
le32_to_cpu(sanitize_log->cdw10_info));
json_object_add_value_uint(dev, "time_over_write",
le32_to_cpu(sanitize_log->est_ovrwrt_time));
json_object_add_value_uint(dev, "time_block_erase",
le32_to_cpu(sanitize_log->est_blk_erase_time));
json_object_add_value_uint(dev, "time_crypto_erase",
le32_to_cpu(sanitize_log->est_crypto_erase_time));
json_object_add_value_uint(dev, "time_over_write_no_dealloc",
le32_to_cpu(sanitize_log->est_ovrwrt_time_with_no_deallocate));
json_object_add_value_uint(dev, "time_block_erase_no_dealloc",
le32_to_cpu(sanitize_log->est_blk_erase_time_with_no_deallocate));
json_object_add_value_uint(dev, "time_crypto_erase_no_dealloc",
le32_to_cpu(sanitize_log->est_crypto_erase_time_with_no_deallocate));
json_object_add_value_object(root, devname, dev);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
void json_predictable_latency_per_nvmset(
struct nvme_predlat_per_nvmset_log_page *plpns_log,
__u16 nvmset_id)
{
struct json_object *root;
root = json_create_object();
json_object_add_value_uint(root, "nvmset_id",
le16_to_cpu(nvmset_id));
json_object_add_value_uint(root, "status",
plpns_log->status);
json_object_add_value_uint(root, "event_type",
le16_to_cpu(plpns_log->event_type));
json_object_add_value_uint(root, "dtwin_reads_typical",
le64_to_cpu(plpns_log->dtwin_rtyp));
json_object_add_value_uint(root, "dtwin_writes_typical",
le64_to_cpu(plpns_log->dtwin_wtyp));
json_object_add_value_uint(root, "dtwin_time_maximum",
le64_to_cpu(plpns_log->dtwin_timemax));
json_object_add_value_uint(root, "ndwin_time_minimum_high",
le64_to_cpu(plpns_log->ndwin_timemin_high));
json_object_add_value_uint(root, "ndwin_time_minimum_low",
le64_to_cpu(plpns_log->ndwin_timemin_low));
json_object_add_value_uint(root, "dtwin_reads_estimate",
le64_to_cpu(plpns_log->dtwin_restimate));
json_object_add_value_uint(root, "dtwin_writes_estimate",
le64_to_cpu(plpns_log->dtwin_westimate));
json_object_add_value_uint(root, "dtwin_time_estimate",
le64_to_cpu(plpns_log->dtwin_testimate));
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
void nvme_show_predictable_latency_per_nvmset(
struct nvme_predlat_per_nvmset_log_page *plpns_log,
__u16 nvmset_id, const char *devname,
enum nvme_print_flags flags)
{
if (flags & BINARY)
return d_raw((unsigned char *)plpns_log,
sizeof(*plpns_log));
if (flags & JSON)
return json_predictable_latency_per_nvmset(plpns_log,
nvmset_id);
printf("Predictable Latency Per NVM Set Log for device: %s\n",
devname);
printf("Predictable Latency Per NVM Set Log for NVM Set ID: %u\n",
le16_to_cpu(nvmset_id));
printf("Status: %u\n", plpns_log->status);
printf("Event Type: %u\n",
le16_to_cpu(plpns_log->event_type));
printf("DTWIN Reads Typical: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_rtyp));
printf("DTWIN Writes Typical: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_wtyp));
printf("DTWIN Time Maximum: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_timemax));
printf("NDWIN Time Minimum High: %"PRIu64" \n",
le64_to_cpu(plpns_log->ndwin_timemin_high));
printf("NDWIN Time Minimum Low: %"PRIu64"\n",
le64_to_cpu(plpns_log->ndwin_timemin_low));
printf("DTWIN Reads Estimate: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_restimate));
printf("DTWIN Writes Estimate: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_westimate));
printf("DTWIN Time Estimate: %"PRIu64"\n\n\n",
le64_to_cpu(plpns_log->dtwin_testimate));
}
void json_predictable_latency_event_agg_log(
struct nvme_predlat_event_agg_log_page *pea_log,
__u64 log_entries)
{
struct json_object *root;
struct json_object *valid_attrs;
struct json_array *valid;
__u64 num_iter;
__u64 num_entries;
root = json_create_object();
num_entries = le64_to_cpu(pea_log->num_entries);
json_object_add_value_uint(root, "num_entries_avail",
num_entries);
valid = json_create_array();
num_iter = min(num_entries, log_entries);
for (int i = 0; i < num_iter; i++) {
valid_attrs = json_create_object();
json_object_add_value_uint(valid_attrs, "entry",
le16_to_cpu(pea_log->entries[i]));
json_array_add_value_object(valid, valid_attrs);
}
json_object_add_value_array(root, "list_of_entries", valid);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
void nvme_show_predictable_latency_event_agg_log(
struct nvme_predlat_event_agg_log_page *pea_log,
__u64 log_entries, __u32 size, const char *devname,
enum nvme_print_flags flags)
{
__u64 num_iter;
__u64 num_entries;
if (flags & BINARY)
return d_raw((unsigned char *)pea_log, size);
if (flags & JSON)
return json_predictable_latency_event_agg_log(pea_log,
log_entries);
num_entries = le64_to_cpu(pea_log->num_entries);
printf("Predictable Latency Event Aggregate Log for"\
" device: %s\n", devname);
printf("Number of Entries Available: %"PRIu64"\n",
(uint64_t)num_entries);
num_iter = min(num_entries, log_entries);
for (int i = 0; i < num_iter; i++) {
printf("Entry[%d]: %u\n", i + 1,
le16_to_cpu(pea_log->entries[i]));
}
}
static const char *nvme_show_nss_hw_error(__u16 error_code)
{
switch (error_code) {
case 0x01:
return "PCIe Correctable Error";
case 0x02:
return "PCIe Uncorrectable Non fatal Error";