forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvme-print-json.c
4749 lines (3915 loc) · 149 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 <errno.h>
#include <time.h>
#include "nvme-print.h"
#include "util/json.h"
#include "nvme.h"
#include "common.h"
#define ERROR_MSG_LEN 100
#define NAME_LEN 128
#define BUF_LEN 320
#define VAL_LEN 4096
#define BYTE_TO_BIT(byte) ((byte) * 8)
#define MS_TO_SEC(time) ((time) / 1000)
#define MS500_TO_MS(time) ((time) * 500)
#define MS500_TO_SEC(time) (MS_TO_SEC(MS500_TO_MS(time)))
#define array_add_obj json_array_add_value_object
#define array_add_str json_array_add_value_string
#define obj_add_array json_object_add_value_array
#define obj_add_int json_object_add_value_int
#define obj_add_obj json_object_add_value_object
#define obj_add_str json_object_add_value_string
#define obj_add_uint json_object_add_value_uint
#define obj_add_uint128 json_object_add_value_uint128
#define obj_add_uint64 json_object_add_value_uint64
static const uint8_t zero_uuid[16] = { 0 };
static struct print_ops json_print_ops;
static struct json_object *json_r;
static void json_feature_show_fields(enum nvme_features_id fid, unsigned int result,
unsigned char *buf);
static void d_json(unsigned char *buf, int len, int width, int group, struct json_object *array)
{
int i;
char ascii[32 + 1] = { 0 };
assert(width < sizeof(ascii));
for (i = 0; i < len; i++) {
ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
if (!((i + 1) % width)) {
array_add_str(array, ascii);
memset(ascii, 0, sizeof(ascii));
}
}
if (strlen(ascii)) {
ascii[i % width + 1] = '\0';
array_add_str(array, ascii);
}
}
static void obj_d(struct json_object *o, const char *k, unsigned char *buf, int len, int width,
int group)
{
struct json_object *data = json_create_array();
d_json(buf, len, width, group, data);
obj_add_array(o, k, data);
}
static void obj_add_uint_x(struct json_object *o, const char *k, __u32 v)
{
char str[STR_LEN];
sprintf(str, "%x", v);
obj_add_str(o, k, str);
}
static void obj_add_uint_0x(struct json_object *o, const char *k, __u32 v)
{
char str[STR_LEN];
sprintf(str, "0x%x", v);
obj_add_str(o, k, str);
}
static void obj_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width)
{
char str[STR_LEN];
sprintf(str, "0x%02x", v);
obj_add_str(o, k, str);
}
static void obj_add_uint_02x(struct json_object *o, const char *k, __u32 v)
{
obj_add_uint_0nx(o, k, v, 2);
}
static void obj_add_uint_nx(struct json_object *o, const char *k, __u32 v)
{
char str[STR_LEN];
sprintf(str, "%#x", v);
obj_add_str(o, k, str);
}
static void obj_add_nprix64(struct json_object *o, const char *k, uint64_t v)
{
char str[STR_LEN];
sprintf(str, "%#"PRIx64"", v);
obj_add_str(o, k, str);
}
static void obj_add_prix64(struct json_object *o, const char *k, uint64_t v)
{
char str[STR_LEN];
sprintf(str, "%"PRIx64"", v);
obj_add_str(o, k, str);
}
static void obj_add_int_secs(struct json_object *o, const char *k, int v)
{
char str[STR_LEN];
sprintf(str, "%d secs", v);
obj_add_str(o, k, str);
}
static void obj_add_result(struct json_object *o, const char *v, ...)
{
va_list ap;
va_start(ap, v);
char *value;
if (vasprintf(&value, v, ap) < 0)
value = NULL;
if (value)
obj_add_str(o, "Result", value);
else
obj_add_str(o, "Result", "Could not allocate string");
va_end(ap);
free(value);
}
static void obj_add_key(struct json_object *o, const char *k, const char *v, ...)
{
va_list ap;
va_start(ap, v);
char *value;
if (vasprintf(&value, v, ap) < 0)
value = NULL;
if (value)
obj_add_str(o, k, value);
else
obj_add_str(o, k, "Could not allocate string");
va_end(ap);
free(value);
}
static struct json_object *obj_create_array_obj(struct json_object *o, const char *k)
{
struct json_object *array = json_create_array();
struct json_object *obj = json_create_object();
obj_add_array(o, k, array);
array_add_obj(array, obj);
return obj;
}
static struct json_object *obj_create(const char *k)
{
struct json_object *array;
struct json_object *obj = json_create_object();
if (json_r) {
array = json_create_array();
obj_add_array(json_r, k, array);
array_add_obj(array, obj);
}
return obj;
}
static void json_print(struct json_object *r)
{
json_print_object(r, NULL);
printf("\n");
json_free_object(r);
}
static void obj_print(struct json_object *o)
{
if (!json_r)
json_print(o);
}
static bool human(void)
{
return json_print_ops.flags & VERBOSE;
}
static void json_id_iocs(struct nvme_id_iocs *iocs)
{
struct json_object *r = json_create_object();
char json_str[STR_LEN];
__u16 i;
for (i = 0; i < ARRAY_SIZE(iocs->iocsc); i++) {
if (iocs->iocsc[i]) {
sprintf(json_str, "I/O Command Set Combination[%u]", i);
obj_add_uint64(r, json_str, le64_to_cpu(iocs->iocsc[i]));
}
}
json_print(r);
}
static void json_nvme_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
unsigned int lba_index, 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 *r = json_create_object();
struct json_object *lbafs = json_create_array();
struct json_object *vs = json_create_array();
int i;
nvme_uint128_t nvmcap = le128_to_cpu(ns->nvmcap);
if (!cap_only) {
obj_add_uint64(r, "nsze", le64_to_cpu(ns->nsze));
obj_add_uint64(r, "ncap", le64_to_cpu(ns->ncap));
obj_add_uint64(r, "nuse", le64_to_cpu(ns->nuse));
obj_add_int(r, "nsfeat", ns->nsfeat);
}
obj_add_int(r, "nlbaf", ns->nlbaf);
if (!cap_only)
obj_add_int(r, "flbas", ns->flbas);
obj_add_int(r, "mc", ns->mc);
obj_add_int(r, "dpc", ns->dpc);
if (!cap_only) {
obj_add_int(r, "dps", ns->dps);
obj_add_int(r, "nmic", ns->nmic);
obj_add_int(r, "rescap", ns->rescap);
obj_add_int(r, "fpi", ns->fpi);
obj_add_int(r, "dlfeat", ns->dlfeat);
obj_add_int(r, "nawun", le16_to_cpu(ns->nawun));
obj_add_int(r, "nawupf", le16_to_cpu(ns->nawupf));
obj_add_int(r, "nacwu", le16_to_cpu(ns->nacwu));
obj_add_int(r, "nabsn", le16_to_cpu(ns->nabsn));
obj_add_int(r, "nabo", le16_to_cpu(ns->nabo));
obj_add_int(r, "nabspf", le16_to_cpu(ns->nabspf));
obj_add_int(r, "noiob", le16_to_cpu(ns->noiob));
obj_add_uint128(r, "nvmcap", nvmcap);
obj_add_int(r, "nsattr", ns->nsattr);
obj_add_int(r, "nvmsetid", le16_to_cpu(ns->nvmsetid));
if (ns->nsfeat & 0x30) {
obj_add_int(r, "npwg", le16_to_cpu(ns->npwg));
obj_add_int(r, "npwa", le16_to_cpu(ns->npwa));
if (ns->nsfeat & 0x10)
obj_add_int(r, "npdg", le16_to_cpu(ns->npdg));
obj_add_int(r, "npda", le16_to_cpu(ns->npda));
obj_add_int(r, "nows", le16_to_cpu(ns->nows));
}
obj_add_int(r, "mssrl", le16_to_cpu(ns->mssrl));
obj_add_uint(r, "mcl", le32_to_cpu(ns->mcl));
obj_add_int(r, "msrc", ns->msrc);
}
obj_add_int(r, "nulbaf", ns->nulbaf);
if (!cap_only) {
obj_add_uint(r, "anagrpid", le32_to_cpu(ns->anagrpid));
obj_add_int(r, "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]);
obj_add_str(r, "eui64", eui64_buf);
obj_add_str(r, "nguid", nguid_buf);
}
obj_add_array(r, "lbafs", lbafs);
for (i = 0; i <= ns->nlbaf; i++) {
struct json_object *lbaf = json_create_object();
obj_add_int(lbaf, "ms", le16_to_cpu(ns->lbaf[i].ms));
obj_add_int(lbaf, "ds", ns->lbaf[i].ds);
obj_add_int(lbaf, "rp", ns->lbaf[i].rp);
array_add_obj(lbafs, lbaf);
}
d_json(ns->vs, strnlen((const char *)ns->vs, sizeof(ns->vs)), 16, 1, vs);
obj_add_array(r, "vs", vs);
json_print(r);
}
void json_nvme_id_ctrl(struct nvme_id_ctrl *ctrl,
void (*vs)(__u8 *vs, struct json_object *r))
{
struct json_object *r = json_create_object();
struct json_object *psds = json_create_array();
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);
obj_add_int(r, "vid", le16_to_cpu(ctrl->vid));
obj_add_int(r, "ssvid", le16_to_cpu(ctrl->ssvid));
obj_add_str(r, "sn", sn);
obj_add_str(r, "mn", mn);
obj_add_str(r, "fr", fr);
obj_add_int(r, "rab", ctrl->rab);
obj_add_int(r, "ieee", ieee);
obj_add_int(r, "cmic", ctrl->cmic);
obj_add_int(r, "mdts", ctrl->mdts);
obj_add_int(r, "cntlid", le16_to_cpu(ctrl->cntlid));
obj_add_uint(r, "ver", le32_to_cpu(ctrl->ver));
obj_add_uint(r, "rtd3r", le32_to_cpu(ctrl->rtd3r));
obj_add_uint(r, "rtd3e", le32_to_cpu(ctrl->rtd3e));
obj_add_uint(r, "oaes", le32_to_cpu(ctrl->oaes));
obj_add_uint(r, "ctratt", le32_to_cpu(ctrl->ctratt));
obj_add_int(r, "rrls", le16_to_cpu(ctrl->rrls));
obj_add_int(r, "cntrltype", ctrl->cntrltype);
obj_add_str(r, "fguid", util_uuid_to_string(ctrl->fguid));
obj_add_int(r, "crdt1", le16_to_cpu(ctrl->crdt1));
obj_add_int(r, "crdt2", le16_to_cpu(ctrl->crdt2));
obj_add_int(r, "crdt3", le16_to_cpu(ctrl->crdt3));
obj_add_int(r, "nvmsr", ctrl->nvmsr);
obj_add_int(r, "vwci", ctrl->vwci);
obj_add_int(r, "mec", ctrl->mec);
obj_add_int(r, "oacs", le16_to_cpu(ctrl->oacs));
obj_add_int(r, "acl", ctrl->acl);
obj_add_int(r, "aerl", ctrl->aerl);
obj_add_int(r, "frmw", ctrl->frmw);
obj_add_int(r, "lpa", ctrl->lpa);
obj_add_int(r, "elpe", ctrl->elpe);
obj_add_int(r, "npss", ctrl->npss);
obj_add_int(r, "avscc", ctrl->avscc);
obj_add_int(r, "apsta", ctrl->apsta);
obj_add_int(r, "wctemp", le16_to_cpu(ctrl->wctemp));
obj_add_int(r, "cctemp", le16_to_cpu(ctrl->cctemp));
obj_add_int(r, "mtfa", le16_to_cpu(ctrl->mtfa));
obj_add_uint(r, "hmpre", le32_to_cpu(ctrl->hmpre));
obj_add_uint(r, "hmmin", le32_to_cpu(ctrl->hmmin));
obj_add_uint128(r, "tnvmcap", tnvmcap);
obj_add_uint128(r, "unvmcap", unvmcap);
obj_add_uint(r, "rpmbs", le32_to_cpu(ctrl->rpmbs));
obj_add_int(r, "edstt", le16_to_cpu(ctrl->edstt));
obj_add_int(r, "dsto", ctrl->dsto);
obj_add_int(r, "fwug", ctrl->fwug);
obj_add_int(r, "kas", le16_to_cpu(ctrl->kas));
obj_add_int(r, "hctma", le16_to_cpu(ctrl->hctma));
obj_add_int(r, "mntmt", le16_to_cpu(ctrl->mntmt));
obj_add_int(r, "mxtmt", le16_to_cpu(ctrl->mxtmt));
obj_add_uint(r, "sanicap", le32_to_cpu(ctrl->sanicap));
obj_add_uint(r, "hmminds", le32_to_cpu(ctrl->hmminds));
obj_add_int(r, "hmmaxd", le16_to_cpu(ctrl->hmmaxd));
obj_add_int(r, "nsetidmax", le16_to_cpu(ctrl->nsetidmax));
obj_add_int(r, "endgidmax", le16_to_cpu(ctrl->endgidmax));
obj_add_int(r, "anatt", ctrl->anatt);
obj_add_int(r, "anacap", ctrl->anacap);
obj_add_uint(r, "anagrpmax", le32_to_cpu(ctrl->anagrpmax));
obj_add_uint(r, "nanagrpid", le32_to_cpu(ctrl->nanagrpid));
obj_add_uint(r, "pels", le32_to_cpu(ctrl->pels));
obj_add_int(r, "domainid", le16_to_cpu(ctrl->domainid));
obj_add_uint128(r, "megcap", megcap);
obj_add_int(r, "sqes", ctrl->sqes);
obj_add_int(r, "cqes", ctrl->cqes);
obj_add_int(r, "maxcmd", le16_to_cpu(ctrl->maxcmd));
obj_add_uint(r, "nn", le32_to_cpu(ctrl->nn));
obj_add_int(r, "oncs", le16_to_cpu(ctrl->oncs));
obj_add_int(r, "fuses", le16_to_cpu(ctrl->fuses));
obj_add_int(r, "fna", ctrl->fna);
obj_add_int(r, "vwc", ctrl->vwc);
obj_add_int(r, "awun", le16_to_cpu(ctrl->awun));
obj_add_int(r, "awupf", le16_to_cpu(ctrl->awupf));
obj_add_int(r, "icsvscc", ctrl->icsvscc);
obj_add_int(r, "nwpc", ctrl->nwpc);
obj_add_int(r, "acwu", le16_to_cpu(ctrl->acwu));
obj_add_int(r, "ocfs", le16_to_cpu(ctrl->ocfs));
obj_add_uint(r, "sgls", le32_to_cpu(ctrl->sgls));
obj_add_uint(r, "mnan", le32_to_cpu(ctrl->mnan));
obj_add_uint128(r, "maxdna", maxdna);
obj_add_uint(r, "maxcna", le32_to_cpu(ctrl->maxcna));
obj_add_uint(r, "oaqd", le32_to_cpu(ctrl->oaqd));
if (strlen(subnqn))
obj_add_str(r, "subnqn", subnqn);
obj_add_uint(r, "ioccsz", le32_to_cpu(ctrl->ioccsz));
obj_add_uint(r, "iorcsz", le32_to_cpu(ctrl->iorcsz));
obj_add_int(r, "icdoff", le16_to_cpu(ctrl->icdoff));
obj_add_int(r, "fcatt", ctrl->fcatt);
obj_add_int(r, "msdbd", ctrl->msdbd);
obj_add_int(r, "ofcs", le16_to_cpu(ctrl->ofcs));
obj_add_array(r, "psds", psds);
for (i = 0; i <= ctrl->npss; i++) {
struct json_object *psd = json_create_object();
obj_add_int(psd, "max_power", le16_to_cpu(ctrl->psd[i].mp));
obj_add_int(psd, "max_power_scale", ctrl->psd[i].flags & 0x1);
obj_add_int(psd, "non-operational_state", (ctrl->psd[i].flags & 2) >> 1);
obj_add_uint(psd, "entry_lat", le32_to_cpu(ctrl->psd[i].enlat));
obj_add_uint(psd, "exit_lat", le32_to_cpu(ctrl->psd[i].exlat));
obj_add_int(psd, "read_tput", ctrl->psd[i].rrt);
obj_add_int(psd, "read_lat", ctrl->psd[i].rrl);
obj_add_int(psd, "write_tput", ctrl->psd[i].rwt);
obj_add_int(psd, "write_lat", ctrl->psd[i].rwl);
obj_add_int(psd, "idle_power", le16_to_cpu(ctrl->psd[i].idlp));
obj_add_int(psd, "idle_scale", nvme_psd_power_scale(ctrl->psd[i].ips));
obj_add_int(psd, "active_power", le16_to_cpu(ctrl->psd[i].actp));
obj_add_int(psd, "active_power_work", ctrl->psd[i].apws & 7);
obj_add_int(psd, "active_scale", nvme_psd_power_scale(ctrl->psd[i].apws));
array_add_obj(psds, psd);
}
if (vs)
vs(ctrl->vs, r);
json_print(r);
}
static void json_error_log(struct nvme_error_log_page *err_log, int entries,
const char *devname)
{
struct json_object *r = json_create_object();
struct json_object *errors = json_create_array();
int i;
obj_add_array(r, "errors", errors);
for (i = 0; i < entries; i++) {
struct json_object *error = json_create_object();
obj_add_uint64(error, "error_count", le64_to_cpu(err_log[i].error_count));
obj_add_int(error, "sqid", le16_to_cpu(err_log[i].sqid));
obj_add_int(error, "cmdid", le16_to_cpu(err_log[i].cmdid));
obj_add_int(error, "status_field", le16_to_cpu(err_log[i].status_field >> 0x1));
obj_add_int(error, "phase_tag", le16_to_cpu(err_log[i].status_field & 0x1));
obj_add_int(error, "parm_error_location",
le16_to_cpu(err_log[i].parm_error_location));
obj_add_uint64(error, "lba", le64_to_cpu(err_log[i].lba));
obj_add_uint(error, "nsid", le32_to_cpu(err_log[i].nsid));
obj_add_int(error, "vs", err_log[i].vs);
obj_add_int(error, "trtype", err_log[i].trtype);
obj_add_uint64(error, "cs", le64_to_cpu(err_log[i].cs));
obj_add_int(error, "trtype_spec_info", le16_to_cpu(err_log[i].trtype_spec_info));
array_add_obj(errors, error);
}
json_print(r);
}
void json_nvme_resv_report(struct nvme_resv_status *status,
int bytes, bool eds)
{
struct json_object *r = json_create_object();
struct json_object *rcs = json_create_array();
int i, j, entries;
int regctl = status->regctl[0] | (status->regctl[1] << 8);
obj_add_uint(r, "gen", le32_to_cpu(status->gen));
obj_add_int(r, "rtype", status->rtype);
obj_add_int(r, "regctl", regctl);
obj_add_int(r, "ptpls", status->ptpls);
/* 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;
obj_add_array(r, "regctls", rcs);
for (i = 0; i < regctl; i++) {
struct json_object *rc = json_create_object();
obj_add_int(rc, "cntlid", le16_to_cpu(status->regctl_ds[i].cntlid));
obj_add_int(rc, "rcsts", status->regctl_ds[i].rcsts);
obj_add_uint64(rc, "hostid", le64_to_cpu(status->regctl_ds[i].hostid));
obj_add_uint64(rc, "rkey", le64_to_cpu(status->regctl_ds[i].rkey));
array_add_obj(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;
obj_add_array(r, "regctlext", rcs);
for (i = 0; i < regctl; i++) {
struct json_object *rc = json_create_object();
obj_add_int(rc, "cntlid", le16_to_cpu(status->regctl_eds[i].cntlid));
obj_add_int(rc, "rcsts", status->regctl_eds[i].rcsts);
obj_add_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]);
obj_add_str(rc, "hostid", hostid);
array_add_obj(rcs, rc);
}
}
json_print(r);
}
void json_fw_log(struct nvme_firmware_slot *fw_log, const char *devname)
{
struct json_object *r = json_create_object();
struct json_object *fwsi = json_create_object();
char fmt[21];
char str[32];
int i;
__le64 *frs;
obj_add_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]));
obj_add_str(fwsi, fmt, str);
}
}
obj_add_obj(r, devname, fwsi);
json_print(r);
}
void json_changed_ns_list_log(struct nvme_ns_list *log,
const char *devname)
{
struct json_object *r = json_create_object();
struct json_object *nsi = json_create_object();
char fmt[32];
char str[32];
__u32 nsid;
int i;
if (log->ns[0] == cpu_to_le32(0xffffffff))
return;
obj_add_str(r, "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);
obj_add_str(nsi, fmt, str);
}
obj_add_obj(r, devname, nsi);
json_print(r);
}
static void json_endurance_log(struct nvme_endurance_group_log *endurance_group, __u16 group_id,
const char *devname)
{
struct json_object *r = json_create_object();
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);
nvme_uint128_t total_end_grp_cap = le128_to_cpu(endurance_group->total_end_grp_cap);
nvme_uint128_t unalloc_end_grp_cap = le128_to_cpu(endurance_group->unalloc_end_grp_cap);
obj_add_int(r, "critical_warning", endurance_group->critical_warning);
obj_add_int(r, "endurance_group_features", endurance_group->endurance_group_features);
obj_add_int(r, "avl_spare", endurance_group->avl_spare);
obj_add_int(r, "avl_spare_threshold", endurance_group->avl_spare_threshold);
obj_add_int(r, "percent_used", endurance_group->percent_used);
obj_add_int(r, "domain_identifier", endurance_group->domain_identifier);
obj_add_uint128(r, "endurance_estimate", endurance_estimate);
obj_add_uint128(r, "data_units_read", data_units_read);
obj_add_uint128(r, "data_units_written", data_units_written);
obj_add_uint128(r, "media_units_written", media_units_written);
obj_add_uint128(r, "host_read_cmds", host_read_cmds);
obj_add_uint128(r, "host_write_cmds", host_write_cmds);
obj_add_uint128(r, "media_data_integrity_err", media_data_integrity_err);
obj_add_uint128(r, "num_err_info_log_entries", num_err_info_log_entries);
obj_add_uint128(r, "total_end_grp_cap", total_end_grp_cap);
obj_add_uint128(r, "unalloc_end_grp_cap", unalloc_end_grp_cap);
json_print(r);
}
static void json_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
const char *devname)
{
struct json_object *r = json_create_object();
int c;
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);
if (human()) {
struct json_object *crt = json_create_object();
obj_add_int(crt, "value", smart->critical_warning);
obj_add_int(crt, "available_spare", smart->critical_warning & 1);
obj_add_int(crt, "temp_threshold", (smart->critical_warning & 2) >> 1);
obj_add_int(crt, "reliability_degraded", (smart->critical_warning & 4) >> 2);
obj_add_int(crt, "ro", (smart->critical_warning & 8) >> 3);
obj_add_int(crt, "vmbu_failed", (smart->critical_warning & 0x10) >> 4);
obj_add_int(crt, "pmr_ro", (smart->critical_warning & 0x20) >> 5);
obj_add_obj(r, "critical_warning", crt);
} else {
obj_add_int(r, "critical_warning", smart->critical_warning);
}
obj_add_int(r, "temperature", temperature);
obj_add_int(r, "avail_spare", smart->avail_spare);
obj_add_int(r, "spare_thresh", smart->spare_thresh);
obj_add_int(r, "percent_used", smart->percent_used);
obj_add_int(r, "endurance_grp_critical_warning_summary", smart->endu_grp_crit_warn_sumry);
obj_add_uint128(r, "data_units_read", data_units_read);
obj_add_uint128(r, "data_units_written", data_units_written);
obj_add_uint128(r, "host_read_commands", host_read_commands);
obj_add_uint128(r, "host_write_commands", host_write_commands);
obj_add_uint128(r, "controller_busy_time", controller_busy_time);
obj_add_uint128(r, "power_cycles", power_cycles);
obj_add_uint128(r, "power_on_hours", power_on_hours);
obj_add_uint128(r, "unsafe_shutdowns", unsafe_shutdowns);
obj_add_uint128(r, "media_errors", media_errors);
obj_add_uint128(r, "num_err_log_entries", num_err_log_entries);
obj_add_uint(r, "warning_temp_time", le32_to_cpu(smart->warning_temp_time));
obj_add_uint(r, "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);
obj_add_int(r, key, temp);
}
obj_add_uint(r, "thm_temp1_trans_count", le32_to_cpu(smart->thm_temp1_trans_count));
obj_add_uint(r, "thm_temp2_trans_count", le32_to_cpu(smart->thm_temp2_trans_count));
obj_add_uint(r, "thm_temp1_total_time", le32_to_cpu(smart->thm_temp1_total_time));
obj_add_uint(r, "thm_temp2_total_time", le32_to_cpu(smart->thm_temp2_total_time));
json_print(r);
}
static void json_ana_log(struct nvme_ana_log *ana_log, const char *devname,
size_t len)
{
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 = json_create_array();
struct json_object *ns_list;
struct json_object *desc;
struct json_object *nsid;
struct json_object *r = json_create_object();
size_t nsid_buf_size;
void *base = ana_log;
__u32 nr_nsids;
int i, j;
obj_add_str(r, "Asymmetric Namespace Access Log for NVMe device", devname);
obj_add_uint64(r, "chgcnt", le64_to_cpu(hdr->chgcnt));
obj_add_uint(r, "ngrps", le16_to_cpu(hdr->ngrps));
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);
obj_add_uint(desc, "grpid", le32_to_cpu(ana_desc->grpid));
obj_add_uint(desc, "nnsids", le32_to_cpu(ana_desc->nnsids));
obj_add_uint64(desc, "chgcnt", le64_to_cpu(ana_desc->chgcnt));
obj_add_str(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();
obj_add_uint(nsid, "nsid", le32_to_cpu(ana_desc->nsids[j]));
array_add_obj(ns_list, nsid);
}
obj_add_array(desc, "NSIDS", ns_list);
offset += nsid_buf_size;
array_add_obj(desc_list, desc);
}
obj_add_array(r, "ANA DESC LIST ", desc_list);
json_print(r);
}
static void json_select_result(enum nvme_features_id fid, __u32 result)
{
struct json_object *r = json_r ? json_r : json_create_object();
char json_str[STR_LEN];
struct json_object *feature = json_create_array();
if (result & 0x1)
array_add_str(feature, "saveable");
if (result & 0x2)
array_add_str(feature, "per-namespace");
if (result & 0x4)
array_add_str(feature, "changeable");
sprintf(json_str, "Feature: %#0*x: select", fid ? 4 : 2, fid);
obj_add_array(r, json_str, feature);
obj_print(r);
}
static void json_self_test_log(struct nvme_self_test_log *self_test, __u8 dst_entries,
__u32 size, const char *devname)
{
struct json_object *valid_attrs;
struct json_object *r = json_create_object();
struct json_object *valid = json_create_array();
int i;
__u32 num_entries = min(dst_entries, NVME_LOG_ST_MAX_RESULTS);
obj_add_int(r, "Current Device Self-Test Operation", self_test->current_operation);
obj_add_int(r, "Current Device Self-Test Completion", self_test->completion);
for (i = 0; i < num_entries; i++) {
valid_attrs = json_create_object();
obj_add_int(valid_attrs, "Self test result", self_test->result[i].dsts & 0xf);
if ((self_test->result[i].dsts & 0xf) == 0xf)
goto add;
obj_add_int(valid_attrs, "Self test code",
self_test->result[i].dsts >> 4);
obj_add_int(valid_attrs, "Segment number",
self_test->result[i].seg);
obj_add_int(valid_attrs, "Valid Diagnostic Information",
self_test->result[i].vdi);
obj_add_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)
obj_add_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)
obj_add_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)
obj_add_int(valid_attrs, "Status Code Type", self_test->result[i].sct);
if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_SC)
obj_add_int(valid_attrs, "Status Code", self_test->result[i].sc);
obj_add_int(valid_attrs, "Vendor Specific",
self_test->result[i].vs[1] << 8 | self_test->result[i].vs[0]);
add:
array_add_obj(valid, valid_attrs);
}
obj_add_array(r, "List of Valid Reports", valid);
json_print(r);
}
static void json_registers_cap(struct nvme_bar_cap *cap, struct json_object *r)
{
char json_str[STR_LEN];
struct json_object *cssa = json_create_array();
struct json_object *csso = json_create_object();
struct json_object *amsa = json_create_array();
struct json_object *amso = json_create_object();
sprintf(json_str, "%"PRIx64"", *(uint64_t *)cap);
obj_add_str(r, "cap", json_str);
obj_add_str(r, "Controller Ready With Media Support (CRWMS)",
cap->crwms ? "Supported" : "Not supported");
obj_add_str(r, "Controller Ready Independent of Media Support (CRIMS)",
cap->crims ? "Supported" : "Not supported");
obj_add_str(r, "NVM Subsystem Shutdown Supported (NSSS)",
cap->nsss ? "Supported" : "Not supported");
obj_add_str(r, "Controller Memory Buffer Supported (CMBS):",
cap->cmbs ? "Supported" : "Not supported");
obj_add_str(r, "Persistent Memory Region Supported (PMRS)",
cap->pmrs ? "Supported" : "Not supported");
sprintf(json_str, "%u bytes", 1 << (12 + cap->mpsmax));
obj_add_str(r, "Memory Page Size Maximum (MPSMAX)", json_str);
sprintf(json_str, "%u bytes", 1 << (12 + cap->mpsmin));
obj_add_str(r, "Memory Page Size Minimum (MPSMIN)", json_str);
obj_add_str(r, "Controller Power Scope (CPS)", !cap->cps ? "Not Reported" : cap->cps == 1 ?
"Controller scope" : cap->cps == 2 ? "Domain scope" : "NVM subsystem scope");
obj_add_str(r, "Boot Partition Support (BPS)", cap->bps ? "Yes" : "No");
obj_add_array(r, "Command Sets Supported (CSS)", cssa);
obj_add_str(csso, "NVM command set", cap->css & 1 ? "Supported" : "Not supported");
obj_add_str(csso, "One or more I/O Command Sets",
cap->css & 0x40 ? "Supported" : "Not supported");
obj_add_str(csso, cap->css & 0x80 ? "Only Admin Command Set" : "I/O Command Set",
"Supported");
array_add_obj(cssa, csso);
obj_add_str(r, "NVM Subsystem Reset Supported (NSSRS)", cap->nssrs ? "Yes" : "No");
sprintf(json_str, "%u bytes", 1 << (2 + cap->dstrd));
obj_add_str(r, "Doorbell Stride (DSTRD)", json_str);
sprintf(json_str, "%u ms", MS500_TO_MS(cap->to));
obj_add_str(r, "Timeout (TO)", json_str);
obj_add_array(r, "Arbitration Mechanism Supported (AMS)", amsa);
obj_add_str(amso, "Weighted Round Robin with Urgent Priority Class",
cap->ams & 2 ? "Supported" : "Not supported");
array_add_obj(amsa, amso);
obj_add_str(r, "Contiguous Queues Required (CQR)", cap->cqr ? "Yes" : "No");
obj_add_uint(r, "Maximum Queue Entries Supported (MQES)", cap->mqes + 1);
}
static void json_registers_version(__u32 vs, struct json_object *r)
{
char json_str[STR_LEN];
sprintf(json_str, "%x", vs);
obj_add_str(r, "Version", json_str);
sprintf(json_str, "%d.%d.%d", NVME_MAJOR(vs), NVME_MINOR(vs), NVME_TERTIARY(vs));
obj_add_str(r, "NVMe specification", json_str);
}
static void json_registers_intms(__u32 intms, struct json_object *r)
{
obj_add_uint_x(r, "intms", intms);
obj_add_uint_x(r, "Interrupt Vector Mask Set (IVMS)", intms);
}
static void json_registers_intmc(__u32 intmc, struct json_object *r)
{
obj_add_uint_x(r, "intmc", intmc);
obj_add_uint_x(r, "Interrupt Vector Mask Set (IVMC)", intmc);
}
static void json_registers_cc_ams(__u8 ams, struct json_object *r)
{
char json_str[STR_LEN];
switch (ams) {
case NVME_CC_AMS_RR:
sprintf(json_str, "Round Robin");
break;
case NVME_CC_AMS_WRRU:
sprintf(json_str, "Weighted Round Robin with Urgent Priority Class");
break;
case NVME_CC_AMS_VS:
sprintf(json_str, "Vendor Specific");
break;
default:
sprintf(json_str, "%s", "Reserved");
break;
}
obj_add_str(r, "Arbitration Mechanism Selected (AMS)", json_str);
}
static void json_registers_cc_shn(__u8 shn, struct json_object *r)
{
char json_str[STR_LEN];
switch (shn) {
case NVME_CC_SHN_NONE:
sprintf(json_str, "No notification; no effect");
break;
case NVME_CC_SHN_NORMAL:
sprintf(json_str, "Normal shutdown notification");
break;
case NVME_CC_SHN_ABRUPT:
sprintf(json_str, "Abrupt shutdown notification");
break;
default:
sprintf(json_str, "%s", "Reserved");
break;
}
obj_add_str(r, "Shutdown Notification (SHN)", json_str);
}
static void json_registers_cc(__u32 cc, struct json_object *r)
{
char json_str[STR_LEN];
sprintf(json_str, "%x", cc);
obj_add_str(r, "cc", json_str);
obj_add_str(r, "Controller Ready Independent of Media Enable (CRIME)",
NVME_CC_CRIME(cc) ? "Enabled" : "Disabled");
sprintf(json_str, "%u bytes", POWER_OF_TWO(NVME_CC_IOCQES(cc)));
obj_add_str(r, "I/O Completion Queue Entry Size (IOCQES): ", json_str);
sprintf(json_str, "%u bytes", POWER_OF_TWO(NVME_CC_IOSQES(cc)));
obj_add_str(r, "I/O Submission Queue Entry Size (IOSQES)", json_str);
json_registers_cc_shn(NVME_CC_SHN(cc), r);
json_registers_cc_ams(NVME_CC_AMS(cc), r);
sprintf(json_str, "%u bytes", POWER_OF_TWO(12 + NVME_CC_MPS(cc)));
obj_add_str(r, "Memory Page Size (MPS)", json_str);
obj_add_str(r, "I/O Command Set Selected (CSS)",
NVME_CC_CSS(cc) == NVME_CC_CSS_NVM ? "NVM Command Set" :
NVME_CC_CSS(cc) == NVME_CC_CSS_CSI ? "All supported I/O Command Sets" :
NVME_CC_CSS(cc) == NVME_CC_CSS_ADMIN ? "Admin Command Set only" : "Reserved");
obj_add_str(r, "Enable (EN)", NVME_CC_EN(cc) ? "Yes" : "No");
}
static void json_registers_csts_shst(__u8 shst, struct json_object *r)