You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fcoe.c 35KB

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