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

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