1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff/*
37219820Sjeff * Abstract:
38219820Sjeff *    Implementation of osm_slvl_rec_rcv_t.
39219820Sjeff * This object represents the SLtoVL Mapping Query Receiver object.
40219820Sjeff * This object is part of the opensm family of objects.
41219820Sjeff */
42219820Sjeff
43219820Sjeff#if HAVE_CONFIG_H
44219820Sjeff#  include <config.h>
45219820Sjeff#endif				/* HAVE_CONFIG_H */
46219820Sjeff
47219820Sjeff#include <string.h>
48219820Sjeff#include <iba/ib_types.h>
49219820Sjeff#include <complib/cl_qmap.h>
50219820Sjeff#include <complib/cl_passivelock.h>
51219820Sjeff#include <complib/cl_debug.h>
52219820Sjeff#include <complib/cl_qlist.h>
53219820Sjeff#include <vendor/osm_vendor_api.h>
54219820Sjeff#include <opensm/osm_port.h>
55219820Sjeff#include <opensm/osm_node.h>
56219820Sjeff#include <opensm/osm_helper.h>
57219820Sjeff#include <opensm/osm_pkey.h>
58219820Sjeff#include <opensm/osm_sa.h>
59219820Sjeff
60219820Sjefftypedef struct osm_slvl_item {
61219820Sjeff	cl_list_item_t list_item;
62219820Sjeff	ib_slvl_table_record_t rec;
63219820Sjeff} osm_slvl_item_t;
64219820Sjeff
65219820Sjefftypedef struct osm_slvl_search_ctxt {
66219820Sjeff	const ib_slvl_table_record_t *p_rcvd_rec;
67219820Sjeff	ib_net64_t comp_mask;
68219820Sjeff	uint8_t in_port_num;
69219820Sjeff	cl_qlist_t *p_list;
70219820Sjeff	osm_sa_t *sa;
71219820Sjeff	const osm_physp_t *p_req_physp;
72219820Sjeff} osm_slvl_search_ctxt_t;
73219820Sjeff
74219820Sjeff/**********************************************************************
75219820Sjeff **********************************************************************/
76219820Sjeffstatic void
77219820Sjeff__osm_sa_slvl_create(IN osm_sa_t * sa,
78219820Sjeff		     IN const osm_physp_t * const p_physp,
79219820Sjeff		     IN osm_slvl_search_ctxt_t * const p_ctxt,
80219820Sjeff		     IN uint8_t in_port_idx)
81219820Sjeff{
82219820Sjeff	osm_slvl_item_t *p_rec_item;
83219820Sjeff	uint16_t lid;
84219820Sjeff	ib_api_status_t status = IB_SUCCESS;
85219820Sjeff
86219820Sjeff	OSM_LOG_ENTER(sa->p_log);
87219820Sjeff
88219820Sjeff	p_rec_item = malloc(sizeof(*p_rec_item));
89219820Sjeff	if (p_rec_item == NULL) {
90219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2602: "
91219820Sjeff			"rec_item alloc failed\n");
92219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
93219820Sjeff		goto Exit;
94219820Sjeff	}
95219820Sjeff
96219820Sjeff	if (p_physp->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH)
97219820Sjeff		lid = p_physp->port_info.base_lid;
98219820Sjeff	else
99219820Sjeff		lid = osm_node_get_base_lid(p_physp->p_node, 0);
100219820Sjeff
101219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
102219820Sjeff		"New SLtoVL Map for: OUT port 0x%016" PRIx64
103219820Sjeff		", lid 0x%X, port %u to In Port:%u\n",
104219820Sjeff		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
105219820Sjeff		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), in_port_idx);
106219820Sjeff
107219820Sjeff	memset(p_rec_item, 0, sizeof(*p_rec_item));
108219820Sjeff
109219820Sjeff	p_rec_item->rec.lid = lid;
110219820Sjeff	p_rec_item->rec.out_port_num = osm_physp_get_port_num(p_physp);
111219820Sjeff	p_rec_item->rec.in_port_num = in_port_idx;
112219820Sjeff	p_rec_item->rec.slvl_tbl =
113219820Sjeff	    *(osm_physp_get_slvl_tbl(p_physp, in_port_idx));
114219820Sjeff
115219820Sjeff	cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
116219820Sjeff
117219820SjeffExit:
118219820Sjeff	OSM_LOG_EXIT(sa->p_log);
119219820Sjeff}
120219820Sjeff
121219820Sjeff/**********************************************************************
122219820Sjeff **********************************************************************/
123219820Sjeffstatic void
124219820Sjeff__osm_sa_slvl_by_comp_mask(IN osm_sa_t * sa,
125219820Sjeff			   IN const osm_port_t * const p_port,
126219820Sjeff			   osm_slvl_search_ctxt_t * const p_ctxt)
127219820Sjeff{
128219820Sjeff	const ib_slvl_table_record_t *p_rcvd_rec;
129219820Sjeff	ib_net64_t comp_mask;
130219820Sjeff	const osm_physp_t *p_out_physp, *p_in_physp;
131219820Sjeff	uint8_t in_port_num, out_port_num;
132219820Sjeff	uint8_t num_ports;
133219820Sjeff	uint8_t in_port_start, in_port_end;
134219820Sjeff	uint8_t out_port_start, out_port_end;
135219820Sjeff	const osm_physp_t *p_req_physp;
136219820Sjeff
137219820Sjeff	OSM_LOG_ENTER(sa->p_log);
138219820Sjeff
139219820Sjeff	p_rcvd_rec = p_ctxt->p_rcvd_rec;
140219820Sjeff	comp_mask = p_ctxt->comp_mask;
141219820Sjeff	num_ports = osm_node_get_num_physp(p_port->p_node);
142219820Sjeff	in_port_start = 0;
143219820Sjeff	in_port_end = num_ports - 1;
144219820Sjeff	out_port_start = 0;
145219820Sjeff	out_port_end = num_ports - 1;
146219820Sjeff	p_req_physp = p_ctxt->p_req_physp;
147219820Sjeff
148219820Sjeff	if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
149219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
150219820Sjeff			"Using Physical Default Port Number: 0x%X (for End Node)\n",
151219820Sjeff			p_port->p_physp->port_num);
152219820Sjeff		p_out_physp = p_port->p_physp;
153219820Sjeff		/* check that the p_out_physp and the p_req_physp share a pkey */
154219820Sjeff		if (osm_physp_share_pkey
155219820Sjeff		    (sa->p_log, p_req_physp, p_out_physp))
156219820Sjeff			__osm_sa_slvl_create(sa, p_out_physp, p_ctxt, 0);
157219820Sjeff	} else {
158219820Sjeff		if (comp_mask & IB_SLVL_COMPMASK_OUT_PORT)
159219820Sjeff			out_port_start = out_port_end =
160219820Sjeff			    p_rcvd_rec->out_port_num;
161219820Sjeff		if (comp_mask & IB_SLVL_COMPMASK_IN_PORT)
162219820Sjeff			in_port_start = in_port_end = p_rcvd_rec->in_port_num;
163219820Sjeff
164219820Sjeff		for (out_port_num = out_port_start;
165219820Sjeff		     out_port_num <= out_port_end; out_port_num++) {
166219820Sjeff			p_out_physp =
167219820Sjeff			    osm_node_get_physp_ptr(p_port->p_node,
168219820Sjeff						   out_port_num);
169219820Sjeff			if (!p_out_physp)
170219820Sjeff				continue;
171219820Sjeff
172219820Sjeff			for (in_port_num = in_port_start;
173219820Sjeff			     in_port_num <= in_port_end; in_port_num++) {
174219820Sjeff#if 0
175219820Sjeff				if (out_port_num && out_port_num == in_port_num)
176219820Sjeff					continue;
177219820Sjeff#endif
178219820Sjeff
179219820Sjeff				p_in_physp =
180219820Sjeff				    osm_node_get_physp_ptr(p_port->p_node,
181219820Sjeff							   in_port_num);
182219820Sjeff				if (!p_in_physp)
183219820Sjeff					continue;
184219820Sjeff
185219820Sjeff				/* if the requester and the p_out_physp don't share a pkey -
186219820Sjeff				   continue */
187219820Sjeff				if (!osm_physp_share_pkey
188219820Sjeff				    (sa->p_log, p_req_physp, p_out_physp))
189219820Sjeff					continue;
190219820Sjeff
191219820Sjeff				__osm_sa_slvl_create(sa, p_out_physp, p_ctxt,
192219820Sjeff						     in_port_num);
193219820Sjeff			}
194219820Sjeff		}
195219820Sjeff	}
196219820Sjeff	OSM_LOG_EXIT(sa->p_log);
197219820Sjeff}
198219820Sjeff
199219820Sjeff/**********************************************************************
200219820Sjeff **********************************************************************/
201219820Sjeffstatic void
202219820Sjeff__osm_sa_slvl_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
203219820Sjeff			      IN void *context)
204219820Sjeff{
205219820Sjeff	const osm_port_t *const p_port = (osm_port_t *) p_map_item;
206219820Sjeff	osm_slvl_search_ctxt_t *const p_ctxt =
207219820Sjeff	    (osm_slvl_search_ctxt_t *) context;
208219820Sjeff
209219820Sjeff	__osm_sa_slvl_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
210219820Sjeff}
211219820Sjeff
212219820Sjeff/**********************************************************************
213219820Sjeff **********************************************************************/
214219820Sjeffvoid osm_slvl_rec_rcv_process(IN void *ctx, IN void *data)
215219820Sjeff{
216219820Sjeff	osm_sa_t *sa = ctx;
217219820Sjeff	osm_madw_t *p_madw = data;
218219820Sjeff	const ib_sa_mad_t *p_rcvd_mad;
219219820Sjeff	const ib_slvl_table_record_t *p_rcvd_rec;
220219820Sjeff	const osm_port_t *p_port = NULL;
221219820Sjeff	cl_qlist_t rec_list;
222219820Sjeff	osm_slvl_search_ctxt_t context;
223219820Sjeff	ib_api_status_t status = IB_SUCCESS;
224219820Sjeff	ib_net64_t comp_mask;
225219820Sjeff	osm_physp_t *p_req_physp;
226219820Sjeff
227219820Sjeff	CL_ASSERT(sa);
228219820Sjeff
229219820Sjeff	OSM_LOG_ENTER(sa->p_log);
230219820Sjeff
231219820Sjeff	CL_ASSERT(p_madw);
232219820Sjeff
233219820Sjeff	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
234219820Sjeff	p_rcvd_rec =
235219820Sjeff	    (ib_slvl_table_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
236219820Sjeff	comp_mask = p_rcvd_mad->comp_mask;
237219820Sjeff
238219820Sjeff	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_SLVL_RECORD);
239219820Sjeff
240219820Sjeff	/* we only support SubnAdmGet and SubnAdmGetTable methods */
241219820Sjeff	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
242219820Sjeff	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
243219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2604: "
244219820Sjeff			"Unsupported Method (%s)\n",
245219820Sjeff			ib_get_sa_method_str(p_rcvd_mad->method));
246219820Sjeff		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
247219820Sjeff		goto Exit;
248219820Sjeff	}
249219820Sjeff
250219820Sjeff	/* update the requester physical port. */
251219820Sjeff	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
252219820Sjeff						osm_madw_get_mad_addr_ptr
253219820Sjeff						(p_madw));
254219820Sjeff	if (p_req_physp == NULL) {
255219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2603: "
256219820Sjeff			"Cannot find requester physical port\n");
257219820Sjeff		goto Exit;
258219820Sjeff	}
259219820Sjeff
260219820Sjeff	cl_qlist_init(&rec_list);
261219820Sjeff
262219820Sjeff	context.p_rcvd_rec = p_rcvd_rec;
263219820Sjeff	context.p_list = &rec_list;
264219820Sjeff	context.comp_mask = p_rcvd_mad->comp_mask;
265219820Sjeff	context.sa = sa;
266219820Sjeff	context.in_port_num = p_rcvd_rec->in_port_num;
267219820Sjeff	context.p_req_physp = p_req_physp;
268219820Sjeff
269219820Sjeff	cl_plock_acquire(sa->p_lock);
270219820Sjeff
271219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
272219820Sjeff		"Got Query Lid:%u(%02X), In-Port:0x%02X(%02X), Out-Port:0x%02X(%02X)\n",
273219820Sjeff		cl_ntoh16(p_rcvd_rec->lid),
274219820Sjeff		(comp_mask & IB_SLVL_COMPMASK_LID) != 0,
275219820Sjeff		p_rcvd_rec->in_port_num,
276219820Sjeff		(comp_mask & IB_SLVL_COMPMASK_IN_PORT) != 0,
277219820Sjeff		p_rcvd_rec->out_port_num,
278219820Sjeff		(comp_mask & IB_SLVL_COMPMASK_OUT_PORT) != 0);
279219820Sjeff
280219820Sjeff	/*
281219820Sjeff	   If the user specified a LID, it obviously narrows our
282219820Sjeff	   work load, since we don't have to search every port
283219820Sjeff	 */
284219820Sjeff	if (comp_mask & IB_SLVL_COMPMASK_LID) {
285219820Sjeff		status =
286219820Sjeff		    osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
287219820Sjeff					     &p_port);
288219820Sjeff		if ((status != IB_SUCCESS) || (p_port == NULL)) {
289219820Sjeff			status = IB_NOT_FOUND;
290219820Sjeff			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2608: "
291219820Sjeff				"No port found with LID %u\n",
292219820Sjeff				cl_ntoh16(p_rcvd_rec->lid));
293219820Sjeff		}
294219820Sjeff	}
295219820Sjeff
296219820Sjeff	if (status == IB_SUCCESS) {
297219820Sjeff		/* if we have a unique port - no need for a port search */
298219820Sjeff		if (p_port)
299219820Sjeff			/*  this does the loop on all the port phys ports */
300219820Sjeff			__osm_sa_slvl_by_comp_mask(sa, p_port, &context);
301219820Sjeff		else
302219820Sjeff			cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
303219820Sjeff					   __osm_sa_slvl_by_comp_mask_cb,
304219820Sjeff					   &context);
305219820Sjeff	}
306219820Sjeff
307219820Sjeff	cl_plock_release(sa->p_lock);
308219820Sjeff
309219820Sjeff	osm_sa_respond(sa, p_madw, sizeof(ib_slvl_table_record_t), &rec_list);
310219820Sjeff
311219820SjeffExit:
312219820Sjeff	OSM_LOG_EXIT(sa->p_log);
313219820Sjeff}
314