2 * Copyright (C) 2009 Fen Systems Ltd <mbrown@fensystems.co.uk>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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.
31 FILE_LICENCE ( BSD2
);
36 #include <gpxe/iobuf.h>
37 #include <gpxe/xfer.h>
38 #include <gpxe/process.h>
39 #include <gpxe/infiniband.h>
40 #include <gpxe/ib_cm.h>
41 #include <gpxe/ib_cmrc.h>
46 * Infiniband Communication-managed Reliable Connections
50 /** CMRC number of send WQEs
52 * This is a policy decision.
54 #define IB_CMRC_NUM_SEND_WQES 4
56 /** CMRC number of receive WQEs
58 * This is a policy decision.
60 #define IB_CMRC_NUM_RECV_WQES 2
62 /** CMRC number of completion queue entries
64 * This is a policy decision
66 #define IB_CMRC_NUM_CQES 8
68 /** An Infiniband Communication-Managed Reliable Connection */
69 struct ib_cmrc_connection
{
70 /** Reference count */
72 /** Data transfer interface */
73 struct xfer_interface xfer
;
74 /** Infiniband device */
75 struct ib_device
*ibdev
;
76 /** Completion queue */
77 struct ib_completion_queue
*cq
;
79 struct ib_queue_pair
*qp
;
81 struct ib_connection
*conn
;
82 /** Destination GID */
85 struct ib_gid_half service_id
;
86 /** QP is connected */
88 /** Shutdown process */
89 struct process shutdown
;
93 * Shut down CMRC connection gracefully
97 * The Infiniband data structures are not reference-counted or
98 * guarded. It is therefore unsafe to shut them down while we may be
99 * in the middle of a callback from the Infiniband stack (e.g. in a
100 * receive completion handler).
102 * This shutdown process will run some time after the call to
103 * ib_cmrc_close(), after control has returned out of the Infiniband
104 * core, and will shut down the Infiniband interfaces cleanly.
106 * The shutdown process holds an implicit reference on the CMRC
107 * connection, ensuring that the structure is not freed before the
108 * shutdown process has run.
110 static void ib_cmrc_shutdown ( struct process
*process
) {
111 struct ib_cmrc_connection
*cmrc
=
112 container_of ( process
, struct ib_cmrc_connection
, shutdown
);
114 DBGC ( cmrc
, "CMRC %p shutting down\n", cmrc
);
116 /* Shut down Infiniband interface */
117 ib_destroy_conn ( cmrc
->ibdev
, cmrc
->qp
, cmrc
->conn
);
118 ib_destroy_qp ( cmrc
->ibdev
, cmrc
->qp
);
119 ib_destroy_cq ( cmrc
->ibdev
, cmrc
->cq
);
120 ib_close ( cmrc
->ibdev
);
122 /* Remove process from run queue */
123 process_del ( &cmrc
->shutdown
);
125 /* Drop the remaining reference */
126 ref_put ( &cmrc
->refcnt
);
130 * Close CMRC connection
132 * @v cmrc Communication-Managed Reliable Connection
133 * @v rc Reason for close
135 static void ib_cmrc_close ( struct ib_cmrc_connection
*cmrc
, int rc
) {
137 /* Close data transfer interface */
138 xfer_nullify ( &cmrc
->xfer
);
139 xfer_close ( &cmrc
->xfer
, rc
);
141 /* Schedule shutdown process */
142 process_add ( &cmrc
->shutdown
);
146 * Handle change of CMRC connection status
148 * @v ibdev Infiniband device
151 * @v rc_cm Connection status code
152 * @v private_data Private data, if available
153 * @v private_data_len Length of private data
155 static void ib_cmrc_changed ( struct ib_device
*ibdev __unused
,
156 struct ib_queue_pair
*qp
,
157 struct ib_connection
*conn __unused
, int rc_cm
,
158 void *private_data
, size_t private_data_len
) {
159 struct ib_cmrc_connection
*cmrc
= ib_qp_get_ownerdata ( qp
);
162 /* Record connection status */
164 DBGC ( cmrc
, "CMRC %p connected\n", cmrc
);
167 DBGC ( cmrc
, "CMRC %p disconnected: %s\n",
168 cmrc
, strerror ( rc_cm
) );
172 /* Pass up any private data */
173 DBGC2 ( cmrc
, "CMRC %p received private data:\n", cmrc
);
174 DBGC2_HDA ( cmrc
, 0, private_data
, private_data_len
);
176 ( rc_xfer
= xfer_deliver_raw ( &cmrc
->xfer
, private_data
,
177 private_data_len
) ) != 0 ) {
178 DBGC ( cmrc
, "CMRC %p could not deliver private data: %s\n",
179 cmrc
, strerror ( rc_xfer
) );
180 ib_cmrc_close ( cmrc
, rc_xfer
);
184 /* If we are disconnected, close the upper connection */
186 ib_cmrc_close ( cmrc
, rc_cm
);
191 /** CMRC connection operations */
192 static struct ib_connection_operations ib_cmrc_conn_op
= {
193 .changed
= ib_cmrc_changed
,
197 * Handle CMRC send completion
199 * @v ibdev Infiniband device
201 * @v iobuf I/O buffer
202 * @v rc Completion status code
204 static void ib_cmrc_complete_send ( struct ib_device
*ibdev __unused
,
205 struct ib_queue_pair
*qp
,
206 struct io_buffer
*iobuf
, int rc
) {
207 struct ib_cmrc_connection
*cmrc
= ib_qp_get_ownerdata ( qp
);
209 /* Free the completed I/O buffer */
212 /* Close the connection on any send errors */
214 DBGC ( cmrc
, "CMRC %p send error: %s\n",
215 cmrc
, strerror ( rc
) );
216 ib_cmrc_close ( cmrc
, rc
);
222 * Handle CMRC receive completion
224 * @v ibdev Infiniband device
226 * @v av Address vector, or NULL
227 * @v iobuf I/O buffer
228 * @v rc Completion status code
230 static void ib_cmrc_complete_recv ( struct ib_device
*ibdev __unused
,
231 struct ib_queue_pair
*qp
,
232 struct ib_address_vector
*av __unused
,
233 struct io_buffer
*iobuf
, int rc
) {
234 struct ib_cmrc_connection
*cmrc
= ib_qp_get_ownerdata ( qp
);
236 /* Close the connection on any receive errors */
238 DBGC ( cmrc
, "CMRC %p receive error: %s\n",
239 cmrc
, strerror ( rc
) );
241 ib_cmrc_close ( cmrc
, rc
);
245 DBGC2 ( cmrc
, "CMRC %p received:\n", cmrc
);
246 DBGC2_HDA ( cmrc
, 0, iobuf
->data
, iob_len ( iobuf
) );
249 if ( ( rc
= xfer_deliver_iob ( &cmrc
->xfer
, iobuf
) ) != 0 ) {
250 DBGC ( cmrc
, "CMRC %p could not deliver data: %s\n",
251 cmrc
, strerror ( rc
) );
252 ib_cmrc_close ( cmrc
, rc
);
257 /** Infiniband CMRC completion operations */
258 static struct ib_completion_queue_operations ib_cmrc_completion_ops
= {
259 .complete_send
= ib_cmrc_complete_send
,
260 .complete_recv
= ib_cmrc_complete_recv
,
266 * @v xfer Data transfer interface
267 * @v iobuf Datagram I/O buffer
268 * @v meta Data transfer metadata
269 * @ret rc Return status code
271 static int ib_cmrc_xfer_deliver_iob ( struct xfer_interface
*xfer
,
272 struct io_buffer
*iobuf
,
273 struct xfer_metadata
*meta __unused
) {
274 struct ib_cmrc_connection
*cmrc
=
275 container_of ( xfer
, struct ib_cmrc_connection
, xfer
);
278 /* If no connection has yet been attempted, send this datagram
279 * as the CM REQ private data. Otherwise, send it via the QP.
281 if ( ! cmrc
->connected
) {
283 /* Abort if we have already sent a CM connection request */
285 DBGC ( cmrc
, "CMRC %p attempt to send before "
286 "connection is complete\n", cmrc
);
291 /* Send via CM connection request */
292 cmrc
->conn
= ib_create_conn ( cmrc
->ibdev
, cmrc
->qp
,
293 &cmrc
->dgid
, &cmrc
->service_id
,
294 iobuf
->data
, iob_len ( iobuf
),
296 if ( ! cmrc
->conn
) {
297 DBGC ( cmrc
, "CMRC %p could not connect\n", cmrc
);
305 if ( ( rc
= ib_post_send ( cmrc
->ibdev
, cmrc
->qp
, NULL
,
306 iob_disown ( iobuf
) ) ) != 0 ) {
307 DBGC ( cmrc
, "CMRC %p could not send: %s\n",
308 cmrc
, strerror ( rc
) );
316 /* Free the I/O buffer if necessary */
319 /* Close the connection on any errors */
321 ib_cmrc_close ( cmrc
, rc
);
327 * Check CMRC flow control window
329 * @v xfer Data transfer interface
330 * @ret len Length of window
332 static size_t ib_cmrc_xfer_window ( struct xfer_interface
*xfer
) {
333 struct ib_cmrc_connection
*cmrc
=
334 container_of ( xfer
, struct ib_cmrc_connection
, xfer
);
336 /* We indicate a window only when we are successfully
339 return ( cmrc
->connected
? IB_MAX_PAYLOAD_SIZE
: 0 );
343 * Close CMRC data-transfer interface
345 * @v xfer Data transfer interface
346 * @v rc Reason for close
348 static void ib_cmrc_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
349 struct ib_cmrc_connection
*cmrc
=
350 container_of ( xfer
, struct ib_cmrc_connection
, xfer
);
352 DBGC ( cmrc
, "CMRC %p closed: %s\n", cmrc
, strerror ( rc
) );
353 ib_cmrc_close ( cmrc
, rc
);
356 /** CMRC data transfer interface operations */
357 static struct xfer_interface_operations ib_cmrc_xfer_operations
= {
358 .close
= ib_cmrc_xfer_close
,
359 .vredirect
= ignore_xfer_vredirect
,
360 .window
= ib_cmrc_xfer_window
,
361 .alloc_iob
= default_xfer_alloc_iob
,
362 .deliver_iob
= ib_cmrc_xfer_deliver_iob
,
363 .deliver_raw
= xfer_deliver_as_iob
,
367 * Open CMRC connection
369 * @v xfer Data transfer interface
370 * @v ibdev Infiniband device
371 * @v dgid Destination GID
372 * @v service_id Service ID
373 * @ret rc Returns status code
375 int ib_cmrc_open ( struct xfer_interface
*xfer
, struct ib_device
*ibdev
,
376 struct ib_gid
*dgid
, struct ib_gid_half
*service_id
) {
377 struct ib_cmrc_connection
*cmrc
;
380 /* Allocate and initialise structure */
381 cmrc
= zalloc ( sizeof ( *cmrc
) );
386 xfer_init ( &cmrc
->xfer
, &ib_cmrc_xfer_operations
, &cmrc
->refcnt
);
388 memcpy ( &cmrc
->dgid
, dgid
, sizeof ( cmrc
->dgid
) );
389 memcpy ( &cmrc
->service_id
, service_id
, sizeof ( cmrc
->service_id
) );
390 process_init_stopped ( &cmrc
->shutdown
, ib_cmrc_shutdown
,
393 /* Open Infiniband device */
394 if ( ( rc
= ib_open ( ibdev
) ) != 0 ) {
395 DBGC ( cmrc
, "CMRC %p could not open device: %s\n",
396 cmrc
, strerror ( rc
) );
400 /* Create completion queue */
401 cmrc
->cq
= ib_create_cq ( ibdev
, IB_CMRC_NUM_CQES
,
402 &ib_cmrc_completion_ops
);
404 DBGC ( cmrc
, "CMRC %p could not create completion queue\n",
410 /* Create queue pair */
411 cmrc
->qp
= ib_create_qp ( ibdev
, IB_QPT_RC
, IB_CMRC_NUM_SEND_WQES
,
412 cmrc
->cq
, IB_CMRC_NUM_RECV_WQES
, cmrc
->cq
);
414 DBGC ( cmrc
, "CMRC %p could not create queue pair\n", cmrc
);
418 ib_qp_set_ownerdata ( cmrc
->qp
, cmrc
);
419 DBGC ( cmrc
, "CMRC %p using QPN %lx\n", cmrc
, cmrc
->qp
->qpn
);
421 /* Attach to parent interface, transfer reference (implicitly)
422 * to our shutdown process, and return.
424 xfer_plug_plug ( &cmrc
->xfer
, xfer
);
427 ib_destroy_qp ( ibdev
, cmrc
->qp
);
429 ib_destroy_cq ( ibdev
, cmrc
->cq
);
433 ref_put ( &cmrc
->refcnt
);