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