1 /* 2 * Copyright (c) 2005 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved. 4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved. 5 * Copyright (c) 2005 Voltaire, Inc. All rights reserved. 6 * Copyright (c) 2005 PathScale, Inc. All rights reserved. 7 * 8 * This software is available to you under a choice of one of two 9 * licenses. You may choose to be licensed under the terms of the GNU 10 * General Public License (GPL) Version 2, available from the file 11 * COPYING in the main directory of this source tree, or the 12 * OpenIB.org BSD license below: 13 * 14 * Redistribution and use in source and binary forms, with or 15 * without modification, are permitted provided that the following 16 * conditions are met: 17 * 18 * - Redistributions of source code must retain the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer. 21 * 22 * - Redistributions in binary form must reproduce the above 23 * copyright notice, this list of conditions and the following 24 * disclaimer in the documentation and/or other materials 25 * provided with the distribution. 26 * 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 * SOFTWARE. 35 */ 36 37 #ifndef UVERBS_H 38 #define UVERBS_H 39 40 #include <linux/kref.h> 41 #include <linux/idr.h> 42 #include <linux/mutex.h> 43 #include <linux/completion.h> 44 #include <linux/cdev.h> 45 46 #include <rdma/ib_verbs.h> 47 #include <rdma/ib_umem.h> 48 #include <rdma/ib_user_verbs.h> 49 #include <rdma/uverbs_std_types.h> 50 51 #define UVERBS_MODULE_NAME ib_uverbs 52 #include <rdma/uverbs_named_ioctl.h> 53 54 static inline void 55 ib_uverbs_init_udata(struct ib_udata *udata, 56 const void __user *ibuf, 57 void __user *obuf, 58 size_t ilen, size_t olen) 59 { 60 udata->inbuf = ibuf; 61 udata->outbuf = obuf; 62 udata->inlen = ilen; 63 udata->outlen = olen; 64 } 65 66 static inline void 67 ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata, 68 const void __user *ibuf, 69 void __user *obuf, 70 size_t ilen, size_t olen) 71 { 72 ib_uverbs_init_udata(udata, 73 ilen ? ibuf : NULL, olen ? obuf : NULL, 74 ilen, olen); 75 } 76 77 /* 78 * Our lifetime rules for these structs are the following: 79 * 80 * struct ib_uverbs_device: One reference is held by the module and 81 * released in ib_uverbs_remove_one(). Another reference is taken by 82 * ib_uverbs_open() each time the character special file is opened, 83 * and released in ib_uverbs_release_file() when the file is released. 84 * 85 * struct ib_uverbs_file: One reference is held by the VFS and 86 * released when the file is closed. Another reference is taken when 87 * an asynchronous event queue file is created and released when the 88 * event file is closed. 89 * 90 * struct ib_uverbs_event_queue: Base structure for 91 * struct ib_uverbs_async_event_file and struct ib_uverbs_completion_event_file. 92 * One reference is held by the VFS and released when the file is closed. 93 * For asynchronous event files, another reference is held by the corresponding 94 * main context file and released when that file is closed. For completion 95 * event files, a reference is taken when a CQ is created that uses the file, 96 * and released when the CQ is destroyed. 97 */ 98 99 struct ib_uverbs_device { 100 refcount_t refcount; 101 u32 num_comp_vectors; 102 struct completion comp; 103 struct device dev; 104 /* First group for device attributes, NULL terminated array */ 105 const struct attribute_group *groups[2]; 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 srcu_struct disassociate_srcu; 112 struct mutex lists_mutex; /* protect lists */ 113 struct list_head uverbs_file_list; 114 struct uverbs_api *uapi; 115 }; 116 117 struct ib_uverbs_event_queue { 118 spinlock_t lock; 119 int is_closed; 120 wait_queue_head_t poll_wait; 121 struct fasync_struct *async_queue; 122 struct list_head event_list; 123 }; 124 125 struct ib_uverbs_async_event_file { 126 struct ib_uobject uobj; 127 struct ib_uverbs_event_queue ev_queue; 128 struct ib_event_handler event_handler; 129 }; 130 131 struct ib_uverbs_completion_event_file { 132 struct ib_uobject uobj; 133 struct ib_uverbs_event_queue ev_queue; 134 }; 135 136 struct ib_uverbs_dmabuf_file { 137 struct ib_uobject uobj; 138 struct dma_buf *dmabuf; 139 struct list_head dmabufs_elm; 140 struct rdma_user_mmap_entry *mmap_entry; 141 struct phys_vec phys_vec; 142 struct p2pdma_provider *provider; 143 struct kref kref; 144 struct completion comp; 145 u8 revoked :1; 146 }; 147 148 struct ib_uverbs_event { 149 union { 150 struct ib_uverbs_async_event_desc async; 151 struct ib_uverbs_comp_event_desc comp; 152 } desc; 153 struct list_head list; 154 struct list_head obj_list; 155 u32 *counter; 156 }; 157 158 struct ib_uverbs_mcast_entry { 159 struct list_head list; 160 union ib_gid gid; 161 u16 lid; 162 }; 163 164 struct ib_uevent_object { 165 struct ib_uobject uobject; 166 struct ib_uverbs_async_event_file *event_file; 167 /* List member for ib_uverbs_async_event_file list */ 168 struct list_head event_list; 169 u32 events_reported; 170 }; 171 172 struct ib_uxrcd_object { 173 struct ib_uobject uobject; 174 atomic_t refcnt; 175 }; 176 177 struct ib_usrq_object { 178 struct ib_uevent_object uevent; 179 struct ib_uxrcd_object *uxrcd; 180 }; 181 182 struct ib_uqp_object { 183 struct ib_uevent_object uevent; 184 /* lock for mcast list */ 185 struct mutex mcast_lock; 186 struct list_head mcast_list; 187 struct ib_uxrcd_object *uxrcd; 188 }; 189 190 struct ib_uwq_object { 191 struct ib_uevent_object uevent; 192 }; 193 194 struct ib_ucq_object { 195 struct ib_uevent_object uevent; 196 struct list_head comp_list; 197 u32 comp_events_reported; 198 }; 199 200 extern const struct file_operations uverbs_event_fops; 201 extern const struct file_operations uverbs_async_event_fops; 202 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue); 203 void ib_uverbs_init_async_event_file(struct ib_uverbs_async_event_file *ev_file); 204 void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue); 205 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res); 206 207 int ib_alloc_ucontext(struct uverbs_attr_bundle *attrs); 208 int ib_init_ucontext(struct uverbs_attr_bundle *attrs); 209 210 void ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file *ev_file, 211 struct ib_ucq_object *uobj); 212 void ib_uverbs_release_uevent(struct ib_uevent_object *uobj); 213 void ib_uverbs_release_file(struct kref *ref); 214 void ib_uverbs_async_handler(struct ib_uverbs_async_event_file *async_file, 215 __u64 element, __u64 event, 216 struct list_head *obj_list, u32 *counter); 217 218 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context); 219 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr); 220 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr); 221 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr); 222 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr); 223 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject, struct ib_xrcd *xrcd, 224 enum rdma_remove_reason why, 225 struct uverbs_attr_bundle *attrs); 226 227 int uverbs_dealloc_mw(struct ib_mw *mw); 228 void ib_uverbs_detach_umcast(struct ib_qp *qp, 229 struct ib_uqp_object *uobj); 230 231 struct bundle_alloc_head { 232 struct_group_tagged(bundle_alloc_head_hdr, hdr, 233 struct bundle_alloc_head *next; 234 ); 235 u8 data[]; 236 }; 237 238 struct bundle_priv { 239 /* Must be first */ 240 struct bundle_alloc_head_hdr alloc_head; 241 struct bundle_alloc_head *allocated_mem; 242 size_t internal_avail; 243 size_t internal_used; 244 245 struct radix_tree_root *radix; 246 void __rcu **radix_slots; 247 unsigned long radix_slots_len; 248 u32 method_key; 249 250 struct ib_uverbs_attr __user *user_attrs; 251 struct ib_uverbs_attr *uattrs; 252 253 DECLARE_BITMAP(uobj_finalize, UVERBS_API_ATTR_BKEY_LEN); 254 DECLARE_BITMAP(spec_finalize, UVERBS_API_ATTR_BKEY_LEN); 255 DECLARE_BITMAP(uobj_hw_obj_valid, UVERBS_API_ATTR_BKEY_LEN); 256 257 /* 258 * Must be last. bundle ends in a flex array which overlaps 259 * internal_buffer. 260 */ 261 struct uverbs_attr_bundle_hdr bundle; 262 u64 internal_buffer[32]; 263 }; 264 265 static inline int uverbs_set_output(const struct uverbs_attr_bundle *bundle, 266 const struct uverbs_attr *attr) 267 { 268 struct bundle_priv *pbundle = 269 container_of(&bundle->hdr, struct bundle_priv, bundle); 270 u16 flags; 271 272 flags = pbundle->uattrs[attr->ptr_attr.uattr_idx].flags | 273 UVERBS_ATTR_F_VALID_OUTPUT; 274 if (put_user(flags, 275 &pbundle->user_attrs[attr->ptr_attr.uattr_idx].flags)) 276 return -EFAULT; 277 return 0; 278 } 279 280 long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 281 282 struct ib_uverbs_flow_spec { 283 union { 284 union { 285 struct ib_uverbs_flow_spec_hdr hdr; 286 struct { 287 __u32 type; 288 __u16 size; 289 __u16 reserved; 290 }; 291 }; 292 struct ib_uverbs_flow_spec_eth eth; 293 struct ib_uverbs_flow_spec_ipv4 ipv4; 294 struct ib_uverbs_flow_spec_esp esp; 295 struct ib_uverbs_flow_spec_tcp_udp tcp_udp; 296 struct ib_uverbs_flow_spec_ipv6 ipv6; 297 struct ib_uverbs_flow_spec_action_tag flow_tag; 298 struct ib_uverbs_flow_spec_action_drop drop; 299 struct ib_uverbs_flow_spec_action_handle action; 300 struct ib_uverbs_flow_spec_action_count flow_count; 301 }; 302 }; 303 304 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type, 305 const void *kern_spec_mask, 306 const void *kern_spec_val, 307 size_t kern_filter_sz, 308 union ib_flow_spec *ib_spec); 309 310 /* 311 * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the 312 * PortInfo CapabilityMask, but was extended with unique bits. 313 */ 314 static inline u32 make_port_cap_flags(const struct ib_port_attr *attr) 315 { 316 u32 res; 317 318 /* All IBA CapabilityMask bits are passed through here, except bit 26, 319 * which is overridden with IP_BASED_GIDS. This is due to a historical 320 * mistake in the implementation of IP_BASED_GIDS. Otherwise all other 321 * bits match the IBA definition across all kernel versions. 322 */ 323 res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS; 324 325 if (attr->ip_gids) 326 res |= IB_UVERBS_PCF_IP_BASED_GIDS; 327 328 return res; 329 } 330 331 static inline struct ib_uverbs_async_event_file * 332 ib_uverbs_get_async_event(struct uverbs_attr_bundle *attrs, 333 u16 id) 334 { 335 struct ib_uobject *async_ev_file_uobj; 336 struct ib_uverbs_async_event_file *async_ev_file; 337 338 async_ev_file_uobj = uverbs_attr_get_uobject(attrs, id); 339 if (IS_ERR(async_ev_file_uobj)) 340 async_ev_file = READ_ONCE(attrs->ufile->default_async_file); 341 else 342 async_ev_file = container_of(async_ev_file_uobj, 343 struct ib_uverbs_async_event_file, 344 uobj); 345 if (async_ev_file) 346 uverbs_uobject_get(&async_ev_file->uobj); 347 return async_ev_file; 348 } 349 350 void copy_port_attr_to_resp(struct ib_port_attr *attr, 351 struct ib_uverbs_query_port_resp *resp, 352 struct ib_device *ib_dev, u8 port_num); 353 354 static inline void ib_uverbs_dmabuf_done(struct kref *kref) 355 { 356 struct ib_uverbs_dmabuf_file *priv = 357 container_of(kref, struct ib_uverbs_dmabuf_file, kref); 358 359 complete(&priv->comp); 360 } 361 362 int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile, 363 enum rdma_remove_reason reason); 364 365 static inline void ib_uverbs_comp_dev(struct ib_uverbs_device *dev) 366 { 367 complete(&dev->comp); 368 } 369 370 371 #endif /* UVERBS_H */ 372