2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Cisco Systems. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
36 #include <linux/file.h>
38 #include <asm/uaccess.h>
42 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
44 (udata)->inbuf = (void __user *) (ibuf); \
45 (udata)->outbuf = (void __user *) (obuf); \
46 (udata)->inlen = (ilen); \
47 (udata)->outlen = (olen); \
50 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
51 const char __user *buf,
52 int in_len, int out_len)
54 struct ib_uverbs_get_context cmd;
55 struct ib_uverbs_get_context_resp resp;
56 struct ib_udata udata;
57 struct ib_device *ibdev = file->device->ib_dev;
58 struct ib_ucontext *ucontext;
62 if (out_len < sizeof resp)
65 if (copy_from_user(&cmd, buf, sizeof cmd))
75 INIT_UDATA(&udata, buf + sizeof cmd,
76 (unsigned long) cmd.response + sizeof resp,
77 in_len - sizeof cmd, out_len - sizeof resp);
79 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
81 return PTR_ERR(file->ucontext);
83 ucontext->device = ibdev;
84 INIT_LIST_HEAD(&ucontext->pd_list);
85 INIT_LIST_HEAD(&ucontext->mr_list);
86 INIT_LIST_HEAD(&ucontext->mw_list);
87 INIT_LIST_HEAD(&ucontext->cq_list);
88 INIT_LIST_HEAD(&ucontext->qp_list);
89 INIT_LIST_HEAD(&ucontext->srq_list);
90 INIT_LIST_HEAD(&ucontext->ah_list);
92 resp.num_comp_vectors = file->device->num_comp_vectors;
94 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
100 if (copy_to_user((void __user *) (unsigned long) cmd.response,
101 &resp, sizeof resp)) {
106 file->async_file = filp->private_data;
108 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
109 ib_uverbs_event_handler);
110 ret = ib_register_event_handler(&file->event_handler);
114 kref_get(&file->async_file->ref);
115 kref_get(&file->ref);
116 file->ucontext = ucontext;
118 fd_install(resp.async_fd, filp);
125 put_unused_fd(resp.async_fd);
129 ibdev->dealloc_ucontext(ucontext);
136 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
137 const char __user *buf,
138 int in_len, int out_len)
140 struct ib_uverbs_query_device cmd;
141 struct ib_uverbs_query_device_resp resp;
142 struct ib_device_attr attr;
145 if (out_len < sizeof resp)
148 if (copy_from_user(&cmd, buf, sizeof cmd))
151 ret = ib_query_device(file->device->ib_dev, &attr);
155 memset(&resp, 0, sizeof resp);
157 resp.fw_ver = attr.fw_ver;
158 resp.node_guid = attr.node_guid;
159 resp.sys_image_guid = attr.sys_image_guid;
160 resp.max_mr_size = attr.max_mr_size;
161 resp.page_size_cap = attr.page_size_cap;
162 resp.vendor_id = attr.vendor_id;
163 resp.vendor_part_id = attr.vendor_part_id;
164 resp.hw_ver = attr.hw_ver;
165 resp.max_qp = attr.max_qp;
166 resp.max_qp_wr = attr.max_qp_wr;
167 resp.device_cap_flags = attr.device_cap_flags;
168 resp.max_sge = attr.max_sge;
169 resp.max_sge_rd = attr.max_sge_rd;
170 resp.max_cq = attr.max_cq;
171 resp.max_cqe = attr.max_cqe;
172 resp.max_mr = attr.max_mr;
173 resp.max_pd = attr.max_pd;
174 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
175 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
176 resp.max_res_rd_atom = attr.max_res_rd_atom;
177 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
178 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
179 resp.atomic_cap = attr.atomic_cap;
180 resp.max_ee = attr.max_ee;
181 resp.max_rdd = attr.max_rdd;
182 resp.max_mw = attr.max_mw;
183 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
184 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
185 resp.max_mcast_grp = attr.max_mcast_grp;
186 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
187 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
188 resp.max_ah = attr.max_ah;
189 resp.max_fmr = attr.max_fmr;
190 resp.max_map_per_fmr = attr.max_map_per_fmr;
191 resp.max_srq = attr.max_srq;
192 resp.max_srq_wr = attr.max_srq_wr;
193 resp.max_srq_sge = attr.max_srq_sge;
194 resp.max_pkeys = attr.max_pkeys;
195 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
196 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
198 if (copy_to_user((void __user *) (unsigned long) cmd.response,
205 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
206 const char __user *buf,
207 int in_len, int out_len)
209 struct ib_uverbs_query_port cmd;
210 struct ib_uverbs_query_port_resp resp;
211 struct ib_port_attr attr;
214 if (out_len < sizeof resp)
217 if (copy_from_user(&cmd, buf, sizeof cmd))
220 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
224 memset(&resp, 0, sizeof resp);
226 resp.state = attr.state;
227 resp.max_mtu = attr.max_mtu;
228 resp.active_mtu = attr.active_mtu;
229 resp.gid_tbl_len = attr.gid_tbl_len;
230 resp.port_cap_flags = attr.port_cap_flags;
231 resp.max_msg_sz = attr.max_msg_sz;
232 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
233 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
234 resp.pkey_tbl_len = attr.pkey_tbl_len;
236 resp.sm_lid = attr.sm_lid;
238 resp.max_vl_num = attr.max_vl_num;
239 resp.sm_sl = attr.sm_sl;
240 resp.subnet_timeout = attr.subnet_timeout;
241 resp.init_type_reply = attr.init_type_reply;
242 resp.active_width = attr.active_width;
243 resp.active_speed = attr.active_speed;
244 resp.phys_state = attr.phys_state;
246 if (copy_to_user((void __user *) (unsigned long) cmd.response,
253 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
254 const char __user *buf,
255 int in_len, int out_len)
257 struct ib_uverbs_alloc_pd cmd;
258 struct ib_uverbs_alloc_pd_resp resp;
259 struct ib_udata udata;
260 struct ib_uobject *uobj;
264 if (out_len < sizeof resp)
267 if (copy_from_user(&cmd, buf, sizeof cmd))
270 INIT_UDATA(&udata, buf + sizeof cmd,
271 (unsigned long) cmd.response + sizeof resp,
272 in_len - sizeof cmd, out_len - sizeof resp);
274 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
278 uobj->context = file->ucontext;
280 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
281 file->ucontext, &udata);
287 pd->device = file->device->ib_dev;
289 atomic_set(&pd->usecnt, 0);
292 if (!idr_pre_get(&ib_uverbs_pd_idr, GFP_KERNEL)) {
297 down(&ib_uverbs_idr_mutex);
298 ret = idr_get_new(&ib_uverbs_pd_idr, pd, &uobj->id);
299 up(&ib_uverbs_idr_mutex);
307 list_add_tail(&uobj->list, &file->ucontext->pd_list);
310 memset(&resp, 0, sizeof resp);
311 resp.pd_handle = uobj->id;
313 if (copy_to_user((void __user *) (unsigned long) cmd.response,
314 &resp, sizeof resp)) {
323 list_del(&uobj->list);
326 down(&ib_uverbs_idr_mutex);
327 idr_remove(&ib_uverbs_pd_idr, uobj->id);
328 up(&ib_uverbs_idr_mutex);
338 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
339 const char __user *buf,
340 int in_len, int out_len)
342 struct ib_uverbs_dealloc_pd cmd;
344 struct ib_uobject *uobj;
347 if (copy_from_user(&cmd, buf, sizeof cmd))
350 down(&ib_uverbs_idr_mutex);
352 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
353 if (!pd || pd->uobject->context != file->ucontext)
358 ret = ib_dealloc_pd(pd);
362 idr_remove(&ib_uverbs_pd_idr, cmd.pd_handle);
365 list_del(&uobj->list);
371 up(&ib_uverbs_idr_mutex);
373 return ret ? ret : in_len;
376 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
377 const char __user *buf, int in_len,
380 struct ib_uverbs_reg_mr cmd;
381 struct ib_uverbs_reg_mr_resp resp;
382 struct ib_udata udata;
383 struct ib_umem_object *obj;
388 if (out_len < sizeof resp)
391 if (copy_from_user(&cmd, buf, sizeof cmd))
394 INIT_UDATA(&udata, buf + sizeof cmd,
395 (unsigned long) cmd.response + sizeof resp,
396 in_len - sizeof cmd, out_len - sizeof resp);
398 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
401 obj = kmalloc(sizeof *obj, GFP_KERNEL);
405 obj->uobject.context = file->ucontext;
408 * We ask for writable memory if any access flags other than
409 * "remote read" are set. "Local write" and "remote write"
410 * obviously require write access. "Remote atomic" can do
411 * things like fetch and add, which will modify memory, and
412 * "MW bind" can change permissions by binding a window.
414 ret = ib_umem_get(file->device->ib_dev, &obj->umem,
415 (void *) (unsigned long) cmd.start, cmd.length,
416 !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
420 obj->umem.virt_base = cmd.hca_va;
422 down(&ib_uverbs_idr_mutex);
424 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
425 if (!pd || pd->uobject->context != file->ucontext) {
430 if (!pd->device->reg_user_mr) {
435 mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
441 mr->device = pd->device;
443 mr->uobject = &obj->uobject;
444 atomic_inc(&pd->usecnt);
445 atomic_set(&mr->usecnt, 0);
447 memset(&resp, 0, sizeof resp);
448 resp.lkey = mr->lkey;
449 resp.rkey = mr->rkey;
452 if (!idr_pre_get(&ib_uverbs_mr_idr, GFP_KERNEL)) {
457 ret = idr_get_new(&ib_uverbs_mr_idr, mr, &obj->uobject.id);
464 resp.mr_handle = obj->uobject.id;
467 list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
470 if (copy_to_user((void __user *) (unsigned long) cmd.response,
471 &resp, sizeof resp)) {
476 up(&ib_uverbs_idr_mutex);
482 list_del(&obj->uobject.list);
489 up(&ib_uverbs_idr_mutex);
491 ib_umem_release(file->device->ib_dev, &obj->umem);
498 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
499 const char __user *buf, int in_len,
502 struct ib_uverbs_dereg_mr cmd;
504 struct ib_umem_object *memobj;
507 if (copy_from_user(&cmd, buf, sizeof cmd))
510 down(&ib_uverbs_idr_mutex);
512 mr = idr_find(&ib_uverbs_mr_idr, cmd.mr_handle);
513 if (!mr || mr->uobject->context != file->ucontext)
516 memobj = container_of(mr->uobject, struct ib_umem_object, uobject);
518 ret = ib_dereg_mr(mr);
522 idr_remove(&ib_uverbs_mr_idr, cmd.mr_handle);
525 list_del(&memobj->uobject.list);
528 ib_umem_release(file->device->ib_dev, &memobj->umem);
532 up(&ib_uverbs_idr_mutex);
534 return ret ? ret : in_len;
537 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
538 const char __user *buf, int in_len,
541 struct ib_uverbs_create_comp_channel cmd;
542 struct ib_uverbs_create_comp_channel_resp resp;
545 if (out_len < sizeof resp)
548 if (copy_from_user(&cmd, buf, sizeof cmd))
551 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
553 return PTR_ERR(filp);
555 if (copy_to_user((void __user *) (unsigned long) cmd.response,
556 &resp, sizeof resp)) {
557 put_unused_fd(resp.fd);
562 fd_install(resp.fd, filp);
566 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
567 const char __user *buf, int in_len,
570 struct ib_uverbs_create_cq cmd;
571 struct ib_uverbs_create_cq_resp resp;
572 struct ib_udata udata;
573 struct ib_ucq_object *uobj;
574 struct ib_uverbs_event_file *ev_file = NULL;
578 if (out_len < sizeof resp)
581 if (copy_from_user(&cmd, buf, sizeof cmd))
584 INIT_UDATA(&udata, buf + sizeof cmd,
585 (unsigned long) cmd.response + sizeof resp,
586 in_len - sizeof cmd, out_len - sizeof resp);
588 if (cmd.comp_vector >= file->device->num_comp_vectors)
591 if (cmd.comp_channel >= 0)
592 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
594 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
598 uobj->uobject.user_handle = cmd.user_handle;
599 uobj->uobject.context = file->ucontext;
600 uobj->comp_events_reported = 0;
601 uobj->async_events_reported = 0;
602 INIT_LIST_HEAD(&uobj->comp_list);
603 INIT_LIST_HEAD(&uobj->async_list);
605 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
606 file->ucontext, &udata);
612 cq->device = file->device->ib_dev;
613 cq->uobject = &uobj->uobject;
614 cq->comp_handler = ib_uverbs_comp_handler;
615 cq->event_handler = ib_uverbs_cq_event_handler;
616 cq->cq_context = ev_file;
617 atomic_set(&cq->usecnt, 0);
620 if (!idr_pre_get(&ib_uverbs_cq_idr, GFP_KERNEL)) {
625 down(&ib_uverbs_idr_mutex);
626 ret = idr_get_new(&ib_uverbs_cq_idr, cq, &uobj->uobject.id);
627 up(&ib_uverbs_idr_mutex);
635 list_add_tail(&uobj->uobject.list, &file->ucontext->cq_list);
638 memset(&resp, 0, sizeof resp);
639 resp.cq_handle = uobj->uobject.id;
642 if (copy_to_user((void __user *) (unsigned long) cmd.response,
643 &resp, sizeof resp)) {
652 list_del(&uobj->uobject.list);
655 down(&ib_uverbs_idr_mutex);
656 idr_remove(&ib_uverbs_cq_idr, uobj->uobject.id);
657 up(&ib_uverbs_idr_mutex);
667 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
668 const char __user *buf, int in_len,
671 struct ib_uverbs_destroy_cq cmd;
672 struct ib_uverbs_destroy_cq_resp resp;
674 struct ib_ucq_object *uobj;
675 struct ib_uverbs_event_file *ev_file;
676 struct ib_uverbs_event *evt, *tmp;
680 if (copy_from_user(&cmd, buf, sizeof cmd))
683 memset(&resp, 0, sizeof resp);
685 down(&ib_uverbs_idr_mutex);
687 cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
688 if (!cq || cq->uobject->context != file->ucontext)
691 user_handle = cq->uobject->user_handle;
692 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
693 ev_file = cq->cq_context;
695 ret = ib_destroy_cq(cq);
699 idr_remove(&ib_uverbs_cq_idr, cmd.cq_handle);
702 list_del(&uobj->uobject.list);
706 spin_lock_irq(&ev_file->lock);
707 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
708 list_del(&evt->list);
711 spin_unlock_irq(&ev_file->lock);
713 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
716 spin_lock_irq(&file->async_file->lock);
717 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
718 list_del(&evt->list);
721 spin_unlock_irq(&file->async_file->lock);
723 resp.comp_events_reported = uobj->comp_events_reported;
724 resp.async_events_reported = uobj->async_events_reported;
728 if (copy_to_user((void __user *) (unsigned long) cmd.response,
733 up(&ib_uverbs_idr_mutex);
735 return ret ? ret : in_len;
738 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
739 const char __user *buf, int in_len,
742 struct ib_uverbs_create_qp cmd;
743 struct ib_uverbs_create_qp_resp resp;
744 struct ib_udata udata;
745 struct ib_uevent_object *uobj;
747 struct ib_cq *scq, *rcq;
750 struct ib_qp_init_attr attr;
753 if (out_len < sizeof resp)
756 if (copy_from_user(&cmd, buf, sizeof cmd))
759 INIT_UDATA(&udata, buf + sizeof cmd,
760 (unsigned long) cmd.response + sizeof resp,
761 in_len - sizeof cmd, out_len - sizeof resp);
763 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
767 down(&ib_uverbs_idr_mutex);
769 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
770 scq = idr_find(&ib_uverbs_cq_idr, cmd.send_cq_handle);
771 rcq = idr_find(&ib_uverbs_cq_idr, cmd.recv_cq_handle);
772 srq = cmd.is_srq ? idr_find(&ib_uverbs_srq_idr, cmd.srq_handle) : NULL;
774 if (!pd || pd->uobject->context != file->ucontext ||
775 !scq || scq->uobject->context != file->ucontext ||
776 !rcq || rcq->uobject->context != file->ucontext ||
777 (cmd.is_srq && (!srq || srq->uobject->context != file->ucontext))) {
782 attr.event_handler = ib_uverbs_qp_event_handler;
783 attr.qp_context = file;
787 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
788 attr.qp_type = cmd.qp_type;
790 attr.cap.max_send_wr = cmd.max_send_wr;
791 attr.cap.max_recv_wr = cmd.max_recv_wr;
792 attr.cap.max_send_sge = cmd.max_send_sge;
793 attr.cap.max_recv_sge = cmd.max_recv_sge;
794 attr.cap.max_inline_data = cmd.max_inline_data;
796 uobj->uobject.user_handle = cmd.user_handle;
797 uobj->uobject.context = file->ucontext;
798 uobj->events_reported = 0;
799 INIT_LIST_HEAD(&uobj->event_list);
801 qp = pd->device->create_qp(pd, &attr, &udata);
807 qp->device = pd->device;
809 qp->send_cq = attr.send_cq;
810 qp->recv_cq = attr.recv_cq;
812 qp->uobject = &uobj->uobject;
813 qp->event_handler = attr.event_handler;
814 qp->qp_context = attr.qp_context;
815 qp->qp_type = attr.qp_type;
816 atomic_inc(&pd->usecnt);
817 atomic_inc(&attr.send_cq->usecnt);
818 atomic_inc(&attr.recv_cq->usecnt);
820 atomic_inc(&attr.srq->usecnt);
822 memset(&resp, 0, sizeof resp);
823 resp.qpn = qp->qp_num;
826 if (!idr_pre_get(&ib_uverbs_qp_idr, GFP_KERNEL)) {
831 ret = idr_get_new(&ib_uverbs_qp_idr, qp, &uobj->uobject.id);
838 resp.qp_handle = uobj->uobject.id;
841 list_add_tail(&uobj->uobject.list, &file->ucontext->qp_list);
844 if (copy_to_user((void __user *) (unsigned long) cmd.response,
845 &resp, sizeof resp)) {
850 up(&ib_uverbs_idr_mutex);
856 list_del(&uobj->uobject.list);
863 up(&ib_uverbs_idr_mutex);
869 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
870 const char __user *buf, int in_len,
873 struct ib_uverbs_modify_qp cmd;
875 struct ib_qp_attr *attr;
878 if (copy_from_user(&cmd, buf, sizeof cmd))
881 attr = kmalloc(sizeof *attr, GFP_KERNEL);
885 down(&ib_uverbs_idr_mutex);
887 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
888 if (!qp || qp->uobject->context != file->ucontext) {
893 attr->qp_state = cmd.qp_state;
894 attr->cur_qp_state = cmd.cur_qp_state;
895 attr->path_mtu = cmd.path_mtu;
896 attr->path_mig_state = cmd.path_mig_state;
897 attr->qkey = cmd.qkey;
898 attr->rq_psn = cmd.rq_psn;
899 attr->sq_psn = cmd.sq_psn;
900 attr->dest_qp_num = cmd.dest_qp_num;
901 attr->qp_access_flags = cmd.qp_access_flags;
902 attr->pkey_index = cmd.pkey_index;
903 attr->alt_pkey_index = cmd.pkey_index;
904 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
905 attr->max_rd_atomic = cmd.max_rd_atomic;
906 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
907 attr->min_rnr_timer = cmd.min_rnr_timer;
908 attr->port_num = cmd.port_num;
909 attr->timeout = cmd.timeout;
910 attr->retry_cnt = cmd.retry_cnt;
911 attr->rnr_retry = cmd.rnr_retry;
912 attr->alt_port_num = cmd.alt_port_num;
913 attr->alt_timeout = cmd.alt_timeout;
915 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
916 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
917 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
918 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
919 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
920 attr->ah_attr.dlid = cmd.dest.dlid;
921 attr->ah_attr.sl = cmd.dest.sl;
922 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
923 attr->ah_attr.static_rate = cmd.dest.static_rate;
924 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
925 attr->ah_attr.port_num = cmd.dest.port_num;
927 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
928 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
929 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
930 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
931 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
932 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
933 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
934 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
935 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
936 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
937 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
939 ret = ib_modify_qp(qp, attr, cmd.attr_mask);
946 up(&ib_uverbs_idr_mutex);
952 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
953 const char __user *buf, int in_len,
956 struct ib_uverbs_destroy_qp cmd;
957 struct ib_uverbs_destroy_qp_resp resp;
959 struct ib_uevent_object *uobj;
960 struct ib_uverbs_event *evt, *tmp;
963 if (copy_from_user(&cmd, buf, sizeof cmd))
966 memset(&resp, 0, sizeof resp);
968 down(&ib_uverbs_idr_mutex);
970 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
971 if (!qp || qp->uobject->context != file->ucontext)
974 uobj = container_of(qp->uobject, struct ib_uevent_object, uobject);
976 ret = ib_destroy_qp(qp);
980 idr_remove(&ib_uverbs_qp_idr, cmd.qp_handle);
983 list_del(&uobj->uobject.list);
986 spin_lock_irq(&file->async_file->lock);
987 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
988 list_del(&evt->list);
991 spin_unlock_irq(&file->async_file->lock);
993 resp.events_reported = uobj->events_reported;
997 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1002 up(&ib_uverbs_idr_mutex);
1004 return ret ? ret : in_len;
1007 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1008 const char __user *buf, int in_len,
1011 struct ib_uverbs_attach_mcast cmd;
1015 if (copy_from_user(&cmd, buf, sizeof cmd))
1018 down(&ib_uverbs_idr_mutex);
1020 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1021 if (qp && qp->uobject->context == file->ucontext)
1022 ret = ib_attach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1024 up(&ib_uverbs_idr_mutex);
1026 return ret ? ret : in_len;
1029 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1030 const char __user *buf, int in_len,
1033 struct ib_uverbs_detach_mcast cmd;
1037 if (copy_from_user(&cmd, buf, sizeof cmd))
1040 down(&ib_uverbs_idr_mutex);
1042 qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
1043 if (qp && qp->uobject->context == file->ucontext)
1044 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1046 up(&ib_uverbs_idr_mutex);
1048 return ret ? ret : in_len;
1051 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1052 const char __user *buf, int in_len,
1055 struct ib_uverbs_create_srq cmd;
1056 struct ib_uverbs_create_srq_resp resp;
1057 struct ib_udata udata;
1058 struct ib_uevent_object *uobj;
1061 struct ib_srq_init_attr attr;
1064 if (out_len < sizeof resp)
1067 if (copy_from_user(&cmd, buf, sizeof cmd))
1070 INIT_UDATA(&udata, buf + sizeof cmd,
1071 (unsigned long) cmd.response + sizeof resp,
1072 in_len - sizeof cmd, out_len - sizeof resp);
1074 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1078 down(&ib_uverbs_idr_mutex);
1080 pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
1082 if (!pd || pd->uobject->context != file->ucontext) {
1087 attr.event_handler = ib_uverbs_srq_event_handler;
1088 attr.srq_context = file;
1089 attr.attr.max_wr = cmd.max_wr;
1090 attr.attr.max_sge = cmd.max_sge;
1091 attr.attr.srq_limit = cmd.srq_limit;
1093 uobj->uobject.user_handle = cmd.user_handle;
1094 uobj->uobject.context = file->ucontext;
1095 uobj->events_reported = 0;
1096 INIT_LIST_HEAD(&uobj->event_list);
1098 srq = pd->device->create_srq(pd, &attr, &udata);
1104 srq->device = pd->device;
1106 srq->uobject = &uobj->uobject;
1107 srq->event_handler = attr.event_handler;
1108 srq->srq_context = attr.srq_context;
1109 atomic_inc(&pd->usecnt);
1110 atomic_set(&srq->usecnt, 0);
1112 memset(&resp, 0, sizeof resp);
1115 if (!idr_pre_get(&ib_uverbs_srq_idr, GFP_KERNEL)) {
1120 ret = idr_get_new(&ib_uverbs_srq_idr, srq, &uobj->uobject.id);
1127 resp.srq_handle = uobj->uobject.id;
1130 list_add_tail(&uobj->uobject.list, &file->ucontext->srq_list);
1133 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1134 &resp, sizeof resp)) {
1139 up(&ib_uverbs_idr_mutex);
1145 list_del(&uobj->uobject.list);
1149 ib_destroy_srq(srq);
1152 up(&ib_uverbs_idr_mutex);
1158 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
1159 const char __user *buf, int in_len,
1162 struct ib_uverbs_modify_srq cmd;
1164 struct ib_srq_attr attr;
1167 if (copy_from_user(&cmd, buf, sizeof cmd))
1170 down(&ib_uverbs_idr_mutex);
1172 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1173 if (!srq || srq->uobject->context != file->ucontext) {
1178 attr.max_wr = cmd.max_wr;
1179 attr.max_sge = cmd.max_sge;
1180 attr.srq_limit = cmd.srq_limit;
1182 ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
1185 up(&ib_uverbs_idr_mutex);
1187 return ret ? ret : in_len;
1190 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
1191 const char __user *buf, int in_len,
1194 struct ib_uverbs_destroy_srq cmd;
1195 struct ib_uverbs_destroy_srq_resp resp;
1197 struct ib_uevent_object *uobj;
1198 struct ib_uverbs_event *evt, *tmp;
1201 if (copy_from_user(&cmd, buf, sizeof cmd))
1204 down(&ib_uverbs_idr_mutex);
1206 memset(&resp, 0, sizeof resp);
1208 srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
1209 if (!srq || srq->uobject->context != file->ucontext)
1212 uobj = container_of(srq->uobject, struct ib_uevent_object, uobject);
1214 ret = ib_destroy_srq(srq);
1218 idr_remove(&ib_uverbs_srq_idr, cmd.srq_handle);
1221 list_del(&uobj->uobject.list);
1224 spin_lock_irq(&file->async_file->lock);
1225 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
1226 list_del(&evt->list);
1229 spin_unlock_irq(&file->async_file->lock);
1231 resp.events_reported = uobj->events_reported;
1235 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1236 &resp, sizeof resp))
1240 up(&ib_uverbs_idr_mutex);
1242 return ret ? ret : in_len;