forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme-print.c
7542 lines (6716 loc) · 250 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
// SPDX-License-Identifier: GPL-2.0-or-later
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <uuid/uuid.h>
#include "nvme.h"
#include "libnvme.h"
#include "nvme-print.h"
#include "nvme-models.h"
#include "util/suffix.h"
#include "common.h"
#define ABSOLUTE_ZERO_CELSIUS -273
static inline long kelvin_to_celsius(long t)
{
return t + ABSOLUTE_ZERO_CELSIUS;
}
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] = '-'};
struct nvme_bar_cap {
__u16 mqes;
__u8 ams_cqr;
__u8 to;
__u16 bps_css_nssrs_dstrd;
__u8 mpsmax_mpsmin;
__u8 rsvd_crms_nsss_cmbs_pmrs;
};
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_STATE_OPTIMIZED:
return "optimized";
case NVME_ANA_STATE_NONOPTIMIZED:
return "non-optimized";
case NVME_ANA_STATE_INACCESSIBLE:
return "inaccessible";
case NVME_ANA_STATE_PERSISTENT_LOSS:
return "persistent-loss";
case NVME_ANA_STATE_CHANGE:
return "change";
}
return "invalid state";
}
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_fw_commit: return "Firmware Commit";
case nvme_admin_fw_download: 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 "NVMe-MI Send";
case nvme_admin_nvme_mi_recv: return "NVMe-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";
case nvme_zns_cmd_mgmt_send: return "Zone Management Send";
case nvme_zns_cmd_mgmt_recv: return "Zone Management Receive";
case nvme_zns_cmd_append: return "Zone Append";
}
}
return "Unknown";
}
static const char *fw_to_string(char *c)
{
static char ret[9];
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_SSTAT_STATUS_MASK) {
case NVME_SANITIZE_SSTAT_STATUS_NEVER_SANITIZED:
return "NVM Subsystem has never been sanitized.";
case NVME_SANITIZE_SSTAT_STATUS_COMPLETE_SUCCESS:
return "Most Recent Sanitize Command Completed Successfully.";
case NVME_SANITIZE_SSTAT_STATUS_IN_PROGESS:
return "Sanitize in Progress.";
case NVME_SANITIZE_SSTAT_STATUS_COMPLETED_FAILED:
return "Most Recent Sanitize Command Failed.";
case NVME_SANITIZE_SSTAT_STATUS_ND_COMPLETE_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, 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;
long double nvmcap = int128_to_double(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_double(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, "nulbaf", ns->nulbaf);
if (!cap_only) {
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_object *psds;
long double tnvmcap = int128_to_double(ctrl->tnvmcap);
long double unvmcap = int128_to_double(ctrl->unvmcap);
long double megcap = int128_to_double(ctrl->megcap);
long double maxdna = int128_to_double(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_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, "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_double(root, "tnvmcap", tnvmcap);
json_object_add_value_double(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, "domainid", le16_to_cpu(ctrl->domainid));
json_object_add_value_double(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_int(root, "sgls", le32_to_cpu(ctrl->sgls));
json_object_add_value_double(root, "maxdna", maxdna);
json_object_add_value_int(root, "maxcna", le32_to_cpu(ctrl->maxcna));
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, "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);
}
static 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);
}
static 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_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 (!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);
}
static 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),
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_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);
}
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_double(root, "endurance_estimate",
endurance_estimate);
json_object_add_value_double(root, "data_units_read", data_units_read);
json_object_add_value_double(root, "data_units_written",
data_units_written);
json_object_add_value_double(root, "mediate_write_commands",
media_units_written);
json_object_add_value_double(root, "host_read_cmds", host_read_cmds);
json_object_add_value_double(root, "host_write_cmds", host_write_cmds);
json_object_add_value_double(root, "media_data_integrity_err",
media_data_integrity_err);
json_object_add_value_double(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_double(root, "data_units_read", data_units_read);
json_object_add_value_double(root, "data_units_written",
data_units_written);
json_object_add_value_double(root, "host_read_commands",
host_read_commands);
json_object_add_value_double(root, "host_write_commands",
host_write_commands);
json_object_add_value_double(root, "controller_busy_time",
controller_busy_time);
json_object_add_value_double(root, "power_cycles", power_cycles);
json_object_add_value_double(root, "power_on_hours", power_on_hours);
json_object_add_value_double(root, "unsafe_shutdowns", unsafe_shutdowns);
json_object_add_value_double(root, "media_errors", media_errors);
json_object_add_value_double(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_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);
}
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_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_int(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);
}
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->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);
}
static 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 nvme_show_predictable_latency_per_nvmset(
struct nvme_nvmset_predictable_lat_log *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_rt));
printf("DTWIN Writes Typical: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_wt));
printf("DTWIN Time Maximum: %"PRIu64"\n",
le64_to_cpu(plpns_log->dtwin_tmax));