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#if HAVE_CONFIG_H
37219820Sjeff#  include <config.h>
38219820Sjeff#endif				/* HAVE_CONFIG_H */
39219820Sjeff
40219820Sjeff#include <string.h>
41219820Sjeff#include <iba/ib_types.h>
42219820Sjeff#include <complib/cl_qmap.h>
43219820Sjeff#include <complib/cl_passivelock.h>
44219820Sjeff#include <complib/cl_debug.h>
45219820Sjeff#include <complib/cl_qlist.h>
46219820Sjeff#include <vendor/osm_vendor_api.h>
47219820Sjeff#include <opensm/osm_port.h>
48219820Sjeff#include <opensm/osm_node.h>
49219820Sjeff#include <opensm/osm_helper.h>
50219820Sjeff#include <opensm/osm_pkey.h>
51219820Sjeff#include <opensm/osm_sa.h>
52219820Sjeff
53219820Sjefftypedef struct osm_pkey_item {
54219820Sjeff	cl_list_item_t list_item;
55219820Sjeff	ib_pkey_table_record_t rec;
56219820Sjeff} osm_pkey_item_t;
57219820Sjeff
58219820Sjefftypedef struct osm_pkey_search_ctxt {
59219820Sjeff	const ib_pkey_table_record_t *p_rcvd_rec;
60219820Sjeff	ib_net64_t comp_mask;
61219820Sjeff	uint16_t block_num;
62219820Sjeff	cl_qlist_t *p_list;
63219820Sjeff	osm_sa_t *sa;
64219820Sjeff	const osm_physp_t *p_req_physp;
65219820Sjeff} osm_pkey_search_ctxt_t;
66219820Sjeff
67219820Sjeff/**********************************************************************
68219820Sjeff **********************************************************************/
69219820Sjeffstatic void
70219820Sjeff__osm_sa_pkey_create(IN osm_sa_t * sa,
71219820Sjeff		     IN osm_physp_t * const p_physp,
72219820Sjeff		     IN osm_pkey_search_ctxt_t * const p_ctxt,
73219820Sjeff		     IN uint16_t block)
74219820Sjeff{
75219820Sjeff	osm_pkey_item_t *p_rec_item;
76219820Sjeff	uint16_t lid;
77219820Sjeff	ib_api_status_t status = IB_SUCCESS;
78219820Sjeff
79219820Sjeff	OSM_LOG_ENTER(sa->p_log);
80219820Sjeff
81219820Sjeff	p_rec_item = malloc(sizeof(*p_rec_item));
82219820Sjeff	if (p_rec_item == NULL) {
83219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4602: "
84219820Sjeff			"rec_item alloc failed\n");
85219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
86219820Sjeff		goto Exit;
87219820Sjeff	}
88219820Sjeff
89219820Sjeff	if (p_physp->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH)
90219820Sjeff		lid = p_physp->port_info.base_lid;
91219820Sjeff	else
92219820Sjeff		lid = osm_node_get_base_lid(p_physp->p_node, 0);
93219820Sjeff
94219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
95219820Sjeff		"New P_Key table for: port 0x%016" PRIx64
96219820Sjeff		", lid %u, port %u Block:%u\n",
97219820Sjeff		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
98219820Sjeff		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), block);
99219820Sjeff
100219820Sjeff	memset(p_rec_item, 0, sizeof(*p_rec_item));
101219820Sjeff
102219820Sjeff	p_rec_item->rec.lid = lid;
103219820Sjeff	p_rec_item->rec.block_num = block;
104219820Sjeff	p_rec_item->rec.port_num = osm_physp_get_port_num(p_physp);
105219820Sjeff	p_rec_item->rec.pkey_tbl =
106219820Sjeff	    *(osm_pkey_tbl_block_get(osm_physp_get_pkey_tbl(p_physp), block));
107219820Sjeff
108219820Sjeff	cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
109219820Sjeff
110219820SjeffExit:
111219820Sjeff	OSM_LOG_EXIT(sa->p_log);
112219820Sjeff}
113219820Sjeff
114219820Sjeff/**********************************************************************
115219820Sjeff **********************************************************************/
116219820Sjeffstatic void
117219820Sjeff__osm_sa_pkey_check_physp(IN osm_sa_t * sa,
118219820Sjeff			  IN osm_physp_t * const p_physp,
119219820Sjeff			  osm_pkey_search_ctxt_t * const p_ctxt)
120219820Sjeff{
121219820Sjeff	ib_net64_t comp_mask = p_ctxt->comp_mask;
122219820Sjeff	uint16_t block, num_blocks;
123219820Sjeff
124219820Sjeff	OSM_LOG_ENTER(sa->p_log);
125219820Sjeff
126219820Sjeff	/* we got here with the phys port - all is left is to get the right block */
127219820Sjeff	if (comp_mask & IB_PKEY_COMPMASK_BLOCK) {
128219820Sjeff		__osm_sa_pkey_create(sa, p_physp, p_ctxt, p_ctxt->block_num);
129219820Sjeff	} else {
130219820Sjeff		num_blocks =
131219820Sjeff		    osm_pkey_tbl_get_num_blocks(osm_physp_get_pkey_tbl
132219820Sjeff						(p_physp));
133219820Sjeff		for (block = 0; block < num_blocks; block++) {
134219820Sjeff			__osm_sa_pkey_create(sa, p_physp, p_ctxt, block);
135219820Sjeff		}
136219820Sjeff	}
137219820Sjeff
138219820Sjeff	OSM_LOG_EXIT(sa->p_log);
139219820Sjeff}
140219820Sjeff
141219820Sjeff/**********************************************************************
142219820Sjeff **********************************************************************/
143219820Sjeffstatic void
144219820Sjeff__osm_sa_pkey_by_comp_mask(IN osm_sa_t * sa,
145219820Sjeff			   IN const osm_port_t * const p_port,
146219820Sjeff			   osm_pkey_search_ctxt_t * const p_ctxt)
147219820Sjeff{
148219820Sjeff	const ib_pkey_table_record_t *p_rcvd_rec;
149219820Sjeff	ib_net64_t comp_mask;
150219820Sjeff	osm_physp_t *p_physp;
151219820Sjeff	uint8_t port_num;
152219820Sjeff	uint8_t num_ports;
153219820Sjeff	const osm_physp_t *p_req_physp;
154219820Sjeff
155219820Sjeff	OSM_LOG_ENTER(sa->p_log);
156219820Sjeff
157219820Sjeff	p_rcvd_rec = p_ctxt->p_rcvd_rec;
158219820Sjeff	comp_mask = p_ctxt->comp_mask;
159219820Sjeff	port_num = p_rcvd_rec->port_num;
160219820Sjeff	p_req_physp = p_ctxt->p_req_physp;
161219820Sjeff
162219820Sjeff	/* if this is a switch port we can search all ports
163219820Sjeff	   otherwise we must be looking on port 0 */
164219820Sjeff	if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
165219820Sjeff		/* we put it in the comp mask and port num */
166219820Sjeff		port_num = p_port->p_physp->port_num;
167219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
168219820Sjeff			"Using Physical Default Port Number: 0x%X (for End Node)\n",
169219820Sjeff			port_num);
170219820Sjeff		comp_mask |= IB_PKEY_COMPMASK_PORT;
171219820Sjeff	}
172219820Sjeff
173219820Sjeff	if (comp_mask & IB_PKEY_COMPMASK_PORT) {
174219820Sjeff		if (port_num < osm_node_get_num_physp(p_port->p_node)) {
175219820Sjeff			p_physp =
176219820Sjeff			    osm_node_get_physp_ptr(p_port->p_node, port_num);
177219820Sjeff			/* Check that the p_physp is valid, and that is shares a pkey
178219820Sjeff			   with the p_req_physp. */
179219820Sjeff			if (p_physp &&
180219820Sjeff			    (osm_physp_share_pkey
181219820Sjeff			     (sa->p_log, p_req_physp, p_physp)))
182219820Sjeff				__osm_sa_pkey_check_physp(sa, p_physp,
183219820Sjeff							  p_ctxt);
184219820Sjeff		} else {
185219820Sjeff			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4603: "
186219820Sjeff				"Given Physical Port Number: 0x%X is out of range should be < 0x%X\n",
187219820Sjeff				port_num,
188219820Sjeff				osm_node_get_num_physp(p_port->p_node));
189219820Sjeff			goto Exit;
190219820Sjeff		}
191219820Sjeff	} else {
192219820Sjeff		num_ports = osm_node_get_num_physp(p_port->p_node);
193219820Sjeff		for (port_num = 0; port_num < num_ports; port_num++) {
194219820Sjeff			p_physp =
195219820Sjeff			    osm_node_get_physp_ptr(p_port->p_node, port_num);
196219820Sjeff			if (!p_physp)
197219820Sjeff				continue;
198219820Sjeff
199219820Sjeff			/* if the requester and the p_physp don't share a pkey -
200219820Sjeff			   continue */
201219820Sjeff			if (!osm_physp_share_pkey
202219820Sjeff			    (sa->p_log, p_req_physp, p_physp))
203219820Sjeff				continue;
204219820Sjeff
205219820Sjeff			__osm_sa_pkey_check_physp(sa, p_physp, p_ctxt);
206219820Sjeff		}
207219820Sjeff	}
208219820SjeffExit:
209219820Sjeff	OSM_LOG_EXIT(sa->p_log);
210219820Sjeff}
211219820Sjeff
212219820Sjeff/**********************************************************************
213219820Sjeff **********************************************************************/
214219820Sjeffstatic void
215219820Sjeff__osm_sa_pkey_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
216219820Sjeff			      IN void *context)
217219820Sjeff{
218219820Sjeff	const osm_port_t *const p_port = (osm_port_t *) p_map_item;
219219820Sjeff	osm_pkey_search_ctxt_t *const p_ctxt =
220219820Sjeff	    (osm_pkey_search_ctxt_t *) context;
221219820Sjeff
222219820Sjeff	__osm_sa_pkey_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
223219820Sjeff}
224219820Sjeff
225219820Sjeff/**********************************************************************
226219820Sjeff **********************************************************************/
227219820Sjeffvoid osm_pkey_rec_rcv_process(IN void *ctx, IN void *data)
228219820Sjeff{
229219820Sjeff	osm_sa_t *sa = ctx;
230219820Sjeff	osm_madw_t *p_madw = data;
231219820Sjeff	const ib_sa_mad_t *p_rcvd_mad;
232219820Sjeff	const ib_pkey_table_record_t *p_rcvd_rec;
233219820Sjeff	const osm_port_t *p_port = NULL;
234219820Sjeff	const ib_pkey_table_t *p_pkey;
235219820Sjeff	cl_qlist_t rec_list;
236219820Sjeff	osm_pkey_search_ctxt_t context;
237219820Sjeff	ib_api_status_t status = IB_SUCCESS;
238219820Sjeff	ib_net64_t comp_mask;
239219820Sjeff	osm_physp_t *p_req_physp;
240219820Sjeff
241219820Sjeff	CL_ASSERT(sa);
242219820Sjeff
243219820Sjeff	OSM_LOG_ENTER(sa->p_log);
244219820Sjeff
245219820Sjeff	CL_ASSERT(p_madw);
246219820Sjeff
247219820Sjeff	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
248219820Sjeff	p_rcvd_rec =
249219820Sjeff	    (ib_pkey_table_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
250219820Sjeff	comp_mask = p_rcvd_mad->comp_mask;
251219820Sjeff
252219820Sjeff	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_PKEY_TBL_RECORD);
253219820Sjeff
254219820Sjeff	/* we only support SubnAdmGet and SubnAdmGetTable methods */
255219820Sjeff	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
256219820Sjeff	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
257219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4605: "
258219820Sjeff			"Unsupported Method (%s)\n",
259219820Sjeff			ib_get_sa_method_str(p_rcvd_mad->method));
260219820Sjeff		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
261219820Sjeff		goto Exit;
262219820Sjeff	}
263219820Sjeff
264219820Sjeff	/*
265219820Sjeff	   p922 - P_KeyTableRecords shall only be provided in response
266219820Sjeff	   to trusted requests.
267219820Sjeff	   Check that the requester is a trusted one.
268219820Sjeff	 */
269219820Sjeff	if (p_rcvd_mad->sm_key != sa->p_subn->opt.sa_key) {
270219820Sjeff		/* This is not a trusted requester! */
271219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4608: "
272219820Sjeff			"Request from non-trusted requester: "
273219820Sjeff			"Given SM_Key:0x%016" PRIx64 "\n",
274219820Sjeff			cl_ntoh64(p_rcvd_mad->sm_key));
275219820Sjeff		osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_REQ_INVALID);
276219820Sjeff		goto Exit;
277219820Sjeff	}
278219820Sjeff
279219820Sjeff	/* update the requester physical port. */
280219820Sjeff	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
281219820Sjeff						osm_madw_get_mad_addr_ptr
282219820Sjeff						(p_madw));
283219820Sjeff	if (p_req_physp == NULL) {
284219820Sjeff		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4604: "
285219820Sjeff			"Cannot find requester physical port\n");
286219820Sjeff		goto Exit;
287219820Sjeff	}
288219820Sjeff
289219820Sjeff	p_pkey = (ib_pkey_table_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
290219820Sjeff
291219820Sjeff	cl_qlist_init(&rec_list);
292219820Sjeff
293219820Sjeff	context.p_rcvd_rec = p_rcvd_rec;
294219820Sjeff	context.p_list = &rec_list;
295219820Sjeff	context.comp_mask = p_rcvd_mad->comp_mask;
296219820Sjeff	context.sa = sa;
297219820Sjeff	context.block_num = p_rcvd_rec->block_num;
298219820Sjeff	context.p_req_physp = p_req_physp;
299219820Sjeff
300219820Sjeff	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
301219820Sjeff		"Got Query Lid:%u(%02X), Block:0x%02X(%02X), Port:0x%02X(%02X)\n",
302219820Sjeff		cl_ntoh16(p_rcvd_rec->lid),
303219820Sjeff		(comp_mask & IB_PKEY_COMPMASK_LID) != 0, p_rcvd_rec->port_num,
304219820Sjeff		(comp_mask & IB_PKEY_COMPMASK_PORT) != 0, p_rcvd_rec->block_num,
305219820Sjeff		(comp_mask & IB_PKEY_COMPMASK_BLOCK) != 0);
306219820Sjeff
307219820Sjeff	cl_plock_acquire(sa->p_lock);
308219820Sjeff
309219820Sjeff	/*
310219820Sjeff	   If the user specified a LID, it obviously narrows our
311219820Sjeff	   work load, since we don't have to search every port
312219820Sjeff	 */
313219820Sjeff	if (comp_mask & IB_PKEY_COMPMASK_LID) {
314219820Sjeff		status = osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
315219820Sjeff						  &p_port);
316219820Sjeff		if (status != IB_SUCCESS || p_port == NULL) {
317219820Sjeff			status = IB_NOT_FOUND;
318219820Sjeff			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 460B: "
319219820Sjeff				"No port found with LID %u\n",
320219820Sjeff				cl_ntoh16(p_rcvd_rec->lid));
321219820Sjeff		}
322219820Sjeff	}
323219820Sjeff
324219820Sjeff	if (status == IB_SUCCESS) {
325219820Sjeff		/* if we got a unique port - no need for a port search */
326219820Sjeff		if (p_port)
327219820Sjeff			/* this does the loop on all the port phys ports */
328219820Sjeff			__osm_sa_pkey_by_comp_mask(sa, p_port, &context);
329219820Sjeff		else
330219820Sjeff			cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
331219820Sjeff					   __osm_sa_pkey_by_comp_mask_cb,
332219820Sjeff					   &context);
333219820Sjeff	}
334219820Sjeff
335219820Sjeff	cl_plock_release(sa->p_lock);
336219820Sjeff
337219820Sjeff	osm_sa_respond(sa, p_madw, sizeof(ib_pkey_table_record_t), &rec_list);
338219820Sjeff
339219820SjeffExit:
340219820Sjeff	OSM_LOG_EXIT(sa->p_log);
341219820Sjeff}
342