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>
39 #include <linux/sched.h>
41 #include <asm/uaccess.h>
44 #include "core_priv.h"
46 struct uverbs_lock_class
{
47 struct lock_class_key key
;
51 static struct uverbs_lock_class pd_lock_class
= { .name
= "PD-uobj" };
52 static struct uverbs_lock_class mr_lock_class
= { .name
= "MR-uobj" };
53 static struct uverbs_lock_class mw_lock_class
= { .name
= "MW-uobj" };
54 static struct uverbs_lock_class cq_lock_class
= { .name
= "CQ-uobj" };
55 static struct uverbs_lock_class qp_lock_class
= { .name
= "QP-uobj" };
56 static struct uverbs_lock_class ah_lock_class
= { .name
= "AH-uobj" };
57 static struct uverbs_lock_class srq_lock_class
= { .name
= "SRQ-uobj" };
58 static struct uverbs_lock_class xrcd_lock_class
= { .name
= "XRCD-uobj" };
59 static struct uverbs_lock_class rule_lock_class
= { .name
= "RULE-uobj" };
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 write operations. When an object is
66 * looked up, a reference must be taken on the object's kref before
67 * dropping this lock. For read operations, the rcu_read_lock()
68 * and rcu_write_lock() but similarly the kref reference is grabbed
69 * before the rcu_read_unlock().
71 * - Each object also has an rwsem. This rwsem must be held for
72 * reading while an operation that uses the object is performed.
73 * For example, while registering an MR, the associated PD's
74 * uobject.mutex must be held for reading. The rwsem must be held
75 * for writing while initializing or destroying an object.
77 * - In addition, each object has a "live" flag. If this flag is not
78 * set, then lookups of the object will fail even if it is found in
79 * the idr. This handles a reader that blocks and does not acquire
80 * the rwsem until after the object is destroyed. The destroy
81 * operation will set the live flag to 0 and then drop the rwsem;
82 * this will allow the reader to acquire the rwsem, see that the
83 * live flag is 0, and then drop the rwsem and its reference to
84 * object. The underlying storage will not be freed until the last
85 * reference to the object is dropped.
88 static void init_uobj(struct ib_uobject
*uobj
, u64 user_handle
,
89 struct ib_ucontext
*context
, struct uverbs_lock_class
*c
)
91 uobj
->user_handle
= user_handle
;
92 uobj
->context
= context
;
93 kref_init(&uobj
->ref
);
94 init_rwsem(&uobj
->mutex
);
95 lockdep_set_class_and_name(&uobj
->mutex
, &c
->key
, c
->name
);
99 static void release_uobj(struct kref
*kref
)
101 kfree_rcu(container_of(kref
, struct ib_uobject
, ref
), rcu
);
104 static void put_uobj(struct ib_uobject
*uobj
)
106 kref_put(&uobj
->ref
, release_uobj
);
109 static void put_uobj_read(struct ib_uobject
*uobj
)
111 up_read(&uobj
->mutex
);
115 static void put_uobj_write(struct ib_uobject
*uobj
)
117 up_write(&uobj
->mutex
);
121 static int idr_add_uobj(struct idr
*idr
, struct ib_uobject
*uobj
)
125 idr_preload(GFP_KERNEL
);
126 spin_lock(&ib_uverbs_idr_lock
);
128 ret
= idr_alloc(idr
, uobj
, 0, 0, GFP_NOWAIT
);
132 spin_unlock(&ib_uverbs_idr_lock
);
135 return ret
< 0 ? ret
: 0;
138 void idr_remove_uobj(struct idr
*idr
, struct ib_uobject
*uobj
)
140 spin_lock(&ib_uverbs_idr_lock
);
141 idr_remove(idr
, uobj
->id
);
142 spin_unlock(&ib_uverbs_idr_lock
);
145 static struct ib_uobject
*__idr_get_uobj(struct idr
*idr
, int id
,
146 struct ib_ucontext
*context
)
148 struct ib_uobject
*uobj
;
151 uobj
= idr_find(idr
, id
);
153 if (uobj
->context
== context
)
154 kref_get(&uobj
->ref
);
163 static struct ib_uobject
*idr_read_uobj(struct idr
*idr
, int id
,
164 struct ib_ucontext
*context
, int nested
)
166 struct ib_uobject
*uobj
;
168 uobj
= __idr_get_uobj(idr
, id
, context
);
173 down_read_nested(&uobj
->mutex
, SINGLE_DEPTH_NESTING
);
175 down_read(&uobj
->mutex
);
184 static struct ib_uobject
*idr_write_uobj(struct idr
*idr
, int id
,
185 struct ib_ucontext
*context
)
187 struct ib_uobject
*uobj
;
189 uobj
= __idr_get_uobj(idr
, id
, context
);
193 down_write(&uobj
->mutex
);
195 put_uobj_write(uobj
);
202 static void *idr_read_obj(struct idr
*idr
, int id
, struct ib_ucontext
*context
,
205 struct ib_uobject
*uobj
;
207 uobj
= idr_read_uobj(idr
, id
, context
, nested
);
208 return uobj
? uobj
->object
: NULL
;
211 static struct ib_pd
*idr_read_pd(int pd_handle
, struct ib_ucontext
*context
)
213 return idr_read_obj(&ib_uverbs_pd_idr
, pd_handle
, context
, 0);
216 static void put_pd_read(struct ib_pd
*pd
)
218 put_uobj_read(pd
->uobject
);
221 static struct ib_cq
*idr_read_cq(int cq_handle
, struct ib_ucontext
*context
, int nested
)
223 return idr_read_obj(&ib_uverbs_cq_idr
, cq_handle
, context
, nested
);
226 static void put_cq_read(struct ib_cq
*cq
)
228 put_uobj_read(cq
->uobject
);
231 static struct ib_ah
*idr_read_ah(int ah_handle
, struct ib_ucontext
*context
)
233 return idr_read_obj(&ib_uverbs_ah_idr
, ah_handle
, context
, 0);
236 static void put_ah_read(struct ib_ah
*ah
)
238 put_uobj_read(ah
->uobject
);
241 static struct ib_qp
*idr_read_qp(int qp_handle
, struct ib_ucontext
*context
)
243 return idr_read_obj(&ib_uverbs_qp_idr
, qp_handle
, context
, 0);
246 static struct ib_qp
*idr_write_qp(int qp_handle
, struct ib_ucontext
*context
)
248 struct ib_uobject
*uobj
;
250 uobj
= idr_write_uobj(&ib_uverbs_qp_idr
, qp_handle
, context
);
251 return uobj
? uobj
->object
: NULL
;
254 static void put_qp_read(struct ib_qp
*qp
)
256 put_uobj_read(qp
->uobject
);
259 static void put_qp_write(struct ib_qp
*qp
)
261 put_uobj_write(qp
->uobject
);
264 static struct ib_srq
*idr_read_srq(int srq_handle
, struct ib_ucontext
*context
)
266 return idr_read_obj(&ib_uverbs_srq_idr
, srq_handle
, context
, 0);
269 static void put_srq_read(struct ib_srq
*srq
)
271 put_uobj_read(srq
->uobject
);
274 static struct ib_xrcd
*idr_read_xrcd(int xrcd_handle
, struct ib_ucontext
*context
,
275 struct ib_uobject
**uobj
)
277 *uobj
= idr_read_uobj(&ib_uverbs_xrcd_idr
, xrcd_handle
, context
, 0);
278 return *uobj
? (*uobj
)->object
: NULL
;
281 static void put_xrcd_read(struct ib_uobject
*uobj
)
286 ssize_t
ib_uverbs_get_context(struct ib_uverbs_file
*file
,
287 struct ib_device
*ib_dev
,
288 const char __user
*buf
,
289 int in_len
, int out_len
)
291 struct ib_uverbs_get_context cmd
;
292 struct ib_uverbs_get_context_resp resp
;
293 struct ib_udata udata
;
294 struct ib_ucontext
*ucontext
;
298 if (out_len
< sizeof resp
)
301 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
304 mutex_lock(&file
->mutex
);
306 if (file
->ucontext
) {
311 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
312 (unsigned long) cmd
.response
+ sizeof resp
,
313 in_len
- sizeof cmd
, out_len
- sizeof resp
);
315 ucontext
= ib_dev
->alloc_ucontext(ib_dev
, &udata
);
316 if (IS_ERR(ucontext
)) {
317 ret
= PTR_ERR(ucontext
);
321 ucontext
->device
= ib_dev
;
322 INIT_LIST_HEAD(&ucontext
->pd_list
);
323 INIT_LIST_HEAD(&ucontext
->mr_list
);
324 INIT_LIST_HEAD(&ucontext
->mw_list
);
325 INIT_LIST_HEAD(&ucontext
->cq_list
);
326 INIT_LIST_HEAD(&ucontext
->qp_list
);
327 INIT_LIST_HEAD(&ucontext
->srq_list
);
328 INIT_LIST_HEAD(&ucontext
->ah_list
);
329 INIT_LIST_HEAD(&ucontext
->xrcd_list
);
330 INIT_LIST_HEAD(&ucontext
->rule_list
);
332 ucontext
->tgid
= get_task_pid(current
->group_leader
, PIDTYPE_PID
);
334 ucontext
->closing
= 0;
336 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
337 ucontext
->umem_tree
= RB_ROOT
;
338 init_rwsem(&ucontext
->umem_rwsem
);
339 ucontext
->odp_mrs_count
= 0;
340 INIT_LIST_HEAD(&ucontext
->no_private_counters
);
342 if (!(ib_dev
->attrs
.device_cap_flags
& IB_DEVICE_ON_DEMAND_PAGING
))
343 ucontext
->invalidate_range
= NULL
;
347 resp
.num_comp_vectors
= file
->device
->num_comp_vectors
;
349 ret
= get_unused_fd_flags(O_CLOEXEC
);
354 filp
= ib_uverbs_alloc_event_file(file
, ib_dev
, 1);
360 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
361 &resp
, sizeof resp
)) {
366 file
->ucontext
= ucontext
;
368 fd_install(resp
.async_fd
, filp
);
370 mutex_unlock(&file
->mutex
);
375 ib_uverbs_free_async_event_file(file
);
379 put_unused_fd(resp
.async_fd
);
382 put_pid(ucontext
->tgid
);
383 ib_dev
->dealloc_ucontext(ucontext
);
386 mutex_unlock(&file
->mutex
);
390 static void copy_query_dev_fields(struct ib_uverbs_file
*file
,
391 struct ib_device
*ib_dev
,
392 struct ib_uverbs_query_device_resp
*resp
,
393 struct ib_device_attr
*attr
)
395 resp
->fw_ver
= attr
->fw_ver
;
396 resp
->node_guid
= ib_dev
->node_guid
;
397 resp
->sys_image_guid
= attr
->sys_image_guid
;
398 resp
->max_mr_size
= attr
->max_mr_size
;
399 resp
->page_size_cap
= attr
->page_size_cap
;
400 resp
->vendor_id
= attr
->vendor_id
;
401 resp
->vendor_part_id
= attr
->vendor_part_id
;
402 resp
->hw_ver
= attr
->hw_ver
;
403 resp
->max_qp
= attr
->max_qp
;
404 resp
->max_qp_wr
= attr
->max_qp_wr
;
405 resp
->device_cap_flags
= attr
->device_cap_flags
;
406 resp
->max_sge
= attr
->max_sge
;
407 resp
->max_sge_rd
= attr
->max_sge_rd
;
408 resp
->max_cq
= attr
->max_cq
;
409 resp
->max_cqe
= attr
->max_cqe
;
410 resp
->max_mr
= attr
->max_mr
;
411 resp
->max_pd
= attr
->max_pd
;
412 resp
->max_qp_rd_atom
= attr
->max_qp_rd_atom
;
413 resp
->max_ee_rd_atom
= attr
->max_ee_rd_atom
;
414 resp
->max_res_rd_atom
= attr
->max_res_rd_atom
;
415 resp
->max_qp_init_rd_atom
= attr
->max_qp_init_rd_atom
;
416 resp
->max_ee_init_rd_atom
= attr
->max_ee_init_rd_atom
;
417 resp
->atomic_cap
= attr
->atomic_cap
;
418 resp
->max_ee
= attr
->max_ee
;
419 resp
->max_rdd
= attr
->max_rdd
;
420 resp
->max_mw
= attr
->max_mw
;
421 resp
->max_raw_ipv6_qp
= attr
->max_raw_ipv6_qp
;
422 resp
->max_raw_ethy_qp
= attr
->max_raw_ethy_qp
;
423 resp
->max_mcast_grp
= attr
->max_mcast_grp
;
424 resp
->max_mcast_qp_attach
= attr
->max_mcast_qp_attach
;
425 resp
->max_total_mcast_qp_attach
= attr
->max_total_mcast_qp_attach
;
426 resp
->max_ah
= attr
->max_ah
;
427 resp
->max_fmr
= attr
->max_fmr
;
428 resp
->max_map_per_fmr
= attr
->max_map_per_fmr
;
429 resp
->max_srq
= attr
->max_srq
;
430 resp
->max_srq_wr
= attr
->max_srq_wr
;
431 resp
->max_srq_sge
= attr
->max_srq_sge
;
432 resp
->max_pkeys
= attr
->max_pkeys
;
433 resp
->local_ca_ack_delay
= attr
->local_ca_ack_delay
;
434 resp
->phys_port_cnt
= ib_dev
->phys_port_cnt
;
437 ssize_t
ib_uverbs_query_device(struct ib_uverbs_file
*file
,
438 struct ib_device
*ib_dev
,
439 const char __user
*buf
,
440 int in_len
, int out_len
)
442 struct ib_uverbs_query_device cmd
;
443 struct ib_uverbs_query_device_resp resp
;
445 if (out_len
< sizeof resp
)
448 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
451 memset(&resp
, 0, sizeof resp
);
452 copy_query_dev_fields(file
, ib_dev
, &resp
, &ib_dev
->attrs
);
454 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
461 ssize_t
ib_uverbs_query_port(struct ib_uverbs_file
*file
,
462 struct ib_device
*ib_dev
,
463 const char __user
*buf
,
464 int in_len
, int out_len
)
466 struct ib_uverbs_query_port cmd
;
467 struct ib_uverbs_query_port_resp resp
;
468 struct ib_port_attr attr
;
471 if (out_len
< sizeof resp
)
474 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
477 ret
= ib_query_port(ib_dev
, cmd
.port_num
, &attr
);
481 memset(&resp
, 0, sizeof resp
);
483 resp
.state
= attr
.state
;
484 resp
.max_mtu
= attr
.max_mtu
;
485 resp
.active_mtu
= attr
.active_mtu
;
486 resp
.gid_tbl_len
= attr
.gid_tbl_len
;
487 resp
.port_cap_flags
= attr
.port_cap_flags
;
488 resp
.max_msg_sz
= attr
.max_msg_sz
;
489 resp
.bad_pkey_cntr
= attr
.bad_pkey_cntr
;
490 resp
.qkey_viol_cntr
= attr
.qkey_viol_cntr
;
491 resp
.pkey_tbl_len
= attr
.pkey_tbl_len
;
493 resp
.sm_lid
= attr
.sm_lid
;
495 resp
.max_vl_num
= attr
.max_vl_num
;
496 resp
.sm_sl
= attr
.sm_sl
;
497 resp
.subnet_timeout
= attr
.subnet_timeout
;
498 resp
.init_type_reply
= attr
.init_type_reply
;
499 resp
.active_width
= attr
.active_width
;
500 resp
.active_speed
= attr
.active_speed
;
501 resp
.phys_state
= attr
.phys_state
;
502 resp
.link_layer
= rdma_port_get_link_layer(ib_dev
,
505 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
512 ssize_t
ib_uverbs_alloc_pd(struct ib_uverbs_file
*file
,
513 struct ib_device
*ib_dev
,
514 const char __user
*buf
,
515 int in_len
, int out_len
)
517 struct ib_uverbs_alloc_pd cmd
;
518 struct ib_uverbs_alloc_pd_resp resp
;
519 struct ib_udata udata
;
520 struct ib_uobject
*uobj
;
524 if (out_len
< sizeof resp
)
527 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
530 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
531 (unsigned long) cmd
.response
+ sizeof resp
,
532 in_len
- sizeof cmd
, out_len
- sizeof resp
);
534 uobj
= kmalloc(sizeof *uobj
, GFP_KERNEL
);
538 init_uobj(uobj
, 0, file
->ucontext
, &pd_lock_class
);
539 down_write(&uobj
->mutex
);
541 pd
= ib_dev
->alloc_pd(ib_dev
, file
->ucontext
, &udata
);
550 atomic_set(&pd
->usecnt
, 0);
553 ret
= idr_add_uobj(&ib_uverbs_pd_idr
, uobj
);
557 memset(&resp
, 0, sizeof resp
);
558 resp
.pd_handle
= uobj
->id
;
560 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
561 &resp
, sizeof resp
)) {
566 mutex_lock(&file
->mutex
);
567 list_add_tail(&uobj
->list
, &file
->ucontext
->pd_list
);
568 mutex_unlock(&file
->mutex
);
572 up_write(&uobj
->mutex
);
577 idr_remove_uobj(&ib_uverbs_pd_idr
, uobj
);
583 put_uobj_write(uobj
);
587 ssize_t
ib_uverbs_dealloc_pd(struct ib_uverbs_file
*file
,
588 struct ib_device
*ib_dev
,
589 const char __user
*buf
,
590 int in_len
, int out_len
)
592 struct ib_uverbs_dealloc_pd cmd
;
593 struct ib_uobject
*uobj
;
597 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
600 uobj
= idr_write_uobj(&ib_uverbs_pd_idr
, cmd
.pd_handle
, file
->ucontext
);
605 if (atomic_read(&pd
->usecnt
)) {
610 ret
= pd
->device
->dealloc_pd(uobj
->object
);
611 WARN_ONCE(ret
, "Infiniband HW driver failed dealloc_pd");
616 put_uobj_write(uobj
);
618 idr_remove_uobj(&ib_uverbs_pd_idr
, uobj
);
620 mutex_lock(&file
->mutex
);
621 list_del(&uobj
->list
);
622 mutex_unlock(&file
->mutex
);
629 put_uobj_write(uobj
);
633 struct xrcd_table_entry
{
635 struct ib_xrcd
*xrcd
;
639 static int xrcd_table_insert(struct ib_uverbs_device
*dev
,
641 struct ib_xrcd
*xrcd
)
643 struct xrcd_table_entry
*entry
, *scan
;
644 struct rb_node
**p
= &dev
->xrcd_tree
.rb_node
;
645 struct rb_node
*parent
= NULL
;
647 entry
= kmalloc(sizeof *entry
, GFP_KERNEL
);
652 entry
->inode
= inode
;
656 scan
= rb_entry(parent
, struct xrcd_table_entry
, node
);
658 if (inode
< scan
->inode
) {
660 } else if (inode
> scan
->inode
) {
668 rb_link_node(&entry
->node
, parent
, p
);
669 rb_insert_color(&entry
->node
, &dev
->xrcd_tree
);
674 static struct xrcd_table_entry
*xrcd_table_search(struct ib_uverbs_device
*dev
,
677 struct xrcd_table_entry
*entry
;
678 struct rb_node
*p
= dev
->xrcd_tree
.rb_node
;
681 entry
= rb_entry(p
, struct xrcd_table_entry
, node
);
683 if (inode
< entry
->inode
)
685 else if (inode
> entry
->inode
)
694 static struct ib_xrcd
*find_xrcd(struct ib_uverbs_device
*dev
, struct inode
*inode
)
696 struct xrcd_table_entry
*entry
;
698 entry
= xrcd_table_search(dev
, inode
);
705 static void xrcd_table_delete(struct ib_uverbs_device
*dev
,
708 struct xrcd_table_entry
*entry
;
710 entry
= xrcd_table_search(dev
, inode
);
713 rb_erase(&entry
->node
, &dev
->xrcd_tree
);
718 ssize_t
ib_uverbs_open_xrcd(struct ib_uverbs_file
*file
,
719 struct ib_device
*ib_dev
,
720 const char __user
*buf
, int in_len
,
723 struct ib_uverbs_open_xrcd cmd
;
724 struct ib_uverbs_open_xrcd_resp resp
;
725 struct ib_udata udata
;
726 struct ib_uxrcd_object
*obj
;
727 struct ib_xrcd
*xrcd
= NULL
;
728 struct fd f
= {NULL
, 0};
729 struct inode
*inode
= NULL
;
733 if (out_len
< sizeof resp
)
736 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
739 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
740 (unsigned long) cmd
.response
+ sizeof resp
,
741 in_len
- sizeof cmd
, out_len
- sizeof resp
);
743 mutex_lock(&file
->device
->xrcd_tree_mutex
);
746 /* search for file descriptor */
750 goto err_tree_mutex_unlock
;
753 inode
= file_inode(f
.file
);
754 xrcd
= find_xrcd(file
->device
, inode
);
755 if (!xrcd
&& !(cmd
.oflags
& O_CREAT
)) {
756 /* no file descriptor. Need CREATE flag */
758 goto err_tree_mutex_unlock
;
761 if (xrcd
&& cmd
.oflags
& O_EXCL
) {
763 goto err_tree_mutex_unlock
;
767 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
770 goto err_tree_mutex_unlock
;
773 init_uobj(&obj
->uobject
, 0, file
->ucontext
, &xrcd_lock_class
);
775 down_write(&obj
->uobject
.mutex
);
778 xrcd
= ib_dev
->alloc_xrcd(ib_dev
, file
->ucontext
, &udata
);
785 xrcd
->device
= ib_dev
;
786 atomic_set(&xrcd
->usecnt
, 0);
787 mutex_init(&xrcd
->tgt_qp_mutex
);
788 INIT_LIST_HEAD(&xrcd
->tgt_qp_list
);
792 atomic_set(&obj
->refcnt
, 0);
793 obj
->uobject
.object
= xrcd
;
794 ret
= idr_add_uobj(&ib_uverbs_xrcd_idr
, &obj
->uobject
);
798 memset(&resp
, 0, sizeof resp
);
799 resp
.xrcd_handle
= obj
->uobject
.id
;
803 /* create new inode/xrcd table entry */
804 ret
= xrcd_table_insert(file
->device
, inode
, xrcd
);
806 goto err_insert_xrcd
;
808 atomic_inc(&xrcd
->usecnt
);
811 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
812 &resp
, sizeof resp
)) {
820 mutex_lock(&file
->mutex
);
821 list_add_tail(&obj
->uobject
.list
, &file
->ucontext
->xrcd_list
);
822 mutex_unlock(&file
->mutex
);
824 obj
->uobject
.live
= 1;
825 up_write(&obj
->uobject
.mutex
);
827 mutex_unlock(&file
->device
->xrcd_tree_mutex
);
833 xrcd_table_delete(file
->device
, inode
);
834 atomic_dec(&xrcd
->usecnt
);
838 idr_remove_uobj(&ib_uverbs_xrcd_idr
, &obj
->uobject
);
841 ib_dealloc_xrcd(xrcd
);
844 put_uobj_write(&obj
->uobject
);
846 err_tree_mutex_unlock
:
850 mutex_unlock(&file
->device
->xrcd_tree_mutex
);
855 ssize_t
ib_uverbs_close_xrcd(struct ib_uverbs_file
*file
,
856 struct ib_device
*ib_dev
,
857 const char __user
*buf
, int in_len
,
860 struct ib_uverbs_close_xrcd cmd
;
861 struct ib_uobject
*uobj
;
862 struct ib_xrcd
*xrcd
= NULL
;
863 struct inode
*inode
= NULL
;
864 struct ib_uxrcd_object
*obj
;
868 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
871 mutex_lock(&file
->device
->xrcd_tree_mutex
);
872 uobj
= idr_write_uobj(&ib_uverbs_xrcd_idr
, cmd
.xrcd_handle
, file
->ucontext
);
880 obj
= container_of(uobj
, struct ib_uxrcd_object
, uobject
);
881 if (atomic_read(&obj
->refcnt
)) {
882 put_uobj_write(uobj
);
887 if (!inode
|| atomic_dec_and_test(&xrcd
->usecnt
)) {
888 ret
= ib_dealloc_xrcd(uobj
->object
);
895 atomic_inc(&xrcd
->usecnt
);
897 put_uobj_write(uobj
);
903 xrcd_table_delete(file
->device
, inode
);
905 idr_remove_uobj(&ib_uverbs_xrcd_idr
, uobj
);
906 mutex_lock(&file
->mutex
);
907 list_del(&uobj
->list
);
908 mutex_unlock(&file
->mutex
);
914 mutex_unlock(&file
->device
->xrcd_tree_mutex
);
918 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device
*dev
,
919 struct ib_xrcd
*xrcd
)
924 if (inode
&& !atomic_dec_and_test(&xrcd
->usecnt
))
927 ib_dealloc_xrcd(xrcd
);
930 xrcd_table_delete(dev
, inode
);
933 ssize_t
ib_uverbs_reg_mr(struct ib_uverbs_file
*file
,
934 struct ib_device
*ib_dev
,
935 const char __user
*buf
, int in_len
,
938 struct ib_uverbs_reg_mr cmd
;
939 struct ib_uverbs_reg_mr_resp resp
;
940 struct ib_udata udata
;
941 struct ib_uobject
*uobj
;
946 if (out_len
< sizeof resp
)
949 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
952 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
953 (unsigned long) cmd
.response
+ sizeof resp
,
954 in_len
- sizeof cmd
, out_len
- sizeof resp
);
956 if ((cmd
.start
& ~PAGE_MASK
) != (cmd
.hca_va
& ~PAGE_MASK
))
959 ret
= ib_check_mr_access(cmd
.access_flags
);
963 uobj
= kmalloc(sizeof *uobj
, GFP_KERNEL
);
967 init_uobj(uobj
, 0, file
->ucontext
, &mr_lock_class
);
968 down_write(&uobj
->mutex
);
970 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
976 if (cmd
.access_flags
& IB_ACCESS_ON_DEMAND
) {
977 if (!(pd
->device
->attrs
.device_cap_flags
&
978 IB_DEVICE_ON_DEMAND_PAGING
)) {
979 pr_debug("ODP support not available\n");
985 mr
= pd
->device
->reg_user_mr(pd
, cmd
.start
, cmd
.length
, cmd
.hca_va
,
986 cmd
.access_flags
, &udata
);
992 mr
->device
= pd
->device
;
995 atomic_inc(&pd
->usecnt
);
998 ret
= idr_add_uobj(&ib_uverbs_mr_idr
, uobj
);
1002 memset(&resp
, 0, sizeof resp
);
1003 resp
.lkey
= mr
->lkey
;
1004 resp
.rkey
= mr
->rkey
;
1005 resp
.mr_handle
= uobj
->id
;
1007 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1008 &resp
, sizeof resp
)) {
1015 mutex_lock(&file
->mutex
);
1016 list_add_tail(&uobj
->list
, &file
->ucontext
->mr_list
);
1017 mutex_unlock(&file
->mutex
);
1021 up_write(&uobj
->mutex
);
1026 idr_remove_uobj(&ib_uverbs_mr_idr
, uobj
);
1035 put_uobj_write(uobj
);
1039 ssize_t
ib_uverbs_rereg_mr(struct ib_uverbs_file
*file
,
1040 struct ib_device
*ib_dev
,
1041 const char __user
*buf
, int in_len
,
1044 struct ib_uverbs_rereg_mr cmd
;
1045 struct ib_uverbs_rereg_mr_resp resp
;
1046 struct ib_udata udata
;
1047 struct ib_pd
*pd
= NULL
;
1049 struct ib_pd
*old_pd
;
1051 struct ib_uobject
*uobj
;
1053 if (out_len
< sizeof(resp
))
1056 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1059 INIT_UDATA(&udata
, buf
+ sizeof(cmd
),
1060 (unsigned long) cmd
.response
+ sizeof(resp
),
1061 in_len
- sizeof(cmd
), out_len
- sizeof(resp
));
1063 if (cmd
.flags
& ~IB_MR_REREG_SUPPORTED
|| !cmd
.flags
)
1066 if ((cmd
.flags
& IB_MR_REREG_TRANS
) &&
1067 (!cmd
.start
|| !cmd
.hca_va
|| 0 >= cmd
.length
||
1068 (cmd
.start
& ~PAGE_MASK
) != (cmd
.hca_va
& ~PAGE_MASK
)))
1071 uobj
= idr_write_uobj(&ib_uverbs_mr_idr
, cmd
.mr_handle
,
1079 if (cmd
.flags
& IB_MR_REREG_ACCESS
) {
1080 ret
= ib_check_mr_access(cmd
.access_flags
);
1085 if (cmd
.flags
& IB_MR_REREG_PD
) {
1086 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
1094 ret
= mr
->device
->rereg_user_mr(mr
, cmd
.flags
, cmd
.start
,
1095 cmd
.length
, cmd
.hca_va
,
1096 cmd
.access_flags
, pd
, &udata
);
1098 if (cmd
.flags
& IB_MR_REREG_PD
) {
1099 atomic_inc(&pd
->usecnt
);
1101 atomic_dec(&old_pd
->usecnt
);
1107 memset(&resp
, 0, sizeof(resp
));
1108 resp
.lkey
= mr
->lkey
;
1109 resp
.rkey
= mr
->rkey
;
1111 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
1112 &resp
, sizeof(resp
)))
1118 if (cmd
.flags
& IB_MR_REREG_PD
)
1123 put_uobj_write(mr
->uobject
);
1128 ssize_t
ib_uverbs_dereg_mr(struct ib_uverbs_file
*file
,
1129 struct ib_device
*ib_dev
,
1130 const char __user
*buf
, int in_len
,
1133 struct ib_uverbs_dereg_mr cmd
;
1135 struct ib_uobject
*uobj
;
1138 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1141 uobj
= idr_write_uobj(&ib_uverbs_mr_idr
, cmd
.mr_handle
, file
->ucontext
);
1147 ret
= ib_dereg_mr(mr
);
1151 put_uobj_write(uobj
);
1156 idr_remove_uobj(&ib_uverbs_mr_idr
, uobj
);
1158 mutex_lock(&file
->mutex
);
1159 list_del(&uobj
->list
);
1160 mutex_unlock(&file
->mutex
);
1167 ssize_t
ib_uverbs_alloc_mw(struct ib_uverbs_file
*file
,
1168 struct ib_device
*ib_dev
,
1169 const char __user
*buf
, int in_len
,
1172 struct ib_uverbs_alloc_mw cmd
;
1173 struct ib_uverbs_alloc_mw_resp resp
;
1174 struct ib_uobject
*uobj
;
1179 if (out_len
< sizeof(resp
))
1182 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1185 uobj
= kmalloc(sizeof(*uobj
), GFP_KERNEL
);
1189 init_uobj(uobj
, 0, file
->ucontext
, &mw_lock_class
);
1190 down_write(&uobj
->mutex
);
1192 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
1198 mw
= pd
->device
->alloc_mw(pd
, cmd
.mw_type
);
1204 mw
->device
= pd
->device
;
1207 atomic_inc(&pd
->usecnt
);
1210 ret
= idr_add_uobj(&ib_uverbs_mw_idr
, uobj
);
1214 memset(&resp
, 0, sizeof(resp
));
1215 resp
.rkey
= mw
->rkey
;
1216 resp
.mw_handle
= uobj
->id
;
1218 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
1219 &resp
, sizeof(resp
))) {
1226 mutex_lock(&file
->mutex
);
1227 list_add_tail(&uobj
->list
, &file
->ucontext
->mw_list
);
1228 mutex_unlock(&file
->mutex
);
1232 up_write(&uobj
->mutex
);
1237 idr_remove_uobj(&ib_uverbs_mw_idr
, uobj
);
1240 uverbs_dealloc_mw(mw
);
1246 put_uobj_write(uobj
);
1250 ssize_t
ib_uverbs_dealloc_mw(struct ib_uverbs_file
*file
,
1251 struct ib_device
*ib_dev
,
1252 const char __user
*buf
, int in_len
,
1255 struct ib_uverbs_dealloc_mw cmd
;
1257 struct ib_uobject
*uobj
;
1260 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1263 uobj
= idr_write_uobj(&ib_uverbs_mw_idr
, cmd
.mw_handle
, file
->ucontext
);
1269 ret
= uverbs_dealloc_mw(mw
);
1273 put_uobj_write(uobj
);
1278 idr_remove_uobj(&ib_uverbs_mw_idr
, uobj
);
1280 mutex_lock(&file
->mutex
);
1281 list_del(&uobj
->list
);
1282 mutex_unlock(&file
->mutex
);
1289 ssize_t
ib_uverbs_create_comp_channel(struct ib_uverbs_file
*file
,
1290 struct ib_device
*ib_dev
,
1291 const char __user
*buf
, int in_len
,
1294 struct ib_uverbs_create_comp_channel cmd
;
1295 struct ib_uverbs_create_comp_channel_resp resp
;
1299 if (out_len
< sizeof resp
)
1302 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1305 ret
= get_unused_fd_flags(O_CLOEXEC
);
1310 filp
= ib_uverbs_alloc_event_file(file
, ib_dev
, 0);
1312 put_unused_fd(resp
.fd
);
1313 return PTR_ERR(filp
);
1316 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1317 &resp
, sizeof resp
)) {
1318 put_unused_fd(resp
.fd
);
1323 fd_install(resp
.fd
, filp
);
1327 static struct ib_ucq_object
*create_cq(struct ib_uverbs_file
*file
,
1328 struct ib_device
*ib_dev
,
1329 struct ib_udata
*ucore
,
1330 struct ib_udata
*uhw
,
1331 struct ib_uverbs_ex_create_cq
*cmd
,
1333 int (*cb
)(struct ib_uverbs_file
*file
,
1334 struct ib_ucq_object
*obj
,
1335 struct ib_uverbs_ex_create_cq_resp
*resp
,
1336 struct ib_udata
*udata
,
1340 struct ib_ucq_object
*obj
;
1341 struct ib_uverbs_event_file
*ev_file
= NULL
;
1344 struct ib_uverbs_ex_create_cq_resp resp
;
1345 struct ib_cq_init_attr attr
= {};
1347 if (cmd
->comp_vector
>= file
->device
->num_comp_vectors
)
1348 return ERR_PTR(-EINVAL
);
1350 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
1352 return ERR_PTR(-ENOMEM
);
1354 init_uobj(&obj
->uobject
, cmd
->user_handle
, file
->ucontext
, &cq_lock_class
);
1355 down_write(&obj
->uobject
.mutex
);
1357 if (cmd
->comp_channel
>= 0) {
1358 ev_file
= ib_uverbs_lookup_comp_file(cmd
->comp_channel
);
1365 obj
->uverbs_file
= file
;
1366 obj
->comp_events_reported
= 0;
1367 obj
->async_events_reported
= 0;
1368 INIT_LIST_HEAD(&obj
->comp_list
);
1369 INIT_LIST_HEAD(&obj
->async_list
);
1371 attr
.cqe
= cmd
->cqe
;
1372 attr
.comp_vector
= cmd
->comp_vector
;
1374 if (cmd_sz
> offsetof(typeof(*cmd
), flags
) + sizeof(cmd
->flags
))
1375 attr
.flags
= cmd
->flags
;
1377 cq
= ib_dev
->create_cq(ib_dev
, &attr
,
1378 file
->ucontext
, uhw
);
1384 cq
->device
= ib_dev
;
1385 cq
->uobject
= &obj
->uobject
;
1386 cq
->comp_handler
= ib_uverbs_comp_handler
;
1387 cq
->event_handler
= ib_uverbs_cq_event_handler
;
1388 cq
->cq_context
= ev_file
;
1389 atomic_set(&cq
->usecnt
, 0);
1391 obj
->uobject
.object
= cq
;
1392 ret
= idr_add_uobj(&ib_uverbs_cq_idr
, &obj
->uobject
);
1396 memset(&resp
, 0, sizeof resp
);
1397 resp
.base
.cq_handle
= obj
->uobject
.id
;
1398 resp
.base
.cqe
= cq
->cqe
;
1400 resp
.response_length
= offsetof(typeof(resp
), response_length
) +
1401 sizeof(resp
.response_length
);
1403 ret
= cb(file
, obj
, &resp
, ucore
, context
);
1407 mutex_lock(&file
->mutex
);
1408 list_add_tail(&obj
->uobject
.list
, &file
->ucontext
->cq_list
);
1409 mutex_unlock(&file
->mutex
);
1411 obj
->uobject
.live
= 1;
1413 up_write(&obj
->uobject
.mutex
);
1418 idr_remove_uobj(&ib_uverbs_cq_idr
, &obj
->uobject
);
1425 ib_uverbs_release_ucq(file
, ev_file
, obj
);
1428 put_uobj_write(&obj
->uobject
);
1430 return ERR_PTR(ret
);
1433 static int ib_uverbs_create_cq_cb(struct ib_uverbs_file
*file
,
1434 struct ib_ucq_object
*obj
,
1435 struct ib_uverbs_ex_create_cq_resp
*resp
,
1436 struct ib_udata
*ucore
, void *context
)
1438 if (ib_copy_to_udata(ucore
, &resp
->base
, sizeof(resp
->base
)))
1444 ssize_t
ib_uverbs_create_cq(struct ib_uverbs_file
*file
,
1445 struct ib_device
*ib_dev
,
1446 const char __user
*buf
, int in_len
,
1449 struct ib_uverbs_create_cq cmd
;
1450 struct ib_uverbs_ex_create_cq cmd_ex
;
1451 struct ib_uverbs_create_cq_resp resp
;
1452 struct ib_udata ucore
;
1453 struct ib_udata uhw
;
1454 struct ib_ucq_object
*obj
;
1456 if (out_len
< sizeof(resp
))
1459 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1462 INIT_UDATA(&ucore
, buf
, (unsigned long)cmd
.response
, sizeof(cmd
), sizeof(resp
));
1464 INIT_UDATA(&uhw
, buf
+ sizeof(cmd
),
1465 (unsigned long)cmd
.response
+ sizeof(resp
),
1466 in_len
- sizeof(cmd
), out_len
- sizeof(resp
));
1468 memset(&cmd_ex
, 0, sizeof(cmd_ex
));
1469 cmd_ex
.user_handle
= cmd
.user_handle
;
1470 cmd_ex
.cqe
= cmd
.cqe
;
1471 cmd_ex
.comp_vector
= cmd
.comp_vector
;
1472 cmd_ex
.comp_channel
= cmd
.comp_channel
;
1474 obj
= create_cq(file
, ib_dev
, &ucore
, &uhw
, &cmd_ex
,
1475 offsetof(typeof(cmd_ex
), comp_channel
) +
1476 sizeof(cmd
.comp_channel
), ib_uverbs_create_cq_cb
,
1480 return PTR_ERR(obj
);
1485 static int ib_uverbs_ex_create_cq_cb(struct ib_uverbs_file
*file
,
1486 struct ib_ucq_object
*obj
,
1487 struct ib_uverbs_ex_create_cq_resp
*resp
,
1488 struct ib_udata
*ucore
, void *context
)
1490 if (ib_copy_to_udata(ucore
, resp
, resp
->response_length
))
1496 int ib_uverbs_ex_create_cq(struct ib_uverbs_file
*file
,
1497 struct ib_device
*ib_dev
,
1498 struct ib_udata
*ucore
,
1499 struct ib_udata
*uhw
)
1501 struct ib_uverbs_ex_create_cq_resp resp
;
1502 struct ib_uverbs_ex_create_cq cmd
;
1503 struct ib_ucq_object
*obj
;
1506 if (ucore
->inlen
< sizeof(cmd
))
1509 err
= ib_copy_from_udata(&cmd
, ucore
, sizeof(cmd
));
1519 if (ucore
->outlen
< (offsetof(typeof(resp
), response_length
) +
1520 sizeof(resp
.response_length
)))
1523 obj
= create_cq(file
, ib_dev
, ucore
, uhw
, &cmd
,
1524 min(ucore
->inlen
, sizeof(cmd
)),
1525 ib_uverbs_ex_create_cq_cb
, NULL
);
1528 return PTR_ERR(obj
);
1533 ssize_t
ib_uverbs_resize_cq(struct ib_uverbs_file
*file
,
1534 struct ib_device
*ib_dev
,
1535 const char __user
*buf
, int in_len
,
1538 struct ib_uverbs_resize_cq cmd
;
1539 struct ib_uverbs_resize_cq_resp resp
;
1540 struct ib_udata udata
;
1544 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1547 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
1548 (unsigned long) cmd
.response
+ sizeof resp
,
1549 in_len
- sizeof cmd
, out_len
- sizeof resp
);
1551 cq
= idr_read_cq(cmd
.cq_handle
, file
->ucontext
, 0);
1555 ret
= cq
->device
->resize_cq(cq
, cmd
.cqe
, &udata
);
1561 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1562 &resp
, sizeof resp
.cqe
))
1568 return ret
? ret
: in_len
;
1571 static int copy_wc_to_user(void __user
*dest
, struct ib_wc
*wc
)
1573 struct ib_uverbs_wc tmp
;
1575 tmp
.wr_id
= wc
->wr_id
;
1576 tmp
.status
= wc
->status
;
1577 tmp
.opcode
= wc
->opcode
;
1578 tmp
.vendor_err
= wc
->vendor_err
;
1579 tmp
.byte_len
= wc
->byte_len
;
1580 tmp
.ex
.imm_data
= (__u32 __force
) wc
->ex
.imm_data
;
1581 tmp
.qp_num
= wc
->qp
->qp_num
;
1582 tmp
.src_qp
= wc
->src_qp
;
1583 tmp
.wc_flags
= wc
->wc_flags
;
1584 tmp
.pkey_index
= wc
->pkey_index
;
1585 tmp
.slid
= wc
->slid
;
1587 tmp
.dlid_path_bits
= wc
->dlid_path_bits
;
1588 tmp
.port_num
= wc
->port_num
;
1591 if (copy_to_user(dest
, &tmp
, sizeof tmp
))
1597 ssize_t
ib_uverbs_poll_cq(struct ib_uverbs_file
*file
,
1598 struct ib_device
*ib_dev
,
1599 const char __user
*buf
, int in_len
,
1602 struct ib_uverbs_poll_cq cmd
;
1603 struct ib_uverbs_poll_cq_resp resp
;
1604 u8 __user
*header_ptr
;
1605 u8 __user
*data_ptr
;
1610 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1613 cq
= idr_read_cq(cmd
.cq_handle
, file
->ucontext
, 0);
1617 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1618 header_ptr
= (void __user
*)(unsigned long) cmd
.response
;
1619 data_ptr
= header_ptr
+ sizeof resp
;
1621 memset(&resp
, 0, sizeof resp
);
1622 while (resp
.count
< cmd
.ne
) {
1623 ret
= ib_poll_cq(cq
, 1, &wc
);
1629 ret
= copy_wc_to_user(data_ptr
, &wc
);
1633 data_ptr
+= sizeof(struct ib_uverbs_wc
);
1637 if (copy_to_user(header_ptr
, &resp
, sizeof resp
)) {
1649 ssize_t
ib_uverbs_req_notify_cq(struct ib_uverbs_file
*file
,
1650 struct ib_device
*ib_dev
,
1651 const char __user
*buf
, int in_len
,
1654 struct ib_uverbs_req_notify_cq cmd
;
1657 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1660 cq
= idr_read_cq(cmd
.cq_handle
, file
->ucontext
, 0);
1664 ib_req_notify_cq(cq
, cmd
.solicited_only
?
1665 IB_CQ_SOLICITED
: IB_CQ_NEXT_COMP
);
1672 ssize_t
ib_uverbs_destroy_cq(struct ib_uverbs_file
*file
,
1673 struct ib_device
*ib_dev
,
1674 const char __user
*buf
, int in_len
,
1677 struct ib_uverbs_destroy_cq cmd
;
1678 struct ib_uverbs_destroy_cq_resp resp
;
1679 struct ib_uobject
*uobj
;
1681 struct ib_ucq_object
*obj
;
1682 struct ib_uverbs_event_file
*ev_file
;
1685 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
1688 uobj
= idr_write_uobj(&ib_uverbs_cq_idr
, cmd
.cq_handle
, file
->ucontext
);
1692 ev_file
= cq
->cq_context
;
1693 obj
= container_of(cq
->uobject
, struct ib_ucq_object
, uobject
);
1695 ret
= ib_destroy_cq(cq
);
1699 put_uobj_write(uobj
);
1704 idr_remove_uobj(&ib_uverbs_cq_idr
, uobj
);
1706 mutex_lock(&file
->mutex
);
1707 list_del(&uobj
->list
);
1708 mutex_unlock(&file
->mutex
);
1710 ib_uverbs_release_ucq(file
, ev_file
, obj
);
1712 memset(&resp
, 0, sizeof resp
);
1713 resp
.comp_events_reported
= obj
->comp_events_reported
;
1714 resp
.async_events_reported
= obj
->async_events_reported
;
1718 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
1719 &resp
, sizeof resp
))
1725 static int create_qp(struct ib_uverbs_file
*file
,
1726 struct ib_udata
*ucore
,
1727 struct ib_udata
*uhw
,
1728 struct ib_uverbs_ex_create_qp
*cmd
,
1730 int (*cb
)(struct ib_uverbs_file
*file
,
1731 struct ib_uverbs_ex_create_qp_resp
*resp
,
1732 struct ib_udata
*udata
),
1735 struct ib_uqp_object
*obj
;
1736 struct ib_device
*device
;
1737 struct ib_pd
*pd
= NULL
;
1738 struct ib_xrcd
*xrcd
= NULL
;
1739 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
1740 struct ib_cq
*scq
= NULL
, *rcq
= NULL
;
1741 struct ib_srq
*srq
= NULL
;
1744 struct ib_qp_init_attr attr
;
1745 struct ib_uverbs_ex_create_qp_resp resp
;
1748 if (cmd
->qp_type
== IB_QPT_RAW_PACKET
&& !capable(CAP_NET_RAW
))
1751 obj
= kzalloc(sizeof *obj
, GFP_KERNEL
);
1755 init_uobj(&obj
->uevent
.uobject
, cmd
->user_handle
, file
->ucontext
,
1757 down_write(&obj
->uevent
.uobject
.mutex
);
1759 if (cmd
->qp_type
== IB_QPT_XRC_TGT
) {
1760 xrcd
= idr_read_xrcd(cmd
->pd_handle
, file
->ucontext
,
1766 device
= xrcd
->device
;
1768 if (cmd
->qp_type
== IB_QPT_XRC_INI
) {
1769 cmd
->max_recv_wr
= 0;
1770 cmd
->max_recv_sge
= 0;
1773 srq
= idr_read_srq(cmd
->srq_handle
,
1775 if (!srq
|| srq
->srq_type
!= IB_SRQT_BASIC
) {
1781 if (cmd
->recv_cq_handle
!= cmd
->send_cq_handle
) {
1782 rcq
= idr_read_cq(cmd
->recv_cq_handle
,
1791 scq
= idr_read_cq(cmd
->send_cq_handle
, file
->ucontext
, !!rcq
);
1793 pd
= idr_read_pd(cmd
->pd_handle
, file
->ucontext
);
1799 device
= pd
->device
;
1802 attr
.event_handler
= ib_uverbs_qp_event_handler
;
1803 attr
.qp_context
= file
;
1808 attr
.sq_sig_type
= cmd
->sq_sig_all
? IB_SIGNAL_ALL_WR
:
1810 attr
.qp_type
= cmd
->qp_type
;
1811 attr
.create_flags
= 0;
1813 attr
.cap
.max_send_wr
= cmd
->max_send_wr
;
1814 attr
.cap
.max_recv_wr
= cmd
->max_recv_wr
;
1815 attr
.cap
.max_send_sge
= cmd
->max_send_sge
;
1816 attr
.cap
.max_recv_sge
= cmd
->max_recv_sge
;
1817 attr
.cap
.max_inline_data
= cmd
->max_inline_data
;
1819 obj
->uevent
.events_reported
= 0;
1820 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
1821 INIT_LIST_HEAD(&obj
->mcast_list
);
1823 if (cmd_sz
>= offsetof(typeof(*cmd
), create_flags
) +
1824 sizeof(cmd
->create_flags
))
1825 attr
.create_flags
= cmd
->create_flags
;
1827 if (attr
.create_flags
& ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK
|
1828 IB_QP_CREATE_CROSS_CHANNEL
|
1829 IB_QP_CREATE_MANAGED_SEND
|
1830 IB_QP_CREATE_MANAGED_RECV
)) {
1835 buf
= (void *)cmd
+ sizeof(*cmd
);
1836 if (cmd_sz
> sizeof(*cmd
))
1837 if (!(buf
[0] == 0 && !memcmp(buf
, buf
+ 1,
1838 cmd_sz
- sizeof(*cmd
) - 1))) {
1843 if (cmd
->qp_type
== IB_QPT_XRC_TGT
)
1844 qp
= ib_create_qp(pd
, &attr
);
1846 qp
= device
->create_qp(pd
, &attr
, uhw
);
1853 if (cmd
->qp_type
!= IB_QPT_XRC_TGT
) {
1855 qp
->device
= device
;
1857 qp
->send_cq
= attr
.send_cq
;
1858 qp
->recv_cq
= attr
.recv_cq
;
1860 qp
->event_handler
= attr
.event_handler
;
1861 qp
->qp_context
= attr
.qp_context
;
1862 qp
->qp_type
= attr
.qp_type
;
1863 atomic_set(&qp
->usecnt
, 0);
1864 atomic_inc(&pd
->usecnt
);
1865 atomic_inc(&attr
.send_cq
->usecnt
);
1867 atomic_inc(&attr
.recv_cq
->usecnt
);
1869 atomic_inc(&attr
.srq
->usecnt
);
1871 qp
->uobject
= &obj
->uevent
.uobject
;
1873 obj
->uevent
.uobject
.object
= qp
;
1874 ret
= idr_add_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
1878 memset(&resp
, 0, sizeof resp
);
1879 resp
.base
.qpn
= qp
->qp_num
;
1880 resp
.base
.qp_handle
= obj
->uevent
.uobject
.id
;
1881 resp
.base
.max_recv_sge
= attr
.cap
.max_recv_sge
;
1882 resp
.base
.max_send_sge
= attr
.cap
.max_send_sge
;
1883 resp
.base
.max_recv_wr
= attr
.cap
.max_recv_wr
;
1884 resp
.base
.max_send_wr
= attr
.cap
.max_send_wr
;
1885 resp
.base
.max_inline_data
= attr
.cap
.max_inline_data
;
1887 resp
.response_length
= offsetof(typeof(resp
), response_length
) +
1888 sizeof(resp
.response_length
);
1890 ret
= cb(file
, &resp
, ucore
);
1895 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
,
1897 atomic_inc(&obj
->uxrcd
->refcnt
);
1898 put_xrcd_read(xrcd_uobj
);
1905 if (rcq
&& rcq
!= scq
)
1910 mutex_lock(&file
->mutex
);
1911 list_add_tail(&obj
->uevent
.uobject
.list
, &file
->ucontext
->qp_list
);
1912 mutex_unlock(&file
->mutex
);
1914 obj
->uevent
.uobject
.live
= 1;
1916 up_write(&obj
->uevent
.uobject
.mutex
);
1920 idr_remove_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
1927 put_xrcd_read(xrcd_uobj
);
1932 if (rcq
&& rcq
!= scq
)
1937 put_uobj_write(&obj
->uevent
.uobject
);
1941 static int ib_uverbs_create_qp_cb(struct ib_uverbs_file
*file
,
1942 struct ib_uverbs_ex_create_qp_resp
*resp
,
1943 struct ib_udata
*ucore
)
1945 if (ib_copy_to_udata(ucore
, &resp
->base
, sizeof(resp
->base
)))
1951 ssize_t
ib_uverbs_create_qp(struct ib_uverbs_file
*file
,
1952 struct ib_device
*ib_dev
,
1953 const char __user
*buf
, int in_len
,
1956 struct ib_uverbs_create_qp cmd
;
1957 struct ib_uverbs_ex_create_qp cmd_ex
;
1958 struct ib_udata ucore
;
1959 struct ib_udata uhw
;
1960 ssize_t resp_size
= sizeof(struct ib_uverbs_create_qp_resp
);
1963 if (out_len
< resp_size
)
1966 if (copy_from_user(&cmd
, buf
, sizeof(cmd
)))
1969 INIT_UDATA(&ucore
, buf
, (unsigned long)cmd
.response
, sizeof(cmd
),
1971 INIT_UDATA(&uhw
, buf
+ sizeof(cmd
),
1972 (unsigned long)cmd
.response
+ resp_size
,
1973 in_len
- sizeof(cmd
) - sizeof(struct ib_uverbs_cmd_hdr
),
1974 out_len
- resp_size
);
1976 memset(&cmd_ex
, 0, sizeof(cmd_ex
));
1977 cmd_ex
.user_handle
= cmd
.user_handle
;
1978 cmd_ex
.pd_handle
= cmd
.pd_handle
;
1979 cmd_ex
.send_cq_handle
= cmd
.send_cq_handle
;
1980 cmd_ex
.recv_cq_handle
= cmd
.recv_cq_handle
;
1981 cmd_ex
.srq_handle
= cmd
.srq_handle
;
1982 cmd_ex
.max_send_wr
= cmd
.max_send_wr
;
1983 cmd_ex
.max_recv_wr
= cmd
.max_recv_wr
;
1984 cmd_ex
.max_send_sge
= cmd
.max_send_sge
;
1985 cmd_ex
.max_recv_sge
= cmd
.max_recv_sge
;
1986 cmd_ex
.max_inline_data
= cmd
.max_inline_data
;
1987 cmd_ex
.sq_sig_all
= cmd
.sq_sig_all
;
1988 cmd_ex
.qp_type
= cmd
.qp_type
;
1989 cmd_ex
.is_srq
= cmd
.is_srq
;
1991 err
= create_qp(file
, &ucore
, &uhw
, &cmd_ex
,
1992 offsetof(typeof(cmd_ex
), is_srq
) +
1993 sizeof(cmd
.is_srq
), ib_uverbs_create_qp_cb
,
2002 static int ib_uverbs_ex_create_qp_cb(struct ib_uverbs_file
*file
,
2003 struct ib_uverbs_ex_create_qp_resp
*resp
,
2004 struct ib_udata
*ucore
)
2006 if (ib_copy_to_udata(ucore
, resp
, resp
->response_length
))
2012 int ib_uverbs_ex_create_qp(struct ib_uverbs_file
*file
,
2013 struct ib_device
*ib_dev
,
2014 struct ib_udata
*ucore
,
2015 struct ib_udata
*uhw
)
2017 struct ib_uverbs_ex_create_qp_resp resp
;
2018 struct ib_uverbs_ex_create_qp cmd
= {0};
2021 if (ucore
->inlen
< (offsetof(typeof(cmd
), comp_mask
) +
2022 sizeof(cmd
.comp_mask
)))
2025 err
= ib_copy_from_udata(&cmd
, ucore
, min(sizeof(cmd
), ucore
->inlen
));
2035 if (ucore
->outlen
< (offsetof(typeof(resp
), response_length
) +
2036 sizeof(resp
.response_length
)))
2039 err
= create_qp(file
, ucore
, uhw
, &cmd
,
2040 min(ucore
->inlen
, sizeof(cmd
)),
2041 ib_uverbs_ex_create_qp_cb
, NULL
);
2049 ssize_t
ib_uverbs_open_qp(struct ib_uverbs_file
*file
,
2050 struct ib_device
*ib_dev
,
2051 const char __user
*buf
, int in_len
, int out_len
)
2053 struct ib_uverbs_open_qp cmd
;
2054 struct ib_uverbs_create_qp_resp resp
;
2055 struct ib_udata udata
;
2056 struct ib_uqp_object
*obj
;
2057 struct ib_xrcd
*xrcd
;
2058 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
2060 struct ib_qp_open_attr attr
;
2063 if (out_len
< sizeof resp
)
2066 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2069 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
2070 (unsigned long) cmd
.response
+ sizeof resp
,
2071 in_len
- sizeof cmd
, out_len
- sizeof resp
);
2073 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
2077 init_uobj(&obj
->uevent
.uobject
, cmd
.user_handle
, file
->ucontext
, &qp_lock_class
);
2078 down_write(&obj
->uevent
.uobject
.mutex
);
2080 xrcd
= idr_read_xrcd(cmd
.pd_handle
, file
->ucontext
, &xrcd_uobj
);
2086 attr
.event_handler
= ib_uverbs_qp_event_handler
;
2087 attr
.qp_context
= file
;
2088 attr
.qp_num
= cmd
.qpn
;
2089 attr
.qp_type
= cmd
.qp_type
;
2091 obj
->uevent
.events_reported
= 0;
2092 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
2093 INIT_LIST_HEAD(&obj
->mcast_list
);
2095 qp
= ib_open_qp(xrcd
, &attr
);
2101 qp
->uobject
= &obj
->uevent
.uobject
;
2103 obj
->uevent
.uobject
.object
= qp
;
2104 ret
= idr_add_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
2108 memset(&resp
, 0, sizeof resp
);
2109 resp
.qpn
= qp
->qp_num
;
2110 resp
.qp_handle
= obj
->uevent
.uobject
.id
;
2112 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2113 &resp
, sizeof resp
)) {
2118 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
, uobject
);
2119 atomic_inc(&obj
->uxrcd
->refcnt
);
2120 put_xrcd_read(xrcd_uobj
);
2122 mutex_lock(&file
->mutex
);
2123 list_add_tail(&obj
->uevent
.uobject
.list
, &file
->ucontext
->qp_list
);
2124 mutex_unlock(&file
->mutex
);
2126 obj
->uevent
.uobject
.live
= 1;
2128 up_write(&obj
->uevent
.uobject
.mutex
);
2133 idr_remove_uobj(&ib_uverbs_qp_idr
, &obj
->uevent
.uobject
);
2139 put_xrcd_read(xrcd_uobj
);
2140 put_uobj_write(&obj
->uevent
.uobject
);
2144 ssize_t
ib_uverbs_query_qp(struct ib_uverbs_file
*file
,
2145 struct ib_device
*ib_dev
,
2146 const char __user
*buf
, int in_len
,
2149 struct ib_uverbs_query_qp cmd
;
2150 struct ib_uverbs_query_qp_resp resp
;
2152 struct ib_qp_attr
*attr
;
2153 struct ib_qp_init_attr
*init_attr
;
2156 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2159 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2160 init_attr
= kmalloc(sizeof *init_attr
, GFP_KERNEL
);
2161 if (!attr
|| !init_attr
) {
2166 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2172 ret
= ib_query_qp(qp
, attr
, cmd
.attr_mask
, init_attr
);
2179 memset(&resp
, 0, sizeof resp
);
2181 resp
.qp_state
= attr
->qp_state
;
2182 resp
.cur_qp_state
= attr
->cur_qp_state
;
2183 resp
.path_mtu
= attr
->path_mtu
;
2184 resp
.path_mig_state
= attr
->path_mig_state
;
2185 resp
.qkey
= attr
->qkey
;
2186 resp
.rq_psn
= attr
->rq_psn
;
2187 resp
.sq_psn
= attr
->sq_psn
;
2188 resp
.dest_qp_num
= attr
->dest_qp_num
;
2189 resp
.qp_access_flags
= attr
->qp_access_flags
;
2190 resp
.pkey_index
= attr
->pkey_index
;
2191 resp
.alt_pkey_index
= attr
->alt_pkey_index
;
2192 resp
.sq_draining
= attr
->sq_draining
;
2193 resp
.max_rd_atomic
= attr
->max_rd_atomic
;
2194 resp
.max_dest_rd_atomic
= attr
->max_dest_rd_atomic
;
2195 resp
.min_rnr_timer
= attr
->min_rnr_timer
;
2196 resp
.port_num
= attr
->port_num
;
2197 resp
.timeout
= attr
->timeout
;
2198 resp
.retry_cnt
= attr
->retry_cnt
;
2199 resp
.rnr_retry
= attr
->rnr_retry
;
2200 resp
.alt_port_num
= attr
->alt_port_num
;
2201 resp
.alt_timeout
= attr
->alt_timeout
;
2203 memcpy(resp
.dest
.dgid
, attr
->ah_attr
.grh
.dgid
.raw
, 16);
2204 resp
.dest
.flow_label
= attr
->ah_attr
.grh
.flow_label
;
2205 resp
.dest
.sgid_index
= attr
->ah_attr
.grh
.sgid_index
;
2206 resp
.dest
.hop_limit
= attr
->ah_attr
.grh
.hop_limit
;
2207 resp
.dest
.traffic_class
= attr
->ah_attr
.grh
.traffic_class
;
2208 resp
.dest
.dlid
= attr
->ah_attr
.dlid
;
2209 resp
.dest
.sl
= attr
->ah_attr
.sl
;
2210 resp
.dest
.src_path_bits
= attr
->ah_attr
.src_path_bits
;
2211 resp
.dest
.static_rate
= attr
->ah_attr
.static_rate
;
2212 resp
.dest
.is_global
= !!(attr
->ah_attr
.ah_flags
& IB_AH_GRH
);
2213 resp
.dest
.port_num
= attr
->ah_attr
.port_num
;
2215 memcpy(resp
.alt_dest
.dgid
, attr
->alt_ah_attr
.grh
.dgid
.raw
, 16);
2216 resp
.alt_dest
.flow_label
= attr
->alt_ah_attr
.grh
.flow_label
;
2217 resp
.alt_dest
.sgid_index
= attr
->alt_ah_attr
.grh
.sgid_index
;
2218 resp
.alt_dest
.hop_limit
= attr
->alt_ah_attr
.grh
.hop_limit
;
2219 resp
.alt_dest
.traffic_class
= attr
->alt_ah_attr
.grh
.traffic_class
;
2220 resp
.alt_dest
.dlid
= attr
->alt_ah_attr
.dlid
;
2221 resp
.alt_dest
.sl
= attr
->alt_ah_attr
.sl
;
2222 resp
.alt_dest
.src_path_bits
= attr
->alt_ah_attr
.src_path_bits
;
2223 resp
.alt_dest
.static_rate
= attr
->alt_ah_attr
.static_rate
;
2224 resp
.alt_dest
.is_global
= !!(attr
->alt_ah_attr
.ah_flags
& IB_AH_GRH
);
2225 resp
.alt_dest
.port_num
= attr
->alt_ah_attr
.port_num
;
2227 resp
.max_send_wr
= init_attr
->cap
.max_send_wr
;
2228 resp
.max_recv_wr
= init_attr
->cap
.max_recv_wr
;
2229 resp
.max_send_sge
= init_attr
->cap
.max_send_sge
;
2230 resp
.max_recv_sge
= init_attr
->cap
.max_recv_sge
;
2231 resp
.max_inline_data
= init_attr
->cap
.max_inline_data
;
2232 resp
.sq_sig_all
= init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
;
2234 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2235 &resp
, sizeof resp
))
2242 return ret
? ret
: in_len
;
2245 /* Remove ignored fields set in the attribute mask */
2246 static int modify_qp_mask(enum ib_qp_type qp_type
, int mask
)
2249 case IB_QPT_XRC_INI
:
2250 return mask
& ~(IB_QP_MAX_DEST_RD_ATOMIC
| IB_QP_MIN_RNR_TIMER
);
2251 case IB_QPT_XRC_TGT
:
2252 return mask
& ~(IB_QP_MAX_QP_RD_ATOMIC
| IB_QP_RETRY_CNT
|
2259 ssize_t
ib_uverbs_modify_qp(struct ib_uverbs_file
*file
,
2260 struct ib_device
*ib_dev
,
2261 const char __user
*buf
, int in_len
,
2264 struct ib_uverbs_modify_qp cmd
;
2265 struct ib_udata udata
;
2267 struct ib_qp_attr
*attr
;
2270 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2273 INIT_UDATA(&udata
, buf
+ sizeof cmd
, NULL
, in_len
- sizeof cmd
,
2276 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2280 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2286 attr
->qp_state
= cmd
.qp_state
;
2287 attr
->cur_qp_state
= cmd
.cur_qp_state
;
2288 attr
->path_mtu
= cmd
.path_mtu
;
2289 attr
->path_mig_state
= cmd
.path_mig_state
;
2290 attr
->qkey
= cmd
.qkey
;
2291 attr
->rq_psn
= cmd
.rq_psn
;
2292 attr
->sq_psn
= cmd
.sq_psn
;
2293 attr
->dest_qp_num
= cmd
.dest_qp_num
;
2294 attr
->qp_access_flags
= cmd
.qp_access_flags
;
2295 attr
->pkey_index
= cmd
.pkey_index
;
2296 attr
->alt_pkey_index
= cmd
.alt_pkey_index
;
2297 attr
->en_sqd_async_notify
= cmd
.en_sqd_async_notify
;
2298 attr
->max_rd_atomic
= cmd
.max_rd_atomic
;
2299 attr
->max_dest_rd_atomic
= cmd
.max_dest_rd_atomic
;
2300 attr
->min_rnr_timer
= cmd
.min_rnr_timer
;
2301 attr
->port_num
= cmd
.port_num
;
2302 attr
->timeout
= cmd
.timeout
;
2303 attr
->retry_cnt
= cmd
.retry_cnt
;
2304 attr
->rnr_retry
= cmd
.rnr_retry
;
2305 attr
->alt_port_num
= cmd
.alt_port_num
;
2306 attr
->alt_timeout
= cmd
.alt_timeout
;
2308 memcpy(attr
->ah_attr
.grh
.dgid
.raw
, cmd
.dest
.dgid
, 16);
2309 attr
->ah_attr
.grh
.flow_label
= cmd
.dest
.flow_label
;
2310 attr
->ah_attr
.grh
.sgid_index
= cmd
.dest
.sgid_index
;
2311 attr
->ah_attr
.grh
.hop_limit
= cmd
.dest
.hop_limit
;
2312 attr
->ah_attr
.grh
.traffic_class
= cmd
.dest
.traffic_class
;
2313 attr
->ah_attr
.dlid
= cmd
.dest
.dlid
;
2314 attr
->ah_attr
.sl
= cmd
.dest
.sl
;
2315 attr
->ah_attr
.src_path_bits
= cmd
.dest
.src_path_bits
;
2316 attr
->ah_attr
.static_rate
= cmd
.dest
.static_rate
;
2317 attr
->ah_attr
.ah_flags
= cmd
.dest
.is_global
? IB_AH_GRH
: 0;
2318 attr
->ah_attr
.port_num
= cmd
.dest
.port_num
;
2320 memcpy(attr
->alt_ah_attr
.grh
.dgid
.raw
, cmd
.alt_dest
.dgid
, 16);
2321 attr
->alt_ah_attr
.grh
.flow_label
= cmd
.alt_dest
.flow_label
;
2322 attr
->alt_ah_attr
.grh
.sgid_index
= cmd
.alt_dest
.sgid_index
;
2323 attr
->alt_ah_attr
.grh
.hop_limit
= cmd
.alt_dest
.hop_limit
;
2324 attr
->alt_ah_attr
.grh
.traffic_class
= cmd
.alt_dest
.traffic_class
;
2325 attr
->alt_ah_attr
.dlid
= cmd
.alt_dest
.dlid
;
2326 attr
->alt_ah_attr
.sl
= cmd
.alt_dest
.sl
;
2327 attr
->alt_ah_attr
.src_path_bits
= cmd
.alt_dest
.src_path_bits
;
2328 attr
->alt_ah_attr
.static_rate
= cmd
.alt_dest
.static_rate
;
2329 attr
->alt_ah_attr
.ah_flags
= cmd
.alt_dest
.is_global
? IB_AH_GRH
: 0;
2330 attr
->alt_ah_attr
.port_num
= cmd
.alt_dest
.port_num
;
2332 if (qp
->real_qp
== qp
) {
2333 ret
= ib_resolve_eth_dmac(qp
, attr
, &cmd
.attr_mask
);
2336 ret
= qp
->device
->modify_qp(qp
, attr
,
2337 modify_qp_mask(qp
->qp_type
, cmd
.attr_mask
), &udata
);
2339 ret
= ib_modify_qp(qp
, attr
, modify_qp_mask(qp
->qp_type
, cmd
.attr_mask
));
2356 ssize_t
ib_uverbs_destroy_qp(struct ib_uverbs_file
*file
,
2357 struct ib_device
*ib_dev
,
2358 const char __user
*buf
, int in_len
,
2361 struct ib_uverbs_destroy_qp cmd
;
2362 struct ib_uverbs_destroy_qp_resp resp
;
2363 struct ib_uobject
*uobj
;
2365 struct ib_uqp_object
*obj
;
2368 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2371 memset(&resp
, 0, sizeof resp
);
2373 uobj
= idr_write_uobj(&ib_uverbs_qp_idr
, cmd
.qp_handle
, file
->ucontext
);
2377 obj
= container_of(uobj
, struct ib_uqp_object
, uevent
.uobject
);
2379 if (!list_empty(&obj
->mcast_list
)) {
2380 put_uobj_write(uobj
);
2384 ret
= ib_destroy_qp(qp
);
2388 put_uobj_write(uobj
);
2394 atomic_dec(&obj
->uxrcd
->refcnt
);
2396 idr_remove_uobj(&ib_uverbs_qp_idr
, uobj
);
2398 mutex_lock(&file
->mutex
);
2399 list_del(&uobj
->list
);
2400 mutex_unlock(&file
->mutex
);
2402 ib_uverbs_release_uevent(file
, &obj
->uevent
);
2404 resp
.events_reported
= obj
->uevent
.events_reported
;
2408 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2409 &resp
, sizeof resp
))
2415 static void *alloc_wr(size_t wr_size
, __u32 num_sge
)
2417 return kmalloc(ALIGN(wr_size
, sizeof (struct ib_sge
)) +
2418 num_sge
* sizeof (struct ib_sge
), GFP_KERNEL
);
2421 ssize_t
ib_uverbs_post_send(struct ib_uverbs_file
*file
,
2422 struct ib_device
*ib_dev
,
2423 const char __user
*buf
, int in_len
,
2426 struct ib_uverbs_post_send cmd
;
2427 struct ib_uverbs_post_send_resp resp
;
2428 struct ib_uverbs_send_wr
*user_wr
;
2429 struct ib_send_wr
*wr
= NULL
, *last
, *next
, *bad_wr
;
2433 ssize_t ret
= -EINVAL
;
2436 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2439 if (in_len
< sizeof cmd
+ cmd
.wqe_size
* cmd
.wr_count
+
2440 cmd
.sge_count
* sizeof (struct ib_uverbs_sge
))
2443 if (cmd
.wqe_size
< sizeof (struct ib_uverbs_send_wr
))
2446 user_wr
= kmalloc(cmd
.wqe_size
, GFP_KERNEL
);
2450 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2454 is_ud
= qp
->qp_type
== IB_QPT_UD
;
2457 for (i
= 0; i
< cmd
.wr_count
; ++i
) {
2458 if (copy_from_user(user_wr
,
2459 buf
+ sizeof cmd
+ i
* cmd
.wqe_size
,
2465 if (user_wr
->num_sge
+ sg_ind
> cmd
.sge_count
) {
2471 struct ib_ud_wr
*ud
;
2473 if (user_wr
->opcode
!= IB_WR_SEND
&&
2474 user_wr
->opcode
!= IB_WR_SEND_WITH_IMM
) {
2479 next_size
= sizeof(*ud
);
2480 ud
= alloc_wr(next_size
, user_wr
->num_sge
);
2486 ud
->ah
= idr_read_ah(user_wr
->wr
.ud
.ah
, file
->ucontext
);
2492 ud
->remote_qpn
= user_wr
->wr
.ud
.remote_qpn
;
2493 ud
->remote_qkey
= user_wr
->wr
.ud
.remote_qkey
;
2496 } else if (user_wr
->opcode
== IB_WR_RDMA_WRITE_WITH_IMM
||
2497 user_wr
->opcode
== IB_WR_RDMA_WRITE
||
2498 user_wr
->opcode
== IB_WR_RDMA_READ
) {
2499 struct ib_rdma_wr
*rdma
;
2501 next_size
= sizeof(*rdma
);
2502 rdma
= alloc_wr(next_size
, user_wr
->num_sge
);
2508 rdma
->remote_addr
= user_wr
->wr
.rdma
.remote_addr
;
2509 rdma
->rkey
= user_wr
->wr
.rdma
.rkey
;
2512 } else if (user_wr
->opcode
== IB_WR_ATOMIC_CMP_AND_SWP
||
2513 user_wr
->opcode
== IB_WR_ATOMIC_FETCH_AND_ADD
) {
2514 struct ib_atomic_wr
*atomic
;
2516 next_size
= sizeof(*atomic
);
2517 atomic
= alloc_wr(next_size
, user_wr
->num_sge
);
2523 atomic
->remote_addr
= user_wr
->wr
.atomic
.remote_addr
;
2524 atomic
->compare_add
= user_wr
->wr
.atomic
.compare_add
;
2525 atomic
->swap
= user_wr
->wr
.atomic
.swap
;
2526 atomic
->rkey
= user_wr
->wr
.atomic
.rkey
;
2529 } else if (user_wr
->opcode
== IB_WR_SEND
||
2530 user_wr
->opcode
== IB_WR_SEND_WITH_IMM
||
2531 user_wr
->opcode
== IB_WR_SEND_WITH_INV
) {
2532 next_size
= sizeof(*next
);
2533 next
= alloc_wr(next_size
, user_wr
->num_sge
);
2543 if (user_wr
->opcode
== IB_WR_SEND_WITH_IMM
||
2544 user_wr
->opcode
== IB_WR_RDMA_WRITE_WITH_IMM
) {
2546 (__be32 __force
) user_wr
->ex
.imm_data
;
2547 } else if (user_wr
->opcode
== IB_WR_SEND_WITH_INV
) {
2548 next
->ex
.invalidate_rkey
= user_wr
->ex
.invalidate_rkey
;
2558 next
->wr_id
= user_wr
->wr_id
;
2559 next
->num_sge
= user_wr
->num_sge
;
2560 next
->opcode
= user_wr
->opcode
;
2561 next
->send_flags
= user_wr
->send_flags
;
2563 if (next
->num_sge
) {
2564 next
->sg_list
= (void *) next
+
2565 ALIGN(next_size
, sizeof(struct ib_sge
));
2566 if (copy_from_user(next
->sg_list
,
2568 cmd
.wr_count
* cmd
.wqe_size
+
2569 sg_ind
* sizeof (struct ib_sge
),
2570 next
->num_sge
* sizeof (struct ib_sge
))) {
2574 sg_ind
+= next
->num_sge
;
2576 next
->sg_list
= NULL
;
2580 ret
= qp
->device
->post_send(qp
->real_qp
, wr
, &bad_wr
);
2582 for (next
= wr
; next
; next
= next
->next
) {
2588 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2589 &resp
, sizeof resp
))
2596 if (is_ud
&& ud_wr(wr
)->ah
)
2597 put_ah_read(ud_wr(wr
)->ah
);
2606 return ret
? ret
: in_len
;
2609 static struct ib_recv_wr
*ib_uverbs_unmarshall_recv(const char __user
*buf
,
2615 struct ib_uverbs_recv_wr
*user_wr
;
2616 struct ib_recv_wr
*wr
= NULL
, *last
, *next
;
2621 if (in_len
< wqe_size
* wr_count
+
2622 sge_count
* sizeof (struct ib_uverbs_sge
))
2623 return ERR_PTR(-EINVAL
);
2625 if (wqe_size
< sizeof (struct ib_uverbs_recv_wr
))
2626 return ERR_PTR(-EINVAL
);
2628 user_wr
= kmalloc(wqe_size
, GFP_KERNEL
);
2630 return ERR_PTR(-ENOMEM
);
2634 for (i
= 0; i
< wr_count
; ++i
) {
2635 if (copy_from_user(user_wr
, buf
+ i
* wqe_size
,
2641 if (user_wr
->num_sge
+ sg_ind
> sge_count
) {
2646 next
= kmalloc(ALIGN(sizeof *next
, sizeof (struct ib_sge
)) +
2647 user_wr
->num_sge
* sizeof (struct ib_sge
),
2661 next
->wr_id
= user_wr
->wr_id
;
2662 next
->num_sge
= user_wr
->num_sge
;
2664 if (next
->num_sge
) {
2665 next
->sg_list
= (void *) next
+
2666 ALIGN(sizeof *next
, sizeof (struct ib_sge
));
2667 if (copy_from_user(next
->sg_list
,
2668 buf
+ wr_count
* wqe_size
+
2669 sg_ind
* sizeof (struct ib_sge
),
2670 next
->num_sge
* sizeof (struct ib_sge
))) {
2674 sg_ind
+= next
->num_sge
;
2676 next
->sg_list
= NULL
;
2691 return ERR_PTR(ret
);
2694 ssize_t
ib_uverbs_post_recv(struct ib_uverbs_file
*file
,
2695 struct ib_device
*ib_dev
,
2696 const char __user
*buf
, int in_len
,
2699 struct ib_uverbs_post_recv cmd
;
2700 struct ib_uverbs_post_recv_resp resp
;
2701 struct ib_recv_wr
*wr
, *next
, *bad_wr
;
2703 ssize_t ret
= -EINVAL
;
2705 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2708 wr
= ib_uverbs_unmarshall_recv(buf
+ sizeof cmd
,
2709 in_len
- sizeof cmd
, cmd
.wr_count
,
2710 cmd
.sge_count
, cmd
.wqe_size
);
2714 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
2719 ret
= qp
->device
->post_recv(qp
->real_qp
, wr
, &bad_wr
);
2724 for (next
= wr
; next
; next
= next
->next
) {
2730 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2731 &resp
, sizeof resp
))
2741 return ret
? ret
: in_len
;
2744 ssize_t
ib_uverbs_post_srq_recv(struct ib_uverbs_file
*file
,
2745 struct ib_device
*ib_dev
,
2746 const char __user
*buf
, int in_len
,
2749 struct ib_uverbs_post_srq_recv cmd
;
2750 struct ib_uverbs_post_srq_recv_resp resp
;
2751 struct ib_recv_wr
*wr
, *next
, *bad_wr
;
2753 ssize_t ret
= -EINVAL
;
2755 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2758 wr
= ib_uverbs_unmarshall_recv(buf
+ sizeof cmd
,
2759 in_len
- sizeof cmd
, cmd
.wr_count
,
2760 cmd
.sge_count
, cmd
.wqe_size
);
2764 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
2769 ret
= srq
->device
->post_srq_recv(srq
, wr
, &bad_wr
);
2774 for (next
= wr
; next
; next
= next
->next
) {
2780 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2781 &resp
, sizeof resp
))
2791 return ret
? ret
: in_len
;
2794 ssize_t
ib_uverbs_create_ah(struct ib_uverbs_file
*file
,
2795 struct ib_device
*ib_dev
,
2796 const char __user
*buf
, int in_len
,
2799 struct ib_uverbs_create_ah cmd
;
2800 struct ib_uverbs_create_ah_resp resp
;
2801 struct ib_uobject
*uobj
;
2804 struct ib_ah_attr attr
;
2807 if (out_len
< sizeof resp
)
2810 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2813 uobj
= kmalloc(sizeof *uobj
, GFP_KERNEL
);
2817 init_uobj(uobj
, cmd
.user_handle
, file
->ucontext
, &ah_lock_class
);
2818 down_write(&uobj
->mutex
);
2820 pd
= idr_read_pd(cmd
.pd_handle
, file
->ucontext
);
2826 attr
.dlid
= cmd
.attr
.dlid
;
2827 attr
.sl
= cmd
.attr
.sl
;
2828 attr
.src_path_bits
= cmd
.attr
.src_path_bits
;
2829 attr
.static_rate
= cmd
.attr
.static_rate
;
2830 attr
.ah_flags
= cmd
.attr
.is_global
? IB_AH_GRH
: 0;
2831 attr
.port_num
= cmd
.attr
.port_num
;
2832 attr
.grh
.flow_label
= cmd
.attr
.grh
.flow_label
;
2833 attr
.grh
.sgid_index
= cmd
.attr
.grh
.sgid_index
;
2834 attr
.grh
.hop_limit
= cmd
.attr
.grh
.hop_limit
;
2835 attr
.grh
.traffic_class
= cmd
.attr
.grh
.traffic_class
;
2836 memset(&attr
.dmac
, 0, sizeof(attr
.dmac
));
2837 memcpy(attr
.grh
.dgid
.raw
, cmd
.attr
.grh
.dgid
, 16);
2839 ah
= ib_create_ah(pd
, &attr
);
2848 ret
= idr_add_uobj(&ib_uverbs_ah_idr
, uobj
);
2852 resp
.ah_handle
= uobj
->id
;
2854 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
2855 &resp
, sizeof resp
)) {
2862 mutex_lock(&file
->mutex
);
2863 list_add_tail(&uobj
->list
, &file
->ucontext
->ah_list
);
2864 mutex_unlock(&file
->mutex
);
2868 up_write(&uobj
->mutex
);
2873 idr_remove_uobj(&ib_uverbs_ah_idr
, uobj
);
2882 put_uobj_write(uobj
);
2886 ssize_t
ib_uverbs_destroy_ah(struct ib_uverbs_file
*file
,
2887 struct ib_device
*ib_dev
,
2888 const char __user
*buf
, int in_len
, int out_len
)
2890 struct ib_uverbs_destroy_ah cmd
;
2892 struct ib_uobject
*uobj
;
2895 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2898 uobj
= idr_write_uobj(&ib_uverbs_ah_idr
, cmd
.ah_handle
, file
->ucontext
);
2903 ret
= ib_destroy_ah(ah
);
2907 put_uobj_write(uobj
);
2912 idr_remove_uobj(&ib_uverbs_ah_idr
, uobj
);
2914 mutex_lock(&file
->mutex
);
2915 list_del(&uobj
->list
);
2916 mutex_unlock(&file
->mutex
);
2923 ssize_t
ib_uverbs_attach_mcast(struct ib_uverbs_file
*file
,
2924 struct ib_device
*ib_dev
,
2925 const char __user
*buf
, int in_len
,
2928 struct ib_uverbs_attach_mcast cmd
;
2930 struct ib_uqp_object
*obj
;
2931 struct ib_uverbs_mcast_entry
*mcast
;
2934 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2937 qp
= idr_write_qp(cmd
.qp_handle
, file
->ucontext
);
2941 obj
= container_of(qp
->uobject
, struct ib_uqp_object
, uevent
.uobject
);
2943 list_for_each_entry(mcast
, &obj
->mcast_list
, list
)
2944 if (cmd
.mlid
== mcast
->lid
&&
2945 !memcmp(cmd
.gid
, mcast
->gid
.raw
, sizeof mcast
->gid
.raw
)) {
2950 mcast
= kmalloc(sizeof *mcast
, GFP_KERNEL
);
2956 mcast
->lid
= cmd
.mlid
;
2957 memcpy(mcast
->gid
.raw
, cmd
.gid
, sizeof mcast
->gid
.raw
);
2959 ret
= ib_attach_mcast(qp
, &mcast
->gid
, cmd
.mlid
);
2961 list_add_tail(&mcast
->list
, &obj
->mcast_list
);
2968 return ret
? ret
: in_len
;
2971 ssize_t
ib_uverbs_detach_mcast(struct ib_uverbs_file
*file
,
2972 struct ib_device
*ib_dev
,
2973 const char __user
*buf
, int in_len
,
2976 struct ib_uverbs_detach_mcast cmd
;
2977 struct ib_uqp_object
*obj
;
2979 struct ib_uverbs_mcast_entry
*mcast
;
2982 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
2985 qp
= idr_write_qp(cmd
.qp_handle
, file
->ucontext
);
2989 ret
= ib_detach_mcast(qp
, (union ib_gid
*) cmd
.gid
, cmd
.mlid
);
2993 obj
= container_of(qp
->uobject
, struct ib_uqp_object
, uevent
.uobject
);
2995 list_for_each_entry(mcast
, &obj
->mcast_list
, list
)
2996 if (cmd
.mlid
== mcast
->lid
&&
2997 !memcmp(cmd
.gid
, mcast
->gid
.raw
, sizeof mcast
->gid
.raw
)) {
2998 list_del(&mcast
->list
);
3006 return ret
? ret
: in_len
;
3009 static int kern_spec_to_ib_spec(struct ib_uverbs_flow_spec
*kern_spec
,
3010 union ib_flow_spec
*ib_spec
)
3012 if (kern_spec
->reserved
)
3015 ib_spec
->type
= kern_spec
->type
;
3017 switch (ib_spec
->type
) {
3018 case IB_FLOW_SPEC_ETH
:
3019 ib_spec
->eth
.size
= sizeof(struct ib_flow_spec_eth
);
3020 if (ib_spec
->eth
.size
!= kern_spec
->eth
.size
)
3022 memcpy(&ib_spec
->eth
.val
, &kern_spec
->eth
.val
,
3023 sizeof(struct ib_flow_eth_filter
));
3024 memcpy(&ib_spec
->eth
.mask
, &kern_spec
->eth
.mask
,
3025 sizeof(struct ib_flow_eth_filter
));
3027 case IB_FLOW_SPEC_IPV4
:
3028 ib_spec
->ipv4
.size
= sizeof(struct ib_flow_spec_ipv4
);
3029 if (ib_spec
->ipv4
.size
!= kern_spec
->ipv4
.size
)
3031 memcpy(&ib_spec
->ipv4
.val
, &kern_spec
->ipv4
.val
,
3032 sizeof(struct ib_flow_ipv4_filter
));
3033 memcpy(&ib_spec
->ipv4
.mask
, &kern_spec
->ipv4
.mask
,
3034 sizeof(struct ib_flow_ipv4_filter
));
3036 case IB_FLOW_SPEC_TCP
:
3037 case IB_FLOW_SPEC_UDP
:
3038 ib_spec
->tcp_udp
.size
= sizeof(struct ib_flow_spec_tcp_udp
);
3039 if (ib_spec
->tcp_udp
.size
!= kern_spec
->tcp_udp
.size
)
3041 memcpy(&ib_spec
->tcp_udp
.val
, &kern_spec
->tcp_udp
.val
,
3042 sizeof(struct ib_flow_tcp_udp_filter
));
3043 memcpy(&ib_spec
->tcp_udp
.mask
, &kern_spec
->tcp_udp
.mask
,
3044 sizeof(struct ib_flow_tcp_udp_filter
));
3052 int ib_uverbs_ex_create_flow(struct ib_uverbs_file
*file
,
3053 struct ib_device
*ib_dev
,
3054 struct ib_udata
*ucore
,
3055 struct ib_udata
*uhw
)
3057 struct ib_uverbs_create_flow cmd
;
3058 struct ib_uverbs_create_flow_resp resp
;
3059 struct ib_uobject
*uobj
;
3060 struct ib_flow
*flow_id
;
3061 struct ib_uverbs_flow_attr
*kern_flow_attr
;
3062 struct ib_flow_attr
*flow_attr
;
3069 if (ucore
->inlen
< sizeof(cmd
))
3072 if (ucore
->outlen
< sizeof(resp
))
3075 err
= ib_copy_from_udata(&cmd
, ucore
, sizeof(cmd
));
3079 ucore
->inbuf
+= sizeof(cmd
);
3080 ucore
->inlen
-= sizeof(cmd
);
3085 if ((cmd
.flow_attr
.type
== IB_FLOW_ATTR_SNIFFER
&&
3086 !capable(CAP_NET_ADMIN
)) || !capable(CAP_NET_RAW
))
3089 if (cmd
.flow_attr
.num_of_specs
> IB_FLOW_SPEC_SUPPORT_LAYERS
)
3092 if (cmd
.flow_attr
.size
> ucore
->inlen
||
3093 cmd
.flow_attr
.size
>
3094 (cmd
.flow_attr
.num_of_specs
* sizeof(struct ib_uverbs_flow_spec
)))
3097 if (cmd
.flow_attr
.reserved
[0] ||
3098 cmd
.flow_attr
.reserved
[1])
3101 if (cmd
.flow_attr
.num_of_specs
) {
3102 kern_flow_attr
= kmalloc(sizeof(*kern_flow_attr
) + cmd
.flow_attr
.size
,
3104 if (!kern_flow_attr
)
3107 memcpy(kern_flow_attr
, &cmd
.flow_attr
, sizeof(*kern_flow_attr
));
3108 err
= ib_copy_from_udata(kern_flow_attr
+ 1, ucore
,
3109 cmd
.flow_attr
.size
);
3113 kern_flow_attr
= &cmd
.flow_attr
;
3116 uobj
= kmalloc(sizeof(*uobj
), GFP_KERNEL
);
3121 init_uobj(uobj
, 0, file
->ucontext
, &rule_lock_class
);
3122 down_write(&uobj
->mutex
);
3124 qp
= idr_read_qp(cmd
.qp_handle
, file
->ucontext
);
3130 flow_attr
= kmalloc(sizeof(*flow_attr
) + cmd
.flow_attr
.size
, GFP_KERNEL
);
3136 flow_attr
->type
= kern_flow_attr
->type
;
3137 flow_attr
->priority
= kern_flow_attr
->priority
;
3138 flow_attr
->num_of_specs
= kern_flow_attr
->num_of_specs
;
3139 flow_attr
->port
= kern_flow_attr
->port
;
3140 flow_attr
->flags
= kern_flow_attr
->flags
;
3141 flow_attr
->size
= sizeof(*flow_attr
);
3143 kern_spec
= kern_flow_attr
+ 1;
3144 ib_spec
= flow_attr
+ 1;
3145 for (i
= 0; i
< flow_attr
->num_of_specs
&&
3146 cmd
.flow_attr
.size
> offsetof(struct ib_uverbs_flow_spec
, reserved
) &&
3147 cmd
.flow_attr
.size
>=
3148 ((struct ib_uverbs_flow_spec
*)kern_spec
)->size
; i
++) {
3149 err
= kern_spec_to_ib_spec(kern_spec
, ib_spec
);
3153 ((union ib_flow_spec
*) ib_spec
)->size
;
3154 cmd
.flow_attr
.size
-= ((struct ib_uverbs_flow_spec
*)kern_spec
)->size
;
3155 kern_spec
+= ((struct ib_uverbs_flow_spec
*) kern_spec
)->size
;
3156 ib_spec
+= ((union ib_flow_spec
*) ib_spec
)->size
;
3158 if (cmd
.flow_attr
.size
|| (i
!= flow_attr
->num_of_specs
)) {
3159 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3160 i
, cmd
.flow_attr
.size
);
3164 flow_id
= ib_create_flow(qp
, flow_attr
, IB_FLOW_DOMAIN_USER
);
3165 if (IS_ERR(flow_id
)) {
3166 err
= PTR_ERR(flow_id
);
3170 flow_id
->uobject
= uobj
;
3171 uobj
->object
= flow_id
;
3173 err
= idr_add_uobj(&ib_uverbs_rule_idr
, uobj
);
3177 memset(&resp
, 0, sizeof(resp
));
3178 resp
.flow_handle
= uobj
->id
;
3180 err
= ib_copy_to_udata(ucore
,
3181 &resp
, sizeof(resp
));
3186 mutex_lock(&file
->mutex
);
3187 list_add_tail(&uobj
->list
, &file
->ucontext
->rule_list
);
3188 mutex_unlock(&file
->mutex
);
3192 up_write(&uobj
->mutex
);
3194 if (cmd
.flow_attr
.num_of_specs
)
3195 kfree(kern_flow_attr
);
3198 idr_remove_uobj(&ib_uverbs_rule_idr
, uobj
);
3200 ib_destroy_flow(flow_id
);
3206 put_uobj_write(uobj
);
3208 if (cmd
.flow_attr
.num_of_specs
)
3209 kfree(kern_flow_attr
);
3213 int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file
*file
,
3214 struct ib_device
*ib_dev
,
3215 struct ib_udata
*ucore
,
3216 struct ib_udata
*uhw
)
3218 struct ib_uverbs_destroy_flow cmd
;
3219 struct ib_flow
*flow_id
;
3220 struct ib_uobject
*uobj
;
3223 if (ucore
->inlen
< sizeof(cmd
))
3226 ret
= ib_copy_from_udata(&cmd
, ucore
, sizeof(cmd
));
3233 uobj
= idr_write_uobj(&ib_uverbs_rule_idr
, cmd
.flow_handle
,
3237 flow_id
= uobj
->object
;
3239 ret
= ib_destroy_flow(flow_id
);
3243 put_uobj_write(uobj
);
3245 idr_remove_uobj(&ib_uverbs_rule_idr
, uobj
);
3247 mutex_lock(&file
->mutex
);
3248 list_del(&uobj
->list
);
3249 mutex_unlock(&file
->mutex
);
3256 static int __uverbs_create_xsrq(struct ib_uverbs_file
*file
,
3257 struct ib_device
*ib_dev
,
3258 struct ib_uverbs_create_xsrq
*cmd
,
3259 struct ib_udata
*udata
)
3261 struct ib_uverbs_create_srq_resp resp
;
3262 struct ib_usrq_object
*obj
;
3265 struct ib_uobject
*uninitialized_var(xrcd_uobj
);
3266 struct ib_srq_init_attr attr
;
3269 obj
= kmalloc(sizeof *obj
, GFP_KERNEL
);
3273 init_uobj(&obj
->uevent
.uobject
, cmd
->user_handle
, file
->ucontext
, &srq_lock_class
);
3274 down_write(&obj
->uevent
.uobject
.mutex
);
3276 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3277 attr
.ext
.xrc
.xrcd
= idr_read_xrcd(cmd
->xrcd_handle
, file
->ucontext
, &xrcd_uobj
);
3278 if (!attr
.ext
.xrc
.xrcd
) {
3283 obj
->uxrcd
= container_of(xrcd_uobj
, struct ib_uxrcd_object
, uobject
);
3284 atomic_inc(&obj
->uxrcd
->refcnt
);
3286 attr
.ext
.xrc
.cq
= idr_read_cq(cmd
->cq_handle
, file
->ucontext
, 0);
3287 if (!attr
.ext
.xrc
.cq
) {
3293 pd
= idr_read_pd(cmd
->pd_handle
, file
->ucontext
);
3299 attr
.event_handler
= ib_uverbs_srq_event_handler
;
3300 attr
.srq_context
= file
;
3301 attr
.srq_type
= cmd
->srq_type
;
3302 attr
.attr
.max_wr
= cmd
->max_wr
;
3303 attr
.attr
.max_sge
= cmd
->max_sge
;
3304 attr
.attr
.srq_limit
= cmd
->srq_limit
;
3306 obj
->uevent
.events_reported
= 0;
3307 INIT_LIST_HEAD(&obj
->uevent
.event_list
);
3309 srq
= pd
->device
->create_srq(pd
, &attr
, udata
);
3315 srq
->device
= pd
->device
;
3317 srq
->srq_type
= cmd
->srq_type
;
3318 srq
->uobject
= &obj
->uevent
.uobject
;
3319 srq
->event_handler
= attr
.event_handler
;
3320 srq
->srq_context
= attr
.srq_context
;
3322 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3323 srq
->ext
.xrc
.cq
= attr
.ext
.xrc
.cq
;
3324 srq
->ext
.xrc
.xrcd
= attr
.ext
.xrc
.xrcd
;
3325 atomic_inc(&attr
.ext
.xrc
.cq
->usecnt
);
3326 atomic_inc(&attr
.ext
.xrc
.xrcd
->usecnt
);
3329 atomic_inc(&pd
->usecnt
);
3330 atomic_set(&srq
->usecnt
, 0);
3332 obj
->uevent
.uobject
.object
= srq
;
3333 ret
= idr_add_uobj(&ib_uverbs_srq_idr
, &obj
->uevent
.uobject
);
3337 memset(&resp
, 0, sizeof resp
);
3338 resp
.srq_handle
= obj
->uevent
.uobject
.id
;
3339 resp
.max_wr
= attr
.attr
.max_wr
;
3340 resp
.max_sge
= attr
.attr
.max_sge
;
3341 if (cmd
->srq_type
== IB_SRQT_XRC
)
3342 resp
.srqn
= srq
->ext
.xrc
.srq_num
;
3344 if (copy_to_user((void __user
*) (unsigned long) cmd
->response
,
3345 &resp
, sizeof resp
)) {
3350 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3351 put_uobj_read(xrcd_uobj
);
3352 put_cq_read(attr
.ext
.xrc
.cq
);
3356 mutex_lock(&file
->mutex
);
3357 list_add_tail(&obj
->uevent
.uobject
.list
, &file
->ucontext
->srq_list
);
3358 mutex_unlock(&file
->mutex
);
3360 obj
->uevent
.uobject
.live
= 1;
3362 up_write(&obj
->uevent
.uobject
.mutex
);
3367 idr_remove_uobj(&ib_uverbs_srq_idr
, &obj
->uevent
.uobject
);
3370 ib_destroy_srq(srq
);
3376 if (cmd
->srq_type
== IB_SRQT_XRC
)
3377 put_cq_read(attr
.ext
.xrc
.cq
);
3380 if (cmd
->srq_type
== IB_SRQT_XRC
) {
3381 atomic_dec(&obj
->uxrcd
->refcnt
);
3382 put_uobj_read(xrcd_uobj
);
3386 put_uobj_write(&obj
->uevent
.uobject
);
3390 ssize_t
ib_uverbs_create_srq(struct ib_uverbs_file
*file
,
3391 struct ib_device
*ib_dev
,
3392 const char __user
*buf
, int in_len
,
3395 struct ib_uverbs_create_srq cmd
;
3396 struct ib_uverbs_create_xsrq xcmd
;
3397 struct ib_uverbs_create_srq_resp resp
;
3398 struct ib_udata udata
;
3401 if (out_len
< sizeof resp
)
3404 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3407 xcmd
.response
= cmd
.response
;
3408 xcmd
.user_handle
= cmd
.user_handle
;
3409 xcmd
.srq_type
= IB_SRQT_BASIC
;
3410 xcmd
.pd_handle
= cmd
.pd_handle
;
3411 xcmd
.max_wr
= cmd
.max_wr
;
3412 xcmd
.max_sge
= cmd
.max_sge
;
3413 xcmd
.srq_limit
= cmd
.srq_limit
;
3415 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
3416 (unsigned long) cmd
.response
+ sizeof resp
,
3417 in_len
- sizeof cmd
- sizeof(struct ib_uverbs_cmd_hdr
),
3418 out_len
- sizeof resp
);
3420 ret
= __uverbs_create_xsrq(file
, ib_dev
, &xcmd
, &udata
);
3427 ssize_t
ib_uverbs_create_xsrq(struct ib_uverbs_file
*file
,
3428 struct ib_device
*ib_dev
,
3429 const char __user
*buf
, int in_len
, int out_len
)
3431 struct ib_uverbs_create_xsrq cmd
;
3432 struct ib_uverbs_create_srq_resp resp
;
3433 struct ib_udata udata
;
3436 if (out_len
< sizeof resp
)
3439 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3442 INIT_UDATA(&udata
, buf
+ sizeof cmd
,
3443 (unsigned long) cmd
.response
+ sizeof resp
,
3444 in_len
- sizeof cmd
- sizeof(struct ib_uverbs_cmd_hdr
),
3445 out_len
- sizeof resp
);
3447 ret
= __uverbs_create_xsrq(file
, ib_dev
, &cmd
, &udata
);
3454 ssize_t
ib_uverbs_modify_srq(struct ib_uverbs_file
*file
,
3455 struct ib_device
*ib_dev
,
3456 const char __user
*buf
, int in_len
,
3459 struct ib_uverbs_modify_srq cmd
;
3460 struct ib_udata udata
;
3462 struct ib_srq_attr attr
;
3465 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3468 INIT_UDATA(&udata
, buf
+ sizeof cmd
, NULL
, in_len
- sizeof cmd
,
3471 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
3475 attr
.max_wr
= cmd
.max_wr
;
3476 attr
.srq_limit
= cmd
.srq_limit
;
3478 ret
= srq
->device
->modify_srq(srq
, &attr
, cmd
.attr_mask
, &udata
);
3482 return ret
? ret
: in_len
;
3485 ssize_t
ib_uverbs_query_srq(struct ib_uverbs_file
*file
,
3486 struct ib_device
*ib_dev
,
3487 const char __user
*buf
,
3488 int in_len
, int out_len
)
3490 struct ib_uverbs_query_srq cmd
;
3491 struct ib_uverbs_query_srq_resp resp
;
3492 struct ib_srq_attr attr
;
3496 if (out_len
< sizeof resp
)
3499 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3502 srq
= idr_read_srq(cmd
.srq_handle
, file
->ucontext
);
3506 ret
= ib_query_srq(srq
, &attr
);
3513 memset(&resp
, 0, sizeof resp
);
3515 resp
.max_wr
= attr
.max_wr
;
3516 resp
.max_sge
= attr
.max_sge
;
3517 resp
.srq_limit
= attr
.srq_limit
;
3519 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
3520 &resp
, sizeof resp
))
3526 ssize_t
ib_uverbs_destroy_srq(struct ib_uverbs_file
*file
,
3527 struct ib_device
*ib_dev
,
3528 const char __user
*buf
, int in_len
,
3531 struct ib_uverbs_destroy_srq cmd
;
3532 struct ib_uverbs_destroy_srq_resp resp
;
3533 struct ib_uobject
*uobj
;
3535 struct ib_uevent_object
*obj
;
3537 struct ib_usrq_object
*us
;
3538 enum ib_srq_type srq_type
;
3540 if (copy_from_user(&cmd
, buf
, sizeof cmd
))
3543 uobj
= idr_write_uobj(&ib_uverbs_srq_idr
, cmd
.srq_handle
, file
->ucontext
);
3547 obj
= container_of(uobj
, struct ib_uevent_object
, uobject
);
3548 srq_type
= srq
->srq_type
;
3550 ret
= ib_destroy_srq(srq
);
3554 put_uobj_write(uobj
);
3559 if (srq_type
== IB_SRQT_XRC
) {
3560 us
= container_of(obj
, struct ib_usrq_object
, uevent
);
3561 atomic_dec(&us
->uxrcd
->refcnt
);
3564 idr_remove_uobj(&ib_uverbs_srq_idr
, uobj
);
3566 mutex_lock(&file
->mutex
);
3567 list_del(&uobj
->list
);
3568 mutex_unlock(&file
->mutex
);
3570 ib_uverbs_release_uevent(file
, obj
);
3572 memset(&resp
, 0, sizeof resp
);
3573 resp
.events_reported
= obj
->events_reported
;
3577 if (copy_to_user((void __user
*) (unsigned long) cmd
.response
,
3578 &resp
, sizeof resp
))
3581 return ret
? ret
: in_len
;
3584 int ib_uverbs_ex_query_device(struct ib_uverbs_file
*file
,
3585 struct ib_device
*ib_dev
,
3586 struct ib_udata
*ucore
,
3587 struct ib_udata
*uhw
)
3589 struct ib_uverbs_ex_query_device_resp resp
;
3590 struct ib_uverbs_ex_query_device cmd
;
3591 struct ib_device_attr attr
;
3594 if (ucore
->inlen
< sizeof(cmd
))
3597 err
= ib_copy_from_udata(&cmd
, ucore
, sizeof(cmd
));
3607 resp
.response_length
= offsetof(typeof(resp
), odp_caps
);
3609 if (ucore
->outlen
< resp
.response_length
)
3612 memset(&attr
, 0, sizeof(attr
));
3614 err
= ib_dev
->query_device(ib_dev
, &attr
, uhw
);
3618 copy_query_dev_fields(file
, ib_dev
, &resp
.base
, &attr
);
3621 if (ucore
->outlen
< resp
.response_length
+ sizeof(resp
.odp_caps
))
3624 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3625 resp
.odp_caps
.general_caps
= attr
.odp_caps
.general_caps
;
3626 resp
.odp_caps
.per_transport_caps
.rc_odp_caps
=
3627 attr
.odp_caps
.per_transport_caps
.rc_odp_caps
;
3628 resp
.odp_caps
.per_transport_caps
.uc_odp_caps
=
3629 attr
.odp_caps
.per_transport_caps
.uc_odp_caps
;
3630 resp
.odp_caps
.per_transport_caps
.ud_odp_caps
=
3631 attr
.odp_caps
.per_transport_caps
.ud_odp_caps
;
3632 resp
.odp_caps
.reserved
= 0;
3634 memset(&resp
.odp_caps
, 0, sizeof(resp
.odp_caps
));
3636 resp
.response_length
+= sizeof(resp
.odp_caps
);
3638 if (ucore
->outlen
< resp
.response_length
+ sizeof(resp
.timestamp_mask
))
3641 resp
.timestamp_mask
= attr
.timestamp_mask
;
3642 resp
.response_length
+= sizeof(resp
.timestamp_mask
);
3644 if (ucore
->outlen
< resp
.response_length
+ sizeof(resp
.hca_core_clock
))
3647 resp
.hca_core_clock
= attr
.hca_core_clock
;
3648 resp
.response_length
+= sizeof(resp
.hca_core_clock
);
3651 err
= ib_copy_to_udata(ucore
, &resp
, resp
.response_length
);