aboutsummaryrefslogblamecommitdiff
path: root/sys/dev/ocs_fc/ocs_domain.c
blob: fe5489343d68d14f2d8b2cd99b525926b4450265 (plain) (tree)
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
















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































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

/**
 * @file
 * Handles the domain object callback from the HW.
 */

/*!
@defgroup domain_sm Domain State Machine: States
*/

#include "ocs.h"

#include "ocs_fabric.h"
#include "ocs_device.h"

#define domain_sm_trace(domain)  \
	do { \
		if (OCS_LOG_ENABLE_DOMAIN_SM_TRACE(domain->ocs)) \
			ocs_log_info(domain->ocs, "[domain] %-20s %-20s\n", __func__, ocs_sm_event_name(evt)); \
	} while (0)

#define domain_trace(domain, fmt, ...) \
	do { \
		if (OCS_LOG_ENABLE_DOMAIN_SM_TRACE(domain ? domain->ocs : NULL)) \
			ocs_log_info(domain ? domain->ocs : NULL, fmt, ##__VA_ARGS__); \
	} while (0)

#define domain_printf(domain, fmt, ...) \
	do { \
		ocs_log_info(domain ? domain->ocs : NULL, fmt, ##__VA_ARGS__); \
	} while (0)

void ocs_mgmt_domain_list(ocs_textbuf_t *textbuf, void *domain);
void ocs_mgmt_domain_get_all(ocs_textbuf_t *textbuf, void *domain);
int ocs_mgmt_domain_get(ocs_textbuf_t *textbuf, char *parent, char *name, void *domain);
int ocs_mgmt_domain_set(char *parent, char *name, char *value, void *domain);
int ocs_mgmt_domain_exec(char *parent, char *action, void *arg_in, uint32_t arg_in_length,
		void *arg_out, uint32_t arg_out_length, void *domain);

static ocs_mgmt_functions_t domain_mgmt_functions = {
	.get_list_handler = ocs_mgmt_domain_list,
	.get_handler = ocs_mgmt_domain_get,
	.get_all_handler = ocs_mgmt_domain_get_all,
	.set_handler = ocs_mgmt_domain_set,
	.exec_handler = ocs_mgmt_domain_exec,
};



/**
 * @brief Accept domain callback events from the HW.
 *
 * <h3 class="desc">Description</h3>
 * HW calls this function with various domain-related events.
 *
 * @param arg Application-specified argument.
 * @param event Domain event.
 * @param data Event specific data.
 *
 * @return Returns 0 on success; or a negative error value on failure.
 */

int32_t
ocs_domain_cb(void *arg, ocs_hw_domain_event_e event, void *data)
{
	ocs_t *ocs = arg;
	ocs_domain_t *domain = NULL;
	int32_t rc = 0;

	ocs_assert(data, -1);

	if (event != OCS_HW_DOMAIN_FOUND) {
		domain = data;
	}

	switch (event) {
	case OCS_HW_DOMAIN_FOUND: {
		uint64_t fcf_wwn = 0;
		ocs_domain_record_t *drec = data;
		ocs_assert(drec, -1);

		/* extract the fcf_wwn */
		fcf_wwn = ocs_be64toh(*((uint64_t*)drec->wwn));

		/* lookup domain, or allocate a new one if one doesn't exist already */
		domain = ocs_domain_find(ocs, fcf_wwn);
		if (domain == NULL) {
			domain = ocs_domain_alloc(ocs, fcf_wwn);
			if (domain == NULL) {
				ocs_log_err(ocs, "ocs_domain_alloc() failed\n");
				rc = -1;
				break;
			}
			ocs_sm_transition(&domain->drvsm, __ocs_domain_init, NULL);
		}
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_FOUND, drec);
		break;
	}

	case OCS_HW_DOMAIN_LOST:
		domain_trace(domain, "OCS_HW_DOMAIN_LOST:\n");
		ocs_domain_hold_frames(domain);
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_LOST, NULL);
		break;

	case OCS_HW_DOMAIN_ALLOC_OK: {
		domain_trace(domain, "OCS_HW_DOMAIN_ALLOC_OK:\n");
		domain->instance_index = 0;
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_ALLOC_OK, NULL);
		break;
	}

	case OCS_HW_DOMAIN_ALLOC_FAIL:
		domain_trace(domain, "OCS_HW_DOMAIN_ALLOC_FAIL:\n");
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_ALLOC_FAIL, NULL);
		break;

	case OCS_HW_DOMAIN_ATTACH_OK:
		domain_trace(domain, "OCS_HW_DOMAIN_ATTACH_OK:\n");
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_ATTACH_OK, NULL);
		break;

	case OCS_HW_DOMAIN_ATTACH_FAIL:
		domain_trace(domain, "OCS_HW_DOMAIN_ATTACH_FAIL:\n");
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_ATTACH_FAIL, NULL);
		break;

	case OCS_HW_DOMAIN_FREE_OK:
		domain_trace(domain, "OCS_HW_DOMAIN_FREE_OK:\n");
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_FREE_OK, NULL);
		break;

	case OCS_HW_DOMAIN_FREE_FAIL:
		domain_trace(domain, "OCS_HW_DOMAIN_FREE_FAIL:\n");
		ocs_domain_post_event(domain, OCS_EVT_DOMAIN_FREE_FAIL, NULL);
		break;

	default:
		ocs_log_warn(ocs, "unsupported event %#x\n", event);
	}

	return rc;
}


/**
 * @brief Find the domain, given its FCF_WWN.
 *
 * <h3 class="desc">Description</h3>
 * Search the domain_list to find a matching domain object.
 *
 * @param ocs Pointer to the OCS device.
 * @param fcf_wwn FCF WWN to find.
 *
 * @return Returns the pointer to the domain if found; or NULL otherwise.
 */

ocs_domain_t *
ocs_domain_find(ocs_t *ocs, uint64_t fcf_wwn)
{
	ocs_domain_t *domain = NULL;

	/* Check to see if this domain is already allocated */
	ocs_device_lock(ocs);
		ocs_list_foreach(&ocs->domain_list, domain) {
			if (fcf_wwn == domain->fcf_wwn) {
				break;
			}
		}
	ocs_device_unlock(ocs);
	return domain;
}

/**
 * @brief Allocate a domain object.
 *
 * <h3 class="desc">Description</h3>
 * A domain object is allocated and initialized. It is associated with the
 * \c ocs argument.
 *
 * @param ocs Pointer to the OCS device.
 * @param fcf_wwn FCF WWN of the domain.
 *
 * @return Returns a pointer to the ocs_domain_t object; or NULL.
 */

ocs_domain_t *
ocs_domain_alloc(ocs_t *ocs, uint64_t fcf_wwn)
{
	ocs_domain_t *domain;

	ocs_assert(ocs, NULL);

	domain = ocs_malloc(ocs, sizeof(*domain), OCS_M_NOWAIT | OCS_M_ZERO);
	if (domain) {

		domain->ocs = ocs;
		domain->instance_index = ocs->domain_instance_count++;
		domain->drvsm.app = domain;
		ocs_domain_lock_init(domain);
		ocs_lock_init(ocs, &domain->lookup_lock, "Domain lookup[%d]", domain->instance_index);

		/* Allocate a sparse vector for sport FC_ID's */
		domain->lookup = spv_new(ocs);
		if (domain->lookup == NULL) {
			ocs_log_err(ocs, "spv_new() failed\n");
			ocs_free(ocs, domain, sizeof(*domain));
			return NULL;
		}

		ocs_list_init(&domain->sport_list, ocs_sport_t, link);
		domain->fcf_wwn = fcf_wwn;
		ocs_log_debug(ocs, "Domain allocated: wwn %016" PRIX64 "\n", domain->fcf_wwn);
		domain->femul_enable = (ocs->ctrlmask & OCS_CTRLMASK_ENABLE_FABRIC_EMULATION) != 0;

		ocs_device_lock(ocs);
			/* if this is the first domain, then assign it as the "root" domain */
			if (ocs_list_empty(&ocs->domain_list)) {
				ocs->domain = domain;
			}
			ocs_list_add_tail(&ocs->domain_list, domain);
		ocs_device_unlock(ocs);

		domain->mgmt_functions = &domain_mgmt_functions;
	} else {
		ocs_log_err(ocs, "domain allocation failed\n");
	}


	return domain;
}

/**
 * @brief Free a domain object.
 *
 * <h3 class="desc">Description</h3>
 * The domain object is freed.
 *
 * @param domain Domain object to free.
 *
 * @return None.
 */

void
ocs_domain_free(ocs_domain_t *domain)
{
	ocs_t *ocs;

	ocs_assert(domain);
	ocs_assert(domain->ocs);

	/* Hold frames to clear the domain pointer from the xport lookup */
	ocs_domain_hold_frames(domain);

	ocs = domain->ocs;

	ocs_log_debug(ocs, "Domain free: wwn %016" PRIX64 "\n", domain->fcf_wwn);

	spv_del(domain->lookup);
	domain->lookup = NULL;

	ocs_device_lock(ocs);
		ocs_list_remove(&ocs->domain_list, domain);
		if (domain == ocs->domain) {
			/* set global domain to the new head */
			ocs->domain = ocs_list_get_head(&ocs->domain_list);
			if (ocs->domain) {
				ocs_log_debug(ocs, "setting new domain, old=%p new=%p\n",
						domain, ocs->domain);
			}
		}

		if (ocs_list_empty(&ocs->domain_list) && ocs->domain_list_empty_cb ) {
			(*ocs->domain_list_empty_cb)(ocs, ocs->domain_list_empty_cb_arg);
		}
	ocs_device_unlock(ocs);

	ocs_lock_free(&domain->lookup_lock);

	ocs_free(ocs, domain, sizeof(*domain));
}

/**
 * @brief Free memory resources of a domain object.
 *
 * <h3 class="desc">Description</h3>
 * After the domain object is freed, its child objects are also freed.
 *
 * @param domain Pointer to a domain object.
 *
 * @return None.
 */

void
ocs_domain_force_free(ocs_domain_t *domain)
{
	ocs_sport_t *sport;
	ocs_sport_t *next;

	/* Shutdown domain sm */
	ocs_sm_disable(&domain->drvsm);

	ocs_scsi_notify_domain_force_free(domain);

	ocs_domain_lock(domain);
		ocs_list_foreach_safe(&domain->sport_list, sport, next) {
			ocs_sport_force_free(sport);
		}
	ocs_domain_unlock(domain);
	ocs_hw_domain_force_free(&domain->ocs->hw, domain);
	ocs_domain_free(domain);
}

/**
 * @brief Register a callback when the domain_list goes empty.
 *
 * <h3 class="desc">Description</h3>
 * A function callback may be registered when the domain_list goes empty.
 *
 * @param ocs Pointer to a device object.
 * @param callback Callback function.
 * @param arg Callback argument.
 *
 * @return None.
 */

void
ocs_register_domain_list_empty_cb(ocs_t *ocs, void (*callback)(ocs_t *ocs, void *arg), void *arg)
{
	ocs_device_lock(ocs);
		ocs->domain_list_empty_cb = callback;
		ocs->domain_list_empty_cb_arg = arg;
		if (ocs_list_empty(&ocs->domain_list) && callback) {
			(*callback)(ocs, arg);
		}
	ocs_device_unlock(ocs);
}

/**
 * @brief Return a pointer to the domain, given the instance index.
 *
 * <h3 class="desc">Description</h3>
 * A pointer to the domain context, given by the index, is returned.
 *
 * @param ocs Pointer to the driver instance context.
 * @param index Instance index.
 *
 * @return Returns a pointer to the domain; or NULL.
 */

ocs_domain_t *
ocs_domain_get_instance(ocs_t *ocs, uint32_t index)
{
	ocs_domain_t *domain = NULL;

	if (index >= OCS_MAX_DOMAINS) {
		ocs_log_err(ocs, "invalid index: %d\n", index);
		return NULL;
	}
	ocs_device_lock(ocs);
		ocs_list_foreach(&ocs->domain_list, domain) {
			if (domain->instance_index == index) {
				break;
			}
		}
	ocs_device_unlock(ocs);
	return domain;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Common event handler.
 *
 * <h3 class="desc">Description</h3>
 * Common/shared events are handled here for the domain state machine.
 *
 * @param funcname Function name text.
 * @param ctx Domain state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

static void *
__ocs_domain_common(const char *funcname, ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	ocs_domain_t *domain = ctx->app;

	switch(evt) {
	case OCS_EVT_ENTER:
	case OCS_EVT_REENTER:
	case OCS_EVT_EXIT:
	case OCS_EVT_ALL_CHILD_NODES_FREE:
		/* this can arise if an FLOGI fails on the SPORT, and the SPORT is shutdown */
		break;
	default:
		ocs_log_warn(domain->ocs, "%-20s %-20s not handled\n", funcname, ocs_sm_event_name(evt));
		break;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Common shutdown.
 *
 * <h3 class="desc">Description</h3>
 * Handles common shutdown events.
 *
 * @param funcname Function name text.
 * @param ctx Remote node state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

static void *
__ocs_domain_common_shutdown(const char *funcname, ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	ocs_domain_t *domain = ctx->app;

	switch(evt) {
	case OCS_EVT_ENTER:
	case OCS_EVT_REENTER:
	case OCS_EVT_EXIT:
		break;
	case OCS_EVT_DOMAIN_FOUND:
		ocs_assert(arg, NULL);
		/* sm: / save drec, mark domain_found_pending */
		ocs_memcpy(&domain->pending_drec, arg, sizeof(domain->pending_drec));
		domain->domain_found_pending = TRUE;
		break;
	case OCS_EVT_DOMAIN_LOST: 
		/* clear drec available
		 * sm: unmark domain_found_pending */
		domain->domain_found_pending = FALSE;
		break;

	default:
		ocs_log_warn(domain->ocs, "%-20s %-20s not handled\n", funcname, ocs_sm_event_name(evt));
		break;
	}

	return NULL;
}

#define std_domain_state_decl(...) \
	ocs_domain_t *domain = NULL; \
	ocs_t *ocs = NULL; \
	\
	ocs_assert(ctx, NULL); \
	ocs_assert(ctx->app, NULL); \
	domain = ctx->app; \
	ocs_assert(domain->ocs, NULL); \
	ocs = domain->ocs; \
	ocs_assert(ocs->xport, NULL);

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Initial state.
 *
 * <h3 class="desc">Description</h3>
 * The initial state for a domain. Each domain is initialized to
 * this state at start of day (SOD).
 *
 * @param ctx Domain state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_init(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_ENTER:
		domain->attached = 0;
		break;

	case OCS_EVT_DOMAIN_FOUND: {
		int32_t		vlan = 0;
		uint32_t	i;
		ocs_domain_record_t *drec = arg;
		ocs_sport_t *sport;

		uint64_t	my_wwnn = ocs->xport->req_wwnn;
		uint64_t	my_wwpn = ocs->xport->req_wwpn;
		uint64_t	be_wwpn;

		/* For now, user must specify both port name and node name, or we let firmware
		 * pick both (same as for vports).
		 * TODO: do we want to allow setting only port name or only node name?
		 */
		if ((my_wwpn == 0) || (my_wwnn == 0)) {
			ocs_log_debug(ocs, "using default hardware WWN configuration \n");
			my_wwpn = ocs_get_wwn(&ocs->hw, OCS_HW_WWN_PORT);
			my_wwnn = ocs_get_wwn(&ocs->hw, OCS_HW_WWN_NODE);
		}

		ocs_log_debug(ocs, "Creating base sport using WWPN %016" PRIx64 " WWNN %016" PRIx64 "\n",
			my_wwpn, my_wwnn);

		/* Allocate a sport and transition to __ocs_sport_allocated */
		sport = ocs_sport_alloc(domain, my_wwpn, my_wwnn, UINT32_MAX, ocs->enable_ini, ocs->enable_tgt);

		if (sport == NULL) {
			ocs_log_err(ocs, "ocs_sport_alloc() failed\n");
			break;
		}
		ocs_sm_transition(&sport->sm, __ocs_sport_allocated, NULL);

		/* If domain is ethernet, then fetch the vlan id value */
		if (drec->is_ethernet) {
			vlan = ocs_bitmap_search((void *)drec->map.vlan, TRUE, 512 * 8);
			if (vlan < 0) {
				ocs_log_err(ocs, "no VLAN id available (FCF=%d)\n",
						drec->index);
				break;
			}
		}

		be_wwpn = ocs_htobe64(sport->wwpn);

		/* allocate ocs_sli_port_t object for local port
		 * Note: drec->fc_id is ALPA from read_topology only if loop
		 */
		if (ocs_hw_port_alloc(&ocs->hw, sport, NULL, (uint8_t *)&be_wwpn)) {
			ocs_log_err(ocs, "Can't allocate port\n");
			ocs_sport_free(sport);
			break;
		}

		/* initialize domain object */
		domain->is_loop = drec->is_loop;
		domain->is_fc = drec->is_fc;

		/*
		 * If the loop position map includes ALPA == 0, then we are in a public loop (NL_PORT)
		 * Note that the first element of the loopmap[] contains the count of elements, and if
		 * ALPA == 0 is present, it will occupy the first location after the count.
		 */
		domain->is_nlport = drec->map.loop[1] == 0x00;

		if (domain->is_loop) {
			ocs_log_debug(ocs, "%s fc_id=%#x speed=%d\n",
					drec->is_loop ? (domain->is_nlport ? "public-loop" : "loop") : "other",
					drec->fc_id, drec->speed);

			sport->fc_id = drec->fc_id;
			sport->topology = OCS_SPORT_TOPOLOGY_LOOP;
			ocs_snprintf(sport->display_name, sizeof(sport->display_name), "s%06x", drec->fc_id);

			if (ocs->enable_ini) {
				uint32_t count = drec->map.loop[0];
				ocs_log_debug(ocs, "%d position map entries\n", count);
				for (i = 1; i <= count; i++) {
					if (drec->map.loop[i] != drec->fc_id) {
						ocs_node_t *node;

						ocs_log_debug(ocs, "%#x -> %#x\n",
								drec->fc_id, drec->map.loop[i]);
						node = ocs_node_alloc(sport, drec->map.loop[i], FALSE, TRUE);
						if (node == NULL) {
							ocs_log_err(ocs, "ocs_node_alloc() failed\n");
							break;
						}
						ocs_node_transition(node, __ocs_d_wait_loop, NULL);
					}
				}
			}
		}

		/* Initiate HW domain alloc */
		if (ocs_hw_domain_alloc(&ocs->hw, domain, drec->index, vlan)) {
			ocs_log_err(ocs, "Failed to initiate HW domain allocation\n");
			break;
		}
		ocs_sm_transition(ctx, __ocs_domain_wait_alloc, arg);
		break;
	}
	default:
		__ocs_domain_common(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Wait for the domain allocation to complete.
 *
 * <h3 class="desc">Description</h3>
 * Waits for the domain state to be allocated. After the HW domain
 * allocation process has been initiated, this state waits for
 * that process to complete (i.e. a domain-alloc-ok event).
 *
 * @param ctx Domain state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_wait_alloc(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	ocs_sport_t *sport;
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_DOMAIN_ALLOC_OK: {
		char prop_buf[32];
		uint64_t wwn_bump = 0;
		fc_plogi_payload_t *sp;

		if (ocs_get_property("wwn_bump", prop_buf, sizeof(prop_buf)) == 0) {
			wwn_bump = ocs_strtoull(prop_buf, 0, 0);
		}

		sport = domain->sport;
		ocs_assert(sport, NULL);
		sp = (fc_plogi_payload_t*) sport->service_params;

		/* Save the domain service parameters */
		ocs_memcpy(domain->service_params + 4, domain->dma.virt, sizeof(fc_plogi_payload_t) - 4);
		ocs_memcpy(sport->service_params + 4, domain->dma.virt, sizeof(fc_plogi_payload_t) - 4);

		/* If we're in fabric emulation mode, the flogi service parameters have not been setup yet,
		 * so we need some reasonable BB credit value
		 */
		if (domain->femul_enable) {
			ocs_memcpy(domain->flogi_service_params + 4, domain->service_params + 4, sizeof(fc_plogi_payload_t) - 4);
		}

		/* Update the sport's service parameters, user might have specified non-default names */
		sp->port_name_hi = ocs_htobe32((uint32_t) (sport->wwpn >> 32ll));
		sp->port_name_lo = ocs_htobe32((uint32_t) sport->wwpn);
		sp->node_name_hi = ocs_htobe32((uint32_t) (sport->wwnn >> 32ll));
		sp->node_name_lo = ocs_htobe32((uint32_t) sport->wwnn);

		if (wwn_bump) {
			sp->port_name_lo = ocs_htobe32(ocs_be32toh(sp->port_name_lo) ^ ((uint32_t)(wwn_bump)));
			sp->port_name_hi = ocs_htobe32(ocs_be32toh(sp->port_name_hi) ^ ((uint32_t)(wwn_bump >> 32)));
			sp->node_name_lo = ocs_htobe32(ocs_be32toh(sp->node_name_lo) ^ ((uint32_t)(wwn_bump)));
			sp->node_name_hi = ocs_htobe32(ocs_be32toh(sp->node_name_hi) ^ ((uint32_t)(wwn_bump >> 32)));
			ocs_log_info(ocs, "Overriding WWN\n");
		}

		/* Take the loop topology path, unless we are an NL_PORT (public loop) */
		if (domain->is_loop && !domain->is_nlport) {
			/*
			 * For loop, we already have our FC ID and don't need fabric login.
			 * Transition to the allocated state and post an event to attach to
			 * the domain. Note that this breaks the normal action/transition
			 * pattern here to avoid a race with the domain attach callback.
			 */
			/* sm: is_loop / domain_attach */
			ocs_sm_transition(ctx, __ocs_domain_allocated, NULL);
			__ocs_domain_attach_internal(domain, sport->fc_id);
			break;
		} else {
			ocs_node_t *node;

			/* alloc fabric node, send FLOGI */
			node = ocs_node_find(sport, FC_ADDR_FABRIC);
			if (node) {
				ocs_log_err(ocs, "Hmmmm ... Fabric Controller node already exists\n");
				break;
			}
			node = ocs_node_alloc(sport, FC_ADDR_FABRIC, FALSE, FALSE);
			if (!node) {
				ocs_log_err(ocs, "Error: ocs_node_alloc() failed\n");
			} else {
				if (ocs->nodedb_mask & OCS_NODEDB_PAUSE_FABRIC_LOGIN) {
					ocs_node_pause(node, __ocs_fabric_init);
				} else {
					ocs_node_transition(node, __ocs_fabric_init, NULL);
				}
			}
			/* Accept frames */
			domain->req_accept_frames = 1;
		}
		/* sm: start fabric logins */
		ocs_sm_transition(ctx, __ocs_domain_allocated, NULL);
		break;
	}

	case OCS_EVT_DOMAIN_ALLOC_FAIL:
		/* TODO: hw/device reset */
		ocs_log_err(ocs, "%s recv'd waiting for DOMAIN_ALLOC_OK; shutting down domain\n",
			ocs_sm_event_name(evt));
		domain->req_domain_free = 1;
		break;

	case OCS_EVT_DOMAIN_FOUND:
		/* Should not happen */
		ocs_assert(evt, NULL);
		break;

	case OCS_EVT_DOMAIN_LOST:
		ocs_log_debug(ocs, "%s received while waiting for ocs_hw_domain_alloc() to complete\n", ocs_sm_event_name(evt));
		ocs_sm_transition(ctx, __ocs_domain_wait_domain_lost, NULL);
		break;

	default:
		__ocs_domain_common(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Wait for the domain attach request.
 *
 * <h3 class="desc">Description</h3>
 * In this state, the domain has been allocated and is waiting for a domain attach request.
 * The attach request comes from a node instance completing the fabric login,
 * or from a point-to-point negotiation and login.
 *
 * @param ctx Remote node state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_allocated(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	int32_t rc = 0;
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_DOMAIN_REQ_ATTACH: {
		uint32_t fc_id;

		ocs_assert(arg, NULL);

		fc_id = *((uint32_t*)arg);
		ocs_log_debug(ocs, "Requesting hw domain attach fc_id x%x\n", fc_id);
		/* Update sport lookup */
		ocs_lock(&domain->lookup_lock);
			spv_set(domain->lookup, fc_id, domain->sport);
		ocs_unlock(&domain->lookup_lock);

		/* Update display name for the sport */
		ocs_node_fcid_display(fc_id, domain->sport->display_name, sizeof(domain->sport->display_name));

		/* Issue domain attach call */
		rc = ocs_hw_domain_attach(&ocs->hw, domain, fc_id);
		if (rc) {
			ocs_log_err(ocs, "ocs_hw_domain_attach failed: %d\n", rc);
			return NULL;
		}
		/* sm: / domain_attach */
		ocs_sm_transition(ctx, __ocs_domain_wait_attach, NULL);
		break;
	}

	case OCS_EVT_DOMAIN_FOUND:
		/* Should not happen */
		ocs_assert(evt, NULL);
		break;

	case OCS_EVT_DOMAIN_LOST: {
		int32_t rc;
		ocs_log_debug(ocs, "%s received while waiting for OCS_EVT_DOMAIN_REQ_ATTACH\n",
			ocs_sm_event_name(evt));
		ocs_domain_lock(domain);
		if (!ocs_list_empty(&domain->sport_list)) {
			/* if there are sports, transition to wait state and
			 * send shutdown to each sport */
			ocs_sport_t	*sport = NULL;
			ocs_sport_t	*sport_next = NULL;
			ocs_sm_transition(ctx, __ocs_domain_wait_sports_free, NULL);
			ocs_list_foreach_safe(&domain->sport_list, sport, sport_next) {
				ocs_sm_post_event(&sport->sm, OCS_EVT_SHUTDOWN, NULL);
			}
			ocs_domain_unlock(domain);
		} else {
			ocs_domain_unlock(domain);
			/* no sports exist, free domain */
			ocs_sm_transition(ctx, __ocs_domain_wait_shutdown, NULL);
			rc = ocs_hw_domain_free(&ocs->hw, domain);
			if (rc) {
				ocs_log_err(ocs, "ocs_hw_domain_free() failed: %d\n", rc);
				/* TODO: hw/device reset needed */
			}
		}

		break;
	}

	default:
		__ocs_domain_common(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Wait for the HW domain attach to complete.
 *
 * <h3 class="desc">Description</h3>
 * Waits for the HW domain attach to complete. Forwards attach ok event to the
 * fabric node state machine.
 *
 * @param ctx Remote node state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_wait_attach(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_DOMAIN_ATTACH_OK: {
		ocs_node_t *node = NULL;
		ocs_node_t *next_node = NULL;
		ocs_sport_t *sport;
		ocs_sport_t *next_sport;

		/* Mark as attached */
		domain->attached = 1;

		/* Register with SCSI API */
		if (ocs->enable_tgt)
			ocs_scsi_tgt_new_domain(domain);
		if (ocs->enable_ini)
			ocs_scsi_ini_new_domain(domain);

		/* Transition to ready */
		/* sm: / forward event to all sports and nodes */
		ocs_sm_transition(ctx, __ocs_domain_ready, NULL);

		/* We have an FCFI, so we can accept frames */
		domain->req_accept_frames = 1;
		/* Set domain notify pending state to avoid duplicate domain event post */
		domain->domain_notify_pend = 1;

		/* Notify all nodes that the domain attach request has completed
		 * Note: sport will have already received notification of sport attached
		 * as a result of the HW's port attach.
		 */
		ocs_domain_lock(domain);
			ocs_list_foreach_safe(&domain->sport_list, sport, next_sport) {
				ocs_sport_lock(sport);
					ocs_list_foreach_safe(&sport->node_list, node, next_node) {
						ocs_node_post_event(node, OCS_EVT_DOMAIN_ATTACH_OK, NULL);
					}
				ocs_sport_unlock(sport);
			}
		ocs_domain_unlock(domain);
		domain->domain_notify_pend = 0;
		break;
	}

	case OCS_EVT_DOMAIN_ATTACH_FAIL:
		ocs_log_debug(ocs, "%s received while waiting for hw attach to complete\n", ocs_sm_event_name(evt));
		/* TODO: hw/device reset */
		break;

	case OCS_EVT_DOMAIN_FOUND:
		/* Should not happen */
		ocs_assert(evt, NULL);
		break;

	case OCS_EVT_DOMAIN_LOST:
		/* Domain lost while waiting for an attach to complete, go to a state that waits for
		 * the domain attach to complete, then handle domain lost
		 */
		ocs_sm_transition(ctx, __ocs_domain_wait_domain_lost, NULL);
		break;

	case OCS_EVT_DOMAIN_REQ_ATTACH:
		/* In P2P we can get an attach request from the other FLOGI path, so drop this one */
		break;

	default:
		__ocs_domain_common(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Ready state.
 *
 * <h3 class="desc">Description</h3>
 * This is a domain ready state. It waits for a domain-lost event, and initiates shutdown.
 *
 * @param ctx Remote node state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_ready(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_ENTER: {

		/* start any pending vports */
		if (ocs_vport_start(domain)) {
			ocs_log_debug(domain->ocs, "ocs_vport_start() did not start all vports\n");
		}
		break;
	}
	case OCS_EVT_DOMAIN_LOST: {
		int32_t rc;
		ocs_domain_lock(domain);
		if (!ocs_list_empty(&domain->sport_list)) {
			/* if there are sports, transition to wait state and send
			* shutdown to each sport */
			ocs_sport_t	*sport = NULL;
			ocs_sport_t	*sport_next = NULL;
			ocs_sm_transition(ctx, __ocs_domain_wait_sports_free, NULL);
			ocs_list_foreach_safe(&domain->sport_list, sport, sport_next) {
				ocs_sm_post_event(&sport->sm, OCS_EVT_SHUTDOWN, NULL);
			}
			ocs_domain_unlock(domain);
		} else {
			ocs_domain_unlock(domain);
			/* no sports exist, free domain */
			ocs_sm_transition(ctx, __ocs_domain_wait_shutdown, NULL);
			rc = ocs_hw_domain_free(&ocs->hw, domain);
			if (rc) {
				ocs_log_err(ocs, "ocs_hw_domain_free() failed: %d\n", rc);
				/* TODO: hw/device reset needed */
			}
		}
		break;
	}

	case OCS_EVT_DOMAIN_FOUND:
		/* Should not happen */
		ocs_assert(evt, NULL);
		break;

	case OCS_EVT_DOMAIN_REQ_ATTACH: {
		/* can happen during p2p */
		uint32_t fc_id;

		ocs_assert(arg, NULL);
		fc_id = *((uint32_t*)arg);

		/* Assume that the domain is attached */
		ocs_assert(domain->attached, NULL);

		/* Verify that the requested FC_ID is the same as the one we're working with */
		ocs_assert(domain->sport->fc_id == fc_id, NULL);
		break;
	}

	default:
		__ocs_domain_common(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Wait for nodes to free prior to the domain shutdown.
 *
 * <h3 class="desc">Description</h3>
 * All nodes are freed, and ready for a domain shutdown.
 *
 * @param ctx Remote node sm context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_wait_sports_free(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_ALL_CHILD_NODES_FREE: {
		int32_t rc;

		/* sm: ocs_hw_domain_free */
		ocs_sm_transition(ctx, __ocs_domain_wait_shutdown, NULL);

		/* Request ocs_hw_domain_free and wait for completion */
		rc = ocs_hw_domain_free(&ocs->hw, domain);
		if (rc) {
			ocs_log_err(ocs, "ocs_hw_domain_free() failed: %d\n", rc);
		}
		break;
	}
	default:
		__ocs_domain_common_shutdown(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Complete the domain shutdown.
 *
 * <h3 class="desc">Description</h3>
 * Waits for a HW domain free to complete.
 *
 * @param ctx Remote node state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_wait_shutdown(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_DOMAIN_FREE_OK: {
		if (ocs->enable_ini)
			ocs_scsi_ini_del_domain(domain);
		if (ocs->enable_tgt)
			ocs_scsi_tgt_del_domain(domain);

		/* sm: domain_free */
		if (domain->domain_found_pending) {
			/* save fcf_wwn and drec from this domain, free current domain and allocate
			 * a new one with the same fcf_wwn
			 * TODO: could use a SLI-4 "re-register VPI" operation here
			 */
			uint64_t fcf_wwn = domain->fcf_wwn;
			ocs_domain_record_t drec = domain->pending_drec;

			ocs_log_debug(ocs, "Reallocating domain\n");
			domain->req_domain_free = 1;
			domain = ocs_domain_alloc(ocs, fcf_wwn);

			if (domain == NULL) {
				ocs_log_err(ocs, "ocs_domain_alloc() failed\n");
				/* TODO: hw/device reset needed */
				return NULL;
			}
			/*
			 * got a new domain; at this point, there are at least two domains
			 * once the req_domain_free flag is processed, the associated domain
			 * will be removed.
			 */
			ocs_sm_transition(&domain->drvsm, __ocs_domain_init, NULL);
			ocs_sm_post_event(&domain->drvsm, OCS_EVT_DOMAIN_FOUND, &drec);
		} else {
			domain->req_domain_free = 1;
		}
		break;
	}

	default:
		__ocs_domain_common_shutdown(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}

/**
 * @ingroup domain_sm
 * @brief Domain state machine: Wait for the domain alloc/attach completion
 * after receiving a domain lost.
 *
 * <h3 class="desc">Description</h3>
 * This state is entered when receiving a domain lost while waiting for a domain alloc
 * or a domain attach to complete.
 *
 * @param ctx Remote node state machine context.
 * @param evt Event to process.
 * @param arg Per event optional argument.
 *
 * @return Returns NULL.
 */

void *
__ocs_domain_wait_domain_lost(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
	std_domain_state_decl();

	domain_sm_trace(domain);

	switch(evt) {
	case OCS_EVT_DOMAIN_ALLOC_OK:
	case OCS_EVT_DOMAIN_ATTACH_OK: {
		int32_t rc;
		ocs_domain_lock(domain);
		if (!ocs_list_empty(&domain->sport_list)) {
			/* if there are sports, transition to wait state and send
			* shutdown to each sport */
			ocs_sport_t	*sport = NULL;
			ocs_sport_t	*sport_next = NULL;
			ocs_sm_transition(ctx, __ocs_domain_wait_sports_free, NULL);
			ocs_list_foreach_safe(&domain->sport_list, sport, sport_next) {
				ocs_sm_post_event(&sport->sm, OCS_EVT_SHUTDOWN, NULL);
			}
			ocs_domain_unlock(domain);
		} else {
			ocs_domain_unlock(domain);
			/* no sports exist, free domain */
			ocs_sm_transition(ctx, __ocs_domain_wait_shutdown, NULL);
			rc = ocs_hw_domain_free(&ocs->hw, domain);
			if (rc) {
				ocs_log_err(ocs, "ocs_hw_domain_free() failed: %d\n", rc);
				/* TODO: hw/device reset needed */
			}
		}
		break;
	}
	case OCS_EVT_DOMAIN_ALLOC_FAIL:
	case OCS_EVT_DOMAIN_ATTACH_FAIL:
		ocs_log_err(ocs, "[domain] %-20s: failed\n", ocs_sm_event_name(evt));
		/* TODO: hw/device reset needed */
		break;

	default:
		__ocs_domain_common_shutdown(__func__, ctx, evt, arg);
		return NULL;
	}

	return NULL;
}



/**
 * @brief Save the port's service parameters.
 *
 * <h3 class="desc">Description</h3>
 * Service parameters from the fabric FLOGI are saved in the domain's
 * flogi_service_params array.
 *
 * @param domain Pointer to the domain.
 * @param payload Service parameters to save.
 *
 * @return None.
 */

void
ocs_domain_save_sparms(ocs_domain_t *domain, void *payload)
{
	ocs_memcpy(domain->flogi_service_params, payload, sizeof (fc_plogi_payload_t));
}
/**
 * @brief Initiator domain attach. (internal call only)
 *
 * Assumes that the domain SM lock is already locked
 *
 * <h3 class="desc">Description</h3>
 * The HW domain attach function is started.
 *
 * @param domain Pointer to the domain object.
 * @param s_id FC_ID of which to register this domain.
 *
 * @return None.
 */

void
__ocs_domain_attach_internal(ocs_domain_t *domain, uint32_t s_id)
{
	ocs_memcpy(domain->dma.virt, ((uint8_t*)domain->flogi_service_params)+4, sizeof (fc_plogi_payload_t) - 4);
	(void)ocs_sm_post_event(&domain->drvsm, OCS_EVT_DOMAIN_REQ_ATTACH, &s_id);
}

/**
 * @brief Initiator domain attach.
 *
 * <h3 class="desc">Description</h3>
 * The HW domain attach function is started.
 *
 * @param domain Pointer to the domain object.
 * @param s_id FC_ID of which to register this domain.
 *
 * @return None.
 */

void
ocs_domain_attach(ocs_domain_t *domain, uint32_t s_id)
{
	__ocs_domain_attach_internal(domain, s_id);
}

int
ocs_domain_post_event(ocs_domain_t *domain, ocs_sm_event_t event, void *arg)
{
	int rc;
	int accept_frames;
	int req_domain_free;

	rc = ocs_sm_post_event(&domain->drvsm, event, arg);

	req_domain_free = domain->req_domain_free;
	domain->req_domain_free = 0;

	accept_frames = domain->req_accept_frames;
	domain->req_accept_frames = 0;

	if (accept_frames) {
		ocs_domain_accept_frames(domain);
	}

	if (req_domain_free) {
		ocs_domain_free(domain);
	}

	return rc;
}


/**
 * @brief Return the WWN as a uint64_t.
 *
 * <h3 class="desc">Description</h3>
 * Calls the HW property function for the WWNN or WWPN, and returns the value
 * as a uint64_t.
 *
 * @param hw Pointer to the HW object.
 * @param prop HW property.
 *
 * @return Returns uint64_t request value.
 */

uint64_t
ocs_get_wwn(ocs_hw_t *hw, ocs_hw_property_e prop)
{
	uint8_t *p = ocs_hw_get_ptr(hw, prop);
	uint64_t value = 0;

	if (p) {
		uint32_t i;
		for (i = 0; i < sizeof(value); i++) {
			value = (value << 8) | p[i];
		}
	}
	return value;
}

/**
 * @brief Generate a domain ddump.
 *
 * <h3 class="desc">Description</h3>
 * Generates a domain ddump.
 *
 * @param textbuf Pointer to the text buffer.
 * @param domain Pointer to the domain context.
 *
 * @return Returns 0 on success, or a negative value on failure.
 */

int
ocs_ddump_domain(ocs_textbuf_t *textbuf, ocs_domain_t *domain)
{
	ocs_sport_t *sport;
	int retval = 0;

	ocs_ddump_section(textbuf, "domain", domain->instance_index);
	ocs_ddump_value(textbuf, "display_name", "%s", domain->display_name);

	ocs_ddump_value(textbuf, "fcf", "%#x", domain->fcf);
	ocs_ddump_value(textbuf, "fcf_indicator", "%#x", domain->fcf_indicator);
	ocs_ddump_value(textbuf, "vlan_id", "%#x", domain->vlan_id);
	ocs_ddump_value(textbuf, "indicator", "%#x", domain->indicator);
	ocs_ddump_value(textbuf, "attached", "%d", domain->attached);
	ocs_ddump_value(textbuf, "is_loop", "%d", domain->is_loop);
	ocs_ddump_value(textbuf, "is_nlport", "%d", domain->is_nlport);

	ocs_scsi_ini_ddump(textbuf, OCS_SCSI_DDUMP_DOMAIN, domain);
	ocs_scsi_tgt_ddump(textbuf, OCS_SCSI_DDUMP_DOMAIN, domain);

	ocs_display_sparams(NULL, "domain_sparms", 1, textbuf, domain->dma.virt);

	if (ocs_domain_lock_try(domain) != TRUE) {
		/* Didn't get the lock */
		return -1;
	}
		ocs_list_foreach(&domain->sport_list, sport) {
			retval = ocs_ddump_sport(textbuf, sport);
			if (retval != 0) {
				break;
			}
		}

#if defined(ENABLE_FABRIC_EMULATION)
		ocs_ddump_ns(textbuf, domain->ocs_ns);
#endif

	ocs_domain_unlock(domain);

	ocs_ddump_endsection(textbuf, "domain", domain->instance_index);

	return retval;
}


void
ocs_mgmt_domain_list(ocs_textbuf_t *textbuf, void *object)
{
	ocs_sport_t *sport;
	ocs_domain_t *domain = (ocs_domain_t *)object;

	ocs_mgmt_start_section(textbuf, "domain", domain->instance_index);

	/* Add my status values to textbuf */
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "fcf");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "fcf_indicator");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "vlan_id");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "indicator");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "attached");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "is_loop");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "display_name");
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RD, "num_sports");
#if defined(ENABLE_FABRIC_EMULATION)
	ocs_mgmt_emit_property_name(textbuf, MGMT_MODE_RW, "femul_enable");
#endif

	if (ocs_domain_lock_try(domain) == TRUE) {


		/* If we get here, then we are holding the domain lock */
		ocs_list_foreach(&domain->sport_list, sport) {
			if ((sport->mgmt_functions) && (sport->mgmt_functions->get_list_handler)) {
				sport->mgmt_functions->get_list_handler(textbuf, sport);
			}
		}
		ocs_domain_unlock(domain);
	}

	ocs_mgmt_end_section(textbuf, "domain", domain->instance_index);
}

int
ocs_mgmt_domain_get(ocs_textbuf_t *textbuf, char *parent, char *name, void *object)
{
	ocs_sport_t *sport;
	ocs_domain_t *domain = (ocs_domain_t *)object;
	char qualifier[80];
	int retval = -1;

	ocs_mgmt_start_section(textbuf, "domain", domain->instance_index);

	snprintf(qualifier, sizeof(qualifier), "%s/domain[%d]", parent, domain->instance_index);

	/* If it doesn't start with my qualifier I don't know what to do with it */
	if (ocs_strncmp(name, qualifier, strlen(qualifier)) == 0) {
		char *unqualified_name = name + strlen(qualifier) +1;

		/* See if it's a value I can supply */
		if (ocs_strcmp(unqualified_name, "display_name") == 0) {
			ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "display_name", domain->display_name);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "fcf") == 0) {
			ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "fcf", "%#x", domain->fcf);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "fcf_indicator") == 0) {
			ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "fcf_indicator", "%#x", domain->fcf_indicator);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "vlan_id") == 0) {
			ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "vlan_id", "%#x", domain->vlan_id);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "indicator") == 0) {
			ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "indicator", "%#x", domain->indicator);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "attached") == 0) {
			ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "attached", domain->attached);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "is_loop") == 0) {
			ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "is_loop",  domain->is_loop);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "is_nlport") == 0) {
			ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "is_nlport",  domain->is_nlport);
			retval = 0;
		} else if (ocs_strcmp(unqualified_name, "display_name") == 0) {
			ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "display_name", domain->display_name);
			retval = 0;
#if defined(ENABLE_FABRIC_EMULATION)
		} else if (ocs_strcmp(unqualified_name, "femul_enable") == 0) {
			ocs_mgmt_emit_int(textbuf, MGMT_MODE_RW, "femul_enable", "%d", domain->femul_enable);
			retval = 0;
#endif
		} else if (ocs_strcmp(unqualified_name, "num_sports") == 0) {
			ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "num_sports", "%d", domain->sport_instance_count);
			retval = 0;
		} else {
			/* If I didn't know the value of this status pass the request to each of my children */

			ocs_domain_lock(domain);
			ocs_list_foreach(&domain->sport_list, sport) {
				if ((sport->mgmt_functions) && (sport->mgmt_functions->get_handler)) {
					retval = sport->mgmt_functions->get_handler(textbuf, qualifier, name, sport);
				}

				if (retval == 0) {
					break;
				}

			}
			ocs_domain_unlock(domain);
		}
	}

	ocs_mgmt_end_section(textbuf, "domain", domain->instance_index);
	return retval;
}

void
ocs_mgmt_domain_get_all(ocs_textbuf_t *textbuf, void *object)
{
	ocs_sport_t *sport;
	ocs_domain_t *domain = (ocs_domain_t *)object;

	ocs_mgmt_start_section(textbuf, "domain", domain->instance_index);

	ocs_mgmt_emit_string(textbuf, MGMT_MODE_RD, "display_name", domain->display_name);
	ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "fcf", "%#x", domain->fcf);
	ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "fcf_indicator", "%#x", domain->fcf_indicator);
	ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "vlan_id", "%#x", domain->vlan_id);
	ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "indicator", "%#x", domain->indicator);
	ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "attached", domain->attached);
	ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "is_loop",  domain->is_loop);
	ocs_mgmt_emit_boolean(textbuf, MGMT_MODE_RD, "is_nlport",  domain->is_nlport);
#if defined(ENABLE_FABRIC_EMULATION)
	ocs_mgmt_emit_int(textbuf, MGMT_MODE_RW, "femul_enable", "%d", domain->femul_enable);
#endif
	ocs_mgmt_emit_int(textbuf, MGMT_MODE_RD, "num_sports",  "%d", domain->sport_instance_count);

	ocs_domain_lock(domain);
	ocs_list_foreach(&domain->sport_list, sport) {
		if ((sport->mgmt_functions) && (sport->mgmt_functions->get_all_handler)) {
			sport->mgmt_functions->get_all_handler(textbuf, sport);
		}
	}
	ocs_domain_unlock(domain);


	ocs_mgmt_end_unnumbered_section(textbuf, "domain");

}

int
ocs_mgmt_domain_set(char *parent, char *name, char *value, void *object)
{
	ocs_sport_t *sport;
	ocs_domain_t *domain = (ocs_domain_t *)object;
	char qualifier[80];
	int retval = -1;

	snprintf(qualifier, sizeof(qualifier), "%s/domain[%d]", parent, domain->instance_index);

	/* If it doesn't start with my qualifier I don't know what to do with it */
	if (ocs_strncmp(name, qualifier, strlen(qualifier)) == 0) {

		/* See if it's a value I can supply */

		/* if (ocs_strcmp(unqualified_name, "display_name") == 0) {

		} else */
		{
			/* If I didn't know the value of this status pass the request to each of my children */
			ocs_domain_lock(domain);
			ocs_list_foreach(&domain->sport_list, sport) {
				if ((sport->mgmt_functions) && (sport->mgmt_functions->set_handler)) {
					retval = sport->mgmt_functions->set_handler(qualifier, name, value, sport);
				}

				if (retval == 0) {
					break;
				}

			}
			ocs_domain_unlock(domain);
		}
	}

	return retval;
}

int
ocs_mgmt_domain_exec(char *parent, char *action, void *arg_in, uint32_t arg_in_length,
			void *arg_out, uint32_t arg_out_length, void *object)
{
	ocs_sport_t *sport;
	ocs_domain_t *domain = (ocs_domain_t *)object;
	char qualifier[80];
	int retval = -1;

	snprintf(qualifier, sizeof(qualifier), "%s.domain%d", parent, domain->instance_index);

	/* If it doesn't start with my qualifier I don't know what to do with it */
	if (ocs_strncmp(action, qualifier, strlen(qualifier)) == 0) {

		{
			/* If I didn't know how to do this action pass the request to each of my children */
			ocs_domain_lock(domain);
			ocs_list_foreach(&domain->sport_list, sport) {
				if ((sport->mgmt_functions) && (sport->mgmt_functions->exec_handler)) {
					retval = sport->mgmt_functions->exec_handler(qualifier, action, arg_in, arg_in_length, arg_out, arg_out_length, sport);
				}

				if (retval == 0) {
					break;
				}

			}
			ocs_domain_unlock(domain);
		}
	}

	return retval;
}