xref: /freebsd/contrib/ofed/libibmad/bm.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3*d6b92ffaSHans Petter Selasky  *
4*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
5*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
6*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
7*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
8*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
9*d6b92ffaSHans Petter Selasky  *
10*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
11*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
12*d6b92ffaSHans Petter Selasky  *     conditions are met:
13*d6b92ffaSHans Petter Selasky  *
14*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
15*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
16*d6b92ffaSHans Petter Selasky  *        disclaimer.
17*d6b92ffaSHans Petter Selasky  *
18*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
19*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
20*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
21*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
22*d6b92ffaSHans Petter Selasky  *
23*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*d6b92ffaSHans Petter Selasky  * SOFTWARE.
31*d6b92ffaSHans Petter Selasky  *
32*d6b92ffaSHans Petter Selasky  */
33*d6b92ffaSHans Petter Selasky 
34*d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
35*d6b92ffaSHans Petter Selasky #  include <config.h>
36*d6b92ffaSHans Petter Selasky #endif				/* HAVE_CONFIG_H */
37*d6b92ffaSHans Petter Selasky 
38*d6b92ffaSHans Petter Selasky #include <string.h>
39*d6b92ffaSHans Petter Selasky 
40*d6b92ffaSHans Petter Selasky #include <infiniband/mad.h>
41*d6b92ffaSHans Petter Selasky 
42*d6b92ffaSHans Petter Selasky #undef DEBUG
43*d6b92ffaSHans Petter Selasky #define DEBUG 	if (ibdebug)	IBWARN
44*d6b92ffaSHans Petter Selasky 
response_expected(int method)45*d6b92ffaSHans Petter Selasky static inline int response_expected(int method)
46*d6b92ffaSHans Petter Selasky {
47*d6b92ffaSHans Petter Selasky 	return method == IB_MAD_METHOD_GET ||
48*d6b92ffaSHans Petter Selasky 	    method == IB_MAD_METHOD_SET || method == IB_MAD_METHOD_TRAP;
49*d6b92ffaSHans Petter Selasky }
50*d6b92ffaSHans Petter Selasky 
bm_call_via(void * data,ib_portid_t * portid,ib_bm_call_t * call,struct ibmad_port * srcport)51*d6b92ffaSHans Petter Selasky uint8_t *bm_call_via(void *data, ib_portid_t * portid, ib_bm_call_t * call,
52*d6b92ffaSHans Petter Selasky 		     struct ibmad_port * srcport)
53*d6b92ffaSHans Petter Selasky {
54*d6b92ffaSHans Petter Selasky 	ib_rpc_t rpc = { 0 };
55*d6b92ffaSHans Petter Selasky 	int resp_expected;
56*d6b92ffaSHans Petter Selasky 	struct {
57*d6b92ffaSHans Petter Selasky 		uint64_t bkey;
58*d6b92ffaSHans Petter Selasky 		uint8_t reserved[32];
59*d6b92ffaSHans Petter Selasky 		uint8_t data[IB_BM_DATA_SZ];
60*d6b92ffaSHans Petter Selasky 	} bm_data;
61*d6b92ffaSHans Petter Selasky 
62*d6b92ffaSHans Petter Selasky 	DEBUG("route %s data %p", portid2str(portid), data);
63*d6b92ffaSHans Petter Selasky 	if (portid->lid <= 0) {
64*d6b92ffaSHans Petter Selasky 		IBWARN("only lid routes are supported");
65*d6b92ffaSHans Petter Selasky 		return NULL;
66*d6b92ffaSHans Petter Selasky 	}
67*d6b92ffaSHans Petter Selasky 
68*d6b92ffaSHans Petter Selasky 	resp_expected = response_expected(call->method);
69*d6b92ffaSHans Petter Selasky 
70*d6b92ffaSHans Petter Selasky 	rpc.mgtclass = IB_BOARD_MGMT_CLASS;
71*d6b92ffaSHans Petter Selasky 
72*d6b92ffaSHans Petter Selasky 	rpc.method = call->method;
73*d6b92ffaSHans Petter Selasky 	rpc.attr.id = call->attrid;
74*d6b92ffaSHans Petter Selasky 	rpc.attr.mod = call->mod;
75*d6b92ffaSHans Petter Selasky 	rpc.timeout = resp_expected ? call->timeout : 0;
76*d6b92ffaSHans Petter Selasky 	// send data and bkey
77*d6b92ffaSHans Petter Selasky 	rpc.datasz = IB_BM_BKEY_AND_DATA_SZ;
78*d6b92ffaSHans Petter Selasky 	rpc.dataoffs = IB_BM_BKEY_OFFS;
79*d6b92ffaSHans Petter Selasky 
80*d6b92ffaSHans Petter Selasky 	// copy data to a buffer which also includes the bkey
81*d6b92ffaSHans Petter Selasky 	bm_data.bkey = htonll(call->bkey);
82*d6b92ffaSHans Petter Selasky 	memset(bm_data.reserved, 0, sizeof(bm_data.reserved));
83*d6b92ffaSHans Petter Selasky 	memcpy(bm_data.data, data, IB_BM_DATA_SZ);
84*d6b92ffaSHans Petter Selasky 
85*d6b92ffaSHans Petter Selasky 	DEBUG
86*d6b92ffaSHans Petter Selasky 	    ("method 0x%x attr 0x%x mod 0x%x datasz %d off %d res_ex %d bkey 0x%08x%08x",
87*d6b92ffaSHans Petter Selasky 	     rpc.method, rpc.attr.id, rpc.attr.mod, rpc.datasz, rpc.dataoffs,
88*d6b92ffaSHans Petter Selasky 	     resp_expected, (int)(call->bkey >> 32), (int)call->bkey);
89*d6b92ffaSHans Petter Selasky 
90*d6b92ffaSHans Petter Selasky 	portid->qp = 1;
91*d6b92ffaSHans Petter Selasky 	if (!portid->qkey)
92*d6b92ffaSHans Petter Selasky 		portid->qkey = IB_DEFAULT_QP1_QKEY;
93*d6b92ffaSHans Petter Selasky 
94*d6b92ffaSHans Petter Selasky 	if (resp_expected) {
95*d6b92ffaSHans Petter Selasky 		/* FIXME: no RMPP for now */
96*d6b92ffaSHans Petter Selasky 		if (mad_rpc(srcport, &rpc, portid, &bm_data, &bm_data))
97*d6b92ffaSHans Petter Selasky 			goto return_ok;
98*d6b92ffaSHans Petter Selasky 		return NULL;
99*d6b92ffaSHans Petter Selasky 	}
100*d6b92ffaSHans Petter Selasky 
101*d6b92ffaSHans Petter Selasky 	if (mad_send_via(&rpc, portid, 0, &bm_data, srcport) < 0)
102*d6b92ffaSHans Petter Selasky 		return NULL;
103*d6b92ffaSHans Petter Selasky 
104*d6b92ffaSHans Petter Selasky return_ok:
105*d6b92ffaSHans Petter Selasky 	memcpy(data, bm_data.data, IB_BM_DATA_SZ);
106*d6b92ffaSHans Petter Selasky 	return data;
107*d6b92ffaSHans Petter Selasky }
108