1/*
2 * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005,2008 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5 * Copyright (c) 2009 HNR Consulting. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses.  You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 *     Redistribution and use in source and binary forms, with or
14 *     without modification, are permitted provided that the following
15 *     conditions are met:
16 *
17 *      - Redistributions of source code must retain the above
18 *        copyright notice, this list of conditions and the following
19 *        disclaimer.
20 *
21 *      - Redistributions in binary form must reproduce the above
22 *        copyright notice, this list of conditions and the following
23 *        disclaimer in the documentation and/or other materials
24 *        provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 *
35 */
36
37/*
38 * Abstract:
39 *    Implementation of osm_si_rcv_t.
40 * This object represents the SwitchInfo Receiver object.
41 * This object is part of the opensm family of objects.
42 */
43
44#if HAVE_CONFIG_H
45#  include <config.h>
46#endif				/* HAVE_CONFIG_H */
47
48#include <string.h>
49#include <iba/ib_types.h>
50#include <complib/cl_qmap.h>
51#include <complib/cl_passivelock.h>
52#include <complib/cl_debug.h>
53#include <opensm/osm_file_ids.h>
54#define FILE_ID OSM_FILE_SW_INFO_RCV_C
55#include <opensm/osm_log.h>
56#include <opensm/osm_switch.h>
57#include <opensm/osm_subnet.h>
58#include <opensm/osm_helper.h>
59#include <opensm/osm_opensm.h>
60
61#if 0
62/**********************************************************************
63 The plock must be held before calling this function.
64**********************************************************************/
65static void si_rcv_get_fwd_tbl(IN osm_sm_t * sm, IN osm_switch_t * p_sw)
66{
67	osm_madw_context_t context;
68	osm_dr_path_t *p_dr_path;
69	osm_physp_t *p_physp;
70	osm_node_t *p_node;
71	uint32_t block_id_ho;
72	uint32_t max_block_id_ho;
73	ib_api_status_t status = IB_SUCCESS;
74
75	OSM_LOG_ENTER(sm->p_log);
76
77	CL_ASSERT(p_sw);
78
79	p_node = p_sw->p_node;
80
81	CL_ASSERT(osm_node_get_type(p_node) == IB_NODE_TYPE_SWITCH);
82
83	context.lft_context.node_guid = osm_node_get_node_guid(p_node);
84	context.lft_context.set_method = FALSE;
85
86	max_block_id_ho = osm_switch_get_max_block_id_in_use(p_sw);
87
88	p_physp = osm_node_get_physp_ptr(p_node, 0);
89	p_dr_path = osm_physp_get_dr_path_ptr(p_physp);
90
91	for (block_id_ho = 0; block_id_ho <= max_block_id_ho; block_id_ho++) {
92		OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
93			"Retrieving FT block %u\n", block_id_ho);
94
95		status = osm_req_get(sm, p_dr_path, IB_MAD_ATTR_LIN_FWD_TBL,
96				     cl_hton32(block_id_ho), TRUE, 0,
97				     CL_DISP_MSGID_NONE, &context);
98		if (status != IB_SUCCESS)
99			/* continue the loop despite the error */
100			OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3603: "
101				"Failure initiating PortInfo request (%s)\n",
102				ib_get_err_str(status));
103	}
104
105	OSM_LOG_EXIT(sm->p_log);
106}
107
108/**********************************************************************
109 The plock must be held before calling this function.
110**********************************************************************/
111static void si_rcv_get_mcast_fwd_tbl(IN osm_sm_t * sm, IN osm_switch_t * p_sw)
112{
113	osm_madw_context_t context;
114	osm_dr_path_t *p_dr_path;
115	osm_physp_t *p_physp;
116	osm_node_t *p_node;
117	osm_mcast_tbl_t *p_tbl;
118	uint32_t block_id_ho;
119	uint32_t max_block_id_ho;
120	uint32_t position;
121	uint32_t max_position;
122	uint32_t attr_mod_ho;
123	ib_api_status_t status = IB_SUCCESS;
124
125	OSM_LOG_ENTER(sm->p_log);
126
127	CL_ASSERT(p_sw);
128
129	p_node = p_sw->p_node;
130
131	CL_ASSERT(osm_node_get_type(p_node) == IB_NODE_TYPE_SWITCH);
132
133	if (osm_switch_get_mcast_fwd_tbl_size(p_sw) == 0) {
134		OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
135			"Multicast not supported by switch 0x%016" PRIx64 "\n",
136			cl_ntoh64(osm_node_get_node_guid(p_node)));
137		goto Exit;
138	}
139
140	context.mft_context.node_guid = osm_node_get_node_guid(p_node);
141	context.mft_context.set_method = FALSE;
142
143	p_tbl = osm_switch_get_mcast_tbl_ptr(p_sw);
144	max_block_id_ho = osm_mcast_tbl_get_max_block(p_tbl);
145
146	if (max_block_id_ho > IB_MCAST_MAX_BLOCK_ID) {
147		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3609: "
148			"Out-of-range mcast block size = %u on switch 0x%016"
149			PRIx64 "\n", max_block_id_ho,
150			cl_ntoh64(osm_node_get_node_guid(p_node)));
151		goto Exit;
152	}
153
154	max_position = osm_mcast_tbl_get_max_position(p_tbl);
155
156	CL_ASSERT(max_position <= IB_MCAST_POSITION_MAX);
157
158	OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
159		"Max MFT block = %u, Max position = %u\n", max_block_id_ho,
160		max_position);
161
162	p_physp = osm_node_get_physp_ptr(p_node, 0);
163	p_dr_path = osm_physp_get_dr_path_ptr(p_physp);
164
165	for (block_id_ho = 0; block_id_ho <= max_block_id_ho; block_id_ho++) {
166		OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
167			"Retrieving MFT block %u\n", block_id_ho);
168
169		for (position = 0; position <= max_position; position++) {
170			OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
171				"Retrieving MFT position %u\n", position);
172
173			attr_mod_ho =
174			    block_id_ho | position << IB_MCAST_POSITION_SHIFT;
175			status =
176			    osm_req_get(sm, p_dr_path,
177					IB_MAD_ATTR_MCAST_FWD_TBL,
178					cl_hton32(attr_mod_ho), TRUE, 0,
179					CL_DISP_MSGID_NONE, &context);
180			if (status != IB_SUCCESS)
181				/* continue the loop despite the error */
182				OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3607: "
183					"Failure initiating PortInfo request (%s)\n",
184					ib_get_err_str(status));
185		}
186	}
187
188Exit:
189	OSM_LOG_EXIT(sm->p_log);
190}
191#endif
192
193/**********************************************************************
194   Lock must be held on entry to this function.
195**********************************************************************/
196static void si_rcv_process_new(IN osm_sm_t * sm, IN osm_node_t * p_node,
197			       IN const osm_madw_t * p_madw)
198{
199	osm_switch_t *p_sw;
200	osm_switch_t *p_check;
201	ib_switch_info_t *p_si;
202	ib_smp_t *p_smp;
203	cl_qmap_t *p_sw_guid_tbl;
204
205	CL_ASSERT(sm);
206
207	OSM_LOG_ENTER(sm->p_log);
208
209	CL_ASSERT(p_madw);
210
211	p_sw_guid_tbl = &sm->p_subn->sw_guid_tbl;
212	p_smp = osm_madw_get_smp_ptr(p_madw);
213	p_si = ib_smp_get_payload_ptr(p_smp);
214
215	osm_dump_switch_info_v2(sm->p_log, p_si, FILE_ID, OSM_LOG_DEBUG);
216
217	p_sw = osm_switch_new(p_node, p_madw);
218	if (p_sw == NULL) {
219		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3608: "
220			"Unable to allocate new switch object\n");
221		goto Exit;
222	}
223
224	/* set subnet max mlid to the minimum MulticastFDBCap of all switches */
225	if (p_si->mcast_cap &&
226	    cl_ntoh16(p_si->mcast_cap) + IB_LID_MCAST_START_HO - 1 <
227	    sm->p_subn->max_mcast_lid_ho) {
228		sm->p_subn->max_mcast_lid_ho = cl_ntoh16(p_si->mcast_cap) +
229			IB_LID_MCAST_START_HO - 1;
230		OSM_LOG(sm->p_log, OSM_LOG_VERBOSE,
231			"Subnet max multicast lid is 0x%X\n",
232			sm->p_subn->max_mcast_lid_ho);
233	}
234
235	/* set subnet max unicast lid to the minimum LinearFDBCap of all switches */
236	if (cl_ntoh16(p_si->lin_cap) < sm->p_subn->max_ucast_lid_ho) {
237		sm->p_subn->max_ucast_lid_ho = cl_ntoh16(p_si->lin_cap);
238		OSM_LOG(sm->p_log, OSM_LOG_VERBOSE,
239			"Subnet max unicast lid is 0x%X\n",
240			sm->p_subn->max_ucast_lid_ho);
241	}
242
243	p_check = (osm_switch_t *) cl_qmap_insert(p_sw_guid_tbl,
244						  osm_node_get_node_guid
245						  (p_node), &p_sw->map_item);
246	if (p_check != p_sw) {
247		/* This shouldn't happen since we hold the lock! */
248		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3605: "
249			"Unable to add new switch object to database\n");
250		osm_switch_delete(&p_sw);
251		goto Exit;
252	}
253
254	p_node->sw = p_sw;
255
256	/* Update the switch info according to the info we just received. */
257	osm_switch_set_switch_info(p_sw, p_si);
258
259#if 0
260	/* Don't bother retrieving the current unicast and multicast tables
261	   from the switches.  The current version of SM does
262	   not support silent take-over of an existing multicast
263	   configuration.
264
265	   Gathering the multicast tables can also generate large amounts
266	   of extra subnet-init traffic.
267
268	   The code to retrieve the tables was fully debugged. */
269
270	si_rcv_get_fwd_tbl(sm, p_sw);
271	if (!sm->p_subn->opt.disable_multicast)
272		si_rcv_get_mcast_fwd_tbl(sm, p_sw);
273#endif
274
275Exit:
276	OSM_LOG_EXIT(sm->p_log);
277}
278
279/**********************************************************************
280   Lock must be held on entry to this function.
281   Return 1 if the caller is expected to send a change_detected event.
282   this can not be done internally as the event needs the lock...
283**********************************************************************/
284static boolean_t si_rcv_process_existing(IN osm_sm_t * sm,
285					 IN osm_node_t * p_node,
286					 IN const osm_madw_t * p_madw)
287{
288	osm_switch_t *p_sw = p_node->sw;
289	ib_switch_info_t *p_si;
290	osm_si_context_t *p_si_context;
291	ib_smp_t *p_smp;
292	osm_epi_lft_change_event_t lft_change;
293	boolean_t is_change_detected = FALSE;
294
295	OSM_LOG_ENTER(sm->p_log);
296
297	CL_ASSERT(p_madw);
298
299	p_smp = osm_madw_get_smp_ptr(p_madw);
300	p_si = ib_smp_get_payload_ptr(p_smp);
301	p_si_context = osm_madw_get_si_context_ptr(p_madw);
302
303	OSM_LOG(sm->p_log, OSM_LOG_DEBUG, "Received logical %cetResp()\n",
304		p_si_context->set_method ? 'S' : 'G');
305
306	osm_switch_set_switch_info(p_sw, p_si);
307
308	if (p_si_context->light_sweep == TRUE && !p_si_context->set_method) {
309		/* If state changed bit is on the mad was returned with an
310		   error - signal a change to the state manager. */
311		if (ib_smp_get_status(p_smp) != 0) {
312			OSM_LOG(sm->p_log, OSM_LOG_VERBOSE,
313				"GetResp() received with error in light sweep. "
314				"Commencing heavy sweep\n");
315			is_change_detected = TRUE;
316		} else if (ib_switch_info_get_state_change(p_si)) {
317			osm_dump_switch_info_v2(sm->p_log, p_si, FILE_ID, OSM_LOG_DEBUG);
318			is_change_detected = TRUE;
319		}
320	}
321
322	if (sm->p_subn->first_time_master_sweep == FALSE &&
323	    p_si_context->set_method && p_si_context->lft_top_change) {
324		lft_change.p_sw = p_sw;
325		lft_change.flags = LFT_CHANGED_LFT_TOP;
326		lft_change.lft_top = cl_ntoh16(p_si->lin_top);
327		lft_change.block_num = 0;
328		osm_opensm_report_event(sm->p_subn->p_osm,
329					OSM_EVENT_ID_LFT_CHANGE,
330					&lft_change);
331	}
332
333	OSM_LOG_EXIT(sm->p_log);
334	return is_change_detected;
335}
336
337static void si_rcv_get_sp0_info(IN osm_sm_t * sm, IN osm_node_t * node)
338{
339	osm_madw_context_t context;
340	osm_physp_t *physp;
341	ib_api_status_t status;
342	int mlnx_epi_supported = 0;
343
344	physp = osm_node_get_physp_ptr(node, 0);
345
346	context.pi_context.node_guid = osm_node_get_node_guid(node);
347	context.pi_context.port_guid = osm_physp_get_port_guid(physp);
348	context.pi_context.set_method = FALSE;
349	context.pi_context.light_sweep = FALSE;
350	context.pi_context.active_transition = FALSE;
351	context.pi_context.client_rereg = FALSE;
352
353	status = osm_req_get(sm, osm_physp_get_dr_path_ptr(physp),
354			     IB_MAD_ATTR_PORT_INFO, 0, TRUE, 0,
355			     CL_DISP_MSGID_NONE, &context);
356	if (status != IB_SUCCESS)
357		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3611: "
358			"Failure initiating PortInfo request (%s)\n",
359			ib_get_err_str(status));
360
361	if (ib_switch_info_is_enhanced_port0(&node->sw->switch_info) &&
362	    sm->p_subn->opt.fdr10) {
363		mlnx_epi_supported = is_mlnx_ext_port_info_supported(
364						ib_node_info_get_vendor_id(&node->node_info),
365						node->node_info.device_id);
366		if (mlnx_epi_supported) {
367			status = osm_req_get(sm,
368					     osm_physp_get_dr_path_ptr(physp),
369					     IB_MAD_ATTR_MLNX_EXTENDED_PORT_INFO,
370					     0, TRUE, 0,
371					     CL_DISP_MSGID_NONE, &context);
372			if (status != IB_SUCCESS)
373				OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3616: "
374					"Failure initiating MLNX ExtPortInfo request (%s)\n",
375					ib_get_err_str(status));
376		}
377	}
378
379}
380
381void osm_si_rcv_process(IN void *context, IN void *data)
382{
383	osm_sm_t *sm = context;
384	osm_madw_t *p_madw = data;
385	ib_switch_info_t *p_si;
386	ib_smp_t *p_smp;
387	osm_node_t *p_node;
388	ib_net64_t node_guid;
389	osm_si_context_t *p_context;
390
391	CL_ASSERT(sm);
392
393	OSM_LOG_ENTER(sm->p_log);
394
395	CL_ASSERT(p_madw);
396
397	p_smp = osm_madw_get_smp_ptr(p_madw);
398	p_si = ib_smp_get_payload_ptr(p_smp);
399	p_context = osm_madw_get_si_context_ptr(p_madw);
400	node_guid = p_context->node_guid;
401
402	OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
403		"Switch GUID 0x%016" PRIx64 ", TID 0x%" PRIx64 "\n",
404		cl_ntoh64(node_guid), cl_ntoh64(p_smp->trans_id));
405
406	if (ib_smp_get_status(p_smp)) {
407		OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
408			"MAD status 0x%x received\n",
409			cl_ntoh16(ib_smp_get_status(p_smp)));
410		goto Exit2;
411	}
412
413	CL_PLOCK_EXCL_ACQUIRE(sm->p_lock);
414
415	p_node = osm_get_node_by_guid(sm->p_subn, node_guid);
416	if (!p_node) {
417		CL_PLOCK_RELEASE(sm->p_lock);
418		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3606: "
419			"SwitchInfo received for nonexistent node "
420			"with GUID 0x%" PRIx64 "\n", cl_ntoh64(node_guid));
421		goto Exit;
422	}
423
424	/* Hack for bad value in Mellanox switch */
425	if (cl_ntoh16(p_si->lin_top) > IB_LID_UCAST_END_HO) {
426		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 3610: "
427			"\n\t\t\t\tBad LinearFDBTop value = 0x%X "
428			"on switch 0x%" PRIx64
429			"\n\t\t\t\tForcing internal correction to 0x%X\n",
430			cl_ntoh16(p_si->lin_top),
431			cl_ntoh64(osm_node_get_node_guid(p_node)), 0);
432		p_si->lin_top = 0;
433	}
434
435	/* Acquire the switch object for this switch. */
436	if (!p_node->sw) {
437		si_rcv_process_new(sm, p_node, p_madw);
438		/* A new switch was found during the sweep so we need
439		   to ignore the current LFT settings. */
440		sm->p_subn->ignore_existing_lfts = TRUE;
441	} else if (si_rcv_process_existing(sm, p_node, p_madw))
442		/* we might get back a request for signaling change was detected */
443		sm->p_subn->force_heavy_sweep = TRUE;
444
445	if (p_context->light_sweep || p_context->set_method)
446		goto Exit;
447
448	si_rcv_get_sp0_info(sm, p_node);
449
450Exit:
451	CL_PLOCK_RELEASE(sm->p_lock);
452Exit2:
453	OSM_LOG_EXIT(sm->p_log);
454}
455