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 	/* lock for mcast list */
170 	struct mutex		mcast_lock;
171 	struct list_head 	mcast_list;
172 	struct ib_uxrcd_object *uxrcd;
173 };
174 
175 struct ib_uwq_object {
176 	struct ib_uevent_object	uevent;
177 };
178 
179 struct ib_ucq_object {
180 	struct ib_uobject	uobject;
181 	struct ib_uverbs_file  *uverbs_file;
182 	struct list_head	comp_list;
183 	struct list_head	async_list;
184 	u32			comp_events_reported;
185 	u32			async_events_reported;
186 };
187 
188 extern spinlock_t ib_uverbs_idr_lock;
189 extern struct idr ib_uverbs_pd_idr;
190 extern struct idr ib_uverbs_mr_idr;
191 extern struct idr ib_uverbs_mw_idr;
192 extern struct idr ib_uverbs_ah_idr;
193 extern struct idr ib_uverbs_cq_idr;
194 extern struct idr ib_uverbs_qp_idr;
195 extern struct idr ib_uverbs_srq_idr;
196 extern struct idr ib_uverbs_xrcd_idr;
197 extern struct idr ib_uverbs_rule_idr;
198 extern struct idr ib_uverbs_wq_idr;
199 extern struct idr ib_uverbs_rwq_ind_tbl_idr;
200 
201 void idr_remove_uobj(struct idr *idp, struct ib_uobject *uobj);
202 
203 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
204 					struct ib_device *ib_dev,
205 					int is_async);
206 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *uverbs_file);
207 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd);
208 
209 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
210 			   struct ib_uverbs_event_file *ev_file,
211 			   struct ib_ucq_object *uobj);
212 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
213 			      struct ib_uevent_object *uobj);
214 
215 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
216 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
217 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
218 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
219 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
220 void ib_uverbs_event_handler(struct ib_event_handler *handler,
221 			     struct ib_event *event);
222 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd);
223 
224 int uverbs_dealloc_mw(struct ib_mw *mw);
225 
226 struct ib_uverbs_flow_spec {
227 	union {
228 		union {
229 			struct ib_uverbs_flow_spec_hdr hdr;
230 			struct {
231 				__u32 type;
232 				__u16 size;
233 				__u16 reserved;
234 			};
235 		};
236 		struct ib_uverbs_flow_spec_eth     eth;
237 		struct ib_uverbs_flow_spec_ipv4    ipv4;
238 		struct ib_uverbs_flow_spec_tcp_udp tcp_udp;
239 		struct ib_uverbs_flow_spec_ipv6    ipv6;
240 	};
241 };
242 
243 #define IB_UVERBS_DECLARE_CMD(name)					\
244 	ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,		\
245 				 struct ib_device *ib_dev,              \
246 				 const char __user *buf, int in_len,	\
247 				 int out_len)
248 
249 IB_UVERBS_DECLARE_CMD(get_context);
250 IB_UVERBS_DECLARE_CMD(query_device);
251 IB_UVERBS_DECLARE_CMD(query_port);
252 IB_UVERBS_DECLARE_CMD(alloc_pd);
253 IB_UVERBS_DECLARE_CMD(dealloc_pd);
254 IB_UVERBS_DECLARE_CMD(reg_mr);
255 IB_UVERBS_DECLARE_CMD(rereg_mr);
256 IB_UVERBS_DECLARE_CMD(dereg_mr);
257 IB_UVERBS_DECLARE_CMD(alloc_mw);
258 IB_UVERBS_DECLARE_CMD(dealloc_mw);
259 IB_UVERBS_DECLARE_CMD(create_comp_channel);
260 IB_UVERBS_DECLARE_CMD(create_cq);
261 IB_UVERBS_DECLARE_CMD(resize_cq);
262 IB_UVERBS_DECLARE_CMD(poll_cq);
263 IB_UVERBS_DECLARE_CMD(req_notify_cq);
264 IB_UVERBS_DECLARE_CMD(destroy_cq);
265 IB_UVERBS_DECLARE_CMD(create_qp);
266 IB_UVERBS_DECLARE_CMD(open_qp);
267 IB_UVERBS_DECLARE_CMD(query_qp);
268 IB_UVERBS_DECLARE_CMD(modify_qp);
269 IB_UVERBS_DECLARE_CMD(destroy_qp);
270 IB_UVERBS_DECLARE_CMD(post_send);
271 IB_UVERBS_DECLARE_CMD(post_recv);
272 IB_UVERBS_DECLARE_CMD(post_srq_recv);
273 IB_UVERBS_DECLARE_CMD(create_ah);
274 IB_UVERBS_DECLARE_CMD(destroy_ah);
275 IB_UVERBS_DECLARE_CMD(attach_mcast);
276 IB_UVERBS_DECLARE_CMD(detach_mcast);
277 IB_UVERBS_DECLARE_CMD(create_srq);
278 IB_UVERBS_DECLARE_CMD(modify_srq);
279 IB_UVERBS_DECLARE_CMD(query_srq);
280 IB_UVERBS_DECLARE_CMD(destroy_srq);
281 IB_UVERBS_DECLARE_CMD(create_xsrq);
282 IB_UVERBS_DECLARE_CMD(open_xrcd);
283 IB_UVERBS_DECLARE_CMD(close_xrcd);
284 
285 #define IB_UVERBS_DECLARE_EX_CMD(name)				\
286 	int ib_uverbs_ex_##name(struct ib_uverbs_file *file,	\
287 				struct ib_device *ib_dev,		\
288 				struct ib_udata *ucore,		\
289 				struct ib_udata *uhw)
290 
291 IB_UVERBS_DECLARE_EX_CMD(create_flow);
292 IB_UVERBS_DECLARE_EX_CMD(destroy_flow);
293 IB_UVERBS_DECLARE_EX_CMD(query_device);
294 IB_UVERBS_DECLARE_EX_CMD(create_cq);
295 IB_UVERBS_DECLARE_EX_CMD(create_qp);
296 IB_UVERBS_DECLARE_EX_CMD(create_wq);
297 IB_UVERBS_DECLARE_EX_CMD(modify_wq);
298 IB_UVERBS_DECLARE_EX_CMD(destroy_wq);
299 IB_UVERBS_DECLARE_EX_CMD(create_rwq_ind_table);
300 IB_UVERBS_DECLARE_EX_CMD(destroy_rwq_ind_table);
301 
302 #endif /* UVERBS_H */
303