aboutsummaryrefslogtreecommitdiff
path: root/lib/sg_pt_linux_nvme.c
blob: 480782cb880ff423bc72ed22a3e943b7675d2b2e (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
/*
 * Copyright (c) 2017-2023 Douglas Gilbert.
 * All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the BSD_LICENSE file.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * The code to use the NVMe Management Interface (MI) SES pass-through
 * was provided by WDC in November 2017.
 */

/*
 * Copyright 2017, Western Digital Corporation
 *
 * Written by Berck Nash
 *
 * Use of this source code is governed by a BSD-style
 * license that can be found in the BSD_LICENSE file.
 *
 * Based on the NVM-Express command line utility, which bore the following
 * notice:
 *
 * Copyright (c) 2014-2015, Intel Corporation.
 *
 * Written by Keith Busch <keith.busch@intel.com>
 *
 * 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
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *                   MA 02110-1301, USA.
 */

/* sg_pt_linux_nvme version 1.21 20231123 */

/* This file contains a small "SPC-only" SNTL to support the SES pass-through
 * of SEND DIAGNOSTIC and RECEIVE DIAGNOSTIC RESULTS through NVME-MI
 * SES Send and SES Receive. */

/* This implementation will be gradually changed to follow the T10 23-047r1
 * proposal: "Broadcom SNT Reference". This assumes that that proposal will
 * be used as the basis T10's forthcoming SNT standard which as yet does
 * not have any drafts. When those drafts appear, they will be followed. */


#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>      /* to define 'major' */
#ifndef major
#include <sys/types.h>
#endif


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_LINUX_MAJOR_H
#include <linux/major.h>
#endif

#include "sg_pt.h"
#include "sg_lib.h"
#include "sg_snt.h"
#include "sg_linux_inc.h"
#include "sg_pt_linux.h"
#include "sg_unaligned.h"
#include "sg_pr2serr.h"

#define SCSI_INQUIRY_OPC     0x12
#define SCSI_REPORT_LUNS_OPC 0xa0
#define SCSI_TEST_UNIT_READY_OPC  0x0
#define SCSI_REQUEST_SENSE_OPC  0x3
#define SCSI_SEND_DIAGNOSTIC_OPC  0x1d
#define SCSI_RECEIVE_DIAGNOSTIC_OPC  0x1c
#define SCSI_MAINT_IN_OPC  0xa3
#define SCSI_READ10_OPC 0x28
#define SCSI_READ16_OPC 0x88
#define SCSI_REP_SUP_OPCS_OPC  0xc
#define SCSI_REP_SUP_TMFS_OPC  0xd
#define SCSI_MODE_SENSE10_OPC  0x5a
#define SCSI_MODE_SELECT10_OPC  0x55
#define SCSI_READ_CAPACITY10_OPC  0x25
#define SCSI_START_STOP_OPC 0x1b
#define SCSI_SYNC_CACHE10_OPC  0x35
#define SCSI_SYNC_CACHE16_OPC  0x91
#define SCSI_VERIFY10_OPC 0x2f
#define SCSI_VERIFY16_OPC 0x8f
#define SCSI_WRITE10_OPC 0x2a
#define SCSI_WRITE16_OPC 0x8a
#define SCSI_WRITE_SAME10_OPC 0x41
#define SCSI_WRITE_SAME16_OPC 0x93
#define SCSI_SERVICE_ACT_IN_OPC  0x9e
#define SCSI_READ_CAPACITY16_SA  0x10
#define SCSI_SA_MSK  0x1f

/* Additional Sense Code (ASC) */
#define NO_ADDITIONAL_SENSE 0x0
#define LOGICAL_UNIT_NOT_READY 0x4
#define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
#define UNRECOVERED_READ_ERR 0x11
#define PARAMETER_LIST_LENGTH_ERR 0x1a
#define INVALID_OPCODE 0x20
#define LBA_OUT_OF_RANGE 0x21
#define INVALID_FIELD_IN_CDB 0x24
#define INVALID_FIELD_IN_PARAM_LIST 0x26
#define UA_RESET_ASC 0x29
#define UA_CHANGED_ASC 0x2a
#define TARGET_CHANGED_ASC 0x3f
#define LUNS_CHANGED_ASCQ 0x0e
#define INSUFF_RES_ASC 0x55
#define INSUFF_RES_ASCQ 0x3
#define LOW_POWER_COND_ON_ASC  0x5e     /* ASCQ=0 */
#define POWER_ON_RESET_ASCQ 0x0
#define BUS_RESET_ASCQ 0x2      /* scsi bus reset occurred */
#define MODE_CHANGED_ASCQ 0x1   /* mode parameters changed */
#define CAPACITY_CHANGED_ASCQ 0x9
#define SAVING_PARAMS_UNSUP 0x39
#define TRANSPORT_PROBLEM 0x4b
#define THRESHOLD_EXCEEDED 0x5d
#define LOW_POWER_COND_ON 0x5e
#define MISCOMPARE_VERIFY_ASC 0x1d
#define MICROCODE_CHANGED_ASCQ 0x1      /* with TARGET_CHANGED_ASC */
#define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
#define PCIE_ERR_ASC 0x4b
#define PCIE_UNSUPP_REQ_ASCQ 0x13

/* NVMe Admin commands */
#define SG_NVME_AD_GET_FEATURE 0xa
#define SG_NVME_AD_SET_FEATURE 0x9
#define SG_NVME_AD_IDENTIFY 0x6         /* similar to SCSI INQUIRY */
#define SG_NVME_AD_DEV_SELT_TEST 0x14
#define SG_NVME_AD_MI_RECEIVE 0x1e      /* MI: Management Interface */
#define SG_NVME_AD_MI_SEND 0x1d         /* hmmm, same opcode as SEND DIAG */

/* NVMe NVM (Non-Volatile Memory) commands */
#define SG_NVME_NVM_FLUSH 0x0           /* SCSI SYNCHRONIZE CACHE */
#define SG_NVME_NVM_COMPARE 0x5         /* SCSI VERIFY(BYTCHK=1) */
#define SG_NVME_NVM_READ 0x2
#define SG_NVME_NVM_VERIFY 0xc          /* SCSI VERIFY(BYTCHK=0) */
#define SG_NVME_NVM_WRITE 0x1
#define SG_NVME_NVM_WRITE_ZEROES 0x8    /* SCSI WRITE SAME */

#define SG_NVME_RW_CONTROL_FUA (1 << 14) /* Force Unit Access bit */


#if (HAVE_NVME && (! IGNORE_NVME))

/* This trims given NVMe block device name in Linux (e.g. /dev/nvme0n1p5)
 * to the name of its associated char device (e.g. /dev/nvme0). If this
 * occurs true is returned and the char device name is placed in 'b' (as
 * long as b_len is sufficient). Otherwise false is returned. */
bool
sg_get_nvme_char_devname(const char * nvme_block_devname, uint32_t b_len,
                         char * b)
{
    uint32_t n, tlen;
    const char * cp;
    char buff[8];

    if ((NULL == b) || (b_len < 5))
        return false;   /* degenerate cases */
    cp = strstr(nvme_block_devname, "nvme");
    if (NULL == cp)
        return false;   /* expected to find "nvme" in given name */
    if (1 != sscanf(cp, "nvme%u", &n))
        return false;   /* didn't find valid "nvme<number>" */
    snprintf(buff, sizeof(buff), "%u", n);
    tlen = (cp - nvme_block_devname) + 4 + strlen(buff);
    if ((tlen + 1) > b_len)
        return false;           /* b isn't long enough to fit output */
    memcpy(b, nvme_block_devname, tlen);
    b[tlen] = '\0';
    return true;
}

static void
mk_sense_from_nvme_status(struct sg_pt_linux_scsi * ptp, int vb)
{
    bool ok;
    bool dsense = !! ptp->dev_stat.scsi_dsense;
    int n;
    uint8_t sstatus, sk, asc, ascq;
    uint8_t * sbp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.response;

    ok = sg_nvme_status2scsi(ptp->nvme_status, &sstatus, &sk, &asc, &ascq);
    if (! ok) { /* can't find a mapping to a SCSI error, so ... */
        sstatus = SAM_STAT_CHECK_CONDITION;
        sk = SPC_SK_ILLEGAL_REQUEST;
        asc = 0xb;
        ascq = 0x0;     /* asc: "WARNING" purposely vague */
    }

    ptp->io_hdr.device_status = sstatus;
    n = ptp->io_hdr.max_response_len;
    if ((n < 8) || ((! dsense) && (n < 14))) {
        pr2ws("%s: sense_len=%d too short, want 14 or more\n", __func__, n);
        return;
    } else
        ptp->io_hdr.response_len = dsense ? n : ((n < 18) ? n : 18);
    memset(sbp, 0, n);
    sg_build_sense_buffer(dsense, sbp, sk, asc, ascq);
    if (dsense && (ptp->nvme_status > 0))
        sg_nvme_desc2sense(sbp, ptp->nvme_stat_dnr, ptp->nvme_stat_more,
                           ptp->nvme_status);
    if (vb > 3)
        pr2ws("%s: [status, sense_key,asc,ascq]: [0x%x, 0x%x,0x%x,0x%x]\n",
              __func__, sstatus, sk, asc, ascq);
}

static void
mk_sense_from_snt_result(struct sg_pt_linux_scsi * ptp,
                         const struct sg_snt_result_t * resp, int vb)
{
    bool cdb_pos_v = false;
    bool param_pos_v = false;
    bool dsense = !! ptp->dev_stat.scsi_dsense;
    int n;
    uint8_t * sbp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.response;
    uint8_t sks[4];

    if (0 == resp->sstatus)
        goto fini;
    ptp->io_hdr.device_status = SAM_STAT_CHECK_CONDITION;
    n = ptp->io_hdr.max_response_len;
    if ((n < 8) || ((! dsense) && (n < 14))) {
        if (vb)
            pr2ws("%s: max_response_len=%d too short, want 14 or more\n",
                  __func__, n);
        return;
    } else
        ptp->io_hdr.response_len = dsense ? n : ((n < 18) ? n : 18);

    memset(sbp, 0, n);
    if (SPC_SK_ILLEGAL_REQUEST == resp->sk) {
        if  (INVALID_FIELD_IN_CDB == resp->asc)
            cdb_pos_v = true;
        else if (INVALID_FIELD_IN_PARAM_LIST == resp->asc)
            param_pos_v = true;
    }
    sg_build_sense_buffer(dsense, sbp, resp->sk, resp->asc, resp->ascq);
    if (resp->in_byte == 0)
        goto fini;
    else if ((! cdb_pos_v) && (! param_pos_v)) {
        goto fini;
    }
    memset(sks, 0, sizeof(sks));
    sks[0] = 0x80;
    if (cdb_pos_v)
        sks[0] |= 0x40;
    if (resp->in_bit < 8) {
        sks[0] |= 0x8;
        sks[0] |= (0x7 & resp->in_bit);
    }
    sg_put_unaligned_be16(resp->in_byte, sks + 1);
    if (dsense) {
        int sl = sbp[7] + 8;

        sbp[7] = sl;
        sbp[sl] = 0x2;
        sbp[sl + 1] = 0x6;
        memcpy(sbp + sl + 4, sks, 3);
    } else
        memcpy(sbp + 15, sks, 3);
fini:
    if (vb > 3)
        pr2ws("%s:  [sense_key,asc,ascq]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
              __func__, resp->asc, cdb_pos_v ? 'C' : 'D', resp->in_byte,
              ((resp->in_bit > 0) ? (0x7 & resp->in_bit) : 0));
}

static void
mk_sense_asc_ascq(struct sg_pt_linux_scsi * ptp, int sk, int asc, int ascq,
                  int vb)
{
    struct sg_snt_result_t sres;

    sg_snt_mk_sense_asc_ascq(&sres, sk, asc, ascq);
    mk_sense_from_snt_result(ptp, &sres, vb);
}

/* Set in_bit to -1 to indicate no bit position of invalid field */
static void
mk_sense_invalid_fld(struct sg_pt_linux_scsi * ptp, bool in_cdb, int in_byte,
                     int in_bit, int vb)
{
    struct sg_snt_result_t sres;

    sg_snt_mk_sense_invalid_fld(&sres, in_cdb, in_byte, in_bit);
    mk_sense_from_snt_result(ptp, &sres, vb);
}

/* Returns 0 for success. Returns SG_LIB_NVME_STATUS if there is non-zero
 * NVMe status (from the completion queue) with the value placed in
 * ptp->nvme_status. If Unix error from ioctl then return negated value
 * (equivalent -errno from basic Unix system functions like open()).
 * CDW0 from the completion queue is placed in ptp->nvme_result in the
 * absence of a Unix error. If time_secs is negative it is treated as
 * a timeout in milliseconds (of abs(time_secs) ). */
static int
sg_nvme_admin_cmd_f(struct sg_pt_linux_scsi * ptp,
                    struct sg_nvme_passthru_cmd *cmdp, void * dp,
                    bool is_read, int time_secs, int vb)
{
    const uint32_t cmd_len = sizeof(struct sg_nvme_passthru_cmd);
    int res;
    uint32_t n;
    uint16_t sct_sc;
    const uint8_t * up = ((const uint8_t *)cmdp) + SG_NVME_OPCODE;
    char nam[64];

    if (vb)
        sg_get_nvme_opcode_name(*up, true /* ADMIN */, sizeof(nam), nam);
    else
        nam[0] = '\0';
    cmdp->timeout_ms = (time_secs < 0) ? (-time_secs) : (1000 * time_secs);
    ptp->os_err = 0;
    if (vb > 2) {
        pr2ws("NVMe Admin command: %s\n", nam);
        hex2stderr((const uint8_t *)cmdp, cmd_len, 1);
        if ((vb > 4) && (! is_read) && dp) {
            uint32_t len = sg_get_unaligned_le32(up + SG_NVME_DATA_LEN);

            if (len > 0) {
                n = len;
                if ((len < 512) || (vb > 5))
                    pr2ws("\nData-out buffer (%u bytes):\n", n);
                else {
                    pr2ws("\nData-out buffer (first 512 of %u bytes):\n", n);
                    n = 512;
                }
                hex2stderr((const uint8_t *)dp, n, 0);
            }
        }
    }
    res = ioctl(ptp->dev_fd, NVME_IOCTL_ADMIN_CMD, cmdp);
    if (res < 0) {  /* OS error (errno negated) */
        ptp->os_err = -res;
        if (vb > 1) {
            pr2ws("%s: ioctl for %s [0x%x] failed: %s "
                  "(errno=%d)\n", __func__, nam, *up, strerror(-res), -res);
        }
        return res;
    }

    /* Now res contains NVMe completion queue CDW3 31:17 (15 bits) */
    ptp->nvme_result = cmdp->result;
    if ((! ptp->nvme_our_snt) && ptp->io_hdr.response &&
        (ptp->io_hdr.max_response_len > 3)) {
        /* build 32 byte "sense" buffer */
        uint8_t * sbp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.response;
        uint16_t st = (uint16_t)res;

        n = ptp->io_hdr.max_response_len;
        n = (n < 32) ? n : 32;
        memset(sbp, 0 , n);
        ptp->io_hdr.response_len = n;
        sg_put_unaligned_le32(cmdp->result,
                              sbp + SG_NVME_CQ_RESULT);
        if (n > 15) /* LSBit will be 0 (Phase bit) after (st << 1) */
            sg_put_unaligned_le16(st << 1, sbp + SG_NVME_CQ_STATUS_P);
    }
    /* clear upper bits (DNR and More) leaving ((SCT << 8) | SC) */
    sct_sc = 0x7ff & res;       /* 11 bits */
    ptp->nvme_status = sct_sc;
    ptp->nvme_stat_dnr = !!(0x4000 & res);
    ptp->nvme_stat_more = !!(0x2000 & res);
    if (sct_sc) {  /* when non-zero, treat as command error */
        if (vb > 1) {
            char b[80];

            pr2ws("%s: ioctl for %s [0x%x] failed, status: %s [0x%x]\n",
                   __func__, nam, *up,
                  sg_get_nvme_cmd_status_str(sct_sc, sizeof(b), b), sct_sc);
        }
        return SG_LIB_NVME_STATUS;      /* == SCSI_PT_DO_NVME_STATUS */
    }
    if ((vb > 4) && is_read && dp) {
        uint32_t len = sg_get_unaligned_le32(up + SG_NVME_DATA_LEN);

        if (len > 0) {
            n = len;
            if ((len < 1024) || (vb > 5))
                pr2ws("\nData-in buffer (%u bytes):\n", n);
            else {
                pr2ws("\nData-in buffer (first 1024 of %u bytes):\n", n);
                n = 1024;
            }
            hex2stderr((const uint8_t *)dp, n, 0);
        }
    }
    return 0;
}

/* see NVME MI document, NVMSR is NVM Subsystem Report */
static void
sg_nvme_check_enclosure_override(struct sg_pt_linux_scsi * ptp, int vb)
{
    uint8_t * up = ptp->nvme_id_ctlp;
    uint8_t nvmsr;

    if (NULL == up)
        return;
    nvmsr = up[253];
    if (vb > 5)
        pr2ws("%s: enter, nvmsr=%u\n", __func__, nvmsr);
    ptp->dev_stat.id_ctl253 = nvmsr;
    switch (ptp->dev_stat.enclosure_override) {
    case 0x0:       /* no override */
        if (0x3 == (0x3 & nvmsr)) {
            ptp->dev_stat.pdt = PDT_DISK;
            ptp->dev_stat.enc_serv = 1;
        } else if (0x2 & nvmsr) {
            ptp->dev_stat.pdt = PDT_SES;
            ptp->dev_stat.enc_serv = 1;
        } else if (0x1 & nvmsr) {
            ptp->dev_stat.pdt = PDT_DISK;
            ptp->dev_stat.enc_serv = 0;
        } else {
            uint32_t nn = sg_get_unaligned_le32(up + 516);

            ptp->dev_stat.pdt = nn ? PDT_DISK : PDT_UNKNOWN;
            ptp->dev_stat.enc_serv = 0;
        }
        break;
    case 0x1:       /* override to SES device */
        ptp->dev_stat.pdt = PDT_SES;
        ptp->dev_stat.enc_serv = 1;
        break;
    case 0x2:       /* override to disk with attached SES device */
        ptp->dev_stat.pdt = PDT_DISK;
        ptp->dev_stat.enc_serv = 1;
        break;
    case 0x3:       /* override to SAFTE device (PDT_PROCESSOR) */
        ptp->dev_stat.pdt = PDT_PROCESSOR;
        ptp->dev_stat.enc_serv = 1;
        break;
    case 0xff:      /* override to normal disk */
        ptp->dev_stat.pdt = PDT_DISK;
        ptp->dev_stat.enc_serv = 0;
        break;
    default:
        pr2ws("%s: unknown enclosure_override value: %d\n", __func__,
              ptp->dev_stat.enclosure_override);
        break;
    }
}

static int
sg_nvme_do_identify(struct sg_pt_linux_scsi * ptp, int cns, int nsid,
                    int time_secs, int u_len, uint8_t * up, int vb)
{
    struct sg_nvme_passthru_cmd cmd;

    memset(&cmd, 0, sizeof(cmd));
    cmd.opcode = SG_NVME_AD_IDENTIFY;
    cmd.nsid = nsid;
    cmd.cdw10 = cns;
    cmd.addr = (uint64_t)(sg_uintptr_t)up;
    cmd.data_len = u_len;
    return sg_nvme_admin_cmd_f(ptp, &cmd, up, true, time_secs, vb);
}

/* Currently only caches associated identify controller response (4096 bytes).
 * Returns 0 on success; otherwise a positive value is returned */
static int
sg_nvme_cache_identify_ctl(struct sg_pt_linux_scsi * ptp, int time_secs,
                           int vb)
{
    int ret;
    uint32_t pg_sz = sg_get_page_size();
    uint8_t * up;

    up = sg_memalign(pg_sz, pg_sz, &ptp->free_nvme_id_ctlp, false);
    ptp->nvme_id_ctlp = up;
    if (NULL == up) {
        pr2ws("%s: sg_memalign() failed to get memory\n", __func__);
        return sg_convert_errno(ENOMEM);
    }
    ret = sg_nvme_do_identify(ptp, 0x1 /* CNS */, 0 /* nsid */, time_secs,
                              pg_sz, up, vb);
    if (0 == ret)
       sg_nvme_check_enclosure_override(ptp, vb);
    return (ret < 0) ? sg_convert_errno(-ret) : ret;
}

/* If nsid==0 then set cmdp->nsid to SG_NVME_BROADCAST_NSID. */
static int
sg_nvme_get_features(struct sg_pt_linux_scsi * ptp, int feature_id,
                     int select, uint32_t nsid, uint64_t din_addr,
                     int time_secs, int vb)
{
    int res;
    struct sg_nvme_passthru_cmd cmd;
    struct sg_nvme_passthru_cmd * cmdp = &cmd;

    if (vb > 4)
        pr2ws("%s: feature_id=0x%x, select=%d\n", __func__, feature_id,
              select);
    memset(cmdp, 0, sizeof(*cmdp));
    cmdp->opcode = SG_NVME_AD_GET_FEATURE;
    cmdp->nsid = nsid ? nsid : SG_NVME_BROADCAST_NSID;
    select &= 0x7;
    feature_id &= 0xff;
    cmdp->cdw10 = (select << 8) | feature_id;
    if (din_addr)
        cmdp->addr = din_addr;
    cmdp->timeout_ms = (time_secs < 0) ? 0 : (1000 * time_secs);
    res = sg_nvme_admin_cmd_f(ptp, cmdp, NULL, false, time_secs, vb);
    if (res)
        return res;
    ptp->os_err = 0;
    ptp->nvme_status = 0;
    return 0;
}

static int
sg_nvme_set_features(struct sg_pt_linux_scsi * ptp, int feature_id,
                     bool save, uint32_t nsid, uint64_t dout_addr,
                     uint32_t cdw11, uint32_t cdw12, uint32_t cdw13,
                     uint32_t cdw15, int time_secs, int vb)
{
    int res;
    struct sg_nvme_passthru_cmd cmd;
    struct sg_nvme_passthru_cmd * cmdp = &cmd;

    if (vb > 4)
        pr2ws("%s: feature_id=0x%x, save=%d\n", __func__, feature_id,
              (int)save);
    memset(cmdp, 0, sizeof(*cmdp));
    cmdp->opcode = SG_NVME_AD_SET_FEATURE;
    cmdp->nsid = nsid ? nsid : SG_NVME_BROADCAST_NSID;
    cmdp->cdw10 = (feature_id & 0xff);
    if (save)
        cmdp->cdw10 |= (1U << 31);      /* set bit 31 */
    cmdp->cdw11 = cdw11;
    cmdp->cdw12 = cdw12;
    cmdp->cdw13 = cdw13;
    cmdp->cdw14 = 0;            /* no UUID support yet */
    cmdp->cdw15 = cdw15;
    if (dout_addr)
        cmdp->addr = dout_addr;
    cmdp->timeout_ms = (time_secs < 0) ? 0 : (1000 * time_secs);
    res = sg_nvme_admin_cmd_f(ptp, cmdp, NULL, false, time_secs, vb);
    if (res) {
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        }
        return res;
    }
    ptp->os_err = 0;
    ptp->nvme_status = 0;
    return 0;
}

static int
sg_ln_snt_inq(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
              int time_secs, int vb)
{
    bool evpd;
    uint16_t pg_cd;
    int res;
    int n = 0;
    int len = ptp->io_hdr.din_xfer_len;
    uint32_t pg_sz = sg_get_page_size();
    uint8_t * bp;
    uint8_t * nvme_id_nsp = NULL;
    uint8_t * free_nvme_id_nsp = NULL;
    struct sg_snt_result_t sg_snt_result;

    if (vb > 5)
        pr2ws("%s: time_secs=%d\n", __func__, time_secs);

    bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    if (0x2 & cdbp[1]) {        /* Reject CmdDt=1 */
        mk_sense_invalid_fld(ptp, true, 1, 1, vb);
        return 0;
    }
    if (NULL == ptp->nvme_id_ctlp) {
        res = sg_nvme_cache_identify_ctl(ptp, time_secs, vb);
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else if (res) /* should be negative errno */
            return res;
    }
    evpd = !!(0x1 & cdbp[1]);
    pg_cd = cdbp[2];
    if (evpd && (0x83 == pg_cd)) {
        if ((ptp->nvme_nsid > 0) &&
            (ptp->nvme_nsid < SG_NVME_BROADCAST_NSID)) {
            nvme_id_nsp = sg_memalign(pg_sz, pg_sz, &free_nvme_id_nsp, false);
            if (nvme_id_nsp) {   /* CNS=0x0 Identify namespace */
                 res = sg_nvme_do_identify(ptp, 0x0, ptp->nvme_nsid,
                                           time_secs, pg_sz, nvme_id_nsp, vb);
                if (SG_LIB_NVME_STATUS == res) {
                    mk_sense_from_nvme_status(ptp, vb);
                    return 0;
                } else if (res) /* should be negative errno */
                    return res;
            }
        }
    }
    ptp->dev_stat.vb = vb;
    n = sg_snt_resp_inq(&ptp->dev_stat, cdbp, ptp->nvme_id_ctlp, nvme_id_nsp,
                        bp, len, &sg_snt_result);
    if (n < 0) {
        mk_sense_from_snt_result(ptp, &sg_snt_result, vb);
        n = 0;
    }
    ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
    if (free_nvme_id_nsp) {
        free(free_nvme_id_nsp);
    }
    return 0;
}

static int
sg_ln_snt_rluns(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                int time_secs, int vb)
{
    int res, n;
    int len = ptp->io_hdr.din_xfer_len;
    uint8_t * bp;
    struct sg_snt_result_t sg_snt_result;

    if (vb > 5)
        pr2ws("%s: time_secs=%d\n", __func__, time_secs);

    if (NULL == ptp->nvme_id_ctlp) {
        res = sg_nvme_cache_identify_ctl(ptp, time_secs, vb);
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else if (res)
            return res;
    }
    bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    ptp->dev_stat.vb = vb;
    n = sg_snt_resp_rluns(&ptp->dev_stat, cdbp, ptp->nvme_id_ctlp,
                          ptp->nvme_nsid, bp, len, &sg_snt_result);
    if (n < 0) {
        mk_sense_from_snt_result(ptp, &sg_snt_result, vb);
        n = 0;
    }
    ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
    return 0;
}

#if 0
struct sg_snt_dev_state_t * dsp, const uint8_t * cdbp,
                  const uint8_t * nvme_id_ctlp, uint32_t nsid, uint8_t * dip,
                  int mx_di_len, struct sg_snt_result_t * resp);
    max_nsid = sg_get_unaligned_le32(ptp->nvme_id_ctlp + 516);
    switch (sel_report) {
    case 0:
    case 2:
        num = max_nsid;
        break;
    case 1:
    case 0x10:
    case 0x12:
        num = 0;
        break;
    case 0x11:
        num = (1 == ptp->nvme_nsid) ? max_nsid :  0;
        break;
    default:
        if (vb > 1)
            pr2ws("%s: bad select_report value: 0x%x\n", __func__,
                  sel_report);
        mk_sense_invalid_fld(ptp, true, 2, 7, vb);
        return 0;
    }
    rl_doutp = (uint8_t *)calloc(num + 1, 8);
    if (NULL == rl_doutp) {
        pr2ws("%s: calloc() failed to get memory\n", __func__);
        return sg_convert_errno(ENOMEM);
    }
    for (k = 0, up = rl_doutp + 8; k < num; ++k, up += 8)
        sg_put_unaligned_be16(k, up);
    n = num * 8;
    sg_put_unaligned_be32(n, rl_doutp);
    n+= 8;
    if (alloc_len > 0) {
        n = (alloc_len < n) ? alloc_len : n;
        n = (n < ptp->io_hdr.din_xfer_len) ? n : ptp->io_hdr.din_xfer_len;
        ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
        if (n > 0)
            memcpy((uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp, rl_doutp,
                   n);
    }
    res = 0;
    free(rl_doutp);
    return res;
}
#endif

static int
sg_snt_tur(struct sg_pt_linux_scsi * ptp, int time_secs, int vb)
{
    int res;
    uint32_t pow_state;

    if (vb > 5)
        pr2ws("%s: start\n", __func__);
    if (NULL == ptp->nvme_id_ctlp) {
        res = sg_nvme_cache_identify_ctl(ptp, time_secs, vb);
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else if (res)
            return res;
    }
    res = sg_nvme_get_features(ptp, 2 /* Power Management */, 0 /* current */,
                               0, 0, time_secs, vb);
    if (0 != res) {
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else
            return res;
    }
    pow_state = (0x1f & ptp->nvme_result);
    if (vb > 5)
        pr2ws("%s: pow_state=%u\n", __func__, pow_state);
#if 0   /* pow_state bounces around too much on laptop */
    if (pow_state)
        mk_sense_asc_ascq(ptp, SPC_SK_NOT_READY, LOW_POWER_COND_ON_ASC, 0,
                          vb);
#endif
    return 0;
}

static int
sg_snt_req_sense(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                  int time_secs, int vb)
{
    bool desc;
    int res;
    uint32_t pow_state, alloc_len, n;
    uint8_t rs_dout[64];

    if (vb > 5)
        pr2ws("%s: time_secs=%d\n", __func__, time_secs);
    if (NULL == ptp->nvme_id_ctlp) {
        res = sg_nvme_cache_identify_ctl(ptp, time_secs, vb);
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else if (res)
            return res;
    }
    desc = !!(0x1 & cdbp[1]);
    alloc_len = cdbp[4];
    res = sg_nvme_get_features(ptp, 0x2 /* Power Management */,
                               0 /* current */, 0, 0, time_secs, vb);
    if (0 != res) {
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else
            return res;
    }
    ptp->io_hdr.response_len = 0;
    pow_state = (0x1f & ptp->nvme_result);
    if (vb > 5)
        pr2ws("%s: pow_state=%u\n", __func__, pow_state);
    memset(rs_dout, 0, sizeof(rs_dout));
    if (pow_state)
        sg_build_sense_buffer(desc, rs_dout, SPC_SK_NO_SENSE,
                              LOW_POWER_COND_ON_ASC, 0);
    else
        sg_build_sense_buffer(desc, rs_dout, SPC_SK_NO_SENSE,
                              NO_ADDITIONAL_SENSE, 0);
    n = desc ? 8 : 18;
    n = (n < alloc_len) ? n : alloc_len;
    n = (n < ptp->io_hdr.din_xfer_len) ? n : ptp->io_hdr.din_xfer_len;
    ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
    if (n > 0)
        memcpy((uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp, rs_dout, n);
    return 0;
}

static uint8_t pc_t10_2_select[] = {0, 3, 1, 2};

/* For MODE SENSE(10) and MODE SELECT(10). 6 byte variants not supported */
static int
sg_ln_snt_mode_ss(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                  int time_secs, int vb)
{
    bool is_msense = (SCSI_MODE_SENSE10_OPC == cdbp[0]);
    int res, len;
    int n = 0;
    uint8_t * bp;
    struct sg_snt_result_t sg_snt_result;

    if (vb > 5)
        pr2ws("%s: mode se%s\n", __func__, (is_msense ? "nse" : "lect"));
    if (NULL == ptp->nvme_id_ctlp) {
        res = sg_nvme_cache_identify_ctl(ptp, time_secs, vb);
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else if (res)
            return res;
    }
    ptp->dev_stat.vb = vb;
    if (is_msense) {    /* MODE SENSE(10) */
        uint8_t pc_t10 = (cdbp[2] >> 6) & 0x3;
        int mp_t10 = (cdbp[2] & 0x3f);

        if ((0x3f == mp_t10) || (0x8 /* caching mpage */ == mp_t10)) {
            /* 0x6 is "Volatile write cache" feature id */
            res = sg_nvme_get_features(ptp, 0x6, pc_t10_2_select[pc_t10], 0,
                                       0, time_secs, vb);
            if (0 != res) {
                if (SG_LIB_NVME_STATUS == res) {
                    mk_sense_from_nvme_status(ptp, vb);
                    return 0;
                } else
                    return res;
            }
            ptp->dev_stat.wce = !!(0x1 & ptp->nvme_result);
        }
        len = ptp->io_hdr.din_xfer_len;
        bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
        n = sg_snt_resp_mode_sense10(&ptp->dev_stat, cdbp, bp, len,
                                     &sg_snt_result);
        ptp->io_hdr.din_resid = (n >= 0) ? len - n : len;
    } else {            /* MODE SELECT(10) */
        bool sp = !!(0x1 & cdbp[1]);    /* Save Page indication */
        uint8_t pre_enc_ov = ptp->dev_stat.enclosure_override;

        len = ptp->io_hdr.dout_xfer_len;
        bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.dout_xferp;
        ptp->dev_stat.wce_changed = false;
        n = sg_snt_resp_mode_select10(&ptp->dev_stat, cdbp, bp, len,
                                      &sg_snt_result);
        if (ptp->dev_stat.wce_changed) {
            /* feature_id=0x6  for "volatile write cache" */
            res = sg_nvme_set_features(ptp, 0x6, sp, ptp->nvme_nsid, 0,
                                       ptp->dev_stat.wce, 0, 0, 0, time_secs,
                                       vb);
            if (res)
                return res;
        }
        if (pre_enc_ov != ptp->dev_stat.enclosure_override)
            sg_nvme_check_enclosure_override(ptp, vb);/* ENC_OV has changed */
    }
    if (n < 0) {
        mk_sense_from_snt_result(ptp, &sg_snt_result, vb);
        n = 0;
    }
    ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
    return 0;
}

/* This is not really a SNTL. For SCSI SEND DIAGNOSTIC(PF=1) NVMe-MI
 * has a special command (SES Send) to tunnel through pages to an
 * enclosure. The NVMe enclosure is meant to understand the SES
 * (SCSI Enclosure Services) use of diagnostics pages that are
 * related to SES. */
static int
sg_snt_senddiag(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                int time_secs, int vb)
{
    bool pf, self_test;
    int res;
    uint8_t st_cd, dpg_cd;
    uint32_t alloc_len, n, dout_len, dpg_len;
    const uint32_t pg_sz = sg_get_page_size();
    uint8_t * dop;
    struct sg_nvme_passthru_cmd cmd;
    uint8_t * cmd_up = (uint8_t *)&cmd;

    st_cd = 0x7 & (cdbp[1] >> 5);
    self_test = !! (0x4 & cdbp[1]);
    pf = !! (0x10 & cdbp[1]);
    if (vb > 5)
        pr2ws("%s: pf=%d, self_test=%d (st_code=%d)\n", __func__, (int)pf,
              (int)self_test, (int)st_cd);
    if (self_test || st_cd) {
        uint32_t nvme_dst;

        memset(cmd_up, 0, sizeof(cmd));
        cmd_up[SG_NVME_OPCODE] = SG_NVME_AD_DEV_SELT_TEST;
        /* just this namespace (if there is one) and controller */
        sg_put_unaligned_le32(ptp->nvme_nsid, cmd_up + SG_NVME_NSID);
        switch (st_cd) {
        case 0: /* Here if self_test is set, do short self-test */
        case 1: /* Background short */
        case 5: /* Foreground short */
            nvme_dst = 1;
            break;
        case 2: /* Background extended */
        case 6: /* Foreground extended */
            nvme_dst = 2;
            break;
        case 4: /* Abort self-test */
            nvme_dst = 0xf;
            break;
        default:
            pr2ws("%s: bad self-test code [0x%x]\n", __func__, st_cd);
            mk_sense_invalid_fld(ptp, true, 1, 7, vb);
            return 0;
        }
        sg_put_unaligned_le32(nvme_dst, cmd_up + SG_NVME_CDW10);
        res = sg_nvme_admin_cmd_f(ptp, &cmd, NULL, false, time_secs, vb);
        if (0 != res) {
            if (SG_LIB_NVME_STATUS == res) {
                mk_sense_from_nvme_status(ptp, vb);
                return 0;
            } else
                return res;
        }
    }
    alloc_len = sg_get_unaligned_be16(cdbp + 3); /* parameter list length */
    dout_len = ptp->io_hdr.dout_xfer_len;
    if (pf) {
        if (0 == alloc_len) {
            mk_sense_invalid_fld(ptp, true, 3, 7, vb);
            if (vb)
                pr2ws("%s: PF bit set bit param_list_len=0\n", __func__);
            return 0;
        }
    } else {    /* PF bit clear */
        if (alloc_len) {
            mk_sense_invalid_fld(ptp, true, 3, 7, vb);
            if (vb)
                pr2ws("%s: param_list_len>0 but PF clear\n", __func__);
            return 0;
        } else
            return 0;     /* nothing to do */
    }
    if (dout_len < 4) {
        if (vb)
            pr2ws("%s: dout length (%u bytes) too short\n", __func__,
                  dout_len);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    n = dout_len;
    n = (n < alloc_len) ? n : alloc_len;
    dop = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.dout_xferp;
    if (! sg_is_aligned(dop, pg_sz)) {  /* is dop page aligned ? */
        if (vb)
            pr2ws("%s: dout [0x%" PRIx64 "] not page aligned\n", __func__,
                  (uint64_t)ptp->io_hdr.dout_xferp);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    dpg_cd = dop[0];
    dpg_len = sg_get_unaligned_be16(dop + 2) + 4;
    /* should we allow for more than one D_PG is dout ?? */
    n = (n < dpg_len) ? n : dpg_len;    /* not yet ... */

    if (vb)
        pr2ws("%s: passing through d_pg=0x%x, len=%u to NVME_MI SES send\n",
              __func__, dpg_cd, dpg_len);
    memset(&cmd, 0, sizeof(cmd));
    cmd.opcode = SG_NVME_AD_MI_SEND;
    cmd.addr = (uint64_t)(sg_uintptr_t)dop;
    cmd.data_len = 0x1000;   /* NVMe 4k page size. Maybe determine this? */
                             /* dout_len > 0x1000, is this a problem?? */
    cmd.cdw10 = 0x0804;      /* NVMe Message Header */
    cmd.cdw11 = 0x9;         /* nvme_mi_ses_send; (0x8 -> mi_ses_recv) */
    cmd.cdw13 = n;
    res = sg_nvme_admin_cmd_f(ptp, &cmd, dop, false, time_secs, vb);
    if (0 != res) {
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        }
    }
    return res;
}

/* This is not really a SNTL. For SCSI RECEIVE DIAGNOSTIC RESULTS(PCV=1)
 * NVMe-MI has a special command (SES Receive) to read pages through a
 * tunnel from an enclosure. The NVMe enclosure is meant to understand the
 * SES (SCSI Enclosure Services) use of diagnostics pages that are
 * related to SES. */
static int
sg_snt_recvdiag(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                int time_secs, int vb)
{
    bool pcv;
    int res;
    uint8_t dpg_cd;
    uint32_t alloc_len, n, din_len;
    uint32_t pg_sz = sg_get_page_size();
    uint8_t * dip;
    struct sg_nvme_passthru_cmd cmd;

    pcv = !! (0x1 & cdbp[1]);
    dpg_cd = cdbp[2];
    alloc_len = sg_get_unaligned_be16(cdbp + 3); /* parameter list length */
    if (vb > 5)
        pr2ws("%s: dpg_cd=0x%x, pcv=%d, alloc_len=0x%x\n", __func__,
              dpg_cd, (int)pcv, alloc_len);
    din_len = ptp->io_hdr.din_xfer_len;
    n = din_len;
    n = (n < alloc_len) ? n : alloc_len;
    dip = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    if (! sg_is_aligned(dip, pg_sz)) {
        if (vb)
            pr2ws("%s: din [0x%" PRIx64 "] not page aligned\n", __func__,
                  (uint64_t)ptp->io_hdr.din_xferp);
        return SCSI_PT_DO_BAD_PARAMS;
    }

    if (vb)
        pr2ws("%s: expecting d_pg=0x%x from NVME_MI SES receive\n", __func__,
              dpg_cd);
    memset(&cmd, 0, sizeof(cmd));
    cmd.opcode = SG_NVME_AD_MI_RECEIVE;
    cmd.addr = (uint64_t)(sg_uintptr_t)dip;
    cmd.data_len = 0x1000;   /* NVMe 4k page size. Maybe determine this? */
                             /* din_len > 0x1000, is this a problem?? */
    cmd.cdw10 = 0x0804;      /* NVMe Message Header */
    cmd.cdw11 = 0x8;         /* nvme_mi_ses_receive */
    cmd.cdw12 = dpg_cd;
    cmd.cdw13 = n;
    res = sg_nvme_admin_cmd_f(ptp, &cmd, dip, true, time_secs, vb);
    if (0 != res) {
        if (SG_LIB_NVME_STATUS == res) {
            mk_sense_from_nvme_status(ptp, vb);
            return 0;
        } else
            return res;
    }
    ptp->io_hdr.din_resid = din_len - n;
    return res;
}

static int
sg_ln_snt_rep_opcodes(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                      int time_secs, int vb)
{
    int n, len;
    uint8_t * bp;
    struct sg_snt_result_t sg_snt_result;

    if (vb > 5)
        pr2ws("%s: time_secs=%d\n", __func__, time_secs);
    len = ptp->io_hdr.din_xfer_len;
    bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    ptp->dev_stat.vb = vb;
    n = sg_snt_resp_rep_opcodes(&ptp->dev_stat, cdbp, 0, 0, bp, len,
                                &sg_snt_result);
    if (n < 0) {
        mk_sense_from_snt_result(ptp, &sg_snt_result, vb);
        n = 0;
    }
    ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
    return 0;
}

static int
sg_ln_snt_rep_tmfs(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                int time_secs, int vb)
{
    int n, len;
    uint8_t * bp;
    struct sg_snt_result_t sg_snt_result;

    if (vb > 5)
        pr2ws("%s: time_secs=%d\n", __func__, time_secs);
    len = ptp->io_hdr.din_xfer_len;
    bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    ptp->dev_stat.vb = vb;
    n = sg_snt_resp_rep_tmfs(&ptp->dev_stat, cdbp, bp, len,
                                &sg_snt_result);
    if (n < 0) {
        mk_sense_from_snt_result(ptp, &sg_snt_result, vb);
        n = 0;
    }
    ptp->io_hdr.din_resid = ptp->io_hdr.din_xfer_len - n;
    return 0;
}

/* Note that the "Returned logical block address" (RLBA) field in the SCSI
 * READ CAPACITY (10+16) command's response provides the address of the _last_
 * LBA (counting origin 0) which will be one less that the "size" in the
 * NVMe Identify command response's NSZE field. One problem is that in
 * some situations NSZE can be zero: temporarily set RLBA field to 0
 * (implying a 1 LB logical units size) pending further research. The LBLIB
 * is the "Logical Block Length In Bytes" field in the RCAP response. */
static int
sg_snt_readcap(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
               int time_secs, int vb)
{
    bool is_rcap10 = (SCSI_READ_CAPACITY10_OPC == cdbp[0]);
    int res, n, len, alloc_len, dps;
    uint8_t flbas, index, lbads;  /* NVMe: 2**LBADS --> Logical Block size */
    uint32_t lbafx;     /* NVME: LBAF0...LBAF15, each 16 bytes */
    uint32_t pg_sz = sg_get_page_size();
    uint64_t nsze;
    uint8_t * bp;
    uint8_t * up;
    uint8_t * free_up = NULL;
    uint8_t resp[32];

    if (vb > 5)
        pr2ws("%s: RCAP%d, time_secs=%d\n", __func__,
              (is_rcap10 ? 10 : 16), time_secs);
    up = sg_memalign(pg_sz, pg_sz, &free_up, false);
    if (NULL == up) {
        pr2ws("%s: sg_memalign() failed to get memory\n", __func__);
        return sg_convert_errno(ENOMEM);
    }
    res = sg_nvme_do_identify(ptp, 0x0 /* CNS */, ptp->nvme_nsid, time_secs,
                              pg_sz, up, vb);
    if (res < 0) {
        res = sg_convert_errno(-res);
        goto fini;
    }
    memset(resp, 0, sizeof(resp));
    nsze = sg_get_unaligned_le64(up + 0);
    flbas = up[26];     /* NVME FLBAS field from Identify, want LBAF[flbas] */
    index = 128 + (4 * (flbas & 0xf));
    lbafx = sg_get_unaligned_le32(up + index);
    lbads = (lbafx >> 16) & 0xff;       /* bits 16 to 23 inclusive, pow2 */
    if (is_rcap10) {
        alloc_len = 8;  /* implicit, not in cdb */
        if (nsze > 0xffffffff)
            sg_put_unaligned_be32(0xffffffff, resp + 0);
        else if (0 == nsze)     /* no good answer here */
            sg_put_unaligned_be32(0, resp + 0);         /* SCSI RLBA field */
        else
            sg_put_unaligned_be32((uint32_t)(nsze - 1), resp + 0);
        sg_put_unaligned_be32(1 << lbads, resp + 4);    /* SCSI LBLIB field */
    } else {
        alloc_len = sg_get_unaligned_be32(cdbp + 10);
        dps = up[29];
        if (0x7 & dps) {
            resp[12] = 0x1;
            n = (0x7 & dps) - 1;
            if (n > 0)
                resp[12] |= (n + n);
        }
        if (0 == nsze)  /* no good answer here */
            sg_put_unaligned_be64(0, resp + 0);
        else
            sg_put_unaligned_be64(nsze - 1, resp + 0);
        sg_put_unaligned_be32(1 << lbads, resp + 8);    /* SCSI LBLIB field */
    }
    len = ptp->io_hdr.din_xfer_len;
    bp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    n = 32;
    n = (n < alloc_len) ? n : alloc_len;
    n = (n < len) ? n : len;
    ptp->io_hdr.din_resid = len - n;
    if (n > 0)
        memcpy(bp, resp, n);
fini:
    if (free_up)
        free(free_up);
    return res;
}

static int
do_nvm_pt_low(struct sg_pt_linux_scsi * ptp,
              struct sg_nvme_passthru_cmd *cmdp, void * dp, int dlen,
              bool is_read, int time_secs, int vb)
{
    const uint32_t cmd_len = sizeof(struct sg_nvme_passthru_cmd);
    int res;
    uint32_t n;
    uint16_t sct_sc;
    const uint8_t * up = ((const uint8_t *)cmdp) + SG_NVME_OPCODE;
    char nam[64];

    if (vb)
        sg_get_nvme_opcode_name(*up, false /* NVM */ , sizeof(nam), nam);
    else
        nam[0] = '\0';
    cmdp->timeout_ms = (time_secs < 0) ? (-time_secs) : (1000 * time_secs);
    ptp->os_err = 0;
    if (vb > 2) {
        pr2ws("NVMe NVM command: %s\n", nam);
        hex2stderr((const uint8_t *)cmdp, cmd_len, 1);
        if ((vb > 4) && (! is_read) && dp) {
            if (dlen > 0) {
                n = dlen;
                if ((dlen < 512) || (vb > 5))
                    pr2ws("\nData-out buffer (%u bytes):\n", n);
                else {
                    pr2ws("\nData-out buffer (first 512 of %u bytes):\n", n);
                    n = 512;
                }
                hex2stderr((const uint8_t *)dp, n, 0);
            }
        }
    }
    res = ioctl(ptp->dev_fd, NVME_IOCTL_IO_CMD, cmdp);
    if (res < 0) {  /* OS error (errno negated) */
        ptp->os_err = -res;
        if (vb > 1) {
            pr2ws("%s: ioctl for %s [0x%x] failed: %s "
                  "(errno=%d)\n", __func__, nam, *up, strerror(-res), -res);
        }
        return res;
    }

    /* Now res contains NVMe completion queue CDW3 31:17 (15 bits) */
    ptp->nvme_result = cmdp->result;
    if ((! ptp->nvme_our_snt) && ptp->io_hdr.response &&
        (ptp->io_hdr.max_response_len > 3)) {
        /* build 32 byte "sense" buffer */
        uint8_t * sbp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.response;
        uint16_t st = (uint16_t)res;

        n = ptp->io_hdr.max_response_len;
        n = (n < 32) ? n : 32;
        memset(sbp, 0 , n);
        ptp->io_hdr.response_len = n;
        sg_put_unaligned_le32(cmdp->result, sbp + SG_NVME_CQ_RESULT);
        if (n > 15) /* LSBit will be 0 (Phase bit) after (st << 1) */
            sg_put_unaligned_le16(st << 1, sbp + SG_NVME_CQ_STATUS_P);
    }
    /* clear upper bits (DNR and More) leaving ((SCT << 8) | SC) */
    sct_sc = 0x7ff & res;       /* 11 bits */
    ptp->nvme_status = sct_sc;
    ptp->nvme_stat_dnr = !!(0x4000 & res);
    ptp->nvme_stat_more = !!(0x2000 & res);
    if (sct_sc) {  /* when non-zero, treat as command error */
        if (vb > 1) {
            char b[80];

            pr2ws("%s: ioctl for %s [0x%x] failed, status: %s [0x%x]\n",
                   __func__, nam, *up,
                  sg_get_nvme_cmd_status_str(sct_sc, sizeof(b), b), sct_sc);
        }
        return SG_LIB_NVME_STATUS;      /* == SCSI_PT_DO_NVME_STATUS */
    }
    if ((vb > 4) && is_read && dp) {
        if (dlen > 0) {
            n = dlen;
            if ((dlen < 1024) || (vb > 5))
                pr2ws("\nData-in buffer (%u bytes):\n", n);
            else {
                pr2ws("\nData-in buffer (first 1024 of %u bytes):\n", n);
                n = 1024;
            }
            hex2stderr((const uint8_t *)dp, n, 0);
        }
    }
    return 0;
}

/* Since ptp can be a char device (e.g. /dev/nvme0) or a blocks device
 * (e.g. /dev/nvme0n1 or /dev/nvme0n1p3) use NVME_IOCTL_IO_CMD which is
 * common to both (and takes a timeout). The difficult is that
 * NVME_IOCTL_IO_CMD takes a nvme_passthru_cmd object point. */
static int
sg_do_nvm_cmd(struct sg_pt_linux_scsi * ptp, struct sg_nvme_user_io * iop,
              uint32_t dlen, bool is_read, int time_secs, int vb)
{

    struct sg_nvme_passthru_cmd nvme_pt_cmd;
    struct sg_nvme_passthru_cmd *cmdp = &nvme_pt_cmd;
    void * dp = (void *)(sg_uintptr_t)iop->addr;

    memset(cmdp, 0, sizeof(*cmdp));
    cmdp->opcode = iop->opcode;
    cmdp->flags = iop->flags;
    cmdp->nsid = ptp->nvme_nsid;
    cmdp->addr = iop->addr;
    cmdp->data_len = dlen;
    cmdp->cdw10 = iop->slba & 0xffffffff;
    cmdp->cdw11 = (iop->slba >> 32) & 0xffffffff;
    cmdp->cdw12 = iop->nblocks; /* lower 16 bits already "0's based" count */

    return do_nvm_pt_low(ptp, cmdp, dp, dlen, is_read, time_secs, vb);
}

static int
sg_snt_rread(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
             int time_secs, int vb)
{
    bool is_read10 = (SCSI_READ10_OPC == cdbp[0]);
    bool have_fua = !!(cdbp[1] & 0x8);
    int res;
    uint32_t nblks_t10 = 0;
    struct sg_nvme_user_io io;
    struct sg_nvme_user_io * iop = &io;

    if (vb > 5)
        pr2ws("%s: fua=%d, time_secs=%d\n", __func__, (int)have_fua,
              time_secs);
    memset(iop, 0, sizeof(*iop));
    iop->opcode = SG_NVME_NVM_READ;
    if (is_read10) {
        iop->slba = sg_get_unaligned_be32(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be16(cdbp + 7);
    } else {
        iop->slba = sg_get_unaligned_be64(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be32(cdbp + 10);
        if (nblks_t10 > (UINT16_MAX + 1)) {
            mk_sense_invalid_fld(ptp, true, 11, -1, vb);
            return 0;
        }
    }
    if (0 == nblks_t10) {         /* NOP in SCSI */
        if (vb > 4)
            pr2ws("%s: nblks_t10 is 0, a NOP in SCSI, can't map to NVMe\n",
                  __func__);
        return 0;
    }
    iop->nblocks = nblks_t10 - 1;       /* crazy "0's based" */
    if (have_fua)
        iop->control |= SG_NVME_RW_CONTROL_FUA;
    iop->addr = (uint64_t)ptp->io_hdr.din_xferp;
    res = sg_do_nvm_cmd(ptp, iop, ptp->io_hdr.din_xfer_len,
                        true /* is_read */, time_secs, vb);
    if (SG_LIB_NVME_STATUS == res) {
        mk_sense_from_nvme_status(ptp, vb);
        return 0;
    }
    return res;
}

static int
sg_snt_write(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
             int time_secs, int vb)
{
    bool is_write10 = (SCSI_WRITE10_OPC == cdbp[0]);
    bool have_fua = !!(cdbp[1] & 0x8);
    int res;
    uint32_t nblks_t10 = 0;
    struct sg_nvme_user_io io;
    struct sg_nvme_user_io * iop = &io;

    if (vb > 5)
        pr2ws("%s: fua=%d, time_secs=%d\n", __func__, (int)have_fua,
              time_secs);
    memset(iop, 0, sizeof(*iop));
    iop->opcode = SG_NVME_NVM_WRITE;
    if (is_write10) {
        iop->slba = sg_get_unaligned_be32(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be16(cdbp + 7);
    } else {
        iop->slba = sg_get_unaligned_be64(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be32(cdbp + 10);
        if (nblks_t10 > (UINT16_MAX + 1)) {
            mk_sense_invalid_fld(ptp, true, 11, -1, vb);
            return 0;
        }
    }
    if (0 == nblks_t10) { /* NOP in SCSI */
        if (vb > 4)
            pr2ws("%s: nblks_t10 is 0, a NOP in SCSI, can't map to NVMe\n",
                  __func__);
        return 0;
    }
    iop->nblocks = nblks_t10 - 1;
    if (have_fua)
        iop->control |= SG_NVME_RW_CONTROL_FUA;
    iop->addr = (uint64_t)ptp->io_hdr.dout_xferp;
    res = sg_do_nvm_cmd(ptp, iop, ptp->io_hdr.dout_xfer_len, false,
                        time_secs, vb);
    if (SG_LIB_NVME_STATUS == res) {
        mk_sense_from_nvme_status(ptp, vb);
        return 0;
    }
    return res;
}

static int
sg_snt_verify(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
           int time_secs, int vb)
{
    bool is_verify10 = (SCSI_VERIFY10_OPC == cdbp[0]);
    uint8_t bytchk = (cdbp[1] >> 1) & 0x3;
    uint32_t dlen = 0;
    int res;
    uint32_t nblks_t10 = 0;
    struct sg_nvme_user_io io;
    struct sg_nvme_user_io * iop = &io;

    if (vb > 5)
        pr2ws("%s: bytchk=%d, time_secs=%d\n", __func__, bytchk, time_secs);
    if (bytchk > 1) {
        mk_sense_invalid_fld(ptp, true, 1, 2, vb);
        return 0;
    }
    memset(iop, 0, sizeof(*iop));
    iop->opcode = bytchk ? SG_NVME_NVM_COMPARE : SG_NVME_NVM_VERIFY;
    if (is_verify10) {
        iop->slba = sg_get_unaligned_be32(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be16(cdbp + 7);
    } else {
        iop->slba = sg_get_unaligned_be64(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be32(cdbp + 10);
        if (nblks_t10 > (UINT16_MAX + 1)) {
            mk_sense_invalid_fld(ptp, true, 11, -1, vb);
            return 0;
        }
    }
    if (0 == nblks_t10) { /* NOP in SCSI */
        if (vb > 4)
            pr2ws("%s: nblks_t10 is 0, a NOP in SCSI, can't map to NVMe\n",
                  __func__);
        return 0;
    }
    iop->nblocks = nblks_t10 - 1;
    if (bytchk) {
        iop->addr = (uint64_t)ptp->io_hdr.dout_xferp;
        dlen = ptp->io_hdr.dout_xfer_len;
    }
    res = sg_do_nvm_cmd(ptp, iop, dlen, false, time_secs, vb);
    if (SG_LIB_NVME_STATUS == res) {
        mk_sense_from_nvme_status(ptp, vb);
        return 0;
    }
    return res;
}

static int
sg_snt_write_same(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                  int time_secs, int vb)
{
    bool is_ws10 = (SCSI_WRITE_SAME10_OPC == cdbp[0]);
    bool ndob = is_ws10 ? false : !!(0x1 & cdbp[1]);
    int res;
    int nblks_t10 = 0;
    struct sg_nvme_user_io io;
    struct sg_nvme_user_io * iop = &io;

    if (vb > 5)
        pr2ws("%s: ndob=%d, time_secs=%d\n", __func__, (int)ndob, time_secs);
    if (! ndob) {
        int flbas, index, lbafx, lbads, lbsize;
        uint8_t * up;
        uint8_t * dp;

        dp = (uint8_t *)(sg_uintptr_t)ptp->io_hdr.dout_xferp;
        if (dp == NULL)
            return sg_convert_errno(ENOMEM);
        if (NULL == ptp->nvme_id_ctlp) {
            res = sg_nvme_cache_identify_ctl(ptp, time_secs, vb);
            if (SG_LIB_NVME_STATUS == res) {
                mk_sense_from_nvme_status(ptp, vb);
                return 0;
            } else if (res)
                return res;
        }
        up = ptp->nvme_id_ctlp;
        flbas = up[26];     /* NVME FLBAS field from Identify */
        index = 128 + (4 * (flbas & 0xf));
        lbafx = sg_get_unaligned_le32(up + index);
        lbads = (lbafx >> 16) & 0xff;  /* bits 16 to 23 inclusive, pow2 */
        lbsize = 1 << lbads;
        if (! sg_all_zeros(dp, lbsize)) {
            mk_sense_asc_ascq(ptp, SPC_SK_ILLEGAL_REQUEST, PCIE_ERR_ASC,
                              PCIE_UNSUPP_REQ_ASCQ, vb);
            return 0;
        }
        /* so given single LB full of zeros, can translate .... */
    }
    memset(iop, 0, sizeof(*iop));
    iop->opcode =  SG_NVME_NVM_WRITE_ZEROES;
    if (is_ws10) {
        iop->slba = sg_get_unaligned_be32(cdbp + 2);
        nblks_t10 = sg_get_unaligned_be16(cdbp + 7);
    } else {
        uint32_t num = sg_get_unaligned_be32(cdbp + 10);

        iop->slba = sg_get_unaligned_be64(cdbp + 2);
        if (num > (UINT16_MAX + 1)) {
            mk_sense_invalid_fld(ptp, true, 11, -1, vb);
            return 0;
        } else
            nblks_t10 = num;
    }
    if (0 == nblks_t10) { /* NOP in SCSI */
        if (vb > 4)
            pr2ws("%s: nblks_t10 is 0, a NOP in SCSI, can't map to NVMe\n",
                  __func__);
        return 0;
    }
    iop->nblocks = nblks_t10 - 1;
    res = sg_do_nvm_cmd(ptp, iop, 0, false, time_secs, vb);
    if (SG_LIB_NVME_STATUS == res) {
        mk_sense_from_nvme_status(ptp, vb);
        return 0;
    }
    return res;
}

static int
sg_snt_sync_cache(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                  int time_secs, int vb)
{
    bool immed = !!(0x2 & cdbp[1]);
    struct sg_nvme_user_io io;
    struct sg_nvme_user_io * iop = &io;
    int res;

    if (vb > 5)
        pr2ws("%s: immed=%d, time_secs=%d\n", __func__, (int)immed,
              time_secs);
    memset(iop, 0, sizeof(*iop));
    iop->opcode =  SG_NVME_NVM_FLUSH;
    if (vb > 4)
        pr2ws("%s: immed bit, lba and num_lbs fields ignored\n", __func__);
    res = sg_do_nvm_cmd(ptp, iop, 0, false, time_secs, vb);
    if (SG_LIB_NVME_STATUS == res) {
        mk_sense_from_nvme_status(ptp, vb);
        return 0;
    }
    return res;
}

static int
sg_snt_start_stop(struct sg_pt_linux_scsi * ptp, const uint8_t * cdbp,
                  int time_secs, int vb)
{
    bool immed = !!(0x1 & cdbp[1]);

    if (vb > 5)
        pr2ws("%s: immed=%d, time_secs=%d, ignore\n", __func__, (int)immed,
              time_secs);
    if (ptp) { }        /* suppress warning */
    return 0;
}

/* Executes NVMe Admin command (or at least forwards it to lower layers).
 * Returns 0 for success, negative numbers are negated 'errno' values from
 * OS system calls. Positive return values are errors from this package.
 * When time_secs is 0 the Linux NVMe Admin command default of 60 seconds
 * is used. */
int
sg_do_nvme_pt(struct sg_pt_base * vp, int fd, int time_secs, int vb)
{
    bool scsi_cdb;
    bool is_read = false;
    int n, len, hold_dev_fd;
    uint16_t sa;
    struct sg_pt_linux_scsi * ptp = &vp->impl;
    struct sg_nvme_passthru_cmd cmd;
    const uint8_t * cdbp;
    void * dp = NULL;

    if (! ptp->io_hdr.request) {
        if (vb)
            pr2ws("No NVMe command given (set_scsi_pt_cdb())\n");
        return SCSI_PT_DO_BAD_PARAMS;
    }
    hold_dev_fd = ptp->dev_fd;
    if (fd >= 0) {
        if ((ptp->dev_fd >= 0) && (fd != ptp->dev_fd)) {
            if (vb)
                pr2ws("%s: file descriptor given to create() and here "
                      "differ\n", __func__);
            return SCSI_PT_DO_BAD_PARAMS;
        }
        ptp->dev_fd = fd;
    } else if (ptp->dev_fd < 0) {
        if (vb)
            pr2ws("%s: invalid file descriptors\n", __func__);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    n = ptp->io_hdr.request_len;
    cdbp = (const uint8_t *)(sg_uintptr_t)ptp->io_hdr.request;
    if (vb > 4)
        pr2ws("%s: opcode=0x%x, fd=%d (dev_fd=%d), time_secs=%d\n", __func__,
              cdbp[0], fd, hold_dev_fd, time_secs);
    scsi_cdb = sg_is_scsi_cdb(cdbp, n);
    /* direct NVMe command (i.e. 64 bytes long) or SNTL */
    ptp->nvme_our_snt = scsi_cdb;
    if (scsi_cdb) {
        switch (cdbp[0]) {
        case SCSI_INQUIRY_OPC:
            return sg_ln_snt_inq(ptp, cdbp, time_secs, vb);
        case SCSI_REPORT_LUNS_OPC:
            return sg_ln_snt_rluns(ptp, cdbp, time_secs, vb);
        case SCSI_TEST_UNIT_READY_OPC:
            return sg_snt_tur(ptp, time_secs, vb);
        case SCSI_REQUEST_SENSE_OPC:
            return sg_snt_req_sense(ptp, cdbp, time_secs, vb);
        case SCSI_READ10_OPC:
        case SCSI_READ16_OPC:
            return sg_snt_rread(ptp, cdbp, time_secs, vb);
        case SCSI_WRITE10_OPC:
        case SCSI_WRITE16_OPC:
            return sg_snt_write(ptp, cdbp, time_secs, vb);
        case SCSI_START_STOP_OPC:
            return sg_snt_start_stop(ptp, cdbp, time_secs, vb);
        case SCSI_SEND_DIAGNOSTIC_OPC:
            return sg_snt_senddiag(ptp, cdbp, time_secs, vb);
        case SCSI_RECEIVE_DIAGNOSTIC_OPC:
            return sg_snt_recvdiag(ptp, cdbp, time_secs, vb);
        case SCSI_MODE_SENSE10_OPC:
        case SCSI_MODE_SELECT10_OPC:
            return sg_ln_snt_mode_ss(ptp, cdbp, time_secs, vb);
        case SCSI_READ_CAPACITY10_OPC:
            return sg_snt_readcap(ptp, cdbp, time_secs, vb);
        case SCSI_VERIFY10_OPC:
        case SCSI_VERIFY16_OPC:
            return sg_snt_verify(ptp, cdbp, time_secs, vb);
        case SCSI_WRITE_SAME10_OPC:
        case SCSI_WRITE_SAME16_OPC:
            return sg_snt_write_same(ptp, cdbp, time_secs, vb);
        case SCSI_SYNC_CACHE10_OPC:
        case SCSI_SYNC_CACHE16_OPC:
            return sg_snt_sync_cache(ptp, cdbp, time_secs, vb);
        case SCSI_SERVICE_ACT_IN_OPC:
            if (SCSI_READ_CAPACITY16_SA == (cdbp[1] & SCSI_SA_MSK))
                return sg_snt_readcap(ptp, cdbp, time_secs, vb);
            goto fini;
        case SCSI_MAINT_IN_OPC:
            sa = SCSI_SA_MSK & cdbp[1];        /* service action */
            if (SCSI_REP_SUP_OPCS_OPC == sa)
                return sg_ln_snt_rep_opcodes(ptp, cdbp, time_secs, vb);
            else if (SCSI_REP_SUP_TMFS_OPC == sa)
                return sg_ln_snt_rep_tmfs(ptp, cdbp, time_secs, vb);
            /* fall through */
        default:
fini:
            if (vb > 2) {
                char b[64];

                sg_get_command_name(cdbp, -1, sizeof(b), b);
                pr2ws("%s: no translation to NVMe for SCSI %s command\n",
                      __func__, b);
            }
            mk_sense_asc_ascq(ptp, SPC_SK_ILLEGAL_REQUEST, INVALID_OPCODE,
                              0, vb);
            return 0;
        }
    }
    len = (int)sizeof(cmd);
    n = (n < len) ? n : len;
    if (n < 64) {
        if (vb)
            pr2ws("%s: command length of %d bytes is too short\n", __func__,
                  n);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    memcpy(&cmd, (const uint8_t *)(sg_uintptr_t)ptp->io_hdr.request, n);
    if (n < len)        /* zero out rest of 'cmd' */
        memset((uint8_t *)&cmd + n, 0, len - n);
    if (ptp->io_hdr.din_xfer_len > 0) {
        cmd.data_len = ptp->io_hdr.din_xfer_len;
        dp = (void *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
        cmd.addr = (uint64_t)(sg_uintptr_t)ptp->io_hdr.din_xferp;
        is_read = true;
    } else if (ptp->io_hdr.dout_xfer_len > 0) {
        cmd.data_len = ptp->io_hdr.dout_xfer_len;
        dp = (void *)(sg_uintptr_t)ptp->io_hdr.dout_xferp;
        cmd.addr = (uint64_t)(sg_uintptr_t)ptp->io_hdr.dout_xferp;
        is_read = false;
    }
    return sg_nvme_admin_cmd_f(ptp, &cmd, dp, is_read, time_secs, vb);
}

#else           /* (HAVE_NVME && (! IGNORE_NVME)) [around line 140] */

int
sg_do_nvme_pt(struct sg_pt_base * vp, int fd, int time_secs, int vb)
{
    static const int inapprop_errno = ENOTTY;   /* inappropriate ioctl */

    if (vb) {
        pr2ws("%s: not supported, ", __func__);
#ifdef HAVE_NVME
        pr2ws("HAVE_NVME, ");
#else
        pr2ws("don't HAVE_NVME, ");
#endif

#ifdef IGNORE_NVME
        pr2ws("IGNORE_NVME");
#else
        pr2ws("don't IGNORE_NVME");
#endif
        pr2ws("\n");
     }
    if (vp) {
        struct sg_pt_linux_scsi * ptp = &vp->impl;

        ptp->os_err = inapprop_errno;
    }
    if (fd) { ; }               /* suppress warning */
    if (time_secs) { ; }        /* suppress warning */
    return -inapprop_errno;
}

#endif          /* (HAVE_NVME && (! IGNORE_NVME)) */

#if (HAVE_NVME && (! IGNORE_NVME))

int
do_nvm_pt(struct sg_pt_base * vp, int submq, int timeout_secs, int vb)
{
    bool is_read = false;
    int dlen;
    struct sg_pt_linux_scsi * ptp = &vp->impl;
    struct sg_nvme_passthru_cmd cmd;
    uint8_t * cmdp = (uint8_t *)&cmd;
    void * dp = NULL;

    if (vb && (submq != 0))
        pr2ws("%s: warning, uses submit queue 0\n", __func__);
    if (ptp->dev_fd < 0) {
        if (vb > 1)
            pr2ws("%s: no NVMe file descriptor given\n", __func__);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    if (! ptp->is_nvme) {
        if (vb > 1)
            pr2ws("%s: file descriptor is not NVMe device\n", __func__);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    if ((! ptp->io_hdr.request) || (64 != ptp->io_hdr.request_len)) {
        if (vb > 1)
            pr2ws("%s: no NVMe 64 byte command present\n", __func__);
        return SCSI_PT_DO_BAD_PARAMS;
    }
    if (sizeof(cmd) > 64)
        memset(cmdp + 64, 0, sizeof(cmd) - 64);
    memcpy(cmdp, (uint8_t *)(sg_uintptr_t)ptp->io_hdr.request, 64);
    ptp->nvme_our_snt = false;

    dlen = ptp->io_hdr.din_xfer_len;
    if (dlen > 0) {
        is_read = true;
        dp = (void *)(sg_uintptr_t)ptp->io_hdr.din_xferp;
    } else {
        dlen = ptp->io_hdr.dout_xfer_len;
        if (dlen > 0)
            dp = (void *)(sg_uintptr_t)ptp->io_hdr.dout_xferp;
    }
    return do_nvm_pt_low(ptp, &cmd, dp, dlen, is_read, timeout_secs, vb);
}

#else           /* (HAVE_NVME && (! IGNORE_NVME)) */

int
do_nvm_pt(struct sg_pt_base * vp, int submq, int timeout_secs, int vb)
{
    if (vb) {
        pr2ws("%s: not supported, ", __func__);
#ifdef HAVE_NVME
        pr2ws("HAVE_NVME, ");
#else
        pr2ws("don't HAVE_NVME, ");
#endif

#ifdef IGNORE_NVME
        pr2ws("IGNORE_NVME");
#else
        pr2ws("don't IGNORE_NVME");
#endif
    }
    if (vp) { }
    if (submq) { }
    if (timeout_secs) { }
    return SCSI_PT_DO_NOT_SUPPORTED;
}

#endif          /* (HAVE_NVME && (! IGNORE_NVME)) */