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