1/*
2 * Copyright (c) 2004-2008 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 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
34 */
35
36/*
37 * Abstract:
38 *    Implementation of osm_mftr_rcv_t.
39 *   This object represents the MulticastForwardingTable Receiver object.
40 *   This object is part of the opensm family of objects.
41 */
42
43#if HAVE_CONFIG_H
44#  include <config.h>
45#endif				/* HAVE_CONFIG_H */
46
47#include <string.h>
48#include <iba/ib_types.h>
49#include <complib/cl_debug.h>
50#include <complib/cl_qlist.h>
51#include <vendor/osm_vendor_api.h>
52#include <opensm/osm_switch.h>
53#include <opensm/osm_helper.h>
54#include <opensm/osm_pkey.h>
55#include <opensm/osm_sa.h>
56
57typedef struct osm_mftr_item {
58	cl_list_item_t list_item;
59	ib_mft_record_t rec;
60} osm_mftr_item_t;
61
62typedef struct osm_mftr_search_ctxt {
63	const ib_mft_record_t *p_rcvd_rec;
64	ib_net64_t comp_mask;
65	cl_qlist_t *p_list;
66	osm_sa_t *sa;
67	const osm_physp_t *p_req_physp;
68} osm_mftr_search_ctxt_t;
69
70/**********************************************************************
71 **********************************************************************/
72static ib_api_status_t
73__osm_mftr_rcv_new_mftr(IN osm_sa_t * sa,
74			IN osm_switch_t * const p_sw,
75			IN cl_qlist_t * const p_list,
76			IN ib_net16_t const lid,
77			IN uint16_t const block, IN uint8_t const position)
78{
79	osm_mftr_item_t *p_rec_item;
80	ib_api_status_t status = IB_SUCCESS;
81	uint16_t position_block_num;
82
83	OSM_LOG_ENTER(sa->p_log);
84
85	p_rec_item = malloc(sizeof(*p_rec_item));
86	if (p_rec_item == NULL) {
87		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A02: "
88			"rec_item alloc failed\n");
89		status = IB_INSUFFICIENT_RESOURCES;
90		goto Exit;
91	}
92
93	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
94		"New MulticastForwardingTable: sw 0x%016" PRIx64
95		"\n\t\t\t\tblock %u position %u lid %u\n",
96		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
97		block, position, cl_ntoh16(lid));
98
99	position_block_num = ((uint16_t) position << 12) |
100	    (block & IB_MCAST_BLOCK_ID_MASK_HO);
101
102	memset(p_rec_item, 0, sizeof(*p_rec_item));
103
104	p_rec_item->rec.lid = lid;
105	p_rec_item->rec.position_block_num = cl_hton16(position_block_num);
106
107	/* copy the mft block */
108	osm_switch_get_mft_block(p_sw, block, position, p_rec_item->rec.mft);
109
110	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
111
112Exit:
113	OSM_LOG_EXIT(sa->p_log);
114	return (status);
115}
116
117/**********************************************************************
118 **********************************************************************/
119static void
120__osm_mftr_rcv_by_comp_mask(IN cl_map_item_t * const p_map_item,
121			    IN void *context)
122{
123	const osm_mftr_search_ctxt_t *const p_ctxt =
124	    (osm_mftr_search_ctxt_t *) context;
125	osm_switch_t *const p_sw = (osm_switch_t *) p_map_item;
126	const ib_mft_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
127	osm_sa_t *sa = p_ctxt->sa;
128	ib_net64_t const comp_mask = p_ctxt->comp_mask;
129	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
130	osm_port_t *p_port;
131	uint16_t min_lid_ho, max_lid_ho;
132	uint16_t position_block_num_ho;
133	uint16_t min_block, max_block, block;
134	const osm_physp_t *p_physp;
135	uint8_t min_position, max_position, position;
136
137	/* In switches, the port guid is the node guid. */
138	p_port =
139	    osm_get_port_by_guid(sa->p_subn, p_sw->p_node->node_info.port_guid);
140	if (!p_port) {
141		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A05: "
142			"Failed to find Port by Node Guid:0x%016" PRIx64
143			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
144		return;
145	}
146
147	/* check that the requester physp and the current physp are under
148	   the same partition. */
149	p_physp = p_port->p_physp;
150	if (!p_physp) {
151		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A06: "
152			"Failed to find default physical Port by Node Guid:0x%016"
153			PRIx64 "\n",
154			cl_ntoh64(p_sw->p_node->node_info.node_guid));
155		return;
156	}
157	if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp))
158		return;
159
160	/* get the port 0 of the switch */
161	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
162
163	/* compare the lids - if required */
164	if (comp_mask & IB_MFTR_COMPMASK_LID) {
165		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
166			"Comparing lid:%u to port lid range: %u .. %u\n",
167			cl_ntoh16(p_rcvd_rec->lid), min_lid_ho, max_lid_ho);
168		/* ok we are ready for range check */
169		if (min_lid_ho > cl_ntoh16(p_rcvd_rec->lid) ||
170		    max_lid_ho < cl_ntoh16(p_rcvd_rec->lid))
171			return;
172	}
173
174	if (!osm_switch_supports_mcast(p_sw))
175		return;
176
177	/* Are there any blocks in use ? */
178	if (osm_switch_get_mft_max_block_in_use(p_sw) == -1)
179		return;
180
181	position_block_num_ho = cl_ntoh16(p_rcvd_rec->position_block_num);
182
183	/* now we need to decide which blocks to output */
184	if (comp_mask & IB_MFTR_COMPMASK_BLOCK) {
185		max_block = min_block =
186		    position_block_num_ho & IB_MCAST_BLOCK_ID_MASK_HO;
187		if (max_block > osm_switch_get_mft_max_block_in_use(p_sw))
188			return;
189	} else {
190		/* use as many blocks as needed */
191		min_block = 0;
192		max_block = osm_switch_get_mft_max_block_in_use(p_sw);
193	}
194
195	/* need to decide which positions to output */
196	if (comp_mask & IB_MFTR_COMPMASK_POSITION) {
197		min_position = max_position =
198		    (position_block_num_ho & 0xF000) >> 12;
199		if (max_position > osm_switch_get_mft_max_position(p_sw))
200			return;
201	} else {
202		/* use as many positions as needed */
203		min_position = 0;
204		max_position = osm_switch_get_mft_max_position(p_sw);
205	}
206
207	/* so we can add these one by one ... */
208	for (block = min_block; block <= max_block; block++)
209		for (position = min_position; position <= max_position;
210		     position++)
211			__osm_mftr_rcv_new_mftr(sa, p_sw, p_ctxt->p_list,
212						osm_port_get_base_lid(p_port),
213						block, position);
214}
215
216/**********************************************************************
217 **********************************************************************/
218void osm_mftr_rcv_process(IN void *ctx, IN void *data)
219{
220	osm_sa_t *sa = ctx;
221	osm_madw_t *p_madw = data;
222	const ib_sa_mad_t *p_rcvd_mad;
223	const ib_mft_record_t *p_rcvd_rec;
224	cl_qlist_t rec_list;
225	osm_mftr_search_ctxt_t context;
226	osm_physp_t *p_req_physp;
227
228	CL_ASSERT(sa);
229
230	OSM_LOG_ENTER(sa->p_log);
231
232	CL_ASSERT(p_madw);
233
234	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
235	p_rcvd_rec = (ib_mft_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
236
237	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_MFT_RECORD);
238
239	/* we only support SubnAdmGet and SubnAdmGetTable methods */
240	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
241	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
242		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A08: "
243			"Unsupported Method (%s)\n",
244			ib_get_sa_method_str(p_rcvd_mad->method));
245		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
246		goto Exit;
247	}
248
249	/* update the requester physical port. */
250	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log,
251						sa->p_subn,
252						osm_madw_get_mad_addr_ptr
253						(p_madw));
254	if (p_req_physp == NULL) {
255		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A07: "
256			"Cannot find requester physical port\n");
257		goto Exit;
258	}
259
260	cl_qlist_init(&rec_list);
261
262	context.p_rcvd_rec = p_rcvd_rec;
263	context.p_list = &rec_list;
264	context.comp_mask = p_rcvd_mad->comp_mask;
265	context.sa = sa;
266	context.p_req_physp = p_req_physp;
267
268	cl_plock_acquire(sa->p_lock);
269
270	/* Go over all switches */
271	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl,
272			   __osm_mftr_rcv_by_comp_mask, &context);
273
274	cl_plock_release(sa->p_lock);
275
276	osm_sa_respond(sa, p_madw, sizeof(ib_mft_record_t), &rec_list);
277
278Exit:
279	OSM_LOG_EXIT(sa->p_log);
280}
281