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)
129 if (!idr_pre_get(idr, GFP_KERNEL))
132 spin_lock(&ib_uverbs_idr_lock);
133 ret = idr_get_new(idr, uobj, &uobj->id);
134 spin_unlock(&ib_uverbs_idr_lock);
142 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
144 spin_lock(&ib_uverbs_idr_lock);
145 idr_remove(idr, uobj->id);
146 spin_unlock(&ib_uverbs_idr_lock);
149 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
150 struct ib_ucontext *context)
152 struct ib_uobject *uobj;
154 spin_lock(&ib_uverbs_idr_lock);
155 uobj = idr_find(idr, id);
157 if (uobj->context == context)
158 kref_get(&uobj->ref);
162 spin_unlock(&ib_uverbs_idr_lock);
167 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
168 struct ib_ucontext *context, int nested)
170 struct ib_uobject *uobj;
172 uobj = __idr_get_uobj(idr, id, context);
177 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
179 down_read(&uobj->mutex);
188 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
189 struct ib_ucontext *context)
191 struct ib_uobject *uobj;
193 uobj = __idr_get_uobj(idr, id, context);
197 down_write(&uobj->mutex);
199 put_uobj_write(uobj);
206 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
209 struct ib_uobject *uobj;
211 uobj = idr_read_uobj(idr, id, context, nested);
212 return uobj ? uobj->object : NULL;
215 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
217 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
220 static void put_pd_read(struct ib_pd *pd)
222 put_uobj_read(pd->uobject);
225 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
227 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
230 static void put_cq_read(struct ib_cq *cq)
232 put_uobj_read(cq->uobject);
235 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
237 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
240 static void put_ah_read(struct ib_ah *ah)
242 put_uobj_read(ah->uobject);
245 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
247 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
250 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
252 struct ib_uobject *uobj;
254 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
255 return uobj ? uobj->object : NULL;
258 static void put_qp_read(struct ib_qp *qp)
260 put_uobj_read(qp->uobject);
263 static void put_qp_write(struct ib_qp *qp)
265 put_uobj_write(qp->uobject);
268 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
270 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
273 static void put_srq_read(struct ib_srq *srq)
275 put_uobj_read(srq->uobject);
278 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
279 struct ib_uobject **uobj)
281 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
282 return *uobj ? (*uobj)->object : NULL;
285 static void put_xrcd_read(struct ib_uobject *uobj)
290 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
291 const char __user *buf,
292 int in_len, int out_len)
294 struct ib_uverbs_get_context cmd;
295 struct ib_uverbs_get_context_resp resp;
296 struct ib_udata udata;
297 struct ib_device *ibdev = file->device->ib_dev;
298 struct ib_ucontext *ucontext;
302 if (out_len < sizeof resp)
305 if (copy_from_user(&cmd, buf, sizeof cmd))
308 mutex_lock(&file->mutex);
310 if (file->ucontext) {
315 INIT_UDATA(&udata, buf + sizeof cmd,
316 (unsigned long) cmd.response + sizeof resp,
317 in_len - sizeof cmd, out_len - sizeof resp);
319 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
320 if (IS_ERR(ucontext)) {
321 ret = PTR_ERR(ucontext);
325 ucontext->device = ibdev;
326 INIT_LIST_HEAD(&ucontext->pd_list);
327 INIT_LIST_HEAD(&ucontext->mr_list);
328 INIT_LIST_HEAD(&ucontext->mw_list);
329 INIT_LIST_HEAD(&ucontext->cq_list);
330 INIT_LIST_HEAD(&ucontext->qp_list);
331 INIT_LIST_HEAD(&ucontext->srq_list);
332 INIT_LIST_HEAD(&ucontext->ah_list);
333 INIT_LIST_HEAD(&ucontext->xrcd_list);
334 ucontext->closing = 0;
336 resp.num_comp_vectors = file->device->num_comp_vectors;
338 ret = get_unused_fd();
343 filp = ib_uverbs_alloc_event_file(file, 1);
349 if (copy_to_user((void __user *) (unsigned long) cmd.response,
350 &resp, sizeof resp)) {
355 file->async_file = filp->private_data;
357 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
358 ib_uverbs_event_handler);
359 ret = ib_register_event_handler(&file->event_handler);
363 kref_get(&file->async_file->ref);
364 kref_get(&file->ref);
365 file->ucontext = ucontext;
367 fd_install(resp.async_fd, filp);
369 mutex_unlock(&file->mutex);
377 put_unused_fd(resp.async_fd);
380 ibdev->dealloc_ucontext(ucontext);
383 mutex_unlock(&file->mutex);
387 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
388 const char __user *buf,
389 int in_len, int out_len)
391 struct ib_uverbs_query_device cmd;
392 struct ib_uverbs_query_device_resp resp;
393 struct ib_device_attr attr;
396 if (out_len < sizeof resp)
399 if (copy_from_user(&cmd, buf, sizeof cmd))
402 ret = ib_query_device(file->device->ib_dev, &attr);
406 memset(&resp, 0, sizeof resp);
408 resp.fw_ver = attr.fw_ver;
409 resp.node_guid = file->device->ib_dev->node_guid;
410 resp.sys_image_guid = attr.sys_image_guid;
411 resp.max_mr_size = attr.max_mr_size;
412 resp.page_size_cap = attr.page_size_cap;
413 resp.vendor_id = attr.vendor_id;
414 resp.vendor_part_id = attr.vendor_part_id;
415 resp.hw_ver = attr.hw_ver;
416 resp.max_qp = attr.max_qp;
417 resp.max_qp_wr = attr.max_qp_wr;
418 resp.device_cap_flags = attr.device_cap_flags;
419 resp.max_sge = attr.max_sge;
420 resp.max_sge_rd = attr.max_sge_rd;
421 resp.max_cq = attr.max_cq;
422 resp.max_cqe = attr.max_cqe;
423 resp.max_mr = attr.max_mr;
424 resp.max_pd = attr.max_pd;
425 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
426 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
427 resp.max_res_rd_atom = attr.max_res_rd_atom;
428 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
429 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
430 resp.atomic_cap = attr.atomic_cap;
431 resp.max_ee = attr.max_ee;
432 resp.max_rdd = attr.max_rdd;
433 resp.max_mw = attr.max_mw;
434 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
435 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
436 resp.max_mcast_grp = attr.max_mcast_grp;
437 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
438 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
439 resp.max_ah = attr.max_ah;
440 resp.max_fmr = attr.max_fmr;
441 resp.max_map_per_fmr = attr.max_map_per_fmr;
442 resp.max_srq = attr.max_srq;
443 resp.max_srq_wr = attr.max_srq_wr;
444 resp.max_srq_sge = attr.max_srq_sge;
445 resp.max_pkeys = attr.max_pkeys;
446 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
447 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
449 if (copy_to_user((void __user *) (unsigned long) cmd.response,
456 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
457 const char __user *buf,
458 int in_len, int out_len)
460 struct ib_uverbs_query_port cmd;
461 struct ib_uverbs_query_port_resp resp;
462 struct ib_port_attr attr;
465 if (out_len < sizeof resp)
468 if (copy_from_user(&cmd, buf, sizeof cmd))
471 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
475 memset(&resp, 0, sizeof resp);
477 resp.state = attr.state;
478 resp.max_mtu = attr.max_mtu;
479 resp.active_mtu = attr.active_mtu;
480 resp.gid_tbl_len = attr.gid_tbl_len;
481 resp.port_cap_flags = attr.port_cap_flags;
482 resp.max_msg_sz = attr.max_msg_sz;
483 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
484 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
485 resp.pkey_tbl_len = attr.pkey_tbl_len;
487 resp.sm_lid = attr.sm_lid;
489 resp.max_vl_num = attr.max_vl_num;
490 resp.sm_sl = attr.sm_sl;
491 resp.subnet_timeout = attr.subnet_timeout;
492 resp.init_type_reply = attr.init_type_reply;
493 resp.active_width = attr.active_width;
494 resp.active_speed = attr.active_speed;
495 resp.phys_state = attr.phys_state;
496 resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
499 if (copy_to_user((void __user *) (unsigned long) cmd.response,
506 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
507 const char __user *buf,
508 int in_len, int out_len)
510 struct ib_uverbs_alloc_pd cmd;
511 struct ib_uverbs_alloc_pd_resp resp;
512 struct ib_udata udata;
513 struct ib_uobject *uobj;
517 if (out_len < sizeof resp)
520 if (copy_from_user(&cmd, buf, sizeof cmd))
523 INIT_UDATA(&udata, buf + sizeof cmd,
524 (unsigned long) cmd.response + sizeof resp,
525 in_len - sizeof cmd, out_len - sizeof resp);
527 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
531 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
532 down_write(&uobj->mutex);
534 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
535 file->ucontext, &udata);
541 pd->device = file->device->ib_dev;
543 atomic_set(&pd->usecnt, 0);
546 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
550 memset(&resp, 0, sizeof resp);
551 resp.pd_handle = uobj->id;
553 if (copy_to_user((void __user *) (unsigned long) cmd.response,
554 &resp, sizeof resp)) {
559 mutex_lock(&file->mutex);
560 list_add_tail(&uobj->list, &file->ucontext->pd_list);
561 mutex_unlock(&file->mutex);
565 up_write(&uobj->mutex);
570 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
576 put_uobj_write(uobj);
580 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
581 const char __user *buf,
582 int in_len, int out_len)
584 struct ib_uverbs_dealloc_pd cmd;
585 struct ib_uobject *uobj;
588 if (copy_from_user(&cmd, buf, sizeof cmd))
591 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
595 ret = ib_dealloc_pd(uobj->object);
599 put_uobj_write(uobj);
604 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
606 mutex_lock(&file->mutex);
607 list_del(&uobj->list);
608 mutex_unlock(&file->mutex);
615 struct xrcd_table_entry {
617 struct ib_xrcd *xrcd;
621 static int xrcd_table_insert(struct ib_uverbs_device *dev,
623 struct ib_xrcd *xrcd)
625 struct xrcd_table_entry *entry, *scan;
626 struct rb_node **p = &dev->xrcd_tree.rb_node;
627 struct rb_node *parent = NULL;
629 entry = kmalloc(sizeof *entry, GFP_KERNEL);
634 entry->inode = inode;
638 scan = rb_entry(parent, struct xrcd_table_entry, node);
640 if (inode < scan->inode) {
642 } else if (inode > scan->inode) {
650 rb_link_node(&entry->node, parent, p);
651 rb_insert_color(&entry->node, &dev->xrcd_tree);
656 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
659 struct xrcd_table_entry *entry;
660 struct rb_node *p = dev->xrcd_tree.rb_node;
663 entry = rb_entry(p, struct xrcd_table_entry, node);
665 if (inode < entry->inode)
667 else if (inode > entry->inode)
676 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
678 struct xrcd_table_entry *entry;
680 entry = xrcd_table_search(dev, inode);
687 static void xrcd_table_delete(struct ib_uverbs_device *dev,
690 struct xrcd_table_entry *entry;
692 entry = xrcd_table_search(dev, inode);
695 rb_erase(&entry->node, &dev->xrcd_tree);
700 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
701 const char __user *buf, int in_len,
704 struct ib_uverbs_open_xrcd cmd;
705 struct ib_uverbs_open_xrcd_resp resp;
706 struct ib_udata udata;
707 struct ib_uxrcd_object *obj;
708 struct ib_xrcd *xrcd = NULL;
709 struct fd f = {NULL, 0};
710 struct inode *inode = NULL;
714 if (out_len < sizeof resp)
717 if (copy_from_user(&cmd, buf, sizeof cmd))
720 INIT_UDATA(&udata, buf + sizeof cmd,
721 (unsigned long) cmd.response + sizeof resp,
722 in_len - sizeof cmd, out_len - sizeof resp);
724 mutex_lock(&file->device->xrcd_tree_mutex);
727 /* search for file descriptor */
731 goto err_tree_mutex_unlock;
734 inode = file_inode(f.file);
735 xrcd = find_xrcd(file->device, inode);
736 if (!xrcd && !(cmd.oflags & O_CREAT)) {
737 /* no file descriptor. Need CREATE flag */
739 goto err_tree_mutex_unlock;
742 if (xrcd && cmd.oflags & O_EXCL) {
744 goto err_tree_mutex_unlock;
748 obj = kmalloc(sizeof *obj, GFP_KERNEL);
751 goto err_tree_mutex_unlock;
754 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
756 down_write(&obj->uobject.mutex);
759 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
760 file->ucontext, &udata);
767 xrcd->device = file->device->ib_dev;
768 atomic_set(&xrcd->usecnt, 0);
769 mutex_init(&xrcd->tgt_qp_mutex);
770 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
774 atomic_set(&obj->refcnt, 0);
775 obj->uobject.object = xrcd;
776 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
780 memset(&resp, 0, sizeof resp);
781 resp.xrcd_handle = obj->uobject.id;
785 /* create new inode/xrcd table entry */
786 ret = xrcd_table_insert(file->device, inode, xrcd);
788 goto err_insert_xrcd;
790 atomic_inc(&xrcd->usecnt);
793 if (copy_to_user((void __user *) (unsigned long) cmd.response,
794 &resp, sizeof resp)) {
802 mutex_lock(&file->mutex);
803 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
804 mutex_unlock(&file->mutex);
806 obj->uobject.live = 1;
807 up_write(&obj->uobject.mutex);
809 mutex_unlock(&file->device->xrcd_tree_mutex);
815 xrcd_table_delete(file->device, inode);
816 atomic_dec(&xrcd->usecnt);
820 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
823 ib_dealloc_xrcd(xrcd);
826 put_uobj_write(&obj->uobject);
828 err_tree_mutex_unlock:
832 mutex_unlock(&file->device->xrcd_tree_mutex);
837 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
838 const char __user *buf, int in_len,
841 struct ib_uverbs_close_xrcd cmd;
842 struct ib_uobject *uobj;
843 struct ib_xrcd *xrcd = NULL;
844 struct inode *inode = NULL;
845 struct ib_uxrcd_object *obj;
849 if (copy_from_user(&cmd, buf, sizeof cmd))
852 mutex_lock(&file->device->xrcd_tree_mutex);
853 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
861 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
862 if (atomic_read(&obj->refcnt)) {
863 put_uobj_write(uobj);
868 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
869 ret = ib_dealloc_xrcd(uobj->object);
876 atomic_inc(&xrcd->usecnt);
878 put_uobj_write(uobj);
884 xrcd_table_delete(file->device, inode);
886 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
887 mutex_lock(&file->mutex);
888 list_del(&uobj->list);
889 mutex_unlock(&file->mutex);
895 mutex_unlock(&file->device->xrcd_tree_mutex);
899 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
900 struct ib_xrcd *xrcd)
905 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
908 ib_dealloc_xrcd(xrcd);
911 xrcd_table_delete(dev, inode);
914 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
915 const char __user *buf, int in_len,
918 struct ib_uverbs_reg_mr cmd;
919 struct ib_uverbs_reg_mr_resp resp;
920 struct ib_udata udata;
921 struct ib_uobject *uobj;
926 if (out_len < sizeof resp)
929 if (copy_from_user(&cmd, buf, sizeof cmd))
932 INIT_UDATA(&udata, buf + sizeof cmd,
933 (unsigned long) cmd.response + sizeof resp,
934 in_len - sizeof cmd, out_len - sizeof resp);
936 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
940 * Local write permission is required if remote write or
941 * remote atomic permission is also requested.
943 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
944 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
947 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
951 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
952 down_write(&uobj->mutex);
954 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
960 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
961 cmd.access_flags, &udata);
967 mr->device = pd->device;
970 atomic_inc(&pd->usecnt);
971 atomic_set(&mr->usecnt, 0);
974 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
978 memset(&resp, 0, sizeof resp);
979 resp.lkey = mr->lkey;
980 resp.rkey = mr->rkey;
981 resp.mr_handle = uobj->id;
983 if (copy_to_user((void __user *) (unsigned long) cmd.response,
984 &resp, sizeof resp)) {
991 mutex_lock(&file->mutex);
992 list_add_tail(&uobj->list, &file->ucontext->mr_list);
993 mutex_unlock(&file->mutex);
997 up_write(&uobj->mutex);
1002 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1011 put_uobj_write(uobj);
1015 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1016 const char __user *buf, int in_len,
1019 struct ib_uverbs_dereg_mr cmd;
1021 struct ib_uobject *uobj;
1024 if (copy_from_user(&cmd, buf, sizeof cmd))
1027 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1033 ret = ib_dereg_mr(mr);
1037 put_uobj_write(uobj);
1042 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1044 mutex_lock(&file->mutex);
1045 list_del(&uobj->list);
1046 mutex_unlock(&file->mutex);
1053 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
1054 const char __user *buf, int in_len,
1057 struct ib_uverbs_alloc_mw cmd;
1058 struct ib_uverbs_alloc_mw_resp resp;
1059 struct ib_uobject *uobj;
1064 if (out_len < sizeof(resp))
1067 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1070 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1074 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1075 down_write(&uobj->mutex);
1077 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1083 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1089 mw->device = pd->device;
1092 atomic_inc(&pd->usecnt);
1095 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1099 memset(&resp, 0, sizeof(resp));
1100 resp.rkey = mw->rkey;
1101 resp.mw_handle = uobj->id;
1103 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1104 &resp, sizeof(resp))) {
1111 mutex_lock(&file->mutex);
1112 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1113 mutex_unlock(&file->mutex);
1117 up_write(&uobj->mutex);
1122 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1131 put_uobj_write(uobj);
1135 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
1136 const char __user *buf, int in_len,
1139 struct ib_uverbs_dealloc_mw cmd;
1141 struct ib_uobject *uobj;
1144 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1147 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1153 ret = ib_dealloc_mw(mw);
1157 put_uobj_write(uobj);
1162 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1164 mutex_lock(&file->mutex);
1165 list_del(&uobj->list);
1166 mutex_unlock(&file->mutex);
1173 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1174 const char __user *buf, int in_len,
1177 struct ib_uverbs_create_comp_channel cmd;
1178 struct ib_uverbs_create_comp_channel_resp resp;
1182 if (out_len < sizeof resp)
1185 if (copy_from_user(&cmd, buf, sizeof cmd))
1188 ret = get_unused_fd();
1193 filp = ib_uverbs_alloc_event_file(file, 0);
1195 put_unused_fd(resp.fd);
1196 return PTR_ERR(filp);
1199 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1200 &resp, sizeof resp)) {
1201 put_unused_fd(resp.fd);
1206 fd_install(resp.fd, filp);
1210 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1211 const char __user *buf, int in_len,
1214 struct ib_uverbs_create_cq cmd;
1215 struct ib_uverbs_create_cq_resp resp;
1216 struct ib_udata udata;
1217 struct ib_ucq_object *obj;
1218 struct ib_uverbs_event_file *ev_file = NULL;
1222 if (out_len < sizeof resp)
1225 if (copy_from_user(&cmd, buf, sizeof cmd))
1228 INIT_UDATA(&udata, buf + sizeof cmd,
1229 (unsigned long) cmd.response + sizeof resp,
1230 in_len - sizeof cmd, out_len - sizeof resp);
1232 if (cmd.comp_vector >= file->device->num_comp_vectors)
1235 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1239 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1240 down_write(&obj->uobject.mutex);
1242 if (cmd.comp_channel >= 0) {
1243 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1250 obj->uverbs_file = file;
1251 obj->comp_events_reported = 0;
1252 obj->async_events_reported = 0;
1253 INIT_LIST_HEAD(&obj->comp_list);
1254 INIT_LIST_HEAD(&obj->async_list);
1256 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1258 file->ucontext, &udata);
1264 cq->device = file->device->ib_dev;
1265 cq->uobject = &obj->uobject;
1266 cq->comp_handler = ib_uverbs_comp_handler;
1267 cq->event_handler = ib_uverbs_cq_event_handler;
1268 cq->cq_context = ev_file;
1269 atomic_set(&cq->usecnt, 0);
1271 obj->uobject.object = cq;
1272 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1276 memset(&resp, 0, sizeof resp);
1277 resp.cq_handle = obj->uobject.id;
1280 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1281 &resp, sizeof resp)) {
1286 mutex_lock(&file->mutex);
1287 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1288 mutex_unlock(&file->mutex);
1290 obj->uobject.live = 1;
1292 up_write(&obj->uobject.mutex);
1297 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1304 ib_uverbs_release_ucq(file, ev_file, obj);
1307 put_uobj_write(&obj->uobject);
1311 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1312 const char __user *buf, int in_len,
1315 struct ib_uverbs_resize_cq cmd;
1316 struct ib_uverbs_resize_cq_resp resp;
1317 struct ib_udata udata;
1321 if (copy_from_user(&cmd, buf, sizeof cmd))
1324 INIT_UDATA(&udata, buf + sizeof cmd,
1325 (unsigned long) cmd.response + sizeof resp,
1326 in_len - sizeof cmd, out_len - sizeof resp);
1328 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1332 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1338 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1339 &resp, sizeof resp.cqe))
1345 return ret ? ret : in_len;
1348 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1350 struct ib_uverbs_wc tmp;
1352 tmp.wr_id = wc->wr_id;
1353 tmp.status = wc->status;
1354 tmp.opcode = wc->opcode;
1355 tmp.vendor_err = wc->vendor_err;
1356 tmp.byte_len = wc->byte_len;
1357 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1358 tmp.qp_num = wc->qp->qp_num;
1359 tmp.src_qp = wc->src_qp;
1360 tmp.wc_flags = wc->wc_flags;
1361 tmp.pkey_index = wc->pkey_index;
1362 tmp.slid = wc->slid;
1364 tmp.dlid_path_bits = wc->dlid_path_bits;
1365 tmp.port_num = wc->port_num;
1368 if (copy_to_user(dest, &tmp, sizeof tmp))
1374 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1375 const char __user *buf, int in_len,
1378 struct ib_uverbs_poll_cq cmd;
1379 struct ib_uverbs_poll_cq_resp resp;
1380 u8 __user *header_ptr;
1381 u8 __user *data_ptr;
1386 if (copy_from_user(&cmd, buf, sizeof cmd))
1389 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1393 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1394 header_ptr = (void __user *)(unsigned long) cmd.response;
1395 data_ptr = header_ptr + sizeof resp;
1397 memset(&resp, 0, sizeof resp);
1398 while (resp.count < cmd.ne) {
1399 ret = ib_poll_cq(cq, 1, &wc);
1405 ret = copy_wc_to_user(data_ptr, &wc);
1409 data_ptr += sizeof(struct ib_uverbs_wc);
1413 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1425 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1426 const char __user *buf, int in_len,
1429 struct ib_uverbs_req_notify_cq cmd;
1432 if (copy_from_user(&cmd, buf, sizeof cmd))
1435 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1439 ib_req_notify_cq(cq, cmd.solicited_only ?
1440 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1447 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1448 const char __user *buf, int in_len,
1451 struct ib_uverbs_destroy_cq cmd;
1452 struct ib_uverbs_destroy_cq_resp resp;
1453 struct ib_uobject *uobj;
1455 struct ib_ucq_object *obj;
1456 struct ib_uverbs_event_file *ev_file;
1459 if (copy_from_user(&cmd, buf, sizeof cmd))
1462 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1466 ev_file = cq->cq_context;
1467 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1469 ret = ib_destroy_cq(cq);
1473 put_uobj_write(uobj);
1478 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1480 mutex_lock(&file->mutex);
1481 list_del(&uobj->list);
1482 mutex_unlock(&file->mutex);
1484 ib_uverbs_release_ucq(file, ev_file, obj);
1486 memset(&resp, 0, sizeof resp);
1487 resp.comp_events_reported = obj->comp_events_reported;
1488 resp.async_events_reported = obj->async_events_reported;
1492 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1493 &resp, sizeof resp))
1499 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1500 const char __user *buf, int in_len,
1503 struct ib_uverbs_create_qp cmd;
1504 struct ib_uverbs_create_qp_resp resp;
1505 struct ib_udata udata;
1506 struct ib_uqp_object *obj;
1507 struct ib_device *device;
1508 struct ib_pd *pd = NULL;
1509 struct ib_xrcd *xrcd = NULL;
1510 struct ib_uobject *uninitialized_var(xrcd_uobj);
1511 struct ib_cq *scq = NULL, *rcq = NULL;
1512 struct ib_srq *srq = NULL;
1514 struct ib_qp_init_attr attr;
1517 if (out_len < sizeof resp)
1520 if (copy_from_user(&cmd, buf, sizeof cmd))
1523 if (cmd.qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1526 INIT_UDATA(&udata, buf + sizeof cmd,
1527 (unsigned long) cmd.response + sizeof resp,
1528 in_len - sizeof cmd, out_len - sizeof resp);
1530 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1534 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1535 down_write(&obj->uevent.uobject.mutex);
1537 if (cmd.qp_type == IB_QPT_XRC_TGT) {
1538 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1543 device = xrcd->device;
1545 if (cmd.qp_type == IB_QPT_XRC_INI) {
1546 cmd.max_recv_wr = cmd.max_recv_sge = 0;
1549 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1550 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1556 if (cmd.recv_cq_handle != cmd.send_cq_handle) {
1557 rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext, 0);
1565 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, !!rcq);
1567 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1573 device = pd->device;
1576 attr.event_handler = ib_uverbs_qp_event_handler;
1577 attr.qp_context = file;
1582 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1583 attr.qp_type = cmd.qp_type;
1584 attr.create_flags = 0;
1586 attr.cap.max_send_wr = cmd.max_send_wr;
1587 attr.cap.max_recv_wr = cmd.max_recv_wr;
1588 attr.cap.max_send_sge = cmd.max_send_sge;
1589 attr.cap.max_recv_sge = cmd.max_recv_sge;
1590 attr.cap.max_inline_data = cmd.max_inline_data;
1592 obj->uevent.events_reported = 0;
1593 INIT_LIST_HEAD(&obj->uevent.event_list);
1594 INIT_LIST_HEAD(&obj->mcast_list);
1596 if (cmd.qp_type == IB_QPT_XRC_TGT)
1597 qp = ib_create_qp(pd, &attr);
1599 qp = device->create_qp(pd, &attr, &udata);
1606 if (cmd.qp_type != IB_QPT_XRC_TGT) {
1608 qp->device = device;
1610 qp->send_cq = attr.send_cq;
1611 qp->recv_cq = attr.recv_cq;
1613 qp->event_handler = attr.event_handler;
1614 qp->qp_context = attr.qp_context;
1615 qp->qp_type = attr.qp_type;
1616 atomic_set(&qp->usecnt, 0);
1617 atomic_inc(&pd->usecnt);
1618 atomic_inc(&attr.send_cq->usecnt);
1620 atomic_inc(&attr.recv_cq->usecnt);
1622 atomic_inc(&attr.srq->usecnt);
1624 qp->uobject = &obj->uevent.uobject;
1626 obj->uevent.uobject.object = qp;
1627 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1631 memset(&resp, 0, sizeof resp);
1632 resp.qpn = qp->qp_num;
1633 resp.qp_handle = obj->uevent.uobject.id;
1634 resp.max_recv_sge = attr.cap.max_recv_sge;
1635 resp.max_send_sge = attr.cap.max_send_sge;
1636 resp.max_recv_wr = attr.cap.max_recv_wr;
1637 resp.max_send_wr = attr.cap.max_send_wr;
1638 resp.max_inline_data = attr.cap.max_inline_data;
1640 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1641 &resp, sizeof resp)) {
1647 put_xrcd_read(xrcd_uobj);
1652 if (rcq && rcq != scq)
1657 mutex_lock(&file->mutex);
1658 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1659 mutex_unlock(&file->mutex);
1661 obj->uevent.uobject.live = 1;
1663 up_write(&obj->uevent.uobject.mutex);
1668 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1675 put_xrcd_read(xrcd_uobj);
1680 if (rcq && rcq != scq)
1685 put_uobj_write(&obj->uevent.uobject);
1689 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1690 const char __user *buf, int in_len, int out_len)
1692 struct ib_uverbs_open_qp cmd;
1693 struct ib_uverbs_create_qp_resp resp;
1694 struct ib_udata udata;
1695 struct ib_uqp_object *obj;
1696 struct ib_xrcd *xrcd;
1697 struct ib_uobject *uninitialized_var(xrcd_uobj);
1699 struct ib_qp_open_attr attr;
1702 if (out_len < sizeof resp)
1705 if (copy_from_user(&cmd, buf, sizeof cmd))
1708 INIT_UDATA(&udata, buf + sizeof cmd,
1709 (unsigned long) cmd.response + sizeof resp,
1710 in_len - sizeof cmd, out_len - sizeof resp);
1712 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1716 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1717 down_write(&obj->uevent.uobject.mutex);
1719 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1725 attr.event_handler = ib_uverbs_qp_event_handler;
1726 attr.qp_context = file;
1727 attr.qp_num = cmd.qpn;
1728 attr.qp_type = cmd.qp_type;
1730 obj->uevent.events_reported = 0;
1731 INIT_LIST_HEAD(&obj->uevent.event_list);
1732 INIT_LIST_HEAD(&obj->mcast_list);
1734 qp = ib_open_qp(xrcd, &attr);
1740 qp->uobject = &obj->uevent.uobject;
1742 obj->uevent.uobject.object = qp;
1743 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1747 memset(&resp, 0, sizeof resp);
1748 resp.qpn = qp->qp_num;
1749 resp.qp_handle = obj->uevent.uobject.id;
1751 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1752 &resp, sizeof resp)) {
1757 put_xrcd_read(xrcd_uobj);
1759 mutex_lock(&file->mutex);
1760 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1761 mutex_unlock(&file->mutex);
1763 obj->uevent.uobject.live = 1;
1765 up_write(&obj->uevent.uobject.mutex);
1770 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1776 put_xrcd_read(xrcd_uobj);
1777 put_uobj_write(&obj->uevent.uobject);
1781 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1782 const char __user *buf, int in_len,
1785 struct ib_uverbs_query_qp cmd;
1786 struct ib_uverbs_query_qp_resp resp;
1788 struct ib_qp_attr *attr;
1789 struct ib_qp_init_attr *init_attr;
1792 if (copy_from_user(&cmd, buf, sizeof cmd))
1795 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1796 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1797 if (!attr || !init_attr) {
1802 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1808 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1815 memset(&resp, 0, sizeof resp);
1817 resp.qp_state = attr->qp_state;
1818 resp.cur_qp_state = attr->cur_qp_state;
1819 resp.path_mtu = attr->path_mtu;
1820 resp.path_mig_state = attr->path_mig_state;
1821 resp.qkey = attr->qkey;
1822 resp.rq_psn = attr->rq_psn;
1823 resp.sq_psn = attr->sq_psn;
1824 resp.dest_qp_num = attr->dest_qp_num;
1825 resp.qp_access_flags = attr->qp_access_flags;
1826 resp.pkey_index = attr->pkey_index;
1827 resp.alt_pkey_index = attr->alt_pkey_index;
1828 resp.sq_draining = attr->sq_draining;
1829 resp.max_rd_atomic = attr->max_rd_atomic;
1830 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1831 resp.min_rnr_timer = attr->min_rnr_timer;
1832 resp.port_num = attr->port_num;
1833 resp.timeout = attr->timeout;
1834 resp.retry_cnt = attr->retry_cnt;
1835 resp.rnr_retry = attr->rnr_retry;
1836 resp.alt_port_num = attr->alt_port_num;
1837 resp.alt_timeout = attr->alt_timeout;
1839 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1840 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1841 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1842 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1843 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1844 resp.dest.dlid = attr->ah_attr.dlid;
1845 resp.dest.sl = attr->ah_attr.sl;
1846 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1847 resp.dest.static_rate = attr->ah_attr.static_rate;
1848 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1849 resp.dest.port_num = attr->ah_attr.port_num;
1851 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1852 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1853 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1854 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1855 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1856 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1857 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1858 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1859 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1860 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1861 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1863 resp.max_send_wr = init_attr->cap.max_send_wr;
1864 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1865 resp.max_send_sge = init_attr->cap.max_send_sge;
1866 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1867 resp.max_inline_data = init_attr->cap.max_inline_data;
1868 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1870 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1871 &resp, sizeof resp))
1878 return ret ? ret : in_len;
1881 /* Remove ignored fields set in the attribute mask */
1882 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1885 case IB_QPT_XRC_INI:
1886 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1887 case IB_QPT_XRC_TGT:
1888 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1895 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1896 const char __user *buf, int in_len,
1899 struct ib_uverbs_modify_qp cmd;
1900 struct ib_udata udata;
1902 struct ib_qp_attr *attr;
1905 if (copy_from_user(&cmd, buf, sizeof cmd))
1908 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1911 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1915 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1921 attr->qp_state = cmd.qp_state;
1922 attr->cur_qp_state = cmd.cur_qp_state;
1923 attr->path_mtu = cmd.path_mtu;
1924 attr->path_mig_state = cmd.path_mig_state;
1925 attr->qkey = cmd.qkey;
1926 attr->rq_psn = cmd.rq_psn;
1927 attr->sq_psn = cmd.sq_psn;
1928 attr->dest_qp_num = cmd.dest_qp_num;
1929 attr->qp_access_flags = cmd.qp_access_flags;
1930 attr->pkey_index = cmd.pkey_index;
1931 attr->alt_pkey_index = cmd.alt_pkey_index;
1932 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1933 attr->max_rd_atomic = cmd.max_rd_atomic;
1934 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1935 attr->min_rnr_timer = cmd.min_rnr_timer;
1936 attr->port_num = cmd.port_num;
1937 attr->timeout = cmd.timeout;
1938 attr->retry_cnt = cmd.retry_cnt;
1939 attr->rnr_retry = cmd.rnr_retry;
1940 attr->alt_port_num = cmd.alt_port_num;
1941 attr->alt_timeout = cmd.alt_timeout;
1943 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1944 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1945 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1946 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1947 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1948 attr->ah_attr.dlid = cmd.dest.dlid;
1949 attr->ah_attr.sl = cmd.dest.sl;
1950 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1951 attr->ah_attr.static_rate = cmd.dest.static_rate;
1952 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1953 attr->ah_attr.port_num = cmd.dest.port_num;
1955 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1956 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1957 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1958 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1959 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1960 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1961 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1962 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1963 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1964 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1965 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1967 if (qp->real_qp == qp) {
1968 ret = qp->device->modify_qp(qp, attr,
1969 modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
1971 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
1987 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1988 const char __user *buf, int in_len,
1991 struct ib_uverbs_destroy_qp cmd;
1992 struct ib_uverbs_destroy_qp_resp resp;
1993 struct ib_uobject *uobj;
1995 struct ib_uqp_object *obj;
1998 if (copy_from_user(&cmd, buf, sizeof cmd))
2001 memset(&resp, 0, sizeof resp);
2003 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
2007 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2009 if (!list_empty(&obj->mcast_list)) {
2010 put_uobj_write(uobj);
2014 ret = ib_destroy_qp(qp);
2018 put_uobj_write(uobj);
2023 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
2025 mutex_lock(&file->mutex);
2026 list_del(&uobj->list);
2027 mutex_unlock(&file->mutex);
2029 ib_uverbs_release_uevent(file, &obj->uevent);
2031 resp.events_reported = obj->uevent.events_reported;
2035 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2036 &resp, sizeof resp))
2042 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2043 const char __user *buf, int in_len,
2046 struct ib_uverbs_post_send cmd;
2047 struct ib_uverbs_post_send_resp resp;
2048 struct ib_uverbs_send_wr *user_wr;
2049 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2053 ssize_t ret = -EINVAL;
2055 if (copy_from_user(&cmd, buf, sizeof cmd))
2058 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2059 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2062 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2065 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2069 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2073 is_ud = qp->qp_type == IB_QPT_UD;
2076 for (i = 0; i < cmd.wr_count; ++i) {
2077 if (copy_from_user(user_wr,
2078 buf + sizeof cmd + i * cmd.wqe_size,
2084 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2089 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2090 user_wr->num_sge * sizeof (struct ib_sge),
2104 next->wr_id = user_wr->wr_id;
2105 next->num_sge = user_wr->num_sge;
2106 next->opcode = user_wr->opcode;
2107 next->send_flags = user_wr->send_flags;
2110 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
2112 if (!next->wr.ud.ah) {
2116 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
2117 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
2119 switch (next->opcode) {
2120 case IB_WR_RDMA_WRITE_WITH_IMM:
2122 (__be32 __force) user_wr->ex.imm_data;
2123 case IB_WR_RDMA_WRITE:
2124 case IB_WR_RDMA_READ:
2125 next->wr.rdma.remote_addr =
2126 user_wr->wr.rdma.remote_addr;
2127 next->wr.rdma.rkey =
2128 user_wr->wr.rdma.rkey;
2130 case IB_WR_SEND_WITH_IMM:
2132 (__be32 __force) user_wr->ex.imm_data;
2134 case IB_WR_SEND_WITH_INV:
2135 next->ex.invalidate_rkey =
2136 user_wr->ex.invalidate_rkey;
2138 case IB_WR_ATOMIC_CMP_AND_SWP:
2139 case IB_WR_ATOMIC_FETCH_AND_ADD:
2140 next->wr.atomic.remote_addr =
2141 user_wr->wr.atomic.remote_addr;
2142 next->wr.atomic.compare_add =
2143 user_wr->wr.atomic.compare_add;
2144 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2145 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2152 if (next->num_sge) {
2153 next->sg_list = (void *) next +
2154 ALIGN(sizeof *next, sizeof (struct ib_sge));
2155 if (copy_from_user(next->sg_list,
2157 cmd.wr_count * cmd.wqe_size +
2158 sg_ind * sizeof (struct ib_sge),
2159 next->num_sge * sizeof (struct ib_sge))) {
2163 sg_ind += next->num_sge;
2165 next->sg_list = NULL;
2169 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2171 for (next = wr; next; next = next->next) {
2177 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2178 &resp, sizeof resp))
2185 if (is_ud && wr->wr.ud.ah)
2186 put_ah_read(wr->wr.ud.ah);
2195 return ret ? ret : in_len;
2198 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2204 struct ib_uverbs_recv_wr *user_wr;
2205 struct ib_recv_wr *wr = NULL, *last, *next;
2210 if (in_len < wqe_size * wr_count +
2211 sge_count * sizeof (struct ib_uverbs_sge))
2212 return ERR_PTR(-EINVAL);
2214 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2215 return ERR_PTR(-EINVAL);
2217 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2219 return ERR_PTR(-ENOMEM);
2223 for (i = 0; i < wr_count; ++i) {
2224 if (copy_from_user(user_wr, buf + i * wqe_size,
2230 if (user_wr->num_sge + sg_ind > sge_count) {
2235 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2236 user_wr->num_sge * sizeof (struct ib_sge),
2250 next->wr_id = user_wr->wr_id;
2251 next->num_sge = user_wr->num_sge;
2253 if (next->num_sge) {
2254 next->sg_list = (void *) next +
2255 ALIGN(sizeof *next, sizeof (struct ib_sge));
2256 if (copy_from_user(next->sg_list,
2257 buf + wr_count * wqe_size +
2258 sg_ind * sizeof (struct ib_sge),
2259 next->num_sge * sizeof (struct ib_sge))) {
2263 sg_ind += next->num_sge;
2265 next->sg_list = NULL;
2280 return ERR_PTR(ret);
2283 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2284 const char __user *buf, int in_len,
2287 struct ib_uverbs_post_recv cmd;
2288 struct ib_uverbs_post_recv_resp resp;
2289 struct ib_recv_wr *wr, *next, *bad_wr;
2291 ssize_t ret = -EINVAL;
2293 if (copy_from_user(&cmd, buf, sizeof cmd))
2296 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2297 in_len - sizeof cmd, cmd.wr_count,
2298 cmd.sge_count, cmd.wqe_size);
2302 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2307 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2312 for (next = wr; next; next = next->next) {
2318 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2319 &resp, sizeof resp))
2329 return ret ? ret : in_len;
2332 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2333 const char __user *buf, int in_len,
2336 struct ib_uverbs_post_srq_recv cmd;
2337 struct ib_uverbs_post_srq_recv_resp resp;
2338 struct ib_recv_wr *wr, *next, *bad_wr;
2340 ssize_t ret = -EINVAL;
2342 if (copy_from_user(&cmd, buf, sizeof cmd))
2345 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2346 in_len - sizeof cmd, cmd.wr_count,
2347 cmd.sge_count, cmd.wqe_size);
2351 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2356 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2361 for (next = wr; next; next = next->next) {
2367 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2368 &resp, sizeof resp))
2378 return ret ? ret : in_len;
2381 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2382 const char __user *buf, int in_len,
2385 struct ib_uverbs_create_ah cmd;
2386 struct ib_uverbs_create_ah_resp resp;
2387 struct ib_uobject *uobj;
2390 struct ib_ah_attr attr;
2393 if (out_len < sizeof resp)
2396 if (copy_from_user(&cmd, buf, sizeof cmd))
2399 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2403 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2404 down_write(&uobj->mutex);
2406 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2412 attr.dlid = cmd.attr.dlid;
2413 attr.sl = cmd.attr.sl;
2414 attr.src_path_bits = cmd.attr.src_path_bits;
2415 attr.static_rate = cmd.attr.static_rate;
2416 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
2417 attr.port_num = cmd.attr.port_num;
2418 attr.grh.flow_label = cmd.attr.grh.flow_label;
2419 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2420 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2421 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2422 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2424 ah = ib_create_ah(pd, &attr);
2433 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2437 resp.ah_handle = uobj->id;
2439 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2440 &resp, sizeof resp)) {
2447 mutex_lock(&file->mutex);
2448 list_add_tail(&uobj->list, &file->ucontext->ah_list);
2449 mutex_unlock(&file->mutex);
2453 up_write(&uobj->mutex);
2458 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2467 put_uobj_write(uobj);
2471 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2472 const char __user *buf, int in_len, int out_len)
2474 struct ib_uverbs_destroy_ah cmd;
2476 struct ib_uobject *uobj;
2479 if (copy_from_user(&cmd, buf, sizeof cmd))
2482 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2487 ret = ib_destroy_ah(ah);
2491 put_uobj_write(uobj);
2496 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2498 mutex_lock(&file->mutex);
2499 list_del(&uobj->list);
2500 mutex_unlock(&file->mutex);
2507 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2508 const char __user *buf, int in_len,
2511 struct ib_uverbs_attach_mcast cmd;
2513 struct ib_uqp_object *obj;
2514 struct ib_uverbs_mcast_entry *mcast;
2517 if (copy_from_user(&cmd, buf, sizeof cmd))
2520 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2524 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2526 list_for_each_entry(mcast, &obj->mcast_list, list)
2527 if (cmd.mlid == mcast->lid &&
2528 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2533 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2539 mcast->lid = cmd.mlid;
2540 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2542 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2544 list_add_tail(&mcast->list, &obj->mcast_list);
2551 return ret ? ret : in_len;
2554 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2555 const char __user *buf, int in_len,
2558 struct ib_uverbs_detach_mcast cmd;
2559 struct ib_uqp_object *obj;
2561 struct ib_uverbs_mcast_entry *mcast;
2564 if (copy_from_user(&cmd, buf, sizeof cmd))
2567 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2571 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2575 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2577 list_for_each_entry(mcast, &obj->mcast_list, list)
2578 if (cmd.mlid == mcast->lid &&
2579 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2580 list_del(&mcast->list);
2588 return ret ? ret : in_len;
2591 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2592 struct ib_uverbs_create_xsrq *cmd,
2593 struct ib_udata *udata)
2595 struct ib_uverbs_create_srq_resp resp;
2596 struct ib_usrq_object *obj;
2599 struct ib_uobject *uninitialized_var(xrcd_uobj);
2600 struct ib_srq_init_attr attr;
2603 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2607 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2608 down_write(&obj->uevent.uobject.mutex);
2610 if (cmd->srq_type == IB_SRQT_XRC) {
2611 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2612 if (!attr.ext.xrc.xrcd) {
2617 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2618 atomic_inc(&obj->uxrcd->refcnt);
2620 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2621 if (!attr.ext.xrc.cq) {
2627 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
2633 attr.event_handler = ib_uverbs_srq_event_handler;
2634 attr.srq_context = file;
2635 attr.srq_type = cmd->srq_type;
2636 attr.attr.max_wr = cmd->max_wr;
2637 attr.attr.max_sge = cmd->max_sge;
2638 attr.attr.srq_limit = cmd->srq_limit;
2640 obj->uevent.events_reported = 0;
2641 INIT_LIST_HEAD(&obj->uevent.event_list);
2643 srq = pd->device->create_srq(pd, &attr, udata);
2649 srq->device = pd->device;
2651 srq->srq_type = cmd->srq_type;
2652 srq->uobject = &obj->uevent.uobject;
2653 srq->event_handler = attr.event_handler;
2654 srq->srq_context = attr.srq_context;
2656 if (cmd->srq_type == IB_SRQT_XRC) {
2657 srq->ext.xrc.cq = attr.ext.xrc.cq;
2658 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2659 atomic_inc(&attr.ext.xrc.cq->usecnt);
2660 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2663 atomic_inc(&pd->usecnt);
2664 atomic_set(&srq->usecnt, 0);
2666 obj->uevent.uobject.object = srq;
2667 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2671 memset(&resp, 0, sizeof resp);
2672 resp.srq_handle = obj->uevent.uobject.id;
2673 resp.max_wr = attr.attr.max_wr;
2674 resp.max_sge = attr.attr.max_sge;
2675 if (cmd->srq_type == IB_SRQT_XRC)
2676 resp.srqn = srq->ext.xrc.srq_num;
2678 if (copy_to_user((void __user *) (unsigned long) cmd->response,
2679 &resp, sizeof resp)) {
2684 if (cmd->srq_type == IB_SRQT_XRC) {
2685 put_uobj_read(xrcd_uobj);
2686 put_cq_read(attr.ext.xrc.cq);
2690 mutex_lock(&file->mutex);
2691 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2692 mutex_unlock(&file->mutex);
2694 obj->uevent.uobject.live = 1;
2696 up_write(&obj->uevent.uobject.mutex);
2701 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2704 ib_destroy_srq(srq);
2710 if (cmd->srq_type == IB_SRQT_XRC)
2711 put_cq_read(attr.ext.xrc.cq);
2714 if (cmd->srq_type == IB_SRQT_XRC) {
2715 atomic_dec(&obj->uxrcd->refcnt);
2716 put_uobj_read(xrcd_uobj);
2720 put_uobj_write(&obj->uevent.uobject);
2724 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2725 const char __user *buf, int in_len,
2728 struct ib_uverbs_create_srq cmd;
2729 struct ib_uverbs_create_xsrq xcmd;
2730 struct ib_uverbs_create_srq_resp resp;
2731 struct ib_udata udata;
2734 if (out_len < sizeof resp)
2737 if (copy_from_user(&cmd, buf, sizeof cmd))
2740 xcmd.response = cmd.response;
2741 xcmd.user_handle = cmd.user_handle;
2742 xcmd.srq_type = IB_SRQT_BASIC;
2743 xcmd.pd_handle = cmd.pd_handle;
2744 xcmd.max_wr = cmd.max_wr;
2745 xcmd.max_sge = cmd.max_sge;
2746 xcmd.srq_limit = cmd.srq_limit;
2748 INIT_UDATA(&udata, buf + sizeof cmd,
2749 (unsigned long) cmd.response + sizeof resp,
2750 in_len - sizeof cmd, out_len - sizeof resp);
2752 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2759 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2760 const char __user *buf, int in_len, int out_len)
2762 struct ib_uverbs_create_xsrq cmd;
2763 struct ib_uverbs_create_srq_resp resp;
2764 struct ib_udata udata;
2767 if (out_len < sizeof resp)
2770 if (copy_from_user(&cmd, buf, sizeof cmd))
2773 INIT_UDATA(&udata, buf + sizeof cmd,
2774 (unsigned long) cmd.response + sizeof resp,
2775 in_len - sizeof cmd, out_len - sizeof resp);
2777 ret = __uverbs_create_xsrq(file, &cmd, &udata);
2784 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2785 const char __user *buf, int in_len,
2788 struct ib_uverbs_modify_srq cmd;
2789 struct ib_udata udata;
2791 struct ib_srq_attr attr;
2794 if (copy_from_user(&cmd, buf, sizeof cmd))
2797 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2800 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2804 attr.max_wr = cmd.max_wr;
2805 attr.srq_limit = cmd.srq_limit;
2807 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2811 return ret ? ret : in_len;
2814 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2815 const char __user *buf,
2816 int in_len, int out_len)
2818 struct ib_uverbs_query_srq cmd;
2819 struct ib_uverbs_query_srq_resp resp;
2820 struct ib_srq_attr attr;
2824 if (out_len < sizeof resp)
2827 if (copy_from_user(&cmd, buf, sizeof cmd))
2830 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2834 ret = ib_query_srq(srq, &attr);
2841 memset(&resp, 0, sizeof resp);
2843 resp.max_wr = attr.max_wr;
2844 resp.max_sge = attr.max_sge;
2845 resp.srq_limit = attr.srq_limit;
2847 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2848 &resp, sizeof resp))
2854 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2855 const char __user *buf, int in_len,
2858 struct ib_uverbs_destroy_srq cmd;
2859 struct ib_uverbs_destroy_srq_resp resp;
2860 struct ib_uobject *uobj;
2862 struct ib_uevent_object *obj;
2865 if (copy_from_user(&cmd, buf, sizeof cmd))
2868 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2872 obj = container_of(uobj, struct ib_uevent_object, uobject);
2874 ret = ib_destroy_srq(srq);
2878 put_uobj_write(uobj);
2883 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2885 mutex_lock(&file->mutex);
2886 list_del(&uobj->list);
2887 mutex_unlock(&file->mutex);
2889 ib_uverbs_release_uevent(file, obj);
2891 memset(&resp, 0, sizeof resp);
2892 resp.events_reported = obj->events_reported;
2896 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2897 &resp, sizeof resp))
2900 return ret ? ret : in_len;