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#include <errno.h>
43
44#include <infiniband/umad.h>
45#include <infiniband/mad.h>
46#include "mad_internal.h"
47
48#undef DEBUG
49#define DEBUG 	if (ibdebug)	IBWARN
50
51static inline int response_expected(int method)
52{
53	return method == IB_MAD_METHOD_GET ||
54	    method == IB_MAD_METHOD_SET || method == IB_MAD_METHOD_TRAP;
55}
56
57uint8_t *ib_vendor_call(void *data, ib_portid_t * portid,
58			ib_vendor_call_t * call)
59{
60	return ib_vendor_call_via(data, portid, call, ibmp);
61}
62
63uint8_t *ib_vendor_call_via(void *data, ib_portid_t * portid,
64			    ib_vendor_call_t * call,
65			    struct ibmad_port * srcport)
66{
67	ib_rpc_v1_t rpc = { 0 };
68	ib_rpc_t *rpcold = (ib_rpc_t *)(void *)&rpc;
69	int range1 = 0, resp_expected;
70	void *p_ret;
71
72	DEBUG("route %s data %p", portid2str(portid), data);
73	if (portid->lid <= 0)
74		return NULL;	/* no direct SMI */
75
76	if (!(range1 = mad_is_vendor_range1(call->mgmt_class)) &&
77	    !(mad_is_vendor_range2(call->mgmt_class)))
78		return NULL;
79
80	resp_expected = response_expected(call->method);
81
82	rpc.mgtclass = call->mgmt_class | IB_MAD_RPC_VERSION1;
83
84	rpc.method = call->method;
85	rpc.attr.id = call->attrid;
86	rpc.attr.mod = call->mod;
87	rpc.timeout = resp_expected ? call->timeout : 0;
88	rpc.datasz =
89	    range1 ? IB_VENDOR_RANGE1_DATA_SIZE : IB_VENDOR_RANGE2_DATA_SIZE;
90	rpc.dataoffs =
91	    range1 ? IB_VENDOR_RANGE1_DATA_OFFS : IB_VENDOR_RANGE2_DATA_OFFS;
92
93	if (!range1)
94		rpc.oui = call->oui;
95
96	DEBUG
97	    ("class 0x%x method 0x%x attr 0x%x mod 0x%x datasz %d off %d res_ex %d",
98	     rpc.mgtclass, rpc.method, rpc.attr.id, rpc.attr.mod, rpc.datasz,
99	     rpc.dataoffs, resp_expected);
100
101	portid->qp = 1;
102	if (!portid->qkey)
103		portid->qkey = IB_DEFAULT_QP1_QKEY;
104
105	if (resp_expected) {
106		p_ret = mad_rpc_rmpp(srcport, rpcold, portid, 0, data);	/* FIXME: no RMPP for now */
107		errno = rpc.error;
108		return p_ret;
109	}
110
111	return mad_send_via(rpcold, portid, 0, data, srcport) < 0 ? 0 : data;	/* FIXME: no RMPP for now */
112}
113