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_smir_rcv_t.
39 * This object represents the SMInfo 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_qmap.h>
50#include <complib/cl_passivelock.h>
51#include <complib/cl_debug.h>
52#include <complib/cl_qlist.h>
53#include <vendor/osm_vendor_api.h>
54#include <opensm/osm_madw.h>
55#include <opensm/osm_log.h>
56#include <opensm/osm_subnet.h>
57#include <opensm/osm_mad_pool.h>
58#include <opensm/osm_helper.h>
59#include <opensm/osm_msgdef.h>
60#include <opensm/osm_port.h>
61#include <opensm/osm_pkey.h>
62#include <opensm/osm_remote_sm.h>
63#include <opensm/osm_sa.h>
64#include <opensm/osm_opensm.h>
65
66typedef struct osm_smir_item {
67	cl_list_item_t list_item;
68	ib_sminfo_record_t rec;
69} osm_smir_item_t;
70
71typedef struct osm_smir_search_ctxt {
72	const ib_sminfo_record_t *p_rcvd_rec;
73	ib_net64_t comp_mask;
74	cl_qlist_t *p_list;
75	osm_sa_t *sa;
76	const osm_physp_t *p_req_physp;
77} osm_smir_search_ctxt_t;
78
79static ib_api_status_t
80__osm_smir_rcv_new_smir(IN osm_sa_t * sa,
81			IN const osm_port_t * const p_port,
82			IN cl_qlist_t * const p_list,
83			IN ib_net64_t const guid,
84			IN ib_net32_t const act_count,
85			IN uint8_t const pri_state,
86			IN const osm_physp_t * const p_req_physp)
87{
88	osm_smir_item_t *p_rec_item;
89	ib_api_status_t status = IB_SUCCESS;
90
91	OSM_LOG_ENTER(sa->p_log);
92
93	p_rec_item = malloc(sizeof(*p_rec_item));
94	if (p_rec_item == NULL) {
95		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2801: "
96			"rec_item alloc failed\n");
97		status = IB_INSUFFICIENT_RESOURCES;
98		goto Exit;
99	}
100
101	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
102		"New SMInfo: GUID 0x%016" PRIx64 "\n", cl_ntoh64(guid));
103
104	memset(p_rec_item, 0, sizeof(*p_rec_item));
105
106	p_rec_item->rec.lid = osm_port_get_base_lid(p_port);
107	p_rec_item->rec.sm_info.guid = guid;
108	p_rec_item->rec.sm_info.act_count = act_count;
109	p_rec_item->rec.sm_info.pri_state = pri_state;
110
111	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
112
113Exit:
114	OSM_LOG_EXIT(sa->p_log);
115	return (status);
116}
117
118/**********************************************************************
119 **********************************************************************/
120static void
121__osm_sa_smir_by_comp_mask(IN osm_sa_t * sa,
122			   IN const osm_remote_sm_t * const p_rem_sm,
123			   osm_smir_search_ctxt_t * const p_ctxt)
124{
125	const ib_sminfo_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
126	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
127	ib_net64_t const comp_mask = p_ctxt->comp_mask;
128
129	OSM_LOG_ENTER(sa->p_log);
130
131	if (comp_mask & IB_SMIR_COMPMASK_GUID) {
132		if (p_rem_sm->smi.guid != p_rcvd_rec->sm_info.guid)
133			goto Exit;
134	}
135
136	if (comp_mask & IB_SMIR_COMPMASK_PRIORITY) {
137		if (ib_sminfo_get_priority(&p_rem_sm->smi) !=
138		    ib_sminfo_get_priority(&p_rcvd_rec->sm_info))
139			goto Exit;
140	}
141
142	if (comp_mask & IB_SMIR_COMPMASK_SMSTATE) {
143		if (ib_sminfo_get_state(&p_rem_sm->smi) !=
144		    ib_sminfo_get_state(&p_rcvd_rec->sm_info))
145			goto Exit;
146	}
147
148	/* Implement any other needed search cases */
149
150	__osm_smir_rcv_new_smir(sa, p_rem_sm->p_port, p_ctxt->p_list,
151				p_rem_sm->smi.guid,
152				p_rem_sm->smi.act_count,
153				p_rem_sm->smi.pri_state, p_req_physp);
154
155Exit:
156	OSM_LOG_EXIT(sa->p_log);
157}
158
159/**********************************************************************
160 **********************************************************************/
161static void
162__osm_sa_smir_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
163			      IN void *context)
164{
165	const osm_remote_sm_t *const p_rem_sm = (osm_remote_sm_t *) p_map_item;
166	osm_smir_search_ctxt_t *const p_ctxt =
167	    (osm_smir_search_ctxt_t *) context;
168
169	__osm_sa_smir_by_comp_mask(p_ctxt->sa, p_rem_sm, p_ctxt);
170}
171
172/**********************************************************************
173 **********************************************************************/
174void osm_smir_rcv_process(IN void *ctx, IN void *data)
175{
176	osm_sa_t *sa = ctx;
177	osm_madw_t *p_madw = data;
178	const ib_sa_mad_t *sad_mad;
179	const ib_sminfo_record_t *p_rcvd_rec;
180	const osm_port_t *p_port = NULL;
181	const ib_sm_info_t *p_smi;
182	cl_qlist_t rec_list;
183	osm_smir_search_ctxt_t context;
184	ib_api_status_t status = IB_SUCCESS;
185	ib_net64_t comp_mask;
186	ib_net64_t port_guid;
187	osm_physp_t *p_req_physp;
188	osm_port_t *local_port;
189	osm_remote_sm_t *p_rem_sm;
190	cl_qmap_t *p_sm_guid_tbl;
191	uint8_t pri_state;
192
193	CL_ASSERT(sa);
194
195	OSM_LOG_ENTER(sa->p_log);
196
197	CL_ASSERT(p_madw);
198
199	sad_mad = osm_madw_get_sa_mad_ptr(p_madw);
200	p_rcvd_rec =
201	    (ib_sminfo_record_t *) ib_sa_mad_get_payload_ptr(sad_mad);
202	comp_mask = sad_mad->comp_mask;
203
204	CL_ASSERT(sad_mad->attr_id == IB_MAD_ATTR_SMINFO_RECORD);
205
206	/* we only support SubnAdmGet and SubnAdmGetTable methods */
207	if (sad_mad->method != IB_MAD_METHOD_GET &&
208	    sad_mad->method != IB_MAD_METHOD_GETTABLE) {
209		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2804: "
210			"Unsupported Method (%s)\n",
211			ib_get_sa_method_str(sad_mad->method));
212		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
213		goto Exit;
214	}
215
216	/* update the requester physical port. */
217	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
218						osm_madw_get_mad_addr_ptr
219						(p_madw));
220	if (p_req_physp == NULL) {
221		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2803: "
222			"Cannot find requester physical port\n");
223		goto Exit;
224	}
225
226	if (osm_log_is_active(sa->p_log, OSM_LOG_DEBUG))
227		osm_dump_sm_info_record(sa->p_log, p_rcvd_rec, OSM_LOG_DEBUG);
228
229	p_smi = &p_rcvd_rec->sm_info;
230
231	cl_qlist_init(&rec_list);
232
233	context.p_rcvd_rec = p_rcvd_rec;
234	context.p_list = &rec_list;
235	context.comp_mask = sad_mad->comp_mask;
236	context.sa = sa;
237	context.p_req_physp = p_req_physp;
238
239	cl_plock_acquire(sa->p_lock);
240
241	/*
242	   If the user specified a LID, it obviously narrows our
243	   work load, since we don't have to search every port
244	 */
245	if (comp_mask & IB_SMIR_COMPMASK_LID) {
246		status =
247		    osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
248					     &p_port);
249		if ((status != IB_SUCCESS) || (p_port == NULL)) {
250			status = IB_NOT_FOUND;
251			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2806: "
252				"No port found with LID %u\n",
253				cl_ntoh16(p_rcvd_rec->lid));
254		}
255	}
256
257	if (status == IB_SUCCESS) {
258		/* Handle our own SM first */
259		local_port = osm_get_port_by_guid(sa->p_subn,
260						  sa->p_subn->sm_port_guid);
261		if (!local_port) {
262			cl_plock_release(sa->p_lock);
263			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2809: "
264				"No port found with GUID 0x%016" PRIx64 "\n",
265				cl_ntoh64(sa->p_subn->sm_port_guid));
266			goto Exit;
267		}
268
269		if (!p_port || local_port == p_port) {
270			if (FALSE ==
271			    osm_physp_share_pkey(sa->p_log, p_req_physp,
272						 local_port->p_physp)) {
273				cl_plock_release(sa->p_lock);
274				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2805: "
275					"Cannot get SMInfo record due to pkey violation\n");
276				goto Exit;
277			}
278
279			/* Check that other search components specified match */
280			if ((comp_mask & IB_SMIR_COMPMASK_GUID) &&
281			    sa->p_subn->sm_port_guid != p_smi->guid)
282				goto Remotes;
283			if ((comp_mask & IB_SMIR_COMPMASK_PRIORITY) &&
284			    sa->p_subn->opt.sm_priority !=
285			       ib_sminfo_get_priority(p_smi))
286				goto Remotes;
287			if ((comp_mask & IB_SMIR_COMPMASK_SMSTATE) &&
288			    sa->p_subn->sm_state != ib_sminfo_get_state(p_smi))
289				goto Remotes;
290
291			/* Now, add local SMInfo to list */
292			pri_state = sa->p_subn->sm_state & 0x0F;
293			pri_state |=
294			    (sa->p_subn->opt.sm_priority & 0x0F) << 4;
295			__osm_smir_rcv_new_smir(sa, local_port, context.p_list,
296						sa->p_subn->sm_port_guid,
297						cl_ntoh32(sa->p_subn->p_osm->stats.qp0_mads_sent),
298						pri_state, p_req_physp);
299		}
300
301	      Remotes:
302		if (p_port && p_port != local_port) {
303			/* Find remote SM corresponding to p_port */
304			port_guid = osm_port_get_guid(p_port);
305			p_sm_guid_tbl = &sa->p_subn->sm_guid_tbl;
306			p_rem_sm =
307			    (osm_remote_sm_t *) cl_qmap_get(p_sm_guid_tbl,
308							    port_guid);
309			if (p_rem_sm !=
310			    (osm_remote_sm_t *) cl_qmap_end(p_sm_guid_tbl))
311				__osm_sa_smir_by_comp_mask(sa, p_rem_sm,
312							   &context);
313			else
314				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 280A: "
315					"No remote SM for GUID 0x%016" PRIx64
316					"\n", cl_ntoh64(port_guid));
317		} else {
318			/* Go over all other known (remote) SMs */
319			cl_qmap_apply_func(&sa->p_subn->sm_guid_tbl,
320					   __osm_sa_smir_by_comp_mask_cb,
321					   &context);
322		}
323	}
324
325	cl_plock_release(sa->p_lock);
326
327	osm_sa_respond(sa, p_madw, sizeof(ib_sminfo_record_t), &rec_list);
328
329Exit:
330	OSM_LOG_EXIT(sa->p_log);
331}
332