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" };
57 static struct uverbs_lock_class rule_lock_class = { .name = "RULE-uobj" };
60 * The ib_uobject locking scheme is as follows:
62 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
63 * needs to be held during all idr operations. When an object is
64 * looked up, a reference must be taken on the object's kref before
67 * - Each object also has an rwsem. This rwsem must be held for
68 * reading while an operation that uses the object is performed.
69 * For example, while registering an MR, the associated PD's
70 * uobject.mutex must be held for reading. The rwsem must be held
71 * for writing while initializing or destroying an object.
73 * - In addition, each object has a "live" flag. If this flag is not
74 * set, then lookups of the object will fail even if it is found in
75 * the idr. This handles a reader that blocks and does not acquire
76 * the rwsem until after the object is destroyed. The destroy
77 * operation will set the live flag to 0 and then drop the rwsem;
78 * this will allow the reader to acquire the rwsem, see that the
79 * live flag is 0, and then drop the rwsem and its reference to
80 * object. The underlying storage will not be freed until the last
81 * reference to the object is dropped.
84 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
85 struct ib_ucontext *context, struct uverbs_lock_class *c)
87 uobj->user_handle = user_handle;
88 uobj->context = context;
89 kref_init(&uobj->ref);
90 init_rwsem(&uobj->mutex);
91 lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
95 static void release_uobj(struct kref *kref)
97 kfree(container_of(kref, struct ib_uobject, ref));
100 static void put_uobj(struct ib_uobject *uobj)
102 kref_put(&uobj->ref, release_uobj);
105 static void put_uobj_read(struct ib_uobject *uobj)
107 up_read(&uobj->mutex);
111 static void put_uobj_write(struct ib_uobject *uobj)
113 up_write(&uobj->mutex);
117 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
121 idr_preload(GFP_KERNEL);
122 spin_lock(&ib_uverbs_idr_lock);
124 ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
128 spin_unlock(&ib_uverbs_idr_lock);
131 return ret < 0 ? ret : 0;
134 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
136 spin_lock(&ib_uverbs_idr_lock);
137 idr_remove(idr, uobj->id);
138 spin_unlock(&ib_uverbs_idr_lock);
141 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
142 struct ib_ucontext *context)
144 struct ib_uobject *uobj;
146 spin_lock(&ib_uverbs_idr_lock);
147 uobj = idr_find(idr, id);
149 if (uobj->context == context)
150 kref_get(&uobj->ref);
154 spin_unlock(&ib_uverbs_idr_lock);
159 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
160 struct ib_ucontext *context, int nested)
162 struct ib_uobject *uobj;
164 uobj = __idr_get_uobj(idr, id, context);
169 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
171 down_read(&uobj->mutex);
180 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
181 struct ib_ucontext *context)
183 struct ib_uobject *uobj;
185 uobj = __idr_get_uobj(idr, id, context);
189 down_write(&uobj->mutex);
191 put_uobj_write(uobj);
198 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
201 struct ib_uobject *uobj;
203 uobj = idr_read_uobj(idr, id, context, nested);
204 return uobj ? uobj->object : NULL;
207 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
209 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
212 static void put_pd_read(struct ib_pd *pd)
214 put_uobj_read(pd->uobject);
217 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
219 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
222 static void put_cq_read(struct ib_cq *cq)
224 put_uobj_read(cq->uobject);
227 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
229 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
232 static void put_ah_read(struct ib_ah *ah)
234 put_uobj_read(ah->uobject);
237 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
239 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
242 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
244 struct ib_uobject *uobj;
246 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
247 return uobj ? uobj->object : NULL;
250 static void put_qp_read(struct ib_qp *qp)
252 put_uobj_read(qp->uobject);
255 static void put_qp_write(struct ib_qp *qp)
257 put_uobj_write(qp->uobject);
260 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
262 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
265 static void put_srq_read(struct ib_srq *srq)
267 put_uobj_read(srq->uobject);
270 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
271 struct ib_uobject **uobj)
273 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
274 return *uobj ? (*uobj)->object : NULL;
277 static void put_xrcd_read(struct ib_uobject *uobj)
282 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
283 const char __user *buf,
284 int in_len, int out_len)
286 struct ib_uverbs_get_context cmd;
287 struct ib_uverbs_get_context_resp resp;
288 struct ib_udata udata;
289 struct ib_device *ibdev = file->device->ib_dev;
290 struct ib_ucontext *ucontext;
294 if (out_len < sizeof resp)
297 if (copy_from_user(&cmd, buf, sizeof cmd))
300 mutex_lock(&file->mutex);
302 if (file->ucontext) {
307 INIT_UDATA(&udata, buf + sizeof cmd,
308 (unsigned long) cmd.response + sizeof resp,
309 in_len - sizeof cmd, out_len - sizeof resp);
311 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
312 if (IS_ERR(ucontext)) {
313 ret = PTR_ERR(ucontext);
317 ucontext->device = ibdev;
318 INIT_LIST_HEAD(&ucontext->pd_list);
319 INIT_LIST_HEAD(&ucontext->mr_list);
320 INIT_LIST_HEAD(&ucontext->mw_list);
321 INIT_LIST_HEAD(&ucontext->cq_list);
322 INIT_LIST_HEAD(&ucontext->qp_list);
323 INIT_LIST_HEAD(&ucontext->srq_list);
324 INIT_LIST_HEAD(&ucontext->ah_list);
325 INIT_LIST_HEAD(&ucontext->xrcd_list);
326 INIT_LIST_HEAD(&ucontext->rule_list);
327 ucontext->closing = 0;
329 resp.num_comp_vectors = file->device->num_comp_vectors;
331 ret = get_unused_fd_flags(O_CLOEXEC);
336 filp = ib_uverbs_alloc_event_file(file, 1);
342 if (copy_to_user((void __user *) (unsigned long) cmd.response,
343 &resp, sizeof resp)) {
348 file->async_file = filp->private_data;
350 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
351 ib_uverbs_event_handler);
352 ret = ib_register_event_handler(&file->event_handler);
356 kref_get(&file->async_file->ref);
357 kref_get(&file->ref);
358 file->ucontext = ucontext;
360 fd_install(resp.async_fd, filp);
362 mutex_unlock(&file->mutex);
370 put_unused_fd(resp.async_fd);
373 ibdev->dealloc_ucontext(ucontext);
376 mutex_unlock(&file->mutex);
380 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
381 const char __user *buf,
382 int in_len, int out_len)
384 struct ib_uverbs_query_device cmd;
385 struct ib_uverbs_query_device_resp resp;
386 struct ib_device_attr attr;
389 if (out_len < sizeof resp)
392 if (copy_from_user(&cmd, buf, sizeof cmd))
395 ret = ib_query_device(file->device->ib_dev, &attr);
399 memset(&resp, 0, sizeof resp);
401 resp.fw_ver = attr.fw_ver;
402 resp.node_guid = file->device->ib_dev->node_guid;
403 resp.sys_image_guid = attr.sys_image_guid;
404 resp.max_mr_size = attr.max_mr_size;
405 resp.page_size_cap = attr.page_size_cap;
406 resp.vendor_id = attr.vendor_id;
407 resp.vendor_part_id = attr.vendor_part_id;
408 resp.hw_ver = attr.hw_ver;
409 resp.max_qp = attr.max_qp;
410 resp.max_qp_wr = attr.max_qp_wr;
411 resp.device_cap_flags = attr.device_cap_flags;
412 resp.max_sge = attr.max_sge;
413 resp.max_sge_rd = attr.max_sge_rd;
414 resp.max_cq = attr.max_cq;
415 resp.max_cqe = attr.max_cqe;
416 resp.max_mr = attr.max_mr;
417 resp.max_pd = attr.max_pd;
418 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
419 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
420 resp.max_res_rd_atom = attr.max_res_rd_atom;
421 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
422 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
423 resp.atomic_cap = attr.atomic_cap;
424 resp.max_ee = attr.max_ee;
425 resp.max_rdd = attr.max_rdd;
426 resp.max_mw = attr.max_mw;
427 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
428 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
429 resp.max_mcast_grp = attr.max_mcast_grp;
430 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
431 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
432 resp.max_ah = attr.max_ah;
433 resp.max_fmr = attr.max_fmr;
434 resp.max_map_per_fmr = attr.max_map_per_fmr;
435 resp.max_srq = attr.max_srq;
436 resp.max_srq_wr = attr.max_srq_wr;
437 resp.max_srq_sge = attr.max_srq_sge;
438 resp.max_pkeys = attr.max_pkeys;
439 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
440 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
442 if (copy_to_user((void __user *) (unsigned long) cmd.response,
449 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
450 const char __user *buf,
451 int in_len, int out_len)
453 struct ib_uverbs_query_port cmd;
454 struct ib_uverbs_query_port_resp resp;
455 struct ib_port_attr attr;
458 if (out_len < sizeof resp)
461 if (copy_from_user(&cmd, buf, sizeof cmd))
464 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
468 memset(&resp, 0, sizeof resp);
470 resp.state = attr.state;
471 resp.max_mtu = attr.max_mtu;
472 resp.active_mtu = attr.active_mtu;
473 resp.gid_tbl_len = attr.gid_tbl_len;
474 resp.port_cap_flags = attr.port_cap_flags;
475 resp.max_msg_sz = attr.max_msg_sz;
476 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
477 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
478 resp.pkey_tbl_len = attr.pkey_tbl_len;
480 resp.sm_lid = attr.sm_lid;
482 resp.max_vl_num = attr.max_vl_num;
483 resp.sm_sl = attr.sm_sl;
484 resp.subnet_timeout = attr.subnet_timeout;
485 resp.init_type_reply = attr.init_type_reply;
486 resp.active_width = attr.active_width;
487 resp.active_speed = attr.active_speed;
488 resp.phys_state = attr.phys_state;
489 resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
492 if (copy_to_user((void __user *) (unsigned long) cmd.response,
499 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
500 const char __user *buf,
501 int in_len, int out_len)
503 struct ib_uverbs_alloc_pd cmd;
504 struct ib_uverbs_alloc_pd_resp resp;
505 struct ib_udata udata;
506 struct ib_uobject *uobj;
510 if (out_len < sizeof resp)
513 if (copy_from_user(&cmd, buf, sizeof cmd))
516 INIT_UDATA(&udata, buf + sizeof cmd,
517 (unsigned long) cmd.response + sizeof resp,
518 in_len - sizeof cmd, out_len - sizeof resp);
520 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
524 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
525 down_write(&uobj->mutex);
527 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
528 file->ucontext, &udata);
534 pd->device = file->device->ib_dev;
536 atomic_set(&pd->usecnt, 0);
539 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
543 memset(&resp, 0, sizeof resp);
544 resp.pd_handle = uobj->id;
546 if (copy_to_user((void __user *) (unsigned long) cmd.response,
547 &resp, sizeof resp)) {
552 mutex_lock(&file->mutex);
553 list_add_tail(&uobj->list, &file->ucontext->pd_list);
554 mutex_unlock(&file->mutex);
558 up_write(&uobj->mutex);
563 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
569 put_uobj_write(uobj);
573 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
574 const char __user *buf,
575 int in_len, int out_len)
577 struct ib_uverbs_dealloc_pd cmd;
578 struct ib_uobject *uobj;
581 if (copy_from_user(&cmd, buf, sizeof cmd))
584 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
588 ret = ib_dealloc_pd(uobj->object);
592 put_uobj_write(uobj);
597 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
599 mutex_lock(&file->mutex);
600 list_del(&uobj->list);
601 mutex_unlock(&file->mutex);
608 struct xrcd_table_entry {
610 struct ib_xrcd *xrcd;
614 static int xrcd_table_insert(struct ib_uverbs_device *dev,
616 struct ib_xrcd *xrcd)
618 struct xrcd_table_entry *entry, *scan;
619 struct rb_node **p = &dev->xrcd_tree.rb_node;
620 struct rb_node *parent = NULL;
622 entry = kmalloc(sizeof *entry, GFP_KERNEL);
627 entry->inode = inode;
631 scan = rb_entry(parent, struct xrcd_table_entry, node);
633 if (inode < scan->inode) {
635 } else if (inode > scan->inode) {
643 rb_link_node(&entry->node, parent, p);
644 rb_insert_color(&entry->node, &dev->xrcd_tree);
649 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
652 struct xrcd_table_entry *entry;
653 struct rb_node *p = dev->xrcd_tree.rb_node;
656 entry = rb_entry(p, struct xrcd_table_entry, node);
658 if (inode < entry->inode)
660 else if (inode > entry->inode)
669 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
671 struct xrcd_table_entry *entry;
673 entry = xrcd_table_search(dev, inode);
680 static void xrcd_table_delete(struct ib_uverbs_device *dev,
683 struct xrcd_table_entry *entry;
685 entry = xrcd_table_search(dev, inode);
688 rb_erase(&entry->node, &dev->xrcd_tree);
693 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
694 const char __user *buf, int in_len,
697 struct ib_uverbs_open_xrcd cmd;
698 struct ib_uverbs_open_xrcd_resp resp;
699 struct ib_udata udata;
700 struct ib_uxrcd_object *obj;
701 struct ib_xrcd *xrcd = NULL;
702 struct fd f = {NULL, 0};
703 struct inode *inode = NULL;
707 if (out_len < sizeof resp)
710 if (copy_from_user(&cmd, buf, sizeof cmd))
713 INIT_UDATA(&udata, buf + sizeof cmd,
714 (unsigned long) cmd.response + sizeof resp,
715 in_len - sizeof cmd, out_len - sizeof resp);
717 mutex_lock(&file->device->xrcd_tree_mutex);
720 /* search for file descriptor */
724 goto err_tree_mutex_unlock;
727 inode = file_inode(f.file);
728 xrcd = find_xrcd(file->device, inode);
729 if (!xrcd && !(cmd.oflags & O_CREAT)) {
730 /* no file descriptor. Need CREATE flag */
732 goto err_tree_mutex_unlock;
735 if (xrcd && cmd.oflags & O_EXCL) {
737 goto err_tree_mutex_unlock;
741 obj = kmalloc(sizeof *obj, GFP_KERNEL);
744 goto err_tree_mutex_unlock;
747 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
749 down_write(&obj->uobject.mutex);
752 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
753 file->ucontext, &udata);
760 xrcd->device = file->device->ib_dev;
761 atomic_set(&xrcd->usecnt, 0);
762 mutex_init(&xrcd->tgt_qp_mutex);
763 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
767 atomic_set(&obj->refcnt, 0);
768 obj->uobject.object = xrcd;
769 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
773 memset(&resp, 0, sizeof resp);
774 resp.xrcd_handle = obj->uobject.id;
778 /* create new inode/xrcd table entry */
779 ret = xrcd_table_insert(file->device, inode, xrcd);
781 goto err_insert_xrcd;
783 atomic_inc(&xrcd->usecnt);
786 if (copy_to_user((void __user *) (unsigned long) cmd.response,
787 &resp, sizeof resp)) {
795 mutex_lock(&file->mutex);
796 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
797 mutex_unlock(&file->mutex);
799 obj->uobject.live = 1;
800 up_write(&obj->uobject.mutex);
802 mutex_unlock(&file->device->xrcd_tree_mutex);
808 xrcd_table_delete(file->device, inode);
809 atomic_dec(&xrcd->usecnt);
813 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
816 ib_dealloc_xrcd(xrcd);
819 put_uobj_write(&obj->uobject);
821 err_tree_mutex_unlock:
825 mutex_unlock(&file->device->xrcd_tree_mutex);
830 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
831 const char __user *buf, int in_len,
834 struct ib_uverbs_close_xrcd cmd;
835 struct ib_uobject *uobj;
836 struct ib_xrcd *xrcd = NULL;
837 struct inode *inode = NULL;
838 struct ib_uxrcd_object *obj;
842 if (copy_from_user(&cmd, buf, sizeof cmd))
845 mutex_lock(&file->device->xrcd_tree_mutex);
846 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
854 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
855 if (atomic_read(&obj->refcnt)) {
856 put_uobj_write(uobj);
861 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
862 ret = ib_dealloc_xrcd(uobj->object);
869 atomic_inc(&xrcd->usecnt);
871 put_uobj_write(uobj);
877 xrcd_table_delete(file->device, inode);
879 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
880 mutex_lock(&file->mutex);
881 list_del(&uobj->list);
882 mutex_unlock(&file->mutex);
888 mutex_unlock(&file->device->xrcd_tree_mutex);
892 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
893 struct ib_xrcd *xrcd)
898 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
901 ib_dealloc_xrcd(xrcd);
904 xrcd_table_delete(dev, inode);
907 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
908 const char __user *buf, int in_len,
911 struct ib_uverbs_reg_mr cmd;
912 struct ib_uverbs_reg_mr_resp resp;
913 struct ib_udata udata;
914 struct ib_uobject *uobj;
919 if (out_len < sizeof resp)
922 if (copy_from_user(&cmd, buf, sizeof cmd))
925 INIT_UDATA(&udata, buf + sizeof cmd,
926 (unsigned long) cmd.response + sizeof resp,
927 in_len - sizeof cmd, out_len - sizeof resp);
929 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
933 * Local write permission is required if remote write or
934 * remote atomic permission is also requested.
936 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
937 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
940 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
944 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
945 down_write(&uobj->mutex);
947 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
953 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
954 cmd.access_flags, &udata);
960 mr->device = pd->device;
963 atomic_inc(&pd->usecnt);
964 atomic_set(&mr->usecnt, 0);
967 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
971 memset(&resp, 0, sizeof resp);
972 resp.lkey = mr->lkey;
973 resp.rkey = mr->rkey;
974 resp.mr_handle = uobj->id;
976 if (copy_to_user((void __user *) (unsigned long) cmd.response,
977 &resp, sizeof resp)) {
984 mutex_lock(&file->mutex);
985 list_add_tail(&uobj->list, &file->ucontext->mr_list);
986 mutex_unlock(&file->mutex);
990 up_write(&uobj->mutex);
995 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1004 put_uobj_write(uobj);
1008 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1009 const char __user *buf, int in_len,
1012 struct ib_uverbs_dereg_mr cmd;
1014 struct ib_uobject *uobj;
1017 if (copy_from_user(&cmd, buf, sizeof cmd))
1020 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1026 ret = ib_dereg_mr(mr);
1030 put_uobj_write(uobj);
1035 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1037 mutex_lock(&file->mutex);
1038 list_del(&uobj->list);
1039 mutex_unlock(&file->mutex);
1046 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
1047 const char __user *buf, int in_len,
1050 struct ib_uverbs_alloc_mw cmd;
1051 struct ib_uverbs_alloc_mw_resp resp;
1052 struct ib_uobject *uobj;
1057 if (out_len < sizeof(resp))
1060 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1063 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1067 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1068 down_write(&uobj->mutex);
1070 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1076 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1082 mw->device = pd->device;
1085 atomic_inc(&pd->usecnt);
1088 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1092 memset(&resp, 0, sizeof(resp));
1093 resp.rkey = mw->rkey;
1094 resp.mw_handle = uobj->id;
1096 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1097 &resp, sizeof(resp))) {
1104 mutex_lock(&file->mutex);
1105 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1106 mutex_unlock(&file->mutex);
1110 up_write(&uobj->mutex);
1115 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1124 put_uobj_write(uobj);
1128 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
1129 const char __user *buf, int in_len,
1132 struct ib_uverbs_dealloc_mw cmd;
1134 struct ib_uobject *uobj;
1137 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1140 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1146 ret = ib_dealloc_mw(mw);
1150 put_uobj_write(uobj);
1155 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1157 mutex_lock(&file->mutex);
1158 list_del(&uobj->list);
1159 mutex_unlock(&file->mutex);
1166 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1167 const char __user *buf, int in_len,
1170 struct ib_uverbs_create_comp_channel cmd;
1171 struct ib_uverbs_create_comp_channel_resp resp;
1175 if (out_len < sizeof resp)
1178 if (copy_from_user(&cmd, buf, sizeof cmd))
1181 ret = get_unused_fd_flags(O_CLOEXEC);
1186 filp = ib_uverbs_alloc_event_file(file, 0);
1188 put_unused_fd(resp.fd);
1189 return PTR_ERR(filp);
1192 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1193 &resp, sizeof resp)) {
1194 put_unused_fd(resp.fd);
1199 fd_install(resp.fd, filp);
1203 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1204 const char __user *buf, int in_len,
1207 struct ib_uverbs_create_cq cmd;
1208 struct ib_uverbs_create_cq_resp resp;
1209 struct ib_udata udata;
1210 struct ib_ucq_object *obj;
1211 struct ib_uverbs_event_file *ev_file = NULL;
1215 if (out_len < sizeof resp)
1218 if (copy_from_user(&cmd, buf, sizeof cmd))
1221 INIT_UDATA(&udata, buf + sizeof cmd,
1222 (unsigned long) cmd.response + sizeof resp,
1223 in_len - sizeof cmd, out_len - sizeof resp);
1225 if (cmd.comp_vector >= file->device->num_comp_vectors)
1228 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1232 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1233 down_write(&obj->uobject.mutex);
1235 if (cmd.comp_channel >= 0) {
1236 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1243 obj->uverbs_file = file;
1244 obj->comp_events_reported = 0;
1245 obj->async_events_reported = 0;
1246 INIT_LIST_HEAD(&obj->comp_list);
1247 INIT_LIST_HEAD(&obj->async_list);
1249 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1251 file->ucontext, &udata);
1257 cq->device = file->device->ib_dev;
1258 cq->uobject = &obj->uobject;
1259 cq->comp_handler = ib_uverbs_comp_handler;
1260 cq->event_handler = ib_uverbs_cq_event_handler;
1261 cq->cq_context = ev_file;
1262 atomic_set(&cq->usecnt, 0);
1264 obj->uobject.object = cq;
1265 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1269 memset(&resp, 0, sizeof resp);
1270 resp.cq_handle = obj->uobject.id;
1273 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1274 &resp, sizeof resp)) {
1279 mutex_lock(&file->mutex);
1280 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1281 mutex_unlock(&file->mutex);
1283 obj->uobject.live = 1;
1285 up_write(&obj->uobject.mutex);
1290 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1297 ib_uverbs_release_ucq(file, ev_file, obj);
1300 put_uobj_write(&obj->uobject);
1304 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1305 const char __user *buf, int in_len,
1308 struct ib_uverbs_resize_cq cmd;
1309 struct ib_uverbs_resize_cq_resp resp;
1310 struct ib_udata udata;
1314 if (copy_from_user(&cmd, buf, sizeof cmd))
1317 INIT_UDATA(&udata, buf + sizeof cmd,
1318 (unsigned long) cmd.response + sizeof resp,
1319 in_len - sizeof cmd, out_len - sizeof resp);
1321 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1325 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1331 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1332 &resp, sizeof resp.cqe))
1338 return ret ? ret : in_len;
1341 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1343 struct ib_uverbs_wc tmp;
1345 tmp.wr_id = wc->wr_id;
1346 tmp.status = wc->status;
1347 tmp.opcode = wc->opcode;
1348 tmp.vendor_err = wc->vendor_err;
1349 tmp.byte_len = wc->byte_len;
1350 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1351 tmp.qp_num = wc->qp->qp_num;
1352 tmp.src_qp = wc->src_qp;
1353 tmp.wc_flags = wc->wc_flags;
1354 tmp.pkey_index = wc->pkey_index;
1355 tmp.slid = wc->slid;
1357 tmp.dlid_path_bits = wc->dlid_path_bits;
1358 tmp.port_num = wc->port_num;
1361 if (copy_to_user(dest, &tmp, sizeof tmp))
1367 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1368 const char __user *buf, int in_len,
1371 struct ib_uverbs_poll_cq cmd;
1372 struct ib_uverbs_poll_cq_resp resp;
1373 u8 __user *header_ptr;
1374 u8 __user *data_ptr;
1379 if (copy_from_user(&cmd, buf, sizeof cmd))
1382 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1386 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1387 header_ptr = (void __user *)(unsigned long) cmd.response;
1388 data_ptr = header_ptr + sizeof resp;
1390 memset(&resp, 0, sizeof resp);
1391 while (resp.count < cmd.ne) {
1392 ret = ib_poll_cq(cq, 1, &wc);
1398 ret = copy_wc_to_user(data_ptr, &wc);
1402 data_ptr += sizeof(struct ib_uverbs_wc);
1406 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1418 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1419 const char __user *buf, int in_len,
1422 struct ib_uverbs_req_notify_cq cmd;
1425 if (copy_from_user(&cmd, buf, sizeof cmd))
1428 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1432 ib_req_notify_cq(cq, cmd.solicited_only ?
1433 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1440 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1441 const char __user *buf, int in_len,
1444 struct ib_uverbs_destroy_cq cmd;
1445 struct ib_uverbs_destroy_cq_resp resp;
1446 struct ib_uobject *uobj;
1448 struct ib_ucq_object *obj;
1449 struct ib_uverbs_event_file *ev_file;
1452 if (copy_from_user(&cmd, buf, sizeof cmd))
1455 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1459 ev_file = cq->cq_context;
1460 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1462 ret = ib_destroy_cq(cq);
1466 put_uobj_write(uobj);
1471 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1473 mutex_lock(&file->mutex);
1474 list_del(&uobj->list);
1475 mutex_unlock(&file->mutex);
1477 ib_uverbs_release_ucq(file, ev_file, obj);
1479 memset(&resp, 0, sizeof resp);
1480 resp.comp_events_reported = obj->comp_events_reported;
1481 resp.async_events_reported = obj->async_events_reported;
1485 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1486 &resp, sizeof resp))
1492 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1493 const char __user *buf, int in_len,
1496 struct ib_uverbs_create_qp cmd;
1497 struct ib_uverbs_create_qp_resp resp;
1498 struct ib_udata udata;
1499 struct ib_uqp_object *obj;
1500 struct ib_device *device;
1501 struct ib_pd *pd = NULL;
1502 struct ib_xrcd *xrcd = NULL;
1503 struct ib_uobject *uninitialized_var(xrcd_uobj);
1504 struct ib_cq *scq = NULL, *rcq = NULL;
1505 struct ib_srq *srq = NULL;
1507 struct ib_qp_init_attr attr;
1510 if (out_len < sizeof resp)
1513 if (copy_from_user(&cmd, buf, sizeof cmd))
1516 if (cmd.qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1519 INIT_UDATA(&udata, buf + sizeof cmd,
1520 (unsigned long) cmd.response + sizeof resp,
1521 in_len - sizeof cmd, out_len - sizeof resp);
1523 obj = kzalloc(sizeof *obj, GFP_KERNEL);
1527 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1528 down_write(&obj->uevent.uobject.mutex);
1530 if (cmd.qp_type == IB_QPT_XRC_TGT) {
1531 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1536 device = xrcd->device;
1538 if (cmd.qp_type == IB_QPT_XRC_INI) {
1539 cmd.max_recv_wr = cmd.max_recv_sge = 0;
1542 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1543 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1549 if (cmd.recv_cq_handle != cmd.send_cq_handle) {
1550 rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext, 0);
1558 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, !!rcq);
1560 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1566 device = pd->device;
1569 attr.event_handler = ib_uverbs_qp_event_handler;
1570 attr.qp_context = file;
1575 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1576 attr.qp_type = cmd.qp_type;
1577 attr.create_flags = 0;
1579 attr.cap.max_send_wr = cmd.max_send_wr;
1580 attr.cap.max_recv_wr = cmd.max_recv_wr;
1581 attr.cap.max_send_sge = cmd.max_send_sge;
1582 attr.cap.max_recv_sge = cmd.max_recv_sge;
1583 attr.cap.max_inline_data = cmd.max_inline_data;
1585 obj->uevent.events_reported = 0;
1586 INIT_LIST_HEAD(&obj->uevent.event_list);
1587 INIT_LIST_HEAD(&obj->mcast_list);
1589 if (cmd.qp_type == IB_QPT_XRC_TGT)
1590 qp = ib_create_qp(pd, &attr);
1592 qp = device->create_qp(pd, &attr, &udata);
1599 if (cmd.qp_type != IB_QPT_XRC_TGT) {
1601 qp->device = device;
1603 qp->send_cq = attr.send_cq;
1604 qp->recv_cq = attr.recv_cq;
1606 qp->event_handler = attr.event_handler;
1607 qp->qp_context = attr.qp_context;
1608 qp->qp_type = attr.qp_type;
1609 atomic_set(&qp->usecnt, 0);
1610 atomic_inc(&pd->usecnt);
1611 atomic_inc(&attr.send_cq->usecnt);
1613 atomic_inc(&attr.recv_cq->usecnt);
1615 atomic_inc(&attr.srq->usecnt);
1617 qp->uobject = &obj->uevent.uobject;
1619 obj->uevent.uobject.object = qp;
1620 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1624 memset(&resp, 0, sizeof resp);
1625 resp.qpn = qp->qp_num;
1626 resp.qp_handle = obj->uevent.uobject.id;
1627 resp.max_recv_sge = attr.cap.max_recv_sge;
1628 resp.max_send_sge = attr.cap.max_send_sge;
1629 resp.max_recv_wr = attr.cap.max_recv_wr;
1630 resp.max_send_wr = attr.cap.max_send_wr;
1631 resp.max_inline_data = attr.cap.max_inline_data;
1633 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1634 &resp, sizeof resp)) {
1640 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1642 atomic_inc(&obj->uxrcd->refcnt);
1643 put_xrcd_read(xrcd_uobj);
1650 if (rcq && rcq != scq)
1655 mutex_lock(&file->mutex);
1656 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1657 mutex_unlock(&file->mutex);
1659 obj->uevent.uobject.live = 1;
1661 up_write(&obj->uevent.uobject.mutex);
1666 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1673 put_xrcd_read(xrcd_uobj);
1678 if (rcq && rcq != scq)
1683 put_uobj_write(&obj->uevent.uobject);
1687 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1688 const char __user *buf, int in_len, int out_len)
1690 struct ib_uverbs_open_qp cmd;
1691 struct ib_uverbs_create_qp_resp resp;
1692 struct ib_udata udata;
1693 struct ib_uqp_object *obj;
1694 struct ib_xrcd *xrcd;
1695 struct ib_uobject *uninitialized_var(xrcd_uobj);
1697 struct ib_qp_open_attr attr;
1700 if (out_len < sizeof resp)
1703 if (copy_from_user(&cmd, buf, sizeof cmd))
1706 INIT_UDATA(&udata, buf + sizeof cmd,
1707 (unsigned long) cmd.response + sizeof resp,
1708 in_len - sizeof cmd, out_len - sizeof resp);
1710 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1714 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1715 down_write(&obj->uevent.uobject.mutex);
1717 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1723 attr.event_handler = ib_uverbs_qp_event_handler;
1724 attr.qp_context = file;
1725 attr.qp_num = cmd.qpn;
1726 attr.qp_type = cmd.qp_type;
1728 obj->uevent.events_reported = 0;
1729 INIT_LIST_HEAD(&obj->uevent.event_list);
1730 INIT_LIST_HEAD(&obj->mcast_list);
1732 qp = ib_open_qp(xrcd, &attr);
1738 qp->uobject = &obj->uevent.uobject;
1740 obj->uevent.uobject.object = qp;
1741 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1745 memset(&resp, 0, sizeof resp);
1746 resp.qpn = qp->qp_num;
1747 resp.qp_handle = obj->uevent.uobject.id;
1749 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1750 &resp, sizeof resp)) {
1755 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1756 atomic_inc(&obj->uxrcd->refcnt);
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);
2024 atomic_dec(&obj->uxrcd->refcnt);
2026 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
2028 mutex_lock(&file->mutex);
2029 list_del(&uobj->list);
2030 mutex_unlock(&file->mutex);
2032 ib_uverbs_release_uevent(file, &obj->uevent);
2034 resp.events_reported = obj->uevent.events_reported;
2038 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2039 &resp, sizeof resp))
2045 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2046 const char __user *buf, int in_len,
2049 struct ib_uverbs_post_send cmd;
2050 struct ib_uverbs_post_send_resp resp;
2051 struct ib_uverbs_send_wr *user_wr;
2052 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2056 ssize_t ret = -EINVAL;
2058 if (copy_from_user(&cmd, buf, sizeof cmd))
2061 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2062 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2065 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2068 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2072 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2076 is_ud = qp->qp_type == IB_QPT_UD;
2079 for (i = 0; i < cmd.wr_count; ++i) {
2080 if (copy_from_user(user_wr,
2081 buf + sizeof cmd + i * cmd.wqe_size,
2087 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2092 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2093 user_wr->num_sge * sizeof (struct ib_sge),
2107 next->wr_id = user_wr->wr_id;
2108 next->num_sge = user_wr->num_sge;
2109 next->opcode = user_wr->opcode;
2110 next->send_flags = user_wr->send_flags;
2113 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
2115 if (!next->wr.ud.ah) {
2119 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
2120 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
2122 switch (next->opcode) {
2123 case IB_WR_RDMA_WRITE_WITH_IMM:
2125 (__be32 __force) user_wr->ex.imm_data;
2126 case IB_WR_RDMA_WRITE:
2127 case IB_WR_RDMA_READ:
2128 next->wr.rdma.remote_addr =
2129 user_wr->wr.rdma.remote_addr;
2130 next->wr.rdma.rkey =
2131 user_wr->wr.rdma.rkey;
2133 case IB_WR_SEND_WITH_IMM:
2135 (__be32 __force) user_wr->ex.imm_data;
2137 case IB_WR_SEND_WITH_INV:
2138 next->ex.invalidate_rkey =
2139 user_wr->ex.invalidate_rkey;
2141 case IB_WR_ATOMIC_CMP_AND_SWP:
2142 case IB_WR_ATOMIC_FETCH_AND_ADD:
2143 next->wr.atomic.remote_addr =
2144 user_wr->wr.atomic.remote_addr;
2145 next->wr.atomic.compare_add =
2146 user_wr->wr.atomic.compare_add;
2147 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2148 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2155 if (next->num_sge) {
2156 next->sg_list = (void *) next +
2157 ALIGN(sizeof *next, sizeof (struct ib_sge));
2158 if (copy_from_user(next->sg_list,
2160 cmd.wr_count * cmd.wqe_size +
2161 sg_ind * sizeof (struct ib_sge),
2162 next->num_sge * sizeof (struct ib_sge))) {
2166 sg_ind += next->num_sge;
2168 next->sg_list = NULL;
2172 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2174 for (next = wr; next; next = next->next) {
2180 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2181 &resp, sizeof resp))
2188 if (is_ud && wr->wr.ud.ah)
2189 put_ah_read(wr->wr.ud.ah);
2198 return ret ? ret : in_len;
2201 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2207 struct ib_uverbs_recv_wr *user_wr;
2208 struct ib_recv_wr *wr = NULL, *last, *next;
2213 if (in_len < wqe_size * wr_count +
2214 sge_count * sizeof (struct ib_uverbs_sge))
2215 return ERR_PTR(-EINVAL);
2217 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2218 return ERR_PTR(-EINVAL);
2220 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2222 return ERR_PTR(-ENOMEM);
2226 for (i = 0; i < wr_count; ++i) {
2227 if (copy_from_user(user_wr, buf + i * wqe_size,
2233 if (user_wr->num_sge + sg_ind > sge_count) {
2238 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2239 user_wr->num_sge * sizeof (struct ib_sge),
2253 next->wr_id = user_wr->wr_id;
2254 next->num_sge = user_wr->num_sge;
2256 if (next->num_sge) {
2257 next->sg_list = (void *) next +
2258 ALIGN(sizeof *next, sizeof (struct ib_sge));
2259 if (copy_from_user(next->sg_list,
2260 buf + wr_count * wqe_size +
2261 sg_ind * sizeof (struct ib_sge),
2262 next->num_sge * sizeof (struct ib_sge))) {
2266 sg_ind += next->num_sge;
2268 next->sg_list = NULL;
2283 return ERR_PTR(ret);
2286 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2287 const char __user *buf, int in_len,
2290 struct ib_uverbs_post_recv cmd;
2291 struct ib_uverbs_post_recv_resp resp;
2292 struct ib_recv_wr *wr, *next, *bad_wr;
2294 ssize_t ret = -EINVAL;
2296 if (copy_from_user(&cmd, buf, sizeof cmd))
2299 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2300 in_len - sizeof cmd, cmd.wr_count,
2301 cmd.sge_count, cmd.wqe_size);
2305 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2310 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2315 for (next = wr; next; next = next->next) {
2321 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2322 &resp, sizeof resp))
2332 return ret ? ret : in_len;
2335 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2336 const char __user *buf, int in_len,
2339 struct ib_uverbs_post_srq_recv cmd;
2340 struct ib_uverbs_post_srq_recv_resp resp;
2341 struct ib_recv_wr *wr, *next, *bad_wr;
2343 ssize_t ret = -EINVAL;
2345 if (copy_from_user(&cmd, buf, sizeof cmd))
2348 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2349 in_len - sizeof cmd, cmd.wr_count,
2350 cmd.sge_count, cmd.wqe_size);
2354 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2359 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2364 for (next = wr; next; next = next->next) {
2370 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2371 &resp, sizeof resp))
2381 return ret ? ret : in_len;
2384 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2385 const char __user *buf, int in_len,
2388 struct ib_uverbs_create_ah cmd;
2389 struct ib_uverbs_create_ah_resp resp;
2390 struct ib_uobject *uobj;
2393 struct ib_ah_attr attr;
2396 if (out_len < sizeof resp)
2399 if (copy_from_user(&cmd, buf, sizeof cmd))
2402 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2406 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2407 down_write(&uobj->mutex);
2409 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2415 attr.dlid = cmd.attr.dlid;
2416 attr.sl = cmd.attr.sl;
2417 attr.src_path_bits = cmd.attr.src_path_bits;
2418 attr.static_rate = cmd.attr.static_rate;
2419 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
2420 attr.port_num = cmd.attr.port_num;
2421 attr.grh.flow_label = cmd.attr.grh.flow_label;
2422 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2423 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2424 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2425 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2427 ah = ib_create_ah(pd, &attr);
2436 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2440 resp.ah_handle = uobj->id;
2442 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2443 &resp, sizeof resp)) {
2450 mutex_lock(&file->mutex);
2451 list_add_tail(&uobj->list, &file->ucontext->ah_list);
2452 mutex_unlock(&file->mutex);
2456 up_write(&uobj->mutex);
2461 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2470 put_uobj_write(uobj);
2474 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2475 const char __user *buf, int in_len, int out_len)
2477 struct ib_uverbs_destroy_ah cmd;
2479 struct ib_uobject *uobj;
2482 if (copy_from_user(&cmd, buf, sizeof cmd))
2485 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2490 ret = ib_destroy_ah(ah);
2494 put_uobj_write(uobj);
2499 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2501 mutex_lock(&file->mutex);
2502 list_del(&uobj->list);
2503 mutex_unlock(&file->mutex);
2510 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2511 const char __user *buf, int in_len,
2514 struct ib_uverbs_attach_mcast cmd;
2516 struct ib_uqp_object *obj;
2517 struct ib_uverbs_mcast_entry *mcast;
2520 if (copy_from_user(&cmd, buf, sizeof cmd))
2523 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2527 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2529 list_for_each_entry(mcast, &obj->mcast_list, list)
2530 if (cmd.mlid == mcast->lid &&
2531 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2536 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2542 mcast->lid = cmd.mlid;
2543 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2545 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2547 list_add_tail(&mcast->list, &obj->mcast_list);
2554 return ret ? ret : in_len;
2557 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2558 const char __user *buf, int in_len,
2561 struct ib_uverbs_detach_mcast cmd;
2562 struct ib_uqp_object *obj;
2564 struct ib_uverbs_mcast_entry *mcast;
2567 if (copy_from_user(&cmd, buf, sizeof cmd))
2570 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2574 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2578 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2580 list_for_each_entry(mcast, &obj->mcast_list, list)
2581 if (cmd.mlid == mcast->lid &&
2582 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2583 list_del(&mcast->list);
2591 return ret ? ret : in_len;
2594 static int kern_spec_to_ib_spec(struct ib_uverbs_flow_spec *kern_spec,
2595 union ib_flow_spec *ib_spec)
2597 ib_spec->type = kern_spec->type;
2599 switch (ib_spec->type) {
2600 case IB_FLOW_SPEC_ETH:
2601 ib_spec->eth.size = sizeof(struct ib_flow_spec_eth);
2602 if (ib_spec->eth.size != kern_spec->eth.size)
2604 memcpy(&ib_spec->eth.val, &kern_spec->eth.val,
2605 sizeof(struct ib_flow_eth_filter));
2606 memcpy(&ib_spec->eth.mask, &kern_spec->eth.mask,
2607 sizeof(struct ib_flow_eth_filter));
2609 case IB_FLOW_SPEC_IPV4:
2610 ib_spec->ipv4.size = sizeof(struct ib_flow_spec_ipv4);
2611 if (ib_spec->ipv4.size != kern_spec->ipv4.size)
2613 memcpy(&ib_spec->ipv4.val, &kern_spec->ipv4.val,
2614 sizeof(struct ib_flow_ipv4_filter));
2615 memcpy(&ib_spec->ipv4.mask, &kern_spec->ipv4.mask,
2616 sizeof(struct ib_flow_ipv4_filter));
2618 case IB_FLOW_SPEC_TCP:
2619 case IB_FLOW_SPEC_UDP:
2620 ib_spec->tcp_udp.size = sizeof(struct ib_flow_spec_tcp_udp);
2621 if (ib_spec->tcp_udp.size != kern_spec->tcp_udp.size)
2623 memcpy(&ib_spec->tcp_udp.val, &kern_spec->tcp_udp.val,
2624 sizeof(struct ib_flow_tcp_udp_filter));
2625 memcpy(&ib_spec->tcp_udp.mask, &kern_spec->tcp_udp.mask,
2626 sizeof(struct ib_flow_tcp_udp_filter));
2634 int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
2635 struct ib_udata *ucore,
2636 struct ib_udata *uhw)
2638 struct ib_uverbs_create_flow cmd;
2639 struct ib_uverbs_create_flow_resp resp;
2640 struct ib_uobject *uobj;
2641 struct ib_flow *flow_id;
2642 struct ib_uverbs_flow_attr *kern_flow_attr;
2643 struct ib_flow_attr *flow_attr;
2650 if (ucore->outlen < sizeof(resp))
2653 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
2657 ucore->inbuf += sizeof(cmd);
2658 ucore->inlen -= sizeof(cmd);
2663 if ((cmd.flow_attr.type == IB_FLOW_ATTR_SNIFFER &&
2664 !capable(CAP_NET_ADMIN)) || !capable(CAP_NET_RAW))
2667 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
2670 if (cmd.flow_attr.size > ucore->inlen ||
2671 cmd.flow_attr.size >
2672 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
2675 if (cmd.flow_attr.num_of_specs) {
2676 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
2678 if (!kern_flow_attr)
2681 memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr));
2682 err = ib_copy_from_udata(kern_flow_attr + 1, ucore,
2683 cmd.flow_attr.size);
2687 kern_flow_attr = &cmd.flow_attr;
2690 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
2695 init_uobj(uobj, 0, file->ucontext, &rule_lock_class);
2696 down_write(&uobj->mutex);
2698 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2704 flow_attr = kmalloc(sizeof(*flow_attr) + cmd.flow_attr.size, GFP_KERNEL);
2710 flow_attr->type = kern_flow_attr->type;
2711 flow_attr->priority = kern_flow_attr->priority;
2712 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
2713 flow_attr->port = kern_flow_attr->port;
2714 flow_attr->flags = kern_flow_attr->flags;
2715 flow_attr->size = sizeof(*flow_attr);
2717 kern_spec = kern_flow_attr + 1;
2718 ib_spec = flow_attr + 1;
2719 for (i = 0; i < flow_attr->num_of_specs &&
2720 cmd.flow_attr.size > offsetof(struct ib_uverbs_flow_spec, reserved) &&
2721 cmd.flow_attr.size >=
2722 ((struct ib_uverbs_flow_spec *)kern_spec)->size; i++) {
2723 err = kern_spec_to_ib_spec(kern_spec, ib_spec);
2727 ((union ib_flow_spec *) ib_spec)->size;
2728 cmd.flow_attr.size -= ((struct ib_uverbs_flow_spec *)kern_spec)->size;
2729 kern_spec += ((struct ib_uverbs_flow_spec *) kern_spec)->size;
2730 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
2732 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
2733 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
2734 i, cmd.flow_attr.size);
2737 flow_id = ib_create_flow(qp, flow_attr, IB_FLOW_DOMAIN_USER);
2738 if (IS_ERR(flow_id)) {
2739 err = PTR_ERR(flow_id);
2743 flow_id->uobject = uobj;
2744 uobj->object = flow_id;
2746 err = idr_add_uobj(&ib_uverbs_rule_idr, uobj);
2750 memset(&resp, 0, sizeof(resp));
2751 resp.flow_handle = uobj->id;
2753 err = ib_copy_to_udata(ucore,
2754 &resp, sizeof(resp));
2759 mutex_lock(&file->mutex);
2760 list_add_tail(&uobj->list, &file->ucontext->rule_list);
2761 mutex_unlock(&file->mutex);
2765 up_write(&uobj->mutex);
2767 if (cmd.flow_attr.num_of_specs)
2768 kfree(kern_flow_attr);
2771 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
2773 ib_destroy_flow(flow_id);
2779 put_uobj_write(uobj);
2781 if (cmd.flow_attr.num_of_specs)
2782 kfree(kern_flow_attr);
2786 int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
2787 struct ib_udata *ucore,
2788 struct ib_udata *uhw)
2790 struct ib_uverbs_destroy_flow cmd;
2791 struct ib_flow *flow_id;
2792 struct ib_uobject *uobj;
2795 ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
2799 uobj = idr_write_uobj(&ib_uverbs_rule_idr, cmd.flow_handle,
2803 flow_id = uobj->object;
2805 ret = ib_destroy_flow(flow_id);
2809 put_uobj_write(uobj);
2811 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
2813 mutex_lock(&file->mutex);
2814 list_del(&uobj->list);
2815 mutex_unlock(&file->mutex);
2822 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2823 struct ib_uverbs_create_xsrq *cmd,
2824 struct ib_udata *udata)
2826 struct ib_uverbs_create_srq_resp resp;
2827 struct ib_usrq_object *obj;
2830 struct ib_uobject *uninitialized_var(xrcd_uobj);
2831 struct ib_srq_init_attr attr;
2834 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2838 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2839 down_write(&obj->uevent.uobject.mutex);
2841 if (cmd->srq_type == IB_SRQT_XRC) {
2842 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2843 if (!attr.ext.xrc.xrcd) {
2848 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2849 atomic_inc(&obj->uxrcd->refcnt);
2851 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2852 if (!attr.ext.xrc.cq) {
2858 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
2864 attr.event_handler = ib_uverbs_srq_event_handler;
2865 attr.srq_context = file;
2866 attr.srq_type = cmd->srq_type;
2867 attr.attr.max_wr = cmd->max_wr;
2868 attr.attr.max_sge = cmd->max_sge;
2869 attr.attr.srq_limit = cmd->srq_limit;
2871 obj->uevent.events_reported = 0;
2872 INIT_LIST_HEAD(&obj->uevent.event_list);
2874 srq = pd->device->create_srq(pd, &attr, udata);
2880 srq->device = pd->device;
2882 srq->srq_type = cmd->srq_type;
2883 srq->uobject = &obj->uevent.uobject;
2884 srq->event_handler = attr.event_handler;
2885 srq->srq_context = attr.srq_context;
2887 if (cmd->srq_type == IB_SRQT_XRC) {
2888 srq->ext.xrc.cq = attr.ext.xrc.cq;
2889 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2890 atomic_inc(&attr.ext.xrc.cq->usecnt);
2891 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2894 atomic_inc(&pd->usecnt);
2895 atomic_set(&srq->usecnt, 0);
2897 obj->uevent.uobject.object = srq;
2898 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2902 memset(&resp, 0, sizeof resp);
2903 resp.srq_handle = obj->uevent.uobject.id;
2904 resp.max_wr = attr.attr.max_wr;
2905 resp.max_sge = attr.attr.max_sge;
2906 if (cmd->srq_type == IB_SRQT_XRC)
2907 resp.srqn = srq->ext.xrc.srq_num;
2909 if (copy_to_user((void __user *) (unsigned long) cmd->response,
2910 &resp, sizeof resp)) {
2915 if (cmd->srq_type == IB_SRQT_XRC) {
2916 put_uobj_read(xrcd_uobj);
2917 put_cq_read(attr.ext.xrc.cq);
2921 mutex_lock(&file->mutex);
2922 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2923 mutex_unlock(&file->mutex);
2925 obj->uevent.uobject.live = 1;
2927 up_write(&obj->uevent.uobject.mutex);
2932 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2935 ib_destroy_srq(srq);
2941 if (cmd->srq_type == IB_SRQT_XRC)
2942 put_cq_read(attr.ext.xrc.cq);
2945 if (cmd->srq_type == IB_SRQT_XRC) {
2946 atomic_dec(&obj->uxrcd->refcnt);
2947 put_uobj_read(xrcd_uobj);
2951 put_uobj_write(&obj->uevent.uobject);
2955 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2956 const char __user *buf, int in_len,
2959 struct ib_uverbs_create_srq cmd;
2960 struct ib_uverbs_create_xsrq xcmd;
2961 struct ib_uverbs_create_srq_resp resp;
2962 struct ib_udata udata;
2965 if (out_len < sizeof resp)
2968 if (copy_from_user(&cmd, buf, sizeof cmd))
2971 xcmd.response = cmd.response;
2972 xcmd.user_handle = cmd.user_handle;
2973 xcmd.srq_type = IB_SRQT_BASIC;
2974 xcmd.pd_handle = cmd.pd_handle;
2975 xcmd.max_wr = cmd.max_wr;
2976 xcmd.max_sge = cmd.max_sge;
2977 xcmd.srq_limit = cmd.srq_limit;
2979 INIT_UDATA(&udata, buf + sizeof cmd,
2980 (unsigned long) cmd.response + sizeof resp,
2981 in_len - sizeof cmd, out_len - sizeof resp);
2983 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2990 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2991 const char __user *buf, int in_len, int out_len)
2993 struct ib_uverbs_create_xsrq cmd;
2994 struct ib_uverbs_create_srq_resp resp;
2995 struct ib_udata udata;
2998 if (out_len < sizeof resp)
3001 if (copy_from_user(&cmd, buf, sizeof cmd))
3004 INIT_UDATA(&udata, buf + sizeof cmd,
3005 (unsigned long) cmd.response + sizeof resp,
3006 in_len - sizeof cmd, out_len - sizeof resp);
3008 ret = __uverbs_create_xsrq(file, &cmd, &udata);
3015 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
3016 const char __user *buf, int in_len,
3019 struct ib_uverbs_modify_srq cmd;
3020 struct ib_udata udata;
3022 struct ib_srq_attr attr;
3025 if (copy_from_user(&cmd, buf, sizeof cmd))
3028 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
3031 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3035 attr.max_wr = cmd.max_wr;
3036 attr.srq_limit = cmd.srq_limit;
3038 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
3042 return ret ? ret : in_len;
3045 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
3046 const char __user *buf,
3047 int in_len, int out_len)
3049 struct ib_uverbs_query_srq cmd;
3050 struct ib_uverbs_query_srq_resp resp;
3051 struct ib_srq_attr attr;
3055 if (out_len < sizeof resp)
3058 if (copy_from_user(&cmd, buf, sizeof cmd))
3061 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3065 ret = ib_query_srq(srq, &attr);
3072 memset(&resp, 0, sizeof resp);
3074 resp.max_wr = attr.max_wr;
3075 resp.max_sge = attr.max_sge;
3076 resp.srq_limit = attr.srq_limit;
3078 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3079 &resp, sizeof resp))
3085 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
3086 const char __user *buf, int in_len,
3089 struct ib_uverbs_destroy_srq cmd;
3090 struct ib_uverbs_destroy_srq_resp resp;
3091 struct ib_uobject *uobj;
3093 struct ib_uevent_object *obj;
3095 struct ib_usrq_object *us;
3096 enum ib_srq_type srq_type;
3098 if (copy_from_user(&cmd, buf, sizeof cmd))
3101 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
3105 obj = container_of(uobj, struct ib_uevent_object, uobject);
3106 srq_type = srq->srq_type;
3108 ret = ib_destroy_srq(srq);
3112 put_uobj_write(uobj);
3117 if (srq_type == IB_SRQT_XRC) {
3118 us = container_of(obj, struct ib_usrq_object, uevent);
3119 atomic_dec(&us->uxrcd->refcnt);
3122 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
3124 mutex_lock(&file->mutex);
3125 list_del(&uobj->list);
3126 mutex_unlock(&file->mutex);
3128 ib_uverbs_release_uevent(file, obj);
3130 memset(&resp, 0, sizeof resp);
3131 resp.events_reported = obj->events_reported;
3135 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3136 &resp, sizeof resp))
3139 return ret ? ret : in_len;