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