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

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <byteswap.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>
#include <ipxe/tables.h>
#include <ipxe/timer.h>
#include <ipxe/retry.h>
#include <ipxe/interface.h>
#include <ipxe/xfer.h>
#include <ipxe/iobuf.h>
#include <ipxe/fc.h>
#include <ipxe/fcels.h>
#include <ipxe/fcns.h>

/** @file
 *
 * Fibre Channel
 *
 */

/** List of Fibre Channel ports */
LIST_HEAD ( fc_ports );

/** List of Fibre Channel peers */
LIST_HEAD ( fc_peers );

/******************************************************************************
 *
 * Well-known addresses
 *
 ******************************************************************************
 */

/** Unassigned port ID */
struct fc_port_id fc_empty_port_id = { .bytes = { 0x00, 0x00, 0x00 } };

/** F_Port contoller port ID */
struct fc_port_id fc_f_port_id = { .bytes = { 0xff, 0xff, 0xfe } };

/** Generic services port ID */
struct fc_port_id fc_gs_port_id = { .bytes = { 0xff, 0xff, 0xfc } };

/** Point-to-point low port ID */
struct fc_port_id fc_ptp_low_port_id = { .bytes = { 0x01, 0x01, 0x01 } };

/** Point-to-point high port ID */
struct fc_port_id fc_ptp_high_port_id = { .bytes = { 0x01, 0x01, 0x02 } };

/******************************************************************************
 *
 * Utility functions
 *
 ******************************************************************************
 */

/**
 * Format Fibre Channel port ID
 *
 * @v id		Fibre Channel port ID
 * @ret id_text		Port ID text
 */
const char * fc_id_ntoa ( const struct fc_port_id *id ) {
	static char id_text[ FC_PORT_ID_STRLEN + 1 /* NUL */ ];

	snprintf ( id_text, sizeof ( id_text ), "%02x.%02x.%02x",
		   id->bytes[0], id->bytes[1], id->bytes[2] );
	return id_text;
}

/**
 * Parse Fibre Channel port ID
 *
 * @v id_text		Port ID text
 * @ret id		Fibre Channel port ID
 * @ret rc		Return status code
 */
int fc_id_aton ( const char *id_text, struct fc_port_id *id ) {
	char *ptr = ( ( char * ) id_text );
	unsigned int i = 0;

	while ( 1 ) {
		id->bytes[i++] = strtoul ( ptr, &ptr, 16 );
		if ( i == sizeof ( id->bytes ) )
			return ( ( *ptr == '\0' ) ? 0 : -EINVAL );
		if ( *ptr != '.' )
			return -EINVAL;
		ptr++;
	}
}

/**
 * Format Fibre Channel WWN
 *
 * @v wwn		Fibre Channel WWN
 * @ret wwn_text	WWN text
 */
const char * fc_ntoa ( const struct fc_name *wwn ) {
	static char wwn_text[ FC_NAME_STRLEN + 1 /* NUL */ ];

	snprintf ( wwn_text, sizeof ( wwn_text ),
		   "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
		   wwn->bytes[0], wwn->bytes[1], wwn->bytes[2], wwn->bytes[3],
		   wwn->bytes[4], wwn->bytes[5], wwn->bytes[6], wwn->bytes[7] );
	return wwn_text;
}

/**
 * Parse Fibre Channel WWN
 *
 * @v wwn_text		WWN text
 * @ret wwn		Fibre Channel WWN
 * @ret rc		Return status code
 */
int fc_aton ( const char *wwn_text, struct fc_name *wwn ) {
	char *ptr = ( ( char * ) wwn_text );
	unsigned int i = 0;

	while ( 1 ) {
		wwn->bytes[i++] = strtoul ( ptr, &ptr, 16 );
		if ( i == sizeof ( wwn->bytes ) )
			return ( ( *ptr == '\0' ) ? 0 : -EINVAL );
		if ( *ptr != ':' )
			return -EINVAL;
		ptr++;
	}
}

/**
 * Fill Fibre Channel socket address
 *
 * @v sa_fc		Fibre Channel socket address to fill in
 * @v id		Fibre Channel port ID
 * @ret sa		Socket address
 */
struct sockaddr * fc_fill_sockaddr ( struct sockaddr_fc *sa_fc,
				     struct fc_port_id *id ) {
	union {
		struct sockaddr sa;
		struct sockaddr_fc fc;
	} *u = container_of ( sa_fc, typeof ( *u ), fc );

	memset ( sa_fc, 0, sizeof ( *sa_fc ) );
	sa_fc->sfc_family = AF_FC;
	memcpy ( &sa_fc->sfc_port_id, id, sizeof ( sa_fc->sfc_port_id ) );
	return &u->sa;
}

/******************************************************************************
 *
 * Fibre Channel link state
 *
 ******************************************************************************
 */

/** Default link status code */
#define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
#define EINFO_EUNKNOWN_LINK_STATUS \
	__einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )

/**
 * Mark Fibre Channel link as up
 *
 * @v link		Fibre Channel link state monitor
 */
static void fc_link_up ( struct fc_link_state *link ) {

	/* Stop retry timer */
	stop_timer ( &link->timer );

	/* Record link state */
	link->rc = 0;
}

/**
 * Mark Fibre Channel link as down
 *
 * @v link		Fibre Channel link state monitor
 * @v rc		Link state
 */
static void fc_link_err ( struct fc_link_state *link, int rc ) {

	/* Record link state */
	if ( rc == 0 )
		rc = -EUNKNOWN_LINK_STATUS;
	link->rc = rc;

	/* Schedule another link examination */
	start_timer_fixed ( &link->timer, FC_LINK_RETRY_DELAY );
}

/**
 * Examine Fibre Channel link state
 *
 * @v link		Fibre Channel link state monitor
 */
static void fc_link_examine ( struct fc_link_state *link ) {

	link->examine ( link );
}

/**
 * Handle Fibre Channel link retry timer expiry
 */
static void fc_link_expired ( struct retry_timer *timer, int over __unused ) {
	struct fc_link_state *link =
		container_of ( timer, struct fc_link_state, timer );

	/* Schedule another link examination */
	start_timer_fixed ( &link->timer, FC_LINK_RETRY_DELAY );

	/* Examine link */
	fc_link_examine ( link );
}

/**
 * Initialise Fibre Channel link state monitor
 *
 * @v link		Fibre Channel link state monitor
 * @v examine		Examine link state method
 * @v refcnt		Reference counter
 */
static void fc_link_init ( struct fc_link_state *link,
			   void ( * examine ) ( struct fc_link_state *link ),
			   struct refcnt *refcnt ) {

	link->rc = -EUNKNOWN_LINK_STATUS;
	timer_init ( &link->timer, fc_link_expired, refcnt );
	link->examine = examine;
}

/**
 * Start monitoring Fibre Channel link state
 *
 * @v link		Fibre Channel link state monitor
 */
static void fc_link_start ( struct fc_link_state *link ) {
	start_timer_nodelay ( &link->timer );
}

/**
 * Stop monitoring Fibre Channel link state
 *
 * @v link		Fibre Channel link state monitor
 */
static void fc_link_stop ( struct fc_link_state *link ) {
	stop_timer ( &link->timer );
}

/******************************************************************************
 *
 * Fibre Channel exchanges
 *
 ******************************************************************************
 */

/** A Fibre Channel exchange */
struct fc_exchange {
	/** Reference count */
	struct refcnt refcnt;
	/** Fibre Channel port */
	struct fc_port *port;
	/** List of active exchanges within this port */
	struct list_head list;

	/** Peer port ID */
	struct fc_port_id peer_port_id;
	/** Data structure type */
	unsigned int type;
	/** Flags */
	unsigned int flags;
	/** Local exchange ID */
	uint16_t xchg_id;
	/** Peer exchange ID */
	uint16_t peer_xchg_id;
	/** Active sequence ID */
	uint8_t seq_id;
	/** Active sequence count */
	uint16_t seq_cnt;

	/** Timeout timer */
	struct retry_timer timer;

	/** Upper-layer protocol interface */
	struct interface ulp;
};

/** Fibre Channel exchange flags */
enum fc_exchange_flags {
	/** We are the exchange originator */
	FC_XCHG_ORIGINATOR = 0x0001,
	/** We have the sequence initiative */
	FC_XCHG_SEQ_INITIATIVE = 0x0002,
	/** This is the first sequence of the exchange */
	FC_XCHG_SEQ_FIRST = 0x0004,
};

/** Fibre Channel timeout */
#define FC_TIMEOUT ( 1 * TICKS_PER_SEC )

/**
 * Create local Fibre Channel exchange identifier
 *
 * @ret xchg_id		Local exchange ID
 */
static unsigned int fc_new_xchg_id ( void ) {
	static uint16_t next_id = 0x0000;

	/* We must avoid using FC_RX_ID_UNKNOWN (0xffff) */
	next_id += 2;
	return next_id;
}

/**
 * Create local Fibre Channel sequence identifier
 *
 * @ret seq_id		Local sequence identifier
 */
static unsigned int fc_new_seq_id ( void ) {
	static uint8_t seq_id = 0x00;

	return (++seq_id);
}

/**
 * Free Fibre Channel exchange
 *
 * @v refcnt		Reference count
 */
static void fc_xchg_free ( struct refcnt *refcnt ) {
	struct fc_exchange *xchg =
		container_of ( refcnt, struct fc_exchange, refcnt );

	assert ( ! timer_running ( &xchg->timer ) );
	assert ( list_empty ( &xchg->list ) );

	fc_port_put ( xchg->port );
	free ( xchg );
}

/**
 * Close Fibre Channel exchange
 *
 * @v xchg		Fibre Channel exchange
 * @v rc		Reason for close
 */
static void fc_xchg_close ( struct fc_exchange *xchg, int rc ) {
	struct fc_port *port = xchg->port;

	if ( rc != 0 ) {
		DBGC2 ( port, "FCXCHG %s/%04x closed: %s\n",
			port->name, xchg->xchg_id, strerror ( rc ) );
	}

	/* Stop timer */
	stop_timer ( &xchg->timer );

	/* If list still holds a reference, remove from list of open
	 * exchanges and drop list's reference.
	 */
	if ( ! list_empty ( &xchg->list ) ) {
		list_del ( &xchg->list );
		INIT_LIST_HEAD ( &xchg->list );
		ref_put ( &xchg->refcnt );
	}

	/* Shutdown interfaces */
	intf_shutdown ( &xchg->ulp, rc );
}

/**
 * Handle exchange timeout
 *
 * @v timer		Timeout timer
 * @v over		Failure indicator
 */
static void fc_xchg_expired ( struct retry_timer *timer, int over __unused ) {
	struct fc_exchange *xchg =
		container_of ( timer, struct fc_exchange, timer );
	struct fc_port *port = xchg->port;

	DBGC ( port, "FCXCHG %s/%04x timed out\n", port->name, xchg->xchg_id );

	/* Terminate the exchange */
	fc_xchg_close ( xchg, -ETIMEDOUT );
}

/**
 * Check Fibre Channel exchange window
 *
 * @v xchg		Fibre Channel exchange
 * @ret len		Length opf window
 */
static size_t fc_xchg_window ( struct fc_exchange *xchg __unused ) {

	/* We don't currently store the path MTU */
	return FC_LOGIN_DEFAULT_MTU;
}

/**
 * Allocate Fibre Channel I/O buffer
 *
 * @v xchg		Fibre Channel exchange
 * @v len		Payload length
 * @ret iobuf		I/O buffer, or NULL
 */
static struct io_buffer * fc_xchg_alloc_iob ( struct fc_exchange *xchg,
					      size_t len ) {
	struct fc_port *port = xchg->port;
	struct io_buffer *iobuf;

	iobuf = xfer_alloc_iob ( &port->transport,
				 ( sizeof ( struct fc_frame_header ) + len ) );
	if ( iobuf ) {
		iob_reserve ( iobuf, sizeof ( struct fc_frame_header ) );
	}
	return iobuf;
}

/**
 * Transmit data as part of a Fibre Channel exchange
 *
 * @v xchg		Fibre Channel exchange
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int fc_xchg_tx ( struct fc_exchange *xchg, struct io_buffer *iobuf,
			struct xfer_metadata *meta ) {
	struct fc_port *port = xchg->port;
	struct sockaddr_fc *dest = ( ( struct sockaddr_fc * ) meta->dest );
	struct fc_frame_header *fchdr;
	unsigned int r_ctl;
	unsigned int f_ctl_es;
	int rc;

	/* Sanity checks */
	if ( ! ( xchg->flags & FC_XCHG_SEQ_INITIATIVE ) ) {
		DBGC ( port, "FCXCHG %s/%04x cannot transmit while not "
		       "holding sequence initiative\n",
		       port->name, xchg->xchg_id );
		rc = -EBUSY;
		goto done;
	}

	/* Calculate routing control */
	switch ( xchg->type ) {
	case FC_TYPE_ELS:
		r_ctl = FC_R_CTL_ELS;
		if ( meta->flags & XFER_FL_RESPONSE ) {
			r_ctl |= FC_R_CTL_SOL_CTRL;
		} else {
			r_ctl |= FC_R_CTL_UNSOL_CTRL;
		}
		break;
	case FC_TYPE_CT:
		r_ctl = FC_R_CTL_DATA;
		if ( meta->flags & XFER_FL_RESPONSE ) {
			r_ctl |= FC_R_CTL_SOL_CTRL;
		} else {
			r_ctl |= FC_R_CTL_UNSOL_CTRL;
		}
		break;
	default:
		r_ctl = FC_R_CTL_DATA;
		switch ( meta->flags &
			 ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ) ) {
		case ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ):
			r_ctl |= FC_R_CTL_CMD_STAT;
			break;
		case ( XFER_FL_CMD_STAT ):
			r_ctl |= FC_R_CTL_UNSOL_CMD;
			break;
		case ( XFER_FL_RESPONSE ):
			r_ctl |= FC_R_CTL_SOL_DATA;
			break;
		default:
			r_ctl |= FC_R_CTL_UNSOL_DATA;
			break;
		}
		break;
	}

	/* Calculate exchange and sequence control */
	f_ctl_es = 0;
	if ( ! ( xchg->flags & FC_XCHG_ORIGINATOR ) )
		f_ctl_es |= FC_F_CTL_ES_RESPONDER;
	if ( xchg->flags & FC_XCHG_SEQ_FIRST )
		f_ctl_es |= FC_F_CTL_ES_FIRST;
	if ( meta->flags & XFER_FL_OUT )
		f_ctl_es |= ( FC_F_CTL_ES_END | FC_F_CTL_ES_LAST );
	if ( meta->flags & XFER_FL_OVER )
		f_ctl_es |= ( FC_F_CTL_ES_END | FC_F_CTL_ES_TRANSFER );

	/* Create frame header */
	fchdr = iob_push ( iobuf, sizeof ( *fchdr ) );
	memset ( fchdr, 0, sizeof ( *fchdr ) );
	fchdr->r_ctl = r_ctl;
	memcpy ( &fchdr->d_id,
		 ( dest ? &dest->sfc_port_id : &xchg->peer_port_id ),
		 sizeof ( fchdr->d_id ) );
	memcpy ( &fchdr->s_id, &port->port_id, sizeof ( fchdr->s_id ) );
	fchdr->type = xchg->type;
	fchdr->f_ctl_es = f_ctl_es;
	fchdr->seq_id = xchg->seq_id;
	fchdr->seq_cnt = htons ( xchg->seq_cnt++ );
	fchdr->ox_id = htons ( ( xchg->flags & FC_XCHG_ORIGINATOR ) ?
			       xchg->xchg_id : xchg->peer_xchg_id );
	fchdr->rx_id = htons ( ( xchg->flags & FC_XCHG_ORIGINATOR ) ?
			       xchg->peer_xchg_id : xchg->xchg_id );
	if ( meta->flags & XFER_FL_ABS_OFFSET ) {
		fchdr->f_ctl_misc |= FC_F_CTL_MISC_REL_OFF;
		fchdr->parameter = htonl ( meta->offset );
	}

	/* Relinquish sequence initiative if applicable */
	if ( meta->flags & XFER_FL_OVER ) {
		xchg->flags &= ~( FC_XCHG_SEQ_INITIATIVE | FC_XCHG_SEQ_FIRST );
		xchg->seq_cnt = 0;
	}

	/* Reset timeout */
	start_timer_fixed ( &xchg->timer, FC_TIMEOUT );

	/* Deliver frame */
	if ( ( rc = xfer_deliver_iob ( &port->transport,
				       iob_disown ( iobuf ) ) ) != 0 ) {
		DBGC ( port, "FCXCHG %s/%04x cannot transmit: %s\n",
		       port->name, xchg->xchg_id, strerror ( rc ) );
		goto done;
	}

 done:
	free_iob ( iobuf );
	return rc;
}

/** Mapping from Fibre Channel routing control information to xfer metadata */
static const uint8_t fc_r_ctl_info_meta_flags[ FC_R_CTL_INFO_MASK + 1 ] = {
	[FC_R_CTL_UNCAT]	= ( 0 ),
	[FC_R_CTL_SOL_DATA]	= ( XFER_FL_RESPONSE ),
	[FC_R_CTL_UNSOL_CTRL]	= ( XFER_FL_CMD_STAT ),
	[FC_R_CTL_SOL_CTRL]	= ( XFER_FL_CMD_STAT ),
	[FC_R_CTL_UNSOL_DATA]	= ( 0 ),
	[FC_R_CTL_DATA_DESC]	= ( XFER_FL_CMD_STAT ),
	[FC_R_CTL_UNSOL_CMD]	= ( XFER_FL_CMD_STAT ),
	[FC_R_CTL_CMD_STAT]	= ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ),
};

/**
 * Receive data as part of a Fibre Channel exchange
 *
 * @v xchg		Fibre Channel exchange
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int fc_xchg_rx ( struct fc_exchange *xchg, struct io_buffer *iobuf,
			struct xfer_metadata *meta __unused ) {
	struct fc_port *port = xchg->port;
	struct fc_frame_header *fchdr = iobuf->data;
	struct xfer_metadata fc_meta;
	struct sockaddr_fc src;
	struct sockaddr_fc dest;
	int rc;

	/* Record peer exchange ID */
	xchg->peer_xchg_id =
		ntohs ( ( fchdr->f_ctl_es & FC_F_CTL_ES_RESPONDER ) ?
			fchdr->rx_id : fchdr->ox_id );

	/* Sequence checks */
	if ( xchg->flags & FC_XCHG_SEQ_INITIATIVE ) {
		DBGC ( port, "FCXCHG %s/%04x received frame while holding "
		       "sequence initiative\n", port->name, xchg->xchg_id );
		rc = -EBUSY;
		goto done;
	}
	if ( ntohs ( fchdr->seq_cnt ) != xchg->seq_cnt ) {
		DBGC ( port, "FCXCHG %s/%04x received out-of-order frame %d "
		       "(expected %d)\n", port->name, xchg->xchg_id,
		       ntohs ( fchdr->seq_cnt ), xchg->seq_cnt );
		rc = -EPIPE;
		goto done;
	}
	if ( xchg->seq_cnt == 0 )
		xchg->seq_id = fchdr->seq_id;
	xchg->seq_cnt++;
	if ( fchdr->seq_id != xchg->seq_id ) {
		DBGC ( port, "FCXCHG %s/%04x received frame for incorrect "
		       "sequence %02x (expected %02x)\n", port->name,
		       xchg->xchg_id, fchdr->seq_id, xchg->seq_id );
		rc = -EPIPE;
		goto done;
	}

	/* Check for end of sequence and transfer of sequence initiative */
	if ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) {
		xchg->seq_cnt = 0;
		if ( fchdr->f_ctl_es & FC_F_CTL_ES_TRANSFER ) {
			xchg->flags |= FC_XCHG_SEQ_INITIATIVE;
			xchg->seq_id = fc_new_seq_id();
		}
	}

	/* Construct metadata */
	memset ( &fc_meta, 0, sizeof ( fc_meta ) );
	fc_meta.flags =
		fc_r_ctl_info_meta_flags[ fchdr->r_ctl & FC_R_CTL_INFO_MASK ];
	if ( fchdr->f_ctl_es & FC_F_CTL_ES_TRANSFER ) {
		fc_meta.flags |= XFER_FL_OVER;
	}
	if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_LAST ) &&
	     ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ) {
		fc_meta.flags |= XFER_FL_OUT;
	}
	if ( fchdr->f_ctl_misc & FC_F_CTL_MISC_REL_OFF ) {
		fc_meta.flags |= XFER_FL_ABS_OFFSET;
		fc_meta.offset = ntohl ( fchdr->parameter );
	}
	fc_meta.src = fc_fill_sockaddr ( &src, &fchdr->s_id );
	fc_meta.dest = fc_fill_sockaddr ( &dest, &fchdr->d_id );

	/* Reset timeout */
	start_timer_fixed ( &xchg->timer, FC_TIMEOUT );

	/* Deliver via exchange's ULP interface */
	iob_pull ( iobuf, sizeof ( *fchdr ) );
	if ( ( rc = xfer_deliver ( &xchg->ulp, iob_disown ( iobuf ),
				   &fc_meta ) ) != 0 ) {
		DBGC ( port, "FCXCHG %s/%04x cannot deliver frame: %s\n",
		       port->name, xchg->xchg_id, strerror ( rc ) );
		goto done;
	}

	/* Close exchange if applicable */
	if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_LAST ) &&
	     ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ) {
		fc_xchg_close ( xchg, 0 );
	}

 done:
	free_iob ( iobuf );
	return rc;
}

/** Fibre Channel exchange ULP interface operations */
static struct interface_operation fc_xchg_ulp_op[] = {
	INTF_OP ( xfer_deliver, struct fc_exchange *, fc_xchg_tx ),
	INTF_OP ( xfer_alloc_iob, struct fc_exchange *, fc_xchg_alloc_iob ),
	INTF_OP ( xfer_window, struct fc_exchange *, fc_xchg_window ),
	INTF_OP ( intf_close, struct fc_exchange *, fc_xchg_close ),
};

/** Fibre Channel exchange ULP interface descriptor */
static struct interface_descriptor fc_xchg_ulp_desc =
	INTF_DESC ( struct fc_exchange, ulp, fc_xchg_ulp_op );

/**
 * Create new Fibre Channel exchange
 *
 * @v port		Fibre Channel port
 * @v peer_port_id	Peer port ID
 * @ret xchg		Exchange, or NULL
 */
static struct fc_exchange * fc_xchg_create ( struct fc_port *port,
					     struct fc_port_id *peer_port_id,
					     unsigned int type ) {
	struct fc_exchange *xchg;

	/* Allocate and initialise structure */
	xchg = zalloc ( sizeof ( *xchg ) );
	if ( ! xchg )
		return NULL;
	ref_init ( &xchg->refcnt, fc_xchg_free );
	intf_init ( &xchg->ulp, &fc_xchg_ulp_desc, &xchg->refcnt );
	timer_init ( &xchg->timer, fc_xchg_expired, &xchg->refcnt );
	xchg->port = fc_port_get ( port );
	memcpy ( &xchg->peer_port_id, peer_port_id,
		 sizeof ( xchg->peer_port_id ) );
	xchg->type = type;
	xchg->xchg_id = fc_new_xchg_id();
	xchg->peer_xchg_id = FC_RX_ID_UNKNOWN;
	xchg->seq_id = fc_new_seq_id();

	/* Transfer reference to list of exchanges and return */
	list_add ( &xchg->list, &port->xchgs );
	return xchg;
}

/**
 * Originate a new Fibre Channel exchange
 *
 * @v parent		Interface to which to attach
 * @v port		Fibre Channel port
 * @v peer_port_id	Peer port ID
 * @ret xchg_id		Exchange ID, or negative error
 */
int fc_xchg_originate ( struct interface *parent, struct fc_port *port,
			struct fc_port_id *peer_port_id, unsigned int type ) {
	struct fc_exchange *xchg;

	/* Allocate and initialise structure */
	xchg = fc_xchg_create ( port, peer_port_id, type );
	if ( ! xchg )
		return -ENOMEM;
	xchg->flags = ( FC_XCHG_ORIGINATOR | FC_XCHG_SEQ_INITIATIVE |
			FC_XCHG_SEQ_FIRST );

	DBGC2 ( port, "FCXCHG %s/%04x originating to %s (type %02x)\n",
		port->name, xchg->xchg_id, fc_id_ntoa ( &xchg->peer_port_id ),
		xchg->type );

	/* Attach to parent interface and return */
	intf_plug_plug ( &xchg->ulp, parent );
	return xchg->xchg_id;
}

/**
 * Open a new responder Fibre Channel exchange
 *
 * @v port		Fibre Channel port
 * @v fchdr		Fibre Channel frame header
 * @ret xchg		Fibre Channel exchange, or NULL
 */
static struct fc_exchange * fc_xchg_respond ( struct fc_port *port,
					      struct fc_frame_header *fchdr ) {
	struct fc_exchange *xchg;
	struct fc_responder *responder;
	unsigned int type = fchdr->type;
	int rc;

	/* Allocate and initialise structure */
	xchg = fc_xchg_create ( port, &fchdr->s_id, type );
	if ( ! xchg )
		return NULL;
	xchg->seq_id = fchdr->seq_id;

	DBGC2 ( port, "FCXCHG %s/%04x responding to %s xchg %04x (type "
		"%02x)\n", port->name, xchg->xchg_id,
		fc_id_ntoa ( &xchg->peer_port_id ),
		ntohs ( fchdr->ox_id ), xchg->type );

	/* Find a responder, if any */
	for_each_table_entry ( responder, FC_RESPONDERS ) {
		if ( responder->type == type ) {
			if ( ( rc = responder->respond ( &xchg->ulp, port,
							 &fchdr->d_id,
							 &fchdr->s_id ) ) !=0 ){
				DBGC ( port, "FCXCHG %s/%04x could not "
				       "respond: %s\n", port->name,
				       xchg->xchg_id, strerror ( rc ) );
			}
		}
		break;
	}

	/* We may or may not have a ULP attached at this point, but
	 * the exchange does exist.
	 */
	return xchg;
}

/******************************************************************************
 *
 * Fibre Channel ports
 *
 ******************************************************************************
 */

/**
 * Close Fibre Channel port
 *
 * @v port		Fibre Channel port
 * @v rc		Reason for close
 */
static void fc_port_close ( struct fc_port *port, int rc ) {
	struct fc_exchange *xchg;
	struct fc_exchange *tmp;

	DBGC ( port, "FCPORT %s closed\n", port->name );

	/* Log out port, if necessary */
	if ( fc_link_ok ( &port->link ) )
		fc_port_logout ( port, rc );

	/* Stop link monitor */
	fc_link_stop ( &port->link );

	/* Shut down interfaces */
	intf_shutdown ( &port->transport, rc );
	intf_shutdown ( &port->flogi, rc );
	intf_shutdown ( &port->ns_plogi, rc );

	/* Shut down any remaining exchanges */
	list_for_each_entry_safe ( xchg, tmp, &port->xchgs, list )
		fc_xchg_close ( xchg, rc );

	/* Remove from list of ports */
	list_del ( &port->list );
	INIT_LIST_HEAD ( &port->list );
}

/**
 * Identify Fibre Channel exchange by local exchange ID
 *
 * @v port		Fibre Channel port
 * @v xchg_id		Local exchange ID
 * @ret xchg		Fibre Channel exchange, or NULL
 */
static struct fc_exchange * fc_port_demux ( struct fc_port *port,
					    unsigned int xchg_id ) {
	struct fc_exchange *xchg;

	list_for_each_entry ( xchg, &port->xchgs, list ) {
		if ( xchg->xchg_id == xchg_id )
			return xchg;
	}
	return NULL;
}

/**
 * Handle received frame from Fibre Channel port
 *
 * @v port		Fibre Channel port
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int fc_port_deliver ( struct fc_port *port, struct io_buffer *iobuf,
			     struct xfer_metadata *meta ) {
	struct fc_frame_header *fchdr = iobuf->data;
	unsigned int xchg_id;
	struct fc_exchange *xchg;
	int rc;

	/* Sanity check */
	if ( iob_len ( iobuf ) < sizeof ( *fchdr ) ) {
		DBGC ( port, "FCPORT %s received underlength frame (%zd "
		       "bytes)\n", port->name, iob_len ( iobuf ) );
		rc = -EINVAL;
		goto err_sanity;
	}

	/* Verify local port ID */
	if ( ( memcmp ( &fchdr->d_id, &port->port_id,
			sizeof ( fchdr->d_id ) ) != 0 ) &&
	     ( memcmp ( &fchdr->d_id, &fc_f_port_id,
			sizeof ( fchdr->d_id ) ) != 0 ) &&
	     ( memcmp ( &port->port_id, &fc_empty_port_id,
			sizeof ( port->port_id ) ) != 0 ) ) {
		DBGC ( port, "FCPORT %s received frame for incorrect port ID "
		       "%s\n", port->name, fc_id_ntoa ( &fchdr->d_id ) );
		rc = -ENOTCONN;
		goto err_port_id;
	}

	/* Demultiplex amongst active exchanges */
	xchg_id = ntohs ( ( fchdr->f_ctl_es & FC_F_CTL_ES_RESPONDER ) ?
			  fchdr->ox_id : fchdr->rx_id );
	xchg = fc_port_demux ( port, xchg_id );

	/* If we have no active exchange and this frame starts a new
	 * exchange, try to create a new responder exchange
	 */
	if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_FIRST ) &&
	     ( fchdr->seq_cnt == 0 ) ) {

		/* Create new exchange */
		xchg = fc_xchg_respond ( port, fchdr );
		if ( ! xchg ) {
			DBGC ( port, "FCPORT %s cannot create new exchange\n",
			       port->name );
			rc = -ENOMEM;
			goto err_respond;
		}
	}

	/* Fail if no exchange exists */
	if ( ! xchg ) {
		DBGC ( port, "FCPORT %s xchg %04x unknown\n",
		       port->name, xchg_id );
		rc = -ENOTCONN;
		goto err_no_xchg;
	}

	/* Pass received frame to exchange */
	ref_get ( &xchg->refcnt );
	if ( ( rc = fc_xchg_rx ( xchg, iob_disown ( iobuf ), meta ) ) != 0 )
		goto err_xchg_rx;

 err_xchg_rx:
	ref_put ( &xchg->refcnt );
 err_no_xchg:
 err_respond:
 err_port_id:
 err_sanity:
	free_iob ( iobuf );
	return rc;
}

/**
 * Log in Fibre Channel port
 *
 * @v port		Fibre Channel port
 * @v port_id		Local port ID
 * @v link_node_wwn	Link node name
 * @v link_port_wwn	Link port name
 * @v has_fabric	Link is to a fabric
 * @ret rc		Return status code
 */
int fc_port_login ( struct fc_port *port, struct fc_port_id *port_id,
		    const struct fc_name *link_node_wwn,
		    const struct fc_name *link_port_wwn, int has_fabric ) {
	struct fc_peer *peer;
	struct fc_peer *tmp;
	int rc;

	/* Perform implicit logout if logged in and details differ */
	if ( fc_link_ok ( &port->link ) &&
	     ( ( ( !! ( port->flags & FC_PORT_HAS_FABRIC ) ) !=
		 ( !! has_fabric ) ) ||
	       ( memcmp ( &port->link_node_wwn, link_node_wwn,
			  sizeof ( port->link_node_wwn ) ) != 0 ) ||
	       ( memcmp ( &port->link_port_wwn, link_port_wwn,
			  sizeof ( port->link_port_wwn ) ) != 0 ) ||
	       ( has_fabric &&
		 ( memcmp ( &port->port_id, port_id,
			    sizeof ( port->port_id ) ) != 0 ) ) ) ) {
		fc_port_logout ( port, 0 );
	}

	/* Log in, if applicable */
	if ( ! fc_link_ok ( &port->link ) ) {

		/* Record link port name */
		memcpy ( &port->link_node_wwn, link_node_wwn,
			 sizeof ( port->link_node_wwn ) );
		memcpy ( &port->link_port_wwn, link_port_wwn,
			 sizeof ( port->link_port_wwn ) );
		DBGC ( port, "FCPORT %s logged in to %s",
		       port->name, fc_ntoa ( &port->link_node_wwn ) );
		DBGC ( port, " port %s\n", fc_ntoa ( &port->link_port_wwn ) );

		/* Calculate local (and possibly remote) port IDs */
		if ( has_fabric ) {
			port->flags |= FC_PORT_HAS_FABRIC;
			memcpy ( &port->port_id, port_id,
				 sizeof ( port->port_id ) );
		} else {
			port->flags &= ~FC_PORT_HAS_FABRIC;
			if ( memcmp ( &port->port_wwn, link_port_wwn,
				      sizeof ( port->port_wwn ) ) > 0 ) {
				memcpy ( &port->port_id, &fc_ptp_high_port_id,
					 sizeof ( port->port_id ) );
				memcpy ( &port->ptp_link_port_id,
					 &fc_ptp_low_port_id,
					 sizeof ( port->ptp_link_port_id ) );
			} else {
				memcpy ( &port->port_id, &fc_ptp_low_port_id,
					 sizeof ( port->port_id ) );
				memcpy ( &port->ptp_link_port_id,
					 &fc_ptp_high_port_id,
					 sizeof ( port->ptp_link_port_id ) );
			}
		}
		DBGC ( port, "FCPORT %s logged in via a %s, with local ID "
		       "%s\n", port->name,
		       ( ( port->flags & FC_PORT_HAS_FABRIC ) ?
			 "fabric" : "point-to-point link" ),
		       fc_id_ntoa ( &port->port_id ) );
	}

	/* Log in to name server, if attached to a fabric */
	if ( has_fabric && ! ( port->flags & FC_PORT_HAS_NS ) ) {

		DBGC ( port, "FCPORT %s attempting login to name server\n",
		       port->name );

		intf_restart ( &port->ns_plogi, -ECANCELED );
		if ( ( rc = fc_els_plogi ( &port->ns_plogi, port,
					   &fc_gs_port_id ) ) != 0 ) {
			DBGC ( port, "FCPORT %s could not initiate name "
			       "server PLOGI: %s\n",
			       port->name, strerror ( rc ) );
			fc_port_logout ( port, rc );
			return rc;
		}
	}

	/* Record login */
	fc_link_up ( &port->link );

	/* Notify peers of link state change */
	list_for_each_entry_safe ( peer, tmp, &fc_peers, list ) {
		fc_peer_get ( peer );
		fc_link_examine ( &peer->link );
		fc_peer_put ( peer );
	}

	return 0;
}

/**
 * Log out Fibre Channel port
 *
 * @v port		Fibre Channel port
 * @v rc		Reason for logout
 */
void fc_port_logout ( struct fc_port *port, int rc ) {
	struct fc_peer *peer;
	struct fc_peer *tmp;

	DBGC ( port, "FCPORT %s logged out: %s\n",
	       port->name, strerror ( rc ) );

	/* Erase port details */
	memset ( &port->port_id, 0, sizeof ( port->port_id ) );
	port->flags = 0;

	/* Record logout */
	fc_link_err ( &port->link, rc );

	/* Notify peers of link state change */
	list_for_each_entry_safe ( peer, tmp, &fc_peers, list ) {
		fc_peer_get ( peer );
		fc_link_examine ( &peer->link );
		fc_peer_put ( peer );
	}
}

/**
 * Handle FLOGI completion
 *
 * @v port		Fibre Channel port
 * @v rc		Reason for completion
 */
static void fc_port_flogi_done ( struct fc_port *port, int rc ) {

	intf_restart ( &port->flogi, rc );

	if ( rc != 0 )
		fc_port_logout ( port, rc );
}

/**
 * Handle name server PLOGI completion
 *
 * @v port		Fibre Channel port
 * @v rc		Reason for completion
 */
static void fc_port_ns_plogi_done ( struct fc_port *port, int rc ) {

	intf_restart ( &port->ns_plogi, rc );

	if ( rc == 0 ) {
		port->flags |= FC_PORT_HAS_NS;
		DBGC ( port, "FCPORT %s logged in to name server\n",
		       port->name );
	} else {
		DBGC ( port, "FCPORT %s could not log in to name server: %s\n",
		       port->name, strerror ( rc ) );
		/* Absence of a name server is not a fatal error */
	}
}

/**
 * Examine Fibre Channel port link state
 *
 * @ link		Fibre Channel link state monitor
 */
static void fc_port_examine ( struct fc_link_state *link ) {
	struct fc_port *port = container_of ( link, struct fc_port, link );
	int rc;

	/* Do nothing if already logged in */
	if ( fc_link_ok ( &port->link ) )
		return;

	DBGC ( port, "FCPORT %s attempting login\n", port->name );

	/* Try to create FLOGI ELS */
	intf_restart ( &port->flogi, -ECANCELED );
	if ( ( rc = fc_els_flogi ( &port->flogi, port ) ) != 0 ) {
		DBGC ( port, "FCPORT %s could not initiate FLOGI: %s\n",
		       port->name, strerror ( rc ) );
		fc_port_logout ( port, rc );
		return;
	}
}

/**
 * Handle change of flow control window
 *
 * @v port		Fibre Channel port
 */
static void fc_port_window_changed ( struct fc_port *port ) {
	size_t window;

	/* Check if transport layer is ready */
	window = xfer_window ( &port->transport );
	if ( window > 0 ) {

		/* Transport layer is ready.  Start login if the link
		 * is not already up.
		 */
		if ( ! fc_link_ok ( &port->link ) )
			fc_link_start ( &port->link );

	} else {

		/* Transport layer is not ready.  Log out port and
		 * wait for transport layer before attempting log in
		 * again.
		 */
		fc_port_logout ( port, -ENOTCONN );
		fc_link_stop ( &port->link );
	}
}

/** Fibre Channel port transport interface operations */
static struct interface_operation fc_port_transport_op[] = {
	INTF_OP ( xfer_deliver, struct fc_port *, fc_port_deliver ),
	INTF_OP ( xfer_window_changed, struct fc_port *,
		  fc_port_window_changed ),
	INTF_OP ( intf_close, struct fc_port *, fc_port_close ),
};

/** Fibre Channel port transport interface descriptor */
static struct interface_descriptor fc_port_transport_desc =
	INTF_DESC ( struct fc_port, transport, fc_port_transport_op );

/** Fibre Channel port FLOGI interface operations */
static struct interface_operation fc_port_flogi_op[] = {
	INTF_OP ( intf_close, struct fc_port *, fc_port_flogi_done ),
};

/** Fibre Channel port FLOGI interface descriptor */
static struct interface_descriptor fc_port_flogi_desc =
	INTF_DESC ( struct fc_port, flogi, fc_port_flogi_op );

/** Fibre Channel port name server PLOGI interface operations */
static struct interface_operation fc_port_ns_plogi_op[] = {
	INTF_OP ( intf_close, struct fc_port *, fc_port_ns_plogi_done ),
};

/** Fibre Channel port name server PLOGI interface descriptor */
static struct interface_descriptor fc_port_ns_plogi_desc =
	INTF_DESC ( struct fc_port, ns_plogi, fc_port_ns_plogi_op );

/**
 * Create Fibre Channel port
 *
 * @v transport		Transport interface
 * @v node		Fibre Channel node name
 * @v port		Fibre Channel port name
 * @v name		Symbolic port name
 * @ret rc		Return status code
 */
int fc_port_open ( struct interface *transport, const struct fc_name *node_wwn,
		   const struct fc_name *port_wwn, const char *name ) {
	struct fc_port *port;

	/* Allocate and initialise structure */
	port = zalloc ( sizeof ( *port ) );
	if ( ! port )
		return -ENOMEM;
	ref_init ( &port->refcnt, NULL );
	intf_init ( &port->transport, &fc_port_transport_desc, &port->refcnt );
	fc_link_init ( &port->link, fc_port_examine, &port->refcnt );
	intf_init ( &port->flogi, &fc_port_flogi_desc, &port->refcnt );
	intf_init ( &port->ns_plogi, &fc_port_ns_plogi_desc, &port->refcnt );
	list_add_tail ( &port->list, &fc_ports );
	INIT_LIST_HEAD ( &port->xchgs );
	memcpy ( &port->node_wwn, node_wwn, sizeof ( port->node_wwn ) );
	memcpy ( &port->port_wwn, port_wwn, sizeof ( port->port_wwn ) );
	snprintf ( port->name, sizeof ( port->name ), "%s", name );

	DBGC ( port, "FCPORT %s opened as %s",
	       port->name, fc_ntoa ( &port->node_wwn ) );
	DBGC ( port, " port %s\n", fc_ntoa ( &port->port_wwn ) );

	/* Attach to transport layer, mortalise self, and return */
	intf_plug_plug ( &port->transport, transport );
	ref_put ( &port->refcnt );
	return 0;
}

/**
 * Find Fibre Channel port by name
 *
 * @v name		Fibre Channel port name
 * @ret port		Fibre Channel port, or NULL
 */
struct fc_port * fc_port_find ( const char *name ) {
	struct fc_port *port;

	list_for_each_entry ( port, &fc_ports, list ) {
		if ( strcmp ( name, port->name ) == 0 )
			return port;
	}
	return NULL;
}

/******************************************************************************
 *
 * Fibre Channel peers
 *
 ******************************************************************************
 */

/**
 * Close Fibre Channel peer
 *
 * @v peer		Fibre Channel peer
 * @v rc		Reason for close
 */
static void fc_peer_close ( struct fc_peer *peer, int rc ) {

	DBGC ( peer, "FCPEER %s closed: %s\n",
	       fc_ntoa ( &peer->port_wwn ) , strerror ( rc ) );

	/* Sanity check */
	assert ( list_empty ( &peer->ulps ) );

	/* Stop link timer */
	fc_link_stop ( &peer->link );

	/* Shut down interfaces */
	intf_shutdown ( &peer->plogi, rc );

	/* Remove from list of peers */
	list_del ( &peer->list );
	INIT_LIST_HEAD ( &peer->list );
}

/**
 * Increment Fibre Channel peer active usage count
 *
 * @v peer		Fibre Channel peer
 */
static void fc_peer_increment ( struct fc_peer *peer ) {

	/* Increment our usage count */
	peer->usage++;
}

/**
 * Decrement Fibre Channel peer active usage count
 *
 * @v peer		Fibre Channel peer
 */
static void fc_peer_decrement ( struct fc_peer *peer ) {

	/* Sanity check */
	assert ( peer->usage > 0 );

	/* Decrement our usage count and log out if we reach zero */
	if ( --(peer->usage) == 0 )
		fc_peer_logout ( peer, 0 );
}

/**
 * Log in Fibre Channel peer
 *
 * @v peer		Fibre Channel peer
 * @v port		Fibre Channel port
 * @v port_id		Port ID
 * @ret rc		Return status code
 */
int fc_peer_login ( struct fc_peer *peer, struct fc_port *port,
		    struct fc_port_id *port_id ) {
	struct fc_ulp *ulp;
	struct fc_ulp *tmp;

	/* Perform implicit logout if logged in and details differ */
	if ( fc_link_ok ( &peer->link ) &&
	     ( ( peer->port != port ) ||
	       ( memcmp ( &peer->port_id, port_id,
			  sizeof ( peer->port_id ) ) !=0 ) ) ) {
		fc_peer_logout ( peer, 0 );
	}

	/* Log in, if applicable */
	if ( ! fc_link_ok ( &peer->link ) ) {

		/* Record peer details */
		assert ( peer->port == NULL );
		peer->port = fc_port_get ( port );
		memcpy ( &peer->port_id, port_id, sizeof ( peer->port_id ) );
		DBGC ( peer, "FCPEER %s logged in via %s as %s\n",
		       fc_ntoa ( &peer->port_wwn ), peer->port->name,
		       fc_id_ntoa ( &peer->port_id ) );

		/* Add login reference */
		fc_peer_get ( peer );
	}

	/* Record login */
	fc_link_up ( &peer->link );

	/* Notify ULPs of link state change */
	list_for_each_entry_safe ( ulp, tmp, &peer->ulps, list ) {
		fc_ulp_get ( ulp );
		fc_link_examine ( &ulp->link );
		fc_ulp_put ( ulp );
	}

	return 0;
}

/**
 * Log out Fibre Channel peer
 *
 * @v peer		Fibre Channel peer
 * @v rc		Reason for logout
 */
void fc_peer_logout ( struct fc_peer *peer, int rc ) {
	struct fc_ulp *ulp;
	struct fc_ulp *tmp;

	DBGC ( peer, "FCPEER %s logged out: %s\n",
	       fc_ntoa ( &peer->port_wwn ), strerror ( rc ) );

	/* Drop login reference, if applicable */
	if ( fc_link_ok ( &peer->link ) )
		fc_peer_put ( peer );

	/* Erase peer details */
	fc_port_put ( peer->port );
	peer->port = NULL;

	/* Record logout */
	fc_link_err ( &peer->link, rc );

	/* Notify ULPs of link state change */
	list_for_each_entry_safe ( ulp, tmp, &peer->ulps, list ) {
		fc_ulp_get ( ulp );
		fc_link_examine ( &ulp->link );
		fc_ulp_put ( ulp );
	}

	/* Close peer if there are no active users */
	if ( peer->usage == 0 )
		fc_peer_close ( peer, rc );
}

/**
 * Handle PLOGI completion
 *
 * @v peer		Fibre Channel peer
 * @v rc		Reason for completion
 */
static void fc_peer_plogi_done ( struct fc_peer *peer, int rc ) {

	intf_restart ( &peer->plogi, rc );

	if ( rc != 0 )
		fc_peer_logout ( peer, rc );
}

/**
 * Initiate PLOGI
 *
 * @v peer		Fibre Channel peer
 * @v port		Fibre Channel port
 * @v peer_port_id	Peer port ID
 * @ret rc		Return status code
 */
static int fc_peer_plogi ( struct fc_peer *peer, struct fc_port *port,
			   struct fc_port_id *peer_port_id ) {
	int rc;

	/* Try to create PLOGI ELS */
	intf_restart ( &peer->plogi, -ECANCELED );
	if ( ( rc = fc_els_plogi ( &peer->plogi, port, peer_port_id ) ) != 0 ) {
		DBGC ( peer, "FCPEER %s could not initiate PLOGI: %s\n",
		       fc_ntoa ( &peer->port_wwn ), strerror ( rc ) );
		fc_peer_logout ( peer, rc );
		return rc;
	}

	return 0;
}

/**
 * Examine Fibre Channel peer link state
 *
 * @ link		Fibre Channel link state monitor
 */
static void fc_peer_examine ( struct fc_link_state *link ) {
	struct fc_peer *peer = container_of ( link, struct fc_peer, link );
	struct fc_port *port;
	int rc;

	/* Check to see if underlying port link has gone down */
	if ( peer->port && ( ! fc_link_ok ( &peer->port->link ) ) ) {
		fc_peer_logout ( peer, -ENOTCONN );
		return;
	}

	/* Do nothing if already logged in */
	if ( fc_link_ok ( &peer->link ) )
		return;

	DBGC ( peer, "FCPEER %s attempting login\n",
	       fc_ntoa ( &peer->port_wwn ) );

	/* Sanity check */
	assert ( peer->port == NULL );

	/* First, look for a port with the peer attached via a
	 * point-to-point link.
	 */
	list_for_each_entry ( port, &fc_ports, list ) {
		if ( fc_link_ok ( &port->link ) &&
		     ( ! ( port->flags & FC_PORT_HAS_FABRIC ) ) &&
		     ( memcmp ( &peer->port_wwn, &port->link_port_wwn,
				sizeof ( peer->port_wwn ) ) == 0 ) ) {
			/* Use this peer port ID, and stop looking */
			fc_peer_plogi ( peer, port, &port->ptp_link_port_id );
			return;
		}
	}

	/* If the peer is not directly attached, try initiating a name
	 * server lookup on any suitable ports.
	 */
	list_for_each_entry ( port, &fc_ports, list ) {
		if ( fc_link_ok ( &port->link ) &&
		     ( port->flags & FC_PORT_HAS_FABRIC ) &&
		     ( port->flags & FC_PORT_HAS_NS ) ) {
			if ( ( rc = fc_ns_query ( peer, port,
						  fc_peer_plogi ) ) != 0 ) {
				DBGC ( peer, "FCPEER %s could not attempt "
				       "name server lookup on %s: %s\n",
				       fc_ntoa ( &peer->port_wwn ), port->name,
				       strerror ( rc ) );
				/* Non-fatal */
			}
		}
	}
}

/** Fibre Channel peer PLOGI interface operations */
static struct interface_operation fc_peer_plogi_op[] = {
	INTF_OP ( intf_close, struct fc_peer *, fc_peer_plogi_done ),
};

/** Fibre Channel peer PLOGI interface descriptor */
static struct interface_descriptor fc_peer_plogi_desc =
	INTF_DESC ( struct fc_peer, plogi, fc_peer_plogi_op );

/**
 * Create Fibre Channel peer
 *
 * @v port_wwn		Node name
 * @ret peer		Fibre Channel peer, or NULL
 */
static struct fc_peer * fc_peer_create ( const struct fc_name *port_wwn ) {
	struct fc_peer *peer;

	/* Allocate and initialise structure */
	peer = zalloc ( sizeof ( *peer ) );
	if ( ! peer )
		return NULL;
	ref_init ( &peer->refcnt, NULL );
	fc_link_init ( &peer->link, fc_peer_examine, &peer->refcnt );
	intf_init ( &peer->plogi, &fc_peer_plogi_desc, &peer->refcnt );
	list_add_tail ( &peer->list, &fc_peers );
	memcpy ( &peer->port_wwn, port_wwn, sizeof ( peer->port_wwn ) );
	INIT_LIST_HEAD ( &peer->ulps );

	/* Start link monitor */
	fc_link_start ( &peer->link );

	DBGC ( peer, "FCPEER %s created\n", fc_ntoa ( &peer->port_wwn ) );
	return peer;
}

/**
 * Get Fibre Channel peer by node name
 *
 * @v port_wwn		Node name
 * @ret peer		Fibre Channel peer, or NULL
 */
struct fc_peer * fc_peer_get_wwn ( const struct fc_name *port_wwn ) {
	struct fc_peer *peer;

	/* Look for an existing peer */
	list_for_each_entry ( peer, &fc_peers, list ) {
		if ( memcmp ( &peer->port_wwn, port_wwn,
			      sizeof ( peer->port_wwn ) ) == 0 )
			return fc_peer_get ( peer );
	}

	/* Create a new peer */
	peer = fc_peer_create ( port_wwn );
	if ( ! peer )
		return NULL;

	return peer;
}

/**
 * Get Fibre Channel peer by port ID
 *
 * @v port		Fibre Channel port
 * @v peer_port_id	Peer port ID
 * @ret peer		Fibre Channel peer, or NULL
 */
struct fc_peer * fc_peer_get_port_id ( struct fc_port *port,
				       const struct fc_port_id *peer_port_id ){
	struct fc_peer *peer;

	/* Look for an existing peer */
	list_for_each_entry ( peer, &fc_peers, list ) {
		if ( ( peer->port == port ) &&
		     ( memcmp ( &peer->port_id, peer_port_id,
				sizeof ( peer->port_id ) ) == 0 ) )
			return fc_peer_get ( peer );
	}

	/* Cannot create a new peer, since we have no port name to use */
	return NULL;
}

/******************************************************************************
 *
 * Fibre Channel upper-layer protocols
 *
 ******************************************************************************
 */

/**
 * Free Fibre Channel upper-layer protocol
 *
 * @v refcnt		Reference count
 */
static void fc_ulp_free ( struct refcnt *refcnt ) {
	struct fc_ulp *ulp = container_of ( refcnt, struct fc_ulp, refcnt );

	fc_peer_put ( ulp->peer );
	free ( ulp );
}

/**
 * Close Fibre Channel upper-layer protocol
 *
 * @v ulp		Fibre Channel upper-layer protocol
 * @v rc		Reason for close
 */
static void fc_ulp_close ( struct fc_ulp *ulp, int rc ) {

	DBGC ( ulp, "FCULP %s/%02x closed: %s\n",
	       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type, strerror ( rc ) );

	/* Sanity check */
	assert ( list_empty ( &ulp->users ) );

	/* Stop link monitor */
	fc_link_stop ( &ulp->link );

	/* Shut down interfaces */
	intf_shutdown ( &ulp->prli, rc );

	/* Remove from list of ULPs */
	list_del ( &ulp->list );
	INIT_LIST_HEAD ( &ulp->list );
}

/**
 * Attach Fibre Channel upper-layer protocol user
 *
 * @v ulp		Fibre Channel upper-layer protocol
 * @v user		Fibre Channel upper-layer protocol user
 */
void fc_ulp_attach ( struct fc_ulp *ulp, struct fc_ulp_user *user ) {

	/* Sanity check */
	assert ( user->ulp == NULL );

	/* Increment peer's usage count */
	fc_peer_increment ( ulp->peer );

	/* Attach user */
	user->ulp = fc_ulp_get ( ulp );
	list_add ( &user->list, &ulp->users );
}

/**
 * Detach Fibre Channel upper-layer protocol user
 *
 * @v user		Fibre Channel upper-layer protocol user
 */
void fc_ulp_detach ( struct fc_ulp_user *user ) {
	struct fc_ulp *ulp = user->ulp;

	/* Do nothing if not attached */
	if ( ! ulp )
		return;

	/* Sanity checks */
	list_check_contains_entry ( user, &ulp->users, list );

	/* Detach user and log out if no users remain */
	list_del ( &user->list );
	if ( list_empty ( &ulp->users ) )
		fc_ulp_logout ( ulp, 0 );

	/* Decrement our peer's usage count */
	fc_peer_decrement ( ulp->peer );

	/* Drop reference */
	user->ulp = NULL;
	fc_ulp_put ( ulp );
}

/**
 * Log in Fibre Channel upper-layer protocol
 *
 * @v ulp		Fibre Channel upper-layer protocol
 * @v param		Service parameters
 * @v param_len		Length of service parameters
 * @v originated	Login was originated by us
 * @ret rc		Return status code
 */
int fc_ulp_login ( struct fc_ulp *ulp, const void *param, size_t param_len,
		   int originated ) {
	struct fc_ulp_user *user;
	struct fc_ulp_user *tmp;

	/* Perform implicit logout if logged in and service parameters differ */
	if ( fc_link_ok ( &ulp->link ) &&
	     ( ( ulp->param_len != param_len ) ||
	       ( memcmp ( ulp->param, param, ulp->param_len ) != 0 ) ) ) {
		fc_ulp_logout ( ulp, 0 );
	}

	/* Work around a bug in some versions of the Linux Fibre
	 * Channel stack, which fail to fully initialise image pairs
	 * established via a PRLI originated by the Linux stack
	 * itself.
	 */
	if ( originated )
		ulp->flags |= FC_ULP_ORIGINATED_LOGIN_OK;
	if ( ! ( ulp->flags & FC_ULP_ORIGINATED_LOGIN_OK ) ) {
		DBGC ( ulp, "FCULP %s/%02x sending extra PRLI to work around "
		       "Linux bug\n",
		       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
		fc_link_stop ( &ulp->link );
		fc_link_start ( &ulp->link );
		return 0;
	}

	/* Log in, if applicable */
	if ( ! fc_link_ok ( &ulp->link ) ) {

		/* Record service parameters */
		assert ( ulp->param == NULL );
		assert ( ulp->param_len == 0 );
		ulp->param = malloc ( param_len );
		if ( ! ulp->param ) {
			DBGC ( ulp, "FCULP %s/%02x could not record "
			       "parameters\n",
			       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
			return -ENOMEM;
		}
		memcpy ( ulp->param, param, param_len );
		ulp->param_len = param_len;
		DBGC ( ulp, "FCULP %s/%02x logged in with parameters:\n",
		       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
		DBGC_HDA ( ulp, 0, ulp->param, ulp->param_len );

		/* Add login reference */
		fc_ulp_get ( ulp );
	}

	/* Record login */
	fc_link_up ( &ulp->link );

	/* Notify users of link state change */
	list_for_each_entry_safe ( user, tmp, &ulp->users, list ) {
		fc_ulp_user_get ( user );
		user->examine ( user );
		fc_ulp_user_put ( user );
	}

	return 0;
}

/**
 * Log out Fibre Channel upper-layer protocol
 *
 * @v ulp		Fibre Channel upper-layer protocol
 * @v rc		Reason for logout
 */
void fc_ulp_logout ( struct fc_ulp *ulp, int rc ) {
	struct fc_ulp_user *user;
	struct fc_ulp_user *tmp;

	DBGC ( ulp, "FCULP %s/%02x logged out: %s\n",
	       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type, strerror ( rc ) );

	/* Drop login reference, if applicable */
	if ( fc_link_ok ( &ulp->link ) )
		fc_ulp_put ( ulp );

	/* Discard service parameters */
	free ( ulp->param );
	ulp->param = NULL;
	ulp->param_len = 0;
	ulp->flags = 0;

	/* Record logout */
	fc_link_err ( &ulp->link, rc );

	/* Notify users of link state change */
	list_for_each_entry_safe ( user, tmp, &ulp->users, list ) {
		fc_ulp_user_get ( user );
		user->examine ( user );
		fc_ulp_user_put ( user );
	}

	/* Close ULP if there are no clients attached */
	if ( list_empty ( &ulp->users ) )
		fc_ulp_close ( ulp, rc );
}

/**
 * Handle PRLI completion
 *
 * @v ulp		Fibre Channel upper-layer protocol
 * @v rc		Reason for completion
 */
static void fc_ulp_prli_done ( struct fc_ulp *ulp, int rc ) {

	intf_restart ( &ulp->prli, rc );

	if ( rc != 0 )
		fc_ulp_logout ( ulp, rc );
}

/**
 * Examine Fibre Channel upper-layer protocol link state
 *
 * @ link		Fibre Channel link state monitor
 */
static void fc_ulp_examine ( struct fc_link_state *link ) {
	struct fc_ulp *ulp = container_of ( link, struct fc_ulp, link );
	int rc;

	/* Check to see if underlying peer link has gone down */
	if ( ! fc_link_ok ( &ulp->peer->link ) ) {
		fc_ulp_logout ( ulp, -ENOTCONN );
		return;
	}

	/* Do nothing if already logged in */
	if ( fc_link_ok ( &ulp->link ) &&
	     ( ulp->flags & FC_ULP_ORIGINATED_LOGIN_OK ) )
		return;

	DBGC ( ulp, "FCULP %s/%02x attempting login\n",
	       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );

	/* Try to create PRLI ELS */
	intf_restart ( &ulp->prli, -ECANCELED );
	if ( ( rc = fc_els_prli ( &ulp->prli, ulp->peer->port,
				  &ulp->peer->port_id, ulp->type ) ) != 0 ) {
		DBGC ( ulp, "FCULP %s/%02x could not initiate PRLI: %s\n",
		       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type,
		       strerror ( rc ) );
		fc_ulp_logout ( ulp, rc );
		return;
	}
}

/** Fibre Channel upper-layer protocol PRLI interface operations */
static struct interface_operation fc_ulp_prli_op[] = {
	INTF_OP ( intf_close, struct fc_ulp *, fc_ulp_prli_done ),
};

/** Fibre Channel upper-layer protocol PRLI interface descriptor */
static struct interface_descriptor fc_ulp_prli_desc =
	INTF_DESC ( struct fc_ulp, prli, fc_ulp_prli_op );

/**
 * Create Fibre Channel upper-layer protocl
 *
 * @v peer		Fibre Channel peer
 * @v type		Type
 * @ret ulp		Fibre Channel upper-layer protocol, or NULL
 */
static struct fc_ulp * fc_ulp_create ( struct fc_peer *peer,
				       unsigned int type ) {
	struct fc_ulp *ulp;

	/* Allocate and initialise structure */
	ulp = zalloc ( sizeof ( *ulp ) );
	if ( ! ulp )
		return NULL;
	ref_init ( &ulp->refcnt, fc_ulp_free );
	fc_link_init ( &ulp->link, fc_ulp_examine, &ulp->refcnt );
	intf_init ( &ulp->prli, &fc_ulp_prli_desc, &ulp->refcnt );
	ulp->peer = fc_peer_get ( peer );
	list_add_tail ( &ulp->list, &peer->ulps );
	ulp->type = type;
	INIT_LIST_HEAD ( &ulp->users );

	/* Start link state monitor */
	fc_link_start ( &ulp->link );

	DBGC ( ulp, "FCULP %s/%02x created\n",
	       fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
	return ulp;
}

/**
 * Get Fibre Channel upper-layer protocol by peer and type
 *
 * @v peer		Fibre Channel peer
 * @v type		Type
 * @ret ulp		Fibre Channel upper-layer protocol, or NULL
 */
static struct fc_ulp * fc_ulp_get_type ( struct fc_peer *peer,
					 unsigned int type ) {
	struct fc_ulp *ulp;

	/* Look for an existing ULP */
	list_for_each_entry ( ulp, &peer->ulps, list ) {
		if ( ulp->type == type )
			return fc_ulp_get ( ulp );
	}

	/* Create a new ULP */
	ulp = fc_ulp_create ( peer, type );
	if ( ! ulp )
		return NULL;

	return ulp;
}

/**
 * Get Fibre Channel upper-layer protocol by port name and type
 *
 * @v port_wwn		Port name
 * @v type		Type
 * @ret ulp		Fibre Channel upper-layer protocol, or NULL
 */
struct fc_ulp * fc_ulp_get_wwn_type ( const struct fc_name *port_wwn,
				      unsigned int type ) {
	struct fc_ulp *ulp;
	struct fc_peer *peer;

	/* Get peer */
	peer = fc_peer_get_wwn ( port_wwn );
	if ( ! peer )
		goto err_peer_get_wwn;

	/* Get ULP */
	ulp = fc_ulp_get_type ( peer, type );
	if ( ! ulp )
		goto err_ulp_get_type;

	/* Drop temporary reference to peer */
	fc_peer_put ( peer );

	return ulp;

	fc_ulp_put ( ulp );
 err_ulp_get_type:
	fc_peer_put ( peer );
 err_peer_get_wwn:
	return NULL;
}

/**
 * Get Fibre Channel upper-layer protocol by port ID and type
 *
 * @v port		Fibre Channel port
 * @v peer_port_id	Peer port ID
 * @v type		Type
 * @ret ulp		Fibre Channel upper-layer protocol, or NULL
 */
struct fc_ulp * fc_ulp_get_port_id_type ( struct fc_port *port,
					  const struct fc_port_id *peer_port_id,
					  unsigned int type ) {
	struct fc_ulp *ulp;
	struct fc_peer *peer;

	/* Get peer */
	peer = fc_peer_get_port_id ( port, peer_port_id );
	if ( ! peer )
		goto err_peer_get_wwn;

	/* Get ULP */
	ulp = fc_ulp_get_type ( peer, type );
	if ( ! ulp )
		goto err_ulp_get_type;

	/* Drop temporary reference to peer */
	fc_peer_put ( peer );

	return ulp;

	fc_ulp_put ( ulp );
 err_ulp_get_type:
	fc_peer_put ( peer );
 err_peer_get_wwn:
	return NULL;
}

/* Drag in objects via fc_ports */
REQUIRING_SYMBOL ( fc_ports );

/* Drag in Fibre Channel configuration */
REQUIRE_OBJECT ( config_fc );