Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

infiniband.c 19KB

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