xref: /freebsd/contrib/ofed/libibverbs/driver.h (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 Cisco Systems, Inc.  All rights reserved.
4  * Copyright (c) 2005 PathScale, Inc.  All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef INFINIBAND_DRIVER_H
36 #define INFINIBAND_DRIVER_H
37 
38 #include <infiniband/verbs.h>
39 #include <infiniband/kern-abi.h>
40 
41 #ifdef __cplusplus
42 #  define BEGIN_C_DECLS extern "C" {
43 #  define END_C_DECLS   }
44 #else /* !__cplusplus */
45 #  define BEGIN_C_DECLS
46 #  define END_C_DECLS
47 #endif /* __cplusplus */
48 
49 /*
50  * Extension that low-level drivers should add to their .so filename
51  * (probably via libtool "-release" option).  For example a low-level
52  * driver named "libfoo" should build a plug-in named "libfoo-rdmav2.so".
53  */
54 #define IBV_DEVICE_LIBRARY_EXTENSION rdmav2
55 
56 struct verbs_device;
57 
58 enum verbs_xrcd_mask {
59 	VERBS_XRCD_HANDLE	= 1 << 0,
60 	VERBS_XRCD_RESERVED	= 1 << 1
61 };
62 
63 struct verbs_xrcd {
64 	struct ibv_xrcd		xrcd;
65 	uint32_t		comp_mask;
66 	uint32_t		handle;
67 };
68 
69 enum verbs_srq_mask {
70 	VERBS_SRQ_TYPE		= 1 << 0,
71 	VERBS_SRQ_XRCD		= 1 << 1,
72 	VERBS_SRQ_CQ		= 1 << 2,
73 	VERBS_SRQ_NUM		= 1 << 3,
74 	VERBS_SRQ_RESERVED	= 1 << 4
75 };
76 
77 struct verbs_srq {
78 	struct ibv_srq		srq;
79 	uint32_t		comp_mask;
80 	enum ibv_srq_type	srq_type;
81 	struct verbs_xrcd      *xrcd;
82 	struct ibv_cq	       *cq;
83 	uint32_t		srq_num;
84 };
85 
86 enum verbs_qp_mask {
87 	VERBS_QP_XRCD		= 1 << 0,
88 	VERBS_QP_RESERVED	= 1 << 1
89 };
90 
91 enum ibv_gid_type {
92 	IBV_GID_TYPE_IB_ROCE_V1,
93 	IBV_GID_TYPE_ROCE_V2,
94 };
95 
96 struct verbs_qp {
97 	struct ibv_qp		qp;
98 	uint32_t		comp_mask;
99 	struct verbs_xrcd       *xrcd;
100 };
101 
102 /* Must change the PRIVATE IBVERBS_PRIVATE_ symbol if this is changed */
103 struct verbs_device_ops {
104 	/* Old interface, do not use in new code. */
105 	struct ibv_context *(*alloc_context)(struct ibv_device *device,
106 					     int cmd_fd);
107 	void (*free_context)(struct ibv_context *context);
108 
109 	/* New interface */
110 	int (*init_context)(struct verbs_device *device,
111 			    struct ibv_context *ctx, int cmd_fd);
112 	void (*uninit_context)(struct verbs_device *device,
113 			       struct ibv_context *ctx);
114 };
115 
116 /* Must change the PRIVATE IBVERBS_PRIVATE_ symbol if this is changed */
117 struct verbs_device {
118 	struct ibv_device device; /* Must be first */
119 	const struct verbs_device_ops *ops;
120 	size_t	sz;
121 	size_t	size_of_context;
122 };
123 
124 static inline struct verbs_device *
125 verbs_get_device(const struct ibv_device *dev)
126 {
127 	return container_of(dev, struct verbs_device, device);
128 }
129 
130 typedef struct verbs_device *(*verbs_driver_init_func)(const char *uverbs_sys_path,
131 						       int abi_version);
132 void verbs_register_driver(const char *name, verbs_driver_init_func init_func);
133 int verbs_init_cq(struct ibv_cq *cq, struct ibv_context *context,
134 		       struct ibv_comp_channel *channel,
135 		       void *cq_context);
136 void verbs_cleanup_cq(struct ibv_cq *cq);
137 int ibv_init_wq(struct ibv_wq *wq);
138 void ibv_cleanup_wq(struct ibv_wq *wq);
139 
140 int ibv_cmd_get_context(struct ibv_context *context, struct ibv_get_context *cmd,
141 			size_t cmd_size, struct ibv_get_context_resp *resp,
142 			size_t resp_size);
143 int ibv_cmd_query_device(struct ibv_context *context,
144 			 struct ibv_device_attr *device_attr,
145 			 uint64_t *raw_fw_ver,
146 			 struct ibv_query_device *cmd, size_t cmd_size);
147 int ibv_cmd_query_device_ex(struct ibv_context *context,
148 			    const struct ibv_query_device_ex_input *input,
149 			    struct ibv_device_attr_ex *attr, size_t attr_size,
150 			    uint64_t *raw_fw_ver,
151 			    struct ibv_query_device_ex *cmd,
152 			    size_t cmd_core_size,
153 			    size_t cmd_size,
154 			    struct ibv_query_device_resp_ex *resp,
155 			    size_t resp_core_size,
156 			    size_t resp_size);
157 int ibv_cmd_query_port(struct ibv_context *context, uint8_t port_num,
158 		       struct ibv_port_attr *port_attr,
159 		       struct ibv_query_port *cmd, size_t cmd_size);
160 int ibv_cmd_alloc_pd(struct ibv_context *context, struct ibv_pd *pd,
161 		     struct ibv_alloc_pd *cmd, size_t cmd_size,
162 		     struct ibv_alloc_pd_resp *resp, size_t resp_size);
163 int ibv_cmd_dealloc_pd(struct ibv_pd *pd);
164 int ibv_cmd_open_xrcd(struct ibv_context *context, struct verbs_xrcd *xrcd,
165 		      int vxrcd_size,
166 		      struct ibv_xrcd_init_attr *attr,
167 		      struct ibv_open_xrcd *cmd, size_t cmd_size,
168 		      struct ibv_open_xrcd_resp *resp, size_t resp_size);
169 int ibv_cmd_close_xrcd(struct verbs_xrcd *xrcd);
170 #define IBV_CMD_REG_MR_HAS_RESP_PARAMS
171 int ibv_cmd_reg_mr(struct ibv_pd *pd, void *addr, size_t length,
172 		   uint64_t hca_va, int access,
173 		   struct ibv_mr *mr, struct ibv_reg_mr *cmd,
174 		   size_t cmd_size,
175 		   struct ibv_reg_mr_resp *resp, size_t resp_size);
176 int ibv_cmd_rereg_mr(struct ibv_mr *mr, uint32_t flags, void *addr,
177 		     size_t length, uint64_t hca_va, int access,
178 		     struct ibv_pd *pd, struct ibv_rereg_mr *cmd,
179 		     size_t cmd_sz, struct ibv_rereg_mr_resp *resp,
180 		     size_t resp_sz);
181 int ibv_cmd_dereg_mr(struct ibv_mr *mr);
182 int ibv_cmd_alloc_mw(struct ibv_pd *pd, enum ibv_mw_type type,
183 		     struct ibv_mw *mw, struct ibv_alloc_mw *cmd,
184 		     size_t cmd_size,
185 		     struct ibv_alloc_mw_resp *resp, size_t resp_size);
186 int ibv_cmd_dealloc_mw(struct ibv_mw *mw,
187 		       struct ibv_dealloc_mw *cmd, size_t cmd_size);
188 int ibv_cmd_create_cq(struct ibv_context *context, int cqe,
189 		      struct ibv_comp_channel *channel,
190 		      int comp_vector, struct ibv_cq *cq,
191 		      struct ibv_create_cq *cmd, size_t cmd_size,
192 		      struct ibv_create_cq_resp *resp, size_t resp_size);
193 int ibv_cmd_create_cq_ex(struct ibv_context *context,
194 			 struct ibv_cq_init_attr_ex *cq_attr,
195 			 struct ibv_cq_ex *cq,
196 			 struct ibv_create_cq_ex *cmd,
197 			 size_t cmd_core_size,
198 			 size_t cmd_size,
199 			 struct ibv_create_cq_resp_ex *resp,
200 			 size_t resp_core_size,
201 			 size_t resp_size);
202 int ibv_cmd_poll_cq(struct ibv_cq *cq, int ne, struct ibv_wc *wc);
203 int ibv_cmd_req_notify_cq(struct ibv_cq *cq, int solicited_only);
204 #define IBV_CMD_RESIZE_CQ_HAS_RESP_PARAMS
205 int ibv_cmd_resize_cq(struct ibv_cq *cq, int cqe,
206 		      struct ibv_resize_cq *cmd, size_t cmd_size,
207 		      struct ibv_resize_cq_resp *resp, size_t resp_size);
208 int ibv_cmd_destroy_cq(struct ibv_cq *cq);
209 
210 int ibv_cmd_create_srq(struct ibv_pd *pd,
211 		       struct ibv_srq *srq, struct ibv_srq_init_attr *attr,
212 		       struct ibv_create_srq *cmd, size_t cmd_size,
213 		       struct ibv_create_srq_resp *resp, size_t resp_size);
214 int ibv_cmd_create_srq_ex(struct ibv_context *context,
215 			  struct verbs_srq *srq, int vsrq_sz,
216 			  struct ibv_srq_init_attr_ex *attr_ex,
217 			  struct ibv_create_xsrq *cmd, size_t cmd_size,
218 			  struct ibv_create_srq_resp *resp, size_t resp_size);
219 int ibv_cmd_modify_srq(struct ibv_srq *srq,
220 		       struct ibv_srq_attr *srq_attr,
221 		       int srq_attr_mask,
222 		       struct ibv_modify_srq *cmd, size_t cmd_size);
223 int ibv_cmd_query_srq(struct ibv_srq *srq,
224 		      struct ibv_srq_attr *srq_attr,
225 		      struct ibv_query_srq *cmd, size_t cmd_size);
226 int ibv_cmd_destroy_srq(struct ibv_srq *srq);
227 
228 int ibv_cmd_create_qp(struct ibv_pd *pd,
229 		      struct ibv_qp *qp, struct ibv_qp_init_attr *attr,
230 		      struct ibv_create_qp *cmd, size_t cmd_size,
231 		      struct ibv_create_qp_resp *resp, size_t resp_size);
232 int ibv_cmd_create_qp_ex(struct ibv_context *context,
233 			 struct verbs_qp *qp, int vqp_sz,
234 			 struct ibv_qp_init_attr_ex *attr_ex,
235 			 struct ibv_create_qp *cmd, size_t cmd_size,
236 			 struct ibv_create_qp_resp *resp, size_t resp_size);
237 int ibv_cmd_create_qp_ex2(struct ibv_context *context,
238 			  struct verbs_qp *qp, int vqp_sz,
239 			  struct ibv_qp_init_attr_ex *qp_attr,
240 			  struct ibv_create_qp_ex *cmd,
241 			  size_t cmd_core_size,
242 			  size_t cmd_size,
243 			  struct ibv_create_qp_resp_ex *resp,
244 			  size_t resp_core_size,
245 			  size_t resp_size);
246 int ibv_cmd_open_qp(struct ibv_context *context,
247 		    struct verbs_qp *qp,  int vqp_sz,
248 		    struct ibv_qp_open_attr *attr,
249 		    struct ibv_open_qp *cmd, size_t cmd_size,
250 		    struct ibv_create_qp_resp *resp, size_t resp_size);
251 int ibv_cmd_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *qp_attr,
252 		     int attr_mask,
253 		     struct ibv_qp_init_attr *qp_init_attr,
254 		     struct ibv_query_qp *cmd, size_t cmd_size);
255 int ibv_cmd_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
256 		      int attr_mask,
257 		      struct ibv_modify_qp *cmd, size_t cmd_size);
258 int ibv_cmd_modify_qp_ex(struct ibv_qp *qp, struct ibv_qp_attr *attr,
259 			 int attr_mask, struct ibv_modify_qp_ex *cmd,
260 			 size_t cmd_core_size, size_t cmd_size,
261 			 struct ibv_modify_qp_resp_ex *resp,
262 			 size_t resp_core_size, size_t resp_size);
263 int ibv_cmd_destroy_qp(struct ibv_qp *qp);
264 int ibv_cmd_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr,
265 		      struct ibv_send_wr **bad_wr);
266 int ibv_cmd_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr,
267 		      struct ibv_recv_wr **bad_wr);
268 int ibv_cmd_post_srq_recv(struct ibv_srq *srq, struct ibv_recv_wr *wr,
269 			  struct ibv_recv_wr **bad_wr);
270 int ibv_cmd_create_ah(struct ibv_pd *pd, struct ibv_ah *ah,
271 		      struct ibv_ah_attr *attr,
272 		      struct ibv_create_ah_resp *resp,
273 		      size_t resp_size);
274 int ibv_cmd_destroy_ah(struct ibv_ah *ah);
275 int ibv_cmd_attach_mcast(struct ibv_qp *qp, const union ibv_gid *gid, uint16_t lid);
276 int ibv_cmd_detach_mcast(struct ibv_qp *qp, const union ibv_gid *gid, uint16_t lid);
277 
278 struct ibv_flow *ibv_cmd_create_flow(struct ibv_qp *qp,
279 				     struct ibv_flow_attr *flow_attr);
280 int ibv_cmd_destroy_flow(struct ibv_flow *flow_id);
281 int ibv_cmd_create_wq(struct ibv_context *context,
282 		      struct ibv_wq_init_attr *wq_init_attr,
283 		      struct ibv_wq *wq,
284 		      struct ibv_create_wq *cmd,
285 		      size_t cmd_core_size,
286 		      size_t cmd_size,
287 		      struct ibv_create_wq_resp *resp,
288 		      size_t resp_core_size,
289 		      size_t resp_size);
290 
291 int ibv_cmd_modify_wq(struct ibv_wq *wq, struct ibv_wq_attr *attr,
292 		      struct ibv_modify_wq *cmd, size_t cmd_core_size,
293 		      size_t cmd_size);
294 int ibv_cmd_destroy_wq(struct ibv_wq *wq);
295 int ibv_cmd_create_rwq_ind_table(struct ibv_context *context,
296 				 struct ibv_rwq_ind_table_init_attr *init_attr,
297 				 struct ibv_rwq_ind_table *rwq_ind_table,
298 				 struct ibv_create_rwq_ind_table *cmd,
299 				 size_t cmd_core_size,
300 				 size_t cmd_size,
301 				 struct ibv_create_rwq_ind_table_resp *resp,
302 				 size_t resp_core_size,
303 				 size_t resp_size);
304 int ibv_cmd_destroy_rwq_ind_table(struct ibv_rwq_ind_table *rwq_ind_table);
305 int ibv_dontfork_range(void *base, size_t size);
306 int ibv_dofork_range(void *base, size_t size);
307 
308 /*
309  * sysfs helper functions
310  */
311 const char *ibv_get_sysfs_path(void);
312 
313 int ibv_read_sysfs_file(const char *dir, const char *file,
314 			char *buf, size_t size);
315 
316 static inline int verbs_get_srq_num(struct ibv_srq *srq, uint32_t *srq_num)
317 {
318 	struct verbs_srq *vsrq = container_of(srq, struct verbs_srq, srq);
319 	if (vsrq->comp_mask & VERBS_SRQ_NUM) {
320 		*srq_num = vsrq->srq_num;
321 		return 0;
322 	}
323 	return ENOSYS;
324 }
325 
326 int ibv_query_gid_type(struct ibv_context *context, uint8_t port_num,
327 		       unsigned int index, enum ibv_gid_type *type);
328 #endif /* INFINIBAND_DRIVER_H */
329