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_slvl_rec_rcv_t.
40  * This object represents the SLtoVL Mapping Query 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_SLVL_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_SLVL_RESP_SIZE SA_ITEM_RESP_SIZE(slvl_rec)
64 
65 typedef struct osm_slvl_search_ctxt {
66 	const ib_slvl_table_record_t *p_rcvd_rec;
67 	ib_net64_t comp_mask;
68 	uint8_t in_port_num;
69 	cl_qlist_t *p_list;
70 	osm_sa_t *sa;
71 	const osm_physp_t *p_req_physp;
72 } osm_slvl_search_ctxt_t;
73 
sa_slvl_create(IN osm_sa_t * sa,IN const osm_physp_t * p_physp,IN osm_slvl_search_ctxt_t * p_ctxt,IN uint8_t in_port_idx)74 static void sa_slvl_create(IN osm_sa_t * sa, IN const osm_physp_t * p_physp,
75 			   IN osm_slvl_search_ctxt_t * p_ctxt,
76 			   IN uint8_t in_port_idx)
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_SLVL_RESP_SIZE);
84 	if (p_rec_item == NULL) {
85 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2602: "
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 SLtoVL Map for: OUT port 0x%016" PRIx64
97 		", lid 0x%X, port %u to In Port:%u\n",
98 		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
99 		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), in_port_idx);
100 
101 	memset(p_rec_item, 0, SA_SLVL_RESP_SIZE);
102 
103 	p_rec_item->resp.slvl_rec.lid = lid;
104 	if (p_physp->p_node->node_info.node_type == IB_NODE_TYPE_SWITCH) {
105 		p_rec_item->resp.slvl_rec.out_port_num = osm_physp_get_port_num(p_physp);
106 		p_rec_item->resp.slvl_rec.in_port_num = in_port_idx;
107 	}
108 	p_rec_item->resp.slvl_rec.slvl_tbl =
109 	    *(osm_physp_get_slvl_tbl(p_physp, in_port_idx));
110 
111 	cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
112 
113 Exit:
114 	OSM_LOG_EXIT(sa->p_log);
115 }
116 
sa_slvl_by_comp_mask(IN osm_sa_t * sa,IN const osm_port_t * p_port,osm_slvl_search_ctxt_t * p_ctxt)117 static void sa_slvl_by_comp_mask(IN osm_sa_t * sa, IN const osm_port_t * p_port,
118 				 osm_slvl_search_ctxt_t * p_ctxt)
119 {
120 	const ib_slvl_table_record_t *p_rcvd_rec;
121 	ib_net64_t comp_mask;
122 	const osm_physp_t *p_out_physp, *p_in_physp;
123 	uint8_t in_port_num, out_port_num;
124 	uint8_t num_ports;
125 	uint8_t in_port_start, in_port_end;
126 	uint8_t out_port_start, out_port_end;
127 	const osm_physp_t *p_req_physp;
128 
129 	OSM_LOG_ENTER(sa->p_log);
130 
131 	p_rcvd_rec = p_ctxt->p_rcvd_rec;
132 	comp_mask = p_ctxt->comp_mask;
133 	num_ports = osm_node_get_num_physp(p_port->p_node);
134 	in_port_start = 0;
135 	in_port_end = num_ports - 1;
136 	out_port_start = 0;
137 	out_port_end = num_ports - 1;
138 	p_req_physp = p_ctxt->p_req_physp;
139 
140 	if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
141 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
142 			"Using Physical Default Port Number: 0x%X (for End Node)\n",
143 			p_port->p_physp->port_num);
144 		p_out_physp = p_port->p_physp;
145 		/* check that the p_out_physp and the p_req_physp share a pkey */
146 		if (osm_physp_share_pkey(sa->p_log, p_req_physp, p_out_physp,
147 					 sa->p_subn->opt.allow_both_pkeys))
148 			sa_slvl_create(sa, p_out_physp, p_ctxt, 0);
149 	} else {
150 		if (comp_mask & IB_SLVL_COMPMASK_OUT_PORT)
151 			out_port_start = out_port_end =
152 			    p_rcvd_rec->out_port_num;
153 		if (comp_mask & IB_SLVL_COMPMASK_IN_PORT)
154 			in_port_start = in_port_end = p_rcvd_rec->in_port_num;
155 
156 		for (out_port_num = out_port_start;
157 		     out_port_num <= out_port_end; out_port_num++) {
158 			p_out_physp =
159 			    osm_node_get_physp_ptr(p_port->p_node,
160 						   out_port_num);
161 			if (!p_out_physp)
162 				continue;
163 
164 			for (in_port_num = in_port_start;
165 			     in_port_num <= in_port_end; in_port_num++) {
166 #if 0
167 				if (out_port_num && out_port_num == in_port_num)
168 					continue;
169 #endif
170 
171 				p_in_physp =
172 				    osm_node_get_physp_ptr(p_port->p_node,
173 							   in_port_num);
174 				if (!p_in_physp)
175 					continue;
176 
177 				/* if the requester and the p_out_physp don't share a pkey -
178 				   continue */
179 				if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_out_physp,
180 							  sa->p_subn->opt.allow_both_pkeys))
181 					continue;
182 
183 				sa_slvl_create(sa, p_out_physp, p_ctxt,
184 					       in_port_num);
185 			}
186 		}
187 	}
188 	OSM_LOG_EXIT(sa->p_log);
189 }
190 
sa_slvl_by_comp_mask_cb(IN cl_map_item_t * p_map_item,IN void * cxt)191 static void sa_slvl_by_comp_mask_cb(IN cl_map_item_t * p_map_item, IN void *cxt)
192 {
193 	const osm_port_t *p_port = (osm_port_t *) p_map_item;
194 	osm_slvl_search_ctxt_t *p_ctxt = cxt;
195 
196 	sa_slvl_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
197 }
198 
osm_slvl_rec_rcv_process(IN void * ctx,IN void * data)199 void osm_slvl_rec_rcv_process(IN void *ctx, IN void *data)
200 {
201 	osm_sa_t *sa = ctx;
202 	osm_madw_t *p_madw = data;
203 	const ib_sa_mad_t *p_rcvd_mad;
204 	const ib_slvl_table_record_t *p_rcvd_rec;
205 	const osm_port_t *p_port = NULL;
206 	cl_qlist_t rec_list;
207 	osm_slvl_search_ctxt_t context;
208 	ib_api_status_t status = IB_SUCCESS;
209 	ib_net64_t comp_mask;
210 	osm_physp_t *p_req_physp;
211 
212 	CL_ASSERT(sa);
213 
214 	OSM_LOG_ENTER(sa->p_log);
215 
216 	CL_ASSERT(p_madw);
217 
218 	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
219 	p_rcvd_rec =
220 	    (ib_slvl_table_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
221 	comp_mask = p_rcvd_mad->comp_mask;
222 
223 	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_SLVL_RECORD);
224 
225 	/* we only support SubnAdmGet and SubnAdmGetTable methods */
226 	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
227 	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
228 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2604: "
229 			"Unsupported Method (%s) for SL2VLRecord request\n",
230 			ib_get_sa_method_str(p_rcvd_mad->method));
231 		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
232 		goto Exit;
233 	}
234 
235 	cl_plock_acquire(sa->p_lock);
236 
237 	/* update the requester physical port */
238 	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
239 						osm_madw_get_mad_addr_ptr
240 						(p_madw));
241 	if (p_req_physp == NULL) {
242 		cl_plock_release(sa->p_lock);
243 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2603: "
244 			"Cannot find requester physical port\n");
245 		goto Exit;
246 	}
247 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
248 		"Requester port GUID 0x%" PRIx64 "\n",
249 		cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
250 
251 	cl_qlist_init(&rec_list);
252 
253 	context.p_rcvd_rec = p_rcvd_rec;
254 	context.p_list = &rec_list;
255 	context.comp_mask = p_rcvd_mad->comp_mask;
256 	context.sa = sa;
257 	context.in_port_num = p_rcvd_rec->in_port_num;
258 	context.p_req_physp = p_req_physp;
259 
260 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
261 		"Got Query Lid:%u(%02X), In-Port:0x%02X(%02X), Out-Port:0x%02X(%02X)\n",
262 		cl_ntoh16(p_rcvd_rec->lid),
263 		(comp_mask & IB_SLVL_COMPMASK_LID) != 0,
264 		p_rcvd_rec->in_port_num,
265 		(comp_mask & IB_SLVL_COMPMASK_IN_PORT) != 0,
266 		p_rcvd_rec->out_port_num,
267 		(comp_mask & IB_SLVL_COMPMASK_OUT_PORT) != 0);
268 
269 	/*
270 	   If the user specified a LID, it obviously narrows our
271 	   work load, since we don't have to search every port
272 	 */
273 	if (comp_mask & IB_SLVL_COMPMASK_LID) {
274 		p_port = osm_get_port_by_lid(sa->p_subn, p_rcvd_rec->lid);
275 		if (!p_port) {
276 			status = IB_NOT_FOUND;
277 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2608: "
278 				"No port found with LID %u\n",
279 				cl_ntoh16(p_rcvd_rec->lid));
280 		}
281 	}
282 
283 	if (status == IB_SUCCESS) {
284 		/* if we have a unique port - no need for a port search */
285 		if (p_port)
286 			/*  this does the loop on all the port phys ports */
287 			sa_slvl_by_comp_mask(sa, p_port, &context);
288 		else
289 			cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
290 					   sa_slvl_by_comp_mask_cb, &context);
291 	}
292 
293 	cl_plock_release(sa->p_lock);
294 
295 	osm_sa_respond(sa, p_madw, sizeof(ib_slvl_table_record_t), &rec_list);
296 
297 Exit:
298 	OSM_LOG_EXIT(sa->p_log);
299 }
300