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.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
37 #include <linux/file.h>
40 #include <asm/uaccess.h>
44 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
46 (udata)->inbuf = (void __user *) (ibuf); \
47 (udata)->outbuf = (void __user *) (obuf); \
48 (udata)->inlen = (ilen); \
49 (udata)->outlen = (olen); \
52 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
53 const char __user *buf,
54 int in_len, int out_len)
56 struct ib_uverbs_get_context cmd;
57 struct ib_uverbs_get_context_resp resp;
58 struct ib_udata udata;
59 struct ib_device *ibdev = file->device->ib_dev;
60 struct ib_ucontext *ucontext;
64 if (out_len < sizeof resp)
67 if (copy_from_user(&cmd, buf, sizeof cmd))
70 mutex_lock(&file->mutex);
77 INIT_UDATA(&udata, buf + sizeof cmd,
78 (unsigned long) cmd.response + sizeof resp,
79 in_len - sizeof cmd, out_len - sizeof resp);
81 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
83 return PTR_ERR(file->ucontext);
85 ucontext->device = ibdev;
86 INIT_LIST_HEAD(&ucontext->pd_list);
87 INIT_LIST_HEAD(&ucontext->mr_list);
88 INIT_LIST_HEAD(&ucontext->mw_list);
89 INIT_LIST_HEAD(&ucontext->cq_list);
90 INIT_LIST_HEAD(&ucontext->qp_list);
91 INIT_LIST_HEAD(&ucontext->srq_list);
92 INIT_LIST_HEAD(&ucontext->ah_list);
94 resp.num_comp_vectors = file->device->num_comp_vectors;
96 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
102 if (copy_to_user((void __user *) (unsigned long) cmd.response,
103 &resp, sizeof resp)) {
108 file->async_file = filp->private_data;
110 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
111 ib_uverbs_event_handler);
112 ret = ib_register_event_handler(&file->event_handler);
116 kref_get(&file->async_file->ref);
117 kref_get(&file->ref);
118 file->ucontext = ucontext;
120 fd_install(resp.async_fd, filp);
122 mutex_unlock(&file->mutex);
127 put_unused_fd(resp.async_fd);
131 ibdev->dealloc_ucontext(ucontext);
134 mutex_unlock(&file->mutex);
138 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
139 const char __user *buf,
140 int in_len, int out_len)
142 struct ib_uverbs_query_device cmd;
143 struct ib_uverbs_query_device_resp resp;
144 struct ib_device_attr attr;
147 if (out_len < sizeof resp)
150 if (copy_from_user(&cmd, buf, sizeof cmd))
153 ret = ib_query_device(file->device->ib_dev, &attr);
157 memset(&resp, 0, sizeof resp);
159 resp.fw_ver = attr.fw_ver;
160 resp.node_guid = file->device->ib_dev->node_guid;
161 resp.sys_image_guid = attr.sys_image_guid;
162 resp.max_mr_size = attr.max_mr_size;
163 resp.page_size_cap = attr.page_size_cap;
164 resp.vendor_id = attr.vendor_id;
165 resp.vendor_part_id = attr.vendor_part_id;
166 resp.hw_ver = attr.hw_ver;
167 resp.max_qp = attr.max_qp;
168 resp.max_qp_wr = attr.max_qp_wr;
169 resp.device_cap_flags = attr.device_cap_flags;
170 resp.max_sge = attr.max_sge;
171 resp.max_sge_rd = attr.max_sge_rd;
172 resp.max_cq = attr.max_cq;
173 resp.max_cqe = attr.max_cqe;
174 resp.max_mr = attr.max_mr;
175 resp.max_pd = attr.max_pd;
176 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
177 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
178 resp.max_res_rd_atom = attr.max_res_rd_atom;
179 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
180 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
181 resp.atomic_cap = attr.atomic_cap;
182 resp.max_ee = attr.max_ee;
183 resp.max_rdd = attr.max_rdd;
184 resp.max_mw = attr.max_mw;
185 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
186 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
187 resp.max_mcast_grp = attr.max_mcast_grp;
188 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
189 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
190 resp.max_ah = attr.max_ah;
191 resp.max_fmr = attr.max_fmr;
192 resp.max_map_per_fmr = attr.max_map_per_fmr;
193 resp.max_srq = attr.max_srq;
194 resp.max_srq_wr = attr.max_srq_wr;
195 resp.max_srq_sge = attr.max_srq_sge;
196 resp.max_pkeys = attr.max_pkeys;
197 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
198 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
200 if (copy_to_user((void __user *) (unsigned long) cmd.response,
207 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
208 const char __user *buf,
209 int in_len, int out_len)
211 struct ib_uverbs_query_port cmd;
212 struct ib_uverbs_query_port_resp resp;
213 struct ib_port_attr attr;
216 if (out_len < sizeof resp)
219 if (copy_from_user(&cmd, buf, sizeof cmd))
222 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
226 memset(&resp, 0, sizeof resp);
228 resp.state = attr.state;
229 resp.max_mtu = attr.max_mtu;
230 resp.active_mtu = attr.active_mtu;
231 resp.gid_tbl_len = attr.gid_tbl_len;
232 resp.port_cap_flags = attr.port_cap_flags;
233 resp.max_msg_sz = attr.max_msg_sz;
234 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
235 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
236 resp.pkey_tbl_len = attr.pkey_tbl_len;
238 resp.sm_lid = attr.sm_lid;
240 resp.max_vl_num = attr.max_vl_num;
241 resp.sm_sl = attr.sm_sl;
242 resp.subnet_timeout = attr.subnet_timeout;
243 resp.init_type_reply = attr.init_type_reply;
244 resp.active_width = attr.active_width;
245 resp.active_speed = attr.active_speed;
246 resp.phys_state = attr.phys_state;
248 if (copy_to_user((void __user *) (unsigned long) cmd.response,
255 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
256 const char __user *buf,
257 int in_len, int out_len)
259 struct ib_uverbs_alloc_pd cmd;
260 struct ib_uverbs_alloc_pd_resp resp;
261 struct ib_udata udata;
262 struct ib_uobject *uobj;
266 if (out_len < sizeof resp)
269 if (copy_from_user(&cmd, buf, sizeof cmd))
272 INIT_UDATA(&udata, buf + sizeof cmd,
273 (unsigned long) cmd.response + sizeof resp,
274 in_len - sizeof cmd, out_len - sizeof resp);
276 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
280 uobj->context = file->ucontext;
282 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
283 file->ucontext, &udata);
289 pd->device = file->device->ib_dev;
291 atomic_set(&pd->usecnt, 0);
293 mutex_lock(&ib_uverbs_idr_mutex);
296 if (!idr_pre_get(&ib_uverbs_pd_idr, GFP_KERNEL)) {
301 ret = idr_get_new(&ib_uverbs_pd_idr, pd, &uobj->id);
308 memset(&resp, 0, sizeof resp);
309 resp.pd_handle = uobj->id;
311 if (copy_to_user((void __user *) (unsigned long) cmd.response,
312 &resp, sizeof resp)) {
317 mutex_lock(&file->mutex);
318 list_add_tail(&uobj->list, &file->ucontext->pd_list);
319 mutex_unlock(&file->mutex);
321 mutex_unlock(&ib_uverbs_idr_mutex);
326 idr_remove(&ib_uverbs_pd_idr, uobj->id);
329 mutex_unlock(&ib_uverbs_idr_mutex);
337 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
338 const char __user *buf,
339 int in_len, int out_len)
341 struct ib_uverbs_dealloc_pd cmd;
343 struct ib_uobject *uobj;
346 if (copy_from_user(&cmd, buf, sizeof cmd))
349 mutex_lock(&ib_uverbs_idr_mutex);
351 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
352 if (!pd || pd->uobject->context != file->ucontext)
357 ret = ib_dealloc_pd(pd);
361 idr_remove(&ib_uverbs_pd_idr, cmd.pd_handle);
363 mutex_lock(&file->mutex);
364 list_del(&uobj->list);
365 mutex_unlock(&file->mutex);
370 mutex_unlock(&ib_uverbs_idr_mutex);
372 return ret ? ret : in_len;
375 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
376 const char __user *buf, int in_len,
379 struct ib_uverbs_reg_mr cmd;
380 struct ib_uverbs_reg_mr_resp resp;
381 struct ib_udata udata;
382 struct ib_umem_object *obj;
387 if (out_len < sizeof resp)
390 if (copy_from_user(&cmd, buf, sizeof cmd))
393 INIT_UDATA(&udata, buf + sizeof cmd,
394 (unsigned long) cmd.response + sizeof resp,
395 in_len - sizeof cmd, out_len - sizeof resp);
397 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
401 * Local write permission is required if remote write or
402 * remote atomic permission is also requested.
404 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
405 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
408 obj = kmalloc(sizeof *obj, GFP_KERNEL);
412 obj->uobject.context = file->ucontext;
415 * We ask for writable memory if any access flags other than
416 * "remote read" are set. "Local write" and "remote write"
417 * obviously require write access. "Remote atomic" can do
418 * things like fetch and add, which will modify memory, and
419 * "MW bind" can change permissions by binding a window.
421 ret = ib_umem_get(file->device->ib_dev, &obj->umem,
422 (void *) (unsigned long) cmd.start, cmd.length,
423 !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
427 obj->umem.virt_base = cmd.hca_va;
429 mutex_lock(&ib_uverbs_idr_mutex);
431 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
432 if (!pd || pd->uobject->context != file->ucontext) {
437 if (!pd->device->reg_user_mr) {
442 mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
448 mr->device = pd->device;
450 mr->uobject = &obj->uobject;
451 atomic_inc(&pd->usecnt);
452 atomic_set(&mr->usecnt, 0);
454 memset(&resp, 0, sizeof resp);
455 resp.lkey = mr->lkey;
456 resp.rkey = mr->rkey;
459 if (!idr_pre_get(&ib_uverbs_mr_idr, GFP_KERNEL)) {
464 ret = idr_get_new(&ib_uverbs_mr_idr, mr, &obj->uobject.id);
471 resp.mr_handle = obj->uobject.id;
473 if (copy_to_user((void __user *) (unsigned long) cmd.response,
474 &resp, sizeof resp)) {
479 mutex_lock(&file->mutex);
480 list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
481 mutex_unlock(&file->mutex);
483 mutex_unlock(&ib_uverbs_idr_mutex);
488 idr_remove(&ib_uverbs_mr_idr, obj->uobject.id);
492 atomic_dec(&pd->usecnt);
495 mutex_unlock(&ib_uverbs_idr_mutex);
497 ib_umem_release(file->device->ib_dev, &obj->umem);
504 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
505 const char __user *buf, int in_len,
508 struct ib_uverbs_dereg_mr cmd;
510 struct ib_umem_object *memobj;
513 if (copy_from_user(&cmd, buf, sizeof cmd))
516 mutex_lock(&ib_uverbs_idr_mutex);
518 mr = idr_find(&ib_uverbs_mr_idr, cmd.mr_handle);
519 if (!mr || mr->uobject->context != file->ucontext)
522 memobj = container_of(mr->uobject, struct ib_umem_object, uobject);
524 ret = ib_dereg_mr(mr);
528 idr_remove(&ib_uverbs_mr_idr, cmd.mr_handle);
530 mutex_lock(&file->mutex);
531 list_del(&memobj->uobject.list);
532 mutex_unlock(&file->mutex);
534 ib_umem_release(file->device->ib_dev, &memobj->umem);
538 mutex_unlock(&ib_uverbs_idr_mutex);
540 return ret ? ret : in_len;
543 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
544 const char __user *buf, int in_len,
547 struct ib_uverbs_create_comp_channel cmd;
548 struct ib_uverbs_create_comp_channel_resp resp;
551 if (out_len < sizeof resp)
554 if (copy_from_user(&cmd, buf, sizeof cmd))
557 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
559 return PTR_ERR(filp);
561 if (copy_to_user((void __user *) (unsigned long) cmd.response,
562 &resp, sizeof resp)) {
563 put_unused_fd(resp.fd);
568 fd_install(resp.fd, filp);
572 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
573 const char __user *buf, int in_len,
576 struct ib_uverbs_create_cq cmd;
577 struct ib_uverbs_create_cq_resp resp;
578 struct ib_udata udata;
579 struct ib_ucq_object *uobj;
580 struct ib_uverbs_event_file *ev_file = NULL;
584 if (out_len < sizeof resp)
587 if (copy_from_user(&cmd, buf, sizeof cmd))
590 INIT_UDATA(&udata, buf + sizeof cmd,
591 (unsigned long) cmd.response + sizeof resp,
592 in_len - sizeof cmd, out_len - sizeof resp);
594 if (cmd.comp_vector >= file->device->num_comp_vectors)
597 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
601 if (cmd.comp_channel >= 0) {
602 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
609 uobj->uobject.user_handle = cmd.user_handle;
610 uobj->uobject.context = file->ucontext;
611 uobj->uverbs_file = file;
612 uobj->comp_events_reported = 0;
613 uobj->async_events_reported = 0;
614 INIT_LIST_HEAD(&uobj->comp_list);
615 INIT_LIST_HEAD(&uobj->async_list);
617 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
618 file->ucontext, &udata);
624 cq->device = file->device->ib_dev;
625 cq->uobject = &uobj->uobject;
626 cq->comp_handler = ib_uverbs_comp_handler;
627 cq->event_handler = ib_uverbs_cq_event_handler;
628 cq->cq_context = ev_file;
629 atomic_set(&cq->usecnt, 0);
631 mutex_lock(&ib_uverbs_idr_mutex);
634 if (!idr_pre_get(&ib_uverbs_cq_idr, GFP_KERNEL)) {
639 ret = idr_get_new(&ib_uverbs_cq_idr, cq, &uobj->uobject.id);
646 memset(&resp, 0, sizeof resp);
647 resp.cq_handle = uobj->uobject.id;
650 if (copy_to_user((void __user *) (unsigned long) cmd.response,
651 &resp, sizeof resp)) {
656 mutex_lock(&file->mutex);
657 list_add_tail(&uobj->uobject.list, &file->ucontext->cq_list);
658 mutex_unlock(&file->mutex);
660 mutex_unlock(&ib_uverbs_idr_mutex);
665 idr_remove(&ib_uverbs_cq_idr, uobj->uobject.id);
668 mutex_unlock(&ib_uverbs_idr_mutex);
673 ib_uverbs_release_ucq(file, ev_file, uobj);
678 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
679 const char __user *buf, int in_len,
682 struct ib_uverbs_resize_cq cmd;
683 struct ib_uverbs_resize_cq_resp resp;
684 struct ib_udata udata;
688 if (copy_from_user(&cmd, buf, sizeof cmd))
691 INIT_UDATA(&udata, buf + sizeof cmd,
692 (unsigned long) cmd.response + sizeof resp,
693 in_len - sizeof cmd, out_len - sizeof resp);
695 mutex_lock(&ib_uverbs_idr_mutex);
697 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
698 if (!cq || cq->uobject->context != file->ucontext || !cq->device->resize_cq)
701 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
705 memset(&resp, 0, sizeof resp);
708 if (copy_to_user((void __user *) (unsigned long) cmd.response,
713 mutex_unlock(&ib_uverbs_idr_mutex);
715 return ret ? ret : in_len;
718 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
719 const char __user *buf, int in_len,
722 struct ib_uverbs_poll_cq cmd;
723 struct ib_uverbs_poll_cq_resp *resp;
730 if (copy_from_user(&cmd, buf, sizeof cmd))
733 wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
737 rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
738 resp = kmalloc(rsize, GFP_KERNEL);
744 mutex_lock(&ib_uverbs_idr_mutex);
745 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
746 if (!cq || cq->uobject->context != file->ucontext) {
751 resp->count = ib_poll_cq(cq, cmd.ne, wc);
753 for (i = 0; i < resp->count; i++) {
754 resp->wc[i].wr_id = wc[i].wr_id;
755 resp->wc[i].status = wc[i].status;
756 resp->wc[i].opcode = wc[i].opcode;
757 resp->wc[i].vendor_err = wc[i].vendor_err;
758 resp->wc[i].byte_len = wc[i].byte_len;
759 resp->wc[i].imm_data = (__u32 __force) wc[i].imm_data;
760 resp->wc[i].qp_num = wc[i].qp_num;
761 resp->wc[i].src_qp = wc[i].src_qp;
762 resp->wc[i].wc_flags = wc[i].wc_flags;
763 resp->wc[i].pkey_index = wc[i].pkey_index;
764 resp->wc[i].slid = wc[i].slid;
765 resp->wc[i].sl = wc[i].sl;
766 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
767 resp->wc[i].port_num = wc[i].port_num;
770 if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
774 mutex_unlock(&ib_uverbs_idr_mutex);
779 return ret ? ret : in_len;
782 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
783 const char __user *buf, int in_len,
786 struct ib_uverbs_req_notify_cq cmd;
790 if (copy_from_user(&cmd, buf, sizeof cmd))
793 mutex_lock(&ib_uverbs_idr_mutex);
794 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
795 if (cq && cq->uobject->context == file->ucontext) {
796 ib_req_notify_cq(cq, cmd.solicited_only ?
797 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
800 mutex_unlock(&ib_uverbs_idr_mutex);
805 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
806 const char __user *buf, int in_len,
809 struct ib_uverbs_destroy_cq cmd;
810 struct ib_uverbs_destroy_cq_resp resp;
812 struct ib_ucq_object *uobj;
813 struct ib_uverbs_event_file *ev_file;
817 if (copy_from_user(&cmd, buf, sizeof cmd))
820 memset(&resp, 0, sizeof resp);
822 mutex_lock(&ib_uverbs_idr_mutex);
824 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
825 if (!cq || cq->uobject->context != file->ucontext)
828 user_handle = cq->uobject->user_handle;
829 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
830 ev_file = cq->cq_context;
832 ret = ib_destroy_cq(cq);
836 idr_remove(&ib_uverbs_cq_idr, cmd.cq_handle);
838 mutex_lock(&file->mutex);
839 list_del(&uobj->uobject.list);
840 mutex_unlock(&file->mutex);
842 ib_uverbs_release_ucq(file, ev_file, uobj);
844 resp.comp_events_reported = uobj->comp_events_reported;
845 resp.async_events_reported = uobj->async_events_reported;
849 if (copy_to_user((void __user *) (unsigned long) cmd.response,
854 mutex_unlock(&ib_uverbs_idr_mutex);
856 return ret ? ret : in_len;
859 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
860 const char __user *buf, int in_len,
863 struct ib_uverbs_create_qp cmd;
864 struct ib_uverbs_create_qp_resp resp;
865 struct ib_udata udata;
866 struct ib_uqp_object *uobj;
868 struct ib_cq *scq, *rcq;
871 struct ib_qp_init_attr attr;
874 if (out_len < sizeof resp)
877 if (copy_from_user(&cmd, buf, sizeof cmd))
880 INIT_UDATA(&udata, buf + sizeof cmd,
881 (unsigned long) cmd.response + sizeof resp,
882 in_len - sizeof cmd, out_len - sizeof resp);
884 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
888 mutex_lock(&ib_uverbs_idr_mutex);
890 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
891 scq = idr_find(&ib_uverbs_cq_idr, cmd.send_cq_handle);
892 rcq = idr_find(&ib_uverbs_cq_idr, cmd.recv_cq_handle);
893 srq = cmd.is_srq ? idr_find(&ib_uverbs_srq_idr, cmd.srq_handle) : NULL;
895 if (!pd || pd->uobject->context != file->ucontext ||
896 !scq || scq->uobject->context != file->ucontext ||
897 !rcq || rcq->uobject->context != file->ucontext ||
898 (cmd.is_srq && (!srq || srq->uobject->context != file->ucontext))) {
903 attr.event_handler = ib_uverbs_qp_event_handler;
904 attr.qp_context = file;
908 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
909 attr.qp_type = cmd.qp_type;
911 attr.cap.max_send_wr = cmd.max_send_wr;
912 attr.cap.max_recv_wr = cmd.max_recv_wr;
913 attr.cap.max_send_sge = cmd.max_send_sge;
914 attr.cap.max_recv_sge = cmd.max_recv_sge;
915 attr.cap.max_inline_data = cmd.max_inline_data;
917 uobj->uevent.uobject.user_handle = cmd.user_handle;
918 uobj->uevent.uobject.context = file->ucontext;
919 uobj->uevent.events_reported = 0;
920 INIT_LIST_HEAD(&uobj->uevent.event_list);
921 INIT_LIST_HEAD(&uobj->mcast_list);
923 qp = pd->device->create_qp(pd, &attr, &udata);
929 qp->device = pd->device;
931 qp->send_cq = attr.send_cq;
932 qp->recv_cq = attr.recv_cq;
934 qp->uobject = &uobj->uevent.uobject;
935 qp->event_handler = attr.event_handler;
936 qp->qp_context = attr.qp_context;
937 qp->qp_type = attr.qp_type;
938 atomic_inc(&pd->usecnt);
939 atomic_inc(&attr.send_cq->usecnt);
940 atomic_inc(&attr.recv_cq->usecnt);
942 atomic_inc(&attr.srq->usecnt);
944 memset(&resp, 0, sizeof resp);
945 resp.qpn = qp->qp_num;
948 if (!idr_pre_get(&ib_uverbs_qp_idr, GFP_KERNEL)) {
953 ret = idr_get_new(&ib_uverbs_qp_idr, qp, &uobj->uevent.uobject.id);
960 resp.qp_handle = uobj->uevent.uobject.id;
961 resp.max_recv_sge = attr.cap.max_recv_sge;
962 resp.max_send_sge = attr.cap.max_send_sge;
963 resp.max_recv_wr = attr.cap.max_recv_wr;
964 resp.max_send_wr = attr.cap.max_send_wr;
965 resp.max_inline_data = attr.cap.max_inline_data;
967 if (copy_to_user((void __user *) (unsigned long) cmd.response,
968 &resp, sizeof resp)) {
973 mutex_lock(&file->mutex);
974 list_add_tail(&uobj->uevent.uobject.list, &file->ucontext->qp_list);
975 mutex_unlock(&file->mutex);
977 mutex_unlock(&ib_uverbs_idr_mutex);
982 idr_remove(&ib_uverbs_qp_idr, uobj->uevent.uobject.id);
986 atomic_dec(&pd->usecnt);
987 atomic_dec(&attr.send_cq->usecnt);
988 atomic_dec(&attr.recv_cq->usecnt);
990 atomic_dec(&attr.srq->usecnt);
993 mutex_unlock(&ib_uverbs_idr_mutex);
999 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1000 const char __user *buf, int in_len,
1003 struct ib_uverbs_modify_qp cmd;
1005 struct ib_qp_attr *attr;
1008 if (copy_from_user(&cmd, buf, sizeof cmd))
1011 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1015 mutex_lock(&ib_uverbs_idr_mutex);
1017 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1018 if (!qp || qp->uobject->context != file->ucontext) {
1023 attr->qp_state = cmd.qp_state;
1024 attr->cur_qp_state = cmd.cur_qp_state;
1025 attr->path_mtu = cmd.path_mtu;
1026 attr->path_mig_state = cmd.path_mig_state;
1027 attr->qkey = cmd.qkey;
1028 attr->rq_psn = cmd.rq_psn;
1029 attr->sq_psn = cmd.sq_psn;
1030 attr->dest_qp_num = cmd.dest_qp_num;
1031 attr->qp_access_flags = cmd.qp_access_flags;
1032 attr->pkey_index = cmd.pkey_index;
1033 attr->alt_pkey_index = cmd.pkey_index;
1034 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1035 attr->max_rd_atomic = cmd.max_rd_atomic;
1036 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1037 attr->min_rnr_timer = cmd.min_rnr_timer;
1038 attr->port_num = cmd.port_num;
1039 attr->timeout = cmd.timeout;
1040 attr->retry_cnt = cmd.retry_cnt;
1041 attr->rnr_retry = cmd.rnr_retry;
1042 attr->alt_port_num = cmd.alt_port_num;
1043 attr->alt_timeout = cmd.alt_timeout;
1045 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1046 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1047 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1048 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1049 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1050 attr->ah_attr.dlid = cmd.dest.dlid;
1051 attr->ah_attr.sl = cmd.dest.sl;
1052 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1053 attr->ah_attr.static_rate = cmd.dest.static_rate;
1054 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1055 attr->ah_attr.port_num = cmd.dest.port_num;
1057 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1058 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1059 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1060 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1061 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1062 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1063 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1064 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1065 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1066 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1067 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1069 ret = ib_modify_qp(qp, attr, cmd.attr_mask);
1076 mutex_unlock(&ib_uverbs_idr_mutex);
1082 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1083 const char __user *buf, int in_len,
1086 struct ib_uverbs_destroy_qp cmd;
1087 struct ib_uverbs_destroy_qp_resp resp;
1089 struct ib_uqp_object *uobj;
1092 if (copy_from_user(&cmd, buf, sizeof cmd))
1095 memset(&resp, 0, sizeof resp);
1097 mutex_lock(&ib_uverbs_idr_mutex);
1099 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1100 if (!qp || qp->uobject->context != file->ucontext)
1103 uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1105 if (!list_empty(&uobj->mcast_list)) {
1110 ret = ib_destroy_qp(qp);
1114 idr_remove(&ib_uverbs_qp_idr, cmd.qp_handle);
1116 mutex_lock(&file->mutex);
1117 list_del(&uobj->uevent.uobject.list);
1118 mutex_unlock(&file->mutex);
1120 ib_uverbs_release_uevent(file, &uobj->uevent);
1122 resp.events_reported = uobj->uevent.events_reported;
1126 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1127 &resp, sizeof resp))
1131 mutex_unlock(&ib_uverbs_idr_mutex);
1133 return ret ? ret : in_len;
1136 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1137 const char __user *buf, int in_len,
1140 struct ib_uverbs_post_send cmd;
1141 struct ib_uverbs_post_send_resp resp;
1142 struct ib_uverbs_send_wr *user_wr;
1143 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
1146 ssize_t ret = -EINVAL;
1148 if (copy_from_user(&cmd, buf, sizeof cmd))
1151 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1152 cmd.sge_count * sizeof (struct ib_uverbs_sge))
1155 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1158 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1162 mutex_lock(&ib_uverbs_idr_mutex);
1164 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1165 if (!qp || qp->uobject->context != file->ucontext)
1170 for (i = 0; i < cmd.wr_count; ++i) {
1171 if (copy_from_user(user_wr,
1172 buf + sizeof cmd + i * cmd.wqe_size,
1178 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1183 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1184 user_wr->num_sge * sizeof (struct ib_sge),
1198 next->wr_id = user_wr->wr_id;
1199 next->num_sge = user_wr->num_sge;
1200 next->opcode = user_wr->opcode;
1201 next->send_flags = user_wr->send_flags;
1202 next->imm_data = (__be32 __force) user_wr->imm_data;
1204 if (qp->qp_type == IB_QPT_UD) {
1205 next->wr.ud.ah = idr_find(&ib_uverbs_ah_idr,
1207 if (!next->wr.ud.ah) {
1211 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
1212 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1214 switch (next->opcode) {
1215 case IB_WR_RDMA_WRITE:
1216 case IB_WR_RDMA_WRITE_WITH_IMM:
1217 case IB_WR_RDMA_READ:
1218 next->wr.rdma.remote_addr =
1219 user_wr->wr.rdma.remote_addr;
1220 next->wr.rdma.rkey =
1221 user_wr->wr.rdma.rkey;
1223 case IB_WR_ATOMIC_CMP_AND_SWP:
1224 case IB_WR_ATOMIC_FETCH_AND_ADD:
1225 next->wr.atomic.remote_addr =
1226 user_wr->wr.atomic.remote_addr;
1227 next->wr.atomic.compare_add =
1228 user_wr->wr.atomic.compare_add;
1229 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1230 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1237 if (next->num_sge) {
1238 next->sg_list = (void *) next +
1239 ALIGN(sizeof *next, sizeof (struct ib_sge));
1240 if (copy_from_user(next->sg_list,
1242 cmd.wr_count * cmd.wqe_size +
1243 sg_ind * sizeof (struct ib_sge),
1244 next->num_sge * sizeof (struct ib_sge))) {
1248 sg_ind += next->num_sge;
1250 next->sg_list = NULL;
1254 ret = qp->device->post_send(qp, wr, &bad_wr);
1256 for (next = wr; next; next = next->next) {
1262 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1263 &resp, sizeof resp))
1267 mutex_unlock(&ib_uverbs_idr_mutex);
1277 return ret ? ret : in_len;
1280 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1286 struct ib_uverbs_recv_wr *user_wr;
1287 struct ib_recv_wr *wr = NULL, *last, *next;
1292 if (in_len < wqe_size * wr_count +
1293 sge_count * sizeof (struct ib_uverbs_sge))
1294 return ERR_PTR(-EINVAL);
1296 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1297 return ERR_PTR(-EINVAL);
1299 user_wr = kmalloc(wqe_size, GFP_KERNEL);
1301 return ERR_PTR(-ENOMEM);
1305 for (i = 0; i < wr_count; ++i) {
1306 if (copy_from_user(user_wr, buf + i * wqe_size,
1312 if (user_wr->num_sge + sg_ind > sge_count) {
1317 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1318 user_wr->num_sge * sizeof (struct ib_sge),
1332 next->wr_id = user_wr->wr_id;
1333 next->num_sge = user_wr->num_sge;
1335 if (next->num_sge) {
1336 next->sg_list = (void *) next +
1337 ALIGN(sizeof *next, sizeof (struct ib_sge));
1338 if (copy_from_user(next->sg_list,
1339 buf + wr_count * 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;
1362 return ERR_PTR(ret);
1365 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1366 const char __user *buf, int in_len,
1369 struct ib_uverbs_post_recv cmd;
1370 struct ib_uverbs_post_recv_resp resp;
1371 struct ib_recv_wr *wr, *next, *bad_wr;
1373 ssize_t ret = -EINVAL;
1375 if (copy_from_user(&cmd, buf, sizeof cmd))
1378 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1379 in_len - sizeof cmd, cmd.wr_count,
1380 cmd.sge_count, cmd.wqe_size);
1384 mutex_lock(&ib_uverbs_idr_mutex);
1386 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1387 if (!qp || qp->uobject->context != file->ucontext)
1391 ret = qp->device->post_recv(qp, wr, &bad_wr);
1393 for (next = wr; next; next = next->next) {
1400 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1401 &resp, sizeof resp))
1405 mutex_unlock(&ib_uverbs_idr_mutex);
1413 return ret ? ret : in_len;
1416 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1417 const char __user *buf, int in_len,
1420 struct ib_uverbs_post_srq_recv cmd;
1421 struct ib_uverbs_post_srq_recv_resp resp;
1422 struct ib_recv_wr *wr, *next, *bad_wr;
1424 ssize_t ret = -EINVAL;
1426 if (copy_from_user(&cmd, buf, sizeof cmd))
1429 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1430 in_len - sizeof cmd, cmd.wr_count,
1431 cmd.sge_count, cmd.wqe_size);
1435 mutex_lock(&ib_uverbs_idr_mutex);
1437 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1438 if (!srq || srq->uobject->context != file->ucontext)
1442 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1444 for (next = wr; next; next = next->next) {
1451 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1452 &resp, sizeof resp))
1456 mutex_unlock(&ib_uverbs_idr_mutex);
1464 return ret ? ret : in_len;
1467 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1468 const char __user *buf, int in_len,
1471 struct ib_uverbs_create_ah cmd;
1472 struct ib_uverbs_create_ah_resp resp;
1473 struct ib_uobject *uobj;
1476 struct ib_ah_attr attr;
1479 if (out_len < sizeof resp)
1482 if (copy_from_user(&cmd, buf, sizeof cmd))
1485 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1489 mutex_lock(&ib_uverbs_idr_mutex);
1491 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
1492 if (!pd || pd->uobject->context != file->ucontext) {
1497 uobj->user_handle = cmd.user_handle;
1498 uobj->context = file->ucontext;
1500 attr.dlid = cmd.attr.dlid;
1501 attr.sl = cmd.attr.sl;
1502 attr.src_path_bits = cmd.attr.src_path_bits;
1503 attr.static_rate = cmd.attr.static_rate;
1504 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
1505 attr.port_num = cmd.attr.port_num;
1506 attr.grh.flow_label = cmd.attr.grh.flow_label;
1507 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
1508 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
1509 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1510 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1512 ah = ib_create_ah(pd, &attr);
1521 if (!idr_pre_get(&ib_uverbs_ah_idr, GFP_KERNEL)) {
1526 ret = idr_get_new(&ib_uverbs_ah_idr, ah, &uobj->id);
1533 resp.ah_handle = uobj->id;
1535 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1536 &resp, sizeof resp)) {
1541 mutex_lock(&file->mutex);
1542 list_add_tail(&uobj->list, &file->ucontext->ah_list);
1543 mutex_unlock(&file->mutex);
1545 mutex_unlock(&ib_uverbs_idr_mutex);
1550 idr_remove(&ib_uverbs_ah_idr, uobj->id);
1556 mutex_unlock(&ib_uverbs_idr_mutex);
1562 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1563 const char __user *buf, int in_len, int out_len)
1565 struct ib_uverbs_destroy_ah cmd;
1567 struct ib_uobject *uobj;
1570 if (copy_from_user(&cmd, buf, sizeof cmd))
1573 mutex_lock(&ib_uverbs_idr_mutex);
1575 ah = idr_find(&ib_uverbs_ah_idr, cmd.ah_handle);
1576 if (!ah || ah->uobject->context != file->ucontext)
1581 ret = ib_destroy_ah(ah);
1585 idr_remove(&ib_uverbs_ah_idr, cmd.ah_handle);
1587 mutex_lock(&file->mutex);
1588 list_del(&uobj->list);
1589 mutex_unlock(&file->mutex);
1594 mutex_unlock(&ib_uverbs_idr_mutex);
1596 return ret ? ret : in_len;
1599 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1600 const char __user *buf, int in_len,
1603 struct ib_uverbs_attach_mcast cmd;
1605 struct ib_uqp_object *uobj;
1606 struct ib_uverbs_mcast_entry *mcast;
1609 if (copy_from_user(&cmd, buf, sizeof cmd))
1612 mutex_lock(&ib_uverbs_idr_mutex);
1614 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1615 if (!qp || qp->uobject->context != file->ucontext)
1618 uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1620 list_for_each_entry(mcast, &uobj->mcast_list, list)
1621 if (cmd.mlid == mcast->lid &&
1622 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1627 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1633 mcast->lid = cmd.mlid;
1634 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1636 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1638 uobj = container_of(qp->uobject, struct ib_uqp_object,
1640 list_add_tail(&mcast->list, &uobj->mcast_list);
1645 mutex_unlock(&ib_uverbs_idr_mutex);
1647 return ret ? ret : in_len;
1650 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1651 const char __user *buf, int in_len,
1654 struct ib_uverbs_detach_mcast cmd;
1655 struct ib_uqp_object *uobj;
1657 struct ib_uverbs_mcast_entry *mcast;
1660 if (copy_from_user(&cmd, buf, sizeof cmd))
1663 mutex_lock(&ib_uverbs_idr_mutex);
1665 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1666 if (!qp || qp->uobject->context != file->ucontext)
1669 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1673 uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1675 list_for_each_entry(mcast, &uobj->mcast_list, list)
1676 if (cmd.mlid == mcast->lid &&
1677 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1678 list_del(&mcast->list);
1684 mutex_unlock(&ib_uverbs_idr_mutex);
1686 return ret ? ret : in_len;
1689 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1690 const char __user *buf, int in_len,
1693 struct ib_uverbs_create_srq cmd;
1694 struct ib_uverbs_create_srq_resp resp;
1695 struct ib_udata udata;
1696 struct ib_uevent_object *uobj;
1699 struct ib_srq_init_attr attr;
1702 if (out_len < sizeof resp)
1705 if (copy_from_user(&cmd, buf, sizeof cmd))
1708 INIT_UDATA(&udata, buf + sizeof cmd,
1709 (unsigned long) cmd.response + sizeof resp,
1710 in_len - sizeof cmd, out_len - sizeof resp);
1712 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1716 mutex_lock(&ib_uverbs_idr_mutex);
1718 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
1720 if (!pd || pd->uobject->context != file->ucontext) {
1725 attr.event_handler = ib_uverbs_srq_event_handler;
1726 attr.srq_context = file;
1727 attr.attr.max_wr = cmd.max_wr;
1728 attr.attr.max_sge = cmd.max_sge;
1729 attr.attr.srq_limit = cmd.srq_limit;
1731 uobj->uobject.user_handle = cmd.user_handle;
1732 uobj->uobject.context = file->ucontext;
1733 uobj->events_reported = 0;
1734 INIT_LIST_HEAD(&uobj->event_list);
1736 srq = pd->device->create_srq(pd, &attr, &udata);
1742 srq->device = pd->device;
1744 srq->uobject = &uobj->uobject;
1745 srq->event_handler = attr.event_handler;
1746 srq->srq_context = attr.srq_context;
1747 atomic_inc(&pd->usecnt);
1748 atomic_set(&srq->usecnt, 0);
1750 memset(&resp, 0, sizeof resp);
1753 if (!idr_pre_get(&ib_uverbs_srq_idr, GFP_KERNEL)) {
1758 ret = idr_get_new(&ib_uverbs_srq_idr, srq, &uobj->uobject.id);
1765 resp.srq_handle = uobj->uobject.id;
1767 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1768 &resp, sizeof resp)) {
1773 mutex_lock(&file->mutex);
1774 list_add_tail(&uobj->uobject.list, &file->ucontext->srq_list);
1775 mutex_unlock(&file->mutex);
1777 mutex_unlock(&ib_uverbs_idr_mutex);
1782 idr_remove(&ib_uverbs_srq_idr, uobj->uobject.id);
1785 ib_destroy_srq(srq);
1786 atomic_dec(&pd->usecnt);
1789 mutex_unlock(&ib_uverbs_idr_mutex);
1795 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
1796 const char __user *buf, int in_len,
1799 struct ib_uverbs_modify_srq cmd;
1801 struct ib_srq_attr attr;
1804 if (copy_from_user(&cmd, buf, sizeof cmd))
1807 mutex_lock(&ib_uverbs_idr_mutex);
1809 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1810 if (!srq || srq->uobject->context != file->ucontext) {
1815 attr.max_wr = cmd.max_wr;
1816 attr.srq_limit = cmd.srq_limit;
1818 ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
1821 mutex_unlock(&ib_uverbs_idr_mutex);
1823 return ret ? ret : in_len;
1826 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
1827 const char __user *buf, int in_len,
1830 struct ib_uverbs_destroy_srq cmd;
1831 struct ib_uverbs_destroy_srq_resp resp;
1833 struct ib_uevent_object *uobj;
1836 if (copy_from_user(&cmd, buf, sizeof cmd))
1839 mutex_lock(&ib_uverbs_idr_mutex);
1841 memset(&resp, 0, sizeof resp);
1843 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1844 if (!srq || srq->uobject->context != file->ucontext)
1847 uobj = container_of(srq->uobject, struct ib_uevent_object, uobject);
1849 ret = ib_destroy_srq(srq);
1853 idr_remove(&ib_uverbs_srq_idr, cmd.srq_handle);
1855 mutex_lock(&file->mutex);
1856 list_del(&uobj->uobject.list);
1857 mutex_unlock(&file->mutex);
1859 ib_uverbs_release_uevent(file, uobj);
1861 resp.events_reported = uobj->events_reported;
1865 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1866 &resp, sizeof resp))
1870 mutex_unlock(&ib_uverbs_idr_mutex);
1872 return ret ? ret : in_len;