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 ssize_t 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_udata udata;
76 struct ib_ucontext *ucontext;
78 struct ib_rdmacg_object cg_obj;
79 struct ib_device *ib_dev;
82 if (out_len < sizeof resp)
85 if (copy_from_user(&cmd, buf, sizeof cmd))
88 mutex_lock(&file->ucontext_lock);
89 ib_dev = srcu_dereference(file->device->ib_dev,
90 &file->device->disassociate_srcu);
101 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
102 u64_to_user_ptr(cmd.response) + sizeof(resp),
103 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
104 out_len - sizeof(resp));
106 ret = ib_rdmacg_try_charge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
110 ucontext = ib_dev->alloc_ucontext(ib_dev, &udata);
111 if (IS_ERR(ucontext)) {
112 ret = PTR_ERR(ucontext);
116 ucontext->device = ib_dev;
117 ucontext->cg_obj = cg_obj;
118 /* ufile is required when some objects are released */
119 ucontext->ufile = file;
121 ucontext->closing = false;
122 ucontext->cleanup_retryable = false;
124 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
125 mutex_init(&ucontext->per_mm_list_lock);
126 INIT_LIST_HEAD(&ucontext->per_mm_list);
127 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
128 ucontext->invalidate_range = NULL;
132 resp.num_comp_vectors = file->device->num_comp_vectors;
134 ret = get_unused_fd_flags(O_CLOEXEC);
139 filp = ib_uverbs_alloc_async_event_file(file, ib_dev);
145 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
150 fd_install(resp.async_fd, filp);
153 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
154 * only after all writes to setup the ucontext have completed
156 smp_store_release(&file->ucontext, ucontext);
158 mutex_unlock(&file->ucontext_lock);
163 ib_uverbs_free_async_event_file(file);
167 put_unused_fd(resp.async_fd);
170 ib_dev->dealloc_ucontext(ucontext);
173 ib_rdmacg_uncharge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
176 mutex_unlock(&file->ucontext_lock);
180 static void copy_query_dev_fields(struct ib_ucontext *ucontext,
181 struct ib_uverbs_query_device_resp *resp,
182 struct ib_device_attr *attr)
184 struct ib_device *ib_dev = ucontext->device;
186 resp->fw_ver = attr->fw_ver;
187 resp->node_guid = ib_dev->node_guid;
188 resp->sys_image_guid = attr->sys_image_guid;
189 resp->max_mr_size = attr->max_mr_size;
190 resp->page_size_cap = attr->page_size_cap;
191 resp->vendor_id = attr->vendor_id;
192 resp->vendor_part_id = attr->vendor_part_id;
193 resp->hw_ver = attr->hw_ver;
194 resp->max_qp = attr->max_qp;
195 resp->max_qp_wr = attr->max_qp_wr;
196 resp->device_cap_flags = lower_32_bits(attr->device_cap_flags);
197 resp->max_sge = min(attr->max_send_sge, attr->max_recv_sge);
198 resp->max_sge_rd = attr->max_sge_rd;
199 resp->max_cq = attr->max_cq;
200 resp->max_cqe = attr->max_cqe;
201 resp->max_mr = attr->max_mr;
202 resp->max_pd = attr->max_pd;
203 resp->max_qp_rd_atom = attr->max_qp_rd_atom;
204 resp->max_ee_rd_atom = attr->max_ee_rd_atom;
205 resp->max_res_rd_atom = attr->max_res_rd_atom;
206 resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
207 resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
208 resp->atomic_cap = attr->atomic_cap;
209 resp->max_ee = attr->max_ee;
210 resp->max_rdd = attr->max_rdd;
211 resp->max_mw = attr->max_mw;
212 resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
213 resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
214 resp->max_mcast_grp = attr->max_mcast_grp;
215 resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
216 resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
217 resp->max_ah = attr->max_ah;
218 resp->max_fmr = attr->max_fmr;
219 resp->max_map_per_fmr = attr->max_map_per_fmr;
220 resp->max_srq = attr->max_srq;
221 resp->max_srq_wr = attr->max_srq_wr;
222 resp->max_srq_sge = attr->max_srq_sge;
223 resp->max_pkeys = attr->max_pkeys;
224 resp->local_ca_ack_delay = attr->local_ca_ack_delay;
225 resp->phys_port_cnt = ib_dev->phys_port_cnt;
228 static ssize_t ib_uverbs_query_device(struct uverbs_attr_bundle *attrs,
229 const char __user *buf, int in_len,
232 struct ib_uverbs_query_device cmd;
233 struct ib_uverbs_query_device_resp resp;
234 struct ib_ucontext *ucontext;
236 ucontext = ib_uverbs_get_ucontext(attrs);
237 if (IS_ERR(ucontext))
238 return PTR_ERR(ucontext);
240 if (out_len < sizeof resp)
243 if (copy_from_user(&cmd, buf, sizeof cmd))
246 memset(&resp, 0, sizeof resp);
247 copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
249 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
256 * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the
257 * PortInfo CapabilityMask, but was extended with unique bits.
259 static u32 make_port_cap_flags(const struct ib_port_attr *attr)
263 /* All IBA CapabilityMask bits are passed through here, except bit 26,
264 * which is overridden with IP_BASED_GIDS. This is due to a historical
265 * mistake in the implementation of IP_BASED_GIDS. Otherwise all other
266 * bits match the IBA definition across all kernel versions.
268 res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS;
271 res |= IB_UVERBS_PCF_IP_BASED_GIDS;
276 static ssize_t ib_uverbs_query_port(struct uverbs_attr_bundle *attrs,
277 const char __user *buf, int in_len,
280 struct ib_uverbs_query_port cmd;
281 struct ib_uverbs_query_port_resp resp;
282 struct ib_port_attr attr;
284 struct ib_ucontext *ucontext;
285 struct ib_device *ib_dev;
287 ucontext = ib_uverbs_get_ucontext(attrs);
288 if (IS_ERR(ucontext))
289 return PTR_ERR(ucontext);
290 ib_dev = ucontext->device;
292 if (out_len < sizeof resp)
295 if (copy_from_user(&cmd, buf, sizeof cmd))
298 ret = ib_query_port(ib_dev, cmd.port_num, &attr);
302 memset(&resp, 0, sizeof resp);
304 resp.state = attr.state;
305 resp.max_mtu = attr.max_mtu;
306 resp.active_mtu = attr.active_mtu;
307 resp.gid_tbl_len = attr.gid_tbl_len;
308 resp.port_cap_flags = make_port_cap_flags(&attr);
309 resp.max_msg_sz = attr.max_msg_sz;
310 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
311 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
312 resp.pkey_tbl_len = attr.pkey_tbl_len;
314 if (rdma_is_grh_required(ib_dev, cmd.port_num))
315 resp.flags |= IB_UVERBS_QPF_GRH_REQUIRED;
317 if (rdma_cap_opa_ah(ib_dev, cmd.port_num)) {
318 resp.lid = OPA_TO_IB_UCAST_LID(attr.lid);
319 resp.sm_lid = OPA_TO_IB_UCAST_LID(attr.sm_lid);
321 resp.lid = ib_lid_cpu16(attr.lid);
322 resp.sm_lid = ib_lid_cpu16(attr.sm_lid);
325 resp.max_vl_num = attr.max_vl_num;
326 resp.sm_sl = attr.sm_sl;
327 resp.subnet_timeout = attr.subnet_timeout;
328 resp.init_type_reply = attr.init_type_reply;
329 resp.active_width = attr.active_width;
330 resp.active_speed = attr.active_speed;
331 resp.phys_state = attr.phys_state;
332 resp.link_layer = rdma_port_get_link_layer(ib_dev,
335 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
341 static ssize_t ib_uverbs_alloc_pd(struct uverbs_attr_bundle *attrs,
342 const char __user *buf, int in_len,
345 struct ib_uverbs_alloc_pd cmd;
346 struct ib_uverbs_alloc_pd_resp resp;
347 struct ib_udata udata;
348 struct ib_uobject *uobj;
351 struct ib_device *ib_dev;
353 if (out_len < sizeof resp)
356 if (copy_from_user(&cmd, buf, sizeof cmd))
359 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
360 u64_to_user_ptr(cmd.response) + sizeof(resp),
361 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
362 out_len - sizeof(resp));
364 uobj = uobj_alloc(UVERBS_OBJECT_PD, attrs, &ib_dev);
366 return PTR_ERR(uobj);
368 pd = ib_dev->alloc_pd(ib_dev, uobj->context, &udata);
376 pd->__internal_mr = NULL;
377 atomic_set(&pd->usecnt, 0);
380 memset(&resp, 0, sizeof resp);
381 resp.pd_handle = uobj->id;
382 pd->res.type = RDMA_RESTRACK_PD;
383 rdma_restrack_add(&pd->res);
385 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
390 return uobj_alloc_commit(uobj, in_len);
396 uobj_alloc_abort(uobj);
400 static ssize_t ib_uverbs_dealloc_pd(struct uverbs_attr_bundle *attrs,
401 const char __user *buf, int in_len,
404 struct ib_uverbs_dealloc_pd cmd;
406 if (copy_from_user(&cmd, buf, sizeof cmd))
409 return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, attrs,
413 struct xrcd_table_entry {
415 struct ib_xrcd *xrcd;
419 static int xrcd_table_insert(struct ib_uverbs_device *dev,
421 struct ib_xrcd *xrcd)
423 struct xrcd_table_entry *entry, *scan;
424 struct rb_node **p = &dev->xrcd_tree.rb_node;
425 struct rb_node *parent = NULL;
427 entry = kmalloc(sizeof *entry, GFP_KERNEL);
432 entry->inode = inode;
436 scan = rb_entry(parent, struct xrcd_table_entry, node);
438 if (inode < scan->inode) {
440 } else if (inode > scan->inode) {
448 rb_link_node(&entry->node, parent, p);
449 rb_insert_color(&entry->node, &dev->xrcd_tree);
454 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
457 struct xrcd_table_entry *entry;
458 struct rb_node *p = dev->xrcd_tree.rb_node;
461 entry = rb_entry(p, struct xrcd_table_entry, node);
463 if (inode < entry->inode)
465 else if (inode > entry->inode)
474 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
476 struct xrcd_table_entry *entry;
478 entry = xrcd_table_search(dev, inode);
485 static void xrcd_table_delete(struct ib_uverbs_device *dev,
488 struct xrcd_table_entry *entry;
490 entry = xrcd_table_search(dev, inode);
493 rb_erase(&entry->node, &dev->xrcd_tree);
498 static ssize_t ib_uverbs_open_xrcd(struct uverbs_attr_bundle *attrs,
499 const char __user *buf, int in_len,
502 struct ib_uverbs_device *ibudev = attrs->ufile->device;
503 struct ib_uverbs_open_xrcd cmd;
504 struct ib_uverbs_open_xrcd_resp resp;
505 struct ib_udata udata;
506 struct ib_uxrcd_object *obj;
507 struct ib_xrcd *xrcd = NULL;
508 struct fd f = {NULL, 0};
509 struct inode *inode = NULL;
512 struct ib_device *ib_dev;
514 if (out_len < sizeof resp)
517 if (copy_from_user(&cmd, buf, sizeof cmd))
520 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
521 u64_to_user_ptr(cmd.response) + sizeof(resp),
522 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
523 out_len - sizeof(resp));
525 mutex_lock(&ibudev->xrcd_tree_mutex);
528 /* search for file descriptor */
532 goto err_tree_mutex_unlock;
535 inode = file_inode(f.file);
536 xrcd = find_xrcd(ibudev, inode);
537 if (!xrcd && !(cmd.oflags & O_CREAT)) {
538 /* no file descriptor. Need CREATE flag */
540 goto err_tree_mutex_unlock;
543 if (xrcd && cmd.oflags & O_EXCL) {
545 goto err_tree_mutex_unlock;
549 obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, attrs,
553 goto err_tree_mutex_unlock;
557 xrcd = ib_dev->alloc_xrcd(ib_dev, obj->uobject.context, &udata);
564 xrcd->device = ib_dev;
565 atomic_set(&xrcd->usecnt, 0);
566 mutex_init(&xrcd->tgt_qp_mutex);
567 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
571 atomic_set(&obj->refcnt, 0);
572 obj->uobject.object = xrcd;
573 memset(&resp, 0, sizeof resp);
574 resp.xrcd_handle = obj->uobject.id;
578 /* create new inode/xrcd table entry */
579 ret = xrcd_table_insert(ibudev, inode, xrcd);
581 goto err_dealloc_xrcd;
583 atomic_inc(&xrcd->usecnt);
586 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
594 mutex_unlock(&ibudev->xrcd_tree_mutex);
596 return uobj_alloc_commit(&obj->uobject, in_len);
601 xrcd_table_delete(ibudev, inode);
602 atomic_dec(&xrcd->usecnt);
606 ib_dealloc_xrcd(xrcd);
609 uobj_alloc_abort(&obj->uobject);
611 err_tree_mutex_unlock:
615 mutex_unlock(&ibudev->xrcd_tree_mutex);
620 static ssize_t ib_uverbs_close_xrcd(struct uverbs_attr_bundle *attrs,
621 const char __user *buf, int in_len,
624 struct ib_uverbs_close_xrcd cmd;
626 if (copy_from_user(&cmd, buf, sizeof cmd))
629 return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, attrs,
633 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject,
634 struct ib_xrcd *xrcd,
635 enum rdma_remove_reason why)
639 struct ib_uverbs_device *dev = uobject->context->ufile->device;
642 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
645 ret = ib_dealloc_xrcd(xrcd);
647 if (ib_is_destroy_retryable(ret, why, uobject)) {
648 atomic_inc(&xrcd->usecnt);
653 xrcd_table_delete(dev, inode);
658 static ssize_t ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs,
659 const char __user *buf, int in_len, int out_len)
661 struct ib_uverbs_reg_mr cmd;
662 struct ib_uverbs_reg_mr_resp resp;
663 struct ib_udata udata;
664 struct ib_uobject *uobj;
668 struct ib_device *ib_dev;
670 if (out_len < sizeof resp)
673 if (copy_from_user(&cmd, buf, sizeof cmd))
676 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
677 u64_to_user_ptr(cmd.response) + sizeof(resp),
678 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
679 out_len - sizeof(resp));
681 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
684 ret = ib_check_mr_access(cmd.access_flags);
688 uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
690 return PTR_ERR(uobj);
692 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
698 if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
699 if (!(pd->device->attrs.device_cap_flags &
700 IB_DEVICE_ON_DEMAND_PAGING)) {
701 pr_debug("ODP support not available\n");
707 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
708 cmd.access_flags, &udata);
714 mr->device = pd->device;
718 atomic_inc(&pd->usecnt);
719 mr->res.type = RDMA_RESTRACK_MR;
720 rdma_restrack_add(&mr->res);
724 memset(&resp, 0, sizeof resp);
725 resp.lkey = mr->lkey;
726 resp.rkey = mr->rkey;
727 resp.mr_handle = uobj->id;
729 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
734 uobj_put_obj_read(pd);
736 return uobj_alloc_commit(uobj, in_len);
742 uobj_put_obj_read(pd);
745 uobj_alloc_abort(uobj);
749 static ssize_t ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs,
750 const char __user *buf, int in_len,
753 struct ib_uverbs_rereg_mr cmd;
754 struct ib_uverbs_rereg_mr_resp resp;
755 struct ib_udata udata;
756 struct ib_pd *pd = NULL;
758 struct ib_pd *old_pd;
760 struct ib_uobject *uobj;
762 if (out_len < sizeof(resp))
765 if (copy_from_user(&cmd, buf, sizeof(cmd)))
768 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
769 u64_to_user_ptr(cmd.response) + sizeof(resp),
770 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
771 out_len - sizeof(resp));
773 if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
776 if ((cmd.flags & IB_MR_REREG_TRANS) &&
777 (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
778 (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
781 uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
783 return PTR_ERR(uobj);
792 if (cmd.flags & IB_MR_REREG_ACCESS) {
793 ret = ib_check_mr_access(cmd.access_flags);
798 if (cmd.flags & IB_MR_REREG_PD) {
799 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
808 ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start,
809 cmd.length, cmd.hca_va,
810 cmd.access_flags, pd, &udata);
812 if (cmd.flags & IB_MR_REREG_PD) {
813 atomic_inc(&pd->usecnt);
815 atomic_dec(&old_pd->usecnt);
821 memset(&resp, 0, sizeof(resp));
822 resp.lkey = mr->lkey;
823 resp.rkey = mr->rkey;
825 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
831 if (cmd.flags & IB_MR_REREG_PD)
832 uobj_put_obj_read(pd);
835 uobj_put_write(uobj);
840 static ssize_t ib_uverbs_dereg_mr(struct uverbs_attr_bundle *attrs,
841 const char __user *buf, int in_len,
844 struct ib_uverbs_dereg_mr cmd;
846 if (copy_from_user(&cmd, buf, sizeof cmd))
849 return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, attrs,
853 static ssize_t ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs,
854 const char __user *buf, int in_len,
857 struct ib_uverbs_alloc_mw cmd;
858 struct ib_uverbs_alloc_mw_resp resp;
859 struct ib_uobject *uobj;
862 struct ib_udata udata;
864 struct ib_device *ib_dev;
866 if (out_len < sizeof(resp))
869 if (copy_from_user(&cmd, buf, sizeof(cmd)))
872 uobj = uobj_alloc(UVERBS_OBJECT_MW, attrs, &ib_dev);
874 return PTR_ERR(uobj);
876 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
882 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
883 u64_to_user_ptr(cmd.response) + sizeof(resp),
884 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
885 out_len - sizeof(resp));
887 mw = pd->device->alloc_mw(pd, cmd.mw_type, &udata);
893 mw->device = pd->device;
896 atomic_inc(&pd->usecnt);
900 memset(&resp, 0, sizeof(resp));
901 resp.rkey = mw->rkey;
902 resp.mw_handle = uobj->id;
904 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp))) {
909 uobj_put_obj_read(pd);
910 return uobj_alloc_commit(uobj, in_len);
913 uverbs_dealloc_mw(mw);
915 uobj_put_obj_read(pd);
917 uobj_alloc_abort(uobj);
921 static ssize_t ib_uverbs_dealloc_mw(struct uverbs_attr_bundle *attrs,
922 const char __user *buf, int in_len,
925 struct ib_uverbs_dealloc_mw cmd;
927 if (copy_from_user(&cmd, buf, sizeof(cmd)))
930 return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, attrs,
934 static ssize_t ib_uverbs_create_comp_channel(struct uverbs_attr_bundle *attrs,
935 const char __user *buf, int in_len,
938 struct ib_uverbs_create_comp_channel cmd;
939 struct ib_uverbs_create_comp_channel_resp resp;
940 struct ib_uobject *uobj;
941 struct ib_uverbs_completion_event_file *ev_file;
942 struct ib_device *ib_dev;
944 if (out_len < sizeof resp)
947 if (copy_from_user(&cmd, buf, sizeof cmd))
950 uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, attrs, &ib_dev);
952 return PTR_ERR(uobj);
956 ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
958 ib_uverbs_init_event_queue(&ev_file->ev_queue);
960 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
961 uobj_alloc_abort(uobj);
965 return uobj_alloc_commit(uobj, in_len);
968 static struct ib_ucq_object *create_cq(struct uverbs_attr_bundle *attrs,
969 struct ib_udata *ucore,
970 struct ib_udata *uhw,
971 struct ib_uverbs_ex_create_cq *cmd,
973 int (*cb)(struct uverbs_attr_bundle *attrs,
974 struct ib_ucq_object *obj,
975 struct ib_uverbs_ex_create_cq_resp *resp,
976 struct ib_udata *udata,
980 struct ib_ucq_object *obj;
981 struct ib_uverbs_completion_event_file *ev_file = NULL;
984 struct ib_uverbs_ex_create_cq_resp resp;
985 struct ib_cq_init_attr attr = {};
986 struct ib_device *ib_dev;
988 if (cmd->comp_vector >= attrs->ufile->device->num_comp_vectors)
989 return ERR_PTR(-EINVAL);
991 obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, attrs,
996 if (cmd->comp_channel >= 0) {
997 ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, attrs);
998 if (IS_ERR(ev_file)) {
999 ret = PTR_ERR(ev_file);
1004 obj->uobject.user_handle = cmd->user_handle;
1005 obj->comp_events_reported = 0;
1006 obj->async_events_reported = 0;
1007 INIT_LIST_HEAD(&obj->comp_list);
1008 INIT_LIST_HEAD(&obj->async_list);
1010 attr.cqe = cmd->cqe;
1011 attr.comp_vector = cmd->comp_vector;
1013 if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
1014 attr.flags = cmd->flags;
1016 cq = ib_dev->create_cq(ib_dev, &attr, obj->uobject.context, uhw);
1022 cq->device = ib_dev;
1023 cq->uobject = &obj->uobject;
1024 cq->comp_handler = ib_uverbs_comp_handler;
1025 cq->event_handler = ib_uverbs_cq_event_handler;
1026 cq->cq_context = ev_file ? &ev_file->ev_queue : NULL;
1027 atomic_set(&cq->usecnt, 0);
1029 obj->uobject.object = cq;
1030 memset(&resp, 0, sizeof resp);
1031 resp.base.cq_handle = obj->uobject.id;
1032 resp.base.cqe = cq->cqe;
1034 resp.response_length = offsetof(typeof(resp), response_length) +
1035 sizeof(resp.response_length);
1037 cq->res.type = RDMA_RESTRACK_CQ;
1038 rdma_restrack_add(&cq->res);
1040 ret = cb(attrs, obj, &resp, ucore, context);
1044 ret = uobj_alloc_commit(&obj->uobject, 0);
1046 return ERR_PTR(ret);
1054 ib_uverbs_release_ucq(attrs->ufile, ev_file, obj);
1057 uobj_alloc_abort(&obj->uobject);
1059 return ERR_PTR(ret);
1062 static int ib_uverbs_create_cq_cb(struct uverbs_attr_bundle *attrs,
1063 struct ib_ucq_object *obj,
1064 struct ib_uverbs_ex_create_cq_resp *resp,
1065 struct ib_udata *ucore, void *context)
1067 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1073 static ssize_t ib_uverbs_create_cq(struct uverbs_attr_bundle *attrs,
1074 const char __user *buf, int in_len,
1077 struct ib_uverbs_create_cq cmd;
1078 struct ib_uverbs_ex_create_cq cmd_ex;
1079 struct ib_uverbs_create_cq_resp resp;
1080 struct ib_udata ucore;
1081 struct ib_udata uhw;
1082 struct ib_ucq_object *obj;
1084 if (out_len < sizeof(resp))
1087 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1090 ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
1091 sizeof(cmd), sizeof(resp));
1093 ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
1094 u64_to_user_ptr(cmd.response) + sizeof(resp),
1095 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1096 out_len - sizeof(resp));
1098 memset(&cmd_ex, 0, sizeof(cmd_ex));
1099 cmd_ex.user_handle = cmd.user_handle;
1100 cmd_ex.cqe = cmd.cqe;
1101 cmd_ex.comp_vector = cmd.comp_vector;
1102 cmd_ex.comp_channel = cmd.comp_channel;
1104 obj = create_cq(attrs, &ucore, &uhw, &cmd_ex,
1105 offsetof(typeof(cmd_ex), comp_channel) +
1106 sizeof(cmd.comp_channel), ib_uverbs_create_cq_cb,
1110 return PTR_ERR(obj);
1115 static int ib_uverbs_ex_create_cq_cb(struct uverbs_attr_bundle *attrs,
1116 struct ib_ucq_object *obj,
1117 struct ib_uverbs_ex_create_cq_resp *resp,
1118 struct ib_udata *ucore, void *context)
1120 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1126 static int ib_uverbs_ex_create_cq(struct uverbs_attr_bundle *attrs,
1127 struct ib_udata *ucore, struct ib_udata *uhw)
1129 struct ib_uverbs_ex_create_cq_resp resp;
1130 struct ib_uverbs_ex_create_cq cmd;
1131 struct ib_ucq_object *obj;
1134 if (ucore->inlen < sizeof(cmd))
1137 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
1147 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1148 sizeof(resp.response_length)))
1151 obj = create_cq(attrs, ucore, uhw, &cmd,
1152 min(ucore->inlen, sizeof(cmd)),
1153 ib_uverbs_ex_create_cq_cb, NULL);
1155 return PTR_ERR_OR_ZERO(obj);
1158 static ssize_t ib_uverbs_resize_cq(struct uverbs_attr_bundle *attrs,
1159 const char __user *buf, int in_len,
1162 struct ib_uverbs_resize_cq cmd;
1163 struct ib_uverbs_resize_cq_resp resp = {};
1164 struct ib_udata udata;
1168 if (copy_from_user(&cmd, buf, sizeof cmd))
1171 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
1172 u64_to_user_ptr(cmd.response) + sizeof(resp),
1173 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1174 out_len - sizeof(resp));
1176 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1180 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1186 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp.cqe))
1190 uobj_put_obj_read(cq);
1192 return ret ? ret : in_len;
1195 static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
1198 struct ib_uverbs_wc tmp;
1200 tmp.wr_id = wc->wr_id;
1201 tmp.status = wc->status;
1202 tmp.opcode = wc->opcode;
1203 tmp.vendor_err = wc->vendor_err;
1204 tmp.byte_len = wc->byte_len;
1205 tmp.ex.imm_data = wc->ex.imm_data;
1206 tmp.qp_num = wc->qp->qp_num;
1207 tmp.src_qp = wc->src_qp;
1208 tmp.wc_flags = wc->wc_flags;
1209 tmp.pkey_index = wc->pkey_index;
1210 if (rdma_cap_opa_ah(ib_dev, wc->port_num))
1211 tmp.slid = OPA_TO_IB_UCAST_LID(wc->slid);
1213 tmp.slid = ib_lid_cpu16(wc->slid);
1215 tmp.dlid_path_bits = wc->dlid_path_bits;
1216 tmp.port_num = wc->port_num;
1219 if (copy_to_user(dest, &tmp, sizeof tmp))
1225 static ssize_t ib_uverbs_poll_cq(struct uverbs_attr_bundle *attrs,
1226 const char __user *buf, int in_len,
1229 struct ib_uverbs_poll_cq cmd;
1230 struct ib_uverbs_poll_cq_resp resp;
1231 u8 __user *header_ptr;
1232 u8 __user *data_ptr;
1237 if (copy_from_user(&cmd, buf, sizeof cmd))
1240 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1244 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1245 header_ptr = u64_to_user_ptr(cmd.response);
1246 data_ptr = header_ptr + sizeof resp;
1248 memset(&resp, 0, sizeof resp);
1249 while (resp.count < cmd.ne) {
1250 ret = ib_poll_cq(cq, 1, &wc);
1256 ret = copy_wc_to_user(cq->device, data_ptr, &wc);
1260 data_ptr += sizeof(struct ib_uverbs_wc);
1264 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1272 uobj_put_obj_read(cq);
1276 static ssize_t ib_uverbs_req_notify_cq(struct uverbs_attr_bundle *attrs,
1277 const char __user *buf, int in_len,
1280 struct ib_uverbs_req_notify_cq cmd;
1283 if (copy_from_user(&cmd, buf, sizeof cmd))
1286 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1290 ib_req_notify_cq(cq, cmd.solicited_only ?
1291 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1293 uobj_put_obj_read(cq);
1298 static ssize_t ib_uverbs_destroy_cq(struct uverbs_attr_bundle *attrs,
1299 const char __user *buf, int in_len,
1302 struct ib_uverbs_destroy_cq cmd;
1303 struct ib_uverbs_destroy_cq_resp resp;
1304 struct ib_uobject *uobj;
1305 struct ib_ucq_object *obj;
1307 if (copy_from_user(&cmd, buf, sizeof cmd))
1310 uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1312 return PTR_ERR(uobj);
1314 obj = container_of(uobj, struct ib_ucq_object, uobject);
1315 memset(&resp, 0, sizeof(resp));
1316 resp.comp_events_reported = obj->comp_events_reported;
1317 resp.async_events_reported = obj->async_events_reported;
1319 uobj_put_destroy(uobj);
1321 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
1327 static int create_qp(struct uverbs_attr_bundle *attrs,
1328 struct ib_udata *ucore,
1329 struct ib_udata *uhw,
1330 struct ib_uverbs_ex_create_qp *cmd,
1332 int (*cb)(struct uverbs_attr_bundle *attrs,
1333 struct ib_uverbs_ex_create_qp_resp *resp,
1334 struct ib_udata *udata),
1337 struct ib_uqp_object *obj;
1338 struct ib_device *device;
1339 struct ib_pd *pd = NULL;
1340 struct ib_xrcd *xrcd = NULL;
1341 struct ib_uobject *xrcd_uobj = ERR_PTR(-ENOENT);
1342 struct ib_cq *scq = NULL, *rcq = NULL;
1343 struct ib_srq *srq = NULL;
1346 struct ib_qp_init_attr attr = {};
1347 struct ib_uverbs_ex_create_qp_resp resp;
1349 struct ib_rwq_ind_table *ind_tbl = NULL;
1351 struct ib_device *ib_dev;
1353 if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1356 obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1359 return PTR_ERR(obj);
1361 obj->uevent.uobject.user_handle = cmd->user_handle;
1362 mutex_init(&obj->mcast_lock);
1364 if (cmd_sz >= offsetof(typeof(*cmd), rwq_ind_tbl_handle) +
1365 sizeof(cmd->rwq_ind_tbl_handle) &&
1366 (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE)) {
1367 ind_tbl = uobj_get_obj_read(rwq_ind_table,
1368 UVERBS_OBJECT_RWQ_IND_TBL,
1369 cmd->rwq_ind_tbl_handle, attrs);
1375 attr.rwq_ind_tbl = ind_tbl;
1378 if (cmd_sz > sizeof(*cmd) &&
1379 !ib_is_udata_cleared(ucore, sizeof(*cmd),
1380 cmd_sz - sizeof(*cmd))) {
1385 if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
1390 if (ind_tbl && !cmd->max_send_wr)
1393 if (cmd->qp_type == IB_QPT_XRC_TGT) {
1394 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
1397 if (IS_ERR(xrcd_uobj)) {
1402 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1407 device = xrcd->device;
1409 if (cmd->qp_type == IB_QPT_XRC_INI) {
1410 cmd->max_recv_wr = 0;
1411 cmd->max_recv_sge = 0;
1414 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
1415 cmd->srq_handle, attrs);
1416 if (!srq || srq->srq_type == IB_SRQT_XRC) {
1423 if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1424 rcq = uobj_get_obj_read(
1425 cq, UVERBS_OBJECT_CQ,
1426 cmd->recv_cq_handle, attrs);
1436 scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
1437 cmd->send_cq_handle, attrs);
1440 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
1442 if (!pd || (!scq && has_sq)) {
1447 device = pd->device;
1450 attr.event_handler = ib_uverbs_qp_event_handler;
1451 attr.qp_context = attrs->ufile;
1456 attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1458 attr.qp_type = cmd->qp_type;
1459 attr.create_flags = 0;
1461 attr.cap.max_send_wr = cmd->max_send_wr;
1462 attr.cap.max_recv_wr = cmd->max_recv_wr;
1463 attr.cap.max_send_sge = cmd->max_send_sge;
1464 attr.cap.max_recv_sge = cmd->max_recv_sge;
1465 attr.cap.max_inline_data = cmd->max_inline_data;
1467 obj->uevent.events_reported = 0;
1468 INIT_LIST_HEAD(&obj->uevent.event_list);
1469 INIT_LIST_HEAD(&obj->mcast_list);
1471 if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
1472 sizeof(cmd->create_flags))
1473 attr.create_flags = cmd->create_flags;
1475 if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1476 IB_QP_CREATE_CROSS_CHANNEL |
1477 IB_QP_CREATE_MANAGED_SEND |
1478 IB_QP_CREATE_MANAGED_RECV |
1479 IB_QP_CREATE_SCATTER_FCS |
1480 IB_QP_CREATE_CVLAN_STRIPPING |
1481 IB_QP_CREATE_SOURCE_QPN |
1482 IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
1487 if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
1488 if (!capable(CAP_NET_RAW)) {
1493 attr.source_qpn = cmd->source_qpn;
1496 buf = (void *)cmd + sizeof(*cmd);
1497 if (cmd_sz > sizeof(*cmd))
1498 if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
1499 cmd_sz - sizeof(*cmd) - 1))) {
1504 if (cmd->qp_type == IB_QPT_XRC_TGT)
1505 qp = ib_create_qp(pd, &attr);
1507 qp = _ib_create_qp(device, pd, &attr, uhw,
1508 &obj->uevent.uobject);
1515 if (cmd->qp_type != IB_QPT_XRC_TGT) {
1516 ret = ib_create_qp_security(qp, device);
1522 qp->send_cq = attr.send_cq;
1523 qp->recv_cq = attr.recv_cq;
1525 qp->rwq_ind_tbl = ind_tbl;
1526 qp->event_handler = attr.event_handler;
1527 qp->qp_context = attr.qp_context;
1528 qp->qp_type = attr.qp_type;
1529 atomic_set(&qp->usecnt, 0);
1530 atomic_inc(&pd->usecnt);
1533 atomic_inc(&attr.send_cq->usecnt);
1535 atomic_inc(&attr.recv_cq->usecnt);
1537 atomic_inc(&attr.srq->usecnt);
1539 atomic_inc(&ind_tbl->usecnt);
1541 /* It is done in _ib_create_qp for other QP types */
1542 qp->uobject = &obj->uevent.uobject;
1545 obj->uevent.uobject.object = qp;
1547 memset(&resp, 0, sizeof resp);
1548 resp.base.qpn = qp->qp_num;
1549 resp.base.qp_handle = obj->uevent.uobject.id;
1550 resp.base.max_recv_sge = attr.cap.max_recv_sge;
1551 resp.base.max_send_sge = attr.cap.max_send_sge;
1552 resp.base.max_recv_wr = attr.cap.max_recv_wr;
1553 resp.base.max_send_wr = attr.cap.max_send_wr;
1554 resp.base.max_inline_data = attr.cap.max_inline_data;
1556 resp.response_length = offsetof(typeof(resp), response_length) +
1557 sizeof(resp.response_length);
1559 ret = cb(attrs, &resp, ucore);
1564 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1566 atomic_inc(&obj->uxrcd->refcnt);
1567 uobj_put_read(xrcd_uobj);
1571 uobj_put_obj_read(pd);
1573 uobj_put_obj_read(scq);
1574 if (rcq && rcq != scq)
1575 uobj_put_obj_read(rcq);
1577 uobj_put_obj_read(srq);
1579 uobj_put_obj_read(ind_tbl);
1581 return uobj_alloc_commit(&obj->uevent.uobject, 0);
1586 if (!IS_ERR(xrcd_uobj))
1587 uobj_put_read(xrcd_uobj);
1589 uobj_put_obj_read(pd);
1591 uobj_put_obj_read(scq);
1592 if (rcq && rcq != scq)
1593 uobj_put_obj_read(rcq);
1595 uobj_put_obj_read(srq);
1597 uobj_put_obj_read(ind_tbl);
1599 uobj_alloc_abort(&obj->uevent.uobject);
1603 static int ib_uverbs_create_qp_cb(struct uverbs_attr_bundle *attrs,
1604 struct ib_uverbs_ex_create_qp_resp *resp,
1605 struct ib_udata *ucore)
1607 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1613 static ssize_t ib_uverbs_create_qp(struct uverbs_attr_bundle *attrs,
1614 const char __user *buf, int in_len,
1617 struct ib_uverbs_create_qp cmd;
1618 struct ib_uverbs_ex_create_qp cmd_ex;
1619 struct ib_udata ucore;
1620 struct ib_udata uhw;
1621 ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
1624 if (out_len < resp_size)
1627 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1630 ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
1631 sizeof(cmd), resp_size);
1632 ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
1633 u64_to_user_ptr(cmd.response) + resp_size,
1634 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1635 out_len - resp_size);
1637 memset(&cmd_ex, 0, sizeof(cmd_ex));
1638 cmd_ex.user_handle = cmd.user_handle;
1639 cmd_ex.pd_handle = cmd.pd_handle;
1640 cmd_ex.send_cq_handle = cmd.send_cq_handle;
1641 cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1642 cmd_ex.srq_handle = cmd.srq_handle;
1643 cmd_ex.max_send_wr = cmd.max_send_wr;
1644 cmd_ex.max_recv_wr = cmd.max_recv_wr;
1645 cmd_ex.max_send_sge = cmd.max_send_sge;
1646 cmd_ex.max_recv_sge = cmd.max_recv_sge;
1647 cmd_ex.max_inline_data = cmd.max_inline_data;
1648 cmd_ex.sq_sig_all = cmd.sq_sig_all;
1649 cmd_ex.qp_type = cmd.qp_type;
1650 cmd_ex.is_srq = cmd.is_srq;
1652 err = create_qp(attrs, &ucore, &uhw, &cmd_ex,
1653 offsetof(typeof(cmd_ex), is_srq) +
1654 sizeof(cmd.is_srq), ib_uverbs_create_qp_cb,
1663 static int ib_uverbs_ex_create_qp_cb(struct uverbs_attr_bundle *attrs,
1664 struct ib_uverbs_ex_create_qp_resp *resp,
1665 struct ib_udata *ucore)
1667 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1673 static int ib_uverbs_ex_create_qp(struct uverbs_attr_bundle *attrs,
1674 struct ib_udata *ucore, struct ib_udata *uhw)
1676 struct ib_uverbs_ex_create_qp_resp resp;
1677 struct ib_uverbs_ex_create_qp cmd = {0};
1680 if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
1681 sizeof(cmd.comp_mask)))
1684 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
1688 if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
1694 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1695 sizeof(resp.response_length)))
1698 err = create_qp(attrs, ucore, uhw, &cmd,
1699 min(ucore->inlen, sizeof(cmd)),
1700 ib_uverbs_ex_create_qp_cb, NULL);
1708 static ssize_t ib_uverbs_open_qp(struct uverbs_attr_bundle *attrs,
1709 const char __user *buf, int in_len,
1712 struct ib_uverbs_open_qp cmd;
1713 struct ib_uverbs_create_qp_resp resp;
1714 struct ib_udata udata;
1715 struct ib_uqp_object *obj;
1716 struct ib_xrcd *xrcd;
1717 struct ib_uobject *uninitialized_var(xrcd_uobj);
1719 struct ib_qp_open_attr attr;
1721 struct ib_device *ib_dev;
1723 if (out_len < sizeof resp)
1726 if (copy_from_user(&cmd, buf, sizeof cmd))
1729 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
1730 u64_to_user_ptr(cmd.response) + sizeof(resp),
1731 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1732 out_len - sizeof(resp));
1734 obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1737 return PTR_ERR(obj);
1739 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, attrs);
1740 if (IS_ERR(xrcd_uobj)) {
1745 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1751 attr.event_handler = ib_uverbs_qp_event_handler;
1752 attr.qp_context = attrs->ufile;
1753 attr.qp_num = cmd.qpn;
1754 attr.qp_type = cmd.qp_type;
1756 obj->uevent.events_reported = 0;
1757 INIT_LIST_HEAD(&obj->uevent.event_list);
1758 INIT_LIST_HEAD(&obj->mcast_list);
1760 qp = ib_open_qp(xrcd, &attr);
1766 obj->uevent.uobject.object = qp;
1767 obj->uevent.uobject.user_handle = cmd.user_handle;
1769 memset(&resp, 0, sizeof resp);
1770 resp.qpn = qp->qp_num;
1771 resp.qp_handle = obj->uevent.uobject.id;
1773 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
1778 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1779 atomic_inc(&obj->uxrcd->refcnt);
1780 qp->uobject = &obj->uevent.uobject;
1781 uobj_put_read(xrcd_uobj);
1783 return uobj_alloc_commit(&obj->uevent.uobject, in_len);
1788 uobj_put_read(xrcd_uobj);
1790 uobj_alloc_abort(&obj->uevent.uobject);
1794 static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
1795 struct rdma_ah_attr *rdma_attr)
1797 const struct ib_global_route *grh;
1799 uverb_attr->dlid = rdma_ah_get_dlid(rdma_attr);
1800 uverb_attr->sl = rdma_ah_get_sl(rdma_attr);
1801 uverb_attr->src_path_bits = rdma_ah_get_path_bits(rdma_attr);
1802 uverb_attr->static_rate = rdma_ah_get_static_rate(rdma_attr);
1803 uverb_attr->is_global = !!(rdma_ah_get_ah_flags(rdma_attr) &
1805 if (uverb_attr->is_global) {
1806 grh = rdma_ah_read_grh(rdma_attr);
1807 memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
1808 uverb_attr->flow_label = grh->flow_label;
1809 uverb_attr->sgid_index = grh->sgid_index;
1810 uverb_attr->hop_limit = grh->hop_limit;
1811 uverb_attr->traffic_class = grh->traffic_class;
1813 uverb_attr->port_num = rdma_ah_get_port_num(rdma_attr);
1816 static ssize_t ib_uverbs_query_qp(struct uverbs_attr_bundle *attrs,
1817 const char __user *buf, int in_len,
1820 struct ib_uverbs_query_qp cmd;
1821 struct ib_uverbs_query_qp_resp resp;
1823 struct ib_qp_attr *attr;
1824 struct ib_qp_init_attr *init_attr;
1827 if (copy_from_user(&cmd, buf, sizeof cmd))
1830 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1831 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1832 if (!attr || !init_attr) {
1837 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1843 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1845 uobj_put_obj_read(qp);
1850 memset(&resp, 0, sizeof resp);
1852 resp.qp_state = attr->qp_state;
1853 resp.cur_qp_state = attr->cur_qp_state;
1854 resp.path_mtu = attr->path_mtu;
1855 resp.path_mig_state = attr->path_mig_state;
1856 resp.qkey = attr->qkey;
1857 resp.rq_psn = attr->rq_psn;
1858 resp.sq_psn = attr->sq_psn;
1859 resp.dest_qp_num = attr->dest_qp_num;
1860 resp.qp_access_flags = attr->qp_access_flags;
1861 resp.pkey_index = attr->pkey_index;
1862 resp.alt_pkey_index = attr->alt_pkey_index;
1863 resp.sq_draining = attr->sq_draining;
1864 resp.max_rd_atomic = attr->max_rd_atomic;
1865 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1866 resp.min_rnr_timer = attr->min_rnr_timer;
1867 resp.port_num = attr->port_num;
1868 resp.timeout = attr->timeout;
1869 resp.retry_cnt = attr->retry_cnt;
1870 resp.rnr_retry = attr->rnr_retry;
1871 resp.alt_port_num = attr->alt_port_num;
1872 resp.alt_timeout = attr->alt_timeout;
1874 copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
1875 copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
1877 resp.max_send_wr = init_attr->cap.max_send_wr;
1878 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1879 resp.max_send_sge = init_attr->cap.max_send_sge;
1880 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1881 resp.max_inline_data = init_attr->cap.max_inline_data;
1882 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1884 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
1891 return ret ? ret : in_len;
1894 /* Remove ignored fields set in the attribute mask */
1895 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1898 case IB_QPT_XRC_INI:
1899 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1900 case IB_QPT_XRC_TGT:
1901 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1908 static void copy_ah_attr_from_uverbs(struct ib_device *dev,
1909 struct rdma_ah_attr *rdma_attr,
1910 struct ib_uverbs_qp_dest *uverb_attr)
1912 rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
1913 if (uverb_attr->is_global) {
1914 rdma_ah_set_grh(rdma_attr, NULL,
1915 uverb_attr->flow_label,
1916 uverb_attr->sgid_index,
1917 uverb_attr->hop_limit,
1918 uverb_attr->traffic_class);
1919 rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
1921 rdma_ah_set_ah_flags(rdma_attr, 0);
1923 rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
1924 rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
1925 rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
1926 rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
1927 rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
1928 rdma_ah_set_make_grd(rdma_attr, false);
1931 static int modify_qp(struct uverbs_attr_bundle *attrs,
1932 struct ib_uverbs_ex_modify_qp *cmd, struct ib_udata *udata)
1934 struct ib_qp_attr *attr;
1938 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1942 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle,
1949 if ((cmd->base.attr_mask & IB_QP_PORT) &&
1950 !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
1955 if ((cmd->base.attr_mask & IB_QP_AV)) {
1956 if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
1961 if (cmd->base.attr_mask & IB_QP_STATE &&
1962 cmd->base.qp_state == IB_QPS_RTR) {
1963 /* We are in INIT->RTR TRANSITION (if we are not,
1964 * this transition will be rejected in subsequent checks).
1965 * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1966 * but the IB_QP_STATE flag is required.
1968 * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1969 * when IB_QP_AV is set, has required inclusion of a valid
1970 * port number in the primary AV. (AVs are created and handled
1971 * differently for infiniband and ethernet (RoCE) ports).
1973 * Check the port number included in the primary AV against
1974 * the port number in the qp struct, which was set (and saved)
1975 * in the RST->INIT transition.
1977 if (cmd->base.dest.port_num != qp->real_qp->port) {
1982 /* We are in SQD->SQD. (If we are not, this transition will
1983 * be rejected later in the verbs layer checks).
1984 * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1985 * together in the SQD->SQD transition.
1987 * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1988 * verbs layer driver does not track primary port changes
1989 * resulting from path migration. Thus, in SQD, if the primary
1990 * AV is modified, the primary port should also be modified).
1992 * Note that in this transition, the IB_QP_STATE flag
1995 if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1996 == (IB_QP_AV | IB_QP_PORT)) &&
1997 cmd->base.port_num != cmd->base.dest.port_num) {
2001 if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
2003 cmd->base.attr_mask |= IB_QP_PORT;
2004 cmd->base.port_num = cmd->base.dest.port_num;
2009 if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
2010 (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
2011 !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
2012 cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
2017 if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
2018 cmd->base.cur_qp_state > IB_QPS_ERR) ||
2019 (cmd->base.attr_mask & IB_QP_STATE &&
2020 cmd->base.qp_state > IB_QPS_ERR)) {
2025 if (cmd->base.attr_mask & IB_QP_STATE)
2026 attr->qp_state = cmd->base.qp_state;
2027 if (cmd->base.attr_mask & IB_QP_CUR_STATE)
2028 attr->cur_qp_state = cmd->base.cur_qp_state;
2029 if (cmd->base.attr_mask & IB_QP_PATH_MTU)
2030 attr->path_mtu = cmd->base.path_mtu;
2031 if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
2032 attr->path_mig_state = cmd->base.path_mig_state;
2033 if (cmd->base.attr_mask & IB_QP_QKEY)
2034 attr->qkey = cmd->base.qkey;
2035 if (cmd->base.attr_mask & IB_QP_RQ_PSN)
2036 attr->rq_psn = cmd->base.rq_psn;
2037 if (cmd->base.attr_mask & IB_QP_SQ_PSN)
2038 attr->sq_psn = cmd->base.sq_psn;
2039 if (cmd->base.attr_mask & IB_QP_DEST_QPN)
2040 attr->dest_qp_num = cmd->base.dest_qp_num;
2041 if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
2042 attr->qp_access_flags = cmd->base.qp_access_flags;
2043 if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
2044 attr->pkey_index = cmd->base.pkey_index;
2045 if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
2046 attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
2047 if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
2048 attr->max_rd_atomic = cmd->base.max_rd_atomic;
2049 if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2050 attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
2051 if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
2052 attr->min_rnr_timer = cmd->base.min_rnr_timer;
2053 if (cmd->base.attr_mask & IB_QP_PORT)
2054 attr->port_num = cmd->base.port_num;
2055 if (cmd->base.attr_mask & IB_QP_TIMEOUT)
2056 attr->timeout = cmd->base.timeout;
2057 if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
2058 attr->retry_cnt = cmd->base.retry_cnt;
2059 if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
2060 attr->rnr_retry = cmd->base.rnr_retry;
2061 if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
2062 attr->alt_port_num = cmd->base.alt_port_num;
2063 attr->alt_timeout = cmd->base.alt_timeout;
2064 attr->alt_pkey_index = cmd->base.alt_pkey_index;
2066 if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
2067 attr->rate_limit = cmd->rate_limit;
2069 if (cmd->base.attr_mask & IB_QP_AV)
2070 copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
2073 if (cmd->base.attr_mask & IB_QP_ALT_PATH)
2074 copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
2075 &cmd->base.alt_dest);
2077 ret = ib_modify_qp_with_udata(qp, attr,
2078 modify_qp_mask(qp->qp_type,
2079 cmd->base.attr_mask),
2083 uobj_put_obj_read(qp);
2090 static ssize_t ib_uverbs_modify_qp(struct uverbs_attr_bundle *attrs,
2091 const char __user *buf, int in_len,
2094 struct ib_uverbs_ex_modify_qp cmd = {};
2095 struct ib_udata udata;
2098 if (copy_from_user(&cmd.base, buf, sizeof(cmd.base)))
2101 if (cmd.base.attr_mask &
2102 ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
2105 ib_uverbs_init_udata(&udata, buf + sizeof(cmd.base), NULL,
2106 in_len - sizeof(cmd.base) - sizeof(struct ib_uverbs_cmd_hdr),
2109 ret = modify_qp(attrs, &cmd, &udata);
2116 static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle *attrs,
2117 struct ib_udata *ucore, struct ib_udata *uhw)
2119 struct ib_uverbs_ex_modify_qp cmd = {};
2123 * Last bit is reserved for extending the attr_mask by
2124 * using another field.
2126 BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
2128 if (ucore->inlen < sizeof(cmd.base))
2131 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
2135 if (cmd.base.attr_mask &
2136 ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
2139 if (ucore->inlen > sizeof(cmd)) {
2140 if (!ib_is_udata_cleared(ucore, sizeof(cmd),
2141 ucore->inlen - sizeof(cmd)))
2145 ret = modify_qp(attrs, &cmd, uhw);
2150 static ssize_t ib_uverbs_destroy_qp(struct uverbs_attr_bundle *attrs,
2151 const char __user *buf, int in_len,
2154 struct ib_uverbs_destroy_qp cmd;
2155 struct ib_uverbs_destroy_qp_resp resp;
2156 struct ib_uobject *uobj;
2157 struct ib_uqp_object *obj;
2159 if (copy_from_user(&cmd, buf, sizeof cmd))
2162 uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2164 return PTR_ERR(uobj);
2166 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2167 memset(&resp, 0, sizeof(resp));
2168 resp.events_reported = obj->uevent.events_reported;
2170 uobj_put_destroy(uobj);
2172 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2178 static void *alloc_wr(size_t wr_size, __u32 num_sge)
2180 if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
2181 sizeof (struct ib_sge))
2184 return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
2185 num_sge * sizeof (struct ib_sge), GFP_KERNEL);
2188 static ssize_t ib_uverbs_post_send(struct uverbs_attr_bundle *attrs,
2189 const char __user *buf, int in_len,
2192 struct ib_uverbs_post_send cmd;
2193 struct ib_uverbs_post_send_resp resp;
2194 struct ib_uverbs_send_wr *user_wr;
2195 struct ib_send_wr *wr = NULL, *last, *next;
2196 const struct ib_send_wr *bad_wr;
2200 ssize_t ret = -EINVAL;
2203 if (copy_from_user(&cmd, buf, sizeof cmd))
2206 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2207 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2210 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2213 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2217 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2221 is_ud = qp->qp_type == IB_QPT_UD;
2224 for (i = 0; i < cmd.wr_count; ++i) {
2225 if (copy_from_user(user_wr,
2226 buf + sizeof cmd + i * cmd.wqe_size,
2232 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2238 struct ib_ud_wr *ud;
2240 if (user_wr->opcode != IB_WR_SEND &&
2241 user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2246 next_size = sizeof(*ud);
2247 ud = alloc_wr(next_size, user_wr->num_sge);
2253 ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2254 user_wr->wr.ud.ah, attrs);
2260 ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2261 ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2264 } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2265 user_wr->opcode == IB_WR_RDMA_WRITE ||
2266 user_wr->opcode == IB_WR_RDMA_READ) {
2267 struct ib_rdma_wr *rdma;
2269 next_size = sizeof(*rdma);
2270 rdma = alloc_wr(next_size, user_wr->num_sge);
2276 rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2277 rdma->rkey = user_wr->wr.rdma.rkey;
2280 } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2281 user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2282 struct ib_atomic_wr *atomic;
2284 next_size = sizeof(*atomic);
2285 atomic = alloc_wr(next_size, user_wr->num_sge);
2291 atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2292 atomic->compare_add = user_wr->wr.atomic.compare_add;
2293 atomic->swap = user_wr->wr.atomic.swap;
2294 atomic->rkey = user_wr->wr.atomic.rkey;
2297 } else if (user_wr->opcode == IB_WR_SEND ||
2298 user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2299 user_wr->opcode == IB_WR_SEND_WITH_INV) {
2300 next_size = sizeof(*next);
2301 next = alloc_wr(next_size, user_wr->num_sge);
2311 if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2312 user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2314 (__be32 __force) user_wr->ex.imm_data;
2315 } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2316 next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2326 next->wr_id = user_wr->wr_id;
2327 next->num_sge = user_wr->num_sge;
2328 next->opcode = user_wr->opcode;
2329 next->send_flags = user_wr->send_flags;
2331 if (next->num_sge) {
2332 next->sg_list = (void *) next +
2333 ALIGN(next_size, sizeof(struct ib_sge));
2334 if (copy_from_user(next->sg_list,
2336 cmd.wr_count * cmd.wqe_size +
2337 sg_ind * sizeof (struct ib_sge),
2338 next->num_sge * sizeof (struct ib_sge))) {
2342 sg_ind += next->num_sge;
2344 next->sg_list = NULL;
2348 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2350 for (next = wr; next; next = next->next) {
2356 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2360 uobj_put_obj_read(qp);
2363 if (is_ud && ud_wr(wr)->ah)
2364 uobj_put_obj_read(ud_wr(wr)->ah);
2373 return ret ? ret : in_len;
2376 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2382 struct ib_uverbs_recv_wr *user_wr;
2383 struct ib_recv_wr *wr = NULL, *last, *next;
2388 if (in_len < wqe_size * wr_count +
2389 sge_count * sizeof (struct ib_uverbs_sge))
2390 return ERR_PTR(-EINVAL);
2392 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2393 return ERR_PTR(-EINVAL);
2395 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2397 return ERR_PTR(-ENOMEM);
2401 for (i = 0; i < wr_count; ++i) {
2402 if (copy_from_user(user_wr, buf + i * wqe_size,
2408 if (user_wr->num_sge + sg_ind > sge_count) {
2413 if (user_wr->num_sge >=
2414 (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
2415 sizeof (struct ib_sge)) {
2420 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2421 user_wr->num_sge * sizeof (struct ib_sge),
2435 next->wr_id = user_wr->wr_id;
2436 next->num_sge = user_wr->num_sge;
2438 if (next->num_sge) {
2439 next->sg_list = (void *) next +
2440 ALIGN(sizeof *next, sizeof (struct ib_sge));
2441 if (copy_from_user(next->sg_list,
2442 buf + wr_count * wqe_size +
2443 sg_ind * sizeof (struct ib_sge),
2444 next->num_sge * sizeof (struct ib_sge))) {
2448 sg_ind += next->num_sge;
2450 next->sg_list = NULL;
2465 return ERR_PTR(ret);
2468 static ssize_t ib_uverbs_post_recv(struct uverbs_attr_bundle *attrs,
2469 const char __user *buf, int in_len,
2472 struct ib_uverbs_post_recv cmd;
2473 struct ib_uverbs_post_recv_resp resp;
2474 struct ib_recv_wr *wr, *next;
2475 const struct ib_recv_wr *bad_wr;
2477 ssize_t ret = -EINVAL;
2479 if (copy_from_user(&cmd, buf, sizeof cmd))
2482 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2483 in_len - sizeof cmd, cmd.wr_count,
2484 cmd.sge_count, cmd.wqe_size);
2488 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2493 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2495 uobj_put_obj_read(qp);
2497 for (next = wr; next; next = next->next) {
2504 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2514 return ret ? ret : in_len;
2517 static ssize_t ib_uverbs_post_srq_recv(struct uverbs_attr_bundle *attrs,
2518 const char __user *buf, int in_len,
2521 struct ib_uverbs_post_srq_recv cmd;
2522 struct ib_uverbs_post_srq_recv_resp resp;
2523 struct ib_recv_wr *wr, *next;
2524 const struct ib_recv_wr *bad_wr;
2526 ssize_t ret = -EINVAL;
2528 if (copy_from_user(&cmd, buf, sizeof cmd))
2531 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2532 in_len - sizeof cmd, cmd.wr_count,
2533 cmd.sge_count, cmd.wqe_size);
2537 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
2542 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2544 uobj_put_obj_read(srq);
2547 for (next = wr; next; next = next->next) {
2553 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2563 return ret ? ret : in_len;
2566 static ssize_t ib_uverbs_create_ah(struct uverbs_attr_bundle *attrs,
2567 const char __user *buf, int in_len,
2570 struct ib_uverbs_create_ah cmd;
2571 struct ib_uverbs_create_ah_resp resp;
2572 struct ib_uobject *uobj;
2575 struct rdma_ah_attr attr = {};
2577 struct ib_udata udata;
2578 struct ib_device *ib_dev;
2580 if (out_len < sizeof resp)
2583 if (copy_from_user(&cmd, buf, sizeof cmd))
2586 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
2587 u64_to_user_ptr(cmd.response) + sizeof(resp),
2588 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
2589 out_len - sizeof(resp));
2591 uobj = uobj_alloc(UVERBS_OBJECT_AH, attrs, &ib_dev);
2593 return PTR_ERR(uobj);
2595 if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2600 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2606 attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2607 rdma_ah_set_make_grd(&attr, false);
2608 rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2609 rdma_ah_set_sl(&attr, cmd.attr.sl);
2610 rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2611 rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2612 rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2614 if (cmd.attr.is_global) {
2615 rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2616 cmd.attr.grh.sgid_index,
2617 cmd.attr.grh.hop_limit,
2618 cmd.attr.grh.traffic_class);
2619 rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2621 rdma_ah_set_ah_flags(&attr, 0);
2624 ah = rdma_create_user_ah(pd, &attr, &udata);
2631 uobj->user_handle = cmd.user_handle;
2634 resp.ah_handle = uobj->id;
2636 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
2641 uobj_put_obj_read(pd);
2642 return uobj_alloc_commit(uobj, in_len);
2645 rdma_destroy_ah(ah);
2648 uobj_put_obj_read(pd);
2651 uobj_alloc_abort(uobj);
2655 static ssize_t ib_uverbs_destroy_ah(struct uverbs_attr_bundle *attrs,
2656 const char __user *buf, int in_len,
2659 struct ib_uverbs_destroy_ah cmd;
2661 if (copy_from_user(&cmd, buf, sizeof cmd))
2664 return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, attrs,
2668 static ssize_t ib_uverbs_attach_mcast(struct uverbs_attr_bundle *attrs,
2669 const char __user *buf, int in_len,
2672 struct ib_uverbs_attach_mcast cmd;
2674 struct ib_uqp_object *obj;
2675 struct ib_uverbs_mcast_entry *mcast;
2678 if (copy_from_user(&cmd, buf, sizeof cmd))
2681 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2685 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2687 mutex_lock(&obj->mcast_lock);
2688 list_for_each_entry(mcast, &obj->mcast_list, list)
2689 if (cmd.mlid == mcast->lid &&
2690 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2695 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2701 mcast->lid = cmd.mlid;
2702 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2704 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2706 list_add_tail(&mcast->list, &obj->mcast_list);
2711 mutex_unlock(&obj->mcast_lock);
2712 uobj_put_obj_read(qp);
2714 return ret ? ret : in_len;
2717 static ssize_t ib_uverbs_detach_mcast(struct uverbs_attr_bundle *attrs,
2718 const char __user *buf, int in_len,
2721 struct ib_uverbs_detach_mcast cmd;
2722 struct ib_uqp_object *obj;
2724 struct ib_uverbs_mcast_entry *mcast;
2728 if (copy_from_user(&cmd, buf, sizeof cmd))
2731 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2735 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2736 mutex_lock(&obj->mcast_lock);
2738 list_for_each_entry(mcast, &obj->mcast_list, list)
2739 if (cmd.mlid == mcast->lid &&
2740 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2741 list_del(&mcast->list);
2752 ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2755 mutex_unlock(&obj->mcast_lock);
2756 uobj_put_obj_read(qp);
2757 return ret ? ret : in_len;
2760 struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2762 struct ib_uflow_resources *resources;
2764 resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2772 resources->counters =
2773 kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2774 resources->collection =
2775 kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2777 if (!resources->counters || !resources->collection)
2781 resources->max = num_specs;
2785 kfree(resources->counters);
2790 EXPORT_SYMBOL(flow_resources_alloc);
2792 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
2799 for (i = 0; i < uflow_res->collection_num; i++)
2800 atomic_dec(&uflow_res->collection[i]->usecnt);
2802 for (i = 0; i < uflow_res->counters_num; i++)
2803 atomic_dec(&uflow_res->counters[i]->usecnt);
2805 kfree(uflow_res->collection);
2806 kfree(uflow_res->counters);
2809 EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2811 void flow_resources_add(struct ib_uflow_resources *uflow_res,
2812 enum ib_flow_spec_type type,
2815 WARN_ON(uflow_res->num >= uflow_res->max);
2818 case IB_FLOW_SPEC_ACTION_HANDLE:
2819 atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2820 uflow_res->collection[uflow_res->collection_num++] =
2821 (struct ib_flow_action *)ibobj;
2823 case IB_FLOW_SPEC_ACTION_COUNT:
2824 atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2825 uflow_res->counters[uflow_res->counters_num++] =
2826 (struct ib_counters *)ibobj;
2834 EXPORT_SYMBOL(flow_resources_add);
2836 static int kern_spec_to_ib_spec_action(const struct uverbs_attr_bundle *attrs,
2837 struct ib_uverbs_flow_spec *kern_spec,
2838 union ib_flow_spec *ib_spec,
2839 struct ib_uflow_resources *uflow_res)
2841 ib_spec->type = kern_spec->type;
2842 switch (ib_spec->type) {
2843 case IB_FLOW_SPEC_ACTION_TAG:
2844 if (kern_spec->flow_tag.size !=
2845 sizeof(struct ib_uverbs_flow_spec_action_tag))
2848 ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2849 ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2851 case IB_FLOW_SPEC_ACTION_DROP:
2852 if (kern_spec->drop.size !=
2853 sizeof(struct ib_uverbs_flow_spec_action_drop))
2856 ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2858 case IB_FLOW_SPEC_ACTION_HANDLE:
2859 if (kern_spec->action.size !=
2860 sizeof(struct ib_uverbs_flow_spec_action_handle))
2862 ib_spec->action.act = uobj_get_obj_read(flow_action,
2863 UVERBS_OBJECT_FLOW_ACTION,
2864 kern_spec->action.handle,
2866 if (!ib_spec->action.act)
2868 ib_spec->action.size =
2869 sizeof(struct ib_flow_spec_action_handle);
2870 flow_resources_add(uflow_res,
2871 IB_FLOW_SPEC_ACTION_HANDLE,
2872 ib_spec->action.act);
2873 uobj_put_obj_read(ib_spec->action.act);
2875 case IB_FLOW_SPEC_ACTION_COUNT:
2876 if (kern_spec->flow_count.size !=
2877 sizeof(struct ib_uverbs_flow_spec_action_count))
2879 ib_spec->flow_count.counters =
2880 uobj_get_obj_read(counters,
2881 UVERBS_OBJECT_COUNTERS,
2882 kern_spec->flow_count.handle,
2884 if (!ib_spec->flow_count.counters)
2886 ib_spec->flow_count.size =
2887 sizeof(struct ib_flow_spec_action_count);
2888 flow_resources_add(uflow_res,
2889 IB_FLOW_SPEC_ACTION_COUNT,
2890 ib_spec->flow_count.counters);
2891 uobj_put_obj_read(ib_spec->flow_count.counters);
2899 static size_t kern_spec_filter_sz(const struct ib_uverbs_flow_spec_hdr *spec)
2901 /* Returns user space filter size, includes padding */
2902 return (spec->size - sizeof(struct ib_uverbs_flow_spec_hdr)) / 2;
2905 static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2906 u16 ib_real_filter_sz)
2909 * User space filter structures must be 64 bit aligned, otherwise this
2910 * may pass, but we won't handle additional new attributes.
2913 if (kern_filter_size > ib_real_filter_sz) {
2914 if (memchr_inv(kern_spec_filter +
2915 ib_real_filter_sz, 0,
2916 kern_filter_size - ib_real_filter_sz))
2918 return ib_real_filter_sz;
2920 return kern_filter_size;
2923 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2924 const void *kern_spec_mask,
2925 const void *kern_spec_val,
2926 size_t kern_filter_sz,
2927 union ib_flow_spec *ib_spec)
2929 ssize_t actual_filter_sz;
2930 ssize_t ib_filter_sz;
2932 /* User flow spec size must be aligned to 4 bytes */
2933 if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2936 ib_spec->type = type;
2938 if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2941 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2942 case IB_FLOW_SPEC_ETH:
2943 ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
2944 actual_filter_sz = spec_filter_size(kern_spec_mask,
2947 if (actual_filter_sz <= 0)
2949 ib_spec->size = sizeof(struct ib_flow_spec_eth);
2950 memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2951 memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2953 case IB_FLOW_SPEC_IPV4:
2954 ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
2955 actual_filter_sz = spec_filter_size(kern_spec_mask,
2958 if (actual_filter_sz <= 0)
2960 ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2961 memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2962 memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2964 case IB_FLOW_SPEC_IPV6:
2965 ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
2966 actual_filter_sz = spec_filter_size(kern_spec_mask,
2969 if (actual_filter_sz <= 0)
2971 ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2972 memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2973 memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2975 if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2976 (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2979 case IB_FLOW_SPEC_TCP:
2980 case IB_FLOW_SPEC_UDP:
2981 ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
2982 actual_filter_sz = spec_filter_size(kern_spec_mask,
2985 if (actual_filter_sz <= 0)
2987 ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2988 memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2989 memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2991 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2992 ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
2993 actual_filter_sz = spec_filter_size(kern_spec_mask,
2996 if (actual_filter_sz <= 0)
2998 ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
2999 memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
3000 memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
3002 if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
3003 (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
3006 case IB_FLOW_SPEC_ESP:
3007 ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
3008 actual_filter_sz = spec_filter_size(kern_spec_mask,
3011 if (actual_filter_sz <= 0)
3013 ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
3014 memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
3015 memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
3017 case IB_FLOW_SPEC_GRE:
3018 ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
3019 actual_filter_sz = spec_filter_size(kern_spec_mask,
3022 if (actual_filter_sz <= 0)
3024 ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
3025 memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
3026 memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
3028 case IB_FLOW_SPEC_MPLS:
3029 ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
3030 actual_filter_sz = spec_filter_size(kern_spec_mask,
3033 if (actual_filter_sz <= 0)
3035 ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
3036 memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
3037 memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
3045 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
3046 union ib_flow_spec *ib_spec)
3048 ssize_t kern_filter_sz;
3049 void *kern_spec_mask;
3050 void *kern_spec_val;
3052 kern_filter_sz = kern_spec_filter_sz(&kern_spec->hdr);
3054 kern_spec_val = (void *)kern_spec +
3055 sizeof(struct ib_uverbs_flow_spec_hdr);
3056 kern_spec_mask = kern_spec_val + kern_filter_sz;
3058 return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
3061 kern_filter_sz, ib_spec);
3064 static int kern_spec_to_ib_spec(struct uverbs_attr_bundle *attrs,
3065 struct ib_uverbs_flow_spec *kern_spec,
3066 union ib_flow_spec *ib_spec,
3067 struct ib_uflow_resources *uflow_res)
3069 if (kern_spec->reserved)
3072 if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
3073 return kern_spec_to_ib_spec_action(attrs, kern_spec, ib_spec,
3076 return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
3079 static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle *attrs,
3080 struct ib_udata *ucore, struct ib_udata *uhw)
3082 struct ib_uverbs_ex_create_wq cmd = {};
3083 struct ib_uverbs_ex_create_wq_resp resp = {};
3084 struct ib_uwq_object *obj;
3089 struct ib_wq_init_attr wq_init_attr = {};
3090 size_t required_cmd_sz;
3091 size_t required_resp_len;
3092 struct ib_device *ib_dev;
3094 required_cmd_sz = offsetof(typeof(cmd), max_sge) + sizeof(cmd.max_sge);
3095 required_resp_len = offsetof(typeof(resp), wqn) + sizeof(resp.wqn);
3097 if (ucore->inlen < required_cmd_sz)
3100 if (ucore->outlen < required_resp_len)
3103 if (ucore->inlen > sizeof(cmd) &&
3104 !ib_is_udata_cleared(ucore, sizeof(cmd),
3105 ucore->inlen - sizeof(cmd)))
3108 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3115 obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, attrs,
3118 return PTR_ERR(obj);
3120 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
3126 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3132 wq_init_attr.cq = cq;
3133 wq_init_attr.max_sge = cmd.max_sge;
3134 wq_init_attr.max_wr = cmd.max_wr;
3135 wq_init_attr.wq_context = attrs->ufile;
3136 wq_init_attr.wq_type = cmd.wq_type;
3137 wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
3138 if (ucore->inlen >= (offsetof(typeof(cmd), create_flags) +
3139 sizeof(cmd.create_flags)))
3140 wq_init_attr.create_flags = cmd.create_flags;
3141 obj->uevent.events_reported = 0;
3142 INIT_LIST_HEAD(&obj->uevent.event_list);
3144 wq = pd->device->create_wq(pd, &wq_init_attr, uhw);
3150 wq->uobject = &obj->uevent.uobject;
3151 obj->uevent.uobject.object = wq;
3152 wq->wq_type = wq_init_attr.wq_type;
3155 wq->device = pd->device;
3156 wq->wq_context = wq_init_attr.wq_context;
3157 atomic_set(&wq->usecnt, 0);
3158 atomic_inc(&pd->usecnt);
3159 atomic_inc(&cq->usecnt);
3160 wq->uobject = &obj->uevent.uobject;
3161 obj->uevent.uobject.object = wq;
3163 memset(&resp, 0, sizeof(resp));
3164 resp.wq_handle = obj->uevent.uobject.id;
3165 resp.max_sge = wq_init_attr.max_sge;
3166 resp.max_wr = wq_init_attr.max_wr;
3167 resp.wqn = wq->wq_num;
3168 resp.response_length = required_resp_len;
3169 err = ib_copy_to_udata(ucore,
3170 &resp, resp.response_length);
3174 uobj_put_obj_read(pd);
3175 uobj_put_obj_read(cq);
3176 return uobj_alloc_commit(&obj->uevent.uobject, 0);
3181 uobj_put_obj_read(cq);
3183 uobj_put_obj_read(pd);
3185 uobj_alloc_abort(&obj->uevent.uobject);
3190 static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle *attrs,
3191 struct ib_udata *ucore, struct ib_udata *uhw)
3193 struct ib_uverbs_ex_destroy_wq cmd = {};
3194 struct ib_uverbs_ex_destroy_wq_resp resp = {};
3195 struct ib_uobject *uobj;
3196 struct ib_uwq_object *obj;
3197 size_t required_cmd_sz;
3198 size_t required_resp_len;
3201 required_cmd_sz = offsetof(typeof(cmd), wq_handle) + sizeof(cmd.wq_handle);
3202 required_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
3204 if (ucore->inlen < required_cmd_sz)
3207 if (ucore->outlen < required_resp_len)
3210 if (ucore->inlen > sizeof(cmd) &&
3211 !ib_is_udata_cleared(ucore, sizeof(cmd),
3212 ucore->inlen - sizeof(cmd)))
3215 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3222 resp.response_length = required_resp_len;
3223 uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3225 return PTR_ERR(uobj);
3227 obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3228 resp.events_reported = obj->uevent.events_reported;
3230 uobj_put_destroy(uobj);
3232 return ib_copy_to_udata(ucore, &resp, resp.response_length);
3235 static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle *attrs,
3236 struct ib_udata *ucore, struct ib_udata *uhw)
3238 struct ib_uverbs_ex_modify_wq cmd = {};
3240 struct ib_wq_attr wq_attr = {};
3241 size_t required_cmd_sz;
3244 required_cmd_sz = offsetof(typeof(cmd), curr_wq_state) + sizeof(cmd.curr_wq_state);
3245 if (ucore->inlen < required_cmd_sz)
3248 if (ucore->inlen > sizeof(cmd) &&
3249 !ib_is_udata_cleared(ucore, sizeof(cmd),
3250 ucore->inlen - sizeof(cmd)))
3253 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3260 if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3263 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3267 wq_attr.curr_wq_state = cmd.curr_wq_state;
3268 wq_attr.wq_state = cmd.wq_state;
3269 if (cmd.attr_mask & IB_WQ_FLAGS) {
3270 wq_attr.flags = cmd.flags;
3271 wq_attr.flags_mask = cmd.flags_mask;
3273 ret = wq->device->modify_wq(wq, &wq_attr, cmd.attr_mask, uhw);
3274 uobj_put_obj_read(wq);
3278 static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle *attrs,
3279 struct ib_udata *ucore,
3280 struct ib_udata *uhw)
3282 struct ib_uverbs_ex_create_rwq_ind_table cmd = {};
3283 struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
3284 struct ib_uobject *uobj;
3286 struct ib_rwq_ind_table_init_attr init_attr = {};
3287 struct ib_rwq_ind_table *rwq_ind_tbl;
3288 struct ib_wq **wqs = NULL;
3289 u32 *wqs_handles = NULL;
3290 struct ib_wq *wq = NULL;
3291 int i, j, num_read_wqs;
3293 u32 expected_in_size;
3294 size_t required_cmd_sz_header;
3295 size_t required_resp_len;
3296 struct ib_device *ib_dev;
3298 required_cmd_sz_header = offsetof(typeof(cmd), log_ind_tbl_size) + sizeof(cmd.log_ind_tbl_size);
3299 required_resp_len = offsetof(typeof(resp), ind_tbl_num) + sizeof(resp.ind_tbl_num);
3301 if (ucore->inlen < required_cmd_sz_header)
3304 if (ucore->outlen < required_resp_len)
3307 err = ib_copy_from_udata(&cmd, ucore, required_cmd_sz_header);
3311 ucore->inbuf += required_cmd_sz_header;
3312 ucore->inlen -= required_cmd_sz_header;
3317 if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3320 num_wq_handles = 1 << cmd.log_ind_tbl_size;
3321 expected_in_size = num_wq_handles * sizeof(__u32);
3322 if (num_wq_handles == 1)
3323 /* input size for wq handles is u64 aligned */
3324 expected_in_size += sizeof(__u32);
3326 if (ucore->inlen < expected_in_size)
3329 if (ucore->inlen > expected_in_size &&
3330 !ib_is_udata_cleared(ucore, expected_in_size,
3331 ucore->inlen - expected_in_size))
3334 wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3339 err = ib_copy_from_udata(wqs_handles, ucore,
3340 num_wq_handles * sizeof(__u32));
3344 wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3350 for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3352 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3353 wqs_handles[num_read_wqs], attrs);
3359 wqs[num_read_wqs] = wq;
3362 uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, attrs, &ib_dev);
3364 err = PTR_ERR(uobj);
3368 init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3369 init_attr.ind_tbl = wqs;
3371 rwq_ind_tbl = ib_dev->create_rwq_ind_table(ib_dev, &init_attr, uhw);
3373 if (IS_ERR(rwq_ind_tbl)) {
3374 err = PTR_ERR(rwq_ind_tbl);
3378 rwq_ind_tbl->ind_tbl = wqs;
3379 rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3380 rwq_ind_tbl->uobject = uobj;
3381 uobj->object = rwq_ind_tbl;
3382 rwq_ind_tbl->device = ib_dev;
3383 atomic_set(&rwq_ind_tbl->usecnt, 0);
3385 for (i = 0; i < num_wq_handles; i++)
3386 atomic_inc(&wqs[i]->usecnt);
3388 resp.ind_tbl_handle = uobj->id;
3389 resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3390 resp.response_length = required_resp_len;
3392 err = ib_copy_to_udata(ucore,
3393 &resp, resp.response_length);
3399 for (j = 0; j < num_read_wqs; j++)
3400 uobj_put_obj_read(wqs[j]);
3402 return uobj_alloc_commit(uobj, 0);
3405 ib_destroy_rwq_ind_table(rwq_ind_tbl);
3407 uobj_alloc_abort(uobj);
3409 for (j = 0; j < num_read_wqs; j++)
3410 uobj_put_obj_read(wqs[j]);
3417 static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle *attrs,
3418 struct ib_udata *ucore,
3419 struct ib_udata *uhw)
3421 struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
3423 size_t required_cmd_sz;
3425 required_cmd_sz = offsetof(typeof(cmd), ind_tbl_handle) + sizeof(cmd.ind_tbl_handle);
3427 if (ucore->inlen < required_cmd_sz)
3430 if (ucore->inlen > sizeof(cmd) &&
3431 !ib_is_udata_cleared(ucore, sizeof(cmd),
3432 ucore->inlen - sizeof(cmd)))
3435 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3442 return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3443 cmd.ind_tbl_handle, attrs, 0);
3446 static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle *attrs,
3447 struct ib_udata *ucore,
3448 struct ib_udata *uhw)
3450 struct ib_uverbs_create_flow cmd;
3451 struct ib_uverbs_create_flow_resp resp;
3452 struct ib_uobject *uobj;
3453 struct ib_flow *flow_id;
3454 struct ib_uverbs_flow_attr *kern_flow_attr;
3455 struct ib_flow_attr *flow_attr;
3457 struct ib_uflow_resources *uflow_res;
3458 struct ib_uverbs_flow_spec_hdr *kern_spec;
3462 struct ib_device *ib_dev;
3464 if (ucore->inlen < sizeof(cmd))
3467 if (ucore->outlen < sizeof(resp))
3470 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3474 ucore->inbuf += sizeof(cmd);
3475 ucore->inlen -= sizeof(cmd);
3480 if (!capable(CAP_NET_RAW))
3483 if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3486 if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3487 ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3488 (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3491 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3494 if (cmd.flow_attr.size > ucore->inlen ||
3495 cmd.flow_attr.size >
3496 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3499 if (cmd.flow_attr.reserved[0] ||
3500 cmd.flow_attr.reserved[1])
3503 if (cmd.flow_attr.num_of_specs) {
3504 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3506 if (!kern_flow_attr)
3509 *kern_flow_attr = cmd.flow_attr;
3510 err = ib_copy_from_udata(&kern_flow_attr->flow_specs, ucore,
3511 cmd.flow_attr.size);
3515 kern_flow_attr = &cmd.flow_attr;
3518 uobj = uobj_alloc(UVERBS_OBJECT_FLOW, attrs, &ib_dev);
3520 err = PTR_ERR(uobj);
3524 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
3530 if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3535 flow_attr = kzalloc(struct_size(flow_attr, flows,
3536 cmd.flow_attr.num_of_specs), GFP_KERNEL);
3541 uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3544 goto err_free_flow_attr;
3547 flow_attr->type = kern_flow_attr->type;
3548 flow_attr->priority = kern_flow_attr->priority;
3549 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3550 flow_attr->port = kern_flow_attr->port;
3551 flow_attr->flags = kern_flow_attr->flags;
3552 flow_attr->size = sizeof(*flow_attr);
3554 kern_spec = kern_flow_attr->flow_specs;
3555 ib_spec = flow_attr + 1;
3556 for (i = 0; i < flow_attr->num_of_specs &&
3557 cmd.flow_attr.size >= sizeof(*kern_spec) &&
3558 cmd.flow_attr.size >= kern_spec->size;
3560 err = kern_spec_to_ib_spec(
3561 attrs, (struct ib_uverbs_flow_spec *)kern_spec,
3562 ib_spec, uflow_res);
3567 ((union ib_flow_spec *) ib_spec)->size;
3568 cmd.flow_attr.size -= kern_spec->size;
3569 kern_spec = ((void *)kern_spec) + kern_spec->size;
3570 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3572 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3573 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3574 i, cmd.flow_attr.size);
3579 flow_id = qp->device->create_flow(qp, flow_attr,
3580 IB_FLOW_DOMAIN_USER, uhw);
3582 if (IS_ERR(flow_id)) {
3583 err = PTR_ERR(flow_id);
3587 ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3589 memset(&resp, 0, sizeof(resp));
3590 resp.flow_handle = uobj->id;
3592 err = ib_copy_to_udata(ucore,
3593 &resp, sizeof(resp));
3597 uobj_put_obj_read(qp);
3599 if (cmd.flow_attr.num_of_specs)
3600 kfree(kern_flow_attr);
3601 return uobj_alloc_commit(uobj, 0);
3603 if (!qp->device->destroy_flow(flow_id))
3604 atomic_dec(&qp->usecnt);
3606 ib_uverbs_flow_resources_free(uflow_res);
3610 uobj_put_obj_read(qp);
3612 uobj_alloc_abort(uobj);
3614 if (cmd.flow_attr.num_of_specs)
3615 kfree(kern_flow_attr);
3619 static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle *attrs,
3620 struct ib_udata *ucore,
3621 struct ib_udata *uhw)
3623 struct ib_uverbs_destroy_flow cmd;
3626 if (ucore->inlen < sizeof(cmd))
3629 ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3636 return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, attrs,
3640 static int __uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3641 struct ib_uverbs_create_xsrq *cmd,
3642 struct ib_udata *udata)
3644 struct ib_uverbs_create_srq_resp resp;
3645 struct ib_usrq_object *obj;
3648 struct ib_uobject *uninitialized_var(xrcd_uobj);
3649 struct ib_srq_init_attr attr;
3651 struct ib_device *ib_dev;
3653 obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, attrs,
3656 return PTR_ERR(obj);
3658 if (cmd->srq_type == IB_SRQT_TM)
3659 attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3661 if (cmd->srq_type == IB_SRQT_XRC) {
3662 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3664 if (IS_ERR(xrcd_uobj)) {
3669 attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3670 if (!attr.ext.xrc.xrcd) {
3675 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3676 atomic_inc(&obj->uxrcd->refcnt);
3679 if (ib_srq_has_cq(cmd->srq_type)) {
3680 attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3681 cmd->cq_handle, attrs);
3688 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, attrs);
3694 attr.event_handler = ib_uverbs_srq_event_handler;
3695 attr.srq_context = attrs->ufile;
3696 attr.srq_type = cmd->srq_type;
3697 attr.attr.max_wr = cmd->max_wr;
3698 attr.attr.max_sge = cmd->max_sge;
3699 attr.attr.srq_limit = cmd->srq_limit;
3701 obj->uevent.events_reported = 0;
3702 INIT_LIST_HEAD(&obj->uevent.event_list);
3704 srq = pd->device->create_srq(pd, &attr, udata);
3710 srq->device = pd->device;
3712 srq->srq_type = cmd->srq_type;
3713 srq->uobject = &obj->uevent.uobject;
3714 srq->event_handler = attr.event_handler;
3715 srq->srq_context = attr.srq_context;
3717 if (ib_srq_has_cq(cmd->srq_type)) {
3718 srq->ext.cq = attr.ext.cq;
3719 atomic_inc(&attr.ext.cq->usecnt);
3722 if (cmd->srq_type == IB_SRQT_XRC) {
3723 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
3724 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
3727 atomic_inc(&pd->usecnt);
3728 atomic_set(&srq->usecnt, 0);
3730 obj->uevent.uobject.object = srq;
3731 obj->uevent.uobject.user_handle = cmd->user_handle;
3733 memset(&resp, 0, sizeof resp);
3734 resp.srq_handle = obj->uevent.uobject.id;
3735 resp.max_wr = attr.attr.max_wr;
3736 resp.max_sge = attr.attr.max_sge;
3737 if (cmd->srq_type == IB_SRQT_XRC)
3738 resp.srqn = srq->ext.xrc.srq_num;
3740 if (copy_to_user(u64_to_user_ptr(cmd->response),
3741 &resp, sizeof resp)) {
3746 if (cmd->srq_type == IB_SRQT_XRC)
3747 uobj_put_read(xrcd_uobj);
3749 if (ib_srq_has_cq(cmd->srq_type))
3750 uobj_put_obj_read(attr.ext.cq);
3752 uobj_put_obj_read(pd);
3753 return uobj_alloc_commit(&obj->uevent.uobject, 0);
3756 ib_destroy_srq(srq);
3759 uobj_put_obj_read(pd);
3762 if (ib_srq_has_cq(cmd->srq_type))
3763 uobj_put_obj_read(attr.ext.cq);
3766 if (cmd->srq_type == IB_SRQT_XRC) {
3767 atomic_dec(&obj->uxrcd->refcnt);
3768 uobj_put_read(xrcd_uobj);
3772 uobj_alloc_abort(&obj->uevent.uobject);
3776 static ssize_t ib_uverbs_create_srq(struct uverbs_attr_bundle *attrs,
3777 const char __user *buf, int in_len,
3780 struct ib_uverbs_create_srq cmd;
3781 struct ib_uverbs_create_xsrq xcmd;
3782 struct ib_uverbs_create_srq_resp resp;
3783 struct ib_udata udata;
3786 if (out_len < sizeof resp)
3789 if (copy_from_user(&cmd, buf, sizeof cmd))
3792 memset(&xcmd, 0, sizeof(xcmd));
3793 xcmd.response = cmd.response;
3794 xcmd.user_handle = cmd.user_handle;
3795 xcmd.srq_type = IB_SRQT_BASIC;
3796 xcmd.pd_handle = cmd.pd_handle;
3797 xcmd.max_wr = cmd.max_wr;
3798 xcmd.max_sge = cmd.max_sge;
3799 xcmd.srq_limit = cmd.srq_limit;
3801 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
3802 u64_to_user_ptr(cmd.response) + sizeof(resp),
3803 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
3804 out_len - sizeof(resp));
3806 ret = __uverbs_create_xsrq(attrs, &xcmd, &udata);
3813 static ssize_t ib_uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3814 const char __user *buf, int in_len,
3817 struct ib_uverbs_create_xsrq cmd;
3818 struct ib_uverbs_create_srq_resp resp;
3819 struct ib_udata udata;
3822 if (out_len < sizeof resp)
3825 if (copy_from_user(&cmd, buf, sizeof cmd))
3828 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
3829 u64_to_user_ptr(cmd.response) + sizeof(resp),
3830 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
3831 out_len - sizeof(resp));
3833 ret = __uverbs_create_xsrq(attrs, &cmd, &udata);
3840 static ssize_t ib_uverbs_modify_srq(struct uverbs_attr_bundle *attrs,
3841 const char __user *buf, int in_len,
3844 struct ib_uverbs_modify_srq cmd;
3845 struct ib_udata udata;
3847 struct ib_srq_attr attr;
3850 if (copy_from_user(&cmd, buf, sizeof cmd))
3853 ib_uverbs_init_udata(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
3856 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3860 attr.max_wr = cmd.max_wr;
3861 attr.srq_limit = cmd.srq_limit;
3863 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
3865 uobj_put_obj_read(srq);
3867 return ret ? ret : in_len;
3870 static ssize_t ib_uverbs_query_srq(struct uverbs_attr_bundle *attrs,
3871 const char __user *buf, int in_len,
3874 struct ib_uverbs_query_srq cmd;
3875 struct ib_uverbs_query_srq_resp resp;
3876 struct ib_srq_attr attr;
3880 if (out_len < sizeof resp)
3883 if (copy_from_user(&cmd, buf, sizeof cmd))
3886 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3890 ret = ib_query_srq(srq, &attr);
3892 uobj_put_obj_read(srq);
3897 memset(&resp, 0, sizeof resp);
3899 resp.max_wr = attr.max_wr;
3900 resp.max_sge = attr.max_sge;
3901 resp.srq_limit = attr.srq_limit;
3903 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
3909 static ssize_t ib_uverbs_destroy_srq(struct uverbs_attr_bundle *attrs,
3910 const char __user *buf, int in_len,
3913 struct ib_uverbs_destroy_srq cmd;
3914 struct ib_uverbs_destroy_srq_resp resp;
3915 struct ib_uobject *uobj;
3916 struct ib_uevent_object *obj;
3918 if (copy_from_user(&cmd, buf, sizeof cmd))
3921 uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3923 return PTR_ERR(uobj);
3925 obj = container_of(uobj, struct ib_uevent_object, uobject);
3926 memset(&resp, 0, sizeof(resp));
3927 resp.events_reported = obj->events_reported;
3929 uobj_put_destroy(uobj);
3931 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
3937 static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle *attrs,
3938 struct ib_udata *ucore,
3939 struct ib_udata *uhw)
3941 struct ib_uverbs_ex_query_device_resp resp = { {0} };
3942 struct ib_uverbs_ex_query_device cmd;
3943 struct ib_device_attr attr = {0};
3944 struct ib_ucontext *ucontext;
3945 struct ib_device *ib_dev;
3948 ucontext = ib_uverbs_get_ucontext(attrs);
3949 if (IS_ERR(ucontext))
3950 return PTR_ERR(ucontext);
3951 ib_dev = ucontext->device;
3953 if (ucore->inlen < sizeof(cmd))
3956 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3966 resp.response_length = offsetof(typeof(resp), odp_caps);
3968 if (ucore->outlen < resp.response_length)
3971 err = ib_dev->query_device(ib_dev, &attr, uhw);
3975 copy_query_dev_fields(ucontext, &resp.base, &attr);
3977 if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
3980 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3981 resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3982 resp.odp_caps.per_transport_caps.rc_odp_caps =
3983 attr.odp_caps.per_transport_caps.rc_odp_caps;
3984 resp.odp_caps.per_transport_caps.uc_odp_caps =
3985 attr.odp_caps.per_transport_caps.uc_odp_caps;
3986 resp.odp_caps.per_transport_caps.ud_odp_caps =
3987 attr.odp_caps.per_transport_caps.ud_odp_caps;
3989 resp.response_length += sizeof(resp.odp_caps);
3991 if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
3994 resp.timestamp_mask = attr.timestamp_mask;
3995 resp.response_length += sizeof(resp.timestamp_mask);
3997 if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
4000 resp.hca_core_clock = attr.hca_core_clock;
4001 resp.response_length += sizeof(resp.hca_core_clock);
4003 if (ucore->outlen < resp.response_length + sizeof(resp.device_cap_flags_ex))
4006 resp.device_cap_flags_ex = attr.device_cap_flags;
4007 resp.response_length += sizeof(resp.device_cap_flags_ex);
4009 if (ucore->outlen < resp.response_length + sizeof(resp.rss_caps))
4012 resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
4013 resp.rss_caps.max_rwq_indirection_tables =
4014 attr.rss_caps.max_rwq_indirection_tables;
4015 resp.rss_caps.max_rwq_indirection_table_size =
4016 attr.rss_caps.max_rwq_indirection_table_size;
4018 resp.response_length += sizeof(resp.rss_caps);
4020 if (ucore->outlen < resp.response_length + sizeof(resp.max_wq_type_rq))
4023 resp.max_wq_type_rq = attr.max_wq_type_rq;
4024 resp.response_length += sizeof(resp.max_wq_type_rq);
4026 if (ucore->outlen < resp.response_length + sizeof(resp.raw_packet_caps))
4029 resp.raw_packet_caps = attr.raw_packet_caps;
4030 resp.response_length += sizeof(resp.raw_packet_caps);
4032 if (ucore->outlen < resp.response_length + sizeof(resp.tm_caps))
4035 resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
4036 resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
4037 resp.tm_caps.max_ops = attr.tm_caps.max_ops;
4038 resp.tm_caps.max_sge = attr.tm_caps.max_sge;
4039 resp.tm_caps.flags = attr.tm_caps.flags;
4040 resp.response_length += sizeof(resp.tm_caps);
4042 if (ucore->outlen < resp.response_length + sizeof(resp.cq_moderation_caps))
4045 resp.cq_moderation_caps.max_cq_moderation_count =
4046 attr.cq_caps.max_cq_moderation_count;
4047 resp.cq_moderation_caps.max_cq_moderation_period =
4048 attr.cq_caps.max_cq_moderation_period;
4049 resp.response_length += sizeof(resp.cq_moderation_caps);
4051 if (ucore->outlen < resp.response_length + sizeof(resp.max_dm_size))
4054 resp.max_dm_size = attr.max_dm_size;
4055 resp.response_length += sizeof(resp.max_dm_size);
4057 err = ib_copy_to_udata(ucore, &resp, resp.response_length);
4061 static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs,
4062 struct ib_udata *ucore, struct ib_udata *uhw)
4064 struct ib_uverbs_ex_modify_cq cmd = {};
4066 size_t required_cmd_sz;
4069 required_cmd_sz = offsetof(typeof(cmd), reserved) +
4070 sizeof(cmd.reserved);
4071 if (ucore->inlen < required_cmd_sz)
4075 if (ucore->inlen > sizeof(cmd) &&
4076 !ib_is_udata_cleared(ucore, sizeof(cmd),
4077 ucore->inlen - sizeof(cmd)))
4080 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
4084 if (!cmd.attr_mask || cmd.reserved)
4087 if (cmd.attr_mask > IB_CQ_MODERATE)
4090 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
4094 ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
4096 uobj_put_obj_read(cq);
4101 const struct uapi_definition uverbs_def_write_intf[] = {
4102 DECLARE_UVERBS_OBJECT(
4104 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH,
4105 ib_uverbs_create_ah,
4106 UAPI_DEF_METHOD_NEEDS_FN(create_ah)),
4107 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DESTROY_AH,
4108 ib_uverbs_destroy_ah,
4109 UAPI_DEF_METHOD_NEEDS_FN(destroy_ah))),
4111 DECLARE_UVERBS_OBJECT(
4112 UVERBS_OBJECT_COMP_CHANNEL,
4113 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL,
4114 ib_uverbs_create_comp_channel)),
4116 DECLARE_UVERBS_OBJECT(
4118 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ,
4119 ib_uverbs_create_cq,
4120 UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
4121 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DESTROY_CQ,
4122 ib_uverbs_destroy_cq,
4123 UAPI_DEF_METHOD_NEEDS_FN(destroy_cq)),
4124 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_POLL_CQ,
4126 UAPI_DEF_METHOD_NEEDS_FN(poll_cq)),
4127 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ,
4128 ib_uverbs_req_notify_cq,
4129 UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq)),
4130 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ,
4131 ib_uverbs_resize_cq,
4132 UAPI_DEF_METHOD_NEEDS_FN(resize_cq)),
4133 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_CREATE_CQ,
4134 ib_uverbs_ex_create_cq,
4135 UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
4136 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_MODIFY_CQ,
4137 ib_uverbs_ex_modify_cq,
4138 UAPI_DEF_METHOD_NEEDS_FN(create_cq))),
4140 DECLARE_UVERBS_OBJECT(
4141 UVERBS_OBJECT_DEVICE,
4142 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT,
4143 ib_uverbs_get_context),
4144 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_QUERY_DEVICE,
4145 ib_uverbs_query_device),
4146 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_QUERY_PORT,
4147 ib_uverbs_query_port,
4148 UAPI_DEF_METHOD_NEEDS_FN(query_port)),
4149 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_QUERY_DEVICE,
4150 ib_uverbs_ex_query_device,
4151 UAPI_DEF_METHOD_NEEDS_FN(query_device)),
4152 UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext),
4153 UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext)),
4155 DECLARE_UVERBS_OBJECT(
4157 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_CREATE_FLOW,
4158 ib_uverbs_ex_create_flow,
4159 UAPI_DEF_METHOD_NEEDS_FN(create_flow)),
4160 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
4161 ib_uverbs_ex_destroy_flow,
4162 UAPI_DEF_METHOD_NEEDS_FN(destroy_flow))),
4164 DECLARE_UVERBS_OBJECT(
4166 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR,
4168 UAPI_DEF_METHOD_NEEDS_FN(dereg_mr)),
4169 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_REG_MR,
4171 UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr)),
4172 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_REREG_MR,
4174 UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr))),
4176 DECLARE_UVERBS_OBJECT(
4178 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_ALLOC_MW,
4180 UAPI_DEF_METHOD_NEEDS_FN(alloc_mw)),
4181 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEALLOC_MW,
4182 ib_uverbs_dealloc_mw,
4183 UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw))),
4185 DECLARE_UVERBS_OBJECT(
4187 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_ALLOC_PD,
4189 UAPI_DEF_METHOD_NEEDS_FN(alloc_pd)),
4190 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEALLOC_PD,
4191 ib_uverbs_dealloc_pd,
4192 UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd))),
4194 DECLARE_UVERBS_OBJECT(
4196 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_ATTACH_MCAST,
4197 ib_uverbs_attach_mcast,
4198 UAPI_DEF_METHOD_NEEDS_FN(attach_mcast),
4199 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
4200 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP,
4201 ib_uverbs_create_qp,
4202 UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
4203 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DESTROY_QP,
4204 ib_uverbs_destroy_qp,
4205 UAPI_DEF_METHOD_NEEDS_FN(destroy_qp)),
4206 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DETACH_MCAST,
4207 ib_uverbs_detach_mcast,
4208 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
4209 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_MODIFY_QP,
4210 ib_uverbs_modify_qp,
4211 UAPI_DEF_METHOD_NEEDS_FN(modify_qp)),
4212 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_POST_RECV,
4213 ib_uverbs_post_recv,
4214 UAPI_DEF_METHOD_NEEDS_FN(post_recv)),
4215 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_POST_SEND,
4216 ib_uverbs_post_send,
4217 UAPI_DEF_METHOD_NEEDS_FN(post_send)),
4218 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_QUERY_QP,
4220 UAPI_DEF_METHOD_NEEDS_FN(query_qp)),
4221 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_CREATE_QP,
4222 ib_uverbs_ex_create_qp,
4223 UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
4224 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_MODIFY_QP,
4225 ib_uverbs_ex_modify_qp,
4226 UAPI_DEF_METHOD_NEEDS_FN(modify_qp))),
4228 DECLARE_UVERBS_OBJECT(
4229 UVERBS_OBJECT_RWQ_IND_TBL,
4230 DECLARE_UVERBS_WRITE_EX(
4231 IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL,
4232 ib_uverbs_ex_create_rwq_ind_table,
4233 UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table)),
4234 DECLARE_UVERBS_WRITE_EX(
4235 IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL,
4236 ib_uverbs_ex_destroy_rwq_ind_table,
4237 UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table))),
4239 DECLARE_UVERBS_OBJECT(
4241 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_CREATE_WQ,
4242 ib_uverbs_ex_create_wq,
4243 UAPI_DEF_METHOD_NEEDS_FN(create_wq)),
4244 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_DESTROY_WQ,
4245 ib_uverbs_ex_destroy_wq,
4246 UAPI_DEF_METHOD_NEEDS_FN(destroy_wq)),
4247 DECLARE_UVERBS_WRITE_EX(IB_USER_VERBS_EX_CMD_MODIFY_WQ,
4248 ib_uverbs_ex_modify_wq,
4249 UAPI_DEF_METHOD_NEEDS_FN(modify_wq))),
4251 DECLARE_UVERBS_OBJECT(
4253 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ,
4254 ib_uverbs_create_srq,
4255 UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4256 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ,
4257 ib_uverbs_create_xsrq,
4258 UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4259 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DESTROY_SRQ,
4260 ib_uverbs_destroy_srq,
4261 UAPI_DEF_METHOD_NEEDS_FN(destroy_srq)),
4262 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_MODIFY_SRQ,
4263 ib_uverbs_modify_srq,
4264 UAPI_DEF_METHOD_NEEDS_FN(modify_srq)),
4265 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_POST_SRQ_RECV,
4266 ib_uverbs_post_srq_recv,
4267 UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv)),
4268 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_QUERY_SRQ,
4269 ib_uverbs_query_srq,
4270 UAPI_DEF_METHOD_NEEDS_FN(query_srq))),
4272 DECLARE_UVERBS_OBJECT(
4274 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CLOSE_XRCD,
4275 ib_uverbs_close_xrcd,
4276 UAPI_DEF_METHOD_NEEDS_FN(dealloc_xrcd)),
4277 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP,
4279 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD,
4280 ib_uverbs_open_xrcd,
4281 UAPI_DEF_METHOD_NEEDS_FN(alloc_xrcd))),