2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
4 * Copyright (c) 2005 PathScale, Inc. All rights reserved.
5 * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/file.h>
38 #include <linux/slab.h>
40 #include <asm/uaccess.h>
44 struct uverbs_lock_class {
45 struct lock_class_key key;
49 static struct uverbs_lock_class pd_lock_class = { .name = "PD-uobj" };
50 static struct uverbs_lock_class mr_lock_class = { .name = "MR-uobj" };
51 static struct uverbs_lock_class mw_lock_class = { .name = "MW-uobj" };
52 static struct uverbs_lock_class cq_lock_class = { .name = "CQ-uobj" };
53 static struct uverbs_lock_class qp_lock_class = { .name = "QP-uobj" };
54 static struct uverbs_lock_class ah_lock_class = { .name = "AH-uobj" };
55 static struct uverbs_lock_class srq_lock_class = { .name = "SRQ-uobj" };
56 static struct uverbs_lock_class xrcd_lock_class = { .name = "XRCD-uobj" };
58 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
60 (udata)->inbuf = (void __user *) (ibuf); \
61 (udata)->outbuf = (void __user *) (obuf); \
62 (udata)->inlen = (ilen); \
63 (udata)->outlen = (olen); \
67 * The ib_uobject locking scheme is as follows:
69 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
70 * needs to be held during all idr operations. When an object is
71 * looked up, a reference must be taken on the object's kref before
74 * - Each object also has an rwsem. This rwsem must be held for
75 * reading while an operation that uses the object is performed.
76 * For example, while registering an MR, the associated PD's
77 * uobject.mutex must be held for reading. The rwsem must be held
78 * for writing while initializing or destroying an object.
80 * - In addition, each object has a "live" flag. If this flag is not
81 * set, then lookups of the object will fail even if it is found in
82 * the idr. This handles a reader that blocks and does not acquire
83 * the rwsem until after the object is destroyed. The destroy
84 * operation will set the live flag to 0 and then drop the rwsem;
85 * this will allow the reader to acquire the rwsem, see that the
86 * live flag is 0, and then drop the rwsem and its reference to
87 * object. The underlying storage will not be freed until the last
88 * reference to the object is dropped.
91 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
92 struct ib_ucontext *context, struct uverbs_lock_class *c)
94 uobj->user_handle = user_handle;
95 uobj->context = context;
96 kref_init(&uobj->ref);
97 init_rwsem(&uobj->mutex);
98 lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
102 static void release_uobj(struct kref *kref)
104 kfree(container_of(kref, struct ib_uobject, ref));
107 static void put_uobj(struct ib_uobject *uobj)
109 kref_put(&uobj->ref, release_uobj);
112 static void put_uobj_read(struct ib_uobject *uobj)
114 up_read(&uobj->mutex);
118 static void put_uobj_write(struct ib_uobject *uobj)
120 up_write(&uobj->mutex);
124 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
128 idr_preload(GFP_KERNEL);
129 spin_lock(&ib_uverbs_idr_lock);
131 ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
135 spin_unlock(&ib_uverbs_idr_lock);
138 return ret < 0 ? ret : 0;
141 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
143 spin_lock(&ib_uverbs_idr_lock);
144 idr_remove(idr, uobj->id);
145 spin_unlock(&ib_uverbs_idr_lock);
148 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
149 struct ib_ucontext *context)
151 struct ib_uobject *uobj;
153 spin_lock(&ib_uverbs_idr_lock);
154 uobj = idr_find(idr, id);
156 if (uobj->context == context)
157 kref_get(&uobj->ref);
161 spin_unlock(&ib_uverbs_idr_lock);
166 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
167 struct ib_ucontext *context, int nested)
169 struct ib_uobject *uobj;
171 uobj = __idr_get_uobj(idr, id, context);
176 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
178 down_read(&uobj->mutex);
187 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
188 struct ib_ucontext *context)
190 struct ib_uobject *uobj;
192 uobj = __idr_get_uobj(idr, id, context);
196 down_write(&uobj->mutex);
198 put_uobj_write(uobj);
205 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
208 struct ib_uobject *uobj;
210 uobj = idr_read_uobj(idr, id, context, nested);
211 return uobj ? uobj->object : NULL;
214 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
216 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
219 static void put_pd_read(struct ib_pd *pd)
221 put_uobj_read(pd->uobject);
224 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
226 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
229 static void put_cq_read(struct ib_cq *cq)
231 put_uobj_read(cq->uobject);
234 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
236 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
239 static void put_ah_read(struct ib_ah *ah)
241 put_uobj_read(ah->uobject);
244 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
246 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
249 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
251 struct ib_uobject *uobj;
253 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
254 return uobj ? uobj->object : NULL;
257 static void put_qp_read(struct ib_qp *qp)
259 put_uobj_read(qp->uobject);
262 static void put_qp_write(struct ib_qp *qp)
264 put_uobj_write(qp->uobject);
267 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
269 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
272 static void put_srq_read(struct ib_srq *srq)
274 put_uobj_read(srq->uobject);
277 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
278 struct ib_uobject **uobj)
280 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
281 return *uobj ? (*uobj)->object : NULL;
284 static void put_xrcd_read(struct ib_uobject *uobj)
289 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
290 const char __user *buf,
291 int in_len, int out_len)
293 struct ib_uverbs_get_context cmd;
294 struct ib_uverbs_get_context_resp resp;
295 struct ib_udata udata;
296 struct ib_device *ibdev = file->device->ib_dev;
297 struct ib_ucontext *ucontext;
301 if (out_len < sizeof resp)
304 if (copy_from_user(&cmd, buf, sizeof cmd))
307 mutex_lock(&file->mutex);
309 if (file->ucontext) {
314 INIT_UDATA(&udata, buf + sizeof cmd,
315 (unsigned long) cmd.response + sizeof resp,
316 in_len - sizeof cmd, out_len - sizeof resp);
318 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
319 if (IS_ERR(ucontext)) {
320 ret = PTR_ERR(ucontext);
324 ucontext->device = ibdev;
325 INIT_LIST_HEAD(&ucontext->pd_list);
326 INIT_LIST_HEAD(&ucontext->mr_list);
327 INIT_LIST_HEAD(&ucontext->mw_list);
328 INIT_LIST_HEAD(&ucontext->cq_list);
329 INIT_LIST_HEAD(&ucontext->qp_list);
330 INIT_LIST_HEAD(&ucontext->srq_list);
331 INIT_LIST_HEAD(&ucontext->ah_list);
332 INIT_LIST_HEAD(&ucontext->xrcd_list);
333 ucontext->closing = 0;
335 resp.num_comp_vectors = file->device->num_comp_vectors;
337 ret = get_unused_fd();
342 filp = ib_uverbs_alloc_event_file(file, 1);
348 if (copy_to_user((void __user *) (unsigned long) cmd.response,
349 &resp, sizeof resp)) {
354 file->async_file = filp->private_data;
356 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
357 ib_uverbs_event_handler);
358 ret = ib_register_event_handler(&file->event_handler);
362 kref_get(&file->async_file->ref);
363 kref_get(&file->ref);
364 file->ucontext = ucontext;
366 fd_install(resp.async_fd, filp);
368 mutex_unlock(&file->mutex);
376 put_unused_fd(resp.async_fd);
379 ibdev->dealloc_ucontext(ucontext);
382 mutex_unlock(&file->mutex);
386 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
387 const char __user *buf,
388 int in_len, int out_len)
390 struct ib_uverbs_query_device cmd;
391 struct ib_uverbs_query_device_resp resp;
392 struct ib_device_attr attr;
395 if (out_len < sizeof resp)
398 if (copy_from_user(&cmd, buf, sizeof cmd))
401 ret = ib_query_device(file->device->ib_dev, &attr);
405 memset(&resp, 0, sizeof resp);
407 resp.fw_ver = attr.fw_ver;
408 resp.node_guid = file->device->ib_dev->node_guid;
409 resp.sys_image_guid = attr.sys_image_guid;
410 resp.max_mr_size = attr.max_mr_size;
411 resp.page_size_cap = attr.page_size_cap;
412 resp.vendor_id = attr.vendor_id;
413 resp.vendor_part_id = attr.vendor_part_id;
414 resp.hw_ver = attr.hw_ver;
415 resp.max_qp = attr.max_qp;
416 resp.max_qp_wr = attr.max_qp_wr;
417 resp.device_cap_flags = attr.device_cap_flags;
418 resp.max_sge = attr.max_sge;
419 resp.max_sge_rd = attr.max_sge_rd;
420 resp.max_cq = attr.max_cq;
421 resp.max_cqe = attr.max_cqe;
422 resp.max_mr = attr.max_mr;
423 resp.max_pd = attr.max_pd;
424 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
425 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
426 resp.max_res_rd_atom = attr.max_res_rd_atom;
427 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
428 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
429 resp.atomic_cap = attr.atomic_cap;
430 resp.max_ee = attr.max_ee;
431 resp.max_rdd = attr.max_rdd;
432 resp.max_mw = attr.max_mw;
433 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
434 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
435 resp.max_mcast_grp = attr.max_mcast_grp;
436 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
437 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
438 resp.max_ah = attr.max_ah;
439 resp.max_fmr = attr.max_fmr;
440 resp.max_map_per_fmr = attr.max_map_per_fmr;
441 resp.max_srq = attr.max_srq;
442 resp.max_srq_wr = attr.max_srq_wr;
443 resp.max_srq_sge = attr.max_srq_sge;
444 resp.max_pkeys = attr.max_pkeys;
445 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
446 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
448 if (copy_to_user((void __user *) (unsigned long) cmd.response,
455 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
456 const char __user *buf,
457 int in_len, int out_len)
459 struct ib_uverbs_query_port cmd;
460 struct ib_uverbs_query_port_resp resp;
461 struct ib_port_attr attr;
464 if (out_len < sizeof resp)
467 if (copy_from_user(&cmd, buf, sizeof cmd))
470 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
474 memset(&resp, 0, sizeof resp);
476 resp.state = attr.state;
477 resp.max_mtu = attr.max_mtu;
478 resp.active_mtu = attr.active_mtu;
479 resp.gid_tbl_len = attr.gid_tbl_len;
480 resp.port_cap_flags = attr.port_cap_flags;
481 resp.max_msg_sz = attr.max_msg_sz;
482 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
483 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
484 resp.pkey_tbl_len = attr.pkey_tbl_len;
486 resp.sm_lid = attr.sm_lid;
488 resp.max_vl_num = attr.max_vl_num;
489 resp.sm_sl = attr.sm_sl;
490 resp.subnet_timeout = attr.subnet_timeout;
491 resp.init_type_reply = attr.init_type_reply;
492 resp.active_width = attr.active_width;
493 resp.active_speed = attr.active_speed;
494 resp.phys_state = attr.phys_state;
495 resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
498 if (copy_to_user((void __user *) (unsigned long) cmd.response,
505 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
506 const char __user *buf,
507 int in_len, int out_len)
509 struct ib_uverbs_alloc_pd cmd;
510 struct ib_uverbs_alloc_pd_resp resp;
511 struct ib_udata udata;
512 struct ib_uobject *uobj;
516 if (out_len < sizeof resp)
519 if (copy_from_user(&cmd, buf, sizeof cmd))
522 INIT_UDATA(&udata, buf + sizeof cmd,
523 (unsigned long) cmd.response + sizeof resp,
524 in_len - sizeof cmd, out_len - sizeof resp);
526 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
530 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
531 down_write(&uobj->mutex);
533 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
534 file->ucontext, &udata);
540 pd->device = file->device->ib_dev;
542 atomic_set(&pd->usecnt, 0);
545 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
549 memset(&resp, 0, sizeof resp);
550 resp.pd_handle = uobj->id;
552 if (copy_to_user((void __user *) (unsigned long) cmd.response,
553 &resp, sizeof resp)) {
558 mutex_lock(&file->mutex);
559 list_add_tail(&uobj->list, &file->ucontext->pd_list);
560 mutex_unlock(&file->mutex);
564 up_write(&uobj->mutex);
569 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
575 put_uobj_write(uobj);
579 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
580 const char __user *buf,
581 int in_len, int out_len)
583 struct ib_uverbs_dealloc_pd cmd;
584 struct ib_uobject *uobj;
587 if (copy_from_user(&cmd, buf, sizeof cmd))
590 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
594 ret = ib_dealloc_pd(uobj->object);
598 put_uobj_write(uobj);
603 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
605 mutex_lock(&file->mutex);
606 list_del(&uobj->list);
607 mutex_unlock(&file->mutex);
614 struct xrcd_table_entry {
616 struct ib_xrcd *xrcd;
620 static int xrcd_table_insert(struct ib_uverbs_device *dev,
622 struct ib_xrcd *xrcd)
624 struct xrcd_table_entry *entry, *scan;
625 struct rb_node **p = &dev->xrcd_tree.rb_node;
626 struct rb_node *parent = NULL;
628 entry = kmalloc(sizeof *entry, GFP_KERNEL);
633 entry->inode = inode;
637 scan = rb_entry(parent, struct xrcd_table_entry, node);
639 if (inode < scan->inode) {
641 } else if (inode > scan->inode) {
649 rb_link_node(&entry->node, parent, p);
650 rb_insert_color(&entry->node, &dev->xrcd_tree);
655 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
658 struct xrcd_table_entry *entry;
659 struct rb_node *p = dev->xrcd_tree.rb_node;
662 entry = rb_entry(p, struct xrcd_table_entry, node);
664 if (inode < entry->inode)
666 else if (inode > entry->inode)
675 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
677 struct xrcd_table_entry *entry;
679 entry = xrcd_table_search(dev, inode);
686 static void xrcd_table_delete(struct ib_uverbs_device *dev,
689 struct xrcd_table_entry *entry;
691 entry = xrcd_table_search(dev, inode);
694 rb_erase(&entry->node, &dev->xrcd_tree);
699 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
700 const char __user *buf, int in_len,
703 struct ib_uverbs_open_xrcd cmd;
704 struct ib_uverbs_open_xrcd_resp resp;
705 struct ib_udata udata;
706 struct ib_uxrcd_object *obj;
707 struct ib_xrcd *xrcd = NULL;
708 struct fd f = {NULL, 0};
709 struct inode *inode = NULL;
713 if (out_len < sizeof resp)
716 if (copy_from_user(&cmd, buf, sizeof cmd))
719 INIT_UDATA(&udata, buf + sizeof cmd,
720 (unsigned long) cmd.response + sizeof resp,
721 in_len - sizeof cmd, out_len - sizeof resp);
723 mutex_lock(&file->device->xrcd_tree_mutex);
726 /* search for file descriptor */
730 goto err_tree_mutex_unlock;
733 inode = file_inode(f.file);
734 xrcd = find_xrcd(file->device, inode);
735 if (!xrcd && !(cmd.oflags & O_CREAT)) {
736 /* no file descriptor. Need CREATE flag */
738 goto err_tree_mutex_unlock;
741 if (xrcd && cmd.oflags & O_EXCL) {
743 goto err_tree_mutex_unlock;
747 obj = kmalloc(sizeof *obj, GFP_KERNEL);
750 goto err_tree_mutex_unlock;
753 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
755 down_write(&obj->uobject.mutex);
758 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
759 file->ucontext, &udata);
766 xrcd->device = file->device->ib_dev;
767 atomic_set(&xrcd->usecnt, 0);
768 mutex_init(&xrcd->tgt_qp_mutex);
769 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
773 atomic_set(&obj->refcnt, 0);
774 obj->uobject.object = xrcd;
775 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
779 memset(&resp, 0, sizeof resp);
780 resp.xrcd_handle = obj->uobject.id;
784 /* create new inode/xrcd table entry */
785 ret = xrcd_table_insert(file->device, inode, xrcd);
787 goto err_insert_xrcd;
789 atomic_inc(&xrcd->usecnt);
792 if (copy_to_user((void __user *) (unsigned long) cmd.response,
793 &resp, sizeof resp)) {
801 mutex_lock(&file->mutex);
802 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
803 mutex_unlock(&file->mutex);
805 obj->uobject.live = 1;
806 up_write(&obj->uobject.mutex);
808 mutex_unlock(&file->device->xrcd_tree_mutex);
814 xrcd_table_delete(file->device, inode);
815 atomic_dec(&xrcd->usecnt);
819 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
822 ib_dealloc_xrcd(xrcd);
825 put_uobj_write(&obj->uobject);
827 err_tree_mutex_unlock:
831 mutex_unlock(&file->device->xrcd_tree_mutex);
836 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
837 const char __user *buf, int in_len,
840 struct ib_uverbs_close_xrcd cmd;
841 struct ib_uobject *uobj;
842 struct ib_xrcd *xrcd = NULL;
843 struct inode *inode = NULL;
844 struct ib_uxrcd_object *obj;
848 if (copy_from_user(&cmd, buf, sizeof cmd))
851 mutex_lock(&file->device->xrcd_tree_mutex);
852 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
860 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
861 if (atomic_read(&obj->refcnt)) {
862 put_uobj_write(uobj);
867 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
868 ret = ib_dealloc_xrcd(uobj->object);
875 atomic_inc(&xrcd->usecnt);
877 put_uobj_write(uobj);
883 xrcd_table_delete(file->device, inode);
885 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
886 mutex_lock(&file->mutex);
887 list_del(&uobj->list);
888 mutex_unlock(&file->mutex);
894 mutex_unlock(&file->device->xrcd_tree_mutex);
898 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
899 struct ib_xrcd *xrcd)
904 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
907 ib_dealloc_xrcd(xrcd);
910 xrcd_table_delete(dev, inode);
913 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
914 const char __user *buf, int in_len,
917 struct ib_uverbs_reg_mr cmd;
918 struct ib_uverbs_reg_mr_resp resp;
919 struct ib_udata udata;
920 struct ib_uobject *uobj;
925 if (out_len < sizeof resp)
928 if (copy_from_user(&cmd, buf, sizeof cmd))
931 INIT_UDATA(&udata, buf + sizeof cmd,
932 (unsigned long) cmd.response + sizeof resp,
933 in_len - sizeof cmd, out_len - sizeof resp);
935 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
939 * Local write permission is required if remote write or
940 * remote atomic permission is also requested.
942 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
943 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
946 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
950 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
951 down_write(&uobj->mutex);
953 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
959 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
960 cmd.access_flags, &udata);
966 mr->device = pd->device;
969 atomic_inc(&pd->usecnt);
970 atomic_set(&mr->usecnt, 0);
973 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
977 memset(&resp, 0, sizeof resp);
978 resp.lkey = mr->lkey;
979 resp.rkey = mr->rkey;
980 resp.mr_handle = uobj->id;
982 if (copy_to_user((void __user *) (unsigned long) cmd.response,
983 &resp, sizeof resp)) {
990 mutex_lock(&file->mutex);
991 list_add_tail(&uobj->list, &file->ucontext->mr_list);
992 mutex_unlock(&file->mutex);
996 up_write(&uobj->mutex);
1001 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1010 put_uobj_write(uobj);
1014 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1015 const char __user *buf, int in_len,
1018 struct ib_uverbs_dereg_mr cmd;
1020 struct ib_uobject *uobj;
1023 if (copy_from_user(&cmd, buf, sizeof cmd))
1026 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1032 ret = ib_dereg_mr(mr);
1036 put_uobj_write(uobj);
1041 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1043 mutex_lock(&file->mutex);
1044 list_del(&uobj->list);
1045 mutex_unlock(&file->mutex);
1052 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
1053 const char __user *buf, int in_len,
1056 struct ib_uverbs_alloc_mw cmd;
1057 struct ib_uverbs_alloc_mw_resp resp;
1058 struct ib_uobject *uobj;
1063 if (out_len < sizeof(resp))
1066 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1069 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1073 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1074 down_write(&uobj->mutex);
1076 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1082 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1088 mw->device = pd->device;
1091 atomic_inc(&pd->usecnt);
1094 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1098 memset(&resp, 0, sizeof(resp));
1099 resp.rkey = mw->rkey;
1100 resp.mw_handle = uobj->id;
1102 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1103 &resp, sizeof(resp))) {
1110 mutex_lock(&file->mutex);
1111 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1112 mutex_unlock(&file->mutex);
1116 up_write(&uobj->mutex);
1121 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1130 put_uobj_write(uobj);
1134 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
1135 const char __user *buf, int in_len,
1138 struct ib_uverbs_dealloc_mw cmd;
1140 struct ib_uobject *uobj;
1143 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1146 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1152 ret = ib_dealloc_mw(mw);
1156 put_uobj_write(uobj);
1161 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1163 mutex_lock(&file->mutex);
1164 list_del(&uobj->list);
1165 mutex_unlock(&file->mutex);
1172 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1173 const char __user *buf, int in_len,
1176 struct ib_uverbs_create_comp_channel cmd;
1177 struct ib_uverbs_create_comp_channel_resp resp;
1181 if (out_len < sizeof resp)
1184 if (copy_from_user(&cmd, buf, sizeof cmd))
1187 ret = get_unused_fd();
1192 filp = ib_uverbs_alloc_event_file(file, 0);
1194 put_unused_fd(resp.fd);
1195 return PTR_ERR(filp);
1198 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1199 &resp, sizeof resp)) {
1200 put_unused_fd(resp.fd);
1205 fd_install(resp.fd, filp);
1209 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1210 const char __user *buf, int in_len,
1213 struct ib_uverbs_create_cq cmd;
1214 struct ib_uverbs_create_cq_resp resp;
1215 struct ib_udata udata;
1216 struct ib_ucq_object *obj;
1217 struct ib_uverbs_event_file *ev_file = NULL;
1221 if (out_len < sizeof resp)
1224 if (copy_from_user(&cmd, buf, sizeof cmd))
1227 INIT_UDATA(&udata, buf + sizeof cmd,
1228 (unsigned long) cmd.response + sizeof resp,
1229 in_len - sizeof cmd, out_len - sizeof resp);
1231 if (cmd.comp_vector >= file->device->num_comp_vectors)
1234 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1238 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1239 down_write(&obj->uobject.mutex);
1241 if (cmd.comp_channel >= 0) {
1242 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1249 obj->uverbs_file = file;
1250 obj->comp_events_reported = 0;
1251 obj->async_events_reported = 0;
1252 INIT_LIST_HEAD(&obj->comp_list);
1253 INIT_LIST_HEAD(&obj->async_list);
1255 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1257 file->ucontext, &udata);
1263 cq->device = file->device->ib_dev;
1264 cq->uobject = &obj->uobject;
1265 cq->comp_handler = ib_uverbs_comp_handler;
1266 cq->event_handler = ib_uverbs_cq_event_handler;
1267 cq->cq_context = ev_file;
1268 atomic_set(&cq->usecnt, 0);
1270 obj->uobject.object = cq;
1271 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1275 memset(&resp, 0, sizeof resp);
1276 resp.cq_handle = obj->uobject.id;
1279 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1280 &resp, sizeof resp)) {
1285 mutex_lock(&file->mutex);
1286 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1287 mutex_unlock(&file->mutex);
1289 obj->uobject.live = 1;
1291 up_write(&obj->uobject.mutex);
1296 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1303 ib_uverbs_release_ucq(file, ev_file, obj);
1306 put_uobj_write(&obj->uobject);
1310 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1311 const char __user *buf, int in_len,
1314 struct ib_uverbs_resize_cq cmd;
1315 struct ib_uverbs_resize_cq_resp resp;
1316 struct ib_udata udata;
1320 if (copy_from_user(&cmd, buf, sizeof cmd))
1323 INIT_UDATA(&udata, buf + sizeof cmd,
1324 (unsigned long) cmd.response + sizeof resp,
1325 in_len - sizeof cmd, out_len - sizeof resp);
1327 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1331 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1337 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1338 &resp, sizeof resp.cqe))
1344 return ret ? ret : in_len;
1347 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1349 struct ib_uverbs_wc tmp;
1351 tmp.wr_id = wc->wr_id;
1352 tmp.status = wc->status;
1353 tmp.opcode = wc->opcode;
1354 tmp.vendor_err = wc->vendor_err;
1355 tmp.byte_len = wc->byte_len;
1356 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1357 tmp.qp_num = wc->qp->qp_num;
1358 tmp.src_qp = wc->src_qp;
1359 tmp.wc_flags = wc->wc_flags;
1360 tmp.pkey_index = wc->pkey_index;
1361 tmp.slid = wc->slid;
1363 tmp.dlid_path_bits = wc->dlid_path_bits;
1364 tmp.port_num = wc->port_num;
1367 if (copy_to_user(dest, &tmp, sizeof tmp))
1373 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1374 const char __user *buf, int in_len,
1377 struct ib_uverbs_poll_cq cmd;
1378 struct ib_uverbs_poll_cq_resp resp;
1379 u8 __user *header_ptr;
1380 u8 __user *data_ptr;
1385 if (copy_from_user(&cmd, buf, sizeof cmd))
1388 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1392 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1393 header_ptr = (void __user *)(unsigned long) cmd.response;
1394 data_ptr = header_ptr + sizeof resp;
1396 memset(&resp, 0, sizeof resp);
1397 while (resp.count < cmd.ne) {
1398 ret = ib_poll_cq(cq, 1, &wc);
1404 ret = copy_wc_to_user(data_ptr, &wc);
1408 data_ptr += sizeof(struct ib_uverbs_wc);
1412 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1424 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1425 const char __user *buf, int in_len,
1428 struct ib_uverbs_req_notify_cq cmd;
1431 if (copy_from_user(&cmd, buf, sizeof cmd))
1434 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1438 ib_req_notify_cq(cq, cmd.solicited_only ?
1439 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1446 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1447 const char __user *buf, int in_len,
1450 struct ib_uverbs_destroy_cq cmd;
1451 struct ib_uverbs_destroy_cq_resp resp;
1452 struct ib_uobject *uobj;
1454 struct ib_ucq_object *obj;
1455 struct ib_uverbs_event_file *ev_file;
1458 if (copy_from_user(&cmd, buf, sizeof cmd))
1461 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1465 ev_file = cq->cq_context;
1466 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1468 ret = ib_destroy_cq(cq);
1472 put_uobj_write(uobj);
1477 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1479 mutex_lock(&file->mutex);
1480 list_del(&uobj->list);
1481 mutex_unlock(&file->mutex);
1483 ib_uverbs_release_ucq(file, ev_file, obj);
1485 memset(&resp, 0, sizeof resp);
1486 resp.comp_events_reported = obj->comp_events_reported;
1487 resp.async_events_reported = obj->async_events_reported;
1491 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1492 &resp, sizeof resp))
1498 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1499 const char __user *buf, int in_len,
1502 struct ib_uverbs_create_qp cmd;
1503 struct ib_uverbs_create_qp_resp resp;
1504 struct ib_udata udata;
1505 struct ib_uqp_object *obj;
1506 struct ib_device *device;
1507 struct ib_pd *pd = NULL;
1508 struct ib_xrcd *xrcd = NULL;
1509 struct ib_uobject *uninitialized_var(xrcd_uobj);
1510 struct ib_cq *scq = NULL, *rcq = NULL;
1511 struct ib_srq *srq = NULL;
1513 struct ib_qp_init_attr attr;
1516 if (out_len < sizeof resp)
1519 if (copy_from_user(&cmd, buf, sizeof cmd))
1522 if (cmd.qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1525 INIT_UDATA(&udata, buf + sizeof cmd,
1526 (unsigned long) cmd.response + sizeof resp,
1527 in_len - sizeof cmd, out_len - sizeof resp);
1529 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1533 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1534 down_write(&obj->uevent.uobject.mutex);
1536 if (cmd.qp_type == IB_QPT_XRC_TGT) {
1537 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1542 device = xrcd->device;
1544 if (cmd.qp_type == IB_QPT_XRC_INI) {
1545 cmd.max_recv_wr = cmd.max_recv_sge = 0;
1548 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1549 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1555 if (cmd.recv_cq_handle != cmd.send_cq_handle) {
1556 rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext, 0);
1564 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, !!rcq);
1566 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1572 device = pd->device;
1575 attr.event_handler = ib_uverbs_qp_event_handler;
1576 attr.qp_context = file;
1581 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1582 attr.qp_type = cmd.qp_type;
1583 attr.create_flags = 0;
1585 attr.cap.max_send_wr = cmd.max_send_wr;
1586 attr.cap.max_recv_wr = cmd.max_recv_wr;
1587 attr.cap.max_send_sge = cmd.max_send_sge;
1588 attr.cap.max_recv_sge = cmd.max_recv_sge;
1589 attr.cap.max_inline_data = cmd.max_inline_data;
1591 obj->uevent.events_reported = 0;
1592 INIT_LIST_HEAD(&obj->uevent.event_list);
1593 INIT_LIST_HEAD(&obj->mcast_list);
1595 if (cmd.qp_type == IB_QPT_XRC_TGT)
1596 qp = ib_create_qp(pd, &attr);
1598 qp = device->create_qp(pd, &attr, &udata);
1605 if (cmd.qp_type != IB_QPT_XRC_TGT) {
1607 qp->device = device;
1609 qp->send_cq = attr.send_cq;
1610 qp->recv_cq = attr.recv_cq;
1612 qp->event_handler = attr.event_handler;
1613 qp->qp_context = attr.qp_context;
1614 qp->qp_type = attr.qp_type;
1615 atomic_set(&qp->usecnt, 0);
1616 atomic_inc(&pd->usecnt);
1617 atomic_inc(&attr.send_cq->usecnt);
1619 atomic_inc(&attr.recv_cq->usecnt);
1621 atomic_inc(&attr.srq->usecnt);
1623 qp->uobject = &obj->uevent.uobject;
1625 obj->uevent.uobject.object = qp;
1626 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1630 memset(&resp, 0, sizeof resp);
1631 resp.qpn = qp->qp_num;
1632 resp.qp_handle = obj->uevent.uobject.id;
1633 resp.max_recv_sge = attr.cap.max_recv_sge;
1634 resp.max_send_sge = attr.cap.max_send_sge;
1635 resp.max_recv_wr = attr.cap.max_recv_wr;
1636 resp.max_send_wr = attr.cap.max_send_wr;
1637 resp.max_inline_data = attr.cap.max_inline_data;
1639 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1640 &resp, sizeof resp)) {
1646 put_xrcd_read(xrcd_uobj);
1651 if (rcq && rcq != scq)
1656 mutex_lock(&file->mutex);
1657 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1658 mutex_unlock(&file->mutex);
1660 obj->uevent.uobject.live = 1;
1662 up_write(&obj->uevent.uobject.mutex);
1667 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1674 put_xrcd_read(xrcd_uobj);
1679 if (rcq && rcq != scq)
1684 put_uobj_write(&obj->uevent.uobject);
1688 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1689 const char __user *buf, int in_len, int out_len)
1691 struct ib_uverbs_open_qp cmd;
1692 struct ib_uverbs_create_qp_resp resp;
1693 struct ib_udata udata;
1694 struct ib_uqp_object *obj;
1695 struct ib_xrcd *xrcd;
1696 struct ib_uobject *uninitialized_var(xrcd_uobj);
1698 struct ib_qp_open_attr attr;
1701 if (out_len < sizeof resp)
1704 if (copy_from_user(&cmd, buf, sizeof cmd))
1707 INIT_UDATA(&udata, buf + sizeof cmd,
1708 (unsigned long) cmd.response + sizeof resp,
1709 in_len - sizeof cmd, out_len - sizeof resp);
1711 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1715 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1716 down_write(&obj->uevent.uobject.mutex);
1718 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1724 attr.event_handler = ib_uverbs_qp_event_handler;
1725 attr.qp_context = file;
1726 attr.qp_num = cmd.qpn;
1727 attr.qp_type = cmd.qp_type;
1729 obj->uevent.events_reported = 0;
1730 INIT_LIST_HEAD(&obj->uevent.event_list);
1731 INIT_LIST_HEAD(&obj->mcast_list);
1733 qp = ib_open_qp(xrcd, &attr);
1739 qp->uobject = &obj->uevent.uobject;
1741 obj->uevent.uobject.object = qp;
1742 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1746 memset(&resp, 0, sizeof resp);
1747 resp.qpn = qp->qp_num;
1748 resp.qp_handle = obj->uevent.uobject.id;
1750 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1751 &resp, sizeof resp)) {
1756 put_xrcd_read(xrcd_uobj);
1758 mutex_lock(&file->mutex);
1759 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1760 mutex_unlock(&file->mutex);
1762 obj->uevent.uobject.live = 1;
1764 up_write(&obj->uevent.uobject.mutex);
1769 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1775 put_xrcd_read(xrcd_uobj);
1776 put_uobj_write(&obj->uevent.uobject);
1780 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1781 const char __user *buf, int in_len,
1784 struct ib_uverbs_query_qp cmd;
1785 struct ib_uverbs_query_qp_resp resp;
1787 struct ib_qp_attr *attr;
1788 struct ib_qp_init_attr *init_attr;
1791 if (copy_from_user(&cmd, buf, sizeof cmd))
1794 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1795 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1796 if (!attr || !init_attr) {
1801 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1807 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1814 memset(&resp, 0, sizeof resp);
1816 resp.qp_state = attr->qp_state;
1817 resp.cur_qp_state = attr->cur_qp_state;
1818 resp.path_mtu = attr->path_mtu;
1819 resp.path_mig_state = attr->path_mig_state;
1820 resp.qkey = attr->qkey;
1821 resp.rq_psn = attr->rq_psn;
1822 resp.sq_psn = attr->sq_psn;
1823 resp.dest_qp_num = attr->dest_qp_num;
1824 resp.qp_access_flags = attr->qp_access_flags;
1825 resp.pkey_index = attr->pkey_index;
1826 resp.alt_pkey_index = attr->alt_pkey_index;
1827 resp.sq_draining = attr->sq_draining;
1828 resp.max_rd_atomic = attr->max_rd_atomic;
1829 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1830 resp.min_rnr_timer = attr->min_rnr_timer;
1831 resp.port_num = attr->port_num;
1832 resp.timeout = attr->timeout;
1833 resp.retry_cnt = attr->retry_cnt;
1834 resp.rnr_retry = attr->rnr_retry;
1835 resp.alt_port_num = attr->alt_port_num;
1836 resp.alt_timeout = attr->alt_timeout;
1838 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1839 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1840 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1841 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1842 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1843 resp.dest.dlid = attr->ah_attr.dlid;
1844 resp.dest.sl = attr->ah_attr.sl;
1845 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1846 resp.dest.static_rate = attr->ah_attr.static_rate;
1847 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1848 resp.dest.port_num = attr->ah_attr.port_num;
1850 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1851 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1852 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1853 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1854 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1855 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1856 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1857 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1858 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1859 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1860 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1862 resp.max_send_wr = init_attr->cap.max_send_wr;
1863 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1864 resp.max_send_sge = init_attr->cap.max_send_sge;
1865 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1866 resp.max_inline_data = init_attr->cap.max_inline_data;
1867 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1869 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1870 &resp, sizeof resp))
1877 return ret ? ret : in_len;
1880 /* Remove ignored fields set in the attribute mask */
1881 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1884 case IB_QPT_XRC_INI:
1885 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1886 case IB_QPT_XRC_TGT:
1887 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1894 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1895 const char __user *buf, int in_len,
1898 struct ib_uverbs_modify_qp cmd;
1899 struct ib_udata udata;
1901 struct ib_qp_attr *attr;
1904 if (copy_from_user(&cmd, buf, sizeof cmd))
1907 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1910 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1914 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1920 attr->qp_state = cmd.qp_state;
1921 attr->cur_qp_state = cmd.cur_qp_state;
1922 attr->path_mtu = cmd.path_mtu;
1923 attr->path_mig_state = cmd.path_mig_state;
1924 attr->qkey = cmd.qkey;
1925 attr->rq_psn = cmd.rq_psn;
1926 attr->sq_psn = cmd.sq_psn;
1927 attr->dest_qp_num = cmd.dest_qp_num;
1928 attr->qp_access_flags = cmd.qp_access_flags;
1929 attr->pkey_index = cmd.pkey_index;
1930 attr->alt_pkey_index = cmd.alt_pkey_index;
1931 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1932 attr->max_rd_atomic = cmd.max_rd_atomic;
1933 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1934 attr->min_rnr_timer = cmd.min_rnr_timer;
1935 attr->port_num = cmd.port_num;
1936 attr->timeout = cmd.timeout;
1937 attr->retry_cnt = cmd.retry_cnt;
1938 attr->rnr_retry = cmd.rnr_retry;
1939 attr->alt_port_num = cmd.alt_port_num;
1940 attr->alt_timeout = cmd.alt_timeout;
1942 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1943 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1944 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1945 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1946 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1947 attr->ah_attr.dlid = cmd.dest.dlid;
1948 attr->ah_attr.sl = cmd.dest.sl;
1949 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1950 attr->ah_attr.static_rate = cmd.dest.static_rate;
1951 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1952 attr->ah_attr.port_num = cmd.dest.port_num;
1954 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1955 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1956 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1957 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1958 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1959 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1960 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1961 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1962 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1963 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1964 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1966 if (qp->real_qp == qp) {
1967 ret = qp->device->modify_qp(qp, attr,
1968 modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
1970 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
1986 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1987 const char __user *buf, int in_len,
1990 struct ib_uverbs_destroy_qp cmd;
1991 struct ib_uverbs_destroy_qp_resp resp;
1992 struct ib_uobject *uobj;
1994 struct ib_uqp_object *obj;
1997 if (copy_from_user(&cmd, buf, sizeof cmd))
2000 memset(&resp, 0, sizeof resp);
2002 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
2006 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2008 if (!list_empty(&obj->mcast_list)) {
2009 put_uobj_write(uobj);
2013 ret = ib_destroy_qp(qp);
2017 put_uobj_write(uobj);
2022 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
2024 mutex_lock(&file->mutex);
2025 list_del(&uobj->list);
2026 mutex_unlock(&file->mutex);
2028 ib_uverbs_release_uevent(file, &obj->uevent);
2030 resp.events_reported = obj->uevent.events_reported;
2034 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2035 &resp, sizeof resp))
2041 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2042 const char __user *buf, int in_len,
2045 struct ib_uverbs_post_send cmd;
2046 struct ib_uverbs_post_send_resp resp;
2047 struct ib_uverbs_send_wr *user_wr;
2048 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2052 ssize_t ret = -EINVAL;
2054 if (copy_from_user(&cmd, buf, sizeof cmd))
2057 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2058 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2061 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2064 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2068 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2072 is_ud = qp->qp_type == IB_QPT_UD;
2075 for (i = 0; i < cmd.wr_count; ++i) {
2076 if (copy_from_user(user_wr,
2077 buf + sizeof cmd + i * cmd.wqe_size,
2083 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2088 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2089 user_wr->num_sge * sizeof (struct ib_sge),
2103 next->wr_id = user_wr->wr_id;
2104 next->num_sge = user_wr->num_sge;
2105 next->opcode = user_wr->opcode;
2106 next->send_flags = user_wr->send_flags;
2109 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
2111 if (!next->wr.ud.ah) {
2115 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
2116 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
2118 switch (next->opcode) {
2119 case IB_WR_RDMA_WRITE_WITH_IMM:
2121 (__be32 __force) user_wr->ex.imm_data;
2122 case IB_WR_RDMA_WRITE:
2123 case IB_WR_RDMA_READ:
2124 next->wr.rdma.remote_addr =
2125 user_wr->wr.rdma.remote_addr;
2126 next->wr.rdma.rkey =
2127 user_wr->wr.rdma.rkey;
2129 case IB_WR_SEND_WITH_IMM:
2131 (__be32 __force) user_wr->ex.imm_data;
2133 case IB_WR_SEND_WITH_INV:
2134 next->ex.invalidate_rkey =
2135 user_wr->ex.invalidate_rkey;
2137 case IB_WR_ATOMIC_CMP_AND_SWP:
2138 case IB_WR_ATOMIC_FETCH_AND_ADD:
2139 next->wr.atomic.remote_addr =
2140 user_wr->wr.atomic.remote_addr;
2141 next->wr.atomic.compare_add =
2142 user_wr->wr.atomic.compare_add;
2143 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2144 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2151 if (next->num_sge) {
2152 next->sg_list = (void *) next +
2153 ALIGN(sizeof *next, sizeof (struct ib_sge));
2154 if (copy_from_user(next->sg_list,
2156 cmd.wr_count * cmd.wqe_size +
2157 sg_ind * sizeof (struct ib_sge),
2158 next->num_sge * sizeof (struct ib_sge))) {
2162 sg_ind += next->num_sge;
2164 next->sg_list = NULL;
2168 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2170 for (next = wr; next; next = next->next) {
2176 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2177 &resp, sizeof resp))
2184 if (is_ud && wr->wr.ud.ah)
2185 put_ah_read(wr->wr.ud.ah);
2194 return ret ? ret : in_len;
2197 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2203 struct ib_uverbs_recv_wr *user_wr;
2204 struct ib_recv_wr *wr = NULL, *last, *next;
2209 if (in_len < wqe_size * wr_count +
2210 sge_count * sizeof (struct ib_uverbs_sge))
2211 return ERR_PTR(-EINVAL);
2213 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2214 return ERR_PTR(-EINVAL);
2216 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2218 return ERR_PTR(-ENOMEM);
2222 for (i = 0; i < wr_count; ++i) {
2223 if (copy_from_user(user_wr, buf + i * wqe_size,
2229 if (user_wr->num_sge + sg_ind > sge_count) {
2234 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2235 user_wr->num_sge * sizeof (struct ib_sge),
2249 next->wr_id = user_wr->wr_id;
2250 next->num_sge = user_wr->num_sge;
2252 if (next->num_sge) {
2253 next->sg_list = (void *) next +
2254 ALIGN(sizeof *next, sizeof (struct ib_sge));
2255 if (copy_from_user(next->sg_list,
2256 buf + wr_count * wqe_size +
2257 sg_ind * sizeof (struct ib_sge),
2258 next->num_sge * sizeof (struct ib_sge))) {
2262 sg_ind += next->num_sge;
2264 next->sg_list = NULL;
2279 return ERR_PTR(ret);
2282 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2283 const char __user *buf, int in_len,
2286 struct ib_uverbs_post_recv cmd;
2287 struct ib_uverbs_post_recv_resp resp;
2288 struct ib_recv_wr *wr, *next, *bad_wr;
2290 ssize_t ret = -EINVAL;
2292 if (copy_from_user(&cmd, buf, sizeof cmd))
2295 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2296 in_len - sizeof cmd, cmd.wr_count,
2297 cmd.sge_count, cmd.wqe_size);
2301 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2306 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2311 for (next = wr; next; next = next->next) {
2317 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2318 &resp, sizeof resp))
2328 return ret ? ret : in_len;
2331 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2332 const char __user *buf, int in_len,
2335 struct ib_uverbs_post_srq_recv cmd;
2336 struct ib_uverbs_post_srq_recv_resp resp;
2337 struct ib_recv_wr *wr, *next, *bad_wr;
2339 ssize_t ret = -EINVAL;
2341 if (copy_from_user(&cmd, buf, sizeof cmd))
2344 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2345 in_len - sizeof cmd, cmd.wr_count,
2346 cmd.sge_count, cmd.wqe_size);
2350 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2355 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2360 for (next = wr; next; next = next->next) {
2366 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2367 &resp, sizeof resp))
2377 return ret ? ret : in_len;
2380 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2381 const char __user *buf, int in_len,
2384 struct ib_uverbs_create_ah cmd;
2385 struct ib_uverbs_create_ah_resp resp;
2386 struct ib_uobject *uobj;
2389 struct ib_ah_attr attr;
2392 if (out_len < sizeof resp)
2395 if (copy_from_user(&cmd, buf, sizeof cmd))
2398 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2402 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2403 down_write(&uobj->mutex);
2405 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2411 attr.dlid = cmd.attr.dlid;
2412 attr.sl = cmd.attr.sl;
2413 attr.src_path_bits = cmd.attr.src_path_bits;
2414 attr.static_rate = cmd.attr.static_rate;
2415 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
2416 attr.port_num = cmd.attr.port_num;
2417 attr.grh.flow_label = cmd.attr.grh.flow_label;
2418 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2419 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2420 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2421 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2423 ah = ib_create_ah(pd, &attr);
2432 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2436 resp.ah_handle = uobj->id;
2438 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2439 &resp, sizeof resp)) {
2446 mutex_lock(&file->mutex);
2447 list_add_tail(&uobj->list, &file->ucontext->ah_list);
2448 mutex_unlock(&file->mutex);
2452 up_write(&uobj->mutex);
2457 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2466 put_uobj_write(uobj);
2470 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2471 const char __user *buf, int in_len, int out_len)
2473 struct ib_uverbs_destroy_ah cmd;
2475 struct ib_uobject *uobj;
2478 if (copy_from_user(&cmd, buf, sizeof cmd))
2481 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2486 ret = ib_destroy_ah(ah);
2490 put_uobj_write(uobj);
2495 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2497 mutex_lock(&file->mutex);
2498 list_del(&uobj->list);
2499 mutex_unlock(&file->mutex);
2506 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2507 const char __user *buf, int in_len,
2510 struct ib_uverbs_attach_mcast cmd;
2512 struct ib_uqp_object *obj;
2513 struct ib_uverbs_mcast_entry *mcast;
2516 if (copy_from_user(&cmd, buf, sizeof cmd))
2519 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2523 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2525 list_for_each_entry(mcast, &obj->mcast_list, list)
2526 if (cmd.mlid == mcast->lid &&
2527 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2532 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2538 mcast->lid = cmd.mlid;
2539 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2541 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2543 list_add_tail(&mcast->list, &obj->mcast_list);
2550 return ret ? ret : in_len;
2553 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2554 const char __user *buf, int in_len,
2557 struct ib_uverbs_detach_mcast cmd;
2558 struct ib_uqp_object *obj;
2560 struct ib_uverbs_mcast_entry *mcast;
2563 if (copy_from_user(&cmd, buf, sizeof cmd))
2566 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2570 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2574 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2576 list_for_each_entry(mcast, &obj->mcast_list, list)
2577 if (cmd.mlid == mcast->lid &&
2578 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2579 list_del(&mcast->list);
2587 return ret ? ret : in_len;
2590 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2591 struct ib_uverbs_create_xsrq *cmd,
2592 struct ib_udata *udata)
2594 struct ib_uverbs_create_srq_resp resp;
2595 struct ib_usrq_object *obj;
2598 struct ib_uobject *uninitialized_var(xrcd_uobj);
2599 struct ib_srq_init_attr attr;
2602 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2606 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2607 down_write(&obj->uevent.uobject.mutex);
2609 if (cmd->srq_type == IB_SRQT_XRC) {
2610 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2611 if (!attr.ext.xrc.xrcd) {
2616 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2617 atomic_inc(&obj->uxrcd->refcnt);
2619 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2620 if (!attr.ext.xrc.cq) {
2626 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
2632 attr.event_handler = ib_uverbs_srq_event_handler;
2633 attr.srq_context = file;
2634 attr.srq_type = cmd->srq_type;
2635 attr.attr.max_wr = cmd->max_wr;
2636 attr.attr.max_sge = cmd->max_sge;
2637 attr.attr.srq_limit = cmd->srq_limit;
2639 obj->uevent.events_reported = 0;
2640 INIT_LIST_HEAD(&obj->uevent.event_list);
2642 srq = pd->device->create_srq(pd, &attr, udata);
2648 srq->device = pd->device;
2650 srq->srq_type = cmd->srq_type;
2651 srq->uobject = &obj->uevent.uobject;
2652 srq->event_handler = attr.event_handler;
2653 srq->srq_context = attr.srq_context;
2655 if (cmd->srq_type == IB_SRQT_XRC) {
2656 srq->ext.xrc.cq = attr.ext.xrc.cq;
2657 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2658 atomic_inc(&attr.ext.xrc.cq->usecnt);
2659 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2662 atomic_inc(&pd->usecnt);
2663 atomic_set(&srq->usecnt, 0);
2665 obj->uevent.uobject.object = srq;
2666 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2670 memset(&resp, 0, sizeof resp);
2671 resp.srq_handle = obj->uevent.uobject.id;
2672 resp.max_wr = attr.attr.max_wr;
2673 resp.max_sge = attr.attr.max_sge;
2674 if (cmd->srq_type == IB_SRQT_XRC)
2675 resp.srqn = srq->ext.xrc.srq_num;
2677 if (copy_to_user((void __user *) (unsigned long) cmd->response,
2678 &resp, sizeof resp)) {
2683 if (cmd->srq_type == IB_SRQT_XRC) {
2684 put_uobj_read(xrcd_uobj);
2685 put_cq_read(attr.ext.xrc.cq);
2689 mutex_lock(&file->mutex);
2690 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2691 mutex_unlock(&file->mutex);
2693 obj->uevent.uobject.live = 1;
2695 up_write(&obj->uevent.uobject.mutex);
2700 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2703 ib_destroy_srq(srq);
2709 if (cmd->srq_type == IB_SRQT_XRC)
2710 put_cq_read(attr.ext.xrc.cq);
2713 if (cmd->srq_type == IB_SRQT_XRC) {
2714 atomic_dec(&obj->uxrcd->refcnt);
2715 put_uobj_read(xrcd_uobj);
2719 put_uobj_write(&obj->uevent.uobject);
2723 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2724 const char __user *buf, int in_len,
2727 struct ib_uverbs_create_srq cmd;
2728 struct ib_uverbs_create_xsrq xcmd;
2729 struct ib_uverbs_create_srq_resp resp;
2730 struct ib_udata udata;
2733 if (out_len < sizeof resp)
2736 if (copy_from_user(&cmd, buf, sizeof cmd))
2739 xcmd.response = cmd.response;
2740 xcmd.user_handle = cmd.user_handle;
2741 xcmd.srq_type = IB_SRQT_BASIC;
2742 xcmd.pd_handle = cmd.pd_handle;
2743 xcmd.max_wr = cmd.max_wr;
2744 xcmd.max_sge = cmd.max_sge;
2745 xcmd.srq_limit = cmd.srq_limit;
2747 INIT_UDATA(&udata, buf + sizeof cmd,
2748 (unsigned long) cmd.response + sizeof resp,
2749 in_len - sizeof cmd, out_len - sizeof resp);
2751 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2758 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2759 const char __user *buf, int in_len, int out_len)
2761 struct ib_uverbs_create_xsrq cmd;
2762 struct ib_uverbs_create_srq_resp resp;
2763 struct ib_udata udata;
2766 if (out_len < sizeof resp)
2769 if (copy_from_user(&cmd, buf, sizeof cmd))
2772 INIT_UDATA(&udata, buf + sizeof cmd,
2773 (unsigned long) cmd.response + sizeof resp,
2774 in_len - sizeof cmd, out_len - sizeof resp);
2776 ret = __uverbs_create_xsrq(file, &cmd, &udata);
2783 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2784 const char __user *buf, int in_len,
2787 struct ib_uverbs_modify_srq cmd;
2788 struct ib_udata udata;
2790 struct ib_srq_attr attr;
2793 if (copy_from_user(&cmd, buf, sizeof cmd))
2796 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2799 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2803 attr.max_wr = cmd.max_wr;
2804 attr.srq_limit = cmd.srq_limit;
2806 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2810 return ret ? ret : in_len;
2813 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2814 const char __user *buf,
2815 int in_len, int out_len)
2817 struct ib_uverbs_query_srq cmd;
2818 struct ib_uverbs_query_srq_resp resp;
2819 struct ib_srq_attr attr;
2823 if (out_len < sizeof resp)
2826 if (copy_from_user(&cmd, buf, sizeof cmd))
2829 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2833 ret = ib_query_srq(srq, &attr);
2840 memset(&resp, 0, sizeof resp);
2842 resp.max_wr = attr.max_wr;
2843 resp.max_sge = attr.max_sge;
2844 resp.srq_limit = attr.srq_limit;
2846 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2847 &resp, sizeof resp))
2853 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2854 const char __user *buf, int in_len,
2857 struct ib_uverbs_destroy_srq cmd;
2858 struct ib_uverbs_destroy_srq_resp resp;
2859 struct ib_uobject *uobj;
2861 struct ib_uevent_object *obj;
2864 if (copy_from_user(&cmd, buf, sizeof cmd))
2867 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2871 obj = container_of(uobj, struct ib_uevent_object, uobject);
2873 ret = ib_destroy_srq(srq);
2877 put_uobj_write(uobj);
2882 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2884 mutex_lock(&file->mutex);
2885 list_del(&uobj->list);
2886 mutex_unlock(&file->mutex);
2888 ib_uverbs_release_uevent(file, obj);
2890 memset(&resp, 0, sizeof resp);
2891 resp.events_reported = obj->events_reported;
2895 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2896 &resp, sizeof resp))
2899 return ret ? ret : in_len;