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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <byteswap.h>
  23. #include <errno.h>
  24. #include "timer.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. /** IPoIB MTU */
  35. #define IPOIB_MTU 2048
  36. /** Number of IPoIB data send work queue entries */
  37. #define IPOIB_DATA_NUM_SEND_WQES 2
  38. /** Number of IPoIB data receive work queue entries */
  39. #define IPOIB_DATA_NUM_RECV_WQES 4
  40. /** Number of IPoIB data completion entries */
  41. #define IPOIB_DATA_NUM_CQES 8
  42. /** Number of IPoIB metadata send work queue entries */
  43. #define IPOIB_META_NUM_SEND_WQES 2
  44. /** Number of IPoIB metadata receive work queue entries */
  45. #define IPOIB_META_NUM_RECV_WQES 2
  46. /** Number of IPoIB metadata completion entries */
  47. #define IPOIB_META_NUM_CQES 8
  48. /** An IPoIB queue set */
  49. struct ipoib_queue_set {
  50. /** Completion queue */
  51. struct ib_completion_queue *cq;
  52. /** Queue pair */
  53. struct ib_queue_pair *qp;
  54. /** Receive work queue fill level */
  55. unsigned int recv_fill;
  56. /** Receive work queue maximum fill level */
  57. unsigned int recv_max_fill;
  58. };
  59. /** An IPoIB device */
  60. struct ipoib_device {
  61. /** Network device */
  62. struct net_device *netdev;
  63. /** Underlying Infiniband device */
  64. struct ib_device *ibdev;
  65. /** Data queue set */
  66. struct ipoib_queue_set data;
  67. /** Data queue set */
  68. struct ipoib_queue_set meta;
  69. /** Broadcast GID */
  70. struct ib_gid broadcast_gid;
  71. /** Broadcast LID */
  72. unsigned int broadcast_lid;
  73. /** Joined to broadcast group */
  74. int broadcast_joined;
  75. /** Data queue key */
  76. unsigned long data_qkey;
  77. };
  78. /**
  79. * IPoIB path cache entry
  80. *
  81. * This serves a similar role to the ARP cache for Ethernet. (ARP
  82. * *is* used on IPoIB; we have two caches to maintain.)
  83. */
  84. struct ipoib_cached_path {
  85. /** Destination GID */
  86. struct ib_gid gid;
  87. /** Destination LID */
  88. unsigned int dlid;
  89. /** Service level */
  90. unsigned int sl;
  91. /** Rate */
  92. unsigned int rate;
  93. };
  94. /** Number of IPoIB path cache entries */
  95. #define IPOIB_NUM_CACHED_PATHS 2
  96. /** IPoIB path cache */
  97. static struct ipoib_cached_path ipoib_path_cache[IPOIB_NUM_CACHED_PATHS];
  98. /** Oldest IPoIB path cache entry index */
  99. static unsigned int ipoib_path_cache_idx = 0;
  100. /** TID half used to identify get path record replies */
  101. #define IPOIB_TID_GET_PATH_REC 0x11111111UL
  102. /** TID half used to identify multicast member record replies */
  103. #define IPOIB_TID_MC_MEMBER_REC 0x22222222UL
  104. /** IPoIB metadata TID */
  105. static uint32_t ipoib_meta_tid = 0;
  106. /** IPv4 broadcast GID */
  107. static const struct ib_gid ipv4_broadcast_gid = {
  108. { { 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff } }
  110. };
  111. /** Maximum time we will wait for the broadcast join to succeed */
  112. #define IPOIB_JOIN_MAX_DELAY_MS 1000
  113. /****************************************************************************
  114. *
  115. * IPoIB link layer
  116. *
  117. ****************************************************************************
  118. */
  119. /** Broadcast QPN used in IPoIB MAC addresses
  120. *
  121. * This is a guaranteed invalid real QPN
  122. */
  123. #define IPOIB_BROADCAST_QPN 0xffffffffUL
  124. /** Broadcast IPoIB address */
  125. static struct ipoib_mac ipoib_broadcast = {
  126. .qpn = ntohl ( IPOIB_BROADCAST_QPN ),
  127. };
  128. /**
  129. * Transmit IPoIB packet
  130. *
  131. * @v iobuf I/O buffer
  132. * @v netdev Network device
  133. * @v net_protocol Network-layer protocol
  134. * @v ll_dest Link-layer destination address
  135. *
  136. * Prepends the IPoIB link-layer header and transmits the packet.
  137. */
  138. static int ipoib_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  139. struct net_protocol *net_protocol,
  140. const void *ll_dest ) {
  141. struct ipoib_hdr *ipoib_hdr =
  142. iob_push ( iobuf, sizeof ( *ipoib_hdr ) );
  143. /* Build IPoIB header */
  144. memcpy ( &ipoib_hdr->pseudo.peer, ll_dest,
  145. sizeof ( ipoib_hdr->pseudo.peer ) );
  146. ipoib_hdr->real.proto = net_protocol->net_proto;
  147. ipoib_hdr->real.reserved = 0;
  148. /* Hand off to network device */
  149. return netdev_tx ( netdev, iobuf );
  150. }
  151. /**
  152. * Process received IPoIB packet
  153. *
  154. * @v iobuf I/O buffer
  155. * @v netdev Network device
  156. *
  157. * Strips off the IPoIB link-layer header and passes up to the
  158. * network-layer protocol.
  159. */
  160. static int ipoib_rx ( struct io_buffer *iobuf, struct net_device *netdev ) {
  161. struct ipoib_hdr *ipoib_hdr = iobuf->data;
  162. /* Sanity check */
  163. if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
  164. DBG ( "IPoIB packet too short for link-layer header\n" );
  165. DBG_HD ( iobuf->data, iob_len ( iobuf ) );
  166. free_iob ( iobuf );
  167. return -EINVAL;
  168. }
  169. /* Strip off IPoIB header */
  170. iob_pull ( iobuf, sizeof ( *ipoib_hdr ) );
  171. /* Hand off to network-layer protocol */
  172. return net_rx ( iobuf, netdev, ipoib_hdr->real.proto,
  173. &ipoib_hdr->pseudo.peer );
  174. }
  175. /**
  176. * Transcribe IPoIB address
  177. *
  178. * @v ll_addr Link-layer address
  179. * @ret string Link-layer address in human-readable format
  180. */
  181. const char * ipoib_ntoa ( const void *ll_addr ) {
  182. static char buf[45];
  183. const struct ipoib_mac *mac = ll_addr;
  184. snprintf ( buf, sizeof ( buf ), "%08lx:%08lx:%08lx:%08lx:%08lx",
  185. htonl ( mac->qpn ), htonl ( mac->gid.u.dwords[0] ),
  186. htonl ( mac->gid.u.dwords[1] ),
  187. htonl ( mac->gid.u.dwords[2] ),
  188. htonl ( mac->gid.u.dwords[3] ) );
  189. return buf;
  190. }
  191. /** IPoIB protocol */
  192. struct ll_protocol ipoib_protocol __ll_protocol = {
  193. .name = "IPoIB",
  194. .ll_proto = htons ( ARPHRD_INFINIBAND ),
  195. .ll_addr_len = IPOIB_ALEN,
  196. .ll_header_len = IPOIB_HLEN,
  197. .ll_broadcast = ( uint8_t * ) &ipoib_broadcast,
  198. .tx = ipoib_tx,
  199. .rx = ipoib_rx,
  200. .ntoa = ipoib_ntoa,
  201. };
  202. /****************************************************************************
  203. *
  204. * IPoIB network device
  205. *
  206. ****************************************************************************
  207. */
  208. /**
  209. * Destroy queue set
  210. *
  211. * @v ipoib IPoIB device
  212. * @v qset Queue set
  213. */
  214. static void ipoib_destroy_qset ( struct ipoib_device *ipoib,
  215. struct ipoib_queue_set *qset ) {
  216. struct ib_device *ibdev = ipoib->ibdev;
  217. if ( qset->qp )
  218. ib_destroy_qp ( ibdev, qset->qp );
  219. if ( qset->cq )
  220. ib_destroy_cq ( ibdev, qset->cq );
  221. memset ( qset, 0, sizeof ( *qset ) );
  222. }
  223. /**
  224. * Create queue set
  225. *
  226. * @v ipoib IPoIB device
  227. * @v qset Queue set
  228. * @ret rc Return status code
  229. */
  230. static int ipoib_create_qset ( struct ipoib_device *ipoib,
  231. struct ipoib_queue_set *qset,
  232. unsigned int num_cqes,
  233. unsigned int num_send_wqes,
  234. unsigned int num_recv_wqes,
  235. unsigned long qkey ) {
  236. struct ib_device *ibdev = ipoib->ibdev;
  237. int rc;
  238. /* Store queue parameters */
  239. qset->recv_max_fill = num_recv_wqes;
  240. /* Allocate completion queue */
  241. qset->cq = ib_create_cq ( ibdev, num_cqes );
  242. if ( ! qset->cq ) {
  243. DBGC ( ipoib, "IPoIB %p could not allocate completion queue\n",
  244. ipoib );
  245. rc = -ENOMEM;
  246. goto err;
  247. }
  248. /* Allocate queue pair */
  249. qset->qp = ib_create_qp ( ibdev, num_send_wqes, qset->cq,
  250. num_recv_wqes, qset->cq, qkey );
  251. if ( ! qset->qp ) {
  252. DBGC ( ipoib, "IPoIB %p could not allocate queue pair\n",
  253. ipoib );
  254. rc = -ENOMEM;
  255. goto err;
  256. }
  257. qset->qp->owner_priv = ipoib->netdev;
  258. return 0;
  259. err:
  260. ipoib_destroy_qset ( ipoib, qset );
  261. return rc;
  262. }
  263. /**
  264. * Find path cache entry by GID
  265. *
  266. * @v gid GID
  267. * @ret entry Path cache entry, or NULL
  268. */
  269. static struct ipoib_cached_path *
  270. ipoib_find_cached_path ( struct ib_gid *gid ) {
  271. struct ipoib_cached_path *path;
  272. unsigned int i;
  273. for ( i = 0 ; i < IPOIB_NUM_CACHED_PATHS ; i++ ) {
  274. path = &ipoib_path_cache[i];
  275. if ( memcmp ( &path->gid, gid, sizeof ( *gid ) ) == 0 )
  276. return path;
  277. }
  278. DBG ( "IPoIB %08lx:%08lx:%08lx:%08lx cache miss\n",
  279. htonl ( gid->u.dwords[0] ), htonl ( gid->u.dwords[1] ),
  280. htonl ( gid->u.dwords[2] ), htonl ( gid->u.dwords[3] ) );
  281. return NULL;
  282. }
  283. /**
  284. * Transmit path record request
  285. *
  286. * @v ipoib IPoIB device
  287. * @v gid Destination GID
  288. * @ret rc Return status code
  289. */
  290. static int ipoib_get_path_record ( struct ipoib_device *ipoib,
  291. struct ib_gid *gid ) {
  292. struct ib_device *ibdev = ipoib->ibdev;
  293. struct io_buffer *iobuf;
  294. struct ib_mad_path_record *path_record;
  295. struct ib_address_vector av;
  296. int rc;
  297. /* Allocate I/O buffer */
  298. iobuf = alloc_iob ( sizeof ( *path_record ) );
  299. if ( ! iobuf )
  300. return -ENOMEM;
  301. iob_put ( iobuf, sizeof ( *path_record ) );
  302. path_record = iobuf->data;
  303. memset ( path_record, 0, sizeof ( *path_record ) );
  304. /* Construct path record request */
  305. path_record->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  306. path_record->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  307. path_record->mad_hdr.class_version = 2;
  308. path_record->mad_hdr.method = IB_MGMT_METHOD_GET;
  309. path_record->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  310. path_record->mad_hdr.tid[0] = IPOIB_TID_GET_PATH_REC;
  311. path_record->mad_hdr.tid[1] = ipoib_meta_tid++;
  312. path_record->sa_hdr.comp_mask[1] =
  313. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  314. memcpy ( &path_record->dgid, gid, sizeof ( path_record->dgid ) );
  315. memcpy ( &path_record->sgid, &ibdev->port_gid,
  316. sizeof ( path_record->sgid ) );
  317. /* Construct address vector */
  318. memset ( &av, 0, sizeof ( av ) );
  319. av.dlid = ibdev->sm_lid;
  320. av.dest_qp = IB_SA_QPN;
  321. av.qkey = IB_GLOBAL_QKEY;
  322. /* Post send request */
  323. if ( ( rc = ib_post_send ( ibdev, ipoib->meta.qp, &av,
  324. iobuf ) ) != 0 ) {
  325. DBGC ( ipoib, "IPoIB %p could not send get path record: %s\n",
  326. ipoib, strerror ( rc ) );
  327. free_iob ( iobuf );
  328. return rc;
  329. }
  330. return 0;
  331. }
  332. /**
  333. * Transmit multicast group membership request
  334. *
  335. * @v ipoib IPoIB device
  336. * @v gid Multicast GID
  337. * @v join Join (rather than leave) group
  338. * @ret rc Return status code
  339. */
  340. static int ipoib_mc_member_record ( struct ipoib_device *ipoib,
  341. struct ib_gid *gid, int join ) {
  342. struct ib_device *ibdev = ipoib->ibdev;
  343. struct io_buffer *iobuf;
  344. struct ib_mad_mc_member_record *mc_member_record;
  345. struct ib_address_vector av;
  346. int rc;
  347. /* Allocate I/O buffer */
  348. iobuf = alloc_iob ( sizeof ( *mc_member_record ) );
  349. if ( ! iobuf )
  350. return -ENOMEM;
  351. iob_put ( iobuf, sizeof ( *mc_member_record ) );
  352. mc_member_record = iobuf->data;
  353. memset ( mc_member_record, 0, sizeof ( *mc_member_record ) );
  354. /* Construct path record request */
  355. mc_member_record->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  356. mc_member_record->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  357. mc_member_record->mad_hdr.class_version = 2;
  358. mc_member_record->mad_hdr.method =
  359. ( join ? IB_MGMT_METHOD_SET : IB_MGMT_METHOD_DELETE );
  360. mc_member_record->mad_hdr.attr_id = htons ( IB_SA_ATTR_MC_MEMBER_REC );
  361. mc_member_record->mad_hdr.tid[0] = IPOIB_TID_MC_MEMBER_REC;
  362. mc_member_record->mad_hdr.tid[1] = ipoib_meta_tid++;
  363. mc_member_record->sa_hdr.comp_mask[1] =
  364. htonl ( IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
  365. IB_SA_MCMEMBER_REC_JOIN_STATE );
  366. mc_member_record->scope__join_state = 1;
  367. memcpy ( &mc_member_record->mgid, gid,
  368. sizeof ( mc_member_record->mgid ) );
  369. memcpy ( &mc_member_record->port_gid, &ibdev->port_gid,
  370. sizeof ( mc_member_record->port_gid ) );
  371. /* Construct address vector */
  372. memset ( &av, 0, sizeof ( av ) );
  373. av.dlid = ibdev->sm_lid;
  374. av.dest_qp = IB_SA_QPN;
  375. av.qkey = IB_GLOBAL_QKEY;
  376. /* Post send request */
  377. if ( ( rc = ib_post_send ( ibdev, ipoib->meta.qp, &av,
  378. iobuf ) ) != 0 ) {
  379. DBGC ( ipoib, "IPoIB %p could not send get path record: %s\n",
  380. ipoib, strerror ( rc ) );
  381. free_iob ( iobuf );
  382. return rc;
  383. }
  384. return 0;
  385. }
  386. /**
  387. * Transmit packet via IPoIB network device
  388. *
  389. * @v netdev Network device
  390. * @v iobuf I/O buffer
  391. * @ret rc Return status code
  392. */
  393. static int ipoib_transmit ( struct net_device *netdev,
  394. struct io_buffer *iobuf ) {
  395. struct ipoib_device *ipoib = netdev->priv;
  396. struct ib_device *ibdev = ipoib->ibdev;
  397. struct ipoib_pseudo_hdr *ipoib_pshdr = iobuf->data;
  398. struct ib_address_vector av;
  399. struct ib_gid *gid;
  400. struct ipoib_cached_path *path;
  401. int rc;
  402. /* Sanity check */
  403. if ( iob_len ( iobuf ) < sizeof ( *ipoib_pshdr ) ) {
  404. DBGC ( ipoib, "IPoIB %p buffer too short\n", ipoib );
  405. return -EINVAL;
  406. }
  407. iob_pull ( iobuf, ( sizeof ( *ipoib_pshdr ) ) );
  408. /* Construct address vector */
  409. memset ( &av, 0, sizeof ( av ) );
  410. av.qkey = IB_GLOBAL_QKEY;
  411. av.gid_present = 1;
  412. if ( ipoib_pshdr->peer.qpn == htonl ( IPOIB_BROADCAST_QPN ) ) {
  413. /* Broadcast address */
  414. av.dest_qp = IB_BROADCAST_QPN;
  415. av.dlid = ipoib->broadcast_lid;
  416. gid = &ipoib->broadcast_gid;
  417. } else {
  418. /* Unicast - look in path cache */
  419. path = ipoib_find_cached_path ( &ipoib_pshdr->peer.gid );
  420. if ( ! path ) {
  421. /* No path entry - get path record */
  422. rc = ipoib_get_path_record ( ipoib,
  423. &ipoib_pshdr->peer.gid );
  424. netdev_tx_complete ( netdev, iobuf );
  425. return rc;
  426. }
  427. av.dest_qp = ntohl ( ipoib_pshdr->peer.qpn );
  428. av.dlid = path->dlid;
  429. av.rate = path->rate;
  430. av.sl = path->sl;
  431. gid = &ipoib_pshdr->peer.gid;
  432. }
  433. memcpy ( &av.gid, gid, sizeof ( av.gid ) );
  434. return ib_post_send ( ibdev, ipoib->data.qp, &av, iobuf );
  435. }
  436. /**
  437. * Handle IPoIB data send completion
  438. *
  439. * @v ibdev Infiniband device
  440. * @v qp Queue pair
  441. * @v completion Completion
  442. * @v iobuf I/O buffer
  443. */
  444. static void ipoib_data_complete_send ( struct ib_device *ibdev __unused,
  445. struct ib_queue_pair *qp,
  446. struct ib_completion *completion,
  447. struct io_buffer *iobuf ) {
  448. struct net_device *netdev = qp->owner_priv;
  449. netdev_tx_complete_err ( netdev, iobuf,
  450. ( completion->syndrome ? -EIO : 0 ) );
  451. }
  452. /**
  453. * Handle IPoIB data receive completion
  454. *
  455. * @v ibdev Infiniband device
  456. * @v qp Queue pair
  457. * @v completion Completion
  458. * @v iobuf I/O buffer
  459. */
  460. static void ipoib_data_complete_recv ( struct ib_device *ibdev __unused,
  461. struct ib_queue_pair *qp,
  462. struct ib_completion *completion,
  463. struct io_buffer *iobuf ) {
  464. struct net_device *netdev = qp->owner_priv;
  465. struct ipoib_device *ipoib = netdev->priv;
  466. struct ipoib_pseudo_hdr *ipoib_pshdr;
  467. if ( completion->syndrome ) {
  468. netdev_rx_err ( netdev, iobuf, -EIO );
  469. goto done;
  470. }
  471. iob_put ( iobuf, completion->len );
  472. if ( iob_len ( iobuf ) < sizeof ( struct ib_global_route_header ) ) {
  473. DBGC ( ipoib, "IPoIB %p received data packet too short to "
  474. "contain GRH\n", ipoib );
  475. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  476. netdev_rx_err ( netdev, iobuf, -EIO );
  477. goto done;
  478. }
  479. iob_pull ( iobuf, sizeof ( struct ib_global_route_header ) );
  480. if ( iob_len ( iobuf ) < sizeof ( struct ipoib_real_hdr ) ) {
  481. DBGC ( ipoib, "IPoIB %p received data packet too short to "
  482. "contain IPoIB header\n", ipoib );
  483. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  484. netdev_rx_err ( netdev, iobuf, -EIO );
  485. goto done;
  486. }
  487. ipoib_pshdr = iob_push ( iobuf, sizeof ( *ipoib_pshdr ) );
  488. /* FIXME: fill in a MAC address for the sake of AoE! */
  489. netdev_rx ( netdev, iobuf );
  490. done:
  491. ipoib->data.recv_fill--;
  492. }
  493. /**
  494. * Handle IPoIB metadata send completion
  495. *
  496. * @v ibdev Infiniband device
  497. * @v qp Queue pair
  498. * @v completion Completion
  499. * @v iobuf I/O buffer
  500. */
  501. static void ipoib_meta_complete_send ( struct ib_device *ibdev __unused,
  502. struct ib_queue_pair *qp,
  503. struct ib_completion *completion,
  504. struct io_buffer *iobuf ) {
  505. struct net_device *netdev = qp->owner_priv;
  506. struct ipoib_device *ipoib = netdev->priv;
  507. if ( completion->syndrome ) {
  508. DBGC ( ipoib, "IPoIB %p metadata TX completion error %x\n",
  509. ipoib, completion->syndrome );
  510. }
  511. free_iob ( iobuf );
  512. }
  513. /**
  514. * Handle received IPoIB path record
  515. *
  516. * @v ipoib IPoIB device
  517. * @v path_record Path record
  518. */
  519. static void ipoib_recv_path_record ( struct ipoib_device *ipoib __unused,
  520. struct ib_mad_path_record *path_record ) {
  521. struct ipoib_cached_path *path;
  522. /* Update path cache entry */
  523. path = &ipoib_path_cache[ipoib_path_cache_idx];
  524. memcpy ( &path->gid, &path_record->dgid, sizeof ( path->gid ) );
  525. path->dlid = ntohs ( path_record->dlid );
  526. path->sl = ( path_record->reserved__sl & 0x0f );
  527. path->rate = ( path_record->rate_selector__rate & 0x3f );
  528. DBG ( "IPoIB %08lx:%08lx:%08lx:%08lx dlid %x sl %x rate %x\n",
  529. htonl ( path->gid.u.dwords[0] ), htonl ( path->gid.u.dwords[1] ),
  530. htonl ( path->gid.u.dwords[2] ), htonl ( path->gid.u.dwords[3] ),
  531. path->dlid, path->sl, path->rate );
  532. /* Update path cache index */
  533. ipoib_path_cache_idx++;
  534. if ( ipoib_path_cache_idx == IPOIB_NUM_CACHED_PATHS )
  535. ipoib_path_cache_idx = 0;
  536. }
  537. /**
  538. * Handle received IPoIB multicast membership record
  539. *
  540. * @v ipoib IPoIB device
  541. * @v mc_member_record Multicast membership record
  542. */
  543. static void ipoib_recv_mc_member_record ( struct ipoib_device *ipoib,
  544. struct ib_mad_mc_member_record *mc_member_record ) {
  545. /* Record parameters */
  546. ipoib->broadcast_joined =
  547. ( mc_member_record->scope__join_state & 0x0f );
  548. ipoib->data_qkey = ntohl ( mc_member_record->qkey );
  549. ipoib->broadcast_lid = ntohs ( mc_member_record->mlid );
  550. DBGC ( ipoib, "IPoIB %p %s broadcast group: qkey %lx mlid %x\n",
  551. ipoib, ( ipoib->broadcast_joined ? "joined" : "left" ),
  552. ipoib->data_qkey, ipoib->broadcast_lid );
  553. }
  554. /**
  555. * Handle IPoIB metadata receive completion
  556. *
  557. * @v ibdev Infiniband device
  558. * @v qp Queue pair
  559. * @v completion Completion
  560. * @v iobuf I/O buffer
  561. */
  562. static void ipoib_meta_complete_recv ( struct ib_device *ibdev __unused,
  563. struct ib_queue_pair *qp,
  564. struct ib_completion *completion,
  565. struct io_buffer *iobuf ) {
  566. struct net_device *netdev = qp->owner_priv;
  567. struct ipoib_device *ipoib = netdev->priv;
  568. union ib_mad *mad;
  569. if ( completion->syndrome ) {
  570. DBGC ( ipoib, "IPoIB %p metadata RX completion error %x\n",
  571. ipoib, completion->syndrome );
  572. goto done;
  573. }
  574. iob_put ( iobuf, completion->len );
  575. if ( iob_len ( iobuf ) < sizeof ( struct ib_global_route_header ) ) {
  576. DBGC ( ipoib, "IPoIB %p received metadata packet too short "
  577. "to contain GRH\n", ipoib );
  578. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  579. goto done;
  580. }
  581. iob_pull ( iobuf, sizeof ( struct ib_global_route_header ) );
  582. if ( iob_len ( iobuf ) < sizeof ( *mad ) ) {
  583. DBGC ( ipoib, "IPoIB %p received metadata packet too short "
  584. "to contain reply\n", ipoib );
  585. DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
  586. goto done;
  587. }
  588. mad = iobuf->data;
  589. if ( mad->mad_hdr.status != 0 ) {
  590. DBGC ( ipoib, "IPoIB %p metadata RX err status %04x\n",
  591. ipoib, ntohs ( mad->mad_hdr.status ) );
  592. goto done;
  593. }
  594. switch ( mad->mad_hdr.tid[0] ) {
  595. case IPOIB_TID_GET_PATH_REC:
  596. ipoib_recv_path_record ( ipoib, &mad->path_record );
  597. break;
  598. case IPOIB_TID_MC_MEMBER_REC:
  599. ipoib_recv_mc_member_record ( ipoib, &mad->mc_member_record );
  600. break;
  601. default:
  602. DBGC ( ipoib, "IPoIB %p unwanted response:\n",
  603. ipoib );
  604. DBGC_HD ( ipoib, mad, sizeof ( *mad ) );
  605. break;
  606. }
  607. done:
  608. ipoib->meta.recv_fill--;
  609. free_iob ( iobuf );
  610. }
  611. /**
  612. * Refill IPoIB receive ring
  613. *
  614. * @v ipoib IPoIB device
  615. */
  616. static void ipoib_refill_recv ( struct ipoib_device *ipoib,
  617. struct ipoib_queue_set *qset ) {
  618. struct ib_device *ibdev = ipoib->ibdev;
  619. struct io_buffer *iobuf;
  620. int rc;
  621. while ( qset->recv_fill < qset->recv_max_fill ) {
  622. iobuf = alloc_iob ( IPOIB_MTU );
  623. if ( ! iobuf )
  624. break;
  625. if ( ( rc = ib_post_recv ( ibdev, qset->qp, iobuf ) ) != 0 ) {
  626. free_iob ( iobuf );
  627. break;
  628. }
  629. qset->recv_fill++;
  630. }
  631. }
  632. /**
  633. * Poll IPoIB network device
  634. *
  635. * @v netdev Network device
  636. */
  637. static void ipoib_poll ( struct net_device *netdev ) {
  638. struct ipoib_device *ipoib = netdev->priv;
  639. struct ib_device *ibdev = ipoib->ibdev;
  640. ib_poll_cq ( ibdev, ipoib->meta.cq, ipoib_meta_complete_send,
  641. ipoib_meta_complete_recv );
  642. ib_poll_cq ( ibdev, ipoib->data.cq, ipoib_data_complete_send,
  643. ipoib_data_complete_recv );
  644. ipoib_refill_recv ( ipoib, &ipoib->meta );
  645. ipoib_refill_recv ( ipoib, &ipoib->data );
  646. }
  647. /**
  648. * Enable/disable interrupts on IPoIB network device
  649. *
  650. * @v netdev Network device
  651. * @v enable Interrupts should be enabled
  652. */
  653. static void ipoib_irq ( struct net_device *netdev __unused,
  654. int enable __unused ) {
  655. /* No implementation */
  656. }
  657. /**
  658. * Open IPoIB network device
  659. *
  660. * @v netdev Network device
  661. * @ret rc Return status code
  662. */
  663. static int ipoib_open ( struct net_device *netdev ) {
  664. struct ipoib_device *ipoib = netdev->priv;
  665. struct ib_device *ibdev = ipoib->ibdev;
  666. int rc;
  667. /* Attach to broadcast multicast GID */
  668. if ( ( rc = ib_mcast_attach ( ibdev, ipoib->data.qp,
  669. &ipoib->broadcast_gid ) ) != 0 ) {
  670. DBG ( "Could not attach to broadcast GID: %s\n",
  671. strerror ( rc ) );
  672. return rc;
  673. }
  674. /* Fill receive rings */
  675. ipoib_refill_recv ( ipoib, &ipoib->meta );
  676. ipoib_refill_recv ( ipoib, &ipoib->data );
  677. return 0;
  678. }
  679. /**
  680. * Close IPoIB network device
  681. *
  682. * @v netdev Network device
  683. */
  684. static void ipoib_close ( struct net_device *netdev ) {
  685. struct ipoib_device *ipoib = netdev->priv;
  686. struct ib_device *ibdev = ipoib->ibdev;
  687. /* Detach from broadcast multicast GID */
  688. ib_mcast_detach ( ibdev, ipoib->data.qp, &ipoib->broadcast_gid );
  689. /* FIXME: should probably flush the receive ring */
  690. }
  691. /** IPoIB network device operations */
  692. static struct net_device_operations ipoib_operations = {
  693. .open = ipoib_open,
  694. .close = ipoib_close,
  695. .transmit = ipoib_transmit,
  696. .poll = ipoib_poll,
  697. .irq = ipoib_irq,
  698. };
  699. /**
  700. * Join IPoIB broadcast group
  701. *
  702. * @v ipoib IPoIB device
  703. * @ret rc Return status code
  704. */
  705. static int ipoib_join_broadcast_group ( struct ipoib_device *ipoib ) {
  706. struct ib_device *ibdev = ipoib->ibdev;
  707. unsigned int delay_ms;
  708. int rc;
  709. /* Make sure we have some receive descriptors */
  710. ipoib_refill_recv ( ipoib, &ipoib->meta );
  711. /* Send join request */
  712. if ( ( rc = ipoib_mc_member_record ( ipoib, &ipoib->broadcast_gid,
  713. 1 ) ) != 0 ) {
  714. DBGC ( ipoib, "IPoIB %p could not send broadcast join: %s\n",
  715. ipoib, strerror ( rc ) );
  716. return rc;
  717. }
  718. /* Wait for join to complete. Ideally we wouldn't delay for
  719. * this long, but we need the queue key before we can set up
  720. * the data queue pair, which we need before we can know the
  721. * MAC address.
  722. */
  723. for ( delay_ms = IPOIB_JOIN_MAX_DELAY_MS ; delay_ms ; delay_ms-- ) {
  724. mdelay ( 1 );
  725. ib_poll_cq ( ibdev, ipoib->meta.cq, ipoib_meta_complete_send,
  726. ipoib_meta_complete_recv );
  727. ipoib_refill_recv ( ipoib, &ipoib->meta );
  728. if ( ipoib->broadcast_joined )
  729. return 0;
  730. }
  731. DBGC ( ipoib, "IPoIB %p timed out waiting for broadcast join\n",
  732. ipoib );
  733. return -ETIMEDOUT;
  734. }
  735. /**
  736. * Probe IPoIB device
  737. *
  738. * @v ibdev Infiniband device
  739. * @ret rc Return status code
  740. */
  741. int ipoib_probe ( struct ib_device *ibdev ) {
  742. struct net_device *netdev;
  743. struct ipoib_device *ipoib;
  744. struct ipoib_mac *mac;
  745. int rc;
  746. /* Allocate network device */
  747. netdev = alloc_ipoibdev ( sizeof ( *ipoib ) );
  748. if ( ! netdev )
  749. return -ENOMEM;
  750. netdev_init ( netdev, &ipoib_operations );
  751. ipoib = netdev->priv;
  752. ib_set_ownerdata ( ibdev, netdev );
  753. netdev->dev = ibdev->dev;
  754. memset ( ipoib, 0, sizeof ( *ipoib ) );
  755. ipoib->netdev = netdev;
  756. ipoib->ibdev = ibdev;
  757. /* Calculate broadcast GID */
  758. memcpy ( &ipoib->broadcast_gid, &ipv4_broadcast_gid,
  759. sizeof ( ipoib->broadcast_gid ) );
  760. ipoib->broadcast_gid.u.words[2] = htons ( ibdev->pkey );
  761. /* Allocate metadata queue set */
  762. if ( ( rc = ipoib_create_qset ( ipoib, &ipoib->meta,
  763. IPOIB_META_NUM_CQES,
  764. IPOIB_META_NUM_SEND_WQES,
  765. IPOIB_META_NUM_RECV_WQES,
  766. IB_GLOBAL_QKEY ) ) != 0 ) {
  767. DBGC ( ipoib, "IPoIB %p could not allocate metadata QP: %s\n",
  768. ipoib, strerror ( rc ) );
  769. goto err_create_meta_qset;
  770. }
  771. /* Join broadcast group */
  772. if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
  773. DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
  774. ipoib, strerror ( rc ) );
  775. goto err_join_broadcast_group;
  776. }
  777. /* Allocate data queue set */
  778. if ( ( rc = ipoib_create_qset ( ipoib, &ipoib->data,
  779. IPOIB_DATA_NUM_CQES,
  780. IPOIB_DATA_NUM_SEND_WQES,
  781. IPOIB_DATA_NUM_RECV_WQES,
  782. ipoib->data_qkey ) ) != 0 ) {
  783. DBGC ( ipoib, "IPoIB %p could not allocate data QP: %s\n",
  784. ipoib, strerror ( rc ) );
  785. goto err_create_data_qset;
  786. }
  787. /* Construct MAC address */
  788. mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
  789. mac->qpn = htonl ( ipoib->data.qp->qpn );
  790. memcpy ( &mac->gid, &ibdev->port_gid, sizeof ( mac->gid ) );
  791. /* Register network device */
  792. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  793. goto err_register_netdev;
  794. return 0;
  795. err_register_netdev:
  796. ipoib_destroy_qset ( ipoib, &ipoib->data );
  797. err_join_broadcast_group:
  798. err_create_data_qset:
  799. ipoib_destroy_qset ( ipoib, &ipoib->meta );
  800. err_create_meta_qset:
  801. netdev_nullify ( netdev );
  802. netdev_put ( netdev );
  803. return rc;
  804. }
  805. /**
  806. * Remove IPoIB device
  807. *
  808. * @v ibdev Infiniband device
  809. */
  810. void ipoib_remove ( struct ib_device *ibdev ) {
  811. struct net_device *netdev = ib_get_ownerdata ( ibdev );
  812. struct ipoib_device *ipoib = netdev->priv;
  813. unregister_netdev ( netdev );
  814. ipoib_destroy_qset ( ipoib, &ipoib->data );
  815. ipoib_destroy_qset ( ipoib, &ipoib->meta );
  816. netdev_nullify ( netdev );
  817. netdev_put ( netdev );
  818. }