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_smir_rcv_t.
40  * This object represents the SMInfo 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_SMINFO_RECORD_C
56 #include <vendor/osm_vendor_api.h>
57 #include <opensm/osm_madw.h>
58 #include <opensm/osm_log.h>
59 #include <opensm/osm_subnet.h>
60 #include <opensm/osm_mad_pool.h>
61 #include <opensm/osm_helper.h>
62 #include <opensm/osm_msgdef.h>
63 #include <opensm/osm_port.h>
64 #include <opensm/osm_pkey.h>
65 #include <opensm/osm_remote_sm.h>
66 #include <opensm/osm_sa.h>
67 #include <opensm/osm_opensm.h>
68 
69 #define SA_SMIR_RESP_SIZE SA_ITEM_RESP_SIZE(sminfo_rec)
70 
71 typedef struct osm_smir_search_ctxt {
72 	const ib_sminfo_record_t *p_rcvd_rec;
73 	ib_net64_t comp_mask;
74 	cl_qlist_t *p_list;
75 	osm_sa_t *sa;
76 	const osm_physp_t *p_req_physp;
77 } osm_smir_search_ctxt_t;
78 
79 static ib_api_status_t smir_rcv_new_smir(IN osm_sa_t * sa,
80 					 IN const osm_port_t * p_port,
81 					 IN cl_qlist_t * p_list,
82 					 IN ib_net64_t const guid,
83 					 IN ib_net32_t const act_count,
84 					 IN uint8_t const pri_state,
85 					 IN const osm_physp_t * p_req_physp)
86 {
87 	osm_sa_item_t *p_rec_item;
88 	ib_api_status_t status = IB_SUCCESS;
89 
90 	OSM_LOG_ENTER(sa->p_log);
91 
92 	p_rec_item = malloc(SA_SMIR_RESP_SIZE);
93 	if (p_rec_item == NULL) {
94 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2801: "
95 			"rec_item alloc failed\n");
96 		status = IB_INSUFFICIENT_RESOURCES;
97 		goto Exit;
98 	}
99 
100 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
101 		"New SMInfo: GUID 0x%016" PRIx64 "\n", cl_ntoh64(guid));
102 
103 	memset(p_rec_item, 0, SA_SMIR_RESP_SIZE);
104 
105 	p_rec_item->resp.sminfo_rec.lid = osm_port_get_base_lid(p_port);
106 	p_rec_item->resp.sminfo_rec.sm_info.guid = guid;
107 	p_rec_item->resp.sminfo_rec.sm_info.act_count = act_count;
108 	p_rec_item->resp.sminfo_rec.sm_info.pri_state = pri_state;
109 
110 	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
111 
112 Exit:
113 	OSM_LOG_EXIT(sa->p_log);
114 	return status;
115 }
116 
117 static void sa_smir_by_comp_mask(IN osm_sa_t * sa,
118 				 IN const osm_remote_sm_t * p_rem_sm,
119 				 osm_smir_search_ctxt_t * p_ctxt)
120 {
121 	const ib_sminfo_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
122 	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
123 	ib_net64_t const comp_mask = p_ctxt->comp_mask;
124 	osm_port_t *p_port;
125 
126 	OSM_LOG_ENTER(sa->p_log);
127 
128 	if (comp_mask & IB_SMIR_COMPMASK_GUID) {
129 		if (p_rem_sm->smi.guid != p_rcvd_rec->sm_info.guid)
130 			goto Exit;
131 	}
132 
133 	if (comp_mask & IB_SMIR_COMPMASK_PRIORITY) {
134 		if (ib_sminfo_get_priority(&p_rem_sm->smi) !=
135 		    ib_sminfo_get_priority(&p_rcvd_rec->sm_info))
136 			goto Exit;
137 	}
138 
139 	if (comp_mask & IB_SMIR_COMPMASK_SMSTATE) {
140 		if (ib_sminfo_get_state(&p_rem_sm->smi) !=
141 		    ib_sminfo_get_state(&p_rcvd_rec->sm_info))
142 			goto Exit;
143 	}
144 
145 	/* Implement any other needed search cases */
146 	p_port = osm_get_port_by_guid(sa->p_subn, p_rem_sm->smi.guid);
147 
148         if (p_port == NULL) {
149                 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2810: "
150                         "No port for remote sm\n");
151                 goto Exit;
152         }
153 
154 	smir_rcv_new_smir(sa, p_port, p_ctxt->p_list,
155 			  p_rem_sm->smi.guid, p_rem_sm->smi.act_count,
156 			  p_rem_sm->smi.pri_state, p_req_physp);
157 
158 Exit:
159 	OSM_LOG_EXIT(sa->p_log);
160 }
161 
162 static void sa_smir_by_comp_mask_cb(IN cl_map_item_t * p_map_item, IN void *cxt)
163 {
164 	const osm_remote_sm_t *p_rem_sm = (osm_remote_sm_t *) p_map_item;
165 	osm_smir_search_ctxt_t *p_ctxt = cxt;
166 
167 	sa_smir_by_comp_mask(p_ctxt->sa, p_rem_sm, p_ctxt);
168 }
169 
170 void osm_smir_rcv_process(IN void *ctx, IN void *data)
171 {
172 	osm_sa_t *sa = ctx;
173 	osm_madw_t *p_madw = data;
174 	const ib_sa_mad_t *sad_mad;
175 	const ib_sminfo_record_t *p_rcvd_rec;
176 	const osm_port_t *p_port = NULL;
177 	const ib_sm_info_t *p_smi;
178 	cl_qlist_t rec_list;
179 	osm_smir_search_ctxt_t context;
180 	ib_api_status_t status = IB_SUCCESS;
181 	ib_net64_t comp_mask;
182 	ib_net64_t port_guid;
183 	osm_physp_t *p_req_physp;
184 	osm_port_t *local_port;
185 	osm_remote_sm_t *p_rem_sm;
186 	cl_qmap_t *p_sm_guid_tbl;
187 	uint8_t pri_state;
188 
189 	CL_ASSERT(sa);
190 
191 	OSM_LOG_ENTER(sa->p_log);
192 
193 	CL_ASSERT(p_madw);
194 
195 	sad_mad = osm_madw_get_sa_mad_ptr(p_madw);
196 	p_rcvd_rec = (ib_sminfo_record_t *) ib_sa_mad_get_payload_ptr(sad_mad);
197 	comp_mask = sad_mad->comp_mask;
198 
199 	CL_ASSERT(sad_mad->attr_id == IB_MAD_ATTR_SMINFO_RECORD);
200 
201 	/* we only support SubnAdmGet and SubnAdmGetTable methods */
202 	if (sad_mad->method != IB_MAD_METHOD_GET &&
203 	    sad_mad->method != IB_MAD_METHOD_GETTABLE) {
204 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2804: "
205 			"Unsupported Method (%s) for SMInfoRecord request\n",
206 			ib_get_sa_method_str(sad_mad->method));
207 		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
208 		goto Exit;
209 	}
210 
211 	cl_plock_acquire(sa->p_lock);
212 
213 	/* update the requester physical port */
214 	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
215 						osm_madw_get_mad_addr_ptr
216 						(p_madw));
217 	if (p_req_physp == NULL) {
218 		cl_plock_release(sa->p_lock);
219 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2803: "
220 			"Cannot find requester physical port\n");
221 		goto Exit;
222 	}
223 
224 	if (OSM_LOG_IS_ACTIVE_V2(sa->p_log, OSM_LOG_DEBUG)) {
225 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
226 			"Requester port GUID 0x%" PRIx64 "\n",
227 			cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
228 		osm_dump_sm_info_record_v2(sa->p_log, p_rcvd_rec, FILE_ID, OSM_LOG_DEBUG);
229 	}
230 
231 	p_smi = &p_rcvd_rec->sm_info;
232 
233 	cl_qlist_init(&rec_list);
234 
235 	context.p_rcvd_rec = p_rcvd_rec;
236 	context.p_list = &rec_list;
237 	context.comp_mask = sad_mad->comp_mask;
238 	context.sa = sa;
239 	context.p_req_physp = p_req_physp;
240 
241 	/*
242 	   If the user specified a LID, it obviously narrows our
243 	   work load, since we don't have to search every port
244 	 */
245 	if (comp_mask & IB_SMIR_COMPMASK_LID) {
246 		p_port = osm_get_port_by_lid(sa->p_subn, p_rcvd_rec->lid);
247 		if (!p_port) {
248 			status = IB_NOT_FOUND;
249 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2806: "
250 				"No port found with LID %u\n",
251 				cl_ntoh16(p_rcvd_rec->lid));
252 		}
253 	}
254 
255 	if (status == IB_SUCCESS) {
256 		/* Handle our own SM first */
257 		local_port = osm_get_port_by_guid(sa->p_subn,
258 						  sa->p_subn->sm_port_guid);
259 		if (!local_port) {
260 			cl_plock_release(sa->p_lock);
261 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2809: "
262 				"No port found with GUID 0x%016" PRIx64 "\n",
263 				cl_ntoh64(sa->p_subn->sm_port_guid));
264 			goto Exit;
265 		}
266 
267 		if (!p_port || local_port == p_port) {
268 			if (FALSE ==
269 			    osm_physp_share_pkey(sa->p_log, p_req_physp,
270 						 local_port->p_physp,
271 						 sa->p_subn->opt.allow_both_pkeys)) {
272 				cl_plock_release(sa->p_lock);
273 				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2805: "
274 					"Cannot get SMInfo record due to pkey violation\n");
275 				goto Exit;
276 			}
277 
278 			/* Check that other search components specified match */
279 			if ((comp_mask & IB_SMIR_COMPMASK_GUID) &&
280 			    sa->p_subn->sm_port_guid != p_smi->guid)
281 				goto Remotes;
282 			if ((comp_mask & IB_SMIR_COMPMASK_PRIORITY) &&
283 			    sa->p_subn->opt.sm_priority !=
284 			    ib_sminfo_get_priority(p_smi))
285 				goto Remotes;
286 			if ((comp_mask & IB_SMIR_COMPMASK_SMSTATE) &&
287 			    sa->p_subn->sm_state != ib_sminfo_get_state(p_smi))
288 				goto Remotes;
289 
290 			/* Now, add local SMInfo to list */
291 			pri_state = sa->p_subn->sm_state & 0x0F;
292 			pri_state |= (sa->p_subn->opt.sm_priority & 0x0F) << 4;
293 			smir_rcv_new_smir(sa, local_port, context.p_list,
294 					  sa->p_subn->sm_port_guid,
295 					  cl_ntoh32(sa->p_subn->p_osm->stats.
296 						    qp0_mads_sent), pri_state,
297 					  p_req_physp);
298 		}
299 
300 	      Remotes:
301 		if (p_port && p_port != local_port) {
302 			/* Find remote SM corresponding to p_port */
303 			port_guid = osm_port_get_guid(p_port);
304 			p_sm_guid_tbl = &sa->p_subn->sm_guid_tbl;
305 			p_rem_sm =
306 			    (osm_remote_sm_t *) cl_qmap_get(p_sm_guid_tbl,
307 							    port_guid);
308 			if (p_rem_sm !=
309 			    (osm_remote_sm_t *) cl_qmap_end(p_sm_guid_tbl))
310 				sa_smir_by_comp_mask(sa, p_rem_sm, &context);
311 			else
312 				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 280A: "
313 					"No remote SM for GUID 0x%016" PRIx64
314 					"\n", cl_ntoh64(port_guid));
315 		} else if (!p_port) {
316 			/* Go over all other known (remote) SMs */
317 			cl_qmap_apply_func(&sa->p_subn->sm_guid_tbl,
318 					   sa_smir_by_comp_mask_cb, &context);
319 		}
320 	}
321 
322 	cl_plock_release(sa->p_lock);
323 
324 	osm_sa_respond(sa, p_madw, sizeof(ib_sminfo_record_t), &rec_list);
325 
326 Exit:
327 	OSM_LOG_EXIT(sa->p_log);
328 }
329