Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

fcoe.c 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <byteswap.h>
  23. #include <ipxe/if_ether.h>
  24. #include <ipxe/if_arp.h>
  25. #include <ipxe/iobuf.h>
  26. #include <ipxe/interface.h>
  27. #include <ipxe/xfer.h>
  28. #include <ipxe/netdevice.h>
  29. #include <ipxe/ethernet.h>
  30. #include <ipxe/vlan.h>
  31. #include <ipxe/features.h>
  32. #include <ipxe/errortab.h>
  33. #include <ipxe/device.h>
  34. #include <ipxe/crc32.h>
  35. #include <ipxe/retry.h>
  36. #include <ipxe/timer.h>
  37. #include <ipxe/fc.h>
  38. #include <ipxe/fip.h>
  39. #include <ipxe/fcoe.h>
  40. /** @file
  41. *
  42. * FCoE protocol
  43. *
  44. */
  45. FEATURE ( FEATURE_PROTOCOL, "FCoE", DHCP_EB_FEATURE_FCOE, 1 );
  46. /* Disambiguate the various error causes */
  47. #define EINVAL_UNDERLENGTH __einfo_error ( EINFO_EINVAL_UNDERLENGTH )
  48. #define EINFO_EINVAL_UNDERLENGTH \
  49. __einfo_uniqify ( EINFO_EINVAL, 0x01, "Underlength packet" )
  50. #define EINVAL_SOF __einfo_error ( EINFO_EINVAL_SOF )
  51. #define EINFO_EINVAL_SOF \
  52. __einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid SoF delimiter" )
  53. #define EINVAL_CRC __einfo_error ( EINFO_EINVAL_CRC )
  54. #define EINFO_EINVAL_CRC \
  55. __einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid CRC (not stripped?)" )
  56. #define EINVAL_EOF __einfo_error ( EINFO_EINVAL_EOF )
  57. #define EINFO_EINVAL_EOF \
  58. __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid EoF delimiter" )
  59. /** An FCoE port */
  60. struct fcoe_port {
  61. /** Reference count */
  62. struct refcnt refcnt;
  63. /** List of FCoE ports */
  64. struct list_head list;
  65. /** Transport interface */
  66. struct interface transport;
  67. /** Network device */
  68. struct net_device *netdev;
  69. /** Node WWN */
  70. union fcoe_name node_wwn;
  71. /** Port WWN */
  72. union fcoe_name port_wwn;
  73. /** FIP retransmission timer */
  74. struct retry_timer timer;
  75. /** FIP timeout counter */
  76. unsigned int timeouts;
  77. /** Flags */
  78. unsigned int flags;
  79. /** FCoE forwarder priority */
  80. unsigned int priority;
  81. /** Keepalive delay (in ms) */
  82. unsigned int keepalive;
  83. /** FCoE forwarder MAC address */
  84. uint8_t fcf_mac[ETH_ALEN];
  85. /** Local MAC address */
  86. uint8_t local_mac[ETH_ALEN];
  87. };
  88. /** FCoE flags */
  89. enum fcoe_flags {
  90. /** Underlying network device is available */
  91. FCOE_HAVE_NETWORK = 0x0001,
  92. /** We have selected an FCoE forwarder to use */
  93. FCOE_HAVE_FCF = 0x0002,
  94. /** We have a FIP-capable FCoE forwarder available to be used */
  95. FCOE_HAVE_FIP_FCF = 0x0004,
  96. /** FCoE forwarder supports server-provided MAC addresses */
  97. FCOE_FCF_ALLOWS_SPMA = 0x0008,
  98. /** An alternative VLAN has been found */
  99. FCOE_VLAN_FOUND = 0x0010,
  100. /** VLAN discovery has timed out */
  101. FCOE_VLAN_TIMED_OUT = 0x0020,
  102. };
  103. struct net_protocol fcoe_protocol __net_protocol;
  104. struct net_protocol fip_protocol __net_protocol;
  105. /** FCoE All-FCoE-MACs address */
  106. static uint8_t all_fcoe_macs[ETH_ALEN] =
  107. { 0x01, 0x10, 0x18, 0x01, 0x00, 0x00 };
  108. /** FCoE All-ENode-MACs address */
  109. static uint8_t all_enode_macs[ETH_ALEN] =
  110. { 0x01, 0x10, 0x18, 0x01, 0x00, 0x01 };
  111. /** FCoE All-FCF-MACs address */
  112. static uint8_t all_fcf_macs[ETH_ALEN] =
  113. { 0x01, 0x10, 0x18, 0x01, 0x00, 0x02 };
  114. /** Default FCoE forwarded MAC address */
  115. static uint8_t default_fcf_mac[ETH_ALEN] =
  116. { 0x0e, 0xfc, 0x00, 0xff, 0xff, 0xfe };
  117. /** Maximum number of VLAN requests before giving up on VLAN discovery */
  118. #define FCOE_MAX_VLAN_REQUESTS 2
  119. /** Delay between retrying VLAN requests */
  120. #define FCOE_VLAN_RETRY_DELAY ( TICKS_PER_SEC )
  121. /** Delay between retrying polling VLAN requests */
  122. #define FCOE_VLAN_POLL_DELAY ( 30 * TICKS_PER_SEC )
  123. /** Maximum number of FIP solicitations before giving up on FIP */
  124. #define FCOE_MAX_FIP_SOLICITATIONS 2
  125. /** Delay between retrying FIP solicitations */
  126. #define FCOE_FIP_RETRY_DELAY ( TICKS_PER_SEC )
  127. /** Maximum number of missing discovery advertisements */
  128. #define FCOE_MAX_FIP_MISSING_KEEPALIVES 4
  129. /** List of FCoE ports */
  130. static LIST_HEAD ( fcoe_ports );
  131. /******************************************************************************
  132. *
  133. * FCoE protocol
  134. *
  135. ******************************************************************************
  136. */
  137. /**
  138. * Identify FCoE port by network device
  139. *
  140. * @v netdev Network device
  141. * @ret fcoe FCoE port, or NULL
  142. */
  143. static struct fcoe_port * fcoe_demux ( struct net_device *netdev ) {
  144. struct fcoe_port *fcoe;
  145. list_for_each_entry ( fcoe, &fcoe_ports, list ) {
  146. if ( fcoe->netdev == netdev )
  147. return fcoe;
  148. }
  149. return NULL;
  150. }
  151. /**
  152. * Reset FCoE port
  153. *
  154. * @v fcoe FCoE port
  155. */
  156. static void fcoe_reset ( struct fcoe_port *fcoe ) {
  157. /* Detach FC port, if any */
  158. intf_restart ( &fcoe->transport, -ECANCELED );
  159. /* Reset any FIP state */
  160. stop_timer ( &fcoe->timer );
  161. fcoe->timeouts = 0;
  162. fcoe->flags = 0;
  163. fcoe->priority = ( FIP_LOWEST_PRIORITY + 1 );
  164. fcoe->keepalive = 0;
  165. memcpy ( fcoe->fcf_mac, default_fcf_mac,
  166. sizeof ( fcoe->fcf_mac ) );
  167. memcpy ( fcoe->local_mac, fcoe->netdev->ll_addr,
  168. sizeof ( fcoe->local_mac ) );
  169. /* Start FIP solicitation if network is available */
  170. if ( netdev_is_open ( fcoe->netdev ) &&
  171. netdev_link_ok ( fcoe->netdev ) ) {
  172. fcoe->flags |= FCOE_HAVE_NETWORK;
  173. start_timer_nodelay ( &fcoe->timer );
  174. DBGC ( fcoe, "FCoE %s starting %s\n", fcoe->netdev->name,
  175. ( vlan_can_be_trunk ( fcoe->netdev ) ?
  176. "VLAN discovery" : "FIP solicitation" ) );
  177. }
  178. /* Send notification of window change */
  179. xfer_window_changed ( &fcoe->transport );
  180. }
  181. /**
  182. * Transmit FCoE packet
  183. *
  184. * @v fcoe FCoE port
  185. * @v iobuf I/O buffer
  186. * @v meta Data transfer metadata
  187. * @ret rc Return status code
  188. */
  189. static int fcoe_deliver ( struct fcoe_port *fcoe,
  190. struct io_buffer *iobuf,
  191. struct xfer_metadata *meta __unused ) {
  192. struct fc_frame_header *fchdr = iobuf->data;
  193. struct fc_els_frame_common *els = ( iobuf->data + sizeof ( *fchdr ) );
  194. struct fcoe_header *fcoehdr;
  195. struct fcoe_footer *fcoeftr;
  196. struct fip_header *fiphdr;
  197. struct fip_login *fipflogi;
  198. struct fip_mac_address *fipmac;
  199. uint32_t crc;
  200. struct net_protocol *net_protocol;
  201. void *ll_source;
  202. int rc;
  203. /* Send as FIP or FCoE as appropriate */
  204. if ( ( fchdr->r_ctl == ( FC_R_CTL_ELS | FC_R_CTL_UNSOL_CTRL ) ) &&
  205. ( els->command == FC_ELS_FLOGI ) &&
  206. ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ) {
  207. /* Create FIP FLOGI descriptor */
  208. fipflogi = iob_push ( iobuf,
  209. offsetof ( typeof ( *fipflogi ), fc ) );
  210. memset ( fipflogi, 0, offsetof ( typeof ( *fipflogi ), fc ) );
  211. fipflogi->type = FIP_FLOGI;
  212. fipflogi->len = ( iob_len ( iobuf ) / 4 );
  213. /* Create FIP MAC address descriptor */
  214. fipmac = iob_put ( iobuf, sizeof ( *fipmac ) );
  215. memset ( fipmac, 0, sizeof ( *fipmac ) );
  216. fipmac->type = FIP_MAC_ADDRESS;
  217. fipmac->len = ( sizeof ( *fipmac ) / 4 );
  218. if ( fcoe->flags & FCOE_FCF_ALLOWS_SPMA ) {
  219. memcpy ( fipmac->mac, fcoe->netdev->ll_addr,
  220. sizeof ( fipmac->mac ) );
  221. }
  222. /* Create FIP header */
  223. fiphdr = iob_push ( iobuf, sizeof ( *fiphdr ) );
  224. memset ( fiphdr, 0, sizeof ( *fiphdr ) );
  225. fiphdr->version = FIP_VERSION;
  226. fiphdr->code = htons ( FIP_CODE_ELS );
  227. fiphdr->subcode = FIP_ELS_REQUEST;
  228. fiphdr->len =
  229. htons ( ( iob_len ( iobuf ) - sizeof ( *fiphdr ) ) / 4);
  230. fiphdr->flags = ( ( fcoe->flags & FCOE_FCF_ALLOWS_SPMA ) ?
  231. htons ( FIP_SP ) : htons ( FIP_FP ) );
  232. /* Send as FIP packet from netdev's own MAC address */
  233. net_protocol = &fip_protocol;
  234. ll_source = fcoe->netdev->ll_addr;
  235. } else {
  236. /* Calculate CRC */
  237. crc = crc32_le ( ~((uint32_t)0), iobuf->data,
  238. iob_len ( iobuf ) );
  239. /* Create FCoE header */
  240. fcoehdr = iob_push ( iobuf, sizeof ( *fcoehdr ) );
  241. memset ( fcoehdr, 0, sizeof ( *fcoehdr ) );
  242. fcoehdr->sof = ( ( fchdr->seq_cnt == ntohs ( 0 ) ) ?
  243. FCOE_SOF_I3 : FCOE_SOF_N3 );
  244. /* Create FCoE footer */
  245. fcoeftr = iob_put ( iobuf, sizeof ( *fcoeftr ) );
  246. memset ( fcoeftr, 0, sizeof ( *fcoeftr ) );
  247. fcoeftr->crc = cpu_to_le32 ( crc ^ ~((uint32_t)0) );
  248. fcoeftr->eof = ( ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ?
  249. FCOE_EOF_T : FCOE_EOF_N );
  250. /* Send as FCoE packet from FCoE MAC address */
  251. net_protocol = &fcoe_protocol;
  252. ll_source = fcoe->local_mac;
  253. }
  254. /* Transmit packet */
  255. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev, net_protocol,
  256. fcoe->fcf_mac, ll_source ) ) != 0 ) {
  257. DBGC ( fcoe, "FCoE %s could not transmit: %s\n",
  258. fcoe->netdev->name, strerror ( rc ) );
  259. goto done;
  260. }
  261. done:
  262. free_iob ( iobuf );
  263. return rc;
  264. }
  265. /**
  266. * Allocate FCoE I/O buffer
  267. *
  268. * @v len Payload length
  269. * @ret iobuf I/O buffer, or NULL
  270. */
  271. static struct io_buffer * fcoe_alloc_iob ( struct fcoe_port *fcoe __unused,
  272. size_t len ) {
  273. struct io_buffer *iobuf;
  274. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( struct fcoe_header ) +
  275. len + sizeof ( struct fcoe_footer ) );
  276. if ( iobuf ) {
  277. iob_reserve ( iobuf, ( MAX_LL_HEADER_LEN +
  278. sizeof ( struct fcoe_header ) ) );
  279. }
  280. return iobuf;
  281. }
  282. /**
  283. * Process incoming FCoE packets
  284. *
  285. * @v iobuf I/O buffer
  286. * @v netdev Network device
  287. * @v ll_dest Link-layer destination address
  288. * @v ll_source Link-layer source address
  289. * @v flags Packet flags
  290. * @ret rc Return status code
  291. */
  292. static int fcoe_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  293. const void *ll_dest, const void *ll_source,
  294. unsigned int flags __unused ) {
  295. struct fcoe_header *fcoehdr;
  296. struct fcoe_footer *fcoeftr;
  297. struct fcoe_port *fcoe;
  298. int rc;
  299. /* Identify FCoE port */
  300. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  301. DBG ( "FCoE received frame for net device %s missing FCoE "
  302. "port\n", netdev->name );
  303. rc = -ENOTCONN;
  304. goto done;
  305. }
  306. /* Discard packets not destined for us */
  307. if ( ( memcmp ( fcoe->local_mac, ll_dest,
  308. sizeof ( fcoe->local_mac ) ) != 0 ) &&
  309. ( memcmp ( default_fcf_mac, ll_dest,
  310. sizeof ( default_fcf_mac ) ) != 0 ) ) {
  311. DBGC2 ( fcoe, "FCoE %s ignoring packet for %s\n",
  312. fcoe->netdev->name, eth_ntoa ( ll_dest ) );
  313. rc = -ENOTCONN;
  314. goto done;
  315. }
  316. /* Sanity check */
  317. if ( iob_len ( iobuf ) < ( sizeof ( *fcoehdr ) + sizeof ( *fcoeftr ) )){
  318. DBGC ( fcoe, "FCoE %s received under-length frame (%zd "
  319. "bytes)\n", fcoe->netdev->name, iob_len ( iobuf ) );
  320. rc = -EINVAL_UNDERLENGTH;
  321. goto done;
  322. }
  323. /* Strip header and footer */
  324. fcoehdr = iobuf->data;
  325. iob_pull ( iobuf, sizeof ( *fcoehdr ) );
  326. fcoeftr = ( iobuf->data + iob_len ( iobuf ) - sizeof ( *fcoeftr ) );
  327. iob_unput ( iobuf, sizeof ( *fcoeftr ) );
  328. /* Validity checks */
  329. if ( fcoehdr->version != FCOE_FRAME_VER ) {
  330. DBGC ( fcoe, "FCoE %s received unsupported frame version "
  331. "%02x\n", fcoe->netdev->name, fcoehdr->version );
  332. rc = -EPROTONOSUPPORT;
  333. goto done;
  334. }
  335. if ( ! ( ( fcoehdr->sof == FCOE_SOF_I3 ) ||
  336. ( fcoehdr->sof == FCOE_SOF_N3 ) ) ) {
  337. DBGC ( fcoe, "FCoE %s received unsupported start-of-frame "
  338. "delimiter %02x\n", fcoe->netdev->name, fcoehdr->sof );
  339. rc = -EINVAL_SOF;
  340. goto done;
  341. }
  342. if ( ( le32_to_cpu ( fcoeftr->crc ) ^ ~((uint32_t)0) ) !=
  343. crc32_le ( ~((uint32_t)0), iobuf->data, iob_len ( iobuf ) ) ) {
  344. DBGC ( fcoe, "FCoE %s received invalid CRC\n",
  345. fcoe->netdev->name );
  346. rc = -EINVAL_CRC;
  347. goto done;
  348. }
  349. if ( ! ( ( fcoeftr->eof == FCOE_EOF_N ) ||
  350. ( fcoeftr->eof == FCOE_EOF_T ) ) ) {
  351. DBGC ( fcoe, "FCoE %s received unsupported end-of-frame "
  352. "delimiter %02x\n", fcoe->netdev->name, fcoeftr->eof );
  353. rc = -EINVAL_EOF;
  354. goto done;
  355. }
  356. /* Record FCF address if applicable */
  357. if ( ( fcoe->flags & FCOE_HAVE_FCF ) &&
  358. ( ! ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ) ) {
  359. memcpy ( &fcoe->fcf_mac, ll_source, sizeof ( fcoe->fcf_mac ) );
  360. }
  361. /* Hand off via transport interface */
  362. if ( ( rc = xfer_deliver_iob ( &fcoe->transport,
  363. iob_disown ( iobuf ) ) ) != 0 ) {
  364. DBGC ( fcoe, "FCoE %s could not deliver frame: %s\n",
  365. fcoe->netdev->name, strerror ( rc ) );
  366. goto done;
  367. }
  368. done:
  369. free_iob ( iobuf );
  370. return rc;
  371. }
  372. /**
  373. * Check FCoE flow control window
  374. *
  375. * @v fcoe FCoE port
  376. * @ret len Length of window
  377. */
  378. static size_t fcoe_window ( struct fcoe_port *fcoe ) {
  379. return ( ( fcoe->flags & FCOE_HAVE_FCF ) ? ~( ( size_t ) 0 ) : 0 );
  380. }
  381. /**
  382. * Close FCoE port
  383. *
  384. * @v fcoe FCoE port
  385. * @v rc Reason for close
  386. */
  387. static void fcoe_close ( struct fcoe_port *fcoe, int rc ) {
  388. stop_timer ( &fcoe->timer );
  389. intf_shutdown ( &fcoe->transport, rc );
  390. netdev_put ( fcoe->netdev );
  391. list_del ( &fcoe->list );
  392. ref_put ( &fcoe->refcnt );
  393. }
  394. /**
  395. * Identify device underlying FCoE port
  396. *
  397. * @v fcoe FCoE port
  398. * @ret device Underlying device
  399. */
  400. static struct device * fcoe_identify_device ( struct fcoe_port *fcoe ) {
  401. return fcoe->netdev->dev;
  402. }
  403. /** FCoE transport interface operations */
  404. static struct interface_operation fcoe_transport_op[] = {
  405. INTF_OP ( xfer_deliver, struct fcoe_port *, fcoe_deliver ),
  406. INTF_OP ( xfer_alloc_iob, struct fcoe_port *, fcoe_alloc_iob ),
  407. INTF_OP ( xfer_window, struct fcoe_port *, fcoe_window ),
  408. INTF_OP ( intf_close, struct fcoe_port *, fcoe_close ),
  409. INTF_OP ( identify_device, struct fcoe_port *,
  410. fcoe_identify_device ),
  411. };
  412. /** FCoE transport interface descriptor */
  413. static struct interface_descriptor fcoe_transport_desc =
  414. INTF_DESC ( struct fcoe_port, transport, fcoe_transport_op );
  415. /******************************************************************************
  416. *
  417. * FIP protocol
  418. *
  419. ******************************************************************************
  420. */
  421. /**
  422. * Parse FIP packet into descriptor set
  423. *
  424. * @v fcoe FCoE port
  425. * @v fiphdr FIP header
  426. * @v len Length of FIP packet
  427. * @v descs Descriptor set to fill in
  428. * @ret rc Return status code
  429. */
  430. static int fcoe_fip_parse ( struct fcoe_port *fcoe, struct fip_header *fiphdr,
  431. size_t len, struct fip_descriptors *descs ) {
  432. union fip_descriptor *desc;
  433. size_t descs_len;
  434. size_t desc_len;
  435. size_t desc_offset;
  436. unsigned int desc_type;
  437. /* Check FIP version */
  438. if ( fiphdr->version != FIP_VERSION ) {
  439. DBGC ( fcoe, "FCoE %s received unsupported FIP version %02x\n",
  440. fcoe->netdev->name, fiphdr->version );
  441. return -EINVAL;
  442. }
  443. /* Check length */
  444. descs_len = ( ntohs ( fiphdr->len ) * 4 );
  445. if ( ( sizeof ( *fiphdr ) + descs_len ) > len ) {
  446. DBGC ( fcoe, "FCoE %s received bad descriptor list length\n",
  447. fcoe->netdev->name );
  448. return -EINVAL;
  449. }
  450. /* Parse descriptor list */
  451. memset ( descs, 0, sizeof ( *descs ) );
  452. for ( desc_offset = 0 ;
  453. desc_offset <= ( descs_len - sizeof ( desc->common ) ) ;
  454. desc_offset += desc_len ) {
  455. /* Find descriptor and validate length */
  456. desc = ( ( ( void * ) ( fiphdr + 1 ) ) + desc_offset );
  457. desc_type = desc->common.type;
  458. desc_len = ( desc->common.len * 4 );
  459. if ( desc_len == 0 ) {
  460. DBGC ( fcoe, "FCoE %s received zero-length "
  461. "descriptor\n", fcoe->netdev->name );
  462. return -EINVAL;
  463. }
  464. if ( ( desc_offset + desc_len ) > descs_len ) {
  465. DBGC ( fcoe, "FCoE %s descriptor overrun\n",
  466. fcoe->netdev->name );
  467. return -EINVAL;
  468. }
  469. /* Handle descriptors that we understand */
  470. if ( ( desc_type > FIP_RESERVED ) &&
  471. ( desc_type < FIP_NUM_DESCRIPTOR_TYPES ) ) {
  472. /* Use only the first instance of a descriptor */
  473. if ( descs->desc[desc_type] == NULL )
  474. descs->desc[desc_type] = desc;
  475. continue;
  476. }
  477. /* Abort if we cannot understand a critical descriptor */
  478. if ( FIP_IS_CRITICAL ( desc_type ) ) {
  479. DBGC ( fcoe, "FCoE %s cannot understand critical "
  480. "descriptor type %02x\n",
  481. fcoe->netdev->name, desc_type );
  482. return -ENOTSUP;
  483. }
  484. /* Ignore non-critical descriptors that we cannot understand */
  485. }
  486. return 0;
  487. }
  488. /**
  489. * Send FIP VLAN request
  490. *
  491. * @v fcoe FCoE port
  492. * @ret rc Return status code
  493. */
  494. static int fcoe_fip_tx_vlan ( struct fcoe_port *fcoe ) {
  495. struct io_buffer *iobuf;
  496. struct {
  497. struct fip_header hdr;
  498. struct fip_mac_address mac_address;
  499. } __attribute__ (( packed )) *request;
  500. int rc;
  501. /* Allocate I/O buffer */
  502. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *request ) );
  503. if ( ! iobuf )
  504. return -ENOMEM;
  505. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  506. /* Construct VLAN request */
  507. request = iob_put ( iobuf, sizeof ( *request ) );
  508. memset ( request, 0, sizeof ( *request ) );
  509. request->hdr.version = FIP_VERSION;
  510. request->hdr.code = htons ( FIP_CODE_VLAN );
  511. request->hdr.subcode = FIP_VLAN_REQUEST;
  512. request->hdr.len = htons ( ( sizeof ( *request ) -
  513. sizeof ( request->hdr ) ) / 4 );
  514. request->mac_address.type = FIP_MAC_ADDRESS;
  515. request->mac_address.len =
  516. ( sizeof ( request->mac_address ) / 4 );
  517. memcpy ( request->mac_address.mac, fcoe->netdev->ll_addr,
  518. sizeof ( request->mac_address.mac ) );
  519. /* Send VLAN request */
  520. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev,
  521. &fip_protocol, all_fcf_macs,
  522. fcoe->netdev->ll_addr ) ) != 0 ) {
  523. DBGC ( fcoe, "FCoE %s could not send VLAN request: "
  524. "%s\n", fcoe->netdev->name, strerror ( rc ) );
  525. return rc;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * Handle received FIP VLAN notification
  531. *
  532. * @v fcoe FCoE port
  533. * @v descs Descriptor list
  534. * @v flags Flags
  535. * @ret rc Return status code
  536. */
  537. static int fcoe_fip_rx_vlan ( struct fcoe_port *fcoe,
  538. struct fip_descriptors *descs,
  539. unsigned int flags __unused ) {
  540. struct fip_mac_address *mac_address = fip_mac_address ( descs );
  541. struct fip_vlan *vlan = fip_vlan ( descs );
  542. unsigned int tag;
  543. int rc;
  544. /* Sanity checks */
  545. if ( ! mac_address ) {
  546. DBGC ( fcoe, "FCoE %s received VLAN notification missing MAC "
  547. "address\n", fcoe->netdev->name );
  548. return -EINVAL;
  549. }
  550. if ( ! vlan ) {
  551. DBGC ( fcoe, "FCoE %s received VLAN notification missing VLAN "
  552. "tag\n", fcoe->netdev->name );
  553. return -EINVAL;
  554. }
  555. /* Create VLAN */
  556. tag = ntohs ( vlan->vlan );
  557. DBGC ( fcoe, "FCoE %s creating VLAN %d for FCF %s\n",
  558. fcoe->netdev->name, tag, eth_ntoa ( mac_address->mac ) );
  559. if ( ( rc = vlan_create ( fcoe->netdev, tag,
  560. FCOE_VLAN_PRIORITY ) ) != 0 ) {
  561. DBGC ( fcoe, "FCoE %s could not create VLAN %d: %s\n",
  562. fcoe->netdev->name, tag, strerror ( rc ) );
  563. return rc;
  564. }
  565. /* Record that a VLAN was found. This FCoE port will play no
  566. * further active role; the real FCoE traffic will use the
  567. * port automatically created for the new VLAN device.
  568. */
  569. fcoe->flags |= FCOE_VLAN_FOUND;
  570. return 0;
  571. }
  572. /**
  573. * Send FIP discovery solicitation
  574. *
  575. * @v fcoe FCoE port
  576. * @ret rc Return status code
  577. */
  578. static int fcoe_fip_tx_solicitation ( struct fcoe_port *fcoe ) {
  579. struct io_buffer *iobuf;
  580. struct {
  581. struct fip_header hdr;
  582. struct fip_mac_address mac_address;
  583. struct fip_name_id name_id;
  584. struct fip_max_fcoe_size max_fcoe_size;
  585. } __attribute__ (( packed )) *solicitation;
  586. int rc;
  587. /* Allocate I/O buffer */
  588. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *solicitation ) );
  589. if ( ! iobuf )
  590. return -ENOMEM;
  591. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  592. /* Construct discovery solicitation */
  593. solicitation = iob_put ( iobuf, sizeof ( *solicitation ) );
  594. memset ( solicitation, 0, sizeof ( *solicitation ) );
  595. solicitation->hdr.version = FIP_VERSION;
  596. solicitation->hdr.code = htons ( FIP_CODE_DISCOVERY );
  597. solicitation->hdr.subcode = FIP_DISCOVERY_SOLICIT;
  598. solicitation->hdr.len = htons ( ( sizeof ( *solicitation ) -
  599. sizeof ( solicitation->hdr ) ) / 4 );
  600. solicitation->hdr.flags = htons ( FIP_FP | FIP_SP );
  601. solicitation->mac_address.type = FIP_MAC_ADDRESS;
  602. solicitation->mac_address.len =
  603. ( sizeof ( solicitation->mac_address ) / 4 );
  604. memcpy ( solicitation->mac_address.mac, fcoe->netdev->ll_addr,
  605. sizeof ( solicitation->mac_address.mac ) );
  606. solicitation->name_id.type = FIP_NAME_ID;
  607. solicitation->name_id.len = ( sizeof ( solicitation->name_id ) / 4 );
  608. memcpy ( &solicitation->name_id.name, &fcoe->node_wwn.fc,
  609. sizeof ( solicitation->name_id.name ) );
  610. solicitation->max_fcoe_size.type = FIP_MAX_FCOE_SIZE;
  611. solicitation->max_fcoe_size.len =
  612. ( sizeof ( solicitation->max_fcoe_size ) / 4 );
  613. solicitation->max_fcoe_size.mtu =
  614. htons ( ETH_MAX_MTU - sizeof ( struct fcoe_header ) -
  615. sizeof ( struct fcoe_footer ) );
  616. /* Send discovery solicitation */
  617. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev,
  618. &fip_protocol, all_fcf_macs,
  619. fcoe->netdev->ll_addr ) ) != 0 ) {
  620. DBGC ( fcoe, "FCoE %s could not send discovery solicitation: "
  621. "%s\n", fcoe->netdev->name, strerror ( rc ) );
  622. return rc;
  623. }
  624. return 0;
  625. }
  626. /**
  627. * Handle received FIP discovery advertisement
  628. *
  629. * @v fcoe FCoE port
  630. * @v descs Descriptor list
  631. * @v flags Flags
  632. * @ret rc Return status code
  633. */
  634. static int fcoe_fip_rx_advertisement ( struct fcoe_port *fcoe,
  635. struct fip_descriptors *descs,
  636. unsigned int flags ) {
  637. struct fip_priority *priority = fip_priority ( descs );
  638. struct fip_mac_address *mac_address = fip_mac_address ( descs );
  639. struct fip_fka_adv_p *fka_adv_p = fip_fka_adv_p ( descs );
  640. /* Sanity checks */
  641. if ( ! priority ) {
  642. DBGC ( fcoe, "FCoE %s received advertisement missing "
  643. "priority\n", fcoe->netdev->name );
  644. return -EINVAL;
  645. }
  646. if ( ! mac_address ) {
  647. DBGC ( fcoe, "FCoE %s received advertisement missing MAC "
  648. "address\n", fcoe->netdev->name );
  649. return -EINVAL;
  650. }
  651. if ( ! fka_adv_p ) {
  652. DBGC ( fcoe, "FCoE %s received advertisement missing FKA ADV "
  653. "period\n", fcoe->netdev->name );
  654. return -EINVAL;
  655. }
  656. if ( ! ( fcoe->flags & FCOE_HAVE_FCF ) ) {
  657. /* We are soliciting for an FCF. Store the highest
  658. * (i.e. lowest-valued) priority solicited
  659. * advertisement that we receive.
  660. */
  661. if ( ( ( flags & ( FIP_A | FIP_S | FIP_F ) ) ==
  662. ( FIP_A | FIP_S | FIP_F ) ) &&
  663. ( priority->priority < fcoe->priority ) ) {
  664. fcoe->flags |= FCOE_HAVE_FIP_FCF;
  665. fcoe->priority = priority->priority;
  666. if ( fka_adv_p->flags & FIP_NO_KEEPALIVE ) {
  667. fcoe->keepalive = 0;
  668. } else {
  669. fcoe->keepalive = ntohl ( fka_adv_p->period );
  670. }
  671. fcoe->flags &= ~FCOE_FCF_ALLOWS_SPMA;
  672. if ( flags & FIP_SP )
  673. fcoe->flags |= FCOE_FCF_ALLOWS_SPMA;
  674. memcpy ( fcoe->fcf_mac, mac_address->mac,
  675. sizeof ( fcoe->fcf_mac ) );
  676. DBGC ( fcoe, "FCoE %s selected FCF %s (pri %d",
  677. fcoe->netdev->name, eth_ntoa ( fcoe->fcf_mac ),
  678. fcoe->priority );
  679. if ( fcoe->keepalive ) {
  680. DBGC ( fcoe, ", FKA ADV %dms",
  681. fcoe->keepalive );
  682. }
  683. DBGC ( fcoe, ", %cPMA)\n",
  684. ( ( fcoe->flags & FCOE_FCF_ALLOWS_SPMA ) ?
  685. 'S' : 'F' ) );
  686. }
  687. } else if ( fcoe->flags & FCOE_HAVE_FIP_FCF ) {
  688. /* We are checking that the FCF remains alive. Reset
  689. * the timeout counter if this is an advertisement
  690. * from our forwarder.
  691. */
  692. if ( memcmp ( fcoe->fcf_mac, mac_address->mac,
  693. sizeof ( fcoe->fcf_mac ) ) == 0 ) {
  694. fcoe->timeouts = 0;
  695. }
  696. } else {
  697. /* We are operating in non-FIP mode and have received
  698. * a FIP advertisement. Reset the link in order to
  699. * attempt FIP.
  700. */
  701. fcoe_reset ( fcoe );
  702. }
  703. return 0;
  704. }
  705. /**
  706. * Handle received FIP ELS response
  707. *
  708. * @v fcoe FCoE port
  709. * @v descs Descriptor list
  710. * @v flags Flags
  711. * @ret rc Return status code
  712. */
  713. static int fcoe_fip_rx_els_response ( struct fcoe_port *fcoe,
  714. struct fip_descriptors *descs,
  715. unsigned int flags __unused ) {
  716. struct fip_els *flogi = fip_flogi ( descs );
  717. struct fip_mac_address *mac_address = fip_mac_address ( descs );
  718. void *frame;
  719. size_t frame_len;
  720. int rc;
  721. /* Sanity checks */
  722. if ( ! flogi ) {
  723. DBGC ( fcoe, "FCoE %s received ELS response missing FLOGI\n",
  724. fcoe->netdev->name );
  725. return -EINVAL;
  726. }
  727. if ( ! mac_address ) {
  728. DBGC ( fcoe, "FCoE %s received ELS response missing MAC "
  729. "address\n", fcoe->netdev->name );
  730. return -EINVAL;
  731. }
  732. /* Record local MAC address */
  733. memcpy ( fcoe->local_mac, mac_address->mac, sizeof ( fcoe->local_mac ));
  734. DBGC ( fcoe, "FCoE %s using local MAC %s\n",
  735. fcoe->netdev->name, eth_ntoa ( fcoe->local_mac ) );
  736. /* Hand off via transport interface */
  737. frame = &flogi->fc;
  738. frame_len = ( ( flogi->len * 4 ) - offsetof ( typeof ( *flogi ), fc ) );
  739. if ( ( rc = xfer_deliver_raw ( &fcoe->transport, frame,
  740. frame_len ) ) != 0 ) {
  741. DBGC ( fcoe, "FCoE %s could not deliver FIP FLOGI frame: %s\n",
  742. fcoe->netdev->name, strerror ( rc ) );
  743. return rc;
  744. }
  745. return 0;
  746. }
  747. /**
  748. * Send FIP keepalive
  749. *
  750. * @v fcoe FCoE port
  751. * @ret rc Return status code
  752. */
  753. static int fcoe_fip_tx_keepalive ( struct fcoe_port *fcoe ) {
  754. struct io_buffer *iobuf;
  755. struct {
  756. struct fip_header hdr;
  757. struct fip_mac_address mac_address;
  758. } __attribute__ (( packed )) *keepalive;
  759. int rc;
  760. /* Allocate I/O buffer */
  761. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *keepalive ) );
  762. if ( ! iobuf )
  763. return -ENOMEM;
  764. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  765. /* Construct keepalive */
  766. keepalive = iob_put ( iobuf, sizeof ( *keepalive ) );
  767. memset ( keepalive, 0, sizeof ( *keepalive ) );
  768. keepalive->hdr.version = FIP_VERSION;
  769. keepalive->hdr.code = htons ( FIP_CODE_MAINTAIN );
  770. keepalive->hdr.subcode = FIP_MAINTAIN_KEEP_ALIVE;
  771. keepalive->hdr.len = htons ( ( sizeof ( *keepalive ) -
  772. sizeof ( keepalive->hdr ) ) / 4 );
  773. keepalive->mac_address.type = FIP_MAC_ADDRESS;
  774. keepalive->mac_address.len =
  775. ( sizeof ( keepalive->mac_address ) / 4 );
  776. memcpy ( keepalive->mac_address.mac, fcoe->netdev->ll_addr,
  777. sizeof ( keepalive->mac_address.mac ) );
  778. /* Send keepalive */
  779. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev,
  780. &fip_protocol, fcoe->fcf_mac,
  781. fcoe->netdev->ll_addr ) ) != 0 ) {
  782. DBGC ( fcoe, "FCoE %s could not send keepalive: %s\n",
  783. fcoe->netdev->name, strerror ( rc ) );
  784. return rc;
  785. }
  786. return 0;
  787. }
  788. /** A FIP handler */
  789. struct fip_handler {
  790. /** Protocol code */
  791. uint16_t code;
  792. /** Protocol subcode */
  793. uint8_t subcode;
  794. /**
  795. * Receive FIP packet
  796. *
  797. * @v fcoe FCoE port
  798. * @v descs Descriptor list
  799. * @v flags Flags
  800. * @ret rc Return status code
  801. */
  802. int ( * rx ) ( struct fcoe_port *fcoe, struct fip_descriptors *descs,
  803. unsigned int flags );
  804. };
  805. /** FIP handlers */
  806. static struct fip_handler fip_handlers[] = {
  807. { FIP_CODE_VLAN, FIP_VLAN_NOTIFY,
  808. fcoe_fip_rx_vlan },
  809. { FIP_CODE_DISCOVERY, FIP_DISCOVERY_ADVERTISE,
  810. fcoe_fip_rx_advertisement },
  811. { FIP_CODE_ELS, FIP_ELS_RESPONSE,
  812. fcoe_fip_rx_els_response },
  813. };
  814. /**
  815. * Process incoming FIP packets
  816. *
  817. * @v iobuf I/O buffer
  818. * @v netdev Network device
  819. * @v ll_dest Link-layer destination address
  820. * @v ll_source Link-layer source address
  821. * @v flags Packet flags
  822. * @ret rc Return status code
  823. */
  824. static int fcoe_fip_rx ( struct io_buffer *iobuf,
  825. struct net_device *netdev,
  826. const void *ll_dest,
  827. const void *ll_source __unused,
  828. unsigned int flags __unused ) {
  829. struct fip_header *fiphdr = iobuf->data;
  830. struct fip_descriptors descs;
  831. struct fip_handler *handler;
  832. struct fcoe_port *fcoe;
  833. unsigned int i;
  834. int rc;
  835. /* Identify FCoE port */
  836. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  837. DBG ( "FCoE received FIP frame for net device %s missing FCoE "
  838. "port\n", netdev->name );
  839. rc = -ENOTCONN;
  840. goto done;
  841. }
  842. /* Discard packets not destined for us */
  843. if ( ( memcmp ( fcoe->netdev->ll_addr, ll_dest, ETH_ALEN ) != 0 ) &&
  844. ( memcmp ( all_fcoe_macs, ll_dest,
  845. sizeof ( all_fcoe_macs ) ) != 0 ) &&
  846. ( memcmp ( all_enode_macs, ll_dest,
  847. sizeof ( all_enode_macs ) ) != 0 ) ) {
  848. DBGC2 ( fcoe, "FCoE %s ignoring FIP packet for %s\n",
  849. fcoe->netdev->name, eth_ntoa ( ll_dest ) );
  850. rc = -ENOTCONN;
  851. goto done;
  852. }
  853. /* Parse FIP packet */
  854. if ( ( rc = fcoe_fip_parse ( fcoe, fiphdr, iob_len ( iobuf ),
  855. &descs ) ) != 0 )
  856. goto done;
  857. /* Find a suitable handler */
  858. for ( i = 0 ; i < ( sizeof ( fip_handlers ) /
  859. sizeof ( fip_handlers[0] ) ) ; i++ ) {
  860. handler = &fip_handlers[i];
  861. if ( ( handler->code == ntohs ( fiphdr->code ) ) &&
  862. ( handler->subcode == fiphdr->subcode ) ) {
  863. rc = handler->rx ( fcoe, &descs,
  864. ntohs ( fiphdr->flags ) );
  865. goto done;
  866. }
  867. }
  868. DBGC ( fcoe, "FCoE %s received unsupported FIP code %04x.%02x\n",
  869. fcoe->netdev->name, ntohs ( fiphdr->code ), fiphdr->subcode );
  870. rc = -ENOTSUP;
  871. done:
  872. free_iob ( iobuf );
  873. return rc;
  874. }
  875. /******************************************************************************
  876. *
  877. * FCoE ports
  878. *
  879. ******************************************************************************
  880. */
  881. /**
  882. * Handle FCoE timer expiry
  883. *
  884. * @v timer FIP timer
  885. * @v over Timer expired
  886. */
  887. static void fcoe_expired ( struct retry_timer *timer, int over __unused ) {
  888. struct fcoe_port *fcoe =
  889. container_of ( timer, struct fcoe_port, timer );
  890. int rc;
  891. /* Sanity check */
  892. assert ( fcoe->flags & FCOE_HAVE_NETWORK );
  893. /* Increment the timeout counter */
  894. fcoe->timeouts++;
  895. if ( vlan_can_be_trunk ( fcoe->netdev ) &
  896. ! ( fcoe->flags & FCOE_VLAN_TIMED_OUT ) ) {
  897. /* If we have already found a VLAN, send infrequent
  898. * VLAN requests, in case VLAN information changes.
  899. */
  900. if ( fcoe->flags & FCOE_VLAN_FOUND ) {
  901. fcoe->flags &= ~FCOE_VLAN_FOUND;
  902. fcoe->timeouts = 0;
  903. start_timer_fixed ( &fcoe->timer,
  904. FCOE_VLAN_POLL_DELAY );
  905. fcoe_fip_tx_vlan ( fcoe );
  906. return;
  907. }
  908. /* If we have not yet found a VLAN, and we have not
  909. * yet timed out and given up on finding one, then
  910. * send a VLAN request and wait.
  911. */
  912. if ( fcoe->timeouts <= FCOE_MAX_VLAN_REQUESTS ) {
  913. start_timer_fixed ( &fcoe->timer,
  914. FCOE_VLAN_RETRY_DELAY );
  915. fcoe_fip_tx_vlan ( fcoe );
  916. return;
  917. }
  918. /* We have timed out waiting for a VLAN; proceed to
  919. * FIP discovery.
  920. */
  921. fcoe->flags |= FCOE_VLAN_TIMED_OUT;
  922. fcoe->timeouts = 0;
  923. DBGC ( fcoe, "FCoE %s giving up on VLAN discovery\n",
  924. fcoe->netdev->name );
  925. start_timer_nodelay ( &fcoe->timer );
  926. } else if ( ! ( fcoe->flags & FCOE_HAVE_FCF ) ) {
  927. /* If we have not yet found a FIP-capable forwarder,
  928. * and we have not yet timed out and given up on
  929. * finding one, then send a FIP solicitation and wait.
  930. */
  931. start_timer_fixed ( &fcoe->timer, FCOE_FIP_RETRY_DELAY );
  932. if ( ( ! ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ) &&
  933. ( fcoe->timeouts <= FCOE_MAX_FIP_SOLICITATIONS ) ) {
  934. fcoe_fip_tx_solicitation ( fcoe );
  935. return;
  936. }
  937. /* Attach Fibre Channel port */
  938. if ( ( rc = fc_port_open ( &fcoe->transport, &fcoe->node_wwn.fc,
  939. &fcoe->port_wwn.fc,
  940. fcoe->netdev->name ) ) != 0 ) {
  941. DBGC ( fcoe, "FCoE %s could not create FC port: %s\n",
  942. fcoe->netdev->name, strerror ( rc ) );
  943. /* We will try again on the next timer expiry */
  944. return;
  945. }
  946. stop_timer ( &fcoe->timer );
  947. /* Either we have found a FIP-capable forwarder, or we
  948. * have timed out and will fall back to pre-FIP mode.
  949. */
  950. fcoe->flags |= FCOE_HAVE_FCF;
  951. fcoe->timeouts = 0;
  952. DBGC ( fcoe, "FCoE %s using %sFIP FCF %s\n", fcoe->netdev->name,
  953. ( ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ? "" : "non-" ),
  954. eth_ntoa ( fcoe->fcf_mac ) );
  955. /* Start sending keepalives if applicable */
  956. if ( fcoe->keepalive )
  957. start_timer_nodelay ( &fcoe->timer );
  958. /* Send notification of window change */
  959. xfer_window_changed ( &fcoe->transport );
  960. } else {
  961. /* Send keepalive */
  962. start_timer_fixed ( &fcoe->timer,
  963. ( ( fcoe->keepalive * TICKS_PER_SEC ) / 1000 ) );
  964. fcoe_fip_tx_keepalive ( fcoe );
  965. /* Abandon FCF if we have not seen its advertisements */
  966. if ( fcoe->timeouts > FCOE_MAX_FIP_MISSING_KEEPALIVES ) {
  967. DBGC ( fcoe, "FCoE %s abandoning FCF %s\n",
  968. fcoe->netdev->name, eth_ntoa ( fcoe->fcf_mac ));
  969. fcoe_reset ( fcoe );
  970. }
  971. }
  972. }
  973. /**
  974. * Create FCoE port
  975. *
  976. * @v netdev Network device
  977. * @ret rc Return status code
  978. */
  979. static int fcoe_probe ( struct net_device *netdev ) {
  980. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  981. struct fcoe_port *fcoe;
  982. int rc;
  983. /* Sanity check */
  984. if ( ll_protocol->ll_proto != htons ( ARPHRD_ETHER ) ) {
  985. /* Not an error; simply skip this net device */
  986. DBG ( "FCoE skipping non-Ethernet device %s\n", netdev->name );
  987. rc = 0;
  988. goto err_non_ethernet;
  989. }
  990. /* Allocate and initialise structure */
  991. fcoe = zalloc ( sizeof ( *fcoe ) );
  992. if ( ! fcoe ) {
  993. rc = -ENOMEM;
  994. goto err_zalloc;
  995. }
  996. ref_init ( &fcoe->refcnt, NULL );
  997. intf_init ( &fcoe->transport, &fcoe_transport_desc, &fcoe->refcnt );
  998. timer_init ( &fcoe->timer, fcoe_expired, &fcoe->refcnt );
  999. fcoe->netdev = netdev_get ( netdev );
  1000. /* Construct node and port names */
  1001. fcoe->node_wwn.fcoe.authority = htons ( FCOE_AUTHORITY_IEEE );
  1002. memcpy ( &fcoe->node_wwn.fcoe.mac, netdev->ll_addr,
  1003. sizeof ( fcoe->node_wwn.fcoe.mac ) );
  1004. fcoe->port_wwn.fcoe.authority = htons ( FCOE_AUTHORITY_IEEE_EXTENDED );
  1005. memcpy ( &fcoe->port_wwn.fcoe.mac, netdev->ll_addr,
  1006. sizeof ( fcoe->port_wwn.fcoe.mac ) );
  1007. DBGC ( fcoe, "FCoE %s is %s", fcoe->netdev->name,
  1008. fc_ntoa ( &fcoe->node_wwn.fc ) );
  1009. DBGC ( fcoe, " port %s\n", fc_ntoa ( &fcoe->port_wwn.fc ) );
  1010. /* Transfer reference to port list */
  1011. list_add ( &fcoe->list, &fcoe_ports );
  1012. return 0;
  1013. netdev_put ( fcoe->netdev );
  1014. err_zalloc:
  1015. err_non_ethernet:
  1016. return rc;
  1017. }
  1018. /**
  1019. * Handle FCoE port device or link state change
  1020. *
  1021. * @v netdev Network device
  1022. */
  1023. static void fcoe_notify ( struct net_device *netdev ) {
  1024. struct fcoe_port *fcoe;
  1025. /* Sanity check */
  1026. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  1027. DBG ( "FCoE notification for net device %s missing FCoE "
  1028. "port\n", netdev->name );
  1029. return;
  1030. }
  1031. /* Reset the FCoE link if necessary */
  1032. if ( ! ( netdev_is_open ( netdev ) &&
  1033. netdev_link_ok ( netdev ) &&
  1034. ( fcoe->flags & FCOE_HAVE_NETWORK ) ) ) {
  1035. fcoe_reset ( fcoe );
  1036. }
  1037. }
  1038. /**
  1039. * Destroy FCoE port
  1040. *
  1041. * @v netdev Network device
  1042. */
  1043. static void fcoe_remove ( struct net_device *netdev ) {
  1044. struct fcoe_port *fcoe;
  1045. /* Sanity check */
  1046. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  1047. DBG ( "FCoE removal of net device %s missing FCoE port\n",
  1048. netdev->name );
  1049. return;
  1050. }
  1051. /* Close FCoE device */
  1052. fcoe_close ( fcoe, 0 );
  1053. }
  1054. /** FCoE driver */
  1055. struct net_driver fcoe_driver __net_driver = {
  1056. .name = "FCoE",
  1057. .probe = fcoe_probe,
  1058. .notify = fcoe_notify,
  1059. .remove = fcoe_remove,
  1060. };
  1061. /** FCoE protocol */
  1062. struct net_protocol fcoe_protocol __net_protocol = {
  1063. .name = "FCoE",
  1064. .net_proto = htons ( ETH_P_FCOE ),
  1065. .rx = fcoe_rx,
  1066. };
  1067. /** FIP protocol */
  1068. struct net_protocol fip_protocol __net_protocol = {
  1069. .name = "FIP",
  1070. .net_proto = htons ( ETH_P_FIP ),
  1071. .rx = fcoe_fip_rx,
  1072. };
  1073. /** Human-readable message for CRC errors
  1074. *
  1075. * It seems as though several drivers neglect to strip the Ethernet
  1076. * CRC, which will cause the FCoE footer to be misplaced and result
  1077. * (coincidentally) in an "invalid CRC" error from FCoE.
  1078. */
  1079. struct errortab fcoe_errors[] __errortab = {
  1080. __einfo_errortab ( EINFO_EINVAL_CRC ),
  1081. };