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_smir_rcv_t.
39219820Sjeff * This object represents the SMInfo 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_madw.h>
55219820Sjeff#include <opensm/osm_log.h>
56219820Sjeff#include <opensm/osm_subnet.h>
57219820Sjeff#include <opensm/osm_mad_pool.h>
58219820Sjeff#include <opensm/osm_helper.h>
59219820Sjeff#include <opensm/osm_msgdef.h>
60219820Sjeff#include <opensm/osm_port.h>
61219820Sjeff#include <opensm/osm_pkey.h>
62219820Sjeff#include <opensm/osm_remote_sm.h>
63219820Sjeff#include <opensm/osm_sa.h>
64219820Sjeff#include <opensm/osm_opensm.h>
65219820Sjeff
66219820Sjefftypedef struct osm_smir_item {
67219820Sjeff	cl_list_item_t list_item;
68219820Sjeff	ib_sminfo_record_t rec;
69219820Sjeff} osm_smir_item_t;
70219820Sjeff
71219820Sjefftypedef struct osm_smir_search_ctxt {
72219820Sjeff	const ib_sminfo_record_t *p_rcvd_rec;
73219820Sjeff	ib_net64_t comp_mask;
74219820Sjeff	cl_qlist_t *p_list;
75219820Sjeff	osm_sa_t *sa;
76219820Sjeff	const osm_physp_t *p_req_physp;
77219820Sjeff} osm_smir_search_ctxt_t;
78219820Sjeff
79219820Sjeffstatic ib_api_status_t
80219820Sjeff__osm_smir_rcv_new_smir(IN osm_sa_t * sa,
81219820Sjeff			IN const osm_port_t * const p_port,
82219820Sjeff			IN cl_qlist_t * const p_list,
83219820Sjeff			IN ib_net64_t const guid,
84219820Sjeff			IN ib_net32_t const act_count,
85219820Sjeff			IN uint8_t const pri_state,
86219820Sjeff			IN const osm_physp_t * const p_req_physp)
87219820Sjeff{
88219820Sjeff	osm_smir_item_t *p_rec_item;
89219820Sjeff	ib_api_status_t status = IB_SUCCESS;
90219820Sjeff
91219820Sjeff	OSM_LOG_ENTER(sa->p_log);
92219820Sjeff
93219820Sjeff	p_rec_item = malloc(sizeof(*p_rec_item));
94219820Sjeff	if (p_rec_item == NULL) {
95219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2801: "
96219820Sjeff			"rec_item alloc failed\n");
97219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
98219820Sjeff		goto Exit;
99219820Sjeff	}
100219820Sjeff
101219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
102219820Sjeff		"New SMInfo: GUID 0x%016" PRIx64 "\n", cl_ntoh64(guid));
103219820Sjeff
104219820Sjeff	memset(p_rec_item, 0, sizeof(*p_rec_item));
105219820Sjeff
106219820Sjeff	p_rec_item->rec.lid = osm_port_get_base_lid(p_port);
107219820Sjeff	p_rec_item->rec.sm_info.guid = guid;
108219820Sjeff	p_rec_item->rec.sm_info.act_count = act_count;
109219820Sjeff	p_rec_item->rec.sm_info.pri_state = pri_state;
110219820Sjeff
111219820Sjeff	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
112219820Sjeff
113219820SjeffExit:
114219820Sjeff	OSM_LOG_EXIT(sa->p_log);
115219820Sjeff	return (status);
116219820Sjeff}
117219820Sjeff
118219820Sjeff/**********************************************************************
119219820Sjeff **********************************************************************/
120219820Sjeffstatic void
121219820Sjeff__osm_sa_smir_by_comp_mask(IN osm_sa_t * sa,
122219820Sjeff			   IN const osm_remote_sm_t * const p_rem_sm,
123219820Sjeff			   osm_smir_search_ctxt_t * const p_ctxt)
124219820Sjeff{
125219820Sjeff	const ib_sminfo_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
126219820Sjeff	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
127219820Sjeff	ib_net64_t const comp_mask = p_ctxt->comp_mask;
128219820Sjeff
129219820Sjeff	OSM_LOG_ENTER(sa->p_log);
130219820Sjeff
131219820Sjeff	if (comp_mask & IB_SMIR_COMPMASK_GUID) {
132219820Sjeff		if (p_rem_sm->smi.guid != p_rcvd_rec->sm_info.guid)
133219820Sjeff			goto Exit;
134219820Sjeff	}
135219820Sjeff
136219820Sjeff	if (comp_mask & IB_SMIR_COMPMASK_PRIORITY) {
137219820Sjeff		if (ib_sminfo_get_priority(&p_rem_sm->smi) !=
138219820Sjeff		    ib_sminfo_get_priority(&p_rcvd_rec->sm_info))
139219820Sjeff			goto Exit;
140219820Sjeff	}
141219820Sjeff
142219820Sjeff	if (comp_mask & IB_SMIR_COMPMASK_SMSTATE) {
143219820Sjeff		if (ib_sminfo_get_state(&p_rem_sm->smi) !=
144219820Sjeff		    ib_sminfo_get_state(&p_rcvd_rec->sm_info))
145219820Sjeff			goto Exit;
146219820Sjeff	}
147219820Sjeff
148219820Sjeff	/* Implement any other needed search cases */
149219820Sjeff
150219820Sjeff	__osm_smir_rcv_new_smir(sa, p_rem_sm->p_port, p_ctxt->p_list,
151219820Sjeff				p_rem_sm->smi.guid,
152219820Sjeff				p_rem_sm->smi.act_count,
153219820Sjeff				p_rem_sm->smi.pri_state, p_req_physp);
154219820Sjeff
155219820SjeffExit:
156219820Sjeff	OSM_LOG_EXIT(sa->p_log);
157219820Sjeff}
158219820Sjeff
159219820Sjeff/**********************************************************************
160219820Sjeff **********************************************************************/
161219820Sjeffstatic void
162219820Sjeff__osm_sa_smir_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
163219820Sjeff			      IN void *context)
164219820Sjeff{
165219820Sjeff	const osm_remote_sm_t *const p_rem_sm = (osm_remote_sm_t *) p_map_item;
166219820Sjeff	osm_smir_search_ctxt_t *const p_ctxt =
167219820Sjeff	    (osm_smir_search_ctxt_t *) context;
168219820Sjeff
169219820Sjeff	__osm_sa_smir_by_comp_mask(p_ctxt->sa, p_rem_sm, p_ctxt);
170219820Sjeff}
171219820Sjeff
172219820Sjeff/**********************************************************************
173219820Sjeff **********************************************************************/
174219820Sjeffvoid osm_smir_rcv_process(IN void *ctx, IN void *data)
175219820Sjeff{
176219820Sjeff	osm_sa_t *sa = ctx;
177219820Sjeff	osm_madw_t *p_madw = data;
178219820Sjeff	const ib_sa_mad_t *sad_mad;
179219820Sjeff	const ib_sminfo_record_t *p_rcvd_rec;
180219820Sjeff	const osm_port_t *p_port = NULL;
181219820Sjeff	const ib_sm_info_t *p_smi;
182219820Sjeff	cl_qlist_t rec_list;
183219820Sjeff	osm_smir_search_ctxt_t context;
184219820Sjeff	ib_api_status_t status = IB_SUCCESS;
185219820Sjeff	ib_net64_t comp_mask;
186219820Sjeff	ib_net64_t port_guid;
187219820Sjeff	osm_physp_t *p_req_physp;
188219820Sjeff	osm_port_t *local_port;
189219820Sjeff	osm_remote_sm_t *p_rem_sm;
190219820Sjeff	cl_qmap_t *p_sm_guid_tbl;
191219820Sjeff	uint8_t pri_state;
192219820Sjeff
193219820Sjeff	CL_ASSERT(sa);
194219820Sjeff
195219820Sjeff	OSM_LOG_ENTER(sa->p_log);
196219820Sjeff
197219820Sjeff	CL_ASSERT(p_madw);
198219820Sjeff
199219820Sjeff	sad_mad = osm_madw_get_sa_mad_ptr(p_madw);
200219820Sjeff	p_rcvd_rec =
201219820Sjeff	    (ib_sminfo_record_t *) ib_sa_mad_get_payload_ptr(sad_mad);
202219820Sjeff	comp_mask = sad_mad->comp_mask;
203219820Sjeff
204219820Sjeff	CL_ASSERT(sad_mad->attr_id == IB_MAD_ATTR_SMINFO_RECORD);
205219820Sjeff
206219820Sjeff	/* we only support SubnAdmGet and SubnAdmGetTable methods */
207219820Sjeff	if (sad_mad->method != IB_MAD_METHOD_GET &&
208219820Sjeff	    sad_mad->method != IB_MAD_METHOD_GETTABLE) {
209219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2804: "
210219820Sjeff			"Unsupported Method (%s)\n",
211219820Sjeff			ib_get_sa_method_str(sad_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 2803: "
222219820Sjeff			"Cannot find requester physical port\n");
223219820Sjeff		goto Exit;
224219820Sjeff	}
225219820Sjeff
226219820Sjeff	if (osm_log_is_active(sa->p_log, OSM_LOG_DEBUG))
227219820Sjeff		osm_dump_sm_info_record(sa->p_log, p_rcvd_rec, OSM_LOG_DEBUG);
228219820Sjeff
229219820Sjeff	p_smi = &p_rcvd_rec->sm_info;
230219820Sjeff
231219820Sjeff	cl_qlist_init(&rec_list);
232219820Sjeff
233219820Sjeff	context.p_rcvd_rec = p_rcvd_rec;
234219820Sjeff	context.p_list = &rec_list;
235219820Sjeff	context.comp_mask = sad_mad->comp_mask;
236219820Sjeff	context.sa = sa;
237219820Sjeff	context.p_req_physp = p_req_physp;
238219820Sjeff
239219820Sjeff	cl_plock_acquire(sa->p_lock);
240219820Sjeff
241219820Sjeff	/*
242219820Sjeff	   If the user specified a LID, it obviously narrows our
243219820Sjeff	   work load, since we don't have to search every port
244219820Sjeff	 */
245219820Sjeff	if (comp_mask & IB_SMIR_COMPMASK_LID) {
246219820Sjeff		status =
247219820Sjeff		    osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
248219820Sjeff					     &p_port);
249219820Sjeff		if ((status != IB_SUCCESS) || (p_port == NULL)) {
250219820Sjeff			status = IB_NOT_FOUND;
251219820Sjeff			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2806: "
252219820Sjeff				"No port found with LID %u\n",
253219820Sjeff				cl_ntoh16(p_rcvd_rec->lid));
254219820Sjeff		}
255219820Sjeff	}
256219820Sjeff
257219820Sjeff	if (status == IB_SUCCESS) {
258219820Sjeff		/* Handle our own SM first */
259219820Sjeff		local_port = osm_get_port_by_guid(sa->p_subn,
260219820Sjeff						  sa->p_subn->sm_port_guid);
261219820Sjeff		if (!local_port) {
262219820Sjeff			cl_plock_release(sa->p_lock);
263219820Sjeff			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2809: "
264219820Sjeff				"No port found with GUID 0x%016" PRIx64 "\n",
265219820Sjeff				cl_ntoh64(sa->p_subn->sm_port_guid));
266219820Sjeff			goto Exit;
267219820Sjeff		}
268219820Sjeff
269219820Sjeff		if (!p_port || local_port == p_port) {
270219820Sjeff			if (FALSE ==
271219820Sjeff			    osm_physp_share_pkey(sa->p_log, p_req_physp,
272219820Sjeff						 local_port->p_physp)) {
273219820Sjeff				cl_plock_release(sa->p_lock);
274219820Sjeff				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2805: "
275219820Sjeff					"Cannot get SMInfo record due to pkey violation\n");
276219820Sjeff				goto Exit;
277219820Sjeff			}
278219820Sjeff
279219820Sjeff			/* Check that other search components specified match */
280219820Sjeff			if ((comp_mask & IB_SMIR_COMPMASK_GUID) &&
281219820Sjeff			    sa->p_subn->sm_port_guid != p_smi->guid)
282219820Sjeff				goto Remotes;
283219820Sjeff			if ((comp_mask & IB_SMIR_COMPMASK_PRIORITY) &&
284219820Sjeff			    sa->p_subn->opt.sm_priority !=
285219820Sjeff			       ib_sminfo_get_priority(p_smi))
286219820Sjeff				goto Remotes;
287219820Sjeff			if ((comp_mask & IB_SMIR_COMPMASK_SMSTATE) &&
288219820Sjeff			    sa->p_subn->sm_state != ib_sminfo_get_state(p_smi))
289219820Sjeff				goto Remotes;
290219820Sjeff
291219820Sjeff			/* Now, add local SMInfo to list */
292219820Sjeff			pri_state = sa->p_subn->sm_state & 0x0F;
293219820Sjeff			pri_state |=
294219820Sjeff			    (sa->p_subn->opt.sm_priority & 0x0F) << 4;
295219820Sjeff			__osm_smir_rcv_new_smir(sa, local_port, context.p_list,
296219820Sjeff						sa->p_subn->sm_port_guid,
297219820Sjeff						cl_ntoh32(sa->p_subn->p_osm->stats.qp0_mads_sent),
298219820Sjeff						pri_state, p_req_physp);
299219820Sjeff		}
300219820Sjeff
301219820Sjeff	      Remotes:
302219820Sjeff		if (p_port && p_port != local_port) {
303219820Sjeff			/* Find remote SM corresponding to p_port */
304219820Sjeff			port_guid = osm_port_get_guid(p_port);
305219820Sjeff			p_sm_guid_tbl = &sa->p_subn->sm_guid_tbl;
306219820Sjeff			p_rem_sm =
307219820Sjeff			    (osm_remote_sm_t *) cl_qmap_get(p_sm_guid_tbl,
308219820Sjeff							    port_guid);
309219820Sjeff			if (p_rem_sm !=
310219820Sjeff			    (osm_remote_sm_t *) cl_qmap_end(p_sm_guid_tbl))
311219820Sjeff				__osm_sa_smir_by_comp_mask(sa, p_rem_sm,
312219820Sjeff							   &context);
313219820Sjeff			else
314219820Sjeff				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 280A: "
315219820Sjeff					"No remote SM for GUID 0x%016" PRIx64
316219820Sjeff					"\n", cl_ntoh64(port_guid));
317219820Sjeff		} else {
318219820Sjeff			/* Go over all other known (remote) SMs */
319219820Sjeff			cl_qmap_apply_func(&sa->p_subn->sm_guid_tbl,
320219820Sjeff					   __osm_sa_smir_by_comp_mask_cb,
321219820Sjeff					   &context);
322219820Sjeff		}
323219820Sjeff	}
324219820Sjeff
325219820Sjeff	cl_plock_release(sa->p_lock);
326219820Sjeff
327219820Sjeff	osm_sa_respond(sa, p_madw, sizeof(ib_sminfo_record_t), &rec_list);
328219820Sjeff
329219820SjeffExit:
330219820Sjeff	OSM_LOG_EXIT(sa->p_log);
331219820Sjeff}
332