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.

infiniband.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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 <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <byteswap.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <ipxe/list.h>
  28. #include <ipxe/errortab.h>
  29. #include <ipxe/if_arp.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/iobuf.h>
  32. #include <ipxe/ipoib.h>
  33. #include <ipxe/process.h>
  34. #include <ipxe/infiniband.h>
  35. #include <ipxe/ib_mi.h>
  36. #include <ipxe/ib_sma.h>
  37. /** @file
  38. *
  39. * Infiniband protocol
  40. *
  41. */
  42. /** List of Infiniband devices */
  43. struct list_head ib_devices = LIST_HEAD_INIT ( ib_devices );
  44. /** List of open Infiniband devices, in reverse order of opening */
  45. static struct list_head open_ib_devices = LIST_HEAD_INIT ( open_ib_devices );
  46. /* Disambiguate the various possible EINPROGRESSes */
  47. #define EINPROGRESS_INIT __einfo_error ( EINFO_EINPROGRESS_INIT )
  48. #define EINFO_EINPROGRESS_INIT __einfo_uniqify \
  49. ( EINFO_EINPROGRESS, 0x01, "Initialising" )
  50. #define EINPROGRESS_ARMED __einfo_error ( EINFO_EINPROGRESS_ARMED )
  51. #define EINFO_EINPROGRESS_ARMED __einfo_uniqify \
  52. ( EINFO_EINPROGRESS, 0x02, "Armed" )
  53. /** Human-readable message for the link statuses */
  54. struct errortab infiniband_errors[] __errortab = {
  55. __einfo_errortab ( EINFO_EINPROGRESS_INIT ),
  56. __einfo_errortab ( EINFO_EINPROGRESS_ARMED ),
  57. };
  58. /***************************************************************************
  59. *
  60. * Completion queues
  61. *
  62. ***************************************************************************
  63. */
  64. /**
  65. * Create completion queue
  66. *
  67. * @v ibdev Infiniband device
  68. * @v num_cqes Number of completion queue entries
  69. * @v op Completion queue operations
  70. * @ret cq New completion queue
  71. */
  72. struct ib_completion_queue *
  73. ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
  74. struct ib_completion_queue_operations *op ) {
  75. struct ib_completion_queue *cq;
  76. int rc;
  77. DBGC ( ibdev, "IBDEV %p creating completion queue\n", ibdev );
  78. /* Allocate and initialise data structure */
  79. cq = zalloc ( sizeof ( *cq ) );
  80. if ( ! cq )
  81. goto err_alloc_cq;
  82. cq->ibdev = ibdev;
  83. list_add ( &cq->list, &ibdev->cqs );
  84. cq->num_cqes = num_cqes;
  85. INIT_LIST_HEAD ( &cq->work_queues );
  86. cq->op = op;
  87. /* Perform device-specific initialisation and get CQN */
  88. if ( ( rc = ibdev->op->create_cq ( ibdev, cq ) ) != 0 ) {
  89. DBGC ( ibdev, "IBDEV %p could not initialise completion "
  90. "queue: %s\n", ibdev, strerror ( rc ) );
  91. goto err_dev_create_cq;
  92. }
  93. DBGC ( ibdev, "IBDEV %p created %d-entry completion queue %p (%p) "
  94. "with CQN %#lx\n", ibdev, num_cqes, cq,
  95. ib_cq_get_drvdata ( cq ), cq->cqn );
  96. return cq;
  97. ibdev->op->destroy_cq ( ibdev, cq );
  98. err_dev_create_cq:
  99. list_del ( &cq->list );
  100. free ( cq );
  101. err_alloc_cq:
  102. return NULL;
  103. }
  104. /**
  105. * Destroy completion queue
  106. *
  107. * @v ibdev Infiniband device
  108. * @v cq Completion queue
  109. */
  110. void ib_destroy_cq ( struct ib_device *ibdev,
  111. struct ib_completion_queue *cq ) {
  112. DBGC ( ibdev, "IBDEV %p destroying completion queue %#lx\n",
  113. ibdev, cq->cqn );
  114. assert ( list_empty ( &cq->work_queues ) );
  115. ibdev->op->destroy_cq ( ibdev, cq );
  116. list_del ( &cq->list );
  117. free ( cq );
  118. }
  119. /**
  120. * Poll completion queue
  121. *
  122. * @v ibdev Infiniband device
  123. * @v cq Completion queue
  124. */
  125. void ib_poll_cq ( struct ib_device *ibdev,
  126. struct ib_completion_queue *cq ) {
  127. struct ib_work_queue *wq;
  128. /* Poll completion queue */
  129. ibdev->op->poll_cq ( ibdev, cq );
  130. /* Refill receive work queues */
  131. list_for_each_entry ( wq, &cq->work_queues, list ) {
  132. if ( ! wq->is_send )
  133. ib_refill_recv ( ibdev, wq->qp );
  134. }
  135. }
  136. /***************************************************************************
  137. *
  138. * Work queues
  139. *
  140. ***************************************************************************
  141. */
  142. /**
  143. * Create queue pair
  144. *
  145. * @v ibdev Infiniband device
  146. * @v type Queue pair type
  147. * @v num_send_wqes Number of send work queue entries
  148. * @v send_cq Send completion queue
  149. * @v num_recv_wqes Number of receive work queue entries
  150. * @v recv_cq Receive completion queue
  151. * @ret qp Queue pair
  152. *
  153. * The queue pair will be left in the INIT state; you must call
  154. * ib_modify_qp() before it is ready to use for sending and receiving.
  155. */
  156. struct ib_queue_pair * ib_create_qp ( struct ib_device *ibdev,
  157. enum ib_queue_pair_type type,
  158. unsigned int num_send_wqes,
  159. struct ib_completion_queue *send_cq,
  160. unsigned int num_recv_wqes,
  161. struct ib_completion_queue *recv_cq ) {
  162. struct ib_queue_pair *qp;
  163. size_t total_size;
  164. int rc;
  165. DBGC ( ibdev, "IBDEV %p creating queue pair\n", ibdev );
  166. /* Allocate and initialise data structure */
  167. total_size = ( sizeof ( *qp ) +
  168. ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ) +
  169. ( num_recv_wqes * sizeof ( qp->recv.iobufs[0] ) ) );
  170. qp = zalloc ( total_size );
  171. if ( ! qp )
  172. goto err_alloc_qp;
  173. qp->ibdev = ibdev;
  174. list_add ( &qp->list, &ibdev->qps );
  175. qp->type = type;
  176. qp->send.qp = qp;
  177. qp->send.is_send = 1;
  178. qp->send.cq = send_cq;
  179. list_add ( &qp->send.list, &send_cq->work_queues );
  180. qp->send.psn = ( random() & 0xffffffUL );
  181. qp->send.num_wqes = num_send_wqes;
  182. qp->send.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) );
  183. qp->recv.qp = qp;
  184. qp->recv.cq = recv_cq;
  185. list_add ( &qp->recv.list, &recv_cq->work_queues );
  186. qp->recv.psn = ( random() & 0xffffffUL );
  187. qp->recv.num_wqes = num_recv_wqes;
  188. qp->recv.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) +
  189. ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ));
  190. INIT_LIST_HEAD ( &qp->mgids );
  191. /* Perform device-specific initialisation and get QPN */
  192. if ( ( rc = ibdev->op->create_qp ( ibdev, qp ) ) != 0 ) {
  193. DBGC ( ibdev, "IBDEV %p could not initialise queue pair: "
  194. "%s\n", ibdev, strerror ( rc ) );
  195. goto err_dev_create_qp;
  196. }
  197. DBGC ( ibdev, "IBDEV %p created queue pair %p (%p) with QPN %#lx\n",
  198. ibdev, qp, ib_qp_get_drvdata ( qp ), qp->qpn );
  199. DBGC ( ibdev, "IBDEV %p QPN %#lx has %d send entries at [%p,%p)\n",
  200. ibdev, qp->qpn, num_send_wqes, qp->send.iobufs,
  201. qp->recv.iobufs );
  202. DBGC ( ibdev, "IBDEV %p QPN %#lx has %d receive entries at [%p,%p)\n",
  203. ibdev, qp->qpn, num_recv_wqes, qp->recv.iobufs,
  204. ( ( ( void * ) qp ) + total_size ) );
  205. /* Calculate externally-visible QPN */
  206. switch ( type ) {
  207. case IB_QPT_SMI:
  208. qp->ext_qpn = IB_QPN_SMI;
  209. break;
  210. case IB_QPT_GSI:
  211. qp->ext_qpn = IB_QPN_GSI;
  212. break;
  213. default:
  214. qp->ext_qpn = qp->qpn;
  215. break;
  216. }
  217. if ( qp->ext_qpn != qp->qpn ) {
  218. DBGC ( ibdev, "IBDEV %p QPN %#lx has external QPN %#lx\n",
  219. ibdev, qp->qpn, qp->ext_qpn );
  220. }
  221. return qp;
  222. ibdev->op->destroy_qp ( ibdev, qp );
  223. err_dev_create_qp:
  224. list_del ( &qp->send.list );
  225. list_del ( &qp->recv.list );
  226. list_del ( &qp->list );
  227. free ( qp );
  228. err_alloc_qp:
  229. return NULL;
  230. }
  231. /**
  232. * Modify queue pair
  233. *
  234. * @v ibdev Infiniband device
  235. * @v qp Queue pair
  236. * @v av New address vector, if applicable
  237. * @ret rc Return status code
  238. */
  239. int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  240. int rc;
  241. DBGC ( ibdev, "IBDEV %p modifying QPN %#lx\n", ibdev, qp->qpn );
  242. if ( ( rc = ibdev->op->modify_qp ( ibdev, qp ) ) != 0 ) {
  243. DBGC ( ibdev, "IBDEV %p could not modify QPN %#lx: %s\n",
  244. ibdev, qp->qpn, strerror ( rc ) );
  245. return rc;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * Destroy queue pair
  251. *
  252. * @v ibdev Infiniband device
  253. * @v qp Queue pair
  254. */
  255. void ib_destroy_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  256. struct io_buffer *iobuf;
  257. unsigned int i;
  258. DBGC ( ibdev, "IBDEV %p destroying QPN %#lx\n",
  259. ibdev, qp->qpn );
  260. assert ( list_empty ( &qp->mgids ) );
  261. /* Perform device-specific destruction */
  262. ibdev->op->destroy_qp ( ibdev, qp );
  263. /* Complete any remaining I/O buffers with errors */
  264. for ( i = 0 ; i < qp->send.num_wqes ; i++ ) {
  265. if ( ( iobuf = qp->send.iobufs[i] ) != NULL )
  266. ib_complete_send ( ibdev, qp, iobuf, -ECANCELED );
  267. }
  268. for ( i = 0 ; i < qp->recv.num_wqes ; i++ ) {
  269. if ( ( iobuf = qp->recv.iobufs[i] ) != NULL ) {
  270. ib_complete_recv ( ibdev, qp, NULL, iobuf,
  271. -ECANCELED );
  272. }
  273. }
  274. /* Remove work queues from completion queue */
  275. list_del ( &qp->send.list );
  276. list_del ( &qp->recv.list );
  277. /* Free QP */
  278. list_del ( &qp->list );
  279. free ( qp );
  280. }
  281. /**
  282. * Find queue pair by QPN
  283. *
  284. * @v ibdev Infiniband device
  285. * @v qpn Queue pair number
  286. * @ret qp Queue pair, or NULL
  287. */
  288. struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
  289. unsigned long qpn ) {
  290. struct ib_queue_pair *qp;
  291. list_for_each_entry ( qp, &ibdev->qps, list ) {
  292. if ( ( qpn == qp->qpn ) || ( qpn == qp->ext_qpn ) )
  293. return qp;
  294. }
  295. return NULL;
  296. }
  297. /**
  298. * Find queue pair by multicast GID
  299. *
  300. * @v ibdev Infiniband device
  301. * @v gid Multicast GID
  302. * @ret qp Queue pair, or NULL
  303. */
  304. struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
  305. struct ib_gid *gid ) {
  306. struct ib_queue_pair *qp;
  307. struct ib_multicast_gid *mgid;
  308. list_for_each_entry ( qp, &ibdev->qps, list ) {
  309. list_for_each_entry ( mgid, &qp->mgids, list ) {
  310. if ( memcmp ( &mgid->gid, gid,
  311. sizeof ( mgid->gid ) ) == 0 ) {
  312. return qp;
  313. }
  314. }
  315. }
  316. return NULL;
  317. }
  318. /**
  319. * Find work queue belonging to completion queue
  320. *
  321. * @v cq Completion queue
  322. * @v qpn Queue pair number
  323. * @v is_send Find send work queue (rather than receive)
  324. * @ret wq Work queue, or NULL if not found
  325. */
  326. struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
  327. unsigned long qpn, int is_send ) {
  328. struct ib_work_queue *wq;
  329. list_for_each_entry ( wq, &cq->work_queues, list ) {
  330. if ( ( wq->qp->qpn == qpn ) && ( wq->is_send == is_send ) )
  331. return wq;
  332. }
  333. return NULL;
  334. }
  335. /**
  336. * Post send work queue entry
  337. *
  338. * @v ibdev Infiniband device
  339. * @v qp Queue pair
  340. * @v av Address vector
  341. * @v iobuf I/O buffer
  342. * @ret rc Return status code
  343. */
  344. int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  345. struct ib_address_vector *av,
  346. struct io_buffer *iobuf ) {
  347. struct ib_address_vector av_copy;
  348. int rc;
  349. /* Check queue fill level */
  350. if ( qp->send.fill >= qp->send.num_wqes ) {
  351. DBGC ( ibdev, "IBDEV %p QPN %#lx send queue full\n",
  352. ibdev, qp->qpn );
  353. return -ENOBUFS;
  354. }
  355. /* Use default address vector if none specified */
  356. if ( ! av )
  357. av = &qp->av;
  358. /* Make modifiable copy of address vector */
  359. memcpy ( &av_copy, av, sizeof ( av_copy ) );
  360. av = &av_copy;
  361. /* Fill in optional parameters in address vector */
  362. if ( ! av->qkey )
  363. av->qkey = qp->qkey;
  364. if ( ! av->rate )
  365. av->rate = IB_RATE_2_5;
  366. /* Post to hardware */
  367. if ( ( rc = ibdev->op->post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
  368. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post send WQE: "
  369. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  370. return rc;
  371. }
  372. qp->send.fill++;
  373. return 0;
  374. }
  375. /**
  376. * Post receive work queue entry
  377. *
  378. * @v ibdev Infiniband device
  379. * @v qp Queue pair
  380. * @v iobuf I/O buffer
  381. * @ret rc Return status code
  382. */
  383. int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  384. struct io_buffer *iobuf ) {
  385. int rc;
  386. /* Check packet length */
  387. if ( iob_tailroom ( iobuf ) < IB_MAX_PAYLOAD_SIZE ) {
  388. DBGC ( ibdev, "IBDEV %p QPN %#lx wrong RX buffer size (%zd)\n",
  389. ibdev, qp->qpn, iob_tailroom ( iobuf ) );
  390. return -EINVAL;
  391. }
  392. /* Check queue fill level */
  393. if ( qp->recv.fill >= qp->recv.num_wqes ) {
  394. DBGC ( ibdev, "IBDEV %p QPN %#lx receive queue full\n",
  395. ibdev, qp->qpn );
  396. return -ENOBUFS;
  397. }
  398. /* Post to hardware */
  399. if ( ( rc = ibdev->op->post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  400. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post receive WQE: "
  401. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  402. return rc;
  403. }
  404. qp->recv.fill++;
  405. return 0;
  406. }
  407. /**
  408. * Complete send work queue entry
  409. *
  410. * @v ibdev Infiniband device
  411. * @v qp Queue pair
  412. * @v iobuf I/O buffer
  413. * @v rc Completion status code
  414. */
  415. void ib_complete_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  416. struct io_buffer *iobuf, int rc ) {
  417. if ( qp->send.cq->op->complete_send ) {
  418. qp->send.cq->op->complete_send ( ibdev, qp, iobuf, rc );
  419. } else {
  420. free_iob ( iobuf );
  421. }
  422. qp->send.fill--;
  423. }
  424. /**
  425. * Complete receive work queue entry
  426. *
  427. * @v ibdev Infiniband device
  428. * @v qp Queue pair
  429. * @v av Address vector
  430. * @v iobuf I/O buffer
  431. * @v rc Completion status code
  432. */
  433. void ib_complete_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  434. struct ib_address_vector *av,
  435. struct io_buffer *iobuf, int rc ) {
  436. if ( qp->recv.cq->op->complete_recv ) {
  437. qp->recv.cq->op->complete_recv ( ibdev, qp, av, iobuf, rc );
  438. } else {
  439. free_iob ( iobuf );
  440. }
  441. qp->recv.fill--;
  442. }
  443. /**
  444. * Refill receive work queue
  445. *
  446. * @v ibdev Infiniband device
  447. * @v qp Queue pair
  448. */
  449. void ib_refill_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  450. struct io_buffer *iobuf;
  451. int rc;
  452. /* Keep filling while unfilled entries remain */
  453. while ( qp->recv.fill < qp->recv.num_wqes ) {
  454. /* Allocate I/O buffer */
  455. iobuf = alloc_iob ( IB_MAX_PAYLOAD_SIZE );
  456. if ( ! iobuf ) {
  457. /* Non-fatal; we will refill on next attempt */
  458. return;
  459. }
  460. /* Post I/O buffer */
  461. if ( ( rc = ib_post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  462. DBGC ( ibdev, "IBDEV %p could not refill: %s\n",
  463. ibdev, strerror ( rc ) );
  464. free_iob ( iobuf );
  465. /* Give up */
  466. return;
  467. }
  468. }
  469. }
  470. /***************************************************************************
  471. *
  472. * Link control
  473. *
  474. ***************************************************************************
  475. */
  476. /**
  477. * Open port
  478. *
  479. * @v ibdev Infiniband device
  480. * @ret rc Return status code
  481. */
  482. int ib_open ( struct ib_device *ibdev ) {
  483. int rc;
  484. /* Increment device open request counter */
  485. if ( ibdev->open_count++ > 0 ) {
  486. /* Device was already open; do nothing */
  487. return 0;
  488. }
  489. /* Create subnet management interface */
  490. ibdev->smi = ib_create_mi ( ibdev, IB_QPT_SMI );
  491. if ( ! ibdev->smi ) {
  492. DBGC ( ibdev, "IBDEV %p could not create SMI\n", ibdev );
  493. rc = -ENOMEM;
  494. goto err_create_smi;
  495. }
  496. /* Create subnet management agent */
  497. if ( ( rc = ib_create_sma ( ibdev, ibdev->smi ) ) != 0 ) {
  498. DBGC ( ibdev, "IBDEV %p could not create SMA: %s\n",
  499. ibdev, strerror ( rc ) );
  500. goto err_create_sma;
  501. }
  502. /* Create general services interface */
  503. ibdev->gsi = ib_create_mi ( ibdev, IB_QPT_GSI );
  504. if ( ! ibdev->gsi ) {
  505. DBGC ( ibdev, "IBDEV %p could not create GSI\n", ibdev );
  506. rc = -ENOMEM;
  507. goto err_create_gsi;
  508. }
  509. /* Open device */
  510. if ( ( rc = ibdev->op->open ( ibdev ) ) != 0 ) {
  511. DBGC ( ibdev, "IBDEV %p could not open: %s\n",
  512. ibdev, strerror ( rc ) );
  513. goto err_open;
  514. }
  515. /* Add to head of open devices list */
  516. list_add ( &ibdev->open_list, &open_ib_devices );
  517. assert ( ibdev->open_count == 1 );
  518. return 0;
  519. ibdev->op->close ( ibdev );
  520. err_open:
  521. ib_destroy_mi ( ibdev, ibdev->gsi );
  522. err_create_gsi:
  523. ib_destroy_sma ( ibdev, ibdev->smi );
  524. err_create_sma:
  525. ib_destroy_mi ( ibdev, ibdev->smi );
  526. err_create_smi:
  527. assert ( ibdev->open_count == 1 );
  528. ibdev->open_count = 0;
  529. return rc;
  530. }
  531. /**
  532. * Close port
  533. *
  534. * @v ibdev Infiniband device
  535. */
  536. void ib_close ( struct ib_device *ibdev ) {
  537. /* Decrement device open request counter */
  538. ibdev->open_count--;
  539. /* Close device if this was the last remaining requested opening */
  540. if ( ibdev->open_count == 0 ) {
  541. list_del ( &ibdev->open_list );
  542. ib_destroy_mi ( ibdev, ibdev->gsi );
  543. ib_destroy_sma ( ibdev, ibdev->smi );
  544. ib_destroy_mi ( ibdev, ibdev->smi );
  545. ibdev->op->close ( ibdev );
  546. }
  547. }
  548. /**
  549. * Get link state
  550. *
  551. * @v ibdev Infiniband device
  552. * @ret rc Link status code
  553. */
  554. int ib_link_rc ( struct ib_device *ibdev ) {
  555. switch ( ibdev->port_state ) {
  556. case IB_PORT_STATE_DOWN: return -ENOTCONN;
  557. case IB_PORT_STATE_INIT: return -EINPROGRESS_INIT;
  558. case IB_PORT_STATE_ARMED: return -EINPROGRESS_ARMED;
  559. case IB_PORT_STATE_ACTIVE: return 0;
  560. default: return -EINVAL;
  561. }
  562. }
  563. /***************************************************************************
  564. *
  565. * Multicast
  566. *
  567. ***************************************************************************
  568. */
  569. /**
  570. * Attach to multicast group
  571. *
  572. * @v ibdev Infiniband device
  573. * @v qp Queue pair
  574. * @v gid Multicast GID
  575. * @ret rc Return status code
  576. *
  577. * Note that this function handles only the local device's attachment
  578. * to the multicast GID; it does not issue the relevant MADs to join
  579. * the multicast group on the subnet.
  580. */
  581. int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  582. struct ib_gid *gid ) {
  583. struct ib_multicast_gid *mgid;
  584. int rc;
  585. /* Add to software multicast GID list */
  586. mgid = zalloc ( sizeof ( *mgid ) );
  587. if ( ! mgid ) {
  588. rc = -ENOMEM;
  589. goto err_alloc_mgid;
  590. }
  591. memcpy ( &mgid->gid, gid, sizeof ( mgid->gid ) );
  592. list_add ( &mgid->list, &qp->mgids );
  593. /* Add to hardware multicast GID list */
  594. if ( ( rc = ibdev->op->mcast_attach ( ibdev, qp, gid ) ) != 0 )
  595. goto err_dev_mcast_attach;
  596. return 0;
  597. err_dev_mcast_attach:
  598. list_del ( &mgid->list );
  599. free ( mgid );
  600. err_alloc_mgid:
  601. return rc;
  602. }
  603. /**
  604. * Detach from multicast group
  605. *
  606. * @v ibdev Infiniband device
  607. * @v qp Queue pair
  608. * @v gid Multicast GID
  609. */
  610. void ib_mcast_detach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  611. struct ib_gid *gid ) {
  612. struct ib_multicast_gid *mgid;
  613. /* Remove from hardware multicast GID list */
  614. ibdev->op->mcast_detach ( ibdev, qp, gid );
  615. /* Remove from software multicast GID list */
  616. list_for_each_entry ( mgid, &qp->mgids, list ) {
  617. if ( memcmp ( &mgid->gid, gid, sizeof ( mgid->gid ) ) == 0 ) {
  618. list_del ( &mgid->list );
  619. free ( mgid );
  620. break;
  621. }
  622. }
  623. }
  624. /***************************************************************************
  625. *
  626. * Miscellaneous
  627. *
  628. ***************************************************************************
  629. */
  630. /**
  631. * Get Infiniband HCA information
  632. *
  633. * @v ibdev Infiniband device
  634. * @ret hca_guid HCA GUID
  635. * @ret num_ports Number of ports
  636. */
  637. int ib_get_hca_info ( struct ib_device *ibdev,
  638. struct ib_gid_half *hca_guid ) {
  639. struct ib_device *tmp;
  640. int num_ports = 0;
  641. /* Search for IB devices with the same physical device to
  642. * identify port count and a suitable Node GUID.
  643. */
  644. for_each_ibdev ( tmp ) {
  645. if ( tmp->dev != ibdev->dev )
  646. continue;
  647. if ( num_ports == 0 ) {
  648. memcpy ( hca_guid, &tmp->gid.u.half[1],
  649. sizeof ( *hca_guid ) );
  650. }
  651. num_ports++;
  652. }
  653. return num_ports;
  654. }
  655. /**
  656. * Set port information
  657. *
  658. * @v ibdev Infiniband device
  659. * @v mad Set port information MAD
  660. */
  661. int ib_set_port_info ( struct ib_device *ibdev, union ib_mad *mad ) {
  662. int rc;
  663. /* Adapters with embedded SMAs do not need to support this method */
  664. if ( ! ibdev->op->set_port_info ) {
  665. DBGC ( ibdev, "IBDEV %p does not support setting port "
  666. "information\n", ibdev );
  667. return -ENOTSUP;
  668. }
  669. if ( ( rc = ibdev->op->set_port_info ( ibdev, mad ) ) != 0 ) {
  670. DBGC ( ibdev, "IBDEV %p could not set port information: %s\n",
  671. ibdev, strerror ( rc ) );
  672. return rc;
  673. }
  674. return 0;
  675. };
  676. /**
  677. * Set partition key table
  678. *
  679. * @v ibdev Infiniband device
  680. * @v mad Set partition key table MAD
  681. */
  682. int ib_set_pkey_table ( struct ib_device *ibdev, union ib_mad *mad ) {
  683. int rc;
  684. /* Adapters with embedded SMAs do not need to support this method */
  685. if ( ! ibdev->op->set_pkey_table ) {
  686. DBGC ( ibdev, "IBDEV %p does not support setting partition "
  687. "key table\n", ibdev );
  688. return -ENOTSUP;
  689. }
  690. if ( ( rc = ibdev->op->set_pkey_table ( ibdev, mad ) ) != 0 ) {
  691. DBGC ( ibdev, "IBDEV %p could not set partition key table: "
  692. "%s\n", ibdev, strerror ( rc ) );
  693. return rc;
  694. }
  695. return 0;
  696. };
  697. /***************************************************************************
  698. *
  699. * Event queues
  700. *
  701. ***************************************************************************
  702. */
  703. /**
  704. * Handle Infiniband link state change
  705. *
  706. * @v ibdev Infiniband device
  707. */
  708. void ib_link_state_changed ( struct ib_device *ibdev ) {
  709. /* Notify IPoIB of link state change */
  710. ipoib_link_state_changed ( ibdev );
  711. }
  712. /**
  713. * Poll event queue
  714. *
  715. * @v ibdev Infiniband device
  716. */
  717. void ib_poll_eq ( struct ib_device *ibdev ) {
  718. struct ib_completion_queue *cq;
  719. /* Poll device's event queue */
  720. ibdev->op->poll_eq ( ibdev );
  721. /* Poll all completion queues */
  722. list_for_each_entry ( cq, &ibdev->cqs, list )
  723. ib_poll_cq ( ibdev, cq );
  724. }
  725. /**
  726. * Single-step the Infiniband event queue
  727. *
  728. * @v process Infiniband event queue process
  729. */
  730. static void ib_step ( struct process *process __unused ) {
  731. struct ib_device *ibdev;
  732. for_each_ibdev ( ibdev )
  733. ib_poll_eq ( ibdev );
  734. }
  735. /** Infiniband event queue process */
  736. struct process ib_process __permanent_process = {
  737. .list = LIST_HEAD_INIT ( ib_process.list ),
  738. .step = ib_step,
  739. };
  740. /***************************************************************************
  741. *
  742. * Infiniband device creation/destruction
  743. *
  744. ***************************************************************************
  745. */
  746. /**
  747. * Allocate Infiniband device
  748. *
  749. * @v priv_size Size of driver private data area
  750. * @ret ibdev Infiniband device, or NULL
  751. */
  752. struct ib_device * alloc_ibdev ( size_t priv_size ) {
  753. struct ib_device *ibdev;
  754. void *drv_priv;
  755. size_t total_len;
  756. total_len = ( sizeof ( *ibdev ) + priv_size );
  757. ibdev = zalloc ( total_len );
  758. if ( ibdev ) {
  759. drv_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
  760. ib_set_drvdata ( ibdev, drv_priv );
  761. INIT_LIST_HEAD ( &ibdev->cqs );
  762. INIT_LIST_HEAD ( &ibdev->qps );
  763. ibdev->port_state = IB_PORT_STATE_DOWN;
  764. ibdev->lid = IB_LID_NONE;
  765. ibdev->pkey = IB_PKEY_DEFAULT;
  766. }
  767. return ibdev;
  768. }
  769. /**
  770. * Register Infiniband device
  771. *
  772. * @v ibdev Infiniband device
  773. * @ret rc Return status code
  774. */
  775. int register_ibdev ( struct ib_device *ibdev ) {
  776. int rc;
  777. /* Add to device list */
  778. ibdev_get ( ibdev );
  779. list_add_tail ( &ibdev->list, &ib_devices );
  780. /* Add IPoIB device */
  781. if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
  782. DBGC ( ibdev, "IBDEV %p could not add IPoIB device: %s\n",
  783. ibdev, strerror ( rc ) );
  784. goto err_ipoib_probe;
  785. }
  786. DBGC ( ibdev, "IBDEV %p registered (phys %s)\n", ibdev,
  787. ibdev->dev->name );
  788. return 0;
  789. err_ipoib_probe:
  790. list_del ( &ibdev->list );
  791. ibdev_put ( ibdev );
  792. return rc;
  793. }
  794. /**
  795. * Unregister Infiniband device
  796. *
  797. * @v ibdev Infiniband device
  798. */
  799. void unregister_ibdev ( struct ib_device *ibdev ) {
  800. /* Close device */
  801. ipoib_remove ( ibdev );
  802. /* Remove from device list */
  803. list_del ( &ibdev->list );
  804. ibdev_put ( ibdev );
  805. DBGC ( ibdev, "IBDEV %p unregistered\n", ibdev );
  806. }
  807. /**
  808. * Find Infiniband device by GID
  809. *
  810. * @v gid GID
  811. * @ret ibdev Infiniband device, or NULL
  812. */
  813. struct ib_device * find_ibdev ( struct ib_gid *gid ) {
  814. struct ib_device *ibdev;
  815. for_each_ibdev ( ibdev ) {
  816. if ( memcmp ( gid, &ibdev->gid, sizeof ( *gid ) ) == 0 )
  817. return ibdev;
  818. }
  819. return NULL;
  820. }
  821. /**
  822. * Get most recently opened Infiniband device
  823. *
  824. * @ret ibdev Most recently opened Infiniband device, or NULL
  825. */
  826. struct ib_device * last_opened_ibdev ( void ) {
  827. struct ib_device *ibdev;
  828. list_for_each_entry ( ibdev, &open_ib_devices, open_list ) {
  829. assert ( ibdev->open_count != 0 );
  830. return ibdev;
  831. }
  832. return NULL;
  833. }