summaryrefslogtreecommitdiffstats
path: root/plugins/micron/micron-nvme.c
blob: 63a7a7950e211189a41c124c0672a76275c39a72 (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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include <limits.h>
#include "linux/types.h"
#include "nvme-print.h"
#include "util/cleanup.h"

#define CREATE_CMD
#include "micron-nvme.h"

/* Supported Vendor specific feature ids */
#define MICRON_FEATURE_CLEAR_PCI_CORRECTABLE_ERRORS	0xC3
#define MICRON_FEATURE_CLEAR_FW_ACTIVATION_HISTORY	0xC1
#define MICRON_FEATURE_TELEMETRY_CONTROL_OPTION		0xCF
#define MICRON_FEATURE_SMBUS_OPTION			0xD5

/* Supported Vendor specific log page sizes */
#define C5_log_size (((452 + 16 * 1024) / 4) * 4096)
#define C0_log_size 512
#define C2_log_size 4096
#define D0_log_size 512
#define FB_log_size 512
#define E1_log_size 256
#define MaxLogChunk (16 * 1024)
#define CommonChunkSize (16 * 4096)

#define min(x, y) ((x) > (y) ? (y) : (x))
#define SensorCount 8

/* Plugin version major_number.minor_number.patch */
static const char *__version_major = "1";
static const char *__version_minor = "0";
static const char *__version_patch = "14";

/*
 * supported models of micron plugin; new models should be added at the end
 * before UNKNOWN_MODEL. Make sure M5410 is first in the list !
 */
enum eDriveModel {
	M5410 = 0,
	M51AX,
	M51BX,
	M51CX,
	M5407,
	M5411,
	UNKNOWN_MODEL
};

#define MICRON_VENDOR_ID 0x1344

static char *fvendorid1 = "/sys/class/nvme/nvme%d/device/vendor";
static char *fvendorid2 = "/sys/class/misc/nvme%d/device/vendor";
static char *fdeviceid1 = "/sys/class/nvme/nvme%d/device/device";
static char *fdeviceid2 = "/sys/class/misc/nvme%d/device/device";
static unsigned short vendor_id;
static unsigned short device_id;

struct LogPageHeader_t {
	unsigned char numDwordsInLogPageHeaderLo;
	unsigned char logPageHeaderFormatVersion;
	unsigned char logPageId;
	unsigned char numDwordsInLogPageHeaderHi;
	unsigned int numValidDwordsInPayload;
	unsigned int numDwordsInEntireLogPage;
};

static void WriteData(__u8 *data, __u32 len, const char *dir, const char *file, const char *msg)
{
	char tempFolder[8192] = { 0 };
	FILE *fpOutFile = NULL;

	sprintf(tempFolder, "%s/%s", dir, file);
	fpOutFile = fopen(tempFolder, "ab+");
	if (fpOutFile) {
		if (fwrite(data, 1, len,  fpOutFile) != len)
			printf("Failed to write %s data to %s\n", msg, tempFolder);
		fclose(fpOutFile);
	} else	{
		printf("Failed to open %s file to write %s\n", tempFolder, msg);
	}
}

static int ReadSysFile(const char *file, unsigned short *id)
{
	int ret = 0;
	char idstr[32] = { '\0' };
	int fd = open(file, O_RDONLY);

	if (fd < 0) {
		perror(file);
		return fd;
	}

	ret = read(fd, idstr, sizeof(idstr));
	close(fd);
	if (ret < 0)
		perror("read");
	else
		*id = strtol(idstr, NULL, 16);

	return ret;
}

static enum eDriveModel GetDriveModel(int idx)
{
	enum eDriveModel eModel = UNKNOWN_MODEL;
	char path[512];

	sprintf(path, fvendorid1, idx);
	if (ReadSysFile(path, &vendor_id) < 0) {
		sprintf(path, fvendorid2, idx);
		ReadSysFile(path, &vendor_id);
	}
	sprintf(path, fdeviceid1, idx);
	if (ReadSysFile(path, &device_id) < 0) {
		sprintf(path, fdeviceid2, idx);
		ReadSysFile(path, &device_id);
	}
	if (vendor_id == MICRON_VENDOR_ID) {
		switch (device_id) {
		case 0x5196:
			fallthrough;
		case 0x51A0:
			fallthrough;
		case 0x51A1:
			fallthrough;
		case 0x51A2:
			eModel = M51AX;
			break;
		case 0x51B0:
			fallthrough;
		case 0x51B1:
			fallthrough;
		case 0x51B2:
			eModel = M51BX;
			break;
		case 0x51C0:
			fallthrough;
		case 0x51C1:
			fallthrough;
		case 0x51C2:
			fallthrough;
		case 0x51C3:
			eModel = M51CX;
			break;
		case 0x5405:
			fallthrough;
		case 0x5406:
			fallthrough;
		case 0x5407:
			eModel = M5407;
			break;
		case 0x5410:
			eModel = M5410;
			break;
		case 0x5411:
			eModel = M5411;
			break;
		default:
			break;
		}
	}
	return eModel;
}

static int ZipAndRemoveDir(char *strDirName, char *strFileName)
{
	int  err = 0;
	char strBuffer[PATH_MAX];
	int  nRet;
	bool is_tgz = false;
	struct stat sb;

	if (strstr(strFileName, ".tar.gz") || strstr(strFileName, ".tgz")) {
		sprintf(strBuffer, "tar -zcf \"%s\" \"%s\"", strFileName, strDirName);
		is_tgz = true;
	} else {
		sprintf(strBuffer, "zip -r \"%s\" \"%s\" >temp.txt 2>&1", strFileName,
				strDirName);
	}

	err = EINVAL;
	nRet = system(strBuffer);

	/* check if log file is created, if not print error message */
	if (nRet < 0 || (stat(strFileName, &sb) == -1)) {
		if (is_tgz)
			sprintf(strBuffer, "check if tar and gzip commands are installed");
		else
			sprintf(strBuffer, "check if zip command is installed");

		fprintf(stderr, "Failed to create log data package, %s!\n", strBuffer);
	}

	sprintf(strBuffer, "rm -f -R \"%s\" >temp.txt 2>&1", strDirName);
	nRet = system(strBuffer);
	if (nRet < 0)
		printf("Failed to remove temporary files!\n");

	err = system("rm -f temp.txt");
	return err;
}

static int SetupDebugDataDirectories(char *strSN, char *strFilePath,
				     char *strMainDirName, char *strOSDirName,
				     char *strCtrlDirName)
{
	int err = 0;
	char strAppend[250];
	struct stat st;
	char *fileLocation = NULL;
	char *fileName;
	int length = 0;
	int nIndex = 0;
	char *strTemp = NULL;
	struct stat dirStat;
	int j;
	int k = 0;
	int i = 0;

	if (strchr(strFilePath, '/')) {
		fileName = strrchr(strFilePath, '\\');
		if (!fileName)
			fileName = strrchr(strFilePath, '/');

		if (fileName) {
			if (!strcmp(fileName, "/"))
				goto exit_status;

			while (strFilePath[nIndex] != '\0') {
				if ('\\' == strFilePath[nIndex] && '\\' == strFilePath[nIndex + 1])
					goto exit_status;
				nIndex++;
			}

			length = (int)strlen(strFilePath) - (int)strlen(fileName);

			if (fileName == strFilePath)
				length = 1;

			fileLocation = (char *)malloc(length + 1);
			if (!fileLocation)
				goto exit_status;
			strncpy(fileLocation, strFilePath, length);
			fileLocation[length] = '\0';

			while (fileLocation[k] != '\0') {
				if (fileLocation[k] == '\\')
					fileLocation[k] = '/';
				k++;
			}

			length = (int)strlen(fileLocation);

			if (':' == fileLocation[length - 1]) {
				strTemp = (char *)malloc(length + 2);
				if (!strTemp) {
					free(fileLocation);
					goto exit_status;
				}
				strcpy(strTemp, fileLocation);
				strcat(strTemp, "/");
				free(fileLocation);

				length = (int)strlen(strTemp);
				fileLocation = (char *)malloc(length + 1);
				if (!fileLocation) {
					free(strTemp);
					goto exit_status;
				}

				memcpy(fileLocation, strTemp, length + 1);
				free(strTemp);
			}

			if (stat(fileLocation, &st)) {
				free(fileLocation);
				goto exit_status;
			}
			free(fileLocation);
		} else {
			goto exit_status;
		}
	}

	nIndex = 0;
	for (i = 0; i < (int)strlen(strSN); i++) {
		if (strSN[i] != ' ' && strSN[i] != '\n' && strSN[i] != '\t' && strSN[i] != '\r')
			strMainDirName[nIndex++] = strSN[i];
	}
	strMainDirName[nIndex] = '\0';

	j = 1;
	while (!stat(strMainDirName, &dirStat)) {
		strMainDirName[nIndex] = '\0';
		sprintf(strAppend, "-%d", j);
		strcat(strMainDirName, strAppend);
		j++;
	}

	if (mkdir(strMainDirName, 0777) < 0) {
		err = -1;
		goto exit_status;
	}

	if (strOSDirName) {
		sprintf(strOSDirName, "%s/%s", strMainDirName, "OS");
		if (mkdir(strOSDirName, 0777) < 0) {
			rmdir(strMainDirName);
			err = -1;
			goto exit_status;
		}
	}
	if (strCtrlDirName) {
		sprintf(strCtrlDirName, "%s/%s", strMainDirName, "Controller");
		if (mkdir(strCtrlDirName, 0777) < 0) {
			if (strOSDirName)
				rmdir(strOSDirName);
			rmdir(strMainDirName);
			err = -1;
	}
	}

exit_status:
	return err;
}

static int GetLogPageSize(int nFD, unsigned char ucLogID, int *nLogSize)
{
	int err = 0;
	unsigned char pTmpBuf[CommonChunkSize] = { 0 };
	struct LogPageHeader_t *pLogHeader = NULL;

	if (ucLogID == 0xC1 || ucLogID == 0xC2 || ucLogID == 0xC4) {
		err = nvme_get_log_simple(nFD, ucLogID, CommonChunkSize, pTmpBuf);
		if (!err) {
			pLogHeader = (struct LogPageHeader_t *) pTmpBuf;
			struct LogPageHeader_t *pLogHeader1 = (struct LogPageHeader_t *) pLogHeader;
			*nLogSize = (int)(pLogHeader1->numDwordsInEntireLogPage) * 4;
			if (!pLogHeader1->logPageHeaderFormatVersion) {
				printf("Unsupported log page format version %d of log page : 0x%X\n",
				       ucLogID, err);
				*nLogSize = 0;
				err = -1;
			}
		} else {
			printf("Getting size of log page : 0x%X failed with %d (ignored)!\n",
					 ucLogID, err);
			*nLogSize = 0;
		}
	}
	return err;
}

static int NVMEGetLogPage(int nFD, unsigned char ucLogID, unsigned char *pBuffer, int nBuffSize)
{
	int err = 0;
	struct nvme_passthru_cmd cmd = { 0 };
	unsigned int uiNumDwords = (unsigned int)nBuffSize / sizeof(unsigned int);
	unsigned int uiMaxChunk = uiNumDwords;
	unsigned int uiNumChunks = 1;
	unsigned int uiXferDwords = 0;
	unsigned long long ullBytesRead = 0;
	unsigned char *pTempPtr = pBuffer;
	unsigned char ucOpCode = 0x02;

	if (!ullBytesRead && (ucLogID == 0xE6 || ucLogID == 0xE7))
		uiMaxChunk = 4096;
	else if (uiMaxChunk > 16 * 1024)
		uiMaxChunk = 16 * 1024;

	uiNumChunks = uiNumDwords / uiMaxChunk;
	if (uiNumDwords % uiMaxChunk > 0)
		uiNumChunks += 1;

	for (unsigned int i = 0; i < uiNumChunks; i++) {
		memset(&cmd, 0, sizeof(cmd));
		uiXferDwords = uiMaxChunk;
		if (i == uiNumChunks - 1 && uiNumDwords % uiMaxChunk > 0)
			uiXferDwords = uiNumDwords % uiMaxChunk;

		cmd.opcode = ucOpCode;
		cmd.cdw10 |= ucLogID;
		cmd.cdw10 |= ((uiXferDwords - 1) & 0x0000FFFF) << 16;

		if (ucLogID == 0x7)
			cmd.cdw10 |= 0x80;
		if (!ullBytesRead && (ucLogID == 0xE6 || ucLogID == 0xE7))
			cmd.cdw11 = 1;
		if (ullBytesRead > 0 && !(ucLogID == 0xE6 || ucLogID == 0xE7)) {
			unsigned long long ullOffset = ullBytesRead;

			cmd.cdw12 = ullOffset & 0xFFFFFFFF;
			cmd.cdw13 = (ullOffset >> 32) & 0xFFFFFFFF;
		}

		cmd.addr = (__u64) (uintptr_t) pTempPtr;
		cmd.nsid = 0xFFFFFFFF;
		cmd.data_len = uiXferDwords * 4;
		err = nvme_submit_admin_passthru(nFD, &cmd, NULL);
		ullBytesRead += uiXferDwords * 4;
		pTempPtr = pBuffer + ullBytesRead;
	}

	return err;
}

static int NVMEResetLog(int nFD, unsigned char ucLogID, int nBufferSize,
						long long llMaxSize)
{
	unsigned int *pBuffer = NULL;
	int err = 0;

	pBuffer = (unsigned int *)calloc(1, nBufferSize);
	if (!pBuffer)
		return err;

	while (!err && llMaxSize > 0) {
		err = NVMEGetLogPage(nFD, ucLogID, (unsigned char *)pBuffer, nBufferSize);
		if (err) {
			free(pBuffer);
			return err;
		}

		if (pBuffer[0] == 0xdeadbeef)
			break;

		llMaxSize = llMaxSize - nBufferSize;
	}

	free(pBuffer);
	return err;
}

static int GetCommonLogPage(int nFD, unsigned char ucLogID,
			    unsigned char **pBuffer, int nBuffSize)
{
	unsigned char *pTempPtr = NULL;
	int err = 0;

	pTempPtr = (unsigned char *)malloc(nBuffSize);
	if (!pTempPtr)
		goto exit_status;
	memset(pTempPtr, 0, nBuffSize);
	err = nvme_get_log_simple(nFD, ucLogID, nBuffSize, pTempPtr);
	*pBuffer = pTempPtr;

exit_status:
	return err;
}

/*
 * Plugin Commands
 */
static int micron_parse_options(struct nvme_dev **dev, int argc, char **argv,
				const char *desc,
				struct argconfig_commandline_options *opts,
				enum eDriveModel *modelp)
{
	int idx;
	int err = parse_and_open(dev, argc, argv, desc, opts);

	if (err) {
		perror("open");
		return -1;
	}

	if (modelp) {
		if (sscanf(argv[optind], "/dev/nvme%d", &idx) != 1)
			idx = 0;
		*modelp = GetDriveModel(idx);
	}

	return 0;
}

static int micron_fw_commit(int fd, int select)
{
	struct nvme_passthru_cmd cmd = {
		.opcode = nvme_admin_fw_commit,
		.cdw10 = 8,
		.cdw12 = select,
	};
	return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
}

static int micron_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\n"
		"select which firmware binary to update for 9200 devices. This requires\n"
		"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=ALL)";
	int xfer = 4096;
	void *fw_buf;
	int selectNo, fw_fd, fw_size, err, offset = 0;
	struct nvme_dev *dev;
	struct stat sb;

	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()
	};

	err = parse_and_open(&dev, argc, argv, desc, opts);
	if (err)
		return err;

	if (strlen(cfg.select) != 3) {
		fprintf(stderr, "Invalid select flag\n");
		dev_close(dev);
		return -EINVAL;
	}

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

	if (!strncmp(cfg.select, "OOB", 3)) {
		selectNo = 18;
	} else if (!strncmp(cfg.select, "EEP", 3)) {
		selectNo = 10;
	} else if (!strncmp(cfg.select, "ALL", 3)) {
		selectNo = 26;
	} else {
		fprintf(stderr, "Invalid select flag\n");
		dev_close(dev);
		return -EINVAL;
	}

	fw_fd = open(cfg.fw, O_RDONLY);
	if (fw_fd < 0) {
		fprintf(stderr, "no firmware file provided\n");
		dev_close(dev);
		return -EINVAL;
	}

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

	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;
	}

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

	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		= dev_fd(dev),
			.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) {
			nvme_show_status(err);
			goto out_free;
		}
		fw_buf += xfer;
		fw_size -= xfer;
		offset += xfer;
	}

	err = micron_fw_commit(dev_fd(dev), selectNo);

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

out_free:
	free(fw_buf);
out:
	close(fw_fd);
	dev_close(dev);
	return err;
}

static int micron_smbus_option(int argc, char **argv,
			       struct command *cmd, struct plugin *plugin)
{
	__u32 result = 0;
	__u32 cdw11 = 0;
	const char *desc = "Enable/Disable/Get status of SMBUS option on controller";
	const char *option = "enable or disable or status";
	const char *value =
	    "1 - hottest component temperature, 0 - composite temperature (default) for enable option, 0 (current), 1 (default), 2 (saved) for status options";
	const char *save = "1 - persistent, 0 - non-persistent (default)";
	int fid = MICRON_FEATURE_SMBUS_OPTION;
	enum eDriveModel model = UNKNOWN_MODEL;
	struct nvme_dev *dev;
	int err = 0;

	struct {
		char *option;
		int  value;
		int  save;
		int  status;
	} opt = {
		.option = "disable",
		.value = 0,
		.save = 0,
		.status = 0,
	};

	OPT_ARGS(opts) = {
		OPT_STRING("option", 'o', "option", &opt.option, option),
		OPT_UINT("value", 'v',	&opt.value, value),
		OPT_UINT("save", 's', &opt.save, save),
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return err;

	if (model != M5407 && model != M5411) {
		printf("This option is not supported for specified drive\n");
		dev_close(dev);
		return err;
	}

	if (!strcmp(opt.option, "enable")) {
		cdw11 = opt.value << 1 | 1;
		err = nvme_set_features_simple(dev_fd(dev), fid, 1, cdw11, opt.save,
					   &result);
		if (!err)
			printf("successfully enabled SMBus on drive\n");
		else
			printf("Failed to enabled SMBus on drive\n");
	} else if (!strcmp(opt.option, "status")) {
		struct nvme_get_features_args args = {
			.args_size	= sizeof(args),
			.fd		= dev_fd(dev),
			.fid		= fid,
			.nsid		= 1,
			.sel		= opt.value,
			.cdw11		= 0,
			.uuidx		= 0,
			.data_len	= 0,
			.data		= NULL,
			.timeout	= NVME_DEFAULT_IOCTL_TIMEOUT,
			.result		= &result,
		};

		err = nvme_get_features(&args);
		if (!err)
			printf("SMBus status on the drive: %s (returns %s temperature)\n",
			       (result & 1) ? "enabled" : "disabled",
			       (result & 2) ? "hottest component" : "composite");
		else
			printf("Failed to retrieve SMBus status on the drive\n");
	} else if (!strcmp(opt.option, "disable")) {
		cdw11 = opt.value << 1 | 0;
		err = nvme_set_features_simple(dev_fd(dev), fid, 1, cdw11, opt.save,
					       &result);
		if (!err)
			printf("Successfully disabled SMBus on drive\n");
		else
			printf("Failed to disable SMBus on drive\n");
	} else {
		printf("Invalid option %s, valid values are enable, disable or status\n",
		       opt.option);
		dev_close(dev);
		return -1;
	}

	close(dev_fd(dev));
	return err;
}

static int micron_temp_stats(int argc, char **argv, struct command *cmd,
			     struct plugin *plugin)
{

	struct nvme_smart_log smart_log;
	unsigned int temperature = 0, i = 0, err = 0;
	unsigned int tempSensors[SensorCount] = { 0 };
	const char *desc = "Retrieve Micron temperature info for the given device ";
	const char *fmt = "output format normal|json";
	struct format {
		char *fmt;
	};
	struct format cfg = {
		.fmt = "normal",
	};
	bool is_json = false;
	struct json_object *root;
	struct json_object *logPages;
	struct nvme_dev *dev;

	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = parse_and_open(&dev, argc, argv, desc, opts);
	if (err) {
		printf("\nDevice not found\n");
		return -1;
	}

	if (!strcmp(cfg.fmt, "json"))
		is_json = true;

	err = nvme_get_log_smart(dev_fd(dev), 0xffffffff, false, &smart_log);
	if (!err) {
		temperature = ((smart_log.temperature[1] << 8) | smart_log.temperature[0]);
		temperature = temperature ? temperature - 273 : 0;
		for (i = 0; i < SensorCount && tempSensors[i]; i++) {
			tempSensors[i] = le16_to_cpu(smart_log.temp_sensor[i]);
			tempSensors[i] = tempSensors[i] ? tempSensors[i] - 273 : 0;
		}
		if (is_json) {
			struct json_object *stats = json_create_object();
			char tempstr[64] = { 0 };

			root = json_create_object();
			logPages = json_create_array();
			json_object_add_value_array(root, "Micron temperature information", logPages);
			sprintf(tempstr, "%u C", temperature);
			json_object_add_value_string(stats, "Current Composite Temperature", tempstr);
			for (i = 0; i < SensorCount && tempSensors[i]; i++) {
				char sensor_str[256] = { 0 };
				char datastr[64] = { 0 };

				sprintf(sensor_str, "Temperature Sensor #%d", (i + 1));
				sprintf(datastr, "%u C", tempSensors[i]);
				json_object_add_value_string(stats, sensor_str, datastr);
			}
			json_array_add_value_object(logPages, stats);
			json_print_object(root, NULL);
			printf("\n");
			json_free_object(root);
		} else {
			printf("Micron temperature information:\n");
			printf("%-10s : %u C\n", "Current Composite Temperature", temperature);
			for (i = 0; i < SensorCount && tempSensors[i]; i++)
				printf("%-10s%d : %u C\n", "Temperature Sensor #", i + 1, tempSensors[i]);
		}
	}
	dev_close(dev);
	return err;
}

static int micron_pcie_stats(int argc, char **argv,
			     struct command *cmd, struct plugin *plugin)
{
	int  i, err = 0, bus, domain, device, function, ctrlIdx;
	char strTempFile[1024], strTempFile2[1024], command[1024];
	struct nvme_dev *dev;
	char *businfo = NULL;
	char *devicename = NULL;
	char tdevice[NAME_MAX] = { 0 };
	ssize_t sLinkSize = 0;
	FILE *fp;
	char correctable[8] = { 0 };
	char uncorrectable[8] = { 0 };
	struct nvme_passthru_cmd admin_cmd = { 0 };
	enum eDriveModel eModel = UNKNOWN_MODEL;
	char *res;
	bool is_json = true;
	bool counters = false;
	struct format {
		char *fmt;
	};
	const char *desc = "Retrieve PCIe event counters";
	const char *fmt = "output format json|normal";
	struct format cfg = {
		.fmt = "json",
	};
	struct pcie_error_counters {
		__u16 receiver_error;
		__u16 bad_tlp;
		__u16 bad_dllp;
		__u16 replay_num_rollover;
		__u16 replay_timer_timeout;
		__u16 advisory_non_fatal_error;
		__u16 DLPES;
		__u16 poisoned_tlp;
		__u16 FCPC;
		__u16 completion_timeout;
		__u16 completion_abort;
		__u16 unexpected_completion;
		__u16 receiver_overflow;
		__u16 malformed_tlp;
		__u16 ecrc_error;
		__u16 unsupported_request_error;
	} pcie_error_counters = { 0 };

	struct {
		char *err;
		int  bit;
		int  val;
	} pcie_correctable_errors[] = {
		{ "Unsupported Request Error Status (URES)", 20,
		offsetof(struct pcie_error_counters, unsupported_request_error)},
		{ "ECRC Error Status (ECRCES)", 19,
		offsetof(struct pcie_error_counters, ecrc_error)},
		{ "Malformed TLP Status (MTS)", 18,
		offsetof(struct pcie_error_counters, malformed_tlp)},
		{ "Receiver Overflow Status (ROS)", 17,
		offsetof(struct pcie_error_counters, receiver_overflow)},
		{ "Unexpected Completion Status (UCS)", 16,
		offsetof(struct pcie_error_counters, unexpected_completion)},
		{ "Completer Abort Status (CAS)", 15,
		offsetof(struct pcie_error_counters, completion_abort)},
		{ "Completion Timeout Status (CTS)", 14,
		offsetof(struct pcie_error_counters, completion_timeout)},
		{ "Flow Control Protocol Error Status (FCPES)", 13,
		offsetof(struct pcie_error_counters, FCPC)},
		{ "Poisoned TLP Status (PTS)", 12,
		offsetof(struct pcie_error_counters, poisoned_tlp)},
		{ "Data Link Protocol Error Status (DLPES)", 4,
		offsetof(struct pcie_error_counters, DLPES)},
	},
	pcie_uncorrectable_errors[] = {
		{ "Advisory Non-Fatal Error Status (ANFES)", 13,
		offsetof(struct pcie_error_counters, advisory_non_fatal_error)},
		{ "Replay Timer Timeout Status (RTS)",	12,
		offsetof(struct pcie_error_counters, replay_timer_timeout)},
		{ "REPLAY_NUM Rollover Status (RRS)", 8,
		offsetof(struct pcie_error_counters, replay_num_rollover)},
		{ "Bad DLLP Status (BDS)", 7,
		offsetof(struct pcie_error_counters, bad_dllp)},
		{ "Bad TLP Status (BTS)", 6,
		offsetof(struct pcie_error_counters, bad_tlp)},
		{ "Receiver Error Status (RES)", 0,
		offsetof(struct pcie_error_counters, receiver_error)},
	};

	__u32 correctable_errors;
	__u32 uncorrectable_errors;

	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = parse_and_open(&dev, argc, argv, desc, opts);
	if (err) {
		printf("\nDevice not found\n");
		return -1;
	}

	/* pull log details based on the model name */
	if (sscanf(argv[optind], "/dev/nvme%d", &ctrlIdx) != 1)
		ctrlIdx = 0;
	eModel = GetDriveModel(ctrlIdx);
	if (eModel == UNKNOWN_MODEL) {
		printf("Unsupported drive model for vs-pcie-stats command\n");
		goto out;
	}

	if (!strcmp(cfg.fmt, "normal"))
		is_json = false;

	if (eModel == M5407) {
		admin_cmd.opcode = 0xD6;
		admin_cmd.addr = (__u64)(uintptr_t)&pcie_error_counters;
		admin_cmd.data_len = sizeof(pcie_error_counters);
		admin_cmd.cdw10 = 1;
		err = nvme_submit_admin_passthru(dev_fd(dev), &admin_cmd, NULL);
		if (!err) {
			counters = true;
			correctable_errors = 10;
			uncorrectable_errors = 6;
			goto print_stats;
		}
	}

	if (strstr(argv[optind], "/dev/nvme") && strstr(argv[optind], "n1")) {
		devicename = strrchr(argv[optind], '/');
	} else if (strstr(argv[optind], "/dev/nvme")) {
		devicename = strrchr(argv[optind], '/');
		sprintf(tdevice, "%s%s", devicename, "n1");
		devicename = tdevice;
	} else {
		printf("Invalid device specified!\n");
		goto out;
	}
	sprintf(strTempFile, "/sys/block/%s/device", devicename);
	memset(strTempFile2, 0x0, 1024);
	sLinkSize = readlink(strTempFile, strTempFile2, 1023);
	if (sLinkSize < 0) {
		err = -errno;
		printf("Failed to read device\n");
		goto out;
	}
	if (strstr(strTempFile2, "../../nvme")) {
		sprintf(strTempFile, "/sys/block/%s/device/device", devicename);
		memset(strTempFile2, 0x0, 1024);
		sLinkSize = readlink(strTempFile, strTempFile2, 1023);
		if (sLinkSize < 0) {
			err = -errno;
			printf("Failed to read device\n");
			goto out;
		}
	}
	businfo = strrchr(strTempFile2, '/');
	if (sscanf(businfo, "/%x:%x:%x.%x", &domain, &bus, &device, &function) != 4)
		domain = bus = device = function = 0;
	sprintf(command, "setpci -s %x:%x.%x ECAP_AER+10.L", bus, device,
			function);
	fp = popen(command, "r");
	if (!fp) {
		printf("Failed to retrieve error count\n");
		goto out;
	}
	res = fgets(correctable, sizeof(correctable), fp);
	if (!res) {
		printf("Failed to retrieve error count\n");
		pclose(fp);
		goto out;
	}
	pclose(fp);

	sprintf(command, "setpci -s %x:%x.%x ECAP_AER+0x4.L", bus, device,
			function);
	fp = popen(command, "r");
	if (!fp) {
		printf("Failed to retrieve error count\n");
		goto out;
	}
	res = fgets(uncorrectable, sizeof(uncorrectable), fp);
	if (!res) {
		printf("Failed to retrieve error count\n");
		pclose(fp);
		goto out;
	}
	pclose(fp);

	correctable_errors = (__u32)strtol(correctable, NULL, 16);
	uncorrectable_errors = (__u32)strtol(uncorrectable, NULL, 16);

print_stats:
	if (is_json) {
		struct json_object *root = json_create_object();
		struct json_object *pcieErrors = json_create_array();
		struct json_object *stats = json_create_object();
		__u8 *pcounter = (__u8 *)&pcie_error_counters;

		json_object_add_value_array(root, "PCIE Stats", pcieErrors);
		for (i = 0; i < ARRAY_SIZE(pcie_correctable_errors); i++) {
			__u16 val = counters ? *(__u16 *)(pcounter + pcie_correctable_errors[i].val) :
				    (correctable_errors >> pcie_correctable_errors[i].bit) & 1;
			json_object_add_value_int(stats, pcie_correctable_errors[i].err, val);
		}
		for (i = 0; i < ARRAY_SIZE(pcie_uncorrectable_errors); i++) {
			__u16 val = counters ? *(__u16 *)(pcounter + pcie_uncorrectable_errors[i].val) :
				    (uncorrectable_errors >> pcie_uncorrectable_errors[i].bit) & 1;
			json_object_add_value_int(stats, pcie_uncorrectable_errors[i].err, val);
		}
		json_array_add_value_object(pcieErrors, stats);
		json_print_object(root, NULL);
		printf("\n");
		json_free_object(root);
	} else if (counters == true) {
		__u8 *pcounter = (__u8 *)&pcie_error_counters;

		for (i = 0; i < ARRAY_SIZE(pcie_correctable_errors); i++)
			printf("%-42s : %-1hu\n", pcie_correctable_errors[i].err,
			       *(__u16 *)(pcounter + pcie_correctable_errors[i].val));
		for (i = 0; i < ARRAY_SIZE(pcie_uncorrectable_errors); i++)
			printf("%-42s : %-1hu\n", pcie_uncorrectable_errors[i].err,
			       *(__u16 *)(pcounter + pcie_uncorrectable_errors[i].val));
	} else if (eModel == M5407 || eModel == M5410) {
		for (i = 0; i < ARRAY_SIZE(pcie_correctable_errors); i++)
			printf("%-42s : %-1d\n", pcie_correctable_errors[i].err,
			       ((correctable_errors >> pcie_correctable_errors[i].bit) & 1));
		for (i = 0; i < ARRAY_SIZE(pcie_uncorrectable_errors); i++)
			printf("%-42s : %-1d\n", pcie_uncorrectable_errors[i].err,
			       ((uncorrectable_errors >> pcie_uncorrectable_errors[i].bit) & 1));
	} else {
		printf("PCIE Stats:\n");
		printf("Device correctable errors detected: %s\n", correctable);
		printf("Device uncorrectable errors detected: %s\n", uncorrectable);
	}

out:
	dev_close(dev);
	return err;
}

static int micron_clear_pcie_correctable_errors(int argc, char **argv,
		struct command *cmd,
		struct plugin *plugin)
{
	int err = -EINVAL, bus, domain, device, function;
	char strTempFile[1024], strTempFile2[1024], command[1024];
	struct nvme_dev *dev;
	char *businfo = NULL;
	char *devicename = NULL;
	char tdevice[PATH_MAX] = { 0 };
	ssize_t sLinkSize = 0;
	enum eDriveModel model = UNKNOWN_MODEL;
	struct nvme_passthru_cmd admin_cmd = { 0 };
	char correctable[8] = { 0 };
	FILE *fp;
	char *res;
	const char *desc = "Clear PCIe Device Correctable Errors";
	__u32 result = 0;
	__u8 fid = MICRON_FEATURE_CLEAR_PCI_CORRECTABLE_ERRORS;

	OPT_ARGS(opts) = {
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return err;

	/* For M51CX models, PCIe errors are cleared using 0xC3 feature */
	if (model == M51CX) {
		err = nvme_set_features_simple(dev_fd(dev), fid, 0, (1 << 31), false,
					       &result);
		if (!err)
			err = (int)result;
		if (!err) {
			printf("Device correctable errors are cleared!\n");
			goto out;
		}
	} else if (model == M5407) {
		admin_cmd.opcode = 0xD6;
		admin_cmd.addr = 0;
		admin_cmd.cdw10 = 0;
		err = nvme_submit_admin_passthru(dev_fd(dev), &admin_cmd, NULL);
		if (!err) {
			printf("Device correctable error counters are cleared!\n");
			goto out;
		} else {
			/* proceed to clear status bits using sysfs interface */
		}
	}

	if (strstr(argv[optind], "/dev/nvme") && strstr(argv[optind], "n1")) {
		devicename = strrchr(argv[optind], '/');
	} else if (strstr(argv[optind], "/dev/nvme")) {
		devicename = strrchr(argv[optind], '/');
		sprintf(tdevice, "%s%s", devicename, "n1");
		devicename = tdevice;
	} else {
		printf("Invalid device specified!\n");
		goto out;
	}
	err = snprintf(strTempFile, sizeof(strTempFile),
				   "/sys/block/%s/device", devicename);
	if (err < 0)
		goto out;

	memset(strTempFile2, 0x0, 1024);
	sLinkSize = readlink(strTempFile, strTempFile2, 1023);
	if (sLinkSize < 0) {
		err = -errno;
		printf("Failed to read device\n");
		goto out;
	}
	if (strstr(strTempFile2, "../../nvme")) {
		err = snprintf(strTempFile, sizeof(strTempFile),
					   "/sys/block/%s/device/device", devicename);
		if (err < 0)
			goto out;
		memset(strTempFile2, 0x0, 1024);
		sLinkSize = readlink(strTempFile, strTempFile2, 1023);
		if (sLinkSize < 0) {
			err = -errno;
			printf("Failed to read device\n");
			goto out;
		}
	}
	businfo = strrchr(strTempFile2, '/');
	if (sscanf(businfo, "/%x:%x:%x.%x", &domain, &bus, &device, &function) != 4)
		domain = bus = device = function = 0;
	sprintf(command, "setpci -s %x:%x.%x ECAP_AER+0x10.L=0xffffffff", bus,
			device, function);
	err = -1;
	fp = popen(command, "r");
	if (!fp) {
		printf("Failed to clear error count\n");
		goto out;
	}
	pclose(fp);

	sprintf(command, "setpci -s %x:%x.%x ECAP_AER+0x10.L", bus, device,
			function);
	fp = popen(command, "r");
	if (!fp) {
		printf("Failed to retrieve error count\n");
		goto out;
	}
	res = fgets(correctable, sizeof(correctable), fp);
	if (!res) {
		printf("Failed to retrieve error count\n");
		pclose(fp);
		goto out;
	}
	pclose(fp);
	printf("Device correctable errors cleared!\n");
	printf("Device correctable errors detected: %s\n", correctable);
	err = 0;
out:
	dev_close(dev);
	return err;
}

static struct logpage {
	const char *field;
	char	   datastr[128];
} d0_log_page[] = {
	{ "NAND Writes (Bytes Written)", { 0 }},
	{ "Program Failure Count", { 0 }},
	{ "Erase Failures", { 0 }},
	{ "Bad Block Count", { 0 }},
	{ "NAND XOR/RAID Recovery Trigger Events", { 0 }},
	{ "NSZE Change Supported", { 0 }},
	{ "Number of NSZE Modifications", { 0 }}
};

static void init_d0_log_page(__u8 *buf, __u8 nsze)
{
	unsigned int logD0[D0_log_size/sizeof(int)] = { 0 };
	__u64 count_lo, count_hi, count;

	memcpy(logD0, buf, sizeof(logD0));


	count = ((__u64)logD0[45] << 32) | logD0[44];
	sprintf(d0_log_page[0].datastr, "0x%"PRIx64, le64_to_cpu(count));

	count_hi = ((__u64)logD0[39] << 32) | logD0[38];
	count_lo = ((__u64)logD0[37] << 32) | logD0[36];
	if (count_hi)
		sprintf(d0_log_page[1].datastr, "0x%"PRIx64"%016"PRIx64,
			le64_to_cpu(count_hi), le64_to_cpu(count_lo));
	else
		sprintf(d0_log_page[1].datastr, "0x%"PRIx64, le64_to_cpu(count_lo));

	count = ((__u64)logD0[25] << 32) | logD0[24];
	sprintf(d0_log_page[2].datastr, "0x%"PRIx64, le64_to_cpu(count));

	sprintf(d0_log_page[3].datastr, "0x%x", logD0[3]);

	count_lo = ((__u64)logD0[37] << 32) | logD0[36];
	count = ((__u64)logD0[25] << 32) | logD0[24];
	count = (__u64)logD0[3] - (count_lo + count);
	sprintf(d0_log_page[4].datastr, "0x%"PRIx64, le64_to_cpu(count));

	sprintf(d0_log_page[5].datastr, "0x%x", nsze);
	sprintf(d0_log_page[6].datastr, "0x%x", logD0[1]);
}

/* OCP and Vendor specific log data format */
struct micron_vs_logpage {
	char *field;
	int  size;	/* FB client spec version 1.0 sizes - M5410 models */
	int  size2; /* FB client spec version 0.7 sizes - M5407 models */
}
/* Smart Health Log information as per OCP spec M51CX models */
ocp_c0_log_page[] = {
	{ "Physical Media Units Written", 16},
	{ "Physical Media Units Read", 16 },
	{ "Raw Bad User NAND Block Count", 6},
	{ "Normalized Bad User NAND Block Count", 2},
	{ "Raw Bad System NAND Block Count", 6},
	{ "Normalized Bad System NAND Block Count", 2},
	{ "XOR Recovery Count", 8},
	{ "Uncorrectable Read Error Count", 8},
	{ "Soft ECC Error Count", 8},
	{ "SSD End to End Detected Counts", 4},
	{ "SSD End to End Corrected Errors", 4},
	{ "System data % life-used", 1},
	{ "Refresh Count", 7},
	{ "Maximum User Data Erase Count", 4},
	{ "Minimum User Data Erase Count", 4},
	{ "Thermal Throttling Count", 1},
	{ "Thermal Throttling Status", 1},
	{ "Reserved", 6},
	{ "PCIe Correctable Error count", 8},
	{ "Incomplete Shutdowns", 4},
	{ "Reserved", 4},
	{ "% Free Blocks", 1},
	{ "Reserved", 7},
	{ "Capacitor Health", 2},
	{ "Reserved", 6},
	{ "Unaligned I/O", 8},
	{ "Security Version Number", 8},
	{ "NUSE", 8},
	{ "PLP Start Count", 16},
	{ "Endurance Estimate", 16},
	{ "Reserved", 302},
	{ "Log Page Version", 2},
	{ "Log Page GUID", 16},
},
/* Extended SMART log information */
e1_log_page[] = {
	{ "Reserved", 12},
	{ "Grown Bad Block Count", 4},
	{ "Per Block Max Erase Count", 4},
	{ "Power On Minutes", 4},
	{ "Reserved", 24},
	{ "Write Protect Reason", 4},
	{ "Reserved", 12},
	{ "Drive Capacity", 8},
	{ "Reserved", 8},
	{ "Total Erase Count", 8},
	{ "Lifetime Use Rate", 8},
	{ "Erase Fail Count", 8},
	{ "Reserved", 8},
	{ "Reported UC Errors", 8},
	{ "Reserved", 24},
	{ "Program Fail Count", 16},
	{ "Total Bytes Read", 16},
	{ "Total Bytes Written", 16},
	{ "Reserved", 16},
	{ "TU Size", 4},
	{ "Total Block Stripe Count", 4},
	{ "Free Block Stripe Count", 4},
	{ "Block Stripe Size", 8},
	{ "Reserved", 16},
	{ "User Block Min Erase Count", 4},
	{ "User Block Avg Erase Count", 4},
	{ "User Block Max Erase Count", 4},
},
/* Vendor Specific Health Log information */
fb_log_page[] = {
	{ "Physical Media Units Written - TLC",  16, 16 },
	{ "Physical Media Units Written - SLC",  16, 16 },
	{ "Normalized Bad User NAND Block Count", 2, 2},
	{ "Raw Bad User NAND Block Count", 6, 6},
	{ "XOR Recovery Count", 8, 8},
	{ "Uncorrectable Read Error Count", 8, 8},
	{ "SSD End to End Corrected Errors", 8, 8},
	{ "SSD End to End Detected Counts", 4, 8},
	{ "SSD End to End Uncorrected Counts", 4, 8},
	{ "System data % life-used", 1, 1},
	{ "Reserved", 0, 3},
	{ "Minimum User Data Erase Count - TLC", 8, 8},
	{ "Maximum User Data Erase Count - TLC", 8, 8},
	{ "Average User Data Erase Count - TLC", 0, 8},
	{ "Minimum User Data Erase Count - SLC", 8, 8},
	{ "Maximum User Data Erase Count - SLC", 8, 8},
	{ "Average User Data Erase Count - SLC", 0, 8},
	{ "Normalized Program Fail Count", 2, 2},
	{ "Raw Program Fail Count", 6, 6},
	{ "Normalized Erase Fail Count", 2, 2},
	{ "Raw Erase Fail Count", 6, 6},
	{ "Pcie Correctable Error Count", 8, 8},
	{ "% Free Blocks (User)", 1, 1},
	{ "Reserved", 0, 3},
	{ "Security Version Number", 8, 8},
	{ "% Free Blocks (System)", 1, 1},
	{ "Reserved", 0, 3},
	{ "Dataset Management (Deallocate) Commands", 16, 16},
	{ "Incomplete TRIM Data", 8, 8},
	{ "% Age of Completed TRIM", 1, 2},
	{ "Background Back-Pressure Gauge", 1, 1},
	{ "Reserved", 0, 3},
	{ "Soft ECC Error Count", 8, 8},
	{ "Refresh Count", 8, 8},
	{ "Normalized Bad System NAND Block Count", 2, 2},
	{ "Raw Bad System NAND Block Count", 6, 6},
	{ "Endurance Estimate", 16, 16},
	{ "Thermal Throttling Status", 1, 1},
	{ "Thermal Throttling Count", 1, 1},
	{ "Unaligned I/O", 8, 8},
	{ "Physical Media Units Read", 16, 16},
	{ "Reserved", 279, 0},
	{ "Log Page Version", 2, 0},
	{ "READ CMDs exceeding threshold", 0, 4},
	{ "WRITE CMDs exceeding threshold", 0, 4},
	{ "TRIMs CMDs exceeding threshold", 0, 4},
	{ "Reserved", 0, 4},
	{ "Reserved", 0, 210},
	{ "Log Page Version", 0, 2},
	{ "Log Page GUID", 0, 16},
};

/*
 * Common function to print Micron VS log pages
 * - buf: raw log data
 * - log_page: format of the data
 * - field_count: log field count
 * - stats: json object to add fields
 * - spec: ocp spec index
 */
static void print_micron_vs_logs(__u8 *buf, struct micron_vs_logpage *log_page, int field_count,
				 struct json_object *stats, __u8 spec)
{
	__u64 lval_lo, lval_hi;
	__u32 ival;
	__u16 sval;
	__u8  cval, lval[8] = { 0 };
	int field;
	int offset = 0;

	for (field = 0; field < field_count; field++) {
		char datastr[1024] = { 0 };
		char *sfield = NULL;
		int size = !spec ? log_page[field].size : log_page[field].size2;

		if (!size)
			continue;
		sfield = log_page[field].field;
		if (size == 16) {
			if (strstr(sfield, "GUID")) {
				sprintf(datastr, "0x%"PRIx64"%"PRIx64"",
						(uint64_t)le64_to_cpu(*(uint64_t *)(&buf[offset + 8])),
						(uint64_t)le64_to_cpu(*(uint64_t *)(&buf[offset])));
			} else {
				lval_lo = *((__u64 *)(&buf[offset]));
				lval_hi = *((__u64 *)(&buf[offset + 8]));
				if (lval_hi)
					sprintf(datastr, "0x%"PRIx64"%016"PRIx64"",
						le64_to_cpu(lval_hi), le64_to_cpu(lval_lo));
				else
					sprintf(datastr, "0x%"PRIx64"", le64_to_cpu(lval_lo));
			}
		} else if (size == 8) {
			lval_lo = *((__u64 *)(&buf[offset]));
			sprintf(datastr, "0x%"PRIx64"", le64_to_cpu(lval_lo));
		} else if (size == 7) {
			/* 7 bytes will be in little-endian format, with last byte as MSB */
			memcpy(&lval[0], &buf[offset], 7);
			memcpy((void *)&lval_lo, lval, 8);
			sprintf(datastr, "0x%"PRIx64"", le64_to_cpu(lval_lo));
		} else if (size == 6) {
			ival	= *((__u32 *)(&buf[offset]));
			sval	= *((__u16 *)(&buf[offset + 4]));
			lval_lo = (((__u64)sval << 32) | ival);
			sprintf(datastr, "0x%"PRIx64"", le64_to_cpu(lval_lo));
		} else if (size == 4) {
			ival	= *((__u32 *)(&buf[offset]));
			sprintf(datastr, "0x%x", le32_to_cpu(ival));
		} else if (size == 2) {
			sval = *((__u16 *)(&buf[offset]));
			sprintf(datastr, "0x%04x", le16_to_cpu(sval));
		} else if (size == 1) {
			cval = buf[offset];
			sprintf(datastr, "0x%02x", cval);
		} else {
			sprintf(datastr, "0");
		}
		offset += size;
		/* do not print reserved values */
		if (strstr(sfield, "Reserved"))
			continue;
		if (stats)
			json_object_add_value_string(stats, sfield, datastr);
		else
			printf("%-40s : %-4s\n", sfield, datastr);
	}
}

static void print_smart_cloud_health_log(__u8 *buf, bool is_json)
{
	struct json_object *root;
	struct json_object *logPages;
	struct json_object *stats = NULL;
	int field_count = ARRAY_SIZE(ocp_c0_log_page);

	if (is_json) {
		root = json_create_object();
		stats = json_create_object();
		logPages = json_create_array();
		json_object_add_value_array(root, "OCP SMART Cloud Health Log: 0xC0",
					    logPages);
	}

	print_micron_vs_logs(buf, ocp_c0_log_page, field_count, stats, 0);

	if (is_json) {
		json_array_add_value_object(logPages, stats);
		json_print_object(root, NULL);
		printf("\n");
		json_free_object(root);
	}
}

static void print_nand_stats_fb(__u8 *buf, __u8 *buf2, __u8 nsze, bool is_json, __u8 spec)
{
	struct json_object *root;
	struct json_object *logPages;
	struct json_object *stats = NULL;
	int field_count = ARRAY_SIZE(fb_log_page);

	if (is_json) {
		root = json_create_object();
		stats = json_create_object();
		logPages = json_create_array();
		json_object_add_value_array(root, "Extended Smart Log Page : 0xFB",
					    logPages);
	}

	print_micron_vs_logs(buf, fb_log_page, field_count, stats, spec);

	/* print last three entries from D0 log page */
	if (buf2) {
		init_d0_log_page(buf2, nsze);

		if (is_json) {
			for (int i = 0; i < 7; i++)
				json_object_add_value_string(stats,
						 d0_log_page[i].field,
						 d0_log_page[i].datastr);
		} else {
			for (int i = 0; i < 7; i++)
				printf("%-40s : %s\n", d0_log_page[i].field, d0_log_page[i].datastr);
		}
	}

	if (is_json) {
		json_array_add_value_object(logPages, stats);
		json_print_object(root, NULL);
		printf("\n");
		json_free_object(root);
	}
}

static void print_nand_stats_d0(__u8 *buf, __u8 oacs, bool is_json)
{
	init_d0_log_page(buf, oacs);

	if (is_json) {
		struct json_object *root = json_create_object();
		struct json_object *stats = json_create_object();
		struct json_object *logPages = json_create_array();

		json_object_add_value_array(root,
					    "Extended Smart Log Page : 0xD0",
					    logPages);

		for (int i = 0; i < 7; i++)
			json_object_add_value_string(stats,
						     d0_log_page[i].field,
						     d0_log_page[i].datastr);

		json_array_add_value_object(logPages, stats);
		json_print_object(root, NULL);
		printf("\n");
		json_free_object(root);
	} else {
		for (int i = 0; i < 7; i++)
			printf("%-40s : %s\n", d0_log_page[i].field, d0_log_page[i].datastr);
	}
}

static bool nsze_from_oacs; /* read nsze for now from idd[4059] */

static int micron_nand_stats(int argc, char **argv,
			     struct command *cmd, struct plugin *plugin)
{
	const char *desc = "Retrieve Micron NAND stats for the given device ";
	unsigned int extSmartLog[D0_log_size/sizeof(int)] = { 0 };
	unsigned int logFB[FB_log_size/sizeof(int)] = { 0 };
	enum eDriveModel eModel = UNKNOWN_MODEL;
	struct nvme_id_ctrl ctrl;
	struct nvme_dev *dev;
	int err, ctrlIdx;
	__u8 nsze;
	bool has_d0_log = true;
	bool has_fb_log = false;
	bool is_json = true;
	struct format {
		char *fmt;
	};
	const char *fmt = "output format json|normal";
	struct format cfg = {
		.fmt = "json",
	};

	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = parse_and_open(&dev, argc, argv, desc, opts);
	if (err) {
		printf("\nDevice not found\n");
		return -1;
	}

	if (!strcmp(cfg.fmt, "normal"))
		is_json = false;

	err = nvme_identify_ctrl(dev_fd(dev), &ctrl);
	if (err) {
		printf("Error %d retrieving controller identification data\n", err);
		goto out;
	}

	/* pull log details based on the model name */
	if (sscanf(argv[optind], "/dev/nvme%d", &ctrlIdx) != 1)
		ctrlIdx = 0;
	eModel = GetDriveModel(ctrlIdx);
	if ((eModel == UNKNOWN_MODEL) || (eModel == M51CX)) {
		printf("Unsupported drive model for vs-nand-stats command\n");
		err = -1;
		goto out;
	}

	err = nvme_get_log_simple(dev_fd(dev), 0xD0, D0_log_size, extSmartLog);
	has_d0_log = !err;

	/* should check for firmware version if this log is supported or not */
	if (eModel == M5407 || eModel == M5410) {
		err = nvme_get_log_simple(dev_fd(dev), 0xFB, FB_log_size, logFB);
		has_fb_log = !err;
	}

	nsze = (ctrl.vs[987] == 0x12);
	if (!nsze && nsze_from_oacs)
		nsze = ((ctrl.oacs >> 3) & 0x1);
	err = 0;
	if (has_fb_log) {
		__u8 spec = (eModel == M5410) ? 0 : 1;	/* FB spec version */

		print_nand_stats_fb((__u8 *)logFB, (__u8 *)extSmartLog, nsze, is_json, spec);
	} else if (has_d0_log) {
		print_nand_stats_d0((__u8 *)extSmartLog, nsze, is_json);
	} else {
		printf("Unable to retrieve extended smart log for the drive\n");
		err = -ENOTTY;
	}
out:
	dev_close(dev);
	if (err > 0)
		nvme_show_status(err);

	return err;
}

static void print_ext_smart_logs_e1(__u8 *buf, bool is_json)
{
	struct json_object *root;
	struct json_object *logPages;
	struct json_object *stats = NULL;
	int field_count = ARRAY_SIZE(e1_log_page);

	if (is_json) {
		root = json_create_object();
		stats = json_create_object();
		logPages = json_create_array();
		json_object_add_value_array(root, "SMART Extended Log:0xE1", logPages);
	} else {
		printf("SMART Extended Log:0xE1\n");
	}

	print_micron_vs_logs(buf, e1_log_page, field_count, stats, 0);

	if (is_json) {
		json_array_add_value_object(logPages, stats);
		json_print_object(root, NULL);
		printf("\n");
		json_free_object(root);
	}
}

static int micron_smart_ext_log(int argc, char **argv,
				struct command *cmd, struct plugin *plugin)
{
	const char *desc = "Retrieve extended SMART logs for the given device ";
	unsigned int extSmartLog[E1_log_size/sizeof(int)] = { 0 };
	enum eDriveModel eModel = UNKNOWN_MODEL;
	int err = 0, ctrlIdx;
	struct nvme_dev *dev;
	bool is_json = true;
	struct format {
		char *fmt;
	};
	const char *fmt = "output format json|normal";
	struct format cfg = {
		.fmt = "json",
	};
	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = parse_and_open(&dev, argc, argv, desc, opts);
	if (err) {
		printf("\nDevice not found\n");
		return -1;
	}
	if (!strcmp(cfg.fmt, "normal"))
		is_json = false;

	if (sscanf(argv[optind], "/dev/nvme%d", &ctrlIdx) != 1)
		ctrlIdx = 0;
	eModel = GetDriveModel(ctrlIdx);
	if (eModel != M51CX) {
		printf("Unsupported drive model for vs-smart-ext-log command\n");
		err = -1;
		goto out;
	}
	err = nvme_get_log_simple(dev_fd(dev), 0xE1, E1_log_size, extSmartLog);
	if (!err)
		print_ext_smart_logs_e1((__u8 *)extSmartLog, is_json);

out:
	dev_close(dev);
	if (err > 0)
		nvme_show_status(err);
	return err;
}

static void GetDriveInfo(const char *strOSDirName, int nFD,
						 struct nvme_id_ctrl *ctrlp)
{
	FILE *fpOutFile = NULL;
	char tempFile[256] = { 0 };
	char strBuffer[1024] = { 0 };
	char model[41] = { 0 };
	char serial[21] = { 0 };
	char fwrev[9] = { 0 };
	char *strPDir = strdup(strOSDirName);
	char *strDest = dirname(strPDir);

	sprintf(tempFile, "%s/%s", strDest, "drive-info.txt");
	fpOutFile = fopen(tempFile, "w+");
	if (!fpOutFile) {
		printf("Failed to create %s\n", tempFile);
		free(strPDir);
		return;
	}

	strncpy(model, ctrlp->mn, 40);
	strncpy(serial, ctrlp->sn, 20);
	strncpy(fwrev, ctrlp->fr, 8);

	sprintf(strBuffer,
			"********************\nDrive Info\n********************\n");

	fprintf(fpOutFile, "%s", strBuffer);
	sprintf(strBuffer,
			"%-20s : /dev/nvme%d\n%-20s : %s\n%-20s : %-20s\n%-20s : %-20s\n",
			"Device Name", nFD,
			"Model No", (char *)model,
			"Serial No", (char *)serial, "FW-Rev", (char *)fwrev);

	fprintf(fpOutFile, "%s", strBuffer);

	sprintf(strBuffer,
			"\n********************\nPCI Info\n********************\n");

	fprintf(fpOutFile, "%s", strBuffer);

	sprintf(strBuffer,
			"%-22s : %04X\n%-22s : %04X\n",
			"VendorId", vendor_id, "DeviceId", device_id);
	fprintf(fpOutFile, "%s", strBuffer);
	fclose(fpOutFile);
	free(strPDir);
}

static void GetTimestampInfo(const char *strOSDirName)
{
	__u8 outstr[1024];
	time_t t;
	struct tm *tmp;
	size_t num;
	char *strPDir;
	char *strDest;

	t = time(NULL);
	tmp = localtime(&t);
	if (!tmp)
		return;

	num = strftime((char *)outstr, sizeof(outstr),
				   "Timestamp (UTC): %a, %d %b %Y %T %z", tmp);
	num += sprintf((char *)(outstr + num), "\nPackage Version: 1.4");
	if (num) {
		strPDir = strdup(strOSDirName);
		strDest = dirname(strPDir);
		WriteData(outstr, num, strDest, "timestamp_info.txt", "timestamp");
		free(strPDir);
	}
}

static void GetCtrlIDDInfo(const char *dir, struct nvme_id_ctrl *ctrlp)
{
	WriteData((__u8 *)ctrlp, sizeof(*ctrlp), dir,
			  "nvme_controller_identify_data.bin", "id-ctrl");
}

static void GetSmartlogData(int fd, const char *dir)
{
	struct nvme_smart_log smart_log;

	if (!nvme_get_log_smart(fd, -1, false, &smart_log))
		WriteData((__u8 *)&smart_log, sizeof(smart_log), dir,
			  "smart_data.bin", "smart log");
}

static void GetErrorlogData(int fd, int entries, const char *dir)
{
	int logSize = entries * sizeof(struct nvme_error_log_page);
	struct nvme_error_log_page *error_log =
				(struct nvme_error_log_page *)calloc(1, logSize);

	if (!error_log)
		return;

	if (!nvme_get_log_error(fd, entries, false, error_log))
		WriteData((__u8 *)error_log, logSize, dir,
			  "error_information_log.bin", "error log");

	free(error_log);
}

static void GetGenericLogs(int fd, const char *dir)
{
	struct nvme_self_test_log self_test_log;
	struct nvme_firmware_slot fw_log;
	struct nvme_cmd_effects_log effects;
	struct nvme_persistent_event_log pevent_log;
	_cleanup_huge_ struct nvme_mem_huge mh = { 0, };
	void *pevent_log_info = NULL;
	__u32 log_len = 0;
	int err = 0;

	/* get self test log */
	if (!nvme_get_log_device_self_test(fd, &self_test_log))
		WriteData((__u8 *)&self_test_log, sizeof(self_test_log), dir,
			  "drive_self_test.bin", "self test log");

	/* get fw slot info log */
	if (!nvme_get_log_fw_slot(fd, false, &fw_log))
		WriteData((__u8 *)&fw_log, sizeof(fw_log), dir,
			  "firmware_slot_info_log.bin", "firmware log");

	/* get effects log */
	if (!nvme_get_log_cmd_effects(fd, NVME_CSI_NVM, &effects))
		WriteData((__u8 *)&effects, sizeof(effects), dir,
			  "command_effects_log.bin", "effects log");

	/* get persistent event log */
	(void)nvme_get_log_persistent_event(fd, NVME_PEVENT_LOG_RELEASE_CTX,
					    sizeof(pevent_log), &pevent_log);
	memset(&pevent_log, 0, sizeof(pevent_log));
	err = nvme_get_log_persistent_event(fd, NVME_PEVENT_LOG_EST_CTX_AND_READ,
						sizeof(pevent_log), &pevent_log);
	if (err) {
		fprintf(stderr, "Setting persistent event log read ctx failed (ignored)!\n");
		return;
	}

	log_len = le64_to_cpu(pevent_log.tll);
	pevent_log_info = nvme_alloc_huge(log_len, &mh);
	if (!pevent_log_info) {
		perror("could not alloc buffer for persistent event log page (ignored)!\n");
		return;
	}

	err = nvme_get_log_persistent_event(fd, NVME_PEVENT_LOG_READ,
					    log_len, pevent_log_info);
	if (!err)
		WriteData((__u8 *)pevent_log_info, log_len, dir,
			  "persistent_event_log.bin", "persistent event log");
}

static void GetNSIDDInfo(int fd, const char *dir, int nsid)
{
	char file[PATH_MAX] = { 0 };
	struct nvme_id_ns ns;

	if (!nvme_identify_ns(fd, nsid, &ns)) {
		sprintf(file, "identify_namespace_%d_data.bin", nsid);
		WriteData((__u8 *)&ns, sizeof(ns), dir, file, "id-ns");
	}
}

static void GetOSConfig(const char *strOSDirName)
{
	FILE *fpOSConfig = NULL;
	char strBuffer[1024];
	char strFileName[PATH_MAX];
	int i;

	struct {
		char *strcmdHeader;
		char *strCommand;
	} cmdArray[] = {
		{ (char *)"SYSTEM INFORMATION", (char *)"uname -a >> %s" },
		{ (char *)"LINUX KERNEL MODULE INFORMATION", (char *)"lsmod >> %s" },
		{ (char *)"LINUX SYSTEM MEMORY INFORMATION", (char *)"cat /proc/meminfo >> %s" },
		{ (char *)"SYSTEM INTERRUPT INFORMATION", (char *)"cat /proc/interrupts >> %s" },
		{ (char *)"CPU INFORMATION", (char *)"cat /proc/cpuinfo >> %s" },
		{ (char *)"IO MEMORY MAP INFORMATION", (char *)"cat /proc/iomem >> %s" },
		{ (char *)"MAJOR NUMBER AND DEVICE GROUP", (char *)"cat /proc/devices >> %s" },
		{ (char *)"KERNEL DMESG", (char *)"dmesg >> %s" },
		{ (char *)"/VAR/LOG/MESSAGES", (char *)"cat /var/log/messages >> %s" }
	};

	sprintf(strFileName, "%s/%s", strOSDirName, "os_config.txt");

	for (i = 0; i < 7; i++) {
		fpOSConfig = fopen(strFileName, "a+");
		if (fpOSConfig) {
			fprintf(fpOSConfig,
				"\n\n\n\n%s\n-----------------------------------------------\n",
				cmdArray[i].strcmdHeader);
			fclose(fpOSConfig);
			fpOSConfig = NULL;
		}
		snprintf(strBuffer, sizeof(strBuffer) - 1,
				 cmdArray[i].strCommand, strFileName);
		if (system(strBuffer))
			fprintf(stderr, "Failed to send \"%s\"\n", strBuffer);
	}
}

static int micron_telemetry_log(int fd, __u8 type, __u8 **data,
				int *logSize, int da)
{
	int err, bs = 512, offset = bs;
	unsigned short data_area[4];
	unsigned char  ctrl_init = (type == 0x8);

	__u8 *buffer = (unsigned char *)calloc(bs, 1);

	if (!buffer)
		return -1;
	if (ctrl_init)
		err = nvme_get_log_telemetry_ctrl(fd, true, 0, bs, buffer);
	else
		err = nvme_get_log_telemetry_host(fd, 0, bs, buffer);
	if (err) {
		fprintf(stderr, "Failed to get telemetry log header for 0x%X\n", type);
		if (buffer)
			free(buffer);
		return err;
	}

	/* compute size of the log */
	data_area[1] = buffer[9]  << 8 | buffer[8];
	data_area[2] = buffer[11] << 8 | buffer[10];
	data_area[3] = buffer[13] << 8 | buffer[12];
	data_area[0] = data_area[1] > data_area[2] ? data_area[1] : data_area[2];
	data_area[0] = data_area[3] > data_area[0] ? data_area[3] : data_area[0];

	if (!data_area[da]) {
		fprintf(stderr, "Requested telemetry data for 0x%X is empty\n", type);
		if (buffer) {
			free(buffer);
			buffer = NULL;
		}
		return -1;
	}

	*logSize = data_area[da] * bs;
	offset = bs;
	err = 0;
	buffer = (unsigned char *)realloc(buffer, (size_t)(*logSize));
	if (buffer) {
		while (!err && offset != *logSize) {
			if (ctrl_init)
				err = nvme_get_log_telemetry_ctrl(fd, true, 0, *logSize, buffer + offset);
			else
				err = nvme_get_log_telemetry_host(fd, 0, *logSize, buffer + offset);
			offset += bs;
		}
	}

	if (!err && buffer) {
		*data = buffer;
	} else {
		fprintf(stderr, "Failed to get telemetry data for 0x%x\n", type);
		if (buffer)
			free(buffer);
	}

	return err;
}

static int GetTelemetryData(int fd, const char *dir)
{
	unsigned char *buffer = NULL;
	int i, err, logSize = 0;
	char msg[256] = { 0 };
	struct {
		__u8 log;
		char *file;
	} tmap[] = {
		{0x07, "nvmetelemetrylog.bin"},
		{0x08, "nvmetelemetrylog.bin"},
	};

	for (i = 0; i < (int)(ARRAY_SIZE(tmap)); i++) {
		err = micron_telemetry_log(fd, tmap[i].log, &buffer, &logSize, 0);
		if (!err && logSize > 0 && buffer) {
			sprintf(msg, "telemetry log: 0x%X", tmap[i].log);
			WriteData(buffer, logSize, dir, tmap[i].file, msg);
		}
		if (buffer) {
			free(buffer);
			buffer = NULL;
		}
		logSize = 0;
	}
	return err;
}

static int GetFeatureSettings(int fd, const char *dir)
{
	unsigned char *bufp, buf[4096] = { 0 };
	int i, err, len, errcnt = 0;
	__u32 attrVal = 0;
	char msg[256] = { 0 };

	struct features {
		int id;
		char *file;
	} fmap[] = {
		{0x01, "nvme_feature_setting_arbitration.bin"},
		{0x02, "nvme_feature_setting_pm.bin"},
		{0x03, "nvme_feature_setting_lba_range_namespace_1.bin"},
		{0x04, "nvme_feature_setting_temp_threshold.bin"},
		{0x05, "nvme_feature_setting_error_recovery.bin"},
		{0x06, "nvme_feature_setting_volatile_write_cache.bin"},
		{0x07, "nvme_feature_setting_num_queues.bin"},
		{0x08, "nvme_feature_setting_interrupt_coalescing.bin"},
		{0x09, "nvme_feature_setting_interrupt_vec_config.bin"},
		{0x0A, "nvme_feature_setting_write_atomicity.bin"},
		{0x0B, "nvme_feature_setting_async_event_config.bin"},
		{0x80, "nvme_feature_setting_sw_progress_marker.bin"},
	};

	for (i = 0; i < (int)(ARRAY_SIZE(fmap)); i++) {
		if (fmap[i].id == 0x03) {
			len = 4096;
			bufp = (unsigned char *)(&buf[0]);
		} else	{
			len = 0;
			bufp = NULL;
		}

	struct nvme_get_features_args args = {
		.args_size	= sizeof(args),
		.fd		= fd,
		.fid		= fmap[i].id,
		.nsid		= 1,
		.sel		= 0,
		.cdw11		= 0x0,
		.uuidx		= 0,
		.data_len	= len,
		.data		= bufp,
		.timeout	= NVME_DEFAULT_IOCTL_TIMEOUT,
		.result		= &attrVal,
	};
		err = nvme_get_features(&args);
		if (!err) {
			sprintf(msg, "feature: 0x%X", fmap[i].id);
			WriteData((__u8 *)&attrVal, sizeof(attrVal), dir, fmap[i].file, msg);
			if (bufp)
				WriteData(bufp, len, dir, fmap[i].file, msg);
		} else {
			fprintf(stderr, "Feature 0x%x data not retrieved, error %d (ignored)!\n",
					fmap[i].id, err);
			errcnt++;
		}
	}
	return (int)(errcnt == ARRAY_SIZE(fmap));
}

static int micron_drive_info(int argc, char **argv, struct command *cmd,
			     struct plugin *plugin)
{
	const char *desc = "Get drive HW information";
	struct nvme_id_ctrl ctrl =	{ 0 };
	struct nvme_passthru_cmd admin_cmd = { 0 };
	struct fb_drive_info {
		unsigned char hw_ver_major;
		unsigned char hw_ver_minor;
		unsigned char ftl_unit_size;
		unsigned char bs_ver_major;
		unsigned char bs_ver_minor;
	} dinfo = { 0 };
	enum eDriveModel model = UNKNOWN_MODEL;
	bool is_json = false;
	struct json_object *root, *driveInfo;
	struct nvme_dev *dev;
	struct format {
		char *fmt;
	};
	int err = 0;

	const char *fmt = "output format normal";
	struct format cfg = {
		.fmt = "normal",
	};

	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return err;

	if (model == UNKNOWN_MODEL) {
		fprintf(stderr, "ERROR : Unsupported drive for vs-drive-info cmd");
		dev_close(dev);
		return -1;
	}

	if (!strcmp(cfg.fmt, "json"))
		is_json = true;

	if (model == M5407) {
		admin_cmd.opcode = 0xD4,
		admin_cmd.addr = (__u64) (uintptr_t) &dinfo;
		admin_cmd.data_len = (__u32)sizeof(dinfo);
		admin_cmd.cdw12 = 3;
		err = nvme_submit_admin_passthru(dev_fd(dev), &admin_cmd, NULL);
		if (err) {
			fprintf(stderr, "ERROR : drive-info opcode failed with 0x%x\n", err);
			dev_close(dev);
			return -1;
		}
	} else {
		err = nvme_identify_ctrl(dev_fd(dev), &ctrl);
		if (err) {
			fprintf(stderr, "ERROR : identify_ctrl() failed with 0x%x\n", err);
			dev_close(dev);
			return -1;
		}
		dinfo.hw_ver_major = ctrl.vs[820];
		dinfo.hw_ver_minor = ctrl.vs[821];
		dinfo.ftl_unit_size = ctrl.vs[822];
	}

	if (is_json) {
		struct json_object *pinfo = json_create_object();
		char tempstr[64] = { 0 };

		root = json_create_object();
		driveInfo = json_create_array();
		json_object_add_value_array(root, "Micron Drive HW Information", driveInfo);
		sprintf(tempstr, "%u.%u", dinfo.hw_ver_major, dinfo.hw_ver_minor);
		json_object_add_value_string(pinfo, "Drive Hardware Version", tempstr);

		if (dinfo.ftl_unit_size) {
			sprintf(tempstr, "%u KB", dinfo.ftl_unit_size);
			json_object_add_value_string(pinfo, "FTL_unit_size", tempstr);
		}

		if (dinfo.bs_ver_major || dinfo.bs_ver_minor) {
			sprintf(tempstr, "%u.%u", dinfo.bs_ver_major, dinfo.bs_ver_minor);
			json_object_add_value_string(pinfo, "Boot Spec.Version", tempstr);
		}

		json_array_add_value_object(driveInfo, pinfo);
		json_print_object(root, NULL);
		printf("\n");
		json_free_object(root);
	} else {
		printf("Drive Hardware Version: %u.%u\n",
				dinfo.hw_ver_major, dinfo.hw_ver_minor);

		if (dinfo.ftl_unit_size)
			printf("FTL_unit_size: %u KB\n", dinfo.ftl_unit_size);

		if (dinfo.bs_ver_major || dinfo.bs_ver_minor)
			printf("Boot  Spec.Version: %u.%u\n",
			       dinfo.bs_ver_major, dinfo.bs_ver_minor);
	}

	dev_close(dev);
	return 0;
}

static int micron_cloud_ssd_plugin_version(int argc, char **argv,
					   struct command *cmd, struct plugin *plugin)
{
	printf("nvme-cli Micron cloud SSD plugin version: %s.%s\n",
		   __version_major, __version_minor);
	return 0;
}

static int micron_plugin_version(int argc, char **argv, struct command *cmd,
				 struct plugin *plugin)
{
	printf("nvme-cli Micron plugin version: %s.%s.%s\n",
		   __version_major, __version_minor, __version_patch);
	return 0;
}

/* Binary format of firmware activation history entry */
struct __packed fw_activation_history_entry {
	__u8                               version;
	__u8                               length;
	__u16                              rsvd1;
	__le16                             valid;
	__le64                             power_on_hour;
	__le64                             rsvd2;
	__le64                             power_cycle_count;
	__u8                               previous_fw[8];
	__u8                               activated_fw[8];
	__u8                               slot;
	__u8                               commit_action_type;
	__le16                             result;
	__u8                               rsvd3[14];
};

/* Binary format for firmware activation history table */
struct __packed micron_fw_activation_history_table {
	__u8                               log_page;
	__u8                               rsvd1[3];
	__le32                             num_entries;
	struct fw_activation_history_entry entries[20];
	__u8                               rsvd2[2790];
	__u16                              version;
	__u8                               GUID[16];
};

static int display_fw_activate_entry(int entry_count, struct fw_activation_history_entry *entry,
				     char *formatted_entry, struct json_object *stats)
{
	time_t timestamp, hours;
	char buffer[32];
	__u8 minutes, seconds;
	static const char * const ca[] = {"000b", "001b", "010b", "011b"};
	char *ptr = formatted_entry;
	int index = 0, entry_size = 82;

	if ((entry->version != 1 && entry->version != 2) || entry->length != 64)
		return -EINVAL;

	sprintf(ptr, "%d", entry_count);
	ptr += 10;

	timestamp = (le64_to_cpu(entry->power_on_hour) & 0x0000FFFFFFFFFFFFUL) / 1000;
	hours = timestamp / 3600;
	minutes = (timestamp % 3600) / 60;
	seconds = (timestamp % 3600) % 60;
	sprintf(ptr, "|%"PRIu64":%u:%u", (uint64_t)hours, minutes, seconds);
	ptr += 12;

	sprintf(ptr, "| %"PRIu64, le64_to_cpu(entry->power_cycle_count));
	ptr += 10;

	/* firmware details */
	memset(buffer, 0, sizeof(buffer));
	memcpy(buffer, entry->previous_fw, sizeof(entry->previous_fw));
	sprintf(ptr, "| %s", buffer);
	ptr += 11;

	memset(buffer, 0, sizeof(buffer));
	memcpy(buffer, entry->activated_fw, sizeof(entry->activated_fw));
	sprintf(ptr, "| %s", buffer);
	ptr += 12;

	/* firmware slot and commit action*/
	sprintf(ptr, "| %d", entry->slot);
	ptr += 9;

	if (entry->commit_action_type <= 3)
		sprintf(ptr, "| %s", ca[entry->commit_action_type]);
	else
		sprintf(ptr, "| xxxb");
	ptr += 9;

	/* result */
	if (entry->result)
		sprintf(ptr, "| Fail #%d", entry->result);
	else
		sprintf(ptr, "| pass");

	/* replace all null characters with spaces */
	ptr = formatted_entry;
	while (index < entry_size) {
		if (ptr[index] == '\0')
			ptr[index] = ' ';
		index++;
	}
	return 0;
}

static void micron_fw_activation_history_header_print(void)
{
	/* header to be printed  field widths = 10 | 12 | 10 | 11 | 12 | 9 | 9 | 9 */
	printf("__________________________________________________________________________________\n");
	printf("          |           |         |          |           |        |        |\n");
	printf("Firmware  | Power On  | Power   | Previous | New FW    | Slot   | Commit | Result\n");
	printf("Activation|   Hour    | cycle   | firmware | activated | number | Action |\n");
	printf("Counter   |           | count   |          |           |        | Type   |\n");
	printf("__________|___________|_________|__________|___________|________|________|________\n");
}

static int micron_fw_activation_history(int argc, char **argv, struct command *cmd,
					struct plugin *plugin)
{
	const char *desc = "Retrieve Firmware Activation history of the given drive";
	char formatted_output[100];
	int count = 0;
	unsigned int logC2[C2_log_size/sizeof(int)] = { 0 };
	enum eDriveModel eModel = UNKNOWN_MODEL;
	struct nvme_dev *dev;
	struct format {
		char *fmt;
	};
	int err;

	const char *fmt = "output format normal";
	struct format cfg = {
		.fmt = "normal",
	};

	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &eModel);
	if (err < 0)
		return -1;

	if (strcmp(cfg.fmt, "normal")) {
		fprintf(stderr, "only normal format is supported currently\n");
		dev_close(dev);
		return -1;
	}

	/* check if product supports fw_history log */
	err = -EINVAL;
	if (eModel != M51CX) {
		fprintf(stderr, "Unsupported drive model for vs-fw-activate-history command\n");
		goto out;
	}

	err = nvme_get_log_simple(dev_fd(dev), 0xC2, C2_log_size, logC2);
	if (err)  {
		fprintf(stderr, "Failed to retrieve fw activation history log, error: %x\n", err);
		goto out;
	}

	/* check if we have at least one entry to print */
	struct micron_fw_activation_history_table *table =
			   (struct micron_fw_activation_history_table *)logC2;

	/* check version and log page */
	if (table->log_page != 0xC2 || (table->version != 2 && table->version != 1)) {
		fprintf(stderr, "Unsupported fw activation history page: %x, version: %x\n",
				table->log_page, table->version);
		goto out;
	}

	if (!table->num_entries) {
		fprintf(stderr, "No entries were found in fw activation history log\n");
		goto out;
	}

	micron_fw_activation_history_header_print();
	for (count = 0; count < table->num_entries; count++) {
		memset(formatted_output, '\0', 100);
		if (!display_fw_activate_entry(count, &table->entries[count], formatted_output,
					       NULL))
			printf("%s\n", formatted_output);
	}
out:
	dev_close(dev);
	return err;
}

#define MICRON_FID_LATENCY_MONITOR 0xD0
#define MICRON_LOG_LATENCY_MONITOR 0xD1

static int micron_latency_stats_track(int argc, char **argv, struct command *cmd,
				      struct plugin *plugin)
{
	int err = 0;
	__u32 result = 0;
	const char *desc = "Enable, Disable or Get cmd latency monitoring stats";
	const char *option = "enable or disable or status, default is status";
	const char *command =
	    "commands to monitor for - all|read|write|trim, default is all i.e, enabled for all commands";
	const char *thrtime =
	    "The threshold value to use for latency monitoring in milliseconds, default is 800ms";

	int fid = MICRON_FID_LATENCY_MONITOR;
	enum eDriveModel model = UNKNOWN_MODEL;
	uint32_t command_mask = 0x7;	   /* 1:read 2:write 4:trim 7:all */
	uint32_t timing_mask = 0x08080800; /* R[31-24]:W[23:16]:T[15:8]:0 */
	uint32_t enable = 2;
	struct nvme_dev *dev;
	struct {
		char *option;
		char *command;
		uint32_t threshold;
	} opt = {
		.option = "status",
		.command = "all",
		.threshold = 0
	};

	OPT_ARGS(opts) = {
		OPT_STRING("option", 'o', "option", &opt.option, option),
		OPT_STRING("command", 'c', "command", &opt.command, command),
		OPT_UINT("threshold", 't', &opt.threshold, thrtime),
		OPT_END()
	};


	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return -1;

	if (!strcmp(opt.option, "enable")) {
		enable = 1;
	} else if (!strcmp(opt.option, "disable")) {
		enable = 0;
	} else if (strcmp(opt.option, "status")) {
		printf("Invalid control option %s specified\n", opt.option);
		dev_close(dev);
		return -1;
	}

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

	err = nvme_get_features(&g_args);
	if (err) {
		printf("Failed to retrieve latency monitoring feature status\n");
		dev_close(dev);
		return err;
	}

	/* If it is to retrieve the status only */
	if (enable == 2) {
		printf("Latency Tracking Statistics is currently %s",
		       (result & 0xFFFF0000) ? "enabled" : "disabled");
		if ((result & 7) == 7) {
			printf(" for All commands\n");
		} else if ((result & 7) > 0) {
			printf(" for");
			if (result & 1)
				printf(" Read");
			if (result & 2)
				printf(" Write");
			if (result & 4)
				printf(" Trim");
			printf(" commands\n");
		} else if (!result) {
			printf("\n");
		}
		dev_close(dev);
		return err;
	}

	/* read and validate threshold values if enable option is specified */
	if (enable == 1) {
		if (opt.threshold > 2550) {
			printf("The maximum threshold value cannot be more than 2550 ms\n");
			dev_close(dev);
			return -1;
		} else if (opt.threshold % 10) {
			/* timing mask is in terms of 10ms units, so min allowed is 10ms */
			printf("The threshold value should be multiple of 10 ms\n");
			dev_close(dev);
			return -1;
		}
		opt.threshold /= 10;
	}

	/* read-in command(s) to be monitored */
	if (!strcmp(opt.command, "read")) {
		command_mask = 0x1;
		timing_mask = (opt.threshold << 24);
	} else if (!strcmp(opt.command, "write")) {
		command_mask = 0x2;
		timing_mask = (opt.threshold << 16);
	} else if (!strcmp(opt.command, "trim")) {
		command_mask = 0x4;
		timing_mask = (opt.threshold << 8);
	} else if (strcmp(opt.command, "all")) {
		printf("Invalid command %s specified for option %s\n",
		opt.command, opt.option);
		dev_close(dev);
		return -1;
	}

	struct nvme_set_features_args args = {
			.args_size		= sizeof(args),
			.fd			= dev_fd(dev),
			.fid			= MICRON_FID_LATENCY_MONITOR,
			.nsid			= 0,
			.cdw11			= enable,
			.cdw12			= command_mask,
			.save			= 1,
			.uuidx			= 0,
			.cdw13			= timing_mask,
			.cdw15			= 0,
			.data_len		= 0,
			.data			= NULL,
			.timeout		= NVME_DEFAULT_IOCTL_TIMEOUT,
			.result			= &result,
	};
	err = nvme_set_features(&args);
	if (!err) {
		printf("Successfully %sd latency monitoring for %s commands with %dms threshold\n",
				opt.option, opt.command, !opt.threshold ? 800 : opt.threshold * 10);
	} else {
		printf("Failed to %s latency monitoring for %s commands with %dms threshold\n",
				opt.option, opt.command, !opt.threshold ? 800 : opt.threshold * 10);
	}

	dev_close(dev);
	return err;
}


static int micron_latency_stats_logs(int argc, char **argv, struct command *cmd,
				     struct plugin *plugin)
{
#define  LATENCY_LOG_ENTRIES 16
	struct latency_log_entry {
		uint64_t   timestamp;
		uint32_t   latency;
		uint32_t   cmdtag;
	union {
		struct {
				uint32_t opcode:8;
		uint32_t fuse:2;
		uint32_t rsvd1:4;
		uint32_t psdt:2;
		uint32_t cid:16;
		};
			uint32_t   dw0;
	};
	uint32_t nsid;
	uint32_t slba_low;
	uint32_t slba_high;
	union {
			struct {
			uint32_t nlb:16;
			uint32_t rsvd2:9;
			uint32_t deac:1;
			uint32_t prinfo:4;
			uint32_t fua:1;
			uint32_t lr:1;
		};
			uint32_t   dw12;
	};
		uint32_t   dsm;
		uint32_t   rfu[6];
	} log[LATENCY_LOG_ENTRIES];
	enum eDriveModel model = UNKNOWN_MODEL;
	struct nvme_dev *dev;
	int err = -1;
	const char *desc = "Display Latency tracking log information";

	OPT_ARGS(opts) = {
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err)
		return err;
	memset(&log, 0, sizeof(log));
	err = nvme_get_log_simple(dev_fd(dev), 0xD1, sizeof(log), &log);
	if (err) {
		if (err < 0)
			printf("Unable to retrieve latency stats log the drive\n");
		dev_close(dev);
		return err;
	}
	/* print header and each log entry */
	printf("Timestamp, Latency, CmdTag, Opcode, Fuse, Psdt, Cid, Nsid, Slba_L, Slba_H, Nlb, ");
	printf("DEAC, PRINFO, FUA, LR\n");
	for (int i = 0; i < LATENCY_LOG_ENTRIES; i++)
		printf("%"PRIu64",%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u\n",
		       log[i].timestamp, log[i].latency, log[i].cmdtag, log[i].opcode,
		       log[i].fuse, log[i].psdt, log[i].cid, log[i].nsid,
		       log[i].slba_low, log[i].slba_high, log[i].nlb,
		       log[i].deac, log[i].prinfo, log[i].fua, log[i].lr);
	printf("\n");
	dev_close(dev);
	return err;
}

static int micron_latency_stats_info(int argc, char **argv, struct command *cmd,
				     struct plugin *plugin)
{
	const char *desc = "display command latency statistics";
	const char *command = "command to display stats - all|read|write|trimdefault is all";
	int err = 0;
	struct nvme_dev *dev;
	enum eDriveModel model = UNKNOWN_MODEL;
	#define LATENCY_BUCKET_COUNT 32
	#define LATENCY_BUCKET_RSVD  32
	struct micron_latency_stats {
		uint64_t version; /* major << 32 | minior */
		uint64_t all_cmds[LATENCY_BUCKET_COUNT + LATENCY_BUCKET_RSVD];
		uint64_t read_cmds[LATENCY_BUCKET_COUNT + LATENCY_BUCKET_RSVD];
		uint64_t write_cmds[LATENCY_BUCKET_COUNT + LATENCY_BUCKET_RSVD];
		uint64_t trim_cmds[LATENCY_BUCKET_COUNT + LATENCY_BUCKET_RSVD];
		uint32_t reserved[255]; /* round up to 4K */
	} log;

	struct latency_thresholds {
		uint32_t start;
		uint32_t end;
		char *unit;
	} thresholds[LATENCY_BUCKET_COUNT] = {
		{0, 50, "us"}, {50, 100, "us"}, {100, 150, "us"}, {150, 200, "us"},
		{200, 300, "us"}, {300, 400, "us"}, {400, 500, "us"}, {500, 600, "us"},
		{600, 700, "us"}, {700, 800, "us"}, {800, 900, "us"}, {900, 1000, "us"},
		{1, 5, "ms"}, {5, 10, "ms"}, {10, 20, "ms"}, {20, 50, "ms"}, {50, 100, "ms"},
		{100, 200, "ms"}, {200, 300, "ms"}, {300, 400, "ms"}, {400, 500, "ms"},
		{500, 600, "ms"}, {600, 700, "ms"}, {700, 800, "ms"}, {800, 900, "ms"},
		{900, 1000, "ms"}, {1, 2, "s"}, {2, 3, "s"}, {3, 4, "s"}, {4, 5, "s"},
		{5, 8, "s"},
		{8, INT_MAX, "s"},
	};

	struct {
		char *command;
	} opt = {
		.command = "all"
	};

	uint64_t *cmd_stats = &log.all_cmds[0];
	char *cmd_str = "All";

	OPT_ARGS(opts) = {
		OPT_STRING("command", 'c', "command", &opt.command, command),
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return err;
	if (!strcmp(opt.command, "read")) {
		cmd_stats = &log.read_cmds[0];
	cmd_str = "Read";
	} else if (!strcmp(opt.command, "write")) {
		cmd_stats = &log.write_cmds[0];
	cmd_str = "Write";
	} else if (!strcmp(opt.command, "trim")) {
		cmd_stats = &log.trim_cmds[0];
	cmd_str = "Trim";
	} else if (strcmp(opt.command, "all")) {
		printf("Invalid command option %s to display latency stats\n", opt.command);
		dev_close(dev);
	return -1;
	}

	memset(&log, 0, sizeof(log));
	err = nvme_get_log_simple(dev_fd(dev), 0xD0, sizeof(log), &log);
	if (err) {
		if (err < 0)
			printf("Unable to retrieve latency stats log the drive\n");
		dev_close(dev);
		return err;
	}
	printf("Micron IO %s Command Latency Statistics\n"
	   "Major Revision : %d\nMinor Revision : %d\n",
	   cmd_str, (int)(log.version >> 32), (int)(log.version & 0xFFFFFFFF));
	printf("=============================================\n");
	printf("Bucket    Start     End        Command Count\n");
	printf("=============================================\n");

	for (int b = 0; b < LATENCY_BUCKET_COUNT; b++) {
		int bucket = b + 1;
		char start[32] = { 0 };
		char end[32] = { 0 };

		sprintf(start, "%u%s", thresholds[b].start, thresholds[b].unit);
		if (thresholds[b].end == INT_MAX)
			sprintf(end, "INF");
		else
			sprintf(end, "%u%s", thresholds[b].end, thresholds[b].unit);
		printf("%2d   %8s    %8s    %8"PRIu64"\n", bucket, start, end, cmd_stats[b]);
	}
	dev_close(dev);
	return err;
}

static int micron_ocp_smart_health_logs(int argc, char **argv, struct command *cmd,
					struct plugin *plugin)
{
	const char *desc = "Retrieve Smart or Extended Smart Health log for the given device ";
	unsigned int logC0[C0_log_size/sizeof(int)] = { 0 };
	unsigned int logFB[FB_log_size/sizeof(int)] = { 0 };
	struct nvme_id_ctrl ctrl;
	enum eDriveModel eModel = UNKNOWN_MODEL;
	struct nvme_dev *dev;
	bool is_json = true;
	struct format {
		char *fmt;
	};
	const char *fmt = "output format normal|json";
	struct format cfg = {
		.fmt = "json",
	};
	int err = 0;

	OPT_ARGS(opts) = {
		OPT_FMT("format", 'f', &cfg.fmt, fmt),
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &eModel);
	if (err < 0)
		return -1;

	if (!strcmp(cfg.fmt, "normal"))
		is_json = false;

	/* For M5410 and M5407, this option prints 0xFB log page */
	if (eModel == M5410 || eModel == M5407) {
		__u8 spec = (eModel == M5410) ? 0 : 1;
		__u8 nsze;

		err = nvme_identify_ctrl(dev_fd(dev), &ctrl);
		if (!err)
			err = nvme_get_log_simple(dev_fd(dev), 0xFB, FB_log_size, logFB);
		if (err) {
			if (err < 0)
				printf("Unable to retrieve smart log 0xFB for the drive\n");
			goto out;
		}

		nsze = (ctrl.vs[987] == 0x12);
		if (!nsze && nsze_from_oacs)
			nsze = ((ctrl.oacs >> 3) & 0x1);
		print_nand_stats_fb((__u8 *)logFB, NULL, nsze, is_json, spec);
		goto out;
	}

	/* check for models that support 0xC0 log */
	if (eModel != M51CX) {
		printf("Unsupported drive model for vs-smart-add-log command\n");
		err = -1;
		goto out;
	}

	err = nvme_get_log_simple(dev_fd(dev), 0xC0, C0_log_size, logC0);
	if (!err)
		print_smart_cloud_health_log((__u8 *)logC0, is_json);
	else if (err < 0)
		printf("Unable to retrieve extended smart log 0xC0 for the drive\n");
out:
	dev_close(dev);
	if (err > 0)
		nvme_show_status(err);
	return err;
}

static int micron_clr_fw_activation_history(int argc, char **argv,
					    struct command *cmd, struct plugin *plugin)
{
	const char *desc = "Clear FW activation history";
	__u32 result = 0;
	__u8 fid = MICRON_FEATURE_CLEAR_FW_ACTIVATION_HISTORY;
	enum eDriveModel model = UNKNOWN_MODEL;
	struct nvme_dev *dev;

	OPT_ARGS(opts) = {
		OPT_END()
	};
	int err = 0;

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return err;

	if (model != M51CX) {
		printf("This option is not supported for specified drive\n");
		dev_close(dev);
		return err;
	}

	err = nvme_set_features_simple(dev_fd(dev), fid, 1 << 31, 0, 0, &result);
	if (!err)
		err = (int)result;
	else
		printf("Failed to clear fw activation history, error = 0x%x\n", err);

	dev_close(dev);
	return err;
}

static int micron_telemetry_cntrl_option(int argc, char **argv,
					 struct command *cmd, struct plugin *plugin)
{
	int err = 0;
	__u32 result = 0;
	const char *desc = "Enable or Disable Controller telemetry log generation";
	const char *option = "enable or disable or status";
	const char *select =
	    "select/save values: enable/disable options1 - save (persistent), 0 - non-persistent and for status options: 0 - current, 1 - default, 2-saved";
	int fid = MICRON_FEATURE_TELEMETRY_CONTROL_OPTION;
	enum eDriveModel model = UNKNOWN_MODEL;
	struct nvme_id_ctrl ctrl = { 0 };
	struct nvme_dev *dev;

	struct {
		char *option;
		int  select;
	} opt = {
		.option = "disable",
		.select = 0,
	};

	OPT_ARGS(opts) = {
		OPT_STRING("option", 'o', "option", &opt.option, option),
		OPT_UINT("select", 's', &opt.select, select),
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return -1;

	err = nvme_identify_ctrl(dev_fd(dev), &ctrl);
	if ((ctrl.lpa & 0x8) != 0x8) {
		printf("drive doesn't support host/controller generated telemetry logs\n");
		dev_close(dev);
		return err;
	}

	if (!strcmp(opt.option, "enable")) {
		struct nvme_set_features_args args = {
				.args_size		= sizeof(args),
				.fd			= dev_fd(dev),
				.fid			= fid,
				.nsid			= 1,
				.cdw11			= 1,
				.cdw12			= 0,
				.save			= (opt.select & 0x1),
				.uuidx			= 0,
				.cdw15			= 0,
				.data_len		= 0,
				.data			= NULL,
				.timeout		= NVME_DEFAULT_IOCTL_TIMEOUT,
				.result			= &result,
		};
		err = nvme_set_features(&args);
		if (!err)
			printf("successfully set controller telemetry option\n");
		else
			printf("Failed to set controller telemetry option\n");
	} else if (!strcmp(opt.option, "disable")) {
		struct nvme_set_features_args args = {
				.args_size		= sizeof(args),
				.fd			= dev_fd(dev),
				.fid			= fid,
				.nsid			= 1,
				.cdw11			= 0,
				.cdw12			= 0,
				.save			= (opt.select & 0x1),
				.uuidx			= 0,
				.cdw15			= 0,
				.data_len		= 0,
				.data			= NULL,
				.timeout		= NVME_DEFAULT_IOCTL_TIMEOUT,
				.result			= &result,
		};
		err = nvme_set_features(&args);
		if (!err)
			printf("successfully disabled controller telemetry option\n");
		else
			printf("Failed to disable controller telemetry option\n");
	} else if (!strcmp(opt.option, "status")) {
		struct nvme_get_features_args args = {
			.args_size	= sizeof(args),
			.fd		= dev_fd(dev),
			.fid		= fid,
			.nsid		= 1,
			.sel		= opt.select & 0x3,
			.cdw11		= 0,
			.uuidx		= 0,
			.data_len	= 0,
			.data		= NULL,
			.timeout	= NVME_DEFAULT_IOCTL_TIMEOUT,
			.result		= &result,
		};

		err = nvme_get_features(&args);
		if (!err)
			printf("Controller telemetry option : %s\n",
			       (result) ? "enabled" : "disabled");
		else
			printf("Failed to retrieve controller telemetry option\n");
	} else {
		printf("invalid option %s, valid values are enable,disable or status\n",
		       opt.option);
		dev_close(dev);
		return -1;
	}

	dev_close(dev);
	return err;
}

/* M51XX models log page header */
struct micron_common_log_header  {
	uint8_t  id;
	uint8_t  version;
	uint16_t pn;
	uint32_t log_size;
	uint32_t max_size;
	uint32_t write_pointer;
	uint32_t next_pointer;
	uint32_t overwritten_bytes;
	uint8_t  flags;
	uint8_t  reserved[7];
};

/* helper function to retrieve logs with specific offset and max chunk size */
int nvme_get_log_lpo(int fd, __u8 log_id, __u32 lpo, __u32 chunk,
			 __u32 data_len, void *data)
{
	__u32 offset = lpo, xfer_len = data_len;
	void *ptr = data;
	struct nvme_get_log_args args = {
		.lpo = offset,
		.result = NULL,
		.log = ptr,
		.args_size = sizeof(args),
		.fd = fd,
		.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
		.lid = log_id,
		.len = xfer_len,
		.nsid = NVME_NSID_ALL,
		.csi = NVME_CSI_NVM,
		.lsi = NVME_LOG_LSI_NONE,
		.lsp = NVME_LOG_LSP_NONE,
		.uuidx = NVME_UUID_NONE,
		.rae = false,
		.ot = false,
	};
	int ret = 0;

	/* divide data into multiple chunks */
	do {
		xfer_len = data_len - offset;
		if (xfer_len > chunk)
			xfer_len = chunk;

		args.lpo = offset;
		args.log = ptr;
		args.len = xfer_len;
		ret = nvme_get_log(&args);
		if (ret)
			return ret;
		offset += xfer_len;
		ptr += xfer_len;
	} while (offset < data_len);
	return ret;
}

/* retrieves logs with common log format */
static int get_common_log(int fd, uint8_t id, uint8_t **buf, int *size)
{
	struct micron_common_log_header hdr = { 0 };
	int log_size = sizeof(hdr), first = 0, second = 0;
	uint8_t *buffer = NULL;
	int ret = -1;
	int chunk = 0x4000; /* max chunk size to be used for these logs */

	ret = nvme_get_log_simple(fd, id, sizeof(hdr), &hdr);
	if (ret) {
		fprintf(stderr, "pull hdr failed for  %u with error: 0x%x\n", id, ret);
		return ret;
	}

	if (hdr.id != id || !hdr.log_size || !hdr.max_size ||
	    hdr.write_pointer < sizeof(hdr)) {
		fprintf(stderr,
		    "invalid log data for LOG: 0x%X, id: 0x%X, size: %u, max: %u, wp: %u, flags: %u, np: %u\n",
		    id, hdr.id, hdr.log_size, hdr.max_size, hdr.write_pointer, hdr.flags,
		    hdr.next_pointer);
		return 1;
	}

	/*
	 * we may have just 32-bytes for some models; write to wfile if log hasn't
	 * yet reached its max size
	 */
	if (hdr.log_size == sizeof(hdr)) {
		buffer = (uint8_t *)malloc(sizeof(hdr));
		if (!buffer) {
			fprintf(stderr, "malloc of %zu bytes failed for log: 0x%X\n",
				sizeof(hdr), id);
			return -ENOMEM;
		}
		memcpy(buffer, (uint8_t *)&hdr, sizeof(hdr));
	} else if (hdr.log_size < hdr.max_size) {
		buffer = (uint8_t *)malloc(sizeof(hdr) + hdr.log_size);
		if (!buffer) {
			fprintf(stderr, "malloc of %zu bytes failed for log: 0x%X\n",
				hdr.log_size + sizeof(hdr), id);
			return -ENOMEM;
		}
		memcpy(buffer, &hdr, sizeof(hdr));
		ret = nvme_get_log_lpo(fd, id, sizeof(hdr), chunk, hdr.log_size,
				       buffer + sizeof(hdr));
		if (!ret)
			log_size += hdr.log_size;
	} else if (hdr.log_size >= hdr.max_size) {
		/*
		 * reached maximum, to maintain, sequence we need to depend on write
		 * pointer to detect wrap-overs. FW doesn't yet implement the condition
		 * hdr.log_size > hdr.max_size; also ignore over-written log data; we
		 * also ignore collisions for now
		 */
		buffer = (uint8_t *)malloc(hdr.max_size + sizeof(hdr));
		if (!buffer) {
			fprintf(stderr, "malloc of %zu bytes failed for log: 0x%X\n",
				hdr.max_size + sizeof(hdr), id);
			return -ENOMEM;
		}
		memcpy(buffer, &hdr, sizeof(hdr));

		first = hdr.max_size - hdr.write_pointer;
		second = hdr.write_pointer - sizeof(hdr);

		if (first) {
			ret = nvme_get_log_lpo(fd, id, hdr.write_pointer, chunk, first,
					       buffer + sizeof(hdr));
			if (ret) {
				free(buffer);
				fprintf(stderr, "failed to get log: 0x%X\n", id);
				return ret;
			}
			log_size += first;
		}
		if (second) {
			ret = nvme_get_log_lpo(fd, id, sizeof(hdr), chunk, second,
					       buffer + sizeof(hdr) + first);
			if (ret) {
				fprintf(stderr, "failed to get log: 0x%X\n", id);
				free(buffer);
				return ret;
			}
			log_size += second;
		}
	}
	*buf = buffer;
	*size = log_size;
	return ret;
}

static int micron_internal_logs(int argc, char **argv, struct command *cmd,
				struct plugin *plugin)
{
	int err = -EINVAL;
	int ctrlIdx, telemetry_option = 0;
	char strOSDirName[1024];
	char strCtrlDirName[1024];
	char strMainDirName[256];
	unsigned int *puiIDDBuf;
	unsigned int uiMask;
	struct nvme_id_ctrl ctrl;
	char sn[20] = { 0 };
	char msg[256] = { 0 };
	int  c_logs_index = 8; /* should be current size of aVendorLogs */
	struct nvme_dev *dev;
	struct {
		unsigned char ucLogPage;
		const char *strFileName;
		int nLogSize;
		int nMaxSize;
	} aVendorLogs[32] = {
		{ 0x03, "firmware_slot_info_log.bin", 512, 0 },
		{ 0xC1, "nvmelog_C1.bin", 0, 0 },
		{ 0xC2, "nvmelog_C2.bin", 0, 0 },
		{ 0xC4, "nvmelog_C4.bin", 0, 0 },
		{ 0xC5, "nvmelog_C5.bin", C5_log_size, 0 },
		{ 0xD0, "nvmelog_D0.bin", D0_log_size, 0 },
		{ 0xE6, "nvmelog_E6.bin", 0, 0 },
		{ 0xE7, "nvmelog_E7.bin", 0, 0 }
	},
	aM51XXLogs[] = {
		{ 0xFB, "nvmelog_FB.bin", 4096, 0 },  /* this should be collected first for M51AX */
		{ 0xD0, "nvmelog_D0.bin", 512, 0 },
		{ 0x03, "firmware_slot_info_log.bin", 512, 0},
		{ 0xF7, "nvmelog_F7.bin", 4096, 512 * 1024 },
		{ 0xF8, "nvmelog_F8.bin", 4096, 512 * 1024 },
		{ 0xF9, "nvmelog_F9.bin", 4096, 200 * 1024 * 1024 },
		{ 0xFC, "nvmelog_FC.bin", 4096, 200 * 1024 * 1024 },
		{ 0xFD, "nvmelog_FD.bin", 4096, 80 * 1024 * 1024 }
	},
	aM51AXLogs[] = {
		{ 0xCA, "nvmelog_CA.bin", 512, 0 },
		{ 0xFA, "nvmelog_FA.bin", 4096, 15232 },
		{ 0xF6, "nvmelog_F6.bin", 4096, 512 * 1024 },
		{ 0xFE, "nvmelog_FE.bin", 4096, 512 * 1024 },
		{ 0xFF, "nvmelog_FF.bin", 4096, 162 * 1024 },
		{ 0x04, "changed_namespace_log.bin", 4096, 0 },
		{ 0x05, "command_effects_log.bin", 4096, 0 },
		{ 0x06, "drive_self_test.bin", 4096, 0 }
	},
	aM51BXLogs[] = {
		{ 0xFA, "nvmelog_FA.bin", 4096, 16376 },
		{ 0xFE, "nvmelog_FE.bin", 4096, 256 * 1024 },
		{ 0xFF, "nvmelog_FF.bin", 4096, 64 * 1024 },
		{ 0xCA, "nvmelog_CA.bin", 512, 1024 }
	},
	aM51CXLogs[] = {
		{ 0xE1, "nvmelog_E1.bin", 0, 0 },
		{ 0xE2, "nvmelog_E2.bin", 0, 0 },
		{ 0xE3, "nvmelog_E3.bin", 0, 0 },
		{ 0xE4, "nvmelog_E4.bin", 0, 0 },
		{ 0xE5, "nvmelog_E5.bin", 0, 0 },
		{ 0xE8, "nvmelog_E8.bin", 0, 0 },
		{ 0xE9, "nvmelog_E9.bin", 0, 0 },
		{ 0xEA, "nvmelog_EA.bin", 0, 0 },
	};

	enum eDriveModel eModel;

	const char *desc = "This retrieves the micron debug log package";
	const char *package = "Log output data file name (required)";
	const char *type = "telemetry log type - host or controller";
	const char *data_area = "telemetry log data area 1, 2 or 3";
	unsigned char *dataBuffer = NULL;
	int bSize = 0;
	int maxSize = 0;

	struct config {
		char *type;
		char *package;
		int  data_area;
		int  log;
	};

	struct config cfg = {
		.type = "",
		.package = "",
		.data_area = -1,
		.log = 0x07,
	};

	OPT_ARGS(opts) = {
		OPT_STRING("type", 't', "log type", &cfg.type, type),
		OPT_STRING("package", 'p', "FILE", &cfg.package, package),
		OPT_UINT("data_area", 'd', &cfg.data_area, data_area),
		OPT_END()
	};

	err = parse_and_open(&dev, argc, argv, desc, opts);
	if (err)
		return err;

	/* if telemetry type is specified, check for data area */
	if (strlen(cfg.type)) {
		if (!strcmp(cfg.type, "controller")) {
			cfg.log = 0x08;
		} else if (strcmp(cfg.type, "host")) {
			printf("telemetry type (host or controller) should be specified i.e. -t=host\n");
			goto out;
		}

		if (cfg.data_area <= 0 || cfg.data_area > 3) {
			printf("data area must be selected using -d option ie --d=1,2,3\n");
			goto out;
		}
		telemetry_option = 1;
	} else if (cfg.data_area > 0) {
		printf("data area option is valid only for telemetry option (i.e --type=host|controller)\n");
		goto out;
	}

	if (!strlen(cfg.package)) {
		if (telemetry_option)
			printf("Log data file must be specified. ie -p=logfile.bin\n");
		else
			printf("Log data file must be specified. ie -p=logfile.zip or -p=logfile.tgz|logfile.tar.gz\n");
		goto out;
	}

	/* pull log details based on the model name */
	if (sscanf(argv[optind], "/dev/nvme%d", &ctrlIdx) != 1)
		ctrlIdx = 0;
	eModel = GetDriveModel(ctrlIdx);
	if (eModel == UNKNOWN_MODEL) {
		printf("Unsupported drive model for vs-internal-log collection\n");
		goto out;
	}

	err = nvme_identify_ctrl(dev_fd(dev), &ctrl);
	if (err)
		goto out;

	err = -EINVAL;
	if (telemetry_option) {
		if ((ctrl.lpa & 0x8) != 0x8) {
			printf("telemetry option is not supported for specified drive\n");
			goto out;
		}
		int logSize = 0; __u8 *buffer = NULL; const char *dir = ".";

		err = micron_telemetry_log(dev_fd(dev), cfg.log,  &buffer, &logSize,
				   cfg.data_area);
		if (!err && logSize > 0 && buffer) {
			sprintf(msg, "telemetry log: 0x%X", cfg.log);
			WriteData(buffer, logSize, dir, cfg.package, msg);
			free(buffer);
		}
		goto out;
	}

	printf("Preparing log package. This will take a few seconds...\n");

	/* trim spaces out of serial number string */
	int i, j = 0;

	for (i = 0; i < sizeof(ctrl.sn); i++) {
		if (isblank((int)ctrl.sn[i]))
			continue;
		sn[j++] = ctrl.sn[i];
	}
	sn[j] = '\0';
	strcpy(ctrl.sn, sn);

	SetupDebugDataDirectories(ctrl.sn, cfg.package, strMainDirName, strOSDirName, strCtrlDirName);

	GetTimestampInfo(strOSDirName);
	GetCtrlIDDInfo(strCtrlDirName, &ctrl);
	GetOSConfig(strOSDirName);
	GetDriveInfo(strOSDirName, ctrlIdx, &ctrl);

	for (int i = 1; i <= ctrl.nn; i++)
		GetNSIDDInfo(dev_fd(dev), strCtrlDirName, i);

	GetSmartlogData(dev_fd(dev), strCtrlDirName);
	GetErrorlogData(dev_fd(dev), ctrl.elpe, strCtrlDirName);
	GetGenericLogs(dev_fd(dev), strCtrlDirName);
	/* pull if telemetry log data is supported */
	if ((ctrl.lpa & 0x8) == 0x8)
		GetTelemetryData(dev_fd(dev), strCtrlDirName);

	GetFeatureSettings(dev_fd(dev), strCtrlDirName);

	if (eModel != M5410 && eModel != M5407) {
		memcpy(&aVendorLogs[c_logs_index], aM51XXLogs, sizeof(aM51XXLogs));
		c_logs_index += ARRAY_SIZE(aM51XXLogs);
		if (eModel == M51AX)
			memcpy((char *)&aVendorLogs[c_logs_index], aM51AXLogs, sizeof(aM51AXLogs));
		else if (eModel == M51BX)
			memcpy((char *)&aVendorLogs[c_logs_index], aM51BXLogs, sizeof(aM51BXLogs));
		else if (eModel == M51CX)
			memcpy((char *)&aVendorLogs[c_logs_index], aM51CXLogs, sizeof(aM51CXLogs));
	}

	for (int i = 0; i < (int)(ARRAY_SIZE(aVendorLogs)) && aVendorLogs[i].ucLogPage; i++) {
		err = -1;
		switch (aVendorLogs[i].ucLogPage) {
		case 0xE1:
			fallthrough;
		case 0xE5:
			fallthrough;
		case 0xE9:
			err = 1;
			break;
		case 0xE2:
			fallthrough;
		case 0xE3:
			fallthrough;
		case 0xE4:
			fallthrough;
		case 0xE8:
			fallthrough;
		case 0xEA:
			err = get_common_log(dev_fd(dev), aVendorLogs[i].ucLogPage,
				 &dataBuffer, &bSize);
			break;
		case 0xC1:
			fallthrough;
		case 0xC2:
			fallthrough;
		case 0xC4:
			err = GetLogPageSize(dev_fd(dev), aVendorLogs[i].ucLogPage,
					     &bSize);
			if (!err && bSize > 0)
				err = GetCommonLogPage(dev_fd(dev), aVendorLogs[i].ucLogPage,
						       &dataBuffer, bSize);
			break;
		case 0xE6:
			fallthrough;
		case 0xE7:
			puiIDDBuf = (unsigned int *)&ctrl;
			uiMask = puiIDDBuf[1015];
			if (!uiMask || (aVendorLogs[i].ucLogPage == 0xE6 && uiMask == 2) ||
			    (aVendorLogs[i].ucLogPage == 0xE7 && uiMask == 1)) {
				bSize = 0;
			} else {
				bSize = (int)puiIDDBuf[1023];
				if (bSize % (16 * 1024))
					bSize += (16 * 1024) - (bSize % (16 * 1024));
			}
			dataBuffer = (unsigned char *)malloc(bSize);
			if (bSize && dataBuffer) {
				memset(dataBuffer, 0, bSize);
				if (eModel == M5410 || eModel == M5407)
					err = NVMEGetLogPage(dev_fd(dev),
							     aVendorLogs[i].ucLogPage, dataBuffer,
							     bSize);
				else
					err = nvme_get_log_simple(dev_fd(dev),
								  aVendorLogs[i].ucLogPage,
								  bSize, dataBuffer);
			}
			break;
		case 0xF7:
			fallthrough;
		case 0xF9:
			fallthrough;
		case 0xFC:
			fallthrough;
		case 0xFD:
			if (eModel == M51BX)
				(void)NVMEResetLog(dev_fd(dev), aVendorLogs[i].ucLogPage,
						   aVendorLogs[i].nLogSize, aVendorLogs[i].nMaxSize);
			fallthrough;
		default:
			bSize = aVendorLogs[i].nLogSize;
			dataBuffer = (unsigned char *)malloc(bSize);
			if (!dataBuffer)
				break;
			memset(dataBuffer, 0, bSize);
			err = nvme_get_log_simple(dev_fd(dev), aVendorLogs[i].ucLogPage,
					  bSize, dataBuffer);
			maxSize = aVendorLogs[i].nMaxSize - bSize;
			while (!err && maxSize > 0 && ((unsigned int *)dataBuffer)[0] != 0xdeadbeef) {
				sprintf(msg, "log 0x%x", aVendorLogs[i].ucLogPage);
				WriteData(dataBuffer, bSize, strCtrlDirName, aVendorLogs[i].strFileName, msg);
				err = nvme_get_log_simple(dev_fd(dev),
					  aVendorLogs[i].ucLogPage,
					  bSize, dataBuffer);
				if (err || (((unsigned int *)dataBuffer)[0] == 0xdeadbeef))
					break;
				maxSize -= bSize;
			}
			break;
		}

		if (!err && dataBuffer && ((unsigned int *)dataBuffer)[0] != 0xdeadbeef) {
			sprintf(msg, "log 0x%x", aVendorLogs[i].ucLogPage);
			WriteData(dataBuffer, bSize, strCtrlDirName, aVendorLogs[i].strFileName, msg);
		}

		if (dataBuffer) {
			free(dataBuffer);
			dataBuffer = NULL;
		}
	}

	err = ZipAndRemoveDir(strMainDirName, cfg.package);
out:
	dev_close(dev);
	return err;
}

#define MIN_LOG_SIZE 512
static int micron_logpage_dir(int argc, char **argv, struct command *cmd,
			      struct plugin *plugin)
{
	int err = -1;
	const char *desc = "List the supported log pages";
	enum eDriveModel model = UNKNOWN_MODEL;
	char logbuf[MIN_LOG_SIZE];
	struct nvme_dev *dev;
	int i;

	OPT_ARGS(opts) = {
		OPT_END()
	};

	err = micron_parse_options(&dev, argc, argv, desc, opts, &model);
	if (err < 0)
		return err;

	struct nvme_supported_logs {
		uint8_t log_id;
		uint8_t supported;
		char	*desc;
	} log_list[] = {
		{0x00, 0, "Support Log Pages"},
		{0x01, 0, "Error Information"},
		{0x02, 0, "SMART / Health Information"},
		{0x03, 0, "Firmware Slot Information"},
		{0x04, 0, "Changed Namespace List"},
		{0x05, 0, "Commands Supported and Effects"},
		{0x06, 0, "Device Self Test"},
		{0x07, 0, "Telemetry Host-Initiated"},
		{0x08, 0, "Telemetry Controller-Initiated"},
		{0x09, 0, "Endurance Group Information"},
		{0x0A, 0, "Predictable Latency Per NVM Set"},
		{0x0B, 0, "Predictable Latency Event Aggregate"},
		{0x0C, 0, "Asymmetric Namespace Access"},
		{0x0D, 0, "Persistent Event Log"},
		{0x0E, 0, "Predictable Latency Event Aggregate"},
		{0x0F, 0, "Endurance Group Event Aggregate"},
		{0x10, 0, "Media Unit Status"},
		{0x11, 0, "Supported Capacity Configuration List"},
		{0x12, 0, "Feature Identifiers Supported and Effects"},
		{0x13, 0, "NVMe-MI Commands Supported and Effects"},
		{0x14, 0, "Command and Feature lockdown"},
		{0x15, 0, "Boot Partition"},
		{0x16, 0, "Rotational Media Information"},
		{0x70, 0, "Discovery"},
		{0x80, 0, "Reservation Notification"},
		{0x81, 0, "Sanitize Status"},
		{0xC0, 0, "SMART Cloud Health Log"},
		{0xC2, 0, "Firmware Activation History"},
		{0xC3, 0, "Latency Monitor Log"},
	};

	printf("Supported log page list\nLog ID : Description\n");
	for (i = 0; i < ARRAY_SIZE(log_list); i++) {
		err = nvme_get_log_simple(dev_fd(dev), log_list[i].log_id,
					  MIN_LOG_SIZE, &logbuf[0]);
		if (err)
			continue;
		printf("%02Xh    : %s\n", log_list[i].log_id, log_list[i].desc);
	}

	return err;
}