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