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 */
40 #include <infiniband/umad.h>
44 #define DEBUG if (ibdebug) IBWARN
54 srandom((int)time(0)*getpid());
58 next = ++trid | (base << 32);
63 mad_encode(void *buf, ib_rpc_t *rpc, ib_dr_path_t *drpath, void *data)
65 int is_resp = rpc->method & IB_MAD_RESPONSE;
68 mad_set_field(buf, 0, IB_MAD_METHOD_F, rpc->method);
69 mad_set_field(buf, 0, IB_MAD_RESPONSE_F, is_resp ? 1 : 0);
70 mad_set_field(buf, 0, IB_MAD_CLASSVER_F, rpc->mgtclass == IB_SA_CLASS ? 2 : 1);
71 mad_set_field(buf, 0, IB_MAD_MGMTCLASS_F, rpc->mgtclass);
72 mad_set_field(buf, 0, IB_MAD_BASEVER_F, 1);
75 if (rpc->mgtclass == IB_SMI_DIRECT_CLASS) {
77 IBWARN("encoding dr mad without drpath (null)");
80 mad_set_field(buf, 0, IB_DRSMP_HOPCNT_F, drpath->cnt);
81 mad_set_field(buf, 0, IB_DRSMP_HOPPTR_F, is_resp ? drpath->cnt + 1 : 0x0);
82 mad_set_field(buf, 0, IB_DRSMP_STATUS_F, rpc->rstatus);
83 mad_set_field(buf, 0, IB_DRSMP_DIRECTION_F, is_resp ? 1 : 0); /* out */
85 mad_set_field(buf, 0, IB_MAD_STATUS_F, rpc->rstatus);
89 rpc->trid = mad_trid();
91 mad_set_field64(buf, 0, IB_MAD_TRID_F, rpc->trid);
92 mad_set_field(buf, 0, IB_MAD_ATTRID_F, rpc->attr.id);
93 mad_set_field(buf, 0, IB_MAD_ATTRMOD_F, rpc->attr.mod);
96 mad_set_field(buf, 0, IB_MAD_MKEY_F, (uint32_t)(rpc->mkey >> 32));
97 mad_set_field(buf, 4, IB_MAD_MKEY_F, (uint32_t)(rpc->mkey & 0xffffffff));
99 if (rpc->mgtclass == IB_SMI_DIRECT_CLASS) {
101 mad_set_field(buf, 0, IB_DRSMP_DRDLID_F, drpath->drdlid ? drpath->drdlid : 0xffff);
102 mad_set_field(buf, 0, IB_DRSMP_DRSLID_F, drpath->drslid ? drpath->drslid : 0xffff);
104 /* bytes 128 - 256 - by default should be zero due to memset*/
106 mad_set_array(buf, 0, IB_DRSMP_RPATH_F, drpath->p);
108 mad_set_array(buf, 0, IB_DRSMP_PATH_F, drpath->p);
111 if (rpc->mgtclass == IB_SA_CLASS)
112 mad_set_field64(buf, 0, IB_SA_COMPMASK_F, rpc->mask);
115 memcpy((char *)buf + rpc->dataoffs, data, rpc->datasz);
117 /* vendor mads range 2 */
118 if (mad_is_vendor_range2(rpc->mgtclass))
119 mad_set_field(buf, 0, IB_VEND2_OUI_F, rpc->oui);
121 return (uint8_t *)buf + IB_MAD_SIZE;
125 mad_build_pkt(void *umad, ib_rpc_t *rpc, ib_portid_t *dport,
126 ib_rmpp_hdr_t *rmpp, void *data)
129 int lid_routed = rpc->mgtclass != IB_SMI_DIRECT_CLASS;
130 int is_smi = (rpc->mgtclass == IB_SMI_CLASS ||
131 rpc->mgtclass == IB_SMI_DIRECT_CLASS);
132 struct ib_mad_addr addr;
135 umad_set_addr(umad, dport->lid, dport->qp, dport->sl, dport->qkey);
137 umad_set_addr(umad, dport->lid, dport->qp, 0, 0);
138 else if ((dport->drpath.drslid != 0xffff) && (dport->lid > 0))
139 umad_set_addr(umad, dport->lid, 0, 0, 0);
141 umad_set_addr(umad, 0xffff, 0, 0, 0);
143 if (dport->grh_present && !is_smi) {
144 addr.grh_present = 1;
145 memcpy(addr.gid, dport->gid, 16);
146 addr.hop_limit = 0xff;
147 addr.traffic_class = 0;
149 umad_set_grh(umad, &addr);
151 umad_set_grh(umad, 0);
152 umad_set_pkey(umad, is_smi ? 0 : dport->pkey_idx);
154 mad = umad_get_mad(umad);
155 p = mad_encode(mad, rpc, lid_routed ? 0 : &dport->drpath, data);
157 if (!is_smi && rmpp) {
158 mad_set_field(mad, 0, IB_SA_RMPP_VERS_F, 1);
159 mad_set_field(mad, 0, IB_SA_RMPP_TYPE_F, rmpp->type);
160 mad_set_field(mad, 0, IB_SA_RMPP_RESP_F, 0x3f);
161 mad_set_field(mad, 0, IB_SA_RMPP_FLAGS_F, rmpp->flags);
162 mad_set_field(mad, 0, IB_SA_RMPP_STATUS_F, rmpp->status);
163 mad_set_field(mad, 0, IB_SA_RMPP_D1_F, rmpp->d1.u);
164 mad_set_field(mad, 0, IB_SA_RMPP_D2_F, rmpp->d2.u);
167 return ((int)(p - mad));