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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. /* Fill in optional parameters in address vector */
  321. if ( ! av->qkey )
  322. av->qkey = qp->qkey;
  323. if ( ! av->rate )
  324. av->rate = IB_RATE_2_5;
  325. /* Post to hardware */
  326. if ( ( rc = ibdev->op->post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
  327. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post send WQE: "
  328. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  329. return rc;
  330. }
  331. qp->send.fill++;
  332. return 0;
  333. }
  334. /**
  335. * Post receive work queue entry
  336. *
  337. * @v ibdev Infiniband device
  338. * @v qp Queue pair
  339. * @v iobuf I/O buffer
  340. * @ret rc Return status code
  341. */
  342. int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  343. struct io_buffer *iobuf ) {
  344. int rc;
  345. /* Check packet length */
  346. if ( iob_tailroom ( iobuf ) < IB_MAX_PAYLOAD_SIZE ) {
  347. DBGC ( ibdev, "IBDEV %p QPN %#lx wrong RX buffer size (%zd)\n",
  348. ibdev, qp->qpn, iob_tailroom ( iobuf ) );
  349. return -EINVAL;
  350. }
  351. /* Check queue fill level */
  352. if ( qp->recv.fill >= qp->recv.num_wqes ) {
  353. DBGC ( ibdev, "IBDEV %p QPN %#lx receive queue full\n",
  354. ibdev, qp->qpn );
  355. return -ENOBUFS;
  356. }
  357. /* Post to hardware */
  358. if ( ( rc = ibdev->op->post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  359. DBGC ( ibdev, "IBDEV %p QPN %#lx could not post receive WQE: "
  360. "%s\n", ibdev, qp->qpn, strerror ( rc ) );
  361. return rc;
  362. }
  363. qp->recv.fill++;
  364. return 0;
  365. }
  366. /**
  367. * Complete send work queue entry
  368. *
  369. * @v ibdev Infiniband device
  370. * @v qp Queue pair
  371. * @v iobuf I/O buffer
  372. * @v rc Completion status code
  373. */
  374. void ib_complete_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  375. struct io_buffer *iobuf, int rc ) {
  376. qp->send.cq->op->complete_send ( ibdev, qp, iobuf, rc );
  377. qp->send.fill--;
  378. }
  379. /**
  380. * Complete receive work queue entry
  381. *
  382. * @v ibdev Infiniband device
  383. * @v qp Queue pair
  384. * @v av Address vector
  385. * @v iobuf I/O buffer
  386. * @v rc Completion status code
  387. */
  388. void ib_complete_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  389. struct ib_address_vector *av,
  390. struct io_buffer *iobuf, int rc ) {
  391. qp->recv.cq->op->complete_recv ( ibdev, qp, av, iobuf, rc );
  392. qp->recv.fill--;
  393. }
  394. /**
  395. * Refill receive work queue
  396. *
  397. * @v ibdev Infiniband device
  398. * @v qp Queue pair
  399. */
  400. void ib_refill_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
  401. struct io_buffer *iobuf;
  402. int rc;
  403. /* Keep filling while unfilled entries remain */
  404. while ( qp->recv.fill < qp->recv.num_wqes ) {
  405. /* Allocate I/O buffer */
  406. iobuf = alloc_iob ( IB_MAX_PAYLOAD_SIZE );
  407. if ( ! iobuf ) {
  408. /* Non-fatal; we will refill on next attempt */
  409. return;
  410. }
  411. /* Post I/O buffer */
  412. if ( ( rc = ib_post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
  413. DBGC ( ibdev, "IBDEV %p could not refill: %s\n",
  414. ibdev, strerror ( rc ) );
  415. free_iob ( iobuf );
  416. /* Give up */
  417. return;
  418. }
  419. }
  420. }
  421. /***************************************************************************
  422. *
  423. * Link control
  424. *
  425. ***************************************************************************
  426. */
  427. /**
  428. * Open port
  429. *
  430. * @v ibdev Infiniband device
  431. * @ret rc Return status code
  432. */
  433. int ib_open ( struct ib_device *ibdev ) {
  434. int rc;
  435. /* Increment device open request counter */
  436. if ( ibdev->open_count++ > 0 ) {
  437. /* Device was already open; do nothing */
  438. return 0;
  439. }
  440. /* Open device */
  441. if ( ( rc = ibdev->op->open ( ibdev ) ) != 0 ) {
  442. DBGC ( ibdev, "IBDEV %p could not open: %s\n",
  443. ibdev, strerror ( rc ) );
  444. goto err_open;
  445. }
  446. /* Create general management agent */
  447. if ( ( rc = ib_create_gma ( &ibdev->gma, ibdev, IB_QKEY_GMA ) ) != 0 ){
  448. DBGC ( ibdev, "IBDEV %p could not create GMA: %s\n",
  449. ibdev, strerror ( rc ) );
  450. goto err_create_gma;
  451. }
  452. assert ( ibdev->open_count == 1 );
  453. return 0;
  454. ib_destroy_gma ( &ibdev->gma );
  455. err_create_gma:
  456. ibdev->op->close ( ibdev );
  457. err_open:
  458. assert ( ibdev->open_count == 1 );
  459. ibdev->open_count = 0;
  460. return rc;
  461. }
  462. /**
  463. * Close port
  464. *
  465. * @v ibdev Infiniband device
  466. */
  467. void ib_close ( struct ib_device *ibdev ) {
  468. /* Decrement device open request counter */
  469. ibdev->open_count--;
  470. /* Close device if this was the last remaining requested opening */
  471. if ( ibdev->open_count == 0 ) {
  472. ib_destroy_gma ( &ibdev->gma );
  473. ibdev->op->close ( ibdev );
  474. }
  475. }
  476. /***************************************************************************
  477. *
  478. * Multicast
  479. *
  480. ***************************************************************************
  481. */
  482. /**
  483. * Attach to multicast group
  484. *
  485. * @v ibdev Infiniband device
  486. * @v qp Queue pair
  487. * @v gid Multicast GID
  488. * @ret rc Return status code
  489. *
  490. * Note that this function handles only the local device's attachment
  491. * to the multicast GID; it does not issue the relevant MADs to join
  492. * the multicast group on the subnet.
  493. */
  494. int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  495. struct ib_gid *gid ) {
  496. struct ib_multicast_gid *mgid;
  497. int rc;
  498. /* Add to software multicast GID list */
  499. mgid = zalloc ( sizeof ( *mgid ) );
  500. if ( ! mgid ) {
  501. rc = -ENOMEM;
  502. goto err_alloc_mgid;
  503. }
  504. memcpy ( &mgid->gid, gid, sizeof ( mgid->gid ) );
  505. list_add ( &mgid->list, &qp->mgids );
  506. /* Add to hardware multicast GID list */
  507. if ( ( rc = ibdev->op->mcast_attach ( ibdev, qp, gid ) ) != 0 )
  508. goto err_dev_mcast_attach;
  509. return 0;
  510. err_dev_mcast_attach:
  511. list_del ( &mgid->list );
  512. free ( mgid );
  513. err_alloc_mgid:
  514. return rc;
  515. }
  516. /**
  517. * Detach from multicast group
  518. *
  519. * @v ibdev Infiniband device
  520. * @v qp Queue pair
  521. * @v gid Multicast GID
  522. */
  523. void ib_mcast_detach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  524. struct ib_gid *gid ) {
  525. struct ib_multicast_gid *mgid;
  526. /* Remove from hardware multicast GID list */
  527. ibdev->op->mcast_detach ( ibdev, qp, gid );
  528. /* Remove from software multicast GID list */
  529. list_for_each_entry ( mgid, &qp->mgids, list ) {
  530. if ( memcmp ( &mgid->gid, gid, sizeof ( mgid->gid ) ) == 0 ) {
  531. list_del ( &mgid->list );
  532. free ( mgid );
  533. break;
  534. }
  535. }
  536. }
  537. /***************************************************************************
  538. *
  539. * Miscellaneous
  540. *
  541. ***************************************************************************
  542. */
  543. /**
  544. * Get Infiniband HCA information
  545. *
  546. * @v ibdev Infiniband device
  547. * @ret hca_guid HCA GUID
  548. * @ret num_ports Number of ports
  549. */
  550. int ib_get_hca_info ( struct ib_device *ibdev,
  551. struct ib_gid_half *hca_guid ) {
  552. struct ib_device *tmp;
  553. int num_ports = 0;
  554. /* Search for IB devices with the same physical device to
  555. * identify port count and a suitable Node GUID.
  556. */
  557. for_each_ibdev ( tmp ) {
  558. if ( tmp->dev != ibdev->dev )
  559. continue;
  560. if ( num_ports == 0 ) {
  561. memcpy ( hca_guid, &tmp->gid.u.half[1],
  562. sizeof ( *hca_guid ) );
  563. }
  564. num_ports++;
  565. }
  566. return num_ports;
  567. }
  568. /** Set port information
  569. *
  570. * @v ibdev Infiniband device
  571. * @v port_info New port information
  572. */
  573. int ib_set_port_info ( struct ib_device *ibdev,
  574. const struct ib_port_info *port_info ) {
  575. int rc;
  576. /* Adapters with embedded SMAs do not need to support this method */
  577. if ( ! ibdev->op->set_port_info ) {
  578. DBGC ( ibdev, "IBDEV %p does not support setting port "
  579. "information\n", ibdev );
  580. return -ENOTSUP;
  581. }
  582. if ( ( rc = ibdev->op->set_port_info ( ibdev, port_info ) ) != 0 ) {
  583. DBGC ( ibdev, "IBDEV %p could not set port information: %s\n",
  584. ibdev, strerror ( rc ) );
  585. return rc;
  586. }
  587. return 0;
  588. };
  589. /***************************************************************************
  590. *
  591. * Event queues
  592. *
  593. ***************************************************************************
  594. */
  595. /**
  596. * Handle Infiniband link state change
  597. *
  598. * @v ibdev Infiniband device
  599. */
  600. void ib_link_state_changed ( struct ib_device *ibdev ) {
  601. /* Notify IPoIB of link state change */
  602. ipoib_link_state_changed ( ibdev );
  603. }
  604. /**
  605. * Poll event queue
  606. *
  607. * @v ibdev Infiniband device
  608. */
  609. void ib_poll_eq ( struct ib_device *ibdev ) {
  610. struct ib_completion_queue *cq;
  611. /* Poll device's event queue */
  612. ibdev->op->poll_eq ( ibdev );
  613. /* Poll all completion queues */
  614. list_for_each_entry ( cq, &ibdev->cqs, list )
  615. ib_poll_cq ( ibdev, cq );
  616. }
  617. /**
  618. * Single-step the Infiniband event queue
  619. *
  620. * @v process Infiniband event queue process
  621. */
  622. static void ib_step ( struct process *process __unused ) {
  623. struct ib_device *ibdev;
  624. for_each_ibdev ( ibdev )
  625. ib_poll_eq ( ibdev );
  626. }
  627. /** Infiniband event queue process */
  628. struct process ib_process __permanent_process = {
  629. .step = ib_step,
  630. };
  631. /***************************************************************************
  632. *
  633. * Infiniband device creation/destruction
  634. *
  635. ***************************************************************************
  636. */
  637. /**
  638. * Allocate Infiniband device
  639. *
  640. * @v priv_size Size of driver private data area
  641. * @ret ibdev Infiniband device, or NULL
  642. */
  643. struct ib_device * alloc_ibdev ( size_t priv_size ) {
  644. struct ib_device *ibdev;
  645. void *drv_priv;
  646. size_t total_len;
  647. total_len = ( sizeof ( *ibdev ) + priv_size );
  648. ibdev = zalloc ( total_len );
  649. if ( ibdev ) {
  650. drv_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
  651. ib_set_drvdata ( ibdev, drv_priv );
  652. INIT_LIST_HEAD ( &ibdev->cqs );
  653. INIT_LIST_HEAD ( &ibdev->qps );
  654. ibdev->lid = IB_LID_NONE;
  655. ibdev->pkey = IB_PKEY_NONE;
  656. }
  657. return ibdev;
  658. }
  659. /**
  660. * Register Infiniband device
  661. *
  662. * @v ibdev Infiniband device
  663. * @ret rc Return status code
  664. */
  665. int register_ibdev ( struct ib_device *ibdev ) {
  666. int rc;
  667. /* Add to device list */
  668. ibdev_get ( ibdev );
  669. list_add_tail ( &ibdev->list, &ib_devices );
  670. /* Add IPoIB device */
  671. if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
  672. DBGC ( ibdev, "IBDEV %p could not add IPoIB device: %s\n",
  673. ibdev, strerror ( rc ) );
  674. goto err_ipoib_probe;
  675. }
  676. DBGC ( ibdev, "IBDEV %p registered (phys %s)\n", ibdev,
  677. ibdev->dev->name );
  678. return 0;
  679. err_ipoib_probe:
  680. list_del ( &ibdev->list );
  681. ibdev_put ( ibdev );
  682. return rc;
  683. }
  684. /**
  685. * Unregister Infiniband device
  686. *
  687. * @v ibdev Infiniband device
  688. */
  689. void unregister_ibdev ( struct ib_device *ibdev ) {
  690. /* Close device */
  691. ipoib_remove ( ibdev );
  692. /* Remove from device list */
  693. list_del ( &ibdev->list );
  694. ibdev_put ( ibdev );
  695. DBGC ( ibdev, "IBDEV %p unregistered\n", ibdev );
  696. }