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

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