1321936Shselasky/*
2321936Shselasky * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3321936Shselasky * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
4321936Shselasky *
5321936Shselasky * This software is available to you under a choice of one of two
6321936Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
7321936Shselasky * General Public License (GPL) Version 2, available from the file
8321936Shselasky * COPYING in the main directory of this source tree, or the
9321936Shselasky * OpenIB.org BSD license below:
10321936Shselasky *
11321936Shselasky *     Redistribution and use in source and binary forms, with or
12321936Shselasky *     without modification, are permitted provided that the following
13321936Shselasky *     conditions are met:
14321936Shselasky *
15321936Shselasky *      - Redistributions of source code must retain the above
16321936Shselasky *        copyright notice, this list of conditions and the following
17321936Shselasky *        disclaimer.
18321936Shselasky *
19321936Shselasky *      - Redistributions in binary form must reproduce the above
20321936Shselasky *        copyright notice, this list of conditions and the following
21321936Shselasky *        disclaimer in the documentation and/or other materials
22321936Shselasky *        provided with the distribution.
23321936Shselasky *
24321936Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25321936Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26321936Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27321936Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28321936Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29321936Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30321936Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31321936Shselasky * SOFTWARE.
32321936Shselasky *
33321936Shselasky */
34321936Shselasky
35321936Shselasky#if HAVE_CONFIG_H
36321936Shselasky#  include <config.h>
37321936Shselasky#endif				/* HAVE_CONFIG_H */
38321936Shselasky
39321936Shselasky#include <stdio.h>
40321936Shselasky#include <stdlib.h>
41321936Shselasky#include <string.h>
42321936Shselasky#include <errno.h>
43321936Shselasky
44321936Shselasky#include <infiniband/umad.h>
45321936Shselasky#include <infiniband/mad.h>
46321936Shselasky
47321936Shselasky#undef DEBUG
48321936Shselasky#define DEBUG 	if (ibdebug)	IBWARN
49321936Shselasky
50321936Shselaskyuint8_t *pma_query_via(void *rcvbuf, ib_portid_t * dest, int port,
51321936Shselasky		       unsigned timeout, unsigned id,
52321936Shselasky		       const struct ibmad_port * srcport)
53321936Shselasky{
54321936Shselasky	ib_rpc_v1_t rpc = { 0 };
55321936Shselasky	ib_rpc_t *rpcold = (ib_rpc_t *)(void *)&rpc;
56321936Shselasky	int lid = dest->lid;
57321936Shselasky	void *p_ret;
58321936Shselasky
59321936Shselasky	DEBUG("lid %u port %d", lid, port);
60321936Shselasky
61321936Shselasky	if (lid == -1) {
62321936Shselasky		IBWARN("only lid routed is supported");
63321936Shselasky		return NULL;
64321936Shselasky	}
65321936Shselasky
66321936Shselasky	rpc.mgtclass = IB_PERFORMANCE_CLASS | IB_MAD_RPC_VERSION1;
67321936Shselasky	rpc.method = IB_MAD_METHOD_GET;
68321936Shselasky	rpc.attr.id = id;
69321936Shselasky
70321936Shselasky	/* Same for attribute IDs */
71321936Shselasky	mad_set_field(rcvbuf, 0, IB_PC_PORT_SELECT_F, port);
72321936Shselasky	rpc.attr.mod = 0;
73321936Shselasky	rpc.timeout = timeout;
74321936Shselasky	rpc.datasz = IB_PC_DATA_SZ;
75321936Shselasky	rpc.dataoffs = IB_PC_DATA_OFFS;
76321936Shselasky
77321936Shselasky	if (!dest->qp)
78321936Shselasky		dest->qp = 1;
79321936Shselasky	if (!dest->qkey)
80321936Shselasky		dest->qkey = IB_DEFAULT_QP1_QKEY;
81321936Shselasky
82321936Shselasky	p_ret = mad_rpc(srcport, rpcold, dest, rcvbuf, rcvbuf);
83321936Shselasky	errno = rpc.error;
84321936Shselasky	return p_ret;
85321936Shselasky}
86321936Shselasky
87321936Shselaskyuint8_t *performance_reset_via(void *rcvbuf, ib_portid_t * dest,
88321936Shselasky			       int port, unsigned mask, unsigned timeout,
89321936Shselasky			       unsigned id, const struct ibmad_port * srcport)
90321936Shselasky{
91321936Shselasky	ib_rpc_v1_t rpc = { 0 };
92321936Shselasky	ib_rpc_t *rpcold = (ib_rpc_t *)(void *)&rpc;
93321936Shselasky
94321936Shselasky	int lid = dest->lid;
95321936Shselasky	void *p_ret;
96321936Shselasky
97321936Shselasky	DEBUG("lid %u port %d mask 0x%x", lid, port, mask);
98321936Shselasky
99321936Shselasky	if (lid == -1) {
100321936Shselasky		IBWARN("only lid routed is supported");
101321936Shselasky		return NULL;
102321936Shselasky	}
103321936Shselasky
104321936Shselasky	if (!mask)
105321936Shselasky		mask = ~0;
106321936Shselasky
107321936Shselasky	rpc.mgtclass = IB_PERFORMANCE_CLASS | IB_MAD_RPC_VERSION1;
108321936Shselasky	rpc.method = IB_MAD_METHOD_SET;
109321936Shselasky	rpc.attr.id = id;
110321936Shselasky
111321936Shselasky	memset(rcvbuf, 0, IB_MAD_SIZE);
112321936Shselasky
113321936Shselasky	/* Next 2 lines - same for attribute IDs */
114321936Shselasky	mad_set_field(rcvbuf, 0, IB_PC_PORT_SELECT_F, port);
115321936Shselasky	mad_set_field(rcvbuf, 0, IB_PC_COUNTER_SELECT_F, mask);
116321936Shselasky	mask = mask >> 16;
117321936Shselasky	if (id == IB_GSI_PORT_COUNTERS_EXT)
118321936Shselasky		mad_set_field(rcvbuf, 0, IB_PC_EXT_COUNTER_SELECT2_F, mask);
119321936Shselasky	else
120321936Shselasky		mad_set_field(rcvbuf, 0, IB_PC_COUNTER_SELECT2_F, mask);
121321936Shselasky	rpc.attr.mod = 0;
122321936Shselasky	rpc.timeout = timeout;
123321936Shselasky	rpc.datasz = IB_PC_DATA_SZ;
124321936Shselasky	rpc.dataoffs = IB_PC_DATA_OFFS;
125321936Shselasky	if (!dest->qp)
126321936Shselasky		dest->qp = 1;
127321936Shselasky	if (!dest->qkey)
128321936Shselasky		dest->qkey = IB_DEFAULT_QP1_QKEY;
129321936Shselasky
130321936Shselasky	p_ret = mad_rpc(srcport, rpcold, dest, rcvbuf, rcvbuf);
131321936Shselasky	errno = rpc.error;
132321936Shselasky	return p_ret;
133321936Shselasky}
134