2 * Copyright (c) 2004-2008 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.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #endif /* HAVE_CONFIG_H */
41 #include <iba/ib_types.h>
42 #include <complib/cl_qmap.h>
43 #include <complib/cl_passivelock.h>
44 #include <complib/cl_debug.h>
45 #include <complib/cl_qlist.h>
46 #include <vendor/osm_vendor_api.h>
47 #include <opensm/osm_port.h>
48 #include <opensm/osm_node.h>
49 #include <opensm/osm_helper.h>
50 #include <opensm/osm_pkey.h>
51 #include <opensm/osm_sa.h>
53 typedef struct osm_pkey_item {
54 cl_list_item_t list_item;
55 ib_pkey_table_record_t rec;
58 typedef struct osm_pkey_search_ctxt {
59 const ib_pkey_table_record_t *p_rcvd_rec;
64 const osm_physp_t *p_req_physp;
65 } osm_pkey_search_ctxt_t;
67 /**********************************************************************
68 **********************************************************************/
70 __osm_sa_pkey_create(IN osm_sa_t * sa,
71 IN osm_physp_t * const p_physp,
72 IN osm_pkey_search_ctxt_t * const p_ctxt,
75 osm_pkey_item_t *p_rec_item;
77 ib_api_status_t status = IB_SUCCESS;
79 OSM_LOG_ENTER(sa->p_log);
81 p_rec_item = malloc(sizeof(*p_rec_item));
82 if (p_rec_item == NULL) {
83 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4602: "
84 "rec_item alloc failed\n");
85 status = IB_INSUFFICIENT_RESOURCES;
89 if (p_physp->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH)
90 lid = p_physp->port_info.base_lid;
92 lid = osm_node_get_base_lid(p_physp->p_node, 0);
94 OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
95 "New P_Key table for: port 0x%016" PRIx64
96 ", lid %u, port %u Block:%u\n",
97 cl_ntoh64(osm_physp_get_port_guid(p_physp)),
98 cl_ntoh16(lid), osm_physp_get_port_num(p_physp), block);
100 memset(p_rec_item, 0, sizeof(*p_rec_item));
102 p_rec_item->rec.lid = lid;
103 p_rec_item->rec.block_num = block;
104 p_rec_item->rec.port_num = osm_physp_get_port_num(p_physp);
105 p_rec_item->rec.pkey_tbl =
106 *(osm_pkey_tbl_block_get(osm_physp_get_pkey_tbl(p_physp), block));
108 cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
111 OSM_LOG_EXIT(sa->p_log);
114 /**********************************************************************
115 **********************************************************************/
117 __osm_sa_pkey_check_physp(IN osm_sa_t * sa,
118 IN osm_physp_t * const p_physp,
119 osm_pkey_search_ctxt_t * const p_ctxt)
121 ib_net64_t comp_mask = p_ctxt->comp_mask;
122 uint16_t block, num_blocks;
124 OSM_LOG_ENTER(sa->p_log);
126 /* we got here with the phys port - all is left is to get the right block */
127 if (comp_mask & IB_PKEY_COMPMASK_BLOCK) {
128 __osm_sa_pkey_create(sa, p_physp, p_ctxt, p_ctxt->block_num);
131 osm_pkey_tbl_get_num_blocks(osm_physp_get_pkey_tbl
133 for (block = 0; block < num_blocks; block++) {
134 __osm_sa_pkey_create(sa, p_physp, p_ctxt, block);
138 OSM_LOG_EXIT(sa->p_log);
141 /**********************************************************************
142 **********************************************************************/
144 __osm_sa_pkey_by_comp_mask(IN osm_sa_t * sa,
145 IN const osm_port_t * const p_port,
146 osm_pkey_search_ctxt_t * const p_ctxt)
148 const ib_pkey_table_record_t *p_rcvd_rec;
149 ib_net64_t comp_mask;
150 osm_physp_t *p_physp;
153 const osm_physp_t *p_req_physp;
155 OSM_LOG_ENTER(sa->p_log);
157 p_rcvd_rec = p_ctxt->p_rcvd_rec;
158 comp_mask = p_ctxt->comp_mask;
159 port_num = p_rcvd_rec->port_num;
160 p_req_physp = p_ctxt->p_req_physp;
162 /* if this is a switch port we can search all ports
163 otherwise we must be looking on port 0 */
164 if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
165 /* we put it in the comp mask and port num */
166 port_num = p_port->p_physp->port_num;
167 OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
168 "Using Physical Default Port Number: 0x%X (for End Node)\n",
170 comp_mask |= IB_PKEY_COMPMASK_PORT;
173 if (comp_mask & IB_PKEY_COMPMASK_PORT) {
174 if (port_num < osm_node_get_num_physp(p_port->p_node)) {
176 osm_node_get_physp_ptr(p_port->p_node, port_num);
177 /* Check that the p_physp is valid, and that is shares a pkey
178 with the p_req_physp. */
180 (osm_physp_share_pkey
181 (sa->p_log, p_req_physp, p_physp)))
182 __osm_sa_pkey_check_physp(sa, p_physp,
185 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4603: "
186 "Given Physical Port Number: 0x%X is out of range should be < 0x%X\n",
188 osm_node_get_num_physp(p_port->p_node));
192 num_ports = osm_node_get_num_physp(p_port->p_node);
193 for (port_num = 0; port_num < num_ports; port_num++) {
195 osm_node_get_physp_ptr(p_port->p_node, port_num);
199 /* if the requester and the p_physp don't share a pkey -
201 if (!osm_physp_share_pkey
202 (sa->p_log, p_req_physp, p_physp))
205 __osm_sa_pkey_check_physp(sa, p_physp, p_ctxt);
209 OSM_LOG_EXIT(sa->p_log);
212 /**********************************************************************
213 **********************************************************************/
215 __osm_sa_pkey_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
218 const osm_port_t *const p_port = (osm_port_t *) p_map_item;
219 osm_pkey_search_ctxt_t *const p_ctxt =
220 (osm_pkey_search_ctxt_t *) context;
222 __osm_sa_pkey_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
225 /**********************************************************************
226 **********************************************************************/
227 void osm_pkey_rec_rcv_process(IN void *ctx, IN void *data)
230 osm_madw_t *p_madw = data;
231 const ib_sa_mad_t *p_rcvd_mad;
232 const ib_pkey_table_record_t *p_rcvd_rec;
233 const osm_port_t *p_port = NULL;
234 const ib_pkey_table_t *p_pkey;
236 osm_pkey_search_ctxt_t context;
237 ib_api_status_t status = IB_SUCCESS;
238 ib_net64_t comp_mask;
239 osm_physp_t *p_req_physp;
243 OSM_LOG_ENTER(sa->p_log);
247 p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
249 (ib_pkey_table_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
250 comp_mask = p_rcvd_mad->comp_mask;
252 CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_PKEY_TBL_RECORD);
254 /* we only support SubnAdmGet and SubnAdmGetTable methods */
255 if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
256 p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
257 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4605: "
258 "Unsupported Method (%s)\n",
259 ib_get_sa_method_str(p_rcvd_mad->method));
260 osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
265 p922 - P_KeyTableRecords shall only be provided in response
267 Check that the requester is a trusted one.
269 if (p_rcvd_mad->sm_key != sa->p_subn->opt.sa_key) {
270 /* This is not a trusted requester! */
271 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4608: "
272 "Request from non-trusted requester: "
273 "Given SM_Key:0x%016" PRIx64 "\n",
274 cl_ntoh64(p_rcvd_mad->sm_key));
275 osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_REQ_INVALID);
279 /* update the requester physical port. */
280 p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
281 osm_madw_get_mad_addr_ptr
283 if (p_req_physp == NULL) {
284 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4604: "
285 "Cannot find requester physical port\n");
289 p_pkey = (ib_pkey_table_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
291 cl_qlist_init(&rec_list);
293 context.p_rcvd_rec = p_rcvd_rec;
294 context.p_list = &rec_list;
295 context.comp_mask = p_rcvd_mad->comp_mask;
297 context.block_num = p_rcvd_rec->block_num;
298 context.p_req_physp = p_req_physp;
300 OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
301 "Got Query Lid:%u(%02X), Block:0x%02X(%02X), Port:0x%02X(%02X)\n",
302 cl_ntoh16(p_rcvd_rec->lid),
303 (comp_mask & IB_PKEY_COMPMASK_LID) != 0, p_rcvd_rec->port_num,
304 (comp_mask & IB_PKEY_COMPMASK_PORT) != 0, p_rcvd_rec->block_num,
305 (comp_mask & IB_PKEY_COMPMASK_BLOCK) != 0);
307 cl_plock_acquire(sa->p_lock);
310 If the user specified a LID, it obviously narrows our
311 work load, since we don't have to search every port
313 if (comp_mask & IB_PKEY_COMPMASK_LID) {
314 status = osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
316 if (status != IB_SUCCESS || p_port == NULL) {
317 status = IB_NOT_FOUND;
318 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 460B: "
319 "No port found with LID %u\n",
320 cl_ntoh16(p_rcvd_rec->lid));
324 if (status == IB_SUCCESS) {
325 /* if we got a unique port - no need for a port search */
327 /* this does the loop on all the port phys ports */
328 __osm_sa_pkey_by_comp_mask(sa, p_port, &context);
330 cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
331 __osm_sa_pkey_by_comp_mask_cb,
335 cl_plock_release(sa->p_lock);
337 osm_sa_respond(sa, p_madw, sizeof(ib_pkey_table_record_t), &rec_list);
340 OSM_LOG_EXIT(sa->p_log);