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>
39 #include <linux/sched.h>
41 #include <linux/uaccess.h>
43 #include <rdma/uverbs_types.h>
44 #include <rdma/uverbs_std_types.h>
45 #include "rdma_core.h"
48 #include "core_priv.h"
51 * Copy a response to userspace. If the provided 'resp' is larger than the
52 * user buffer it is silently truncated. If the user provided a larger buffer
53 * then the trailing portion is zero filled.
55 * These semantics are intended to support future extension of the output
58 static int uverbs_response(struct uverbs_attr_bundle
*attrs
, const void *resp
,
63 if (uverbs_attr_is_valid(attrs
, UVERBS_ATTR_CORE_OUT
))
64 return uverbs_copy_to_struct_or_zero(
65 attrs
, UVERBS_ATTR_CORE_OUT
, resp
, resp_len
);
67 if (copy_to_user(attrs
->ucore
.outbuf
, resp
,
68 min(attrs
->ucore
.outlen
, resp_len
)))
71 if (resp_len
< attrs
->ucore
.outlen
) {
73 * Zero fill any extra memory that user
74 * space might have provided.
76 ret
= clear_user(attrs
->ucore
.outbuf
+ resp_len
,
77 attrs
->ucore
.outlen
- resp_len
);
86 * Copy a request from userspace. If the provided 'req' is larger than the
87 * user buffer then the user buffer is zero extended into the 'req'. If 'req'
88 * is smaller than the user buffer then the uncopied bytes in the user buffer
91 static int uverbs_request(struct uverbs_attr_bundle
*attrs
, void *req
,
94 if (copy_from_user(req
, attrs
->ucore
.inbuf
,
95 min(attrs
->ucore
.inlen
, req_len
)))
98 if (attrs
->ucore
.inlen
< req_len
) {
99 memset(req
+ attrs
->ucore
.inlen
, 0,
100 req_len
- attrs
->ucore
.inlen
);
101 } else if (attrs
->ucore
.inlen
> req_len
) {
102 if (!ib_is_buffer_cleared(attrs
->ucore
.inbuf
+ req_len
,
103 attrs
->ucore
.inlen
- req_len
))
110 * Generate the value for the 'response_length' protocol used by write_ex.
111 * This is the number of bytes the kernel actually wrote. Userspace can use
112 * this to detect what structure members in the response the kernel
115 static u32
uverbs_response_length(struct uverbs_attr_bundle
*attrs
,
118 return min_t(size_t, attrs
->ucore
.outlen
, resp_len
);
122 * The iterator version of the request interface is for handlers that need to
123 * step over a flex array at the end of a command header.
125 struct uverbs_req_iter
{
126 const void __user
*cur
;
127 const void __user
*end
;
130 static int uverbs_request_start(struct uverbs_attr_bundle
*attrs
,
131 struct uverbs_req_iter
*iter
,
135 if (attrs
->ucore
.inlen
< req_len
)
138 if (copy_from_user(req
, attrs
->ucore
.inbuf
, req_len
))
141 iter
->cur
= attrs
->ucore
.inbuf
+ req_len
;
142 iter
->end
= attrs
->ucore
.inbuf
+ attrs
->ucore
.inlen
;
146 static int uverbs_request_next(struct uverbs_req_iter
*iter
, void *val
,
149 if (iter
->cur
+ len
> iter
->end
)
152 if (copy_from_user(val
, iter
->cur
, len
))
159 static const void __user
*uverbs_request_next_ptr(struct uverbs_req_iter
*iter
,
162 const void __user
*res
= iter
->cur
;
164 if (iter
->cur
+ len
> iter
->end
)
165 return (void __force __user
*)ERR_PTR(-ENOSPC
);
170 static int uverbs_request_finish(struct uverbs_req_iter
*iter
)
172 if (!ib_is_buffer_cleared(iter
->cur
, iter
->end
- iter
->cur
))
178 * When calling a destroy function during an error unwind we need to pass in
179 * the udata that is sanitized of all user arguments. Ie from the driver
180 * perspective it looks like no udata was passed.
182 struct ib_udata
*uverbs_get_cleared_udata(struct uverbs_attr_bundle
*attrs
)
184 attrs
->driver_udata
= (struct ib_udata
){};
185 return &attrs
->driver_udata
;
188 static struct ib_uverbs_completion_event_file
*
189 _ib_uverbs_lookup_comp_file(s32 fd
, struct uverbs_attr_bundle
*attrs
)
191 struct ib_uobject
*uobj
= ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL
,
197 uverbs_uobject_get(uobj
);
200 return container_of(uobj
, struct ib_uverbs_completion_event_file
,
203 #define ib_uverbs_lookup_comp_file(_fd, _ufile) \
204 _ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
206 int ib_alloc_ucontext(struct uverbs_attr_bundle
*attrs
)
208 struct ib_uverbs_file
*ufile
= attrs
->ufile
;
209 struct ib_ucontext
*ucontext
;
210 struct ib_device
*ib_dev
;
212 ib_dev
= srcu_dereference(ufile
->device
->ib_dev
,
213 &ufile
->device
->disassociate_srcu
);
217 ucontext
= rdma_zalloc_drv_obj(ib_dev
, ib_ucontext
);
221 ucontext
->res
.type
= RDMA_RESTRACK_CTX
;
222 ucontext
->device
= ib_dev
;
223 ucontext
->ufile
= ufile
;
224 xa_init_flags(&ucontext
->mmap_xa
, XA_FLAGS_ALLOC
);
225 attrs
->context
= ucontext
;
229 int ib_init_ucontext(struct uverbs_attr_bundle
*attrs
)
231 struct ib_ucontext
*ucontext
= attrs
->context
;
232 struct ib_uverbs_file
*file
= attrs
->ufile
;
235 if (!down_read_trylock(&file
->hw_destroy_rwsem
))
237 mutex_lock(&file
->ucontext_lock
);
238 if (file
->ucontext
) {
243 ret
= ib_rdmacg_try_charge(&ucontext
->cg_obj
, ucontext
->device
,
244 RDMACG_RESOURCE_HCA_HANDLE
);
248 ret
= ucontext
->device
->ops
.alloc_ucontext(ucontext
,
249 &attrs
->driver_udata
);
253 rdma_restrack_uadd(&ucontext
->res
);
256 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
257 * only after all writes to setup the ucontext have completed
259 smp_store_release(&file
->ucontext
, ucontext
);
261 mutex_unlock(&file
->ucontext_lock
);
262 up_read(&file
->hw_destroy_rwsem
);
266 ib_rdmacg_uncharge(&ucontext
->cg_obj
, ucontext
->device
,
267 RDMACG_RESOURCE_HCA_HANDLE
);
269 mutex_unlock(&file
->ucontext_lock
);
270 up_read(&file
->hw_destroy_rwsem
);
274 static int ib_uverbs_get_context(struct uverbs_attr_bundle
*attrs
)
276 struct ib_uverbs_get_context_resp resp
;
277 struct ib_uverbs_get_context cmd
;
278 struct ib_device
*ib_dev
;
279 struct ib_uobject
*uobj
;
282 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
286 ret
= ib_alloc_ucontext(attrs
);
290 uobj
= uobj_alloc(UVERBS_OBJECT_ASYNC_EVENT
, attrs
, &ib_dev
);
296 resp
= (struct ib_uverbs_get_context_resp
){
297 .num_comp_vectors
= attrs
->ufile
->device
->num_comp_vectors
,
298 .async_fd
= uobj
->id
,
300 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
304 ret
= ib_init_ucontext(attrs
);
308 ib_uverbs_init_async_event_file(
309 container_of(uobj
, struct ib_uverbs_async_event_file
, uobj
));
310 rdma_alloc_commit_uobject(uobj
, attrs
);
314 rdma_alloc_abort_uobject(uobj
, attrs
);
316 kfree(attrs
->context
);
317 attrs
->context
= NULL
;
321 static void copy_query_dev_fields(struct ib_ucontext
*ucontext
,
322 struct ib_uverbs_query_device_resp
*resp
,
323 struct ib_device_attr
*attr
)
325 struct ib_device
*ib_dev
= ucontext
->device
;
327 resp
->fw_ver
= attr
->fw_ver
;
328 resp
->node_guid
= ib_dev
->node_guid
;
329 resp
->sys_image_guid
= attr
->sys_image_guid
;
330 resp
->max_mr_size
= attr
->max_mr_size
;
331 resp
->page_size_cap
= attr
->page_size_cap
;
332 resp
->vendor_id
= attr
->vendor_id
;
333 resp
->vendor_part_id
= attr
->vendor_part_id
;
334 resp
->hw_ver
= attr
->hw_ver
;
335 resp
->max_qp
= attr
->max_qp
;
336 resp
->max_qp_wr
= attr
->max_qp_wr
;
337 resp
->device_cap_flags
= lower_32_bits(attr
->device_cap_flags
);
338 resp
->max_sge
= min(attr
->max_send_sge
, attr
->max_recv_sge
);
339 resp
->max_sge_rd
= attr
->max_sge_rd
;
340 resp
->max_cq
= attr
->max_cq
;
341 resp
->max_cqe
= attr
->max_cqe
;
342 resp
->max_mr
= attr
->max_mr
;
343 resp
->max_pd
= attr
->max_pd
;
344 resp
->max_qp_rd_atom
= attr
->max_qp_rd_atom
;
345 resp
->max_ee_rd_atom
= attr
->max_ee_rd_atom
;
346 resp
->max_res_rd_atom
= attr
->max_res_rd_atom
;
347 resp
->max_qp_init_rd_atom
= attr
->max_qp_init_rd_atom
;
348 resp
->max_ee_init_rd_atom
= attr
->max_ee_init_rd_atom
;
349 resp
->atomic_cap
= attr
->atomic_cap
;
350 resp
->max_ee
= attr
->max_ee
;
351 resp
->max_rdd
= attr
->max_rdd
;
352 resp
->max_mw
= attr
->max_mw
;
353 resp
->max_raw_ipv6_qp
= attr
->max_raw_ipv6_qp
;
354 resp
->max_raw_ethy_qp
= attr
->max_raw_ethy_qp
;
355 resp
->max_mcast_grp
= attr
->max_mcast_grp
;
356 resp
->max_mcast_qp_attach
= attr
->max_mcast_qp_attach
;
357 resp
->max_total_mcast_qp_attach
= attr
->max_total_mcast_qp_attach
;
358 resp
->max_ah
= attr
->max_ah
;
359 resp
->max_fmr
= attr
->max_fmr
;
360 resp
->max_map_per_fmr
= attr
->max_map_per_fmr
;
361 resp
->max_srq
= attr
->max_srq
;
362 resp
->max_srq_wr
= attr
->max_srq_wr
;
363 resp
->max_srq_sge
= attr
->max_srq_sge
;
364 resp
->max_pkeys
= attr
->max_pkeys
;
365 resp
->local_ca_ack_delay
= attr
->local_ca_ack_delay
;
366 resp
->phys_port_cnt
= ib_dev
->phys_port_cnt
;
369 static int ib_uverbs_query_device(struct uverbs_attr_bundle
*attrs
)
371 struct ib_uverbs_query_device cmd
;
372 struct ib_uverbs_query_device_resp resp
;
373 struct ib_ucontext
*ucontext
;
376 ucontext
= ib_uverbs_get_ucontext(attrs
);
377 if (IS_ERR(ucontext
))
378 return PTR_ERR(ucontext
);
380 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
384 memset(&resp
, 0, sizeof resp
);
385 copy_query_dev_fields(ucontext
, &resp
, &ucontext
->device
->attrs
);
387 return uverbs_response(attrs
, &resp
, sizeof(resp
));
390 static int ib_uverbs_query_port(struct uverbs_attr_bundle
*attrs
)
392 struct ib_uverbs_query_port cmd
;
393 struct ib_uverbs_query_port_resp resp
;
394 struct ib_port_attr attr
;
396 struct ib_ucontext
*ucontext
;
397 struct ib_device
*ib_dev
;
399 ucontext
= ib_uverbs_get_ucontext(attrs
);
400 if (IS_ERR(ucontext
))
401 return PTR_ERR(ucontext
);
402 ib_dev
= ucontext
->device
;
404 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
408 ret
= ib_query_port(ib_dev
, cmd
.port_num
, &attr
);
412 memset(&resp
, 0, sizeof resp
);
413 copy_port_attr_to_resp(&attr
, &resp
, ib_dev
, cmd
.port_num
);
415 return uverbs_response(attrs
, &resp
, sizeof(resp
));
418 static int ib_uverbs_alloc_pd(struct uverbs_attr_bundle
*attrs
)
420 struct ib_uverbs_alloc_pd cmd
;
421 struct ib_uverbs_alloc_pd_resp resp
;
422 struct ib_uobject
*uobj
;
425 struct ib_device
*ib_dev
;
427 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
431 uobj
= uobj_alloc(UVERBS_OBJECT_PD
, attrs
, &ib_dev
);
433 return PTR_ERR(uobj
);
435 pd
= rdma_zalloc_drv_obj(ib_dev
, ib_pd
);
443 pd
->__internal_mr
= NULL
;
444 atomic_set(&pd
->usecnt
, 0);
445 pd
->res
.type
= RDMA_RESTRACK_PD
;
447 ret
= ib_dev
->ops
.alloc_pd(pd
, &attrs
->driver_udata
);
452 memset(&resp
, 0, sizeof resp
);
453 resp
.pd_handle
= uobj
->id
;
454 rdma_restrack_uadd(&pd
->res
);
456 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
460 rdma_alloc_commit_uobject(uobj
, attrs
);
464 ib_dealloc_pd_user(pd
, uverbs_get_cleared_udata(attrs
));
469 uobj_alloc_abort(uobj
, attrs
);
473 static int ib_uverbs_dealloc_pd(struct uverbs_attr_bundle
*attrs
)
475 struct ib_uverbs_dealloc_pd cmd
;
478 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
482 return uobj_perform_destroy(UVERBS_OBJECT_PD
, cmd
.pd_handle
, attrs
);
485 struct xrcd_table_entry
{
487 struct ib_xrcd
*xrcd
;
491 static int xrcd_table_insert(struct ib_uverbs_device
*dev
,
493 struct ib_xrcd
*xrcd
)
495 struct xrcd_table_entry
*entry
, *scan
;
496 struct rb_node
**p
= &dev
->xrcd_tree
.rb_node
;
497 struct rb_node
*parent
= NULL
;
499 entry
= kmalloc(sizeof *entry
, GFP_KERNEL
);
504 entry
->inode
= inode
;
508 scan
= rb_entry(parent
, struct xrcd_table_entry
, node
);
510 if (inode
< scan
->inode
) {
512 } else if (inode
> scan
->inode
) {
520 rb_link_node(&entry
->node
, parent
, p
);
521 rb_insert_color(&entry
->node
, &dev
->xrcd_tree
);
526 static struct xrcd_table_entry
*xrcd_table_search(struct ib_uverbs_device
*dev
,
529 struct xrcd_table_entry
*entry
;
530 struct rb_node
*p
= dev
->xrcd_tree
.rb_node
;
533 entry
= rb_entry(p
, struct xrcd_table_entry
, node
);
535 if (inode
< entry
->inode
)
537 else if (inode
> entry
->inode
)
546 static struct ib_xrcd
*find_xrcd(struct ib_uverbs_device
*dev
, struct inode
*inode
)
548 struct xrcd_table_entry
*entry
;
550 entry
= xrcd_table_search(dev
, inode
);
557 static void xrcd_table_delete(struct ib_uverbs_device
*dev
,
560 struct xrcd_table_entry
*entry
;
562 entry
= xrcd_table_search(dev
, inode
);
565 rb_erase(&entry
->node
, &dev
->xrcd_tree
);
570 static int ib_uverbs_open_xrcd(struct uverbs_attr_bundle
*attrs
)
572 struct ib_uverbs_device
*ibudev
= attrs
->ufile
->device
;
573 struct ib_uverbs_open_xrcd cmd
;
574 struct ib_uverbs_open_xrcd_resp resp
;
575 struct ib_uxrcd_object
*obj
;
576 struct ib_xrcd
*xrcd
= NULL
;
577 struct fd f
= {NULL
, 0};
578 struct inode
*inode
= NULL
;
581 struct ib_device
*ib_dev
;
583 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
587 mutex_lock(&ibudev
->xrcd_tree_mutex
);
590 /* search for file descriptor */
594 goto err_tree_mutex_unlock
;
597 inode
= file_inode(f
.file
);
598 xrcd
= find_xrcd(ibudev
, inode
);
599 if (!xrcd
&& !(cmd
.oflags
& O_CREAT
)) {
600 /* no file descriptor. Need CREATE flag */
602 goto err_tree_mutex_unlock
;
605 if (xrcd
&& cmd
.oflags
& O_EXCL
) {
607 goto err_tree_mutex_unlock
;
611 obj
= (struct ib_uxrcd_object
*)uobj_alloc(UVERBS_OBJECT_XRCD
, attrs
,
615 goto err_tree_mutex_unlock
;
619 xrcd
= ib_dev
->ops
.alloc_xrcd(ib_dev
, &attrs
->driver_udata
);
626 xrcd
->device
= ib_dev
;
627 atomic_set(&xrcd
->usecnt
, 0);
628 mutex_init(&xrcd
->tgt_qp_mutex
);
629 INIT_LIST_HEAD(&xrcd
->tgt_qp_list
);
633 atomic_set(&obj
->refcnt
, 0);
634 obj
->uobject
.object
= xrcd
;
635 memset(&resp
, 0, sizeof resp
);
636 resp
.xrcd_handle
= obj
->uobject
.id
;
640 /* create new inode/xrcd table entry */
641 ret
= xrcd_table_insert(ibudev
, inode
, xrcd
);
643 goto err_dealloc_xrcd
;
645 atomic_inc(&xrcd
->usecnt
);
648 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
655 mutex_unlock(&ibudev
->xrcd_tree_mutex
);
657 rdma_alloc_commit_uobject(&obj
->uobject
, attrs
);
663 xrcd_table_delete(ibudev
, inode
);
664 atomic_dec(&xrcd
->usecnt
);
668 ib_dealloc_xrcd(xrcd
, uverbs_get_cleared_udata(attrs
));
671 uobj_alloc_abort(&obj
->uobject
, attrs
);
673 err_tree_mutex_unlock
:
677 mutex_unlock(&ibudev
->xrcd_tree_mutex
);
682 static int ib_uverbs_close_xrcd(struct uverbs_attr_bundle
*attrs
)
684 struct ib_uverbs_close_xrcd cmd
;
687 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
691 return uobj_perform_destroy(UVERBS_OBJECT_XRCD
, cmd
.xrcd_handle
, attrs
);
694 int ib_uverbs_dealloc_xrcd(struct ib_uobject
*uobject
, struct ib_xrcd
*xrcd
,
695 enum rdma_remove_reason why
,
696 struct uverbs_attr_bundle
*attrs
)
700 struct ib_uverbs_device
*dev
= attrs
->ufile
->device
;
703 if (inode
&& !atomic_dec_and_test(&xrcd
->usecnt
))
706 ret
= ib_dealloc_xrcd(xrcd
, &attrs
->driver_udata
);
708 if (ib_is_destroy_retryable(ret
, why
, uobject
)) {
709 atomic_inc(&xrcd
->usecnt
);
714 xrcd_table_delete(dev
, inode
);
719 static int ib_uverbs_reg_mr(struct uverbs_attr_bundle
*attrs
)
721 struct ib_uverbs_reg_mr cmd
;
722 struct ib_uverbs_reg_mr_resp resp
;
723 struct ib_uobject
*uobj
;
727 struct ib_device
*ib_dev
;
729 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
733 if ((cmd
.start
& ~PAGE_MASK
) != (cmd
.hca_va
& ~PAGE_MASK
))
736 ret
= ib_check_mr_access(cmd
.access_flags
);
740 uobj
= uobj_alloc(UVERBS_OBJECT_MR
, attrs
, &ib_dev
);
742 return PTR_ERR(uobj
);
744 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
.pd_handle
, attrs
);
750 if (cmd
.access_flags
& IB_ACCESS_ON_DEMAND
) {
751 if (!(pd
->device
->attrs
.device_cap_flags
&
752 IB_DEVICE_ON_DEMAND_PAGING
)) {
753 pr_debug("ODP support not available\n");
759 mr
= pd
->device
->ops
.reg_user_mr(pd
, cmd
.start
, cmd
.length
, cmd
.hca_va
,
761 &attrs
->driver_udata
);
767 mr
->device
= pd
->device
;
769 mr
->type
= IB_MR_TYPE_USER
;
771 mr
->sig_attrs
= NULL
;
773 atomic_inc(&pd
->usecnt
);
774 mr
->res
.type
= RDMA_RESTRACK_MR
;
775 rdma_restrack_uadd(&mr
->res
);
779 memset(&resp
, 0, sizeof resp
);
780 resp
.lkey
= mr
->lkey
;
781 resp
.rkey
= mr
->rkey
;
782 resp
.mr_handle
= uobj
->id
;
784 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
788 uobj_put_obj_read(pd
);
790 rdma_alloc_commit_uobject(uobj
, attrs
);
794 ib_dereg_mr_user(mr
, uverbs_get_cleared_udata(attrs
));
797 uobj_put_obj_read(pd
);
800 uobj_alloc_abort(uobj
, attrs
);
804 static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle
*attrs
)
806 struct ib_uverbs_rereg_mr cmd
;
807 struct ib_uverbs_rereg_mr_resp resp
;
808 struct ib_pd
*pd
= NULL
;
810 struct ib_pd
*old_pd
;
812 struct ib_uobject
*uobj
;
814 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
818 if (cmd
.flags
& ~IB_MR_REREG_SUPPORTED
|| !cmd
.flags
)
821 if ((cmd
.flags
& IB_MR_REREG_TRANS
) &&
822 (!cmd
.start
|| !cmd
.hca_va
|| 0 >= cmd
.length
||
823 (cmd
.start
& ~PAGE_MASK
) != (cmd
.hca_va
& ~PAGE_MASK
)))
826 uobj
= uobj_get_write(UVERBS_OBJECT_MR
, cmd
.mr_handle
, attrs
);
828 return PTR_ERR(uobj
);
837 if (cmd
.flags
& IB_MR_REREG_ACCESS
) {
838 ret
= ib_check_mr_access(cmd
.access_flags
);
843 if (cmd
.flags
& IB_MR_REREG_PD
) {
844 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
.pd_handle
,
853 ret
= mr
->device
->ops
.rereg_user_mr(mr
, cmd
.flags
, cmd
.start
,
854 cmd
.length
, cmd
.hca_va
,
855 cmd
.access_flags
, pd
,
856 &attrs
->driver_udata
);
860 if (cmd
.flags
& IB_MR_REREG_PD
) {
861 atomic_inc(&pd
->usecnt
);
863 atomic_dec(&old_pd
->usecnt
);
866 memset(&resp
, 0, sizeof(resp
));
867 resp
.lkey
= mr
->lkey
;
868 resp
.rkey
= mr
->rkey
;
870 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
873 if (cmd
.flags
& IB_MR_REREG_PD
)
874 uobj_put_obj_read(pd
);
877 uobj_put_write(uobj
);
882 static int ib_uverbs_dereg_mr(struct uverbs_attr_bundle
*attrs
)
884 struct ib_uverbs_dereg_mr cmd
;
887 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
891 return uobj_perform_destroy(UVERBS_OBJECT_MR
, cmd
.mr_handle
, attrs
);
894 static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle
*attrs
)
896 struct ib_uverbs_alloc_mw cmd
;
897 struct ib_uverbs_alloc_mw_resp resp
;
898 struct ib_uobject
*uobj
;
902 struct ib_device
*ib_dev
;
904 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
908 uobj
= uobj_alloc(UVERBS_OBJECT_MW
, attrs
, &ib_dev
);
910 return PTR_ERR(uobj
);
912 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
.pd_handle
, attrs
);
918 if (cmd
.mw_type
!= IB_MW_TYPE_1
&& cmd
.mw_type
!= IB_MW_TYPE_2
) {
923 mw
= pd
->device
->ops
.alloc_mw(pd
, cmd
.mw_type
, &attrs
->driver_udata
);
929 mw
->device
= pd
->device
;
932 atomic_inc(&pd
->usecnt
);
936 memset(&resp
, 0, sizeof(resp
));
937 resp
.rkey
= mw
->rkey
;
938 resp
.mw_handle
= uobj
->id
;
940 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
944 uobj_put_obj_read(pd
);
945 rdma_alloc_commit_uobject(uobj
, attrs
);
949 uverbs_dealloc_mw(mw
);
951 uobj_put_obj_read(pd
);
953 uobj_alloc_abort(uobj
, attrs
);
957 static int ib_uverbs_dealloc_mw(struct uverbs_attr_bundle
*attrs
)
959 struct ib_uverbs_dealloc_mw cmd
;
962 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
966 return uobj_perform_destroy(UVERBS_OBJECT_MW
, cmd
.mw_handle
, attrs
);
969 static int ib_uverbs_create_comp_channel(struct uverbs_attr_bundle
*attrs
)
971 struct ib_uverbs_create_comp_channel cmd
;
972 struct ib_uverbs_create_comp_channel_resp resp
;
973 struct ib_uobject
*uobj
;
974 struct ib_uverbs_completion_event_file
*ev_file
;
975 struct ib_device
*ib_dev
;
978 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
982 uobj
= uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL
, attrs
, &ib_dev
);
984 return PTR_ERR(uobj
);
988 ev_file
= container_of(uobj
, struct ib_uverbs_completion_event_file
,
990 ib_uverbs_init_event_queue(&ev_file
->ev_queue
);
992 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
994 uobj_alloc_abort(uobj
, attrs
);
998 rdma_alloc_commit_uobject(uobj
, attrs
);
1002 static struct ib_ucq_object
*create_cq(struct uverbs_attr_bundle
*attrs
,
1003 struct ib_uverbs_ex_create_cq
*cmd
)
1005 struct ib_ucq_object
*obj
;
1006 struct ib_uverbs_completion_event_file
*ev_file
= NULL
;
1009 struct ib_uverbs_ex_create_cq_resp resp
;
1010 struct ib_cq_init_attr attr
= {};
1011 struct ib_device
*ib_dev
;
1013 if (cmd
->comp_vector
>= attrs
->ufile
->device
->num_comp_vectors
)
1014 return ERR_PTR(-EINVAL
);
1016 obj
= (struct ib_ucq_object
*)uobj_alloc(UVERBS_OBJECT_CQ
, attrs
,
1021 if (cmd
->comp_channel
>= 0) {
1022 ev_file
= ib_uverbs_lookup_comp_file(cmd
->comp_channel
, attrs
);
1023 if (IS_ERR(ev_file
)) {
1024 ret
= PTR_ERR(ev_file
);
1029 obj
->uevent
.uobject
.user_handle
= cmd
->user_handle
;
1030 INIT_LIST_HEAD(&obj
->comp_list
);
1031 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
1033 attr
.cqe
= cmd
->cqe
;
1034 attr
.comp_vector
= cmd
->comp_vector
;
1035 attr
.flags
= cmd
->flags
;
1037 cq
= rdma_zalloc_drv_obj(ib_dev
, ib_cq
);
1042 cq
->device
= ib_dev
;
1044 cq
->comp_handler
= ib_uverbs_comp_handler
;
1045 cq
->event_handler
= ib_uverbs_cq_event_handler
;
1046 cq
->cq_context
= ev_file
? &ev_file
->ev_queue
: NULL
;
1047 atomic_set(&cq
->usecnt
, 0);
1049 ret
= ib_dev
->ops
.create_cq(cq
, &attr
, &attrs
->driver_udata
);
1053 obj
->uevent
.uobject
.object
= cq
;
1054 memset(&resp
, 0, sizeof resp
);
1055 resp
.base
.cq_handle
= obj
->uevent
.uobject
.id
;
1056 resp
.base
.cqe
= cq
->cqe
;
1057 resp
.response_length
= uverbs_response_length(attrs
, sizeof(resp
));
1059 cq
->res
.type
= RDMA_RESTRACK_CQ
;
1060 rdma_restrack_uadd(&cq
->res
);
1062 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
1066 rdma_alloc_commit_uobject(&obj
->uevent
.uobject
, attrs
);
1070 ib_destroy_cq_user(cq
, uverbs_get_cleared_udata(attrs
));
1076 ib_uverbs_release_ucq(ev_file
, obj
);
1079 uobj_alloc_abort(&obj
->uevent
.uobject
, attrs
);
1081 return ERR_PTR(ret
);
1084 static int ib_uverbs_create_cq(struct uverbs_attr_bundle
*attrs
)
1086 struct ib_uverbs_create_cq cmd
;
1087 struct ib_uverbs_ex_create_cq cmd_ex
;
1088 struct ib_ucq_object
*obj
;
1091 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1095 memset(&cmd_ex
, 0, sizeof(cmd_ex
));
1096 cmd_ex
.user_handle
= cmd
.user_handle
;
1097 cmd_ex
.cqe
= cmd
.cqe
;
1098 cmd_ex
.comp_vector
= cmd
.comp_vector
;
1099 cmd_ex
.comp_channel
= cmd
.comp_channel
;
1101 obj
= create_cq(attrs
, &cmd_ex
);
1102 return PTR_ERR_OR_ZERO(obj
);
1105 static int ib_uverbs_ex_create_cq(struct uverbs_attr_bundle
*attrs
)
1107 struct ib_uverbs_ex_create_cq cmd
;
1108 struct ib_ucq_object
*obj
;
1111 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1121 obj
= create_cq(attrs
, &cmd
);
1122 return PTR_ERR_OR_ZERO(obj
);
1125 static int ib_uverbs_resize_cq(struct uverbs_attr_bundle
*attrs
)
1127 struct ib_uverbs_resize_cq cmd
;
1128 struct ib_uverbs_resize_cq_resp resp
= {};
1132 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1136 cq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
, cmd
.cq_handle
, attrs
);
1140 ret
= cq
->device
->ops
.resize_cq(cq
, cmd
.cqe
, &attrs
->driver_udata
);
1146 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
1148 rdma_lookup_put_uobject(&cq
->uobject
->uevent
.uobject
,
1149 UVERBS_LOOKUP_READ
);
1154 static int copy_wc_to_user(struct ib_device
*ib_dev
, void __user
*dest
,
1157 struct ib_uverbs_wc tmp
;
1159 tmp
.wr_id
= wc
->wr_id
;
1160 tmp
.status
= wc
->status
;
1161 tmp
.opcode
= wc
->opcode
;
1162 tmp
.vendor_err
= wc
->vendor_err
;
1163 tmp
.byte_len
= wc
->byte_len
;
1164 tmp
.ex
.imm_data
= wc
->ex
.imm_data
;
1165 tmp
.qp_num
= wc
->qp
->qp_num
;
1166 tmp
.src_qp
= wc
->src_qp
;
1167 tmp
.wc_flags
= wc
->wc_flags
;
1168 tmp
.pkey_index
= wc
->pkey_index
;
1169 if (rdma_cap_opa_ah(ib_dev
, wc
->port_num
))
1170 tmp
.slid
= OPA_TO_IB_UCAST_LID(wc
->slid
);
1172 tmp
.slid
= ib_lid_cpu16(wc
->slid
);
1174 tmp
.dlid_path_bits
= wc
->dlid_path_bits
;
1175 tmp
.port_num
= wc
->port_num
;
1178 if (copy_to_user(dest
, &tmp
, sizeof tmp
))
1184 static int ib_uverbs_poll_cq(struct uverbs_attr_bundle
*attrs
)
1186 struct ib_uverbs_poll_cq cmd
;
1187 struct ib_uverbs_poll_cq_resp resp
;
1188 u8 __user
*header_ptr
;
1189 u8 __user
*data_ptr
;
1194 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1198 cq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
, cmd
.cq_handle
, attrs
);
1202 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1203 header_ptr
= attrs
->ucore
.outbuf
;
1204 data_ptr
= header_ptr
+ sizeof resp
;
1206 memset(&resp
, 0, sizeof resp
);
1207 while (resp
.count
< cmd
.ne
) {
1208 ret
= ib_poll_cq(cq
, 1, &wc
);
1214 ret
= copy_wc_to_user(cq
->device
, data_ptr
, &wc
);
1218 data_ptr
+= sizeof(struct ib_uverbs_wc
);
1222 if (copy_to_user(header_ptr
, &resp
, sizeof resp
)) {
1228 if (uverbs_attr_is_valid(attrs
, UVERBS_ATTR_CORE_OUT
))
1229 ret
= uverbs_output_written(attrs
, UVERBS_ATTR_CORE_OUT
);
1232 rdma_lookup_put_uobject(&cq
->uobject
->uevent
.uobject
,
1233 UVERBS_LOOKUP_READ
);
1237 static int ib_uverbs_req_notify_cq(struct uverbs_attr_bundle
*attrs
)
1239 struct ib_uverbs_req_notify_cq cmd
;
1243 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1247 cq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
, cmd
.cq_handle
, attrs
);
1251 ib_req_notify_cq(cq
, cmd
.solicited_only
?
1252 IB_CQ_SOLICITED
: IB_CQ_NEXT_COMP
);
1254 rdma_lookup_put_uobject(&cq
->uobject
->uevent
.uobject
,
1255 UVERBS_LOOKUP_READ
);
1259 static int ib_uverbs_destroy_cq(struct uverbs_attr_bundle
*attrs
)
1261 struct ib_uverbs_destroy_cq cmd
;
1262 struct ib_uverbs_destroy_cq_resp resp
;
1263 struct ib_uobject
*uobj
;
1264 struct ib_ucq_object
*obj
;
1267 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1271 uobj
= uobj_get_destroy(UVERBS_OBJECT_CQ
, cmd
.cq_handle
, attrs
);
1273 return PTR_ERR(uobj
);
1275 obj
= container_of(uobj
, struct ib_ucq_object
, uevent
.uobject
);
1276 memset(&resp
, 0, sizeof(resp
));
1277 resp
.comp_events_reported
= obj
->comp_events_reported
;
1278 resp
.async_events_reported
= obj
->uevent
.events_reported
;
1280 uobj_put_destroy(uobj
);
1282 return uverbs_response(attrs
, &resp
, sizeof(resp
));
1285 static int create_qp(struct uverbs_attr_bundle
*attrs
,
1286 struct ib_uverbs_ex_create_qp
*cmd
)
1288 struct ib_uqp_object
*obj
;
1289 struct ib_device
*device
;
1290 struct ib_pd
*pd
= NULL
;
1291 struct ib_xrcd
*xrcd
= NULL
;
1292 struct ib_uobject
*xrcd_uobj
= ERR_PTR(-ENOENT
);
1293 struct ib_cq
*scq
= NULL
, *rcq
= NULL
;
1294 struct ib_srq
*srq
= NULL
;
1296 struct ib_qp_init_attr attr
= {};
1297 struct ib_uverbs_ex_create_qp_resp resp
;
1299 struct ib_rwq_ind_table
*ind_tbl
= NULL
;
1301 struct ib_device
*ib_dev
;
1303 if (cmd
->qp_type
== IB_QPT_RAW_PACKET
&& !capable(CAP_NET_RAW
))
1306 obj
= (struct ib_uqp_object
*)uobj_alloc(UVERBS_OBJECT_QP
, attrs
,
1309 return PTR_ERR(obj
);
1311 obj
->uevent
.uobject
.user_handle
= cmd
->user_handle
;
1312 mutex_init(&obj
->mcast_lock
);
1314 if (cmd
->comp_mask
& IB_UVERBS_CREATE_QP_MASK_IND_TABLE
) {
1315 ind_tbl
= uobj_get_obj_read(rwq_ind_table
,
1316 UVERBS_OBJECT_RWQ_IND_TBL
,
1317 cmd
->rwq_ind_tbl_handle
, attrs
);
1323 attr
.rwq_ind_tbl
= ind_tbl
;
1326 if (ind_tbl
&& (cmd
->max_recv_wr
|| cmd
->max_recv_sge
|| cmd
->is_srq
)) {
1331 if (ind_tbl
&& !cmd
->max_send_wr
)
1334 if (cmd
->qp_type
== IB_QPT_XRC_TGT
) {
1335 xrcd_uobj
= uobj_get_read(UVERBS_OBJECT_XRCD
, cmd
->pd_handle
,
1338 if (IS_ERR(xrcd_uobj
)) {
1343 xrcd
= (struct ib_xrcd
*)xrcd_uobj
->object
;
1348 device
= xrcd
->device
;
1350 if (cmd
->qp_type
== IB_QPT_XRC_INI
) {
1351 cmd
->max_recv_wr
= 0;
1352 cmd
->max_recv_sge
= 0;
1355 srq
= uobj_get_obj_read(srq
, UVERBS_OBJECT_SRQ
,
1356 cmd
->srq_handle
, attrs
);
1357 if (!srq
|| srq
->srq_type
== IB_SRQT_XRC
) {
1364 if (cmd
->recv_cq_handle
!= cmd
->send_cq_handle
) {
1365 rcq
= uobj_get_obj_read(
1366 cq
, UVERBS_OBJECT_CQ
,
1367 cmd
->recv_cq_handle
, attrs
);
1377 scq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
,
1378 cmd
->send_cq_handle
, attrs
);
1381 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
->pd_handle
,
1383 if (!pd
|| (!scq
&& has_sq
)) {
1388 device
= pd
->device
;
1391 attr
.event_handler
= ib_uverbs_qp_event_handler
;
1396 attr
.sq_sig_type
= cmd
->sq_sig_all
? IB_SIGNAL_ALL_WR
:
1398 attr
.qp_type
= cmd
->qp_type
;
1399 attr
.create_flags
= 0;
1401 attr
.cap
.max_send_wr
= cmd
->max_send_wr
;
1402 attr
.cap
.max_recv_wr
= cmd
->max_recv_wr
;
1403 attr
.cap
.max_send_sge
= cmd
->max_send_sge
;
1404 attr
.cap
.max_recv_sge
= cmd
->max_recv_sge
;
1405 attr
.cap
.max_inline_data
= cmd
->max_inline_data
;
1407 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
1408 INIT_LIST_HEAD(&obj
->mcast_list
);
1410 attr
.create_flags
= cmd
->create_flags
;
1411 if (attr
.create_flags
& ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK
|
1412 IB_QP_CREATE_CROSS_CHANNEL
|
1413 IB_QP_CREATE_MANAGED_SEND
|
1414 IB_QP_CREATE_MANAGED_RECV
|
1415 IB_QP_CREATE_SCATTER_FCS
|
1416 IB_QP_CREATE_CVLAN_STRIPPING
|
1417 IB_QP_CREATE_SOURCE_QPN
|
1418 IB_QP_CREATE_PCI_WRITE_END_PADDING
)) {
1423 if (attr
.create_flags
& IB_QP_CREATE_SOURCE_QPN
) {
1424 if (!capable(CAP_NET_RAW
)) {
1429 attr
.source_qpn
= cmd
->source_qpn
;
1432 if (cmd
->qp_type
== IB_QPT_XRC_TGT
)
1433 qp
= ib_create_qp(pd
, &attr
);
1435 qp
= _ib_create_qp(device
, pd
, &attr
, &attrs
->driver_udata
,
1443 if (cmd
->qp_type
!= IB_QPT_XRC_TGT
) {
1444 ret
= ib_create_qp_security(qp
, device
);
1448 atomic_inc(&pd
->usecnt
);
1450 atomic_inc(&attr
.send_cq
->usecnt
);
1452 atomic_inc(&attr
.recv_cq
->usecnt
);
1454 atomic_inc(&attr
.srq
->usecnt
);
1456 atomic_inc(&ind_tbl
->usecnt
);
1458 /* It is done in _ib_create_qp for other QP types */
1462 obj
->uevent
.uobject
.object
= qp
;
1464 memset(&resp
, 0, sizeof resp
);
1465 resp
.base
.qpn
= qp
->qp_num
;
1466 resp
.base
.qp_handle
= obj
->uevent
.uobject
.id
;
1467 resp
.base
.max_recv_sge
= attr
.cap
.max_recv_sge
;
1468 resp
.base
.max_send_sge
= attr
.cap
.max_send_sge
;
1469 resp
.base
.max_recv_wr
= attr
.cap
.max_recv_wr
;
1470 resp
.base
.max_send_wr
= attr
.cap
.max_send_wr
;
1471 resp
.base
.max_inline_data
= attr
.cap
.max_inline_data
;
1472 resp
.response_length
= uverbs_response_length(attrs
, sizeof(resp
));
1474 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
1479 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
,
1481 atomic_inc(&obj
->uxrcd
->refcnt
);
1482 uobj_put_read(xrcd_uobj
);
1486 uobj_put_obj_read(pd
);
1488 rdma_lookup_put_uobject(&scq
->uobject
->uevent
.uobject
,
1489 UVERBS_LOOKUP_READ
);
1490 if (rcq
&& rcq
!= scq
)
1491 rdma_lookup_put_uobject(&rcq
->uobject
->uevent
.uobject
,
1492 UVERBS_LOOKUP_READ
);
1494 rdma_lookup_put_uobject(&srq
->uobject
->uevent
.uobject
,
1495 UVERBS_LOOKUP_READ
);
1497 uobj_put_obj_read(ind_tbl
);
1499 rdma_alloc_commit_uobject(&obj
->uevent
.uobject
, attrs
);
1502 ib_destroy_qp_user(qp
, uverbs_get_cleared_udata(attrs
));
1505 if (!IS_ERR(xrcd_uobj
))
1506 uobj_put_read(xrcd_uobj
);
1508 uobj_put_obj_read(pd
);
1510 rdma_lookup_put_uobject(&scq
->uobject
->uevent
.uobject
,
1511 UVERBS_LOOKUP_READ
);
1512 if (rcq
&& rcq
!= scq
)
1513 rdma_lookup_put_uobject(&rcq
->uobject
->uevent
.uobject
,
1514 UVERBS_LOOKUP_READ
);
1516 rdma_lookup_put_uobject(&srq
->uobject
->uevent
.uobject
,
1517 UVERBS_LOOKUP_READ
);
1519 uobj_put_obj_read(ind_tbl
);
1521 uobj_alloc_abort(&obj
->uevent
.uobject
, attrs
);
1525 static int ib_uverbs_create_qp(struct uverbs_attr_bundle
*attrs
)
1527 struct ib_uverbs_create_qp cmd
;
1528 struct ib_uverbs_ex_create_qp cmd_ex
;
1531 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1535 memset(&cmd_ex
, 0, sizeof(cmd_ex
));
1536 cmd_ex
.user_handle
= cmd
.user_handle
;
1537 cmd_ex
.pd_handle
= cmd
.pd_handle
;
1538 cmd_ex
.send_cq_handle
= cmd
.send_cq_handle
;
1539 cmd_ex
.recv_cq_handle
= cmd
.recv_cq_handle
;
1540 cmd_ex
.srq_handle
= cmd
.srq_handle
;
1541 cmd_ex
.max_send_wr
= cmd
.max_send_wr
;
1542 cmd_ex
.max_recv_wr
= cmd
.max_recv_wr
;
1543 cmd_ex
.max_send_sge
= cmd
.max_send_sge
;
1544 cmd_ex
.max_recv_sge
= cmd
.max_recv_sge
;
1545 cmd_ex
.max_inline_data
= cmd
.max_inline_data
;
1546 cmd_ex
.sq_sig_all
= cmd
.sq_sig_all
;
1547 cmd_ex
.qp_type
= cmd
.qp_type
;
1548 cmd_ex
.is_srq
= cmd
.is_srq
;
1550 return create_qp(attrs
, &cmd_ex
);
1553 static int ib_uverbs_ex_create_qp(struct uverbs_attr_bundle
*attrs
)
1555 struct ib_uverbs_ex_create_qp cmd
;
1558 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1562 if (cmd
.comp_mask
& ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK
)
1568 return create_qp(attrs
, &cmd
);
1571 static int ib_uverbs_open_qp(struct uverbs_attr_bundle
*attrs
)
1573 struct ib_uverbs_open_qp cmd
;
1574 struct ib_uverbs_create_qp_resp resp
;
1575 struct ib_uqp_object
*obj
;
1576 struct ib_xrcd
*xrcd
;
1577 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
1579 struct ib_qp_open_attr attr
= {};
1581 struct ib_device
*ib_dev
;
1583 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1587 obj
= (struct ib_uqp_object
*)uobj_alloc(UVERBS_OBJECT_QP
, attrs
,
1590 return PTR_ERR(obj
);
1592 xrcd_uobj
= uobj_get_read(UVERBS_OBJECT_XRCD
, cmd
.pd_handle
, attrs
);
1593 if (IS_ERR(xrcd_uobj
)) {
1598 xrcd
= (struct ib_xrcd
*)xrcd_uobj
->object
;
1604 attr
.event_handler
= ib_uverbs_qp_event_handler
;
1605 attr
.qp_num
= cmd
.qpn
;
1606 attr
.qp_type
= cmd
.qp_type
;
1608 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
1609 INIT_LIST_HEAD(&obj
->mcast_list
);
1611 qp
= ib_open_qp(xrcd
, &attr
);
1617 obj
->uevent
.uobject
.object
= qp
;
1618 obj
->uevent
.uobject
.user_handle
= cmd
.user_handle
;
1620 memset(&resp
, 0, sizeof resp
);
1621 resp
.qpn
= qp
->qp_num
;
1622 resp
.qp_handle
= obj
->uevent
.uobject
.id
;
1624 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
1628 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
, uobject
);
1629 atomic_inc(&obj
->uxrcd
->refcnt
);
1631 uobj_put_read(xrcd_uobj
);
1633 rdma_alloc_commit_uobject(&obj
->uevent
.uobject
, attrs
);
1637 ib_destroy_qp_user(qp
, uverbs_get_cleared_udata(attrs
));
1639 uobj_put_read(xrcd_uobj
);
1641 uobj_alloc_abort(&obj
->uevent
.uobject
, attrs
);
1645 static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest
*uverb_attr
,
1646 struct rdma_ah_attr
*rdma_attr
)
1648 const struct ib_global_route
*grh
;
1650 uverb_attr
->dlid
= rdma_ah_get_dlid(rdma_attr
);
1651 uverb_attr
->sl
= rdma_ah_get_sl(rdma_attr
);
1652 uverb_attr
->src_path_bits
= rdma_ah_get_path_bits(rdma_attr
);
1653 uverb_attr
->static_rate
= rdma_ah_get_static_rate(rdma_attr
);
1654 uverb_attr
->is_global
= !!(rdma_ah_get_ah_flags(rdma_attr
) &
1656 if (uverb_attr
->is_global
) {
1657 grh
= rdma_ah_read_grh(rdma_attr
);
1658 memcpy(uverb_attr
->dgid
, grh
->dgid
.raw
, 16);
1659 uverb_attr
->flow_label
= grh
->flow_label
;
1660 uverb_attr
->sgid_index
= grh
->sgid_index
;
1661 uverb_attr
->hop_limit
= grh
->hop_limit
;
1662 uverb_attr
->traffic_class
= grh
->traffic_class
;
1664 uverb_attr
->port_num
= rdma_ah_get_port_num(rdma_attr
);
1667 static int ib_uverbs_query_qp(struct uverbs_attr_bundle
*attrs
)
1669 struct ib_uverbs_query_qp cmd
;
1670 struct ib_uverbs_query_qp_resp resp
;
1672 struct ib_qp_attr
*attr
;
1673 struct ib_qp_init_attr
*init_attr
;
1676 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1680 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
1681 init_attr
= kmalloc(sizeof *init_attr
, GFP_KERNEL
);
1682 if (!attr
|| !init_attr
) {
1687 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
1693 ret
= ib_query_qp(qp
, attr
, cmd
.attr_mask
, init_attr
);
1695 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
1696 UVERBS_LOOKUP_READ
);
1701 memset(&resp
, 0, sizeof resp
);
1703 resp
.qp_state
= attr
->qp_state
;
1704 resp
.cur_qp_state
= attr
->cur_qp_state
;
1705 resp
.path_mtu
= attr
->path_mtu
;
1706 resp
.path_mig_state
= attr
->path_mig_state
;
1707 resp
.qkey
= attr
->qkey
;
1708 resp
.rq_psn
= attr
->rq_psn
;
1709 resp
.sq_psn
= attr
->sq_psn
;
1710 resp
.dest_qp_num
= attr
->dest_qp_num
;
1711 resp
.qp_access_flags
= attr
->qp_access_flags
;
1712 resp
.pkey_index
= attr
->pkey_index
;
1713 resp
.alt_pkey_index
= attr
->alt_pkey_index
;
1714 resp
.sq_draining
= attr
->sq_draining
;
1715 resp
.max_rd_atomic
= attr
->max_rd_atomic
;
1716 resp
.max_dest_rd_atomic
= attr
->max_dest_rd_atomic
;
1717 resp
.min_rnr_timer
= attr
->min_rnr_timer
;
1718 resp
.port_num
= attr
->port_num
;
1719 resp
.timeout
= attr
->timeout
;
1720 resp
.retry_cnt
= attr
->retry_cnt
;
1721 resp
.rnr_retry
= attr
->rnr_retry
;
1722 resp
.alt_port_num
= attr
->alt_port_num
;
1723 resp
.alt_timeout
= attr
->alt_timeout
;
1725 copy_ah_attr_to_uverbs(&resp
.dest
, &attr
->ah_attr
);
1726 copy_ah_attr_to_uverbs(&resp
.alt_dest
, &attr
->alt_ah_attr
);
1728 resp
.max_send_wr
= init_attr
->cap
.max_send_wr
;
1729 resp
.max_recv_wr
= init_attr
->cap
.max_recv_wr
;
1730 resp
.max_send_sge
= init_attr
->cap
.max_send_sge
;
1731 resp
.max_recv_sge
= init_attr
->cap
.max_recv_sge
;
1732 resp
.max_inline_data
= init_attr
->cap
.max_inline_data
;
1733 resp
.sq_sig_all
= init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
;
1735 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
1744 /* Remove ignored fields set in the attribute mask */
1745 static int modify_qp_mask(enum ib_qp_type qp_type
, int mask
)
1748 case IB_QPT_XRC_INI
:
1749 return mask
& ~(IB_QP_MAX_DEST_RD_ATOMIC
| IB_QP_MIN_RNR_TIMER
);
1750 case IB_QPT_XRC_TGT
:
1751 return mask
& ~(IB_QP_MAX_QP_RD_ATOMIC
| IB_QP_RETRY_CNT
|
1758 static void copy_ah_attr_from_uverbs(struct ib_device
*dev
,
1759 struct rdma_ah_attr
*rdma_attr
,
1760 struct ib_uverbs_qp_dest
*uverb_attr
)
1762 rdma_attr
->type
= rdma_ah_find_type(dev
, uverb_attr
->port_num
);
1763 if (uverb_attr
->is_global
) {
1764 rdma_ah_set_grh(rdma_attr
, NULL
,
1765 uverb_attr
->flow_label
,
1766 uverb_attr
->sgid_index
,
1767 uverb_attr
->hop_limit
,
1768 uverb_attr
->traffic_class
);
1769 rdma_ah_set_dgid_raw(rdma_attr
, uverb_attr
->dgid
);
1771 rdma_ah_set_ah_flags(rdma_attr
, 0);
1773 rdma_ah_set_dlid(rdma_attr
, uverb_attr
->dlid
);
1774 rdma_ah_set_sl(rdma_attr
, uverb_attr
->sl
);
1775 rdma_ah_set_path_bits(rdma_attr
, uverb_attr
->src_path_bits
);
1776 rdma_ah_set_static_rate(rdma_attr
, uverb_attr
->static_rate
);
1777 rdma_ah_set_port_num(rdma_attr
, uverb_attr
->port_num
);
1778 rdma_ah_set_make_grd(rdma_attr
, false);
1781 static int modify_qp(struct uverbs_attr_bundle
*attrs
,
1782 struct ib_uverbs_ex_modify_qp
*cmd
)
1784 struct ib_qp_attr
*attr
;
1788 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1792 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
->base
.qp_handle
,
1799 if ((cmd
->base
.attr_mask
& IB_QP_PORT
) &&
1800 !rdma_is_port_valid(qp
->device
, cmd
->base
.port_num
)) {
1805 if ((cmd
->base
.attr_mask
& IB_QP_AV
)) {
1806 if (!rdma_is_port_valid(qp
->device
, cmd
->base
.dest
.port_num
)) {
1811 if (cmd
->base
.attr_mask
& IB_QP_STATE
&&
1812 cmd
->base
.qp_state
== IB_QPS_RTR
) {
1813 /* We are in INIT->RTR TRANSITION (if we are not,
1814 * this transition will be rejected in subsequent checks).
1815 * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1816 * but the IB_QP_STATE flag is required.
1818 * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1819 * when IB_QP_AV is set, has required inclusion of a valid
1820 * port number in the primary AV. (AVs are created and handled
1821 * differently for infiniband and ethernet (RoCE) ports).
1823 * Check the port number included in the primary AV against
1824 * the port number in the qp struct, which was set (and saved)
1825 * in the RST->INIT transition.
1827 if (cmd
->base
.dest
.port_num
!= qp
->real_qp
->port
) {
1832 /* We are in SQD->SQD. (If we are not, this transition will
1833 * be rejected later in the verbs layer checks).
1834 * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1835 * together in the SQD->SQD transition.
1837 * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1838 * verbs layer driver does not track primary port changes
1839 * resulting from path migration. Thus, in SQD, if the primary
1840 * AV is modified, the primary port should also be modified).
1842 * Note that in this transition, the IB_QP_STATE flag
1845 if (((cmd
->base
.attr_mask
& (IB_QP_AV
| IB_QP_PORT
))
1846 == (IB_QP_AV
| IB_QP_PORT
)) &&
1847 cmd
->base
.port_num
!= cmd
->base
.dest
.port_num
) {
1851 if ((cmd
->base
.attr_mask
& (IB_QP_AV
| IB_QP_PORT
))
1853 cmd
->base
.attr_mask
|= IB_QP_PORT
;
1854 cmd
->base
.port_num
= cmd
->base
.dest
.port_num
;
1859 if ((cmd
->base
.attr_mask
& IB_QP_ALT_PATH
) &&
1860 (!rdma_is_port_valid(qp
->device
, cmd
->base
.alt_port_num
) ||
1861 !rdma_is_port_valid(qp
->device
, cmd
->base
.alt_dest
.port_num
) ||
1862 cmd
->base
.alt_port_num
!= cmd
->base
.alt_dest
.port_num
)) {
1867 if ((cmd
->base
.attr_mask
& IB_QP_CUR_STATE
&&
1868 cmd
->base
.cur_qp_state
> IB_QPS_ERR
) ||
1869 (cmd
->base
.attr_mask
& IB_QP_STATE
&&
1870 cmd
->base
.qp_state
> IB_QPS_ERR
)) {
1875 if (cmd
->base
.attr_mask
& IB_QP_STATE
)
1876 attr
->qp_state
= cmd
->base
.qp_state
;
1877 if (cmd
->base
.attr_mask
& IB_QP_CUR_STATE
)
1878 attr
->cur_qp_state
= cmd
->base
.cur_qp_state
;
1879 if (cmd
->base
.attr_mask
& IB_QP_PATH_MTU
)
1880 attr
->path_mtu
= cmd
->base
.path_mtu
;
1881 if (cmd
->base
.attr_mask
& IB_QP_PATH_MIG_STATE
)
1882 attr
->path_mig_state
= cmd
->base
.path_mig_state
;
1883 if (cmd
->base
.attr_mask
& IB_QP_QKEY
)
1884 attr
->qkey
= cmd
->base
.qkey
;
1885 if (cmd
->base
.attr_mask
& IB_QP_RQ_PSN
)
1886 attr
->rq_psn
= cmd
->base
.rq_psn
;
1887 if (cmd
->base
.attr_mask
& IB_QP_SQ_PSN
)
1888 attr
->sq_psn
= cmd
->base
.sq_psn
;
1889 if (cmd
->base
.attr_mask
& IB_QP_DEST_QPN
)
1890 attr
->dest_qp_num
= cmd
->base
.dest_qp_num
;
1891 if (cmd
->base
.attr_mask
& IB_QP_ACCESS_FLAGS
)
1892 attr
->qp_access_flags
= cmd
->base
.qp_access_flags
;
1893 if (cmd
->base
.attr_mask
& IB_QP_PKEY_INDEX
)
1894 attr
->pkey_index
= cmd
->base
.pkey_index
;
1895 if (cmd
->base
.attr_mask
& IB_QP_EN_SQD_ASYNC_NOTIFY
)
1896 attr
->en_sqd_async_notify
= cmd
->base
.en_sqd_async_notify
;
1897 if (cmd
->base
.attr_mask
& IB_QP_MAX_QP_RD_ATOMIC
)
1898 attr
->max_rd_atomic
= cmd
->base
.max_rd_atomic
;
1899 if (cmd
->base
.attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
)
1900 attr
->max_dest_rd_atomic
= cmd
->base
.max_dest_rd_atomic
;
1901 if (cmd
->base
.attr_mask
& IB_QP_MIN_RNR_TIMER
)
1902 attr
->min_rnr_timer
= cmd
->base
.min_rnr_timer
;
1903 if (cmd
->base
.attr_mask
& IB_QP_PORT
)
1904 attr
->port_num
= cmd
->base
.port_num
;
1905 if (cmd
->base
.attr_mask
& IB_QP_TIMEOUT
)
1906 attr
->timeout
= cmd
->base
.timeout
;
1907 if (cmd
->base
.attr_mask
& IB_QP_RETRY_CNT
)
1908 attr
->retry_cnt
= cmd
->base
.retry_cnt
;
1909 if (cmd
->base
.attr_mask
& IB_QP_RNR_RETRY
)
1910 attr
->rnr_retry
= cmd
->base
.rnr_retry
;
1911 if (cmd
->base
.attr_mask
& IB_QP_ALT_PATH
) {
1912 attr
->alt_port_num
= cmd
->base
.alt_port_num
;
1913 attr
->alt_timeout
= cmd
->base
.alt_timeout
;
1914 attr
->alt_pkey_index
= cmd
->base
.alt_pkey_index
;
1916 if (cmd
->base
.attr_mask
& IB_QP_RATE_LIMIT
)
1917 attr
->rate_limit
= cmd
->rate_limit
;
1919 if (cmd
->base
.attr_mask
& IB_QP_AV
)
1920 copy_ah_attr_from_uverbs(qp
->device
, &attr
->ah_attr
,
1923 if (cmd
->base
.attr_mask
& IB_QP_ALT_PATH
)
1924 copy_ah_attr_from_uverbs(qp
->device
, &attr
->alt_ah_attr
,
1925 &cmd
->base
.alt_dest
);
1927 ret
= ib_modify_qp_with_udata(qp
, attr
,
1928 modify_qp_mask(qp
->qp_type
,
1929 cmd
->base
.attr_mask
),
1930 &attrs
->driver_udata
);
1933 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
1934 UVERBS_LOOKUP_READ
);
1941 static int ib_uverbs_modify_qp(struct uverbs_attr_bundle
*attrs
)
1943 struct ib_uverbs_ex_modify_qp cmd
;
1946 ret
= uverbs_request(attrs
, &cmd
.base
, sizeof(cmd
.base
));
1950 if (cmd
.base
.attr_mask
&
1951 ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK
<< 1) - 1))
1954 return modify_qp(attrs
, &cmd
);
1957 static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle
*attrs
)
1959 struct ib_uverbs_ex_modify_qp cmd
;
1960 struct ib_uverbs_ex_modify_qp_resp resp
= {
1961 .response_length
= uverbs_response_length(attrs
, sizeof(resp
))
1965 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1970 * Last bit is reserved for extending the attr_mask by
1971 * using another field.
1973 BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK
== (1 << 31));
1975 if (cmd
.base
.attr_mask
&
1976 ~((IB_USER_LAST_QP_ATTR_MASK
<< 1) - 1))
1979 ret
= modify_qp(attrs
, &cmd
);
1983 return uverbs_response(attrs
, &resp
, sizeof(resp
));
1986 static int ib_uverbs_destroy_qp(struct uverbs_attr_bundle
*attrs
)
1988 struct ib_uverbs_destroy_qp cmd
;
1989 struct ib_uverbs_destroy_qp_resp resp
;
1990 struct ib_uobject
*uobj
;
1991 struct ib_uqp_object
*obj
;
1994 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
1998 uobj
= uobj_get_destroy(UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
2000 return PTR_ERR(uobj
);
2002 obj
= container_of(uobj
, struct ib_uqp_object
, uevent
.uobject
);
2003 memset(&resp
, 0, sizeof(resp
));
2004 resp
.events_reported
= obj
->uevent
.events_reported
;
2006 uobj_put_destroy(uobj
);
2008 return uverbs_response(attrs
, &resp
, sizeof(resp
));
2011 static void *alloc_wr(size_t wr_size
, __u32 num_sge
)
2013 if (num_sge
>= (U32_MAX
- ALIGN(wr_size
, sizeof (struct ib_sge
))) /
2014 sizeof (struct ib_sge
))
2017 return kmalloc(ALIGN(wr_size
, sizeof (struct ib_sge
)) +
2018 num_sge
* sizeof (struct ib_sge
), GFP_KERNEL
);
2021 static int ib_uverbs_post_send(struct uverbs_attr_bundle
*attrs
)
2023 struct ib_uverbs_post_send cmd
;
2024 struct ib_uverbs_post_send_resp resp
;
2025 struct ib_uverbs_send_wr
*user_wr
;
2026 struct ib_send_wr
*wr
= NULL
, *last
, *next
;
2027 const struct ib_send_wr
*bad_wr
;
2033 const struct ib_sge __user
*sgls
;
2034 const void __user
*wqes
;
2035 struct uverbs_req_iter iter
;
2037 ret
= uverbs_request_start(attrs
, &iter
, &cmd
, sizeof(cmd
));
2040 wqes
= uverbs_request_next_ptr(&iter
, cmd
.wqe_size
* cmd
.wr_count
);
2042 return PTR_ERR(wqes
);
2043 sgls
= uverbs_request_next_ptr(
2044 &iter
, cmd
.sge_count
* sizeof(struct ib_uverbs_sge
));
2046 return PTR_ERR(sgls
);
2047 ret
= uverbs_request_finish(&iter
);
2051 user_wr
= kmalloc(cmd
.wqe_size
, GFP_KERNEL
);
2055 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
2061 is_ud
= qp
->qp_type
== IB_QPT_UD
;
2064 for (i
= 0; i
< cmd
.wr_count
; ++i
) {
2065 if (copy_from_user(user_wr
, wqes
+ i
* cmd
.wqe_size
,
2071 if (user_wr
->num_sge
+ sg_ind
> cmd
.sge_count
) {
2077 struct ib_ud_wr
*ud
;
2079 if (user_wr
->opcode
!= IB_WR_SEND
&&
2080 user_wr
->opcode
!= IB_WR_SEND_WITH_IMM
) {
2085 next_size
= sizeof(*ud
);
2086 ud
= alloc_wr(next_size
, user_wr
->num_sge
);
2092 ud
->ah
= uobj_get_obj_read(ah
, UVERBS_OBJECT_AH
,
2093 user_wr
->wr
.ud
.ah
, attrs
);
2099 ud
->remote_qpn
= user_wr
->wr
.ud
.remote_qpn
;
2100 ud
->remote_qkey
= user_wr
->wr
.ud
.remote_qkey
;
2103 } else if (user_wr
->opcode
== IB_WR_RDMA_WRITE_WITH_IMM
||
2104 user_wr
->opcode
== IB_WR_RDMA_WRITE
||
2105 user_wr
->opcode
== IB_WR_RDMA_READ
) {
2106 struct ib_rdma_wr
*rdma
;
2108 next_size
= sizeof(*rdma
);
2109 rdma
= alloc_wr(next_size
, user_wr
->num_sge
);
2115 rdma
->remote_addr
= user_wr
->wr
.rdma
.remote_addr
;
2116 rdma
->rkey
= user_wr
->wr
.rdma
.rkey
;
2119 } else if (user_wr
->opcode
== IB_WR_ATOMIC_CMP_AND_SWP
||
2120 user_wr
->opcode
== IB_WR_ATOMIC_FETCH_AND_ADD
) {
2121 struct ib_atomic_wr
*atomic
;
2123 next_size
= sizeof(*atomic
);
2124 atomic
= alloc_wr(next_size
, user_wr
->num_sge
);
2130 atomic
->remote_addr
= user_wr
->wr
.atomic
.remote_addr
;
2131 atomic
->compare_add
= user_wr
->wr
.atomic
.compare_add
;
2132 atomic
->swap
= user_wr
->wr
.atomic
.swap
;
2133 atomic
->rkey
= user_wr
->wr
.atomic
.rkey
;
2136 } else if (user_wr
->opcode
== IB_WR_SEND
||
2137 user_wr
->opcode
== IB_WR_SEND_WITH_IMM
||
2138 user_wr
->opcode
== IB_WR_SEND_WITH_INV
) {
2139 next_size
= sizeof(*next
);
2140 next
= alloc_wr(next_size
, user_wr
->num_sge
);
2150 if (user_wr
->opcode
== IB_WR_SEND_WITH_IMM
||
2151 user_wr
->opcode
== IB_WR_RDMA_WRITE_WITH_IMM
) {
2153 (__be32 __force
) user_wr
->ex
.imm_data
;
2154 } else if (user_wr
->opcode
== IB_WR_SEND_WITH_INV
) {
2155 next
->ex
.invalidate_rkey
= user_wr
->ex
.invalidate_rkey
;
2165 next
->wr_id
= user_wr
->wr_id
;
2166 next
->num_sge
= user_wr
->num_sge
;
2167 next
->opcode
= user_wr
->opcode
;
2168 next
->send_flags
= user_wr
->send_flags
;
2170 if (next
->num_sge
) {
2171 next
->sg_list
= (void *) next
+
2172 ALIGN(next_size
, sizeof(struct ib_sge
));
2173 if (copy_from_user(next
->sg_list
, sgls
+ sg_ind
,
2175 sizeof(struct ib_sge
))) {
2179 sg_ind
+= next
->num_sge
;
2181 next
->sg_list
= NULL
;
2185 ret
= qp
->device
->ops
.post_send(qp
->real_qp
, wr
, &bad_wr
);
2187 for (next
= wr
; next
; next
= next
->next
) {
2193 ret2
= uverbs_response(attrs
, &resp
, sizeof(resp
));
2198 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
2199 UVERBS_LOOKUP_READ
);
2202 if (is_ud
&& ud_wr(wr
)->ah
)
2203 uobj_put_obj_read(ud_wr(wr
)->ah
);
2215 static struct ib_recv_wr
*
2216 ib_uverbs_unmarshall_recv(struct uverbs_req_iter
*iter
, u32 wr_count
,
2217 u32 wqe_size
, u32 sge_count
)
2219 struct ib_uverbs_recv_wr
*user_wr
;
2220 struct ib_recv_wr
*wr
= NULL
, *last
, *next
;
2224 const struct ib_sge __user
*sgls
;
2225 const void __user
*wqes
;
2227 if (wqe_size
< sizeof (struct ib_uverbs_recv_wr
))
2228 return ERR_PTR(-EINVAL
);
2230 wqes
= uverbs_request_next_ptr(iter
, wqe_size
* wr_count
);
2232 return ERR_CAST(wqes
);
2233 sgls
= uverbs_request_next_ptr(
2234 iter
, sge_count
* sizeof(struct ib_uverbs_sge
));
2236 return ERR_CAST(sgls
);
2237 ret
= uverbs_request_finish(iter
);
2239 return ERR_PTR(ret
);
2241 user_wr
= kmalloc(wqe_size
, GFP_KERNEL
);
2243 return ERR_PTR(-ENOMEM
);
2247 for (i
= 0; i
< wr_count
; ++i
) {
2248 if (copy_from_user(user_wr
, wqes
+ i
* wqe_size
,
2254 if (user_wr
->num_sge
+ sg_ind
> sge_count
) {
2259 if (user_wr
->num_sge
>=
2260 (U32_MAX
- ALIGN(sizeof *next
, sizeof (struct ib_sge
))) /
2261 sizeof (struct ib_sge
)) {
2266 next
= kmalloc(ALIGN(sizeof *next
, sizeof (struct ib_sge
)) +
2267 user_wr
->num_sge
* sizeof (struct ib_sge
),
2281 next
->wr_id
= user_wr
->wr_id
;
2282 next
->num_sge
= user_wr
->num_sge
;
2284 if (next
->num_sge
) {
2285 next
->sg_list
= (void *) next
+
2286 ALIGN(sizeof *next
, sizeof (struct ib_sge
));
2287 if (copy_from_user(next
->sg_list
, sgls
+ sg_ind
,
2289 sizeof(struct ib_sge
))) {
2293 sg_ind
+= next
->num_sge
;
2295 next
->sg_list
= NULL
;
2310 return ERR_PTR(ret
);
2313 static int ib_uverbs_post_recv(struct uverbs_attr_bundle
*attrs
)
2315 struct ib_uverbs_post_recv cmd
;
2316 struct ib_uverbs_post_recv_resp resp
;
2317 struct ib_recv_wr
*wr
, *next
;
2318 const struct ib_recv_wr
*bad_wr
;
2321 struct uverbs_req_iter iter
;
2323 ret
= uverbs_request_start(attrs
, &iter
, &cmd
, sizeof(cmd
));
2327 wr
= ib_uverbs_unmarshall_recv(&iter
, cmd
.wr_count
, cmd
.wqe_size
,
2332 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
2339 ret
= qp
->device
->ops
.post_recv(qp
->real_qp
, wr
, &bad_wr
);
2341 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
2342 UVERBS_LOOKUP_READ
);
2344 for (next
= wr
; next
; next
= next
->next
) {
2351 ret2
= uverbs_response(attrs
, &resp
, sizeof(resp
));
2364 static int ib_uverbs_post_srq_recv(struct uverbs_attr_bundle
*attrs
)
2366 struct ib_uverbs_post_srq_recv cmd
;
2367 struct ib_uverbs_post_srq_recv_resp resp
;
2368 struct ib_recv_wr
*wr
, *next
;
2369 const struct ib_recv_wr
*bad_wr
;
2372 struct uverbs_req_iter iter
;
2374 ret
= uverbs_request_start(attrs
, &iter
, &cmd
, sizeof(cmd
));
2378 wr
= ib_uverbs_unmarshall_recv(&iter
, cmd
.wr_count
, cmd
.wqe_size
,
2383 srq
= uobj_get_obj_read(srq
, UVERBS_OBJECT_SRQ
, cmd
.srq_handle
, attrs
);
2390 ret
= srq
->device
->ops
.post_srq_recv(srq
, wr
, &bad_wr
);
2392 rdma_lookup_put_uobject(&srq
->uobject
->uevent
.uobject
,
2393 UVERBS_LOOKUP_READ
);
2396 for (next
= wr
; next
; next
= next
->next
) {
2402 ret2
= uverbs_response(attrs
, &resp
, sizeof(resp
));
2416 static int ib_uverbs_create_ah(struct uverbs_attr_bundle
*attrs
)
2418 struct ib_uverbs_create_ah cmd
;
2419 struct ib_uverbs_create_ah_resp resp
;
2420 struct ib_uobject
*uobj
;
2423 struct rdma_ah_attr attr
= {};
2425 struct ib_device
*ib_dev
;
2427 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
2431 uobj
= uobj_alloc(UVERBS_OBJECT_AH
, attrs
, &ib_dev
);
2433 return PTR_ERR(uobj
);
2435 if (!rdma_is_port_valid(ib_dev
, cmd
.attr
.port_num
)) {
2440 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
.pd_handle
, attrs
);
2446 attr
.type
= rdma_ah_find_type(ib_dev
, cmd
.attr
.port_num
);
2447 rdma_ah_set_make_grd(&attr
, false);
2448 rdma_ah_set_dlid(&attr
, cmd
.attr
.dlid
);
2449 rdma_ah_set_sl(&attr
, cmd
.attr
.sl
);
2450 rdma_ah_set_path_bits(&attr
, cmd
.attr
.src_path_bits
);
2451 rdma_ah_set_static_rate(&attr
, cmd
.attr
.static_rate
);
2452 rdma_ah_set_port_num(&attr
, cmd
.attr
.port_num
);
2454 if (cmd
.attr
.is_global
) {
2455 rdma_ah_set_grh(&attr
, NULL
, cmd
.attr
.grh
.flow_label
,
2456 cmd
.attr
.grh
.sgid_index
,
2457 cmd
.attr
.grh
.hop_limit
,
2458 cmd
.attr
.grh
.traffic_class
);
2459 rdma_ah_set_dgid_raw(&attr
, cmd
.attr
.grh
.dgid
);
2461 rdma_ah_set_ah_flags(&attr
, 0);
2464 ah
= rdma_create_user_ah(pd
, &attr
, &attrs
->driver_udata
);
2471 uobj
->user_handle
= cmd
.user_handle
;
2474 resp
.ah_handle
= uobj
->id
;
2476 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
2480 uobj_put_obj_read(pd
);
2481 rdma_alloc_commit_uobject(uobj
, attrs
);
2485 rdma_destroy_ah_user(ah
, RDMA_DESTROY_AH_SLEEPABLE
,
2486 uverbs_get_cleared_udata(attrs
));
2489 uobj_put_obj_read(pd
);
2492 uobj_alloc_abort(uobj
, attrs
);
2496 static int ib_uverbs_destroy_ah(struct uverbs_attr_bundle
*attrs
)
2498 struct ib_uverbs_destroy_ah cmd
;
2501 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
2505 return uobj_perform_destroy(UVERBS_OBJECT_AH
, cmd
.ah_handle
, attrs
);
2508 static int ib_uverbs_attach_mcast(struct uverbs_attr_bundle
*attrs
)
2510 struct ib_uverbs_attach_mcast cmd
;
2512 struct ib_uqp_object
*obj
;
2513 struct ib_uverbs_mcast_entry
*mcast
;
2516 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
2520 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
2526 mutex_lock(&obj
->mcast_lock
);
2527 list_for_each_entry(mcast
, &obj
->mcast_list
, list
)
2528 if (cmd
.mlid
== mcast
->lid
&&
2529 !memcmp(cmd
.gid
, mcast
->gid
.raw
, sizeof mcast
->gid
.raw
)) {
2534 mcast
= kmalloc(sizeof *mcast
, GFP_KERNEL
);
2540 mcast
->lid
= cmd
.mlid
;
2541 memcpy(mcast
->gid
.raw
, cmd
.gid
, sizeof mcast
->gid
.raw
);
2543 ret
= ib_attach_mcast(qp
, &mcast
->gid
, cmd
.mlid
);
2545 list_add_tail(&mcast
->list
, &obj
->mcast_list
);
2550 mutex_unlock(&obj
->mcast_lock
);
2551 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
2552 UVERBS_LOOKUP_READ
);
2557 static int ib_uverbs_detach_mcast(struct uverbs_attr_bundle
*attrs
)
2559 struct ib_uverbs_detach_mcast cmd
;
2560 struct ib_uqp_object
*obj
;
2562 struct ib_uverbs_mcast_entry
*mcast
;
2566 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
2570 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
2575 mutex_lock(&obj
->mcast_lock
);
2577 list_for_each_entry(mcast
, &obj
->mcast_list
, list
)
2578 if (cmd
.mlid
== mcast
->lid
&&
2579 !memcmp(cmd
.gid
, mcast
->gid
.raw
, sizeof mcast
->gid
.raw
)) {
2580 list_del(&mcast
->list
);
2591 ret
= ib_detach_mcast(qp
, (union ib_gid
*)cmd
.gid
, cmd
.mlid
);
2594 mutex_unlock(&obj
->mcast_lock
);
2595 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
2596 UVERBS_LOOKUP_READ
);
2600 struct ib_uflow_resources
*flow_resources_alloc(size_t num_specs
)
2602 struct ib_uflow_resources
*resources
;
2604 resources
= kzalloc(sizeof(*resources
), GFP_KERNEL
);
2612 resources
->counters
=
2613 kcalloc(num_specs
, sizeof(*resources
->counters
), GFP_KERNEL
);
2614 resources
->collection
=
2615 kcalloc(num_specs
, sizeof(*resources
->collection
), GFP_KERNEL
);
2617 if (!resources
->counters
|| !resources
->collection
)
2621 resources
->max
= num_specs
;
2625 kfree(resources
->counters
);
2630 EXPORT_SYMBOL(flow_resources_alloc
);
2632 void ib_uverbs_flow_resources_free(struct ib_uflow_resources
*uflow_res
)
2639 for (i
= 0; i
< uflow_res
->collection_num
; i
++)
2640 atomic_dec(&uflow_res
->collection
[i
]->usecnt
);
2642 for (i
= 0; i
< uflow_res
->counters_num
; i
++)
2643 atomic_dec(&uflow_res
->counters
[i
]->usecnt
);
2645 kfree(uflow_res
->collection
);
2646 kfree(uflow_res
->counters
);
2649 EXPORT_SYMBOL(ib_uverbs_flow_resources_free
);
2651 void flow_resources_add(struct ib_uflow_resources
*uflow_res
,
2652 enum ib_flow_spec_type type
,
2655 WARN_ON(uflow_res
->num
>= uflow_res
->max
);
2658 case IB_FLOW_SPEC_ACTION_HANDLE
:
2659 atomic_inc(&((struct ib_flow_action
*)ibobj
)->usecnt
);
2660 uflow_res
->collection
[uflow_res
->collection_num
++] =
2661 (struct ib_flow_action
*)ibobj
;
2663 case IB_FLOW_SPEC_ACTION_COUNT
:
2664 atomic_inc(&((struct ib_counters
*)ibobj
)->usecnt
);
2665 uflow_res
->counters
[uflow_res
->counters_num
++] =
2666 (struct ib_counters
*)ibobj
;
2674 EXPORT_SYMBOL(flow_resources_add
);
2676 static int kern_spec_to_ib_spec_action(struct uverbs_attr_bundle
*attrs
,
2677 struct ib_uverbs_flow_spec
*kern_spec
,
2678 union ib_flow_spec
*ib_spec
,
2679 struct ib_uflow_resources
*uflow_res
)
2681 ib_spec
->type
= kern_spec
->type
;
2682 switch (ib_spec
->type
) {
2683 case IB_FLOW_SPEC_ACTION_TAG
:
2684 if (kern_spec
->flow_tag
.size
!=
2685 sizeof(struct ib_uverbs_flow_spec_action_tag
))
2688 ib_spec
->flow_tag
.size
= sizeof(struct ib_flow_spec_action_tag
);
2689 ib_spec
->flow_tag
.tag_id
= kern_spec
->flow_tag
.tag_id
;
2691 case IB_FLOW_SPEC_ACTION_DROP
:
2692 if (kern_spec
->drop
.size
!=
2693 sizeof(struct ib_uverbs_flow_spec_action_drop
))
2696 ib_spec
->drop
.size
= sizeof(struct ib_flow_spec_action_drop
);
2698 case IB_FLOW_SPEC_ACTION_HANDLE
:
2699 if (kern_spec
->action
.size
!=
2700 sizeof(struct ib_uverbs_flow_spec_action_handle
))
2702 ib_spec
->action
.act
= uobj_get_obj_read(flow_action
,
2703 UVERBS_OBJECT_FLOW_ACTION
,
2704 kern_spec
->action
.handle
,
2706 if (!ib_spec
->action
.act
)
2708 ib_spec
->action
.size
=
2709 sizeof(struct ib_flow_spec_action_handle
);
2710 flow_resources_add(uflow_res
,
2711 IB_FLOW_SPEC_ACTION_HANDLE
,
2712 ib_spec
->action
.act
);
2713 uobj_put_obj_read(ib_spec
->action
.act
);
2715 case IB_FLOW_SPEC_ACTION_COUNT
:
2716 if (kern_spec
->flow_count
.size
!=
2717 sizeof(struct ib_uverbs_flow_spec_action_count
))
2719 ib_spec
->flow_count
.counters
=
2720 uobj_get_obj_read(counters
,
2721 UVERBS_OBJECT_COUNTERS
,
2722 kern_spec
->flow_count
.handle
,
2724 if (!ib_spec
->flow_count
.counters
)
2726 ib_spec
->flow_count
.size
=
2727 sizeof(struct ib_flow_spec_action_count
);
2728 flow_resources_add(uflow_res
,
2729 IB_FLOW_SPEC_ACTION_COUNT
,
2730 ib_spec
->flow_count
.counters
);
2731 uobj_put_obj_read(ib_spec
->flow_count
.counters
);
2739 static ssize_t
spec_filter_size(const void *kern_spec_filter
, u16 kern_filter_size
,
2740 u16 ib_real_filter_sz
)
2743 * User space filter structures must be 64 bit aligned, otherwise this
2744 * may pass, but we won't handle additional new attributes.
2747 if (kern_filter_size
> ib_real_filter_sz
) {
2748 if (memchr_inv(kern_spec_filter
+
2749 ib_real_filter_sz
, 0,
2750 kern_filter_size
- ib_real_filter_sz
))
2752 return ib_real_filter_sz
;
2754 return kern_filter_size
;
2757 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type
,
2758 const void *kern_spec_mask
,
2759 const void *kern_spec_val
,
2760 size_t kern_filter_sz
,
2761 union ib_flow_spec
*ib_spec
)
2763 ssize_t actual_filter_sz
;
2764 ssize_t ib_filter_sz
;
2766 /* User flow spec size must be aligned to 4 bytes */
2767 if (kern_filter_sz
!= ALIGN(kern_filter_sz
, 4))
2770 ib_spec
->type
= type
;
2772 if (ib_spec
->type
== (IB_FLOW_SPEC_INNER
| IB_FLOW_SPEC_VXLAN_TUNNEL
))
2775 switch (ib_spec
->type
& ~IB_FLOW_SPEC_INNER
) {
2776 case IB_FLOW_SPEC_ETH
:
2777 ib_filter_sz
= offsetof(struct ib_flow_eth_filter
, real_sz
);
2778 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2781 if (actual_filter_sz
<= 0)
2783 ib_spec
->size
= sizeof(struct ib_flow_spec_eth
);
2784 memcpy(&ib_spec
->eth
.val
, kern_spec_val
, actual_filter_sz
);
2785 memcpy(&ib_spec
->eth
.mask
, kern_spec_mask
, actual_filter_sz
);
2787 case IB_FLOW_SPEC_IPV4
:
2788 ib_filter_sz
= offsetof(struct ib_flow_ipv4_filter
, real_sz
);
2789 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2792 if (actual_filter_sz
<= 0)
2794 ib_spec
->size
= sizeof(struct ib_flow_spec_ipv4
);
2795 memcpy(&ib_spec
->ipv4
.val
, kern_spec_val
, actual_filter_sz
);
2796 memcpy(&ib_spec
->ipv4
.mask
, kern_spec_mask
, actual_filter_sz
);
2798 case IB_FLOW_SPEC_IPV6
:
2799 ib_filter_sz
= offsetof(struct ib_flow_ipv6_filter
, real_sz
);
2800 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2803 if (actual_filter_sz
<= 0)
2805 ib_spec
->size
= sizeof(struct ib_flow_spec_ipv6
);
2806 memcpy(&ib_spec
->ipv6
.val
, kern_spec_val
, actual_filter_sz
);
2807 memcpy(&ib_spec
->ipv6
.mask
, kern_spec_mask
, actual_filter_sz
);
2809 if ((ntohl(ib_spec
->ipv6
.mask
.flow_label
)) >= BIT(20) ||
2810 (ntohl(ib_spec
->ipv6
.val
.flow_label
)) >= BIT(20))
2813 case IB_FLOW_SPEC_TCP
:
2814 case IB_FLOW_SPEC_UDP
:
2815 ib_filter_sz
= offsetof(struct ib_flow_tcp_udp_filter
, real_sz
);
2816 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2819 if (actual_filter_sz
<= 0)
2821 ib_spec
->size
= sizeof(struct ib_flow_spec_tcp_udp
);
2822 memcpy(&ib_spec
->tcp_udp
.val
, kern_spec_val
, actual_filter_sz
);
2823 memcpy(&ib_spec
->tcp_udp
.mask
, kern_spec_mask
, actual_filter_sz
);
2825 case IB_FLOW_SPEC_VXLAN_TUNNEL
:
2826 ib_filter_sz
= offsetof(struct ib_flow_tunnel_filter
, real_sz
);
2827 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2830 if (actual_filter_sz
<= 0)
2832 ib_spec
->tunnel
.size
= sizeof(struct ib_flow_spec_tunnel
);
2833 memcpy(&ib_spec
->tunnel
.val
, kern_spec_val
, actual_filter_sz
);
2834 memcpy(&ib_spec
->tunnel
.mask
, kern_spec_mask
, actual_filter_sz
);
2836 if ((ntohl(ib_spec
->tunnel
.mask
.tunnel_id
)) >= BIT(24) ||
2837 (ntohl(ib_spec
->tunnel
.val
.tunnel_id
)) >= BIT(24))
2840 case IB_FLOW_SPEC_ESP
:
2841 ib_filter_sz
= offsetof(struct ib_flow_esp_filter
, real_sz
);
2842 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2845 if (actual_filter_sz
<= 0)
2847 ib_spec
->esp
.size
= sizeof(struct ib_flow_spec_esp
);
2848 memcpy(&ib_spec
->esp
.val
, kern_spec_val
, actual_filter_sz
);
2849 memcpy(&ib_spec
->esp
.mask
, kern_spec_mask
, actual_filter_sz
);
2851 case IB_FLOW_SPEC_GRE
:
2852 ib_filter_sz
= offsetof(struct ib_flow_gre_filter
, real_sz
);
2853 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2856 if (actual_filter_sz
<= 0)
2858 ib_spec
->gre
.size
= sizeof(struct ib_flow_spec_gre
);
2859 memcpy(&ib_spec
->gre
.val
, kern_spec_val
, actual_filter_sz
);
2860 memcpy(&ib_spec
->gre
.mask
, kern_spec_mask
, actual_filter_sz
);
2862 case IB_FLOW_SPEC_MPLS
:
2863 ib_filter_sz
= offsetof(struct ib_flow_mpls_filter
, real_sz
);
2864 actual_filter_sz
= spec_filter_size(kern_spec_mask
,
2867 if (actual_filter_sz
<= 0)
2869 ib_spec
->mpls
.size
= sizeof(struct ib_flow_spec_mpls
);
2870 memcpy(&ib_spec
->mpls
.val
, kern_spec_val
, actual_filter_sz
);
2871 memcpy(&ib_spec
->mpls
.mask
, kern_spec_mask
, actual_filter_sz
);
2879 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec
*kern_spec
,
2880 union ib_flow_spec
*ib_spec
)
2882 size_t kern_filter_sz
;
2883 void *kern_spec_mask
;
2884 void *kern_spec_val
;
2886 if (check_sub_overflow((size_t)kern_spec
->hdr
.size
,
2887 sizeof(struct ib_uverbs_flow_spec_hdr
),
2891 kern_filter_sz
/= 2;
2893 kern_spec_val
= (void *)kern_spec
+
2894 sizeof(struct ib_uverbs_flow_spec_hdr
);
2895 kern_spec_mask
= kern_spec_val
+ kern_filter_sz
;
2897 return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec
->type
,
2900 kern_filter_sz
, ib_spec
);
2903 static int kern_spec_to_ib_spec(struct uverbs_attr_bundle
*attrs
,
2904 struct ib_uverbs_flow_spec
*kern_spec
,
2905 union ib_flow_spec
*ib_spec
,
2906 struct ib_uflow_resources
*uflow_res
)
2908 if (kern_spec
->reserved
)
2911 if (kern_spec
->type
>= IB_FLOW_SPEC_ACTION_TAG
)
2912 return kern_spec_to_ib_spec_action(attrs
, kern_spec
, ib_spec
,
2915 return kern_spec_to_ib_spec_filter(kern_spec
, ib_spec
);
2918 static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle
*attrs
)
2920 struct ib_uverbs_ex_create_wq cmd
;
2921 struct ib_uverbs_ex_create_wq_resp resp
= {};
2922 struct ib_uwq_object
*obj
;
2927 struct ib_wq_init_attr wq_init_attr
= {};
2928 struct ib_device
*ib_dev
;
2930 err
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
2937 obj
= (struct ib_uwq_object
*)uobj_alloc(UVERBS_OBJECT_WQ
, attrs
,
2940 return PTR_ERR(obj
);
2942 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
.pd_handle
, attrs
);
2948 cq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
, cmd
.cq_handle
, attrs
);
2954 wq_init_attr
.cq
= cq
;
2955 wq_init_attr
.max_sge
= cmd
.max_sge
;
2956 wq_init_attr
.max_wr
= cmd
.max_wr
;
2957 wq_init_attr
.wq_context
= attrs
->ufile
;
2958 wq_init_attr
.wq_type
= cmd
.wq_type
;
2959 wq_init_attr
.event_handler
= ib_uverbs_wq_event_handler
;
2960 wq_init_attr
.create_flags
= cmd
.create_flags
;
2961 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
2963 wq
= pd
->device
->ops
.create_wq(pd
, &wq_init_attr
, &attrs
->driver_udata
);
2970 obj
->uevent
.uobject
.object
= wq
;
2971 wq
->wq_type
= wq_init_attr
.wq_type
;
2974 wq
->device
= pd
->device
;
2975 wq
->wq_context
= wq_init_attr
.wq_context
;
2976 atomic_set(&wq
->usecnt
, 0);
2977 atomic_inc(&pd
->usecnt
);
2978 atomic_inc(&cq
->usecnt
);
2980 obj
->uevent
.uobject
.object
= wq
;
2982 memset(&resp
, 0, sizeof(resp
));
2983 resp
.wq_handle
= obj
->uevent
.uobject
.id
;
2984 resp
.max_sge
= wq_init_attr
.max_sge
;
2985 resp
.max_wr
= wq_init_attr
.max_wr
;
2986 resp
.wqn
= wq
->wq_num
;
2987 resp
.response_length
= uverbs_response_length(attrs
, sizeof(resp
));
2988 err
= uverbs_response(attrs
, &resp
, sizeof(resp
));
2992 uobj_put_obj_read(pd
);
2993 rdma_lookup_put_uobject(&cq
->uobject
->uevent
.uobject
,
2994 UVERBS_LOOKUP_READ
);
2995 rdma_alloc_commit_uobject(&obj
->uevent
.uobject
, attrs
);
2999 ib_destroy_wq(wq
, uverbs_get_cleared_udata(attrs
));
3001 rdma_lookup_put_uobject(&cq
->uobject
->uevent
.uobject
,
3002 UVERBS_LOOKUP_READ
);
3004 uobj_put_obj_read(pd
);
3006 uobj_alloc_abort(&obj
->uevent
.uobject
, attrs
);
3011 static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle
*attrs
)
3013 struct ib_uverbs_ex_destroy_wq cmd
;
3014 struct ib_uverbs_ex_destroy_wq_resp resp
= {};
3015 struct ib_uobject
*uobj
;
3016 struct ib_uwq_object
*obj
;
3019 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3026 resp
.response_length
= uverbs_response_length(attrs
, sizeof(resp
));
3027 uobj
= uobj_get_destroy(UVERBS_OBJECT_WQ
, cmd
.wq_handle
, attrs
);
3029 return PTR_ERR(uobj
);
3031 obj
= container_of(uobj
, struct ib_uwq_object
, uevent
.uobject
);
3032 resp
.events_reported
= obj
->uevent
.events_reported
;
3034 uobj_put_destroy(uobj
);
3036 return uverbs_response(attrs
, &resp
, sizeof(resp
));
3039 static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle
*attrs
)
3041 struct ib_uverbs_ex_modify_wq cmd
;
3043 struct ib_wq_attr wq_attr
= {};
3046 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3053 if (cmd
.attr_mask
> (IB_WQ_STATE
| IB_WQ_CUR_STATE
| IB_WQ_FLAGS
))
3056 wq
= uobj_get_obj_read(wq
, UVERBS_OBJECT_WQ
, cmd
.wq_handle
, attrs
);
3060 wq_attr
.curr_wq_state
= cmd
.curr_wq_state
;
3061 wq_attr
.wq_state
= cmd
.wq_state
;
3062 if (cmd
.attr_mask
& IB_WQ_FLAGS
) {
3063 wq_attr
.flags
= cmd
.flags
;
3064 wq_attr
.flags_mask
= cmd
.flags_mask
;
3066 ret
= wq
->device
->ops
.modify_wq(wq
, &wq_attr
, cmd
.attr_mask
,
3067 &attrs
->driver_udata
);
3068 rdma_lookup_put_uobject(&wq
->uobject
->uevent
.uobject
,
3069 UVERBS_LOOKUP_READ
);
3073 static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle
*attrs
)
3075 struct ib_uverbs_ex_create_rwq_ind_table cmd
;
3076 struct ib_uverbs_ex_create_rwq_ind_table_resp resp
= {};
3077 struct ib_uobject
*uobj
;
3079 struct ib_rwq_ind_table_init_attr init_attr
= {};
3080 struct ib_rwq_ind_table
*rwq_ind_tbl
;
3081 struct ib_wq
**wqs
= NULL
;
3082 u32
*wqs_handles
= NULL
;
3083 struct ib_wq
*wq
= NULL
;
3084 int i
, j
, num_read_wqs
;
3086 struct uverbs_req_iter iter
;
3087 struct ib_device
*ib_dev
;
3089 err
= uverbs_request_start(attrs
, &iter
, &cmd
, sizeof(cmd
));
3096 if (cmd
.log_ind_tbl_size
> IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE
)
3099 num_wq_handles
= 1 << cmd
.log_ind_tbl_size
;
3100 wqs_handles
= kcalloc(num_wq_handles
, sizeof(*wqs_handles
),
3105 err
= uverbs_request_next(&iter
, wqs_handles
,
3106 num_wq_handles
* sizeof(__u32
));
3110 err
= uverbs_request_finish(&iter
);
3114 wqs
= kcalloc(num_wq_handles
, sizeof(*wqs
), GFP_KERNEL
);
3120 for (num_read_wqs
= 0; num_read_wqs
< num_wq_handles
;
3122 wq
= uobj_get_obj_read(wq
, UVERBS_OBJECT_WQ
,
3123 wqs_handles
[num_read_wqs
], attrs
);
3129 wqs
[num_read_wqs
] = wq
;
3132 uobj
= uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL
, attrs
, &ib_dev
);
3134 err
= PTR_ERR(uobj
);
3138 init_attr
.log_ind_tbl_size
= cmd
.log_ind_tbl_size
;
3139 init_attr
.ind_tbl
= wqs
;
3141 rwq_ind_tbl
= ib_dev
->ops
.create_rwq_ind_table(ib_dev
, &init_attr
,
3142 &attrs
->driver_udata
);
3144 if (IS_ERR(rwq_ind_tbl
)) {
3145 err
= PTR_ERR(rwq_ind_tbl
);
3149 rwq_ind_tbl
->ind_tbl
= wqs
;
3150 rwq_ind_tbl
->log_ind_tbl_size
= init_attr
.log_ind_tbl_size
;
3151 rwq_ind_tbl
->uobject
= uobj
;
3152 uobj
->object
= rwq_ind_tbl
;
3153 rwq_ind_tbl
->device
= ib_dev
;
3154 atomic_set(&rwq_ind_tbl
->usecnt
, 0);
3156 for (i
= 0; i
< num_wq_handles
; i
++)
3157 atomic_inc(&wqs
[i
]->usecnt
);
3159 resp
.ind_tbl_handle
= uobj
->id
;
3160 resp
.ind_tbl_num
= rwq_ind_tbl
->ind_tbl_num
;
3161 resp
.response_length
= uverbs_response_length(attrs
, sizeof(resp
));
3163 err
= uverbs_response(attrs
, &resp
, sizeof(resp
));
3169 for (j
= 0; j
< num_read_wqs
; j
++)
3170 rdma_lookup_put_uobject(&wqs
[j
]->uobject
->uevent
.uobject
,
3171 UVERBS_LOOKUP_READ
);
3173 rdma_alloc_commit_uobject(uobj
, attrs
);
3177 ib_destroy_rwq_ind_table(rwq_ind_tbl
);
3179 uobj_alloc_abort(uobj
, attrs
);
3181 for (j
= 0; j
< num_read_wqs
; j
++)
3182 rdma_lookup_put_uobject(&wqs
[j
]->uobject
->uevent
.uobject
,
3183 UVERBS_LOOKUP_READ
);
3190 static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle
*attrs
)
3192 struct ib_uverbs_ex_destroy_rwq_ind_table cmd
;
3195 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3202 return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL
,
3203 cmd
.ind_tbl_handle
, attrs
);
3206 static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle
*attrs
)
3208 struct ib_uverbs_create_flow cmd
;
3209 struct ib_uverbs_create_flow_resp resp
;
3210 struct ib_uobject
*uobj
;
3211 struct ib_flow
*flow_id
;
3212 struct ib_uverbs_flow_attr
*kern_flow_attr
;
3213 struct ib_flow_attr
*flow_attr
;
3215 struct ib_uflow_resources
*uflow_res
;
3216 struct ib_uverbs_flow_spec_hdr
*kern_spec
;
3217 struct uverbs_req_iter iter
;
3221 struct ib_device
*ib_dev
;
3223 err
= uverbs_request_start(attrs
, &iter
, &cmd
, sizeof(cmd
));
3230 if (!capable(CAP_NET_RAW
))
3233 if (cmd
.flow_attr
.flags
>= IB_FLOW_ATTR_FLAGS_RESERVED
)
3236 if ((cmd
.flow_attr
.flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
) &&
3237 ((cmd
.flow_attr
.type
== IB_FLOW_ATTR_ALL_DEFAULT
) ||
3238 (cmd
.flow_attr
.type
== IB_FLOW_ATTR_MC_DEFAULT
)))
3241 if (cmd
.flow_attr
.num_of_specs
> IB_FLOW_SPEC_SUPPORT_LAYERS
)
3244 if (cmd
.flow_attr
.size
>
3245 (cmd
.flow_attr
.num_of_specs
* sizeof(struct ib_uverbs_flow_spec
)))
3248 if (cmd
.flow_attr
.reserved
[0] ||
3249 cmd
.flow_attr
.reserved
[1])
3252 if (cmd
.flow_attr
.num_of_specs
) {
3253 kern_flow_attr
= kmalloc(sizeof(*kern_flow_attr
) + cmd
.flow_attr
.size
,
3255 if (!kern_flow_attr
)
3258 *kern_flow_attr
= cmd
.flow_attr
;
3259 err
= uverbs_request_next(&iter
, &kern_flow_attr
->flow_specs
,
3260 cmd
.flow_attr
.size
);
3264 kern_flow_attr
= &cmd
.flow_attr
;
3267 err
= uverbs_request_finish(&iter
);
3271 uobj
= uobj_alloc(UVERBS_OBJECT_FLOW
, attrs
, &ib_dev
);
3273 err
= PTR_ERR(uobj
);
3277 qp
= uobj_get_obj_read(qp
, UVERBS_OBJECT_QP
, cmd
.qp_handle
, attrs
);
3283 if (qp
->qp_type
!= IB_QPT_UD
&& qp
->qp_type
!= IB_QPT_RAW_PACKET
) {
3288 flow_attr
= kzalloc(struct_size(flow_attr
, flows
,
3289 cmd
.flow_attr
.num_of_specs
), GFP_KERNEL
);
3294 uflow_res
= flow_resources_alloc(cmd
.flow_attr
.num_of_specs
);
3297 goto err_free_flow_attr
;
3300 flow_attr
->type
= kern_flow_attr
->type
;
3301 flow_attr
->priority
= kern_flow_attr
->priority
;
3302 flow_attr
->num_of_specs
= kern_flow_attr
->num_of_specs
;
3303 flow_attr
->port
= kern_flow_attr
->port
;
3304 flow_attr
->flags
= kern_flow_attr
->flags
;
3305 flow_attr
->size
= sizeof(*flow_attr
);
3307 kern_spec
= kern_flow_attr
->flow_specs
;
3308 ib_spec
= flow_attr
+ 1;
3309 for (i
= 0; i
< flow_attr
->num_of_specs
&&
3310 cmd
.flow_attr
.size
>= sizeof(*kern_spec
) &&
3311 cmd
.flow_attr
.size
>= kern_spec
->size
;
3313 err
= kern_spec_to_ib_spec(
3314 attrs
, (struct ib_uverbs_flow_spec
*)kern_spec
,
3315 ib_spec
, uflow_res
);
3320 ((union ib_flow_spec
*) ib_spec
)->size
;
3321 cmd
.flow_attr
.size
-= kern_spec
->size
;
3322 kern_spec
= ((void *)kern_spec
) + kern_spec
->size
;
3323 ib_spec
+= ((union ib_flow_spec
*) ib_spec
)->size
;
3325 if (cmd
.flow_attr
.size
|| (i
!= flow_attr
->num_of_specs
)) {
3326 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3327 i
, cmd
.flow_attr
.size
);
3332 flow_id
= qp
->device
->ops
.create_flow(
3333 qp
, flow_attr
, IB_FLOW_DOMAIN_USER
, &attrs
->driver_udata
);
3335 if (IS_ERR(flow_id
)) {
3336 err
= PTR_ERR(flow_id
);
3340 ib_set_flow(uobj
, flow_id
, qp
, qp
->device
, uflow_res
);
3342 memset(&resp
, 0, sizeof(resp
));
3343 resp
.flow_handle
= uobj
->id
;
3345 err
= uverbs_response(attrs
, &resp
, sizeof(resp
));
3349 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
3350 UVERBS_LOOKUP_READ
);
3352 if (cmd
.flow_attr
.num_of_specs
)
3353 kfree(kern_flow_attr
);
3354 rdma_alloc_commit_uobject(uobj
, attrs
);
3357 if (!qp
->device
->ops
.destroy_flow(flow_id
))
3358 atomic_dec(&qp
->usecnt
);
3360 ib_uverbs_flow_resources_free(uflow_res
);
3364 rdma_lookup_put_uobject(&qp
->uobject
->uevent
.uobject
,
3365 UVERBS_LOOKUP_READ
);
3367 uobj_alloc_abort(uobj
, attrs
);
3369 if (cmd
.flow_attr
.num_of_specs
)
3370 kfree(kern_flow_attr
);
3374 static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle
*attrs
)
3376 struct ib_uverbs_destroy_flow cmd
;
3379 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3386 return uobj_perform_destroy(UVERBS_OBJECT_FLOW
, cmd
.flow_handle
, attrs
);
3389 static int __uverbs_create_xsrq(struct uverbs_attr_bundle
*attrs
,
3390 struct ib_uverbs_create_xsrq
*cmd
,
3391 struct ib_udata
*udata
)
3393 struct ib_uverbs_create_srq_resp resp
;
3394 struct ib_usrq_object
*obj
;
3397 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
3398 struct ib_srq_init_attr attr
;
3400 struct ib_device
*ib_dev
;
3402 obj
= (struct ib_usrq_object
*)uobj_alloc(UVERBS_OBJECT_SRQ
, attrs
,
3405 return PTR_ERR(obj
);
3407 if (cmd
->srq_type
== IB_SRQT_TM
)
3408 attr
.ext
.tag_matching
.max_num_tags
= cmd
->max_num_tags
;
3410 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3411 xrcd_uobj
= uobj_get_read(UVERBS_OBJECT_XRCD
, cmd
->xrcd_handle
,
3413 if (IS_ERR(xrcd_uobj
)) {
3418 attr
.ext
.xrc
.xrcd
= (struct ib_xrcd
*)xrcd_uobj
->object
;
3419 if (!attr
.ext
.xrc
.xrcd
) {
3424 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
, uobject
);
3425 atomic_inc(&obj
->uxrcd
->refcnt
);
3428 if (ib_srq_has_cq(cmd
->srq_type
)) {
3429 attr
.ext
.cq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
,
3430 cmd
->cq_handle
, attrs
);
3437 pd
= uobj_get_obj_read(pd
, UVERBS_OBJECT_PD
, cmd
->pd_handle
, attrs
);
3443 attr
.event_handler
= ib_uverbs_srq_event_handler
;
3444 attr
.srq_context
= attrs
->ufile
;
3445 attr
.srq_type
= cmd
->srq_type
;
3446 attr
.attr
.max_wr
= cmd
->max_wr
;
3447 attr
.attr
.max_sge
= cmd
->max_sge
;
3448 attr
.attr
.srq_limit
= cmd
->srq_limit
;
3450 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
3452 srq
= rdma_zalloc_drv_obj(ib_dev
, ib_srq
);
3458 srq
->device
= pd
->device
;
3460 srq
->srq_type
= cmd
->srq_type
;
3462 srq
->event_handler
= attr
.event_handler
;
3463 srq
->srq_context
= attr
.srq_context
;
3465 ret
= pd
->device
->ops
.create_srq(srq
, &attr
, udata
);
3469 if (ib_srq_has_cq(cmd
->srq_type
)) {
3470 srq
->ext
.cq
= attr
.ext
.cq
;
3471 atomic_inc(&attr
.ext
.cq
->usecnt
);
3474 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3475 srq
->ext
.xrc
.xrcd
= attr
.ext
.xrc
.xrcd
;
3476 atomic_inc(&attr
.ext
.xrc
.xrcd
->usecnt
);
3479 atomic_inc(&pd
->usecnt
);
3480 atomic_set(&srq
->usecnt
, 0);
3482 obj
->uevent
.uobject
.object
= srq
;
3483 obj
->uevent
.uobject
.user_handle
= cmd
->user_handle
;
3485 memset(&resp
, 0, sizeof resp
);
3486 resp
.srq_handle
= obj
->uevent
.uobject
.id
;
3487 resp
.max_wr
= attr
.attr
.max_wr
;
3488 resp
.max_sge
= attr
.attr
.max_sge
;
3489 if (cmd
->srq_type
== IB_SRQT_XRC
)
3490 resp
.srqn
= srq
->ext
.xrc
.srq_num
;
3492 ret
= uverbs_response(attrs
, &resp
, sizeof(resp
));
3496 if (cmd
->srq_type
== IB_SRQT_XRC
)
3497 uobj_put_read(xrcd_uobj
);
3499 if (ib_srq_has_cq(cmd
->srq_type
))
3500 rdma_lookup_put_uobject(&attr
.ext
.cq
->uobject
->uevent
.uobject
,
3501 UVERBS_LOOKUP_READ
);
3503 uobj_put_obj_read(pd
);
3504 rdma_alloc_commit_uobject(&obj
->uevent
.uobject
, attrs
);
3508 ib_destroy_srq_user(srq
, uverbs_get_cleared_udata(attrs
));
3509 /* It was released in ib_destroy_srq_user */
3514 uobj_put_obj_read(pd
);
3517 if (ib_srq_has_cq(cmd
->srq_type
))
3518 rdma_lookup_put_uobject(&attr
.ext
.cq
->uobject
->uevent
.uobject
,
3519 UVERBS_LOOKUP_READ
);
3522 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3523 atomic_dec(&obj
->uxrcd
->refcnt
);
3524 uobj_put_read(xrcd_uobj
);
3528 uobj_alloc_abort(&obj
->uevent
.uobject
, attrs
);
3532 static int ib_uverbs_create_srq(struct uverbs_attr_bundle
*attrs
)
3534 struct ib_uverbs_create_srq cmd
;
3535 struct ib_uverbs_create_xsrq xcmd
;
3538 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3542 memset(&xcmd
, 0, sizeof(xcmd
));
3543 xcmd
.response
= cmd
.response
;
3544 xcmd
.user_handle
= cmd
.user_handle
;
3545 xcmd
.srq_type
= IB_SRQT_BASIC
;
3546 xcmd
.pd_handle
= cmd
.pd_handle
;
3547 xcmd
.max_wr
= cmd
.max_wr
;
3548 xcmd
.max_sge
= cmd
.max_sge
;
3549 xcmd
.srq_limit
= cmd
.srq_limit
;
3551 return __uverbs_create_xsrq(attrs
, &xcmd
, &attrs
->driver_udata
);
3554 static int ib_uverbs_create_xsrq(struct uverbs_attr_bundle
*attrs
)
3556 struct ib_uverbs_create_xsrq cmd
;
3559 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3563 return __uverbs_create_xsrq(attrs
, &cmd
, &attrs
->driver_udata
);
3566 static int ib_uverbs_modify_srq(struct uverbs_attr_bundle
*attrs
)
3568 struct ib_uverbs_modify_srq cmd
;
3570 struct ib_srq_attr attr
;
3573 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3577 srq
= uobj_get_obj_read(srq
, UVERBS_OBJECT_SRQ
, cmd
.srq_handle
, attrs
);
3581 attr
.max_wr
= cmd
.max_wr
;
3582 attr
.srq_limit
= cmd
.srq_limit
;
3584 ret
= srq
->device
->ops
.modify_srq(srq
, &attr
, cmd
.attr_mask
,
3585 &attrs
->driver_udata
);
3587 rdma_lookup_put_uobject(&srq
->uobject
->uevent
.uobject
,
3588 UVERBS_LOOKUP_READ
);
3593 static int ib_uverbs_query_srq(struct uverbs_attr_bundle
*attrs
)
3595 struct ib_uverbs_query_srq cmd
;
3596 struct ib_uverbs_query_srq_resp resp
;
3597 struct ib_srq_attr attr
;
3601 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3605 srq
= uobj_get_obj_read(srq
, UVERBS_OBJECT_SRQ
, cmd
.srq_handle
, attrs
);
3609 ret
= ib_query_srq(srq
, &attr
);
3611 rdma_lookup_put_uobject(&srq
->uobject
->uevent
.uobject
,
3612 UVERBS_LOOKUP_READ
);
3617 memset(&resp
, 0, sizeof resp
);
3619 resp
.max_wr
= attr
.max_wr
;
3620 resp
.max_sge
= attr
.max_sge
;
3621 resp
.srq_limit
= attr
.srq_limit
;
3623 return uverbs_response(attrs
, &resp
, sizeof(resp
));
3626 static int ib_uverbs_destroy_srq(struct uverbs_attr_bundle
*attrs
)
3628 struct ib_uverbs_destroy_srq cmd
;
3629 struct ib_uverbs_destroy_srq_resp resp
;
3630 struct ib_uobject
*uobj
;
3631 struct ib_uevent_object
*obj
;
3634 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3638 uobj
= uobj_get_destroy(UVERBS_OBJECT_SRQ
, cmd
.srq_handle
, attrs
);
3640 return PTR_ERR(uobj
);
3642 obj
= container_of(uobj
, struct ib_uevent_object
, uobject
);
3643 memset(&resp
, 0, sizeof(resp
));
3644 resp
.events_reported
= obj
->events_reported
;
3646 uobj_put_destroy(uobj
);
3648 return uverbs_response(attrs
, &resp
, sizeof(resp
));
3651 static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle
*attrs
)
3653 struct ib_uverbs_ex_query_device_resp resp
= {};
3654 struct ib_uverbs_ex_query_device cmd
;
3655 struct ib_device_attr attr
= {0};
3656 struct ib_ucontext
*ucontext
;
3657 struct ib_device
*ib_dev
;
3660 ucontext
= ib_uverbs_get_ucontext(attrs
);
3661 if (IS_ERR(ucontext
))
3662 return PTR_ERR(ucontext
);
3663 ib_dev
= ucontext
->device
;
3665 err
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3675 err
= ib_dev
->ops
.query_device(ib_dev
, &attr
, &attrs
->driver_udata
);
3679 copy_query_dev_fields(ucontext
, &resp
.base
, &attr
);
3681 resp
.odp_caps
.general_caps
= attr
.odp_caps
.general_caps
;
3682 resp
.odp_caps
.per_transport_caps
.rc_odp_caps
=
3683 attr
.odp_caps
.per_transport_caps
.rc_odp_caps
;
3684 resp
.odp_caps
.per_transport_caps
.uc_odp_caps
=
3685 attr
.odp_caps
.per_transport_caps
.uc_odp_caps
;
3686 resp
.odp_caps
.per_transport_caps
.ud_odp_caps
=
3687 attr
.odp_caps
.per_transport_caps
.ud_odp_caps
;
3688 resp
.xrc_odp_caps
= attr
.odp_caps
.per_transport_caps
.xrc_odp_caps
;
3690 resp
.timestamp_mask
= attr
.timestamp_mask
;
3691 resp
.hca_core_clock
= attr
.hca_core_clock
;
3692 resp
.device_cap_flags_ex
= attr
.device_cap_flags
;
3693 resp
.rss_caps
.supported_qpts
= attr
.rss_caps
.supported_qpts
;
3694 resp
.rss_caps
.max_rwq_indirection_tables
=
3695 attr
.rss_caps
.max_rwq_indirection_tables
;
3696 resp
.rss_caps
.max_rwq_indirection_table_size
=
3697 attr
.rss_caps
.max_rwq_indirection_table_size
;
3698 resp
.max_wq_type_rq
= attr
.max_wq_type_rq
;
3699 resp
.raw_packet_caps
= attr
.raw_packet_caps
;
3700 resp
.tm_caps
.max_rndv_hdr_size
= attr
.tm_caps
.max_rndv_hdr_size
;
3701 resp
.tm_caps
.max_num_tags
= attr
.tm_caps
.max_num_tags
;
3702 resp
.tm_caps
.max_ops
= attr
.tm_caps
.max_ops
;
3703 resp
.tm_caps
.max_sge
= attr
.tm_caps
.max_sge
;
3704 resp
.tm_caps
.flags
= attr
.tm_caps
.flags
;
3705 resp
.cq_moderation_caps
.max_cq_moderation_count
=
3706 attr
.cq_caps
.max_cq_moderation_count
;
3707 resp
.cq_moderation_caps
.max_cq_moderation_period
=
3708 attr
.cq_caps
.max_cq_moderation_period
;
3709 resp
.max_dm_size
= attr
.max_dm_size
;
3710 resp
.response_length
= uverbs_response_length(attrs
, sizeof(resp
));
3712 return uverbs_response(attrs
, &resp
, sizeof(resp
));
3715 static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle
*attrs
)
3717 struct ib_uverbs_ex_modify_cq cmd
;
3721 ret
= uverbs_request(attrs
, &cmd
, sizeof(cmd
));
3725 if (!cmd
.attr_mask
|| cmd
.reserved
)
3728 if (cmd
.attr_mask
> IB_CQ_MODERATE
)
3731 cq
= uobj_get_obj_read(cq
, UVERBS_OBJECT_CQ
, cmd
.cq_handle
, attrs
);
3735 ret
= rdma_set_cq_moderation(cq
, cmd
.attr
.cq_count
, cmd
.attr
.cq_period
);
3737 rdma_lookup_put_uobject(&cq
->uobject
->uevent
.uobject
,
3738 UVERBS_LOOKUP_READ
);
3743 * Describe the input structs for write(). Some write methods have an input
3744 * only struct, most have an input and output. If the struct has an output then
3745 * the 'response' u64 must be the first field in the request structure.
3747 * If udata is present then both the request and response structs have a
3748 * trailing driver_data flex array. In this case the size of the base struct
3749 * cannot be changed.
3751 #define UAPI_DEF_WRITE_IO(req, resp) \
3752 .write.has_resp = 1 + \
3753 BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) + \
3754 BUILD_BUG_ON_ZERO(sizeof(((req *)0)->response) != \
3756 .write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
3758 #define UAPI_DEF_WRITE_I(req) .write.req_size = sizeof(req)
3760 #define UAPI_DEF_WRITE_UDATA_IO(req, resp) \
3761 UAPI_DEF_WRITE_IO(req, resp), \
3762 .write.has_udata = \
3764 BUILD_BUG_ON_ZERO(offsetof(req, driver_data) != \
3766 BUILD_BUG_ON_ZERO(offsetof(resp, driver_data) != \
3769 #define UAPI_DEF_WRITE_UDATA_I(req) \
3770 UAPI_DEF_WRITE_I(req), \
3771 .write.has_udata = \
3772 1 + BUILD_BUG_ON_ZERO(offsetof(req, driver_data) != \
3776 * The _EX versions are for use with WRITE_EX and allow the last struct member
3777 * to be specified. Buffers that do not include that member will be rejected.
3779 #define UAPI_DEF_WRITE_IO_EX(req, req_last_member, resp, resp_last_member) \
3780 .write.has_resp = 1, \
3781 .write.req_size = offsetofend(req, req_last_member), \
3782 .write.resp_size = offsetofend(resp, resp_last_member)
3784 #define UAPI_DEF_WRITE_I_EX(req, req_last_member) \
3785 .write.req_size = offsetofend(req, req_last_member)
3787 const struct uapi_definition uverbs_def_write_intf
[] = {
3788 DECLARE_UVERBS_OBJECT(
3790 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH
,
3791 ib_uverbs_create_ah
,
3792 UAPI_DEF_WRITE_UDATA_IO(
3793 struct ib_uverbs_create_ah
,
3794 struct ib_uverbs_create_ah_resp
),
3795 UAPI_DEF_METHOD_NEEDS_FN(create_ah
)),
3796 DECLARE_UVERBS_WRITE(
3797 IB_USER_VERBS_CMD_DESTROY_AH
,
3798 ib_uverbs_destroy_ah
,
3799 UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_ah
),
3800 UAPI_DEF_METHOD_NEEDS_FN(destroy_ah
))),
3802 DECLARE_UVERBS_OBJECT(
3803 UVERBS_OBJECT_COMP_CHANNEL
,
3804 DECLARE_UVERBS_WRITE(
3805 IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
,
3806 ib_uverbs_create_comp_channel
,
3808 struct ib_uverbs_create_comp_channel
,
3809 struct ib_uverbs_create_comp_channel_resp
))),
3811 DECLARE_UVERBS_OBJECT(
3813 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ
,
3814 ib_uverbs_create_cq
,
3815 UAPI_DEF_WRITE_UDATA_IO(
3816 struct ib_uverbs_create_cq
,
3817 struct ib_uverbs_create_cq_resp
),
3818 UAPI_DEF_METHOD_NEEDS_FN(create_cq
)),
3819 DECLARE_UVERBS_WRITE(
3820 IB_USER_VERBS_CMD_DESTROY_CQ
,
3821 ib_uverbs_destroy_cq
,
3822 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_cq
,
3823 struct ib_uverbs_destroy_cq_resp
),
3824 UAPI_DEF_METHOD_NEEDS_FN(destroy_cq
)),
3825 DECLARE_UVERBS_WRITE(
3826 IB_USER_VERBS_CMD_POLL_CQ
,
3828 UAPI_DEF_WRITE_IO(struct ib_uverbs_poll_cq
,
3829 struct ib_uverbs_poll_cq_resp
),
3830 UAPI_DEF_METHOD_NEEDS_FN(poll_cq
)),
3831 DECLARE_UVERBS_WRITE(
3832 IB_USER_VERBS_CMD_REQ_NOTIFY_CQ
,
3833 ib_uverbs_req_notify_cq
,
3834 UAPI_DEF_WRITE_I(struct ib_uverbs_req_notify_cq
),
3835 UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq
)),
3836 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ
,
3837 ib_uverbs_resize_cq
,
3838 UAPI_DEF_WRITE_UDATA_IO(
3839 struct ib_uverbs_resize_cq
,
3840 struct ib_uverbs_resize_cq_resp
),
3841 UAPI_DEF_METHOD_NEEDS_FN(resize_cq
)),
3842 DECLARE_UVERBS_WRITE_EX(
3843 IB_USER_VERBS_EX_CMD_CREATE_CQ
,
3844 ib_uverbs_ex_create_cq
,
3845 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_cq
,
3847 struct ib_uverbs_ex_create_cq_resp
,
3849 UAPI_DEF_METHOD_NEEDS_FN(create_cq
)),
3850 DECLARE_UVERBS_WRITE_EX(
3851 IB_USER_VERBS_EX_CMD_MODIFY_CQ
,
3852 ib_uverbs_ex_modify_cq
,
3853 UAPI_DEF_WRITE_I(struct ib_uverbs_ex_modify_cq
),
3854 UAPI_DEF_METHOD_NEEDS_FN(create_cq
))),
3856 DECLARE_UVERBS_OBJECT(
3857 UVERBS_OBJECT_DEVICE
,
3858 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT
,
3859 ib_uverbs_get_context
,
3860 UAPI_DEF_WRITE_UDATA_IO(
3861 struct ib_uverbs_get_context
,
3862 struct ib_uverbs_get_context_resp
)),
3863 DECLARE_UVERBS_WRITE(
3864 IB_USER_VERBS_CMD_QUERY_DEVICE
,
3865 ib_uverbs_query_device
,
3866 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_device
,
3867 struct ib_uverbs_query_device_resp
)),
3868 DECLARE_UVERBS_WRITE(
3869 IB_USER_VERBS_CMD_QUERY_PORT
,
3870 ib_uverbs_query_port
,
3871 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_port
,
3872 struct ib_uverbs_query_port_resp
),
3873 UAPI_DEF_METHOD_NEEDS_FN(query_port
)),
3874 DECLARE_UVERBS_WRITE_EX(
3875 IB_USER_VERBS_EX_CMD_QUERY_DEVICE
,
3876 ib_uverbs_ex_query_device
,
3877 UAPI_DEF_WRITE_IO_EX(
3878 struct ib_uverbs_ex_query_device
,
3880 struct ib_uverbs_ex_query_device_resp
,
3882 UAPI_DEF_METHOD_NEEDS_FN(query_device
)),
3883 UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext
),
3884 UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext
)),
3886 DECLARE_UVERBS_OBJECT(
3888 DECLARE_UVERBS_WRITE_EX(
3889 IB_USER_VERBS_EX_CMD_CREATE_FLOW
,
3890 ib_uverbs_ex_create_flow
,
3891 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_create_flow
,
3893 struct ib_uverbs_create_flow_resp
,
3895 UAPI_DEF_METHOD_NEEDS_FN(create_flow
)),
3896 DECLARE_UVERBS_WRITE_EX(
3897 IB_USER_VERBS_EX_CMD_DESTROY_FLOW
,
3898 ib_uverbs_ex_destroy_flow
,
3899 UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_flow
),
3900 UAPI_DEF_METHOD_NEEDS_FN(destroy_flow
))),
3902 DECLARE_UVERBS_OBJECT(
3904 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR
,
3906 UAPI_DEF_WRITE_I(struct ib_uverbs_dereg_mr
),
3907 UAPI_DEF_METHOD_NEEDS_FN(dereg_mr
)),
3908 DECLARE_UVERBS_WRITE(
3909 IB_USER_VERBS_CMD_REG_MR
,
3911 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_reg_mr
,
3912 struct ib_uverbs_reg_mr_resp
),
3913 UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr
)),
3914 DECLARE_UVERBS_WRITE(
3915 IB_USER_VERBS_CMD_REREG_MR
,
3917 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_rereg_mr
,
3918 struct ib_uverbs_rereg_mr_resp
),
3919 UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr
))),
3921 DECLARE_UVERBS_OBJECT(
3923 DECLARE_UVERBS_WRITE(
3924 IB_USER_VERBS_CMD_ALLOC_MW
,
3926 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_mw
,
3927 struct ib_uverbs_alloc_mw_resp
),
3928 UAPI_DEF_METHOD_NEEDS_FN(alloc_mw
)),
3929 DECLARE_UVERBS_WRITE(
3930 IB_USER_VERBS_CMD_DEALLOC_MW
,
3931 ib_uverbs_dealloc_mw
,
3932 UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_mw
),
3933 UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw
))),
3935 DECLARE_UVERBS_OBJECT(
3937 DECLARE_UVERBS_WRITE(
3938 IB_USER_VERBS_CMD_ALLOC_PD
,
3940 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_pd
,
3941 struct ib_uverbs_alloc_pd_resp
),
3942 UAPI_DEF_METHOD_NEEDS_FN(alloc_pd
)),
3943 DECLARE_UVERBS_WRITE(
3944 IB_USER_VERBS_CMD_DEALLOC_PD
,
3945 ib_uverbs_dealloc_pd
,
3946 UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_pd
),
3947 UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd
))),
3949 DECLARE_UVERBS_OBJECT(
3951 DECLARE_UVERBS_WRITE(
3952 IB_USER_VERBS_CMD_ATTACH_MCAST
,
3953 ib_uverbs_attach_mcast
,
3954 UAPI_DEF_WRITE_I(struct ib_uverbs_attach_mcast
),
3955 UAPI_DEF_METHOD_NEEDS_FN(attach_mcast
),
3956 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast
)),
3957 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP
,
3958 ib_uverbs_create_qp
,
3959 UAPI_DEF_WRITE_UDATA_IO(
3960 struct ib_uverbs_create_qp
,
3961 struct ib_uverbs_create_qp_resp
),
3962 UAPI_DEF_METHOD_NEEDS_FN(create_qp
)),
3963 DECLARE_UVERBS_WRITE(
3964 IB_USER_VERBS_CMD_DESTROY_QP
,
3965 ib_uverbs_destroy_qp
,
3966 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_qp
,
3967 struct ib_uverbs_destroy_qp_resp
),
3968 UAPI_DEF_METHOD_NEEDS_FN(destroy_qp
)),
3969 DECLARE_UVERBS_WRITE(
3970 IB_USER_VERBS_CMD_DETACH_MCAST
,
3971 ib_uverbs_detach_mcast
,
3972 UAPI_DEF_WRITE_I(struct ib_uverbs_detach_mcast
),
3973 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast
)),
3974 DECLARE_UVERBS_WRITE(
3975 IB_USER_VERBS_CMD_MODIFY_QP
,
3976 ib_uverbs_modify_qp
,
3977 UAPI_DEF_WRITE_I(struct ib_uverbs_modify_qp
),
3978 UAPI_DEF_METHOD_NEEDS_FN(modify_qp
)),
3979 DECLARE_UVERBS_WRITE(
3980 IB_USER_VERBS_CMD_POST_RECV
,
3981 ib_uverbs_post_recv
,
3982 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_recv
,
3983 struct ib_uverbs_post_recv_resp
),
3984 UAPI_DEF_METHOD_NEEDS_FN(post_recv
)),
3985 DECLARE_UVERBS_WRITE(
3986 IB_USER_VERBS_CMD_POST_SEND
,
3987 ib_uverbs_post_send
,
3988 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_send
,
3989 struct ib_uverbs_post_send_resp
),
3990 UAPI_DEF_METHOD_NEEDS_FN(post_send
)),
3991 DECLARE_UVERBS_WRITE(
3992 IB_USER_VERBS_CMD_QUERY_QP
,
3994 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_qp
,
3995 struct ib_uverbs_query_qp_resp
),
3996 UAPI_DEF_METHOD_NEEDS_FN(query_qp
)),
3997 DECLARE_UVERBS_WRITE_EX(
3998 IB_USER_VERBS_EX_CMD_CREATE_QP
,
3999 ib_uverbs_ex_create_qp
,
4000 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_qp
,
4002 struct ib_uverbs_ex_create_qp_resp
,
4004 UAPI_DEF_METHOD_NEEDS_FN(create_qp
)),
4005 DECLARE_UVERBS_WRITE_EX(
4006 IB_USER_VERBS_EX_CMD_MODIFY_QP
,
4007 ib_uverbs_ex_modify_qp
,
4008 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_modify_qp
,
4010 struct ib_uverbs_ex_modify_qp_resp
,
4012 UAPI_DEF_METHOD_NEEDS_FN(modify_qp
))),
4014 DECLARE_UVERBS_OBJECT(
4015 UVERBS_OBJECT_RWQ_IND_TBL
,
4016 DECLARE_UVERBS_WRITE_EX(
4017 IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL
,
4018 ib_uverbs_ex_create_rwq_ind_table
,
4019 UAPI_DEF_WRITE_IO_EX(
4020 struct ib_uverbs_ex_create_rwq_ind_table
,
4022 struct ib_uverbs_ex_create_rwq_ind_table_resp
,
4024 UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table
)),
4025 DECLARE_UVERBS_WRITE_EX(
4026 IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL
,
4027 ib_uverbs_ex_destroy_rwq_ind_table
,
4029 struct ib_uverbs_ex_destroy_rwq_ind_table
),
4030 UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table
))),
4032 DECLARE_UVERBS_OBJECT(
4034 DECLARE_UVERBS_WRITE_EX(
4035 IB_USER_VERBS_EX_CMD_CREATE_WQ
,
4036 ib_uverbs_ex_create_wq
,
4037 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_wq
,
4039 struct ib_uverbs_ex_create_wq_resp
,
4041 UAPI_DEF_METHOD_NEEDS_FN(create_wq
)),
4042 DECLARE_UVERBS_WRITE_EX(
4043 IB_USER_VERBS_EX_CMD_DESTROY_WQ
,
4044 ib_uverbs_ex_destroy_wq
,
4045 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_destroy_wq
,
4047 struct ib_uverbs_ex_destroy_wq_resp
,
4049 UAPI_DEF_METHOD_NEEDS_FN(destroy_wq
)),
4050 DECLARE_UVERBS_WRITE_EX(
4051 IB_USER_VERBS_EX_CMD_MODIFY_WQ
,
4052 ib_uverbs_ex_modify_wq
,
4053 UAPI_DEF_WRITE_I_EX(struct ib_uverbs_ex_modify_wq
,
4055 UAPI_DEF_METHOD_NEEDS_FN(modify_wq
))),
4057 DECLARE_UVERBS_OBJECT(
4059 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ
,
4060 ib_uverbs_create_srq
,
4061 UAPI_DEF_WRITE_UDATA_IO(
4062 struct ib_uverbs_create_srq
,
4063 struct ib_uverbs_create_srq_resp
),
4064 UAPI_DEF_METHOD_NEEDS_FN(create_srq
)),
4065 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ
,
4066 ib_uverbs_create_xsrq
,
4067 UAPI_DEF_WRITE_UDATA_IO(
4068 struct ib_uverbs_create_xsrq
,
4069 struct ib_uverbs_create_srq_resp
),
4070 UAPI_DEF_METHOD_NEEDS_FN(create_srq
)),
4071 DECLARE_UVERBS_WRITE(
4072 IB_USER_VERBS_CMD_DESTROY_SRQ
,
4073 ib_uverbs_destroy_srq
,
4074 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_srq
,
4075 struct ib_uverbs_destroy_srq_resp
),
4076 UAPI_DEF_METHOD_NEEDS_FN(destroy_srq
)),
4077 DECLARE_UVERBS_WRITE(
4078 IB_USER_VERBS_CMD_MODIFY_SRQ
,
4079 ib_uverbs_modify_srq
,
4080 UAPI_DEF_WRITE_UDATA_I(struct ib_uverbs_modify_srq
),
4081 UAPI_DEF_METHOD_NEEDS_FN(modify_srq
)),
4082 DECLARE_UVERBS_WRITE(
4083 IB_USER_VERBS_CMD_POST_SRQ_RECV
,
4084 ib_uverbs_post_srq_recv
,
4085 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_srq_recv
,
4086 struct ib_uverbs_post_srq_recv_resp
),
4087 UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv
)),
4088 DECLARE_UVERBS_WRITE(
4089 IB_USER_VERBS_CMD_QUERY_SRQ
,
4090 ib_uverbs_query_srq
,
4091 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_srq
,
4092 struct ib_uverbs_query_srq_resp
),
4093 UAPI_DEF_METHOD_NEEDS_FN(query_srq
))),
4095 DECLARE_UVERBS_OBJECT(
4097 DECLARE_UVERBS_WRITE(
4098 IB_USER_VERBS_CMD_CLOSE_XRCD
,
4099 ib_uverbs_close_xrcd
,
4100 UAPI_DEF_WRITE_I(struct ib_uverbs_close_xrcd
),
4101 UAPI_DEF_METHOD_NEEDS_FN(dealloc_xrcd
)),
4102 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP
,
4104 UAPI_DEF_WRITE_UDATA_IO(
4105 struct ib_uverbs_open_qp
,
4106 struct ib_uverbs_create_qp_resp
)),
4107 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD
,
4108 ib_uverbs_open_xrcd
,
4109 UAPI_DEF_WRITE_UDATA_IO(
4110 struct ib_uverbs_open_xrcd
,
4111 struct ib_uverbs_open_xrcd_resp
),
4112 UAPI_DEF_METHOD_NEEDS_FN(alloc_xrcd
))),