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
43321936Shselasky#include <infiniband/umad.h>
44321936Shselasky#include <infiniband/mad.h>
45321936Shselasky#include "mad_internal.h"
46321936Shselasky
47321936Shselasky#undef DEBUG
48321936Shselasky#define DEBUG 	if (ibdebug)	IBWARN
49321936Shselasky
50321936Shselaskyvoid smp_mkey_set(struct ibmad_port *srcport, uint64_t mkey)
51321936Shselasky{
52321936Shselasky	srcport->smp_mkey = mkey;
53321936Shselasky}
54321936Shselasky
55321936Shselaskyuint64_t smp_mkey_get(const struct ibmad_port *srcport)
56321936Shselasky{
57321936Shselasky	return srcport->smp_mkey;
58321936Shselasky}
59321936Shselasky
60321936Shselaskyuint8_t *smp_set_status_via(void *data, ib_portid_t * portid, unsigned attrid,
61321936Shselasky			    unsigned mod, unsigned timeout, int *rstatus,
62321936Shselasky			    const struct ibmad_port *srcport)
63321936Shselasky{
64321936Shselasky	ib_rpc_t rpc = { 0 };
65321936Shselasky	uint8_t *res;
66321936Shselasky
67321936Shselasky	DEBUG("attr 0x%x mod 0x%x route %s", attrid, mod, portid2str(portid));
68321936Shselasky	if ((portid->lid <= 0) ||
69321936Shselasky	    (portid->drpath.drslid == 0xffff) ||
70321936Shselasky	    (portid->drpath.drdlid == 0xffff))
71321936Shselasky		rpc.mgtclass = IB_SMI_DIRECT_CLASS;	/* direct SMI */
72321936Shselasky	else
73321936Shselasky		rpc.mgtclass = IB_SMI_CLASS;	/* Lid routed SMI */
74321936Shselasky
75321936Shselasky	rpc.method = IB_MAD_METHOD_SET;
76321936Shselasky	rpc.attr.id = attrid;
77321936Shselasky	rpc.attr.mod = mod;
78321936Shselasky	rpc.timeout = timeout;
79321936Shselasky	rpc.datasz = IB_SMP_DATA_SIZE;
80321936Shselasky	rpc.dataoffs = IB_SMP_DATA_OFFS;
81321936Shselasky	rpc.mkey = srcport->smp_mkey;
82321936Shselasky
83321936Shselasky	portid->sl = 0;
84321936Shselasky	portid->qp = 0;
85321936Shselasky
86321936Shselasky	res = mad_rpc(srcport, &rpc, portid, data, data);
87321936Shselasky	if (rstatus)
88321936Shselasky		*rstatus = rpc.rstatus;
89321936Shselasky	return res;
90321936Shselasky}
91321936Shselasky
92321936Shselaskyuint8_t *smp_set_via(void *data, ib_portid_t * portid, unsigned attrid,
93321936Shselasky		     unsigned mod, unsigned timeout,
94321936Shselasky		     const struct ibmad_port *srcport)
95321936Shselasky{
96321936Shselasky	return smp_set_status_via(data, portid, attrid, mod, timeout, NULL,
97321936Shselasky				  srcport);
98321936Shselasky}
99321936Shselasky
100321936Shselaskyuint8_t *smp_set(void *data, ib_portid_t * portid, unsigned attrid,
101321936Shselasky		 unsigned mod, unsigned timeout)
102321936Shselasky{
103321936Shselasky	return smp_set_via(data, portid, attrid, mod, timeout, ibmp);
104321936Shselasky}
105321936Shselasky
106321936Shselaskyuint8_t *smp_query_status_via(void *rcvbuf, ib_portid_t * portid,
107321936Shselasky			      unsigned attrid, unsigned mod, unsigned timeout,
108321936Shselasky			      int *rstatus, const struct ibmad_port * srcport)
109321936Shselasky{
110321936Shselasky	ib_rpc_t rpc = { 0 };
111321936Shselasky	uint8_t *res;
112321936Shselasky
113321936Shselasky	DEBUG("attr 0x%x mod 0x%x route %s", attrid, mod, portid2str(portid));
114321936Shselasky	rpc.method = IB_MAD_METHOD_GET;
115321936Shselasky	rpc.attr.id = attrid;
116321936Shselasky	rpc.attr.mod = mod;
117321936Shselasky	rpc.timeout = timeout;
118321936Shselasky	rpc.datasz = IB_SMP_DATA_SIZE;
119321936Shselasky	rpc.dataoffs = IB_SMP_DATA_OFFS;
120321936Shselasky	rpc.mkey = srcport->smp_mkey;
121321936Shselasky
122321936Shselasky	if ((portid->lid <= 0) ||
123321936Shselasky	    (portid->drpath.drslid == 0xffff) ||
124321936Shselasky	    (portid->drpath.drdlid == 0xffff))
125321936Shselasky		rpc.mgtclass = IB_SMI_DIRECT_CLASS;	/* direct SMI */
126321936Shselasky	else
127321936Shselasky		rpc.mgtclass = IB_SMI_CLASS;	/* Lid routed SMI */
128321936Shselasky
129321936Shselasky	portid->sl = 0;
130321936Shselasky	portid->qp = 0;
131321936Shselasky
132321936Shselasky	res = mad_rpc(srcport, &rpc, portid, rcvbuf, rcvbuf);
133321936Shselasky	if (rstatus)
134321936Shselasky		*rstatus = rpc.rstatus;
135321936Shselasky	return res;
136321936Shselasky}
137321936Shselasky
138321936Shselaskyuint8_t *smp_query_via(void *rcvbuf, ib_portid_t * portid, unsigned attrid,
139321936Shselasky		       unsigned mod, unsigned timeout,
140321936Shselasky		       const struct ibmad_port * srcport)
141321936Shselasky{
142321936Shselasky	return smp_query_status_via(rcvbuf, portid, attrid, mod, timeout, NULL,
143321936Shselasky				    srcport);
144321936Shselasky}
145321936Shselasky
146321936Shselaskyuint8_t *smp_query(void *rcvbuf, ib_portid_t * portid, unsigned attrid,
147321936Shselasky		   unsigned mod, unsigned timeout)
148321936Shselasky{
149321936Shselasky	return smp_query_via(rcvbuf, portid, attrid, mod, timeout, ibmp);
150321936Shselasky}
151