xref: /freebsd/contrib/ofed/libibmad/serv.c (revision 4f52dfbb)
1 /*
2  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  *
32  */
33 
34 #if HAVE_CONFIG_H
35 #  include <config.h>
36 #endif				/* HAVE_CONFIG_H */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 
43 #include <infiniband/umad.h>
44 #include <infiniband/mad.h>
45 
46 #include "mad_internal.h"
47 
48 #undef DEBUG
49 #define DEBUG	if (ibdebug)	IBWARN
50 
51 int mad_send(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp,
52 	     void *data)
53 {
54 	return mad_send_via(rpc, dport, rmpp, data, ibmp);
55 }
56 
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)
59 {
60 	uint8_t pktbuf[1024];
61 	void *umad = pktbuf;
62 
63 	memset(pktbuf, 0, umad_size() + IB_MAD_SIZE);
64 
65 	DEBUG("rmpp %p data %p", rmpp, data);
66 
67 	if (mad_build_pkt(umad, rpc, dport, rmpp, data) < 0)
68 		return -1;
69 
70 	if (ibdebug) {
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);
74 	}
75 
76 	if (umad_send(srcport->port_id, srcport->class_agents[rpc->mgtclass & 0xff],
77 		      umad, IB_MAD_SIZE, mad_get_timeout(srcport, rpc->timeout),
78 		      0) < 0) {
79 		IBWARN("send failed; %s", strerror(errno));
80 		return -1;
81 	}
82 
83 	return 0;
84 }
85 
86 int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus)
87 {
88 	return mad_respond_via(umad, portid, rstatus, ibmp);
89 }
90 
91 int mad_respond_via(void *umad, ib_portid_t * portid, uint32_t rstatus,
92 		    struct ibmad_port *srcport)
93 {
94 	uint8_t *mad = umad_get_mad(umad);
95 	ib_mad_addr_t *mad_addr;
96 	ib_rpc_t rpc = { 0 };
97 	ib_portid_t rport;
98 	int is_smi;
99 
100 	if (!portid) {
101 		if (!(mad_addr = umad_get_mad_addr(umad))) {
102 			errno = EINVAL;
103 			return -1;
104 		}
105 
106 		memset(&rport, 0, sizeof(rport));
107 
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;
112 
113 		if (mad_addr->grh_present) {
114 			rport.grh_present = 1;
115 			memcpy(&rport.gid, &mad_addr->gid, sizeof(rport.gid));
116 		}
117 
118 		portid = &rport;
119 	}
120 
121 	DEBUG("dest %s", portid2str(portid));
122 
123 	rpc.mgtclass = mad_get_field(mad, 0, IB_MAD_MGMTCLASS_F);
124 
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;
130 
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);
137 
138 	rpc.trid = mad_get_field64(mad, 0, IB_MAD_TRID_F);
139 	rpc.rstatus = rstatus;
140 
141 	/* cleared by default: timeout, datasz, dataoffs, mkey, mask */
142 
143 	is_smi = rpc.mgtclass == IB_SMI_CLASS ||
144 	    rpc.mgtclass == IB_SMI_DIRECT_CLASS;
145 
146 	if (is_smi)
147 		portid->qp = 0;
148 	else if (!portid->qp)
149 		portid->qp = 1;
150 
151 	if (!portid->qkey && portid->qp == 1)
152 		portid->qkey = IB_DEFAULT_QP1_QKEY;
153 
154 	DEBUG
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);
158 
159 	if (mad_build_pkt(umad, &rpc, portid, 0, 0) < 0)
160 		return -1;
161 
162 	if (ibdebug > 1)
163 		xdump(stderr, "mad respond pkt\n", mad, IB_MAD_SIZE);
164 
165 	if (umad_send
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));
169 		return -1;
170 	}
171 
172 	return 0;
173 }
174 
175 void *mad_receive(void *umad, int timeout)
176 {
177 	return mad_receive_via(umad, timeout, ibmp);
178 }
179 
180 void *mad_receive_via(void *umad, int timeout, struct ibmad_port *srcport)
181 {
182 	void *mad = umad ? umad : umad_alloc(1, umad_size() + IB_MAD_SIZE);
183 	int agent;
184 	int length = IB_MAD_SIZE;
185 
186 	if ((agent = umad_recv(srcport->port_id, mad, &length,
187 			       mad_get_timeout(srcport, timeout))) < 0) {
188 		if (!umad)
189 			umad_free(mad);
190 		DEBUG("recv failed: %s", strerror(errno));
191 		return 0;
192 	}
193 
194 	return mad;
195 }
196 
197 void *mad_alloc(void)
198 {
199 	return umad_alloc(1, umad_size() + IB_MAD_SIZE);
200 }
201 
202 void mad_free(void *umad)
203 {
204 	umad_free(umad);
205 }
206