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