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" };
59 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
61 (udata)->inbuf = (void __user *) (ibuf); \
62 (udata)->outbuf = (void __user *) (obuf); \
63 (udata)->inlen = (ilen); \
64 (udata)->outlen = (olen); \
68 * The ib_uobject locking scheme is as follows:
70 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
71 * needs to be held during all idr operations. When an object is
72 * looked up, a reference must be taken on the object's kref before
75 * - Each object also has an rwsem. This rwsem must be held for
76 * reading while an operation that uses the object is performed.
77 * For example, while registering an MR, the associated PD's
78 * uobject.mutex must be held for reading. The rwsem must be held
79 * for writing while initializing or destroying an object.
81 * - In addition, each object has a "live" flag. If this flag is not
82 * set, then lookups of the object will fail even if it is found in
83 * the idr. This handles a reader that blocks and does not acquire
84 * the rwsem until after the object is destroyed. The destroy
85 * operation will set the live flag to 0 and then drop the rwsem;
86 * this will allow the reader to acquire the rwsem, see that the
87 * live flag is 0, and then drop the rwsem and its reference to
88 * object. The underlying storage will not be freed until the last
89 * reference to the object is dropped.
92 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
93 struct ib_ucontext *context, struct uverbs_lock_class *c)
95 uobj->user_handle = user_handle;
96 uobj->context = context;
97 kref_init(&uobj->ref);
98 init_rwsem(&uobj->mutex);
99 lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
103 static void release_uobj(struct kref *kref)
105 kfree(container_of(kref, struct ib_uobject, ref));
108 static void put_uobj(struct ib_uobject *uobj)
110 kref_put(&uobj->ref, release_uobj);
113 static void put_uobj_read(struct ib_uobject *uobj)
115 up_read(&uobj->mutex);
119 static void put_uobj_write(struct ib_uobject *uobj)
121 up_write(&uobj->mutex);
125 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
129 idr_preload(GFP_KERNEL);
130 spin_lock(&ib_uverbs_idr_lock);
132 ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
136 spin_unlock(&ib_uverbs_idr_lock);
139 return ret < 0 ? ret : 0;
142 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
144 spin_lock(&ib_uverbs_idr_lock);
145 idr_remove(idr, uobj->id);
146 spin_unlock(&ib_uverbs_idr_lock);
149 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
150 struct ib_ucontext *context)
152 struct ib_uobject *uobj;
154 spin_lock(&ib_uverbs_idr_lock);
155 uobj = idr_find(idr, id);
157 if (uobj->context == context)
158 kref_get(&uobj->ref);
162 spin_unlock(&ib_uverbs_idr_lock);
167 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
168 struct ib_ucontext *context, int nested)
170 struct ib_uobject *uobj;
172 uobj = __idr_get_uobj(idr, id, context);
177 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
179 down_read(&uobj->mutex);
188 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
189 struct ib_ucontext *context)
191 struct ib_uobject *uobj;
193 uobj = __idr_get_uobj(idr, id, context);
197 down_write(&uobj->mutex);
199 put_uobj_write(uobj);
206 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
209 struct ib_uobject *uobj;
211 uobj = idr_read_uobj(idr, id, context, nested);
212 return uobj ? uobj->object : NULL;
215 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
217 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
220 static void put_pd_read(struct ib_pd *pd)
222 put_uobj_read(pd->uobject);
225 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
227 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
230 static void put_cq_read(struct ib_cq *cq)
232 put_uobj_read(cq->uobject);
235 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
237 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
240 static void put_ah_read(struct ib_ah *ah)
242 put_uobj_read(ah->uobject);
245 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
247 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
250 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
252 struct ib_uobject *uobj;
254 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
255 return uobj ? uobj->object : NULL;
258 static void put_qp_read(struct ib_qp *qp)
260 put_uobj_read(qp->uobject);
263 static void put_qp_write(struct ib_qp *qp)
265 put_uobj_write(qp->uobject);
268 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
270 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
273 static void put_srq_read(struct ib_srq *srq)
275 put_uobj_read(srq->uobject);
278 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
279 struct ib_uobject **uobj)
281 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
282 return *uobj ? (*uobj)->object : NULL;
285 static void put_xrcd_read(struct ib_uobject *uobj)
290 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
291 const char __user *buf,
292 int in_len, int out_len)
294 struct ib_uverbs_get_context cmd;
295 struct ib_uverbs_get_context_resp resp;
296 struct ib_udata udata;
297 struct ib_device *ibdev = file->device->ib_dev;
298 struct ib_ucontext *ucontext;
302 if (out_len < sizeof resp)
305 if (copy_from_user(&cmd, buf, sizeof cmd))
308 mutex_lock(&file->mutex);
310 if (file->ucontext) {
315 INIT_UDATA(&udata, buf + sizeof cmd,
316 (unsigned long) cmd.response + sizeof resp,
317 in_len - sizeof cmd, out_len - sizeof resp);
319 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
320 if (IS_ERR(ucontext)) {
321 ret = PTR_ERR(ucontext);
325 ucontext->device = ibdev;
326 INIT_LIST_HEAD(&ucontext->pd_list);
327 INIT_LIST_HEAD(&ucontext->mr_list);
328 INIT_LIST_HEAD(&ucontext->mw_list);
329 INIT_LIST_HEAD(&ucontext->cq_list);
330 INIT_LIST_HEAD(&ucontext->qp_list);
331 INIT_LIST_HEAD(&ucontext->srq_list);
332 INIT_LIST_HEAD(&ucontext->ah_list);
333 INIT_LIST_HEAD(&ucontext->xrcd_list);
334 INIT_LIST_HEAD(&ucontext->rule_list);
335 ucontext->closing = 0;
337 resp.num_comp_vectors = file->device->num_comp_vectors;
339 ret = get_unused_fd_flags(O_CLOEXEC);
344 filp = ib_uverbs_alloc_event_file(file, 1);
350 if (copy_to_user((void __user *) (unsigned long) cmd.response,
351 &resp, sizeof resp)) {
356 file->async_file = filp->private_data;
358 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
359 ib_uverbs_event_handler);
360 ret = ib_register_event_handler(&file->event_handler);
364 kref_get(&file->async_file->ref);
365 kref_get(&file->ref);
366 file->ucontext = ucontext;
368 fd_install(resp.async_fd, filp);
370 mutex_unlock(&file->mutex);
378 put_unused_fd(resp.async_fd);
381 ibdev->dealloc_ucontext(ucontext);
384 mutex_unlock(&file->mutex);
388 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
389 const char __user *buf,
390 int in_len, int out_len)
392 struct ib_uverbs_query_device cmd;
393 struct ib_uverbs_query_device_resp resp;
394 struct ib_device_attr attr;
397 if (out_len < sizeof resp)
400 if (copy_from_user(&cmd, buf, sizeof cmd))
403 ret = ib_query_device(file->device->ib_dev, &attr);
407 memset(&resp, 0, sizeof resp);
409 resp.fw_ver = attr.fw_ver;
410 resp.node_guid = file->device->ib_dev->node_guid;
411 resp.sys_image_guid = attr.sys_image_guid;
412 resp.max_mr_size = attr.max_mr_size;
413 resp.page_size_cap = attr.page_size_cap;
414 resp.vendor_id = attr.vendor_id;
415 resp.vendor_part_id = attr.vendor_part_id;
416 resp.hw_ver = attr.hw_ver;
417 resp.max_qp = attr.max_qp;
418 resp.max_qp_wr = attr.max_qp_wr;
419 resp.device_cap_flags = attr.device_cap_flags;
420 resp.max_sge = attr.max_sge;
421 resp.max_sge_rd = attr.max_sge_rd;
422 resp.max_cq = attr.max_cq;
423 resp.max_cqe = attr.max_cqe;
424 resp.max_mr = attr.max_mr;
425 resp.max_pd = attr.max_pd;
426 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
427 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
428 resp.max_res_rd_atom = attr.max_res_rd_atom;
429 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
430 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
431 resp.atomic_cap = attr.atomic_cap;
432 resp.max_ee = attr.max_ee;
433 resp.max_rdd = attr.max_rdd;
434 resp.max_mw = attr.max_mw;
435 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
436 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
437 resp.max_mcast_grp = attr.max_mcast_grp;
438 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
439 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
440 resp.max_ah = attr.max_ah;
441 resp.max_fmr = attr.max_fmr;
442 resp.max_map_per_fmr = attr.max_map_per_fmr;
443 resp.max_srq = attr.max_srq;
444 resp.max_srq_wr = attr.max_srq_wr;
445 resp.max_srq_sge = attr.max_srq_sge;
446 resp.max_pkeys = attr.max_pkeys;
447 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
448 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
450 if (copy_to_user((void __user *) (unsigned long) cmd.response,
457 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
458 const char __user *buf,
459 int in_len, int out_len)
461 struct ib_uverbs_query_port cmd;
462 struct ib_uverbs_query_port_resp resp;
463 struct ib_port_attr attr;
466 if (out_len < sizeof resp)
469 if (copy_from_user(&cmd, buf, sizeof cmd))
472 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
476 memset(&resp, 0, sizeof resp);
478 resp.state = attr.state;
479 resp.max_mtu = attr.max_mtu;
480 resp.active_mtu = attr.active_mtu;
481 resp.gid_tbl_len = attr.gid_tbl_len;
482 resp.port_cap_flags = attr.port_cap_flags;
483 resp.max_msg_sz = attr.max_msg_sz;
484 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
485 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
486 resp.pkey_tbl_len = attr.pkey_tbl_len;
488 resp.sm_lid = attr.sm_lid;
490 resp.max_vl_num = attr.max_vl_num;
491 resp.sm_sl = attr.sm_sl;
492 resp.subnet_timeout = attr.subnet_timeout;
493 resp.init_type_reply = attr.init_type_reply;
494 resp.active_width = attr.active_width;
495 resp.active_speed = attr.active_speed;
496 resp.phys_state = attr.phys_state;
497 resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
500 if (copy_to_user((void __user *) (unsigned long) cmd.response,
507 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
508 const char __user *buf,
509 int in_len, int out_len)
511 struct ib_uverbs_alloc_pd cmd;
512 struct ib_uverbs_alloc_pd_resp resp;
513 struct ib_udata udata;
514 struct ib_uobject *uobj;
518 if (out_len < sizeof resp)
521 if (copy_from_user(&cmd, buf, sizeof cmd))
524 INIT_UDATA(&udata, buf + sizeof cmd,
525 (unsigned long) cmd.response + sizeof resp,
526 in_len - sizeof cmd, out_len - sizeof resp);
528 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
532 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
533 down_write(&uobj->mutex);
535 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
536 file->ucontext, &udata);
542 pd->device = file->device->ib_dev;
544 atomic_set(&pd->usecnt, 0);
547 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
551 memset(&resp, 0, sizeof resp);
552 resp.pd_handle = uobj->id;
554 if (copy_to_user((void __user *) (unsigned long) cmd.response,
555 &resp, sizeof resp)) {
560 mutex_lock(&file->mutex);
561 list_add_tail(&uobj->list, &file->ucontext->pd_list);
562 mutex_unlock(&file->mutex);
566 up_write(&uobj->mutex);
571 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
577 put_uobj_write(uobj);
581 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
582 const char __user *buf,
583 int in_len, int out_len)
585 struct ib_uverbs_dealloc_pd cmd;
586 struct ib_uobject *uobj;
589 if (copy_from_user(&cmd, buf, sizeof cmd))
592 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
596 ret = ib_dealloc_pd(uobj->object);
600 put_uobj_write(uobj);
605 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
607 mutex_lock(&file->mutex);
608 list_del(&uobj->list);
609 mutex_unlock(&file->mutex);
616 struct xrcd_table_entry {
618 struct ib_xrcd *xrcd;
622 static int xrcd_table_insert(struct ib_uverbs_device *dev,
624 struct ib_xrcd *xrcd)
626 struct xrcd_table_entry *entry, *scan;
627 struct rb_node **p = &dev->xrcd_tree.rb_node;
628 struct rb_node *parent = NULL;
630 entry = kmalloc(sizeof *entry, GFP_KERNEL);
635 entry->inode = inode;
639 scan = rb_entry(parent, struct xrcd_table_entry, node);
641 if (inode < scan->inode) {
643 } else if (inode > scan->inode) {
651 rb_link_node(&entry->node, parent, p);
652 rb_insert_color(&entry->node, &dev->xrcd_tree);
657 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
660 struct xrcd_table_entry *entry;
661 struct rb_node *p = dev->xrcd_tree.rb_node;
664 entry = rb_entry(p, struct xrcd_table_entry, node);
666 if (inode < entry->inode)
668 else if (inode > entry->inode)
677 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
679 struct xrcd_table_entry *entry;
681 entry = xrcd_table_search(dev, inode);
688 static void xrcd_table_delete(struct ib_uverbs_device *dev,
691 struct xrcd_table_entry *entry;
693 entry = xrcd_table_search(dev, inode);
696 rb_erase(&entry->node, &dev->xrcd_tree);
701 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
702 const char __user *buf, int in_len,
705 struct ib_uverbs_open_xrcd cmd;
706 struct ib_uverbs_open_xrcd_resp resp;
707 struct ib_udata udata;
708 struct ib_uxrcd_object *obj;
709 struct ib_xrcd *xrcd = NULL;
710 struct fd f = {NULL, 0};
711 struct inode *inode = NULL;
715 if (out_len < sizeof resp)
718 if (copy_from_user(&cmd, buf, sizeof cmd))
721 INIT_UDATA(&udata, buf + sizeof cmd,
722 (unsigned long) cmd.response + sizeof resp,
723 in_len - sizeof cmd, out_len - sizeof resp);
725 mutex_lock(&file->device->xrcd_tree_mutex);
728 /* search for file descriptor */
732 goto err_tree_mutex_unlock;
735 inode = file_inode(f.file);
736 xrcd = find_xrcd(file->device, inode);
737 if (!xrcd && !(cmd.oflags & O_CREAT)) {
738 /* no file descriptor. Need CREATE flag */
740 goto err_tree_mutex_unlock;
743 if (xrcd && cmd.oflags & O_EXCL) {
745 goto err_tree_mutex_unlock;
749 obj = kmalloc(sizeof *obj, GFP_KERNEL);
752 goto err_tree_mutex_unlock;
755 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
757 down_write(&obj->uobject.mutex);
760 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
761 file->ucontext, &udata);
768 xrcd->device = file->device->ib_dev;
769 atomic_set(&xrcd->usecnt, 0);
770 mutex_init(&xrcd->tgt_qp_mutex);
771 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
775 atomic_set(&obj->refcnt, 0);
776 obj->uobject.object = xrcd;
777 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
781 memset(&resp, 0, sizeof resp);
782 resp.xrcd_handle = obj->uobject.id;
786 /* create new inode/xrcd table entry */
787 ret = xrcd_table_insert(file->device, inode, xrcd);
789 goto err_insert_xrcd;
791 atomic_inc(&xrcd->usecnt);
794 if (copy_to_user((void __user *) (unsigned long) cmd.response,
795 &resp, sizeof resp)) {
803 mutex_lock(&file->mutex);
804 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
805 mutex_unlock(&file->mutex);
807 obj->uobject.live = 1;
808 up_write(&obj->uobject.mutex);
810 mutex_unlock(&file->device->xrcd_tree_mutex);
816 xrcd_table_delete(file->device, inode);
817 atomic_dec(&xrcd->usecnt);
821 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
824 ib_dealloc_xrcd(xrcd);
827 put_uobj_write(&obj->uobject);
829 err_tree_mutex_unlock:
833 mutex_unlock(&file->device->xrcd_tree_mutex);
838 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
839 const char __user *buf, int in_len,
842 struct ib_uverbs_close_xrcd cmd;
843 struct ib_uobject *uobj;
844 struct ib_xrcd *xrcd = NULL;
845 struct inode *inode = NULL;
846 struct ib_uxrcd_object *obj;
850 if (copy_from_user(&cmd, buf, sizeof cmd))
853 mutex_lock(&file->device->xrcd_tree_mutex);
854 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
862 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
863 if (atomic_read(&obj->refcnt)) {
864 put_uobj_write(uobj);
869 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
870 ret = ib_dealloc_xrcd(uobj->object);
877 atomic_inc(&xrcd->usecnt);
879 put_uobj_write(uobj);
885 xrcd_table_delete(file->device, inode);
887 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
888 mutex_lock(&file->mutex);
889 list_del(&uobj->list);
890 mutex_unlock(&file->mutex);
896 mutex_unlock(&file->device->xrcd_tree_mutex);
900 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
901 struct ib_xrcd *xrcd)
906 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
909 ib_dealloc_xrcd(xrcd);
912 xrcd_table_delete(dev, inode);
915 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
916 const char __user *buf, int in_len,
919 struct ib_uverbs_reg_mr cmd;
920 struct ib_uverbs_reg_mr_resp resp;
921 struct ib_udata udata;
922 struct ib_uobject *uobj;
927 if (out_len < sizeof resp)
930 if (copy_from_user(&cmd, buf, sizeof cmd))
933 INIT_UDATA(&udata, buf + sizeof cmd,
934 (unsigned long) cmd.response + sizeof resp,
935 in_len - sizeof cmd, out_len - sizeof resp);
937 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
941 * Local write permission is required if remote write or
942 * remote atomic permission is also requested.
944 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
945 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
948 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
952 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
953 down_write(&uobj->mutex);
955 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
961 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
962 cmd.access_flags, &udata);
968 mr->device = pd->device;
971 atomic_inc(&pd->usecnt);
972 atomic_set(&mr->usecnt, 0);
975 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
979 memset(&resp, 0, sizeof resp);
980 resp.lkey = mr->lkey;
981 resp.rkey = mr->rkey;
982 resp.mr_handle = uobj->id;
984 if (copy_to_user((void __user *) (unsigned long) cmd.response,
985 &resp, sizeof resp)) {
992 mutex_lock(&file->mutex);
993 list_add_tail(&uobj->list, &file->ucontext->mr_list);
994 mutex_unlock(&file->mutex);
998 up_write(&uobj->mutex);
1003 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1012 put_uobj_write(uobj);
1016 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1017 const char __user *buf, int in_len,
1020 struct ib_uverbs_dereg_mr cmd;
1022 struct ib_uobject *uobj;
1025 if (copy_from_user(&cmd, buf, sizeof cmd))
1028 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1034 ret = ib_dereg_mr(mr);
1038 put_uobj_write(uobj);
1043 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1045 mutex_lock(&file->mutex);
1046 list_del(&uobj->list);
1047 mutex_unlock(&file->mutex);
1054 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
1055 const char __user *buf, int in_len,
1058 struct ib_uverbs_alloc_mw cmd;
1059 struct ib_uverbs_alloc_mw_resp resp;
1060 struct ib_uobject *uobj;
1065 if (out_len < sizeof(resp))
1068 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1071 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1075 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1076 down_write(&uobj->mutex);
1078 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1084 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1090 mw->device = pd->device;
1093 atomic_inc(&pd->usecnt);
1096 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1100 memset(&resp, 0, sizeof(resp));
1101 resp.rkey = mw->rkey;
1102 resp.mw_handle = uobj->id;
1104 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1105 &resp, sizeof(resp))) {
1112 mutex_lock(&file->mutex);
1113 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1114 mutex_unlock(&file->mutex);
1118 up_write(&uobj->mutex);
1123 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1132 put_uobj_write(uobj);
1136 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
1137 const char __user *buf, int in_len,
1140 struct ib_uverbs_dealloc_mw cmd;
1142 struct ib_uobject *uobj;
1145 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1148 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1154 ret = ib_dealloc_mw(mw);
1158 put_uobj_write(uobj);
1163 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1165 mutex_lock(&file->mutex);
1166 list_del(&uobj->list);
1167 mutex_unlock(&file->mutex);
1174 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1175 const char __user *buf, int in_len,
1178 struct ib_uverbs_create_comp_channel cmd;
1179 struct ib_uverbs_create_comp_channel_resp resp;
1183 if (out_len < sizeof resp)
1186 if (copy_from_user(&cmd, buf, sizeof cmd))
1189 ret = get_unused_fd_flags(O_CLOEXEC);
1194 filp = ib_uverbs_alloc_event_file(file, 0);
1196 put_unused_fd(resp.fd);
1197 return PTR_ERR(filp);
1200 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1201 &resp, sizeof resp)) {
1202 put_unused_fd(resp.fd);
1207 fd_install(resp.fd, filp);
1211 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1212 const char __user *buf, int in_len,
1215 struct ib_uverbs_create_cq cmd;
1216 struct ib_uverbs_create_cq_resp resp;
1217 struct ib_udata udata;
1218 struct ib_ucq_object *obj;
1219 struct ib_uverbs_event_file *ev_file = NULL;
1223 if (out_len < sizeof resp)
1226 if (copy_from_user(&cmd, buf, sizeof cmd))
1229 INIT_UDATA(&udata, buf + sizeof cmd,
1230 (unsigned long) cmd.response + sizeof resp,
1231 in_len - sizeof cmd, out_len - sizeof resp);
1233 if (cmd.comp_vector >= file->device->num_comp_vectors)
1236 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1240 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1241 down_write(&obj->uobject.mutex);
1243 if (cmd.comp_channel >= 0) {
1244 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1251 obj->uverbs_file = file;
1252 obj->comp_events_reported = 0;
1253 obj->async_events_reported = 0;
1254 INIT_LIST_HEAD(&obj->comp_list);
1255 INIT_LIST_HEAD(&obj->async_list);
1257 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1259 file->ucontext, &udata);
1265 cq->device = file->device->ib_dev;
1266 cq->uobject = &obj->uobject;
1267 cq->comp_handler = ib_uverbs_comp_handler;
1268 cq->event_handler = ib_uverbs_cq_event_handler;
1269 cq->cq_context = ev_file;
1270 atomic_set(&cq->usecnt, 0);
1272 obj->uobject.object = cq;
1273 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1277 memset(&resp, 0, sizeof resp);
1278 resp.cq_handle = obj->uobject.id;
1281 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1282 &resp, sizeof resp)) {
1287 mutex_lock(&file->mutex);
1288 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1289 mutex_unlock(&file->mutex);
1291 obj->uobject.live = 1;
1293 up_write(&obj->uobject.mutex);
1298 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1305 ib_uverbs_release_ucq(file, ev_file, obj);
1308 put_uobj_write(&obj->uobject);
1312 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1313 const char __user *buf, int in_len,
1316 struct ib_uverbs_resize_cq cmd;
1317 struct ib_uverbs_resize_cq_resp resp;
1318 struct ib_udata udata;
1322 if (copy_from_user(&cmd, buf, sizeof cmd))
1325 INIT_UDATA(&udata, buf + sizeof cmd,
1326 (unsigned long) cmd.response + sizeof resp,
1327 in_len - sizeof cmd, out_len - sizeof resp);
1329 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1333 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1339 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1340 &resp, sizeof resp.cqe))
1346 return ret ? ret : in_len;
1349 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1351 struct ib_uverbs_wc tmp;
1353 tmp.wr_id = wc->wr_id;
1354 tmp.status = wc->status;
1355 tmp.opcode = wc->opcode;
1356 tmp.vendor_err = wc->vendor_err;
1357 tmp.byte_len = wc->byte_len;
1358 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1359 tmp.qp_num = wc->qp->qp_num;
1360 tmp.src_qp = wc->src_qp;
1361 tmp.wc_flags = wc->wc_flags;
1362 tmp.pkey_index = wc->pkey_index;
1363 tmp.slid = wc->slid;
1365 tmp.dlid_path_bits = wc->dlid_path_bits;
1366 tmp.port_num = wc->port_num;
1369 if (copy_to_user(dest, &tmp, sizeof tmp))
1375 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1376 const char __user *buf, int in_len,
1379 struct ib_uverbs_poll_cq cmd;
1380 struct ib_uverbs_poll_cq_resp resp;
1381 u8 __user *header_ptr;
1382 u8 __user *data_ptr;
1387 if (copy_from_user(&cmd, buf, sizeof cmd))
1390 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1394 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1395 header_ptr = (void __user *)(unsigned long) cmd.response;
1396 data_ptr = header_ptr + sizeof resp;
1398 memset(&resp, 0, sizeof resp);
1399 while (resp.count < cmd.ne) {
1400 ret = ib_poll_cq(cq, 1, &wc);
1406 ret = copy_wc_to_user(data_ptr, &wc);
1410 data_ptr += sizeof(struct ib_uverbs_wc);
1414 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1426 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1427 const char __user *buf, int in_len,
1430 struct ib_uverbs_req_notify_cq cmd;
1433 if (copy_from_user(&cmd, buf, sizeof cmd))
1436 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1440 ib_req_notify_cq(cq, cmd.solicited_only ?
1441 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1448 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1449 const char __user *buf, int in_len,
1452 struct ib_uverbs_destroy_cq cmd;
1453 struct ib_uverbs_destroy_cq_resp resp;
1454 struct ib_uobject *uobj;
1456 struct ib_ucq_object *obj;
1457 struct ib_uverbs_event_file *ev_file;
1460 if (copy_from_user(&cmd, buf, sizeof cmd))
1463 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1467 ev_file = cq->cq_context;
1468 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1470 ret = ib_destroy_cq(cq);
1474 put_uobj_write(uobj);
1479 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1481 mutex_lock(&file->mutex);
1482 list_del(&uobj->list);
1483 mutex_unlock(&file->mutex);
1485 ib_uverbs_release_ucq(file, ev_file, obj);
1487 memset(&resp, 0, sizeof resp);
1488 resp.comp_events_reported = obj->comp_events_reported;
1489 resp.async_events_reported = obj->async_events_reported;
1493 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1494 &resp, sizeof resp))
1500 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1501 const char __user *buf, int in_len,
1504 struct ib_uverbs_create_qp cmd;
1505 struct ib_uverbs_create_qp_resp resp;
1506 struct ib_udata udata;
1507 struct ib_uqp_object *obj;
1508 struct ib_device *device;
1509 struct ib_pd *pd = NULL;
1510 struct ib_xrcd *xrcd = NULL;
1511 struct ib_uobject *uninitialized_var(xrcd_uobj);
1512 struct ib_cq *scq = NULL, *rcq = NULL;
1513 struct ib_srq *srq = NULL;
1515 struct ib_qp_init_attr attr;
1518 if (out_len < sizeof resp)
1521 if (copy_from_user(&cmd, buf, sizeof cmd))
1524 if (cmd.qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1527 INIT_UDATA(&udata, buf + sizeof cmd,
1528 (unsigned long) cmd.response + sizeof resp,
1529 in_len - sizeof cmd, out_len - sizeof resp);
1531 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1535 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1536 down_write(&obj->uevent.uobject.mutex);
1538 if (cmd.qp_type == IB_QPT_XRC_TGT) {
1539 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1544 device = xrcd->device;
1546 if (cmd.qp_type == IB_QPT_XRC_INI) {
1547 cmd.max_recv_wr = cmd.max_recv_sge = 0;
1550 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1551 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1557 if (cmd.recv_cq_handle != cmd.send_cq_handle) {
1558 rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext, 0);
1566 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, !!rcq);
1568 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1574 device = pd->device;
1577 attr.event_handler = ib_uverbs_qp_event_handler;
1578 attr.qp_context = file;
1583 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1584 attr.qp_type = cmd.qp_type;
1585 attr.create_flags = 0;
1587 attr.cap.max_send_wr = cmd.max_send_wr;
1588 attr.cap.max_recv_wr = cmd.max_recv_wr;
1589 attr.cap.max_send_sge = cmd.max_send_sge;
1590 attr.cap.max_recv_sge = cmd.max_recv_sge;
1591 attr.cap.max_inline_data = cmd.max_inline_data;
1593 obj->uevent.events_reported = 0;
1594 INIT_LIST_HEAD(&obj->uevent.event_list);
1595 INIT_LIST_HEAD(&obj->mcast_list);
1597 if (cmd.qp_type == IB_QPT_XRC_TGT)
1598 qp = ib_create_qp(pd, &attr);
1600 qp = device->create_qp(pd, &attr, &udata);
1607 if (cmd.qp_type != IB_QPT_XRC_TGT) {
1609 qp->device = device;
1611 qp->send_cq = attr.send_cq;
1612 qp->recv_cq = attr.recv_cq;
1614 qp->event_handler = attr.event_handler;
1615 qp->qp_context = attr.qp_context;
1616 qp->qp_type = attr.qp_type;
1617 atomic_set(&qp->usecnt, 0);
1618 atomic_inc(&pd->usecnt);
1619 atomic_inc(&attr.send_cq->usecnt);
1621 atomic_inc(&attr.recv_cq->usecnt);
1623 atomic_inc(&attr.srq->usecnt);
1625 qp->uobject = &obj->uevent.uobject;
1627 obj->uevent.uobject.object = qp;
1628 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1632 memset(&resp, 0, sizeof resp);
1633 resp.qpn = qp->qp_num;
1634 resp.qp_handle = obj->uevent.uobject.id;
1635 resp.max_recv_sge = attr.cap.max_recv_sge;
1636 resp.max_send_sge = attr.cap.max_send_sge;
1637 resp.max_recv_wr = attr.cap.max_recv_wr;
1638 resp.max_send_wr = attr.cap.max_send_wr;
1639 resp.max_inline_data = attr.cap.max_inline_data;
1641 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1642 &resp, sizeof resp)) {
1648 put_xrcd_read(xrcd_uobj);
1653 if (rcq && rcq != scq)
1658 mutex_lock(&file->mutex);
1659 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1660 mutex_unlock(&file->mutex);
1662 obj->uevent.uobject.live = 1;
1664 up_write(&obj->uevent.uobject.mutex);
1669 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1676 put_xrcd_read(xrcd_uobj);
1681 if (rcq && rcq != scq)
1686 put_uobj_write(&obj->uevent.uobject);
1690 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1691 const char __user *buf, int in_len, int out_len)
1693 struct ib_uverbs_open_qp cmd;
1694 struct ib_uverbs_create_qp_resp resp;
1695 struct ib_udata udata;
1696 struct ib_uqp_object *obj;
1697 struct ib_xrcd *xrcd;
1698 struct ib_uobject *uninitialized_var(xrcd_uobj);
1700 struct ib_qp_open_attr attr;
1703 if (out_len < sizeof resp)
1706 if (copy_from_user(&cmd, buf, sizeof cmd))
1709 INIT_UDATA(&udata, buf + sizeof cmd,
1710 (unsigned long) cmd.response + sizeof resp,
1711 in_len - sizeof cmd, out_len - sizeof resp);
1713 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1717 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1718 down_write(&obj->uevent.uobject.mutex);
1720 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1726 attr.event_handler = ib_uverbs_qp_event_handler;
1727 attr.qp_context = file;
1728 attr.qp_num = cmd.qpn;
1729 attr.qp_type = cmd.qp_type;
1731 obj->uevent.events_reported = 0;
1732 INIT_LIST_HEAD(&obj->uevent.event_list);
1733 INIT_LIST_HEAD(&obj->mcast_list);
1735 qp = ib_open_qp(xrcd, &attr);
1741 qp->uobject = &obj->uevent.uobject;
1743 obj->uevent.uobject.object = qp;
1744 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1748 memset(&resp, 0, sizeof resp);
1749 resp.qpn = qp->qp_num;
1750 resp.qp_handle = obj->uevent.uobject.id;
1752 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1753 &resp, sizeof resp)) {
1758 put_xrcd_read(xrcd_uobj);
1760 mutex_lock(&file->mutex);
1761 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1762 mutex_unlock(&file->mutex);
1764 obj->uevent.uobject.live = 1;
1766 up_write(&obj->uevent.uobject.mutex);
1771 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1777 put_xrcd_read(xrcd_uobj);
1778 put_uobj_write(&obj->uevent.uobject);
1782 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1783 const char __user *buf, int in_len,
1786 struct ib_uverbs_query_qp cmd;
1787 struct ib_uverbs_query_qp_resp resp;
1789 struct ib_qp_attr *attr;
1790 struct ib_qp_init_attr *init_attr;
1793 if (copy_from_user(&cmd, buf, sizeof cmd))
1796 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1797 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1798 if (!attr || !init_attr) {
1803 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1809 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1816 memset(&resp, 0, sizeof resp);
1818 resp.qp_state = attr->qp_state;
1819 resp.cur_qp_state = attr->cur_qp_state;
1820 resp.path_mtu = attr->path_mtu;
1821 resp.path_mig_state = attr->path_mig_state;
1822 resp.qkey = attr->qkey;
1823 resp.rq_psn = attr->rq_psn;
1824 resp.sq_psn = attr->sq_psn;
1825 resp.dest_qp_num = attr->dest_qp_num;
1826 resp.qp_access_flags = attr->qp_access_flags;
1827 resp.pkey_index = attr->pkey_index;
1828 resp.alt_pkey_index = attr->alt_pkey_index;
1829 resp.sq_draining = attr->sq_draining;
1830 resp.max_rd_atomic = attr->max_rd_atomic;
1831 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1832 resp.min_rnr_timer = attr->min_rnr_timer;
1833 resp.port_num = attr->port_num;
1834 resp.timeout = attr->timeout;
1835 resp.retry_cnt = attr->retry_cnt;
1836 resp.rnr_retry = attr->rnr_retry;
1837 resp.alt_port_num = attr->alt_port_num;
1838 resp.alt_timeout = attr->alt_timeout;
1840 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1841 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1842 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1843 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1844 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1845 resp.dest.dlid = attr->ah_attr.dlid;
1846 resp.dest.sl = attr->ah_attr.sl;
1847 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1848 resp.dest.static_rate = attr->ah_attr.static_rate;
1849 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1850 resp.dest.port_num = attr->ah_attr.port_num;
1852 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1853 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1854 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1855 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1856 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1857 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1858 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1859 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1860 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1861 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1862 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1864 resp.max_send_wr = init_attr->cap.max_send_wr;
1865 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1866 resp.max_send_sge = init_attr->cap.max_send_sge;
1867 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1868 resp.max_inline_data = init_attr->cap.max_inline_data;
1869 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1871 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1872 &resp, sizeof resp))
1879 return ret ? ret : in_len;
1882 /* Remove ignored fields set in the attribute mask */
1883 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1886 case IB_QPT_XRC_INI:
1887 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1888 case IB_QPT_XRC_TGT:
1889 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1896 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1897 const char __user *buf, int in_len,
1900 struct ib_uverbs_modify_qp cmd;
1901 struct ib_udata udata;
1903 struct ib_qp_attr *attr;
1906 if (copy_from_user(&cmd, buf, sizeof cmd))
1909 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1912 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1916 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1922 attr->qp_state = cmd.qp_state;
1923 attr->cur_qp_state = cmd.cur_qp_state;
1924 attr->path_mtu = cmd.path_mtu;
1925 attr->path_mig_state = cmd.path_mig_state;
1926 attr->qkey = cmd.qkey;
1927 attr->rq_psn = cmd.rq_psn;
1928 attr->sq_psn = cmd.sq_psn;
1929 attr->dest_qp_num = cmd.dest_qp_num;
1930 attr->qp_access_flags = cmd.qp_access_flags;
1931 attr->pkey_index = cmd.pkey_index;
1932 attr->alt_pkey_index = cmd.alt_pkey_index;
1933 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1934 attr->max_rd_atomic = cmd.max_rd_atomic;
1935 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1936 attr->min_rnr_timer = cmd.min_rnr_timer;
1937 attr->port_num = cmd.port_num;
1938 attr->timeout = cmd.timeout;
1939 attr->retry_cnt = cmd.retry_cnt;
1940 attr->rnr_retry = cmd.rnr_retry;
1941 attr->alt_port_num = cmd.alt_port_num;
1942 attr->alt_timeout = cmd.alt_timeout;
1944 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1945 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1946 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1947 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1948 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1949 attr->ah_attr.dlid = cmd.dest.dlid;
1950 attr->ah_attr.sl = cmd.dest.sl;
1951 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1952 attr->ah_attr.static_rate = cmd.dest.static_rate;
1953 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1954 attr->ah_attr.port_num = cmd.dest.port_num;
1956 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1957 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1958 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1959 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1960 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1961 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1962 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1963 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1964 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1965 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1966 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1968 if (qp->real_qp == qp) {
1969 ret = qp->device->modify_qp(qp, attr,
1970 modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
1972 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
1988 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1989 const char __user *buf, int in_len,
1992 struct ib_uverbs_destroy_qp cmd;
1993 struct ib_uverbs_destroy_qp_resp resp;
1994 struct ib_uobject *uobj;
1996 struct ib_uqp_object *obj;
1999 if (copy_from_user(&cmd, buf, sizeof cmd))
2002 memset(&resp, 0, sizeof resp);
2004 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
2008 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2010 if (!list_empty(&obj->mcast_list)) {
2011 put_uobj_write(uobj);
2015 ret = ib_destroy_qp(qp);
2019 put_uobj_write(uobj);
2024 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
2026 mutex_lock(&file->mutex);
2027 list_del(&uobj->list);
2028 mutex_unlock(&file->mutex);
2030 ib_uverbs_release_uevent(file, &obj->uevent);
2032 resp.events_reported = obj->uevent.events_reported;
2036 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2037 &resp, sizeof resp))
2043 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2044 const char __user *buf, int in_len,
2047 struct ib_uverbs_post_send cmd;
2048 struct ib_uverbs_post_send_resp resp;
2049 struct ib_uverbs_send_wr *user_wr;
2050 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2054 ssize_t ret = -EINVAL;
2056 if (copy_from_user(&cmd, buf, sizeof cmd))
2059 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2060 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2063 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2066 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2070 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2074 is_ud = qp->qp_type == IB_QPT_UD;
2077 for (i = 0; i < cmd.wr_count; ++i) {
2078 if (copy_from_user(user_wr,
2079 buf + sizeof cmd + i * cmd.wqe_size,
2085 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2090 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2091 user_wr->num_sge * sizeof (struct ib_sge),
2105 next->wr_id = user_wr->wr_id;
2106 next->num_sge = user_wr->num_sge;
2107 next->opcode = user_wr->opcode;
2108 next->send_flags = user_wr->send_flags;
2111 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
2113 if (!next->wr.ud.ah) {
2117 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
2118 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
2120 switch (next->opcode) {
2121 case IB_WR_RDMA_WRITE_WITH_IMM:
2123 (__be32 __force) user_wr->ex.imm_data;
2124 case IB_WR_RDMA_WRITE:
2125 case IB_WR_RDMA_READ:
2126 next->wr.rdma.remote_addr =
2127 user_wr->wr.rdma.remote_addr;
2128 next->wr.rdma.rkey =
2129 user_wr->wr.rdma.rkey;
2131 case IB_WR_SEND_WITH_IMM:
2133 (__be32 __force) user_wr->ex.imm_data;
2135 case IB_WR_SEND_WITH_INV:
2136 next->ex.invalidate_rkey =
2137 user_wr->ex.invalidate_rkey;
2139 case IB_WR_ATOMIC_CMP_AND_SWP:
2140 case IB_WR_ATOMIC_FETCH_AND_ADD:
2141 next->wr.atomic.remote_addr =
2142 user_wr->wr.atomic.remote_addr;
2143 next->wr.atomic.compare_add =
2144 user_wr->wr.atomic.compare_add;
2145 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2146 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2153 if (next->num_sge) {
2154 next->sg_list = (void *) next +
2155 ALIGN(sizeof *next, sizeof (struct ib_sge));
2156 if (copy_from_user(next->sg_list,
2158 cmd.wr_count * cmd.wqe_size +
2159 sg_ind * sizeof (struct ib_sge),
2160 next->num_sge * sizeof (struct ib_sge))) {
2164 sg_ind += next->num_sge;
2166 next->sg_list = NULL;
2170 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2172 for (next = wr; next; next = next->next) {
2178 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2179 &resp, sizeof resp))
2186 if (is_ud && wr->wr.ud.ah)
2187 put_ah_read(wr->wr.ud.ah);
2196 return ret ? ret : in_len;
2199 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2205 struct ib_uverbs_recv_wr *user_wr;
2206 struct ib_recv_wr *wr = NULL, *last, *next;
2211 if (in_len < wqe_size * wr_count +
2212 sge_count * sizeof (struct ib_uverbs_sge))
2213 return ERR_PTR(-EINVAL);
2215 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2216 return ERR_PTR(-EINVAL);
2218 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2220 return ERR_PTR(-ENOMEM);
2224 for (i = 0; i < wr_count; ++i) {
2225 if (copy_from_user(user_wr, buf + i * wqe_size,
2231 if (user_wr->num_sge + sg_ind > sge_count) {
2236 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2237 user_wr->num_sge * sizeof (struct ib_sge),
2251 next->wr_id = user_wr->wr_id;
2252 next->num_sge = user_wr->num_sge;
2254 if (next->num_sge) {
2255 next->sg_list = (void *) next +
2256 ALIGN(sizeof *next, sizeof (struct ib_sge));
2257 if (copy_from_user(next->sg_list,
2258 buf + wr_count * wqe_size +
2259 sg_ind * sizeof (struct ib_sge),
2260 next->num_sge * sizeof (struct ib_sge))) {
2264 sg_ind += next->num_sge;
2266 next->sg_list = NULL;
2281 return ERR_PTR(ret);
2284 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2285 const char __user *buf, int in_len,
2288 struct ib_uverbs_post_recv cmd;
2289 struct ib_uverbs_post_recv_resp resp;
2290 struct ib_recv_wr *wr, *next, *bad_wr;
2292 ssize_t ret = -EINVAL;
2294 if (copy_from_user(&cmd, buf, sizeof cmd))
2297 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2298 in_len - sizeof cmd, cmd.wr_count,
2299 cmd.sge_count, cmd.wqe_size);
2303 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2308 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2313 for (next = wr; next; next = next->next) {
2319 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2320 &resp, sizeof resp))
2330 return ret ? ret : in_len;
2333 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2334 const char __user *buf, int in_len,
2337 struct ib_uverbs_post_srq_recv cmd;
2338 struct ib_uverbs_post_srq_recv_resp resp;
2339 struct ib_recv_wr *wr, *next, *bad_wr;
2341 ssize_t ret = -EINVAL;
2343 if (copy_from_user(&cmd, buf, sizeof cmd))
2346 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2347 in_len - sizeof cmd, cmd.wr_count,
2348 cmd.sge_count, cmd.wqe_size);
2352 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2357 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2362 for (next = wr; next; next = next->next) {
2368 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2369 &resp, sizeof resp))
2379 return ret ? ret : in_len;
2382 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2383 const char __user *buf, int in_len,
2386 struct ib_uverbs_create_ah cmd;
2387 struct ib_uverbs_create_ah_resp resp;
2388 struct ib_uobject *uobj;
2391 struct ib_ah_attr attr;
2394 if (out_len < sizeof resp)
2397 if (copy_from_user(&cmd, buf, sizeof cmd))
2400 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2404 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2405 down_write(&uobj->mutex);
2407 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2413 attr.dlid = cmd.attr.dlid;
2414 attr.sl = cmd.attr.sl;
2415 attr.src_path_bits = cmd.attr.src_path_bits;
2416 attr.static_rate = cmd.attr.static_rate;
2417 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
2418 attr.port_num = cmd.attr.port_num;
2419 attr.grh.flow_label = cmd.attr.grh.flow_label;
2420 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2421 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2422 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2423 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2425 ah = ib_create_ah(pd, &attr);
2434 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2438 resp.ah_handle = uobj->id;
2440 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2441 &resp, sizeof resp)) {
2448 mutex_lock(&file->mutex);
2449 list_add_tail(&uobj->list, &file->ucontext->ah_list);
2450 mutex_unlock(&file->mutex);
2454 up_write(&uobj->mutex);
2459 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2468 put_uobj_write(uobj);
2472 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2473 const char __user *buf, int in_len, int out_len)
2475 struct ib_uverbs_destroy_ah cmd;
2477 struct ib_uobject *uobj;
2480 if (copy_from_user(&cmd, buf, sizeof cmd))
2483 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2488 ret = ib_destroy_ah(ah);
2492 put_uobj_write(uobj);
2497 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2499 mutex_lock(&file->mutex);
2500 list_del(&uobj->list);
2501 mutex_unlock(&file->mutex);
2508 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2509 const char __user *buf, int in_len,
2512 struct ib_uverbs_attach_mcast cmd;
2514 struct ib_uqp_object *obj;
2515 struct ib_uverbs_mcast_entry *mcast;
2518 if (copy_from_user(&cmd, buf, sizeof cmd))
2521 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2525 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2527 list_for_each_entry(mcast, &obj->mcast_list, list)
2528 if (cmd.mlid == mcast->lid &&
2529 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2534 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2540 mcast->lid = cmd.mlid;
2541 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2543 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2545 list_add_tail(&mcast->list, &obj->mcast_list);
2552 return ret ? ret : in_len;
2555 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2556 const char __user *buf, int in_len,
2559 struct ib_uverbs_detach_mcast cmd;
2560 struct ib_uqp_object *obj;
2562 struct ib_uverbs_mcast_entry *mcast;
2565 if (copy_from_user(&cmd, buf, sizeof cmd))
2568 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2572 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2576 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2578 list_for_each_entry(mcast, &obj->mcast_list, list)
2579 if (cmd.mlid == mcast->lid &&
2580 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2581 list_del(&mcast->list);
2589 return ret ? ret : in_len;
2592 static int kern_spec_to_ib_spec(struct ib_kern_spec *kern_spec,
2593 union ib_flow_spec *ib_spec)
2595 ib_spec->type = kern_spec->type;
2597 switch (ib_spec->type) {
2598 case IB_FLOW_SPEC_ETH:
2599 ib_spec->eth.size = sizeof(struct ib_flow_spec_eth);
2600 if (ib_spec->eth.size != kern_spec->eth.size)
2602 memcpy(&ib_spec->eth.val, &kern_spec->eth.val,
2603 sizeof(struct ib_flow_eth_filter));
2604 memcpy(&ib_spec->eth.mask, &kern_spec->eth.mask,
2605 sizeof(struct ib_flow_eth_filter));
2607 case IB_FLOW_SPEC_IPV4:
2608 ib_spec->ipv4.size = sizeof(struct ib_flow_spec_ipv4);
2609 if (ib_spec->ipv4.size != kern_spec->ipv4.size)
2611 memcpy(&ib_spec->ipv4.val, &kern_spec->ipv4.val,
2612 sizeof(struct ib_flow_ipv4_filter));
2613 memcpy(&ib_spec->ipv4.mask, &kern_spec->ipv4.mask,
2614 sizeof(struct ib_flow_ipv4_filter));
2616 case IB_FLOW_SPEC_TCP:
2617 case IB_FLOW_SPEC_UDP:
2618 ib_spec->tcp_udp.size = sizeof(struct ib_flow_spec_tcp_udp);
2619 if (ib_spec->tcp_udp.size != kern_spec->tcp_udp.size)
2621 memcpy(&ib_spec->tcp_udp.val, &kern_spec->tcp_udp.val,
2622 sizeof(struct ib_flow_tcp_udp_filter));
2623 memcpy(&ib_spec->tcp_udp.mask, &kern_spec->tcp_udp.mask,
2624 sizeof(struct ib_flow_tcp_udp_filter));
2632 ssize_t ib_uverbs_create_flow(struct ib_uverbs_file *file,
2633 const char __user *buf, int in_len,
2636 struct ib_uverbs_create_flow cmd;
2637 struct ib_uverbs_create_flow_resp resp;
2638 struct ib_uobject *uobj;
2639 struct ib_flow *flow_id;
2640 struct ib_kern_flow_attr *kern_flow_attr;
2641 struct ib_flow_attr *flow_attr;
2649 if (out_len < sizeof(resp))
2652 if (copy_from_user(&cmd, buf, sizeof(cmd)))
2655 if ((cmd.flow_attr.type == IB_FLOW_ATTR_SNIFFER &&
2656 !capable(CAP_NET_ADMIN)) || !capable(CAP_NET_RAW))
2659 if (cmd.flow_attr.num_of_specs) {
2660 kern_flow_attr = kmalloc(cmd.flow_attr.size, GFP_KERNEL);
2661 if (!kern_flow_attr)
2664 memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr));
2665 kern_attr_size = cmd.flow_attr.size - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr_ex);
2666 if (copy_from_user(kern_flow_attr + 1, buf + sizeof(cmd),
2672 kern_flow_attr = &cmd.flow_attr;
2673 kern_attr_size = sizeof(cmd.flow_attr);
2676 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
2681 init_uobj(uobj, 0, file->ucontext, &rule_lock_class);
2682 down_write(&uobj->mutex);
2684 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2690 flow_attr = kmalloc(cmd.flow_attr.size, GFP_KERNEL);
2696 flow_attr->type = kern_flow_attr->type;
2697 flow_attr->priority = kern_flow_attr->priority;
2698 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
2699 flow_attr->port = kern_flow_attr->port;
2700 flow_attr->flags = kern_flow_attr->flags;
2701 flow_attr->size = sizeof(*flow_attr);
2703 kern_spec = kern_flow_attr + 1;
2704 ib_spec = flow_attr + 1;
2705 for (i = 0; i < flow_attr->num_of_specs && kern_attr_size > 0; i++) {
2706 err = kern_spec_to_ib_spec(kern_spec, ib_spec);
2710 ((union ib_flow_spec *) ib_spec)->size;
2711 kern_attr_size -= ((struct ib_kern_spec *) kern_spec)->size;
2712 kern_spec += ((struct ib_kern_spec *) kern_spec)->size;
2713 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
2715 if (kern_attr_size) {
2716 pr_warn("create flow failed, %d bytes left from uverb cmd\n",
2720 flow_id = ib_create_flow(qp, flow_attr, IB_FLOW_DOMAIN_USER);
2721 if (IS_ERR(flow_id)) {
2722 err = PTR_ERR(flow_id);
2726 flow_id->uobject = uobj;
2727 uobj->object = flow_id;
2729 err = idr_add_uobj(&ib_uverbs_rule_idr, uobj);
2733 memset(&resp, 0, sizeof(resp));
2734 resp.flow_handle = uobj->id;
2736 if (copy_to_user((void __user *)(unsigned long) cmd.response,
2737 &resp, sizeof(resp))) {
2743 mutex_lock(&file->mutex);
2744 list_add_tail(&uobj->list, &file->ucontext->rule_list);
2745 mutex_unlock(&file->mutex);
2749 up_write(&uobj->mutex);
2751 if (cmd.flow_attr.num_of_specs)
2752 kfree(kern_flow_attr);
2755 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
2757 ib_destroy_flow(flow_id);
2763 put_uobj_write(uobj);
2765 if (cmd.flow_attr.num_of_specs)
2766 kfree(kern_flow_attr);
2770 ssize_t ib_uverbs_destroy_flow(struct ib_uverbs_file *file,
2771 const char __user *buf, int in_len,
2773 struct ib_uverbs_destroy_flow cmd;
2774 struct ib_flow *flow_id;
2775 struct ib_uobject *uobj;
2778 if (copy_from_user(&cmd, buf, sizeof(cmd)))
2781 uobj = idr_write_uobj(&ib_uverbs_rule_idr, cmd.flow_handle,
2785 flow_id = uobj->object;
2787 ret = ib_destroy_flow(flow_id);
2791 put_uobj_write(uobj);
2793 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
2795 mutex_lock(&file->mutex);
2796 list_del(&uobj->list);
2797 mutex_unlock(&file->mutex);
2801 return ret ? ret : in_len;
2804 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2805 struct ib_uverbs_create_xsrq *cmd,
2806 struct ib_udata *udata)
2808 struct ib_uverbs_create_srq_resp resp;
2809 struct ib_usrq_object *obj;
2812 struct ib_uobject *uninitialized_var(xrcd_uobj);
2813 struct ib_srq_init_attr attr;
2816 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2820 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2821 down_write(&obj->uevent.uobject.mutex);
2823 if (cmd->srq_type == IB_SRQT_XRC) {
2824 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2825 if (!attr.ext.xrc.xrcd) {
2830 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2831 atomic_inc(&obj->uxrcd->refcnt);
2833 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2834 if (!attr.ext.xrc.cq) {
2840 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
2846 attr.event_handler = ib_uverbs_srq_event_handler;
2847 attr.srq_context = file;
2848 attr.srq_type = cmd->srq_type;
2849 attr.attr.max_wr = cmd->max_wr;
2850 attr.attr.max_sge = cmd->max_sge;
2851 attr.attr.srq_limit = cmd->srq_limit;
2853 obj->uevent.events_reported = 0;
2854 INIT_LIST_HEAD(&obj->uevent.event_list);
2856 srq = pd->device->create_srq(pd, &attr, udata);
2862 srq->device = pd->device;
2864 srq->srq_type = cmd->srq_type;
2865 srq->uobject = &obj->uevent.uobject;
2866 srq->event_handler = attr.event_handler;
2867 srq->srq_context = attr.srq_context;
2869 if (cmd->srq_type == IB_SRQT_XRC) {
2870 srq->ext.xrc.cq = attr.ext.xrc.cq;
2871 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2872 atomic_inc(&attr.ext.xrc.cq->usecnt);
2873 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2876 atomic_inc(&pd->usecnt);
2877 atomic_set(&srq->usecnt, 0);
2879 obj->uevent.uobject.object = srq;
2880 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2884 memset(&resp, 0, sizeof resp);
2885 resp.srq_handle = obj->uevent.uobject.id;
2886 resp.max_wr = attr.attr.max_wr;
2887 resp.max_sge = attr.attr.max_sge;
2888 if (cmd->srq_type == IB_SRQT_XRC)
2889 resp.srqn = srq->ext.xrc.srq_num;
2891 if (copy_to_user((void __user *) (unsigned long) cmd->response,
2892 &resp, sizeof resp)) {
2897 if (cmd->srq_type == IB_SRQT_XRC) {
2898 put_uobj_read(xrcd_uobj);
2899 put_cq_read(attr.ext.xrc.cq);
2903 mutex_lock(&file->mutex);
2904 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2905 mutex_unlock(&file->mutex);
2907 obj->uevent.uobject.live = 1;
2909 up_write(&obj->uevent.uobject.mutex);
2914 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2917 ib_destroy_srq(srq);
2923 if (cmd->srq_type == IB_SRQT_XRC)
2924 put_cq_read(attr.ext.xrc.cq);
2927 if (cmd->srq_type == IB_SRQT_XRC) {
2928 atomic_dec(&obj->uxrcd->refcnt);
2929 put_uobj_read(xrcd_uobj);
2933 put_uobj_write(&obj->uevent.uobject);
2937 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2938 const char __user *buf, int in_len,
2941 struct ib_uverbs_create_srq cmd;
2942 struct ib_uverbs_create_xsrq xcmd;
2943 struct ib_uverbs_create_srq_resp resp;
2944 struct ib_udata udata;
2947 if (out_len < sizeof resp)
2950 if (copy_from_user(&cmd, buf, sizeof cmd))
2953 xcmd.response = cmd.response;
2954 xcmd.user_handle = cmd.user_handle;
2955 xcmd.srq_type = IB_SRQT_BASIC;
2956 xcmd.pd_handle = cmd.pd_handle;
2957 xcmd.max_wr = cmd.max_wr;
2958 xcmd.max_sge = cmd.max_sge;
2959 xcmd.srq_limit = cmd.srq_limit;
2961 INIT_UDATA(&udata, buf + sizeof cmd,
2962 (unsigned long) cmd.response + sizeof resp,
2963 in_len - sizeof cmd, out_len - sizeof resp);
2965 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2972 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2973 const char __user *buf, int in_len, int out_len)
2975 struct ib_uverbs_create_xsrq cmd;
2976 struct ib_uverbs_create_srq_resp resp;
2977 struct ib_udata udata;
2980 if (out_len < sizeof resp)
2983 if (copy_from_user(&cmd, buf, sizeof cmd))
2986 INIT_UDATA(&udata, buf + sizeof cmd,
2987 (unsigned long) cmd.response + sizeof resp,
2988 in_len - sizeof cmd, out_len - sizeof resp);
2990 ret = __uverbs_create_xsrq(file, &cmd, &udata);
2997 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2998 const char __user *buf, int in_len,
3001 struct ib_uverbs_modify_srq cmd;
3002 struct ib_udata udata;
3004 struct ib_srq_attr attr;
3007 if (copy_from_user(&cmd, buf, sizeof cmd))
3010 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
3013 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3017 attr.max_wr = cmd.max_wr;
3018 attr.srq_limit = cmd.srq_limit;
3020 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
3024 return ret ? ret : in_len;
3027 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
3028 const char __user *buf,
3029 int in_len, int out_len)
3031 struct ib_uverbs_query_srq cmd;
3032 struct ib_uverbs_query_srq_resp resp;
3033 struct ib_srq_attr attr;
3037 if (out_len < sizeof resp)
3040 if (copy_from_user(&cmd, buf, sizeof cmd))
3043 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3047 ret = ib_query_srq(srq, &attr);
3054 memset(&resp, 0, sizeof resp);
3056 resp.max_wr = attr.max_wr;
3057 resp.max_sge = attr.max_sge;
3058 resp.srq_limit = attr.srq_limit;
3060 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3061 &resp, sizeof resp))
3067 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
3068 const char __user *buf, int in_len,
3071 struct ib_uverbs_destroy_srq cmd;
3072 struct ib_uverbs_destroy_srq_resp resp;
3073 struct ib_uobject *uobj;
3075 struct ib_uevent_object *obj;
3078 if (copy_from_user(&cmd, buf, sizeof cmd))
3081 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
3085 obj = container_of(uobj, struct ib_uevent_object, uobject);
3087 ret = ib_destroy_srq(srq);
3091 put_uobj_write(uobj);
3096 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
3098 mutex_lock(&file->mutex);
3099 list_del(&uobj->list);
3100 mutex_unlock(&file->mutex);
3102 ib_uverbs_release_uevent(file, obj);
3104 memset(&resp, 0, sizeof resp);
3105 resp.events_reported = obj->events_reported;
3109 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3110 &resp, sizeof resp))
3113 return ret ? ret : in_len;