2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006 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
35 * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
38 #include <linux/file.h>
41 #include <asm/uaccess.h>
45 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
47 (udata)->inbuf = (void __user *) (ibuf); \
48 (udata)->outbuf = (void __user *) (obuf); \
49 (udata)->inlen = (ilen); \
50 (udata)->outlen = (olen); \
53 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
54 const char __user *buf,
55 int in_len, int out_len)
57 struct ib_uverbs_get_context cmd;
58 struct ib_uverbs_get_context_resp resp;
59 struct ib_udata udata;
60 struct ib_device *ibdev = file->device->ib_dev;
61 struct ib_ucontext *ucontext;
65 if (out_len < sizeof resp)
68 if (copy_from_user(&cmd, buf, sizeof cmd))
71 mutex_lock(&file->mutex);
78 INIT_UDATA(&udata, buf + sizeof cmd,
79 (unsigned long) cmd.response + sizeof resp,
80 in_len - sizeof cmd, out_len - sizeof resp);
82 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
83 if (IS_ERR(ucontext)) {
84 ret = PTR_ERR(file->ucontext);
88 ucontext->device = ibdev;
89 INIT_LIST_HEAD(&ucontext->pd_list);
90 INIT_LIST_HEAD(&ucontext->mr_list);
91 INIT_LIST_HEAD(&ucontext->mw_list);
92 INIT_LIST_HEAD(&ucontext->cq_list);
93 INIT_LIST_HEAD(&ucontext->qp_list);
94 INIT_LIST_HEAD(&ucontext->srq_list);
95 INIT_LIST_HEAD(&ucontext->ah_list);
97 resp.num_comp_vectors = file->device->num_comp_vectors;
99 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
105 if (copy_to_user((void __user *) (unsigned long) cmd.response,
106 &resp, sizeof resp)) {
111 file->async_file = filp->private_data;
113 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
114 ib_uverbs_event_handler);
115 ret = ib_register_event_handler(&file->event_handler);
119 kref_get(&file->async_file->ref);
120 kref_get(&file->ref);
121 file->ucontext = ucontext;
123 fd_install(resp.async_fd, filp);
125 mutex_unlock(&file->mutex);
130 put_unused_fd(resp.async_fd);
134 ibdev->dealloc_ucontext(ucontext);
137 mutex_unlock(&file->mutex);
141 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
142 const char __user *buf,
143 int in_len, int out_len)
145 struct ib_uverbs_query_device cmd;
146 struct ib_uverbs_query_device_resp resp;
147 struct ib_device_attr attr;
150 if (out_len < sizeof resp)
153 if (copy_from_user(&cmd, buf, sizeof cmd))
156 ret = ib_query_device(file->device->ib_dev, &attr);
160 memset(&resp, 0, sizeof resp);
162 resp.fw_ver = attr.fw_ver;
163 resp.node_guid = file->device->ib_dev->node_guid;
164 resp.sys_image_guid = attr.sys_image_guid;
165 resp.max_mr_size = attr.max_mr_size;
166 resp.page_size_cap = attr.page_size_cap;
167 resp.vendor_id = attr.vendor_id;
168 resp.vendor_part_id = attr.vendor_part_id;
169 resp.hw_ver = attr.hw_ver;
170 resp.max_qp = attr.max_qp;
171 resp.max_qp_wr = attr.max_qp_wr;
172 resp.device_cap_flags = attr.device_cap_flags;
173 resp.max_sge = attr.max_sge;
174 resp.max_sge_rd = attr.max_sge_rd;
175 resp.max_cq = attr.max_cq;
176 resp.max_cqe = attr.max_cqe;
177 resp.max_mr = attr.max_mr;
178 resp.max_pd = attr.max_pd;
179 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
180 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
181 resp.max_res_rd_atom = attr.max_res_rd_atom;
182 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
183 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
184 resp.atomic_cap = attr.atomic_cap;
185 resp.max_ee = attr.max_ee;
186 resp.max_rdd = attr.max_rdd;
187 resp.max_mw = attr.max_mw;
188 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
189 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
190 resp.max_mcast_grp = attr.max_mcast_grp;
191 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
192 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
193 resp.max_ah = attr.max_ah;
194 resp.max_fmr = attr.max_fmr;
195 resp.max_map_per_fmr = attr.max_map_per_fmr;
196 resp.max_srq = attr.max_srq;
197 resp.max_srq_wr = attr.max_srq_wr;
198 resp.max_srq_sge = attr.max_srq_sge;
199 resp.max_pkeys = attr.max_pkeys;
200 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
201 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
203 if (copy_to_user((void __user *) (unsigned long) cmd.response,
210 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
211 const char __user *buf,
212 int in_len, int out_len)
214 struct ib_uverbs_query_port cmd;
215 struct ib_uverbs_query_port_resp resp;
216 struct ib_port_attr attr;
219 if (out_len < sizeof resp)
222 if (copy_from_user(&cmd, buf, sizeof cmd))
225 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
229 memset(&resp, 0, sizeof resp);
231 resp.state = attr.state;
232 resp.max_mtu = attr.max_mtu;
233 resp.active_mtu = attr.active_mtu;
234 resp.gid_tbl_len = attr.gid_tbl_len;
235 resp.port_cap_flags = attr.port_cap_flags;
236 resp.max_msg_sz = attr.max_msg_sz;
237 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
238 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
239 resp.pkey_tbl_len = attr.pkey_tbl_len;
241 resp.sm_lid = attr.sm_lid;
243 resp.max_vl_num = attr.max_vl_num;
244 resp.sm_sl = attr.sm_sl;
245 resp.subnet_timeout = attr.subnet_timeout;
246 resp.init_type_reply = attr.init_type_reply;
247 resp.active_width = attr.active_width;
248 resp.active_speed = attr.active_speed;
249 resp.phys_state = attr.phys_state;
251 if (copy_to_user((void __user *) (unsigned long) cmd.response,
258 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
259 const char __user *buf,
260 int in_len, int out_len)
262 struct ib_uverbs_alloc_pd cmd;
263 struct ib_uverbs_alloc_pd_resp resp;
264 struct ib_udata udata;
265 struct ib_uobject *uobj;
269 if (out_len < sizeof resp)
272 if (copy_from_user(&cmd, buf, sizeof cmd))
275 INIT_UDATA(&udata, buf + sizeof cmd,
276 (unsigned long) cmd.response + sizeof resp,
277 in_len - sizeof cmd, out_len - sizeof resp);
279 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
283 uobj->context = file->ucontext;
285 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
286 file->ucontext, &udata);
292 pd->device = file->device->ib_dev;
294 atomic_set(&pd->usecnt, 0);
296 mutex_lock(&ib_uverbs_idr_mutex);
299 if (!idr_pre_get(&ib_uverbs_pd_idr, GFP_KERNEL)) {
304 ret = idr_get_new(&ib_uverbs_pd_idr, pd, &uobj->id);
311 memset(&resp, 0, sizeof resp);
312 resp.pd_handle = uobj->id;
314 if (copy_to_user((void __user *) (unsigned long) cmd.response,
315 &resp, sizeof resp)) {
320 mutex_lock(&file->mutex);
321 list_add_tail(&uobj->list, &file->ucontext->pd_list);
322 mutex_unlock(&file->mutex);
324 mutex_unlock(&ib_uverbs_idr_mutex);
329 idr_remove(&ib_uverbs_pd_idr, uobj->id);
332 mutex_unlock(&ib_uverbs_idr_mutex);
340 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
341 const char __user *buf,
342 int in_len, int out_len)
344 struct ib_uverbs_dealloc_pd cmd;
346 struct ib_uobject *uobj;
349 if (copy_from_user(&cmd, buf, sizeof cmd))
352 mutex_lock(&ib_uverbs_idr_mutex);
354 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
355 if (!pd || pd->uobject->context != file->ucontext)
360 ret = ib_dealloc_pd(pd);
364 idr_remove(&ib_uverbs_pd_idr, cmd.pd_handle);
366 mutex_lock(&file->mutex);
367 list_del(&uobj->list);
368 mutex_unlock(&file->mutex);
373 mutex_unlock(&ib_uverbs_idr_mutex);
375 return ret ? ret : in_len;
378 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
379 const char __user *buf, int in_len,
382 struct ib_uverbs_reg_mr cmd;
383 struct ib_uverbs_reg_mr_resp resp;
384 struct ib_udata udata;
385 struct ib_umem_object *obj;
390 if (out_len < sizeof resp)
393 if (copy_from_user(&cmd, buf, sizeof cmd))
396 INIT_UDATA(&udata, buf + sizeof cmd,
397 (unsigned long) cmd.response + sizeof resp,
398 in_len - sizeof cmd, out_len - sizeof resp);
400 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
404 * Local write permission is required if remote write or
405 * remote atomic permission is also requested.
407 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
408 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
411 obj = kmalloc(sizeof *obj, GFP_KERNEL);
415 obj->uobject.context = file->ucontext;
418 * We ask for writable memory if any access flags other than
419 * "remote read" are set. "Local write" and "remote write"
420 * obviously require write access. "Remote atomic" can do
421 * things like fetch and add, which will modify memory, and
422 * "MW bind" can change permissions by binding a window.
424 ret = ib_umem_get(file->device->ib_dev, &obj->umem,
425 (void *) (unsigned long) cmd.start, cmd.length,
426 !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
430 obj->umem.virt_base = cmd.hca_va;
432 mutex_lock(&ib_uverbs_idr_mutex);
434 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
435 if (!pd || pd->uobject->context != file->ucontext) {
440 if (!pd->device->reg_user_mr) {
445 mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
451 mr->device = pd->device;
453 mr->uobject = &obj->uobject;
454 atomic_inc(&pd->usecnt);
455 atomic_set(&mr->usecnt, 0);
457 memset(&resp, 0, sizeof resp);
458 resp.lkey = mr->lkey;
459 resp.rkey = mr->rkey;
462 if (!idr_pre_get(&ib_uverbs_mr_idr, GFP_KERNEL)) {
467 ret = idr_get_new(&ib_uverbs_mr_idr, mr, &obj->uobject.id);
474 resp.mr_handle = obj->uobject.id;
476 if (copy_to_user((void __user *) (unsigned long) cmd.response,
477 &resp, sizeof resp)) {
482 mutex_lock(&file->mutex);
483 list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
484 mutex_unlock(&file->mutex);
486 mutex_unlock(&ib_uverbs_idr_mutex);
491 idr_remove(&ib_uverbs_mr_idr, obj->uobject.id);
497 mutex_unlock(&ib_uverbs_idr_mutex);
499 ib_umem_release(file->device->ib_dev, &obj->umem);
506 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
507 const char __user *buf, int in_len,
510 struct ib_uverbs_dereg_mr cmd;
512 struct ib_umem_object *memobj;
515 if (copy_from_user(&cmd, buf, sizeof cmd))
518 mutex_lock(&ib_uverbs_idr_mutex);
520 mr = idr_find(&ib_uverbs_mr_idr, cmd.mr_handle);
521 if (!mr || mr->uobject->context != file->ucontext)
524 memobj = container_of(mr->uobject, struct ib_umem_object, uobject);
526 ret = ib_dereg_mr(mr);
530 idr_remove(&ib_uverbs_mr_idr, cmd.mr_handle);
532 mutex_lock(&file->mutex);
533 list_del(&memobj->uobject.list);
534 mutex_unlock(&file->mutex);
536 ib_umem_release(file->device->ib_dev, &memobj->umem);
540 mutex_unlock(&ib_uverbs_idr_mutex);
542 return ret ? ret : in_len;
545 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
546 const char __user *buf, int in_len,
549 struct ib_uverbs_create_comp_channel cmd;
550 struct ib_uverbs_create_comp_channel_resp resp;
553 if (out_len < sizeof resp)
556 if (copy_from_user(&cmd, buf, sizeof cmd))
559 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
561 return PTR_ERR(filp);
563 if (copy_to_user((void __user *) (unsigned long) cmd.response,
564 &resp, sizeof resp)) {
565 put_unused_fd(resp.fd);
570 fd_install(resp.fd, filp);
574 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
575 const char __user *buf, int in_len,
578 struct ib_uverbs_create_cq cmd;
579 struct ib_uverbs_create_cq_resp resp;
580 struct ib_udata udata;
581 struct ib_ucq_object *uobj;
582 struct ib_uverbs_event_file *ev_file = NULL;
586 if (out_len < sizeof resp)
589 if (copy_from_user(&cmd, buf, sizeof cmd))
592 INIT_UDATA(&udata, buf + sizeof cmd,
593 (unsigned long) cmd.response + sizeof resp,
594 in_len - sizeof cmd, out_len - sizeof resp);
596 if (cmd.comp_vector >= file->device->num_comp_vectors)
599 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
603 if (cmd.comp_channel >= 0) {
604 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
611 uobj->uobject.user_handle = cmd.user_handle;
612 uobj->uobject.context = file->ucontext;
613 uobj->uverbs_file = file;
614 uobj->comp_events_reported = 0;
615 uobj->async_events_reported = 0;
616 INIT_LIST_HEAD(&uobj->comp_list);
617 INIT_LIST_HEAD(&uobj->async_list);
619 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
620 file->ucontext, &udata);
626 cq->device = file->device->ib_dev;
627 cq->uobject = &uobj->uobject;
628 cq->comp_handler = ib_uverbs_comp_handler;
629 cq->event_handler = ib_uverbs_cq_event_handler;
630 cq->cq_context = ev_file;
631 atomic_set(&cq->usecnt, 0);
633 mutex_lock(&ib_uverbs_idr_mutex);
636 if (!idr_pre_get(&ib_uverbs_cq_idr, GFP_KERNEL)) {
641 ret = idr_get_new(&ib_uverbs_cq_idr, cq, &uobj->uobject.id);
648 memset(&resp, 0, sizeof resp);
649 resp.cq_handle = uobj->uobject.id;
652 if (copy_to_user((void __user *) (unsigned long) cmd.response,
653 &resp, sizeof resp)) {
658 mutex_lock(&file->mutex);
659 list_add_tail(&uobj->uobject.list, &file->ucontext->cq_list);
660 mutex_unlock(&file->mutex);
662 mutex_unlock(&ib_uverbs_idr_mutex);
667 idr_remove(&ib_uverbs_cq_idr, uobj->uobject.id);
670 mutex_unlock(&ib_uverbs_idr_mutex);
675 ib_uverbs_release_ucq(file, ev_file, uobj);
680 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
681 const char __user *buf, int in_len,
684 struct ib_uverbs_resize_cq cmd;
685 struct ib_uverbs_resize_cq_resp resp;
686 struct ib_udata udata;
690 if (copy_from_user(&cmd, buf, sizeof cmd))
693 INIT_UDATA(&udata, buf + sizeof cmd,
694 (unsigned long) cmd.response + sizeof resp,
695 in_len - sizeof cmd, out_len - sizeof resp);
697 mutex_lock(&ib_uverbs_idr_mutex);
699 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
700 if (!cq || cq->uobject->context != file->ucontext || !cq->device->resize_cq)
703 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
707 memset(&resp, 0, sizeof resp);
710 if (copy_to_user((void __user *) (unsigned long) cmd.response,
715 mutex_unlock(&ib_uverbs_idr_mutex);
717 return ret ? ret : in_len;
720 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
721 const char __user *buf, int in_len,
724 struct ib_uverbs_poll_cq cmd;
725 struct ib_uverbs_poll_cq_resp *resp;
732 if (copy_from_user(&cmd, buf, sizeof cmd))
735 wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
739 rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
740 resp = kmalloc(rsize, GFP_KERNEL);
746 mutex_lock(&ib_uverbs_idr_mutex);
747 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
748 if (!cq || cq->uobject->context != file->ucontext) {
753 resp->count = ib_poll_cq(cq, cmd.ne, wc);
755 for (i = 0; i < resp->count; i++) {
756 resp->wc[i].wr_id = wc[i].wr_id;
757 resp->wc[i].status = wc[i].status;
758 resp->wc[i].opcode = wc[i].opcode;
759 resp->wc[i].vendor_err = wc[i].vendor_err;
760 resp->wc[i].byte_len = wc[i].byte_len;
761 resp->wc[i].imm_data = (__u32 __force) wc[i].imm_data;
762 resp->wc[i].qp_num = wc[i].qp_num;
763 resp->wc[i].src_qp = wc[i].src_qp;
764 resp->wc[i].wc_flags = wc[i].wc_flags;
765 resp->wc[i].pkey_index = wc[i].pkey_index;
766 resp->wc[i].slid = wc[i].slid;
767 resp->wc[i].sl = wc[i].sl;
768 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
769 resp->wc[i].port_num = wc[i].port_num;
772 if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
776 mutex_unlock(&ib_uverbs_idr_mutex);
781 return ret ? ret : in_len;
784 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
785 const char __user *buf, int in_len,
788 struct ib_uverbs_req_notify_cq cmd;
792 if (copy_from_user(&cmd, buf, sizeof cmd))
795 mutex_lock(&ib_uverbs_idr_mutex);
796 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
797 if (cq && cq->uobject->context == file->ucontext) {
798 ib_req_notify_cq(cq, cmd.solicited_only ?
799 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
802 mutex_unlock(&ib_uverbs_idr_mutex);
807 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
808 const char __user *buf, int in_len,
811 struct ib_uverbs_destroy_cq cmd;
812 struct ib_uverbs_destroy_cq_resp resp;
814 struct ib_ucq_object *uobj;
815 struct ib_uverbs_event_file *ev_file;
819 if (copy_from_user(&cmd, buf, sizeof cmd))
822 memset(&resp, 0, sizeof resp);
824 mutex_lock(&ib_uverbs_idr_mutex);
826 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
827 if (!cq || cq->uobject->context != file->ucontext)
830 user_handle = cq->uobject->user_handle;
831 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
832 ev_file = cq->cq_context;
834 ret = ib_destroy_cq(cq);
838 idr_remove(&ib_uverbs_cq_idr, cmd.cq_handle);
840 mutex_lock(&file->mutex);
841 list_del(&uobj->uobject.list);
842 mutex_unlock(&file->mutex);
844 ib_uverbs_release_ucq(file, ev_file, uobj);
846 resp.comp_events_reported = uobj->comp_events_reported;
847 resp.async_events_reported = uobj->async_events_reported;
851 if (copy_to_user((void __user *) (unsigned long) cmd.response,
856 mutex_unlock(&ib_uverbs_idr_mutex);
858 return ret ? ret : in_len;
861 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
862 const char __user *buf, int in_len,
865 struct ib_uverbs_create_qp cmd;
866 struct ib_uverbs_create_qp_resp resp;
867 struct ib_udata udata;
868 struct ib_uqp_object *uobj;
870 struct ib_cq *scq, *rcq;
873 struct ib_qp_init_attr attr;
876 if (out_len < sizeof resp)
879 if (copy_from_user(&cmd, buf, sizeof cmd))
882 INIT_UDATA(&udata, buf + sizeof cmd,
883 (unsigned long) cmd.response + sizeof resp,
884 in_len - sizeof cmd, out_len - sizeof resp);
886 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
890 mutex_lock(&ib_uverbs_idr_mutex);
892 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
893 scq = idr_find(&ib_uverbs_cq_idr, cmd.send_cq_handle);
894 rcq = idr_find(&ib_uverbs_cq_idr, cmd.recv_cq_handle);
895 srq = cmd.is_srq ? idr_find(&ib_uverbs_srq_idr, cmd.srq_handle) : NULL;
897 if (!pd || pd->uobject->context != file->ucontext ||
898 !scq || scq->uobject->context != file->ucontext ||
899 !rcq || rcq->uobject->context != file->ucontext ||
900 (cmd.is_srq && (!srq || srq->uobject->context != file->ucontext))) {
905 attr.event_handler = ib_uverbs_qp_event_handler;
906 attr.qp_context = file;
910 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
911 attr.qp_type = cmd.qp_type;
913 attr.cap.max_send_wr = cmd.max_send_wr;
914 attr.cap.max_recv_wr = cmd.max_recv_wr;
915 attr.cap.max_send_sge = cmd.max_send_sge;
916 attr.cap.max_recv_sge = cmd.max_recv_sge;
917 attr.cap.max_inline_data = cmd.max_inline_data;
919 uobj->uevent.uobject.user_handle = cmd.user_handle;
920 uobj->uevent.uobject.context = file->ucontext;
921 uobj->uevent.events_reported = 0;
922 INIT_LIST_HEAD(&uobj->uevent.event_list);
923 INIT_LIST_HEAD(&uobj->mcast_list);
925 qp = pd->device->create_qp(pd, &attr, &udata);
931 qp->device = pd->device;
933 qp->send_cq = attr.send_cq;
934 qp->recv_cq = attr.recv_cq;
936 qp->uobject = &uobj->uevent.uobject;
937 qp->event_handler = attr.event_handler;
938 qp->qp_context = attr.qp_context;
939 qp->qp_type = attr.qp_type;
940 atomic_inc(&pd->usecnt);
941 atomic_inc(&attr.send_cq->usecnt);
942 atomic_inc(&attr.recv_cq->usecnt);
944 atomic_inc(&attr.srq->usecnt);
946 memset(&resp, 0, sizeof resp);
947 resp.qpn = qp->qp_num;
950 if (!idr_pre_get(&ib_uverbs_qp_idr, GFP_KERNEL)) {
955 ret = idr_get_new(&ib_uverbs_qp_idr, qp, &uobj->uevent.uobject.id);
962 resp.qp_handle = uobj->uevent.uobject.id;
963 resp.max_recv_sge = attr.cap.max_recv_sge;
964 resp.max_send_sge = attr.cap.max_send_sge;
965 resp.max_recv_wr = attr.cap.max_recv_wr;
966 resp.max_send_wr = attr.cap.max_send_wr;
967 resp.max_inline_data = attr.cap.max_inline_data;
969 if (copy_to_user((void __user *) (unsigned long) cmd.response,
970 &resp, sizeof resp)) {
975 mutex_lock(&file->mutex);
976 list_add_tail(&uobj->uevent.uobject.list, &file->ucontext->qp_list);
977 mutex_unlock(&file->mutex);
979 mutex_unlock(&ib_uverbs_idr_mutex);
984 idr_remove(&ib_uverbs_qp_idr, uobj->uevent.uobject.id);
990 mutex_unlock(&ib_uverbs_idr_mutex);
996 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
997 const char __user *buf, int in_len,
1000 struct ib_uverbs_query_qp cmd;
1001 struct ib_uverbs_query_qp_resp resp;
1003 struct ib_qp_attr *attr;
1004 struct ib_qp_init_attr *init_attr;
1007 if (copy_from_user(&cmd, buf, sizeof cmd))
1010 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1011 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1012 if (!attr || !init_attr) {
1017 mutex_lock(&ib_uverbs_idr_mutex);
1019 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1020 if (qp && qp->uobject->context == file->ucontext)
1021 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1025 mutex_unlock(&ib_uverbs_idr_mutex);
1030 memset(&resp, 0, sizeof resp);
1032 resp.qp_state = attr->qp_state;
1033 resp.cur_qp_state = attr->cur_qp_state;
1034 resp.path_mtu = attr->path_mtu;
1035 resp.path_mig_state = attr->path_mig_state;
1036 resp.qkey = attr->qkey;
1037 resp.rq_psn = attr->rq_psn;
1038 resp.sq_psn = attr->sq_psn;
1039 resp.dest_qp_num = attr->dest_qp_num;
1040 resp.qp_access_flags = attr->qp_access_flags;
1041 resp.pkey_index = attr->pkey_index;
1042 resp.alt_pkey_index = attr->alt_pkey_index;
1043 resp.en_sqd_async_notify = attr->en_sqd_async_notify;
1044 resp.max_rd_atomic = attr->max_rd_atomic;
1045 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1046 resp.min_rnr_timer = attr->min_rnr_timer;
1047 resp.port_num = attr->port_num;
1048 resp.timeout = attr->timeout;
1049 resp.retry_cnt = attr->retry_cnt;
1050 resp.rnr_retry = attr->rnr_retry;
1051 resp.alt_port_num = attr->alt_port_num;
1052 resp.alt_timeout = attr->alt_timeout;
1054 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1055 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1056 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1057 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1058 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1059 resp.dest.dlid = attr->ah_attr.dlid;
1060 resp.dest.sl = attr->ah_attr.sl;
1061 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1062 resp.dest.static_rate = attr->ah_attr.static_rate;
1063 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1064 resp.dest.port_num = attr->ah_attr.port_num;
1066 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1067 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1068 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1069 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1070 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1071 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1072 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1073 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1074 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1075 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1076 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1078 resp.max_send_wr = init_attr->cap.max_send_wr;
1079 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1080 resp.max_send_sge = init_attr->cap.max_send_sge;
1081 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1082 resp.max_inline_data = init_attr->cap.max_inline_data;
1083 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1085 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1086 &resp, sizeof resp))
1093 return ret ? ret : in_len;
1096 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1097 const char __user *buf, int in_len,
1100 struct ib_uverbs_modify_qp cmd;
1102 struct ib_qp_attr *attr;
1105 if (copy_from_user(&cmd, buf, sizeof cmd))
1108 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1112 mutex_lock(&ib_uverbs_idr_mutex);
1114 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1115 if (!qp || qp->uobject->context != file->ucontext) {
1120 attr->qp_state = cmd.qp_state;
1121 attr->cur_qp_state = cmd.cur_qp_state;
1122 attr->path_mtu = cmd.path_mtu;
1123 attr->path_mig_state = cmd.path_mig_state;
1124 attr->qkey = cmd.qkey;
1125 attr->rq_psn = cmd.rq_psn;
1126 attr->sq_psn = cmd.sq_psn;
1127 attr->dest_qp_num = cmd.dest_qp_num;
1128 attr->qp_access_flags = cmd.qp_access_flags;
1129 attr->pkey_index = cmd.pkey_index;
1130 attr->alt_pkey_index = cmd.alt_pkey_index;
1131 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1132 attr->max_rd_atomic = cmd.max_rd_atomic;
1133 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1134 attr->min_rnr_timer = cmd.min_rnr_timer;
1135 attr->port_num = cmd.port_num;
1136 attr->timeout = cmd.timeout;
1137 attr->retry_cnt = cmd.retry_cnt;
1138 attr->rnr_retry = cmd.rnr_retry;
1139 attr->alt_port_num = cmd.alt_port_num;
1140 attr->alt_timeout = cmd.alt_timeout;
1142 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1143 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1144 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1145 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1146 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1147 attr->ah_attr.dlid = cmd.dest.dlid;
1148 attr->ah_attr.sl = cmd.dest.sl;
1149 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1150 attr->ah_attr.static_rate = cmd.dest.static_rate;
1151 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1152 attr->ah_attr.port_num = cmd.dest.port_num;
1154 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1155 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1156 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1157 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1158 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1159 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1160 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1161 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1162 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1163 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1164 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1166 ret = ib_modify_qp(qp, attr, cmd.attr_mask);
1173 mutex_unlock(&ib_uverbs_idr_mutex);
1179 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1180 const char __user *buf, int in_len,
1183 struct ib_uverbs_destroy_qp cmd;
1184 struct ib_uverbs_destroy_qp_resp resp;
1186 struct ib_uqp_object *uobj;
1189 if (copy_from_user(&cmd, buf, sizeof cmd))
1192 memset(&resp, 0, sizeof resp);
1194 mutex_lock(&ib_uverbs_idr_mutex);
1196 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1197 if (!qp || qp->uobject->context != file->ucontext)
1200 uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1202 if (!list_empty(&uobj->mcast_list)) {
1207 ret = ib_destroy_qp(qp);
1211 idr_remove(&ib_uverbs_qp_idr, cmd.qp_handle);
1213 mutex_lock(&file->mutex);
1214 list_del(&uobj->uevent.uobject.list);
1215 mutex_unlock(&file->mutex);
1217 ib_uverbs_release_uevent(file, &uobj->uevent);
1219 resp.events_reported = uobj->uevent.events_reported;
1223 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1224 &resp, sizeof resp))
1228 mutex_unlock(&ib_uverbs_idr_mutex);
1230 return ret ? ret : in_len;
1233 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1234 const char __user *buf, int in_len,
1237 struct ib_uverbs_post_send cmd;
1238 struct ib_uverbs_post_send_resp resp;
1239 struct ib_uverbs_send_wr *user_wr;
1240 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
1243 ssize_t ret = -EINVAL;
1245 if (copy_from_user(&cmd, buf, sizeof cmd))
1248 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1249 cmd.sge_count * sizeof (struct ib_uverbs_sge))
1252 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1255 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1259 mutex_lock(&ib_uverbs_idr_mutex);
1261 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1262 if (!qp || qp->uobject->context != file->ucontext)
1267 for (i = 0; i < cmd.wr_count; ++i) {
1268 if (copy_from_user(user_wr,
1269 buf + sizeof cmd + i * cmd.wqe_size,
1275 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1280 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1281 user_wr->num_sge * sizeof (struct ib_sge),
1295 next->wr_id = user_wr->wr_id;
1296 next->num_sge = user_wr->num_sge;
1297 next->opcode = user_wr->opcode;
1298 next->send_flags = user_wr->send_flags;
1299 next->imm_data = (__be32 __force) user_wr->imm_data;
1301 if (qp->qp_type == IB_QPT_UD) {
1302 next->wr.ud.ah = idr_find(&ib_uverbs_ah_idr,
1304 if (!next->wr.ud.ah) {
1308 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
1309 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1311 switch (next->opcode) {
1312 case IB_WR_RDMA_WRITE:
1313 case IB_WR_RDMA_WRITE_WITH_IMM:
1314 case IB_WR_RDMA_READ:
1315 next->wr.rdma.remote_addr =
1316 user_wr->wr.rdma.remote_addr;
1317 next->wr.rdma.rkey =
1318 user_wr->wr.rdma.rkey;
1320 case IB_WR_ATOMIC_CMP_AND_SWP:
1321 case IB_WR_ATOMIC_FETCH_AND_ADD:
1322 next->wr.atomic.remote_addr =
1323 user_wr->wr.atomic.remote_addr;
1324 next->wr.atomic.compare_add =
1325 user_wr->wr.atomic.compare_add;
1326 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1327 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1334 if (next->num_sge) {
1335 next->sg_list = (void *) next +
1336 ALIGN(sizeof *next, sizeof (struct ib_sge));
1337 if (copy_from_user(next->sg_list,
1339 cmd.wr_count * cmd.wqe_size +
1340 sg_ind * sizeof (struct ib_sge),
1341 next->num_sge * sizeof (struct ib_sge))) {
1345 sg_ind += next->num_sge;
1347 next->sg_list = NULL;
1351 ret = qp->device->post_send(qp, wr, &bad_wr);
1353 for (next = wr; next; next = next->next) {
1359 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1360 &resp, sizeof resp))
1364 mutex_unlock(&ib_uverbs_idr_mutex);
1374 return ret ? ret : in_len;
1377 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1383 struct ib_uverbs_recv_wr *user_wr;
1384 struct ib_recv_wr *wr = NULL, *last, *next;
1389 if (in_len < wqe_size * wr_count +
1390 sge_count * sizeof (struct ib_uverbs_sge))
1391 return ERR_PTR(-EINVAL);
1393 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1394 return ERR_PTR(-EINVAL);
1396 user_wr = kmalloc(wqe_size, GFP_KERNEL);
1398 return ERR_PTR(-ENOMEM);
1402 for (i = 0; i < wr_count; ++i) {
1403 if (copy_from_user(user_wr, buf + i * wqe_size,
1409 if (user_wr->num_sge + sg_ind > sge_count) {
1414 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1415 user_wr->num_sge * sizeof (struct ib_sge),
1429 next->wr_id = user_wr->wr_id;
1430 next->num_sge = user_wr->num_sge;
1432 if (next->num_sge) {
1433 next->sg_list = (void *) next +
1434 ALIGN(sizeof *next, sizeof (struct ib_sge));
1435 if (copy_from_user(next->sg_list,
1436 buf + wr_count * wqe_size +
1437 sg_ind * sizeof (struct ib_sge),
1438 next->num_sge * sizeof (struct ib_sge))) {
1442 sg_ind += next->num_sge;
1444 next->sg_list = NULL;
1459 return ERR_PTR(ret);
1462 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1463 const char __user *buf, int in_len,
1466 struct ib_uverbs_post_recv cmd;
1467 struct ib_uverbs_post_recv_resp resp;
1468 struct ib_recv_wr *wr, *next, *bad_wr;
1470 ssize_t ret = -EINVAL;
1472 if (copy_from_user(&cmd, buf, sizeof cmd))
1475 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1476 in_len - sizeof cmd, cmd.wr_count,
1477 cmd.sge_count, cmd.wqe_size);
1481 mutex_lock(&ib_uverbs_idr_mutex);
1483 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1484 if (!qp || qp->uobject->context != file->ucontext)
1488 ret = qp->device->post_recv(qp, wr, &bad_wr);
1490 for (next = wr; next; next = next->next) {
1497 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1498 &resp, sizeof resp))
1502 mutex_unlock(&ib_uverbs_idr_mutex);
1510 return ret ? ret : in_len;
1513 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1514 const char __user *buf, int in_len,
1517 struct ib_uverbs_post_srq_recv cmd;
1518 struct ib_uverbs_post_srq_recv_resp resp;
1519 struct ib_recv_wr *wr, *next, *bad_wr;
1521 ssize_t ret = -EINVAL;
1523 if (copy_from_user(&cmd, buf, sizeof cmd))
1526 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1527 in_len - sizeof cmd, cmd.wr_count,
1528 cmd.sge_count, cmd.wqe_size);
1532 mutex_lock(&ib_uverbs_idr_mutex);
1534 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1535 if (!srq || srq->uobject->context != file->ucontext)
1539 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1541 for (next = wr; next; next = next->next) {
1548 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1549 &resp, sizeof resp))
1553 mutex_unlock(&ib_uverbs_idr_mutex);
1561 return ret ? ret : in_len;
1564 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1565 const char __user *buf, int in_len,
1568 struct ib_uverbs_create_ah cmd;
1569 struct ib_uverbs_create_ah_resp resp;
1570 struct ib_uobject *uobj;
1573 struct ib_ah_attr attr;
1576 if (out_len < sizeof resp)
1579 if (copy_from_user(&cmd, buf, sizeof cmd))
1582 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1586 mutex_lock(&ib_uverbs_idr_mutex);
1588 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
1589 if (!pd || pd->uobject->context != file->ucontext) {
1594 uobj->user_handle = cmd.user_handle;
1595 uobj->context = file->ucontext;
1597 attr.dlid = cmd.attr.dlid;
1598 attr.sl = cmd.attr.sl;
1599 attr.src_path_bits = cmd.attr.src_path_bits;
1600 attr.static_rate = cmd.attr.static_rate;
1601 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
1602 attr.port_num = cmd.attr.port_num;
1603 attr.grh.flow_label = cmd.attr.grh.flow_label;
1604 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
1605 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
1606 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1607 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1609 ah = ib_create_ah(pd, &attr);
1618 if (!idr_pre_get(&ib_uverbs_ah_idr, GFP_KERNEL)) {
1623 ret = idr_get_new(&ib_uverbs_ah_idr, ah, &uobj->id);
1630 resp.ah_handle = uobj->id;
1632 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1633 &resp, sizeof resp)) {
1638 mutex_lock(&file->mutex);
1639 list_add_tail(&uobj->list, &file->ucontext->ah_list);
1640 mutex_unlock(&file->mutex);
1642 mutex_unlock(&ib_uverbs_idr_mutex);
1647 idr_remove(&ib_uverbs_ah_idr, uobj->id);
1653 mutex_unlock(&ib_uverbs_idr_mutex);
1659 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1660 const char __user *buf, int in_len, int out_len)
1662 struct ib_uverbs_destroy_ah cmd;
1664 struct ib_uobject *uobj;
1667 if (copy_from_user(&cmd, buf, sizeof cmd))
1670 mutex_lock(&ib_uverbs_idr_mutex);
1672 ah = idr_find(&ib_uverbs_ah_idr, cmd.ah_handle);
1673 if (!ah || ah->uobject->context != file->ucontext)
1678 ret = ib_destroy_ah(ah);
1682 idr_remove(&ib_uverbs_ah_idr, cmd.ah_handle);
1684 mutex_lock(&file->mutex);
1685 list_del(&uobj->list);
1686 mutex_unlock(&file->mutex);
1691 mutex_unlock(&ib_uverbs_idr_mutex);
1693 return ret ? ret : in_len;
1696 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1697 const char __user *buf, int in_len,
1700 struct ib_uverbs_attach_mcast cmd;
1702 struct ib_uqp_object *uobj;
1703 struct ib_uverbs_mcast_entry *mcast;
1706 if (copy_from_user(&cmd, buf, sizeof cmd))
1709 mutex_lock(&ib_uverbs_idr_mutex);
1711 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1712 if (!qp || qp->uobject->context != file->ucontext)
1715 uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1717 list_for_each_entry(mcast, &uobj->mcast_list, list)
1718 if (cmd.mlid == mcast->lid &&
1719 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1724 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1730 mcast->lid = cmd.mlid;
1731 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1733 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1735 uobj = container_of(qp->uobject, struct ib_uqp_object,
1737 list_add_tail(&mcast->list, &uobj->mcast_list);
1742 mutex_unlock(&ib_uverbs_idr_mutex);
1744 return ret ? ret : in_len;
1747 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1748 const char __user *buf, int in_len,
1751 struct ib_uverbs_detach_mcast cmd;
1752 struct ib_uqp_object *uobj;
1754 struct ib_uverbs_mcast_entry *mcast;
1757 if (copy_from_user(&cmd, buf, sizeof cmd))
1760 mutex_lock(&ib_uverbs_idr_mutex);
1762 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1763 if (!qp || qp->uobject->context != file->ucontext)
1766 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1770 uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1772 list_for_each_entry(mcast, &uobj->mcast_list, list)
1773 if (cmd.mlid == mcast->lid &&
1774 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1775 list_del(&mcast->list);
1781 mutex_unlock(&ib_uverbs_idr_mutex);
1783 return ret ? ret : in_len;
1786 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1787 const char __user *buf, int in_len,
1790 struct ib_uverbs_create_srq cmd;
1791 struct ib_uverbs_create_srq_resp resp;
1792 struct ib_udata udata;
1793 struct ib_uevent_object *uobj;
1796 struct ib_srq_init_attr attr;
1799 if (out_len < sizeof resp)
1802 if (copy_from_user(&cmd, buf, sizeof cmd))
1805 INIT_UDATA(&udata, buf + sizeof cmd,
1806 (unsigned long) cmd.response + sizeof resp,
1807 in_len - sizeof cmd, out_len - sizeof resp);
1809 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1813 mutex_lock(&ib_uverbs_idr_mutex);
1815 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
1817 if (!pd || pd->uobject->context != file->ucontext) {
1822 attr.event_handler = ib_uverbs_srq_event_handler;
1823 attr.srq_context = file;
1824 attr.attr.max_wr = cmd.max_wr;
1825 attr.attr.max_sge = cmd.max_sge;
1826 attr.attr.srq_limit = cmd.srq_limit;
1828 uobj->uobject.user_handle = cmd.user_handle;
1829 uobj->uobject.context = file->ucontext;
1830 uobj->events_reported = 0;
1831 INIT_LIST_HEAD(&uobj->event_list);
1833 srq = pd->device->create_srq(pd, &attr, &udata);
1839 srq->device = pd->device;
1841 srq->uobject = &uobj->uobject;
1842 srq->event_handler = attr.event_handler;
1843 srq->srq_context = attr.srq_context;
1844 atomic_inc(&pd->usecnt);
1845 atomic_set(&srq->usecnt, 0);
1847 memset(&resp, 0, sizeof resp);
1850 if (!idr_pre_get(&ib_uverbs_srq_idr, GFP_KERNEL)) {
1855 ret = idr_get_new(&ib_uverbs_srq_idr, srq, &uobj->uobject.id);
1862 resp.srq_handle = uobj->uobject.id;
1863 resp.max_wr = attr.attr.max_wr;
1864 resp.max_sge = attr.attr.max_sge;
1866 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1867 &resp, sizeof resp)) {
1872 mutex_lock(&file->mutex);
1873 list_add_tail(&uobj->uobject.list, &file->ucontext->srq_list);
1874 mutex_unlock(&file->mutex);
1876 mutex_unlock(&ib_uverbs_idr_mutex);
1881 idr_remove(&ib_uverbs_srq_idr, uobj->uobject.id);
1884 ib_destroy_srq(srq);
1887 mutex_unlock(&ib_uverbs_idr_mutex);
1893 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
1894 const char __user *buf, int in_len,
1897 struct ib_uverbs_modify_srq cmd;
1899 struct ib_srq_attr attr;
1902 if (copy_from_user(&cmd, buf, sizeof cmd))
1905 mutex_lock(&ib_uverbs_idr_mutex);
1907 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1908 if (!srq || srq->uobject->context != file->ucontext) {
1913 attr.max_wr = cmd.max_wr;
1914 attr.srq_limit = cmd.srq_limit;
1916 ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
1919 mutex_unlock(&ib_uverbs_idr_mutex);
1921 return ret ? ret : in_len;
1924 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
1925 const char __user *buf,
1926 int in_len, int out_len)
1928 struct ib_uverbs_query_srq cmd;
1929 struct ib_uverbs_query_srq_resp resp;
1930 struct ib_srq_attr attr;
1934 if (out_len < sizeof resp)
1937 if (copy_from_user(&cmd, buf, sizeof cmd))
1940 mutex_lock(&ib_uverbs_idr_mutex);
1942 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1943 if (srq && srq->uobject->context == file->ucontext)
1944 ret = ib_query_srq(srq, &attr);
1948 mutex_unlock(&ib_uverbs_idr_mutex);
1953 memset(&resp, 0, sizeof resp);
1955 resp.max_wr = attr.max_wr;
1956 resp.max_sge = attr.max_sge;
1957 resp.srq_limit = attr.srq_limit;
1959 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1960 &resp, sizeof resp))
1964 return ret ? ret : in_len;
1967 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
1968 const char __user *buf, int in_len,
1971 struct ib_uverbs_destroy_srq cmd;
1972 struct ib_uverbs_destroy_srq_resp resp;
1974 struct ib_uevent_object *uobj;
1977 if (copy_from_user(&cmd, buf, sizeof cmd))
1980 mutex_lock(&ib_uverbs_idr_mutex);
1982 memset(&resp, 0, sizeof resp);
1984 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1985 if (!srq || srq->uobject->context != file->ucontext)
1988 uobj = container_of(srq->uobject, struct ib_uevent_object, uobject);
1990 ret = ib_destroy_srq(srq);
1994 idr_remove(&ib_uverbs_srq_idr, cmd.srq_handle);
1996 mutex_lock(&file->mutex);
1997 list_del(&uobj->uobject.list);
1998 mutex_unlock(&file->mutex);
2000 ib_uverbs_release_uevent(file, uobj);
2002 resp.events_reported = uobj->events_reported;
2006 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2007 &resp, sizeof resp))
2011 mutex_unlock(&ib_uverbs_idr_mutex);
2013 return ret ? ret : in_len;