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