1/*
2 * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3 * Copyright (c) 2014 Intel Corporation.  All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 *     Redistribution and use in source and binary forms, with or
12 *     without modification, are permitted provided that the following
13 *     conditions are met:
14 *
15 *      - Redistributions of source code must retain the above
16 *        copyright notice, this list of conditions and the following
17 *        disclaimer.
18 *
19 *      - Redistributions in binary form must reproduce the above
20 *        copyright notice, this list of conditions and the following
21 *        disclaimer in the documentation and/or other materials
22 *        provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 */
34#ifndef _UMAD_H
35#define _UMAD_H
36
37#include <stdint.h>
38#include <stdlib.h>
39#include <arpa/inet.h>
40
41#ifdef _KERNEL
42#include <sys/endian.h>
43#include <linux/types.h> /* __be16, __be32 and __be64 */
44#else
45#include <infiniband/endian.h>
46#include <infiniband/types.h>
47#endif
48
49#ifdef __cplusplus
50#  define BEGIN_C_DECLS extern "C" {
51#  define END_C_DECLS   }
52#else				/* !__cplusplus */
53#  define BEGIN_C_DECLS
54#  define END_C_DECLS
55#endif				/* __cplusplus */
56
57BEGIN_C_DECLS
58
59typedef __be16 __attribute__((deprecated)) be16_t;
60typedef __be32 __attribute__((deprecated)) be32_t;
61typedef __be64 __attribute__((deprecated)) be64_t;
62
63/*
64 * A GID data structure that may be used in definitions of on-the-wire data
65 * structures. Do not cast umad_gid pointers to ibv_gid pointers because the
66 * alignment of these two data structures is different.
67 */
68union umad_gid {
69	uint8_t		raw[16];
70	__be16		raw_be16[8];
71	struct {
72		__be64	subnet_prefix;
73		__be64	interface_id;
74	} global;
75} __attribute__((aligned(4))) __attribute__((packed));
76
77#define UMAD_MAX_DEVICES 32
78#define UMAD_ANY_PORT	0
79typedef struct ib_mad_addr {
80	__be32 qpn;
81	__be32 qkey;
82	__be16 lid;
83	uint8_t sl;
84	uint8_t path_bits;
85	uint8_t grh_present;
86	uint8_t gid_index;
87	uint8_t hop_limit;
88	uint8_t traffic_class;
89	union {
90		uint8_t		gid[16]; /* network-byte order */
91		union umad_gid	ib_gid;
92	};
93	__be32 flow_label;
94	uint16_t pkey_index;
95	uint8_t reserved[6];
96} ib_mad_addr_t;
97
98typedef struct ib_user_mad {
99	uint32_t agent_id;
100	uint32_t status;
101	uint32_t timeout_ms;
102	uint32_t retries;
103	uint32_t length;
104	ib_mad_addr_t addr;
105	uint8_t data[0];
106} ib_user_mad_t;
107
108#define IB_UMAD_ABI_VERSION	5
109#define IB_UMAD_ABI_DIR		"/sys/class/infiniband_mad"
110#define IB_UMAD_ABI_FILE	"abi_version"
111
112#define IB_IOCTL_MAGIC		0x1b
113
114#define IB_USER_MAD_REGISTER_AGENT \
115	_IOWR(IB_IOCTL_MAGIC, 1, uint8_t [28] /* struct ib_user_mad_reg_req */)
116#define IB_USER_MAD_UNREGISTER_AGENT	_IOW(IB_IOCTL_MAGIC, 2, uint32_t)
117#define IB_USER_MAD_ENABLE_PKEY		_IO(IB_IOCTL_MAGIC, 3)
118#define IB_USER_MAD_REGISTER_AGENT2 \
119	_IOWR(IB_IOCTL_MAGIC, 4, uint8_t [40] /* struct ib_user_mad_reg_req2 */)
120
121#define UMAD_CA_NAME_LEN	20
122#define UMAD_CA_MAX_PORTS	10	/* 0 - 9 */
123#define UMAD_CA_MAX_AGENTS	32
124
125#define SYS_INFINIBAND		"/sys/class/infiniband"
126
127#define SYS_INFINIBAND_MAD	"/sys/class/infiniband_mad"
128#define SYS_IB_MAD_PORT		"port"
129#define SYS_IB_MAD_DEV		"ibdev"
130
131#define UMAD_MAX_PORTS		64
132
133#define UMAD_DEV_DIR		"/dev"
134
135#define SYS_CA_PORTS_DIR	"ports"
136
137#define SYS_NODE_TYPE		"node_type"
138#define SYS_CA_FW_VERS		"fw_ver"
139#define SYS_CA_HW_VERS		"hw_rev"
140#define SYS_CA_TYPE		"hca_type"
141#define SYS_CA_NODE_GUID	"node_guid"
142#define SYS_CA_SYS_GUID		"sys_image_guid"
143
144#define SYS_PORT_LMC		"lid_mask_count"
145#define SYS_PORT_SMLID		"sm_lid"
146#define SYS_PORT_SMSL		"sm_sl"
147#define SYS_PORT_LID		"lid"
148#define SYS_PORT_STATE		"state"
149#define SYS_PORT_PHY_STATE	"phys_state"
150#define SYS_PORT_CAPMASK	"cap_mask"
151#define SYS_PORT_RATE		"rate"
152#define SYS_PORT_GUID		"port_guid"
153#define SYS_PORT_GID		"gids/0"
154#define SYS_PORT_LINK_LAYER	"link_layer"
155
156typedef struct umad_port {
157	char ca_name[UMAD_CA_NAME_LEN];
158	int portnum;
159	unsigned base_lid;
160	unsigned lmc;
161	unsigned sm_lid;
162	unsigned sm_sl;
163	unsigned state;
164	unsigned phys_state;
165	unsigned rate;
166	__be32 capmask;
167	__be64	 gid_prefix;
168	__be64	 port_guid;
169	unsigned pkeys_size;
170	uint16_t *pkeys;
171	char link_layer[UMAD_CA_NAME_LEN];
172} umad_port_t;
173
174typedef struct umad_ca {
175	char ca_name[UMAD_CA_NAME_LEN];
176	unsigned node_type;
177	int numports;
178	char fw_ver[20];
179	char ca_type[40];
180	char hw_ver[20];
181	__be64 node_guid;
182	__be64 system_guid;
183	umad_port_t *ports[UMAD_CA_MAX_PORTS];
184} umad_ca_t;
185
186int umad_init(void);
187int umad_done(void);
188
189int umad_get_cas_names(char cas[][UMAD_CA_NAME_LEN], int max);
190int umad_get_ca_portguids(const char *ca_name, __be64 *portguids, int max);
191
192int umad_get_ca(const char *ca_name, umad_ca_t * ca);
193int umad_release_ca(umad_ca_t * ca);
194int umad_get_port(const char *ca_name, int portnum, umad_port_t * port);
195int umad_release_port(umad_port_t * port);
196
197int umad_get_issm_path(const char *ca_name, int portnum, char path[], int max);
198
199int umad_open_port(const char *ca_name, int portnum);
200int umad_close_port(int portid);
201
202void *umad_get_mad(void *umad);
203size_t umad_size(void);
204int umad_status(void *umad);
205
206ib_mad_addr_t *umad_get_mad_addr(void *umad);
207int umad_set_grh_net(void *umad, void *mad_addr);
208int umad_set_grh(void *umad, void *mad_addr);
209int umad_set_addr_net(void *umad, __be16 dlid, __be32 dqp, int sl, __be32 qkey);
210int umad_set_addr(void *umad, int dlid, int dqp, int sl, int qkey);
211int umad_set_pkey(void *umad, int pkey_index);
212int umad_get_pkey(void *umad);
213
214int umad_send(int portid, int agentid, void *umad, int length,
215	      int timeout_ms, int retries);
216int umad_recv(int portid, void *umad, int *length, int timeout_ms);
217int umad_poll(int portid, int timeout_ms);
218int umad_get_fd(int portid);
219
220int umad_register(int portid, int mgmt_class, int mgmt_version,
221		  uint8_t rmpp_version, long method_mask[16 / sizeof(long)]);
222int umad_register_oui(int portid, int mgmt_class, uint8_t rmpp_version,
223		      uint8_t oui[3], long method_mask[16 / sizeof(long)]);
224int umad_unregister(int portid, int agentid);
225
226enum {
227	UMAD_USER_RMPP = (1 << 0)
228};
229
230struct umad_reg_attr {
231	uint8_t    mgmt_class;
232	uint8_t    mgmt_class_version;
233	uint32_t   flags;
234	uint64_t   method_mask[2];
235	uint32_t   oui;
236	uint8_t    rmpp_version;
237};
238
239int umad_register2(int port_fd, struct umad_reg_attr *attr,
240		   uint32_t *agent_id);
241
242int umad_debug(int level);
243void umad_addr_dump(ib_mad_addr_t * addr);
244void umad_dump(void *umad);
245
246static inline void *umad_alloc(int num, size_t size)
247{				/* alloc array of umad buffers */
248	return calloc(num, size);
249}
250
251static inline void umad_free(void *umad)
252{
253	free(umad);
254}
255
256/* Users should use the glibc functions directly, not these wrappers */
257#ifndef ntohll
258#undef ntohll
259static inline __attribute__((deprecated)) uint64_t ntohll(uint64_t x) { return be64toh(x); }
260#define ntohll ntohll
261#endif
262#ifndef htonll
263#undef htonll
264static inline __attribute__((deprecated)) uint64_t htonll(uint64_t x) { return htobe64(x); }
265#define htonll htonll
266#endif
267
268END_C_DECLS
269#endif				/* _UMAD_H */
270