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.

ib_cmrc.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <ipxe/iobuf.h>
  35. #include <ipxe/xfer.h>
  36. #include <ipxe/process.h>
  37. #include <ipxe/infiniband.h>
  38. #include <ipxe/ib_cm.h>
  39. #include <ipxe/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 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. union ib_gid dgid;
  77. /** Service ID */
  78. union ib_guid 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 cmrc Communication-Managed Reliable Connection
  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 ib_cmrc_connection *cmrc ) {
  103. DBGC ( cmrc, "CMRC %p shutting down\n", cmrc );
  104. /* Shut down Infiniband interface */
  105. ib_destroy_conn ( cmrc->ibdev, cmrc->qp, cmrc->conn );
  106. ib_destroy_qp ( cmrc->ibdev, cmrc->qp );
  107. ib_destroy_cq ( cmrc->ibdev, cmrc->cq );
  108. ib_close ( cmrc->ibdev );
  109. /* Cancel any pending shutdown */
  110. process_del ( &cmrc->shutdown );
  111. /* Drop the remaining reference */
  112. ref_put ( &cmrc->refcnt );
  113. }
  114. /**
  115. * Close CMRC connection
  116. *
  117. * @v cmrc Communication-Managed Reliable Connection
  118. * @v rc Reason for close
  119. */
  120. static void ib_cmrc_close ( struct ib_cmrc_connection *cmrc, int rc ) {
  121. /* Close data transfer interface */
  122. intf_shutdown ( &cmrc->xfer, rc );
  123. /* Schedule shutdown process */
  124. process_add ( &cmrc->shutdown );
  125. }
  126. /**
  127. * Handle change of CMRC connection status
  128. *
  129. * @v ibdev Infiniband device
  130. * @v qp Queue pair
  131. * @v conn Connection
  132. * @v rc_cm Connection status code
  133. * @v private_data Private data, if available
  134. * @v private_data_len Length of private data
  135. */
  136. static void ib_cmrc_changed ( struct ib_device *ibdev __unused,
  137. struct ib_queue_pair *qp,
  138. struct ib_connection *conn __unused, int rc_cm,
  139. void *private_data, size_t private_data_len ) {
  140. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  141. int rc_xfer;
  142. /* Record connection status */
  143. if ( rc_cm == 0 ) {
  144. DBGC ( cmrc, "CMRC %p connected\n", cmrc );
  145. cmrc->connected = 1;
  146. } else {
  147. DBGC ( cmrc, "CMRC %p disconnected: %s\n",
  148. cmrc, strerror ( rc_cm ) );
  149. cmrc->connected = 0;
  150. }
  151. /* Pass up any private data */
  152. DBGC2 ( cmrc, "CMRC %p received private data:\n", cmrc );
  153. DBGC2_HDA ( cmrc, 0, private_data, private_data_len );
  154. if ( private_data &&
  155. ( rc_xfer = xfer_deliver_raw ( &cmrc->xfer, private_data,
  156. private_data_len ) ) != 0 ) {
  157. DBGC ( cmrc, "CMRC %p could not deliver private data: %s\n",
  158. cmrc, strerror ( rc_xfer ) );
  159. ib_cmrc_close ( cmrc, rc_xfer );
  160. return;
  161. }
  162. /* Notify upper connection of window change */
  163. xfer_window_changed ( &cmrc->xfer );
  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 dest Destination address vector, or NULL
  202. * @v source Source address vector, or NULL
  203. * @v iobuf I/O buffer
  204. * @v rc Completion status code
  205. */
  206. static void ib_cmrc_complete_recv ( struct ib_device *ibdev __unused,
  207. struct ib_queue_pair *qp,
  208. struct ib_address_vector *dest __unused,
  209. struct ib_address_vector *source __unused,
  210. struct io_buffer *iobuf, int rc ) {
  211. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  212. /* Close the connection on any receive errors */
  213. if ( rc != 0 ) {
  214. DBGC ( cmrc, "CMRC %p receive error: %s\n",
  215. cmrc, strerror ( rc ) );
  216. free_iob ( iobuf );
  217. ib_cmrc_close ( cmrc, rc );
  218. return;
  219. }
  220. DBGC2 ( cmrc, "CMRC %p received:\n", cmrc );
  221. DBGC2_HDA ( cmrc, 0, iobuf->data, iob_len ( iobuf ) );
  222. /* Pass up data */
  223. if ( ( rc = xfer_deliver_iob ( &cmrc->xfer, iobuf ) ) != 0 ) {
  224. DBGC ( cmrc, "CMRC %p could not deliver data: %s\n",
  225. cmrc, strerror ( rc ) );
  226. ib_cmrc_close ( cmrc, rc );
  227. return;
  228. }
  229. }
  230. /** Infiniband CMRC completion operations */
  231. static struct ib_completion_queue_operations ib_cmrc_completion_ops = {
  232. .complete_send = ib_cmrc_complete_send,
  233. .complete_recv = ib_cmrc_complete_recv,
  234. };
  235. /** Infiniband CMRC queue pair operations */
  236. static struct ib_queue_pair_operations ib_cmrc_queue_pair_ops = {
  237. .alloc_iob = alloc_iob,
  238. };
  239. /**
  240. * Send data via CMRC
  241. *
  242. * @v cmrc CMRC connection
  243. * @v iobuf Datagram I/O buffer
  244. * @v meta Data transfer metadata
  245. * @ret rc Return status code
  246. */
  247. static int ib_cmrc_xfer_deliver ( struct ib_cmrc_connection *cmrc,
  248. struct io_buffer *iobuf,
  249. struct xfer_metadata *meta __unused ) {
  250. int rc;
  251. /* If no connection has yet been attempted, send this datagram
  252. * as the CM REQ private data. Otherwise, send it via the QP.
  253. */
  254. if ( ! cmrc->connected ) {
  255. /* Abort if we have already sent a CM connection request */
  256. if ( cmrc->conn ) {
  257. DBGC ( cmrc, "CMRC %p attempt to send before "
  258. "connection is complete\n", cmrc );
  259. rc = -EIO;
  260. goto out;
  261. }
  262. /* Send via CM connection request */
  263. cmrc->conn = ib_create_conn ( cmrc->ibdev, cmrc->qp,
  264. &cmrc->dgid, &cmrc->service_id,
  265. iobuf->data, iob_len ( iobuf ),
  266. &ib_cmrc_conn_op );
  267. if ( ! cmrc->conn ) {
  268. DBGC ( cmrc, "CMRC %p could not connect\n", cmrc );
  269. rc = -ENOMEM;
  270. goto out;
  271. }
  272. } else {
  273. /* Send via QP */
  274. if ( ( rc = ib_post_send ( cmrc->ibdev, cmrc->qp, NULL,
  275. iob_disown ( iobuf ) ) ) != 0 ) {
  276. DBGC ( cmrc, "CMRC %p could not send: %s\n",
  277. cmrc, strerror ( rc ) );
  278. goto out;
  279. }
  280. }
  281. return 0;
  282. out:
  283. /* Free the I/O buffer if necessary */
  284. free_iob ( iobuf );
  285. /* Close the connection on any errors */
  286. if ( rc != 0 )
  287. ib_cmrc_close ( cmrc, rc );
  288. return rc;
  289. }
  290. /**
  291. * Check CMRC flow control window
  292. *
  293. * @v cmrc CMRC connection
  294. * @ret len Length of window
  295. */
  296. static size_t ib_cmrc_xfer_window ( struct ib_cmrc_connection *cmrc ) {
  297. /* We indicate a window only when we are successfully
  298. * connected.
  299. */
  300. return ( cmrc->connected ? IB_MAX_PAYLOAD_SIZE : 0 );
  301. }
  302. /**
  303. * Identify device underlying CMRC connection
  304. *
  305. * @v cmrc CMRC connection
  306. * @ret device Underlying device
  307. */
  308. static struct device *
  309. ib_cmrc_identify_device ( struct ib_cmrc_connection *cmrc ) {
  310. return cmrc->ibdev->dev;
  311. }
  312. /** CMRC data transfer interface operations */
  313. static struct interface_operation ib_cmrc_xfer_operations[] = {
  314. INTF_OP ( xfer_deliver, struct ib_cmrc_connection *,
  315. ib_cmrc_xfer_deliver ),
  316. INTF_OP ( xfer_window, struct ib_cmrc_connection *,
  317. ib_cmrc_xfer_window ),
  318. INTF_OP ( intf_close, struct ib_cmrc_connection *, ib_cmrc_close ),
  319. INTF_OP ( identify_device, struct ib_cmrc_connection *,
  320. ib_cmrc_identify_device ),
  321. };
  322. /** CMRC data transfer interface descriptor */
  323. static struct interface_descriptor ib_cmrc_xfer_desc =
  324. INTF_DESC ( struct ib_cmrc_connection, xfer, ib_cmrc_xfer_operations );
  325. /** CMRC shutdown process descriptor */
  326. static struct process_descriptor ib_cmrc_shutdown_desc =
  327. PROC_DESC_ONCE ( struct ib_cmrc_connection, shutdown,
  328. ib_cmrc_shutdown );
  329. /**
  330. * Open CMRC connection
  331. *
  332. * @v xfer Data transfer interface
  333. * @v ibdev Infiniband device
  334. * @v dgid Destination GID
  335. * @v service_id Service ID
  336. * @ret rc Returns status code
  337. */
  338. int ib_cmrc_open ( struct interface *xfer, struct ib_device *ibdev,
  339. union ib_gid *dgid, union ib_guid *service_id ) {
  340. struct ib_cmrc_connection *cmrc;
  341. int rc;
  342. /* Allocate and initialise structure */
  343. cmrc = zalloc ( sizeof ( *cmrc ) );
  344. if ( ! cmrc ) {
  345. rc = -ENOMEM;
  346. goto err_alloc;
  347. }
  348. ref_init ( &cmrc->refcnt, NULL );
  349. intf_init ( &cmrc->xfer, &ib_cmrc_xfer_desc, &cmrc->refcnt );
  350. cmrc->ibdev = ibdev;
  351. memcpy ( &cmrc->dgid, dgid, sizeof ( cmrc->dgid ) );
  352. memcpy ( &cmrc->service_id, service_id, sizeof ( cmrc->service_id ) );
  353. process_init_stopped ( &cmrc->shutdown, &ib_cmrc_shutdown_desc,
  354. &cmrc->refcnt );
  355. /* Open Infiniband device */
  356. if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
  357. DBGC ( cmrc, "CMRC %p could not open device: %s\n",
  358. cmrc, strerror ( rc ) );
  359. goto err_open;
  360. }
  361. /* Create completion queue */
  362. cmrc->cq = ib_create_cq ( ibdev, IB_CMRC_NUM_CQES,
  363. &ib_cmrc_completion_ops );
  364. if ( ! cmrc->cq ) {
  365. DBGC ( cmrc, "CMRC %p could not create completion queue\n",
  366. cmrc );
  367. rc = -ENOMEM;
  368. goto err_create_cq;
  369. }
  370. /* Create queue pair */
  371. cmrc->qp = ib_create_qp ( ibdev, IB_QPT_RC, IB_CMRC_NUM_SEND_WQES,
  372. cmrc->cq, IB_CMRC_NUM_RECV_WQES, cmrc->cq,
  373. &ib_cmrc_queue_pair_ops );
  374. if ( ! cmrc->qp ) {
  375. DBGC ( cmrc, "CMRC %p could not create queue pair\n", cmrc );
  376. rc = -ENOMEM;
  377. goto err_create_qp;
  378. }
  379. ib_qp_set_ownerdata ( cmrc->qp, cmrc );
  380. DBGC ( cmrc, "CMRC %p using QPN %#lx\n", cmrc, cmrc->qp->qpn );
  381. /* Attach to parent interface, transfer reference (implicitly)
  382. * to our shutdown process, and return.
  383. */
  384. intf_plug_plug ( &cmrc->xfer, xfer );
  385. return 0;
  386. ib_destroy_qp ( ibdev, cmrc->qp );
  387. err_create_qp:
  388. ib_destroy_cq ( ibdev, cmrc->cq );
  389. err_create_cq:
  390. ib_close ( ibdev );
  391. err_open:
  392. ref_put ( &cmrc->refcnt );
  393. err_alloc:
  394. return rc;
  395. }