1/*
2 * Copyright (c) 2004-2007 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 <unistd.h>
41#include <pthread.h>
42#include <string.h>
43#include <sys/time.h>
44
45#include <mad.h>
46#include <infiniband/common.h>
47
48#undef DEBUG
49#define DEBUG 	if (ibdebug)	IBWARN
50
51uint8_t *
52sa_rpc_call(const void *ibmad_port, void *rcvbuf, ib_portid_t *portid,
53	    ib_sa_call_t *sa, unsigned timeout)
54{
55	ib_rpc_t rpc = {0};
56	uint8_t *p;
57
58	DEBUG("attr 0x%x mod 0x%x route %s", sa->attrid, sa->mod,
59	      portid2str(portid));
60
61	if (portid->lid <= 0) {
62		IBWARN("only lid routes are supported");
63		return 0;
64	}
65
66	rpc.mgtclass = IB_SA_CLASS;
67	rpc.method = sa->method;
68	rpc.attr.id = sa->attrid;
69	rpc.attr.mod = sa->mod;
70	rpc.mask = sa->mask;
71	rpc.timeout = timeout;
72	rpc.datasz = IB_SA_DATA_SIZE;
73	rpc.dataoffs = IB_SA_DATA_OFFS;
74	rpc.trid = sa->trid;
75
76	portid->qp = 1;
77	if (!portid->qkey)
78		portid->qkey = IB_DEFAULT_QP1_QKEY;
79
80	p = mad_rpc_rmpp(ibmad_port, &rpc, portid, 0/*&sa->rmpp*/, rcvbuf);	/* TODO: RMPP */
81
82	sa->recsz = rpc.recsz;
83
84	return p;
85}
86
87/* PathRecord */
88#define IB_PR_COMPMASK_DGID				(1ull<<2)
89#define IB_PR_COMPMASK_SGID				(1ull<<3)
90#define IB_PR_COMPMASK_DLID				(1ull<<4)
91#define	IB_PR_COMPMASK_SLID				(1ull<<5)
92#define	IB_PR_COMPMASK_RAWTRAFIC			(1ull<<6)
93#define	IB_PR_COMPMASK_RESV0				(1ull<<7)
94#define	IB_PR_COMPMASK_FLOWLABEL			(1ull<<8)
95#define	IB_PR_COMPMASK_HOPLIMIT				(1ull<<9)
96#define	IB_PR_COMPMASK_TCLASS				(1ull<<10)
97#define	IB_PR_COMPMASK_REVERSIBLE			(1ull<<11)
98#define	IB_PR_COMPMASK_NUMBPATH				(1ull<<12)
99#define	IB_PR_COMPMASK_PKEY				(1ull<<13)
100#define	IB_PR_COMPMASK_RESV1				(1ull<<14)
101#define	IB_PR_COMPMASK_SL				(1ull<<15)
102#define	IB_PR_COMPMASK_MTUSELEC				(1ull<<16)
103#define	IB_PR_COMPMASK_MTU				(1ull<<17)
104#define	IB_PR_COMPMASK_RATESELEC			(1ull<<18)
105#define	IB_PR_COMPMASK_RATE				(1ull<<19)
106#define	IB_PR_COMPMASK_PKTLIFETIMESELEC			(1ull<<20)
107#define	IB_PR_COMPMASK_PKTLIFETIME			(1ull<<21)
108#define	IB_PR_COMPMASK_PREFERENCE			(1ull<<22)
109
110#define IB_PR_DEF_MASK (IB_PR_COMPMASK_DGID |\
111			IB_PR_COMPMASK_SGID |\
112			IB_PR_COMPMASK_NUMBPATH)
113
114int
115ib_path_query_via(const void *srcport, ibmad_gid_t srcgid, ibmad_gid_t destgid, ib_portid_t *sm_id, void *buf)
116{
117	int npath;
118	ib_sa_call_t sa = {0};
119	uint8_t *p;
120	int dlid;
121
122	npath = 1;			/* only MAD_METHOD_GET is supported */
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_NPATH_F, &npath);
132	mad_encode_field(buf, IB_SA_PR_DGID_F, destgid);
133	mad_encode_field(buf, IB_SA_PR_SGID_F, srcgid);
134
135	if (srcport) {
136		p = sa_rpc_call (srcport, buf, sm_id, &sa, 0);
137	} else {
138		p = safe_sa_call(buf, sm_id, &sa, 0);
139	}
140	if (!p) {
141		IBWARN("sa call path_query failed");
142		return -1;
143	}
144
145	mad_decode_field(p, IB_SA_PR_DLID_F, &dlid);
146	return dlid;
147}
148int
149ib_path_query(ibmad_gid_t srcgid, ibmad_gid_t destgid, ib_portid_t *sm_id, void *buf)
150{
151	return ib_path_query_via (NULL, srcgid, destgid, sm_id, buf);
152}
153