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