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_sir_rcv_t.
39219820Sjeff * This object represents the SwitchInfo 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_node.h>
53219820Sjeff#include <opensm/osm_helper.h>
54219820Sjeff#include <opensm/osm_pkey.h>
55219820Sjeff#include <opensm/osm_sa.h>
56219820Sjeff
57219820Sjefftypedef struct osm_sir_item {
58219820Sjeff	cl_list_item_t list_item;
59219820Sjeff	ib_switch_info_record_t rec;
60219820Sjeff} osm_sir_item_t;
61219820Sjeff
62219820Sjefftypedef struct osm_sir_search_ctxt {
63219820Sjeff	const ib_switch_info_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_sir_search_ctxt_t;
69219820Sjeff
70219820Sjeff/**********************************************************************
71219820Sjeff **********************************************************************/
72219820Sjeffstatic ib_api_status_t
73219820Sjeff__osm_sir_rcv_new_sir(IN osm_sa_t * sa,
74219820Sjeff		      IN const osm_switch_t * const p_sw,
75219820Sjeff		      IN cl_qlist_t * const p_list, IN ib_net16_t const lid)
76219820Sjeff{
77219820Sjeff	osm_sir_item_t *p_rec_item;
78219820Sjeff	ib_api_status_t status = IB_SUCCESS;
79219820Sjeff
80219820Sjeff	OSM_LOG_ENTER(sa->p_log);
81219820Sjeff
82219820Sjeff	p_rec_item = malloc(sizeof(*p_rec_item));
83219820Sjeff	if (p_rec_item == NULL) {
84219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5308: "
85219820Sjeff			"rec_item alloc failed\n");
86219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
87219820Sjeff		goto Exit;
88219820Sjeff	}
89219820Sjeff
90219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
91219820Sjeff		"New SwitchInfoRecord: lid %u\n", cl_ntoh16(lid));
92219820Sjeff
93219820Sjeff	memset(p_rec_item, 0, sizeof(*p_rec_item));
94219820Sjeff
95219820Sjeff	p_rec_item->rec.lid = lid;
96219820Sjeff	p_rec_item->rec.switch_info = p_sw->switch_info;
97219820Sjeff
98219820Sjeff	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
99219820Sjeff
100219820SjeffExit:
101219820Sjeff	OSM_LOG_EXIT(sa->p_log);
102219820Sjeff	return (status);
103219820Sjeff}
104219820Sjeff
105219820Sjeff/**********************************************************************
106219820Sjeff **********************************************************************/
107219820Sjeffstatic void
108219820Sjeff__osm_sir_rcv_create_sir(IN osm_sa_t * sa,
109219820Sjeff			 IN const osm_switch_t * const p_sw,
110219820Sjeff			 IN cl_qlist_t * const p_list,
111219820Sjeff			 IN ib_net16_t const match_lid,
112219820Sjeff			 IN const osm_physp_t * const p_req_physp)
113219820Sjeff{
114219820Sjeff	osm_port_t *p_port;
115219820Sjeff	const osm_physp_t *p_physp;
116219820Sjeff	uint16_t match_lid_ho;
117219820Sjeff	ib_net16_t min_lid_ho;
118219820Sjeff	ib_net16_t max_lid_ho;
119219820Sjeff
120219820Sjeff	OSM_LOG_ENTER(sa->p_log);
121219820Sjeff
122219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
123219820Sjeff		"Looking for SwitchInfoRecord with LID: %u\n",
124219820Sjeff		cl_ntoh16(match_lid));
125219820Sjeff
126219820Sjeff	/* In switches, the port guid is the node guid. */
127219820Sjeff	p_port =
128219820Sjeff	    osm_get_port_by_guid(sa->p_subn, p_sw->p_node->node_info.port_guid);
129219820Sjeff	if (!p_port) {
130219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 530A: "
131219820Sjeff			"Failed to find Port by Node Guid:0x%016" PRIx64
132219820Sjeff			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
133219820Sjeff		goto Exit;
134219820Sjeff	}
135219820Sjeff
136219820Sjeff	/* check that the requester physp and the current physp are under
137219820Sjeff	   the same partition. */
138219820Sjeff	p_physp = p_port->p_physp;
139219820Sjeff	if (!p_physp) {
140219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 530B: "
141219820Sjeff			"Failed to find default physical Port by Node Guid:0x%016"
142219820Sjeff			PRIx64 "\n",
143219820Sjeff			cl_ntoh64(p_sw->p_node->node_info.node_guid));
144219820Sjeff		goto Exit;
145219820Sjeff	}
146219820Sjeff	if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp))
147219820Sjeff		goto Exit;
148219820Sjeff
149219820Sjeff	/* get the port 0 of the switch */
150219820Sjeff	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
151219820Sjeff
152219820Sjeff	match_lid_ho = cl_ntoh16(match_lid);
153219820Sjeff	if (match_lid_ho) {
154219820Sjeff		/*
155219820Sjeff		   We validate that the lid belongs to this switch.
156219820Sjeff		 */
157219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
158219820Sjeff			"Comparing LID: %u <= %u <= %u\n",
159219820Sjeff			min_lid_ho, match_lid_ho, max_lid_ho);
160219820Sjeff
161219820Sjeff		if (match_lid_ho < min_lid_ho || match_lid_ho > max_lid_ho)
162219820Sjeff			goto Exit;
163219820Sjeff
164219820Sjeff	}
165219820Sjeff
166219820Sjeff	__osm_sir_rcv_new_sir(sa, p_sw, p_list, osm_port_get_base_lid(p_port));
167219820Sjeff
168219820SjeffExit:
169219820Sjeff	OSM_LOG_EXIT(sa->p_log);
170219820Sjeff}
171219820Sjeff
172219820Sjeff/**********************************************************************
173219820Sjeff **********************************************************************/
174219820Sjeffstatic void
175219820Sjeff__osm_sir_rcv_by_comp_mask(IN cl_map_item_t * const p_map_item,
176219820Sjeff			   IN void *context)
177219820Sjeff{
178219820Sjeff	const osm_sir_search_ctxt_t *const p_ctxt =
179219820Sjeff	    (osm_sir_search_ctxt_t *) context;
180219820Sjeff	const osm_switch_t *const p_sw = (osm_switch_t *) p_map_item;
181219820Sjeff	const ib_switch_info_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
182219820Sjeff	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
183219820Sjeff	osm_sa_t *sa = p_ctxt->sa;
184219820Sjeff	ib_net64_t const comp_mask = p_ctxt->comp_mask;
185219820Sjeff	ib_net16_t match_lid = 0;
186219820Sjeff
187219820Sjeff	OSM_LOG_ENTER(p_ctxt->sa->p_log);
188219820Sjeff
189219820Sjeff	osm_dump_switch_info(p_ctxt->sa->p_log,
190219820Sjeff			     &p_sw->switch_info, OSM_LOG_VERBOSE);
191219820Sjeff
192219820Sjeff	if (comp_mask & IB_SWIR_COMPMASK_LID) {
193219820Sjeff		match_lid = p_rcvd_rec->lid;
194219820Sjeff		if (!match_lid)
195219820Sjeff			goto Exit;
196219820Sjeff	}
197219820Sjeff
198219820Sjeff	__osm_sir_rcv_create_sir(sa, p_sw, p_ctxt->p_list,
199219820Sjeff				 match_lid, p_req_physp);
200219820Sjeff
201219820SjeffExit:
202219820Sjeff	OSM_LOG_EXIT(p_ctxt->sa->p_log);
203219820Sjeff}
204219820Sjeff
205219820Sjeff/**********************************************************************
206219820Sjeff **********************************************************************/
207219820Sjeffvoid osm_sir_rcv_process(IN void *ctx, IN void *data)
208219820Sjeff{
209219820Sjeff	osm_sa_t *sa = ctx;
210219820Sjeff	osm_madw_t *p_madw = data;
211219820Sjeff	const ib_sa_mad_t *sad_mad;
212219820Sjeff	const ib_switch_info_record_t *p_rcvd_rec;
213219820Sjeff	cl_qlist_t rec_list;
214219820Sjeff	osm_sir_search_ctxt_t context;
215219820Sjeff	osm_physp_t *p_req_physp;
216219820Sjeff
217219820Sjeff	CL_ASSERT(sa);
218219820Sjeff
219219820Sjeff	OSM_LOG_ENTER(sa->p_log);
220219820Sjeff
221219820Sjeff	CL_ASSERT(p_madw);
222219820Sjeff
223219820Sjeff	sad_mad = osm_madw_get_sa_mad_ptr(p_madw);
224219820Sjeff	p_rcvd_rec =
225219820Sjeff	    (ib_switch_info_record_t *) ib_sa_mad_get_payload_ptr(sad_mad);
226219820Sjeff
227219820Sjeff	CL_ASSERT(sad_mad->attr_id == IB_MAD_ATTR_SWITCH_INFO_RECORD);
228219820Sjeff
229219820Sjeff	/* we only support SubnAdmGet and SubnAdmGetTable methods */
230219820Sjeff	if (sad_mad->method != IB_MAD_METHOD_GET &&
231219820Sjeff	    sad_mad->method != IB_MAD_METHOD_GETTABLE) {
232219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5305: "
233219820Sjeff			"Unsupported Method (%s)\n",
234219820Sjeff			ib_get_sa_method_str(sad_mad->method));
235219820Sjeff		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
236219820Sjeff		goto Exit;
237219820Sjeff	}
238219820Sjeff
239219820Sjeff	/* update the requester physical port. */
240219820Sjeff	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
241219820Sjeff						osm_madw_get_mad_addr_ptr
242219820Sjeff						(p_madw));
243219820Sjeff	if (p_req_physp == NULL) {
244219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5304: "
245219820Sjeff			"Cannot find requester physical port\n");
246219820Sjeff		goto Exit;
247219820Sjeff	}
248219820Sjeff
249219820Sjeff	if (osm_log_is_active(sa->p_log, OSM_LOG_DEBUG))
250219820Sjeff		osm_dump_switch_info_record(sa->p_log, p_rcvd_rec,
251219820Sjeff					    OSM_LOG_DEBUG);
252219820Sjeff
253219820Sjeff	cl_qlist_init(&rec_list);
254219820Sjeff
255219820Sjeff	context.p_rcvd_rec = p_rcvd_rec;
256219820Sjeff	context.p_list = &rec_list;
257219820Sjeff	context.comp_mask = sad_mad->comp_mask;
258219820Sjeff	context.sa = sa;
259219820Sjeff	context.p_req_physp = p_req_physp;
260219820Sjeff
261219820Sjeff	cl_plock_acquire(sa->p_lock);
262219820Sjeff
263219820Sjeff	/* Go over all switches */
264219820Sjeff	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl,
265219820Sjeff			   __osm_sir_rcv_by_comp_mask, &context);
266219820Sjeff
267219820Sjeff	cl_plock_release(sa->p_lock);
268219820Sjeff
269219820Sjeff	osm_sa_respond(sa, p_madw, sizeof(ib_switch_info_record_t), &rec_list);
270219820Sjeff
271219820SjeffExit:
272219820Sjeff	OSM_LOG_EXIT(sa->p_log);
273219820Sjeff}
274