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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. .push = ipoib_push,
  305. .pull = ipoib_pull,
  306. .ntoa = ipoib_ntoa,
  307. .mc_hash = ipoib_mc_hash,
  308. };
  309. /****************************************************************************
  310. *
  311. * IPoIB network device
  312. *
  313. ****************************************************************************
  314. */
  315. /**
  316. * Destroy queue set
  317. *
  318. * @v ipoib IPoIB device
  319. * @v qset Queue set
  320. */
  321. static void ipoib_destroy_qset ( struct ipoib_device *ipoib,
  322. struct ipoib_queue_set *qset ) {
  323. struct ib_device *ibdev = ipoib->ibdev;
  324. if ( qset->qp )
  325. ib_destroy_qp ( ibdev, qset->qp );
  326. if ( qset->cq )
  327. ib_destroy_cq ( ibdev, qset->cq );
  328. memset ( qset, 0, sizeof ( *qset ) );
  329. }
  330. /**
  331. * Create queue set
  332. *
  333. * @v ipoib IPoIB device
  334. * @v qset Queue set
  335. * @v num_cqes Number of completion queue entries
  336. * @v cq_op Completion queue operations
  337. * @v num_send_wqes Number of send work queue entries
  338. * @v num_recv_wqes Number of receive work queue entries
  339. * @v qkey Queue key
  340. * @ret rc Return status code
  341. */
  342. static int ipoib_create_qset ( struct ipoib_device *ipoib,
  343. struct ipoib_queue_set *qset,
  344. unsigned int num_cqes,
  345. struct ib_completion_queue_operations *cq_op,
  346. unsigned int num_send_wqes,
  347. unsigned int num_recv_wqes,
  348. unsigned long qkey ) {
  349. struct ib_device *ibdev = ipoib->ibdev;
  350. int rc;
  351. /* Sanity check */
  352. assert ( qset->cq == NULL );
  353. assert ( qset->qp == NULL );
  354. /* Store queue parameters */
  355. qset->recv_max_fill = num_recv_wqes;
  356. /* Allocate completion queue */
  357. qset->cq = ib_create_cq ( ibdev, num_cqes, cq_op );
  358. if ( ! qset->cq ) {
  359. DBGC ( ipoib, "IPoIB %p could not allocate completion queue\n",
  360. ipoib );
  361. rc = -ENOMEM;
  362. goto err;
  363. }
  364. /* Allocate queue pair */
  365. qset->qp = ib_create_qp ( ibdev, num_send_wqes, qset->cq,
  366. num_recv_wqes, qset->cq, qkey );
  367. if ( ! qset->qp ) {
  368. DBGC ( ipoib, "IPoIB %p could not allocate queue pair\n",
  369. ipoib );
  370. rc = -ENOMEM;
  371. goto err;
  372. }
  373. ib_qp_set_ownerdata ( qset->qp, ipoib->netdev );
  374. return 0;
  375. err:
  376. ipoib_destroy_qset ( ipoib, qset );
  377. return rc;
  378. }
  379. /**
  380. * Transmit path record request
  381. *
  382. * @v ipoib IPoIB device
  383. * @v gid Destination GID
  384. * @ret rc Return status code
  385. */
  386. static int ipoib_get_path_record ( struct ipoib_device *ipoib,
  387. struct ib_gid *gid ) {
  388. struct ib_device *ibdev = ipoib->ibdev;
  389. struct io_buffer *iobuf;
  390. struct ib_mad_sa *sa;
  391. struct ib_address_vector av;
  392. int rc;
  393. /* Allocate I/O buffer */
  394. iobuf = alloc_iob ( sizeof ( *sa ) );
  395. if ( ! iobuf )
  396. return -ENOMEM;
  397. iob_put ( iobuf, sizeof ( *sa ) );
  398. sa = iobuf->data;
  399. memset ( sa, 0, sizeof ( *sa ) );
  400. /* Construct path record request */
  401. sa->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  402. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  403. sa->mad_hdr.class_version = 2;
  404. sa->mad_hdr.method = IB_MGMT_METHOD_GET;
  405. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  406. sa->mad_hdr.tid[0] = IPOIB_TID_GET_PATH_REC;
  407. sa->mad_hdr.tid[1] = ipoib_meta_tid++;
  408. sa->sa_hdr.comp_mask[1] =
  409. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  410. memcpy ( &sa->sa_data.path_record.dgid, gid,
  411. sizeof ( sa->sa_data.path_record.dgid ) );
  412. memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
  413. sizeof ( sa->sa_data.path_record.sgid ) );
  414. /* Construct address vector */
  415. memset ( &av, 0, sizeof ( av ) );
  416. av.lid = ibdev->sm_lid;
  417. av.sl = ibdev->sm_sl;
  418. av.qpn = IB_SA_QPN;
  419. av.qkey = IB_GLOBAL_QKEY;
  420. /* Post send request */
  421. if ( ( rc = ib_post_send ( ibdev, ipoib->meta.qp, &av,
  422. iobuf ) ) != 0 ) {
  423. DBGC ( ipoib, "IPoIB %p could not send get path record: %s\n",
  424. ipoib, strerror ( rc ) );
  425. free_iob ( iobuf );
  426. return rc;
  427. }
  428. return 0;
  429. }
  430. /**
  431. * Transmit multicast group membership request
  432. *
  433. * @v ipoib IPoIB device
  434. * @v gid Multicast GID
  435. * @v join Join (rather than leave) group
  436. * @ret rc Return status code
  437. */
  438. static int ipoib_mc_member_record ( struct ipoib_device *ipoib,
  439. struct ib_gid *gid, int join ) {
  440. struct ib_device *ibdev = ipoib->ibdev;
  441. struct io_buffer *iobuf;
  442. struct ib_mad_sa *sa;
  443. struct ib_address_vector av;
  444. int rc;
  445. /* Allocate I/O buffer */
  446. iobuf = alloc_iob ( sizeof ( *sa ) );
  447. if ( ! iobuf )
  448. return -ENOMEM;
  449. iob_put ( iobuf, sizeof ( *sa ) );
  450. sa = iobuf->data;
  451. memset ( sa, 0, sizeof ( *sa ) );
  452. /* Construct path record request */
  453. sa->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  454. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  455. sa->mad_hdr.class_version = 2;
  456. sa->mad_hdr.method =
  457. ( join ? IB_MGMT_METHOD_SET : IB_MGMT_METHOD_DELETE );
  458. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_MC_MEMBER_REC );
  459. sa->mad_hdr.tid[0] = IPOIB_TID_MC_MEMBER_REC;
  460. sa->mad_hdr.tid[1] = ipoib_meta_tid++;
  461. sa->sa_hdr.comp_mask[1] =
  462. htonl ( IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
  463. IB_SA_MCMEMBER_REC_JOIN_STATE );
  464. sa->sa_data.mc_member_record.scope__join_state = 1;
  465. memcpy ( &sa->sa_data.mc_member_record.mgid, gid,
  466. sizeof ( sa->sa_data.mc_member_record.mgid ) );
  467. memcpy ( &sa->sa_data.mc_member_record.port_gid, &ibdev->gid,
  468. sizeof ( sa->sa_data.mc_member_record.port_gid ) );
  469. /* Construct address vector */
  470. memset ( &av, 0, sizeof ( av ) );
  471. av.lid = ibdev->sm_lid;
  472. av.sl = ibdev->sm_sl;
  473. av.qpn = IB_SA_QPN;
  474. av.qkey = IB_GLOBAL_QKEY;
  475. /* Post send request */
  476. if ( ( rc = ib_post_send ( ibdev, ipoib->meta.qp, &av,
  477. iobuf ) ) != 0 ) {
  478. DBGC ( ipoib, "IPoIB %p could not send get path record: %s\n",
  479. ipoib, strerror ( rc ) );
  480. free_iob ( iobuf );
  481. return rc;
  482. }
  483. return 0;
  484. }
  485. /**
  486. * Transmit packet via IPoIB network device
  487. *
  488. * @v netdev Network device
  489. * @v iobuf I/O buffer
  490. * @ret rc Return status code
  491. */
  492. static int ipoib_transmit ( struct net_device *netdev,
  493. struct io_buffer *iobuf ) {
  494. struct ipoib_device *ipoib = netdev->priv;
  495. struct ib_device *ibdev = ipoib->ibdev;
  496. struct ipoib_hdr *ipoib_hdr;
  497. struct ipoib_peer *dest;
  498. struct ib_address_vector av;
  499. struct ib_gid *gid;
  500. /* Sanity check */
  501. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  502. DBGC ( ipoib, "IPoIB %p buffer too short\n", ipoib );
  503. return -EINVAL;
  504. }
  505. ipoib_hdr = iobuf->data;
  506. /* Attempting transmission while link is down will put the
  507. * queue pair into an error state, so don't try it.
  508. */
  509. if ( ! ib_link_ok ( ibdev ) )
  510. return -ENETUNREACH;
  511. /* Identify destination address */
  512. dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
  513. if ( ! dest )
  514. return -ENXIO;
  515. ipoib_hdr->u.reserved = 0;
  516. /* Construct address vector */
  517. memset ( &av, 0, sizeof ( av ) );
  518. av.qkey = ipoib->data_qkey;
  519. av.gid_present = 1;
  520. if ( dest->mac.qpn == htonl ( IPOIB_BROADCAST_QPN ) ) {
  521. /* Broadcast */
  522. av.qpn = IB_BROADCAST_QPN;
  523. av.lid = ipoib->broadcast_lid;
  524. gid = &ipoib->broadcast_gid;
  525. } else {
  526. /* Unicast */
  527. if ( ! dest->lid ) {
  528. /* No LID yet - get path record to fetch LID */
  529. ipoib_get_path_record ( ipoib, &dest->mac.gid );
  530. return -ENOENT;
  531. }
  532. av.qpn = ntohl ( dest->mac.qpn );
  533. av.lid = dest->lid;
  534. av.rate = dest->rate;
  535. av.sl = dest->sl;
  536. gid = &dest->mac.gid;
  537. }
  538. memcpy ( &av.gid, gid, sizeof ( av.gid ) );
  539. return ib_post_send ( ibdev, ipoib->data.qp, &av, iobuf );
  540. }
  541. /**
  542. * Handle IPoIB data send completion
  543. *
  544. * @v ibdev Infiniband device
  545. * @v qp Queue pair
  546. * @v iobuf I/O buffer
  547. * @v rc Completion status code
  548. */
  549. static void ipoib_data_complete_send ( struct ib_device *ibdev __unused,
  550. struct ib_queue_pair *qp,
  551. struct io_buffer *iobuf, int rc ) {
  552. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  553. netdev_tx_complete_err ( netdev, iobuf, rc );
  554. }
  555. /**
  556. * Handle IPoIB data receive completion
  557. *
  558. * @v ibdev Infiniband device
  559. * @v qp Queue pair
  560. * @v av Address vector, or NULL
  561. * @v iobuf I/O buffer
  562. * @v rc Completion status code
  563. */
  564. static void ipoib_data_complete_recv ( struct ib_device *ibdev __unused,
  565. struct ib_queue_pair *qp,
  566. struct ib_address_vector *av,
  567. struct io_buffer *iobuf, int rc ) {
  568. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  569. struct ipoib_device *ipoib = netdev->priv;
  570. struct ipoib_hdr *ipoib_hdr;
  571. struct ipoib_peer *src;
  572. if ( rc != 0 ) {
  573. netdev_rx_err ( netdev, iobuf, rc );
  574. return;
  575. }
  576. /* Sanity check */
  577. if ( iob_len ( iobuf ) < sizeof ( struct ipoib_hdr ) ) {
  578. DBGC ( ipoib, "IPoIB %p received data packet too short to "
  579. "contain IPoIB header\n", ipoib );
  580. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  581. netdev_rx_err ( netdev, iobuf, -EIO );
  582. return;
  583. }
  584. ipoib_hdr = iobuf->data;
  585. /* Parse source address */
  586. if ( av->gid_present ) {
  587. src = ipoib_cache_peer ( &av->gid, av->qpn );
  588. ipoib_hdr->u.peer.src = src->key;
  589. }
  590. /* Hand off to network layer */
  591. netdev_rx ( netdev, iobuf );
  592. }
  593. /** IPoIB data completion operations */
  594. static struct ib_completion_queue_operations ipoib_data_cq_op = {
  595. .complete_send = ipoib_data_complete_send,
  596. .complete_recv = ipoib_data_complete_recv,
  597. };
  598. /**
  599. * Handle IPoIB metadata send completion
  600. *
  601. * @v ibdev Infiniband device
  602. * @v qp Queue pair
  603. * @v iobuf I/O buffer
  604. * @v rc Completion status code
  605. */
  606. static void ipoib_meta_complete_send ( struct ib_device *ibdev __unused,
  607. struct ib_queue_pair *qp,
  608. struct io_buffer *iobuf, int rc ) {
  609. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  610. struct ipoib_device *ipoib = netdev->priv;
  611. if ( rc != 0 ) {
  612. DBGC ( ipoib, "IPoIB %p metadata TX completion error: %s\n",
  613. ipoib, strerror ( rc ) );
  614. }
  615. free_iob ( iobuf );
  616. }
  617. /**
  618. * Handle received IPoIB path record
  619. *
  620. * @v ipoib IPoIB device
  621. * @v path_record Path record
  622. */
  623. static void ipoib_recv_path_record ( struct ipoib_device *ipoib,
  624. struct ib_path_record *path_record ) {
  625. struct ipoib_peer *peer;
  626. /* Locate peer cache entry */
  627. peer = ipoib_lookup_peer_by_gid ( &path_record->dgid );
  628. if ( ! peer ) {
  629. DBGC ( ipoib, "IPoIB %p received unsolicited path record\n",
  630. ipoib );
  631. return;
  632. }
  633. /* Update path cache entry */
  634. peer->lid = ntohs ( path_record->dlid );
  635. peer->sl = ( path_record->reserved__sl & 0x0f );
  636. peer->rate = ( path_record->rate_selector__rate & 0x3f );
  637. DBG ( "IPoIB peer %x has dlid %x sl %x rate %x\n",
  638. peer->key, peer->lid, peer->sl, peer->rate );
  639. }
  640. /**
  641. * Handle received IPoIB multicast membership record
  642. *
  643. * @v ipoib IPoIB device
  644. * @v mc_member_record Multicast membership record
  645. */
  646. static void ipoib_recv_mc_member_record ( struct ipoib_device *ipoib,
  647. struct ib_mc_member_record *mc_member_record ) {
  648. int joined;
  649. int rc;
  650. /* Record parameters */
  651. joined = ( mc_member_record->scope__join_state & 0x0f );
  652. ipoib->data_qkey = ntohl ( mc_member_record->qkey );
  653. ipoib->broadcast_lid = ntohs ( mc_member_record->mlid );
  654. DBGC ( ipoib, "IPoIB %p %s broadcast group: qkey %lx mlid %x\n",
  655. ipoib, ( joined ? "joined" : "left" ), ipoib->data_qkey,
  656. ipoib->broadcast_lid );
  657. /* Update data queue pair qkey */
  658. if ( ( rc = ib_modify_qp ( ipoib->ibdev, ipoib->data.qp,
  659. IB_MODIFY_QKEY, ipoib->data_qkey ) ) != 0 ){
  660. DBGC ( ipoib, "IPoIB %p could not update data qkey: %s\n",
  661. ipoib, strerror ( rc ) );
  662. return;
  663. }
  664. }
  665. /**
  666. * Handle IPoIB metadata receive completion
  667. *
  668. * @v ibdev Infiniband device
  669. * @v qp Queue pair
  670. * @v av Address vector, or NULL
  671. * @v iobuf I/O buffer
  672. * @v rc Completion status code
  673. */
  674. static void
  675. ipoib_meta_complete_recv ( struct ib_device *ibdev __unused,
  676. struct ib_queue_pair *qp,
  677. struct ib_address_vector *av __unused,
  678. struct io_buffer *iobuf, int rc ) {
  679. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  680. struct ipoib_device *ipoib = netdev->priv;
  681. struct ib_mad_sa *sa;
  682. if ( rc != 0 ) {
  683. DBGC ( ipoib, "IPoIB %p metadata RX completion error: %s\n",
  684. ipoib, strerror ( rc ) );
  685. goto done;
  686. }
  687. if ( iob_len ( iobuf ) < sizeof ( *sa ) ) {
  688. DBGC ( ipoib, "IPoIB %p received metadata packet too short "
  689. "to contain reply\n", ipoib );
  690. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  691. goto done;
  692. }
  693. sa = iobuf->data;
  694. if ( sa->mad_hdr.status != 0 ) {
  695. DBGC ( ipoib, "IPoIB %p metadata RX err status %04x\n",
  696. ipoib, ntohs ( sa->mad_hdr.status ) );
  697. goto done;
  698. }
  699. switch ( sa->mad_hdr.tid[0] ) {
  700. case IPOIB_TID_GET_PATH_REC:
  701. ipoib_recv_path_record ( ipoib, &sa->sa_data.path_record );
  702. break;
  703. case IPOIB_TID_MC_MEMBER_REC:
  704. ipoib_recv_mc_member_record ( ipoib,
  705. &sa->sa_data.mc_member_record );
  706. break;
  707. default:
  708. DBGC ( ipoib, "IPoIB %p unwanted response:\n",
  709. ipoib );
  710. DBGC_HD ( ipoib, sa, sizeof ( *sa ) );
  711. break;
  712. }
  713. done:
  714. free_iob ( iobuf );
  715. }
  716. /** IPoIB metadata completion operations */
  717. static struct ib_completion_queue_operations ipoib_meta_cq_op = {
  718. .complete_send = ipoib_meta_complete_send,
  719. .complete_recv = ipoib_meta_complete_recv,
  720. };
  721. /**
  722. * Refill IPoIB receive ring
  723. *
  724. * @v ipoib IPoIB device
  725. */
  726. static void ipoib_refill_recv ( struct ipoib_device *ipoib,
  727. struct ipoib_queue_set *qset ) {
  728. struct ib_device *ibdev = ipoib->ibdev;
  729. struct io_buffer *iobuf;
  730. int rc;
  731. while ( qset->qp->recv.fill < qset->recv_max_fill ) {
  732. iobuf = alloc_iob ( IPOIB_PKT_LEN );
  733. if ( ! iobuf )
  734. break;
  735. if ( ( rc = ib_post_recv ( ibdev, qset->qp, iobuf ) ) != 0 ) {
  736. free_iob ( iobuf );
  737. break;
  738. }
  739. }
  740. }
  741. /**
  742. * Poll IPoIB network device
  743. *
  744. * @v netdev Network device
  745. */
  746. static void ipoib_poll ( struct net_device *netdev ) {
  747. struct ipoib_device *ipoib = netdev->priv;
  748. struct ib_device *ibdev = ipoib->ibdev;
  749. ib_poll_cq ( ibdev, ipoib->meta.cq );
  750. ib_poll_cq ( ibdev, ipoib->data.cq );
  751. ipoib_refill_recv ( ipoib, &ipoib->meta );
  752. ipoib_refill_recv ( ipoib, &ipoib->data );
  753. }
  754. /**
  755. * Enable/disable interrupts on IPoIB network device
  756. *
  757. * @v netdev Network device
  758. * @v enable Interrupts should be enabled
  759. */
  760. static void ipoib_irq ( struct net_device *netdev __unused,
  761. int enable __unused ) {
  762. /* No implementation */
  763. }
  764. /**
  765. * Join IPv4 broadcast multicast group
  766. *
  767. * @v ipoib IPoIB device
  768. * @ret rc Return status code
  769. */
  770. static int ipoib_join_broadcast_group ( struct ipoib_device *ipoib ) {
  771. int rc;
  772. /* Sanity check */
  773. if ( ! ipoib->data.qp )
  774. return 0;
  775. /* Attach data queue to broadcast multicast GID */
  776. assert ( ipoib->broadcast_attached == 0 );
  777. if ( ( rc = ib_mcast_attach ( ipoib->ibdev, ipoib->data.qp,
  778. &ipoib->broadcast_gid ) ) != 0 ){
  779. DBGC ( ipoib, "IPoIB %p could not attach to broadcast GID: "
  780. "%s\n", ipoib, strerror ( rc ) );
  781. return rc;
  782. }
  783. ipoib->broadcast_attached = 1;
  784. /* Initiate broadcast group join */
  785. if ( ( rc = ipoib_mc_member_record ( ipoib, &ipoib->broadcast_gid,
  786. 1 ) ) != 0 ) {
  787. DBGC ( ipoib, "IPoIB %p could not send broadcast join: %s\n",
  788. ipoib, strerror ( rc ) );
  789. return rc;
  790. }
  791. /* We will set link up on the network device when we receive
  792. * the broadcast join response.
  793. */
  794. return 0;
  795. }
  796. /**
  797. * Leave IPv4 broadcast multicast group
  798. *
  799. * @v ipoib IPoIB device
  800. */
  801. static void ipoib_leave_broadcast_group ( struct ipoib_device *ipoib ) {
  802. /* Detach data queue from broadcast multicast GID */
  803. if ( ipoib->broadcast_attached ) {
  804. assert ( ipoib->data.qp != NULL );
  805. ib_mcast_detach ( ipoib->ibdev, ipoib->data.qp,
  806. &ipoib->broadcast_gid );
  807. ipoib->broadcast_attached = 0;
  808. }
  809. }
  810. /**
  811. * Open IPoIB network device
  812. *
  813. * @v netdev Network device
  814. * @ret rc Return status code
  815. */
  816. static int ipoib_open ( struct net_device *netdev ) {
  817. struct ipoib_device *ipoib = netdev->priv;
  818. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  819. int rc;
  820. /* Open IB device */
  821. if ( ( rc = ib_open ( ipoib->ibdev ) ) != 0 ) {
  822. DBGC ( ipoib, "IPoIB %p could not open device: %s\n",
  823. ipoib, strerror ( rc ) );
  824. goto err_ib_open;
  825. }
  826. /* Allocate metadata queue set */
  827. if ( ( rc = ipoib_create_qset ( ipoib, &ipoib->meta,
  828. IPOIB_META_NUM_CQES,
  829. &ipoib_meta_cq_op,
  830. IPOIB_META_NUM_SEND_WQES,
  831. IPOIB_META_NUM_RECV_WQES,
  832. IB_GLOBAL_QKEY ) ) != 0 ) {
  833. DBGC ( ipoib, "IPoIB %p could not allocate metadata QP: %s\n",
  834. ipoib, strerror ( rc ) );
  835. goto err_create_meta_qset;
  836. }
  837. /* Allocate data queue set */
  838. if ( ( rc = ipoib_create_qset ( ipoib, &ipoib->data,
  839. IPOIB_DATA_NUM_CQES,
  840. &ipoib_data_cq_op,
  841. IPOIB_DATA_NUM_SEND_WQES,
  842. IPOIB_DATA_NUM_RECV_WQES,
  843. IB_GLOBAL_QKEY ) ) != 0 ) {
  844. DBGC ( ipoib, "IPoIB %p could not allocate data QP: %s\n",
  845. ipoib, strerror ( rc ) );
  846. goto err_create_data_qset;
  847. }
  848. /* Update MAC address with data QPN */
  849. mac->qpn = htonl ( ipoib->data.qp->qpn );
  850. /* Fill receive rings */
  851. ipoib_refill_recv ( ipoib, &ipoib->meta );
  852. ipoib_refill_recv ( ipoib, &ipoib->data );
  853. /* Join broadcast group */
  854. if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
  855. DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
  856. ipoib, strerror ( rc ) );
  857. goto err_join_broadcast;
  858. }
  859. return 0;
  860. err_join_broadcast:
  861. ipoib_destroy_qset ( ipoib, &ipoib->data );
  862. err_create_data_qset:
  863. ipoib_destroy_qset ( ipoib, &ipoib->meta );
  864. err_create_meta_qset:
  865. ib_close ( ipoib->ibdev );
  866. err_ib_open:
  867. return rc;
  868. }
  869. /**
  870. * Close IPoIB network device
  871. *
  872. * @v netdev Network device
  873. */
  874. static void ipoib_close ( struct net_device *netdev ) {
  875. struct ipoib_device *ipoib = netdev->priv;
  876. struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  877. /* Leave broadcast group */
  878. ipoib_leave_broadcast_group ( ipoib );
  879. /* Remove data QPN from MAC address */
  880. mac->qpn = 0;
  881. /* Tear down the queues */
  882. ipoib_destroy_qset ( ipoib, &ipoib->data );
  883. ipoib_destroy_qset ( ipoib, &ipoib->meta );
  884. /* Close IB device */
  885. ib_close ( ipoib->ibdev );
  886. }
  887. /** IPoIB network device operations */
  888. static struct net_device_operations ipoib_operations = {
  889. .open = ipoib_open,
  890. .close = ipoib_close,
  891. .transmit = ipoib_transmit,
  892. .poll = ipoib_poll,
  893. .irq = ipoib_irq,
  894. };
  895. /**
  896. * Update IPoIB dynamic Infiniband parameters
  897. *
  898. * @v ipoib IPoIB device
  899. *
  900. * The Infiniband port GID and partition key will change at runtime,
  901. * when the link is established (or lost). The MAC address is based
  902. * on the port GID, and the broadcast GID is based on the partition
  903. * key. This function recalculates these IPoIB device parameters.
  904. */
  905. static void ipoib_set_ib_params ( struct ipoib_device *ipoib ) {
  906. struct ib_device *ibdev = ipoib->ibdev;
  907. struct net_device *netdev = ipoib->netdev;
  908. struct ipoib_mac *mac;
  909. /* Calculate GID portion of MAC address based on port GID */
  910. mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  911. memcpy ( &mac->gid, &ibdev->gid, sizeof ( mac->gid ) );
  912. /* Calculate broadcast GID based on partition key */
  913. memcpy ( &ipoib->broadcast_gid, &ipoib_broadcast.gid,
  914. sizeof ( ipoib->broadcast_gid ) );
  915. ipoib->broadcast_gid.u.words[2] = htons ( ibdev->pkey );
  916. /* Set net device link state to reflect Infiniband link state */
  917. if ( ib_link_ok ( ibdev ) ) {
  918. netdev_link_up ( netdev );
  919. } else {
  920. netdev_link_down ( netdev );
  921. }
  922. }
  923. /**
  924. * Handle link status change
  925. *
  926. * @v ibdev Infiniband device
  927. */
  928. void ipoib_link_state_changed ( struct ib_device *ibdev ) {
  929. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  930. struct ipoib_device *ipoib = netdev->priv;
  931. int rc;
  932. /* Leave existing broadcast group */
  933. ipoib_leave_broadcast_group ( ipoib );
  934. /* Update MAC address and broadcast GID based on new port GID
  935. * and partition key.
  936. */
  937. ipoib_set_ib_params ( ipoib );
  938. /* Join new broadcast group */
  939. if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
  940. DBGC ( ipoib, "IPoIB %p could not rejoin broadcast group: "
  941. "%s\n", ipoib, strerror ( rc ) );
  942. return;
  943. }
  944. }
  945. /**
  946. * Probe IPoIB device
  947. *
  948. * @v ibdev Infiniband device
  949. * @ret rc Return status code
  950. */
  951. int ipoib_probe ( struct ib_device *ibdev ) {
  952. struct net_device *netdev;
  953. struct ipoib_device *ipoib;
  954. int rc;
  955. /* Allocate network device */
  956. netdev = alloc_ipoibdev ( sizeof ( *ipoib ) );
  957. if ( ! netdev )
  958. return -ENOMEM;
  959. netdev_init ( netdev, &ipoib_operations );
  960. ipoib = netdev->priv;
  961. ib_set_ownerdata ( ibdev, netdev );
  962. netdev->dev = ibdev->dev;
  963. memset ( ipoib, 0, sizeof ( *ipoib ) );
  964. ipoib->netdev = netdev;
  965. ipoib->ibdev = ibdev;
  966. /* Calculate as much of the broadcast GID and the MAC address
  967. * as we can. We won't know either of these in full until we
  968. * have link-up.
  969. */
  970. ipoib_set_ib_params ( ipoib );
  971. /* Register network device */
  972. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  973. goto err_register_netdev;
  974. return 0;
  975. err_register_netdev:
  976. netdev_nullify ( netdev );
  977. netdev_put ( netdev );
  978. return rc;
  979. }
  980. /**
  981. * Remove IPoIB device
  982. *
  983. * @v ibdev Infiniband device
  984. */
  985. void ipoib_remove ( struct ib_device *ibdev ) {
  986. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  987. unregister_netdev ( netdev );
  988. netdev_nullify ( netdev );
  989. netdev_put ( netdev );
  990. }
  991. /**
  992. * Allocate IPoIB device
  993. *
  994. * @v priv_size Size of driver private data
  995. * @ret netdev Network device, or NULL
  996. */
  997. struct net_device * alloc_ipoibdev ( size_t priv_size ) {
  998. struct net_device *netdev;
  999. netdev = alloc_netdev ( priv_size );
  1000. if ( netdev ) {
  1001. netdev->ll_protocol = &ipoib_protocol;
  1002. netdev->ll_broadcast = ( uint8_t * ) &ipoib_broadcast;
  1003. netdev->max_pkt_len = IPOIB_PKT_LEN;
  1004. }
  1005. return netdev;
  1006. }