xref: /freebsd/contrib/ofed/libibmad/sa.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3*d6b92ffaSHans Petter Selasky  *
4*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
5*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
6*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
7*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
8*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
9*d6b92ffaSHans Petter Selasky  *
10*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
11*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
12*d6b92ffaSHans Petter Selasky  *     conditions are met:
13*d6b92ffaSHans Petter Selasky  *
14*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
15*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
16*d6b92ffaSHans Petter Selasky  *        disclaimer.
17*d6b92ffaSHans Petter Selasky  *
18*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
19*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
20*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
21*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
22*d6b92ffaSHans Petter Selasky  *
23*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*d6b92ffaSHans Petter Selasky  * SOFTWARE.
31*d6b92ffaSHans Petter Selasky  *
32*d6b92ffaSHans Petter Selasky  */
33*d6b92ffaSHans Petter Selasky 
34*d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
35*d6b92ffaSHans Petter Selasky #  include <config.h>
36*d6b92ffaSHans Petter Selasky #endif				/* HAVE_CONFIG_H */
37*d6b92ffaSHans Petter Selasky 
38*d6b92ffaSHans Petter Selasky #include <stdio.h>
39*d6b92ffaSHans Petter Selasky #include <stdlib.h>
40*d6b92ffaSHans Petter Selasky #include <string.h>
41*d6b92ffaSHans Petter Selasky 
42*d6b92ffaSHans Petter Selasky #include <infiniband/umad.h>
43*d6b92ffaSHans Petter Selasky #include <infiniband/mad.h>
44*d6b92ffaSHans Petter Selasky #include "mad_internal.h"
45*d6b92ffaSHans Petter Selasky 
46*d6b92ffaSHans Petter Selasky #undef DEBUG
47*d6b92ffaSHans Petter Selasky #define DEBUG 	if (ibdebug)	IBWARN
48*d6b92ffaSHans Petter Selasky 
sa_rpc_call(const struct ibmad_port * ibmad_port,void * rcvbuf,ib_portid_t * portid,ib_sa_call_t * sa,unsigned timeout)49*d6b92ffaSHans Petter Selasky uint8_t *sa_rpc_call(const struct ibmad_port *ibmad_port, void *rcvbuf,
50*d6b92ffaSHans Petter Selasky 		     ib_portid_t * portid, ib_sa_call_t * sa, unsigned timeout)
51*d6b92ffaSHans Petter Selasky {
52*d6b92ffaSHans Petter Selasky 	ib_rpc_t rpc = { 0 };
53*d6b92ffaSHans Petter Selasky 	uint8_t *p;
54*d6b92ffaSHans Petter Selasky 
55*d6b92ffaSHans Petter Selasky 	DEBUG("attr 0x%x mod 0x%x route %s", sa->attrid, sa->mod,
56*d6b92ffaSHans Petter Selasky 	      portid2str(portid));
57*d6b92ffaSHans Petter Selasky 
58*d6b92ffaSHans Petter Selasky 	if (portid->lid <= 0) {
59*d6b92ffaSHans Petter Selasky 		IBWARN("only lid routes are supported");
60*d6b92ffaSHans Petter Selasky 		return NULL;
61*d6b92ffaSHans Petter Selasky 	}
62*d6b92ffaSHans Petter Selasky 
63*d6b92ffaSHans Petter Selasky 	rpc.mgtclass = IB_SA_CLASS;
64*d6b92ffaSHans Petter Selasky 	rpc.method = sa->method;
65*d6b92ffaSHans Petter Selasky 	rpc.attr.id = sa->attrid;
66*d6b92ffaSHans Petter Selasky 	rpc.attr.mod = sa->mod;
67*d6b92ffaSHans Petter Selasky 	rpc.mask = sa->mask;
68*d6b92ffaSHans Petter Selasky 	rpc.timeout = timeout;
69*d6b92ffaSHans Petter Selasky 	rpc.datasz = IB_SA_DATA_SIZE;
70*d6b92ffaSHans Petter Selasky 	rpc.dataoffs = IB_SA_DATA_OFFS;
71*d6b92ffaSHans Petter Selasky 	rpc.trid = sa->trid;
72*d6b92ffaSHans Petter Selasky 
73*d6b92ffaSHans Petter Selasky 	portid->qp = 1;
74*d6b92ffaSHans Petter Selasky 	if (!portid->qkey)
75*d6b92ffaSHans Petter Selasky 		portid->qkey = IB_DEFAULT_QP1_QKEY;
76*d6b92ffaSHans Petter Selasky 
77*d6b92ffaSHans Petter Selasky 	p = mad_rpc_rmpp(ibmad_port, &rpc, portid, 0 /*&sa->rmpp */ , rcvbuf);	/* TODO: RMPP */
78*d6b92ffaSHans Petter Selasky 
79*d6b92ffaSHans Petter Selasky 	sa->recsz = rpc.recsz;
80*d6b92ffaSHans Petter Selasky 
81*d6b92ffaSHans Petter Selasky 	return p;
82*d6b92ffaSHans Petter Selasky }
83*d6b92ffaSHans Petter Selasky 
sa_call(void * rcvbuf,ib_portid_t * portid,ib_sa_call_t * sa,unsigned timeout)84*d6b92ffaSHans Petter Selasky uint8_t *sa_call(void *rcvbuf, ib_portid_t * portid, ib_sa_call_t * sa,
85*d6b92ffaSHans Petter Selasky 		 unsigned timeout)
86*d6b92ffaSHans Petter Selasky {
87*d6b92ffaSHans Petter Selasky 	return sa_rpc_call(ibmp, rcvbuf, portid, sa, timeout);
88*d6b92ffaSHans Petter Selasky }
89*d6b92ffaSHans Petter Selasky 
90*d6b92ffaSHans Petter Selasky /* PathRecord */
91*d6b92ffaSHans Petter Selasky #define IB_PR_COMPMASK_DGID				(1ull<<2)
92*d6b92ffaSHans Petter Selasky #define IB_PR_COMPMASK_SGID				(1ull<<3)
93*d6b92ffaSHans Petter Selasky #define IB_PR_COMPMASK_DLID				(1ull<<4)
94*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_SLID				(1ull<<5)
95*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_RAWTRAFIC			(1ull<<6)
96*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_RESV0				(1ull<<7)
97*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_FLOWLABEL			(1ull<<8)
98*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_HOPLIMIT				(1ull<<9)
99*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_TCLASS				(1ull<<10)
100*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_REVERSIBLE			(1ull<<11)
101*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_NUMBPATH				(1ull<<12)
102*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_PKEY				(1ull<<13)
103*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_RESV1				(1ull<<14)
104*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_SL				(1ull<<15)
105*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_MTUSELEC				(1ull<<16)
106*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_MTU				(1ull<<17)
107*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_RATESELEC			(1ull<<18)
108*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_RATE				(1ull<<19)
109*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_PKTLIFETIMESELEC			(1ull<<20)
110*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_PKTLIFETIME			(1ull<<21)
111*d6b92ffaSHans Petter Selasky #define	IB_PR_COMPMASK_PREFERENCE			(1ull<<22)
112*d6b92ffaSHans Petter Selasky 
113*d6b92ffaSHans Petter Selasky #define IB_PR_DEF_MASK (IB_PR_COMPMASK_DGID |\
114*d6b92ffaSHans Petter Selasky 			IB_PR_COMPMASK_SGID)
115*d6b92ffaSHans Petter Selasky 
ib_path_query_via(const struct ibmad_port * srcport,ibmad_gid_t srcgid,ibmad_gid_t destgid,ib_portid_t * sm_id,void * buf)116*d6b92ffaSHans Petter Selasky int ib_path_query_via(const struct ibmad_port *srcport, ibmad_gid_t srcgid,
117*d6b92ffaSHans Petter Selasky 		      ibmad_gid_t destgid, ib_portid_t * sm_id, void *buf)
118*d6b92ffaSHans Petter Selasky {
119*d6b92ffaSHans Petter Selasky 	ib_sa_call_t sa = { 0 };
120*d6b92ffaSHans Petter Selasky 	uint8_t *p;
121*d6b92ffaSHans Petter Selasky 	int dlid;
122*d6b92ffaSHans Petter Selasky 
123*d6b92ffaSHans Petter Selasky 	memset(&sa, 0, sizeof sa);
124*d6b92ffaSHans Petter Selasky 	sa.method = IB_MAD_METHOD_GET;
125*d6b92ffaSHans Petter Selasky 	sa.attrid = IB_SA_ATTR_PATHRECORD;
126*d6b92ffaSHans Petter Selasky 	sa.mask = IB_PR_DEF_MASK;
127*d6b92ffaSHans Petter Selasky 	sa.trid = mad_trid();
128*d6b92ffaSHans Petter Selasky 
129*d6b92ffaSHans Petter Selasky 	memset(buf, 0, IB_SA_PR_RECSZ);
130*d6b92ffaSHans Petter Selasky 
131*d6b92ffaSHans Petter Selasky 	mad_encode_field(buf, IB_SA_PR_DGID_F, destgid);
132*d6b92ffaSHans Petter Selasky 	mad_encode_field(buf, IB_SA_PR_SGID_F, srcgid);
133*d6b92ffaSHans Petter Selasky 
134*d6b92ffaSHans Petter Selasky 	p = sa_rpc_call(srcport, buf, sm_id, &sa, 0);
135*d6b92ffaSHans Petter Selasky 	if (!p) {
136*d6b92ffaSHans Petter Selasky 		IBWARN("sa call path_query failed");
137*d6b92ffaSHans Petter Selasky 		return -1;
138*d6b92ffaSHans Petter Selasky 	}
139*d6b92ffaSHans Petter Selasky 
140*d6b92ffaSHans Petter Selasky 	mad_decode_field(p, IB_SA_PR_DLID_F, &dlid);
141*d6b92ffaSHans Petter Selasky 	return dlid;
142*d6b92ffaSHans Petter Selasky }
143*d6b92ffaSHans Petter Selasky 
ib_path_query(ibmad_gid_t srcgid,ibmad_gid_t destgid,ib_portid_t * sm_id,void * buf)144*d6b92ffaSHans Petter Selasky int ib_path_query(ibmad_gid_t srcgid, ibmad_gid_t destgid, ib_portid_t * sm_id,
145*d6b92ffaSHans Petter Selasky 		  void *buf)
146*d6b92ffaSHans Petter Selasky {
147*d6b92ffaSHans Petter Selasky 	return ib_path_query_via(ibmp, srcgid, destgid, sm_id, buf);
148*d6b92ffaSHans Petter Selasky }
149*d6b92ffaSHans Petter Selasky 
150*d6b92ffaSHans Petter Selasky /* NodeRecord */
151*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_LID				(1ull<<0)
152*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_RESERVED1			(1ull<<1)
153*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_BASEVERSION			(1ull<<2)
154*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_CLASSVERSION			(1ull<<3)
155*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_NODETYPE				(1ull<<4)
156*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_NUMPORTS				(1ull<<5)
157*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_SYSIMAGEGUID			(1ull<<6)
158*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_NODEGUID				(1ull<<7)
159*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_PORTGUID				(1ull<<8)
160*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_PARTCAP				(1ull<<9)
161*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_DEVID				(1ull<<10)
162*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_REV				(1ull<<11)
163*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_PORTNUM				(1ull<<12)
164*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_VENDID				(1ull<<13)
165*d6b92ffaSHans Petter Selasky #define IB_NR_COMPMASK_NODEDESC				(1ull<<14)
166*d6b92ffaSHans Petter Selasky 
167*d6b92ffaSHans Petter Selasky #define IB_NR_DEF_MASK IB_NR_COMPMASK_PORTGUID
168*d6b92ffaSHans Petter Selasky 
ib_node_query_via(const struct ibmad_port * srcport,uint64_t guid,ib_portid_t * sm_id,void * buf)169*d6b92ffaSHans Petter Selasky int ib_node_query_via(const struct ibmad_port *srcport, uint64_t guid,
170*d6b92ffaSHans Petter Selasky 		      ib_portid_t * sm_id, void *buf)
171*d6b92ffaSHans Petter Selasky {
172*d6b92ffaSHans Petter Selasky 	ib_sa_call_t sa = { 0 };
173*d6b92ffaSHans Petter Selasky 	uint8_t *p;
174*d6b92ffaSHans Petter Selasky 
175*d6b92ffaSHans Petter Selasky 	memset(&sa, 0, sizeof sa);
176*d6b92ffaSHans Petter Selasky 	sa.method = IB_MAD_METHOD_GET;
177*d6b92ffaSHans Petter Selasky 	sa.attrid = IB_SA_ATTR_NODERECORD;
178*d6b92ffaSHans Petter Selasky 	sa.mask = IB_NR_DEF_MASK;
179*d6b92ffaSHans Petter Selasky 	sa.trid = mad_trid();
180*d6b92ffaSHans Petter Selasky 
181*d6b92ffaSHans Petter Selasky 	memset(buf, 0, IB_SA_NR_RECSZ);
182*d6b92ffaSHans Petter Selasky 
183*d6b92ffaSHans Petter Selasky 	mad_encode_field(buf, IB_SA_NR_PORT_GUID_F, &guid);
184*d6b92ffaSHans Petter Selasky 
185*d6b92ffaSHans Petter Selasky 	p = sa_rpc_call(srcport, buf, sm_id, &sa, 0);
186*d6b92ffaSHans Petter Selasky 	if (!p) {
187*d6b92ffaSHans Petter Selasky 		IBWARN("sa call node_query failed");
188*d6b92ffaSHans Petter Selasky 		return -1;
189*d6b92ffaSHans Petter Selasky 	}
190*d6b92ffaSHans Petter Selasky 
191*d6b92ffaSHans Petter Selasky 	return 0;
192*d6b92ffaSHans Petter Selasky }
193