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