1 /*
2  * Copyright (c) 2006-2009 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2002-2012 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_gir_rcv_t.
40  * This object represents the GUIDInfoRecord 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_GUIDINFO_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_guid.h>
60 #include <opensm/osm_helper.h>
61 #include <opensm/osm_pkey.h>
62 #include <opensm/osm_sa.h>
63 
64 #define SA_GIR_RESP_SIZE SA_ITEM_RESP_SIZE(guid_rec)
65 
66 #define MOD_GIR_COMP_MASK (IB_GIR_COMPMASK_LID | IB_GIR_COMPMASK_BLOCKNUM)
67 
68 typedef struct osm_gir_item {
69 	cl_list_item_t list_item;
70 	ib_guidinfo_record_t rec;
71 } osm_gir_item_t;
72 
73 typedef struct osm_gir_search_ctxt {
74 	const ib_guidinfo_record_t *p_rcvd_rec;
75 	ib_net64_t comp_mask;
76 	cl_qlist_t *p_list;
77 	osm_sa_t *sa;
78 	const osm_physp_t *p_req_physp;
79 } osm_gir_search_ctxt_t;
80 
gir_rcv_new_gir(IN osm_sa_t * sa,IN const osm_node_t * p_node,IN cl_qlist_t * p_list,IN ib_net64_t const match_port_guid,IN ib_net16_t const match_lid,IN const osm_physp_t * p_physp,IN uint8_t const block_num)81 static ib_api_status_t gir_rcv_new_gir(IN osm_sa_t * sa,
82 				       IN const osm_node_t * p_node,
83 				       IN cl_qlist_t * p_list,
84 				       IN ib_net64_t const match_port_guid,
85 				       IN ib_net16_t const match_lid,
86 				       IN const osm_physp_t * p_physp,
87 				       IN uint8_t const block_num)
88 {
89 	osm_sa_item_t *p_rec_item;
90 	ib_api_status_t status = IB_SUCCESS;
91 
92 	OSM_LOG_ENTER(sa->p_log);
93 
94 	p_rec_item = malloc(SA_GIR_RESP_SIZE);
95 	if (p_rec_item == NULL) {
96 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5102: "
97 			"rec_item alloc failed\n");
98 		status = IB_INSUFFICIENT_RESOURCES;
99 		goto Exit;
100 	}
101 
102 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
103 		"New GUIDInfoRecord: lid %u, block num %d\n",
104 		cl_ntoh16(match_lid), block_num);
105 
106 	memset(p_rec_item, 0, SA_GIR_RESP_SIZE);
107 
108 	p_rec_item->resp.guid_rec.lid = match_lid;
109 	p_rec_item->resp.guid_rec.block_num = block_num;
110 	if (p_physp->p_guids)
111 		memcpy(&p_rec_item->resp.guid_rec.guid_info,
112 		       *p_physp->p_guids + block_num * GUID_TABLE_MAX_ENTRIES,
113 		       sizeof(ib_guid_info_t));
114 	else if (!block_num)
115 		p_rec_item->resp.guid_rec.guid_info.guid[0] = osm_physp_get_port_guid(p_physp);
116 
117 	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
118 
119 Exit:
120 	OSM_LOG_EXIT(sa->p_log);
121 	return status;
122 }
123 
sa_gir_create_gir(IN osm_sa_t * sa,IN osm_node_t * p_node,IN cl_qlist_t * p_list,IN ib_net64_t const match_port_guid,IN ib_net16_t const match_lid,IN const osm_physp_t * p_req_physp,IN uint8_t const match_block_num)124 static void sa_gir_create_gir(IN osm_sa_t * sa, IN osm_node_t * p_node,
125 			      IN cl_qlist_t * p_list,
126 			      IN ib_net64_t const match_port_guid,
127 			      IN ib_net16_t const match_lid,
128 			      IN const osm_physp_t * p_req_physp,
129 			      IN uint8_t const match_block_num)
130 {
131 	const osm_physp_t *p_physp;
132 	uint8_t port_num;
133 	uint8_t num_ports;
134 	uint16_t match_lid_ho;
135 	ib_net16_t base_lid_ho;
136 	ib_net16_t max_lid_ho;
137 	uint8_t lmc;
138 	ib_net64_t port_guid;
139 	uint8_t block_num, start_block_num, end_block_num, num_blocks;
140 
141 	OSM_LOG_ENTER(sa->p_log);
142 
143 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
144 		"Looking for GUIDRecord with LID: %u GUID:0x%016"
145 		PRIx64 "\n", cl_ntoh16(match_lid), cl_ntoh64(match_port_guid));
146 
147 	/*
148 	   For switches, do not return the GUIDInfo record(s)
149 	   for each port on the switch, just for port 0.
150 	 */
151 	if (osm_node_get_type(p_node) == IB_NODE_TYPE_SWITCH)
152 		num_ports = 1;
153 	else
154 		num_ports = osm_node_get_num_physp(p_node);
155 
156 	for (port_num = 0; port_num < num_ports; port_num++) {
157 		p_physp = osm_node_get_physp_ptr(p_node, port_num);
158 		if (!p_physp)
159 			continue;
160 
161 		/* Check to see if the found p_physp and the requester physp
162 		   share a pkey. If not, continue */
163 		if (!osm_physp_share_pkey(sa->p_log, p_physp, p_req_physp,
164 					  sa->p_subn->opt.allow_both_pkeys))
165 			continue;
166 
167 		port_guid = osm_physp_get_port_guid(p_physp);
168 
169 		if (match_port_guid && (port_guid != match_port_guid))
170 			continue;
171 
172 		/*
173 		   Note: the following check is a temporary workaround
174 		   Since 1. GUIDCap should never be 0 on ports where this applies
175 		   and   2. GUIDCap should not be used on ports where it doesn't apply
176 		   So this should really be a check for whether the port is a
177 		   switch external port or not!
178 		 */
179 		if (p_physp->port_info.guid_cap == 0)
180 			continue;
181 
182 		num_blocks = p_physp->port_info.guid_cap / 8;
183 		if (p_physp->port_info.guid_cap % 8)
184 			num_blocks++;
185 		if (match_block_num == 255) {
186 			start_block_num = 0;
187 			end_block_num = num_blocks - 1;
188 		} else {
189 			if (match_block_num >= num_blocks)
190 				continue;
191 			end_block_num = start_block_num = match_block_num;
192 		}
193 
194 		base_lid_ho = cl_ntoh16(osm_physp_get_base_lid(p_physp));
195 		match_lid_ho = cl_ntoh16(match_lid);
196 		if (match_lid_ho) {
197 			lmc = osm_physp_get_lmc(p_physp);
198 			max_lid_ho = (uint16_t) (base_lid_ho + (1 << lmc) - 1);
199 
200 			/*
201 			   We validate that the lid belongs to this node.
202 			 */
203 			OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
204 				"Comparing LID: %u <= %u <= %u\n",
205 				base_lid_ho, match_lid_ho, max_lid_ho);
206 
207 			if (match_lid_ho < base_lid_ho
208 			    || match_lid_ho > max_lid_ho)
209 				continue;
210 		}
211 
212 		for (block_num = start_block_num; block_num <= end_block_num;
213 		     block_num++)
214 			gir_rcv_new_gir(sa, p_node, p_list, port_guid,
215 					cl_ntoh16(base_lid_ho), p_physp,
216 					block_num);
217 	}
218 
219 	OSM_LOG_EXIT(sa->p_log);
220 }
221 
sa_gir_by_comp_mask_cb(IN cl_map_item_t * p_map_item,IN void * cxt)222 static void sa_gir_by_comp_mask_cb(IN cl_map_item_t * p_map_item, IN void *cxt)
223 {
224 	const osm_gir_search_ctxt_t *p_ctxt = cxt;
225 	osm_node_t *const p_node = (osm_node_t *) p_map_item;
226 	const ib_guidinfo_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
227 	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
228 	osm_sa_t *sa = p_ctxt->sa;
229 	const ib_guid_info_t *p_comp_gi;
230 	ib_net64_t const comp_mask = p_ctxt->comp_mask;
231 	ib_net64_t match_port_guid = 0;
232 	ib_net16_t match_lid = 0;
233 	uint8_t match_block_num = 255;
234 
235 	OSM_LOG_ENTER(p_ctxt->sa->p_log);
236 
237 	if (comp_mask & IB_GIR_COMPMASK_LID)
238 		match_lid = p_rcvd_rec->lid;
239 
240 	if (comp_mask & IB_GIR_COMPMASK_BLOCKNUM)
241 		match_block_num = p_rcvd_rec->block_num;
242 
243 	p_comp_gi = &p_rcvd_rec->guid_info;
244 	/* Different rule for block 0 v. other blocks */
245 	if (comp_mask & IB_GIR_COMPMASK_GID0) {
246 		if (!p_rcvd_rec->block_num)
247 			match_port_guid = osm_physp_get_port_guid(p_req_physp);
248 		if (p_comp_gi->guid[0] != match_port_guid)
249 			goto Exit;
250 	}
251 
252 	if (comp_mask & IB_GIR_COMPMASK_GID1) {
253 		if (p_comp_gi->guid[1] != 0)
254 			goto Exit;
255 	}
256 
257 	if (comp_mask & IB_GIR_COMPMASK_GID2) {
258 		if (p_comp_gi->guid[2] != 0)
259 			goto Exit;
260 	}
261 
262 	if (comp_mask & IB_GIR_COMPMASK_GID3) {
263 		if (p_comp_gi->guid[3] != 0)
264 			goto Exit;
265 	}
266 
267 	if (comp_mask & IB_GIR_COMPMASK_GID4) {
268 		if (p_comp_gi->guid[4] != 0)
269 			goto Exit;
270 	}
271 
272 	if (comp_mask & IB_GIR_COMPMASK_GID5) {
273 		if (p_comp_gi->guid[5] != 0)
274 			goto Exit;
275 	}
276 
277 	if (comp_mask & IB_GIR_COMPMASK_GID6) {
278 		if (p_comp_gi->guid[6] != 0)
279 			goto Exit;
280 	}
281 
282 	if (comp_mask & IB_GIR_COMPMASK_GID7) {
283 		if (p_comp_gi->guid[7] != 0)
284 			goto Exit;
285 	}
286 
287 	sa_gir_create_gir(sa, p_node, p_ctxt->p_list, match_port_guid,
288 			  match_lid, p_req_physp, match_block_num);
289 
290 Exit:
291 	OSM_LOG_EXIT(p_ctxt->sa->p_log);
292 }
293 
check_mod_comp_mask(ib_net64_t comp_mask)294 static inline boolean_t check_mod_comp_mask(ib_net64_t comp_mask)
295 {
296 	return ((comp_mask & MOD_GIR_COMP_MASK) == MOD_GIR_COMP_MASK);
297 }
298 
coalesce_comp_mask(IN osm_madw_t * p_madw)299 static uint8_t coalesce_comp_mask(IN osm_madw_t *p_madw)
300 {
301 	uint8_t comp_mask = 0;
302 	ib_sa_mad_t *p_sa_mad;
303 
304 	p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
305 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID0)
306 		comp_mask |= 1<<0;
307 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID1)
308 		comp_mask |= 1<<1;
309 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID2)
310 		comp_mask |= 1<<2;
311 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID3)
312 		comp_mask |= 1<<3;
313 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID4)
314 		comp_mask |= 1<<4;
315 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID5)
316 		comp_mask |= 1<<5;
317 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID6)
318 		comp_mask |= 1<<6;
319 	if (p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID7)
320 		comp_mask |= 1<<7;
321 	return comp_mask;
322 }
323 
guidinfo_respond(IN osm_sa_t * sa,IN osm_madw_t * p_madw,IN ib_guidinfo_record_t * p_guidinfo_rec)324 static void guidinfo_respond(IN osm_sa_t *sa, IN osm_madw_t *p_madw,
325 			     IN ib_guidinfo_record_t * p_guidinfo_rec)
326 {
327 	cl_qlist_t rec_list;
328 	osm_sa_item_t *item;
329 
330 	OSM_LOG_ENTER(sa->p_log);
331 
332 	item = malloc(SA_GIR_RESP_SIZE);
333 	if (!item) {
334 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5101: "
335 			"rec_item alloc failed\n");
336 		goto Exit;
337 	}
338 
339 	item->resp.guid_rec = *p_guidinfo_rec;
340 
341 	cl_qlist_init(&rec_list);
342 	cl_qlist_insert_tail(&rec_list, &item->list_item);
343 
344 	osm_sa_respond(sa, p_madw, sizeof(ib_guidinfo_record_t), &rec_list);
345 
346 Exit:
347 	OSM_LOG_EXIT(sa->p_log);
348 }
349 
gir_respond(IN osm_sa_t * sa,IN osm_madw_t * p_madw)350 static void gir_respond(IN osm_sa_t *sa, IN osm_madw_t *p_madw)
351 {
352 	ib_sa_mad_t *p_sa_mad;
353 	ib_guidinfo_record_t *p_rcvd_rec;
354 	ib_guidinfo_record_t guidinfo_rec;
355 
356 	p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
357 	p_rcvd_rec = (ib_guidinfo_record_t *) ib_sa_mad_get_payload_ptr(p_sa_mad);
358 	if (OSM_LOG_IS_ACTIVE_V2(sa->p_log, OSM_LOG_DEBUG))
359 		osm_dump_guidinfo_record_v2(sa->p_log, p_rcvd_rec, FILE_ID, OSM_LOG_DEBUG);
360 
361 	guidinfo_rec = *p_rcvd_rec;
362 	guidinfo_respond(sa, p_madw, &guidinfo_rec);
363 }
364 
sm_assigned_guid(uint8_t assigned_byte)365 static ib_net64_t sm_assigned_guid(uint8_t assigned_byte)
366 {
367 	static uint32_t uniq_count;
368 
369 	if (++uniq_count == 0) {
370 		uniq_count--;
371 		return 0;
372 	}
373 	return cl_hton64(((uint64_t) uniq_count) |
374 			 (((uint64_t) assigned_byte) << 32) |
375 			 (((uint64_t) OSM_VENDOR_ID_OPENIB) << 40));
376 }
377 
del_guidinfo(IN osm_sa_t * sa,IN osm_madw_t * p_madw,IN osm_port_t * p_port,IN uint8_t block_num)378 static void del_guidinfo(IN osm_sa_t *sa, IN osm_madw_t *p_madw,
379 			 IN osm_port_t *p_port, IN uint8_t block_num)
380 {
381 	int i;
382 	uint32_t max_block;
383 	ib_sa_mad_t *p_sa_mad;
384 	ib_guidinfo_record_t *p_rcvd_rec;
385 	ib_net64_t del_alias_guid;
386 	osm_alias_guid_t *p_alias_guid;
387 	cl_list_item_t *p_list_item;
388 	osm_mcm_port_t *p_mcm_port;
389 	osm_mcm_alias_guid_t *p_mcm_alias_guid;
390 	uint8_t del_mask;
391 	int dirty = 0;
392 
393 	if (!p_port->p_physp->p_guids)
394 		goto Exit;
395 
396 	max_block = (p_port->p_physp->port_info.guid_cap + GUID_TABLE_MAX_ENTRIES - 1) /
397 		     GUID_TABLE_MAX_ENTRIES;
398 
399 	if (block_num >= max_block) {
400 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5116: "
401 			"block_num %d is higher than Max GUID Cap block %d "
402 			"for port GUID 0x%" PRIx64 "\n",
403 			block_num, max_block, cl_ntoh64(p_port->p_physp->port_guid));
404 		CL_PLOCK_RELEASE(sa->p_lock);
405 		osm_sa_send_error(sa, p_madw,
406 				  IB_SA_MAD_STATUS_NO_RECORDS);
407 		return;
408 	}
409 
410 	p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
411 	p_rcvd_rec =
412 		(ib_guidinfo_record_t *) ib_sa_mad_get_payload_ptr(p_sa_mad);
413 
414 	del_mask = coalesce_comp_mask(p_madw);
415 
416 	for (i = block_num * GUID_TABLE_MAX_ENTRIES;
417 	     (block_num + 1) * GUID_TABLE_MAX_ENTRIES < p_port->p_physp->port_info.guid_cap ? i < (block_num + 1) * GUID_TABLE_MAX_ENTRIES : i < p_port->p_physp->port_info.guid_cap;
418 	     i++) {
419 		/* can't delete block 0 index 0 (base guid is RO) for alias guid table */
420 		if (i == 0 && p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID0) {
421 			CL_PLOCK_RELEASE(sa->p_lock);
422 			OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
423 				"Not allowed to delete RO GID 0\n");
424 			osm_sa_send_error(sa, p_madw,
425 					  IB_SA_MAD_STATUS_REQ_INVALID);
426 			return;
427 		}
428 		if (!(del_mask & 1<<(i % 8)))
429 			continue;
430 
431 		del_alias_guid = (*p_port->p_physp->p_guids)[i];
432 		if (del_alias_guid) {
433 			/* Search all of port's multicast groups for alias */
434 			p_list_item = cl_qlist_head(&p_port->mcm_list);
435 			while (p_list_item != cl_qlist_end(&p_port->mcm_list)) {
436 				p_mcm_port = cl_item_obj(p_list_item,
437 							 p_mcm_port, list_item);
438 				p_list_item = cl_qlist_next(p_list_item);
439 				p_mcm_alias_guid = osm_mgrp_get_mcm_alias_guid(p_mcm_port->mgrp, del_alias_guid);
440 				if (p_mcm_alias_guid) {
441 					CL_PLOCK_RELEASE(sa->p_lock);
442 					osm_sa_send_error(sa, p_madw,
443 							  IB_SA_MAD_STATUS_DENIED);
444 					return;
445 				}
446 			}
447 		}
448 	}
449 
450 	for (i = block_num * GUID_TABLE_MAX_ENTRIES;
451 	     (block_num + 1) * GUID_TABLE_MAX_ENTRIES < p_port->p_physp->port_info.guid_cap ? i < (block_num + 1) * GUID_TABLE_MAX_ENTRIES : i < p_port->p_physp->port_info.guid_cap;
452 	     i++) {
453 		if (!(del_mask & 1<<(i % 8)))
454 			continue;
455 
456 		del_alias_guid = (*p_port->p_physp->p_guids)[i];
457 		if (del_alias_guid) {
458 			/* remove original from alias guid table */
459 			p_alias_guid = (osm_alias_guid_t *)
460 				cl_qmap_remove(&sa->p_subn->alias_port_guid_tbl,
461 					       del_alias_guid);
462 			if (p_alias_guid != (osm_alias_guid_t *)
463 						cl_qmap_end(&sa->p_subn->alias_port_guid_tbl))
464 				osm_alias_guid_delete(&p_alias_guid);
465 			else
466 				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 510B: "
467 					"Original alias GUID 0x%" PRIx64
468 					" at index %u not found\n",
469 					cl_ntoh64(del_alias_guid), i);
470 			/* clear guid at index */
471 			(*p_port->p_physp->p_guids)[i] = 0;
472 			dirty = 1;
473 		}
474 	}
475 
476 	if (dirty) {
477 		if (osm_queue_guidinfo(sa, p_port, block_num))
478 			osm_sm_signal(sa->sm, OSM_SIGNAL_GUID_PROCESS_REQUEST);
479 		sa->dirty = TRUE;
480 	}
481 
482 	memcpy(&p_rcvd_rec->guid_info,
483 	       &((*p_port->p_physp->p_guids)[block_num * GUID_TABLE_MAX_ENTRIES]),
484 	       sizeof(ib_guid_info_t));
485 
486 Exit:
487 	CL_PLOCK_RELEASE(sa->p_lock);
488 	gir_respond(sa, p_madw);
489 }
490 
set_guidinfo(IN osm_sa_t * sa,IN osm_madw_t * p_madw,IN osm_port_t * p_port,IN uint8_t block_num)491 static void set_guidinfo(IN osm_sa_t *sa, IN osm_madw_t *p_madw,
492 			 IN osm_port_t *p_port, IN uint8_t block_num)
493 {
494 	uint32_t max_block;
495 	int i, j, dirty = 0;
496 	ib_sa_mad_t *p_sa_mad;
497 	ib_guidinfo_record_t *p_rcvd_rec;
498 	osm_assigned_guids_t *p_assigned_guids = 0;
499 	osm_alias_guid_t *p_alias_guid, *p_alias_guid_check;
500 	cl_map_item_t *p_item;
501 	ib_net64_t set_alias_guid, del_alias_guid, assigned_guid;
502 	uint8_t set_mask;
503 
504 	max_block = (p_port->p_physp->port_info.guid_cap + GUID_TABLE_MAX_ENTRIES - 1) /
505 		     GUID_TABLE_MAX_ENTRIES;
506 	if (block_num >= max_block) {
507 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5118: "
508 			"block_num %d is higher than Max GUID Cap block %d "
509 			"for port GUID 0x%" PRIx64 "\n",
510 			block_num, max_block, cl_ntoh64(p_port->p_physp->port_guid));
511 		CL_PLOCK_RELEASE(sa->p_lock);
512 		osm_sa_send_error(sa, p_madw,
513 				  IB_SA_MAD_STATUS_NO_RECORDS);
514 		return;
515 	}
516 	if (!p_port->p_physp->p_guids) {
517 		p_port->p_physp->p_guids = calloc(max_block * GUID_TABLE_MAX_ENTRIES,
518 						  sizeof(ib_net64_t));
519 		if (!p_port->p_physp->p_guids) {
520 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5103: "
521 				"GUID table memory allocation failed for port "
522 				"GUID 0x%" PRIx64 "\n",
523 				cl_ntoh64(p_port->p_physp->port_guid));
524 			CL_PLOCK_RELEASE(sa->p_lock);
525 			osm_sa_send_error(sa, p_madw,
526 					  IB_SA_MAD_STATUS_NO_RESOURCES);
527 			return;
528 		}
529 		/* setup base port guid in index 0 */
530 		(*p_port->p_physp->p_guids)[0] = p_port->p_physp->port_guid;
531 	}
532 
533 	p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
534 	p_rcvd_rec = (ib_guidinfo_record_t *) ib_sa_mad_get_payload_ptr(p_sa_mad);
535 
536 	if (OSM_LOG_IS_ACTIVE_V2(sa->p_log, OSM_LOG_DEBUG)) {
537 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG, "Dump of incoming record\n");
538 		osm_dump_guidinfo_record_v2(sa->p_log, p_rcvd_rec, FILE_ID, OSM_LOG_DEBUG);
539 	}
540 
541 	set_mask = coalesce_comp_mask(p_madw);
542 
543 	for (i = block_num * GUID_TABLE_MAX_ENTRIES;
544 	     (block_num + 1) * GUID_TABLE_MAX_ENTRIES < p_port->p_physp->port_info.guid_cap ? i < (block_num + 1) * GUID_TABLE_MAX_ENTRIES : i < p_port->p_physp->port_info.guid_cap;
545 	     i++) {
546 		/* can't set block 0 index 0 (base guid is RO) for alias guid table */
547 		if (i == 0 && p_sa_mad->comp_mask & IB_GIR_COMPMASK_GID0) {
548 			CL_PLOCK_RELEASE(sa->p_lock);
549 			OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
550 				"Not allowed to set RO GID 0\n");
551 			osm_sa_send_error(sa, p_madw,
552 					  IB_SA_MAD_STATUS_REQ_INVALID);
553 			return;
554 		}
555 
556 		if (!(set_mask & 1<<(i % 8)))
557 			continue;
558 
559 		set_alias_guid = p_rcvd_rec->guid_info.guid[i % 8];
560 		if (!set_alias_guid) {
561 			/* was a GUID previously assigned for this index ? */
562 			set_alias_guid = (*p_port->p_physp->p_guids)[i];
563 			if (set_alias_guid) {
564 				p_rcvd_rec->guid_info.guid[i % 8] = set_alias_guid;
565 				continue;
566 			}
567 			/* Is there a persistent SA assigned guid for this index ? */
568 			if (!p_assigned_guids)
569 				p_assigned_guids =
570 				    osm_get_assigned_guids_by_guid(sa->p_subn,
571 								   p_port->p_physp->port_guid);
572 			if (p_assigned_guids) {
573 				set_alias_guid = p_assigned_guids->assigned_guid[i];
574 				if (set_alias_guid) {
575 					p_rcvd_rec->guid_info.guid[i % 8] = set_alias_guid;
576 					p_item = cl_qmap_get(&sa->sm->p_subn->alias_port_guid_tbl,
577 							     set_alias_guid);
578 					if (p_item == cl_qmap_end(&sa->sm->p_subn->alias_port_guid_tbl))
579 						goto add_alias_guid;
580 					else {
581 						p_alias_guid = (osm_alias_guid_t *) p_item;
582 						if (p_alias_guid->p_base_port != p_port) {
583 							OSM_LOG(sa->p_log,
584 								OSM_LOG_ERROR,
585 								"ERR 5110: "
586 								" Assigned alias port GUID 0x%" PRIx64
587 								" index %d base port GUID 0x%" PRIx64
588 								" now attempted on port GUID 0x%" PRIx64
589 								"\n",
590 								cl_ntoh64(p_alias_guid->alias_guid), i,
591 								cl_ntoh64(p_alias_guid->p_base_port->guid),
592 								cl_ntoh64(p_port->guid));
593 							/* clear response guid at index to indicate duplicate */
594 							p_rcvd_rec->guid_info.guid[i % 8] = 0;
595 						}
596 						continue;
597 					}
598 				}
599 			}
600 		}
601 		if (!set_alias_guid) {
602 			for (j = 0; j < 1000; j++) {
603 				assigned_guid = sm_assigned_guid(sa->p_subn->opt.sm_assigned_guid);
604 				if (!assigned_guid) {
605 					CL_PLOCK_RELEASE(sa->p_lock);
606 					OSM_LOG(sa->p_log, OSM_LOG_ERROR,
607 						"ERR 510E: No more assigned guids available\n");
608 					osm_sa_send_error(sa, p_madw,
609 							  IB_SA_MAD_STATUS_NO_RESOURCES);
610 					return;
611 				}
612 				p_item = cl_qmap_get(&sa->sm->p_subn->alias_port_guid_tbl,
613 						     assigned_guid);
614 				if (p_item == cl_qmap_end(&sa->sm->p_subn->alias_port_guid_tbl)) {
615 					set_alias_guid = assigned_guid;
616 					p_rcvd_rec->guid_info.guid[i % 8] = assigned_guid;
617 					if (!p_assigned_guids) {
618 						p_assigned_guids = osm_assigned_guids_new(p_port->p_physp->port_guid,
619 											  max_block * GUID_TABLE_MAX_ENTRIES);
620 						if (p_assigned_guids) {
621 							cl_qmap_insert(&(sa->p_subn->assigned_guids_tbl),
622 								       p_assigned_guids->port_guid,
623 								       &p_assigned_guids->map_item);
624 						} else {
625 							OSM_LOG(sa->p_log,
626 								OSM_LOG_ERROR,
627 								"ERR 510D: osm_assigned_guids_new failed port GUID 0x%" PRIx64 " index %d\n",
628 								cl_ntoh64(p_port->p_physp->port_guid), i);
629 							CL_PLOCK_RELEASE(sa->p_lock);
630 							osm_sa_send_error(sa, p_madw,
631 									  IB_SA_MAD_STATUS_NO_RESOURCES);
632 							return;
633 						}
634 					}
635 					if (p_assigned_guids)
636 						p_assigned_guids->assigned_guid[i] = assigned_guid;
637 					break;
638 				}
639 			}
640 			if (!set_alias_guid) {
641 				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 510A: "
642 					"SA assigned GUID %d failed for "
643 					"port GUID 0x%" PRIx64 "\n", i,
644 					cl_ntoh64(p_port->p_physp->port_guid));
645 				continue;
646 			}
647 		}
648 
649 add_alias_guid:
650 		/* allocate alias guid and add to alias guid table */
651 		p_alias_guid = osm_alias_guid_new(set_alias_guid, p_port);
652 		if (!p_alias_guid) {
653 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5107: "
654 				"Alias guid %d memory allocation failed"
655 				" for port GUID 0x%" PRIx64 "\n",
656 				i, cl_ntoh64(p_port->p_physp->port_guid));
657 			CL_PLOCK_RELEASE(sa->p_lock);
658 			return;
659 		}
660 
661 		p_alias_guid_check =
662 			(osm_alias_guid_t *) cl_qmap_insert(&sa->sm->p_subn->alias_port_guid_tbl,
663 							    p_alias_guid->alias_guid,
664 							    &p_alias_guid->map_item);
665 		if (p_alias_guid_check != p_alias_guid) {
666 			/* alias GUID is a duplicate if it exists on another port or on the same port but at another index */
667 			if (p_alias_guid_check->p_base_port != p_port ||
668 			    (*p_port->p_physp->p_guids)[i] != set_alias_guid) {
669 				OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5108: "
670 					"Duplicate alias port GUID 0x%" PRIx64
671 					" index %d base port GUID 0x%" PRIx64
672 					", alias GUID already assigned to "
673 					"base port GUID 0x%" PRIx64 "\n",
674 					cl_ntoh64(p_alias_guid->alias_guid), i,
675 					cl_ntoh64(p_alias_guid->p_base_port->guid),
676 					cl_ntoh64(p_alias_guid_check->p_base_port->guid));
677 				/* clear response guid at index to indicate duplicate */
678 				p_rcvd_rec->guid_info.guid[i % 8] = 0;
679 			}
680 			osm_alias_guid_delete(&p_alias_guid);
681 		} else {
682 			del_alias_guid = (*p_port->p_physp->p_guids)[i];
683 			if (del_alias_guid) {
684 				/* remove original from alias guid table */
685 				p_alias_guid_check = (osm_alias_guid_t *)
686 					cl_qmap_remove(&sa->p_subn->alias_port_guid_tbl,
687 						       del_alias_guid);
688 				if (p_alias_guid_check)
689 					osm_alias_guid_delete(&p_alias_guid_check);
690 				else
691 					OSM_LOG(sa->p_log, OSM_LOG_ERROR,
692 						"ERR 510C: Original alias GUID "
693 						"0x%" PRIx64 "at index %u "
694 						"not found\n",
695 						cl_ntoh64(del_alias_guid),
696 						i);
697 			}
698 
699 			/* insert or replace guid at index */
700 			(*p_port->p_physp->p_guids)[i] = set_alias_guid;
701 			dirty = 1;
702 		}
703 	}
704 
705 	if (dirty) {
706 		if (osm_queue_guidinfo(sa, p_port, block_num))
707 			osm_sm_signal(sa->sm, OSM_SIGNAL_GUID_PROCESS_REQUEST);
708 		sa->dirty = TRUE;
709 	}
710 
711 	memcpy(&p_rcvd_rec->guid_info,
712 	       &((*p_port->p_physp->p_guids)[block_num * GUID_TABLE_MAX_ENTRIES]),
713 	       sizeof(ib_guid_info_t));
714 
715 	CL_PLOCK_RELEASE(sa->p_lock);
716 	gir_respond(sa, p_madw);
717 }
718 
get_guidinfo(IN osm_sa_t * sa,IN osm_madw_t * p_madw,IN osm_physp_t * p_req_physp)719 static void get_guidinfo(IN osm_sa_t *sa, IN osm_madw_t *p_madw,
720 			 IN osm_physp_t *p_req_physp)
721 {
722 	const ib_sa_mad_t *p_rcvd_mad;
723 	const ib_guidinfo_record_t *p_rcvd_rec;
724 	cl_qlist_t rec_list;
725 	osm_gir_search_ctxt_t context;
726 
727 	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
728 	p_rcvd_rec =
729 	    (ib_guidinfo_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
730 
731 	cl_qlist_init(&rec_list);
732 
733 	context.p_rcvd_rec = p_rcvd_rec;
734 	context.p_list = &rec_list;
735 	context.comp_mask = p_rcvd_mad->comp_mask;
736 	context.sa = sa;
737 	context.p_req_physp = p_req_physp;
738 
739 
740 	cl_qmap_apply_func(&sa->p_subn->node_guid_tbl, sa_gir_by_comp_mask_cb,
741 			   &context);
742 
743 	CL_PLOCK_RELEASE(sa->p_lock);
744 
745 	osm_sa_respond(sa, p_madw, sizeof(ib_guidinfo_record_t), &rec_list);
746 }
747 
osm_gir_rcv_process(IN void * ctx,IN void * data)748 void osm_gir_rcv_process(IN void *ctx, IN void *data)
749 {
750 	osm_sa_t *sa = ctx;
751 	osm_madw_t *p_madw = data;
752 	const ib_sa_mad_t *p_rcvd_mad;
753 	osm_physp_t *p_req_physp;
754 	osm_port_t *p_port;
755 	const ib_guidinfo_record_t *p_rcvd_rec;
756 
757 	CL_ASSERT(sa);
758 
759 	OSM_LOG_ENTER(sa->p_log);
760 
761 	CL_ASSERT(p_madw);
762 
763 	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
764 
765 	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_GUIDINFO_RECORD);
766 
767 	switch(p_rcvd_mad->method) {
768 	case IB_MAD_METHOD_GET:
769 	case IB_MAD_METHOD_GETTABLE:
770 		/* update the requester physical port */
771 		CL_PLOCK_ACQUIRE(sa->p_lock);
772 		p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
773 							osm_madw_get_mad_addr_ptr(p_madw));
774 		if (p_req_physp == NULL) {
775 			CL_PLOCK_RELEASE(sa->p_lock);
776 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5104: "
777 				"Cannot find requester physical port\n");
778 			goto Exit;
779 		}
780 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
781 			"Requester port GUID 0x%" PRIx64 "\n",
782 			cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
783 
784 		get_guidinfo(sa, p_madw, p_req_physp);
785 		goto Exit;
786 	case IB_MAD_METHOD_SET:
787 	case IB_MAD_METHOD_DELETE:
788 		if (!check_mod_comp_mask(p_rcvd_mad->comp_mask)) {
789 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5106: "
790 				"component mask = 0x%016" PRIx64 ", "
791 				"expected comp mask = 0x%016" PRIx64 "\n",
792 				cl_ntoh64(p_rcvd_mad->comp_mask),
793 				CL_NTOH64(MOD_GIR_COMP_MASK));
794 			osm_sa_send_error(sa, p_madw,
795 					  IB_SA_MAD_STATUS_INSUF_COMPS);
796 			goto Exit;
797 		}
798 		p_rcvd_rec = (ib_guidinfo_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
799 
800 		/* update the requester physical port */
801 		CL_PLOCK_EXCL_ACQUIRE(sa->p_lock);
802 		p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
803 							osm_madw_get_mad_addr_ptr(p_madw));
804 		if (p_req_physp == NULL) {
805 			CL_PLOCK_RELEASE(sa->p_lock);
806 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5104: "
807 				"Cannot find requester physical port\n");
808 			goto Exit;
809 		}
810 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
811 			"Requester port GUID 0x%" PRIx64 "\n",
812 			cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
813 
814 		p_port = osm_get_port_by_lid(sa->p_subn, p_rcvd_rec->lid);
815 		if (!p_port) {
816 			CL_PLOCK_RELEASE(sa->p_lock);
817 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5117: "
818 				"Port with LID %u not found\n",
819 				cl_ntoh16(p_rcvd_rec->lid));
820 				osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_NO_RECORDS);
821 			goto Exit;
822 		}
823 		if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_port->p_physp,
824 					  sa->p_subn->opt.allow_both_pkeys)) {
825 			CL_PLOCK_RELEASE(sa->p_lock);
826 			goto Exit;
827 		}
828 
829 		if (p_rcvd_mad->method == IB_MAD_METHOD_SET)
830 			set_guidinfo(sa, p_madw, p_port, p_rcvd_rec->block_num);
831 		else
832 			del_guidinfo(sa, p_madw, p_port, p_rcvd_rec->block_num);
833 		break;
834 	default:
835 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5105: "
836 			"Unsupported Method (%s) for GUIDInfoRecord request\n",
837 			ib_get_sa_method_str(p_rcvd_mad->method));
838 		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
839 		break;
840 	}
841 
842 Exit:
843 	OSM_LOG_EXIT(sa->p_log);
844 }
845