2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
4 * Copyright (c) 2005 PathScale, Inc. All rights reserved.
5 * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/file.h>
38 #include <linux/slab.h>
40 #include <asm/uaccess.h>
44 struct uverbs_lock_class
{
45 struct lock_class_key key
;
49 static struct uverbs_lock_class pd_lock_class
= { .name
= "PD-uobj" };
50 static struct uverbs_lock_class mr_lock_class
= { .name
= "MR-uobj" };
51 static struct uverbs_lock_class mw_lock_class
= { .name
= "MW-uobj" };
52 static struct uverbs_lock_class cq_lock_class
= { .name
= "CQ-uobj" };
53 static struct uverbs_lock_class qp_lock_class
= { .name
= "QP-uobj" };
54 static struct uverbs_lock_class ah_lock_class
= { .name
= "AH-uobj" };
55 static struct uverbs_lock_class srq_lock_class
= { .name
= "SRQ-uobj" };
56 static struct uverbs_lock_class xrcd_lock_class
= { .name
= "XRCD-uobj" };
57 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING
58 static struct uverbs_lock_class rule_lock_class
= { .name
= "RULE-uobj" };
59 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
61 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
63 (udata)->inbuf = (void __user *) (ibuf); \
64 (udata)->outbuf = (void __user *) (obuf); \
65 (udata)->inlen = (ilen); \
66 (udata)->outlen = (olen); \
70 * The ib_uobject locking scheme is as follows:
72 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
73 * needs to be held during all idr operations. When an object is
74 * looked up, a reference must be taken on the object's kref before
77 * - Each object also has an rwsem. This rwsem must be held for
78 * reading while an operation that uses the object is performed.
79 * For example, while registering an MR, the associated PD's
80 * uobject.mutex must be held for reading. The rwsem must be held
81 * for writing while initializing or destroying an object.
83 * - In addition, each object has a "live" flag. If this flag is not
84 * set, then lookups of the object will fail even if it is found in
85 * the idr. This handles a reader that blocks and does not acquire
86 * the rwsem until after the object is destroyed. The destroy
87 * operation will set the live flag to 0 and then drop the rwsem;
88 * this will allow the reader to acquire the rwsem, see that the
89 * live flag is 0, and then drop the rwsem and its reference to
90 * object. The underlying storage will not be freed until the last
91 * reference to the object is dropped.
94 static void init_uobj(struct ib_uobject
*uobj
, u64 user_handle
,
95 struct ib_ucontext
*context
, struct uverbs_lock_class
*c
)
97 uobj
->user_handle
= user_handle
;
98 uobj
->context
= context
;
99 kref_init(&uobj
->ref
);
100 init_rwsem(&uobj
->mutex
);
101 lockdep_set_class_and_name(&uobj
->mutex
, &c
->key
, c
->name
);
105 static void release_uobj(struct kref
*kref
)
107 kfree(container_of(kref
, struct ib_uobject
, ref
));
110 static void put_uobj(struct ib_uobject
*uobj
)
112 kref_put(&uobj
->ref
, release_uobj
);
115 static void put_uobj_read(struct ib_uobject
*uobj
)
117 up_read(&uobj
->mutex
);
121 static void put_uobj_write(struct ib_uobject
*uobj
)
123 up_write(&uobj
->mutex
);
127 static int idr_add_uobj(struct idr
*idr
, struct ib_uobject
*uobj
)
131 idr_preload(GFP_KERNEL
);
132 spin_lock(&ib_uverbs_idr_lock
);
134 ret
= idr_alloc(idr
, uobj
, 0, 0, GFP_NOWAIT
);
138 spin_unlock(&ib_uverbs_idr_lock
);
141 return ret
< 0 ? ret
: 0;
144 void idr_remove_uobj(struct idr
*idr
, struct ib_uobject
*uobj
)
146 spin_lock(&ib_uverbs_idr_lock
);
147 idr_remove(idr
, uobj
->id
);
148 spin_unlock(&ib_uverbs_idr_lock
);
151 static struct ib_uobject
*__idr_get_uobj(struct idr
*idr
, int id
,
152 struct ib_ucontext
*context
)
154 struct ib_uobject
*uobj
;
156 spin_lock(&ib_uverbs_idr_lock
);
157 uobj
= idr_find(idr
, id
);
159 if (uobj
->context
== context
)
160 kref_get(&uobj
->ref
);
164 spin_unlock(&ib_uverbs_idr_lock
);
169 static struct ib_uobject
*idr_read_uobj(struct idr
*idr
, int id
,
170 struct ib_ucontext
*context
, int nested
)
172 struct ib_uobject
*uobj
;
174 uobj
= __idr_get_uobj(idr
, id
, context
);
179 down_read_nested(&uobj
->mutex
, SINGLE_DEPTH_NESTING
);
181 down_read(&uobj
->mutex
);
190 static struct ib_uobject
*idr_write_uobj(struct idr
*idr
, int id
,
191 struct ib_ucontext
*context
)
193 struct ib_uobject
*uobj
;
195 uobj
= __idr_get_uobj(idr
, id
, context
);
199 down_write(&uobj
->mutex
);
201 put_uobj_write(uobj
);
208 static void *idr_read_obj(struct idr
*idr
, int id
, struct ib_ucontext
*context
,
211 struct ib_uobject
*uobj
;
213 uobj
= idr_read_uobj(idr
, id
, context
, nested
);
214 return uobj
? uobj
->object
: NULL
;
217 static struct ib_pd
*idr_read_pd(int pd_handle
, struct ib_ucontext
*context
)
219 return idr_read_obj(&ib_uverbs_pd_idr
, pd_handle
, context
, 0);
222 static void put_pd_read(struct ib_pd
*pd
)
224 put_uobj_read(pd
->uobject
);
227 static struct ib_cq
*idr_read_cq(int cq_handle
, struct ib_ucontext
*context
, int nested
)
229 return idr_read_obj(&ib_uverbs_cq_idr
, cq_handle
, context
, nested
);
232 static void put_cq_read(struct ib_cq
*cq
)
234 put_uobj_read(cq
->uobject
);
237 static struct ib_ah
*idr_read_ah(int ah_handle
, struct ib_ucontext
*context
)
239 return idr_read_obj(&ib_uverbs_ah_idr
, ah_handle
, context
, 0);
242 static void put_ah_read(struct ib_ah
*ah
)
244 put_uobj_read(ah
->uobject
);
247 static struct ib_qp
*idr_read_qp(int qp_handle
, struct ib_ucontext
*context
)
249 return idr_read_obj(&ib_uverbs_qp_idr
, qp_handle
, context
, 0);
252 static struct ib_qp
*idr_write_qp(int qp_handle
, struct ib_ucontext
*context
)
254 struct ib_uobject
*uobj
;
256 uobj
= idr_write_uobj(&ib_uverbs_qp_idr
, qp_handle
, context
);
257 return uobj
? uobj
->object
: NULL
;
260 static void put_qp_read(struct ib_qp
*qp
)
262 put_uobj_read(qp
->uobject
);
265 static void put_qp_write(struct ib_qp
*qp
)
267 put_uobj_write(qp
->uobject
);
270 static struct ib_srq
*idr_read_srq(int srq_handle
, struct ib_ucontext
*context
)
272 return idr_read_obj(&ib_uverbs_srq_idr
, srq_handle
, context
, 0);
275 static void put_srq_read(struct ib_srq
*srq
)
277 put_uobj_read(srq
->uobject
);
280 static struct ib_xrcd
*idr_read_xrcd(int xrcd_handle
, struct ib_ucontext
*context
,
281 struct ib_uobject
**uobj
)
283 *uobj
= idr_read_uobj(&ib_uverbs_xrcd_idr
, xrcd_handle
, context
, 0);
284 return *uobj
? (*uobj
)->object
: NULL
;
287 static void put_xrcd_read(struct ib_uobject
*uobj
)
292 ssize_t
ib_uverbs_get_context(struct ib_uverbs_file
*file
,
293 const char __user
*buf
,
294 int in_len
, int out_len
)
296 struct ib_uverbs_get_context cmd
;
297 struct ib_uverbs_get_context_resp resp
;
298 struct ib_udata udata
;
299 struct ib_device
*ibdev
= file
->device
->ib_dev
;
300 struct ib_ucontext
*ucontext
;
304 if (out_len
< sizeof resp
)
307 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
310 mutex_lock(&file
->mutex
);
312 if (file
->ucontext
) {
317 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
318 (unsigned long) cmd
.response
+ sizeof resp
,
319 in_len
- sizeof cmd
, out_len
- sizeof resp
);
321 ucontext
= ibdev
->alloc_ucontext(ibdev
, &udata
);
322 if (IS_ERR(ucontext
)) {
323 ret
= PTR_ERR(ucontext
);
327 ucontext
->device
= ibdev
;
328 INIT_LIST_HEAD(&ucontext
->pd_list
);
329 INIT_LIST_HEAD(&ucontext
->mr_list
);
330 INIT_LIST_HEAD(&ucontext
->mw_list
);
331 INIT_LIST_HEAD(&ucontext
->cq_list
);
332 INIT_LIST_HEAD(&ucontext
->qp_list
);
333 INIT_LIST_HEAD(&ucontext
->srq_list
);
334 INIT_LIST_HEAD(&ucontext
->ah_list
);
335 INIT_LIST_HEAD(&ucontext
->xrcd_list
);
336 INIT_LIST_HEAD(&ucontext
->rule_list
);
337 ucontext
->closing
= 0;
339 resp
.num_comp_vectors
= file
->device
->num_comp_vectors
;
341 ret
= get_unused_fd_flags(O_CLOEXEC
);
346 filp
= ib_uverbs_alloc_event_file(file
, 1);
352 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
353 &resp
, sizeof resp
)) {
358 file
->async_file
= filp
->private_data
;
360 INIT_IB_EVENT_HANDLER(&file
->event_handler
, file
->device
->ib_dev
,
361 ib_uverbs_event_handler
);
362 ret
= ib_register_event_handler(&file
->event_handler
);
366 kref_get(&file
->async_file
->ref
);
367 kref_get(&file
->ref
);
368 file
->ucontext
= ucontext
;
370 fd_install(resp
.async_fd
, filp
);
372 mutex_unlock(&file
->mutex
);
380 put_unused_fd(resp
.async_fd
);
383 ibdev
->dealloc_ucontext(ucontext
);
386 mutex_unlock(&file
->mutex
);
390 ssize_t
ib_uverbs_query_device(struct ib_uverbs_file
*file
,
391 const char __user
*buf
,
392 int in_len
, int out_len
)
394 struct ib_uverbs_query_device cmd
;
395 struct ib_uverbs_query_device_resp resp
;
396 struct ib_device_attr attr
;
399 if (out_len
< sizeof resp
)
402 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
405 ret
= ib_query_device(file
->device
->ib_dev
, &attr
);
409 memset(&resp
, 0, sizeof resp
);
411 resp
.fw_ver
= attr
.fw_ver
;
412 resp
.node_guid
= file
->device
->ib_dev
->node_guid
;
413 resp
.sys_image_guid
= attr
.sys_image_guid
;
414 resp
.max_mr_size
= attr
.max_mr_size
;
415 resp
.page_size_cap
= attr
.page_size_cap
;
416 resp
.vendor_id
= attr
.vendor_id
;
417 resp
.vendor_part_id
= attr
.vendor_part_id
;
418 resp
.hw_ver
= attr
.hw_ver
;
419 resp
.max_qp
= attr
.max_qp
;
420 resp
.max_qp_wr
= attr
.max_qp_wr
;
421 resp
.device_cap_flags
= attr
.device_cap_flags
;
422 resp
.max_sge
= attr
.max_sge
;
423 resp
.max_sge_rd
= attr
.max_sge_rd
;
424 resp
.max_cq
= attr
.max_cq
;
425 resp
.max_cqe
= attr
.max_cqe
;
426 resp
.max_mr
= attr
.max_mr
;
427 resp
.max_pd
= attr
.max_pd
;
428 resp
.max_qp_rd_atom
= attr
.max_qp_rd_atom
;
429 resp
.max_ee_rd_atom
= attr
.max_ee_rd_atom
;
430 resp
.max_res_rd_atom
= attr
.max_res_rd_atom
;
431 resp
.max_qp_init_rd_atom
= attr
.max_qp_init_rd_atom
;
432 resp
.max_ee_init_rd_atom
= attr
.max_ee_init_rd_atom
;
433 resp
.atomic_cap
= attr
.atomic_cap
;
434 resp
.max_ee
= attr
.max_ee
;
435 resp
.max_rdd
= attr
.max_rdd
;
436 resp
.max_mw
= attr
.max_mw
;
437 resp
.max_raw_ipv6_qp
= attr
.max_raw_ipv6_qp
;
438 resp
.max_raw_ethy_qp
= attr
.max_raw_ethy_qp
;
439 resp
.max_mcast_grp
= attr
.max_mcast_grp
;
440 resp
.max_mcast_qp_attach
= attr
.max_mcast_qp_attach
;
441 resp
.max_total_mcast_qp_attach
= attr
.max_total_mcast_qp_attach
;
442 resp
.max_ah
= attr
.max_ah
;
443 resp
.max_fmr
= attr
.max_fmr
;
444 resp
.max_map_per_fmr
= attr
.max_map_per_fmr
;
445 resp
.max_srq
= attr
.max_srq
;
446 resp
.max_srq_wr
= attr
.max_srq_wr
;
447 resp
.max_srq_sge
= attr
.max_srq_sge
;
448 resp
.max_pkeys
= attr
.max_pkeys
;
449 resp
.local_ca_ack_delay
= attr
.local_ca_ack_delay
;
450 resp
.phys_port_cnt
= file
->device
->ib_dev
->phys_port_cnt
;
452 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
459 ssize_t
ib_uverbs_query_port(struct ib_uverbs_file
*file
,
460 const char __user
*buf
,
461 int in_len
, int out_len
)
463 struct ib_uverbs_query_port cmd
;
464 struct ib_uverbs_query_port_resp resp
;
465 struct ib_port_attr attr
;
468 if (out_len
< sizeof resp
)
471 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
474 ret
= ib_query_port(file
->device
->ib_dev
, cmd
.port_num
, &attr
);
478 memset(&resp
, 0, sizeof resp
);
480 resp
.state
= attr
.state
;
481 resp
.max_mtu
= attr
.max_mtu
;
482 resp
.active_mtu
= attr
.active_mtu
;
483 resp
.gid_tbl_len
= attr
.gid_tbl_len
;
484 resp
.port_cap_flags
= attr
.port_cap_flags
;
485 resp
.max_msg_sz
= attr
.max_msg_sz
;
486 resp
.bad_pkey_cntr
= attr
.bad_pkey_cntr
;
487 resp
.qkey_viol_cntr
= attr
.qkey_viol_cntr
;
488 resp
.pkey_tbl_len
= attr
.pkey_tbl_len
;
490 resp
.sm_lid
= attr
.sm_lid
;
492 resp
.max_vl_num
= attr
.max_vl_num
;
493 resp
.sm_sl
= attr
.sm_sl
;
494 resp
.subnet_timeout
= attr
.subnet_timeout
;
495 resp
.init_type_reply
= attr
.init_type_reply
;
496 resp
.active_width
= attr
.active_width
;
497 resp
.active_speed
= attr
.active_speed
;
498 resp
.phys_state
= attr
.phys_state
;
499 resp
.link_layer
= rdma_port_get_link_layer(file
->device
->ib_dev
,
502 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
509 ssize_t
ib_uverbs_alloc_pd(struct ib_uverbs_file
*file
,
510 const char __user
*buf
,
511 int in_len
, int out_len
)
513 struct ib_uverbs_alloc_pd cmd
;
514 struct ib_uverbs_alloc_pd_resp resp
;
515 struct ib_udata udata
;
516 struct ib_uobject
*uobj
;
520 if (out_len
< sizeof resp
)
523 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
526 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
527 (unsigned long) cmd
.response
+ sizeof resp
,
528 in_len
- sizeof cmd
, out_len
- sizeof resp
);
530 uobj
= kmalloc(sizeof *uobj
, GFP_KERNEL
);
534 init_uobj(uobj
, 0, file
->ucontext
, &pd_lock_class
);
535 down_write(&uobj
->mutex
);
537 pd
= file
->device
->ib_dev
->alloc_pd(file
->device
->ib_dev
,
538 file
->ucontext
, &udata
);
544 pd
->device
= file
->device
->ib_dev
;
546 atomic_set(&pd
->usecnt
, 0);
549 ret
= idr_add_uobj(&ib_uverbs_pd_idr
, uobj
);
553 memset(&resp
, 0, sizeof resp
);
554 resp
.pd_handle
= uobj
->id
;
556 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
557 &resp
, sizeof resp
)) {
562 mutex_lock(&file
->mutex
);
563 list_add_tail(&uobj
->list
, &file
->ucontext
->pd_list
);
564 mutex_unlock(&file
->mutex
);
568 up_write(&uobj
->mutex
);
573 idr_remove_uobj(&ib_uverbs_pd_idr
, uobj
);
579 put_uobj_write(uobj
);
583 ssize_t
ib_uverbs_dealloc_pd(struct ib_uverbs_file
*file
,
584 const char __user
*buf
,
585 int in_len
, int out_len
)
587 struct ib_uverbs_dealloc_pd cmd
;
588 struct ib_uobject
*uobj
;
591 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
594 uobj
= idr_write_uobj(&ib_uverbs_pd_idr
, cmd
.pd_handle
, file
->ucontext
);
598 ret
= ib_dealloc_pd(uobj
->object
);
602 put_uobj_write(uobj
);
607 idr_remove_uobj(&ib_uverbs_pd_idr
, uobj
);
609 mutex_lock(&file
->mutex
);
610 list_del(&uobj
->list
);
611 mutex_unlock(&file
->mutex
);
618 struct xrcd_table_entry
{
620 struct ib_xrcd
*xrcd
;
624 static int xrcd_table_insert(struct ib_uverbs_device
*dev
,
626 struct ib_xrcd
*xrcd
)
628 struct xrcd_table_entry
*entry
, *scan
;
629 struct rb_node
**p
= &dev
->xrcd_tree
.rb_node
;
630 struct rb_node
*parent
= NULL
;
632 entry
= kmalloc(sizeof *entry
, GFP_KERNEL
);
637 entry
->inode
= inode
;
641 scan
= rb_entry(parent
, struct xrcd_table_entry
, node
);
643 if (inode
< scan
->inode
) {
645 } else if (inode
> scan
->inode
) {
653 rb_link_node(&entry
->node
, parent
, p
);
654 rb_insert_color(&entry
->node
, &dev
->xrcd_tree
);
659 static struct xrcd_table_entry
*xrcd_table_search(struct ib_uverbs_device
*dev
,
662 struct xrcd_table_entry
*entry
;
663 struct rb_node
*p
= dev
->xrcd_tree
.rb_node
;
666 entry
= rb_entry(p
, struct xrcd_table_entry
, node
);
668 if (inode
< entry
->inode
)
670 else if (inode
> entry
->inode
)
679 static struct ib_xrcd
*find_xrcd(struct ib_uverbs_device
*dev
, struct inode
*inode
)
681 struct xrcd_table_entry
*entry
;
683 entry
= xrcd_table_search(dev
, inode
);
690 static void xrcd_table_delete(struct ib_uverbs_device
*dev
,
693 struct xrcd_table_entry
*entry
;
695 entry
= xrcd_table_search(dev
, inode
);
698 rb_erase(&entry
->node
, &dev
->xrcd_tree
);
703 ssize_t
ib_uverbs_open_xrcd(struct ib_uverbs_file
*file
,
704 const char __user
*buf
, int in_len
,
707 struct ib_uverbs_open_xrcd cmd
;
708 struct ib_uverbs_open_xrcd_resp resp
;
709 struct ib_udata udata
;
710 struct ib_uxrcd_object
*obj
;
711 struct ib_xrcd
*xrcd
= NULL
;
712 struct fd f
= {NULL
, 0};
713 struct inode
*inode
= NULL
;
717 if (out_len
< sizeof resp
)
720 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
723 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
724 (unsigned long) cmd
.response
+ sizeof resp
,
725 in_len
- sizeof cmd
, out_len
- sizeof resp
);
727 mutex_lock(&file
->device
->xrcd_tree_mutex
);
730 /* search for file descriptor */
734 goto err_tree_mutex_unlock
;
737 inode
= file_inode(f
.file
);
738 xrcd
= find_xrcd(file
->device
, inode
);
739 if (!xrcd
&& !(cmd
.oflags
& O_CREAT
)) {
740 /* no file descriptor. Need CREATE flag */
742 goto err_tree_mutex_unlock
;
745 if (xrcd
&& cmd
.oflags
& O_EXCL
) {
747 goto err_tree_mutex_unlock
;
751 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
754 goto err_tree_mutex_unlock
;
757 init_uobj(&obj
->uobject
, 0, file
->ucontext
, &xrcd_lock_class
);
759 down_write(&obj
->uobject
.mutex
);
762 xrcd
= file
->device
->ib_dev
->alloc_xrcd(file
->device
->ib_dev
,
763 file
->ucontext
, &udata
);
770 xrcd
->device
= file
->device
->ib_dev
;
771 atomic_set(&xrcd
->usecnt
, 0);
772 mutex_init(&xrcd
->tgt_qp_mutex
);
773 INIT_LIST_HEAD(&xrcd
->tgt_qp_list
);
777 atomic_set(&obj
->refcnt
, 0);
778 obj
->uobject
.object
= xrcd
;
779 ret
= idr_add_uobj(&ib_uverbs_xrcd_idr
, &obj
->uobject
);
783 memset(&resp
, 0, sizeof resp
);
784 resp
.xrcd_handle
= obj
->uobject
.id
;
788 /* create new inode/xrcd table entry */
789 ret
= xrcd_table_insert(file
->device
, inode
, xrcd
);
791 goto err_insert_xrcd
;
793 atomic_inc(&xrcd
->usecnt
);
796 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
797 &resp
, sizeof resp
)) {
805 mutex_lock(&file
->mutex
);
806 list_add_tail(&obj
->uobject
.list
, &file
->ucontext
->xrcd_list
);
807 mutex_unlock(&file
->mutex
);
809 obj
->uobject
.live
= 1;
810 up_write(&obj
->uobject
.mutex
);
812 mutex_unlock(&file
->device
->xrcd_tree_mutex
);
818 xrcd_table_delete(file
->device
, inode
);
819 atomic_dec(&xrcd
->usecnt
);
823 idr_remove_uobj(&ib_uverbs_xrcd_idr
, &obj
->uobject
);
826 ib_dealloc_xrcd(xrcd
);
829 put_uobj_write(&obj
->uobject
);
831 err_tree_mutex_unlock
:
835 mutex_unlock(&file
->device
->xrcd_tree_mutex
);
840 ssize_t
ib_uverbs_close_xrcd(struct ib_uverbs_file
*file
,
841 const char __user
*buf
, int in_len
,
844 struct ib_uverbs_close_xrcd cmd
;
845 struct ib_uobject
*uobj
;
846 struct ib_xrcd
*xrcd
= NULL
;
847 struct inode
*inode
= NULL
;
848 struct ib_uxrcd_object
*obj
;
852 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
855 mutex_lock(&file
->device
->xrcd_tree_mutex
);
856 uobj
= idr_write_uobj(&ib_uverbs_xrcd_idr
, cmd
.xrcd_handle
, file
->ucontext
);
864 obj
= container_of(uobj
, struct ib_uxrcd_object
, uobject
);
865 if (atomic_read(&obj
->refcnt
)) {
866 put_uobj_write(uobj
);
871 if (!inode
|| atomic_dec_and_test(&xrcd
->usecnt
)) {
872 ret
= ib_dealloc_xrcd(uobj
->object
);
879 atomic_inc(&xrcd
->usecnt
);
881 put_uobj_write(uobj
);
887 xrcd_table_delete(file
->device
, inode
);
889 idr_remove_uobj(&ib_uverbs_xrcd_idr
, uobj
);
890 mutex_lock(&file
->mutex
);
891 list_del(&uobj
->list
);
892 mutex_unlock(&file
->mutex
);
898 mutex_unlock(&file
->device
->xrcd_tree_mutex
);
902 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device
*dev
,
903 struct ib_xrcd
*xrcd
)
908 if (inode
&& !atomic_dec_and_test(&xrcd
->usecnt
))
911 ib_dealloc_xrcd(xrcd
);
914 xrcd_table_delete(dev
, inode
);
917 ssize_t
ib_uverbs_reg_mr(struct ib_uverbs_file
*file
,
918 const char __user
*buf
, int in_len
,
921 struct ib_uverbs_reg_mr cmd
;
922 struct ib_uverbs_reg_mr_resp resp
;
923 struct ib_udata udata
;
924 struct ib_uobject
*uobj
;
929 if (out_len
< sizeof resp
)
932 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
935 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
936 (unsigned long) cmd
.response
+ sizeof resp
,
937 in_len
- sizeof cmd
, out_len
- sizeof resp
);
939 if ((cmd
.start
& ~PAGE_MASK
) != (cmd
.hca_va
& ~PAGE_MASK
))
943 * Local write permission is required if remote write or
944 * remote atomic permission is also requested.
946 if (cmd
.access_flags
& (IB_ACCESS_REMOTE_ATOMIC
| IB_ACCESS_REMOTE_WRITE
) &&
947 !(cmd
.access_flags
& IB_ACCESS_LOCAL_WRITE
))
950 uobj
= kmalloc(sizeof *uobj
, GFP_KERNEL
);
954 init_uobj(uobj
, 0, file
->ucontext
, &mr_lock_class
);
955 down_write(&uobj
->mutex
);
957 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
963 mr
= pd
->device
->reg_user_mr(pd
, cmd
.start
, cmd
.length
, cmd
.hca_va
,
964 cmd
.access_flags
, &udata
);
970 mr
->device
= pd
->device
;
973 atomic_inc(&pd
->usecnt
);
974 atomic_set(&mr
->usecnt
, 0);
977 ret
= idr_add_uobj(&ib_uverbs_mr_idr
, uobj
);
981 memset(&resp
, 0, sizeof resp
);
982 resp
.lkey
= mr
->lkey
;
983 resp
.rkey
= mr
->rkey
;
984 resp
.mr_handle
= uobj
->id
;
986 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
987 &resp
, sizeof resp
)) {
994 mutex_lock(&file
->mutex
);
995 list_add_tail(&uobj
->list
, &file
->ucontext
->mr_list
);
996 mutex_unlock(&file
->mutex
);
1000 up_write(&uobj
->mutex
);
1005 idr_remove_uobj(&ib_uverbs_mr_idr
, uobj
);
1014 put_uobj_write(uobj
);
1018 ssize_t
ib_uverbs_dereg_mr(struct ib_uverbs_file
*file
,
1019 const char __user
*buf
, int in_len
,
1022 struct ib_uverbs_dereg_mr cmd
;
1024 struct ib_uobject
*uobj
;
1027 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1030 uobj
= idr_write_uobj(&ib_uverbs_mr_idr
, cmd
.mr_handle
, file
->ucontext
);
1036 ret
= ib_dereg_mr(mr
);
1040 put_uobj_write(uobj
);
1045 idr_remove_uobj(&ib_uverbs_mr_idr
, uobj
);
1047 mutex_lock(&file
->mutex
);
1048 list_del(&uobj
->list
);
1049 mutex_unlock(&file
->mutex
);
1056 ssize_t
ib_uverbs_alloc_mw(struct ib_uverbs_file
*file
,
1057 const char __user
*buf
, int in_len
,
1060 struct ib_uverbs_alloc_mw cmd
;
1061 struct ib_uverbs_alloc_mw_resp resp
;
1062 struct ib_uobject
*uobj
;
1067 if (out_len
< sizeof(resp
))
1070 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1073 uobj
= kmalloc(sizeof(*uobj
), GFP_KERNEL
);
1077 init_uobj(uobj
, 0, file
->ucontext
, &mw_lock_class
);
1078 down_write(&uobj
->mutex
);
1080 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
1086 mw
= pd
->device
->alloc_mw(pd
, cmd
.mw_type
);
1092 mw
->device
= pd
->device
;
1095 atomic_inc(&pd
->usecnt
);
1098 ret
= idr_add_uobj(&ib_uverbs_mw_idr
, uobj
);
1102 memset(&resp
, 0, sizeof(resp
));
1103 resp
.rkey
= mw
->rkey
;
1104 resp
.mw_handle
= uobj
->id
;
1106 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
1107 &resp
, sizeof(resp
))) {
1114 mutex_lock(&file
->mutex
);
1115 list_add_tail(&uobj
->list
, &file
->ucontext
->mw_list
);
1116 mutex_unlock(&file
->mutex
);
1120 up_write(&uobj
->mutex
);
1125 idr_remove_uobj(&ib_uverbs_mw_idr
, uobj
);
1134 put_uobj_write(uobj
);
1138 ssize_t
ib_uverbs_dealloc_mw(struct ib_uverbs_file
*file
,
1139 const char __user
*buf
, int in_len
,
1142 struct ib_uverbs_dealloc_mw cmd
;
1144 struct ib_uobject
*uobj
;
1147 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1150 uobj
= idr_write_uobj(&ib_uverbs_mw_idr
, cmd
.mw_handle
, file
->ucontext
);
1156 ret
= ib_dealloc_mw(mw
);
1160 put_uobj_write(uobj
);
1165 idr_remove_uobj(&ib_uverbs_mw_idr
, uobj
);
1167 mutex_lock(&file
->mutex
);
1168 list_del(&uobj
->list
);
1169 mutex_unlock(&file
->mutex
);
1176 ssize_t
ib_uverbs_create_comp_channel(struct ib_uverbs_file
*file
,
1177 const char __user
*buf
, int in_len
,
1180 struct ib_uverbs_create_comp_channel cmd
;
1181 struct ib_uverbs_create_comp_channel_resp resp
;
1185 if (out_len
< sizeof resp
)
1188 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1191 ret
= get_unused_fd_flags(O_CLOEXEC
);
1196 filp
= ib_uverbs_alloc_event_file(file
, 0);
1198 put_unused_fd(resp
.fd
);
1199 return PTR_ERR(filp
);
1202 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1203 &resp
, sizeof resp
)) {
1204 put_unused_fd(resp
.fd
);
1209 fd_install(resp
.fd
, filp
);
1213 ssize_t
ib_uverbs_create_cq(struct ib_uverbs_file
*file
,
1214 const char __user
*buf
, int in_len
,
1217 struct ib_uverbs_create_cq cmd
;
1218 struct ib_uverbs_create_cq_resp resp
;
1219 struct ib_udata udata
;
1220 struct ib_ucq_object
*obj
;
1221 struct ib_uverbs_event_file
*ev_file
= NULL
;
1225 if (out_len
< sizeof resp
)
1228 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1231 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
1232 (unsigned long) cmd
.response
+ sizeof resp
,
1233 in_len
- sizeof cmd
, out_len
- sizeof resp
);
1235 if (cmd
.comp_vector
>= file
->device
->num_comp_vectors
)
1238 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
1242 init_uobj(&obj
->uobject
, cmd
.user_handle
, file
->ucontext
, &cq_lock_class
);
1243 down_write(&obj
->uobject
.mutex
);
1245 if (cmd
.comp_channel
>= 0) {
1246 ev_file
= ib_uverbs_lookup_comp_file(cmd
.comp_channel
);
1253 obj
->uverbs_file
= file
;
1254 obj
->comp_events_reported
= 0;
1255 obj
->async_events_reported
= 0;
1256 INIT_LIST_HEAD(&obj
->comp_list
);
1257 INIT_LIST_HEAD(&obj
->async_list
);
1259 cq
= file
->device
->ib_dev
->create_cq(file
->device
->ib_dev
, cmd
.cqe
,
1261 file
->ucontext
, &udata
);
1267 cq
->device
= file
->device
->ib_dev
;
1268 cq
->uobject
= &obj
->uobject
;
1269 cq
->comp_handler
= ib_uverbs_comp_handler
;
1270 cq
->event_handler
= ib_uverbs_cq_event_handler
;
1271 cq
->cq_context
= ev_file
;
1272 atomic_set(&cq
->usecnt
, 0);
1274 obj
->uobject
.object
= cq
;
1275 ret
= idr_add_uobj(&ib_uverbs_cq_idr
, &obj
->uobject
);
1279 memset(&resp
, 0, sizeof resp
);
1280 resp
.cq_handle
= obj
->uobject
.id
;
1283 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1284 &resp
, sizeof resp
)) {
1289 mutex_lock(&file
->mutex
);
1290 list_add_tail(&obj
->uobject
.list
, &file
->ucontext
->cq_list
);
1291 mutex_unlock(&file
->mutex
);
1293 obj
->uobject
.live
= 1;
1295 up_write(&obj
->uobject
.mutex
);
1300 idr_remove_uobj(&ib_uverbs_cq_idr
, &obj
->uobject
);
1307 ib_uverbs_release_ucq(file
, ev_file
, obj
);
1310 put_uobj_write(&obj
->uobject
);
1314 ssize_t
ib_uverbs_resize_cq(struct ib_uverbs_file
*file
,
1315 const char __user
*buf
, int in_len
,
1318 struct ib_uverbs_resize_cq cmd
;
1319 struct ib_uverbs_resize_cq_resp resp
;
1320 struct ib_udata udata
;
1324 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1327 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
1328 (unsigned long) cmd
.response
+ sizeof resp
,
1329 in_len
- sizeof cmd
, out_len
- sizeof resp
);
1331 cq
= idr_read_cq(cmd
.cq_handle
, file
->ucontext
, 0);
1335 ret
= cq
->device
->resize_cq(cq
, cmd
.cqe
, &udata
);
1341 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1342 &resp
, sizeof resp
.cqe
))
1348 return ret
? ret
: in_len
;
1351 static int copy_wc_to_user(void __user
*dest
, struct ib_wc
*wc
)
1353 struct ib_uverbs_wc tmp
;
1355 tmp
.wr_id
= wc
->wr_id
;
1356 tmp
.status
= wc
->status
;
1357 tmp
.opcode
= wc
->opcode
;
1358 tmp
.vendor_err
= wc
->vendor_err
;
1359 tmp
.byte_len
= wc
->byte_len
;
1360 tmp
.ex
.imm_data
= (__u32 __force
) wc
->ex
.imm_data
;
1361 tmp
.qp_num
= wc
->qp
->qp_num
;
1362 tmp
.src_qp
= wc
->src_qp
;
1363 tmp
.wc_flags
= wc
->wc_flags
;
1364 tmp
.pkey_index
= wc
->pkey_index
;
1365 tmp
.slid
= wc
->slid
;
1367 tmp
.dlid_path_bits
= wc
->dlid_path_bits
;
1368 tmp
.port_num
= wc
->port_num
;
1371 if (copy_to_user(dest
, &tmp
, sizeof tmp
))
1377 ssize_t
ib_uverbs_poll_cq(struct ib_uverbs_file
*file
,
1378 const char __user
*buf
, int in_len
,
1381 struct ib_uverbs_poll_cq cmd
;
1382 struct ib_uverbs_poll_cq_resp resp
;
1383 u8 __user
*header_ptr
;
1384 u8 __user
*data_ptr
;
1389 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1392 cq
= idr_read_cq(cmd
.cq_handle
, file
->ucontext
, 0);
1396 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1397 header_ptr
= (void __user
*)(unsigned long) cmd
.response
;
1398 data_ptr
= header_ptr
+ sizeof resp
;
1400 memset(&resp
, 0, sizeof resp
);
1401 while (resp
.count
< cmd
.ne
) {
1402 ret
= ib_poll_cq(cq
, 1, &wc
);
1408 ret
= copy_wc_to_user(data_ptr
, &wc
);
1412 data_ptr
+= sizeof(struct ib_uverbs_wc
);
1416 if (copy_to_user(header_ptr
, &resp
, sizeof resp
)) {
1428 ssize_t
ib_uverbs_req_notify_cq(struct ib_uverbs_file
*file
,
1429 const char __user
*buf
, int in_len
,
1432 struct ib_uverbs_req_notify_cq cmd
;
1435 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1438 cq
= idr_read_cq(cmd
.cq_handle
, file
->ucontext
, 0);
1442 ib_req_notify_cq(cq
, cmd
.solicited_only
?
1443 IB_CQ_SOLICITED
: IB_CQ_NEXT_COMP
);
1450 ssize_t
ib_uverbs_destroy_cq(struct ib_uverbs_file
*file
,
1451 const char __user
*buf
, int in_len
,
1454 struct ib_uverbs_destroy_cq cmd
;
1455 struct ib_uverbs_destroy_cq_resp resp
;
1456 struct ib_uobject
*uobj
;
1458 struct ib_ucq_object
*obj
;
1459 struct ib_uverbs_event_file
*ev_file
;
1462 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1465 uobj
= idr_write_uobj(&ib_uverbs_cq_idr
, cmd
.cq_handle
, file
->ucontext
);
1469 ev_file
= cq
->cq_context
;
1470 obj
= container_of(cq
->uobject
, struct ib_ucq_object
, uobject
);
1472 ret
= ib_destroy_cq(cq
);
1476 put_uobj_write(uobj
);
1481 idr_remove_uobj(&ib_uverbs_cq_idr
, uobj
);
1483 mutex_lock(&file
->mutex
);
1484 list_del(&uobj
->list
);
1485 mutex_unlock(&file
->mutex
);
1487 ib_uverbs_release_ucq(file
, ev_file
, obj
);
1489 memset(&resp
, 0, sizeof resp
);
1490 resp
.comp_events_reported
= obj
->comp_events_reported
;
1491 resp
.async_events_reported
= obj
->async_events_reported
;
1495 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1496 &resp
, sizeof resp
))
1502 ssize_t
ib_uverbs_create_qp(struct ib_uverbs_file
*file
,
1503 const char __user
*buf
, int in_len
,
1506 struct ib_uverbs_create_qp cmd
;
1507 struct ib_uverbs_create_qp_resp resp
;
1508 struct ib_udata udata
;
1509 struct ib_uqp_object
*obj
;
1510 struct ib_device
*device
;
1511 struct ib_pd
*pd
= NULL
;
1512 struct ib_xrcd
*xrcd
= NULL
;
1513 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
1514 struct ib_cq
*scq
= NULL
, *rcq
= NULL
;
1515 struct ib_srq
*srq
= NULL
;
1517 struct ib_qp_init_attr attr
;
1520 if (out_len
< sizeof resp
)
1523 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1526 if (cmd
.qp_type
== IB_QPT_RAW_PACKET
&& !capable(CAP_NET_RAW
))
1529 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
1530 (unsigned long) cmd
.response
+ sizeof resp
,
1531 in_len
- sizeof cmd
, out_len
- sizeof resp
);
1533 obj
= kzalloc(sizeof *obj
, GFP_KERNEL
);
1537 init_uobj(&obj
->uevent
.uobject
, cmd
.user_handle
, file
->ucontext
, &qp_lock_class
);
1538 down_write(&obj
->uevent
.uobject
.mutex
);
1540 if (cmd
.qp_type
== IB_QPT_XRC_TGT
) {
1541 xrcd
= idr_read_xrcd(cmd
.pd_handle
, file
->ucontext
, &xrcd_uobj
);
1546 device
= xrcd
->device
;
1548 if (cmd
.qp_type
== IB_QPT_XRC_INI
) {
1549 cmd
.max_recv_wr
= cmd
.max_recv_sge
= 0;
1552 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
1553 if (!srq
|| srq
->srq_type
!= IB_SRQT_BASIC
) {
1559 if (cmd
.recv_cq_handle
!= cmd
.send_cq_handle
) {
1560 rcq
= idr_read_cq(cmd
.recv_cq_handle
, file
->ucontext
, 0);
1568 scq
= idr_read_cq(cmd
.send_cq_handle
, file
->ucontext
, !!rcq
);
1570 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
1576 device
= pd
->device
;
1579 attr
.event_handler
= ib_uverbs_qp_event_handler
;
1580 attr
.qp_context
= file
;
1585 attr
.sq_sig_type
= cmd
.sq_sig_all
? IB_SIGNAL_ALL_WR
: IB_SIGNAL_REQ_WR
;
1586 attr
.qp_type
= cmd
.qp_type
;
1587 attr
.create_flags
= 0;
1589 attr
.cap
.max_send_wr
= cmd
.max_send_wr
;
1590 attr
.cap
.max_recv_wr
= cmd
.max_recv_wr
;
1591 attr
.cap
.max_send_sge
= cmd
.max_send_sge
;
1592 attr
.cap
.max_recv_sge
= cmd
.max_recv_sge
;
1593 attr
.cap
.max_inline_data
= cmd
.max_inline_data
;
1595 obj
->uevent
.events_reported
= 0;
1596 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
1597 INIT_LIST_HEAD(&obj
->mcast_list
);
1599 if (cmd
.qp_type
== IB_QPT_XRC_TGT
)
1600 qp
= ib_create_qp(pd
, &attr
);
1602 qp
= device
->create_qp(pd
, &attr
, &udata
);
1609 if (cmd
.qp_type
!= IB_QPT_XRC_TGT
) {
1611 qp
->device
= device
;
1613 qp
->send_cq
= attr
.send_cq
;
1614 qp
->recv_cq
= attr
.recv_cq
;
1616 qp
->event_handler
= attr
.event_handler
;
1617 qp
->qp_context
= attr
.qp_context
;
1618 qp
->qp_type
= attr
.qp_type
;
1619 atomic_set(&qp
->usecnt
, 0);
1620 atomic_inc(&pd
->usecnt
);
1621 atomic_inc(&attr
.send_cq
->usecnt
);
1623 atomic_inc(&attr
.recv_cq
->usecnt
);
1625 atomic_inc(&attr
.srq
->usecnt
);
1627 qp
->uobject
= &obj
->uevent
.uobject
;
1629 obj
->uevent
.uobject
.object
= qp
;
1630 ret
= idr_add_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
1634 memset(&resp
, 0, sizeof resp
);
1635 resp
.qpn
= qp
->qp_num
;
1636 resp
.qp_handle
= obj
->uevent
.uobject
.id
;
1637 resp
.max_recv_sge
= attr
.cap
.max_recv_sge
;
1638 resp
.max_send_sge
= attr
.cap
.max_send_sge
;
1639 resp
.max_recv_wr
= attr
.cap
.max_recv_wr
;
1640 resp
.max_send_wr
= attr
.cap
.max_send_wr
;
1641 resp
.max_inline_data
= attr
.cap
.max_inline_data
;
1643 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1644 &resp
, sizeof resp
)) {
1650 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
,
1652 atomic_inc(&obj
->uxrcd
->refcnt
);
1653 put_xrcd_read(xrcd_uobj
);
1660 if (rcq
&& rcq
!= scq
)
1665 mutex_lock(&file
->mutex
);
1666 list_add_tail(&obj
->uevent
.uobject
.list
, &file
->ucontext
->qp_list
);
1667 mutex_unlock(&file
->mutex
);
1669 obj
->uevent
.uobject
.live
= 1;
1671 up_write(&obj
->uevent
.uobject
.mutex
);
1676 idr_remove_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
1683 put_xrcd_read(xrcd_uobj
);
1688 if (rcq
&& rcq
!= scq
)
1693 put_uobj_write(&obj
->uevent
.uobject
);
1697 ssize_t
ib_uverbs_open_qp(struct ib_uverbs_file
*file
,
1698 const char __user
*buf
, int in_len
, int out_len
)
1700 struct ib_uverbs_open_qp cmd
;
1701 struct ib_uverbs_create_qp_resp resp
;
1702 struct ib_udata udata
;
1703 struct ib_uqp_object
*obj
;
1704 struct ib_xrcd
*xrcd
;
1705 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
1707 struct ib_qp_open_attr attr
;
1710 if (out_len
< sizeof resp
)
1713 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1716 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
1717 (unsigned long) cmd
.response
+ sizeof resp
,
1718 in_len
- sizeof cmd
, out_len
- sizeof resp
);
1720 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
1724 init_uobj(&obj
->uevent
.uobject
, cmd
.user_handle
, file
->ucontext
, &qp_lock_class
);
1725 down_write(&obj
->uevent
.uobject
.mutex
);
1727 xrcd
= idr_read_xrcd(cmd
.pd_handle
, file
->ucontext
, &xrcd_uobj
);
1733 attr
.event_handler
= ib_uverbs_qp_event_handler
;
1734 attr
.qp_context
= file
;
1735 attr
.qp_num
= cmd
.qpn
;
1736 attr
.qp_type
= cmd
.qp_type
;
1738 obj
->uevent
.events_reported
= 0;
1739 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
1740 INIT_LIST_HEAD(&obj
->mcast_list
);
1742 qp
= ib_open_qp(xrcd
, &attr
);
1748 qp
->uobject
= &obj
->uevent
.uobject
;
1750 obj
->uevent
.uobject
.object
= qp
;
1751 ret
= idr_add_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
1755 memset(&resp
, 0, sizeof resp
);
1756 resp
.qpn
= qp
->qp_num
;
1757 resp
.qp_handle
= obj
->uevent
.uobject
.id
;
1759 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1760 &resp
, sizeof resp
)) {
1765 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
, uobject
);
1766 atomic_inc(&obj
->uxrcd
->refcnt
);
1767 put_xrcd_read(xrcd_uobj
);
1769 mutex_lock(&file
->mutex
);
1770 list_add_tail(&obj
->uevent
.uobject
.list
, &file
->ucontext
->qp_list
);
1771 mutex_unlock(&file
->mutex
);
1773 obj
->uevent
.uobject
.live
= 1;
1775 up_write(&obj
->uevent
.uobject
.mutex
);
1780 idr_remove_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
1786 put_xrcd_read(xrcd_uobj
);
1787 put_uobj_write(&obj
->uevent
.uobject
);
1791 ssize_t
ib_uverbs_query_qp(struct ib_uverbs_file
*file
,
1792 const char __user
*buf
, int in_len
,
1795 struct ib_uverbs_query_qp cmd
;
1796 struct ib_uverbs_query_qp_resp resp
;
1798 struct ib_qp_attr
*attr
;
1799 struct ib_qp_init_attr
*init_attr
;
1802 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1805 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
1806 init_attr
= kmalloc(sizeof *init_attr
, GFP_KERNEL
);
1807 if (!attr
|| !init_attr
) {
1812 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
1818 ret
= ib_query_qp(qp
, attr
, cmd
.attr_mask
, init_attr
);
1825 memset(&resp
, 0, sizeof resp
);
1827 resp
.qp_state
= attr
->qp_state
;
1828 resp
.cur_qp_state
= attr
->cur_qp_state
;
1829 resp
.path_mtu
= attr
->path_mtu
;
1830 resp
.path_mig_state
= attr
->path_mig_state
;
1831 resp
.qkey
= attr
->qkey
;
1832 resp
.rq_psn
= attr
->rq_psn
;
1833 resp
.sq_psn
= attr
->sq_psn
;
1834 resp
.dest_qp_num
= attr
->dest_qp_num
;
1835 resp
.qp_access_flags
= attr
->qp_access_flags
;
1836 resp
.pkey_index
= attr
->pkey_index
;
1837 resp
.alt_pkey_index
= attr
->alt_pkey_index
;
1838 resp
.sq_draining
= attr
->sq_draining
;
1839 resp
.max_rd_atomic
= attr
->max_rd_atomic
;
1840 resp
.max_dest_rd_atomic
= attr
->max_dest_rd_atomic
;
1841 resp
.min_rnr_timer
= attr
->min_rnr_timer
;
1842 resp
.port_num
= attr
->port_num
;
1843 resp
.timeout
= attr
->timeout
;
1844 resp
.retry_cnt
= attr
->retry_cnt
;
1845 resp
.rnr_retry
= attr
->rnr_retry
;
1846 resp
.alt_port_num
= attr
->alt_port_num
;
1847 resp
.alt_timeout
= attr
->alt_timeout
;
1849 memcpy(resp
.dest
.dgid
, attr
->ah_attr
.grh
.dgid
.raw
, 16);
1850 resp
.dest
.flow_label
= attr
->ah_attr
.grh
.flow_label
;
1851 resp
.dest
.sgid_index
= attr
->ah_attr
.grh
.sgid_index
;
1852 resp
.dest
.hop_limit
= attr
->ah_attr
.grh
.hop_limit
;
1853 resp
.dest
.traffic_class
= attr
->ah_attr
.grh
.traffic_class
;
1854 resp
.dest
.dlid
= attr
->ah_attr
.dlid
;
1855 resp
.dest
.sl
= attr
->ah_attr
.sl
;
1856 resp
.dest
.src_path_bits
= attr
->ah_attr
.src_path_bits
;
1857 resp
.dest
.static_rate
= attr
->ah_attr
.static_rate
;
1858 resp
.dest
.is_global
= !!(attr
->ah_attr
.ah_flags
& IB_AH_GRH
);
1859 resp
.dest
.port_num
= attr
->ah_attr
.port_num
;
1861 memcpy(resp
.alt_dest
.dgid
, attr
->alt_ah_attr
.grh
.dgid
.raw
, 16);
1862 resp
.alt_dest
.flow_label
= attr
->alt_ah_attr
.grh
.flow_label
;
1863 resp
.alt_dest
.sgid_index
= attr
->alt_ah_attr
.grh
.sgid_index
;
1864 resp
.alt_dest
.hop_limit
= attr
->alt_ah_attr
.grh
.hop_limit
;
1865 resp
.alt_dest
.traffic_class
= attr
->alt_ah_attr
.grh
.traffic_class
;
1866 resp
.alt_dest
.dlid
= attr
->alt_ah_attr
.dlid
;
1867 resp
.alt_dest
.sl
= attr
->alt_ah_attr
.sl
;
1868 resp
.alt_dest
.src_path_bits
= attr
->alt_ah_attr
.src_path_bits
;
1869 resp
.alt_dest
.static_rate
= attr
->alt_ah_attr
.static_rate
;
1870 resp
.alt_dest
.is_global
= !!(attr
->alt_ah_attr
.ah_flags
& IB_AH_GRH
);
1871 resp
.alt_dest
.port_num
= attr
->alt_ah_attr
.port_num
;
1873 resp
.max_send_wr
= init_attr
->cap
.max_send_wr
;
1874 resp
.max_recv_wr
= init_attr
->cap
.max_recv_wr
;
1875 resp
.max_send_sge
= init_attr
->cap
.max_send_sge
;
1876 resp
.max_recv_sge
= init_attr
->cap
.max_recv_sge
;
1877 resp
.max_inline_data
= init_attr
->cap
.max_inline_data
;
1878 resp
.sq_sig_all
= init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
;
1880 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1881 &resp
, sizeof resp
))
1888 return ret
? ret
: in_len
;
1891 /* Remove ignored fields set in the attribute mask */
1892 static int modify_qp_mask(enum ib_qp_type qp_type
, int mask
)
1895 case IB_QPT_XRC_INI
:
1896 return mask
& ~(IB_QP_MAX_DEST_RD_ATOMIC
| IB_QP_MIN_RNR_TIMER
);
1897 case IB_QPT_XRC_TGT
:
1898 return mask
& ~(IB_QP_MAX_QP_RD_ATOMIC
| IB_QP_RETRY_CNT
|
1905 ssize_t
ib_uverbs_modify_qp(struct ib_uverbs_file
*file
,
1906 const char __user
*buf
, int in_len
,
1909 struct ib_uverbs_modify_qp cmd
;
1910 struct ib_udata udata
;
1912 struct ib_qp_attr
*attr
;
1915 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1918 INIT_UDATA(&udata
, buf
+ sizeof cmd
, NULL
, in_len
- sizeof cmd
,
1921 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
1925 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
1931 attr
->qp_state
= cmd
.qp_state
;
1932 attr
->cur_qp_state
= cmd
.cur_qp_state
;
1933 attr
->path_mtu
= cmd
.path_mtu
;
1934 attr
->path_mig_state
= cmd
.path_mig_state
;
1935 attr
->qkey
= cmd
.qkey
;
1936 attr
->rq_psn
= cmd
.rq_psn
;
1937 attr
->sq_psn
= cmd
.sq_psn
;
1938 attr
->dest_qp_num
= cmd
.dest_qp_num
;
1939 attr
->qp_access_flags
= cmd
.qp_access_flags
;
1940 attr
->pkey_index
= cmd
.pkey_index
;
1941 attr
->alt_pkey_index
= cmd
.alt_pkey_index
;
1942 attr
->en_sqd_async_notify
= cmd
.en_sqd_async_notify
;
1943 attr
->max_rd_atomic
= cmd
.max_rd_atomic
;
1944 attr
->max_dest_rd_atomic
= cmd
.max_dest_rd_atomic
;
1945 attr
->min_rnr_timer
= cmd
.min_rnr_timer
;
1946 attr
->port_num
= cmd
.port_num
;
1947 attr
->timeout
= cmd
.timeout
;
1948 attr
->retry_cnt
= cmd
.retry_cnt
;
1949 attr
->rnr_retry
= cmd
.rnr_retry
;
1950 attr
->alt_port_num
= cmd
.alt_port_num
;
1951 attr
->alt_timeout
= cmd
.alt_timeout
;
1953 memcpy(attr
->ah_attr
.grh
.dgid
.raw
, cmd
.dest
.dgid
, 16);
1954 attr
->ah_attr
.grh
.flow_label
= cmd
.dest
.flow_label
;
1955 attr
->ah_attr
.grh
.sgid_index
= cmd
.dest
.sgid_index
;
1956 attr
->ah_attr
.grh
.hop_limit
= cmd
.dest
.hop_limit
;
1957 attr
->ah_attr
.grh
.traffic_class
= cmd
.dest
.traffic_class
;
1958 attr
->ah_attr
.dlid
= cmd
.dest
.dlid
;
1959 attr
->ah_attr
.sl
= cmd
.dest
.sl
;
1960 attr
->ah_attr
.src_path_bits
= cmd
.dest
.src_path_bits
;
1961 attr
->ah_attr
.static_rate
= cmd
.dest
.static_rate
;
1962 attr
->ah_attr
.ah_flags
= cmd
.dest
.is_global
? IB_AH_GRH
: 0;
1963 attr
->ah_attr
.port_num
= cmd
.dest
.port_num
;
1965 memcpy(attr
->alt_ah_attr
.grh
.dgid
.raw
, cmd
.alt_dest
.dgid
, 16);
1966 attr
->alt_ah_attr
.grh
.flow_label
= cmd
.alt_dest
.flow_label
;
1967 attr
->alt_ah_attr
.grh
.sgid_index
= cmd
.alt_dest
.sgid_index
;
1968 attr
->alt_ah_attr
.grh
.hop_limit
= cmd
.alt_dest
.hop_limit
;
1969 attr
->alt_ah_attr
.grh
.traffic_class
= cmd
.alt_dest
.traffic_class
;
1970 attr
->alt_ah_attr
.dlid
= cmd
.alt_dest
.dlid
;
1971 attr
->alt_ah_attr
.sl
= cmd
.alt_dest
.sl
;
1972 attr
->alt_ah_attr
.src_path_bits
= cmd
.alt_dest
.src_path_bits
;
1973 attr
->alt_ah_attr
.static_rate
= cmd
.alt_dest
.static_rate
;
1974 attr
->alt_ah_attr
.ah_flags
= cmd
.alt_dest
.is_global
? IB_AH_GRH
: 0;
1975 attr
->alt_ah_attr
.port_num
= cmd
.alt_dest
.port_num
;
1977 if (qp
->real_qp
== qp
) {
1978 ret
= qp
->device
->modify_qp(qp
, attr
,
1979 modify_qp_mask(qp
->qp_type
, cmd
.attr_mask
), &udata
);
1981 ret
= ib_modify_qp(qp
, attr
, modify_qp_mask(qp
->qp_type
, cmd
.attr_mask
));
1997 ssize_t
ib_uverbs_destroy_qp(struct ib_uverbs_file
*file
,
1998 const char __user
*buf
, int in_len
,
2001 struct ib_uverbs_destroy_qp cmd
;
2002 struct ib_uverbs_destroy_qp_resp resp
;
2003 struct ib_uobject
*uobj
;
2005 struct ib_uqp_object
*obj
;
2008 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2011 memset(&resp
, 0, sizeof resp
);
2013 uobj
= idr_write_uobj(&ib_uverbs_qp_idr
, cmd
.qp_handle
, file
->ucontext
);
2017 obj
= container_of(uobj
, struct ib_uqp_object
, uevent
.uobject
);
2019 if (!list_empty(&obj
->mcast_list
)) {
2020 put_uobj_write(uobj
);
2024 ret
= ib_destroy_qp(qp
);
2028 put_uobj_write(uobj
);
2034 atomic_dec(&obj
->uxrcd
->refcnt
);
2036 idr_remove_uobj(&ib_uverbs_qp_idr
, uobj
);
2038 mutex_lock(&file
->mutex
);
2039 list_del(&uobj
->list
);
2040 mutex_unlock(&file
->mutex
);
2042 ib_uverbs_release_uevent(file
, &obj
->uevent
);
2044 resp
.events_reported
= obj
->uevent
.events_reported
;
2048 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2049 &resp
, sizeof resp
))
2055 ssize_t
ib_uverbs_post_send(struct ib_uverbs_file
*file
,
2056 const char __user
*buf
, int in_len
,
2059 struct ib_uverbs_post_send cmd
;
2060 struct ib_uverbs_post_send_resp resp
;
2061 struct ib_uverbs_send_wr
*user_wr
;
2062 struct ib_send_wr
*wr
= NULL
, *last
, *next
, *bad_wr
;
2066 ssize_t ret
= -EINVAL
;
2068 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2071 if (in_len
< sizeof cmd
+ cmd
.wqe_size
* cmd
.wr_count
+
2072 cmd
.sge_count
* sizeof (struct ib_uverbs_sge
))
2075 if (cmd
.wqe_size
< sizeof (struct ib_uverbs_send_wr
))
2078 user_wr
= kmalloc(cmd
.wqe_size
, GFP_KERNEL
);
2082 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2086 is_ud
= qp
->qp_type
== IB_QPT_UD
;
2089 for (i
= 0; i
< cmd
.wr_count
; ++i
) {
2090 if (copy_from_user(user_wr
,
2091 buf
+ sizeof cmd
+ i
* cmd
.wqe_size
,
2097 if (user_wr
->num_sge
+ sg_ind
> cmd
.sge_count
) {
2102 next
= kmalloc(ALIGN(sizeof *next
, sizeof (struct ib_sge
)) +
2103 user_wr
->num_sge
* sizeof (struct ib_sge
),
2117 next
->wr_id
= user_wr
->wr_id
;
2118 next
->num_sge
= user_wr
->num_sge
;
2119 next
->opcode
= user_wr
->opcode
;
2120 next
->send_flags
= user_wr
->send_flags
;
2123 next
->wr
.ud
.ah
= idr_read_ah(user_wr
->wr
.ud
.ah
,
2125 if (!next
->wr
.ud
.ah
) {
2129 next
->wr
.ud
.remote_qpn
= user_wr
->wr
.ud
.remote_qpn
;
2130 next
->wr
.ud
.remote_qkey
= user_wr
->wr
.ud
.remote_qkey
;
2132 switch (next
->opcode
) {
2133 case IB_WR_RDMA_WRITE_WITH_IMM
:
2135 (__be32 __force
) user_wr
->ex
.imm_data
;
2136 case IB_WR_RDMA_WRITE
:
2137 case IB_WR_RDMA_READ
:
2138 next
->wr
.rdma
.remote_addr
=
2139 user_wr
->wr
.rdma
.remote_addr
;
2140 next
->wr
.rdma
.rkey
=
2141 user_wr
->wr
.rdma
.rkey
;
2143 case IB_WR_SEND_WITH_IMM
:
2145 (__be32 __force
) user_wr
->ex
.imm_data
;
2147 case IB_WR_SEND_WITH_INV
:
2148 next
->ex
.invalidate_rkey
=
2149 user_wr
->ex
.invalidate_rkey
;
2151 case IB_WR_ATOMIC_CMP_AND_SWP
:
2152 case IB_WR_ATOMIC_FETCH_AND_ADD
:
2153 next
->wr
.atomic
.remote_addr
=
2154 user_wr
->wr
.atomic
.remote_addr
;
2155 next
->wr
.atomic
.compare_add
=
2156 user_wr
->wr
.atomic
.compare_add
;
2157 next
->wr
.atomic
.swap
= user_wr
->wr
.atomic
.swap
;
2158 next
->wr
.atomic
.rkey
= user_wr
->wr
.atomic
.rkey
;
2165 if (next
->num_sge
) {
2166 next
->sg_list
= (void *) next
+
2167 ALIGN(sizeof *next
, sizeof (struct ib_sge
));
2168 if (copy_from_user(next
->sg_list
,
2170 cmd
.wr_count
* cmd
.wqe_size
+
2171 sg_ind
* sizeof (struct ib_sge
),
2172 next
->num_sge
* sizeof (struct ib_sge
))) {
2176 sg_ind
+= next
->num_sge
;
2178 next
->sg_list
= NULL
;
2182 ret
= qp
->device
->post_send(qp
->real_qp
, wr
, &bad_wr
);
2184 for (next
= wr
; next
; next
= next
->next
) {
2190 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2191 &resp
, sizeof resp
))
2198 if (is_ud
&& wr
->wr
.ud
.ah
)
2199 put_ah_read(wr
->wr
.ud
.ah
);
2208 return ret
? ret
: in_len
;
2211 static struct ib_recv_wr
*ib_uverbs_unmarshall_recv(const char __user
*buf
,
2217 struct ib_uverbs_recv_wr
*user_wr
;
2218 struct ib_recv_wr
*wr
= NULL
, *last
, *next
;
2223 if (in_len
< wqe_size
* wr_count
+
2224 sge_count
* sizeof (struct ib_uverbs_sge
))
2225 return ERR_PTR(-EINVAL
);
2227 if (wqe_size
< sizeof (struct ib_uverbs_recv_wr
))
2228 return ERR_PTR(-EINVAL
);
2230 user_wr
= kmalloc(wqe_size
, GFP_KERNEL
);
2232 return ERR_PTR(-ENOMEM
);
2236 for (i
= 0; i
< wr_count
; ++i
) {
2237 if (copy_from_user(user_wr
, buf
+ i
* wqe_size
,
2243 if (user_wr
->num_sge
+ sg_ind
> sge_count
) {
2248 next
= kmalloc(ALIGN(sizeof *next
, sizeof (struct ib_sge
)) +
2249 user_wr
->num_sge
* sizeof (struct ib_sge
),
2263 next
->wr_id
= user_wr
->wr_id
;
2264 next
->num_sge
= user_wr
->num_sge
;
2266 if (next
->num_sge
) {
2267 next
->sg_list
= (void *) next
+
2268 ALIGN(sizeof *next
, sizeof (struct ib_sge
));
2269 if (copy_from_user(next
->sg_list
,
2270 buf
+ wr_count
* wqe_size
+
2271 sg_ind
* sizeof (struct ib_sge
),
2272 next
->num_sge
* sizeof (struct ib_sge
))) {
2276 sg_ind
+= next
->num_sge
;
2278 next
->sg_list
= NULL
;
2293 return ERR_PTR(ret
);
2296 ssize_t
ib_uverbs_post_recv(struct ib_uverbs_file
*file
,
2297 const char __user
*buf
, int in_len
,
2300 struct ib_uverbs_post_recv cmd
;
2301 struct ib_uverbs_post_recv_resp resp
;
2302 struct ib_recv_wr
*wr
, *next
, *bad_wr
;
2304 ssize_t ret
= -EINVAL
;
2306 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2309 wr
= ib_uverbs_unmarshall_recv(buf
+ sizeof cmd
,
2310 in_len
- sizeof cmd
, cmd
.wr_count
,
2311 cmd
.sge_count
, cmd
.wqe_size
);
2315 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2320 ret
= qp
->device
->post_recv(qp
->real_qp
, wr
, &bad_wr
);
2325 for (next
= wr
; next
; next
= next
->next
) {
2331 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2332 &resp
, sizeof resp
))
2342 return ret
? ret
: in_len
;
2345 ssize_t
ib_uverbs_post_srq_recv(struct ib_uverbs_file
*file
,
2346 const char __user
*buf
, int in_len
,
2349 struct ib_uverbs_post_srq_recv cmd
;
2350 struct ib_uverbs_post_srq_recv_resp resp
;
2351 struct ib_recv_wr
*wr
, *next
, *bad_wr
;
2353 ssize_t ret
= -EINVAL
;
2355 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2358 wr
= ib_uverbs_unmarshall_recv(buf
+ sizeof cmd
,
2359 in_len
- sizeof cmd
, cmd
.wr_count
,
2360 cmd
.sge_count
, cmd
.wqe_size
);
2364 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
2369 ret
= srq
->device
->post_srq_recv(srq
, wr
, &bad_wr
);
2374 for (next
= wr
; next
; next
= next
->next
) {
2380 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2381 &resp
, sizeof resp
))
2391 return ret
? ret
: in_len
;
2394 ssize_t
ib_uverbs_create_ah(struct ib_uverbs_file
*file
,
2395 const char __user
*buf
, int in_len
,
2398 struct ib_uverbs_create_ah cmd
;
2399 struct ib_uverbs_create_ah_resp resp
;
2400 struct ib_uobject
*uobj
;
2403 struct ib_ah_attr attr
;
2406 if (out_len
< sizeof resp
)
2409 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2412 uobj
= kmalloc(sizeof *uobj
, GFP_KERNEL
);
2416 init_uobj(uobj
, cmd
.user_handle
, file
->ucontext
, &ah_lock_class
);
2417 down_write(&uobj
->mutex
);
2419 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
2425 attr
.dlid
= cmd
.attr
.dlid
;
2426 attr
.sl
= cmd
.attr
.sl
;
2427 attr
.src_path_bits
= cmd
.attr
.src_path_bits
;
2428 attr
.static_rate
= cmd
.attr
.static_rate
;
2429 attr
.ah_flags
= cmd
.attr
.is_global
? IB_AH_GRH
: 0;
2430 attr
.port_num
= cmd
.attr
.port_num
;
2431 attr
.grh
.flow_label
= cmd
.attr
.grh
.flow_label
;
2432 attr
.grh
.sgid_index
= cmd
.attr
.grh
.sgid_index
;
2433 attr
.grh
.hop_limit
= cmd
.attr
.grh
.hop_limit
;
2434 attr
.grh
.traffic_class
= cmd
.attr
.grh
.traffic_class
;
2435 memcpy(attr
.grh
.dgid
.raw
, cmd
.attr
.grh
.dgid
, 16);
2437 ah
= ib_create_ah(pd
, &attr
);
2446 ret
= idr_add_uobj(&ib_uverbs_ah_idr
, uobj
);
2450 resp
.ah_handle
= uobj
->id
;
2452 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2453 &resp
, sizeof resp
)) {
2460 mutex_lock(&file
->mutex
);
2461 list_add_tail(&uobj
->list
, &file
->ucontext
->ah_list
);
2462 mutex_unlock(&file
->mutex
);
2466 up_write(&uobj
->mutex
);
2471 idr_remove_uobj(&ib_uverbs_ah_idr
, uobj
);
2480 put_uobj_write(uobj
);
2484 ssize_t
ib_uverbs_destroy_ah(struct ib_uverbs_file
*file
,
2485 const char __user
*buf
, int in_len
, int out_len
)
2487 struct ib_uverbs_destroy_ah cmd
;
2489 struct ib_uobject
*uobj
;
2492 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2495 uobj
= idr_write_uobj(&ib_uverbs_ah_idr
, cmd
.ah_handle
, file
->ucontext
);
2500 ret
= ib_destroy_ah(ah
);
2504 put_uobj_write(uobj
);
2509 idr_remove_uobj(&ib_uverbs_ah_idr
, uobj
);
2511 mutex_lock(&file
->mutex
);
2512 list_del(&uobj
->list
);
2513 mutex_unlock(&file
->mutex
);
2520 ssize_t
ib_uverbs_attach_mcast(struct ib_uverbs_file
*file
,
2521 const char __user
*buf
, int in_len
,
2524 struct ib_uverbs_attach_mcast cmd
;
2526 struct ib_uqp_object
*obj
;
2527 struct ib_uverbs_mcast_entry
*mcast
;
2530 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2533 qp
= idr_write_qp(cmd
.qp_handle
, file
->ucontext
);
2537 obj
= container_of(qp
->uobject
, struct ib_uqp_object
, uevent
.uobject
);
2539 list_for_each_entry(mcast
, &obj
->mcast_list
, list
)
2540 if (cmd
.mlid
== mcast
->lid
&&
2541 !memcmp(cmd
.gid
, mcast
->gid
.raw
, sizeof mcast
->gid
.raw
)) {
2546 mcast
= kmalloc(sizeof *mcast
, GFP_KERNEL
);
2552 mcast
->lid
= cmd
.mlid
;
2553 memcpy(mcast
->gid
.raw
, cmd
.gid
, sizeof mcast
->gid
.raw
);
2555 ret
= ib_attach_mcast(qp
, &mcast
->gid
, cmd
.mlid
);
2557 list_add_tail(&mcast
->list
, &obj
->mcast_list
);
2564 return ret
? ret
: in_len
;
2567 ssize_t
ib_uverbs_detach_mcast(struct ib_uverbs_file
*file
,
2568 const char __user
*buf
, int in_len
,
2571 struct ib_uverbs_detach_mcast cmd
;
2572 struct ib_uqp_object
*obj
;
2574 struct ib_uverbs_mcast_entry
*mcast
;
2577 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2580 qp
= idr_write_qp(cmd
.qp_handle
, file
->ucontext
);
2584 ret
= ib_detach_mcast(qp
, (union ib_gid
*) cmd
.gid
, cmd
.mlid
);
2588 obj
= container_of(qp
->uobject
, struct ib_uqp_object
, uevent
.uobject
);
2590 list_for_each_entry(mcast
, &obj
->mcast_list
, list
)
2591 if (cmd
.mlid
== mcast
->lid
&&
2592 !memcmp(cmd
.gid
, mcast
->gid
.raw
, sizeof mcast
->gid
.raw
)) {
2593 list_del(&mcast
->list
);
2601 return ret
? ret
: in_len
;
2604 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING
2605 static int kern_spec_to_ib_spec(struct ib_kern_spec
*kern_spec
,
2606 union ib_flow_spec
*ib_spec
)
2608 ib_spec
->type
= kern_spec
->type
;
2610 switch (ib_spec
->type
) {
2611 case IB_FLOW_SPEC_ETH
:
2612 ib_spec
->eth
.size
= sizeof(struct ib_flow_spec_eth
);
2613 if (ib_spec
->eth
.size
!= kern_spec
->eth
.size
)
2615 memcpy(&ib_spec
->eth
.val
, &kern_spec
->eth
.val
,
2616 sizeof(struct ib_flow_eth_filter
));
2617 memcpy(&ib_spec
->eth
.mask
, &kern_spec
->eth
.mask
,
2618 sizeof(struct ib_flow_eth_filter
));
2620 case IB_FLOW_SPEC_IPV4
:
2621 ib_spec
->ipv4
.size
= sizeof(struct ib_flow_spec_ipv4
);
2622 if (ib_spec
->ipv4
.size
!= kern_spec
->ipv4
.size
)
2624 memcpy(&ib_spec
->ipv4
.val
, &kern_spec
->ipv4
.val
,
2625 sizeof(struct ib_flow_ipv4_filter
));
2626 memcpy(&ib_spec
->ipv4
.mask
, &kern_spec
->ipv4
.mask
,
2627 sizeof(struct ib_flow_ipv4_filter
));
2629 case IB_FLOW_SPEC_TCP
:
2630 case IB_FLOW_SPEC_UDP
:
2631 ib_spec
->tcp_udp
.size
= sizeof(struct ib_flow_spec_tcp_udp
);
2632 if (ib_spec
->tcp_udp
.size
!= kern_spec
->tcp_udp
.size
)
2634 memcpy(&ib_spec
->tcp_udp
.val
, &kern_spec
->tcp_udp
.val
,
2635 sizeof(struct ib_flow_tcp_udp_filter
));
2636 memcpy(&ib_spec
->tcp_udp
.mask
, &kern_spec
->tcp_udp
.mask
,
2637 sizeof(struct ib_flow_tcp_udp_filter
));
2645 ssize_t
ib_uverbs_create_flow(struct ib_uverbs_file
*file
,
2646 const char __user
*buf
, int in_len
,
2649 struct ib_uverbs_create_flow cmd
;
2650 struct ib_uverbs_create_flow_resp resp
;
2651 struct ib_uobject
*uobj
;
2652 struct ib_flow
*flow_id
;
2653 struct ib_kern_flow_attr
*kern_flow_attr
;
2654 struct ib_flow_attr
*flow_attr
;
2662 if (out_len
< sizeof(resp
))
2665 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
2671 if ((cmd
.flow_attr
.type
== IB_FLOW_ATTR_SNIFFER
&&
2672 !capable(CAP_NET_ADMIN
)) || !capable(CAP_NET_RAW
))
2675 if (cmd
.flow_attr
.num_of_specs
< 0 ||
2676 cmd
.flow_attr
.num_of_specs
> IB_FLOW_SPEC_SUPPORT_LAYERS
)
2679 kern_attr_size
= cmd
.flow_attr
.size
- sizeof(cmd
) -
2680 sizeof(struct ib_uverbs_cmd_hdr_ex
);
2682 if (cmd
.flow_attr
.size
< 0 || cmd
.flow_attr
.size
> in_len
||
2683 kern_attr_size
< 0 || kern_attr_size
>
2684 (cmd
.flow_attr
.num_of_specs
* sizeof(struct ib_kern_spec
)))
2687 if (cmd
.flow_attr
.num_of_specs
) {
2688 kern_flow_attr
= kmalloc(cmd
.flow_attr
.size
, GFP_KERNEL
);
2689 if (!kern_flow_attr
)
2692 memcpy(kern_flow_attr
, &cmd
.flow_attr
, sizeof(*kern_flow_attr
));
2693 if (copy_from_user(kern_flow_attr
+ 1, buf
+ sizeof(cmd
),
2699 kern_flow_attr
= &cmd
.flow_attr
;
2700 kern_attr_size
= sizeof(cmd
.flow_attr
);
2703 uobj
= kmalloc(sizeof(*uobj
), GFP_KERNEL
);
2708 init_uobj(uobj
, 0, file
->ucontext
, &rule_lock_class
);
2709 down_write(&uobj
->mutex
);
2711 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2717 flow_attr
= kmalloc(cmd
.flow_attr
.size
, GFP_KERNEL
);
2723 flow_attr
->type
= kern_flow_attr
->type
;
2724 flow_attr
->priority
= kern_flow_attr
->priority
;
2725 flow_attr
->num_of_specs
= kern_flow_attr
->num_of_specs
;
2726 flow_attr
->port
= kern_flow_attr
->port
;
2727 flow_attr
->flags
= kern_flow_attr
->flags
;
2728 flow_attr
->size
= sizeof(*flow_attr
);
2730 kern_spec
= kern_flow_attr
+ 1;
2731 ib_spec
= flow_attr
+ 1;
2732 for (i
= 0; i
< flow_attr
->num_of_specs
&& kern_attr_size
> 0; i
++) {
2733 err
= kern_spec_to_ib_spec(kern_spec
, ib_spec
);
2737 ((union ib_flow_spec
*) ib_spec
)->size
;
2738 kern_attr_size
-= ((struct ib_kern_spec
*) kern_spec
)->size
;
2739 kern_spec
+= ((struct ib_kern_spec
*) kern_spec
)->size
;
2740 ib_spec
+= ((union ib_flow_spec
*) ib_spec
)->size
;
2742 if (kern_attr_size
) {
2743 pr_warn("create flow failed, %d bytes left from uverb cmd\n",
2747 flow_id
= ib_create_flow(qp
, flow_attr
, IB_FLOW_DOMAIN_USER
);
2748 if (IS_ERR(flow_id
)) {
2749 err
= PTR_ERR(flow_id
);
2753 flow_id
->uobject
= uobj
;
2754 uobj
->object
= flow_id
;
2756 err
= idr_add_uobj(&ib_uverbs_rule_idr
, uobj
);
2760 memset(&resp
, 0, sizeof(resp
));
2761 resp
.flow_handle
= uobj
->id
;
2763 if (copy_to_user((void __user
*)(unsigned long) cmd
.response
,
2764 &resp
, sizeof(resp
))) {
2770 mutex_lock(&file
->mutex
);
2771 list_add_tail(&uobj
->list
, &file
->ucontext
->rule_list
);
2772 mutex_unlock(&file
->mutex
);
2776 up_write(&uobj
->mutex
);
2778 if (cmd
.flow_attr
.num_of_specs
)
2779 kfree(kern_flow_attr
);
2782 idr_remove_uobj(&ib_uverbs_rule_idr
, uobj
);
2784 ib_destroy_flow(flow_id
);
2790 put_uobj_write(uobj
);
2792 if (cmd
.flow_attr
.num_of_specs
)
2793 kfree(kern_flow_attr
);
2797 ssize_t
ib_uverbs_destroy_flow(struct ib_uverbs_file
*file
,
2798 const char __user
*buf
, int in_len
,
2800 struct ib_uverbs_destroy_flow cmd
;
2801 struct ib_flow
*flow_id
;
2802 struct ib_uobject
*uobj
;
2805 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
2808 uobj
= idr_write_uobj(&ib_uverbs_rule_idr
, cmd
.flow_handle
,
2812 flow_id
= uobj
->object
;
2814 ret
= ib_destroy_flow(flow_id
);
2818 put_uobj_write(uobj
);
2820 idr_remove_uobj(&ib_uverbs_rule_idr
, uobj
);
2822 mutex_lock(&file
->mutex
);
2823 list_del(&uobj
->list
);
2824 mutex_unlock(&file
->mutex
);
2828 return ret
? ret
: in_len
;
2830 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
2832 static int __uverbs_create_xsrq(struct ib_uverbs_file
*file
,
2833 struct ib_uverbs_create_xsrq
*cmd
,
2834 struct ib_udata
*udata
)
2836 struct ib_uverbs_create_srq_resp resp
;
2837 struct ib_usrq_object
*obj
;
2840 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
2841 struct ib_srq_init_attr attr
;
2844 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
2848 init_uobj(&obj
->uevent
.uobject
, cmd
->user_handle
, file
->ucontext
, &srq_lock_class
);
2849 down_write(&obj
->uevent
.uobject
.mutex
);
2851 if (cmd
->srq_type
== IB_SRQT_XRC
) {
2852 attr
.ext
.xrc
.xrcd
= idr_read_xrcd(cmd
->xrcd_handle
, file
->ucontext
, &xrcd_uobj
);
2853 if (!attr
.ext
.xrc
.xrcd
) {
2858 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
, uobject
);
2859 atomic_inc(&obj
->uxrcd
->refcnt
);
2861 attr
.ext
.xrc
.cq
= idr_read_cq(cmd
->cq_handle
, file
->ucontext
, 0);
2862 if (!attr
.ext
.xrc
.cq
) {
2868 pd
= idr_read_pd(cmd
->pd_handle
, file
->ucontext
);
2874 attr
.event_handler
= ib_uverbs_srq_event_handler
;
2875 attr
.srq_context
= file
;
2876 attr
.srq_type
= cmd
->srq_type
;
2877 attr
.attr
.max_wr
= cmd
->max_wr
;
2878 attr
.attr
.max_sge
= cmd
->max_sge
;
2879 attr
.attr
.srq_limit
= cmd
->srq_limit
;
2881 obj
->uevent
.events_reported
= 0;
2882 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
2884 srq
= pd
->device
->create_srq(pd
, &attr
, udata
);
2890 srq
->device
= pd
->device
;
2892 srq
->srq_type
= cmd
->srq_type
;
2893 srq
->uobject
= &obj
->uevent
.uobject
;
2894 srq
->event_handler
= attr
.event_handler
;
2895 srq
->srq_context
= attr
.srq_context
;
2897 if (cmd
->srq_type
== IB_SRQT_XRC
) {
2898 srq
->ext
.xrc
.cq
= attr
.ext
.xrc
.cq
;
2899 srq
->ext
.xrc
.xrcd
= attr
.ext
.xrc
.xrcd
;
2900 atomic_inc(&attr
.ext
.xrc
.cq
->usecnt
);
2901 atomic_inc(&attr
.ext
.xrc
.xrcd
->usecnt
);
2904 atomic_inc(&pd
->usecnt
);
2905 atomic_set(&srq
->usecnt
, 0);
2907 obj
->uevent
.uobject
.object
= srq
;
2908 ret
= idr_add_uobj(&ib_uverbs_srq_idr
, &obj
->uevent
.uobject
);
2912 memset(&resp
, 0, sizeof resp
);
2913 resp
.srq_handle
= obj
->uevent
.uobject
.id
;
2914 resp
.max_wr
= attr
.attr
.max_wr
;
2915 resp
.max_sge
= attr
.attr
.max_sge
;
2916 if (cmd
->srq_type
== IB_SRQT_XRC
)
2917 resp
.srqn
= srq
->ext
.xrc
.srq_num
;
2919 if (copy_to_user((void __user
*) (unsigned long) cmd
->response
,
2920 &resp
, sizeof resp
)) {
2925 if (cmd
->srq_type
== IB_SRQT_XRC
) {
2926 put_uobj_read(xrcd_uobj
);
2927 put_cq_read(attr
.ext
.xrc
.cq
);
2931 mutex_lock(&file
->mutex
);
2932 list_add_tail(&obj
->uevent
.uobject
.list
, &file
->ucontext
->srq_list
);
2933 mutex_unlock(&file
->mutex
);
2935 obj
->uevent
.uobject
.live
= 1;
2937 up_write(&obj
->uevent
.uobject
.mutex
);
2942 idr_remove_uobj(&ib_uverbs_srq_idr
, &obj
->uevent
.uobject
);
2945 ib_destroy_srq(srq
);
2951 if (cmd
->srq_type
== IB_SRQT_XRC
)
2952 put_cq_read(attr
.ext
.xrc
.cq
);
2955 if (cmd
->srq_type
== IB_SRQT_XRC
) {
2956 atomic_dec(&obj
->uxrcd
->refcnt
);
2957 put_uobj_read(xrcd_uobj
);
2961 put_uobj_write(&obj
->uevent
.uobject
);
2965 ssize_t
ib_uverbs_create_srq(struct ib_uverbs_file
*file
,
2966 const char __user
*buf
, int in_len
,
2969 struct ib_uverbs_create_srq cmd
;
2970 struct ib_uverbs_create_xsrq xcmd
;
2971 struct ib_uverbs_create_srq_resp resp
;
2972 struct ib_udata udata
;
2975 if (out_len
< sizeof resp
)
2978 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2981 xcmd
.response
= cmd
.response
;
2982 xcmd
.user_handle
= cmd
.user_handle
;
2983 xcmd
.srq_type
= IB_SRQT_BASIC
;
2984 xcmd
.pd_handle
= cmd
.pd_handle
;
2985 xcmd
.max_wr
= cmd
.max_wr
;
2986 xcmd
.max_sge
= cmd
.max_sge
;
2987 xcmd
.srq_limit
= cmd
.srq_limit
;
2989 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
2990 (unsigned long) cmd
.response
+ sizeof resp
,
2991 in_len
- sizeof cmd
, out_len
- sizeof resp
);
2993 ret
= __uverbs_create_xsrq(file
, &xcmd
, &udata
);
3000 ssize_t
ib_uverbs_create_xsrq(struct ib_uverbs_file
*file
,
3001 const char __user
*buf
, int in_len
, int out_len
)
3003 struct ib_uverbs_create_xsrq cmd
;
3004 struct ib_uverbs_create_srq_resp resp
;
3005 struct ib_udata udata
;
3008 if (out_len
< sizeof resp
)
3011 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3014 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
3015 (unsigned long) cmd
.response
+ sizeof resp
,
3016 in_len
- sizeof cmd
, out_len
- sizeof resp
);
3018 ret
= __uverbs_create_xsrq(file
, &cmd
, &udata
);
3025 ssize_t
ib_uverbs_modify_srq(struct ib_uverbs_file
*file
,
3026 const char __user
*buf
, int in_len
,
3029 struct ib_uverbs_modify_srq cmd
;
3030 struct ib_udata udata
;
3032 struct ib_srq_attr attr
;
3035 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3038 INIT_UDATA(&udata
, buf
+ sizeof cmd
, NULL
, in_len
- sizeof cmd
,
3041 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
3045 attr
.max_wr
= cmd
.max_wr
;
3046 attr
.srq_limit
= cmd
.srq_limit
;
3048 ret
= srq
->device
->modify_srq(srq
, &attr
, cmd
.attr_mask
, &udata
);
3052 return ret
? ret
: in_len
;
3055 ssize_t
ib_uverbs_query_srq(struct ib_uverbs_file
*file
,
3056 const char __user
*buf
,
3057 int in_len
, int out_len
)
3059 struct ib_uverbs_query_srq cmd
;
3060 struct ib_uverbs_query_srq_resp resp
;
3061 struct ib_srq_attr attr
;
3065 if (out_len
< sizeof resp
)
3068 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3071 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
3075 ret
= ib_query_srq(srq
, &attr
);
3082 memset(&resp
, 0, sizeof resp
);
3084 resp
.max_wr
= attr
.max_wr
;
3085 resp
.max_sge
= attr
.max_sge
;
3086 resp
.srq_limit
= attr
.srq_limit
;
3088 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
3089 &resp
, sizeof resp
))
3095 ssize_t
ib_uverbs_destroy_srq(struct ib_uverbs_file
*file
,
3096 const char __user
*buf
, int in_len
,
3099 struct ib_uverbs_destroy_srq cmd
;
3100 struct ib_uverbs_destroy_srq_resp resp
;
3101 struct ib_uobject
*uobj
;
3103 struct ib_uevent_object
*obj
;
3105 struct ib_usrq_object
*us
;
3106 enum ib_srq_type srq_type
;
3108 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3111 uobj
= idr_write_uobj(&ib_uverbs_srq_idr
, cmd
.srq_handle
, file
->ucontext
);
3115 obj
= container_of(uobj
, struct ib_uevent_object
, uobject
);
3116 srq_type
= srq
->srq_type
;
3118 ret
= ib_destroy_srq(srq
);
3122 put_uobj_write(uobj
);
3127 if (srq_type
== IB_SRQT_XRC
) {
3128 us
= container_of(obj
, struct ib_usrq_object
, uevent
);
3129 atomic_dec(&us
->uxrcd
->refcnt
);
3132 idr_remove_uobj(&ib_uverbs_srq_idr
, uobj
);
3134 mutex_lock(&file
->mutex
);
3135 list_del(&uobj
->list
);
3136 mutex_unlock(&file
->mutex
);
3138 ib_uverbs_release_uevent(file
, obj
);
3140 memset(&resp
, 0, sizeof resp
);
3141 resp
.events_reported
= obj
->events_reported
;
3145 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
3146 &resp
, sizeof resp
))
3149 return ret
? ret
: in_len
;