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/lockdep.h>
40 #include <asm/uaccess.h>
41 #include <asm/fcntl.h>
45 static struct lock_class_key pd_lock_key;
46 static struct lock_class_key mr_lock_key;
47 static struct lock_class_key cq_lock_key;
48 static struct lock_class_key qp_lock_key;
49 static struct lock_class_key ah_lock_key;
50 static struct lock_class_key srq_lock_key;
52 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
54 (udata)->inbuf = (void __user *) (ibuf); \
55 (udata)->outbuf = (void __user *) (obuf); \
56 (udata)->inlen = (ilen); \
57 (udata)->outlen = (olen); \
61 * The ib_uobject locking scheme is as follows:
63 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
64 * needs to be held during all idr operations. When an object is
65 * looked up, a reference must be taken on the object's kref before
68 * - Each object also has an rwsem. This rwsem must be held for
69 * reading while an operation that uses the object is performed.
70 * For example, while registering an MR, the associated PD's
71 * uobject.mutex must be held for reading. The rwsem must be held
72 * for writing while initializing or destroying an object.
74 * - In addition, each object has a "live" flag. If this flag is not
75 * set, then lookups of the object will fail even if it is found in
76 * the idr. This handles a reader that blocks and does not acquire
77 * the rwsem until after the object is destroyed. The destroy
78 * operation will set the live flag to 0 and then drop the rwsem;
79 * this will allow the reader to acquire the rwsem, see that the
80 * live flag is 0, and then drop the rwsem and its reference to
81 * object. The underlying storage will not be freed until the last
82 * reference to the object is dropped.
85 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
86 struct ib_ucontext *context, struct lock_class_key *key)
88 uobj->user_handle = user_handle;
89 uobj->context = context;
90 kref_init(&uobj->ref);
91 init_rwsem(&uobj->mutex);
92 lockdep_set_class(&uobj->mutex, key);
96 static void release_uobj(struct kref *kref)
98 kfree(container_of(kref, struct ib_uobject, ref));
101 static void put_uobj(struct ib_uobject *uobj)
103 kref_put(&uobj->ref, release_uobj);
106 static void put_uobj_read(struct ib_uobject *uobj)
108 up_read(&uobj->mutex);
112 static void put_uobj_write(struct ib_uobject *uobj)
114 up_write(&uobj->mutex);
118 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
123 if (!idr_pre_get(idr, GFP_KERNEL))
126 spin_lock(&ib_uverbs_idr_lock);
127 ret = idr_get_new(idr, uobj, &uobj->id);
128 spin_unlock(&ib_uverbs_idr_lock);
136 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
138 spin_lock(&ib_uverbs_idr_lock);
139 idr_remove(idr, uobj->id);
140 spin_unlock(&ib_uverbs_idr_lock);
143 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
144 struct ib_ucontext *context)
146 struct ib_uobject *uobj;
148 spin_lock(&ib_uverbs_idr_lock);
149 uobj = idr_find(idr, id);
151 if (uobj->context == context)
152 kref_get(&uobj->ref);
156 spin_unlock(&ib_uverbs_idr_lock);
161 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
162 struct ib_ucontext *context, int nested)
164 struct ib_uobject *uobj;
166 uobj = __idr_get_uobj(idr, id, context);
171 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
173 down_read(&uobj->mutex);
182 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
183 struct ib_ucontext *context)
185 struct ib_uobject *uobj;
187 uobj = __idr_get_uobj(idr, id, context);
191 down_write(&uobj->mutex);
193 put_uobj_write(uobj);
200 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
203 struct ib_uobject *uobj;
205 uobj = idr_read_uobj(idr, id, context, nested);
206 return uobj ? uobj->object : NULL;
209 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
211 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
214 static void put_pd_read(struct ib_pd *pd)
216 put_uobj_read(pd->uobject);
219 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
221 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
224 static void put_cq_read(struct ib_cq *cq)
226 put_uobj_read(cq->uobject);
229 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
231 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
234 static void put_ah_read(struct ib_ah *ah)
236 put_uobj_read(ah->uobject);
239 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
241 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
244 static void put_qp_read(struct ib_qp *qp)
246 put_uobj_read(qp->uobject);
249 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
251 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
254 static void put_srq_read(struct ib_srq *srq)
256 put_uobj_read(srq->uobject);
259 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle,
260 struct ib_ucontext *context,
261 struct ib_uobject **uobj)
263 *uobj = idr_read_uobj(&ib_uverbs_xrc_domain_idr, xrcd_handle,
265 return *uobj ? (*uobj)->object : NULL;
268 static void put_xrcd_read(struct ib_uobject *uobj)
273 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
274 const char __user *buf,
275 int in_len, int out_len)
277 struct ib_uverbs_get_context cmd;
278 struct ib_uverbs_get_context_resp resp;
279 struct ib_udata udata;
280 struct ib_device *ibdev = file->device->ib_dev;
281 struct ib_ucontext *ucontext;
285 if (out_len < sizeof resp)
288 if (copy_from_user(&cmd, buf, sizeof cmd))
291 mutex_lock(&file->mutex);
293 if (file->ucontext) {
298 INIT_UDATA(&udata, buf + sizeof cmd,
299 (unsigned long) cmd.response + sizeof resp,
300 in_len - sizeof cmd, out_len - sizeof resp);
302 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
303 if (IS_ERR(ucontext)) {
304 ret = PTR_ERR(file->ucontext);
308 ucontext->device = ibdev;
309 INIT_LIST_HEAD(&ucontext->pd_list);
310 INIT_LIST_HEAD(&ucontext->mr_list);
311 INIT_LIST_HEAD(&ucontext->mw_list);
312 INIT_LIST_HEAD(&ucontext->cq_list);
313 INIT_LIST_HEAD(&ucontext->qp_list);
314 INIT_LIST_HEAD(&ucontext->srq_list);
315 INIT_LIST_HEAD(&ucontext->ah_list);
316 INIT_LIST_HEAD(&ucontext->xrcd_list);
317 ucontext->closing = 0;
319 resp.num_comp_vectors = file->device->num_comp_vectors;
321 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
327 if (copy_to_user((void __user *) (unsigned long) cmd.response,
328 &resp, sizeof resp)) {
333 file->async_file = filp->private_data;
335 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
336 ib_uverbs_event_handler);
337 ret = ib_register_event_handler(&file->event_handler);
341 kref_get(&file->async_file->ref);
342 kref_get(&file->ref);
343 file->ucontext = ucontext;
345 fd_install(resp.async_fd, filp);
347 mutex_unlock(&file->mutex);
352 put_unused_fd(resp.async_fd);
356 ibdev->dealloc_ucontext(ucontext);
359 mutex_unlock(&file->mutex);
363 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
364 const char __user *buf,
365 int in_len, int out_len)
367 struct ib_uverbs_query_device cmd;
368 struct ib_uverbs_query_device_resp resp;
369 struct ib_device_attr attr;
372 if (out_len < sizeof resp)
375 if (copy_from_user(&cmd, buf, sizeof cmd))
378 ret = ib_query_device(file->device->ib_dev, &attr);
382 memset(&resp, 0, sizeof resp);
384 resp.fw_ver = attr.fw_ver;
385 resp.node_guid = file->device->ib_dev->node_guid;
386 resp.sys_image_guid = attr.sys_image_guid;
387 resp.max_mr_size = attr.max_mr_size;
388 resp.page_size_cap = attr.page_size_cap;
389 resp.vendor_id = attr.vendor_id;
390 resp.vendor_part_id = attr.vendor_part_id;
391 resp.hw_ver = attr.hw_ver;
392 resp.max_qp = attr.max_qp;
393 resp.max_qp_wr = attr.max_qp_wr;
394 resp.device_cap_flags = attr.device_cap_flags;
395 resp.max_sge = attr.max_sge;
396 resp.max_sge_rd = attr.max_sge_rd;
397 resp.max_cq = attr.max_cq;
398 resp.max_cqe = attr.max_cqe;
399 resp.max_mr = attr.max_mr;
400 resp.max_pd = attr.max_pd;
401 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
402 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
403 resp.max_res_rd_atom = attr.max_res_rd_atom;
404 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
405 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
406 resp.atomic_cap = attr.atomic_cap;
407 resp.max_ee = attr.max_ee;
408 resp.max_rdd = attr.max_rdd;
409 resp.max_mw = attr.max_mw;
410 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
411 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
412 resp.max_mcast_grp = attr.max_mcast_grp;
413 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
414 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
415 resp.max_ah = attr.max_ah;
416 resp.max_fmr = attr.max_fmr;
417 resp.max_map_per_fmr = attr.max_map_per_fmr;
418 resp.max_srq = attr.max_srq;
419 resp.max_srq_wr = attr.max_srq_wr;
420 resp.max_srq_sge = attr.max_srq_sge;
421 resp.max_pkeys = attr.max_pkeys;
422 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
423 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
425 if (copy_to_user((void __user *) (unsigned long) cmd.response,
432 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
433 const char __user *buf,
434 int in_len, int out_len)
436 struct ib_uverbs_query_port cmd;
437 struct ib_uverbs_query_port_resp resp;
438 struct ib_port_attr attr;
441 if (out_len < sizeof resp)
444 if (copy_from_user(&cmd, buf, sizeof cmd))
447 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
451 memset(&resp, 0, sizeof resp);
453 resp.state = attr.state;
454 resp.max_mtu = attr.max_mtu;
455 resp.active_mtu = attr.active_mtu;
456 resp.gid_tbl_len = attr.gid_tbl_len;
457 resp.port_cap_flags = attr.port_cap_flags;
458 resp.max_msg_sz = attr.max_msg_sz;
459 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
460 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
461 resp.pkey_tbl_len = attr.pkey_tbl_len;
463 resp.sm_lid = attr.sm_lid;
465 resp.max_vl_num = attr.max_vl_num;
466 resp.sm_sl = attr.sm_sl;
467 resp.subnet_timeout = attr.subnet_timeout;
468 resp.init_type_reply = attr.init_type_reply;
469 resp.active_width = attr.active_width;
470 resp.active_speed = attr.active_speed;
471 resp.phys_state = attr.phys_state;
472 resp.link_layer = attr.link_layer;
474 if (copy_to_user((void __user *) (unsigned long) cmd.response,
481 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
482 const char __user *buf,
483 int in_len, int out_len)
485 struct ib_uverbs_alloc_pd cmd;
486 struct ib_uverbs_alloc_pd_resp resp;
487 struct ib_udata udata;
488 struct ib_uobject *uobj;
492 if (out_len < sizeof resp)
495 if (copy_from_user(&cmd, buf, sizeof cmd))
498 INIT_UDATA(&udata, buf + sizeof cmd,
499 (unsigned long) cmd.response + sizeof resp,
500 in_len - sizeof cmd, out_len - sizeof resp);
502 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
506 init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
507 down_write(&uobj->mutex);
509 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
510 file->ucontext, &udata);
516 pd->device = file->device->ib_dev;
518 atomic_set(&pd->usecnt, 0);
521 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
525 memset(&resp, 0, sizeof resp);
526 resp.pd_handle = uobj->id;
528 if (copy_to_user((void __user *) (unsigned long) cmd.response,
529 &resp, sizeof resp)) {
534 mutex_lock(&file->mutex);
535 list_add_tail(&uobj->list, &file->ucontext->pd_list);
536 mutex_unlock(&file->mutex);
540 up_write(&uobj->mutex);
545 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
551 put_uobj_write(uobj);
555 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
556 const char __user *buf,
557 int in_len, int out_len)
559 struct ib_uverbs_dealloc_pd cmd;
560 struct ib_uobject *uobj;
563 if (copy_from_user(&cmd, buf, sizeof cmd))
566 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
570 ret = ib_dealloc_pd(uobj->object);
574 put_uobj_write(uobj);
579 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
581 mutex_lock(&file->mutex);
582 list_del(&uobj->list);
583 mutex_unlock(&file->mutex);
590 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
591 const char __user *buf, int in_len,
594 struct ib_uverbs_reg_mr cmd;
595 struct ib_uverbs_reg_mr_resp resp;
596 struct ib_udata udata;
597 struct ib_uobject *uobj;
602 if (out_len < sizeof resp)
605 if (copy_from_user(&cmd, buf, sizeof cmd))
608 INIT_UDATA(&udata, buf + sizeof cmd,
609 (unsigned long) cmd.response + sizeof resp,
610 in_len - sizeof cmd, out_len - sizeof resp);
612 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
616 * Local write permission is required if remote write or
617 * remote atomic permission is also requested.
619 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
620 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
623 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
627 init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
628 down_write(&uobj->mutex);
630 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
636 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
637 cmd.access_flags, &udata, 0);
643 mr->device = pd->device;
646 atomic_inc(&pd->usecnt);
647 atomic_set(&mr->usecnt, 0);
650 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
654 memset(&resp, 0, sizeof resp);
655 resp.lkey = mr->lkey;
656 resp.rkey = mr->rkey;
657 resp.mr_handle = uobj->id;
659 if (copy_to_user((void __user *) (unsigned long) cmd.response,
660 &resp, sizeof resp)) {
667 mutex_lock(&file->mutex);
668 list_add_tail(&uobj->list, &file->ucontext->mr_list);
669 mutex_unlock(&file->mutex);
673 up_write(&uobj->mutex);
678 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
687 put_uobj_write(uobj);
691 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
692 const char __user *buf, int in_len,
695 struct ib_uverbs_dereg_mr cmd;
697 struct ib_uobject *uobj;
700 if (copy_from_user(&cmd, buf, sizeof cmd))
703 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
709 ret = ib_dereg_mr(mr);
713 put_uobj_write(uobj);
718 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
720 mutex_lock(&file->mutex);
721 list_del(&uobj->list);
722 mutex_unlock(&file->mutex);
729 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
730 const char __user *buf, int in_len,
733 struct ib_uverbs_create_comp_channel cmd;
734 struct ib_uverbs_create_comp_channel_resp resp;
737 if (out_len < sizeof resp)
740 if (copy_from_user(&cmd, buf, sizeof cmd))
743 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
745 return PTR_ERR(filp);
747 if (copy_to_user((void __user *) (unsigned long) cmd.response,
748 &resp, sizeof resp)) {
749 put_unused_fd(resp.fd);
754 fd_install(resp.fd, filp);
758 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
759 const char __user *buf, int in_len,
762 struct ib_uverbs_create_cq cmd;
763 struct ib_uverbs_create_cq_resp resp;
764 struct ib_udata udata;
765 struct ib_ucq_object *obj;
766 struct ib_uverbs_event_file *ev_file = NULL;
770 if (out_len < sizeof resp)
773 if (copy_from_user(&cmd, buf, sizeof cmd))
776 INIT_UDATA(&udata, buf + sizeof cmd,
777 (unsigned long) cmd.response + sizeof resp,
778 in_len - sizeof cmd, out_len - sizeof resp);
780 if (cmd.comp_vector >= file->device->num_comp_vectors)
783 obj = kmalloc(sizeof *obj, GFP_KERNEL);
787 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
788 down_write(&obj->uobject.mutex);
790 if (cmd.comp_channel >= 0) {
791 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
798 obj->uverbs_file = file;
799 obj->comp_events_reported = 0;
800 obj->async_events_reported = 0;
801 INIT_LIST_HEAD(&obj->comp_list);
802 INIT_LIST_HEAD(&obj->async_list);
804 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
806 file->ucontext, &udata);
812 cq->device = file->device->ib_dev;
813 cq->uobject = &obj->uobject;
814 cq->comp_handler = ib_uverbs_comp_handler;
815 cq->event_handler = ib_uverbs_cq_event_handler;
816 cq->cq_context = ev_file;
817 atomic_set(&cq->usecnt, 0);
819 obj->uobject.object = cq;
820 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
824 memset(&resp, 0, sizeof resp);
825 resp.cq_handle = obj->uobject.id;
828 if (copy_to_user((void __user *) (unsigned long) cmd.response,
829 &resp, sizeof resp)) {
834 mutex_lock(&file->mutex);
835 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
836 mutex_unlock(&file->mutex);
838 obj->uobject.live = 1;
840 up_write(&obj->uobject.mutex);
845 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
852 ib_uverbs_release_ucq(file, ev_file, obj);
855 put_uobj_write(&obj->uobject);
859 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
860 const char __user *buf, int in_len,
863 struct ib_uverbs_resize_cq cmd;
864 struct ib_uverbs_resize_cq_resp resp;
865 struct ib_udata udata;
869 if (copy_from_user(&cmd, buf, sizeof cmd))
872 INIT_UDATA(&udata, buf + sizeof cmd,
873 (unsigned long) cmd.response + sizeof resp,
874 in_len - sizeof cmd, out_len - sizeof resp);
876 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
880 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
886 if (copy_to_user((void __user *) (unsigned long) cmd.response,
887 &resp, sizeof resp.cqe))
893 return ret ? ret : in_len;
896 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
897 const char __user *buf, int in_len,
900 struct ib_uverbs_poll_cq cmd;
901 struct ib_uverbs_poll_cq_resp *resp;
908 if (copy_from_user(&cmd, buf, sizeof cmd))
911 wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
915 rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
916 resp = kmalloc(rsize, GFP_KERNEL);
922 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
928 resp->count = ib_poll_cq(cq, cmd.ne, wc);
932 for (i = 0; i < resp->count; i++) {
933 resp->wc[i].wr_id = wc[i].wr_id;
934 resp->wc[i].status = wc[i].status;
935 resp->wc[i].opcode = wc[i].opcode;
936 resp->wc[i].vendor_err = wc[i].vendor_err;
937 resp->wc[i].byte_len = wc[i].byte_len;
938 resp->wc[i].ex.imm_data = (__u32 __force) wc[i].ex.imm_data;
939 resp->wc[i].qp_num = wc[i].qp->qp_num;
940 resp->wc[i].src_qp = wc[i].src_qp;
941 resp->wc[i].wc_flags = wc[i].wc_flags;
942 resp->wc[i].pkey_index = wc[i].pkey_index;
943 resp->wc[i].slid = wc[i].slid;
944 resp->wc[i].sl = wc[i].sl;
945 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
946 resp->wc[i].port_num = wc[i].port_num;
949 if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
957 return ret ? ret : in_len;
960 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
961 const char __user *buf, int in_len,
964 struct ib_uverbs_req_notify_cq cmd;
967 if (copy_from_user(&cmd, buf, sizeof cmd))
970 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
974 ib_req_notify_cq(cq, cmd.solicited_only ?
975 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
982 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
983 const char __user *buf, int in_len,
986 struct ib_uverbs_destroy_cq cmd;
987 struct ib_uverbs_destroy_cq_resp resp;
988 struct ib_uobject *uobj;
990 struct ib_ucq_object *obj;
991 struct ib_uverbs_event_file *ev_file;
994 if (copy_from_user(&cmd, buf, sizeof cmd))
997 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1001 ev_file = cq->cq_context;
1002 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1004 ret = ib_destroy_cq(cq);
1008 put_uobj_write(uobj);
1013 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1015 mutex_lock(&file->mutex);
1016 list_del(&uobj->list);
1017 mutex_unlock(&file->mutex);
1019 ib_uverbs_release_ucq(file, ev_file, obj);
1021 memset(&resp, 0, sizeof resp);
1022 resp.comp_events_reported = obj->comp_events_reported;
1023 resp.async_events_reported = obj->async_events_reported;
1027 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1028 &resp, sizeof resp))
1034 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1035 const char __user *buf, int in_len,
1038 struct ib_uverbs_create_qp cmd;
1039 struct ib_uverbs_create_qp_resp resp;
1040 struct ib_udata udata;
1041 struct ib_uqp_object *obj;
1043 struct ib_cq *scq, *rcq;
1046 struct ib_qp_init_attr attr;
1047 struct ib_xrcd *xrcd;
1048 struct ib_uobject *xrcd_uobj;
1051 if (out_len < sizeof resp)
1054 if (copy_from_user(&cmd, buf, sizeof cmd))
1057 INIT_UDATA(&udata, buf + sizeof cmd,
1058 (unsigned long) cmd.response + sizeof resp,
1059 in_len - sizeof cmd, out_len - sizeof resp);
1061 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1065 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
1066 down_write(&obj->uevent.uobject.mutex);
1068 srq = (cmd.is_srq && cmd.qp_type != IB_QPT_XRC) ?
1069 idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
1070 xrcd = cmd.qp_type == IB_QPT_XRC ?
1071 idr_read_xrcd(cmd.srq_handle, file->ucontext, &xrcd_uobj) : NULL;
1072 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1073 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
1074 rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
1075 scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
1077 if (!pd || !scq || !rcq || (cmd.is_srq && !srq) ||
1078 (cmd.qp_type == IB_QPT_XRC && !xrcd)) {
1083 attr.create_flags = 0;
1084 attr.event_handler = ib_uverbs_qp_event_handler;
1085 attr.qp_context = file;
1089 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1090 attr.qp_type = cmd.qp_type;
1092 attr.create_flags = 0;
1094 attr.cap.max_send_wr = cmd.max_send_wr;
1095 attr.cap.max_recv_wr = cmd.max_recv_wr;
1096 attr.cap.max_send_sge = cmd.max_send_sge;
1097 attr.cap.max_recv_sge = cmd.max_recv_sge;
1098 attr.cap.max_inline_data = cmd.max_inline_data;
1100 obj->uevent.events_reported = 0;
1101 INIT_LIST_HEAD(&obj->uevent.event_list);
1102 INIT_LIST_HEAD(&obj->mcast_list);
1104 qp = pd->device->create_qp(pd, &attr, &udata);
1110 qp->device = pd->device;
1112 qp->send_cq = attr.send_cq;
1113 qp->recv_cq = attr.recv_cq;
1115 qp->uobject = &obj->uevent.uobject;
1116 qp->event_handler = attr.event_handler;
1117 qp->qp_context = attr.qp_context;
1118 qp->qp_type = attr.qp_type;
1119 qp->xrcd = attr.xrcd;
1120 atomic_inc(&pd->usecnt);
1121 atomic_inc(&attr.send_cq->usecnt);
1122 atomic_inc(&attr.recv_cq->usecnt);
1124 atomic_inc(&attr.srq->usecnt);
1126 atomic_inc(&attr.xrcd->usecnt);
1128 obj->uevent.uobject.object = qp;
1129 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1133 memset(&resp, 0, sizeof resp);
1134 resp.qpn = qp->qp_num;
1135 resp.qp_handle = obj->uevent.uobject.id;
1136 resp.max_recv_sge = attr.cap.max_recv_sge;
1137 resp.max_send_sge = attr.cap.max_send_sge;
1138 resp.max_recv_wr = attr.cap.max_recv_wr;
1139 resp.max_send_wr = attr.cap.max_send_wr;
1140 resp.max_inline_data = attr.cap.max_inline_data;
1142 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1143 &resp, sizeof resp)) {
1155 put_xrcd_read(xrcd_uobj);
1157 mutex_lock(&file->mutex);
1158 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1159 mutex_unlock(&file->mutex);
1161 obj->uevent.uobject.live = 1;
1163 up_write(&obj->uevent.uobject.mutex);
1168 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1178 if (rcq && rcq != scq)
1183 put_xrcd_read(xrcd_uobj);
1185 put_uobj_write(&obj->uevent.uobject);
1189 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1190 const char __user *buf, int in_len,
1193 struct ib_uverbs_query_qp cmd;
1194 struct ib_uverbs_query_qp_resp resp;
1196 struct ib_qp_attr *attr;
1197 struct ib_qp_init_attr *init_attr;
1200 if (copy_from_user(&cmd, buf, sizeof cmd))
1203 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1204 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1205 if (!attr || !init_attr) {
1210 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1216 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1223 memset(&resp, 0, sizeof resp);
1225 resp.qp_state = attr->qp_state;
1226 resp.cur_qp_state = attr->cur_qp_state;
1227 resp.path_mtu = attr->path_mtu;
1228 resp.path_mig_state = attr->path_mig_state;
1229 resp.qkey = attr->qkey;
1230 resp.rq_psn = attr->rq_psn;
1231 resp.sq_psn = attr->sq_psn;
1232 resp.dest_qp_num = attr->dest_qp_num;
1233 resp.qp_access_flags = attr->qp_access_flags;
1234 resp.pkey_index = attr->pkey_index;
1235 resp.alt_pkey_index = attr->alt_pkey_index;
1236 resp.sq_draining = attr->sq_draining;
1237 resp.max_rd_atomic = attr->max_rd_atomic;
1238 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1239 resp.min_rnr_timer = attr->min_rnr_timer;
1240 resp.port_num = attr->port_num;
1241 resp.timeout = attr->timeout;
1242 resp.retry_cnt = attr->retry_cnt;
1243 resp.rnr_retry = attr->rnr_retry;
1244 resp.alt_port_num = attr->alt_port_num;
1245 resp.alt_timeout = attr->alt_timeout;
1247 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1248 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1249 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1250 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1251 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1252 resp.dest.dlid = attr->ah_attr.dlid;
1253 resp.dest.sl = attr->ah_attr.sl;
1254 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1255 resp.dest.static_rate = attr->ah_attr.static_rate;
1256 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1257 resp.dest.port_num = attr->ah_attr.port_num;
1259 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1260 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1261 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1262 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1263 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1264 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1265 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1266 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1267 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1268 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1269 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1271 resp.max_send_wr = init_attr->cap.max_send_wr;
1272 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1273 resp.max_send_sge = init_attr->cap.max_send_sge;
1274 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1275 resp.max_inline_data = init_attr->cap.max_inline_data;
1276 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1278 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1279 &resp, sizeof resp))
1286 return ret ? ret : in_len;
1289 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1290 const char __user *buf, int in_len,
1293 struct ib_uverbs_modify_qp cmd;
1294 struct ib_udata udata;
1296 struct ib_qp_attr *attr;
1299 if (copy_from_user(&cmd, buf, sizeof cmd))
1302 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1305 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1309 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1315 attr->qp_state = cmd.qp_state;
1316 attr->cur_qp_state = cmd.cur_qp_state;
1317 attr->path_mtu = cmd.path_mtu;
1318 attr->path_mig_state = cmd.path_mig_state;
1319 attr->qkey = cmd.qkey;
1320 attr->rq_psn = cmd.rq_psn;
1321 attr->sq_psn = cmd.sq_psn;
1322 attr->dest_qp_num = cmd.dest_qp_num;
1323 attr->qp_access_flags = cmd.qp_access_flags;
1324 attr->pkey_index = cmd.pkey_index;
1325 attr->alt_pkey_index = cmd.alt_pkey_index;
1326 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1327 attr->max_rd_atomic = cmd.max_rd_atomic;
1328 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1329 attr->min_rnr_timer = cmd.min_rnr_timer;
1330 attr->port_num = cmd.port_num;
1331 attr->timeout = cmd.timeout;
1332 attr->retry_cnt = cmd.retry_cnt;
1333 attr->rnr_retry = cmd.rnr_retry;
1334 attr->alt_port_num = cmd.alt_port_num;
1335 attr->alt_timeout = cmd.alt_timeout;
1337 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1338 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1339 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1340 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1341 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1342 attr->ah_attr.dlid = cmd.dest.dlid;
1343 attr->ah_attr.sl = cmd.dest.sl;
1344 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1345 attr->ah_attr.static_rate = cmd.dest.static_rate;
1346 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1347 attr->ah_attr.port_num = cmd.dest.port_num;
1349 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1350 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1351 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1352 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1353 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1354 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1355 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1356 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1357 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1358 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1359 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1361 ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
1376 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1377 const char __user *buf, int in_len,
1380 struct ib_uverbs_destroy_qp cmd;
1381 struct ib_uverbs_destroy_qp_resp resp;
1382 struct ib_uobject *uobj;
1384 struct ib_uqp_object *obj;
1387 if (copy_from_user(&cmd, buf, sizeof cmd))
1390 memset(&resp, 0, sizeof resp);
1392 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1396 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1398 if (!list_empty(&obj->mcast_list)) {
1399 put_uobj_write(uobj);
1403 ret = ib_destroy_qp(qp);
1407 put_uobj_write(uobj);
1412 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
1414 mutex_lock(&file->mutex);
1415 list_del(&uobj->list);
1416 mutex_unlock(&file->mutex);
1418 ib_uverbs_release_uevent(file, &obj->uevent);
1420 resp.events_reported = obj->uevent.events_reported;
1424 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1425 &resp, sizeof resp))
1431 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1432 const char __user *buf, int in_len,
1435 struct ib_uverbs_post_send cmd;
1436 struct ib_uverbs_post_send_resp resp;
1437 struct ib_uverbs_send_wr *user_wr;
1438 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
1442 ssize_t ret = -EINVAL;
1444 if (copy_from_user(&cmd, buf, sizeof cmd))
1447 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1448 cmd.sge_count * sizeof (struct ib_uverbs_sge))
1451 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1454 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1458 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1462 is_ud = qp->qp_type == IB_QPT_UD;
1465 for (i = 0; i < cmd.wr_count; ++i) {
1466 if (copy_from_user(user_wr,
1467 buf + sizeof cmd + i * cmd.wqe_size,
1473 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1478 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1479 user_wr->num_sge * sizeof (struct ib_sge),
1493 next->wr_id = user_wr->wr_id;
1494 next->num_sge = user_wr->num_sge;
1495 next->opcode = user_wr->opcode;
1496 next->send_flags = user_wr->send_flags;
1499 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1501 if (!next->wr.ud.ah) {
1505 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
1506 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1508 switch (next->opcode) {
1509 case IB_WR_RDMA_WRITE_WITH_IMM:
1511 (__be32 __force) user_wr->ex.imm_data;
1512 case IB_WR_RDMA_WRITE:
1513 case IB_WR_RDMA_READ:
1514 next->wr.rdma.remote_addr =
1515 user_wr->wr.rdma.remote_addr;
1516 next->wr.rdma.rkey =
1517 user_wr->wr.rdma.rkey;
1519 case IB_WR_SEND_WITH_IMM:
1521 (__be32 __force) user_wr->ex.imm_data;
1523 case IB_WR_SEND_WITH_INV:
1524 next->ex.invalidate_rkey =
1525 user_wr->ex.invalidate_rkey;
1527 case IB_WR_ATOMIC_CMP_AND_SWP:
1528 case IB_WR_ATOMIC_FETCH_AND_ADD:
1529 next->wr.atomic.remote_addr =
1530 user_wr->wr.atomic.remote_addr;
1531 next->wr.atomic.compare_add =
1532 user_wr->wr.atomic.compare_add;
1533 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1534 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1541 if (next->num_sge) {
1542 next->sg_list = (void *) next +
1543 ALIGN(sizeof *next, sizeof (struct ib_sge));
1544 if (copy_from_user(next->sg_list,
1546 cmd.wr_count * cmd.wqe_size +
1547 sg_ind * sizeof (struct ib_sge),
1548 next->num_sge * sizeof (struct ib_sge))) {
1552 sg_ind += next->num_sge;
1554 next->sg_list = NULL;
1558 ret = qp->device->post_send(qp, wr, &bad_wr);
1560 for (next = wr; next; next = next->next) {
1566 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1567 &resp, sizeof resp))
1574 if (is_ud && wr->wr.ud.ah)
1575 put_ah_read(wr->wr.ud.ah);
1584 return ret ? ret : in_len;
1587 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1593 struct ib_uverbs_recv_wr *user_wr;
1594 struct ib_recv_wr *wr = NULL, *last, *next;
1599 if (in_len < wqe_size * wr_count +
1600 sge_count * sizeof (struct ib_uverbs_sge))
1601 return ERR_PTR(-EINVAL);
1603 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1604 return ERR_PTR(-EINVAL);
1606 user_wr = kmalloc(wqe_size, GFP_KERNEL);
1608 return ERR_PTR(-ENOMEM);
1612 for (i = 0; i < wr_count; ++i) {
1613 if (copy_from_user(user_wr, buf + i * wqe_size,
1619 if (user_wr->num_sge + sg_ind > sge_count) {
1624 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1625 user_wr->num_sge * sizeof (struct ib_sge),
1639 next->wr_id = user_wr->wr_id;
1640 next->num_sge = user_wr->num_sge;
1642 if (next->num_sge) {
1643 next->sg_list = (void *) next +
1644 ALIGN(sizeof *next, sizeof (struct ib_sge));
1645 if (copy_from_user(next->sg_list,
1646 buf + wr_count * wqe_size +
1647 sg_ind * sizeof (struct ib_sge),
1648 next->num_sge * sizeof (struct ib_sge))) {
1652 sg_ind += next->num_sge;
1654 next->sg_list = NULL;
1669 return ERR_PTR(ret);
1672 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1673 const char __user *buf, int in_len,
1676 struct ib_uverbs_post_recv cmd;
1677 struct ib_uverbs_post_recv_resp resp;
1678 struct ib_recv_wr *wr, *next, *bad_wr;
1680 ssize_t ret = -EINVAL;
1682 if (copy_from_user(&cmd, buf, sizeof cmd))
1685 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1686 in_len - sizeof cmd, cmd.wr_count,
1687 cmd.sge_count, cmd.wqe_size);
1691 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1696 ret = qp->device->post_recv(qp, wr, &bad_wr);
1701 for (next = wr; next; next = next->next) {
1707 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1708 &resp, sizeof resp))
1718 return ret ? ret : in_len;
1721 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1722 const char __user *buf, int in_len,
1725 struct ib_uverbs_post_srq_recv cmd;
1726 struct ib_uverbs_post_srq_recv_resp resp;
1727 struct ib_recv_wr *wr, *next, *bad_wr;
1729 ssize_t ret = -EINVAL;
1731 if (copy_from_user(&cmd, buf, sizeof cmd))
1734 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1735 in_len - sizeof cmd, cmd.wr_count,
1736 cmd.sge_count, cmd.wqe_size);
1740 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1745 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1750 for (next = wr; next; next = next->next) {
1756 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1757 &resp, sizeof resp))
1767 return ret ? ret : in_len;
1770 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1771 const char __user *buf, int in_len,
1774 struct ib_uverbs_create_ah cmd;
1775 struct ib_uverbs_create_ah_resp resp;
1776 struct ib_uobject *uobj;
1779 struct ib_ah_attr attr;
1782 if (out_len < sizeof resp)
1785 if (copy_from_user(&cmd, buf, sizeof cmd))
1788 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1792 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
1793 down_write(&uobj->mutex);
1795 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1801 attr.dlid = cmd.attr.dlid;
1802 attr.sl = cmd.attr.sl;
1803 attr.src_path_bits = cmd.attr.src_path_bits;
1804 attr.static_rate = cmd.attr.static_rate;
1805 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
1806 attr.port_num = cmd.attr.port_num;
1807 attr.grh.flow_label = cmd.attr.grh.flow_label;
1808 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
1809 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
1810 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1811 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1813 ah = ib_create_ah(pd, &attr);
1822 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
1826 resp.ah_handle = uobj->id;
1828 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1829 &resp, sizeof resp)) {
1836 mutex_lock(&file->mutex);
1837 list_add_tail(&uobj->list, &file->ucontext->ah_list);
1838 mutex_unlock(&file->mutex);
1842 up_write(&uobj->mutex);
1847 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1856 put_uobj_write(uobj);
1860 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1861 const char __user *buf, int in_len, int out_len)
1863 struct ib_uverbs_destroy_ah cmd;
1865 struct ib_uobject *uobj;
1868 if (copy_from_user(&cmd, buf, sizeof cmd))
1871 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
1876 ret = ib_destroy_ah(ah);
1880 put_uobj_write(uobj);
1885 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1887 mutex_lock(&file->mutex);
1888 list_del(&uobj->list);
1889 mutex_unlock(&file->mutex);
1896 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1897 const char __user *buf, int in_len,
1900 struct ib_uverbs_attach_mcast cmd;
1902 struct ib_uqp_object *obj;
1903 struct ib_uverbs_mcast_entry *mcast;
1906 if (copy_from_user(&cmd, buf, sizeof cmd))
1909 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1913 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1915 list_for_each_entry(mcast, &obj->mcast_list, list)
1916 if (cmd.mlid == mcast->lid &&
1917 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1922 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1928 mcast->lid = cmd.mlid;
1929 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1931 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1933 list_add_tail(&mcast->list, &obj->mcast_list);
1940 return ret ? ret : in_len;
1943 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1944 const char __user *buf, int in_len,
1947 struct ib_uverbs_detach_mcast cmd;
1948 struct ib_uqp_object *obj;
1950 struct ib_uverbs_mcast_entry *mcast;
1953 if (copy_from_user(&cmd, buf, sizeof cmd))
1956 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1960 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1964 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1966 list_for_each_entry(mcast, &obj->mcast_list, list)
1967 if (cmd.mlid == mcast->lid &&
1968 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1969 list_del(&mcast->list);
1977 return ret ? ret : in_len;
1980 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1981 const char __user *buf, int in_len,
1984 struct ib_uverbs_create_srq cmd;
1985 struct ib_uverbs_create_srq_resp resp;
1986 struct ib_udata udata;
1987 struct ib_uevent_object *obj;
1990 struct ib_srq_init_attr attr;
1993 if (out_len < sizeof resp)
1996 if (copy_from_user(&cmd, buf, sizeof cmd))
1999 INIT_UDATA(&udata, buf + sizeof cmd,
2000 (unsigned long) cmd.response + sizeof resp,
2001 in_len - sizeof cmd, out_len - sizeof resp);
2003 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2007 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
2008 down_write(&obj->uobject.mutex);
2010 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2016 attr.event_handler = ib_uverbs_srq_event_handler;
2017 attr.srq_context = file;
2018 attr.attr.max_wr = cmd.max_wr;
2019 attr.attr.max_sge = cmd.max_sge;
2020 attr.attr.srq_limit = cmd.srq_limit;
2022 obj->events_reported = 0;
2023 INIT_LIST_HEAD(&obj->event_list);
2025 srq = pd->device->create_srq(pd, &attr, &udata);
2031 srq->device = pd->device;
2033 srq->uobject = &obj->uobject;
2034 srq->event_handler = attr.event_handler;
2035 srq->srq_context = attr.srq_context;
2036 srq->ext.xrc.cq = NULL;
2037 srq->ext.xrc.xrcd = NULL;
2038 atomic_inc(&pd->usecnt);
2039 atomic_set(&srq->usecnt, 0);
2041 obj->uobject.object = srq;
2042 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2046 memset(&resp, 0, sizeof resp);
2047 resp.srq_handle = obj->uobject.id;
2048 resp.max_wr = attr.attr.max_wr;
2049 resp.max_sge = attr.attr.max_sge;
2051 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2052 &resp, sizeof resp)) {
2059 mutex_lock(&file->mutex);
2060 list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
2061 mutex_unlock(&file->mutex);
2063 obj->uobject.live = 1;
2065 up_write(&obj->uobject.mutex);
2070 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2073 ib_destroy_srq(srq);
2079 put_uobj_write(&obj->uobject);
2083 ssize_t ib_uverbs_create_xrc_srq(struct ib_uverbs_file *file,
2084 const char __user *buf, int in_len,
2087 struct ib_uverbs_create_xsrq cmd;
2088 struct ib_uverbs_create_srq_resp resp;
2089 struct ib_udata udata;
2090 struct ib_uevent_object *obj;
2093 struct ib_cq *xrc_cq;
2094 struct ib_xrcd *xrcd;
2095 struct ib_srq_init_attr attr;
2096 struct ib_uobject *xrcd_uobj;
2099 if (out_len < sizeof resp)
2102 if (copy_from_user(&cmd, buf, sizeof cmd))
2105 INIT_UDATA(&udata, buf + sizeof cmd,
2106 (unsigned long) cmd.response + sizeof resp,
2107 in_len - sizeof cmd, out_len - sizeof resp);
2109 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2113 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext,
2115 down_write(&obj->uobject.mutex);
2117 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2123 xrc_cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
2129 xrcd = idr_read_xrcd(cmd.xrcd_handle, file->ucontext, &xrcd_uobj);
2136 attr.event_handler = ib_uverbs_srq_event_handler;
2137 attr.srq_context = file;
2138 attr.attr.max_wr = cmd.max_wr;
2139 attr.attr.max_sge = cmd.max_sge;
2140 attr.attr.srq_limit = cmd.srq_limit;
2142 obj->events_reported = 0;
2143 INIT_LIST_HEAD(&obj->event_list);
2145 srq = pd->device->create_xrc_srq(pd, xrc_cq, xrcd, &attr, &udata);
2151 srq->device = pd->device;
2153 srq->uobject = &obj->uobject;
2154 srq->event_handler = attr.event_handler;
2155 srq->srq_context = attr.srq_context;
2156 srq->ext.xrc.cq = xrc_cq;
2157 srq->ext.xrc.xrcd = xrcd;
2158 atomic_inc(&pd->usecnt);
2159 atomic_inc(&xrc_cq->usecnt);
2160 atomic_inc(&xrcd->usecnt);
2162 atomic_set(&srq->usecnt, 0);
2164 obj->uobject.object = srq;
2165 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2169 memset(&resp, 0, sizeof resp);
2170 resp.srq_handle = obj->uobject.id;
2171 resp.max_wr = attr.attr.max_wr;
2172 resp.max_sge = attr.attr.max_sge;
2174 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2175 &resp, sizeof resp)) {
2180 put_xrcd_read(xrcd_uobj);
2181 put_cq_read(xrc_cq);
2184 mutex_lock(&file->mutex);
2185 list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
2186 mutex_unlock(&file->mutex);
2188 obj->uobject.live = 1;
2190 up_write(&obj->uobject.mutex);
2195 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2198 ib_destroy_srq(srq);
2201 put_xrcd_read(xrcd_uobj);
2204 put_cq_read(xrc_cq);
2210 put_uobj_write(&obj->uobject);
2214 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2215 const char __user *buf, int in_len,
2218 struct ib_uverbs_modify_srq cmd;
2219 struct ib_udata udata;
2221 struct ib_srq_attr attr;
2224 if (copy_from_user(&cmd, buf, sizeof cmd))
2227 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2230 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2234 attr.max_wr = cmd.max_wr;
2235 attr.srq_limit = cmd.srq_limit;
2237 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2241 return ret ? ret : in_len;
2244 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2245 const char __user *buf,
2246 int in_len, int out_len)
2248 struct ib_uverbs_query_srq cmd;
2249 struct ib_uverbs_query_srq_resp resp;
2250 struct ib_srq_attr attr;
2254 if (out_len < sizeof resp)
2257 if (copy_from_user(&cmd, buf, sizeof cmd))
2260 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2264 ret = ib_query_srq(srq, &attr);
2271 memset(&resp, 0, sizeof resp);
2273 resp.max_wr = attr.max_wr;
2274 resp.max_sge = attr.max_sge;
2275 resp.srq_limit = attr.srq_limit;
2277 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2278 &resp, sizeof resp))
2284 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2285 const char __user *buf, int in_len,
2288 struct ib_uverbs_destroy_srq cmd;
2289 struct ib_uverbs_destroy_srq_resp resp;
2290 struct ib_uobject *uobj;
2292 struct ib_uevent_object *obj;
2295 if (copy_from_user(&cmd, buf, sizeof cmd))
2298 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2302 obj = container_of(uobj, struct ib_uevent_object, uobject);
2304 ret = ib_destroy_srq(srq);
2308 put_uobj_write(uobj);
2313 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2315 mutex_lock(&file->mutex);
2316 list_del(&uobj->list);
2317 mutex_unlock(&file->mutex);
2319 ib_uverbs_release_uevent(file, obj);
2321 memset(&resp, 0, sizeof resp);
2322 resp.events_reported = obj->events_reported;
2326 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2327 &resp, sizeof resp))
2330 return ret ? ret : in_len;
2333 static struct inode *xrc_file2inode(struct file *f)
2335 return f->f_dentry->d_inode;
2338 struct xrcd_table_entry {
2339 struct rb_node node;
2340 struct inode *inode;
2341 struct ib_xrcd *xrcd;
2344 static int xrcd_table_insert(struct ib_device *dev,
2346 struct ib_xrcd *xrcd)
2348 struct xrcd_table_entry *entry, *scan;
2349 struct rb_node **p = &dev->ib_uverbs_xrcd_table.rb_node;
2350 struct rb_node *parent = NULL;
2352 entry = kmalloc(sizeof(struct xrcd_table_entry), GFP_KERNEL);
2361 scan = rb_entry(parent, struct xrcd_table_entry, node);
2363 if (i_n < scan->inode)
2365 else if (i_n > scan->inode)
2366 p = &(*p)->rb_right;
2373 rb_link_node(&entry->node, parent, p);
2374 rb_insert_color(&entry->node, &dev->ib_uverbs_xrcd_table);
2379 static struct xrcd_table_entry *xrcd_table_search(struct ib_device *dev,
2382 struct xrcd_table_entry *scan;
2383 struct rb_node **p = &dev->ib_uverbs_xrcd_table.rb_node;
2384 struct rb_node *parent = NULL;
2388 scan = rb_entry(parent, struct xrcd_table_entry, node);
2390 if (i_n < scan->inode)
2392 else if (i_n > scan->inode)
2393 p = &(*p)->rb_right;
2400 static int find_xrcd(struct ib_device *dev, struct inode *i_n,
2401 struct ib_xrcd **xrcd)
2403 struct xrcd_table_entry *entry;
2405 entry = xrcd_table_search(dev, i_n);
2409 *xrcd = entry->xrcd;
2414 static void xrcd_table_delete(struct ib_device *dev,
2417 struct xrcd_table_entry *entry = xrcd_table_search(dev, i_n);
2421 rb_erase(&entry->node, &dev->ib_uverbs_xrcd_table);
2426 ssize_t ib_uverbs_open_xrc_domain(struct ib_uverbs_file *file,
2427 const char __user *buf, int in_len,
2430 struct ib_uverbs_open_xrc_domain cmd;
2431 struct ib_uverbs_open_xrc_domain_resp resp;
2432 struct ib_udata udata;
2433 struct ib_uobject *uobj;
2434 struct ib_uxrcd_object *xrcd_uobj;
2435 struct ib_xrcd *xrcd = NULL;
2436 struct file *f = NULL;
2437 struct inode *inode = NULL;
2441 if (out_len < sizeof resp)
2444 if (copy_from_user(&cmd, buf, sizeof cmd))
2447 INIT_UDATA(&udata, buf + sizeof cmd,
2448 (unsigned long) cmd.response + sizeof resp,
2449 in_len - sizeof cmd, out_len - sizeof resp);
2451 mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
2452 if (cmd.fd != (u32) (-1)) {
2453 /* search for file descriptor */
2457 goto err_table_mutex_unlock;
2460 inode = xrc_file2inode(f);
2463 goto err_table_mutex_unlock;
2466 ret = find_xrcd(file->device->ib_dev, inode, &xrcd);
2467 if (ret && !(cmd.oflags & O_CREAT)) {
2468 /* no file descriptor. Need CREATE flag */
2470 goto err_table_mutex_unlock;
2473 if (xrcd && cmd.oflags & O_EXCL) {
2475 goto err_table_mutex_unlock;
2479 xrcd_uobj = kmalloc(sizeof *xrcd_uobj, GFP_KERNEL);
2482 goto err_table_mutex_unlock;
2485 uobj = &xrcd_uobj->uobject;
2486 init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
2487 down_write(&uobj->mutex);
2490 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
2491 file->ucontext, &udata);
2493 ret = PTR_ERR(xrcd);
2496 xrcd->uobject = (cmd.fd == -1) ? uobj : NULL;
2497 xrcd->inode = inode;
2498 xrcd->device = file->device->ib_dev;
2499 atomic_set(&xrcd->usecnt, 0);
2503 uobj->object = xrcd;
2504 ret = idr_add_uobj(&ib_uverbs_xrc_domain_idr, uobj);
2508 memset(&resp, 0, sizeof resp);
2509 resp.xrcd_handle = uobj->id;
2513 /* create new inode/xrcd table entry */
2514 ret = xrcd_table_insert(file->device->ib_dev, inode, xrcd);
2516 goto err_insert_xrcd;
2518 atomic_inc(&xrcd->usecnt);
2523 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2524 &resp, sizeof resp)) {
2529 INIT_LIST_HEAD(&xrcd_uobj->xrc_reg_qp_list);
2531 mutex_lock(&file->mutex);
2532 list_add_tail(&uobj->list, &file->ucontext->xrcd_list);
2533 mutex_unlock(&file->mutex);
2537 up_write(&uobj->mutex);
2539 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2546 xrcd_table_delete(file->device->ib_dev, inode);
2547 atomic_dec(&xrcd->usecnt);
2551 idr_remove_uobj(&ib_uverbs_xrc_domain_idr, uobj);
2554 ib_dealloc_xrcd(xrcd);
2557 put_uobj_write(uobj);
2559 err_table_mutex_unlock:
2563 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2567 ssize_t ib_uverbs_close_xrc_domain(struct ib_uverbs_file *file,
2568 const char __user *buf, int in_len,
2571 struct ib_uverbs_close_xrc_domain cmd;
2572 struct ib_uobject *uobj, *t_uobj;
2573 struct ib_uxrcd_object *xrcd_uobj;
2574 struct ib_xrcd *xrcd = NULL;
2575 struct inode *inode = NULL;
2578 if (copy_from_user(&cmd, buf, sizeof cmd))
2581 mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
2582 uobj = idr_write_uobj(&ib_uverbs_xrc_domain_idr, cmd.xrcd_handle,
2586 goto err_unlock_mutex;
2589 mutex_lock(&file->mutex);
2591 list_for_each_entry(t_uobj, &file->ucontext->qp_list, list) {
2592 struct ib_qp *qp = t_uobj->object;
2593 if (qp->xrcd && qp->xrcd == uobj->object) {
2600 list_for_each_entry(t_uobj, &file->ucontext->srq_list, list) {
2601 struct ib_srq *srq = t_uobj->object;
2602 if (srq->ext.xrc.xrcd && srq->ext.xrc.xrcd == uobj->object) {
2608 mutex_unlock(&file->mutex);
2610 put_uobj_write(uobj);
2611 goto err_unlock_mutex;
2614 xrcd_uobj = container_of(uobj, struct ib_uxrcd_object, uobject);
2615 if (!list_empty(&xrcd_uobj->xrc_reg_qp_list)) {
2617 put_uobj_write(uobj);
2618 goto err_unlock_mutex;
2621 xrcd = (struct ib_xrcd *) (uobj->object);
2622 inode = xrcd->inode;
2625 atomic_dec(&xrcd->usecnt);
2627 ret = ib_dealloc_xrcd(uobj->object);
2631 put_uobj_write(uobj);
2634 goto err_unlock_mutex;
2637 xrcd_table_delete(file->device->ib_dev, inode);
2639 idr_remove_uobj(&ib_uverbs_xrc_domain_idr, uobj);
2641 mutex_lock(&file->mutex);
2642 list_del(&uobj->list);
2643 mutex_unlock(&file->mutex);
2647 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2651 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2655 void ib_uverbs_dealloc_xrcd(struct ib_device *ib_dev,
2656 struct ib_xrcd *xrcd)
2658 struct inode *inode = NULL;
2661 inode = xrcd->inode;
2663 atomic_dec(&xrcd->usecnt);
2665 ret = ib_dealloc_xrcd(xrcd);
2667 xrcd_table_delete(ib_dev, inode);
2670 ssize_t ib_uverbs_create_xrc_rcv_qp(struct ib_uverbs_file *file,
2671 const char __user *buf, int in_len,
2674 struct ib_uverbs_create_xrc_rcv_qp cmd;
2675 struct ib_uverbs_create_xrc_rcv_qp_resp resp;
2676 struct ib_uxrc_rcv_object *obj;
2677 struct ib_qp_init_attr init_attr;
2678 struct ib_xrcd *xrcd;
2679 struct ib_uobject *uobj;
2680 struct ib_uxrcd_object *xrcd_uobj;
2684 if (out_len < sizeof resp)
2687 if (copy_from_user(&cmd, buf, sizeof cmd))
2690 obj = kzalloc(sizeof *obj, GFP_KERNEL);
2694 xrcd = idr_read_xrcd(cmd.xrc_domain_handle, file->ucontext, &uobj);
2700 init_attr.event_handler = ib_uverbs_xrc_rcv_qp_event_handler;
2701 init_attr.qp_context = file;
2702 init_attr.srq = NULL;
2703 init_attr.sq_sig_type =
2704 cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
2705 init_attr.qp_type = IB_QPT_XRC;
2706 init_attr.xrcd = xrcd;
2708 init_attr.cap.max_send_wr = 1;
2709 init_attr.cap.max_recv_wr = 0;
2710 init_attr.cap.max_send_sge = 1;
2711 init_attr.cap.max_recv_sge = 0;
2712 init_attr.cap.max_inline_data = 0;
2714 err = xrcd->device->create_xrc_rcv_qp(&init_attr, &qp_num);
2718 memset(&resp, 0, sizeof resp);
2721 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2722 &resp, sizeof resp)) {
2727 atomic_inc(&xrcd->usecnt);
2728 put_xrcd_read(uobj);
2729 obj->qp_num = qp_num;
2730 obj->domain_handle = cmd.xrc_domain_handle;
2731 xrcd_uobj = container_of(uobj, struct ib_uxrcd_object, uobject);
2732 mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
2733 list_add_tail(&obj->list, &xrcd_uobj->xrc_reg_qp_list);
2734 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2739 xrcd->device->unreg_xrc_rcv_qp(xrcd, file, qp_num);
2741 put_xrcd_read(uobj);
2747 ssize_t ib_uverbs_modify_xrc_rcv_qp(struct ib_uverbs_file *file,
2748 const char __user *buf, int in_len,
2751 struct ib_uverbs_modify_xrc_rcv_qp cmd;
2752 struct ib_qp_attr *attr;
2753 struct ib_xrcd *xrcd;
2754 struct ib_uobject *uobj;
2757 if (copy_from_user(&cmd, buf, sizeof cmd))
2760 attr = kzalloc(sizeof *attr, GFP_KERNEL);
2764 xrcd = idr_read_xrcd(cmd.xrc_domain_handle, file->ucontext, &uobj);
2770 attr->qp_state = cmd.qp_state;
2771 attr->cur_qp_state = cmd.cur_qp_state;
2772 attr->qp_access_flags = cmd.qp_access_flags;
2773 attr->pkey_index = cmd.pkey_index;
2774 attr->port_num = cmd.port_num;
2775 attr->path_mtu = cmd.path_mtu;
2776 attr->path_mig_state = cmd.path_mig_state;
2777 attr->qkey = cmd.qkey;
2778 attr->rq_psn = cmd.rq_psn;
2779 attr->sq_psn = cmd.sq_psn;
2780 attr->dest_qp_num = cmd.dest_qp_num;
2781 attr->alt_pkey_index = cmd.alt_pkey_index;
2782 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
2783 attr->max_rd_atomic = cmd.max_rd_atomic;
2784 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
2785 attr->min_rnr_timer = cmd.min_rnr_timer;
2786 attr->port_num = cmd.port_num;
2787 attr->timeout = cmd.timeout;
2788 attr->retry_cnt = cmd.retry_cnt;
2789 attr->rnr_retry = cmd.rnr_retry;
2790 attr->alt_port_num = cmd.alt_port_num;
2791 attr->alt_timeout = cmd.alt_timeout;
2793 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
2794 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
2795 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
2796 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
2797 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
2798 attr->ah_attr.dlid = cmd.dest.dlid;
2799 attr->ah_attr.sl = cmd.dest.sl;
2800 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
2801 attr->ah_attr.static_rate = cmd.dest.static_rate;
2802 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
2803 attr->ah_attr.port_num = cmd.dest.port_num;
2805 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
2806 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
2807 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
2808 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
2809 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
2810 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
2811 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
2812 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
2813 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
2814 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
2815 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
2817 err = xrcd->device->modify_xrc_rcv_qp(xrcd, cmd.qp_num, attr, cmd.attr_mask);
2818 put_xrcd_read(uobj);
2820 return err ? err : in_len;
2823 ssize_t ib_uverbs_query_xrc_rcv_qp(struct ib_uverbs_file *file,
2824 const char __user *buf, int in_len,
2827 struct ib_uverbs_query_xrc_rcv_qp cmd;
2828 struct ib_uverbs_query_qp_resp resp;
2829 struct ib_qp_attr *attr;
2830 struct ib_qp_init_attr *init_attr;
2831 struct ib_xrcd *xrcd;
2832 struct ib_uobject *uobj;
2835 if (copy_from_user(&cmd, buf, sizeof cmd))
2838 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2839 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
2840 if (!attr || !init_attr) {
2845 xrcd = idr_read_xrcd(cmd.xrc_domain_handle, file->ucontext, &uobj);
2851 ret = xrcd->device->query_xrc_rcv_qp(xrcd, cmd.qp_num, attr,
2852 cmd.attr_mask, init_attr);
2854 put_xrcd_read(uobj);
2859 memset(&resp, 0, sizeof resp);
2860 resp.qp_state = attr->qp_state;
2861 resp.cur_qp_state = attr->cur_qp_state;
2862 resp.path_mtu = attr->path_mtu;
2863 resp.path_mig_state = attr->path_mig_state;
2864 resp.qkey = attr->qkey;
2865 resp.rq_psn = attr->rq_psn;
2866 resp.sq_psn = attr->sq_psn;
2867 resp.dest_qp_num = attr->dest_qp_num;
2868 resp.qp_access_flags = attr->qp_access_flags;
2869 resp.pkey_index = attr->pkey_index;
2870 resp.alt_pkey_index = attr->alt_pkey_index;
2871 resp.sq_draining = attr->sq_draining;
2872 resp.max_rd_atomic = attr->max_rd_atomic;
2873 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
2874 resp.min_rnr_timer = attr->min_rnr_timer;
2875 resp.port_num = attr->port_num;
2876 resp.timeout = attr->timeout;
2877 resp.retry_cnt = attr->retry_cnt;
2878 resp.rnr_retry = attr->rnr_retry;
2879 resp.alt_port_num = attr->alt_port_num;
2880 resp.alt_timeout = attr->alt_timeout;
2882 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
2883 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
2884 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
2885 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
2886 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
2887 resp.dest.dlid = attr->ah_attr.dlid;
2888 resp.dest.sl = attr->ah_attr.sl;
2889 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
2890 resp.dest.static_rate = attr->ah_attr.static_rate;
2891 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
2892 resp.dest.port_num = attr->ah_attr.port_num;
2894 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
2895 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
2896 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
2897 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
2898 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
2899 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
2900 resp.alt_dest.sl = attr->alt_ah_attr.sl;
2901 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
2902 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
2903 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
2904 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
2906 resp.max_send_wr = init_attr->cap.max_send_wr;
2907 resp.max_recv_wr = init_attr->cap.max_recv_wr;
2908 resp.max_send_sge = init_attr->cap.max_send_sge;
2909 resp.max_recv_sge = init_attr->cap.max_recv_sge;
2910 resp.max_inline_data = init_attr->cap.max_inline_data;
2911 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
2913 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2914 &resp, sizeof resp))
2921 return ret ? ret : in_len;
2924 ssize_t ib_uverbs_reg_xrc_rcv_qp(struct ib_uverbs_file *file,
2925 const char __user *buf, int in_len,
2928 struct ib_uverbs_reg_xrc_rcv_qp cmd;
2929 struct ib_uxrc_rcv_object *qp_obj, *tmp;
2930 struct ib_xrcd *xrcd;
2931 struct ib_uobject *uobj;
2932 struct ib_uxrcd_object *xrcd_uobj;
2935 if (copy_from_user(&cmd, buf, sizeof cmd))
2938 qp_obj = kmalloc(sizeof *qp_obj, GFP_KERNEL);
2942 xrcd = idr_read_xrcd(cmd.xrc_domain_handle, file->ucontext, &uobj);
2948 ret = xrcd->device->reg_xrc_rcv_qp(xrcd, file, cmd.qp_num);
2952 xrcd_uobj = container_of(uobj, struct ib_uxrcd_object, uobject);
2953 mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
2954 list_for_each_entry(tmp, &xrcd_uobj->xrc_reg_qp_list, list)
2955 if (cmd.qp_num == tmp->qp_num) {
2957 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2958 put_xrcd_read(uobj);
2961 qp_obj->qp_num = cmd.qp_num;
2962 qp_obj->domain_handle = cmd.xrc_domain_handle;
2963 list_add_tail(&qp_obj->list, &xrcd_uobj->xrc_reg_qp_list);
2964 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
2965 atomic_inc(&xrcd->usecnt);
2966 put_xrcd_read(uobj);
2970 put_xrcd_read(uobj);
2977 int ib_uverbs_cleanup_xrc_rcv_qp(struct ib_uverbs_file *file,
2978 struct ib_xrcd *xrcd, u32 qp_num)
2981 err = xrcd->device->unreg_xrc_rcv_qp(xrcd, file, qp_num);
2983 atomic_dec(&xrcd->usecnt);
2987 ssize_t ib_uverbs_unreg_xrc_rcv_qp(struct ib_uverbs_file *file,
2988 const char __user *buf, int in_len,
2991 struct ib_uverbs_unreg_xrc_rcv_qp cmd;
2992 struct ib_uxrc_rcv_object *qp_obj, *tmp;
2993 struct ib_xrcd *xrcd;
2994 struct ib_uobject *uobj;
2995 struct ib_uxrcd_object *xrcd_uobj;
2998 if (copy_from_user(&cmd, buf, sizeof cmd))
3001 xrcd = idr_read_xrcd(cmd.xrc_domain_handle, file->ucontext, &uobj);
3005 ret = xrcd->device->unreg_xrc_rcv_qp(xrcd, file, cmd.qp_num);
3007 put_xrcd_read(uobj);
3010 atomic_dec(&xrcd->usecnt);
3012 xrcd_uobj = container_of(uobj, struct ib_uxrcd_object, uobject);
3013 mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
3014 list_for_each_entry_safe(qp_obj, tmp, &xrcd_uobj->xrc_reg_qp_list, list)
3015 if (cmd.qp_num == qp_obj->qp_num) {
3016 list_del(&qp_obj->list);
3020 mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
3021 put_xrcd_read(uobj);