aboutsummaryrefslogtreecommitdiff
path: root/src/sg_logs.c
blob: 7be5a220b9f9801714304e11a51beb209607e93c (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
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
/* A utility program originally written for the Linux OS SCSI subsystem.
 *  Copyright (C) 2000-2022 D. Gilbert
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This program outputs information provided by a SCSI LOG SENSE command
 * and in some cases issues a LOG SELECT command.
 *
 */

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
#include <errno.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "sg_lib.h"
#include "sg_lib_names.h"
#include "sg_cmds_basic.h"
#ifdef SG_LIB_WIN32
#include "sg_pt.h"      /* needed for scsi_pt_win32_direct() */
#endif
#include "sg_unaligned.h"
#include "sg_pr2serr.h"

static const char * version_str = "2.06 20221107";    /* spc6r06 + sbc5r03 */

#define MY_NAME "sg_logs"

#define MX_ALLOC_LEN (0xfffc)
#define MX_INLEN_ALLOC_LEN (0x800000)
#define DEF_INLEN_ALLOC_LEN (0x40000)
#define SHORT_RESP_LEN 128

#define SUPP_PAGES_LPAGE 0x0
#define BUFF_OVER_UNDER_LPAGE 0x1
#define WRITE_ERR_LPAGE 0x2
#define READ_ERR_LPAGE 0x3
#define READ_REV_ERR_LPAGE 0x4
#define VERIFY_ERR_LPAGE 0x5
#define NON_MEDIUM_LPAGE 0x6
#define LAST_N_ERR_LPAGE 0x7
#define FORMAT_STATUS_LPAGE 0x8
#define LAST_N_DEFERRED_LPAGE 0xb
#define LB_PROV_LPAGE 0xc
#define TEMPERATURE_LPAGE 0xd
#define START_STOP_LPAGE 0xe
#define APP_CLIENT_LPAGE 0xf
#define SELF_TEST_LPAGE 0x10
#define SOLID_STATE_MEDIA_LPAGE 0x11
#define REQ_RECOVERY_LPAGE 0x13
#define DEVICE_STATS_LPAGE 0x14
#define BACKGROUND_SCAN_LPAGE 0x15
#define SAT_ATA_RESULTS_LPAGE 0x16
#define PROTO_SPECIFIC_LPAGE 0x18
#define STATS_LPAGE 0x19
#define PCT_LPAGE 0x1a
#define TAPE_ALERT_LPAGE 0x2e
#define IE_LPAGE 0x2f
#define NOT_SPG_SUBPG 0x0                       /* page: any */
#define SUPP_SPGS_SUBPG 0xff                    /* page: any */
#define PENDING_DEFECTS_SUBPG 0x1               /* page 0x15 */
#define BACKGROUND_OP_SUBPG 0x2                 /* page 0x15 */
#define CACHE_STATS_SUBPG 0x20                  /* page 0x19 */
#define CMD_DUR_LIMITS_SUBPG 0x21               /* page 0x19 */
#define ENV_REPORTING_SUBPG 0x1                 /* page 0xd */
#define UTILIZATION_SUBPG 0x1                   /* page 0xe */
#define ENV_LIMITS_SUBPG 0x2                    /* page 0xd */
#define LPS_MISALIGNMENT_SUBPG 0x3              /* page 0x15 */
#define ZONED_BLOCK_DEV_STATS_SUBPG 0x1         /* page 0x14 */
#define LAST_N_INQUIRY_DATA_CH_SUBPG 0x1        /* page 0xb */
#define LAST_N_MODE_PG_DATA_CH_SUBPG 0x2        /* page 0xb */

/* Vendor product numbers/identifiers */
#define VP_NONE   (-1)
#define VP_SEAG   0
#define VP_HITA   1
#define VP_TOSH   2
#define VP_LTO5   3
#define VP_LTO6   4
#define VP_ALL    99

#define MVP_OFFSET 8

/* Vendor product masks
 * MVP_STD OR-ed with MVP_<vendor> is a T10 defined lpage with vendor
 * specific parameter codes (e.g. Information Exceptions lpage [0x2f]) */
#define MVP_STD    (1 << (MVP_OFFSET - 1))
#define MVP_SEAG   (1 << (VP_SEAG + MVP_OFFSET))
#define MVP_HITA   (1 << (VP_HITA + MVP_OFFSET))
#define MVP_TOSH   (1 << (VP_TOSH + MVP_OFFSET))
#define MVP_LTO5   (1 << (VP_LTO5 + MVP_OFFSET))
#define MVP_LTO6   (1 << (VP_LTO6 + MVP_OFFSET))

#define OVP_LTO    (MVP_LTO5 | MVP_LTO6)
#define OVP_ALL    (~0)


#define PCB_STR_LEN 128

#define LOG_SENSE_PROBE_ALLOC_LEN 4
#define LOG_SENSE_DEF_TIMEOUT 64        /* seconds */

static uint8_t * rsp_buff;
static uint8_t * free_rsp_buff;
static int rsp_buff_sz = MX_ALLOC_LEN + 4;
static const int parr_sz = 4096;

static const char * unknown_s = "unknown";
static const char * not_avail = "not available";
static const char * param_c = "Parameter code";
static const char * param_c_snake = "parameter_code";
static const char * as_s_s = "as_string";
static const char * restricted_s = "restricted";

static struct option long_options[] = {
        {"All", no_argument, 0, 'A'},   /* equivalent to '-aa' */
        {"ALL", no_argument, 0, 'A'},   /* equivalent to '-aa' */
        {"all", no_argument, 0, 'a'},
        {"brief", no_argument, 0, 'b'},
        {"control", required_argument, 0, 'c'},
        {"enumerate", no_argument, 0, 'e'},
        {"exclude", no_argument, 0, 'E'},
        {"filter", required_argument, 0, 'f'},
        {"help", no_argument, 0, 'h'},
        {"hex", no_argument, 0, 'H'},
        {"in", required_argument, 0, 'i'},
        {"inhex", required_argument, 0, 'i'},
        {"json", optional_argument, 0, 'j'},
        {"list", no_argument, 0, 'l'},
        {"maxlen", required_argument, 0, 'm'},
        {"name", no_argument, 0, 'n'},
        {"new", no_argument, 0, 'N'},
        {"no_inq", no_argument, 0, 'x'},
        {"no-inq", no_argument, 0, 'x'},
        {"old", no_argument, 0, 'O'},
        {"page", required_argument, 0, 'p'},
        {"paramp", required_argument, 0, 'P'},
        {"pcb", no_argument, 0, 'q'},
        {"ppc", no_argument, 0, 'Q'},
        {"pdt", required_argument, 0, 'D'},
        {"raw", no_argument, 0, 'r'},
        {"readonly", no_argument, 0, 'X'},
        {"reset", no_argument, 0, 'R'},
        {"sp", no_argument, 0, 's'},
        {"select", no_argument, 0, 'S'},
        {"temperature", no_argument, 0, 't'},
        {"transport", no_argument, 0, 'T'},
        {"undefined", no_argument, 0, 'u'},
        {"vendor", required_argument, 0, 'M'},
        {"verbose", no_argument, 0, 'v'},
        {"version", no_argument, 0, 'V'},
        {0, 0, 0, 0},
};

struct opts_t {
    bool do_name;
    bool do_pcb;
    bool do_ppc;
    bool do_raw;
    bool do_pcreset;
    bool do_select;
    bool do_sp;
    bool do_temperature;
    bool do_transport;
    bool exclude_vendor;
    bool filter_given;
    bool maxlen_given;
    bool o_readonly;
    bool opt_new;
    bool verbose_given;
    bool version_given;
    int do_all;
    int do_brief;
    int do_enumerate;
    int do_help;
    int do_hex;
    int do_list;
    int dstrhex_no_ascii;       /* value for dStrHex() no_ascii argument */
    int hex2str_oformat;        /* value for hex2str() oformat argument */
    int vend_prod_num;  /* one of the VP_* constants or -1 (def) */
    int deduced_vpn;    /* deduced vendor_prod_num; from INQUIRY, etc */
    int verbose;
    int filter;
    int page_control;
    int maxlen;
    int pg_code;
    int subpg_code;
    int paramp;
    int no_inq;
    int dev_pdt;        /* from device or --pdt=DT */
    int decod_subpg_code;
    int undefined_hex;  /* hex format of undefined/unrecognized fields */
    const char * device_name;
    const char * in_fn;
    const char * pg_arg;
    const char * vend_prod;
    const struct log_elem * lep;
    sgj_state json_st;
};


struct log_elem {
    int pg_code;
    int subpg_code;     /* only unless subpg_high>0 then this is only */
    int subpg_high;     /* when >0 this is high end of subpage range */
    int pdt;            /* -1 for all */
    int flags;          /* bit mask; or-ed with MVP_* constants */
    const char * name;
    const char * acron;
    bool (*show_pagep)(const uint8_t * resp, int len,
                       struct opts_t * op, sgj_opaque_p jop);
                        /* Returns true if done */
};

struct vp_name_t {
    int vend_prod_num;       /* vendor/product identifier */
    const char * acron;
    const char * name;
    const char * t10_vendorp;
    const char * t10_productp;
};

static const char * ls_s = "log_sense: ";

static bool show_supported_pgs_page(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_supported_pgs_sub_page(const uint8_t * resp, int len,
                                        struct opts_t * op, sgj_opaque_p jop);
static bool show_buffer_over_under_run_page(const uint8_t * resp, int len,
                                            struct opts_t * op,
                                            sgj_opaque_p jop);
static bool show_error_counter_page(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_non_medium_error_page(const uint8_t * resp, int len,
                                       struct opts_t * op, sgj_opaque_p jop);
static bool show_last_n_error_page(const uint8_t * resp, int len,
                                   struct opts_t * op, sgj_opaque_p jop);
static bool show_format_status_page(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_last_n_deferred_error_page(const uint8_t * resp, int len,
                                            struct opts_t * op,
                                            sgj_opaque_p jop);
static bool show_last_n_inq_data_ch_page(const uint8_t * resp, int len,
                                         struct opts_t * op,
                                         sgj_opaque_p jop);
static bool show_last_n_mode_pg_data_ch_page(const uint8_t * resp, int len,
                                             struct opts_t * op,
                                             sgj_opaque_p jop);
static bool show_lb_provisioning_page(const uint8_t * resp, int len,
                                      struct opts_t * op, sgj_opaque_p jop);
static bool show_sequential_access_page(const uint8_t * resp, int len,
                                        struct opts_t * op, sgj_opaque_p jop);
static bool show_temperature_page(const uint8_t * resp, int len,
                                  struct opts_t * op, sgj_opaque_p jop);
static bool show_start_stop_page(const uint8_t * resp, int len,
                                 struct opts_t * op, sgj_opaque_p jop);
static bool show_utilization_page(const uint8_t * resp, int len,
                                  struct opts_t * op, sgj_opaque_p jop);
static bool show_app_client_page(const uint8_t * resp, int len,
                                 struct opts_t * op, sgj_opaque_p jop);
static bool show_self_test_page(const uint8_t * resp, int len,
                                struct opts_t * op, sgj_opaque_p jop);
static bool show_solid_state_media_page(const uint8_t * resp, int len,
                                        struct opts_t * op, sgj_opaque_p jop);
static bool show_device_stats_page(const uint8_t * resp, int len,
                                   struct opts_t * op, sgj_opaque_p jop);
static bool show_media_stats_page(const uint8_t * resp, int len,
                                  struct opts_t * op, sgj_opaque_p jop);
static bool show_dt_device_status_page(const uint8_t * resp, int len,
                                       struct opts_t * op, sgj_opaque_p jop);
static bool show_tapealert_response_page(const uint8_t * resp, int len,
                                         struct opts_t * op,
                                         sgj_opaque_p jop);
static bool show_requested_recovery_page(const uint8_t * resp, int len,
                                         struct opts_t * op,
                                         sgj_opaque_p jop);
static bool show_background_scan_results_page(const uint8_t * resp, int len,
                                              struct opts_t * op,
                                              sgj_opaque_p jop);
static bool show_zoned_block_dev_stats(const uint8_t * resp, int len,
                                       struct opts_t * op, sgj_opaque_p jop);
static bool show_pending_defects_page(const uint8_t * resp, int len,
                                      struct opts_t * op, sgj_opaque_p jop);
static bool show_background_op_page(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_lps_misalignment_page(const uint8_t * resp, int len,
                                       struct opts_t * op, sgj_opaque_p jop);
static bool show_element_stats_page(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_service_buffer_info_page(const uint8_t * resp, int len,
                                          struct opts_t * op,
                                          sgj_opaque_p jop);
static bool show_ata_pt_results_page(const uint8_t * resp, int len,
                                     struct opts_t * op, sgj_opaque_p jop);
static bool show_tape_diag_data_page(const uint8_t * resp, int len,
                                     struct opts_t * op, sgj_opaque_p jop);
static bool show_mchanger_diag_data_page(const uint8_t * resp, int len,
                                         struct opts_t * op,
                                         sgj_opaque_p jop);
static bool show_non_volatile_cache_page(const uint8_t * resp, int len,
                                         struct opts_t * op,
                                         sgj_opaque_p jop);
static bool show_volume_stats_pages(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_protocol_specific_page(const uint8_t * resp, int len,
                                        struct opts_t * op, sgj_opaque_p jop);
static bool show_stats_perform_pages(const uint8_t * resp, int len,
                                     struct opts_t * op, sgj_opaque_p jop);
static bool show_cache_stats_page(const uint8_t * resp, int len,
                                  struct opts_t * op, sgj_opaque_p jop);
static bool show_power_condition_transitions_page(const uint8_t * resp,
                                 int len, struct opts_t * op,
                                                  sgj_opaque_p jop);
static bool show_environmental_reporting_page(const uint8_t * resp, int len,
                                              struct opts_t * op,
                                              sgj_opaque_p jop);
static bool show_environmental_limits_page(const uint8_t * resp, int len,
                                           struct opts_t * op,
                                           sgj_opaque_p jop);
static bool show_cmd_dur_limits_page(const uint8_t * resp, int len,
                                     struct opts_t * op, sgj_opaque_p jop);
static bool show_data_compression_page(const uint8_t * resp, int len,
                                       struct opts_t * op, sgj_opaque_p jop);
static bool show_tape_alert_ssc_page(const uint8_t * resp, int len,
                                     struct opts_t * op, sgj_opaque_p jop);
static bool show_ie_page(const uint8_t * resp, int len,
                         struct opts_t * op, sgj_opaque_p jop);
static bool show_tape_usage_page(const uint8_t * resp, int len,
                                 struct opts_t * op, sgj_opaque_p jop);
static bool show_tape_capacity_page(const uint8_t * resp, int len,
                                     struct opts_t * op, sgj_opaque_p jop);
static bool show_seagate_cache_page(const uint8_t * resp, int len,
                                    struct opts_t * op, sgj_opaque_p jop);
static bool show_seagate_factory_page(const uint8_t * resp, int len,
                                      struct opts_t * op, sgj_opaque_p jop);
static bool show_hgst_perf_page(const uint8_t * resp, int len,
                                struct opts_t * op, sgj_opaque_p jop);
static bool show_hgst_misc_page(const uint8_t * resp, int len,
                                struct opts_t * op, sgj_opaque_p jop);

/* elements in page_number/subpage_number order */
static struct log_elem log_arr[] = {
    {SUPP_PAGES_LPAGE, 0, 0, -1, MVP_STD, "Supported log pages", "sp",
     show_supported_pgs_page},          /* 0, 0 */
    {SUPP_PAGES_LPAGE, SUPP_SPGS_SUBPG, 0, -1, MVP_STD, "Supported log pages "
     "and subpages", "ssp", show_supported_pgs_sub_page}, /* 0, 0xff */
    {BUFF_OVER_UNDER_LPAGE, 0, 0, -1, MVP_STD, "Buffer over-run/under-run",
     "bou", show_buffer_over_under_run_page},  /* 0x1, 0x0 */
    {WRITE_ERR_LPAGE, 0, 0, -1, MVP_STD, "Write error counters", "we",
     show_error_counter_page},          /* 0x2, 0x0 */
    {READ_ERR_LPAGE, 0, 0, -1, MVP_STD, "Read error counters", "re",
     show_error_counter_page},          /* 0x3, 0x0 */
    {READ_REV_ERR_LPAGE, 0, 0, -1, MVP_STD, "Read reverse error counters",
     "rre", show_error_counter_page},          /* 0x4, 0x0 */
    {VERIFY_ERR_LPAGE, 0, 0, -1, MVP_STD, "Verify error counters", "ve",
     show_error_counter_page},          /* 0x5, 0x0 */
    {NON_MEDIUM_LPAGE, 0, 0, -1, MVP_STD, "Non medium", "nm",
     show_non_medium_error_page},       /* 0x6, 0x0 */
    {LAST_N_ERR_LPAGE, 0, 0, -1, MVP_STD, "Last n error", "lne",
     show_last_n_error_page},           /* 0x7, 0x0 */
    {FORMAT_STATUS_LPAGE, 0, 0, 0, MVP_STD, "Format status", "fs",
     show_format_status_page},          /* 0x8, 0x0  SBC */
    {LAST_N_DEFERRED_LPAGE, 0, 0, -1, MVP_STD, "Last n deferred error", "lnd",
     show_last_n_deferred_error_page},  /* 0xb, 0x0 */
    {LAST_N_DEFERRED_LPAGE, LAST_N_INQUIRY_DATA_CH_SUBPG, 0, -1, MVP_STD,
     "Last n inquiry data changed", "lnic",
     show_last_n_inq_data_ch_page},     /* 0xb, 0x1 */
    {LAST_N_DEFERRED_LPAGE, LAST_N_MODE_PG_DATA_CH_SUBPG, 0, -1, MVP_STD,
     "Last n mode page data changed", "lnmc",
     show_last_n_mode_pg_data_ch_page}, /* 0xb, 0x2 */
    {LB_PROV_LPAGE, 0, 0, 0, MVP_STD, "Logical block provisioning", "lbp",
     show_lb_provisioning_page},        /* 0xc, 0x0  SBC */
    {0xc, 0, 0, PDT_TAPE, MVP_STD, "Sequential access device", "sad",
     show_sequential_access_page},      /* 0xc, 0x0  SSC */
    {TEMPERATURE_LPAGE, 0, 0, -1, MVP_STD, "Temperature", "temp",
     show_temperature_page},            /* 0xd, 0x0 */
    {TEMPERATURE_LPAGE, ENV_REPORTING_SUBPG, 0, -1, MVP_STD,  /* 0xd, 0x1 */
     "Environmental reporting", "enr", show_environmental_reporting_page},
    {TEMPERATURE_LPAGE, ENV_LIMITS_SUBPG, 0, -1, MVP_STD,     /* 0xd, 0x2 */
     "Environmental limits", "enl", show_environmental_limits_page},
    {START_STOP_LPAGE, 0, 0, -1, MVP_STD, "Start-stop cycle counter", "sscc",
     show_start_stop_page},             /* 0xe, 0x0 */
    {START_STOP_LPAGE, UTILIZATION_SUBPG, 0, 0, MVP_STD, "Utilization",
     "util", show_utilization_page},    /* 0xe, 0x1 SBC */    /* sbc4r04 */
    {APP_CLIENT_LPAGE, 0, 0, -1, MVP_STD, "Application client", "ac",
     show_app_client_page},             /* 0xf, 0x0 */
    {SELF_TEST_LPAGE, 0, 0, -1, MVP_STD, "Self test results", "str",
     show_self_test_page},              /* 0x10, 0x0 */
    {SOLID_STATE_MEDIA_LPAGE, 0, 0, 0, MVP_STD, "Solid state media", "ssm",
     show_solid_state_media_page},      /* 0x11, 0x0  SBC */
    {0x11, 0, 0, PDT_TAPE, MVP_STD, "DT Device status", "dtds",
     show_dt_device_status_page},       /* 0x11, 0x0  SSC,ADC */
    {0x12, 0, 0, PDT_TAPE, MVP_STD, "Tape alert response", "tar",
     show_tapealert_response_page},      /* 0x12, 0x0  SSC,ADC */
    {REQ_RECOVERY_LPAGE, 0, 0, PDT_TAPE, MVP_STD, "Requested recovery", "rr",
     show_requested_recovery_page},     /* 0x13, 0x0  SSC,ADC */
    {DEVICE_STATS_LPAGE, 0, 0, PDT_TAPE, MVP_STD, "Device statistics", "ds",
     show_device_stats_page},           /* 0x14, 0x0  SSC,ADC */
    {DEVICE_STATS_LPAGE, 0, 0, PDT_MCHANGER, MVP_STD,   /* 0x14, 0x0  SMC */
     "Media changer statistics", "mcs", show_media_stats_page},
    {DEVICE_STATS_LPAGE, ZONED_BLOCK_DEV_STATS_SUBPG,   /* 0x14,0x1 zbc2r01 */
     0, 0, MVP_STD, "Zoned block device statistics", "zbds",
     show_zoned_block_dev_stats},
    {BACKGROUND_SCAN_LPAGE, 0, 0, 0, MVP_STD, "Background scan results",
     "bsr", show_background_scan_results_page}, /* 0x15, 0x0  SBC */
    {BACKGROUND_SCAN_LPAGE, BACKGROUND_OP_SUBPG, 0, 0, MVP_STD,
     "Background operation", "bop", show_background_op_page},
                                        /* 0x15, 0x2  SBC */
    {BACKGROUND_SCAN_LPAGE, LPS_MISALIGNMENT_SUBPG, 0, 0, MVP_STD,
     "LPS misalignment", "lps", show_lps_misalignment_page},
                                        /* 0x15, 0x3  SBC-4 */
    {0x15, 0, 0, PDT_MCHANGER, MVP_STD, "Element statistics", "els",
     show_element_stats_page},          /* 0x15, 0x0  SMC */
    {0x15, 0, 0, PDT_ADC, MVP_STD, "Service buffers information", "sbi",
     show_service_buffer_info_page},    /* 0x15, 0x0  ADC */
    {BACKGROUND_SCAN_LPAGE, PENDING_DEFECTS_SUBPG, 0, 0, MVP_STD,
     "Pending defects", "pd", show_pending_defects_page}, /* 0x15, 0x1  SBC */
    {SAT_ATA_RESULTS_LPAGE, 0, 0, 0, MVP_STD, "ATA pass-through results",
     "aptr", show_ata_pt_results_page}, /* 0x16, 0x0  SAT */
    {0x16, 0, 0, PDT_TAPE, MVP_STD, "Tape diagnostic data", "tdd",
     show_tape_diag_data_page},         /* 0x16, 0x0  SSC */
    {0x16, 0, 0, PDT_MCHANGER, MVP_STD, "Media changer diagnostic data",
     "mcdd", show_mchanger_diag_data_page}, /* 0x16, 0x0  SMC */
    {0x17, 0, 0, 0, MVP_STD, "Non volatile cache", "nvc",
     show_non_volatile_cache_page},     /* 0x17, 0x0  SBC */
    {0x17, 0, 0xf, PDT_TAPE, MVP_STD, "Volume statistics", "vs",
     show_volume_stats_pages},          /* 0x17, 0x0...0xf  SSC */
    {PROTO_SPECIFIC_LPAGE, 0, 0, -1, MVP_STD, "Protocol specific port",
     "psp", show_protocol_specific_page},  /* 0x18, 0x0  */
    {STATS_LPAGE, 0, 0, -1, MVP_STD, "General Statistics and Performance",
     "gsp", show_stats_perform_pages},  /* 0x19, 0x0  */
    {STATS_LPAGE, 0x1, 0x1f, -1, MVP_STD, "Group Statistics and Performance",
     "grsp", show_stats_perform_pages}, /* 0x19, 0x1...0x1f  */
    {STATS_LPAGE, CACHE_STATS_SUBPG, 0, -1, MVP_STD,    /* 0x19, 0x20  */
     "Cache memory statistics", "cms", show_cache_stats_page},
    {STATS_LPAGE, CMD_DUR_LIMITS_SUBPG, 0, -1, MVP_STD, /* 0x19, 0x21  */
     "Command duration limits statistics", "cdl",
     show_cmd_dur_limits_page /* spc6r01 */ },
    {PCT_LPAGE, 0, 0, -1, MVP_STD, "Power condition transitions", "pct",
     show_power_condition_transitions_page}, /* 0x1a, 0  */
    {0x1b, 0, 0, PDT_TAPE, MVP_STD, "Data compression", "dc",
     show_data_compression_page},       /* 0x1b, 0  SSC */
    {0x2d, 0, 0, PDT_TAPE, MVP_STD, "Current service information", "csi",
     NULL},                             /* 0x2d, 0  SSC */
    {TAPE_ALERT_LPAGE, 0, 0, PDT_TAPE, MVP_STD, "Tape alert", "ta",
     show_tape_alert_ssc_page},         /* 0x2e, 0  SSC */
    {IE_LPAGE, 0, 0, -1, (MVP_STD | MVP_HITA),
     "Informational exceptions", "ie", show_ie_page},       /* 0x2f, 0  */
/* vendor specific */
    {0x30, 0, 0, PDT_DISK, MVP_HITA, "Performance counters (Hitachi)",
     "pc_hi", show_hgst_perf_page},     /* 0x30, 0  SBC */
    {0x30, 0, 0, PDT_TAPE, OVP_LTO, "Tape usage (lto-5, 6)", "tu_",
     show_tape_usage_page},             /* 0x30, 0  SSC */
    {0x31, 0, 0, PDT_TAPE, OVP_LTO, "Tape capacity (lto-5, 6)",
     "tc_", show_tape_capacity_page},   /* 0x31, 0  SSC */
    {0x32, 0, 0, PDT_TAPE, MVP_LTO5, "Data compression (lto-5)",
     "dc_", show_data_compression_page}, /* 0x32, 0  SSC; redirect to 0x1b */
    {0x33, 0, 0, PDT_TAPE, MVP_LTO5, "Write errors (lto-5)", "we_",
     NULL},                             /* 0x33, 0  SSC */
    {0x34, 0, 0, PDT_TAPE, MVP_LTO5, "Read forward errors (lto-5)",
     "rfe_", NULL},                             /* 0x34, 0  SSC */
    {0x35, 0, 0, PDT_TAPE, OVP_LTO, "DT Device Error (lto-5, 6)",
     "dtde_", NULL},                             /* 0x35, 0  SSC */
    {0x37, 0, 0, PDT_DISK, MVP_SEAG, "Cache (seagate)", "c_se",
     show_seagate_cache_page},          /* 0x37, 0  SBC */
    {0x37, 0, 0, PDT_DISK, MVP_HITA, "Miscellaneous (hitachi)", "mi_hi",
     show_hgst_misc_page},                             /* 0x37, 0  SBC */
    {0x37, 0, 0, PDT_TAPE, MVP_LTO5, "Performance characteristics "
     "(lto-5)", "pc_", NULL},                             /* 0x37, 0  SSC */
    {0x38, 0, 0, PDT_TAPE, MVP_LTO5, "Blocks/bytes transferred "
     "(lto-5)", "bbt_", NULL},                             /* 0x38, 0  SSC */
    {0x39, 0, 0, PDT_TAPE, MVP_LTO5, "Host port 0 interface errors "
     "(lto-5)", "hp0_", NULL},                             /* 0x39, 0  SSC */
    {0x3a, 0, 0, PDT_TAPE, MVP_LTO5, "Drive control verification "
     "(lto-5)", "dcv_", NULL},                             /* 0x3a, 0  SSC */
    {0x3b, 0, 0, PDT_TAPE, MVP_LTO5, "Host port 1 interface errors "
     "(lto-5)", "hp1_", NULL},                             /* 0x3b, 0  SSC */
    {0x3c, 0, 0, PDT_TAPE, MVP_LTO5, "Drive usage information "
     "(lto-5)", "dui_", NULL},                             /* 0x3c, 0  SSC */
    {0x3d, 0, 0, PDT_TAPE, MVP_LTO5, "Subsystem statistics (lto-5)",
     "ss_", NULL},                             /* 0x3d, 0  SSC */
    {0x3e, 0, 0, PDT_DISK, MVP_SEAG, "Factory (seagate)", "f_se",
     show_seagate_factory_page},        /* 0x3e, 0  SBC */
    {0x3e, 0, 0, PDT_DISK, MVP_HITA, "Factory (hitachi)", "f_hi",
     NULL},                             /* 0x3e, 0  SBC */
    {0x3e, 0, 0, PDT_TAPE, OVP_LTO, "Device Status (lto-5, 6)",
     "ds_", NULL},                             /* 0x3e, 0  SSC */

    {-1, -1, -1, -1, 0, NULL, "zzzzz", NULL},           /* end sentinel */
};

/* Supported vendor product codes */
/* Arrange in alphabetical order by acronym */
static struct vp_name_t vp_arr[] = {
    {VP_SEAG, "sea", "Seagate", "SEAGATE", NULL},
    {VP_HITA, "hit", "Hitachi", "HGST", NULL},
    {VP_HITA, "wdc", "WDC/Hitachi", "WDC", NULL},
    {VP_TOSH, "tos", "Toshiba", "TOSHIBA", NULL},
    {VP_LTO5, "lto5", "LTO-5 (tape drive consortium)", NULL, NULL},
    {VP_LTO6, "lto6", "LTO-6 (tape drive consortium)", NULL, NULL},
    {VP_ALL, "all", "enumerate all vendor specific", NULL, NULL},
    {0, NULL, NULL, NULL, NULL},
};

static char t10_vendor_str[10];
static char t10_product_str[18];

#ifdef SG_LIB_WIN32
static bool win32_spt_init_state = false;
static bool win32_spt_curr_state = false;
#endif


static void
usage(int hval)
{
    if (1 == hval) {
        pr2serr(
           "Usage: sg_logs [-ALL] [--all] [--brief] [--control=PC] "
           "[--enumerate]\n"
           "               [--exclude] [--filter=FL] [--help] [--hex] "
           "[--in=FN]\n"
           "               [--json[=JO]] [--list] [--maxlen=LEN] [--name] "
           "[--no_inq]\n"
           "               [--page=PG] [--paramp=PP] [--pcb] [--ppc] "
           "[--pdt=DT]\n"
           "               [--raw] [--readonly] [--reset] [--select] "
           "[--sp]\n"
           "               [--undefined] [--temperature] [--transport] "
           "[--vendor=VP]\n"
           "               [--verbose] [--version] DEVICE\n"
           "  where the main options are:\n"
           "    --ALL|-A        fetch and decode all log pages and "
           "subpages\n"
           "    --all|-a        fetch and decode all log pages, but not "
           "subpages; use\n"
           "                    twice to fetch and decode all log pages "
           "and subpages\n"
           "    --brief|-b      shorten the output of some log pages\n"
           "    --enumerate|-e    enumerate known pages, ignore DEVICE. "
           "Sort order,\n"
           "                      '-e': all by acronym; '-ee': non-vendor "
           "by acronym;\n"
           "                      '-eee': all numerically; '-eeee': "
           "non-v numerically\n"
           "    --filter=FL|-f FL    FL is parameter code to display (def: "
           "all);\n"
           "                         with '-e' then FL>=0 enumerate that "
           "pdt + spc\n"
           "                         FL=-1 all (default), FL=-2 spc only\n"
           "    --help|-h       print usage message then exit. Use twice "
           "for more help\n"
           "    --hex|-H        output response in hex (default: decode if "
           "known)\n"
           "    --in=FN|-i FN    FN is a filename containing a log page "
           "in ASCII hex\n"
           "                     or binary if --raw also given. --inhex=FN "
           "also accepted\n"
           "    --json[=JO]|-jJO    output in JSON instead of human "
            "readable\n"
            "                        test. Use --json=? for JSON help\n"
           "    --list|-l       list supported log pages; twice: list "
           "supported log\n"
           "                    pages and subpages page; thrice: merge of "
           "both pages\n"
           "    --page=PG|-p PG    PG is either log page acronym, PGN or "
           "PGN,SPGN\n"
           "                       where (S)PGN is a (sub) page number\n");
        pr2serr(
           "    --raw|-r        either output response in binary to stdout "
           "or, if\n"
           "                    '--in=FN' is given, FN is decoded as "
           "binary\n"
           "    --temperature|-t    decode temperature (log page 0xd or "
           "0x2f)\n"
           "    --transport|-T    decode transport (protocol specific port "
           "0x18) page\n"
           "    --vendor=VP|-M VP    vendor/product abbreviation [or "
           "number]\n"
           "    --verbose|-v    increase verbosity\n\n"
           "Performs a SCSI LOG SENSE (or LOG SELECT) command and decodes "
           "the response.\nIf only DEVICE is given then '-p sp' (supported "
           "pages) is assumed. Use\n'-e' to see known pages and their "
           "acronyms. For more help use '-hh'.\n");
    } else if (hval > 1) {
        pr2serr(
           "  where sg_logs' lesser used options are:\n"
           "    --control=PC|-c PC    page control(PC) (default: 1)\n"
           "                          0: current threshold, 1: current "
           "cumulative\n"
           "                          2: default threshold, 3: default "
           "cumulative\n"
           "    --exclude|-E    exclude vendor specific pages and "
           "parameters\n"
           "    --list|-l       list supported log page names (equivalent to "
           "'-p sp')\n"
           "                    use twice to list supported log page and "
           "subpage names\n"
           "    --maxlen=LEN|-m LEN    max response length (def: 0 "
           "-> everything)\n"
           "                           when > 1 will request LEN bytes\n"
           "    --name|-n       decode some pages into multiple name=value "
           "lines\n"
           "    --no_inq|-x     no initial INQUIRY output (twice: and no "
           "INQUIRY call)\n"
           "    --old|-O        use old interface (use as first option)\n"
           "    --paramp=PP|-P PP    place PP in parameter pointer field in "
           "cdb (def: 0)\n"
           "    --pcb|-q        show parameter control bytes in decoded "
           "output\n"
           "    --ppc|-Q        set the Parameter Pointer Control (PPC) bit "
           "(def: 0)\n"
           "    --pdt=DT|-D DT    DT is peripheral device type to use with "
           "'--in=FN'\n"
           "                      or when '--no_inq' is used\n"
           "    --readonly|-X    open DEVICE read-only (def: first "
           "read-write then if\n"
           "                     fails try open again read-only)\n"
           "    --reset|-R      reset log parameters (takes PC and SP into "
           "account)\n"
           "                    (uses PCR bit in LOG SELECT)\n"
           "    --select|-S     perform LOG SELECT (def: LOG SENSE)\n"
           "    --sp|-s         set the Saving Parameters (SP) bit (def: "
           "0)\n"
           "    --undefined|-u    hex format for undefined/unrecognized "
           "fields,\n"
           "                      use one or more times; format as per "
           "--hex\n"
           "    --version|-V    output version string then exit\n\n"
           "If DEVICE and --select are given, a LOG SELECT command will be "
           "issued.\nIf DEVICE is not given and '--in=FN' is given then FN "
           "will decoded as if\nit were a log page. The contents of FN "
           "generated by either a prior\n'sg_logs -HHH ...' invocation or "
           "by a text editor.\nLog pages defined in SPC are common "
           "to all device types.\n");
    }
}

static void
usage_old()
{
    printf("Usage: sg_logs [-a] [-A] [-b] [-c=PC] [-D=DT] [-e] [-E] [-f=FL] "
           "[-h]\n"
           "               [-H] [-i=FN] [-l] [-L] [-m=LEN] [-M=VP] [-n] "
           "[-p=PG]\n"
           "               [-paramp=PP] [-pcb] [-ppc] [-r] [-select] [-sp] "
           "[-t] [-T]\n"
           "               [-u] [-v] [-V] [-x] [-X] [-?] DEVICE\n"
           "  where:\n"
           "    -a     fetch and decode all log pages\n"
           "    -A     fetch and decode all log pages and subpages\n"
           "    -b     shorten the output of some log pages\n"
           "    -c=PC    page control(PC) (default: 1)\n"
           "                  0: current threshold, 1: current cumulative\n"
           "                  2: default threshold, 3: default cumulative\n"
           "    -e     enumerate known log pages\n"
           "    -D=DT    DT is peripheral device type to use with "
           "'--in=FN'\n"
           "    -E     exclude vendor specific pages and parameters\n"
           "    -f=FL    filter match parameter code or pdt\n"
           "    -h     output in hex (default: decode if known)\n"
           "    -H     output in hex (same as '-h')\n"
           "    -i=FN    FN is a filename containing a log page "
           "in ASCII hex.\n"
           "    -l     list supported log page names (equivalent to "
           "'-p=0')\n"
           "    -L     list supported log page and subpages names "
           "(equivalent to\n"
           "           '-p=0,ff')\n"
           "    -m=LEN   max response length (decimal) (def: 0 "
           "-> everything)\n"
           "    -M=VP    vendor/product abbreviation [or number]\n"
           "    -n       decode some pages into multiple name=value "
           "lines\n"
           "    -N|--new    use new interface\n"
           "    -p=PG    PG is an acronym (def: 'sp')\n"
           "    -p=PGN    page code in hex (def: 0)\n"
           "    -p=PGN,SPGN    page and subpage codes in hex, (defs: 0,0)\n"
           "    -paramp=PP   (in hex) (def: 0)\n"
           "    -pcb     show parameter control bytes in decoded "
           "output\n");
    printf("    -ppc     set the Parameter Pointer Control (PPC) bit "
           "(def: 0)\n"
           "    -r       reset log parameters (takes PC and SP into "
           "account)\n"
           "             (uses PCR bit in LOG SELECT)\n"
           "    -select  perform LOG SELECT (def: LOG SENSE)\n"
           "    -sp      set the Saving Parameters (SP) bit (def: 0)\n"
           "    -t       outputs temperature log page (0xd)\n"
           "    -T       outputs transport (protocol specific port) log "
           "page (0x18)\n"
           "    -u       hex format for undefined/unrecognized fields\n"
           "    -v       increase verbosity\n"
           "    -V       output version string\n"
           "    -x       no initial INQUIRY output (twice: no INQUIRY call)\n"
           "    -X       open DEVICE read-only (def: first read-write then "
           "if fails\n"
           "             try open again with read-only)\n"
           "    -?       output this usage message\n\n"
           "Performs a SCSI LOG SENSE (or LOG SELECT) command\n");
}

/* Return vendor product mask given vendor product number */
static int
get_vp_mask(int vpn)
{
    if (vpn < 0)
        return 0;
    else
        return (vpn >= (32 - MVP_OFFSET)) ?  OVP_ALL :
                                             (1 << (vpn + MVP_OFFSET));
}

static int
asort_comp(const void * lp, const void * rp)
{
    const struct log_elem * const * lepp =
                (const struct log_elem * const *)lp;
    const struct log_elem * const * repp =
                (const struct log_elem * const *)rp;

    return strcmp((*lepp)->acron, (*repp)->acron);
}

static void
enumerate_helper(const struct log_elem * lep, bool first,
                 const struct opts_t * op)
{
    char b[80];
    char bb[80];
    const char * cp;
    bool vendor_lpage = ! (MVP_STD & lep->flags);

    if (first) {
        if (1 == op->verbose) {
            printf("acronym   pg[,spg]        name\n");
            printf("===============================================\n");
        } else if (2 == op->verbose) {
            printf("acronym   pg[,spg]        pdt   name\n");
            printf("===================================================\n");
        }
    }
    if ((0 == (op->do_enumerate % 2)) && vendor_lpage)
        return;     /* if do_enumerate is even then skip vendor pages */
    else if ((! op->filter_given) || (-1 == op->filter))
        ;           /* otherwise enumerate all lpages if no --filter= */
    else if (-2 == op->filter) {   /* skip non-SPC pages */
        if (lep->pdt >= 0)
            return;
    } else if (-10 == op->filter) {   /* skip non-disk like pages */
        if (sg_lib_pdt_decay(lep->pdt) != 0)
            return;
    } else if (-11 == op->filter) {   /* skip tape like device pages */
        if (sg_lib_pdt_decay(lep->pdt) != 1)
            return;
    } else if ((op->filter >= 0) && (op->filter <= 0x1f)) {
        if ((lep->pdt >= 0) && (lep->pdt != op->filter) &&
            (lep->pdt != sg_lib_pdt_decay(op->filter)))
            return;
    }
    if (op->vend_prod_num >= 0) {
        if (! (lep->flags & get_vp_mask(op->vend_prod_num)))
            return;
    }
    if (op->deduced_vpn >= 0) {
        if (! (lep->flags & get_vp_mask(op->deduced_vpn)))
            return;
    }
    if (lep->subpg_high > 0)
        snprintf(b, sizeof(b), "0x%x,0x%x->0x%x", lep->pg_code,
                 lep->subpg_code, lep->subpg_high);
    else if (lep->subpg_code > 0)
        snprintf(b, sizeof(b), "0x%x,0x%x", lep->pg_code,
                 lep->subpg_code);
    else
        snprintf(b, sizeof(b), "0x%x", lep->pg_code);
    snprintf(bb, sizeof(bb), "%-16s", b);
    cp = (op->verbose && (! lep->show_pagep)) ? " [hex only]" : "";
    if (op->verbose > 1) {
        if (lep->pdt < 0)
            printf("  %-8s%s-     %s%s\n", lep->acron, bb, lep->name, cp);
        else
            printf("  %-8s%s0x%02x  %s%s\n", lep->acron, bb, lep->pdt,
                   lep->name, cp);
    } else
        printf("  %-8s%s%s%s\n", lep->acron, bb, lep->name, cp);
}

static void
enumerate_pages(const struct opts_t * op)
{
    int j;
    struct log_elem * lep;
    struct log_elem ** lep_arr;

    if (op->do_enumerate < 3) { /* -e, -ee: sort by acronym */
        int k;
        struct log_elem ** lepp;

        for (k = 0, lep = log_arr; lep->pg_code >=0; ++lep, ++k)
            ;
        ++k;
        lep_arr = (struct log_elem **)calloc(k, sizeof(struct log_elem *));
        if (NULL == lep_arr) {
            pr2serr("%s: out of memory\n", __func__);
            return;
        }
        for (k = 0, lep = log_arr; lep->pg_code >=0; ++lep, ++k)
            lep_arr[k] = lep;
        lep_arr[k++] = lep;     /* put sentinel on end */
        qsort(lep_arr, k, sizeof(struct log_elem *), asort_comp);
        printf("Known log pages in acronym order:\n");
        for (lepp = lep_arr, j = 0; (*lepp)->pg_code >=0; ++lepp, ++j)
            enumerate_helper(*lepp, (0 == j), op);
        free(lep_arr);
    } else {    /* -eee, -eeee numeric sort (as per table) */
        printf("Known log pages in numerical order:\n");
        for (lep = log_arr, j = 0; lep->pg_code >=0; ++lep, ++j)
            enumerate_helper(lep, (0 == j), op);
    }
}

static const struct log_elem *
acron_search(const char * acron)
{
    const struct log_elem * lep;

    for (lep = log_arr; lep->pg_code >=0; ++lep) {
        if (0 == strcmp(acron, lep->acron))
            return lep;
    }
    return NULL;
}

static int
find_vpn_by_acron(const char * vp_ap)
{
    const struct vp_name_t * vpp;

    for (vpp = vp_arr; vpp->acron; ++vpp) {
        size_t k;
        size_t len = strlen(vpp->acron);

        for (k = 0; k < len; ++k) {
            if (tolower((uint8_t)vp_ap[k]) != (uint8_t)vpp->acron[k])
                break;
        }
        if (k < len)
            continue;
        return vpp->vend_prod_num;
    }
    return VP_NONE;
}

/* Find vendor product number using T10 VENDOR and PRODUCT ID fields in a
   INQUIRY response. */
static int
find_vpn_by_inquiry(void)
{
    size_t len;
    size_t t10_v_len = strlen(t10_vendor_str);
    size_t t10_p_len = strlen(t10_product_str);
    const struct vp_name_t * vpp;

    if ((0 == t10_v_len) && (0 == t10_p_len))
        return VP_NONE;
    for (vpp = vp_arr; vpp->acron; ++vpp) {
        bool matched = false;

        if (vpp->t10_vendorp && (t10_v_len > 0)) {
            len = strlen(vpp->t10_vendorp);
            len = (len > t10_v_len) ? t10_v_len : len;
            if (strncmp(vpp->t10_vendorp, t10_vendor_str, len))
                continue;
            matched = true;
        }
        if (vpp->t10_productp && (t10_p_len > 0)) {
            len = strlen(vpp->t10_productp);
            len = (len > t10_p_len) ? t10_p_len : len;
            if (strncmp(vpp->t10_productp, t10_product_str, len))
                continue;
            matched = true;
        }
        if (matched)
            return vpp->vend_prod_num;
    }
    return VP_NONE;
}

static void
enumerate_vp(void)
{
    const struct vp_name_t * vpp;
    bool seen = false;

    for (vpp = vp_arr; vpp->acron; ++vpp) {
        if (vpp->name) {
            if (! seen) {
                printf("\nVendor/product identifiers:\n");
                seen = true;
            }
            printf("  %-10s %d      %s\n", vpp->acron,
                   vpp->vend_prod_num, vpp->name);
        }
    }
}

static const struct log_elem *
pg_subpg_pdt_search(int pg_code, int subpg_code, int pdt, int vpn)
{
    const struct log_elem * lep;
    int d_pdt;
    int vp_mask = get_vp_mask(vpn);

    d_pdt = sg_lib_pdt_decay(pdt);
    for (lep = log_arr; lep->pg_code >=0; ++lep) {
        if (pg_code == lep->pg_code) {
            if (subpg_code == lep->subpg_code) {
                if ((MVP_STD & lep->flags) || (0 == vp_mask) ||
                    (vp_mask & lep->flags))
                    ;
                else
                    continue;
                if ((lep->pdt < 0) || (pdt == lep->pdt) || (pdt < 0))
                    return lep;
                else if (d_pdt == lep->pdt)
                    return lep;
                else if (pdt == sg_lib_pdt_decay(lep->pdt))
                    return lep;
            } else if ((lep->subpg_high > 0) &&
                     (subpg_code > lep->subpg_code) &&
                     (subpg_code <= lep->subpg_high))
                return lep;
        }
    }
    return NULL;
}

static void
js_snakenv_ihexstr_nex(sgj_state * jsp, sgj_opaque_p jop,
                       const char * conv2sname, int64_t val_i,
                       bool hex_as_well, const char * str_name,
                       const char * val_s, const char * nex_s)
{

    if ((NULL == jsp) || (NULL == jop))
        return;
    if (sgj_is_snake_name(conv2sname))
        sgj_js_nv_ihexstr_nex(jsp, jop, conv2sname, val_i, hex_as_well,
                              str_name, val_s, nex_s);
    else {
        char b[128];

        sgj_convert_to_snake_name(conv2sname, b, sizeof(b));
        sgj_js_nv_ihexstr_nex(jsp, jop, b, val_i, hex_as_well, str_name,
                              val_s, nex_s);
    }
}

static void
usage_for(int hval, const struct opts_t * op)
{
    if (op->opt_new)
        usage(hval);
    else
        usage_old();
}

/* Processes command line options according to new option format. Returns
 * 0 is ok, else SG_LIB_SYNTAX_ERROR is returned. */
static int
new_parse_cmd_line(struct opts_t * op, int argc, char * argv[])
{
    while (1) {
        int c, n;
        int option_index = 0;

        c = getopt_long(argc, argv, "aAbc:D:eEf:hHi:j::lLm:M:nNOp:P:qQrRsStT"
                        "uvVxX", long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
        case 'a':
            ++op->do_all;
            break;
        case 'A':
            op->do_all += 2;
            break;
        case 'b':
            ++op->do_brief;
            break;
        case 'c':
            n = sg_get_num(optarg);
            if ((n < 0) || (n > 3)) {
                pr2serr("bad argument to '--control='\n");
                usage(2);
                return SG_LIB_SYNTAX_ERROR;
            }
            op->page_control = n;
            break;
        case 'D':
            if (0 == memcmp("-1", optarg, 3))
                op->dev_pdt = -1;
            else {
                n = sg_get_num(optarg);
                if ((n < 0) || (n > 31)) {
                    pr2serr("bad argument to '--pdt='\n");
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->dev_pdt = n;
            }
            break;
        case 'e':
            ++op->do_enumerate;
            break;
        case 'E':
            op->exclude_vendor = true;
            break;
        case 'f':
            if ('-' == optarg[0]) {
                n = sg_get_num(optarg + 1);
                if ((n < 0) || (n > 0x30)) {
                    pr2serr("bad negated argument to '--filter='\n");
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->filter = -n;
            } else {
                n = sg_get_num(optarg);
                if ((n < 0) || (n > 0xffff)) {
                    pr2serr("bad argument to '--filter='\n");
                    usage(1);
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->filter = n;
            }
            op->filter_given = true;
            break;
        case 'h':
        case '?':
            ++op->do_help;
            break;
        case 'H':
            ++op->do_hex;
            break;
        case 'i':
            op->in_fn = optarg;
            break;
        case 'j':
           if (! sgj_init_state(&op->json_st, optarg)) {
                int bad_char = op->json_st.first_bad_char;
                char e[1500];

                if (bad_char) {
                    pr2serr("bad argument to --json= option, unrecognized "
                            "character '%c'\n\n", bad_char);
                }
                sg_json_usage(0, e, sizeof(e));
                pr2serr("%s", e);
                return SG_LIB_SYNTAX_ERROR;
            }
            break;
        case 'l':
            ++op->do_list;
            break;
        case 'L':
            op->do_list += 2;
            break;
        case 'm':
            n = sg_get_num(optarg);
            if ((n < 0) || (1 == n)) {
                pr2serr("bad argument to '--maxlen=', from 2 and up "
                        "expected\n");
                usage(2);
                return SG_LIB_SYNTAX_ERROR;
            } else if (n < 4) {
                pr2serr("Warning: setting '--maxlen' to 4\n");
                n = 4;
            }
            op->maxlen = n;
            op->maxlen_given = true;
            break;
        case 'M':
            if (op->vend_prod) {
                pr2serr("only one '--vendor=' option permitted\n");
                usage(2);
                return SG_LIB_SYNTAX_ERROR;
            } else
                op->vend_prod = optarg;
            break;
        case 'n':
            op->do_name = true;
            break;
        case 'N':
            break;      /* ignore */
        case 'O':
            op->opt_new = false;
            return 0;
        case 'p':
            op->pg_arg = optarg;
            break;
        case 'P':
            n = sg_get_num(optarg);
            if (n < 0) {
                pr2serr("bad argument to '--paramp='\n");
                usage(2);
                return SG_LIB_SYNTAX_ERROR;
            }
            op->paramp = n;
            break;
        case 'q':
            op->do_pcb = true;
            break;
        case 'Q':       /* N.B. PPC bit obsoleted in SPC-4 rev 18 */
            op->do_ppc = true;
            break;
        case 'r':
            op->do_raw = true;
            break;
        case 'R':
            op->do_pcreset = true;
            op->do_select = true;
            break;
        case 's':
            op->do_sp = true;
            break;
        case 'S':
            op->do_select = true;
            break;
        case 't':
            op->do_temperature = true;
            break;
        case 'T':
            op->do_transport = true;
            break;
        case 'u':
            ++op->undefined_hex;
            break;
        case 'v':
            op->verbose_given = true;
            ++op->verbose;
            break;
        case 'V':
            op->version_given = true;
            break;
        case 'x':
            ++op->no_inq;
            break;
        case 'X':
            op->o_readonly = true;
            break;
        default:
            pr2serr("unrecognised option code %c [0x%x]\n", c, c);
            if (op->do_help)
                break;
            usage(1);
            return SG_LIB_SYNTAX_ERROR;
        }
    }
    if (optind < argc) {
        if (NULL == op->device_name) {
            op->device_name = argv[optind];
            ++optind;
        }
        if (optind < argc) {
            for (; optind < argc; ++optind)
                pr2serr("Unexpected extra argument: %s\n", argv[optind]);
            usage(1);
            return SG_LIB_SYNTAX_ERROR;
        }
    }
    return 0;
}

/* Processes command line options according to old option format. Returns
 * 0 is ok, else SG_LIB_SYNTAX_ERROR is returned. */
static int
old_parse_cmd_line(struct opts_t * op, int argc, char * argv[])
{
    bool jmp_out;
    int k, num, n;
    unsigned int u, uu;
    const char * cp;

    for (k = 1; k < argc; ++k) {
        int plen;

        cp = argv[k];
        plen = strlen(cp);
        if (plen <= 0)
            continue;
        if ('-' == *cp) {
            for (--plen, ++cp, jmp_out = false; plen > 0; --plen, ++cp) {
                switch (*cp) {
                case 'a':
                    ++op->do_all;
                    break;
                case 'A':
                    op->do_all += 2;
                    break;
                case 'b':
                    ++op->do_brief;
                    break;
                case 'e':
                    ++op->do_enumerate;
                    break;
                case 'E':
                    op->exclude_vendor = true;
                    break;
                case 'h':
                case 'H':
                    ++op->do_hex;
                    break;
                case 'l':
                    ++op->do_list;
                    break;
                case 'L':
                    op->do_list += 2;
                    break;
                case 'n':
                    op->do_name = true;
                    break;
                case 'N':
                    op->opt_new = true;
                    return 0;
                case 'O':
                    break;
                case 'r':
                    op->do_pcreset = true;
                    op->do_select = true;
                    break;
                case 't':
                    op->do_temperature = true;
                    break;
                case 'T':
                    op->do_transport = true;
                    break;
                case 'u':
                    ++op->undefined_hex;
                    break;
                case 'v':
                    op->verbose_given = true;
                    ++op->verbose;
                    break;
                case 'V':
                    op->version_given = true;
                    break;
                case 'x':
                    ++op->no_inq;
                    break;
                case 'X':
                    op->o_readonly = true;
                    break;
                case '?':
                    ++op->do_help;
                    break;
                case '-':
                    ++cp;
                    jmp_out = true;
                    break;
                default:
                    jmp_out = true;
                    break;
                }
                if (jmp_out)
                    break;
            }
            if (plen <= 0)
                continue;
            if (0 == strncmp("c=", cp, 2)) {
                num = sscanf(cp + 2, "%6x", &u);
                if ((1 != num) || (u > 3)) {
                    pr2serr("Bad page control after '-c=' option [0..3]\n");
                    usage_old();
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->page_control = u;
            } else if (0 == strncmp("D=", cp, 2)) {
                n = sg_get_num(cp + 2);
                if ((n < 0) || (n > 31)) {
                    pr2serr("Bad argument after '-D=' option\n");
                    usage_old();
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->dev_pdt = n;
            } else if (0 == strncmp("f=", cp, 2)) {
                n = sg_get_num(cp + 2);
                if ((n < 0) || (n > 0xffff)) {
                    pr2serr("Bad argument after '-f=' option\n");
                    usage_old();
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->filter = n;
                op->filter_given = true;
            } else if (0 == strncmp("i=", cp, 2))
                op->in_fn = cp + 2;
            else if (0 == strncmp("m=", cp, 2)) {
                num = sscanf(cp + 2, "%8d", &n);
                if ((1 != num) || (n < 0)) {
                    pr2serr("Bad maximum response length after '-m=' "
                            "option\n");
                    usage_old();
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->maxlen_given = true;
                op->maxlen = n;
            } else if (0 == strncmp("M=", cp, 2)) {
                if (op->vend_prod) {
                    pr2serr("only one '-M=' option permitted\n");
                    usage(2);
                    return SG_LIB_SYNTAX_ERROR;
                } else
                    op->vend_prod = cp + 2;
            } else if (0 == strncmp("p=", cp, 2)) {
                const char * ccp = cp + 2;
                const struct log_elem * lep;

                if (isalpha((uint8_t)ccp[0])) {
                    char * xp;
                    char b[80];

                    if (strlen(ccp) >= (sizeof(b) - 1)) {
                        pr2serr("argument to '-p=' is too long\n");
                        return SG_LIB_SYNTAX_ERROR;
                    }
                    strcpy(b, ccp);
                    xp = (char *)strchr(b, ',');
                    if (xp)
                        *xp = '\0';
                    lep = acron_search(b);
                    if (NULL == lep) {
                        pr2serr("bad argument to '--page=' no acronyn match "
                                "to '%s'\n", b);
                        pr2serr("  Try using '-e' or'-ee' to see available "
                                "acronyns\n");
                        return SG_LIB_SYNTAX_ERROR;
                    }
                    op->lep = lep;
                    op->pg_code = lep->pg_code;
                    if (xp) {
                        n = sg_get_num_nomult(xp + 1);
                        if ((n < 0) || (n > 255)) {
                            pr2serr("Bad second value in argument to "
                                    "'--page='\n");
                            return SG_LIB_SYNTAX_ERROR;
                        }
                        op->subpg_code = n;
                    } else
                        op->subpg_code = lep->subpg_code;
                } else {
                    /* numeric arg: either 'pg_num' or 'pg_num,subpg_num' */
                    if (NULL == strchr(cp + 2, ',')) {
                        num = sscanf(cp + 2, "%6x", &u);
                        if ((1 != num) || (u > 63)) {
                            pr2serr("Bad page code value after '-p=' "
                                    "option\n");
                            usage_old();
                            return SG_LIB_SYNTAX_ERROR;
                        }
                        op->pg_code = u;
                    } else if (2 == sscanf(cp + 2, "%4x,%4x", &u, &uu)) {
                        if (uu > 255) {
                            pr2serr("Bad sub page code value after '-p=' "
                                    "option\n");
                            usage_old();
                            return SG_LIB_SYNTAX_ERROR;
                        }
                        op->pg_code = u;
                        op->subpg_code = uu;
                    } else {
                        pr2serr("Bad page code, subpage code sequence after "
                                "'-p=' option\n");
                        usage_old();
                        return SG_LIB_SYNTAX_ERROR;
                    }
                }
            } else if (0 == strncmp("paramp=", cp, 7)) {
                num = sscanf(cp + 7, "%8x", &u);
                if ((1 != num) || (u > 0xffff)) {
                    pr2serr("Bad parameter pointer after '-paramp=' "
                            "option\n");
                    usage_old();
                    return SG_LIB_SYNTAX_ERROR;
                }
                op->paramp = u;
            } else if (0 == strncmp("pcb", cp, 3))
                op->do_pcb = true;
            else if (0 == strncmp("ppc", cp, 3))
                op->do_ppc = true;
            else if (0 == strncmp("select", cp, 6))
                op->do_select = true;
            else if (0 == strncmp("sp", cp, 2))
                op->do_sp = true;
            else if (0 == strncmp("old", cp, 3))
                ;
            else if (jmp_out) {
                pr2serr("Unrecognized option: %s\n", cp);
                usage_old();
                return SG_LIB_SYNTAX_ERROR;
            }
        } else if (0 == op->device_name)
            op->device_name = cp;
        else {
            pr2serr("too many arguments, got: %s, not expecting: %s\n",
                    op->device_name, cp);
            usage_old();
            return SG_LIB_SYNTAX_ERROR;
        }
    }
    return 0;
}

/* Process command line options. First check using new option format unless
 * the SG3_UTILS_OLD_OPTS environment variable is defined which causes the
 * old option format to be checked first. Both new and old format can be
 * countermanded by a '-O' and '-N' options respectively. As soon as either
 * of these options is detected (when processing the other format), processing
 * stops and is restarted using the other format. Clear? */
static int
parse_cmd_line(struct opts_t * op, int argc, char * argv[])
{
    int res;
    char * cp;

    cp = getenv("SG3_UTILS_OLD_OPTS");
    if (cp) {
        op->opt_new = false;
        res = old_parse_cmd_line(op, argc, argv);
        if ((0 == res) && op->opt_new)
            res = new_parse_cmd_line(op, argc, argv);
    } else {
        op->opt_new = true;
        res = new_parse_cmd_line(op, argc, argv);
        if ((0 == res) && (0 == op->opt_new))
            res = old_parse_cmd_line(op, argc, argv);
    }
    return res;
}

static void
dStrRaw(const uint8_t * str, int len)
{
    int k;

    for (k = 0; k < len; ++k)
        printf("%c", str[k]);
}

/* Returns 'xp' with "unknown" if all bits set; otherwise decoded (big endian)
 * number in 'xp'. Number rendered in decimal if in_hex=false otherwise in
 * hex with leading '0x' prepended. */
static char *
num_or_unknown(const uint8_t * xp, int num_bytes /* max is 8 */, bool in_hex,
               char * b, int blen)
{
    if (sg_all_ffs(xp, num_bytes))
        snprintf(b, blen, "%s", unknown_s);
    else {
        uint64_t num = sg_get_unaligned_be(num_bytes, xp);

        if (in_hex)
            snprintf(b, blen, "0x%" PRIx64, num);
        else
            snprintf(b, blen, "%" PRIu64, num);
    }
    return b;
}

/* Call LOG SENSE twice: the first time ask for 4 byte response to determine
   actual length of response; then a second time requesting the
   min(actual_len, mx_resp_len) bytes. If the calculated length for the
   second fetch is odd then it is incremented (perhaps should be made modulo
   4 in the future for SAS). Returns 0 if ok, SG_LIB_CAT_INVALID_OP for
   log_sense not supported, SG_LIB_CAT_ILLEGAL_REQ for bad field in log sense
   command, SG_LIB_CAT_NOT_READY, SG_LIB_CAT_UNIT_ATTENTION,
   SG_LIB_CAT_ABORTED_COMMAND and -1 for other errors. */
static int
do_logs(int sg_fd, uint8_t * resp, int mx_resp_len,
        const struct opts_t * op)
{
    int calc_len, request_len, res, resid, vb;

#ifdef SG_LIB_WIN32
#ifdef SG_LIB_WIN32_DIRECT
    if (! win32_spt_init_state) {
        if (win32_spt_curr_state) {
            if (mx_resp_len < 16384) {
                scsi_pt_win32_direct(0);
                win32_spt_curr_state = false;
            }
        } else {
            if (mx_resp_len >= 16384) {
                scsi_pt_win32_direct(SG_LIB_WIN32_DIRECT /* SPT direct */);
                win32_spt_curr_state = true;
            }
        }
    }
#endif
#endif
    memset(resp, 0, mx_resp_len);
    vb = op->verbose;
    if (op->maxlen > 1)
        request_len = mx_resp_len;
    else {
        request_len = LOG_SENSE_PROBE_ALLOC_LEN;
        if ((res = sg_ll_log_sense_v2(sg_fd, op->do_ppc, op->do_sp,
                                      op->page_control, op->pg_code,
                                      op->subpg_code, op->paramp,
                                      resp, request_len, LOG_SENSE_DEF_TIMEOUT,
                                      &resid, true /* noisy */, vb)))
            return res;
        if (resid > 0) {
            res = SG_LIB_WILD_RESID;
            goto resid_err;
        }
        calc_len = sg_get_unaligned_be16(resp + 2) + 4;
        if ((! op->do_raw) && (vb > 1)) {
            pr2serr("  Log sense (find length) response:\n");
            hex2stderr(resp, LOG_SENSE_PROBE_ALLOC_LEN, 1);
            pr2serr("  hence calculated response length=%d\n", calc_len);
        }
        if (op->pg_code != (0x3f & resp[0])) {
            if (vb)
                pr2serr("Page code does not appear in first byte of "
                        "response so it's suspect\n");
            if (calc_len > 0x40) {
                calc_len = 0x40;
                if (vb)
                    pr2serr("Trim response length to 64 bytes due to "
                            "suspect response format\n");
            }
        }
        /* Some HBAs don't like odd transfer lengths */
        if (calc_len % 2)
            calc_len += 1;
        if (calc_len > mx_resp_len)
            calc_len = mx_resp_len;
        request_len = calc_len;
    }
    if ((res = sg_ll_log_sense_v2(sg_fd, op->do_ppc, op->do_sp,
                                  op->page_control, op->pg_code,
                                  op->subpg_code, op->paramp,
                                  resp, request_len,
                                  LOG_SENSE_DEF_TIMEOUT, &resid,
                                  true /* noisy */, vb)))
        return res;
    if (resid > 0) {
        request_len -= resid;
        if (request_len < 4) {
            request_len += resid;
            res = SG_LIB_WILD_RESID;
            goto resid_err;
        }
    }
    if ((! op->do_raw) && (vb > 1)) {
        pr2serr("  Log sense response:\n");
        hex2stderr(resp, request_len, 1);
    }
    return 0;
resid_err:
    pr2serr("%s: request_len=%d, resid=%d, problems\n", __func__, request_len,
            resid);
    request_len -= resid;
    if ((request_len > 0) && (! op->do_raw) && (vb > 1)) {
        pr2serr("  Log sense (resid_err) response:\n");
        hex2stderr(resp, request_len, 1);
    }
    return res;
}

sgj_opaque_p
sg_log_js_hdr(sgj_state * jsp, sgj_opaque_p jop, const char * name,
              const uint8_t * log_hdrp)
{
    bool ds = !! (log_hdrp[0] & 0x80);
    bool spf = !! (log_hdrp[0] & 0x40);
    int pg = log_hdrp[0] & 0x3f;
    int subpg = log_hdrp[1];
    size_t nlen = strlen(name);
    sgj_opaque_p jo2p;
    char b[80];

    if ((nlen < 4) || (0 != strcmp("age", name + nlen - 3))) {
        memcpy(b, name, nlen);
        memcpy(b + nlen, " log page", 10);
        jo2p = sgj_snake_named_subobject_r(jsp, jop, b);
    } else
        jo2p = sgj_snake_named_subobject_r(jsp, jop, name);

    sgj_js_nv_ihex_nex(jsp, jo2p, "ds", (int)ds, false, "Did not Save");
    sgj_js_nv_ihex_nex(jsp, jo2p, "spf", (int)spf, NULL, "SubPage Format");
    sgj_js_nv_ihex(jsp, jo2p, "page_code", pg);
    sgj_js_nv_ihex(jsp, jo2p, "subpage_code", subpg);
    return jo2p;
}



/* DS made obsolete in spc4r03; TMC and ETC made obsolete in spc5r03. */
static char *
get_pcb_str(int pcb, char * outp, int maxoutlen)
{
    char buff[PCB_STR_LEN];
    int n;

    n = sprintf(buff, "du=%d [ds=%d] tsd=%d [etc=%d] ", ((pcb & 0x80) ? 1 : 0),
                ((pcb & 0x40) ? 1 : 0), ((pcb & 0x20) ? 1 : 0),
                ((pcb & 0x10) ? 1 : 0));
    if (pcb & 0x10)
        n += sprintf(buff + n, "[tmc=%d] ", ((pcb & 0xc) >> 2));
#if 1
    n += sprintf(buff + n, "format+linking=%d  [0x%.2x]", pcb & 3,
                 pcb);
#else
    if (pcb & 0x1)
        n += sprintf(buff + n, "lbin=%d ", ((pcb & 0x2) >> 1));
    n += sprintf(buff + n, "lp=%d  [0x%.2x]", pcb & 0x1, pcb);
#endif
    if (outp && (n < maxoutlen)) {
        memcpy(outp, buff, n);
        outp[n] = '\0';
    } else if (outp && (maxoutlen > 0))
        outp[0] = '\0';
    return outp;
}

static void
js_pcb(sgj_state * jsp, sgj_opaque_p jop, int pcb)
{
    sgj_opaque_p jo2p = sgj_snake_named_subobject_r(jsp, jop,
                                                    "parameter_control_byte");

    sgj_js_nv_ihex_nex(jsp, jo2p, "du", (pcb & 0x80) ? 1 : 0, false,
                       "Disable Update");
    sgj_js_nv_ihex_nex(jsp, jo2p, "ds", (pcb & 0x40) ? 1 : 0, false,
                       "Disable Save [obsolete]");
    sgj_js_nv_ihex_nex(jsp, jo2p, "tsd", (pcb & 0x20) ? 1 : 0, false,
                       "Target Save Disable");
    sgj_js_nv_ihex_nex(jsp, jo2p, "etc", (pcb & 0x10) ? 1 : 0, false,
                       "Enable Threshold Comparison [obsolete]");
    sgj_js_nv_ihex_nex(jsp, jo2p, "tmc", (pcb & 0xc) >> 2, false,
                       "Threshold Met Criteria [obsolete]");
    sgj_js_nv_ihex_nex(jsp, jo2p, "format_and_linking", pcb & 0x3, false,
                       NULL);
}

/* SUPP_PAGES_LPAGE [0x0,0x0] <sp> */
static bool
show_supported_pgs_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    int num, k;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p, jo3p;
    sgj_opaque_p jap = NULL;
    char b[64];
    static const char * slpgs = "Supported log pages";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x0]:\n", slpgs);  /* introduced in: SPC-2 */
    num = len - 4;
    bp = &resp[0] + 4;
    if ((op->do_hex > 0) || op->do_raw) {
        if (op->do_raw)
            dStrRaw(resp, len);
        else
            hex2stdout(resp, len, op->dstrhex_no_ascii);
        return true;
    }
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, slpgs, resp);
        jap = sgj_named_subarray_r(jsp, jo2p, "supported_pages_list");
    }

    for (k = 0; k < num; ++k) {
        int pg_code = bp[k] & 0x3f;
        const struct log_elem * lep;

        snprintf(b, sizeof(b) - 1, "  0x%02x        ", pg_code);
        lep = pg_subpg_pdt_search(pg_code, 0, op->dev_pdt, -1);
        if (lep) {
            if (op->do_brief > 1)
                sgj_pr_hr(jsp, "    %s\n", lep->name);
            else if (op->do_brief)
                sgj_pr_hr(jsp, "%s%s\n", b, lep->name);
            else
                sgj_pr_hr(jsp, "%s%s [%s]\n", b, lep->name, lep->acron);
        } else
            sgj_pr_hr(jsp, "%s\n", b);
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            sgj_js_nv_ihex(jsp, jo3p, "page_code", pg_code);
            sgj_js_nv_s(jsp, jo3p, "name", lep ? lep->name : unknown_s);
            sgj_js_nv_s(jsp, jo3p, "acronym", lep ? lep->acron : unknown_s);
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        }
    }
    return true;
}

/* SUPP_PAGES_LPAGE,SUPP_SPGS_SUBPG [0x0,0xff] <ssp> or all subpages of a
 * given page code: [<pg_code>,0xff] where <pg_code> > 0 */
static bool
show_supported_pgs_sub_page(const uint8_t * resp, int len,
                            struct opts_t * op, sgj_opaque_p jop)
{
    int num, k;
    const uint8_t * bp;
    const struct log_elem * lep = NULL;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p, jo3p;
    sgj_opaque_p jap = NULL;
    char b[64];
    static const char * slpass = "Supported log pages and subpages";
    static const char * sss = "Supported subpages";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (op->pg_code > 0)
            sgj_pr_hr(jsp, "%s  [0x%x, 0xff]:\n", sss, op->pg_code);
        else
            sgj_pr_hr(jsp, "%s  [0x0, 0xff]:\n", sss);
    }
    num = len - 4;
    bp = &resp[0] + 4;
    if ((op->do_hex > 0) || op->do_raw) {
        if (op->do_raw)
            dStrRaw(resp, len);
        else
            hex2stdout(resp, len, op->dstrhex_no_ascii);
        return true;
    }
    if (jsp->pr_as_json) {
        if (op->pg_code > 0) {
            jo2p = sg_log_js_hdr(jsp, jop, sss, resp);
            jap = sgj_named_subarray_r(jsp, jo2p,
                                       "supported_subpage_descriptors");
        } else {
            jo2p = sg_log_js_hdr(jsp, jop, slpass, resp);
            jap = sgj_named_subarray_r(jsp, jo2p,
                               "supported_page_subpage_descriptors");
        }
    }

    for (k = 0; k < num; k += 2) {
        bool pr_name = true;
        int pg_code = bp[k];
        int subpg_code = bp[k + 1];

        /* formerly ignored [pg, 0xff] when pg > 0, don't know why */
        if (NOT_SPG_SUBPG == subpg_code)
            snprintf(b, sizeof(b) - 1, "  0x%02x        ", pg_code);
        else
            snprintf(b, sizeof(b) - 1, "  0x%02x,0x%02x   ", pg_code,
                     subpg_code);
        if ((pg_code > 0) && (subpg_code == 0xff)) {
            sgj_pr_hr(jsp, "%s\n", b);
            pr_name = false;
        } else {
            lep = pg_subpg_pdt_search(pg_code, subpg_code, op->dev_pdt, -1);
            if (lep) {
                if (op->do_brief > 1)
                    sgj_pr_hr(jsp, "    %s\n", lep->name);
                else if (op->do_brief)
                    sgj_pr_hr(jsp, "%s%s\n", b, lep->name);
                else
                    sgj_pr_hr(jsp, "%s%s [%s]\n", b, lep->name, lep->acron);
            } else
                sgj_pr_hr(jsp, "%s\n", b);
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            sgj_js_nv_ihex(jsp, jo3p, "page_code", pg_code);
            sgj_js_nv_ihex(jsp, jo3p, "subpage_code", subpg_code);
            if (pr_name) {
                sgj_js_nv_s(jsp, jo3p, "name", lep ? lep->name : unknown_s);
                sgj_js_nv_s(jsp, jo3p, "acronym", lep ? lep->acron :
                                                        unknown_s);
            }
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        }
    }
    return true;
}

/* BUFF_OVER_UNDER_LPAGE [0x1] <bou>  introduced: SPC-2 */
static bool
show_buffer_over_under_run_page(const uint8_t * resp, int len,
                                struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    uint64_t count;
    const uint8_t * bp;
    const char * cp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    static const char * bourlp = "Buffer over-run/under-run log page";
    static const char * orurc = "over_run_under_run_counter";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x1]\n", bourlp);
    num = len - 4;
    bp = &resp[0] + 4;
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, bourlp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                        "buffer_over_run_under_run_log_parameters");
    }
    while (num > 3) {
        cp = NULL;
        pl = bp[3] + 4;
        count = (pl > 4) ? sg_get_unaligned_be(pl - 4, bp + 4) : 0;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        switch (pc) {
        case 0x0:
            cp = "under-run";
            break;
        case 0x1:
            cp = "over-run";
            break;
        case 0x2:
            cp = "service delivery subsystem busy, under-run";
            break;
        case 0x3:
            cp = "service delivery subsystem busy, over-run";
            break;
        case 0x4:
            cp = "transfer too slow, under-run";
            break;
        case 0x5:
            cp = "transfer too slow, over-run";
            break;
        case 0x20:
            cp = "command, under-run";
            break;
        case 0x21:
            cp = "command, over-run";
            break;
        case 0x22:
            cp = "command, service delivery subsystem busy, under-run";
            break;
        case 0x23:
            cp = "command, service delivery subsystem busy, over-run";
            break;
        case 0x24:
            cp = "command, transfer too slow, under-run";
            break;
        case 0x25:
            cp = "command, transfer too slow, over-run";
            break;
        case 0x40:
            cp = "I_T nexus, under-run";
            break;
        case 0x41:
            cp = "I_T nexus, over-run";
            break;
        case 0x42:
            cp = "I_T nexus, service delivery subsystem busy, under-run";
            break;
        case 0x43:
            cp = "I_T nexus, service delivery subsystem busy, over-run";
            break;
        case 0x44:
            cp = "I_T nexus, transfer too slow, under-run";
            break;
        case 0x45:
            cp = "I_T nexus, transfer too slow, over-run";
            break;
        case 0x80:
            cp = "time, under-run";
            break;
        case 0x81:
            cp = "time, over-run";
            break;
        case 0x82:
            cp = "time, service delivery subsystem busy, under-run";
            break;
        case 0x83:
            cp = "time, service delivery subsystem busy, over-run";
            break;
        case 0x84:
            cp = "time, transfer too slow, under-run";
            break;
        case 0x85:
            cp = "time, transfer too slow, over-run";
            break;
        default:
            pr2serr("  undefined parameter code [0x%x], count = %"
                    PRIu64 "\n", pc, count);
            break;
        }
            sgj_js_nv_ihex(jsp, jo3p, param_c_snake, pc);
        sgj_pr_hr(jsp, "  %s=0x%x\n", param_c, pc);
        if (cp) {
            sgj_pr_hr(jsp, "    %s = %" PRIu64 "\n", cp, count);
            js_snakenv_ihexstr_nex(jsp, jo3p, param_c, pc, true,
                                   NULL, cp, NULL);
            sgj_js_nv_ihex(jsp, jo3p, orurc, count);
        } else
            sgj_pr_hr(jsp, "    counter = %" PRIu64 "\n", count);

        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* WRITE_ERR_LPAGE; READ_ERR_LPAGE; READ_REV_ERR_LPAGE; VERIFY_ERR_LPAGE */
/* [0x2, 0x3, 0x4, 0x5] <we, re, rre, ve>  introduced: SPC-3 */
static bool
show_error_counter_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    bool skip_out = false;
    bool evsm_output = false;
    int n, num, pl, pc, pg_code;
    uint64_t val;
    const uint8_t * bp;
    const char * pg_cp = NULL;
    const char * par_cp = NULL;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[128] SG_C_CPP_ZERO_INIT;
    char d[128];
    char e[64];
    static const char * wec = "Write error counter";
    static const char * rec = "Read error counter";
    static const char * rrec = "Read reverse error counter";
    static const char * vec = "Verify error counter";

    pg_code = resp[0] & 0x3f;
    switch(pg_code) {
    case WRITE_ERR_LPAGE:
        pg_cp = wec;
        break;
    case READ_ERR_LPAGE:
        pg_cp = rec;
        break;
    case READ_REV_ERR_LPAGE:
        pg_cp = rrec;
        break;
    case VERIFY_ERR_LPAGE:
        pg_cp = vec;
        break;
    default:
        pr2serr("expecting error counter page, got page = 0x%x\n",
                pg_code);
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s log page  [0x%x]\n", pg_cp, pg_code);
    if (jsp->pr_as_json) {
        n = strlen(pg_cp);
        memcpy(b, pg_cp, n);
        memcpy(b + n, " log", 4);
        n = strlen(b);
        memcpy(b + n, " page", 5);
        jo2p = sg_log_js_hdr(jsp, jop, b, resp);
        memcpy(b + n, " parameters", 11);
        sgj_convert_to_snake_name(b, d, sizeof(d) - 1);
        jap = sgj_named_subarray_r(jsp, jo2p, d);
    }
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        par_cp = NULL;
        switch (pc) {
        case 0:
            par_cp = "Errors corrected without substantial delay";
            break;
        case 1:
            par_cp = "Errors corrected with possible delays";
            break;
        case 2:
            par_cp = "Total rewrites or rereads";
            break;
        case 3:
            par_cp = "Total errors corrected";
            break;
        case 4:
            par_cp = "Total times correction algorithm processed";
            break;
        case 5:
            par_cp = "Total bytes processed";
            break;
        case 6:
            par_cp = "Total uncorrected errors";
            break;
        default:
            if (op->exclude_vendor) {
                skip_out = true;
                if ((op->verbose > 0) && (0 == op->do_brief) &&
                    (! evsm_output)) {
                    evsm_output = true;
                    pr2serr("  Vendor specific parameter(s) being ignored\n");
                }
            } else {
                if (0x8009 == pc)
                    par_cp = "Track following errors [Hitachi]";
                else if (0x8015 == pc)
                    par_cp = "Positioning errors [Hitachi]";
                else {
                    snprintf(e, sizeof(e), "Reserved or vendor specific "
                             "[0x%x]", pc);
                    par_cp = e;
                }
            }
            break;
        }

        if (skip_out)
            skip_out = false;
        else if (par_cp) {
            val = sg_get_unaligned_be(pl - 4, bp + 4);
            if (val > ((uint64_t)1 << 40))
                snprintf(d, sizeof(d), "%" PRIu64 " [%" PRIu64 " TB]",
                         val, (val / (1000UL * 1000 * 1000 * 1000)));
            else if (val > ((uint64_t)1 << 30))
                snprintf(d, sizeof(d), "%" PRIu64 " [%" PRIu64 " GB]",
                         val, (val / (1000UL * 1000 * 1000)));
            else
                snprintf(d, sizeof(d), "%" PRIu64, val);
            sgj_pr_hr(jsp, "  %s = %s\n", par_cp, d);
            if (jsp->pr_as_json) {
                js_snakenv_ihexstr_nex(jsp, jo3p, param_c, pc, true,
                                       NULL, par_cp, NULL);
                sgj_convert_to_snake_name(pg_cp, e, sizeof(e) - 1);
                n = strlen(e);
                memcpy(e + n, "_counter", 9); /* take trailing null */
                sgj_js_nv_ihexstr(jsp, jo3p, e, val, as_s_s, d);
            }
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* NON_MEDIUM_LPAGE [0x6] <nm>  introduced: SPC-2 */
static bool
show_non_medium_error_page(const uint8_t * resp, int len,
                           struct opts_t * op, sgj_opaque_p jop)
{
    bool skip_out = false;
    bool evsm_output = false;
    int num, pl, pc;
    uint64_t count;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[128] SG_C_CPP_ZERO_INIT;
    static const char * nmelp = "Non-medium error log page";
    static const char * nmec = "Non-medium error count";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x6]\n", nmelp);
    num = len - 4;
    bp = &resp[0] + 4;
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, nmelp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                        "non_medium_error_log_parameters");
    }
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        switch (pc) {
        case 0:
            snprintf(b, sizeof(b), "%s", nmec);
            break;
        default:
            if (pc <= 0x7fff)
                snprintf(b, sizeof(b), "  Reserved [0x%x]", pc);
            else {
                if (op->exclude_vendor) {
                    skip_out = true;
                    if ((op->verbose > 0) && (0 == op->do_brief) &&
                        (! evsm_output)) {
                        evsm_output = true;
                        pr2serr("  Vendor specific parameter(s) being "
                                "ignored\n");
                    }
                } else
                    snprintf(b, sizeof(b), "Vendor specific [0x%x]", pc);
            }
            break;
        }
        if (skip_out)
            skip_out = false;
        else {
            count = sg_get_unaligned_be(pl - 4, bp + 4);
            sgj_pr_hr(jsp, "  %s = %" PRIu64 "\n", b, count);
            js_snakenv_ihexstr_nex(jsp, jo3p, param_c, pc, true,
                                   NULL, b, NULL);
            sgj_js_nv_ihex(jsp, jo3p, nmec, count);
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* PCT_LPAGE [0x1a] <pct>  introduced: SPC-4 */
static bool
show_power_condition_transitions_page(const uint8_t * resp, int len,
                                      struct opts_t * op, sgj_opaque_p jop)
{
    bool partial;
    int num, pl, pc;
    uint64_t count;
    const uint8_t * bp;
    const char * cp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[128];
    char bb[64];
    static const char * pctlp = "Power condition transitions log page";
    static const char * att = "Accumulated transitions to";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x1a]\n", pctlp);
    num = len - 4;
    bp = &resp[0] + 4;
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, pctlp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                        "power_condition_transition_log_parameters");
    }

    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        cp = NULL;
        partial = true;
        switch (pc) {
        case 1:
            cp = "active";
            break;
        case 2:
            cp = "idle_a";
            break;
        case 3:
            cp = "idle_b";
            break;
        case 4:
            cp = "idle_c";
            break;
        case 8:
            cp = "standby_z";
            break;
        case 9:
            cp = "standby_y";
            break;
        default:
            snprintf(bb, sizeof(bb), "Reserved [0x%x]", pc);
            cp = bb;
            partial = false;
            break;
        }
        if (partial) {
            snprintf(b, sizeof(b), "%s %s", att, cp);
            cp = b;
        }
        count = sg_get_unaligned_be(pl - 4, bp + 4);
        sgj_pr_hr(jsp, "  %s = %" PRIu64 "\n", cp, count);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json) {
            js_snakenv_ihexstr_nex(jsp, jo3p, cp, count, true,
                                   NULL, NULL, "saturating counter");
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        }
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

static char *
temperature_str(int8_t t, bool reporting, char * b, int blen)
{
    if (-128 == t) {
        if (reporting)
            snprintf(b, blen, "%s", not_avail);
        else
            snprintf(b, blen, "no limit");
    } else
        snprintf(b, blen, "%d C", t);
    return b;
}

static char *
humidity_str(uint8_t h, bool reporting, char * b, int blen)
{
    if (255 == h) {
        if (reporting)
            snprintf(b, blen, "%s", not_avail);
        else
            snprintf(b, blen, "no limit");
    } else if (h <= 100)
        snprintf(b, blen, "%u %%", h);
    else
        snprintf(b, blen, "reserved value [%u]", h);
    return b;
}

/* ENV_REPORTING_SUBPG [0xd,0x1] <env> introduced: SPC-5 (rev 02). "mounted"
 * changed to "other" in spc5r11 */
static bool
show_environmental_reporting_page(const uint8_t * resp, int len,
                                  struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc, blen;
    bool other_valid;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[32];
    static const char * erlp = "Environmental reporting log page";
    static const char * temp = "Temperature";
    static const char * lmaxt = "Lifetime maximum temperature";
    static const char * lmint = "Lifetime minimum temperature";
    static const char * maxtspo = "Maximum temperature since power on";
    static const char * mintspo = "Minimum temperature since power on";
    static const char * maxot = "Maximum other temperature";
    static const char * minot = "Minimum other temperature";
    static const char * relhum = "Relative humidity";
    static const char * lmaxrh = "Lifetime maximum relative humidity";
    static const char * lminrh = "Lifetime minimum relative humidity";
    static const char * maxrhspo = "Maximum relative humidity since power on";
    static const char * minrhspo = "Minimum relative humidity since power on";
    static const char * maxorh = "Maximum other relative humidity";
    static const char * minorh = "Minimum other relative humidity";

    blen = sizeof(b);
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0xd,0x1]\n", erlp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, erlp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                                   "environmental_reporting_log_parameters");
    }
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }
        other_valid = !!(bp[4] & 1);
        if (pc < 0x100) {
            if (pl < 12)  {
                pr2serr("  <<expect parameter 0x%x to be at least 12 bytes "
                        "long, got %d, skip>>\n", pc, pl);
                goto inner;
            }
            sgj_pr_hr(jsp, "  %s=0x%x\n", param_c, pc);
            sgj_js_nv_ihex(jsp, jo3p, param_c_snake, pc);
            sgj_pr_hr(jsp, "    OTV=%d\n", (int)other_valid);
            sgj_js_nv_ihex_nex(jsp, jo3p, "otv",  (int)other_valid,
                               false, "Other Temperature Valid");

            temperature_str(bp[5], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", temp, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, temp, bp[5], false,
                                   NULL, b, "current [Celsius]");
            temperature_str(bp[6], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lmaxt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lmaxt, bp[6], false,
                                   NULL, b, NULL);
            temperature_str(bp[7], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lmint, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lmint, bp[7], false,
                                   NULL, b, NULL);
            temperature_str(bp[8], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", maxtspo, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, maxtspo, bp[8], false,
                                   NULL, b, NULL);
            temperature_str(bp[9], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", mintspo, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, mintspo, bp[9], false,
                                   NULL, b, NULL);
            if (other_valid) {
                temperature_str(bp[10], true, b, blen);
                sgj_pr_hr(jsp, "    %s: %s\n", maxot, b);
                js_snakenv_ihexstr_nex(jsp, jo3p, maxot, bp[10], false,
                                       NULL, b, NULL);
                temperature_str(bp[11], true, b, blen);
                sgj_pr_hr(jsp, "    %s: %s\n", minot, b);
                js_snakenv_ihexstr_nex(jsp, jo3p, minot, bp[11], false,
                                       NULL, b, NULL);
            }
        } else if (pc < 0x200) {
            if (pl < 12)  {
                pr2serr("  <<expect parameter 0x%x to be at least 12 bytes "
                        "long, got %d, skip>>\n", pc, pl);
                goto inner;
            }
            sgj_pr_hr(jsp, "  %s=0x%x\n", param_c, pc);
            sgj_js_nv_ihex(jsp, jo3p, param_c_snake, pc);
            sgj_pr_hr(jsp, "    ORHV=%d\n", (int)other_valid);
            sgj_js_nv_ihex_nex(jsp, jo3p, "orhv",  (int)other_valid,
                               false, "Other Relative Humidity Valid");

            humidity_str(bp[5], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", relhum, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, relhum, bp[5], false,
                                   NULL, b, NULL);
            humidity_str(bp[6], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lmaxrh, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lmaxrh, bp[6], false,
                                   NULL, b, NULL);
            humidity_str(bp[7], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lminrh, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lminrh, bp[7], false,
                                   NULL, b, NULL);
            humidity_str(bp[8], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", maxrhspo, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, maxrhspo, bp[8], false,
                                   NULL, b, NULL);
            humidity_str(bp[9], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", minrhspo, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, minrhspo, bp[9], false,
                                   NULL, b, NULL);
            if (other_valid) {
                humidity_str(bp[10], true, b, blen);
                sgj_pr_hr(jsp, "    %s: %s\n", maxorh, b);
                js_snakenv_ihexstr_nex(jsp, jo3p, maxorh, bp[10], false,
                                       NULL, b, NULL);
                humidity_str(bp[11], true, b, blen);
                sgj_pr_hr(jsp, "    %s: %s\n", minorh, b);
                js_snakenv_ihexstr_nex(jsp, jo3p, minorh, bp[11], false,
                                       NULL, b, NULL);
            }
        } else
            sgj_pr_hr(jsp, "  <<unexpected parameter code 0x%x\n", pc);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2], str,
                      sizeof(str)));
inner:
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* ENV_LIMITS_SUBPG [0xd,0x2] <enl> introduced: SPC-5 (rev 02) */
static bool
show_environmental_limits_page(const uint8_t * resp, int len,
                               struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc, blen;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[32];
    static const char * ellp = "Environmental limits log page";
    static const char * hctlt = "High critical temperature limit trigger";
    static const char * hctlr = "High critical temperature limit reset";
    static const char * lctlr = "High critical temperature limit reset";
    static const char * lctlt = "High critical temperature limit trigger";
    static const char * hotlt = "High operating temperature limit trigger";
    static const char * hotlr = "High operating temperature limit reset";
    static const char * lotlr = "High operating temperature limit reset";
    static const char * lotlt = "High operating temperature limit trigger";
    static const char * hcrhlt =
                "High critical relative humidity limit trigger";
    static const char * hcrhlr =
                "High critical relative humidity limit reset";
    static const char * lcrhlr =
                "High critical relative humidity limit reset";
    static const char * lcrhlt =
                "High critical relative humidity limit trigger";
    static const char * horhlt =
                "High operating relative humidity limit trigger";
    static const char * horhlr =
                "High operating relative humidity limit reset";
    static const char * lorhlr =
                "High operating relative humidity limit reset";
    static const char * lorhlt =
                "High operating relative humidity limit trigger";

    blen = sizeof(b);
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0xd,0x2]\n", ellp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, ellp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                                   "environmental_limits_log_parameters");
    }
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }
        if (pc < 0x100) {
            if (pl < 12)  {
                pr2serr("  <<expect parameter 0x%x to be at least 12 bytes "
                        "long, got %d, skip>>\n", pc, pl);
                goto inner;
            }
            sgj_pr_hr(jsp, "  %s=0x%x\n", param_c, pc);
            sgj_js_nv_ihex(jsp, jo3p, param_c_snake, pc);

            temperature_str(bp[4], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", hctlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, hctlt, bp[4], false,
                                   NULL, b, "[Celsius]");
            temperature_str(bp[5], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", hctlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, hctlr, bp[5], false,
                                   NULL, b, NULL);
            temperature_str(bp[6], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lctlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lctlr, bp[6], false,
                                   NULL, b, NULL);
            temperature_str(bp[7], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lctlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lctlt, bp[7], false,
                                   NULL, b, NULL);
            temperature_str(bp[8], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", hotlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, hotlt, bp[8], false,
                                   NULL, b, NULL);
            temperature_str(bp[9], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", hotlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, hotlr, bp[9], false,
                                   NULL, b, NULL);
            temperature_str(bp[10], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lotlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lotlr, bp[10], false,
                                   NULL, b, NULL);
            temperature_str(bp[11], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lotlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lotlt, bp[11], false,
                                   NULL, b, NULL);
        } else if (pc < 0x200) {
            if (pl < 12)  {
                pr2serr("  <<expect parameter 0x%x to be at least 12 bytes "
                        "long, got %d, skip>>\n", pc, pl);
                goto inner;
            }
            sgj_pr_hr(jsp, "  %s=0x%x\n", param_c, pc);
            sgj_js_nv_ihex(jsp, jo3p, param_c_snake, pc);

            humidity_str(bp[4], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", hcrhlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, hcrhlt, bp[4], false,
                                   NULL, b, "[percentage]");
            humidity_str(bp[5], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", hcrhlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, hcrhlr, bp[5], false,
                                   NULL, b, NULL);
            humidity_str(bp[6], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lcrhlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lcrhlr, bp[6], false,
                                   NULL, b, NULL);
            humidity_str(bp[7], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lcrhlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lcrhlt, bp[7], false,
                                   NULL, b, NULL);
            humidity_str(bp[8], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", horhlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, horhlt, bp[8], false,
                                   NULL, b, NULL);
            humidity_str(bp[9], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", horhlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, horhlr, bp[9], false,
                                   NULL, b, NULL);
            humidity_str(bp[10], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lorhlr, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lorhlr, bp[10], false,
                                   NULL, b, NULL);
            humidity_str(bp[11], true, b, blen);
            sgj_pr_hr(jsp, "    %s: %s\n", lorhlt, b);
            js_snakenv_ihexstr_nex(jsp, jo3p, lorhlt, bp[11], false,
                                   NULL, b, NULL);
        } else
             sgj_pr_hr(jsp, "  <<unexpected parameter code 0x%x\n", pc);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
inner:
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* CMD_DUR_LIMITS_SUBPG [0x19,0x21] <cdl>
 * introduced: SPC-6 rev 1, significantly changed rev 6 */
static bool
show_cmd_dur_limits_page(const uint8_t * resp, int len,
                         struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    uint32_t count, noitmc_v, noatmc_v, noitatmc_v, noc_v;
    const uint8_t * bp;
    const char * cp;
    const char * thp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[144];
    static const char * cdllp = "Command duration limits statistics log page";
    static const char * t2cdld = "T2 command duration limit descriptor";
    static const char * cdlt2amp = "CDL T2A mode page";
    static const char * cdlt2bmp = "CDL T2B mode page";
    static const char * first_7[] = {"First", "Second", "Third", "Fourth",
                                     "Fifth", "Sixth", "Seventh"};
    static const char * noitmc = "Number of inactive target miss commands";
    static const char * noatmc = "Number of active target miss commands";
    static const char * noitatmc =
        "Number of inactive target and active target miss commands";
    static const char * noc = "Number of commands";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x19,0x21]\n", cdllp);
    num = len - 4;
    bp = &resp[0] + 4;
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, cdllp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                        "command_duration_limits_statistcs_log_parameters");
    }

    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;         /* parameter length */
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        switch (pc) {
        case 0x1:
            /* spc6r06: table 349 name "Number of READ commands" seems to
             * be wrong. Use what surrounding text and table 347 suggest */
            cp = "Achievable latency target";
            count =  sg_get_unaligned_be32(bp + 4);
            sgj_pr_hr(jsp, "  %s = %" PRIu32 "\n", cp, count);
            if (jsp->pr_as_json) {
                sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL, cp);
                js_snakenv_ihexstr_nex(jsp, jop, cp, count, true, NULL, NULL,
                                       "unit: microsecond");
            }
            break;
        case 0x11:
        case 0x12:
        case 0x13:
        case 0x14:
        case 0x15:
        case 0x16:
        case 0x17:
            sgj_pr_hr(jsp, "  parameter code 0x%x restricted\n", pc);
            if (jsp->pr_as_json)
                sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                  restricted_s);
            break;
        case 0x21:
        case 0x22:
        case 0x23:
        case 0x24:
        case 0x25:
        case 0x26:
        case 0x27:
            thp = first_7[pc - 0x21];
            sgj_pr_hr(jsp, "  %s %s for %s [pc=0x%x]:\n", thp, t2cdld,
                      cdlt2amp, pc);
            noitmc_v = sg_get_unaligned_be32(bp + 4);
            sgj_pr_hr(jsp, "    %s = %u\n", noitmc, noitmc_v);
            noatmc_v = sg_get_unaligned_be32(bp + 8);
            sgj_pr_hr(jsp, "    %s = %u\n", noatmc, noatmc_v);
            noitatmc_v = sg_get_unaligned_be32(bp + 12);
            sgj_pr_hr(jsp, "    %s = %u\n", noitatmc, noitatmc_v);
            noc_v = sg_get_unaligned_be32(bp + 16);
            sgj_pr_hr(jsp, "    %s = %u\n", noc, noc_v);
            if (jsp->pr_as_json) {
                snprintf(b, sizeof(b), "%s %s for %s", thp, t2cdld, cdlt2amp);
                sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL, b);

                js_snakenv_ihexstr_nex(jsp, jop, noitmc, noitmc_v, true, NULL,
                                       NULL, NULL);
                js_snakenv_ihexstr_nex(jsp, jop, noatmc, noatmc_v, true, NULL,
                                       NULL, NULL);
                js_snakenv_ihexstr_nex(jsp, jop, noitatmc, noitatmc_v, true,
                                       NULL, NULL, NULL);
                js_snakenv_ihexstr_nex(jsp, jop, noc, noc_v, true, NULL,
                                       NULL, NULL);
            }
            break;
        case 0x31:
        case 0x32:
        case 0x33:
        case 0x34:
        case 0x35:
        case 0x36:
        case 0x37:
            sgj_pr_hr(jsp, "  parameter code 0x%x restricted\n", pc);
            if (jsp->pr_as_json)
                sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                  restricted_s);
            break;
        case 0x41:
        case 0x42:
        case 0x43:
        case 0x44:
        case 0x45:
        case 0x46:
        case 0x47:
            /* This short form introduced in draft spc6r06 */
            thp = first_7[pc - 0x41];
            sgj_pr_hr(jsp, "  %s %s for %s [pc=0x%x]:\n", thp, t2cdld,
                      cdlt2bmp, pc);
            noitmc_v = sg_get_unaligned_be32(bp + 4);
            sgj_pr_hr(jsp, "    %s = %u\n", noitmc, noitmc_v);
            noatmc_v = sg_get_unaligned_be32(bp + 8);
            sgj_pr_hr(jsp, "    %s = %u\n", noatmc, noatmc_v);
            noitatmc_v = sg_get_unaligned_be32(bp + 12);
            sgj_pr_hr(jsp, "    %s = %u\n", noitatmc, noitatmc_v);
            noc_v = sg_get_unaligned_be32(bp + 16);
            sgj_pr_hr(jsp, "    %s = %u\n", noc, noc_v);
            if (jsp->pr_as_json) {
                snprintf(b, sizeof(b), "%s %s for %s", thp, t2cdld, cdlt2amp);
                sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL, b);

                js_snakenv_ihexstr_nex(jsp, jop, noitmc, noitmc_v, true, NULL,
                                       NULL, NULL);
                js_snakenv_ihexstr_nex(jsp, jop, noatmc, noatmc_v, true, NULL,
                                       NULL, NULL);
                js_snakenv_ihexstr_nex(jsp, jop, noitatmc, noitatmc_v, true,
                                       NULL, NULL, NULL);
                js_snakenv_ihexstr_nex(jsp, jop, noc, noc_v, true, NULL,
                                       NULL, NULL);
            }

            break;
        default:
             sgj_pr_hr(jsp, "  <<unexpected parameter code 0x%x\n", pc);
            break;
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Tape usage: Vendor specific (LTO-5 and LTO-6): 0x30 */
static bool
show_tape_usage_page(const uint8_t * resp, int len, struct opts_t * op,
                     sgj_opaque_p jop)
{
    int k, num, extra;
    unsigned int n;
    uint64_t ull;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed tape usage page\n");
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Tape usage page  (LTO-5 and LTO-6 specific) [0x30]\n");
    for (k = num; k > 0; k -= extra, bp += extra) {
        int pc = sg_get_unaligned_be16(bp + 0);

        extra = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, extra);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            break;
        }
        ull = n = 0;
        switch (bp[3]) {
        case 2:
            n = sg_get_unaligned_be16(bp + 4);
            break;
        case 4:
            n = sg_get_unaligned_be32(bp + 4);
            break;
        case 8:
            ull = sg_get_unaligned_be64(bp + 4);
            break;
        }
        switch (pc) {
        case 0x01:
            if (extra == 8)
                printf("  Thread count: %u", n);
            break;
        case 0x02:
            if (extra == 12)
                printf("  Total data sets written: %" PRIu64, ull);
            break;
        case 0x03:
            if (extra == 8)
                printf("  Total write retries: %u", n);
            break;
        case 0x04:
            if (extra == 6)
                printf("  Total unrecovered write errors: %u", n);
            break;
        case 0x05:
            if (extra == 6)
                printf("  Total suspended writes: %u", n);
            break;
        case 0x06:
            if (extra == 6)
                printf("  Total fatal suspended writes: %u", n);
            break;
        case 0x07:
            if (extra == 12)
                printf("  Total data sets read: %" PRIu64, ull);
            break;
        case 0x08:
            if (extra == 8)
                printf("  Total read retries: %u", n);
            break;
        case 0x09:
            if (extra == 6)
                printf("  Total unrecovered read errors: %u", n);
            break;
        case 0x0a:
            if (extra == 6)
                printf("  Total suspended reads: %u", n);
            break;
        case 0x0b:
            if (extra == 6)
                printf("  Total fatal suspended reads: %u", n);
            break;
        default:
            printf("  unknown parameter code = 0x%x, contents in "
                   "hex:\n", pc);
            hex2stdout(bp, extra, 1);
            break;
        }
        printf("\n");
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
        if (op->filter_given)
            break;
    }
    return true;
}

/* 0x30 */
static bool
show_hgst_perf_page(const uint8_t * resp, int len, struct opts_t * op,
                    sgj_opaque_p jop)
{
    bool valid = false;
    int num, pl;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("HGST/WDC performance counters page [0x30]\n");
    num = len - 4;
    if (num < 0x30) {
        printf("HGST/WDC performance counters page too short (%d) < 48\n",
               num);
        return valid;
    }
    bp = &resp[0] + 4;
    while (num > 3) {
        int pc = sg_get_unaligned_be16(bp + 0);

        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        switch (pc) {
        case 0:
            valid = true;
            printf("  Zero Seeks = %u\n", sg_get_unaligned_be16(bp + 4));
            printf("  Seeks >= 2/3 = %u\n", sg_get_unaligned_be16(bp + 6));
            printf("  Seeks >= 1/3 and < 2/3 = %u\n",
                   sg_get_unaligned_be16(bp + 8));
            printf("  Seeks >= 1/6 and < 1/3 = %u\n",
                   sg_get_unaligned_be16(bp + 10));
            printf("  Seeks >= 1/12 and < 1/6 = %u\n",
                   sg_get_unaligned_be16(bp + 12));
            printf("  Seeks > 0 and < 1/12 = %u\n",
                   sg_get_unaligned_be16(bp + 14));
            printf("  Overrun Counter = %u\n",
                   sg_get_unaligned_be16(bp + 20));
            printf("  Underrun Counter = %u\n",
                   sg_get_unaligned_be16(bp + 22));
            printf("  Device Cache Full Read Hits = %u\n",
                   sg_get_unaligned_be32(bp + 24));
            printf("  Device Cache Partial Read Hits = %u\n",
                   sg_get_unaligned_be32(bp + 28));
            printf("  Device Cache Write Hits = %u\n",
                   sg_get_unaligned_be32(bp + 32));
            printf("  Device Cache Fast Writes = %u\n",
                   sg_get_unaligned_be32(bp + 36));
            printf("  Device Cache Read Misses = %u\n",
                   sg_get_unaligned_be32(bp + 40));
            break;
        default:
            valid = false;
            printf("  Unknown HGST/WDC parameter code = 0x%x", pc);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return valid;
}

/* Tape capacity: vendor specific (LTO-5 and LTO-6 ?): 0x31 */
static bool
show_tape_capacity_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    int k, num, extra;
    unsigned int n;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed tape capacity page\n");
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Tape capacity page  (LTO-5 and LTO-6 specific) [0x31]\n");
    for (k = num; k > 0; k -= extra, bp += extra) {
        int pc = sg_get_unaligned_be16(bp + 0);

        extra = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, extra);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            break;
        }
        if (extra != 8)
            continue;
        n = sg_get_unaligned_be32(bp + 4);
        switch (pc) {
        case 0x01:
            printf("  Main partition remaining capacity (in MiB): %u", n);
            break;
        case 0x02:
            printf("  Alternate partition remaining capacity (in MiB): %u", n);
            break;
        case 0x03:
            printf("  Main partition maximum capacity (in MiB): %u", n);
            break;
        case 0x04:
            printf("  Alternate partition maximum capacity (in MiB): %u", n);
            break;
        default:
            printf("  unknown parameter code = 0x%x, contents in "
                    "hex:\n", pc);
            hex2stdout(bp, extra, 1);
            break;
        }
        printf("\n");
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
        if (op->filter_given)
            break;
    }
    return true;
}

/* Data compression: originally vendor specific 0x32 (LTO-5), then
 * ssc-4 standardizes it at 0x1b <dc> */
static bool
show_data_compression_page(const uint8_t * resp, int len,
                           struct opts_t * op, sgj_opaque_p jop)
{
    int k, j, pl, num, extra, pc, pg_code;
    uint64_t n;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    pg_code = resp[0] & 0x3f;
    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed data compression page\n");
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (0x1b == pg_code)
            printf("Data compression page  (ssc-4) [0x1b]\n");
        else
            printf("Data compression page  (LTO-5 specific) [0x%x]\n",
                   pg_code);
    }
    for (k = num; k > 0; k -= extra, bp += extra) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3];
        extra = pl + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, extra);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            break;
        }
        if ((0 == pl) || (pl > 8)) {
            printf("badly formed data compression log parameter\n");
            printf("  parameter code = 0x%x, contents in hex:\n", pc);
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            goto skip_para;
        }
        /* variable length integer, max length 8 bytes */
        for (j = 0, n = 0; j < pl; ++j) {
            if (j > 0)
                n <<= 8;
            n |= bp[4 + j];
        }
        switch (pc) {
        case 0x00:
            printf("  Read compression ratio x100: %" PRIu64 , n);
            break;
        case 0x01:
            printf("  Write compression ratio x100: %" PRIu64 , n);
            break;
        case 0x02:
            printf("  Megabytes transferred to server: %" PRIu64 , n);
            break;
        case 0x03:
            printf("  Bytes transferred to server: %" PRIu64 , n);
            break;
        case 0x04:
            printf("  Megabytes read from tape: %" PRIu64 , n);
            break;
        case 0x05:
            printf("  Bytes read from tape: %" PRIu64 , n);
            break;
        case 0x06:
            printf("  Megabytes transferred from server: %" PRIu64 , n);
            break;
        case 0x07:
            printf("  Bytes transferred from server: %" PRIu64 , n);
            break;
        case 0x08:
            printf("  Megabytes written to tape: %" PRIu64 , n);
            break;
        case 0x09:
            printf("  Bytes written to tape: %" PRIu64 , n);
            break;
        case 0x100:
            printf("  Data compression enabled: 0x%" PRIx64, n);
            break;
        default:
            printf("  unknown parameter code = 0x%x, contents in "
                    "hex:\n", pc);
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            break;
        }
skip_para:
        printf("\n");
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
        if (op->filter_given)
            break;
    }
    return true;
}

/* LAST_N_ERR_LPAGE [0x7] <lne>  introduced: SPC-2 */
static bool
show_last_n_error_page(const uint8_t * resp, int len,
                       struct opts_t * op, sgj_opaque_p jop)
{
    int k, num, pl;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[256];
    static const char * lneelp = "Last n error events log page";
    static const char * eed = "error_event_data";

    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        sgj_pr_hr(jsp, "No error events logged\n");
        return true;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x7]\n", lneelp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, lneelp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p, "error_event_log_parameters");
    }

    for (k = num; k > 0; k -= pl, bp += pl) {
        uint16_t pc;

        if (k < 3) {
            pr2serr("short %s\n", lneelp);
            return false;
        }
        pl = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
            sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL, NULL);
        }

        sgj_pr_hr(jsp, "  Error event %u [0x%x]:\n", pc, pc);
        if (pl > 4) {
            if ((bp[2] & 0x1) && (bp[2] & 0x2)) {
                sgj_pr_hr(jsp, "    [binary]:\n");
                hex2str(bp + 4, pl - 4, "    ", op->hex2str_oformat,
                        sizeof(b), b);
                sgj_pr_hr(jsp, "%s\n", b);
                if (jsp->pr_as_json)
                    sgj_js_nv_hex_bytes(jsp, jo3p, eed, bp + 4, pl - 4);
            } else if (0x01 == (bp[2] & 0x3)) {  /* ASCII */
                sgj_pr_hr(jsp, "    %.*s\n", pl - 4, (const char *)(bp + 4));
                if (jsp->pr_as_json)
                    sgj_js_nv_s_len(jsp, jo3p, eed,
                                    (const char *)(bp + 4), pl - 4);
            } else {
                sgj_pr_hr(jsp, "    [data counter?? (LP bit should be "
                          "set)]:\n");
                hex2str(bp + 4, pl - 4, "    ", op->hex2str_oformat,
                        sizeof(b), b);
                sgj_pr_hr(jsp, "%s\n", b);
                if (jsp->pr_as_json)
                    sgj_js_nv_hex_bytes(jsp, jo3p, eed, bp + 4, pl - 4);
            }
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
    }
    return true;
}

/* LAST_N_DEFERRED_LPAGE [0xb] <lnd>  introduced: SPC-2 */
static bool
show_last_n_deferred_error_page(const uint8_t * resp, int len,
                                struct opts_t * op, sgj_opaque_p jop)
{
    int k, n, num, pl;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p, jo4p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[512];
    static const char * lndeoaelp =
                "Last n deferred errors or asynchronous events log page";
    static const char * deoae = "Deferred error or asynchronous event";
    static const char * sd = "sense_data";

    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("No deferred errors logged\n");
        return true;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0xb]\n", lndeoaelp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, lndeoaelp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                "deferred_error_or_asynchronous_event_log_parameters");
    }

    for (k = num; k > 0; k -= pl, bp += pl) {
        int pc;

        if (k < 3) {
            pr2serr("short %s\n", lndeoaelp);
            return false;
        }
        pl = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
            sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL, deoae);
        }
        sgj_pr_hr(jsp, "  %s [0x%x]:\n", deoae, pc);
        if (op->do_brief > 0) {
            hex2stdout(bp + 4, pl - 4, op->dstrhex_no_ascii);
            hex2str(bp + 4, pl - 4, "    ", op->hex2str_oformat,
                    sizeof(b), b);
            sgj_pr_hr(jsp, "%s\n", b);
            if (jsp->pr_as_json)
                sgj_js_nv_hex_bytes(jsp, jo3p, sd, bp + 4, pl - 4);
        } else {

            n = sg_get_sense_str("    ", bp + 4, pl - 4,  false, sizeof(b),
                                 b);
            sgj_pr_hr(jsp, "%.*s\n", n, b);
            if (jsp->pr_as_json) {
                jo4p = sgj_named_subobject_r(jsp, jo3p, sd);
                sgj_js_sense(jsp, jo4p, bp + 4, pl - 4);
            }
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
    }
    return true;
}

static const char * clgc = "Change list generation code";
static const char * cgn = "Changed generation number";

/* LAST_N_INQUIRY_DATA_CH_SUBPG [0xb,0x1] <lnic> introduced: SPC-5 (rev 17) */
static bool
show_last_n_inq_data_ch_page(const uint8_t * resp, int len,
                             struct opts_t * op, sgj_opaque_p jop)
{
    bool vpd;
    int j, num, pl, vpd_pg;
    uint32_t k, n;
    const uint8_t * bp;
    const char * vpd_pg_name = NULL;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p, jo4p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    sgj_opaque_p ja2p;
    char str[PCB_STR_LEN];
    char b[128];
    static const char * lnidclp = "Last n inquiry data changed log page";
    static const char * idci = "Inquiry data changed indicator";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0xb,0x1]\n", lnidclp);
    num = len - 4;
    bp = &resp[0] + 4;
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, lnidclp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                                   "inquiry_data_changed_log_parameters");
    }

    while (num > 3) {
        int pc = sg_get_unaligned_be16(bp + 0);

        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
            sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                              0 == pc ? clgc : idci);
        }
        if (0 == pc) {
            if (pl < 8)  {
                pr2serr("  <<expect parameter 0x%x to be at least 8 bytes "
                        "long, got %d, skip>>\n", pc, pl);
                goto skip;
            }
            sgj_pr_hr(jsp, "  %s [pc=0x0]:\n", clgc);
            for (j = 4, k = 1; j < pl; j +=4, ++k) {
                n = sg_get_unaligned_be32(bp + j);
                sgj_pr_hr(jsp, "    %s [0x%x]: %u\n", cgn, k, n);
            }
            if (jsp->pr_as_json) {
                ja2p = sgj_named_subarray_r(jsp, jo3p,
                                            "changed_generation_numbers");
                for (j = 4, k = 1; j < pl; j +=4, ++k) {
                    jo4p = sgj_new_unattached_object_r(jsp);
                    n = sg_get_unaligned_be32(bp + j);
                    js_snakenv_ihexstr_nex(jsp, jo4p, cgn, n, true, NULL,
                                           NULL, NULL);
                    sgj_js_nv_o(jsp, ja2p, NULL /* name */, jo4p);
                }
            }
        } else {        /* pc > 0x0 */
            int m;
            const int nn = sg_lib_names_mode_len;
            struct sg_lib_simple_value_name_t * nvp = sg_lib_names_vpd_arr;

            snprintf(b, sizeof(b), "  %s 0x%x, ", param_c, pc);
            vpd = !! (1 & *(bp + 4));
            vpd_pg = *(bp + 5);
            if (vpd) {
                for (m = 0; m < nn; ++m, ++nvp) {
                    if (nvp->value == vpd_pg)
                        break;
                }
                vpd_pg_name = (m < nn) ? nvp->name : NULL;
            } else
                vpd_pg_name = "Standard INQUIRY";

            if (jsp->pr_as_json) {
                sgj_js_nv_i(jsp, jo3p, "vpd", (int)vpd);
                sgj_js_nv_ihex(jsp, jo3p, "changed_page_code", vpd_pg);
                if (vpd_pg_name)
                    sgj_js_nv_s(jsp, jo3p, "changed_page_name", vpd_pg_name);
            }
            if (vpd) {
                sgj_pr_hr(jsp, "%sVPD page 0x%x changed\n", b, vpd_pg);
                if (0 == op->do_brief) {
                    if (vpd_pg_name)
                        sgj_pr_hr(jsp, "    name: %s\n", vpd_pg_name);
                }
            } else
                sgj_pr_hr(jsp, "%sStandard INQUIRY data changed\n", b);
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
skip:
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
        num -= pl;
        bp += pl;
    }
    return true;
}

/* LAST_N_MODE_PG_DATA_CH_SUBPG [0xb,0x2] <lnmc> introduced: SPC-5 (rev 17) */
static bool
show_last_n_mode_pg_data_ch_page(const uint8_t * resp, int len,
                                 struct opts_t * op, sgj_opaque_p jop)
{
    bool spf;
    int j, k, num, pl, pg_code, spg_code;
    uint32_t n;
    const uint8_t * bp;
    const char * mode_pg_name = NULL;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p, jo4p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    sgj_opaque_p ja2p;
    char str[PCB_STR_LEN];
    char b[128];
    static const char * lnmpdclp = "Last n mode page data changed log page";
    static const char * mpdci = "Mode page data changed indicator";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0xb,0x2]\n", lnmpdclp);
    num = len - 4;
    bp = &resp[0] + 4;
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, lnmpdclp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                                   "mode_page_data_changed_log_parameters");
    }

    while (num > 3) {
        int pc = sg_get_unaligned_be16(bp + 0);

        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
            sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                              0 == pc ? clgc : mpdci);
        }
        if (0 == pc) {  /* Same as LAST_N_INQUIRY_DATA_CH_SUBPG [0xb,0x1] */
            if (pl < 8)  {
                pr2serr("  <<expect parameter 0x%x to be at least 8 bytes "
                        "long, got %d, skip>>\n", pc, pl);
                goto skip;
            }
            sgj_pr_hr(jsp, "  %s [pc=0x0]:\n", clgc);
            for (j = 4, k = 1; j < pl; j +=4, ++k) {
                n = sg_get_unaligned_be32(bp + j);
                sgj_pr_hr(jsp, "    %s [0x%x]: %u\n", cgn, k, n);
            }
            if (jsp->pr_as_json) {
                ja2p = sgj_named_subarray_r(jsp, jo3p,
                                            "changed_generation_numbers");
                for (j = 4, k = 1; j < pl; j +=4, ++k) {
                    jo4p = sgj_new_unattached_object_r(jsp);
                    n = sg_get_unaligned_be32(bp + j);
                    js_snakenv_ihexstr_nex(jsp, jo4p, cgn, n, true, NULL,
                                           NULL, NULL);
                    sgj_js_nv_o(jsp, ja2p, NULL /* name */, jo4p);
                }
            }
        } else {        /* pc > 0x0 */
            int k, val;
            const int nn = sg_lib_names_mode_len;
            struct sg_lib_simple_value_name_t * nmp = sg_lib_names_mode_arr;

            snprintf(b, sizeof(b), "  %s 0x%x, ", param_c, pc);
            spf = !! (0x40 & *(bp + 4));
            pg_code = 0x3f & *(bp + 4);
            spg_code = *(bp + 5);
            if (spf)       /* SPF bit set */
                sgj_pr_hr(jsp, "%smode page 0x%x,0%x changed\n", b, pg_code,
                          spg_code);
            else
                sgj_pr_hr(jsp, "%smode page 0x%x changed\n", b, pg_code);

            val = (pg_code << 8) | spg_code;
            for (k = 0; k < nn; ++k, ++nmp) {
                if (nmp->value == val)
                    break;
            }
            mode_pg_name = (k < nn) ? nmp->name : NULL;
            if ((0 == op->do_brief) && mode_pg_name)
                sgj_pr_hr(jsp, "    name: %s\n", nmp->name);
            if (jsp->pr_as_json) {
                sgj_js_nv_i(jsp, jo3p, "spf", (int)spf);
                sgj_js_nv_ihex(jsp, jo3p, "mode_page_code", pg_code);
                sgj_js_nv_ihex(jsp, jo3p, "subpage_code", spg_code);
                if (mode_pg_name)
                    sgj_js_nv_s(jsp, jo3p, "mode_page_name", mode_pg_name);
            }
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
skip:
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->filter_given)
            break;
        num -= pl;
        bp += pl;
    }
    return true;
}

static const char * self_test_code[] = {
    "default", "background short", "background extended", "reserved",
    "aborted background", "foreground short", "foreground extended",
    "reserved"};

static const char * self_test_result[] = {
    "completed without error",
    "aborted by SEND DIAGNOSTIC",
    "aborted other than by SEND DIAGNOSTIC",
    "unknown error, unable to complete",
    "self test completed with failure in test segment (which one unknown)",
    "first segment in self test failed",
    "second segment in self test failed",
    "another segment in self test failed",
    "reserved", "reserved", "reserved", "reserved", "reserved", "reserved",
    "reserved",
    "self test in progress"};

/* SELF_TEST_LPAGE [0x10] <str>  introduced: SPC-3 */
static bool
show_self_test_page(const uint8_t * resp, int len, struct opts_t * op,
                    sgj_opaque_p jop)
{
    bool addr_all_ffs;
    int k, num, res, st_c;
    unsigned int v;
    uint32_t n;
    uint64_t ull;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[80];
    static const char * strlp = "Self-test results log page";
    static const char * stc_s = "Self-test code";
    static const char * str_s = "Self-test result";
    static const char * stn_s = "Self-test number";
    static const char * apoh = "Accumulated power on hours";

    num = len - 4;
    if (num < 0x190) {
        pr2serr("short %s [length 0x%x rather than 0x190 bytes]\n", strlp,
                 num);
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x10]\n", strlp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, strlp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                                   "self_test_results_log_parameters");
    }

    for (k = 0, bp = resp + 4; k < 20; ++k, bp += 20 ) {
        int pc = sg_get_unaligned_be16(bp + 0);
        int pl = bp[3] + 4;

        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            break;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
            sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                              "Self-test results");
        }
        n = sg_get_unaligned_be16(bp + 6);
        if ((0 == n) && (0 == bp[4])) {
            if (jsp->pr_as_json)
                sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
            break;
        }
        sgj_pr_hr(jsp, "  Parameter code = %d, accumulated power-on hours "
                  "= %d\n", pc, n);
        st_c = (bp[4] >> 5) & 0x7;
        sgj_pr_hr(jsp, "    %s: %s [%d]\n", stc_s, self_test_code[st_c],
                  st_c);
        res = bp[4] & 0xf;
        sgj_pr_hr(jsp, "    %s: %s [%d]\n", str_s, self_test_result[res],
                  res);
        if (bp[5])
            sgj_pr_hr(jsp, "    %s = %d\n", stn_s, (int)bp[5]);
        ull = sg_get_unaligned_be64(bp + 8);

        addr_all_ffs = sg_all_ffs(bp + 8, 8);
        if (! addr_all_ffs) {
            addr_all_ffs = false;
            if ((res > 0) && ( res < 0xf))
                sgj_pr_hr(jsp, "    address of first error = 0x%" PRIx64 "\n",
                          ull);
        }
            addr_all_ffs = false;
        v = bp[16] & 0xf;
        if (v) {
            if (op->do_brief)
                sgj_pr_hr(jsp, "    sense key = 0x%x , asc = 0x%x, ascq "
                          "= 0x%x\n", v, bp[17], bp[18]);
            else {
                sgj_pr_hr(jsp, "    sense key = 0x%x [%s]\n", v,
                          sg_get_sense_key_str(v, sizeof(b), b));

                sgj_pr_hr(jsp, "      asc = 0x%x, ascq = 0x%x [%s]\n",
                          bp[17], bp[18], sg_get_asc_ascq_str(bp[17], bp[18],
                          sizeof(b), b));
            }
        }
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2],
                      str, sizeof(str)));
        if (jsp->pr_as_json) {
            js_snakenv_ihexstr_nex(jsp, jo3p, stc_s, st_c, true, NULL,
                                   self_test_code[st_c], NULL);
            js_snakenv_ihexstr_nex(jsp, jo3p, str_s, res, true, NULL,
                                    self_test_result[res], NULL);
            js_snakenv_ihexstr_nex(jsp, jo3p, stn_s, bp[5], false, NULL,
                                   NULL, "segment number that failed");
            js_snakenv_ihexstr_nex(jsp, jo3p, apoh, n, true, NULL,
                   (0xffff == n ? "65535 hours or more" : NULL), NULL);
            sgj_js_nv_ihexstr(jsp, jo3p, "address_of_first_failure", pc, NULL,
                              addr_all_ffs ? "no errors detected" : NULL);
            sgj_js_nv_ihexstr(jsp, jo3p, "sense_key", v, NULL,
                              sg_get_sense_key_str(v, sizeof(b), b));
            sgj_js_nv_ihexstr(jsp, jo3p, "additional_sense_code", bp[17],
                              NULL, NULL);
            sgj_js_nv_ihexstr(jsp, jo3p, "additional_sense_code_qualifier",
                              bp[18], NULL, sg_get_asc_ascq_str(bp[17],
                              bp[18], sizeof(b), b));
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        }
        if (op->filter_given)
            break;
    }
    return true;
}

/* TEMPERATURE_LPAGE [0xd] <temp>  introduced: SPC-3
 * N.B. The ENV_REPORTING_SUBPG [0xd,0x1] and the ENV_LIMITS_SUBPG [0xd,0x2]
 * (both added SPC-5) are a superset of this page. */
static bool
show_temperature_page(const uint8_t * resp, int len, struct opts_t * op,
                      sgj_opaque_p jop)
{
    int k, num, extra;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    static const char * tlp = "Temperature log page";
    static const char * ctemp = "Current temperature";
    static const char * rtemp = "Reference temperature";

    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed Temperature page\n");
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (! op->do_temperature)
            sgj_pr_hr(jsp, "%s  [0xd]\n", tlp);
    }
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, tlp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p, "temperature_log_parameters");
    }

    for (k = num; k > 0; k -= extra, bp += extra) {
        int pc;

        if (k < 3) {
            pr2serr("short Temperature page\n");
            return true;
        }
        extra = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, extra);
            goto skip;
        } else if (op->do_hex) {
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            goto skip;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
            sgj_js_nv_ihex(jsp, jo3p, param_c_snake, pc);
        }

        switch (pc) {
        case 0:
            if ((extra > 5) && (k > 5)) {
                if (0 == bp[5])
                    sgj_pr_hr(jsp, "  %s = 0 C (or less)\n", ctemp);
                else if (bp[5] < 0xff)
                    sgj_pr_hr(jsp, "  %s = %d C\n", ctemp, bp[5]);
                else
                    sgj_pr_hr(jsp, "  %s = <%s>\n", ctemp, not_avail);
                if (jsp->pr_as_json) {
                    const char * cp = NULL;

                    if (0 == bp[5])
                        cp = "0 or less Celsius";
                    else if (0xff == bp[5])
                        cp = "temperature not available";
                    js_snakenv_ihexstr_nex(jsp, jo3p, "temperature", bp[5],
                                           false, NULL, cp,
                                           "current [unit: celsius]");
                }
            }
            break;
        case 1:
            if ((extra > 5) && (k > 5)) {
                if (bp[5] < 0xff)
                    sgj_pr_hr(jsp, "  %s = %d C\n", rtemp, bp[5]);
                else
                    sgj_pr_hr(jsp, "  %s = <%s>\n", rtemp, not_avail);
                if (jsp->pr_as_json) {
                    const char * cp;

                    if (0 == bp[5])
                        cp = "in C (or less)";
                    else if (0xff == bp[5])
                        cp = "not available";
                    else
                        cp = "in C";
                    sgj_js_nv_ihex_nex(jsp, jo3p, "reference_temperature",
                                       bp[5], true, cp);
                }
            }
            break;
        default:
            if (! op->do_temperature) {
                sgj_pr_hr(jsp, "  unknown parameter code = 0x%x, contents "
                          "in hex:\n", pc);
                hex2stdout(bp, extra, op->dstrhex_no_ascii);
            } else {
                if (jsp->pr_as_json)
                    sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
                continue;
            }
            break;
        }
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2], str,
                      sizeof(str)));
skip:
        if (op->filter_given)
            break;
    }
    return true;
}

/* START_STOP_LPAGE [0xe] <sscc>  introduced: SPC-3 */
static bool
show_start_stop_page(const uint8_t * resp, int len, struct opts_t * op,
                     sgj_opaque_p jop)
{
    int k, num, extra;
    uint32_t val;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[256];
    static const char * sscclp = "Start-stop cycle counter log page";
    static const char * dom = "Date of manufacture";
    static const char * ad = "Accounting date";
    static const char * sccodl = "Specified cycle count over device lifetime";
    static const char * assc = "Accumulated start-stop cycles";
    static const char * slucodl =
                        "Specified load-unload count over device lifetime";
    static const char * aluc = "Accumulated load-unload cycles";

    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed Start-stop cycle counter page\n");
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0xe]\n", sscclp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, sscclp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                                   "start_stop_cycle_log_parameters");
    }

    for (k = num; k > 0; k -= extra, bp += extra) {
        int pc;

        if (k < 3) {
            pr2serr("short %s\n", sscclp);
            return false;
        }
        extra = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, extra);
            goto skip;
        } else if (op->do_hex) {
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            goto skip;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        switch (pc) {
        case 1:
            if (10 == extra) {
                 sgj_pr_hr(jsp, "  %s, year: %.4s, week: %.2s\n", dom,
                       bp + 4, bp + 8);
                if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      "Date of manufacture");
                    sgj_js_nv_s_len(jsp, jo3p, "year_of_manufacture",
                                    (const char *)(bp + 4), 4);
                    sgj_js_nv_s_len(jsp, jo3p, "week_of_manufacture",
                                    (const char *)(bp + 8), 2);
                }
            } else if (op->verbose) {
                pr2serr("%s parameter length strange: %d\n", dom, extra - 4);
                hex2stderr(bp, extra, 1);
            }
            break;
        case 2:
            if (10 == extra) {
                sgj_pr_hr(jsp, "  %s, year: %.4s, week: %.2s\n", ad, bp + 4,
                          bp + 8);
                if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      "Accounting date");
                    sgj_js_nv_s_len(jsp, jo3p, "year_of_manufacture",
                                    (const char *)(bp + 4), 4);
                    sgj_js_nv_s_len(jsp, jo3p, "week_of_manufacture",
                                    (const char *)(bp + 8), 2);
                }
            } else if (op->verbose) {
                pr2serr("%s parameter length strange: %d\n", ad, extra - 4);
                hex2stderr(bp, extra, 1);
            }
            break;
        case 3:
            if (extra > 7) {
                val = sg_get_unaligned_be32(bp + 4);
                sgj_pr_hr(jsp, "  %s = %u\n", sccodl, val);
                if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      sccodl);
                    js_snakenv_ihexstr_nex(jsp, jo3p, sccodl, val, false,
                                           NULL, NULL, NULL);
                }
            }
            break;
        case 4:
            if (extra > 7) {
                val = sg_get_unaligned_be32(bp + 4);
                sgj_pr_hr(jsp, "  %s = %u\n", assc, val);
                if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      assc);
                    js_snakenv_ihexstr_nex(jsp, jo3p, assc, val, false,
                                           NULL, NULL, NULL);
                }
            }
            break;
        case 5:
            if (extra > 7) {
                val = sg_get_unaligned_be32(bp + 4);
                sgj_pr_hr(jsp, "  %s = %u\n", slucodl, val);
                if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      slucodl);
                    js_snakenv_ihexstr_nex(jsp, jo3p, slucodl, val, false,
                                           NULL, NULL, NULL);
                }
            }
            break;
        case 6:
            if (extra > 7) {
                val = sg_get_unaligned_be32(bp + 4);
                sgj_pr_hr(jsp, "  %s = %u\n", aluc, val);
                if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      aluc);
                    js_snakenv_ihexstr_nex(jsp, jo3p, aluc, val, false,
                                           NULL, NULL, NULL);
                }
            }
            break;
        default:
            sgj_pr_hr(jsp, "  unknown %s = 0x%x, contents in hex:\n",
                      param_c, pc);
            hex2str(bp, extra, "    ", op->hex2str_oformat, sizeof(b), b);
            sgj_pr_hr(jsp, "%s\n", b);
            if (jsp->pr_as_json) {
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      unknown_s);
                sgj_js_nv_hex_bytes(jsp, jo3p, "in_hex", bp, extra);
            }
            break;
        }
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2], str,
                      sizeof(str)));
skip:
        if (op->filter_given)
            break;
    }
    return true;
}

/* APP_CLIENT_LPAGE [0xf] <ac>  introduced: SPC-3 */
static bool
show_app_client_page(const uint8_t * resp, int len, struct opts_t * op,
                     sgj_opaque_p jop)
{
    int k, num, extra;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed Application Client page\n");
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (op->do_hex == 0)))
        printf("Application client page  [0xf]\n");
    if (0 == op->filter_given) {
        if ((len > 128) && (0 == op->do_hex) && (0 == op->undefined_hex)) {
            hex2fp(resp, 64, "  ", 0 != op->dstrhex_no_ascii, stdout);
            printf("  .....  [truncated after 64 of %d bytes (use '-H' to "
                   "see the rest)]\n", len);
        }
        else
            hex2fp(resp, len, "  ", 0 != op->dstrhex_no_ascii, stdout);
        return true;
    }
    /* only here if filter_given set */
    for (k = num; k > 0; k -= extra, bp += extra) {
        int pc;

        if (k < 3) {
            pr2serr("short Application client page\n");
            return true;
        }
        extra = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter != pc)
            continue;
        if (op->do_raw) {
            dStrRaw(bp, extra);
            break;
        }
        hex2stdout(bp, extra, op->dstrhex_no_ascii);
        printf("\n");
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
        break;
    }
    return true;
}

/* IE_LPAGE [0x2f] <ie> "Informational Exceptions"  introduced: SPC-3
 * Previously known as "SMART Status and Temperature Reading" lpage.  */
static bool
show_ie_page(const uint8_t * resp, int len, struct opts_t * op,
             sgj_opaque_p jop)
{
    bool skip = false;
    int k, num, param_len;
    const uint8_t * bp;
    const char * cp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    char b[512];
    char bb[64];
    bool full, decoded;
    static const char * ielp = "Informational exceptions log page";
    static const char * ieasc =
                         "informational_exceptions_additional_sense_code";
    static const char * ct = "Current temperature";
    static const char * tt = "Threshold temperature";
    static const char * mt = "Maximum temperature";
    static const char * ce = "common extension";

    full = ! op->do_temperature;
    num = len - 4;
    bp = &resp[0] + 4;
    if (num < 4) {
        pr2serr("badly formed %s\n", ielp);
        return false;
    }
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (full)
            sgj_pr_hr(jsp, "%s  [0x2f]\n", ielp);
    }
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, ielp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                           "informational_exceptions_log_parameters");
    }

    for (k = num; k > 0; k -= param_len, bp += param_len) {
        int pc;

        if (k < 3) {
            pr2serr("short %s\n", ielp);
            return false;
        }
        param_len = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, param_len);
            goto skip;
        } else if (op->do_hex) {
            hex2stdout(bp, param_len, op->dstrhex_no_ascii);
            goto skip;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }
        decoded = true;
        cp = NULL;

        switch (pc) {
        case 0x0:
            if (param_len > 5) {
                bool na;
                uint8_t t;

                if (full) {
                     sgj_pr_hr(jsp, "  IE asc = 0x%x, ascq = 0x%x\n", bp[4],
                               bp[5]);
                    if (bp[4] || bp[5])
                        if(sg_get_asc_ascq_str(bp[4], bp[5], sizeof(b), b))
                             sgj_pr_hr(jsp, "    [%s]\n", b);
                    if (jsp->pr_as_json) {
                        sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                          "Informational exceptions general");
                        sgj_js_nv_ihexstr(jsp, jo3p, ieasc, bp[4], NULL,
                                          NULL);
                        snprintf(b, sizeof(b), "%s_qualifier", ieasc);
                        sgj_js_nv_ihexstr(jsp, jo3p, b, bp[5], NULL,
                                          sg_get_asc_ascq_str(bp[4], bp[5],
                                          sizeof(bb), bb));
                    }
                }
                if (param_len <= 6)
                    break;
                t = bp[6];
                na = (0xff == t);
                if (na)
                    snprintf(b, sizeof(b), "%u C", t);
                else
                    snprintf(b, sizeof(b), "<%s>", unknown_s);
                sgj_pr_hr(jsp, "    %s = %s\n", ct, b);
                if (jsp->pr_as_json)
                    js_snakenv_ihexstr_nex(jsp, jo3p, ct, t, true,
                                           NULL, na ? unknown_s : NULL,
                                           "[unit: celsius]");
                if (param_len > 7) {
                    t = bp[7];
                    na = (0xff == t);
                    if (na)
                        snprintf(b, sizeof(b), "%u C", t);
                    else
                        snprintf(b, sizeof(b), "<%s>", unknown_s);
                    sgj_pr_hr(jsp, "    %s = %s  [%s]\n", tt, b, ce);
                    if (jsp->pr_as_json)
                        js_snakenv_ihexstr_nex(jsp, jo3p, tt, t, true, NULL,
                                               na ? unknown_s : NULL, ce);
                    t = bp[8];
                    if ((param_len > 8) && (t >= bp[6])) {
                        na = (0xff == t);
                        if (na)
                            snprintf(b, sizeof(b), "%u C", t);
                        else
                            snprintf(b, sizeof(b), "<%s>", unknown_s);
                        sgj_pr_hr(jsp, "    %s = %s  [%s]\n", mt, b, ce);
                        if (jsp->pr_as_json)
                            js_snakenv_ihexstr_nex(jsp, jo3p, mt, t, true,
                                                   NULL,
                                                   na ? unknown_s : NULL, ce);
                    }
                }
            }
            decoded = true;
            break;
        default:
            if (op->do_brief > 0) {
                cp = NULL;
                skip = true;
                break;
            }
            if (VP_HITA == op->vend_prod_num) {
                switch (pc) {
                case 0x1:
                    cp = "Remaining reserve 1";
                    break;
                case 0x2:
                    cp = "Remaining reserve XOR";
                    break;
                case 0x3:
                    cp = "XOR depletion";
                    break;
                case 0x4:
                    cp = "Volatile memory backup failure";
                    break;
                case 0x5:
                    cp = "Wear indicator";
                    break;
                case 0x6:
                    cp = "System area wear indicator";
                    break;
                case 0x7:
                    cp = "Channel hangs";
                    break;
                case 0x8:
                    cp = "Flash scan failure";
                    break;
                default:
                    decoded = false;
                    break;
                }
                if (cp) {
                    sgj_pr_hr(jsp, "  %s:\n", cp);
                    sgj_pr_hr(jsp, "    SMART sense_code=0x%x sense_qualifier"
                              "=0x%x threshold=%d%% trip=%d\n", bp[4], bp[5],
                              bp[6], bp[7]);
                    if (jsp->pr_as_json) {
                        sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                          cp);
                        sgj_js_nv_ihex(jsp, jo3p, "smart_sense_code", bp[4]);
                        sgj_js_nv_ihex(jsp, jo3p, "smart_sense_qualifier",
                                       bp[5]);
                        sgj_js_nv_ihex(jsp, jo3p, "smart_threshold", bp[6]);
                        sgj_js_nv_ihex(jsp, jo3p, "smart_trip", bp[7]);
                    }
                }
            } else {
                decoded = false;
                if (jsp->pr_as_json)
                    sgj_js_nv_ihexstr(jsp, jo3p, param_c_snake, pc, NULL,
                                      unknown_s);
            }
            break;
        }               /* end of switch statement */
        if (skip)
            skip = false;
        else if ((! decoded) && full) {
            hex2str(bp, param_len, "    ", 0 != op->dstrhex_no_ascii,
                    sizeof(b), b);
            sgj_pr_hr(jsp, "  %s = 0x%x, contents in hex:\n%s", param_c, pc,
                      b);
        }

        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2], str,
                      sizeof(str)));
skip:
        if (op->filter_given)
            break;
    }           /* end of for loop */
    return true;
}

/* called for SAS port of PROTO_SPECIFIC_LPAGE [0x18] */
static void
show_sas_phy_event_info(int pes, unsigned int val, unsigned int thresh_val)
{
    unsigned int u;

    switch (pes) {
    case 0:
        printf("     No event\n");
        break;
    case 0x1:
        printf("     Invalid word count: %u\n", val);
        break;
    case 0x2:
        printf("     Running disparity error count: %u\n", val);
        break;
    case 0x3:
        printf("     Loss of dword synchronization count: %u\n", val);
        break;
    case 0x4:
        printf("     Phy reset problem count: %u\n", val);
        break;
    case 0x5:
        printf("     Elasticity buffer overflow count: %u\n", val);
        break;
    case 0x6:
        printf("     Received ERROR  count: %u\n", val);
        break;
    case 0x7:
        printf("     Invalid SPL packet count: %u\n", val);
        break;
    case 0x8:
        printf("     Loss of SPL packet synchronization count: %u\n", val);
        break;
    case 0x20:
        printf("     Received address frame error count: %u\n", val);
        break;
    case 0x21:
        printf("     Transmitted abandon-class OPEN_REJECT count: %u\n", val);
        break;
    case 0x22:
        printf("     Received abandon-class OPEN_REJECT count: %u\n", val);
        break;
    case 0x23:
        printf("     Transmitted retry-class OPEN_REJECT count: %u\n", val);
        break;
    case 0x24:
        printf("     Received retry-class OPEN_REJECT count: %u\n", val);
        break;
    case 0x25:
        printf("     Received AIP (WAITING ON PARTIAL) count: %u\n", val);
        break;
    case 0x26:
        printf("     Received AIP (WAITING ON CONNECTION) count: %u\n", val);
        break;
    case 0x27:
        printf("     Transmitted BREAK count: %u\n", val);
        break;
    case 0x28:
        printf("     Received BREAK count: %u\n", val);
        break;
    case 0x29:
        printf("     Break timeout count: %u\n", val);
        break;
    case 0x2a:
        printf("     Connection count: %u\n", val);
        break;
    case 0x2b:
        printf("     Peak transmitted pathway blocked count: %u\n",
               val & 0xff);
        printf("         Peak value detector threshold: %u\n",
               thresh_val & 0xff);
        break;
    case 0x2c:
        u = val & 0xffff;
        if (u < 0x8000)
            printf("     Peak transmitted arbitration wait time (us): "
                   "%u\n", u);
        else
            printf("     Peak transmitted arbitration wait time (ms): "
                   "%u\n", 33 + (u - 0x8000));
        u = thresh_val & 0xffff;
        if (u < 0x8000)
            printf("         Peak value detector threshold (us): %u\n",
                   u);
        else
            printf("         Peak value detector threshold (ms): %u\n",
                   33 + (u - 0x8000));
        break;
    case 0x2d:
        printf("     Peak arbitration time (us): %u\n", val);
        printf("         Peak value detector threshold: %u\n", thresh_val);
        break;
    case 0x2e:
        printf("     Peak connection time (us): %u\n", val);
        printf("         Peak value detector threshold: %u\n", thresh_val);
        break;
    case 0x2f:
        printf("     Persistent connection count: %u\n", val);
        break;
    case 0x40:
        printf("     Transmitted SSP frame count: %u\n", val);
        break;
    case 0x41:
        printf("     Received SSP frame count: %u\n", val);
        break;
    case 0x42:
        printf("     Transmitted SSP frame error count: %u\n", val);
        break;
    case 0x43:
        printf("     Received SSP frame error count: %u\n", val);
        break;
    case 0x44:
        printf("     Transmitted CREDIT_BLOCKED count: %u\n", val);
        break;
    case 0x45:
        printf("     Received CREDIT_BLOCKED count: %u\n", val);
        break;
    case 0x50:
        printf("     Transmitted SATA frame count: %u\n", val);
        break;
    case 0x51:
        printf("     Received SATA frame count: %u\n", val);
        break;
    case 0x52:
        printf("     SATA flow control buffer overflow count: %u\n", val);
        break;
    case 0x60:
        printf("     Transmitted SMP frame count: %u\n", val);
        break;
    case 0x61:
        printf("     Received SMP frame count: %u\n", val);
        break;
    case 0x63:
        printf("     Received SMP frame error count: %u\n", val);
        break;
    default:
        printf("     Unknown phy event source: %d, val=%u, thresh_val=%u\n",
               pes, val, thresh_val);
        break;
    }
}

static const char * sas_link_rate_arr[16] = {
    "phy enabled; unknown rate",
    "phy disabled",
    "phy enabled; speed negotiation failed",
    "phy enabled; SATA spinup hold state",
    "phy enabled; port selector",
    "phy enabled; reset in progress",
    "phy enabled; unsupported phy attached",
    "reserved [0x7]",
    "1.5 Gbps",                 /* 0x8 */
    "3 Gbps",
    "6 Gbps",
    "12 Gbps",
    "22.5 Gbps",
    "reserved [0xd]",
    "reserved [0xe]",
    "reserved [0xf]",
};

static char *
sas_negot_link_rate(int lrate, char * b, int blen)
{
    int mask = 0xf;

    if (~mask & lrate)
        snprintf(b, blen, "bad link_rate value=0x%x\n", lrate);
    else
        snprintf(b, blen, "%s", sas_link_rate_arr[lrate]);
    return b;
}

/* helper for SAS port of PROTO_SPECIFIC_LPAGE [0x18] */
static void
show_sas_port_param(const uint8_t * bp, int param_len,
                    struct opts_t * op, sgj_opaque_p jop)
{
    int j, m, nphys, t, sz, spld_len;
    const uint8_t * vcp;
    uint64_t ull;
    unsigned int ui;
    char s[64];

// yyyyyyyyy
if (jop) { };
    sz = sizeof(s);
    t = sg_get_unaligned_be16(bp + 0);
    if (op->do_name)
        printf("rel_target_port=%d\n", t);
    else
        printf("relative target port id = %d\n", t);
    if (op->do_name)
        printf("  gen_code=%d\n", bp[6]);
    else
        printf("  generation code = %d\n", bp[6]);
    nphys = bp[7];
    if (op->do_name)
        printf("  num_phys=%d\n", nphys);
    else {
        printf("  number of phys = %d\n", nphys);
        if ((op->do_pcb) && (! op->do_name)) {
            char str[PCB_STR_LEN];

            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
        }
    }

    for (j = 0, vcp = bp + 8; j < (param_len - 8);
         vcp += spld_len, j += spld_len) {
        if (op->do_name)
            printf("    phy_id=%d\n", vcp[1]);
        else
            printf("  phy identifier = %d\n", vcp[1]);
        spld_len = vcp[3];
        if (spld_len < 44)
            spld_len = 48;      /* in SAS-1 and SAS-1.1 vcp[3]==0 */
        else
            spld_len += 4;
        if (op->do_name) {
            t = ((0x70 & vcp[4]) >> 4);
            printf("      att_dev_type=%d\n", t);
            printf("      att_iport_mask=0x%x\n", vcp[6]);
            printf("      att_phy_id=%d\n", vcp[24]);
            printf("      att_reason=0x%x\n", (vcp[4] & 0xf));
            ull = sg_get_unaligned_be64(vcp + 16);
            printf("      att_sas_addr=0x%" PRIx64 "\n", ull);
            printf("      att_tport_mask=0x%x\n", vcp[7]);
            ui = sg_get_unaligned_be32(vcp + 32);
            printf("      inv_dwords=%u\n", ui);
            ui = sg_get_unaligned_be32(vcp + 40);
            printf("      loss_dword_sync=%u\n", ui);
            printf("      neg_log_lrate=%d\n", 0xf & vcp[5]);
            ui = sg_get_unaligned_be32(vcp + 44);
            printf("      phy_reset_probs=%u\n", ui);
            ui = sg_get_unaligned_be32(vcp + 36);
            printf("      running_disparity=%u\n", ui);
            printf("      reason=0x%x\n", (vcp[5] & 0xf0) >> 4);
            ull = sg_get_unaligned_be64(vcp + 8);
            printf("      sas_addr=0x%" PRIx64 "\n", ull);
        } else {
            t = ((0x70 & vcp[4]) >> 4);
            /* attached SAS device type. In SAS-1.1 case 2 was an edge
             * expander; in SAS-2 case 3 is marked as obsolete. */
            switch (t) {
            case 0: snprintf(s, sz, "no device attached"); break;
            case 1: snprintf(s, sz, "SAS or SATA device"); break;
            case 2: snprintf(s, sz, "expander device"); break;
            case 3: snprintf(s, sz, "expander device (fanout)"); break;
            default: snprintf(s, sz, "reserved [%d]", t); break;
            }
            /* the word 'SAS' in following added in spl4r01 */
            printf("    attached SAS device type: %s\n", s);
            t = 0xf & vcp[4];
            switch (t) {
            case 0: snprintf(s, sz, "%s", unknown_s); break;
            case 1: snprintf(s, sz, "power on"); break;
            case 2: snprintf(s, sz, "hard reset"); break;
            case 3: snprintf(s, sz, "SMP phy control function"); break;
            case 4: snprintf(s, sz, "loss of dword synchronization"); break;
            case 5: snprintf(s, sz, "mux mix up"); break;
            case 6: snprintf(s, sz, "I_T nexus loss timeout for STP/SATA");
                break;
            case 7: snprintf(s, sz, "break timeout timer expired"); break;
            case 8: snprintf(s, sz, "phy test function stopped"); break;
            case 9: snprintf(s, sz, "expander device reduced functionality");
                 break;
            default: snprintf(s, sz, "reserved [0x%x]", t); break;
            }
            printf("    attached reason: %s\n", s);
            t = (vcp[5] & 0xf0) >> 4;
            switch (t) {
            case 0: snprintf(s, sz, "%s", unknown_s); break;
            case 1: snprintf(s, sz, "power on"); break;
            case 2: snprintf(s, sz, "hard reset"); break;
            case 3: snprintf(s, sz, "SMP phy control function"); break;
            case 4: snprintf(s, sz, "loss of dword synchronization"); break;
            case 5: snprintf(s, sz, "mux mix up"); break;
            case 6: snprintf(s, sz, "I_T nexus loss timeout for STP/SATA");
                break;
            case 7: snprintf(s, sz, "break timeout timer expired"); break;
            case 8: snprintf(s, sz, "phy test function stopped"); break;
            case 9: snprintf(s, sz, "expander device reduced functionality");
                 break;
            default: snprintf(s, sz, "reserved [0x%x]", t); break;
            }
            printf("    reason: %s\n", s);
            printf("    negotiated logical link rate: %s\n",
                   sas_negot_link_rate((0xf & vcp[5]), s, sz));
            printf("    attached initiator port: ssp=%d stp=%d smp=%d\n",
                   !! (vcp[6] & 8), !! (vcp[6] & 4), !! (vcp[6] & 2));
            printf("    attached target port: ssp=%d stp=%d smp=%d\n",
                   !! (vcp[7] & 8), !! (vcp[7] & 4), !! (vcp[7] & 2));
            ull = sg_get_unaligned_be64(vcp + 8);
            printf("    SAS address = 0x%" PRIx64 "\n", ull);
            ull = sg_get_unaligned_be64(vcp + 16);
            printf("    attached SAS address = 0x%" PRIx64 "\n", ull);
            printf("    attached phy identifier = %d\n", vcp[24]);
            if (0 == op->do_brief) {
                ui = sg_get_unaligned_be32(vcp + 32);
                printf("    Invalid DWORD count = %u\n", ui);
                ui = sg_get_unaligned_be32(vcp + 36);
                printf("    Running disparity error count = %u\n", ui);
                ui = sg_get_unaligned_be32(vcp + 40);
                printf("    Loss of DWORD synchronization count = %u\n", ui);
                ui = sg_get_unaligned_be32(vcp + 44);
                printf("    Phy reset problem count = %u\n", ui);
            }
        }
        if (op->do_brief > 0)
            continue;
        if (spld_len > 51) {
            int num_ped;
            const uint8_t * xcp;

            num_ped = vcp[51];
            if (op->verbose > 1)
                printf("    <<Phy event descriptors: %d, spld_len: %d, "
                       "calc_ped: %d>>\n", num_ped, spld_len,
                       (spld_len - 52) / 12);
            if (num_ped > 0) {
                if (op->do_name) {
                   printf("      phy_event_desc_num=%d\n", num_ped);
                   return;      /* don't decode at this stage */
                } else
                   printf("    Phy event descriptors:\n");
            }
            xcp = vcp + 52;
            for (m = 0; m < (num_ped * 12); m += 12, xcp += 12) {
                int pes = xcp[3];
                unsigned int pvdt;

                ui = sg_get_unaligned_be32(xcp + 4);
                pvdt = sg_get_unaligned_be32(xcp + 8);
                show_sas_phy_event_info(pes, ui, pvdt);
            }
        } else if (op->verbose)
           printf("    <<No phy event descriptors>>\n");
    }
}

/* PROTO_SPECIFIC_LPAGE [0x18] <psp> */
static bool
show_protocol_specific_page(const uint8_t * resp, int len,
                            struct opts_t * op, sgj_opaque_p jop)
{
    int k, num, pl, pid;
    const uint8_t * bp;

if (jop) { };
    num = len - 4;
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (op->do_name)
            printf("log_page=0x%x\n", PROTO_SPECIFIC_LPAGE);
    }
    for (k = 0, bp = resp + 4; k < num; ) {
        int pc = sg_get_unaligned_be16(bp + 0);

        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto skip;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto skip;
        }
        pid = 0xf & bp[4];
        if (6 != pid) {
            pr2serr("Protocol identifier: %d, only support SAS (SPL) which "
                    "is 6\n", pid);
            return false;   /* only decode SAS log page */
        }
        if ((0 == k) && (! op->do_name))
            printf("Protocol Specific port page for SAS SSP  (sas-2) "
                   "[0x18]\n");
        show_sas_port_param(bp, pl, op, jop);
        if (op->filter_given)
            break;
skip:
        k += pl;
        bp += pl;
    }
    return true;
}

/* Returns true if processed page, false otherwise */
/* STATS_LPAGE [0x19], subpages: 0x0 to 0x1f <gsp,grsp>  introduced: SPC-4 */
static bool
show_stats_perform_pages(const uint8_t * resp, int len,
                         struct opts_t * op, sgj_opaque_p jop)
{
    bool nam, spf;
    int k, num, param_len, param_code, subpg_code, extra;
    uint64_t ull;
    const uint8_t * bp;
    const char * ccp;
    char str[PCB_STR_LEN];

if (jop) { };
    nam = op->do_name;
    num = len - 4;
    bp = resp + 4;
    spf = !!(resp[0] & 0x40);
    subpg_code = spf ? resp[1] : 0;
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (nam) {
            printf("log_page=0x%x\n", STATS_LPAGE);
            if (subpg_code > 0)
                printf("log_subpage=0x%x\n", subpg_code);
        } else {
            if (0 == subpg_code)
                printf("General Statistics and Performance  [0x19]\n");
            else
                printf("Group Statistics and Performance (%d)  "
                       "[0x19,0x%x]\n", subpg_code, subpg_code);
        }
    }
    if (subpg_code > 31)
        return false;
    if (0 == subpg_code) { /* General statistics and performance log page */
        if (num < 0x5c)
            return false;
        for (k = num; k > 0; k -= extra, bp += extra) {
            unsigned int ui;

            if (k < 3)
                return false;
            param_len = bp[3];
            extra = param_len + 4;
            param_code = sg_get_unaligned_be16(bp + 0);
            if (op->filter_given) {
                if (param_code != op->filter)
                    continue;
            }
            if (op->do_raw) {
                dStrRaw(bp, extra);
                goto skip;
            } else if (op->do_hex) {
                hex2stdout(bp, extra, op->dstrhex_no_ascii);
                goto skip;
            }
            switch (param_code) {
            case 1:     /* Statistics and performance log parameter */
                ccp = nam ? "parameter_code=1" : "Statistics and performance "
                        "log parameter";
                printf("%s\n", ccp);
                ull = sg_get_unaligned_be64(bp + 4);
                ccp = nam ? "read_commands=" : "number of read commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 12);
                ccp = nam ? "write_commands=" : "number of write commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 20);
                ccp = nam ? "lb_received="
                          : "number of logical blocks received = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 28);
                ccp = nam ? "lb_transmitted="
                          : "number of logical blocks transmitted = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 36);
                ccp = nam ? "read_proc_intervals="
                          : "read command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 44);
                ccp = nam ? "write_proc_intervals="
                          : "write command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 52);
                ccp = nam ? "weight_rw_commands=" : "weighted number of "
                                "read commands plus write commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 60);
                ccp = nam ? "weight_rw_processing=" : "weighted read command "
                                "processing plus write command processing = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                break;
            case 2:     /* Idle time log parameter */
                ccp = nam ? "parameter_code=2" : "Idle time log parameter";
                printf("%s\n", ccp);
                ull = sg_get_unaligned_be64(bp + 4);
                ccp = nam ? "idle_time_intervals=" : "idle time "
                                "intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                break;
            case 3:     /* Time interval log parameter for general stats */
                ccp = nam ? "parameter_code=3" : "Time interval log "
                        "parameter for general stats";
                printf("%s\n", ccp);
                ui = sg_get_unaligned_be32(bp + 4);
                ccp = nam ? "time_interval_neg_exp=" : "time interval "
                                "negative exponent = ";
                printf("  %s%u\n", ccp, ui);
                ui = sg_get_unaligned_be32(bp + 8);
                ccp = nam ? "time_interval_int=" : "time interval "
                                "integer = ";
                printf("  %s%u\n", ccp, ui);
                break;
            case 4:     /* FUA statistics and performance log parameter */
                ccp = nam ? "parameter_code=4" : "Force unit access "
                        "statistics and performance log parameter ";
                printf("%s\n", ccp);
                ull = sg_get_unaligned_be64(bp + 4);
                ccp = nam ? "read_fua_commands=" : "number of read FUA "
                                "commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 12);
                ccp = nam ? "write_fua_commands=" : "number of write FUA "
                                "commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 20);
                ccp = nam ? "read_fua_nv_commands="
                          : "number of read FUA_NV commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 28);
                ccp = nam ? "write_fua_nv_commands="
                          : "number of write FUA_NV commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 36);
                ccp = nam ? "read_fua_proc_intervals="
                          : "read FUA command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 44);
                ccp = nam ? "write_fua_proc_intervals="
                          : "write FUA command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 52);
                ccp = nam ? "read_fua_nv_proc_intervals="
                          : "read FUA_NV command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 60);
                ccp = nam ? "write_fua_nv_proc_intervals="
                          : "write FUA_NV command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                break;
            case 6:     /* Time interval log parameter for cache stats */
                ccp = nam ? "parameter_code=6" : "Time interval log "
                        "parameter for cache stats";
                printf("%s\n", ccp);
                ull = sg_get_unaligned_be64(bp + 4);
                ccp = nam ? "time_interval_neg_exp=" : "time interval "
                                "negative exponent = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 8);
                ccp = nam ? "time_interval_int=" : "time interval "
                                "integer = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                break;
            default:
                if (nam) {
                    printf("parameter_code=%d\n", param_code);
                    printf("  unknown=1\n");
                } else
                    pr2serr("show_performance...  unknown parameter code "
                            "%d\n", param_code);
                if (op->verbose)
                    hex2stderr(bp, extra, 1);
                break;
            }
            if ((op->do_pcb) && (! op->do_name))
                printf("    <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
skip:
            if (op->filter_given)
                break;
        }
    } else {    /* Group statistics and performance (n) log page */
        if (num < 0x34)
            return false;
        for (k = num; k > 0; k -= extra, bp += extra) {
            if (k < 3)
                return false;
            param_len = bp[3];
            extra = param_len + 4;
            param_code = sg_get_unaligned_be16(bp + 0);
            if (op->filter_given) {
                if (param_code != op->filter)
                    continue;
            }
            if (op->do_raw) {
                dStrRaw(bp, extra);
                goto skip2;
            } else if (op->do_hex) {
                hex2stdout(bp, extra, op->dstrhex_no_ascii);
                goto skip2;
            }
            switch (param_code) {
            case 1:     /* Group n Statistics and performance log parameter */
                if (nam)
                    printf("parameter_code=1\n");
                else
                    printf("Group %d Statistics and performance log "
                           "parameter\n", subpg_code);
                ull = sg_get_unaligned_be64(bp + 4);
                ccp = nam ? "gn_read_commands=" : "group n number of read "
                                "commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 12);
                ccp = nam ? "gn_write_commands=" : "group n number of write "
                                "commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 20);
                ccp = nam ? "gn_lb_received="
                          : "group n number of logical blocks received = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 28);
                ccp = nam ? "gn_lb_transmitted="
                          : "group n number of logical blocks transmitted = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 36);
                ccp = nam ? "gn_read_proc_intervals="
                          : "group n read command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 44);
                ccp = nam ? "gn_write_proc_intervals="
                          : "group n write command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                break;
            case 4: /* Group n FUA statistics and performance log parameter */
                ccp = nam ? "parameter_code=4" : "Group n force unit access "
                        "statistics and performance log parameter";
                printf("%s\n", ccp);
                ull = sg_get_unaligned_be64(bp + 4);
                ccp = nam ? "gn_read_fua_commands="
                          : "group n number of read FUA commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 12);
                ccp = nam ? "gn_write_fua_commands="
                          : "group n number of write FUA commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 20);
                ccp = nam ? "gn_read_fua_nv_commands="
                          : "group n number of read FUA_NV commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 28);
                ccp = nam ? "gn_write_fua_nv_commands="
                          : "group n number of write FUA_NV commands = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 36);
                ccp = nam ? "gn_read_fua_proc_intervals="
                          : "group n read FUA command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 44);
                ccp = nam ? "gn_write_fua_proc_intervals=" : "group n write "
                            "FUA command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 52);
                ccp = nam ? "gn_read_fua_nv_proc_intervals=" : "group n "
                            "read FUA_NV command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                ull = sg_get_unaligned_be64(bp + 60);
                ccp = nam ? "gn_write_fua_nv_proc_intervals=" : "group n "
                            "write FUA_NV command processing intervals = ";
                printf("  %s%" PRIu64 "\n", ccp, ull);
                break;
            default:
                if (nam) {
                    printf("parameter_code=%d\n", param_code);
                    printf("  unknown=1\n");
                } else
                    pr2serr("show_performance...  unknown parameter code "
                            "%d\n", param_code);
                if (op->verbose)
                    hex2stderr(bp, extra, 1);
                break;
            }
            if ((op->do_pcb) && (! op->do_name))
                printf("    <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
skip2:
            if (op->filter_given)
                break;
        }
    }
    return true;
}

/* Returns true if processed page, false otherwise */
/* STATS_LPAGE [0x19], CACHE_STATS_SUBPG [0x20] <cms>  introduced: SPC-4 */
static bool
show_cache_stats_page(const uint8_t * resp, int len, struct opts_t * op,
                      sgj_opaque_p jop)
{
    int k, num, subpg_code, extra;
    bool nam, spf;
    unsigned int ui;
    const uint8_t * bp;
    const char * ccp;
    uint64_t ull;
    char str[PCB_STR_LEN];

if (jop) { };
    nam = op->do_name;
    num = len - 4;
    bp = resp + 4;
    if (num < 4) {
        pr2serr("badly formed Cache memory statistics page\n");
        return false;
    }
    spf = !!(resp[0] & 0x40);
    subpg_code = spf ? resp[1] : 0;
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (nam) {
            printf("log_page=0x%x\n", STATS_LPAGE);
            if (subpg_code > 0)
                printf("log_subpage=0x%x\n", subpg_code);
        } else
            printf("Cache memory statistics page  [0x19,0x20]\n");
    }

    for (k = num; k > 0; k -= extra, bp += extra) {
        int pc;

        if (k < 3) {
            pr2serr("short Cache memory statistics page\n");
            return false;
        }
        if (8 != bp[3]) {
            printf("Cache memory statistics page parameter length not "
                   "8\n");
            return false;
        }
        extra = bp[3] + 4;
        pc = sg_get_unaligned_be16(bp + 0);
        if (op->filter_given) {
            if (pc != op->filter)
                continue;
        }
        if (op->do_raw) {
            dStrRaw(bp, extra);
            goto skip;
        } else if (op->do_hex) {
            hex2stdout(bp, extra, op->dstrhex_no_ascii);
            goto skip;
        }
        switch (pc) {
        case 1:     /* Read cache memory hits log parameter */
            ccp = nam ? "parameter_code=1" :
                        "Read cache memory hits log parameter";
            printf("%s\n", ccp);
            ull = sg_get_unaligned_be64(bp + 4);
            ccp = nam ? "read_cache_memory_hits=" :
                        "read cache memory hits = ";
            printf("  %s%" PRIu64 "\n", ccp, ull);
            break;
        case 2:     /* Reads to cache memory log parameter */
            ccp = nam ? "parameter_code=2" :
                        "Reads to cache memory log parameter";
            printf("%s\n", ccp);
            ull = sg_get_unaligned_be64(bp + 4);
            ccp = nam ? "reads_to_cache_memory=" :
                        "reads to cache memory = ";
            printf("  %s%" PRIu64 "\n", ccp, ull);
            break;
        case 3:     /* Write cache memory hits log parameter */
            ccp = nam ? "parameter_code=3" :
                        "Write cache memory hits log parameter";
            printf("%s\n", ccp);
            ull = sg_get_unaligned_be64(bp + 4);
            ccp = nam ? "write_cache_memory_hits=" :
                        "write cache memory hits = ";
            printf("  %s%" PRIu64 "\n", ccp, ull);
            break;
        case 4:     /* Writes from cache memory log parameter */
            ccp = nam ? "parameter_code=4" :
                        "Writes from cache memory log parameter";
            printf("%s\n", ccp);
            ull = sg_get_unaligned_be64(bp + 4);
            ccp = nam ? "writes_from_cache_memory=" :
                        "writes from cache memory = ";
            printf("  %s%" PRIu64 "\n", ccp, ull);
            break;
        case 5:     /* Time from last hard reset log parameter */
            ccp = nam ? "parameter_code=5" :
                        "Time from last hard reset log parameter";
            printf("%s\n", ccp);
            ull = sg_get_unaligned_be64(bp + 4);
            ccp = nam ? "time_from_last_hard_reset=" :
                        "time from last hard reset = ";
            printf("  %s%" PRIu64 "\n", ccp, ull);
            break;
        case 6:     /* Time interval log parameter for cache stats */
            ccp = nam ? "parameter_code=6" :
                        "Time interval log parameter";
            printf("%s\n", ccp);
            ui = sg_get_unaligned_be32(bp + 4);
            ccp = nam ? "time_interval_neg_exp=" : "time interval "
                            "negative exponent = ";
            printf("  %s%u\n", ccp, ui);
            ui = sg_get_unaligned_be32(bp + 8);
            ccp = nam ? "time_interval_int=" : "time interval "
                            "integer = ";
            printf("  %s%u\n", ccp, ui);
            break;
        default:
            if (nam) {
                printf("parameter_code=%d\n", pc);
                printf("  unknown=1\n");
            } else
                pr2serr("show_performance...  unknown parameter code %d\n",
                        pc);
            if (op->verbose)
                hex2stderr(bp, extra, 1);
            break;
        }
        if ((op->do_pcb) && (! op->do_name))
            printf("    <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
skip:
        if (op->filter_given)
            break;
    }
    return true;
}

/* FORMAT_STATUS_LPAGE [0x8] <fs>  introduced: SBC-2 */
static bool
show_format_status_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    int k, num, pl, pc;
    bool is_count;
    const uint8_t * bp;
    const uint8_t * xp;
    uint64_t ull;
    char str[PCB_STR_LEN];
    char b[512];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Format status page  [0x8]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        is_count = true;
        switch (pc) {
        case 0:
            if (pl < 5)
                printf("  Format data out: <empty>\n");
            else {
                if (sg_all_ffs(bp + 4, pl - 4))
                    printf("  Format data out: <not available>\n");
                else {
                    hex2str(bp + 4, pl - 4, "    ", 0 != op->dstrhex_no_ascii,
                            sizeof(b), b);
                    printf("  Format data out:\n%s", b);
                }
            }
            is_count = false;
            break;
        case 1:
            printf("  Grown defects during certification");
            break;
        case 2:
            printf("  Total blocks reassigned during format");
            break;
        case 3:
            printf("  Total new blocks reassigned");
            break;
        case 4:
            printf("  Power on minutes since format");
            break;
        default:
            printf("  Unknown Format parameter code = 0x%x\n", pc);
            is_count = false;
            hex2fp(bp, pl, "    ", 0 != op->dstrhex_no_ascii, stdout);
            break;
        }
        if (is_count) {
            k = pl - 4;
            xp = bp + 4;
            if (sg_all_ffs(xp, k))
                printf(" <not available>\n");
            else {
                if (k > (int)sizeof(ull)) {
                    xp += (k - sizeof(ull));
                    k = sizeof(ull);
                }
                ull = sg_get_unaligned_be(k, xp);
                printf(" = %" PRIu64 "\n", ull);
            }
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Non-volatile cache page [0x17] <nvc>  introduced: SBC-2 */
static bool
show_non_volatile_cache_page(const uint8_t * resp, int len,
                             struct opts_t * op, sgj_opaque_p jop)
{
    int j, num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Non-volatile cache page  [0x17]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0:
            printf("  Remaining non-volatile time: ");
            if (3 == bp[4]) {
                j = sg_get_unaligned_be24(bp + 5);
                switch (j) {
                case 0:
                    printf("0 (i.e. it is now volatile)\n");
                    break;
                case 1:
                    printf("<%s>\n", unknown_s);
                    break;
                case 0xffffff:
                    printf("<indefinite>\n");
                    break;
                default:
                    printf("%d minutes [%d:%d]\n", j, (j / 60), (j % 60));
                    break;
                }
            } else
                printf("<unexpected parameter length=%d>\n", bp[4]);
            break;
        case 1:
            printf("  Maximum non-volatile time: ");
            if (3 == bp[4]) {
                j = sg_get_unaligned_be24(bp + 5);
                switch (j) {
                case 0:
                    printf("0 (i.e. it is now volatile)\n");
                    break;
                case 1:
                    printf("<reserved>\n");
                    break;
                case 0xffffff:
                    printf("<indefinite>\n");
                    break;
                default:
                    printf("%d minutes [%d:%d]\n", j, (j / 60), (j % 60));
                    break;
                }
            } else
                printf("<unexpected parameter length=%d>\n", bp[4]);
            break;
        default:
            printf("  Unknown parameter code = 0x%x\n", pc);
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* LB_PROV_LPAGE [0xc] <lbp> introduced: SBC-3 */
static bool
show_lb_provisioning_page(const uint8_t * resp, int len,
                          struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc;
    const uint8_t * bp;
    const char * cp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Logical block provisioning page  [0xc]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x1:
            cp = "  Available LBA mapping threshold";
            break;
        case 0x2:
            cp = "  Used LBA mapping threshold";
            break;
        case 0x3:
            cp = "  Available provisioning resource percentage";
            break;
        case 0x100:
            cp = "  De-duplicated LBA";
            break;
        case 0x101:
            cp = "  Compressed LBA";
            break;
        case 0x102:
            cp = "  Total efficiency LBA";
            break;
        default:
            cp = NULL;
            break;
        }
        if (cp) {
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    pr2serr("\n    truncated by response length, expected at "
                            "least 8 bytes\n");
                else
                    pr2serr("\n    parameter length >= 8 expected, got %d\n",
                            pl);
                break;
            }
            if (0x3 == pc)      /* resource percentage log parameter */
                printf("  %s: %u %%\n", cp, sg_get_unaligned_be16(bp + 4));
            else                /* resource count log parameters */
                printf("  %s resource count: %u\n", cp,
                       sg_get_unaligned_be32(bp + 4));
            if (pl > 8) {
                switch (bp[8] & 0x3) {      /* SCOPE field */
                case 0: cp = "not reported"; break;
                case 1: cp = "dedicated to lu"; break;
                case 2: cp = "not dedicated to lu"; break;
                case 3: cp = "reserved"; break;
                }
                printf("    Scope: %s\n", cp);
            }
        } else if ((pc >= 0xfff0) && (pc <= 0xffff)) {
            if (op->exclude_vendor) {
                if ((op->verbose > 0) && (0 == op->do_brief) &&
                    (! evsm_output)) {
                    evsm_output = true;
                    printf("  Vendor specific parameter(s) being ignored\n");
                }
            } else {
                printf("  Vendor specific [0x%x]:", pc);
                hex2stdout(bp, ((pl < num) ? pl : num), op->dstrhex_no_ascii);
            }
        } else {
            printf("  Reserved [parameter_code=0x%x]:\n", pc);
            hex2stdout(bp, ((pl < num) ? pl : num), op->dstrhex_no_ascii);
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* UTILIZATION_SUBPG [0xe,0x1] <util>  introduced: SBC-4 */
static bool
show_utilization_page(const uint8_t * resp, int len, struct opts_t * op,
                      sgj_opaque_p jop)
{
    int num, pl, pc, k;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Utilization page  [0xe,0x1]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            printf("  Workload utilization:");
            if ((pl < 6) || (num < 6)) {
                if (num < 6)
                    pr2serr("\n    truncated by response length, expected "
                            "at least 6 bytes\n");
                else
                    pr2serr("\n    parameter length >= 6 expected, got %d\n",
                            pl);
                break;
            }
            k = sg_get_unaligned_be16(bp + 4);
            printf(" %d.%02d %%\n", k / 100, k % 100);
            break;
        case 0x1:
            printf("  Utilization usage rate based on date and time:");
            if ((pl < 6) || (num < 6)) {
                if (num < 6)
                    pr2serr("\n    truncated by response length, expected "
                            "at least 6 bytes\n");
                else
                    pr2serr("\n    parameter length >= 6 expected, got %d\n",
                            pl);
                break;
            }
            printf(" %d %%\n", bp[4]);
            break;
        default:
            printf("  Reserved [parameter_code=0x%x]:\n", pc);
            hex2stdout(bp, ((pl < num) ? pl : num), op->dstrhex_no_ascii);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* SOLID_STATE_MEDIA_LPAGE [0x11] <ssm>  introduced: SBC-3 */
static bool
show_solid_state_media_page(const uint8_t * resp, int len,
                            struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    const uint8_t * bp;
    sgj_state * jsp = &op->json_st;
    sgj_opaque_p jo2p;
    sgj_opaque_p jo3p = NULL;
    sgj_opaque_p jap = NULL;
    char str[PCB_STR_LEN];
    static const char * ssmlp = "Solid state media log page";
    static const char * puei = "Percentage used endurance indicator";

    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        sgj_pr_hr(jsp, "%s  [0x11]\n", ssmlp);
    if (jsp->pr_as_json) {
        jo2p = sg_log_js_hdr(jsp, jop, ssmlp, resp);
        jap = sgj_named_subarray_r(jsp, jo2p,
                           "solid_state_media_log_parameters");
    }
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        if (jsp->pr_as_json) {
            jo3p = sgj_new_unattached_object_r(jsp);
            if (op->do_pcb)
                js_pcb(jsp, jo3p, bp[2]);
        }

        switch (pc) {
        case 0x1:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    pr2serr("\n    truncated by response length, expected "
                            "at least 8 bytes\n");
                else
                    pr2serr("\n    parameter length >= 8 expected, got %d\n",
                            pl);
                break;
            }
            sgj_pr_hr(jsp, "  %s: %u %%\n", puei, bp[7]);
            if (jsp->pr_as_json) {
                js_snakenv_ihexstr_nex(jsp, jo3p, param_c, pc, true,
                                       NULL, puei, NULL);
                js_snakenv_ihexstr_nex(jsp, jo3p, puei, bp[7], false,
                                       NULL, NULL, NULL);
            }
            break;
        default:
            printf("  Reserved [parameter_code=0x%x]:\n", pc);
            hex2stdout(bp, ((pl < num) ? pl : num), op->dstrhex_no_ascii);
            break;
        }
        if (jsp->pr_as_json)
            sgj_js_nv_o(jsp, jap, NULL /* name */, jo3p);
        if (op->do_pcb)
            sgj_pr_hr(jsp, "        <%s>\n", get_pcb_str(bp[2], str,
                      sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

static const char * dt_dev_activity[] = {
    "No DT device activity",
    "Cleaning operation in progress",
    "Volume is being loaded",
    "Volume is being unloaded",
    "Other medium activity",
    "Reading from medium",
    "Writing to medium",
    "Locating medium",
    "Rewinding medium", /* 8 */
    "Erasing volume",
    "Formatting volume",
    "Calibrating",
    "Other DT device activity",
    "Microcode update in progress",
    "Reading encrypted from medium",
    "Writing encrypted to medium",
    "Diagnostic operation in progress", /* 10 */
};

/* DT device status [0x11] <dtds> (ssc, adc) */
static bool
show_dt_device_status_page(const uint8_t * resp, int len,
                           struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc, j;
    const uint8_t * bp;
    char str[PCB_STR_LEN];
    char b[512];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("DT device status page (ssc-3, adc-3) [0x11]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            printf("  Very high frequency data:\n");
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    pr2serr("    truncated by response length, expected at "
                            "least 8 bytes\n");
                else
                    pr2serr("    parameter length >= 8 expected, got %d\n",
                            pl);
                break;
            }
            printf("  PAMR=%d HUI=%d MACC=%d CMPR=%d ", !!(0x80 & bp[4]),
                   !!(0x40 & bp[4]), !!(0x20 & bp[4]), !!(0x10 & bp[4]));
            printf("WRTP=%d CRQST=%d CRQRD=%d DINIT=%d\n", !!(0x8 & bp[4]),
                   !!(0x4 & bp[4]), !!(0x2 & bp[4]), !!(0x1 & bp[4]));
            printf("  INXTN=%d RAA=%d MPRSNT=%d ", !!(0x80 & bp[5]),
                   !!(0x20 & bp[5]), !!(0x10 & bp[5]));
            printf("MSTD=%d MTHRD=%d MOUNTED=%d\n",
                   !!(0x4 & bp[5]), !!(0x2 & bp[5]), !!(0x1 & bp[5]));
            printf("  DT device activity: ");
            j = bp[6];
            if (j < (int)SG_ARRAY_SIZE(dt_dev_activity))
                printf("%s\n", dt_dev_activity[j]);
            else if (j < 0x80)
                printf("Reserved [0x%x]\n", j);
            else
                printf("Vendor specific [0x%x]\n", j);
            printf("  VS=%d TDDEC=%d EPP=%d ", !!(0x80 & bp[7]),
                   !!(0x20 & bp[7]), !!(0x10 & bp[7]));
            printf("ESR=%d RRQST=%d INTFC=%d TAFC=%d\n", !!(0x8 & bp[7]),
                   !!(0x4 & bp[7]), !!(0x2 & bp[7]), !!(0x1 & bp[7]));
            break;
        case 0x1:
            printf("  Very high frequency polling delay: ");
            if ((pl < 6) || (num < 6)) {
                if (num < 6)
                    pr2serr("\n    truncated by response length, expected at "
                            "least 6 bytes\n");
                else
                    pr2serr("\n    parameter length >= 6 expected, got %d\n",
                            pl);
                break;
            }
            printf(" %d milliseconds\n", sg_get_unaligned_be16(bp + 4));
            break;
        case 0x2:
            printf("   DT device ADC data encryption control status (hex "
                   "only now):\n");
            if ((pl < 12) || (num < 12)) {
                if (num < 12)
                    pr2serr("    truncated by response length, expected at "
                            "least 12 bytes\n");
                else
                    pr2serr("    parameter length >= 12 expected, got %d\n",
                            pl);
                break;
            }
            hex2fp(bp + 4, 8, "      ", 0 != op->dstrhex_no_ascii, stdout);
            break;
        case 0x3:
            printf("   Key management error data (hex only now):\n");
            if ((pl < 16) || (num < 16)) {
                if (num < 16)
                    pr2serr("    truncated by response length, expected at "
                            "least 16 bytes\n");
                else
                    pr2serr("    parameter length >= 16 expected, got %d\n",
                            pl);
                break;
            }
            hex2fp(bp + 4, 12, "      ", 0 != op->dstrhex_no_ascii, stdout);
            break;
        default:
            if ((pc >= 0x101) && (pc <= 0x1ff)) {
                printf("  Primary port %d status:\n", pc - 0x100);
                if (12 == bp[3]) { /* if length of desc is 12, assume SAS */
                    printf("    SAS: negotiated physical link rate: %s\n",
                           sas_negot_link_rate((0xf & (bp[4] >> 4)), b,
                                               sizeof(b)));
                    printf("    signal=%d, pic=%d, ", !!(0x2 & bp[4]),
                           !!(0x1 & bp[4]));
                    printf("hashed SAS addr: 0x%u\n",
                            sg_get_unaligned_be24(bp + 5));
                    printf("    SAS addr: 0x%" PRIx64 "\n",
                            sg_get_unaligned_be64(bp + 8));
                } else {
                    printf("    non-SAS transport, in hex:\n");
                    hex2fp(bp + 4, ((pl < num) ? pl : num) - 4, "      ",
                           0 != op->dstrhex_no_ascii, stdout);
                }
            } else if (pc >= 0x8000) {
                if (op->exclude_vendor) {
                    if ((op->verbose > 0) && (0 == op->do_brief) &&
                        (! evsm_output)) {
                        evsm_output = true;
                        printf("  Vendor specific parameter(s) being "
                               "ignored\n");
                    }
                } else {
                    printf("  Vendor specific [parameter_code=0x%x]:\n", pc);
                    hex2fp(bp, ((pl < num) ? pl : num), "    ",
                           0 != op->dstrhex_no_ascii, stdout);
                }
            } else {
                printf("  Reserved [parameter_code=0x%x]:\n", pc);
                hex2fp(bp, ((pl < num) ? pl : num), "    ",
                       0 != op->dstrhex_no_ascii, stdout);
            }
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* TapeAlert response [0x12] <tar> (adc,ssc) */
static bool
show_tapealert_response_page(const uint8_t * resp, int len,
                             struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc, k, mod, div;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("TapeAlert response page (ssc-3, adc-3) [0x12]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            if (pl < 12) {

            }
            for (k = 1; k < 0x41; ++k) {
                mod = ((k - 1) % 8);
                div = (k - 1) / 8;
                if (0 == mod) {
                    if (div > 0)
                        printf("\n");
                    printf("  Flag%02Xh: %d", k, !! (bp[4 + div] & 0x80));
                } else
                    printf("  %02Xh: %d", k,
                           !! (bp[4 + div] & (1 << (7 - mod))));
            }
            printf("\n");
            break;
        default:
            if (pc <= 0x8000) {
                printf("  Reserved [parameter_code=0x%x]:\n", pc);
                hex2fp(bp, ((pl < num) ? pl : num), "    ",
                       0 != op->dstrhex_no_ascii, stdout);
            } else {
                if (op->exclude_vendor) {
                    if ((op->verbose > 0) && (0 == op->do_brief) &&
                        (! evsm_output)) {
                        evsm_output = true;
                        printf("  Vendor specific parameter(s) being "
                               "ignored\n");
                    }
                } else {
                    printf("  Vendor specific [parameter_code=0x%x]:\n", pc);
                    hex2fp(bp, ((pl < num) ? pl : num), "    ",
                           0 != op->dstrhex_no_ascii, stdout);
                }
            }
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

#define NUM_REQ_REC_ARR_ELEMS 16
static const char * req_rec_arr[NUM_REQ_REC_ARR_ELEMS] = {
    "Recovery not requested",
    "Recovery requested, no recovery procedure defined",
    "Instruct operator to push volume",
    "Instruct operator to remove and re-insert volume",
    "Issue UNLOAD command. Instruct operator to remove and re-insert volume",
    "Instruct operator to power cycle target device",
    "Issue LOAD command",
    "Issue UNLOAD command",
    "Issue LOGICAL UNIT RESET task management function",        /* 0x8 */
    "No recovery procedure defined. Contact service organization",
    "Issue UNLOAD command. Instruct operator to remove and quarantine "
        "volume",
    "Instruct operator to not insert a volume. Contact service organization",
    "Issue UNLOAD command. Instruct operator to remove volume. Contact "
        "service organization",
    "Request creation of target device error log",
    "Retrieve a target device error log",
    "Modify configuration to all microcode update and instruct operator to "
        "re-insert volume",     /* 0xf */
};

/* REQ_RECOVERY_LPAGE Requested recovery [0x13] <rr> (ssc) */
static bool
show_requested_recovery_page(const uint8_t * resp, int len,
                             struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc, j, k;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Requested recovery page (ssc-3) [0x13]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            printf("  Recovery procedures:\n");
            for (k = 4; k < pl; ++ k) {
                j = bp[k];
                if (j < NUM_REQ_REC_ARR_ELEMS)
                    printf("    %s\n", req_rec_arr[j]);
                else if (j < 0x80)
                    printf("    Reserved [0x%x]\n", j);
                else
                    printf("    Vendor specific [0x%x]\n", j);
            }
            break;
        default:
            if (pc <= 0x8000) {
                printf("  Reserved [parameter_code=0x%x]:\n", pc);
                hex2fp(bp, ((pl < num) ? pl : num), "    ",
                       0 != op->dstrhex_no_ascii, stdout);
            } else {
                if (op->exclude_vendor) {
                    if ((op->verbose > 0) && (0 == op->do_brief) &&
                        (! evsm_output)) {
                        evsm_output = true;
                        printf("  Vendor specific parameter(s) being "
                               "ignored\n");
                    }
                } else {
                    printf("  Vendor specific [parameter_code=0x%x]:\n", pc);
                    hex2fp(bp, ((pl < num) ? pl : num), "    ",
                           0 != op->dstrhex_no_ascii, stdout);
                }
            }
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* SAT_ATA_RESULTS_LPAGE (SAT-2) [0x16] <aptr> */
static bool
show_ata_pt_results_page(const uint8_t * resp, int len,
                         struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    const uint8_t * bp;
    const uint8_t * dp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("ATA pass-through results page (sat-2) [0x16]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        if ((pc < 0xf) && (pl > 17)) {
            int extend, count;

            printf("  Log_index=0x%x (parameter_code=0x%x)\n", pc + 1, pc);
            dp = bp + 4;       /* dp is start of ATA Return descriptor
                                 * which is 14 bytes long */
            extend = dp[2] & 1;
            count = dp[5] + (extend ? (dp[4] << 8) : 0);
            printf("    extend=%d  error=0x%x count=0x%x\n", extend,
                   dp[3], count);
            if (extend)
                printf("    lba=0x%02x%02x%02x%02x%02x%02x\n", dp[10], dp[8],
                       dp[6], dp[11], dp[9], dp[7]);
            else
                printf("    lba=0x%02x%02x%02x\n", dp[11], dp[9], dp[7]);
            printf("    device=0x%x  status=0x%x\n", dp[12], dp[13]);
        } else if (pl > 17) {
            printf("  Reserved [parameter_code=0x%x]:\n", pc);
            hex2fp(bp, ((pl < num) ? pl : num), "    ",
                   0 != op->dstrhex_no_ascii, stdout);
        } else {
            printf("  short parameter length: %d [parameter_code=0x%x]:\n",
                   pl, pc);
            hex2fp(bp, ((pl < num) ? pl : num), "    ",
                   0 != op->dstrhex_no_ascii, stdout);
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

static const char * bms_status[] = {
    "no background scans active",
    "background medium scan is active",
    "background pre-scan is active",
    "background scan halted due to fatal error",
    "background scan halted due to a vendor specific pattern of error",
    "background scan halted due to medium formatted without P-List",
    "background scan halted - vendor specific cause",
    "background scan halted due to temperature out of range",
    ("background scan enabled, none active (waiting for BMS interval timer "
        "to expire)"),  /* clang warns about this, add parens to quieten */
    "background scan halted - scan results list full",
    "background scan halted - pre-scan time limit timer expired" /* 10 */,
};

static const char * reassign_status[] = {
    "Reassign status: Reserved [0x0]",
    "Reassignment pending receipt of Reassign or Write command",
    "Logical block successfully reassigned by device server",
    "Reassign status: Reserved [0x3]",
    "Reassignment by device server failed",
    "Logical block recovered by device server via rewrite",
    "Logical block reassigned by application client, has valid data",
    "Logical block reassigned by application client, contains no valid data",
    "Logical block unsuccessfully reassigned by application client", /* 8 */
};

/* Background scan results [0x15,0] <bsr> for disk  introduced: SBC-3 */
static bool
show_background_scan_results_page(const uint8_t * resp, int len,
                                  struct opts_t * op, sgj_opaque_p jop)
{
    bool skip_out = false;
    bool evsm_output = false;
    int j, m, num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Background scan results page  [0x15]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0:
            printf("  Status parameters:\n");
            if ((pl < 16) || (num < 16)) {
                if (num < 16)
                    pr2serr("    truncated by response length, expected at "
                            "least 16 bytes\n");
                else
                    pr2serr("    parameter length >= 16 expected, got %d\n",
                            pl);
                break;
            }
            printf("    Accumulated power on minutes: ");
            j = sg_get_unaligned_be32(bp + 4);
            printf("%d [h:m  %d:%d]\n", j, (j / 60), (j % 60));
            printf("    Status: ");
            j = bp[9];
            if (j < (int)SG_ARRAY_SIZE(bms_status))
                printf("%s\n", bms_status[j]);
            else
                printf("unknown [0x%x] background scan status value\n", j);
            j = sg_get_unaligned_be16(bp + 10);
            printf("    Number of background scans performed: %d\n", j);
            j = sg_get_unaligned_be16(bp + 12);
#ifdef SG_LIB_MINGW
            printf("    Background medium scan progress: %g %%\n",
                   (double)(j * 100.0 / 65536.0));
#else
            printf("    Background medium scan progress: %.2f %%\n",
                   (double)(j * 100.0 / 65536.0));
#endif
            j = sg_get_unaligned_be16(bp + 14);
            if (0 == j)
                printf("    Number of background medium scans performed: 0 "
                       "[not reported]\n");
            else
                printf("    Number of background medium scans performed: "
                       "%d\n", j);
            break;
        default:
            if (pc > 0x800) {
                if ((pc >= 0x8000) && (pc <= 0xafff)) {
                    if (op->exclude_vendor) {
                        skip_out = true;
                        if ((op->verbose > 0) && (0 == op->do_brief) &&
                            (! evsm_output)) {
                            evsm_output = true;
                            printf("  Vendor specific parameter(s) being "
                                   "ignored\n");
                        }
                    } else
                        printf("  Medium scan parameter # %d [0x%x], vendor "
                               "specific\n", pc, pc);
                } else
                    printf("  Medium scan parameter # %d [0x%x], "
                           "reserved\n", pc, pc);
                if (skip_out)
                    skip_out = false;
                else
                    hex2fp(bp, ((pl < num) ? pl : num), "    ",
                           0 != op->dstrhex_no_ascii, stdout);
                break;
            } else
                printf("  Medium scan parameter # %d [0x%x]\n", pc, pc);
            if ((pl < 24) || (num < 24)) {
                if (num < 24)
                    pr2serr("    truncated by response length, expected at "
                            "least 24 bytes\n");
                else
                    pr2serr("    parameter length >= 24 expected, got %d\n",
                            pl);
                break;
            }
            printf("    Power on minutes when error detected: ");
            j = sg_get_unaligned_be32(bp + 4);
            printf("%d [%d:%d]\n", j, (j / 60), (j % 60));
            j = (bp[8] >> 4) & 0xf;
            if (j < (int)SG_ARRAY_SIZE(reassign_status))
                printf("    %s\n", reassign_status[j]);
            else
                printf("    Reassign status: reserved [0x%x]\n", j);
            printf("    sense key: %s  [sk,asc,ascq: 0x%x,0x%x,0x%x]\n",
                   sg_get_sense_key_str(bp[8] & 0xf, sizeof(str), str),
                   bp[8] & 0xf, bp[9], bp[10]);
            if (bp[9] || bp[10])
                printf("      %s\n", sg_get_asc_ascq_str(bp[9], bp[10],
                                                         sizeof(str), str));
            if (op->verbose) {
                printf("    vendor bytes [11 -> 15]: ");
                for (m = 0; m < 5; ++m)
                    printf("0x%02x ", bp[11 + m]);
                printf("\n");
            }
            printf("    LBA (associated with medium error): 0x");
            if (sg_all_zeros(bp + 16, 8))
                printf("0\n");
            else {
                for (m = 0; m < 8; ++m)
                    printf("%02x", bp[16 + m]);
                printf("\n");
            }
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* ZONED_BLOCK_DEV_STATS_SUBPG [0x14,0x1] <zbds>  introduced: zbc2r01 */
static bool
show_zoned_block_dev_stats(const uint8_t * resp, int len,
                           struct opts_t * op, sgj_opaque_p jop)
{
    bool trunc, bad_pl;
    int num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Zoned block device statistics page  [0x14,0x1]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        trunc = false;
        bad_pl = false;
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (4 == pl)    /* DC HC560 has empty descriptors */
            goto skip;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Maximum open zones: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x1:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Maximum explicitly open zones: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x2:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Maximum implicitly open zones: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x3:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Minimum empty zones: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x4:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Maximum non-sequential zones: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x5:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Zones emptied: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x6:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Suboptimal write commands: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x7:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Commands exceeding optimal limit: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x8:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Failed explicit opens: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0x9:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Read rule violations: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0xa:
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Write rule violations: %" PRIu32 "\n",
                       sg_get_unaligned_be32(bp + 8));
            break;
        case 0xb:       /* added zbc2r04 */
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    trunc = true;
                else
                    bad_pl = true;
            } else
                printf("  Maximum implicitly open or before required zones: "
                       "%" PRIu32 "\n", sg_get_unaligned_be32(bp + 8));
            break;
        default:
            printf("  Reserved [parameter_code=0x%x]:\n", pc);
            hex2fp(bp, ((pl < num) ? pl : num), "    ",
                   0 != op->dstrhex_no_ascii, stdout);
            break;
        }
        if (trunc)
            pr2serr("    truncated by response length, expected at least "
                    "8 bytes\n");
        if (bad_pl)
            pr2serr("    parameter length >= 8 expected, got %d\n", pl);
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* PENDING_DEFECTS_SUBPG [0x15,0x1] <pd>  introduced: SBC-4 */
static bool
show_pending_defects_page(const uint8_t * resp, int len,
                          struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    uint32_t count;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Pending defects page  [0x15,0x1]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            printf("  Pending defect count: ");
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    pr2serr("\n    truncated by response length, expected "
                            "at least 8 bytes\n");
                else
                    pr2serr("\n    parameter length >= 8 expected, got %d\n",
                            pl);
                break;
            }
            count = sg_get_unaligned_be32(bp + 4);
            if (0 == count) {
                printf("0\n");
                break;
            }
            printf("%3u  |     LBA            Accumulated power_on\n", count);
            printf("-----------------------------|---------------");
            printf("-----------hours---------\n");
            break;
        default:
            printf("  Pending defect %4d:  ", pc);
            if ((pl < 16) || (num < 16)) {
                if (num < 16)
                    pr2serr("\n    truncated by response length, expected "
                            "at least 16 bytes\n");
                else
                    pr2serr("\n    parameter length >= 16 expected, got %d\n",
                            pl);
                break;
            }
            printf("        0x%-16" PRIx64 "      %5u\n",
                   sg_get_unaligned_be64(bp + 8),
                   sg_get_unaligned_be32(bp + 4));
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* BACKGROUND_OP_SUBPG [0x15,0x2] <bop>  introduced: SBC-4 rev 7 */
static bool
show_background_op_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Background operation page  [0x15,0x2]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            printf("  Background operation:");
            if ((pl < 8) || (num < 8)) {
                if (num < 8)
                    pr2serr("\n    truncated by response length, expected "
                            "at least 8 bytes\n");
                else
                    pr2serr("\n    parameter length >= 8 expected, got %d\n",
                            pl);
                break;
            }
            printf(" BO_STATUS=%d\n", bp[4]);
            break;
        default:
            printf("  Reserved [parameter_code=0x%x]:\n", pc);
            hex2fp(bp, ((pl < num) ? pl : num), "    ",
                   0 != op->dstrhex_no_ascii, stdout);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* LPS misalignment page [0x15,0x3] <lps>  introduced: SBC-4 rev 10
   LPS: "Long Physical Sector" a term from an ATA feature set */
static bool
show_lps_misalignment_page(const uint8_t * resp, int len,
                           struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("LPS misalignment page  [0x15,0x3]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0x0:
            printf("  LPS misalignment count: ");
            if (4 == bp[3])
                printf("max lpsm: %" PRIu16 ", count=%" PRIu16 "\n",
                       sg_get_unaligned_be16(bp + 4),
                       sg_get_unaligned_be16(bp + 6));
            else
                printf("<unexpected pc=0 parameter length=%d>\n", bp[4]);
            break;
        default:
            if (pc <= 0xf000) {         /* parameter codes 0x1 to 0xf000 */
                if (8 == bp[3])
                    printf("  LBA of misaligned block: 0x%" PRIx64 "\n",
                           sg_get_unaligned_be64(bp + 4));
                else
                    printf("<unexpected pc=0x%x parameter length=%d>\n",
                           pc, bp[4]);
            } else {
                printf("<unexpected pc=0x%x>\n", pc);
                hex2fp(bp, ((pl < num) ? pl : num), "    ",
                       0 != op->dstrhex_no_ascii, stdout);
            }
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Service buffer information [0x15] <sbi> (adc) */
static bool
show_service_buffer_info_page(const uint8_t * resp, int len,
                              struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Service buffer information page (adc-3) [0x15]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        if (pc < 0x100) {
            printf("  Service buffer identifier: 0x%x\n", pc);
            printf("    Buffer id: 0x%x, tu=%d, nmp=%d, nmm=%d, "
                   "offline=%d\n", bp[4], !!(0x10 & bp[5]),
                   !!(0x8 & bp[5]), !!(0x4 & bp[5]), !!(0x2 & bp[5]));
            printf("    pd=%d, code_set: %s, Service buffer title:\n",
                   !!(0x1 & bp[5]), sg_get_desig_code_set_str(0xf & bp[6]));
            printf("      %.*s\n", pl - 8, bp + 8);
        } else if (pc < 0x8000) {
            printf("  parameter_code=0x%x, Reserved, parameter in hex:\n",
                   pc);
            hex2fp(bp + 4, pl - 4, "    ", 0 != op->dstrhex_no_ascii, stdout);
        } else {
            if (op->exclude_vendor) {
                if ((op->verbose > 0) && (0 == op->do_brief) &&
                    (! evsm_output)) {
                    evsm_output = true;
                    printf("  Vendor specific parameter(s) being "
                           "ignored\n");
                }
            } else {
                printf("  parameter_code=0x%x, Vendor-specific, parameter in "
                       "hex:\n", pc);
                hex2fp(bp + 4, pl - 4, "    ", 0 != op->dstrhex_no_ascii,
                       stdout);
            }
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Sequential access device page [0xc] <sad> for tape */
static bool
show_sequential_access_page(const uint8_t * resp, int len,
                            struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc;
    const uint8_t * bp;
    uint64_t ull, gbytes;
    bool all_set;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Sequential access device page (ssc-3)\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        ull = sg_get_unaligned_be(pl - 4, bp + 4);
        all_set = sg_all_ffs(bp + 4, pl - 4);
        gbytes = ull / 1000000000;
        switch (pc) {
        case 0:
            printf("  Data bytes received with WRITE commands: %" PRIu64
                   " GB", gbytes);
            if (op->verbose)
                printf(" [%" PRIu64 " bytes]", ull);
            printf("\n");
            break;
        case 1:
            printf("  Data bytes written to media by WRITE commands: %" PRIu64
                   " GB", gbytes);
            if (op->verbose)
                printf(" [%" PRIu64 " bytes]", ull);
            printf("\n");
            break;
        case 2:
            printf("  Data bytes read from media by READ commands: %" PRIu64
                   " GB", gbytes);
            if (op->verbose)
                printf(" [%" PRIu64 " bytes]", ull);
            printf("\n");
            break;
        case 3:
            printf("  Data bytes transferred by READ commands: %" PRIu64
                   " GB", gbytes);
            if (op->verbose)
                printf(" [%" PRIu64 " bytes]", ull);
            printf("\n");
            break;
        case 4:
            if (! all_set)
                printf("  Native capacity from BOP to EOD: %" PRIu64 " MB\n",
                       ull);
            break;
        case 5:
            if (! all_set)
                printf("  Native capacity from BOP to EW of current "
                       "partition: %" PRIu64 " MB\n", ull);
            break;
        case 6:
            if (! all_set)
                printf("  Minimum native capacity from EW to EOP of current "
                       "partition: %" PRIu64 " MB\n", ull);
            break;
        case 7:
            if (! all_set)
                printf("  Native capacity from BOP to current position: %"
                       PRIu64 " MB\n", ull);
            break;
        case 8:
            if (! all_set)
                printf("  Maximum native capacity in device object buffer: %"
                       PRIu64 " MB\n", ull);
            break;
        case 0x100:
            if (ull > 0)
                printf("  Cleaning action required\n");
            else
                printf("  Cleaning action not required (or completed)\n");
            if (op->verbose)
                printf("    cleaning value: %" PRIu64 "\n", ull);
            break;
        default:
            if (pc >= 0x8000) {
                if (op->exclude_vendor) {
                    if ((op->verbose > 0) && (0 == op->do_brief) &&
                        (! evsm_output)) {
                        evsm_output = true;
                        printf("  Vendor specific parameter(s) being "
                               "ignored\n");
                    }
                } else
                    printf("  Vendor specific parameter [0x%x] value: %"
                           PRIu64 "\n", pc, ull);
            } else
                printf("  Reserved parameter [0x%x] value: %" PRIu64 "\n",
                       pc, ull);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Device statistics 0x14 <ds> for tape and ADC */
static bool
show_device_stats_page(const uint8_t * resp, int len,
                       struct opts_t * op, sgj_opaque_p jop)
{
    bool evsm_output = false;
    int num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Device statistics page (ssc-3 and adc)\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
             goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
             goto filter_chk;
        }
        if (pc < 0x1000) {
            bool vl_num = true;

            switch (pc) {
            case 0:
                printf("  Lifetime media loads:");
                break;
            case 1:
                printf("  Lifetime cleaning operations:");
                break;
            case 2:
                printf("  Lifetime power on hours:");
                break;
            case 3:
                printf("  Lifetime media motion (head) hours:");
                break;
            case 4:
                printf("  Lifetime metres of tape processed:");
                break;
            case 5:
                printf("  Lifetime media motion (head) hours when "
                       "incompatible media last loaded:");
                break;
            case 6:
                printf("  Lifetime power on hours when last temperature "
                       "condition occurred:");
                break;
            case 7:
                printf("  Lifetime power on hours when last power "
                       "consumption condition occurred:");
                break;
            case 8:
                printf("  Media motion (head) hours since last successful "
                       "cleaning operation:");
                break;
            case 9:
                printf("  Media motion (head) hours since 2nd to last "
                       "successful cleaning:");
                break;
            case 0xa:
                printf("  Media motion (head) hours since 3rd to last "
                       "successful cleaning:");
                break;
            case 0xb:
                printf("  Lifetime power on hours when last operator "
                       "initiated forced reset\n    and/or emergency "
                       "eject occurred:");
                break;
            case 0xc:
                printf("  Lifetime power cycles:");
                break;
            case 0xd:
                printf("  Volume loads since last parameter reset:");
                break;
            case 0xe:
                printf("  Hard write errors:");
                break;
            case 0xf:
                printf("  Hard read errors:");
                break;
            case 0x10:
                printf("  Duty cycle sample time (ms):");
                break;
            case 0x11:
                printf("  Read duty cycle:");
                break;
            case 0x12:
                printf("  Write duty cycle:");
                break;
            case 0x13:
                printf("  Activity duty cycle:");
                break;
            case 0x14:
                printf("  Volume not present duty cycle:");
                break;
            case 0x15:
                printf("  Ready duty cycle:");
                break;
            case 0x16:
                printf("  MBs transferred from app client in duty cycle "
                       "sample time:");
                break;
            case 0x17:
                printf("  MBs transferred to app client in duty cycle "
                       "sample time:");
                break;
            case 0x40:
                printf("  Drive manufacturer's serial number:");
                break;
            case 0x41:
                printf("  Drive serial number:");
                break;
            case 0x42:          /* added ssc5r02b */
                vl_num = false;
                printf("  Manufacturing date (yyyymmdd): %.*s\n", pl - 4,
                       bp + 4);
                break;
            case 0x43:          /* added ssc5r02b */
                vl_num = false;
                printf("  Manufacturing date (yyyyww): %.*s\n", pl - 4,
                       bp + 4);
                break;
            case 0x80:
                printf("  Medium removal prevented:");
                break;
            case 0x81:
                printf("  Maximum recommended mechanism temperature "
                       "exceeded:");
                break;
            default:
                vl_num = false;
                printf("  Reserved parameter code [0x%x] data in hex:\n", pc);
                hex2fp(bp + 4, pl - 4, "    ", 0 != op->dstrhex_no_ascii,
                       stdout);
                break;
            }
            if (vl_num)
                printf(" %" PRIu64 "\n", sg_get_unaligned_be(pl - 4, bp + 4));
        } else {        /* parameter_code >= 0x1000 */
            int k;
            const uint8_t * p = bp + 4;

            switch (pc) {
            case 0x1000:
                printf("  Media motion (head) hours for each medium type:\n");
                for (k = 0; ((pl - 4) - k) >= 8; k += 8, p += 8)
                    printf("    [%d] Density code: %u, Medium type: 0x%x, "
                           "hours: %u\n", ((k / 8) + 1), p[2], p[3],
                           sg_get_unaligned_be32(p + 4));
                break;
            default:
                if (pc >= 0x8000) {
                    if (op->exclude_vendor) {
                        if ((op->verbose > 0) && (0 == op->do_brief) &&
                            (! evsm_output)) {
                            evsm_output = true;
                            printf("  Vendor specific parameter(s) being "
                                   "ignored\n");
                        }
                    } else
                        printf("  Vendor specific parameter [0x%x], dump in "
                               "hex:\n", pc);
                } else {
                    printf("  Reserved parameter [0x%x], dump in hex:\n", pc);
                    hex2fp(bp + 4, pl - 4, "    ", 0 != op->dstrhex_no_ascii,
                           stdout);
                }
                break;
            }
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Media changer statistics 0x14 <mcs> for media changer */
static bool
show_media_stats_page(const uint8_t * resp, int len, struct opts_t * op,
                      sgj_opaque_p jop)
{
    int num, pl, pc;
    const uint8_t * bp;
    uint64_t ull;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Media statistics page (smc-3)\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        ull = sg_get_unaligned_be(pl - 4, bp + 4);
        switch (pc) {
        case 0:
            printf("  Number of moves: %" PRIu64 "\n", ull);
            break;
        case 1:
            printf("  Number of picks: %" PRIu64 "\n", ull);
            break;
        case 2:
            printf("  Number of pick retries: %" PRIu64 "\n", ull);
            break;
        case 3:
            printf("  Number of places: %" PRIu64 "\n", ull);
            break;
        case 4:
            printf("  Number of place retries: %" PRIu64 "\n", ull);
            break;
        case 5:
            printf("  Number of volume tags read by volume "
                   "tag reader: %" PRIu64 "\n", ull);
            break;
        case 6:
            printf("  Number of invalid volume tags returned by "
                   "volume tag reader: %" PRIu64 "\n", ull);
            break;
        case 7:
            printf("  Number of library door opens: %" PRIu64 "\n", ull);
            break;
        case 8:
            printf("  Number of import/export door opens: %" PRIu64 "\n",
                   ull);
            break;
        case 9:
            printf("  Number of physical inventory scans: %" PRIu64 "\n",
                   ull);
            break;
        case 0xa:
            printf("  Number of medium transport unrecovered errors: "
                   "%" PRIu64 "\n", ull);
            break;
        case 0xb:
            printf("  Number of medium transport recovered errors: "
                   "%" PRIu64 "\n", ull);
            break;
        case 0xc:
            printf("  Number of medium transport X axis translation "
                   "unrecovered errors: %" PRIu64 "\n", ull);
            break;
        case 0xd:
            printf("  Number of medium transport X axis translation "
                   "recovered errors: %" PRIu64 "\n", ull);
            break;
        case 0xe:
            printf("  Number of medium transport Y axis translation "
                   "unrecovered errors: %" PRIu64 "\n", ull);
            break;
        case 0xf:
            printf("  Number of medium transport Y axis translation "
                   "recovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x10:
            printf("  Number of medium transport Z axis translation "
                   "unrecovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x11:
            printf("  Number of medium transport Z axis translation "
                   "recovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x12:
            printf("  Number of medium transport rotational translation "
                   "unrecovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x13:
            printf("  Number of medium transport rotational translation "
                   "recovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x14:
            printf("  Number of medium transport inversion translation "
                   "unrecovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x15:
            printf("  Number of medium transport inversion translation "
                   "recovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x16:
            printf("  Number of medium transport auxiliary translation "
                   "unrecovered errors: %" PRIu64 "\n", ull);
            break;
        case 0x17:
            printf("  Number of medium transport auxiliary translation "
                   "recovered errors: %" PRIu64 "\n", ull);
            break;
        default:
            printf("  Reserved parameter [0x%x] value: %" PRIu64 "\n",
                   pc, ull);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Element statistics page, 0x15 <els> for SMC */
static bool
show_element_stats_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    unsigned int v;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Element statistics page (smc-3) [0x15]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        printf("  Element address: %d\n", pc);
        v = sg_get_unaligned_be32(bp + 4);
        printf("    Number of places: %u\n", v);
        v = sg_get_unaligned_be32(bp + 8);
        printf("    Number of place retries: %u\n", v);
        v = sg_get_unaligned_be32(bp + 12);
        printf("    Number of picks: %u\n", v);
        v = sg_get_unaligned_be32(bp + 16);
        printf("    Number of pick retries: %u\n", v);
        v = sg_get_unaligned_be32(bp + 20);
        printf("    Number of determined volume identifiers: %u\n", v);
        v = sg_get_unaligned_be32(bp + 24);
        printf("    Number of unreadable volume identifiers: %u\n", v);
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Tape diagnostic data [0x16] <tdd> for tape */
static bool
show_tape_diag_data_page(const uint8_t * resp, int len,
                         struct opts_t * op, sgj_opaque_p jop)
{
    int k, n, num, pl, pc;
    unsigned int v;
    const uint8_t * bp;
    char str[PCB_STR_LEN];
    char b[512];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Tape diagnostics data page (ssc-3) [0x16]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        printf("  Parameter code: %d\n", pc);
        printf("    Density code: 0x%x\n", bp[6]);
        printf("    Medium type: 0x%x\n", bp[7]);
        v = sg_get_unaligned_be32(bp + 8);
        printf("    Lifetime media motion hours: %u\n", v);
        printf("    Repeat: %d\n", !!(bp[13] & 0x80));
        v = bp[13] & 0xf;
        printf("    Sense key: 0x%x [%s]\n", v,
               sg_get_sense_key_str(v, sizeof(b), b));
        printf("    Additional sense code: 0x%x\n", bp[14]);
        printf("    Additional sense code qualifier: 0x%x\n", bp[15]);
        if (bp[14] || bp[15])
            printf("      [%s]\n", sg_get_asc_ascq_str(bp[14], bp[15],
                   sizeof(b), b));
        v = sg_get_unaligned_be32(bp + 16);
        printf("    Vendor specific code qualifier: 0x%x\n", v);
        v = sg_get_unaligned_be32(bp + 20);
        printf("    Product revision level: %u\n", v);
        v = sg_get_unaligned_be32(bp + 24);
        printf("    Hours since last clean: %u\n", v);
        printf("    Operation code: 0x%x\n", bp[28]);
        printf("    Service action: 0x%x\n", bp[29] & 0xf);
        // Check Medium id number for all zeros
        // ssc4r03.pdf does not define this field, why? xxxxxx
        if (sg_all_zeros(bp + 32, 32))
            printf("    Medium id number is 32 bytes of zero\n");
        else {
            hex2str(bp + 32, 32, "      ", 0 /* with ASCII */, sizeof(b), b);
            printf("    Medium id number (in hex):\n%s", b);
        }
        printf("    Timestamp origin: 0x%x\n", bp[64] & 0xf);
        // Check Timestamp for all zeros
        if (sg_all_zeros(bp + 66, 6))
            printf("    Timestamp is all zeros:\n");
        else {
            hex2str(bp + 66, 6, NULL, 0 != op->dstrhex_no_ascii,
                    sizeof(b), b);
            printf("    Timestamp: %s", b);
        }
        if (pl > 72) {
            n = pl - 72;
            k = hex2str(bp + 72, n, "      ", 0 != op->dstrhex_no_ascii,
                        sizeof(b), b);
            printf("    Vendor specific:\n");
            printf("%s", b);
            if (k >= (int)sizeof(b) - 1)
                printf("      <truncated>\n");
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Media changer diagnostic data [0x16] <mcdd> for media changer */
static bool
show_mchanger_diag_data_page(const uint8_t * resp, int len,
                             struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc;
    unsigned int v;
    const uint8_t * bp;
    char str[PCB_STR_LEN];
    char b[512];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Media changer diagnostics data page (smc-3) [0x16]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        printf("  Parameter code: %d\n", pc);
        printf("    Repeat: %d\n", !!(bp[5] & 0x80));
        v = bp[5] & 0xf;
        printf("    Sense key: 0x%x [%s]\n", v,
               sg_get_sense_key_str(v, sizeof(b), b));
        printf("    Additional sense code: 0x%x\n", bp[6]);
        printf("    Additional sense code qualifier: 0x%x\n", bp[7]);
        if (bp[6] || bp[7])
            printf("      [%s]\n", sg_get_asc_ascq_str(bp[6], bp[7],
                   sizeof(b), b));
        v = sg_get_unaligned_be32(bp + 8);
        printf("    Vendor specific code qualifier: 0x%x\n", v);
        v = sg_get_unaligned_be32(bp + 12);
        printf("    Product revision level: %u\n", v);
        v = sg_get_unaligned_be32(bp + 16);
        printf("    Number of moves: %u\n", v);
        v = sg_get_unaligned_be32(bp + 20);
        printf("    Number of pick: %u\n", v);
        v = sg_get_unaligned_be32(bp + 24);
        printf("    Number of pick retries: %u\n", v);
        v = sg_get_unaligned_be32(bp + 28);
        printf("    Number of places: %u\n", v);
        v = sg_get_unaligned_be32(bp + 32);
        printf("    Number of place retries: %u\n", v);
        v = sg_get_unaligned_be32(bp + 36);
        printf("    Number of determined volume identifiers: %u\n", v);
        v = sg_get_unaligned_be32(bp + 40);
        printf("    Number of unreadable volume identifiers: %u\n", v);
        printf("    Operation code: 0x%x\n", bp[44]);
        printf("    Service action: 0x%x\n", bp[45] & 0xf);
        printf("    Media changer error type: 0x%x\n", bp[46]);
        printf("    MTAV: %d\n", !!(bp[47] & 0x8));
        printf("    IAV: %d\n", !!(bp[47] & 0x4));
        printf("    LSAV: %d\n", !!(bp[47] & 0x2));
        printf("    DAV: %d\n", !!(bp[47] & 0x1));
        v = sg_get_unaligned_be16(bp + 48);
        printf("    Medium transport address: 0x%x\n", v);
        v = sg_get_unaligned_be16(bp + 50);
        printf("    Initial address: 0x%x\n", v);
        v = sg_get_unaligned_be16(bp + 52);
        printf("    Last successful address: 0x%x\n", v);
        v = sg_get_unaligned_be16(bp + 54);
        printf("    Destination address: 0x%x\n", v);
        if (pl > 91) {
            printf("    Volume tag information:\n");
            hex2fp(bp + 56, 36, "    ", 0 != op->dstrhex_no_ascii, stdout);
        }
        if (pl > 99) {
            printf("    Timestamp origin: 0x%x\n", bp[92] & 0xf);
            printf("    Timestamp:\n");
            hex2fp(bp + 94, 6, "    ", 0 != op->dstrhex_no_ascii, stdout);
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* Helper for show_volume_stats_pages() */
static void
volume_stats_partition(const uint8_t * xp, int len, bool in_hex)
{
    uint64_t ull;

    while (len > 3) {
        bool all_ffs, ffs_last_fe;
        int dl, pn;

        dl = xp[0] + 1;
        if (dl < 3)
            return;
        pn = sg_get_unaligned_be16(xp + 2);
        ffs_last_fe = false;
        all_ffs = false;
        if (sg_all_ffs(xp + 4, dl - 3)) {
            switch (xp[4 + dl - 3]) {
            case 0xff:
                all_ffs = true;
                break;
            case 0xfe:
                ffs_last_fe = true;
                break;
            default:
                break;
            }
        }
        if (! (all_ffs || ffs_last_fe)) {
            ull = sg_get_unaligned_be(dl - 4, xp + 4);
            if (in_hex)
                printf("    partition number: %d, partition record data "
                       "counter: 0x%" PRIx64 "\n", pn, ull);
            else
                printf("    partition number: %d, partition record data "
                       "counter: %" PRIu64 "\n", pn, ull);
        } else if (all_ffs)
            printf("    partition number: %d, partition record data "
                   "counter is all 0xFFs\n", pn);
        else    /* ffs_last_fe is true */
            printf("    partition number: %d, partition record data "
                   "counter is all 0xFFs apart\n    from a trailing "
                   "0xFE\n", pn);
        xp += dl;
        len -= dl;
    }
}

/* Helper for show_volume_stats_pages() */
static void
volume_stats_history(const uint8_t * xp, int len)
{
    while (len > 3) {
        int dl, mhi;

        dl = xp[0] + 1;
        if (dl < 4)
            return;
        mhi = sg_get_unaligned_be16(xp + 2);
        if (dl < 12)
            printf("    index: %d\n", mhi);
        else if (12 == dl)
            printf("    index: %d, vendor: %.8s\n", mhi, xp + 4);
        else
            printf("    index: %d, vendor: %.8s, unit serial number: %.*s\n",
                   mhi, xp + 4, dl - 12, xp + 12);
        xp += dl;
        len -= dl;
    }
}

/* Volume Statistics log page and subpages (ssc-4) [0x17, 0x0-0xf] <vs> */
static bool
show_volume_stats_pages(const uint8_t * resp, int len,
                       struct opts_t * op, sgj_opaque_p jop)
{
    bool skip_out = false;
    bool evsm_output = false;
    int num, pl, pc, subpg_code;
    bool spf;
    const uint8_t * bp;
    char str[PCB_STR_LEN];
    char b[512];

if (jop) { };
    spf = !!(resp[0] & 0x40);
    subpg_code = spf ? resp[1] : 0;
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (subpg_code < 0x10)
            printf("Volume statistics page (ssc-4), subpage=%d\n",
                   subpg_code);
        else {
            printf("Volume statistics page (ssc-4), subpage=%d; Reserved, "
                   "skip\n", subpg_code);
            return false;
        }
    }
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }

        switch (pc) {
        case 0:
            printf("  Page valid: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 1:
            printf("  Thread count: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 2:
            printf("  Total data sets written: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 3:
            printf("  Total write retries: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 4:
            printf("  Total unrecovered write errors: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 5:
            printf("  Total suspended writes: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 6:
            printf("  Total fatal suspended writes: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 7:
            printf("  Total data sets read: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 8:
            printf("  Total read retries: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 9:
            printf("  Total unrecovered read errors: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0xa:
            printf("  Total suspended reads: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0xb:
            printf("  Total fatal suspended reads: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0xc:
            printf("  Last mount unrecovered write errors: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0xd:
            printf("  Last mount unrecovered read errors: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0xe:
            printf("  Last mount megabytes written: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0xf:
            printf("  Last mount megabytes read: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x10:
            printf("  Lifetime megabytes written: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x11:
            printf("  Lifetime megabytes read: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x12:
            printf("  Last load write compression ratio: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x13:
            printf("  Last load read compression ratio: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x14:
            printf("  Medium mount time: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x15:
            printf("  Medium ready time: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x16:
            printf("  Total native capacity [MB]: %s\n",
                   num_or_unknown(bp + 4, pl - 4, false, b, sizeof(b)));
            break;
        case 0x17:
            printf("  Total used native capacity [MB]: %s\n",
                   num_or_unknown(bp + 4, pl - 4, false, b, sizeof(b)));
            break;
        case 0x1a:
            printf("  Volume stop writes of forward wraps: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x1b:
            printf("  Volume stop writes of backward wraps: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x40:
            printf("  Volume serial number: %.*s\n", pl - 4, bp + 4);
            break;
        case 0x41:
            printf("  Tape lot identifier: %.*s\n", pl - 4, bp + 4);
            break;
        case 0x42:
            printf("  Volume barcode: %.*s\n", pl - 4, bp + 4);
            break;
        case 0x43:
            printf("  Volume manufacturer: %.*s\n", pl - 4, bp + 4);
            break;
        case 0x44:
            printf("  Volume license code: %.*s\n", pl - 4, bp + 4);
            break;
        case 0x45:
            printf("  Volume personality: %.*s\n", pl - 4, bp + 4);
            break;
        case 0x80:
            printf("  Write protect: %s\n",
                   num_or_unknown(bp + 4, pl - 4, false, b, sizeof(b)));
            break;
        case 0x81:
            printf("  WORM: %s\n",
                   num_or_unknown(bp + 4, pl - 4, false, b, sizeof(b)));
            break;
        case 0x82:
            printf("  Maximum recommended tape path temperature exceeded: "
                   "%s\n", num_or_unknown(bp + 4, pl - 4, false, b,
                                          sizeof(b)));
            break;
        case 0x100:
            printf("  Volume write mounts: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x101:
            printf("  Beginning of medium passes: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x102:
            printf("  Middle of medium passes: %" PRIu64 "\n",
                   sg_get_unaligned_be(pl - 4, bp + 4));
            break;
        case 0x200:
            printf("  Logical position of first encrypted logical object:\n");
            volume_stats_partition(bp + 4, pl - 4, true);
            break;
        case 0x201:
            printf("  Logical position of first unencrypted logical object "
                   "after first\n  encrypted logical object:\n");
            volume_stats_partition(bp + 4, pl - 4, true);
            break;
        case 0x202:
            printf("  Native capacity partition(s) [MB]:\n");
            volume_stats_partition(bp + 4, pl - 4, false);
            break;
        case 0x203:
            printf("  Used native capacity partition(s) [MB]:\n");
            volume_stats_partition(bp + 4, pl - 4, false);
            break;
        case 0x204:
            printf("  Remaining native capacity partition(s) [MB]:\n");
            volume_stats_partition(bp + 4, pl - 4, false);
            break;
        case 0x300:
            printf("  Mount history:\n");
            volume_stats_history(bp + 4, pl - 4);
            break;

        default:
            if (pc >= 0xf000) {
                    if (op->exclude_vendor) {
                    skip_out = true;
                    if ((op->verbose > 0) && (0 == op->do_brief) &&
                        (! evsm_output)) {
                        evsm_output = true;
                        printf("  Vendor specific parameter(s) being "
                               "ignored\n");
                    }
                } else
                    printf("  Vendor specific parameter code (0x%x), payload "
                           "in hex\n", pc);
            } else
                printf("  Reserved parameter code (0x%x), payload in hex\n",
                       pc);
            if (skip_out)
                skip_out = false;
            else
                hex2fp(bp + 4, pl - 4, "    ", 0 != op->dstrhex_no_ascii,
                       stdout);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* TAPE_ALERT_LPAGE [0x2e] <ta> */
static bool
show_tape_alert_ssc_page(const uint8_t * resp, int len,
                         struct opts_t * op, sgj_opaque_p jop)
{
    int num, pl, pc, flag;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    /* N.B. the Tape alert log page for smc-3 is different */
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Tape alert page (ssc-3) [0x2e]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        flag = bp[4] & 1;
        if (op->verbose && (0 == op->do_brief) && flag)
            printf("  >>>> ");
        if ((0 == op->do_brief) || op->verbose || flag) {
            if (NULL == sg_lib_tapealert_strs[0])
                printf("  No string available for code 0x%x, flag: %d\n",
                       pc, flag);
            else if (pc <= 0x40)
                printf("  %s: %d\n", sg_lib_tapealert_strs[pc], flag);
            else
                printf("  Reserved parameter code 0x%x, flag: %d\n", pc,
                       flag);
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* 0x37 */
static bool
show_seagate_cache_page(const uint8_t * resp, int len,
                        struct opts_t * op, sgj_opaque_p jop)
{
    bool skip = false;
    int num, pl, pc;
    int bsti = 0;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex))) {
        if (resp[1] > 0) {
            printf("Suspicious page 0x37, SPF=0 but subpage=0x%x\n", resp[1]);
            if (op->verbose)
                printf("... try vendor=wdc\n");
            if (op->do_brief > 0)
                return true;
        } else
            printf("Seagate cache page [0x37]\n");
    }
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0:
            ++bsti;
            if (bsti < 2)
                printf("  Blocks sent to initiator");
            else
                skip = true;
            break;
        case 1:
            printf("  Blocks received from initiator");
            break;
        case 2:
            printf("  Blocks read from cache and sent to initiator");
            break;
        case 3:
            printf("  Number of read and write commands whose size "
                   "<= segment size");
            break;
        case 4:
            printf("  Number of read and write commands whose size "
                   "> segment size");
            break;
        default:
            printf("  Unknown Seagate parameter code = 0x%x", pc);
            break;
        }
        if (skip)
            skip = false;
        else {
            printf(" = %" PRIu64 "", sg_get_unaligned_be(pl - 4, bp + 4));
            printf("\n");
            if (op->do_pcb)
                printf("        <%s>\n", get_pcb_str(bp[2], str,
                       sizeof(str)));
        }
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

/* 0x37 */
static bool
show_hgst_misc_page(const uint8_t * resp, int len, struct opts_t * op,
                    sgj_opaque_p jop)
{
    bool valid = false;
    int num, pl, pc;
    const uint8_t * bp;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("HGST/WDC miscellaneous page [0x37, 0x%x]\n",
               op->decod_subpg_code);
    num = len - 4;
    if (num < 0x30) {
        printf("HGST/WDC miscellaneous page too short (%d) < 48\n", num);
        return valid;
    }
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        switch (pc) {
        case 0:
            valid = true;
            printf("  Power on hours = %u\n", sg_get_unaligned_be32(bp + 4));
            printf("  Total Bytes Read = %" PRIu64 "\n",
                   sg_get_unaligned_be64(bp + 8));
            printf("  Total Bytes Written = %" PRIu64 "\n",
                   sg_get_unaligned_be64(bp + 16));
            printf("  Max Drive Temp (Celsius) = %u\n", bp[24]);
            printf("  GList Size = %u\n", sg_get_unaligned_be16(bp + 25));
            printf("  Number of Information Exceptions = %u\n", bp[27]);
            printf("  MED EXC = %u\n", !! (0x80 & bp[28]));
            printf("  HDW EXC = %u\n", !! (0x40 & bp[28]));
            printf("  Total Read Commands = %" PRIu64 "\n",
                   sg_get_unaligned_be64(bp + 29));
            printf("  Total Write Commands = %" PRIu64 "\n",
                   sg_get_unaligned_be64(bp + 37));
            printf("  Flash Correction Count = %u\n",
                   sg_get_unaligned_be16(bp + 46));
            break;
        default:
            valid = false;
            printf("  Unknown HGST/WDC parameter code = 0x%x", pc);
            break;
        }
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return valid;
}

/* 0x3e */
static bool
show_seagate_factory_page(const uint8_t * resp, int len,
                          struct opts_t * op, sgj_opaque_p jop)
{
    bool valid = false;
    int num, pl, pc;
    const uint8_t * bp;
    uint64_t ull;
    char str[PCB_STR_LEN];

if (jop) { };
    if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
        printf("Seagate/Hitachi factory page [0x3e]\n");
    num = len - 4;
    bp = &resp[0] + 4;
    while (num > 3) {
        pc = sg_get_unaligned_be16(bp + 0);
        pl = bp[3] + 4;
        if (op->filter_given) {
            if (pc != op->filter)
                goto skip;
        }
        if (op->do_raw) {
            dStrRaw(bp, pl);
            goto filter_chk;
        } else if (op->do_hex) {
            hex2stdout(bp, pl, op->dstrhex_no_ascii);
            goto filter_chk;
        }
        valid = true;
        switch (pc) {
        case 0:
            printf("  number of hours powered up");
            break;
        case 8:
            printf("  number of minutes until next internal SMART test");
            break;
        default:
            valid = false;
            printf("  Unknown Seagate/Hitachi parameter code = 0x%x", pc);
            break;
        }
        if (valid) {
            ull = sg_get_unaligned_be(pl - 4, bp + 4);
            if (0 == pc)
                printf(" = %.2f", ((double)ull) / 60.0 );
            else
                printf(" = %" PRIu64 "", ull);
        }
        printf("\n");
        if (op->do_pcb)
            printf("        <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
filter_chk:
        if (op->filter_given)
            break;
skip:
        num -= pl;
        bp += pl;
    }
    return true;
}

static void
decode_page_contents(const uint8_t * resp, int len, struct opts_t * op,
                     sgj_opaque_p jop)
{
    int pg_code, subpg_code, vpn;
    bool spf;
    bool done = false;
    const struct log_elem * lep;

    if (len < 3) {
        pr2serr("%s: response has bad length: %d\n", __func__, len);
        return;
    }
    spf = !!(resp[0] & 0x40);
    pg_code = resp[0] & 0x3f;
    if ((VP_HITA == op->vend_prod_num) && (pg_code >= 0x30))
        subpg_code = resp[1];   /* Hitachi don't set SPF on VS pages */
    else
        subpg_code = spf ? resp[1] : 0;
    op->decod_subpg_code = subpg_code;
    if ((SUPP_SPGS_SUBPG == subpg_code) && (SUPP_PAGES_LPAGE != pg_code)) {
        done = show_supported_pgs_sub_page(resp, len, op, jop);
        if (done)
            return;
    }
    vpn = (op->vend_prod_num >= 0) ? op->vend_prod_num : op->deduced_vpn;
    lep = pg_subpg_pdt_search(pg_code, subpg_code, op->dev_pdt, vpn);

    /* Below is the indirect function call to all the show_* functions */
    if (lep && lep->show_pagep)
        done = (*lep->show_pagep)(resp, len, op, jop);

    if (! done) {
        if (0 == op->do_hex) {
            static const char * unable_s = "Unable to decode page = 0x";

            if (subpg_code > 0)
                printf("%s%x, subpage = 0x%x, here is hex:\n", unable_s,
                       pg_code, subpg_code);
            else
                printf("%s%x, here is hex:\n", unable_s, pg_code);
        }
        if ((len > 128) && (0 == op->do_hex)) {
            hex2fp(resp, 64, "  ", 0 != op->dstrhex_no_ascii, stdout);
            printf("  .....  [truncated after 64 of %d bytes (use '-H' to "
                   "see the rest)]\n", len);
        } else {
            if (0 == op->do_hex)
                hex2fp(resp, len, "  ", 0 != op->dstrhex_no_ascii, stdout);
            else
                hex2stdout(resp, len, op->dstrhex_no_ascii);
        }
    }
}

/* Tries to fetch the TEMPERATURE_LPAGE [0xd] page first. If that fails
 * tries to get the Informational Exceptions (IE_LPAGE) page. */
static int
fetchTemperature(int sg_fd, uint8_t * resp, int max_len, struct opts_t * op,
                 sgj_opaque_p jop)
{
    int len;
    int res = 0;

    op->pg_code = TEMPERATURE_LPAGE;
    op->subpg_code = NOT_SPG_SUBPG;
    res = do_logs(sg_fd, resp, max_len, op);
    if (0 == res) {
        len = sg_get_unaligned_be16(resp + 2) + 4;
        if (op->do_raw)
            dStrRaw(resp, len);
        else if (op->do_hex)
            hex2stdout(resp, len, op->dstrhex_no_ascii);
        else
            show_temperature_page(resp, len, op, jop);
    } else if (SG_LIB_CAT_NOT_READY == res)
        pr2serr("Device not ready\n");
    else {
        op->pg_code = IE_LPAGE;
        res = do_logs(sg_fd, resp, max_len, op);
        if (0 == res) {
            len = sg_get_unaligned_be16(resp + 2) + 4;
            if (op->do_raw)
                dStrRaw(resp, len);
            else if (op->do_hex)
                hex2stdout(resp, len, op->dstrhex_no_ascii);
            else
                show_ie_page(resp, len, op, jop);
        } else
            pr2serr("Unable to find temperature in either Temperature or "
                    "IE log page\n");
    }
    sg_cmds_close_device(sg_fd);
    return (res >= 0) ? res : SG_LIB_CAT_OTHER;
}

/* Returns 0 if successful else SG_LIB_SYNTAX_ERROR. */
static int
decode_pg_arg(struct opts_t * op)
{
    int nn;
    const struct log_elem * lep;
    char * cp;

    if (isalpha((uint8_t)op->pg_arg[0])) {
        char b[80];

        if (strlen(op->pg_arg) >= (sizeof(b) - 1)) {
            pr2serr("argument to '--page=' is too long\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        strcpy(b, op->pg_arg);
        cp = (char *)strchr(b, ',');
        if (cp)
            *cp = '\0';
        lep = acron_search(b);
        if (NULL == lep) {
            pr2serr("bad argument to '--page=' no acronyn match to "
                    "'%s'\n", b);
            pr2serr("  Try using '-e' or'-ee' to see available "
                    "acronyns\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        op->lep = lep;
        op->pg_code = lep->pg_code;
        if (cp) {
            nn = sg_get_num_nomult(cp + 1);
            if ((nn < 0) || (nn > 255)) {
                pr2serr("Bad second value in argument to "
                        "'--page='\n");
                return SG_LIB_SYNTAX_ERROR;
            }
            op->subpg_code = nn;
        } else
            op->subpg_code = lep->subpg_code;
    } else { /* numeric arg: either 'pg_num' or 'pg_num,subpg_num' */
        int n;

        cp = (char *)strchr(op->pg_arg, ',');
        n = sg_get_num_nomult(op->pg_arg);
        if ((n < 0) || (n > 63)) {
            pr2serr("Bad argument to '--page='\n");
            usage(1);
            return SG_LIB_SYNTAX_ERROR;
        }
        if (cp) {
            nn = sg_get_num_nomult(cp + 1);
            if ((nn < 0) || (nn > 255)) {
                pr2serr("Bad second value in argument to "
                        "'--page='\n");
                usage(1);
                return SG_LIB_SYNTAX_ERROR;
            }
        } else
            nn = 0;
        op->pg_code = n;
        op->subpg_code = nn;
    }
    return 0;
}

/* Since the Supported subpages page is sitting in the rsp_buff which is
 * MX_ALLOC_LEN bytes long (~ 64 KB) then move it (from rsp_buff+0 to
 * rsp_buff+pg_len-1) to the top end of that buffer. Then there is room
 * to merge supp_pgs_rsp with the supported subpages with the result back
 * at the bottom of rsp_buff. The new length of the merged subpages page
 * (excluding its 4 byte header) is returned.
 * Assumes both pages are in ascending order (as required by SPC-4). */
static int
merge_both_supported(const uint8_t * supp_pgs_p, int su_p_pg_len, int pg_len)
{
    uint8_t pg;
    int k, kp, ks;
    int max_blen = (2 * su_p_pg_len) + pg_len;
    uint8_t * m_buff = rsp_buff + (rsp_buff_sz - pg_len);
    uint8_t * r_buff = rsp_buff + 4;

    if (pg_len > 0)
        memmove(m_buff, rsp_buff + 4, pg_len);
    for (k = 0, kp = 0, ks = 0; k < max_blen; k += 2) {
        if (kp < su_p_pg_len)
            pg = supp_pgs_p[kp];
        else
            pg = 0xff;
        if (ks < pg_len) {
            if (m_buff[ks] < pg) {
                r_buff[k] = m_buff[ks];
                r_buff[k + 1] = m_buff[ks + 1];
                ks += 2;
            } else if ((m_buff[ks] == pg) && (m_buff[ks + 1] == 0)) {
                r_buff[k] = m_buff[ks];
                r_buff[k + 1] = m_buff[ks + 1];
                ks += 2;
                ++kp;
            } else {
                r_buff[k] = pg;
                r_buff[k + 1] = 0;
                ++kp;
            }
        } else {
            if (0xff == pg)
                break;
            r_buff[k] = pg;
            r_buff[k + 1] = 0;
            ++kp;
        }
    }
    sg_put_unaligned_be16(k, rsp_buff + 2);
    return k;
}


int
main(int argc, char * argv[])
{
    bool as_json;
    int k, pg_len, res, vb;
    int resp_len = 0;
    int su_p_pg_len = 0;
    int in_len = -1;
    int sg_fd = -1;
    int ret = 0;
    uint8_t * parr;
    uint8_t * free_parr = NULL;
    struct opts_t * op;
    sgj_state * jsp;
    sgj_opaque_p jop = NULL;
    struct sg_simple_inquiry_resp inq_out;
    struct opts_t opts SG_C_CPP_ZERO_INIT;
    uint8_t supp_pgs_rsp[256];

    op = &opts;
    /* N.B. some disks only give data for current cumulative */
    op->page_control = 1;
    op->dev_pdt = -1;
    op->vend_prod_num = VP_NONE;
    op->deduced_vpn = VP_NONE;
    res = parse_cmd_line(op, argc, argv);
    if (res)
        return SG_LIB_SYNTAX_ERROR;
    if (op->do_help) {
        usage_for(op->do_help, op);
        return 0;
    }
    jsp = &op->json_st;
    as_json = jsp->pr_as_json;
    if (as_json) {
        jop = sgj_start_r(MY_NAME, version_str, argc, argv, jsp);
    }
#ifdef DEBUG
    pr2serr("In DEBUG mode, ");
    if (op->verbose_given && op->version_given) {
        pr2serr("but override: '-vV' given, zero verbose and continue\n");
        op->verbose_given = false;
        op->version_given = false;
        op->verbose = 0;
    } else if (! op->verbose_given) {
        pr2serr("set '-vv'\n");
        op->verbose = 2;
    } else
        pr2serr("keep verbose=%d\n", op->verbose);
#else
    if (op->verbose_given && op->version_given)
        pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
#endif
    if (op->version_given) {
        pr2serr("Version string: %s\n", version_str);
        return 0;
    }
    if (op->do_hex > 0) {
        if (op->do_hex > 2) {
            op->dstrhex_no_ascii = -1;
            op->hex2str_oformat = 1;
        } else {
            op->dstrhex_no_ascii = (1 == op->do_hex);
            op->hex2str_oformat = (1 == op->do_hex);
        }
    } else {
        if (op->undefined_hex > 0) {
            if (op->undefined_hex > 2) {
                op->dstrhex_no_ascii = -1;
                op->hex2str_oformat = 1;
            } else {
                op->dstrhex_no_ascii = (1 == op->undefined_hex);
                op->hex2str_oformat = (1 == op->undefined_hex);
            }
        } else {       /* default when no --hex nor --undefined */
            op->dstrhex_no_ascii = -1;
            op->hex2str_oformat = 1;
        }
    }
    vb = op->verbose;
    if (op->vend_prod) {
        if (0 == memcmp("-1", op->vend_prod,3))
            k = VP_NONE;
        else if (isdigit((uint8_t)op->vend_prod[0]))
            k = sg_get_num_nomult(op->vend_prod);
        else
            k = find_vpn_by_acron(op->vend_prod);
        op->vend_prod_num = k;
        if (VP_ALL == k)
            ;
        else if ((k < 0) || (k > (32 - MVP_OFFSET))) {
            pr2serr("Bad vendor/product acronym after '--vendor=' "
                    " ('-M ') option\n");
            enumerate_vp();
            return SG_LIB_SYNTAX_ERROR;
        }
    }
    if (op->do_enumerate > 0) {
        if (op->device_name && vb)
            pr2serr("Warning: device: %s is being ignored\n",
                    op->device_name);
        enumerate_pages(op);
        return 0;
    }
    if (op->in_fn) {
        if (op->maxlen_given) {
            if (op->maxlen > MX_INLEN_ALLOC_LEN) {
                pr2serr("bad argument to '--maxlen=' when --in= given, from "
                        "2 to %d (inclusive) expected\n", MX_INLEN_ALLOC_LEN);
                return SG_LIB_SYNTAX_ERROR;
            }
            rsp_buff_sz = op->maxlen;
        } else
            rsp_buff_sz = DEF_INLEN_ALLOC_LEN;
    } else {
        if (op->maxlen_given) {
            if (op->maxlen > MX_ALLOC_LEN) {
                pr2serr("bad argument to '--maxlen=', from 2 to 65535 "
                        "(inclusive) expected\n");
                return SG_LIB_SYNTAX_ERROR;
            }
            rsp_buff_sz = op->maxlen;
        }
    }
    rsp_buff = sg_memalign(rsp_buff_sz, 0 /* page aligned */, &free_rsp_buff,
                           false);
    if (NULL == rsp_buff) {
        pr2serr("Unable to allocate %d bytes on the heap\n", rsp_buff_sz);
        ret = sg_convert_errno(ENOMEM);
        goto err_out;
    }
    if (NULL == op->device_name) {
        if (op->in_fn) {
            const struct log_elem * lep;
            const uint8_t * bp;
            int pg_code, subpg_code, pdt, n;
            uint16_t u;

            if ((ret = sg_f2hex_arr(op->in_fn, op->do_raw, false, rsp_buff,
                                    &in_len, rsp_buff_sz)))
                goto err_out;
            if (vb > 2)
                pr2serr("Read %d [0x%x] bytes of user supplied data\n",
                        in_len, in_len);
            if (op->do_raw)
                op->do_raw = false;    /* can interfere on decode */
            if (in_len < 4) {
                pr2serr("--in=%s only decoded %d bytes (needs 4 at least)\n",
                        op->in_fn, in_len);
                ret = SG_LIB_SYNTAX_ERROR;
                goto err_out;
            }
            if (op->pg_arg && (0 == op->do_brief))
                pr2serr(">>> --page=%s option is being ignored, using values "
                        "in file: %s\n", op->pg_arg, op->in_fn);
            for (bp = rsp_buff, k = 0; k < in_len; bp += n, k += n) {
                pg_code = bp[0] & 0x3f;
                subpg_code = (bp[0] & 0x40) ? bp[1] : 0;
                u = sg_get_unaligned_be16(bp + 2);
                n = u + 4;
                if (n > (in_len - k)) {
                    pr2serr("bytes decoded remaining (%d) less than lpage "
                            "length (%d), try decoding anyway\n", in_len - k,
                            n);
                    n = in_len - k;
                }
                if (op->exclude_vendor && (pg_code >= 0x30))
                    continue;
                pdt = op->dev_pdt;
                lep = pg_subpg_pdt_search(pg_code, subpg_code, pdt,
                                          op->vend_prod_num);
                if (lep) {
                    /* Below is the indirect function call to all the
                     * show_* functions */
                    if (lep->show_pagep)
                        (*lep->show_pagep)(bp, n, op, jop);
                    else
                        printf("Unable to decode %s [%s]\n", lep->name,
                               lep->acron);
                } else {
                    printf("Unable to decode page=0x%x", pg_code);
                    if (subpg_code > 0)
                        printf(", subpage=0x%x", subpg_code);
                    if (pdt >= 0)
                        printf(", pdt=0x%x\n", pdt);
                    else
                        printf("\n");
                }
            }
            ret = 0;
            goto err_out;
        }
        if (op->pg_arg) {         /* do this for 'sg_logs -p xxx' */
            ret = decode_pg_arg(op);
            if (ret)
                goto err_out;
        }
        pr2serr("No DEVICE argument given\n");
        usage_for(1, op);
        ret = SG_LIB_SYNTAX_ERROR;
        goto err_out;
    }
    if (op->do_select) {
        if (op->do_temperature) {
            pr2serr("--select cannot be used with --temperature\n");
            ret = SG_LIB_CONTRADICT;
            goto err_out;
        }
        if (op->do_transport) {
            pr2serr("--select cannot be used with --transport\n");
            ret = SG_LIB_CONTRADICT;
            goto err_out;
        }
    } else if (op->do_raw) {
        if (sg_set_binary_mode(STDOUT_FILENO) < 0) {
            perror("sg_set_binary_mode");
            ret = SG_LIB_FILE_ERROR;
            goto err_out;
        }
    }
    if (op->do_all) {
        if (op->do_select) {
            pr2serr("--all conflicts with --select\n");
            ret = SG_LIB_CONTRADICT;
            goto err_out;
        }
    }
    if (op->in_fn) {
        if (! op->do_select) {
            pr2serr("--in=FN can only be used with --select when DEVICE "
                    "given\n");
            ret = SG_LIB_CONTRADICT;
            goto err_out;
        }
        if ((ret = sg_f2hex_arr(op->in_fn, op->do_raw, false, rsp_buff,
                                &in_len, rsp_buff_sz)))
            goto err_out;
        if (vb > 2)
            pr2serr("Read %d [0x%x] bytes of user supplied data\n", in_len,
                    in_len);
    }
    if (op->pg_arg) {
        if (op->do_all) {
            if (0 == op->do_brief)
                pr2serr(">>> warning: --page=%s ignored when --all given\n",
                        op->pg_arg);
        } else {
            ret = decode_pg_arg(op);
            if (ret)
                goto err_out;
        }
    }

#ifdef SG_LIB_WIN32
#ifdef SG_LIB_WIN32_DIRECT
    win32_spt_init_state = !! scsi_pt_win32_spt_state();
    if (vb > 4)
        pr2serr("Initial win32 SPT interface state: %s\n",
                win32_spt_init_state ? "direct" : "indirect");
#endif
#endif
    sg_fd = sg_cmds_open_device(op->device_name, op->o_readonly, vb);
    if ((sg_fd < 0) && (! op->o_readonly))
        sg_fd = sg_cmds_open_device(op->device_name, true /* ro */, vb);
    if (sg_fd < 0) {
        pr2serr("error opening file: %s: %s \n", op->device_name,
                safe_strerror(-sg_fd));
        ret = sg_convert_errno(-sg_fd);
        goto err_out;
    }
    if (op->do_list || op->do_all) {
        op->pg_code = SUPP_PAGES_LPAGE;
        if ((op->do_list > 1) || (op->do_all > 1))
            op->subpg_code = SUPP_SPGS_SUBPG;
    }
    if (op->do_transport) {
        if ((op->pg_code > 0) || (op->subpg_code > 0) ||
            op->do_temperature) {
            pr2serr("'-T' should not be mixed with options implying other "
                    "pages\n");
            ret = SG_LIB_FILE_ERROR;
            goto err_out;
        }
        op->pg_code = PROTO_SPECIFIC_LPAGE;
    }

    memset(&inq_out, 0, sizeof(inq_out));
    if (op->no_inq < 2) {
        if (sg_simple_inquiry(sg_fd, &inq_out, true, vb)) {
            pr2serr("%s doesn't respond to a SCSI INQUIRY\n",
                    op->device_name);
            ret = SG_LIB_CAT_OTHER;
            goto err_out;
        }
        op->dev_pdt = inq_out.peripheral_type;
        if ((! op->do_raw) && (0 == op->do_hex) && (! op->do_name) &&
            (0 == op->no_inq) && (0 == op->do_brief))
            sgj_pr_hr(jsp, "    %.8s  %.16s  %.4s\n", inq_out.vendor,
                      inq_out.product, inq_out.revision);
        memcpy(t10_vendor_str, inq_out.vendor, 8);
        memcpy(t10_product_str, inq_out.product, 16);
        if (VP_NONE == op->vend_prod_num)
            op->deduced_vpn = find_vpn_by_inquiry();
    }

    if (op->do_temperature) {
        ret = fetchTemperature(sg_fd, rsp_buff, SHORT_RESP_LEN, op, jop);
        goto err_out;
    }
    if (op->do_select) {
        k = sg_ll_log_select(sg_fd, op->do_pcreset, op->do_sp,
                             op->page_control, op->pg_code, op->subpg_code,
                             rsp_buff, ((in_len > 0) ? in_len : 0), true, vb);
        if (k) {
            if (SG_LIB_CAT_NOT_READY == k)
                pr2serr("log_select: device not ready\n");
            else if (SG_LIB_CAT_ILLEGAL_REQ == k)
                pr2serr("log_select: field in cdb illegal\n");
            else if (SG_LIB_CAT_INVALID_OP == k)
                pr2serr("log_select: not supported\n");
            else if (SG_LIB_CAT_UNIT_ATTENTION == k)
                pr2serr("log_select: unit attention\n");
            else if (SG_LIB_CAT_ABORTED_COMMAND == k)
                pr2serr("log_select: aborted command\n");
            else
                pr2serr("log_select: failed (%d), try '-v' for more "
                        "information\n", k);
        }
        ret = (k >= 0) ?  k : SG_LIB_CAT_OTHER;
        goto err_out;
    }
    if (op->do_list > 2) {
        const int supp_pgs_blen = sizeof(supp_pgs_rsp);

        op->subpg_code = 0;
        res = do_logs(sg_fd, supp_pgs_rsp, supp_pgs_blen, op);
        if (res != 0)
            goto bad;
        su_p_pg_len = sg_get_unaligned_be16(supp_pgs_rsp + 2);
        if ((su_p_pg_len + 4) > supp_pgs_blen) {
            pr2serr("Supported log pages log page is too long [%d], exit\n",
                    su_p_pg_len);
            res = SG_LIB_CAT_OTHER;
            goto bad;
        }
        op->subpg_code = SUPP_SPGS_SUBPG;
    }
    resp_len = (op->maxlen > 0) ? op->maxlen : MX_ALLOC_LEN;
    res = do_logs(sg_fd, rsp_buff, resp_len, op);
    if (0 == res) {
        pg_len = sg_get_unaligned_be16(rsp_buff + 2);
        if ((pg_len + 4) > resp_len) {
            pr2serr("Only fetched %d bytes of response (available: %d "
                    "bytes)\n    truncate output\n",
                   resp_len, pg_len + 4);
            pg_len = resp_len - 4;
        }
        goto good;
    }
bad:
    if (SG_LIB_CAT_INVALID_OP == res)
        pr2serr("%snot supported\n", ls_s);
    else if (SG_LIB_CAT_NOT_READY == res)
        pr2serr("%sdevice not ready\n", ls_s);
    else if (SG_LIB_CAT_ILLEGAL_REQ == res) {
        if ((op->do_list > 2) && (SUPP_SPGS_SUBPG == op->subpg_code)) {
            rsp_buff[0] = 0x40;
            rsp_buff[1] = SUPP_SPGS_SUBPG;
            pg_len = 0;
            res = 0;
            if (op->verbose)
                pr2serr("%sfield in cdb illegal in [0,0xff], "
                        "continue with merge\n", ls_s);
            goto good;
        } else
            pr2serr("%sfield in cdb illegal\n", ls_s);
    } else if (SG_LIB_CAT_UNIT_ATTENTION == res)
        pr2serr("%sunit attention\n", ls_s);
    else if (SG_LIB_CAT_ABORTED_COMMAND == res)
        pr2serr("%saborted command\n", ls_s);
    else if (SG_LIB_TRANSPORT_ERROR == res)
        pr2serr("%stransport error\n", ls_s);
    else
        pr2serr("%sother error [%d]\n", ls_s, res);
    ret = res;
    goto err_out;

good:
    if (op->do_list > 2)
        pg_len = merge_both_supported(supp_pgs_rsp + 4, su_p_pg_len, pg_len);

    if (0 == op->do_all) {
        if (op->filter_given) {
            if (op->do_hex > 2)
                hex2stdout(rsp_buff, pg_len + 4, op->dstrhex_no_ascii);
            else
                decode_page_contents(rsp_buff, pg_len + 4, op, jop);
        } else if (op->do_raw)
            dStrRaw(rsp_buff, pg_len + 4);
        else if (op->do_hex > 1)
            hex2stdout(rsp_buff, pg_len + 4, op->dstrhex_no_ascii);
        else if (pg_len > 1) {
            if (op->do_hex) {
                if (rsp_buff[0] & 0x40)
                    printf("Log page code=0x%x,0x%x, DS=%d, SPF=1, "
                           "page_len=0x%x\n", rsp_buff[0] & 0x3f, rsp_buff[1],
                           !!(rsp_buff[0] & 0x80), pg_len);
                else
                    printf("Log page code=0x%x, DS=%d, SPF=0, page_len=0x%x\n",
                           rsp_buff[0] & 0x3f, !!(rsp_buff[0] & 0x80), pg_len);
                hex2stdout(rsp_buff, pg_len + 4, op->dstrhex_no_ascii);
            }
            else
                decode_page_contents(rsp_buff, pg_len + 4, op, jop);
        }
    }
    ret = res;

    if (op->do_all && (pg_len > 1)) {
        int my_len = pg_len;
        bool spf;

        parr = sg_memalign(parr_sz, 0, &free_parr, false);
        if (NULL == parr) {
            pr2serr("Unable to allocate heap for parr\n");
            ret = sg_convert_errno(ENOMEM);
            goto err_out;
        }
        spf = !!(rsp_buff[0] & 0x40);
        if (my_len > parr_sz) {
            pr2serr("Unexpectedly large page_len=%d, trim to %d\n", my_len,
                    parr_sz);
            my_len = parr_sz;
        }
        memcpy(parr, rsp_buff + 4, my_len);
        for (k = 0; k < my_len; ++k) {
            op->pg_code = parr[k] & 0x3f;
            if (spf)
                op->subpg_code = parr[++k];
            else
                op->subpg_code = NOT_SPG_SUBPG;

            /* Some devices include [pg_code, 0xff] for all pg_code > 0 */
            if ((op->pg_code > 0) && (SUPP_SPGS_SUBPG == op->subpg_code))
                continue;       /* skip since no new information */
            if ((op->pg_code >= 0x30) && op->exclude_vendor)
                continue;
            if (! op->do_raw)
                printf("\n");
            res = do_logs(sg_fd, rsp_buff, resp_len, op);
            if (0 == res) {
                pg_len = sg_get_unaligned_be16(rsp_buff + 2);
                if ((pg_len + 4) > resp_len) {
                    pr2serr("Only fetched %d bytes of response, truncate "
                            "output\n", resp_len);
                    pg_len = resp_len - 4;
                }
                if (op->do_raw && (! op->filter_given))
                    dStrRaw(rsp_buff, pg_len + 4);
                else if (op->do_hex > 4)
                    decode_page_contents(rsp_buff, pg_len + 4, op, jop);
                else if (op->do_hex > 1)
                    hex2stdout(rsp_buff, pg_len + 4, op->dstrhex_no_ascii);
                else if (1 == op->do_hex) {
                    if (0 == op->do_brief) {
                        if (rsp_buff[0] & 0x40)
                            printf("Log page code=0x%x,0x%x, DS=%d, SPF=1, "
                                   "page_len=0x%x\n", rsp_buff[0] & 0x3f,
                                   rsp_buff[1], !!(rsp_buff[0] & 0x80),
                                   pg_len);
                        else
                            printf("Log page code=0x%x, DS=%d, SPF=0, "
                                   "page_len=0x%x\n", rsp_buff[0] & 0x3f,
                                   !!(rsp_buff[0] & 0x80), pg_len);
                    }
                    hex2stdout(rsp_buff, pg_len + 4, op->dstrhex_no_ascii);
                }
                else
                    decode_page_contents(rsp_buff, pg_len + 4, op, jop);
            } else if (SG_LIB_CAT_INVALID_OP == res)
                pr2serr("%spage=0x%x,0x%x not supported\n", ls_s,
                        op->pg_code, op->subpg_code);
            else if (SG_LIB_CAT_NOT_READY == res)
                pr2serr("%sdevice not ready\n", ls_s);
            else if (SG_LIB_CAT_ILLEGAL_REQ == res)
                pr2serr("%sfield in cdb illegal [page=0x%x,0x%x]\n", ls_s,
                        op->pg_code, op->subpg_code);
            else if (SG_LIB_CAT_UNIT_ATTENTION == res)
                pr2serr("%sunit attention\n", ls_s);
            else if (SG_LIB_CAT_ABORTED_COMMAND == res)
                pr2serr("%saborted command\n", ls_s);
            else
                pr2serr("%sfailed, try '-v' for more information\n", ls_s);
        }
    }
err_out:
    if (free_rsp_buff)
        free(free_rsp_buff);
    if (free_parr)
        free(free_parr);
    if (sg_fd >= 0)
        sg_cmds_close_device(sg_fd);
    if (0 == vb) {
        if (! sg_if_can2stderr("sg_logs failed: ", ret))
            pr2serr("Some error occurred, try again with '-v' or '-vv' for "
                    "more information\n");
    }
    if (as_json) {
        if (0 == op->do_hex)
            sgj_js2file(jsp, NULL, ret, stdout);
        sgj_finish(jsp);
    }
    return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
}