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.

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