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