summaryrefslogtreecommitdiffstats
path: root/src/spdk/examples/nvme/nvme_manage/nvme_manage.c
blob: c202dab42766198f96e02ab1f43eee942c6e750d (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
/*-
 *   BSD LICENSE
 *
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "spdk/stdinc.h"

#include "spdk/nvme.h"
#include "spdk/env.h"
#include "spdk/string.h"
#include "spdk/util.h"
#include "spdk/opal.h"

#define MAX_DEVS 64

struct dev {
	struct spdk_pci_addr			pci_addr;
	struct spdk_nvme_ctrlr			*ctrlr;
	const struct spdk_nvme_ctrlr_data	*cdata;
	struct spdk_nvme_ns_data		*common_ns_data;
	int					outstanding_admin_cmds;
	struct spdk_opal_dev			*opal_dev;
};

static struct dev devs[MAX_DEVS];
static int num_devs = 0;
static int g_shm_id = -1;

#define foreach_dev(iter) \
	for (iter = devs; iter - devs < num_devs; iter++)

enum controller_display_model {
	CONTROLLER_DISPLAY_ALL			= 0x0,
	CONTROLLER_DISPLAY_SIMPLISTIC		= 0x1,
};

static int
cmp_devs(const void *ap, const void *bp)
{
	const struct dev *a = ap, *b = bp;

	return spdk_pci_addr_compare(&a->pci_addr, &b->pci_addr);
}

static bool
probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
	 struct spdk_nvme_ctrlr_opts *opts)
{
	return true;
}

static void
identify_common_ns_cb(void *cb_arg, const struct spdk_nvme_cpl *cpl)
{
	struct dev *dev = cb_arg;

	if (cpl->status.sc != SPDK_NVME_SC_SUCCESS) {
		/* Identify Namespace for NSID = FFFFFFFFh is optional, so failure is not fatal. */
		spdk_dma_free(dev->common_ns_data);
		dev->common_ns_data = NULL;
	}

	dev->outstanding_admin_cmds--;
}

static void
attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
	  struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
{
	struct dev *dev;
	struct spdk_nvme_cmd cmd;

	/* add to dev list */
	dev = &devs[num_devs++];
	spdk_pci_addr_parse(&dev->pci_addr, trid->traddr);
	dev->ctrlr = ctrlr;

	/* Retrieve controller data */
	dev->cdata = spdk_nvme_ctrlr_get_data(dev->ctrlr);

	dev->common_ns_data = spdk_dma_zmalloc(sizeof(struct spdk_nvme_ns_data), 4096, NULL);
	if (dev->common_ns_data == NULL) {
		fprintf(stderr, "common_ns_data allocation failure\n");
		return;
	}

	/* Identify Namespace with NSID set to FFFFFFFFh to get common namespace capabilities. */
	memset(&cmd, 0, sizeof(cmd));
	cmd.opc = SPDK_NVME_OPC_IDENTIFY;
	cmd.cdw10_bits.identify.cns = 0; /* CNS = 0 (Identify Namespace) */
	cmd.nsid = SPDK_NVME_GLOBAL_NS_TAG;

	dev->outstanding_admin_cmds++;
	if (spdk_nvme_ctrlr_cmd_admin_raw(ctrlr, &cmd, dev->common_ns_data,
					  sizeof(struct spdk_nvme_ns_data), identify_common_ns_cb, dev) != 0) {
		dev->outstanding_admin_cmds--;
		spdk_dma_free(dev->common_ns_data);
		dev->common_ns_data = NULL;
	}

	while (dev->outstanding_admin_cmds) {
		spdk_nvme_ctrlr_process_admin_completions(ctrlr);
	}
}

static void usage(void)
{
	printf("NVMe Management Options");
	printf("\n");
	printf("\t[1: list controllers]\n");
	printf("\t[2: create namespace]\n");
	printf("\t[3: delete namespace]\n");
	printf("\t[4: attach namespace to controller]\n");
	printf("\t[5: detach namespace from controller]\n");
	printf("\t[6: format namespace or controller]\n");
	printf("\t[7: firmware update]\n");
	printf("\t[8: opal]\n");
	printf("\t[9: quit]\n");
}

static void
display_namespace_dpc(const struct spdk_nvme_ns_data *nsdata)
{
	if (nsdata->dpc.pit1 || nsdata->dpc.pit2 || nsdata->dpc.pit3) {
		if (nsdata->dpc.pit1) {
			printf("PIT1 ");
		}

		if (nsdata->dpc.pit2) {
			printf("PIT2 ");
		}

		if (nsdata->dpc.pit3) {
			printf("PIT3 ");
		}
	} else {
		printf("Not Supported\n");
		return;
	}

	if (nsdata->dpc.md_start && nsdata->dpc.md_end) {
		printf("Location: Head or Tail\n");
	} else if (nsdata->dpc.md_start) {
		printf("Location: Head\n");
	} else if (nsdata->dpc.md_end) {
		printf("Location: Tail\n");
	} else {
		printf("Not Supported\n");
	}
}

static void
display_namespace(struct spdk_nvme_ns *ns)
{
	const struct spdk_nvme_ns_data		*nsdata;
	uint32_t				i;

	nsdata = spdk_nvme_ns_get_data(ns);

	printf("Namespace ID:%d\n", spdk_nvme_ns_get_id(ns));

	printf("Size (in LBAs):              %lld (%lldM)\n",
	       (long long)nsdata->nsze,
	       (long long)nsdata->nsze / 1024 / 1024);
	printf("Capacity (in LBAs):          %lld (%lldM)\n",
	       (long long)nsdata->ncap,
	       (long long)nsdata->ncap / 1024 / 1024);
	printf("Utilization (in LBAs):       %lld (%lldM)\n",
	       (long long)nsdata->nuse,
	       (long long)nsdata->nuse / 1024 / 1024);
	printf("Format Progress Indicator:   %s\n",
	       nsdata->fpi.fpi_supported ? "Supported" : "Not Supported");
	if (nsdata->fpi.fpi_supported && nsdata->fpi.percentage_remaining) {
		printf("Formatted Percentage:	%d%%\n", 100 - nsdata->fpi.percentage_remaining);
	}
	printf("Number of LBA Formats:       %d\n", nsdata->nlbaf + 1);
	printf("Current LBA Format:          LBA Format #%02d\n",
	       nsdata->flbas.format);
	for (i = 0; i <= nsdata->nlbaf; i++)
		printf("LBA Format #%02d: Data Size: %5d  Metadata Size: %5d\n",
		       i, 1 << nsdata->lbaf[i].lbads, nsdata->lbaf[i].ms);
	printf("Data Protection Capabilities:");
	display_namespace_dpc(nsdata);
	if (SPDK_NVME_FMT_NVM_PROTECTION_DISABLE == nsdata->dps.pit) {
		printf("Data Protection Setting:     N/A\n");
	} else {
		printf("Data Protection Setting:     PIT%d Location: %s\n",
		       nsdata->dps.pit, nsdata->dps.md_start ? "Head" : "Tail");
	}
	printf("Multipath IO and Sharing:    %s\n",
	       nsdata->nmic.can_share ? "Supported" : "Not Supported");
	printf("\n");
}

static void
display_controller(struct dev *dev, int model)
{
	struct spdk_nvme_ns			*ns;
	const struct spdk_nvme_ctrlr_data	*cdata;
	uint8_t					str[128];
	uint32_t				nsid;

	cdata = spdk_nvme_ctrlr_get_data(dev->ctrlr);

	if (model == CONTROLLER_DISPLAY_SIMPLISTIC) {
		printf("%04x:%02x:%02x.%02x ",
		       dev->pci_addr.domain, dev->pci_addr.bus, dev->pci_addr.dev, dev->pci_addr.func);
		printf("%-40.40s %-20.20s ",
		       cdata->mn, cdata->sn);
		printf("%5d ", cdata->cntlid);
		printf("\n");
		return;
	}

	printf("=====================================================\n");
	printf("NVMe Controller:	%04x:%02x:%02x.%02x\n",
	       dev->pci_addr.domain, dev->pci_addr.bus, dev->pci_addr.dev, dev->pci_addr.func);
	printf("============================\n");
	printf("Controller Capabilities/Features\n");
	printf("Controller ID:		%d\n", cdata->cntlid);
	snprintf(str, sizeof(cdata->sn) + 1, "%s", cdata->sn);
	printf("Serial Number:		%s\n", str);
	printf("\n");

	printf("Admin Command Set Attributes\n");
	printf("============================\n");
	printf("Namespace Manage And Attach:		%s\n",
	       cdata->oacs.ns_manage ? "Supported" : "Not Supported");
	printf("Namespace Format:			%s\n",
	       cdata->oacs.format ? "Supported" : "Not Supported");
	printf("\n");
	printf("NVM Command Set Attributes\n");
	printf("============================\n");
	if (cdata->fna.format_all_ns) {
		printf("Namespace format operation applies to all namespaces\n");
	} else {
		printf("Namespace format operation applies to per namespace\n");
	}
	printf("\n");
	printf("Namespace Attributes\n");
	printf("============================\n");
	for (nsid = spdk_nvme_ctrlr_get_first_active_ns(dev->ctrlr);
	     nsid != 0; nsid = spdk_nvme_ctrlr_get_next_active_ns(dev->ctrlr, nsid)) {
		ns = spdk_nvme_ctrlr_get_ns(dev->ctrlr, nsid);
		assert(ns != NULL);
		display_namespace(ns);
	}
}

static void
display_controller_list(void)
{
	struct dev			*iter;

	foreach_dev(iter) {
		display_controller(iter, CONTROLLER_DISPLAY_ALL);
	}
}

static char *
get_line(char *buf, int buf_size, FILE *f, bool secret)
{
	char *ch;
	size_t len;
	struct termios default_attr = {}, new_attr = {};
	int ret;

	if (secret) {
		ret = tcgetattr(STDIN_FILENO, &default_attr);
		if (ret) {
			return NULL;
		}

		new_attr = default_attr;
		new_attr.c_lflag &= ~ECHO;  /* disable echo */
		ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_attr);
		if (ret) {
			return NULL;
		}
	}

	ch = fgets(buf, buf_size, f);
	if (ch == NULL) {
		return NULL;
	}

	if (secret) {
		ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, &default_attr); /* restore default confing */
		if (ret) {
			return NULL;
		}
	}

	len = strlen(buf);
	if (len > 0 && buf[len - 1] == '\n') {
		buf[len - 1] = '\0';
	}
	return buf;
}

static struct dev *
get_controller(void)
{
	struct spdk_pci_addr			pci_addr;
	char					address[64];
	char					*p;
	int					ch;
	struct dev				*iter;

	memset(address, 0, sizeof(address));

	foreach_dev(iter) {
		display_controller(iter, CONTROLLER_DISPLAY_SIMPLISTIC);
	}

	printf("Please Input PCI Address(domain:bus:dev.func):\n");

	while ((ch = getchar()) != '\n' && ch != EOF);
	p = get_line(address, 64, stdin, false);
	if (p == NULL) {
		return NULL;
	}

	while (isspace(*p)) {
		p++;
	}

	if (spdk_pci_addr_parse(&pci_addr, p) < 0) {
		return NULL;
	}

	foreach_dev(iter) {
		if (spdk_pci_addr_compare(&pci_addr, &iter->pci_addr) == 0) {
			return iter;
		}
	}
	return NULL;
}

static int
get_lba_format(const struct spdk_nvme_ns_data *ns_data)
{
	int lbaf, i;

	printf("\nSupported LBA formats:\n");
	for (i = 0; i <= ns_data->nlbaf; i++) {
		printf("%2d: %d data bytes", i, 1 << ns_data->lbaf[i].lbads);
		if (ns_data->lbaf[i].ms) {
			printf(" + %d metadata bytes", ns_data->lbaf[i].ms);
		}
		printf("\n");
	}

	printf("Please input LBA format index (0 - %d):\n", ns_data->nlbaf);
	if (scanf("%d", &lbaf) != 1 || lbaf > ns_data->nlbaf) {
		return -1;
	}

	return lbaf;
}

static void
identify_allocated_ns_cb(void *cb_arg, const struct spdk_nvme_cpl *cpl)
{
	struct dev *dev = cb_arg;

	dev->outstanding_admin_cmds--;
}

static uint32_t
get_allocated_nsid(struct dev *dev)
{
	uint32_t nsid;
	size_t i;
	struct spdk_nvme_ns_list *ns_list;
	struct spdk_nvme_cmd cmd = {0};

	ns_list = spdk_dma_zmalloc(sizeof(*ns_list), 4096, NULL);
	if (ns_list == NULL) {
		printf("Allocation error\n");
		return 0;
	}

	cmd.opc = SPDK_NVME_OPC_IDENTIFY;
	cmd.cdw10_bits.identify.cns = SPDK_NVME_IDENTIFY_ALLOCATED_NS_LIST;
	cmd.nsid = 0;

	dev->outstanding_admin_cmds++;
	if (spdk_nvme_ctrlr_cmd_admin_raw(dev->ctrlr, &cmd, ns_list, sizeof(*ns_list),
					  identify_allocated_ns_cb, dev)) {
		printf("Identify command failed\n");
		spdk_dma_free(ns_list);
		return 0;
	}

	while (dev->outstanding_admin_cmds) {
		spdk_nvme_ctrlr_process_admin_completions(dev->ctrlr);
	}

	printf("Allocated Namespace IDs:\n");
	for (i = 0; i < SPDK_COUNTOF(ns_list->ns_list); i++) {
		if (ns_list->ns_list[i] == 0) {
			break;
		}
		printf("%u\n", ns_list->ns_list[i]);
	}

	spdk_dma_free(ns_list);

	printf("Please Input Namespace ID:\n");
	if (!scanf("%u", &nsid)) {
		printf("Invalid Namespace ID\n");
		nsid = 0;
	}

	return nsid;
}

static void
ns_attach(struct dev *device, int attachment_op, int ctrlr_id, int ns_id)
{
	int ret = 0;
	struct spdk_nvme_ctrlr_list *ctrlr_list;

	ctrlr_list = spdk_dma_zmalloc(sizeof(struct spdk_nvme_ctrlr_list),
				      4096, NULL);
	if (ctrlr_list == NULL) {
		printf("Allocation error (controller list)\n");
		exit(1);
	}

	ctrlr_list->ctrlr_count = 1;
	ctrlr_list->ctrlr_list[0] = ctrlr_id;

	if (attachment_op == SPDK_NVME_NS_CTRLR_ATTACH) {
		ret = spdk_nvme_ctrlr_attach_ns(device->ctrlr, ns_id, ctrlr_list);
	} else if (attachment_op == SPDK_NVME_NS_CTRLR_DETACH) {
		ret = spdk_nvme_ctrlr_detach_ns(device->ctrlr, ns_id, ctrlr_list);
	}

	if (ret) {
		fprintf(stdout, "ns attach: Failed\n");
	}

	spdk_dma_free(ctrlr_list);
}

static void
ns_manage_add(struct dev *device, uint64_t ns_size, uint64_t ns_capacity, int ns_lbasize,
	      uint8_t ns_dps_type, uint8_t ns_dps_location, uint8_t ns_nmic)
{
	uint32_t nsid;
	struct spdk_nvme_ns_data *ndata;

	ndata = spdk_dma_zmalloc(sizeof(struct spdk_nvme_ns_data), 4096, NULL);
	if (ndata == NULL) {
		printf("Allocation error (namespace data)\n");
		exit(1);
	}

	ndata->nsze = ns_size;
	ndata->ncap = ns_capacity;
	ndata->flbas.format = ns_lbasize;
	if (SPDK_NVME_FMT_NVM_PROTECTION_DISABLE != ns_dps_type) {
		ndata->dps.pit = ns_dps_type;
		ndata->dps.md_start = ns_dps_location;
	}
	ndata->nmic.can_share = ns_nmic;
	nsid = spdk_nvme_ctrlr_create_ns(device->ctrlr, ndata);
	if (nsid == 0) {
		fprintf(stdout, "ns manage: Failed\n");
	} else {
		printf("Created namespace ID %u\n", nsid);
	}

	spdk_dma_free(ndata);
}

static void
ns_manage_delete(struct dev *device, int ns_id)
{
	int ret = 0;

	ret = spdk_nvme_ctrlr_delete_ns(device->ctrlr, ns_id);
	if (ret) {
		fprintf(stdout, "ns manage: Failed\n");
		return;
	}
}

static void
nvme_manage_format(struct dev *device, int ns_id, int ses, int pi, int pil, int ms, int lbaf)
{
	int ret = 0;
	struct spdk_nvme_format format = {};

	format.lbaf	= lbaf;
	format.ms	= ms;
	format.pi	= pi;
	format.pil	= pil;
	format.ses	= ses;
	ret = spdk_nvme_ctrlr_format(device->ctrlr, ns_id, &format);
	if (ret) {
		fprintf(stdout, "nvme format: Failed\n");
		return;
	}
}

static void
attach_and_detach_ns(int attachment_op)
{
	uint32_t	nsid;
	struct dev	*ctrlr;

	ctrlr = get_controller();
	if (ctrlr == NULL) {
		printf("Invalid controller PCI Address.\n");
		return;
	}

	if (!ctrlr->cdata->oacs.ns_manage) {
		printf("Controller does not support ns management\n");
		return;
	}

	nsid = get_allocated_nsid(ctrlr);
	if (nsid == 0) {
		printf("Invalid Namespace ID\n");
		return;
	}

	ns_attach(ctrlr, attachment_op, ctrlr->cdata->cntlid, nsid);
}

static void
add_ns(void)
{
	uint64_t	ns_size		= 0;
	uint64_t	ns_capacity	= 0;
	int		ns_lbasize;
	int		ns_dps_type	= 0;
	int		ns_dps_location	= 0;
	int		ns_nmic		= 0;
	struct dev	*ctrlr		= NULL;

	ctrlr = get_controller();
	if (ctrlr == NULL) {
		printf("Invalid controller PCI Address.\n");
		return;
	}

	if (!ctrlr->cdata->oacs.ns_manage) {
		printf("Controller does not support ns management\n");
		return;
	}

	if (!ctrlr->common_ns_data) {
		printf("Controller did not return common namespace capabilities\n");
		return;
	}

	ns_lbasize = get_lba_format(ctrlr->common_ns_data);
	if (ns_lbasize < 0) {
		printf("Invalid LBA format number\n");
		return;
	}

	printf("Please Input Namespace Size (in LBAs):\n");
	if (!scanf("%" SCNu64, &ns_size)) {
		printf("Invalid Namespace Size\n");
		while (getchar() != '\n');
		return;
	}

	printf("Please Input Namespace Capacity (in LBAs):\n");
	if (!scanf("%" SCNu64, &ns_capacity)) {
		printf("Invalid Namespace Capacity\n");
		while (getchar() != '\n');
		return;
	}

	printf("Please Input Data Protection Type (0 - 3):\n");
	if (!scanf("%d", &ns_dps_type)) {
		printf("Invalid Data Protection Type\n");
		while (getchar() != '\n');
		return;
	}

	if (SPDK_NVME_FMT_NVM_PROTECTION_DISABLE != ns_dps_type) {
		printf("Please Input Data Protection Location (1: Head; 0: Tail):\n");
		if (!scanf("%d", &ns_dps_location)) {
			printf("Invalid Data Protection Location\n");
			while (getchar() != '\n');
			return;
		}
	}

	printf("Please Input Multi-path IO and Sharing Capabilities (1: Share; 0: Private):\n");
	if (!scanf("%d", &ns_nmic)) {
		printf("Invalid Multi-path IO and Sharing Capabilities\n");
		while (getchar() != '\n');
		return;
	}

	ns_manage_add(ctrlr, ns_size, ns_capacity, ns_lbasize,
		      ns_dps_type, ns_dps_location, ns_nmic);
}

static void
delete_ns(void)
{
	int					ns_id;
	struct dev				*ctrlr;

	ctrlr = get_controller();
	if (ctrlr == NULL) {
		printf("Invalid controller PCI Address.\n");
		return;
	}

	if (!ctrlr->cdata->oacs.ns_manage) {
		printf("Controller does not support ns management\n");
		return;
	}

	printf("Please Input Namespace ID:\n");
	if (!scanf("%d", &ns_id)) {
		printf("Invalid Namespace ID\n");
		while (getchar() != '\n');
		return;
	}

	ns_manage_delete(ctrlr, ns_id);
}

static void
format_nvm(void)
{
	int					ns_id;
	int					ses;
	int					pil;
	int					pi;
	int					ms;
	int					lbaf;
	char					option;
	struct dev				*ctrlr;
	const struct spdk_nvme_ctrlr_data	*cdata;
	struct spdk_nvme_ns			*ns;
	const struct spdk_nvme_ns_data		*nsdata;

	ctrlr = get_controller();
	if (ctrlr == NULL) {
		printf("Invalid controller PCI BDF.\n");
		return;
	}

	cdata = ctrlr->cdata;

	if (!cdata->oacs.format) {
		printf("Controller does not support Format NVM command\n");
		return;
	}

	if (cdata->fna.format_all_ns) {
		ns_id = SPDK_NVME_GLOBAL_NS_TAG;
		ns = spdk_nvme_ctrlr_get_ns(ctrlr->ctrlr, 1);
	} else {
		printf("Please Input Namespace ID (1 - %d):\n", cdata->nn);
		if (!scanf("%d", &ns_id)) {
			printf("Invalid Namespace ID\n");
			while (getchar() != '\n');
			return;
		}
		ns = spdk_nvme_ctrlr_get_ns(ctrlr->ctrlr, ns_id);
	}

	if (ns == NULL) {
		printf("Namespace ID %d not found\n", ns_id);
		while (getchar() != '\n');
		return;
	}

	nsdata = spdk_nvme_ns_get_data(ns);

	printf("Please Input Secure Erase Setting:\n");
	printf("	0: No secure erase operation requested\n");
	printf("	1: User data erase\n");
	if (cdata->fna.crypto_erase_supported) {
		printf("	2: Cryptographic erase\n");
	}
	if (!scanf("%d", &ses)) {
		printf("Invalid Secure Erase Setting\n");
		while (getchar() != '\n');
		return;
	}

	lbaf = get_lba_format(nsdata);
	if (lbaf < 0) {
		printf("Invalid LBA format number\n");
		return;
	}

	if (nsdata->lbaf[lbaf].ms) {
		printf("Please Input Protection Information:\n");
		printf("	0: Protection information is not enabled\n");
		printf("	1: Protection information is enabled, Type 1\n");
		printf("	2: Protection information is enabled, Type 2\n");
		printf("	3: Protection information is enabled, Type 3\n");
		if (!scanf("%d", &pi)) {
			printf("Invalid protection information\n");
			while (getchar() != '\n');
			return;
		}

		if (pi) {
			printf("Please Input Protection Information Location:\n");
			printf("	0: Protection information transferred as the last eight bytes of metadata\n");
			printf("	1: Protection information transferred as the first eight bytes of metadata\n");
			if (!scanf("%d", &pil)) {
				printf("Invalid protection information location\n");
				while (getchar() != '\n');
				return;
			}
		} else {
			pil = 0;
		}

		printf("Please Input Metadata Setting:\n");
		printf("	0: Metadata is transferred as part of a separate buffer\n");
		printf("	1: Metadata is transferred as part of an extended data LBA\n");
		if (!scanf("%d", &ms)) {
			printf("Invalid metadata setting\n");
			while (getchar() != '\n');
			return;
		}
	} else {
		ms = 0;
		pi = 0;
		pil = 0;
	}

	printf("Warning: use this utility at your own risk.\n"
	       "This command will format your namespace and all data will be lost.\n"
	       "This command may take several minutes to complete,\n"
	       "so do not interrupt the utility until it completes.\n"
	       "Press 'Y' to continue with the format operation.\n");

	while (getchar() != '\n');
	if (!scanf("%c", &option)) {
		printf("Invalid option\n");
		while (getchar() != '\n');
		return;
	}

	if (option == 'y' || option == 'Y') {
		nvme_manage_format(ctrlr, ns_id, ses, pi, pil, ms, lbaf);
	} else {
		printf("NVMe format abort\n");
	}
}

static void
update_firmware_image(void)
{
	int					rc;
	int					fd = -1;
	int					slot;
	unsigned int				size;
	struct stat				fw_stat;
	char					path[256];
	void					*fw_image;
	struct dev				*ctrlr;
	const struct spdk_nvme_ctrlr_data	*cdata;
	enum spdk_nvme_fw_commit_action		commit_action;
	struct spdk_nvme_status			status;

	ctrlr = get_controller();
	if (ctrlr == NULL) {
		printf("Invalid controller PCI BDF.\n");
		return;
	}

	cdata = ctrlr->cdata;

	if (!cdata->oacs.firmware) {
		printf("Controller does not support firmware download and commit command\n");
		return;
	}

	printf("Please Input The Path Of Firmware Image\n");

	if (get_line(path, sizeof(path), stdin, false) == NULL) {
		printf("Invalid path setting\n");
		while (getchar() != '\n');
		return;
	}

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		perror("Open file failed");
		return;
	}
	rc = fstat(fd, &fw_stat);
	if (rc < 0) {
		printf("Fstat failed\n");
		close(fd);
		return;
	}

	if (fw_stat.st_size % 4) {
		printf("Firmware image size is not multiple of 4\n");
		close(fd);
		return;
	}

	size = fw_stat.st_size;

	fw_image = spdk_dma_zmalloc(size, 4096, NULL);
	if (fw_image == NULL) {
		printf("Allocation error\n");
		close(fd);
		return;
	}

	if (read(fd, fw_image, size) != ((ssize_t)(size))) {
		printf("Read firmware image failed\n");
		close(fd);
		spdk_dma_free(fw_image);
		return;
	}
	close(fd);

	printf("Please Input Slot(0 - 7):\n");
	if (!scanf("%d", &slot)) {
		printf("Invalid Slot\n");
		spdk_dma_free(fw_image);
		while (getchar() != '\n');
		return;
	}

	commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG;
	rc = spdk_nvme_ctrlr_update_firmware(ctrlr->ctrlr, fw_image, size, slot, commit_action, &status);
	if (rc == -ENXIO && status.sct == SPDK_NVME_SCT_COMMAND_SPECIFIC &&
	    status.sc == SPDK_NVME_SC_FIRMWARE_REQ_CONVENTIONAL_RESET) {
		printf("conventional reset is needed to enable firmware !\n");
	} else if (rc) {
		printf("spdk_nvme_ctrlr_update_firmware failed\n");
	} else {
		printf("spdk_nvme_ctrlr_update_firmware success\n");
	}
	spdk_dma_free(fw_image);
}

static void
opal_dump_info(struct spdk_opal_d0_features_info *feat)
{
	if (feat->tper.hdr.code) {
		printf("\nOpal TPer feature:\n");
		printf("ACKNACK = %s", (feat->tper.acknack ? "Y, " : "N, "));
		printf("ASYNC = %s", (feat->tper.async ? "Y, " : "N, "));
		printf("BufferManagement = %s\n", (feat->tper.buffer_management ? "Y, " : "N, "));
		printf("ComIDManagement = %s", (feat->tper.comid_management ? "Y, " : "N, "));
		printf("Streaming = %s", (feat->tper.streaming ? "Y, " : "N, "));
		printf("Sync = %s\n", (feat->tper.sync ? "Y" : "N"));
		printf("\n");
	}

	if (feat->locking.hdr.code) {
		printf("Opal Locking feature:\n");
		printf("Locked = %s", (feat->locking.locked ? "Y, " : "N, "));
		printf("Locking Enabled = %s", (feat->locking.locking_enabled ? "Y, " : "N, "));
		printf("Locking supported = %s\n", (feat->locking.locking_supported ? "Y" : "N"));

		printf("MBR done = %s", (feat->locking.mbr_done ? "Y, " : "N, "));
		printf("MBR enabled = %s", (feat->locking.mbr_enabled ? "Y, " : "N, "));
		printf("Media encrypt = %s\n", (feat->locking.media_encryption ? "Y" : "N"));
		printf("\n");
	}

	if (feat->geo.hdr.code) {
		printf("Opal Geometry feature:\n");
		printf("Align = %s", (feat->geo.alignment_granularity ? "Y, " : "N, "));
		printf("Logical block size = %d, ", from_be32(&feat->geo.logical_block_size));
		printf("Lowest aligned LBA = %ld\n", from_be64(&feat->geo.lowest_aligned_lba));
		printf("\n");
	}

	if (feat->single_user.hdr.code) {
		printf("Opal Single User Mode feature:\n");
		printf("Any in SUM = %s", (feat->single_user.any ? "Y, " : "N, "));
		printf("All in SUM = %s", (feat->single_user.all ? "Y, " : "N, "));
		printf("Policy: %s Authority,\n", (feat->single_user.policy ? "Admin" : "Users"));
		printf("Number of locking objects = %d\n ", from_be32(&feat->single_user.num_locking_objects));
		printf("\n");
	}

	if (feat->datastore.hdr.code) {
		printf("Opal DataStore feature:\n");
		printf("Table alignment = %d, ", from_be32(&feat->datastore.alignment));
		printf("Max number of tables = %d, ", from_be16(&feat->datastore.max_tables));
		printf("Max size of tables = %d\n", from_be32(&feat->datastore.max_table_size));
		printf("\n");
	}

	if (feat->v100.hdr.code) {
		printf("Opal V100 feature:\n");
		printf("Base comID = %d, ", from_be16(&feat->v100.base_comid));
		printf("Number of comIDs = %d, ", from_be16(&feat->v100.number_comids));
		printf("Range crossing = %s\n", (feat->v100.range_crossing ? "N" : "Y"));
		printf("\n");
	}

	if (feat->v200.hdr.code) {
		printf("Opal V200 feature:\n");
		printf("Base comID = %d, ", from_be16(&feat->v200.base_comid));
		printf("Number of comIDs = %d, ", from_be16(&feat->v200.num_comids));
		printf("Initial PIN = %d,\n", feat->v200.initial_pin);
		printf("Reverted PIN = %d, ", feat->v200.reverted_pin);
		printf("Number of admins = %d, ", from_be16(&feat->v200.num_locking_admin_auth));
		printf("Number of users = %d\n", from_be16(&feat->v200.num_locking_user_auth));
		printf("\n");
	}
}

static void
opal_usage(void)
{
	printf("Opal General Usage:\n");
	printf("\n");
	printf("\t[1: scan device]\n");
	printf("\t[2: init - take ownership and activate locking]\n");
	printf("\t[3: revert tper]\n");
	printf("\t[4: setup locking range]\n");
	printf("\t[5: list locking ranges]\n");
	printf("\t[6: enable user]\n");
	printf("\t[7: set new password]\n");
	printf("\t[8: add user to locking range]\n");
	printf("\t[9: lock/unlock range]\n");
	printf("\t[10: erase locking range]\n");
	printf("\t[0: quit]\n");
}

static void
opal_scan(struct dev *iter)
{
	while (getchar() != '\n');
	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}

		printf("\n\nOpal Supported:\n");
		display_controller(iter, CONTROLLER_DISPLAY_SIMPLISTIC);
		opal_dump_info(spdk_opal_get_d0_features_info(iter->opal_dev));
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
		printf("%04x:%02x:%02x.%02x: Opal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_init(struct dev *iter)
{
	char new_passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ret;
	int ch;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please input the new password for ownership:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(new_passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n...\n");
		if (passwd_p) {
			ret = spdk_opal_cmd_take_ownership(iter->opal_dev, passwd_p);
			if (ret) {
				printf("Take ownership failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			ret = spdk_opal_cmd_activate_locking_sp(iter->opal_dev, passwd_p);
			if (ret) {
				printf("Locking SP activate failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			printf("...\nOpal Init Success\n");
		} else {
			printf("Input password invalid. Opal Init failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_locking_usage(void)
{
	printf("Choose Opal locking state:\n");
	printf("\n");
	printf("\t[1: read write lock]\n");
	printf("\t[2: read only]\n");
	printf("\t[3: read write unlock]\n");
}

static void
opal_setup_lockingrange(struct dev *iter)
{
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ret;
	int ch;
	uint64_t range_start;
	uint64_t range_length;
	int locking_range_id;
	struct spdk_opal_locking_range_info *info;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please input the password for setting up locking range:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n");
		if (passwd_p) {
			printf("Specify locking range id:\n");
			if (!scanf("%d", &locking_range_id)) {
				printf("Invalid locking range id\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			printf("range length:\n");
			if (!scanf("%" SCNu64, &range_length)) {
				printf("Invalid range length\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			printf("range start:\n");
			if (!scanf("%" SCNu64, &range_start)) {
				printf("Invalid range start address\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			while (getchar() != '\n');

			ret = spdk_opal_cmd_setup_locking_range(iter->opal_dev,
								OPAL_ADMIN1, locking_range_id, range_start, range_length, passwd_p);
			if (ret) {
				printf("Setup locking range failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			ret = spdk_opal_cmd_get_locking_range_info(iter->opal_dev,
					passwd_p, OPAL_ADMIN1, locking_range_id);
			if (ret) {
				printf("Get locking range info failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			info = spdk_opal_get_locking_range_info(iter->opal_dev, locking_range_id);

			printf("\nlocking range ID: %d\n", info->locking_range_id);
			printf("range start: %ld\n", info->range_start);
			printf("range length: %ld\n", info->range_length);
			printf("read lock enabled: %d\n", info->read_lock_enabled);
			printf("write lock enabled: %d\n", info->write_lock_enabled);
			printf("read locked: %d\n", info->read_locked);
			printf("write locked: %d\n", info->write_locked);

			printf("...\n...\nOpal setup locking range success\n");
		} else {
			printf("Input password invalid. Opal setup locking range failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_list_locking_ranges(struct dev *iter)
{
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ret;
	int ch;
	int max_ranges;
	int i;
	struct spdk_opal_locking_range_info *info;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please input password:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n");
		if (passwd_p) {
			ret = spdk_opal_cmd_get_max_ranges(iter->opal_dev, passwd_p);
			if (ret <= 0) {
				printf("get max ranges failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			max_ranges = ret;
			for (i = 0; i < max_ranges; i++) {
				ret = spdk_opal_cmd_get_locking_range_info(iter->opal_dev,
						passwd_p, OPAL_ADMIN1, i);
				if (ret) {
					printf("Get locking range info failure: %d\n", ret);
					spdk_opal_dev_destruct(iter->opal_dev);
					return;
				}
				info = spdk_opal_get_locking_range_info(iter->opal_dev, i);
				if (info == NULL) {
					continue;
				}

				printf("===============================================\n");
				printf("locking range ID: %d\t", info->locking_range_id);
				if (i == 0) { printf("(Global Range)"); }
				printf("\n===============================================\n");
				printf("range start: %ld\t", info->range_start);
				printf("range length: %ld\n", info->range_length);
				printf("read lock enabled: %d\t", info->read_lock_enabled);
				printf("write lock enabled: %d\t", info->write_lock_enabled);
				printf("read locked: %d\t", info->read_locked);
				printf("write locked: %d\n", info->write_locked);
				printf("\n");
			}
		} else {
			printf("Input password invalid. List locking ranges failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_new_user_enable(struct dev *iter)
{
	int user_id;
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	char user_pw[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *user_pw_p;
	int ret;
	int ch;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please input admin password:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n");
		if (passwd_p) {
			printf("which user to enable: ");
			if (!scanf("%d", &user_id)) {
				printf("Invalid user id\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			ret = spdk_opal_cmd_enable_user(iter->opal_dev, user_id, passwd_p);
			if (ret) {
				printf("Enable user failure error code: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			printf("Please set a new password for this user:");
			while ((ch = getchar()) != '\n' && ch != EOF);
			user_pw_p = get_line(user_pw, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
			if (user_pw_p == NULL) {
				printf("Input password invalid. Enable user failure\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			ret = spdk_opal_cmd_set_new_passwd(iter->opal_dev, user_id, user_pw_p, passwd_p, true);
			if (ret) {
				printf("Set new password failure error code: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			printf("\n...\n...\nEnable User Success\n");
		} else {
			printf("Input password invalid. Enable user failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_change_password(struct dev *iter)
{
	int user_id;
	char old_passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *old_passwd_p;
	char new_passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *new_passwd_p;
	int ret;
	int ch;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("user id: ");
		if (!scanf("%d", &user_id)) {
			printf("Invalid user id\n");
			spdk_opal_dev_destruct(iter->opal_dev);
			return;
		}
		printf("Password:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		old_passwd_p = get_line(old_passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n");
		if (old_passwd_p) {
			printf("Please input new password:\n");
			new_passwd_p = get_line(new_passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
			printf("\n");
			if (new_passwd_p == NULL) {
				printf("Input password invalid. Change password failure\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			ret = spdk_opal_cmd_set_new_passwd(iter->opal_dev, user_id, new_passwd_p, old_passwd_p, false);
			if (ret) {
				printf("Set new password failure error code: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			printf("...\n...\nChange password Success\n");
		} else {
			printf("Input password invalid. Change password failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_add_user_to_locking_range(struct dev *iter)
{
	int locking_range_id, user_id;
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ret;
	int ch;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please input admin password:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n");
		if (passwd_p) {
			printf("Specify locking range id:\n");
			if (!scanf("%d", &locking_range_id)) {
				printf("Invalid locking range id\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			printf("which user to enable:\n");
			if (!scanf("%d", &user_id)) {
				printf("Invalid user id\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			while (getchar() != '\n');

			ret = spdk_opal_cmd_add_user_to_locking_range(iter->opal_dev, user_id, locking_range_id,
					OPAL_READONLY, passwd_p);
			ret += spdk_opal_cmd_add_user_to_locking_range(iter->opal_dev, user_id, locking_range_id,
					OPAL_READWRITE, passwd_p);
			if (ret) {
				printf("Add user to locking range error: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			printf("...\n...\nAdd user to locking range Success\n");
		} else {
			printf("Input password invalid. Add user to locking range failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_user_lock_unlock_range(struct dev *iter)
{
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ch;
	int ret;
	int user_id;
	int locking_range_id;
	int state;
	enum spdk_opal_lock_state state_flag;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("User id: ");
		if (!scanf("%d", &user_id)) {
			printf("Invalid user id\n");
			spdk_opal_dev_destruct(iter->opal_dev);
			return;
		}

		printf("Please input password:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n");
		if (passwd_p) {
			printf("Specify locking range id:\n");
			if (!scanf("%d", &locking_range_id)) {
				printf("Invalid locking range id\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}

			opal_locking_usage();
			if (!scanf("%d", &state)) {
				printf("Invalid option\n");
			}
			switch (state) {
			case 1:
				state_flag = OPAL_RWLOCK;
				break;
			case 2:
				state_flag = OPAL_READONLY;
				break;
			case 3:
				state_flag = OPAL_READWRITE;
				break;
			default:
				printf("Invalid options\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			while (getchar() != '\n');

			ret = spdk_opal_cmd_lock_unlock(iter->opal_dev, user_id, state_flag,
							locking_range_id, passwd_p);
			if (ret) {
				printf("lock/unlock range failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			printf("...\n...\nLock/unlock range Success\n");
		} else {
			printf("Input password invalid. lock/unlock range failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_revert_tper(struct dev *iter)
{
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ret;
	int ch;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please be noted this operation will erase ALL DATA on this drive\n");
		printf("Please don't ternminate this excecution. Otherwise undefined error may occur\n");
		printf("Please input password for revert TPer:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		printf("\n...\n");
		if (passwd_p) {
			ret = spdk_opal_cmd_revert_tper(iter->opal_dev, passwd_p);
			if (ret) {
				printf("Revert TPer failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			printf("...\nRevert TPer Success\n");
		} else {
			printf("Input password invalid. Revert TPer failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
opal_erase_locking_range(struct dev *iter)
{
	char passwd[SPDK_OPAL_MAX_PASSWORD_SIZE] = {0};
	char *passwd_p;
	int ret;
	int ch;
	int locking_range_id;

	if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
		iter->opal_dev = spdk_opal_dev_construct(iter->ctrlr);
		if (iter->opal_dev == NULL) {
			return;
		}
		printf("Please be noted this operation will erase ALL DATA on this range\n");
		printf("Please input password for erase locking range:");
		while ((ch = getchar()) != '\n' && ch != EOF);
		passwd_p = get_line(passwd, SPDK_OPAL_MAX_PASSWORD_SIZE, stdin, true);
		if (passwd_p) {
			printf("\nSpecify locking range id:\n");
			if (!scanf("%d", &locking_range_id)) {
				printf("Invalid locking range id\n");
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			printf("\n...\n");
			ret = spdk_opal_cmd_secure_erase_locking_range(iter->opal_dev, OPAL_ADMIN1, locking_range_id,
					passwd_p);
			if (ret) {
				printf("Erase locking range failure: %d\n", ret);
				spdk_opal_dev_destruct(iter->opal_dev);
				return;
			}
			printf("...\nErase locking range Success\n");
		} else {
			printf("Input password invalid. Erase locking range failure\n");
		}
		spdk_opal_dev_destruct(iter->opal_dev);
	} else {
		printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
		       iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
	}
}

static void
test_opal(void)
{
	int exit_flag = false;
	struct dev *ctrlr;

	ctrlr = get_controller();
	if (ctrlr == NULL) {
		printf("Invalid controller PCI Address.\n");
		return;
	}

	opal_usage();
	while (!exit_flag) {
		int cmd;
		if (!scanf("%d", &cmd)) {
			printf("Invalid Command: command must be number 0-9\n");
			while (getchar() != '\n');
			opal_usage();
			continue;
		}

		switch (cmd) {
		case 0:
			exit_flag = true;
			continue;
		case 1:
			opal_scan(ctrlr);
			break;
		case 2:
			opal_init(ctrlr);   /* Take ownership, Activate Locking SP */
			break;
		case 3:
			opal_revert_tper(ctrlr);
			break;
		case 4:
			opal_setup_lockingrange(ctrlr);
			break;
		case 5:
			opal_list_locking_ranges(ctrlr);
			break;
		case 6:
			opal_new_user_enable(ctrlr);
			break;
		case 7:
			opal_change_password(ctrlr);
			break;
		case 8:
			opal_add_user_to_locking_range(ctrlr);
			break;
		case 9:
			opal_user_lock_unlock_range(ctrlr);
			break;
		case 10:
			opal_erase_locking_range(ctrlr);
			break;

		default:
			printf("Invalid option\n");
		}

		printf("\npress Enter to display Opal cmd menu ...\n");
		while (getchar() != '\n');
		opal_usage();
	}
}

static void
args_usage(const char *program_name)
{
	printf("%s [options]", program_name);
	printf("\n");
	printf("options:\n");
	printf(" -i         shared memory group ID\n");
}

static int
parse_args(int argc, char **argv)
{
	int op;

	while ((op = getopt(argc, argv, "i:")) != -1) {
		switch (op) {
		case 'i':
			g_shm_id = spdk_strtol(optarg, 10);
			if (g_shm_id < 0) {
				fprintf(stderr, "Invalid shared memory ID\n");
				return g_shm_id;
			}
			break;
		default:
			args_usage(argv[0]);
			return 1;
		}
	}

	return 0;
}

int main(int argc, char **argv)
{
	int			i, rc;
	struct spdk_env_opts	opts;

	rc = parse_args(argc, argv);
	if (rc != 0) {
		return rc;
	}

	spdk_env_opts_init(&opts);
	opts.name = "nvme_manage";
	opts.core_mask = "0x1";
	opts.shm_id = g_shm_id;
	if (spdk_env_init(&opts) < 0) {
		fprintf(stderr, "Unable to initialize SPDK env\n");
		return 1;
	}

	if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) {
		fprintf(stderr, "spdk_nvme_probe() failed\n");
		return 1;
	}

	qsort(devs, num_devs, sizeof(devs[0]), cmp_devs);

	usage();

	while (1) {
		int cmd;
		bool exit_flag = false;

		if (!scanf("%d", &cmd)) {
			printf("Invalid Command: command must be number 1-8\n");
			while (getchar() != '\n');
			usage();
			continue;
		}
		switch (cmd) {
		case 1:
			display_controller_list();
			break;
		case 2:
			add_ns();
			break;
		case 3:
			delete_ns();
			break;
		case 4:
			attach_and_detach_ns(SPDK_NVME_NS_CTRLR_ATTACH);
			break;
		case 5:
			attach_and_detach_ns(SPDK_NVME_NS_CTRLR_DETACH);
			break;
		case 6:
			format_nvm();
			break;
		case 7:
			update_firmware_image();
			break;
		case 8:
			test_opal();
			break;
		case 9:
			exit_flag = true;
			break;
		default:
			printf("Invalid Command\n");
			break;
		}

		if (exit_flag) {
			break;
		}

		while (getchar() != '\n');
		printf("press Enter to display cmd menu ...\n");
		while (getchar() != '\n');
		usage();
	}

	printf("Cleaning up...\n");

	for (i = 0; i < num_devs; i++) {
		struct dev *dev = &devs[i];
		spdk_nvme_detach(dev->ctrlr);
	}

	return 0;
}