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.

ipoib.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * Copyright (C) 2007 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 <stdint.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <byteswap.h>
  24. #include <errno.h>
  25. #include <ipxe/errortab.h>
  26. #include <ipxe/if_arp.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/netdevice.h>
  29. #include <ipxe/infiniband.h>
  30. #include <ipxe/ib_pathrec.h>
  31. #include <ipxe/ib_mcast.h>
  32. #include <ipxe/ipoib.h>
  33. /** @file
  34. *
  35. * IP over Infiniband
  36. */
  37. /** Number of IPoIB send work queue entries */
  38. #define IPOIB_NUM_SEND_WQES 2
  39. /** Number of IPoIB receive work queue entries */
  40. #define IPOIB_NUM_RECV_WQES 4
  41. /** Number of IPoIB completion entries */
  42. #define IPOIB_NUM_CQES 8
  43. /** An IPoIB device */
  44. struct ipoib_device {
  45. /** Network device */
  46. struct net_device *netdev;
  47. /** Underlying Infiniband device */
  48. struct ib_device *ibdev;
  49. /** Completion queue */
  50. struct ib_completion_queue *cq;
  51. /** Queue pair */
  52. struct ib_queue_pair *qp;
  53. /** Broadcast MAC */
  54. struct ipoib_mac broadcast;
  55. /** Joined to IPv4 broadcast multicast group
  56. *
  57. * This flag indicates whether or not we have initiated the
  58. * join to the IPv4 broadcast multicast group.
  59. */
  60. int broadcast_joined;
  61. /** IPv4 broadcast multicast group membership */
  62. struct ib_mc_membership broadcast_membership;
  63. };
  64. /** Broadcast IPoIB address */
  65. static struct ipoib_mac ipoib_broadcast = {
  66. .flags__qpn = htonl ( IB_QPN_BROADCAST ),
  67. .gid.bytes = { 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff },
  69. };
  70. /** Link status for "broadcast join in progress" */
  71. #define EINPROGRESS_JOINING __einfo_error ( EINFO_EINPROGRESS_JOINING )
  72. #define EINFO_EINPROGRESS_JOINING __einfo_uniqify \
  73. ( EINFO_EINPROGRESS, 0x01, "Joining" )
  74. /** Human-readable message for the link status */
  75. struct errortab ipoib_errors[] __errortab = {
  76. __einfo_errortab ( EINFO_EINPROGRESS_JOINING ),
  77. };
  78. /****************************************************************************
  79. *
  80. * IPoIB peer cache
  81. *
  82. ****************************************************************************
  83. */
  84. /**
  85. * IPoIB peer address
  86. *
  87. * The IPoIB link-layer header is only four bytes long and so does not
  88. * have sufficient room to store IPoIB MAC address(es). We therefore
  89. * maintain a cache of MAC addresses identified by a single-byte key,
  90. * and abuse the spare two bytes within the link-layer header to
  91. * communicate these MAC addresses between the link-layer code and the
  92. * netdevice driver.
  93. */
  94. struct ipoib_peer {
  95. /** Key */
  96. uint8_t key;
  97. /** MAC address */
  98. struct ipoib_mac mac;
  99. };
  100. /** Number of IPoIB peer cache entries
  101. *
  102. * Must be a power of two.
  103. */
  104. #define IPOIB_NUM_CACHED_PEERS 4
  105. /** IPoIB peer address cache */
  106. static struct ipoib_peer ipoib_peer_cache[IPOIB_NUM_CACHED_PEERS];
  107. /** Oldest IPoIB peer cache entry index */
  108. static unsigned int ipoib_peer_cache_idx = 0;
  109. /** IPoIB peer cache entry validity flag */
  110. #define IPOIB_PEER_KEY_VALID 0x80
  111. /**
  112. * Look up cached peer by key
  113. *
  114. * @v key Peer cache key
  115. * @ret peer Peer cache entry, or NULL
  116. */
  117. static struct ipoib_peer * ipoib_lookup_peer_by_key ( unsigned int key ) {
  118. struct ipoib_peer *peer;
  119. unsigned int i;
  120. if ( ! key )
  121. return NULL;
  122. for ( i = 0 ; i < IPOIB_NUM_CACHED_PEERS ; i++ ) {
  123. peer = &ipoib_peer_cache[i];
  124. if ( peer->key == key )
  125. return peer;
  126. }
  127. DBG ( "IPoIB warning: peer cache lost track of key %x while still in "
  128. "use\n", key );
  129. return NULL;
  130. }
  131. /**
  132. * Store GID and QPN in peer cache
  133. *
  134. * @v mac Peer MAC address
  135. * @ret peer Peer cache entry
  136. */
  137. static struct ipoib_peer * ipoib_cache_peer ( const struct ipoib_mac *mac ) {
  138. struct ipoib_peer *peer;
  139. uint8_t key;
  140. unsigned int i;
  141. /* Look for existing cache entry */
  142. for ( i = 0 ; i < IPOIB_NUM_CACHED_PEERS ; i++ ) {
  143. peer = &ipoib_peer_cache[i];
  144. if ( memcmp ( &peer->mac, mac, sizeof ( peer->mac ) ) == 0 )
  145. return peer;
  146. }
  147. /* No entry found: create a new one */
  148. key = ( ipoib_peer_cache_idx++ | IPOIB_PEER_KEY_VALID );
  149. peer = &ipoib_peer_cache[ key % IPOIB_NUM_CACHED_PEERS ];
  150. if ( peer->key )
  151. DBG ( "IPoIB peer %x evicted from cache\n", peer->key );
  152. memset ( peer, 0, sizeof ( *peer ) );
  153. peer->key = key;
  154. memcpy ( &peer->mac, mac, sizeof ( peer->mac ) );
  155. DBG ( "IPoIB peer %x has MAC %s\n",
  156. peer->key, ipoib_ntoa ( &peer->mac ) );
  157. return peer;
  158. }
  159. /****************************************************************************
  160. *
  161. * IPoIB link layer
  162. *
  163. ****************************************************************************
  164. */
  165. /**
  166. * Add IPoIB link-layer header
  167. *
  168. * @v netdev Network device
  169. * @v iobuf I/O buffer
  170. * @v ll_dest Link-layer destination address
  171. * @v ll_source Source link-layer address
  172. * @v net_proto Network-layer protocol, in network-byte order
  173. * @ret rc Return status code
  174. */
  175. static int ipoib_push ( struct net_device *netdev __unused,
  176. struct io_buffer *iobuf, const void *ll_dest,
  177. const void *ll_source __unused, uint16_t net_proto ) {
  178. struct ipoib_hdr *ipoib_hdr =
  179. iob_push ( iobuf, sizeof ( *ipoib_hdr ) );
  180. const struct ipoib_mac *dest_mac = ll_dest;
  181. const struct ipoib_mac *src_mac = ll_source;
  182. struct ipoib_peer *dest;
  183. struct ipoib_peer *src;
  184. /* Add link-layer addresses to cache */
  185. dest = ipoib_cache_peer ( dest_mac );
  186. src = ipoib_cache_peer ( src_mac );
  187. /* Build IPoIB header */
  188. ipoib_hdr->proto = net_proto;
  189. ipoib_hdr->u.peer.dest = dest->key;
  190. ipoib_hdr->u.peer.src = src->key;
  191. return 0;
  192. }
  193. /**
  194. * Remove IPoIB link-layer header
  195. *
  196. * @v netdev Network device
  197. * @v iobuf I/O buffer
  198. * @ret ll_dest Link-layer destination address
  199. * @ret ll_source Source link-layer address
  200. * @ret net_proto Network-layer protocol, in network-byte order
  201. * @ret flags Packet flags
  202. * @ret rc Return status code
  203. */
  204. static int ipoib_pull ( struct net_device *netdev,
  205. struct io_buffer *iobuf, const void **ll_dest,
  206. const void **ll_source, uint16_t *net_proto,
  207. unsigned int *flags ) {
  208. struct ipoib_device *ipoib = netdev->priv;
  209. struct ipoib_hdr *ipoib_hdr = iobuf->data;
  210. struct ipoib_peer *dest;
  211. struct ipoib_peer *source;
  212. /* Sanity check */
  213. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  214. DBG ( "IPoIB packet too short for link-layer header\n" );
  215. DBG_HD ( iobuf->data, iob_len ( iobuf ) );
  216. return -EINVAL;
  217. }
  218. /* Strip off IPoIB header */
  219. iob_pull ( iobuf, sizeof ( *ipoib_hdr ) );
  220. /* Identify source and destination addresses, and clear
  221. * reserved word in IPoIB header
  222. */
  223. dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
  224. source = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.src );
  225. ipoib_hdr->u.reserved = 0;
  226. /* Fill in required fields */
  227. *ll_dest = ( dest ? &dest->mac : &ipoib->broadcast );
  228. *ll_source = ( source ? &source->mac : &ipoib->broadcast );
  229. *net_proto = ipoib_hdr->proto;
  230. *flags = ( ( *ll_dest == &ipoib->broadcast ) ?
  231. ( LL_MULTICAST | LL_BROADCAST ) : 0 );
  232. return 0;
  233. }
  234. /**
  235. * Initialise IPoIB link-layer address
  236. *
  237. * @v hw_addr Hardware address
  238. * @v ll_addr Link-layer address
  239. */
  240. static void ipoib_init_addr ( const void *hw_addr, void *ll_addr ) {
  241. const union ib_guid *guid = hw_addr;
  242. struct ipoib_mac *mac = ll_addr;
  243. memset ( mac, 0, sizeof ( *mac ) );
  244. memcpy ( &mac->gid.s.guid, guid, sizeof ( mac->gid.s.guid ) );
  245. }
  246. /**
  247. * Transcribe IPoIB link-layer address
  248. *
  249. * @v ll_addr Link-layer address
  250. * @ret string Link-layer address in human-readable format
  251. */
  252. const char * ipoib_ntoa ( const void *ll_addr ) {
  253. static char buf[45];
  254. const struct ipoib_mac *mac = ll_addr;
  255. snprintf ( buf, sizeof ( buf ), "%08x:%08x:%08x:%08x:%08x",
  256. htonl ( mac->flags__qpn ), htonl ( mac->gid.dwords[0] ),
  257. htonl ( mac->gid.dwords[1] ),
  258. htonl ( mac->gid.dwords[2] ),
  259. htonl ( mac->gid.dwords[3] ) );
  260. return buf;
  261. }
  262. /**
  263. * Hash multicast address
  264. *
  265. * @v af Address family
  266. * @v net_addr Network-layer address
  267. * @v ll_addr Link-layer address to fill in
  268. * @ret rc Return status code
  269. */
  270. static int ipoib_mc_hash ( unsigned int af __unused,
  271. const void *net_addr __unused,
  272. void *ll_addr __unused ) {
  273. return -ENOTSUP;
  274. }
  275. /**
  276. * Generate Mellanox Ethernet-compatible compressed link-layer address
  277. *
  278. * @v ll_addr Link-layer address
  279. * @v eth_addr Ethernet-compatible address to fill in
  280. */
  281. static int ipoib_mlx_eth_addr ( const union ib_guid *guid,
  282. uint8_t *eth_addr ) {
  283. eth_addr[0] = ( ( guid->bytes[3] == 2 ) ? 0x00 : 0x02 );
  284. eth_addr[1] = guid->bytes[1];
  285. eth_addr[2] = guid->bytes[2];
  286. eth_addr[3] = guid->bytes[5];
  287. eth_addr[4] = guid->bytes[6];
  288. eth_addr[5] = guid->bytes[7];
  289. return 0;
  290. }
  291. /** An IPoIB Ethernet-compatible compressed link-layer address generator */
  292. struct ipoib_eth_addr_handler {
  293. /** GUID byte 1 */
  294. uint8_t byte1;
  295. /** GUID byte 2 */
  296. uint8_t byte2;
  297. /** Handler */
  298. int ( * eth_addr ) ( const union ib_guid *guid,
  299. uint8_t *eth_addr );
  300. };
  301. /** IPoIB Ethernet-compatible compressed link-layer address generators */
  302. static struct ipoib_eth_addr_handler ipoib_eth_addr_handlers[] = {
  303. { 0x02, 0xc9, ipoib_mlx_eth_addr },
  304. };
  305. /**
  306. * Generate Ethernet-compatible compressed link-layer address
  307. *
  308. * @v ll_addr Link-layer address
  309. * @v eth_addr Ethernet-compatible address to fill in
  310. */
  311. static int ipoib_eth_addr ( const void *ll_addr, void *eth_addr ) {
  312. const struct ipoib_mac *ipoib_addr = ll_addr;
  313. const union ib_guid *guid = &ipoib_addr->gid.s.guid;
  314. struct ipoib_eth_addr_handler *handler;
  315. unsigned int i;
  316. for ( i = 0 ; i < ( sizeof ( ipoib_eth_addr_handlers ) /
  317. sizeof ( ipoib_eth_addr_handlers[0] ) ) ; i++ ) {
  318. handler = &ipoib_eth_addr_handlers[i];
  319. if ( ( handler->byte1 == guid->bytes[1] ) &&
  320. ( handler->byte2 == guid->bytes[2] ) ) {
  321. return handler->eth_addr ( guid, eth_addr );
  322. }
  323. }
  324. return -ENOTSUP;
  325. }
  326. /** IPoIB protocol */
  327. struct ll_protocol ipoib_protocol __ll_protocol = {
  328. .name = "IPoIB",
  329. .ll_proto = htons ( ARPHRD_INFINIBAND ),
  330. .hw_addr_len = sizeof ( union ib_guid ),
  331. .ll_addr_len = IPOIB_ALEN,
  332. .ll_header_len = IPOIB_HLEN,
  333. .push = ipoib_push,
  334. .pull = ipoib_pull,
  335. .init_addr = ipoib_init_addr,
  336. .ntoa = ipoib_ntoa,
  337. .mc_hash = ipoib_mc_hash,
  338. .eth_addr = ipoib_eth_addr,
  339. };
  340. /**
  341. * Allocate IPoIB device
  342. *
  343. * @v priv_size Size of driver private data
  344. * @ret netdev Network device, or NULL
  345. */
  346. struct net_device * alloc_ipoibdev ( size_t priv_size ) {
  347. struct net_device *netdev;
  348. netdev = alloc_netdev ( priv_size );
  349. if ( netdev ) {
  350. netdev->ll_protocol = &ipoib_protocol;
  351. netdev->ll_broadcast = ( uint8_t * ) &ipoib_broadcast;
  352. netdev->max_pkt_len = IB_MAX_PAYLOAD_SIZE;
  353. }
  354. return netdev;
  355. }
  356. /****************************************************************************
  357. *
  358. * IPoIB network device
  359. *
  360. ****************************************************************************
  361. */
  362. /**
  363. * Transmit packet via IPoIB network device
  364. *
  365. * @v netdev Network device
  366. * @v iobuf I/O buffer
  367. * @ret rc Return status code
  368. */
  369. static int ipoib_transmit ( struct net_device *netdev,
  370. struct io_buffer *iobuf ) {
  371. struct ipoib_device *ipoib = netdev->priv;
  372. struct ib_device *ibdev = ipoib->ibdev;
  373. struct ipoib_hdr *ipoib_hdr;
  374. struct ipoib_peer *dest;
  375. struct ib_address_vector av;
  376. int rc;
  377. /* Sanity check */
  378. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  379. DBGC ( ipoib, "IPoIB %p buffer too short\n", ipoib );
  380. return -EINVAL;
  381. }
  382. ipoib_hdr = iobuf->data;
  383. /* Attempting transmission while link is down will put the
  384. * queue pair into an error state, so don't try it.
  385. */
  386. if ( ! ib_link_ok ( ibdev ) )
  387. return -ENETUNREACH;
  388. /* Identify destination address */
  389. dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
  390. if ( ! dest )
  391. return -ENXIO;
  392. ipoib_hdr->u.reserved = 0;
  393. /* Construct address vector */
  394. memset ( &av, 0, sizeof ( av ) );
  395. av.qpn = ( ntohl ( dest->mac.flags__qpn ) & IB_QPN_MASK );
  396. av.gid_present = 1;
  397. memcpy ( &av.gid, &dest->mac.gid, sizeof ( av.gid ) );
  398. if ( ( rc = ib_resolve_path ( ibdev, &av ) ) != 0 ) {
  399. /* Path not resolved yet */
  400. return rc;
  401. }
  402. return ib_post_send ( ibdev, ipoib->qp, &av, iobuf );
  403. }
  404. /**
  405. * Handle IPoIB send completion
  406. *
  407. * @v ibdev Infiniband device
  408. * @v qp Queue pair
  409. * @v iobuf I/O buffer
  410. * @v rc Completion status code
  411. */
  412. static void ipoib_complete_send ( struct ib_device *ibdev __unused,
  413. struct ib_queue_pair *qp,
  414. struct io_buffer *iobuf, int rc ) {
  415. struct ipoib_device *ipoib = ib_qp_get_ownerdata ( qp );
  416. netdev_tx_complete_err ( ipoib->netdev, iobuf, rc );
  417. }
  418. /**
  419. * Handle IPoIB receive completion
  420. *
  421. * @v ibdev Infiniband device
  422. * @v qp Queue pair
  423. * @v av Address vector, or NULL
  424. * @v iobuf I/O buffer
  425. * @v rc Completion status code
  426. */
  427. static void ipoib_complete_recv ( struct ib_device *ibdev __unused,
  428. struct ib_queue_pair *qp,
  429. struct ib_address_vector *av,
  430. struct io_buffer *iobuf, int rc ) {
  431. struct ipoib_device *ipoib = ib_qp_get_ownerdata ( qp );
  432. struct net_device *netdev = ipoib->netdev;
  433. struct ipoib_hdr *ipoib_hdr;
  434. struct ipoib_mac ll_src;
  435. struct ipoib_peer *src;
  436. /* Record errors */
  437. if ( rc != 0 ) {
  438. netdev_rx_err ( netdev, iobuf, rc );
  439. return;
  440. }
  441. /* Sanity check */
  442. if ( iob_len ( iobuf ) < sizeof ( struct ipoib_hdr ) ) {
  443. DBGC ( ipoib, "IPoIB %p received packet too short to "
  444. "contain IPoIB header\n", ipoib );
  445. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  446. netdev_rx_err ( netdev, iobuf, -EIO );
  447. return;
  448. }
  449. ipoib_hdr = iobuf->data;
  450. if ( ! av ) {
  451. DBGC ( ipoib, "IPoIB %p received packet without address "
  452. "vector\n", ipoib );
  453. netdev_rx_err ( netdev, iobuf, -ENOTTY );
  454. return;
  455. }
  456. /* Parse source address */
  457. if ( av->gid_present ) {
  458. ll_src.flags__qpn = htonl ( av->qpn );
  459. memcpy ( &ll_src.gid, &av->gid, sizeof ( ll_src.gid ) );
  460. src = ipoib_cache_peer ( &ll_src );
  461. ipoib_hdr->u.peer.src = src->key;
  462. }
  463. /* Hand off to network layer */
  464. netdev_rx ( netdev, iobuf );
  465. }
  466. /** IPoIB completion operations */
  467. static struct ib_completion_queue_operations ipoib_cq_op = {
  468. .complete_send = ipoib_complete_send,
  469. .complete_recv = ipoib_complete_recv,
  470. };
  471. /**
  472. * Poll IPoIB network device
  473. *
  474. * @v netdev Network device
  475. */
  476. static void ipoib_poll ( struct net_device *netdev ) {
  477. struct ipoib_device *ipoib = netdev->priv;
  478. struct ib_device *ibdev = ipoib->ibdev;
  479. ib_poll_eq ( ibdev );
  480. }
  481. /**
  482. * Handle IPv4 broadcast multicast group join completion
  483. *
  484. * @v ibdev Infiniband device
  485. * @v qp Queue pair
  486. * @v membership Multicast group membership
  487. * @v rc Status code
  488. * @v mad Response MAD (or NULL on error)
  489. */
  490. void ipoib_join_complete ( struct ib_device *ibdev __unused,
  491. struct ib_queue_pair *qp __unused,
  492. struct ib_mc_membership *membership, int rc,
  493. union ib_mad *mad __unused ) {
  494. struct ipoib_device *ipoib = container_of ( membership,
  495. struct ipoib_device, broadcast_membership );
  496. /* Record join status as link status */
  497. netdev_link_err ( ipoib->netdev, rc );
  498. }
  499. /**
  500. * Join IPv4 broadcast multicast group
  501. *
  502. * @v ipoib IPoIB device
  503. * @ret rc Return status code
  504. */
  505. static int ipoib_join_broadcast_group ( struct ipoib_device *ipoib ) {
  506. int rc;
  507. if ( ( rc = ib_mcast_join ( ipoib->ibdev, ipoib->qp,
  508. &ipoib->broadcast_membership,
  509. &ipoib->broadcast.gid,
  510. ipoib_join_complete ) ) != 0 ) {
  511. DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
  512. ipoib, strerror ( rc ) );
  513. return rc;
  514. }
  515. ipoib->broadcast_joined = 1;
  516. return 0;
  517. }
  518. /**
  519. * Leave IPv4 broadcast multicast group
  520. *
  521. * @v ipoib IPoIB device
  522. */
  523. static void ipoib_leave_broadcast_group ( struct ipoib_device *ipoib ) {
  524. if ( ipoib->broadcast_joined ) {
  525. ib_mcast_leave ( ipoib->ibdev, ipoib->qp,
  526. &ipoib->broadcast_membership );
  527. ipoib->broadcast_joined = 0;
  528. }
  529. }
  530. /**
  531. * Handle link status change
  532. *
  533. * @v ibdev Infiniband device
  534. */
  535. static void ipoib_link_state_changed ( struct ib_device *ibdev ) {
  536. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  537. struct ipoib_device *ipoib = netdev->priv;
  538. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  539. int rc;
  540. /* Leave existing broadcast group */
  541. ipoib_leave_broadcast_group ( ipoib );
  542. /* Update MAC address based on potentially-new GID prefix */
  543. memcpy ( &mac->gid.s.prefix, &ibdev->gid.s.prefix,
  544. sizeof ( mac->gid.s.prefix ) );
  545. /* Update broadcast GID based on potentially-new partition key */
  546. ipoib->broadcast.gid.words[2] =
  547. htons ( ibdev->pkey | IB_PKEY_FULL );
  548. /* Set net device link state to reflect Infiniband link state */
  549. rc = ib_link_rc ( ibdev );
  550. netdev_link_err ( netdev, ( rc ? rc : -EINPROGRESS_JOINING ) );
  551. /* Join new broadcast group */
  552. if ( ib_is_open ( ibdev ) && ib_link_ok ( ibdev ) &&
  553. ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) ) {
  554. DBGC ( ipoib, "IPoIB %p could not rejoin broadcast group: "
  555. "%s\n", ipoib, strerror ( rc ) );
  556. netdev_link_err ( netdev, rc );
  557. return;
  558. }
  559. }
  560. /**
  561. * Open IPoIB network device
  562. *
  563. * @v netdev Network device
  564. * @ret rc Return status code
  565. */
  566. static int ipoib_open ( struct net_device *netdev ) {
  567. struct ipoib_device *ipoib = netdev->priv;
  568. struct ib_device *ibdev = ipoib->ibdev;
  569. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  570. int rc;
  571. /* Open IB device */
  572. if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
  573. DBGC ( ipoib, "IPoIB %p could not open device: %s\n",
  574. ipoib, strerror ( rc ) );
  575. goto err_ib_open;
  576. }
  577. /* Allocate completion queue */
  578. ipoib->cq = ib_create_cq ( ibdev, IPOIB_NUM_CQES, &ipoib_cq_op );
  579. if ( ! ipoib->cq ) {
  580. DBGC ( ipoib, "IPoIB %p could not allocate completion queue\n",
  581. ipoib );
  582. rc = -ENOMEM;
  583. goto err_create_cq;
  584. }
  585. /* Allocate queue pair */
  586. ipoib->qp = ib_create_qp ( ibdev, IB_QPT_UD,
  587. IPOIB_NUM_SEND_WQES, ipoib->cq,
  588. IPOIB_NUM_RECV_WQES, ipoib->cq );
  589. if ( ! ipoib->qp ) {
  590. DBGC ( ipoib, "IPoIB %p could not allocate queue pair\n",
  591. ipoib );
  592. rc = -ENOMEM;
  593. goto err_create_qp;
  594. }
  595. ib_qp_set_ownerdata ( ipoib->qp, ipoib );
  596. /* Update MAC address with QPN */
  597. mac->flags__qpn = htonl ( ipoib->qp->qpn );
  598. /* Fill receive rings */
  599. ib_refill_recv ( ibdev, ipoib->qp );
  600. /* Fake a link status change to join the broadcast group */
  601. ipoib_link_state_changed ( ibdev );
  602. return 0;
  603. ib_destroy_qp ( ibdev, ipoib->qp );
  604. err_create_qp:
  605. ib_destroy_cq ( ibdev, ipoib->cq );
  606. err_create_cq:
  607. ib_close ( ibdev );
  608. err_ib_open:
  609. return rc;
  610. }
  611. /**
  612. * Close IPoIB network device
  613. *
  614. * @v netdev Network device
  615. */
  616. static void ipoib_close ( struct net_device *netdev ) {
  617. struct ipoib_device *ipoib = netdev->priv;
  618. struct ib_device *ibdev = ipoib->ibdev;
  619. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  620. /* Leave broadcast group */
  621. ipoib_leave_broadcast_group ( ipoib );
  622. /* Remove QPN from MAC address */
  623. mac->flags__qpn = 0;
  624. /* Tear down the queues */
  625. ib_destroy_qp ( ibdev, ipoib->qp );
  626. ib_destroy_cq ( ibdev, ipoib->cq );
  627. /* Close IB device */
  628. ib_close ( ibdev );
  629. }
  630. /** IPoIB network device operations */
  631. static struct net_device_operations ipoib_operations = {
  632. .open = ipoib_open,
  633. .close = ipoib_close,
  634. .transmit = ipoib_transmit,
  635. .poll = ipoib_poll,
  636. };
  637. /**
  638. * Probe IPoIB device
  639. *
  640. * @v ibdev Infiniband device
  641. * @ret rc Return status code
  642. */
  643. static int ipoib_probe ( struct ib_device *ibdev ) {
  644. struct net_device *netdev;
  645. struct ipoib_device *ipoib;
  646. int rc;
  647. /* Allocate network device */
  648. netdev = alloc_ipoibdev ( sizeof ( *ipoib ) );
  649. if ( ! netdev )
  650. return -ENOMEM;
  651. netdev_init ( netdev, &ipoib_operations );
  652. ipoib = netdev->priv;
  653. ib_set_ownerdata ( ibdev, netdev );
  654. netdev->dev = ibdev->dev;
  655. memset ( ipoib, 0, sizeof ( *ipoib ) );
  656. ipoib->netdev = netdev;
  657. ipoib->ibdev = ibdev;
  658. /* Extract hardware address */
  659. memcpy ( netdev->hw_addr, &ibdev->gid.s.guid,
  660. sizeof ( ibdev->gid.s.guid ) );
  661. /* Set default broadcast address */
  662. memcpy ( &ipoib->broadcast, &ipoib_broadcast,
  663. sizeof ( ipoib->broadcast ) );
  664. netdev->ll_broadcast = ( ( uint8_t * ) &ipoib->broadcast );
  665. /* Register network device */
  666. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  667. goto err_register_netdev;
  668. return 0;
  669. err_register_netdev:
  670. netdev_nullify ( netdev );
  671. netdev_put ( netdev );
  672. return rc;
  673. }
  674. /**
  675. * Remove IPoIB device
  676. *
  677. * @v ibdev Infiniband device
  678. */
  679. static void ipoib_remove ( struct ib_device *ibdev ) {
  680. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  681. unregister_netdev ( netdev );
  682. netdev_nullify ( netdev );
  683. netdev_put ( netdev );
  684. }
  685. /** IPoIB driver */
  686. struct ib_driver ipoib_driver __ib_driver = {
  687. .name = "IPoIB",
  688. .probe = ipoib_probe,
  689. .notify = ipoib_link_state_changed,
  690. .remove = ipoib_remove,
  691. };