PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/net/infiniband/ib_cmrc.c

https://gitlab.com/waldi/ipxe
C | 464 lines | 233 code | 53 blank | 178 comment | 26 complexity | b7411629ce5bfcf02379feb6a5ae8cac MD5 | raw file
  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. /** Name */
  66. const char *name;
  67. /** Data transfer interface */
  68. struct interface xfer;
  69. /** Infiniband device */
  70. struct ib_device *ibdev;
  71. /** Completion queue */
  72. struct ib_completion_queue *cq;
  73. /** Queue pair */
  74. struct ib_queue_pair *qp;
  75. /** Connection */
  76. struct ib_connection *conn;
  77. /** Destination GID */
  78. union ib_gid dgid;
  79. /** Service ID */
  80. union ib_guid service_id;
  81. /** QP is connected */
  82. int connected;
  83. /** Shutdown process */
  84. struct process shutdown;
  85. };
  86. /**
  87. * Shut down CMRC connection gracefully
  88. *
  89. * @v cmrc Communication-Managed Reliable Connection
  90. *
  91. * The Infiniband data structures are not reference-counted or
  92. * guarded. It is therefore unsafe to shut them down while we may be
  93. * in the middle of a callback from the Infiniband stack (e.g. in a
  94. * receive completion handler).
  95. *
  96. * This shutdown process will run some time after the call to
  97. * ib_cmrc_close(), after control has returned out of the Infiniband
  98. * core, and will shut down the Infiniband interfaces cleanly.
  99. *
  100. * The shutdown process holds an implicit reference on the CMRC
  101. * connection, ensuring that the structure is not freed before the
  102. * shutdown process has run.
  103. */
  104. static void ib_cmrc_shutdown ( struct ib_cmrc_connection *cmrc ) {
  105. struct ib_device *ibdev = cmrc->ibdev;
  106. DBGC ( cmrc, "CMRC %s %s shutting down\n",
  107. ibdev->name, cmrc->name );
  108. /* Shut down Infiniband interface */
  109. ib_destroy_conn ( ibdev, cmrc->qp, cmrc->conn );
  110. ib_destroy_qp ( ibdev, cmrc->qp );
  111. ib_destroy_cq ( ibdev, cmrc->cq );
  112. ib_close ( ibdev );
  113. /* Cancel any pending shutdown */
  114. process_del ( &cmrc->shutdown );
  115. /* Drop the remaining reference */
  116. ref_put ( &cmrc->refcnt );
  117. }
  118. /**
  119. * Close CMRC connection
  120. *
  121. * @v cmrc Communication-Managed Reliable Connection
  122. * @v rc Reason for close
  123. */
  124. static void ib_cmrc_close ( struct ib_cmrc_connection *cmrc, int rc ) {
  125. /* Close data transfer interface */
  126. intf_shutdown ( &cmrc->xfer, rc );
  127. /* Schedule shutdown process */
  128. process_add ( &cmrc->shutdown );
  129. }
  130. /**
  131. * Handle change of CMRC connection status
  132. *
  133. * @v ibdev Infiniband device
  134. * @v qp Queue pair
  135. * @v conn Connection
  136. * @v rc_cm Connection status code
  137. * @v private_data Private data, if available
  138. * @v private_data_len Length of private data
  139. */
  140. static void ib_cmrc_changed ( struct ib_device *ibdev,
  141. struct ib_queue_pair *qp,
  142. struct ib_connection *conn __unused, int rc_cm,
  143. void *private_data, size_t private_data_len ) {
  144. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  145. int rc_xfer;
  146. /* Record connection status */
  147. if ( rc_cm == 0 ) {
  148. DBGC ( cmrc, "CMRC %s %s connected\n",
  149. ibdev->name, cmrc->name );
  150. cmrc->connected = 1;
  151. } else {
  152. DBGC ( cmrc, "CMRC %s %s disconnected: %s\n",
  153. ibdev->name, cmrc->name, strerror ( rc_cm ) );
  154. cmrc->connected = 0;
  155. }
  156. /* Pass up any private data */
  157. DBGC2 ( cmrc, "CMRC %s %s received private data:\n",
  158. ibdev->name, cmrc->name );
  159. DBGC2_HDA ( cmrc, 0, private_data, private_data_len );
  160. if ( private_data &&
  161. ( rc_xfer = xfer_deliver_raw ( &cmrc->xfer, private_data,
  162. private_data_len ) ) != 0 ) {
  163. DBGC ( cmrc, "CMRC %s %s could not deliver private data: %s\n",
  164. ibdev->name, cmrc->name, strerror ( rc_xfer ) );
  165. ib_cmrc_close ( cmrc, rc_xfer );
  166. return;
  167. }
  168. /* Notify upper connection of window change */
  169. xfer_window_changed ( &cmrc->xfer );
  170. /* If we are disconnected, close the upper connection */
  171. if ( rc_cm != 0 ) {
  172. ib_cmrc_close ( cmrc, rc_cm );
  173. return;
  174. }
  175. }
  176. /** CMRC connection operations */
  177. static struct ib_connection_operations ib_cmrc_conn_op = {
  178. .changed = ib_cmrc_changed,
  179. };
  180. /**
  181. * Handle CMRC send completion
  182. *
  183. * @v ibdev Infiniband device
  184. * @v qp Queue pair
  185. * @v iobuf I/O buffer
  186. * @v rc Completion status code
  187. */
  188. static void ib_cmrc_complete_send ( struct ib_device *ibdev,
  189. struct ib_queue_pair *qp,
  190. struct io_buffer *iobuf, int rc ) {
  191. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  192. /* Free the completed I/O buffer */
  193. free_iob ( iobuf );
  194. /* Close the connection on any send errors */
  195. if ( rc != 0 ) {
  196. DBGC ( cmrc, "CMRC %s %s send error: %s\n",
  197. ibdev->name, cmrc->name, strerror ( rc ) );
  198. ib_cmrc_close ( cmrc, rc );
  199. return;
  200. }
  201. }
  202. /**
  203. * Handle CMRC receive completion
  204. *
  205. * @v ibdev Infiniband device
  206. * @v qp Queue pair
  207. * @v dest Destination address vector, or NULL
  208. * @v source Source address vector, or NULL
  209. * @v iobuf I/O buffer
  210. * @v rc Completion status code
  211. */
  212. static void ib_cmrc_complete_recv ( struct ib_device *ibdev,
  213. struct ib_queue_pair *qp,
  214. struct ib_address_vector *dest __unused,
  215. struct ib_address_vector *source __unused,
  216. struct io_buffer *iobuf, int rc ) {
  217. struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
  218. /* Close the connection on any receive errors */
  219. if ( rc != 0 ) {
  220. DBGC ( cmrc, "CMRC %s %s receive error: %s\n",
  221. ibdev->name, cmrc->name, strerror ( rc ) );
  222. free_iob ( iobuf );
  223. ib_cmrc_close ( cmrc, rc );
  224. return;
  225. }
  226. DBGC2 ( cmrc, "CMRC %s %s received:\n", ibdev->name, cmrc->name );
  227. DBGC2_HDA ( cmrc, 0, iobuf->data, iob_len ( iobuf ) );
  228. /* Pass up data */
  229. if ( ( rc = xfer_deliver_iob ( &cmrc->xfer, iobuf ) ) != 0 ) {
  230. DBGC ( cmrc, "CMRC %s %s could not deliver data: %s\n",
  231. ibdev->name, cmrc->name, strerror ( rc ) );
  232. ib_cmrc_close ( cmrc, rc );
  233. return;
  234. }
  235. }
  236. /** Infiniband CMRC completion operations */
  237. static struct ib_completion_queue_operations ib_cmrc_completion_ops = {
  238. .complete_send = ib_cmrc_complete_send,
  239. .complete_recv = ib_cmrc_complete_recv,
  240. };
  241. /** Infiniband CMRC queue pair operations */
  242. static struct ib_queue_pair_operations ib_cmrc_queue_pair_ops = {
  243. .alloc_iob = alloc_iob,
  244. };
  245. /**
  246. * Send data via CMRC
  247. *
  248. * @v cmrc CMRC connection
  249. * @v iobuf Datagram I/O buffer
  250. * @v meta Data transfer metadata
  251. * @ret rc Return status code
  252. */
  253. static int ib_cmrc_xfer_deliver ( struct ib_cmrc_connection *cmrc,
  254. struct io_buffer *iobuf,
  255. struct xfer_metadata *meta __unused ) {
  256. struct ib_device *ibdev = cmrc->ibdev;
  257. int rc;
  258. /* If no connection has yet been attempted, send this datagram
  259. * as the CM REQ private data. Otherwise, send it via the QP.
  260. */
  261. if ( ! cmrc->connected ) {
  262. /* Abort if we have already sent a CM connection request */
  263. if ( cmrc->conn ) {
  264. DBGC ( cmrc, "CMRC %s %s attempt to send before "
  265. "connection is complete\n",
  266. ibdev->name, cmrc->name );
  267. rc = -EIO;
  268. goto out;
  269. }
  270. /* Send via CM connection request */
  271. cmrc->conn = ib_create_conn ( cmrc->ibdev, cmrc->qp,
  272. &cmrc->dgid, &cmrc->service_id,
  273. iobuf->data, iob_len ( iobuf ),
  274. &ib_cmrc_conn_op );
  275. if ( ! cmrc->conn ) {
  276. DBGC ( cmrc, "CMRC %s %s could not connect\n",
  277. ibdev->name, cmrc->name );
  278. rc = -ENOMEM;
  279. goto out;
  280. }
  281. DBGC ( cmrc, "CMRC %s %s using CM %08x\n",
  282. ibdev->name, cmrc->name, cmrc->conn->local_id );
  283. } else {
  284. /* Send via QP */
  285. if ( ( rc = ib_post_send ( cmrc->ibdev, cmrc->qp, NULL,
  286. iob_disown ( iobuf ) ) ) != 0 ) {
  287. DBGC ( cmrc, "CMRC %s %s could not send: %s\n",
  288. ibdev->name, cmrc->name, strerror ( rc ) );
  289. goto out;
  290. }
  291. }
  292. return 0;
  293. out:
  294. /* Free the I/O buffer if necessary */
  295. free_iob ( iobuf );
  296. /* Close the connection on any errors */
  297. if ( rc != 0 )
  298. ib_cmrc_close ( cmrc, rc );
  299. return rc;
  300. }
  301. /**
  302. * Check CMRC flow control window
  303. *
  304. * @v cmrc CMRC connection
  305. * @ret len Length of window
  306. */
  307. static size_t ib_cmrc_xfer_window ( struct ib_cmrc_connection *cmrc ) {
  308. /* We indicate a window only when we are successfully
  309. * connected.
  310. */
  311. return ( cmrc->connected ? IB_MAX_PAYLOAD_SIZE : 0 );
  312. }
  313. /**
  314. * Identify device underlying CMRC connection
  315. *
  316. * @v cmrc CMRC connection
  317. * @ret device Underlying device
  318. */
  319. static struct device *
  320. ib_cmrc_identify_device ( struct ib_cmrc_connection *cmrc ) {
  321. return cmrc->ibdev->dev;
  322. }
  323. /** CMRC data transfer interface operations */
  324. static struct interface_operation ib_cmrc_xfer_operations[] = {
  325. INTF_OP ( xfer_deliver, struct ib_cmrc_connection *,
  326. ib_cmrc_xfer_deliver ),
  327. INTF_OP ( xfer_window, struct ib_cmrc_connection *,
  328. ib_cmrc_xfer_window ),
  329. INTF_OP ( intf_close, struct ib_cmrc_connection *, ib_cmrc_close ),
  330. INTF_OP ( identify_device, struct ib_cmrc_connection *,
  331. ib_cmrc_identify_device ),
  332. };
  333. /** CMRC data transfer interface descriptor */
  334. static struct interface_descriptor ib_cmrc_xfer_desc =
  335. INTF_DESC ( struct ib_cmrc_connection, xfer, ib_cmrc_xfer_operations );
  336. /** CMRC shutdown process descriptor */
  337. static struct process_descriptor ib_cmrc_shutdown_desc =
  338. PROC_DESC_ONCE ( struct ib_cmrc_connection, shutdown,
  339. ib_cmrc_shutdown );
  340. /**
  341. * Open CMRC connection
  342. *
  343. * @v xfer Data transfer interface
  344. * @v ibdev Infiniband device
  345. * @v dgid Destination GID
  346. * @v service_id Service ID
  347. * @v name Connection name
  348. * @ret rc Returns status code
  349. */
  350. int ib_cmrc_open ( struct interface *xfer, struct ib_device *ibdev,
  351. union ib_gid *dgid, union ib_guid *service_id,
  352. const char *name ) {
  353. struct ib_cmrc_connection *cmrc;
  354. int rc;
  355. /* Allocate and initialise structure */
  356. cmrc = zalloc ( sizeof ( *cmrc ) );
  357. if ( ! cmrc ) {
  358. rc = -ENOMEM;
  359. goto err_alloc;
  360. }
  361. ref_init ( &cmrc->refcnt, NULL );
  362. cmrc->name = name;
  363. intf_init ( &cmrc->xfer, &ib_cmrc_xfer_desc, &cmrc->refcnt );
  364. cmrc->ibdev = ibdev;
  365. memcpy ( &cmrc->dgid, dgid, sizeof ( cmrc->dgid ) );
  366. memcpy ( &cmrc->service_id, service_id, sizeof ( cmrc->service_id ) );
  367. process_init_stopped ( &cmrc->shutdown, &ib_cmrc_shutdown_desc,
  368. &cmrc->refcnt );
  369. /* Open Infiniband device */
  370. if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
  371. DBGC ( cmrc, "CMRC %s %s could not open device: %s\n",
  372. ibdev->name, cmrc->name, strerror ( rc ) );
  373. goto err_open;
  374. }
  375. /* Create completion queue */
  376. cmrc->cq = ib_create_cq ( ibdev, IB_CMRC_NUM_CQES,
  377. &ib_cmrc_completion_ops );
  378. if ( ! cmrc->cq ) {
  379. DBGC ( cmrc, "CMRC %s %s could not create completion queue\n",
  380. ibdev->name, cmrc->name );
  381. rc = -ENOMEM;
  382. goto err_create_cq;
  383. }
  384. /* Create queue pair */
  385. cmrc->qp = ib_create_qp ( ibdev, IB_QPT_RC, IB_CMRC_NUM_SEND_WQES,
  386. cmrc->cq, IB_CMRC_NUM_RECV_WQES, cmrc->cq,
  387. &ib_cmrc_queue_pair_ops, name );
  388. if ( ! cmrc->qp ) {
  389. DBGC ( cmrc, "CMRC %s %s could not create queue pair\n",
  390. ibdev->name, cmrc->name );
  391. rc = -ENOMEM;
  392. goto err_create_qp;
  393. }
  394. ib_qp_set_ownerdata ( cmrc->qp, cmrc );
  395. DBGC ( cmrc, "CMRC %s %s using QPN %#lx\n",
  396. ibdev->name, cmrc->name, cmrc->qp->qpn );
  397. /* Attach to parent interface, transfer reference (implicitly)
  398. * to our shutdown process, and return.
  399. */
  400. intf_plug_plug ( &cmrc->xfer, xfer );
  401. return 0;
  402. ib_destroy_qp ( ibdev, cmrc->qp );
  403. err_create_qp:
  404. ib_destroy_cq ( ibdev, cmrc->cq );
  405. err_create_cq:
  406. ib_close ( ibdev );
  407. err_open:
  408. ref_put ( &cmrc->refcnt );
  409. err_alloc:
  410. return rc;
  411. }