These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / net / infiniband / ib_pathrec.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 <ipxe/infiniband.h>
32 #include <ipxe/ib_mi.h>
33 #include <ipxe/ib_pathrec.h>
34
35 /** @file
36  *
37  * Infiniband path lookups
38  *
39  */
40
41 /**
42  * Handle path transaction completion
43  *
44  * @v ibdev             Infiniband device
45  * @v mi                Management interface
46  * @v madx              Management transaction
47  * @v rc                Status code
48  * @v mad               Received MAD (or NULL on error)
49  * @v av                Source address vector (or NULL on error)
50  */
51 static void ib_path_complete ( struct ib_device *ibdev,
52                                struct ib_mad_interface *mi,
53                                struct ib_mad_transaction *madx,
54                                int rc, union ib_mad *mad,
55                                struct ib_address_vector *av __unused ) {
56         struct ib_path *path = ib_madx_get_ownerdata ( madx );
57         union ib_gid *dgid = &path->av.gid;
58         struct ib_path_record *pathrec = &mad->sa.sa_data.path_record;
59
60         /* Report failures */
61         if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
62                 rc = -ENETUNREACH;
63         if ( rc != 0 ) {
64                 DBGC ( ibdev, "IBDEV %p path lookup for " IB_GID_FMT
65                        " failed: %s\n",
66                        ibdev, IB_GID_ARGS ( dgid ), strerror ( rc ) );
67                 goto out;
68         }
69
70         /* Extract values from MAD */
71         path->av.lid = ntohs ( pathrec->dlid );
72         path->av.sl = ( pathrec->reserved__sl & 0x0f );
73         path->av.rate = ( pathrec->rate_selector__rate & 0x3f );
74         DBGC ( ibdev, "IBDEV %p path to " IB_GID_FMT " is %04x sl %d rate "
75                "%d\n", ibdev, IB_GID_ARGS ( dgid ), path->av.lid, path->av.sl,
76                path->av.rate );
77
78  out:
79         /* Destroy the completed transaction */
80         ib_destroy_madx ( ibdev, mi, madx );
81         path->madx = NULL;
82
83         /* Hand off to upper completion handler */
84         path->op->complete ( ibdev, path, rc, &path->av );
85 }
86
87 /** Path transaction completion operations */
88 static struct ib_mad_transaction_operations ib_path_op = {
89         .complete = ib_path_complete,
90 };
91
92 /**
93  * Create path
94  *
95  * @v ibdev             Infiniband device
96  * @v av                Address vector to complete
97  * @v op                Path operations
98  * @ret path            Path
99  */
100 struct ib_path *
101 ib_create_path ( struct ib_device *ibdev, struct ib_address_vector *av,
102                  struct ib_path_operations *op ) {
103         struct ib_path *path;
104         union ib_mad mad;
105         struct ib_mad_sa *sa = &mad.sa;
106
107         /* Allocate and initialise structure */
108         path = zalloc ( sizeof ( *path ) );
109         if ( ! path )
110                 goto err_alloc_path;
111         path->ibdev = ibdev;
112         memcpy ( &path->av, av, sizeof ( path->av ) );
113         path->op = op;
114
115         /* Construct path request */
116         memset ( sa, 0, sizeof ( *sa ) );
117         sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
118         sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
119         sa->mad_hdr.method = IB_MGMT_METHOD_GET;
120         sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
121         sa->sa_hdr.comp_mask[1] =
122                 htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
123         memcpy ( &sa->sa_data.path_record.dgid, &path->av.gid,
124                  sizeof ( sa->sa_data.path_record.dgid ) );
125         memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
126                  sizeof ( sa->sa_data.path_record.sgid ) );
127
128         /* Create management transaction */
129         path->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
130                                       &ib_path_op );
131         if ( ! path->madx )
132                 goto err_create_madx;
133         ib_madx_set_ownerdata ( path->madx, path );
134
135         return path;
136
137         ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
138  err_create_madx:
139         free ( path );
140  err_alloc_path:
141         return NULL;
142 }
143
144 /**
145  * Destroy path
146  *
147  * @v ibdev             Infiniband device
148  * @v path              Path
149  */
150 void ib_destroy_path ( struct ib_device *ibdev, struct ib_path *path ) {
151
152         if ( path->madx )
153                 ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
154         free ( path );
155 }
156
157 /** Number of path cache entries
158  *
159  * Must be a power of two.
160  */
161 #define IB_NUM_CACHED_PATHS 4
162
163 /** A cached path */
164 struct ib_cached_path {
165         /** Path */
166         struct ib_path *path;
167 };
168
169 /** Path cache */
170 static struct ib_cached_path ib_path_cache[IB_NUM_CACHED_PATHS];
171
172 /** Oldest path cache entry index */
173 static unsigned int ib_path_cache_idx;
174
175 /**
176  * Find path cache entry
177  *
178  * @v ibdev             Infiniband device
179  * @v dgid              Destination GID
180  * @ret path            Path cache entry, or NULL
181  */
182 static struct ib_cached_path *
183 ib_find_path_cache_entry ( struct ib_device *ibdev, union ib_gid *dgid ) {
184         struct ib_cached_path *cached;
185         unsigned int i;
186
187         for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
188                 cached = &ib_path_cache[i];
189                 if ( ! cached->path )
190                         continue;
191                 if ( cached->path->ibdev != ibdev )
192                         continue;
193                 if ( memcmp ( &cached->path->av.gid, dgid,
194                               sizeof ( cached->path->av.gid ) ) != 0 )
195                         continue;
196                 return cached;
197         }
198
199         return NULL;
200 }
201
202 /**
203  * Handle cached path transaction completion
204  *
205  * @v ibdev             Infiniband device
206  * @v path              Path
207  * @v rc                Status code
208  * @v av                Address vector, or NULL on error
209  */
210 static void ib_cached_path_complete ( struct ib_device *ibdev,
211                                       struct ib_path *path, int rc,
212                                       struct ib_address_vector *av __unused ) {
213         struct ib_cached_path *cached = ib_path_get_ownerdata ( path );
214
215         /* If the transaction failed, erase the cache entry */
216         if ( rc != 0 ) {
217                 /* Destroy the old cache entry */
218                 ib_destroy_path ( ibdev, path );
219                 memset ( cached, 0, sizeof ( *cached ) );
220                 return;
221         }
222
223         /* Do not destroy the completed transaction; we still need to
224          * refer to the resolved path.
225          */
226 }
227
228 /** Cached path transaction completion operations */
229 static struct ib_path_operations ib_cached_path_op = {
230         .complete = ib_cached_path_complete,
231 };
232
233 /**
234  * Resolve path
235  *
236  * @v ibdev             Infiniband device
237  * @v av                Address vector to complete
238  * @ret rc              Return status code
239  *
240  * This provides a non-transactional way to resolve a path, via a
241  * cache similar to ARP.
242  */
243 int ib_resolve_path ( struct ib_device *ibdev, struct ib_address_vector *av ) {
244         union ib_gid *gid = &av->gid;
245         struct ib_cached_path *cached;
246         unsigned int cache_idx;
247
248         /* Sanity check */
249         if ( ! av->gid_present ) {
250                 DBGC ( ibdev, "IBDEV %p attempt to look up path without GID\n",
251                        ibdev );
252                 return -EINVAL;
253         }
254
255         /* Look in cache for a matching entry */
256         cached = ib_find_path_cache_entry ( ibdev, gid );
257         if ( cached && cached->path->av.lid ) {
258                 /* Populated entry found */
259                 av->lid = cached->path->av.lid;
260                 av->rate = cached->path->av.rate;
261                 av->sl = cached->path->av.sl;
262                 DBGC2 ( ibdev, "IBDEV %p cache hit for " IB_GID_FMT "\n",
263                         ibdev, IB_GID_ARGS ( gid ) );
264                 return 0;
265         }
266         DBGC ( ibdev, "IBDEV %p cache miss for " IB_GID_FMT "%s\n", ibdev,
267                IB_GID_ARGS ( gid ), ( cached ? " (in progress)" : "" ) );
268
269         /* If lookup is already in progress, do nothing */
270         if ( cached )
271                 return -ENOENT;
272
273         /* Locate a new cache entry to use */
274         cache_idx = ( (ib_path_cache_idx++) % IB_NUM_CACHED_PATHS );
275         cached = &ib_path_cache[cache_idx];
276
277         /* Destroy the old cache entry */
278         if ( cached->path )
279                 ib_destroy_path ( ibdev, cached->path );
280         memset ( cached, 0, sizeof ( *cached ) );
281
282         /* Create new path */
283         cached->path = ib_create_path ( ibdev, av, &ib_cached_path_op );
284         if ( ! cached->path ) {
285                 DBGC ( ibdev, "IBDEV %p could not create path\n",
286                        ibdev );
287                 return -ENOMEM;
288         }
289         ib_path_set_ownerdata ( cached->path, cached );
290
291         /* Not found yet */
292         return -ENOENT;
293 }