1/*
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.
5 *
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:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
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.
24 *
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
32 * SOFTWARE.
33 *
34 */
35
36/*
37 * Abstract:
38 *    Implementation of osm_lftr_rcv_t.
39 *   This object represents the LinearForwardingTable Receiver object.
40 *   This object is part of the opensm family of objects.
41 */
42
43#if HAVE_CONFIG_H
44#  include <config.h>
45#endif				/* HAVE_CONFIG_H */
46
47#include <string.h>
48#include <iba/ib_types.h>
49#include <complib/cl_debug.h>
50#include <complib/cl_qlist.h>
51#include <vendor/osm_vendor_api.h>
52#include <opensm/osm_switch.h>
53#include <opensm/osm_helper.h>
54#include <opensm/osm_pkey.h>
55#include <opensm/osm_sa.h>
56
57typedef struct osm_lftr_item {
58	cl_list_item_t list_item;
59	ib_lft_record_t rec;
60} osm_lftr_item_t;
61
62typedef 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/**********************************************************************
71 **********************************************************************/
72static ib_api_status_t
73__osm_lftr_rcv_new_lftr(IN osm_sa_t * sa,
74			IN const osm_switch_t * const p_sw,
75			IN cl_qlist_t * const p_list,
76			IN ib_net16_t const lid, IN uint16_t const block)
77{
78	osm_lftr_item_t *p_rec_item;
79	ib_api_status_t status = IB_SUCCESS;
80
81	OSM_LOG_ENTER(sa->p_log);
82
83	p_rec_item = malloc(sizeof(*p_rec_item));
84	if (p_rec_item == NULL) {
85		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4402: "
86			"rec_item alloc failed\n");
87		status = IB_INSUFFICIENT_RESOURCES;
88		goto Exit;
89	}
90
91	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
92		"New LinearForwardingTable: sw 0x%016" PRIx64
93		"\n\t\t\t\tblock 0x%02X lid %u\n",
94		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
95		block, cl_ntoh16(lid));
96
97	memset(p_rec_item, 0, sizeof(*p_rec_item));
98
99	p_rec_item->rec.lid = lid;
100	p_rec_item->rec.block_num = cl_hton16(block);
101
102	/* copy the lft block */
103	osm_switch_get_lft_block(p_sw, block, p_rec_item->rec.lft);
104
105	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
106
107Exit:
108	OSM_LOG_EXIT(sa->p_log);
109	return (status);
110}
111
112/**********************************************************************
113 **********************************************************************/
114static void
115__osm_lftr_rcv_by_comp_mask(IN cl_map_item_t * const p_map_item,
116			    IN void *context)
117{
118	const osm_lftr_search_ctxt_t *const p_ctxt =
119	    (osm_lftr_search_ctxt_t *) context;
120	const osm_switch_t *const p_sw = (osm_switch_t *) p_map_item;
121	const ib_lft_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
122	osm_sa_t *sa = p_ctxt->sa;
123	ib_net64_t const comp_mask = p_ctxt->comp_mask;
124	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
125	osm_port_t *p_port;
126	uint16_t min_lid_ho, max_lid_ho;
127	uint16_t min_block, max_block, block;
128	const osm_physp_t *p_physp;
129
130	/* In switches, the port guid is the node guid. */
131	p_port = osm_get_port_by_guid(sa->p_subn,
132				      p_sw->p_node->node_info.port_guid);
133	if (!p_port) {
134		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4405: "
135			"Failed to find Port by Node Guid:0x%016" PRIx64
136			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
137		return;
138	}
139
140	/* check that the requester physp and the current physp are under
141	   the same partition. */
142	p_physp = p_port->p_physp;
143	if (!p_physp) {
144		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4406: "
145			"Failed to find default physical Port by Node Guid:0x%016"
146			PRIx64 "\n",
147			cl_ntoh64(p_sw->p_node->node_info.node_guid));
148		return;
149	}
150	if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp))
151		return;
152
153	/* get the port 0 of the switch */
154	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
155
156	/* compare the lids - if required */
157	if (comp_mask & IB_LFTR_COMPMASK_LID) {
158		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
159			"Comparing lid:%u to port lid range: %u .. %u\n",
160			cl_ntoh16(p_rcvd_rec->lid), min_lid_ho, max_lid_ho);
161		/* ok we are ready for range check */
162		if (min_lid_ho > cl_ntoh16(p_rcvd_rec->lid) ||
163		    max_lid_ho < cl_ntoh16(p_rcvd_rec->lid))
164			return;
165	}
166
167	/* now we need to decide which blocks to output */
168	max_block = osm_switch_get_max_block_id_in_use(p_sw);
169	if (comp_mask & IB_LFTR_COMPMASK_BLOCK) {
170		min_block = cl_ntoh16(p_rcvd_rec->block_num);
171		if (min_block > max_block)
172			return;
173		max_block = min_block;
174	} else			/* use as many blocks as "in use" */
175		min_block = 0;
176
177	/* so we can add these blocks one by one ... */
178	for (block = min_block; block <= max_block; block++)
179		__osm_lftr_rcv_new_lftr(sa, p_sw, p_ctxt->p_list,
180					osm_port_get_base_lid(p_port), block);
181}
182
183/**********************************************************************
184 **********************************************************************/
185void osm_lftr_rcv_process(IN void *ctx, IN void *data)
186{
187	osm_sa_t *sa = ctx;
188	osm_madw_t *p_madw = data;
189	const ib_sa_mad_t *p_rcvd_mad;
190	const ib_lft_record_t *p_rcvd_rec;
191	cl_qlist_t rec_list;
192	osm_lftr_search_ctxt_t context;
193	osm_physp_t *p_req_physp;
194
195	CL_ASSERT(sa);
196
197	OSM_LOG_ENTER(sa->p_log);
198
199	CL_ASSERT(p_madw);
200
201	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
202	p_rcvd_rec = (ib_lft_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
203
204	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_LFT_RECORD);
205
206	/* we only support SubnAdmGet and SubnAdmGetTable methods */
207	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
208	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
209		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4408: "
210			"Unsupported Method (%s)\n",
211			ib_get_sa_method_str(p_rcvd_mad->method));
212		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
213		goto Exit;
214	}
215
216	/* update the requester physical port. */
217	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
218						osm_madw_get_mad_addr_ptr
219						(p_madw));
220	if (p_req_physp == NULL) {
221		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4407: "
222			"Cannot find requester physical port\n");
223		goto Exit;
224	}
225
226	cl_qlist_init(&rec_list);
227
228	context.p_rcvd_rec = p_rcvd_rec;
229	context.p_list = &rec_list;
230	context.comp_mask = p_rcvd_mad->comp_mask;
231	context.sa = sa;
232	context.p_req_physp = p_req_physp;
233
234	cl_plock_acquire(sa->p_lock);
235
236	/* Go over all switches */
237	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl,
238			   __osm_lftr_rcv_by_comp_mask, &context);
239
240	cl_plock_release(sa->p_lock);
241
242	osm_sa_respond(sa, p_madw, sizeof(ib_lft_record_t), &rec_list);
243
244Exit:
245	OSM_LOG_EXIT(sa->p_log);
246}
247