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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. * @ret rc Return status code
  290. */
  291. static int fcoe_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  292. const void *ll_dest, const void *ll_source ) {
  293. struct fcoe_header *fcoehdr;
  294. struct fcoe_footer *fcoeftr;
  295. struct fcoe_port *fcoe;
  296. int rc;
  297. /* Identify FCoE port */
  298. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  299. DBG ( "FCoE received frame for net device %s missing FCoE "
  300. "port\n", netdev->name );
  301. rc = -ENOTCONN;
  302. goto done;
  303. }
  304. /* Discard packets not destined for us */
  305. if ( ( memcmp ( fcoe->local_mac, ll_dest,
  306. sizeof ( fcoe->local_mac ) ) != 0 ) &&
  307. ( memcmp ( default_fcf_mac, ll_dest,
  308. sizeof ( default_fcf_mac ) ) != 0 ) ) {
  309. DBGC2 ( fcoe, "FCoE %s ignoring packet for %s\n",
  310. fcoe->netdev->name, eth_ntoa ( ll_dest ) );
  311. rc = -ENOTCONN;
  312. goto done;
  313. }
  314. /* Sanity check */
  315. if ( iob_len ( iobuf ) < ( sizeof ( *fcoehdr ) + sizeof ( *fcoeftr ) )){
  316. DBGC ( fcoe, "FCoE %s received under-length frame (%zd "
  317. "bytes)\n", fcoe->netdev->name, iob_len ( iobuf ) );
  318. rc = -EINVAL_UNDERLENGTH;
  319. goto done;
  320. }
  321. /* Strip header and footer */
  322. fcoehdr = iobuf->data;
  323. iob_pull ( iobuf, sizeof ( *fcoehdr ) );
  324. fcoeftr = ( iobuf->data + iob_len ( iobuf ) - sizeof ( *fcoeftr ) );
  325. iob_unput ( iobuf, sizeof ( *fcoeftr ) );
  326. /* Validity checks */
  327. if ( fcoehdr->version != FCOE_FRAME_VER ) {
  328. DBGC ( fcoe, "FCoE %s received unsupported frame version "
  329. "%02x\n", fcoe->netdev->name, fcoehdr->version );
  330. rc = -EPROTONOSUPPORT;
  331. goto done;
  332. }
  333. if ( ! ( ( fcoehdr->sof == FCOE_SOF_I3 ) ||
  334. ( fcoehdr->sof == FCOE_SOF_N3 ) ) ) {
  335. DBGC ( fcoe, "FCoE %s received unsupported start-of-frame "
  336. "delimiter %02x\n", fcoe->netdev->name, fcoehdr->sof );
  337. rc = -EINVAL_SOF;
  338. goto done;
  339. }
  340. if ( ( le32_to_cpu ( fcoeftr->crc ) ^ ~((uint32_t)0) ) !=
  341. crc32_le ( ~((uint32_t)0), iobuf->data, iob_len ( iobuf ) ) ) {
  342. DBGC ( fcoe, "FCoE %s received invalid CRC\n",
  343. fcoe->netdev->name );
  344. rc = -EINVAL_CRC;
  345. goto done;
  346. }
  347. if ( ! ( ( fcoeftr->eof == FCOE_EOF_N ) ||
  348. ( fcoeftr->eof == FCOE_EOF_T ) ) ) {
  349. DBGC ( fcoe, "FCoE %s received unsupported end-of-frame "
  350. "delimiter %02x\n", fcoe->netdev->name, fcoeftr->eof );
  351. rc = -EINVAL_EOF;
  352. goto done;
  353. }
  354. /* Record FCF address if applicable */
  355. if ( ( fcoe->flags & FCOE_HAVE_FCF ) &&
  356. ( ! ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ) ) {
  357. memcpy ( &fcoe->fcf_mac, ll_source, sizeof ( fcoe->fcf_mac ) );
  358. }
  359. /* Hand off via transport interface */
  360. if ( ( rc = xfer_deliver_iob ( &fcoe->transport,
  361. iob_disown ( iobuf ) ) ) != 0 ) {
  362. DBGC ( fcoe, "FCoE %s could not deliver frame: %s\n",
  363. fcoe->netdev->name, strerror ( rc ) );
  364. goto done;
  365. }
  366. done:
  367. free_iob ( iobuf );
  368. return rc;
  369. }
  370. /**
  371. * Check FCoE flow control window
  372. *
  373. * @v fcoe FCoE port
  374. * @ret len Length of window
  375. */
  376. static size_t fcoe_window ( struct fcoe_port *fcoe ) {
  377. return ( ( fcoe->flags & FCOE_HAVE_FCF ) ? ~( ( size_t ) 0 ) : 0 );
  378. }
  379. /**
  380. * Close FCoE port
  381. *
  382. * @v fcoe FCoE port
  383. * @v rc Reason for close
  384. */
  385. static void fcoe_close ( struct fcoe_port *fcoe, int rc ) {
  386. stop_timer ( &fcoe->timer );
  387. intf_shutdown ( &fcoe->transport, rc );
  388. netdev_put ( fcoe->netdev );
  389. list_del ( &fcoe->list );
  390. ref_put ( &fcoe->refcnt );
  391. }
  392. /**
  393. * Identify device underlying FCoE port
  394. *
  395. * @v fcoe FCoE port
  396. * @ret device Underlying device
  397. */
  398. static struct device * fcoe_identify_device ( struct fcoe_port *fcoe ) {
  399. return fcoe->netdev->dev;
  400. }
  401. /** FCoE transport interface operations */
  402. static struct interface_operation fcoe_transport_op[] = {
  403. INTF_OP ( xfer_deliver, struct fcoe_port *, fcoe_deliver ),
  404. INTF_OP ( xfer_alloc_iob, struct fcoe_port *, fcoe_alloc_iob ),
  405. INTF_OP ( xfer_window, struct fcoe_port *, fcoe_window ),
  406. INTF_OP ( intf_close, struct fcoe_port *, fcoe_close ),
  407. INTF_OP ( identify_device, struct fcoe_port *,
  408. fcoe_identify_device ),
  409. };
  410. /** FCoE transport interface descriptor */
  411. static struct interface_descriptor fcoe_transport_desc =
  412. INTF_DESC ( struct fcoe_port, transport, fcoe_transport_op );
  413. /******************************************************************************
  414. *
  415. * FIP protocol
  416. *
  417. ******************************************************************************
  418. */
  419. /**
  420. * Parse FIP packet into descriptor set
  421. *
  422. * @v fcoe FCoE port
  423. * @v fiphdr FIP header
  424. * @v len Length of FIP packet
  425. * @v descs Descriptor set to fill in
  426. * @ret rc Return status code
  427. */
  428. static int fcoe_fip_parse ( struct fcoe_port *fcoe, struct fip_header *fiphdr,
  429. size_t len, struct fip_descriptors *descs ) {
  430. union fip_descriptor *desc;
  431. size_t descs_len;
  432. size_t desc_len;
  433. size_t desc_offset;
  434. unsigned int desc_type;
  435. /* Check FIP version */
  436. if ( fiphdr->version != FIP_VERSION ) {
  437. DBGC ( fcoe, "FCoE %s received unsupported FIP version %02x\n",
  438. fcoe->netdev->name, fiphdr->version );
  439. return -EINVAL;
  440. }
  441. /* Check length */
  442. descs_len = ( ntohs ( fiphdr->len ) * 4 );
  443. if ( ( sizeof ( *fiphdr ) + descs_len ) > len ) {
  444. DBGC ( fcoe, "FCoE %s received bad descriptor list length\n",
  445. fcoe->netdev->name );
  446. return -EINVAL;
  447. }
  448. /* Parse descriptor list */
  449. memset ( descs, 0, sizeof ( *descs ) );
  450. for ( desc_offset = 0 ;
  451. desc_offset <= ( descs_len - sizeof ( desc->common ) ) ;
  452. desc_offset += desc_len ) {
  453. /* Find descriptor and validate length */
  454. desc = ( ( ( void * ) ( fiphdr + 1 ) ) + desc_offset );
  455. desc_type = desc->common.type;
  456. desc_len = ( desc->common.len * 4 );
  457. if ( desc_len == 0 ) {
  458. DBGC ( fcoe, "FCoE %s received zero-length "
  459. "descriptor\n", fcoe->netdev->name );
  460. return -EINVAL;
  461. }
  462. if ( ( desc_offset + desc_len ) > descs_len ) {
  463. DBGC ( fcoe, "FCoE %s descriptor overrun\n",
  464. fcoe->netdev->name );
  465. return -EINVAL;
  466. }
  467. /* Handle descriptors that we understand */
  468. if ( ( desc_type > FIP_RESERVED ) &&
  469. ( desc_type < FIP_NUM_DESCRIPTOR_TYPES ) ) {
  470. /* Use only the first instance of a descriptor */
  471. if ( descs->desc[desc_type] == NULL )
  472. descs->desc[desc_type] = desc;
  473. continue;
  474. }
  475. /* Abort if we cannot understand a critical descriptor */
  476. if ( FIP_IS_CRITICAL ( desc_type ) ) {
  477. DBGC ( fcoe, "FCoE %s cannot understand critical "
  478. "descriptor type %02x\n",
  479. fcoe->netdev->name, desc_type );
  480. return -ENOTSUP;
  481. }
  482. /* Ignore non-critical descriptors that we cannot understand */
  483. }
  484. return 0;
  485. }
  486. /**
  487. * Send FIP VLAN request
  488. *
  489. * @v fcoe FCoE port
  490. * @ret rc Return status code
  491. */
  492. static int fcoe_fip_tx_vlan ( struct fcoe_port *fcoe ) {
  493. struct io_buffer *iobuf;
  494. struct {
  495. struct fip_header hdr;
  496. struct fip_mac_address mac_address;
  497. } __attribute__ (( packed )) *request;
  498. int rc;
  499. /* Allocate I/O buffer */
  500. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *request ) );
  501. if ( ! iobuf )
  502. return -ENOMEM;
  503. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  504. /* Construct VLAN request */
  505. request = iob_put ( iobuf, sizeof ( *request ) );
  506. memset ( request, 0, sizeof ( *request ) );
  507. request->hdr.version = FIP_VERSION;
  508. request->hdr.code = htons ( FIP_CODE_VLAN );
  509. request->hdr.subcode = FIP_VLAN_REQUEST;
  510. request->hdr.len = htons ( ( sizeof ( *request ) -
  511. sizeof ( request->hdr ) ) / 4 );
  512. request->mac_address.type = FIP_MAC_ADDRESS;
  513. request->mac_address.len =
  514. ( sizeof ( request->mac_address ) / 4 );
  515. memcpy ( request->mac_address.mac, fcoe->netdev->ll_addr,
  516. sizeof ( request->mac_address.mac ) );
  517. /* Send VLAN request */
  518. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev,
  519. &fip_protocol, all_fcf_macs,
  520. fcoe->netdev->ll_addr ) ) != 0 ) {
  521. DBGC ( fcoe, "FCoE %s could not send VLAN request: "
  522. "%s\n", fcoe->netdev->name, strerror ( rc ) );
  523. return rc;
  524. }
  525. return 0;
  526. }
  527. /**
  528. * Handle received FIP VLAN notification
  529. *
  530. * @v fcoe FCoE port
  531. * @v descs Descriptor list
  532. * @v flags Flags
  533. * @ret rc Return status code
  534. */
  535. static int fcoe_fip_rx_vlan ( struct fcoe_port *fcoe,
  536. struct fip_descriptors *descs,
  537. unsigned int flags __unused ) {
  538. struct fip_mac_address *mac_address = fip_mac_address ( descs );
  539. struct fip_vlan *vlan = fip_vlan ( descs );
  540. unsigned int tag;
  541. int rc;
  542. /* Sanity checks */
  543. if ( ! mac_address ) {
  544. DBGC ( fcoe, "FCoE %s received VLAN notification missing MAC "
  545. "address\n", fcoe->netdev->name );
  546. return -EINVAL;
  547. }
  548. if ( ! vlan ) {
  549. DBGC ( fcoe, "FCoE %s received VLAN notification missing VLAN "
  550. "tag\n", fcoe->netdev->name );
  551. return -EINVAL;
  552. }
  553. /* Create VLAN */
  554. tag = ntohs ( vlan->vlan );
  555. DBGC ( fcoe, "FCoE %s creating VLAN %d for FCF %s\n",
  556. fcoe->netdev->name, tag, eth_ntoa ( mac_address->mac ) );
  557. if ( ( rc = vlan_create ( fcoe->netdev, tag,
  558. FCOE_VLAN_PRIORITY ) ) != 0 ) {
  559. DBGC ( fcoe, "FCoE %s could not create VLAN %d: %s\n",
  560. fcoe->netdev->name, tag, strerror ( rc ) );
  561. return rc;
  562. }
  563. /* Record that a VLAN was found. This FCoE port will play no
  564. * further active role; the real FCoE traffic will use the
  565. * port automatically created for the new VLAN device.
  566. */
  567. fcoe->flags |= FCOE_VLAN_FOUND;
  568. return 0;
  569. }
  570. /**
  571. * Send FIP discovery solicitation
  572. *
  573. * @v fcoe FCoE port
  574. * @ret rc Return status code
  575. */
  576. static int fcoe_fip_tx_solicitation ( struct fcoe_port *fcoe ) {
  577. struct io_buffer *iobuf;
  578. struct {
  579. struct fip_header hdr;
  580. struct fip_mac_address mac_address;
  581. struct fip_name_id name_id;
  582. struct fip_max_fcoe_size max_fcoe_size;
  583. } __attribute__ (( packed )) *solicitation;
  584. int rc;
  585. /* Allocate I/O buffer */
  586. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *solicitation ) );
  587. if ( ! iobuf )
  588. return -ENOMEM;
  589. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  590. /* Construct discovery solicitation */
  591. solicitation = iob_put ( iobuf, sizeof ( *solicitation ) );
  592. memset ( solicitation, 0, sizeof ( *solicitation ) );
  593. solicitation->hdr.version = FIP_VERSION;
  594. solicitation->hdr.code = htons ( FIP_CODE_DISCOVERY );
  595. solicitation->hdr.subcode = FIP_DISCOVERY_SOLICIT;
  596. solicitation->hdr.len = htons ( ( sizeof ( *solicitation ) -
  597. sizeof ( solicitation->hdr ) ) / 4 );
  598. solicitation->hdr.flags = htons ( FIP_FP | FIP_SP );
  599. solicitation->mac_address.type = FIP_MAC_ADDRESS;
  600. solicitation->mac_address.len =
  601. ( sizeof ( solicitation->mac_address ) / 4 );
  602. memcpy ( solicitation->mac_address.mac, fcoe->netdev->ll_addr,
  603. sizeof ( solicitation->mac_address.mac ) );
  604. solicitation->name_id.type = FIP_NAME_ID;
  605. solicitation->name_id.len = ( sizeof ( solicitation->name_id ) / 4 );
  606. memcpy ( &solicitation->name_id.name, &fcoe->node_wwn.fc,
  607. sizeof ( solicitation->name_id.name ) );
  608. solicitation->max_fcoe_size.type = FIP_MAX_FCOE_SIZE;
  609. solicitation->max_fcoe_size.len =
  610. ( sizeof ( solicitation->max_fcoe_size ) / 4 );
  611. solicitation->max_fcoe_size.mtu =
  612. htons ( ETH_MAX_MTU - sizeof ( struct fcoe_header ) -
  613. sizeof ( struct fcoe_footer ) );
  614. /* Send discovery solicitation */
  615. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev,
  616. &fip_protocol, all_fcf_macs,
  617. fcoe->netdev->ll_addr ) ) != 0 ) {
  618. DBGC ( fcoe, "FCoE %s could not send discovery solicitation: "
  619. "%s\n", fcoe->netdev->name, strerror ( rc ) );
  620. return rc;
  621. }
  622. return 0;
  623. }
  624. /**
  625. * Handle received FIP discovery advertisement
  626. *
  627. * @v fcoe FCoE port
  628. * @v descs Descriptor list
  629. * @v flags Flags
  630. * @ret rc Return status code
  631. */
  632. static int fcoe_fip_rx_advertisement ( struct fcoe_port *fcoe,
  633. struct fip_descriptors *descs,
  634. unsigned int flags ) {
  635. struct fip_priority *priority = fip_priority ( descs );
  636. struct fip_mac_address *mac_address = fip_mac_address ( descs );
  637. struct fip_fka_adv_p *fka_adv_p = fip_fka_adv_p ( descs );
  638. /* Sanity checks */
  639. if ( ! priority ) {
  640. DBGC ( fcoe, "FCoE %s received advertisement missing "
  641. "priority\n", fcoe->netdev->name );
  642. return -EINVAL;
  643. }
  644. if ( ! mac_address ) {
  645. DBGC ( fcoe, "FCoE %s received advertisement missing MAC "
  646. "address\n", fcoe->netdev->name );
  647. return -EINVAL;
  648. }
  649. if ( ! fka_adv_p ) {
  650. DBGC ( fcoe, "FCoE %s received advertisement missing FKA ADV "
  651. "period\n", fcoe->netdev->name );
  652. return -EINVAL;
  653. }
  654. if ( ! ( fcoe->flags & FCOE_HAVE_FCF ) ) {
  655. /* We are soliciting for an FCF. Store the highest
  656. * (i.e. lowest-valued) priority solicited
  657. * advertisement that we receive.
  658. */
  659. if ( ( ( flags & ( FIP_A | FIP_S | FIP_F ) ) ==
  660. ( FIP_A | FIP_S | FIP_F ) ) &&
  661. ( priority->priority < fcoe->priority ) ) {
  662. fcoe->flags |= FCOE_HAVE_FIP_FCF;
  663. fcoe->priority = priority->priority;
  664. if ( fka_adv_p->flags & FIP_NO_KEEPALIVE ) {
  665. fcoe->keepalive = 0;
  666. } else {
  667. fcoe->keepalive = ntohl ( fka_adv_p->period );
  668. }
  669. fcoe->flags &= ~FCOE_FCF_ALLOWS_SPMA;
  670. if ( flags & FIP_SP )
  671. fcoe->flags |= FCOE_FCF_ALLOWS_SPMA;
  672. memcpy ( fcoe->fcf_mac, mac_address->mac,
  673. sizeof ( fcoe->fcf_mac ) );
  674. DBGC ( fcoe, "FCoE %s selected FCF %s (pri %d",
  675. fcoe->netdev->name, eth_ntoa ( fcoe->fcf_mac ),
  676. fcoe->priority );
  677. if ( fcoe->keepalive ) {
  678. DBGC ( fcoe, ", FKA ADV %dms",
  679. fcoe->keepalive );
  680. }
  681. DBGC ( fcoe, ", %cPMA)\n",
  682. ( ( fcoe->flags & FCOE_FCF_ALLOWS_SPMA ) ?
  683. 'S' : 'F' ) );
  684. }
  685. } else if ( fcoe->flags & FCOE_HAVE_FIP_FCF ) {
  686. /* We are checking that the FCF remains alive. Reset
  687. * the timeout counter if this is an advertisement
  688. * from our forwarder.
  689. */
  690. if ( memcmp ( fcoe->fcf_mac, mac_address->mac,
  691. sizeof ( fcoe->fcf_mac ) ) == 0 ) {
  692. fcoe->timeouts = 0;
  693. }
  694. } else {
  695. /* We are operating in non-FIP mode and have received
  696. * a FIP advertisement. Reset the link in order to
  697. * attempt FIP.
  698. */
  699. fcoe_reset ( fcoe );
  700. }
  701. return 0;
  702. }
  703. /**
  704. * Handle received FIP ELS response
  705. *
  706. * @v fcoe FCoE port
  707. * @v descs Descriptor list
  708. * @v flags Flags
  709. * @ret rc Return status code
  710. */
  711. static int fcoe_fip_rx_els_response ( struct fcoe_port *fcoe,
  712. struct fip_descriptors *descs,
  713. unsigned int flags __unused ) {
  714. struct fip_els *flogi = fip_flogi ( descs );
  715. struct fip_mac_address *mac_address = fip_mac_address ( descs );
  716. void *frame;
  717. size_t frame_len;
  718. int rc;
  719. /* Sanity checks */
  720. if ( ! flogi ) {
  721. DBGC ( fcoe, "FCoE %s received ELS response missing FLOGI\n",
  722. fcoe->netdev->name );
  723. return -EINVAL;
  724. }
  725. if ( ! mac_address ) {
  726. DBGC ( fcoe, "FCoE %s received ELS response missing MAC "
  727. "address\n", fcoe->netdev->name );
  728. return -EINVAL;
  729. }
  730. /* Record local MAC address */
  731. memcpy ( fcoe->local_mac, mac_address->mac, sizeof ( fcoe->local_mac ));
  732. DBGC ( fcoe, "FCoE %s using local MAC %s\n",
  733. fcoe->netdev->name, eth_ntoa ( fcoe->local_mac ) );
  734. /* Hand off via transport interface */
  735. frame = &flogi->fc;
  736. frame_len = ( ( flogi->len * 4 ) - offsetof ( typeof ( *flogi ), fc ) );
  737. if ( ( rc = xfer_deliver_raw ( &fcoe->transport, frame,
  738. frame_len ) ) != 0 ) {
  739. DBGC ( fcoe, "FCoE %s could not deliver FIP FLOGI frame: %s\n",
  740. fcoe->netdev->name, strerror ( rc ) );
  741. return rc;
  742. }
  743. return 0;
  744. }
  745. /**
  746. * Send FIP keepalive
  747. *
  748. * @v fcoe FCoE port
  749. * @ret rc Return status code
  750. */
  751. static int fcoe_fip_tx_keepalive ( struct fcoe_port *fcoe ) {
  752. struct io_buffer *iobuf;
  753. struct {
  754. struct fip_header hdr;
  755. struct fip_mac_address mac_address;
  756. } __attribute__ (( packed )) *keepalive;
  757. int rc;
  758. /* Allocate I/O buffer */
  759. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *keepalive ) );
  760. if ( ! iobuf )
  761. return -ENOMEM;
  762. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  763. /* Construct keepalive */
  764. keepalive = iob_put ( iobuf, sizeof ( *keepalive ) );
  765. memset ( keepalive, 0, sizeof ( *keepalive ) );
  766. keepalive->hdr.version = FIP_VERSION;
  767. keepalive->hdr.code = htons ( FIP_CODE_MAINTAIN );
  768. keepalive->hdr.subcode = FIP_MAINTAIN_KEEP_ALIVE;
  769. keepalive->hdr.len = htons ( ( sizeof ( *keepalive ) -
  770. sizeof ( keepalive->hdr ) ) / 4 );
  771. keepalive->mac_address.type = FIP_MAC_ADDRESS;
  772. keepalive->mac_address.len =
  773. ( sizeof ( keepalive->mac_address ) / 4 );
  774. memcpy ( keepalive->mac_address.mac, fcoe->netdev->ll_addr,
  775. sizeof ( keepalive->mac_address.mac ) );
  776. /* Send keepalive */
  777. if ( ( rc = net_tx ( iob_disown ( iobuf ), fcoe->netdev,
  778. &fip_protocol, fcoe->fcf_mac,
  779. fcoe->netdev->ll_addr ) ) != 0 ) {
  780. DBGC ( fcoe, "FCoE %s could not send keepalive: %s\n",
  781. fcoe->netdev->name, strerror ( rc ) );
  782. return rc;
  783. }
  784. return 0;
  785. }
  786. /** A FIP handler */
  787. struct fip_handler {
  788. /** Protocol code */
  789. uint16_t code;
  790. /** Protocol subcode */
  791. uint8_t subcode;
  792. /**
  793. * Receive FIP packet
  794. *
  795. * @v fcoe FCoE port
  796. * @v descs Descriptor list
  797. * @v flags Flags
  798. * @ret rc Return status code
  799. */
  800. int ( * rx ) ( struct fcoe_port *fcoe, struct fip_descriptors *descs,
  801. unsigned int flags );
  802. };
  803. /** FIP handlers */
  804. static struct fip_handler fip_handlers[] = {
  805. { FIP_CODE_VLAN, FIP_VLAN_NOTIFY,
  806. fcoe_fip_rx_vlan },
  807. { FIP_CODE_DISCOVERY, FIP_DISCOVERY_ADVERTISE,
  808. fcoe_fip_rx_advertisement },
  809. { FIP_CODE_ELS, FIP_ELS_RESPONSE,
  810. fcoe_fip_rx_els_response },
  811. };
  812. /**
  813. * Process incoming FIP packets
  814. *
  815. * @v iobuf I/O buffer
  816. * @v netdev Network device
  817. * @v ll_dest Link-layer destination address
  818. * @v ll_source Link-layer source address
  819. * @ret rc Return status code
  820. */
  821. static int fcoe_fip_rx ( struct io_buffer *iobuf,
  822. struct net_device *netdev,
  823. const void *ll_dest,
  824. const void *ll_source __unused ) {
  825. struct fip_header *fiphdr = iobuf->data;
  826. struct fip_descriptors descs;
  827. struct fip_handler *handler;
  828. struct fcoe_port *fcoe;
  829. unsigned int i;
  830. int rc;
  831. /* Identify FCoE port */
  832. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  833. DBG ( "FCoE received FIP frame for net device %s missing FCoE "
  834. "port\n", netdev->name );
  835. rc = -ENOTCONN;
  836. goto done;
  837. }
  838. /* Discard packets not destined for us */
  839. if ( ( memcmp ( fcoe->netdev->ll_addr, ll_dest, ETH_ALEN ) != 0 ) &&
  840. ( memcmp ( all_fcoe_macs, ll_dest,
  841. sizeof ( all_fcoe_macs ) ) != 0 ) &&
  842. ( memcmp ( all_enode_macs, ll_dest,
  843. sizeof ( all_enode_macs ) ) != 0 ) ) {
  844. DBGC2 ( fcoe, "FCoE %s ignoring FIP packet for %s\n",
  845. fcoe->netdev->name, eth_ntoa ( ll_dest ) );
  846. rc = -ENOTCONN;
  847. goto done;
  848. }
  849. /* Parse FIP packet */
  850. if ( ( rc = fcoe_fip_parse ( fcoe, fiphdr, iob_len ( iobuf ),
  851. &descs ) ) != 0 )
  852. goto done;
  853. /* Find a suitable handler */
  854. for ( i = 0 ; i < ( sizeof ( fip_handlers ) /
  855. sizeof ( fip_handlers[0] ) ) ; i++ ) {
  856. handler = &fip_handlers[i];
  857. if ( ( handler->code == ntohs ( fiphdr->code ) ) &&
  858. ( handler->subcode == fiphdr->subcode ) ) {
  859. rc = handler->rx ( fcoe, &descs,
  860. ntohs ( fiphdr->flags ) );
  861. goto done;
  862. }
  863. }
  864. DBGC ( fcoe, "FCoE %s received unsupported FIP code %04x.%02x\n",
  865. fcoe->netdev->name, ntohs ( fiphdr->code ), fiphdr->subcode );
  866. rc = -ENOTSUP;
  867. done:
  868. free_iob ( iobuf );
  869. return rc;
  870. }
  871. /******************************************************************************
  872. *
  873. * FCoE ports
  874. *
  875. ******************************************************************************
  876. */
  877. /**
  878. * Handle FCoE timer expiry
  879. *
  880. * @v timer FIP timer
  881. * @v over Timer expired
  882. */
  883. static void fcoe_expired ( struct retry_timer *timer, int over __unused ) {
  884. struct fcoe_port *fcoe =
  885. container_of ( timer, struct fcoe_port, timer );
  886. int rc;
  887. /* Sanity check */
  888. assert ( fcoe->flags & FCOE_HAVE_NETWORK );
  889. /* Increment the timeout counter */
  890. fcoe->timeouts++;
  891. if ( vlan_can_be_trunk ( fcoe->netdev ) &
  892. ! ( fcoe->flags & FCOE_VLAN_TIMED_OUT ) ) {
  893. /* If we have already found a VLAN, send infrequent
  894. * VLAN requests, in case VLAN information changes.
  895. */
  896. if ( fcoe->flags & FCOE_VLAN_FOUND ) {
  897. fcoe->flags &= ~FCOE_VLAN_FOUND;
  898. fcoe->timeouts = 0;
  899. start_timer_fixed ( &fcoe->timer,
  900. FCOE_VLAN_POLL_DELAY );
  901. fcoe_fip_tx_vlan ( fcoe );
  902. return;
  903. }
  904. /* If we have not yet found a VLAN, and we have not
  905. * yet timed out and given up on finding one, then
  906. * send a VLAN request and wait.
  907. */
  908. if ( fcoe->timeouts <= FCOE_MAX_VLAN_REQUESTS ) {
  909. start_timer_fixed ( &fcoe->timer,
  910. FCOE_VLAN_RETRY_DELAY );
  911. fcoe_fip_tx_vlan ( fcoe );
  912. return;
  913. }
  914. /* We have timed out waiting for a VLAN; proceed to
  915. * FIP discovery.
  916. */
  917. fcoe->flags |= FCOE_VLAN_TIMED_OUT;
  918. fcoe->timeouts = 0;
  919. DBGC ( fcoe, "FCoE %s giving up on VLAN discovery\n",
  920. fcoe->netdev->name );
  921. start_timer_nodelay ( &fcoe->timer );
  922. } else if ( ! ( fcoe->flags & FCOE_HAVE_FCF ) ) {
  923. /* If we have not yet found a FIP-capable forwarder,
  924. * and we have not yet timed out and given up on
  925. * finding one, then send a FIP solicitation and wait.
  926. */
  927. start_timer_fixed ( &fcoe->timer, FCOE_FIP_RETRY_DELAY );
  928. if ( ( ! ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ) &&
  929. ( fcoe->timeouts <= FCOE_MAX_FIP_SOLICITATIONS ) ) {
  930. fcoe_fip_tx_solicitation ( fcoe );
  931. return;
  932. }
  933. /* Attach Fibre Channel port */
  934. if ( ( rc = fc_port_open ( &fcoe->transport, &fcoe->node_wwn.fc,
  935. &fcoe->port_wwn.fc,
  936. fcoe->netdev->name ) ) != 0 ) {
  937. DBGC ( fcoe, "FCoE %s could not create FC port: %s\n",
  938. fcoe->netdev->name, strerror ( rc ) );
  939. /* We will try again on the next timer expiry */
  940. return;
  941. }
  942. stop_timer ( &fcoe->timer );
  943. /* Either we have found a FIP-capable forwarder, or we
  944. * have timed out and will fall back to pre-FIP mode.
  945. */
  946. fcoe->flags |= FCOE_HAVE_FCF;
  947. fcoe->timeouts = 0;
  948. DBGC ( fcoe, "FCoE %s using %sFIP FCF %s\n", fcoe->netdev->name,
  949. ( ( fcoe->flags & FCOE_HAVE_FIP_FCF ) ? "" : "non-" ),
  950. eth_ntoa ( fcoe->fcf_mac ) );
  951. /* Start sending keepalives if applicable */
  952. if ( fcoe->keepalive )
  953. start_timer_nodelay ( &fcoe->timer );
  954. /* Send notification of window change */
  955. xfer_window_changed ( &fcoe->transport );
  956. } else {
  957. /* Send keepalive */
  958. start_timer_fixed ( &fcoe->timer,
  959. ( ( fcoe->keepalive * TICKS_PER_SEC ) / 1000 ) );
  960. fcoe_fip_tx_keepalive ( fcoe );
  961. /* Abandon FCF if we have not seen its advertisements */
  962. if ( fcoe->timeouts > FCOE_MAX_FIP_MISSING_KEEPALIVES ) {
  963. DBGC ( fcoe, "FCoE %s abandoning FCF %s\n",
  964. fcoe->netdev->name, eth_ntoa ( fcoe->fcf_mac ));
  965. fcoe_reset ( fcoe );
  966. }
  967. }
  968. }
  969. /**
  970. * Create FCoE port
  971. *
  972. * @v netdev Network device
  973. * @ret rc Return status code
  974. */
  975. static int fcoe_probe ( struct net_device *netdev ) {
  976. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  977. struct fcoe_port *fcoe;
  978. int rc;
  979. /* Sanity check */
  980. if ( ll_protocol->ll_proto != htons ( ARPHRD_ETHER ) ) {
  981. /* Not an error; simply skip this net device */
  982. DBG ( "FCoE skipping non-Ethernet device %s\n", netdev->name );
  983. rc = 0;
  984. goto err_non_ethernet;
  985. }
  986. /* Allocate and initialise structure */
  987. fcoe = zalloc ( sizeof ( *fcoe ) );
  988. if ( ! fcoe ) {
  989. rc = -ENOMEM;
  990. goto err_zalloc;
  991. }
  992. ref_init ( &fcoe->refcnt, NULL );
  993. intf_init ( &fcoe->transport, &fcoe_transport_desc, &fcoe->refcnt );
  994. timer_init ( &fcoe->timer, fcoe_expired, &fcoe->refcnt );
  995. fcoe->netdev = netdev_get ( netdev );
  996. /* Construct node and port names */
  997. fcoe->node_wwn.fcoe.authority = htons ( FCOE_AUTHORITY_IEEE );
  998. memcpy ( &fcoe->node_wwn.fcoe.mac, netdev->ll_addr,
  999. sizeof ( fcoe->node_wwn.fcoe.mac ) );
  1000. fcoe->port_wwn.fcoe.authority = htons ( FCOE_AUTHORITY_IEEE_EXTENDED );
  1001. memcpy ( &fcoe->port_wwn.fcoe.mac, netdev->ll_addr,
  1002. sizeof ( fcoe->port_wwn.fcoe.mac ) );
  1003. DBGC ( fcoe, "FCoE %s is %s", fcoe->netdev->name,
  1004. fc_ntoa ( &fcoe->node_wwn.fc ) );
  1005. DBGC ( fcoe, " port %s\n", fc_ntoa ( &fcoe->port_wwn.fc ) );
  1006. /* Transfer reference to port list */
  1007. list_add ( &fcoe->list, &fcoe_ports );
  1008. return 0;
  1009. netdev_put ( fcoe->netdev );
  1010. err_zalloc:
  1011. err_non_ethernet:
  1012. return rc;
  1013. }
  1014. /**
  1015. * Handle FCoE port device or link state change
  1016. *
  1017. * @v netdev Network device
  1018. */
  1019. static void fcoe_notify ( struct net_device *netdev ) {
  1020. struct fcoe_port *fcoe;
  1021. /* Sanity check */
  1022. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  1023. DBG ( "FCoE notification for net device %s missing FCoE "
  1024. "port\n", netdev->name );
  1025. return;
  1026. }
  1027. /* Reset the FCoE link if necessary */
  1028. if ( ! ( netdev_is_open ( netdev ) &&
  1029. netdev_link_ok ( netdev ) &&
  1030. ( fcoe->flags & FCOE_HAVE_NETWORK ) ) ) {
  1031. fcoe_reset ( fcoe );
  1032. }
  1033. }
  1034. /**
  1035. * Destroy FCoE port
  1036. *
  1037. * @v netdev Network device
  1038. */
  1039. static void fcoe_remove ( struct net_device *netdev ) {
  1040. struct fcoe_port *fcoe;
  1041. /* Sanity check */
  1042. if ( ( fcoe = fcoe_demux ( netdev ) ) == NULL ) {
  1043. DBG ( "FCoE removal of net device %s missing FCoE port\n",
  1044. netdev->name );
  1045. return;
  1046. }
  1047. /* Close FCoE device */
  1048. fcoe_close ( fcoe, 0 );
  1049. }
  1050. /** FCoE driver */
  1051. struct net_driver fcoe_driver __net_driver = {
  1052. .name = "FCoE",
  1053. .probe = fcoe_probe,
  1054. .notify = fcoe_notify,
  1055. .remove = fcoe_remove,
  1056. };
  1057. /** FCoE protocol */
  1058. struct net_protocol fcoe_protocol __net_protocol = {
  1059. .name = "FCoE",
  1060. .net_proto = htons ( ETH_P_FCOE ),
  1061. .rx = fcoe_rx,
  1062. };
  1063. /** FIP protocol */
  1064. struct net_protocol fip_protocol __net_protocol = {
  1065. .name = "FIP",
  1066. .net_proto = htons ( ETH_P_FIP ),
  1067. .rx = fcoe_fip_rx,
  1068. };
  1069. /** Human-readable message for CRC errors
  1070. *
  1071. * It seems as though several drivers neglect to strip the Ethernet
  1072. * CRC, which will cause the FCoE footer to be misplaced and result
  1073. * (coincidentally) in an "invalid CRC" error from FCoE.
  1074. */
  1075. struct errortab fcoe_errors[] __errortab = {
  1076. __einfo_errortab ( EINFO_EINVAL_CRC ),
  1077. };