summaryrefslogtreecommitdiffstats
path: root/plugins/memblaze/memblaze-nvme.c
blob: a6a8ddf4afd8ce01a44f4c0650096446322a1f10 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
// SPDX-License-Identifier: GPL-2.0-or-later
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#include "nvme.h"
#include "libnvme.h"
#include "plugin.h"
#include "linux/types.h"
#include "nvme-print.h"

#define CREATE_CMD
#include "memblaze-nvme.h"
#include "memblaze-utils.h"

enum {
    // feature id
    MB_FEAT_POWER_MGMT = 0x02,
    MB_FEAT_HIGH_LATENCY = 0xE1,
    // log id
    GLP_ID_VU_GET_READ_LATENCY_HISTOGRAM = 0xC1,
    GLP_ID_VU_GET_WRITE_LATENCY_HISTOGRAM = 0xC2,
    GLP_ID_VU_GET_HIGH_LATENCY_LOG = 0xC3,
    MB_FEAT_CLEAR_ERRORLOG = 0xF7,
};

#define LOG_PAGE_SIZE                                      (0x1000)
#define DO_PRINT_FLAG                                      (1)
#define NOT_PRINT_FLAG                                     (0)
#define FID_C1_LOG_FILENAME                                "log_c1.csv"
#define FID_C2_LOG_FILENAME                                "log_c2.csv"
#define FID_C3_LOG_FILENAME                                "log_c3.csv"

/*
 * Return -1 if @fw1 < @fw2
 * Return 0 if @fw1 == @fw2
 * Return 1 if @fw1 > @fw2
 */
static int compare_fw_version(const char *fw1, const char *fw2)
{
    while (*fw1 != '\0') {
        if (*fw2 == '\0' || *fw1 > *fw2)
            return 1;
        if (*fw1 < *fw2)
            return -1;
        fw1++;
        fw2++;
    }

    if (*fw2 != '\0')
        return -1;

    return 0;
}

/**********************************************************
 * input: firmware version string
 * output:
 *     1: new intel format
 *     0: old memblaze format
 * *******************************************************/
#define MEMBLAZE_FORMAT         (0)
#define INTEL_FORMAT            (1)

// 2.13 = papaya
#define IS_PAPAYA(str)          (!strcmp(str, "2.13"))
// 2.83 = raisin
#define IS_RAISIN(str)          (!strcmp(str, "2.83"))
// 2.94 = kumquat
#define IS_KUMQUAT(str)         (!strcmp(str, "2.94"))
// 0.60 = loquat
#define IS_LOQUAT(str)          (!strcmp(str, "0.60"))

#define STR_VER_SIZE            (5)

int getlogpage_format_type(char *model_name)
{
    int logpage_format_type = INTEL_FORMAT;
    const char *boundary_model_name1 = "P"; // MEMBLAZE P7936DT0640M00
    const char *boundary_model_name2 = "P5920"; // Use INTEL_FORMAT from Raisin P5920.
    if (0 == strncmp(model_name, boundary_model_name1, strlen(boundary_model_name1)))
    {
        if (strncmp(model_name, boundary_model_name2, strlen(boundary_model_name2)) < 0)
        {
            logpage_format_type = MEMBLAZE_FORMAT;
        }
    }
    return logpage_format_type;
}

static __u32 item_id_2_u32(struct nvme_memblaze_smart_log_item *item)
{
    __le32  __id = 0;
    memcpy(&__id, item->id, 3);
    return le32_to_cpu(__id);
}

static __u64 raw_2_u64(const __u8 *buf, size_t len)
{
    __le64  val = 0;
    memcpy(&val, buf, len);
    return le64_to_cpu(val);
}

#define STRN2_01    "Additional Smart Log for NVME device"
#define STRN2_02    "namespace-id"
#define STRN1_01    "key"
#define STRN1_02    "normalized"
#define STRN1_03    "raw"
#define STR00_01    "program_fail_count"
#define STR01_01    "erase_fail_count"
#define STR02_01    "wear_leveling"
#define STR02_03    "min: "
#define STR02_04    ", max: "
#define STR02_05    ", avg: "
#define STR03_01    "end_to_end_error_detection_count"
#define STR04_01    "crc_error_count"
#define STR05_01    "timed_workload_media_wear"
#define STR06_01    "timed_workload_host_reads"
#define STR07_01    "timed_workload_timer"
#define STR07_02    " min"
#define STR08_01    "thermal_throttle_status"
#define STR08_02    ", cnt: "
#define STR09_01    "retry_buffer_overflow_count"
#define STR10_01    "pll_lock_loss_count"
#define STR11_01    "nand_bytes_written"
#define STR11_03    "sectors: "
#define STR12_01    "host_bytes_written"
#define STR12_03    "sectors: "
#define STR13_01    "system_area_life_left"
#define STR14_01    "total_read"
#define STR15_01    "tempt_since_born"
#define STR15_03    "max: "
#define STR15_04    ", min: "
#define STR15_05    ", curr: "
#define STR16_01    "power_consumption"
#define STR16_03    "max: "
#define STR16_04    ", min: "
#define STR16_05    ", curr: "
#define STR17_01    "tempt_since_bootup"
#define STR17_03    "max: "
#define STR17_04    ", min: "
#define STR17_05    ", curr: "
#define STR18_01    "power_loss_protection"
#define STR19_01    "read_fail_count"
#define STR20_01    "thermal_throttle_time"
#define STR21_01    "flash_media_error"

static void get_memblaze_new_smart_info(struct nvme_p4_smart_log *smart, int index, u8 *nm_val, u8 *raw_val)
{
    memcpy(nm_val, smart->itemArr[index].nmVal, NM_SIZE);
    memcpy(raw_val, smart->itemArr[index].rawVal, RAW_SIZE);
}

static void show_memblaze_smart_log_new(struct nvme_memblaze_smart_log *s,
    unsigned int nsid, const char *devname)
{
    struct nvme_p4_smart_log *smart = (struct nvme_p4_smart_log *)s;
    u8 *nm = malloc(NM_SIZE * sizeof(u8));
    u8 *raw = malloc(RAW_SIZE * sizeof(u8));

    if (!nm) {
        if (raw)
            free(raw);
        return;
    }
    if (!raw) {
        free(nm);
        return;
    }
    /* Table Title */
    printf("%s:%s %s:%x\n", STRN2_01, devname, STRN2_02, nsid);
    /* Clumn Name*/
    printf("%-34s%-11s%s\n", STRN1_01, STRN1_02, STRN1_03);
    /* 00 RAISIN_SI_VD_PROGRAM_FAIL */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_PROGRAM_FAIL, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR00_01, *nm, int48_to_long(raw));
    /* 01 RAISIN_SI_VD_ERASE_FAIL */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_ERASE_FAIL, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR01_01, *nm, int48_to_long(raw));
    /* 02 RAISIN_SI_VD_WEARLEVELING_COUNT */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_WEARLEVELING_COUNT, nm, raw);
    printf("%-31s : %3d%%       %s%u%s%u%s%u\n", STR02_01, *nm,
        STR02_03, *raw, STR02_04, *(raw+2), STR02_05, *(raw+4));
    /* 03 RAISIN_SI_VD_E2E_DECTECTION_COUNT */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_E2E_DECTECTION_COUNT, nm, raw);
    printf("%-31s: %3d%%       %"PRIu64"\n", STR03_01, *nm, int48_to_long(raw));
    /* 04 RAISIN_SI_VD_PCIE_CRC_ERR_COUNT */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_PCIE_CRC_ERR_COUNT, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR04_01, *nm, int48_to_long(raw));
    /* 05 RAISIN_SI_VD_TIMED_WORKLOAD_MEDIA_WEAR */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TIMED_WORKLOAD_MEDIA_WEAR, nm, raw);
    printf("%-32s: %3d%%       %.3f%%\n", STR05_01, *nm, ((float)int48_to_long(raw))/1000);
    /* 06 RAISIN_SI_VD_TIMED_WORKLOAD_HOST_READ */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TIMED_WORKLOAD_HOST_READ, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"%%\n", STR06_01, *nm, int48_to_long(raw));
    /* 07 RAISIN_SI_VD_TIMED_WORKLOAD_TIMER */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TIMED_WORKLOAD_TIMER, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"%s\n", STR07_01, *nm, int48_to_long(raw), STR07_02);
    /* 08 RAISIN_SI_VD_THERMAL_THROTTLE_STATUS */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_THERMAL_THROTTLE_STATUS, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"%%%s%"PRIu64"\n", STR08_01, *nm,
        int48_to_long(raw), STR08_02, int48_to_long(raw+1));
    /* 09 RAISIN_SI_VD_RETRY_BUFF_OVERFLOW_COUNT */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_RETRY_BUFF_OVERFLOW_COUNT, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR09_01, *nm, int48_to_long(raw));
    /* 10 RAISIN_SI_VD_PLL_LOCK_LOSS_COUNT */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_PLL_LOCK_LOSS_COUNT, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR10_01, *nm, int48_to_long(raw));
    /* 11 RAISIN_SI_VD_TOTAL_WRITE */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TOTAL_WRITE, nm, raw);
    printf("%-32s: %3d%%       %s%"PRIu64"\n", STR11_01, *nm, STR11_03, int48_to_long(raw));
    /* 12 RAISIN_SI_VD_HOST_WRITE */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_HOST_WRITE, nm, raw);
    printf("%-32s: %3d%%       %s%"PRIu64"\n", STR12_01, *nm, STR12_03, int48_to_long(raw));
    /* 13 RAISIN_SI_VD_SYSTEM_AREA_LIFE_LEFT */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_SYSTEM_AREA_LIFE_LEFT, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR13_01, *nm, int48_to_long(raw));
    /* 14 RAISIN_SI_VD_TOTAL_READ */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TOTAL_READ, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR14_01, *nm, int48_to_long(raw));
    /* 15 RAISIN_SI_VD_TEMPT_SINCE_BORN */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TEMPT_SINCE_BORN, nm, raw);
    printf("%-32s: %3d%%       %s%u%s%u%s%u\n", STR15_01,  *nm,
        STR15_03, *raw, STR15_04, *(raw+2), STR15_05, *(raw+4));
    /* 16 RAISIN_SI_VD_POWER_CONSUMPTION */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_POWER_CONSUMPTION, nm, raw);
    printf("%-32s: %3d%%       %s%u%s%u%s%u\n", STR16_01,  *nm,
        STR16_03, *raw, STR16_04, *(raw+2), STR16_05, *(raw+4));
    /* 17 RAISIN_SI_VD_TEMPT_SINCE_BOOTUP */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_TEMPT_SINCE_BOOTUP, nm, raw);
    printf("%-32s: %3d%%       %s%u%s%u%s%u\n", STR17_01,  *nm, STR17_03, *raw,
        STR17_04, *(raw+2), STR17_05, *(raw+4));
    /* 18 RAISIN_SI_VD_POWER_LOSS_PROTECTION */
    /* 19 RAISIN_SI_VD_READ_FAIL */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_READ_FAIL, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR19_01, *nm, int48_to_long(raw));
    /* 20 RAISIN_SI_VD_THERMAL_THROTTLE_TIME */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_THERMAL_THROTTLE_TIME, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR20_01, *nm, int48_to_long(raw));
    /* 21 RAISIN_SI_VD_FLASH_MEDIA_ERROR */
    get_memblaze_new_smart_info(smart, RAISIN_SI_VD_FLASH_MEDIA_ERROR, nm, raw);
    printf("%-32s: %3d%%       %"PRIu64"\n", STR21_01, *nm, int48_to_long(raw));

    free(nm);
    free(raw);
}

static void show_memblaze_smart_log_old(struct nvme_memblaze_smart_log *smart,
    unsigned int nsid, const char *devname, const char *fw_ver)
{
    char fw_ver_local[STR_VER_SIZE + 1];
    struct nvme_memblaze_smart_log_item *item;

    strncpy(fw_ver_local, fw_ver, STR_VER_SIZE);
    *(fw_ver_local + STR_VER_SIZE) = '\0';

    printf("Additional Smart Log for NVME device:%s namespace-id:%x\n", devname, nsid);

    printf("Total write in GB since last factory reset			: %"PRIu64"\n",
        int48_to_long(smart->items[TOTAL_WRITE].rawval));
    printf("Total read in GB since last factory reset			: %"PRIu64"\n",
        int48_to_long(smart->items[TOTAL_READ].rawval));

    printf("Thermal throttling status[1:HTP in progress]			: %u\n",
        smart->items[THERMAL_THROTTLE].thermal_throttle.on);
    printf("Total thermal throttling minutes since power on			: %u\n",
        smart->items[THERMAL_THROTTLE].thermal_throttle.count);

    printf("Maximum temperature in Kelvin since last factory reset		: %u\n",
        le16_to_cpu(smart->items[TEMPT_SINCE_RESET].temperature.max));
    printf("Minimum temperature in Kelvin since last factory reset		: %u\n",
        le16_to_cpu(smart->items[TEMPT_SINCE_RESET].temperature.min));
    if (compare_fw_version(fw_ver, "0.09.0300") != 0) {
        printf("Maximum temperature in Kelvin since power on			: %u\n",
            le16_to_cpu(smart->items[TEMPT_SINCE_BOOTUP].temperature_p.max));
        printf("Minimum temperature in Kelvin since power on			: %u\n",
            le16_to_cpu(smart->items[TEMPT_SINCE_BOOTUP].temperature_p.min));
    }
    printf("Current temperature in Kelvin					: %u\n",
        le16_to_cpu(smart->items[TEMPT_SINCE_RESET].temperature.curr));

    printf("Maximum power in watt since power on				: %u\n",
        le16_to_cpu(smart->items[POWER_CONSUMPTION].power.max));
    printf("Minimum power in watt since power on				: %u\n",
        le16_to_cpu(smart->items[POWER_CONSUMPTION].power.min));
    printf("Current power in watt						: %u\n",
        le16_to_cpu(smart->items[POWER_CONSUMPTION].power.curr));

    item = &smart->items[POWER_LOSS_PROTECTION];
    if (item_id_2_u32(item) == 0xEC)
        printf("Power loss protection normalized value				: %u\n",
            item->power_loss_protection.curr);

    item = &smart->items[WEARLEVELING_COUNT];
    if (item_id_2_u32(item) == 0xAD) {
        printf("Percentage of wearleveling count left				: %u\n",
            le16_to_cpu(item->nmval));
        printf("Wearleveling count min erase cycle				: %u\n",
            le16_to_cpu(item->wearleveling_count.min));
        printf("Wearleveling count max erase cycle				: %u\n",
            le16_to_cpu(item->wearleveling_count.max));
        printf("Wearleveling count avg erase cycle				: %u\n",
            le16_to_cpu(item->wearleveling_count.avg));
    }

    item = &smart->items[HOST_WRITE];
    if (item_id_2_u32(item) == 0xF5)
        printf("Total host write in GiB since device born 			: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

    item = &smart->items[THERMAL_THROTTLE_CNT];
    if (item_id_2_u32(item) == 0xEB)
        printf("Thermal throttling count since device born 			: %u\n",
            item->thermal_throttle_cnt.cnt);

    item = &smart->items[CORRECT_PCIE_PORT0];
    if (item_id_2_u32(item) == 0xED)
        printf("PCIE Correctable Error Count of Port0    			: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

    item = &smart->items[CORRECT_PCIE_PORT1];
    if (item_id_2_u32(item) == 0xEE)
        printf("PCIE Correctable Error Count of Port1 	        		: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

    item = &smart->items[REBUILD_FAIL];
    if (item_id_2_u32(item) == 0xEF)
        printf("End-to-End Error Detection Count 	        		: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

    item = &smart->items[ERASE_FAIL];
    if (item_id_2_u32(item) == 0xF0)
        printf("Erase Fail Count 		                        	: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

    item = &smart->items[PROGRAM_FAIL];
    if (item_id_2_u32(item) == 0xF1)
        printf("Program Fail Count 		                        	: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

    item = &smart->items[READ_FAIL];
    if (item_id_2_u32(item) == 0xF2)
        printf("Read Fail Count	                                 		: %llu\n",
            (unsigned long long)raw_2_u64(item->rawval, sizeof(item->rawval)));

     if ( IS_PAPAYA(fw_ver_local) ) {
        struct nvme_p4_smart_log *s = (struct nvme_p4_smart_log *)smart;
        u8 *nm = malloc(NM_SIZE * sizeof(u8));
        u8 *raw = malloc(RAW_SIZE * sizeof(u8));

	if (!nm) {
            if (raw)
                free(raw);
            return;
	}
	if (!raw) {
            free(nm);
            return;
	}
        /* 00 RAISIN_SI_VD_PROGRAM_FAIL */
        get_memblaze_new_smart_info(s, PROGRAM_FAIL, nm, raw);
        printf("%-32s                                : %3d%%       %"PRIu64"\n",
			STR00_01, *nm, int48_to_long(raw));
        /* 01 RAISIN_SI_VD_ERASE_FAIL */
        get_memblaze_new_smart_info(s, ERASE_FAIL, nm, raw);
        printf("%-32s                                : %3d%%       %"PRIu64"\n",
			STR01_01, *nm, int48_to_long(raw));
        /* 02 RAISIN_SI_VD_WEARLEVELING_COUNT */
        get_memblaze_new_smart_info(s, WEARLEVELING_COUNT, nm, raw);
        printf("%-31s                                 : %3d%%       %s%u%s%u%s%u\n",
			STR02_01, *nm, STR02_03, *raw, STR02_04, *(raw+2), STR02_05, *(raw+4));
        /* 11 RAISIN_SI_VD_TOTAL_WRITE */
        get_memblaze_new_smart_info(s, TOTAL_WRITE, nm, raw);
        printf("%-32s                                : %3d%%       %"PRIu64"\n",
			STR11_01, *nm, 32*int48_to_long(raw));
        /* 12 RAISIN_SI_VD_HOST_WRITE */
        get_memblaze_new_smart_info(s, HOST_WRITE, nm, raw);
        printf("%-32s                                : %3d%%       %"PRIu64"\n",
			STR12_01, *nm, 32*int48_to_long(raw));

        free(nm);
        free(raw);
    }
}

static int show_memblaze_smart_log(int fd, __u32 nsid, const char *devname,
    struct nvme_memblaze_smart_log *smart)
{
    struct nvme_id_ctrl ctrl;
    char fw_ver[10];
    int err = 0;

    err = nvme_identify_ctrl(fd, &ctrl);
    if (err)
        return err;

    snprintf(fw_ver, sizeof(fw_ver), "%c.%c%c.%c%c%c%c",
        ctrl.fr[0], ctrl.fr[1], ctrl.fr[2], ctrl.fr[3],
        ctrl.fr[4], ctrl.fr[5], ctrl.fr[6]);

    if (getlogpage_format_type(ctrl.mn)) // Intel Format & new format
    {
        show_memblaze_smart_log_new(smart, nsid, devname);
    }
    else  // Memblaze Format & old format
    {
        show_memblaze_smart_log_old(smart, nsid, devname, fw_ver);
    }
	return err;
}

int parse_params(char *str, int number, ...)
{
    va_list argp;
    int *param;
    char *c;
    int value;

    va_start(argp, number);

    while (number > 0) {
        c = strtok(str, ",");
        if ( c == NULL) {
            printf("No enough parameters. abort...\n");
            va_end(argp);
            return 1;
        }

        if (isalnum((int)*c) == 0) {
            printf("%s is not a valid number\n", c);
            va_end(argp);
            return 1;
        }
        value = atoi(c);
        param = va_arg(argp, int *);
        *param = value;

        if (str) {
            str = strchr(str, ',');
            if (str) { str++; }
        }
        number--;
    }
    va_end(argp);

    return 0;
}

static int mb_get_additional_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
	struct nvme_memblaze_smart_log smart_log;
	int err, fd;
	char *desc = "Get Memblaze vendor specific additional smart log (optionally, "\
		      "for the specified namespace), and show it.";
	const char *namespace = "(optional) desired namespace";
	const char *raw = "dump output in binary format";
	struct config {
		__u32 namespace_id;
		bool  raw_binary;
	};

	struct config cfg = {
		.namespace_id = NVME_NSID_ALL,
	};

	OPT_ARGS(opts) = {
		OPT_UINT("namespace-id", 'n', &cfg.namespace_id,  namespace),
		OPT_FLAG("raw-binary",   'b', &cfg.raw_binary,    raw),
		OPT_END()
	};

	fd = parse_and_open(argc, argv, desc, opts);
	if (fd < 0)
		return fd;

	err = nvme_get_nsid_log(fd, false, 0xca, cfg.namespace_id,
		sizeof(smart_log), &smart_log);
	if (!err) {
		if (!cfg.raw_binary)
			err = show_memblaze_smart_log(fd, cfg.namespace_id, devicename, &smart_log);
		else
			d_raw((unsigned char *)&smart_log, sizeof(smart_log));
	}
	if (err > 0)
		nvme_show_status(err);

	close(fd);
	return err;
}

static char *mb_feature_to_string(int feature)
{
    switch (feature) {
    case MB_FEAT_POWER_MGMT: return "Memblaze power management";
    case MB_FEAT_HIGH_LATENCY: return "Memblaze high latency log";
    case MB_FEAT_CLEAR_ERRORLOG: return "Memblaze clear error log";
    default: return "Unknown";
    }
}

static int mb_get_powermanager_status(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
    const char *desc = "Get Memblaze power management ststus\n	(value 0 - 25w, 1 - 20w, 2 - 15w)";
    int err, fd;
    __u32 result;
    __u32 feature_id = MB_FEAT_POWER_MGMT;

    OPT_ARGS(opts) = {
        OPT_END()
    };

    fd = parse_and_open(argc, argv, desc, opts);
    if (fd < 0) return fd;

    struct nvme_get_features_args args = {
	    .args_size		= sizeof(args),
	    .fd			= fd,
	    .fid		= feature_id,
	    .nsid		= 0,
	    .sel		= 0,
	    .cdw11		= 0,
	    .uuidx		= 0,
	    .data_len		= 0,
	    .data		= NULL,
	    .timeout		= NVME_DEFAULT_IOCTL_TIMEOUT,
	    .result		= &result,
    };
    err = nvme_get_features(&args);
    if (err < 0) {
        perror("get-feature");
    }
    if (!err) {
        printf("get-feature:0x%02x (%s), %s value: %#08x\n", feature_id,
            mb_feature_to_string(feature_id),
            nvme_select_to_string(0), result);
    } else if (err > 0)
	    nvme_show_status(err);
    close(fd);
    return err;
}

static int mb_set_powermanager_status(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
    const char *desc = "Set Memblaze power management status\n	(value 0 - 25w, 1 - 20w, 2 - 15w)";
    const char *value = "new value of feature (required)";
    const char *save = "specifies that the controller shall save the attribute";
    int err, fd;
    __u32 result;

    struct config {
        __u32 feature_id;
        __u32 value;
        bool  save;
    };

    struct config cfg = {
        .feature_id   = MB_FEAT_POWER_MGMT,
        .value        = 0,
        .save         = 0,
    };

    OPT_ARGS(opts) = {
        OPT_UINT("value",        'v', &cfg.value,        value),
        OPT_FLAG("save",         's', &cfg.save,         save),
        OPT_END()
    };

    fd = parse_and_open(argc, argv, desc, opts);
    if (fd < 0) return fd;

    struct nvme_set_features_args args = {
	    .args_size		= sizeof(args),
	    .fd			= fd,
	    .fid		= cfg.feature_id,
	    .nsid		= 0,
	    .cdw11		= cfg.value,
	    .cdw12		= 0,
	    .save		= cfg.save,
	    .uuidx		= 0,
	    .cdw15		= 0,
	    .data_len		= 0,
	    .data		= NULL,
	    .timeout		= NVME_DEFAULT_IOCTL_TIMEOUT,
	    .result		= &result,
    };
    err = nvme_set_features(&args);
    if (err < 0) {
        perror("set-feature");
    }
    if (!err) {
        printf("set-feature:%02x (%s), value:%#08x\n", cfg.feature_id,
            mb_feature_to_string(cfg.feature_id), cfg.value);
    } else if (err > 0)
	nvme_show_status(err);

    close(fd);
    return err;
}

#define P2MIN                                              (1)
#define P2MAX                                              (5000)
#define MB_FEAT_HIGH_LATENCY_VALUE_SHIFT                   (15)
static int mb_set_high_latency_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
    const char *desc = "Set Memblaze high latency log\n"\
                       "	input parameter p1,p2\n"\
                       "	p1 value: 0 is disable, 1 is enable\n"\
                       "	p2 value: 1 .. 5000 ms";
    const char *param = "input parameters";
    int err, fd;
    __u32 result;
    int param1 = 0, param2 = 0;

    struct config {
        __u32 feature_id;
        char * param;
        __u32 value;
    };

    struct config cfg = {
        .feature_id   = MB_FEAT_HIGH_LATENCY,
        .param        = "0,0",
        .value        = 0,
    };

    OPT_ARGS(opts) = {
        OPT_LIST("param",        'p', &cfg.param,       param),
        OPT_END()
    };

    fd = parse_and_open(argc, argv, desc, opts);
    if (fd < 0) return fd;

    if (parse_params(cfg.param, 2, &param1, &param2)) {
        printf("setfeature: invalid formats %s\n", cfg.param);
        close(fd);
        return EINVAL;
    }
    if ((param1 == 1) && (param2 < P2MIN || param2 > P2MAX)) {
        printf("setfeature: invalid high io latency threshold %d\n", param2);
        close(fd);
        return EINVAL;
    }
    cfg.value = (param1 << MB_FEAT_HIGH_LATENCY_VALUE_SHIFT) | param2;

    struct nvme_set_features_args args = {
	    .args_size		= sizeof(args),
	    .fd			= fd,
	    .fid		= cfg.feature_id,
	    .nsid		= 0,
	    .cdw11		= cfg.value,
	    .cdw12		= 0,
	    .save		= false,
	    .uuidx		= 0,
	    .cdw15		= 0,
	    .data_len		= 0,
	    .data		= NULL,
	    .timeout		= NVME_DEFAULT_IOCTL_TIMEOUT,
	    .result		= &result,
    };
    err = nvme_set_features(&args);
    if (err < 0) {
        perror("set-feature");
    }
    if (!err) {
        printf("set-feature:0x%02X (%s), value:%#08x\n", cfg.feature_id,
            mb_feature_to_string(cfg.feature_id), cfg.value);
    } else if (err > 0)
	nvme_show_status(err);

    close(fd);
    return err;
}

static int glp_high_latency_show_bar(FILE *fdi, int print)
{
    fPRINT_PARAM1("Memblaze High Latency Log\n");
    fPRINT_PARAM1("---------------------------------------------------------------------------------------------\n");
    fPRINT_PARAM1("Timestamp                        Type    QID    CID    NSID     StartLBA      NumLBA   Latency\n");
    fPRINT_PARAM1("---------------------------------------------------------------------------------------------\n");
    return 0;
}

/* High latency log page definiton
 * Total 32 bytes
 */
typedef struct
{
    __u8 port;
    __u8 revision;
    __u16 rsvd;
    __u8 opcode;
    __u8 sqe;
    __u16 cid;
    __u32 nsid;
    __u32 latency;
    __u64 sLBA;
    __u16 numLBA;
    __u16 timestampH;
    __u32 timestampL;
} log_page_high_latency_t; /* total 32 bytes */

static int find_deadbeef(char *buf)
{
    if (((*(buf + 0) & 0xff) == 0xef) && ((*(buf + 1) & 0xff) == 0xbe) && \
            ((*(buf + 2) & 0xff) == 0xad) && ((*(buf + 3) & 0xff) == 0xde))
    {
        return 1;
    }
    return 0;
}

#define TIME_STR_SIZE                                      (44)
static int glp_high_latency(FILE *fdi, char *buf, int buflen, int print)
{
    log_page_high_latency_t *logEntry;
    char string[TIME_STR_SIZE];
    int i, entrySize;
    __u64 timestamp;
    time_t tt = 0;
    struct tm *t = NULL;
    int millisec = 0;

    if (find_deadbeef(buf)) return 0;

    entrySize = sizeof(log_page_high_latency_t);
    for (i = 0; i < buflen; i += entrySize)
    {
        logEntry = (log_page_high_latency_t *)(buf + i);

        if (logEntry->latency == 0 && logEntry->revision == 0)
        {
            return 1;
        }

        if (0 == logEntry->timestampH)  //  generate host time string
        {
            snprintf(string, sizeof(string), "%d", logEntry->timestampL);
        }
        else  //  sort
        {
            timestamp = logEntry->timestampH - 1;
            timestamp = timestamp << 32;
            timestamp += logEntry->timestampL;
            tt = timestamp / 1000;
            millisec = timestamp % 1000;
            t = gmtime(&tt);
            snprintf(string, sizeof(string), "%4d%02d%02d--%02d:%02d:%02d.%03d UTC",
                     1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, millisec);
        }

	if (fdi) {
		fprintf(fdi, "%-32s %-7x %-6x %-6x %-8x %4x%08x  %-8x %-d\n",
			string, logEntry->opcode, logEntry->sqe,
			logEntry->cid, logEntry->nsid,
			(__u32)(logEntry->sLBA >> 32),
			(__u32)logEntry->sLBA, logEntry->numLBA,
			logEntry->latency);
	}
        if (print)
        {
            printf("%-32s %-7x %-6x %-6x %-8x %4x%08x  %-8x %-d\n",
                   string, logEntry->opcode, logEntry->sqe, logEntry->cid, logEntry->nsid,
                   (__u32)(logEntry->sLBA >> 32), (__u32)logEntry->sLBA, logEntry->numLBA, logEntry->latency);
        }
    }
    return 1;
}

static int mb_high_latency_log_print(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
    const char *desc = "Get Memblaze high latency log";
    int err, fd;
    char buf[LOG_PAGE_SIZE];
    FILE *fdi = NULL;

    OPT_ARGS(opts) = {
        OPT_END()
    };

    fd = parse_and_open(argc, argv, desc, opts);
    if (fd < 0) return fd;

    fdi = fopen(FID_C3_LOG_FILENAME, "w+");

    glp_high_latency_show_bar(fdi, DO_PRINT_FLAG);
    err = nvme_get_log_simple(fd, GLP_ID_VU_GET_HIGH_LATENCY_LOG, sizeof(buf), &buf);

    while (1) {
        if (!glp_high_latency(fdi, buf, LOG_PAGE_SIZE, DO_PRINT_FLAG)) break;
        err = nvme_get_log_simple(fd, GLP_ID_VU_GET_HIGH_LATENCY_LOG, sizeof(buf), &buf);
        if ( err) {
	    nvme_show_status(err);
            break;
        }
    }

    if (NULL != fdi) fclose(fdi);
    close(fd);
    return err;
}


static int memblaze_fw_commit(int fd, int select)
{
	struct nvme_passthru_cmd cmd = {
		.opcode		= nvme_admin_fw_commit,
		.cdw10		= 8,
		.cdw12      = select,
	};

	return nvme_submit_admin_passthru(fd, &cmd, NULL);
}

static int mb_selective_download(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
	const char *desc =
		"This performs a selective firmware download, which allows the user to "
		"select which firmware binary to update for 9200 devices. This requires a power cycle once the "
		"update completes. The options available are: \n\n"
		"OOB - This updates the OOB and main firmware\n"
		"EEP - This updates the eeprom and main firmware\n"
		"ALL - This updates the eeprom, OOB, and main firmware";
	const char *fw = "firmware file (required)";
	const char *select = "FW Select (e.g., --select=OOB, EEP, ALL)";
	int xfer = 4096;
	void *fw_buf;
	int fd, selectNo,fw_fd,fw_size,err,offset = 0;
	struct stat sb;
	int i;

	struct config {
		char  *fw;
		char  *select;
	};

	struct config cfg = {
		.fw     = "",
		.select = "\0",
	};

	OPT_ARGS(opts) = {
		OPT_STRING("fw", 'f', "FILE", &cfg.fw, fw),
		OPT_STRING("select", 's', "flag", &cfg.select, select),
		OPT_END()
	};

	fd = parse_and_open(argc, argv, desc, opts);
	if (fd < 0)
		return fd;

	if (strlen(cfg.select) != 3) {
		fprintf(stderr, "Invalid select flag\n");
		err = EINVAL;
		goto out;
	}

	for (i = 0; i < 3; i++) {
		cfg.select[i] = toupper(cfg.select[i]);
	}

	if (strncmp(cfg.select,"OOB", 3) == 0) {
		selectNo = 18;
	} else if (strncmp(cfg.select,"EEP", 3) == 0) {
		selectNo = 10;
	} else if (strncmp(cfg.select,"ALL", 3) == 0) {
		selectNo = 26;
	} else {
		fprintf(stderr, "Invalid select flag\n");
		err = EINVAL;
		goto out;
	}

	fw_fd = open(cfg.fw, O_RDONLY);
	if (fw_fd < 0) {
		fprintf(stderr, "no firmware file provided\n");
		err = EINVAL;
		goto out;
	}

	err = fstat(fw_fd, &sb);
	if (err < 0) {
		perror("fstat");
		err = errno;
		goto out_close;
	}

	fw_size = sb.st_size;
	if (fw_size & 0x3) {
		fprintf(stderr, "Invalid size:%d for f/w image\n", fw_size);
		err = EINVAL;
		goto out_close;
	}

	if (posix_memalign(&fw_buf, getpagesize(), fw_size)) {
		fprintf(stderr, "No memory for f/w size:%d\n", fw_size);
		err = ENOMEM;
		goto out_close;
	}

	if (read(fw_fd, fw_buf, fw_size) != ((ssize_t)(fw_size))) {
		err = errno;
		goto out_free;
	}

	while (fw_size > 0) {
		xfer = min(xfer, fw_size);

		struct nvme_fw_download_args args = {
			.args_size	= sizeof(args),
			.fd		= fd,
			.offset		= offset,
			.data_len	= xfer,
			.data		= fw_buf,
			.timeout	= NVME_DEFAULT_IOCTL_TIMEOUT,
			.result		= NULL,
		};
		err = nvme_fw_download(&args);
		if (err < 0) {
			perror("fw-download");
			goto out_free;
		} else if (err != 0) {
			nvme_show_status(err);
			goto out_free;
		}
		fw_buf     += xfer;
		fw_size    -= xfer;
		offset += xfer;
	}

	err = memblaze_fw_commit(fd,selectNo);

	if(err == 0x10B || err == 0x20B) {
		err = 0;
		fprintf(stderr, "Update successful! Please power cycle for changes to take effect\n");
	}

out_free:
	free(fw_buf);
out_close:
	close(fw_fd);
out:
	close(fd);
	return err;
}

static void ioLatencyHistogramOutput(FILE *fd, int index, int start, int end, char *unit0,
                                     char *unit1, unsigned int *pHistogram, int print)
{
    int len;
    char string[64], subString0[12], subString1[12];

    snprintf(subString0, sizeof(subString0), "%d%s", start, unit0);
    if (end != 0x7FFFFFFF)
        snprintf(subString1, sizeof(subString1), "%d%s", end, unit1);
    else
        snprintf(subString1, sizeof(subString1), "%s", "+INF");
    len = snprintf(string, sizeof(string), "%-11d %-11s %-11s %-11u\n",
		   index, subString0, subString1,
                   pHistogram[index]);
    fwrite(string, 1, len, fd);
    if (print)
        printf("%s", string);
}

int io_latency_histogram(char *file, char *buf, int print, int logid)
{
    FILE *fdi = fopen(file, "w+");
    int i, index;
    char unit[2][3];
    unsigned int *revision = (unsigned int *)buf;

    if (logid == GLP_ID_VU_GET_READ_LATENCY_HISTOGRAM)
    {
        fPRINT_PARAM1("Memblaze IO Read Command Latency Histogram\n");
    }
    else if (logid == GLP_ID_VU_GET_WRITE_LATENCY_HISTOGRAM)
    {
        fPRINT_PARAM1("Memblaze IO Write Command Latency Histogram\n");
    }
    fPRINT_PARAM2("Major Revision : %d\n", revision[1]);
    fPRINT_PARAM2("Minor Revision : %d\n", revision[0]);
    buf += 8;

    if (revision[1] == 1 && revision[0] == 0)
    {
        fPRINT_PARAM1("--------------------------------------------------\n");
        fPRINT_PARAM1("Bucket      Start       End         Value         \n");
        fPRINT_PARAM1("--------------------------------------------------\n");
        index = 0;
        strcpy(unit[0], "us");
        strcpy(unit[1], "us");
        for (i = 0; i < 32; i++, index++)
        {
            if (i == 31)
            {
                strcpy(unit[1], "ms");
                ioLatencyHistogramOutput(fdi, index, i * 32, 1, unit[0], unit[1], (unsigned int *)buf, print);
            }
            else
            {
                ioLatencyHistogramOutput(fdi, index, i * 32, (i + 1) * 32, unit[0], unit[1], (unsigned int *)buf,
                                         print);
            }
        }

        strcpy(unit[0], "ms");
        strcpy(unit[1], "ms");
        for (i = 1; i < 32; i++, index++)
        {
            ioLatencyHistogramOutput(fdi, index, i, i + 1, unit[0], unit[1], (unsigned int *)buf, print);
        }

        for (i = 1; i < 32; i++, index++)
        {
            if (i == 31)
            {
                strcpy(unit[1], "s");
                ioLatencyHistogramOutput(fdi, index, i * 32, 1, unit[0], unit[1], (unsigned int *)buf, print);
            }
            else
            {
                ioLatencyHistogramOutput(fdi, index, i * 32, (i + 1) * 32, unit[0], unit[1], (unsigned int *)buf,
                                         print);
            }
        }

        strcpy(unit[0], "s");
        strcpy(unit[1], "s");
        for (i = 1; i < 4; i++, index++)
        {
            ioLatencyHistogramOutput(fdi, index, i, i + 1, unit[0], unit[1], (unsigned int *)buf, print);
        }

        ioLatencyHistogramOutput(fdi, index, i, 0x7FFFFFFF, unit[0], unit[1], (unsigned int *)buf, print);
    }
    else
    {
        fPRINT_PARAM1("Unsupported io latency histogram revision\n");
    }

    if (fdi)
	    fclose(fdi);
    return 1;
}

static int mb_lat_stats_log_print(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
    char stats[LOG_PAGE_SIZE];
    int err = 0;
    int fd;
    char f1[] = FID_C1_LOG_FILENAME;
    char f2[] = FID_C2_LOG_FILENAME;

    const char *desc = "Get Latency Statistics log and show it.";
    const char *write = "Get write statistics (read default)";

    struct config {
        bool  write;
    };
    struct config cfg = {
        .write = 0,
    };

    OPT_ARGS(opts) = {
        OPT_FLAG("write",      'w', &cfg.write,      write),
        OPT_END()
    };

    fd = parse_and_open(argc, argv, desc, opts);
    if (fd < 0) return fd;

    err = nvme_get_log_simple(fd, cfg.write ? 0xc2 : 0xc1, sizeof(stats), &stats);
    if (!err)
        io_latency_histogram(cfg.write ? f2 : f1, stats, DO_PRINT_FLAG,
         cfg.write ? GLP_ID_VU_GET_WRITE_LATENCY_HISTOGRAM : GLP_ID_VU_GET_READ_LATENCY_HISTOGRAM);
    else
	nvme_show_status(err);

    close(fd);
    return err;
}

#define OP         0xFC
#define FID        0x68
static int memblaze_clear_error_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
    int err, fd;
    char *desc = "Clear Memblaze devices error log.";

    //const char *value = "new value of feature (required)";
    //const char *save = "specifies that the controller shall save the attribute";
    __u32 result;

    struct config {
        __u32 feature_id;
        __u32 value;
        int   save;
    };

    struct config cfg = {
        .feature_id   = 0xf7,
        .value        = 0x534d0001,
        .save         = 0,
    };

    OPT_ARGS(opts) = {
        OPT_END()
    };

    fd = parse_and_open(argc, argv, desc, opts);
    if (fd < 0)
        return fd;

    struct nvme_set_features_args args = {
        .args_size      = sizeof(args),
        .fd             = fd,
        .fid            = cfg.feature_id,
        .nsid           = 0,
        .cdw11          = cfg.value,
        .cdw12          = 0,
        .save           = cfg.save,
        .uuidx          = 0,
        .cdw15          = 0,
        .data_len       = 0,
        .data           = NULL,
        .timeout        = NVME_DEFAULT_IOCTL_TIMEOUT,
        .result         = &result,
    };
    err = nvme_set_features(&args);
    if (err < 0) {
        perror("set-feature");
    }
    if (!err) {
        printf("set-feature:%02x (%s), value:%#08x\n", cfg.feature_id, mb_feature_to_string(cfg.feature_id), cfg.value);
    } else if (err > 0)
        nvme_show_status(err);

/*
	struct nvme_admin_cmd admin_cmd = {
		.opcode		= OP,
		.cdw10		= FID,
	};

	err = nvme_submit_admin_passthru(fd, &admin_cmd);

	if (!err) {
		printf("OP(0x%2X) FID(0x%2X) Clear error log success.\n", OP, FID);
	} else {
		printf("NVMe Status:%s(%x)\n", nvme_status_to_string(err), err);
	};
*/
    close(fd);
    return err;
}

static int mb_set_lat_stats(int argc, char **argv,
		struct command *command, struct plugin *plugin)
{
	int err, fd;
	const char *desc = (
			"Enable/Disable Latency Statistics Tracking.\n"
			"No argument prints current status.");
	const char *enable_desc = "Enable LST";
	const char *disable_desc = "Disable LST";
	const __u32 nsid = 0;
	const __u8 fid = 0xe2;
	const __u8 sel = 0;
	const __u32 cdw11 = 0x0;
	const __u32 cdw12 = 0x0;
	const __u32 data_len = 32;
	const __u32 save = 0;
	__u32 result;
	void *buf = NULL;

	struct config {
		bool enable, disable;
	};

	struct config cfg = {
		.enable = false,
		.disable = false,
	};

	const struct argconfig_commandline_options command_line_options[] = {
		{"enable", 'e', "", CFG_FLAG, &cfg.enable, no_argument, enable_desc},
		{"disable", 'd', "", CFG_FLAG, &cfg.disable, no_argument, disable_desc},
		{NULL}
	};

	fd = parse_and_open(argc, argv, desc, command_line_options);

	enum Option {
		None = -1,
		True = 1,
		False = 0,
	};
	enum Option option = None;

	if (cfg.enable && cfg.disable)
		printf("Cannot enable and disable simultaneously.");
	else if (cfg.enable || cfg.disable)
		option = cfg.enable;

	struct nvme_get_features_args args_get = {
		.args_size	= sizeof(args_get),
		.fd		= fd,
		.fid		= fid,
		.nsid		= nsid,
		.sel		= sel,
		.cdw11		= cdw11,
		.uuidx		= 0,
		.data_len	= data_len,
		.data		= buf,
		.timeout	= NVME_DEFAULT_IOCTL_TIMEOUT,
		.result		= &result,
	};

	struct nvme_set_features_args args_set = {
		.args_size	= sizeof(args_set),
		.fd		= fd,
		.fid		= fid,
		.nsid		= nsid,
		.cdw11		= option,
		.cdw12		= cdw12,
		.save		= save,
		.uuidx		= 0,
		.cdw15		= 0,
		.data_len	= data_len,
		.data		= buf,
		.timeout	= NVME_DEFAULT_IOCTL_TIMEOUT,
		.result		= &result,
	};

	if (fd < 0)
		return fd;
	switch (option) {
	case None:
		err = nvme_get_features(&args_get);
		if (!err) {
			printf(
				"Latency Statistics Tracking (FID 0x%X) is currently (%i).\n",
				fid, result);
		} else {
			printf("Could not read feature id 0xE2.\n");
			close(fd);
			return err;
		}
		break;
	case True:
	case False:
	        err = nvme_set_features(&args_set);
		if (err > 0) {
			nvme_show_status(err);
		} else if (err < 0) {
			perror("Enable latency tracking");
			fprintf(stderr, "Command failed while parsing.\n");
		} else {
			printf("Successfully set enable bit for FID (0x%X) to %i.\n",
				0xe2, option);
		}
		break;
	default:
		printf("%d not supported.\n", option);
		err = EINVAL;
	}
	close(fd);
	return err;
}