1/*
2 * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3 * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 *     Redistribution and use in source and binary forms, with or
12 *     without modification, are permitted provided that the following
13 *     conditions are met:
14 *
15 *      - Redistributions of source code must retain the above
16 *        copyright notice, this list of conditions and the following
17 *        disclaimer.
18 *
19 *      - Redistributions in binary form must reproduce the above
20 *        copyright notice, this list of conditions and the following
21 *        disclaimer in the documentation and/or other materials
22 *        provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 */
34
35#if HAVE_CONFIG_H
36#  include <config.h>
37#endif				/* HAVE_CONFIG_H */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42
43#include <infiniband/umad.h>
44#include <infiniband/mad.h>
45#include "mad_internal.h"
46
47#undef DEBUG
48#define DEBUG 	if (ibdebug)	IBWARN
49
50void smp_mkey_set(struct ibmad_port *srcport, uint64_t mkey)
51{
52	srcport->smp_mkey = mkey;
53}
54
55uint64_t smp_mkey_get(const struct ibmad_port *srcport)
56{
57	return srcport->smp_mkey;
58}
59
60uint8_t *smp_set_status_via(void *data, ib_portid_t * portid, unsigned attrid,
61			    unsigned mod, unsigned timeout, int *rstatus,
62			    const struct ibmad_port *srcport)
63{
64	ib_rpc_t rpc = { 0 };
65	uint8_t *res;
66
67	DEBUG("attr 0x%x mod 0x%x route %s", attrid, mod, portid2str(portid));
68	if ((portid->lid <= 0) ||
69	    (portid->drpath.drslid == 0xffff) ||
70	    (portid->drpath.drdlid == 0xffff))
71		rpc.mgtclass = IB_SMI_DIRECT_CLASS;	/* direct SMI */
72	else
73		rpc.mgtclass = IB_SMI_CLASS;	/* Lid routed SMI */
74
75	rpc.method = IB_MAD_METHOD_SET;
76	rpc.attr.id = attrid;
77	rpc.attr.mod = mod;
78	rpc.timeout = timeout;
79	rpc.datasz = IB_SMP_DATA_SIZE;
80	rpc.dataoffs = IB_SMP_DATA_OFFS;
81	rpc.mkey = srcport->smp_mkey;
82
83	portid->sl = 0;
84	portid->qp = 0;
85
86	res = mad_rpc(srcport, &rpc, portid, data, data);
87	if (rstatus)
88		*rstatus = rpc.rstatus;
89	return res;
90}
91
92uint8_t *smp_set_via(void *data, ib_portid_t * portid, unsigned attrid,
93		     unsigned mod, unsigned timeout,
94		     const struct ibmad_port *srcport)
95{
96	return smp_set_status_via(data, portid, attrid, mod, timeout, NULL,
97				  srcport);
98}
99
100uint8_t *smp_set(void *data, ib_portid_t * portid, unsigned attrid,
101		 unsigned mod, unsigned timeout)
102{
103	return smp_set_via(data, portid, attrid, mod, timeout, ibmp);
104}
105
106uint8_t *smp_query_status_via(void *rcvbuf, ib_portid_t * portid,
107			      unsigned attrid, unsigned mod, unsigned timeout,
108			      int *rstatus, const struct ibmad_port * srcport)
109{
110	ib_rpc_t rpc = { 0 };
111	uint8_t *res;
112
113	DEBUG("attr 0x%x mod 0x%x route %s", attrid, mod, portid2str(portid));
114	rpc.method = IB_MAD_METHOD_GET;
115	rpc.attr.id = attrid;
116	rpc.attr.mod = mod;
117	rpc.timeout = timeout;
118	rpc.datasz = IB_SMP_DATA_SIZE;
119	rpc.dataoffs = IB_SMP_DATA_OFFS;
120	rpc.mkey = srcport->smp_mkey;
121
122	if ((portid->lid <= 0) ||
123	    (portid->drpath.drslid == 0xffff) ||
124	    (portid->drpath.drdlid == 0xffff))
125		rpc.mgtclass = IB_SMI_DIRECT_CLASS;	/* direct SMI */
126	else
127		rpc.mgtclass = IB_SMI_CLASS;	/* Lid routed SMI */
128
129	portid->sl = 0;
130	portid->qp = 0;
131
132	res = mad_rpc(srcport, &rpc, portid, rcvbuf, rcvbuf);
133	if (rstatus)
134		*rstatus = rpc.rstatus;
135	return res;
136}
137
138uint8_t *smp_query_via(void *rcvbuf, ib_portid_t * portid, unsigned attrid,
139		       unsigned mod, unsigned timeout,
140		       const struct ibmad_port * srcport)
141{
142	return smp_query_status_via(rcvbuf, portid, attrid, mod, timeout, NULL,
143				    srcport);
144}
145
146uint8_t *smp_query(void *rcvbuf, ib_portid_t * portid, unsigned attrid,
147		   unsigned mod, unsigned timeout)
148{
149	return smp_query_via(rcvbuf, portid, attrid, mod, timeout, ibmp);
150}
151