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