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