GNU Linux-libre 4.19.207-gnu1
[releases.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 <linux/uaccess.h>
50
51 #include <rdma/ib.h>
52 #include <rdma/uverbs_std_types.h>
53
54 #include "uverbs.h"
55 #include "core_priv.h"
56 #include "rdma_core.h"
57
58 MODULE_AUTHOR("Roland Dreier");
59 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
60 MODULE_LICENSE("Dual BSD/GPL");
61
62 enum {
63         IB_UVERBS_MAJOR       = 231,
64         IB_UVERBS_BASE_MINOR  = 192,
65         IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
66         IB_UVERBS_NUM_FIXED_MINOR = 32,
67         IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
68 };
69
70 #define IB_UVERBS_BASE_DEV      MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
71
72 static dev_t dynamic_uverbs_dev;
73 static struct class *uverbs_class;
74
75 static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
76
77 static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
78                                      const char __user *buf, int in_len,
79                                      int out_len) = {
80         [IB_USER_VERBS_CMD_GET_CONTEXT]         = ib_uverbs_get_context,
81         [IB_USER_VERBS_CMD_QUERY_DEVICE]        = ib_uverbs_query_device,
82         [IB_USER_VERBS_CMD_QUERY_PORT]          = ib_uverbs_query_port,
83         [IB_USER_VERBS_CMD_ALLOC_PD]            = ib_uverbs_alloc_pd,
84         [IB_USER_VERBS_CMD_DEALLOC_PD]          = ib_uverbs_dealloc_pd,
85         [IB_USER_VERBS_CMD_REG_MR]              = ib_uverbs_reg_mr,
86         [IB_USER_VERBS_CMD_REREG_MR]            = ib_uverbs_rereg_mr,
87         [IB_USER_VERBS_CMD_DEREG_MR]            = ib_uverbs_dereg_mr,
88         [IB_USER_VERBS_CMD_ALLOC_MW]            = ib_uverbs_alloc_mw,
89         [IB_USER_VERBS_CMD_DEALLOC_MW]          = ib_uverbs_dealloc_mw,
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         [IB_USER_VERBS_CMD_CREATE_XSRQ]         = ib_uverbs_create_xsrq,
114         [IB_USER_VERBS_CMD_OPEN_QP]             = ib_uverbs_open_qp,
115 };
116
117 static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
118                                     struct ib_udata *ucore,
119                                     struct ib_udata *uhw) = {
120         [IB_USER_VERBS_EX_CMD_CREATE_FLOW]      = ib_uverbs_ex_create_flow,
121         [IB_USER_VERBS_EX_CMD_DESTROY_FLOW]     = ib_uverbs_ex_destroy_flow,
122         [IB_USER_VERBS_EX_CMD_QUERY_DEVICE]     = ib_uverbs_ex_query_device,
123         [IB_USER_VERBS_EX_CMD_CREATE_CQ]        = ib_uverbs_ex_create_cq,
124         [IB_USER_VERBS_EX_CMD_CREATE_QP]        = ib_uverbs_ex_create_qp,
125         [IB_USER_VERBS_EX_CMD_CREATE_WQ]        = ib_uverbs_ex_create_wq,
126         [IB_USER_VERBS_EX_CMD_MODIFY_WQ]        = ib_uverbs_ex_modify_wq,
127         [IB_USER_VERBS_EX_CMD_DESTROY_WQ]       = ib_uverbs_ex_destroy_wq,
128         [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table,
129         [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table,
130         [IB_USER_VERBS_EX_CMD_MODIFY_QP]        = ib_uverbs_ex_modify_qp,
131         [IB_USER_VERBS_EX_CMD_MODIFY_CQ]        = ib_uverbs_ex_modify_cq,
132 };
133
134 static void ib_uverbs_add_one(struct ib_device *device);
135 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
136
137 /*
138  * Must be called with the ufile->device->disassociate_srcu held, and the lock
139  * must be held until use of the ucontext is finished.
140  */
141 struct ib_ucontext *ib_uverbs_get_ucontext(struct ib_uverbs_file *ufile)
142 {
143         /*
144          * We do not hold the hw_destroy_rwsem lock for this flow, instead
145          * srcu is used. It does not matter if someone races this with
146          * get_context, we get NULL or valid ucontext.
147          */
148         struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);
149
150         if (!srcu_dereference(ufile->device->ib_dev,
151                               &ufile->device->disassociate_srcu))
152                 return ERR_PTR(-EIO);
153
154         if (!ucontext)
155                 return ERR_PTR(-EINVAL);
156
157         return ucontext;
158 }
159 EXPORT_SYMBOL(ib_uverbs_get_ucontext);
160
161 int uverbs_dealloc_mw(struct ib_mw *mw)
162 {
163         struct ib_pd *pd = mw->pd;
164         int ret;
165
166         ret = mw->device->dealloc_mw(mw);
167         if (!ret)
168                 atomic_dec(&pd->usecnt);
169         return ret;
170 }
171
172 static void ib_uverbs_release_dev(struct kobject *kobj)
173 {
174         struct ib_uverbs_device *dev =
175                 container_of(kobj, struct ib_uverbs_device, kobj);
176
177         uverbs_destroy_api(dev->uapi);
178         cleanup_srcu_struct(&dev->disassociate_srcu);
179         kfree(dev);
180 }
181
182 static struct kobj_type ib_uverbs_dev_ktype = {
183         .release = ib_uverbs_release_dev,
184 };
185
186 static void ib_uverbs_release_async_event_file(struct kref *ref)
187 {
188         struct ib_uverbs_async_event_file *file =
189                 container_of(ref, struct ib_uverbs_async_event_file, ref);
190
191         kfree(file);
192 }
193
194 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
195                           struct ib_uverbs_completion_event_file *ev_file,
196                           struct ib_ucq_object *uobj)
197 {
198         struct ib_uverbs_event *evt, *tmp;
199
200         if (ev_file) {
201                 spin_lock_irq(&ev_file->ev_queue.lock);
202                 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
203                         list_del(&evt->list);
204                         kfree(evt);
205                 }
206                 spin_unlock_irq(&ev_file->ev_queue.lock);
207
208                 uverbs_uobject_put(&ev_file->uobj);
209         }
210
211         spin_lock_irq(&file->async_file->ev_queue.lock);
212         list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
213                 list_del(&evt->list);
214                 kfree(evt);
215         }
216         spin_unlock_irq(&file->async_file->ev_queue.lock);
217 }
218
219 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
220                               struct ib_uevent_object *uobj)
221 {
222         struct ib_uverbs_event *evt, *tmp;
223
224         spin_lock_irq(&file->async_file->ev_queue.lock);
225         list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
226                 list_del(&evt->list);
227                 kfree(evt);
228         }
229         spin_unlock_irq(&file->async_file->ev_queue.lock);
230 }
231
232 void ib_uverbs_detach_umcast(struct ib_qp *qp,
233                              struct ib_uqp_object *uobj)
234 {
235         struct ib_uverbs_mcast_entry *mcast, *tmp;
236
237         list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
238                 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
239                 list_del(&mcast->list);
240                 kfree(mcast);
241         }
242 }
243
244 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
245 {
246         complete(&dev->comp);
247 }
248
249 void ib_uverbs_release_file(struct kref *ref)
250 {
251         struct ib_uverbs_file *file =
252                 container_of(ref, struct ib_uverbs_file, ref);
253         struct ib_device *ib_dev;
254         int srcu_key;
255
256         release_ufile_idr_uobject(file);
257
258         srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
259         ib_dev = srcu_dereference(file->device->ib_dev,
260                                   &file->device->disassociate_srcu);
261         if (ib_dev && !ib_dev->disassociate_ucontext)
262                 module_put(ib_dev->owner);
263         srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
264
265         if (atomic_dec_and_test(&file->device->refcount))
266                 ib_uverbs_comp_dev(file->device);
267
268         if (file->async_file)
269                 kref_put(&file->async_file->ref,
270                          ib_uverbs_release_async_event_file);
271         kobject_put(&file->device->kobj);
272         kfree(file);
273 }
274
275 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
276                                     struct file *filp, char __user *buf,
277                                     size_t count, loff_t *pos,
278                                     size_t eventsz)
279 {
280         struct ib_uverbs_event *event;
281         int ret = 0;
282
283         spin_lock_irq(&ev_queue->lock);
284
285         while (list_empty(&ev_queue->event_list)) {
286                 spin_unlock_irq(&ev_queue->lock);
287
288                 if (filp->f_flags & O_NONBLOCK)
289                         return -EAGAIN;
290
291                 if (wait_event_interruptible(ev_queue->poll_wait,
292                                              (!list_empty(&ev_queue->event_list) ||
293                                               ev_queue->is_closed)))
294                         return -ERESTARTSYS;
295
296                 spin_lock_irq(&ev_queue->lock);
297
298                 /* If device was disassociated and no event exists set an error */
299                 if (list_empty(&ev_queue->event_list) && ev_queue->is_closed) {
300                         spin_unlock_irq(&ev_queue->lock);
301                         return -EIO;
302                 }
303         }
304
305         event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
306
307         if (eventsz > count) {
308                 ret   = -EINVAL;
309                 event = NULL;
310         } else {
311                 list_del(ev_queue->event_list.next);
312                 if (event->counter) {
313                         ++(*event->counter);
314                         list_del(&event->obj_list);
315                 }
316         }
317
318         spin_unlock_irq(&ev_queue->lock);
319
320         if (event) {
321                 if (copy_to_user(buf, event, eventsz))
322                         ret = -EFAULT;
323                 else
324                         ret = eventsz;
325         }
326
327         kfree(event);
328
329         return ret;
330 }
331
332 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
333                                           size_t count, loff_t *pos)
334 {
335         struct ib_uverbs_async_event_file *file = filp->private_data;
336
337         return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,
338                                     sizeof(struct ib_uverbs_async_event_desc));
339 }
340
341 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
342                                          size_t count, loff_t *pos)
343 {
344         struct ib_uverbs_completion_event_file *comp_ev_file =
345                 filp->private_data;
346
347         return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,
348                                     pos,
349                                     sizeof(struct ib_uverbs_comp_event_desc));
350 }
351
352 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
353                                          struct file *filp,
354                                          struct poll_table_struct *wait)
355 {
356         __poll_t pollflags = 0;
357
358         poll_wait(filp, &ev_queue->poll_wait, wait);
359
360         spin_lock_irq(&ev_queue->lock);
361         if (!list_empty(&ev_queue->event_list))
362                 pollflags = EPOLLIN | EPOLLRDNORM;
363         else if (ev_queue->is_closed)
364                 pollflags = EPOLLERR;
365         spin_unlock_irq(&ev_queue->lock);
366
367         return pollflags;
368 }
369
370 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
371                                                struct poll_table_struct *wait)
372 {
373         struct ib_uverbs_async_event_file *file = filp->private_data;
374
375         return ib_uverbs_event_poll(&file->ev_queue, filp, wait);
376 }
377
378 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
379                                               struct poll_table_struct *wait)
380 {
381         struct ib_uverbs_completion_event_file *comp_ev_file =
382                 filp->private_data;
383
384         return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
385 }
386
387 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
388 {
389         struct ib_uverbs_async_event_file *file = filp->private_data;
390
391         return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);
392 }
393
394 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
395 {
396         struct ib_uverbs_completion_event_file *comp_ev_file =
397                 filp->private_data;
398
399         return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
400 }
401
402 static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
403 {
404         struct ib_uverbs_async_event_file *file = filp->private_data;
405         struct ib_uverbs_file *uverbs_file = file->uverbs_file;
406         struct ib_uverbs_event *entry, *tmp;
407         int closed_already = 0;
408
409         mutex_lock(&uverbs_file->device->lists_mutex);
410         spin_lock_irq(&file->ev_queue.lock);
411         closed_already = file->ev_queue.is_closed;
412         file->ev_queue.is_closed = 1;
413         list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
414                 if (entry->counter)
415                         list_del(&entry->obj_list);
416                 kfree(entry);
417         }
418         spin_unlock_irq(&file->ev_queue.lock);
419         if (!closed_already) {
420                 list_del(&file->list);
421                 ib_unregister_event_handler(&uverbs_file->event_handler);
422         }
423         mutex_unlock(&uverbs_file->device->lists_mutex);
424
425         kref_put(&uverbs_file->ref, ib_uverbs_release_file);
426         kref_put(&file->ref, ib_uverbs_release_async_event_file);
427
428         return 0;
429 }
430
431 static int ib_uverbs_comp_event_close(struct inode *inode, struct file *filp)
432 {
433         struct ib_uobject *uobj = filp->private_data;
434         struct ib_uverbs_completion_event_file *file = container_of(
435                 uobj, struct ib_uverbs_completion_event_file, uobj);
436         struct ib_uverbs_event *entry, *tmp;
437
438         spin_lock_irq(&file->ev_queue.lock);
439         list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
440                 if (entry->counter)
441                         list_del(&entry->obj_list);
442                 kfree(entry);
443         }
444         file->ev_queue.is_closed = 1;
445         spin_unlock_irq(&file->ev_queue.lock);
446
447         uverbs_close_fd(filp);
448
449         return 0;
450 }
451
452 const struct file_operations uverbs_event_fops = {
453         .owner   = THIS_MODULE,
454         .read    = ib_uverbs_comp_event_read,
455         .poll    = ib_uverbs_comp_event_poll,
456         .release = ib_uverbs_comp_event_close,
457         .fasync  = ib_uverbs_comp_event_fasync,
458         .llseek  = no_llseek,
459 };
460
461 static const struct file_operations uverbs_async_event_fops = {
462         .owner   = THIS_MODULE,
463         .read    = ib_uverbs_async_event_read,
464         .poll    = ib_uverbs_async_event_poll,
465         .release = ib_uverbs_async_event_close,
466         .fasync  = ib_uverbs_async_event_fasync,
467         .llseek  = no_llseek,
468 };
469
470 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
471 {
472         struct ib_uverbs_event_queue   *ev_queue = cq_context;
473         struct ib_ucq_object           *uobj;
474         struct ib_uverbs_event         *entry;
475         unsigned long                   flags;
476
477         if (!ev_queue)
478                 return;
479
480         spin_lock_irqsave(&ev_queue->lock, flags);
481         if (ev_queue->is_closed) {
482                 spin_unlock_irqrestore(&ev_queue->lock, flags);
483                 return;
484         }
485
486         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
487         if (!entry) {
488                 spin_unlock_irqrestore(&ev_queue->lock, flags);
489                 return;
490         }
491
492         uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
493
494         entry->desc.comp.cq_handle = cq->uobject->user_handle;
495         entry->counter             = &uobj->comp_events_reported;
496
497         list_add_tail(&entry->list, &ev_queue->event_list);
498         list_add_tail(&entry->obj_list, &uobj->comp_list);
499         spin_unlock_irqrestore(&ev_queue->lock, flags);
500
501         wake_up_interruptible(&ev_queue->poll_wait);
502         kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
503 }
504
505 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
506                                     __u64 element, __u64 event,
507                                     struct list_head *obj_list,
508                                     u32 *counter)
509 {
510         struct ib_uverbs_event *entry;
511         unsigned long flags;
512
513         spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
514         if (file->async_file->ev_queue.is_closed) {
515                 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
516                 return;
517         }
518
519         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
520         if (!entry) {
521                 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
522                 return;
523         }
524
525         entry->desc.async.element    = element;
526         entry->desc.async.event_type = event;
527         entry->desc.async.reserved   = 0;
528         entry->counter               = counter;
529
530         list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
531         if (obj_list)
532                 list_add_tail(&entry->obj_list, obj_list);
533         spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
534
535         wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
536         kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
537 }
538
539 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
540 {
541         struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
542                                                   struct ib_ucq_object, uobject);
543
544         ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
545                                 event->event, &uobj->async_list,
546                                 &uobj->async_events_reported);
547 }
548
549 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
550 {
551         struct ib_uevent_object *uobj;
552
553         /* for XRC target qp's, check that qp is live */
554         if (!event->element.qp->uobject)
555                 return;
556
557         uobj = container_of(event->element.qp->uobject,
558                             struct ib_uevent_object, uobject);
559
560         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
561                                 event->event, &uobj->event_list,
562                                 &uobj->events_reported);
563 }
564
565 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
566 {
567         struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
568                                                   struct ib_uevent_object, uobject);
569
570         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
571                                 event->event, &uobj->event_list,
572                                 &uobj->events_reported);
573 }
574
575 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
576 {
577         struct ib_uevent_object *uobj;
578
579         uobj = container_of(event->element.srq->uobject,
580                             struct ib_uevent_object, uobject);
581
582         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
583                                 event->event, &uobj->event_list,
584                                 &uobj->events_reported);
585 }
586
587 void ib_uverbs_event_handler(struct ib_event_handler *handler,
588                              struct ib_event *event)
589 {
590         struct ib_uverbs_file *file =
591                 container_of(handler, struct ib_uverbs_file, event_handler);
592
593         ib_uverbs_async_handler(file, event->element.port_num, event->event,
594                                 NULL, NULL);
595 }
596
597 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
598 {
599         kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
600         file->async_file = NULL;
601 }
602
603 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
604 {
605         spin_lock_init(&ev_queue->lock);
606         INIT_LIST_HEAD(&ev_queue->event_list);
607         init_waitqueue_head(&ev_queue->poll_wait);
608         ev_queue->is_closed   = 0;
609         ev_queue->async_queue = NULL;
610 }
611
612 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
613                                               struct ib_device  *ib_dev)
614 {
615         struct ib_uverbs_async_event_file *ev_file;
616         struct file *filp;
617
618         ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
619         if (!ev_file)
620                 return ERR_PTR(-ENOMEM);
621
622         ib_uverbs_init_event_queue(&ev_file->ev_queue);
623         ev_file->uverbs_file = uverbs_file;
624         kref_get(&ev_file->uverbs_file->ref);
625         kref_init(&ev_file->ref);
626         filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
627                                   ev_file, O_RDONLY);
628         if (IS_ERR(filp))
629                 goto err_put_refs;
630
631         mutex_lock(&uverbs_file->device->lists_mutex);
632         list_add_tail(&ev_file->list,
633                       &uverbs_file->device->uverbs_events_file_list);
634         mutex_unlock(&uverbs_file->device->lists_mutex);
635
636         WARN_ON(uverbs_file->async_file);
637         uverbs_file->async_file = ev_file;
638         kref_get(&uverbs_file->async_file->ref);
639         INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
640                               ib_dev,
641                               ib_uverbs_event_handler);
642         ib_register_event_handler(&uverbs_file->event_handler);
643         /* At that point async file stuff was fully set */
644
645         return filp;
646
647 err_put_refs:
648         kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
649         kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
650         return filp;
651 }
652
653 static bool verify_command_mask(struct ib_uverbs_file *ufile, u32 command,
654                                 bool extended)
655 {
656         if (!extended)
657                 return ufile->uverbs_cmd_mask & BIT_ULL(command);
658
659         return ufile->uverbs_ex_cmd_mask & BIT_ULL(command);
660 }
661
662 static bool verify_command_idx(u32 command, bool extended)
663 {
664         if (extended)
665                 return command < ARRAY_SIZE(uverbs_ex_cmd_table) &&
666                        uverbs_ex_cmd_table[command];
667
668         return command < ARRAY_SIZE(uverbs_cmd_table) &&
669                uverbs_cmd_table[command];
670 }
671
672 static ssize_t process_hdr(struct ib_uverbs_cmd_hdr *hdr,
673                            u32 *command, bool *extended)
674 {
675         if (hdr->command & ~(u32)(IB_USER_VERBS_CMD_FLAG_EXTENDED |
676                                    IB_USER_VERBS_CMD_COMMAND_MASK))
677                 return -EINVAL;
678
679         *command = hdr->command & IB_USER_VERBS_CMD_COMMAND_MASK;
680         *extended = hdr->command & IB_USER_VERBS_CMD_FLAG_EXTENDED;
681
682         if (!verify_command_idx(*command, *extended))
683                 return -EOPNOTSUPP;
684
685         return 0;
686 }
687
688 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
689                           struct ib_uverbs_ex_cmd_hdr *ex_hdr,
690                           size_t count, bool extended)
691 {
692         if (extended) {
693                 count -= sizeof(*hdr) + sizeof(*ex_hdr);
694
695                 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
696                         return -EINVAL;
697
698                 if (ex_hdr->cmd_hdr_reserved)
699                         return -EINVAL;
700
701                 if (ex_hdr->response) {
702                         if (!hdr->out_words && !ex_hdr->provider_out_words)
703                                 return -EINVAL;
704
705                         if (!access_ok(VERIFY_WRITE,
706                                        u64_to_user_ptr(ex_hdr->response),
707                                        (hdr->out_words + ex_hdr->provider_out_words) * 8))
708                                 return -EFAULT;
709                 } else {
710                         if (hdr->out_words || ex_hdr->provider_out_words)
711                                 return -EINVAL;
712                 }
713
714                 return 0;
715         }
716
717         /* not extended command */
718         if (hdr->in_words * 4 != count)
719                 return -EINVAL;
720
721         return 0;
722 }
723
724 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
725                              size_t count, loff_t *pos)
726 {
727         struct ib_uverbs_file *file = filp->private_data;
728         struct ib_uverbs_ex_cmd_hdr ex_hdr;
729         struct ib_uverbs_cmd_hdr hdr;
730         bool extended;
731         int srcu_key;
732         u32 command;
733         ssize_t ret;
734
735         if (!ib_safe_file_access(filp)) {
736                 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
737                             task_tgid_vnr(current), current->comm);
738                 return -EACCES;
739         }
740
741         if (count < sizeof(hdr))
742                 return -EINVAL;
743
744         if (copy_from_user(&hdr, buf, sizeof(hdr)))
745                 return -EFAULT;
746
747         ret = process_hdr(&hdr, &command, &extended);
748         if (ret)
749                 return ret;
750
751         if (extended) {
752                 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
753                         return -EINVAL;
754                 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
755                         return -EFAULT;
756         }
757
758         ret = verify_hdr(&hdr, &ex_hdr, count, extended);
759         if (ret)
760                 return ret;
761
762         srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
763
764         if (!verify_command_mask(file, command, extended)) {
765                 ret = -EOPNOTSUPP;
766                 goto out;
767         }
768
769         buf += sizeof(hdr);
770
771         if (!extended) {
772                 ret = uverbs_cmd_table[command](file, buf,
773                                                 hdr.in_words * 4,
774                                                 hdr.out_words * 4);
775         } else {
776                 struct ib_udata ucore;
777                 struct ib_udata uhw;
778
779                 buf += sizeof(ex_hdr);
780
781                 ib_uverbs_init_udata_buf_or_null(&ucore, buf,
782                                         u64_to_user_ptr(ex_hdr.response),
783                                         hdr.in_words * 8, hdr.out_words * 8);
784
785                 ib_uverbs_init_udata_buf_or_null(&uhw,
786                                         buf + ucore.inlen,
787                                         u64_to_user_ptr(ex_hdr.response) + ucore.outlen,
788                                         ex_hdr.provider_in_words * 8,
789                                         ex_hdr.provider_out_words * 8);
790
791                 ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw);
792                 ret = (ret) ? : count;
793         }
794
795 out:
796         srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
797         return ret;
798 }
799
800 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
801 {
802         struct ib_uverbs_file *file = filp->private_data;
803         struct ib_ucontext *ucontext;
804         int ret = 0;
805         int srcu_key;
806
807         srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
808         ucontext = ib_uverbs_get_ucontext(file);
809         if (IS_ERR(ucontext)) {
810                 ret = PTR_ERR(ucontext);
811                 goto out;
812         }
813
814         ret = ucontext->device->mmap(ucontext, vma);
815 out:
816         srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
817         return ret;
818 }
819
820 /*
821  * ib_uverbs_open() does not need the BKL:
822  *
823  *  - the ib_uverbs_device structures are properly reference counted and
824  *    everything else is purely local to the file being created, so
825  *    races against other open calls are not a problem;
826  *  - there is no ioctl method to race against;
827  *  - the open method will either immediately run -ENXIO, or all
828  *    required initialization will be done.
829  */
830 static int ib_uverbs_open(struct inode *inode, struct file *filp)
831 {
832         struct ib_uverbs_device *dev;
833         struct ib_uverbs_file *file;
834         struct ib_device *ib_dev;
835         int ret;
836         int module_dependent;
837         int srcu_key;
838
839         dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
840         if (!atomic_inc_not_zero(&dev->refcount))
841                 return -ENXIO;
842
843         srcu_key = srcu_read_lock(&dev->disassociate_srcu);
844         mutex_lock(&dev->lists_mutex);
845         ib_dev = srcu_dereference(dev->ib_dev,
846                                   &dev->disassociate_srcu);
847         if (!ib_dev) {
848                 ret = -EIO;
849                 goto err;
850         }
851
852         /* In case IB device supports disassociate ucontext, there is no hard
853          * dependency between uverbs device and its low level device.
854          */
855         module_dependent = !(ib_dev->disassociate_ucontext);
856
857         if (module_dependent) {
858                 if (!try_module_get(ib_dev->owner)) {
859                         ret = -ENODEV;
860                         goto err;
861                 }
862         }
863
864         file = kzalloc(sizeof(*file), GFP_KERNEL);
865         if (!file) {
866                 ret = -ENOMEM;
867                 if (module_dependent)
868                         goto err_module;
869
870                 goto err;
871         }
872
873         file->device     = dev;
874         kref_init(&file->ref);
875         mutex_init(&file->ucontext_lock);
876
877         spin_lock_init(&file->uobjects_lock);
878         INIT_LIST_HEAD(&file->uobjects);
879         init_rwsem(&file->hw_destroy_rwsem);
880
881         filp->private_data = file;
882         kobject_get(&dev->kobj);
883         list_add_tail(&file->list, &dev->uverbs_file_list);
884         mutex_unlock(&dev->lists_mutex);
885         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
886
887         file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask;
888         file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask;
889
890         setup_ufile_idr_uobject(file);
891
892         return nonseekable_open(inode, filp);
893
894 err_module:
895         module_put(ib_dev->owner);
896
897 err:
898         mutex_unlock(&dev->lists_mutex);
899         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
900         if (atomic_dec_and_test(&dev->refcount))
901                 ib_uverbs_comp_dev(dev);
902
903         return ret;
904 }
905
906 static int ib_uverbs_close(struct inode *inode, struct file *filp)
907 {
908         struct ib_uverbs_file *file = filp->private_data;
909
910         uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
911
912         mutex_lock(&file->device->lists_mutex);
913         if (!file->is_closed) {
914                 list_del(&file->list);
915                 file->is_closed = 1;
916         }
917         mutex_unlock(&file->device->lists_mutex);
918
919         kref_put(&file->ref, ib_uverbs_release_file);
920
921         return 0;
922 }
923
924 static const struct file_operations uverbs_fops = {
925         .owner   = THIS_MODULE,
926         .write   = ib_uverbs_write,
927         .open    = ib_uverbs_open,
928         .release = ib_uverbs_close,
929         .llseek  = no_llseek,
930         .unlocked_ioctl = ib_uverbs_ioctl,
931         .compat_ioctl = ib_uverbs_ioctl,
932 };
933
934 static const struct file_operations uverbs_mmap_fops = {
935         .owner   = THIS_MODULE,
936         .write   = ib_uverbs_write,
937         .mmap    = ib_uverbs_mmap,
938         .open    = ib_uverbs_open,
939         .release = ib_uverbs_close,
940         .llseek  = no_llseek,
941         .unlocked_ioctl = ib_uverbs_ioctl,
942         .compat_ioctl = ib_uverbs_ioctl,
943 };
944
945 static struct ib_client uverbs_client = {
946         .name   = "uverbs",
947         .add    = ib_uverbs_add_one,
948         .remove = ib_uverbs_remove_one
949 };
950
951 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
952                           char *buf)
953 {
954         int ret = -ENODEV;
955         int srcu_key;
956         struct ib_uverbs_device *dev = dev_get_drvdata(device);
957         struct ib_device *ib_dev;
958
959         if (!dev)
960                 return -ENODEV;
961
962         srcu_key = srcu_read_lock(&dev->disassociate_srcu);
963         ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
964         if (ib_dev)
965                 ret = sprintf(buf, "%s\n", ib_dev->name);
966         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
967
968         return ret;
969 }
970 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
971
972 static ssize_t show_dev_abi_version(struct device *device,
973                                     struct device_attribute *attr, char *buf)
974 {
975         struct ib_uverbs_device *dev = dev_get_drvdata(device);
976         int ret = -ENODEV;
977         int srcu_key;
978         struct ib_device *ib_dev;
979
980         if (!dev)
981                 return -ENODEV;
982         srcu_key = srcu_read_lock(&dev->disassociate_srcu);
983         ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
984         if (ib_dev)
985                 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
986         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
987
988         return ret;
989 }
990 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
991
992 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
993                          __stringify(IB_USER_VERBS_ABI_VERSION));
994
995 static int ib_uverbs_create_uapi(struct ib_device *device,
996                                  struct ib_uverbs_device *uverbs_dev)
997 {
998         struct uverbs_api *uapi;
999
1000         uapi = uverbs_alloc_api(device->driver_specs, device->driver_id);
1001         if (IS_ERR(uapi))
1002                 return PTR_ERR(uapi);
1003
1004         uverbs_dev->uapi = uapi;
1005         return 0;
1006 }
1007
1008 static void ib_uverbs_add_one(struct ib_device *device)
1009 {
1010         int devnum;
1011         dev_t base;
1012         struct ib_uverbs_device *uverbs_dev;
1013         int ret;
1014
1015         if (!device->alloc_ucontext)
1016                 return;
1017
1018         uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1019         if (!uverbs_dev)
1020                 return;
1021
1022         ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1023         if (ret) {
1024                 kfree(uverbs_dev);
1025                 return;
1026         }
1027
1028         atomic_set(&uverbs_dev->refcount, 1);
1029         init_completion(&uverbs_dev->comp);
1030         uverbs_dev->xrcd_tree = RB_ROOT;
1031         mutex_init(&uverbs_dev->xrcd_tree_mutex);
1032         kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
1033         mutex_init(&uverbs_dev->lists_mutex);
1034         INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1035         INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1036
1037         devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1038         if (devnum >= IB_UVERBS_MAX_DEVICES)
1039                 goto err;
1040         uverbs_dev->devnum = devnum;
1041         set_bit(devnum, dev_map);
1042         if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1043                 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1044         else
1045                 base = IB_UVERBS_BASE_DEV + devnum;
1046
1047         rcu_assign_pointer(uverbs_dev->ib_dev, device);
1048         uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1049
1050         if (ib_uverbs_create_uapi(device, uverbs_dev))
1051                 goto err_uapi;
1052
1053         cdev_init(&uverbs_dev->cdev, NULL);
1054         uverbs_dev->cdev.owner = THIS_MODULE;
1055         uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1056         cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj);
1057         kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1058         if (cdev_add(&uverbs_dev->cdev, base, 1))
1059                 goto err_cdev;
1060
1061         uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1062                                         uverbs_dev->cdev.dev, uverbs_dev,
1063                                         "uverbs%d", uverbs_dev->devnum);
1064         if (IS_ERR(uverbs_dev->dev))
1065                 goto err_cdev;
1066
1067         if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1068                 goto err_class;
1069         if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1070                 goto err_class;
1071
1072         ib_set_client_data(device, &uverbs_client, uverbs_dev);
1073
1074         return;
1075
1076 err_class:
1077         device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1078 err_cdev:
1079         cdev_del(&uverbs_dev->cdev);
1080 err_uapi:
1081         clear_bit(devnum, dev_map);
1082 err:
1083         if (atomic_dec_and_test(&uverbs_dev->refcount))
1084                 ib_uverbs_comp_dev(uverbs_dev);
1085         wait_for_completion(&uverbs_dev->comp);
1086         kobject_put(&uverbs_dev->kobj);
1087         return;
1088 }
1089
1090 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1091                                         struct ib_device *ib_dev)
1092 {
1093         struct ib_uverbs_file *file;
1094         struct ib_uverbs_async_event_file *event_file;
1095         struct ib_event event;
1096
1097         /* Pending running commands to terminate */
1098         uverbs_disassociate_api_pre(uverbs_dev);
1099         event.event = IB_EVENT_DEVICE_FATAL;
1100         event.element.port_num = 0;
1101         event.device = ib_dev;
1102
1103         mutex_lock(&uverbs_dev->lists_mutex);
1104         while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1105                 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1106                                         struct ib_uverbs_file, list);
1107                 file->is_closed = 1;
1108                 list_del(&file->list);
1109                 kref_get(&file->ref);
1110
1111                 /* We must release the mutex before going ahead and calling
1112                  * uverbs_cleanup_ufile, as it might end up indirectly calling
1113                  * uverbs_close, for example due to freeing the resources (e.g
1114                  * mmput).
1115                  */
1116                 mutex_unlock(&uverbs_dev->lists_mutex);
1117
1118                 ib_uverbs_event_handler(&file->event_handler, &event);
1119                 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1120                 kref_put(&file->ref, ib_uverbs_release_file);
1121
1122                 mutex_lock(&uverbs_dev->lists_mutex);
1123         }
1124
1125         while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1126                 event_file = list_first_entry(&uverbs_dev->
1127                                               uverbs_events_file_list,
1128                                               struct ib_uverbs_async_event_file,
1129                                               list);
1130                 spin_lock_irq(&event_file->ev_queue.lock);
1131                 event_file->ev_queue.is_closed = 1;
1132                 spin_unlock_irq(&event_file->ev_queue.lock);
1133
1134                 list_del(&event_file->list);
1135                 ib_unregister_event_handler(
1136                         &event_file->uverbs_file->event_handler);
1137                 event_file->uverbs_file->event_handler.device =
1138                         NULL;
1139
1140                 wake_up_interruptible(&event_file->ev_queue.poll_wait);
1141                 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1142         }
1143         mutex_unlock(&uverbs_dev->lists_mutex);
1144
1145         uverbs_disassociate_api(uverbs_dev->uapi);
1146 }
1147
1148 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1149 {
1150         struct ib_uverbs_device *uverbs_dev = client_data;
1151         int wait_clients = 1;
1152
1153         if (!uverbs_dev)
1154                 return;
1155
1156         dev_set_drvdata(uverbs_dev->dev, NULL);
1157         device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1158         cdev_del(&uverbs_dev->cdev);
1159         clear_bit(uverbs_dev->devnum, dev_map);
1160
1161         if (device->disassociate_ucontext) {
1162                 /* We disassociate HW resources and immediately return.
1163                  * Userspace will see a EIO errno for all future access.
1164                  * Upon returning, ib_device may be freed internally and is not
1165                  * valid any more.
1166                  * uverbs_device is still available until all clients close
1167                  * their files, then the uverbs device ref count will be zero
1168                  * and its resources will be freed.
1169                  * Note: At this point no more files can be opened since the
1170                  * cdev was deleted, however active clients can still issue
1171                  * commands and close their open files.
1172                  */
1173                 ib_uverbs_free_hw_resources(uverbs_dev, device);
1174                 wait_clients = 0;
1175         }
1176
1177         if (atomic_dec_and_test(&uverbs_dev->refcount))
1178                 ib_uverbs_comp_dev(uverbs_dev);
1179         if (wait_clients)
1180                 wait_for_completion(&uverbs_dev->comp);
1181
1182         kobject_put(&uverbs_dev->kobj);
1183 }
1184
1185 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1186 {
1187         if (mode)
1188                 *mode = 0666;
1189         return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1190 }
1191
1192 static int __init ib_uverbs_init(void)
1193 {
1194         int ret;
1195
1196         ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1197                                      IB_UVERBS_NUM_FIXED_MINOR,
1198                                      "infiniband_verbs");
1199         if (ret) {
1200                 pr_err("user_verbs: couldn't register device number\n");
1201                 goto out;
1202         }
1203
1204         ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1205                                   IB_UVERBS_NUM_DYNAMIC_MINOR,
1206                                   "infiniband_verbs");
1207         if (ret) {
1208                 pr_err("couldn't register dynamic device number\n");
1209                 goto out_alloc;
1210         }
1211
1212         uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1213         if (IS_ERR(uverbs_class)) {
1214                 ret = PTR_ERR(uverbs_class);
1215                 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1216                 goto out_chrdev;
1217         }
1218
1219         uverbs_class->devnode = uverbs_devnode;
1220
1221         ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1222         if (ret) {
1223                 pr_err("user_verbs: couldn't create abi_version attribute\n");
1224                 goto out_class;
1225         }
1226
1227         ret = ib_register_client(&uverbs_client);
1228         if (ret) {
1229                 pr_err("user_verbs: couldn't register client\n");
1230                 goto out_class;
1231         }
1232
1233         return 0;
1234
1235 out_class:
1236         class_destroy(uverbs_class);
1237
1238 out_chrdev:
1239         unregister_chrdev_region(dynamic_uverbs_dev,
1240                                  IB_UVERBS_NUM_DYNAMIC_MINOR);
1241
1242 out_alloc:
1243         unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1244                                  IB_UVERBS_NUM_FIXED_MINOR);
1245
1246 out:
1247         return ret;
1248 }
1249
1250 static void __exit ib_uverbs_cleanup(void)
1251 {
1252         ib_unregister_client(&uverbs_client);
1253         class_destroy(uverbs_class);
1254         unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1255                                  IB_UVERBS_NUM_FIXED_MINOR);
1256         unregister_chrdev_region(dynamic_uverbs_dev,
1257                                  IB_UVERBS_NUM_DYNAMIC_MINOR);
1258 }
1259
1260 module_init(ib_uverbs_init);
1261 module_exit(ib_uverbs_cleanup);