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