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_lftr_rcv_t.
39219820Sjeff *   This object represents the LinearForwardingTable 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_lftr_item {
58219820Sjeff	cl_list_item_t list_item;
59219820Sjeff	ib_lft_record_t rec;
60219820Sjeff} osm_lftr_item_t;
61219820Sjeff
62219820Sjefftypedef struct osm_lftr_search_ctxt {
63219820Sjeff	const ib_lft_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_lftr_search_ctxt_t;
69219820Sjeff
70219820Sjeff/**********************************************************************
71219820Sjeff **********************************************************************/
72219820Sjeffstatic ib_api_status_t
73219820Sjeff__osm_lftr_rcv_new_lftr(IN osm_sa_t * sa,
74219820Sjeff			IN const osm_switch_t * const p_sw,
75219820Sjeff			IN cl_qlist_t * const p_list,
76219820Sjeff			IN ib_net16_t const lid, IN uint16_t const block)
77219820Sjeff{
78219820Sjeff	osm_lftr_item_t *p_rec_item;
79219820Sjeff	ib_api_status_t status = IB_SUCCESS;
80219820Sjeff
81219820Sjeff	OSM_LOG_ENTER(sa->p_log);
82219820Sjeff
83219820Sjeff	p_rec_item = malloc(sizeof(*p_rec_item));
84219820Sjeff	if (p_rec_item == NULL) {
85219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4402: "
86219820Sjeff			"rec_item alloc failed\n");
87219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
88219820Sjeff		goto Exit;
89219820Sjeff	}
90219820Sjeff
91219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
92219820Sjeff		"New LinearForwardingTable: sw 0x%016" PRIx64
93219820Sjeff		"\n\t\t\t\tblock 0x%02X lid %u\n",
94219820Sjeff		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
95219820Sjeff		block, cl_ntoh16(lid));
96219820Sjeff
97219820Sjeff	memset(p_rec_item, 0, sizeof(*p_rec_item));
98219820Sjeff
99219820Sjeff	p_rec_item->rec.lid = lid;
100219820Sjeff	p_rec_item->rec.block_num = cl_hton16(block);
101219820Sjeff
102219820Sjeff	/* copy the lft block */
103219820Sjeff	osm_switch_get_lft_block(p_sw, block, p_rec_item->rec.lft);
104219820Sjeff
105219820Sjeff	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
106219820Sjeff
107219820SjeffExit:
108219820Sjeff	OSM_LOG_EXIT(sa->p_log);
109219820Sjeff	return (status);
110219820Sjeff}
111219820Sjeff
112219820Sjeff/**********************************************************************
113219820Sjeff **********************************************************************/
114219820Sjeffstatic void
115219820Sjeff__osm_lftr_rcv_by_comp_mask(IN cl_map_item_t * const p_map_item,
116219820Sjeff			    IN void *context)
117219820Sjeff{
118219820Sjeff	const osm_lftr_search_ctxt_t *const p_ctxt =
119219820Sjeff	    (osm_lftr_search_ctxt_t *) context;
120219820Sjeff	const osm_switch_t *const p_sw = (osm_switch_t *) p_map_item;
121219820Sjeff	const ib_lft_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
122219820Sjeff	osm_sa_t *sa = p_ctxt->sa;
123219820Sjeff	ib_net64_t const comp_mask = p_ctxt->comp_mask;
124219820Sjeff	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
125219820Sjeff	osm_port_t *p_port;
126219820Sjeff	uint16_t min_lid_ho, max_lid_ho;
127219820Sjeff	uint16_t min_block, max_block, block;
128219820Sjeff	const osm_physp_t *p_physp;
129219820Sjeff
130219820Sjeff	/* In switches, the port guid is the node guid. */
131219820Sjeff	p_port = osm_get_port_by_guid(sa->p_subn,
132219820Sjeff				      p_sw->p_node->node_info.port_guid);
133219820Sjeff	if (!p_port) {
134219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4405: "
135219820Sjeff			"Failed to find Port by Node Guid:0x%016" PRIx64
136219820Sjeff			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
137219820Sjeff		return;
138219820Sjeff	}
139219820Sjeff
140219820Sjeff	/* check that the requester physp and the current physp are under
141219820Sjeff	   the same partition. */
142219820Sjeff	p_physp = p_port->p_physp;
143219820Sjeff	if (!p_physp) {
144219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4406: "
145219820Sjeff			"Failed to find default physical Port by Node Guid:0x%016"
146219820Sjeff			PRIx64 "\n",
147219820Sjeff			cl_ntoh64(p_sw->p_node->node_info.node_guid));
148219820Sjeff		return;
149219820Sjeff	}
150219820Sjeff	if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp))
151219820Sjeff		return;
152219820Sjeff
153219820Sjeff	/* get the port 0 of the switch */
154219820Sjeff	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
155219820Sjeff
156219820Sjeff	/* compare the lids - if required */
157219820Sjeff	if (comp_mask & IB_LFTR_COMPMASK_LID) {
158219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
159219820Sjeff			"Comparing lid:%u to port lid range: %u .. %u\n",
160219820Sjeff			cl_ntoh16(p_rcvd_rec->lid), min_lid_ho, max_lid_ho);
161219820Sjeff		/* ok we are ready for range check */
162219820Sjeff		if (min_lid_ho > cl_ntoh16(p_rcvd_rec->lid) ||
163219820Sjeff		    max_lid_ho < cl_ntoh16(p_rcvd_rec->lid))
164219820Sjeff			return;
165219820Sjeff	}
166219820Sjeff
167219820Sjeff	/* now we need to decide which blocks to output */
168219820Sjeff	max_block = osm_switch_get_max_block_id_in_use(p_sw);
169219820Sjeff	if (comp_mask & IB_LFTR_COMPMASK_BLOCK) {
170219820Sjeff		min_block = cl_ntoh16(p_rcvd_rec->block_num);
171219820Sjeff		if (min_block > max_block)
172219820Sjeff			return;
173219820Sjeff		max_block = min_block;
174219820Sjeff	} else			/* use as many blocks as "in use" */
175219820Sjeff		min_block = 0;
176219820Sjeff
177219820Sjeff	/* so we can add these blocks one by one ... */
178219820Sjeff	for (block = min_block; block <= max_block; block++)
179219820Sjeff		__osm_lftr_rcv_new_lftr(sa, p_sw, p_ctxt->p_list,
180219820Sjeff					osm_port_get_base_lid(p_port), block);
181219820Sjeff}
182219820Sjeff
183219820Sjeff/**********************************************************************
184219820Sjeff **********************************************************************/
185219820Sjeffvoid osm_lftr_rcv_process(IN void *ctx, IN void *data)
186219820Sjeff{
187219820Sjeff	osm_sa_t *sa = ctx;
188219820Sjeff	osm_madw_t *p_madw = data;
189219820Sjeff	const ib_sa_mad_t *p_rcvd_mad;
190219820Sjeff	const ib_lft_record_t *p_rcvd_rec;
191219820Sjeff	cl_qlist_t rec_list;
192219820Sjeff	osm_lftr_search_ctxt_t context;
193219820Sjeff	osm_physp_t *p_req_physp;
194219820Sjeff
195219820Sjeff	CL_ASSERT(sa);
196219820Sjeff
197219820Sjeff	OSM_LOG_ENTER(sa->p_log);
198219820Sjeff
199219820Sjeff	CL_ASSERT(p_madw);
200219820Sjeff
201219820Sjeff	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
202219820Sjeff	p_rcvd_rec = (ib_lft_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
203219820Sjeff
204219820Sjeff	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_LFT_RECORD);
205219820Sjeff
206219820Sjeff	/* we only support SubnAdmGet and SubnAdmGetTable methods */
207219820Sjeff	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
208219820Sjeff	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
209219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4408: "
210219820Sjeff			"Unsupported Method (%s)\n",
211219820Sjeff			ib_get_sa_method_str(p_rcvd_mad->method));
212219820Sjeff		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
213219820Sjeff		goto Exit;
214219820Sjeff	}
215219820Sjeff
216219820Sjeff	/* update the requester physical port. */
217219820Sjeff	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
218219820Sjeff						osm_madw_get_mad_addr_ptr
219219820Sjeff						(p_madw));
220219820Sjeff	if (p_req_physp == NULL) {
221219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4407: "
222219820Sjeff			"Cannot find requester physical port\n");
223219820Sjeff		goto Exit;
224219820Sjeff	}
225219820Sjeff
226219820Sjeff	cl_qlist_init(&rec_list);
227219820Sjeff
228219820Sjeff	context.p_rcvd_rec = p_rcvd_rec;
229219820Sjeff	context.p_list = &rec_list;
230219820Sjeff	context.comp_mask = p_rcvd_mad->comp_mask;
231219820Sjeff	context.sa = sa;
232219820Sjeff	context.p_req_physp = p_req_physp;
233219820Sjeff
234219820Sjeff	cl_plock_acquire(sa->p_lock);
235219820Sjeff
236219820Sjeff	/* Go over all switches */
237219820Sjeff	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl,
238219820Sjeff			   __osm_lftr_rcv_by_comp_mask, &context);
239219820Sjeff
240219820Sjeff	cl_plock_release(sa->p_lock);
241219820Sjeff
242219820Sjeff	osm_sa_respond(sa, p_madw, sizeof(ib_lft_record_t), &rec_list);
243219820Sjeff
244219820SjeffExit:
245219820Sjeff	OSM_LOG_EXIT(sa->p_log);
246219820Sjeff}
247