Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ipoib.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. * Transcribe IPoIB address
  222. *
  223. * @v ll_addr Link-layer address
  224. * @ret string Link-layer address in human-readable format
  225. */
  226. const char * ipoib_ntoa ( const void *ll_addr ) {
  227. static char buf[45];
  228. const struct ipoib_mac *mac = ll_addr;
  229. snprintf ( buf, sizeof ( buf ), "%08x:%08x:%08x:%08x:%08x",
  230. htonl ( mac->qpn ), htonl ( mac->gid.u.dwords[0] ),
  231. htonl ( mac->gid.u.dwords[1] ),
  232. htonl ( mac->gid.u.dwords[2] ),
  233. htonl ( mac->gid.u.dwords[3] ) );
  234. return buf;
  235. }
  236. /**
  237. * Hash multicast address
  238. *
  239. * @v af Address family
  240. * @v net_addr Network-layer address
  241. * @v ll_addr Link-layer address to fill in
  242. * @ret rc Return status code
  243. */
  244. static int ipoib_mc_hash ( unsigned int af __unused,
  245. const void *net_addr __unused,
  246. void *ll_addr __unused ) {
  247. return -ENOTSUP;
  248. }
  249. /** IPoIB protocol */
  250. struct ll_protocol ipoib_protocol __ll_protocol = {
  251. .name = "IPoIB",
  252. .ll_proto = htons ( ARPHRD_INFINIBAND ),
  253. .ll_addr_len = IPOIB_ALEN,
  254. .ll_header_len = IPOIB_HLEN,
  255. .push = ipoib_push,
  256. .pull = ipoib_pull,
  257. .ntoa = ipoib_ntoa,
  258. .mc_hash = ipoib_mc_hash,
  259. };
  260. /**
  261. * Allocate IPoIB device
  262. *
  263. * @v priv_size Size of driver private data
  264. * @ret netdev Network device, or NULL
  265. */
  266. struct net_device * alloc_ipoibdev ( size_t priv_size ) {
  267. struct net_device *netdev;
  268. netdev = alloc_netdev ( priv_size );
  269. if ( netdev ) {
  270. netdev->ll_protocol = &ipoib_protocol;
  271. netdev->ll_broadcast = ( uint8_t * ) &ipoib_broadcast;
  272. netdev->max_pkt_len = IB_MAX_PAYLOAD_SIZE;
  273. }
  274. return netdev;
  275. }
  276. /****************************************************************************
  277. *
  278. * IPoIB network device
  279. *
  280. ****************************************************************************
  281. */
  282. /**
  283. * Transmit packet via IPoIB network device
  284. *
  285. * @v netdev Network device
  286. * @v iobuf I/O buffer
  287. * @ret rc Return status code
  288. */
  289. static int ipoib_transmit ( struct net_device *netdev,
  290. struct io_buffer *iobuf ) {
  291. struct ipoib_device *ipoib = netdev->priv;
  292. struct ib_device *ibdev = ipoib->ibdev;
  293. struct ipoib_hdr *ipoib_hdr;
  294. struct ipoib_peer *dest;
  295. struct ib_address_vector av;
  296. int rc;
  297. /* Sanity check */
  298. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  299. DBGC ( ipoib, "IPoIB %p buffer too short\n", ipoib );
  300. return -EINVAL;
  301. }
  302. ipoib_hdr = iobuf->data;
  303. /* Attempting transmission while link is down will put the
  304. * queue pair into an error state, so don't try it.
  305. */
  306. if ( ! ib_link_ok ( ibdev ) )
  307. return -ENETUNREACH;
  308. /* Identify destination address */
  309. dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
  310. if ( ! dest )
  311. return -ENXIO;
  312. ipoib_hdr->u.reserved = 0;
  313. /* Construct address vector */
  314. memset ( &av, 0, sizeof ( av ) );
  315. av.qpn = ntohl ( dest->mac.qpn );
  316. av.gid_present = 1;
  317. memcpy ( &av.gid, &dest->mac.gid, sizeof ( av.gid ) );
  318. if ( ( rc = ib_resolve_path ( ibdev, &av ) ) != 0 ) {
  319. /* Path not resolved yet */
  320. return rc;
  321. }
  322. return ib_post_send ( ibdev, ipoib->qp, &av, iobuf );
  323. }
  324. /**
  325. * Handle IPoIB send completion
  326. *
  327. * @v ibdev Infiniband device
  328. * @v qp Queue pair
  329. * @v iobuf I/O buffer
  330. * @v rc Completion status code
  331. */
  332. static void ipoib_complete_send ( struct ib_device *ibdev __unused,
  333. struct ib_queue_pair *qp,
  334. struct io_buffer *iobuf, int rc ) {
  335. struct ipoib_device *ipoib = ib_qp_get_ownerdata ( qp );
  336. netdev_tx_complete_err ( ipoib->netdev, iobuf, rc );
  337. }
  338. /**
  339. * Handle IPoIB receive completion
  340. *
  341. * @v ibdev Infiniband device
  342. * @v qp Queue pair
  343. * @v av Address vector, or NULL
  344. * @v iobuf I/O buffer
  345. * @v rc Completion status code
  346. */
  347. static void ipoib_complete_recv ( struct ib_device *ibdev __unused,
  348. struct ib_queue_pair *qp,
  349. struct ib_address_vector *av,
  350. struct io_buffer *iobuf, int rc ) {
  351. struct ipoib_device *ipoib = ib_qp_get_ownerdata ( qp );
  352. struct net_device *netdev = ipoib->netdev;
  353. struct ipoib_hdr *ipoib_hdr;
  354. struct ipoib_mac ll_src;
  355. struct ipoib_peer *src;
  356. if ( rc != 0 ) {
  357. netdev_rx_err ( netdev, iobuf, rc );
  358. return;
  359. }
  360. /* Sanity check */
  361. if ( iob_len ( iobuf ) < sizeof ( struct ipoib_hdr ) ) {
  362. DBGC ( ipoib, "IPoIB %p received packet too short to "
  363. "contain IPoIB header\n", ipoib );
  364. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  365. netdev_rx_err ( netdev, iobuf, -EIO );
  366. return;
  367. }
  368. ipoib_hdr = iobuf->data;
  369. /* Parse source address */
  370. if ( av->gid_present ) {
  371. ll_src.qpn = htonl ( av->qpn );
  372. memcpy ( &ll_src.gid, &av->gid, sizeof ( ll_src.gid ) );
  373. src = ipoib_cache_peer ( &ll_src );
  374. ipoib_hdr->u.peer.src = src->key;
  375. }
  376. /* Hand off to network layer */
  377. netdev_rx ( netdev, iobuf );
  378. }
  379. /** IPoIB completion operations */
  380. static struct ib_completion_queue_operations ipoib_cq_op = {
  381. .complete_send = ipoib_complete_send,
  382. .complete_recv = ipoib_complete_recv,
  383. };
  384. /**
  385. * Poll IPoIB network device
  386. *
  387. * @v netdev Network device
  388. */
  389. static void ipoib_poll ( struct net_device *netdev ) {
  390. struct ipoib_device *ipoib = netdev->priv;
  391. struct ib_device *ibdev = ipoib->ibdev;
  392. ib_poll_eq ( ibdev );
  393. }
  394. /**
  395. * Enable/disable interrupts on IPoIB network device
  396. *
  397. * @v netdev Network device
  398. * @v enable Interrupts should be enabled
  399. */
  400. static void ipoib_irq ( struct net_device *netdev __unused,
  401. int enable __unused ) {
  402. /* No implementation */
  403. }
  404. /**
  405. * Handle IPv4 broadcast multicast group join completion
  406. *
  407. * @v ibdev Infiniband device
  408. * @v qp Queue pair
  409. * @v membership Multicast group membership
  410. * @v rc Status code
  411. * @v mad Response MAD (or NULL on error)
  412. */
  413. void ipoib_join_complete ( struct ib_device *ibdev __unused,
  414. struct ib_queue_pair *qp __unused,
  415. struct ib_mc_membership *membership, int rc,
  416. union ib_mad *mad __unused ) {
  417. struct ipoib_device *ipoib = container_of ( membership,
  418. struct ipoib_device, broadcast_membership );
  419. /* Record join status as link status */
  420. netdev_link_err ( ipoib->netdev, rc );
  421. }
  422. /**
  423. * Join IPv4 broadcast multicast group
  424. *
  425. * @v ipoib IPoIB device
  426. * @ret rc Return status code
  427. */
  428. static int ipoib_join_broadcast_group ( struct ipoib_device *ipoib ) {
  429. int rc;
  430. if ( ( rc = ib_mcast_join ( ipoib->ibdev, ipoib->qp,
  431. &ipoib->broadcast_membership,
  432. &ipoib->broadcast.gid,
  433. ipoib_join_complete ) ) != 0 ) {
  434. DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
  435. ipoib, strerror ( rc ) );
  436. return rc;
  437. }
  438. ipoib->broadcast_joined = 1;
  439. return 0;
  440. }
  441. /**
  442. * Leave IPv4 broadcast multicast group
  443. *
  444. * @v ipoib IPoIB device
  445. */
  446. static void ipoib_leave_broadcast_group ( struct ipoib_device *ipoib ) {
  447. if ( ipoib->broadcast_joined ) {
  448. ib_mcast_leave ( ipoib->ibdev, ipoib->qp,
  449. &ipoib->broadcast_membership );
  450. ipoib->broadcast_joined = 0;
  451. }
  452. }
  453. /**
  454. * Open IPoIB network device
  455. *
  456. * @v netdev Network device
  457. * @ret rc Return status code
  458. */
  459. static int ipoib_open ( struct net_device *netdev ) {
  460. struct ipoib_device *ipoib = netdev->priv;
  461. struct ib_device *ibdev = ipoib->ibdev;
  462. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  463. int rc;
  464. /* Open IB device */
  465. if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
  466. DBGC ( ipoib, "IPoIB %p could not open device: %s\n",
  467. ipoib, strerror ( rc ) );
  468. goto err_ib_open;
  469. }
  470. /* Allocate completion queue */
  471. ipoib->cq = ib_create_cq ( ibdev, IPOIB_NUM_CQES, &ipoib_cq_op );
  472. if ( ! ipoib->cq ) {
  473. DBGC ( ipoib, "IPoIB %p could not allocate completion queue\n",
  474. ipoib );
  475. rc = -ENOMEM;
  476. goto err_create_cq;
  477. }
  478. /* Allocate queue pair */
  479. ipoib->qp = ib_create_qp ( ibdev, IB_QPT_UD,
  480. IPOIB_NUM_SEND_WQES, ipoib->cq,
  481. IPOIB_NUM_RECV_WQES, ipoib->cq );
  482. if ( ! ipoib->qp ) {
  483. DBGC ( ipoib, "IPoIB %p could not allocate queue pair\n",
  484. ipoib );
  485. rc = -ENOMEM;
  486. goto err_create_qp;
  487. }
  488. ib_qp_set_ownerdata ( ipoib->qp, ipoib );
  489. /* Update MAC address with QPN */
  490. mac->qpn = htonl ( ipoib->qp->qpn );
  491. /* Fill receive rings */
  492. ib_refill_recv ( ibdev, ipoib->qp );
  493. /* Fake a link status change to join the broadcast group */
  494. ipoib_link_state_changed ( ibdev );
  495. return 0;
  496. ib_destroy_qp ( ibdev, ipoib->qp );
  497. err_create_qp:
  498. ib_destroy_cq ( ibdev, ipoib->cq );
  499. err_create_cq:
  500. ib_close ( ibdev );
  501. err_ib_open:
  502. return rc;
  503. }
  504. /**
  505. * Close IPoIB network device
  506. *
  507. * @v netdev Network device
  508. */
  509. static void ipoib_close ( struct net_device *netdev ) {
  510. struct ipoib_device *ipoib = netdev->priv;
  511. struct ib_device *ibdev = ipoib->ibdev;
  512. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  513. /* Leave broadcast group */
  514. ipoib_leave_broadcast_group ( ipoib );
  515. /* Remove QPN from MAC address */
  516. mac->qpn = 0;
  517. /* Tear down the queues */
  518. ib_destroy_qp ( ibdev, ipoib->qp );
  519. ib_destroy_cq ( ibdev, ipoib->cq );
  520. /* Close IB device */
  521. ib_close ( ibdev );
  522. }
  523. /** IPoIB network device operations */
  524. static struct net_device_operations ipoib_operations = {
  525. .open = ipoib_open,
  526. .close = ipoib_close,
  527. .transmit = ipoib_transmit,
  528. .poll = ipoib_poll,
  529. .irq = ipoib_irq,
  530. };
  531. /**
  532. * Update IPoIB dynamic Infiniband parameters
  533. *
  534. * @v ipoib IPoIB device
  535. *
  536. * The Infiniband port GID and partition key will change at runtime,
  537. * when the link is established (or lost). The MAC address is based
  538. * on the port GID, and the broadcast GID is based on the partition
  539. * key. This function recalculates these IPoIB device parameters.
  540. */
  541. static void ipoib_set_ib_params ( struct ipoib_device *ipoib ) {
  542. struct ib_device *ibdev = ipoib->ibdev;
  543. struct net_device *netdev = ipoib->netdev;
  544. struct ipoib_mac *mac;
  545. /* Calculate GID portion of MAC address based on port GID */
  546. mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  547. memcpy ( &mac->gid, &ibdev->gid, sizeof ( mac->gid ) );
  548. /* Calculate broadcast GID based on partition key */
  549. memcpy ( &ipoib->broadcast, &ipoib_broadcast,
  550. sizeof ( ipoib->broadcast ) );
  551. ipoib->broadcast.gid.u.words[2] = htons ( ibdev->pkey );
  552. /* Set net device link state to reflect Infiniband link state */
  553. if ( ib_link_ok ( ibdev ) ) {
  554. netdev_link_up ( netdev );
  555. } else {
  556. netdev_link_down ( netdev );
  557. }
  558. }
  559. /**
  560. * Handle link status change
  561. *
  562. * @v ibdev Infiniband device
  563. */
  564. void ipoib_link_state_changed ( struct ib_device *ibdev ) {
  565. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  566. struct ipoib_device *ipoib = netdev->priv;
  567. int rc;
  568. /* Leave existing broadcast group */
  569. ipoib_leave_broadcast_group ( ipoib );
  570. /* Update MAC address and broadcast GID based on new port GID
  571. * and partition key.
  572. */
  573. ipoib_set_ib_params ( ipoib );
  574. /* Join new broadcast group */
  575. if ( ib_link_ok ( ibdev ) &&
  576. ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) ) {
  577. DBGC ( ipoib, "IPoIB %p could not rejoin broadcast group: "
  578. "%s\n", ipoib, strerror ( rc ) );
  579. return;
  580. }
  581. }
  582. /**
  583. * Probe IPoIB device
  584. *
  585. * @v ibdev Infiniband device
  586. * @ret rc Return status code
  587. */
  588. int ipoib_probe ( struct ib_device *ibdev ) {
  589. struct net_device *netdev;
  590. struct ipoib_device *ipoib;
  591. int rc;
  592. /* Allocate network device */
  593. netdev = alloc_ipoibdev ( sizeof ( *ipoib ) );
  594. if ( ! netdev )
  595. return -ENOMEM;
  596. netdev_init ( netdev, &ipoib_operations );
  597. ipoib = netdev->priv;
  598. ib_set_ownerdata ( ibdev, netdev );
  599. netdev->dev = ibdev->dev;
  600. netdev->ll_broadcast = ( ( uint8_t * ) &ipoib->broadcast );
  601. memset ( ipoib, 0, sizeof ( *ipoib ) );
  602. ipoib->netdev = netdev;
  603. ipoib->ibdev = ibdev;
  604. /* Calculate as much of the broadcast GID and the MAC address
  605. * as we can. We won't know either of these in full until we
  606. * have link-up.
  607. */
  608. ipoib_set_ib_params ( ipoib );
  609. /* Register network device */
  610. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  611. goto err_register_netdev;
  612. return 0;
  613. err_register_netdev:
  614. netdev_nullify ( netdev );
  615. netdev_put ( netdev );
  616. return rc;
  617. }
  618. /**
  619. * Remove IPoIB device
  620. *
  621. * @v ibdev Infiniband device
  622. */
  623. void ipoib_remove ( struct ib_device *ibdev ) {
  624. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  625. unregister_netdev ( netdev );
  626. netdev_nullify ( netdev );
  627. netdev_put ( netdev );
  628. }