[infiniband] Use "%#lx" as format specifier for queue pair numbers
[ipxe.git] / src / net / infiniband / ib_cm.c
1 /*
2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <byteswap.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <ipxe/infiniband.h>
33 #include <ipxe/ib_mi.h>
34 #include <ipxe/ib_pathrec.h>
35 #include <ipxe/ib_cm.h>
36
37 /**
38 * @file
39 *
40 * Infiniband communication management
41 *
42 */
43
44 /** List of connections */
45 static LIST_HEAD ( ib_cm_conns );
46
47 /**
48 * Find connection by local communication ID
49 *
50 * @v local_id Local communication ID
51 * @ret conn Connection, or NULL
52 */
53 static struct ib_connection * ib_cm_find ( uint32_t local_id ) {
54 struct ib_connection *conn;
55
56 list_for_each_entry ( conn, &ib_cm_conns, list ) {
57 if ( conn->local_id == local_id )
58 return conn;
59 }
60 return NULL;
61 }
62
63 /**
64 * Send "ready to use" response
65 *
66 * @v ibdev Infiniband device
67 * @v mi Management interface
68 * @v av Address vector
69 * @v local_id Local communication ID
70 * @v remote_id Remote communication ID
71 * @ret rc Return status code
72 */
73 static int ib_cm_send_rtu ( struct ib_device *ibdev,
74 struct ib_mad_interface *mi,
75 struct ib_address_vector *av,
76 uint32_t local_id, uint32_t remote_id ) {
77 union ib_mad mad;
78 struct ib_cm_ready_to_use *rtu = &mad.cm.cm_data.ready_to_use;
79 int rc;
80
81 /* Construct "ready to use" response */
82 memset ( &mad, 0, sizeof ( mad ) );
83 mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
84 mad.hdr.class_version = IB_CM_CLASS_VERSION;
85 mad.hdr.method = IB_MGMT_METHOD_SEND;
86 mad.hdr.attr_id = htons ( IB_CM_ATTR_READY_TO_USE );
87 rtu->local_id = htonl ( local_id );
88 rtu->remote_id = htonl ( remote_id );
89 if ( ( rc = ib_mi_send ( ibdev, mi, &mad, av ) ) != 0 ){
90 DBG ( "CM could not send RTU: %s\n", strerror ( rc ) );
91 return rc;
92 }
93
94 return 0;
95 }
96
97 /**
98 * Handle duplicate connection replies
99 *
100 * @v ibdev Infiniband device
101 * @v mi Management interface
102 * @v mad Received MAD
103 * @v av Source address vector
104 * @ret rc Return status code
105 *
106 * If a "ready to use" MAD is lost, the peer may resend the connection
107 * reply. We have to respond to these with duplicate "ready to use"
108 * MADs, otherwise the peer may time out and drop the connection.
109 */
110 static void ib_cm_recv_rep ( struct ib_device *ibdev,
111 struct ib_mad_interface *mi,
112 union ib_mad *mad,
113 struct ib_address_vector *av ) {
114 struct ib_cm_connect_reply *rep = &mad->cm.cm_data.connect_reply;
115 struct ib_connection *conn;
116 uint32_t local_id = ntohl ( rep->remote_id );
117 int rc;
118
119 /* Identify connection */
120 conn = ib_cm_find ( local_id );
121 if ( conn ) {
122 /* Try to send "ready to use" reply */
123 if ( ( rc = ib_cm_send_rtu ( ibdev, mi, av, conn->local_id,
124 conn->remote_id ) ) != 0 ) {
125 /* Ignore errors; the remote end will retry */
126 }
127 } else {
128 DBG ( "CM unidentified connection %08x\n", local_id );
129 }
130 }
131
132 /**
133 * Send reply to disconnection request
134 *
135 * @v ibdev Infiniband device
136 * @v mi Management interface
137 * @v av Address vector
138 * @v local_id Local communication ID
139 * @v remote_id Remote communication ID
140 * @ret rc Return status code
141 */
142 static int ib_cm_send_drep ( struct ib_device *ibdev,
143 struct ib_mad_interface *mi,
144 struct ib_address_vector *av,
145 uint32_t local_id, uint32_t remote_id ) {
146 union ib_mad mad;
147 struct ib_cm_disconnect_reply *drep = &mad.cm.cm_data.disconnect_reply;
148 int rc;
149
150 /* Construct reply to disconnection request */
151 memset ( &mad, 0, sizeof ( mad ) );
152 mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
153 mad.hdr.class_version = IB_CM_CLASS_VERSION;
154 mad.hdr.method = IB_MGMT_METHOD_SEND;
155 mad.hdr.attr_id = htons ( IB_CM_ATTR_DISCONNECT_REPLY );
156 drep->local_id = htonl ( local_id );
157 drep->remote_id = htonl ( remote_id );
158 if ( ( rc = ib_mi_send ( ibdev, mi, &mad, av ) ) != 0 ){
159 DBG ( "CM could not send DREP: %s\n", strerror ( rc ) );
160 return rc;
161 }
162
163 return 0;
164 }
165
166 /**
167 * Handle disconnection requests
168 *
169 * @v ibdev Infiniband device
170 * @v mi Management interface
171 * @v mad Received MAD
172 * @v av Source address vector
173 * @ret rc Return status code
174 */
175 static void ib_cm_recv_dreq ( struct ib_device *ibdev,
176 struct ib_mad_interface *mi,
177 union ib_mad *mad,
178 struct ib_address_vector *av ) {
179 struct ib_cm_disconnect_request *dreq =
180 &mad->cm.cm_data.disconnect_request;
181 struct ib_connection *conn;
182 uint32_t local_id = ntohl ( dreq->remote_id );
183 uint32_t remote_id = ntohl ( dreq->local_id );
184 int rc;
185
186 /* Identify connection */
187 conn = ib_cm_find ( local_id );
188 if ( conn ) {
189 /* Notify upper layer */
190 conn->op->changed ( ibdev, conn->qp, conn, -ENOTCONN,
191 &dreq->private_data,
192 sizeof ( dreq->private_data ) );
193 } else {
194 DBG ( "CM unidentified connection %08x\n", local_id );
195 }
196
197 /* Send reply */
198 if ( ( rc = ib_cm_send_drep ( ibdev, mi, av, local_id,
199 remote_id ) ) != 0 ) {
200 /* Ignore errors; the remote end will retry */
201 }
202 };
203
204 /** Communication management agents */
205 struct ib_mad_agent ib_cm_agent[] __ib_mad_agent = {
206 {
207 .mgmt_class = IB_MGMT_CLASS_CM,
208 .class_version = IB_CM_CLASS_VERSION,
209 .attr_id = htons ( IB_CM_ATTR_CONNECT_REPLY ),
210 .handle = ib_cm_recv_rep,
211 },
212 {
213 .mgmt_class = IB_MGMT_CLASS_CM,
214 .class_version = IB_CM_CLASS_VERSION,
215 .attr_id = htons ( IB_CM_ATTR_DISCONNECT_REQUEST ),
216 .handle = ib_cm_recv_dreq,
217 },
218 };
219
220 /**
221 * Convert connection rejection reason to return status code
222 *
223 * @v reason Rejection reason (in network byte order)
224 * @ret rc Return status code
225 */
226 static int ib_cm_rejection_reason_to_rc ( uint16_t reason ) {
227 switch ( reason ) {
228 case htons ( IB_CM_REJECT_BAD_SERVICE_ID ) :
229 return -ENODEV;
230 case htons ( IB_CM_REJECT_STALE_CONN ) :
231 return -EALREADY;
232 case htons ( IB_CM_REJECT_CONSUMER ) :
233 return -ENOTTY;
234 default:
235 return -EPERM;
236 }
237 }
238
239 /**
240 * Handle connection request transaction completion
241 *
242 * @v ibdev Infiniband device
243 * @v mi Management interface
244 * @v madx Management transaction
245 * @v rc Status code
246 * @v mad Received MAD (or NULL on error)
247 * @v av Source address vector (or NULL on error)
248 */
249 static void ib_cm_req_complete ( struct ib_device *ibdev,
250 struct ib_mad_interface *mi,
251 struct ib_mad_transaction *madx,
252 int rc, union ib_mad *mad,
253 struct ib_address_vector *av ) {
254 struct ib_connection *conn = ib_madx_get_ownerdata ( madx );
255 struct ib_queue_pair *qp = conn->qp;
256 struct ib_cm_common *common = &mad->cm.cm_data.common;
257 struct ib_cm_connect_reply *rep = &mad->cm.cm_data.connect_reply;
258 struct ib_cm_connect_reject *rej = &mad->cm.cm_data.connect_reject;
259 void *private_data = NULL;
260 size_t private_data_len = 0;
261
262 /* Report failures */
263 if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
264 rc = -EIO;
265 if ( rc != 0 ) {
266 DBGC ( conn, "CM %p connection request failed: %s\n",
267 conn, strerror ( rc ) );
268 goto out;
269 }
270
271 /* Record remote communication ID */
272 conn->remote_id = ntohl ( common->local_id );
273
274 /* Handle response */
275 switch ( mad->hdr.attr_id ) {
276
277 case htons ( IB_CM_ATTR_CONNECT_REPLY ) :
278 /* Extract fields */
279 qp->av.qpn = ( ntohl ( rep->local_qpn ) >> 8 );
280 qp->send.psn = ( ntohl ( rep->starting_psn ) >> 8 );
281 private_data = &rep->private_data;
282 private_data_len = sizeof ( rep->private_data );
283 DBGC ( conn, "CM %p connected to QPN %#lx PSN %#x\n",
284 conn, qp->av.qpn, qp->send.psn );
285
286 /* Modify queue pair */
287 if ( ( rc = ib_modify_qp ( ibdev, qp ) ) != 0 ) {
288 DBGC ( conn, "CM %p could not modify queue pair: %s\n",
289 conn, strerror ( rc ) );
290 goto out;
291 }
292
293 /* Send "ready to use" reply */
294 if ( ( rc = ib_cm_send_rtu ( ibdev, mi, av, conn->local_id,
295 conn->remote_id ) ) != 0 ) {
296 /* Treat as non-fatal */
297 rc = 0;
298 }
299 break;
300
301 case htons ( IB_CM_ATTR_CONNECT_REJECT ) :
302 /* Extract fields */
303 DBGC ( conn, "CM %p connection rejected (reason %d)\n",
304 conn, ntohs ( rej->reason ) );
305 /* Private data is valid only for a Consumer Reject */
306 if ( rej->reason == htons ( IB_CM_REJECT_CONSUMER ) ) {
307 private_data = &rej->private_data;
308 private_data_len = sizeof ( rej->private_data );
309 }
310 rc = ib_cm_rejection_reason_to_rc ( rej->reason );
311 break;
312
313 default:
314 DBGC ( conn, "CM %p unexpected response (attribute %04x)\n",
315 conn, ntohs ( mad->hdr.attr_id ) );
316 rc = -ENOTSUP;
317 break;
318 }
319
320 out:
321 /* Destroy the completed transaction */
322 ib_destroy_madx ( ibdev, ibdev->gsi, madx );
323 conn->madx = NULL;
324
325 /* Hand off to the upper completion handler */
326 conn->op->changed ( ibdev, qp, conn, rc, private_data,
327 private_data_len );
328 }
329
330 /** Connection request operations */
331 static struct ib_mad_transaction_operations ib_cm_req_op = {
332 .complete = ib_cm_req_complete,
333 };
334
335 /**
336 * Handle connection path transaction completion
337 *
338 * @v ibdev Infiniband device
339 * @v path Path
340 * @v rc Status code
341 * @v av Address vector, or NULL on error
342 */
343 static void ib_cm_path_complete ( struct ib_device *ibdev,
344 struct ib_path *path, int rc,
345 struct ib_address_vector *av ) {
346 struct ib_connection *conn = ib_path_get_ownerdata ( path );
347 struct ib_queue_pair *qp = conn->qp;
348 union ib_mad mad;
349 struct ib_cm_connect_request *req = &mad.cm.cm_data.connect_request;
350 size_t private_data_len;
351
352 /* Report failures */
353 if ( rc != 0 ) {
354 DBGC ( conn, "CM %p path lookup failed: %s\n",
355 conn, strerror ( rc ) );
356 conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
357 goto out;
358 }
359
360 /* Update queue pair peer path */
361 memcpy ( &qp->av, av, sizeof ( qp->av ) );
362
363 /* Construct connection request */
364 memset ( &mad, 0, sizeof ( mad ) );
365 mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
366 mad.hdr.class_version = IB_CM_CLASS_VERSION;
367 mad.hdr.method = IB_MGMT_METHOD_SEND;
368 mad.hdr.attr_id = htons ( IB_CM_ATTR_CONNECT_REQUEST );
369 req->local_id = htonl ( conn->local_id );
370 memcpy ( &req->service_id, &conn->service_id,
371 sizeof ( req->service_id ) );
372 memcpy ( &req->local_ca, &ibdev->node_guid, sizeof ( req->local_ca ) );
373 req->local_qpn__responder_resources = htonl ( ( qp->qpn << 8 ) | 1 );
374 req->local_eecn__initiator_depth = htonl ( ( 0 << 8 ) | 1 );
375 req->remote_eecn__remote_timeout__service_type__ee_flow_ctrl =
376 htonl ( ( 0x14 << 3 ) | ( IB_CM_TRANSPORT_RC << 1 ) |
377 ( 0 << 0 ) );
378 req->starting_psn__local_timeout__retry_count =
379 htonl ( ( qp->recv.psn << 8 ) | ( 0x14 << 3 ) |
380 ( 0x07 << 0 ) );
381 req->pkey = htons ( ibdev->pkey );
382 req->payload_mtu__rdc_exists__rnr_retry =
383 ( ( IB_MTU_2048 << 4 ) | ( 1 << 3 ) | ( 0x07 << 0 ) );
384 req->max_cm_retries__srq = ( ( 0x0f << 4 ) | ( 0 << 3 ) );
385 req->primary.local_lid = htons ( ibdev->lid );
386 req->primary.remote_lid = htons ( conn->qp->av.lid );
387 memcpy ( &req->primary.local_gid, &ibdev->gid,
388 sizeof ( req->primary.local_gid ) );
389 memcpy ( &req->primary.remote_gid, &conn->qp->av.gid,
390 sizeof ( req->primary.remote_gid ) );
391 req->primary.flow_label__rate =
392 htonl ( ( 0 << 12 ) | ( conn->qp->av.rate << 0 ) );
393 req->primary.hop_limit = 0;
394 req->primary.sl__subnet_local =
395 ( ( conn->qp->av.sl << 4 ) | ( 1 << 3 ) );
396 req->primary.local_ack_timeout = ( 0x13 << 3 );
397 private_data_len = conn->private_data_len;
398 if ( private_data_len > sizeof ( req->private_data ) )
399 private_data_len = sizeof ( req->private_data );
400 memcpy ( &req->private_data, &conn->private_data, private_data_len );
401
402 /* Create connection request */
403 av->qpn = IB_QPN_GSI;
404 av->qkey = IB_QKEY_GSI;
405 conn->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, av,
406 &ib_cm_req_op );
407 if ( ! conn->madx ) {
408 DBGC ( conn, "CM %p could not create connection request\n",
409 conn );
410 conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
411 goto out;
412 }
413 ib_madx_set_ownerdata ( conn->madx, conn );
414
415 out:
416 /* Destroy the completed transaction */
417 ib_destroy_path ( ibdev, path );
418 conn->path = NULL;
419 }
420
421 /** Connection path operations */
422 static struct ib_path_operations ib_cm_path_op = {
423 .complete = ib_cm_path_complete,
424 };
425
426 /**
427 * Create connection to remote QP
428 *
429 * @v ibdev Infiniband device
430 * @v qp Queue pair
431 * @v dgid Target GID
432 * @v service_id Target service ID
433 * @v private_data Connection request private data
434 * @v private_data_len Length of connection request private data
435 * @v op Connection operations
436 * @ret conn Connection
437 */
438 struct ib_connection *
439 ib_create_conn ( struct ib_device *ibdev, struct ib_queue_pair *qp,
440 union ib_gid *dgid, union ib_guid *service_id,
441 void *private_data, size_t private_data_len,
442 struct ib_connection_operations *op ) {
443 struct ib_connection *conn;
444
445 /* Allocate and initialise request */
446 conn = zalloc ( sizeof ( *conn ) + private_data_len );
447 if ( ! conn )
448 goto err_alloc_conn;
449 conn->ibdev = ibdev;
450 conn->qp = qp;
451 memset ( &qp->av, 0, sizeof ( qp->av ) );
452 qp->av.gid_present = 1;
453 memcpy ( &qp->av.gid, dgid, sizeof ( qp->av.gid ) );
454 conn->local_id = random();
455 memcpy ( &conn->service_id, service_id, sizeof ( conn->service_id ) );
456 conn->op = op;
457 conn->private_data_len = private_data_len;
458 memcpy ( &conn->private_data, private_data, private_data_len );
459
460 /* Create path */
461 conn->path = ib_create_path ( ibdev, &qp->av, &ib_cm_path_op );
462 if ( ! conn->path )
463 goto err_create_path;
464 ib_path_set_ownerdata ( conn->path, conn );
465
466 /* Add to list of connections */
467 list_add ( &conn->list, &ib_cm_conns );
468
469 DBGC ( conn, "CM %p created for IBDEV %s QPN %#lx\n",
470 conn, ibdev->name, qp->qpn );
471 DBGC ( conn, "CM %p connecting to " IB_GID_FMT " " IB_GUID_FMT "\n",
472 conn, IB_GID_ARGS ( dgid ), IB_GUID_ARGS ( service_id ) );
473
474 return conn;
475
476 ib_destroy_path ( ibdev, conn->path );
477 err_create_path:
478 free ( conn );
479 err_alloc_conn:
480 return NULL;
481 }
482
483 /**
484 * Destroy connection to remote QP
485 *
486 * @v ibdev Infiniband device
487 * @v qp Queue pair
488 * @v conn Connection
489 */
490 void ib_destroy_conn ( struct ib_device *ibdev,
491 struct ib_queue_pair *qp __unused,
492 struct ib_connection *conn ) {
493
494 list_del ( &conn->list );
495 if ( conn->madx )
496 ib_destroy_madx ( ibdev, ibdev->gsi, conn->madx );
497 if ( conn->path )
498 ib_destroy_path ( ibdev, conn->path );
499 free ( conn );
500 }