[IB] uverbs: ABI-breaking fixes for userspace verbs
[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 DEFINE_IDR(ib_uverbs_srq_idr);
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) = {
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,
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);
121                 kfree(container_of(uobj, struct ib_uevent_object, uobject));
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);
129                 kfree(container_of(uobj, struct ib_ucq_object, uobject));
130         }
131
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);
137                 kfree(container_of(uobj, struct ib_uevent_object, uobject));
138         }
139
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);
144                 struct ib_device *mrdev = mr->device;
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);
151                 ib_umem_release_on_close(mrdev, &memobj->umem);
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;
183         struct ib_uverbs_event *event;
184         int eventsz;
185         int ret = 0;
186
187         spin_lock_irq(&file->lock);
188
189         while (list_empty(&file->event_list)) {
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,
196                                              !list_empty(&file->event_list)))
197                         return -ERESTARTSYS;
198
199                 spin_lock_irq(&file->lock);
200         }
201
202         event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
203
204         if (file->is_async)
205                 eventsz = sizeof (struct ib_uverbs_async_event_desc);
206         else
207                 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
208
209         if (eventsz > count) {
210                 ret   = -EINVAL;
211                 event = NULL;
212         } else {
213                 list_del(file->event_list.next);
214                 if (event->counter) {
215                         ++(*event->counter);
216                         list_del(&event->obj_list);
217                 }
218         }
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);
243         if (!list_empty(&file->event_list))
244                 pollflags = POLLIN | POLLRDNORM;
245         spin_unlock_irq(&file->lock);
246
247         return pollflags;
248 }
249
250 void ib_uverbs_release_event_file(struct kref *ref)
251 {
252         struct ib_uverbs_event_file *file =
253                 container_of(ref, struct ib_uverbs_event_file, ref);
254
255         kfree(file);
256 }
257
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
265 static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
266 {
267         struct ib_uverbs_event_file *file = filp->private_data;
268         struct ib_uverbs_event *entry, *tmp;
269
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
279         ib_uverbs_event_fasync(-1, filp, 0);
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);
286
287         return 0;
288 }
289
290 static struct file_operations uverbs_event_fops = {
291         .owner   = THIS_MODULE,
292         .read    = ib_uverbs_event_read,
293         .poll    = ib_uverbs_event_poll,
294         .release = ib_uverbs_event_close,
295         .fasync  = ib_uverbs_event_fasync
296 };
297
298 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
299 {
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         }
313
314         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
315         if (!entry)
316                 return;
317
318         uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
319
320         entry->desc.comp.cq_handle = cq->uobject->user_handle;
321         entry->counter             = &uobj->comp_events_reported;
322
323         list_add_tail(&entry->list, &file->event_list);
324         list_add_tail(&entry->obj_list, &uobj->comp_list);
325         spin_unlock_irqrestore(&file->lock, flags);
326
327         wake_up_interruptible(&file->poll_wait);
328         kill_fasync(&file->async_queue, SIGIO, POLL_IN);
329 }
330
331 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
332                                     __u64 element, __u64 event,
333                                     struct list_head *obj_list,
334                                     u32 *counter)
335 {
336         struct ib_uverbs_event *entry;
337         unsigned long flags;
338
339         spin_lock_irqsave(&file->async_file->lock, flags);
340         if (!file->async_file->file) {
341                 spin_unlock_irqrestore(&file->async_file->lock, flags);
342                 return;
343         }
344
345         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
346         if (!entry)
347                 return;
348
349         entry->desc.async.element    = element;
350         entry->desc.async.event_type = event;
351         entry->counter               = counter;
352
353         list_add_tail(&entry->list, &file->async_file->event_list);
354         if (obj_list)
355                 list_add_tail(&entry->obj_list, obj_list);
356         spin_unlock_irqrestore(&file->async_file->lock, flags);
357
358         wake_up_interruptible(&file->async_file->poll_wait);
359         kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
360 }
361
362 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
363 {
364         struct ib_uverbs_event_file *ev_file = context_ptr;
365         struct ib_ucq_object *uobj;
366
367         uobj = container_of(event->element.cq->uobject,
368                             struct ib_ucq_object, uobject);
369
370         ib_uverbs_async_handler(ev_file->uverbs_file, uobj->uobject.user_handle,
371                                 event->event, &uobj->async_list,
372                                 &uobj->async_events_reported);
373                                 
374 }
375
376 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
377 {
378         struct ib_uevent_object *uobj;
379
380         uobj = container_of(event->element.qp->uobject,
381                             struct ib_uevent_object, uobject);
382
383         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
384                                 event->event, &uobj->event_list,
385                                 &uobj->events_reported);
386 }
387
388 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
389 {
390         struct ib_uevent_object *uobj;
391
392         uobj = container_of(event->element.srq->uobject,
393                             struct ib_uevent_object, uobject);
394
395         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
396                                 event->event, &uobj->event_list,
397                                 &uobj->events_reported);
398 }
399
400 void ib_uverbs_event_handler(struct ib_event_handler *handler,
401                              struct ib_event *event)
402 {
403         struct ib_uverbs_file *file =
404                 container_of(handler, struct ib_uverbs_file, event_handler);
405
406         ib_uverbs_async_handler(file, event->element.port_num, event->event,
407                                 NULL, NULL);
408 }
409
410 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
411                                         int is_async, int *fd)
412 {
413         struct ib_uverbs_event_file *ev_file;
414         struct file *filp;
415         int ret;
416
417         ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
418         if (!ev_file)
419                 return ERR_PTR(-ENOMEM);
420
421         kref_init(&ev_file->ref);
422         spin_lock_init(&ev_file->lock);
423         INIT_LIST_HEAD(&ev_file->event_list);
424         init_waitqueue_head(&ev_file->poll_wait);
425         ev_file->uverbs_file = uverbs_file;
426         ev_file->async_queue = NULL;
427         ev_file->is_async    = is_async;
428
429         *fd = get_unused_fd();
430         if (*fd < 0) {
431                 ret = *fd;
432                 goto err;
433         }
434
435         filp = get_empty_filp();
436         if (!filp) {
437                 ret = -ENFILE;
438                 goto err_fd;
439         }
440
441         ev_file->file      = filp;
442
443         /*
444          * fops_get() can't fail here, because we're coming from a
445          * system call on a uverbs file, which will already have a
446          * module reference.
447          */
448         filp->f_op         = fops_get(&uverbs_event_fops);
449         filp->f_vfsmnt     = mntget(uverbs_event_mnt);
450         filp->f_dentry     = dget(uverbs_event_mnt->mnt_root);
451         filp->f_mapping    = filp->f_dentry->d_inode->i_mapping;
452         filp->f_flags      = O_RDONLY;
453         filp->f_mode       = FMODE_READ;
454         filp->private_data = ev_file;
455
456         return filp;
457
458 err_fd:
459         put_unused_fd(*fd);
460
461 err:
462         kfree(ev_file);
463         return ERR_PTR(ret);
464 }
465
466 /*
467  * Look up a completion event file by FD.  If lookup is successful,
468  * takes a ref to the event file struct that it returns; if
469  * unsuccessful, returns NULL.
470  */
471 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
472 {
473         struct ib_uverbs_event_file *ev_file = NULL;
474         struct file *filp;
475
476         filp = fget(fd);
477         if (!filp)
478                 return NULL;
479
480         if (filp->f_op != &uverbs_event_fops)
481                 goto out;
482
483         ev_file = filp->private_data;
484         if (ev_file->is_async) {
485                 ev_file = NULL;
486                 goto out;
487         }
488
489         kref_get(&ev_file->ref);
490
491 out:
492         fput(filp);
493         return ev_file;
494 }
495
496 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
497                              size_t count, loff_t *pos)
498 {
499         struct ib_uverbs_file *file = filp->private_data;
500         struct ib_uverbs_cmd_hdr hdr;
501
502         if (count < sizeof hdr)
503                 return -EINVAL;
504
505         if (copy_from_user(&hdr, buf, sizeof hdr))
506                 return -EFAULT;
507
508         if (hdr.in_words * 4 != count)
509                 return -EINVAL;
510
511         if (hdr.command < 0                             ||
512             hdr.command >= ARRAY_SIZE(uverbs_cmd_table) ||
513             !uverbs_cmd_table[hdr.command])
514                 return -EINVAL;
515
516         if (!file->ucontext &&
517             hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
518                 return -EINVAL;
519
520         return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
521                                              hdr.in_words * 4, hdr.out_words * 4);
522 }
523
524 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
525 {
526         struct ib_uverbs_file *file = filp->private_data;
527
528         if (!file->ucontext)
529                 return -ENODEV;
530         else
531                 return file->device->ib_dev->mmap(file->ucontext, vma);
532 }
533
534 static int ib_uverbs_open(struct inode *inode, struct file *filp)
535 {
536         struct ib_uverbs_device *dev =
537                 container_of(inode->i_cdev, struct ib_uverbs_device, dev);
538         struct ib_uverbs_file *file;
539
540         if (!try_module_get(dev->ib_dev->owner))
541                 return -ENODEV;
542
543         file = kmalloc(sizeof *file, GFP_KERNEL);
544         if (!file) {
545                 module_put(dev->ib_dev->owner);
546                 return -ENOMEM;
547         }
548
549         file->device   = dev;
550         file->ucontext = NULL;
551         kref_init(&file->ref);
552         init_MUTEX(&file->mutex);
553
554         filp->private_data = file;
555
556         return 0;
557 }
558
559 static int ib_uverbs_close(struct inode *inode, struct file *filp)
560 {
561         struct ib_uverbs_file *file = filp->private_data;
562
563         ib_dealloc_ucontext(file->ucontext);
564
565         kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
566         kref_put(&file->ref, ib_uverbs_release_file);
567
568         return 0;
569 }
570
571 static struct file_operations uverbs_fops = {
572         .owner   = THIS_MODULE,
573         .write   = ib_uverbs_write,
574         .open    = ib_uverbs_open,
575         .release = ib_uverbs_close
576 };
577
578 static struct file_operations uverbs_mmap_fops = {
579         .owner   = THIS_MODULE,
580         .write   = ib_uverbs_write,
581         .mmap    = ib_uverbs_mmap,
582         .open    = ib_uverbs_open,
583         .release = ib_uverbs_close
584 };
585
586 static struct ib_client uverbs_client = {
587         .name   = "uverbs",
588         .add    = ib_uverbs_add_one,
589         .remove = ib_uverbs_remove_one
590 };
591
592 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
593 {
594         struct ib_uverbs_device *dev =
595                 container_of(class_dev, struct ib_uverbs_device, class_dev);
596
597         return sprintf(buf, "%s\n", dev->ib_dev->name);
598 }
599 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
600
601 static void ib_uverbs_release_class_dev(struct class_device *class_dev)
602 {
603         struct ib_uverbs_device *dev =
604                 container_of(class_dev, struct ib_uverbs_device, class_dev);
605
606         cdev_del(&dev->dev);
607         clear_bit(dev->devnum, dev_map);
608         kfree(dev);
609 }
610
611 static struct class uverbs_class = {
612         .name    = "infiniband_verbs",
613         .release = ib_uverbs_release_class_dev
614 };
615
616 static ssize_t show_abi_version(struct class *class, char *buf)
617 {
618         return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
619 }
620 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
621
622 static void ib_uverbs_add_one(struct ib_device *device)
623 {
624         struct ib_uverbs_device *uverbs_dev;
625
626         if (!device->alloc_ucontext)
627                 return;
628
629         uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL);
630         if (!uverbs_dev)
631                 return;
632
633         memset(uverbs_dev, 0, sizeof *uverbs_dev);
634
635         spin_lock(&map_lock);
636         uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
637         if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
638                 spin_unlock(&map_lock);
639                 goto err;
640         }
641         set_bit(uverbs_dev->devnum, dev_map);
642         spin_unlock(&map_lock);
643
644         uverbs_dev->ib_dev           = device;
645         uverbs_dev->num_comp_vectors = 1;
646
647         if (device->mmap)
648                 cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops);
649         else
650                 cdev_init(&uverbs_dev->dev, &uverbs_fops);
651         uverbs_dev->dev.owner = THIS_MODULE;
652         kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum);
653         if (cdev_add(&uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
654                 goto err;
655
656         uverbs_dev->class_dev.class = &uverbs_class;
657         uverbs_dev->class_dev.dev   = device->dma_device;
658         uverbs_dev->class_dev.devt  = uverbs_dev->dev.dev;
659         snprintf(uverbs_dev->class_dev.class_id, BUS_ID_SIZE, "uverbs%d", uverbs_dev->devnum);
660         if (class_device_register(&uverbs_dev->class_dev))
661                 goto err_cdev;
662
663         if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_ibdev))
664                 goto err_class;
665
666         ib_set_client_data(device, &uverbs_client, uverbs_dev);
667
668         return;
669
670 err_class:
671         class_device_unregister(&uverbs_dev->class_dev);
672
673 err_cdev:
674         cdev_del(&uverbs_dev->dev);
675         clear_bit(uverbs_dev->devnum, dev_map);
676
677 err:
678         kfree(uverbs_dev);
679         return;
680 }
681
682 static void ib_uverbs_remove_one(struct ib_device *device)
683 {
684         struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
685
686         if (!uverbs_dev)
687                 return;
688
689         class_device_unregister(&uverbs_dev->class_dev);
690 }
691
692 static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
693                                                const char *dev_name, void *data)
694 {
695         return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
696                              INFINIBANDEVENTFS_MAGIC);
697 }
698
699 static struct file_system_type uverbs_event_fs = {
700         /* No owner field so module can be unloaded */
701         .name    = "infinibandeventfs",
702         .get_sb  = uverbs_event_get_sb,
703         .kill_sb = kill_litter_super
704 };
705
706 static int __init ib_uverbs_init(void)
707 {
708         int ret;
709
710         spin_lock_init(&map_lock);
711
712         ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
713                                      "infiniband_verbs");
714         if (ret) {
715                 printk(KERN_ERR "user_verbs: couldn't register device number\n");
716                 goto out;
717         }
718
719         ret = class_register(&uverbs_class);
720         if (ret) {
721                 printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
722                 goto out_chrdev;
723         }
724
725         ret = class_create_file(&uverbs_class, &class_attr_abi_version);
726         if (ret) {
727                 printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
728                 goto out_class;
729         }
730
731         ret = register_filesystem(&uverbs_event_fs);
732         if (ret) {
733                 printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
734                 goto out_class;
735         }
736
737         uverbs_event_mnt = kern_mount(&uverbs_event_fs);
738         if (IS_ERR(uverbs_event_mnt)) {
739                 ret = PTR_ERR(uverbs_event_mnt);
740                 printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
741                 goto out_fs;
742         }
743
744         ret = ib_register_client(&uverbs_client);
745         if (ret) {
746                 printk(KERN_ERR "user_verbs: couldn't register client\n");
747                 goto out_mnt;
748         }
749
750         return 0;
751
752 out_mnt:
753         mntput(uverbs_event_mnt);
754
755 out_fs:
756         unregister_filesystem(&uverbs_event_fs);
757
758 out_class:
759         class_unregister(&uverbs_class);
760
761 out_chrdev:
762         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
763
764 out:
765         return ret;
766 }
767
768 static void __exit ib_uverbs_cleanup(void)
769 {
770         ib_unregister_client(&uverbs_client);
771         mntput(uverbs_event_mnt);
772         unregister_filesystem(&uverbs_event_fs);
773         class_unregister(&uverbs_class);
774         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
775 }
776
777 module_init(ib_uverbs_init);
778 module_exit(ib_uverbs_cleanup);