xref: /freebsd/sys/ofed/drivers/infiniband/core/uverbs.h (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
5  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
6  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
7  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
8  * Copyright (c) 2005 PathScale, Inc. All rights reserved.
9  *
10  * This software is available to you under a choice of one of two
11  * licenses.  You may choose to be licensed under the terms of the GNU
12  * General Public License (GPL) Version 2, available from the file
13  * COPYING in the main directory of this source tree, or the
14  * OpenIB.org BSD license below:
15  *
16  *     Redistribution and use in source and binary forms, with or
17  *     without modification, are permitted provided that the following
18  *     conditions are met:
19  *
20  *      - Redistributions of source code must retain the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer.
23  *
24  *      - Redistributions in binary form must reproduce the above
25  *        copyright notice, this list of conditions and the following
26  *        disclaimer in the documentation and/or other materials
27  *        provided with the distribution.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  *
38  * $FreeBSD$
39  */
40 
41 #ifndef UVERBS_H
42 #define UVERBS_H
43 
44 #include <linux/kref.h>
45 #include <linux/idr.h>
46 #include <linux/mutex.h>
47 #include <linux/completion.h>
48 #include <linux/cdev.h>
49 #include <linux/srcu.h>
50 #include <linux/rcupdate.h>
51 #include <linux/rbtree.h>
52 
53 #include <rdma/ib_verbs.h>
54 #include <rdma/ib_umem.h>
55 #include <rdma/ib_user_verbs.h>
56 
57 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen)			\
58 	do {								\
59 		(udata)->inbuf  = (const void __user *) (ibuf);		\
60 		(udata)->outbuf = (void __user *) (obuf);		\
61 		(udata)->inlen  = (ilen);				\
62 		(udata)->outlen = (olen);				\
63 	} while (0)
64 
65 #define INIT_UDATA_BUF_OR_NULL(udata, ibuf, obuf, ilen, olen)			\
66 	do {									\
67 		(udata)->inbuf  = (ilen) ? (const void __user *) (ibuf) : NULL;	\
68 		(udata)->outbuf = (olen) ? (void __user *) (obuf) : NULL;	\
69 		(udata)->inlen  = (ilen);					\
70 		(udata)->outlen = (olen);					\
71 	} while (0)
72 
73 /*
74  * Our lifetime rules for these structs are the following:
75  *
76  * struct ib_uverbs_device: One reference is held by the module and
77  * released in ib_uverbs_remove_one().  Another reference is taken by
78  * ib_uverbs_open() each time the character special file is opened,
79  * and released in ib_uverbs_release_file() when the file is released.
80  *
81  * struct ib_uverbs_file: One reference is held by the VFS and
82  * released when the file is closed.  Another reference is taken when
83  * an asynchronous event queue file is created and released when the
84  * event file is closed.
85  *
86  * struct ib_uverbs_event_file: One reference is held by the VFS and
87  * released when the file is closed.  For asynchronous event files,
88  * another reference is held by the corresponding main context file
89  * and released when that file is closed.  For completion event files,
90  * a reference is taken when a CQ is created that uses the file, and
91  * released when the CQ is destroyed.
92  */
93 
94 struct ib_uverbs_device {
95 	atomic_t				refcount;
96 	int					num_comp_vectors;
97 	struct completion			comp;
98 	struct device			       *dev;
99 	struct ib_device	__rcu	       *ib_dev;
100 	int					devnum;
101 	struct cdev			        cdev;
102 	struct rb_root				xrcd_tree;
103 	struct mutex				xrcd_tree_mutex;
104 	struct kobject				kobj;
105 	struct srcu_struct			disassociate_srcu;
106 	struct mutex				lists_mutex; /* protect lists */
107 	struct list_head			uverbs_file_list;
108 	struct list_head			uverbs_events_file_list;
109 };
110 
111 struct ib_uverbs_event_file {
112 	struct kref				ref;
113 	int					is_async;
114 	struct ib_uverbs_file		       *uverbs_file;
115 	spinlock_t				lock;
116 	int					is_closed;
117 	wait_queue_head_t			poll_wait;
118 	struct fasync_struct		       *async_queue;
119 	struct list_head			event_list;
120 	struct list_head			list;
121 };
122 
123 struct ib_uverbs_file {
124 	struct kref				ref;
125 	struct mutex				mutex;
126 	struct mutex                            cleanup_mutex; /* protect cleanup */
127 	struct ib_uverbs_device		       *device;
128 	struct ib_ucontext		       *ucontext;
129 	struct ib_event_handler			event_handler;
130 	struct ib_uverbs_event_file	       *async_file;
131 	struct list_head			list;
132 	int					is_closed;
133 };
134 
135 struct ib_uverbs_event {
136 	union {
137 		struct ib_uverbs_async_event_desc	async;
138 		struct ib_uverbs_comp_event_desc	comp;
139 	}					desc;
140 	struct list_head			list;
141 	struct list_head			obj_list;
142 	u32				       *counter;
143 };
144 
145 struct ib_uverbs_mcast_entry {
146 	struct list_head	list;
147 	union ib_gid 		gid;
148 	u16 			lid;
149 };
150 
151 struct ib_uevent_object {
152 	struct ib_uobject	uobject;
153 	struct list_head	event_list;
154 	u32			events_reported;
155 };
156 
157 struct ib_uxrcd_object {
158 	struct ib_uobject	uobject;
159 	atomic_t		refcnt;
160 };
161 
162 struct ib_usrq_object {
163 	struct ib_uevent_object	uevent;
164 	struct ib_uxrcd_object *uxrcd;
165 };
166 
167 struct ib_uqp_object {
168 	struct ib_uevent_object	uevent;
169 	struct list_head 	mcast_list;
170 	struct ib_uxrcd_object *uxrcd;
171 };
172 
173 struct ib_uwq_object {
174 	struct ib_uevent_object	uevent;
175 };
176 
177 struct ib_ucq_object {
178 	struct ib_uobject	uobject;
179 	struct ib_uverbs_file  *uverbs_file;
180 	struct list_head	comp_list;
181 	struct list_head	async_list;
182 	u32			comp_events_reported;
183 	u32			async_events_reported;
184 };
185 
186 extern spinlock_t ib_uverbs_idr_lock;
187 extern struct idr ib_uverbs_pd_idr;
188 extern struct idr ib_uverbs_mr_idr;
189 extern struct idr ib_uverbs_mw_idr;
190 extern struct idr ib_uverbs_ah_idr;
191 extern struct idr ib_uverbs_cq_idr;
192 extern struct idr ib_uverbs_qp_idr;
193 extern struct idr ib_uverbs_srq_idr;
194 extern struct idr ib_uverbs_xrcd_idr;
195 extern struct idr ib_uverbs_rule_idr;
196 extern struct idr ib_uverbs_wq_idr;
197 extern struct idr ib_uverbs_rwq_ind_tbl_idr;
198 
199 void idr_remove_uobj(struct idr *idp, struct ib_uobject *uobj);
200 
201 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
202 					struct ib_device *ib_dev,
203 					int is_async);
204 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *uverbs_file);
205 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd);
206 
207 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
208 			   struct ib_uverbs_event_file *ev_file,
209 			   struct ib_ucq_object *uobj);
210 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
211 			      struct ib_uevent_object *uobj);
212 
213 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
214 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
215 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
216 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
217 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
218 void ib_uverbs_event_handler(struct ib_event_handler *handler,
219 			     struct ib_event *event);
220 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd);
221 
222 int uverbs_dealloc_mw(struct ib_mw *mw);
223 
224 struct ib_uverbs_flow_spec {
225 	union {
226 		union {
227 			struct ib_uverbs_flow_spec_hdr hdr;
228 			struct {
229 				__u32 type;
230 				__u16 size;
231 				__u16 reserved;
232 			};
233 		};
234 		struct ib_uverbs_flow_spec_eth     eth;
235 		struct ib_uverbs_flow_spec_ipv4    ipv4;
236 		struct ib_uverbs_flow_spec_tcp_udp tcp_udp;
237 		struct ib_uverbs_flow_spec_ipv6    ipv6;
238 	};
239 };
240 
241 #define IB_UVERBS_DECLARE_CMD(name)					\
242 	ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,		\
243 				 struct ib_device *ib_dev,              \
244 				 const char __user *buf, int in_len,	\
245 				 int out_len)
246 
247 IB_UVERBS_DECLARE_CMD(get_context);
248 IB_UVERBS_DECLARE_CMD(query_device);
249 IB_UVERBS_DECLARE_CMD(query_port);
250 IB_UVERBS_DECLARE_CMD(alloc_pd);
251 IB_UVERBS_DECLARE_CMD(dealloc_pd);
252 IB_UVERBS_DECLARE_CMD(reg_mr);
253 IB_UVERBS_DECLARE_CMD(rereg_mr);
254 IB_UVERBS_DECLARE_CMD(dereg_mr);
255 IB_UVERBS_DECLARE_CMD(alloc_mw);
256 IB_UVERBS_DECLARE_CMD(dealloc_mw);
257 IB_UVERBS_DECLARE_CMD(create_comp_channel);
258 IB_UVERBS_DECLARE_CMD(create_cq);
259 IB_UVERBS_DECLARE_CMD(resize_cq);
260 IB_UVERBS_DECLARE_CMD(poll_cq);
261 IB_UVERBS_DECLARE_CMD(req_notify_cq);
262 IB_UVERBS_DECLARE_CMD(destroy_cq);
263 IB_UVERBS_DECLARE_CMD(create_qp);
264 IB_UVERBS_DECLARE_CMD(open_qp);
265 IB_UVERBS_DECLARE_CMD(query_qp);
266 IB_UVERBS_DECLARE_CMD(modify_qp);
267 IB_UVERBS_DECLARE_CMD(destroy_qp);
268 IB_UVERBS_DECLARE_CMD(post_send);
269 IB_UVERBS_DECLARE_CMD(post_recv);
270 IB_UVERBS_DECLARE_CMD(post_srq_recv);
271 IB_UVERBS_DECLARE_CMD(create_ah);
272 IB_UVERBS_DECLARE_CMD(destroy_ah);
273 IB_UVERBS_DECLARE_CMD(attach_mcast);
274 IB_UVERBS_DECLARE_CMD(detach_mcast);
275 IB_UVERBS_DECLARE_CMD(create_srq);
276 IB_UVERBS_DECLARE_CMD(modify_srq);
277 IB_UVERBS_DECLARE_CMD(query_srq);
278 IB_UVERBS_DECLARE_CMD(destroy_srq);
279 IB_UVERBS_DECLARE_CMD(create_xsrq);
280 IB_UVERBS_DECLARE_CMD(open_xrcd);
281 IB_UVERBS_DECLARE_CMD(close_xrcd);
282 
283 #define IB_UVERBS_DECLARE_EX_CMD(name)				\
284 	int ib_uverbs_ex_##name(struct ib_uverbs_file *file,	\
285 				struct ib_device *ib_dev,		\
286 				struct ib_udata *ucore,		\
287 				struct ib_udata *uhw)
288 
289 IB_UVERBS_DECLARE_EX_CMD(create_flow);
290 IB_UVERBS_DECLARE_EX_CMD(destroy_flow);
291 IB_UVERBS_DECLARE_EX_CMD(query_device);
292 IB_UVERBS_DECLARE_EX_CMD(create_cq);
293 IB_UVERBS_DECLARE_EX_CMD(create_qp);
294 IB_UVERBS_DECLARE_EX_CMD(create_wq);
295 IB_UVERBS_DECLARE_EX_CMD(modify_wq);
296 IB_UVERBS_DECLARE_EX_CMD(destroy_wq);
297 IB_UVERBS_DECLARE_EX_CMD(create_rwq_ind_table);
298 IB_UVERBS_DECLARE_EX_CMD(destroy_rwq_ind_table);
299 
300 #endif /* UVERBS_H */
301