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