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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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/ipoib.h>
  30. /** @file
  31. *
  32. * IP over Infiniband
  33. */
  34. /** Number of IPoIB data send work queue entries */
  35. #define IPOIB_DATA_NUM_SEND_WQES 2
  36. /** Number of IPoIB data receive work queue entries */
  37. #define IPOIB_DATA_NUM_RECV_WQES 4
  38. /** Number of IPoIB data completion entries */
  39. #define IPOIB_DATA_NUM_CQES 8
  40. /** Number of IPoIB metadata send work queue entries */
  41. #define IPOIB_META_NUM_SEND_WQES 2
  42. /** Number of IPoIB metadata receive work queue entries */
  43. #define IPOIB_META_NUM_RECV_WQES 2
  44. /** Number of IPoIB metadata completion entries */
  45. #define IPOIB_META_NUM_CQES 8
  46. /** An IPoIB queue set */
  47. struct ipoib_queue_set {
  48. /** Completion queue */
  49. struct ib_completion_queue *cq;
  50. /** Queue pair */
  51. struct ib_queue_pair *qp;
  52. /** Receive work queue maximum fill level */
  53. unsigned int recv_max_fill;
  54. };
  55. /** An IPoIB device */
  56. struct ipoib_device {
  57. /** Network device */
  58. struct net_device *netdev;
  59. /** Underlying Infiniband device */
  60. struct ib_device *ibdev;
  61. /** Data queue set */
  62. struct ipoib_queue_set data;
  63. /** Data queue set */
  64. struct ipoib_queue_set meta;
  65. /** Broadcast GID */
  66. struct ib_gid broadcast_gid;
  67. /** Broadcast LID */
  68. unsigned int broadcast_lid;
  69. /** Data queue key */
  70. unsigned long data_qkey;
  71. /** Attached to multicast group
  72. *
  73. * This flag indicates whether or not we have attached our
  74. * data queue pair to the broadcast multicast GID.
  75. */
  76. int broadcast_attached;
  77. };
  78. /** TID half used to identify get path record replies */
  79. #define IPOIB_TID_GET_PATH_REC 0x11111111UL
  80. /** TID half used to identify multicast member record replies */
  81. #define IPOIB_TID_MC_MEMBER_REC 0x22222222UL
  82. /** IPoIB metadata TID */
  83. static uint32_t ipoib_meta_tid = 0;
  84. /** Broadcast QPN used in IPoIB MAC addresses
  85. *
  86. * This is a guaranteed invalid real QPN
  87. */
  88. #define IPOIB_BROADCAST_QPN 0xffffffffUL
  89. /** Broadcast IPoIB address */
  90. static struct ipoib_mac ipoib_broadcast = {
  91. .qpn = ntohl ( IPOIB_BROADCAST_QPN ),
  92. .gid.u.bytes = { 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff },
  94. };
  95. /****************************************************************************
  96. *
  97. * IPoIB peer cache
  98. *
  99. ****************************************************************************
  100. */
  101. /**
  102. * IPoIB peer address
  103. *
  104. * This serves a similar role to the ARP cache for Ethernet. (ARP
  105. * *is* used on IPoIB; we have two caches to maintain.)
  106. */
  107. struct ipoib_peer {
  108. /** Key */
  109. uint8_t key;
  110. /** MAC address */
  111. struct ipoib_mac mac;
  112. /** LID */
  113. unsigned int lid;
  114. /** Service level */
  115. unsigned int sl;
  116. /** Rate */
  117. unsigned int rate;
  118. };
  119. /** Number of IPoIB peer cache entries
  120. *
  121. * Must be a power of two.
  122. */
  123. #define IPOIB_NUM_CACHED_PEERS 4
  124. /** IPoIB peer address cache */
  125. static struct ipoib_peer ipoib_peer_cache[IPOIB_NUM_CACHED_PEERS];
  126. /** Oldest IPoIB peer cache entry index */
  127. static unsigned int ipoib_peer_cache_idx = 1;
  128. /**
  129. * Look up cached peer by key
  130. *
  131. * @v key Peer cache key
  132. * @ret peer Peer cache entry, or NULL
  133. */
  134. static struct ipoib_peer * ipoib_lookup_peer_by_key ( unsigned int key ) {
  135. struct ipoib_peer *peer;
  136. unsigned int i;
  137. for ( i = 0 ; i < IPOIB_NUM_CACHED_PEERS ; i++ ) {
  138. peer = &ipoib_peer_cache[i];
  139. if ( peer->key == key )
  140. return peer;
  141. }
  142. if ( key != 0 ) {
  143. DBG ( "IPoIB warning: peer cache lost track of key %x while "
  144. "still in use\n", key );
  145. }
  146. return NULL;
  147. }
  148. /**
  149. * Look up cached peer by GID
  150. *
  151. * @v gid Peer GID
  152. * @ret peer Peer cache entry, or NULL
  153. */
  154. static struct ipoib_peer *
  155. ipoib_lookup_peer_by_gid ( const struct ib_gid *gid ) {
  156. struct ipoib_peer *peer;
  157. unsigned int i;
  158. for ( i = 0 ; i < IPOIB_NUM_CACHED_PEERS ; i++ ) {
  159. peer = &ipoib_peer_cache[i];
  160. if ( memcmp ( &peer->mac.gid, gid,
  161. sizeof ( peer->mac.gid) ) == 0 ) {
  162. return peer;
  163. }
  164. }
  165. return NULL;
  166. }
  167. /**
  168. * Store GID and QPN in peer cache
  169. *
  170. * @v gid Peer GID
  171. * @v qpn Peer QPN
  172. * @ret peer Peer cache entry
  173. */
  174. static struct ipoib_peer *
  175. ipoib_cache_peer ( const struct ib_gid *gid, unsigned long qpn ) {
  176. struct ipoib_peer *peer;
  177. unsigned int key;
  178. /* Look for existing cache entry */
  179. peer = ipoib_lookup_peer_by_gid ( gid );
  180. if ( peer ) {
  181. assert ( peer->mac.qpn = ntohl ( qpn ) );
  182. return peer;
  183. }
  184. /* No entry found: create a new one */
  185. key = ipoib_peer_cache_idx++;
  186. peer = &ipoib_peer_cache[ key % IPOIB_NUM_CACHED_PEERS ];
  187. if ( peer->key )
  188. DBG ( "IPoIB peer %x evicted from cache\n", peer->key );
  189. memset ( peer, 0, sizeof ( *peer ) );
  190. peer->key = key;
  191. peer->mac.qpn = htonl ( qpn );
  192. memcpy ( &peer->mac.gid, gid, sizeof ( peer->mac.gid ) );
  193. DBG ( "IPoIB peer %x has GID %08x:%08x:%08x:%08x and QPN %lx\n",
  194. peer->key, htonl ( gid->u.dwords[0] ),
  195. htonl ( gid->u.dwords[1] ), htonl ( gid->u.dwords[2] ),
  196. htonl ( gid->u.dwords[3] ), qpn );
  197. return peer;
  198. }
  199. /****************************************************************************
  200. *
  201. * IPoIB link layer
  202. *
  203. ****************************************************************************
  204. */
  205. /**
  206. * Add IPoIB link-layer header
  207. *
  208. * @v netdev Network device
  209. * @v iobuf I/O buffer
  210. * @v ll_dest Link-layer destination address
  211. * @v ll_source Source link-layer address
  212. * @v net_proto Network-layer protocol, in network-byte order
  213. * @ret rc Return status code
  214. */
  215. static int ipoib_push ( struct net_device *netdev __unused,
  216. struct io_buffer *iobuf, const void *ll_dest,
  217. const void *ll_source __unused, uint16_t net_proto ) {
  218. struct ipoib_hdr *ipoib_hdr =
  219. iob_push ( iobuf, sizeof ( *ipoib_hdr ) );
  220. const struct ipoib_mac *dest_mac = ll_dest;
  221. const struct ipoib_mac *src_mac = ll_source;
  222. struct ipoib_peer *dest;
  223. struct ipoib_peer *src;
  224. /* Add link-layer addresses to cache */
  225. dest = ipoib_cache_peer ( &dest_mac->gid, ntohl ( dest_mac->qpn ) );
  226. src = ipoib_cache_peer ( &src_mac->gid, ntohl ( src_mac->qpn ) );
  227. /* Build IPoIB header */
  228. ipoib_hdr->proto = net_proto;
  229. ipoib_hdr->u.peer.dest = dest->key;
  230. ipoib_hdr->u.peer.src = src->key;
  231. return 0;
  232. }
  233. /**
  234. * Remove IPoIB link-layer header
  235. *
  236. * @v netdev Network device
  237. * @v iobuf I/O buffer
  238. * @ret ll_dest Link-layer destination address
  239. * @ret ll_source Source link-layer address
  240. * @ret net_proto Network-layer protocol, in network-byte order
  241. * @ret rc Return status code
  242. */
  243. static int ipoib_pull ( struct net_device *netdev __unused,
  244. struct io_buffer *iobuf, const void **ll_dest,
  245. const void **ll_source, uint16_t *net_proto ) {
  246. struct ipoib_hdr *ipoib_hdr = iobuf->data;
  247. struct ipoib_peer *dest;
  248. struct ipoib_peer *source;
  249. /* Sanity check */
  250. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  251. DBG ( "IPoIB packet too short for link-layer header\n" );
  252. DBG_HD ( iobuf->data, iob_len ( iobuf ) );
  253. return -EINVAL;
  254. }
  255. /* Strip off IPoIB header */
  256. iob_pull ( iobuf, sizeof ( *ipoib_hdr ) );
  257. /* Identify source and destination addresses, and clear
  258. * reserved word in IPoIB header
  259. */
  260. dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
  261. source = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.src );
  262. ipoib_hdr->u.reserved = 0;
  263. /* Fill in required fields */
  264. *ll_dest = ( dest ? &dest->mac : &ipoib_broadcast );
  265. *ll_source = ( source ? &source->mac : &ipoib_broadcast );
  266. *net_proto = ipoib_hdr->proto;
  267. return 0;
  268. }
  269. /**
  270. * Transcribe IPoIB address
  271. *
  272. * @v ll_addr Link-layer address
  273. * @ret string Link-layer address in human-readable format
  274. */
  275. const char * ipoib_ntoa ( const void *ll_addr ) {
  276. static char buf[45];
  277. const struct ipoib_mac *mac = ll_addr;
  278. snprintf ( buf, sizeof ( buf ), "%08x:%08x:%08x:%08x:%08x",
  279. htonl ( mac->qpn ), htonl ( mac->gid.u.dwords[0] ),
  280. htonl ( mac->gid.u.dwords[1] ),
  281. htonl ( mac->gid.u.dwords[2] ),
  282. htonl ( mac->gid.u.dwords[3] ) );
  283. return buf;
  284. }
  285. /**
  286. * Hash multicast address
  287. *
  288. * @v af Address family
  289. * @v net_addr Network-layer address
  290. * @v ll_addr Link-layer address to fill in
  291. * @ret rc Return status code
  292. */
  293. static int ipoib_mc_hash ( unsigned int af __unused,
  294. const void *net_addr __unused,
  295. void *ll_addr __unused ) {
  296. return -ENOTSUP;
  297. }
  298. /** IPoIB protocol */
  299. struct ll_protocol ipoib_protocol __ll_protocol = {
  300. .name = "IPoIB",
  301. .ll_proto = htons ( ARPHRD_INFINIBAND ),
  302. .ll_addr_len = IPOIB_ALEN,
  303. .ll_header_len = IPOIB_HLEN,
  304. .ll_broadcast = ( uint8_t * ) &ipoib_broadcast,
  305. .push = ipoib_push,
  306. .pull = ipoib_pull,
  307. .ntoa = ipoib_ntoa,
  308. .mc_hash = ipoib_mc_hash,
  309. };
  310. /****************************************************************************
  311. *
  312. * IPoIB network device
  313. *
  314. ****************************************************************************
  315. */
  316. /**
  317. * Destroy queue set
  318. *
  319. * @v ipoib IPoIB device
  320. * @v qset Queue set
  321. */
  322. static void ipoib_destroy_qset ( struct ipoib_device *ipoib,
  323. struct ipoib_queue_set *qset ) {
  324. struct ib_device *ibdev = ipoib->ibdev;
  325. if ( qset->qp )
  326. ib_destroy_qp ( ibdev, qset->qp );
  327. if ( qset->cq )
  328. ib_destroy_cq ( ibdev, qset->cq );
  329. memset ( qset, 0, sizeof ( *qset ) );
  330. }
  331. /**
  332. * Create queue set
  333. *
  334. * @v ipoib IPoIB device
  335. * @v qset Queue set
  336. * @v num_cqes Number of completion queue entries
  337. * @v cq_op Completion queue operations
  338. * @v num_send_wqes Number of send work queue entries
  339. * @v num_recv_wqes Number of receive work queue entries
  340. * @v qkey Queue key
  341. * @ret rc Return status code
  342. */
  343. static int ipoib_create_qset ( struct ipoib_device *ipoib,
  344. struct ipoib_queue_set *qset,
  345. unsigned int num_cqes,
  346. struct ib_completion_queue_operations *cq_op,
  347. unsigned int num_send_wqes,
  348. unsigned int num_recv_wqes,
  349. unsigned long qkey ) {
  350. struct ib_device *ibdev = ipoib->ibdev;
  351. int rc;
  352. /* Sanity check */
  353. assert ( qset->cq == NULL );
  354. assert ( qset->qp == NULL );
  355. /* Store queue parameters */
  356. qset->recv_max_fill = num_recv_wqes;
  357. /* Allocate completion queue */
  358. qset->cq = ib_create_cq ( ibdev, num_cqes, cq_op );
  359. if ( ! qset->cq ) {
  360. DBGC ( ipoib, "IPoIB %p could not allocate completion queue\n",
  361. ipoib );
  362. rc = -ENOMEM;
  363. goto err;
  364. }
  365. /* Allocate queue pair */
  366. qset->qp = ib_create_qp ( ibdev, num_send_wqes, qset->cq,
  367. num_recv_wqes, qset->cq, qkey );
  368. if ( ! qset->qp ) {
  369. DBGC ( ipoib, "IPoIB %p could not allocate queue pair\n",
  370. ipoib );
  371. rc = -ENOMEM;
  372. goto err;
  373. }
  374. ib_qp_set_ownerdata ( qset->qp, ipoib->netdev );
  375. return 0;
  376. err:
  377. ipoib_destroy_qset ( ipoib, qset );
  378. return rc;
  379. }
  380. /**
  381. * Transmit path record request
  382. *
  383. * @v ipoib IPoIB device
  384. * @v gid Destination GID
  385. * @ret rc Return status code
  386. */
  387. static int ipoib_get_path_record ( struct ipoib_device *ipoib,
  388. struct ib_gid *gid ) {
  389. struct ib_device *ibdev = ipoib->ibdev;
  390. struct io_buffer *iobuf;
  391. struct ib_mad_sa *sa;
  392. struct ib_address_vector av;
  393. int rc;
  394. /* Allocate I/O buffer */
  395. iobuf = alloc_iob ( sizeof ( *sa ) );
  396. if ( ! iobuf )
  397. return -ENOMEM;
  398. iob_put ( iobuf, sizeof ( *sa ) );
  399. sa = iobuf->data;
  400. memset ( sa, 0, sizeof ( *sa ) );
  401. /* Construct path record request */
  402. sa->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  403. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  404. sa->mad_hdr.class_version = 2;
  405. sa->mad_hdr.method = IB_MGMT_METHOD_GET;
  406. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  407. sa->mad_hdr.tid[0] = IPOIB_TID_GET_PATH_REC;
  408. sa->mad_hdr.tid[1] = ipoib_meta_tid++;
  409. sa->sa_hdr.comp_mask[1] =
  410. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  411. memcpy ( &sa->sa_data.path_record.dgid, gid,
  412. sizeof ( sa->sa_data.path_record.dgid ) );
  413. memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
  414. sizeof ( sa->sa_data.path_record.sgid ) );
  415. /* Construct address vector */
  416. memset ( &av, 0, sizeof ( av ) );
  417. av.lid = ibdev->sm_lid;
  418. av.sl = ibdev->sm_sl;
  419. av.qpn = IB_SA_QPN;
  420. av.qkey = IB_GLOBAL_QKEY;
  421. /* Post send request */
  422. if ( ( rc = ib_post_send ( ibdev, ipoib->meta.qp, &av,
  423. iobuf ) ) != 0 ) {
  424. DBGC ( ipoib, "IPoIB %p could not send get path record: %s\n",
  425. ipoib, strerror ( rc ) );
  426. free_iob ( iobuf );
  427. return rc;
  428. }
  429. return 0;
  430. }
  431. /**
  432. * Transmit multicast group membership request
  433. *
  434. * @v ipoib IPoIB device
  435. * @v gid Multicast GID
  436. * @v join Join (rather than leave) group
  437. * @ret rc Return status code
  438. */
  439. static int ipoib_mc_member_record ( struct ipoib_device *ipoib,
  440. struct ib_gid *gid, int join ) {
  441. struct ib_device *ibdev = ipoib->ibdev;
  442. struct io_buffer *iobuf;
  443. struct ib_mad_sa *sa;
  444. struct ib_address_vector av;
  445. int rc;
  446. /* Allocate I/O buffer */
  447. iobuf = alloc_iob ( sizeof ( *sa ) );
  448. if ( ! iobuf )
  449. return -ENOMEM;
  450. iob_put ( iobuf, sizeof ( *sa ) );
  451. sa = iobuf->data;
  452. memset ( sa, 0, sizeof ( *sa ) );
  453. /* Construct path record request */
  454. sa->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  455. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  456. sa->mad_hdr.class_version = 2;
  457. sa->mad_hdr.method =
  458. ( join ? IB_MGMT_METHOD_SET : IB_MGMT_METHOD_DELETE );
  459. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_MC_MEMBER_REC );
  460. sa->mad_hdr.tid[0] = IPOIB_TID_MC_MEMBER_REC;
  461. sa->mad_hdr.tid[1] = ipoib_meta_tid++;
  462. sa->sa_hdr.comp_mask[1] =
  463. htonl ( IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
  464. IB_SA_MCMEMBER_REC_JOIN_STATE );
  465. sa->sa_data.mc_member_record.scope__join_state = 1;
  466. memcpy ( &sa->sa_data.mc_member_record.mgid, gid,
  467. sizeof ( sa->sa_data.mc_member_record.mgid ) );
  468. memcpy ( &sa->sa_data.mc_member_record.port_gid, &ibdev->gid,
  469. sizeof ( sa->sa_data.mc_member_record.port_gid ) );
  470. /* Construct address vector */
  471. memset ( &av, 0, sizeof ( av ) );
  472. av.lid = ibdev->sm_lid;
  473. av.sl = ibdev->sm_sl;
  474. av.qpn = IB_SA_QPN;
  475. av.qkey = IB_GLOBAL_QKEY;
  476. /* Post send request */
  477. if ( ( rc = ib_post_send ( ibdev, ipoib->meta.qp, &av,
  478. iobuf ) ) != 0 ) {
  479. DBGC ( ipoib, "IPoIB %p could not send get path record: %s\n",
  480. ipoib, strerror ( rc ) );
  481. free_iob ( iobuf );
  482. return rc;
  483. }
  484. return 0;
  485. }
  486. /**
  487. * Transmit packet via IPoIB network device
  488. *
  489. * @v netdev Network device
  490. * @v iobuf I/O buffer
  491. * @ret rc Return status code
  492. */
  493. static int ipoib_transmit ( struct net_device *netdev,
  494. struct io_buffer *iobuf ) {
  495. struct ipoib_device *ipoib = netdev->priv;
  496. struct ib_device *ibdev = ipoib->ibdev;
  497. struct ipoib_hdr *ipoib_hdr;
  498. struct ipoib_peer *dest;
  499. struct ib_address_vector av;
  500. struct ib_gid *gid;
  501. /* Sanity check */
  502. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  503. DBGC ( ipoib, "IPoIB %p buffer too short\n", ipoib );
  504. return -EINVAL;
  505. }
  506. ipoib_hdr = iobuf->data;
  507. /* Attempting transmission while link is down will put the
  508. * queue pair into an error state, so don't try it.
  509. */
  510. if ( ! ib_link_ok ( ibdev ) )
  511. return -ENETUNREACH;
  512. /* Identify destination address */
  513. dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
  514. if ( ! dest )
  515. return -ENXIO;
  516. ipoib_hdr->u.reserved = 0;
  517. /* Construct address vector */
  518. memset ( &av, 0, sizeof ( av ) );
  519. av.qkey = ipoib->data_qkey;
  520. av.gid_present = 1;
  521. if ( dest->mac.qpn == htonl ( IPOIB_BROADCAST_QPN ) ) {
  522. /* Broadcast */
  523. av.qpn = IB_BROADCAST_QPN;
  524. av.lid = ipoib->broadcast_lid;
  525. gid = &ipoib->broadcast_gid;
  526. } else {
  527. /* Unicast */
  528. if ( ! dest->lid ) {
  529. /* No LID yet - get path record to fetch LID */
  530. ipoib_get_path_record ( ipoib, &dest->mac.gid );
  531. return -ENOENT;
  532. }
  533. av.qpn = ntohl ( dest->mac.qpn );
  534. av.lid = dest->lid;
  535. av.rate = dest->rate;
  536. av.sl = dest->sl;
  537. gid = &dest->mac.gid;
  538. }
  539. memcpy ( &av.gid, gid, sizeof ( av.gid ) );
  540. return ib_post_send ( ibdev, ipoib->data.qp, &av, iobuf );
  541. }
  542. /**
  543. * Handle IPoIB data send completion
  544. *
  545. * @v ibdev Infiniband device
  546. * @v qp Queue pair
  547. * @v iobuf I/O buffer
  548. * @v rc Completion status code
  549. */
  550. static void ipoib_data_complete_send ( struct ib_device *ibdev __unused,
  551. struct ib_queue_pair *qp,
  552. struct io_buffer *iobuf, int rc ) {
  553. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  554. netdev_tx_complete_err ( netdev, iobuf, rc );
  555. }
  556. /**
  557. * Handle IPoIB data receive completion
  558. *
  559. * @v ibdev Infiniband device
  560. * @v qp Queue pair
  561. * @v av Address vector, or NULL
  562. * @v iobuf I/O buffer
  563. * @v rc Completion status code
  564. */
  565. static void ipoib_data_complete_recv ( struct ib_device *ibdev __unused,
  566. struct ib_queue_pair *qp,
  567. struct ib_address_vector *av,
  568. struct io_buffer *iobuf, int rc ) {
  569. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  570. struct ipoib_device *ipoib = netdev->priv;
  571. struct ipoib_hdr *ipoib_hdr;
  572. struct ipoib_peer *src;
  573. if ( rc != 0 ) {
  574. netdev_rx_err ( netdev, iobuf, rc );
  575. return;
  576. }
  577. /* Sanity check */
  578. if ( iob_len ( iobuf ) < sizeof ( struct ipoib_hdr ) ) {
  579. DBGC ( ipoib, "IPoIB %p received data packet too short to "
  580. "contain IPoIB header\n", ipoib );
  581. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  582. netdev_rx_err ( netdev, iobuf, -EIO );
  583. return;
  584. }
  585. ipoib_hdr = iobuf->data;
  586. /* Parse source address */
  587. if ( av->gid_present ) {
  588. src = ipoib_cache_peer ( &av->gid, av->qpn );
  589. ipoib_hdr->u.peer.src = src->key;
  590. }
  591. /* Hand off to network layer */
  592. netdev_rx ( netdev, iobuf );
  593. }
  594. /** IPoIB data completion operations */
  595. static struct ib_completion_queue_operations ipoib_data_cq_op = {
  596. .complete_send = ipoib_data_complete_send,
  597. .complete_recv = ipoib_data_complete_recv,
  598. };
  599. /**
  600. * Handle IPoIB metadata send completion
  601. *
  602. * @v ibdev Infiniband device
  603. * @v qp Queue pair
  604. * @v iobuf I/O buffer
  605. * @v rc Completion status code
  606. */
  607. static void ipoib_meta_complete_send ( struct ib_device *ibdev __unused,
  608. struct ib_queue_pair *qp,
  609. struct io_buffer *iobuf, int rc ) {
  610. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  611. struct ipoib_device *ipoib = netdev->priv;
  612. if ( rc != 0 ) {
  613. DBGC ( ipoib, "IPoIB %p metadata TX completion error: %s\n",
  614. ipoib, strerror ( rc ) );
  615. }
  616. free_iob ( iobuf );
  617. }
  618. /**
  619. * Handle received IPoIB path record
  620. *
  621. * @v ipoib IPoIB device
  622. * @v path_record Path record
  623. */
  624. static void ipoib_recv_path_record ( struct ipoib_device *ipoib,
  625. struct ib_path_record *path_record ) {
  626. struct ipoib_peer *peer;
  627. /* Locate peer cache entry */
  628. peer = ipoib_lookup_peer_by_gid ( &path_record->dgid );
  629. if ( ! peer ) {
  630. DBGC ( ipoib, "IPoIB %p received unsolicited path record\n",
  631. ipoib );
  632. return;
  633. }
  634. /* Update path cache entry */
  635. peer->lid = ntohs ( path_record->dlid );
  636. peer->sl = ( path_record->reserved__sl & 0x0f );
  637. peer->rate = ( path_record->rate_selector__rate & 0x3f );
  638. DBG ( "IPoIB peer %x has dlid %x sl %x rate %x\n",
  639. peer->key, peer->lid, peer->sl, peer->rate );
  640. }
  641. /**
  642. * Handle received IPoIB multicast membership record
  643. *
  644. * @v ipoib IPoIB device
  645. * @v mc_member_record Multicast membership record
  646. */
  647. static void ipoib_recv_mc_member_record ( struct ipoib_device *ipoib,
  648. struct ib_mc_member_record *mc_member_record ) {
  649. int joined;
  650. int rc;
  651. /* Record parameters */
  652. joined = ( mc_member_record->scope__join_state & 0x0f );
  653. ipoib->data_qkey = ntohl ( mc_member_record->qkey );
  654. ipoib->broadcast_lid = ntohs ( mc_member_record->mlid );
  655. DBGC ( ipoib, "IPoIB %p %s broadcast group: qkey %lx mlid %x\n",
  656. ipoib, ( joined ? "joined" : "left" ), ipoib->data_qkey,
  657. ipoib->broadcast_lid );
  658. /* Update data queue pair qkey */
  659. if ( ( rc = ib_modify_qp ( ipoib->ibdev, ipoib->data.qp,
  660. IB_MODIFY_QKEY, ipoib->data_qkey ) ) != 0 ){
  661. DBGC ( ipoib, "IPoIB %p could not update data qkey: %s\n",
  662. ipoib, strerror ( rc ) );
  663. return;
  664. }
  665. }
  666. /**
  667. * Handle IPoIB metadata receive completion
  668. *
  669. * @v ibdev Infiniband device
  670. * @v qp Queue pair
  671. * @v av Address vector, or NULL
  672. * @v iobuf I/O buffer
  673. * @v rc Completion status code
  674. */
  675. static void
  676. ipoib_meta_complete_recv ( struct ib_device *ibdev __unused,
  677. struct ib_queue_pair *qp,
  678. struct ib_address_vector *av __unused,
  679. struct io_buffer *iobuf, int rc ) {
  680. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  681. struct ipoib_device *ipoib = netdev->priv;
  682. struct ib_mad_sa *sa;
  683. if ( rc != 0 ) {
  684. DBGC ( ipoib, "IPoIB %p metadata RX completion error: %s\n",
  685. ipoib, strerror ( rc ) );
  686. goto done;
  687. }
  688. if ( iob_len ( iobuf ) < sizeof ( *sa ) ) {
  689. DBGC ( ipoib, "IPoIB %p received metadata packet too short "
  690. "to contain reply\n", ipoib );
  691. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  692. goto done;
  693. }
  694. sa = iobuf->data;
  695. if ( sa->mad_hdr.status != 0 ) {
  696. DBGC ( ipoib, "IPoIB %p metadata RX err status %04x\n",
  697. ipoib, ntohs ( sa->mad_hdr.status ) );
  698. goto done;
  699. }
  700. switch ( sa->mad_hdr.tid[0] ) {
  701. case IPOIB_TID_GET_PATH_REC:
  702. ipoib_recv_path_record ( ipoib, &sa->sa_data.path_record );
  703. break;
  704. case IPOIB_TID_MC_MEMBER_REC:
  705. ipoib_recv_mc_member_record ( ipoib,
  706. &sa->sa_data.mc_member_record );
  707. break;
  708. default:
  709. DBGC ( ipoib, "IPoIB %p unwanted response:\n",
  710. ipoib );
  711. DBGC_HD ( ipoib, sa, sizeof ( *sa ) );
  712. break;
  713. }
  714. done:
  715. free_iob ( iobuf );
  716. }
  717. /** IPoIB metadata completion operations */
  718. static struct ib_completion_queue_operations ipoib_meta_cq_op = {
  719. .complete_send = ipoib_meta_complete_send,
  720. .complete_recv = ipoib_meta_complete_recv,
  721. };
  722. /**
  723. * Refill IPoIB receive ring
  724. *
  725. * @v ipoib IPoIB device
  726. */
  727. static void ipoib_refill_recv ( struct ipoib_device *ipoib,
  728. struct ipoib_queue_set *qset ) {
  729. struct ib_device *ibdev = ipoib->ibdev;
  730. struct io_buffer *iobuf;
  731. int rc;
  732. while ( qset->qp->recv.fill < qset->recv_max_fill ) {
  733. iobuf = alloc_iob ( IPOIB_PKT_LEN );
  734. if ( ! iobuf )
  735. break;
  736. if ( ( rc = ib_post_recv ( ibdev, qset->qp, iobuf ) ) != 0 ) {
  737. free_iob ( iobuf );
  738. break;
  739. }
  740. }
  741. }
  742. /**
  743. * Poll IPoIB network device
  744. *
  745. * @v netdev Network device
  746. */
  747. static void ipoib_poll ( struct net_device *netdev ) {
  748. struct ipoib_device *ipoib = netdev->priv;
  749. struct ib_device *ibdev = ipoib->ibdev;
  750. ib_poll_cq ( ibdev, ipoib->meta.cq );
  751. ib_poll_cq ( ibdev, ipoib->data.cq );
  752. ipoib_refill_recv ( ipoib, &ipoib->meta );
  753. ipoib_refill_recv ( ipoib, &ipoib->data );
  754. }
  755. /**
  756. * Enable/disable interrupts on IPoIB network device
  757. *
  758. * @v netdev Network device
  759. * @v enable Interrupts should be enabled
  760. */
  761. static void ipoib_irq ( struct net_device *netdev __unused,
  762. int enable __unused ) {
  763. /* No implementation */
  764. }
  765. /**
  766. * Join IPv4 broadcast multicast group
  767. *
  768. * @v ipoib IPoIB device
  769. * @ret rc Return status code
  770. */
  771. static int ipoib_join_broadcast_group ( struct ipoib_device *ipoib ) {
  772. int rc;
  773. /* Sanity check */
  774. if ( ! ipoib->data.qp )
  775. return 0;
  776. /* Attach data queue to broadcast multicast GID */
  777. assert ( ipoib->broadcast_attached == 0 );
  778. if ( ( rc = ib_mcast_attach ( ipoib->ibdev, ipoib->data.qp,
  779. &ipoib->broadcast_gid ) ) != 0 ){
  780. DBGC ( ipoib, "IPoIB %p could not attach to broadcast GID: "
  781. "%s\n", ipoib, strerror ( rc ) );
  782. return rc;
  783. }
  784. ipoib->broadcast_attached = 1;
  785. /* Initiate broadcast group join */
  786. if ( ( rc = ipoib_mc_member_record ( ipoib, &ipoib->broadcast_gid,
  787. 1 ) ) != 0 ) {
  788. DBGC ( ipoib, "IPoIB %p could not send broadcast join: %s\n",
  789. ipoib, strerror ( rc ) );
  790. return rc;
  791. }
  792. /* We will set link up on the network device when we receive
  793. * the broadcast join response.
  794. */
  795. return 0;
  796. }
  797. /**
  798. * Leave IPv4 broadcast multicast group
  799. *
  800. * @v ipoib IPoIB device
  801. */
  802. static void ipoib_leave_broadcast_group ( struct ipoib_device *ipoib ) {
  803. /* Detach data queue from broadcast multicast GID */
  804. if ( ipoib->broadcast_attached ) {
  805. assert ( ipoib->data.qp != NULL );
  806. ib_mcast_detach ( ipoib->ibdev, ipoib->data.qp,
  807. &ipoib->broadcast_gid );
  808. ipoib->broadcast_attached = 0;
  809. }
  810. }
  811. /**
  812. * Open IPoIB network device
  813. *
  814. * @v netdev Network device
  815. * @ret rc Return status code
  816. */
  817. static int ipoib_open ( struct net_device *netdev ) {
  818. struct ipoib_device *ipoib = netdev->priv;
  819. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  820. int rc;
  821. /* Open IB device */
  822. if ( ( rc = ib_open ( ipoib->ibdev ) ) != 0 ) {
  823. DBGC ( ipoib, "IPoIB %p could not open device: %s\n",
  824. ipoib, strerror ( rc ) );
  825. goto err_ib_open;
  826. }
  827. /* Allocate metadata queue set */
  828. if ( ( rc = ipoib_create_qset ( ipoib, &ipoib->meta,
  829. IPOIB_META_NUM_CQES,
  830. &ipoib_meta_cq_op,
  831. IPOIB_META_NUM_SEND_WQES,
  832. IPOIB_META_NUM_RECV_WQES,
  833. IB_GLOBAL_QKEY ) ) != 0 ) {
  834. DBGC ( ipoib, "IPoIB %p could not allocate metadata QP: %s\n",
  835. ipoib, strerror ( rc ) );
  836. goto err_create_meta_qset;
  837. }
  838. /* Allocate data queue set */
  839. if ( ( rc = ipoib_create_qset ( ipoib, &ipoib->data,
  840. IPOIB_DATA_NUM_CQES,
  841. &ipoib_data_cq_op,
  842. IPOIB_DATA_NUM_SEND_WQES,
  843. IPOIB_DATA_NUM_RECV_WQES,
  844. IB_GLOBAL_QKEY ) ) != 0 ) {
  845. DBGC ( ipoib, "IPoIB %p could not allocate data QP: %s\n",
  846. ipoib, strerror ( rc ) );
  847. goto err_create_data_qset;
  848. }
  849. /* Update MAC address with data QPN */
  850. mac->qpn = htonl ( ipoib->data.qp->qpn );
  851. /* Fill receive rings */
  852. ipoib_refill_recv ( ipoib, &ipoib->meta );
  853. ipoib_refill_recv ( ipoib, &ipoib->data );
  854. /* Join broadcast group */
  855. if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
  856. DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
  857. ipoib, strerror ( rc ) );
  858. goto err_join_broadcast;
  859. }
  860. return 0;
  861. err_join_broadcast:
  862. ipoib_destroy_qset ( ipoib, &ipoib->data );
  863. err_create_data_qset:
  864. ipoib_destroy_qset ( ipoib, &ipoib->meta );
  865. err_create_meta_qset:
  866. ib_close ( ipoib->ibdev );
  867. err_ib_open:
  868. return rc;
  869. }
  870. /**
  871. * Close IPoIB network device
  872. *
  873. * @v netdev Network device
  874. */
  875. static void ipoib_close ( struct net_device *netdev ) {
  876. struct ipoib_device *ipoib = netdev->priv;
  877. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  878. /* Leave broadcast group */
  879. ipoib_leave_broadcast_group ( ipoib );
  880. /* Remove data QPN from MAC address */
  881. mac->qpn = 0;
  882. /* Tear down the queues */
  883. ipoib_destroy_qset ( ipoib, &ipoib->data );
  884. ipoib_destroy_qset ( ipoib, &ipoib->meta );
  885. /* Close IB device */
  886. ib_close ( ipoib->ibdev );
  887. }
  888. /** IPoIB network device operations */
  889. static struct net_device_operations ipoib_operations = {
  890. .open = ipoib_open,
  891. .close = ipoib_close,
  892. .transmit = ipoib_transmit,
  893. .poll = ipoib_poll,
  894. .irq = ipoib_irq,
  895. };
  896. /**
  897. * Update IPoIB dynamic Infiniband parameters
  898. *
  899. * @v ipoib IPoIB device
  900. *
  901. * The Infiniband port GID and partition key will change at runtime,
  902. * when the link is established (or lost). The MAC address is based
  903. * on the port GID, and the broadcast GID is based on the partition
  904. * key. This function recalculates these IPoIB device parameters.
  905. */
  906. static void ipoib_set_ib_params ( struct ipoib_device *ipoib ) {
  907. struct ib_device *ibdev = ipoib->ibdev;
  908. struct net_device *netdev = ipoib->netdev;
  909. struct ipoib_mac *mac;
  910. /* Calculate GID portion of MAC address based on port GID */
  911. mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  912. memcpy ( &mac->gid, &ibdev->gid, sizeof ( mac->gid ) );
  913. /* Calculate broadcast GID based on partition key */
  914. memcpy ( &ipoib->broadcast_gid, &ipoib_broadcast.gid,
  915. sizeof ( ipoib->broadcast_gid ) );
  916. ipoib->broadcast_gid.u.words[2] = htons ( ibdev->pkey );
  917. /* Set net device link state to reflect Infiniband link state */
  918. if ( ib_link_ok ( ibdev ) ) {
  919. netdev_link_up ( netdev );
  920. } else {
  921. netdev_link_down ( netdev );
  922. }
  923. }
  924. /**
  925. * Handle link status change
  926. *
  927. * @v ibdev Infiniband device
  928. */
  929. void ipoib_link_state_changed ( struct ib_device *ibdev ) {
  930. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  931. struct ipoib_device *ipoib = netdev->priv;
  932. int rc;
  933. /* Leave existing broadcast group */
  934. ipoib_leave_broadcast_group ( ipoib );
  935. /* Update MAC address and broadcast GID based on new port GID
  936. * and partition key.
  937. */
  938. ipoib_set_ib_params ( ipoib );
  939. /* Join new broadcast group */
  940. if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
  941. DBGC ( ipoib, "IPoIB %p could not rejoin broadcast group: "
  942. "%s\n", ipoib, strerror ( rc ) );
  943. return;
  944. }
  945. }
  946. /**
  947. * Probe IPoIB device
  948. *
  949. * @v ibdev Infiniband device
  950. * @ret rc Return status code
  951. */
  952. int ipoib_probe ( struct ib_device *ibdev ) {
  953. struct net_device *netdev;
  954. struct ipoib_device *ipoib;
  955. int rc;
  956. /* Allocate network device */
  957. netdev = alloc_ipoibdev ( sizeof ( *ipoib ) );
  958. if ( ! netdev )
  959. return -ENOMEM;
  960. netdev_init ( netdev, &ipoib_operations );
  961. ipoib = netdev->priv;
  962. ib_set_ownerdata ( ibdev, netdev );
  963. netdev->dev = ibdev->dev;
  964. memset ( ipoib, 0, sizeof ( *ipoib ) );
  965. ipoib->netdev = netdev;
  966. ipoib->ibdev = ibdev;
  967. /* Calculate as much of the broadcast GID and the MAC address
  968. * as we can. We won't know either of these in full until we
  969. * have link-up.
  970. */
  971. ipoib_set_ib_params ( ipoib );
  972. /* Register network device */
  973. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  974. goto err_register_netdev;
  975. return 0;
  976. err_register_netdev:
  977. netdev_nullify ( netdev );
  978. netdev_put ( netdev );
  979. return rc;
  980. }
  981. /**
  982. * Remove IPoIB device
  983. *
  984. * @v ibdev Infiniband device
  985. */
  986. void ipoib_remove ( struct ib_device *ibdev ) {
  987. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  988. unregister_netdev ( netdev );
  989. netdev_nullify ( netdev );
  990. netdev_put ( netdev );
  991. }