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, struct ib_uverbs_file *ufile)
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 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
69 const char __user *buf,
70 int in_len, int out_len)
72 struct ib_uverbs_get_context cmd;
73 struct ib_uverbs_get_context_resp resp;
74 struct ib_udata udata;
75 struct ib_ucontext *ucontext;
77 struct ib_rdmacg_object cg_obj;
78 struct ib_device *ib_dev;
81 if (out_len < sizeof resp)
84 if (copy_from_user(&cmd, buf, sizeof cmd))
87 mutex_lock(&file->ucontext_lock);
88 ib_dev = srcu_dereference(file->device->ib_dev,
89 &file->device->disassociate_srcu);
100 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
101 u64_to_user_ptr(cmd.response) + sizeof(resp),
102 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
103 out_len - sizeof(resp));
105 ret = ib_rdmacg_try_charge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
109 ucontext = ib_dev->alloc_ucontext(ib_dev, &udata);
110 if (IS_ERR(ucontext)) {
111 ret = PTR_ERR(ucontext);
115 ucontext->device = ib_dev;
116 ucontext->cg_obj = cg_obj;
117 /* ufile is required when some objects are released */
118 ucontext->ufile = file;
120 ucontext->closing = false;
121 ucontext->cleanup_retryable = false;
123 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
124 mutex_init(&ucontext->per_mm_list_lock);
125 INIT_LIST_HEAD(&ucontext->per_mm_list);
126 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
127 ucontext->invalidate_range = NULL;
131 resp.num_comp_vectors = file->device->num_comp_vectors;
133 ret = get_unused_fd_flags(O_CLOEXEC);
138 filp = ib_uverbs_alloc_async_event_file(file, ib_dev);
144 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
149 fd_install(resp.async_fd, filp);
152 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
153 * only after all writes to setup the ucontext have completed
155 smp_store_release(&file->ucontext, ucontext);
157 mutex_unlock(&file->ucontext_lock);
162 ib_uverbs_free_async_event_file(file);
166 put_unused_fd(resp.async_fd);
169 ib_dev->dealloc_ucontext(ucontext);
172 ib_rdmacg_uncharge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
175 mutex_unlock(&file->ucontext_lock);
179 static void copy_query_dev_fields(struct ib_ucontext *ucontext,
180 struct ib_uverbs_query_device_resp *resp,
181 struct ib_device_attr *attr)
183 struct ib_device *ib_dev = ucontext->device;
185 resp->fw_ver = attr->fw_ver;
186 resp->node_guid = ib_dev->node_guid;
187 resp->sys_image_guid = attr->sys_image_guid;
188 resp->max_mr_size = attr->max_mr_size;
189 resp->page_size_cap = attr->page_size_cap;
190 resp->vendor_id = attr->vendor_id;
191 resp->vendor_part_id = attr->vendor_part_id;
192 resp->hw_ver = attr->hw_ver;
193 resp->max_qp = attr->max_qp;
194 resp->max_qp_wr = attr->max_qp_wr;
195 resp->device_cap_flags = lower_32_bits(attr->device_cap_flags);
196 resp->max_sge = min(attr->max_send_sge, attr->max_recv_sge);
197 resp->max_sge_rd = attr->max_sge_rd;
198 resp->max_cq = attr->max_cq;
199 resp->max_cqe = attr->max_cqe;
200 resp->max_mr = attr->max_mr;
201 resp->max_pd = attr->max_pd;
202 resp->max_qp_rd_atom = attr->max_qp_rd_atom;
203 resp->max_ee_rd_atom = attr->max_ee_rd_atom;
204 resp->max_res_rd_atom = attr->max_res_rd_atom;
205 resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
206 resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
207 resp->atomic_cap = attr->atomic_cap;
208 resp->max_ee = attr->max_ee;
209 resp->max_rdd = attr->max_rdd;
210 resp->max_mw = attr->max_mw;
211 resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
212 resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
213 resp->max_mcast_grp = attr->max_mcast_grp;
214 resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
215 resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
216 resp->max_ah = attr->max_ah;
217 resp->max_fmr = attr->max_fmr;
218 resp->max_map_per_fmr = attr->max_map_per_fmr;
219 resp->max_srq = attr->max_srq;
220 resp->max_srq_wr = attr->max_srq_wr;
221 resp->max_srq_sge = attr->max_srq_sge;
222 resp->max_pkeys = attr->max_pkeys;
223 resp->local_ca_ack_delay = attr->local_ca_ack_delay;
224 resp->phys_port_cnt = ib_dev->phys_port_cnt;
227 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
228 const char __user *buf,
229 int in_len, int out_len)
231 struct ib_uverbs_query_device cmd;
232 struct ib_uverbs_query_device_resp resp;
233 struct ib_ucontext *ucontext;
235 ucontext = ib_uverbs_get_ucontext(file);
236 if (IS_ERR(ucontext))
237 return PTR_ERR(ucontext);
239 if (out_len < sizeof resp)
242 if (copy_from_user(&cmd, buf, sizeof cmd))
245 memset(&resp, 0, sizeof resp);
246 copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
248 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
255 * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the
256 * PortInfo CapabilityMask, but was extended with unique bits.
258 static u32 make_port_cap_flags(const struct ib_port_attr *attr)
262 /* All IBA CapabilityMask bits are passed through here, except bit 26,
263 * which is overridden with IP_BASED_GIDS. This is due to a historical
264 * mistake in the implementation of IP_BASED_GIDS. Otherwise all other
265 * bits match the IBA definition across all kernel versions.
267 res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS;
270 res |= IB_UVERBS_PCF_IP_BASED_GIDS;
275 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
276 const char __user *buf,
277 int in_len, int out_len)
279 struct ib_uverbs_query_port cmd;
280 struct ib_uverbs_query_port_resp resp;
281 struct ib_port_attr attr;
283 struct ib_ucontext *ucontext;
284 struct ib_device *ib_dev;
286 ucontext = ib_uverbs_get_ucontext(file);
287 if (IS_ERR(ucontext))
288 return PTR_ERR(ucontext);
289 ib_dev = ucontext->device;
291 if (out_len < sizeof resp)
294 if (copy_from_user(&cmd, buf, sizeof cmd))
297 ret = ib_query_port(ib_dev, cmd.port_num, &attr);
301 memset(&resp, 0, sizeof resp);
303 resp.state = attr.state;
304 resp.max_mtu = attr.max_mtu;
305 resp.active_mtu = attr.active_mtu;
306 resp.gid_tbl_len = attr.gid_tbl_len;
307 resp.port_cap_flags = make_port_cap_flags(&attr);
308 resp.max_msg_sz = attr.max_msg_sz;
309 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
310 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
311 resp.pkey_tbl_len = attr.pkey_tbl_len;
313 if (rdma_is_grh_required(ib_dev, cmd.port_num))
314 resp.flags |= IB_UVERBS_QPF_GRH_REQUIRED;
316 if (rdma_cap_opa_ah(ib_dev, cmd.port_num)) {
317 resp.lid = OPA_TO_IB_UCAST_LID(attr.lid);
318 resp.sm_lid = OPA_TO_IB_UCAST_LID(attr.sm_lid);
320 resp.lid = ib_lid_cpu16(attr.lid);
321 resp.sm_lid = ib_lid_cpu16(attr.sm_lid);
324 resp.max_vl_num = attr.max_vl_num;
325 resp.sm_sl = attr.sm_sl;
326 resp.subnet_timeout = attr.subnet_timeout;
327 resp.init_type_reply = attr.init_type_reply;
328 resp.active_width = attr.active_width;
329 resp.active_speed = attr.active_speed;
330 resp.phys_state = attr.phys_state;
331 resp.link_layer = rdma_port_get_link_layer(ib_dev,
334 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
340 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
341 const char __user *buf,
342 int in_len, int out_len)
344 struct ib_uverbs_alloc_pd cmd;
345 struct ib_uverbs_alloc_pd_resp resp;
346 struct ib_udata udata;
347 struct ib_uobject *uobj;
350 struct ib_device *ib_dev;
352 if (out_len < sizeof resp)
355 if (copy_from_user(&cmd, buf, sizeof cmd))
358 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
359 u64_to_user_ptr(cmd.response) + sizeof(resp),
360 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
361 out_len - sizeof(resp));
363 uobj = uobj_alloc(UVERBS_OBJECT_PD, file, &ib_dev);
365 return PTR_ERR(uobj);
367 pd = ib_dev->alloc_pd(ib_dev, uobj->context, &udata);
375 pd->__internal_mr = NULL;
376 atomic_set(&pd->usecnt, 0);
379 memset(&resp, 0, sizeof resp);
380 resp.pd_handle = uobj->id;
381 pd->res.type = RDMA_RESTRACK_PD;
382 rdma_restrack_add(&pd->res);
384 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
389 return uobj_alloc_commit(uobj, in_len);
395 uobj_alloc_abort(uobj);
399 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
400 const char __user *buf,
401 int in_len, int out_len)
403 struct ib_uverbs_dealloc_pd cmd;
405 if (copy_from_user(&cmd, buf, sizeof cmd))
408 return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, file,
412 struct xrcd_table_entry {
414 struct ib_xrcd *xrcd;
418 static int xrcd_table_insert(struct ib_uverbs_device *dev,
420 struct ib_xrcd *xrcd)
422 struct xrcd_table_entry *entry, *scan;
423 struct rb_node **p = &dev->xrcd_tree.rb_node;
424 struct rb_node *parent = NULL;
426 entry = kmalloc(sizeof *entry, GFP_KERNEL);
431 entry->inode = inode;
435 scan = rb_entry(parent, struct xrcd_table_entry, node);
437 if (inode < scan->inode) {
439 } else if (inode > scan->inode) {
447 rb_link_node(&entry->node, parent, p);
448 rb_insert_color(&entry->node, &dev->xrcd_tree);
453 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
456 struct xrcd_table_entry *entry;
457 struct rb_node *p = dev->xrcd_tree.rb_node;
460 entry = rb_entry(p, struct xrcd_table_entry, node);
462 if (inode < entry->inode)
464 else if (inode > entry->inode)
473 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
475 struct xrcd_table_entry *entry;
477 entry = xrcd_table_search(dev, inode);
484 static void xrcd_table_delete(struct ib_uverbs_device *dev,
487 struct xrcd_table_entry *entry;
489 entry = xrcd_table_search(dev, inode);
492 rb_erase(&entry->node, &dev->xrcd_tree);
497 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
498 const char __user *buf, int in_len,
501 struct ib_uverbs_open_xrcd cmd;
502 struct ib_uverbs_open_xrcd_resp resp;
503 struct ib_udata udata;
504 struct ib_uxrcd_object *obj;
505 struct ib_xrcd *xrcd = NULL;
506 struct fd f = {NULL, 0};
507 struct inode *inode = NULL;
510 struct ib_device *ib_dev;
512 if (out_len < sizeof resp)
515 if (copy_from_user(&cmd, buf, sizeof cmd))
518 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
519 u64_to_user_ptr(cmd.response) + sizeof(resp),
520 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
521 out_len - sizeof(resp));
523 mutex_lock(&file->device->xrcd_tree_mutex);
526 /* search for file descriptor */
530 goto err_tree_mutex_unlock;
533 inode = file_inode(f.file);
534 xrcd = find_xrcd(file->device, inode);
535 if (!xrcd && !(cmd.oflags & O_CREAT)) {
536 /* no file descriptor. Need CREATE flag */
538 goto err_tree_mutex_unlock;
541 if (xrcd && cmd.oflags & O_EXCL) {
543 goto err_tree_mutex_unlock;
547 obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, file,
551 goto err_tree_mutex_unlock;
555 xrcd = ib_dev->alloc_xrcd(ib_dev, obj->uobject.context, &udata);
562 xrcd->device = ib_dev;
563 atomic_set(&xrcd->usecnt, 0);
564 mutex_init(&xrcd->tgt_qp_mutex);
565 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
569 atomic_set(&obj->refcnt, 0);
570 obj->uobject.object = xrcd;
571 memset(&resp, 0, sizeof resp);
572 resp.xrcd_handle = obj->uobject.id;
576 /* create new inode/xrcd table entry */
577 ret = xrcd_table_insert(file->device, inode, xrcd);
579 goto err_dealloc_xrcd;
581 atomic_inc(&xrcd->usecnt);
584 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
592 mutex_unlock(&file->device->xrcd_tree_mutex);
594 return uobj_alloc_commit(&obj->uobject, in_len);
599 xrcd_table_delete(file->device, inode);
600 atomic_dec(&xrcd->usecnt);
604 ib_dealloc_xrcd(xrcd);
607 uobj_alloc_abort(&obj->uobject);
609 err_tree_mutex_unlock:
613 mutex_unlock(&file->device->xrcd_tree_mutex);
618 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
619 const char __user *buf, int in_len,
622 struct ib_uverbs_close_xrcd cmd;
624 if (copy_from_user(&cmd, buf, sizeof cmd))
627 return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, file,
631 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject,
632 struct ib_xrcd *xrcd,
633 enum rdma_remove_reason why)
637 struct ib_uverbs_device *dev = uobject->context->ufile->device;
640 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
643 ret = ib_dealloc_xrcd(xrcd);
645 if (ib_is_destroy_retryable(ret, why, uobject)) {
646 atomic_inc(&xrcd->usecnt);
651 xrcd_table_delete(dev, inode);
656 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
657 const char __user *buf, int in_len,
660 struct ib_uverbs_reg_mr cmd;
661 struct ib_uverbs_reg_mr_resp resp;
662 struct ib_udata udata;
663 struct ib_uobject *uobj;
667 struct ib_device *ib_dev;
669 if (out_len < sizeof resp)
672 if (copy_from_user(&cmd, buf, sizeof cmd))
675 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
676 u64_to_user_ptr(cmd.response) + sizeof(resp),
677 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
678 out_len - sizeof(resp));
680 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
683 ret = ib_check_mr_access(cmd.access_flags);
687 uobj = uobj_alloc(UVERBS_OBJECT_MR, file, &ib_dev);
689 return PTR_ERR(uobj);
691 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
697 if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
698 if (!(pd->device->attrs.device_cap_flags &
699 IB_DEVICE_ON_DEMAND_PAGING)) {
700 pr_debug("ODP support not available\n");
706 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
707 cmd.access_flags, &udata);
713 mr->device = pd->device;
717 atomic_inc(&pd->usecnt);
718 mr->res.type = RDMA_RESTRACK_MR;
719 rdma_restrack_add(&mr->res);
723 memset(&resp, 0, sizeof resp);
724 resp.lkey = mr->lkey;
725 resp.rkey = mr->rkey;
726 resp.mr_handle = uobj->id;
728 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
733 uobj_put_obj_read(pd);
735 return uobj_alloc_commit(uobj, in_len);
741 uobj_put_obj_read(pd);
744 uobj_alloc_abort(uobj);
748 ssize_t ib_uverbs_rereg_mr(struct ib_uverbs_file *file,
749 const char __user *buf, int in_len,
752 struct ib_uverbs_rereg_mr cmd;
753 struct ib_uverbs_rereg_mr_resp resp;
754 struct ib_udata udata;
755 struct ib_pd *pd = NULL;
757 struct ib_pd *old_pd;
759 struct ib_uobject *uobj;
761 if (out_len < sizeof(resp))
764 if (copy_from_user(&cmd, buf, sizeof(cmd)))
767 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
768 u64_to_user_ptr(cmd.response) + sizeof(resp),
769 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
770 out_len - sizeof(resp));
772 if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
775 if ((cmd.flags & IB_MR_REREG_TRANS) &&
776 (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
777 (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
780 uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, file);
782 return PTR_ERR(uobj);
791 if (cmd.flags & IB_MR_REREG_ACCESS) {
792 ret = ib_check_mr_access(cmd.access_flags);
797 if (cmd.flags & IB_MR_REREG_PD) {
798 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
807 ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start,
808 cmd.length, cmd.hca_va,
809 cmd.access_flags, pd, &udata);
811 if (cmd.flags & IB_MR_REREG_PD) {
812 atomic_inc(&pd->usecnt);
814 atomic_dec(&old_pd->usecnt);
820 memset(&resp, 0, sizeof(resp));
821 resp.lkey = mr->lkey;
822 resp.rkey = mr->rkey;
824 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
830 if (cmd.flags & IB_MR_REREG_PD)
831 uobj_put_obj_read(pd);
834 uobj_put_write(uobj);
839 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
840 const char __user *buf, int in_len,
843 struct ib_uverbs_dereg_mr cmd;
845 if (copy_from_user(&cmd, buf, sizeof cmd))
848 return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, file,
852 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
853 const char __user *buf, int in_len,
856 struct ib_uverbs_alloc_mw cmd;
857 struct ib_uverbs_alloc_mw_resp resp;
858 struct ib_uobject *uobj;
861 struct ib_udata udata;
863 struct ib_device *ib_dev;
865 if (out_len < sizeof(resp))
868 if (copy_from_user(&cmd, buf, sizeof(cmd)))
871 uobj = uobj_alloc(UVERBS_OBJECT_MW, file, &ib_dev);
873 return PTR_ERR(uobj);
875 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
881 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
882 u64_to_user_ptr(cmd.response) + sizeof(resp),
883 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
884 out_len - sizeof(resp));
886 mw = pd->device->alloc_mw(pd, cmd.mw_type, &udata);
892 mw->device = pd->device;
895 atomic_inc(&pd->usecnt);
899 memset(&resp, 0, sizeof(resp));
900 resp.rkey = mw->rkey;
901 resp.mw_handle = uobj->id;
903 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp))) {
908 uobj_put_obj_read(pd);
909 return uobj_alloc_commit(uobj, in_len);
912 uverbs_dealloc_mw(mw);
914 uobj_put_obj_read(pd);
916 uobj_alloc_abort(uobj);
920 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
921 const char __user *buf, int in_len,
924 struct ib_uverbs_dealloc_mw cmd;
926 if (copy_from_user(&cmd, buf, sizeof(cmd)))
929 return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, file,
933 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
934 const char __user *buf, int in_len,
937 struct ib_uverbs_create_comp_channel cmd;
938 struct ib_uverbs_create_comp_channel_resp resp;
939 struct ib_uobject *uobj;
940 struct ib_uverbs_completion_event_file *ev_file;
941 struct ib_device *ib_dev;
943 if (out_len < sizeof resp)
946 if (copy_from_user(&cmd, buf, sizeof cmd))
949 uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, file, &ib_dev);
951 return PTR_ERR(uobj);
955 ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
957 ib_uverbs_init_event_queue(&ev_file->ev_queue);
959 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
960 uobj_alloc_abort(uobj);
964 return uobj_alloc_commit(uobj, in_len);
967 static struct ib_ucq_object *create_cq(struct ib_uverbs_file *file,
968 struct ib_udata *ucore,
969 struct ib_udata *uhw,
970 struct ib_uverbs_ex_create_cq *cmd,
972 int (*cb)(struct ib_uverbs_file *file,
973 struct ib_ucq_object *obj,
974 struct ib_uverbs_ex_create_cq_resp *resp,
975 struct ib_udata *udata,
979 struct ib_ucq_object *obj;
980 struct ib_uverbs_completion_event_file *ev_file = NULL;
983 struct ib_uverbs_ex_create_cq_resp resp;
984 struct ib_cq_init_attr attr = {};
985 struct ib_device *ib_dev;
987 if (cmd->comp_vector >= file->device->num_comp_vectors)
988 return ERR_PTR(-EINVAL);
990 obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, file,
995 if (!ib_dev->create_cq) {
1000 if (cmd->comp_channel >= 0) {
1001 ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, file);
1002 if (IS_ERR(ev_file)) {
1003 ret = PTR_ERR(ev_file);
1008 obj->uobject.user_handle = cmd->user_handle;
1009 obj->comp_events_reported = 0;
1010 obj->async_events_reported = 0;
1011 INIT_LIST_HEAD(&obj->comp_list);
1012 INIT_LIST_HEAD(&obj->async_list);
1014 attr.cqe = cmd->cqe;
1015 attr.comp_vector = cmd->comp_vector;
1017 if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
1018 attr.flags = cmd->flags;
1020 cq = ib_dev->create_cq(ib_dev, &attr, obj->uobject.context, uhw);
1026 cq->device = ib_dev;
1027 cq->uobject = &obj->uobject;
1028 cq->comp_handler = ib_uverbs_comp_handler;
1029 cq->event_handler = ib_uverbs_cq_event_handler;
1030 cq->cq_context = ev_file ? &ev_file->ev_queue : NULL;
1031 atomic_set(&cq->usecnt, 0);
1033 obj->uobject.object = cq;
1034 memset(&resp, 0, sizeof resp);
1035 resp.base.cq_handle = obj->uobject.id;
1036 resp.base.cqe = cq->cqe;
1038 resp.response_length = offsetof(typeof(resp), response_length) +
1039 sizeof(resp.response_length);
1041 cq->res.type = RDMA_RESTRACK_CQ;
1042 rdma_restrack_add(&cq->res);
1044 ret = cb(file, obj, &resp, ucore, context);
1048 ret = uobj_alloc_commit(&obj->uobject, 0);
1050 return ERR_PTR(ret);
1058 ib_uverbs_release_ucq(file, ev_file, obj);
1061 uobj_alloc_abort(&obj->uobject);
1063 return ERR_PTR(ret);
1066 static int ib_uverbs_create_cq_cb(struct ib_uverbs_file *file,
1067 struct ib_ucq_object *obj,
1068 struct ib_uverbs_ex_create_cq_resp *resp,
1069 struct ib_udata *ucore, void *context)
1071 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1077 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1078 const char __user *buf, int in_len,
1081 struct ib_uverbs_create_cq cmd;
1082 struct ib_uverbs_ex_create_cq cmd_ex;
1083 struct ib_uverbs_create_cq_resp resp;
1084 struct ib_udata ucore;
1085 struct ib_udata uhw;
1086 struct ib_ucq_object *obj;
1088 if (out_len < sizeof(resp))
1091 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1094 ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
1095 sizeof(cmd), sizeof(resp));
1097 ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
1098 u64_to_user_ptr(cmd.response) + sizeof(resp),
1099 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1100 out_len - sizeof(resp));
1102 memset(&cmd_ex, 0, sizeof(cmd_ex));
1103 cmd_ex.user_handle = cmd.user_handle;
1104 cmd_ex.cqe = cmd.cqe;
1105 cmd_ex.comp_vector = cmd.comp_vector;
1106 cmd_ex.comp_channel = cmd.comp_channel;
1108 obj = create_cq(file, &ucore, &uhw, &cmd_ex,
1109 offsetof(typeof(cmd_ex), comp_channel) +
1110 sizeof(cmd.comp_channel), ib_uverbs_create_cq_cb,
1114 return PTR_ERR(obj);
1119 static int ib_uverbs_ex_create_cq_cb(struct ib_uverbs_file *file,
1120 struct ib_ucq_object *obj,
1121 struct ib_uverbs_ex_create_cq_resp *resp,
1122 struct ib_udata *ucore, void *context)
1124 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1130 int ib_uverbs_ex_create_cq(struct ib_uverbs_file *file,
1131 struct ib_udata *ucore,
1132 struct ib_udata *uhw)
1134 struct ib_uverbs_ex_create_cq_resp resp;
1135 struct ib_uverbs_ex_create_cq cmd;
1136 struct ib_ucq_object *obj;
1139 if (ucore->inlen < sizeof(cmd))
1142 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
1152 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1153 sizeof(resp.response_length)))
1156 obj = create_cq(file, ucore, uhw, &cmd,
1157 min(ucore->inlen, sizeof(cmd)),
1158 ib_uverbs_ex_create_cq_cb, NULL);
1160 return PTR_ERR_OR_ZERO(obj);
1163 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1164 const char __user *buf, int in_len,
1167 struct ib_uverbs_resize_cq cmd;
1168 struct ib_uverbs_resize_cq_resp resp = {};
1169 struct ib_udata udata;
1173 if (copy_from_user(&cmd, buf, sizeof cmd))
1176 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
1177 u64_to_user_ptr(cmd.response) + sizeof(resp),
1178 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1179 out_len - sizeof(resp));
1181 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
1185 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1191 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp.cqe))
1195 uobj_put_obj_read(cq);
1197 return ret ? ret : in_len;
1200 static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
1203 struct ib_uverbs_wc tmp;
1205 tmp.wr_id = wc->wr_id;
1206 tmp.status = wc->status;
1207 tmp.opcode = wc->opcode;
1208 tmp.vendor_err = wc->vendor_err;
1209 tmp.byte_len = wc->byte_len;
1210 tmp.ex.imm_data = wc->ex.imm_data;
1211 tmp.qp_num = wc->qp->qp_num;
1212 tmp.src_qp = wc->src_qp;
1213 tmp.wc_flags = wc->wc_flags;
1214 tmp.pkey_index = wc->pkey_index;
1215 if (rdma_cap_opa_ah(ib_dev, wc->port_num))
1216 tmp.slid = OPA_TO_IB_UCAST_LID(wc->slid);
1218 tmp.slid = ib_lid_cpu16(wc->slid);
1220 tmp.dlid_path_bits = wc->dlid_path_bits;
1221 tmp.port_num = wc->port_num;
1224 if (copy_to_user(dest, &tmp, sizeof tmp))
1230 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1231 const char __user *buf, int in_len,
1234 struct ib_uverbs_poll_cq cmd;
1235 struct ib_uverbs_poll_cq_resp resp;
1236 u8 __user *header_ptr;
1237 u8 __user *data_ptr;
1242 if (copy_from_user(&cmd, buf, sizeof cmd))
1245 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
1249 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1250 header_ptr = u64_to_user_ptr(cmd.response);
1251 data_ptr = header_ptr + sizeof resp;
1253 memset(&resp, 0, sizeof resp);
1254 while (resp.count < cmd.ne) {
1255 ret = ib_poll_cq(cq, 1, &wc);
1261 ret = copy_wc_to_user(cq->device, data_ptr, &wc);
1265 data_ptr += sizeof(struct ib_uverbs_wc);
1269 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1277 uobj_put_obj_read(cq);
1281 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1282 const char __user *buf, int in_len,
1285 struct ib_uverbs_req_notify_cq cmd;
1288 if (copy_from_user(&cmd, buf, sizeof cmd))
1291 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
1295 ib_req_notify_cq(cq, cmd.solicited_only ?
1296 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1298 uobj_put_obj_read(cq);
1303 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1304 const char __user *buf, int in_len,
1307 struct ib_uverbs_destroy_cq cmd;
1308 struct ib_uverbs_destroy_cq_resp resp;
1309 struct ib_uobject *uobj;
1310 struct ib_ucq_object *obj;
1312 if (copy_from_user(&cmd, buf, sizeof cmd))
1315 uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, file);
1317 return PTR_ERR(uobj);
1319 obj = container_of(uobj, struct ib_ucq_object, uobject);
1320 memset(&resp, 0, sizeof(resp));
1321 resp.comp_events_reported = obj->comp_events_reported;
1322 resp.async_events_reported = obj->async_events_reported;
1324 uobj_put_destroy(uobj);
1326 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
1332 static int create_qp(struct ib_uverbs_file *file,
1333 struct ib_udata *ucore,
1334 struct ib_udata *uhw,
1335 struct ib_uverbs_ex_create_qp *cmd,
1337 int (*cb)(struct ib_uverbs_file *file,
1338 struct ib_uverbs_ex_create_qp_resp *resp,
1339 struct ib_udata *udata),
1342 struct ib_uqp_object *obj;
1343 struct ib_device *device;
1344 struct ib_pd *pd = NULL;
1345 struct ib_xrcd *xrcd = NULL;
1346 struct ib_uobject *xrcd_uobj = ERR_PTR(-ENOENT);
1347 struct ib_cq *scq = NULL, *rcq = NULL;
1348 struct ib_srq *srq = NULL;
1351 struct ib_qp_init_attr attr = {};
1352 struct ib_uverbs_ex_create_qp_resp resp;
1354 struct ib_rwq_ind_table *ind_tbl = NULL;
1356 struct ib_device *ib_dev;
1358 if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1361 obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, file,
1364 return PTR_ERR(obj);
1366 obj->uevent.uobject.user_handle = cmd->user_handle;
1367 mutex_init(&obj->mcast_lock);
1369 if (cmd_sz >= offsetof(typeof(*cmd), rwq_ind_tbl_handle) +
1370 sizeof(cmd->rwq_ind_tbl_handle) &&
1371 (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE)) {
1372 ind_tbl = uobj_get_obj_read(rwq_ind_table,
1373 UVERBS_OBJECT_RWQ_IND_TBL,
1374 cmd->rwq_ind_tbl_handle, file);
1380 attr.rwq_ind_tbl = ind_tbl;
1383 if (cmd_sz > sizeof(*cmd) &&
1384 !ib_is_udata_cleared(ucore, sizeof(*cmd),
1385 cmd_sz - sizeof(*cmd))) {
1390 if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
1395 if (ind_tbl && !cmd->max_send_wr)
1398 if (cmd->qp_type == IB_QPT_XRC_TGT) {
1399 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
1402 if (IS_ERR(xrcd_uobj)) {
1407 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1412 device = xrcd->device;
1414 if (cmd->qp_type == IB_QPT_XRC_INI) {
1415 cmd->max_recv_wr = 0;
1416 cmd->max_recv_sge = 0;
1419 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
1420 cmd->srq_handle, file);
1421 if (!srq || srq->srq_type == IB_SRQT_XRC) {
1428 if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1429 rcq = uobj_get_obj_read(
1430 cq, UVERBS_OBJECT_CQ,
1431 cmd->recv_cq_handle, file);
1441 scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
1442 cmd->send_cq_handle, file);
1445 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
1447 if (!pd || (!scq && has_sq)) {
1452 device = pd->device;
1455 attr.event_handler = ib_uverbs_qp_event_handler;
1456 attr.qp_context = file;
1461 attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1463 attr.qp_type = cmd->qp_type;
1464 attr.create_flags = 0;
1466 attr.cap.max_send_wr = cmd->max_send_wr;
1467 attr.cap.max_recv_wr = cmd->max_recv_wr;
1468 attr.cap.max_send_sge = cmd->max_send_sge;
1469 attr.cap.max_recv_sge = cmd->max_recv_sge;
1470 attr.cap.max_inline_data = cmd->max_inline_data;
1472 obj->uevent.events_reported = 0;
1473 INIT_LIST_HEAD(&obj->uevent.event_list);
1474 INIT_LIST_HEAD(&obj->mcast_list);
1476 if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
1477 sizeof(cmd->create_flags))
1478 attr.create_flags = cmd->create_flags;
1480 if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1481 IB_QP_CREATE_CROSS_CHANNEL |
1482 IB_QP_CREATE_MANAGED_SEND |
1483 IB_QP_CREATE_MANAGED_RECV |
1484 IB_QP_CREATE_SCATTER_FCS |
1485 IB_QP_CREATE_CVLAN_STRIPPING |
1486 IB_QP_CREATE_SOURCE_QPN |
1487 IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
1492 if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
1493 if (!capable(CAP_NET_RAW)) {
1498 attr.source_qpn = cmd->source_qpn;
1501 buf = (void *)cmd + sizeof(*cmd);
1502 if (cmd_sz > sizeof(*cmd))
1503 if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
1504 cmd_sz - sizeof(*cmd) - 1))) {
1509 if (cmd->qp_type == IB_QPT_XRC_TGT)
1510 qp = ib_create_qp(pd, &attr);
1512 qp = _ib_create_qp(device, pd, &attr, uhw,
1513 &obj->uevent.uobject);
1520 if (cmd->qp_type != IB_QPT_XRC_TGT) {
1521 ret = ib_create_qp_security(qp, device);
1527 qp->send_cq = attr.send_cq;
1528 qp->recv_cq = attr.recv_cq;
1530 qp->rwq_ind_tbl = ind_tbl;
1531 qp->event_handler = attr.event_handler;
1532 qp->qp_context = attr.qp_context;
1533 qp->qp_type = attr.qp_type;
1534 atomic_set(&qp->usecnt, 0);
1535 atomic_inc(&pd->usecnt);
1538 atomic_inc(&attr.send_cq->usecnt);
1540 atomic_inc(&attr.recv_cq->usecnt);
1542 atomic_inc(&attr.srq->usecnt);
1544 atomic_inc(&ind_tbl->usecnt);
1546 /* It is done in _ib_create_qp for other QP types */
1547 qp->uobject = &obj->uevent.uobject;
1550 obj->uevent.uobject.object = qp;
1552 memset(&resp, 0, sizeof resp);
1553 resp.base.qpn = qp->qp_num;
1554 resp.base.qp_handle = obj->uevent.uobject.id;
1555 resp.base.max_recv_sge = attr.cap.max_recv_sge;
1556 resp.base.max_send_sge = attr.cap.max_send_sge;
1557 resp.base.max_recv_wr = attr.cap.max_recv_wr;
1558 resp.base.max_send_wr = attr.cap.max_send_wr;
1559 resp.base.max_inline_data = attr.cap.max_inline_data;
1561 resp.response_length = offsetof(typeof(resp), response_length) +
1562 sizeof(resp.response_length);
1564 ret = cb(file, &resp, ucore);
1569 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1571 atomic_inc(&obj->uxrcd->refcnt);
1572 uobj_put_read(xrcd_uobj);
1576 uobj_put_obj_read(pd);
1578 uobj_put_obj_read(scq);
1579 if (rcq && rcq != scq)
1580 uobj_put_obj_read(rcq);
1582 uobj_put_obj_read(srq);
1584 uobj_put_obj_read(ind_tbl);
1586 return uobj_alloc_commit(&obj->uevent.uobject, 0);
1591 if (!IS_ERR(xrcd_uobj))
1592 uobj_put_read(xrcd_uobj);
1594 uobj_put_obj_read(pd);
1596 uobj_put_obj_read(scq);
1597 if (rcq && rcq != scq)
1598 uobj_put_obj_read(rcq);
1600 uobj_put_obj_read(srq);
1602 uobj_put_obj_read(ind_tbl);
1604 uobj_alloc_abort(&obj->uevent.uobject);
1608 static int ib_uverbs_create_qp_cb(struct ib_uverbs_file *file,
1609 struct ib_uverbs_ex_create_qp_resp *resp,
1610 struct ib_udata *ucore)
1612 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1618 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1619 const char __user *buf, int in_len,
1622 struct ib_uverbs_create_qp cmd;
1623 struct ib_uverbs_ex_create_qp cmd_ex;
1624 struct ib_udata ucore;
1625 struct ib_udata uhw;
1626 ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
1629 if (out_len < resp_size)
1632 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1635 ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
1636 sizeof(cmd), resp_size);
1637 ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
1638 u64_to_user_ptr(cmd.response) + resp_size,
1639 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1640 out_len - resp_size);
1642 memset(&cmd_ex, 0, sizeof(cmd_ex));
1643 cmd_ex.user_handle = cmd.user_handle;
1644 cmd_ex.pd_handle = cmd.pd_handle;
1645 cmd_ex.send_cq_handle = cmd.send_cq_handle;
1646 cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1647 cmd_ex.srq_handle = cmd.srq_handle;
1648 cmd_ex.max_send_wr = cmd.max_send_wr;
1649 cmd_ex.max_recv_wr = cmd.max_recv_wr;
1650 cmd_ex.max_send_sge = cmd.max_send_sge;
1651 cmd_ex.max_recv_sge = cmd.max_recv_sge;
1652 cmd_ex.max_inline_data = cmd.max_inline_data;
1653 cmd_ex.sq_sig_all = cmd.sq_sig_all;
1654 cmd_ex.qp_type = cmd.qp_type;
1655 cmd_ex.is_srq = cmd.is_srq;
1657 err = create_qp(file, &ucore, &uhw, &cmd_ex,
1658 offsetof(typeof(cmd_ex), is_srq) +
1659 sizeof(cmd.is_srq), ib_uverbs_create_qp_cb,
1668 static int ib_uverbs_ex_create_qp_cb(struct ib_uverbs_file *file,
1669 struct ib_uverbs_ex_create_qp_resp *resp,
1670 struct ib_udata *ucore)
1672 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1678 int ib_uverbs_ex_create_qp(struct ib_uverbs_file *file,
1679 struct ib_udata *ucore,
1680 struct ib_udata *uhw)
1682 struct ib_uverbs_ex_create_qp_resp resp;
1683 struct ib_uverbs_ex_create_qp cmd = {0};
1686 if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
1687 sizeof(cmd.comp_mask)))
1690 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
1694 if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
1700 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1701 sizeof(resp.response_length)))
1704 err = create_qp(file, ucore, uhw, &cmd,
1705 min(ucore->inlen, sizeof(cmd)),
1706 ib_uverbs_ex_create_qp_cb, NULL);
1714 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1715 const char __user *buf, int in_len, int out_len)
1717 struct ib_uverbs_open_qp cmd;
1718 struct ib_uverbs_create_qp_resp resp;
1719 struct ib_udata udata;
1720 struct ib_uqp_object *obj;
1721 struct ib_xrcd *xrcd;
1722 struct ib_uobject *uninitialized_var(xrcd_uobj);
1724 struct ib_qp_open_attr attr;
1726 struct ib_device *ib_dev;
1728 if (out_len < sizeof resp)
1731 if (copy_from_user(&cmd, buf, sizeof cmd))
1734 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
1735 u64_to_user_ptr(cmd.response) + sizeof(resp),
1736 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1737 out_len - sizeof(resp));
1739 obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, file,
1742 return PTR_ERR(obj);
1744 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, file);
1745 if (IS_ERR(xrcd_uobj)) {
1750 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1756 attr.event_handler = ib_uverbs_qp_event_handler;
1757 attr.qp_context = file;
1758 attr.qp_num = cmd.qpn;
1759 attr.qp_type = cmd.qp_type;
1761 obj->uevent.events_reported = 0;
1762 INIT_LIST_HEAD(&obj->uevent.event_list);
1763 INIT_LIST_HEAD(&obj->mcast_list);
1765 qp = ib_open_qp(xrcd, &attr);
1771 obj->uevent.uobject.object = qp;
1772 obj->uevent.uobject.user_handle = cmd.user_handle;
1774 memset(&resp, 0, sizeof resp);
1775 resp.qpn = qp->qp_num;
1776 resp.qp_handle = obj->uevent.uobject.id;
1778 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
1783 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1784 atomic_inc(&obj->uxrcd->refcnt);
1785 qp->uobject = &obj->uevent.uobject;
1786 uobj_put_read(xrcd_uobj);
1788 return uobj_alloc_commit(&obj->uevent.uobject, in_len);
1793 uobj_put_read(xrcd_uobj);
1795 uobj_alloc_abort(&obj->uevent.uobject);
1799 static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
1800 struct rdma_ah_attr *rdma_attr)
1802 const struct ib_global_route *grh;
1804 uverb_attr->dlid = rdma_ah_get_dlid(rdma_attr);
1805 uverb_attr->sl = rdma_ah_get_sl(rdma_attr);
1806 uverb_attr->src_path_bits = rdma_ah_get_path_bits(rdma_attr);
1807 uverb_attr->static_rate = rdma_ah_get_static_rate(rdma_attr);
1808 uverb_attr->is_global = !!(rdma_ah_get_ah_flags(rdma_attr) &
1810 if (uverb_attr->is_global) {
1811 grh = rdma_ah_read_grh(rdma_attr);
1812 memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
1813 uverb_attr->flow_label = grh->flow_label;
1814 uverb_attr->sgid_index = grh->sgid_index;
1815 uverb_attr->hop_limit = grh->hop_limit;
1816 uverb_attr->traffic_class = grh->traffic_class;
1818 uverb_attr->port_num = rdma_ah_get_port_num(rdma_attr);
1821 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1822 const char __user *buf, int in_len,
1825 struct ib_uverbs_query_qp cmd;
1826 struct ib_uverbs_query_qp_resp resp;
1828 struct ib_qp_attr *attr;
1829 struct ib_qp_init_attr *init_attr;
1832 if (copy_from_user(&cmd, buf, sizeof cmd))
1835 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1836 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1837 if (!attr || !init_attr) {
1842 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
1848 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1850 uobj_put_obj_read(qp);
1855 memset(&resp, 0, sizeof resp);
1857 resp.qp_state = attr->qp_state;
1858 resp.cur_qp_state = attr->cur_qp_state;
1859 resp.path_mtu = attr->path_mtu;
1860 resp.path_mig_state = attr->path_mig_state;
1861 resp.qkey = attr->qkey;
1862 resp.rq_psn = attr->rq_psn;
1863 resp.sq_psn = attr->sq_psn;
1864 resp.dest_qp_num = attr->dest_qp_num;
1865 resp.qp_access_flags = attr->qp_access_flags;
1866 resp.pkey_index = attr->pkey_index;
1867 resp.alt_pkey_index = attr->alt_pkey_index;
1868 resp.sq_draining = attr->sq_draining;
1869 resp.max_rd_atomic = attr->max_rd_atomic;
1870 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1871 resp.min_rnr_timer = attr->min_rnr_timer;
1872 resp.port_num = attr->port_num;
1873 resp.timeout = attr->timeout;
1874 resp.retry_cnt = attr->retry_cnt;
1875 resp.rnr_retry = attr->rnr_retry;
1876 resp.alt_port_num = attr->alt_port_num;
1877 resp.alt_timeout = attr->alt_timeout;
1879 copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
1880 copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
1882 resp.max_send_wr = init_attr->cap.max_send_wr;
1883 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1884 resp.max_send_sge = init_attr->cap.max_send_sge;
1885 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1886 resp.max_inline_data = init_attr->cap.max_inline_data;
1887 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1889 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
1896 return ret ? ret : in_len;
1899 /* Remove ignored fields set in the attribute mask */
1900 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1903 case IB_QPT_XRC_INI:
1904 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1905 case IB_QPT_XRC_TGT:
1906 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1913 static void copy_ah_attr_from_uverbs(struct ib_device *dev,
1914 struct rdma_ah_attr *rdma_attr,
1915 struct ib_uverbs_qp_dest *uverb_attr)
1917 rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
1918 if (uverb_attr->is_global) {
1919 rdma_ah_set_grh(rdma_attr, NULL,
1920 uverb_attr->flow_label,
1921 uverb_attr->sgid_index,
1922 uverb_attr->hop_limit,
1923 uverb_attr->traffic_class);
1924 rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
1926 rdma_ah_set_ah_flags(rdma_attr, 0);
1928 rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
1929 rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
1930 rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
1931 rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
1932 rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
1933 rdma_ah_set_make_grd(rdma_attr, false);
1936 static int modify_qp(struct ib_uverbs_file *file,
1937 struct ib_uverbs_ex_modify_qp *cmd, struct ib_udata *udata)
1939 struct ib_qp_attr *attr;
1943 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1947 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle, file);
1953 if ((cmd->base.attr_mask & IB_QP_PORT) &&
1954 !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
1959 if ((cmd->base.attr_mask & IB_QP_AV)) {
1960 if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
1965 if (cmd->base.attr_mask & IB_QP_STATE &&
1966 cmd->base.qp_state == IB_QPS_RTR) {
1967 /* We are in INIT->RTR TRANSITION (if we are not,
1968 * this transition will be rejected in subsequent checks).
1969 * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1970 * but the IB_QP_STATE flag is required.
1972 * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1973 * when IB_QP_AV is set, has required inclusion of a valid
1974 * port number in the primary AV. (AVs are created and handled
1975 * differently for infiniband and ethernet (RoCE) ports).
1977 * Check the port number included in the primary AV against
1978 * the port number in the qp struct, which was set (and saved)
1979 * in the RST->INIT transition.
1981 if (cmd->base.dest.port_num != qp->real_qp->port) {
1986 /* We are in SQD->SQD. (If we are not, this transition will
1987 * be rejected later in the verbs layer checks).
1988 * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1989 * together in the SQD->SQD transition.
1991 * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1992 * verbs layer driver does not track primary port changes
1993 * resulting from path migration. Thus, in SQD, if the primary
1994 * AV is modified, the primary port should also be modified).
1996 * Note that in this transition, the IB_QP_STATE flag
1999 if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
2000 == (IB_QP_AV | IB_QP_PORT)) &&
2001 cmd->base.port_num != cmd->base.dest.port_num) {
2005 if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
2007 cmd->base.attr_mask |= IB_QP_PORT;
2008 cmd->base.port_num = cmd->base.dest.port_num;
2013 if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
2014 (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
2015 !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
2016 cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
2021 if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
2022 cmd->base.cur_qp_state > IB_QPS_ERR) ||
2023 (cmd->base.attr_mask & IB_QP_STATE &&
2024 cmd->base.qp_state > IB_QPS_ERR)) {
2029 if (cmd->base.attr_mask & IB_QP_STATE)
2030 attr->qp_state = cmd->base.qp_state;
2031 if (cmd->base.attr_mask & IB_QP_CUR_STATE)
2032 attr->cur_qp_state = cmd->base.cur_qp_state;
2033 if (cmd->base.attr_mask & IB_QP_PATH_MTU)
2034 attr->path_mtu = cmd->base.path_mtu;
2035 if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
2036 attr->path_mig_state = cmd->base.path_mig_state;
2037 if (cmd->base.attr_mask & IB_QP_QKEY)
2038 attr->qkey = cmd->base.qkey;
2039 if (cmd->base.attr_mask & IB_QP_RQ_PSN)
2040 attr->rq_psn = cmd->base.rq_psn;
2041 if (cmd->base.attr_mask & IB_QP_SQ_PSN)
2042 attr->sq_psn = cmd->base.sq_psn;
2043 if (cmd->base.attr_mask & IB_QP_DEST_QPN)
2044 attr->dest_qp_num = cmd->base.dest_qp_num;
2045 if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
2046 attr->qp_access_flags = cmd->base.qp_access_flags;
2047 if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
2048 attr->pkey_index = cmd->base.pkey_index;
2049 if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
2050 attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
2051 if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
2052 attr->max_rd_atomic = cmd->base.max_rd_atomic;
2053 if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2054 attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
2055 if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
2056 attr->min_rnr_timer = cmd->base.min_rnr_timer;
2057 if (cmd->base.attr_mask & IB_QP_PORT)
2058 attr->port_num = cmd->base.port_num;
2059 if (cmd->base.attr_mask & IB_QP_TIMEOUT)
2060 attr->timeout = cmd->base.timeout;
2061 if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
2062 attr->retry_cnt = cmd->base.retry_cnt;
2063 if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
2064 attr->rnr_retry = cmd->base.rnr_retry;
2065 if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
2066 attr->alt_port_num = cmd->base.alt_port_num;
2067 attr->alt_timeout = cmd->base.alt_timeout;
2068 attr->alt_pkey_index = cmd->base.alt_pkey_index;
2070 if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
2071 attr->rate_limit = cmd->rate_limit;
2073 if (cmd->base.attr_mask & IB_QP_AV)
2074 copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
2077 if (cmd->base.attr_mask & IB_QP_ALT_PATH)
2078 copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
2079 &cmd->base.alt_dest);
2081 ret = ib_modify_qp_with_udata(qp, attr,
2082 modify_qp_mask(qp->qp_type,
2083 cmd->base.attr_mask),
2087 uobj_put_obj_read(qp);
2094 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
2095 const char __user *buf, int in_len,
2098 struct ib_uverbs_ex_modify_qp cmd = {};
2099 struct ib_udata udata;
2102 if (copy_from_user(&cmd.base, buf, sizeof(cmd.base)))
2105 if (cmd.base.attr_mask &
2106 ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
2109 ib_uverbs_init_udata(&udata, buf + sizeof(cmd.base), NULL,
2110 in_len - sizeof(cmd.base) - sizeof(struct ib_uverbs_cmd_hdr),
2113 ret = modify_qp(file, &cmd, &udata);
2120 int ib_uverbs_ex_modify_qp(struct ib_uverbs_file *file,
2121 struct ib_udata *ucore,
2122 struct ib_udata *uhw)
2124 struct ib_uverbs_ex_modify_qp cmd = {};
2128 * Last bit is reserved for extending the attr_mask by
2129 * using another field.
2131 BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
2133 if (ucore->inlen < sizeof(cmd.base))
2136 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
2140 if (cmd.base.attr_mask &
2141 ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
2144 if (ucore->inlen > sizeof(cmd)) {
2145 if (!ib_is_udata_cleared(ucore, sizeof(cmd),
2146 ucore->inlen - sizeof(cmd)))
2150 ret = modify_qp(file, &cmd, uhw);
2155 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
2156 const char __user *buf, int in_len,
2159 struct ib_uverbs_destroy_qp cmd;
2160 struct ib_uverbs_destroy_qp_resp resp;
2161 struct ib_uobject *uobj;
2162 struct ib_uqp_object *obj;
2164 if (copy_from_user(&cmd, buf, sizeof cmd))
2167 uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, file);
2169 return PTR_ERR(uobj);
2171 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2172 memset(&resp, 0, sizeof(resp));
2173 resp.events_reported = obj->uevent.events_reported;
2175 uobj_put_destroy(uobj);
2177 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2183 static void *alloc_wr(size_t wr_size, __u32 num_sge)
2185 if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
2186 sizeof (struct ib_sge))
2189 return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
2190 num_sge * sizeof (struct ib_sge), GFP_KERNEL);
2193 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2194 const char __user *buf, int in_len,
2197 struct ib_uverbs_post_send cmd;
2198 struct ib_uverbs_post_send_resp resp;
2199 struct ib_uverbs_send_wr *user_wr;
2200 struct ib_send_wr *wr = NULL, *last, *next;
2201 const struct ib_send_wr *bad_wr;
2205 ssize_t ret = -EINVAL;
2208 if (copy_from_user(&cmd, buf, sizeof cmd))
2211 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2212 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2215 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2218 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2222 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
2226 is_ud = qp->qp_type == IB_QPT_UD;
2229 for (i = 0; i < cmd.wr_count; ++i) {
2230 if (copy_from_user(user_wr,
2231 buf + sizeof cmd + i * cmd.wqe_size,
2237 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2243 struct ib_ud_wr *ud;
2245 if (user_wr->opcode != IB_WR_SEND &&
2246 user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2251 next_size = sizeof(*ud);
2252 ud = alloc_wr(next_size, user_wr->num_sge);
2258 ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2259 user_wr->wr.ud.ah, file);
2265 ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2266 ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2269 } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2270 user_wr->opcode == IB_WR_RDMA_WRITE ||
2271 user_wr->opcode == IB_WR_RDMA_READ) {
2272 struct ib_rdma_wr *rdma;
2274 next_size = sizeof(*rdma);
2275 rdma = alloc_wr(next_size, user_wr->num_sge);
2281 rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2282 rdma->rkey = user_wr->wr.rdma.rkey;
2285 } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2286 user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2287 struct ib_atomic_wr *atomic;
2289 next_size = sizeof(*atomic);
2290 atomic = alloc_wr(next_size, user_wr->num_sge);
2296 atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2297 atomic->compare_add = user_wr->wr.atomic.compare_add;
2298 atomic->swap = user_wr->wr.atomic.swap;
2299 atomic->rkey = user_wr->wr.atomic.rkey;
2302 } else if (user_wr->opcode == IB_WR_SEND ||
2303 user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2304 user_wr->opcode == IB_WR_SEND_WITH_INV) {
2305 next_size = sizeof(*next);
2306 next = alloc_wr(next_size, user_wr->num_sge);
2316 if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2317 user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2319 (__be32 __force) user_wr->ex.imm_data;
2320 } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2321 next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2331 next->wr_id = user_wr->wr_id;
2332 next->num_sge = user_wr->num_sge;
2333 next->opcode = user_wr->opcode;
2334 next->send_flags = user_wr->send_flags;
2336 if (next->num_sge) {
2337 next->sg_list = (void *) next +
2338 ALIGN(next_size, sizeof(struct ib_sge));
2339 if (copy_from_user(next->sg_list,
2341 cmd.wr_count * cmd.wqe_size +
2342 sg_ind * sizeof (struct ib_sge),
2343 next->num_sge * sizeof (struct ib_sge))) {
2347 sg_ind += next->num_sge;
2349 next->sg_list = NULL;
2353 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2355 for (next = wr; next; next = next->next) {
2361 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2365 uobj_put_obj_read(qp);
2368 if (is_ud && ud_wr(wr)->ah)
2369 uobj_put_obj_read(ud_wr(wr)->ah);
2378 return ret ? ret : in_len;
2381 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2387 struct ib_uverbs_recv_wr *user_wr;
2388 struct ib_recv_wr *wr = NULL, *last, *next;
2393 if (in_len < wqe_size * wr_count +
2394 sge_count * sizeof (struct ib_uverbs_sge))
2395 return ERR_PTR(-EINVAL);
2397 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2398 return ERR_PTR(-EINVAL);
2400 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2402 return ERR_PTR(-ENOMEM);
2406 for (i = 0; i < wr_count; ++i) {
2407 if (copy_from_user(user_wr, buf + i * wqe_size,
2413 if (user_wr->num_sge + sg_ind > sge_count) {
2418 if (user_wr->num_sge >=
2419 (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
2420 sizeof (struct ib_sge)) {
2425 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2426 user_wr->num_sge * sizeof (struct ib_sge),
2440 next->wr_id = user_wr->wr_id;
2441 next->num_sge = user_wr->num_sge;
2443 if (next->num_sge) {
2444 next->sg_list = (void *) next +
2445 ALIGN(sizeof *next, sizeof (struct ib_sge));
2446 if (copy_from_user(next->sg_list,
2447 buf + wr_count * wqe_size +
2448 sg_ind * sizeof (struct ib_sge),
2449 next->num_sge * sizeof (struct ib_sge))) {
2453 sg_ind += next->num_sge;
2455 next->sg_list = NULL;
2470 return ERR_PTR(ret);
2473 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2474 const char __user *buf, int in_len,
2477 struct ib_uverbs_post_recv cmd;
2478 struct ib_uverbs_post_recv_resp resp;
2479 struct ib_recv_wr *wr, *next;
2480 const struct ib_recv_wr *bad_wr;
2482 ssize_t ret = -EINVAL;
2484 if (copy_from_user(&cmd, buf, sizeof cmd))
2487 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2488 in_len - sizeof cmd, cmd.wr_count,
2489 cmd.sge_count, cmd.wqe_size);
2493 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
2498 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2500 uobj_put_obj_read(qp);
2502 for (next = wr; next; next = next->next) {
2509 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2519 return ret ? ret : in_len;
2522 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2523 const char __user *buf, int in_len,
2526 struct ib_uverbs_post_srq_recv cmd;
2527 struct ib_uverbs_post_srq_recv_resp resp;
2528 struct ib_recv_wr *wr, *next;
2529 const struct ib_recv_wr *bad_wr;
2531 ssize_t ret = -EINVAL;
2533 if (copy_from_user(&cmd, buf, sizeof cmd))
2536 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2537 in_len - sizeof cmd, cmd.wr_count,
2538 cmd.sge_count, cmd.wqe_size);
2542 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
2547 ret = srq->device->post_srq_recv ?
2548 srq->device->post_srq_recv(srq, wr, &bad_wr) : -EOPNOTSUPP;
2550 uobj_put_obj_read(srq);
2553 for (next = wr; next; next = next->next) {
2559 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
2569 return ret ? ret : in_len;
2572 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2573 const char __user *buf, int in_len,
2576 struct ib_uverbs_create_ah cmd;
2577 struct ib_uverbs_create_ah_resp resp;
2578 struct ib_uobject *uobj;
2581 struct rdma_ah_attr attr = {};
2583 struct ib_udata udata;
2584 struct ib_device *ib_dev;
2586 if (out_len < sizeof resp)
2589 if (copy_from_user(&cmd, buf, sizeof cmd))
2592 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
2593 u64_to_user_ptr(cmd.response) + sizeof(resp),
2594 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
2595 out_len - sizeof(resp));
2597 uobj = uobj_alloc(UVERBS_OBJECT_AH, file, &ib_dev);
2599 return PTR_ERR(uobj);
2601 if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2606 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
2612 attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2613 rdma_ah_set_make_grd(&attr, false);
2614 rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2615 rdma_ah_set_sl(&attr, cmd.attr.sl);
2616 rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2617 rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2618 rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2620 if (cmd.attr.is_global) {
2621 rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2622 cmd.attr.grh.sgid_index,
2623 cmd.attr.grh.hop_limit,
2624 cmd.attr.grh.traffic_class);
2625 rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2627 rdma_ah_set_ah_flags(&attr, 0);
2630 ah = rdma_create_user_ah(pd, &attr, &udata);
2637 uobj->user_handle = cmd.user_handle;
2640 resp.ah_handle = uobj->id;
2642 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
2647 uobj_put_obj_read(pd);
2648 return uobj_alloc_commit(uobj, in_len);
2651 rdma_destroy_ah(ah);
2654 uobj_put_obj_read(pd);
2657 uobj_alloc_abort(uobj);
2661 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2662 const char __user *buf, int in_len, int out_len)
2664 struct ib_uverbs_destroy_ah cmd;
2666 if (copy_from_user(&cmd, buf, sizeof cmd))
2669 return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, file,
2673 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2674 const char __user *buf, int in_len,
2677 struct ib_uverbs_attach_mcast cmd;
2679 struct ib_uqp_object *obj;
2680 struct ib_uverbs_mcast_entry *mcast;
2683 if (copy_from_user(&cmd, buf, sizeof cmd))
2686 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
2690 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2692 mutex_lock(&obj->mcast_lock);
2693 list_for_each_entry(mcast, &obj->mcast_list, list)
2694 if (cmd.mlid == mcast->lid &&
2695 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2700 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2706 mcast->lid = cmd.mlid;
2707 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2709 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2711 list_add_tail(&mcast->list, &obj->mcast_list);
2716 mutex_unlock(&obj->mcast_lock);
2717 uobj_put_obj_read(qp);
2719 return ret ? ret : in_len;
2722 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2723 const char __user *buf, int in_len,
2726 struct ib_uverbs_detach_mcast cmd;
2727 struct ib_uqp_object *obj;
2729 struct ib_uverbs_mcast_entry *mcast;
2733 if (copy_from_user(&cmd, buf, sizeof cmd))
2736 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
2740 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2741 mutex_lock(&obj->mcast_lock);
2743 list_for_each_entry(mcast, &obj->mcast_list, list)
2744 if (cmd.mlid == mcast->lid &&
2745 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2746 list_del(&mcast->list);
2757 ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2760 mutex_unlock(&obj->mcast_lock);
2761 uobj_put_obj_read(qp);
2762 return ret ? ret : in_len;
2765 struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2767 struct ib_uflow_resources *resources;
2769 resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2777 resources->counters =
2778 kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2779 resources->collection =
2780 kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2782 if (!resources->counters || !resources->collection)
2786 resources->max = num_specs;
2790 kfree(resources->counters);
2795 EXPORT_SYMBOL(flow_resources_alloc);
2797 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
2804 for (i = 0; i < uflow_res->collection_num; i++)
2805 atomic_dec(&uflow_res->collection[i]->usecnt);
2807 for (i = 0; i < uflow_res->counters_num; i++)
2808 atomic_dec(&uflow_res->counters[i]->usecnt);
2810 kfree(uflow_res->collection);
2811 kfree(uflow_res->counters);
2814 EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2816 void flow_resources_add(struct ib_uflow_resources *uflow_res,
2817 enum ib_flow_spec_type type,
2820 WARN_ON(uflow_res->num >= uflow_res->max);
2823 case IB_FLOW_SPEC_ACTION_HANDLE:
2824 atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2825 uflow_res->collection[uflow_res->collection_num++] =
2826 (struct ib_flow_action *)ibobj;
2828 case IB_FLOW_SPEC_ACTION_COUNT:
2829 atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2830 uflow_res->counters[uflow_res->counters_num++] =
2831 (struct ib_counters *)ibobj;
2839 EXPORT_SYMBOL(flow_resources_add);
2841 static int kern_spec_to_ib_spec_action(struct ib_uverbs_file *ufile,
2842 struct ib_uverbs_flow_spec *kern_spec,
2843 union ib_flow_spec *ib_spec,
2844 struct ib_uflow_resources *uflow_res)
2846 ib_spec->type = kern_spec->type;
2847 switch (ib_spec->type) {
2848 case IB_FLOW_SPEC_ACTION_TAG:
2849 if (kern_spec->flow_tag.size !=
2850 sizeof(struct ib_uverbs_flow_spec_action_tag))
2853 ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2854 ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2856 case IB_FLOW_SPEC_ACTION_DROP:
2857 if (kern_spec->drop.size !=
2858 sizeof(struct ib_uverbs_flow_spec_action_drop))
2861 ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2863 case IB_FLOW_SPEC_ACTION_HANDLE:
2864 if (kern_spec->action.size !=
2865 sizeof(struct ib_uverbs_flow_spec_action_handle))
2867 ib_spec->action.act = uobj_get_obj_read(flow_action,
2868 UVERBS_OBJECT_FLOW_ACTION,
2869 kern_spec->action.handle,
2871 if (!ib_spec->action.act)
2873 ib_spec->action.size =
2874 sizeof(struct ib_flow_spec_action_handle);
2875 flow_resources_add(uflow_res,
2876 IB_FLOW_SPEC_ACTION_HANDLE,
2877 ib_spec->action.act);
2878 uobj_put_obj_read(ib_spec->action.act);
2880 case IB_FLOW_SPEC_ACTION_COUNT:
2881 if (kern_spec->flow_count.size !=
2882 sizeof(struct ib_uverbs_flow_spec_action_count))
2884 ib_spec->flow_count.counters =
2885 uobj_get_obj_read(counters,
2886 UVERBS_OBJECT_COUNTERS,
2887 kern_spec->flow_count.handle,
2889 if (!ib_spec->flow_count.counters)
2891 ib_spec->flow_count.size =
2892 sizeof(struct ib_flow_spec_action_count);
2893 flow_resources_add(uflow_res,
2894 IB_FLOW_SPEC_ACTION_COUNT,
2895 ib_spec->flow_count.counters);
2896 uobj_put_obj_read(ib_spec->flow_count.counters);
2904 static size_t kern_spec_filter_sz(const struct ib_uverbs_flow_spec_hdr *spec)
2906 /* Returns user space filter size, includes padding */
2907 return (spec->size - sizeof(struct ib_uverbs_flow_spec_hdr)) / 2;
2910 static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2911 u16 ib_real_filter_sz)
2914 * User space filter structures must be 64 bit aligned, otherwise this
2915 * may pass, but we won't handle additional new attributes.
2918 if (kern_filter_size > ib_real_filter_sz) {
2919 if (memchr_inv(kern_spec_filter +
2920 ib_real_filter_sz, 0,
2921 kern_filter_size - ib_real_filter_sz))
2923 return ib_real_filter_sz;
2925 return kern_filter_size;
2928 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2929 const void *kern_spec_mask,
2930 const void *kern_spec_val,
2931 size_t kern_filter_sz,
2932 union ib_flow_spec *ib_spec)
2934 ssize_t actual_filter_sz;
2935 ssize_t ib_filter_sz;
2937 /* User flow spec size must be aligned to 4 bytes */
2938 if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2941 ib_spec->type = type;
2943 if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2946 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2947 case IB_FLOW_SPEC_ETH:
2948 ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
2949 actual_filter_sz = spec_filter_size(kern_spec_mask,
2952 if (actual_filter_sz <= 0)
2954 ib_spec->size = sizeof(struct ib_flow_spec_eth);
2955 memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2956 memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2958 case IB_FLOW_SPEC_IPV4:
2959 ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
2960 actual_filter_sz = spec_filter_size(kern_spec_mask,
2963 if (actual_filter_sz <= 0)
2965 ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2966 memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2967 memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2969 case IB_FLOW_SPEC_IPV6:
2970 ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
2971 actual_filter_sz = spec_filter_size(kern_spec_mask,
2974 if (actual_filter_sz <= 0)
2976 ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2977 memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2978 memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2980 if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2981 (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2984 case IB_FLOW_SPEC_TCP:
2985 case IB_FLOW_SPEC_UDP:
2986 ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
2987 actual_filter_sz = spec_filter_size(kern_spec_mask,
2990 if (actual_filter_sz <= 0)
2992 ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2993 memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2994 memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2996 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2997 ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
2998 actual_filter_sz = spec_filter_size(kern_spec_mask,
3001 if (actual_filter_sz <= 0)
3003 ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
3004 memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
3005 memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
3007 if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
3008 (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
3011 case IB_FLOW_SPEC_ESP:
3012 ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
3013 actual_filter_sz = spec_filter_size(kern_spec_mask,
3016 if (actual_filter_sz <= 0)
3018 ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
3019 memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
3020 memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
3022 case IB_FLOW_SPEC_GRE:
3023 ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
3024 actual_filter_sz = spec_filter_size(kern_spec_mask,
3027 if (actual_filter_sz <= 0)
3029 ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
3030 memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
3031 memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
3033 case IB_FLOW_SPEC_MPLS:
3034 ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
3035 actual_filter_sz = spec_filter_size(kern_spec_mask,
3038 if (actual_filter_sz <= 0)
3040 ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
3041 memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
3042 memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
3050 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
3051 union ib_flow_spec *ib_spec)
3053 ssize_t kern_filter_sz;
3054 void *kern_spec_mask;
3055 void *kern_spec_val;
3057 kern_filter_sz = kern_spec_filter_sz(&kern_spec->hdr);
3059 kern_spec_val = (void *)kern_spec +
3060 sizeof(struct ib_uverbs_flow_spec_hdr);
3061 kern_spec_mask = kern_spec_val + kern_filter_sz;
3063 return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
3066 kern_filter_sz, ib_spec);
3069 static int kern_spec_to_ib_spec(struct ib_uverbs_file *ufile,
3070 struct ib_uverbs_flow_spec *kern_spec,
3071 union ib_flow_spec *ib_spec,
3072 struct ib_uflow_resources *uflow_res)
3074 if (kern_spec->reserved)
3077 if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
3078 return kern_spec_to_ib_spec_action(ufile, kern_spec, ib_spec,
3081 return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
3084 int ib_uverbs_ex_create_wq(struct ib_uverbs_file *file,
3085 struct ib_udata *ucore,
3086 struct ib_udata *uhw)
3088 struct ib_uverbs_ex_create_wq cmd = {};
3089 struct ib_uverbs_ex_create_wq_resp resp = {};
3090 struct ib_uwq_object *obj;
3095 struct ib_wq_init_attr wq_init_attr = {};
3096 size_t required_cmd_sz;
3097 size_t required_resp_len;
3098 struct ib_device *ib_dev;
3100 required_cmd_sz = offsetof(typeof(cmd), max_sge) + sizeof(cmd.max_sge);
3101 required_resp_len = offsetof(typeof(resp), wqn) + sizeof(resp.wqn);
3103 if (ucore->inlen < required_cmd_sz)
3106 if (ucore->outlen < required_resp_len)
3109 if (ucore->inlen > sizeof(cmd) &&
3110 !ib_is_udata_cleared(ucore, sizeof(cmd),
3111 ucore->inlen - sizeof(cmd)))
3114 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3121 obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, file,
3124 return PTR_ERR(obj);
3126 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
3132 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
3138 wq_init_attr.cq = cq;
3139 wq_init_attr.max_sge = cmd.max_sge;
3140 wq_init_attr.max_wr = cmd.max_wr;
3141 wq_init_attr.wq_context = file;
3142 wq_init_attr.wq_type = cmd.wq_type;
3143 wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
3144 if (ucore->inlen >= (offsetof(typeof(cmd), create_flags) +
3145 sizeof(cmd.create_flags)))
3146 wq_init_attr.create_flags = cmd.create_flags;
3147 obj->uevent.events_reported = 0;
3148 INIT_LIST_HEAD(&obj->uevent.event_list);
3150 if (!pd->device->create_wq) {
3154 wq = pd->device->create_wq(pd, &wq_init_attr, uhw);
3160 wq->uobject = &obj->uevent.uobject;
3161 obj->uevent.uobject.object = wq;
3162 wq->wq_type = wq_init_attr.wq_type;
3165 wq->device = pd->device;
3166 wq->wq_context = wq_init_attr.wq_context;
3167 atomic_set(&wq->usecnt, 0);
3168 atomic_inc(&pd->usecnt);
3169 atomic_inc(&cq->usecnt);
3170 wq->uobject = &obj->uevent.uobject;
3171 obj->uevent.uobject.object = wq;
3173 memset(&resp, 0, sizeof(resp));
3174 resp.wq_handle = obj->uevent.uobject.id;
3175 resp.max_sge = wq_init_attr.max_sge;
3176 resp.max_wr = wq_init_attr.max_wr;
3177 resp.wqn = wq->wq_num;
3178 resp.response_length = required_resp_len;
3179 err = ib_copy_to_udata(ucore,
3180 &resp, resp.response_length);
3184 uobj_put_obj_read(pd);
3185 uobj_put_obj_read(cq);
3186 return uobj_alloc_commit(&obj->uevent.uobject, 0);
3191 uobj_put_obj_read(cq);
3193 uobj_put_obj_read(pd);
3195 uobj_alloc_abort(&obj->uevent.uobject);
3200 int ib_uverbs_ex_destroy_wq(struct ib_uverbs_file *file,
3201 struct ib_udata *ucore,
3202 struct ib_udata *uhw)
3204 struct ib_uverbs_ex_destroy_wq cmd = {};
3205 struct ib_uverbs_ex_destroy_wq_resp resp = {};
3206 struct ib_uobject *uobj;
3207 struct ib_uwq_object *obj;
3208 size_t required_cmd_sz;
3209 size_t required_resp_len;
3212 required_cmd_sz = offsetof(typeof(cmd), wq_handle) + sizeof(cmd.wq_handle);
3213 required_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
3215 if (ucore->inlen < required_cmd_sz)
3218 if (ucore->outlen < required_resp_len)
3221 if (ucore->inlen > sizeof(cmd) &&
3222 !ib_is_udata_cleared(ucore, sizeof(cmd),
3223 ucore->inlen - sizeof(cmd)))
3226 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3233 resp.response_length = required_resp_len;
3234 uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, file);
3236 return PTR_ERR(uobj);
3238 obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3239 resp.events_reported = obj->uevent.events_reported;
3241 uobj_put_destroy(uobj);
3243 return ib_copy_to_udata(ucore, &resp, resp.response_length);
3246 int ib_uverbs_ex_modify_wq(struct ib_uverbs_file *file,
3247 struct ib_udata *ucore,
3248 struct ib_udata *uhw)
3250 struct ib_uverbs_ex_modify_wq cmd = {};
3252 struct ib_wq_attr wq_attr = {};
3253 size_t required_cmd_sz;
3256 required_cmd_sz = offsetof(typeof(cmd), curr_wq_state) + sizeof(cmd.curr_wq_state);
3257 if (ucore->inlen < required_cmd_sz)
3260 if (ucore->inlen > sizeof(cmd) &&
3261 !ib_is_udata_cleared(ucore, sizeof(cmd),
3262 ucore->inlen - sizeof(cmd)))
3265 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3272 if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3275 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, file);
3279 wq_attr.curr_wq_state = cmd.curr_wq_state;
3280 wq_attr.wq_state = cmd.wq_state;
3281 if (cmd.attr_mask & IB_WQ_FLAGS) {
3282 wq_attr.flags = cmd.flags;
3283 wq_attr.flags_mask = cmd.flags_mask;
3285 if (!wq->device->modify_wq) {
3289 ret = wq->device->modify_wq(wq, &wq_attr, cmd.attr_mask, uhw);
3291 uobj_put_obj_read(wq);
3295 int ib_uverbs_ex_create_rwq_ind_table(struct ib_uverbs_file *file,
3296 struct ib_udata *ucore,
3297 struct ib_udata *uhw)
3299 struct ib_uverbs_ex_create_rwq_ind_table cmd = {};
3300 struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
3301 struct ib_uobject *uobj;
3303 struct ib_rwq_ind_table_init_attr init_attr = {};
3304 struct ib_rwq_ind_table *rwq_ind_tbl;
3305 struct ib_wq **wqs = NULL;
3306 u32 *wqs_handles = NULL;
3307 struct ib_wq *wq = NULL;
3308 int i, j, num_read_wqs;
3310 u32 expected_in_size;
3311 size_t required_cmd_sz_header;
3312 size_t required_resp_len;
3313 struct ib_device *ib_dev;
3315 required_cmd_sz_header = offsetof(typeof(cmd), log_ind_tbl_size) + sizeof(cmd.log_ind_tbl_size);
3316 required_resp_len = offsetof(typeof(resp), ind_tbl_num) + sizeof(resp.ind_tbl_num);
3318 if (ucore->inlen < required_cmd_sz_header)
3321 if (ucore->outlen < required_resp_len)
3324 err = ib_copy_from_udata(&cmd, ucore, required_cmd_sz_header);
3328 ucore->inbuf += required_cmd_sz_header;
3329 ucore->inlen -= required_cmd_sz_header;
3334 if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3337 num_wq_handles = 1 << cmd.log_ind_tbl_size;
3338 expected_in_size = num_wq_handles * sizeof(__u32);
3339 if (num_wq_handles == 1)
3340 /* input size for wq handles is u64 aligned */
3341 expected_in_size += sizeof(__u32);
3343 if (ucore->inlen < expected_in_size)
3346 if (ucore->inlen > expected_in_size &&
3347 !ib_is_udata_cleared(ucore, expected_in_size,
3348 ucore->inlen - expected_in_size))
3351 wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3356 err = ib_copy_from_udata(wqs_handles, ucore,
3357 num_wq_handles * sizeof(__u32));
3361 wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3367 for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3369 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3370 wqs_handles[num_read_wqs], file);
3376 wqs[num_read_wqs] = wq;
3379 uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, file, &ib_dev);
3381 err = PTR_ERR(uobj);
3385 init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3386 init_attr.ind_tbl = wqs;
3388 if (!ib_dev->create_rwq_ind_table) {
3392 rwq_ind_tbl = ib_dev->create_rwq_ind_table(ib_dev, &init_attr, uhw);
3394 if (IS_ERR(rwq_ind_tbl)) {
3395 err = PTR_ERR(rwq_ind_tbl);
3399 rwq_ind_tbl->ind_tbl = wqs;
3400 rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3401 rwq_ind_tbl->uobject = uobj;
3402 uobj->object = rwq_ind_tbl;
3403 rwq_ind_tbl->device = ib_dev;
3404 atomic_set(&rwq_ind_tbl->usecnt, 0);
3406 for (i = 0; i < num_wq_handles; i++)
3407 atomic_inc(&wqs[i]->usecnt);
3409 resp.ind_tbl_handle = uobj->id;
3410 resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3411 resp.response_length = required_resp_len;
3413 err = ib_copy_to_udata(ucore,
3414 &resp, resp.response_length);
3420 for (j = 0; j < num_read_wqs; j++)
3421 uobj_put_obj_read(wqs[j]);
3423 return uobj_alloc_commit(uobj, 0);
3426 ib_destroy_rwq_ind_table(rwq_ind_tbl);
3428 uobj_alloc_abort(uobj);
3430 for (j = 0; j < num_read_wqs; j++)
3431 uobj_put_obj_read(wqs[j]);
3438 int ib_uverbs_ex_destroy_rwq_ind_table(struct ib_uverbs_file *file,
3439 struct ib_udata *ucore,
3440 struct ib_udata *uhw)
3442 struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
3444 size_t required_cmd_sz;
3446 required_cmd_sz = offsetof(typeof(cmd), ind_tbl_handle) + sizeof(cmd.ind_tbl_handle);
3448 if (ucore->inlen < required_cmd_sz)
3451 if (ucore->inlen > sizeof(cmd) &&
3452 !ib_is_udata_cleared(ucore, sizeof(cmd),
3453 ucore->inlen - sizeof(cmd)))
3456 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
3463 return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3464 cmd.ind_tbl_handle, file, 0);
3467 int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
3468 struct ib_udata *ucore,
3469 struct ib_udata *uhw)
3471 struct ib_uverbs_create_flow cmd;
3472 struct ib_uverbs_create_flow_resp resp;
3473 struct ib_uobject *uobj;
3474 struct ib_flow *flow_id;
3475 struct ib_uverbs_flow_attr *kern_flow_attr;
3476 struct ib_flow_attr *flow_attr;
3478 struct ib_uflow_resources *uflow_res;
3479 struct ib_uverbs_flow_spec_hdr *kern_spec;
3483 struct ib_device *ib_dev;
3485 if (ucore->inlen < sizeof(cmd))
3488 if (ucore->outlen < sizeof(resp))
3491 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3495 ucore->inbuf += sizeof(cmd);
3496 ucore->inlen -= sizeof(cmd);
3501 if (!capable(CAP_NET_RAW))
3504 if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3507 if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3508 ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3509 (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3512 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3515 if (cmd.flow_attr.size > ucore->inlen ||
3516 cmd.flow_attr.size >
3517 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3520 if (cmd.flow_attr.reserved[0] ||
3521 cmd.flow_attr.reserved[1])
3524 if (cmd.flow_attr.num_of_specs) {
3525 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3527 if (!kern_flow_attr)
3530 *kern_flow_attr = cmd.flow_attr;
3531 err = ib_copy_from_udata(&kern_flow_attr->flow_specs, ucore,
3532 cmd.flow_attr.size);
3536 kern_flow_attr = &cmd.flow_attr;
3539 uobj = uobj_alloc(UVERBS_OBJECT_FLOW, file, &ib_dev);
3541 err = PTR_ERR(uobj);
3545 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
3551 if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3556 if (!qp->device->create_flow) {
3561 flow_attr = kzalloc(struct_size(flow_attr, flows,
3562 cmd.flow_attr.num_of_specs), GFP_KERNEL);
3567 uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3570 goto err_free_flow_attr;
3573 flow_attr->type = kern_flow_attr->type;
3574 flow_attr->priority = kern_flow_attr->priority;
3575 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3576 flow_attr->port = kern_flow_attr->port;
3577 flow_attr->flags = kern_flow_attr->flags;
3578 flow_attr->size = sizeof(*flow_attr);
3580 kern_spec = kern_flow_attr->flow_specs;
3581 ib_spec = flow_attr + 1;
3582 for (i = 0; i < flow_attr->num_of_specs &&
3583 cmd.flow_attr.size >= sizeof(*kern_spec) &&
3584 cmd.flow_attr.size >= kern_spec->size;
3586 err = kern_spec_to_ib_spec(
3587 file, (struct ib_uverbs_flow_spec *)kern_spec,
3588 ib_spec, uflow_res);
3593 ((union ib_flow_spec *) ib_spec)->size;
3594 cmd.flow_attr.size -= kern_spec->size;
3595 kern_spec = ((void *)kern_spec) + kern_spec->size;
3596 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3598 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3599 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3600 i, cmd.flow_attr.size);
3605 flow_id = qp->device->create_flow(qp, flow_attr,
3606 IB_FLOW_DOMAIN_USER, uhw);
3608 if (IS_ERR(flow_id)) {
3609 err = PTR_ERR(flow_id);
3613 ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3615 memset(&resp, 0, sizeof(resp));
3616 resp.flow_handle = uobj->id;
3618 err = ib_copy_to_udata(ucore,
3619 &resp, sizeof(resp));
3623 uobj_put_obj_read(qp);
3625 if (cmd.flow_attr.num_of_specs)
3626 kfree(kern_flow_attr);
3627 return uobj_alloc_commit(uobj, 0);
3629 if (!qp->device->destroy_flow(flow_id))
3630 atomic_dec(&qp->usecnt);
3632 ib_uverbs_flow_resources_free(uflow_res);
3636 uobj_put_obj_read(qp);
3638 uobj_alloc_abort(uobj);
3640 if (cmd.flow_attr.num_of_specs)
3641 kfree(kern_flow_attr);
3645 int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
3646 struct ib_udata *ucore,
3647 struct ib_udata *uhw)
3649 struct ib_uverbs_destroy_flow cmd;
3652 if (ucore->inlen < sizeof(cmd))
3655 ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3662 return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, file,
3666 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
3667 struct ib_uverbs_create_xsrq *cmd,
3668 struct ib_udata *udata)
3670 struct ib_uverbs_create_srq_resp resp;
3671 struct ib_usrq_object *obj;
3674 struct ib_uobject *uninitialized_var(xrcd_uobj);
3675 struct ib_srq_init_attr attr;
3677 struct ib_device *ib_dev;
3679 obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, file,
3682 return PTR_ERR(obj);
3684 if (cmd->srq_type == IB_SRQT_TM)
3685 attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3687 if (cmd->srq_type == IB_SRQT_XRC) {
3688 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3690 if (IS_ERR(xrcd_uobj)) {
3695 attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3696 if (!attr.ext.xrc.xrcd) {
3701 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3702 atomic_inc(&obj->uxrcd->refcnt);
3705 if (ib_srq_has_cq(cmd->srq_type)) {
3706 attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3707 cmd->cq_handle, file);
3714 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, file);
3720 attr.event_handler = ib_uverbs_srq_event_handler;
3721 attr.srq_context = file;
3722 attr.srq_type = cmd->srq_type;
3723 attr.attr.max_wr = cmd->max_wr;
3724 attr.attr.max_sge = cmd->max_sge;
3725 attr.attr.srq_limit = cmd->srq_limit;
3727 obj->uevent.events_reported = 0;
3728 INIT_LIST_HEAD(&obj->uevent.event_list);
3730 srq = pd->device->create_srq(pd, &attr, udata);
3736 srq->device = pd->device;
3738 srq->srq_type = cmd->srq_type;
3739 srq->uobject = &obj->uevent.uobject;
3740 srq->event_handler = attr.event_handler;
3741 srq->srq_context = attr.srq_context;
3743 if (ib_srq_has_cq(cmd->srq_type)) {
3744 srq->ext.cq = attr.ext.cq;
3745 atomic_inc(&attr.ext.cq->usecnt);
3748 if (cmd->srq_type == IB_SRQT_XRC) {
3749 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
3750 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
3753 atomic_inc(&pd->usecnt);
3754 atomic_set(&srq->usecnt, 0);
3756 obj->uevent.uobject.object = srq;
3757 obj->uevent.uobject.user_handle = cmd->user_handle;
3759 memset(&resp, 0, sizeof resp);
3760 resp.srq_handle = obj->uevent.uobject.id;
3761 resp.max_wr = attr.attr.max_wr;
3762 resp.max_sge = attr.attr.max_sge;
3763 if (cmd->srq_type == IB_SRQT_XRC)
3764 resp.srqn = srq->ext.xrc.srq_num;
3766 if (copy_to_user(u64_to_user_ptr(cmd->response),
3767 &resp, sizeof resp)) {
3772 if (cmd->srq_type == IB_SRQT_XRC)
3773 uobj_put_read(xrcd_uobj);
3775 if (ib_srq_has_cq(cmd->srq_type))
3776 uobj_put_obj_read(attr.ext.cq);
3778 uobj_put_obj_read(pd);
3779 return uobj_alloc_commit(&obj->uevent.uobject, 0);
3782 ib_destroy_srq(srq);
3785 uobj_put_obj_read(pd);
3788 if (ib_srq_has_cq(cmd->srq_type))
3789 uobj_put_obj_read(attr.ext.cq);
3792 if (cmd->srq_type == IB_SRQT_XRC) {
3793 atomic_dec(&obj->uxrcd->refcnt);
3794 uobj_put_read(xrcd_uobj);
3798 uobj_alloc_abort(&obj->uevent.uobject);
3802 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
3803 const char __user *buf, int in_len,
3806 struct ib_uverbs_create_srq cmd;
3807 struct ib_uverbs_create_xsrq xcmd;
3808 struct ib_uverbs_create_srq_resp resp;
3809 struct ib_udata udata;
3812 if (out_len < sizeof resp)
3815 if (copy_from_user(&cmd, buf, sizeof cmd))
3818 memset(&xcmd, 0, sizeof(xcmd));
3819 xcmd.response = cmd.response;
3820 xcmd.user_handle = cmd.user_handle;
3821 xcmd.srq_type = IB_SRQT_BASIC;
3822 xcmd.pd_handle = cmd.pd_handle;
3823 xcmd.max_wr = cmd.max_wr;
3824 xcmd.max_sge = cmd.max_sge;
3825 xcmd.srq_limit = cmd.srq_limit;
3827 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
3828 u64_to_user_ptr(cmd.response) + sizeof(resp),
3829 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
3830 out_len - sizeof(resp));
3832 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
3839 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
3840 const char __user *buf, int in_len, int out_len)
3842 struct ib_uverbs_create_xsrq cmd;
3843 struct ib_uverbs_create_srq_resp resp;
3844 struct ib_udata udata;
3847 if (out_len < sizeof resp)
3850 if (copy_from_user(&cmd, buf, sizeof cmd))
3853 ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
3854 u64_to_user_ptr(cmd.response) + sizeof(resp),
3855 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
3856 out_len - sizeof(resp));
3858 ret = __uverbs_create_xsrq(file, &cmd, &udata);
3865 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
3866 const char __user *buf, int in_len,
3869 struct ib_uverbs_modify_srq cmd;
3870 struct ib_udata udata;
3872 struct ib_srq_attr attr;
3875 if (copy_from_user(&cmd, buf, sizeof cmd))
3878 ib_uverbs_init_udata(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
3881 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
3885 attr.max_wr = cmd.max_wr;
3886 attr.srq_limit = cmd.srq_limit;
3888 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
3890 uobj_put_obj_read(srq);
3892 return ret ? ret : in_len;
3895 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
3896 const char __user *buf,
3897 int in_len, int out_len)
3899 struct ib_uverbs_query_srq cmd;
3900 struct ib_uverbs_query_srq_resp resp;
3901 struct ib_srq_attr attr;
3905 if (out_len < sizeof resp)
3908 if (copy_from_user(&cmd, buf, sizeof cmd))
3911 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
3915 ret = ib_query_srq(srq, &attr);
3917 uobj_put_obj_read(srq);
3922 memset(&resp, 0, sizeof resp);
3924 resp.max_wr = attr.max_wr;
3925 resp.max_sge = attr.max_sge;
3926 resp.srq_limit = attr.srq_limit;
3928 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
3934 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
3935 const char __user *buf, int in_len,
3938 struct ib_uverbs_destroy_srq cmd;
3939 struct ib_uverbs_destroy_srq_resp resp;
3940 struct ib_uobject *uobj;
3941 struct ib_uevent_object *obj;
3943 if (copy_from_user(&cmd, buf, sizeof cmd))
3946 uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
3948 return PTR_ERR(uobj);
3950 obj = container_of(uobj, struct ib_uevent_object, uobject);
3951 memset(&resp, 0, sizeof(resp));
3952 resp.events_reported = obj->events_reported;
3954 uobj_put_destroy(uobj);
3956 if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
3962 int ib_uverbs_ex_query_device(struct ib_uverbs_file *file,
3963 struct ib_udata *ucore,
3964 struct ib_udata *uhw)
3966 struct ib_uverbs_ex_query_device_resp resp = { {0} };
3967 struct ib_uverbs_ex_query_device cmd;
3968 struct ib_device_attr attr = {0};
3969 struct ib_ucontext *ucontext;
3970 struct ib_device *ib_dev;
3973 ucontext = ib_uverbs_get_ucontext(file);
3974 if (IS_ERR(ucontext))
3975 return PTR_ERR(ucontext);
3976 ib_dev = ucontext->device;
3978 if (!ib_dev->query_device)
3981 if (ucore->inlen < sizeof(cmd))
3984 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3994 resp.response_length = offsetof(typeof(resp), odp_caps);
3996 if (ucore->outlen < resp.response_length)
3999 err = ib_dev->query_device(ib_dev, &attr, uhw);
4003 copy_query_dev_fields(ucontext, &resp.base, &attr);
4005 if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
4008 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4009 resp.odp_caps.general_caps = attr.odp_caps.general_caps;
4010 resp.odp_caps.per_transport_caps.rc_odp_caps =
4011 attr.odp_caps.per_transport_caps.rc_odp_caps;
4012 resp.odp_caps.per_transport_caps.uc_odp_caps =
4013 attr.odp_caps.per_transport_caps.uc_odp_caps;
4014 resp.odp_caps.per_transport_caps.ud_odp_caps =
4015 attr.odp_caps.per_transport_caps.ud_odp_caps;
4017 resp.response_length += sizeof(resp.odp_caps);
4019 if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
4022 resp.timestamp_mask = attr.timestamp_mask;
4023 resp.response_length += sizeof(resp.timestamp_mask);
4025 if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
4028 resp.hca_core_clock = attr.hca_core_clock;
4029 resp.response_length += sizeof(resp.hca_core_clock);
4031 if (ucore->outlen < resp.response_length + sizeof(resp.device_cap_flags_ex))
4034 resp.device_cap_flags_ex = attr.device_cap_flags;
4035 resp.response_length += sizeof(resp.device_cap_flags_ex);
4037 if (ucore->outlen < resp.response_length + sizeof(resp.rss_caps))
4040 resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
4041 resp.rss_caps.max_rwq_indirection_tables =
4042 attr.rss_caps.max_rwq_indirection_tables;
4043 resp.rss_caps.max_rwq_indirection_table_size =
4044 attr.rss_caps.max_rwq_indirection_table_size;
4046 resp.response_length += sizeof(resp.rss_caps);
4048 if (ucore->outlen < resp.response_length + sizeof(resp.max_wq_type_rq))
4051 resp.max_wq_type_rq = attr.max_wq_type_rq;
4052 resp.response_length += sizeof(resp.max_wq_type_rq);
4054 if (ucore->outlen < resp.response_length + sizeof(resp.raw_packet_caps))
4057 resp.raw_packet_caps = attr.raw_packet_caps;
4058 resp.response_length += sizeof(resp.raw_packet_caps);
4060 if (ucore->outlen < resp.response_length + sizeof(resp.tm_caps))
4063 resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
4064 resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
4065 resp.tm_caps.max_ops = attr.tm_caps.max_ops;
4066 resp.tm_caps.max_sge = attr.tm_caps.max_sge;
4067 resp.tm_caps.flags = attr.tm_caps.flags;
4068 resp.response_length += sizeof(resp.tm_caps);
4070 if (ucore->outlen < resp.response_length + sizeof(resp.cq_moderation_caps))
4073 resp.cq_moderation_caps.max_cq_moderation_count =
4074 attr.cq_caps.max_cq_moderation_count;
4075 resp.cq_moderation_caps.max_cq_moderation_period =
4076 attr.cq_caps.max_cq_moderation_period;
4077 resp.response_length += sizeof(resp.cq_moderation_caps);
4079 if (ucore->outlen < resp.response_length + sizeof(resp.max_dm_size))
4082 resp.max_dm_size = attr.max_dm_size;
4083 resp.response_length += sizeof(resp.max_dm_size);
4085 err = ib_copy_to_udata(ucore, &resp, resp.response_length);
4089 int ib_uverbs_ex_modify_cq(struct ib_uverbs_file *file,
4090 struct ib_udata *ucore,
4091 struct ib_udata *uhw)
4093 struct ib_uverbs_ex_modify_cq cmd = {};
4095 size_t required_cmd_sz;
4098 required_cmd_sz = offsetof(typeof(cmd), reserved) +
4099 sizeof(cmd.reserved);
4100 if (ucore->inlen < required_cmd_sz)
4104 if (ucore->inlen > sizeof(cmd) &&
4105 !ib_is_udata_cleared(ucore, sizeof(cmd),
4106 ucore->inlen - sizeof(cmd)))
4109 ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
4113 if (!cmd.attr_mask || cmd.reserved)
4116 if (cmd.attr_mask > IB_CQ_MODERATE)
4119 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
4123 ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
4125 uobj_put_obj_read(cq);