xref: /NextBSD/contrib/ofed/management/libibmad/src/mad.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
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 <string.h>
42 #include <pthread.h>
43 #include <sys/time.h>
44 
45 #include <infiniband/common.h>
46 #include <infiniband/umad.h>
47 #include <mad.h>
48 
49 #undef DEBUG
50 #define DEBUG	if (ibdebug)	IBWARN
51 
52 void
mad_decode_field(uint8_t * buf,int field,void * val)53 mad_decode_field(uint8_t *buf, int field, void *val)
54 {
55 	ib_field_t *f = ib_mad_f + field;
56 
57 	if (!field) {
58 		*(int *)val = *(int *)buf;
59 		return;
60 	}
61 	if (f->bitlen <= 32) {
62 		*(uint32_t *)val = _get_field(buf, 0, f);
63 		return;
64 	}
65 	if (f->bitlen == 64) {
66 		*(uint64_t *)val = _get_field64(buf, 0, f);
67 		return;
68 	}
69 	_get_array(buf, 0, f, val);
70 }
71 
72 void
mad_encode_field(uint8_t * buf,int field,void * val)73 mad_encode_field(uint8_t *buf, int field, void *val)
74 {
75 	ib_field_t *f = ib_mad_f + field;
76 
77 	if (!field) {
78 		*(int *)buf = *(int *)val;
79 		return;
80 	}
81 	if (f->bitlen <= 32) {
82 		_set_field(buf, 0, f, *(uint32_t *)val);
83 		return;
84 	}
85 	if (f->bitlen == 64) {
86 		_set_field64(buf, 0, f, *(uint64_t *)val);
87 		return;
88 	}
89 	_set_array(buf, 0, f, val);
90 }
91 
92 uint64_t
mad_trid(void)93 mad_trid(void)
94 {
95 	static uint64_t base;
96 	static uint64_t trid;
97 	uint64_t next;
98 
99 	if (!base) {
100 		srandom(time(0)*getpid());
101 		base = random();
102 		trid = random();
103 	}
104 	next = ++trid | (base << 32);
105 	return next;
106 }
107 
108 void *
mad_encode(void * buf,ib_rpc_t * rpc,ib_dr_path_t * drpath,void * data)109 mad_encode(void *buf, ib_rpc_t *rpc, ib_dr_path_t *drpath, void *data)
110 {
111 	int is_resp = rpc->method & IB_MAD_RESPONSE;
112 
113 	/* first word */
114 	mad_set_field(buf, 0, IB_MAD_METHOD_F, rpc->method);
115 	mad_set_field(buf, 0, IB_MAD_RESPONSE_F, is_resp ? 1 : 0);
116 	mad_set_field(buf, 0, IB_MAD_CLASSVER_F, rpc->mgtclass == IB_SA_CLASS ? 2 : 1);
117 	mad_set_field(buf, 0, IB_MAD_MGMTCLASS_F, rpc->mgtclass);
118 	mad_set_field(buf, 0, IB_MAD_BASEVER_F, 1);
119 
120 	/* second word */
121 	if (rpc->mgtclass == IB_SMI_DIRECT_CLASS) {
122 		if (!drpath) {
123 			IBWARN("encoding dr mad without drpath (null)");
124 			return 0;
125 		}
126 		mad_set_field(buf, 0, IB_DRSMP_HOPCNT_F, drpath->cnt);
127 		mad_set_field(buf, 0, IB_DRSMP_HOPPTR_F, is_resp ? drpath->cnt + 1 : 0x0);
128 		mad_set_field(buf, 0, IB_DRSMP_STATUS_F, rpc->rstatus);
129 		mad_set_field(buf, 0, IB_DRSMP_DIRECTION_F, is_resp ? 1 : 0);	/* out */
130 	} else
131 		mad_set_field(buf, 0, IB_MAD_STATUS_F, rpc->rstatus);
132 
133 	/* words 3,4,5,6 */
134 	if (!rpc->trid)
135 		rpc->trid = mad_trid();
136 
137 	mad_set_field64(buf, 0, IB_MAD_TRID_F, rpc->trid);
138 	mad_set_field(buf, 0, IB_MAD_ATTRID_F, rpc->attr.id);
139 	mad_set_field(buf, 0, IB_MAD_ATTRMOD_F, rpc->attr.mod);
140 
141 	/* words 7,8 */
142 	mad_set_field(buf, 0, IB_MAD_MKEY_F, rpc->mkey >> 32);
143 	mad_set_field(buf, 4, IB_MAD_MKEY_F, rpc->mkey & 0xffffffff);
144 
145 	if (rpc->mgtclass == IB_SMI_DIRECT_CLASS) {
146 		/* word 9 */
147 		mad_set_field(buf, 0, IB_DRSMP_DRDLID_F, drpath->drdlid ? drpath->drdlid : 0xffff);
148 		mad_set_field(buf, 0, IB_DRSMP_DRSLID_F, drpath->drslid ? drpath->drslid : 0xffff);
149 
150 		/* bytes 128 - 256 - by default should be zero due to memset*/
151 		if (is_resp)
152 			mad_set_array(buf, 0, IB_DRSMP_RPATH_F, drpath->p);
153 		else
154 			mad_set_array(buf, 0, IB_DRSMP_PATH_F, drpath->p);
155 	}
156 
157 	if (rpc->mgtclass == IB_SA_CLASS)
158 		mad_set_field64(buf, 0, IB_SA_COMPMASK_F, rpc->mask);
159 
160 	if (data)
161 		memcpy((char *)buf + rpc->dataoffs, data, rpc->datasz);
162 
163 	/* vendor mads range 2 */
164 	if (mad_is_vendor_range2(rpc->mgtclass))
165 		mad_set_field(buf, 0, IB_VEND2_OUI_F, rpc->oui);
166 
167 	return (uint8_t *)buf + IB_MAD_SIZE;
168 }
169 
170 int
mad_build_pkt(void * umad,ib_rpc_t * rpc,ib_portid_t * dport,ib_rmpp_hdr_t * rmpp,void * data)171 mad_build_pkt(void *umad, ib_rpc_t *rpc, ib_portid_t *dport,
172 	      ib_rmpp_hdr_t *rmpp, void *data)
173 {
174 	uint8_t *p, *mad;
175 	int lid_routed = rpc->mgtclass != IB_SMI_DIRECT_CLASS;
176 	int is_smi = (rpc->mgtclass == IB_SMI_CLASS ||
177 		      rpc->mgtclass == IB_SMI_DIRECT_CLASS);
178 	struct ib_mad_addr addr;
179 
180 	if (!is_smi)
181 		umad_set_addr(umad, dport->lid, dport->qp, dport->sl, dport->qkey);
182 	else if (lid_routed)
183 		umad_set_addr(umad, dport->lid, dport->qp, 0, 0);
184 	else if ((dport->drpath.drslid != 0xffff) && (dport->lid > 0))
185 		umad_set_addr(umad, dport->lid, 0, 0, 0);
186 	else
187 		umad_set_addr(umad, 0xffff, 0, 0, 0);
188 
189 	if (dport->grh_present && !is_smi) {
190 		addr.grh_present = 1;
191 		memcpy(addr.gid, dport->gid, 16);
192 		addr.hop_limit = 0xff;
193 		addr.traffic_class = 0;
194 		addr.flow_label = 0;
195 		umad_set_grh(umad, &addr);
196 	} else
197 		umad_set_grh(umad, 0);
198 	umad_set_pkey(umad, is_smi ? 0 : dport->pkey_idx);
199 
200 	mad = umad_get_mad(umad);
201 	p = mad_encode(mad, rpc, lid_routed ? 0 : &dport->drpath, data);
202 
203 	if (!is_smi && rmpp) {
204 		mad_set_field(mad, 0, IB_SA_RMPP_VERS_F, 1);
205 		mad_set_field(mad, 0, IB_SA_RMPP_TYPE_F, rmpp->type);
206 		mad_set_field(mad, 0, IB_SA_RMPP_RESP_F, 0x3f);
207 		mad_set_field(mad, 0, IB_SA_RMPP_FLAGS_F, rmpp->flags);
208 		mad_set_field(mad, 0, IB_SA_RMPP_STATUS_F, rmpp->status);
209 		mad_set_field(mad, 0, IB_SA_RMPP_D1_F, rmpp->d1.u);
210 		mad_set_field(mad, 0, IB_SA_RMPP_D2_F, rmpp->d2.u);
211 	}
212 
213 	return p - mad;
214 }
215