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 20KB

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