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_cpi_rcv_t.
39 * This object represents the ClassPortInfo 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_qmap.h>
50#include <complib/cl_passivelock.h>
51#include <complib/cl_debug.h>
52#include <complib/cl_qlist.h>
53#include <vendor/osm_vendor_api.h>
54#include <opensm/osm_helper.h>
55#include <opensm/osm_sa.h>
56
57#define MAX_MSECS_TO_RTV 24
58/* Precalculated table in msec (index is related to encoded value) */
59/* 4.096 usec * 2 ** n (where n = 8 - 31) */
60const static uint32_t __msecs_to_rtv_table[MAX_MSECS_TO_RTV] = {
61	1, 2, 4, 8,
62	16, 33, 67, 134,
63	268, 536, 1073, 2147,
64	4294, 8589, 17179, 34359,
65	68719, 137438, 274877, 549755,
66	1099511, 2199023, 4398046, 8796093
67};
68
69/**********************************************************************
70 **********************************************************************/
71static void
72__osm_cpi_rcv_respond(IN osm_sa_t * sa,
73		      IN const osm_madw_t * const p_madw)
74{
75	osm_madw_t *p_resp_madw;
76	const ib_sa_mad_t *p_sa_mad;
77	ib_sa_mad_t *p_resp_sa_mad;
78	ib_class_port_info_t *p_resp_cpi;
79	ib_gid_t zero_gid;
80	uint8_t rtv;
81
82	OSM_LOG_ENTER(sa->p_log);
83
84	memset(&zero_gid, 0, sizeof(ib_gid_t));
85
86	/*
87	   Get a MAD to reply. Address of Mad is in the received mad_wrapper
88	 */
89	p_resp_madw = osm_mad_pool_get(sa->p_mad_pool, p_madw->h_bind,
90				       MAD_BLOCK_SIZE, &p_madw->mad_addr);
91	if (!p_resp_madw) {
92		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1408: "
93			"Unable to allocate MAD\n");
94		goto Exit;
95	}
96
97	p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
98	p_resp_sa_mad = osm_madw_get_sa_mad_ptr(p_resp_madw);
99
100	memcpy(p_resp_sa_mad, p_sa_mad, IB_SA_MAD_HDR_SIZE);
101	p_resp_sa_mad->method |= IB_MAD_METHOD_RESP_MASK;
102	/* C15-0.1.5 - always return SM_Key = 0 (table 185 p 884) */
103	p_resp_sa_mad->sm_key = 0;
104
105	p_resp_cpi =
106	    (ib_class_port_info_t *) ib_sa_mad_get_payload_ptr(p_resp_sa_mad);
107
108	/* finally do it (the job) man ! */
109	p_resp_cpi->base_ver = 1;
110	p_resp_cpi->class_ver = 2;
111	/* Calculate encoded response time value */
112	/* transaction timeout is in msec */
113	if (sa->p_subn->opt.transaction_timeout >
114	    __msecs_to_rtv_table[MAX_MSECS_TO_RTV - 1])
115		rtv = MAX_MSECS_TO_RTV - 1;
116	else {
117		for (rtv = 0; rtv < MAX_MSECS_TO_RTV; rtv++) {
118			if (sa->p_subn->opt.transaction_timeout <=
119			    __msecs_to_rtv_table[rtv])
120				break;
121		}
122	}
123	rtv += 8;
124	ib_class_set_resp_time_val(p_resp_cpi, rtv);
125	p_resp_cpi->redir_gid = zero_gid;
126	p_resp_cpi->redir_tc_sl_fl = 0;
127	p_resp_cpi->redir_lid = 0;
128	p_resp_cpi->redir_pkey = 0;
129	p_resp_cpi->redir_qp = CL_NTOH32(1);
130	p_resp_cpi->redir_qkey = IB_QP1_WELL_KNOWN_Q_KEY;
131	p_resp_cpi->trap_gid = zero_gid;
132	p_resp_cpi->trap_tc_sl_fl = 0;
133	p_resp_cpi->trap_lid = 0;
134	p_resp_cpi->trap_pkey = 0;
135	p_resp_cpi->trap_hop_qp = 0;
136	p_resp_cpi->trap_qkey = IB_QP1_WELL_KNOWN_Q_KEY;
137
138	/* set specific capability mask bits */
139	/* we do not support the following options/optional records:
140	   OSM_CAP_IS_SUBN_OPT_RECS_SUP :
141	   RandomForwardingTableRecord,
142	   ServiceAssociationRecord
143	   other optional records supported "under the table"
144
145	   OSM_CAP_IS_MULTIPATH_SUP:
146	   TraceRecord
147
148	   OSM_CAP_IS_REINIT_SUP:
149	   For reinitialization functionality.
150
151	   So not sending traps, but supporting Get(Notice) and Set(Notice).
152	 */
153
154	/* Note host notation replaced later */
155#if defined (VENDOR_RMPP_SUPPORT) && defined (DUAL_SIDED_RMPP)
156	p_resp_cpi->cap_mask = OSM_CAP_IS_SUBN_GET_SET_NOTICE_SUP |
157	    OSM_CAP_IS_PORT_INFO_CAPMASK_MATCH_SUPPORTED |
158	    OSM_CAP_IS_MULTIPATH_SUP;
159#else
160	p_resp_cpi->cap_mask = OSM_CAP_IS_SUBN_GET_SET_NOTICE_SUP |
161	    OSM_CAP_IS_PORT_INFO_CAPMASK_MATCH_SUPPORTED;
162#endif
163	if (sa->p_subn->opt.qos)
164		ib_class_set_cap_mask2(p_resp_cpi, OSM_CAP2_IS_QOS_SUPPORTED);
165
166	if (!sa->p_subn->opt.disable_multicast)
167		p_resp_cpi->cap_mask |= OSM_CAP_IS_UD_MCAST_SUP;
168	p_resp_cpi->cap_mask = cl_hton16(p_resp_cpi->cap_mask);
169
170	if (osm_log_is_active(sa->p_log, OSM_LOG_FRAMES))
171		osm_dump_sa_mad(sa->p_log, p_resp_sa_mad, OSM_LOG_FRAMES);
172
173	osm_sa_send(sa, p_resp_madw, FALSE);
174
175Exit:
176	OSM_LOG_EXIT(sa->p_log);
177}
178
179/**********************************************************************
180 * This code actually handles the call
181 **********************************************************************/
182void osm_cpi_rcv_process(IN void *context, IN void *data)
183{
184	osm_sa_t *sa = context;
185	osm_madw_t *p_madw = data;
186	const ib_sa_mad_t *p_sa_mad;
187
188	OSM_LOG_ENTER(sa->p_log);
189
190	CL_ASSERT(p_madw);
191
192	p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
193
194	/* we only support GET */
195	if (p_sa_mad->method != IB_MAD_METHOD_GET) {
196		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1403: "
197			"Unsupported Method (%s)\n",
198			ib_get_sa_method_str(p_sa_mad->method));
199		osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_REQ_INVALID);
200		goto Exit;
201	}
202
203	CL_ASSERT(p_sa_mad->attr_id == IB_MAD_ATTR_CLASS_PORT_INFO);
204
205	/*
206	   CLASS PORT INFO does not really look on the SMDB - no lock required.
207	 */
208
209	__osm_cpi_rcv_respond(sa, p_madw);
210
211Exit:
212	OSM_LOG_EXIT(sa->p_log);
213}
214