[PATCH] IB: Add copyright notices
[safe/jmp/linux-2.6] / drivers / infiniband / core / uverbs_main.c
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
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);
72
73 static spinlock_t map_lock;
74 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
75
76 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
77                                      const char __user *buf, int in_len,
78                                      int out_len) = {
79         [IB_USER_VERBS_CMD_QUERY_PARAMS]  = ib_uverbs_query_params,
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_QUERY_GID]     = ib_uverbs_query_gid,
84         [IB_USER_VERBS_CMD_QUERY_PKEY]    = ib_uverbs_query_pkey,
85         [IB_USER_VERBS_CMD_ALLOC_PD]      = ib_uverbs_alloc_pd,
86         [IB_USER_VERBS_CMD_DEALLOC_PD]    = ib_uverbs_dealloc_pd,
87         [IB_USER_VERBS_CMD_REG_MR]        = ib_uverbs_reg_mr,
88         [IB_USER_VERBS_CMD_DEREG_MR]      = ib_uverbs_dereg_mr,
89         [IB_USER_VERBS_CMD_CREATE_CQ]     = ib_uverbs_create_cq,
90         [IB_USER_VERBS_CMD_DESTROY_CQ]    = ib_uverbs_destroy_cq,
91         [IB_USER_VERBS_CMD_CREATE_QP]     = ib_uverbs_create_qp,
92         [IB_USER_VERBS_CMD_MODIFY_QP]     = ib_uverbs_modify_qp,
93         [IB_USER_VERBS_CMD_DESTROY_QP]    = ib_uverbs_destroy_qp,
94         [IB_USER_VERBS_CMD_ATTACH_MCAST]  = ib_uverbs_attach_mcast,
95         [IB_USER_VERBS_CMD_DETACH_MCAST]  = ib_uverbs_detach_mcast,
96 };
97
98 static struct vfsmount *uverbs_event_mnt;
99
100 static void ib_uverbs_add_one(struct ib_device *device);
101 static void ib_uverbs_remove_one(struct ib_device *device);
102
103 static int ib_dealloc_ucontext(struct ib_ucontext *context)
104 {
105         struct ib_uobject *uobj, *tmp;
106
107         if (!context)
108                 return 0;
109
110         down(&ib_uverbs_idr_mutex);
111
112         /* XXX Free AHs */
113
114         list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
115                 struct ib_qp *qp = idr_find(&ib_uverbs_qp_idr, uobj->id);
116                 idr_remove(&ib_uverbs_qp_idr, uobj->id);
117                 ib_destroy_qp(qp);
118                 list_del(&uobj->list);
119                 kfree(uobj);
120         }
121
122         list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
123                 struct ib_cq *cq = idr_find(&ib_uverbs_cq_idr, uobj->id);
124                 idr_remove(&ib_uverbs_cq_idr, uobj->id);
125                 ib_destroy_cq(cq);
126                 list_del(&uobj->list);
127                 kfree(uobj);
128         }
129
130         /* XXX Free SRQs */
131         /* XXX Free MWs */
132
133         list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
134                 struct ib_mr *mr = idr_find(&ib_uverbs_mr_idr, uobj->id);
135                 struct ib_device *mrdev = mr->device;
136                 struct ib_umem_object *memobj;
137
138                 idr_remove(&ib_uverbs_mr_idr, uobj->id);
139                 ib_dereg_mr(mr);
140
141                 memobj = container_of(uobj, struct ib_umem_object, uobject);
142                 ib_umem_release_on_close(mrdev, &memobj->umem);
143
144                 list_del(&uobj->list);
145                 kfree(memobj);
146         }
147
148         list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
149                 struct ib_pd *pd = idr_find(&ib_uverbs_pd_idr, uobj->id);
150                 idr_remove(&ib_uverbs_pd_idr, uobj->id);
151                 ib_dealloc_pd(pd);
152                 list_del(&uobj->list);
153                 kfree(uobj);
154         }
155
156         up(&ib_uverbs_idr_mutex);
157
158         return context->device->dealloc_ucontext(context);
159 }
160
161 static void ib_uverbs_release_file(struct kref *ref)
162 {
163         struct ib_uverbs_file *file =
164                 container_of(ref, struct ib_uverbs_file, ref);
165
166         module_put(file->device->ib_dev->owner);
167         kfree(file);
168 }
169
170 static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
171                                     size_t count, loff_t *pos)
172 {
173         struct ib_uverbs_event_file *file = filp->private_data;
174         void *event;
175         int eventsz;
176         int ret = 0;
177
178         spin_lock_irq(&file->lock);
179
180         while (list_empty(&file->event_list) && file->fd >= 0) {
181                 spin_unlock_irq(&file->lock);
182
183                 if (filp->f_flags & O_NONBLOCK)
184                         return -EAGAIN;
185
186                 if (wait_event_interruptible(file->poll_wait,
187                                              !list_empty(&file->event_list) ||
188                                              file->fd < 0))
189                         return -ERESTARTSYS;
190
191                 spin_lock_irq(&file->lock);
192         }
193
194         if (file->fd < 0) {
195                 spin_unlock_irq(&file->lock);
196                 return -ENODEV;
197         }
198
199         if (file->is_async) {
200                 event   = list_entry(file->event_list.next,
201                                      struct ib_uverbs_async_event, list);
202                 eventsz = sizeof (struct ib_uverbs_async_event_desc);
203         } else {
204                 event   = list_entry(file->event_list.next,
205                                      struct ib_uverbs_comp_event, list);
206                 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
207         }
208
209         if (eventsz > count) {
210                 ret   = -EINVAL;
211                 event = NULL;
212         } else
213                 list_del(file->event_list.next);
214
215         spin_unlock_irq(&file->lock);
216
217         if (event) {
218                 if (copy_to_user(buf, event, eventsz))
219                         ret = -EFAULT;
220                 else
221                         ret = eventsz;
222         }
223
224         kfree(event);
225
226         return ret;
227 }
228
229 static unsigned int ib_uverbs_event_poll(struct file *filp,
230                                          struct poll_table_struct *wait)
231 {
232         unsigned int pollflags = 0;
233         struct ib_uverbs_event_file *file = filp->private_data;
234
235         poll_wait(filp, &file->poll_wait, wait);
236
237         spin_lock_irq(&file->lock);
238         if (file->fd < 0)
239                 pollflags = POLLERR;
240         else if (!list_empty(&file->event_list))
241                 pollflags = POLLIN | POLLRDNORM;
242         spin_unlock_irq(&file->lock);
243
244         return pollflags;
245 }
246
247 static void ib_uverbs_event_release(struct ib_uverbs_event_file *file)
248 {
249         struct list_head *entry, *tmp;
250
251         spin_lock_irq(&file->lock);
252         if (file->fd != -1) {
253                 file->fd = -1;
254                 list_for_each_safe(entry, tmp, &file->event_list)
255                         if (file->is_async)
256                                 kfree(list_entry(entry, struct ib_uverbs_async_event, list));
257                         else
258                                 kfree(list_entry(entry, struct ib_uverbs_comp_event, list));
259         }
260         spin_unlock_irq(&file->lock);
261 }
262
263 static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
264 {
265         struct ib_uverbs_event_file *file = filp->private_data;
266
267         return fasync_helper(fd, filp, on, &file->async_queue);
268 }
269
270 static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
271 {
272         struct ib_uverbs_event_file *file = filp->private_data;
273
274         ib_uverbs_event_release(file);
275         ib_uverbs_event_fasync(-1, filp, 0);
276         kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
277
278         return 0;
279 }
280
281 static struct file_operations uverbs_event_fops = {
282         /*
283          * No .owner field since we artificially create event files,
284          * so there is no increment to the module reference count in
285          * the open path.  All event files come from a uverbs command
286          * file, which already takes a module reference, so this is OK.
287          */
288         .read    = ib_uverbs_event_read,
289         .poll    = ib_uverbs_event_poll,
290         .release = ib_uverbs_event_close,
291         .fasync  = ib_uverbs_event_fasync
292 };
293
294 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
295 {
296         struct ib_uverbs_file       *file = cq_context;
297         struct ib_uverbs_comp_event *entry;
298         unsigned long                flags;
299
300         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
301         if (!entry)
302                 return;
303
304         entry->desc.cq_handle = cq->uobject->user_handle;
305
306         spin_lock_irqsave(&file->comp_file[0].lock, flags);
307         list_add_tail(&entry->list, &file->comp_file[0].event_list);
308         spin_unlock_irqrestore(&file->comp_file[0].lock, flags);
309
310         wake_up_interruptible(&file->comp_file[0].poll_wait);
311         kill_fasync(&file->comp_file[0].async_queue, SIGIO, POLL_IN);
312 }
313
314 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
315                                     __u64 element, __u64 event)
316 {
317         struct ib_uverbs_async_event *entry;
318         unsigned long flags;
319
320         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
321         if (!entry)
322                 return;
323
324         entry->desc.element    = element;
325         entry->desc.event_type = event;
326
327         spin_lock_irqsave(&file->async_file.lock, flags);
328         list_add_tail(&entry->list, &file->async_file.event_list);
329         spin_unlock_irqrestore(&file->async_file.lock, flags);
330
331         wake_up_interruptible(&file->async_file.poll_wait);
332         kill_fasync(&file->async_file.async_queue, SIGIO, POLL_IN);
333 }
334
335 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
336 {
337         ib_uverbs_async_handler(context_ptr,
338                                 event->element.cq->uobject->user_handle,
339                                 event->event);
340 }
341
342 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
343 {
344         ib_uverbs_async_handler(context_ptr,
345                                 event->element.qp->uobject->user_handle,
346                                 event->event);
347 }
348
349 static void ib_uverbs_event_handler(struct ib_event_handler *handler,
350                                     struct ib_event *event)
351 {
352         struct ib_uverbs_file *file =
353                 container_of(handler, struct ib_uverbs_file, event_handler);
354
355         ib_uverbs_async_handler(file, event->element.port_num, event->event);
356 }
357
358 static int ib_uverbs_event_init(struct ib_uverbs_event_file *file,
359                                 struct ib_uverbs_file *uverbs_file)
360 {
361         struct file *filp;
362
363         spin_lock_init(&file->lock);
364         INIT_LIST_HEAD(&file->event_list);
365         init_waitqueue_head(&file->poll_wait);
366         file->uverbs_file = uverbs_file;
367         file->async_queue = NULL;
368
369         file->fd = get_unused_fd();
370         if (file->fd < 0)
371                 return file->fd;
372
373         filp = get_empty_filp();
374         if (!filp) {
375                 put_unused_fd(file->fd);
376                 return -ENFILE;
377         }
378
379         filp->f_op         = &uverbs_event_fops;
380         filp->f_vfsmnt     = mntget(uverbs_event_mnt);
381         filp->f_dentry     = dget(uverbs_event_mnt->mnt_root);
382         filp->f_mapping    = filp->f_dentry->d_inode->i_mapping;
383         filp->f_flags      = O_RDONLY;
384         filp->f_mode       = FMODE_READ;
385         filp->private_data = file;
386
387         fd_install(file->fd, filp);
388
389         return 0;
390 }
391
392 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
393                              size_t count, loff_t *pos)
394 {
395         struct ib_uverbs_file *file = filp->private_data;
396         struct ib_uverbs_cmd_hdr hdr;
397
398         if (count < sizeof hdr)
399                 return -EINVAL;
400
401         if (copy_from_user(&hdr, buf, sizeof hdr))
402                 return -EFAULT;
403
404         if (hdr.in_words * 4 != count)
405                 return -EINVAL;
406
407         if (hdr.command < 0 || hdr.command >= ARRAY_SIZE(uverbs_cmd_table))
408                 return -EINVAL;
409
410         if (!file->ucontext                               &&
411             hdr.command != IB_USER_VERBS_CMD_QUERY_PARAMS &&
412             hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
413                 return -EINVAL;
414
415         return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
416                                              hdr.in_words * 4, hdr.out_words * 4);
417 }
418
419 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
420 {
421         struct ib_uverbs_file *file = filp->private_data;
422
423         if (!file->ucontext)
424                 return -ENODEV;
425         else
426                 return file->device->ib_dev->mmap(file->ucontext, vma);
427 }
428
429 static int ib_uverbs_open(struct inode *inode, struct file *filp)
430 {
431         struct ib_uverbs_device *dev =
432                 container_of(inode->i_cdev, struct ib_uverbs_device, dev);
433         struct ib_uverbs_file *file;
434         int i = 0;
435         int ret;
436
437         if (!try_module_get(dev->ib_dev->owner))
438                 return -ENODEV;
439
440         file = kmalloc(sizeof *file +
441                        (dev->num_comp - 1) * sizeof (struct ib_uverbs_event_file),
442                        GFP_KERNEL);
443         if (!file)
444                 return -ENOMEM;
445
446         file->device = dev;
447         kref_init(&file->ref);
448
449         file->ucontext = NULL;
450
451         ret = ib_uverbs_event_init(&file->async_file, file);
452         if (ret)
453                 goto err;
454
455         file->async_file.is_async = 1;
456
457         kref_get(&file->ref);
458
459         for (i = 0; i < dev->num_comp; ++i) {
460                 ret = ib_uverbs_event_init(&file->comp_file[i], file);
461                 if (ret)
462                         goto err_async;
463                 kref_get(&file->ref);
464                 file->comp_file[i].is_async = 0;
465         }
466
467
468         filp->private_data = file;
469
470         INIT_IB_EVENT_HANDLER(&file->event_handler, dev->ib_dev,
471                               ib_uverbs_event_handler);
472         if (ib_register_event_handler(&file->event_handler))
473                 goto err_async;
474
475         return 0;
476
477 err_async:
478         while (i--)
479                 ib_uverbs_event_release(&file->comp_file[i]);
480
481         ib_uverbs_event_release(&file->async_file);
482
483 err:
484         kref_put(&file->ref, ib_uverbs_release_file);
485
486         return ret;
487 }
488
489 static int ib_uverbs_close(struct inode *inode, struct file *filp)
490 {
491         struct ib_uverbs_file *file = filp->private_data;
492         int i;
493
494         ib_unregister_event_handler(&file->event_handler);
495         ib_uverbs_event_release(&file->async_file);
496         ib_dealloc_ucontext(file->ucontext);
497
498         for (i = 0; i < file->device->num_comp; ++i)
499                 ib_uverbs_event_release(&file->comp_file[i]);
500
501         kref_put(&file->ref, ib_uverbs_release_file);
502
503         return 0;
504 }
505
506 static struct file_operations uverbs_fops = {
507         .owner   = THIS_MODULE,
508         .write   = ib_uverbs_write,
509         .open    = ib_uverbs_open,
510         .release = ib_uverbs_close
511 };
512
513 static struct file_operations uverbs_mmap_fops = {
514         .owner   = THIS_MODULE,
515         .write   = ib_uverbs_write,
516         .mmap    = ib_uverbs_mmap,
517         .open    = ib_uverbs_open,
518         .release = ib_uverbs_close
519 };
520
521 static struct ib_client uverbs_client = {
522         .name   = "uverbs",
523         .add    = ib_uverbs_add_one,
524         .remove = ib_uverbs_remove_one
525 };
526
527 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
528 {
529         struct ib_uverbs_device *dev =
530                 container_of(class_dev, struct ib_uverbs_device, class_dev);
531
532         return sprintf(buf, "%s\n", dev->ib_dev->name);
533 }
534 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
535
536 static void ib_uverbs_release_class_dev(struct class_device *class_dev)
537 {
538         struct ib_uverbs_device *dev =
539                 container_of(class_dev, struct ib_uverbs_device, class_dev);
540
541         cdev_del(&dev->dev);
542         clear_bit(dev->devnum, dev_map);
543         kfree(dev);
544 }
545
546 static struct class uverbs_class = {
547         .name    = "infiniband_verbs",
548         .release = ib_uverbs_release_class_dev
549 };
550
551 static ssize_t show_abi_version(struct class *class, char *buf)
552 {
553         return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
554 }
555 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
556
557 static void ib_uverbs_add_one(struct ib_device *device)
558 {
559         struct ib_uverbs_device *uverbs_dev;
560
561         if (!device->alloc_ucontext)
562                 return;
563
564         uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL);
565         if (!uverbs_dev)
566                 return;
567
568         memset(uverbs_dev, 0, sizeof *uverbs_dev);
569
570         spin_lock(&map_lock);
571         uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
572         if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
573                 spin_unlock(&map_lock);
574                 goto err;
575         }
576         set_bit(uverbs_dev->devnum, dev_map);
577         spin_unlock(&map_lock);
578
579         uverbs_dev->ib_dev   = device;
580         uverbs_dev->num_comp = 1;
581
582         if (device->mmap)
583                 cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops);
584         else
585                 cdev_init(&uverbs_dev->dev, &uverbs_fops);
586         uverbs_dev->dev.owner = THIS_MODULE;
587         kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum);
588         if (cdev_add(&uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
589                 goto err;
590
591         uverbs_dev->class_dev.class = &uverbs_class;
592         uverbs_dev->class_dev.dev   = device->dma_device;
593         uverbs_dev->class_dev.devt  = uverbs_dev->dev.dev;
594         snprintf(uverbs_dev->class_dev.class_id, BUS_ID_SIZE, "uverbs%d", uverbs_dev->devnum);
595         if (class_device_register(&uverbs_dev->class_dev))
596                 goto err_cdev;
597
598         if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_ibdev))
599                 goto err_class;
600
601         ib_set_client_data(device, &uverbs_client, uverbs_dev);
602
603         return;
604
605 err_class:
606         class_device_unregister(&uverbs_dev->class_dev);
607
608 err_cdev:
609         cdev_del(&uverbs_dev->dev);
610         clear_bit(uverbs_dev->devnum, dev_map);
611
612 err:
613         kfree(uverbs_dev);
614         return;
615 }
616
617 static void ib_uverbs_remove_one(struct ib_device *device)
618 {
619         struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
620
621         if (!uverbs_dev)
622                 return;
623
624         class_device_unregister(&uverbs_dev->class_dev);
625 }
626
627 static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
628                                                const char *dev_name, void *data)
629 {
630         return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
631                              INFINIBANDEVENTFS_MAGIC);
632 }
633
634 static struct file_system_type uverbs_event_fs = {
635         /* No owner field so module can be unloaded */
636         .name    = "infinibandeventfs",
637         .get_sb  = uverbs_event_get_sb,
638         .kill_sb = kill_litter_super
639 };
640
641 static int __init ib_uverbs_init(void)
642 {
643         int ret;
644
645         spin_lock_init(&map_lock);
646
647         ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
648                                      "infiniband_verbs");
649         if (ret) {
650                 printk(KERN_ERR "user_verbs: couldn't register device number\n");
651                 goto out;
652         }
653
654         ret = class_register(&uverbs_class);
655         if (ret) {
656                 printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
657                 goto out_chrdev;
658         }
659
660         ret = class_create_file(&uverbs_class, &class_attr_abi_version);
661         if (ret) {
662                 printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
663                 goto out_class;
664         }
665
666         ret = register_filesystem(&uverbs_event_fs);
667         if (ret) {
668                 printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
669                 goto out_class;
670         }
671
672         uverbs_event_mnt = kern_mount(&uverbs_event_fs);
673         if (IS_ERR(uverbs_event_mnt)) {
674                 ret = PTR_ERR(uverbs_event_mnt);
675                 printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
676                 goto out_fs;
677         }
678
679         ret = ib_register_client(&uverbs_client);
680         if (ret) {
681                 printk(KERN_ERR "user_verbs: couldn't register client\n");
682                 goto out_mnt;
683         }
684
685         return 0;
686
687 out_mnt:
688         mntput(uverbs_event_mnt);
689
690 out_fs:
691         unregister_filesystem(&uverbs_event_fs);
692
693 out_class:
694         class_unregister(&uverbs_class);
695
696 out_chrdev:
697         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
698
699 out:
700         return ret;
701 }
702
703 static void __exit ib_uverbs_cleanup(void)
704 {
705         ib_unregister_client(&uverbs_client);
706         mntput(uverbs_event_mnt);
707         unregister_filesystem(&uverbs_event_fs);
708         class_unregister(&uverbs_class);
709         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
710 }
711
712 module_init(ib_uverbs_init);
713 module_exit(ib_uverbs_cleanup);