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

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