RDMA/uverbs: Export XRC domains to user space
[linux-3.10.git] / drivers / infiniband / core / uverbs_main.c
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
6  * Copyright (c) 2005 PathScale, Inc. All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/device.h>
40 #include <linux/err.h>
41 #include <linux/fs.h>
42 #include <linux/poll.h>
43 #include <linux/sched.h>
44 #include <linux/file.h>
45 #include <linux/cdev.h>
46 #include <linux/anon_inodes.h>
47 #include <linux/slab.h>
48
49 #include <asm/uaccess.h>
50
51 #include "uverbs.h"
52
53 MODULE_AUTHOR("Roland Dreier");
54 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
55 MODULE_LICENSE("Dual BSD/GPL");
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 static struct class *uverbs_class;
66
67 DEFINE_SPINLOCK(ib_uverbs_idr_lock);
68 DEFINE_IDR(ib_uverbs_pd_idr);
69 DEFINE_IDR(ib_uverbs_mr_idr);
70 DEFINE_IDR(ib_uverbs_mw_idr);
71 DEFINE_IDR(ib_uverbs_ah_idr);
72 DEFINE_IDR(ib_uverbs_cq_idr);
73 DEFINE_IDR(ib_uverbs_qp_idr);
74 DEFINE_IDR(ib_uverbs_srq_idr);
75 DEFINE_IDR(ib_uverbs_xrcd_idr);
76
77 static DEFINE_SPINLOCK(map_lock);
78 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
79
80 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
81                                      const char __user *buf, int in_len,
82                                      int out_len) = {
83         [IB_USER_VERBS_CMD_GET_CONTEXT]         = ib_uverbs_get_context,
84         [IB_USER_VERBS_CMD_QUERY_DEVICE]        = ib_uverbs_query_device,
85         [IB_USER_VERBS_CMD_QUERY_PORT]          = ib_uverbs_query_port,
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_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
91         [IB_USER_VERBS_CMD_CREATE_CQ]           = ib_uverbs_create_cq,
92         [IB_USER_VERBS_CMD_RESIZE_CQ]           = ib_uverbs_resize_cq,
93         [IB_USER_VERBS_CMD_POLL_CQ]             = ib_uverbs_poll_cq,
94         [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ]       = ib_uverbs_req_notify_cq,
95         [IB_USER_VERBS_CMD_DESTROY_CQ]          = ib_uverbs_destroy_cq,
96         [IB_USER_VERBS_CMD_CREATE_QP]           = ib_uverbs_create_qp,
97         [IB_USER_VERBS_CMD_QUERY_QP]            = ib_uverbs_query_qp,
98         [IB_USER_VERBS_CMD_MODIFY_QP]           = ib_uverbs_modify_qp,
99         [IB_USER_VERBS_CMD_DESTROY_QP]          = ib_uverbs_destroy_qp,
100         [IB_USER_VERBS_CMD_POST_SEND]           = ib_uverbs_post_send,
101         [IB_USER_VERBS_CMD_POST_RECV]           = ib_uverbs_post_recv,
102         [IB_USER_VERBS_CMD_POST_SRQ_RECV]       = ib_uverbs_post_srq_recv,
103         [IB_USER_VERBS_CMD_CREATE_AH]           = ib_uverbs_create_ah,
104         [IB_USER_VERBS_CMD_DESTROY_AH]          = ib_uverbs_destroy_ah,
105         [IB_USER_VERBS_CMD_ATTACH_MCAST]        = ib_uverbs_attach_mcast,
106         [IB_USER_VERBS_CMD_DETACH_MCAST]        = ib_uverbs_detach_mcast,
107         [IB_USER_VERBS_CMD_CREATE_SRQ]          = ib_uverbs_create_srq,
108         [IB_USER_VERBS_CMD_MODIFY_SRQ]          = ib_uverbs_modify_srq,
109         [IB_USER_VERBS_CMD_QUERY_SRQ]           = ib_uverbs_query_srq,
110         [IB_USER_VERBS_CMD_DESTROY_SRQ]         = ib_uverbs_destroy_srq,
111         [IB_USER_VERBS_CMD_OPEN_XRCD]           = ib_uverbs_open_xrcd,
112         [IB_USER_VERBS_CMD_CLOSE_XRCD]          = ib_uverbs_close_xrcd,
113 };
114
115 static void ib_uverbs_add_one(struct ib_device *device);
116 static void ib_uverbs_remove_one(struct ib_device *device);
117
118 static void ib_uverbs_release_dev(struct kref *ref)
119 {
120         struct ib_uverbs_device *dev =
121                 container_of(ref, struct ib_uverbs_device, ref);
122
123         complete(&dev->comp);
124 }
125
126 static void ib_uverbs_release_event_file(struct kref *ref)
127 {
128         struct ib_uverbs_event_file *file =
129                 container_of(ref, struct ib_uverbs_event_file, ref);
130
131         kfree(file);
132 }
133
134 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
135                           struct ib_uverbs_event_file *ev_file,
136                           struct ib_ucq_object *uobj)
137 {
138         struct ib_uverbs_event *evt, *tmp;
139
140         if (ev_file) {
141                 spin_lock_irq(&ev_file->lock);
142                 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
143                         list_del(&evt->list);
144                         kfree(evt);
145                 }
146                 spin_unlock_irq(&ev_file->lock);
147
148                 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
149         }
150
151         spin_lock_irq(&file->async_file->lock);
152         list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
153                 list_del(&evt->list);
154                 kfree(evt);
155         }
156         spin_unlock_irq(&file->async_file->lock);
157 }
158
159 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
160                               struct ib_uevent_object *uobj)
161 {
162         struct ib_uverbs_event *evt, *tmp;
163
164         spin_lock_irq(&file->async_file->lock);
165         list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
166                 list_del(&evt->list);
167                 kfree(evt);
168         }
169         spin_unlock_irq(&file->async_file->lock);
170 }
171
172 static void ib_uverbs_detach_umcast(struct ib_qp *qp,
173                                     struct ib_uqp_object *uobj)
174 {
175         struct ib_uverbs_mcast_entry *mcast, *tmp;
176
177         list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
178                 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
179                 list_del(&mcast->list);
180                 kfree(mcast);
181         }
182 }
183
184 static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
185                                       struct ib_ucontext *context)
186 {
187         struct ib_uobject *uobj, *tmp;
188
189         if (!context)
190                 return 0;
191
192         context->closing = 1;
193
194         list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
195                 struct ib_ah *ah = uobj->object;
196
197                 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
198                 ib_destroy_ah(ah);
199                 kfree(uobj);
200         }
201
202         list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
203                 struct ib_qp *qp = uobj->object;
204                 struct ib_uqp_object *uqp =
205                         container_of(uobj, struct ib_uqp_object, uevent.uobject);
206
207                 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
208                 ib_uverbs_detach_umcast(qp, uqp);
209                 ib_destroy_qp(qp);
210                 ib_uverbs_release_uevent(file, &uqp->uevent);
211                 kfree(uqp);
212         }
213
214         list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
215                 struct ib_cq *cq = uobj->object;
216                 struct ib_uverbs_event_file *ev_file = cq->cq_context;
217                 struct ib_ucq_object *ucq =
218                         container_of(uobj, struct ib_ucq_object, uobject);
219
220                 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
221                 ib_destroy_cq(cq);
222                 ib_uverbs_release_ucq(file, ev_file, ucq);
223                 kfree(ucq);
224         }
225
226         list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
227                 struct ib_srq *srq = uobj->object;
228                 struct ib_uevent_object *uevent =
229                         container_of(uobj, struct ib_uevent_object, uobject);
230
231                 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
232                 ib_destroy_srq(srq);
233                 ib_uverbs_release_uevent(file, uevent);
234                 kfree(uevent);
235         }
236
237         /* XXX Free MWs */
238
239         list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
240                 struct ib_mr *mr = uobj->object;
241
242                 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
243                 ib_dereg_mr(mr);
244                 kfree(uobj);
245         }
246
247         mutex_lock(&file->device->xrcd_tree_mutex);
248         list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
249                 struct ib_xrcd *xrcd = uobj->object;
250                 struct ib_uxrcd_object *uxrcd =
251                         container_of(uobj, struct ib_uxrcd_object, uobject);
252
253                 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
254                 ib_uverbs_dealloc_xrcd(file->device, xrcd);
255                 kfree(uxrcd);
256         }
257         mutex_unlock(&file->device->xrcd_tree_mutex);
258
259         list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
260                 struct ib_pd *pd = uobj->object;
261
262                 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
263                 ib_dealloc_pd(pd);
264                 kfree(uobj);
265         }
266
267         return context->device->dealloc_ucontext(context);
268 }
269
270 static void ib_uverbs_release_file(struct kref *ref)
271 {
272         struct ib_uverbs_file *file =
273                 container_of(ref, struct ib_uverbs_file, ref);
274
275         module_put(file->device->ib_dev->owner);
276         kref_put(&file->device->ref, ib_uverbs_release_dev);
277
278         kfree(file);
279 }
280
281 static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
282                                     size_t count, loff_t *pos)
283 {
284         struct ib_uverbs_event_file *file = filp->private_data;
285         struct ib_uverbs_event *event;
286         int eventsz;
287         int ret = 0;
288
289         spin_lock_irq(&file->lock);
290
291         while (list_empty(&file->event_list)) {
292                 spin_unlock_irq(&file->lock);
293
294                 if (filp->f_flags & O_NONBLOCK)
295                         return -EAGAIN;
296
297                 if (wait_event_interruptible(file->poll_wait,
298                                              !list_empty(&file->event_list)))
299                         return -ERESTARTSYS;
300
301                 spin_lock_irq(&file->lock);
302         }
303
304         event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
305
306         if (file->is_async)
307                 eventsz = sizeof (struct ib_uverbs_async_event_desc);
308         else
309                 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
310
311         if (eventsz > count) {
312                 ret   = -EINVAL;
313                 event = NULL;
314         } else {
315                 list_del(file->event_list.next);
316                 if (event->counter) {
317                         ++(*event->counter);
318                         list_del(&event->obj_list);
319                 }
320         }
321
322         spin_unlock_irq(&file->lock);
323
324         if (event) {
325                 if (copy_to_user(buf, event, eventsz))
326                         ret = -EFAULT;
327                 else
328                         ret = eventsz;
329         }
330
331         kfree(event);
332
333         return ret;
334 }
335
336 static unsigned int ib_uverbs_event_poll(struct file *filp,
337                                          struct poll_table_struct *wait)
338 {
339         unsigned int pollflags = 0;
340         struct ib_uverbs_event_file *file = filp->private_data;
341
342         poll_wait(filp, &file->poll_wait, wait);
343
344         spin_lock_irq(&file->lock);
345         if (!list_empty(&file->event_list))
346                 pollflags = POLLIN | POLLRDNORM;
347         spin_unlock_irq(&file->lock);
348
349         return pollflags;
350 }
351
352 static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
353 {
354         struct ib_uverbs_event_file *file = filp->private_data;
355
356         return fasync_helper(fd, filp, on, &file->async_queue);
357 }
358
359 static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
360 {
361         struct ib_uverbs_event_file *file = filp->private_data;
362         struct ib_uverbs_event *entry, *tmp;
363
364         spin_lock_irq(&file->lock);
365         file->is_closed = 1;
366         list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
367                 if (entry->counter)
368                         list_del(&entry->obj_list);
369                 kfree(entry);
370         }
371         spin_unlock_irq(&file->lock);
372
373         if (file->is_async) {
374                 ib_unregister_event_handler(&file->uverbs_file->event_handler);
375                 kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
376         }
377         kref_put(&file->ref, ib_uverbs_release_event_file);
378
379         return 0;
380 }
381
382 static const struct file_operations uverbs_event_fops = {
383         .owner   = THIS_MODULE,
384         .read    = ib_uverbs_event_read,
385         .poll    = ib_uverbs_event_poll,
386         .release = ib_uverbs_event_close,
387         .fasync  = ib_uverbs_event_fasync,
388         .llseek  = no_llseek,
389 };
390
391 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
392 {
393         struct ib_uverbs_event_file    *file = cq_context;
394         struct ib_ucq_object           *uobj;
395         struct ib_uverbs_event         *entry;
396         unsigned long                   flags;
397
398         if (!file)
399                 return;
400
401         spin_lock_irqsave(&file->lock, flags);
402         if (file->is_closed) {
403                 spin_unlock_irqrestore(&file->lock, flags);
404                 return;
405         }
406
407         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
408         if (!entry) {
409                 spin_unlock_irqrestore(&file->lock, flags);
410                 return;
411         }
412
413         uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
414
415         entry->desc.comp.cq_handle = cq->uobject->user_handle;
416         entry->counter             = &uobj->comp_events_reported;
417
418         list_add_tail(&entry->list, &file->event_list);
419         list_add_tail(&entry->obj_list, &uobj->comp_list);
420         spin_unlock_irqrestore(&file->lock, flags);
421
422         wake_up_interruptible(&file->poll_wait);
423         kill_fasync(&file->async_queue, SIGIO, POLL_IN);
424 }
425
426 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
427                                     __u64 element, __u64 event,
428                                     struct list_head *obj_list,
429                                     u32 *counter)
430 {
431         struct ib_uverbs_event *entry;
432         unsigned long flags;
433
434         spin_lock_irqsave(&file->async_file->lock, flags);
435         if (file->async_file->is_closed) {
436                 spin_unlock_irqrestore(&file->async_file->lock, flags);
437                 return;
438         }
439
440         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
441         if (!entry) {
442                 spin_unlock_irqrestore(&file->async_file->lock, flags);
443                 return;
444         }
445
446         entry->desc.async.element    = element;
447         entry->desc.async.event_type = event;
448         entry->counter               = counter;
449
450         list_add_tail(&entry->list, &file->async_file->event_list);
451         if (obj_list)
452                 list_add_tail(&entry->obj_list, obj_list);
453         spin_unlock_irqrestore(&file->async_file->lock, flags);
454
455         wake_up_interruptible(&file->async_file->poll_wait);
456         kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
457 }
458
459 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
460 {
461         struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
462                                                   struct ib_ucq_object, uobject);
463
464         ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
465                                 event->event, &uobj->async_list,
466                                 &uobj->async_events_reported);
467 }
468
469 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
470 {
471         struct ib_uevent_object *uobj;
472
473         uobj = container_of(event->element.qp->uobject,
474                             struct ib_uevent_object, uobject);
475
476         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
477                                 event->event, &uobj->event_list,
478                                 &uobj->events_reported);
479 }
480
481 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
482 {
483         struct ib_uevent_object *uobj;
484
485         uobj = container_of(event->element.srq->uobject,
486                             struct ib_uevent_object, uobject);
487
488         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
489                                 event->event, &uobj->event_list,
490                                 &uobj->events_reported);
491 }
492
493 void ib_uverbs_event_handler(struct ib_event_handler *handler,
494                              struct ib_event *event)
495 {
496         struct ib_uverbs_file *file =
497                 container_of(handler, struct ib_uverbs_file, event_handler);
498
499         ib_uverbs_async_handler(file, event->element.port_num, event->event,
500                                 NULL, NULL);
501 }
502
503 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
504                                         int is_async)
505 {
506         struct ib_uverbs_event_file *ev_file;
507         struct file *filp;
508
509         ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
510         if (!ev_file)
511                 return ERR_PTR(-ENOMEM);
512
513         kref_init(&ev_file->ref);
514         spin_lock_init(&ev_file->lock);
515         INIT_LIST_HEAD(&ev_file->event_list);
516         init_waitqueue_head(&ev_file->poll_wait);
517         ev_file->uverbs_file = uverbs_file;
518         ev_file->async_queue = NULL;
519         ev_file->is_async    = is_async;
520         ev_file->is_closed   = 0;
521
522         filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
523                                   ev_file, O_RDONLY);
524         if (IS_ERR(filp))
525                 kfree(ev_file);
526
527         return filp;
528 }
529
530 /*
531  * Look up a completion event file by FD.  If lookup is successful,
532  * takes a ref to the event file struct that it returns; if
533  * unsuccessful, returns NULL.
534  */
535 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
536 {
537         struct ib_uverbs_event_file *ev_file = NULL;
538         struct file *filp;
539
540         filp = fget(fd);
541         if (!filp)
542                 return NULL;
543
544         if (filp->f_op != &uverbs_event_fops)
545                 goto out;
546
547         ev_file = filp->private_data;
548         if (ev_file->is_async) {
549                 ev_file = NULL;
550                 goto out;
551         }
552
553         kref_get(&ev_file->ref);
554
555 out:
556         fput(filp);
557         return ev_file;
558 }
559
560 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
561                              size_t count, loff_t *pos)
562 {
563         struct ib_uverbs_file *file = filp->private_data;
564         struct ib_uverbs_cmd_hdr hdr;
565
566         if (count < sizeof hdr)
567                 return -EINVAL;
568
569         if (copy_from_user(&hdr, buf, sizeof hdr))
570                 return -EFAULT;
571
572         if (hdr.in_words * 4 != count)
573                 return -EINVAL;
574
575         if (hdr.command < 0                             ||
576             hdr.command >= ARRAY_SIZE(uverbs_cmd_table) ||
577             !uverbs_cmd_table[hdr.command])
578                 return -EINVAL;
579
580         if (!file->ucontext &&
581             hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
582                 return -EINVAL;
583
584         if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command)))
585                 return -ENOSYS;
586
587         return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
588                                              hdr.in_words * 4, hdr.out_words * 4);
589 }
590
591 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
592 {
593         struct ib_uverbs_file *file = filp->private_data;
594
595         if (!file->ucontext)
596                 return -ENODEV;
597         else
598                 return file->device->ib_dev->mmap(file->ucontext, vma);
599 }
600
601 /*
602  * ib_uverbs_open() does not need the BKL:
603  *
604  *  - the ib_uverbs_device structures are properly reference counted and
605  *    everything else is purely local to the file being created, so
606  *    races against other open calls are not a problem;
607  *  - there is no ioctl method to race against;
608  *  - the open method will either immediately run -ENXIO, or all
609  *    required initialization will be done.
610  */
611 static int ib_uverbs_open(struct inode *inode, struct file *filp)
612 {
613         struct ib_uverbs_device *dev;
614         struct ib_uverbs_file *file;
615         int ret;
616
617         dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
618         if (dev)
619                 kref_get(&dev->ref);
620         else
621                 return -ENXIO;
622
623         if (!try_module_get(dev->ib_dev->owner)) {
624                 ret = -ENODEV;
625                 goto err;
626         }
627
628         file = kmalloc(sizeof *file, GFP_KERNEL);
629         if (!file) {
630                 ret = -ENOMEM;
631                 goto err_module;
632         }
633
634         file->device     = dev;
635         file->ucontext   = NULL;
636         file->async_file = NULL;
637         kref_init(&file->ref);
638         mutex_init(&file->mutex);
639
640         filp->private_data = file;
641
642         return nonseekable_open(inode, filp);
643
644 err_module:
645         module_put(dev->ib_dev->owner);
646
647 err:
648         kref_put(&dev->ref, ib_uverbs_release_dev);
649         return ret;
650 }
651
652 static int ib_uverbs_close(struct inode *inode, struct file *filp)
653 {
654         struct ib_uverbs_file *file = filp->private_data;
655
656         ib_uverbs_cleanup_ucontext(file, file->ucontext);
657
658         if (file->async_file)
659                 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
660
661         kref_put(&file->ref, ib_uverbs_release_file);
662
663         return 0;
664 }
665
666 static const struct file_operations uverbs_fops = {
667         .owner   = THIS_MODULE,
668         .write   = ib_uverbs_write,
669         .open    = ib_uverbs_open,
670         .release = ib_uverbs_close,
671         .llseek  = no_llseek,
672 };
673
674 static const struct file_operations uverbs_mmap_fops = {
675         .owner   = THIS_MODULE,
676         .write   = ib_uverbs_write,
677         .mmap    = ib_uverbs_mmap,
678         .open    = ib_uverbs_open,
679         .release = ib_uverbs_close,
680         .llseek  = no_llseek,
681 };
682
683 static struct ib_client uverbs_client = {
684         .name   = "uverbs",
685         .add    = ib_uverbs_add_one,
686         .remove = ib_uverbs_remove_one
687 };
688
689 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
690                           char *buf)
691 {
692         struct ib_uverbs_device *dev = dev_get_drvdata(device);
693
694         if (!dev)
695                 return -ENODEV;
696
697         return sprintf(buf, "%s\n", dev->ib_dev->name);
698 }
699 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
700
701 static ssize_t show_dev_abi_version(struct device *device,
702                                     struct device_attribute *attr, char *buf)
703 {
704         struct ib_uverbs_device *dev = dev_get_drvdata(device);
705
706         if (!dev)
707                 return -ENODEV;
708
709         return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
710 }
711 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
712
713 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
714                          __stringify(IB_USER_VERBS_ABI_VERSION));
715
716 static dev_t overflow_maj;
717 static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
718
719 /*
720  * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
721  * requesting a new major number and doubling the number of max devices we
722  * support. It's stupid, but simple.
723  */
724 static int find_overflow_devnum(void)
725 {
726         int ret;
727
728         if (!overflow_maj) {
729                 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
730                                           "infiniband_verbs");
731                 if (ret) {
732                         printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");
733                         return ret;
734                 }
735         }
736
737         ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
738         if (ret >= IB_UVERBS_MAX_DEVICES)
739                 return -1;
740
741         return ret;
742 }
743
744 static void ib_uverbs_add_one(struct ib_device *device)
745 {
746         int devnum;
747         dev_t base;
748         struct ib_uverbs_device *uverbs_dev;
749
750         if (!device->alloc_ucontext)
751                 return;
752
753         uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
754         if (!uverbs_dev)
755                 return;
756
757         kref_init(&uverbs_dev->ref);
758         init_completion(&uverbs_dev->comp);
759         uverbs_dev->xrcd_tree = RB_ROOT;
760         mutex_init(&uverbs_dev->xrcd_tree_mutex);
761
762         spin_lock(&map_lock);
763         devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
764         if (devnum >= IB_UVERBS_MAX_DEVICES) {
765                 spin_unlock(&map_lock);
766                 devnum = find_overflow_devnum();
767                 if (devnum < 0)
768                         goto err;
769
770                 spin_lock(&map_lock);
771                 uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
772                 base = devnum + overflow_maj;
773                 set_bit(devnum, overflow_map);
774         } else {
775                 uverbs_dev->devnum = devnum;
776                 base = devnum + IB_UVERBS_BASE_DEV;
777                 set_bit(devnum, dev_map);
778         }
779         spin_unlock(&map_lock);
780
781         uverbs_dev->ib_dev           = device;
782         uverbs_dev->num_comp_vectors = device->num_comp_vectors;
783
784         cdev_init(&uverbs_dev->cdev, NULL);
785         uverbs_dev->cdev.owner = THIS_MODULE;
786         uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
787         kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
788         if (cdev_add(&uverbs_dev->cdev, base, 1))
789                 goto err_cdev;
790
791         uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
792                                         uverbs_dev->cdev.dev, uverbs_dev,
793                                         "uverbs%d", uverbs_dev->devnum);
794         if (IS_ERR(uverbs_dev->dev))
795                 goto err_cdev;
796
797         if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
798                 goto err_class;
799         if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
800                 goto err_class;
801
802         ib_set_client_data(device, &uverbs_client, uverbs_dev);
803
804         return;
805
806 err_class:
807         device_destroy(uverbs_class, uverbs_dev->cdev.dev);
808
809 err_cdev:
810         cdev_del(&uverbs_dev->cdev);
811         if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
812                 clear_bit(devnum, dev_map);
813         else
814                 clear_bit(devnum, overflow_map);
815
816 err:
817         kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
818         wait_for_completion(&uverbs_dev->comp);
819         kfree(uverbs_dev);
820         return;
821 }
822
823 static void ib_uverbs_remove_one(struct ib_device *device)
824 {
825         struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
826
827         if (!uverbs_dev)
828                 return;
829
830         dev_set_drvdata(uverbs_dev->dev, NULL);
831         device_destroy(uverbs_class, uverbs_dev->cdev.dev);
832         cdev_del(&uverbs_dev->cdev);
833
834         if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
835                 clear_bit(uverbs_dev->devnum, dev_map);
836         else
837                 clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
838
839         kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
840         wait_for_completion(&uverbs_dev->comp);
841         kfree(uverbs_dev);
842 }
843
844 static char *uverbs_devnode(struct device *dev, mode_t *mode)
845 {
846         if (mode)
847                 *mode = 0666;
848         return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
849 }
850
851 static int __init ib_uverbs_init(void)
852 {
853         int ret;
854
855         ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
856                                      "infiniband_verbs");
857         if (ret) {
858                 printk(KERN_ERR "user_verbs: couldn't register device number\n");
859                 goto out;
860         }
861
862         uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
863         if (IS_ERR(uverbs_class)) {
864                 ret = PTR_ERR(uverbs_class);
865                 printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
866                 goto out_chrdev;
867         }
868
869         uverbs_class->devnode = uverbs_devnode;
870
871         ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
872         if (ret) {
873                 printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
874                 goto out_class;
875         }
876
877         ret = ib_register_client(&uverbs_client);
878         if (ret) {
879                 printk(KERN_ERR "user_verbs: couldn't register client\n");
880                 goto out_class;
881         }
882
883         return 0;
884
885 out_class:
886         class_destroy(uverbs_class);
887
888 out_chrdev:
889         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
890
891 out:
892         return ret;
893 }
894
895 static void __exit ib_uverbs_cleanup(void)
896 {
897         ib_unregister_client(&uverbs_client);
898         class_destroy(uverbs_class);
899         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
900         if (overflow_maj)
901                 unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
902         idr_destroy(&ib_uverbs_pd_idr);
903         idr_destroy(&ib_uverbs_mr_idr);
904         idr_destroy(&ib_uverbs_mw_idr);
905         idr_destroy(&ib_uverbs_ah_idr);
906         idr_destroy(&ib_uverbs_cq_idr);
907         idr_destroy(&ib_uverbs_qp_idr);
908         idr_destroy(&ib_uverbs_srq_idr);
909 }
910
911 module_init(ib_uverbs_init);
912 module_exit(ib_uverbs_cleanup);