2 * Copyright (c) 2004-2009 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 */
43 #include <infiniband/umad.h>
44 #include <infiniband/mad.h>
46 #include "mad_internal.h"
49 #define DEBUG if (ibdebug) IBWARN
51 int mad_send(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp,
54 return mad_send_via(rpc, dport, rmpp, data, ibmp);
57 int mad_send_via(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp,
58 void *data, struct ibmad_port *srcport)
63 memset(pktbuf, 0, umad_size() + IB_MAD_SIZE);
65 DEBUG("rmpp %p data %p", rmpp, data);
67 if (mad_build_pkt(umad, rpc, dport, rmpp, data) < 0)
71 IBWARN("data offs %d sz %d", rpc->dataoffs, rpc->datasz);
72 xdump(stderr, "mad send data\n",
73 (char *)umad_get_mad(umad) + rpc->dataoffs, rpc->datasz);
76 if (umad_send(srcport->port_id, srcport->class_agents[rpc->mgtclass & 0xff],
77 umad, IB_MAD_SIZE, mad_get_timeout(srcport, rpc->timeout),
79 IBWARN("send failed; %s", strerror(errno));
86 int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus)
88 return mad_respond_via(umad, portid, rstatus, ibmp);
91 int mad_respond_via(void *umad, ib_portid_t * portid, uint32_t rstatus,
92 struct ibmad_port *srcport)
94 uint8_t *mad = umad_get_mad(umad);
95 ib_mad_addr_t *mad_addr;
101 if (!(mad_addr = umad_get_mad_addr(umad))) {
106 memset(&rport, 0, sizeof(rport));
108 rport.lid = ntohs(mad_addr->lid);
109 rport.qp = ntohl(mad_addr->qpn);
110 rport.qkey = ntohl(mad_addr->qkey);
111 rport.sl = mad_addr->sl;
113 if (mad_addr->grh_present) {
114 rport.grh_present = 1;
115 memcpy(&rport.gid, &mad_addr->gid, sizeof(rport.gid));
121 DEBUG("dest %s", portid2str(portid));
123 rpc.mgtclass = mad_get_field(mad, 0, IB_MAD_MGMTCLASS_F);
125 rpc.method = mad_get_field(mad, 0, IB_MAD_METHOD_F);
126 if (rpc.method == IB_MAD_METHOD_SET)
127 rpc.method = IB_MAD_METHOD_GET;
128 if (rpc.method != IB_MAD_METHOD_SEND)
129 rpc.method |= IB_MAD_RESPONSE;
131 rpc.attr.id = mad_get_field(mad, 0, IB_MAD_ATTRID_F);
132 rpc.attr.mod = mad_get_field(mad, 0, IB_MAD_ATTRMOD_F);
133 if (rpc.mgtclass == IB_SA_CLASS)
134 rpc.recsz = mad_get_field(mad, 0, IB_SA_ATTROFFS_F);
135 if (mad_is_vendor_range2(rpc.mgtclass))
136 rpc.oui = mad_get_field(mad, 0, IB_VEND2_OUI_F);
138 rpc.trid = mad_get_field64(mad, 0, IB_MAD_TRID_F);
139 rpc.rstatus = rstatus;
141 /* cleared by default: timeout, datasz, dataoffs, mkey, mask */
143 is_smi = rpc.mgtclass == IB_SMI_CLASS ||
144 rpc.mgtclass == IB_SMI_DIRECT_CLASS;
148 else if (!portid->qp)
151 if (!portid->qkey && portid->qp == 1)
152 portid->qkey = IB_DEFAULT_QP1_QKEY;
155 ("qp 0x%x class 0x%x method %d attr 0x%x mod 0x%x datasz %d off %d qkey %x",
156 portid->qp, rpc.mgtclass, rpc.method, rpc.attr.id, rpc.attr.mod,
157 rpc.datasz, rpc.dataoffs, portid->qkey);
159 if (mad_build_pkt(umad, &rpc, portid, 0, 0) < 0)
163 xdump(stderr, "mad respond pkt\n", mad, IB_MAD_SIZE);
166 (srcport->port_id, srcport->class_agents[rpc.mgtclass], umad,
167 IB_MAD_SIZE, mad_get_timeout(srcport, rpc.timeout), 0) < 0) {
168 DEBUG("send failed; %s", strerror(errno));
175 void *mad_receive(void *umad, int timeout)
177 return mad_receive_via(umad, timeout, ibmp);
180 void *mad_receive_via(void *umad, int timeout, struct ibmad_port *srcport)
182 void *mad = umad ? umad : umad_alloc(1, umad_size() + IB_MAD_SIZE);
184 int length = IB_MAD_SIZE;
186 if ((agent = umad_recv(srcport->port_id, mad, &length,
187 mad_get_timeout(srcport, timeout))) < 0) {
190 DEBUG("recv failed: %s", strerror(errno));
197 void *mad_alloc(void)
199 return umad_alloc(1, umad_size() + IB_MAD_SIZE);
202 void mad_free(void *umad)