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

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