2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
4 * Copyright (c) 2005 PathScale, Inc. All rights reserved.
5 * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
38 #include <linux/file.h>
41 #include <asm/uaccess.h>
45 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 kref_get(&uobj->ref);
152 spin_unlock(&ib_uverbs_idr_lock);
157 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
158 struct ib_ucontext *context, int nested)
160 struct ib_uobject *uobj;
162 uobj = __idr_get_uobj(idr, id, context);
167 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
169 down_read(&uobj->mutex);
178 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
179 struct ib_ucontext *context)
181 struct ib_uobject *uobj;
183 uobj = __idr_get_uobj(idr, id, context);
187 down_write(&uobj->mutex);
189 put_uobj_write(uobj);
196 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
199 struct ib_uobject *uobj;
201 uobj = idr_read_uobj(idr, id, context, nested);
202 return uobj ? uobj->object : NULL;
205 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
207 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
210 static void put_pd_read(struct ib_pd *pd)
212 put_uobj_read(pd->uobject);
215 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
217 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
220 static void put_cq_read(struct ib_cq *cq)
222 put_uobj_read(cq->uobject);
225 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
227 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
230 static void put_ah_read(struct ib_ah *ah)
232 put_uobj_read(ah->uobject);
235 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
237 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
240 static void put_qp_read(struct ib_qp *qp)
242 put_uobj_read(qp->uobject);
245 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
247 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
250 static void put_srq_read(struct ib_srq *srq)
252 put_uobj_read(srq->uobject);
255 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
256 const char __user *buf,
257 int in_len, int out_len)
259 struct ib_uverbs_get_context cmd;
260 struct ib_uverbs_get_context_resp resp;
261 struct ib_udata udata;
262 struct ib_device *ibdev = file->device->ib_dev;
263 struct ib_ucontext *ucontext;
267 if (out_len < sizeof resp)
270 if (copy_from_user(&cmd, buf, sizeof cmd))
273 mutex_lock(&file->mutex);
275 if (file->ucontext) {
280 INIT_UDATA(&udata, buf + sizeof cmd,
281 (unsigned long) cmd.response + sizeof resp,
282 in_len - sizeof cmd, out_len - sizeof resp);
284 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
285 if (IS_ERR(ucontext)) {
286 ret = PTR_ERR(file->ucontext);
290 ucontext->device = ibdev;
291 INIT_LIST_HEAD(&ucontext->pd_list);
292 INIT_LIST_HEAD(&ucontext->mr_list);
293 INIT_LIST_HEAD(&ucontext->mw_list);
294 INIT_LIST_HEAD(&ucontext->cq_list);
295 INIT_LIST_HEAD(&ucontext->qp_list);
296 INIT_LIST_HEAD(&ucontext->srq_list);
297 INIT_LIST_HEAD(&ucontext->ah_list);
299 resp.num_comp_vectors = file->device->num_comp_vectors;
301 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
307 if (copy_to_user((void __user *) (unsigned long) cmd.response,
308 &resp, sizeof resp)) {
313 file->async_file = filp->private_data;
315 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
316 ib_uverbs_event_handler);
317 ret = ib_register_event_handler(&file->event_handler);
321 kref_get(&file->async_file->ref);
322 kref_get(&file->ref);
323 file->ucontext = ucontext;
325 fd_install(resp.async_fd, filp);
327 mutex_unlock(&file->mutex);
332 put_unused_fd(resp.async_fd);
336 ibdev->dealloc_ucontext(ucontext);
339 mutex_unlock(&file->mutex);
343 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
344 const char __user *buf,
345 int in_len, int out_len)
347 struct ib_uverbs_query_device cmd;
348 struct ib_uverbs_query_device_resp resp;
349 struct ib_device_attr attr;
352 if (out_len < sizeof resp)
355 if (copy_from_user(&cmd, buf, sizeof cmd))
358 ret = ib_query_device(file->device->ib_dev, &attr);
362 memset(&resp, 0, sizeof resp);
364 resp.fw_ver = attr.fw_ver;
365 resp.node_guid = file->device->ib_dev->node_guid;
366 resp.sys_image_guid = attr.sys_image_guid;
367 resp.max_mr_size = attr.max_mr_size;
368 resp.page_size_cap = attr.page_size_cap;
369 resp.vendor_id = attr.vendor_id;
370 resp.vendor_part_id = attr.vendor_part_id;
371 resp.hw_ver = attr.hw_ver;
372 resp.max_qp = attr.max_qp;
373 resp.max_qp_wr = attr.max_qp_wr;
374 resp.device_cap_flags = attr.device_cap_flags;
375 resp.max_sge = attr.max_sge;
376 resp.max_sge_rd = attr.max_sge_rd;
377 resp.max_cq = attr.max_cq;
378 resp.max_cqe = attr.max_cqe;
379 resp.max_mr = attr.max_mr;
380 resp.max_pd = attr.max_pd;
381 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
382 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
383 resp.max_res_rd_atom = attr.max_res_rd_atom;
384 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
385 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
386 resp.atomic_cap = attr.atomic_cap;
387 resp.max_ee = attr.max_ee;
388 resp.max_rdd = attr.max_rdd;
389 resp.max_mw = attr.max_mw;
390 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
391 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
392 resp.max_mcast_grp = attr.max_mcast_grp;
393 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
394 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
395 resp.max_ah = attr.max_ah;
396 resp.max_fmr = attr.max_fmr;
397 resp.max_map_per_fmr = attr.max_map_per_fmr;
398 resp.max_srq = attr.max_srq;
399 resp.max_srq_wr = attr.max_srq_wr;
400 resp.max_srq_sge = attr.max_srq_sge;
401 resp.max_pkeys = attr.max_pkeys;
402 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
403 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
405 if (copy_to_user((void __user *) (unsigned long) cmd.response,
412 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
413 const char __user *buf,
414 int in_len, int out_len)
416 struct ib_uverbs_query_port cmd;
417 struct ib_uverbs_query_port_resp resp;
418 struct ib_port_attr attr;
421 if (out_len < sizeof resp)
424 if (copy_from_user(&cmd, buf, sizeof cmd))
427 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
431 memset(&resp, 0, sizeof resp);
433 resp.state = attr.state;
434 resp.max_mtu = attr.max_mtu;
435 resp.active_mtu = attr.active_mtu;
436 resp.gid_tbl_len = attr.gid_tbl_len;
437 resp.port_cap_flags = attr.port_cap_flags;
438 resp.max_msg_sz = attr.max_msg_sz;
439 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
440 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
441 resp.pkey_tbl_len = attr.pkey_tbl_len;
443 resp.sm_lid = attr.sm_lid;
445 resp.max_vl_num = attr.max_vl_num;
446 resp.sm_sl = attr.sm_sl;
447 resp.subnet_timeout = attr.subnet_timeout;
448 resp.init_type_reply = attr.init_type_reply;
449 resp.active_width = attr.active_width;
450 resp.active_speed = attr.active_speed;
451 resp.phys_state = attr.phys_state;
453 if (copy_to_user((void __user *) (unsigned long) cmd.response,
460 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
461 const char __user *buf,
462 int in_len, int out_len)
464 struct ib_uverbs_alloc_pd cmd;
465 struct ib_uverbs_alloc_pd_resp resp;
466 struct ib_udata udata;
467 struct ib_uobject *uobj;
471 if (out_len < sizeof resp)
474 if (copy_from_user(&cmd, buf, sizeof cmd))
477 INIT_UDATA(&udata, buf + sizeof cmd,
478 (unsigned long) cmd.response + sizeof resp,
479 in_len - sizeof cmd, out_len - sizeof resp);
481 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
485 init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
486 down_write(&uobj->mutex);
488 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
489 file->ucontext, &udata);
495 pd->device = file->device->ib_dev;
497 atomic_set(&pd->usecnt, 0);
500 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
504 memset(&resp, 0, sizeof resp);
505 resp.pd_handle = uobj->id;
507 if (copy_to_user((void __user *) (unsigned long) cmd.response,
508 &resp, sizeof resp)) {
513 mutex_lock(&file->mutex);
514 list_add_tail(&uobj->list, &file->ucontext->pd_list);
515 mutex_unlock(&file->mutex);
519 up_write(&uobj->mutex);
524 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
530 put_uobj_write(uobj);
534 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
535 const char __user *buf,
536 int in_len, int out_len)
538 struct ib_uverbs_dealloc_pd cmd;
539 struct ib_uobject *uobj;
542 if (copy_from_user(&cmd, buf, sizeof cmd))
545 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
549 ret = ib_dealloc_pd(uobj->object);
553 put_uobj_write(uobj);
558 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
560 mutex_lock(&file->mutex);
561 list_del(&uobj->list);
562 mutex_unlock(&file->mutex);
569 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
570 const char __user *buf, int in_len,
573 struct ib_uverbs_reg_mr cmd;
574 struct ib_uverbs_reg_mr_resp resp;
575 struct ib_udata udata;
576 struct ib_umem_object *obj;
581 if (out_len < sizeof resp)
584 if (copy_from_user(&cmd, buf, sizeof cmd))
587 INIT_UDATA(&udata, buf + sizeof cmd,
588 (unsigned long) cmd.response + sizeof resp,
589 in_len - sizeof cmd, out_len - sizeof resp);
591 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
595 * Local write permission is required if remote write or
596 * remote atomic permission is also requested.
598 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
599 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
602 obj = kmalloc(sizeof *obj, GFP_KERNEL);
606 init_uobj(&obj->uobject, 0, file->ucontext, &mr_lock_key);
607 down_write(&obj->uobject.mutex);
610 * We ask for writable memory if any access flags other than
611 * "remote read" are set. "Local write" and "remote write"
612 * obviously require write access. "Remote atomic" can do
613 * things like fetch and add, which will modify memory, and
614 * "MW bind" can change permissions by binding a window.
616 ret = ib_umem_get(file->device->ib_dev, &obj->umem,
617 (void *) (unsigned long) cmd.start, cmd.length,
618 !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
622 obj->umem.virt_base = cmd.hca_va;
624 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
630 mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
636 mr->device = pd->device;
638 mr->uobject = &obj->uobject;
639 atomic_inc(&pd->usecnt);
640 atomic_set(&mr->usecnt, 0);
642 obj->uobject.object = mr;
643 ret = idr_add_uobj(&ib_uverbs_mr_idr, &obj->uobject);
647 memset(&resp, 0, sizeof resp);
648 resp.lkey = mr->lkey;
649 resp.rkey = mr->rkey;
650 resp.mr_handle = obj->uobject.id;
652 if (copy_to_user((void __user *) (unsigned long) cmd.response,
653 &resp, sizeof resp)) {
660 mutex_lock(&file->mutex);
661 list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
662 mutex_unlock(&file->mutex);
664 obj->uobject.live = 1;
666 up_write(&obj->uobject.mutex);
671 idr_remove_uobj(&ib_uverbs_mr_idr, &obj->uobject);
680 ib_umem_release(file->device->ib_dev, &obj->umem);
683 put_uobj_write(&obj->uobject);
687 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
688 const char __user *buf, int in_len,
691 struct ib_uverbs_dereg_mr cmd;
693 struct ib_uobject *uobj;
694 struct ib_umem_object *memobj;
697 if (copy_from_user(&cmd, buf, sizeof cmd))
700 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
704 memobj = container_of(uobj, struct ib_umem_object, uobject);
707 ret = ib_dereg_mr(mr);
711 put_uobj_write(uobj);
716 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
718 mutex_lock(&file->mutex);
719 list_del(&uobj->list);
720 mutex_unlock(&file->mutex);
722 ib_umem_release(file->device->ib_dev, &memobj->umem);
729 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
730 const char __user *buf, int in_len,
733 struct ib_uverbs_create_comp_channel cmd;
734 struct ib_uverbs_create_comp_channel_resp resp;
737 if (out_len < sizeof resp)
740 if (copy_from_user(&cmd, buf, sizeof cmd))
743 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
745 return PTR_ERR(filp);
747 if (copy_to_user((void __user *) (unsigned long) cmd.response,
748 &resp, sizeof resp)) {
749 put_unused_fd(resp.fd);
754 fd_install(resp.fd, filp);
758 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
759 const char __user *buf, int in_len,
762 struct ib_uverbs_create_cq cmd;
763 struct ib_uverbs_create_cq_resp resp;
764 struct ib_udata udata;
765 struct ib_ucq_object *obj;
766 struct ib_uverbs_event_file *ev_file = NULL;
770 if (out_len < sizeof resp)
773 if (copy_from_user(&cmd, buf, sizeof cmd))
776 INIT_UDATA(&udata, buf + sizeof cmd,
777 (unsigned long) cmd.response + sizeof resp,
778 in_len - sizeof cmd, out_len - sizeof resp);
780 if (cmd.comp_vector >= file->device->num_comp_vectors)
783 obj = kmalloc(sizeof *obj, GFP_KERNEL);
787 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
788 down_write(&obj->uobject.mutex);
790 if (cmd.comp_channel >= 0) {
791 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
798 obj->uverbs_file = file;
799 obj->comp_events_reported = 0;
800 obj->async_events_reported = 0;
801 INIT_LIST_HEAD(&obj->comp_list);
802 INIT_LIST_HEAD(&obj->async_list);
804 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
806 file->ucontext, &udata);
812 cq->device = file->device->ib_dev;
813 cq->uobject = &obj->uobject;
814 cq->comp_handler = ib_uverbs_comp_handler;
815 cq->event_handler = ib_uverbs_cq_event_handler;
816 cq->cq_context = ev_file;
817 atomic_set(&cq->usecnt, 0);
819 obj->uobject.object = cq;
820 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
824 memset(&resp, 0, sizeof resp);
825 resp.cq_handle = obj->uobject.id;
828 if (copy_to_user((void __user *) (unsigned long) cmd.response,
829 &resp, sizeof resp)) {
834 mutex_lock(&file->mutex);
835 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
836 mutex_unlock(&file->mutex);
838 obj->uobject.live = 1;
840 up_write(&obj->uobject.mutex);
845 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
852 ib_uverbs_release_ucq(file, ev_file, obj);
855 put_uobj_write(&obj->uobject);
859 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
860 const char __user *buf, int in_len,
863 struct ib_uverbs_resize_cq cmd;
864 struct ib_uverbs_resize_cq_resp resp;
865 struct ib_udata udata;
869 if (copy_from_user(&cmd, buf, sizeof cmd))
872 INIT_UDATA(&udata, buf + sizeof cmd,
873 (unsigned long) cmd.response + sizeof resp,
874 in_len - sizeof cmd, out_len - sizeof resp);
876 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
880 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
886 if (copy_to_user((void __user *) (unsigned long) cmd.response,
887 &resp, sizeof resp.cqe))
893 return ret ? ret : in_len;
896 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
897 const char __user *buf, int in_len,
900 struct ib_uverbs_poll_cq cmd;
901 struct ib_uverbs_poll_cq_resp *resp;
908 if (copy_from_user(&cmd, buf, sizeof cmd))
911 wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
915 rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
916 resp = kmalloc(rsize, GFP_KERNEL);
922 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
928 resp->count = ib_poll_cq(cq, cmd.ne, wc);
932 for (i = 0; i < resp->count; i++) {
933 resp->wc[i].wr_id = wc[i].wr_id;
934 resp->wc[i].status = wc[i].status;
935 resp->wc[i].opcode = wc[i].opcode;
936 resp->wc[i].vendor_err = wc[i].vendor_err;
937 resp->wc[i].byte_len = wc[i].byte_len;
938 resp->wc[i].imm_data = (__u32 __force) wc[i].imm_data;
939 resp->wc[i].qp_num = wc[i].qp->qp_num;
940 resp->wc[i].src_qp = wc[i].src_qp;
941 resp->wc[i].wc_flags = wc[i].wc_flags;
942 resp->wc[i].pkey_index = wc[i].pkey_index;
943 resp->wc[i].slid = wc[i].slid;
944 resp->wc[i].sl = wc[i].sl;
945 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
946 resp->wc[i].port_num = wc[i].port_num;
949 if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
957 return ret ? ret : in_len;
960 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
961 const char __user *buf, int in_len,
964 struct ib_uverbs_req_notify_cq cmd;
967 if (copy_from_user(&cmd, buf, sizeof cmd))
970 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
974 ib_req_notify_cq(cq, cmd.solicited_only ?
975 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
982 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
983 const char __user *buf, int in_len,
986 struct ib_uverbs_destroy_cq cmd;
987 struct ib_uverbs_destroy_cq_resp resp;
988 struct ib_uobject *uobj;
990 struct ib_ucq_object *obj;
991 struct ib_uverbs_event_file *ev_file;
994 if (copy_from_user(&cmd, buf, sizeof cmd))
997 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1001 ev_file = cq->cq_context;
1002 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1004 ret = ib_destroy_cq(cq);
1008 put_uobj_write(uobj);
1013 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1015 mutex_lock(&file->mutex);
1016 list_del(&uobj->list);
1017 mutex_unlock(&file->mutex);
1019 ib_uverbs_release_ucq(file, ev_file, obj);
1021 memset(&resp, 0, sizeof resp);
1022 resp.comp_events_reported = obj->comp_events_reported;
1023 resp.async_events_reported = obj->async_events_reported;
1027 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1028 &resp, sizeof resp))
1034 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1035 const char __user *buf, int in_len,
1038 struct ib_uverbs_create_qp cmd;
1039 struct ib_uverbs_create_qp_resp resp;
1040 struct ib_udata udata;
1041 struct ib_uqp_object *obj;
1043 struct ib_cq *scq, *rcq;
1046 struct ib_qp_init_attr attr;
1049 if (out_len < sizeof resp)
1052 if (copy_from_user(&cmd, buf, sizeof cmd))
1055 INIT_UDATA(&udata, buf + sizeof cmd,
1056 (unsigned long) cmd.response + sizeof resp,
1057 in_len - sizeof cmd, out_len - sizeof resp);
1059 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1063 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
1064 down_write(&obj->uevent.uobject.mutex);
1066 srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
1067 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1068 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
1069 rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
1070 scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
1072 if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
1077 attr.event_handler = ib_uverbs_qp_event_handler;
1078 attr.qp_context = file;
1082 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1083 attr.qp_type = cmd.qp_type;
1085 attr.cap.max_send_wr = cmd.max_send_wr;
1086 attr.cap.max_recv_wr = cmd.max_recv_wr;
1087 attr.cap.max_send_sge = cmd.max_send_sge;
1088 attr.cap.max_recv_sge = cmd.max_recv_sge;
1089 attr.cap.max_inline_data = cmd.max_inline_data;
1091 obj->uevent.events_reported = 0;
1092 INIT_LIST_HEAD(&obj->uevent.event_list);
1093 INIT_LIST_HEAD(&obj->mcast_list);
1095 qp = pd->device->create_qp(pd, &attr, &udata);
1101 qp->device = pd->device;
1103 qp->send_cq = attr.send_cq;
1104 qp->recv_cq = attr.recv_cq;
1106 qp->uobject = &obj->uevent.uobject;
1107 qp->event_handler = attr.event_handler;
1108 qp->qp_context = attr.qp_context;
1109 qp->qp_type = attr.qp_type;
1110 atomic_inc(&pd->usecnt);
1111 atomic_inc(&attr.send_cq->usecnt);
1112 atomic_inc(&attr.recv_cq->usecnt);
1114 atomic_inc(&attr.srq->usecnt);
1116 obj->uevent.uobject.object = qp;
1117 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1121 memset(&resp, 0, sizeof resp);
1122 resp.qpn = qp->qp_num;
1123 resp.qp_handle = obj->uevent.uobject.id;
1124 resp.max_recv_sge = attr.cap.max_recv_sge;
1125 resp.max_send_sge = attr.cap.max_send_sge;
1126 resp.max_recv_wr = attr.cap.max_recv_wr;
1127 resp.max_send_wr = attr.cap.max_send_wr;
1128 resp.max_inline_data = attr.cap.max_inline_data;
1130 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1131 &resp, sizeof resp)) {
1143 mutex_lock(&file->mutex);
1144 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1145 mutex_unlock(&file->mutex);
1147 obj->uevent.uobject.live = 1;
1149 up_write(&obj->uevent.uobject.mutex);
1154 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1164 if (rcq && rcq != scq)
1169 put_uobj_write(&obj->uevent.uobject);
1173 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1174 const char __user *buf, int in_len,
1177 struct ib_uverbs_query_qp cmd;
1178 struct ib_uverbs_query_qp_resp resp;
1180 struct ib_qp_attr *attr;
1181 struct ib_qp_init_attr *init_attr;
1184 if (copy_from_user(&cmd, buf, sizeof cmd))
1187 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1188 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1189 if (!attr || !init_attr) {
1194 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1200 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1207 memset(&resp, 0, sizeof resp);
1209 resp.qp_state = attr->qp_state;
1210 resp.cur_qp_state = attr->cur_qp_state;
1211 resp.path_mtu = attr->path_mtu;
1212 resp.path_mig_state = attr->path_mig_state;
1213 resp.qkey = attr->qkey;
1214 resp.rq_psn = attr->rq_psn;
1215 resp.sq_psn = attr->sq_psn;
1216 resp.dest_qp_num = attr->dest_qp_num;
1217 resp.qp_access_flags = attr->qp_access_flags;
1218 resp.pkey_index = attr->pkey_index;
1219 resp.alt_pkey_index = attr->alt_pkey_index;
1220 resp.sq_draining = attr->sq_draining;
1221 resp.max_rd_atomic = attr->max_rd_atomic;
1222 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1223 resp.min_rnr_timer = attr->min_rnr_timer;
1224 resp.port_num = attr->port_num;
1225 resp.timeout = attr->timeout;
1226 resp.retry_cnt = attr->retry_cnt;
1227 resp.rnr_retry = attr->rnr_retry;
1228 resp.alt_port_num = attr->alt_port_num;
1229 resp.alt_timeout = attr->alt_timeout;
1231 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1232 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1233 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1234 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1235 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1236 resp.dest.dlid = attr->ah_attr.dlid;
1237 resp.dest.sl = attr->ah_attr.sl;
1238 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1239 resp.dest.static_rate = attr->ah_attr.static_rate;
1240 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1241 resp.dest.port_num = attr->ah_attr.port_num;
1243 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1244 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1245 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1246 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1247 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1248 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1249 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1250 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1251 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1252 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1253 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1255 resp.max_send_wr = init_attr->cap.max_send_wr;
1256 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1257 resp.max_send_sge = init_attr->cap.max_send_sge;
1258 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1259 resp.max_inline_data = init_attr->cap.max_inline_data;
1260 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1262 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1263 &resp, sizeof resp))
1270 return ret ? ret : in_len;
1273 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1274 const char __user *buf, int in_len,
1277 struct ib_uverbs_modify_qp cmd;
1278 struct ib_udata udata;
1280 struct ib_qp_attr *attr;
1283 if (copy_from_user(&cmd, buf, sizeof cmd))
1286 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1289 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1293 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1299 attr->qp_state = cmd.qp_state;
1300 attr->cur_qp_state = cmd.cur_qp_state;
1301 attr->path_mtu = cmd.path_mtu;
1302 attr->path_mig_state = cmd.path_mig_state;
1303 attr->qkey = cmd.qkey;
1304 attr->rq_psn = cmd.rq_psn;
1305 attr->sq_psn = cmd.sq_psn;
1306 attr->dest_qp_num = cmd.dest_qp_num;
1307 attr->qp_access_flags = cmd.qp_access_flags;
1308 attr->pkey_index = cmd.pkey_index;
1309 attr->alt_pkey_index = cmd.alt_pkey_index;
1310 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1311 attr->max_rd_atomic = cmd.max_rd_atomic;
1312 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1313 attr->min_rnr_timer = cmd.min_rnr_timer;
1314 attr->port_num = cmd.port_num;
1315 attr->timeout = cmd.timeout;
1316 attr->retry_cnt = cmd.retry_cnt;
1317 attr->rnr_retry = cmd.rnr_retry;
1318 attr->alt_port_num = cmd.alt_port_num;
1319 attr->alt_timeout = cmd.alt_timeout;
1321 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1322 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1323 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1324 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1325 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1326 attr->ah_attr.dlid = cmd.dest.dlid;
1327 attr->ah_attr.sl = cmd.dest.sl;
1328 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1329 attr->ah_attr.static_rate = cmd.dest.static_rate;
1330 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1331 attr->ah_attr.port_num = cmd.dest.port_num;
1333 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1334 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1335 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1336 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1337 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1338 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1339 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1340 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1341 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1342 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1343 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1345 ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
1360 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1361 const char __user *buf, int in_len,
1364 struct ib_uverbs_destroy_qp cmd;
1365 struct ib_uverbs_destroy_qp_resp resp;
1366 struct ib_uobject *uobj;
1368 struct ib_uqp_object *obj;
1371 if (copy_from_user(&cmd, buf, sizeof cmd))
1374 memset(&resp, 0, sizeof resp);
1376 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1380 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1382 if (!list_empty(&obj->mcast_list)) {
1383 put_uobj_write(uobj);
1387 ret = ib_destroy_qp(qp);
1391 put_uobj_write(uobj);
1396 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
1398 mutex_lock(&file->mutex);
1399 list_del(&uobj->list);
1400 mutex_unlock(&file->mutex);
1402 ib_uverbs_release_uevent(file, &obj->uevent);
1404 resp.events_reported = obj->uevent.events_reported;
1408 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1409 &resp, sizeof resp))
1415 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1416 const char __user *buf, int in_len,
1419 struct ib_uverbs_post_send cmd;
1420 struct ib_uverbs_post_send_resp resp;
1421 struct ib_uverbs_send_wr *user_wr;
1422 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
1426 ssize_t ret = -EINVAL;
1428 if (copy_from_user(&cmd, buf, sizeof cmd))
1431 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1432 cmd.sge_count * sizeof (struct ib_uverbs_sge))
1435 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1438 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1442 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1446 is_ud = qp->qp_type == IB_QPT_UD;
1449 for (i = 0; i < cmd.wr_count; ++i) {
1450 if (copy_from_user(user_wr,
1451 buf + sizeof cmd + i * cmd.wqe_size,
1457 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1462 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1463 user_wr->num_sge * sizeof (struct ib_sge),
1477 next->wr_id = user_wr->wr_id;
1478 next->num_sge = user_wr->num_sge;
1479 next->opcode = user_wr->opcode;
1480 next->send_flags = user_wr->send_flags;
1481 next->imm_data = (__be32 __force) user_wr->imm_data;
1484 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1486 if (!next->wr.ud.ah) {
1490 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
1491 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1493 switch (next->opcode) {
1494 case IB_WR_RDMA_WRITE:
1495 case IB_WR_RDMA_WRITE_WITH_IMM:
1496 case IB_WR_RDMA_READ:
1497 next->wr.rdma.remote_addr =
1498 user_wr->wr.rdma.remote_addr;
1499 next->wr.rdma.rkey =
1500 user_wr->wr.rdma.rkey;
1502 case IB_WR_ATOMIC_CMP_AND_SWP:
1503 case IB_WR_ATOMIC_FETCH_AND_ADD:
1504 next->wr.atomic.remote_addr =
1505 user_wr->wr.atomic.remote_addr;
1506 next->wr.atomic.compare_add =
1507 user_wr->wr.atomic.compare_add;
1508 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1509 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1516 if (next->num_sge) {
1517 next->sg_list = (void *) next +
1518 ALIGN(sizeof *next, sizeof (struct ib_sge));
1519 if (copy_from_user(next->sg_list,
1521 cmd.wr_count * cmd.wqe_size +
1522 sg_ind * sizeof (struct ib_sge),
1523 next->num_sge * sizeof (struct ib_sge))) {
1527 sg_ind += next->num_sge;
1529 next->sg_list = NULL;
1533 ret = qp->device->post_send(qp, wr, &bad_wr);
1535 for (next = wr; next; next = next->next) {
1541 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1542 &resp, sizeof resp))
1549 if (is_ud && wr->wr.ud.ah)
1550 put_ah_read(wr->wr.ud.ah);
1559 return ret ? ret : in_len;
1562 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1568 struct ib_uverbs_recv_wr *user_wr;
1569 struct ib_recv_wr *wr = NULL, *last, *next;
1574 if (in_len < wqe_size * wr_count +
1575 sge_count * sizeof (struct ib_uverbs_sge))
1576 return ERR_PTR(-EINVAL);
1578 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1579 return ERR_PTR(-EINVAL);
1581 user_wr = kmalloc(wqe_size, GFP_KERNEL);
1583 return ERR_PTR(-ENOMEM);
1587 for (i = 0; i < wr_count; ++i) {
1588 if (copy_from_user(user_wr, buf + i * wqe_size,
1594 if (user_wr->num_sge + sg_ind > sge_count) {
1599 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1600 user_wr->num_sge * sizeof (struct ib_sge),
1614 next->wr_id = user_wr->wr_id;
1615 next->num_sge = user_wr->num_sge;
1617 if (next->num_sge) {
1618 next->sg_list = (void *) next +
1619 ALIGN(sizeof *next, sizeof (struct ib_sge));
1620 if (copy_from_user(next->sg_list,
1621 buf + wr_count * wqe_size +
1622 sg_ind * sizeof (struct ib_sge),
1623 next->num_sge * sizeof (struct ib_sge))) {
1627 sg_ind += next->num_sge;
1629 next->sg_list = NULL;
1644 return ERR_PTR(ret);
1647 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1648 const char __user *buf, int in_len,
1651 struct ib_uverbs_post_recv cmd;
1652 struct ib_uverbs_post_recv_resp resp;
1653 struct ib_recv_wr *wr, *next, *bad_wr;
1655 ssize_t ret = -EINVAL;
1657 if (copy_from_user(&cmd, buf, sizeof cmd))
1660 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1661 in_len - sizeof cmd, cmd.wr_count,
1662 cmd.sge_count, cmd.wqe_size);
1666 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1671 ret = qp->device->post_recv(qp, wr, &bad_wr);
1676 for (next = wr; next; next = next->next) {
1682 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1683 &resp, sizeof resp))
1693 return ret ? ret : in_len;
1696 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1697 const char __user *buf, int in_len,
1700 struct ib_uverbs_post_srq_recv cmd;
1701 struct ib_uverbs_post_srq_recv_resp resp;
1702 struct ib_recv_wr *wr, *next, *bad_wr;
1704 ssize_t ret = -EINVAL;
1706 if (copy_from_user(&cmd, buf, sizeof cmd))
1709 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1710 in_len - sizeof cmd, cmd.wr_count,
1711 cmd.sge_count, cmd.wqe_size);
1715 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1720 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1725 for (next = wr; next; next = next->next) {
1731 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1732 &resp, sizeof resp))
1742 return ret ? ret : in_len;
1745 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1746 const char __user *buf, int in_len,
1749 struct ib_uverbs_create_ah cmd;
1750 struct ib_uverbs_create_ah_resp resp;
1751 struct ib_uobject *uobj;
1754 struct ib_ah_attr attr;
1757 if (out_len < sizeof resp)
1760 if (copy_from_user(&cmd, buf, sizeof cmd))
1763 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1767 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
1768 down_write(&uobj->mutex);
1770 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1776 attr.dlid = cmd.attr.dlid;
1777 attr.sl = cmd.attr.sl;
1778 attr.src_path_bits = cmd.attr.src_path_bits;
1779 attr.static_rate = cmd.attr.static_rate;
1780 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
1781 attr.port_num = cmd.attr.port_num;
1782 attr.grh.flow_label = cmd.attr.grh.flow_label;
1783 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
1784 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
1785 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1786 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1788 ah = ib_create_ah(pd, &attr);
1797 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
1801 resp.ah_handle = uobj->id;
1803 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1804 &resp, sizeof resp)) {
1811 mutex_lock(&file->mutex);
1812 list_add_tail(&uobj->list, &file->ucontext->ah_list);
1813 mutex_unlock(&file->mutex);
1817 up_write(&uobj->mutex);
1822 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1831 put_uobj_write(uobj);
1835 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1836 const char __user *buf, int in_len, int out_len)
1838 struct ib_uverbs_destroy_ah cmd;
1840 struct ib_uobject *uobj;
1843 if (copy_from_user(&cmd, buf, sizeof cmd))
1846 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
1851 ret = ib_destroy_ah(ah);
1855 put_uobj_write(uobj);
1860 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1862 mutex_lock(&file->mutex);
1863 list_del(&uobj->list);
1864 mutex_unlock(&file->mutex);
1871 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1872 const char __user *buf, int in_len,
1875 struct ib_uverbs_attach_mcast cmd;
1877 struct ib_uqp_object *obj;
1878 struct ib_uverbs_mcast_entry *mcast;
1881 if (copy_from_user(&cmd, buf, sizeof cmd))
1884 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1888 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1890 list_for_each_entry(mcast, &obj->mcast_list, list)
1891 if (cmd.mlid == mcast->lid &&
1892 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1897 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1903 mcast->lid = cmd.mlid;
1904 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1906 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1908 list_add_tail(&mcast->list, &obj->mcast_list);
1915 return ret ? ret : in_len;
1918 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1919 const char __user *buf, int in_len,
1922 struct ib_uverbs_detach_mcast cmd;
1923 struct ib_uqp_object *obj;
1925 struct ib_uverbs_mcast_entry *mcast;
1928 if (copy_from_user(&cmd, buf, sizeof cmd))
1931 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1935 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1939 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1941 list_for_each_entry(mcast, &obj->mcast_list, list)
1942 if (cmd.mlid == mcast->lid &&
1943 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1944 list_del(&mcast->list);
1952 return ret ? ret : in_len;
1955 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1956 const char __user *buf, int in_len,
1959 struct ib_uverbs_create_srq cmd;
1960 struct ib_uverbs_create_srq_resp resp;
1961 struct ib_udata udata;
1962 struct ib_uevent_object *obj;
1965 struct ib_srq_init_attr attr;
1968 if (out_len < sizeof resp)
1971 if (copy_from_user(&cmd, buf, sizeof cmd))
1974 INIT_UDATA(&udata, buf + sizeof cmd,
1975 (unsigned long) cmd.response + sizeof resp,
1976 in_len - sizeof cmd, out_len - sizeof resp);
1978 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1982 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
1983 down_write(&obj->uobject.mutex);
1985 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1991 attr.event_handler = ib_uverbs_srq_event_handler;
1992 attr.srq_context = file;
1993 attr.attr.max_wr = cmd.max_wr;
1994 attr.attr.max_sge = cmd.max_sge;
1995 attr.attr.srq_limit = cmd.srq_limit;
1997 obj->events_reported = 0;
1998 INIT_LIST_HEAD(&obj->event_list);
2000 srq = pd->device->create_srq(pd, &attr, &udata);
2006 srq->device = pd->device;
2008 srq->uobject = &obj->uobject;
2009 srq->event_handler = attr.event_handler;
2010 srq->srq_context = attr.srq_context;
2011 atomic_inc(&pd->usecnt);
2012 atomic_set(&srq->usecnt, 0);
2014 obj->uobject.object = srq;
2015 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2019 memset(&resp, 0, sizeof resp);
2020 resp.srq_handle = obj->uobject.id;
2021 resp.max_wr = attr.attr.max_wr;
2022 resp.max_sge = attr.attr.max_sge;
2024 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2025 &resp, sizeof resp)) {
2032 mutex_lock(&file->mutex);
2033 list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
2034 mutex_unlock(&file->mutex);
2036 obj->uobject.live = 1;
2038 up_write(&obj->uobject.mutex);
2043 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2046 ib_destroy_srq(srq);
2052 put_uobj_write(&obj->uobject);
2056 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2057 const char __user *buf, int in_len,
2060 struct ib_uverbs_modify_srq cmd;
2061 struct ib_udata udata;
2063 struct ib_srq_attr attr;
2066 if (copy_from_user(&cmd, buf, sizeof cmd))
2069 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2072 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2076 attr.max_wr = cmd.max_wr;
2077 attr.srq_limit = cmd.srq_limit;
2079 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2083 return ret ? ret : in_len;
2086 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2087 const char __user *buf,
2088 int in_len, int out_len)
2090 struct ib_uverbs_query_srq cmd;
2091 struct ib_uverbs_query_srq_resp resp;
2092 struct ib_srq_attr attr;
2096 if (out_len < sizeof resp)
2099 if (copy_from_user(&cmd, buf, sizeof cmd))
2102 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2106 ret = ib_query_srq(srq, &attr);
2113 memset(&resp, 0, sizeof resp);
2115 resp.max_wr = attr.max_wr;
2116 resp.max_sge = attr.max_sge;
2117 resp.srq_limit = attr.srq_limit;
2119 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2120 &resp, sizeof resp))
2126 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2127 const char __user *buf, int in_len,
2130 struct ib_uverbs_destroy_srq cmd;
2131 struct ib_uverbs_destroy_srq_resp resp;
2132 struct ib_uobject *uobj;
2134 struct ib_uevent_object *obj;
2137 if (copy_from_user(&cmd, buf, sizeof cmd))
2140 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2144 obj = container_of(uobj, struct ib_uevent_object, uobject);
2146 ret = ib_destroy_srq(srq);
2150 put_uobj_write(uobj);
2155 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2157 mutex_lock(&file->mutex);
2158 list_del(&uobj->list);
2159 mutex_unlock(&file->mutex);
2161 ib_uverbs_release_uevent(file, obj);
2163 memset(&resp, 0, sizeof resp);
2164 resp.events_reported = obj->events_reported;
2168 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2169 &resp, sizeof resp))
2172 return ret ? ret : in_len;