2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 #include <ipxe/infiniband.h>
33 #include <ipxe/ib_mi.h>
34 #include <ipxe/ib_pathrec.h>
35 #include <ipxe/ib_cm.h>
40 * Infiniband communication management
44 /** List of connections */
45 static LIST_HEAD ( ib_cm_conns );
48 * Find connection by local communication ID
50 * @v local_id Local communication ID
51 * @ret conn Connection, or NULL
53 static struct ib_connection * ib_cm_find ( uint32_t local_id ) {
54 struct ib_connection *conn;
56 list_for_each_entry ( conn, &ib_cm_conns, list ) {
57 if ( conn->local_id == local_id )
64 * Send "ready to use" response
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
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 ) {
78 struct ib_cm_ready_to_use *rtu = &mad.cm.cm_data.ready_to_use;
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 ) );
98 * Handle duplicate connection replies
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
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.
110 static void ib_cm_recv_rep ( struct ib_device *ibdev,
111 struct ib_mad_interface *mi,
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 );
119 /* Identify connection */
120 conn = ib_cm_find ( local_id );
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 */
128 DBG ( "CM unidentified connection %08x\n", local_id );
133 * Send reply to disconnection request
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
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 ) {
147 struct ib_cm_disconnect_reply *drep = &mad.cm.cm_data.disconnect_reply;
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 ) );
167 * Handle disconnection requests
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
175 static void ib_cm_recv_dreq ( struct ib_device *ibdev,
176 struct ib_mad_interface *mi,
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 );
186 /* Identify connection */
187 conn = ib_cm_find ( local_id );
189 /* Notify upper layer */
190 conn->op->changed ( ibdev, conn->qp, conn, -ENOTCONN,
192 sizeof ( dreq->private_data ) );
194 DBG ( "CM unidentified connection %08x\n", local_id );
198 if ( ( rc = ib_cm_send_drep ( ibdev, mi, av, local_id,
199 remote_id ) ) != 0 ) {
200 /* Ignore errors; the remote end will retry */
204 /** Communication management agents */
205 struct ib_mad_agent ib_cm_agent[] __ib_mad_agent = {
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,
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,
221 * Convert connection rejection reason to return status code
223 * @v reason Rejection reason (in network byte order)
224 * @ret rc Return status code
226 static int ib_cm_rejection_reason_to_rc ( uint16_t reason ) {
228 case htons ( IB_CM_REJECT_BAD_SERVICE_ID ) :
230 case htons ( IB_CM_REJECT_STALE_CONN ) :
232 case htons ( IB_CM_REJECT_CONSUMER ) :
240 * Handle connection request transaction completion
242 * @v ibdev Infiniband device
243 * @v mi Management interface
244 * @v madx Management transaction
246 * @v mad Received MAD (or NULL on error)
247 * @v av Source address vector (or NULL on error)
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;
262 /* Report failures */
263 if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
266 DBGC ( conn, "CM %p connection request failed: %s\n",
267 conn, strerror ( rc ) );
271 /* Record remote communication ID */
272 conn->remote_id = ntohl ( common->local_id );
274 /* Handle response */
275 switch ( mad->hdr.attr_id ) {
277 case htons ( IB_CM_ATTR_CONNECT_REPLY ) :
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 );
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 ) );
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 */
301 case htons ( IB_CM_ATTR_CONNECT_REJECT ) :
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 );
310 rc = ib_cm_rejection_reason_to_rc ( rej->reason );
314 DBGC ( conn, "CM %p unexpected response (attribute %04x)\n",
315 conn, ntohs ( mad->hdr.attr_id ) );
321 /* Destroy the completed transaction */
322 ib_destroy_madx ( ibdev, ibdev->gsi, madx );
325 /* Hand off to the upper completion handler */
326 conn->op->changed ( ibdev, qp, conn, rc, private_data,
330 /** Connection request operations */
331 static struct ib_mad_transaction_operations ib_cm_req_op = {
332 .complete = ib_cm_req_complete,
336 * Handle connection path transaction completion
338 * @v ibdev Infiniband device
341 * @v av Address vector, or NULL on error
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;
349 struct ib_cm_connect_request *req = &mad.cm.cm_data.connect_request;
350 size_t private_data_len;
352 /* Report failures */
354 DBGC ( conn, "CM %p path lookup failed: %s\n",
355 conn, strerror ( rc ) );
356 conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
360 /* Update queue pair peer path */
361 memcpy ( &qp->av, av, sizeof ( qp->av ) );
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 ) |
378 req->starting_psn__local_timeout__retry_count =
379 htonl ( ( qp->recv.psn << 8 ) | ( 0x14 << 3 ) |
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 );
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,
407 if ( ! conn->madx ) {
408 DBGC ( conn, "CM %p could not create connection request\n",
410 conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
413 ib_madx_set_ownerdata ( conn->madx, conn );
416 /* Destroy the completed transaction */
417 ib_destroy_path ( ibdev, path );
421 /** Connection path operations */
422 static struct ib_path_operations ib_cm_path_op = {
423 .complete = ib_cm_path_complete,
427 * Create connection to remote QP
429 * @v ibdev Infiniband device
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
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;
445 /* Allocate and initialise request */
446 conn = zalloc ( sizeof ( *conn ) + private_data_len );
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 ) );
457 conn->private_data_len = private_data_len;
458 memcpy ( &conn->private_data, private_data, private_data_len );
461 conn->path = ib_create_path ( ibdev, &qp->av, &ib_cm_path_op );
463 goto err_create_path;
464 ib_path_set_ownerdata ( conn->path, conn );
466 /* Add to list of connections */
467 list_add ( &conn->list, &ib_cm_conns );
469 DBGC ( conn, "CM %p created for IBDEV %p QPN %lx\n",
470 conn, ibdev, 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 ) );
476 ib_destroy_path ( ibdev, conn->path );
484 * Destroy connection to remote QP
486 * @v ibdev Infiniband device
490 void ib_destroy_conn ( struct ib_device *ibdev,
491 struct ib_queue_pair *qp __unused,
492 struct ib_connection *conn ) {
494 list_del ( &conn->list );
496 ib_destroy_madx ( ibdev, ibdev->gsi, conn->madx );
498 ib_destroy_path ( ibdev, conn->path );