2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
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.
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:
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
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.
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
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>
53 #include <rdma/ib_verbs.h>
54 #include <rdma/ib_umem.h>
55 #include <rdma/ib_user_verbs.h>
57 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
59 (udata)->inbuf = (const void __user *) (ibuf); \
60 (udata)->outbuf = (void __user *) (obuf); \
61 (udata)->inlen = (ilen); \
62 (udata)->outlen = (olen); \
65 #define INIT_UDATA_BUF_OR_NULL(udata, ibuf, obuf, ilen, olen) \
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); \
74 * Our lifetime rules for these structs are the following:
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.
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.
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.
94 struct ib_uverbs_device {
97 struct completion comp;
99 struct ib_device __rcu *ib_dev;
102 struct rb_root xrcd_tree;
103 struct mutex xrcd_tree_mutex;
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;
111 struct ib_uverbs_event_file {
114 struct ib_uverbs_file *uverbs_file;
117 wait_queue_head_t poll_wait;
118 struct fasync_struct *async_queue;
119 struct list_head event_list;
120 struct list_head list;
123 struct ib_uverbs_file {
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;
135 struct ib_uverbs_event {
137 struct ib_uverbs_async_event_desc async;
138 struct ib_uverbs_comp_event_desc comp;
140 struct list_head list;
141 struct list_head obj_list;
145 struct ib_uverbs_mcast_entry {
146 struct list_head list;
151 struct ib_uevent_object {
152 struct ib_uobject uobject;
153 struct list_head event_list;
157 struct ib_uxrcd_object {
158 struct ib_uobject uobject;
162 struct ib_usrq_object {
163 struct ib_uevent_object uevent;
164 struct ib_uxrcd_object *uxrcd;
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;
175 struct ib_uwq_object {
176 struct ib_uevent_object uevent;
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;
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;
201 void idr_remove_uobj(struct idr *idp, struct ib_uobject *uobj);
203 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
204 struct ib_device *ib_dev,
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);
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);
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);
224 int uverbs_dealloc_mw(struct ib_mw *mw);
226 struct ib_uverbs_flow_spec {
229 struct ib_uverbs_flow_spec_hdr hdr;
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;
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, \
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);
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)
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);
302 #endif /* UVERBS_H */