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