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

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