2 * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2009 HNR Consulting. All rights reserved.
4 * Copyright (c) 2011 Mellanox Technologies LTD. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #endif /* HAVE_CONFIG_H */
46 #include <infiniband/umad.h>
47 #include <infiniband/mad.h>
49 #include "mad_internal.h"
52 #define DEBUG if (ibdebug) IBWARN
54 #define GET_IB_USERLAND_TID(tid) (tid & 0x00000000ffffffff)
56 * Generate the 64 bit MAD transaction ID. The upper 32 bits are reserved for
57 * use by the kernel. We clear the upper 32 bits here, but MADs received from
58 * the kernel may contain kernel specific data in these bits, consequently
59 * userland TID matching should only be done on the lower 32 bits.
61 uint64_t mad_trid(void)
67 srandom((int)time(0) * getpid());
71 next = GET_IB_USERLAND_TID(next);
75 int mad_get_timeout(const struct ibmad_port *srcport, int override_ms)
77 return (override_ms ? override_ms :
78 srcport->timeout ? srcport->timeout : madrpc_timeout);
81 int mad_get_retries(const struct ibmad_port *srcport)
83 return (srcport->retries ? srcport->retries : madrpc_retries);
86 void *mad_encode(void *buf, ib_rpc_t * rpc, ib_dr_path_t * drpath, void *data)
88 int is_resp = rpc->method & IB_MAD_RESPONSE;
92 mad_set_field(buf, 0, IB_MAD_METHOD_F, rpc->method);
93 mad_set_field(buf, 0, IB_MAD_RESPONSE_F, is_resp ? 1 : 0);
94 mgtclass = rpc->mgtclass & 0xff;
95 if (mgtclass == IB_SA_CLASS || mgtclass == IB_CC_CLASS)
96 mad_set_field(buf, 0, IB_MAD_CLASSVER_F, 2);
98 mad_set_field(buf, 0, IB_MAD_CLASSVER_F, 1);
99 mad_set_field(buf, 0, IB_MAD_MGMTCLASS_F, rpc->mgtclass & 0xff);
100 mad_set_field(buf, 0, IB_MAD_BASEVER_F, 1);
103 if ((rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS) {
105 IBWARN("encoding dr mad without drpath (null)");
109 if (drpath->cnt >= IB_SUBNET_PATH_HOPS_MAX) {
110 IBWARN("dr path with hop count %d", drpath->cnt);
114 mad_set_field(buf, 0, IB_DRSMP_HOPCNT_F, drpath->cnt);
115 mad_set_field(buf, 0, IB_DRSMP_HOPPTR_F,
116 is_resp ? drpath->cnt + 1 : 0x0);
117 mad_set_field(buf, 0, IB_DRSMP_STATUS_F, rpc->rstatus);
118 mad_set_field(buf, 0, IB_DRSMP_DIRECTION_F, is_resp ? 1 : 0); /* out */
120 mad_set_field(buf, 0, IB_MAD_STATUS_F, rpc->rstatus);
124 rpc->trid = mad_trid();
126 mad_set_field64(buf, 0, IB_MAD_TRID_F, rpc->trid);
127 mad_set_field(buf, 0, IB_MAD_ATTRID_F, rpc->attr.id);
128 mad_set_field(buf, 0, IB_MAD_ATTRMOD_F, rpc->attr.mod);
131 mad_set_field64(buf, 0, IB_MAD_MKEY_F, rpc->mkey);
133 if ((rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS) {
135 mad_set_field(buf, 0, IB_DRSMP_DRDLID_F,
136 drpath->drdlid ? drpath->drdlid : 0xffff);
137 mad_set_field(buf, 0, IB_DRSMP_DRSLID_F,
138 drpath->drslid ? drpath->drslid : 0xffff);
140 /* bytes 128 - 256 - by default should be zero due to memset */
142 mad_set_array(buf, 0, IB_DRSMP_RPATH_F, drpath->p);
144 mad_set_array(buf, 0, IB_DRSMP_PATH_F, drpath->p);
147 if ((rpc->mgtclass & 0xff) == IB_SA_CLASS)
148 mad_set_field64(buf, 0, IB_SA_COMPMASK_F, rpc->mask);
150 if ((rpc->mgtclass & 0xff) == IB_CC_CLASS) {
151 ib_rpc_cc_t *rpccc = (ib_rpc_cc_t *)rpc;
152 mad_set_field64(buf, 0, IB_CC_CCKEY_F, rpccc->cckey);
156 memcpy((char *)buf + rpc->dataoffs, data, rpc->datasz);
158 /* vendor mads range 2 */
159 if (mad_is_vendor_range2(rpc->mgtclass & 0xff))
160 mad_set_field(buf, 0, IB_VEND2_OUI_F, rpc->oui);
162 return (uint8_t *) buf + IB_MAD_SIZE;
165 int mad_build_pkt(void *umad, ib_rpc_t * rpc, ib_portid_t * dport,
166 ib_rmpp_hdr_t * rmpp, void *data)
169 int lid_routed = (rpc->mgtclass & 0xff) != IB_SMI_DIRECT_CLASS;
170 int is_smi = ((rpc->mgtclass & 0xff) == IB_SMI_CLASS ||
171 (rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS);
172 struct ib_mad_addr addr;
175 umad_set_addr(umad, dport->lid, dport->qp, dport->sl,
178 umad_set_addr(umad, dport->lid, dport->qp, 0, 0);
179 else if ((dport->drpath.drslid != 0xffff) && (dport->lid > 0))
180 umad_set_addr(umad, dport->lid, 0, 0, 0);
182 umad_set_addr(umad, 0xffff, 0, 0, 0);
184 if (dport->grh_present && !is_smi) {
185 addr.grh_present = 1;
186 memcpy(addr.gid, dport->gid, 16);
187 addr.hop_limit = 0xff;
188 addr.traffic_class = 0;
190 umad_set_grh(umad, &addr);
192 umad_set_grh(umad, 0);
193 umad_set_pkey(umad, is_smi ? 0 : dport->pkey_idx);
195 mad = umad_get_mad(umad);
196 p = mad_encode(mad, rpc, lid_routed ? 0 : &dport->drpath, data);
200 if (!is_smi && rmpp) {
201 mad_set_field(mad, 0, IB_SA_RMPP_VERS_F, 1);
202 mad_set_field(mad, 0, IB_SA_RMPP_TYPE_F, rmpp->type);
203 mad_set_field(mad, 0, IB_SA_RMPP_RESP_F, 0x3f);
204 mad_set_field(mad, 0, IB_SA_RMPP_FLAGS_F, rmpp->flags);
205 mad_set_field(mad, 0, IB_SA_RMPP_STATUS_F, rmpp->status);
206 mad_set_field(mad, 0, IB_SA_RMPP_D1_F, rmpp->d1.u);
207 mad_set_field(mad, 0, IB_SA_RMPP_D2_F, rmpp->d2.u);
210 return ((int)(p - mad));