strace: Remove the ability to write to Qstrace
[akaros.git] / kern / drivers / net / udrvr / 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 #if 0   /* AKAROS */
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/sched.h>
45 #include <linux/file.h>
46 #include <linux/cdev.h>
47 #include <linux/anon_inodes.h>
48 #include <linux/slab.h>
49
50 #include <asm/uaccess.h>
51 #endif  /* AKAROS */
52
53 #include "uverbs.h"
54
55 #if 1   /* AKAROS */
56
57 #define wake_up_interruptible(p)
58 #define kill_fasync(p, a, b)
59
60 #define kref_init(p)    kref_init(p, ib_uverbs_release_file, 1)
61 #define kref_put(p, f)  kref_put(p)
62 #define kref_get(p)     kref_get(p, 1)
63 #define put_pid(p)
64
65 #define kobject_init(a, b)
66
67 #define device_destroy(c, p)
68 #define device_create_file(d, p)        0
69 #define class_create_file(a, b)         0
70 #define try_module_get(p)               1
71 #define module_put(p)
72 #define kobject_get(p)
73 #define kobject_put(p)
74 #define nonseekable_open(a, b)          0
75
76 #define MKDEV(a, b)                     0
77 #define alloc_chrdev_region(a, b, c, d) -1
78
79 #endif  /* AKAROS */
80
81 MODULE_AUTHOR("Roland Dreier");
82 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
83 MODULE_LICENSE("Dual BSD/GPL");
84
85 enum {
86         IB_UVERBS_MAJOR       = 231,
87         IB_UVERBS_BASE_MINOR  = 192,
88         IB_UVERBS_MAX_DEVICES = 32
89 };
90
91 #define IB_UVERBS_BASE_DEV      MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
92
93 static struct class *uverbs_class;
94
95 DEFINE_SPINLOCK(ib_uverbs_idr_lock);
96 DEFINE_IDR(ib_uverbs_pd_idr);
97 DEFINE_IDR(ib_uverbs_mr_idr);
98 DEFINE_IDR(ib_uverbs_mw_idr);
99 DEFINE_IDR(ib_uverbs_ah_idr);
100 DEFINE_IDR(ib_uverbs_cq_idr);
101 DEFINE_IDR(ib_uverbs_qp_idr);
102 DEFINE_IDR(ib_uverbs_srq_idr);
103 DEFINE_IDR(ib_uverbs_xrcd_idr);
104 DEFINE_IDR(ib_uverbs_rule_idr);
105
106 static DEFINE_SPINLOCK(map_lock);
107 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
108
109 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
110                                      const char __user *buf, int in_len,
111                                      int out_len) = {
112         [IB_USER_VERBS_CMD_GET_CONTEXT]         = ib_uverbs_get_context,
113         [IB_USER_VERBS_CMD_QUERY_DEVICE]        = ib_uverbs_query_device,
114         [IB_USER_VERBS_CMD_QUERY_PORT]          = ib_uverbs_query_port,
115         [IB_USER_VERBS_CMD_ALLOC_PD]            = ib_uverbs_alloc_pd,
116         [IB_USER_VERBS_CMD_DEALLOC_PD]          = ib_uverbs_dealloc_pd,
117         [IB_USER_VERBS_CMD_REG_MR]              = ib_uverbs_reg_mr,
118         [IB_USER_VERBS_CMD_REREG_MR]            = ib_uverbs_rereg_mr,
119         [IB_USER_VERBS_CMD_DEREG_MR]            = ib_uverbs_dereg_mr,
120         [IB_USER_VERBS_CMD_ALLOC_MW]            = ib_uverbs_alloc_mw,
121         [IB_USER_VERBS_CMD_DEALLOC_MW]          = ib_uverbs_dealloc_mw,
122         [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
123         [IB_USER_VERBS_CMD_CREATE_CQ]           = ib_uverbs_create_cq,
124         [IB_USER_VERBS_CMD_RESIZE_CQ]           = ib_uverbs_resize_cq,
125         [IB_USER_VERBS_CMD_POLL_CQ]             = ib_uverbs_poll_cq,
126         [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ]       = ib_uverbs_req_notify_cq,
127         [IB_USER_VERBS_CMD_DESTROY_CQ]          = ib_uverbs_destroy_cq,
128         [IB_USER_VERBS_CMD_CREATE_QP]           = ib_uverbs_create_qp,
129         [IB_USER_VERBS_CMD_QUERY_QP]            = ib_uverbs_query_qp,
130         [IB_USER_VERBS_CMD_MODIFY_QP]           = ib_uverbs_modify_qp,
131         [IB_USER_VERBS_CMD_DESTROY_QP]          = ib_uverbs_destroy_qp,
132         [IB_USER_VERBS_CMD_POST_SEND]           = ib_uverbs_post_send,
133         [IB_USER_VERBS_CMD_POST_RECV]           = ib_uverbs_post_recv,
134         [IB_USER_VERBS_CMD_POST_SRQ_RECV]       = ib_uverbs_post_srq_recv,
135         [IB_USER_VERBS_CMD_CREATE_AH]           = ib_uverbs_create_ah,
136         [IB_USER_VERBS_CMD_DESTROY_AH]          = ib_uverbs_destroy_ah,
137         [IB_USER_VERBS_CMD_ATTACH_MCAST]        = ib_uverbs_attach_mcast,
138         [IB_USER_VERBS_CMD_DETACH_MCAST]        = ib_uverbs_detach_mcast,
139         [IB_USER_VERBS_CMD_CREATE_SRQ]          = ib_uverbs_create_srq,
140         [IB_USER_VERBS_CMD_MODIFY_SRQ]          = ib_uverbs_modify_srq,
141         [IB_USER_VERBS_CMD_QUERY_SRQ]           = ib_uverbs_query_srq,
142         [IB_USER_VERBS_CMD_DESTROY_SRQ]         = ib_uverbs_destroy_srq,
143         [IB_USER_VERBS_CMD_OPEN_XRCD]           = ib_uverbs_open_xrcd,
144         [IB_USER_VERBS_CMD_CLOSE_XRCD]          = ib_uverbs_close_xrcd,
145         [IB_USER_VERBS_CMD_CREATE_XSRQ]         = ib_uverbs_create_xsrq,
146         [IB_USER_VERBS_CMD_OPEN_QP]             = ib_uverbs_open_qp,
147 };
148
149 static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
150                                     struct ib_udata *ucore,
151                                     struct ib_udata *uhw) = {
152         [IB_USER_VERBS_EX_CMD_CREATE_FLOW]      = ib_uverbs_ex_create_flow,
153         [IB_USER_VERBS_EX_CMD_DESTROY_FLOW]     = ib_uverbs_ex_destroy_flow,
154         [IB_USER_VERBS_EX_CMD_QUERY_DEVICE]     = ib_uverbs_ex_query_device,
155 };
156
157 static void ib_uverbs_add_one(struct ib_device *device);
158 static void ib_uverbs_remove_one(struct ib_device *device);
159
160 static void ib_uverbs_release_dev(struct kobject *kobj)
161 {
162         struct ib_uverbs_device *dev =
163                 container_of(kobj, struct ib_uverbs_device, kobj);
164
165         kfree(dev);
166 }
167
168 #if 0   /* AKAROS */
169 static struct kobj_type ib_uverbs_dev_ktype = {
170         .release = ib_uverbs_release_dev,
171 };
172 #endif  /* AKAROS */
173
174 static void ib_uverbs_release_event_file(struct kref *ref)
175 {
176         struct ib_uverbs_event_file *file =
177                 container_of(ref, struct ib_uverbs_event_file, ref);
178
179         kfree(file);
180 }
181
182 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
183                           struct ib_uverbs_event_file *ev_file,
184                           struct ib_ucq_object *uobj)
185 {
186         struct ib_uverbs_event *evt, *tmp;
187
188         if (ev_file) {
189                 spin_lock_irq(&ev_file->lock);
190                 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
191                         list_del(&evt->list);
192                         kfree(evt);
193                 }
194                 spin_unlock_irq(&ev_file->lock);
195
196                 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
197         }
198
199 #if 0   /* AKAROS */
200         spin_lock_irq(&file->async_file->lock);
201         list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
202                 list_del(&evt->list);
203                 kfree(evt);
204         }
205         spin_unlock_irq(&file->async_file->lock);
206 #endif  /* AKAROS */
207 }
208
209 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
210                               struct ib_uevent_object *uobj)
211 {
212 #if 0   /* AKAROS */
213         struct ib_uverbs_event *evt, *tmp;
214
215         spin_lock_irq(&file->async_file->lock);
216         list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
217                 list_del(&evt->list);
218                 kfree(evt);
219         }
220         spin_unlock_irq(&file->async_file->lock);
221 #endif  /* AKAROS */
222 }
223
224 static void ib_uverbs_detach_umcast(struct ib_qp *qp,
225                                     struct ib_uqp_object *uobj)
226 {
227         struct ib_uverbs_mcast_entry *mcast, *tmp;
228
229         list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
230                 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
231                 list_del(&mcast->list);
232                 kfree(mcast);
233         }
234 }
235
236 static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
237                                       struct ib_ucontext *context)
238 {
239         struct ib_uobject *uobj, *tmp;
240
241         if (!context)
242                 return 0;
243
244         context->closing = 1;
245
246         list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
247                 struct ib_ah *ah = uobj->object;
248
249                 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
250                 ib_destroy_ah(ah);
251                 kfree(uobj);
252         }
253
254         /* Remove MWs before QPs, in order to support type 2A MWs. */
255         list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
256                 struct ib_mw *mw = uobj->object;
257
258                 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
259                 ib_dealloc_mw(mw);
260                 kfree(uobj);
261         }
262
263         list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
264                 struct ib_flow *flow_id = uobj->object;
265
266                 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
267                 ib_destroy_flow(flow_id);
268                 kfree(uobj);
269         }
270
271         list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
272                 struct ib_qp *qp = uobj->object;
273                 struct ib_uqp_object *uqp =
274                         container_of(uobj, struct ib_uqp_object, uevent.uobject);
275
276                 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
277                 if (qp != qp->real_qp) {
278                         ib_close_qp(qp);
279                 } else {
280                         ib_uverbs_detach_umcast(qp, uqp);
281                         ib_destroy_qp(qp);
282                 }
283                 ib_uverbs_release_uevent(file, &uqp->uevent);
284                 kfree(uqp);
285         }
286
287         list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
288                 struct ib_srq *srq = uobj->object;
289                 struct ib_uevent_object *uevent =
290                         container_of(uobj, struct ib_uevent_object, uobject);
291
292                 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
293                 ib_destroy_srq(srq);
294                 ib_uverbs_release_uevent(file, uevent);
295                 kfree(uevent);
296         }
297
298         list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
299                 struct ib_cq *cq = uobj->object;
300                 struct ib_uverbs_event_file *ev_file = cq->cq_context;
301                 struct ib_ucq_object *ucq =
302                         container_of(uobj, struct ib_ucq_object, uobject);
303
304                 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
305                 ib_destroy_cq(cq);
306                 ib_uverbs_release_ucq(file, ev_file, ucq);
307                 kfree(ucq);
308         }
309
310         list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
311                 struct ib_mr *mr = uobj->object;
312
313                 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
314                 ib_dereg_mr(mr);
315                 kfree(uobj);
316         }
317
318         mutex_lock(&file->device->xrcd_tree_mutex);
319         list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
320                 struct ib_xrcd *xrcd = uobj->object;
321                 struct ib_uxrcd_object *uxrcd =
322                         container_of(uobj, struct ib_uxrcd_object, uobject);
323
324                 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
325                 ib_uverbs_dealloc_xrcd(file->device, xrcd);
326                 kfree(uxrcd);
327         }
328         mutex_unlock(&file->device->xrcd_tree_mutex);
329
330         list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
331                 struct ib_pd *pd = uobj->object;
332
333                 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
334                 ib_dealloc_pd(pd);
335                 kfree(uobj);
336         }
337
338         put_pid(context->tgid);
339
340         return context->device->dealloc_ucontext(context);
341 }
342
343 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
344 {
345         complete(&dev->comp);
346 }
347
348 static void ib_uverbs_release_file(struct kref *ref)
349 {
350         struct ib_uverbs_file *file =
351                 container_of(ref, struct ib_uverbs_file, ref);
352
353         module_put(file->device->ib_dev->owner);
354         if (atomic_dec_and_test(&file->device->refcount))
355                 ib_uverbs_comp_dev(file->device);
356
357         kfree(file);
358 }
359
360 #if 0   /* AKAROS */
361 static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
362                                     size_t count, loff_t *pos)
363 {
364         struct ib_uverbs_event_file *file = filp->private_data;
365         struct ib_uverbs_event *event;
366         int eventsz;
367         int ret = 0;
368
369         spin_lock_irq(&file->lock);
370
371         while (list_empty(&file->event_list)) {
372                 spin_unlock_irq(&file->lock);
373
374                 if (filp->f_flags & O_NONBLOCK)
375                         return -EAGAIN;
376
377                 if (wait_event_interruptible(file->poll_wait,
378                                              !list_empty(&file->event_list)))
379                         return -ERESTARTSYS;
380
381                 spin_lock_irq(&file->lock);
382         }
383
384         event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
385
386         if (file->is_async)
387                 eventsz = sizeof (struct ib_uverbs_async_event_desc);
388         else
389                 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
390
391         if (eventsz > count) {
392                 ret   = -EINVAL;
393                 event = NULL;
394         } else {
395                 list_del(file->event_list.next);
396                 if (event->counter) {
397                         ++(*event->counter);
398                         list_del(&event->obj_list);
399                 }
400         }
401
402         spin_unlock_irq(&file->lock);
403
404         if (event) {
405                 if (copy_to_user(buf, event, eventsz))
406                         ret = -EFAULT;
407                 else
408                         ret = eventsz;
409         }
410
411         kfree(event);
412
413         return ret;
414 }
415
416 static unsigned int ib_uverbs_event_poll(struct file *filp,
417                                          struct poll_table_struct *wait)
418 {
419         unsigned int pollflags = 0;
420         struct ib_uverbs_event_file *file = filp->private_data;
421
422         poll_wait(filp, &file->poll_wait, wait);
423
424         spin_lock_irq(&file->lock);
425         if (!list_empty(&file->event_list))
426                 pollflags = POLLIN | POLLRDNORM;
427         spin_unlock_irq(&file->lock);
428
429         return pollflags;
430 }
431
432 static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
433 {
434         struct ib_uverbs_event_file *file = filp->private_data;
435
436         return fasync_helper(fd, filp, on, &file->async_queue);
437 }
438
439 static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
440 {
441         struct ib_uverbs_event_file *file = filp->private_data;
442         struct ib_uverbs_event *entry, *tmp;
443
444         spin_lock_irq(&file->lock);
445         file->is_closed = 1;
446         list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
447                 if (entry->counter)
448                         list_del(&entry->obj_list);
449                 kfree(entry);
450         }
451         spin_unlock_irq(&file->lock);
452
453         if (file->is_async) {
454                 ib_unregister_event_handler(&file->uverbs_file->event_handler);
455                 kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
456         }
457         kref_put(&file->ref, ib_uverbs_release_event_file);
458
459         return 0;
460 }
461
462 static const struct file_operations uverbs_event_fops = {
463         .owner   = THIS_MODULE,
464         .read    = ib_uverbs_event_read,
465         .poll    = ib_uverbs_event_poll,
466         .release = ib_uverbs_event_close,
467         .fasync  = ib_uverbs_event_fasync,
468         .llseek  = no_llseek,
469 };
470
471 #endif  /* AKAROS */
472
473 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
474 {
475         struct ib_uverbs_event_file    *file = cq_context;
476         struct ib_ucq_object           *uobj;
477         struct ib_uverbs_event         *entry;
478         unsigned long                   flags;
479
480         if (!file)
481                 return;
482
483         spin_lock_irqsave(&file->lock, flags);
484         if (file->is_closed) {
485                 spin_unlock_irqrestore(&file->lock, flags);
486                 return;
487         }
488
489         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
490         if (!entry) {
491                 spin_unlock_irqrestore(&file->lock, flags);
492                 return;
493         }
494
495         uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
496
497         entry->desc.comp.cq_handle = cq->uobject->user_handle;
498         entry->counter             = &uobj->comp_events_reported;
499
500         list_add_tail(&entry->list, &file->event_list);
501         list_add_tail(&entry->obj_list, &uobj->comp_list);
502         spin_unlock_irqrestore(&file->lock, flags);
503
504         wake_up_interruptible(&file->poll_wait);
505         kill_fasync(&file->async_queue, SIGIO, POLL_IN);
506 }
507
508 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
509                                     __u64 element, __u64 event,
510                                     struct list_head *obj_list,
511                                     u32 *counter)
512 {
513         struct ib_uverbs_event *entry;
514         unsigned long flags;
515
516         spin_lock_irqsave(&file->async_file->lock, flags);
517         if (file->async_file->is_closed) {
518                 spin_unlock_irqrestore(&file->async_file->lock, flags);
519                 return;
520         }
521
522         entry = kmalloc(sizeof *entry, GFP_ATOMIC);
523         if (!entry) {
524                 spin_unlock_irqrestore(&file->async_file->lock, flags);
525                 return;
526         }
527
528         entry->desc.async.element    = element;
529         entry->desc.async.event_type = event;
530         entry->desc.async.reserved   = 0;
531         entry->counter               = counter;
532
533         list_add_tail(&entry->list, &file->async_file->event_list);
534         if (obj_list)
535                 list_add_tail(&entry->obj_list, obj_list);
536         spin_unlock_irqrestore(&file->async_file->lock, flags);
537
538         wake_up_interruptible(&file->async_file->poll_wait);
539         kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
540 }
541
542 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
543 {
544         struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
545                                                   struct ib_ucq_object, uobject);
546
547         ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
548                                 event->event, &uobj->async_list,
549                                 &uobj->async_events_reported);
550 }
551
552 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
553 {
554         struct ib_uevent_object *uobj;
555
556         /* for XRC target qp's, check that qp is live */
557         if (!event->element.qp->uobject || !event->element.qp->uobject->live)
558                 return;
559
560         uobj = container_of(event->element.qp->uobject,
561                             struct ib_uevent_object, uobject);
562
563         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
564                                 event->event, &uobj->event_list,
565                                 &uobj->events_reported);
566 }
567
568 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
569 {
570         struct ib_uevent_object *uobj;
571
572         uobj = container_of(event->element.srq->uobject,
573                             struct ib_uevent_object, uobject);
574
575         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
576                                 event->event, &uobj->event_list,
577                                 &uobj->events_reported);
578 }
579
580 void ib_uverbs_event_handler(struct ib_event_handler *handler,
581                              struct ib_event *event)
582 {
583         struct ib_uverbs_file *file =
584                 container_of(handler, struct ib_uverbs_file, event_handler);
585
586         ib_uverbs_async_handler(file, event->element.port_num, event->event,
587                                 NULL, NULL);
588 }
589
590 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
591                                         int is_async)
592 {
593 #if 0   /* AKAROS */
594         struct ib_uverbs_event_file *ev_file;
595         struct file *filp;
596
597         ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
598         if (!ev_file)
599                 return ERR_PTR(-ENOMEM);
600
601         kref_init(&ev_file->ref);
602         spin_lock_init(&ev_file->lock);
603         INIT_LIST_HEAD(&ev_file->event_list);
604         init_waitqueue_head(&ev_file->poll_wait);
605         ev_file->uverbs_file = uverbs_file;
606         ev_file->async_queue = NULL;
607         ev_file->is_async    = is_async;
608         ev_file->is_closed   = 0;
609
610         filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
611                                   ev_file, O_RDONLY);
612         if (IS_ERR(filp))
613                 kfree(ev_file);
614
615         return filp;
616 #else   /* AKAROS */
617         BUG_ON(1);
618         return NULL;
619 #endif  /* AKAROS */
620 }
621
622 /*
623  * Look up a completion event file by FD.  If lookup is successful,
624  * takes a ref to the event file struct that it returns; if
625  * unsuccessful, returns NULL.
626  */
627 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
628 {
629         struct ib_uverbs_event_file *ev_file = NULL;
630 #if 0   /* AKAROS */
631         struct fd f = fdget(fd);
632
633         if (!f.file)
634                 return NULL;
635
636         if (f.file->f_op != &uverbs_event_fops)
637                 goto out;
638
639         ev_file = f.file->private_data;
640         if (ev_file->is_async) {
641                 ev_file = NULL;
642                 goto out;
643         }
644
645         kref_get(&ev_file->ref);
646
647 out:
648         fdput(f);
649 #lse    /* AKAROS */
650         BUG_ON(1);
651 #endif  /* AKAROS */
652         return ev_file;
653 }
654
655 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
656                              size_t count, loff_t *pos)
657 {
658         struct ib_uverbs_file *file = filp->private_data;
659         struct ib_uverbs_cmd_hdr hdr;
660         __u32 flags;
661
662         if (count < sizeof hdr)
663                 return -EINVAL;
664
665         if (copy_from_user(&hdr, buf, sizeof hdr))
666                 return -EFAULT;
667
668 #if 1   /* AKAROS */
669         ssize_t ret;
670
671         if ((ret = check_old_abi(filp, buf, count)))
672                 return ret;
673 #endif  /* AKAROS */
674
675         flags = (hdr.command &
676                  IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
677
678         if (!flags) {
679                 __u32 command;
680
681                 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
682                                            IB_USER_VERBS_CMD_COMMAND_MASK))
683                         return -EINVAL;
684
685                 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
686
687                 if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
688                     !uverbs_cmd_table[command])
689                         return -EINVAL;
690
691                 if (!file->ucontext &&
692                     command != IB_USER_VERBS_CMD_GET_CONTEXT)
693                         return -EINVAL;
694
695                 if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << command)))
696                         return -ENOSYS;
697
698                 if (hdr.in_words * 4 != count)
699                         return -EINVAL;
700
701                 return uverbs_cmd_table[command](file,
702                                                  buf + sizeof(hdr),
703                                                  hdr.in_words * 4,
704                                                  hdr.out_words * 4);
705
706         } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
707                 __u32 command;
708
709                 struct ib_uverbs_ex_cmd_hdr ex_hdr;
710                 struct ib_udata ucore;
711                 struct ib_udata uhw;
712                 int err;
713                 size_t written_count = count;
714
715                 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
716                                            IB_USER_VERBS_CMD_COMMAND_MASK))
717                         return -EINVAL;
718
719                 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
720
721                 if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
722                     !uverbs_ex_cmd_table[command])
723                         return -ENOSYS;
724
725                 if (!file->ucontext)
726                         return -EINVAL;
727
728                 if (!(file->device->ib_dev->uverbs_ex_cmd_mask & (1ull << command)))
729                         return -ENOSYS;
730
731                 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
732                         return -EINVAL;
733
734                 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
735                         return -EFAULT;
736
737                 count -= sizeof(hdr) + sizeof(ex_hdr);
738                 buf += sizeof(hdr) + sizeof(ex_hdr);
739
740                 if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count)
741                         return -EINVAL;
742
743                 if (ex_hdr.cmd_hdr_reserved)
744                         return -EINVAL;
745
746                 if (ex_hdr.response) {
747                         if (!hdr.out_words && !ex_hdr.provider_out_words)
748                                 return -EINVAL;
749
750                         if (!access_ok(VERIFY_WRITE,
751                                        (void __user *) (unsigned long) ex_hdr.response,
752                                        (hdr.out_words + ex_hdr.provider_out_words) * 8))
753                                 return -EFAULT;
754                 } else {
755                         if (hdr.out_words || ex_hdr.provider_out_words)
756                                 return -EINVAL;
757                 }
758
759                 INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
760                                        hdr.in_words * 8, hdr.out_words * 8);
761
762                 INIT_UDATA_BUF_OR_NULL(&uhw,
763                                        buf + ucore.inlen,
764                                        (unsigned long) ex_hdr.response + ucore.outlen,
765                                        ex_hdr.provider_in_words * 8,
766                                        ex_hdr.provider_out_words * 8);
767
768                 err = uverbs_ex_cmd_table[command](file,
769                                                    &ucore,
770                                                    &uhw);
771
772                 if (err)
773                         return err;
774
775                 return written_count;
776         }
777
778         return -ENOSYS;
779 }
780
781 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
782 {
783         struct ib_uverbs_file *file = filp->private_data;
784
785         if (!file->ucontext)
786                 return -ENODEV;
787         else
788                 return file->device->ib_dev->mmap(file->ucontext, vma);
789 }
790
791 /*
792  * ib_uverbs_open() does not need the BKL:
793  *
794  *  - the ib_uverbs_device structures are properly reference counted and
795  *    everything else is purely local to the file being created, so
796  *    races against other open calls are not a problem;
797  *  - there is no ioctl method to race against;
798  *  - the open method will either immediately run -ENXIO, or all
799  *    required initialization will be done.
800  */
801 static int ib_uverbs_open(struct inode *inode, struct file *filp)
802 {
803         struct ib_uverbs_device *dev;
804         struct ib_uverbs_file *file;
805         int ret;
806
807 #if 0   /* AKAROS */
808         dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
809 #else   /* AKAROS */
810         dev = get_fs_info(filp);
811 #endif  /* AKAROS */
812         if (!atomic_inc_not_zero(&dev->refcount))
813                 return -ENXIO;
814
815         if (!try_module_get(dev->ib_dev->owner)) {
816                 ret = -ENODEV;
817                 goto err;
818         }
819
820         file = kmalloc(sizeof *file, GFP_KERNEL);
821         if (!file) {
822                 ret = -ENOMEM;
823                 goto err_module;
824         }
825
826         file->device     = dev;
827         file->ucontext   = NULL;
828         file->async_file = NULL;
829         kref_init(&file->ref);
830         mutex_init(&file->mutex);
831
832         filp->private_data = file;
833         kobject_get(&dev->kobj);
834
835         return nonseekable_open(inode, filp);
836
837 err_module:
838         module_put(dev->ib_dev->owner);
839
840 err:
841         if (atomic_dec_and_test(&dev->refcount))
842                 ib_uverbs_comp_dev(dev);
843
844         return ret;
845 }
846
847 static int ib_uverbs_close(struct inode *inode, struct file *filp)
848 {
849         struct ib_uverbs_file *file = filp->private_data;
850         struct ib_uverbs_device *dev = file->device;
851
852         ib_uverbs_cleanup_ucontext(file, file->ucontext);
853
854         if (file->async_file)
855                 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
856
857         kref_put(&file->ref, ib_uverbs_release_file);
858         kobject_put(&dev->kobj);
859
860         return 0;
861 }
862
863 static const struct file_operations uverbs_fops = {
864 #if 0   /* AKAROS */
865         .owner   = THIS_MODULE,
866 #endif  /* AKAROS */
867         .write   = ib_uverbs_write,
868         .open    = ib_uverbs_open,
869         .release = ib_uverbs_close,
870 #if 0   /* AKAROS */
871         .llseek  = no_llseek,
872 #endif  /* AKAROS */
873 };
874
875 static const struct file_operations uverbs_mmap_fops = {
876 #if 0   /* AKAROS */
877         .owner   = THIS_MODULE,
878 #endif  /* AKAROS */
879         .write   = ib_uverbs_write,
880         .mmap    = ib_uverbs_mmap,
881         .open    = ib_uverbs_open,
882         .release = ib_uverbs_close,
883 #if 0   /* AKAROS */
884         .llseek  = no_llseek,
885 #endif  /* AKAROS */
886 };
887
888 static struct ib_client uverbs_client = {
889         .name   = "uverbs",
890         .add    = ib_uverbs_add_one,
891         .remove = ib_uverbs_remove_one
892 };
893
894 #if 0   /* AKAROS */
895 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
896                           char *buf)
897 {
898         struct ib_uverbs_device *dev = dev_get_drvdata(device);
899
900         if (!dev)
901                 return -ENODEV;
902
903         return sprintf(buf, "%s\n", dev->ib_dev->name);
904 }
905 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
906
907 static ssize_t show_dev_abi_version(struct device *device,
908                                     struct device_attribute *attr, char *buf)
909 {
910         struct ib_uverbs_device *dev = dev_get_drvdata(device);
911
912         if (!dev)
913                 return -ENODEV;
914
915         return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
916 }
917 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
918
919 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
920                          __stringify(IB_USER_VERBS_ABI_VERSION));
921 #endif  /* AKAROS */
922
923 static dev_t overflow_maj;
924 static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
925
926 /*
927  * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
928  * requesting a new major number and doubling the number of max devices we
929  * support. It's stupid, but simple.
930  */
931 static int find_overflow_devnum(void)
932 {
933         int ret;
934
935         if (!overflow_maj) {
936                 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
937                                           "infiniband_verbs");
938                 if (ret) {
939                         printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");
940                         return ret;
941                 }
942         }
943
944         ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
945         if (ret >= IB_UVERBS_MAX_DEVICES)
946                 return -1;
947
948         return ret;
949 }
950
951 static void ib_uverbs_add_one(struct ib_device *device)
952 {
953         int devnum;
954         dev_t base;
955         struct ib_uverbs_device *uverbs_dev;
956
957         if (!device->alloc_ucontext)
958                 return;
959
960         uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
961         if (!uverbs_dev)
962                 return;
963
964         atomic_set(&uverbs_dev->refcount, 1);
965         init_completion(&uverbs_dev->comp);
966         uverbs_dev->xrcd_tree = RB_ROOT;
967         mutex_init(&uverbs_dev->xrcd_tree_mutex);
968         kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
969
970         spin_lock(&map_lock);
971         devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
972         if (devnum >= IB_UVERBS_MAX_DEVICES) {
973                 spin_unlock(&map_lock);
974                 devnum = find_overflow_devnum();
975                 if (devnum < 0)
976                         goto err;
977
978                 spin_lock(&map_lock);
979                 uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
980                 base = devnum + overflow_maj;
981                 set_bit(devnum, overflow_map);
982         } else {
983                 uverbs_dev->devnum = devnum;
984                 base = devnum + IB_UVERBS_BASE_DEV;
985                 set_bit(devnum, dev_map);
986         }
987         spin_unlock(&map_lock);
988
989         uverbs_dev->ib_dev           = device;
990         uverbs_dev->num_comp_vectors = device->num_comp_vectors;
991
992 #if 0   /* AKAROS */
993         cdev_init(&uverbs_dev->cdev, NULL);
994         uverbs_dev->cdev.owner = THIS_MODULE;
995         uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
996         uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj;
997         kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
998         if (cdev_add(&uverbs_dev->cdev, base, 1))
999                 goto err_cdev;
1000
1001         uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
1002                                         uverbs_dev->cdev.dev, uverbs_dev,
1003                                         "uverbs%d", uverbs_dev->devnum);
1004         if (IS_ERR(uverbs_dev->dev))
1005                 goto err_cdev;
1006
1007         if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1008                 goto err_class;
1009         if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1010                 goto err_class;
1011 #else   /* AKAROS */
1012         sysfs_create(devnum, &uverbs_mmap_fops, uverbs_dev);
1013 #endif  /* AKAROS */
1014
1015         ib_set_client_data(device, &uverbs_client, uverbs_dev);
1016
1017         return;
1018
1019 #if 0   /* AKAROS */
1020 err_class:
1021         device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1022
1023 err_cdev:
1024         cdev_del(&uverbs_dev->cdev);
1025         if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
1026                 clear_bit(devnum, dev_map);
1027         else
1028                 clear_bit(devnum, overflow_map);
1029 #endif  /* AKAROS */
1030
1031 err:
1032         if (atomic_dec_and_test(&uverbs_dev->refcount))
1033                 ib_uverbs_comp_dev(uverbs_dev);
1034         wait_for_completion(&uverbs_dev->comp);
1035         kobject_put(&uverbs_dev->kobj);
1036         return;
1037 }
1038
1039 static void ib_uverbs_remove_one(struct ib_device *device)
1040 {
1041         struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
1042
1043         if (!uverbs_dev)
1044                 return;
1045
1046 #if 0   /* AKAROS */
1047         dev_set_drvdata(uverbs_dev->dev, NULL);
1048         device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1049         cdev_del(&uverbs_dev->cdev);
1050 #endif  /* AKAROS */
1051
1052         if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
1053                 clear_bit(uverbs_dev->devnum, dev_map);
1054         else
1055                 clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
1056
1057         if (atomic_dec_and_test(&uverbs_dev->refcount))
1058                 ib_uverbs_comp_dev(uverbs_dev);
1059         wait_for_completion(&uverbs_dev->comp);
1060         kobject_put(&uverbs_dev->kobj);
1061 }
1062
1063 #if 0   /* AKAROS */
1064 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1065 {
1066         if (mode)
1067                 *mode = 0666;
1068         return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1069 }
1070
1071 static int __init ib_uverbs_init(void)
1072 #else   /* AKAROS */
1073 int __init ib_uverbs_init(void)
1074 #endif  /* AKAROS */
1075 {
1076         int ret;
1077
1078 #if 0   /* AKAROS */
1079         ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
1080                                      "infiniband_verbs");
1081         if (ret) {
1082                 printk(KERN_ERR "user_verbs: couldn't register device number\n");
1083                 goto out;
1084         }
1085
1086         uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1087         if (IS_ERR(uverbs_class)) {
1088                 ret = PTR_ERR(uverbs_class);
1089                 printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
1090                 goto out_chrdev;
1091         }
1092
1093         uverbs_class->devnode = uverbs_devnode;
1094
1095         ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1096         if (ret) {
1097                 printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
1098                 goto out_class;
1099         }
1100 #else   /* AKAROS */
1101         sysfs_init();
1102 #endif  /* AKAROS */
1103
1104         ret = ib_register_client(&uverbs_client);
1105         if (ret) {
1106                 printk(KERN_ERR "user_verbs: couldn't register client\n");
1107                 goto out_class;
1108         }
1109
1110         return 0;
1111
1112 out_class:
1113 #if 0   /* AKAROS */
1114         class_destroy(uverbs_class);
1115
1116 out_chrdev:
1117         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1118
1119 out:
1120 #endif  /* AKAROS */
1121         return ret;
1122 }
1123
1124 static void __exit ib_uverbs_cleanup(void)
1125 {
1126         ib_unregister_client(&uverbs_client);
1127 #if 0   /* AKAROS */
1128         class_destroy(uverbs_class);
1129         unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1130         if (overflow_maj)
1131                 unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
1132         idr_destroy(&ib_uverbs_pd_idr);
1133         idr_destroy(&ib_uverbs_mr_idr);
1134         idr_destroy(&ib_uverbs_mw_idr);
1135         idr_destroy(&ib_uverbs_ah_idr);
1136         idr_destroy(&ib_uverbs_cq_idr);
1137         idr_destroy(&ib_uverbs_qp_idr);
1138         idr_destroy(&ib_uverbs_srq_idr);
1139 #endif  /* AKAROS */
1140 }
1141
1142 module_init(ib_uverbs_init);
1143 module_exit(ib_uverbs_cleanup);