1/*
2 * Copyright (c) 2004-2009 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 <string.h>
41
42#include <infiniband/umad.h>
43#include <infiniband/mad.h>
44#include "mad_internal.h"
45
46#undef DEBUG
47#define DEBUG 	if (ibdebug)	IBWARN
48
49uint8_t *sa_rpc_call(const struct ibmad_port *ibmad_port, void *rcvbuf,
50		     ib_portid_t * portid, ib_sa_call_t * sa, unsigned timeout)
51{
52	ib_rpc_t rpc = { 0 };
53	uint8_t *p;
54
55	DEBUG("attr 0x%x mod 0x%x route %s", sa->attrid, sa->mod,
56	      portid2str(portid));
57
58	if (portid->lid <= 0) {
59		IBWARN("only lid routes are supported");
60		return NULL;
61	}
62
63	rpc.mgtclass = IB_SA_CLASS;
64	rpc.method = sa->method;
65	rpc.attr.id = sa->attrid;
66	rpc.attr.mod = sa->mod;
67	rpc.mask = sa->mask;
68	rpc.timeout = timeout;
69	rpc.datasz = IB_SA_DATA_SIZE;
70	rpc.dataoffs = IB_SA_DATA_OFFS;
71	rpc.trid = sa->trid;
72
73	portid->qp = 1;
74	if (!portid->qkey)
75		portid->qkey = IB_DEFAULT_QP1_QKEY;
76
77	p = mad_rpc_rmpp(ibmad_port, &rpc, portid, 0 /*&sa->rmpp */ , rcvbuf);	/* TODO: RMPP */
78
79	sa->recsz = rpc.recsz;
80
81	return p;
82}
83
84uint8_t *sa_call(void *rcvbuf, ib_portid_t * portid, ib_sa_call_t * sa,
85		 unsigned timeout)
86{
87	return sa_rpc_call(ibmp, rcvbuf, portid, sa, timeout);
88}
89
90/* PathRecord */
91#define IB_PR_COMPMASK_DGID				(1ull<<2)
92#define IB_PR_COMPMASK_SGID				(1ull<<3)
93#define IB_PR_COMPMASK_DLID				(1ull<<4)
94#define	IB_PR_COMPMASK_SLID				(1ull<<5)
95#define	IB_PR_COMPMASK_RAWTRAFIC			(1ull<<6)
96#define	IB_PR_COMPMASK_RESV0				(1ull<<7)
97#define	IB_PR_COMPMASK_FLOWLABEL			(1ull<<8)
98#define	IB_PR_COMPMASK_HOPLIMIT				(1ull<<9)
99#define	IB_PR_COMPMASK_TCLASS				(1ull<<10)
100#define	IB_PR_COMPMASK_REVERSIBLE			(1ull<<11)
101#define	IB_PR_COMPMASK_NUMBPATH				(1ull<<12)
102#define	IB_PR_COMPMASK_PKEY				(1ull<<13)
103#define	IB_PR_COMPMASK_RESV1				(1ull<<14)
104#define	IB_PR_COMPMASK_SL				(1ull<<15)
105#define	IB_PR_COMPMASK_MTUSELEC				(1ull<<16)
106#define	IB_PR_COMPMASK_MTU				(1ull<<17)
107#define	IB_PR_COMPMASK_RATESELEC			(1ull<<18)
108#define	IB_PR_COMPMASK_RATE				(1ull<<19)
109#define	IB_PR_COMPMASK_PKTLIFETIMESELEC			(1ull<<20)
110#define	IB_PR_COMPMASK_PKTLIFETIME			(1ull<<21)
111#define	IB_PR_COMPMASK_PREFERENCE			(1ull<<22)
112
113#define IB_PR_DEF_MASK (IB_PR_COMPMASK_DGID |\
114			IB_PR_COMPMASK_SGID)
115
116int ib_path_query_via(const struct ibmad_port *srcport, ibmad_gid_t srcgid,
117		      ibmad_gid_t destgid, ib_portid_t * sm_id, void *buf)
118{
119	ib_sa_call_t sa = { 0 };
120	uint8_t *p;
121	int dlid;
122
123	memset(&sa, 0, sizeof sa);
124	sa.method = IB_MAD_METHOD_GET;
125	sa.attrid = IB_SA_ATTR_PATHRECORD;
126	sa.mask = IB_PR_DEF_MASK;
127	sa.trid = mad_trid();
128
129	memset(buf, 0, IB_SA_PR_RECSZ);
130
131	mad_encode_field(buf, IB_SA_PR_DGID_F, destgid);
132	mad_encode_field(buf, IB_SA_PR_SGID_F, srcgid);
133
134	p = sa_rpc_call(srcport, buf, sm_id, &sa, 0);
135	if (!p) {
136		IBWARN("sa call path_query failed");
137		return -1;
138	}
139
140	mad_decode_field(p, IB_SA_PR_DLID_F, &dlid);
141	return dlid;
142}
143
144int ib_path_query(ibmad_gid_t srcgid, ibmad_gid_t destgid, ib_portid_t * sm_id,
145		  void *buf)
146{
147	return ib_path_query_via(ibmp, srcgid, destgid, sm_id, buf);
148}
149
150/* NodeRecord */
151#define IB_NR_COMPMASK_LID				(1ull<<0)
152#define IB_NR_COMPMASK_RESERVED1			(1ull<<1)
153#define IB_NR_COMPMASK_BASEVERSION			(1ull<<2)
154#define IB_NR_COMPMASK_CLASSVERSION			(1ull<<3)
155#define IB_NR_COMPMASK_NODETYPE				(1ull<<4)
156#define IB_NR_COMPMASK_NUMPORTS				(1ull<<5)
157#define IB_NR_COMPMASK_SYSIMAGEGUID			(1ull<<6)
158#define IB_NR_COMPMASK_NODEGUID				(1ull<<7)
159#define IB_NR_COMPMASK_PORTGUID				(1ull<<8)
160#define IB_NR_COMPMASK_PARTCAP				(1ull<<9)
161#define IB_NR_COMPMASK_DEVID				(1ull<<10)
162#define IB_NR_COMPMASK_REV				(1ull<<11)
163#define IB_NR_COMPMASK_PORTNUM				(1ull<<12)
164#define IB_NR_COMPMASK_VENDID				(1ull<<13)
165#define IB_NR_COMPMASK_NODEDESC				(1ull<<14)
166
167#define IB_NR_DEF_MASK IB_NR_COMPMASK_PORTGUID
168
169int ib_node_query_via(const struct ibmad_port *srcport, uint64_t guid,
170		      ib_portid_t * sm_id, void *buf)
171{
172	ib_sa_call_t sa = { 0 };
173	uint8_t *p;
174
175	memset(&sa, 0, sizeof sa);
176	sa.method = IB_MAD_METHOD_GET;
177	sa.attrid = IB_SA_ATTR_NODERECORD;
178	sa.mask = IB_NR_DEF_MASK;
179	sa.trid = mad_trid();
180
181	memset(buf, 0, IB_SA_NR_RECSZ);
182
183	mad_encode_field(buf, IB_SA_NR_PORT_GUID_F, &guid);
184
185	p = sa_rpc_call(srcport, buf, sm_id, &sa, 0);
186	if (!p) {
187		IBWARN("sa call node_query failed");
188		return -1;
189	}
190
191	return 0;
192}
193