Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / infiniband / ib_mi.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 <errno.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <byteswap.h>
29 #include <ipxe/infiniband.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/ib_mi.h>
32
33 /**
34  * @file
35  *
36  * Infiniband management interfaces
37  *
38  */
39
40 /** Management interface number of send WQEs
41  *
42  * This is a policy decision.
43  */
44 #define IB_MI_NUM_SEND_WQES 4
45
46 /** Management interface number of receive WQEs
47  *
48  * This is a policy decision.
49  */
50 #define IB_MI_NUM_RECV_WQES 2
51
52 /** Management interface number of completion queue entries
53  *
54  * This is a policy decision
55  */
56 #define IB_MI_NUM_CQES 8
57
58 /** TID magic signature */
59 #define IB_MI_TID_MAGIC ( ( 'i' << 24 ) | ( 'P' << 16 ) | ( 'X' << 8 ) | 'E' )
60
61 /** TID to use for next MAD */
62 static unsigned int next_tid;
63
64 /**
65  * Handle received MAD
66  *
67  * @v ibdev             Infiniband device
68  * @v mi                Management interface
69  * @v mad               Received MAD
70  * @v av                Source address vector
71  * @ret rc              Return status code
72  */
73 static int ib_mi_handle ( struct ib_device *ibdev,
74                           struct ib_mad_interface *mi,
75                           union ib_mad *mad,
76                           struct ib_address_vector *av ) {
77         struct ib_mad_hdr *hdr = &mad->hdr;
78         struct ib_mad_transaction *madx;
79         struct ib_mad_agent *agent;
80
81         /* Look for a matching transaction by TID */
82         list_for_each_entry ( madx, &mi->madx, list ) {
83                 if ( memcmp ( &hdr->tid, &madx->mad.hdr.tid,
84                               sizeof ( hdr->tid ) ) != 0 )
85                         continue;
86                 /* Found a matching transaction */
87                 madx->op->complete ( ibdev, mi, madx, 0, mad, av );
88                 return 0;
89         }
90
91         /* If there is no matching transaction, look for a listening agent */
92         for_each_table_entry ( agent, IB_MAD_AGENTS ) {
93                 if ( ( ( agent->mgmt_class & IB_MGMT_CLASS_MASK ) !=
94                        ( hdr->mgmt_class & IB_MGMT_CLASS_MASK ) ) ||
95                      ( agent->class_version != hdr->class_version ) ||
96                      ( agent->attr_id != hdr->attr_id ) )
97                         continue;
98                 /* Found a matching agent */
99                 agent->handle ( ibdev, mi, mad, av );
100                 return 0;
101         }
102
103         /* Otherwise, ignore it */
104         DBGC ( mi, "MI %p RX TID %08x%08x ignored\n",
105                mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
106         return -ENOTSUP;
107 }
108
109 /**
110  * Complete receive via management interface
111  *
112  *
113  * @v ibdev             Infiniband device
114  * @v qp                Queue pair
115  * @v dest              Destination address vector
116  * @v source            Source address vector
117  * @v iobuf             I/O buffer
118  * @v rc                Completion status code
119  */
120 static void ib_mi_complete_recv ( struct ib_device *ibdev,
121                                   struct ib_queue_pair *qp,
122                                   struct ib_address_vector *dest __unused,
123                                   struct ib_address_vector *source,
124                                   struct io_buffer *iobuf, int rc ) {
125         struct ib_mad_interface *mi = ib_qp_get_ownerdata ( qp );
126         union ib_mad *mad;
127         struct ib_mad_hdr *hdr;
128
129         /* Ignore errors */
130         if ( rc != 0 ) {
131                 DBGC ( mi, "MI %p RX error: %s\n", mi, strerror ( rc ) );
132                 goto out;
133         }
134
135         /* Sanity checks */
136         if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
137                 DBGC ( mi, "MI %p RX bad size (%zd bytes)\n",
138                        mi, iob_len ( iobuf ) );
139                 DBGC_HDA ( mi, 0, iobuf->data, iob_len ( iobuf ) );
140                 goto out;
141         }
142         mad = iobuf->data;
143         hdr = &mad->hdr;
144         if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
145                 DBGC ( mi, "MI %p RX unsupported base version %x\n",
146                        mi, hdr->base_version );
147                 DBGC_HDA ( mi, 0, mad, sizeof ( *mad ) );
148                 goto out;
149         }
150         DBGC ( mi, "MI %p RX TID %08x%08x (%02x,%02x,%02x,%04x) status "
151                "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
152                hdr->mgmt_class, hdr->class_version, hdr->method,
153                ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
154         DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
155
156         /* Handle MAD */
157         if ( ( rc = ib_mi_handle ( ibdev, mi, mad, source ) ) != 0 )
158                 goto out;
159
160  out:
161         free_iob ( iobuf );
162 }
163
164 /** Management interface completion operations */
165 static struct ib_completion_queue_operations ib_mi_completion_ops = {
166         .complete_recv = ib_mi_complete_recv,
167 };
168
169 /** Management interface queue pair operations */
170 static struct ib_queue_pair_operations ib_mi_queue_pair_ops = {
171         .alloc_iob = alloc_iob,
172 };
173
174 /**
175  * Transmit MAD
176  *
177  * @v ibdev             Infiniband device
178  * @v mi                Management interface
179  * @v mad               MAD
180  * @v av                Destination address vector
181  * @ret rc              Return status code
182  */
183 int ib_mi_send ( struct ib_device *ibdev, struct ib_mad_interface *mi,
184                  union ib_mad *mad, struct ib_address_vector *av ) {
185         struct ib_mad_hdr *hdr = &mad->hdr;
186         struct io_buffer *iobuf;
187         int rc;
188
189         /* Set common fields */
190         hdr->base_version = IB_MGMT_BASE_VERSION;
191         if ( ( hdr->tid[0] == 0 ) && ( hdr->tid[1] == 0 ) ) {
192                 hdr->tid[0] = htonl ( IB_MI_TID_MAGIC );
193                 hdr->tid[1] = htonl ( ++next_tid );
194         }
195         DBGC ( mi, "MI %p TX TID %08x%08x (%02x,%02x,%02x,%04x) status "
196                "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
197                hdr->mgmt_class, hdr->class_version, hdr->method,
198                ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
199         DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
200
201         /* Construct directed route portion of response, if necessary */
202         if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
203                 struct ib_mad_smp *smp = &mad->smp;
204                 unsigned int hop_pointer;
205                 unsigned int hop_count;
206
207                 smp->mad_hdr.status |= htons ( IB_SMP_STATUS_D_INBOUND );
208                 hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
209                 hop_count = smp->mad_hdr.class_specific.smp.hop_count;
210                 assert ( hop_count == hop_pointer );
211                 if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
212                                      sizeof ( smp->return_path.hops[0] ) ) ) {
213                         smp->return_path.hops[hop_pointer] = ibdev->port;
214                 } else {
215                         DBGC ( mi, "MI %p TX TID %08x%08x invalid hop pointer "
216                                "%d\n", mi, ntohl ( hdr->tid[0] ),
217                                ntohl ( hdr->tid[1] ), hop_pointer );
218                         return -EINVAL;
219                 }
220         }
221
222         /* Construct I/O buffer */
223         iobuf = alloc_iob ( sizeof ( *mad ) );
224         if ( ! iobuf ) {
225                 DBGC ( mi, "MI %p could not allocate buffer for TID "
226                        "%08x%08x\n",
227                        mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
228                 return -ENOMEM;
229         }
230         memcpy ( iob_put ( iobuf, sizeof ( *mad ) ), mad, sizeof ( *mad ) );
231
232         /* Send I/O buffer */
233         if ( ( rc = ib_post_send ( ibdev, mi->qp, av, iobuf ) ) != 0 ) {
234                 DBGC ( mi, "MI %p TX TID %08x%08x failed: %s\n",
235                        mi,  ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
236                        strerror ( rc ) );
237                 free_iob ( iobuf );
238                 return rc;
239         }
240
241         return 0;
242 }
243
244 /**
245  * Handle management transaction timer expiry
246  *
247  * @v timer             Retry timer
248  * @v expired           Failure indicator
249  */
250 static void ib_mi_timer_expired ( struct retry_timer *timer, int expired ) {
251         struct ib_mad_transaction *madx =
252                 container_of ( timer, struct ib_mad_transaction, timer );
253         struct ib_mad_interface *mi = madx->mi;
254         struct ib_device *ibdev = mi->ibdev;
255         struct ib_mad_hdr *hdr = &madx->mad.hdr;
256
257         /* Abandon transaction if we have tried too many times */
258         if ( expired ) {
259                 DBGC ( mi, "MI %p abandoning TID %08x%08x\n",
260                        mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
261                 madx->op->complete ( ibdev, mi, madx, -ETIMEDOUT, NULL, NULL );
262                 return;
263         }
264
265         /* Restart retransmission timer */
266         start_timer ( timer );
267
268         /* Resend MAD */
269         ib_mi_send ( ibdev, mi, &madx->mad, &madx->av );
270 }
271
272 /**
273  * Create management transaction
274  *
275  * @v ibdev             Infiniband device
276  * @v mi                Management interface
277  * @v mad               MAD to send
278  * @v av                Destination address, or NULL to use SM's GSI
279  * @v op                Management transaction operations
280  * @ret madx            Management transaction, or NULL
281  */
282 struct ib_mad_transaction *
283 ib_create_madx ( struct ib_device *ibdev, struct ib_mad_interface *mi,
284                  union ib_mad *mad, struct ib_address_vector *av,
285                  struct ib_mad_transaction_operations *op ) {
286         struct ib_mad_transaction *madx;
287
288         /* Allocate and initialise structure */
289         madx = zalloc ( sizeof ( *madx ) );
290         if ( ! madx )
291                 return NULL;
292         timer_init ( &madx->timer, ib_mi_timer_expired, NULL );
293         madx->mi = mi;
294         madx->op = op;
295
296         /* Determine address vector */
297         if ( av ) {
298                 memcpy ( &madx->av, av, sizeof ( madx->av ) );
299         } else {
300                 madx->av.lid = ibdev->sm_lid;
301                 madx->av.sl = ibdev->sm_sl;
302                 madx->av.qpn = IB_QPN_GSI;
303                 madx->av.qkey = IB_QKEY_GSI;
304         }
305
306         /* Copy MAD */
307         memcpy ( &madx->mad, mad, sizeof ( madx->mad ) );
308
309         /* Add to list and start timer to send initial MAD */
310         list_add ( &madx->list, &mi->madx );
311         start_timer_nodelay ( &madx->timer );
312
313         return madx;
314 }
315
316 /**
317  * Destroy management transaction
318  *
319  * @v ibdev             Infiniband device
320  * @v mi                Management interface
321  * @v madx              Management transaction
322  */
323 void ib_destroy_madx ( struct ib_device *ibdev __unused,
324                        struct ib_mad_interface *mi __unused,
325                        struct ib_mad_transaction *madx ) {
326
327         /* Stop timer and remove from list */
328         stop_timer ( &madx->timer );
329         list_del ( &madx->list );
330
331         /* Free transaction */
332         free ( madx );
333 }
334
335 /**
336  * Create management interface
337  *
338  * @v ibdev             Infiniband device
339  * @v type              Queue pair type
340  * @ret mi              Management agent, or NULL
341  */
342 struct ib_mad_interface * ib_create_mi ( struct ib_device *ibdev,
343                                          enum ib_queue_pair_type type ) {
344         struct ib_mad_interface *mi;
345         int rc;
346
347         /* Allocate and initialise fields */
348         mi = zalloc ( sizeof ( *mi ) );
349         if ( ! mi )
350                 goto err_alloc;
351         mi->ibdev = ibdev;
352         INIT_LIST_HEAD ( &mi->madx );
353
354         /* Create completion queue */
355         mi->cq = ib_create_cq ( ibdev, IB_MI_NUM_CQES, &ib_mi_completion_ops );
356         if ( ! mi->cq ) {
357                 DBGC ( mi, "MI %p could not allocate completion queue\n", mi );
358                 goto err_create_cq;
359         }
360
361         /* Create queue pair */
362         mi->qp = ib_create_qp ( ibdev, type, IB_MI_NUM_SEND_WQES, mi->cq,
363                                 IB_MI_NUM_RECV_WQES, mi->cq,
364                                 &ib_mi_queue_pair_ops );
365         if ( ! mi->qp ) {
366                 DBGC ( mi, "MI %p could not allocate queue pair\n", mi );
367                 goto err_create_qp;
368         }
369         ib_qp_set_ownerdata ( mi->qp, mi );
370         DBGC ( mi, "MI %p (%s) running on QPN %#lx\n",
371                mi, ( ( type == IB_QPT_SMI ) ? "SMI" : "GSI" ), mi->qp->qpn );
372
373         /* Set queue key */
374         mi->qp->qkey = ( ( type == IB_QPT_SMI ) ? IB_QKEY_SMI : IB_QKEY_GSI );
375         if ( ( rc = ib_modify_qp ( ibdev, mi->qp ) ) != 0 ) {
376                 DBGC ( mi, "MI %p could not set queue key: %s\n",
377                        mi, strerror ( rc ) );
378                 goto err_modify_qp;
379         }
380
381         /* Fill receive ring */
382         ib_refill_recv ( ibdev, mi->qp );
383         return mi;
384
385  err_modify_qp:
386         ib_destroy_qp ( ibdev, mi->qp );
387  err_create_qp:
388         ib_destroy_cq ( ibdev, mi->cq );
389  err_create_cq:
390         free ( mi );
391  err_alloc:
392         return NULL;
393 }
394
395 /**
396  * Destroy management interface
397  *
398  * @v mi                Management interface
399  */
400 void ib_destroy_mi ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {
401         struct ib_mad_transaction *madx;
402         struct ib_mad_transaction *tmp;
403
404         /* Flush any outstanding requests */
405         list_for_each_entry_safe ( madx, tmp, &mi->madx, list ) {
406                 DBGC ( mi, "MI %p destroyed while TID %08x%08x in progress\n",
407                        mi, ntohl ( madx->mad.hdr.tid[0] ),
408                        ntohl ( madx->mad.hdr.tid[1] ) );
409                 madx->op->complete ( ibdev, mi, madx, -ECANCELED, NULL, NULL );
410         }
411
412         ib_destroy_qp ( ibdev, mi->qp );
413         ib_destroy_cq ( ibdev, mi->cq );
414         free ( mi );
415 }