2 * Copyright (c) 2004,2005 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 */
44 #include <netinet/in.h>
46 #include <infiniband/common.h>
47 #include <infiniband/umad.h>
51 #define DEBUG if (ibdebug) IBWARN
54 mad_send(ib_rpc_t *rpc, ib_portid_t *dport, ib_rmpp_hdr_t *rmpp, void *data)
59 memset(pktbuf, 0, umad_size());
61 DEBUG("rmpp %p data %p", rmpp, data);
63 if (mad_build_pkt(umad, rpc, dport, rmpp, data) < 0)
67 IBWARN("data offs %d sz %d", rpc->dataoffs, rpc->datasz);
68 xdump(stderr, "mad send data\n",
69 (char *)umad_get_mad(umad) + rpc->dataoffs, rpc->datasz);
72 if (umad_send(madrpc_portid(), mad_class_agent(rpc->mgtclass),
73 umad, IB_MAD_SIZE, rpc->timeout, 0) < 0) {
74 IBWARN("send failed; %m");
82 mad_respond(void *umad, ib_portid_t *portid, uint32_t rstatus)
84 uint8_t *mad = umad_get_mad(umad);
85 ib_mad_addr_t *mad_addr;
91 if (!(mad_addr = umad_get_mad_addr(umad)))
94 memset(&rport, 0, sizeof(rport));
96 rport.lid = ntohs(mad_addr->lid);
97 rport.qp = ntohl(mad_addr->qpn);
98 rport.qkey = ntohl(mad_addr->qkey);
99 rport.sl = mad_addr->sl;
104 DEBUG("dest %s", portid2str(portid));
106 rpc.mgtclass = mad_get_field(mad, 0, IB_MAD_MGMTCLASS_F);
108 rpc.method = mad_get_field(mad, 0, IB_MAD_METHOD_F);
109 if (rpc.method == IB_MAD_METHOD_SET)
110 rpc.method = IB_MAD_METHOD_GET;
111 if (rpc.method != IB_MAD_METHOD_SEND)
112 rpc.method |= IB_MAD_RESPONSE;
114 rpc.attr.id = mad_get_field(mad, 0, IB_MAD_ATTRID_F);
115 rpc.attr.mod = mad_get_field(mad, 0, IB_MAD_ATTRMOD_F);
116 if (rpc.mgtclass == IB_SA_CLASS)
117 rpc.recsz = mad_get_field(mad, 0, IB_SA_ATTROFFS_F);
118 if (mad_is_vendor_range2(rpc.mgtclass))
119 rpc.oui = mad_get_field(mad, 0, IB_VEND2_OUI_F);
121 rpc.trid = mad_get_field64(mad, 0, IB_MAD_TRID_F);
123 /* cleared by default: timeout, datasz, dataoffs, mkey, mask */
125 is_smi = rpc.mgtclass == IB_SMI_CLASS ||
126 rpc.mgtclass == IB_SMI_DIRECT_CLASS;
130 else if (!portid->qp)
133 if (!portid->qkey && portid->qp == 1)
134 portid->qkey = IB_DEFAULT_QP1_QKEY;
136 DEBUG("qp 0x%x class 0x%x method %d attr 0x%x mod 0x%x datasz %d off %d qkey %x",
137 portid->qp, rpc.mgtclass, rpc.method, rpc.attr.id, rpc.attr.mod,
138 rpc.datasz, rpc.dataoffs, portid->qkey);
140 if (mad_build_pkt(umad, &rpc, portid, 0, 0) < 0)
144 xdump(stderr, "mad respond pkt\n", mad, IB_MAD_SIZE);
146 if (umad_send(madrpc_portid(), mad_class_agent(rpc.mgtclass), umad,
147 IB_MAD_SIZE, rpc.timeout, 0) < 0) {
148 DEBUG("send failed; %m");
156 mad_receive(void *umad, int timeout)
158 void *mad = umad ? umad : umad_alloc(1, umad_size() + IB_MAD_SIZE);
160 int length = IB_MAD_SIZE;
162 if ((agent = umad_recv(madrpc_portid(), mad,
163 &length, timeout)) < 0) {
166 DEBUG("recv failed: %m");
176 return umad_alloc(1, umad_size() + IB_MAD_SIZE);