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#if HAVE_CONFIG_H
37#  include <config.h>
38#endif				/* HAVE_CONFIG_H */
39
40#include <string.h>
41#include <iba/ib_types.h>
42#include <complib/cl_qmap.h>
43#include <complib/cl_passivelock.h>
44#include <complib/cl_debug.h>
45#include <complib/cl_qlist.h>
46#include <vendor/osm_vendor_api.h>
47#include <opensm/osm_port.h>
48#include <opensm/osm_node.h>
49#include <opensm/osm_helper.h>
50#include <opensm/osm_pkey.h>
51#include <opensm/osm_sa.h>
52
53typedef struct osm_pkey_item {
54	cl_list_item_t list_item;
55	ib_pkey_table_record_t rec;
56} osm_pkey_item_t;
57
58typedef struct osm_pkey_search_ctxt {
59	const ib_pkey_table_record_t *p_rcvd_rec;
60	ib_net64_t comp_mask;
61	uint16_t block_num;
62	cl_qlist_t *p_list;
63	osm_sa_t *sa;
64	const osm_physp_t *p_req_physp;
65} osm_pkey_search_ctxt_t;
66
67/**********************************************************************
68 **********************************************************************/
69static void
70__osm_sa_pkey_create(IN osm_sa_t * sa,
71		     IN osm_physp_t * const p_physp,
72		     IN osm_pkey_search_ctxt_t * const p_ctxt,
73		     IN uint16_t block)
74{
75	osm_pkey_item_t *p_rec_item;
76	uint16_t lid;
77	ib_api_status_t status = IB_SUCCESS;
78
79	OSM_LOG_ENTER(sa->p_log);
80
81	p_rec_item = malloc(sizeof(*p_rec_item));
82	if (p_rec_item == NULL) {
83		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4602: "
84			"rec_item alloc failed\n");
85		status = IB_INSUFFICIENT_RESOURCES;
86		goto Exit;
87	}
88
89	if (p_physp->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH)
90		lid = p_physp->port_info.base_lid;
91	else
92		lid = osm_node_get_base_lid(p_physp->p_node, 0);
93
94	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
95		"New P_Key table for: port 0x%016" PRIx64
96		", lid %u, port %u Block:%u\n",
97		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
98		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), block);
99
100	memset(p_rec_item, 0, sizeof(*p_rec_item));
101
102	p_rec_item->rec.lid = lid;
103	p_rec_item->rec.block_num = block;
104	p_rec_item->rec.port_num = osm_physp_get_port_num(p_physp);
105	p_rec_item->rec.pkey_tbl =
106	    *(osm_pkey_tbl_block_get(osm_physp_get_pkey_tbl(p_physp), block));
107
108	cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
109
110Exit:
111	OSM_LOG_EXIT(sa->p_log);
112}
113
114/**********************************************************************
115 **********************************************************************/
116static void
117__osm_sa_pkey_check_physp(IN osm_sa_t * sa,
118			  IN osm_physp_t * const p_physp,
119			  osm_pkey_search_ctxt_t * const p_ctxt)
120{
121	ib_net64_t comp_mask = p_ctxt->comp_mask;
122	uint16_t block, num_blocks;
123
124	OSM_LOG_ENTER(sa->p_log);
125
126	/* we got here with the phys port - all is left is to get the right block */
127	if (comp_mask & IB_PKEY_COMPMASK_BLOCK) {
128		__osm_sa_pkey_create(sa, p_physp, p_ctxt, p_ctxt->block_num);
129	} else {
130		num_blocks =
131		    osm_pkey_tbl_get_num_blocks(osm_physp_get_pkey_tbl
132						(p_physp));
133		for (block = 0; block < num_blocks; block++) {
134			__osm_sa_pkey_create(sa, p_physp, p_ctxt, block);
135		}
136	}
137
138	OSM_LOG_EXIT(sa->p_log);
139}
140
141/**********************************************************************
142 **********************************************************************/
143static void
144__osm_sa_pkey_by_comp_mask(IN osm_sa_t * sa,
145			   IN const osm_port_t * const p_port,
146			   osm_pkey_search_ctxt_t * const p_ctxt)
147{
148	const ib_pkey_table_record_t *p_rcvd_rec;
149	ib_net64_t comp_mask;
150	osm_physp_t *p_physp;
151	uint8_t port_num;
152	uint8_t num_ports;
153	const osm_physp_t *p_req_physp;
154
155	OSM_LOG_ENTER(sa->p_log);
156
157	p_rcvd_rec = p_ctxt->p_rcvd_rec;
158	comp_mask = p_ctxt->comp_mask;
159	port_num = p_rcvd_rec->port_num;
160	p_req_physp = p_ctxt->p_req_physp;
161
162	/* if this is a switch port we can search all ports
163	   otherwise we must be looking on port 0 */
164	if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
165		/* we put it in the comp mask and port num */
166		port_num = p_port->p_physp->port_num;
167		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
168			"Using Physical Default Port Number: 0x%X (for End Node)\n",
169			port_num);
170		comp_mask |= IB_PKEY_COMPMASK_PORT;
171	}
172
173	if (comp_mask & IB_PKEY_COMPMASK_PORT) {
174		if (port_num < osm_node_get_num_physp(p_port->p_node)) {
175			p_physp =
176			    osm_node_get_physp_ptr(p_port->p_node, port_num);
177			/* Check that the p_physp is valid, and that is shares a pkey
178			   with the p_req_physp. */
179			if (p_physp &&
180			    (osm_physp_share_pkey
181			     (sa->p_log, p_req_physp, p_physp)))
182				__osm_sa_pkey_check_physp(sa, p_physp,
183							  p_ctxt);
184		} else {
185			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4603: "
186				"Given Physical Port Number: 0x%X is out of range should be < 0x%X\n",
187				port_num,
188				osm_node_get_num_physp(p_port->p_node));
189			goto Exit;
190		}
191	} else {
192		num_ports = osm_node_get_num_physp(p_port->p_node);
193		for (port_num = 0; port_num < num_ports; port_num++) {
194			p_physp =
195			    osm_node_get_physp_ptr(p_port->p_node, port_num);
196			if (!p_physp)
197				continue;
198
199			/* if the requester and the p_physp don't share a pkey -
200			   continue */
201			if (!osm_physp_share_pkey
202			    (sa->p_log, p_req_physp, p_physp))
203				continue;
204
205			__osm_sa_pkey_check_physp(sa, p_physp, p_ctxt);
206		}
207	}
208Exit:
209	OSM_LOG_EXIT(sa->p_log);
210}
211
212/**********************************************************************
213 **********************************************************************/
214static void
215__osm_sa_pkey_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
216			      IN void *context)
217{
218	const osm_port_t *const p_port = (osm_port_t *) p_map_item;
219	osm_pkey_search_ctxt_t *const p_ctxt =
220	    (osm_pkey_search_ctxt_t *) context;
221
222	__osm_sa_pkey_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
223}
224
225/**********************************************************************
226 **********************************************************************/
227void osm_pkey_rec_rcv_process(IN void *ctx, IN void *data)
228{
229	osm_sa_t *sa = ctx;
230	osm_madw_t *p_madw = data;
231	const ib_sa_mad_t *p_rcvd_mad;
232	const ib_pkey_table_record_t *p_rcvd_rec;
233	const osm_port_t *p_port = NULL;
234	const ib_pkey_table_t *p_pkey;
235	cl_qlist_t rec_list;
236	osm_pkey_search_ctxt_t context;
237	ib_api_status_t status = IB_SUCCESS;
238	ib_net64_t comp_mask;
239	osm_physp_t *p_req_physp;
240
241	CL_ASSERT(sa);
242
243	OSM_LOG_ENTER(sa->p_log);
244
245	CL_ASSERT(p_madw);
246
247	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
248	p_rcvd_rec =
249	    (ib_pkey_table_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
250	comp_mask = p_rcvd_mad->comp_mask;
251
252	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_PKEY_TBL_RECORD);
253
254	/* we only support SubnAdmGet and SubnAdmGetTable methods */
255	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
256	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
257		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4605: "
258			"Unsupported Method (%s)\n",
259			ib_get_sa_method_str(p_rcvd_mad->method));
260		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
261		goto Exit;
262	}
263
264	/*
265	   p922 - P_KeyTableRecords shall only be provided in response
266	   to trusted requests.
267	   Check that the requester is a trusted one.
268	 */
269	if (p_rcvd_mad->sm_key != sa->p_subn->opt.sa_key) {
270		/* This is not a trusted requester! */
271		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4608: "
272			"Request from non-trusted requester: "
273			"Given SM_Key:0x%016" PRIx64 "\n",
274			cl_ntoh64(p_rcvd_mad->sm_key));
275		osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_REQ_INVALID);
276		goto Exit;
277	}
278
279	/* update the requester physical port. */
280	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
281						osm_madw_get_mad_addr_ptr
282						(p_madw));
283	if (p_req_physp == NULL) {
284		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4604: "
285			"Cannot find requester physical port\n");
286		goto Exit;
287	}
288
289	p_pkey = (ib_pkey_table_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
290
291	cl_qlist_init(&rec_list);
292
293	context.p_rcvd_rec = p_rcvd_rec;
294	context.p_list = &rec_list;
295	context.comp_mask = p_rcvd_mad->comp_mask;
296	context.sa = sa;
297	context.block_num = p_rcvd_rec->block_num;
298	context.p_req_physp = p_req_physp;
299
300	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
301		"Got Query Lid:%u(%02X), Block:0x%02X(%02X), Port:0x%02X(%02X)\n",
302		cl_ntoh16(p_rcvd_rec->lid),
303		(comp_mask & IB_PKEY_COMPMASK_LID) != 0, p_rcvd_rec->port_num,
304		(comp_mask & IB_PKEY_COMPMASK_PORT) != 0, p_rcvd_rec->block_num,
305		(comp_mask & IB_PKEY_COMPMASK_BLOCK) != 0);
306
307	cl_plock_acquire(sa->p_lock);
308
309	/*
310	   If the user specified a LID, it obviously narrows our
311	   work load, since we don't have to search every port
312	 */
313	if (comp_mask & IB_PKEY_COMPMASK_LID) {
314		status = osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
315						  &p_port);
316		if (status != IB_SUCCESS || p_port == NULL) {
317			status = IB_NOT_FOUND;
318			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 460B: "
319				"No port found with LID %u\n",
320				cl_ntoh16(p_rcvd_rec->lid));
321		}
322	}
323
324	if (status == IB_SUCCESS) {
325		/* if we got a unique port - no need for a port search */
326		if (p_port)
327			/* this does the loop on all the port phys ports */
328			__osm_sa_pkey_by_comp_mask(sa, p_port, &context);
329		else
330			cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
331					   __osm_sa_pkey_by_comp_mask_cb,
332					   &context);
333	}
334
335	cl_plock_release(sa->p_lock);
336
337	osm_sa_respond(sa, p_madw, sizeof(ib_pkey_table_record_t), &rec_list);
338
339Exit:
340	OSM_LOG_EXIT(sa->p_log);
341}
342