xref: /freebsd/sys/ofed/drivers/infiniband/core/uverbs.h (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
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) != 0) ?				\
68 		    (const void __user *) (ibuf) : NULL;			\
69 		(udata)->outbuf = ((olen) != 0) ?				\
70 		    (void __user *) (obuf) : NULL;				\
71 		(udata)->inlen  = (ilen);					\
72 		(udata)->outlen = (olen);					\
73 	} while (0)
74 
75 /*
76  * Our lifetime rules for these structs are the following:
77  *
78  * struct ib_uverbs_device: One reference is held by the module and
79  * released in ib_uverbs_remove_one().  Another reference is taken by
80  * ib_uverbs_open() each time the character special file is opened,
81  * and released in ib_uverbs_release_file() when the file is released.
82  *
83  * struct ib_uverbs_file: One reference is held by the VFS and
84  * released when the file is closed.  Another reference is taken when
85  * an asynchronous event queue file is created and released when the
86  * event file is closed.
87  *
88  * struct ib_uverbs_event_file: One reference is held by the VFS and
89  * released when the file is closed.  For asynchronous event files,
90  * another reference is held by the corresponding main context file
91  * and released when that file is closed.  For completion event files,
92  * a reference is taken when a CQ is created that uses the file, and
93  * released when the CQ is destroyed.
94  */
95 
96 struct ib_uverbs_device {
97 	atomic_t				refcount;
98 	int					num_comp_vectors;
99 	struct completion			comp;
100 	struct device			       *dev;
101 	struct ib_device	__rcu	       *ib_dev;
102 	int					devnum;
103 	struct cdev			        cdev;
104 	struct rb_root				xrcd_tree;
105 	struct mutex				xrcd_tree_mutex;
106 	struct kobject				kobj;
107 	struct srcu_struct			disassociate_srcu;
108 	struct mutex				lists_mutex; /* protect lists */
109 	struct list_head			uverbs_file_list;
110 	struct list_head			uverbs_events_file_list;
111 };
112 
113 struct ib_uverbs_event_file {
114 	struct kref				ref;
115 	int					is_async;
116 	struct ib_uverbs_file		       *uverbs_file;
117 	spinlock_t				lock;
118 	int					is_closed;
119 	wait_queue_head_t			poll_wait;
120 	struct fasync_struct		       *async_queue;
121 	struct list_head			event_list;
122 	struct list_head			list;
123 };
124 
125 struct ib_uverbs_file {
126 	struct kref				ref;
127 	struct mutex				mutex;
128 	struct mutex                            cleanup_mutex; /* protect cleanup */
129 	struct ib_uverbs_device		       *device;
130 	struct ib_ucontext		       *ucontext;
131 	struct ib_event_handler			event_handler;
132 	struct ib_uverbs_event_file	       *async_file;
133 	struct list_head			list;
134 	int					is_closed;
135 };
136 
137 struct ib_uverbs_event {
138 	union {
139 		struct ib_uverbs_async_event_desc	async;
140 		struct ib_uverbs_comp_event_desc	comp;
141 	}					desc;
142 	struct list_head			list;
143 	struct list_head			obj_list;
144 	u32				       *counter;
145 };
146 
147 struct ib_uverbs_mcast_entry {
148 	struct list_head	list;
149 	union ib_gid 		gid;
150 	u16 			lid;
151 };
152 
153 struct ib_uevent_object {
154 	struct ib_uobject	uobject;
155 	struct list_head	event_list;
156 	u32			events_reported;
157 };
158 
159 struct ib_uxrcd_object {
160 	struct ib_uobject	uobject;
161 	atomic_t		refcnt;
162 };
163 
164 struct ib_usrq_object {
165 	struct ib_uevent_object	uevent;
166 	struct ib_uxrcd_object *uxrcd;
167 };
168 
169 struct ib_uqp_object {
170 	struct ib_uevent_object	uevent;
171 	/* lock for mcast list */
172 	struct mutex		mcast_lock;
173 	struct list_head 	mcast_list;
174 	struct ib_uxrcd_object *uxrcd;
175 };
176 
177 struct ib_uwq_object {
178 	struct ib_uevent_object	uevent;
179 };
180 
181 struct ib_ucq_object {
182 	struct ib_uobject	uobject;
183 	struct ib_uverbs_file  *uverbs_file;
184 	struct list_head	comp_list;
185 	struct list_head	async_list;
186 	u32			comp_events_reported;
187 	u32			async_events_reported;
188 };
189 
190 extern spinlock_t ib_uverbs_idr_lock;
191 extern struct idr ib_uverbs_pd_idr;
192 extern struct idr ib_uverbs_mr_idr;
193 extern struct idr ib_uverbs_mw_idr;
194 extern struct idr ib_uverbs_ah_idr;
195 extern struct idr ib_uverbs_cq_idr;
196 extern struct idr ib_uverbs_qp_idr;
197 extern struct idr ib_uverbs_srq_idr;
198 extern struct idr ib_uverbs_xrcd_idr;
199 extern struct idr ib_uverbs_rule_idr;
200 extern struct idr ib_uverbs_wq_idr;
201 extern struct idr ib_uverbs_rwq_ind_tbl_idr;
202 
203 void idr_remove_uobj(struct idr *idp, struct ib_uobject *uobj);
204 
205 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
206 					struct ib_device *ib_dev,
207 					int is_async);
208 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *uverbs_file);
209 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd);
210 
211 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
212 			   struct ib_uverbs_event_file *ev_file,
213 			   struct ib_ucq_object *uobj);
214 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
215 			      struct ib_uevent_object *uobj);
216 
217 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
218 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
219 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
220 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
221 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
222 void ib_uverbs_event_handler(struct ib_event_handler *handler,
223 			     struct ib_event *event);
224 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd);
225 
226 int uverbs_dealloc_mw(struct ib_mw *mw);
227 
228 struct ib_uverbs_flow_spec {
229 	union {
230 		union {
231 			struct ib_uverbs_flow_spec_hdr hdr;
232 			struct {
233 				__u32 type;
234 				__u16 size;
235 				__u16 reserved;
236 			};
237 		};
238 		struct ib_uverbs_flow_spec_eth     eth;
239 		struct ib_uverbs_flow_spec_ipv4    ipv4;
240 		struct ib_uverbs_flow_spec_tcp_udp tcp_udp;
241 		struct ib_uverbs_flow_spec_ipv6    ipv6;
242 	};
243 };
244 
245 #define IB_UVERBS_DECLARE_CMD(name)					\
246 	ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,		\
247 				 struct ib_device *ib_dev,              \
248 				 const char __user *buf, int in_len,	\
249 				 int out_len)
250 
251 IB_UVERBS_DECLARE_CMD(get_context);
252 IB_UVERBS_DECLARE_CMD(query_device);
253 IB_UVERBS_DECLARE_CMD(query_port);
254 IB_UVERBS_DECLARE_CMD(alloc_pd);
255 IB_UVERBS_DECLARE_CMD(dealloc_pd);
256 IB_UVERBS_DECLARE_CMD(reg_mr);
257 IB_UVERBS_DECLARE_CMD(rereg_mr);
258 IB_UVERBS_DECLARE_CMD(dereg_mr);
259 IB_UVERBS_DECLARE_CMD(alloc_mw);
260 IB_UVERBS_DECLARE_CMD(dealloc_mw);
261 IB_UVERBS_DECLARE_CMD(create_comp_channel);
262 IB_UVERBS_DECLARE_CMD(create_cq);
263 IB_UVERBS_DECLARE_CMD(resize_cq);
264 IB_UVERBS_DECLARE_CMD(poll_cq);
265 IB_UVERBS_DECLARE_CMD(req_notify_cq);
266 IB_UVERBS_DECLARE_CMD(destroy_cq);
267 IB_UVERBS_DECLARE_CMD(create_qp);
268 IB_UVERBS_DECLARE_CMD(open_qp);
269 IB_UVERBS_DECLARE_CMD(query_qp);
270 IB_UVERBS_DECLARE_CMD(modify_qp);
271 IB_UVERBS_DECLARE_CMD(destroy_qp);
272 IB_UVERBS_DECLARE_CMD(post_send);
273 IB_UVERBS_DECLARE_CMD(post_recv);
274 IB_UVERBS_DECLARE_CMD(post_srq_recv);
275 IB_UVERBS_DECLARE_CMD(create_ah);
276 IB_UVERBS_DECLARE_CMD(destroy_ah);
277 IB_UVERBS_DECLARE_CMD(attach_mcast);
278 IB_UVERBS_DECLARE_CMD(detach_mcast);
279 IB_UVERBS_DECLARE_CMD(create_srq);
280 IB_UVERBS_DECLARE_CMD(modify_srq);
281 IB_UVERBS_DECLARE_CMD(query_srq);
282 IB_UVERBS_DECLARE_CMD(destroy_srq);
283 IB_UVERBS_DECLARE_CMD(create_xsrq);
284 IB_UVERBS_DECLARE_CMD(open_xrcd);
285 IB_UVERBS_DECLARE_CMD(close_xrcd);
286 
287 #define IB_UVERBS_DECLARE_EX_CMD(name)				\
288 	int ib_uverbs_ex_##name(struct ib_uverbs_file *file,	\
289 				struct ib_device *ib_dev,		\
290 				struct ib_udata *ucore,		\
291 				struct ib_udata *uhw)
292 
293 IB_UVERBS_DECLARE_EX_CMD(create_flow);
294 IB_UVERBS_DECLARE_EX_CMD(destroy_flow);
295 IB_UVERBS_DECLARE_EX_CMD(query_device);
296 IB_UVERBS_DECLARE_EX_CMD(create_cq);
297 IB_UVERBS_DECLARE_EX_CMD(create_qp);
298 IB_UVERBS_DECLARE_EX_CMD(create_wq);
299 IB_UVERBS_DECLARE_EX_CMD(modify_wq);
300 IB_UVERBS_DECLARE_EX_CMD(destroy_wq);
301 IB_UVERBS_DECLARE_EX_CMD(create_rwq_ind_table);
302 IB_UVERBS_DECLARE_EX_CMD(destroy_rwq_ind_table);
303 
304 #endif /* UVERBS_H */
305