Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

infiniband.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 <gpxe/list.h>
  28. #include <gpxe/if_arp.h>
  29. #include <gpxe/netdevice.h>
  30. #include <gpxe/iobuf.h>
  31. #include <gpxe/ipoib.h>
  32. #include <gpxe/process.h>
  33. #include <gpxe/infiniband.h>
  34. /** @file
  35. *
  36. * Infiniband protocol
  37. *
  38. */
  39. /** List of Infiniband devices */
  40. struct list_head ib_devices = LIST_HEAD_INIT ( ib_devices );
  41. /***************************************************************************
  42. *
  43. * Completion queues
  44. *
  45. ***************************************************************************
  46. */
  47. /**
  48. * Create completion queue
  49. *
  50. * @v ibdev Infiniband device
  51. * @v num_cqes Number of completion queue entries
  52. * @v op Completion queue operations
  53. * @ret cq New completion queue
  54. */
  55. struct ib_completion_queue *
  56. ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
  57. struct ib_completion_queue_operations *op ) {
  58. struct ib_completion_queue *cq;
  59. int rc;
  60. DBGC ( ibdev, "IBDEV %p creating completion queue\n", ibdev );
  61. /* Allocate and initialise data structure */
  62. cq = zalloc ( sizeof ( *cq ) );
  63. if ( ! cq )
  64. goto err_alloc_cq;
  65. cq->ibdev = ibdev;
  66. list_add ( &cq->list, &ibdev->cqs );
  67. cq->num_cqes = num_cqes;
  68. INIT_LIST_HEAD ( &cq->work_queues );
  69. cq->op = op;
  70. /* Perform device-specific initialisation and get CQN */
  71. if ( ( rc = ibdev->op->create_cq ( ibdev, cq ) ) != 0 ) {
  72. DBGC ( ibdev, "IBDEV %p could not initialise completion "
  73. "queue: %s\n", ibdev, strerror ( rc ) );
  74. goto err_dev_create_cq;
  75. }
  76. DBGC ( ibdev, "IBDEV %p created %d-entry completion queue %p (%p) "
  77. "with CQN %#lx\n", ibdev, num_cqes, cq,
  78. ib_cq_get_drvdata ( cq ), cq->cqn );
  79. return cq;
  80. ibdev->op->destroy_cq ( ibdev, cq );
  81. err_dev_create_cq:
  82. list_del ( &cq->list );
  83. free ( cq );
  84. err_alloc_cq:
  85. return NULL;
  86. }
  87. /**
  88. * Destroy completion queue
  89. *
  90. * @v ibdev Infiniband device
  91. * @v cq Completion queue
  92. */
  93. void ib_destroy_cq ( struct ib_device *ibdev,
  94. struct ib_completion_queue *cq ) {
  95. DBGC ( ibdev, "IBDEV %p destroying completion queue %#lx\n",
  96. ibdev, cq->cqn );
  97. assert ( list_empty ( &cq->work_queues ) );
  98. ibdev->op->destroy_cq ( ibdev, cq );
  99. list_del ( &cq->list );
  100. free ( cq );
  101. }
  102. /**
  103. * Poll completion queue
  104. *
  105. * @v ibdev Infiniband device
  106. * @v cq Completion queue
  107. */
  108. void ib_poll_cq ( struct ib_device *ibdev,
  109. struct ib_completion_queue *cq ) {
  110. struct ib_work_queue *wq;
  111. /* Poll completion queue */
  112. ibdev->op->poll_cq ( ibdev, cq );
  113. /* Refill receive work queues */
  114. list_for_each_entry ( wq, &cq->work_queues, list ) {
  115. if ( ! wq->is_send )
  116. ib_refill_recv ( ibdev, wq->qp );
  117. }
  118. }
  119. /***************************************************************************
  120. *
  121. * Work queues
  122. *
  123. ***************************************************************************
  124. */
  125. /**
  126. * Create queue pair
  127. *
  128. * @v ibdev Infiniband device
  129. * @v num_send_wqes Number of send work queue entries
  130. * @v send_cq Send completion queue
  131. * @v num_recv_wqes Number of receive work queue entries
  132. * @v recv_cq Receive completion queue
  133. * @v qkey Queue key
  134. * @ret qp Queue pair
  135. */
  136. struct ib_queue_pair * ib_create_qp ( struct ib_device *ibdev,
  137. unsigned int num_send_wqes,
  138. struct ib_completion_queue *send_cq,
  139. unsigned int num_recv_wqes,
  140. struct ib_completion_queue *recv_cq,
  141. unsigned long qkey ) {
  142. struct ib_queue_pair *qp;
  143. size_t total_size;
  144. int rc;
  145. DBGC ( ibdev, "IBDEV %p creating queue pair\n", ibdev );
  146. /* Allocate and initialise data structure */
  147. total_size = ( sizeof ( *qp ) +
  148. ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ) +
  149. ( num_recv_wqes * sizeof ( qp->recv.iobufs[0] ) ) );
  150. qp = zalloc ( total_size );
  151. if ( ! qp )
  152. goto err_alloc_qp;
  153. qp->ibdev = ibdev;
  154. list_add ( &qp->list, &ibdev->qps );
  155. qp->qkey = qkey;
  156. qp->send.qp = qp;
  157. qp->send.is_send = 1;
  158. qp->send.cq = send_cq;
  159. list_add ( &qp->send.list, &send_cq->work_queues );
  160. qp->send.num_wqes = num_send_wqes;
  161. qp->send.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) );
  162. qp->recv.qp = qp;
  163. qp->recv.cq = recv_cq;
  164. list_add ( &qp->recv.list, &recv_cq->work_queues );
  165. qp->recv.num_wqes = num_recv_wqes;
  166. qp->recv.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) +
  167. ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ));
  168. INIT_LIST_HEAD ( &qp->mgids );
  169. /* Perform device-specific initialisation and get QPN */
  170. if ( ( rc = ibdev->op->create_qp ( ibdev, qp ) ) != 0 ) {
  171. DBGC ( ibdev, "IBDEV %p could not initialise queue pair: "
  172. "%s\n", ibdev, strerror ( rc ) );
  173. goto err_dev_create_qp;
  174. }
  175. DBGC ( ibdev, "IBDEV %p created queue pair %p (%p) with QPN %#lx\n",
  176. ibdev, qp, ib_qp_get_drvdata ( qp ), qp->qpn );
  177. DBGC ( ibdev, "IBDEV %p QPN %#lx has %d send entries at [%p,%p)\n",
  178. ibdev, qp->qpn, num_send_wqes, qp->send.iobufs,
  179. qp->recv.iobufs );
  180. DBGC ( ibdev, "IBDEV %p QPN %#lx has %d receive entries at [%p,%p)\n",
  181. ibdev, qp->qpn, num_recv_wqes, qp->recv.iobufs,
  182. ( ( ( void * ) qp ) + total_size ) );
  183. return qp;
  184. ibdev->op->destroy_qp ( ibdev, qp );
  185. err_dev_create_qp:
  186. list_del ( &qp->send.list );
  187. list_del ( &qp->recv.list );
  188. list_del ( &qp->list );
  189. free ( qp );
  190. err_alloc_qp:
  191. return NULL;
  192. }
  193. /**
  194. * Modify queue pair
  195. *
  196. * @v ibdev Infiniband device
  197. * @v qp Queue pair
  198. * @v mod_list Modification list
  199. * @v qkey New queue key, if applicable
  200. * @ret rc Return status code
  201. */
  202. int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  203. unsigned long mod_list, unsigned long qkey ) {
  204. int rc;
  205. DBGC ( ibdev, "IBDEV %p modifying QPN %#lx\n", ibdev, qp->qpn );
  206. if ( mod_list & IB_MODIFY_QKEY )
  207. qp->qkey = qkey;
  208. if ( ( rc = ibdev->op->modify_qp ( ibdev, qp, mod_list ) ) != 0 ) {
  209. DBGC ( ibdev, "IBDEV %p could not modify QPN %#lx: %s\n",
  210. ibdev, qp->qpn, strerror ( rc ) );
  211. return rc;
  212. }
  213. return 0;
  214. }
  215. /**
  216. * Destroy queue pair
  217. *
  218. * @v ibdev Infiniband device
  219. * @v qp Queue pair
  220. */
  221. void ib_destroy_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  222. struct io_buffer *iobuf;
  223. unsigned int i;
  224. DBGC ( ibdev, "IBDEV %p destroying QPN %#lx\n",
  225. ibdev, qp->qpn );
  226. assert ( list_empty ( &qp->mgids ) );
  227. /* Perform device-specific destruction */
  228. ibdev->op->destroy_qp ( ibdev, qp );
  229. /* Complete any remaining I/O buffers with errors */
  230. for ( i = 0 ; i < qp->send.num_wqes ; i++ ) {
  231. if ( ( iobuf = qp->send.iobufs[i] ) != NULL )
  232. ib_complete_send ( ibdev, qp, iobuf, -ECANCELED );
  233. }
  234. for ( i = 0 ; i < qp->recv.num_wqes ; i++ ) {
  235. if ( ( iobuf = qp->recv.iobufs[i] ) != NULL ) {
  236. ib_complete_recv ( ibdev, qp, NULL, iobuf,
  237. -ECANCELED );
  238. }
  239. }
  240. /* Remove work queues from completion queue */
  241. list_del ( &qp->send.list );
  242. list_del ( &qp->recv.list );
  243. /* Free QP */
  244. list_del ( &qp->list );
  245. free ( qp );
  246. }
  247. /**
  248. * Find queue pair by QPN
  249. *
  250. * @v ibdev Infiniband device
  251. * @v qpn Queue pair number
  252. * @ret qp Queue pair, or NULL
  253. */
  254. struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
  255. unsigned long qpn ) {
  256. struct ib_queue_pair *qp;
  257. list_for_each_entry ( qp, &ibdev->qps, list ) {
  258. if ( qp->qpn == qpn )
  259. return qp;
  260. }
  261. return NULL;
  262. }
  263. /**
  264. * Find queue pair by multicast GID
  265. *
  266. * @v ibdev Infiniband device
  267. * @v gid Multicast GID
  268. * @ret qp Queue pair, or NULL
  269. */
  270. struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
  271. struct ib_gid *gid ) {
  272. struct ib_queue_pair *qp;
  273. struct ib_multicast_gid *mgid;
  274. list_for_each_entry ( qp, &ibdev->qps, list ) {
  275. list_for_each_entry ( mgid, &qp->mgids, list ) {
  276. if ( memcmp ( &mgid->gid, gid,
  277. sizeof ( mgid->gid ) ) == 0 ) {
  278. return qp;
  279. }
  280. }
  281. }
  282. return NULL;
  283. }
  284. /**
  285. * Find work queue belonging to completion queue
  286. *
  287. * @v cq Completion queue
  288. * @v qpn Queue pair number
  289. * @v is_send Find send work queue (rather than receive)
  290. * @ret wq Work queue, or NULL if not found
  291. */
  292. struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
  293. unsigned long qpn, int is_send ) {
  294. struct ib_work_queue *wq;
  295. list_for_each_entry ( wq, &cq->work_queues, list ) {
  296. if ( ( wq->qp->qpn == qpn ) && ( wq->is_send == is_send ) )
  297. return wq;
  298. }
  299. return NULL;
  300. }
  301. /**
  302. * Post send work queue entry
  303. *
  304. * @v ibdev Infiniband device
  305. * @v qp Queue pair
  306. * @v av Address vector
  307. * @v iobuf I/O buffer
  308. * @ret rc Return status code
  309. */
  310. int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  311. struct ib_address_vector *av,
  312. struct io_buffer *iobuf ) {
  313. int rc;
  314. /* Check queue fill level */
  315. if ( qp->send.fill >= qp->send.num_wqes ) {
  316. DBGC ( ibdev, "IBDEV %p QPN %#lx send queue full\n",
  317. ibdev, qp->qpn );
  318. return -ENOBUFS;
  319. }
  320. /* Post to hardware */
  321. if ( ( rc = ibdev->op->post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
  322. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post send WQE: "
  323. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  324. return rc;
  325. }
  326. qp->send.fill++;
  327. return 0;
  328. }
  329. /**
  330. * Post receive work queue entry
  331. *
  332. * @v ibdev Infiniband device
  333. * @v qp Queue pair
  334. * @v iobuf I/O buffer
  335. * @ret rc Return status code
  336. */
  337. int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  338. struct io_buffer *iobuf ) {
  339. int rc;
  340. /* Check packet length */
  341. if ( iob_tailroom ( iobuf ) < IB_MAX_PAYLOAD_SIZE ) {
  342. DBGC ( ibdev, "IBDEV %p QPN %#lx wrong RX buffer size (%zd)\n",
  343. ibdev, qp->qpn, iob_tailroom ( iobuf ) );
  344. return -EINVAL;
  345. }
  346. /* Check queue fill level */
  347. if ( qp->recv.fill >= qp->recv.num_wqes ) {
  348. DBGC ( ibdev, "IBDEV %p QPN %#lx receive queue full\n",
  349. ibdev, qp->qpn );
  350. return -ENOBUFS;
  351. }
  352. /* Post to hardware */
  353. if ( ( rc = ibdev->op->post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  354. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post receive WQE: "
  355. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  356. return rc;
  357. }
  358. qp->recv.fill++;
  359. return 0;
  360. }
  361. /**
  362. * Complete send work queue entry
  363. *
  364. * @v ibdev Infiniband device
  365. * @v qp Queue pair
  366. * @v iobuf I/O buffer
  367. * @v rc Completion status code
  368. */
  369. void ib_complete_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  370. struct io_buffer *iobuf, int rc ) {
  371. qp->send.cq->op->complete_send ( ibdev, qp, iobuf, rc );
  372. qp->send.fill--;
  373. }
  374. /**
  375. * Complete receive work queue entry
  376. *
  377. * @v ibdev Infiniband device
  378. * @v qp Queue pair
  379. * @v av Address vector
  380. * @v iobuf I/O buffer
  381. * @v rc Completion status code
  382. */
  383. void ib_complete_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  384. struct ib_address_vector *av,
  385. struct io_buffer *iobuf, int rc ) {
  386. qp->recv.cq->op->complete_recv ( ibdev, qp, av, iobuf, rc );
  387. qp->recv.fill--;
  388. }
  389. /**
  390. * Refill receive work queue
  391. *
  392. * @v ibdev Infiniband device
  393. * @v qp Queue pair
  394. */
  395. void ib_refill_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  396. struct io_buffer *iobuf;
  397. int rc;
  398. /* Keep filling while unfilled entries remain */
  399. while ( qp->recv.fill < qp->recv.num_wqes ) {
  400. /* Allocate I/O buffer */
  401. iobuf = alloc_iob ( IB_MAX_PAYLOAD_SIZE );
  402. if ( ! iobuf ) {
  403. /* Non-fatal; we will refill on next attempt */
  404. return;
  405. }
  406. /* Post I/O buffer */
  407. if ( ( rc = ib_post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  408. DBGC ( ibdev, "IBDEV %p could not refill: %s\n",
  409. ibdev, strerror ( rc ) );
  410. free_iob ( iobuf );
  411. /* Give up */
  412. return;
  413. }
  414. }
  415. }
  416. /***************************************************************************
  417. *
  418. * Link control
  419. *
  420. ***************************************************************************
  421. */
  422. /**
  423. * Open port
  424. *
  425. * @v ibdev Infiniband device
  426. * @ret rc Return status code
  427. */
  428. int ib_open ( struct ib_device *ibdev ) {
  429. int rc;
  430. /* Increment device open request counter */
  431. if ( ibdev->open_count++ > 0 ) {
  432. /* Device was already open; do nothing */
  433. return 0;
  434. }
  435. /* Open device */
  436. if ( ( rc = ibdev->op->open ( ibdev ) ) != 0 ) {
  437. DBGC ( ibdev, "IBDEV %p could not open: %s\n",
  438. ibdev, strerror ( rc ) );
  439. goto err_open;
  440. }
  441. /* Create general management agent */
  442. if ( ( rc = ib_create_gma ( &ibdev->gma, ibdev, IB_QKEY_GMA ) ) != 0 ){
  443. DBGC ( ibdev, "IBDEV %p could not create GMA: %s\n",
  444. ibdev, strerror ( rc ) );
  445. goto err_create_gma;
  446. }
  447. assert ( ibdev->open_count == 1 );
  448. return 0;
  449. ib_destroy_gma ( &ibdev->gma );
  450. err_create_gma:
  451. ibdev->op->close ( ibdev );
  452. err_open:
  453. assert ( ibdev->open_count == 1 );
  454. ibdev->open_count = 0;
  455. return rc;
  456. }
  457. /**
  458. * Close port
  459. *
  460. * @v ibdev Infiniband device
  461. */
  462. void ib_close ( struct ib_device *ibdev ) {
  463. /* Decrement device open request counter */
  464. ibdev->open_count--;
  465. /* Close device if this was the last remaining requested opening */
  466. if ( ibdev->open_count == 0 ) {
  467. ib_destroy_gma ( &ibdev->gma );
  468. ibdev->op->close ( ibdev );
  469. }
  470. }
  471. /***************************************************************************
  472. *
  473. * Multicast
  474. *
  475. ***************************************************************************
  476. */
  477. /**
  478. * Attach to multicast group
  479. *
  480. * @v ibdev Infiniband device
  481. * @v qp Queue pair
  482. * @v gid Multicast GID
  483. * @ret rc Return status code
  484. */
  485. int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  486. struct ib_gid *gid ) {
  487. struct ib_multicast_gid *mgid;
  488. int rc;
  489. /* Add to software multicast GID list */
  490. mgid = zalloc ( sizeof ( *mgid ) );
  491. if ( ! mgid ) {
  492. rc = -ENOMEM;
  493. goto err_alloc_mgid;
  494. }
  495. memcpy ( &mgid->gid, gid, sizeof ( mgid->gid ) );
  496. list_add ( &mgid->list, &qp->mgids );
  497. /* Add to hardware multicast GID list */
  498. if ( ( rc = ibdev->op->mcast_attach ( ibdev, qp, gid ) ) != 0 )
  499. goto err_dev_mcast_attach;
  500. return 0;
  501. err_dev_mcast_attach:
  502. list_del ( &mgid->list );
  503. free ( mgid );
  504. err_alloc_mgid:
  505. return rc;
  506. }
  507. /**
  508. * Detach from multicast group
  509. *
  510. * @v ibdev Infiniband device
  511. * @v qp Queue pair
  512. * @v gid Multicast GID
  513. */
  514. void ib_mcast_detach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  515. struct ib_gid *gid ) {
  516. struct ib_multicast_gid *mgid;
  517. /* Remove from hardware multicast GID list */
  518. ibdev->op->mcast_detach ( ibdev, qp, gid );
  519. /* Remove from software multicast GID list */
  520. list_for_each_entry ( mgid, &qp->mgids, list ) {
  521. if ( memcmp ( &mgid->gid, gid, sizeof ( mgid->gid ) ) == 0 ) {
  522. list_del ( &mgid->list );
  523. free ( mgid );
  524. break;
  525. }
  526. }
  527. }
  528. /***************************************************************************
  529. *
  530. * Miscellaneous
  531. *
  532. ***************************************************************************
  533. */
  534. /**
  535. * Get Infiniband HCA information
  536. *
  537. * @v ibdev Infiniband device
  538. * @ret hca_guid HCA GUID
  539. * @ret num_ports Number of ports
  540. */
  541. int ib_get_hca_info ( struct ib_device *ibdev,
  542. struct ib_gid_half *hca_guid ) {
  543. struct ib_device *tmp;
  544. int num_ports = 0;
  545. /* Search for IB devices with the same physical device to
  546. * identify port count and a suitable Node GUID.
  547. */
  548. for_each_ibdev ( tmp ) {
  549. if ( tmp->dev != ibdev->dev )
  550. continue;
  551. if ( num_ports == 0 ) {
  552. memcpy ( hca_guid, &tmp->gid.u.half[1],
  553. sizeof ( *hca_guid ) );
  554. }
  555. num_ports++;
  556. }
  557. return num_ports;
  558. }
  559. /***************************************************************************
  560. *
  561. * Event queues
  562. *
  563. ***************************************************************************
  564. */
  565. /**
  566. * Handle Infiniband link state change
  567. *
  568. * @v ibdev Infiniband device
  569. */
  570. void ib_link_state_changed ( struct ib_device *ibdev ) {
  571. /* Notify IPoIB of link state change */
  572. ipoib_link_state_changed ( ibdev );
  573. }
  574. /**
  575. * Poll event queue
  576. *
  577. * @v ibdev Infiniband device
  578. */
  579. void ib_poll_eq ( struct ib_device *ibdev ) {
  580. struct ib_completion_queue *cq;
  581. /* Poll device's event queue */
  582. ibdev->op->poll_eq ( ibdev );
  583. /* Poll all completion queues */
  584. list_for_each_entry ( cq, &ibdev->cqs, list )
  585. ib_poll_cq ( ibdev, cq );
  586. }
  587. /**
  588. * Single-step the Infiniband event queue
  589. *
  590. * @v process Infiniband event queue process
  591. */
  592. static void ib_step ( struct process *process __unused ) {
  593. struct ib_device *ibdev;
  594. for_each_ibdev ( ibdev )
  595. ib_poll_eq ( ibdev );
  596. }
  597. /** Infiniband event queue process */
  598. struct process ib_process __permanent_process = {
  599. .step = ib_step,
  600. };
  601. /***************************************************************************
  602. *
  603. * Infiniband device creation/destruction
  604. *
  605. ***************************************************************************
  606. */
  607. /**
  608. * Allocate Infiniband device
  609. *
  610. * @v priv_size Size of driver private data area
  611. * @ret ibdev Infiniband device, or NULL
  612. */
  613. struct ib_device * alloc_ibdev ( size_t priv_size ) {
  614. struct ib_device *ibdev;
  615. void *drv_priv;
  616. size_t total_len;
  617. total_len = ( sizeof ( *ibdev ) + priv_size );
  618. ibdev = zalloc ( total_len );
  619. if ( ibdev ) {
  620. drv_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
  621. ib_set_drvdata ( ibdev, drv_priv );
  622. INIT_LIST_HEAD ( &ibdev->cqs );
  623. INIT_LIST_HEAD ( &ibdev->qps );
  624. ibdev->lid = IB_LID_NONE;
  625. ibdev->pkey = IB_PKEY_NONE;
  626. }
  627. return ibdev;
  628. }
  629. /**
  630. * Register Infiniband device
  631. *
  632. * @v ibdev Infiniband device
  633. * @ret rc Return status code
  634. */
  635. int register_ibdev ( struct ib_device *ibdev ) {
  636. int rc;
  637. /* Add to device list */
  638. ibdev_get ( ibdev );
  639. list_add_tail ( &ibdev->list, &ib_devices );
  640. /* Add IPoIB device */
  641. if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
  642. DBGC ( ibdev, "IBDEV %p could not add IPoIB device: %s\n",
  643. ibdev, strerror ( rc ) );
  644. goto err_ipoib_probe;
  645. }
  646. DBGC ( ibdev, "IBDEV %p registered (phys %s)\n", ibdev,
  647. ibdev->dev->name );
  648. return 0;
  649. err_ipoib_probe:
  650. list_del ( &ibdev->list );
  651. ibdev_put ( ibdev );
  652. return rc;
  653. }
  654. /**
  655. * Unregister Infiniband device
  656. *
  657. * @v ibdev Infiniband device
  658. */
  659. void unregister_ibdev ( struct ib_device *ibdev ) {
  660. /* Close device */
  661. ipoib_remove ( ibdev );
  662. /* Remove from device list */
  663. list_del ( &ibdev->list );
  664. ibdev_put ( ibdev );
  665. DBGC ( ibdev, "IBDEV %p unregistered\n", ibdev );
  666. }