xref: /openbmc/linux/drivers/infiniband/core/uverbs.h (revision cfdfc14e)
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
6  * Copyright (c) 2005 PathScale, Inc. All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36 
37 #ifndef UVERBS_H
38 #define UVERBS_H
39 
40 #include <linux/kref.h>
41 #include <linux/idr.h>
42 #include <linux/mutex.h>
43 #include <linux/completion.h>
44 #include <linux/cdev.h>
45 
46 #include <rdma/ib_verbs.h>
47 #include <rdma/ib_umem.h>
48 #include <rdma/ib_user_verbs.h>
49 #include <rdma/uverbs_std_types.h>
50 
51 #define UVERBS_MODULE_NAME ib_uverbs
52 #include <rdma/uverbs_named_ioctl.h>
53 
54 static inline void
55 ib_uverbs_init_udata(struct ib_udata *udata,
56 		     const void __user *ibuf,
57 		     void __user *obuf,
58 		     size_t ilen, size_t olen)
59 {
60 	udata->inbuf  = ibuf;
61 	udata->outbuf = obuf;
62 	udata->inlen  = ilen;
63 	udata->outlen = olen;
64 }
65 
66 static inline void
67 ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata,
68 				 const void __user *ibuf,
69 				 void __user *obuf,
70 				 size_t ilen, size_t olen)
71 {
72 	ib_uverbs_init_udata(udata,
73 			     ilen ? ibuf : NULL, olen ? obuf : NULL,
74 			     ilen, olen);
75 }
76 
77 /*
78  * Our lifetime rules for these structs are the following:
79  *
80  * struct ib_uverbs_device: One reference is held by the module and
81  * released in ib_uverbs_remove_one().  Another reference is taken by
82  * ib_uverbs_open() each time the character special file is opened,
83  * and released in ib_uverbs_release_file() when the file is released.
84  *
85  * struct ib_uverbs_file: One reference is held by the VFS and
86  * released when the file is closed.  Another reference is taken when
87  * an asynchronous event queue file is created and released when the
88  * event file is closed.
89  *
90  * struct ib_uverbs_event_queue: Base structure for
91  * struct ib_uverbs_async_event_file and struct ib_uverbs_completion_event_file.
92  * One reference is held by the VFS and released when the file is closed.
93  * For asynchronous event files, another reference is held by the corresponding
94  * main context file and released when that file is closed.  For completion
95  * event files, a reference is taken when a CQ is created that uses the file,
96  * and released when the CQ is destroyed.
97  */
98 
99 struct ib_uverbs_device {
100 	atomic_t				refcount;
101 	int					num_comp_vectors;
102 	struct completion			comp;
103 	struct device			       *dev;
104 	struct ib_device	__rcu	       *ib_dev;
105 	int					devnum;
106 	struct cdev			        cdev;
107 	struct rb_root				xrcd_tree;
108 	struct mutex				xrcd_tree_mutex;
109 	struct kobject				kobj;
110 	struct srcu_struct			disassociate_srcu;
111 	struct mutex				lists_mutex; /* protect lists */
112 	struct list_head			uverbs_file_list;
113 	struct list_head			uverbs_events_file_list;
114 	struct uverbs_root_spec			*specs_root;
115 };
116 
117 struct ib_uverbs_event_queue {
118 	spinlock_t				lock;
119 	int					is_closed;
120 	wait_queue_head_t			poll_wait;
121 	struct fasync_struct		       *async_queue;
122 	struct list_head			event_list;
123 };
124 
125 struct ib_uverbs_async_event_file {
126 	struct ib_uverbs_event_queue		ev_queue;
127 	struct ib_uverbs_file		       *uverbs_file;
128 	struct kref				ref;
129 	struct list_head			list;
130 };
131 
132 struct ib_uverbs_completion_event_file {
133 	struct ib_uobject_file			uobj_file;
134 	struct ib_uverbs_event_queue		ev_queue;
135 };
136 
137 struct ib_uverbs_file {
138 	struct kref				ref;
139 	struct mutex				mutex;
140 	struct mutex                            cleanup_mutex; /* protect cleanup */
141 	struct ib_uverbs_device		       *device;
142 	struct ib_ucontext		       *ucontext;
143 	struct ib_event_handler			event_handler;
144 	struct ib_uverbs_async_event_file       *async_file;
145 	struct list_head			list;
146 	int					is_closed;
147 
148 	struct idr		idr;
149 	/* spinlock protects write access to idr */
150 	spinlock_t		idr_lock;
151 };
152 
153 struct ib_uverbs_event {
154 	union {
155 		struct ib_uverbs_async_event_desc	async;
156 		struct ib_uverbs_comp_event_desc	comp;
157 	}					desc;
158 	struct list_head			list;
159 	struct list_head			obj_list;
160 	u32				       *counter;
161 };
162 
163 struct ib_uverbs_mcast_entry {
164 	struct list_head	list;
165 	union ib_gid 		gid;
166 	u16 			lid;
167 };
168 
169 struct ib_uevent_object {
170 	struct ib_uobject	uobject;
171 	struct list_head	event_list;
172 	u32			events_reported;
173 };
174 
175 struct ib_uxrcd_object {
176 	struct ib_uobject	uobject;
177 	atomic_t		refcnt;
178 };
179 
180 struct ib_usrq_object {
181 	struct ib_uevent_object	uevent;
182 	struct ib_uxrcd_object *uxrcd;
183 };
184 
185 struct ib_uqp_object {
186 	struct ib_uevent_object	uevent;
187 	/* lock for mcast list */
188 	struct mutex		mcast_lock;
189 	struct list_head 	mcast_list;
190 	struct ib_uxrcd_object *uxrcd;
191 };
192 
193 struct ib_uwq_object {
194 	struct ib_uevent_object	uevent;
195 };
196 
197 struct ib_ucq_object {
198 	struct ib_uobject	uobject;
199 	struct ib_uverbs_file  *uverbs_file;
200 	struct list_head	comp_list;
201 	struct list_head	async_list;
202 	u32			comp_events_reported;
203 	u32			async_events_reported;
204 };
205 
206 struct ib_uflow_resources;
207 struct ib_uflow_object {
208 	struct ib_uobject		uobject;
209 	struct ib_uflow_resources	*resources;
210 };
211 
212 extern const struct file_operations uverbs_event_fops;
213 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue);
214 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
215 					      struct ib_device *ib_dev);
216 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *uverbs_file);
217 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res);
218 
219 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
220 			   struct ib_uverbs_completion_event_file *ev_file,
221 			   struct ib_ucq_object *uobj);
222 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
223 			      struct ib_uevent_object *uobj);
224 void ib_uverbs_release_file(struct kref *ref);
225 
226 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
227 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
228 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
229 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
230 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
231 void ib_uverbs_event_handler(struct ib_event_handler *handler,
232 			     struct ib_event *event);
233 int ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd,
234 			   enum rdma_remove_reason why);
235 
236 int uverbs_dealloc_mw(struct ib_mw *mw);
237 void ib_uverbs_detach_umcast(struct ib_qp *qp,
238 			     struct ib_uqp_object *uobj);
239 
240 void create_udata(struct uverbs_attr_bundle *ctx, struct ib_udata *udata);
241 extern const struct uverbs_attr_def uverbs_uhw_compat_in;
242 extern const struct uverbs_attr_def uverbs_uhw_compat_out;
243 long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
244 int uverbs_destroy_def_handler(struct ib_device *ib_dev,
245 			       struct ib_uverbs_file *file,
246 			       struct uverbs_attr_bundle *attrs);
247 
248 struct ib_uverbs_flow_spec {
249 	union {
250 		union {
251 			struct ib_uverbs_flow_spec_hdr hdr;
252 			struct {
253 				__u32 type;
254 				__u16 size;
255 				__u16 reserved;
256 			};
257 		};
258 		struct ib_uverbs_flow_spec_eth     eth;
259 		struct ib_uverbs_flow_spec_ipv4    ipv4;
260 		struct ib_uverbs_flow_spec_esp     esp;
261 		struct ib_uverbs_flow_spec_tcp_udp tcp_udp;
262 		struct ib_uverbs_flow_spec_ipv6    ipv6;
263 		struct ib_uverbs_flow_spec_action_tag	flow_tag;
264 		struct ib_uverbs_flow_spec_action_drop	drop;
265 		struct ib_uverbs_flow_spec_action_handle action;
266 		struct ib_uverbs_flow_spec_action_count flow_count;
267 	};
268 };
269 
270 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
271 					  const void *kern_spec_mask,
272 					  const void *kern_spec_val,
273 					  size_t kern_filter_sz,
274 					  union ib_flow_spec *ib_spec);
275 
276 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_DEVICE);
277 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_PD);
278 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_MR);
279 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_COMP_CHANNEL);
280 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_CQ);
281 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_QP);
282 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_AH);
283 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_MW);
284 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_SRQ);
285 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_FLOW);
286 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_WQ);
287 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL);
288 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_XRCD);
289 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_FLOW_ACTION);
290 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_DM);
291 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_COUNTERS);
292 
293 #define IB_UVERBS_DECLARE_CMD(name)					\
294 	ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,		\
295 				 struct ib_device *ib_dev,              \
296 				 const char __user *buf, int in_len,	\
297 				 int out_len)
298 
299 IB_UVERBS_DECLARE_CMD(get_context);
300 IB_UVERBS_DECLARE_CMD(query_device);
301 IB_UVERBS_DECLARE_CMD(query_port);
302 IB_UVERBS_DECLARE_CMD(alloc_pd);
303 IB_UVERBS_DECLARE_CMD(dealloc_pd);
304 IB_UVERBS_DECLARE_CMD(reg_mr);
305 IB_UVERBS_DECLARE_CMD(rereg_mr);
306 IB_UVERBS_DECLARE_CMD(dereg_mr);
307 IB_UVERBS_DECLARE_CMD(alloc_mw);
308 IB_UVERBS_DECLARE_CMD(dealloc_mw);
309 IB_UVERBS_DECLARE_CMD(create_comp_channel);
310 IB_UVERBS_DECLARE_CMD(create_cq);
311 IB_UVERBS_DECLARE_CMD(resize_cq);
312 IB_UVERBS_DECLARE_CMD(poll_cq);
313 IB_UVERBS_DECLARE_CMD(req_notify_cq);
314 IB_UVERBS_DECLARE_CMD(destroy_cq);
315 IB_UVERBS_DECLARE_CMD(create_qp);
316 IB_UVERBS_DECLARE_CMD(open_qp);
317 IB_UVERBS_DECLARE_CMD(query_qp);
318 IB_UVERBS_DECLARE_CMD(modify_qp);
319 IB_UVERBS_DECLARE_CMD(destroy_qp);
320 IB_UVERBS_DECLARE_CMD(post_send);
321 IB_UVERBS_DECLARE_CMD(post_recv);
322 IB_UVERBS_DECLARE_CMD(post_srq_recv);
323 IB_UVERBS_DECLARE_CMD(create_ah);
324 IB_UVERBS_DECLARE_CMD(destroy_ah);
325 IB_UVERBS_DECLARE_CMD(attach_mcast);
326 IB_UVERBS_DECLARE_CMD(detach_mcast);
327 IB_UVERBS_DECLARE_CMD(create_srq);
328 IB_UVERBS_DECLARE_CMD(modify_srq);
329 IB_UVERBS_DECLARE_CMD(query_srq);
330 IB_UVERBS_DECLARE_CMD(destroy_srq);
331 IB_UVERBS_DECLARE_CMD(create_xsrq);
332 IB_UVERBS_DECLARE_CMD(open_xrcd);
333 IB_UVERBS_DECLARE_CMD(close_xrcd);
334 
335 #define IB_UVERBS_DECLARE_EX_CMD(name)				\
336 	int ib_uverbs_ex_##name(struct ib_uverbs_file *file,	\
337 				struct ib_device *ib_dev,		\
338 				struct ib_udata *ucore,		\
339 				struct ib_udata *uhw)
340 
341 IB_UVERBS_DECLARE_EX_CMD(create_flow);
342 IB_UVERBS_DECLARE_EX_CMD(destroy_flow);
343 IB_UVERBS_DECLARE_EX_CMD(query_device);
344 IB_UVERBS_DECLARE_EX_CMD(create_cq);
345 IB_UVERBS_DECLARE_EX_CMD(create_qp);
346 IB_UVERBS_DECLARE_EX_CMD(create_wq);
347 IB_UVERBS_DECLARE_EX_CMD(modify_wq);
348 IB_UVERBS_DECLARE_EX_CMD(destroy_wq);
349 IB_UVERBS_DECLARE_EX_CMD(create_rwq_ind_table);
350 IB_UVERBS_DECLARE_EX_CMD(destroy_rwq_ind_table);
351 IB_UVERBS_DECLARE_EX_CMD(modify_qp);
352 IB_UVERBS_DECLARE_EX_CMD(modify_cq);
353 
354 #endif /* UVERBS_H */
355