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_nr_rcv_t.
39 * This object represents the NodeInfo 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_node.h>
53#include <opensm/osm_helper.h>
54#include <opensm/osm_pkey.h>
55#include <opensm/osm_sa.h>
56
57typedef struct osm_nr_item {
58	cl_list_item_t list_item;
59	ib_node_record_t rec;
60} osm_nr_item_t;
61
62typedef struct osm_nr_search_ctxt {
63	const ib_node_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_nr_search_ctxt_t;
69
70/**********************************************************************
71 **********************************************************************/
72static ib_api_status_t
73__osm_nr_rcv_new_nr(IN osm_sa_t * sa,
74		    IN const osm_node_t * const p_node,
75		    IN cl_qlist_t * const p_list,
76		    IN ib_net64_t const port_guid, IN ib_net16_t const lid)
77{
78	osm_nr_item_t *p_rec_item;
79	ib_api_status_t status = IB_SUCCESS;
80
81	OSM_LOG_ENTER(sa->p_log);
82
83	p_rec_item = malloc(sizeof(*p_rec_item));
84	if (p_rec_item == NULL) {
85		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1D02: "
86			"rec_item alloc failed\n");
87		status = IB_INSUFFICIENT_RESOURCES;
88		goto Exit;
89	}
90
91	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
92		"New NodeRecord: node 0x%016" PRIx64
93		"\n\t\t\t\tport 0x%016" PRIx64 ", lid %u\n",
94		cl_ntoh64(osm_node_get_node_guid(p_node)),
95		cl_ntoh64(port_guid), cl_ntoh16(lid));
96
97	memset(p_rec_item, 0, sizeof(*p_rec_item));
98
99	p_rec_item->rec.lid = lid;
100
101	p_rec_item->rec.node_info = p_node->node_info;
102	p_rec_item->rec.node_info.port_guid = port_guid;
103	memcpy(&(p_rec_item->rec.node_desc), &(p_node->node_desc),
104	       IB_NODE_DESCRIPTION_SIZE);
105	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
106
107Exit:
108	OSM_LOG_EXIT(sa->p_log);
109	return (status);
110}
111
112/**********************************************************************
113 **********************************************************************/
114static void
115__osm_nr_rcv_create_nr(IN osm_sa_t * sa,
116		       IN osm_node_t * const p_node,
117		       IN cl_qlist_t * const p_list,
118		       IN ib_net64_t const match_port_guid,
119		       IN ib_net16_t const match_lid,
120		       IN const osm_physp_t * const p_req_physp)
121{
122	const osm_physp_t *p_physp;
123	uint8_t port_num;
124	uint8_t num_ports;
125	uint16_t match_lid_ho;
126	ib_net16_t base_lid;
127	ib_net16_t base_lid_ho;
128	ib_net16_t max_lid_ho;
129	uint8_t lmc;
130	ib_net64_t port_guid;
131
132	OSM_LOG_ENTER(sa->p_log);
133
134	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
135		"Looking for NodeRecord with LID: %u GUID:0x%016"
136		PRIx64 "\n", cl_ntoh16(match_lid),
137		cl_ntoh64(match_port_guid));
138
139	/*
140	   For switches, do not return the NodeInfo record
141	   for each port on the switch, just for port 0.
142	 */
143	if (osm_node_get_type(p_node) == IB_NODE_TYPE_SWITCH)
144		num_ports = 1;
145	else
146		num_ports = osm_node_get_num_physp(p_node);
147
148	for (port_num = 0; port_num < num_ports; port_num++) {
149		p_physp = osm_node_get_physp_ptr(p_node, port_num);
150		if (!p_physp)
151			continue;
152
153		/* Check to see if the found p_physp and the requester physp
154		   share a pkey. If not - continue */
155		if (!osm_physp_share_pkey(sa->p_log, p_physp, p_req_physp))
156			continue;
157
158		port_guid = osm_physp_get_port_guid(p_physp);
159
160		if (match_port_guid && (port_guid != match_port_guid))
161			continue;
162
163		base_lid = osm_physp_get_base_lid(p_physp);
164		base_lid_ho = cl_ntoh16(base_lid);
165		lmc = osm_physp_get_lmc(p_physp);
166		max_lid_ho = (uint16_t) (base_lid_ho + (1 << lmc) - 1);
167		match_lid_ho = cl_ntoh16(match_lid);
168
169		if (match_lid_ho) {
170			/*
171			   We validate that the lid belongs to this node.
172			 */
173			OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
174				"Comparing LID: %u <= %u <= %u\n",
175				base_lid_ho, match_lid_ho, max_lid_ho);
176
177			if (match_lid_ho < base_lid_ho
178			    || match_lid_ho > max_lid_ho)
179				continue;
180		}
181
182		__osm_nr_rcv_new_nr(sa, p_node, p_list, port_guid, base_lid);
183
184	}
185
186	OSM_LOG_EXIT(sa->p_log);
187}
188
189/**********************************************************************
190 **********************************************************************/
191static void
192__osm_nr_rcv_by_comp_mask(IN cl_map_item_t * const p_map_item, IN void *context)
193{
194	const osm_nr_search_ctxt_t *const p_ctxt =
195	    (osm_nr_search_ctxt_t *) context;
196	osm_node_t *const p_node = (osm_node_t *) p_map_item;
197	const ib_node_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
198	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
199	osm_sa_t *sa = p_ctxt->sa;
200	ib_net64_t const comp_mask = p_ctxt->comp_mask;
201	ib_net64_t match_port_guid = 0;
202	ib_net16_t match_lid = 0;
203
204	OSM_LOG_ENTER(p_ctxt->sa->p_log);
205
206	osm_dump_node_info(p_ctxt->sa->p_log,
207			   &p_node->node_info, OSM_LOG_VERBOSE);
208
209	if (comp_mask & IB_NR_COMPMASK_LID)
210		match_lid = p_rcvd_rec->lid;
211
212	if (comp_mask & IB_NR_COMPMASK_NODEGUID) {
213		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
214			"Looking for node 0x%016" PRIx64
215			", found 0x%016" PRIx64 "\n",
216			cl_ntoh64(p_rcvd_rec->node_info.node_guid),
217			cl_ntoh64(osm_node_get_node_guid(p_node)));
218
219		if ((p_node->node_info.node_guid !=
220		     p_rcvd_rec->node_info.node_guid))
221			goto Exit;
222	}
223	if (comp_mask & IB_NR_COMPMASK_PORTGUID) {
224		match_port_guid = p_rcvd_rec->node_info.port_guid;
225	}
226	if (comp_mask & IB_NR_COMPMASK_SYSIMAGEGUID) {
227		if ((p_node->node_info.sys_guid !=
228		     p_rcvd_rec->node_info.sys_guid))
229			goto Exit;
230	}
231	if (comp_mask & IB_NR_COMPMASK_BASEVERSION) {
232		if ((p_node->node_info.base_version !=
233		     p_rcvd_rec->node_info.base_version))
234			goto Exit;
235	}
236	if (comp_mask & IB_NR_COMPMASK_CLASSVERSION) {
237		if ((p_node->node_info.class_version !=
238		     p_rcvd_rec->node_info.class_version))
239			goto Exit;
240	}
241	if (comp_mask & IB_NR_COMPMASK_NODETYPE) {
242		if ((p_node->node_info.node_type !=
243		     p_rcvd_rec->node_info.node_type))
244			goto Exit;
245	}
246	if (comp_mask & IB_NR_COMPMASK_NUMPORTS) {
247		if ((p_node->node_info.num_ports !=
248		     p_rcvd_rec->node_info.num_ports))
249			goto Exit;
250	}
251	if (comp_mask & IB_NR_COMPMASK_PARTCAP) {
252		if ((p_node->node_info.partition_cap !=
253		     p_rcvd_rec->node_info.partition_cap))
254			goto Exit;
255	}
256	if (comp_mask & IB_NR_COMPMASK_DEVID) {
257		if ((p_node->node_info.device_id !=
258		     p_rcvd_rec->node_info.device_id))
259			goto Exit;
260	}
261	if (comp_mask & IB_NR_COMPMASK_REV) {
262		if ((p_node->node_info.revision !=
263		     p_rcvd_rec->node_info.revision))
264			goto Exit;
265	}
266	if (comp_mask & IB_NR_COMPMASK_PORTNUM) {
267		if (ib_node_info_get_local_port_num(&p_node->node_info) !=
268		    ib_node_info_get_local_port_num(&p_rcvd_rec->node_info))
269			goto Exit;
270	}
271	if (comp_mask & IB_NR_COMPMASK_VENDID) {
272		if (ib_node_info_get_vendor_id(&p_node->node_info) !=
273		    ib_node_info_get_vendor_id(&p_rcvd_rec->node_info))
274			goto Exit;
275	}
276	if (comp_mask & IB_NR_COMPMASK_NODEDESC) {
277		if (strncmp((char *)&p_node->node_desc,
278			    (char *)&p_rcvd_rec->node_desc,
279			    sizeof(ib_node_desc_t)))
280			goto Exit;
281	}
282
283	__osm_nr_rcv_create_nr(sa, p_node, p_ctxt->p_list,
284			       match_port_guid, match_lid, p_req_physp);
285
286Exit:
287	OSM_LOG_EXIT(p_ctxt->sa->p_log);
288}
289
290/**********************************************************************
291 **********************************************************************/
292void osm_nr_rcv_process(IN void *ctx, IN void *data)
293{
294	osm_sa_t *sa = ctx;
295	osm_madw_t *p_madw = data;
296	const ib_sa_mad_t *p_rcvd_mad;
297	const ib_node_record_t *p_rcvd_rec;
298	cl_qlist_t rec_list;
299	osm_nr_search_ctxt_t context;
300	osm_physp_t *p_req_physp;
301
302	CL_ASSERT(sa);
303
304	OSM_LOG_ENTER(sa->p_log);
305
306	CL_ASSERT(p_madw);
307
308	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
309	p_rcvd_rec = (ib_node_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
310
311	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_NODE_RECORD);
312
313	/* we only support SubnAdmGet and SubnAdmGetTable methods */
314	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
315	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
316		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1D05: "
317			"Unsupported Method (%s)\n",
318			ib_get_sa_method_str(p_rcvd_mad->method));
319		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
320		goto Exit;
321	}
322
323	/* update the requester physical port. */
324	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
325						osm_madw_get_mad_addr_ptr
326						(p_madw));
327	if (p_req_physp == NULL) {
328		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1D04: "
329			"Cannot find requester physical port\n");
330		goto Exit;
331	}
332
333	if (osm_log_is_active(sa->p_log, OSM_LOG_DEBUG))
334		osm_dump_node_record(sa->p_log, p_rcvd_rec, OSM_LOG_DEBUG);
335
336	cl_qlist_init(&rec_list);
337
338	context.p_rcvd_rec = p_rcvd_rec;
339	context.p_list = &rec_list;
340	context.comp_mask = p_rcvd_mad->comp_mask;
341	context.sa = sa;
342	context.p_req_physp = p_req_physp;
343
344	cl_plock_acquire(sa->p_lock);
345
346	cl_qmap_apply_func(&sa->p_subn->node_guid_tbl,
347			   __osm_nr_rcv_by_comp_mask, &context);
348
349	cl_plock_release(sa->p_lock);
350
351	osm_sa_respond(sa, p_madw, sizeof(ib_node_record_t), &rec_list);
352
353Exit:
354	OSM_LOG_EXIT(sa->p_log);
355}
356