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