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_mftr_rcv_t.
39219820Sjeff *   This object represents the MulticastForwardingTable 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_debug.h>
50219820Sjeff#include <complib/cl_qlist.h>
51219820Sjeff#include <vendor/osm_vendor_api.h>
52219820Sjeff#include <opensm/osm_switch.h>
53219820Sjeff#include <opensm/osm_helper.h>
54219820Sjeff#include <opensm/osm_pkey.h>
55219820Sjeff#include <opensm/osm_sa.h>
56219820Sjeff
57219820Sjefftypedef struct osm_mftr_item {
58219820Sjeff	cl_list_item_t list_item;
59219820Sjeff	ib_mft_record_t rec;
60219820Sjeff} osm_mftr_item_t;
61219820Sjeff
62219820Sjefftypedef struct osm_mftr_search_ctxt {
63219820Sjeff	const ib_mft_record_t *p_rcvd_rec;
64219820Sjeff	ib_net64_t comp_mask;
65219820Sjeff	cl_qlist_t *p_list;
66219820Sjeff	osm_sa_t *sa;
67219820Sjeff	const osm_physp_t *p_req_physp;
68219820Sjeff} osm_mftr_search_ctxt_t;
69219820Sjeff
70219820Sjeff/**********************************************************************
71219820Sjeff **********************************************************************/
72219820Sjeffstatic ib_api_status_t
73219820Sjeff__osm_mftr_rcv_new_mftr(IN osm_sa_t * sa,
74219820Sjeff			IN osm_switch_t * const p_sw,
75219820Sjeff			IN cl_qlist_t * const p_list,
76219820Sjeff			IN ib_net16_t const lid,
77219820Sjeff			IN uint16_t const block, IN uint8_t const position)
78219820Sjeff{
79219820Sjeff	osm_mftr_item_t *p_rec_item;
80219820Sjeff	ib_api_status_t status = IB_SUCCESS;
81219820Sjeff	uint16_t position_block_num;
82219820Sjeff
83219820Sjeff	OSM_LOG_ENTER(sa->p_log);
84219820Sjeff
85219820Sjeff	p_rec_item = malloc(sizeof(*p_rec_item));
86219820Sjeff	if (p_rec_item == NULL) {
87219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A02: "
88219820Sjeff			"rec_item alloc failed\n");
89219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
90219820Sjeff		goto Exit;
91219820Sjeff	}
92219820Sjeff
93219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
94219820Sjeff		"New MulticastForwardingTable: sw 0x%016" PRIx64
95219820Sjeff		"\n\t\t\t\tblock %u position %u lid %u\n",
96219820Sjeff		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
97219820Sjeff		block, position, cl_ntoh16(lid));
98219820Sjeff
99219820Sjeff	position_block_num = ((uint16_t) position << 12) |
100219820Sjeff	    (block & IB_MCAST_BLOCK_ID_MASK_HO);
101219820Sjeff
102219820Sjeff	memset(p_rec_item, 0, sizeof(*p_rec_item));
103219820Sjeff
104219820Sjeff	p_rec_item->rec.lid = lid;
105219820Sjeff	p_rec_item->rec.position_block_num = cl_hton16(position_block_num);
106219820Sjeff
107219820Sjeff	/* copy the mft block */
108219820Sjeff	osm_switch_get_mft_block(p_sw, block, position, p_rec_item->rec.mft);
109219820Sjeff
110219820Sjeff	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
111219820Sjeff
112219820SjeffExit:
113219820Sjeff	OSM_LOG_EXIT(sa->p_log);
114219820Sjeff	return (status);
115219820Sjeff}
116219820Sjeff
117219820Sjeff/**********************************************************************
118219820Sjeff **********************************************************************/
119219820Sjeffstatic void
120219820Sjeff__osm_mftr_rcv_by_comp_mask(IN cl_map_item_t * const p_map_item,
121219820Sjeff			    IN void *context)
122219820Sjeff{
123219820Sjeff	const osm_mftr_search_ctxt_t *const p_ctxt =
124219820Sjeff	    (osm_mftr_search_ctxt_t *) context;
125219820Sjeff	osm_switch_t *const p_sw = (osm_switch_t *) p_map_item;
126219820Sjeff	const ib_mft_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
127219820Sjeff	osm_sa_t *sa = p_ctxt->sa;
128219820Sjeff	ib_net64_t const comp_mask = p_ctxt->comp_mask;
129219820Sjeff	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
130219820Sjeff	osm_port_t *p_port;
131219820Sjeff	uint16_t min_lid_ho, max_lid_ho;
132219820Sjeff	uint16_t position_block_num_ho;
133219820Sjeff	uint16_t min_block, max_block, block;
134219820Sjeff	const osm_physp_t *p_physp;
135219820Sjeff	uint8_t min_position, max_position, position;
136219820Sjeff
137219820Sjeff	/* In switches, the port guid is the node guid. */
138219820Sjeff	p_port =
139219820Sjeff	    osm_get_port_by_guid(sa->p_subn, p_sw->p_node->node_info.port_guid);
140219820Sjeff	if (!p_port) {
141219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A05: "
142219820Sjeff			"Failed to find Port by Node Guid:0x%016" PRIx64
143219820Sjeff			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
144219820Sjeff		return;
145219820Sjeff	}
146219820Sjeff
147219820Sjeff	/* check that the requester physp and the current physp are under
148219820Sjeff	   the same partition. */
149219820Sjeff	p_physp = p_port->p_physp;
150219820Sjeff	if (!p_physp) {
151219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A06: "
152219820Sjeff			"Failed to find default physical Port by Node Guid:0x%016"
153219820Sjeff			PRIx64 "\n",
154219820Sjeff			cl_ntoh64(p_sw->p_node->node_info.node_guid));
155219820Sjeff		return;
156219820Sjeff	}
157219820Sjeff	if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp))
158219820Sjeff		return;
159219820Sjeff
160219820Sjeff	/* get the port 0 of the switch */
161219820Sjeff	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
162219820Sjeff
163219820Sjeff	/* compare the lids - if required */
164219820Sjeff	if (comp_mask & IB_MFTR_COMPMASK_LID) {
165219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
166219820Sjeff			"Comparing lid:%u to port lid range: %u .. %u\n",
167219820Sjeff			cl_ntoh16(p_rcvd_rec->lid), min_lid_ho, max_lid_ho);
168219820Sjeff		/* ok we are ready for range check */
169219820Sjeff		if (min_lid_ho > cl_ntoh16(p_rcvd_rec->lid) ||
170219820Sjeff		    max_lid_ho < cl_ntoh16(p_rcvd_rec->lid))
171219820Sjeff			return;
172219820Sjeff	}
173219820Sjeff
174219820Sjeff	if (!osm_switch_supports_mcast(p_sw))
175219820Sjeff		return;
176219820Sjeff
177219820Sjeff	/* Are there any blocks in use ? */
178219820Sjeff	if (osm_switch_get_mft_max_block_in_use(p_sw) == -1)
179219820Sjeff		return;
180219820Sjeff
181219820Sjeff	position_block_num_ho = cl_ntoh16(p_rcvd_rec->position_block_num);
182219820Sjeff
183219820Sjeff	/* now we need to decide which blocks to output */
184219820Sjeff	if (comp_mask & IB_MFTR_COMPMASK_BLOCK) {
185219820Sjeff		max_block = min_block =
186219820Sjeff		    position_block_num_ho & IB_MCAST_BLOCK_ID_MASK_HO;
187219820Sjeff		if (max_block > osm_switch_get_mft_max_block_in_use(p_sw))
188219820Sjeff			return;
189219820Sjeff	} else {
190219820Sjeff		/* use as many blocks as needed */
191219820Sjeff		min_block = 0;
192219820Sjeff		max_block = osm_switch_get_mft_max_block_in_use(p_sw);
193219820Sjeff	}
194219820Sjeff
195219820Sjeff	/* need to decide which positions to output */
196219820Sjeff	if (comp_mask & IB_MFTR_COMPMASK_POSITION) {
197219820Sjeff		min_position = max_position =
198219820Sjeff		    (position_block_num_ho & 0xF000) >> 12;
199219820Sjeff		if (max_position > osm_switch_get_mft_max_position(p_sw))
200219820Sjeff			return;
201219820Sjeff	} else {
202219820Sjeff		/* use as many positions as needed */
203219820Sjeff		min_position = 0;
204219820Sjeff		max_position = osm_switch_get_mft_max_position(p_sw);
205219820Sjeff	}
206219820Sjeff
207219820Sjeff	/* so we can add these one by one ... */
208219820Sjeff	for (block = min_block; block <= max_block; block++)
209219820Sjeff		for (position = min_position; position <= max_position;
210219820Sjeff		     position++)
211219820Sjeff			__osm_mftr_rcv_new_mftr(sa, p_sw, p_ctxt->p_list,
212219820Sjeff						osm_port_get_base_lid(p_port),
213219820Sjeff						block, position);
214219820Sjeff}
215219820Sjeff
216219820Sjeff/**********************************************************************
217219820Sjeff **********************************************************************/
218219820Sjeffvoid osm_mftr_rcv_process(IN void *ctx, IN void *data)
219219820Sjeff{
220219820Sjeff	osm_sa_t *sa = ctx;
221219820Sjeff	osm_madw_t *p_madw = data;
222219820Sjeff	const ib_sa_mad_t *p_rcvd_mad;
223219820Sjeff	const ib_mft_record_t *p_rcvd_rec;
224219820Sjeff	cl_qlist_t rec_list;
225219820Sjeff	osm_mftr_search_ctxt_t context;
226219820Sjeff	osm_physp_t *p_req_physp;
227219820Sjeff
228219820Sjeff	CL_ASSERT(sa);
229219820Sjeff
230219820Sjeff	OSM_LOG_ENTER(sa->p_log);
231219820Sjeff
232219820Sjeff	CL_ASSERT(p_madw);
233219820Sjeff
234219820Sjeff	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
235219820Sjeff	p_rcvd_rec = (ib_mft_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
236219820Sjeff
237219820Sjeff	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_MFT_RECORD);
238219820Sjeff
239219820Sjeff	/* we only support SubnAdmGet and SubnAdmGetTable methods */
240219820Sjeff	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
241219820Sjeff	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
242219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A08: "
243219820Sjeff			"Unsupported Method (%s)\n",
244219820Sjeff			ib_get_sa_method_str(p_rcvd_mad->method));
245219820Sjeff		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
246219820Sjeff		goto Exit;
247219820Sjeff	}
248219820Sjeff
249219820Sjeff	/* update the requester physical port. */
250219820Sjeff	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log,
251219820Sjeff						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 4A07: "
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.p_req_physp = p_req_physp;
267219820Sjeff
268219820Sjeff	cl_plock_acquire(sa->p_lock);
269219820Sjeff
270219820Sjeff	/* Go over all switches */
271219820Sjeff	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl,
272219820Sjeff			   __osm_mftr_rcv_by_comp_mask, &context);
273219820Sjeff
274219820Sjeff	cl_plock_release(sa->p_lock);
275219820Sjeff
276219820Sjeff	osm_sa_respond(sa, p_madw, sizeof(ib_mft_record_t), &rec_list);
277219820Sjeff
278219820SjeffExit:
279219820Sjeff	OSM_LOG_EXIT(sa->p_log);
280219820Sjeff}
281