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"
50 static struct ib_uverbs_completion_event_file *
51 _ib_uverbs_lookup_comp_file(s32 fd, const struct uverbs_attr_bundle *attrs)
53 struct ib_uobject *uobj = ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL,
59 uverbs_uobject_get(uobj);
62 return container_of(uobj, struct ib_uverbs_completion_event_file,
65 #define ib_uverbs_lookup_comp_file(_fd, _ufile) \
66 _ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
68 static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs,
69 const char __user *buf, int in_len,
72 struct ib_uverbs_file *file = attrs->ufile;
73 struct ib_uverbs_get_context cmd;
74 struct ib_uverbs_get_context_resp resp;
75 struct ib_ucontext *ucontext;
77 struct ib_rdmacg_object cg_obj;
78 struct ib_device *ib_dev;
81 if (out_len < sizeof resp)
84 if (copy_from_user(&cmd, buf, sizeof cmd))
87 mutex_lock(&file->ucontext_lock);
88 ib_dev = srcu_dereference(file->device->ib_dev,
89 &file->device->disassociate_srcu);
100 ret = ib_rdmacg_try_charge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
104 ucontext = ib_dev->alloc_ucontext(ib_dev, &attrs->driver_udata);
105 if (IS_ERR(ucontext)) {
106 ret = PTR_ERR(ucontext);
110 ucontext->device = ib_dev;
111 ucontext->cg_obj = cg_obj;
112 /* ufile is required when some objects are released */
113 ucontext->ufile = file;
115 ucontext->closing = false;
116 ucontext->cleanup_retryable = false;
118 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
119 mutex_init(&ucontext->per_mm_list_lock);
120 INIT_LIST_HEAD(&ucontext->per_mm_list);
121 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
122 ucontext->invalidate_range = NULL;
126 resp.num_comp_vectors = file->device->num_comp_vectors;
128 ret = get_unused_fd_flags(O_CLOEXEC);
133 filp = ib_uverbs_alloc_async_event_file(file, ib_dev);
139 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
144 fd_install(resp.async_fd, filp);
147 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
148 * only after all writes to setup the ucontext have completed
150 smp_store_release(&file->ucontext, ucontext);
152 mutex_unlock(&file->ucontext_lock);
157 ib_uverbs_free_async_event_file(file);
161 put_unused_fd(resp.async_fd);
164 ib_dev->dealloc_ucontext(ucontext);
167 ib_rdmacg_uncharge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
170 mutex_unlock(&file->ucontext_lock);
174 static void copy_query_dev_fields(struct ib_ucontext *ucontext,
175 struct ib_uverbs_query_device_resp *resp,
176 struct ib_device_attr *attr)
178 struct ib_device *ib_dev = ucontext->device;
180 resp->fw_ver = attr->fw_ver;
181 resp->node_guid = ib_dev->node_guid;
182 resp->sys_image_guid = attr->sys_image_guid;
183 resp->max_mr_size = attr->max_mr_size;
184 resp->page_size_cap = attr->page_size_cap;
185 resp->vendor_id = attr->vendor_id;
186 resp->vendor_part_id = attr->vendor_part_id;
187 resp->hw_ver = attr->hw_ver;
188 resp->max_qp = attr->max_qp;
189 resp->max_qp_wr = attr->max_qp_wr;
190 resp->device_cap_flags = lower_32_bits(attr->device_cap_flags);
191 resp->max_sge = min(attr->max_send_sge, attr->max_recv_sge);
192 resp->max_sge_rd = attr->max_sge_rd;
193 resp->max_cq = attr->max_cq;
194 resp->max_cqe = attr->max_cqe;
195 resp->max_mr = attr->max_mr;
196 resp->max_pd = attr->max_pd;
197 resp->max_qp_rd_atom = attr->max_qp_rd_atom;
198 resp->max_ee_rd_atom = attr->max_ee_rd_atom;
199 resp->max_res_rd_atom = attr->max_res_rd_atom;
200 resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
201 resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
202 resp->atomic_cap = attr->atomic_cap;
203 resp->max_ee = attr->max_ee;
204 resp->max_rdd = attr->max_rdd;
205 resp->max_mw = attr->max_mw;
206 resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
207 resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
208 resp->max_mcast_grp = attr->max_mcast_grp;
209 resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
210 resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
211 resp->max_ah = attr->max_ah;
212 resp->max_fmr = attr->max_fmr;
213 resp->max_map_per_fmr = attr->max_map_per_fmr;
214 resp->max_srq = attr->max_srq;
215 resp->max_srq_wr = attr->max_srq_wr;
216 resp->max_srq_sge = attr->max_srq_sge;
217 resp->max_pkeys = attr->max_pkeys;
218 resp->local_ca_ack_delay = attr->local_ca_ack_delay;
219 resp->phys_port_cnt = ib_dev->phys_port_cnt;
222 static int ib_uverbs_query_device(struct uverbs_attr_bundle *attrs,
223 const char __user *buf, int in_len,
226 struct ib_uverbs_query_device cmd;
227 struct ib_uverbs_query_device_resp resp;
228 struct ib_ucontext *ucontext;
230 ucontext = ib_uverbs_get_ucontext(attrs);
231 if (IS_ERR(ucontext))
232 return PTR_ERR(ucontext);
234 if (out_len < sizeof resp)
237 if (copy_from_user(&cmd, buf, sizeof cmd))
240 memset(&resp, 0, sizeof resp);
241 copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
243 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
250 * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the
251 * PortInfo CapabilityMask, but was extended with unique bits.
253 static u32 make_port_cap_flags(const struct ib_port_attr *attr)
257 /* All IBA CapabilityMask bits are passed through here, except bit 26,
258 * which is overridden with IP_BASED_GIDS. This is due to a historical
259 * mistake in the implementation of IP_BASED_GIDS. Otherwise all other
260 * bits match the IBA definition across all kernel versions.
262 res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS;
265 res |= IB_UVERBS_PCF_IP_BASED_GIDS;
270 static int ib_uverbs_query_port(struct uverbs_attr_bundle *attrs,
271 const char __user *buf, int in_len, int out_len)
273 struct ib_uverbs_query_port cmd;
274 struct ib_uverbs_query_port_resp resp;
275 struct ib_port_attr attr;
277 struct ib_ucontext *ucontext;
278 struct ib_device *ib_dev;
280 ucontext = ib_uverbs_get_ucontext(attrs);
281 if (IS_ERR(ucontext))
282 return PTR_ERR(ucontext);
283 ib_dev = ucontext->device;
285 if (out_len < sizeof resp)
288 if (copy_from_user(&cmd, buf, sizeof cmd))
291 ret = ib_query_port(ib_dev, cmd.port_num, &attr);
295 memset(&resp, 0, sizeof resp);
297 resp.state = attr.state;
298 resp.max_mtu = attr.max_mtu;
299 resp.active_mtu = attr.active_mtu;
300 resp.gid_tbl_len = attr.gid_tbl_len;
301 resp.port_cap_flags = make_port_cap_flags(&attr);
302 resp.max_msg_sz = attr.max_msg_sz;
303 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
304 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
305 resp.pkey_tbl_len = attr.pkey_tbl_len;
307 if (rdma_is_grh_required(ib_dev, cmd.port_num))
308 resp.flags |= IB_UVERBS_QPF_GRH_REQUIRED;
310 if (rdma_cap_opa_ah(ib_dev, cmd.port_num)) {
311 resp.lid = OPA_TO_IB_UCAST_LID(attr.lid);
312 resp.sm_lid = OPA_TO_IB_UCAST_LID(attr.sm_lid);
314 resp.lid = ib_lid_cpu16(attr.lid);
315 resp.sm_lid = ib_lid_cpu16(attr.sm_lid);
318 resp.max_vl_num = attr.max_vl_num;
319 resp.sm_sl = attr.sm_sl;
320 resp.subnet_timeout = attr.subnet_timeout;
321 resp.init_type_reply = attr.init_type_reply;
322 resp.active_width = attr.active_width;
323 resp.active_speed = attr.active_speed;
324 resp.phys_state = attr.phys_state;
325 resp.link_layer = rdma_port_get_link_layer(ib_dev,
328 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
334 static int ib_uverbs_alloc_pd(struct uverbs_attr_bundle *attrs,
335 const char __user *buf, int in_len, int out_len)
337 struct ib_uverbs_alloc_pd cmd;
338 struct ib_uverbs_alloc_pd_resp resp;
339 struct ib_uobject *uobj;
342 struct ib_device *ib_dev;
344 if (out_len < sizeof resp)
347 if (copy_from_user(&cmd, buf, sizeof cmd))
350 uobj = uobj_alloc(UVERBS_OBJECT_PD, attrs, &ib_dev);
352 return PTR_ERR(uobj);
354 pd = ib_dev->alloc_pd(ib_dev, uobj->context, &attrs->driver_udata);
362 pd->__internal_mr = NULL;
363 atomic_set(&pd->usecnt, 0);
366 memset(&resp, 0, sizeof resp);
367 resp.pd_handle = uobj->id;
368 pd->res.type = RDMA_RESTRACK_PD;
369 rdma_restrack_add(&pd->res);
371 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
376 return uobj_alloc_commit(uobj);
382 uobj_alloc_abort(uobj);
386 static int ib_uverbs_dealloc_pd(struct uverbs_attr_bundle *attrs,
387 const char __user *buf, int in_len, int out_len)
389 struct ib_uverbs_dealloc_pd cmd;
391 if (copy_from_user(&cmd, buf, sizeof cmd))
394 return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
397 struct xrcd_table_entry {
399 struct ib_xrcd *xrcd;
403 static int xrcd_table_insert(struct ib_uverbs_device *dev,
405 struct ib_xrcd *xrcd)
407 struct xrcd_table_entry *entry, *scan;
408 struct rb_node **p = &dev->xrcd_tree.rb_node;
409 struct rb_node *parent = NULL;
411 entry = kmalloc(sizeof *entry, GFP_KERNEL);
416 entry->inode = inode;
420 scan = rb_entry(parent, struct xrcd_table_entry, node);
422 if (inode < scan->inode) {
424 } else if (inode > scan->inode) {
432 rb_link_node(&entry->node, parent, p);
433 rb_insert_color(&entry->node, &dev->xrcd_tree);
438 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
441 struct xrcd_table_entry *entry;
442 struct rb_node *p = dev->xrcd_tree.rb_node;
445 entry = rb_entry(p, struct xrcd_table_entry, node);
447 if (inode < entry->inode)
449 else if (inode > entry->inode)
458 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
460 struct xrcd_table_entry *entry;
462 entry = xrcd_table_search(dev, inode);
469 static void xrcd_table_delete(struct ib_uverbs_device *dev,
472 struct xrcd_table_entry *entry;
474 entry = xrcd_table_search(dev, inode);
477 rb_erase(&entry->node, &dev->xrcd_tree);
482 static int ib_uverbs_open_xrcd(struct uverbs_attr_bundle *attrs,
483 const char __user *buf, int in_len, int out_len)
485 struct ib_uverbs_device *ibudev = attrs->ufile->device;
486 struct ib_uverbs_open_xrcd cmd;
487 struct ib_uverbs_open_xrcd_resp resp;
488 struct ib_uxrcd_object *obj;
489 struct ib_xrcd *xrcd = NULL;
490 struct fd f = {NULL, 0};
491 struct inode *inode = NULL;
494 struct ib_device *ib_dev;
496 if (out_len < sizeof resp)
499 if (copy_from_user(&cmd, buf, sizeof cmd))
502 mutex_lock(&ibudev->xrcd_tree_mutex);
505 /* search for file descriptor */
509 goto err_tree_mutex_unlock;
512 inode = file_inode(f.file);
513 xrcd = find_xrcd(ibudev, inode);
514 if (!xrcd && !(cmd.oflags & O_CREAT)) {
515 /* no file descriptor. Need CREATE flag */
517 goto err_tree_mutex_unlock;
520 if (xrcd && cmd.oflags & O_EXCL) {
522 goto err_tree_mutex_unlock;
526 obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, attrs,
530 goto err_tree_mutex_unlock;
534 xrcd = ib_dev->alloc_xrcd(ib_dev, obj->uobject.context,
535 &attrs->driver_udata);
542 xrcd->device = ib_dev;
543 atomic_set(&xrcd->usecnt, 0);
544 mutex_init(&xrcd->tgt_qp_mutex);
545 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
549 atomic_set(&obj->refcnt, 0);
550 obj->uobject.object = xrcd;
551 memset(&resp, 0, sizeof resp);
552 resp.xrcd_handle = obj->uobject.id;
556 /* create new inode/xrcd table entry */
557 ret = xrcd_table_insert(ibudev, inode, xrcd);
559 goto err_dealloc_xrcd;
561 atomic_inc(&xrcd->usecnt);
564 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
572 mutex_unlock(&ibudev->xrcd_tree_mutex);
574 return uobj_alloc_commit(&obj->uobject);
579 xrcd_table_delete(ibudev, inode);
580 atomic_dec(&xrcd->usecnt);
584 ib_dealloc_xrcd(xrcd);
587 uobj_alloc_abort(&obj->uobject);
589 err_tree_mutex_unlock:
593 mutex_unlock(&ibudev->xrcd_tree_mutex);
598 static int ib_uverbs_close_xrcd(struct uverbs_attr_bundle *attrs,
599 const char __user *buf, int in_len, int out_len)
601 struct ib_uverbs_close_xrcd cmd;
603 if (copy_from_user(&cmd, buf, sizeof cmd))
606 return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, attrs);
609 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject,
610 struct ib_xrcd *xrcd,
611 enum rdma_remove_reason why)
615 struct ib_uverbs_device *dev = uobject->context->ufile->device;
618 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
621 ret = ib_dealloc_xrcd(xrcd);
623 if (ib_is_destroy_retryable(ret, why, uobject)) {
624 atomic_inc(&xrcd->usecnt);
629 xrcd_table_delete(dev, inode);
634 static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs,
635 const char __user *buf, int in_len, int out_len)
637 struct ib_uverbs_reg_mr cmd;
638 struct ib_uverbs_reg_mr_resp resp;
639 struct ib_uobject *uobj;
643 struct ib_device *ib_dev;
645 if (out_len < sizeof resp)
648 if (copy_from_user(&cmd, buf, sizeof cmd))
651 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
654 ret = ib_check_mr_access(cmd.access_flags);
658 uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
660 return PTR_ERR(uobj);
662 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
668 if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
669 if (!(pd->device->attrs.device_cap_flags &
670 IB_DEVICE_ON_DEMAND_PAGING)) {
671 pr_debug("ODP support not available\n");
677 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
678 cmd.access_flags, &attrs->driver_udata);
684 mr->device = pd->device;
688 atomic_inc(&pd->usecnt);
689 mr->res.type = RDMA_RESTRACK_MR;
690 rdma_restrack_add(&mr->res);
694 memset(&resp, 0, sizeof resp);
695 resp.lkey = mr->lkey;
696 resp.rkey = mr->rkey;
697 resp.mr_handle = uobj->id;
699 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
704 uobj_put_obj_read(pd);
706 return uobj_alloc_commit(uobj);
712 uobj_put_obj_read(pd);
715 uobj_alloc_abort(uobj);
719 static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs,
720 const char __user *buf, int in_len, int out_len)
722 struct ib_uverbs_rereg_mr cmd;
723 struct ib_uverbs_rereg_mr_resp resp;
724 struct ib_pd *pd = NULL;
726 struct ib_pd *old_pd;
728 struct ib_uobject *uobj;
730 if (out_len < sizeof(resp))
733 if (copy_from_user(&cmd, buf, sizeof(cmd)))
736 if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
739 if ((cmd.flags & IB_MR_REREG_TRANS) &&
740 (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
741 (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
744 uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
746 return PTR_ERR(uobj);
755 if (cmd.flags & IB_MR_REREG_ACCESS) {
756 ret = ib_check_mr_access(cmd.access_flags);
761 if (cmd.flags & IB_MR_REREG_PD) {
762 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
771 ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start, cmd.length,
772 cmd.hca_va, cmd.access_flags, pd,
773 &attrs->driver_udata);
775 if (cmd.flags & IB_MR_REREG_PD) {
776 atomic_inc(&pd->usecnt);
778 atomic_dec(&old_pd->usecnt);
784 memset(&resp, 0, sizeof(resp));
785 resp.lkey = mr->lkey;
786 resp.rkey = mr->rkey;
788 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
794 if (cmd.flags & IB_MR_REREG_PD)
795 uobj_put_obj_read(pd);
798 uobj_put_write(uobj);
803 static int ib_uverbs_dereg_mr(struct uverbs_attr_bundle *attrs,
804 const char __user *buf, int in_len, int out_len)
806 struct ib_uverbs_dereg_mr cmd;
808 if (copy_from_user(&cmd, buf, sizeof cmd))
811 return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
814 static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs,
815 const char __user *buf, int in_len, int out_len)
817 struct ib_uverbs_alloc_mw cmd;
818 struct ib_uverbs_alloc_mw_resp resp;
819 struct ib_uobject *uobj;
823 struct ib_device *ib_dev;
825 if (out_len < sizeof(resp))
828 if (copy_from_user(&cmd, buf, sizeof(cmd)))
831 uobj = uobj_alloc(UVERBS_OBJECT_MW, attrs, &ib_dev);
833 return PTR_ERR(uobj);
835 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
841 mw = pd->device->alloc_mw(pd, cmd.mw_type, &attrs->driver_udata);
847 mw->device = pd->device;
850 atomic_inc(&pd->usecnt);
854 memset(&resp, 0, sizeof(resp));
855 resp.rkey = mw->rkey;
856 resp.mw_handle = uobj->id;
858 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp))) {
863 uobj_put_obj_read(pd);
864 return uobj_alloc_commit(uobj);
867 uverbs_dealloc_mw(mw);
869 uobj_put_obj_read(pd);
871 uobj_alloc_abort(uobj);
875 static int ib_uverbs_dealloc_mw(struct uverbs_attr_bundle *attrs,
876 const char __user *buf, int in_len, int out_len)
878 struct ib_uverbs_dealloc_mw cmd;
880 if (copy_from_user(&cmd, buf, sizeof(cmd)))
883 return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, attrs);
886 static int ib_uverbs_create_comp_channel(struct uverbs_attr_bundle *attrs,
887 const char __user *buf, int in_len,
890 struct ib_uverbs_create_comp_channel cmd;
891 struct ib_uverbs_create_comp_channel_resp resp;
892 struct ib_uobject *uobj;
893 struct ib_uverbs_completion_event_file *ev_file;
894 struct ib_device *ib_dev;
896 if (out_len < sizeof resp)
899 if (copy_from_user(&cmd, buf, sizeof cmd))
902 uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, attrs, &ib_dev);
904 return PTR_ERR(uobj);
908 ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
910 ib_uverbs_init_event_queue(&ev_file->ev_queue);
912 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
913 uobj_alloc_abort(uobj);
917 return uobj_alloc_commit(uobj);
920 static struct ib_ucq_object *create_cq(struct uverbs_attr_bundle *attrs,
921 struct ib_udata *ucore,
922 struct ib_uverbs_ex_create_cq *cmd,
924 int (*cb)(struct uverbs_attr_bundle *attrs,
925 struct ib_ucq_object *obj,
926 struct ib_uverbs_ex_create_cq_resp *resp,
927 struct ib_udata *ucore,
931 struct ib_ucq_object *obj;
932 struct ib_uverbs_completion_event_file *ev_file = NULL;
935 struct ib_uverbs_ex_create_cq_resp resp;
936 struct ib_cq_init_attr attr = {};
937 struct ib_device *ib_dev;
939 if (cmd->comp_vector >= attrs->ufile->device->num_comp_vectors)
940 return ERR_PTR(-EINVAL);
942 obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, attrs,
947 if (cmd->comp_channel >= 0) {
948 ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, attrs);
949 if (IS_ERR(ev_file)) {
950 ret = PTR_ERR(ev_file);
955 obj->uobject.user_handle = cmd->user_handle;
956 obj->comp_events_reported = 0;
957 obj->async_events_reported = 0;
958 INIT_LIST_HEAD(&obj->comp_list);
959 INIT_LIST_HEAD(&obj->async_list);
962 attr.comp_vector = cmd->comp_vector;
964 if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
965 attr.flags = cmd->flags;
967 cq = ib_dev->create_cq(ib_dev, &attr, obj->uobject.context,
968 &attrs->driver_udata);
975 cq->uobject = &obj->uobject;
976 cq->comp_handler = ib_uverbs_comp_handler;
977 cq->event_handler = ib_uverbs_cq_event_handler;
978 cq->cq_context = ev_file ? &ev_file->ev_queue : NULL;
979 atomic_set(&cq->usecnt, 0);
981 obj->uobject.object = cq;
982 memset(&resp, 0, sizeof resp);
983 resp.base.cq_handle = obj->uobject.id;
984 resp.base.cqe = cq->cqe;
986 resp.response_length = offsetof(typeof(resp), response_length) +
987 sizeof(resp.response_length);
989 cq->res.type = RDMA_RESTRACK_CQ;
990 rdma_restrack_add(&cq->res);
992 ret = cb(attrs, obj, &resp, ucore, context);
996 ret = uobj_alloc_commit(&obj->uobject);
1006 ib_uverbs_release_ucq(attrs->ufile, ev_file, obj);
1009 uobj_alloc_abort(&obj->uobject);
1011 return ERR_PTR(ret);
1014 static int ib_uverbs_create_cq_cb(struct uverbs_attr_bundle *attrs,
1015 struct ib_ucq_object *obj,
1016 struct ib_uverbs_ex_create_cq_resp *resp,
1017 struct ib_udata *ucore, void *context)
1019 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1025 static int ib_uverbs_create_cq(struct uverbs_attr_bundle *attrs,
1026 const char __user *buf, int in_len, int out_len)
1028 struct ib_uverbs_create_cq cmd;
1029 struct ib_uverbs_ex_create_cq cmd_ex;
1030 struct ib_uverbs_create_cq_resp resp;
1031 struct ib_udata ucore;
1032 struct ib_ucq_object *obj;
1034 if (out_len < sizeof(resp))
1037 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1040 ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
1041 sizeof(cmd), sizeof(resp));
1043 memset(&cmd_ex, 0, sizeof(cmd_ex));
1044 cmd_ex.user_handle = cmd.user_handle;
1045 cmd_ex.cqe = cmd.cqe;
1046 cmd_ex.comp_vector = cmd.comp_vector;
1047 cmd_ex.comp_channel = cmd.comp_channel;
1049 obj = create_cq(attrs, &ucore, &cmd_ex,
1050 offsetof(typeof(cmd_ex), comp_channel) +
1051 sizeof(cmd.comp_channel),
1052 ib_uverbs_create_cq_cb, NULL);
1055 return PTR_ERR(obj);
1060 static int ib_uverbs_ex_create_cq_cb(struct uverbs_attr_bundle *attrs,
1061 struct ib_ucq_object *obj,
1062 struct ib_uverbs_ex_create_cq_resp *resp,
1063 struct ib_udata *ucore, void *context)
1065 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1071 static int ib_uverbs_ex_create_cq(struct uverbs_attr_bundle *attrs,
1072 struct ib_udata *ucore)
1074 struct ib_uverbs_ex_create_cq_resp resp;
1075 struct ib_uverbs_ex_create_cq cmd;
1076 struct ib_ucq_object *obj;
1079 if (ucore->inlen < sizeof(cmd))
1082 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
1092 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1093 sizeof(resp.response_length)))
1096 obj = create_cq(attrs, ucore, &cmd, min(ucore->inlen, sizeof(cmd)),
1097 ib_uverbs_ex_create_cq_cb, NULL);
1099 return PTR_ERR_OR_ZERO(obj);
1102 static int ib_uverbs_resize_cq(struct uverbs_attr_bundle *attrs,
1103 const char __user *buf, int in_len, int out_len)
1105 struct ib_uverbs_resize_cq cmd;
1106 struct ib_uverbs_resize_cq_resp resp = {};
1110 if (copy_from_user(&cmd, buf, sizeof cmd))
1113 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1117 ret = cq->device->resize_cq(cq, cmd.cqe, &attrs->driver_udata);
1123 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp.cqe))
1127 uobj_put_obj_read(cq);
1132 static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
1135 struct ib_uverbs_wc tmp;
1137 tmp.wr_id = wc->wr_id;
1138 tmp.status = wc->status;
1139 tmp.opcode = wc->opcode;
1140 tmp.vendor_err = wc->vendor_err;
1141 tmp.byte_len = wc->byte_len;
1142 tmp.ex.imm_data = wc->ex.imm_data;
1143 tmp.qp_num = wc->qp->qp_num;
1144 tmp.src_qp = wc->src_qp;
1145 tmp.wc_flags = wc->wc_flags;
1146 tmp.pkey_index = wc->pkey_index;
1147 if (rdma_cap_opa_ah(ib_dev, wc->port_num))
1148 tmp.slid = OPA_TO_IB_UCAST_LID(wc->slid);
1150 tmp.slid = ib_lid_cpu16(wc->slid);
1152 tmp.dlid_path_bits = wc->dlid_path_bits;
1153 tmp.port_num = wc->port_num;
1156 if (copy_to_user(dest, &tmp, sizeof tmp))
1162 static int ib_uverbs_poll_cq(struct uverbs_attr_bundle *attrs,
1163 const char __user *buf, int in_len, int out_len)
1165 struct ib_uverbs_poll_cq cmd;
1166 struct ib_uverbs_poll_cq_resp resp;
1167 u8 __user *header_ptr;
1168 u8 __user *data_ptr;
1173 if (copy_from_user(&cmd, buf, sizeof cmd))
1176 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1180 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1181 header_ptr = u64_to_user_ptr(cmd.response);
1182 data_ptr = header_ptr + sizeof resp;
1184 memset(&resp, 0, sizeof resp);
1185 while (resp.count < cmd.ne) {
1186 ret = ib_poll_cq(cq, 1, &wc);
1192 ret = copy_wc_to_user(cq->device, data_ptr, &wc);
1196 data_ptr += sizeof(struct ib_uverbs_wc);
1200 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1208 uobj_put_obj_read(cq);
1212 static int ib_uverbs_req_notify_cq(struct uverbs_attr_bundle *attrs,
1213 const char __user *buf, int in_len,
1216 struct ib_uverbs_req_notify_cq cmd;
1219 if (copy_from_user(&cmd, buf, sizeof cmd))
1222 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1226 ib_req_notify_cq(cq, cmd.solicited_only ?
1227 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1229 uobj_put_obj_read(cq);
1234 static int ib_uverbs_destroy_cq(struct uverbs_attr_bundle *attrs,
1235 const char __user *buf, int in_len, int out_len)
1237 struct ib_uverbs_destroy_cq cmd;
1238 struct ib_uverbs_destroy_cq_resp resp;
1239 struct ib_uobject *uobj;
1240 struct ib_ucq_object *obj;
1242 if (copy_from_user(&cmd, buf, sizeof cmd))
1245 uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1247 return PTR_ERR(uobj);
1249 obj = container_of(uobj, struct ib_ucq_object, uobject);
1250 memset(&resp, 0, sizeof(resp));
1251 resp.comp_events_reported = obj->comp_events_reported;
1252 resp.async_events_reported = obj->async_events_reported;
1254 uobj_put_destroy(uobj);
1256 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
1262 static int create_qp(struct uverbs_attr_bundle *attrs,
1263 struct ib_udata *ucore,
1264 struct ib_uverbs_ex_create_qp *cmd,
1266 int (*cb)(struct uverbs_attr_bundle *attrs,
1267 struct ib_uverbs_ex_create_qp_resp *resp,
1268 struct ib_udata *udata),
1271 struct ib_uqp_object *obj;
1272 struct ib_device *device;
1273 struct ib_pd *pd = NULL;
1274 struct ib_xrcd *xrcd = NULL;
1275 struct ib_uobject *xrcd_uobj = ERR_PTR(-ENOENT);
1276 struct ib_cq *scq = NULL, *rcq = NULL;
1277 struct ib_srq *srq = NULL;
1280 struct ib_qp_init_attr attr = {};
1281 struct ib_uverbs_ex_create_qp_resp resp;
1283 struct ib_rwq_ind_table *ind_tbl = NULL;
1285 struct ib_device *ib_dev;
1287 if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1290 obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1293 return PTR_ERR(obj);
1295 obj->uevent.uobject.user_handle = cmd->user_handle;
1296 mutex_init(&obj->mcast_lock);
1298 if (cmd_sz >= offsetof(typeof(*cmd), rwq_ind_tbl_handle) +
1299 sizeof(cmd->rwq_ind_tbl_handle) &&
1300 (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE)) {
1301 ind_tbl = uobj_get_obj_read(rwq_ind_table,
1302 UVERBS_OBJECT_RWQ_IND_TBL,
1303 cmd->rwq_ind_tbl_handle, attrs);
1309 attr.rwq_ind_tbl = ind_tbl;
1312 if (cmd_sz > sizeof(*cmd) &&
1313 !ib_is_udata_cleared(ucore, sizeof(*cmd),
1314 cmd_sz - sizeof(*cmd))) {
1319 if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
1324 if (ind_tbl && !cmd->max_send_wr)
1327 if (cmd->qp_type == IB_QPT_XRC_TGT) {
1328 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
1331 if (IS_ERR(xrcd_uobj)) {
1336 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1341 device = xrcd->device;
1343 if (cmd->qp_type == IB_QPT_XRC_INI) {
1344 cmd->max_recv_wr = 0;
1345 cmd->max_recv_sge = 0;
1348 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
1349 cmd->srq_handle, attrs);
1350 if (!srq || srq->srq_type == IB_SRQT_XRC) {
1357 if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1358 rcq = uobj_get_obj_read(
1359 cq, UVERBS_OBJECT_CQ,
1360 cmd->recv_cq_handle, attrs);
1370 scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
1371 cmd->send_cq_handle, attrs);
1374 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
1376 if (!pd || (!scq && has_sq)) {
1381 device = pd->device;
1384 attr.event_handler = ib_uverbs_qp_event_handler;
1385 attr.qp_context = attrs->ufile;
1390 attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1392 attr.qp_type = cmd->qp_type;
1393 attr.create_flags = 0;
1395 attr.cap.max_send_wr = cmd->max_send_wr;
1396 attr.cap.max_recv_wr = cmd->max_recv_wr;
1397 attr.cap.max_send_sge = cmd->max_send_sge;
1398 attr.cap.max_recv_sge = cmd->max_recv_sge;
1399 attr.cap.max_inline_data = cmd->max_inline_data;
1401 obj->uevent.events_reported = 0;
1402 INIT_LIST_HEAD(&obj->uevent.event_list);
1403 INIT_LIST_HEAD(&obj->mcast_list);
1405 if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
1406 sizeof(cmd->create_flags))
1407 attr.create_flags = cmd->create_flags;
1409 if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1410 IB_QP_CREATE_CROSS_CHANNEL |
1411 IB_QP_CREATE_MANAGED_SEND |
1412 IB_QP_CREATE_MANAGED_RECV |
1413 IB_QP_CREATE_SCATTER_FCS |
1414 IB_QP_CREATE_CVLAN_STRIPPING |
1415 IB_QP_CREATE_SOURCE_QPN |
1416 IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
1421 if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
1422 if (!capable(CAP_NET_RAW)) {
1427 attr.source_qpn = cmd->source_qpn;
1430 buf = (void *)cmd + sizeof(*cmd);
1431 if (cmd_sz > sizeof(*cmd))
1432 if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
1433 cmd_sz - sizeof(*cmd) - 1))) {
1438 if (cmd->qp_type == IB_QPT_XRC_TGT)
1439 qp = ib_create_qp(pd, &attr);
1441 qp = _ib_create_qp(device, pd, &attr, &attrs->driver_udata,
1442 &obj->uevent.uobject);
1449 if (cmd->qp_type != IB_QPT_XRC_TGT) {
1450 ret = ib_create_qp_security(qp, device);
1456 qp->send_cq = attr.send_cq;
1457 qp->recv_cq = attr.recv_cq;
1459 qp->rwq_ind_tbl = ind_tbl;
1460 qp->event_handler = attr.event_handler;
1461 qp->qp_context = attr.qp_context;
1462 qp->qp_type = attr.qp_type;
1463 atomic_set(&qp->usecnt, 0);
1464 atomic_inc(&pd->usecnt);
1467 atomic_inc(&attr.send_cq->usecnt);
1469 atomic_inc(&attr.recv_cq->usecnt);
1471 atomic_inc(&attr.srq->usecnt);
1473 atomic_inc(&ind_tbl->usecnt);
1475 /* It is done in _ib_create_qp for other QP types */
1476 qp->uobject = &obj->uevent.uobject;
1479 obj->uevent.uobject.object = qp;
1481 memset(&resp, 0, sizeof resp);
1482 resp.base.qpn = qp->qp_num;
1483 resp.base.qp_handle = obj->uevent.uobject.id;
1484 resp.base.max_recv_sge = attr.cap.max_recv_sge;
1485 resp.base.max_send_sge = attr.cap.max_send_sge;
1486 resp.base.max_recv_wr = attr.cap.max_recv_wr;
1487 resp.base.max_send_wr = attr.cap.max_send_wr;
1488 resp.base.max_inline_data = attr.cap.max_inline_data;
1490 resp.response_length = offsetof(typeof(resp), response_length) +
1491 sizeof(resp.response_length);
1493 ret = cb(attrs, &resp, ucore);
1498 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1500 atomic_inc(&obj->uxrcd->refcnt);
1501 uobj_put_read(xrcd_uobj);
1505 uobj_put_obj_read(pd);
1507 uobj_put_obj_read(scq);
1508 if (rcq && rcq != scq)
1509 uobj_put_obj_read(rcq);
1511 uobj_put_obj_read(srq);
1513 uobj_put_obj_read(ind_tbl);
1515 return uobj_alloc_commit(&obj->uevent.uobject);
1520 if (!IS_ERR(xrcd_uobj))
1521 uobj_put_read(xrcd_uobj);
1523 uobj_put_obj_read(pd);
1525 uobj_put_obj_read(scq);
1526 if (rcq && rcq != scq)
1527 uobj_put_obj_read(rcq);
1529 uobj_put_obj_read(srq);
1531 uobj_put_obj_read(ind_tbl);
1533 uobj_alloc_abort(&obj->uevent.uobject);
1537 static int ib_uverbs_create_qp_cb(struct uverbs_attr_bundle *attrs,
1538 struct ib_uverbs_ex_create_qp_resp *resp,
1539 struct ib_udata *ucore)
1541 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1547 static int ib_uverbs_create_qp(struct uverbs_attr_bundle *attrs,
1548 const char __user *buf, int in_len, int out_len)
1550 struct ib_uverbs_create_qp cmd;
1551 struct ib_uverbs_ex_create_qp cmd_ex;
1552 struct ib_udata ucore;
1553 ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
1556 if (out_len < resp_size)
1559 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1562 ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
1563 sizeof(cmd), resp_size);
1565 memset(&cmd_ex, 0, sizeof(cmd_ex));
1566 cmd_ex.user_handle = cmd.user_handle;
1567 cmd_ex.pd_handle = cmd.pd_handle;
1568 cmd_ex.send_cq_handle = cmd.send_cq_handle;
1569 cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1570 cmd_ex.srq_handle = cmd.srq_handle;
1571 cmd_ex.max_send_wr = cmd.max_send_wr;
1572 cmd_ex.max_recv_wr = cmd.max_recv_wr;
1573 cmd_ex.max_send_sge = cmd.max_send_sge;
1574 cmd_ex.max_recv_sge = cmd.max_recv_sge;
1575 cmd_ex.max_inline_data = cmd.max_inline_data;
1576 cmd_ex.sq_sig_all = cmd.sq_sig_all;
1577 cmd_ex.qp_type = cmd.qp_type;
1578 cmd_ex.is_srq = cmd.is_srq;
1580 err = create_qp(attrs, &ucore, &cmd_ex,
1581 offsetof(typeof(cmd_ex), is_srq) + sizeof(cmd.is_srq),
1582 ib_uverbs_create_qp_cb, NULL);
1590 static int ib_uverbs_ex_create_qp_cb(struct uverbs_attr_bundle *attrs,
1591 struct ib_uverbs_ex_create_qp_resp *resp,
1592 struct ib_udata *ucore)
1594 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1600 static int ib_uverbs_ex_create_qp(struct uverbs_attr_bundle *attrs,
1601 struct ib_udata *ucore)
1603 struct ib_uverbs_ex_create_qp_resp resp;
1604 struct ib_uverbs_ex_create_qp cmd = {0};
1607 if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
1608 sizeof(cmd.comp_mask)))
1611 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
1615 if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
1621 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1622 sizeof(resp.response_length)))
1625 err = create_qp(attrs, ucore, &cmd,
1626 min(ucore->inlen, sizeof(cmd)),
1627 ib_uverbs_ex_create_qp_cb, NULL);
1635 static int ib_uverbs_open_qp(struct uverbs_attr_bundle *attrs,
1636 const char __user *buf, int in_len, int out_len)
1638 struct ib_uverbs_open_qp cmd;
1639 struct ib_uverbs_create_qp_resp resp;
1640 struct ib_uqp_object *obj;
1641 struct ib_xrcd *xrcd;
1642 struct ib_uobject *uninitialized_var(xrcd_uobj);
1644 struct ib_qp_open_attr attr;
1646 struct ib_device *ib_dev;
1648 if (out_len < sizeof resp)
1651 if (copy_from_user(&cmd, buf, sizeof cmd))
1654 obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1657 return PTR_ERR(obj);
1659 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, attrs);
1660 if (IS_ERR(xrcd_uobj)) {
1665 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1671 attr.event_handler = ib_uverbs_qp_event_handler;
1672 attr.qp_context = attrs->ufile;
1673 attr.qp_num = cmd.qpn;
1674 attr.qp_type = cmd.qp_type;
1676 obj->uevent.events_reported = 0;
1677 INIT_LIST_HEAD(&obj->uevent.event_list);
1678 INIT_LIST_HEAD(&obj->mcast_list);
1680 qp = ib_open_qp(xrcd, &attr);
1686 obj->uevent.uobject.object = qp;
1687 obj->uevent.uobject.user_handle = cmd.user_handle;
1689 memset(&resp, 0, sizeof resp);
1690 resp.qpn = qp->qp_num;
1691 resp.qp_handle = obj->uevent.uobject.id;
1693 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
1698 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1699 atomic_inc(&obj->uxrcd->refcnt);
1700 qp->uobject = &obj->uevent.uobject;
1701 uobj_put_read(xrcd_uobj);
1703 return uobj_alloc_commit(&obj->uevent.uobject);
1708 uobj_put_read(xrcd_uobj);
1710 uobj_alloc_abort(&obj->uevent.uobject);
1714 static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
1715 struct rdma_ah_attr *rdma_attr)
1717 const struct ib_global_route *grh;
1719 uverb_attr->dlid = rdma_ah_get_dlid(rdma_attr);
1720 uverb_attr->sl = rdma_ah_get_sl(rdma_attr);
1721 uverb_attr->src_path_bits = rdma_ah_get_path_bits(rdma_attr);
1722 uverb_attr->static_rate = rdma_ah_get_static_rate(rdma_attr);
1723 uverb_attr->is_global = !!(rdma_ah_get_ah_flags(rdma_attr) &
1725 if (uverb_attr->is_global) {
1726 grh = rdma_ah_read_grh(rdma_attr);
1727 memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
1728 uverb_attr->flow_label = grh->flow_label;
1729 uverb_attr->sgid_index = grh->sgid_index;
1730 uverb_attr->hop_limit = grh->hop_limit;
1731 uverb_attr->traffic_class = grh->traffic_class;
1733 uverb_attr->port_num = rdma_ah_get_port_num(rdma_attr);
1736 static int ib_uverbs_query_qp(struct uverbs_attr_bundle *attrs,
1737 const char __user *buf, int in_len, int out_len)
1739 struct ib_uverbs_query_qp cmd;
1740 struct ib_uverbs_query_qp_resp resp;
1742 struct ib_qp_attr *attr;
1743 struct ib_qp_init_attr *init_attr;
1746 if (copy_from_user(&cmd, buf, sizeof cmd))
1749 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1750 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1751 if (!attr || !init_attr) {
1756 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1762 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1764 uobj_put_obj_read(qp);
1769 memset(&resp, 0, sizeof resp);
1771 resp.qp_state = attr->qp_state;
1772 resp.cur_qp_state = attr->cur_qp_state;
1773 resp.path_mtu = attr->path_mtu;
1774 resp.path_mig_state = attr->path_mig_state;
1775 resp.qkey = attr->qkey;
1776 resp.rq_psn = attr->rq_psn;
1777 resp.sq_psn = attr->sq_psn;
1778 resp.dest_qp_num = attr->dest_qp_num;
1779 resp.qp_access_flags = attr->qp_access_flags;
1780 resp.pkey_index = attr->pkey_index;
1781 resp.alt_pkey_index = attr->alt_pkey_index;
1782 resp.sq_draining = attr->sq_draining;
1783 resp.max_rd_atomic = attr->max_rd_atomic;
1784 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1785 resp.min_rnr_timer = attr->min_rnr_timer;
1786 resp.port_num = attr->port_num;
1787 resp.timeout = attr->timeout;
1788 resp.retry_cnt = attr->retry_cnt;
1789 resp.rnr_retry = attr->rnr_retry;
1790 resp.alt_port_num = attr->alt_port_num;
1791 resp.alt_timeout = attr->alt_timeout;
1793 copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
1794 copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
1796 resp.max_send_wr = init_attr->cap.max_send_wr;
1797 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1798 resp.max_send_sge = init_attr->cap.max_send_sge;
1799 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1800 resp.max_inline_data = init_attr->cap.max_inline_data;
1801 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1803 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
1813 /* Remove ignored fields set in the attribute mask */
1814 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1817 case IB_QPT_XRC_INI:
1818 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1819 case IB_QPT_XRC_TGT:
1820 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1827 static void copy_ah_attr_from_uverbs(struct ib_device *dev,
1828 struct rdma_ah_attr *rdma_attr,
1829 struct ib_uverbs_qp_dest *uverb_attr)
1831 rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
1832 if (uverb_attr->is_global) {
1833 rdma_ah_set_grh(rdma_attr, NULL,
1834 uverb_attr->flow_label,
1835 uverb_attr->sgid_index,
1836 uverb_attr->hop_limit,
1837 uverb_attr->traffic_class);
1838 rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
1840 rdma_ah_set_ah_flags(rdma_attr, 0);
1842 rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
1843 rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
1844 rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
1845 rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
1846 rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
1847 rdma_ah_set_make_grd(rdma_attr, false);
1850 static int modify_qp(struct uverbs_attr_bundle *attrs,
1851 struct ib_uverbs_ex_modify_qp *cmd)
1853 struct ib_qp_attr *attr;
1857 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1861 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle,
1868 if ((cmd->base.attr_mask & IB_QP_PORT) &&
1869 !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
1874 if ((cmd->base.attr_mask & IB_QP_AV)) {
1875 if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
1880 if (cmd->base.attr_mask & IB_QP_STATE &&
1881 cmd->base.qp_state == IB_QPS_RTR) {
1882 /* We are in INIT->RTR TRANSITION (if we are not,
1883 * this transition will be rejected in subsequent checks).
1884 * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1885 * but the IB_QP_STATE flag is required.
1887 * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1888 * when IB_QP_AV is set, has required inclusion of a valid
1889 * port number in the primary AV. (AVs are created and handled
1890 * differently for infiniband and ethernet (RoCE) ports).
1892 * Check the port number included in the primary AV against
1893 * the port number in the qp struct, which was set (and saved)
1894 * in the RST->INIT transition.
1896 if (cmd->base.dest.port_num != qp->real_qp->port) {
1901 /* We are in SQD->SQD. (If we are not, this transition will
1902 * be rejected later in the verbs layer checks).
1903 * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1904 * together in the SQD->SQD transition.
1906 * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1907 * verbs layer driver does not track primary port changes
1908 * resulting from path migration. Thus, in SQD, if the primary
1909 * AV is modified, the primary port should also be modified).
1911 * Note that in this transition, the IB_QP_STATE flag
1914 if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1915 == (IB_QP_AV | IB_QP_PORT)) &&
1916 cmd->base.port_num != cmd->base.dest.port_num) {
1920 if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1922 cmd->base.attr_mask |= IB_QP_PORT;
1923 cmd->base.port_num = cmd->base.dest.port_num;
1928 if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
1929 (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
1930 !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
1931 cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
1936 if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
1937 cmd->base.cur_qp_state > IB_QPS_ERR) ||
1938 (cmd->base.attr_mask & IB_QP_STATE &&
1939 cmd->base.qp_state > IB_QPS_ERR)) {
1944 if (cmd->base.attr_mask & IB_QP_STATE)
1945 attr->qp_state = cmd->base.qp_state;
1946 if (cmd->base.attr_mask & IB_QP_CUR_STATE)
1947 attr->cur_qp_state = cmd->base.cur_qp_state;
1948 if (cmd->base.attr_mask & IB_QP_PATH_MTU)
1949 attr->path_mtu = cmd->base.path_mtu;
1950 if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
1951 attr->path_mig_state = cmd->base.path_mig_state;
1952 if (cmd->base.attr_mask & IB_QP_QKEY)
1953 attr->qkey = cmd->base.qkey;
1954 if (cmd->base.attr_mask & IB_QP_RQ_PSN)
1955 attr->rq_psn = cmd->base.rq_psn;
1956 if (cmd->base.attr_mask & IB_QP_SQ_PSN)
1957 attr->sq_psn = cmd->base.sq_psn;
1958 if (cmd->base.attr_mask & IB_QP_DEST_QPN)
1959 attr->dest_qp_num = cmd->base.dest_qp_num;
1960 if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
1961 attr->qp_access_flags = cmd->base.qp_access_flags;
1962 if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
1963 attr->pkey_index = cmd->base.pkey_index;
1964 if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
1965 attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
1966 if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1967 attr->max_rd_atomic = cmd->base.max_rd_atomic;
1968 if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1969 attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
1970 if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
1971 attr->min_rnr_timer = cmd->base.min_rnr_timer;
1972 if (cmd->base.attr_mask & IB_QP_PORT)
1973 attr->port_num = cmd->base.port_num;
1974 if (cmd->base.attr_mask & IB_QP_TIMEOUT)
1975 attr->timeout = cmd->base.timeout;
1976 if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
1977 attr->retry_cnt = cmd->base.retry_cnt;
1978 if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
1979 attr->rnr_retry = cmd->base.rnr_retry;
1980 if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
1981 attr->alt_port_num = cmd->base.alt_port_num;
1982 attr->alt_timeout = cmd->base.alt_timeout;
1983 attr->alt_pkey_index = cmd->base.alt_pkey_index;
1985 if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
1986 attr->rate_limit = cmd->rate_limit;
1988 if (cmd->base.attr_mask & IB_QP_AV)
1989 copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
1992 if (cmd->base.attr_mask & IB_QP_ALT_PATH)
1993 copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
1994 &cmd->base.alt_dest);
1996 ret = ib_modify_qp_with_udata(qp, attr,
1997 modify_qp_mask(qp->qp_type,
1998 cmd->base.attr_mask),
1999 &attrs->driver_udata);
2002 uobj_put_obj_read(qp);
2009 static int ib_uverbs_modify_qp(struct uverbs_attr_bundle *attrs,
2010 const char __user *buf, int in_len, int out_len)
2012 struct ib_uverbs_ex_modify_qp cmd = {};
2014 if (copy_from_user(&cmd.base, buf, sizeof(cmd.base)))
2017 if (cmd.base.attr_mask &
2018 ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
2021 return modify_qp(attrs, &cmd);
2024 static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle *attrs,
2025 struct ib_udata *ucore)
2027 struct ib_uverbs_ex_modify_qp cmd = {};
2031 * Last bit is reserved for extending the attr_mask by
2032 * using another field.
2034 BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
2036 if (ucore->inlen < sizeof(cmd.base))
2039 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
2043 if (cmd.base.attr_mask &
2044 ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
2047 if (ucore->inlen > sizeof(cmd)) {
2048 if (!ib_is_udata_cleared(ucore, sizeof(cmd),
2049 ucore->inlen - sizeof(cmd)))
2053 ret = modify_qp(attrs, &cmd);
2058 static int ib_uverbs_destroy_qp(struct uverbs_attr_bundle *attrs,
2059 const char __user *buf, int in_len, int out_len)
2061 struct ib_uverbs_destroy_qp cmd;
2062 struct ib_uverbs_destroy_qp_resp resp;
2063 struct ib_uobject *uobj;
2064 struct ib_uqp_object *obj;
2066 if (copy_from_user(&cmd, buf, sizeof cmd))
2069 uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2071 return PTR_ERR(uobj);
2073 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2074 memset(&resp, 0, sizeof(resp));
2075 resp.events_reported = obj->uevent.events_reported;
2077 uobj_put_destroy(uobj);
2079 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2085 static void *alloc_wr(size_t wr_size, __u32 num_sge)
2087 if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
2088 sizeof (struct ib_sge))
2091 return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
2092 num_sge * sizeof (struct ib_sge), GFP_KERNEL);
2095 static int ib_uverbs_post_send(struct uverbs_attr_bundle *attrs,
2096 const char __user *buf, int in_len, int out_len)
2098 struct ib_uverbs_post_send cmd;
2099 struct ib_uverbs_post_send_resp resp;
2100 struct ib_uverbs_send_wr *user_wr;
2101 struct ib_send_wr *wr = NULL, *last, *next;
2102 const struct ib_send_wr *bad_wr;
2106 ssize_t ret = -EINVAL;
2109 if (copy_from_user(&cmd, buf, sizeof cmd))
2112 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2113 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2116 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2119 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2123 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2127 is_ud = qp->qp_type == IB_QPT_UD;
2130 for (i = 0; i < cmd.wr_count; ++i) {
2131 if (copy_from_user(user_wr,
2132 buf + sizeof cmd + i * cmd.wqe_size,
2138 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2144 struct ib_ud_wr *ud;
2146 if (user_wr->opcode != IB_WR_SEND &&
2147 user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2152 next_size = sizeof(*ud);
2153 ud = alloc_wr(next_size, user_wr->num_sge);
2159 ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2160 user_wr->wr.ud.ah, attrs);
2166 ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2167 ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2170 } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2171 user_wr->opcode == IB_WR_RDMA_WRITE ||
2172 user_wr->opcode == IB_WR_RDMA_READ) {
2173 struct ib_rdma_wr *rdma;
2175 next_size = sizeof(*rdma);
2176 rdma = alloc_wr(next_size, user_wr->num_sge);
2182 rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2183 rdma->rkey = user_wr->wr.rdma.rkey;
2186 } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2187 user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2188 struct ib_atomic_wr *atomic;
2190 next_size = sizeof(*atomic);
2191 atomic = alloc_wr(next_size, user_wr->num_sge);
2197 atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2198 atomic->compare_add = user_wr->wr.atomic.compare_add;
2199 atomic->swap = user_wr->wr.atomic.swap;
2200 atomic->rkey = user_wr->wr.atomic.rkey;
2203 } else if (user_wr->opcode == IB_WR_SEND ||
2204 user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2205 user_wr->opcode == IB_WR_SEND_WITH_INV) {
2206 next_size = sizeof(*next);
2207 next = alloc_wr(next_size, user_wr->num_sge);
2217 if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2218 user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2220 (__be32 __force) user_wr->ex.imm_data;
2221 } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2222 next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2232 next->wr_id = user_wr->wr_id;
2233 next->num_sge = user_wr->num_sge;
2234 next->opcode = user_wr->opcode;
2235 next->send_flags = user_wr->send_flags;
2237 if (next->num_sge) {
2238 next->sg_list = (void *) next +
2239 ALIGN(next_size, sizeof(struct ib_sge));
2240 if (copy_from_user(next->sg_list,
2242 cmd.wr_count * cmd.wqe_size +
2243 sg_ind * sizeof (struct ib_sge),
2244 next->num_sge * sizeof (struct ib_sge))) {
2248 sg_ind += next->num_sge;
2250 next->sg_list = NULL;
2254 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2256 for (next = wr; next; next = next->next) {
2262 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2266 uobj_put_obj_read(qp);
2269 if (is_ud && ud_wr(wr)->ah)
2270 uobj_put_obj_read(ud_wr(wr)->ah);
2282 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2288 struct ib_uverbs_recv_wr *user_wr;
2289 struct ib_recv_wr *wr = NULL, *last, *next;
2294 if (in_len < wqe_size * wr_count +
2295 sge_count * sizeof (struct ib_uverbs_sge))
2296 return ERR_PTR(-EINVAL);
2298 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2299 return ERR_PTR(-EINVAL);
2301 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2303 return ERR_PTR(-ENOMEM);
2307 for (i = 0; i < wr_count; ++i) {
2308 if (copy_from_user(user_wr, buf + i * wqe_size,
2314 if (user_wr->num_sge + sg_ind > sge_count) {
2319 if (user_wr->num_sge >=
2320 (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
2321 sizeof (struct ib_sge)) {
2326 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2327 user_wr->num_sge * sizeof (struct ib_sge),
2341 next->wr_id = user_wr->wr_id;
2342 next->num_sge = user_wr->num_sge;
2344 if (next->num_sge) {
2345 next->sg_list = (void *) next +
2346 ALIGN(sizeof *next, sizeof (struct ib_sge));
2347 if (copy_from_user(next->sg_list,
2348 buf + wr_count * wqe_size +
2349 sg_ind * sizeof (struct ib_sge),
2350 next->num_sge * sizeof (struct ib_sge))) {
2354 sg_ind += next->num_sge;
2356 next->sg_list = NULL;
2371 return ERR_PTR(ret);
2374 static int ib_uverbs_post_recv(struct uverbs_attr_bundle *attrs,
2375 const char __user *buf, int in_len, int out_len)
2377 struct ib_uverbs_post_recv cmd;
2378 struct ib_uverbs_post_recv_resp resp;
2379 struct ib_recv_wr *wr, *next;
2380 const struct ib_recv_wr *bad_wr;
2382 ssize_t ret = -EINVAL;
2384 if (copy_from_user(&cmd, buf, sizeof cmd))
2387 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2388 in_len - sizeof cmd, cmd.wr_count,
2389 cmd.sge_count, cmd.wqe_size);
2393 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2398 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2400 uobj_put_obj_read(qp);
2402 for (next = wr; next; next = next->next) {
2409 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2422 static int ib_uverbs_post_srq_recv(struct uverbs_attr_bundle *attrs,
2423 const char __user *buf, int in_len,
2426 struct ib_uverbs_post_srq_recv cmd;
2427 struct ib_uverbs_post_srq_recv_resp resp;
2428 struct ib_recv_wr *wr, *next;
2429 const struct ib_recv_wr *bad_wr;
2431 ssize_t ret = -EINVAL;
2433 if (copy_from_user(&cmd, buf, sizeof cmd))
2436 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2437 in_len - sizeof cmd, cmd.wr_count,
2438 cmd.sge_count, cmd.wqe_size);
2442 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
2447 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2449 uobj_put_obj_read(srq);
2452 for (next = wr; next; next = next->next) {
2458 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2471 static int ib_uverbs_create_ah(struct uverbs_attr_bundle *attrs,
2472 const char __user *buf, int in_len, int out_len)
2474 struct ib_uverbs_create_ah cmd;
2475 struct ib_uverbs_create_ah_resp resp;
2476 struct ib_uobject *uobj;
2479 struct rdma_ah_attr attr = {};
2481 struct ib_device *ib_dev;
2483 if (out_len < sizeof resp)
2486 if (copy_from_user(&cmd, buf, sizeof cmd))
2489 uobj = uobj_alloc(UVERBS_OBJECT_AH, attrs, &ib_dev);
2491 return PTR_ERR(uobj);
2493 if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2498 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2504 attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2505 rdma_ah_set_make_grd(&attr, false);
2506 rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2507 rdma_ah_set_sl(&attr, cmd.attr.sl);
2508 rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2509 rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2510 rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2512 if (cmd.attr.is_global) {
2513 rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2514 cmd.attr.grh.sgid_index,
2515 cmd.attr.grh.hop_limit,
2516 cmd.attr.grh.traffic_class);
2517 rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2519 rdma_ah_set_ah_flags(&attr, 0);
2522 ah = rdma_create_user_ah(pd, &attr, &attrs->driver_udata);
2529 uobj->user_handle = cmd.user_handle;
2532 resp.ah_handle = uobj->id;
2534 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
2539 uobj_put_obj_read(pd);
2540 return uobj_alloc_commit(uobj);
2543 rdma_destroy_ah(ah);
2546 uobj_put_obj_read(pd);
2549 uobj_alloc_abort(uobj);
2553 static int ib_uverbs_destroy_ah(struct uverbs_attr_bundle *attrs,
2554 const char __user *buf, int in_len, int out_len)
2556 struct ib_uverbs_destroy_ah cmd;
2558 if (copy_from_user(&cmd, buf, sizeof cmd))
2561 return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, attrs);
2564 static int ib_uverbs_attach_mcast(struct uverbs_attr_bundle *attrs,
2565 const char __user *buf, int in_len,
2568 struct ib_uverbs_attach_mcast cmd;
2570 struct ib_uqp_object *obj;
2571 struct ib_uverbs_mcast_entry *mcast;
2574 if (copy_from_user(&cmd, buf, sizeof cmd))
2577 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2581 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2583 mutex_lock(&obj->mcast_lock);
2584 list_for_each_entry(mcast, &obj->mcast_list, list)
2585 if (cmd.mlid == mcast->lid &&
2586 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2591 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2597 mcast->lid = cmd.mlid;
2598 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2600 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2602 list_add_tail(&mcast->list, &obj->mcast_list);
2607 mutex_unlock(&obj->mcast_lock);
2608 uobj_put_obj_read(qp);
2613 static int ib_uverbs_detach_mcast(struct uverbs_attr_bundle *attrs,
2614 const char __user *buf, int in_len,
2617 struct ib_uverbs_detach_mcast cmd;
2618 struct ib_uqp_object *obj;
2620 struct ib_uverbs_mcast_entry *mcast;
2624 if (copy_from_user(&cmd, buf, sizeof cmd))
2627 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2631 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2632 mutex_lock(&obj->mcast_lock);
2634 list_for_each_entry(mcast, &obj->mcast_list, list)
2635 if (cmd.mlid == mcast->lid &&
2636 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2637 list_del(&mcast->list);
2648 ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2651 mutex_unlock(&obj->mcast_lock);
2652 uobj_put_obj_read(qp);
2656 struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2658 struct ib_uflow_resources *resources;
2660 resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2668 resources->counters =
2669 kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2670 resources->collection =
2671 kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2673 if (!resources->counters || !resources->collection)
2677 resources->max = num_specs;
2681 kfree(resources->counters);
2686 EXPORT_SYMBOL(flow_resources_alloc);
2688 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
2695 for (i = 0; i < uflow_res->collection_num; i++)
2696 atomic_dec(&uflow_res->collection[i]->usecnt);
2698 for (i = 0; i < uflow_res->counters_num; i++)
2699 atomic_dec(&uflow_res->counters[i]->usecnt);
2701 kfree(uflow_res->collection);
2702 kfree(uflow_res->counters);
2705 EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2707 void flow_resources_add(struct ib_uflow_resources *uflow_res,
2708 enum ib_flow_spec_type type,
2711 WARN_ON(uflow_res->num >= uflow_res->max);
2714 case IB_FLOW_SPEC_ACTION_HANDLE:
2715 atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2716 uflow_res->collection[uflow_res->collection_num++] =
2717 (struct ib_flow_action *)ibobj;
2719 case IB_FLOW_SPEC_ACTION_COUNT:
2720 atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2721 uflow_res->counters[uflow_res->counters_num++] =
2722 (struct ib_counters *)ibobj;
2730 EXPORT_SYMBOL(flow_resources_add);
2732 static int kern_spec_to_ib_spec_action(const struct uverbs_attr_bundle *attrs,
2733 struct ib_uverbs_flow_spec *kern_spec,
2734 union ib_flow_spec *ib_spec,
2735 struct ib_uflow_resources *uflow_res)
2737 ib_spec->type = kern_spec->type;
2738 switch (ib_spec->type) {
2739 case IB_FLOW_SPEC_ACTION_TAG:
2740 if (kern_spec->flow_tag.size !=
2741 sizeof(struct ib_uverbs_flow_spec_action_tag))
2744 ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2745 ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2747 case IB_FLOW_SPEC_ACTION_DROP:
2748 if (kern_spec->drop.size !=
2749 sizeof(struct ib_uverbs_flow_spec_action_drop))
2752 ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2754 case IB_FLOW_SPEC_ACTION_HANDLE:
2755 if (kern_spec->action.size !=
2756 sizeof(struct ib_uverbs_flow_spec_action_handle))
2758 ib_spec->action.act = uobj_get_obj_read(flow_action,
2759 UVERBS_OBJECT_FLOW_ACTION,
2760 kern_spec->action.handle,
2762 if (!ib_spec->action.act)
2764 ib_spec->action.size =
2765 sizeof(struct ib_flow_spec_action_handle);
2766 flow_resources_add(uflow_res,
2767 IB_FLOW_SPEC_ACTION_HANDLE,
2768 ib_spec->action.act);
2769 uobj_put_obj_read(ib_spec->action.act);
2771 case IB_FLOW_SPEC_ACTION_COUNT:
2772 if (kern_spec->flow_count.size !=
2773 sizeof(struct ib_uverbs_flow_spec_action_count))
2775 ib_spec->flow_count.counters =
2776 uobj_get_obj_read(counters,
2777 UVERBS_OBJECT_COUNTERS,
2778 kern_spec->flow_count.handle,
2780 if (!ib_spec->flow_count.counters)
2782 ib_spec->flow_count.size =
2783 sizeof(struct ib_flow_spec_action_count);
2784 flow_resources_add(uflow_res,
2785 IB_FLOW_SPEC_ACTION_COUNT,
2786 ib_spec->flow_count.counters);
2787 uobj_put_obj_read(ib_spec->flow_count.counters);
2795 static size_t kern_spec_filter_sz(const struct ib_uverbs_flow_spec_hdr *spec)
2797 /* Returns user space filter size, includes padding */
2798 return (spec->size - sizeof(struct ib_uverbs_flow_spec_hdr)) / 2;
2801 static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2802 u16 ib_real_filter_sz)
2805 * User space filter structures must be 64 bit aligned, otherwise this
2806 * may pass, but we won't handle additional new attributes.
2809 if (kern_filter_size > ib_real_filter_sz) {
2810 if (memchr_inv(kern_spec_filter +
2811 ib_real_filter_sz, 0,
2812 kern_filter_size - ib_real_filter_sz))
2814 return ib_real_filter_sz;
2816 return kern_filter_size;
2819 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2820 const void *kern_spec_mask,
2821 const void *kern_spec_val,
2822 size_t kern_filter_sz,
2823 union ib_flow_spec *ib_spec)
2825 ssize_t actual_filter_sz;
2826 ssize_t ib_filter_sz;
2828 /* User flow spec size must be aligned to 4 bytes */
2829 if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2832 ib_spec->type = type;
2834 if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2837 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2838 case IB_FLOW_SPEC_ETH:
2839 ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
2840 actual_filter_sz = spec_filter_size(kern_spec_mask,
2843 if (actual_filter_sz <= 0)
2845 ib_spec->size = sizeof(struct ib_flow_spec_eth);
2846 memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2847 memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2849 case IB_FLOW_SPEC_IPV4:
2850 ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
2851 actual_filter_sz = spec_filter_size(kern_spec_mask,
2854 if (actual_filter_sz <= 0)
2856 ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2857 memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2858 memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2860 case IB_FLOW_SPEC_IPV6:
2861 ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
2862 actual_filter_sz = spec_filter_size(kern_spec_mask,
2865 if (actual_filter_sz <= 0)
2867 ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2868 memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2869 memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2871 if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2872 (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2875 case IB_FLOW_SPEC_TCP:
2876 case IB_FLOW_SPEC_UDP:
2877 ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
2878 actual_filter_sz = spec_filter_size(kern_spec_mask,
2881 if (actual_filter_sz <= 0)
2883 ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2884 memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2885 memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2887 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2888 ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
2889 actual_filter_sz = spec_filter_size(kern_spec_mask,
2892 if (actual_filter_sz <= 0)
2894 ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
2895 memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
2896 memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
2898 if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
2899 (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
2902 case IB_FLOW_SPEC_ESP:
2903 ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
2904 actual_filter_sz = spec_filter_size(kern_spec_mask,
2907 if (actual_filter_sz <= 0)
2909 ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
2910 memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
2911 memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
2913 case IB_FLOW_SPEC_GRE:
2914 ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
2915 actual_filter_sz = spec_filter_size(kern_spec_mask,
2918 if (actual_filter_sz <= 0)
2920 ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
2921 memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
2922 memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
2924 case IB_FLOW_SPEC_MPLS:
2925 ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
2926 actual_filter_sz = spec_filter_size(kern_spec_mask,
2929 if (actual_filter_sz <= 0)
2931 ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
2932 memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
2933 memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
2941 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
2942 union ib_flow_spec *ib_spec)
2944 ssize_t kern_filter_sz;
2945 void *kern_spec_mask;
2946 void *kern_spec_val;
2948 kern_filter_sz = kern_spec_filter_sz(&kern_spec->hdr);
2950 kern_spec_val = (void *)kern_spec +
2951 sizeof(struct ib_uverbs_flow_spec_hdr);
2952 kern_spec_mask = kern_spec_val + kern_filter_sz;
2954 return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
2957 kern_filter_sz, ib_spec);
2960 static int kern_spec_to_ib_spec(struct uverbs_attr_bundle *attrs,
2961 struct ib_uverbs_flow_spec *kern_spec,
2962 union ib_flow_spec *ib_spec,
2963 struct ib_uflow_resources *uflow_res)
2965 if (kern_spec->reserved)
2968 if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
2969 return kern_spec_to_ib_spec_action(attrs, kern_spec, ib_spec,
2972 return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
2975 static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle *attrs,
2976 struct ib_udata *ucore)
2978 struct ib_uverbs_ex_create_wq cmd = {};
2979 struct ib_uverbs_ex_create_wq_resp resp = {};
2980 struct ib_uwq_object *obj;
2985 struct ib_wq_init_attr wq_init_attr = {};
2986 size_t required_cmd_sz;
2987 size_t required_resp_len;
2988 struct ib_device *ib_dev;
2990 required_cmd_sz = offsetof(typeof(cmd), max_sge) + sizeof(cmd.max_sge);
2991 required_resp_len = offsetof(typeof(resp), wqn) + sizeof(resp.wqn);
2993 if (ucore->inlen < required_cmd_sz)
2996 if (ucore->outlen < required_resp_len)
2999 if (ucore->inlen > sizeof(cmd) &&
3000 !ib_is_udata_cleared(ucore, sizeof(cmd),
3001 ucore->inlen - sizeof(cmd)))
3004 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3011 obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, attrs,
3014 return PTR_ERR(obj);
3016 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
3022 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3028 wq_init_attr.cq = cq;
3029 wq_init_attr.max_sge = cmd.max_sge;
3030 wq_init_attr.max_wr = cmd.max_wr;
3031 wq_init_attr.wq_context = attrs->ufile;
3032 wq_init_attr.wq_type = cmd.wq_type;
3033 wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
3034 if (ucore->inlen >= (offsetof(typeof(cmd), create_flags) +
3035 sizeof(cmd.create_flags)))
3036 wq_init_attr.create_flags = cmd.create_flags;
3037 obj->uevent.events_reported = 0;
3038 INIT_LIST_HEAD(&obj->uevent.event_list);
3040 wq = pd->device->create_wq(pd, &wq_init_attr, &attrs->driver_udata);
3046 wq->uobject = &obj->uevent.uobject;
3047 obj->uevent.uobject.object = wq;
3048 wq->wq_type = wq_init_attr.wq_type;
3051 wq->device = pd->device;
3052 wq->wq_context = wq_init_attr.wq_context;
3053 atomic_set(&wq->usecnt, 0);
3054 atomic_inc(&pd->usecnt);
3055 atomic_inc(&cq->usecnt);
3056 wq->uobject = &obj->uevent.uobject;
3057 obj->uevent.uobject.object = wq;
3059 memset(&resp, 0, sizeof(resp));
3060 resp.wq_handle = obj->uevent.uobject.id;
3061 resp.max_sge = wq_init_attr.max_sge;
3062 resp.max_wr = wq_init_attr.max_wr;
3063 resp.wqn = wq->wq_num;
3064 resp.response_length = required_resp_len;
3065 err = ib_copy_to_udata(ucore,
3066 &resp, resp.response_length);
3070 uobj_put_obj_read(pd);
3071 uobj_put_obj_read(cq);
3072 return uobj_alloc_commit(&obj->uevent.uobject);
3077 uobj_put_obj_read(cq);
3079 uobj_put_obj_read(pd);
3081 uobj_alloc_abort(&obj->uevent.uobject);
3086 static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle *attrs,
3087 struct ib_udata *ucore)
3089 struct ib_uverbs_ex_destroy_wq cmd = {};
3090 struct ib_uverbs_ex_destroy_wq_resp resp = {};
3091 struct ib_uobject *uobj;
3092 struct ib_uwq_object *obj;
3093 size_t required_cmd_sz;
3094 size_t required_resp_len;
3097 required_cmd_sz = offsetof(typeof(cmd), wq_handle) + sizeof(cmd.wq_handle);
3098 required_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
3100 if (ucore->inlen < required_cmd_sz)
3103 if (ucore->outlen < required_resp_len)
3106 if (ucore->inlen > sizeof(cmd) &&
3107 !ib_is_udata_cleared(ucore, sizeof(cmd),
3108 ucore->inlen - sizeof(cmd)))
3111 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3118 resp.response_length = required_resp_len;
3119 uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3121 return PTR_ERR(uobj);
3123 obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3124 resp.events_reported = obj->uevent.events_reported;
3126 uobj_put_destroy(uobj);
3128 return ib_copy_to_udata(ucore, &resp, resp.response_length);
3131 static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle *attrs,
3132 struct ib_udata *ucore)
3134 struct ib_uverbs_ex_modify_wq cmd = {};
3136 struct ib_wq_attr wq_attr = {};
3137 size_t required_cmd_sz;
3140 required_cmd_sz = offsetof(typeof(cmd), curr_wq_state) + sizeof(cmd.curr_wq_state);
3141 if (ucore->inlen < required_cmd_sz)
3144 if (ucore->inlen > sizeof(cmd) &&
3145 !ib_is_udata_cleared(ucore, sizeof(cmd),
3146 ucore->inlen - sizeof(cmd)))
3149 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3156 if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3159 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3163 wq_attr.curr_wq_state = cmd.curr_wq_state;
3164 wq_attr.wq_state = cmd.wq_state;
3165 if (cmd.attr_mask & IB_WQ_FLAGS) {
3166 wq_attr.flags = cmd.flags;
3167 wq_attr.flags_mask = cmd.flags_mask;
3169 ret = wq->device->modify_wq(wq, &wq_attr, cmd.attr_mask,
3170 &attrs->driver_udata);
3171 uobj_put_obj_read(wq);
3175 static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle *attrs,
3176 struct ib_udata *ucore)
3178 struct ib_uverbs_ex_create_rwq_ind_table cmd = {};
3179 struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
3180 struct ib_uobject *uobj;
3182 struct ib_rwq_ind_table_init_attr init_attr = {};
3183 struct ib_rwq_ind_table *rwq_ind_tbl;
3184 struct ib_wq **wqs = NULL;
3185 u32 *wqs_handles = NULL;
3186 struct ib_wq *wq = NULL;
3187 int i, j, num_read_wqs;
3189 u32 expected_in_size;
3190 size_t required_cmd_sz_header;
3191 size_t required_resp_len;
3192 struct ib_device *ib_dev;
3194 required_cmd_sz_header = offsetof(typeof(cmd), log_ind_tbl_size) + sizeof(cmd.log_ind_tbl_size);
3195 required_resp_len = offsetof(typeof(resp), ind_tbl_num) + sizeof(resp.ind_tbl_num);
3197 if (ucore->inlen < required_cmd_sz_header)
3200 if (ucore->outlen < required_resp_len)
3203 err = ib_copy_from_udata(&cmd, ucore, required_cmd_sz_header);
3207 ucore->inbuf += required_cmd_sz_header;
3208 ucore->inlen -= required_cmd_sz_header;
3213 if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3216 num_wq_handles = 1 << cmd.log_ind_tbl_size;
3217 expected_in_size = num_wq_handles * sizeof(__u32);
3218 if (num_wq_handles == 1)
3219 /* input size for wq handles is u64 aligned */
3220 expected_in_size += sizeof(__u32);
3222 if (ucore->inlen < expected_in_size)
3225 if (ucore->inlen > expected_in_size &&
3226 !ib_is_udata_cleared(ucore, expected_in_size,
3227 ucore->inlen - expected_in_size))
3230 wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3235 err = ib_copy_from_udata(wqs_handles, ucore,
3236 num_wq_handles * sizeof(__u32));
3240 wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3246 for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3248 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3249 wqs_handles[num_read_wqs], attrs);
3255 wqs[num_read_wqs] = wq;
3258 uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, attrs, &ib_dev);
3260 err = PTR_ERR(uobj);
3264 init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3265 init_attr.ind_tbl = wqs;
3267 rwq_ind_tbl = ib_dev->create_rwq_ind_table(ib_dev, &init_attr,
3268 &attrs->driver_udata);
3270 if (IS_ERR(rwq_ind_tbl)) {
3271 err = PTR_ERR(rwq_ind_tbl);
3275 rwq_ind_tbl->ind_tbl = wqs;
3276 rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3277 rwq_ind_tbl->uobject = uobj;
3278 uobj->object = rwq_ind_tbl;
3279 rwq_ind_tbl->device = ib_dev;
3280 atomic_set(&rwq_ind_tbl->usecnt, 0);
3282 for (i = 0; i < num_wq_handles; i++)
3283 atomic_inc(&wqs[i]->usecnt);
3285 resp.ind_tbl_handle = uobj->id;
3286 resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3287 resp.response_length = required_resp_len;
3289 err = ib_copy_to_udata(ucore,
3290 &resp, resp.response_length);
3296 for (j = 0; j < num_read_wqs; j++)
3297 uobj_put_obj_read(wqs[j]);
3299 return uobj_alloc_commit(uobj);
3302 ib_destroy_rwq_ind_table(rwq_ind_tbl);
3304 uobj_alloc_abort(uobj);
3306 for (j = 0; j < num_read_wqs; j++)
3307 uobj_put_obj_read(wqs[j]);
3314 static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle *attrs,
3315 struct ib_udata *ucore)
3317 struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
3319 size_t required_cmd_sz;
3321 required_cmd_sz = offsetof(typeof(cmd), ind_tbl_handle) + sizeof(cmd.ind_tbl_handle);
3323 if (ucore->inlen < required_cmd_sz)
3326 if (ucore->inlen > sizeof(cmd) &&
3327 !ib_is_udata_cleared(ucore, sizeof(cmd),
3328 ucore->inlen - sizeof(cmd)))
3331 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3338 return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3339 cmd.ind_tbl_handle, attrs);
3342 static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle *attrs,
3343 struct ib_udata *ucore)
3345 struct ib_uverbs_create_flow cmd;
3346 struct ib_uverbs_create_flow_resp resp;
3347 struct ib_uobject *uobj;
3348 struct ib_flow *flow_id;
3349 struct ib_uverbs_flow_attr *kern_flow_attr;
3350 struct ib_flow_attr *flow_attr;
3352 struct ib_uflow_resources *uflow_res;
3353 struct ib_uverbs_flow_spec_hdr *kern_spec;
3357 struct ib_device *ib_dev;
3359 if (ucore->inlen < sizeof(cmd))
3362 if (ucore->outlen < sizeof(resp))
3365 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3369 ucore->inbuf += sizeof(cmd);
3370 ucore->inlen -= sizeof(cmd);
3375 if (!capable(CAP_NET_RAW))
3378 if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3381 if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3382 ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3383 (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3386 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3389 if (cmd.flow_attr.size > ucore->inlen ||
3390 cmd.flow_attr.size >
3391 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3394 if (cmd.flow_attr.reserved[0] ||
3395 cmd.flow_attr.reserved[1])
3398 if (cmd.flow_attr.num_of_specs) {
3399 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3401 if (!kern_flow_attr)
3404 *kern_flow_attr = cmd.flow_attr;
3405 err = ib_copy_from_udata(&kern_flow_attr->flow_specs, ucore,
3406 cmd.flow_attr.size);
3410 kern_flow_attr = &cmd.flow_attr;
3413 uobj = uobj_alloc(UVERBS_OBJECT_FLOW, attrs, &ib_dev);
3415 err = PTR_ERR(uobj);
3419 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
3425 if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3430 flow_attr = kzalloc(struct_size(flow_attr, flows,
3431 cmd.flow_attr.num_of_specs), GFP_KERNEL);
3436 uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3439 goto err_free_flow_attr;
3442 flow_attr->type = kern_flow_attr->type;
3443 flow_attr->priority = kern_flow_attr->priority;
3444 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3445 flow_attr->port = kern_flow_attr->port;
3446 flow_attr->flags = kern_flow_attr->flags;
3447 flow_attr->size = sizeof(*flow_attr);
3449 kern_spec = kern_flow_attr->flow_specs;
3450 ib_spec = flow_attr + 1;
3451 for (i = 0; i < flow_attr->num_of_specs &&
3452 cmd.flow_attr.size >= sizeof(*kern_spec) &&
3453 cmd.flow_attr.size >= kern_spec->size;
3455 err = kern_spec_to_ib_spec(
3456 attrs, (struct ib_uverbs_flow_spec *)kern_spec,
3457 ib_spec, uflow_res);
3462 ((union ib_flow_spec *) ib_spec)->size;
3463 cmd.flow_attr.size -= kern_spec->size;
3464 kern_spec = ((void *)kern_spec) + kern_spec->size;
3465 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3467 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3468 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3469 i, cmd.flow_attr.size);
3474 flow_id = qp->device->create_flow(qp, flow_attr, IB_FLOW_DOMAIN_USER,
3475 &attrs->driver_udata);
3477 if (IS_ERR(flow_id)) {
3478 err = PTR_ERR(flow_id);
3482 ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3484 memset(&resp, 0, sizeof(resp));
3485 resp.flow_handle = uobj->id;
3487 err = ib_copy_to_udata(ucore,
3488 &resp, sizeof(resp));
3492 uobj_put_obj_read(qp);
3494 if (cmd.flow_attr.num_of_specs)
3495 kfree(kern_flow_attr);
3496 return uobj_alloc_commit(uobj);
3498 if (!qp->device->destroy_flow(flow_id))
3499 atomic_dec(&qp->usecnt);
3501 ib_uverbs_flow_resources_free(uflow_res);
3505 uobj_put_obj_read(qp);
3507 uobj_alloc_abort(uobj);
3509 if (cmd.flow_attr.num_of_specs)
3510 kfree(kern_flow_attr);
3514 static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle *attrs,
3515 struct ib_udata *ucore)
3517 struct ib_uverbs_destroy_flow cmd;
3520 if (ucore->inlen < sizeof(cmd))
3523 ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3530 return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, attrs);
3533 static int __uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3534 struct ib_uverbs_create_xsrq *cmd,
3535 struct ib_udata *udata)
3537 struct ib_uverbs_create_srq_resp resp;
3538 struct ib_usrq_object *obj;
3541 struct ib_uobject *uninitialized_var(xrcd_uobj);
3542 struct ib_srq_init_attr attr;
3544 struct ib_device *ib_dev;
3546 obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, attrs,
3549 return PTR_ERR(obj);
3551 if (cmd->srq_type == IB_SRQT_TM)
3552 attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3554 if (cmd->srq_type == IB_SRQT_XRC) {
3555 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3557 if (IS_ERR(xrcd_uobj)) {
3562 attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3563 if (!attr.ext.xrc.xrcd) {
3568 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3569 atomic_inc(&obj->uxrcd->refcnt);
3572 if (ib_srq_has_cq(cmd->srq_type)) {
3573 attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3574 cmd->cq_handle, attrs);
3581 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, attrs);
3587 attr.event_handler = ib_uverbs_srq_event_handler;
3588 attr.srq_context = attrs->ufile;
3589 attr.srq_type = cmd->srq_type;
3590 attr.attr.max_wr = cmd->max_wr;
3591 attr.attr.max_sge = cmd->max_sge;
3592 attr.attr.srq_limit = cmd->srq_limit;
3594 obj->uevent.events_reported = 0;
3595 INIT_LIST_HEAD(&obj->uevent.event_list);
3597 srq = pd->device->create_srq(pd, &attr, udata);
3603 srq->device = pd->device;
3605 srq->srq_type = cmd->srq_type;
3606 srq->uobject = &obj->uevent.uobject;
3607 srq->event_handler = attr.event_handler;
3608 srq->srq_context = attr.srq_context;
3610 if (ib_srq_has_cq(cmd->srq_type)) {
3611 srq->ext.cq = attr.ext.cq;
3612 atomic_inc(&attr.ext.cq->usecnt);
3615 if (cmd->srq_type == IB_SRQT_XRC) {
3616 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
3617 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
3620 atomic_inc(&pd->usecnt);
3621 atomic_set(&srq->usecnt, 0);
3623 obj->uevent.uobject.object = srq;
3624 obj->uevent.uobject.user_handle = cmd->user_handle;
3626 memset(&resp, 0, sizeof resp);
3627 resp.srq_handle = obj->uevent.uobject.id;
3628 resp.max_wr = attr.attr.max_wr;
3629 resp.max_sge = attr.attr.max_sge;
3630 if (cmd->srq_type == IB_SRQT_XRC)
3631 resp.srqn = srq->ext.xrc.srq_num;
3633 if (copy_to_user(u64_to_user_ptr(cmd->response),
3634 &resp, sizeof resp)) {
3639 if (cmd->srq_type == IB_SRQT_XRC)
3640 uobj_put_read(xrcd_uobj);
3642 if (ib_srq_has_cq(cmd->srq_type))
3643 uobj_put_obj_read(attr.ext.cq);
3645 uobj_put_obj_read(pd);
3646 return uobj_alloc_commit(&obj->uevent.uobject);
3649 ib_destroy_srq(srq);
3652 uobj_put_obj_read(pd);
3655 if (ib_srq_has_cq(cmd->srq_type))
3656 uobj_put_obj_read(attr.ext.cq);
3659 if (cmd->srq_type == IB_SRQT_XRC) {
3660 atomic_dec(&obj->uxrcd->refcnt);
3661 uobj_put_read(xrcd_uobj);
3665 uobj_alloc_abort(&obj->uevent.uobject);
3669 static int ib_uverbs_create_srq(struct uverbs_attr_bundle *attrs,
3670 const char __user *buf, int in_len, int out_len)
3672 struct ib_uverbs_create_srq cmd;
3673 struct ib_uverbs_create_xsrq xcmd;
3674 struct ib_uverbs_create_srq_resp resp;
3676 if (out_len < sizeof resp)
3679 if (copy_from_user(&cmd, buf, sizeof cmd))
3682 memset(&xcmd, 0, sizeof(xcmd));
3683 xcmd.response = cmd.response;
3684 xcmd.user_handle = cmd.user_handle;
3685 xcmd.srq_type = IB_SRQT_BASIC;
3686 xcmd.pd_handle = cmd.pd_handle;
3687 xcmd.max_wr = cmd.max_wr;
3688 xcmd.max_sge = cmd.max_sge;
3689 xcmd.srq_limit = cmd.srq_limit;
3691 return __uverbs_create_xsrq(attrs, &xcmd, &attrs->driver_udata);
3694 static int ib_uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3695 const char __user *buf, int in_len,
3698 struct ib_uverbs_create_xsrq cmd;
3699 struct ib_uverbs_create_srq_resp resp;
3701 if (out_len < sizeof resp)
3704 if (copy_from_user(&cmd, buf, sizeof cmd))
3707 return __uverbs_create_xsrq(attrs, &cmd, &attrs->driver_udata);
3710 static int ib_uverbs_modify_srq(struct uverbs_attr_bundle *attrs,
3711 const char __user *buf, int in_len, int out_len)
3713 struct ib_uverbs_modify_srq cmd;
3715 struct ib_srq_attr attr;
3718 if (copy_from_user(&cmd, buf, sizeof cmd))
3721 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3725 attr.max_wr = cmd.max_wr;
3726 attr.srq_limit = cmd.srq_limit;
3728 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask,
3729 &attrs->driver_udata);
3731 uobj_put_obj_read(srq);
3736 static int ib_uverbs_query_srq(struct uverbs_attr_bundle *attrs,
3737 const char __user *buf, int in_len, int out_len)
3739 struct ib_uverbs_query_srq cmd;
3740 struct ib_uverbs_query_srq_resp resp;
3741 struct ib_srq_attr attr;
3745 if (out_len < sizeof resp)
3748 if (copy_from_user(&cmd, buf, sizeof cmd))
3751 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3755 ret = ib_query_srq(srq, &attr);
3757 uobj_put_obj_read(srq);
3762 memset(&resp, 0, sizeof resp);
3764 resp.max_wr = attr.max_wr;
3765 resp.max_sge = attr.max_sge;
3766 resp.srq_limit = attr.srq_limit;
3768 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
3774 static int ib_uverbs_destroy_srq(struct uverbs_attr_bundle *attrs,
3775 const char __user *buf, int in_len,
3778 struct ib_uverbs_destroy_srq cmd;
3779 struct ib_uverbs_destroy_srq_resp resp;
3780 struct ib_uobject *uobj;
3781 struct ib_uevent_object *obj;
3783 if (copy_from_user(&cmd, buf, sizeof cmd))
3786 uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3788 return PTR_ERR(uobj);
3790 obj = container_of(uobj, struct ib_uevent_object, uobject);
3791 memset(&resp, 0, sizeof(resp));
3792 resp.events_reported = obj->events_reported;
3794 uobj_put_destroy(uobj);
3796 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
3802 static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle *attrs,
3803 struct ib_udata *ucore)
3805 struct ib_uverbs_ex_query_device_resp resp = { {0} };
3806 struct ib_uverbs_ex_query_device cmd;
3807 struct ib_device_attr attr = {0};
3808 struct ib_ucontext *ucontext;
3809 struct ib_device *ib_dev;
3812 ucontext = ib_uverbs_get_ucontext(attrs);
3813 if (IS_ERR(ucontext))
3814 return PTR_ERR(ucontext);
3815 ib_dev = ucontext->device;
3817 if (ucore->inlen < sizeof(cmd))
3820 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3830 resp.response_length = offsetof(typeof(resp), odp_caps);
3832 if (ucore->outlen < resp.response_length)
3835 err = ib_dev->query_device(ib_dev, &attr, &attrs->driver_udata);
3839 copy_query_dev_fields(ucontext, &resp.base, &attr);
3841 if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
3844 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3845 resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3846 resp.odp_caps.per_transport_caps.rc_odp_caps =
3847 attr.odp_caps.per_transport_caps.rc_odp_caps;
3848 resp.odp_caps.per_transport_caps.uc_odp_caps =
3849 attr.odp_caps.per_transport_caps.uc_odp_caps;
3850 resp.odp_caps.per_transport_caps.ud_odp_caps =
3851 attr.odp_caps.per_transport_caps.ud_odp_caps;
3853 resp.response_length += sizeof(resp.odp_caps);
3855 if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
3858 resp.timestamp_mask = attr.timestamp_mask;
3859 resp.response_length += sizeof(resp.timestamp_mask);
3861 if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
3864 resp.hca_core_clock = attr.hca_core_clock;
3865 resp.response_length += sizeof(resp.hca_core_clock);
3867 if (ucore->outlen < resp.response_length + sizeof(resp.device_cap_flags_ex))
3870 resp.device_cap_flags_ex = attr.device_cap_flags;
3871 resp.response_length += sizeof(resp.device_cap_flags_ex);
3873 if (ucore->outlen < resp.response_length + sizeof(resp.rss_caps))
3876 resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
3877 resp.rss_caps.max_rwq_indirection_tables =
3878 attr.rss_caps.max_rwq_indirection_tables;
3879 resp.rss_caps.max_rwq_indirection_table_size =
3880 attr.rss_caps.max_rwq_indirection_table_size;
3882 resp.response_length += sizeof(resp.rss_caps);
3884 if (ucore->outlen < resp.response_length + sizeof(resp.max_wq_type_rq))
3887 resp.max_wq_type_rq = attr.max_wq_type_rq;
3888 resp.response_length += sizeof(resp.max_wq_type_rq);
3890 if (ucore->outlen < resp.response_length + sizeof(resp.raw_packet_caps))
3893 resp.raw_packet_caps = attr.raw_packet_caps;
3894 resp.response_length += sizeof(resp.raw_packet_caps);
3896 if (ucore->outlen < resp.response_length + sizeof(resp.tm_caps))
3899 resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
3900 resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
3901 resp.tm_caps.max_ops = attr.tm_caps.max_ops;
3902 resp.tm_caps.max_sge = attr.tm_caps.max_sge;
3903 resp.tm_caps.flags = attr.tm_caps.flags;
3904 resp.response_length += sizeof(resp.tm_caps);
3906 if (ucore->outlen < resp.response_length + sizeof(resp.cq_moderation_caps))
3909 resp.cq_moderation_caps.max_cq_moderation_count =
3910 attr.cq_caps.max_cq_moderation_count;
3911 resp.cq_moderation_caps.max_cq_moderation_period =
3912 attr.cq_caps.max_cq_moderation_period;
3913 resp.response_length += sizeof(resp.cq_moderation_caps);
3915 if (ucore->outlen < resp.response_length + sizeof(resp.max_dm_size))
3918 resp.max_dm_size = attr.max_dm_size;
3919 resp.response_length += sizeof(resp.max_dm_size);
3921 err = ib_copy_to_udata(ucore, &resp, resp.response_length);
3925 static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs,
3926 struct ib_udata *ucore)
3928 struct ib_uverbs_ex_modify_cq cmd = {};
3930 size_t required_cmd_sz;
3933 required_cmd_sz = offsetof(typeof(cmd), reserved) +
3934 sizeof(cmd.reserved);
3935 if (ucore->inlen < required_cmd_sz)
3939 if (ucore->inlen > sizeof(cmd) &&
3940 !ib_is_udata_cleared(ucore, sizeof(cmd),
3941 ucore->inlen - sizeof(cmd)))
3944 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3948 if (!cmd.attr_mask || cmd.reserved)
3951 if (cmd.attr_mask > IB_CQ_MODERATE)
3954 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3958 ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
3960 uobj_put_obj_read(cq);
3966 * Describe the input structs for write(). Some write methods have an input
3967 * only struct, most have an input and output. If the struct has an output then
3968 * the 'response' u64 must be the first field in the request structure.
3970 * If udata is present then both the request and response structs have a
3971 * trailing driver_data flex array. In this case the size of the base struct
3972 * cannot be changed.
3974 #define offsetof_after(_struct, _member) \
3975 (offsetof(_struct, _member) + sizeof(((_struct *)NULL)->_member))
3977 #define UAPI_DEF_WRITE_IO(req, resp) \
3978 .write.has_resp = 1 + \
3979 BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) + \
3980 BUILD_BUG_ON_ZERO(sizeof(((req *)0)->response) != \
3982 .write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
3984 #define UAPI_DEF_WRITE_I(req) .write.req_size = sizeof(req)
3986 #define UAPI_DEF_WRITE_UDATA_IO(req, resp) \
3987 UAPI_DEF_WRITE_IO(req, resp), \
3988 .write.has_udata = \
3990 BUILD_BUG_ON_ZERO(offsetof(req, driver_data) != \
3992 BUILD_BUG_ON_ZERO(offsetof(resp, driver_data) != \
3995 #define UAPI_DEF_WRITE_UDATA_I(req) \
3996 UAPI_DEF_WRITE_I(req), \
3997 .write.has_udata = \
3998 1 + BUILD_BUG_ON_ZERO(offsetof(req, driver_data) != \
4002 * The _EX versions are for use with WRITE_EX and allow the last struct member
4003 * to be specified. Buffers that do not include that member will be rejected.
4005 #define UAPI_DEF_WRITE_IO_EX(req, req_last_member, resp, resp_last_member) \
4006 .write.has_resp = 1, \
4007 .write.req_size = offsetof_after(req, req_last_member), \
4008 .write.resp_size = offsetof_after(resp, resp_last_member)
4010 #define UAPI_DEF_WRITE_I_EX(req, req_last_member) \
4011 .write.req_size = offsetof_after(req, req_last_member)
4013 const struct uapi_definition uverbs_def_write_intf[] = {
4014 DECLARE_UVERBS_OBJECT(
4016 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH,
4017 ib_uverbs_create_ah,
4018 UAPI_DEF_WRITE_UDATA_IO(
4019 struct ib_uverbs_create_ah,
4020 struct ib_uverbs_create_ah_resp),
4021 UAPI_DEF_METHOD_NEEDS_FN(create_ah)),
4022 DECLARE_UVERBS_WRITE(
4023 IB_USER_VERBS_CMD_DESTROY_AH,
4024 ib_uverbs_destroy_ah,
4025 UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_ah),
4026 UAPI_DEF_METHOD_NEEDS_FN(destroy_ah))),
4028 DECLARE_UVERBS_OBJECT(
4029 UVERBS_OBJECT_COMP_CHANNEL,
4030 DECLARE_UVERBS_WRITE(
4031 IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL,
4032 ib_uverbs_create_comp_channel,
4034 struct ib_uverbs_create_comp_channel,
4035 struct ib_uverbs_create_comp_channel_resp))),
4037 DECLARE_UVERBS_OBJECT(
4039 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ,
4040 ib_uverbs_create_cq,
4041 UAPI_DEF_WRITE_UDATA_IO(
4042 struct ib_uverbs_create_cq,
4043 struct ib_uverbs_create_cq_resp),
4044 UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
4045 DECLARE_UVERBS_WRITE(
4046 IB_USER_VERBS_CMD_DESTROY_CQ,
4047 ib_uverbs_destroy_cq,
4048 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_cq,
4049 struct ib_uverbs_destroy_cq_resp),
4050 UAPI_DEF_METHOD_NEEDS_FN(destroy_cq)),
4051 DECLARE_UVERBS_WRITE(
4052 IB_USER_VERBS_CMD_POLL_CQ,
4054 UAPI_DEF_WRITE_IO(struct ib_uverbs_poll_cq,
4055 struct ib_uverbs_poll_cq_resp),
4056 UAPI_DEF_METHOD_NEEDS_FN(poll_cq)),
4057 DECLARE_UVERBS_WRITE(
4058 IB_USER_VERBS_CMD_REQ_NOTIFY_CQ,
4059 ib_uverbs_req_notify_cq,
4060 UAPI_DEF_WRITE_I(struct ib_uverbs_req_notify_cq),
4061 UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq)),
4062 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ,
4063 ib_uverbs_resize_cq,
4064 UAPI_DEF_WRITE_UDATA_IO(
4065 struct ib_uverbs_resize_cq,
4066 struct ib_uverbs_resize_cq_resp),
4067 UAPI_DEF_METHOD_NEEDS_FN(resize_cq)),
4068 DECLARE_UVERBS_WRITE_EX(
4069 IB_USER_VERBS_EX_CMD_CREATE_CQ,
4070 ib_uverbs_ex_create_cq,
4071 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_cq,
4073 struct ib_uverbs_ex_create_cq_resp,
4075 UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
4076 DECLARE_UVERBS_WRITE_EX(
4077 IB_USER_VERBS_EX_CMD_MODIFY_CQ,
4078 ib_uverbs_ex_modify_cq,
4079 UAPI_DEF_WRITE_I(struct ib_uverbs_ex_modify_cq),
4080 UAPI_DEF_METHOD_NEEDS_FN(create_cq))),
4082 DECLARE_UVERBS_OBJECT(
4083 UVERBS_OBJECT_DEVICE,
4084 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT,
4085 ib_uverbs_get_context,
4086 UAPI_DEF_WRITE_UDATA_IO(
4087 struct ib_uverbs_get_context,
4088 struct ib_uverbs_get_context_resp)),
4089 DECLARE_UVERBS_WRITE(
4090 IB_USER_VERBS_CMD_QUERY_DEVICE,
4091 ib_uverbs_query_device,
4092 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_device,
4093 struct ib_uverbs_query_device_resp)),
4094 DECLARE_UVERBS_WRITE(
4095 IB_USER_VERBS_CMD_QUERY_PORT,
4096 ib_uverbs_query_port,
4097 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_port,
4098 struct ib_uverbs_query_port_resp),
4099 UAPI_DEF_METHOD_NEEDS_FN(query_port)),
4100 DECLARE_UVERBS_WRITE_EX(
4101 IB_USER_VERBS_EX_CMD_QUERY_DEVICE,
4102 ib_uverbs_ex_query_device,
4103 UAPI_DEF_WRITE_IO_EX(
4104 struct ib_uverbs_ex_query_device,
4106 struct ib_uverbs_ex_query_device_resp,
4108 UAPI_DEF_METHOD_NEEDS_FN(query_device)),
4109 UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext),
4110 UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext)),
4112 DECLARE_UVERBS_OBJECT(
4114 DECLARE_UVERBS_WRITE_EX(
4115 IB_USER_VERBS_EX_CMD_CREATE_FLOW,
4116 ib_uverbs_ex_create_flow,
4117 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_create_flow,
4119 struct ib_uverbs_create_flow_resp,
4121 UAPI_DEF_METHOD_NEEDS_FN(create_flow)),
4122 DECLARE_UVERBS_WRITE_EX(
4123 IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
4124 ib_uverbs_ex_destroy_flow,
4125 UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_flow),
4126 UAPI_DEF_METHOD_NEEDS_FN(destroy_flow))),
4128 DECLARE_UVERBS_OBJECT(
4130 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR,
4132 UAPI_DEF_WRITE_I(struct ib_uverbs_dereg_mr),
4133 UAPI_DEF_METHOD_NEEDS_FN(dereg_mr)),
4134 DECLARE_UVERBS_WRITE(
4135 IB_USER_VERBS_CMD_REG_MR,
4137 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_reg_mr,
4138 struct ib_uverbs_reg_mr_resp),
4139 UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr)),
4140 DECLARE_UVERBS_WRITE(
4141 IB_USER_VERBS_CMD_REREG_MR,
4143 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_rereg_mr,
4144 struct ib_uverbs_rereg_mr_resp),
4145 UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr))),
4147 DECLARE_UVERBS_OBJECT(
4149 DECLARE_UVERBS_WRITE(
4150 IB_USER_VERBS_CMD_ALLOC_MW,
4152 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_mw,
4153 struct ib_uverbs_alloc_mw_resp),
4154 UAPI_DEF_METHOD_NEEDS_FN(alloc_mw)),
4155 DECLARE_UVERBS_WRITE(
4156 IB_USER_VERBS_CMD_DEALLOC_MW,
4157 ib_uverbs_dealloc_mw,
4158 UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_mw),
4159 UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw))),
4161 DECLARE_UVERBS_OBJECT(
4163 DECLARE_UVERBS_WRITE(
4164 IB_USER_VERBS_CMD_ALLOC_PD,
4166 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_pd,
4167 struct ib_uverbs_alloc_pd_resp),
4168 UAPI_DEF_METHOD_NEEDS_FN(alloc_pd)),
4169 DECLARE_UVERBS_WRITE(
4170 IB_USER_VERBS_CMD_DEALLOC_PD,
4171 ib_uverbs_dealloc_pd,
4172 UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_pd),
4173 UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd))),
4175 DECLARE_UVERBS_OBJECT(
4177 DECLARE_UVERBS_WRITE(
4178 IB_USER_VERBS_CMD_ATTACH_MCAST,
4179 ib_uverbs_attach_mcast,
4180 UAPI_DEF_WRITE_I(struct ib_uverbs_attach_mcast),
4181 UAPI_DEF_METHOD_NEEDS_FN(attach_mcast),
4182 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
4183 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP,
4184 ib_uverbs_create_qp,
4185 UAPI_DEF_WRITE_UDATA_IO(
4186 struct ib_uverbs_create_qp,
4187 struct ib_uverbs_create_qp_resp),
4188 UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
4189 DECLARE_UVERBS_WRITE(
4190 IB_USER_VERBS_CMD_DESTROY_QP,
4191 ib_uverbs_destroy_qp,
4192 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_qp,
4193 struct ib_uverbs_destroy_qp_resp),
4194 UAPI_DEF_METHOD_NEEDS_FN(destroy_qp)),
4195 DECLARE_UVERBS_WRITE(
4196 IB_USER_VERBS_CMD_DETACH_MCAST,
4197 ib_uverbs_detach_mcast,
4198 UAPI_DEF_WRITE_I(struct ib_uverbs_detach_mcast),
4199 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
4200 DECLARE_UVERBS_WRITE(
4201 IB_USER_VERBS_CMD_MODIFY_QP,
4202 ib_uverbs_modify_qp,
4203 UAPI_DEF_WRITE_I(struct ib_uverbs_modify_qp),
4204 UAPI_DEF_METHOD_NEEDS_FN(modify_qp)),
4205 DECLARE_UVERBS_WRITE(
4206 IB_USER_VERBS_CMD_POST_RECV,
4207 ib_uverbs_post_recv,
4208 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_recv,
4209 struct ib_uverbs_post_recv_resp),
4210 UAPI_DEF_METHOD_NEEDS_FN(post_recv)),
4211 DECLARE_UVERBS_WRITE(
4212 IB_USER_VERBS_CMD_POST_SEND,
4213 ib_uverbs_post_send,
4214 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_send,
4215 struct ib_uverbs_post_send_resp),
4216 UAPI_DEF_METHOD_NEEDS_FN(post_send)),
4217 DECLARE_UVERBS_WRITE(
4218 IB_USER_VERBS_CMD_QUERY_QP,
4220 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_qp,
4221 struct ib_uverbs_query_qp_resp),
4222 UAPI_DEF_METHOD_NEEDS_FN(query_qp)),
4223 DECLARE_UVERBS_WRITE_EX(
4224 IB_USER_VERBS_EX_CMD_CREATE_QP,
4225 ib_uverbs_ex_create_qp,
4226 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_qp,
4228 struct ib_uverbs_ex_create_qp_resp,
4230 UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
4231 DECLARE_UVERBS_WRITE_EX(
4232 IB_USER_VERBS_EX_CMD_MODIFY_QP,
4233 ib_uverbs_ex_modify_qp,
4234 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_modify_qp,
4236 struct ib_uverbs_ex_modify_qp_resp,
4238 UAPI_DEF_METHOD_NEEDS_FN(modify_qp))),
4240 DECLARE_UVERBS_OBJECT(
4241 UVERBS_OBJECT_RWQ_IND_TBL,
4242 DECLARE_UVERBS_WRITE_EX(
4243 IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL,
4244 ib_uverbs_ex_create_rwq_ind_table,
4245 UAPI_DEF_WRITE_IO_EX(
4246 struct ib_uverbs_ex_create_rwq_ind_table,
4248 struct ib_uverbs_ex_create_rwq_ind_table_resp,
4250 UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table)),
4251 DECLARE_UVERBS_WRITE_EX(
4252 IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL,
4253 ib_uverbs_ex_destroy_rwq_ind_table,
4255 struct ib_uverbs_ex_destroy_rwq_ind_table),
4256 UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table))),
4258 DECLARE_UVERBS_OBJECT(
4260 DECLARE_UVERBS_WRITE_EX(
4261 IB_USER_VERBS_EX_CMD_CREATE_WQ,
4262 ib_uverbs_ex_create_wq,
4263 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_wq,
4265 struct ib_uverbs_ex_create_wq_resp,
4267 UAPI_DEF_METHOD_NEEDS_FN(create_wq)),
4268 DECLARE_UVERBS_WRITE_EX(
4269 IB_USER_VERBS_EX_CMD_DESTROY_WQ,
4270 ib_uverbs_ex_destroy_wq,
4271 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_destroy_wq,
4273 struct ib_uverbs_ex_destroy_wq_resp,
4275 UAPI_DEF_METHOD_NEEDS_FN(destroy_wq)),
4276 DECLARE_UVERBS_WRITE_EX(
4277 IB_USER_VERBS_EX_CMD_MODIFY_WQ,
4278 ib_uverbs_ex_modify_wq,
4279 UAPI_DEF_WRITE_I_EX(struct ib_uverbs_ex_modify_wq,
4281 UAPI_DEF_METHOD_NEEDS_FN(modify_wq))),
4283 DECLARE_UVERBS_OBJECT(
4285 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ,
4286 ib_uverbs_create_srq,
4287 UAPI_DEF_WRITE_UDATA_IO(
4288 struct ib_uverbs_create_srq,
4289 struct ib_uverbs_create_srq_resp),
4290 UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4291 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ,
4292 ib_uverbs_create_xsrq,
4293 UAPI_DEF_WRITE_UDATA_IO(
4294 struct ib_uverbs_create_xsrq,
4295 struct ib_uverbs_create_srq_resp),
4296 UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4297 DECLARE_UVERBS_WRITE(
4298 IB_USER_VERBS_CMD_DESTROY_SRQ,
4299 ib_uverbs_destroy_srq,
4300 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_srq,
4301 struct ib_uverbs_destroy_srq_resp),
4302 UAPI_DEF_METHOD_NEEDS_FN(destroy_srq)),
4303 DECLARE_UVERBS_WRITE(
4304 IB_USER_VERBS_CMD_MODIFY_SRQ,
4305 ib_uverbs_modify_srq,
4306 UAPI_DEF_WRITE_UDATA_I(struct ib_uverbs_modify_srq),
4307 UAPI_DEF_METHOD_NEEDS_FN(modify_srq)),
4308 DECLARE_UVERBS_WRITE(
4309 IB_USER_VERBS_CMD_POST_SRQ_RECV,
4310 ib_uverbs_post_srq_recv,
4311 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_srq_recv,
4312 struct ib_uverbs_post_srq_recv_resp),
4313 UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv)),
4314 DECLARE_UVERBS_WRITE(
4315 IB_USER_VERBS_CMD_QUERY_SRQ,
4316 ib_uverbs_query_srq,
4317 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_srq,
4318 struct ib_uverbs_query_srq_resp),
4319 UAPI_DEF_METHOD_NEEDS_FN(query_srq))),
4321 DECLARE_UVERBS_OBJECT(
4323 DECLARE_UVERBS_WRITE(
4324 IB_USER_VERBS_CMD_CLOSE_XRCD,
4325 ib_uverbs_close_xrcd,
4326 UAPI_DEF_WRITE_I(struct ib_uverbs_close_xrcd),
4327 UAPI_DEF_METHOD_NEEDS_FN(dealloc_xrcd)),
4328 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP,
4330 UAPI_DEF_WRITE_UDATA_IO(
4331 struct ib_uverbs_open_qp,
4332 struct ib_uverbs_create_qp_resp)),
4333 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD,
4334 ib_uverbs_open_xrcd,
4335 UAPI_DEF_WRITE_UDATA_IO(
4336 struct ib_uverbs_open_xrcd,
4337 struct ib_uverbs_open_xrcd_resp),
4338 UAPI_DEF_METHOD_NEEDS_FN(alloc_xrcd))),