]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/infiniband/core/uverbs_cmd.c
RDMA/core: Use READ_ONCE for ib_ufile.async_file
[linux.git] / drivers / infiniband / core / uverbs_cmd.c
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.
6  *
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:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
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.
25  *
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.
34  */
35
36 #include <linux/file.h>
37 #include <linux/fs.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40
41 #include <linux/uaccess.h>
42
43 #include <rdma/uverbs_types.h>
44 #include <rdma/uverbs_std_types.h>
45 #include "rdma_core.h"
46
47 #include "uverbs.h"
48 #include "core_priv.h"
49
50 /*
51  * Copy a response to userspace. If the provided 'resp' is larger than the
52  * user buffer it is silently truncated. If the user provided a larger buffer
53  * then the trailing portion is zero filled.
54  *
55  * These semantics are intended to support future extension of the output
56  * structures.
57  */
58 static int uverbs_response(struct uverbs_attr_bundle *attrs, const void *resp,
59                            size_t resp_len)
60 {
61         int ret;
62
63         if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
64                 return uverbs_copy_to_struct_or_zero(
65                         attrs, UVERBS_ATTR_CORE_OUT, resp, resp_len);
66
67         if (copy_to_user(attrs->ucore.outbuf, resp,
68                          min(attrs->ucore.outlen, resp_len)))
69                 return -EFAULT;
70
71         if (resp_len < attrs->ucore.outlen) {
72                 /*
73                  * Zero fill any extra memory that user
74                  * space might have provided.
75                  */
76                 ret = clear_user(attrs->ucore.outbuf + resp_len,
77                                  attrs->ucore.outlen - resp_len);
78                 if (ret)
79                         return -EFAULT;
80         }
81
82         return 0;
83 }
84
85 /*
86  * Copy a request from userspace. If the provided 'req' is larger than the
87  * user buffer then the user buffer is zero extended into the 'req'. If 'req'
88  * is smaller than the user buffer then the uncopied bytes in the user buffer
89  * must be zero.
90  */
91 static int uverbs_request(struct uverbs_attr_bundle *attrs, void *req,
92                           size_t req_len)
93 {
94         if (copy_from_user(req, attrs->ucore.inbuf,
95                            min(attrs->ucore.inlen, req_len)))
96                 return -EFAULT;
97
98         if (attrs->ucore.inlen < req_len) {
99                 memset(req + attrs->ucore.inlen, 0,
100                        req_len - attrs->ucore.inlen);
101         } else if (attrs->ucore.inlen > req_len) {
102                 if (!ib_is_buffer_cleared(attrs->ucore.inbuf + req_len,
103                                           attrs->ucore.inlen - req_len))
104                         return -EOPNOTSUPP;
105         }
106         return 0;
107 }
108
109 /*
110  * Generate the value for the 'response_length' protocol used by write_ex.
111  * This is the number of bytes the kernel actually wrote. Userspace can use
112  * this to detect what structure members in the response the kernel
113  * understood.
114  */
115 static u32 uverbs_response_length(struct uverbs_attr_bundle *attrs,
116                                   size_t resp_len)
117 {
118         return min_t(size_t, attrs->ucore.outlen, resp_len);
119 }
120
121 /*
122  * The iterator version of the request interface is for handlers that need to
123  * step over a flex array at the end of a command header.
124  */
125 struct uverbs_req_iter {
126         const void __user *cur;
127         const void __user *end;
128 };
129
130 static int uverbs_request_start(struct uverbs_attr_bundle *attrs,
131                                 struct uverbs_req_iter *iter,
132                                 void *req,
133                                 size_t req_len)
134 {
135         if (attrs->ucore.inlen < req_len)
136                 return -ENOSPC;
137
138         if (copy_from_user(req, attrs->ucore.inbuf, req_len))
139                 return -EFAULT;
140
141         iter->cur = attrs->ucore.inbuf + req_len;
142         iter->end = attrs->ucore.inbuf + attrs->ucore.inlen;
143         return 0;
144 }
145
146 static int uverbs_request_next(struct uverbs_req_iter *iter, void *val,
147                                size_t len)
148 {
149         if (iter->cur + len > iter->end)
150                 return -ENOSPC;
151
152         if (copy_from_user(val, iter->cur, len))
153                 return -EFAULT;
154
155         iter->cur += len;
156         return 0;
157 }
158
159 static const void __user *uverbs_request_next_ptr(struct uverbs_req_iter *iter,
160                                                   size_t len)
161 {
162         const void __user *res = iter->cur;
163
164         if (iter->cur + len > iter->end)
165                 return (void __force __user *)ERR_PTR(-ENOSPC);
166         iter->cur += len;
167         return res;
168 }
169
170 static int uverbs_request_finish(struct uverbs_req_iter *iter)
171 {
172         if (!ib_is_buffer_cleared(iter->cur, iter->end - iter->cur))
173                 return -EOPNOTSUPP;
174         return 0;
175 }
176
177 /*
178  * When calling a destroy function during an error unwind we need to pass in
179  * the udata that is sanitized of all user arguments. Ie from the driver
180  * perspective it looks like no udata was passed.
181  */
182 struct ib_udata *uverbs_get_cleared_udata(struct uverbs_attr_bundle *attrs)
183 {
184         attrs->driver_udata = (struct ib_udata){};
185         return &attrs->driver_udata;
186 }
187
188 static struct ib_uverbs_completion_event_file *
189 _ib_uverbs_lookup_comp_file(s32 fd, struct uverbs_attr_bundle *attrs)
190 {
191         struct ib_uobject *uobj = ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL,
192                                                fd, attrs);
193
194         if (IS_ERR(uobj))
195                 return (void *)uobj;
196
197         uverbs_uobject_get(uobj);
198         uobj_put_read(uobj);
199
200         return container_of(uobj, struct ib_uverbs_completion_event_file,
201                             uobj);
202 }
203 #define ib_uverbs_lookup_comp_file(_fd, _ufile)                                \
204         _ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
205
206 static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs)
207 {
208         struct ib_uverbs_file *file = attrs->ufile;
209         struct ib_uverbs_get_context      cmd;
210         struct ib_uverbs_get_context_resp resp;
211         struct ib_ucontext               *ucontext;
212         struct ib_rdmacg_object          cg_obj;
213         struct ib_device *ib_dev;
214         struct ib_uobject *uobj;
215         int ret;
216
217         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
218         if (ret)
219                 return ret;
220
221         mutex_lock(&file->ucontext_lock);
222         ib_dev = srcu_dereference(file->device->ib_dev,
223                                   &file->device->disassociate_srcu);
224         if (!ib_dev) {
225                 ret = -EIO;
226                 goto err;
227         }
228
229         if (file->ucontext) {
230                 ret = -EINVAL;
231                 goto err;
232         }
233
234         ret = ib_rdmacg_try_charge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
235         if (ret)
236                 goto err;
237
238         ucontext = rdma_zalloc_drv_obj(ib_dev, ib_ucontext);
239         if (!ucontext) {
240                 ret = -ENOMEM;
241                 goto err_alloc;
242         }
243
244         attrs->context = ucontext;
245
246         ucontext->res.type = RDMA_RESTRACK_CTX;
247         ucontext->device = ib_dev;
248         ucontext->cg_obj = cg_obj;
249         /* ufile is required when some objects are released */
250         ucontext->ufile = file;
251
252         ucontext->closing = false;
253         ucontext->cleanup_retryable = false;
254
255         xa_init_flags(&ucontext->mmap_xa, XA_FLAGS_ALLOC);
256
257         uobj = uobj_alloc(UVERBS_OBJECT_ASYNC_EVENT, attrs, &ib_dev);
258         if (IS_ERR(uobj)) {
259                 ret = PTR_ERR(uobj);
260                 goto err_free;
261         }
262
263         resp.async_fd = uobj->id;
264         resp.num_comp_vectors = file->device->num_comp_vectors;
265
266         ret = uverbs_response(attrs, &resp, sizeof(resp));
267         if (ret)
268                 goto err_uobj;
269
270         ret = ib_dev->ops.alloc_ucontext(ucontext, &attrs->driver_udata);
271         if (ret)
272                 goto err_uobj;
273
274         rdma_restrack_uadd(&ucontext->res);
275
276         ib_uverbs_init_async_event_file(
277                 container_of(uobj, struct ib_uverbs_async_event_file, uobj));
278         rdma_alloc_commit_uobject(uobj, attrs);
279
280         /*
281          * Make sure that ib_uverbs_get_ucontext() sees the pointer update
282          * only after all writes to setup the ucontext have completed
283          */
284         smp_store_release(&file->ucontext, ucontext);
285
286         mutex_unlock(&file->ucontext_lock);
287
288         return 0;
289
290 err_uobj:
291         rdma_alloc_abort_uobject(uobj, attrs);
292
293 err_free:
294         kfree(ucontext);
295
296 err_alloc:
297         ib_rdmacg_uncharge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
298
299 err:
300         mutex_unlock(&file->ucontext_lock);
301         return ret;
302 }
303
304 static void copy_query_dev_fields(struct ib_ucontext *ucontext,
305                                   struct ib_uverbs_query_device_resp *resp,
306                                   struct ib_device_attr *attr)
307 {
308         struct ib_device *ib_dev = ucontext->device;
309
310         resp->fw_ver            = attr->fw_ver;
311         resp->node_guid         = ib_dev->node_guid;
312         resp->sys_image_guid    = attr->sys_image_guid;
313         resp->max_mr_size       = attr->max_mr_size;
314         resp->page_size_cap     = attr->page_size_cap;
315         resp->vendor_id         = attr->vendor_id;
316         resp->vendor_part_id    = attr->vendor_part_id;
317         resp->hw_ver            = attr->hw_ver;
318         resp->max_qp            = attr->max_qp;
319         resp->max_qp_wr         = attr->max_qp_wr;
320         resp->device_cap_flags  = lower_32_bits(attr->device_cap_flags);
321         resp->max_sge           = min(attr->max_send_sge, attr->max_recv_sge);
322         resp->max_sge_rd        = attr->max_sge_rd;
323         resp->max_cq            = attr->max_cq;
324         resp->max_cqe           = attr->max_cqe;
325         resp->max_mr            = attr->max_mr;
326         resp->max_pd            = attr->max_pd;
327         resp->max_qp_rd_atom    = attr->max_qp_rd_atom;
328         resp->max_ee_rd_atom    = attr->max_ee_rd_atom;
329         resp->max_res_rd_atom   = attr->max_res_rd_atom;
330         resp->max_qp_init_rd_atom       = attr->max_qp_init_rd_atom;
331         resp->max_ee_init_rd_atom       = attr->max_ee_init_rd_atom;
332         resp->atomic_cap                = attr->atomic_cap;
333         resp->max_ee                    = attr->max_ee;
334         resp->max_rdd                   = attr->max_rdd;
335         resp->max_mw                    = attr->max_mw;
336         resp->max_raw_ipv6_qp           = attr->max_raw_ipv6_qp;
337         resp->max_raw_ethy_qp           = attr->max_raw_ethy_qp;
338         resp->max_mcast_grp             = attr->max_mcast_grp;
339         resp->max_mcast_qp_attach       = attr->max_mcast_qp_attach;
340         resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
341         resp->max_ah                    = attr->max_ah;
342         resp->max_fmr                   = attr->max_fmr;
343         resp->max_map_per_fmr           = attr->max_map_per_fmr;
344         resp->max_srq                   = attr->max_srq;
345         resp->max_srq_wr                = attr->max_srq_wr;
346         resp->max_srq_sge               = attr->max_srq_sge;
347         resp->max_pkeys                 = attr->max_pkeys;
348         resp->local_ca_ack_delay        = attr->local_ca_ack_delay;
349         resp->phys_port_cnt             = ib_dev->phys_port_cnt;
350 }
351
352 static int ib_uverbs_query_device(struct uverbs_attr_bundle *attrs)
353 {
354         struct ib_uverbs_query_device      cmd;
355         struct ib_uverbs_query_device_resp resp;
356         struct ib_ucontext *ucontext;
357         int ret;
358
359         ucontext = ib_uverbs_get_ucontext(attrs);
360         if (IS_ERR(ucontext))
361                 return PTR_ERR(ucontext);
362
363         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
364         if (ret)
365                 return ret;
366
367         memset(&resp, 0, sizeof resp);
368         copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
369
370         return uverbs_response(attrs, &resp, sizeof(resp));
371 }
372
373 static int ib_uverbs_query_port(struct uverbs_attr_bundle *attrs)
374 {
375         struct ib_uverbs_query_port      cmd;
376         struct ib_uverbs_query_port_resp resp;
377         struct ib_port_attr              attr;
378         int                              ret;
379         struct ib_ucontext *ucontext;
380         struct ib_device *ib_dev;
381
382         ucontext = ib_uverbs_get_ucontext(attrs);
383         if (IS_ERR(ucontext))
384                 return PTR_ERR(ucontext);
385         ib_dev = ucontext->device;
386
387         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
388         if (ret)
389                 return ret;
390
391         ret = ib_query_port(ib_dev, cmd.port_num, &attr);
392         if (ret)
393                 return ret;
394
395         memset(&resp, 0, sizeof resp);
396         copy_port_attr_to_resp(&attr, &resp, ib_dev, cmd.port_num);
397
398         return uverbs_response(attrs, &resp, sizeof(resp));
399 }
400
401 static int ib_uverbs_alloc_pd(struct uverbs_attr_bundle *attrs)
402 {
403         struct ib_uverbs_alloc_pd      cmd;
404         struct ib_uverbs_alloc_pd_resp resp;
405         struct ib_uobject             *uobj;
406         struct ib_pd                  *pd;
407         int                            ret;
408         struct ib_device *ib_dev;
409
410         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
411         if (ret)
412                 return ret;
413
414         uobj = uobj_alloc(UVERBS_OBJECT_PD, attrs, &ib_dev);
415         if (IS_ERR(uobj))
416                 return PTR_ERR(uobj);
417
418         pd = rdma_zalloc_drv_obj(ib_dev, ib_pd);
419         if (!pd) {
420                 ret = -ENOMEM;
421                 goto err;
422         }
423
424         pd->device  = ib_dev;
425         pd->uobject = uobj;
426         pd->__internal_mr = NULL;
427         atomic_set(&pd->usecnt, 0);
428         pd->res.type = RDMA_RESTRACK_PD;
429
430         ret = ib_dev->ops.alloc_pd(pd, &attrs->driver_udata);
431         if (ret)
432                 goto err_alloc;
433
434         uobj->object = pd;
435         memset(&resp, 0, sizeof resp);
436         resp.pd_handle = uobj->id;
437         rdma_restrack_uadd(&pd->res);
438
439         ret = uverbs_response(attrs, &resp, sizeof(resp));
440         if (ret)
441                 goto err_copy;
442
443         rdma_alloc_commit_uobject(uobj, attrs);
444         return 0;
445
446 err_copy:
447         ib_dealloc_pd_user(pd, uverbs_get_cleared_udata(attrs));
448         pd = NULL;
449 err_alloc:
450         kfree(pd);
451 err:
452         uobj_alloc_abort(uobj, attrs);
453         return ret;
454 }
455
456 static int ib_uverbs_dealloc_pd(struct uverbs_attr_bundle *attrs)
457 {
458         struct ib_uverbs_dealloc_pd cmd;
459         int ret;
460
461         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
462         if (ret)
463                 return ret;
464
465         return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
466 }
467
468 struct xrcd_table_entry {
469         struct rb_node  node;
470         struct ib_xrcd *xrcd;
471         struct inode   *inode;
472 };
473
474 static int xrcd_table_insert(struct ib_uverbs_device *dev,
475                             struct inode *inode,
476                             struct ib_xrcd *xrcd)
477 {
478         struct xrcd_table_entry *entry, *scan;
479         struct rb_node **p = &dev->xrcd_tree.rb_node;
480         struct rb_node *parent = NULL;
481
482         entry = kmalloc(sizeof *entry, GFP_KERNEL);
483         if (!entry)
484                 return -ENOMEM;
485
486         entry->xrcd  = xrcd;
487         entry->inode = inode;
488
489         while (*p) {
490                 parent = *p;
491                 scan = rb_entry(parent, struct xrcd_table_entry, node);
492
493                 if (inode < scan->inode) {
494                         p = &(*p)->rb_left;
495                 } else if (inode > scan->inode) {
496                         p = &(*p)->rb_right;
497                 } else {
498                         kfree(entry);
499                         return -EEXIST;
500                 }
501         }
502
503         rb_link_node(&entry->node, parent, p);
504         rb_insert_color(&entry->node, &dev->xrcd_tree);
505         igrab(inode);
506         return 0;
507 }
508
509 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
510                                                   struct inode *inode)
511 {
512         struct xrcd_table_entry *entry;
513         struct rb_node *p = dev->xrcd_tree.rb_node;
514
515         while (p) {
516                 entry = rb_entry(p, struct xrcd_table_entry, node);
517
518                 if (inode < entry->inode)
519                         p = p->rb_left;
520                 else if (inode > entry->inode)
521                         p = p->rb_right;
522                 else
523                         return entry;
524         }
525
526         return NULL;
527 }
528
529 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
530 {
531         struct xrcd_table_entry *entry;
532
533         entry = xrcd_table_search(dev, inode);
534         if (!entry)
535                 return NULL;
536
537         return entry->xrcd;
538 }
539
540 static void xrcd_table_delete(struct ib_uverbs_device *dev,
541                               struct inode *inode)
542 {
543         struct xrcd_table_entry *entry;
544
545         entry = xrcd_table_search(dev, inode);
546         if (entry) {
547                 iput(inode);
548                 rb_erase(&entry->node, &dev->xrcd_tree);
549                 kfree(entry);
550         }
551 }
552
553 static int ib_uverbs_open_xrcd(struct uverbs_attr_bundle *attrs)
554 {
555         struct ib_uverbs_device *ibudev = attrs->ufile->device;
556         struct ib_uverbs_open_xrcd      cmd;
557         struct ib_uverbs_open_xrcd_resp resp;
558         struct ib_uxrcd_object         *obj;
559         struct ib_xrcd                 *xrcd = NULL;
560         struct fd                       f = {NULL, 0};
561         struct inode                   *inode = NULL;
562         int                             ret = 0;
563         int                             new_xrcd = 0;
564         struct ib_device *ib_dev;
565
566         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
567         if (ret)
568                 return ret;
569
570         mutex_lock(&ibudev->xrcd_tree_mutex);
571
572         if (cmd.fd != -1) {
573                 /* search for file descriptor */
574                 f = fdget(cmd.fd);
575                 if (!f.file) {
576                         ret = -EBADF;
577                         goto err_tree_mutex_unlock;
578                 }
579
580                 inode = file_inode(f.file);
581                 xrcd = find_xrcd(ibudev, inode);
582                 if (!xrcd && !(cmd.oflags & O_CREAT)) {
583                         /* no file descriptor. Need CREATE flag */
584                         ret = -EAGAIN;
585                         goto err_tree_mutex_unlock;
586                 }
587
588                 if (xrcd && cmd.oflags & O_EXCL) {
589                         ret = -EINVAL;
590                         goto err_tree_mutex_unlock;
591                 }
592         }
593
594         obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, attrs,
595                                                    &ib_dev);
596         if (IS_ERR(obj)) {
597                 ret = PTR_ERR(obj);
598                 goto err_tree_mutex_unlock;
599         }
600
601         if (!xrcd) {
602                 xrcd = ib_dev->ops.alloc_xrcd(ib_dev, &attrs->driver_udata);
603                 if (IS_ERR(xrcd)) {
604                         ret = PTR_ERR(xrcd);
605                         goto err;
606                 }
607
608                 xrcd->inode   = inode;
609                 xrcd->device  = ib_dev;
610                 atomic_set(&xrcd->usecnt, 0);
611                 mutex_init(&xrcd->tgt_qp_mutex);
612                 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
613                 new_xrcd = 1;
614         }
615
616         atomic_set(&obj->refcnt, 0);
617         obj->uobject.object = xrcd;
618         memset(&resp, 0, sizeof resp);
619         resp.xrcd_handle = obj->uobject.id;
620
621         if (inode) {
622                 if (new_xrcd) {
623                         /* create new inode/xrcd table entry */
624                         ret = xrcd_table_insert(ibudev, inode, xrcd);
625                         if (ret)
626                                 goto err_dealloc_xrcd;
627                 }
628                 atomic_inc(&xrcd->usecnt);
629         }
630
631         ret = uverbs_response(attrs, &resp, sizeof(resp));
632         if (ret)
633                 goto err_copy;
634
635         if (f.file)
636                 fdput(f);
637
638         mutex_unlock(&ibudev->xrcd_tree_mutex);
639
640         rdma_alloc_commit_uobject(&obj->uobject, attrs);
641         return 0;
642
643 err_copy:
644         if (inode) {
645                 if (new_xrcd)
646                         xrcd_table_delete(ibudev, inode);
647                 atomic_dec(&xrcd->usecnt);
648         }
649
650 err_dealloc_xrcd:
651         ib_dealloc_xrcd(xrcd, uverbs_get_cleared_udata(attrs));
652
653 err:
654         uobj_alloc_abort(&obj->uobject, attrs);
655
656 err_tree_mutex_unlock:
657         if (f.file)
658                 fdput(f);
659
660         mutex_unlock(&ibudev->xrcd_tree_mutex);
661
662         return ret;
663 }
664
665 static int ib_uverbs_close_xrcd(struct uverbs_attr_bundle *attrs)
666 {
667         struct ib_uverbs_close_xrcd cmd;
668         int ret;
669
670         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
671         if (ret)
672                 return ret;
673
674         return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, attrs);
675 }
676
677 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject, struct ib_xrcd *xrcd,
678                            enum rdma_remove_reason why,
679                            struct uverbs_attr_bundle *attrs)
680 {
681         struct inode *inode;
682         int ret;
683         struct ib_uverbs_device *dev = attrs->ufile->device;
684
685         inode = xrcd->inode;
686         if (inode && !atomic_dec_and_test(&xrcd->usecnt))
687                 return 0;
688
689         ret = ib_dealloc_xrcd(xrcd, &attrs->driver_udata);
690
691         if (ib_is_destroy_retryable(ret, why, uobject)) {
692                 atomic_inc(&xrcd->usecnt);
693                 return ret;
694         }
695
696         if (inode)
697                 xrcd_table_delete(dev, inode);
698
699         return ret;
700 }
701
702 static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
703 {
704         struct ib_uverbs_reg_mr      cmd;
705         struct ib_uverbs_reg_mr_resp resp;
706         struct ib_uobject           *uobj;
707         struct ib_pd                *pd;
708         struct ib_mr                *mr;
709         int                          ret;
710         struct ib_device *ib_dev;
711
712         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
713         if (ret)
714                 return ret;
715
716         if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
717                 return -EINVAL;
718
719         ret = ib_check_mr_access(cmd.access_flags);
720         if (ret)
721                 return ret;
722
723         uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
724         if (IS_ERR(uobj))
725                 return PTR_ERR(uobj);
726
727         pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
728         if (!pd) {
729                 ret = -EINVAL;
730                 goto err_free;
731         }
732
733         if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
734                 if (!(pd->device->attrs.device_cap_flags &
735                       IB_DEVICE_ON_DEMAND_PAGING)) {
736                         pr_debug("ODP support not available\n");
737                         ret = -EINVAL;
738                         goto err_put;
739                 }
740         }
741
742         mr = pd->device->ops.reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
743                                          cmd.access_flags,
744                                          &attrs->driver_udata);
745         if (IS_ERR(mr)) {
746                 ret = PTR_ERR(mr);
747                 goto err_put;
748         }
749
750         mr->device  = pd->device;
751         mr->pd      = pd;
752         mr->type    = IB_MR_TYPE_USER;
753         mr->dm      = NULL;
754         mr->sig_attrs = NULL;
755         mr->uobject = uobj;
756         atomic_inc(&pd->usecnt);
757         mr->res.type = RDMA_RESTRACK_MR;
758         rdma_restrack_uadd(&mr->res);
759
760         uobj->object = mr;
761
762         memset(&resp, 0, sizeof resp);
763         resp.lkey      = mr->lkey;
764         resp.rkey      = mr->rkey;
765         resp.mr_handle = uobj->id;
766
767         ret = uverbs_response(attrs, &resp, sizeof(resp));
768         if (ret)
769                 goto err_copy;
770
771         uobj_put_obj_read(pd);
772
773         rdma_alloc_commit_uobject(uobj, attrs);
774         return 0;
775
776 err_copy:
777         ib_dereg_mr_user(mr, uverbs_get_cleared_udata(attrs));
778
779 err_put:
780         uobj_put_obj_read(pd);
781
782 err_free:
783         uobj_alloc_abort(uobj, attrs);
784         return ret;
785 }
786
787 static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs)
788 {
789         struct ib_uverbs_rereg_mr      cmd;
790         struct ib_uverbs_rereg_mr_resp resp;
791         struct ib_pd                *pd = NULL;
792         struct ib_mr                *mr;
793         struct ib_pd                *old_pd;
794         int                          ret;
795         struct ib_uobject           *uobj;
796
797         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
798         if (ret)
799                 return ret;
800
801         if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
802                 return -EINVAL;
803
804         if ((cmd.flags & IB_MR_REREG_TRANS) &&
805             (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
806              (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
807                         return -EINVAL;
808
809         uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
810         if (IS_ERR(uobj))
811                 return PTR_ERR(uobj);
812
813         mr = uobj->object;
814
815         if (mr->dm) {
816                 ret = -EINVAL;
817                 goto put_uobjs;
818         }
819
820         if (cmd.flags & IB_MR_REREG_ACCESS) {
821                 ret = ib_check_mr_access(cmd.access_flags);
822                 if (ret)
823                         goto put_uobjs;
824         }
825
826         if (cmd.flags & IB_MR_REREG_PD) {
827                 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
828                                        attrs);
829                 if (!pd) {
830                         ret = -EINVAL;
831                         goto put_uobjs;
832                 }
833         }
834
835         old_pd = mr->pd;
836         ret = mr->device->ops.rereg_user_mr(mr, cmd.flags, cmd.start,
837                                             cmd.length, cmd.hca_va,
838                                             cmd.access_flags, pd,
839                                             &attrs->driver_udata);
840         if (ret)
841                 goto put_uobj_pd;
842
843         if (cmd.flags & IB_MR_REREG_PD) {
844                 atomic_inc(&pd->usecnt);
845                 mr->pd = pd;
846                 atomic_dec(&old_pd->usecnt);
847         }
848
849         memset(&resp, 0, sizeof(resp));
850         resp.lkey      = mr->lkey;
851         resp.rkey      = mr->rkey;
852
853         ret = uverbs_response(attrs, &resp, sizeof(resp));
854
855 put_uobj_pd:
856         if (cmd.flags & IB_MR_REREG_PD)
857                 uobj_put_obj_read(pd);
858
859 put_uobjs:
860         uobj_put_write(uobj);
861
862         return ret;
863 }
864
865 static int ib_uverbs_dereg_mr(struct uverbs_attr_bundle *attrs)
866 {
867         struct ib_uverbs_dereg_mr cmd;
868         int ret;
869
870         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
871         if (ret)
872                 return ret;
873
874         return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
875 }
876
877 static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs)
878 {
879         struct ib_uverbs_alloc_mw      cmd;
880         struct ib_uverbs_alloc_mw_resp resp;
881         struct ib_uobject             *uobj;
882         struct ib_pd                  *pd;
883         struct ib_mw                  *mw;
884         int                            ret;
885         struct ib_device *ib_dev;
886
887         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
888         if (ret)
889                 return ret;
890
891         uobj = uobj_alloc(UVERBS_OBJECT_MW, attrs, &ib_dev);
892         if (IS_ERR(uobj))
893                 return PTR_ERR(uobj);
894
895         pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
896         if (!pd) {
897                 ret = -EINVAL;
898                 goto err_free;
899         }
900
901         if (cmd.mw_type != IB_MW_TYPE_1 && cmd.mw_type != IB_MW_TYPE_2) {
902                 ret = -EINVAL;
903                 goto err_put;
904         }
905
906         mw = pd->device->ops.alloc_mw(pd, cmd.mw_type, &attrs->driver_udata);
907         if (IS_ERR(mw)) {
908                 ret = PTR_ERR(mw);
909                 goto err_put;
910         }
911
912         mw->device  = pd->device;
913         mw->pd      = pd;
914         mw->uobject = uobj;
915         atomic_inc(&pd->usecnt);
916
917         uobj->object = mw;
918
919         memset(&resp, 0, sizeof(resp));
920         resp.rkey      = mw->rkey;
921         resp.mw_handle = uobj->id;
922
923         ret = uverbs_response(attrs, &resp, sizeof(resp));
924         if (ret)
925                 goto err_copy;
926
927         uobj_put_obj_read(pd);
928         rdma_alloc_commit_uobject(uobj, attrs);
929         return 0;
930
931 err_copy:
932         uverbs_dealloc_mw(mw);
933 err_put:
934         uobj_put_obj_read(pd);
935 err_free:
936         uobj_alloc_abort(uobj, attrs);
937         return ret;
938 }
939
940 static int ib_uverbs_dealloc_mw(struct uverbs_attr_bundle *attrs)
941 {
942         struct ib_uverbs_dealloc_mw cmd;
943         int ret;
944
945         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
946         if (ret)
947                 return ret;
948
949         return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, attrs);
950 }
951
952 static int ib_uverbs_create_comp_channel(struct uverbs_attr_bundle *attrs)
953 {
954         struct ib_uverbs_create_comp_channel       cmd;
955         struct ib_uverbs_create_comp_channel_resp  resp;
956         struct ib_uobject                         *uobj;
957         struct ib_uverbs_completion_event_file    *ev_file;
958         struct ib_device *ib_dev;
959         int ret;
960
961         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
962         if (ret)
963                 return ret;
964
965         uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, attrs, &ib_dev);
966         if (IS_ERR(uobj))
967                 return PTR_ERR(uobj);
968
969         resp.fd = uobj->id;
970
971         ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
972                                uobj);
973         ib_uverbs_init_event_queue(&ev_file->ev_queue);
974
975         ret = uverbs_response(attrs, &resp, sizeof(resp));
976         if (ret) {
977                 uobj_alloc_abort(uobj, attrs);
978                 return ret;
979         }
980
981         rdma_alloc_commit_uobject(uobj, attrs);
982         return 0;
983 }
984
985 static struct ib_ucq_object *create_cq(struct uverbs_attr_bundle *attrs,
986                                        struct ib_uverbs_ex_create_cq *cmd)
987 {
988         struct ib_ucq_object           *obj;
989         struct ib_uverbs_completion_event_file    *ev_file = NULL;
990         struct ib_cq                   *cq;
991         int                             ret;
992         struct ib_uverbs_ex_create_cq_resp resp;
993         struct ib_cq_init_attr attr = {};
994         struct ib_device *ib_dev;
995
996         if (cmd->comp_vector >= attrs->ufile->device->num_comp_vectors)
997                 return ERR_PTR(-EINVAL);
998
999         obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, attrs,
1000                                                  &ib_dev);
1001         if (IS_ERR(obj))
1002                 return obj;
1003
1004         if (cmd->comp_channel >= 0) {
1005                 ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, attrs);
1006                 if (IS_ERR(ev_file)) {
1007                         ret = PTR_ERR(ev_file);
1008                         goto err;
1009                 }
1010         }
1011
1012         obj->uevent.uobject.user_handle = cmd->user_handle;
1013         INIT_LIST_HEAD(&obj->comp_list);
1014         INIT_LIST_HEAD(&obj->uevent.event_list);
1015
1016         attr.cqe = cmd->cqe;
1017         attr.comp_vector = cmd->comp_vector;
1018         attr.flags = cmd->flags;
1019
1020         cq = rdma_zalloc_drv_obj(ib_dev, ib_cq);
1021         if (!cq) {
1022                 ret = -ENOMEM;
1023                 goto err_file;
1024         }
1025         cq->device        = ib_dev;
1026         cq->uobject       = obj;
1027         cq->comp_handler  = ib_uverbs_comp_handler;
1028         cq->event_handler = ib_uverbs_cq_event_handler;
1029         cq->cq_context    = ev_file ? &ev_file->ev_queue : NULL;
1030         atomic_set(&cq->usecnt, 0);
1031
1032         ret = ib_dev->ops.create_cq(cq, &attr, &attrs->driver_udata);
1033         if (ret)
1034                 goto err_free;
1035
1036         obj->uevent.uobject.object = cq;
1037         memset(&resp, 0, sizeof resp);
1038         resp.base.cq_handle = obj->uevent.uobject.id;
1039         resp.base.cqe       = cq->cqe;
1040         resp.response_length = uverbs_response_length(attrs, sizeof(resp));
1041
1042         cq->res.type = RDMA_RESTRACK_CQ;
1043         rdma_restrack_uadd(&cq->res);
1044
1045         ret = uverbs_response(attrs, &resp, sizeof(resp));
1046         if (ret)
1047                 goto err_cb;
1048
1049         rdma_alloc_commit_uobject(&obj->uevent.uobject, attrs);
1050         return obj;
1051
1052 err_cb:
1053         ib_destroy_cq_user(cq, uverbs_get_cleared_udata(attrs));
1054         cq = NULL;
1055 err_free:
1056         kfree(cq);
1057 err_file:
1058         if (ev_file)
1059                 ib_uverbs_release_ucq(ev_file, obj);
1060
1061 err:
1062         uobj_alloc_abort(&obj->uevent.uobject, attrs);
1063
1064         return ERR_PTR(ret);
1065 }
1066
1067 static int ib_uverbs_create_cq(struct uverbs_attr_bundle *attrs)
1068 {
1069         struct ib_uverbs_create_cq      cmd;
1070         struct ib_uverbs_ex_create_cq   cmd_ex;
1071         struct ib_ucq_object           *obj;
1072         int ret;
1073
1074         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1075         if (ret)
1076                 return ret;
1077
1078         memset(&cmd_ex, 0, sizeof(cmd_ex));
1079         cmd_ex.user_handle = cmd.user_handle;
1080         cmd_ex.cqe = cmd.cqe;
1081         cmd_ex.comp_vector = cmd.comp_vector;
1082         cmd_ex.comp_channel = cmd.comp_channel;
1083
1084         obj = create_cq(attrs, &cmd_ex);
1085         return PTR_ERR_OR_ZERO(obj);
1086 }
1087
1088 static int ib_uverbs_ex_create_cq(struct uverbs_attr_bundle *attrs)
1089 {
1090         struct ib_uverbs_ex_create_cq  cmd;
1091         struct ib_ucq_object           *obj;
1092         int ret;
1093
1094         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1095         if (ret)
1096                 return ret;
1097
1098         if (cmd.comp_mask)
1099                 return -EINVAL;
1100
1101         if (cmd.reserved)
1102                 return -EINVAL;
1103
1104         obj = create_cq(attrs, &cmd);
1105         return PTR_ERR_OR_ZERO(obj);
1106 }
1107
1108 static int ib_uverbs_resize_cq(struct uverbs_attr_bundle *attrs)
1109 {
1110         struct ib_uverbs_resize_cq      cmd;
1111         struct ib_uverbs_resize_cq_resp resp = {};
1112         struct ib_cq                    *cq;
1113         int                             ret = -EINVAL;
1114
1115         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1116         if (ret)
1117                 return ret;
1118
1119         cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1120         if (!cq)
1121                 return -EINVAL;
1122
1123         ret = cq->device->ops.resize_cq(cq, cmd.cqe, &attrs->driver_udata);
1124         if (ret)
1125                 goto out;
1126
1127         resp.cqe = cq->cqe;
1128
1129         ret = uverbs_response(attrs, &resp, sizeof(resp));
1130 out:
1131         rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1132                                 UVERBS_LOOKUP_READ);
1133
1134         return ret;
1135 }
1136
1137 static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
1138                            struct ib_wc *wc)
1139 {
1140         struct ib_uverbs_wc tmp;
1141
1142         tmp.wr_id               = wc->wr_id;
1143         tmp.status              = wc->status;
1144         tmp.opcode              = wc->opcode;
1145         tmp.vendor_err          = wc->vendor_err;
1146         tmp.byte_len            = wc->byte_len;
1147         tmp.ex.imm_data         = wc->ex.imm_data;
1148         tmp.qp_num              = wc->qp->qp_num;
1149         tmp.src_qp              = wc->src_qp;
1150         tmp.wc_flags            = wc->wc_flags;
1151         tmp.pkey_index          = wc->pkey_index;
1152         if (rdma_cap_opa_ah(ib_dev, wc->port_num))
1153                 tmp.slid        = OPA_TO_IB_UCAST_LID(wc->slid);
1154         else
1155                 tmp.slid        = ib_lid_cpu16(wc->slid);
1156         tmp.sl                  = wc->sl;
1157         tmp.dlid_path_bits      = wc->dlid_path_bits;
1158         tmp.port_num            = wc->port_num;
1159         tmp.reserved            = 0;
1160
1161         if (copy_to_user(dest, &tmp, sizeof tmp))
1162                 return -EFAULT;
1163
1164         return 0;
1165 }
1166
1167 static int ib_uverbs_poll_cq(struct uverbs_attr_bundle *attrs)
1168 {
1169         struct ib_uverbs_poll_cq       cmd;
1170         struct ib_uverbs_poll_cq_resp  resp;
1171         u8 __user                     *header_ptr;
1172         u8 __user                     *data_ptr;
1173         struct ib_cq                  *cq;
1174         struct ib_wc                   wc;
1175         int                            ret;
1176
1177         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1178         if (ret)
1179                 return ret;
1180
1181         cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1182         if (!cq)
1183                 return -EINVAL;
1184
1185         /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1186         header_ptr = attrs->ucore.outbuf;
1187         data_ptr = header_ptr + sizeof resp;
1188
1189         memset(&resp, 0, sizeof resp);
1190         while (resp.count < cmd.ne) {
1191                 ret = ib_poll_cq(cq, 1, &wc);
1192                 if (ret < 0)
1193                         goto out_put;
1194                 if (!ret)
1195                         break;
1196
1197                 ret = copy_wc_to_user(cq->device, data_ptr, &wc);
1198                 if (ret)
1199                         goto out_put;
1200
1201                 data_ptr += sizeof(struct ib_uverbs_wc);
1202                 ++resp.count;
1203         }
1204
1205         if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1206                 ret = -EFAULT;
1207                 goto out_put;
1208         }
1209         ret = 0;
1210
1211         if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
1212                 ret = uverbs_output_written(attrs, UVERBS_ATTR_CORE_OUT);
1213
1214 out_put:
1215         rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1216                                 UVERBS_LOOKUP_READ);
1217         return ret;
1218 }
1219
1220 static int ib_uverbs_req_notify_cq(struct uverbs_attr_bundle *attrs)
1221 {
1222         struct ib_uverbs_req_notify_cq cmd;
1223         struct ib_cq                  *cq;
1224         int ret;
1225
1226         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1227         if (ret)
1228                 return ret;
1229
1230         cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1231         if (!cq)
1232                 return -EINVAL;
1233
1234         ib_req_notify_cq(cq, cmd.solicited_only ?
1235                          IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1236
1237         rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1238                                 UVERBS_LOOKUP_READ);
1239         return 0;
1240 }
1241
1242 static int ib_uverbs_destroy_cq(struct uverbs_attr_bundle *attrs)
1243 {
1244         struct ib_uverbs_destroy_cq      cmd;
1245         struct ib_uverbs_destroy_cq_resp resp;
1246         struct ib_uobject               *uobj;
1247         struct ib_ucq_object            *obj;
1248         int ret;
1249
1250         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1251         if (ret)
1252                 return ret;
1253
1254         uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1255         if (IS_ERR(uobj))
1256                 return PTR_ERR(uobj);
1257
1258         obj = container_of(uobj, struct ib_ucq_object, uevent.uobject);
1259         memset(&resp, 0, sizeof(resp));
1260         resp.comp_events_reported  = obj->comp_events_reported;
1261         resp.async_events_reported = obj->uevent.events_reported;
1262
1263         uobj_put_destroy(uobj);
1264
1265         return uverbs_response(attrs, &resp, sizeof(resp));
1266 }
1267
1268 static int create_qp(struct uverbs_attr_bundle *attrs,
1269                      struct ib_uverbs_ex_create_qp *cmd)
1270 {
1271         struct ib_uqp_object            *obj;
1272         struct ib_device                *device;
1273         struct ib_pd                    *pd = NULL;
1274         struct ib_xrcd                  *xrcd = NULL;
1275         struct ib_uobject               *xrcd_uobj = ERR_PTR(-ENOENT);
1276         struct ib_cq                    *scq = NULL, *rcq = NULL;
1277         struct ib_srq                   *srq = NULL;
1278         struct ib_qp                    *qp;
1279         struct ib_qp_init_attr          attr = {};
1280         struct ib_uverbs_ex_create_qp_resp resp;
1281         int                             ret;
1282         struct ib_rwq_ind_table *ind_tbl = NULL;
1283         bool has_sq = true;
1284         struct ib_device *ib_dev;
1285
1286         if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
1287                 return -EPERM;
1288
1289         obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1290                                                  &ib_dev);
1291         if (IS_ERR(obj))
1292                 return PTR_ERR(obj);
1293         obj->uxrcd = NULL;
1294         obj->uevent.uobject.user_handle = cmd->user_handle;
1295         mutex_init(&obj->mcast_lock);
1296
1297         if (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE) {
1298                 ind_tbl = uobj_get_obj_read(rwq_ind_table,
1299                                             UVERBS_OBJECT_RWQ_IND_TBL,
1300                                             cmd->rwq_ind_tbl_handle, attrs);
1301                 if (!ind_tbl) {
1302                         ret = -EINVAL;
1303                         goto err_put;
1304                 }
1305
1306                 attr.rwq_ind_tbl = ind_tbl;
1307         }
1308
1309         if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
1310                 ret = -EINVAL;
1311                 goto err_put;
1312         }
1313
1314         if (ind_tbl && !cmd->max_send_wr)
1315                 has_sq = false;
1316
1317         if (cmd->qp_type == IB_QPT_XRC_TGT) {
1318                 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
1319                                           attrs);
1320
1321                 if (IS_ERR(xrcd_uobj)) {
1322                         ret = -EINVAL;
1323                         goto err_put;
1324                 }
1325
1326                 xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1327                 if (!xrcd) {
1328                         ret = -EINVAL;
1329                         goto err_put;
1330                 }
1331                 device = xrcd->device;
1332         } else {
1333                 if (cmd->qp_type == IB_QPT_XRC_INI) {
1334                         cmd->max_recv_wr = 0;
1335                         cmd->max_recv_sge = 0;
1336                 } else {
1337                         if (cmd->is_srq) {
1338                                 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
1339                                                         cmd->srq_handle, attrs);
1340                                 if (!srq || srq->srq_type == IB_SRQT_XRC) {
1341                                         ret = -EINVAL;
1342                                         goto err_put;
1343                                 }
1344                         }
1345
1346                         if (!ind_tbl) {
1347                                 if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1348                                         rcq = uobj_get_obj_read(
1349                                                 cq, UVERBS_OBJECT_CQ,
1350                                                 cmd->recv_cq_handle, attrs);
1351                                         if (!rcq) {
1352                                                 ret = -EINVAL;
1353                                                 goto err_put;
1354                                         }
1355                                 }
1356                         }
1357                 }
1358
1359                 if (has_sq)
1360                         scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
1361                                                 cmd->send_cq_handle, attrs);
1362                 if (!ind_tbl)
1363                         rcq = rcq ?: scq;
1364                 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
1365                                        attrs);
1366                 if (!pd || (!scq && has_sq)) {
1367                         ret = -EINVAL;
1368                         goto err_put;
1369                 }
1370
1371                 device = pd->device;
1372         }
1373
1374         attr.event_handler = ib_uverbs_qp_event_handler;
1375         attr.send_cq       = scq;
1376         attr.recv_cq       = rcq;
1377         attr.srq           = srq;
1378         attr.xrcd          = xrcd;
1379         attr.sq_sig_type   = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1380                                               IB_SIGNAL_REQ_WR;
1381         attr.qp_type       = cmd->qp_type;
1382         attr.create_flags  = 0;
1383
1384         attr.cap.max_send_wr     = cmd->max_send_wr;
1385         attr.cap.max_recv_wr     = cmd->max_recv_wr;
1386         attr.cap.max_send_sge    = cmd->max_send_sge;
1387         attr.cap.max_recv_sge    = cmd->max_recv_sge;
1388         attr.cap.max_inline_data = cmd->max_inline_data;
1389
1390         INIT_LIST_HEAD(&obj->uevent.event_list);
1391         INIT_LIST_HEAD(&obj->mcast_list);
1392
1393         attr.create_flags = cmd->create_flags;
1394         if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1395                                 IB_QP_CREATE_CROSS_CHANNEL |
1396                                 IB_QP_CREATE_MANAGED_SEND |
1397                                 IB_QP_CREATE_MANAGED_RECV |
1398                                 IB_QP_CREATE_SCATTER_FCS |
1399                                 IB_QP_CREATE_CVLAN_STRIPPING |
1400                                 IB_QP_CREATE_SOURCE_QPN |
1401                                 IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
1402                 ret = -EINVAL;
1403                 goto err_put;
1404         }
1405
1406         if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
1407                 if (!capable(CAP_NET_RAW)) {
1408                         ret = -EPERM;
1409                         goto err_put;
1410                 }
1411
1412                 attr.source_qpn = cmd->source_qpn;
1413         }
1414
1415         if (cmd->qp_type == IB_QPT_XRC_TGT)
1416                 qp = ib_create_qp(pd, &attr);
1417         else
1418                 qp = _ib_create_qp(device, pd, &attr, &attrs->driver_udata,
1419                                    obj);
1420
1421         if (IS_ERR(qp)) {
1422                 ret = PTR_ERR(qp);
1423                 goto err_put;
1424         }
1425
1426         if (cmd->qp_type != IB_QPT_XRC_TGT) {
1427                 ret = ib_create_qp_security(qp, device);
1428                 if (ret)
1429                         goto err_cb;
1430
1431                 qp->pd            = pd;
1432                 qp->send_cq       = attr.send_cq;
1433                 qp->recv_cq       = attr.recv_cq;
1434                 qp->srq           = attr.srq;
1435                 qp->rwq_ind_tbl   = ind_tbl;
1436                 qp->event_handler = attr.event_handler;
1437                 qp->qp_type       = attr.qp_type;
1438                 atomic_set(&qp->usecnt, 0);
1439                 atomic_inc(&pd->usecnt);
1440                 qp->port = 0;
1441                 if (attr.send_cq)
1442                         atomic_inc(&attr.send_cq->usecnt);
1443                 if (attr.recv_cq)
1444                         atomic_inc(&attr.recv_cq->usecnt);
1445                 if (attr.srq)
1446                         atomic_inc(&attr.srq->usecnt);
1447                 if (ind_tbl)
1448                         atomic_inc(&ind_tbl->usecnt);
1449         } else {
1450                 /* It is done in _ib_create_qp for other QP types */
1451                 qp->uobject = obj;
1452         }
1453
1454         obj->uevent.uobject.object = qp;
1455
1456         memset(&resp, 0, sizeof resp);
1457         resp.base.qpn             = qp->qp_num;
1458         resp.base.qp_handle       = obj->uevent.uobject.id;
1459         resp.base.max_recv_sge    = attr.cap.max_recv_sge;
1460         resp.base.max_send_sge    = attr.cap.max_send_sge;
1461         resp.base.max_recv_wr     = attr.cap.max_recv_wr;
1462         resp.base.max_send_wr     = attr.cap.max_send_wr;
1463         resp.base.max_inline_data = attr.cap.max_inline_data;
1464         resp.response_length = uverbs_response_length(attrs, sizeof(resp));
1465
1466         ret = uverbs_response(attrs, &resp, sizeof(resp));
1467         if (ret)
1468                 goto err_cb;
1469
1470         if (xrcd) {
1471                 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1472                                           uobject);
1473                 atomic_inc(&obj->uxrcd->refcnt);
1474                 uobj_put_read(xrcd_uobj);
1475         }
1476
1477         if (pd)
1478                 uobj_put_obj_read(pd);
1479         if (scq)
1480                 rdma_lookup_put_uobject(&scq->uobject->uevent.uobject,
1481                                         UVERBS_LOOKUP_READ);
1482         if (rcq && rcq != scq)
1483                 rdma_lookup_put_uobject(&rcq->uobject->uevent.uobject,
1484                                         UVERBS_LOOKUP_READ);
1485         if (srq)
1486                 rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
1487                                         UVERBS_LOOKUP_READ);
1488         if (ind_tbl)
1489                 uobj_put_obj_read(ind_tbl);
1490
1491         rdma_alloc_commit_uobject(&obj->uevent.uobject, attrs);
1492         return 0;
1493 err_cb:
1494         ib_destroy_qp_user(qp, uverbs_get_cleared_udata(attrs));
1495
1496 err_put:
1497         if (!IS_ERR(xrcd_uobj))
1498                 uobj_put_read(xrcd_uobj);
1499         if (pd)
1500                 uobj_put_obj_read(pd);
1501         if (scq)
1502                 rdma_lookup_put_uobject(&scq->uobject->uevent.uobject,
1503                                         UVERBS_LOOKUP_READ);
1504         if (rcq && rcq != scq)
1505                 rdma_lookup_put_uobject(&rcq->uobject->uevent.uobject,
1506                                         UVERBS_LOOKUP_READ);
1507         if (srq)
1508                 rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
1509                                         UVERBS_LOOKUP_READ);
1510         if (ind_tbl)
1511                 uobj_put_obj_read(ind_tbl);
1512
1513         uobj_alloc_abort(&obj->uevent.uobject, attrs);
1514         return ret;
1515 }
1516
1517 static int ib_uverbs_create_qp(struct uverbs_attr_bundle *attrs)
1518 {
1519         struct ib_uverbs_create_qp      cmd;
1520         struct ib_uverbs_ex_create_qp   cmd_ex;
1521         int ret;
1522
1523         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1524         if (ret)
1525                 return ret;
1526
1527         memset(&cmd_ex, 0, sizeof(cmd_ex));
1528         cmd_ex.user_handle = cmd.user_handle;
1529         cmd_ex.pd_handle = cmd.pd_handle;
1530         cmd_ex.send_cq_handle = cmd.send_cq_handle;
1531         cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1532         cmd_ex.srq_handle = cmd.srq_handle;
1533         cmd_ex.max_send_wr = cmd.max_send_wr;
1534         cmd_ex.max_recv_wr = cmd.max_recv_wr;
1535         cmd_ex.max_send_sge = cmd.max_send_sge;
1536         cmd_ex.max_recv_sge = cmd.max_recv_sge;
1537         cmd_ex.max_inline_data = cmd.max_inline_data;
1538         cmd_ex.sq_sig_all = cmd.sq_sig_all;
1539         cmd_ex.qp_type = cmd.qp_type;
1540         cmd_ex.is_srq = cmd.is_srq;
1541
1542         return create_qp(attrs, &cmd_ex);
1543 }
1544
1545 static int ib_uverbs_ex_create_qp(struct uverbs_attr_bundle *attrs)
1546 {
1547         struct ib_uverbs_ex_create_qp cmd;
1548         int ret;
1549
1550         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1551         if (ret)
1552                 return ret;
1553
1554         if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
1555                 return -EINVAL;
1556
1557         if (cmd.reserved)
1558                 return -EINVAL;
1559
1560         return create_qp(attrs, &cmd);
1561 }
1562
1563 static int ib_uverbs_open_qp(struct uverbs_attr_bundle *attrs)
1564 {
1565         struct ib_uverbs_open_qp        cmd;
1566         struct ib_uverbs_create_qp_resp resp;
1567         struct ib_uqp_object           *obj;
1568         struct ib_xrcd                 *xrcd;
1569         struct ib_uobject              *uninitialized_var(xrcd_uobj);
1570         struct ib_qp                   *qp;
1571         struct ib_qp_open_attr          attr = {};
1572         int ret;
1573         struct ib_device *ib_dev;
1574
1575         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1576         if (ret)
1577                 return ret;
1578
1579         obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1580                                                  &ib_dev);
1581         if (IS_ERR(obj))
1582                 return PTR_ERR(obj);
1583
1584         xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, attrs);
1585         if (IS_ERR(xrcd_uobj)) {
1586                 ret = -EINVAL;
1587                 goto err_put;
1588         }
1589
1590         xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1591         if (!xrcd) {
1592                 ret = -EINVAL;
1593                 goto err_xrcd;
1594         }
1595
1596         attr.event_handler = ib_uverbs_qp_event_handler;
1597         attr.qp_num        = cmd.qpn;
1598         attr.qp_type       = cmd.qp_type;
1599
1600         INIT_LIST_HEAD(&obj->uevent.event_list);
1601         INIT_LIST_HEAD(&obj->mcast_list);
1602
1603         qp = ib_open_qp(xrcd, &attr);
1604         if (IS_ERR(qp)) {
1605                 ret = PTR_ERR(qp);
1606                 goto err_xrcd;
1607         }
1608
1609         obj->uevent.uobject.object = qp;
1610         obj->uevent.uobject.user_handle = cmd.user_handle;
1611
1612         memset(&resp, 0, sizeof resp);
1613         resp.qpn       = qp->qp_num;
1614         resp.qp_handle = obj->uevent.uobject.id;
1615
1616         ret = uverbs_response(attrs, &resp, sizeof(resp));
1617         if (ret)
1618                 goto err_destroy;
1619
1620         obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1621         atomic_inc(&obj->uxrcd->refcnt);
1622         qp->uobject = obj;
1623         uobj_put_read(xrcd_uobj);
1624
1625         rdma_alloc_commit_uobject(&obj->uevent.uobject, attrs);
1626         return 0;
1627
1628 err_destroy:
1629         ib_destroy_qp_user(qp, uverbs_get_cleared_udata(attrs));
1630 err_xrcd:
1631         uobj_put_read(xrcd_uobj);
1632 err_put:
1633         uobj_alloc_abort(&obj->uevent.uobject, attrs);
1634         return ret;
1635 }
1636
1637 static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
1638                                    struct rdma_ah_attr *rdma_attr)
1639 {
1640         const struct ib_global_route   *grh;
1641
1642         uverb_attr->dlid              = rdma_ah_get_dlid(rdma_attr);
1643         uverb_attr->sl                = rdma_ah_get_sl(rdma_attr);
1644         uverb_attr->src_path_bits     = rdma_ah_get_path_bits(rdma_attr);
1645         uverb_attr->static_rate       = rdma_ah_get_static_rate(rdma_attr);
1646         uverb_attr->is_global         = !!(rdma_ah_get_ah_flags(rdma_attr) &
1647                                          IB_AH_GRH);
1648         if (uverb_attr->is_global) {
1649                 grh = rdma_ah_read_grh(rdma_attr);
1650                 memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
1651                 uverb_attr->flow_label        = grh->flow_label;
1652                 uverb_attr->sgid_index        = grh->sgid_index;
1653                 uverb_attr->hop_limit         = grh->hop_limit;
1654                 uverb_attr->traffic_class     = grh->traffic_class;
1655         }
1656         uverb_attr->port_num          = rdma_ah_get_port_num(rdma_attr);
1657 }
1658
1659 static int ib_uverbs_query_qp(struct uverbs_attr_bundle *attrs)
1660 {
1661         struct ib_uverbs_query_qp      cmd;
1662         struct ib_uverbs_query_qp_resp resp;
1663         struct ib_qp                   *qp;
1664         struct ib_qp_attr              *attr;
1665         struct ib_qp_init_attr         *init_attr;
1666         int                            ret;
1667
1668         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1669         if (ret)
1670                 return ret;
1671
1672         attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1673         init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1674         if (!attr || !init_attr) {
1675                 ret = -ENOMEM;
1676                 goto out;
1677         }
1678
1679         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1680         if (!qp) {
1681                 ret = -EINVAL;
1682                 goto out;
1683         }
1684
1685         ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1686
1687         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1688                                 UVERBS_LOOKUP_READ);
1689
1690         if (ret)
1691                 goto out;
1692
1693         memset(&resp, 0, sizeof resp);
1694
1695         resp.qp_state               = attr->qp_state;
1696         resp.cur_qp_state           = attr->cur_qp_state;
1697         resp.path_mtu               = attr->path_mtu;
1698         resp.path_mig_state         = attr->path_mig_state;
1699         resp.qkey                   = attr->qkey;
1700         resp.rq_psn                 = attr->rq_psn;
1701         resp.sq_psn                 = attr->sq_psn;
1702         resp.dest_qp_num            = attr->dest_qp_num;
1703         resp.qp_access_flags        = attr->qp_access_flags;
1704         resp.pkey_index             = attr->pkey_index;
1705         resp.alt_pkey_index         = attr->alt_pkey_index;
1706         resp.sq_draining            = attr->sq_draining;
1707         resp.max_rd_atomic          = attr->max_rd_atomic;
1708         resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1709         resp.min_rnr_timer          = attr->min_rnr_timer;
1710         resp.port_num               = attr->port_num;
1711         resp.timeout                = attr->timeout;
1712         resp.retry_cnt              = attr->retry_cnt;
1713         resp.rnr_retry              = attr->rnr_retry;
1714         resp.alt_port_num           = attr->alt_port_num;
1715         resp.alt_timeout            = attr->alt_timeout;
1716
1717         copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
1718         copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
1719
1720         resp.max_send_wr            = init_attr->cap.max_send_wr;
1721         resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1722         resp.max_send_sge           = init_attr->cap.max_send_sge;
1723         resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1724         resp.max_inline_data        = init_attr->cap.max_inline_data;
1725         resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1726
1727         ret = uverbs_response(attrs, &resp, sizeof(resp));
1728
1729 out:
1730         kfree(attr);
1731         kfree(init_attr);
1732
1733         return ret;
1734 }
1735
1736 /* Remove ignored fields set in the attribute mask */
1737 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1738 {
1739         switch (qp_type) {
1740         case IB_QPT_XRC_INI:
1741                 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1742         case IB_QPT_XRC_TGT:
1743                 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1744                                 IB_QP_RNR_RETRY);
1745         default:
1746                 return mask;
1747         }
1748 }
1749
1750 static void copy_ah_attr_from_uverbs(struct ib_device *dev,
1751                                      struct rdma_ah_attr *rdma_attr,
1752                                      struct ib_uverbs_qp_dest *uverb_attr)
1753 {
1754         rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
1755         if (uverb_attr->is_global) {
1756                 rdma_ah_set_grh(rdma_attr, NULL,
1757                                 uverb_attr->flow_label,
1758                                 uverb_attr->sgid_index,
1759                                 uverb_attr->hop_limit,
1760                                 uverb_attr->traffic_class);
1761                 rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
1762         } else {
1763                 rdma_ah_set_ah_flags(rdma_attr, 0);
1764         }
1765         rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
1766         rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
1767         rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
1768         rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
1769         rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
1770         rdma_ah_set_make_grd(rdma_attr, false);
1771 }
1772
1773 static int modify_qp(struct uverbs_attr_bundle *attrs,
1774                      struct ib_uverbs_ex_modify_qp *cmd)
1775 {
1776         struct ib_qp_attr *attr;
1777         struct ib_qp *qp;
1778         int ret;
1779
1780         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1781         if (!attr)
1782                 return -ENOMEM;
1783
1784         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle,
1785                                attrs);
1786         if (!qp) {
1787                 ret = -EINVAL;
1788                 goto out;
1789         }
1790
1791         if ((cmd->base.attr_mask & IB_QP_PORT) &&
1792             !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
1793                 ret = -EINVAL;
1794                 goto release_qp;
1795         }
1796
1797         if ((cmd->base.attr_mask & IB_QP_AV)) {
1798                 if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
1799                         ret = -EINVAL;
1800                         goto release_qp;
1801                 }
1802
1803                 if (cmd->base.attr_mask & IB_QP_STATE &&
1804                     cmd->base.qp_state == IB_QPS_RTR) {
1805                 /* We are in INIT->RTR TRANSITION (if we are not,
1806                  * this transition will be rejected in subsequent checks).
1807                  * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1808                  * but the IB_QP_STATE flag is required.
1809                  *
1810                  * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1811                  * when IB_QP_AV is set, has required inclusion of a valid
1812                  * port number in the primary AV. (AVs are created and handled
1813                  * differently for infiniband and ethernet (RoCE) ports).
1814                  *
1815                  * Check the port number included in the primary AV against
1816                  * the port number in the qp struct, which was set (and saved)
1817                  * in the RST->INIT transition.
1818                  */
1819                         if (cmd->base.dest.port_num != qp->real_qp->port) {
1820                                 ret = -EINVAL;
1821                                 goto release_qp;
1822                         }
1823                 } else {
1824                 /* We are in SQD->SQD. (If we are not, this transition will
1825                  * be rejected later in the verbs layer checks).
1826                  * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1827                  * together in the SQD->SQD transition.
1828                  *
1829                  * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1830                  * verbs layer driver does not track primary port changes
1831                  * resulting from path migration. Thus, in SQD, if the primary
1832                  * AV is modified, the primary port should also be modified).
1833                  *
1834                  * Note that in this transition, the IB_QP_STATE flag
1835                  * is not allowed.
1836                  */
1837                         if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1838                              == (IB_QP_AV | IB_QP_PORT)) &&
1839                             cmd->base.port_num != cmd->base.dest.port_num) {
1840                                 ret = -EINVAL;
1841                                 goto release_qp;
1842                         }
1843                         if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1844                             == IB_QP_AV) {
1845                                 cmd->base.attr_mask |= IB_QP_PORT;
1846                                 cmd->base.port_num = cmd->base.dest.port_num;
1847                         }
1848                 }
1849         }
1850
1851         if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
1852             (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
1853             !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
1854             cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
1855                 ret = -EINVAL;
1856                 goto release_qp;
1857         }
1858
1859         if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
1860             cmd->base.cur_qp_state > IB_QPS_ERR) ||
1861             (cmd->base.attr_mask & IB_QP_STATE &&
1862             cmd->base.qp_state > IB_QPS_ERR)) {
1863                 ret = -EINVAL;
1864                 goto release_qp;
1865         }
1866
1867         if (cmd->base.attr_mask & IB_QP_STATE)
1868                 attr->qp_state = cmd->base.qp_state;
1869         if (cmd->base.attr_mask & IB_QP_CUR_STATE)
1870                 attr->cur_qp_state = cmd->base.cur_qp_state;
1871         if (cmd->base.attr_mask & IB_QP_PATH_MTU)
1872                 attr->path_mtu = cmd->base.path_mtu;
1873         if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
1874                 attr->path_mig_state = cmd->base.path_mig_state;
1875         if (cmd->base.attr_mask & IB_QP_QKEY)
1876                 attr->qkey = cmd->base.qkey;
1877         if (cmd->base.attr_mask & IB_QP_RQ_PSN)
1878                 attr->rq_psn = cmd->base.rq_psn;
1879         if (cmd->base.attr_mask & IB_QP_SQ_PSN)
1880                 attr->sq_psn = cmd->base.sq_psn;
1881         if (cmd->base.attr_mask & IB_QP_DEST_QPN)
1882                 attr->dest_qp_num = cmd->base.dest_qp_num;
1883         if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
1884                 attr->qp_access_flags = cmd->base.qp_access_flags;
1885         if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
1886                 attr->pkey_index = cmd->base.pkey_index;
1887         if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
1888                 attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
1889         if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1890                 attr->max_rd_atomic = cmd->base.max_rd_atomic;
1891         if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1892                 attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
1893         if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
1894                 attr->min_rnr_timer = cmd->base.min_rnr_timer;
1895         if (cmd->base.attr_mask & IB_QP_PORT)
1896                 attr->port_num = cmd->base.port_num;
1897         if (cmd->base.attr_mask & IB_QP_TIMEOUT)
1898                 attr->timeout = cmd->base.timeout;
1899         if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
1900                 attr->retry_cnt = cmd->base.retry_cnt;
1901         if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
1902                 attr->rnr_retry = cmd->base.rnr_retry;
1903         if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
1904                 attr->alt_port_num = cmd->base.alt_port_num;
1905                 attr->alt_timeout = cmd->base.alt_timeout;
1906                 attr->alt_pkey_index = cmd->base.alt_pkey_index;
1907         }
1908         if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
1909                 attr->rate_limit = cmd->rate_limit;
1910
1911         if (cmd->base.attr_mask & IB_QP_AV)
1912                 copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
1913                                          &cmd->base.dest);
1914
1915         if (cmd->base.attr_mask & IB_QP_ALT_PATH)
1916                 copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
1917                                          &cmd->base.alt_dest);
1918
1919         ret = ib_modify_qp_with_udata(qp, attr,
1920                                       modify_qp_mask(qp->qp_type,
1921                                                      cmd->base.attr_mask),
1922                                       &attrs->driver_udata);
1923
1924 release_qp:
1925         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1926                                 UVERBS_LOOKUP_READ);
1927 out:
1928         kfree(attr);
1929
1930         return ret;
1931 }
1932
1933 static int ib_uverbs_modify_qp(struct uverbs_attr_bundle *attrs)
1934 {
1935         struct ib_uverbs_ex_modify_qp cmd;
1936         int ret;
1937
1938         ret = uverbs_request(attrs, &cmd.base, sizeof(cmd.base));
1939         if (ret)
1940                 return ret;
1941
1942         if (cmd.base.attr_mask &
1943             ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
1944                 return -EOPNOTSUPP;
1945
1946         return modify_qp(attrs, &cmd);
1947 }
1948
1949 static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle *attrs)
1950 {
1951         struct ib_uverbs_ex_modify_qp cmd;
1952         struct ib_uverbs_ex_modify_qp_resp resp = {
1953                 .response_length = uverbs_response_length(attrs, sizeof(resp))
1954         };
1955         int ret;
1956
1957         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1958         if (ret)
1959                 return ret;
1960
1961         /*
1962          * Last bit is reserved for extending the attr_mask by
1963          * using another field.
1964          */
1965         BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
1966
1967         if (cmd.base.attr_mask &
1968             ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
1969                 return -EOPNOTSUPP;
1970
1971         ret = modify_qp(attrs, &cmd);
1972         if (ret)
1973                 return ret;
1974
1975         return uverbs_response(attrs, &resp, sizeof(resp));
1976 }
1977
1978 static int ib_uverbs_destroy_qp(struct uverbs_attr_bundle *attrs)
1979 {
1980         struct ib_uverbs_destroy_qp      cmd;
1981         struct ib_uverbs_destroy_qp_resp resp;
1982         struct ib_uobject               *uobj;
1983         struct ib_uqp_object            *obj;
1984         int ret;
1985
1986         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1987         if (ret)
1988                 return ret;
1989
1990         uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1991         if (IS_ERR(uobj))
1992                 return PTR_ERR(uobj);
1993
1994         obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1995         memset(&resp, 0, sizeof(resp));
1996         resp.events_reported = obj->uevent.events_reported;
1997
1998         uobj_put_destroy(uobj);
1999
2000         return uverbs_response(attrs, &resp, sizeof(resp));
2001 }
2002
2003 static void *alloc_wr(size_t wr_size, __u32 num_sge)
2004 {
2005         if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
2006                        sizeof (struct ib_sge))
2007                 return NULL;
2008
2009         return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
2010                          num_sge * sizeof (struct ib_sge), GFP_KERNEL);
2011 }
2012
2013 static int ib_uverbs_post_send(struct uverbs_attr_bundle *attrs)
2014 {
2015         struct ib_uverbs_post_send      cmd;
2016         struct ib_uverbs_post_send_resp resp;
2017         struct ib_uverbs_send_wr       *user_wr;
2018         struct ib_send_wr              *wr = NULL, *last, *next;
2019         const struct ib_send_wr        *bad_wr;
2020         struct ib_qp                   *qp;
2021         int                             i, sg_ind;
2022         int                             is_ud;
2023         int ret, ret2;
2024         size_t                          next_size;
2025         const struct ib_sge __user *sgls;
2026         const void __user *wqes;
2027         struct uverbs_req_iter iter;
2028
2029         ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2030         if (ret)
2031                 return ret;
2032         wqes = uverbs_request_next_ptr(&iter, cmd.wqe_size * cmd.wr_count);
2033         if (IS_ERR(wqes))
2034                 return PTR_ERR(wqes);
2035         sgls = uverbs_request_next_ptr(
2036                 &iter, cmd.sge_count * sizeof(struct ib_uverbs_sge));
2037         if (IS_ERR(sgls))
2038                 return PTR_ERR(sgls);
2039         ret = uverbs_request_finish(&iter);
2040         if (ret)
2041                 return ret;
2042
2043         user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2044         if (!user_wr)
2045                 return -ENOMEM;
2046
2047         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2048         if (!qp) {
2049                 ret = -EINVAL;
2050                 goto out;
2051         }
2052
2053         is_ud = qp->qp_type == IB_QPT_UD;
2054         sg_ind = 0;
2055         last = NULL;
2056         for (i = 0; i < cmd.wr_count; ++i) {
2057                 if (copy_from_user(user_wr, wqes + i * cmd.wqe_size,
2058                                    cmd.wqe_size)) {
2059                         ret = -EFAULT;
2060                         goto out_put;
2061                 }
2062
2063                 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2064                         ret = -EINVAL;
2065                         goto out_put;
2066                 }
2067
2068                 if (is_ud) {
2069                         struct ib_ud_wr *ud;
2070
2071                         if (user_wr->opcode != IB_WR_SEND &&
2072                             user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2073                                 ret = -EINVAL;
2074                                 goto out_put;
2075                         }
2076
2077                         next_size = sizeof(*ud);
2078                         ud = alloc_wr(next_size, user_wr->num_sge);
2079                         if (!ud) {
2080                                 ret = -ENOMEM;
2081                                 goto out_put;
2082                         }
2083
2084                         ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2085                                                    user_wr->wr.ud.ah, attrs);
2086                         if (!ud->ah) {
2087                                 kfree(ud);
2088                                 ret = -EINVAL;
2089                                 goto out_put;
2090                         }
2091                         ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2092                         ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2093
2094                         next = &ud->wr;
2095                 } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2096                            user_wr->opcode == IB_WR_RDMA_WRITE ||
2097                            user_wr->opcode == IB_WR_RDMA_READ) {
2098                         struct ib_rdma_wr *rdma;
2099
2100                         next_size = sizeof(*rdma);
2101                         rdma = alloc_wr(next_size, user_wr->num_sge);
2102                         if (!rdma) {
2103                                 ret = -ENOMEM;
2104                                 goto out_put;
2105                         }
2106
2107                         rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2108                         rdma->rkey = user_wr->wr.rdma.rkey;
2109
2110                         next = &rdma->wr;
2111                 } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2112                            user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2113                         struct ib_atomic_wr *atomic;
2114
2115                         next_size = sizeof(*atomic);
2116                         atomic = alloc_wr(next_size, user_wr->num_sge);
2117                         if (!atomic) {
2118                                 ret = -ENOMEM;
2119                                 goto out_put;
2120                         }
2121
2122                         atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2123                         atomic->compare_add = user_wr->wr.atomic.compare_add;
2124                         atomic->swap = user_wr->wr.atomic.swap;
2125                         atomic->rkey = user_wr->wr.atomic.rkey;
2126
2127                         next = &atomic->wr;
2128                 } else if (user_wr->opcode == IB_WR_SEND ||
2129                            user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2130                            user_wr->opcode == IB_WR_SEND_WITH_INV) {
2131                         next_size = sizeof(*next);
2132                         next = alloc_wr(next_size, user_wr->num_sge);
2133                         if (!next) {
2134                                 ret = -ENOMEM;
2135                                 goto out_put;
2136                         }
2137                 } else {
2138                         ret = -EINVAL;
2139                         goto out_put;
2140                 }
2141
2142                 if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2143                     user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2144                         next->ex.imm_data =
2145                                         (__be32 __force) user_wr->ex.imm_data;
2146                 } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2147                         next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2148                 }
2149
2150                 if (!last)
2151                         wr = next;
2152                 else
2153                         last->next = next;
2154                 last = next;
2155
2156                 next->next       = NULL;
2157                 next->wr_id      = user_wr->wr_id;
2158                 next->num_sge    = user_wr->num_sge;
2159                 next->opcode     = user_wr->opcode;
2160                 next->send_flags = user_wr->send_flags;
2161
2162                 if (next->num_sge) {
2163                         next->sg_list = (void *) next +
2164                                 ALIGN(next_size, sizeof(struct ib_sge));
2165                         if (copy_from_user(next->sg_list, sgls + sg_ind,
2166                                            next->num_sge *
2167                                                    sizeof(struct ib_sge))) {
2168                                 ret = -EFAULT;
2169                                 goto out_put;
2170                         }
2171                         sg_ind += next->num_sge;
2172                 } else
2173                         next->sg_list = NULL;
2174         }
2175
2176         resp.bad_wr = 0;
2177         ret = qp->device->ops.post_send(qp->real_qp, wr, &bad_wr);
2178         if (ret)
2179                 for (next = wr; next; next = next->next) {
2180                         ++resp.bad_wr;
2181                         if (next == bad_wr)
2182                                 break;
2183                 }
2184
2185         ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2186         if (ret2)
2187                 ret = ret2;
2188
2189 out_put:
2190         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2191                                 UVERBS_LOOKUP_READ);
2192
2193         while (wr) {
2194                 if (is_ud && ud_wr(wr)->ah)
2195                         uobj_put_obj_read(ud_wr(wr)->ah);
2196                 next = wr->next;
2197                 kfree(wr);
2198                 wr = next;
2199         }
2200
2201 out:
2202         kfree(user_wr);
2203
2204         return ret;
2205 }
2206
2207 static struct ib_recv_wr *
2208 ib_uverbs_unmarshall_recv(struct uverbs_req_iter *iter, u32 wr_count,
2209                           u32 wqe_size, u32 sge_count)
2210 {
2211         struct ib_uverbs_recv_wr *user_wr;
2212         struct ib_recv_wr        *wr = NULL, *last, *next;
2213         int                       sg_ind;
2214         int                       i;
2215         int                       ret;
2216         const struct ib_sge __user *sgls;
2217         const void __user *wqes;
2218
2219         if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2220                 return ERR_PTR(-EINVAL);
2221
2222         wqes = uverbs_request_next_ptr(iter, wqe_size * wr_count);
2223         if (IS_ERR(wqes))
2224                 return ERR_CAST(wqes);
2225         sgls = uverbs_request_next_ptr(
2226                 iter, sge_count * sizeof(struct ib_uverbs_sge));
2227         if (IS_ERR(sgls))
2228                 return ERR_CAST(sgls);
2229         ret = uverbs_request_finish(iter);
2230         if (ret)
2231                 return ERR_PTR(ret);
2232
2233         user_wr = kmalloc(wqe_size, GFP_KERNEL);
2234         if (!user_wr)
2235                 return ERR_PTR(-ENOMEM);
2236
2237         sg_ind = 0;
2238         last = NULL;
2239         for (i = 0; i < wr_count; ++i) {
2240                 if (copy_from_user(user_wr, wqes + i * wqe_size,
2241                                    wqe_size)) {
2242                         ret = -EFAULT;
2243                         goto err;
2244                 }
2245
2246                 if (user_wr->num_sge + sg_ind > sge_count) {
2247                         ret = -EINVAL;
2248                         goto err;
2249                 }
2250
2251                 if (user_wr->num_sge >=
2252                     (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
2253                     sizeof (struct ib_sge)) {
2254                         ret = -EINVAL;
2255                         goto err;
2256                 }
2257
2258                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2259                                user_wr->num_sge * sizeof (struct ib_sge),
2260                                GFP_KERNEL);
2261                 if (!next) {
2262                         ret = -ENOMEM;
2263                         goto err;
2264                 }
2265
2266                 if (!last)
2267                         wr = next;
2268                 else
2269                         last->next = next;
2270                 last = next;
2271
2272                 next->next       = NULL;
2273                 next->wr_id      = user_wr->wr_id;
2274                 next->num_sge    = user_wr->num_sge;
2275
2276                 if (next->num_sge) {
2277                         next->sg_list = (void *) next +
2278                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
2279                         if (copy_from_user(next->sg_list, sgls + sg_ind,
2280                                            next->num_sge *
2281                                                    sizeof(struct ib_sge))) {
2282                                 ret = -EFAULT;
2283                                 goto err;
2284                         }
2285                         sg_ind += next->num_sge;
2286                 } else
2287                         next->sg_list = NULL;
2288         }
2289
2290         kfree(user_wr);
2291         return wr;
2292
2293 err:
2294         kfree(user_wr);
2295
2296         while (wr) {
2297                 next = wr->next;
2298                 kfree(wr);
2299                 wr = next;
2300         }
2301
2302         return ERR_PTR(ret);
2303 }
2304
2305 static int ib_uverbs_post_recv(struct uverbs_attr_bundle *attrs)
2306 {
2307         struct ib_uverbs_post_recv      cmd;
2308         struct ib_uverbs_post_recv_resp resp;
2309         struct ib_recv_wr              *wr, *next;
2310         const struct ib_recv_wr        *bad_wr;
2311         struct ib_qp                   *qp;
2312         int ret, ret2;
2313         struct uverbs_req_iter iter;
2314
2315         ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2316         if (ret)
2317                 return ret;
2318
2319         wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2320                                        cmd.sge_count);
2321         if (IS_ERR(wr))
2322                 return PTR_ERR(wr);
2323
2324         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2325         if (!qp) {
2326                 ret = -EINVAL;
2327                 goto out;
2328         }
2329
2330         resp.bad_wr = 0;
2331         ret = qp->device->ops.post_recv(qp->real_qp, wr, &bad_wr);
2332
2333         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2334                                 UVERBS_LOOKUP_READ);
2335         if (ret) {
2336                 for (next = wr; next; next = next->next) {
2337                         ++resp.bad_wr;
2338                         if (next == bad_wr)
2339                                 break;
2340                 }
2341         }
2342
2343         ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2344         if (ret2)
2345                 ret = ret2;
2346 out:
2347         while (wr) {
2348                 next = wr->next;
2349                 kfree(wr);
2350                 wr = next;
2351         }
2352
2353         return ret;
2354 }
2355
2356 static int ib_uverbs_post_srq_recv(struct uverbs_attr_bundle *attrs)
2357 {
2358         struct ib_uverbs_post_srq_recv      cmd;
2359         struct ib_uverbs_post_srq_recv_resp resp;
2360         struct ib_recv_wr                  *wr, *next;
2361         const struct ib_recv_wr            *bad_wr;
2362         struct ib_srq                      *srq;
2363         int ret, ret2;
2364         struct uverbs_req_iter iter;
2365
2366         ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2367         if (ret)
2368                 return ret;
2369
2370         wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2371                                        cmd.sge_count);
2372         if (IS_ERR(wr))
2373                 return PTR_ERR(wr);
2374
2375         srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
2376         if (!srq) {
2377                 ret = -EINVAL;
2378                 goto out;
2379         }
2380
2381         resp.bad_wr = 0;
2382         ret = srq->device->ops.post_srq_recv(srq, wr, &bad_wr);
2383
2384         rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
2385                                 UVERBS_LOOKUP_READ);
2386
2387         if (ret)
2388                 for (next = wr; next; next = next->next) {
2389                         ++resp.bad_wr;
2390                         if (next == bad_wr)
2391                                 break;
2392                 }
2393
2394         ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2395         if (ret2)
2396                 ret = ret2;
2397
2398 out:
2399         while (wr) {
2400                 next = wr->next;
2401                 kfree(wr);
2402                 wr = next;
2403         }
2404
2405         return ret;
2406 }
2407
2408 static int ib_uverbs_create_ah(struct uverbs_attr_bundle *attrs)
2409 {
2410         struct ib_uverbs_create_ah       cmd;
2411         struct ib_uverbs_create_ah_resp  resp;
2412         struct ib_uobject               *uobj;
2413         struct ib_pd                    *pd;
2414         struct ib_ah                    *ah;
2415         struct rdma_ah_attr             attr = {};
2416         int ret;
2417         struct ib_device *ib_dev;
2418
2419         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2420         if (ret)
2421                 return ret;
2422
2423         uobj = uobj_alloc(UVERBS_OBJECT_AH, attrs, &ib_dev);
2424         if (IS_ERR(uobj))
2425                 return PTR_ERR(uobj);
2426
2427         if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2428                 ret = -EINVAL;
2429                 goto err;
2430         }
2431
2432         pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2433         if (!pd) {
2434                 ret = -EINVAL;
2435                 goto err;
2436         }
2437
2438         attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2439         rdma_ah_set_make_grd(&attr, false);
2440         rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2441         rdma_ah_set_sl(&attr, cmd.attr.sl);
2442         rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2443         rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2444         rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2445
2446         if (cmd.attr.is_global) {
2447                 rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2448                                 cmd.attr.grh.sgid_index,
2449                                 cmd.attr.grh.hop_limit,
2450                                 cmd.attr.grh.traffic_class);
2451                 rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2452         } else {
2453                 rdma_ah_set_ah_flags(&attr, 0);
2454         }
2455
2456         ah = rdma_create_user_ah(pd, &attr, &attrs->driver_udata);
2457         if (IS_ERR(ah)) {
2458                 ret = PTR_ERR(ah);
2459                 goto err_put;
2460         }
2461
2462         ah->uobject  = uobj;
2463         uobj->user_handle = cmd.user_handle;
2464         uobj->object = ah;
2465
2466         resp.ah_handle = uobj->id;
2467
2468         ret = uverbs_response(attrs, &resp, sizeof(resp));
2469         if (ret)
2470                 goto err_copy;
2471
2472         uobj_put_obj_read(pd);
2473         rdma_alloc_commit_uobject(uobj, attrs);
2474         return 0;
2475
2476 err_copy:
2477         rdma_destroy_ah_user(ah, RDMA_DESTROY_AH_SLEEPABLE,
2478                              uverbs_get_cleared_udata(attrs));
2479
2480 err_put:
2481         uobj_put_obj_read(pd);
2482
2483 err:
2484         uobj_alloc_abort(uobj, attrs);
2485         return ret;
2486 }
2487
2488 static int ib_uverbs_destroy_ah(struct uverbs_attr_bundle *attrs)
2489 {
2490         struct ib_uverbs_destroy_ah cmd;
2491         int ret;
2492
2493         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2494         if (ret)
2495                 return ret;
2496
2497         return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, attrs);
2498 }
2499
2500 static int ib_uverbs_attach_mcast(struct uverbs_attr_bundle *attrs)
2501 {
2502         struct ib_uverbs_attach_mcast cmd;
2503         struct ib_qp                 *qp;
2504         struct ib_uqp_object         *obj;
2505         struct ib_uverbs_mcast_entry *mcast;
2506         int                           ret;
2507
2508         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2509         if (ret)
2510                 return ret;
2511
2512         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2513         if (!qp)
2514                 return -EINVAL;
2515
2516         obj = qp->uobject;
2517
2518         mutex_lock(&obj->mcast_lock);
2519         list_for_each_entry(mcast, &obj->mcast_list, list)
2520                 if (cmd.mlid == mcast->lid &&
2521                     !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2522                         ret = 0;
2523                         goto out_put;
2524                 }
2525
2526         mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2527         if (!mcast) {
2528                 ret = -ENOMEM;
2529                 goto out_put;
2530         }
2531
2532         mcast->lid = cmd.mlid;
2533         memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2534
2535         ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2536         if (!ret)
2537                 list_add_tail(&mcast->list, &obj->mcast_list);
2538         else
2539                 kfree(mcast);
2540
2541 out_put:
2542         mutex_unlock(&obj->mcast_lock);
2543         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2544                                 UVERBS_LOOKUP_READ);
2545
2546         return ret;
2547 }
2548
2549 static int ib_uverbs_detach_mcast(struct uverbs_attr_bundle *attrs)
2550 {
2551         struct ib_uverbs_detach_mcast cmd;
2552         struct ib_uqp_object         *obj;
2553         struct ib_qp                 *qp;
2554         struct ib_uverbs_mcast_entry *mcast;
2555         int                           ret;
2556         bool                          found = false;
2557
2558         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2559         if (ret)
2560                 return ret;
2561
2562         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2563         if (!qp)
2564                 return -EINVAL;
2565
2566         obj = qp->uobject;
2567         mutex_lock(&obj->mcast_lock);
2568
2569         list_for_each_entry(mcast, &obj->mcast_list, list)
2570                 if (cmd.mlid == mcast->lid &&
2571                     !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2572                         list_del(&mcast->list);
2573                         kfree(mcast);
2574                         found = true;
2575                         break;
2576                 }
2577
2578         if (!found) {
2579                 ret = -EINVAL;
2580                 goto out_put;
2581         }
2582
2583         ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2584
2585 out_put:
2586         mutex_unlock(&obj->mcast_lock);
2587         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2588                                 UVERBS_LOOKUP_READ);
2589         return ret;
2590 }
2591
2592 struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2593 {
2594         struct ib_uflow_resources *resources;
2595
2596         resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2597
2598         if (!resources)
2599                 return NULL;
2600
2601         if (!num_specs)
2602                 goto out;
2603
2604         resources->counters =
2605                 kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2606         resources->collection =
2607                 kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2608
2609         if (!resources->counters || !resources->collection)
2610                 goto err;
2611
2612 out:
2613         resources->max = num_specs;
2614         return resources;
2615
2616 err:
2617         kfree(resources->counters);
2618         kfree(resources);
2619
2620         return NULL;
2621 }
2622 EXPORT_SYMBOL(flow_resources_alloc);
2623
2624 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
2625 {
2626         unsigned int i;
2627
2628         if (!uflow_res)
2629                 return;
2630
2631         for (i = 0; i < uflow_res->collection_num; i++)
2632                 atomic_dec(&uflow_res->collection[i]->usecnt);
2633
2634         for (i = 0; i < uflow_res->counters_num; i++)
2635                 atomic_dec(&uflow_res->counters[i]->usecnt);
2636
2637         kfree(uflow_res->collection);
2638         kfree(uflow_res->counters);
2639         kfree(uflow_res);
2640 }
2641 EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2642
2643 void flow_resources_add(struct ib_uflow_resources *uflow_res,
2644                         enum ib_flow_spec_type type,
2645                         void *ibobj)
2646 {
2647         WARN_ON(uflow_res->num >= uflow_res->max);
2648
2649         switch (type) {
2650         case IB_FLOW_SPEC_ACTION_HANDLE:
2651                 atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2652                 uflow_res->collection[uflow_res->collection_num++] =
2653                         (struct ib_flow_action *)ibobj;
2654                 break;
2655         case IB_FLOW_SPEC_ACTION_COUNT:
2656                 atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2657                 uflow_res->counters[uflow_res->counters_num++] =
2658                         (struct ib_counters *)ibobj;
2659                 break;
2660         default:
2661                 WARN_ON(1);
2662         }
2663
2664         uflow_res->num++;
2665 }
2666 EXPORT_SYMBOL(flow_resources_add);
2667
2668 static int kern_spec_to_ib_spec_action(struct uverbs_attr_bundle *attrs,
2669                                        struct ib_uverbs_flow_spec *kern_spec,
2670                                        union ib_flow_spec *ib_spec,
2671                                        struct ib_uflow_resources *uflow_res)
2672 {
2673         ib_spec->type = kern_spec->type;
2674         switch (ib_spec->type) {
2675         case IB_FLOW_SPEC_ACTION_TAG:
2676                 if (kern_spec->flow_tag.size !=
2677                     sizeof(struct ib_uverbs_flow_spec_action_tag))
2678                         return -EINVAL;
2679
2680                 ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2681                 ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2682                 break;
2683         case IB_FLOW_SPEC_ACTION_DROP:
2684                 if (kern_spec->drop.size !=
2685                     sizeof(struct ib_uverbs_flow_spec_action_drop))
2686                         return -EINVAL;
2687
2688                 ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2689                 break;
2690         case IB_FLOW_SPEC_ACTION_HANDLE:
2691                 if (kern_spec->action.size !=
2692                     sizeof(struct ib_uverbs_flow_spec_action_handle))
2693                         return -EOPNOTSUPP;
2694                 ib_spec->action.act = uobj_get_obj_read(flow_action,
2695                                                         UVERBS_OBJECT_FLOW_ACTION,
2696                                                         kern_spec->action.handle,
2697                                                         attrs);
2698                 if (!ib_spec->action.act)
2699                         return -EINVAL;
2700                 ib_spec->action.size =
2701                         sizeof(struct ib_flow_spec_action_handle);
2702                 flow_resources_add(uflow_res,
2703                                    IB_FLOW_SPEC_ACTION_HANDLE,
2704                                    ib_spec->action.act);
2705                 uobj_put_obj_read(ib_spec->action.act);
2706                 break;
2707         case IB_FLOW_SPEC_ACTION_COUNT:
2708                 if (kern_spec->flow_count.size !=
2709                         sizeof(struct ib_uverbs_flow_spec_action_count))
2710                         return -EINVAL;
2711                 ib_spec->flow_count.counters =
2712                         uobj_get_obj_read(counters,
2713                                           UVERBS_OBJECT_COUNTERS,
2714                                           kern_spec->flow_count.handle,
2715                                           attrs);
2716                 if (!ib_spec->flow_count.counters)
2717                         return -EINVAL;
2718                 ib_spec->flow_count.size =
2719                                 sizeof(struct ib_flow_spec_action_count);
2720                 flow_resources_add(uflow_res,
2721                                    IB_FLOW_SPEC_ACTION_COUNT,
2722                                    ib_spec->flow_count.counters);
2723                 uobj_put_obj_read(ib_spec->flow_count.counters);
2724                 break;
2725         default:
2726                 return -EINVAL;
2727         }
2728         return 0;
2729 }
2730
2731 static size_t kern_spec_filter_sz(const struct ib_uverbs_flow_spec_hdr *spec)
2732 {
2733         /* Returns user space filter size, includes padding */
2734         return (spec->size - sizeof(struct ib_uverbs_flow_spec_hdr)) / 2;
2735 }
2736
2737 static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2738                                 u16 ib_real_filter_sz)
2739 {
2740         /*
2741          * User space filter structures must be 64 bit aligned, otherwise this
2742          * may pass, but we won't handle additional new attributes.
2743          */
2744
2745         if (kern_filter_size > ib_real_filter_sz) {
2746                 if (memchr_inv(kern_spec_filter +
2747                                ib_real_filter_sz, 0,
2748                                kern_filter_size - ib_real_filter_sz))
2749                         return -EINVAL;
2750                 return ib_real_filter_sz;
2751         }
2752         return kern_filter_size;
2753 }
2754
2755 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2756                                           const void *kern_spec_mask,
2757                                           const void *kern_spec_val,
2758                                           size_t kern_filter_sz,
2759                                           union ib_flow_spec *ib_spec)
2760 {
2761         ssize_t actual_filter_sz;
2762         ssize_t ib_filter_sz;
2763
2764         /* User flow spec size must be aligned to 4 bytes */
2765         if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2766                 return -EINVAL;
2767
2768         ib_spec->type = type;
2769
2770         if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2771                 return -EINVAL;
2772
2773         switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2774         case IB_FLOW_SPEC_ETH:
2775                 ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
2776                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2777                                                     kern_filter_sz,
2778                                                     ib_filter_sz);
2779                 if (actual_filter_sz <= 0)
2780                         return -EINVAL;
2781                 ib_spec->size = sizeof(struct ib_flow_spec_eth);
2782                 memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2783                 memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2784                 break;
2785         case IB_FLOW_SPEC_IPV4:
2786                 ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
2787                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2788                                                     kern_filter_sz,
2789                                                     ib_filter_sz);
2790                 if (actual_filter_sz <= 0)
2791                         return -EINVAL;
2792                 ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2793                 memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2794                 memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2795                 break;
2796         case IB_FLOW_SPEC_IPV6:
2797                 ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
2798                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2799                                                     kern_filter_sz,
2800                                                     ib_filter_sz);
2801                 if (actual_filter_sz <= 0)
2802                         return -EINVAL;
2803                 ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2804                 memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2805                 memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2806
2807                 if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2808                     (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2809                         return -EINVAL;
2810                 break;
2811         case IB_FLOW_SPEC_TCP:
2812         case IB_FLOW_SPEC_UDP:
2813                 ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
2814                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2815                                                     kern_filter_sz,
2816                                                     ib_filter_sz);
2817                 if (actual_filter_sz <= 0)
2818                         return -EINVAL;
2819                 ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2820                 memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2821                 memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2822                 break;
2823         case IB_FLOW_SPEC_VXLAN_TUNNEL:
2824                 ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
2825                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2826                                                     kern_filter_sz,
2827                                                     ib_filter_sz);
2828                 if (actual_filter_sz <= 0)
2829                         return -EINVAL;
2830                 ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
2831                 memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
2832                 memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
2833
2834                 if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
2835                     (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
2836                         return -EINVAL;
2837                 break;
2838         case IB_FLOW_SPEC_ESP:
2839                 ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
2840                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2841                                                     kern_filter_sz,
2842                                                     ib_filter_sz);
2843                 if (actual_filter_sz <= 0)
2844                         return -EINVAL;
2845                 ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
2846                 memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
2847                 memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
2848                 break;
2849         case IB_FLOW_SPEC_GRE:
2850                 ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
2851                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2852                                                     kern_filter_sz,
2853                                                     ib_filter_sz);
2854                 if (actual_filter_sz <= 0)
2855                         return -EINVAL;
2856                 ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
2857                 memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
2858                 memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
2859                 break;
2860         case IB_FLOW_SPEC_MPLS:
2861                 ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
2862                 actual_filter_sz = spec_filter_size(kern_spec_mask,
2863                                                     kern_filter_sz,
2864                                                     ib_filter_sz);
2865                 if (actual_filter_sz <= 0)
2866                         return -EINVAL;
2867                 ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
2868                 memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
2869                 memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
2870                 break;
2871         default:
2872                 return -EINVAL;
2873         }
2874         return 0;
2875 }
2876
2877 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
2878                                        union ib_flow_spec *ib_spec)
2879 {
2880         ssize_t kern_filter_sz;
2881         void *kern_spec_mask;
2882         void *kern_spec_val;
2883
2884         kern_filter_sz = kern_spec_filter_sz(&kern_spec->hdr);
2885
2886         kern_spec_val = (void *)kern_spec +
2887                 sizeof(struct ib_uverbs_flow_spec_hdr);
2888         kern_spec_mask = kern_spec_val + kern_filter_sz;
2889
2890         return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
2891                                                      kern_spec_mask,
2892                                                      kern_spec_val,
2893                                                      kern_filter_sz, ib_spec);
2894 }
2895
2896 static int kern_spec_to_ib_spec(struct uverbs_attr_bundle *attrs,
2897                                 struct ib_uverbs_flow_spec *kern_spec,
2898                                 union ib_flow_spec *ib_spec,
2899                                 struct ib_uflow_resources *uflow_res)
2900 {
2901         if (kern_spec->reserved)
2902                 return -EINVAL;
2903
2904         if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
2905                 return kern_spec_to_ib_spec_action(attrs, kern_spec, ib_spec,
2906                                                    uflow_res);
2907         else
2908                 return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
2909 }
2910
2911 static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle *attrs)
2912 {
2913         struct ib_uverbs_ex_create_wq cmd;
2914         struct ib_uverbs_ex_create_wq_resp resp = {};
2915         struct ib_uwq_object           *obj;
2916         int err = 0;
2917         struct ib_cq *cq;
2918         struct ib_pd *pd;
2919         struct ib_wq *wq;
2920         struct ib_wq_init_attr wq_init_attr = {};
2921         struct ib_device *ib_dev;
2922
2923         err = uverbs_request(attrs, &cmd, sizeof(cmd));
2924         if (err)
2925                 return err;
2926
2927         if (cmd.comp_mask)
2928                 return -EOPNOTSUPP;
2929
2930         obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, attrs,
2931                                                  &ib_dev);
2932         if (IS_ERR(obj))
2933                 return PTR_ERR(obj);
2934
2935         pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2936         if (!pd) {
2937                 err = -EINVAL;
2938                 goto err_uobj;
2939         }
2940
2941         cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
2942         if (!cq) {
2943                 err = -EINVAL;
2944                 goto err_put_pd;
2945         }
2946
2947         wq_init_attr.cq = cq;
2948         wq_init_attr.max_sge = cmd.max_sge;
2949         wq_init_attr.max_wr = cmd.max_wr;
2950         wq_init_attr.wq_context = attrs->ufile;
2951         wq_init_attr.wq_type = cmd.wq_type;
2952         wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
2953         wq_init_attr.create_flags = cmd.create_flags;
2954         INIT_LIST_HEAD(&obj->uevent.event_list);
2955
2956         wq = pd->device->ops.create_wq(pd, &wq_init_attr, &attrs->driver_udata);
2957         if (IS_ERR(wq)) {
2958                 err = PTR_ERR(wq);
2959                 goto err_put_cq;
2960         }
2961
2962         wq->uobject = obj;
2963         obj->uevent.uobject.object = wq;
2964         wq->wq_type = wq_init_attr.wq_type;
2965         wq->cq = cq;
2966         wq->pd = pd;
2967         wq->device = pd->device;
2968         wq->wq_context = wq_init_attr.wq_context;
2969         atomic_set(&wq->usecnt, 0);
2970         atomic_inc(&pd->usecnt);
2971         atomic_inc(&cq->usecnt);
2972         wq->uobject = obj;
2973         obj->uevent.uobject.object = wq;
2974
2975         memset(&resp, 0, sizeof(resp));
2976         resp.wq_handle = obj->uevent.uobject.id;
2977         resp.max_sge = wq_init_attr.max_sge;
2978         resp.max_wr = wq_init_attr.max_wr;
2979         resp.wqn = wq->wq_num;
2980         resp.response_length = uverbs_response_length(attrs, sizeof(resp));
2981         err = uverbs_response(attrs, &resp, sizeof(resp));
2982         if (err)
2983                 goto err_copy;
2984
2985         uobj_put_obj_read(pd);
2986         rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2987                                 UVERBS_LOOKUP_READ);
2988         rdma_alloc_commit_uobject(&obj->uevent.uobject, attrs);
2989         return 0;
2990
2991 err_copy:
2992         ib_destroy_wq(wq, uverbs_get_cleared_udata(attrs));
2993 err_put_cq:
2994         rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2995                                 UVERBS_LOOKUP_READ);
2996 err_put_pd:
2997         uobj_put_obj_read(pd);
2998 err_uobj:
2999         uobj_alloc_abort(&obj->uevent.uobject, attrs);
3000
3001         return err;
3002 }
3003
3004 static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle *attrs)
3005 {
3006         struct ib_uverbs_ex_destroy_wq  cmd;
3007         struct ib_uverbs_ex_destroy_wq_resp     resp = {};
3008         struct ib_uobject               *uobj;
3009         struct ib_uwq_object            *obj;
3010         int                             ret;
3011
3012         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3013         if (ret)
3014                 return ret;
3015
3016         if (cmd.comp_mask)
3017                 return -EOPNOTSUPP;
3018
3019         resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3020         uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3021         if (IS_ERR(uobj))
3022                 return PTR_ERR(uobj);
3023
3024         obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3025         resp.events_reported = obj->uevent.events_reported;
3026
3027         uobj_put_destroy(uobj);
3028
3029         return uverbs_response(attrs, &resp, sizeof(resp));
3030 }
3031
3032 static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle *attrs)
3033 {
3034         struct ib_uverbs_ex_modify_wq cmd;
3035         struct ib_wq *wq;
3036         struct ib_wq_attr wq_attr = {};
3037         int ret;
3038
3039         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3040         if (ret)
3041                 return ret;
3042
3043         if (!cmd.attr_mask)
3044                 return -EINVAL;
3045
3046         if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3047                 return -EINVAL;
3048
3049         wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3050         if (!wq)
3051                 return -EINVAL;
3052
3053         wq_attr.curr_wq_state = cmd.curr_wq_state;
3054         wq_attr.wq_state = cmd.wq_state;
3055         if (cmd.attr_mask & IB_WQ_FLAGS) {
3056                 wq_attr.flags = cmd.flags;
3057                 wq_attr.flags_mask = cmd.flags_mask;
3058         }
3059         ret = wq->device->ops.modify_wq(wq, &wq_attr, cmd.attr_mask,
3060                                         &attrs->driver_udata);
3061         rdma_lookup_put_uobject(&wq->uobject->uevent.uobject,
3062                                 UVERBS_LOOKUP_READ);
3063         return ret;
3064 }
3065
3066 static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3067 {
3068         struct ib_uverbs_ex_create_rwq_ind_table cmd;
3069         struct ib_uverbs_ex_create_rwq_ind_table_resp  resp = {};
3070         struct ib_uobject                 *uobj;
3071         int err;
3072         struct ib_rwq_ind_table_init_attr init_attr = {};
3073         struct ib_rwq_ind_table *rwq_ind_tbl;
3074         struct ib_wq    **wqs = NULL;
3075         u32 *wqs_handles = NULL;
3076         struct ib_wq    *wq = NULL;
3077         int i, j, num_read_wqs;
3078         u32 num_wq_handles;
3079         struct uverbs_req_iter iter;
3080         struct ib_device *ib_dev;
3081
3082         err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3083         if (err)
3084                 return err;
3085
3086         if (cmd.comp_mask)
3087                 return -EOPNOTSUPP;
3088
3089         if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3090                 return -EINVAL;
3091
3092         num_wq_handles = 1 << cmd.log_ind_tbl_size;
3093         wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3094                               GFP_KERNEL);
3095         if (!wqs_handles)
3096                 return -ENOMEM;
3097
3098         err = uverbs_request_next(&iter, wqs_handles,
3099                                   num_wq_handles * sizeof(__u32));
3100         if (err)
3101                 goto err_free;
3102
3103         err = uverbs_request_finish(&iter);
3104         if (err)
3105                 goto err_free;
3106
3107         wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3108         if (!wqs) {
3109                 err = -ENOMEM;
3110                 goto  err_free;
3111         }
3112
3113         for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3114                         num_read_wqs++) {
3115                 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3116                                        wqs_handles[num_read_wqs], attrs);
3117                 if (!wq) {
3118                         err = -EINVAL;
3119                         goto put_wqs;
3120                 }
3121
3122                 wqs[num_read_wqs] = wq;
3123         }
3124
3125         uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, attrs, &ib_dev);
3126         if (IS_ERR(uobj)) {
3127                 err = PTR_ERR(uobj);
3128                 goto put_wqs;
3129         }
3130
3131         init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3132         init_attr.ind_tbl = wqs;
3133
3134         rwq_ind_tbl = ib_dev->ops.create_rwq_ind_table(ib_dev, &init_attr,
3135                                                        &attrs->driver_udata);
3136
3137         if (IS_ERR(rwq_ind_tbl)) {
3138                 err = PTR_ERR(rwq_ind_tbl);
3139                 goto err_uobj;
3140         }
3141
3142         rwq_ind_tbl->ind_tbl = wqs;
3143         rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3144         rwq_ind_tbl->uobject = uobj;
3145         uobj->object = rwq_ind_tbl;
3146         rwq_ind_tbl->device = ib_dev;
3147         atomic_set(&rwq_ind_tbl->usecnt, 0);
3148
3149         for (i = 0; i < num_wq_handles; i++)
3150                 atomic_inc(&wqs[i]->usecnt);
3151
3152         resp.ind_tbl_handle = uobj->id;
3153         resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3154         resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3155
3156         err = uverbs_response(attrs, &resp, sizeof(resp));
3157         if (err)
3158                 goto err_copy;
3159
3160         kfree(wqs_handles);
3161
3162         for (j = 0; j < num_read_wqs; j++)
3163                 rdma_lookup_put_uobject(&wqs[j]->uobject->uevent.uobject,
3164                                         UVERBS_LOOKUP_READ);
3165
3166         rdma_alloc_commit_uobject(uobj, attrs);
3167         return 0;
3168
3169 err_copy:
3170         ib_destroy_rwq_ind_table(rwq_ind_tbl);
3171 err_uobj:
3172         uobj_alloc_abort(uobj, attrs);
3173 put_wqs:
3174         for (j = 0; j < num_read_wqs; j++)
3175                 rdma_lookup_put_uobject(&wqs[j]->uobject->uevent.uobject,
3176                                         UVERBS_LOOKUP_READ);
3177 err_free:
3178         kfree(wqs_handles);
3179         kfree(wqs);
3180         return err;
3181 }
3182
3183 static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3184 {
3185         struct ib_uverbs_ex_destroy_rwq_ind_table cmd;
3186         int ret;
3187
3188         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3189         if (ret)
3190                 return ret;
3191
3192         if (cmd.comp_mask)
3193                 return -EOPNOTSUPP;
3194
3195         return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3196                                     cmd.ind_tbl_handle, attrs);
3197 }
3198
3199 static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle *attrs)
3200 {
3201         struct ib_uverbs_create_flow      cmd;
3202         struct ib_uverbs_create_flow_resp resp;
3203         struct ib_uobject                 *uobj;
3204         struct ib_flow                    *flow_id;
3205         struct ib_uverbs_flow_attr        *kern_flow_attr;
3206         struct ib_flow_attr               *flow_attr;
3207         struct ib_qp                      *qp;
3208         struct ib_uflow_resources         *uflow_res;
3209         struct ib_uverbs_flow_spec_hdr    *kern_spec;
3210         struct uverbs_req_iter iter;
3211         int err;
3212         void *ib_spec;
3213         int i;
3214         struct ib_device *ib_dev;
3215
3216         err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3217         if (err)
3218                 return err;
3219
3220         if (cmd.comp_mask)
3221                 return -EINVAL;
3222
3223         if (!capable(CAP_NET_RAW))
3224                 return -EPERM;
3225
3226         if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3227                 return -EINVAL;
3228
3229         if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3230             ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3231              (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3232                 return -EINVAL;
3233
3234         if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3235                 return -EINVAL;
3236
3237         if (cmd.flow_attr.size >
3238             (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3239                 return -EINVAL;
3240
3241         if (cmd.flow_attr.reserved[0] ||
3242             cmd.flow_attr.reserved[1])
3243                 return -EINVAL;
3244
3245         if (cmd.flow_attr.num_of_specs) {
3246                 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3247                                          GFP_KERNEL);
3248                 if (!kern_flow_attr)
3249                         return -ENOMEM;
3250
3251                 *kern_flow_attr = cmd.flow_attr;
3252                 err = uverbs_request_next(&iter, &kern_flow_attr->flow_specs,
3253                                           cmd.flow_attr.size);
3254                 if (err)
3255                         goto err_free_attr;
3256         } else {
3257                 kern_flow_attr = &cmd.flow_attr;
3258         }
3259
3260         err = uverbs_request_finish(&iter);
3261         if (err)
3262                 goto err_free_attr;
3263
3264         uobj = uobj_alloc(UVERBS_OBJECT_FLOW, attrs, &ib_dev);
3265         if (IS_ERR(uobj)) {
3266                 err = PTR_ERR(uobj);
3267                 goto err_free_attr;
3268         }
3269
3270         qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
3271         if (!qp) {
3272                 err = -EINVAL;
3273                 goto err_uobj;
3274         }
3275
3276         if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3277                 err = -EINVAL;
3278                 goto err_put;
3279         }
3280
3281         flow_attr = kzalloc(struct_size(flow_attr, flows,
3282                                 cmd.flow_attr.num_of_specs), GFP_KERNEL);
3283         if (!flow_attr) {
3284                 err = -ENOMEM;
3285                 goto err_put;
3286         }
3287         uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3288         if (!uflow_res) {
3289                 err = -ENOMEM;
3290                 goto err_free_flow_attr;
3291         }
3292
3293         flow_attr->type = kern_flow_attr->type;
3294         flow_attr->priority = kern_flow_attr->priority;
3295         flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3296         flow_attr->port = kern_flow_attr->port;
3297         flow_attr->flags = kern_flow_attr->flags;
3298         flow_attr->size = sizeof(*flow_attr);
3299
3300         kern_spec = kern_flow_attr->flow_specs;
3301         ib_spec = flow_attr + 1;
3302         for (i = 0; i < flow_attr->num_of_specs &&
3303                         cmd.flow_attr.size >= sizeof(*kern_spec) &&
3304                         cmd.flow_attr.size >= kern_spec->size;
3305              i++) {
3306                 err = kern_spec_to_ib_spec(
3307                                 attrs, (struct ib_uverbs_flow_spec *)kern_spec,
3308                                 ib_spec, uflow_res);
3309                 if (err)
3310                         goto err_free;
3311
3312                 flow_attr->size +=
3313                         ((union ib_flow_spec *) ib_spec)->size;
3314                 cmd.flow_attr.size -= kern_spec->size;
3315                 kern_spec = ((void *)kern_spec) + kern_spec->size;
3316                 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3317         }
3318         if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3319                 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3320                         i, cmd.flow_attr.size);
3321                 err = -EINVAL;
3322                 goto err_free;
3323         }
3324
3325         flow_id = qp->device->ops.create_flow(
3326                 qp, flow_attr, IB_FLOW_DOMAIN_USER, &attrs->driver_udata);
3327
3328         if (IS_ERR(flow_id)) {
3329                 err = PTR_ERR(flow_id);
3330                 goto err_free;
3331         }
3332
3333         ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3334
3335         memset(&resp, 0, sizeof(resp));
3336         resp.flow_handle = uobj->id;
3337
3338         err = uverbs_response(attrs, &resp, sizeof(resp));
3339         if (err)
3340                 goto err_copy;
3341
3342         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3343                                 UVERBS_LOOKUP_READ);
3344         kfree(flow_attr);
3345         if (cmd.flow_attr.num_of_specs)
3346                 kfree(kern_flow_attr);
3347         rdma_alloc_commit_uobject(uobj, attrs);
3348         return 0;
3349 err_copy:
3350         if (!qp->device->ops.destroy_flow(flow_id))
3351                 atomic_dec(&qp->usecnt);
3352 err_free:
3353         ib_uverbs_flow_resources_free(uflow_res);
3354 err_free_flow_attr:
3355         kfree(flow_attr);
3356 err_put:
3357         rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3358                                 UVERBS_LOOKUP_READ);
3359 err_uobj:
3360         uobj_alloc_abort(uobj, attrs);
3361 err_free_attr:
3362         if (cmd.flow_attr.num_of_specs)
3363                 kfree(kern_flow_attr);
3364         return err;
3365 }
3366
3367 static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle *attrs)
3368 {
3369         struct ib_uverbs_destroy_flow   cmd;
3370         int                             ret;
3371
3372         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3373         if (ret)
3374                 return ret;
3375
3376         if (cmd.comp_mask)
3377                 return -EINVAL;
3378
3379         return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, attrs);
3380 }
3381
3382 static int __uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3383                                 struct ib_uverbs_create_xsrq *cmd,
3384                                 struct ib_udata *udata)
3385 {
3386         struct ib_uverbs_create_srq_resp resp;
3387         struct ib_usrq_object           *obj;
3388         struct ib_pd                    *pd;
3389         struct ib_srq                   *srq;
3390         struct ib_uobject               *uninitialized_var(xrcd_uobj);
3391         struct ib_srq_init_attr          attr;
3392         int ret;
3393         struct ib_device *ib_dev;
3394
3395         obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, attrs,
3396                                                   &ib_dev);
3397         if (IS_ERR(obj))
3398                 return PTR_ERR(obj);
3399
3400         if (cmd->srq_type == IB_SRQT_TM)
3401                 attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3402
3403         if (cmd->srq_type == IB_SRQT_XRC) {
3404                 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3405                                           attrs);
3406                 if (IS_ERR(xrcd_uobj)) {
3407                         ret = -EINVAL;
3408                         goto err;
3409                 }
3410
3411                 attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3412                 if (!attr.ext.xrc.xrcd) {
3413                         ret = -EINVAL;
3414                         goto err_put_xrcd;
3415                 }
3416
3417                 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3418                 atomic_inc(&obj->uxrcd->refcnt);
3419         }
3420
3421         if (ib_srq_has_cq(cmd->srq_type)) {
3422                 attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3423                                                 cmd->cq_handle, attrs);
3424                 if (!attr.ext.cq) {
3425                         ret = -EINVAL;
3426                         goto err_put_xrcd;
3427                 }
3428         }
3429
3430         pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, attrs);
3431         if (!pd) {
3432                 ret = -EINVAL;
3433                 goto err_put_cq;
3434         }
3435
3436         attr.event_handler  = ib_uverbs_srq_event_handler;
3437         attr.srq_context    = attrs->ufile;
3438         attr.srq_type       = cmd->srq_type;
3439         attr.attr.max_wr    = cmd->max_wr;
3440         attr.attr.max_sge   = cmd->max_sge;
3441         attr.attr.srq_limit = cmd->srq_limit;
3442
3443         INIT_LIST_HEAD(&obj->uevent.event_list);
3444
3445         srq = rdma_zalloc_drv_obj(ib_dev, ib_srq);
3446         if (!srq) {
3447                 ret = -ENOMEM;
3448                 goto err_put;
3449         }
3450
3451         srq->device        = pd->device;
3452         srq->pd            = pd;
3453         srq->srq_type      = cmd->srq_type;
3454         srq->uobject       = obj;
3455         srq->event_handler = attr.event_handler;
3456         srq->srq_context   = attr.srq_context;
3457
3458         ret = pd->device->ops.create_srq(srq, &attr, udata);
3459         if (ret)
3460                 goto err_free;
3461
3462         if (ib_srq_has_cq(cmd->srq_type)) {
3463                 srq->ext.cq       = attr.ext.cq;
3464                 atomic_inc(&attr.ext.cq->usecnt);
3465         }
3466
3467         if (cmd->srq_type == IB_SRQT_XRC) {
3468                 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
3469                 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
3470         }
3471
3472         atomic_inc(&pd->usecnt);
3473         atomic_set(&srq->usecnt, 0);
3474
3475         obj->uevent.uobject.object = srq;
3476         obj->uevent.uobject.user_handle = cmd->user_handle;
3477
3478         memset(&resp, 0, sizeof resp);
3479         resp.srq_handle = obj->uevent.uobject.id;
3480         resp.max_wr     = attr.attr.max_wr;
3481         resp.max_sge    = attr.attr.max_sge;
3482         if (cmd->srq_type == IB_SRQT_XRC)
3483                 resp.srqn = srq->ext.xrc.srq_num;
3484
3485         ret = uverbs_response(attrs, &resp, sizeof(resp));
3486         if (ret)
3487                 goto err_copy;
3488
3489         if (cmd->srq_type == IB_SRQT_XRC)
3490                 uobj_put_read(xrcd_uobj);
3491
3492         if (ib_srq_has_cq(cmd->srq_type))
3493                 rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3494                                         UVERBS_LOOKUP_READ);
3495
3496         uobj_put_obj_read(pd);
3497         rdma_alloc_commit_uobject(&obj->uevent.uobject, attrs);
3498         return 0;
3499
3500 err_copy:
3501         ib_destroy_srq_user(srq, uverbs_get_cleared_udata(attrs));
3502         /* It was released in ib_destroy_srq_user */
3503         srq = NULL;
3504 err_free:
3505         kfree(srq);
3506 err_put:
3507         uobj_put_obj_read(pd);
3508
3509 err_put_cq:
3510         if (ib_srq_has_cq(cmd->srq_type))
3511                 rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3512                                         UVERBS_LOOKUP_READ);
3513
3514 err_put_xrcd:
3515         if (cmd->srq_type == IB_SRQT_XRC) {
3516                 atomic_dec(&obj->uxrcd->refcnt);
3517                 uobj_put_read(xrcd_uobj);
3518         }
3519
3520 err:
3521         uobj_alloc_abort(&obj->uevent.uobject, attrs);
3522         return ret;
3523 }
3524
3525 static int ib_uverbs_create_srq(struct uverbs_attr_bundle *attrs)
3526 {
3527         struct ib_uverbs_create_srq      cmd;
3528         struct ib_uverbs_create_xsrq     xcmd;
3529         int ret;
3530
3531         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3532         if (ret)
3533                 return ret;
3534
3535         memset(&xcmd, 0, sizeof(xcmd));
3536         xcmd.response    = cmd.response;
3537         xcmd.user_handle = cmd.user_handle;
3538         xcmd.srq_type    = IB_SRQT_BASIC;
3539         xcmd.pd_handle   = cmd.pd_handle;
3540         xcmd.max_wr      = cmd.max_wr;
3541         xcmd.max_sge     = cmd.max_sge;
3542         xcmd.srq_limit   = cmd.srq_limit;
3543
3544         return __uverbs_create_xsrq(attrs, &xcmd, &attrs->driver_udata);
3545 }
3546
3547 static int ib_uverbs_create_xsrq(struct uverbs_attr_bundle *attrs)
3548 {
3549         struct ib_uverbs_create_xsrq     cmd;
3550         int ret;
3551
3552         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3553         if (ret)
3554                 return ret;
3555
3556         return __uverbs_create_xsrq(attrs, &cmd, &attrs->driver_udata);
3557 }
3558
3559 static int ib_uverbs_modify_srq(struct uverbs_attr_bundle *attrs)
3560 {
3561         struct ib_uverbs_modify_srq cmd;
3562         struct ib_srq              *srq;
3563         struct ib_srq_attr          attr;
3564         int                         ret;
3565
3566         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3567         if (ret)
3568                 return ret;
3569
3570         srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3571         if (!srq)
3572                 return -EINVAL;
3573
3574         attr.max_wr    = cmd.max_wr;
3575         attr.srq_limit = cmd.srq_limit;
3576
3577         ret = srq->device->ops.modify_srq(srq, &attr, cmd.attr_mask,
3578                                           &attrs->driver_udata);
3579
3580         rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3581                                 UVERBS_LOOKUP_READ);
3582
3583         return ret;
3584 }
3585
3586 static int ib_uverbs_query_srq(struct uverbs_attr_bundle *attrs)
3587 {
3588         struct ib_uverbs_query_srq      cmd;
3589         struct ib_uverbs_query_srq_resp resp;
3590         struct ib_srq_attr              attr;
3591         struct ib_srq                   *srq;
3592         int                             ret;
3593
3594         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3595         if (ret)
3596                 return ret;
3597
3598         srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3599         if (!srq)
3600                 return -EINVAL;
3601
3602         ret = ib_query_srq(srq, &attr);
3603
3604         rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3605                                 UVERBS_LOOKUP_READ);
3606
3607         if (ret)
3608                 return ret;
3609
3610         memset(&resp, 0, sizeof resp);
3611
3612         resp.max_wr    = attr.max_wr;
3613         resp.max_sge   = attr.max_sge;
3614         resp.srq_limit = attr.srq_limit;
3615
3616         return uverbs_response(attrs, &resp, sizeof(resp));
3617 }
3618
3619 static int ib_uverbs_destroy_srq(struct uverbs_attr_bundle *attrs)
3620 {
3621         struct ib_uverbs_destroy_srq      cmd;
3622         struct ib_uverbs_destroy_srq_resp resp;
3623         struct ib_uobject                *uobj;
3624         struct ib_uevent_object          *obj;
3625         int ret;
3626
3627         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3628         if (ret)
3629                 return ret;
3630
3631         uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3632         if (IS_ERR(uobj))
3633                 return PTR_ERR(uobj);
3634
3635         obj = container_of(uobj, struct ib_uevent_object, uobject);
3636         memset(&resp, 0, sizeof(resp));
3637         resp.events_reported = obj->events_reported;
3638
3639         uobj_put_destroy(uobj);
3640
3641         return uverbs_response(attrs, &resp, sizeof(resp));
3642 }
3643
3644 static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle *attrs)
3645 {
3646         struct ib_uverbs_ex_query_device_resp resp = {};
3647         struct ib_uverbs_ex_query_device  cmd;
3648         struct ib_device_attr attr = {0};
3649         struct ib_ucontext *ucontext;
3650         struct ib_device *ib_dev;
3651         int err;
3652
3653         ucontext = ib_uverbs_get_ucontext(attrs);
3654         if (IS_ERR(ucontext))
3655                 return PTR_ERR(ucontext);
3656         ib_dev = ucontext->device;
3657
3658         err = uverbs_request(attrs, &cmd, sizeof(cmd));
3659         if (err)
3660                 return err;
3661
3662         if (cmd.comp_mask)
3663                 return -EINVAL;
3664
3665         if (cmd.reserved)
3666                 return -EINVAL;
3667
3668         err = ib_dev->ops.query_device(ib_dev, &attr, &attrs->driver_udata);
3669         if (err)
3670                 return err;
3671
3672         copy_query_dev_fields(ucontext, &resp.base, &attr);
3673
3674         resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3675         resp.odp_caps.per_transport_caps.rc_odp_caps =
3676                 attr.odp_caps.per_transport_caps.rc_odp_caps;
3677         resp.odp_caps.per_transport_caps.uc_odp_caps =
3678                 attr.odp_caps.per_transport_caps.uc_odp_caps;
3679         resp.odp_caps.per_transport_caps.ud_odp_caps =
3680                 attr.odp_caps.per_transport_caps.ud_odp_caps;
3681         resp.xrc_odp_caps = attr.odp_caps.per_transport_caps.xrc_odp_caps;
3682
3683         resp.timestamp_mask = attr.timestamp_mask;
3684         resp.hca_core_clock = attr.hca_core_clock;
3685         resp.device_cap_flags_ex = attr.device_cap_flags;
3686         resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
3687         resp.rss_caps.max_rwq_indirection_tables =
3688                 attr.rss_caps.max_rwq_indirection_tables;
3689         resp.rss_caps.max_rwq_indirection_table_size =
3690                 attr.rss_caps.max_rwq_indirection_table_size;
3691         resp.max_wq_type_rq = attr.max_wq_type_rq;
3692         resp.raw_packet_caps = attr.raw_packet_caps;
3693         resp.tm_caps.max_rndv_hdr_size  = attr.tm_caps.max_rndv_hdr_size;
3694         resp.tm_caps.max_num_tags       = attr.tm_caps.max_num_tags;
3695         resp.tm_caps.max_ops            = attr.tm_caps.max_ops;
3696         resp.tm_caps.max_sge            = attr.tm_caps.max_sge;
3697         resp.tm_caps.flags              = attr.tm_caps.flags;
3698         resp.cq_moderation_caps.max_cq_moderation_count  =
3699                 attr.cq_caps.max_cq_moderation_count;
3700         resp.cq_moderation_caps.max_cq_moderation_period =
3701                 attr.cq_caps.max_cq_moderation_period;
3702         resp.max_dm_size = attr.max_dm_size;
3703         resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3704
3705         return uverbs_response(attrs, &resp, sizeof(resp));
3706 }
3707
3708 static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs)
3709 {
3710         struct ib_uverbs_ex_modify_cq cmd;
3711         struct ib_cq *cq;
3712         int ret;
3713
3714         ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3715         if (ret)
3716                 return ret;
3717
3718         if (!cmd.attr_mask || cmd.reserved)
3719                 return -EINVAL;
3720
3721         if (cmd.attr_mask > IB_CQ_MODERATE)
3722                 return -EOPNOTSUPP;
3723
3724         cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3725         if (!cq)
3726                 return -EINVAL;
3727
3728         ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
3729
3730         rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
3731                                 UVERBS_LOOKUP_READ);
3732         return ret;
3733 }
3734
3735 /*
3736  * Describe the input structs for write(). Some write methods have an input
3737  * only struct, most have an input and output. If the struct has an output then
3738  * the 'response' u64 must be the first field in the request structure.
3739  *
3740  * If udata is present then both the request and response structs have a
3741  * trailing driver_data flex array. In this case the size of the base struct
3742  * cannot be changed.
3743  */
3744 #define UAPI_DEF_WRITE_IO(req, resp)                                           \
3745         .write.has_resp = 1 +                                                  \
3746                           BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) +    \
3747                           BUILD_BUG_ON_ZERO(sizeof(((req *)0)->response) !=    \
3748                                             sizeof(u64)),                      \
3749         .write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
3750
3751 #define UAPI_DEF_WRITE_I(req) .write.req_size = sizeof(req)
3752
3753 #define UAPI_DEF_WRITE_UDATA_IO(req, resp)                                     \
3754         UAPI_DEF_WRITE_IO(req, resp),                                          \
3755                 .write.has_udata =                                             \
3756                         1 +                                                    \
3757                         BUILD_BUG_ON_ZERO(offsetof(req, driver_data) !=        \
3758                                           sizeof(req)) +                       \
3759                         BUILD_BUG_ON_ZERO(offsetof(resp, driver_data) !=       \
3760                                           sizeof(resp))
3761
3762 #define UAPI_DEF_WRITE_UDATA_I(req)                                            \
3763         UAPI_DEF_WRITE_I(req),                                                 \
3764                 .write.has_udata =                                             \
3765                         1 + BUILD_BUG_ON_ZERO(offsetof(req, driver_data) !=    \
3766                                               sizeof(req))
3767
3768 /*
3769  * The _EX versions are for use with WRITE_EX and allow the last struct member
3770  * to be specified. Buffers that do not include that member will be rejected.
3771  */
3772 #define UAPI_DEF_WRITE_IO_EX(req, req_last_member, resp, resp_last_member)     \
3773         .write.has_resp = 1,                                                   \
3774         .write.req_size = offsetofend(req, req_last_member),                   \
3775         .write.resp_size = offsetofend(resp, resp_last_member)
3776
3777 #define UAPI_DEF_WRITE_I_EX(req, req_last_member)                              \
3778         .write.req_size = offsetofend(req, req_last_member)
3779
3780 const struct uapi_definition uverbs_def_write_intf[] = {
3781         DECLARE_UVERBS_OBJECT(
3782                 UVERBS_OBJECT_AH,
3783                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH,
3784                                      ib_uverbs_create_ah,
3785                                      UAPI_DEF_WRITE_UDATA_IO(
3786                                              struct ib_uverbs_create_ah,
3787                                              struct ib_uverbs_create_ah_resp),
3788                                      UAPI_DEF_METHOD_NEEDS_FN(create_ah)),
3789                 DECLARE_UVERBS_WRITE(
3790                         IB_USER_VERBS_CMD_DESTROY_AH,
3791                         ib_uverbs_destroy_ah,
3792                         UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_ah),
3793                         UAPI_DEF_METHOD_NEEDS_FN(destroy_ah))),
3794
3795         DECLARE_UVERBS_OBJECT(
3796                 UVERBS_OBJECT_COMP_CHANNEL,
3797                 DECLARE_UVERBS_WRITE(
3798                         IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL,
3799                         ib_uverbs_create_comp_channel,
3800                         UAPI_DEF_WRITE_IO(
3801                                 struct ib_uverbs_create_comp_channel,
3802                                 struct ib_uverbs_create_comp_channel_resp))),
3803
3804         DECLARE_UVERBS_OBJECT(
3805                 UVERBS_OBJECT_CQ,
3806                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ,
3807                                      ib_uverbs_create_cq,
3808                                      UAPI_DEF_WRITE_UDATA_IO(
3809                                              struct ib_uverbs_create_cq,
3810                                              struct ib_uverbs_create_cq_resp),
3811                                      UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3812                 DECLARE_UVERBS_WRITE(
3813                         IB_USER_VERBS_CMD_DESTROY_CQ,
3814                         ib_uverbs_destroy_cq,
3815                         UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_cq,
3816                                           struct ib_uverbs_destroy_cq_resp),
3817                         UAPI_DEF_METHOD_NEEDS_FN(destroy_cq)),
3818                 DECLARE_UVERBS_WRITE(
3819                         IB_USER_VERBS_CMD_POLL_CQ,
3820                         ib_uverbs_poll_cq,
3821                         UAPI_DEF_WRITE_IO(struct ib_uverbs_poll_cq,
3822                                           struct ib_uverbs_poll_cq_resp),
3823                         UAPI_DEF_METHOD_NEEDS_FN(poll_cq)),
3824                 DECLARE_UVERBS_WRITE(
3825                         IB_USER_VERBS_CMD_REQ_NOTIFY_CQ,
3826                         ib_uverbs_req_notify_cq,
3827                         UAPI_DEF_WRITE_I(struct ib_uverbs_req_notify_cq),
3828                         UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq)),
3829                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ,
3830                                      ib_uverbs_resize_cq,
3831                                      UAPI_DEF_WRITE_UDATA_IO(
3832                                              struct ib_uverbs_resize_cq,
3833                                              struct ib_uverbs_resize_cq_resp),
3834                                      UAPI_DEF_METHOD_NEEDS_FN(resize_cq)),
3835                 DECLARE_UVERBS_WRITE_EX(
3836                         IB_USER_VERBS_EX_CMD_CREATE_CQ,
3837                         ib_uverbs_ex_create_cq,
3838                         UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_cq,
3839                                              reserved,
3840                                              struct ib_uverbs_ex_create_cq_resp,
3841                                              response_length),
3842                         UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3843                 DECLARE_UVERBS_WRITE_EX(
3844                         IB_USER_VERBS_EX_CMD_MODIFY_CQ,
3845                         ib_uverbs_ex_modify_cq,
3846                         UAPI_DEF_WRITE_I(struct ib_uverbs_ex_modify_cq),
3847                         UAPI_DEF_METHOD_NEEDS_FN(create_cq))),
3848
3849         DECLARE_UVERBS_OBJECT(
3850                 UVERBS_OBJECT_DEVICE,
3851                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT,
3852                                      ib_uverbs_get_context,
3853                                      UAPI_DEF_WRITE_UDATA_IO(
3854                                              struct ib_uverbs_get_context,
3855                                              struct ib_uverbs_get_context_resp)),
3856                 DECLARE_UVERBS_WRITE(
3857                         IB_USER_VERBS_CMD_QUERY_DEVICE,
3858                         ib_uverbs_query_device,
3859                         UAPI_DEF_WRITE_IO(struct ib_uverbs_query_device,
3860                                           struct ib_uverbs_query_device_resp)),
3861                 DECLARE_UVERBS_WRITE(
3862                         IB_USER_VERBS_CMD_QUERY_PORT,
3863                         ib_uverbs_query_port,
3864                         UAPI_DEF_WRITE_IO(struct ib_uverbs_query_port,
3865                                           struct ib_uverbs_query_port_resp),
3866                         UAPI_DEF_METHOD_NEEDS_FN(query_port)),
3867                 DECLARE_UVERBS_WRITE_EX(
3868                         IB_USER_VERBS_EX_CMD_QUERY_DEVICE,
3869                         ib_uverbs_ex_query_device,
3870                         UAPI_DEF_WRITE_IO_EX(
3871                                 struct ib_uverbs_ex_query_device,
3872                                 reserved,
3873                                 struct ib_uverbs_ex_query_device_resp,
3874                                 response_length),
3875                         UAPI_DEF_METHOD_NEEDS_FN(query_device)),
3876                 UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext),
3877                 UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext)),
3878
3879         DECLARE_UVERBS_OBJECT(
3880                 UVERBS_OBJECT_FLOW,
3881                 DECLARE_UVERBS_WRITE_EX(
3882                         IB_USER_VERBS_EX_CMD_CREATE_FLOW,
3883                         ib_uverbs_ex_create_flow,
3884                         UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_create_flow,
3885                                              flow_attr,
3886                                              struct ib_uverbs_create_flow_resp,
3887                                              flow_handle),
3888                         UAPI_DEF_METHOD_NEEDS_FN(create_flow)),
3889                 DECLARE_UVERBS_WRITE_EX(
3890                         IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
3891                         ib_uverbs_ex_destroy_flow,
3892                         UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_flow),
3893                         UAPI_DEF_METHOD_NEEDS_FN(destroy_flow))),
3894
3895         DECLARE_UVERBS_OBJECT(
3896                 UVERBS_OBJECT_MR,
3897                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR,
3898                                      ib_uverbs_dereg_mr,
3899                                      UAPI_DEF_WRITE_I(struct ib_uverbs_dereg_mr),
3900                                      UAPI_DEF_METHOD_NEEDS_FN(dereg_mr)),
3901                 DECLARE_UVERBS_WRITE(
3902                         IB_USER_VERBS_CMD_REG_MR,
3903                         ib_uverbs_reg_mr,
3904                         UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_reg_mr,
3905                                                 struct ib_uverbs_reg_mr_resp),
3906                         UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr)),
3907                 DECLARE_UVERBS_WRITE(
3908                         IB_USER_VERBS_CMD_REREG_MR,
3909                         ib_uverbs_rereg_mr,
3910                         UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_rereg_mr,
3911                                                 struct ib_uverbs_rereg_mr_resp),
3912                         UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr))),
3913
3914         DECLARE_UVERBS_OBJECT(
3915                 UVERBS_OBJECT_MW,
3916                 DECLARE_UVERBS_WRITE(
3917                         IB_USER_VERBS_CMD_ALLOC_MW,
3918                         ib_uverbs_alloc_mw,
3919                         UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_mw,
3920                                                 struct ib_uverbs_alloc_mw_resp),
3921                         UAPI_DEF_METHOD_NEEDS_FN(alloc_mw)),
3922                 DECLARE_UVERBS_WRITE(
3923                         IB_USER_VERBS_CMD_DEALLOC_MW,
3924                         ib_uverbs_dealloc_mw,
3925                         UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_mw),
3926                         UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw))),
3927
3928         DECLARE_UVERBS_OBJECT(
3929                 UVERBS_OBJECT_PD,
3930                 DECLARE_UVERBS_WRITE(
3931                         IB_USER_VERBS_CMD_ALLOC_PD,
3932                         ib_uverbs_alloc_pd,
3933                         UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_pd,
3934                                                 struct ib_uverbs_alloc_pd_resp),
3935                         UAPI_DEF_METHOD_NEEDS_FN(alloc_pd)),
3936                 DECLARE_UVERBS_WRITE(
3937                         IB_USER_VERBS_CMD_DEALLOC_PD,
3938                         ib_uverbs_dealloc_pd,
3939                         UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_pd),
3940                         UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd))),
3941
3942         DECLARE_UVERBS_OBJECT(
3943                 UVERBS_OBJECT_QP,
3944                 DECLARE_UVERBS_WRITE(
3945                         IB_USER_VERBS_CMD_ATTACH_MCAST,
3946                         ib_uverbs_attach_mcast,
3947                         UAPI_DEF_WRITE_I(struct ib_uverbs_attach_mcast),
3948                         UAPI_DEF_METHOD_NEEDS_FN(attach_mcast),
3949                         UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3950                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP,
3951                                      ib_uverbs_create_qp,
3952                                      UAPI_DEF_WRITE_UDATA_IO(
3953                                              struct ib_uverbs_create_qp,
3954                                              struct ib_uverbs_create_qp_resp),
3955                                      UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3956                 DECLARE_UVERBS_WRITE(
3957                         IB_USER_VERBS_CMD_DESTROY_QP,
3958                         ib_uverbs_destroy_qp,
3959                         UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_qp,
3960                                           struct ib_uverbs_destroy_qp_resp),
3961                         UAPI_DEF_METHOD_NEEDS_FN(destroy_qp)),
3962                 DECLARE_UVERBS_WRITE(
3963                         IB_USER_VERBS_CMD_DETACH_MCAST,
3964                         ib_uverbs_detach_mcast,
3965                         UAPI_DEF_WRITE_I(struct ib_uverbs_detach_mcast),
3966                         UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3967                 DECLARE_UVERBS_WRITE(
3968                         IB_USER_VERBS_CMD_MODIFY_QP,
3969                         ib_uverbs_modify_qp,
3970                         UAPI_DEF_WRITE_I(struct ib_uverbs_modify_qp),
3971                         UAPI_DEF_METHOD_NEEDS_FN(modify_qp)),
3972                 DECLARE_UVERBS_WRITE(
3973                         IB_USER_VERBS_CMD_POST_RECV,
3974                         ib_uverbs_post_recv,
3975                         UAPI_DEF_WRITE_IO(struct ib_uverbs_post_recv,
3976                                           struct ib_uverbs_post_recv_resp),
3977                         UAPI_DEF_METHOD_NEEDS_FN(post_recv)),
3978                 DECLARE_UVERBS_WRITE(
3979                         IB_USER_VERBS_CMD_POST_SEND,
3980                         ib_uverbs_post_send,
3981                         UAPI_DEF_WRITE_IO(struct ib_uverbs_post_send,
3982                                           struct ib_uverbs_post_send_resp),
3983                         UAPI_DEF_METHOD_NEEDS_FN(post_send)),
3984                 DECLARE_UVERBS_WRITE(
3985                         IB_USER_VERBS_CMD_QUERY_QP,
3986                         ib_uverbs_query_qp,
3987                         UAPI_DEF_WRITE_IO(struct ib_uverbs_query_qp,
3988                                           struct ib_uverbs_query_qp_resp),
3989                         UAPI_DEF_METHOD_NEEDS_FN(query_qp)),
3990                 DECLARE_UVERBS_WRITE_EX(
3991                         IB_USER_VERBS_EX_CMD_CREATE_QP,
3992                         ib_uverbs_ex_create_qp,
3993                         UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_qp,
3994                                              comp_mask,
3995                                              struct ib_uverbs_ex_create_qp_resp,
3996                                              response_length),
3997                         UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3998                 DECLARE_UVERBS_WRITE_EX(
3999                         IB_USER_VERBS_EX_CMD_MODIFY_QP,
4000                         ib_uverbs_ex_modify_qp,
4001                         UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_modify_qp,
4002                                              base,
4003                                              struct ib_uverbs_ex_modify_qp_resp,
4004                                              response_length),
4005                         UAPI_DEF_METHOD_NEEDS_FN(modify_qp))),
4006
4007         DECLARE_UVERBS_OBJECT(
4008                 UVERBS_OBJECT_RWQ_IND_TBL,
4009                 DECLARE_UVERBS_WRITE_EX(
4010                         IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL,
4011                         ib_uverbs_ex_create_rwq_ind_table,
4012                         UAPI_DEF_WRITE_IO_EX(
4013                                 struct ib_uverbs_ex_create_rwq_ind_table,
4014                                 log_ind_tbl_size,
4015                                 struct ib_uverbs_ex_create_rwq_ind_table_resp,
4016                                 ind_tbl_num),
4017                         UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table)),
4018                 DECLARE_UVERBS_WRITE_EX(
4019                         IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL,
4020                         ib_uverbs_ex_destroy_rwq_ind_table,
4021                         UAPI_DEF_WRITE_I(
4022                                 struct ib_uverbs_ex_destroy_rwq_ind_table),
4023                         UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table))),
4024
4025         DECLARE_UVERBS_OBJECT(
4026                 UVERBS_OBJECT_WQ,
4027                 DECLARE_UVERBS_WRITE_EX(
4028                         IB_USER_VERBS_EX_CMD_CREATE_WQ,
4029                         ib_uverbs_ex_create_wq,
4030                         UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_wq,
4031                                              max_sge,
4032                                              struct ib_uverbs_ex_create_wq_resp,
4033                                              wqn),
4034                         UAPI_DEF_METHOD_NEEDS_FN(create_wq)),
4035                 DECLARE_UVERBS_WRITE_EX(
4036                         IB_USER_VERBS_EX_CMD_DESTROY_WQ,
4037                         ib_uverbs_ex_destroy_wq,
4038                         UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_destroy_wq,
4039                                              wq_handle,
4040                                              struct ib_uverbs_ex_destroy_wq_resp,
4041                                              reserved),
4042                         UAPI_DEF_METHOD_NEEDS_FN(destroy_wq)),
4043                 DECLARE_UVERBS_WRITE_EX(
4044                         IB_USER_VERBS_EX_CMD_MODIFY_WQ,
4045                         ib_uverbs_ex_modify_wq,
4046                         UAPI_DEF_WRITE_I_EX(struct ib_uverbs_ex_modify_wq,
4047                                             curr_wq_state),
4048                         UAPI_DEF_METHOD_NEEDS_FN(modify_wq))),
4049
4050         DECLARE_UVERBS_OBJECT(
4051                 UVERBS_OBJECT_SRQ,
4052                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ,
4053                                      ib_uverbs_create_srq,
4054                                      UAPI_DEF_WRITE_UDATA_IO(
4055                                              struct ib_uverbs_create_srq,
4056                                              struct ib_uverbs_create_srq_resp),
4057                                      UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4058                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ,
4059                                      ib_uverbs_create_xsrq,
4060                                      UAPI_DEF_WRITE_UDATA_IO(
4061                                              struct ib_uverbs_create_xsrq,
4062                                              struct ib_uverbs_create_srq_resp),
4063                                      UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4064                 DECLARE_UVERBS_WRITE(
4065                         IB_USER_VERBS_CMD_DESTROY_SRQ,
4066                         ib_uverbs_destroy_srq,
4067                         UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_srq,
4068                                           struct ib_uverbs_destroy_srq_resp),
4069                         UAPI_DEF_METHOD_NEEDS_FN(destroy_srq)),
4070                 DECLARE_UVERBS_WRITE(
4071                         IB_USER_VERBS_CMD_MODIFY_SRQ,
4072                         ib_uverbs_modify_srq,
4073                         UAPI_DEF_WRITE_UDATA_I(struct ib_uverbs_modify_srq),
4074                         UAPI_DEF_METHOD_NEEDS_FN(modify_srq)),
4075                 DECLARE_UVERBS_WRITE(
4076                         IB_USER_VERBS_CMD_POST_SRQ_RECV,
4077                         ib_uverbs_post_srq_recv,
4078                         UAPI_DEF_WRITE_IO(struct ib_uverbs_post_srq_recv,
4079                                           struct ib_uverbs_post_srq_recv_resp),
4080                         UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv)),
4081                 DECLARE_UVERBS_WRITE(
4082                         IB_USER_VERBS_CMD_QUERY_SRQ,
4083                         ib_uverbs_query_srq,
4084                         UAPI_DEF_WRITE_IO(struct ib_uverbs_query_srq,
4085                                           struct ib_uverbs_query_srq_resp),
4086                         UAPI_DEF_METHOD_NEEDS_FN(query_srq))),
4087
4088         DECLARE_UVERBS_OBJECT(
4089                 UVERBS_OBJECT_XRCD,
4090                 DECLARE_UVERBS_WRITE(
4091                         IB_USER_VERBS_CMD_CLOSE_XRCD,
4092                         ib_uverbs_close_xrcd,
4093                         UAPI_DEF_WRITE_I(struct ib_uverbs_close_xrcd),
4094                         UAPI_DEF_METHOD_NEEDS_FN(dealloc_xrcd)),
4095                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP,
4096                                      ib_uverbs_open_qp,
4097                                      UAPI_DEF_WRITE_UDATA_IO(
4098                                              struct ib_uverbs_open_qp,
4099                                              struct ib_uverbs_create_qp_resp)),
4100                 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD,
4101                                      ib_uverbs_open_xrcd,
4102                                      UAPI_DEF_WRITE_UDATA_IO(
4103                                              struct ib_uverbs_open_xrcd,
4104                                              struct ib_uverbs_open_xrcd_resp),
4105                                      UAPI_DEF_METHOD_NEEDS_FN(alloc_xrcd))),
4106
4107         {},
4108 };