2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
4 * Copyright (c) 2005 PathScale, Inc. All rights reserved.
5 * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
38 #include <linux/file.h>
41 #include <asm/uaccess.h>
45 static struct lock_class_key pd_lock_key;
46 static struct lock_class_key mr_lock_key;
47 static struct lock_class_key cq_lock_key;
48 static struct lock_class_key qp_lock_key;
49 static struct lock_class_key ah_lock_key;
50 static struct lock_class_key srq_lock_key;
52 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
54 (udata)->inbuf = (void __user *) (ibuf); \
55 (udata)->outbuf = (void __user *) (obuf); \
56 (udata)->inlen = (ilen); \
57 (udata)->outlen = (olen); \
61 * The ib_uobject locking scheme is as follows:
63 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
64 * needs to be held during all idr operations. When an object is
65 * looked up, a reference must be taken on the object's kref before
68 * - Each object also has an rwsem. This rwsem must be held for
69 * reading while an operation that uses the object is performed.
70 * For example, while registering an MR, the associated PD's
71 * uobject.mutex must be held for reading. The rwsem must be held
72 * for writing while initializing or destroying an object.
74 * - In addition, each object has a "live" flag. If this flag is not
75 * set, then lookups of the object will fail even if it is found in
76 * the idr. This handles a reader that blocks and does not acquire
77 * the rwsem until after the object is destroyed. The destroy
78 * operation will set the live flag to 0 and then drop the rwsem;
79 * this will allow the reader to acquire the rwsem, see that the
80 * live flag is 0, and then drop the rwsem and its reference to
81 * object. The underlying storage will not be freed until the last
82 * reference to the object is dropped.
85 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
86 struct ib_ucontext *context, struct lock_class_key *key)
88 uobj->user_handle = user_handle;
89 uobj->context = context;
90 kref_init(&uobj->ref);
91 init_rwsem(&uobj->mutex);
92 lockdep_set_class(&uobj->mutex, key);
96 static void release_uobj(struct kref *kref)
98 kfree(container_of(kref, struct ib_uobject, ref));
101 static void put_uobj(struct ib_uobject *uobj)
103 kref_put(&uobj->ref, release_uobj);
106 static void put_uobj_read(struct ib_uobject *uobj)
108 up_read(&uobj->mutex);
112 static void put_uobj_write(struct ib_uobject *uobj)
114 up_write(&uobj->mutex);
118 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
123 if (!idr_pre_get(idr, GFP_KERNEL))
126 spin_lock(&ib_uverbs_idr_lock);
127 ret = idr_get_new(idr, uobj, &uobj->id);
128 spin_unlock(&ib_uverbs_idr_lock);
136 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
138 spin_lock(&ib_uverbs_idr_lock);
139 idr_remove(idr, uobj->id);
140 spin_unlock(&ib_uverbs_idr_lock);
143 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
144 struct ib_ucontext *context)
146 struct ib_uobject *uobj;
148 spin_lock(&ib_uverbs_idr_lock);
149 uobj = idr_find(idr, id);
151 if (uobj->context == context)
152 kref_get(&uobj->ref);
156 spin_unlock(&ib_uverbs_idr_lock);
161 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
162 struct ib_ucontext *context, int nested)
164 struct ib_uobject *uobj;
166 uobj = __idr_get_uobj(idr, id, context);
171 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
173 down_read(&uobj->mutex);
182 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
183 struct ib_ucontext *context)
185 struct ib_uobject *uobj;
187 uobj = __idr_get_uobj(idr, id, context);
191 down_write(&uobj->mutex);
193 put_uobj_write(uobj);
200 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
203 struct ib_uobject *uobj;
205 uobj = idr_read_uobj(idr, id, context, nested);
206 return uobj ? uobj->object : NULL;
209 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
211 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
214 static void put_pd_read(struct ib_pd *pd)
216 put_uobj_read(pd->uobject);
219 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
221 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
224 static void put_cq_read(struct ib_cq *cq)
226 put_uobj_read(cq->uobject);
229 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
231 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
234 static void put_ah_read(struct ib_ah *ah)
236 put_uobj_read(ah->uobject);
239 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
241 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
244 static void put_qp_read(struct ib_qp *qp)
246 put_uobj_read(qp->uobject);
249 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
251 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
254 static void put_srq_read(struct ib_srq *srq)
256 put_uobj_read(srq->uobject);
259 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
260 const char __user *buf,
261 int in_len, int out_len)
263 struct ib_uverbs_get_context cmd;
264 struct ib_uverbs_get_context_resp resp;
265 struct ib_udata udata;
266 struct ib_device *ibdev = file->device->ib_dev;
267 struct ib_ucontext *ucontext;
271 if (out_len < sizeof resp)
274 if (copy_from_user(&cmd, buf, sizeof cmd))
277 mutex_lock(&file->mutex);
279 if (file->ucontext) {
284 INIT_UDATA(&udata, buf + sizeof cmd,
285 (unsigned long) cmd.response + sizeof resp,
286 in_len - sizeof cmd, out_len - sizeof resp);
288 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
289 if (IS_ERR(ucontext)) {
290 ret = PTR_ERR(file->ucontext);
294 ucontext->device = ibdev;
295 INIT_LIST_HEAD(&ucontext->pd_list);
296 INIT_LIST_HEAD(&ucontext->mr_list);
297 INIT_LIST_HEAD(&ucontext->mw_list);
298 INIT_LIST_HEAD(&ucontext->cq_list);
299 INIT_LIST_HEAD(&ucontext->qp_list);
300 INIT_LIST_HEAD(&ucontext->srq_list);
301 INIT_LIST_HEAD(&ucontext->ah_list);
302 ucontext->closing = 0;
304 resp.num_comp_vectors = file->device->num_comp_vectors;
306 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
312 if (copy_to_user((void __user *) (unsigned long) cmd.response,
313 &resp, sizeof resp)) {
318 file->async_file = filp->private_data;
320 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
321 ib_uverbs_event_handler);
322 ret = ib_register_event_handler(&file->event_handler);
326 kref_get(&file->async_file->ref);
327 kref_get(&file->ref);
328 file->ucontext = ucontext;
330 fd_install(resp.async_fd, filp);
332 mutex_unlock(&file->mutex);
337 put_unused_fd(resp.async_fd);
341 ibdev->dealloc_ucontext(ucontext);
344 mutex_unlock(&file->mutex);
348 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
349 const char __user *buf,
350 int in_len, int out_len)
352 struct ib_uverbs_query_device cmd;
353 struct ib_uverbs_query_device_resp resp;
354 struct ib_device_attr attr;
357 if (out_len < sizeof resp)
360 if (copy_from_user(&cmd, buf, sizeof cmd))
363 ret = ib_query_device(file->device->ib_dev, &attr);
367 memset(&resp, 0, sizeof resp);
369 resp.fw_ver = attr.fw_ver;
370 resp.node_guid = file->device->ib_dev->node_guid;
371 resp.sys_image_guid = attr.sys_image_guid;
372 resp.max_mr_size = attr.max_mr_size;
373 resp.page_size_cap = attr.page_size_cap;
374 resp.vendor_id = attr.vendor_id;
375 resp.vendor_part_id = attr.vendor_part_id;
376 resp.hw_ver = attr.hw_ver;
377 resp.max_qp = attr.max_qp;
378 resp.max_qp_wr = attr.max_qp_wr;
379 resp.device_cap_flags = attr.device_cap_flags;
380 resp.max_sge = attr.max_sge;
381 resp.max_sge_rd = attr.max_sge_rd;
382 resp.max_cq = attr.max_cq;
383 resp.max_cqe = attr.max_cqe;
384 resp.max_mr = attr.max_mr;
385 resp.max_pd = attr.max_pd;
386 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
387 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
388 resp.max_res_rd_atom = attr.max_res_rd_atom;
389 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
390 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
391 resp.atomic_cap = attr.atomic_cap;
392 resp.max_ee = attr.max_ee;
393 resp.max_rdd = attr.max_rdd;
394 resp.max_mw = attr.max_mw;
395 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
396 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
397 resp.max_mcast_grp = attr.max_mcast_grp;
398 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
399 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
400 resp.max_ah = attr.max_ah;
401 resp.max_fmr = attr.max_fmr;
402 resp.max_map_per_fmr = attr.max_map_per_fmr;
403 resp.max_srq = attr.max_srq;
404 resp.max_srq_wr = attr.max_srq_wr;
405 resp.max_srq_sge = attr.max_srq_sge;
406 resp.max_pkeys = attr.max_pkeys;
407 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
408 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
410 if (copy_to_user((void __user *) (unsigned long) cmd.response,
417 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
418 const char __user *buf,
419 int in_len, int out_len)
421 struct ib_uverbs_query_port cmd;
422 struct ib_uverbs_query_port_resp resp;
423 struct ib_port_attr attr;
426 if (out_len < sizeof resp)
429 if (copy_from_user(&cmd, buf, sizeof cmd))
432 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
436 memset(&resp, 0, sizeof resp);
438 resp.state = attr.state;
439 resp.max_mtu = attr.max_mtu;
440 resp.active_mtu = attr.active_mtu;
441 resp.gid_tbl_len = attr.gid_tbl_len;
442 resp.port_cap_flags = attr.port_cap_flags;
443 resp.max_msg_sz = attr.max_msg_sz;
444 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
445 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
446 resp.pkey_tbl_len = attr.pkey_tbl_len;
448 resp.sm_lid = attr.sm_lid;
450 resp.max_vl_num = attr.max_vl_num;
451 resp.sm_sl = attr.sm_sl;
452 resp.subnet_timeout = attr.subnet_timeout;
453 resp.init_type_reply = attr.init_type_reply;
454 resp.active_width = attr.active_width;
455 resp.active_speed = attr.active_speed;
456 resp.phys_state = attr.phys_state;
458 if (copy_to_user((void __user *) (unsigned long) cmd.response,
465 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
466 const char __user *buf,
467 int in_len, int out_len)
469 struct ib_uverbs_alloc_pd cmd;
470 struct ib_uverbs_alloc_pd_resp resp;
471 struct ib_udata udata;
472 struct ib_uobject *uobj;
476 if (out_len < sizeof resp)
479 if (copy_from_user(&cmd, buf, sizeof cmd))
482 INIT_UDATA(&udata, buf + sizeof cmd,
483 (unsigned long) cmd.response + sizeof resp,
484 in_len - sizeof cmd, out_len - sizeof resp);
486 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
490 init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
491 down_write(&uobj->mutex);
493 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
494 file->ucontext, &udata);
500 pd->device = file->device->ib_dev;
502 atomic_set(&pd->usecnt, 0);
505 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
509 memset(&resp, 0, sizeof resp);
510 resp.pd_handle = uobj->id;
512 if (copy_to_user((void __user *) (unsigned long) cmd.response,
513 &resp, sizeof resp)) {
518 mutex_lock(&file->mutex);
519 list_add_tail(&uobj->list, &file->ucontext->pd_list);
520 mutex_unlock(&file->mutex);
524 up_write(&uobj->mutex);
529 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
535 put_uobj_write(uobj);
539 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
540 const char __user *buf,
541 int in_len, int out_len)
543 struct ib_uverbs_dealloc_pd cmd;
544 struct ib_uobject *uobj;
547 if (copy_from_user(&cmd, buf, sizeof cmd))
550 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
554 ret = ib_dealloc_pd(uobj->object);
558 put_uobj_write(uobj);
563 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
565 mutex_lock(&file->mutex);
566 list_del(&uobj->list);
567 mutex_unlock(&file->mutex);
574 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
575 const char __user *buf, int in_len,
578 struct ib_uverbs_reg_mr cmd;
579 struct ib_uverbs_reg_mr_resp resp;
580 struct ib_udata udata;
581 struct ib_uobject *uobj;
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.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
600 * Local write permission is required if remote write or
601 * remote atomic permission is also requested.
603 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
604 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
607 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
611 init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
612 down_write(&uobj->mutex);
614 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
620 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
621 cmd.access_flags, &udata);
627 mr->device = pd->device;
630 atomic_inc(&pd->usecnt);
631 atomic_set(&mr->usecnt, 0);
634 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
638 memset(&resp, 0, sizeof resp);
639 resp.lkey = mr->lkey;
640 resp.rkey = mr->rkey;
641 resp.mr_handle = uobj->id;
643 if (copy_to_user((void __user *) (unsigned long) cmd.response,
644 &resp, sizeof resp)) {
651 mutex_lock(&file->mutex);
652 list_add_tail(&uobj->list, &file->ucontext->mr_list);
653 mutex_unlock(&file->mutex);
657 up_write(&uobj->mutex);
662 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
671 put_uobj_write(uobj);
675 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
676 const char __user *buf, int in_len,
679 struct ib_uverbs_dereg_mr cmd;
681 struct ib_uobject *uobj;
684 if (copy_from_user(&cmd, buf, sizeof cmd))
687 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
693 ret = ib_dereg_mr(mr);
697 put_uobj_write(uobj);
702 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
704 mutex_lock(&file->mutex);
705 list_del(&uobj->list);
706 mutex_unlock(&file->mutex);
713 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
714 const char __user *buf, int in_len,
717 struct ib_uverbs_create_comp_channel cmd;
718 struct ib_uverbs_create_comp_channel_resp resp;
721 if (out_len < sizeof resp)
724 if (copy_from_user(&cmd, buf, sizeof cmd))
727 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
729 return PTR_ERR(filp);
731 if (copy_to_user((void __user *) (unsigned long) cmd.response,
732 &resp, sizeof resp)) {
733 put_unused_fd(resp.fd);
738 fd_install(resp.fd, filp);
742 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
743 const char __user *buf, int in_len,
746 struct ib_uverbs_create_cq cmd;
747 struct ib_uverbs_create_cq_resp resp;
748 struct ib_udata udata;
749 struct ib_ucq_object *obj;
750 struct ib_uverbs_event_file *ev_file = NULL;
754 if (out_len < sizeof resp)
757 if (copy_from_user(&cmd, buf, sizeof cmd))
760 INIT_UDATA(&udata, buf + sizeof cmd,
761 (unsigned long) cmd.response + sizeof resp,
762 in_len - sizeof cmd, out_len - sizeof resp);
764 if (cmd.comp_vector >= file->device->num_comp_vectors)
767 obj = kmalloc(sizeof *obj, GFP_KERNEL);
771 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
772 down_write(&obj->uobject.mutex);
774 if (cmd.comp_channel >= 0) {
775 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
782 obj->uverbs_file = file;
783 obj->comp_events_reported = 0;
784 obj->async_events_reported = 0;
785 INIT_LIST_HEAD(&obj->comp_list);
786 INIT_LIST_HEAD(&obj->async_list);
788 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
790 file->ucontext, &udata);
796 cq->device = file->device->ib_dev;
797 cq->uobject = &obj->uobject;
798 cq->comp_handler = ib_uverbs_comp_handler;
799 cq->event_handler = ib_uverbs_cq_event_handler;
800 cq->cq_context = ev_file;
801 atomic_set(&cq->usecnt, 0);
803 obj->uobject.object = cq;
804 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
808 memset(&resp, 0, sizeof resp);
809 resp.cq_handle = obj->uobject.id;
812 if (copy_to_user((void __user *) (unsigned long) cmd.response,
813 &resp, sizeof resp)) {
818 mutex_lock(&file->mutex);
819 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
820 mutex_unlock(&file->mutex);
822 obj->uobject.live = 1;
824 up_write(&obj->uobject.mutex);
829 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
836 ib_uverbs_release_ucq(file, ev_file, obj);
839 put_uobj_write(&obj->uobject);
843 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
844 const char __user *buf, int in_len,
847 struct ib_uverbs_resize_cq cmd;
848 struct ib_uverbs_resize_cq_resp resp;
849 struct ib_udata udata;
853 if (copy_from_user(&cmd, buf, sizeof cmd))
856 INIT_UDATA(&udata, buf + sizeof cmd,
857 (unsigned long) cmd.response + sizeof resp,
858 in_len - sizeof cmd, out_len - sizeof resp);
860 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
864 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
870 if (copy_to_user((void __user *) (unsigned long) cmd.response,
871 &resp, sizeof resp.cqe))
877 return ret ? ret : in_len;
880 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
881 const char __user *buf, int in_len,
884 struct ib_uverbs_poll_cq cmd;
885 struct ib_uverbs_poll_cq_resp *resp;
892 if (copy_from_user(&cmd, buf, sizeof cmd))
895 wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
899 rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
900 resp = kmalloc(rsize, GFP_KERNEL);
906 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
912 resp->count = ib_poll_cq(cq, cmd.ne, wc);
916 for (i = 0; i < resp->count; i++) {
917 resp->wc[i].wr_id = wc[i].wr_id;
918 resp->wc[i].status = wc[i].status;
919 resp->wc[i].opcode = wc[i].opcode;
920 resp->wc[i].vendor_err = wc[i].vendor_err;
921 resp->wc[i].byte_len = wc[i].byte_len;
922 resp->wc[i].imm_data = (__u32 __force) wc[i].imm_data;
923 resp->wc[i].qp_num = wc[i].qp->qp_num;
924 resp->wc[i].src_qp = wc[i].src_qp;
925 resp->wc[i].wc_flags = wc[i].wc_flags;
926 resp->wc[i].pkey_index = wc[i].pkey_index;
927 resp->wc[i].slid = wc[i].slid;
928 resp->wc[i].sl = wc[i].sl;
929 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
930 resp->wc[i].port_num = wc[i].port_num;
933 if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
941 return ret ? ret : in_len;
944 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
945 const char __user *buf, int in_len,
948 struct ib_uverbs_req_notify_cq cmd;
951 if (copy_from_user(&cmd, buf, sizeof cmd))
954 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
958 ib_req_notify_cq(cq, cmd.solicited_only ?
959 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
966 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
967 const char __user *buf, int in_len,
970 struct ib_uverbs_destroy_cq cmd;
971 struct ib_uverbs_destroy_cq_resp resp;
972 struct ib_uobject *uobj;
974 struct ib_ucq_object *obj;
975 struct ib_uverbs_event_file *ev_file;
978 if (copy_from_user(&cmd, buf, sizeof cmd))
981 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
985 ev_file = cq->cq_context;
986 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
988 ret = ib_destroy_cq(cq);
992 put_uobj_write(uobj);
997 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
999 mutex_lock(&file->mutex);
1000 list_del(&uobj->list);
1001 mutex_unlock(&file->mutex);
1003 ib_uverbs_release_ucq(file, ev_file, obj);
1005 memset(&resp, 0, sizeof resp);
1006 resp.comp_events_reported = obj->comp_events_reported;
1007 resp.async_events_reported = obj->async_events_reported;
1011 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1012 &resp, sizeof resp))
1018 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1019 const char __user *buf, int in_len,
1022 struct ib_uverbs_create_qp cmd;
1023 struct ib_uverbs_create_qp_resp resp;
1024 struct ib_udata udata;
1025 struct ib_uqp_object *obj;
1027 struct ib_cq *scq, *rcq;
1030 struct ib_qp_init_attr attr;
1033 if (out_len < sizeof resp)
1036 if (copy_from_user(&cmd, buf, sizeof cmd))
1039 INIT_UDATA(&udata, buf + sizeof cmd,
1040 (unsigned long) cmd.response + sizeof resp,
1041 in_len - sizeof cmd, out_len - sizeof resp);
1043 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1047 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
1048 down_write(&obj->uevent.uobject.mutex);
1050 srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
1051 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1052 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
1053 rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
1054 scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
1056 if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
1061 attr.event_handler = ib_uverbs_qp_event_handler;
1062 attr.qp_context = file;
1066 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1067 attr.qp_type = cmd.qp_type;
1068 attr.create_flags = 0;
1070 attr.cap.max_send_wr = cmd.max_send_wr;
1071 attr.cap.max_recv_wr = cmd.max_recv_wr;
1072 attr.cap.max_send_sge = cmd.max_send_sge;
1073 attr.cap.max_recv_sge = cmd.max_recv_sge;
1074 attr.cap.max_inline_data = cmd.max_inline_data;
1076 obj->uevent.events_reported = 0;
1077 INIT_LIST_HEAD(&obj->uevent.event_list);
1078 INIT_LIST_HEAD(&obj->mcast_list);
1080 qp = pd->device->create_qp(pd, &attr, &udata);
1086 qp->device = pd->device;
1088 qp->send_cq = attr.send_cq;
1089 qp->recv_cq = attr.recv_cq;
1091 qp->uobject = &obj->uevent.uobject;
1092 qp->event_handler = attr.event_handler;
1093 qp->qp_context = attr.qp_context;
1094 qp->qp_type = attr.qp_type;
1095 atomic_inc(&pd->usecnt);
1096 atomic_inc(&attr.send_cq->usecnt);
1097 atomic_inc(&attr.recv_cq->usecnt);
1099 atomic_inc(&attr.srq->usecnt);
1101 obj->uevent.uobject.object = qp;
1102 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1106 memset(&resp, 0, sizeof resp);
1107 resp.qpn = qp->qp_num;
1108 resp.qp_handle = obj->uevent.uobject.id;
1109 resp.max_recv_sge = attr.cap.max_recv_sge;
1110 resp.max_send_sge = attr.cap.max_send_sge;
1111 resp.max_recv_wr = attr.cap.max_recv_wr;
1112 resp.max_send_wr = attr.cap.max_send_wr;
1113 resp.max_inline_data = attr.cap.max_inline_data;
1115 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1116 &resp, sizeof resp)) {
1128 mutex_lock(&file->mutex);
1129 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1130 mutex_unlock(&file->mutex);
1132 obj->uevent.uobject.live = 1;
1134 up_write(&obj->uevent.uobject.mutex);
1139 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1149 if (rcq && rcq != scq)
1154 put_uobj_write(&obj->uevent.uobject);
1158 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1159 const char __user *buf, int in_len,
1162 struct ib_uverbs_query_qp cmd;
1163 struct ib_uverbs_query_qp_resp resp;
1165 struct ib_qp_attr *attr;
1166 struct ib_qp_init_attr *init_attr;
1169 if (copy_from_user(&cmd, buf, sizeof cmd))
1172 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1173 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1174 if (!attr || !init_attr) {
1179 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1185 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1192 memset(&resp, 0, sizeof resp);
1194 resp.qp_state = attr->qp_state;
1195 resp.cur_qp_state = attr->cur_qp_state;
1196 resp.path_mtu = attr->path_mtu;
1197 resp.path_mig_state = attr->path_mig_state;
1198 resp.qkey = attr->qkey;
1199 resp.rq_psn = attr->rq_psn;
1200 resp.sq_psn = attr->sq_psn;
1201 resp.dest_qp_num = attr->dest_qp_num;
1202 resp.qp_access_flags = attr->qp_access_flags;
1203 resp.pkey_index = attr->pkey_index;
1204 resp.alt_pkey_index = attr->alt_pkey_index;
1205 resp.sq_draining = attr->sq_draining;
1206 resp.max_rd_atomic = attr->max_rd_atomic;
1207 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1208 resp.min_rnr_timer = attr->min_rnr_timer;
1209 resp.port_num = attr->port_num;
1210 resp.timeout = attr->timeout;
1211 resp.retry_cnt = attr->retry_cnt;
1212 resp.rnr_retry = attr->rnr_retry;
1213 resp.alt_port_num = attr->alt_port_num;
1214 resp.alt_timeout = attr->alt_timeout;
1216 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1217 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1218 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1219 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1220 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1221 resp.dest.dlid = attr->ah_attr.dlid;
1222 resp.dest.sl = attr->ah_attr.sl;
1223 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1224 resp.dest.static_rate = attr->ah_attr.static_rate;
1225 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1226 resp.dest.port_num = attr->ah_attr.port_num;
1228 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1229 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1230 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1231 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1232 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1233 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1234 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1235 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1236 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1237 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1238 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1240 resp.max_send_wr = init_attr->cap.max_send_wr;
1241 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1242 resp.max_send_sge = init_attr->cap.max_send_sge;
1243 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1244 resp.max_inline_data = init_attr->cap.max_inline_data;
1245 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1247 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1248 &resp, sizeof resp))
1255 return ret ? ret : in_len;
1258 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1259 const char __user *buf, int in_len,
1262 struct ib_uverbs_modify_qp cmd;
1263 struct ib_udata udata;
1265 struct ib_qp_attr *attr;
1268 if (copy_from_user(&cmd, buf, sizeof cmd))
1271 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1274 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1278 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1284 attr->qp_state = cmd.qp_state;
1285 attr->cur_qp_state = cmd.cur_qp_state;
1286 attr->path_mtu = cmd.path_mtu;
1287 attr->path_mig_state = cmd.path_mig_state;
1288 attr->qkey = cmd.qkey;
1289 attr->rq_psn = cmd.rq_psn;
1290 attr->sq_psn = cmd.sq_psn;
1291 attr->dest_qp_num = cmd.dest_qp_num;
1292 attr->qp_access_flags = cmd.qp_access_flags;
1293 attr->pkey_index = cmd.pkey_index;
1294 attr->alt_pkey_index = cmd.alt_pkey_index;
1295 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1296 attr->max_rd_atomic = cmd.max_rd_atomic;
1297 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1298 attr->min_rnr_timer = cmd.min_rnr_timer;
1299 attr->port_num = cmd.port_num;
1300 attr->timeout = cmd.timeout;
1301 attr->retry_cnt = cmd.retry_cnt;
1302 attr->rnr_retry = cmd.rnr_retry;
1303 attr->alt_port_num = cmd.alt_port_num;
1304 attr->alt_timeout = cmd.alt_timeout;
1306 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1307 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1308 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1309 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1310 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1311 attr->ah_attr.dlid = cmd.dest.dlid;
1312 attr->ah_attr.sl = cmd.dest.sl;
1313 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1314 attr->ah_attr.static_rate = cmd.dest.static_rate;
1315 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1316 attr->ah_attr.port_num = cmd.dest.port_num;
1318 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1319 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1320 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1321 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1322 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1323 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1324 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1325 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1326 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1327 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1328 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1330 ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
1345 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1346 const char __user *buf, int in_len,
1349 struct ib_uverbs_destroy_qp cmd;
1350 struct ib_uverbs_destroy_qp_resp resp;
1351 struct ib_uobject *uobj;
1353 struct ib_uqp_object *obj;
1356 if (copy_from_user(&cmd, buf, sizeof cmd))
1359 memset(&resp, 0, sizeof resp);
1361 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1365 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1367 if (!list_empty(&obj->mcast_list)) {
1368 put_uobj_write(uobj);
1372 ret = ib_destroy_qp(qp);
1376 put_uobj_write(uobj);
1381 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
1383 mutex_lock(&file->mutex);
1384 list_del(&uobj->list);
1385 mutex_unlock(&file->mutex);
1387 ib_uverbs_release_uevent(file, &obj->uevent);
1389 resp.events_reported = obj->uevent.events_reported;
1393 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1394 &resp, sizeof resp))
1400 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1401 const char __user *buf, int in_len,
1404 struct ib_uverbs_post_send cmd;
1405 struct ib_uverbs_post_send_resp resp;
1406 struct ib_uverbs_send_wr *user_wr;
1407 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
1411 ssize_t ret = -EINVAL;
1413 if (copy_from_user(&cmd, buf, sizeof cmd))
1416 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1417 cmd.sge_count * sizeof (struct ib_uverbs_sge))
1420 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1423 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1427 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1431 is_ud = qp->qp_type == IB_QPT_UD;
1434 for (i = 0; i < cmd.wr_count; ++i) {
1435 if (copy_from_user(user_wr,
1436 buf + sizeof cmd + i * cmd.wqe_size,
1442 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1447 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1448 user_wr->num_sge * sizeof (struct ib_sge),
1462 next->wr_id = user_wr->wr_id;
1463 next->num_sge = user_wr->num_sge;
1464 next->opcode = user_wr->opcode;
1465 next->send_flags = user_wr->send_flags;
1468 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1470 if (!next->wr.ud.ah) {
1474 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
1475 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1477 switch (next->opcode) {
1478 case IB_WR_RDMA_WRITE_WITH_IMM:
1480 (__be32 __force) user_wr->ex.imm_data;
1481 case IB_WR_RDMA_WRITE:
1482 case IB_WR_RDMA_READ:
1483 next->wr.rdma.remote_addr =
1484 user_wr->wr.rdma.remote_addr;
1485 next->wr.rdma.rkey =
1486 user_wr->wr.rdma.rkey;
1488 case IB_WR_SEND_WITH_IMM:
1490 (__be32 __force) user_wr->ex.imm_data;
1492 case IB_WR_SEND_WITH_INV:
1493 next->ex.invalidate_rkey =
1494 user_wr->ex.invalidate_rkey;
1496 case IB_WR_ATOMIC_CMP_AND_SWP:
1497 case IB_WR_ATOMIC_FETCH_AND_ADD:
1498 next->wr.atomic.remote_addr =
1499 user_wr->wr.atomic.remote_addr;
1500 next->wr.atomic.compare_add =
1501 user_wr->wr.atomic.compare_add;
1502 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1503 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1510 if (next->num_sge) {
1511 next->sg_list = (void *) next +
1512 ALIGN(sizeof *next, sizeof (struct ib_sge));
1513 if (copy_from_user(next->sg_list,
1515 cmd.wr_count * cmd.wqe_size +
1516 sg_ind * sizeof (struct ib_sge),
1517 next->num_sge * sizeof (struct ib_sge))) {
1521 sg_ind += next->num_sge;
1523 next->sg_list = NULL;
1527 ret = qp->device->post_send(qp, wr, &bad_wr);
1529 for (next = wr; next; next = next->next) {
1535 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1536 &resp, sizeof resp))
1543 if (is_ud && wr->wr.ud.ah)
1544 put_ah_read(wr->wr.ud.ah);
1553 return ret ? ret : in_len;
1556 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1562 struct ib_uverbs_recv_wr *user_wr;
1563 struct ib_recv_wr *wr = NULL, *last, *next;
1568 if (in_len < wqe_size * wr_count +
1569 sge_count * sizeof (struct ib_uverbs_sge))
1570 return ERR_PTR(-EINVAL);
1572 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1573 return ERR_PTR(-EINVAL);
1575 user_wr = kmalloc(wqe_size, GFP_KERNEL);
1577 return ERR_PTR(-ENOMEM);
1581 for (i = 0; i < wr_count; ++i) {
1582 if (copy_from_user(user_wr, buf + i * wqe_size,
1588 if (user_wr->num_sge + sg_ind > sge_count) {
1593 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1594 user_wr->num_sge * sizeof (struct ib_sge),
1608 next->wr_id = user_wr->wr_id;
1609 next->num_sge = user_wr->num_sge;
1611 if (next->num_sge) {
1612 next->sg_list = (void *) next +
1613 ALIGN(sizeof *next, sizeof (struct ib_sge));
1614 if (copy_from_user(next->sg_list,
1615 buf + wr_count * wqe_size +
1616 sg_ind * sizeof (struct ib_sge),
1617 next->num_sge * sizeof (struct ib_sge))) {
1621 sg_ind += next->num_sge;
1623 next->sg_list = NULL;
1638 return ERR_PTR(ret);
1641 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1642 const char __user *buf, int in_len,
1645 struct ib_uverbs_post_recv cmd;
1646 struct ib_uverbs_post_recv_resp resp;
1647 struct ib_recv_wr *wr, *next, *bad_wr;
1649 ssize_t ret = -EINVAL;
1651 if (copy_from_user(&cmd, buf, sizeof cmd))
1654 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1655 in_len - sizeof cmd, cmd.wr_count,
1656 cmd.sge_count, cmd.wqe_size);
1660 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1665 ret = qp->device->post_recv(qp, wr, &bad_wr);
1670 for (next = wr; next; next = next->next) {
1676 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1677 &resp, sizeof resp))
1687 return ret ? ret : in_len;
1690 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1691 const char __user *buf, int in_len,
1694 struct ib_uverbs_post_srq_recv cmd;
1695 struct ib_uverbs_post_srq_recv_resp resp;
1696 struct ib_recv_wr *wr, *next, *bad_wr;
1698 ssize_t ret = -EINVAL;
1700 if (copy_from_user(&cmd, buf, sizeof cmd))
1703 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1704 in_len - sizeof cmd, cmd.wr_count,
1705 cmd.sge_count, cmd.wqe_size);
1709 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1714 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1719 for (next = wr; next; next = next->next) {
1725 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1726 &resp, sizeof resp))
1736 return ret ? ret : in_len;
1739 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1740 const char __user *buf, int in_len,
1743 struct ib_uverbs_create_ah cmd;
1744 struct ib_uverbs_create_ah_resp resp;
1745 struct ib_uobject *uobj;
1748 struct ib_ah_attr attr;
1751 if (out_len < sizeof resp)
1754 if (copy_from_user(&cmd, buf, sizeof cmd))
1757 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1761 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
1762 down_write(&uobj->mutex);
1764 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1770 attr.dlid = cmd.attr.dlid;
1771 attr.sl = cmd.attr.sl;
1772 attr.src_path_bits = cmd.attr.src_path_bits;
1773 attr.static_rate = cmd.attr.static_rate;
1774 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
1775 attr.port_num = cmd.attr.port_num;
1776 attr.grh.flow_label = cmd.attr.grh.flow_label;
1777 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
1778 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
1779 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1780 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1782 ah = ib_create_ah(pd, &attr);
1791 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
1795 resp.ah_handle = uobj->id;
1797 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1798 &resp, sizeof resp)) {
1805 mutex_lock(&file->mutex);
1806 list_add_tail(&uobj->list, &file->ucontext->ah_list);
1807 mutex_unlock(&file->mutex);
1811 up_write(&uobj->mutex);
1816 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1825 put_uobj_write(uobj);
1829 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1830 const char __user *buf, int in_len, int out_len)
1832 struct ib_uverbs_destroy_ah cmd;
1834 struct ib_uobject *uobj;
1837 if (copy_from_user(&cmd, buf, sizeof cmd))
1840 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
1845 ret = ib_destroy_ah(ah);
1849 put_uobj_write(uobj);
1854 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1856 mutex_lock(&file->mutex);
1857 list_del(&uobj->list);
1858 mutex_unlock(&file->mutex);
1865 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1866 const char __user *buf, int in_len,
1869 struct ib_uverbs_attach_mcast cmd;
1871 struct ib_uqp_object *obj;
1872 struct ib_uverbs_mcast_entry *mcast;
1875 if (copy_from_user(&cmd, buf, sizeof cmd))
1878 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1882 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1884 list_for_each_entry(mcast, &obj->mcast_list, list)
1885 if (cmd.mlid == mcast->lid &&
1886 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1891 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1897 mcast->lid = cmd.mlid;
1898 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1900 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1902 list_add_tail(&mcast->list, &obj->mcast_list);
1909 return ret ? ret : in_len;
1912 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1913 const char __user *buf, int in_len,
1916 struct ib_uverbs_detach_mcast cmd;
1917 struct ib_uqp_object *obj;
1919 struct ib_uverbs_mcast_entry *mcast;
1922 if (copy_from_user(&cmd, buf, sizeof cmd))
1925 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1929 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1933 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1935 list_for_each_entry(mcast, &obj->mcast_list, list)
1936 if (cmd.mlid == mcast->lid &&
1937 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1938 list_del(&mcast->list);
1946 return ret ? ret : in_len;
1949 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1950 const char __user *buf, int in_len,
1953 struct ib_uverbs_create_srq cmd;
1954 struct ib_uverbs_create_srq_resp resp;
1955 struct ib_udata udata;
1956 struct ib_uevent_object *obj;
1959 struct ib_srq_init_attr attr;
1962 if (out_len < sizeof resp)
1965 if (copy_from_user(&cmd, buf, sizeof cmd))
1968 INIT_UDATA(&udata, buf + sizeof cmd,
1969 (unsigned long) cmd.response + sizeof resp,
1970 in_len - sizeof cmd, out_len - sizeof resp);
1972 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1976 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
1977 down_write(&obj->uobject.mutex);
1979 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1985 attr.event_handler = ib_uverbs_srq_event_handler;
1986 attr.srq_context = file;
1987 attr.attr.max_wr = cmd.max_wr;
1988 attr.attr.max_sge = cmd.max_sge;
1989 attr.attr.srq_limit = cmd.srq_limit;
1991 obj->events_reported = 0;
1992 INIT_LIST_HEAD(&obj->event_list);
1994 srq = pd->device->create_srq(pd, &attr, &udata);
2000 srq->device = pd->device;
2002 srq->uobject = &obj->uobject;
2003 srq->event_handler = attr.event_handler;
2004 srq->srq_context = attr.srq_context;
2005 atomic_inc(&pd->usecnt);
2006 atomic_set(&srq->usecnt, 0);
2008 obj->uobject.object = srq;
2009 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2013 memset(&resp, 0, sizeof resp);
2014 resp.srq_handle = obj->uobject.id;
2015 resp.max_wr = attr.attr.max_wr;
2016 resp.max_sge = attr.attr.max_sge;
2018 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2019 &resp, sizeof resp)) {
2026 mutex_lock(&file->mutex);
2027 list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
2028 mutex_unlock(&file->mutex);
2030 obj->uobject.live = 1;
2032 up_write(&obj->uobject.mutex);
2037 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2040 ib_destroy_srq(srq);
2046 put_uobj_write(&obj->uobject);
2050 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2051 const char __user *buf, int in_len,
2054 struct ib_uverbs_modify_srq cmd;
2055 struct ib_udata udata;
2057 struct ib_srq_attr attr;
2060 if (copy_from_user(&cmd, buf, sizeof cmd))
2063 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2066 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2070 attr.max_wr = cmd.max_wr;
2071 attr.srq_limit = cmd.srq_limit;
2073 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2077 return ret ? ret : in_len;
2080 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2081 const char __user *buf,
2082 int in_len, int out_len)
2084 struct ib_uverbs_query_srq cmd;
2085 struct ib_uverbs_query_srq_resp resp;
2086 struct ib_srq_attr attr;
2090 if (out_len < sizeof resp)
2093 if (copy_from_user(&cmd, buf, sizeof cmd))
2096 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2100 ret = ib_query_srq(srq, &attr);
2107 memset(&resp, 0, sizeof resp);
2109 resp.max_wr = attr.max_wr;
2110 resp.max_sge = attr.max_sge;
2111 resp.srq_limit = attr.srq_limit;
2113 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2114 &resp, sizeof resp))
2120 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2121 const char __user *buf, int in_len,
2124 struct ib_uverbs_destroy_srq cmd;
2125 struct ib_uverbs_destroy_srq_resp resp;
2126 struct ib_uobject *uobj;
2128 struct ib_uevent_object *obj;
2131 if (copy_from_user(&cmd, buf, sizeof cmd))
2134 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2138 obj = container_of(uobj, struct ib_uevent_object, uobject);
2140 ret = ib_destroy_srq(srq);
2144 put_uobj_write(uobj);
2149 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2151 mutex_lock(&file->mutex);
2152 list_del(&uobj->list);
2153 mutex_unlock(&file->mutex);
2155 ib_uverbs_release_uevent(file, obj);
2157 memset(&resp, 0, sizeof resp);
2158 resp.events_reported = obj->events_reported;
2162 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2163 &resp, sizeof resp))
2166 return ret ? ret : in_len;