選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ib_cmrc.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (C) 2009 Fen Systems Ltd <mbrown@fensystems.co.uk>.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  28. * OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. FILE_LICENCE ( BSD2 );
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <gpxe/iobuf.h>
  35. #include <gpxe/xfer.h>
  36. #include <gpxe/process.h>
  37. #include <gpxe/infiniband.h>
  38. #include <gpxe/ib_cm.h>
  39. #include <gpxe/ib_cmrc.h>
  40. /**
  41. * @file
  42. *
  43. * Infiniband Communication-managed Reliable Connections
  44. *
  45. */
  46. /** CMRC number of send WQEs
  47. *
  48. * This is a policy decision.
  49. */
  50. #define IB_CMRC_NUM_SEND_WQES 4
  51. /** CMRC number of receive WQEs
  52. *
  53. * This is a policy decision.
  54. */
  55. #define IB_CMRC_NUM_RECV_WQES 2
  56. /** CMRC number of completion queue entries
  57. *
  58. * This is a policy decision
  59. */
  60. #define IB_CMRC_NUM_CQES 8
  61. /** An Infiniband Communication-Managed Reliable Connection */
  62. struct ib_cmrc_connection {
  63. /** Reference count */
  64. struct refcnt refcnt;
  65. /** Data transfer interface */
  66. struct xfer_interface xfer;
  67. /** Infiniband device */
  68. struct ib_device *ibdev;
  69. /** Completion queue */
  70. struct ib_completion_queue *cq;
  71. /** Queue pair */
  72. struct ib_queue_pair *qp;
  73. /** Connection */
  74. struct ib_connection *conn;
  75. /** Destination GID */
  76. struct ib_gid dgid;
  77. /** Service ID */
  78. struct ib_gid_half service_id;
  79. /** QP is connected */
  80. int connected;
  81. /** Shutdown process */
  82. struct process shutdown;
  83. };
  84. /**
  85. * Shut down CMRC connection gracefully
  86. *
  87. * @v process Process
  88. *
  89. * The Infiniband data structures are not reference-counted or
  90. * guarded. It is therefore unsafe to shut them down while we may be
  91. * in the middle of a callback from the Infiniband stack (e.g. in a
  92. * receive completion handler).
  93. *
  94. * This shutdown process will run some time after the call to
  95. * ib_cmrc_close(), after control has returned out of the Infiniband
  96. * core, and will shut down the Infiniband interfaces cleanly.
  97. *
  98. * The shutdown process holds an implicit reference on the CMRC
  99. * connection, ensuring that the structure is not freed before the
  100. * shutdown process has run.
  101. */
  102. static void ib_cmrc_shutdown ( struct process *process ) {
  103. struct ib_cmrc_connection *cmrc =
  104. container_of ( process, struct ib_cmrc_connection, shutdown );
  105. DBGC ( cmrc, "CMRC %p shutting down\n", cmrc );
  106. /* Shut down Infiniband interface */
  107. ib_destroy_conn ( cmrc->ibdev, cmrc->qp, cmrc->conn );
  108. ib_destroy_qp ( cmrc->ibdev, cmrc->qp );
  109. ib_destroy_cq ( cmrc->ibdev, cmrc->cq );
  110. ib_close ( cmrc->ibdev );
  111. /* Remove process from run queue */
  112. process_del ( &cmrc->shutdown );
  113. /* Drop the remaining reference */
  114. ref_put ( &cmrc->refcnt );
  115. }
  116. /**
  117. * Close CMRC connection
  118. *
  119. * @v cmrc Communication-Managed Reliable Connection
  120. * @v rc Reason for close
  121. */
  122. static void ib_cmrc_close ( struct ib_cmrc_connection *cmrc, int rc ) {
  123. /* Close data transfer interface */
  124. xfer_nullify ( &cmrc->xfer );
  125. xfer_close ( &cmrc->xfer, rc );
  126. /* Schedule shutdown process */
  127. process_add ( &cmrc->shutdown );
  128. }
  129. /**
  130. * Handle change of CMRC connection status
  131. *
  132. * @v ibdev Infiniband device
  133. * @v qp Queue pair
  134. * @v conn Connection
  135. * @v rc_cm Connection status code
  136. * @v private_data Private data, if available
  137. * @v private_data_len Length of private data
  138. */
  139. static void ib_cmrc_changed ( struct ib_device *ibdev __unused,
  140. struct ib_queue_pair *qp,
  141. struct ib_connection *conn __unused, int rc_cm,
  142. void *private_data, size_t private_data_len ) {
  143. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  144. int rc_xfer;
  145. /* Record connection status */
  146. if ( rc_cm == 0 ) {
  147. DBGC ( cmrc, "CMRC %p connected\n", cmrc );
  148. cmrc->connected = 1;
  149. } else {
  150. DBGC ( cmrc, "CMRC %p disconnected: %s\n",
  151. cmrc, strerror ( rc_cm ) );
  152. cmrc->connected = 0;
  153. }
  154. /* Pass up any private data */
  155. DBGC2 ( cmrc, "CMRC %p received private data:\n", cmrc );
  156. DBGC2_HDA ( cmrc, 0, private_data, private_data_len );
  157. if ( ( rc_xfer = xfer_deliver_raw ( &cmrc->xfer, private_data,
  158. private_data_len ) ) != 0 ) {
  159. DBGC ( cmrc, "CMRC %p could not deliver private data: %s\n",
  160. cmrc, strerror ( rc_xfer ) );
  161. ib_cmrc_close ( cmrc, rc_xfer );
  162. return;
  163. }
  164. /* If we are disconnected, close the upper connection */
  165. if ( rc_cm != 0 ) {
  166. ib_cmrc_close ( cmrc, rc_cm );
  167. return;
  168. }
  169. }
  170. /** CMRC connection operations */
  171. static struct ib_connection_operations ib_cmrc_conn_op = {
  172. .changed = ib_cmrc_changed,
  173. };
  174. /**
  175. * Handle CMRC send completion
  176. *
  177. * @v ibdev Infiniband device
  178. * @v qp Queue pair
  179. * @v iobuf I/O buffer
  180. * @v rc Completion status code
  181. */
  182. static void ib_cmrc_complete_send ( struct ib_device *ibdev __unused,
  183. struct ib_queue_pair *qp,
  184. struct io_buffer *iobuf, int rc ) {
  185. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  186. /* Free the completed I/O buffer */
  187. free_iob ( iobuf );
  188. /* Close the connection on any send errors */
  189. if ( rc != 0 ) {
  190. DBGC ( cmrc, "CMRC %p send error: %s\n",
  191. cmrc, strerror ( rc ) );
  192. ib_cmrc_close ( cmrc, rc );
  193. return;
  194. }
  195. }
  196. /**
  197. * Handle CMRC receive completion
  198. *
  199. * @v ibdev Infiniband device
  200. * @v qp Queue pair
  201. * @v av Address vector, or NULL
  202. * @v iobuf I/O buffer
  203. * @v rc Completion status code
  204. */
  205. static void ib_cmrc_complete_recv ( struct ib_device *ibdev __unused,
  206. struct ib_queue_pair *qp,
  207. struct ib_address_vector *av __unused,
  208. struct io_buffer *iobuf, int rc ) {
  209. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  210. /* Close the connection on any receive errors */
  211. if ( rc != 0 ) {
  212. DBGC ( cmrc, "CMRC %p receive error: %s\n",
  213. cmrc, strerror ( rc ) );
  214. free_iob ( iobuf );
  215. ib_cmrc_close ( cmrc, rc );
  216. return;
  217. }
  218. DBGC2 ( cmrc, "CMRC %p received:\n", cmrc );
  219. DBGC2_HDA ( cmrc, 0, iobuf->data, iob_len ( iobuf ) );
  220. /* Pass up data */
  221. if ( ( rc = xfer_deliver_iob ( &cmrc->xfer, iobuf ) ) != 0 ) {
  222. DBGC ( cmrc, "CMRC %p could not deliver data: %s\n",
  223. cmrc, strerror ( rc ) );
  224. ib_cmrc_close ( cmrc, rc );
  225. return;
  226. }
  227. }
  228. /** Infiniband CMRC completion operations */
  229. static struct ib_completion_queue_operations ib_cmrc_completion_ops = {
  230. .complete_send = ib_cmrc_complete_send,
  231. .complete_recv = ib_cmrc_complete_recv,
  232. };
  233. /**
  234. * Send data via CMRC
  235. *
  236. * @v xfer Data transfer interface
  237. * @v iobuf Datagram I/O buffer
  238. * @v meta Data transfer metadata
  239. * @ret rc Return status code
  240. */
  241. static int ib_cmrc_xfer_deliver_iob ( struct xfer_interface *xfer,
  242. struct io_buffer *iobuf,
  243. struct xfer_metadata *meta __unused ) {
  244. struct ib_cmrc_connection *cmrc =
  245. container_of ( xfer, struct ib_cmrc_connection, xfer );
  246. int rc;
  247. /* If no connection has yet been attempted, send this datagram
  248. * as the CM REQ private data. Otherwise, send it via the QP.
  249. */
  250. if ( ! cmrc->connected ) {
  251. /* Abort if we have already sent a CM connection request */
  252. if ( cmrc->conn ) {
  253. DBGC ( cmrc, "CMRC %p attempt to send before "
  254. "connection is complete\n", cmrc );
  255. rc = -EIO;
  256. goto out;
  257. }
  258. /* Send via CM connection request */
  259. cmrc->conn = ib_create_conn ( cmrc->ibdev, cmrc->qp,
  260. &cmrc->dgid, &cmrc->service_id,
  261. iobuf->data, iob_len ( iobuf ),
  262. &ib_cmrc_conn_op );
  263. if ( ! cmrc->conn ) {
  264. DBGC ( cmrc, "CMRC %p could not connect\n", cmrc );
  265. rc = -ENOMEM;
  266. goto out;
  267. }
  268. } else {
  269. /* Send via QP */
  270. if ( ( rc = ib_post_send ( cmrc->ibdev, cmrc->qp, NULL,
  271. iob_disown ( iobuf ) ) ) != 0 ) {
  272. DBGC ( cmrc, "CMRC %p could not send: %s\n",
  273. cmrc, strerror ( rc ) );
  274. goto out;
  275. }
  276. }
  277. return 0;
  278. out:
  279. /* Free the I/O buffer if necessary */
  280. free_iob ( iobuf );
  281. /* Close the connection on any errors */
  282. if ( rc != 0 )
  283. ib_cmrc_close ( cmrc, rc );
  284. return rc;
  285. }
  286. /**
  287. * Check CMRC flow control window
  288. *
  289. * @v xfer Data transfer interface
  290. * @ret len Length of window
  291. */
  292. static size_t ib_cmrc_xfer_window ( struct xfer_interface *xfer ) {
  293. struct ib_cmrc_connection *cmrc =
  294. container_of ( xfer, struct ib_cmrc_connection, xfer );
  295. /* We indicate a window only when we are successfully
  296. * connected.
  297. */
  298. return ( cmrc->connected ? IB_MAX_PAYLOAD_SIZE : 0 );
  299. }
  300. /**
  301. * Close CMRC data-transfer interface
  302. *
  303. * @v xfer Data transfer interface
  304. * @v rc Reason for close
  305. */
  306. static void ib_cmrc_xfer_close ( struct xfer_interface *xfer, int rc ) {
  307. struct ib_cmrc_connection *cmrc =
  308. container_of ( xfer, struct ib_cmrc_connection, xfer );
  309. DBGC ( cmrc, "CMRC %p closed: %s\n", cmrc, strerror ( rc ) );
  310. ib_cmrc_close ( cmrc, rc );
  311. }
  312. /** CMRC data transfer interface operations */
  313. static struct xfer_interface_operations ib_cmrc_xfer_operations = {
  314. .close = ib_cmrc_xfer_close,
  315. .vredirect = ignore_xfer_vredirect,
  316. .window = ib_cmrc_xfer_window,
  317. .alloc_iob = default_xfer_alloc_iob,
  318. .deliver_iob = ib_cmrc_xfer_deliver_iob,
  319. .deliver_raw = xfer_deliver_as_iob,
  320. };
  321. /**
  322. * Open CMRC connection
  323. *
  324. * @v xfer Data transfer interface
  325. * @v ibdev Infiniband device
  326. * @v dgid Destination GID
  327. * @v service_id Service ID
  328. * @ret rc Returns status code
  329. */
  330. int ib_cmrc_open ( struct xfer_interface *xfer, struct ib_device *ibdev,
  331. struct ib_gid *dgid, struct ib_gid_half *service_id ) {
  332. struct ib_cmrc_connection *cmrc;
  333. int rc;
  334. /* Allocate and initialise structure */
  335. cmrc = zalloc ( sizeof ( *cmrc ) );
  336. if ( ! cmrc ) {
  337. rc = -ENOMEM;
  338. goto err_alloc;
  339. }
  340. xfer_init ( &cmrc->xfer, &ib_cmrc_xfer_operations, &cmrc->refcnt );
  341. cmrc->ibdev = ibdev;
  342. memcpy ( &cmrc->dgid, dgid, sizeof ( cmrc->dgid ) );
  343. memcpy ( &cmrc->service_id, service_id, sizeof ( cmrc->service_id ) );
  344. process_init_stopped ( &cmrc->shutdown, ib_cmrc_shutdown,
  345. &cmrc->refcnt );
  346. /* Open Infiniband device */
  347. if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
  348. DBGC ( cmrc, "CMRC %p could not open device: %s\n",
  349. cmrc, strerror ( rc ) );
  350. goto err_open;
  351. }
  352. /* Create completion queue */
  353. cmrc->cq = ib_create_cq ( ibdev, IB_CMRC_NUM_CQES,
  354. &ib_cmrc_completion_ops );
  355. if ( ! cmrc->cq ) {
  356. DBGC ( cmrc, "CMRC %p could not create completion queue\n",
  357. cmrc );
  358. rc = -ENOMEM;
  359. goto err_create_cq;
  360. }
  361. /* Create queue pair */
  362. cmrc->qp = ib_create_qp ( ibdev, IB_QPT_RC, IB_CMRC_NUM_SEND_WQES,
  363. cmrc->cq, IB_CMRC_NUM_RECV_WQES, cmrc->cq );
  364. if ( ! cmrc->qp ) {
  365. DBGC ( cmrc, "CMRC %p could not create queue pair\n", cmrc );
  366. rc = -ENOMEM;
  367. goto err_create_qp;
  368. }
  369. ib_qp_set_ownerdata ( cmrc->qp, cmrc );
  370. DBGC ( cmrc, "CMRC %p using QPN %lx\n", cmrc, cmrc->qp->qpn );
  371. /* Attach to parent interface, transfer reference (implicitly)
  372. * to our shutdown process, and return.
  373. */
  374. xfer_plug_plug ( &cmrc->xfer, xfer );
  375. return 0;
  376. ib_destroy_qp ( ibdev, cmrc->qp );
  377. err_create_qp:
  378. ib_destroy_cq ( ibdev, cmrc->cq );
  379. err_create_cq:
  380. ib_close ( ibdev );
  381. err_open:
  382. ref_put ( &cmrc->refcnt );
  383. err_alloc:
  384. return rc;
  385. }