xref: /freebsd/contrib/ofed/libibmad/mad.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3*d6b92ffaSHans Petter Selasky  * Copyright (c) 2009 HNR Consulting.  All rights reserved.
4*d6b92ffaSHans Petter Selasky  * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
5*d6b92ffaSHans Petter Selasky  *
6*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
7*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
8*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
9*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
10*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
11*d6b92ffaSHans Petter Selasky  *
12*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
13*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
14*d6b92ffaSHans Petter Selasky  *     conditions are met:
15*d6b92ffaSHans Petter Selasky  *
16*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
17*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
18*d6b92ffaSHans Petter Selasky  *        disclaimer.
19*d6b92ffaSHans Petter Selasky  *
20*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
21*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
22*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
23*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
24*d6b92ffaSHans Petter Selasky  *
25*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*d6b92ffaSHans Petter Selasky  * SOFTWARE.
33*d6b92ffaSHans Petter Selasky  *
34*d6b92ffaSHans Petter Selasky  */
35*d6b92ffaSHans Petter Selasky 
36*d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
37*d6b92ffaSHans Petter Selasky #  include <config.h>
38*d6b92ffaSHans Petter Selasky #endif				/* HAVE_CONFIG_H */
39*d6b92ffaSHans Petter Selasky 
40*d6b92ffaSHans Petter Selasky #include <stdio.h>
41*d6b92ffaSHans Petter Selasky #include <stdlib.h>
42*d6b92ffaSHans Petter Selasky #include <string.h>
43*d6b92ffaSHans Petter Selasky #include <time.h>
44*d6b92ffaSHans Petter Selasky #include <errno.h>
45*d6b92ffaSHans Petter Selasky 
46*d6b92ffaSHans Petter Selasky #include <infiniband/umad.h>
47*d6b92ffaSHans Petter Selasky #include <infiniband/mad.h>
48*d6b92ffaSHans Petter Selasky 
49*d6b92ffaSHans Petter Selasky #include "mad_internal.h"
50*d6b92ffaSHans Petter Selasky 
51*d6b92ffaSHans Petter Selasky #undef DEBUG
52*d6b92ffaSHans Petter Selasky #define DEBUG	if (ibdebug)	IBWARN
53*d6b92ffaSHans Petter Selasky 
54*d6b92ffaSHans Petter Selasky #define GET_IB_USERLAND_TID(tid)	(tid & 0x00000000ffffffff)
55*d6b92ffaSHans Petter Selasky /*
56*d6b92ffaSHans Petter Selasky  * Generate the 64 bit MAD transaction ID. The upper 32 bits are reserved for
57*d6b92ffaSHans Petter Selasky  * use by the kernel. We clear the upper 32 bits here, but MADs received from
58*d6b92ffaSHans Petter Selasky  * the kernel may contain kernel specific data in these bits, consequently
59*d6b92ffaSHans Petter Selasky  * userland TID matching should only be done on the lower 32 bits.
60*d6b92ffaSHans Petter Selasky  */
mad_trid(void)61*d6b92ffaSHans Petter Selasky uint64_t mad_trid(void)
62*d6b92ffaSHans Petter Selasky {
63*d6b92ffaSHans Petter Selasky 	static uint64_t trid;
64*d6b92ffaSHans Petter Selasky 	uint64_t next;
65*d6b92ffaSHans Petter Selasky 
66*d6b92ffaSHans Petter Selasky 	if (!trid) {
67*d6b92ffaSHans Petter Selasky 		srandom((int)time(0) * getpid());
68*d6b92ffaSHans Petter Selasky 		trid = random();
69*d6b92ffaSHans Petter Selasky 	}
70*d6b92ffaSHans Petter Selasky 	next = ++trid;
71*d6b92ffaSHans Petter Selasky 	next = GET_IB_USERLAND_TID(next);
72*d6b92ffaSHans Petter Selasky 	return next;
73*d6b92ffaSHans Petter Selasky }
74*d6b92ffaSHans Petter Selasky 
mad_get_timeout(const struct ibmad_port * srcport,int override_ms)75*d6b92ffaSHans Petter Selasky int mad_get_timeout(const struct ibmad_port *srcport, int override_ms)
76*d6b92ffaSHans Petter Selasky {
77*d6b92ffaSHans Petter Selasky 	return (override_ms ? override_ms :
78*d6b92ffaSHans Petter Selasky 		srcport->timeout ? srcport->timeout : madrpc_timeout);
79*d6b92ffaSHans Petter Selasky }
80*d6b92ffaSHans Petter Selasky 
mad_get_retries(const struct ibmad_port * srcport)81*d6b92ffaSHans Petter Selasky int mad_get_retries(const struct ibmad_port *srcport)
82*d6b92ffaSHans Petter Selasky {
83*d6b92ffaSHans Petter Selasky 	return (srcport->retries ? srcport->retries : madrpc_retries);
84*d6b92ffaSHans Petter Selasky }
85*d6b92ffaSHans Petter Selasky 
mad_encode(void * buf,ib_rpc_t * rpc,ib_dr_path_t * drpath,void * data)86*d6b92ffaSHans Petter Selasky void *mad_encode(void *buf, ib_rpc_t * rpc, ib_dr_path_t * drpath, void *data)
87*d6b92ffaSHans Petter Selasky {
88*d6b92ffaSHans Petter Selasky 	int is_resp = rpc->method & IB_MAD_RESPONSE;
89*d6b92ffaSHans Petter Selasky 	int mgtclass;
90*d6b92ffaSHans Petter Selasky 
91*d6b92ffaSHans Petter Selasky 	/* first word */
92*d6b92ffaSHans Petter Selasky 	mad_set_field(buf, 0, IB_MAD_METHOD_F, rpc->method);
93*d6b92ffaSHans Petter Selasky 	mad_set_field(buf, 0, IB_MAD_RESPONSE_F, is_resp ? 1 : 0);
94*d6b92ffaSHans Petter Selasky 	mgtclass = rpc->mgtclass & 0xff;
95*d6b92ffaSHans Petter Selasky 	if (mgtclass == IB_SA_CLASS || mgtclass == IB_CC_CLASS)
96*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_MAD_CLASSVER_F, 2);
97*d6b92ffaSHans Petter Selasky 	else
98*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_MAD_CLASSVER_F, 1);
99*d6b92ffaSHans Petter Selasky 	mad_set_field(buf, 0, IB_MAD_MGMTCLASS_F, rpc->mgtclass & 0xff);
100*d6b92ffaSHans Petter Selasky 	mad_set_field(buf, 0, IB_MAD_BASEVER_F, 1);
101*d6b92ffaSHans Petter Selasky 
102*d6b92ffaSHans Petter Selasky 	/* second word */
103*d6b92ffaSHans Petter Selasky 	if ((rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS) {
104*d6b92ffaSHans Petter Selasky 		if (!drpath) {
105*d6b92ffaSHans Petter Selasky 			IBWARN("encoding dr mad without drpath (null)");
106*d6b92ffaSHans Petter Selasky 			errno = EINVAL;
107*d6b92ffaSHans Petter Selasky 			return NULL;
108*d6b92ffaSHans Petter Selasky 		}
109*d6b92ffaSHans Petter Selasky 		if (drpath->cnt >= IB_SUBNET_PATH_HOPS_MAX) {
110*d6b92ffaSHans Petter Selasky 			IBWARN("dr path with hop count %d", drpath->cnt);
111*d6b92ffaSHans Petter Selasky 			errno = EINVAL;
112*d6b92ffaSHans Petter Selasky 			return NULL;
113*d6b92ffaSHans Petter Selasky 		}
114*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_DRSMP_HOPCNT_F, drpath->cnt);
115*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_DRSMP_HOPPTR_F,
116*d6b92ffaSHans Petter Selasky 			      is_resp ? drpath->cnt + 1 : 0x0);
117*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_DRSMP_STATUS_F, rpc->rstatus);
118*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_DRSMP_DIRECTION_F, is_resp ? 1 : 0);	/* out */
119*d6b92ffaSHans Petter Selasky 	} else
120*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_MAD_STATUS_F, rpc->rstatus);
121*d6b92ffaSHans Petter Selasky 
122*d6b92ffaSHans Petter Selasky 	/* words 3,4,5,6 */
123*d6b92ffaSHans Petter Selasky 	if (!rpc->trid)
124*d6b92ffaSHans Petter Selasky 		rpc->trid = mad_trid();
125*d6b92ffaSHans Petter Selasky 
126*d6b92ffaSHans Petter Selasky 	mad_set_field64(buf, 0, IB_MAD_TRID_F, rpc->trid);
127*d6b92ffaSHans Petter Selasky 	mad_set_field(buf, 0, IB_MAD_ATTRID_F, rpc->attr.id);
128*d6b92ffaSHans Petter Selasky 	mad_set_field(buf, 0, IB_MAD_ATTRMOD_F, rpc->attr.mod);
129*d6b92ffaSHans Petter Selasky 
130*d6b92ffaSHans Petter Selasky 	/* words 7,8 */
131*d6b92ffaSHans Petter Selasky 	mad_set_field64(buf, 0, IB_MAD_MKEY_F, rpc->mkey);
132*d6b92ffaSHans Petter Selasky 
133*d6b92ffaSHans Petter Selasky 	if ((rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS) {
134*d6b92ffaSHans Petter Selasky 		/* word 9 */
135*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_DRSMP_DRDLID_F,
136*d6b92ffaSHans Petter Selasky 			      drpath->drdlid ? drpath->drdlid : 0xffff);
137*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_DRSMP_DRSLID_F,
138*d6b92ffaSHans Petter Selasky 			      drpath->drslid ? drpath->drslid : 0xffff);
139*d6b92ffaSHans Petter Selasky 
140*d6b92ffaSHans Petter Selasky 		/* bytes 128 - 256 - by default should be zero due to memset */
141*d6b92ffaSHans Petter Selasky 		if (is_resp)
142*d6b92ffaSHans Petter Selasky 			mad_set_array(buf, 0, IB_DRSMP_RPATH_F, drpath->p);
143*d6b92ffaSHans Petter Selasky 		else
144*d6b92ffaSHans Petter Selasky 			mad_set_array(buf, 0, IB_DRSMP_PATH_F, drpath->p);
145*d6b92ffaSHans Petter Selasky 	}
146*d6b92ffaSHans Petter Selasky 
147*d6b92ffaSHans Petter Selasky 	if ((rpc->mgtclass & 0xff) == IB_SA_CLASS)
148*d6b92ffaSHans Petter Selasky 		mad_set_field64(buf, 0, IB_SA_COMPMASK_F, rpc->mask);
149*d6b92ffaSHans Petter Selasky 
150*d6b92ffaSHans Petter Selasky 	if ((rpc->mgtclass & 0xff) == IB_CC_CLASS) {
151*d6b92ffaSHans Petter Selasky 		ib_rpc_cc_t *rpccc = (ib_rpc_cc_t *)rpc;
152*d6b92ffaSHans Petter Selasky 		mad_set_field64(buf, 0, IB_CC_CCKEY_F, rpccc->cckey);
153*d6b92ffaSHans Petter Selasky 	}
154*d6b92ffaSHans Petter Selasky 
155*d6b92ffaSHans Petter Selasky 	if (data)
156*d6b92ffaSHans Petter Selasky 		memcpy((char *)buf + rpc->dataoffs, data, rpc->datasz);
157*d6b92ffaSHans Petter Selasky 
158*d6b92ffaSHans Petter Selasky 	/* vendor mads range 2 */
159*d6b92ffaSHans Petter Selasky 	if (mad_is_vendor_range2(rpc->mgtclass & 0xff))
160*d6b92ffaSHans Petter Selasky 		mad_set_field(buf, 0, IB_VEND2_OUI_F, rpc->oui);
161*d6b92ffaSHans Petter Selasky 
162*d6b92ffaSHans Petter Selasky 	return (uint8_t *) buf + IB_MAD_SIZE;
163*d6b92ffaSHans Petter Selasky }
164*d6b92ffaSHans Petter Selasky 
mad_build_pkt(void * umad,ib_rpc_t * rpc,ib_portid_t * dport,ib_rmpp_hdr_t * rmpp,void * data)165*d6b92ffaSHans Petter Selasky int mad_build_pkt(void *umad, ib_rpc_t * rpc, ib_portid_t * dport,
166*d6b92ffaSHans Petter Selasky 		  ib_rmpp_hdr_t * rmpp, void *data)
167*d6b92ffaSHans Petter Selasky {
168*d6b92ffaSHans Petter Selasky 	uint8_t *p, *mad;
169*d6b92ffaSHans Petter Selasky 	int lid_routed = (rpc->mgtclass & 0xff) != IB_SMI_DIRECT_CLASS;
170*d6b92ffaSHans Petter Selasky 	int is_smi = ((rpc->mgtclass & 0xff) == IB_SMI_CLASS ||
171*d6b92ffaSHans Petter Selasky 		      (rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS);
172*d6b92ffaSHans Petter Selasky 	struct ib_mad_addr addr;
173*d6b92ffaSHans Petter Selasky 
174*d6b92ffaSHans Petter Selasky 	if (!is_smi)
175*d6b92ffaSHans Petter Selasky 		umad_set_addr(umad, dport->lid, dport->qp, dport->sl,
176*d6b92ffaSHans Petter Selasky 			      dport->qkey);
177*d6b92ffaSHans Petter Selasky 	else if (lid_routed)
178*d6b92ffaSHans Petter Selasky 		umad_set_addr(umad, dport->lid, dport->qp, 0, 0);
179*d6b92ffaSHans Petter Selasky 	else if ((dport->drpath.drslid != 0xffff) && (dport->lid > 0))
180*d6b92ffaSHans Petter Selasky 		umad_set_addr(umad, dport->lid, 0, 0, 0);
181*d6b92ffaSHans Petter Selasky 	else
182*d6b92ffaSHans Petter Selasky 		umad_set_addr(umad, 0xffff, 0, 0, 0);
183*d6b92ffaSHans Petter Selasky 
184*d6b92ffaSHans Petter Selasky 	if (dport->grh_present && !is_smi) {
185*d6b92ffaSHans Petter Selasky 		addr.grh_present = 1;
186*d6b92ffaSHans Petter Selasky 		memcpy(addr.gid, dport->gid, 16);
187*d6b92ffaSHans Petter Selasky 		addr.hop_limit = 0xff;
188*d6b92ffaSHans Petter Selasky 		addr.traffic_class = 0;
189*d6b92ffaSHans Petter Selasky 		addr.flow_label = 0;
190*d6b92ffaSHans Petter Selasky 		umad_set_grh(umad, &addr);
191*d6b92ffaSHans Petter Selasky 	} else
192*d6b92ffaSHans Petter Selasky 		umad_set_grh(umad, 0);
193*d6b92ffaSHans Petter Selasky 	umad_set_pkey(umad, is_smi ? 0 : dport->pkey_idx);
194*d6b92ffaSHans Petter Selasky 
195*d6b92ffaSHans Petter Selasky 	mad = umad_get_mad(umad);
196*d6b92ffaSHans Petter Selasky 	p = mad_encode(mad, rpc, lid_routed ? 0 : &dport->drpath, data);
197*d6b92ffaSHans Petter Selasky 	if (!p)
198*d6b92ffaSHans Petter Selasky 		return -1;
199*d6b92ffaSHans Petter Selasky 
200*d6b92ffaSHans Petter Selasky 	if (!is_smi && rmpp) {
201*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_VERS_F, 1);
202*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_TYPE_F, rmpp->type);
203*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_RESP_F, 0x3f);
204*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_FLAGS_F, rmpp->flags);
205*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_STATUS_F, rmpp->status);
206*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_D1_F, rmpp->d1.u);
207*d6b92ffaSHans Petter Selasky 		mad_set_field(mad, 0, IB_SA_RMPP_D2_F, rmpp->d2.u);
208*d6b92ffaSHans Petter Selasky 	}
209*d6b92ffaSHans Petter Selasky 
210*d6b92ffaSHans Petter Selasky 	return ((int)(p - mad));
211*d6b92ffaSHans Petter Selasky }
212