1 /*
2  * Copyright (c) 2004-2009 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  * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  *
35  */
36 
37 /*
38  * Abstract:
39  *    Implementation of osm_vlarb_rec_rcv_t.
40  * This object represents the VLArbitrationRecord Receiver object.
41  * This object is part of the opensm family of objects.
42  */
43 
44 #if HAVE_CONFIG_H
45 #  include <config.h>
46 #endif				/* HAVE_CONFIG_H */
47 
48 #include <string.h>
49 #include <iba/ib_types.h>
50 #include <complib/cl_qmap.h>
51 #include <complib/cl_passivelock.h>
52 #include <complib/cl_debug.h>
53 #include <complib/cl_qlist.h>
54 #include <opensm/osm_file_ids.h>
55 #define FILE_ID OSM_FILE_SA_VLARB_RECORD_C
56 #include <vendor/osm_vendor_api.h>
57 #include <opensm/osm_port.h>
58 #include <opensm/osm_node.h>
59 #include <opensm/osm_helper.h>
60 #include <opensm/osm_pkey.h>
61 #include <opensm/osm_sa.h>
62 
63 #define SA_VLA_RESP_SIZE SA_ITEM_RESP_SIZE(vlarb_rec)
64 
65 typedef struct osm_vl_arb_search_ctxt {
66 	const ib_vl_arb_table_record_t *p_rcvd_rec;
67 	ib_net64_t comp_mask;
68 	uint8_t block_num;
69 	cl_qlist_t *p_list;
70 	osm_sa_t *sa;
71 	const osm_physp_t *p_req_physp;
72 } osm_vl_arb_search_ctxt_t;
73 
74 static void sa_vl_arb_create(IN osm_sa_t * sa, IN osm_physp_t * p_physp,
75 			     IN osm_vl_arb_search_ctxt_t * p_ctxt,
76 			     IN uint8_t block)
77 {
78 	osm_sa_item_t *p_rec_item;
79 	uint16_t lid;
80 
81 	OSM_LOG_ENTER(sa->p_log);
82 
83 	p_rec_item = malloc(SA_VLA_RESP_SIZE);
84 	if (p_rec_item == NULL) {
85 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2A02: "
86 			"rec_item alloc failed\n");
87 		goto Exit;
88 	}
89 
90 	if (p_physp->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH)
91 		lid = p_physp->port_info.base_lid;
92 	else
93 		lid = osm_node_get_base_lid(p_physp->p_node, 0);
94 
95 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
96 		"New VLArbitration for: port 0x%016" PRIx64
97 		", lid %u, port %u Block:%u\n",
98 		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
99 		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), block);
100 
101 	memset(p_rec_item, 0, SA_VLA_RESP_SIZE);
102 
103 	p_rec_item->resp.vlarb_rec.lid = lid;
104 	p_rec_item->resp.vlarb_rec.port_num = osm_physp_get_port_num(p_physp);
105 	p_rec_item->resp.vlarb_rec.block_num = block;
106 	p_rec_item->resp.vlarb_rec.vl_arb_tbl = *(osm_physp_get_vla_tbl(p_physp, block));
107 
108 	cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
109 
110 Exit:
111 	OSM_LOG_EXIT(sa->p_log);
112 }
113 
114 static void sa_vl_arb_check_physp(IN osm_sa_t * sa, IN osm_physp_t * p_physp,
115 				  osm_vl_arb_search_ctxt_t * p_ctxt)
116 {
117 	ib_net64_t comp_mask = p_ctxt->comp_mask;
118 	uint8_t block;
119 
120 	OSM_LOG_ENTER(sa->p_log);
121 
122 	/* we got here with the phys port - all that's left is to get the right block */
123 	for (block = 1; block <= 4; block++) {
124 		if (!(comp_mask & IB_VLA_COMPMASK_BLOCK)
125 		    || block == p_ctxt->block_num)
126 			sa_vl_arb_create(sa, p_physp, p_ctxt, block);
127 	}
128 
129 	OSM_LOG_EXIT(sa->p_log);
130 }
131 
132 static void sa_vl_arb_by_comp_mask(osm_sa_t * sa, IN const osm_port_t * p_port,
133 				   osm_vl_arb_search_ctxt_t * p_ctxt)
134 {
135 	const ib_vl_arb_table_record_t *p_rcvd_rec;
136 	ib_net64_t comp_mask;
137 	osm_physp_t *p_physp;
138 	uint8_t port_num;
139 	uint8_t num_ports;
140 	const osm_physp_t *p_req_physp;
141 
142 	OSM_LOG_ENTER(sa->p_log);
143 
144 	p_rcvd_rec = p_ctxt->p_rcvd_rec;
145 	comp_mask = p_ctxt->comp_mask;
146 	port_num = p_rcvd_rec->port_num;
147 	p_req_physp = p_ctxt->p_req_physp;
148 
149 	/* if this is a switch port we can search all ports
150 	   otherwise we must be looking on port 0 */
151 	if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
152 		/* we put it in the comp mask and port num */
153 		port_num = p_port->p_physp->port_num;
154 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
155 			"Using Physical Default Port Number: 0x%X (for End Node)\n",
156 			port_num);
157 		comp_mask |= IB_VLA_COMPMASK_OUT_PORT;
158 	}
159 
160 	if (comp_mask & IB_VLA_COMPMASK_OUT_PORT) {
161 		if (port_num < osm_node_get_num_physp(p_port->p_node)) {
162 			p_physp =
163 			    osm_node_get_physp_ptr(p_port->p_node, port_num);
164 			/* check that the p_physp is valid, and that the requester
165 			   and the p_physp share a pkey. */
166 			if (p_physp &&
167 			    osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp,
168 						 sa->p_subn->opt.allow_both_pkeys))
169 				sa_vl_arb_check_physp(sa, p_physp, p_ctxt);
170 		} else {
171 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2A03: "
172 				"Given Physical Port Number: 0x%X is out of range should be < 0x%X\n",
173 				port_num,
174 				osm_node_get_num_physp(p_port->p_node));
175 			goto Exit;
176 		}
177 	} else {
178 		num_ports = osm_node_get_num_physp(p_port->p_node);
179 		for (port_num = 0; port_num < num_ports; port_num++) {
180 			p_physp =
181 			    osm_node_get_physp_ptr(p_port->p_node, port_num);
182 			if (!p_physp)
183 				continue;
184 
185 			/* if the requester and the p_physp don't share a pkey -
186 			   continue */
187 			if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp,
188 						  sa->p_subn->opt.allow_both_pkeys))
189 				continue;
190 
191 			sa_vl_arb_check_physp(sa, p_physp, p_ctxt);
192 		}
193 	}
194 Exit:
195 	OSM_LOG_EXIT(sa->p_log);
196 }
197 
198 static void sa_vl_arb_by_comp_mask_cb(IN cl_map_item_t * p_map_item, void *cxt)
199 {
200 	const osm_port_t *p_port = (osm_port_t *) p_map_item;
201 	osm_vl_arb_search_ctxt_t *p_ctxt = cxt;
202 
203 	sa_vl_arb_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
204 }
205 
206 void osm_vlarb_rec_rcv_process(IN void *ctx, IN void *data)
207 {
208 	osm_sa_t *sa = ctx;
209 	osm_madw_t *p_madw = data;
210 	const ib_sa_mad_t *sad_mad;
211 	const ib_vl_arb_table_record_t *p_rcvd_rec;
212 	const osm_port_t *p_port = NULL;
213 	cl_qlist_t rec_list;
214 	osm_vl_arb_search_ctxt_t context;
215 	ib_api_status_t status = IB_SUCCESS;
216 	ib_net64_t comp_mask;
217 	osm_physp_t *p_req_physp;
218 
219 	CL_ASSERT(sa);
220 
221 	OSM_LOG_ENTER(sa->p_log);
222 
223 	CL_ASSERT(p_madw);
224 
225 	sad_mad = osm_madw_get_sa_mad_ptr(p_madw);
226 	p_rcvd_rec =
227 	    (ib_vl_arb_table_record_t *) ib_sa_mad_get_payload_ptr(sad_mad);
228 	comp_mask = sad_mad->comp_mask;
229 
230 	CL_ASSERT(sad_mad->attr_id == IB_MAD_ATTR_VLARB_RECORD);
231 
232 	/* we only support SubnAdmGet and SubnAdmGetTable methods */
233 	if (sad_mad->method != IB_MAD_METHOD_GET &&
234 	    sad_mad->method != IB_MAD_METHOD_GETTABLE) {
235 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2A05: "
236 			"Unsupported Method (%s) for a VLArbRecord request\n",
237 			ib_get_sa_method_str(sad_mad->method));
238 		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
239 		goto Exit;
240 	}
241 
242 	cl_plock_acquire(sa->p_lock);
243 
244 	/* update the requester physical port */
245 	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
246 						osm_madw_get_mad_addr_ptr
247 						(p_madw));
248 	if (p_req_physp == NULL) {
249 		cl_plock_release(sa->p_lock);
250 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2A04: "
251 			"Cannot find requester physical port\n");
252 		goto Exit;
253 	}
254 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
255 		"Requester port GUID 0x%" PRIx64 "\n",
256 		cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
257 
258 	cl_qlist_init(&rec_list);
259 
260 	context.p_rcvd_rec = p_rcvd_rec;
261 	context.p_list = &rec_list;
262 	context.comp_mask = sad_mad->comp_mask;
263 	context.sa = sa;
264 	context.block_num = p_rcvd_rec->block_num;
265 	context.p_req_physp = p_req_physp;
266 
267 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
268 		"Got Query Lid:%u(%02X), Port:0x%02X(%02X), Block:0x%02X(%02X)\n",
269 		cl_ntoh16(p_rcvd_rec->lid),
270 		(comp_mask & IB_VLA_COMPMASK_LID) != 0, p_rcvd_rec->port_num,
271 		(comp_mask & IB_VLA_COMPMASK_OUT_PORT) != 0,
272 		p_rcvd_rec->block_num,
273 		(comp_mask & IB_VLA_COMPMASK_BLOCK) != 0);
274 
275 	/*
276 	   If the user specified a LID, it obviously narrows our
277 	   work load, since we don't have to search every port
278 	 */
279 	if (comp_mask & IB_VLA_COMPMASK_LID) {
280 		p_port = osm_get_port_by_lid(sa->p_subn, p_rcvd_rec->lid);
281 		if (!p_port) {
282 			status = IB_NOT_FOUND;
283 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2A09: "
284 				"No port found with LID %u\n",
285 				cl_ntoh16(p_rcvd_rec->lid));
286 		}
287 	}
288 
289 	if (status == IB_SUCCESS) {
290 		/* if we got a unique port - no need for a port search */
291 		if (p_port)
292 			/*  this does the loop on all the port phys ports */
293 			sa_vl_arb_by_comp_mask(sa, p_port, &context);
294 		else
295 			cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
296 					   sa_vl_arb_by_comp_mask_cb, &context);
297 	}
298 
299 	cl_plock_release(sa->p_lock);
300 
301 	osm_sa_respond(sa, p_madw, sizeof(ib_vl_arb_table_record_t), &rec_list);
302 
303 Exit:
304 	OSM_LOG_EXIT(sa->p_log);
305 }
306