2 * Copyright (c) 2004-2007 Voltaire Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
36 #endif /* HAVE_CONFIG_H */
45 #include <infiniband/common.h>
46 #include <infiniband/umad.h>
50 #define DEBUG if (ibdebug) IBWARN
53 mad_decode_field(uint8_t *buf, int field, void *val)
55 ib_field_t *f = ib_mad_f + field;
58 *(int *)val = *(int *)buf;
61 if (f->bitlen <= 32) {
62 *(uint32_t *)val = _get_field(buf, 0, f);
65 if (f->bitlen == 64) {
66 *(uint64_t *)val = _get_field64(buf, 0, f);
69 _get_array(buf, 0, f, val);
73 mad_encode_field(uint8_t *buf, int field, void *val)
75 ib_field_t *f = ib_mad_f + field;
78 *(int *)buf = *(int *)val;
81 if (f->bitlen <= 32) {
82 _set_field(buf, 0, f, *(uint32_t *)val);
85 if (f->bitlen == 64) {
86 _set_field64(buf, 0, f, *(uint64_t *)val);
89 _set_array(buf, 0, f, val);
100 srandom(time(0)*getpid());
104 next = ++trid | (base << 32);
109 mad_encode(void *buf, ib_rpc_t *rpc, ib_dr_path_t *drpath, void *data)
111 int is_resp = rpc->method & IB_MAD_RESPONSE;
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);
121 if (rpc->mgtclass == IB_SMI_DIRECT_CLASS) {
123 IBWARN("encoding dr mad without drpath (null)");
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 */
131 mad_set_field(buf, 0, IB_MAD_STATUS_F, rpc->rstatus);
135 rpc->trid = mad_trid();
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);
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);
145 if (rpc->mgtclass == IB_SMI_DIRECT_CLASS) {
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);
150 /* bytes 128 - 256 - by default should be zero due to memset*/
152 mad_set_array(buf, 0, IB_DRSMP_RPATH_F, drpath->p);
154 mad_set_array(buf, 0, IB_DRSMP_PATH_F, drpath->p);
157 if (rpc->mgtclass == IB_SA_CLASS)
158 mad_set_field64(buf, 0, IB_SA_COMPMASK_F, rpc->mask);
161 memcpy((char *)buf + rpc->dataoffs, data, rpc->datasz);
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);
167 return (uint8_t *)buf + IB_MAD_SIZE;
171 mad_build_pkt(void *umad, ib_rpc_t *rpc, ib_portid_t *dport,
172 ib_rmpp_hdr_t *rmpp, void *data)
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;
181 umad_set_addr(umad, dport->lid, dport->qp, dport->sl, dport->qkey);
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);
187 umad_set_addr(umad, 0xffff, 0, 0, 0);
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;
195 umad_set_grh(umad, &addr);
197 umad_set_grh(umad, 0);
198 umad_set_pkey(umad, is_smi ? 0 : dport->pkey_idx);
200 mad = umad_get_mad(umad);
201 p = mad_encode(mad, rpc, lid_routed ? 0 : &dport->drpath, data);
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);