summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/disabled/gpxe/src/net/infiniband/ib_pathrec.c
blob: 136e628d295366cc62d47ac8d22e8fe5dcc366f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <gpxe/infiniband.h>
#include <gpxe/ib_mi.h>
#include <gpxe/ib_pathrec.h>

/** @file
 *
 * Infiniband path lookups
 *
 */

/**
 * Handle path transaction completion
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v madx		Management transaction
 * @v rc		Status code
 * @v mad		Received MAD (or NULL on error)
 * @v av		Source address vector (or NULL on error)
 */
static void ib_path_complete ( struct ib_device *ibdev,
			       struct ib_mad_interface *mi,
			       struct ib_mad_transaction *madx,
			       int rc, union ib_mad *mad,
			       struct ib_address_vector *av __unused ) {
	struct ib_path *path = ib_madx_get_ownerdata ( madx );
	struct ib_gid *dgid = &path->av.gid;
	struct ib_path_record *pathrec = &mad->sa.sa_data.path_record;

	/* Report failures */
	if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
		rc = -ENETUNREACH;
	if ( rc != 0 ) {
		DBGC ( ibdev, "IBDEV %p path lookup for %08x:%08x:%08x:%08x "
		       "failed: %s\n", ibdev, htonl ( dgid->u.dwords[0] ),
		       htonl ( dgid->u.dwords[1] ),
		       htonl ( dgid->u.dwords[2] ),
		       htonl ( dgid->u.dwords[3] ), strerror ( rc ) );
		goto out;
	}

	/* Extract values from MAD */
	path->av.lid = ntohs ( pathrec->dlid );
	path->av.sl = ( pathrec->reserved__sl & 0x0f );
	path->av.rate = ( pathrec->rate_selector__rate & 0x3f );
	DBGC ( ibdev, "IBDEV %p path to %08x:%08x:%08x:%08x is %04x sl %d "
	       "rate %d\n", ibdev, htonl ( dgid->u.dwords[0] ),
	       htonl ( dgid->u.dwords[1] ), htonl ( dgid->u.dwords[2] ),
	       htonl ( dgid->u.dwords[3] ), path->av.lid, path->av.sl,
	       path->av.rate );

 out:
	/* Destroy the completed transaction */
	ib_destroy_madx ( ibdev, mi, madx );
	path->madx = NULL;

	/* Hand off to upper completion handler */
	path->op->complete ( ibdev, path, rc, &path->av );
}

/** Path transaction completion operations */
static struct ib_mad_transaction_operations ib_path_op = {
	.complete = ib_path_complete,
};

/**
 * Create path
 *
 * @v ibdev		Infiniband device
 * @v av		Address vector to complete
 * @v op		Path operations
 * @ret path		Path
 */
struct ib_path *
ib_create_path ( struct ib_device *ibdev, struct ib_address_vector *av,
		 struct ib_path_operations *op ) {
	struct ib_path *path;
	union ib_mad mad;
	struct ib_mad_sa *sa = &mad.sa;

	/* Allocate and initialise structure */
	path = zalloc ( sizeof ( *path ) );
	if ( ! path )
		goto err_alloc_path;
	path->ibdev = ibdev;
	memcpy ( &path->av, av, sizeof ( path->av ) );
	path->op = op;

	/* Construct path request */
	memset ( sa, 0, sizeof ( *sa ) );
	sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
	sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
	sa->mad_hdr.method = IB_MGMT_METHOD_GET;
	sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
	sa->sa_hdr.comp_mask[1] =
		htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
	memcpy ( &sa->sa_data.path_record.dgid, &path->av.gid,
		 sizeof ( sa->sa_data.path_record.dgid ) );
	memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
		 sizeof ( sa->sa_data.path_record.sgid ) );

	/* Create management transaction */
	path->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
				      &ib_path_op );
	if ( ! path->madx )
		goto err_create_madx;
	ib_madx_set_ownerdata ( path->madx, path );

	return path;

	ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
 err_create_madx:
	free ( path );
 err_alloc_path:
	return NULL;
}

/**
 * Destroy path
 *
 * @v ibdev		Infiniband device
 * @v path		Path
 */
void ib_destroy_path ( struct ib_device *ibdev, struct ib_path *path ) {

	if ( path->madx )
		ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
	free ( path );
}

/** Number of path cache entries
 *
 * Must be a power of two.
 */
#define IB_NUM_CACHED_PATHS 4

/** A cached path */
struct ib_cached_path {
	/** Path */
	struct ib_path *path;
};

/** Path cache */
static struct ib_cached_path ib_path_cache[IB_NUM_CACHED_PATHS];

/** Oldest path cache entry index */
static unsigned int ib_path_cache_idx;

/**
 * Find path cache entry
 *
 * @v ibdev		Infiniband device
 * @v dgid		Destination GID
 * @ret path		Path cache entry, or NULL
 */
static struct ib_cached_path *
ib_find_path_cache_entry ( struct ib_device *ibdev, struct ib_gid *dgid ) {
	struct ib_cached_path *cached;
	unsigned int i;

	for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
		cached = &ib_path_cache[i];
		if ( ! cached->path )
			continue;
		if ( cached->path->ibdev != ibdev )
			continue;
		if ( memcmp ( &cached->path->av.gid, dgid,
			      sizeof ( cached->path->av.gid ) ) != 0 )
			continue;
		return cached;
	}

	return NULL;
}

/**
 * Handle cached path transaction completion
 *
 * @v ibdev		Infiniband device
 * @v path		Path
 * @v rc		Status code
 * @v av		Address vector, or NULL on error
 */
static void ib_cached_path_complete ( struct ib_device *ibdev,
				      struct ib_path *path, int rc,
				      struct ib_address_vector *av __unused ) {
	struct ib_cached_path *cached = ib_path_get_ownerdata ( path );

	/* If the transaction failed, erase the cache entry */
	if ( rc != 0 ) {
		/* Destroy the old cache entry */
		ib_destroy_path ( ibdev, path );
		memset ( cached, 0, sizeof ( *cached ) );
		return;
	}

	/* Do not destroy the completed transaction; we still need to
	 * refer to the resolved path.
	 */
}

/** Cached path transaction completion operations */
static struct ib_path_operations ib_cached_path_op = {
	.complete = ib_cached_path_complete,
};

/**
 * Resolve path
 *
 * @v ibdev		Infiniband device
 * @v av		Address vector to complete
 * @ret rc		Return status code
 *
 * This provides a non-transactional way to resolve a path, via a
 * cache similar to ARP.
 */
int ib_resolve_path ( struct ib_device *ibdev, struct ib_address_vector *av ) {
	struct ib_gid *gid = &av->gid;
	struct ib_cached_path *cached;
	unsigned int cache_idx;

	/* Sanity check */
	if ( ! av->gid_present ) {
		DBGC ( ibdev, "IBDEV %p attempt to look up path "
		       "without GID\n", ibdev );
		return -EINVAL;
	}

	/* Look in cache for a matching entry */
	cached = ib_find_path_cache_entry ( ibdev, gid );
	if ( cached && cached->path->av.lid ) {
		/* Populated entry found */
		av->lid = cached->path->av.lid;
		av->rate = cached->path->av.rate;
		av->sl = cached->path->av.sl;
		DBGC2 ( ibdev, "IBDEV %p cache hit for %08x:%08x:%08x:%08x\n",
			ibdev, htonl ( gid->u.dwords[0] ),
			htonl ( gid->u.dwords[1] ), htonl ( gid->u.dwords[2] ),
			htonl ( gid->u.dwords[3] ) );
		return 0;
	}
	DBGC ( ibdev, "IBDEV %p cache miss for %08x:%08x:%08x:%08x%s\n",
	       ibdev, htonl ( gid->u.dwords[0] ), htonl ( gid->u.dwords[1] ),
	       htonl ( gid->u.dwords[2] ), htonl ( gid->u.dwords[3] ),
	       ( cached ? " (in progress)" : "" ) );

	/* If lookup is already in progress, do nothing */
	if ( cached )
		return -ENOENT;

	/* Locate a new cache entry to use */
	cache_idx = ( (ib_path_cache_idx++) % IB_NUM_CACHED_PATHS );
	cached = &ib_path_cache[cache_idx];

	/* Destroy the old cache entry */
	if ( cached->path )
		ib_destroy_path ( ibdev, cached->path );
	memset ( cached, 0, sizeof ( *cached ) );

	/* Create new path */
	cached->path = ib_create_path ( ibdev, av, &ib_cached_path_op );
	if ( ! cached->path ) {
		DBGC ( ibdev, "IBDEV %p could not create path\n",
		       ibdev );
		return -ENOMEM;
	}
	ib_path_set_ownerdata ( cached->path, cached );

	/* Not found yet */
	return -ENOENT;
}