1 /*
2  * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2002-2005,2008 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_lftr_rcv_t.
40  *   This object represents the LinearForwardingTable 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_debug.h>
51 #include <complib/cl_qlist.h>
52 #include <opensm/osm_file_ids.h>
53 #define FILE_ID OSM_FILE_SA_LFT_RECORD_C
54 #include <vendor/osm_vendor_api.h>
55 #include <opensm/osm_switch.h>
56 #include <opensm/osm_helper.h>
57 #include <opensm/osm_pkey.h>
58 #include <opensm/osm_sa.h>
59 
60 #define SA_LFTR_RESP_SIZE SA_ITEM_RESP_SIZE(lft_rec)
61 
62 typedef struct osm_lftr_search_ctxt {
63 	const ib_lft_record_t *p_rcvd_rec;
64 	ib_net64_t comp_mask;
65 	cl_qlist_t *p_list;
66 	osm_sa_t *sa;
67 	const osm_physp_t *p_req_physp;
68 } osm_lftr_search_ctxt_t;
69 
70 static ib_api_status_t lftr_rcv_new_lftr(IN osm_sa_t * sa,
71 					 IN const osm_switch_t * p_sw,
72 					 IN cl_qlist_t * p_list,
73 					 IN ib_net16_t lid, IN uint16_t block)
74 {
75 	osm_sa_item_t *p_rec_item;
76 	ib_api_status_t status = IB_SUCCESS;
77 
78 	OSM_LOG_ENTER(sa->p_log);
79 
80 	p_rec_item = malloc(SA_LFTR_RESP_SIZE);
81 	if (p_rec_item == NULL) {
82 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4402: "
83 			"rec_item alloc failed\n");
84 		status = IB_INSUFFICIENT_RESOURCES;
85 		goto Exit;
86 	}
87 
88 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
89 		"New LinearForwardingTable: sw 0x%016" PRIx64
90 		"\n\t\t\t\tblock 0x%02X lid %u\n",
91 		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
92 		block, cl_ntoh16(lid));
93 
94 	memset(p_rec_item, 0, SA_LFTR_RESP_SIZE);
95 
96 	p_rec_item->resp.lft_rec.lid = lid;
97 	p_rec_item->resp.lft_rec.block_num = cl_hton16(block);
98 
99 	/* copy the lft block */
100 	osm_switch_get_lft_block(p_sw, block, p_rec_item->resp.lft_rec.lft);
101 
102 	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
103 
104 Exit:
105 	OSM_LOG_EXIT(sa->p_log);
106 	return status;
107 }
108 
109 static void lftr_rcv_by_comp_mask(IN cl_map_item_t * p_map_item, IN void *cxt)
110 {
111 	const osm_lftr_search_ctxt_t *p_ctxt = cxt;
112 	const osm_switch_t *p_sw = (osm_switch_t *) p_map_item;
113 	const ib_lft_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
114 	osm_sa_t *sa = p_ctxt->sa;
115 	ib_net64_t const comp_mask = p_ctxt->comp_mask;
116 	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
117 	osm_port_t *p_port;
118 	uint16_t min_lid_ho, max_lid_ho;
119 	uint16_t min_block, max_block, block;
120 	const osm_physp_t *p_physp;
121 
122 	/* In switches, the port guid is the node guid. */
123 	p_port = osm_get_port_by_guid(sa->p_subn,
124 				      p_sw->p_node->node_info.port_guid);
125 	if (!p_port) {
126 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4405: "
127 			"Failed to find Port by Node Guid:0x%016" PRIx64
128 			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
129 		return;
130 	}
131 
132 	/* check that the requester physp and the current physp are under
133 	   the same partition. */
134 	p_physp = p_port->p_physp;
135 	if (!p_physp) {
136 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4406: "
137 			"Failed to find default physical Port by Node Guid:0x%016"
138 			PRIx64 "\n",
139 			cl_ntoh64(p_sw->p_node->node_info.node_guid));
140 		return;
141 	}
142 	if (!osm_physp_share_pkey(sa->p_log, p_req_physp,
143 				  p_physp, sa->p_subn->opt.allow_both_pkeys))
144 		return;
145 
146 	/* get the port 0 of the switch */
147 	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
148 
149 	/* compare the lids - if required */
150 	if (comp_mask & IB_LFTR_COMPMASK_LID) {
151 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
152 			"Comparing lid:%u to port lid range: %u .. %u\n",
153 			cl_ntoh16(p_rcvd_rec->lid), min_lid_ho, max_lid_ho);
154 		/* ok we are ready for range check */
155 		if (min_lid_ho > cl_ntoh16(p_rcvd_rec->lid) ||
156 		    max_lid_ho < cl_ntoh16(p_rcvd_rec->lid))
157 			return;
158 	}
159 
160 	/* now we need to decide which blocks to output */
161 	max_block = osm_switch_get_max_block_id_in_use(p_sw);
162 	if (comp_mask & IB_LFTR_COMPMASK_BLOCK) {
163 		min_block = cl_ntoh16(p_rcvd_rec->block_num);
164 		if (min_block > max_block)
165 			return;
166 		max_block = min_block;
167 	} else			/* use as many blocks as "in use" */
168 		min_block = 0;
169 
170 	/* so we can add these blocks one by one ... */
171 	for (block = min_block; block <= max_block; block++)
172 		lftr_rcv_new_lftr(sa, p_sw, p_ctxt->p_list,
173 				  osm_port_get_base_lid(p_port), block);
174 }
175 
176 void osm_lftr_rcv_process(IN void *ctx, IN void *data)
177 {
178 	osm_sa_t *sa = ctx;
179 	osm_madw_t *p_madw = data;
180 	const ib_sa_mad_t *p_rcvd_mad;
181 	const ib_lft_record_t *p_rcvd_rec;
182 	cl_qlist_t rec_list;
183 	osm_lftr_search_ctxt_t context;
184 	osm_physp_t *p_req_physp;
185 
186 	CL_ASSERT(sa);
187 
188 	OSM_LOG_ENTER(sa->p_log);
189 
190 	CL_ASSERT(p_madw);
191 
192 	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
193 	p_rcvd_rec = (ib_lft_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
194 
195 	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_LFT_RECORD);
196 
197 	/* we only support SubnAdmGet and SubnAdmGetTable methods */
198 	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
199 	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
200 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4408: "
201 			"Unsupported Method (%s) for LFTRecord request\n",
202 			ib_get_sa_method_str(p_rcvd_mad->method));
203 		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
204 		goto Exit;
205 	}
206 
207 	cl_plock_acquire(sa->p_lock);
208 
209 	/* update the requester physical port */
210 	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
211 						osm_madw_get_mad_addr_ptr
212 						(p_madw));
213 	if (p_req_physp == NULL) {
214 		cl_plock_release(sa->p_lock);
215 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4407: "
216 			"Cannot find requester physical port\n");
217 		goto Exit;
218 	}
219 
220 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
221 		"Requester port GUID 0x%" PRIx64 "\n",
222 		cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
223 
224 	cl_qlist_init(&rec_list);
225 
226 	context.p_rcvd_rec = p_rcvd_rec;
227 	context.p_list = &rec_list;
228 	context.comp_mask = p_rcvd_mad->comp_mask;
229 	context.sa = sa;
230 	context.p_req_physp = p_req_physp;
231 
232 	/* Go over all switches */
233 	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl, lftr_rcv_by_comp_mask,
234 			   &context);
235 
236 	cl_plock_release(sa->p_lock);
237 
238 	osm_sa_respond(sa, p_madw, sizeof(ib_lft_record_t), &rec_list);
239 
240 Exit:
241 	OSM_LOG_EXIT(sa->p_log);
242 }
243