Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / infiniband / ib_sma.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/settings.h>
30 #include <ipxe/infiniband.h>
31 #include <ipxe/iobuf.h>
32 #include <ipxe/ib_mi.h>
33 #include <ipxe/ib_sma.h>
34
35 /**
36  * @file
37  *
38  * Infiniband Subnet Management Agent
39  *
40  */
41
42 /**
43  * Node information
44  *
45  * @v ibdev             Infiniband device
46  * @v mi                Management interface
47  * @v mad               Received MAD
48  * @v av                Source address vector
49  */
50 static void ib_sma_node_info ( struct ib_device *ibdev,
51                                struct ib_mad_interface *mi,
52                                union ib_mad *mad,
53                                struct ib_address_vector *av ) {
54         struct ib_node_info *node_info = &mad->smp.smp_data.node_info;
55         int rc;
56
57         /* Fill in information */
58         memset ( node_info, 0, sizeof ( *node_info ) );
59         node_info->base_version = IB_MGMT_BASE_VERSION;
60         node_info->class_version = IB_SMP_CLASS_VERSION;
61         node_info->node_type = IB_NODE_TYPE_HCA;
62         node_info->num_ports = ib_count_ports ( ibdev );
63         memcpy ( &node_info->sys_guid, &ibdev->node_guid,
64                  sizeof ( node_info->sys_guid ) );
65         memcpy ( &node_info->node_guid, &ibdev->node_guid,
66                  sizeof ( node_info->node_guid ) );
67         memcpy ( &node_info->port_guid, &ibdev->gid.s.guid,
68                  sizeof ( node_info->port_guid ) );
69         node_info->partition_cap = htons ( 1 );
70         node_info->local_port_num = ibdev->port;
71
72         /* Send GetResponse */
73         mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
74         if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
75                 DBGC ( mi, "SMA %p could not send NodeInfo GetResponse: %s\n",
76                        mi, strerror ( rc ) );
77                 return;
78         }
79 }
80
81 /**
82  * Node description
83  *
84  * @v ibdev             Infiniband device
85  * @v mi                Management interface
86  * @v mad               Received MAD
87  * @v av                Source address vector
88  */
89 static void ib_sma_node_desc ( struct ib_device *ibdev,
90                                struct ib_mad_interface *mi,
91                                union ib_mad *mad,
92                                struct ib_address_vector *av ) {
93         struct ib_node_desc *node_desc = &mad->smp.smp_data.node_desc;
94         union ib_guid *guid = &ibdev->node_guid;
95         char hostname[ sizeof ( node_desc->node_string ) ];
96         int hostname_len;
97         int rc;
98
99         /* Fill in information */
100         memset ( node_desc, 0, sizeof ( *node_desc ) );
101         hostname_len = fetch_string_setting ( NULL, &hostname_setting,
102                                               hostname, sizeof ( hostname ) );
103         snprintf ( node_desc->node_string, sizeof ( node_desc->node_string ),
104                    "iPXE %s%s%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x (%s)",
105                    hostname, ( ( hostname_len >= 0 ) ? " " : "" ),
106                    guid->bytes[0], guid->bytes[1], guid->bytes[2],
107                    guid->bytes[3], guid->bytes[4], guid->bytes[5],
108                    guid->bytes[6], guid->bytes[7], ibdev->dev->name );
109
110         /* Send GetResponse */
111         mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
112         if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
113                 DBGC ( mi, "SMA %p could not send NodeDesc GetResponse: %s\n",
114                        mi, strerror ( rc ) );
115                 return;
116         }
117 }
118
119 /**
120  * GUID information
121  *
122  * @v ibdev             Infiniband device
123  * @v mi                Management interface
124  * @v mad               Received MAD
125  * @v av                Source address vector
126  */
127 static void ib_sma_guid_info ( struct ib_device *ibdev,
128                                struct ib_mad_interface *mi,
129                                union ib_mad *mad,
130                                struct ib_address_vector *av ) {
131         struct ib_guid_info *guid_info = &mad->smp.smp_data.guid_info;
132         int rc;
133
134         /* Fill in information */
135         memset ( guid_info, 0, sizeof ( *guid_info ) );
136         memcpy ( guid_info->guid[0], &ibdev->gid.s.guid,
137                  sizeof ( guid_info->guid[0] ) );
138
139         /* Send GetResponse */
140         mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
141         if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
142                 DBGC ( mi, "SMA %p could not send GuidInfo GetResponse: %s\n",
143                        mi, strerror ( rc ) );
144                 return;
145         }
146 }
147
148 /**
149  * Set port information
150  *
151  * @v ibdev             Infiniband device
152  * @v mi                Management interface
153  * @v mad               Received MAD
154  * @ret rc              Return status code
155  */
156 static int ib_sma_set_port_info ( struct ib_device *ibdev,
157                                   struct ib_mad_interface *mi,
158                                   union ib_mad *mad ) {
159         const struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
160         unsigned int link_width_enabled;
161         unsigned int link_speed_enabled;
162         int rc;
163
164         /* Set parameters */
165         memcpy ( &ibdev->gid.s.prefix, port_info->gid_prefix,
166                  sizeof ( ibdev->gid.s.prefix ) );
167         ibdev->lid = ntohs ( port_info->lid );
168         ibdev->sm_lid = ntohs ( port_info->mastersm_lid );
169         if ( ( link_width_enabled = port_info->link_width_enabled ) )
170                 ibdev->link_width_enabled = link_width_enabled;
171         if ( ( link_speed_enabled =
172                ( port_info->link_speed_active__link_speed_enabled & 0xf ) ) )
173                 ibdev->link_speed_enabled = link_speed_enabled;
174         ibdev->sm_sl = ( port_info->neighbour_mtu__mastersm_sl & 0xf );
175         DBGC ( mi, "SMA %p set LID %04x SMLID %04x link width %02x speed "
176                "%02x\n", mi, ibdev->lid, ibdev->sm_lid,
177                ibdev->link_width_enabled, ibdev->link_speed_enabled );
178
179         /* Update parameters on device */
180         if ( ( rc = ib_set_port_info ( ibdev, mad ) ) != 0 ) {
181                 DBGC ( mi, "SMA %p could not set port information: %s\n",
182                        mi, strerror ( rc ) );
183                 return rc;
184         }
185
186         return 0;
187 }
188
189 /**
190  * Port information
191  *
192  * @v ibdev             Infiniband device
193  * @v mi                Management interface
194  * @v mad               Received MAD
195  * @v av                Source address vector
196  */
197 static void ib_sma_port_info ( struct ib_device *ibdev,
198                                struct ib_mad_interface *mi,
199                                union ib_mad *mad,
200                                struct ib_address_vector *av ) {
201         struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
202         int rc;
203
204         /* Set parameters if applicable */
205         if ( mad->hdr.method == IB_MGMT_METHOD_SET ) {
206                 if ( ( rc = ib_sma_set_port_info ( ibdev, mi, mad ) ) != 0 ) {
207                         mad->hdr.status =
208                               htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
209                         /* Fall through to generate GetResponse */
210                 }
211         }
212
213         /* Fill in information */
214         memset ( port_info, 0, sizeof ( *port_info ) );
215         memcpy ( port_info->gid_prefix, &ibdev->gid.s.prefix,
216                  sizeof ( port_info->gid_prefix ) );
217         port_info->lid = ntohs ( ibdev->lid );
218         port_info->mastersm_lid = ntohs ( ibdev->sm_lid );
219         port_info->local_port_num = ibdev->port;
220         port_info->link_width_enabled = ibdev->link_width_enabled;
221         port_info->link_width_supported = ibdev->link_width_supported;
222         port_info->link_width_active = ibdev->link_width_active;
223         port_info->link_speed_supported__port_state =
224                 ( ( ibdev->link_speed_supported << 4 ) | ibdev->port_state );
225         port_info->port_phys_state__link_down_def_state =
226                 ( ( IB_PORT_PHYS_STATE_POLLING << 4 ) |
227                   IB_PORT_PHYS_STATE_POLLING );
228         port_info->link_speed_active__link_speed_enabled =
229                 ( ( ibdev->link_speed_active << 4 ) |
230                   ibdev->link_speed_enabled );
231         port_info->neighbour_mtu__mastersm_sl =
232                 ( ( IB_MTU_2048 << 4 ) | ibdev->sm_sl );
233         port_info->vl_cap__init_type = ( IB_VL_0 << 4 );
234         port_info->init_type_reply__mtu_cap = IB_MTU_2048;
235         port_info->operational_vls__enforcement = ( IB_VL_0 << 4 );
236         port_info->guid_cap = 1;
237
238         /* Send GetResponse */
239         mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
240         if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
241                 DBGC ( mi, "SMA %p could not send PortInfo GetResponse: %s\n",
242                        mi, strerror ( rc ) );
243                 return;
244         }
245 }
246
247 /**
248  * Set partition key table
249  *
250  * @v ibdev             Infiniband device
251  * @v mi                Management interface
252  * @v mad               Received MAD
253  * @ret rc              Return status code
254  */
255 static int ib_sma_set_pkey_table ( struct ib_device *ibdev,
256                                    struct ib_mad_interface *mi,
257                                    union ib_mad *mad ) {
258         struct ib_pkey_table *pkey_table = &mad->smp.smp_data.pkey_table;
259         int rc;
260
261         /* Set parameters */
262         ibdev->pkey = ntohs ( pkey_table->pkey[0] );
263         DBGC ( mi, "SMA %p set pkey %04x\n", mi, ibdev->pkey );
264
265         /* Update parameters on device */
266         if ( ( rc = ib_set_pkey_table ( ibdev, mad ) ) != 0 ) {
267                 DBGC ( mi, "SMA %p could not set pkey table: %s\n",
268                        mi, strerror ( rc ) );
269                 return rc;
270         }
271
272         return 0;
273 }
274
275 /**
276  * Partition key table
277  *
278  * @v ibdev             Infiniband device
279  * @v mi                Management interface
280  * @v mad               Received MAD
281  * @v av                Source address vector
282  */
283 static void ib_sma_pkey_table ( struct ib_device *ibdev,
284                                 struct ib_mad_interface *mi,
285                                 union ib_mad *mad,
286                                 struct ib_address_vector *av ) {
287         struct ib_pkey_table *pkey_table = &mad->smp.smp_data.pkey_table;
288         int rc;
289
290         /* Set parameters, if applicable */
291         if ( mad->hdr.method == IB_MGMT_METHOD_SET ) {
292                 if ( ( rc = ib_sma_set_pkey_table ( ibdev, mi, mad ) ) != 0 ) {
293                         mad->hdr.status =
294                               htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
295                         /* Fall through to generate GetResponse */
296                 }
297         }
298
299         /* Fill in information */
300         mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
301         memset ( pkey_table, 0, sizeof ( *pkey_table ) );
302         pkey_table->pkey[0] = htons ( ibdev->pkey );
303
304         /* Send GetResponse */
305         mad->hdr.method = IB_MGMT_METHOD_GET_RESP;
306         if ( ( rc = ib_mi_send ( ibdev, mi, mad, av ) ) != 0 ) {
307                 DBGC ( mi, "SMA %p could not send PKeyTable GetResponse: %s\n",
308                        mi, strerror ( rc ) );
309                 return;
310         }
311 }
312
313 /** Subnet management agent */
314 struct ib_mad_agent ib_sma_agent[] __ib_mad_agent = {
315         {
316                 .mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
317                 .class_version = IB_SMP_CLASS_VERSION,
318                 .attr_id = htons ( IB_SMP_ATTR_NODE_INFO ),
319                 .handle = ib_sma_node_info,
320         },
321         {
322                 .mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
323                 .class_version = IB_SMP_CLASS_VERSION,
324                 .attr_id = htons ( IB_SMP_ATTR_NODE_DESC ),
325                 .handle = ib_sma_node_desc,
326         },
327         {
328                 .mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
329                 .class_version = IB_SMP_CLASS_VERSION,
330                 .attr_id = htons ( IB_SMP_ATTR_GUID_INFO ),
331                 .handle = ib_sma_guid_info,
332         },
333         {
334                 .mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
335                 .class_version = IB_SMP_CLASS_VERSION,
336                 .attr_id = htons ( IB_SMP_ATTR_PORT_INFO ),
337                 .handle = ib_sma_port_info,
338         },
339         {
340                 .mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED,
341                 .class_version = IB_SMP_CLASS_VERSION,
342                 .attr_id = htons ( IB_SMP_ATTR_PKEY_TABLE ),
343                 .handle = ib_sma_pkey_table,
344         },
345 };
346
347 /**
348  * Create subnet management agent and interface
349  *
350  * @v ibdev             Infiniband device
351  * @v mi                Management interface
352  * @ret rc              Return status code
353  */
354 int ib_create_sma ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {
355
356         /* Nothing to do */
357         DBGC ( ibdev, "IBDEV %p SMA using SMI %p\n", ibdev, mi );
358
359         return 0;
360 }
361
362 /**
363  * Destroy subnet management agent and interface
364  *
365  * @v ibdev             Infiniband device
366  * @v mi                Management interface
367  */
368 void ib_destroy_sma ( struct ib_device *ibdev __unused,
369                       struct ib_mad_interface *mi __unused ) {
370         /* Nothing to do */
371 }