summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/disabled/gpxe/src/net/infiniband/ib_sma.c
blob: eb4f987aca5d2a27e52c0909b3a039e31bc0e67f (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * 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 <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <byteswap.h>
#include <gpxe/infiniband.h>
#include <gpxe/iobuf.h>
#include <gpxe/ib_mi.h>
#include <gpxe/ib_sma.h>

/**
 * @file
 *
 * Infiniband Subnet Management Agent
 *
 */

/**
 * Node information
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @v av		Source address vector
 */
static void ib_sma_node_info ( struct ib_device *ibdev,
			       struct ib_mad_interface *mi,
			       union ib_mad *mad,
			       struct ib_address_vector *av ) {
	struct ib_node_info *node_info = &mad->smp.smp_data.node_info;
	int rc;

	/* Fill in information */
	memset ( node_info, 0, sizeof ( *node_info ) );
	node_info->base_version = IB_MGMT_BASE_VERSION;
	node_info->class_version = IB_SMP_CLASS_VERSION;
	node_info->node_type = IB_NODE_TYPE_HCA;
	node_info->num_ports = ib_get_hca_info ( ibdev, &node_info->sys_guid );
	memcpy ( &node_info->node_guid, &node_info->sys_guid,
		 sizeof ( node_info->node_guid ) );
	memcpy ( &node_info->port_guid, &ibdev->gid.u.half[1],
		 sizeof ( node_info->port_guid ) );
	node_info->partition_cap = htons ( 1 );
	node_info->local_port_num = ibdev->port;

	/* Send GetResponse */
	mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
	if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not send NodeInfo GetResponse: %s\n",
		       mi, strerror ( rc ) );
		return;
	}
}

/**
 * Node description
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @v av		Source address vector
 */
static void ib_sma_node_desc ( struct ib_device *ibdev,
			       struct ib_mad_interface *mi,
			       union ib_mad *mad,
			       struct ib_address_vector *av ) {
	struct ib_node_desc *node_desc = &mad->smp.smp_data.node_desc;
	struct ib_gid_half *guid = &ibdev->gid.u.half[1];
	int rc;

	/* Fill in information */
	memset ( node_desc, 0, sizeof ( *node_desc ) );
	snprintf ( node_desc->node_string, sizeof ( node_desc->node_string ),
		   "gPXE %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x (%s)",
		   guid->u.bytes[0], guid->u.bytes[1], guid->u.bytes[2],
		   guid->u.bytes[3], guid->u.bytes[4], guid->u.bytes[5],
		   guid->u.bytes[6], guid->u.bytes[7], ibdev->dev->name );

	/* Send GetResponse */
	mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
	if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not send NodeDesc GetResponse: %s\n",
		       mi, strerror ( rc ) );
		return;
	}
}

/**
 * GUID information
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @v av		Source address vector
 */
static void ib_sma_guid_info ( struct ib_device *ibdev,
			       struct ib_mad_interface *mi,
			       union ib_mad *mad,
			       struct ib_address_vector *av ) {
	struct ib_guid_info *guid_info = &mad->smp.smp_data.guid_info;
	int rc;

	/* Fill in information */
	memset ( guid_info, 0, sizeof ( *guid_info ) );
	memcpy ( guid_info->guid[0], &ibdev->gid.u.half[1],
		 sizeof ( guid_info->guid[0] ) );

	/* Send GetResponse */
	mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
	if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not send GuidInfo GetResponse: %s\n",
		       mi, strerror ( rc ) );
		return;
	}
}

/**
 * Set port information
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @ret rc		Return status code
 */
static int ib_sma_set_port_info ( struct ib_device *ibdev,
				  struct ib_mad_interface *mi,
				  union ib_mad *mad ) {
	const struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
	unsigned int link_width_enabled;
	unsigned int link_speed_enabled;
	int rc;

	/* Set parameters */
	memcpy ( &ibdev->gid.u.half[0], port_info->gid_prefix,
		 sizeof ( ibdev->gid.u.half[0] ) );
	ibdev->lid = ntohs ( port_info->lid );
	ibdev->sm_lid = ntohs ( port_info->mastersm_lid );
	if ( ( link_width_enabled = port_info->link_width_enabled ) )
		ibdev->link_width_enabled = link_width_enabled;
	if ( ( link_speed_enabled =
	       ( port_info->link_speed_active__link_speed_enabled & 0xf ) ) )
		ibdev->link_speed_enabled = link_speed_enabled;
	ibdev->sm_sl = ( port_info->neighbour_mtu__mastersm_sl & 0xf );
	DBGC ( mi, "SMA %p set LID %04x SMLID %04x link width %02x speed "
	       "%02x\n", mi, ibdev->lid, ibdev->sm_lid,
	       ibdev->link_width_enabled, ibdev->link_speed_enabled );

	/* Update parameters on device */
	if ( ( rc = ib_set_port_info ( ibdev, mad ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not set port information: %s\n",
		       mi, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Port information
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @v av		Source address vector
 */
static void ib_sma_port_info ( struct ib_device *ibdev,
			       struct ib_mad_interface *mi,
			       union ib_mad *mad,
			       struct ib_address_vector *av ) {
	struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
	int rc;

	/* Set parameters if applicable */
	if ( mad->hdr.method == IB_MGMT_METHOD_SET ) {
		if ( ( rc = ib_sma_set_port_info ( ibdev, mi, mad ) ) != 0 ) {
			mad->hdr.status =
			      htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
			/* Fall through to generate GetResponse */
		}
	}

	/* Fill in information */
	memset ( port_info, 0, sizeof ( *port_info ) );
	memcpy ( port_info->gid_prefix, &ibdev->gid.u.half[0],
		 sizeof ( port_info->gid_prefix ) );
	port_info->lid = ntohs ( ibdev->lid );
	port_info->mastersm_lid = ntohs ( ibdev->sm_lid );
	port_info->local_port_num = ibdev->port;
	port_info->link_width_enabled = ibdev->link_width_enabled;
	port_info->link_width_supported = ibdev->link_width_supported;
	port_info->link_width_active = ibdev->link_width_active;
	port_info->link_speed_supported__port_state =
		( ( ibdev->link_speed_supported << 4 ) | ibdev->port_state );
	port_info->port_phys_state__link_down_def_state =
		( ( IB_PORT_PHYS_STATE_POLLING << 4 ) |
		  IB_PORT_PHYS_STATE_POLLING );
	port_info->link_speed_active__link_speed_enabled =
		( ( ibdev->link_speed_active << 4 ) |
		  ibdev->link_speed_enabled );
	port_info->neighbour_mtu__mastersm_sl =
		( ( IB_MTU_2048 << 4 ) | ibdev->sm_sl );
	port_info->vl_cap__init_type = ( IB_VL_0 << 4 );
	port_info->init_type_reply__mtu_cap = IB_MTU_2048;
	port_info->operational_vls__enforcement = ( IB_VL_0 << 4 );
	port_info->guid_cap = 1;

	/* Send GetResponse */
	mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
	if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not send PortInfo GetResponse: %s\n",
		       mi, strerror ( rc ) );
		return;
	}
}

/**
 * Set partition key table
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @ret rc		Return status code
 */
static int ib_sma_set_pkey_table ( struct ib_device *ibdev,
				   struct ib_mad_interface *mi,
				   union ib_mad *mad ) {
	struct ib_pkey_table *pkey_table = &mad->smp.smp_data.pkey_table;
	int rc;

	/* Set parameters */
	ibdev->pkey = ntohs ( pkey_table->pkey[0] );
	DBGC ( mi, "SMA %p set pkey %04x\n", mi, ibdev->pkey );

	/* Update parameters on device */
	if ( ( rc = ib_set_pkey_table ( ibdev, mad ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not set pkey table: %s\n",
		       mi, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Partition key table
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @v mad		Received MAD
 * @v av		Source address vector
 */
static void ib_sma_pkey_table ( struct ib_device *ibdev,
				struct ib_mad_interface *mi,
				union ib_mad *mad,
				struct ib_address_vector *av ) {
	struct ib_pkey_table *pkey_table = &mad->smp.smp_data.pkey_table;
	int rc;

	/* Set parameters, if applicable */
	if ( mad->hdr.method == IB_MGMT_METHOD_SET ) {
		if ( ( rc = ib_sma_set_pkey_table ( ibdev, mi, mad ) ) != 0 ) {
			mad->hdr.status =
			      htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
			/* Fall through to generate GetResponse */
		}
	}

	/* Fill in information */
	mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
	memset ( pkey_table, 0, sizeof ( *pkey_table ) );
	pkey_table->pkey[0] = htons ( ibdev->pkey );

	/* Send GetResponse */
	mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
	if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
		DBGC ( mi, "SMA %p could not send PKeyTable GetResponse: %s\n",
		       mi, strerror ( rc ) );
		return;
	}
}

/** Subnet management agent */
struct ib_mad_agent ib_sma_agent[] __ib_mad_agent = {
	{
		.mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
		.class_version = IB_SMP_CLASS_VERSION,
		.attr_id = htons ( IB_SMP_ATTR_NODE_INFO ),
		.handle = ib_sma_node_info,
	},
	{
		.mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
		.class_version = IB_SMP_CLASS_VERSION,
		.attr_id = htons ( IB_SMP_ATTR_NODE_DESC ),
		.handle = ib_sma_node_desc,
	},
	{
		.mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
		.class_version = IB_SMP_CLASS_VERSION,
		.attr_id = htons ( IB_SMP_ATTR_GUID_INFO ),
		.handle = ib_sma_guid_info,
	},
	{
		.mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
		.class_version = IB_SMP_CLASS_VERSION,
		.attr_id = htons ( IB_SMP_ATTR_PORT_INFO ),
		.handle = ib_sma_port_info,
	},
	{
		.mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
		.class_version = IB_SMP_CLASS_VERSION,
		.attr_id = htons ( IB_SMP_ATTR_PKEY_TABLE ),
		.handle = ib_sma_pkey_table,
	},
};

/**
 * Create subnet management agent and interface
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 * @ret rc		Return status code
 */
int ib_create_sma ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {

	/* Nothing to do */
	DBGC ( ibdev, "IBDEV %p SMA using SMI %p\n", ibdev, mi );

	return 0;
}

/**
 * Destroy subnet management agent and interface
 *
 * @v ibdev		Infiniband device
 * @v mi		Management interface
 */
void ib_destroy_sma ( struct ib_device *ibdev __unused,
		      struct ib_mad_interface *mi __unused ) {
	/* Nothing to do */
}