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 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING
58 static struct uverbs_lock_class rule_lock_class = { .name = "RULE-uobj" };
59 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
62 * The ib_uobject locking scheme is as follows:
64 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
65 * needs to be held during all idr operations. When an object is
66 * looked up, a reference must be taken on the object's kref before
69 * - Each object also has an rwsem. This rwsem must be held for
70 * reading while an operation that uses the object is performed.
71 * For example, while registering an MR, the associated PD's
72 * uobject.mutex must be held for reading. The rwsem must be held
73 * for writing while initializing or destroying an object.
75 * - In addition, each object has a "live" flag. If this flag is not
76 * set, then lookups of the object will fail even if it is found in
77 * the idr. This handles a reader that blocks and does not acquire
78 * the rwsem until after the object is destroyed. The destroy
79 * operation will set the live flag to 0 and then drop the rwsem;
80 * this will allow the reader to acquire the rwsem, see that the
81 * live flag is 0, and then drop the rwsem and its reference to
82 * object. The underlying storage will not be freed until the last
83 * reference to the object is dropped.
86 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
87 struct ib_ucontext *context, struct uverbs_lock_class *c)
89 uobj->user_handle = user_handle;
90 uobj->context = context;
91 kref_init(&uobj->ref);
92 init_rwsem(&uobj->mutex);
93 lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
97 static void release_uobj(struct kref *kref)
99 kfree(container_of(kref, struct ib_uobject, ref));
102 static void put_uobj(struct ib_uobject *uobj)
104 kref_put(&uobj->ref, release_uobj);
107 static void put_uobj_read(struct ib_uobject *uobj)
109 up_read(&uobj->mutex);
113 static void put_uobj_write(struct ib_uobject *uobj)
115 up_write(&uobj->mutex);
119 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
123 idr_preload(GFP_KERNEL);
124 spin_lock(&ib_uverbs_idr_lock);
126 ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
130 spin_unlock(&ib_uverbs_idr_lock);
133 return ret < 0 ? ret : 0;
136 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
138 spin_lock(&ib_uverbs_idr_lock);
139 idr_remove(idr, uobj->id);
140 spin_unlock(&ib_uverbs_idr_lock);
143 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
144 struct ib_ucontext *context)
146 struct ib_uobject *uobj;
148 spin_lock(&ib_uverbs_idr_lock);
149 uobj = idr_find(idr, id);
151 if (uobj->context == context)
152 kref_get(&uobj->ref);
156 spin_unlock(&ib_uverbs_idr_lock);
161 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
162 struct ib_ucontext *context, int nested)
164 struct ib_uobject *uobj;
166 uobj = __idr_get_uobj(idr, id, context);
171 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
173 down_read(&uobj->mutex);
182 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
183 struct ib_ucontext *context)
185 struct ib_uobject *uobj;
187 uobj = __idr_get_uobj(idr, id, context);
191 down_write(&uobj->mutex);
193 put_uobj_write(uobj);
200 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
203 struct ib_uobject *uobj;
205 uobj = idr_read_uobj(idr, id, context, nested);
206 return uobj ? uobj->object : NULL;
209 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
211 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
214 static void put_pd_read(struct ib_pd *pd)
216 put_uobj_read(pd->uobject);
219 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
221 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
224 static void put_cq_read(struct ib_cq *cq)
226 put_uobj_read(cq->uobject);
229 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
231 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
234 static void put_ah_read(struct ib_ah *ah)
236 put_uobj_read(ah->uobject);
239 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
241 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
244 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
246 struct ib_uobject *uobj;
248 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
249 return uobj ? uobj->object : NULL;
252 static void put_qp_read(struct ib_qp *qp)
254 put_uobj_read(qp->uobject);
257 static void put_qp_write(struct ib_qp *qp)
259 put_uobj_write(qp->uobject);
262 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
264 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
267 static void put_srq_read(struct ib_srq *srq)
269 put_uobj_read(srq->uobject);
272 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
273 struct ib_uobject **uobj)
275 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
276 return *uobj ? (*uobj)->object : NULL;
279 static void put_xrcd_read(struct ib_uobject *uobj)
284 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
285 const char __user *buf,
286 int in_len, int out_len)
288 struct ib_uverbs_get_context cmd;
289 struct ib_uverbs_get_context_resp resp;
290 struct ib_udata udata;
291 struct ib_device *ibdev = file->device->ib_dev;
292 struct ib_ucontext *ucontext;
296 if (out_len < sizeof resp)
299 if (copy_from_user(&cmd, buf, sizeof cmd))
302 mutex_lock(&file->mutex);
304 if (file->ucontext) {
309 INIT_UDATA(&udata, buf + sizeof cmd,
310 (unsigned long) cmd.response + sizeof resp,
311 in_len - sizeof cmd, out_len - sizeof resp);
313 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
314 if (IS_ERR(ucontext)) {
315 ret = PTR_ERR(ucontext);
319 ucontext->device = ibdev;
320 INIT_LIST_HEAD(&ucontext->pd_list);
321 INIT_LIST_HEAD(&ucontext->mr_list);
322 INIT_LIST_HEAD(&ucontext->mw_list);
323 INIT_LIST_HEAD(&ucontext->cq_list);
324 INIT_LIST_HEAD(&ucontext->qp_list);
325 INIT_LIST_HEAD(&ucontext->srq_list);
326 INIT_LIST_HEAD(&ucontext->ah_list);
327 INIT_LIST_HEAD(&ucontext->xrcd_list);
328 INIT_LIST_HEAD(&ucontext->rule_list);
329 ucontext->closing = 0;
331 resp.num_comp_vectors = file->device->num_comp_vectors;
333 ret = get_unused_fd_flags(O_CLOEXEC);
338 filp = ib_uverbs_alloc_event_file(file, 1);
344 if (copy_to_user((void __user *) (unsigned long) cmd.response,
345 &resp, sizeof resp)) {
350 file->async_file = filp->private_data;
352 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
353 ib_uverbs_event_handler);
354 ret = ib_register_event_handler(&file->event_handler);
358 kref_get(&file->async_file->ref);
359 kref_get(&file->ref);
360 file->ucontext = ucontext;
362 fd_install(resp.async_fd, filp);
364 mutex_unlock(&file->mutex);
372 put_unused_fd(resp.async_fd);
375 ibdev->dealloc_ucontext(ucontext);
378 mutex_unlock(&file->mutex);
382 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
383 const char __user *buf,
384 int in_len, int out_len)
386 struct ib_uverbs_query_device cmd;
387 struct ib_uverbs_query_device_resp resp;
388 struct ib_device_attr attr;
391 if (out_len < sizeof resp)
394 if (copy_from_user(&cmd, buf, sizeof cmd))
397 ret = ib_query_device(file->device->ib_dev, &attr);
401 memset(&resp, 0, sizeof resp);
403 resp.fw_ver = attr.fw_ver;
404 resp.node_guid = file->device->ib_dev->node_guid;
405 resp.sys_image_guid = attr.sys_image_guid;
406 resp.max_mr_size = attr.max_mr_size;
407 resp.page_size_cap = attr.page_size_cap;
408 resp.vendor_id = attr.vendor_id;
409 resp.vendor_part_id = attr.vendor_part_id;
410 resp.hw_ver = attr.hw_ver;
411 resp.max_qp = attr.max_qp;
412 resp.max_qp_wr = attr.max_qp_wr;
413 resp.device_cap_flags = attr.device_cap_flags;
414 resp.max_sge = attr.max_sge;
415 resp.max_sge_rd = attr.max_sge_rd;
416 resp.max_cq = attr.max_cq;
417 resp.max_cqe = attr.max_cqe;
418 resp.max_mr = attr.max_mr;
419 resp.max_pd = attr.max_pd;
420 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
421 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
422 resp.max_res_rd_atom = attr.max_res_rd_atom;
423 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
424 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
425 resp.atomic_cap = attr.atomic_cap;
426 resp.max_ee = attr.max_ee;
427 resp.max_rdd = attr.max_rdd;
428 resp.max_mw = attr.max_mw;
429 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
430 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
431 resp.max_mcast_grp = attr.max_mcast_grp;
432 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
433 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
434 resp.max_ah = attr.max_ah;
435 resp.max_fmr = attr.max_fmr;
436 resp.max_map_per_fmr = attr.max_map_per_fmr;
437 resp.max_srq = attr.max_srq;
438 resp.max_srq_wr = attr.max_srq_wr;
439 resp.max_srq_sge = attr.max_srq_sge;
440 resp.max_pkeys = attr.max_pkeys;
441 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
442 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
444 if (copy_to_user((void __user *) (unsigned long) cmd.response,
451 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
452 const char __user *buf,
453 int in_len, int out_len)
455 struct ib_uverbs_query_port cmd;
456 struct ib_uverbs_query_port_resp resp;
457 struct ib_port_attr attr;
460 if (out_len < sizeof resp)
463 if (copy_from_user(&cmd, buf, sizeof cmd))
466 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
470 memset(&resp, 0, sizeof resp);
472 resp.state = attr.state;
473 resp.max_mtu = attr.max_mtu;
474 resp.active_mtu = attr.active_mtu;
475 resp.gid_tbl_len = attr.gid_tbl_len;
476 resp.port_cap_flags = attr.port_cap_flags;
477 resp.max_msg_sz = attr.max_msg_sz;
478 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
479 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
480 resp.pkey_tbl_len = attr.pkey_tbl_len;
482 resp.sm_lid = attr.sm_lid;
484 resp.max_vl_num = attr.max_vl_num;
485 resp.sm_sl = attr.sm_sl;
486 resp.subnet_timeout = attr.subnet_timeout;
487 resp.init_type_reply = attr.init_type_reply;
488 resp.active_width = attr.active_width;
489 resp.active_speed = attr.active_speed;
490 resp.phys_state = attr.phys_state;
491 resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
494 if (copy_to_user((void __user *) (unsigned long) cmd.response,
501 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
502 const char __user *buf,
503 int in_len, int out_len)
505 struct ib_uverbs_alloc_pd cmd;
506 struct ib_uverbs_alloc_pd_resp resp;
507 struct ib_udata udata;
508 struct ib_uobject *uobj;
512 if (out_len < sizeof resp)
515 if (copy_from_user(&cmd, buf, sizeof cmd))
518 INIT_UDATA(&udata, buf + sizeof cmd,
519 (unsigned long) cmd.response + sizeof resp,
520 in_len - sizeof cmd, out_len - sizeof resp);
522 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
526 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
527 down_write(&uobj->mutex);
529 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
530 file->ucontext, &udata);
536 pd->device = file->device->ib_dev;
538 atomic_set(&pd->usecnt, 0);
541 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
545 memset(&resp, 0, sizeof resp);
546 resp.pd_handle = uobj->id;
548 if (copy_to_user((void __user *) (unsigned long) cmd.response,
549 &resp, sizeof resp)) {
554 mutex_lock(&file->mutex);
555 list_add_tail(&uobj->list, &file->ucontext->pd_list);
556 mutex_unlock(&file->mutex);
560 up_write(&uobj->mutex);
565 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
571 put_uobj_write(uobj);
575 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
576 const char __user *buf,
577 int in_len, int out_len)
579 struct ib_uverbs_dealloc_pd cmd;
580 struct ib_uobject *uobj;
583 if (copy_from_user(&cmd, buf, sizeof cmd))
586 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
590 ret = ib_dealloc_pd(uobj->object);
594 put_uobj_write(uobj);
599 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
601 mutex_lock(&file->mutex);
602 list_del(&uobj->list);
603 mutex_unlock(&file->mutex);
610 struct xrcd_table_entry {
612 struct ib_xrcd *xrcd;
616 static int xrcd_table_insert(struct ib_uverbs_device *dev,
618 struct ib_xrcd *xrcd)
620 struct xrcd_table_entry *entry, *scan;
621 struct rb_node **p = &dev->xrcd_tree.rb_node;
622 struct rb_node *parent = NULL;
624 entry = kmalloc(sizeof *entry, GFP_KERNEL);
629 entry->inode = inode;
633 scan = rb_entry(parent, struct xrcd_table_entry, node);
635 if (inode < scan->inode) {
637 } else if (inode > scan->inode) {
645 rb_link_node(&entry->node, parent, p);
646 rb_insert_color(&entry->node, &dev->xrcd_tree);
651 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
654 struct xrcd_table_entry *entry;
655 struct rb_node *p = dev->xrcd_tree.rb_node;
658 entry = rb_entry(p, struct xrcd_table_entry, node);
660 if (inode < entry->inode)
662 else if (inode > entry->inode)
671 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
673 struct xrcd_table_entry *entry;
675 entry = xrcd_table_search(dev, inode);
682 static void xrcd_table_delete(struct ib_uverbs_device *dev,
685 struct xrcd_table_entry *entry;
687 entry = xrcd_table_search(dev, inode);
690 rb_erase(&entry->node, &dev->xrcd_tree);
695 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
696 const char __user *buf, int in_len,
699 struct ib_uverbs_open_xrcd cmd;
700 struct ib_uverbs_open_xrcd_resp resp;
701 struct ib_udata udata;
702 struct ib_uxrcd_object *obj;
703 struct ib_xrcd *xrcd = NULL;
704 struct fd f = {NULL, 0};
705 struct inode *inode = NULL;
709 if (out_len < sizeof resp)
712 if (copy_from_user(&cmd, buf, sizeof cmd))
715 INIT_UDATA(&udata, buf + sizeof cmd,
716 (unsigned long) cmd.response + sizeof resp,
717 in_len - sizeof cmd, out_len - sizeof resp);
719 mutex_lock(&file->device->xrcd_tree_mutex);
722 /* search for file descriptor */
726 goto err_tree_mutex_unlock;
729 inode = file_inode(f.file);
730 xrcd = find_xrcd(file->device, inode);
731 if (!xrcd && !(cmd.oflags & O_CREAT)) {
732 /* no file descriptor. Need CREATE flag */
734 goto err_tree_mutex_unlock;
737 if (xrcd && cmd.oflags & O_EXCL) {
739 goto err_tree_mutex_unlock;
743 obj = kmalloc(sizeof *obj, GFP_KERNEL);
746 goto err_tree_mutex_unlock;
749 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
751 down_write(&obj->uobject.mutex);
754 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
755 file->ucontext, &udata);
762 xrcd->device = file->device->ib_dev;
763 atomic_set(&xrcd->usecnt, 0);
764 mutex_init(&xrcd->tgt_qp_mutex);
765 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
769 atomic_set(&obj->refcnt, 0);
770 obj->uobject.object = xrcd;
771 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
775 memset(&resp, 0, sizeof resp);
776 resp.xrcd_handle = obj->uobject.id;
780 /* create new inode/xrcd table entry */
781 ret = xrcd_table_insert(file->device, inode, xrcd);
783 goto err_insert_xrcd;
785 atomic_inc(&xrcd->usecnt);
788 if (copy_to_user((void __user *) (unsigned long) cmd.response,
789 &resp, sizeof resp)) {
797 mutex_lock(&file->mutex);
798 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
799 mutex_unlock(&file->mutex);
801 obj->uobject.live = 1;
802 up_write(&obj->uobject.mutex);
804 mutex_unlock(&file->device->xrcd_tree_mutex);
810 xrcd_table_delete(file->device, inode);
811 atomic_dec(&xrcd->usecnt);
815 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
818 ib_dealloc_xrcd(xrcd);
821 put_uobj_write(&obj->uobject);
823 err_tree_mutex_unlock:
827 mutex_unlock(&file->device->xrcd_tree_mutex);
832 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
833 const char __user *buf, int in_len,
836 struct ib_uverbs_close_xrcd cmd;
837 struct ib_uobject *uobj;
838 struct ib_xrcd *xrcd = NULL;
839 struct inode *inode = NULL;
840 struct ib_uxrcd_object *obj;
844 if (copy_from_user(&cmd, buf, sizeof cmd))
847 mutex_lock(&file->device->xrcd_tree_mutex);
848 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
856 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
857 if (atomic_read(&obj->refcnt)) {
858 put_uobj_write(uobj);
863 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
864 ret = ib_dealloc_xrcd(uobj->object);
871 atomic_inc(&xrcd->usecnt);
873 put_uobj_write(uobj);
879 xrcd_table_delete(file->device, inode);
881 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
882 mutex_lock(&file->mutex);
883 list_del(&uobj->list);
884 mutex_unlock(&file->mutex);
890 mutex_unlock(&file->device->xrcd_tree_mutex);
894 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
895 struct ib_xrcd *xrcd)
900 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
903 ib_dealloc_xrcd(xrcd);
906 xrcd_table_delete(dev, inode);
909 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
910 const char __user *buf, int in_len,
913 struct ib_uverbs_reg_mr cmd;
914 struct ib_uverbs_reg_mr_resp resp;
915 struct ib_udata udata;
916 struct ib_uobject *uobj;
921 if (out_len < sizeof resp)
924 if (copy_from_user(&cmd, buf, sizeof cmd))
927 INIT_UDATA(&udata, buf + sizeof cmd,
928 (unsigned long) cmd.response + sizeof resp,
929 in_len - sizeof cmd, out_len - sizeof resp);
931 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
935 * Local write permission is required if remote write or
936 * remote atomic permission is also requested.
938 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
939 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
942 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
946 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
947 down_write(&uobj->mutex);
949 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
955 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
956 cmd.access_flags, &udata);
962 mr->device = pd->device;
965 atomic_inc(&pd->usecnt);
966 atomic_set(&mr->usecnt, 0);
969 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
973 memset(&resp, 0, sizeof resp);
974 resp.lkey = mr->lkey;
975 resp.rkey = mr->rkey;
976 resp.mr_handle = uobj->id;
978 if (copy_to_user((void __user *) (unsigned long) cmd.response,
979 &resp, sizeof resp)) {
986 mutex_lock(&file->mutex);
987 list_add_tail(&uobj->list, &file->ucontext->mr_list);
988 mutex_unlock(&file->mutex);
992 up_write(&uobj->mutex);
997 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1006 put_uobj_write(uobj);
1010 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1011 const char __user *buf, int in_len,
1014 struct ib_uverbs_dereg_mr cmd;
1016 struct ib_uobject *uobj;
1019 if (copy_from_user(&cmd, buf, sizeof cmd))
1022 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1028 ret = ib_dereg_mr(mr);
1032 put_uobj_write(uobj);
1037 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1039 mutex_lock(&file->mutex);
1040 list_del(&uobj->list);
1041 mutex_unlock(&file->mutex);
1048 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
1049 const char __user *buf, int in_len,
1052 struct ib_uverbs_alloc_mw cmd;
1053 struct ib_uverbs_alloc_mw_resp resp;
1054 struct ib_uobject *uobj;
1059 if (out_len < sizeof(resp))
1062 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1065 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1069 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1070 down_write(&uobj->mutex);
1072 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1078 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1084 mw->device = pd->device;
1087 atomic_inc(&pd->usecnt);
1090 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1094 memset(&resp, 0, sizeof(resp));
1095 resp.rkey = mw->rkey;
1096 resp.mw_handle = uobj->id;
1098 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1099 &resp, sizeof(resp))) {
1106 mutex_lock(&file->mutex);
1107 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1108 mutex_unlock(&file->mutex);
1112 up_write(&uobj->mutex);
1117 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1126 put_uobj_write(uobj);
1130 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
1131 const char __user *buf, int in_len,
1134 struct ib_uverbs_dealloc_mw cmd;
1136 struct ib_uobject *uobj;
1139 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1142 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1148 ret = ib_dealloc_mw(mw);
1152 put_uobj_write(uobj);
1157 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1159 mutex_lock(&file->mutex);
1160 list_del(&uobj->list);
1161 mutex_unlock(&file->mutex);
1168 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1169 const char __user *buf, int in_len,
1172 struct ib_uverbs_create_comp_channel cmd;
1173 struct ib_uverbs_create_comp_channel_resp resp;
1177 if (out_len < sizeof resp)
1180 if (copy_from_user(&cmd, buf, sizeof cmd))
1183 ret = get_unused_fd_flags(O_CLOEXEC);
1188 filp = ib_uverbs_alloc_event_file(file, 0);
1190 put_unused_fd(resp.fd);
1191 return PTR_ERR(filp);
1194 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1195 &resp, sizeof resp)) {
1196 put_unused_fd(resp.fd);
1201 fd_install(resp.fd, filp);
1205 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1206 const char __user *buf, int in_len,
1209 struct ib_uverbs_create_cq cmd;
1210 struct ib_uverbs_create_cq_resp resp;
1211 struct ib_udata udata;
1212 struct ib_ucq_object *obj;
1213 struct ib_uverbs_event_file *ev_file = NULL;
1217 if (out_len < sizeof resp)
1220 if (copy_from_user(&cmd, buf, sizeof cmd))
1223 INIT_UDATA(&udata, buf + sizeof cmd,
1224 (unsigned long) cmd.response + sizeof resp,
1225 in_len - sizeof cmd, out_len - sizeof resp);
1227 if (cmd.comp_vector >= file->device->num_comp_vectors)
1230 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1234 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1235 down_write(&obj->uobject.mutex);
1237 if (cmd.comp_channel >= 0) {
1238 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1245 obj->uverbs_file = file;
1246 obj->comp_events_reported = 0;
1247 obj->async_events_reported = 0;
1248 INIT_LIST_HEAD(&obj->comp_list);
1249 INIT_LIST_HEAD(&obj->async_list);
1251 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1253 file->ucontext, &udata);
1259 cq->device = file->device->ib_dev;
1260 cq->uobject = &obj->uobject;
1261 cq->comp_handler = ib_uverbs_comp_handler;
1262 cq->event_handler = ib_uverbs_cq_event_handler;
1263 cq->cq_context = ev_file;
1264 atomic_set(&cq->usecnt, 0);
1266 obj->uobject.object = cq;
1267 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1271 memset(&resp, 0, sizeof resp);
1272 resp.cq_handle = obj->uobject.id;
1275 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1276 &resp, sizeof resp)) {
1281 mutex_lock(&file->mutex);
1282 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1283 mutex_unlock(&file->mutex);
1285 obj->uobject.live = 1;
1287 up_write(&obj->uobject.mutex);
1292 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1299 ib_uverbs_release_ucq(file, ev_file, obj);
1302 put_uobj_write(&obj->uobject);
1306 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1307 const char __user *buf, int in_len,
1310 struct ib_uverbs_resize_cq cmd;
1311 struct ib_uverbs_resize_cq_resp resp;
1312 struct ib_udata udata;
1316 if (copy_from_user(&cmd, buf, sizeof cmd))
1319 INIT_UDATA(&udata, buf + sizeof cmd,
1320 (unsigned long) cmd.response + sizeof resp,
1321 in_len - sizeof cmd, out_len - sizeof resp);
1323 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1327 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1333 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1334 &resp, sizeof resp.cqe))
1340 return ret ? ret : in_len;
1343 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1345 struct ib_uverbs_wc tmp;
1347 tmp.wr_id = wc->wr_id;
1348 tmp.status = wc->status;
1349 tmp.opcode = wc->opcode;
1350 tmp.vendor_err = wc->vendor_err;
1351 tmp.byte_len = wc->byte_len;
1352 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1353 tmp.qp_num = wc->qp->qp_num;
1354 tmp.src_qp = wc->src_qp;
1355 tmp.wc_flags = wc->wc_flags;
1356 tmp.pkey_index = wc->pkey_index;
1357 tmp.slid = wc->slid;
1359 tmp.dlid_path_bits = wc->dlid_path_bits;
1360 tmp.port_num = wc->port_num;
1363 if (copy_to_user(dest, &tmp, sizeof tmp))
1369 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1370 const char __user *buf, int in_len,
1373 struct ib_uverbs_poll_cq cmd;
1374 struct ib_uverbs_poll_cq_resp resp;
1375 u8 __user *header_ptr;
1376 u8 __user *data_ptr;
1381 if (copy_from_user(&cmd, buf, sizeof cmd))
1384 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1388 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1389 header_ptr = (void __user *)(unsigned long) cmd.response;
1390 data_ptr = header_ptr + sizeof resp;
1392 memset(&resp, 0, sizeof resp);
1393 while (resp.count < cmd.ne) {
1394 ret = ib_poll_cq(cq, 1, &wc);
1400 ret = copy_wc_to_user(data_ptr, &wc);
1404 data_ptr += sizeof(struct ib_uverbs_wc);
1408 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1420 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1421 const char __user *buf, int in_len,
1424 struct ib_uverbs_req_notify_cq cmd;
1427 if (copy_from_user(&cmd, buf, sizeof cmd))
1430 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1434 ib_req_notify_cq(cq, cmd.solicited_only ?
1435 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1442 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1443 const char __user *buf, int in_len,
1446 struct ib_uverbs_destroy_cq cmd;
1447 struct ib_uverbs_destroy_cq_resp resp;
1448 struct ib_uobject *uobj;
1450 struct ib_ucq_object *obj;
1451 struct ib_uverbs_event_file *ev_file;
1454 if (copy_from_user(&cmd, buf, sizeof cmd))
1457 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1461 ev_file = cq->cq_context;
1462 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1464 ret = ib_destroy_cq(cq);
1468 put_uobj_write(uobj);
1473 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1475 mutex_lock(&file->mutex);
1476 list_del(&uobj->list);
1477 mutex_unlock(&file->mutex);
1479 ib_uverbs_release_ucq(file, ev_file, obj);
1481 memset(&resp, 0, sizeof resp);
1482 resp.comp_events_reported = obj->comp_events_reported;
1483 resp.async_events_reported = obj->async_events_reported;
1487 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1488 &resp, sizeof resp))
1494 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1495 const char __user *buf, int in_len,
1498 struct ib_uverbs_create_qp cmd;
1499 struct ib_uverbs_create_qp_resp resp;
1500 struct ib_udata udata;
1501 struct ib_uqp_object *obj;
1502 struct ib_device *device;
1503 struct ib_pd *pd = NULL;
1504 struct ib_xrcd *xrcd = NULL;
1505 struct ib_uobject *uninitialized_var(xrcd_uobj);
1506 struct ib_cq *scq = NULL, *rcq = NULL;
1507 struct ib_srq *srq = NULL;
1509 struct ib_qp_init_attr attr;
1512 if (out_len < sizeof resp)
1515 if (copy_from_user(&cmd, buf, sizeof cmd))
1518 if (cmd.qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1521 INIT_UDATA(&udata, buf + sizeof cmd,
1522 (unsigned long) cmd.response + sizeof resp,
1523 in_len - sizeof cmd, out_len - sizeof resp);
1525 obj = kzalloc(sizeof *obj, GFP_KERNEL);
1529 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1530 down_write(&obj->uevent.uobject.mutex);
1532 if (cmd.qp_type == IB_QPT_XRC_TGT) {
1533 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1538 device = xrcd->device;
1540 if (cmd.qp_type == IB_QPT_XRC_INI) {
1541 cmd.max_recv_wr = cmd.max_recv_sge = 0;
1544 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1545 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1551 if (cmd.recv_cq_handle != cmd.send_cq_handle) {
1552 rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext, 0);
1560 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, !!rcq);
1562 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1568 device = pd->device;
1571 attr.event_handler = ib_uverbs_qp_event_handler;
1572 attr.qp_context = file;
1577 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1578 attr.qp_type = cmd.qp_type;
1579 attr.create_flags = 0;
1581 attr.cap.max_send_wr = cmd.max_send_wr;
1582 attr.cap.max_recv_wr = cmd.max_recv_wr;
1583 attr.cap.max_send_sge = cmd.max_send_sge;
1584 attr.cap.max_recv_sge = cmd.max_recv_sge;
1585 attr.cap.max_inline_data = cmd.max_inline_data;
1587 obj->uevent.events_reported = 0;
1588 INIT_LIST_HEAD(&obj->uevent.event_list);
1589 INIT_LIST_HEAD(&obj->mcast_list);
1591 if (cmd.qp_type == IB_QPT_XRC_TGT)
1592 qp = ib_create_qp(pd, &attr);
1594 qp = device->create_qp(pd, &attr, &udata);
1601 if (cmd.qp_type != IB_QPT_XRC_TGT) {
1603 qp->device = device;
1605 qp->send_cq = attr.send_cq;
1606 qp->recv_cq = attr.recv_cq;
1608 qp->event_handler = attr.event_handler;
1609 qp->qp_context = attr.qp_context;
1610 qp->qp_type = attr.qp_type;
1611 atomic_set(&qp->usecnt, 0);
1612 atomic_inc(&pd->usecnt);
1613 atomic_inc(&attr.send_cq->usecnt);
1615 atomic_inc(&attr.recv_cq->usecnt);
1617 atomic_inc(&attr.srq->usecnt);
1619 qp->uobject = &obj->uevent.uobject;
1621 obj->uevent.uobject.object = qp;
1622 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1626 memset(&resp, 0, sizeof resp);
1627 resp.qpn = qp->qp_num;
1628 resp.qp_handle = obj->uevent.uobject.id;
1629 resp.max_recv_sge = attr.cap.max_recv_sge;
1630 resp.max_send_sge = attr.cap.max_send_sge;
1631 resp.max_recv_wr = attr.cap.max_recv_wr;
1632 resp.max_send_wr = attr.cap.max_send_wr;
1633 resp.max_inline_data = attr.cap.max_inline_data;
1635 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1636 &resp, sizeof resp)) {
1642 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1644 atomic_inc(&obj->uxrcd->refcnt);
1645 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 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1758 atomic_inc(&obj->uxrcd->refcnt);
1759 put_xrcd_read(xrcd_uobj);
1761 mutex_lock(&file->mutex);
1762 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1763 mutex_unlock(&file->mutex);
1765 obj->uevent.uobject.live = 1;
1767 up_write(&obj->uevent.uobject.mutex);
1772 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1778 put_xrcd_read(xrcd_uobj);
1779 put_uobj_write(&obj->uevent.uobject);
1783 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1784 const char __user *buf, int in_len,
1787 struct ib_uverbs_query_qp cmd;
1788 struct ib_uverbs_query_qp_resp resp;
1790 struct ib_qp_attr *attr;
1791 struct ib_qp_init_attr *init_attr;
1794 if (copy_from_user(&cmd, buf, sizeof cmd))
1797 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1798 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1799 if (!attr || !init_attr) {
1804 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1810 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1817 memset(&resp, 0, sizeof resp);
1819 resp.qp_state = attr->qp_state;
1820 resp.cur_qp_state = attr->cur_qp_state;
1821 resp.path_mtu = attr->path_mtu;
1822 resp.path_mig_state = attr->path_mig_state;
1823 resp.qkey = attr->qkey;
1824 resp.rq_psn = attr->rq_psn;
1825 resp.sq_psn = attr->sq_psn;
1826 resp.dest_qp_num = attr->dest_qp_num;
1827 resp.qp_access_flags = attr->qp_access_flags;
1828 resp.pkey_index = attr->pkey_index;
1829 resp.alt_pkey_index = attr->alt_pkey_index;
1830 resp.sq_draining = attr->sq_draining;
1831 resp.max_rd_atomic = attr->max_rd_atomic;
1832 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1833 resp.min_rnr_timer = attr->min_rnr_timer;
1834 resp.port_num = attr->port_num;
1835 resp.timeout = attr->timeout;
1836 resp.retry_cnt = attr->retry_cnt;
1837 resp.rnr_retry = attr->rnr_retry;
1838 resp.alt_port_num = attr->alt_port_num;
1839 resp.alt_timeout = attr->alt_timeout;
1841 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1842 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1843 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1844 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1845 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1846 resp.dest.dlid = attr->ah_attr.dlid;
1847 resp.dest.sl = attr->ah_attr.sl;
1848 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1849 resp.dest.static_rate = attr->ah_attr.static_rate;
1850 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1851 resp.dest.port_num = attr->ah_attr.port_num;
1853 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1854 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1855 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1856 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1857 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1858 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1859 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1860 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1861 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1862 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1863 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1865 resp.max_send_wr = init_attr->cap.max_send_wr;
1866 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1867 resp.max_send_sge = init_attr->cap.max_send_sge;
1868 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1869 resp.max_inline_data = init_attr->cap.max_inline_data;
1870 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1872 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1873 &resp, sizeof resp))
1880 return ret ? ret : in_len;
1883 /* Remove ignored fields set in the attribute mask */
1884 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1887 case IB_QPT_XRC_INI:
1888 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1889 case IB_QPT_XRC_TGT:
1890 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1897 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1898 const char __user *buf, int in_len,
1901 struct ib_uverbs_modify_qp cmd;
1902 struct ib_udata udata;
1904 struct ib_qp_attr *attr;
1907 if (copy_from_user(&cmd, buf, sizeof cmd))
1910 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1913 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1917 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1923 attr->qp_state = cmd.qp_state;
1924 attr->cur_qp_state = cmd.cur_qp_state;
1925 attr->path_mtu = cmd.path_mtu;
1926 attr->path_mig_state = cmd.path_mig_state;
1927 attr->qkey = cmd.qkey;
1928 attr->rq_psn = cmd.rq_psn;
1929 attr->sq_psn = cmd.sq_psn;
1930 attr->dest_qp_num = cmd.dest_qp_num;
1931 attr->qp_access_flags = cmd.qp_access_flags;
1932 attr->pkey_index = cmd.pkey_index;
1933 attr->alt_pkey_index = cmd.alt_pkey_index;
1934 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1935 attr->max_rd_atomic = cmd.max_rd_atomic;
1936 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1937 attr->min_rnr_timer = cmd.min_rnr_timer;
1938 attr->port_num = cmd.port_num;
1939 attr->timeout = cmd.timeout;
1940 attr->retry_cnt = cmd.retry_cnt;
1941 attr->rnr_retry = cmd.rnr_retry;
1942 attr->alt_port_num = cmd.alt_port_num;
1943 attr->alt_timeout = cmd.alt_timeout;
1945 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1946 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1947 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1948 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1949 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1950 attr->ah_attr.dlid = cmd.dest.dlid;
1951 attr->ah_attr.sl = cmd.dest.sl;
1952 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1953 attr->ah_attr.static_rate = cmd.dest.static_rate;
1954 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1955 attr->ah_attr.port_num = cmd.dest.port_num;
1957 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1958 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1959 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1960 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1961 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1962 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1963 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1964 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1965 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1966 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1967 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1969 if (qp->real_qp == qp) {
1970 ret = qp->device->modify_qp(qp, attr,
1971 modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
1973 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
1989 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1990 const char __user *buf, int in_len,
1993 struct ib_uverbs_destroy_qp cmd;
1994 struct ib_uverbs_destroy_qp_resp resp;
1995 struct ib_uobject *uobj;
1997 struct ib_uqp_object *obj;
2000 if (copy_from_user(&cmd, buf, sizeof cmd))
2003 memset(&resp, 0, sizeof resp);
2005 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
2009 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2011 if (!list_empty(&obj->mcast_list)) {
2012 put_uobj_write(uobj);
2016 ret = ib_destroy_qp(qp);
2020 put_uobj_write(uobj);
2026 atomic_dec(&obj->uxrcd->refcnt);
2028 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
2030 mutex_lock(&file->mutex);
2031 list_del(&uobj->list);
2032 mutex_unlock(&file->mutex);
2034 ib_uverbs_release_uevent(file, &obj->uevent);
2036 resp.events_reported = obj->uevent.events_reported;
2040 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2041 &resp, sizeof resp))
2047 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2048 const char __user *buf, int in_len,
2051 struct ib_uverbs_post_send cmd;
2052 struct ib_uverbs_post_send_resp resp;
2053 struct ib_uverbs_send_wr *user_wr;
2054 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2058 ssize_t ret = -EINVAL;
2060 if (copy_from_user(&cmd, buf, sizeof cmd))
2063 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2064 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2067 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2070 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2074 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2078 is_ud = qp->qp_type == IB_QPT_UD;
2081 for (i = 0; i < cmd.wr_count; ++i) {
2082 if (copy_from_user(user_wr,
2083 buf + sizeof cmd + i * cmd.wqe_size,
2089 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2094 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2095 user_wr->num_sge * sizeof (struct ib_sge),
2109 next->wr_id = user_wr->wr_id;
2110 next->num_sge = user_wr->num_sge;
2111 next->opcode = user_wr->opcode;
2112 next->send_flags = user_wr->send_flags;
2115 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
2117 if (!next->wr.ud.ah) {
2121 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
2122 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
2124 switch (next->opcode) {
2125 case IB_WR_RDMA_WRITE_WITH_IMM:
2127 (__be32 __force) user_wr->ex.imm_data;
2128 case IB_WR_RDMA_WRITE:
2129 case IB_WR_RDMA_READ:
2130 next->wr.rdma.remote_addr =
2131 user_wr->wr.rdma.remote_addr;
2132 next->wr.rdma.rkey =
2133 user_wr->wr.rdma.rkey;
2135 case IB_WR_SEND_WITH_IMM:
2137 (__be32 __force) user_wr->ex.imm_data;
2139 case IB_WR_SEND_WITH_INV:
2140 next->ex.invalidate_rkey =
2141 user_wr->ex.invalidate_rkey;
2143 case IB_WR_ATOMIC_CMP_AND_SWP:
2144 case IB_WR_ATOMIC_FETCH_AND_ADD:
2145 next->wr.atomic.remote_addr =
2146 user_wr->wr.atomic.remote_addr;
2147 next->wr.atomic.compare_add =
2148 user_wr->wr.atomic.compare_add;
2149 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2150 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2157 if (next->num_sge) {
2158 next->sg_list = (void *) next +
2159 ALIGN(sizeof *next, sizeof (struct ib_sge));
2160 if (copy_from_user(next->sg_list,
2162 cmd.wr_count * cmd.wqe_size +
2163 sg_ind * sizeof (struct ib_sge),
2164 next->num_sge * sizeof (struct ib_sge))) {
2168 sg_ind += next->num_sge;
2170 next->sg_list = NULL;
2174 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2176 for (next = wr; next; next = next->next) {
2182 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2183 &resp, sizeof resp))
2190 if (is_ud && wr->wr.ud.ah)
2191 put_ah_read(wr->wr.ud.ah);
2200 return ret ? ret : in_len;
2203 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2209 struct ib_uverbs_recv_wr *user_wr;
2210 struct ib_recv_wr *wr = NULL, *last, *next;
2215 if (in_len < wqe_size * wr_count +
2216 sge_count * sizeof (struct ib_uverbs_sge))
2217 return ERR_PTR(-EINVAL);
2219 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2220 return ERR_PTR(-EINVAL);
2222 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2224 return ERR_PTR(-ENOMEM);
2228 for (i = 0; i < wr_count; ++i) {
2229 if (copy_from_user(user_wr, buf + i * wqe_size,
2235 if (user_wr->num_sge + sg_ind > sge_count) {
2240 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2241 user_wr->num_sge * sizeof (struct ib_sge),
2255 next->wr_id = user_wr->wr_id;
2256 next->num_sge = user_wr->num_sge;
2258 if (next->num_sge) {
2259 next->sg_list = (void *) next +
2260 ALIGN(sizeof *next, sizeof (struct ib_sge));
2261 if (copy_from_user(next->sg_list,
2262 buf + wr_count * wqe_size +
2263 sg_ind * sizeof (struct ib_sge),
2264 next->num_sge * sizeof (struct ib_sge))) {
2268 sg_ind += next->num_sge;
2270 next->sg_list = NULL;
2285 return ERR_PTR(ret);
2288 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2289 const char __user *buf, int in_len,
2292 struct ib_uverbs_post_recv cmd;
2293 struct ib_uverbs_post_recv_resp resp;
2294 struct ib_recv_wr *wr, *next, *bad_wr;
2296 ssize_t ret = -EINVAL;
2298 if (copy_from_user(&cmd, buf, sizeof cmd))
2301 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2302 in_len - sizeof cmd, cmd.wr_count,
2303 cmd.sge_count, cmd.wqe_size);
2307 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2312 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2317 for (next = wr; next; next = next->next) {
2323 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2324 &resp, sizeof resp))
2334 return ret ? ret : in_len;
2337 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2338 const char __user *buf, int in_len,
2341 struct ib_uverbs_post_srq_recv cmd;
2342 struct ib_uverbs_post_srq_recv_resp resp;
2343 struct ib_recv_wr *wr, *next, *bad_wr;
2345 ssize_t ret = -EINVAL;
2347 if (copy_from_user(&cmd, buf, sizeof cmd))
2350 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2351 in_len - sizeof cmd, cmd.wr_count,
2352 cmd.sge_count, cmd.wqe_size);
2356 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2361 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2366 for (next = wr; next; next = next->next) {
2372 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2373 &resp, sizeof resp))
2383 return ret ? ret : in_len;
2386 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2387 const char __user *buf, int in_len,
2390 struct ib_uverbs_create_ah cmd;
2391 struct ib_uverbs_create_ah_resp resp;
2392 struct ib_uobject *uobj;
2395 struct ib_ah_attr attr;
2398 if (out_len < sizeof resp)
2401 if (copy_from_user(&cmd, buf, sizeof cmd))
2404 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2408 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2409 down_write(&uobj->mutex);
2411 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2417 attr.dlid = cmd.attr.dlid;
2418 attr.sl = cmd.attr.sl;
2419 attr.src_path_bits = cmd.attr.src_path_bits;
2420 attr.static_rate = cmd.attr.static_rate;
2421 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
2422 attr.port_num = cmd.attr.port_num;
2423 attr.grh.flow_label = cmd.attr.grh.flow_label;
2424 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2425 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2426 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2427 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2429 ah = ib_create_ah(pd, &attr);
2438 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2442 resp.ah_handle = uobj->id;
2444 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2445 &resp, sizeof resp)) {
2452 mutex_lock(&file->mutex);
2453 list_add_tail(&uobj->list, &file->ucontext->ah_list);
2454 mutex_unlock(&file->mutex);
2458 up_write(&uobj->mutex);
2463 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2472 put_uobj_write(uobj);
2476 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2477 const char __user *buf, int in_len, int out_len)
2479 struct ib_uverbs_destroy_ah cmd;
2481 struct ib_uobject *uobj;
2484 if (copy_from_user(&cmd, buf, sizeof cmd))
2487 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2492 ret = ib_destroy_ah(ah);
2496 put_uobj_write(uobj);
2501 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2503 mutex_lock(&file->mutex);
2504 list_del(&uobj->list);
2505 mutex_unlock(&file->mutex);
2512 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2513 const char __user *buf, int in_len,
2516 struct ib_uverbs_attach_mcast cmd;
2518 struct ib_uqp_object *obj;
2519 struct ib_uverbs_mcast_entry *mcast;
2522 if (copy_from_user(&cmd, buf, sizeof cmd))
2525 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2529 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2531 list_for_each_entry(mcast, &obj->mcast_list, list)
2532 if (cmd.mlid == mcast->lid &&
2533 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2538 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2544 mcast->lid = cmd.mlid;
2545 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2547 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2549 list_add_tail(&mcast->list, &obj->mcast_list);
2556 return ret ? ret : in_len;
2559 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2560 const char __user *buf, int in_len,
2563 struct ib_uverbs_detach_mcast cmd;
2564 struct ib_uqp_object *obj;
2566 struct ib_uverbs_mcast_entry *mcast;
2569 if (copy_from_user(&cmd, buf, sizeof cmd))
2572 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2576 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2580 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2582 list_for_each_entry(mcast, &obj->mcast_list, list)
2583 if (cmd.mlid == mcast->lid &&
2584 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2585 list_del(&mcast->list);
2593 return ret ? ret : in_len;
2596 #ifdef CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING
2597 static int kern_spec_to_ib_spec(struct ib_uverbs_flow_spec *kern_spec,
2598 union ib_flow_spec *ib_spec)
2600 ib_spec->type = kern_spec->type;
2602 switch (ib_spec->type) {
2603 case IB_FLOW_SPEC_ETH:
2604 ib_spec->eth.size = sizeof(struct ib_flow_spec_eth);
2605 if (ib_spec->eth.size != kern_spec->eth.size)
2607 memcpy(&ib_spec->eth.val, &kern_spec->eth.val,
2608 sizeof(struct ib_flow_eth_filter));
2609 memcpy(&ib_spec->eth.mask, &kern_spec->eth.mask,
2610 sizeof(struct ib_flow_eth_filter));
2612 case IB_FLOW_SPEC_IPV4:
2613 ib_spec->ipv4.size = sizeof(struct ib_flow_spec_ipv4);
2614 if (ib_spec->ipv4.size != kern_spec->ipv4.size)
2616 memcpy(&ib_spec->ipv4.val, &kern_spec->ipv4.val,
2617 sizeof(struct ib_flow_ipv4_filter));
2618 memcpy(&ib_spec->ipv4.mask, &kern_spec->ipv4.mask,
2619 sizeof(struct ib_flow_ipv4_filter));
2621 case IB_FLOW_SPEC_TCP:
2622 case IB_FLOW_SPEC_UDP:
2623 ib_spec->tcp_udp.size = sizeof(struct ib_flow_spec_tcp_udp);
2624 if (ib_spec->tcp_udp.size != kern_spec->tcp_udp.size)
2626 memcpy(&ib_spec->tcp_udp.val, &kern_spec->tcp_udp.val,
2627 sizeof(struct ib_flow_tcp_udp_filter));
2628 memcpy(&ib_spec->tcp_udp.mask, &kern_spec->tcp_udp.mask,
2629 sizeof(struct ib_flow_tcp_udp_filter));
2637 int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
2638 struct ib_udata *ucore,
2639 struct ib_udata *uhw)
2641 struct ib_uverbs_create_flow cmd;
2642 struct ib_uverbs_create_flow_resp resp;
2643 struct ib_uobject *uobj;
2644 struct ib_flow *flow_id;
2645 struct ib_uverbs_flow_attr *kern_flow_attr;
2646 struct ib_flow_attr *flow_attr;
2653 if (ucore->outlen < sizeof(resp))
2656 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
2660 ucore->inbuf += sizeof(cmd);
2661 ucore->inlen -= sizeof(cmd);
2666 if ((cmd.flow_attr.type == IB_FLOW_ATTR_SNIFFER &&
2667 !capable(CAP_NET_ADMIN)) || !capable(CAP_NET_RAW))
2670 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
2673 if (cmd.flow_attr.size > ucore->inlen ||
2674 cmd.flow_attr.size >
2675 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
2678 if (cmd.flow_attr.num_of_specs) {
2679 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
2681 if (!kern_flow_attr)
2684 memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr));
2685 err = ib_copy_from_udata(kern_flow_attr + 1, ucore,
2686 cmd.flow_attr.size);
2690 kern_flow_attr = &cmd.flow_attr;
2693 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
2698 init_uobj(uobj, 0, file->ucontext, &rule_lock_class);
2699 down_write(&uobj->mutex);
2701 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2707 flow_attr = kmalloc(sizeof(*flow_attr) + cmd.flow_attr.size, GFP_KERNEL);
2713 flow_attr->type = kern_flow_attr->type;
2714 flow_attr->priority = kern_flow_attr->priority;
2715 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
2716 flow_attr->port = kern_flow_attr->port;
2717 flow_attr->flags = kern_flow_attr->flags;
2718 flow_attr->size = sizeof(*flow_attr);
2720 kern_spec = kern_flow_attr + 1;
2721 ib_spec = flow_attr + 1;
2722 for (i = 0; i < flow_attr->num_of_specs &&
2723 cmd.flow_attr.size > offsetof(struct ib_uverbs_flow_spec, reserved) &&
2724 cmd.flow_attr.size >=
2725 ((struct ib_uverbs_flow_spec *)kern_spec)->size; i++) {
2726 err = kern_spec_to_ib_spec(kern_spec, ib_spec);
2730 ((union ib_flow_spec *) ib_spec)->size;
2731 cmd.flow_attr.size -= ((struct ib_uverbs_flow_spec *)kern_spec)->size;
2732 kern_spec += ((struct ib_uverbs_flow_spec *) kern_spec)->size;
2733 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
2735 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
2736 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
2737 i, cmd.flow_attr.size);
2740 flow_id = ib_create_flow(qp, flow_attr, IB_FLOW_DOMAIN_USER);
2741 if (IS_ERR(flow_id)) {
2742 err = PTR_ERR(flow_id);
2746 flow_id->uobject = uobj;
2747 uobj->object = flow_id;
2749 err = idr_add_uobj(&ib_uverbs_rule_idr, uobj);
2753 memset(&resp, 0, sizeof(resp));
2754 resp.flow_handle = uobj->id;
2756 err = ib_copy_to_udata(ucore,
2757 &resp, sizeof(resp));
2762 mutex_lock(&file->mutex);
2763 list_add_tail(&uobj->list, &file->ucontext->rule_list);
2764 mutex_unlock(&file->mutex);
2768 up_write(&uobj->mutex);
2770 if (cmd.flow_attr.num_of_specs)
2771 kfree(kern_flow_attr);
2774 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
2776 ib_destroy_flow(flow_id);
2782 put_uobj_write(uobj);
2784 if (cmd.flow_attr.num_of_specs)
2785 kfree(kern_flow_attr);
2789 int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
2790 struct ib_udata *ucore,
2791 struct ib_udata *uhw)
2793 struct ib_uverbs_destroy_flow cmd;
2794 struct ib_flow *flow_id;
2795 struct ib_uobject *uobj;
2798 ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
2802 uobj = idr_write_uobj(&ib_uverbs_rule_idr, cmd.flow_handle,
2806 flow_id = uobj->object;
2808 ret = ib_destroy_flow(flow_id);
2812 put_uobj_write(uobj);
2814 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
2816 mutex_lock(&file->mutex);
2817 list_del(&uobj->list);
2818 mutex_unlock(&file->mutex);
2824 #endif /* CONFIG_INFINIBAND_EXPERIMENTAL_UVERBS_FLOW_STEERING */
2826 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2827 struct ib_uverbs_create_xsrq *cmd,
2828 struct ib_udata *udata)
2830 struct ib_uverbs_create_srq_resp resp;
2831 struct ib_usrq_object *obj;
2834 struct ib_uobject *uninitialized_var(xrcd_uobj);
2835 struct ib_srq_init_attr attr;
2838 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2842 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2843 down_write(&obj->uevent.uobject.mutex);
2845 if (cmd->srq_type == IB_SRQT_XRC) {
2846 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2847 if (!attr.ext.xrc.xrcd) {
2852 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2853 atomic_inc(&obj->uxrcd->refcnt);
2855 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2856 if (!attr.ext.xrc.cq) {
2862 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
2868 attr.event_handler = ib_uverbs_srq_event_handler;
2869 attr.srq_context = file;
2870 attr.srq_type = cmd->srq_type;
2871 attr.attr.max_wr = cmd->max_wr;
2872 attr.attr.max_sge = cmd->max_sge;
2873 attr.attr.srq_limit = cmd->srq_limit;
2875 obj->uevent.events_reported = 0;
2876 INIT_LIST_HEAD(&obj->uevent.event_list);
2878 srq = pd->device->create_srq(pd, &attr, udata);
2884 srq->device = pd->device;
2886 srq->srq_type = cmd->srq_type;
2887 srq->uobject = &obj->uevent.uobject;
2888 srq->event_handler = attr.event_handler;
2889 srq->srq_context = attr.srq_context;
2891 if (cmd->srq_type == IB_SRQT_XRC) {
2892 srq->ext.xrc.cq = attr.ext.xrc.cq;
2893 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2894 atomic_inc(&attr.ext.xrc.cq->usecnt);
2895 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2898 atomic_inc(&pd->usecnt);
2899 atomic_set(&srq->usecnt, 0);
2901 obj->uevent.uobject.object = srq;
2902 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2906 memset(&resp, 0, sizeof resp);
2907 resp.srq_handle = obj->uevent.uobject.id;
2908 resp.max_wr = attr.attr.max_wr;
2909 resp.max_sge = attr.attr.max_sge;
2910 if (cmd->srq_type == IB_SRQT_XRC)
2911 resp.srqn = srq->ext.xrc.srq_num;
2913 if (copy_to_user((void __user *) (unsigned long) cmd->response,
2914 &resp, sizeof resp)) {
2919 if (cmd->srq_type == IB_SRQT_XRC) {
2920 put_uobj_read(xrcd_uobj);
2921 put_cq_read(attr.ext.xrc.cq);
2925 mutex_lock(&file->mutex);
2926 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2927 mutex_unlock(&file->mutex);
2929 obj->uevent.uobject.live = 1;
2931 up_write(&obj->uevent.uobject.mutex);
2936 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2939 ib_destroy_srq(srq);
2945 if (cmd->srq_type == IB_SRQT_XRC)
2946 put_cq_read(attr.ext.xrc.cq);
2949 if (cmd->srq_type == IB_SRQT_XRC) {
2950 atomic_dec(&obj->uxrcd->refcnt);
2951 put_uobj_read(xrcd_uobj);
2955 put_uobj_write(&obj->uevent.uobject);
2959 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2960 const char __user *buf, int in_len,
2963 struct ib_uverbs_create_srq cmd;
2964 struct ib_uverbs_create_xsrq xcmd;
2965 struct ib_uverbs_create_srq_resp resp;
2966 struct ib_udata udata;
2969 if (out_len < sizeof resp)
2972 if (copy_from_user(&cmd, buf, sizeof cmd))
2975 xcmd.response = cmd.response;
2976 xcmd.user_handle = cmd.user_handle;
2977 xcmd.srq_type = IB_SRQT_BASIC;
2978 xcmd.pd_handle = cmd.pd_handle;
2979 xcmd.max_wr = cmd.max_wr;
2980 xcmd.max_sge = cmd.max_sge;
2981 xcmd.srq_limit = cmd.srq_limit;
2983 INIT_UDATA(&udata, buf + sizeof cmd,
2984 (unsigned long) cmd.response + sizeof resp,
2985 in_len - sizeof cmd, out_len - sizeof resp);
2987 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2994 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2995 const char __user *buf, int in_len, int out_len)
2997 struct ib_uverbs_create_xsrq cmd;
2998 struct ib_uverbs_create_srq_resp resp;
2999 struct ib_udata udata;
3002 if (out_len < sizeof resp)
3005 if (copy_from_user(&cmd, buf, sizeof cmd))
3008 INIT_UDATA(&udata, buf + sizeof cmd,
3009 (unsigned long) cmd.response + sizeof resp,
3010 in_len - sizeof cmd, out_len - sizeof resp);
3012 ret = __uverbs_create_xsrq(file, &cmd, &udata);
3019 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
3020 const char __user *buf, int in_len,
3023 struct ib_uverbs_modify_srq cmd;
3024 struct ib_udata udata;
3026 struct ib_srq_attr attr;
3029 if (copy_from_user(&cmd, buf, sizeof cmd))
3032 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
3035 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3039 attr.max_wr = cmd.max_wr;
3040 attr.srq_limit = cmd.srq_limit;
3042 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
3046 return ret ? ret : in_len;
3049 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
3050 const char __user *buf,
3051 int in_len, int out_len)
3053 struct ib_uverbs_query_srq cmd;
3054 struct ib_uverbs_query_srq_resp resp;
3055 struct ib_srq_attr attr;
3059 if (out_len < sizeof resp)
3062 if (copy_from_user(&cmd, buf, sizeof cmd))
3065 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3069 ret = ib_query_srq(srq, &attr);
3076 memset(&resp, 0, sizeof resp);
3078 resp.max_wr = attr.max_wr;
3079 resp.max_sge = attr.max_sge;
3080 resp.srq_limit = attr.srq_limit;
3082 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3083 &resp, sizeof resp))
3089 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
3090 const char __user *buf, int in_len,
3093 struct ib_uverbs_destroy_srq cmd;
3094 struct ib_uverbs_destroy_srq_resp resp;
3095 struct ib_uobject *uobj;
3097 struct ib_uevent_object *obj;
3099 struct ib_usrq_object *us;
3100 enum ib_srq_type srq_type;
3102 if (copy_from_user(&cmd, buf, sizeof cmd))
3105 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
3109 obj = container_of(uobj, struct ib_uevent_object, uobject);
3110 srq_type = srq->srq_type;
3112 ret = ib_destroy_srq(srq);
3116 put_uobj_write(uobj);
3121 if (srq_type == IB_SRQT_XRC) {
3122 us = container_of(obj, struct ib_usrq_object, uevent);
3123 atomic_dec(&us->uxrcd->refcnt);
3126 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
3128 mutex_lock(&file->mutex);
3129 list_del(&uobj->list);
3130 mutex_unlock(&file->mutex);
3132 ib_uverbs_release_uevent(file, obj);
3134 memset(&resp, 0, sizeof resp);
3135 resp.events_reported = obj->events_reported;
3139 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3140 &resp, sizeof resp))
3143 return ret ? ret : in_len;