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 #include <linux/wait.h>
51
52 #include <rdma/ib_verbs.h>
53 #include <rdma/ib_umem.h>
54 #include <rdma/ib_user_verbs.h>
55 #include <rdma/uverbs_std_types.h>
56
57 #define UVERBS_MODULE_NAME ib_uverbs
58 #include <rdma/uverbs_named_ioctl.h>
59
60 static inline void
ib_uverbs_init_udata(struct ib_udata * udata,const void __user * ibuf,void __user * obuf,size_t ilen,size_t olen)61 ib_uverbs_init_udata(struct ib_udata *udata,
62 const void __user *ibuf,
63 void __user *obuf,
64 size_t ilen, size_t olen)
65 {
66 udata->inbuf = ibuf;
67 udata->outbuf = obuf;
68 udata->inlen = ilen;
69 udata->outlen = olen;
70 }
71
72 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)73 ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata,
74 const void __user *ibuf,
75 void __user *obuf,
76 size_t ilen, size_t olen)
77 {
78 ib_uverbs_init_udata(udata,
79 ilen ? ibuf : NULL, olen ? obuf : NULL,
80 ilen, olen);
81 }
82
83 /*
84 * Our lifetime rules for these structs are the following:
85 *
86 * struct ib_uverbs_device: One reference is held by the module and
87 * released in ib_uverbs_remove_one(). Another reference is taken by
88 * ib_uverbs_open() each time the character special file is opened,
89 * and released in ib_uverbs_release_file() when the file is released.
90 *
91 * struct ib_uverbs_file: One reference is held by the VFS and
92 * released when the file is closed. Another reference is taken when
93 * an asynchronous event queue file is created and released when the
94 * event file is closed.
95 *
96 * struct ib_uverbs_event_queue: Base structure for
97 * struct ib_uverbs_async_event_file and struct ib_uverbs_completion_event_file.
98 * One reference is held by the VFS and released when the file is closed.
99 * For asynchronous event files, another reference is held by the corresponding
100 * main context file and released when that file is closed. For completion
101 * event files, a reference is taken when a CQ is created that uses the file,
102 * and released when the CQ is destroyed.
103 */
104
105 struct ib_uverbs_device {
106 atomic_t refcount;
107 u32 num_comp_vectors;
108 struct completion comp;
109 struct device dev;
110 struct ib_device __rcu *ib_dev;
111 int devnum;
112 struct cdev cdev;
113 struct rb_root xrcd_tree;
114 struct mutex xrcd_tree_mutex;
115 struct srcu_struct disassociate_srcu;
116 struct mutex lists_mutex; /* protect lists */
117 struct list_head uverbs_file_list;
118 struct uverbs_api *uapi;
119 };
120
121 struct ib_uverbs_event_queue {
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 };
128
129 struct ib_uverbs_async_event_file {
130 struct ib_uobject uobj;
131 struct ib_uverbs_event_queue ev_queue;
132 struct ib_event_handler event_handler;
133 };
134
135 struct ib_uverbs_completion_event_file {
136 struct ib_uobject uobj;
137 struct ib_uverbs_event_queue ev_queue;
138 };
139
140 struct ib_uverbs_file {
141 struct kref ref;
142 struct ib_uverbs_device *device;
143 struct mutex ucontext_lock;
144 /*
145 * ucontext must be accessed via ib_uverbs_get_ucontext() or with
146 * ucontext_lock held
147 */
148 struct ib_ucontext *ucontext;
149 struct ib_uverbs_async_event_file *async_file;
150 struct list_head list;
151
152 /*
153 * To access the uobjects list hw_destroy_rwsem must be held for write
154 * OR hw_destroy_rwsem held for read AND uobjects_lock held.
155 * hw_destroy_rwsem should be called across any destruction of the HW
156 * object of an associated uobject.
157 */
158 struct rw_semaphore hw_destroy_rwsem;
159 spinlock_t uobjects_lock;
160 struct list_head uobjects;
161
162 struct mutex umap_lock;
163 struct list_head umaps;
164
165 struct xarray idr;
166 };
167
168 struct ib_uverbs_event {
169 union {
170 struct ib_uverbs_async_event_desc async;
171 struct ib_uverbs_comp_event_desc comp;
172 } desc;
173 struct list_head list;
174 struct list_head obj_list;
175 u32 *counter;
176 };
177
178 struct ib_uverbs_mcast_entry {
179 struct list_head list;
180 union ib_gid gid;
181 u16 lid;
182 };
183
184 struct ib_uevent_object {
185 struct ib_uobject uobject;
186 /* List member for ib_uverbs_async_event_file list */
187 struct list_head event_list;
188 u32 events_reported;
189 };
190
191 struct ib_uxrcd_object {
192 struct ib_uobject uobject;
193 atomic_t refcnt;
194 };
195
196 struct ib_usrq_object {
197 struct ib_uevent_object uevent;
198 struct ib_uxrcd_object *uxrcd;
199 };
200
201 struct ib_uqp_object {
202 struct ib_uevent_object uevent;
203 /* lock for mcast list */
204 struct mutex mcast_lock;
205 struct list_head mcast_list;
206 struct ib_uxrcd_object *uxrcd;
207 };
208
209 struct ib_uwq_object {
210 struct ib_uevent_object uevent;
211 };
212
213 struct ib_ucq_object {
214 struct ib_uevent_object uevent;
215 struct list_head comp_list;
216 u32 comp_events_reported;
217 };
218
219 extern const struct file_operations uverbs_event_fops;
220 extern const struct file_operations uverbs_async_event_fops;
221 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue);
222 void ib_uverbs_init_async_event_file(struct ib_uverbs_async_event_file *ev_file);
223 void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue);
224 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res);
225
226 int ib_alloc_ucontext(struct uverbs_attr_bundle *attrs);
227 int ib_init_ucontext(struct uverbs_attr_bundle *attrs);
228
229 void ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file *ev_file,
230 struct ib_ucq_object *uobj);
231 void ib_uverbs_release_uevent(struct ib_uevent_object *uobj);
232 void ib_uverbs_release_file(struct kref *ref);
233
234 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
235 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
236 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
237 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
238 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
239 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject, struct ib_xrcd *xrcd,
240 enum rdma_remove_reason why,
241 struct uverbs_attr_bundle *attrs);
242
243 int uverbs_dealloc_mw(struct ib_mw *mw);
244 void ib_uverbs_detach_umcast(struct ib_qp *qp,
245 struct ib_uqp_object *uobj);
246
247 long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
248
249 struct ib_uverbs_flow_spec {
250 union {
251 union {
252 struct ib_uverbs_flow_spec_hdr hdr;
253 struct {
254 __u32 type;
255 __u16 size;
256 __u16 reserved;
257 };
258 };
259 struct ib_uverbs_flow_spec_eth eth;
260 struct ib_uverbs_flow_spec_ipv4 ipv4;
261 struct ib_uverbs_flow_spec_esp esp;
262 struct ib_uverbs_flow_spec_tcp_udp tcp_udp;
263 struct ib_uverbs_flow_spec_ipv6 ipv6;
264 struct ib_uverbs_flow_spec_action_tag flow_tag;
265 struct ib_uverbs_flow_spec_action_drop drop;
266 struct ib_uverbs_flow_spec_action_handle action;
267 struct ib_uverbs_flow_spec_action_count flow_count;
268 };
269 };
270
271 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
272 const void *kern_spec_mask,
273 const void *kern_spec_val,
274 size_t kern_filter_sz,
275 union ib_flow_spec *ib_spec);
276
277 /*
278 * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the
279 * PortInfo CapabilityMask, but was extended with unique bits.
280 */
make_port_cap_flags(const struct ib_port_attr * attr)281 static inline u32 make_port_cap_flags(const struct ib_port_attr *attr)
282 {
283 u32 res;
284
285 /* All IBA CapabilityMask bits are passed through here, except bit 26,
286 * which is overridden with IP_BASED_GIDS. This is due to a historical
287 * mistake in the implementation of IP_BASED_GIDS. Otherwise all other
288 * bits match the IBA definition across all kernel versions.
289 */
290 res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS;
291
292 if (attr->ip_gids)
293 res |= IB_UVERBS_PCF_IP_BASED_GIDS;
294
295 return res;
296 }
297
298
299 void copy_port_attr_to_resp(struct ib_port_attr *attr,
300 struct ib_uverbs_query_port_resp *resp,
301 struct ib_device *ib_dev, u8 port_num);
302 #endif /* UVERBS_H */
303