1/*
2 * Copyright (c) 2004, 2005 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#ifndef _OSMV_SVC_H_
37#define _OSMV_SVC_H_
38
39#include <stdlib.h>
40#include <string.h>
41#include <iba/ib_types.h>
42#include <vendor/osm_vendor_mlx_defs.h>
43
44#ifdef __cplusplus
45#  define BEGIN_C_DECLS extern "C" {
46#  define END_C_DECLS   }
47#else				/* !__cplusplus */
48#  define BEGIN_C_DECLS
49#  define END_C_DECLS
50#endif				/* __cplusplus */
51
52BEGIN_C_DECLS
53inline static uint8_t osmv_invert_method(IN uint8_t req_method)
54{
55	switch (req_method) {
56	case IB_MAD_METHOD_GET_RESP:
57		/* Not a 1-1 mapping! */
58		return IB_MAD_METHOD_GET;
59
60	case IB_MAD_METHOD_GET:
61		return IB_MAD_METHOD_GET_RESP;
62
63	case IB_MAD_METHOD_SET:
64		return IB_MAD_METHOD_GET_RESP;
65
66	case IB_MAD_METHOD_GETTABLE_RESP:
67		return IB_MAD_METHOD_GETTABLE;
68
69	case IB_MAD_METHOD_GETTABLE:
70		return IB_MAD_METHOD_GETTABLE_RESP;
71
72	case IB_MAD_METHOD_GETMULTI_RESP:
73		/* Not a 1-1 mapping! */
74		return IB_MAD_METHOD_GETMULTI;
75
76	case IB_MAD_METHOD_GETTRACETABLE:
77	case IB_MAD_METHOD_GETMULTI:
78		return IB_MAD_METHOD_GETMULTI_RESP;
79
80	case IB_MAD_METHOD_TRAP:
81		return IB_MAD_METHOD_TRAP_REPRESS;
82
83	case IB_MAD_METHOD_TRAP_REPRESS:
84		return IB_MAD_METHOD_TRAP;
85
86	case IB_MAD_METHOD_REPORT:
87		return IB_MAD_METHOD_REPORT_RESP;
88
89	case IB_MAD_METHOD_REPORT_RESP:
90		return IB_MAD_METHOD_REPORT;
91
92		/*  IB_MAD_METHOD_SEND does not have a response */
93	case IB_MAD_METHOD_SEND:
94		return IB_MAD_METHOD_SEND;
95
96	default:
97		CL_ASSERT(FALSE);
98	}
99
100	return 0;		/* Just make the compiler happy */
101}
102
103inline static boolean_t osmv_mad_is_rmpp(IN const ib_mad_t * p_mad)
104{
105	uint8_t rmpp_flags;
106	CL_ASSERT(NULL != p_mad);
107
108	rmpp_flags = ((ib_rmpp_mad_t *) p_mad)->rmpp_flags;
109	/* HACK - JUST SA and DevMgt for now - need to add BIS and DevAdm */
110	if ((p_mad->mgmt_class != IB_MCLASS_SUBN_ADM) &&
111	    (p_mad->mgmt_class != IB_MCLASS_DEV_MGMT))
112		return (0);
113	return (0 != (rmpp_flags & IB_RMPP_FLAG_ACTIVE));
114}
115
116inline static boolean_t osmv_mad_is_multi_resp(IN const ib_mad_t * p_mad)
117{
118	CL_ASSERT(NULL != p_mad);
119	return (IB_MAD_METHOD_GETMULTI == p_mad->method
120		|| IB_MAD_METHOD_GETTRACETABLE == p_mad->method);
121}
122
123inline static boolean_t osmv_mad_is_sa(IN const ib_mad_t * p_mad)
124{
125	CL_ASSERT(NULL != p_mad);
126	return (IB_MCLASS_SUBN_ADM == p_mad->mgmt_class);
127}
128
129inline static boolean_t osmv_rmpp_is_abort_stop(IN const ib_mad_t * p_mad)
130{
131	uint8_t rmpp_type;
132	CL_ASSERT(p_mad);
133
134	rmpp_type = ((ib_rmpp_mad_t *) p_mad)->rmpp_type;
135	return (IB_RMPP_TYPE_STOP == rmpp_type
136		|| IB_RMPP_TYPE_ABORT == rmpp_type);
137}
138
139inline static boolean_t osmv_rmpp_is_data(IN const ib_mad_t * p_mad)
140{
141	CL_ASSERT(p_mad);
142	return (IB_RMPP_TYPE_DATA == ((ib_rmpp_mad_t *) p_mad)->rmpp_type);
143}
144
145inline static boolean_t osmv_rmpp_is_ack(IN const ib_mad_t * p_mad)
146{
147	CL_ASSERT(p_mad);
148	return (IB_RMPP_TYPE_ACK == ((ib_rmpp_mad_t *) p_mad)->rmpp_type);
149}
150
151inline static boolean_t osmv_rmpp_is_first(IN const ib_mad_t * p_mad)
152{
153	uint8_t rmpp_flags;
154	CL_ASSERT(NULL != p_mad);
155
156	rmpp_flags = ((ib_rmpp_mad_t *) p_mad)->rmpp_flags;
157	return (0 != (IB_RMPP_FLAG_FIRST & rmpp_flags));
158}
159
160inline static boolean_t osmv_rmpp_is_last(IN const ib_mad_t * p_mad)
161{
162	uint8_t rmpp_flags;
163	CL_ASSERT(NULL != p_mad);
164
165	rmpp_flags = ((ib_rmpp_mad_t *) p_mad)->rmpp_flags;
166	return (0 != (IB_RMPP_FLAG_LAST & rmpp_flags));
167}
168
169inline static uint8_t *osmv_mad_copy(IN const ib_mad_t * p_mad)
170{
171	uint8_t *p_copy;
172
173	CL_ASSERT(p_mad);
174	p_copy = malloc(MAD_BLOCK_SIZE);
175
176	if (NULL != p_copy) {
177		memset(p_copy, 0, MAD_BLOCK_SIZE);
178		memcpy(p_copy, p_mad, MAD_BLOCK_SIZE);
179	}
180
181	return p_copy;
182}
183
184/* Should be passed externally from the Makefile */
185/*  #define OSMV_RANDOM_DROP 1 */
186#define OSMV_DROP_RATE   0.3
187
188inline static boolean_t osmv_random_drop(void)
189{
190	srand(1);		/* Pick a new base */
191	return (rand() / (double)RAND_MAX < OSMV_DROP_RATE);
192}
193
194END_C_DECLS
195#endif				/* _OSMV_SVC_H_ */
196