1/*
2 * Copyright (c) 2004-2007 Voltaire Inc.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#if HAVE_CONFIG_H
35#  include <config.h>
36#endif /* HAVE_CONFIG_H */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <string.h>
42#include <pthread.h>
43#include <sys/time.h>
44
45#include <infiniband/umad.h>
46#include "mad.h"
47
48#undef DEBUG
49#define DEBUG 	if (ibdebug)	IBWARN
50
51static uint8_t *
52pma_query_via(void *rcvbuf, ib_portid_t *dest, int port,
53	      unsigned timeout, unsigned id, const void *srcport)
54{
55	ib_rpc_t rpc = {0};
56	int lid = dest->lid;
57
58	DEBUG("lid %d port %d", lid, port);
59
60	if (lid == -1) {
61		IBWARN("only lid routed is supported");
62		return 0;
63	}
64
65	rpc.mgtclass = IB_PERFORMANCE_CLASS;
66	rpc.method = IB_MAD_METHOD_GET;
67	rpc.attr.id = id;
68
69	/* Same for attribute IDs */
70	mad_set_field(rcvbuf, 0, IB_PC_PORT_SELECT_F, port);
71	rpc.attr.mod = 0;
72	rpc.timeout = timeout;
73	rpc.datasz = IB_PC_DATA_SZ;
74	rpc.dataoffs = IB_PC_DATA_OFFS;
75
76	dest->qp = 1;
77	if (!dest->qkey)
78		dest->qkey = IB_DEFAULT_QP1_QKEY;
79
80	if (srcport) {
81		return mad_rpc(srcport, &rpc, dest, rcvbuf, rcvbuf);
82	} else {
83		return madrpc(&rpc, dest, rcvbuf, rcvbuf);
84	}
85}
86
87uint8_t *
88pma_query(void *rcvbuf, ib_portid_t *dest, int port, unsigned timeout, unsigned id)
89{
90	return pma_query_via(rcvbuf, dest, port, timeout, id, NULL);
91}
92
93uint8_t *
94perf_classportinfo_query_via(void *rcvbuf, ib_portid_t *dest, int port,
95			     unsigned timeout, const void *srcport)
96{
97	return pma_query_via(rcvbuf, dest, port, timeout, CLASS_PORT_INFO,
98			     srcport);
99}
100
101uint8_t *
102perf_classportinfo_query(void *rcvbuf, ib_portid_t *dest, int port, unsigned timeout)
103{
104	return pma_query(rcvbuf, dest, port, timeout, CLASS_PORT_INFO);
105}
106
107uint8_t *
108port_performance_query_via(void *rcvbuf, ib_portid_t *dest, int port,
109			   unsigned timeout, const void *srcport)
110{
111	return pma_query_via(rcvbuf, dest, port, timeout,
112			     IB_GSI_PORT_COUNTERS, srcport);
113}
114
115uint8_t *
116port_performance_query(void *rcvbuf, ib_portid_t *dest, int port, unsigned timeout)
117{
118	return pma_query(rcvbuf, dest, port, timeout, IB_GSI_PORT_COUNTERS);
119}
120
121static uint8_t *
122performance_reset_via(void *rcvbuf, ib_portid_t *dest, int port, unsigned mask,
123		      unsigned timeout, unsigned id, const void *srcport)
124{
125	ib_rpc_t rpc = {0};
126	int lid = dest->lid;
127
128	DEBUG("lid %d port %d mask 0x%x", lid, port, mask);
129
130	if (lid == -1) {
131		IBWARN("only lid routed is supported");
132		return 0;
133	}
134
135	if (!mask)
136		mask = ~0;
137
138	rpc.mgtclass = IB_PERFORMANCE_CLASS;
139	rpc.method = IB_MAD_METHOD_SET;
140	rpc.attr.id = id;
141
142	memset(rcvbuf, 0, IB_MAD_SIZE);
143
144	/* Same for attribute IDs */
145	mad_set_field(rcvbuf, 0, IB_PC_PORT_SELECT_F, port);
146	mad_set_field(rcvbuf, 0, IB_PC_COUNTER_SELECT_F, mask);
147	rpc.attr.mod = 0;
148	rpc.timeout = timeout;
149	rpc.datasz = IB_PC_DATA_SZ;
150	rpc.dataoffs = IB_PC_DATA_OFFS;
151	dest->qp = 1;
152	if (!dest->qkey)
153		dest->qkey = IB_DEFAULT_QP1_QKEY;
154
155	if (srcport) {
156		return mad_rpc(srcport, &rpc, dest, rcvbuf, rcvbuf);
157	} else {
158		return madrpc(&rpc, dest, rcvbuf, rcvbuf);
159	}
160}
161
162static uint8_t *
163performance_reset(void *rcvbuf, ib_portid_t *dest, int port, unsigned mask,
164		  unsigned timeout, unsigned id)
165{
166	return performance_reset_via(rcvbuf, dest, port, mask, timeout,
167				     id, NULL);
168}
169
170uint8_t *
171port_performance_reset_via(void *rcvbuf, ib_portid_t *dest, int port,
172			   unsigned mask, unsigned timeout, const void *srcport)
173{
174	return performance_reset_via(rcvbuf, dest, port, mask, timeout,
175				     IB_GSI_PORT_COUNTERS, srcport);
176}
177
178uint8_t *
179port_performance_reset(void *rcvbuf, ib_portid_t *dest, int port, unsigned mask,
180		       unsigned timeout)
181{
182	return performance_reset(rcvbuf, dest, port, mask, timeout, IB_GSI_PORT_COUNTERS);
183}
184
185uint8_t *
186port_performance_ext_query_via(void *rcvbuf, ib_portid_t *dest, int port,
187			       unsigned timeout, const void *srcport)
188{
189	return pma_query_via(rcvbuf, dest, port, timeout,
190			     IB_GSI_PORT_COUNTERS_EXT, srcport);
191}
192
193uint8_t *
194port_performance_ext_query(void *rcvbuf, ib_portid_t *dest, int port, unsigned timeout)
195{
196	return pma_query(rcvbuf, dest, port, timeout, IB_GSI_PORT_COUNTERS_EXT);
197}
198
199uint8_t *
200port_performance_ext_reset_via(void *rcvbuf, ib_portid_t *dest, int port,
201			       unsigned mask, unsigned timeout,
202			       const void *srcport)
203{
204	return performance_reset_via(rcvbuf, dest, port, mask, timeout,
205				     IB_GSI_PORT_COUNTERS_EXT, srcport);
206}
207
208uint8_t *
209port_performance_ext_reset(void *rcvbuf, ib_portid_t *dest, int port, unsigned mask,
210			   unsigned timeout)
211{
212	return performance_reset(rcvbuf, dest, port, mask, timeout, IB_GSI_PORT_COUNTERS_EXT);
213}
214
215uint8_t *
216port_samples_control_query_via(void *rcvbuf, ib_portid_t *dest, int port,
217			       unsigned timeout, const void *srcport)
218{
219	return pma_query_via(rcvbuf, dest, port, timeout,
220			     IB_GSI_PORT_SAMPLES_CONTROL, srcport);
221}
222
223uint8_t *
224port_samples_control_query(void *rcvbuf, ib_portid_t *dest, int port, unsigned timeout)
225{
226	return pma_query(rcvbuf, dest, port, timeout, IB_GSI_PORT_SAMPLES_CONTROL);
227}
228
229uint8_t *
230port_samples_result_query_via(void *rcvbuf, ib_portid_t *dest, int port,
231			      unsigned timeout, const void *srcport)
232{
233	return pma_query_via(rcvbuf, dest, port, timeout,
234			     IB_GSI_PORT_SAMPLES_RESULT, srcport);
235}
236
237uint8_t *
238port_samples_result_query(void *rcvbuf, ib_portid_t *dest, int port,  unsigned timeout)
239{
240	return pma_query(rcvbuf, dest, port, timeout, IB_GSI_PORT_SAMPLES_RESULT);
241}
242