summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/idm/idm_conn_sm.c
blob: 45ba8872b801ae6e3fe5cac66952acbe76192509 (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
/*
 * 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 (c) 2013 by Delphix. All rights reserved.
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 * Copyright 2020 Joyent, Inc.
 */

#include <sys/cpuvar.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/modctl.h>
#include <sys/socket.h>
#include <sys/strsubr.h>
#include <sys/note.h>
#include <sys/sdt.h>

#define	IDM_CONN_SM_STRINGS
#define	IDM_CN_NOTIFY_STRINGS
#include <sys/idm/idm.h>

boolean_t	idm_sm_logging = B_FALSE;

extern idm_global_t	idm; /* Global state */

static void
idm_conn_event_handler(void *event_ctx_opaque);

static void
idm_state_s1_free(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s2_xpt_wait(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s3_xpt_up(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s4_in_login(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s5_logged_in(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s6_in_logout(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_logout_req_timeout(void *arg);

static void
idm_state_s7_logout_req(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s8_cleanup(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s9_init_error(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s9a_rejected(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s9b_wait_snd_done_cb(idm_pdu_t *pdu,
    idm_status_t status);

static void
idm_state_s9b_wait_snd_done(idm_conn_t *ic,
    idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s10_in_cleanup(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s11_complete(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_state_s12_enable_dm(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_update_state(idm_conn_t *ic, idm_conn_state_t new_state,
    idm_conn_event_ctx_t *event_ctx);

static void
idm_conn_unref(void *ic_void);

static void
idm_conn_reject_unref(void *ic_void);

static idm_pdu_event_action_t
idm_conn_sm_validate_pdu(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx,
    idm_pdu_t *pdu);

static idm_status_t
idm_ffp_enable(idm_conn_t *ic);

static void
idm_ffp_disable(idm_conn_t *ic, idm_ffp_disable_t disable_type);

static void
idm_initial_login_actions(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

static void
idm_login_success_actions(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx);

idm_status_t
idm_conn_sm_init(idm_conn_t *ic)
{
	char taskq_name[32];

	/*
	 * Caller should have assigned a unique connection ID.  Use this
	 * connection ID to create a unique connection name string
	 */
	ASSERT(ic->ic_internal_cid != 0);
	(void) snprintf(taskq_name, sizeof (taskq_name) - 1, "conn_sm%08x",
	    ic->ic_internal_cid);

	ic->ic_state_taskq = taskq_create(taskq_name, 1, minclsyspri, 4, 16384,
	    TASKQ_PREPOPULATE);
	if (ic->ic_state_taskq == NULL) {
		return (IDM_STATUS_FAIL);
	}

	idm_sm_audit_init(&ic->ic_state_audit);
	mutex_init(&ic->ic_state_mutex, NULL, MUTEX_DEFAULT, NULL);
	cv_init(&ic->ic_state_cv, NULL, CV_DEFAULT, NULL);

	ic->ic_state = CS_S1_FREE;
	ic->ic_last_state = CS_S1_FREE;

	return (IDM_STATUS_SUCCESS);
}

void
idm_conn_sm_fini(idm_conn_t *ic)
{

	/*
	 * The connection may only be partially created. If there
	 * is no taskq, then the connection SM was not initialized.
	 */
	if (ic->ic_state_taskq == NULL) {
		return;
	}

	taskq_destroy(ic->ic_state_taskq);

	cv_destroy(&ic->ic_state_cv);
	/*
	 * The thread that generated the event that got us here may still
	 * hold the ic_state_mutex. Once it is released we can safely
	 * destroy it since there is no way to locate the object now.
	 */
	mutex_enter(&ic->ic_state_mutex);
	IDM_SM_TIMER_CLEAR(ic);
	mutex_destroy(&ic->ic_state_mutex);
}

void
idm_conn_event(idm_conn_t *ic, idm_conn_event_t event, uintptr_t event_info)
{
	mutex_enter(&ic->ic_state_mutex);
	idm_conn_event_locked(ic, event, event_info, CT_NONE);
	mutex_exit(&ic->ic_state_mutex);
}


idm_status_t
idm_conn_reinstate_event(idm_conn_t *old_ic, idm_conn_t *new_ic)
{
	int result;

	mutex_enter(&old_ic->ic_state_mutex);
	if (((old_ic->ic_conn_type == CONN_TYPE_INI) &&
	    (old_ic->ic_state != CS_S8_CLEANUP)) ||
	    ((old_ic->ic_conn_type == CONN_TYPE_TGT) &&
	    (old_ic->ic_state < CS_S5_LOGGED_IN))) {
		result = IDM_STATUS_FAIL;
	} else {
		result = IDM_STATUS_SUCCESS;
		new_ic->ic_reinstate_conn = old_ic;
		idm_conn_event_locked(new_ic->ic_reinstate_conn,
		    CE_CONN_REINSTATE, (uintptr_t)new_ic, CT_NONE);
	}
	mutex_exit(&old_ic->ic_state_mutex);

	return (result);
}

void
idm_conn_tx_pdu_event(idm_conn_t *ic, idm_conn_event_t event,
    uintptr_t event_info)
{
	ASSERT(mutex_owned(&ic->ic_state_mutex));
	ic->ic_pdu_events++;
	idm_conn_event_locked(ic, event, event_info, CT_TX_PDU);
}

void
idm_conn_rx_pdu_event(idm_conn_t *ic, idm_conn_event_t event,
    uintptr_t event_info)
{
	ASSERT(mutex_owned(&ic->ic_state_mutex));
	ic->ic_pdu_events++;
	idm_conn_event_locked(ic, event, event_info, CT_RX_PDU);
}

void
idm_conn_event_locked(idm_conn_t *ic, idm_conn_event_t event,
    uintptr_t event_info, idm_pdu_event_type_t pdu_event_type)
{
	idm_conn_event_ctx_t	*event_ctx;

	ASSERT(mutex_owned(&ic->ic_state_mutex));

	idm_sm_audit_event(&ic->ic_state_audit, SAS_IDM_CONN,
	    (int)ic->ic_state, (int)event, event_info);

	/*
	 * It's very difficult to prevent a few straggling events
	 * at the end.  For example idm_sorx_thread will generate
	 * a CE_TRANSPORT_FAIL event when it exits.  Rather than
	 * push complicated restrictions all over the code to
	 * prevent this we will simply drop the events (and in
	 * the case of PDU events release them appropriately)
	 * since they are irrelevant once we are in a terminal state.
	 * Of course those threads need to have appropriate holds on
	 * the connection otherwise it might disappear.
	 */
	if ((ic->ic_state == CS_S9_INIT_ERROR) ||
	    (ic->ic_state == CS_S9A_REJECTED) ||
	    (ic->ic_state == CS_S11_COMPLETE)) {
		if ((pdu_event_type == CT_TX_PDU) ||
		    (pdu_event_type == CT_RX_PDU)) {
			ic->ic_pdu_events--;
			idm_pdu_complete((idm_pdu_t *)event_info,
			    IDM_STATUS_SUCCESS);
		}
		IDM_SM_LOG(CE_NOTE, "*** Dropping event %s (%d) because of"
		    "state %s (%d)",
		    idm_ce_name[event], event,
		    idm_cs_name[ic->ic_state], ic->ic_state);
		return;
	}

	/*
	 * Normal event handling
	 */
	idm_conn_hold(ic);

	event_ctx = kmem_zalloc(sizeof (*event_ctx), KM_SLEEP);
	event_ctx->iec_ic = ic;
	event_ctx->iec_event = event;
	event_ctx->iec_info = event_info;
	event_ctx->iec_pdu_event_type = pdu_event_type;

	(void) taskq_dispatch(ic->ic_state_taskq, &idm_conn_event_handler,
	    event_ctx, TQ_SLEEP);
}

static void
idm_conn_event_handler(void *event_ctx_opaque)
{
	idm_conn_event_ctx_t *event_ctx = event_ctx_opaque;
	idm_conn_t *ic = event_ctx->iec_ic;
	idm_pdu_t *pdu = (idm_pdu_t *)event_ctx->iec_info;
	idm_pdu_event_action_t action;

	IDM_SM_LOG(CE_NOTE, "idm_conn_event_handler: conn %p event %s(%d)",
	    (void *)ic, idm_ce_name[event_ctx->iec_event],
	    event_ctx->iec_event);
	DTRACE_PROBE2(conn__event,
	    idm_conn_t *, ic, idm_conn_event_ctx_t *, event_ctx);

	/*
	 * Validate event
	 */
	ASSERT(event_ctx->iec_event != CE_UNDEFINED);
	ASSERT3U(event_ctx->iec_event, <, CE_MAX_EVENT);

	/*
	 * Validate current state
	 */
	ASSERT(ic->ic_state != CS_S0_UNDEFINED);
	ASSERT3U(ic->ic_state, <, CS_MAX_STATE);

	/*
	 * Validate PDU-related events against the current state.  If a PDU
	 * is not allowed in the current state we change the event to a
	 * protocol error.  This simplifies the state-specific event handlers.
	 * For example the CS_S2_XPT_WAIT state only needs to handle the
	 * CE_TX_PROTOCOL_ERROR and CE_RX_PROTOCOL_ERROR events since
	 * no PDU's can be transmitted or received in that state.
	 */
	event_ctx->iec_pdu_forwarded = B_FALSE;
	if (event_ctx->iec_pdu_event_type != CT_NONE) {
		ASSERT(pdu != NULL);
		action = idm_conn_sm_validate_pdu(ic, event_ctx, pdu);

		switch (action) {
		case CA_TX_PROTOCOL_ERROR:
			/*
			 * Change event and forward the PDU
			 */
			event_ctx->iec_event = CE_TX_PROTOCOL_ERROR;
			break;
		case CA_RX_PROTOCOL_ERROR:
			/*
			 * Change event and forward the PDU.
			 */
			event_ctx->iec_event = CE_RX_PROTOCOL_ERROR;
			break;
		case CA_FORWARD:
			/*
			 * Let the state-specific event handlers take
			 * care of it.
			 */
			break;
		case CA_DROP:
			/*
			 * It never even happened
			 */
			IDM_SM_LOG(CE_NOTE, "*** drop PDU %p", (void *) pdu);
			idm_pdu_complete(pdu, IDM_STATUS_FAIL);
			event_ctx->iec_info = (uintptr_t)NULL;
			break;
		default:
			ASSERT(0);
			break;
		}
	}

	switch (ic->ic_state) {
	case CS_S1_FREE:
		idm_state_s1_free(ic, event_ctx);
		break;
	case CS_S2_XPT_WAIT:
		idm_state_s2_xpt_wait(ic, event_ctx);
		break;
	case CS_S3_XPT_UP:
		idm_state_s3_xpt_up(ic, event_ctx);
		break;
	case CS_S4_IN_LOGIN:
		idm_state_s4_in_login(ic, event_ctx);
		break;
	case CS_S5_LOGGED_IN:
		idm_state_s5_logged_in(ic, event_ctx);
		break;
	case CS_S6_IN_LOGOUT:
		idm_state_s6_in_logout(ic, event_ctx);
		break;
	case CS_S7_LOGOUT_REQ:
		idm_state_s7_logout_req(ic, event_ctx);
		break;
	case CS_S8_CLEANUP:
		idm_state_s8_cleanup(ic, event_ctx);
		break;
	case CS_S9A_REJECTED:
		idm_state_s9a_rejected(ic, event_ctx);
		break;
	case CS_S9B_WAIT_SND_DONE:
		idm_state_s9b_wait_snd_done(ic, event_ctx);
		break;
	case CS_S9_INIT_ERROR:
		idm_state_s9_init_error(ic, event_ctx);
		break;
	case CS_S10_IN_CLEANUP:
		idm_state_s10_in_cleanup(ic, event_ctx);
		break;
	case CS_S11_COMPLETE:
		idm_state_s11_complete(ic, event_ctx);
		break;
	case CS_S12_ENABLE_DM:
		idm_state_s12_enable_dm(ic, event_ctx);
		break;
	default:
		ASSERT(0);
		break;
	}

	/*
	 * Now that we've updated the state machine, if this was
	 * a PDU-related event take the appropriate action on the PDU
	 * (transmit it, forward it to the clients RX callback, drop
	 * it, etc).
	 */
	if (event_ctx->iec_pdu_event_type != CT_NONE) {
		switch (action) {
		case CA_TX_PROTOCOL_ERROR:
			idm_pdu_tx_protocol_error(ic, pdu);
			break;
		case CA_RX_PROTOCOL_ERROR:
			idm_pdu_rx_protocol_error(ic, pdu);
			break;
		case CA_FORWARD:
			if (!event_ctx->iec_pdu_forwarded) {
				if (event_ctx->iec_pdu_event_type ==
				    CT_RX_PDU) {
					idm_pdu_rx_forward(ic, pdu);
				} else {
					idm_pdu_tx_forward(ic, pdu);
				}
			}
			break;
		case CA_DROP:
			/* Already completed above. */
			ASSERT3P(event_ctx->iec_info, ==, NULL);
			break;
		default:
			ASSERT(0);
			break;
		}
	}

	/*
	 * Update outstanding PDU event count (see idm_pdu_tx for
	 * how this is used)
	 */
	if ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ||
	    (event_ctx->iec_pdu_event_type == CT_RX_PDU)) {
		mutex_enter(&ic->ic_state_mutex);
		ic->ic_pdu_events--;
		mutex_exit(&ic->ic_state_mutex);
	}

	idm_conn_rele(ic);
	kmem_free(event_ctx, sizeof (*event_ctx));
}

static void
idm_state_s1_free(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	switch (event_ctx->iec_event) {
	case CE_CONNECT_REQ:
		/* T1 */
		idm_update_state(ic, CS_S2_XPT_WAIT, event_ctx);
		break;
	case CE_CONNECT_ACCEPT:
		/* T3 */
		idm_update_state(ic, CS_S3_XPT_UP, event_ctx);
		break;
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
		/* This should never happen */
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	default:
		ASSERT(0);
		/*NOTREACHED*/
	}
}


static void
idm_state_s2_xpt_wait(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	switch (event_ctx->iec_event) {
	case CE_CONNECT_SUCCESS:
		/* T4 */
		idm_update_state(ic, CS_S4_IN_LOGIN, event_ctx);
		break;
	case CE_TRANSPORT_FAIL:
	case CE_CONNECT_FAIL:
	case CE_LOGOUT_OTHER_CONN_RCV:
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
		/* T2 */
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	default:
		ASSERT(0);
		/*NOTREACHED*/
	}
}


static void
idm_login_timeout(void *arg)
{
	idm_conn_t *ic = arg;

	ic->ic_state_timeout = 0;
	idm_conn_event(ic, CE_LOGIN_TIMEOUT, (uintptr_t)NULL);
}

static void
idm_state_s3_xpt_up(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	switch (event_ctx->iec_event) {
	case CE_LOGIN_RCV:
		/* T4 */
		/* Keep login timeout active through S3 and into S4 */
		idm_initial_login_actions(ic, event_ctx);
		idm_update_state(ic, CS_S4_IN_LOGIN, event_ctx);
		break;
	case CE_LOGIN_TIMEOUT:
		/*
		 * Don't need to cancel login timer since the timer is
		 * presumed to be the source of this event.
		 */
		(void) idm_notify_client(ic, CN_LOGIN_FAIL, (uintptr_t)NULL);
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	case CE_CONNECT_REJECT:
		/*
		 * Iscsit doesn't want to hear from us again in this case.
		 * Since it rejected the connection it doesn't have a
		 * connection context to handle additional notifications.
		 * IDM needs to just clean things up on its own.
		 */
		IDM_SM_TIMER_CLEAR(ic);
		idm_update_state(ic, CS_S9A_REJECTED, event_ctx);
		break;
	case CE_CONNECT_FAIL:
	case CE_TRANSPORT_FAIL:
	case CE_LOGOUT_OTHER_CONN_SND:
		/* T6 */
		IDM_SM_TIMER_CLEAR(ic);
		(void) idm_notify_client(ic, CN_LOGIN_FAIL, (uintptr_t)NULL);
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
		/* Don't care */
		break;
	default:
		ASSERT(0);
		/*NOTREACHED*/
	}
}

static void
idm_state_s4_in_login(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t *pdu;

	/*
	 * Login timer should no longer be active after leaving this
	 * state.
	 */
	switch (event_ctx->iec_event) {
	case CE_LOGIN_SUCCESS_RCV:
	case CE_LOGIN_SUCCESS_SND:
		ASSERT(ic->ic_client_callback == NULL);

		IDM_SM_TIMER_CLEAR(ic);
		idm_login_success_actions(ic, event_ctx);
		if (ic->ic_rdma_extensions) {
			/* T19 */
			idm_update_state(ic, CS_S12_ENABLE_DM, event_ctx);
		} else {
			/* T5 */
			idm_update_state(ic, CS_S5_LOGGED_IN, event_ctx);
		}
		break;
	case CE_LOGIN_TIMEOUT:
		/* T7 */
		(void) idm_notify_client(ic, CN_LOGIN_FAIL, (uintptr_t)NULL);
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	case CE_LOGIN_FAIL_SND:
		/*
		 * Allow the logout response pdu to be sent and defer
		 * the state machine cleanup until the completion callback.
		 * Only 1 level or callback interposition is allowed.
		 */
		IDM_SM_TIMER_CLEAR(ic);
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		ASSERT(ic->ic_client_callback == NULL);
		ic->ic_client_callback = pdu->isp_callback;
		pdu->isp_callback =
		    idm_state_s9b_wait_snd_done_cb;
		idm_update_state(ic, CS_S9B_WAIT_SND_DONE,
		    event_ctx);
		break;
	case CE_LOGIN_FAIL_RCV:
		ASSERT(ic->ic_client_callback == NULL);
		/*
		 * Need to deliver this PDU to the initiator now because after
		 * we update the state to CS_S9_INIT_ERROR the initiator will
		 * no longer be in an appropriate state.
		 */
		event_ctx->iec_pdu_forwarded = B_TRUE;
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		idm_pdu_rx_forward(ic, pdu);
		/* FALLTHROUGH */
	case CE_TRANSPORT_FAIL:
	case CE_LOGOUT_OTHER_CONN_SND:
	case CE_LOGOUT_OTHER_CONN_RCV:
		/* T7 */
		IDM_SM_TIMER_CLEAR(ic);
		(void) idm_notify_client(ic, CN_LOGIN_FAIL, (uintptr_t)NULL);
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	case CE_LOGOUT_SESSION_SUCCESS:
		/*
		 * T8
		 * A session reinstatement request can be received while a
		 * session is active and a login is in process. The iSCSI
		 * connections are shut down by a CE_LOGOUT_SESSION_SUCCESS
		 * event sent from the session to the IDM layer.
		 */
		IDM_SM_TIMER_CLEAR(ic);
		if (IDM_CONN_ISTGT(ic)) {
			ic->ic_transport_ops->it_tgt_conn_disconnect(ic);
		} else {
			ic->ic_transport_ops->it_ini_conn_disconnect(ic);
		}
		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;

	case CE_LOGIN_SND:
		ASSERT(ic->ic_client_callback == NULL);
		/*
		 * Initiator connections will see initial login PDU
		 * in this state.  Target connections see initial
		 * login PDU in "xpt up" state.
		 */
		mutex_enter(&ic->ic_state_mutex);
		if (!(ic->ic_state_flags & CF_INITIAL_LOGIN)) {
			idm_initial_login_actions(ic, event_ctx);
		}
		mutex_exit(&ic->ic_state_mutex);
		break;
	case CE_MISC_TX:
	case CE_MISC_RX:
	case CE_LOGIN_RCV:
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
		/* Don't care */
		break;
	default:
		ASSERT(0);
		/*NOTREACHED*/
	}
}


static void
idm_state_s5_logged_in(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	switch (event_ctx->iec_event) {
	case CE_MISC_RX:
		/* MC/S: when removing the non-leading connection */
	case CE_LOGOUT_THIS_CONN_RCV:
	case CE_LOGOUT_THIS_CONN_SND:
	case CE_LOGOUT_OTHER_CONN_RCV:
	case CE_LOGOUT_OTHER_CONN_SND:
		/* T9 */
		idm_ffp_disable(ic, FD_CONN_LOGOUT); /* Explicit logout */
		idm_update_state(ic, CS_S6_IN_LOGOUT, event_ctx);
		break;
	case CE_LOGOUT_SESSION_RCV:
	case CE_LOGOUT_SESSION_SND:
		/* T9 */
		idm_ffp_disable(ic, FD_SESS_LOGOUT); /* Explicit logout */
		idm_update_state(ic, CS_S6_IN_LOGOUT, event_ctx);
		break;
	case CE_LOGOUT_SESSION_SUCCESS:
		/* T8 */
		idm_ffp_disable(ic, FD_SESS_LOGOUT); /* Explicit logout */

		/* Close connection */
		if (IDM_CONN_ISTGT(ic)) {
			ic->ic_transport_ops->it_tgt_conn_disconnect(ic);
		} else {
			ic->ic_transport_ops->it_ini_conn_disconnect(ic);
		}

		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;
	case CE_ASYNC_LOGOUT_RCV:
	case CE_ASYNC_LOGOUT_SND:
		/* T11 */
		idm_update_state(ic, CS_S7_LOGOUT_REQ, event_ctx);
		break;
	case CE_TRANSPORT_FAIL:
	case CE_ASYNC_DROP_CONN_RCV:
	case CE_ASYNC_DROP_CONN_SND:
	case CE_ASYNC_DROP_ALL_CONN_RCV:
	case CE_ASYNC_DROP_ALL_CONN_SND:
		/* T15 */
		idm_ffp_disable(ic, FD_CONN_FAIL); /* Implicit logout */
		idm_update_state(ic, CS_S8_CLEANUP, event_ctx);
		break;
	case CE_MISC_TX:
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
	case CE_LOGIN_TIMEOUT:
		/* Don't care */
		break;
	default:
		ASSERT(0);
	}
}

static void
idm_state_s6_in_logout_success_snd_done(idm_pdu_t *pdu, idm_status_t status)
{
	idm_conn_t		*ic = pdu->isp_ic;

	/*
	 * This pdu callback can be invoked by the tx thread,
	 * so run the disconnect code from another thread.
	 */
	pdu->isp_status = status;
	idm_conn_event(ic, CE_LOGOUT_SUCCESS_SND_DONE, (uintptr_t)pdu);
}

static void
idm_state_s6_in_logout_fail_snd_done(idm_pdu_t *pdu, idm_status_t status)
{
	idm_conn_t		*ic = pdu->isp_ic;

	/*
	 * This pdu callback can be invoked by the tx thread,
	 * so run the disconnect code from another thread.
	 */
	pdu->isp_status = status;
	idm_conn_event(ic, CE_LOGOUT_FAIL_SND_DONE, (uintptr_t)pdu);
}

static void
idm_state_s6_in_logout(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t *pdu;

	switch (event_ctx->iec_event) {
	case CE_LOGOUT_SUCCESS_SND_DONE:
		pdu = (idm_pdu_t *)event_ctx->iec_info;

		/* Close connection (if it's not already closed) */
		ASSERT(IDM_CONN_ISTGT(ic));
		ic->ic_transport_ops->it_tgt_conn_disconnect(ic);

		/* restore client callback */
		pdu->isp_callback =  ic->ic_client_callback;
		ic->ic_client_callback = NULL;
		idm_pdu_complete(pdu, pdu->isp_status);
		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;
	case CE_LOGOUT_FAIL_SND_DONE:
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		/* restore client callback */
		pdu->isp_callback =  ic->ic_client_callback;
		ic->ic_client_callback = NULL;
		idm_pdu_complete(pdu, pdu->isp_status);
		idm_update_state(ic, CS_S8_CLEANUP, event_ctx);
		break;
	case CE_LOGOUT_SUCCESS_SND:
	case CE_LOGOUT_FAIL_SND:
		/*
		 * Allow the logout response pdu to be sent and defer
		 * the state machine update until the completion callback.
		 * Only 1 level or callback interposition is allowed.
		 */
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		ASSERT(ic->ic_client_callback == NULL);
		ic->ic_client_callback = pdu->isp_callback;
		if (event_ctx->iec_event == CE_LOGOUT_SUCCESS_SND) {
			pdu->isp_callback =
			    idm_state_s6_in_logout_success_snd_done;
		} else {
			pdu->isp_callback =
			    idm_state_s6_in_logout_fail_snd_done;
		}
		break;
	case CE_LOGOUT_SUCCESS_RCV:
		/*
		 * Need to deliver this PDU to the initiator now because after
		 * we update the state to CS_S11_COMPLETE the initiator will
		 * no longer be in an appropriate state.
		 */
		event_ctx->iec_pdu_forwarded = B_TRUE;
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		idm_pdu_rx_forward(ic, pdu);
		/* FALLTHROUGH */
	case CE_LOGOUT_SESSION_SUCCESS:
		/* T13 */

		/* Close connection (if it's not already closed) */
		if (IDM_CONN_ISTGT(ic)) {
			ic->ic_transport_ops->it_tgt_conn_disconnect(ic);
		} else {
			ic->ic_transport_ops->it_ini_conn_disconnect(ic);
		}

		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;
	case CE_ASYNC_LOGOUT_RCV:
		/* T14 Do nothing */
		break;
	case CE_TRANSPORT_FAIL:
	case CE_ASYNC_DROP_CONN_RCV:
	case CE_ASYNC_DROP_CONN_SND:
	case CE_ASYNC_DROP_ALL_CONN_RCV:
	case CE_ASYNC_DROP_ALL_CONN_SND:
	case CE_LOGOUT_FAIL_RCV:
		idm_update_state(ic, CS_S8_CLEANUP, event_ctx);
		break;
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
	case CE_MISC_TX:
	case CE_MISC_RX:
	case CE_LOGIN_TIMEOUT:
		/* Don't care */
		break;
	default:
		ASSERT(0);
	}
}


static void
idm_logout_req_timeout(void *arg)
{
	idm_conn_t *ic = arg;

	ic->ic_state_timeout = 0;
	idm_conn_event(ic, CE_LOGOUT_TIMEOUT, (uintptr_t)NULL);
}

static void
idm_state_s7_logout_req(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	/* Must cancel logout timer before leaving this state */
	switch (event_ctx->iec_event) {
	case CE_LOGOUT_THIS_CONN_RCV:
	case CE_LOGOUT_THIS_CONN_SND:
	case CE_LOGOUT_OTHER_CONN_RCV:
	case CE_LOGOUT_OTHER_CONN_SND:
		/* T10 */
		if (IDM_CONN_ISTGT(ic)) {
			IDM_SM_TIMER_CLEAR(ic);
		}
		idm_ffp_disable(ic, FD_CONN_LOGOUT); /* Explicit logout */
		idm_update_state(ic, CS_S6_IN_LOGOUT, event_ctx);
		break;
	case CE_LOGOUT_SESSION_RCV:
	case CE_LOGOUT_SESSION_SND:
		/* T10 */
		if (IDM_CONN_ISTGT(ic)) {
			IDM_SM_TIMER_CLEAR(ic);
		}
		idm_ffp_disable(ic, FD_SESS_LOGOUT); /* Explicit logout */
		idm_update_state(ic, CS_S6_IN_LOGOUT, event_ctx);
		break;
	case CE_ASYNC_LOGOUT_RCV:
	case CE_ASYNC_LOGOUT_SND:
		/* T12 Do nothing */
		break;
	case CE_TRANSPORT_FAIL:
	case CE_ASYNC_DROP_CONN_RCV:
	case CE_ASYNC_DROP_CONN_SND:
	case CE_ASYNC_DROP_ALL_CONN_RCV:
	case CE_ASYNC_DROP_ALL_CONN_SND:
		/* T16 */
		if (IDM_CONN_ISTGT(ic)) {
			IDM_SM_TIMER_CLEAR(ic);
		}
		/* FALLTHROUGH */
	case CE_LOGOUT_TIMEOUT:
		idm_ffp_disable(ic, FD_CONN_FAIL); /* Implicit logout */
		idm_update_state(ic, CS_S8_CLEANUP, event_ctx);
		break;
	case CE_LOGOUT_SESSION_SUCCESS:
		/* T18 */
		if (IDM_CONN_ISTGT(ic)) {
			IDM_SM_TIMER_CLEAR(ic);
		}
		idm_ffp_disable(ic, FD_SESS_LOGOUT); /* Explicit logout */

		/* Close connection (if it's not already closed) */
		if (IDM_CONN_ISTGT(ic)) {
			ic->ic_transport_ops->it_tgt_conn_disconnect(ic);
		} else {
			ic->ic_transport_ops->it_ini_conn_disconnect(ic);
		}

		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
	case CE_MISC_TX:
	case CE_MISC_RX:
	case CE_LOGIN_TIMEOUT:
		/* Don't care */
		break;
	default:
		ASSERT(0);
	}
}


static void
idm_cleanup_timeout(void *arg)
{
	idm_conn_t *ic = arg;

	ic->ic_state_timeout = 0;
	idm_conn_event(ic, CE_CLEANUP_TIMEOUT, (uintptr_t)NULL);
}

static void
idm_state_s8_cleanup(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t *pdu;

	/*
	 * Need to cancel the cleanup timeout before leaving this state
	 * if it hasn't already fired.
	 */
	switch (event_ctx->iec_event) {
	case CE_LOGOUT_SUCCESS_RCV:
	case CE_LOGOUT_SUCCESS_SND:
	case CE_LOGOUT_SESSION_SUCCESS:
		IDM_SM_TIMER_CLEAR(ic);
		/*FALLTHROUGH*/
	case CE_CLEANUP_TIMEOUT:
		/* M1 */
		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;
	case CE_LOGOUT_OTHER_CONN_RCV:
	case CE_LOGOUT_OTHER_CONN_SND:
		/* M2 */
		idm_update_state(ic, CS_S10_IN_CLEANUP, event_ctx);
		break;
	case CE_LOGOUT_SUCCESS_SND_DONE:
	case CE_LOGOUT_FAIL_SND_DONE:
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		/* restore client callback */
		pdu->isp_callback =  ic->ic_client_callback;
		ic->ic_client_callback = NULL;
		idm_pdu_complete(pdu, pdu->isp_status);
		break;
	case CE_LOGOUT_SESSION_RCV:
	case CE_LOGOUT_SESSION_SND:
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
	case CE_MISC_TX:
	case CE_MISC_RX:
	case CE_TRANSPORT_FAIL:
	case CE_LOGIN_TIMEOUT:
	case CE_LOGOUT_TIMEOUT:
		/* Don't care */
		break;
	default:
		ASSERT(0);
	}
}

/* ARGSUSED */
static void
idm_state_s9_init_error(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	/* All events ignored in this state */
}

/* ARGSUSED */
static void
idm_state_s9a_rejected(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	/* All events ignored in this state */
}


static void
idm_state_s9b_wait_snd_done_cb(idm_pdu_t *pdu, idm_status_t status)
{
	idm_conn_t		*ic = pdu->isp_ic;

	/*
	 * This pdu callback can be invoked by the tx thread,
	 * so run the disconnect code from another thread.
	 */
	pdu->isp_status = status;
	idm_conn_event(ic, CE_LOGIN_FAIL_SND_DONE, (uintptr_t)pdu);
}

/*
 * CS_S9B_WAIT_SND_DONE -- wait for callback completion.
 */
/* ARGSUSED */
static void
idm_state_s9b_wait_snd_done(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t *pdu;
	/*
	 * Wait for completion of the login fail sequence and then
	 * go to state S9_INIT_ERROR to clean up the connection.
	 */
	switch (event_ctx->iec_event) {
	case CE_LOGIN_FAIL_SND_DONE:
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		/* restore client callback */
		pdu->isp_callback =  ic->ic_client_callback;
		ic->ic_client_callback = NULL;
		idm_pdu_complete(pdu, pdu->isp_status);
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;

	/* All other events ignored */
	}
}




static void
idm_state_s10_in_cleanup(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t *pdu;

	/*
	 * Need to cancel the cleanup timeout before leaving this state
	 * if it hasn't already fired.
	 */
	switch (event_ctx->iec_event) {
	case CE_LOGOUT_FAIL_RCV:
	case CE_LOGOUT_FAIL_SND:
		idm_update_state(ic, CS_S8_CLEANUP, event_ctx);
		break;
	case CE_LOGOUT_SUCCESS_SND:
	case CE_LOGOUT_SUCCESS_RCV:
	case CE_LOGOUT_SESSION_SUCCESS:
		IDM_SM_TIMER_CLEAR(ic);
		/*FALLTHROUGH*/
	case CE_CLEANUP_TIMEOUT:
		idm_update_state(ic, CS_S11_COMPLETE, event_ctx);
		break;
	case CE_LOGOUT_SUCCESS_SND_DONE:
	case CE_LOGOUT_FAIL_SND_DONE:
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		/* restore client callback */
		pdu->isp_callback =  ic->ic_client_callback;
		ic->ic_client_callback = NULL;
		idm_pdu_complete(pdu, pdu->isp_status);
		break;
	case CE_TX_PROTOCOL_ERROR:
	case CE_RX_PROTOCOL_ERROR:
	case CE_MISC_TX:
	case CE_MISC_RX:
	case CE_LOGIN_TIMEOUT:
	case CE_LOGOUT_TIMEOUT:
		/* Don't care */
		break;
	default:
		ASSERT(0);
	}
}

/* ARGSUSED */
static void
idm_state_s11_complete(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t *pdu;

	/*
	 * Cleanup logout success/fail completion if it's been delayed
	 * until now.
	 *
	 * All new events are filtered out before reaching this state, but
	 * there might already be events in the event queue, so handle the
	 * SND_DONE events here. Note that if either of the following
	 * SND_DONE events happens AFTER the change to state S11, then the
	 * event filter inside dm_conn_event_locked does enough cleanup.
	 */
	switch (event_ctx->iec_event) {
	case CE_LOGOUT_SUCCESS_SND_DONE:
	case CE_LOGOUT_FAIL_SND_DONE:
		pdu = (idm_pdu_t *)event_ctx->iec_info;
		/* restore client callback */
		pdu->isp_callback =  ic->ic_client_callback;
		ic->ic_client_callback = NULL;
		idm_pdu_complete(pdu, pdu->isp_status);
		break;
	}

}

static void
idm_state_s12_enable_dm(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	switch (event_ctx->iec_event) {
	case CE_ENABLE_DM_SUCCESS:
		/* T20 */
		idm_update_state(ic, CS_S5_LOGGED_IN, event_ctx);
		break;
	case CE_ENABLE_DM_FAIL:
		/* T21 */
		idm_update_state(ic, CS_S9_INIT_ERROR, event_ctx);
		break;
	case CE_TRANSPORT_FAIL:
		/*
		 * We expect to always hear back from the transport layer
		 * once we have an "enable data-mover" request outstanding.
		 * Therefore we'll ignore other events that may occur even
		 * when they clearly indicate a problem and wait for
		 * CE_ENABLE_DM_FAIL.  On a related note this means the
		 * transport must ensure that it eventually completes the
		 * "enable data-mover" operation with either success or
		 * failure -- otherwise we'll be stuck here.
		 */
		break;
	default:
		ASSERT(0);
		break;
	}
}

static void
idm_update_state(idm_conn_t *ic, idm_conn_state_t new_state,
    idm_conn_event_ctx_t *event_ctx)
{
	int rc;
	idm_status_t idm_status;

	/*
	 * Validate new state
	 */
	ASSERT(new_state != CS_S0_UNDEFINED);
	ASSERT3U(new_state, <, CS_MAX_STATE);

	/*
	 * Update state in context.  We protect this with a mutex
	 * even though the state machine code is single threaded so that
	 * other threads can check the state value atomically.
	 */
	new_state = (new_state < CS_MAX_STATE) ?
	    new_state : CS_S0_UNDEFINED;

	IDM_SM_LOG(CE_NOTE, "idm_update_state: conn %p, evt %s(%d), "
	    "%s(%d) --> %s(%d)", (void *)ic,
	    idm_ce_name[event_ctx->iec_event], event_ctx->iec_event,
	    idm_cs_name[ic->ic_state], ic->ic_state,
	    idm_cs_name[new_state], new_state);

	DTRACE_PROBE2(conn__state__change,
	    idm_conn_t *, ic, idm_conn_state_t, new_state);

	mutex_enter(&ic->ic_state_mutex);
	idm_sm_audit_state_change(&ic->ic_state_audit, SAS_IDM_CONN,
	    (int)ic->ic_state, (int)new_state);
	ic->ic_last_state = ic->ic_state;
	ic->ic_state = new_state;
	cv_signal(&ic->ic_state_cv);
	mutex_exit(&ic->ic_state_mutex);

	switch (ic->ic_state) {
	case CS_S1_FREE:
		ASSERT(0); /* Initial state, can't return */
		break;
	case CS_S2_XPT_WAIT:
		if ((rc = idm_ini_conn_finish(ic)) != 0) {
			idm_conn_event(ic, CE_CONNECT_FAIL, (uintptr_t)NULL);
		} else {
			idm_conn_event(ic, CE_CONNECT_SUCCESS, (uintptr_t)NULL);
		}
		break;
	case CS_S3_XPT_UP:
		/*
		 * Finish any connection related setup including
		 * waking up the idm_tgt_conn_accept thread.
		 * and starting the login timer.  If the function
		 * fails then we return to "free" state.
		 */
		if ((rc = idm_tgt_conn_finish(ic)) != IDM_STATUS_SUCCESS) {
			switch (rc) {
			case IDM_STATUS_REJECT:
				idm_conn_event(ic, CE_CONNECT_REJECT,
				    (uintptr_t)NULL);
				break;
			default:
				idm_conn_event(ic, CE_CONNECT_FAIL,
				    (uintptr_t)NULL);
				break;
			}
		}

		/*
		 * First login received will cause a transition to
		 * CS_S4_IN_LOGIN.  Start login timer.
		 */
		IDM_SM_TIMER_CHECK(ic);
		ic->ic_state_timeout = timeout(idm_login_timeout, ic,
		    drv_usectohz(IDM_LOGIN_SECONDS * 1000000));
		break;
	case CS_S4_IN_LOGIN:
		if (ic->ic_conn_type == CONN_TYPE_INI) {
			(void) idm_notify_client(ic, CN_READY_FOR_LOGIN,
			    (uintptr_t)NULL);
			mutex_enter(&ic->ic_state_mutex);
			ic->ic_state_flags |= CF_LOGIN_READY;
			cv_signal(&ic->ic_state_cv);
			mutex_exit(&ic->ic_state_mutex);
		}
		break;
	case CS_S5_LOGGED_IN:
		ASSERT(!ic->ic_ffp);
		/*
		 * IDM can go to FFP before the initiator but it
		 * needs to go to FFP after the target (IDM target should
		 * go to FFP after notify_ack).
		 */
		idm_status = idm_ffp_enable(ic);
		if (idm_status != IDM_STATUS_SUCCESS) {
			idm_conn_event(ic, CE_TRANSPORT_FAIL, (uintptr_t)NULL);
		}

		if (ic->ic_reinstate_conn) {
			/* Connection reinstatement is complete */
			idm_conn_event(ic->ic_reinstate_conn,
			    CE_CONN_REINSTATE_SUCCESS, (uintptr_t)NULL);
		}
		break;
	case CS_S6_IN_LOGOUT:
		break;
	case CS_S7_LOGOUT_REQ:
		/* Start logout timer for target connections */
		if (IDM_CONN_ISTGT(ic)) {
			IDM_SM_TIMER_CHECK(ic);
			ic->ic_state_timeout = timeout(idm_logout_req_timeout,
			    ic, drv_usectohz(IDM_LOGOUT_SECONDS*1000000));
		}
		break;
	case CS_S8_CLEANUP:
		/* Close connection (if it's not already closed) */
		if (IDM_CONN_ISTGT(ic)) {
			ic->ic_transport_ops->it_tgt_conn_disconnect(ic);
		} else {
			ic->ic_transport_ops->it_ini_conn_disconnect(ic);
		}

		/* Stop executing active tasks */
		(void) idm_task_abort(ic, NULL, AT_INTERNAL_SUSPEND);

		/* Start logout timer */
		IDM_SM_TIMER_CHECK(ic);
		ic->ic_state_timeout = timeout(idm_cleanup_timeout, ic,
		    drv_usectohz(IDM_CLEANUP_SECONDS*1000000));
		break;
	case CS_S10_IN_CLEANUP:
		break;
	case CS_S9A_REJECTED:
		/*
		 * We never finished establishing the connection so no
		 * disconnect.  No client notifications because the client
		 * rejected the connection.
		 */
		idm_refcnt_async_wait_ref(&ic->ic_refcnt,
		    &idm_conn_reject_unref);
		break;
	case CS_S9B_WAIT_SND_DONE:
		break;
	case CS_S9_INIT_ERROR:
		if (IDM_CONN_ISTGT(ic)) {
			ic->ic_transport_ops->it_tgt_conn_disconnect(ic);
		} else {
			mutex_enter(&ic->ic_state_mutex);
			ic->ic_state_flags |= CF_ERROR;
			ic->ic_conn_sm_status = IDM_STATUS_FAIL;
			cv_signal(&ic->ic_state_cv);
			mutex_exit(&ic->ic_state_mutex);
			if (ic->ic_last_state != CS_S1_FREE &&
			    ic->ic_last_state != CS_S2_XPT_WAIT) {
				ic->ic_transport_ops->it_ini_conn_disconnect(
				    ic);
			} else {
				(void) idm_notify_client(ic, CN_CONNECT_FAIL,
				    (uintptr_t)NULL);
			}
		}
		/*FALLTHROUGH*/
	case CS_S11_COMPLETE:
		/*
		 * No more traffic on this connection.  If this is an
		 * initiator connection and we weren't connected yet
		 * then don't send the "connect lost" event.
		 * It's useful to the initiator to know whether we were
		 * logging in at the time so send that information in the
		 * data field.
		 */
		if (IDM_CONN_ISTGT(ic) ||
		    ((ic->ic_last_state != CS_S1_FREE) &&
		    (ic->ic_last_state != CS_S2_XPT_WAIT))) {
			(void) idm_notify_client(ic, CN_CONNECT_LOST,
			    (uintptr_t)(ic->ic_last_state == CS_S4_IN_LOGIN));
		}

		/* Abort all tasks */
		(void) idm_task_abort(ic, NULL, AT_INTERNAL_ABORT);

		/*
		 * Handle terminal state actions on the global taskq so
		 * we can clean up all the connection resources from
		 * a separate thread context.
		 */
		idm_refcnt_async_wait_ref(&ic->ic_refcnt, &idm_conn_unref);
		break;
	case CS_S12_ENABLE_DM:

		/*
		 * The Enable DM state indicates the initiator to initiate
		 * the hello sequence and the target to get ready to accept
		 * the iSER Hello Message.
		 */
		idm_status = (IDM_CONN_ISINI(ic)) ?
		    ic->ic_transport_ops->it_ini_enable_datamover(ic) :
		    ic->ic_transport_ops->it_tgt_enable_datamover(ic);

		if (idm_status == IDM_STATUS_SUCCESS) {
			idm_conn_event(ic, CE_ENABLE_DM_SUCCESS,
			    (uintptr_t)NULL);
		} else {
			idm_conn_event(ic, CE_ENABLE_DM_FAIL, (uintptr_t)NULL);
		}

		break;

	default:
		ASSERT(0);
		break;

	}
}


static void
idm_conn_unref(void *ic_void)
{
	idm_conn_t *ic = ic_void;

	/*
	 * Client should not be notified that the connection is destroyed
	 * until all references on the idm connection have been removed.
	 * Otherwise references on the associated client context would need
	 * to be tracked separately which seems like a waste (at least when
	 * there is a one for one correspondence with references on the
	 * IDM connection).
	 */
	if (IDM_CONN_ISTGT(ic)) {
		(void) idm_notify_client(ic, CN_CONNECT_DESTROY,
		    (uintptr_t)NULL);
		idm_svc_conn_destroy(ic);
	} else {
		/* Initiator may destroy connection during this call */
		(void) idm_notify_client(ic, CN_CONNECT_DESTROY,
		    (uintptr_t)NULL);
	}
}

static void
idm_conn_reject_unref(void *ic_void)
{
	idm_conn_t *ic = ic_void;

	ASSERT(IDM_CONN_ISTGT(ic));

	/* Don't notify the client since it rejected the connection */
	idm_svc_conn_destroy(ic);
}



static idm_pdu_event_action_t
idm_conn_sm_validate_pdu(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx,
    idm_pdu_t *pdu)
{
	char			*reason_string;
	idm_pdu_event_action_t	action;

	ASSERT((event_ctx->iec_pdu_event_type == CT_RX_PDU) ||
	    (event_ctx->iec_pdu_event_type == CT_TX_PDU));

	/*
	 * Let's check the simple stuff first.  Make sure if this is a
	 * target connection that the PDU is appropriate for a target
	 * and if this is an initiator connection that the PDU is
	 * appropriate for an initiator.  This code is not in the data
	 * path so organization is more important than performance.
	 */
	switch (IDM_PDU_OPCODE(pdu)) {
	case ISCSI_OP_NOOP_OUT:
	case ISCSI_OP_SCSI_CMD:
	case ISCSI_OP_SCSI_TASK_MGT_MSG:
	case ISCSI_OP_LOGIN_CMD:
	case ISCSI_OP_TEXT_CMD:
	case ISCSI_OP_SCSI_DATA:
	case ISCSI_OP_LOGOUT_CMD:
	case ISCSI_OP_SNACK_CMD:
		/*
		 * Only the initiator should send these PDU's and
		 * only the target should receive them.
		 */
		if (IDM_CONN_ISINI(ic) &&
		    (event_ctx->iec_pdu_event_type == CT_RX_PDU)) {
			reason_string = "Invalid RX PDU for initiator";
			action = CA_RX_PROTOCOL_ERROR;
			goto validate_pdu_done;
		}

		if (IDM_CONN_ISTGT(ic) &&
		    (event_ctx->iec_pdu_event_type == CT_TX_PDU)) {
			reason_string = "Invalid TX PDU for target";
			action = CA_TX_PROTOCOL_ERROR;
			goto validate_pdu_done;
		}
		break;
	case ISCSI_OP_NOOP_IN:
	case ISCSI_OP_SCSI_RSP:
	case ISCSI_OP_SCSI_TASK_MGT_RSP:
	case ISCSI_OP_LOGIN_RSP:
	case ISCSI_OP_TEXT_RSP:
	case ISCSI_OP_SCSI_DATA_RSP:
	case ISCSI_OP_LOGOUT_RSP:
	case ISCSI_OP_RTT_RSP:
	case ISCSI_OP_ASYNC_EVENT:
	case ISCSI_OP_REJECT_MSG:
		/*
		 * Only the target should send these PDU's and
		 * only the initiator should receive them.
		 */
		if (IDM_CONN_ISTGT(ic) &&
		    (event_ctx->iec_pdu_event_type == CT_RX_PDU)) {
			reason_string = "Invalid RX PDU for target";
			action = CA_RX_PROTOCOL_ERROR;
			goto validate_pdu_done;
		}

		if (IDM_CONN_ISINI(ic) &&
		    (event_ctx->iec_pdu_event_type == CT_TX_PDU)) {
			reason_string = "Invalid TX PDU for initiator";
			action = CA_TX_PROTOCOL_ERROR;
			goto validate_pdu_done;
		}
		break;
	default:
		reason_string = "Unknown PDU Type";
		action = ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ?
		    CA_TX_PROTOCOL_ERROR : CA_RX_PROTOCOL_ERROR);
		goto validate_pdu_done;
	}

	/*
	 * Now validate the opcodes against the current state.
	 */
	reason_string = "PDU not allowed in current state";
	switch (IDM_PDU_OPCODE(pdu)) {
	case ISCSI_OP_NOOP_OUT:
	case ISCSI_OP_NOOP_IN:
		/*
		 * Obviously S1-S3 are not allowed since login hasn't started.
		 * S8 is probably out as well since the connection has been
		 * dropped.
		 */
		switch (ic->ic_state) {
		case CS_S4_IN_LOGIN:
		case CS_S5_LOGGED_IN:
		case CS_S6_IN_LOGOUT:
		case CS_S7_LOGOUT_REQ:
			action = CA_FORWARD;
			goto validate_pdu_done;
		case CS_S8_CLEANUP:
		case CS_S10_IN_CLEANUP:
			action = CA_DROP;
			goto validate_pdu_done;
		default:
			action = ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ?
			    CA_TX_PROTOCOL_ERROR : CA_RX_PROTOCOL_ERROR);
			goto validate_pdu_done;
		}
		/*NOTREACHED*/
	case ISCSI_OP_SCSI_CMD:
	case ISCSI_OP_SCSI_RSP:
	case ISCSI_OP_SCSI_TASK_MGT_MSG:
	case ISCSI_OP_SCSI_TASK_MGT_RSP:
	case ISCSI_OP_SCSI_DATA:
	case ISCSI_OP_SCSI_DATA_RSP:
	case ISCSI_OP_RTT_RSP:
	case ISCSI_OP_SNACK_CMD:
	case ISCSI_OP_TEXT_CMD:
	case ISCSI_OP_TEXT_RSP:
		switch (ic->ic_state) {
		case CS_S5_LOGGED_IN:
		case CS_S6_IN_LOGOUT:
		case CS_S7_LOGOUT_REQ:
			action = CA_FORWARD;
			goto validate_pdu_done;
		case CS_S8_CLEANUP:
		case CS_S10_IN_CLEANUP:
			action = CA_DROP;
			goto validate_pdu_done;
		default:
			action = ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ?
			    CA_TX_PROTOCOL_ERROR : CA_RX_PROTOCOL_ERROR);
			goto validate_pdu_done;
		}
		/*NOTREACHED*/
	case ISCSI_OP_LOGOUT_CMD:
	case ISCSI_OP_LOGOUT_RSP:
	case ISCSI_OP_REJECT_MSG:
	case ISCSI_OP_ASYNC_EVENT:
		switch (ic->ic_state) {
		case CS_S5_LOGGED_IN:
		case CS_S6_IN_LOGOUT:
		case CS_S7_LOGOUT_REQ:
			action = CA_FORWARD;
			goto validate_pdu_done;
		case CS_S8_CLEANUP:
		case CS_S10_IN_CLEANUP:
			action = CA_DROP;
			goto validate_pdu_done;
		default:
			action = ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ?
			    CA_TX_PROTOCOL_ERROR : CA_RX_PROTOCOL_ERROR);
			goto validate_pdu_done;
		}
		/*NOTREACHED*/
	case ISCSI_OP_LOGIN_CMD:
	case ISCSI_OP_LOGIN_RSP:
		switch (ic->ic_state) {
		case CS_S3_XPT_UP:
		case CS_S4_IN_LOGIN:
			action = CA_FORWARD;
			goto validate_pdu_done;
		default:
			action = ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ?
			    CA_TX_PROTOCOL_ERROR : CA_RX_PROTOCOL_ERROR);
			goto validate_pdu_done;
		}
		/*NOTREACHED*/
	default:
		/* This should never happen -- we already checked above */
		ASSERT(0);
		/*NOTREACHED*/
	}

	action = ((event_ctx->iec_pdu_event_type == CT_TX_PDU) ?
	    CA_TX_PROTOCOL_ERROR : CA_RX_PROTOCOL_ERROR);

validate_pdu_done:
	if (action != CA_FORWARD) {
		DTRACE_PROBE2(idm__int__protocol__error,
		    idm_conn_event_ctx_t *, event_ctx,
		    char *, reason_string);
	}

	return (action);
}

/* ARGSUSED */
void
idm_pdu_tx_protocol_error(idm_conn_t *ic, idm_pdu_t *pdu)
{
	/*
	 * Return the PDU to the caller indicating it was a protocol error.
	 * Caller can take appropriate action.
	 */
	idm_pdu_complete(pdu, IDM_STATUS_PROTOCOL_ERROR);
}

void
idm_pdu_rx_protocol_error(idm_conn_t *ic, idm_pdu_t *pdu)
{
	/*
	 * Forward PDU to caller indicating it is a protocol error.
	 * Caller should take appropriate action.
	 */
	(*ic->ic_conn_ops.icb_rx_error)(ic, pdu, IDM_STATUS_PROTOCOL_ERROR);
}

idm_status_t
idm_notify_client(idm_conn_t *ic, idm_client_notify_t cn, uintptr_t data)
{
	/*
	 * We may want to make this more complicated at some point but
	 * for now lets just call the client's notify function and return
	 * the status.
	 */
	ASSERT(!mutex_owned(&ic->ic_state_mutex));
	cn = (cn > CN_MAX) ? CN_MAX : cn;
	IDM_SM_LOG(CE_NOTE, "idm_notify_client: ic=%p %s(%d)\n",
	    (void *)ic, idm_cn_strings[cn], cn);
	return ((*ic->ic_conn_ops.icb_client_notify)(ic, cn, data));
}

static idm_status_t
idm_ffp_enable(idm_conn_t *ic)
{
	idm_status_t rc;

	/*
	 * On the initiator side the client will see this notification
	 * before the actual login succes PDU.  This shouldn't be a big
	 * deal since the initiator drives the connection.  It can simply
	 * wait for the login response then start sending SCSI commands.
	 * Kind ugly though compared with the way things work on target
	 * connections.
	 */
	mutex_enter(&ic->ic_state_mutex);
	ic->ic_ffp = B_TRUE;
	mutex_exit(&ic->ic_state_mutex);

	rc = idm_notify_client(ic, CN_FFP_ENABLED, (uintptr_t)NULL);
	if (rc != IDM_STATUS_SUCCESS) {
		mutex_enter(&ic->ic_state_mutex);
		ic->ic_ffp = B_FALSE;
		mutex_exit(&ic->ic_state_mutex);
	}
	return (rc);
}

static void
idm_ffp_disable(idm_conn_t *ic, idm_ffp_disable_t disable_type)
{
	mutex_enter(&ic->ic_state_mutex);
	ic->ic_ffp = B_FALSE;
	mutex_exit(&ic->ic_state_mutex);

	/* Client can't "fail" CN_FFP_DISABLED */
	(void) idm_notify_client(ic, CN_FFP_DISABLED,
	    (uintptr_t)disable_type);
}

static void
idm_initial_login_actions(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	ASSERT((event_ctx->iec_event == CE_LOGIN_RCV) ||
	    (event_ctx->iec_event == CE_LOGIN_SND));

	/*
	 * Currently it's not clear what we would do here -- since
	 * we went to the trouble of coding an "initial login" hook
	 * we'll leave it in for now.  Remove before integration if
	 * it's not used for anything.
	 */
	ic->ic_state_flags |= CF_INITIAL_LOGIN;
}

static void
idm_login_success_actions(idm_conn_t *ic, idm_conn_event_ctx_t *event_ctx)
{
	idm_pdu_t		*pdu = (idm_pdu_t *)event_ctx->iec_info;
	iscsi_login_hdr_t	*login_req =
	    (iscsi_login_hdr_t *)pdu->isp_hdr;

	ASSERT((event_ctx->iec_event == CE_LOGIN_SUCCESS_RCV) ||
	    (event_ctx->iec_event == CE_LOGIN_SUCCESS_SND));

	/*
	 * Save off CID
	 */
	mutex_enter(&ic->ic_state_mutex);
	ic->ic_login_cid = ntohs(login_req->cid);
	ic->ic_login_info_valid =  B_TRUE;

	mutex_exit(&ic->ic_state_mutex);
}