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