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