]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ofed/libibmad/mad.c
bhnd(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / contrib / ofed / libibmad / mad.c
1 /*
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.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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
32  * SOFTWARE.
33  *
34  */
35
36 #if HAVE_CONFIG_H
37 #  include <config.h>
38 #endif                          /* HAVE_CONFIG_H */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <errno.h>
45
46 #include <infiniband/umad.h>
47 #include <infiniband/mad.h>
48
49 #include "mad_internal.h"
50
51 #undef DEBUG
52 #define DEBUG   if (ibdebug)    IBWARN
53
54 #define GET_IB_USERLAND_TID(tid)        (tid & 0x00000000ffffffff)
55 /*
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.
60  */
61 uint64_t mad_trid(void)
62 {
63         static uint64_t trid;
64         uint64_t next;
65
66         if (!trid) {
67                 srandom((int)time(0) * getpid());
68                 trid = random();
69         }
70         next = ++trid;
71         next = GET_IB_USERLAND_TID(next);
72         return next;
73 }
74
75 int mad_get_timeout(const struct ibmad_port *srcport, int override_ms)
76 {
77         return (override_ms ? override_ms :
78                 srcport->timeout ? srcport->timeout : madrpc_timeout);
79 }
80
81 int mad_get_retries(const struct ibmad_port *srcport)
82 {
83         return (srcport->retries ? srcport->retries : madrpc_retries);
84 }
85
86 void *mad_encode(void *buf, ib_rpc_t * rpc, ib_dr_path_t * drpath, void *data)
87 {
88         int is_resp = rpc->method & IB_MAD_RESPONSE;
89         int mgtclass;
90
91         /* first word */
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);
97         else
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);
101
102         /* second word */
103         if ((rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS) {
104                 if (!drpath) {
105                         IBWARN("encoding dr mad without drpath (null)");
106                         errno = EINVAL;
107                         return NULL;
108                 }
109                 if (drpath->cnt >= IB_SUBNET_PATH_HOPS_MAX) {
110                         IBWARN("dr path with hop count %d", drpath->cnt);
111                         errno = EINVAL;
112                         return NULL;
113                 }
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 */
119         } else
120                 mad_set_field(buf, 0, IB_MAD_STATUS_F, rpc->rstatus);
121
122         /* words 3,4,5,6 */
123         if (!rpc->trid)
124                 rpc->trid = mad_trid();
125
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);
129
130         /* words 7,8 */
131         mad_set_field64(buf, 0, IB_MAD_MKEY_F, rpc->mkey);
132
133         if ((rpc->mgtclass & 0xff) == IB_SMI_DIRECT_CLASS) {
134                 /* word 9 */
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);
139
140                 /* bytes 128 - 256 - by default should be zero due to memset */
141                 if (is_resp)
142                         mad_set_array(buf, 0, IB_DRSMP_RPATH_F, drpath->p);
143                 else
144                         mad_set_array(buf, 0, IB_DRSMP_PATH_F, drpath->p);
145         }
146
147         if ((rpc->mgtclass & 0xff) == IB_SA_CLASS)
148                 mad_set_field64(buf, 0, IB_SA_COMPMASK_F, rpc->mask);
149
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);
153         }
154
155         if (data)
156                 memcpy((char *)buf + rpc->dataoffs, data, rpc->datasz);
157
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);
161
162         return (uint8_t *) buf + IB_MAD_SIZE;
163 }
164
165 int mad_build_pkt(void *umad, ib_rpc_t * rpc, ib_portid_t * dport,
166                   ib_rmpp_hdr_t * rmpp, void *data)
167 {
168         uint8_t *p, *mad;
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;
173
174         if (!is_smi)
175                 umad_set_addr(umad, dport->lid, dport->qp, dport->sl,
176                               dport->qkey);
177         else if (lid_routed)
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);
181         else
182                 umad_set_addr(umad, 0xffff, 0, 0, 0);
183
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;
189                 addr.flow_label = 0;
190                 umad_set_grh(umad, &addr);
191         } else
192                 umad_set_grh(umad, 0);
193         umad_set_pkey(umad, is_smi ? 0 : dport->pkey_idx);
194
195         mad = umad_get_mad(umad);
196         p = mad_encode(mad, rpc, lid_routed ? 0 : &dport->drpath, data);
197         if (!p)
198                 return -1;
199
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);
208         }
209
210         return ((int)(p - mad));
211 }