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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <byteswap.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <gpxe/list.h>
  27. #include <gpxe/if_arp.h>
  28. #include <gpxe/netdevice.h>
  29. #include <gpxe/iobuf.h>
  30. #include <gpxe/ipoib.h>
  31. #include <gpxe/process.h>
  32. #include <gpxe/infiniband.h>
  33. /** @file
  34. *
  35. * Infiniband protocol
  36. *
  37. */
  38. /** List of Infiniband devices */
  39. struct list_head ib_devices = LIST_HEAD_INIT ( ib_devices );
  40. /**
  41. * Create completion queue
  42. *
  43. * @v ibdev Infiniband device
  44. * @v num_cqes Number of completion queue entries
  45. * @v op Completion queue operations
  46. * @ret cq New completion queue
  47. */
  48. struct ib_completion_queue *
  49. ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
  50. struct ib_completion_queue_operations *op ) {
  51. struct ib_completion_queue *cq;
  52. int rc;
  53. DBGC ( ibdev, "IBDEV %p creating completion queue\n", ibdev );
  54. /* Allocate and initialise data structure */
  55. cq = zalloc ( sizeof ( *cq ) );
  56. if ( ! cq )
  57. goto err_alloc_cq;
  58. cq->num_cqes = num_cqes;
  59. INIT_LIST_HEAD ( &cq->work_queues );
  60. cq->op = op;
  61. /* Perform device-specific initialisation and get CQN */
  62. if ( ( rc = ibdev->op->create_cq ( ibdev, cq ) ) != 0 ) {
  63. DBGC ( ibdev, "IBDEV %p could not initialise completion "
  64. "queue: %s\n", ibdev, strerror ( rc ) );
  65. goto err_dev_create_cq;
  66. }
  67. DBGC ( ibdev, "IBDEV %p created %d-entry completion queue %p (%p) "
  68. "with CQN %#lx\n", ibdev, num_cqes, cq,
  69. ib_cq_get_drvdata ( cq ), cq->cqn );
  70. return cq;
  71. ibdev->op->destroy_cq ( ibdev, cq );
  72. err_dev_create_cq:
  73. free ( cq );
  74. err_alloc_cq:
  75. return NULL;
  76. }
  77. /**
  78. * Destroy completion queue
  79. *
  80. * @v ibdev Infiniband device
  81. * @v cq Completion queue
  82. */
  83. void ib_destroy_cq ( struct ib_device *ibdev,
  84. struct ib_completion_queue *cq ) {
  85. DBGC ( ibdev, "IBDEV %p destroying completion queue %#lx\n",
  86. ibdev, cq->cqn );
  87. assert ( list_empty ( &cq->work_queues ) );
  88. ibdev->op->destroy_cq ( ibdev, cq );
  89. free ( cq );
  90. }
  91. /**
  92. * Create queue pair
  93. *
  94. * @v ibdev Infiniband device
  95. * @v num_send_wqes Number of send work queue entries
  96. * @v send_cq Send completion queue
  97. * @v num_recv_wqes Number of receive work queue entries
  98. * @v recv_cq Receive completion queue
  99. * @v qkey Queue key
  100. * @ret qp Queue pair
  101. */
  102. struct ib_queue_pair * ib_create_qp ( struct ib_device *ibdev,
  103. unsigned int num_send_wqes,
  104. struct ib_completion_queue *send_cq,
  105. unsigned int num_recv_wqes,
  106. struct ib_completion_queue *recv_cq,
  107. unsigned long qkey ) {
  108. struct ib_queue_pair *qp;
  109. size_t total_size;
  110. int rc;
  111. DBGC ( ibdev, "IBDEV %p creating queue pair\n", ibdev );
  112. /* Allocate and initialise data structure */
  113. total_size = ( sizeof ( *qp ) +
  114. ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ) +
  115. ( num_recv_wqes * sizeof ( qp->recv.iobufs[0] ) ) );
  116. qp = zalloc ( total_size );
  117. if ( ! qp )
  118. goto err_alloc_qp;
  119. qp->ibdev = ibdev;
  120. list_add ( &qp->list, &ibdev->qps );
  121. qp->qkey = qkey;
  122. qp->send.qp = qp;
  123. qp->send.is_send = 1;
  124. qp->send.cq = send_cq;
  125. list_add ( &qp->send.list, &send_cq->work_queues );
  126. qp->send.num_wqes = num_send_wqes;
  127. qp->send.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) );
  128. qp->recv.qp = qp;
  129. qp->recv.cq = recv_cq;
  130. list_add ( &qp->recv.list, &recv_cq->work_queues );
  131. qp->recv.num_wqes = num_recv_wqes;
  132. qp->recv.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) +
  133. ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ));
  134. INIT_LIST_HEAD ( &qp->mgids );
  135. /* Perform device-specific initialisation and get QPN */
  136. if ( ( rc = ibdev->op->create_qp ( ibdev, qp ) ) != 0 ) {
  137. DBGC ( ibdev, "IBDEV %p could not initialise queue pair: "
  138. "%s\n", ibdev, strerror ( rc ) );
  139. goto err_dev_create_qp;
  140. }
  141. DBGC ( ibdev, "IBDEV %p created queue pair %p (%p) with QPN %#lx\n",
  142. ibdev, qp, ib_qp_get_drvdata ( qp ), qp->qpn );
  143. DBGC ( ibdev, "IBDEV %p QPN %#lx has %d send entries at [%p,%p)\n",
  144. ibdev, qp->qpn, num_send_wqes, qp->send.iobufs,
  145. qp->recv.iobufs );
  146. DBGC ( ibdev, "IBDEV %p QPN %#lx has %d receive entries at [%p,%p)\n",
  147. ibdev, qp->qpn, num_recv_wqes, qp->recv.iobufs,
  148. ( ( ( void * ) qp ) + total_size ) );
  149. return qp;
  150. ibdev->op->destroy_qp ( ibdev, qp );
  151. err_dev_create_qp:
  152. list_del ( &qp->send.list );
  153. list_del ( &qp->recv.list );
  154. list_del ( &qp->list );
  155. free ( qp );
  156. err_alloc_qp:
  157. return NULL;
  158. }
  159. /**
  160. * Modify queue pair
  161. *
  162. * @v ibdev Infiniband device
  163. * @v qp Queue pair
  164. * @v mod_list Modification list
  165. * @v qkey New queue key, if applicable
  166. * @ret rc Return status code
  167. */
  168. int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  169. unsigned long mod_list, unsigned long qkey ) {
  170. int rc;
  171. DBGC ( ibdev, "IBDEV %p modifying QPN %#lx\n", ibdev, qp->qpn );
  172. if ( mod_list & IB_MODIFY_QKEY )
  173. qp->qkey = qkey;
  174. if ( ( rc = ibdev->op->modify_qp ( ibdev, qp, mod_list ) ) != 0 ) {
  175. DBGC ( ibdev, "IBDEV %p could not modify QPN %#lx: %s\n",
  176. ibdev, qp->qpn, strerror ( rc ) );
  177. return rc;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * Destroy queue pair
  183. *
  184. * @v ibdev Infiniband device
  185. * @v qp Queue pair
  186. */
  187. void ib_destroy_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  188. struct io_buffer *iobuf;
  189. unsigned int i;
  190. DBGC ( ibdev, "IBDEV %p destroying QPN %#lx\n",
  191. ibdev, qp->qpn );
  192. assert ( list_empty ( &qp->mgids ) );
  193. /* Perform device-specific destruction */
  194. ibdev->op->destroy_qp ( ibdev, qp );
  195. /* Complete any remaining I/O buffers with errors */
  196. for ( i = 0 ; i < qp->send.num_wqes ; i++ ) {
  197. if ( ( iobuf = qp->send.iobufs[i] ) != NULL )
  198. ib_complete_send ( ibdev, qp, iobuf, -ECANCELED );
  199. }
  200. for ( i = 0 ; i < qp->recv.num_wqes ; i++ ) {
  201. if ( ( iobuf = qp->recv.iobufs[i] ) != NULL ) {
  202. ib_complete_recv ( ibdev, qp, NULL, iobuf,
  203. -ECANCELED );
  204. }
  205. }
  206. /* Remove work queues from completion queue */
  207. list_del ( &qp->send.list );
  208. list_del ( &qp->recv.list );
  209. /* Free QP */
  210. list_del ( &qp->list );
  211. free ( qp );
  212. }
  213. /**
  214. * Find queue pair by QPN
  215. *
  216. * @v ibdev Infiniband device
  217. * @v qpn Queue pair number
  218. * @ret qp Queue pair, or NULL
  219. */
  220. struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
  221. unsigned long qpn ) {
  222. struct ib_queue_pair *qp;
  223. list_for_each_entry ( qp, &ibdev->qps, list ) {
  224. if ( qp->qpn == qpn )
  225. return qp;
  226. }
  227. return NULL;
  228. }
  229. /**
  230. * Find queue pair by multicast GID
  231. *
  232. * @v ibdev Infiniband device
  233. * @v gid Multicast GID
  234. * @ret qp Queue pair, or NULL
  235. */
  236. struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
  237. struct ib_gid *gid ) {
  238. struct ib_queue_pair *qp;
  239. struct ib_multicast_gid *mgid;
  240. list_for_each_entry ( qp, &ibdev->qps, list ) {
  241. list_for_each_entry ( mgid, &qp->mgids, list ) {
  242. if ( memcmp ( &mgid->gid, gid,
  243. sizeof ( mgid->gid ) ) == 0 ) {
  244. return qp;
  245. }
  246. }
  247. }
  248. return NULL;
  249. }
  250. /**
  251. * Find work queue belonging to completion queue
  252. *
  253. * @v cq Completion queue
  254. * @v qpn Queue pair number
  255. * @v is_send Find send work queue (rather than receive)
  256. * @ret wq Work queue, or NULL if not found
  257. */
  258. struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
  259. unsigned long qpn, int is_send ) {
  260. struct ib_work_queue *wq;
  261. list_for_each_entry ( wq, &cq->work_queues, list ) {
  262. if ( ( wq->qp->qpn == qpn ) && ( wq->is_send == is_send ) )
  263. return wq;
  264. }
  265. return NULL;
  266. }
  267. /**
  268. * Post send work queue entry
  269. *
  270. * @v ibdev Infiniband device
  271. * @v qp Queue pair
  272. * @v av Address vector
  273. * @v iobuf I/O buffer
  274. * @ret rc Return status code
  275. */
  276. int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  277. struct ib_address_vector *av,
  278. struct io_buffer *iobuf ) {
  279. int rc;
  280. /* Check queue fill level */
  281. if ( qp->send.fill >= qp->send.num_wqes ) {
  282. DBGC ( ibdev, "IBDEV %p QPN %#lx send queue full\n",
  283. ibdev, qp->qpn );
  284. return -ENOBUFS;
  285. }
  286. /* Post to hardware */
  287. if ( ( rc = ibdev->op->post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
  288. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post send WQE: "
  289. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  290. return rc;
  291. }
  292. qp->send.fill++;
  293. return 0;
  294. }
  295. /**
  296. * Post receive work queue entry
  297. *
  298. * @v ibdev Infiniband device
  299. * @v qp Queue pair
  300. * @v iobuf I/O buffer
  301. * @ret rc Return status code
  302. */
  303. int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  304. struct io_buffer *iobuf ) {
  305. int rc;
  306. /* Check queue fill level */
  307. if ( qp->recv.fill >= qp->recv.num_wqes ) {
  308. DBGC ( ibdev, "IBDEV %p QPN %#lx receive queue full\n",
  309. ibdev, qp->qpn );
  310. return -ENOBUFS;
  311. }
  312. /* Post to hardware */
  313. if ( ( rc = ibdev->op->post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  314. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post receive WQE: "
  315. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  316. return rc;
  317. }
  318. qp->recv.fill++;
  319. return 0;
  320. }
  321. /**
  322. * Complete send work queue entry
  323. *
  324. * @v ibdev Infiniband device
  325. * @v qp Queue pair
  326. * @v iobuf I/O buffer
  327. * @v rc Completion status code
  328. */
  329. void ib_complete_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  330. struct io_buffer *iobuf, int rc ) {
  331. qp->send.cq->op->complete_send ( ibdev, qp, iobuf, rc );
  332. qp->send.fill--;
  333. }
  334. /**
  335. * Complete receive work queue entry
  336. *
  337. * @v ibdev Infiniband device
  338. * @v qp Queue pair
  339. * @v av Address vector
  340. * @v iobuf I/O buffer
  341. * @v rc Completion status code
  342. */
  343. void ib_complete_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  344. struct ib_address_vector *av,
  345. struct io_buffer *iobuf, int rc ) {
  346. qp->recv.cq->op->complete_recv ( ibdev, qp, av, iobuf, rc );
  347. qp->recv.fill--;
  348. }
  349. /**
  350. * Attach to multicast group
  351. *
  352. * @v ibdev Infiniband device
  353. * @v qp Queue pair
  354. * @v gid Multicast GID
  355. * @ret rc Return status code
  356. */
  357. int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  358. struct ib_gid *gid ) {
  359. struct ib_multicast_gid *mgid;
  360. int rc;
  361. /* Add to software multicast GID list */
  362. mgid = zalloc ( sizeof ( *mgid ) );
  363. if ( ! mgid ) {
  364. rc = -ENOMEM;
  365. goto err_alloc_mgid;
  366. }
  367. memcpy ( &mgid->gid, gid, sizeof ( mgid->gid ) );
  368. list_add ( &mgid->list, &qp->mgids );
  369. /* Add to hardware multicast GID list */
  370. if ( ( rc = ibdev->op->mcast_attach ( ibdev, qp, gid ) ) != 0 )
  371. goto err_dev_mcast_attach;
  372. return 0;
  373. err_dev_mcast_attach:
  374. list_del ( &mgid->list );
  375. free ( mgid );
  376. err_alloc_mgid:
  377. return rc;
  378. }
  379. /**
  380. * Detach from multicast group
  381. *
  382. * @v ibdev Infiniband device
  383. * @v qp Queue pair
  384. * @v gid Multicast GID
  385. */
  386. void ib_mcast_detach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  387. struct ib_gid *gid ) {
  388. struct ib_multicast_gid *mgid;
  389. /* Remove from hardware multicast GID list */
  390. ibdev->op->mcast_detach ( ibdev, qp, gid );
  391. /* Remove from software multicast GID list */
  392. list_for_each_entry ( mgid, &qp->mgids, list ) {
  393. if ( memcmp ( &mgid->gid, gid, sizeof ( mgid->gid ) ) == 0 ) {
  394. list_del ( &mgid->list );
  395. free ( mgid );
  396. break;
  397. }
  398. }
  399. }
  400. /***************************************************************************
  401. *
  402. * Event queues
  403. *
  404. ***************************************************************************
  405. */
  406. /**
  407. * Handle Infiniband link state change
  408. *
  409. * @v ibdev Infiniband device
  410. */
  411. void ib_link_state_changed ( struct ib_device *ibdev ) {
  412. /* Notify IPoIB of link state change */
  413. ipoib_link_state_changed ( ibdev );
  414. }
  415. /**
  416. * Single-step the Infiniband event queue
  417. *
  418. * @v process Infiniband event queue process
  419. */
  420. static void ib_step ( struct process *process __unused ) {
  421. struct ib_device *ibdev;
  422. list_for_each_entry ( ibdev, &ib_devices, list ) {
  423. ibdev->op->poll_eq ( ibdev );
  424. }
  425. }
  426. /** Infiniband event queue process */
  427. struct process ib_process __permanent_process = {
  428. .step = ib_step,
  429. };
  430. /***************************************************************************
  431. *
  432. * Infiniband device creation/destruction
  433. *
  434. ***************************************************************************
  435. */
  436. /**
  437. * Allocate Infiniband device
  438. *
  439. * @v priv_size Size of driver private data area
  440. * @ret ibdev Infiniband device, or NULL
  441. */
  442. struct ib_device * alloc_ibdev ( size_t priv_size ) {
  443. struct ib_device *ibdev;
  444. void *drv_priv;
  445. size_t total_len;
  446. total_len = ( sizeof ( *ibdev ) + priv_size );
  447. ibdev = zalloc ( total_len );
  448. if ( ibdev ) {
  449. drv_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
  450. ib_set_drvdata ( ibdev, drv_priv );
  451. INIT_LIST_HEAD ( &ibdev->qps );
  452. ibdev->lid = IB_LID_NONE;
  453. ibdev->pkey = IB_PKEY_NONE;
  454. }
  455. return ibdev;
  456. }
  457. /**
  458. * Register Infiniband device
  459. *
  460. * @v ibdev Infiniband device
  461. * @ret rc Return status code
  462. */
  463. int register_ibdev ( struct ib_device *ibdev ) {
  464. int rc;
  465. /* Add to device list */
  466. ibdev_get ( ibdev );
  467. list_add_tail ( &ibdev->list, &ib_devices );
  468. /* Open link */
  469. if ( ( rc = ib_open ( ibdev ) ) != 0 )
  470. goto err_open;
  471. /* Add IPoIB device */
  472. if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
  473. DBGC ( ibdev, "IBDEV %p could not add IPoIB device: %s\n",
  474. ibdev, strerror ( rc ) );
  475. goto err_ipoib_probe;
  476. }
  477. DBGC ( ibdev, "IBDEV %p registered (phys %s)\n", ibdev,
  478. ibdev->dev->name );
  479. return 0;
  480. err_ipoib_probe:
  481. ib_close ( ibdev );
  482. err_open:
  483. list_del ( &ibdev->list );
  484. ibdev_put ( ibdev );
  485. return rc;
  486. }
  487. /**
  488. * Unregister Infiniband device
  489. *
  490. * @v ibdev Infiniband device
  491. */
  492. void unregister_ibdev ( struct ib_device *ibdev ) {
  493. /* Close device */
  494. ipoib_remove ( ibdev );
  495. ib_close ( ibdev );
  496. /* Remove from device list */
  497. list_del ( &ibdev->list );
  498. ibdev_put ( ibdev );
  499. DBGC ( ibdev, "IBDEV %p unregistered\n", ibdev );
  500. }