forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme-print-json.c
2815 lines (2410 loc) · 94.2 KB
/
nvme-print-json.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
// SPDX-License-Identifier: GPL-2.0-or-later
#include <assert.h>
#include "nvme-print-json.h"
#include "util/json.h"
#include "nvme.h"
#include "common.h"
static const uint8_t zero_uuid[16] = { 0 };
void json_nvme_id_ns(struct nvme_id_ns *ns, bool cap_only)
{
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_object *lbafs;
int i;
nvme_uint128_t nvmcap = le128_to_cpu(ns->nvmcap);
root = json_create_object();
if (!cap_only) {
json_object_add_value_uint64(root, "nsze", le64_to_cpu(ns->nsze));
json_object_add_value_uint64(root, "ncap", le64_to_cpu(ns->ncap));
json_object_add_value_uint64(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);
if (!cap_only)
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);
if (!cap_only) {
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, "dlfeat", ns->dlfeat);
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_uint128(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_uint(root, "mcl", le32_to_cpu(ns->mcl));
json_object_add_value_int(root, "msrc", ns->msrc);
}
json_object_add_value_int(root, "nulbaf", ns->nulbaf);
if (!cap_only) {
json_object_add_value_uint(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);
}
void json_nvme_id_ctrl(struct nvme_id_ctrl *ctrl,
void (*vs)(__u8 *vs, struct json_object *root))
{
struct json_object *root;
struct json_object *psds;
nvme_uint128_t tnvmcap = le128_to_cpu(ctrl->tnvmcap);
nvme_uint128_t unvmcap = le128_to_cpu(ctrl->unvmcap);
nvme_uint128_t megcap = le128_to_cpu(ctrl->megcap);
nvme_uint128_t maxdna = le128_to_cpu(ctrl->maxdna);
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_uint(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, "cntrltype", ctrl->cntrltype);
json_object_add_value_string(root, "fguid", util_uuid_to_string(ctrl->fguid));
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, "nvmsr", ctrl->nvmsr);
json_object_add_value_int(root, "vwci", ctrl->vwci);
json_object_add_value_int(root, "mec", ctrl->mec);
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_uint128(root, "tnvmcap", tnvmcap);
json_object_add_value_uint128(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_uint(root, "sanicap", le32_to_cpu(ctrl->sanicap));
json_object_add_value_uint(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, "endgidmax", le16_to_cpu(ctrl->endgidmax));
json_object_add_value_int(root, "anatt",ctrl->anatt);
json_object_add_value_int(root, "anacap", ctrl->anacap);
json_object_add_value_uint(root, "anagrpmax",
le32_to_cpu(ctrl->anagrpmax));
json_object_add_value_uint(root, "nanagrpid",
le32_to_cpu(ctrl->nanagrpid));
json_object_add_value_uint(root, "pels", le32_to_cpu(ctrl->pels));
json_object_add_value_int(root, "domainid", le16_to_cpu(ctrl->domainid));
json_object_add_value_uint128(root, "megcap", megcap);
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_uint(root, "sgls", le32_to_cpu(ctrl->sgls));
json_object_add_value_uint(root, "mnan", le32_to_cpu(ctrl->mnan));
json_object_add_value_uint128(root, "maxdna", maxdna);
json_object_add_value_uint(root, "maxcna", le32_to_cpu(ctrl->maxcna));
if (strlen(subnqn))
json_object_add_value_string(root, "subnqn", subnqn);
json_object_add_value_uint(root, "ioccsz", le32_to_cpu(ctrl->ioccsz));
json_object_add_value_uint(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, "fcatt", ctrl->fcatt);
json_object_add_value_int(root, "msdbd", ctrl->msdbd);
json_object_add_value_int(root, "ofcs", le16_to_cpu(ctrl->ofcs));
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].mp));
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].enlat));
json_object_add_value_uint(psd, "exit_lat",
le32_to_cpu(ctrl->psd[i].exlat));
json_object_add_value_int(psd, "read_tput",
ctrl->psd[i].rrt);
json_object_add_value_int(psd, "read_lat",
ctrl->psd[i].rrl);
json_object_add_value_int(psd, "write_tput",
ctrl->psd[i].rwt);
json_object_add_value_int(psd, "write_lat",
ctrl->psd[i].rwl);
json_object_add_value_int(psd, "idle_power",
le16_to_cpu(ctrl->psd[i].idlp));
json_object_add_value_int(psd, "idle_scale",
nvme_psd_power_scale(ctrl->psd[i].ips));
json_object_add_value_int(psd, "active_power",
le16_to_cpu(ctrl->psd[i].actp));
json_object_add_value_int(psd, "active_power_work",
ctrl->psd[i].apws & 0x7);
json_object_add_value_int(psd, "active_scale",
nvme_psd_power_scale(ctrl->psd[i].apws));
json_array_add_value_object(psds, psd);
}
if(vs)
vs(ctrl->vs, root);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
void json_error_log(struct nvme_error_log_page *err_log, int entries)
{
struct json_object *root;
struct json_object *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_uint64(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 >> 0x1));
json_object_add_value_int(error, "phase_tag",
le16_to_cpu(err_log[i].status_field & 0x1));
json_object_add_value_int(error, "parm_error_location",
le16_to_cpu(err_log[i].parm_error_location));
json_object_add_value_uint64(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_uint64(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);
}
void json_nvme_resv_report(struct nvme_resv_status *status,
int bytes, bool eds)
{
struct json_object *root;
struct json_object *rcs;
int i, j, regctl, entries;
regctl = status->regctl[0] | (status->regctl[1] << 8);
root = json_create_object();
json_object_add_value_uint(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 (!eds) {
/*
* 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_uint64(rc, "hostid",
le64_to_cpu(status->regctl_ds[i].hostid));
json_object_add_value_uint64(rc, "rkey",
le64_to_cpu(status->regctl_ds[i].rkey));
json_array_add_value_object(rcs, rc);
}
} else {
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(status->regctl_eds[i].cntlid));
json_object_add_value_int(rc, "rcsts",
status->regctl_eds[i].rcsts);
json_object_add_value_uint64(rc, "rkey",
le64_to_cpu(status->regctl_eds[i].rkey));
for (j = 0; j < 16; j++)
sprintf(hostid + j * 2, "%02x",
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);
}
void json_fw_log(struct nvme_firmware_slot *fw_log, const char *devname)
{
struct json_object *root;
struct json_object *fwsi;
char fmt[21];
char str[32];
int i;
__le64 *frs;
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][0]) {
snprintf(fmt, sizeof(fmt), "Firmware Rev Slot %d",
i + 1);
frs = (__le64 *)&fw_log->frs[i];
snprintf(str, sizeof(str), "%"PRIu64" (%s)",
le64_to_cpu(*frs),
util_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);
}
void json_changed_ns_list_log(struct nvme_ns_list *log,
const char *devname)
{
struct json_object *root;
struct json_object *nsi;
char fmt[32];
char str[32];
__u32 nsid;
int i;
if (log->ns[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_ID_NS_LIST_MAX; i++) {
nsid = le32_to_cpu(log->ns[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);
}
void json_endurance_log(struct nvme_endurance_group_log *endurance_group,
__u16 group_id)
{
struct json_object *root;
nvme_uint128_t endurance_estimate =
le128_to_cpu(endurance_group->endurance_estimate);
nvme_uint128_t data_units_read =
le128_to_cpu(endurance_group->data_units_read);
nvme_uint128_t data_units_written =
le128_to_cpu(endurance_group->data_units_written);
nvme_uint128_t media_units_written =
le128_to_cpu(endurance_group->media_units_written);
nvme_uint128_t host_read_cmds =
le128_to_cpu(endurance_group->host_read_cmds);
nvme_uint128_t host_write_cmds =
le128_to_cpu(endurance_group->host_write_cmds);
nvme_uint128_t media_data_integrity_err =
le128_to_cpu(endurance_group->media_data_integrity_err);
nvme_uint128_t num_err_info_log_entries =
le128_to_cpu(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_uint128(root, "endurance_estimate",
endurance_estimate);
json_object_add_value_uint128(root, "data_units_read", data_units_read);
json_object_add_value_uint128(root, "data_units_written",
data_units_written);
json_object_add_value_uint128(root, "media_units_written",
media_units_written);
json_object_add_value_uint128(root, "host_read_cmds", host_read_cmds);
json_object_add_value_uint128(root, "host_write_cmds", host_write_cmds);
json_object_add_value_uint128(root, "media_data_integrity_err",
media_data_integrity_err);
json_object_add_value_uint128(root, "num_err_info_log_entries",
num_err_info_log_entries);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
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]);
nvme_uint128_t data_units_read = le128_to_cpu(smart->data_units_read);
nvme_uint128_t data_units_written = le128_to_cpu(smart->data_units_written);
nvme_uint128_t host_read_commands = le128_to_cpu(smart->host_reads);
nvme_uint128_t host_write_commands = le128_to_cpu(smart->host_writes);
nvme_uint128_t controller_busy_time = le128_to_cpu(smart->ctrl_busy_time);
nvme_uint128_t power_cycles = le128_to_cpu(smart->power_cycles);
nvme_uint128_t power_on_hours = le128_to_cpu(smart->power_on_hours);
nvme_uint128_t unsafe_shutdowns = le128_to_cpu(smart->unsafe_shutdowns);
nvme_uint128_t media_errors = le128_to_cpu(smart->media_errors);
nvme_uint128_t num_err_log_entries = le128_to_cpu(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_uint128(root, "data_units_read", data_units_read);
json_object_add_value_uint128(root, "data_units_written",
data_units_written);
json_object_add_value_uint128(root, "host_read_commands",
host_read_commands);
json_object_add_value_uint128(root, "host_write_commands",
host_write_commands);
json_object_add_value_uint128(root, "controller_busy_time",
controller_busy_time);
json_object_add_value_uint128(root, "power_cycles", power_cycles);
json_object_add_value_uint128(root, "power_on_hours", power_on_hours);
json_object_add_value_uint128(root, "unsafe_shutdowns", unsafe_shutdowns);
json_object_add_value_uint128(root, "media_errors", media_errors);
json_object_add_value_uint128(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);
}
void json_ana_log(struct nvme_ana_log *ana_log, const char *devname)
{
int offset = sizeof(struct nvme_ana_log);
struct nvme_ana_log *hdr = ana_log;
struct nvme_ana_group_desc *ana_desc;
struct json_object *desc_list;
struct json_object *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_uint64(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);
}
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_object *valid;
int i;
__u32 num_entries;
root = json_create_object();
json_object_add_value_int(root, "Current Device Self-Test Operation",
self_test->current_operation);
json_object_add_value_int(root, "Current Device Self-Test Completion",
self_test->completion);
valid = json_create_array();
num_entries = min(dst_entries, NVME_LOG_ST_MAX_RESULTS);
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_uint64(valid_attrs, "Power on hours",
le64_to_cpu(self_test->result[i].poh));
if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_NSID)
json_object_add_value_uint(valid_attrs, "Namespace Identifier",
le32_to_cpu(self_test->result[i].nsid));
if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_FLBA) {
json_object_add_value_uint64(valid_attrs, "Failing LBA",
le64_to_cpu(self_test->result[i].flba));
}
if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_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_DIAG_INFO_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);
}
struct json_object* json_effects_log(enum nvme_csi csi,
struct nvme_cmd_effects_log *effects_log)
{
struct json_object *root;
struct json_object *acs;
struct json_object *iocs;
unsigned int opcode;
char key[128];
__u32 effect;
root = json_create_object();
json_object_add_value_uint(root, "command_set_identifier", csi);
acs = json_create_object();
for (opcode = 0; opcode < 256; opcode++) {
effect = le32_to_cpu(effects_log->acs[opcode]);
if (effect & NVME_CMD_EFFECTS_CSUPP) {
sprintf(key, "ACS_%u (%s)", opcode,
nvme_cmd_to_string(1, opcode));
json_object_add_value_uint(acs, key, effect);
}
}
json_object_add_value_object(root, "admin_cmd_set", acs);
iocs = json_create_object();
for (opcode = 0; opcode < 256; opcode++) {
effect = le32_to_cpu(effects_log->iocs[opcode]);
if (effect & NVME_CMD_EFFECTS_CSUPP) {
sprintf(key, "IOCS_%u (%s)", opcode,
nvme_cmd_to_string(0, opcode));
json_object_add_value_uint(iocs, key, effect);
}
}
json_object_add_value_object(root, "io_cmd_set", iocs);
return root;
}
void json_effects_log_list(struct list_head *list)
{
struct json_object *json_list;
nvme_effects_log_node_t *node;
json_list = json_create_array();
list_for_each(list, node, node) {
struct json_object *json_page =
json_effects_log(node->csi, &node->effects);
json_array_add_value_object(json_list, json_page);
}
json_print_object(json_list, NULL);
printf("\n");
json_free_object(json_list);
}
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->sstat);
root = json_create_object();
dev = json_create_object();
sstat = json_create_object();
json_object_add_value_int(dev, "sprog",
le16_to_cpu(sanitize_log->sprog));
json_object_add_value_int(sstat, "global_erased",
(status & NVME_SANITIZE_SSTAT_GLOBAL_DATA_ERASED) >> 8);
json_object_add_value_int(sstat, "no_cmplted_passes",
(status >> NVME_SANITIZE_SSTAT_COMPLETED_PASSES_SHIFT) &
NVME_SANITIZE_SSTAT_COMPLETED_PASSES_MASK);
status_str = get_sanitize_log_sstat_status_str(status);
sprintf(str, "(%d) %s", status & NVME_SANITIZE_SSTAT_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->scdw10));
json_object_add_value_uint(dev, "time_over_write",
le32_to_cpu(sanitize_log->eto));
json_object_add_value_uint(dev, "time_block_erase",
le32_to_cpu(sanitize_log->etbe));
json_object_add_value_uint(dev, "time_crypto_erase",
le32_to_cpu(sanitize_log->etce));
json_object_add_value_uint(dev, "time_over_write_no_dealloc",
le32_to_cpu(sanitize_log->etond));
json_object_add_value_uint(dev, "time_block_erase_no_dealloc",
le32_to_cpu(sanitize_log->etbend));
json_object_add_value_uint(dev, "time_crypto_erase_no_dealloc",
le32_to_cpu(sanitize_log->etcend));
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_nvmset_predictable_lat_log *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_uint64(root, "dtwin_reads_typical",
le64_to_cpu(plpns_log->dtwin_rt));
json_object_add_value_uint64(root, "dtwin_writes_typical",
le64_to_cpu(plpns_log->dtwin_wt));
json_object_add_value_uint64(root, "dtwin_time_maximum",
le64_to_cpu(plpns_log->dtwin_tmax));
json_object_add_value_uint64(root, "ndwin_time_minimum_high",
le64_to_cpu(plpns_log->ndwin_tmin_hi));
json_object_add_value_uint64(root, "ndwin_time_minimum_low",
le64_to_cpu(plpns_log->ndwin_tmin_lo));
json_object_add_value_uint64(root, "dtwin_reads_estimate",
le64_to_cpu(plpns_log->dtwin_re));
json_object_add_value_uint64(root, "dtwin_writes_estimate",
le64_to_cpu(plpns_log->dtwin_we));
json_object_add_value_uint64(root, "dtwin_time_estimate",
le64_to_cpu(plpns_log->dtwin_te));
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
void json_predictable_latency_event_agg_log(
struct nvme_aggregate_predictable_lat_event *pea_log,
__u64 log_entries)
{
struct json_object *root;
struct json_object *valid_attrs;
struct json_object *valid;
__u64 num_iter;
__u64 num_entries;
root = json_create_object();
num_entries = le64_to_cpu(pea_log->num_entries);
json_object_add_value_uint64(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 add_bitmap(int i, __u8 seb, struct json_object *root, int json_flag)
{
char evt_str[50];
char key[128];
for (int bit = 0; bit < 8; bit++) {
if (nvme_pel_event_to_string(bit + i * 8)) {
if (json_flag == 1) {
sprintf(key, "bitmap_%x", (bit + i * 8));
if ((seb >> bit) & 0x1)
snprintf(evt_str, sizeof(evt_str), "Support %s",
nvme_pel_event_to_string(bit + i * 8));
json_object_add_value_string(root, key, evt_str);
} else {
if (nvme_pel_event_to_string(bit + i * 8))
if ((seb >> bit) & 0x1)
printf(" Support %s\n",
nvme_pel_event_to_string(bit + i * 8));
}
}
}
}
void json_persistent_event_log(void *pevent_log_info, __u32 size)
{
struct json_object *root;
struct json_object *valid_attrs;
struct json_object *valid;
__u32 offset, por_info_len, por_info_list;
__u64 *fw_rev;
char key[128];
char fw_str[50];
struct nvme_smart_log *smart_event;
struct nvme_fw_commit_event *fw_commit_event;
struct nvme_time_stamp_change_event *ts_change_event;
struct nvme_power_on_reset_info_list *por_event;
struct nvme_nss_hw_err_event *nss_hw_err_event;
struct nvme_change_ns_event *ns_event;
struct nvme_format_nvm_start_event *format_start_event;
struct nvme_format_nvm_compln_event *format_cmpln_event;
struct nvme_sanitize_start_event *sanitize_start_event;
struct nvme_sanitize_compln_event *sanitize_cmpln_event;
struct nvme_thermal_exc_event *thermal_exc_event;
struct nvme_persistent_event_log *pevent_log_head;
struct nvme_persistent_event_entry *pevent_entry_head;
root = json_create_object();
valid = json_create_array();
offset = sizeof(*pevent_log_head);
if (size >= offset) {
pevent_log_head = pevent_log_info;
char sn[sizeof(pevent_log_head->sn) + 1],
mn[sizeof(pevent_log_head->mn) + 1],
subnqn[sizeof(pevent_log_head->subnqn) + 1];
snprintf(sn, sizeof(sn), "%-.*s",
(int)sizeof(pevent_log_head->sn), pevent_log_head->sn);
snprintf(mn, sizeof(mn), "%-.*s",
(int)sizeof(pevent_log_head->mn), pevent_log_head->mn);
snprintf(subnqn, sizeof(subnqn), "%-.*s",
(int)sizeof(pevent_log_head->subnqn), pevent_log_head->subnqn);
json_object_add_value_uint(root, "log_id",
pevent_log_head->lid);
json_object_add_value_uint(root, "total_num_of_events",
le32_to_cpu(pevent_log_head->tnev));
json_object_add_value_uint64(root, "total_log_len",
le64_to_cpu(pevent_log_head->tll));
json_object_add_value_uint(root, "log_revision",
pevent_log_head->rv);
json_object_add_value_uint(root, "log_header_len",
le16_to_cpu(pevent_log_head->lhl));
json_object_add_value_uint64(root, "timestamp",
le64_to_cpu(pevent_log_head->ts));
json_object_add_value_uint128(root, "power_on_hours",
le128_to_cpu(pevent_log_head->poh));
json_object_add_value_uint64(root, "power_cycle_count",
le64_to_cpu(pevent_log_head->pcc));
json_object_add_value_uint(root, "pci_vid",
le16_to_cpu(pevent_log_head->vid));
json_object_add_value_uint(root, "pci_ssvid",
le16_to_cpu(pevent_log_head->ssvid));
json_object_add_value_string(root, "sn", sn);
json_object_add_value_string(root, "mn", mn);
json_object_add_value_string(root, "subnqn", subnqn);
json_object_add_value_uint(root, "gen_number",
le16_to_cpu(pevent_log_head->gen_number));
json_object_add_value_uint(root, "rci",
le32_to_cpu(pevent_log_head->rci));
for (int i = 0; i < 32; i++) {
if (pevent_log_head->seb[i] == 0)
continue;
add_bitmap(i, pevent_log_head->seb[i], root, 1);
}
} else {
printf("No log data can be shown with this log len at least " \
"512 bytes is required or can be 0 to read the complete "\
"log page after context established\n");
return;
}
for (int i = 0; i < le32_to_cpu(pevent_log_head->tnev); i++) {
if (offset + sizeof(*pevent_entry_head) >= size)
break;
pevent_entry_head = pevent_log_info + offset;
if ((offset + pevent_entry_head->ehl + 3 +
le16_to_cpu(pevent_entry_head->el)) >= size)
break;
valid_attrs = json_create_object();
json_object_add_value_uint(valid_attrs, "event_number", i);
json_object_add_value_string(valid_attrs, "event_type",
nvme_pel_event_to_string(pevent_entry_head->etype));
json_object_add_value_uint(valid_attrs, "event_type_rev",
pevent_entry_head->etype_rev);
json_object_add_value_uint(valid_attrs, "event_header_len",
pevent_entry_head->ehl);
json_object_add_value_uint(valid_attrs, "event_header_additional_info",
pevent_entry_head->ehai);
json_object_add_value_uint(valid_attrs, "ctrl_id",
le16_to_cpu(pevent_entry_head->cntlid));
json_object_add_value_uint64(valid_attrs, "event_time_stamp",
le64_to_cpu(pevent_entry_head->ets));
json_object_add_value_uint(valid_attrs, "port_id",
le16_to_cpu(pevent_entry_head->pelpid));
json_object_add_value_uint(valid_attrs, "vu_info_len",
le16_to_cpu(pevent_entry_head->vsil));