Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005 Topspin Communications. All rights reserved. |
| 3 | * Copyright (c) 2005 Cisco Systems. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 4 | * Copyright (c) 2005 Mellanox Technologies. All rights reserved. |
| 5 | * Copyright (c) 2005 Voltaire, Inc. All rights reserved. |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 6 | * |
| 7 | * This software is available to you under a choice of one of two |
| 8 | * licenses. You may choose to be licensed under the terms of the GNU |
| 9 | * General Public License (GPL) Version 2, available from the file |
| 10 | * COPYING in the main directory of this source tree, or the |
| 11 | * OpenIB.org BSD license below: |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or |
| 14 | * without modification, are permitted provided that the following |
| 15 | * conditions are met: |
| 16 | * |
| 17 | * - Redistributions of source code must retain the above |
| 18 | * copyright notice, this list of conditions and the following |
| 19 | * disclaimer. |
| 20 | * |
| 21 | * - Redistributions in binary form must reproduce the above |
| 22 | * copyright notice, this list of conditions and the following |
| 23 | * disclaimer in the documentation and/or other materials |
| 24 | * provided with the distribution. |
| 25 | * |
| 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 30 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 31 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 32 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 | * SOFTWARE. |
| 34 | * |
| 35 | * $Id: uverbs_main.c 2733 2005-06-28 19:14:34Z roland $ |
| 36 | */ |
| 37 | |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/device.h> |
| 41 | #include <linux/err.h> |
| 42 | #include <linux/fs.h> |
| 43 | #include <linux/poll.h> |
| 44 | #include <linux/file.h> |
| 45 | #include <linux/mount.h> |
| 46 | |
| 47 | #include <asm/uaccess.h> |
| 48 | |
| 49 | #include "uverbs.h" |
| 50 | |
| 51 | MODULE_AUTHOR("Roland Dreier"); |
| 52 | MODULE_DESCRIPTION("InfiniBand userspace verbs access"); |
| 53 | MODULE_LICENSE("Dual BSD/GPL"); |
| 54 | |
| 55 | #define INFINIBANDEVENTFS_MAGIC 0x49426576 /* "IBev" */ |
| 56 | |
| 57 | enum { |
| 58 | IB_UVERBS_MAJOR = 231, |
| 59 | IB_UVERBS_BASE_MINOR = 192, |
| 60 | IB_UVERBS_MAX_DEVICES = 32 |
| 61 | }; |
| 62 | |
| 63 | #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR) |
| 64 | |
| 65 | DECLARE_MUTEX(ib_uverbs_idr_mutex); |
| 66 | DEFINE_IDR(ib_uverbs_pd_idr); |
| 67 | DEFINE_IDR(ib_uverbs_mr_idr); |
| 68 | DEFINE_IDR(ib_uverbs_mw_idr); |
| 69 | DEFINE_IDR(ib_uverbs_ah_idr); |
| 70 | DEFINE_IDR(ib_uverbs_cq_idr); |
| 71 | DEFINE_IDR(ib_uverbs_qp_idr); |
Roland Dreier | f520ba5 | 2005-08-18 12:24:13 -0700 | [diff] [blame] | 72 | DEFINE_IDR(ib_uverbs_srq_idr); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 73 | |
| 74 | static spinlock_t map_lock; |
| 75 | static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES); |
| 76 | |
| 77 | static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file, |
| 78 | const char __user *buf, int in_len, |
| 79 | int out_len) = { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 80 | [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context, |
| 81 | [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device, |
| 82 | [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port, |
| 83 | [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd, |
| 84 | [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd, |
| 85 | [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr, |
| 86 | [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr, |
| 87 | [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel, |
| 88 | [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq, |
| 89 | [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq, |
| 90 | [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp, |
| 91 | [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp, |
| 92 | [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp, |
| 93 | [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast, |
| 94 | [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast, |
| 95 | [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq, |
| 96 | [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq, |
| 97 | [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq, |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static struct vfsmount *uverbs_event_mnt; |
| 101 | |
| 102 | static void ib_uverbs_add_one(struct ib_device *device); |
| 103 | static void ib_uverbs_remove_one(struct ib_device *device); |
| 104 | |
| 105 | static int ib_dealloc_ucontext(struct ib_ucontext *context) |
| 106 | { |
| 107 | struct ib_uobject *uobj, *tmp; |
| 108 | |
| 109 | if (!context) |
| 110 | return 0; |
| 111 | |
| 112 | down(&ib_uverbs_idr_mutex); |
| 113 | |
| 114 | /* XXX Free AHs */ |
| 115 | |
| 116 | list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) { |
| 117 | struct ib_qp *qp = idr_find(&ib_uverbs_qp_idr, uobj->id); |
| 118 | idr_remove(&ib_uverbs_qp_idr, uobj->id); |
| 119 | ib_destroy_qp(qp); |
| 120 | list_del(&uobj->list); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 121 | kfree(container_of(uobj, struct ib_uevent_object, uobject)); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) { |
| 125 | struct ib_cq *cq = idr_find(&ib_uverbs_cq_idr, uobj->id); |
| 126 | idr_remove(&ib_uverbs_cq_idr, uobj->id); |
| 127 | ib_destroy_cq(cq); |
| 128 | list_del(&uobj->list); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 129 | kfree(container_of(uobj, struct ib_ucq_object, uobject)); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Roland Dreier | f520ba5 | 2005-08-18 12:24:13 -0700 | [diff] [blame] | 132 | list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) { |
| 133 | struct ib_srq *srq = idr_find(&ib_uverbs_srq_idr, uobj->id); |
| 134 | idr_remove(&ib_uverbs_srq_idr, uobj->id); |
| 135 | ib_destroy_srq(srq); |
| 136 | list_del(&uobj->list); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 137 | kfree(container_of(uobj, struct ib_uevent_object, uobject)); |
Roland Dreier | f520ba5 | 2005-08-18 12:24:13 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 140 | /* XXX Free MWs */ |
| 141 | |
| 142 | list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) { |
| 143 | struct ib_mr *mr = idr_find(&ib_uverbs_mr_idr, uobj->id); |
Roland Dreier | e1bcfcaa | 2005-08-26 18:34:14 -0700 | [diff] [blame] | 144 | struct ib_device *mrdev = mr->device; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 145 | struct ib_umem_object *memobj; |
| 146 | |
| 147 | idr_remove(&ib_uverbs_mr_idr, uobj->id); |
| 148 | ib_dereg_mr(mr); |
| 149 | |
| 150 | memobj = container_of(uobj, struct ib_umem_object, uobject); |
Roland Dreier | e1bcfcaa | 2005-08-26 18:34:14 -0700 | [diff] [blame] | 151 | ib_umem_release_on_close(mrdev, &memobj->umem); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 152 | |
| 153 | list_del(&uobj->list); |
| 154 | kfree(memobj); |
| 155 | } |
| 156 | |
| 157 | list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) { |
| 158 | struct ib_pd *pd = idr_find(&ib_uverbs_pd_idr, uobj->id); |
| 159 | idr_remove(&ib_uverbs_pd_idr, uobj->id); |
| 160 | ib_dealloc_pd(pd); |
| 161 | list_del(&uobj->list); |
| 162 | kfree(uobj); |
| 163 | } |
| 164 | |
| 165 | up(&ib_uverbs_idr_mutex); |
| 166 | |
| 167 | return context->device->dealloc_ucontext(context); |
| 168 | } |
| 169 | |
| 170 | static void ib_uverbs_release_file(struct kref *ref) |
| 171 | { |
| 172 | struct ib_uverbs_file *file = |
| 173 | container_of(ref, struct ib_uverbs_file, ref); |
| 174 | |
| 175 | module_put(file->device->ib_dev->owner); |
| 176 | kfree(file); |
| 177 | } |
| 178 | |
| 179 | static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf, |
| 180 | size_t count, loff_t *pos) |
| 181 | { |
| 182 | struct ib_uverbs_event_file *file = filp->private_data; |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 183 | struct ib_uverbs_event *event; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 184 | int eventsz; |
| 185 | int ret = 0; |
| 186 | |
| 187 | spin_lock_irq(&file->lock); |
| 188 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 189 | while (list_empty(&file->event_list)) { |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 190 | spin_unlock_irq(&file->lock); |
| 191 | |
| 192 | if (filp->f_flags & O_NONBLOCK) |
| 193 | return -EAGAIN; |
| 194 | |
| 195 | if (wait_event_interruptible(file->poll_wait, |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 196 | !list_empty(&file->event_list))) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 197 | return -ERESTARTSYS; |
| 198 | |
| 199 | spin_lock_irq(&file->lock); |
| 200 | } |
| 201 | |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 202 | event = list_entry(file->event_list.next, struct ib_uverbs_event, list); |
| 203 | |
| 204 | if (file->is_async) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 205 | eventsz = sizeof (struct ib_uverbs_async_event_desc); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 206 | else |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 207 | eventsz = sizeof (struct ib_uverbs_comp_event_desc); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 208 | |
| 209 | if (eventsz > count) { |
| 210 | ret = -EINVAL; |
| 211 | event = NULL; |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 212 | } else { |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 213 | list_del(file->event_list.next); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 214 | if (event->counter) { |
| 215 | ++(*event->counter); |
| 216 | list_del(&event->obj_list); |
| 217 | } |
| 218 | } |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 219 | |
| 220 | spin_unlock_irq(&file->lock); |
| 221 | |
| 222 | if (event) { |
| 223 | if (copy_to_user(buf, event, eventsz)) |
| 224 | ret = -EFAULT; |
| 225 | else |
| 226 | ret = eventsz; |
| 227 | } |
| 228 | |
| 229 | kfree(event); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static unsigned int ib_uverbs_event_poll(struct file *filp, |
| 235 | struct poll_table_struct *wait) |
| 236 | { |
| 237 | unsigned int pollflags = 0; |
| 238 | struct ib_uverbs_event_file *file = filp->private_data; |
| 239 | |
| 240 | poll_wait(filp, &file->poll_wait, wait); |
| 241 | |
| 242 | spin_lock_irq(&file->lock); |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 243 | if (!list_empty(&file->event_list)) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 244 | pollflags = POLLIN | POLLRDNORM; |
| 245 | spin_unlock_irq(&file->lock); |
| 246 | |
| 247 | return pollflags; |
| 248 | } |
| 249 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 250 | void ib_uverbs_release_event_file(struct kref *ref) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 251 | { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 252 | struct ib_uverbs_event_file *file = |
| 253 | container_of(ref, struct ib_uverbs_event_file, ref); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 254 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 255 | kfree(file); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Gleb Natapov | abdf119 | 2005-07-27 14:40:00 -0700 | [diff] [blame] | 258 | static int ib_uverbs_event_fasync(int fd, struct file *filp, int on) |
| 259 | { |
| 260 | struct ib_uverbs_event_file *file = filp->private_data; |
| 261 | |
| 262 | return fasync_helper(fd, filp, on, &file->async_queue); |
| 263 | } |
| 264 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 265 | static int ib_uverbs_event_close(struct inode *inode, struct file *filp) |
| 266 | { |
| 267 | struct ib_uverbs_event_file *file = filp->private_data; |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 268 | struct ib_uverbs_event *entry, *tmp; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 269 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 270 | spin_lock_irq(&file->lock); |
| 271 | file->file = NULL; |
| 272 | list_for_each_entry_safe(entry, tmp, &file->event_list, list) { |
| 273 | if (entry->counter) |
| 274 | list_del(&entry->obj_list); |
| 275 | kfree(entry); |
| 276 | } |
| 277 | spin_unlock_irq(&file->lock); |
| 278 | |
Gleb Natapov | abdf119 | 2005-07-27 14:40:00 -0700 | [diff] [blame] | 279 | ib_uverbs_event_fasync(-1, filp, 0); |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 280 | |
| 281 | if (file->is_async) { |
| 282 | ib_unregister_event_handler(&file->uverbs_file->event_handler); |
| 283 | kref_put(&file->uverbs_file->ref, ib_uverbs_release_file); |
| 284 | } |
| 285 | kref_put(&file->ref, ib_uverbs_release_event_file); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static struct file_operations uverbs_event_fops = { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 291 | .owner = THIS_MODULE, |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 292 | .read = ib_uverbs_event_read, |
| 293 | .poll = ib_uverbs_event_poll, |
Gleb Natapov | abdf119 | 2005-07-27 14:40:00 -0700 | [diff] [blame] | 294 | .release = ib_uverbs_event_close, |
| 295 | .fasync = ib_uverbs_event_fasync |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context) |
| 299 | { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 300 | struct ib_uverbs_event_file *file = cq_context; |
| 301 | struct ib_ucq_object *uobj; |
| 302 | struct ib_uverbs_event *entry; |
| 303 | unsigned long flags; |
| 304 | |
| 305 | if (!file) |
| 306 | return; |
| 307 | |
| 308 | spin_lock_irqsave(&file->lock, flags); |
| 309 | if (!file->file) { |
| 310 | spin_unlock_irqrestore(&file->lock, flags); |
| 311 | return; |
| 312 | } |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 313 | |
| 314 | entry = kmalloc(sizeof *entry, GFP_ATOMIC); |
Roland Dreier | 305a7e8 | 2005-10-11 15:39:38 -0700 | [diff] [blame] | 315 | if (!entry) { |
| 316 | spin_unlock_irqrestore(&file->lock, flags); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 317 | return; |
Roland Dreier | 305a7e8 | 2005-10-11 15:39:38 -0700 | [diff] [blame] | 318 | } |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 319 | |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 320 | uobj = container_of(cq->uobject, struct ib_ucq_object, uobject); |
| 321 | |
| 322 | entry->desc.comp.cq_handle = cq->uobject->user_handle; |
| 323 | entry->counter = &uobj->comp_events_reported; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 324 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 325 | list_add_tail(&entry->list, &file->event_list); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 326 | list_add_tail(&entry->obj_list, &uobj->comp_list); |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 327 | spin_unlock_irqrestore(&file->lock, flags); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 328 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 329 | wake_up_interruptible(&file->poll_wait); |
| 330 | kill_fasync(&file->async_queue, SIGIO, POLL_IN); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void ib_uverbs_async_handler(struct ib_uverbs_file *file, |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 334 | __u64 element, __u64 event, |
| 335 | struct list_head *obj_list, |
| 336 | u32 *counter) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 337 | { |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 338 | struct ib_uverbs_event *entry; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 339 | unsigned long flags; |
| 340 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 341 | spin_lock_irqsave(&file->async_file->lock, flags); |
| 342 | if (!file->async_file->file) { |
| 343 | spin_unlock_irqrestore(&file->async_file->lock, flags); |
| 344 | return; |
| 345 | } |
| 346 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 347 | entry = kmalloc(sizeof *entry, GFP_ATOMIC); |
Roland Dreier | 305a7e8 | 2005-10-11 15:39:38 -0700 | [diff] [blame] | 348 | if (!entry) { |
| 349 | spin_unlock_irqrestore(&file->async_file->lock, flags); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 350 | return; |
Roland Dreier | 305a7e8 | 2005-10-11 15:39:38 -0700 | [diff] [blame] | 351 | } |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 352 | |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 353 | entry->desc.async.element = element; |
| 354 | entry->desc.async.event_type = event; |
| 355 | entry->counter = counter; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 356 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 357 | list_add_tail(&entry->list, &file->async_file->event_list); |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 358 | if (obj_list) |
| 359 | list_add_tail(&entry->obj_list, obj_list); |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 360 | spin_unlock_irqrestore(&file->async_file->lock, flags); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 361 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 362 | wake_up_interruptible(&file->async_file->poll_wait); |
| 363 | kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr) |
| 367 | { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 368 | struct ib_uverbs_event_file *ev_file = context_ptr; |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 369 | struct ib_ucq_object *uobj; |
| 370 | |
| 371 | uobj = container_of(event->element.cq->uobject, |
| 372 | struct ib_ucq_object, uobject); |
| 373 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 374 | ib_uverbs_async_handler(ev_file->uverbs_file, uobj->uobject.user_handle, |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 375 | event->event, &uobj->async_list, |
| 376 | &uobj->async_events_reported); |
| 377 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr) |
| 381 | { |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 382 | struct ib_uevent_object *uobj; |
| 383 | |
| 384 | uobj = container_of(event->element.qp->uobject, |
| 385 | struct ib_uevent_object, uobject); |
| 386 | |
| 387 | ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, |
| 388 | event->event, &uobj->event_list, |
| 389 | &uobj->events_reported); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Roland Dreier | f520ba5 | 2005-08-18 12:24:13 -0700 | [diff] [blame] | 392 | void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr) |
| 393 | { |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 394 | struct ib_uevent_object *uobj; |
| 395 | |
| 396 | uobj = container_of(event->element.srq->uobject, |
| 397 | struct ib_uevent_object, uobject); |
| 398 | |
| 399 | ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, |
| 400 | event->event, &uobj->event_list, |
| 401 | &uobj->events_reported); |
Roland Dreier | f520ba5 | 2005-08-18 12:24:13 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 404 | void ib_uverbs_event_handler(struct ib_event_handler *handler, |
| 405 | struct ib_event *event) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 406 | { |
| 407 | struct ib_uverbs_file *file = |
| 408 | container_of(handler, struct ib_uverbs_file, event_handler); |
| 409 | |
Roland Dreier | 63aaf64 | 2005-09-09 15:55:08 -0700 | [diff] [blame] | 410 | ib_uverbs_async_handler(file, event->element.port_num, event->event, |
| 411 | NULL, NULL); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 414 | struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file, |
| 415 | int is_async, int *fd) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 416 | { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 417 | struct ib_uverbs_event_file *ev_file; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 418 | struct file *filp; |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 419 | int ret; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 420 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 421 | ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL); |
| 422 | if (!ev_file) |
| 423 | return ERR_PTR(-ENOMEM); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 424 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 425 | kref_init(&ev_file->ref); |
| 426 | spin_lock_init(&ev_file->lock); |
| 427 | INIT_LIST_HEAD(&ev_file->event_list); |
| 428 | init_waitqueue_head(&ev_file->poll_wait); |
| 429 | ev_file->uverbs_file = uverbs_file; |
| 430 | ev_file->async_queue = NULL; |
| 431 | ev_file->is_async = is_async; |
| 432 | |
| 433 | *fd = get_unused_fd(); |
| 434 | if (*fd < 0) { |
| 435 | ret = *fd; |
| 436 | goto err; |
| 437 | } |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 438 | |
| 439 | filp = get_empty_filp(); |
| 440 | if (!filp) { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 441 | ret = -ENFILE; |
| 442 | goto err_fd; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 445 | ev_file->file = filp; |
| 446 | |
| 447 | /* |
| 448 | * fops_get() can't fail here, because we're coming from a |
| 449 | * system call on a uverbs file, which will already have a |
| 450 | * module reference. |
| 451 | */ |
| 452 | filp->f_op = fops_get(&uverbs_event_fops); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 453 | filp->f_vfsmnt = mntget(uverbs_event_mnt); |
| 454 | filp->f_dentry = dget(uverbs_event_mnt->mnt_root); |
| 455 | filp->f_mapping = filp->f_dentry->d_inode->i_mapping; |
| 456 | filp->f_flags = O_RDONLY; |
| 457 | filp->f_mode = FMODE_READ; |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 458 | filp->private_data = ev_file; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 459 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 460 | return filp; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 461 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 462 | err_fd: |
| 463 | put_unused_fd(*fd); |
| 464 | |
| 465 | err: |
| 466 | kfree(ev_file); |
| 467 | return ERR_PTR(ret); |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Look up a completion event file by FD. If lookup is successful, |
| 472 | * takes a ref to the event file struct that it returns; if |
| 473 | * unsuccessful, returns NULL. |
| 474 | */ |
| 475 | struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd) |
| 476 | { |
| 477 | struct ib_uverbs_event_file *ev_file = NULL; |
| 478 | struct file *filp; |
| 479 | |
| 480 | filp = fget(fd); |
| 481 | if (!filp) |
| 482 | return NULL; |
| 483 | |
| 484 | if (filp->f_op != &uverbs_event_fops) |
| 485 | goto out; |
| 486 | |
| 487 | ev_file = filp->private_data; |
| 488 | if (ev_file->is_async) { |
| 489 | ev_file = NULL; |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | kref_get(&ev_file->ref); |
| 494 | |
| 495 | out: |
| 496 | fput(filp); |
| 497 | return ev_file; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf, |
| 501 | size_t count, loff_t *pos) |
| 502 | { |
| 503 | struct ib_uverbs_file *file = filp->private_data; |
| 504 | struct ib_uverbs_cmd_hdr hdr; |
| 505 | |
| 506 | if (count < sizeof hdr) |
| 507 | return -EINVAL; |
| 508 | |
| 509 | if (copy_from_user(&hdr, buf, sizeof hdr)) |
| 510 | return -EFAULT; |
| 511 | |
| 512 | if (hdr.in_words * 4 != count) |
| 513 | return -EINVAL; |
| 514 | |
Roland Dreier | 63c47c2 | 2005-09-26 13:01:03 -0700 | [diff] [blame] | 515 | if (hdr.command < 0 || |
| 516 | hdr.command >= ARRAY_SIZE(uverbs_cmd_table) || |
| 517 | !uverbs_cmd_table[hdr.command]) |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 518 | return -EINVAL; |
| 519 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 520 | if (!file->ucontext && |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 521 | hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr, |
| 525 | hdr.in_words * 4, hdr.out_words * 4); |
| 526 | } |
| 527 | |
| 528 | static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma) |
| 529 | { |
| 530 | struct ib_uverbs_file *file = filp->private_data; |
| 531 | |
| 532 | if (!file->ucontext) |
| 533 | return -ENODEV; |
| 534 | else |
| 535 | return file->device->ib_dev->mmap(file->ucontext, vma); |
| 536 | } |
| 537 | |
| 538 | static int ib_uverbs_open(struct inode *inode, struct file *filp) |
| 539 | { |
| 540 | struct ib_uverbs_device *dev = |
| 541 | container_of(inode->i_cdev, struct ib_uverbs_device, dev); |
| 542 | struct ib_uverbs_file *file; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 543 | |
| 544 | if (!try_module_get(dev->ib_dev->owner)) |
| 545 | return -ENODEV; |
| 546 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 547 | file = kmalloc(sizeof *file, GFP_KERNEL); |
Roland Dreier | 63c47c2 | 2005-09-26 13:01:03 -0700 | [diff] [blame] | 548 | if (!file) { |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 549 | module_put(dev->ib_dev->owner); |
| 550 | return -ENOMEM; |
Roland Dreier | 63c47c2 | 2005-09-26 13:01:03 -0700 | [diff] [blame] | 551 | } |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 552 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 553 | file->device = dev; |
| 554 | file->ucontext = NULL; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 555 | kref_init(&file->ref); |
Roland Dreier | 63c47c2 | 2005-09-26 13:01:03 -0700 | [diff] [blame] | 556 | init_MUTEX(&file->mutex); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 557 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 558 | filp->private_data = file; |
| 559 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 560 | return 0; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static int ib_uverbs_close(struct inode *inode, struct file *filp) |
| 564 | { |
| 565 | struct ib_uverbs_file *file = filp->private_data; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 566 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 567 | ib_dealloc_ucontext(file->ucontext); |
| 568 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 569 | kref_put(&file->async_file->ref, ib_uverbs_release_event_file); |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 570 | kref_put(&file->ref, ib_uverbs_release_file); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static struct file_operations uverbs_fops = { |
| 576 | .owner = THIS_MODULE, |
| 577 | .write = ib_uverbs_write, |
| 578 | .open = ib_uverbs_open, |
| 579 | .release = ib_uverbs_close |
| 580 | }; |
| 581 | |
| 582 | static struct file_operations uverbs_mmap_fops = { |
| 583 | .owner = THIS_MODULE, |
| 584 | .write = ib_uverbs_write, |
| 585 | .mmap = ib_uverbs_mmap, |
| 586 | .open = ib_uverbs_open, |
| 587 | .release = ib_uverbs_close |
| 588 | }; |
| 589 | |
| 590 | static struct ib_client uverbs_client = { |
| 591 | .name = "uverbs", |
| 592 | .add = ib_uverbs_add_one, |
| 593 | .remove = ib_uverbs_remove_one |
| 594 | }; |
| 595 | |
| 596 | static ssize_t show_ibdev(struct class_device *class_dev, char *buf) |
| 597 | { |
| 598 | struct ib_uverbs_device *dev = |
| 599 | container_of(class_dev, struct ib_uverbs_device, class_dev); |
| 600 | |
| 601 | return sprintf(buf, "%s\n", dev->ib_dev->name); |
| 602 | } |
| 603 | static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
| 604 | |
Roland Dreier | 274c089 | 2005-09-29 14:17:48 -0700 | [diff] [blame] | 605 | static ssize_t show_dev_abi_version(struct class_device *class_dev, char *buf) |
| 606 | { |
| 607 | struct ib_uverbs_device *dev = |
| 608 | container_of(class_dev, struct ib_uverbs_device, class_dev); |
| 609 | |
| 610 | return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver); |
| 611 | } |
| 612 | static CLASS_DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL); |
| 613 | |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 614 | static void ib_uverbs_release_class_dev(struct class_device *class_dev) |
| 615 | { |
| 616 | struct ib_uverbs_device *dev = |
| 617 | container_of(class_dev, struct ib_uverbs_device, class_dev); |
| 618 | |
| 619 | cdev_del(&dev->dev); |
| 620 | clear_bit(dev->devnum, dev_map); |
| 621 | kfree(dev); |
| 622 | } |
| 623 | |
| 624 | static struct class uverbs_class = { |
| 625 | .name = "infiniband_verbs", |
| 626 | .release = ib_uverbs_release_class_dev |
| 627 | }; |
| 628 | |
| 629 | static ssize_t show_abi_version(struct class *class, char *buf) |
| 630 | { |
| 631 | return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION); |
| 632 | } |
| 633 | static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); |
| 634 | |
| 635 | static void ib_uverbs_add_one(struct ib_device *device) |
| 636 | { |
| 637 | struct ib_uverbs_device *uverbs_dev; |
| 638 | |
| 639 | if (!device->alloc_ucontext) |
| 640 | return; |
| 641 | |
| 642 | uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL); |
| 643 | if (!uverbs_dev) |
| 644 | return; |
| 645 | |
| 646 | memset(uverbs_dev, 0, sizeof *uverbs_dev); |
| 647 | |
| 648 | spin_lock(&map_lock); |
| 649 | uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES); |
| 650 | if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) { |
| 651 | spin_unlock(&map_lock); |
| 652 | goto err; |
| 653 | } |
| 654 | set_bit(uverbs_dev->devnum, dev_map); |
| 655 | spin_unlock(&map_lock); |
| 656 | |
Roland Dreier | 6b73597e | 2005-09-26 13:53:25 -0700 | [diff] [blame] | 657 | uverbs_dev->ib_dev = device; |
| 658 | uverbs_dev->num_comp_vectors = 1; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 659 | |
| 660 | if (device->mmap) |
| 661 | cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops); |
| 662 | else |
| 663 | cdev_init(&uverbs_dev->dev, &uverbs_fops); |
| 664 | uverbs_dev->dev.owner = THIS_MODULE; |
| 665 | kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum); |
| 666 | if (cdev_add(&uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1)) |
| 667 | goto err; |
| 668 | |
| 669 | uverbs_dev->class_dev.class = &uverbs_class; |
| 670 | uverbs_dev->class_dev.dev = device->dma_device; |
| 671 | uverbs_dev->class_dev.devt = uverbs_dev->dev.dev; |
| 672 | snprintf(uverbs_dev->class_dev.class_id, BUS_ID_SIZE, "uverbs%d", uverbs_dev->devnum); |
| 673 | if (class_device_register(&uverbs_dev->class_dev)) |
| 674 | goto err_cdev; |
| 675 | |
| 676 | if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_ibdev)) |
| 677 | goto err_class; |
Roland Dreier | 274c089 | 2005-09-29 14:17:48 -0700 | [diff] [blame] | 678 | if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_abi_version)) |
| 679 | goto err_class; |
Roland Dreier | bc38a6a | 2005-07-07 17:57:13 -0700 | [diff] [blame] | 680 | |
| 681 | ib_set_client_data(device, &uverbs_client, uverbs_dev); |
| 682 | |
| 683 | return; |
| 684 | |
| 685 | err_class: |
| 686 | class_device_unregister(&uverbs_dev->class_dev); |
| 687 | |
| 688 | err_cdev: |
| 689 | cdev_del(&uverbs_dev->dev); |
| 690 | clear_bit(uverbs_dev->devnum, dev_map); |
| 691 | |
| 692 | err: |
| 693 | kfree(uverbs_dev); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | static void ib_uverbs_remove_one(struct ib_device *device) |
| 698 | { |
| 699 | struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client); |
| 700 | |
| 701 | if (!uverbs_dev) |
| 702 | return; |
| 703 | |
| 704 | class_device_unregister(&uverbs_dev->class_dev); |
| 705 | } |
| 706 | |
| 707 | static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags, |
| 708 | const char *dev_name, void *data) |
| 709 | { |
| 710 | return get_sb_pseudo(fs_type, "infinibandevent:", NULL, |
| 711 | INFINIBANDEVENTFS_MAGIC); |
| 712 | } |
| 713 | |
| 714 | static struct file_system_type uverbs_event_fs = { |
| 715 | /* No owner field so module can be unloaded */ |
| 716 | .name = "infinibandeventfs", |
| 717 | .get_sb = uverbs_event_get_sb, |
| 718 | .kill_sb = kill_litter_super |
| 719 | }; |
| 720 | |
| 721 | static int __init ib_uverbs_init(void) |
| 722 | { |
| 723 | int ret; |
| 724 | |
| 725 | spin_lock_init(&map_lock); |
| 726 | |
| 727 | ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES, |
| 728 | "infiniband_verbs"); |
| 729 | if (ret) { |
| 730 | printk(KERN_ERR "user_verbs: couldn't register device number\n"); |
| 731 | goto out; |
| 732 | } |
| 733 | |
| 734 | ret = class_register(&uverbs_class); |
| 735 | if (ret) { |
| 736 | printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n"); |
| 737 | goto out_chrdev; |
| 738 | } |
| 739 | |
| 740 | ret = class_create_file(&uverbs_class, &class_attr_abi_version); |
| 741 | if (ret) { |
| 742 | printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n"); |
| 743 | goto out_class; |
| 744 | } |
| 745 | |
| 746 | ret = register_filesystem(&uverbs_event_fs); |
| 747 | if (ret) { |
| 748 | printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n"); |
| 749 | goto out_class; |
| 750 | } |
| 751 | |
| 752 | uverbs_event_mnt = kern_mount(&uverbs_event_fs); |
| 753 | if (IS_ERR(uverbs_event_mnt)) { |
| 754 | ret = PTR_ERR(uverbs_event_mnt); |
| 755 | printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n"); |
| 756 | goto out_fs; |
| 757 | } |
| 758 | |
| 759 | ret = ib_register_client(&uverbs_client); |
| 760 | if (ret) { |
| 761 | printk(KERN_ERR "user_verbs: couldn't register client\n"); |
| 762 | goto out_mnt; |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | |
| 767 | out_mnt: |
| 768 | mntput(uverbs_event_mnt); |
| 769 | |
| 770 | out_fs: |
| 771 | unregister_filesystem(&uverbs_event_fs); |
| 772 | |
| 773 | out_class: |
| 774 | class_unregister(&uverbs_class); |
| 775 | |
| 776 | out_chrdev: |
| 777 | unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES); |
| 778 | |
| 779 | out: |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | static void __exit ib_uverbs_cleanup(void) |
| 784 | { |
| 785 | ib_unregister_client(&uverbs_client); |
| 786 | mntput(uverbs_event_mnt); |
| 787 | unregister_filesystem(&uverbs_event_fs); |
| 788 | class_unregister(&uverbs_class); |
| 789 | unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES); |
| 790 | } |
| 791 | |
| 792 | module_init(ib_uverbs_init); |
| 793 | module_exit(ib_uverbs_cleanup); |