1/*
2 * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 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_slvl_rcv_t.
40 * This object represents the SLtoVL 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_passivelock.h>
51#include <complib/cl_debug.h>
52#include <opensm/osm_file_ids.h>
53#define FILE_ID OSM_FILE_SLVL_MAP_RCV_C
54#include <opensm/osm_madw.h>
55#include <opensm/osm_log.h>
56#include <opensm/osm_node.h>
57#include <opensm/osm_subnet.h>
58#include <opensm/osm_helper.h>
59#include <opensm/osm_sm.h>
60
61/*
62 * WE ONLY RECEIVE GET or SET responses
63 */
64void osm_slvl_rcv_process(IN void *context, IN void *p_data)
65{
66	osm_sm_t *sm = context;
67	osm_madw_t *p_madw = p_data;
68	ib_slvl_table_t *p_slvl_tbl;
69	ib_smp_t *p_smp;
70	osm_port_t *p_port;
71	osm_physp_t *p_physp;
72	osm_node_t *p_node;
73	osm_slvl_context_t *p_context;
74	ib_net64_t port_guid;
75	ib_net64_t node_guid;
76	uint32_t attr_mod;
77	uint8_t startinport, endinport, startoutport, endoutport;
78	uint8_t in_port, out_port;
79
80	CL_ASSERT(sm);
81
82	OSM_LOG_ENTER(sm->p_log);
83
84	CL_ASSERT(p_madw);
85
86	p_smp = osm_madw_get_smp_ptr(p_madw);
87	p_context = osm_madw_get_slvl_context_ptr(p_madw);
88	p_slvl_tbl = ib_smp_get_payload_ptr(p_smp);
89
90	port_guid = p_context->port_guid;
91	node_guid = p_context->node_guid;
92
93	CL_ASSERT(p_smp->attr_id == IB_MAD_ATTR_SLVL_TABLE);
94
95	if (!sm->p_subn->opt.suppress_sl2vl_mad_status_errors &&
96	    ib_smp_get_status(p_smp)) {
97		OSM_LOG(sm->p_log, OSM_LOG_DEBUG,
98			"MAD status 0x%x received\n",
99			cl_ntoh16(ib_smp_get_status(p_smp)));
100		goto Exit2;
101	}
102
103	cl_plock_excl_acquire(sm->p_lock);
104	p_port = osm_get_port_by_guid(sm->p_subn, port_guid);
105
106	if (!p_port) {
107		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 2C06: "
108			"No port object for port with GUID 0x%" PRIx64
109			"\n\t\t\t\tfor parent node GUID 0x%" PRIx64
110			", TID 0x%" PRIx64 "\n", cl_ntoh64(port_guid),
111			cl_ntoh64(node_guid), cl_ntoh64(p_smp->trans_id));
112		goto Exit;
113	}
114
115	p_node = p_port->p_node;
116	CL_ASSERT(p_node);
117
118	/* in case of a non switch node the attr modifier should be ignored */
119	if (osm_node_get_type(p_node) == IB_NODE_TYPE_SWITCH) {
120		unsigned num_ports = osm_node_get_num_physp(p_node) - 1;
121		attr_mod = cl_ntoh32(p_smp->attr_mod);
122
123		if (attr_mod & 0x10000) {
124			startoutport = ib_switch_info_is_enhanced_port0(&p_node->sw->switch_info) ? 0 : 1;
125			endoutport = osm_node_get_num_physp(p_node) - 1;
126		} else
127			startoutport = endoutport = attr_mod & 0xff;
128
129		if (attr_mod & 0x20000) {
130			startinport = ib_switch_info_is_enhanced_port0(&p_node->sw->switch_info) ? 0 : 1;
131			endinport = osm_node_get_num_physp(p_node) - 1;
132		} else
133			startinport = endinport = (attr_mod >> 8) & 0xff;
134
135		if (startinport > num_ports || startoutport > num_ports) {
136			OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 2C07"
137				"Invalid attribute modifier 0x%x received in"
138				" response from switch 0x%" PRIx64 "\n",
139				cl_ntoh32(attr_mod), cl_ntoh64(node_guid));
140			goto Exit;
141		}
142
143	} else {
144		startoutport = endoutport = p_port->p_physp->port_num;
145		startinport = endinport = 0;
146	}
147
148	OSM_LOG(sm->p_log, OSM_LOG_DEBUG, "Received SLtoVL GetResp"
149		" in_port_num %u out_port_num %u with GUID 0x%" PRIx64
150		" for parent node GUID 0x%" PRIx64 ", TID 0x%" PRIx64 "\n",
151		startinport == endinport ? startinport : 0xff,
152		startoutport == endoutport ? startoutport : 0xff,
153		cl_ntoh64(port_guid), cl_ntoh64(node_guid),
154		cl_ntoh64(p_smp->trans_id));
155
156	osm_dump_slvl_map_table_v2(sm->p_log, port_guid,
157				   startinport == endinport ? startinport : 0xff,
158				   startoutport == endoutport ? startoutport : 0xff,
159				   p_slvl_tbl, FILE_ID, OSM_LOG_DEBUG);
160
161	for (out_port = startoutport; out_port <= endoutport; out_port++) {
162		p_physp = osm_node_get_physp_ptr(p_node, out_port);
163		for (in_port = startinport; in_port <= endinport; in_port++)
164			osm_physp_set_slvl_tbl(p_physp, p_slvl_tbl, in_port);
165	}
166
167Exit:
168	cl_plock_release(sm->p_lock);
169
170Exit2:
171	OSM_LOG_EXIT(sm->p_log);
172}
173