|
root / base / usr / src / uts / common / sys / idm
idm Plain Text 1907 lines 49.5 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _IDM_H
#define	_IDM_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/stmf_defines.h>

typedef enum {
	IDM_STATUS_SUCCESS = 0,
	IDM_STATUS_FAIL,
	IDM_STATUS_NORESOURCES,
	IDM_STATUS_REJECT,
	IDM_STATUS_IO,
	IDM_STATUS_ABORTED,
	IDM_STATUS_SUSPENDED,
	IDM_STATUS_HEADER_DIGEST,
	IDM_STATUS_DATA_DIGEST,
	IDM_STATUS_PROTOCOL_ERROR,
	IDM_STATUS_LOGIN_FAIL
} idm_status_t;

#define	IDM_CLIENT_NOTIFY_LIST() \
	item(CN_UNDEFINED) \
	item(CN_CONNECT_ACCEPT)		/* Target only */ \
	item(CN_LOGIN_FAIL) \
	item(CN_READY_FOR_LOGIN)	/* Initiator only */ \
	item(CN_FFP_ENABLED) \
	item(CN_FFP_DISABLED) \
	item(CN_CONNECT_LOST) \
	item(CN_CONNECT_DESTROY) \
	item(CN_CONNECT_FAIL) \
	item(CN_MAX)

typedef enum {
#define	item(a) a,
	IDM_CLIENT_NOTIFY_LIST()
#undef	item
} idm_client_notify_t;

#ifdef IDM_CN_NOTIFY_STRINGS
static const char *idm_cn_strings[CN_MAX + 1] = {
#define	item(a) #a,
	IDM_CLIENT_NOTIFY_LIST()
#undef	item
};
#endif

typedef enum {
	FD_CONN_FAIL,
	FD_CONN_LOGOUT,
	FD_SESS_LOGOUT
} idm_ffp_disable_t;

typedef enum {
	AT_INTERNAL_SUSPEND,
	AT_INTERNAL_ABORT,
	AT_TASK_MGMT_ABORT
} idm_abort_type_t;

#define	IDM_TASK_STATE_LIST() \
	item(TASK_IDLE) \
	item(TASK_ACTIVE) \
	item(TASK_SUSPENDING) \
	item(TASK_SUSPENDED) \
	item(TASK_ABORTING) \
	item(TASK_ABORTED) \
	item(TASK_COMPLETE) \
	item(TASK_MAX_STATE)

typedef enum {
#define	item(a) a,
	IDM_TASK_STATE_LIST()
#undef	item
} idm_task_state_t;

#ifdef IDM_TASK_SM_STRINGS
static const char *idm_ts_name[TASK_MAX_STATE+1] = {
#define	item(a) #a,
	IDM_TASK_STATE_LIST()
#undef	item
};
#endif

typedef enum {
	KV_HANDLED = 0,
	KV_HANDLED_NO_TRANSIT,
	KV_UNHANDLED,
	KV_TARGET_ONLY,
	KV_NO_RESOURCES,
	KV_INTERNAL_ERROR,
	KV_VALUE_ERROR,
	KV_MISSING_FIELDS,
	KV_AUTH_FAILED
} kv_status_t;

/*
 * Request structures
 */

/* Defined in idm_impl.h */
struct idm_conn_s;
struct idm_svc_s;
struct idm_buf_s;
struct idm_pdu_s;
struct idm_task_s;

typedef idm_status_t (idm_client_notify_cb_t)(
    struct idm_conn_s *ic, idm_client_notify_t cn, uintptr_t data);

typedef void (idm_rx_pdu_cb_t)(struct idm_conn_s *ic, struct idm_pdu_s *pdu);

typedef void (idm_rx_pdu_error_cb_t)(struct idm_conn_s *ic,
    struct idm_pdu_s *pdu, idm_status_t status);

typedef void (idm_buf_cb_t)(struct idm_buf_s *idb, idm_status_t status);

typedef void (idm_pdu_cb_t)(struct idm_pdu_s *pdu, idm_status_t status);

typedef void (idm_task_cb_t)(struct idm_task_s *task, idm_status_t status);

typedef void (idm_build_hdr_cb_t)(
    struct idm_task_s *task, struct idm_pdu_s *pdu, uint8_t opcode);

typedef void (idm_update_statsn_cb_t)(
    struct idm_task_s *task, struct idm_pdu_s *pdu);

typedef void (idm_keepalive_cb_t)(struct idm_conn_s *ic);

typedef union idm_sockaddr {
	struct sockaddr		sin;
	struct sockaddr_in	sin4;
	struct sockaddr_in6	sin6;
} idm_sockaddr_t;

#define	SIZEOF_SOCKADDR(so)		\
	((so)->sa_family == AF_INET ?	\
	sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6))

typedef struct {
	idm_rx_pdu_cb_t		*icb_rx_scsi_cmd;
	idm_rx_pdu_cb_t		*icb_rx_scsi_rsp;
	idm_rx_pdu_cb_t		*icb_rx_misc;
	idm_rx_pdu_error_cb_t	*icb_rx_error;
	idm_task_cb_t		*icb_task_aborted;
	idm_client_notify_cb_t	*icb_client_notify;
	idm_build_hdr_cb_t	*icb_build_hdr;
	idm_update_statsn_cb_t	*icb_update_statsn; /* advance statsn */
	idm_keepalive_cb_t	*icb_keepalive;
} idm_conn_ops_t;

typedef struct {
	int			cr_domain;
	int			cr_type;
	int			cr_protocol;
	boolean_t		cr_bound;
	idm_sockaddr_t		cr_bound_addr;
	idm_sockaddr_t		cr_ini_dst_addr;
	ldi_ident_t		cr_li;
	idm_conn_ops_t		icr_conn_ops;
	boolean_t		cr_boot_conn;
} idm_conn_req_t;

typedef struct {
	uint16_t		sr_port;
	ldi_ident_t		sr_li;
	idm_conn_ops_t		sr_conn_ops;
} idm_svc_req_t;


/* This is not how other networking code handles this */
typedef struct {
	union {
		struct in_addr	in4;
		struct in6_addr	in6;
	} i_addr;
	/* i_insize determines which is valid in the union above */
	int			i_insize;
} idm_ipaddr_t;

typedef struct {
	idm_ipaddr_t		a_addr;
	uint32_t		a_port,
				a_oid;
} idm_addr_t;

typedef struct {
	uint32_t		al_vers,			/* In */
				al_oid;				/* In */
	uint32_t		al_in_cnt;			/* In */
	uint32_t		al_out_cnt;			/* Out */
	uint32_t		al_tpgt;			/* Out */
	idm_addr_t		al_addrs[1];			/* Out */
} idm_addr_list_t;

/*
 * State machine auditing
 */

#define	SM_AUDIT_BUF_MAX_REC	32

typedef enum {
	SAR_UNDEFINED = 0,
	SAR_STATE_EVENT,
	SAR_STATE_CHANGE
} sm_audit_record_type_t;

typedef enum {
	SAS_UNDEFINED = 0,
	SAS_IDM_CONN,
	SAS_IDM_TASK,
	SAS_ISCSIT_TGT,
	SAS_ISCSIT_SESS,
	SAS_ISCSIT_LOGIN,
	SAS_ISCSI_CMD,
	SAS_ISCSI_SESS,
	SAS_ISCSI_CONN,
	SAS_ISCSI_LOGIN
} sm_audit_sm_type_t;

typedef struct {
	timespec_t		sar_timestamp;
	sm_audit_sm_type_t	sar_sm_type;
	sm_audit_record_type_t	sar_type;
	int			sar_state;
	int			sar_new_state;	/* Only for SAR_STATE_CHANGE */
	int			sar_event;	/* Only for SAR_STATE_EVENT */
	uintptr_t		sar_event_info;	/* Only for SAR_STATE_EVENT */
} sm_audit_record_t;

typedef struct {
	int			sab_index;
	int			sab_max_index;
	sm_audit_record_t	sab_records[SM_AUDIT_BUF_MAX_REC];
} sm_audit_buf_t;

extern boolean_t idm_sm_logging;
extern boolean_t idm_conn_logging;
extern boolean_t idm_svc_logging;

#define	IDM_SM_LOG if (idm_sm_logging) cmn_err
#define	IDM_CONN_LOG if (idm_conn_logging) cmn_err
#define	IDM_SVC_LOG if (idm_svc_logging) cmn_err

void idm_sm_audit_init(sm_audit_buf_t *audit_buf);

void idm_sm_audit_event(sm_audit_buf_t *audit_buf,
    sm_audit_sm_type_t sm_type,
    int state, int event, uintptr_t event_info);

void idm_sm_audit_state_change(sm_audit_buf_t *audit_buf,
    sm_audit_sm_type_t sm_type, int state, int new_state);


#include <sys/iscsi_protocol.h>
#include <sys/idm/idm_conn_sm.h>
#include <sys/idm/idm_transport.h>
#include <sys/idm/idm_impl.h>
#include <sys/idm/idm_text.h>
#include <sys/idm/idm_so.h>

/*
 * iSCSI Initiator Services
 */

idm_status_t
idm_ini_conn_create(idm_conn_req_t *cr, idm_conn_t **new_con);

idm_status_t
idm_ini_conn_connect(idm_conn_t *ic);

void
idm_ini_conn_disconnect(idm_conn_t *ic);

void
idm_ini_conn_disconnect_sync(idm_conn_t *ic);

void
idm_ini_conn_destroy(idm_conn_t *ic);

/*
 * iSCSI Target Services
 */

idm_status_t
idm_tgt_svc_create(idm_svc_req_t *sr, idm_svc_t **new_svc);

idm_status_t
idm_tgt_svc_online(idm_svc_t *is);

void
idm_tgt_svc_offline(idm_svc_t *is);

void
idm_tgt_svc_destroy(idm_svc_t *is);

void
idm_tgt_svc_destroy_if_unref(idm_svc_t *is);

idm_svc_t *
idm_tgt_svc_lookup(uint16_t port);

void
idm_tgt_svc_hold(idm_svc_t *is);

void
idm_tgt_svc_rele_and_destroy(idm_svc_t *is);

idm_status_t
idm_tgt_conn_accept(idm_conn_t *ic);

void
idm_tgt_conn_reject(idm_conn_t *ic);

void
idm_conn_hold(idm_conn_t *ic);

void
idm_conn_rele(idm_conn_t *ic);

void
idm_conn_set_target_name(idm_conn_t *ic, char *target_name);

void
idm_conn_set_initiator_name(idm_conn_t *ic, char *initiator_name);

void
idm_conn_set_isid(idm_conn_t *ic, uint8_t isid[ISCSI_ISID_LEN]);

/*
 * Target data transfer services
 */
idm_status_t
idm_buf_tx_to_ini(idm_task_t *idt, idm_buf_t *idb,
    uint32_t offset, uint32_t xfer_length,
    idm_buf_cb_t idb_buf_cb, void *cb_arg);

idm_status_t
idm_buf_rx_from_ini(idm_task_t *idt, idm_buf_t *idb,
    uint32_t offset, uint32_t xfer_length,
    idm_buf_cb_t idb_buf_cb, void *cb_arg);

void
idm_buf_tx_to_ini_done(idm_task_t *idt, idm_buf_t *idb, idm_status_t status);

void
idm_buf_rx_from_ini_done(idm_task_t *idt, idm_buf_t *idb, idm_status_t status);

#define	XFER_BUF_TX_TO_INI	0
#define	XFER_BUF_RX_FROM_INI	1
/*
 * Shared Initiator/Target Services
 */
kv_status_t
idm_negotiate_key_values(idm_conn_t *ic, nvlist_t *request_nvl,
    nvlist_t *response_nvl, nvlist_t *negotiated_nvl);

void
idm_notice_key_values(idm_conn_t *ic, nvlist_t *negotiated_nvl);

kv_status_t
idm_declare_key_values(idm_conn_t *ic, nvlist_t *config_nvl,
    nvlist_t *outgoing_nvl);

/*
 * Buffer services
 */

idm_buf_t *
idm_buf_alloc(idm_conn_t *ic, void *bufptr, uint64_t buflen);

void
idm_buf_free(idm_buf_t *idb);

void
idm_buf_bind_in(idm_task_t *idt, idm_buf_t *buf);

void
idm_buf_bind_out(idm_task_t *idt, idm_buf_t *buf);

void
idm_buf_unbind_in(idm_task_t *idt, idm_buf_t *buf);

void
idm_buf_unbind_out(idm_task_t *idt, idm_buf_t *buf);

idm_buf_t *
idm_buf_find(void *lbuf, size_t data_offset);

void
idm_bufpat_set(idm_buf_t *idb);

boolean_t
idm_bufpat_check(idm_buf_t *idb, int check_len, idm_bufpat_check_type_t type);

extern boolean_t idm_pattern_checking;

#define	IDM_BUFPAT_SET(CHK_BUF)					\
	if (idm_pattern_checking && (CHK_BUF)->idb_bufalloc) {	\
		idm_bufpat_set(CHK_BUF);			\
	}

#define	IDM_BUFPAT_CHECK(CHK_BUF, CHK_LEN, CHK_TYPE)		\
	if (idm_pattern_checking) {				\
		(void) idm_bufpat_check(CHK_BUF, CHK_LEN, CHK_TYPE);	\
	}

/*
 * Task services
 */
idm_task_t *
idm_task_alloc(idm_conn_t *ic);

void
idm_task_start(idm_task_t *idt, uintptr_t handle);

stmf_status_t
idm_task_abort(idm_conn_t *ic, idm_task_t *idt, idm_abort_type_t abort_type);

void
idm_task_cleanup(idm_task_t *idt);

void
idm_task_done(idm_task_t *idt);

void
idm_task_free(idm_task_t *idt);

idm_task_t *
idm_task_find(idm_conn_t *ic, uint32_t itt, uint32_t ttt);

idm_task_t *
idm_task_find_and_complete(idm_conn_t *ic, uint32_t itt, uint32_t ttt);

void *
idm_task_find_by_handle(idm_conn_t *ic, uintptr_t handle);

void
idm_task_hold(idm_task_t *idt);

void
idm_task_rele(idm_task_t *idt);

/*
 * PDU Services
 */

idm_pdu_t *
idm_pdu_alloc(uint_t hdrlen, uint_t datalen);

idm_pdu_t *
idm_pdu_alloc_nosleep(uint_t hdrlen, uint_t datalen);

void
idm_pdu_free(idm_pdu_t *pdu);

void
idm_pdu_init(idm_pdu_t *pdu, idm_conn_t *ic, void *private, idm_pdu_cb_t *cb);

void
idm_pdu_init_hdr(idm_pdu_t *pdu, uint8_t *hdr, uint_t hdrlen);

void
idm_pdu_init_data(idm_pdu_t *pdu, uint8_t *data, uint_t datalen);

void
idm_pdu_complete(idm_pdu_t *pdu, idm_status_t status);

void
idm_pdu_tx(idm_pdu_t *pdu);

/*
 * Object reference tracking
 */

void
idm_refcnt_init(idm_refcnt_t *refcnt, void *referenced_obj);

void
idm_refcnt_destroy(idm_refcnt_t *refcnt);

void
idm_refcnt_reset(idm_refcnt_t *refcnt);

void
idm_refcnt_hold(idm_refcnt_t *refcnt);

void
idm_refcnt_rele(idm_refcnt_t *refcnt);

void
idm_refcnt_rele_and_destroy(idm_refcnt_t *refcnt, idm_refcnt_cb_t *cb_func);

void
idm_refcnt_wait_ref(idm_refcnt_t *refcnt);

void
idm_refcnt_async_wait_ref(idm_refcnt_t *refcnt, idm_refcnt_cb_t *cb_func);

int
idm_refcnt_is_held(idm_refcnt_t *refcnt);

#ifdef	__cplusplus
}
#endif

#endif /* _IDM_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
 */
#ifndef	_IDM_CONN_SM_H_
#define	_IDM_CONN_SM_H_

#ifdef	__cplusplus
extern "C" {
#endif


/*
 * IDM connection state machine events.  Most events get generated internally
 * either by the state machine or by the IDM TX/RX code.  For example when IDM
 * receives a login request for a target connectionit will generate a
 * CE_LOGIN_RCV event.  Similarly when the target sends a successful login
 * response IDM generate a "CE_LOGIN_SUCCESS_SND" event.  The following
 * events are not detected on the TX/RX path and must be generated explicitly
 * by the client when appropriate:
 *
 * CE_LOGOUT_OTHER_CONN_RCV
 * CE_ASYNC_DROP_CONN_RCV   (Only because the message may be received on
 * a different connection from the connection being dropped)
 * CE_ASYNC_DROP_ALL_CONN_RCV
 * CE_LOGOUT_OTHER_CONN_SND
 * CE_ASYNC_DROP_ALL_CONN_SND
 *
 * The following events might occur in any state since they are driven
 * by the PDU's that IDM receives:
 *
 * CE_LOGIN_RCV
 * CE_LOGIN_SUCCESS_RCV
 * CE_LOGIN_FAIL_RCV
 * CE_LOGOUT_SUCCESS_RCV
 * CE_LOGOUT_FAIL_RCV
 * CE_ASYNC_LOGOUT_RCV
 * CE_MISC_RCV
 * CE_RX_PROTOCOL_ERROR
 */

#define	IDM_LOGIN_SECONDS	20
#define	IDM_LOGOUT_SECONDS	20
#define	IDM_CLEANUP_SECONDS	0

#define	IDM_CONN_EVENT_LIST() \
	item(CE_UNDEFINED) \
	/* Initiator events */ \
	item(CE_CONNECT_REQ) \
	item(CE_CONNECT_FAIL) \
	item(CE_CONNECT_SUCCESS) \
	item(CE_LOGIN_SND) \
	item(CE_LOGIN_SUCCESS_RCV) \
	item(CE_LOGIN_FAIL_RCV) \
	item(CE_LOGOUT_THIS_CONN_SND) \
	item(CE_LOGOUT_OTHER_CONN_SND) \
	item(CE_LOGOUT_SESSION_SND) \
	item(CE_LOGOUT_SUCCESS_RCV) \
	item(CE_LOGOUT_FAIL_RCV) \
	item(CE_ASYNC_LOGOUT_RCV) \
	item(CE_ASYNC_DROP_CONN_RCV) \
	item(CE_ASYNC_DROP_ALL_CONN_RCV) \
	/* Target events */ \
	item(CE_CONNECT_ACCEPT) \
	item(CE_CONNECT_REJECT) \
	item(CE_LOGIN_RCV) \
	item(CE_LOGIN_TIMEOUT) \
	item(CE_LOGIN_SUCCESS_SND) \
	item(CE_LOGIN_FAIL_SND) \
	item(CE_LOGIN_FAIL_SND_DONE) \
	item(CE_LOGOUT_THIS_CONN_RCV) \
	item(CE_LOGOUT_OTHER_CONN_RCV) \
	item(CE_LOGOUT_SESSION_RCV) \
	item(CE_LOGOUT_SUCCESS_SND) \
	item(CE_LOGOUT_SUCCESS_SND_DONE) \
	item(CE_LOGOUT_FAIL_SND) \
	item(CE_LOGOUT_FAIL_SND_DONE) \
	item(CE_CLEANUP_TIMEOUT) \
	item(CE_ASYNC_LOGOUT_SND) \
	item(CE_ASYNC_DROP_CONN_SND) \
	item(CE_ASYNC_DROP_ALL_CONN_SND) \
	item(CE_LOGOUT_TIMEOUT) \
	/* Common events */ \
	item(CE_TRANSPORT_FAIL) \
	item(CE_MISC_TX) \
	item(CE_TX_PROTOCOL_ERROR) \
	item(CE_MISC_RX) \
	item(CE_RX_PROTOCOL_ERROR) \
	item(CE_LOGOUT_SESSION_SUCCESS) \
	item(CE_CONN_REINSTATE) \
	item(CE_CONN_REINSTATE_SUCCESS) \
	item(CE_CONN_REINSTATE_FAIL) \
	item(CE_ENABLE_DM_SUCCESS) \
	item(CE_ENABLE_DM_FAIL) \
	/* Add new events above CE_MAX_EVENT */ \
	item(CE_MAX_EVENT)

/* Update idm_ce_name table whenever connection events are modified */
typedef enum {
#define	item(a) a,
	IDM_CONN_EVENT_LIST()
#undef	item
} idm_conn_event_t;

#ifdef IDM_CONN_SM_STRINGS
/* An array of event text values, for use in logging events */
static const char *idm_ce_name[CE_MAX_EVENT+1] = {
#define	item(a) #a,
	IDM_CONN_EVENT_LIST()
#undef	item
};
#endif

#define	CONN_STATE_LIST() \
	item(CS_S0_UNDEFINED) \
	item(CS_S1_FREE) \
	item(CS_S2_XPT_WAIT) \
	item(CS_S3_XPT_UP) \
	item(CS_S4_IN_LOGIN) \
	item(CS_S5_LOGGED_IN) \
	item(CS_S6_IN_LOGOUT) \
	item(CS_S7_LOGOUT_REQ) \
	item(CS_S8_CLEANUP) \
	item(CS_S9_INIT_ERROR) \
	item(CS_S10_IN_CLEANUP) \
	item(CS_S11_COMPLETE) \
	item(CS_S12_ENABLE_DM) \
	item(CS_S9A_REJECTED) \
	item(CS_S9B_WAIT_SND_DONE) \
	/* Add new connection states above CS_MAX_STATE */ \
	item(CS_MAX_STATE)

/* Update idm_cs_name table whenever connection states are modified */
typedef enum {
#define	item(a) a,
	CONN_STATE_LIST()
#undef	item
} idm_conn_state_t;

#ifdef IDM_CONN_SM_STRINGS
/* An array of state text values, for use in logging state transitions */
static const char *idm_cs_name[CS_MAX_STATE+1] = {
#define	item(a) #a,
	CONN_STATE_LIST()
#undef	item
};
#endif

/*
 * Currently the state machine has a condition where idm_login_timeout() is
 * left active after the connection has been closed. This causes the system
 * to panic when idm_login_timeout() modifies the freed memory. In an attempt
 * to isolate and find this issue special attention is being placed on
 * the ic_state_timeout value. After each untimeout call the value will now
 * be cleared. Just before the value is set the code will check for 0 and
 * display an error. One final change is being done in idm_conn_sm_fini() which
 * if ic_state_machine is not 0, an error message will be displayed and
 * untimeout() called. That should prevent customer sites from seeing the
 * panic. The code also calls ASSERT(0) which should cause a panic during
 * system test.
 */
#define	IDM_SM_TIMER_CHECK(ic) \
	if (ic->ic_state_timeout) { \
		cmn_err(CE_WARN, "%s: existing timeout still set. " \
		    "state: %s, last: %s\n", __func__, \
		    idm_cs_name[ic->ic_state], \
		    idm_cs_name[ic->ic_last_state]); \
		ASSERT(0); \
	}

#define	IDM_SM_TIMER_CLEAR(ic) \
	(void) untimeout(ic->ic_state_timeout); \
	ic->ic_state_timeout = 0;

typedef enum {
	CT_NONE = 0,
	CT_RX_PDU,
	CT_TX_PDU
} idm_pdu_event_type_t;

typedef enum {
	CA_TX_PROTOCOL_ERROR,	/* Send "protocol error" to state machine */
	CA_RX_PROTOCOL_ERROR,	/* Send "protocol error" to state machine */
	CA_FORWARD,		/* State machine event and forward to client */
	CA_DROP			/* Drop PDU */
} idm_pdu_event_action_t;

typedef struct {
	struct idm_conn_s	*iec_ic;
	idm_conn_event_t	iec_event;
	uintptr_t		iec_info;
	idm_pdu_event_type_t	iec_pdu_event_type;
	boolean_t		iec_pdu_forwarded;
} idm_conn_event_ctx_t;

idm_status_t
idm_conn_sm_init(struct idm_conn_s *ic);

void
idm_conn_sm_fini(struct idm_conn_s *ic);

idm_status_t
idm_notify_client(struct idm_conn_s *ic, idm_client_notify_t cn,
    uintptr_t data);

void
idm_conn_event(struct idm_conn_s *ic, idm_conn_event_t event, uintptr_t data);

void
idm_conn_event(struct idm_conn_s *ic, idm_conn_event_t event, uintptr_t data);

void
idm_conn_event_locked(struct idm_conn_s *ic, idm_conn_event_t event,
    uintptr_t event_info, idm_pdu_event_type_t pdu_event_type);

idm_status_t
idm_conn_reinstate_event(struct idm_conn_s *old_ic, struct idm_conn_s *new_ic);

void
idm_conn_tx_pdu_event(struct idm_conn_s *ic, idm_conn_event_t event,
    uintptr_t data);

void
idm_conn_rx_pdu_event(struct idm_conn_s *ic, idm_conn_event_t event,
    uintptr_t data);

char *
idm_conn_state_str(struct idm_conn_s *ic);

#ifdef	__cplusplus
}
#endif

#endif /* _IDM_CONN_SM_H_ */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 */
/*
 * Copyright 2014-2015 Nexenta Systems, Inc.  All rights reserved.
 */

#ifndef	_IDM_IMPL_H_
#define	_IDM_IMPL_H_

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/avl.h>
#include <sys/socket_impl.h>
#include <sys/taskq_impl.h>

/*
 * IDM lock order:
 *
 * idm_taskid_table_lock, idm_task_t.idt_mutex
 */

#define	CF_LOGIN_READY		0x00000001
#define	CF_INITIAL_LOGIN	0x00000002
#define	CF_ERROR		0x80000000

typedef enum {
	CONN_TYPE_INI = 1,
	CONN_TYPE_TGT
} idm_conn_type_t;

/*
 * Watchdog interval in seconds
 */
#define	IDM_WD_INTERVAL			5

/*
 * Timeout period before the client "keepalive" callback is invoked in
 * seconds if the connection is idle.
 */
#define	IDM_TRANSPORT_KEEPALIVE_IDLE_TIMEOUT	20

/*
 * Timeout period before a TRANSPORT_FAIL event is generated in seconds
 * if the connection is idle.
 */
#define	IDM_TRANSPORT_FAIL_IDLE_TIMEOUT	30

/*
 * IDM reference count structure.  Audit code is shamelessly adapted
 * from CIFS server.
 */

#define	REFCNT_AUDIT_STACK_DEPTH	16
#define	REFCNT_AUDIT_BUF_MAX_REC	16

typedef struct {
	uint32_t		anr_refcnt;
	int			anr_depth;
	pc_t			anr_stack[REFCNT_AUDIT_STACK_DEPTH];
} refcnt_audit_record_t;

typedef struct {
	int			anb_index;
	int			anb_max_index;
	refcnt_audit_record_t	anb_records[REFCNT_AUDIT_BUF_MAX_REC];
} refcnt_audit_buf_t;

#define	REFCNT_AUDIT(_rf_) {				\
	refcnt_audit_record_t	*anr;			\
							\
	anr = (_rf_)->ir_audit_buf.anb_records;		\
	anr += (_rf_)->ir_audit_buf.anb_index;		\
	(_rf_)->ir_audit_buf.anb_index++;		\
	(_rf_)->ir_audit_buf.anb_index &=		\
	    (_rf_)->ir_audit_buf.anb_max_index;		\
	anr->anr_refcnt = (_rf_)->ir_refcnt;		\
	anr->anr_depth = getpcstack(anr->anr_stack,	\
	    REFCNT_AUDIT_STACK_DEPTH);			\
}

struct idm_refcnt_s;

typedef void (idm_refcnt_cb_t)(void *ref_obj);

typedef enum {
	REF_NOWAIT,
	REF_WAIT_SYNC,
	REF_WAIT_ASYNC
} idm_refcnt_wait_t;

typedef struct idm_refcnt_s {
	int			ir_refcnt;
	void			*ir_referenced_obj;
	idm_refcnt_wait_t	ir_waiting;
	kmutex_t		ir_mutex;
	kcondvar_t		ir_cv;
	idm_refcnt_cb_t		*ir_cb;
	refcnt_audit_buf_t	ir_audit_buf;
} idm_refcnt_t;

/*
 * connection parameters - These parameters would be populated at
 * connection create, or during key-value negotiation at login
 */
typedef struct idm_conn_params_s {
	uint32_t		max_recv_dataseglen;
	uint32_t		max_xmit_dataseglen;
	uint32_t		conn_login_max;
	uint32_t		conn_login_interval;
	boolean_t		nonblock_socket;
} idm_conn_param_t;

typedef struct idm_svc_s {
	list_node_t		is_list_node;
	kmutex_t		is_mutex;
	kcondvar_t		is_cv;
	kmutex_t		is_count_mutex;
	kcondvar_t		is_count_cv;
	idm_refcnt_t		is_refcnt;
	int			is_online;
	/* transport-specific service components */
	void			*is_so_svc;
	void			*is_iser_svc;
	idm_svc_req_t		is_svc_req;
} idm_svc_t;

#define	ISCSI_MAX_TSIH_LEN	6	/* 0x%04x */
#define	ISCSI_MAX_ISID_LEN	ISCSI_ISID_LEN * 2

typedef struct idm_conn_s {
	list_node_t		ic_list_node;
	void			*ic_handle;
	idm_refcnt_t		ic_refcnt;
	idm_svc_t		*ic_svc_binding; /* Target conn. only */
	idm_sockaddr_t		ic_ini_dst_addr;
	struct sockaddr_storage	ic_laddr;	/* conn local address */
	struct sockaddr_storage	ic_raddr;	/* conn remote address */

	/*
	 * the target_name, initiator_name, initiator session
	 * identifier and target session identifying handle
	 * are only used for target connections.
	 */
	char			ic_target_name[ISCSI_MAX_NAME_LEN + 1];
	char			ic_initiator_name[ISCSI_MAX_NAME_LEN + 1];
	char			ic_tsih[ISCSI_MAX_TSIH_LEN + 1];
	char			ic_isid[ISCSI_MAX_ISID_LEN + 1];
	idm_conn_state_t	ic_state;
	idm_conn_state_t	ic_last_state;
	sm_audit_buf_t		ic_state_audit;
	kmutex_t		ic_state_mutex;
	kcondvar_t		ic_state_cv;
	uint32_t		ic_state_flags;
	timeout_id_t		ic_state_timeout;
	struct idm_conn_s	*ic_reinstate_conn; /* For conn reinst. */
	struct idm_conn_s	*ic_logout_conn; /* For other conn logout */
	taskq_t			*ic_state_taskq;
	int			ic_pdu_events;
	boolean_t		ic_login_info_valid;
	boolean_t		ic_rdma_extensions;
	uint16_t		ic_login_cid;

	kmutex_t		ic_mutex;
	kcondvar_t		ic_cv;
	idm_status_t		ic_conn_sm_status;

	boolean_t		ic_ffp;
	boolean_t		ic_keepalive;
	uint32_t		ic_internal_cid;

	uint32_t		ic_conn_flags;
	idm_conn_type_t		ic_conn_type;
	idm_conn_ops_t		ic_conn_ops;
	idm_transport_ops_t	*ic_transport_ops;
	idm_transport_type_t	ic_transport_type;
	int			ic_transport_hdrlen;
	void			*ic_transport_private;
	idm_conn_param_t	ic_conn_params;
	/*
	 * Save client callback to interpose idm callback
	 */
	idm_pdu_cb_t		*ic_client_callback;
	clock_t			ic_timestamp;
} idm_conn_t;

#define	IDM_CONN_HEADER_DIGEST	0x00000001
#define	IDM_CONN_DATA_DIGEST	0x00000002
#define	IDM_CONN_USE_SCOREBOARD	0x00000004

#define	IDM_CONN_ISINI(ICI_IC)	((ICI_IC)->ic_conn_type == CONN_TYPE_INI)
#define	IDM_CONN_ISTGT(ICI_IC)	((ICI_IC)->ic_conn_type == CONN_TYPE_TGT)

/*
 * An IDM target task can transfer data using multiple buffers. The task
 * will maintain a list of buffers, and each buffer will contain the relative
 * offset of the transfer and a pointer to the next buffer in the list.
 *
 * Note on client private data:
 * idt_private is intended to be a pointer to some sort of client-
 * specific state.
 *
 * idt_client_handle is a more generic client-private piece of data that can
 * be used by the client for the express purpose of task lookup.  The driving
 * use case for this is for the client to store the initiator task tag for
 * a given task so that it may be more easily retrieved for task management.
 *
 * The key take away here is that clients should never call
 * idm_task_find_by_handle in the performance path.
 *
 * An initiator will require only one buffer per task, the offset will be 0.
 */

typedef struct idm_task_s {
	idm_conn_t		*idt_ic;	/* Associated connection */
	/* connection type is in idt_ic->ic_conn_type */
	kmutex_t		idt_mutex;
	void			*idt_private;	/* Client private data */
	uintptr_t		idt_client_handle;	/* Client private */
	uint32_t		idt_tt;		/* Task tag */
	uint32_t		idt_r2t_ttt;	/* R2T Target Task tag */
	idm_task_state_t	idt_state;
	idm_refcnt_t		idt_refcnt;

	/*
	 * Statistics
	 */
	int			idt_tx_to_ini_start;
	int			idt_tx_to_ini_done;
	int			idt_rx_from_ini_start;
	int			idt_rx_from_ini_done;
	int			idt_tx_bytes;	/* IDM_CONN_USE_SCOREBOARD */
	int			idt_rx_bytes;	/* IDM_CONN_USE_SCOREBOARD */

	uint32_t		idt_exp_datasn;	/* expected datasn */
	uint32_t		idt_exp_rttsn;	/* expected rttsn */
	list_t			idt_inbufv;	/* chunks of IN buffers */
	list_t			idt_outbufv;	/* chunks of OUT buffers */

	/*
	 * Transport header, which describes this tasks remote tagged buffer
	 */
	int			idt_transport_hdrlen;
	void			*idt_transport_hdr;
	uint32_t		idt_flags;	/* phase collapse */
} idm_task_t;

int idm_task_constructor(void *task_void, void *arg, int flags);
void idm_task_destructor(void *task_void, void *arg);

#define	IDM_TASKIDS_MAX		16384
#define	IDM_BUF_MAGIC		0x49425546	/* "IBUF" */

#define	IDM_TASK_PHASECOLLAPSE_REQ	0x00000001 /* request phase collapse */
#define	IDM_TASK_PHASECOLLAPSE_SUCCESS	0x00000002 /* phase collapse success */

/* Protect with task mutex */
typedef struct idm_buf_s {
	uint32_t	idb_magic;	/* "IBUF" */

	/*
	 * Note: idm_tx_link *must* be the second element in the list for
	 * proper TX PDU ordering.
	 */
	list_node_t	idm_tx_link;	/* link in a list of TX objects */

	list_node_t	idb_buflink;	/* link in a multi-buffer data xfer */
	idm_conn_t	*idb_ic;	/* Associated connection */
	void		*idb_buf;	/* data */
	uint64_t	idb_buflen;	/* length of buffer */
	size_t		idb_bufoffset;	/* offset in a multi-buffer xfer */
	boolean_t	idb_bufalloc;  /* true if alloc'd in idm_buf_alloc */
	/*
	 * DataPDUInOrder=Yes, so to track that the PDUs in a sequence are sent
	 * in continuously increasing address order, check that offsets for a
	 * single buffer xfer are in order.
	 */
	uint32_t	idb_exp_offset;
	size_t		idb_xfer_len;	/* Current requested xfer len */
	void		*idb_buf_private; /* transport-specific buf handle */
	void		*idb_reg_private; /* transport-specific reg handle */
	void		*idb_bufptr; /* transport-specific bcopy pointer */
	boolean_t	idb_bufbcopy;	/* true if bcopy required */

	idm_buf_cb_t	*idb_buf_cb;	/* Data Completion Notify, tgt only */
	void		*idb_cb_arg;	/* Client private data */
	idm_task_t	*idb_task_binding;
	timespec_t	idb_xfer_start;
	timespec_t	idb_xfer_done;
	boolean_t	idb_in_transport;
	boolean_t	idb_tx_thread;		/* Sockets only */
	iscsi_hdr_t	idb_data_hdr_tmpl;	/* Sockets only */
	idm_status_t	idb_status;
} idm_buf_t;

typedef enum {
	BP_CHECK_QUICK,
	BP_CHECK_THOROUGH,
	BP_CHECK_ASSERT
} idm_bufpat_check_type_t;

#define	BUFPAT_MATCH(bc_bufpat, bc_idb)			\
	((bufpat->bufpat_idb == bc_idb) &&		\
	    (bufpat->bufpat_bufmagic == IDM_BUF_MAGIC))

typedef struct idm_bufpat_s {
	void		*bufpat_idb;
	uint32_t	bufpat_bufmagic;
	uint32_t	bufpat_offset;
} idm_bufpat_t;

#define	PDU_MAX_IOVLEN	12
#define	IDM_PDU_MAGIC	0x49504455	/* "IPDU" */

typedef struct idm_pdu_s {
	uint32_t	isp_magic;	/* "IPDU" */

	/*
	 * Internal - Order is vital.  idm_tx_link *must* be the second
	 * element in this structure for proper TX PDU ordering.
	 */
	list_node_t	idm_tx_link;

	list_node_t	isp_client_lnd;

	idm_conn_t	*isp_ic;	/* Must be set */
	iscsi_hdr_t	*isp_hdr;
	uint_t		isp_hdrlen;
	uint8_t		*isp_data;
	uint_t		isp_datalen;

	/* Transport header */
	void		*isp_transport_hdr;
	uint32_t	isp_transport_hdrlen;
	void		*isp_transport_private;

	/*
	 * isp_data is used for sending SCSI status, NOP, text, scsi and
	 * non-scsi data. Data is received using isp_iov and isp_iovlen
	 * to support data over multiple buffers.
	 */
	void		*isp_private;
	idm_pdu_cb_t	*isp_callback;
	idm_status_t	isp_status;

	/*
	 * The following four elements are only used in
	 * idm_sorecv_scsidata() currently.
	 */
	struct iovec	isp_iov[PDU_MAX_IOVLEN];
	int		isp_iovlen;
	idm_buf_t	*isp_sorx_buf;

	/* Implementation data for idm_pdu_alloc and sorx PDU cache */
	uint32_t	isp_flags;
	uint_t		isp_hdrbuflen;
	uint_t		isp_databuflen;
	hrtime_t	isp_queue_time;

	/* Taskq dispatching state for deferred PDU */
	taskq_ent_t	isp_tqent;
} idm_pdu_t;

/*
 * This "generic" object is used when removing an item from the ic_tx_list
 * in order to determine whether it's an idm_pdu_t or an idm_buf_t
 */

typedef struct {
	uint32_t	idm_tx_obj_magic;
	/*
	 * idm_tx_link *must* be the second element in this structure.
	 */
	list_node_t	idm_tx_link;
} idm_tx_obj_t;


#define	IDM_PDU_OPCODE(PDU) \
	((PDU)->isp_hdr->opcode & ISCSI_OPCODE_MASK)

#define	IDM_PDU_ALLOC		0x00000001
#define	IDM_PDU_ADDL_HDR	0x00000002
#define	IDM_PDU_ADDL_DATA	0x00000004
#define	IDM_PDU_LOGIN_TX	0x00000008
#define	IDM_PDU_SET_STATSN	0x00000010
#define	IDM_PDU_ADVANCE_STATSN	0x00000020

#define	OSD_EXT_CDB_AHSLEN	(200 - 15)
#define	BIDI_AHS_LENGTH		5
/*
 * Additional Header Segment (AHS)
 * AHS is only valid for SCSI Requests and contains SCSI CDB information
 * which doesn't fit in the standard 16 byte area of the PDU. Commonly
 * this only holds true for OSD device commands.
 *
 * IDM_SORX_CACHE_ASHLEN is the amount of memory which is preallocated in bytes.
 * When used in the header the AHS length is stored as the number of 4-byte
 * words; so IDM_SORX_WIRE_ASHLEN is IDM_SORX_CACHE_ASHLEN in words.
 */
#define	IDM_SORX_CACHE_AHSLEN \
	((OSD_EXT_CDB_AHSLEN + 3) + (BIDI_AHS_LENGTH + 3))
#define	IDM_SORX_WIRE_AHSLEN (IDM_SORX_CACHE_AHSLEN / sizeof (uint32_t))
#define	IDM_SORX_CACHE_HDRLEN	(sizeof (iscsi_hdr_t) + IDM_SORX_CACHE_AHSLEN)

/*
 * ID pool
 */

#define	IDM_IDPOOL_MAGIC	0x4944504C	/* IDPL */
#define	IDM_IDPOOL_MIN_SIZE	64	/* Number of IDs to begin with */
#define	IDM_IDPOOL_MAX_SIZE	64 * 1024

typedef struct idm_idpool {
	uint32_t	id_magic;
	kmutex_t	id_mutex;
	uint8_t		*id_pool;
	uint32_t	id_size;
	uint8_t		id_bit;
	uint8_t		id_bit_idx;
	uint32_t	id_idx;
	uint32_t	id_idx_msk;
	uint32_t	id_free_counter;
	uint32_t	id_max_free_counter;
} idm_idpool_t;

/*
 * Global IDM state structure
 */
typedef struct {
	kmutex_t	idm_global_mutex;
	taskq_t		*idm_global_taskq;
	kthread_t	*idm_wd_thread;
	kt_did_t	idm_wd_thread_did;
	boolean_t	idm_wd_thread_running;
	kcondvar_t	idm_wd_cv;
	list_t		idm_tgt_svc_list;
	kcondvar_t	idm_tgt_svc_cv;
	list_t		idm_tgt_conn_list;
	int		idm_tgt_conn_count;
	list_t		idm_ini_conn_list;
	kmem_cache_t	*idm_buf_cache;
	kmem_cache_t	*idm_task_cache;
	krwlock_t	idm_taskid_table_lock;
	idm_task_t	**idm_taskid_table;
	uint32_t	idm_taskid_next;
	uint32_t	idm_taskid_max;
	idm_idpool_t	idm_conn_id_pool;
	kmem_cache_t	*idm_sotx_pdu_cache;
	kmem_cache_t	*idm_sorx_pdu_cache;
	kmem_cache_t	*idm_so_128k_buf_cache;
} idm_global_t;

extern idm_global_t	idm; /* Global state */

int
idm_idpool_create(idm_idpool_t	*pool);

void
idm_idpool_destroy(idm_idpool_t *pool);

int
idm_idpool_alloc(idm_idpool_t *pool, uint16_t *id);

void
idm_idpool_free(idm_idpool_t *pool, uint16_t id);

void
idm_pdu_rx(idm_conn_t *ic, idm_pdu_t *pdu);

void
idm_pdu_tx_forward(idm_conn_t *ic, idm_pdu_t *pdu);

boolean_t
idm_pdu_rx_forward_ffp(idm_conn_t *ic, idm_pdu_t *pdu);

void
idm_pdu_rx_forward(idm_conn_t *ic, idm_pdu_t *pdu);

void
idm_pdu_tx_protocol_error(idm_conn_t *ic, idm_pdu_t *pdu);

void
idm_pdu_rx_protocol_error(idm_conn_t *ic, idm_pdu_t *pdu);

void idm_parse_login_rsp(idm_conn_t *ic, idm_pdu_t *logout_req_pdu,
    boolean_t rx);

void idm_parse_logout_req(idm_conn_t *ic, idm_pdu_t *logout_req_pdu,
    boolean_t rx);

void idm_parse_logout_rsp(idm_conn_t *ic, idm_pdu_t *login_rsp_pdu,
    boolean_t rx);

idm_status_t idm_svc_conn_create(idm_svc_t *is, idm_transport_type_t type,
    idm_conn_t **ic_result);

void idm_svc_conn_destroy(idm_conn_t *ic);

idm_status_t idm_ini_conn_finish(idm_conn_t *ic);

idm_status_t idm_tgt_conn_finish(idm_conn_t *ic);

idm_conn_t *idm_conn_create_common(idm_conn_type_t conn_type,
    idm_transport_type_t tt, idm_conn_ops_t *conn_ops);

void idm_conn_destroy_common(idm_conn_t *ic);

void idm_conn_close(idm_conn_t *ic);

uint32_t idm_cid_alloc(void);

void idm_cid_free(uint32_t cid);

uint32_t idm_crc32c(void *address, unsigned long length);

uint32_t idm_crc32c_continued(void *address, unsigned long length,
    uint32_t crc);

void idm_listbuf_insert(list_t *lst, idm_buf_t *buf);

idm_conn_t *idm_lookup_conn(uint8_t *isid, uint16_t tsih, uint16_t cid);

#ifdef	__cplusplus
}
#endif

#endif /* _IDM_IMPL_H_ */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _IDM_SO_H
#define	_IDM_SO_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/idm/idm_transport.h>
#include <sys/ksocket.h>

/*
 * Define TCP window size (send and receive buffer sizes)
 */

#define	IDM_RCVBUF_SIZE		(256 * 1024)
#define	IDM_SNDBUF_SIZE		(256 * 1024)

/*
 * Lower and upper bounds to use the 128k buffer cache.  Below the lower bound
 * allocations will use the built-in Solaris buffer caches.  We don't expect
 * to see allocations above the upper bound because SBD currently allocates
 * 128k buffers.
 */

#define	IDM_SO_BUF_CACHE_LB	(32 * 1024)
#define	IDM_SO_BUF_CACHE_UB	(128 * 1024)

/* sockets-specific portion of idm_svc_t */
typedef struct idm_so_svc_s {
	ksocket_t		is_so;
	kthread_t		*is_thread;
	kt_did_t		is_thread_did;
	boolean_t		is_thread_running;
} idm_so_svc_t;

/* sockets-specific portion of idm_conn_t */
typedef struct idm_so_conn_s {
	ksocket_t		ic_so;

	kthread_t		*ic_tx_thread;
	kt_did_t		ic_tx_thread_did;
	boolean_t		ic_tx_thread_running;
	kmutex_t		ic_tx_mutex;
	kcondvar_t		ic_tx_cv;
	list_t			ic_tx_list;	/* List of PDUs for transmit */

	kthread_t		*ic_rx_thread;
	kt_did_t		ic_rx_thread_did;
	boolean_t		ic_rx_thread_running;
} idm_so_conn_t;

void idm_so_init(idm_transport_t *it);
void idm_so_fini();

/* used by idm_so_timed_socket_connect */
typedef struct idm_so_timed_socket_s {
	kcondvar_t	it_cv;
	boolean_t	it_callback_called;
	int		it_socket_error_code;
} idm_so_timed_socket_t;

/* Socket functions */

ksocket_t
idm_socreate(int domain, int type, int protocol);

void idm_soshutdown(ksocket_t so);

void idm_sodestroy(ksocket_t so);

int idm_ss_compare(const struct sockaddr_storage *cmp_ss1,
    const struct sockaddr_storage *cmp_ss2,
    boolean_t v4_mapped_as_v4,
    boolean_t compare_ports);

int idm_get_ipaddr(idm_addr_list_t **);

void idm_addr_to_sa(idm_addr_t *dportal,
    struct sockaddr_storage *sa);

#define	IDM_SA_NTOP_BUFSIZ (INET6_ADDRSTRLEN + sizeof ("[].65535") + 1)

const char *idm_sa_ntop(const struct sockaddr_storage *sa,
    char *buf, size_t size);

int idm_sorecv(ksocket_t so, void *msg, size_t len);

int idm_sosendto(ksocket_t so, void *buff, size_t len,
    struct sockaddr *name, socklen_t namelen);

int idm_iov_sosend(ksocket_t so, iovec_t *iop, int iovlen,
    size_t total_len);

int idm_iov_sorecv(ksocket_t so, iovec_t *iop, int iovlen,
    size_t total_len);

void idm_sotx_thread(void *arg);
void idm_sorx_thread(void *arg);


int idm_sotx_pdu_constructor(void *hdl, void *arg, int flags);

void idm_sotx_pdu_destructor(void *pdu_void, void *arg);

int idm_sorx_pdu_constructor(void *hdl, void *arg, int flags);

void idm_sorx_pdu_destructor(void *pdu_void, void *arg);

void idm_so_svc_port_watcher(void *arg);

int idm_so_timed_socket_connect(ksocket_t ks,
    struct sockaddr_storage *sa, int sa_sz, int login_max_usec);

#ifdef	__cplusplus
}
#endif

#endif /* _IDM_SO_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#ifndef _IDM_TEXT_H_
#define	_IDM_TEXT_H_

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/idm/idm_impl.h>

/*
 * Numerical identifiers for iSCSI name-value pair keys (just so that
 * we can use case statements to handle a particular key-value pair
 * after we find it in idm_kvpair_xlate).
 *
 * We want to use a bitmask to keep track of negotiated key-value pairs
 * so keep this enum under 64 values -- or spend some time reworking the
 * login code.
 */
typedef enum {
	KI_AUTH_METHOD = 1,
	KI_KRB_AP_REQ,
	KI_KRB_AP_REP,

	/* SPKM */
	KI_SPKM_REQ,
	KI_SPKM_ERROR,
	KI_SPKM_REP_TI,
	KI_SPKM_REP_IT,

	/*
	 * SRP
	 */
	KI_SRP_U,
	KI_TARGET_AUTH,
	KI_SRP_GROUP,
	KI_SRP_A,
	KI_SRP_B,
	KI_SRP_M,
	KI_SRM_HM,

	/*
	 * CHAP
	 */
	KI_CHAP_A,
	KI_CHAP_I,
	KI_CHAP_C,
	KI_CHAP_N,
	KI_CHAP_R,


	/*
	 * ISCSI Operational Parameter Keys
	 */
	KI_HEADER_DIGEST,
	KI_DATA_DIGEST,
	KI_MAX_CONNECTIONS,
	KI_SEND_TARGETS,
	KI_TARGET_NAME,
	KI_INITIATOR_NAME,
	KI_TARGET_ALIAS,
	KI_INITIATOR_ALIAS,
	KI_TARGET_ADDRESS,
	KI_TARGET_PORTAL_GROUP_TAG,
	KI_INITIAL_R2T,
	KI_IMMEDIATE_DATA,
	KI_MAX_RECV_DATA_SEGMENT_LENGTH,
	KI_MAX_BURST_LENGTH,
	KI_FIRST_BURST_LENGTH,
	KI_DEFAULT_TIME_2_WAIT,
	KI_DEFAULT_TIME_2_RETAIN,
	KI_MAX_OUTSTANDING_R2T,
	KI_DATA_PDU_IN_ORDER,
	KI_DATA_SEQUENCE_IN_ORDER,
	KI_ERROR_RECOVERY_LEVEL,
	KI_SESSION_TYPE,
	KI_OFMARKER,
	KI_OFMARKERINT,
	KI_IFMARKER,
	KI_IFMARKERINT,

	/*
	 * iSER-specific keys
	 */
	KI_RDMA_EXTENSIONS,
	KI_TARGET_RECV_DATA_SEGMENT_LENGTH,
	KI_INITIATOR_RECV_DATA_SEGMENT_LENGTH,
	KI_MAX_OUTSTANDING_UNEXPECTED_PDUS,

	/*
	 * End of list marker, no keys below here.
	 */
	KI_MAX_KEY
} iscsikey_id_t;

/* Numerical types for iSCSI name-value pair values */
typedef enum {
	KT_TEXT,
	KT_ISCSI_NAME,
	KT_ISCSI_LOCAL_NAME,
	KT_BOOLEAN,
	KT_NUMERICAL, /* Hex or decimal constant */
	KT_LARGE_NUMERICAL, /* Hex, decimal or Base64 constant */
	KT_NUMERIC_RANGE,
	KT_REGULAR_BINARY, /* Hex, decimal, base64 not longer than 64 bits */
	KT_LARGE_BINARY, /* Hex, decimal, base64 longer than 64 bites */
	KT_BINARY,	/* Regular binary or large binary */
	KT_SIMPLE,
	KT_LIST_OF_VALUES
} idmkey_type_t;

typedef struct {
	iscsikey_id_t		ik_key_id;
	char			*ik_key_name;
	idmkey_type_t		ik_idm_type; /* RFC type */
	boolean_t		ik_declarative;
} idm_kv_xlate_t;

const idm_kv_xlate_t *
idm_lookup_kv_xlate(const char *key, int keylen);

int
idm_nvlist_add_keyvalue(nvlist_t *nvl, char *key, int keylen, char *value);

int
idm_textbuf_to_nvlist(nvlist_t *nvl, char **textbuf, int *textbuflen);

int
idm_textbuf_to_firstfraglen(void *textbuf, int textbuflen);

int
idm_nvlist_to_textbuf(nvlist_t *nvl, char **textbuf, int *textbuflen,
    int *tblen_required);

kv_status_t
idm_nvstat_to_kvstat(int nvrc);

void
idm_kvstat_to_error(kv_status_t kvrc, uint8_t *class, uint8_t *detail);

int
idm_nvlist_add_id(nvlist_t *nvl, iscsikey_id_t kv_id, char *value);

nvpair_t *
idm_get_next_listvalue(nvpair_t *value_list, nvpair_t *curr_nvp);

char *
idm_id_to_name(iscsikey_id_t kv_id);

char *
idm_nvpair_value_to_textbuf(nvpair_t *nvp);

idm_status_t
idm_pdu_list_to_nvlist(list_t *pdu_list, nvlist_t **nvlist,
    uint8_t *error_detail);

void *
idm_nvlist_to_itextbuf(nvlist_t *nvl);

char *
idm_pdu_init_text_data(idm_pdu_t *pdu, void *arg,
    int max_xfer_len, char *bufptr, int *transit);

void
idm_itextbuf_free(void *arg);

#ifdef	__cplusplus
}
#endif

#endif /* _IDM_TEXT_H_ */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_IDM_TRANSPORT_H_
#define	_IDM_TRANSPORT_H_

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/nvpair.h>
#include <sys/sunldi.h>

#define	IDM_TRANSPORT_PATHLEN	0x40

/* Note, this is tied to iSER currently */
#define	IDM_TRANSPORT_HEADER_LENGTH	0x20

/*
 * idm_transport_type_t
 * An enumerated list of the transports available to iSER.
 * Note that new transports should be added to the enum prior to NUM_TYPES.
 */
typedef enum {
	IDM_TRANSPORT_TYPE_ISER	= 0,
	IDM_TRANSPORT_TYPE_SOCKETS,
	IDM_TRANSPORT_NUM_TYPES,
	IDM_TRANSPORT_TYPE_UNDEFINED
} idm_transport_type_t;

/*
 * idm_transport_caps_t
 * Encodes a set of attributes describing an IDM transport's capabilities.
 *	JB - do we need this?
 */
typedef struct idm_transport_caps_s {
	uint32_t	flags;
} idm_transport_caps_t;

/*
 * Transport routine definitions for idm_transport_ops_t below
 */

/* Send_Control - transmit a Control-type PDU */
typedef void (transport_tx_op_t)(struct idm_conn_s *ic, struct idm_pdu_s *pdu);

/*
 * Target transport data primitives, caller (IDM) holds idt->idt_mutex,
 * and the transport should release the mutex before returning.
 */
typedef idm_status_t (transport_buf_tx_to_ini_op_t)(struct idm_task_s *idt,
    struct idm_buf_s *idb);
typedef idm_status_t (transport_buf_rx_from_ini_op_t)(struct idm_task_s *idt,
    struct idm_buf_s *idb);

/* Initiator transport data handlers */
typedef void (transport_rx_datain_op_t)(struct idm_conn_s *ic,
    struct idm_pdu_s *pdu);
typedef void (transport_rx_rtt_op_t)(struct idm_conn_s *ic,
    struct idm_pdu_s *pdu);

/* Target transport Data-out handler */
typedef void (transport_rx_dataout_op_t)(struct idm_conn_s *ic,
    struct idm_pdu_s *pdu);

/* Transport-specific resource allocation and free */
typedef idm_status_t (transport_alloc_conn_rsrc_op_t)(struct idm_conn_s *ic);
typedef idm_status_t (transport_free_conn_rsrc_op_t)(struct idm_conn_s *ic);

/* Transport driver operations enable/disable */
typedef idm_status_t (transport_tgt_enable_datamover_op_t)(struct
    idm_conn_s *ic);
typedef idm_status_t (transport_ini_enable_datamover_op_t)(struct
    idm_conn_s *ic);
typedef idm_status_t (transport_conn_terminate_op_t)(struct idm_conn_s *ic);

/* Task resource cleanup */
typedef idm_status_t (transport_free_task_rsrcs_op_t)(struct idm_task_s *it);

/* Negotiate key value pairs */
typedef kv_status_t (transport_negotiate_key_values_op_t)(struct
    idm_conn_s *ic, nvlist_t *request_nvl, nvlist_t *response_nvl,
    nvlist_t *negotiated_nvl);

/* Activate the negotiated key value pairs */
typedef void (transport_notice_key_values_op_t)(struct idm_conn_s *ic,
    nvlist_t *negotiated_nvl);

/* Declare the declarative key value pairs */
typedef kv_status_t (transport_declare_key_values_op_t)(struct idm_conn_s *ic,
    nvlist_t *config_nvl, nvlist_t *outgoing_nvl);

/* Transport capability probe */
typedef boolean_t (transport_conn_is_capable_op_t)(idm_conn_req_t *ic,
    struct idm_transport_caps_s *caps);

/* Transport buffer services */
typedef idm_status_t (transport_buf_alloc_op_t)(struct idm_buf_s *idb,
    uint64_t buflen);
typedef idm_status_t (transport_buf_setup_op_t)(struct idm_buf_s *idb);
typedef void (transport_buf_teardown_op_t)(struct idm_buf_s *idb);
typedef void (transport_buf_free_op_t)(struct idm_buf_s *idb);

/* Transport target context and service management services */
typedef idm_status_t (transport_tgt_svc_create_op_t)(idm_svc_req_t *sr,
    struct idm_svc_s *is);
typedef void (transport_tgt_svc_destroy_op_t)(struct idm_svc_s *is);
typedef idm_status_t (transport_tgt_svc_online_op_t)(struct idm_svc_s *is);
typedef void (transport_tgt_svc_offline_op_t)(struct idm_svc_s *is);

/* Transport target connection establishment */
typedef void (transport_tgt_conn_destroy_op_t)(struct idm_conn_s *ic);
typedef idm_status_t (transport_tgt_conn_connect_op_t)(struct idm_conn_s *ic);
typedef void (transport_tgt_conn_disconnect_op_t)(struct idm_conn_s *ic);

/* Transport initiator context and connection management services */
typedef idm_status_t (transport_ini_conn_create_op_t)(idm_conn_req_t *cr,
    struct idm_conn_s *ic);
typedef void (transport_ini_conn_destroy_op_t)(struct idm_conn_s *ic);
typedef idm_status_t (transport_ini_conn_connect_op_t)(struct idm_conn_s *ic);
typedef void (transport_ini_conn_disconnect_op_t)(struct idm_conn_s *ic);


/*
 * idm_transport_ops_t
 * Encodes a set of vectors into an IDM transport driver that implement the
 * transport-specific Datamover operations for IDM usage. These routines are
 * invoked by the IDM layer to execute the transport-specific implementations
 * of the DataMover primitives and supporting routines.
 */
typedef struct idm_transport_ops_s {
	transport_tx_op_t			*it_tx_pdu;
	transport_buf_tx_to_ini_op_t		*it_buf_tx_to_ini;
	transport_buf_rx_from_ini_op_t		*it_buf_rx_from_ini;
	transport_rx_datain_op_t		*it_rx_datain;
	transport_rx_rtt_op_t			*it_rx_rtt;
	transport_rx_dataout_op_t		*it_rx_dataout;
	transport_alloc_conn_rsrc_op_t		*it_alloc_conn_rsrc;
	transport_free_conn_rsrc_op_t		*it_free_conn_rsrc;
	transport_tgt_enable_datamover_op_t	*it_tgt_enable_datamover;
	transport_ini_enable_datamover_op_t	*it_ini_enable_datamover;
	transport_conn_terminate_op_t		*it_conn_terminate;
	transport_free_task_rsrcs_op_t		*it_free_task_rsrc;
	transport_negotiate_key_values_op_t	*it_negotiate_key_values;
	transport_notice_key_values_op_t	*it_notice_key_values;
	transport_conn_is_capable_op_t		*it_conn_is_capable;
	transport_buf_alloc_op_t		*it_buf_alloc;
	transport_buf_free_op_t			*it_buf_free;
	transport_buf_setup_op_t		*it_buf_setup;
	transport_buf_teardown_op_t		*it_buf_teardown;
	transport_tgt_svc_create_op_t		*it_tgt_svc_create;
	transport_tgt_svc_destroy_op_t		*it_tgt_svc_destroy;
	transport_tgt_svc_online_op_t		*it_tgt_svc_online;
	transport_tgt_svc_offline_op_t		*it_tgt_svc_offline;
	transport_tgt_conn_destroy_op_t		*it_tgt_conn_destroy;
	transport_tgt_conn_connect_op_t		*it_tgt_conn_connect;
	transport_tgt_conn_disconnect_op_t	*it_tgt_conn_disconnect;
	transport_ini_conn_create_op_t		*it_ini_conn_create;
	transport_ini_conn_destroy_op_t		*it_ini_conn_destroy;
	transport_ini_conn_connect_op_t		*it_ini_conn_connect;
	transport_ini_conn_disconnect_op_t	*it_ini_conn_disconnect;
	transport_declare_key_values_op_t	*it_declare_key_values;
} idm_transport_ops_t;

/*
 * idm_transport_t encodes all of the data related to an IDM transport
 * type. In addition to type and capabilities, it also stores a pointer
 * to the connection and transport operation implementations, and also
 * it stores the LDI handle.
 */
typedef struct idm_transport_s {
	idm_transport_type_t	it_type;
	char			*it_device_path;
	ldi_handle_t		it_ldi_hdl;
	idm_transport_ops_t	*it_ops;
	idm_transport_caps_t	*it_caps;
} idm_transport_t;

/*
 * idm_transport_attr_t encodes details of a transport driver seeking
 * registration with the IDM kernel module.
 */
typedef struct idm_transport_attr_s {
	idm_transport_type_t	type;
	idm_transport_ops_t	*it_ops;
	idm_transport_caps_t	*it_caps;
} idm_transport_attr_t;

/* IDM transport API */
idm_status_t
idm_transport_register(idm_transport_attr_t *attr);

idm_transport_t *
idm_transport_lookup(idm_conn_req_t *cr);

void
idm_transport_setup(ldi_ident_t li, boolean_t boot_conn);

void
idm_transport_teardown();

#ifdef	__cplusplus
}
#endif

#endif /* _IDM_TRANSPORT_H_ */