IB/core: convert to idr_alloc()
[linux/fpc-iii.git] / drivers / infiniband / core / uverbs_cmd.c
bloba7d00f6b3bc1c8b2c2ac5cfab340adebd375e7cb
1 /*
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
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
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
33 * SOFTWARE.
36 #include <linux/file.h>
37 #include <linux/fs.h>
38 #include <linux/slab.h>
40 #include <asm/uaccess.h>
42 #include "uverbs.h"
44 struct uverbs_lock_class {
45 struct lock_class_key key;
46 char name[16];
49 static struct uverbs_lock_class pd_lock_class = { .name = "PD-uobj" };
50 static struct uverbs_lock_class mr_lock_class = { .name = "MR-uobj" };
51 static struct uverbs_lock_class mw_lock_class = { .name = "MW-uobj" };
52 static struct uverbs_lock_class cq_lock_class = { .name = "CQ-uobj" };
53 static struct uverbs_lock_class qp_lock_class = { .name = "QP-uobj" };
54 static struct uverbs_lock_class ah_lock_class = { .name = "AH-uobj" };
55 static struct uverbs_lock_class srq_lock_class = { .name = "SRQ-uobj" };
56 static struct uverbs_lock_class xrcd_lock_class = { .name = "XRCD-uobj" };
58 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
59 do { \
60 (udata)->inbuf = (void __user *) (ibuf); \
61 (udata)->outbuf = (void __user *) (obuf); \
62 (udata)->inlen = (ilen); \
63 (udata)->outlen = (olen); \
64 } while (0)
67 * The ib_uobject locking scheme is as follows:
69 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
70 * needs to be held during all idr operations. When an object is
71 * looked up, a reference must be taken on the object's kref before
72 * dropping this lock.
74 * - Each object also has an rwsem. This rwsem must be held for
75 * reading while an operation that uses the object is performed.
76 * For example, while registering an MR, the associated PD's
77 * uobject.mutex must be held for reading. The rwsem must be held
78 * for writing while initializing or destroying an object.
80 * - In addition, each object has a "live" flag. If this flag is not
81 * set, then lookups of the object will fail even if it is found in
82 * the idr. This handles a reader that blocks and does not acquire
83 * the rwsem until after the object is destroyed. The destroy
84 * operation will set the live flag to 0 and then drop the rwsem;
85 * this will allow the reader to acquire the rwsem, see that the
86 * live flag is 0, and then drop the rwsem and its reference to
87 * object. The underlying storage will not be freed until the last
88 * reference to the object is dropped.
91 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
92 struct ib_ucontext *context, struct uverbs_lock_class *c)
94 uobj->user_handle = user_handle;
95 uobj->context = context;
96 kref_init(&uobj->ref);
97 init_rwsem(&uobj->mutex);
98 lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
99 uobj->live = 0;
102 static void release_uobj(struct kref *kref)
104 kfree(container_of(kref, struct ib_uobject, ref));
107 static void put_uobj(struct ib_uobject *uobj)
109 kref_put(&uobj->ref, release_uobj);
112 static void put_uobj_read(struct ib_uobject *uobj)
114 up_read(&uobj->mutex);
115 put_uobj(uobj);
118 static void put_uobj_write(struct ib_uobject *uobj)
120 up_write(&uobj->mutex);
121 put_uobj(uobj);
124 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
126 int ret;
128 idr_preload(GFP_KERNEL);
129 spin_lock(&ib_uverbs_idr_lock);
131 ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
132 if (ret >= 0)
133 uobj->id = ret;
135 spin_unlock(&ib_uverbs_idr_lock);
136 idr_preload_end();
138 return ret < 0 ? ret : 0;
141 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
143 spin_lock(&ib_uverbs_idr_lock);
144 idr_remove(idr, uobj->id);
145 spin_unlock(&ib_uverbs_idr_lock);
148 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
149 struct ib_ucontext *context)
151 struct ib_uobject *uobj;
153 spin_lock(&ib_uverbs_idr_lock);
154 uobj = idr_find(idr, id);
155 if (uobj) {
156 if (uobj->context == context)
157 kref_get(&uobj->ref);
158 else
159 uobj = NULL;
161 spin_unlock(&ib_uverbs_idr_lock);
163 return uobj;
166 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
167 struct ib_ucontext *context, int nested)
169 struct ib_uobject *uobj;
171 uobj = __idr_get_uobj(idr, id, context);
172 if (!uobj)
173 return NULL;
175 if (nested)
176 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
177 else
178 down_read(&uobj->mutex);
179 if (!uobj->live) {
180 put_uobj_read(uobj);
181 return NULL;
184 return uobj;
187 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
188 struct ib_ucontext *context)
190 struct ib_uobject *uobj;
192 uobj = __idr_get_uobj(idr, id, context);
193 if (!uobj)
194 return NULL;
196 down_write(&uobj->mutex);
197 if (!uobj->live) {
198 put_uobj_write(uobj);
199 return NULL;
202 return uobj;
205 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
206 int nested)
208 struct ib_uobject *uobj;
210 uobj = idr_read_uobj(idr, id, context, nested);
211 return uobj ? uobj->object : NULL;
214 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
216 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
219 static void put_pd_read(struct ib_pd *pd)
221 put_uobj_read(pd->uobject);
224 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
226 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
229 static void put_cq_read(struct ib_cq *cq)
231 put_uobj_read(cq->uobject);
234 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
236 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
239 static void put_ah_read(struct ib_ah *ah)
241 put_uobj_read(ah->uobject);
244 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
246 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
249 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
251 struct ib_uobject *uobj;
253 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
254 return uobj ? uobj->object : NULL;
257 static void put_qp_read(struct ib_qp *qp)
259 put_uobj_read(qp->uobject);
262 static void put_qp_write(struct ib_qp *qp)
264 put_uobj_write(qp->uobject);
267 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
269 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
272 static void put_srq_read(struct ib_srq *srq)
274 put_uobj_read(srq->uobject);
277 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
278 struct ib_uobject **uobj)
280 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
281 return *uobj ? (*uobj)->object : NULL;
284 static void put_xrcd_read(struct ib_uobject *uobj)
286 put_uobj_read(uobj);
289 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
290 const char __user *buf,
291 int in_len, int out_len)
293 struct ib_uverbs_get_context cmd;
294 struct ib_uverbs_get_context_resp resp;
295 struct ib_udata udata;
296 struct ib_device *ibdev = file->device->ib_dev;
297 struct ib_ucontext *ucontext;
298 struct file *filp;
299 int ret;
301 if (out_len < sizeof resp)
302 return -ENOSPC;
304 if (copy_from_user(&cmd, buf, sizeof cmd))
305 return -EFAULT;
307 mutex_lock(&file->mutex);
309 if (file->ucontext) {
310 ret = -EINVAL;
311 goto err;
314 INIT_UDATA(&udata, buf + sizeof cmd,
315 (unsigned long) cmd.response + sizeof resp,
316 in_len - sizeof cmd, out_len - sizeof resp);
318 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
319 if (IS_ERR(ucontext)) {
320 ret = PTR_ERR(ucontext);
321 goto err;
324 ucontext->device = ibdev;
325 INIT_LIST_HEAD(&ucontext->pd_list);
326 INIT_LIST_HEAD(&ucontext->mr_list);
327 INIT_LIST_HEAD(&ucontext->mw_list);
328 INIT_LIST_HEAD(&ucontext->cq_list);
329 INIT_LIST_HEAD(&ucontext->qp_list);
330 INIT_LIST_HEAD(&ucontext->srq_list);
331 INIT_LIST_HEAD(&ucontext->ah_list);
332 INIT_LIST_HEAD(&ucontext->xrcd_list);
333 ucontext->closing = 0;
335 resp.num_comp_vectors = file->device->num_comp_vectors;
337 ret = get_unused_fd();
338 if (ret < 0)
339 goto err_free;
340 resp.async_fd = ret;
342 filp = ib_uverbs_alloc_event_file(file, 1);
343 if (IS_ERR(filp)) {
344 ret = PTR_ERR(filp);
345 goto err_fd;
348 if (copy_to_user((void __user *) (unsigned long) cmd.response,
349 &resp, sizeof resp)) {
350 ret = -EFAULT;
351 goto err_file;
354 file->async_file = filp->private_data;
356 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
357 ib_uverbs_event_handler);
358 ret = ib_register_event_handler(&file->event_handler);
359 if (ret)
360 goto err_file;
362 kref_get(&file->async_file->ref);
363 kref_get(&file->ref);
364 file->ucontext = ucontext;
366 fd_install(resp.async_fd, filp);
368 mutex_unlock(&file->mutex);
370 return in_len;
372 err_file:
373 fput(filp);
375 err_fd:
376 put_unused_fd(resp.async_fd);
378 err_free:
379 ibdev->dealloc_ucontext(ucontext);
381 err:
382 mutex_unlock(&file->mutex);
383 return ret;
386 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
387 const char __user *buf,
388 int in_len, int out_len)
390 struct ib_uverbs_query_device cmd;
391 struct ib_uverbs_query_device_resp resp;
392 struct ib_device_attr attr;
393 int ret;
395 if (out_len < sizeof resp)
396 return -ENOSPC;
398 if (copy_from_user(&cmd, buf, sizeof cmd))
399 return -EFAULT;
401 ret = ib_query_device(file->device->ib_dev, &attr);
402 if (ret)
403 return ret;
405 memset(&resp, 0, sizeof resp);
407 resp.fw_ver = attr.fw_ver;
408 resp.node_guid = file->device->ib_dev->node_guid;
409 resp.sys_image_guid = attr.sys_image_guid;
410 resp.max_mr_size = attr.max_mr_size;
411 resp.page_size_cap = attr.page_size_cap;
412 resp.vendor_id = attr.vendor_id;
413 resp.vendor_part_id = attr.vendor_part_id;
414 resp.hw_ver = attr.hw_ver;
415 resp.max_qp = attr.max_qp;
416 resp.max_qp_wr = attr.max_qp_wr;
417 resp.device_cap_flags = attr.device_cap_flags;
418 resp.max_sge = attr.max_sge;
419 resp.max_sge_rd = attr.max_sge_rd;
420 resp.max_cq = attr.max_cq;
421 resp.max_cqe = attr.max_cqe;
422 resp.max_mr = attr.max_mr;
423 resp.max_pd = attr.max_pd;
424 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
425 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
426 resp.max_res_rd_atom = attr.max_res_rd_atom;
427 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
428 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
429 resp.atomic_cap = attr.atomic_cap;
430 resp.max_ee = attr.max_ee;
431 resp.max_rdd = attr.max_rdd;
432 resp.max_mw = attr.max_mw;
433 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
434 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
435 resp.max_mcast_grp = attr.max_mcast_grp;
436 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
437 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
438 resp.max_ah = attr.max_ah;
439 resp.max_fmr = attr.max_fmr;
440 resp.max_map_per_fmr = attr.max_map_per_fmr;
441 resp.max_srq = attr.max_srq;
442 resp.max_srq_wr = attr.max_srq_wr;
443 resp.max_srq_sge = attr.max_srq_sge;
444 resp.max_pkeys = attr.max_pkeys;
445 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
446 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
448 if (copy_to_user((void __user *) (unsigned long) cmd.response,
449 &resp, sizeof resp))
450 return -EFAULT;
452 return in_len;
455 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
456 const char __user *buf,
457 int in_len, int out_len)
459 struct ib_uverbs_query_port cmd;
460 struct ib_uverbs_query_port_resp resp;
461 struct ib_port_attr attr;
462 int ret;
464 if (out_len < sizeof resp)
465 return -ENOSPC;
467 if (copy_from_user(&cmd, buf, sizeof cmd))
468 return -EFAULT;
470 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
471 if (ret)
472 return ret;
474 memset(&resp, 0, sizeof resp);
476 resp.state = attr.state;
477 resp.max_mtu = attr.max_mtu;
478 resp.active_mtu = attr.active_mtu;
479 resp.gid_tbl_len = attr.gid_tbl_len;
480 resp.port_cap_flags = attr.port_cap_flags;
481 resp.max_msg_sz = attr.max_msg_sz;
482 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
483 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
484 resp.pkey_tbl_len = attr.pkey_tbl_len;
485 resp.lid = attr.lid;
486 resp.sm_lid = attr.sm_lid;
487 resp.lmc = attr.lmc;
488 resp.max_vl_num = attr.max_vl_num;
489 resp.sm_sl = attr.sm_sl;
490 resp.subnet_timeout = attr.subnet_timeout;
491 resp.init_type_reply = attr.init_type_reply;
492 resp.active_width = attr.active_width;
493 resp.active_speed = attr.active_speed;
494 resp.phys_state = attr.phys_state;
495 resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
496 cmd.port_num);
498 if (copy_to_user((void __user *) (unsigned long) cmd.response,
499 &resp, sizeof resp))
500 return -EFAULT;
502 return in_len;
505 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
506 const char __user *buf,
507 int in_len, int out_len)
509 struct ib_uverbs_alloc_pd cmd;
510 struct ib_uverbs_alloc_pd_resp resp;
511 struct ib_udata udata;
512 struct ib_uobject *uobj;
513 struct ib_pd *pd;
514 int ret;
516 if (out_len < sizeof resp)
517 return -ENOSPC;
519 if (copy_from_user(&cmd, buf, sizeof cmd))
520 return -EFAULT;
522 INIT_UDATA(&udata, buf + sizeof cmd,
523 (unsigned long) cmd.response + sizeof resp,
524 in_len - sizeof cmd, out_len - sizeof resp);
526 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
527 if (!uobj)
528 return -ENOMEM;
530 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
531 down_write(&uobj->mutex);
533 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
534 file->ucontext, &udata);
535 if (IS_ERR(pd)) {
536 ret = PTR_ERR(pd);
537 goto err;
540 pd->device = file->device->ib_dev;
541 pd->uobject = uobj;
542 atomic_set(&pd->usecnt, 0);
544 uobj->object = pd;
545 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
546 if (ret)
547 goto err_idr;
549 memset(&resp, 0, sizeof resp);
550 resp.pd_handle = uobj->id;
552 if (copy_to_user((void __user *) (unsigned long) cmd.response,
553 &resp, sizeof resp)) {
554 ret = -EFAULT;
555 goto err_copy;
558 mutex_lock(&file->mutex);
559 list_add_tail(&uobj->list, &file->ucontext->pd_list);
560 mutex_unlock(&file->mutex);
562 uobj->live = 1;
564 up_write(&uobj->mutex);
566 return in_len;
568 err_copy:
569 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
571 err_idr:
572 ib_dealloc_pd(pd);
574 err:
575 put_uobj_write(uobj);
576 return ret;
579 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
580 const char __user *buf,
581 int in_len, int out_len)
583 struct ib_uverbs_dealloc_pd cmd;
584 struct ib_uobject *uobj;
585 int ret;
587 if (copy_from_user(&cmd, buf, sizeof cmd))
588 return -EFAULT;
590 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
591 if (!uobj)
592 return -EINVAL;
594 ret = ib_dealloc_pd(uobj->object);
595 if (!ret)
596 uobj->live = 0;
598 put_uobj_write(uobj);
600 if (ret)
601 return ret;
603 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
605 mutex_lock(&file->mutex);
606 list_del(&uobj->list);
607 mutex_unlock(&file->mutex);
609 put_uobj(uobj);
611 return in_len;
614 struct xrcd_table_entry {
615 struct rb_node node;
616 struct ib_xrcd *xrcd;
617 struct inode *inode;
620 static int xrcd_table_insert(struct ib_uverbs_device *dev,
621 struct inode *inode,
622 struct ib_xrcd *xrcd)
624 struct xrcd_table_entry *entry, *scan;
625 struct rb_node **p = &dev->xrcd_tree.rb_node;
626 struct rb_node *parent = NULL;
628 entry = kmalloc(sizeof *entry, GFP_KERNEL);
629 if (!entry)
630 return -ENOMEM;
632 entry->xrcd = xrcd;
633 entry->inode = inode;
635 while (*p) {
636 parent = *p;
637 scan = rb_entry(parent, struct xrcd_table_entry, node);
639 if (inode < scan->inode) {
640 p = &(*p)->rb_left;
641 } else if (inode > scan->inode) {
642 p = &(*p)->rb_right;
643 } else {
644 kfree(entry);
645 return -EEXIST;
649 rb_link_node(&entry->node, parent, p);
650 rb_insert_color(&entry->node, &dev->xrcd_tree);
651 igrab(inode);
652 return 0;
655 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
656 struct inode *inode)
658 struct xrcd_table_entry *entry;
659 struct rb_node *p = dev->xrcd_tree.rb_node;
661 while (p) {
662 entry = rb_entry(p, struct xrcd_table_entry, node);
664 if (inode < entry->inode)
665 p = p->rb_left;
666 else if (inode > entry->inode)
667 p = p->rb_right;
668 else
669 return entry;
672 return NULL;
675 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
677 struct xrcd_table_entry *entry;
679 entry = xrcd_table_search(dev, inode);
680 if (!entry)
681 return NULL;
683 return entry->xrcd;
686 static void xrcd_table_delete(struct ib_uverbs_device *dev,
687 struct inode *inode)
689 struct xrcd_table_entry *entry;
691 entry = xrcd_table_search(dev, inode);
692 if (entry) {
693 iput(inode);
694 rb_erase(&entry->node, &dev->xrcd_tree);
695 kfree(entry);
699 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
700 const char __user *buf, int in_len,
701 int out_len)
703 struct ib_uverbs_open_xrcd cmd;
704 struct ib_uverbs_open_xrcd_resp resp;
705 struct ib_udata udata;
706 struct ib_uxrcd_object *obj;
707 struct ib_xrcd *xrcd = NULL;
708 struct fd f = {NULL, 0};
709 struct inode *inode = NULL;
710 int ret = 0;
711 int new_xrcd = 0;
713 if (out_len < sizeof resp)
714 return -ENOSPC;
716 if (copy_from_user(&cmd, buf, sizeof cmd))
717 return -EFAULT;
719 INIT_UDATA(&udata, buf + sizeof cmd,
720 (unsigned long) cmd.response + sizeof resp,
721 in_len - sizeof cmd, out_len - sizeof resp);
723 mutex_lock(&file->device->xrcd_tree_mutex);
725 if (cmd.fd != -1) {
726 /* search for file descriptor */
727 f = fdget(cmd.fd);
728 if (!f.file) {
729 ret = -EBADF;
730 goto err_tree_mutex_unlock;
733 inode = file_inode(f.file);
734 xrcd = find_xrcd(file->device, inode);
735 if (!xrcd && !(cmd.oflags & O_CREAT)) {
736 /* no file descriptor. Need CREATE flag */
737 ret = -EAGAIN;
738 goto err_tree_mutex_unlock;
741 if (xrcd && cmd.oflags & O_EXCL) {
742 ret = -EINVAL;
743 goto err_tree_mutex_unlock;
747 obj = kmalloc(sizeof *obj, GFP_KERNEL);
748 if (!obj) {
749 ret = -ENOMEM;
750 goto err_tree_mutex_unlock;
753 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
755 down_write(&obj->uobject.mutex);
757 if (!xrcd) {
758 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
759 file->ucontext, &udata);
760 if (IS_ERR(xrcd)) {
761 ret = PTR_ERR(xrcd);
762 goto err;
765 xrcd->inode = inode;
766 xrcd->device = file->device->ib_dev;
767 atomic_set(&xrcd->usecnt, 0);
768 mutex_init(&xrcd->tgt_qp_mutex);
769 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
770 new_xrcd = 1;
773 atomic_set(&obj->refcnt, 0);
774 obj->uobject.object = xrcd;
775 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
776 if (ret)
777 goto err_idr;
779 memset(&resp, 0, sizeof resp);
780 resp.xrcd_handle = obj->uobject.id;
782 if (inode) {
783 if (new_xrcd) {
784 /* create new inode/xrcd table entry */
785 ret = xrcd_table_insert(file->device, inode, xrcd);
786 if (ret)
787 goto err_insert_xrcd;
789 atomic_inc(&xrcd->usecnt);
792 if (copy_to_user((void __user *) (unsigned long) cmd.response,
793 &resp, sizeof resp)) {
794 ret = -EFAULT;
795 goto err_copy;
798 if (f.file)
799 fdput(f);
801 mutex_lock(&file->mutex);
802 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
803 mutex_unlock(&file->mutex);
805 obj->uobject.live = 1;
806 up_write(&obj->uobject.mutex);
808 mutex_unlock(&file->device->xrcd_tree_mutex);
809 return in_len;
811 err_copy:
812 if (inode) {
813 if (new_xrcd)
814 xrcd_table_delete(file->device, inode);
815 atomic_dec(&xrcd->usecnt);
818 err_insert_xrcd:
819 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
821 err_idr:
822 ib_dealloc_xrcd(xrcd);
824 err:
825 put_uobj_write(&obj->uobject);
827 err_tree_mutex_unlock:
828 if (f.file)
829 fdput(f);
831 mutex_unlock(&file->device->xrcd_tree_mutex);
833 return ret;
836 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
837 const char __user *buf, int in_len,
838 int out_len)
840 struct ib_uverbs_close_xrcd cmd;
841 struct ib_uobject *uobj;
842 struct ib_xrcd *xrcd = NULL;
843 struct inode *inode = NULL;
844 struct ib_uxrcd_object *obj;
845 int live;
846 int ret = 0;
848 if (copy_from_user(&cmd, buf, sizeof cmd))
849 return -EFAULT;
851 mutex_lock(&file->device->xrcd_tree_mutex);
852 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
853 if (!uobj) {
854 ret = -EINVAL;
855 goto out;
858 xrcd = uobj->object;
859 inode = xrcd->inode;
860 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
861 if (atomic_read(&obj->refcnt)) {
862 put_uobj_write(uobj);
863 ret = -EBUSY;
864 goto out;
867 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
868 ret = ib_dealloc_xrcd(uobj->object);
869 if (!ret)
870 uobj->live = 0;
873 live = uobj->live;
874 if (inode && ret)
875 atomic_inc(&xrcd->usecnt);
877 put_uobj_write(uobj);
879 if (ret)
880 goto out;
882 if (inode && !live)
883 xrcd_table_delete(file->device, inode);
885 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
886 mutex_lock(&file->mutex);
887 list_del(&uobj->list);
888 mutex_unlock(&file->mutex);
890 put_uobj(uobj);
891 ret = in_len;
893 out:
894 mutex_unlock(&file->device->xrcd_tree_mutex);
895 return ret;
898 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
899 struct ib_xrcd *xrcd)
901 struct inode *inode;
903 inode = xrcd->inode;
904 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
905 return;
907 ib_dealloc_xrcd(xrcd);
909 if (inode)
910 xrcd_table_delete(dev, inode);
913 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
914 const char __user *buf, int in_len,
915 int out_len)
917 struct ib_uverbs_reg_mr cmd;
918 struct ib_uverbs_reg_mr_resp resp;
919 struct ib_udata udata;
920 struct ib_uobject *uobj;
921 struct ib_pd *pd;
922 struct ib_mr *mr;
923 int ret;
925 if (out_len < sizeof resp)
926 return -ENOSPC;
928 if (copy_from_user(&cmd, buf, sizeof cmd))
929 return -EFAULT;
931 INIT_UDATA(&udata, buf + sizeof cmd,
932 (unsigned long) cmd.response + sizeof resp,
933 in_len - sizeof cmd, out_len - sizeof resp);
935 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
936 return -EINVAL;
939 * Local write permission is required if remote write or
940 * remote atomic permission is also requested.
942 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
943 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
944 return -EINVAL;
946 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
947 if (!uobj)
948 return -ENOMEM;
950 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
951 down_write(&uobj->mutex);
953 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
954 if (!pd) {
955 ret = -EINVAL;
956 goto err_free;
959 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
960 cmd.access_flags, &udata);
961 if (IS_ERR(mr)) {
962 ret = PTR_ERR(mr);
963 goto err_put;
966 mr->device = pd->device;
967 mr->pd = pd;
968 mr->uobject = uobj;
969 atomic_inc(&pd->usecnt);
970 atomic_set(&mr->usecnt, 0);
972 uobj->object = mr;
973 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
974 if (ret)
975 goto err_unreg;
977 memset(&resp, 0, sizeof resp);
978 resp.lkey = mr->lkey;
979 resp.rkey = mr->rkey;
980 resp.mr_handle = uobj->id;
982 if (copy_to_user((void __user *) (unsigned long) cmd.response,
983 &resp, sizeof resp)) {
984 ret = -EFAULT;
985 goto err_copy;
988 put_pd_read(pd);
990 mutex_lock(&file->mutex);
991 list_add_tail(&uobj->list, &file->ucontext->mr_list);
992 mutex_unlock(&file->mutex);
994 uobj->live = 1;
996 up_write(&uobj->mutex);
998 return in_len;
1000 err_copy:
1001 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1003 err_unreg:
1004 ib_dereg_mr(mr);
1006 err_put:
1007 put_pd_read(pd);
1009 err_free:
1010 put_uobj_write(uobj);
1011 return ret;
1014 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1015 const char __user *buf, int in_len,
1016 int out_len)
1018 struct ib_uverbs_dereg_mr cmd;
1019 struct ib_mr *mr;
1020 struct ib_uobject *uobj;
1021 int ret = -EINVAL;
1023 if (copy_from_user(&cmd, buf, sizeof cmd))
1024 return -EFAULT;
1026 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1027 if (!uobj)
1028 return -EINVAL;
1030 mr = uobj->object;
1032 ret = ib_dereg_mr(mr);
1033 if (!ret)
1034 uobj->live = 0;
1036 put_uobj_write(uobj);
1038 if (ret)
1039 return ret;
1041 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1043 mutex_lock(&file->mutex);
1044 list_del(&uobj->list);
1045 mutex_unlock(&file->mutex);
1047 put_uobj(uobj);
1049 return in_len;
1052 ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
1053 const char __user *buf, int in_len,
1054 int out_len)
1056 struct ib_uverbs_alloc_mw cmd;
1057 struct ib_uverbs_alloc_mw_resp resp;
1058 struct ib_uobject *uobj;
1059 struct ib_pd *pd;
1060 struct ib_mw *mw;
1061 int ret;
1063 if (out_len < sizeof(resp))
1064 return -ENOSPC;
1066 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1067 return -EFAULT;
1069 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1070 if (!uobj)
1071 return -ENOMEM;
1073 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1074 down_write(&uobj->mutex);
1076 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1077 if (!pd) {
1078 ret = -EINVAL;
1079 goto err_free;
1082 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1083 if (IS_ERR(mw)) {
1084 ret = PTR_ERR(mw);
1085 goto err_put;
1088 mw->device = pd->device;
1089 mw->pd = pd;
1090 mw->uobject = uobj;
1091 atomic_inc(&pd->usecnt);
1093 uobj->object = mw;
1094 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1095 if (ret)
1096 goto err_unalloc;
1098 memset(&resp, 0, sizeof(resp));
1099 resp.rkey = mw->rkey;
1100 resp.mw_handle = uobj->id;
1102 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1103 &resp, sizeof(resp))) {
1104 ret = -EFAULT;
1105 goto err_copy;
1108 put_pd_read(pd);
1110 mutex_lock(&file->mutex);
1111 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1112 mutex_unlock(&file->mutex);
1114 uobj->live = 1;
1116 up_write(&uobj->mutex);
1118 return in_len;
1120 err_copy:
1121 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1123 err_unalloc:
1124 ib_dealloc_mw(mw);
1126 err_put:
1127 put_pd_read(pd);
1129 err_free:
1130 put_uobj_write(uobj);
1131 return ret;
1134 ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
1135 const char __user *buf, int in_len,
1136 int out_len)
1138 struct ib_uverbs_dealloc_mw cmd;
1139 struct ib_mw *mw;
1140 struct ib_uobject *uobj;
1141 int ret = -EINVAL;
1143 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1144 return -EFAULT;
1146 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1147 if (!uobj)
1148 return -EINVAL;
1150 mw = uobj->object;
1152 ret = ib_dealloc_mw(mw);
1153 if (!ret)
1154 uobj->live = 0;
1156 put_uobj_write(uobj);
1158 if (ret)
1159 return ret;
1161 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1163 mutex_lock(&file->mutex);
1164 list_del(&uobj->list);
1165 mutex_unlock(&file->mutex);
1167 put_uobj(uobj);
1169 return in_len;
1172 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1173 const char __user *buf, int in_len,
1174 int out_len)
1176 struct ib_uverbs_create_comp_channel cmd;
1177 struct ib_uverbs_create_comp_channel_resp resp;
1178 struct file *filp;
1179 int ret;
1181 if (out_len < sizeof resp)
1182 return -ENOSPC;
1184 if (copy_from_user(&cmd, buf, sizeof cmd))
1185 return -EFAULT;
1187 ret = get_unused_fd();
1188 if (ret < 0)
1189 return ret;
1190 resp.fd = ret;
1192 filp = ib_uverbs_alloc_event_file(file, 0);
1193 if (IS_ERR(filp)) {
1194 put_unused_fd(resp.fd);
1195 return PTR_ERR(filp);
1198 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1199 &resp, sizeof resp)) {
1200 put_unused_fd(resp.fd);
1201 fput(filp);
1202 return -EFAULT;
1205 fd_install(resp.fd, filp);
1206 return in_len;
1209 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1210 const char __user *buf, int in_len,
1211 int out_len)
1213 struct ib_uverbs_create_cq cmd;
1214 struct ib_uverbs_create_cq_resp resp;
1215 struct ib_udata udata;
1216 struct ib_ucq_object *obj;
1217 struct ib_uverbs_event_file *ev_file = NULL;
1218 struct ib_cq *cq;
1219 int ret;
1221 if (out_len < sizeof resp)
1222 return -ENOSPC;
1224 if (copy_from_user(&cmd, buf, sizeof cmd))
1225 return -EFAULT;
1227 INIT_UDATA(&udata, buf + sizeof cmd,
1228 (unsigned long) cmd.response + sizeof resp,
1229 in_len - sizeof cmd, out_len - sizeof resp);
1231 if (cmd.comp_vector >= file->device->num_comp_vectors)
1232 return -EINVAL;
1234 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1235 if (!obj)
1236 return -ENOMEM;
1238 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1239 down_write(&obj->uobject.mutex);
1241 if (cmd.comp_channel >= 0) {
1242 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1243 if (!ev_file) {
1244 ret = -EINVAL;
1245 goto err;
1249 obj->uverbs_file = file;
1250 obj->comp_events_reported = 0;
1251 obj->async_events_reported = 0;
1252 INIT_LIST_HEAD(&obj->comp_list);
1253 INIT_LIST_HEAD(&obj->async_list);
1255 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1256 cmd.comp_vector,
1257 file->ucontext, &udata);
1258 if (IS_ERR(cq)) {
1259 ret = PTR_ERR(cq);
1260 goto err_file;
1263 cq->device = file->device->ib_dev;
1264 cq->uobject = &obj->uobject;
1265 cq->comp_handler = ib_uverbs_comp_handler;
1266 cq->event_handler = ib_uverbs_cq_event_handler;
1267 cq->cq_context = ev_file;
1268 atomic_set(&cq->usecnt, 0);
1270 obj->uobject.object = cq;
1271 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1272 if (ret)
1273 goto err_free;
1275 memset(&resp, 0, sizeof resp);
1276 resp.cq_handle = obj->uobject.id;
1277 resp.cqe = cq->cqe;
1279 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1280 &resp, sizeof resp)) {
1281 ret = -EFAULT;
1282 goto err_copy;
1285 mutex_lock(&file->mutex);
1286 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1287 mutex_unlock(&file->mutex);
1289 obj->uobject.live = 1;
1291 up_write(&obj->uobject.mutex);
1293 return in_len;
1295 err_copy:
1296 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1298 err_free:
1299 ib_destroy_cq(cq);
1301 err_file:
1302 if (ev_file)
1303 ib_uverbs_release_ucq(file, ev_file, obj);
1305 err:
1306 put_uobj_write(&obj->uobject);
1307 return ret;
1310 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1311 const char __user *buf, int in_len,
1312 int out_len)
1314 struct ib_uverbs_resize_cq cmd;
1315 struct ib_uverbs_resize_cq_resp resp;
1316 struct ib_udata udata;
1317 struct ib_cq *cq;
1318 int ret = -EINVAL;
1320 if (copy_from_user(&cmd, buf, sizeof cmd))
1321 return -EFAULT;
1323 INIT_UDATA(&udata, buf + sizeof cmd,
1324 (unsigned long) cmd.response + sizeof resp,
1325 in_len - sizeof cmd, out_len - sizeof resp);
1327 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1328 if (!cq)
1329 return -EINVAL;
1331 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1332 if (ret)
1333 goto out;
1335 resp.cqe = cq->cqe;
1337 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1338 &resp, sizeof resp.cqe))
1339 ret = -EFAULT;
1341 out:
1342 put_cq_read(cq);
1344 return ret ? ret : in_len;
1347 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1349 struct ib_uverbs_wc tmp;
1351 tmp.wr_id = wc->wr_id;
1352 tmp.status = wc->status;
1353 tmp.opcode = wc->opcode;
1354 tmp.vendor_err = wc->vendor_err;
1355 tmp.byte_len = wc->byte_len;
1356 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1357 tmp.qp_num = wc->qp->qp_num;
1358 tmp.src_qp = wc->src_qp;
1359 tmp.wc_flags = wc->wc_flags;
1360 tmp.pkey_index = wc->pkey_index;
1361 tmp.slid = wc->slid;
1362 tmp.sl = wc->sl;
1363 tmp.dlid_path_bits = wc->dlid_path_bits;
1364 tmp.port_num = wc->port_num;
1365 tmp.reserved = 0;
1367 if (copy_to_user(dest, &tmp, sizeof tmp))
1368 return -EFAULT;
1370 return 0;
1373 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1374 const char __user *buf, int in_len,
1375 int out_len)
1377 struct ib_uverbs_poll_cq cmd;
1378 struct ib_uverbs_poll_cq_resp resp;
1379 u8 __user *header_ptr;
1380 u8 __user *data_ptr;
1381 struct ib_cq *cq;
1382 struct ib_wc wc;
1383 int ret;
1385 if (copy_from_user(&cmd, buf, sizeof cmd))
1386 return -EFAULT;
1388 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1389 if (!cq)
1390 return -EINVAL;
1392 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1393 header_ptr = (void __user *)(unsigned long) cmd.response;
1394 data_ptr = header_ptr + sizeof resp;
1396 memset(&resp, 0, sizeof resp);
1397 while (resp.count < cmd.ne) {
1398 ret = ib_poll_cq(cq, 1, &wc);
1399 if (ret < 0)
1400 goto out_put;
1401 if (!ret)
1402 break;
1404 ret = copy_wc_to_user(data_ptr, &wc);
1405 if (ret)
1406 goto out_put;
1408 data_ptr += sizeof(struct ib_uverbs_wc);
1409 ++resp.count;
1412 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1413 ret = -EFAULT;
1414 goto out_put;
1417 ret = in_len;
1419 out_put:
1420 put_cq_read(cq);
1421 return ret;
1424 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1425 const char __user *buf, int in_len,
1426 int out_len)
1428 struct ib_uverbs_req_notify_cq cmd;
1429 struct ib_cq *cq;
1431 if (copy_from_user(&cmd, buf, sizeof cmd))
1432 return -EFAULT;
1434 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1435 if (!cq)
1436 return -EINVAL;
1438 ib_req_notify_cq(cq, cmd.solicited_only ?
1439 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1441 put_cq_read(cq);
1443 return in_len;
1446 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1447 const char __user *buf, int in_len,
1448 int out_len)
1450 struct ib_uverbs_destroy_cq cmd;
1451 struct ib_uverbs_destroy_cq_resp resp;
1452 struct ib_uobject *uobj;
1453 struct ib_cq *cq;
1454 struct ib_ucq_object *obj;
1455 struct ib_uverbs_event_file *ev_file;
1456 int ret = -EINVAL;
1458 if (copy_from_user(&cmd, buf, sizeof cmd))
1459 return -EFAULT;
1461 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1462 if (!uobj)
1463 return -EINVAL;
1464 cq = uobj->object;
1465 ev_file = cq->cq_context;
1466 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
1468 ret = ib_destroy_cq(cq);
1469 if (!ret)
1470 uobj->live = 0;
1472 put_uobj_write(uobj);
1474 if (ret)
1475 return ret;
1477 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1479 mutex_lock(&file->mutex);
1480 list_del(&uobj->list);
1481 mutex_unlock(&file->mutex);
1483 ib_uverbs_release_ucq(file, ev_file, obj);
1485 memset(&resp, 0, sizeof resp);
1486 resp.comp_events_reported = obj->comp_events_reported;
1487 resp.async_events_reported = obj->async_events_reported;
1489 put_uobj(uobj);
1491 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1492 &resp, sizeof resp))
1493 return -EFAULT;
1495 return in_len;
1498 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1499 const char __user *buf, int in_len,
1500 int out_len)
1502 struct ib_uverbs_create_qp cmd;
1503 struct ib_uverbs_create_qp_resp resp;
1504 struct ib_udata udata;
1505 struct ib_uqp_object *obj;
1506 struct ib_device *device;
1507 struct ib_pd *pd = NULL;
1508 struct ib_xrcd *xrcd = NULL;
1509 struct ib_uobject *uninitialized_var(xrcd_uobj);
1510 struct ib_cq *scq = NULL, *rcq = NULL;
1511 struct ib_srq *srq = NULL;
1512 struct ib_qp *qp;
1513 struct ib_qp_init_attr attr;
1514 int ret;
1516 if (out_len < sizeof resp)
1517 return -ENOSPC;
1519 if (copy_from_user(&cmd, buf, sizeof cmd))
1520 return -EFAULT;
1522 if (cmd.qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1523 return -EPERM;
1525 INIT_UDATA(&udata, buf + sizeof cmd,
1526 (unsigned long) cmd.response + sizeof resp,
1527 in_len - sizeof cmd, out_len - sizeof resp);
1529 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1530 if (!obj)
1531 return -ENOMEM;
1533 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1534 down_write(&obj->uevent.uobject.mutex);
1536 if (cmd.qp_type == IB_QPT_XRC_TGT) {
1537 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1538 if (!xrcd) {
1539 ret = -EINVAL;
1540 goto err_put;
1542 device = xrcd->device;
1543 } else {
1544 if (cmd.qp_type == IB_QPT_XRC_INI) {
1545 cmd.max_recv_wr = cmd.max_recv_sge = 0;
1546 } else {
1547 if (cmd.is_srq) {
1548 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1549 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1550 ret = -EINVAL;
1551 goto err_put;
1555 if (cmd.recv_cq_handle != cmd.send_cq_handle) {
1556 rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext, 0);
1557 if (!rcq) {
1558 ret = -EINVAL;
1559 goto err_put;
1564 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, !!rcq);
1565 rcq = rcq ?: scq;
1566 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1567 if (!pd || !scq) {
1568 ret = -EINVAL;
1569 goto err_put;
1572 device = pd->device;
1575 attr.event_handler = ib_uverbs_qp_event_handler;
1576 attr.qp_context = file;
1577 attr.send_cq = scq;
1578 attr.recv_cq = rcq;
1579 attr.srq = srq;
1580 attr.xrcd = xrcd;
1581 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1582 attr.qp_type = cmd.qp_type;
1583 attr.create_flags = 0;
1585 attr.cap.max_send_wr = cmd.max_send_wr;
1586 attr.cap.max_recv_wr = cmd.max_recv_wr;
1587 attr.cap.max_send_sge = cmd.max_send_sge;
1588 attr.cap.max_recv_sge = cmd.max_recv_sge;
1589 attr.cap.max_inline_data = cmd.max_inline_data;
1591 obj->uevent.events_reported = 0;
1592 INIT_LIST_HEAD(&obj->uevent.event_list);
1593 INIT_LIST_HEAD(&obj->mcast_list);
1595 if (cmd.qp_type == IB_QPT_XRC_TGT)
1596 qp = ib_create_qp(pd, &attr);
1597 else
1598 qp = device->create_qp(pd, &attr, &udata);
1600 if (IS_ERR(qp)) {
1601 ret = PTR_ERR(qp);
1602 goto err_put;
1605 if (cmd.qp_type != IB_QPT_XRC_TGT) {
1606 qp->real_qp = qp;
1607 qp->device = device;
1608 qp->pd = pd;
1609 qp->send_cq = attr.send_cq;
1610 qp->recv_cq = attr.recv_cq;
1611 qp->srq = attr.srq;
1612 qp->event_handler = attr.event_handler;
1613 qp->qp_context = attr.qp_context;
1614 qp->qp_type = attr.qp_type;
1615 atomic_set(&qp->usecnt, 0);
1616 atomic_inc(&pd->usecnt);
1617 atomic_inc(&attr.send_cq->usecnt);
1618 if (attr.recv_cq)
1619 atomic_inc(&attr.recv_cq->usecnt);
1620 if (attr.srq)
1621 atomic_inc(&attr.srq->usecnt);
1623 qp->uobject = &obj->uevent.uobject;
1625 obj->uevent.uobject.object = qp;
1626 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1627 if (ret)
1628 goto err_destroy;
1630 memset(&resp, 0, sizeof resp);
1631 resp.qpn = qp->qp_num;
1632 resp.qp_handle = obj->uevent.uobject.id;
1633 resp.max_recv_sge = attr.cap.max_recv_sge;
1634 resp.max_send_sge = attr.cap.max_send_sge;
1635 resp.max_recv_wr = attr.cap.max_recv_wr;
1636 resp.max_send_wr = attr.cap.max_send_wr;
1637 resp.max_inline_data = attr.cap.max_inline_data;
1639 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1640 &resp, sizeof resp)) {
1641 ret = -EFAULT;
1642 goto err_copy;
1645 if (xrcd)
1646 put_xrcd_read(xrcd_uobj);
1647 if (pd)
1648 put_pd_read(pd);
1649 if (scq)
1650 put_cq_read(scq);
1651 if (rcq && rcq != scq)
1652 put_cq_read(rcq);
1653 if (srq)
1654 put_srq_read(srq);
1656 mutex_lock(&file->mutex);
1657 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1658 mutex_unlock(&file->mutex);
1660 obj->uevent.uobject.live = 1;
1662 up_write(&obj->uevent.uobject.mutex);
1664 return in_len;
1666 err_copy:
1667 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1669 err_destroy:
1670 ib_destroy_qp(qp);
1672 err_put:
1673 if (xrcd)
1674 put_xrcd_read(xrcd_uobj);
1675 if (pd)
1676 put_pd_read(pd);
1677 if (scq)
1678 put_cq_read(scq);
1679 if (rcq && rcq != scq)
1680 put_cq_read(rcq);
1681 if (srq)
1682 put_srq_read(srq);
1684 put_uobj_write(&obj->uevent.uobject);
1685 return ret;
1688 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1689 const char __user *buf, int in_len, int out_len)
1691 struct ib_uverbs_open_qp cmd;
1692 struct ib_uverbs_create_qp_resp resp;
1693 struct ib_udata udata;
1694 struct ib_uqp_object *obj;
1695 struct ib_xrcd *xrcd;
1696 struct ib_uobject *uninitialized_var(xrcd_uobj);
1697 struct ib_qp *qp;
1698 struct ib_qp_open_attr attr;
1699 int ret;
1701 if (out_len < sizeof resp)
1702 return -ENOSPC;
1704 if (copy_from_user(&cmd, buf, sizeof cmd))
1705 return -EFAULT;
1707 INIT_UDATA(&udata, buf + sizeof cmd,
1708 (unsigned long) cmd.response + sizeof resp,
1709 in_len - sizeof cmd, out_len - sizeof resp);
1711 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1712 if (!obj)
1713 return -ENOMEM;
1715 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1716 down_write(&obj->uevent.uobject.mutex);
1718 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1719 if (!xrcd) {
1720 ret = -EINVAL;
1721 goto err_put;
1724 attr.event_handler = ib_uverbs_qp_event_handler;
1725 attr.qp_context = file;
1726 attr.qp_num = cmd.qpn;
1727 attr.qp_type = cmd.qp_type;
1729 obj->uevent.events_reported = 0;
1730 INIT_LIST_HEAD(&obj->uevent.event_list);
1731 INIT_LIST_HEAD(&obj->mcast_list);
1733 qp = ib_open_qp(xrcd, &attr);
1734 if (IS_ERR(qp)) {
1735 ret = PTR_ERR(qp);
1736 goto err_put;
1739 qp->uobject = &obj->uevent.uobject;
1741 obj->uevent.uobject.object = qp;
1742 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1743 if (ret)
1744 goto err_destroy;
1746 memset(&resp, 0, sizeof resp);
1747 resp.qpn = qp->qp_num;
1748 resp.qp_handle = obj->uevent.uobject.id;
1750 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1751 &resp, sizeof resp)) {
1752 ret = -EFAULT;
1753 goto err_remove;
1756 put_xrcd_read(xrcd_uobj);
1758 mutex_lock(&file->mutex);
1759 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1760 mutex_unlock(&file->mutex);
1762 obj->uevent.uobject.live = 1;
1764 up_write(&obj->uevent.uobject.mutex);
1766 return in_len;
1768 err_remove:
1769 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1771 err_destroy:
1772 ib_destroy_qp(qp);
1774 err_put:
1775 put_xrcd_read(xrcd_uobj);
1776 put_uobj_write(&obj->uevent.uobject);
1777 return ret;
1780 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1781 const char __user *buf, int in_len,
1782 int out_len)
1784 struct ib_uverbs_query_qp cmd;
1785 struct ib_uverbs_query_qp_resp resp;
1786 struct ib_qp *qp;
1787 struct ib_qp_attr *attr;
1788 struct ib_qp_init_attr *init_attr;
1789 int ret;
1791 if (copy_from_user(&cmd, buf, sizeof cmd))
1792 return -EFAULT;
1794 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1795 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1796 if (!attr || !init_attr) {
1797 ret = -ENOMEM;
1798 goto out;
1801 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1802 if (!qp) {
1803 ret = -EINVAL;
1804 goto out;
1807 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1809 put_qp_read(qp);
1811 if (ret)
1812 goto out;
1814 memset(&resp, 0, sizeof resp);
1816 resp.qp_state = attr->qp_state;
1817 resp.cur_qp_state = attr->cur_qp_state;
1818 resp.path_mtu = attr->path_mtu;
1819 resp.path_mig_state = attr->path_mig_state;
1820 resp.qkey = attr->qkey;
1821 resp.rq_psn = attr->rq_psn;
1822 resp.sq_psn = attr->sq_psn;
1823 resp.dest_qp_num = attr->dest_qp_num;
1824 resp.qp_access_flags = attr->qp_access_flags;
1825 resp.pkey_index = attr->pkey_index;
1826 resp.alt_pkey_index = attr->alt_pkey_index;
1827 resp.sq_draining = attr->sq_draining;
1828 resp.max_rd_atomic = attr->max_rd_atomic;
1829 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1830 resp.min_rnr_timer = attr->min_rnr_timer;
1831 resp.port_num = attr->port_num;
1832 resp.timeout = attr->timeout;
1833 resp.retry_cnt = attr->retry_cnt;
1834 resp.rnr_retry = attr->rnr_retry;
1835 resp.alt_port_num = attr->alt_port_num;
1836 resp.alt_timeout = attr->alt_timeout;
1838 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1839 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1840 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1841 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1842 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1843 resp.dest.dlid = attr->ah_attr.dlid;
1844 resp.dest.sl = attr->ah_attr.sl;
1845 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1846 resp.dest.static_rate = attr->ah_attr.static_rate;
1847 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1848 resp.dest.port_num = attr->ah_attr.port_num;
1850 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1851 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1852 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1853 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1854 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1855 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1856 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1857 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1858 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1859 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1860 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1862 resp.max_send_wr = init_attr->cap.max_send_wr;
1863 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1864 resp.max_send_sge = init_attr->cap.max_send_sge;
1865 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1866 resp.max_inline_data = init_attr->cap.max_inline_data;
1867 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1869 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1870 &resp, sizeof resp))
1871 ret = -EFAULT;
1873 out:
1874 kfree(attr);
1875 kfree(init_attr);
1877 return ret ? ret : in_len;
1880 /* Remove ignored fields set in the attribute mask */
1881 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1883 switch (qp_type) {
1884 case IB_QPT_XRC_INI:
1885 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1886 case IB_QPT_XRC_TGT:
1887 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1888 IB_QP_RNR_RETRY);
1889 default:
1890 return mask;
1894 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1895 const char __user *buf, int in_len,
1896 int out_len)
1898 struct ib_uverbs_modify_qp cmd;
1899 struct ib_udata udata;
1900 struct ib_qp *qp;
1901 struct ib_qp_attr *attr;
1902 int ret;
1904 if (copy_from_user(&cmd, buf, sizeof cmd))
1905 return -EFAULT;
1907 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1908 out_len);
1910 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1911 if (!attr)
1912 return -ENOMEM;
1914 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1915 if (!qp) {
1916 ret = -EINVAL;
1917 goto out;
1920 attr->qp_state = cmd.qp_state;
1921 attr->cur_qp_state = cmd.cur_qp_state;
1922 attr->path_mtu = cmd.path_mtu;
1923 attr->path_mig_state = cmd.path_mig_state;
1924 attr->qkey = cmd.qkey;
1925 attr->rq_psn = cmd.rq_psn;
1926 attr->sq_psn = cmd.sq_psn;
1927 attr->dest_qp_num = cmd.dest_qp_num;
1928 attr->qp_access_flags = cmd.qp_access_flags;
1929 attr->pkey_index = cmd.pkey_index;
1930 attr->alt_pkey_index = cmd.alt_pkey_index;
1931 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1932 attr->max_rd_atomic = cmd.max_rd_atomic;
1933 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1934 attr->min_rnr_timer = cmd.min_rnr_timer;
1935 attr->port_num = cmd.port_num;
1936 attr->timeout = cmd.timeout;
1937 attr->retry_cnt = cmd.retry_cnt;
1938 attr->rnr_retry = cmd.rnr_retry;
1939 attr->alt_port_num = cmd.alt_port_num;
1940 attr->alt_timeout = cmd.alt_timeout;
1942 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1943 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1944 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1945 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1946 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1947 attr->ah_attr.dlid = cmd.dest.dlid;
1948 attr->ah_attr.sl = cmd.dest.sl;
1949 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1950 attr->ah_attr.static_rate = cmd.dest.static_rate;
1951 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1952 attr->ah_attr.port_num = cmd.dest.port_num;
1954 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1955 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1956 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1957 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1958 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1959 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1960 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1961 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1962 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1963 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1964 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1966 if (qp->real_qp == qp) {
1967 ret = qp->device->modify_qp(qp, attr,
1968 modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
1969 } else {
1970 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
1973 put_qp_read(qp);
1975 if (ret)
1976 goto out;
1978 ret = in_len;
1980 out:
1981 kfree(attr);
1983 return ret;
1986 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1987 const char __user *buf, int in_len,
1988 int out_len)
1990 struct ib_uverbs_destroy_qp cmd;
1991 struct ib_uverbs_destroy_qp_resp resp;
1992 struct ib_uobject *uobj;
1993 struct ib_qp *qp;
1994 struct ib_uqp_object *obj;
1995 int ret = -EINVAL;
1997 if (copy_from_user(&cmd, buf, sizeof cmd))
1998 return -EFAULT;
2000 memset(&resp, 0, sizeof resp);
2002 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
2003 if (!uobj)
2004 return -EINVAL;
2005 qp = uobj->object;
2006 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2008 if (!list_empty(&obj->mcast_list)) {
2009 put_uobj_write(uobj);
2010 return -EBUSY;
2013 ret = ib_destroy_qp(qp);
2014 if (!ret)
2015 uobj->live = 0;
2017 put_uobj_write(uobj);
2019 if (ret)
2020 return ret;
2022 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
2024 mutex_lock(&file->mutex);
2025 list_del(&uobj->list);
2026 mutex_unlock(&file->mutex);
2028 ib_uverbs_release_uevent(file, &obj->uevent);
2030 resp.events_reported = obj->uevent.events_reported;
2032 put_uobj(uobj);
2034 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2035 &resp, sizeof resp))
2036 return -EFAULT;
2038 return in_len;
2041 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
2042 const char __user *buf, int in_len,
2043 int out_len)
2045 struct ib_uverbs_post_send cmd;
2046 struct ib_uverbs_post_send_resp resp;
2047 struct ib_uverbs_send_wr *user_wr;
2048 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2049 struct ib_qp *qp;
2050 int i, sg_ind;
2051 int is_ud;
2052 ssize_t ret = -EINVAL;
2054 if (copy_from_user(&cmd, buf, sizeof cmd))
2055 return -EFAULT;
2057 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2058 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2059 return -EINVAL;
2061 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2062 return -EINVAL;
2064 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2065 if (!user_wr)
2066 return -ENOMEM;
2068 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2069 if (!qp)
2070 goto out;
2072 is_ud = qp->qp_type == IB_QPT_UD;
2073 sg_ind = 0;
2074 last = NULL;
2075 for (i = 0; i < cmd.wr_count; ++i) {
2076 if (copy_from_user(user_wr,
2077 buf + sizeof cmd + i * cmd.wqe_size,
2078 cmd.wqe_size)) {
2079 ret = -EFAULT;
2080 goto out_put;
2083 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2084 ret = -EINVAL;
2085 goto out_put;
2088 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2089 user_wr->num_sge * sizeof (struct ib_sge),
2090 GFP_KERNEL);
2091 if (!next) {
2092 ret = -ENOMEM;
2093 goto out_put;
2096 if (!last)
2097 wr = next;
2098 else
2099 last->next = next;
2100 last = next;
2102 next->next = NULL;
2103 next->wr_id = user_wr->wr_id;
2104 next->num_sge = user_wr->num_sge;
2105 next->opcode = user_wr->opcode;
2106 next->send_flags = user_wr->send_flags;
2108 if (is_ud) {
2109 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
2110 file->ucontext);
2111 if (!next->wr.ud.ah) {
2112 ret = -EINVAL;
2113 goto out_put;
2115 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
2116 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
2117 } else {
2118 switch (next->opcode) {
2119 case IB_WR_RDMA_WRITE_WITH_IMM:
2120 next->ex.imm_data =
2121 (__be32 __force) user_wr->ex.imm_data;
2122 case IB_WR_RDMA_WRITE:
2123 case IB_WR_RDMA_READ:
2124 next->wr.rdma.remote_addr =
2125 user_wr->wr.rdma.remote_addr;
2126 next->wr.rdma.rkey =
2127 user_wr->wr.rdma.rkey;
2128 break;
2129 case IB_WR_SEND_WITH_IMM:
2130 next->ex.imm_data =
2131 (__be32 __force) user_wr->ex.imm_data;
2132 break;
2133 case IB_WR_SEND_WITH_INV:
2134 next->ex.invalidate_rkey =
2135 user_wr->ex.invalidate_rkey;
2136 break;
2137 case IB_WR_ATOMIC_CMP_AND_SWP:
2138 case IB_WR_ATOMIC_FETCH_AND_ADD:
2139 next->wr.atomic.remote_addr =
2140 user_wr->wr.atomic.remote_addr;
2141 next->wr.atomic.compare_add =
2142 user_wr->wr.atomic.compare_add;
2143 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2144 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2145 break;
2146 default:
2147 break;
2151 if (next->num_sge) {
2152 next->sg_list = (void *) next +
2153 ALIGN(sizeof *next, sizeof (struct ib_sge));
2154 if (copy_from_user(next->sg_list,
2155 buf + sizeof cmd +
2156 cmd.wr_count * cmd.wqe_size +
2157 sg_ind * sizeof (struct ib_sge),
2158 next->num_sge * sizeof (struct ib_sge))) {
2159 ret = -EFAULT;
2160 goto out_put;
2162 sg_ind += next->num_sge;
2163 } else
2164 next->sg_list = NULL;
2167 resp.bad_wr = 0;
2168 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2169 if (ret)
2170 for (next = wr; next; next = next->next) {
2171 ++resp.bad_wr;
2172 if (next == bad_wr)
2173 break;
2176 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2177 &resp, sizeof resp))
2178 ret = -EFAULT;
2180 out_put:
2181 put_qp_read(qp);
2183 while (wr) {
2184 if (is_ud && wr->wr.ud.ah)
2185 put_ah_read(wr->wr.ud.ah);
2186 next = wr->next;
2187 kfree(wr);
2188 wr = next;
2191 out:
2192 kfree(user_wr);
2194 return ret ? ret : in_len;
2197 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2198 int in_len,
2199 u32 wr_count,
2200 u32 sge_count,
2201 u32 wqe_size)
2203 struct ib_uverbs_recv_wr *user_wr;
2204 struct ib_recv_wr *wr = NULL, *last, *next;
2205 int sg_ind;
2206 int i;
2207 int ret;
2209 if (in_len < wqe_size * wr_count +
2210 sge_count * sizeof (struct ib_uverbs_sge))
2211 return ERR_PTR(-EINVAL);
2213 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2214 return ERR_PTR(-EINVAL);
2216 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2217 if (!user_wr)
2218 return ERR_PTR(-ENOMEM);
2220 sg_ind = 0;
2221 last = NULL;
2222 for (i = 0; i < wr_count; ++i) {
2223 if (copy_from_user(user_wr, buf + i * wqe_size,
2224 wqe_size)) {
2225 ret = -EFAULT;
2226 goto err;
2229 if (user_wr->num_sge + sg_ind > sge_count) {
2230 ret = -EINVAL;
2231 goto err;
2234 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2235 user_wr->num_sge * sizeof (struct ib_sge),
2236 GFP_KERNEL);
2237 if (!next) {
2238 ret = -ENOMEM;
2239 goto err;
2242 if (!last)
2243 wr = next;
2244 else
2245 last->next = next;
2246 last = next;
2248 next->next = NULL;
2249 next->wr_id = user_wr->wr_id;
2250 next->num_sge = user_wr->num_sge;
2252 if (next->num_sge) {
2253 next->sg_list = (void *) next +
2254 ALIGN(sizeof *next, sizeof (struct ib_sge));
2255 if (copy_from_user(next->sg_list,
2256 buf + wr_count * wqe_size +
2257 sg_ind * sizeof (struct ib_sge),
2258 next->num_sge * sizeof (struct ib_sge))) {
2259 ret = -EFAULT;
2260 goto err;
2262 sg_ind += next->num_sge;
2263 } else
2264 next->sg_list = NULL;
2267 kfree(user_wr);
2268 return wr;
2270 err:
2271 kfree(user_wr);
2273 while (wr) {
2274 next = wr->next;
2275 kfree(wr);
2276 wr = next;
2279 return ERR_PTR(ret);
2282 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2283 const char __user *buf, int in_len,
2284 int out_len)
2286 struct ib_uverbs_post_recv cmd;
2287 struct ib_uverbs_post_recv_resp resp;
2288 struct ib_recv_wr *wr, *next, *bad_wr;
2289 struct ib_qp *qp;
2290 ssize_t ret = -EINVAL;
2292 if (copy_from_user(&cmd, buf, sizeof cmd))
2293 return -EFAULT;
2295 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2296 in_len - sizeof cmd, cmd.wr_count,
2297 cmd.sge_count, cmd.wqe_size);
2298 if (IS_ERR(wr))
2299 return PTR_ERR(wr);
2301 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2302 if (!qp)
2303 goto out;
2305 resp.bad_wr = 0;
2306 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2308 put_qp_read(qp);
2310 if (ret)
2311 for (next = wr; next; next = next->next) {
2312 ++resp.bad_wr;
2313 if (next == bad_wr)
2314 break;
2317 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2318 &resp, sizeof resp))
2319 ret = -EFAULT;
2321 out:
2322 while (wr) {
2323 next = wr->next;
2324 kfree(wr);
2325 wr = next;
2328 return ret ? ret : in_len;
2331 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2332 const char __user *buf, int in_len,
2333 int out_len)
2335 struct ib_uverbs_post_srq_recv cmd;
2336 struct ib_uverbs_post_srq_recv_resp resp;
2337 struct ib_recv_wr *wr, *next, *bad_wr;
2338 struct ib_srq *srq;
2339 ssize_t ret = -EINVAL;
2341 if (copy_from_user(&cmd, buf, sizeof cmd))
2342 return -EFAULT;
2344 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2345 in_len - sizeof cmd, cmd.wr_count,
2346 cmd.sge_count, cmd.wqe_size);
2347 if (IS_ERR(wr))
2348 return PTR_ERR(wr);
2350 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2351 if (!srq)
2352 goto out;
2354 resp.bad_wr = 0;
2355 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2357 put_srq_read(srq);
2359 if (ret)
2360 for (next = wr; next; next = next->next) {
2361 ++resp.bad_wr;
2362 if (next == bad_wr)
2363 break;
2366 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2367 &resp, sizeof resp))
2368 ret = -EFAULT;
2370 out:
2371 while (wr) {
2372 next = wr->next;
2373 kfree(wr);
2374 wr = next;
2377 return ret ? ret : in_len;
2380 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2381 const char __user *buf, int in_len,
2382 int out_len)
2384 struct ib_uverbs_create_ah cmd;
2385 struct ib_uverbs_create_ah_resp resp;
2386 struct ib_uobject *uobj;
2387 struct ib_pd *pd;
2388 struct ib_ah *ah;
2389 struct ib_ah_attr attr;
2390 int ret;
2392 if (out_len < sizeof resp)
2393 return -ENOSPC;
2395 if (copy_from_user(&cmd, buf, sizeof cmd))
2396 return -EFAULT;
2398 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2399 if (!uobj)
2400 return -ENOMEM;
2402 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2403 down_write(&uobj->mutex);
2405 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2406 if (!pd) {
2407 ret = -EINVAL;
2408 goto err;
2411 attr.dlid = cmd.attr.dlid;
2412 attr.sl = cmd.attr.sl;
2413 attr.src_path_bits = cmd.attr.src_path_bits;
2414 attr.static_rate = cmd.attr.static_rate;
2415 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
2416 attr.port_num = cmd.attr.port_num;
2417 attr.grh.flow_label = cmd.attr.grh.flow_label;
2418 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2419 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2420 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2421 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2423 ah = ib_create_ah(pd, &attr);
2424 if (IS_ERR(ah)) {
2425 ret = PTR_ERR(ah);
2426 goto err_put;
2429 ah->uobject = uobj;
2430 uobj->object = ah;
2432 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2433 if (ret)
2434 goto err_destroy;
2436 resp.ah_handle = uobj->id;
2438 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2439 &resp, sizeof resp)) {
2440 ret = -EFAULT;
2441 goto err_copy;
2444 put_pd_read(pd);
2446 mutex_lock(&file->mutex);
2447 list_add_tail(&uobj->list, &file->ucontext->ah_list);
2448 mutex_unlock(&file->mutex);
2450 uobj->live = 1;
2452 up_write(&uobj->mutex);
2454 return in_len;
2456 err_copy:
2457 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2459 err_destroy:
2460 ib_destroy_ah(ah);
2462 err_put:
2463 put_pd_read(pd);
2465 err:
2466 put_uobj_write(uobj);
2467 return ret;
2470 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2471 const char __user *buf, int in_len, int out_len)
2473 struct ib_uverbs_destroy_ah cmd;
2474 struct ib_ah *ah;
2475 struct ib_uobject *uobj;
2476 int ret;
2478 if (copy_from_user(&cmd, buf, sizeof cmd))
2479 return -EFAULT;
2481 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2482 if (!uobj)
2483 return -EINVAL;
2484 ah = uobj->object;
2486 ret = ib_destroy_ah(ah);
2487 if (!ret)
2488 uobj->live = 0;
2490 put_uobj_write(uobj);
2492 if (ret)
2493 return ret;
2495 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2497 mutex_lock(&file->mutex);
2498 list_del(&uobj->list);
2499 mutex_unlock(&file->mutex);
2501 put_uobj(uobj);
2503 return in_len;
2506 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2507 const char __user *buf, int in_len,
2508 int out_len)
2510 struct ib_uverbs_attach_mcast cmd;
2511 struct ib_qp *qp;
2512 struct ib_uqp_object *obj;
2513 struct ib_uverbs_mcast_entry *mcast;
2514 int ret;
2516 if (copy_from_user(&cmd, buf, sizeof cmd))
2517 return -EFAULT;
2519 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2520 if (!qp)
2521 return -EINVAL;
2523 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2525 list_for_each_entry(mcast, &obj->mcast_list, list)
2526 if (cmd.mlid == mcast->lid &&
2527 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2528 ret = 0;
2529 goto out_put;
2532 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2533 if (!mcast) {
2534 ret = -ENOMEM;
2535 goto out_put;
2538 mcast->lid = cmd.mlid;
2539 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2541 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2542 if (!ret)
2543 list_add_tail(&mcast->list, &obj->mcast_list);
2544 else
2545 kfree(mcast);
2547 out_put:
2548 put_qp_write(qp);
2550 return ret ? ret : in_len;
2553 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2554 const char __user *buf, int in_len,
2555 int out_len)
2557 struct ib_uverbs_detach_mcast cmd;
2558 struct ib_uqp_object *obj;
2559 struct ib_qp *qp;
2560 struct ib_uverbs_mcast_entry *mcast;
2561 int ret = -EINVAL;
2563 if (copy_from_user(&cmd, buf, sizeof cmd))
2564 return -EFAULT;
2566 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2567 if (!qp)
2568 return -EINVAL;
2570 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2571 if (ret)
2572 goto out_put;
2574 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2576 list_for_each_entry(mcast, &obj->mcast_list, list)
2577 if (cmd.mlid == mcast->lid &&
2578 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2579 list_del(&mcast->list);
2580 kfree(mcast);
2581 break;
2584 out_put:
2585 put_qp_write(qp);
2587 return ret ? ret : in_len;
2590 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2591 struct ib_uverbs_create_xsrq *cmd,
2592 struct ib_udata *udata)
2594 struct ib_uverbs_create_srq_resp resp;
2595 struct ib_usrq_object *obj;
2596 struct ib_pd *pd;
2597 struct ib_srq *srq;
2598 struct ib_uobject *uninitialized_var(xrcd_uobj);
2599 struct ib_srq_init_attr attr;
2600 int ret;
2602 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2603 if (!obj)
2604 return -ENOMEM;
2606 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2607 down_write(&obj->uevent.uobject.mutex);
2609 if (cmd->srq_type == IB_SRQT_XRC) {
2610 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2611 if (!attr.ext.xrc.xrcd) {
2612 ret = -EINVAL;
2613 goto err;
2616 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2617 atomic_inc(&obj->uxrcd->refcnt);
2619 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2620 if (!attr.ext.xrc.cq) {
2621 ret = -EINVAL;
2622 goto err_put_xrcd;
2626 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
2627 if (!pd) {
2628 ret = -EINVAL;
2629 goto err_put_cq;
2632 attr.event_handler = ib_uverbs_srq_event_handler;
2633 attr.srq_context = file;
2634 attr.srq_type = cmd->srq_type;
2635 attr.attr.max_wr = cmd->max_wr;
2636 attr.attr.max_sge = cmd->max_sge;
2637 attr.attr.srq_limit = cmd->srq_limit;
2639 obj->uevent.events_reported = 0;
2640 INIT_LIST_HEAD(&obj->uevent.event_list);
2642 srq = pd->device->create_srq(pd, &attr, udata);
2643 if (IS_ERR(srq)) {
2644 ret = PTR_ERR(srq);
2645 goto err_put;
2648 srq->device = pd->device;
2649 srq->pd = pd;
2650 srq->srq_type = cmd->srq_type;
2651 srq->uobject = &obj->uevent.uobject;
2652 srq->event_handler = attr.event_handler;
2653 srq->srq_context = attr.srq_context;
2655 if (cmd->srq_type == IB_SRQT_XRC) {
2656 srq->ext.xrc.cq = attr.ext.xrc.cq;
2657 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2658 atomic_inc(&attr.ext.xrc.cq->usecnt);
2659 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2662 atomic_inc(&pd->usecnt);
2663 atomic_set(&srq->usecnt, 0);
2665 obj->uevent.uobject.object = srq;
2666 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2667 if (ret)
2668 goto err_destroy;
2670 memset(&resp, 0, sizeof resp);
2671 resp.srq_handle = obj->uevent.uobject.id;
2672 resp.max_wr = attr.attr.max_wr;
2673 resp.max_sge = attr.attr.max_sge;
2674 if (cmd->srq_type == IB_SRQT_XRC)
2675 resp.srqn = srq->ext.xrc.srq_num;
2677 if (copy_to_user((void __user *) (unsigned long) cmd->response,
2678 &resp, sizeof resp)) {
2679 ret = -EFAULT;
2680 goto err_copy;
2683 if (cmd->srq_type == IB_SRQT_XRC) {
2684 put_uobj_read(xrcd_uobj);
2685 put_cq_read(attr.ext.xrc.cq);
2687 put_pd_read(pd);
2689 mutex_lock(&file->mutex);
2690 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2691 mutex_unlock(&file->mutex);
2693 obj->uevent.uobject.live = 1;
2695 up_write(&obj->uevent.uobject.mutex);
2697 return 0;
2699 err_copy:
2700 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2702 err_destroy:
2703 ib_destroy_srq(srq);
2705 err_put:
2706 put_pd_read(pd);
2708 err_put_cq:
2709 if (cmd->srq_type == IB_SRQT_XRC)
2710 put_cq_read(attr.ext.xrc.cq);
2712 err_put_xrcd:
2713 if (cmd->srq_type == IB_SRQT_XRC) {
2714 atomic_dec(&obj->uxrcd->refcnt);
2715 put_uobj_read(xrcd_uobj);
2718 err:
2719 put_uobj_write(&obj->uevent.uobject);
2720 return ret;
2723 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2724 const char __user *buf, int in_len,
2725 int out_len)
2727 struct ib_uverbs_create_srq cmd;
2728 struct ib_uverbs_create_xsrq xcmd;
2729 struct ib_uverbs_create_srq_resp resp;
2730 struct ib_udata udata;
2731 int ret;
2733 if (out_len < sizeof resp)
2734 return -ENOSPC;
2736 if (copy_from_user(&cmd, buf, sizeof cmd))
2737 return -EFAULT;
2739 xcmd.response = cmd.response;
2740 xcmd.user_handle = cmd.user_handle;
2741 xcmd.srq_type = IB_SRQT_BASIC;
2742 xcmd.pd_handle = cmd.pd_handle;
2743 xcmd.max_wr = cmd.max_wr;
2744 xcmd.max_sge = cmd.max_sge;
2745 xcmd.srq_limit = cmd.srq_limit;
2747 INIT_UDATA(&udata, buf + sizeof cmd,
2748 (unsigned long) cmd.response + sizeof resp,
2749 in_len - sizeof cmd, out_len - sizeof resp);
2751 ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2752 if (ret)
2753 return ret;
2755 return in_len;
2758 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2759 const char __user *buf, int in_len, int out_len)
2761 struct ib_uverbs_create_xsrq cmd;
2762 struct ib_uverbs_create_srq_resp resp;
2763 struct ib_udata udata;
2764 int ret;
2766 if (out_len < sizeof resp)
2767 return -ENOSPC;
2769 if (copy_from_user(&cmd, buf, sizeof cmd))
2770 return -EFAULT;
2772 INIT_UDATA(&udata, buf + sizeof cmd,
2773 (unsigned long) cmd.response + sizeof resp,
2774 in_len - sizeof cmd, out_len - sizeof resp);
2776 ret = __uverbs_create_xsrq(file, &cmd, &udata);
2777 if (ret)
2778 return ret;
2780 return in_len;
2783 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2784 const char __user *buf, int in_len,
2785 int out_len)
2787 struct ib_uverbs_modify_srq cmd;
2788 struct ib_udata udata;
2789 struct ib_srq *srq;
2790 struct ib_srq_attr attr;
2791 int ret;
2793 if (copy_from_user(&cmd, buf, sizeof cmd))
2794 return -EFAULT;
2796 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2797 out_len);
2799 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2800 if (!srq)
2801 return -EINVAL;
2803 attr.max_wr = cmd.max_wr;
2804 attr.srq_limit = cmd.srq_limit;
2806 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2808 put_srq_read(srq);
2810 return ret ? ret : in_len;
2813 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2814 const char __user *buf,
2815 int in_len, int out_len)
2817 struct ib_uverbs_query_srq cmd;
2818 struct ib_uverbs_query_srq_resp resp;
2819 struct ib_srq_attr attr;
2820 struct ib_srq *srq;
2821 int ret;
2823 if (out_len < sizeof resp)
2824 return -ENOSPC;
2826 if (copy_from_user(&cmd, buf, sizeof cmd))
2827 return -EFAULT;
2829 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2830 if (!srq)
2831 return -EINVAL;
2833 ret = ib_query_srq(srq, &attr);
2835 put_srq_read(srq);
2837 if (ret)
2838 return ret;
2840 memset(&resp, 0, sizeof resp);
2842 resp.max_wr = attr.max_wr;
2843 resp.max_sge = attr.max_sge;
2844 resp.srq_limit = attr.srq_limit;
2846 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2847 &resp, sizeof resp))
2848 return -EFAULT;
2850 return in_len;
2853 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2854 const char __user *buf, int in_len,
2855 int out_len)
2857 struct ib_uverbs_destroy_srq cmd;
2858 struct ib_uverbs_destroy_srq_resp resp;
2859 struct ib_uobject *uobj;
2860 struct ib_srq *srq;
2861 struct ib_uevent_object *obj;
2862 int ret = -EINVAL;
2864 if (copy_from_user(&cmd, buf, sizeof cmd))
2865 return -EFAULT;
2867 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2868 if (!uobj)
2869 return -EINVAL;
2870 srq = uobj->object;
2871 obj = container_of(uobj, struct ib_uevent_object, uobject);
2873 ret = ib_destroy_srq(srq);
2874 if (!ret)
2875 uobj->live = 0;
2877 put_uobj_write(uobj);
2879 if (ret)
2880 return ret;
2882 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2884 mutex_lock(&file->mutex);
2885 list_del(&uobj->list);
2886 mutex_unlock(&file->mutex);
2888 ib_uverbs_release_uevent(file, obj);
2890 memset(&resp, 0, sizeof resp);
2891 resp.events_reported = obj->events_reported;
2893 put_uobj(uobj);
2895 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2896 &resp, sizeof resp))
2897 ret = -EFAULT;
2899 return ret ? ret : in_len;