IB/uverbs: Make lockdep output more readable
[firefly-linux-kernel-4.4.55.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
40 #include <asm/uaccess.h>
41
42 #include "uverbs.h"
43
44 struct uverbs_lock_class {
45         struct lock_class_key   key;
46         char                    name[16];
47 };
48
49 static struct uverbs_lock_class pd_lock_class   = { .name = "PD-uobj" };
50 static struct uverbs_lock_class mr_lock_class   = { .name = "MR-uobj" };
51 static struct uverbs_lock_class cq_lock_class   = { .name = "CQ-uobj" };
52 static struct uverbs_lock_class qp_lock_class   = { .name = "QP-uobj" };
53 static struct uverbs_lock_class ah_lock_class   = { .name = "AH-uobj" };
54 static struct uverbs_lock_class srq_lock_class  = { .name = "SRQ-uobj" };
55 static struct uverbs_lock_class xrcd_lock_class = { .name = "XRCD-uobj" };
56
57 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen)                       \
58         do {                                                            \
59                 (udata)->inbuf  = (void __user *) (ibuf);               \
60                 (udata)->outbuf = (void __user *) (obuf);               \
61                 (udata)->inlen  = (ilen);                               \
62                 (udata)->outlen = (olen);                               \
63         } while (0)
64
65 /*
66  * The ib_uobject locking scheme is as follows:
67  *
68  * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
69  *   needs to be held during all idr operations.  When an object is
70  *   looked up, a reference must be taken on the object's kref before
71  *   dropping this lock.
72  *
73  * - Each object also has an rwsem.  This rwsem must be held for
74  *   reading while an operation that uses the object is performed.
75  *   For example, while registering an MR, the associated PD's
76  *   uobject.mutex must be held for reading.  The rwsem must be held
77  *   for writing while initializing or destroying an object.
78  *
79  * - In addition, each object has a "live" flag.  If this flag is not
80  *   set, then lookups of the object will fail even if it is found in
81  *   the idr.  This handles a reader that blocks and does not acquire
82  *   the rwsem until after the object is destroyed.  The destroy
83  *   operation will set the live flag to 0 and then drop the rwsem;
84  *   this will allow the reader to acquire the rwsem, see that the
85  *   live flag is 0, and then drop the rwsem and its reference to
86  *   object.  The underlying storage will not be freed until the last
87  *   reference to the object is dropped.
88  */
89
90 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
91                       struct ib_ucontext *context, struct uverbs_lock_class *c)
92 {
93         uobj->user_handle = user_handle;
94         uobj->context     = context;
95         kref_init(&uobj->ref);
96         init_rwsem(&uobj->mutex);
97         lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
98         uobj->live        = 0;
99 }
100
101 static void release_uobj(struct kref *kref)
102 {
103         kfree(container_of(kref, struct ib_uobject, ref));
104 }
105
106 static void put_uobj(struct ib_uobject *uobj)
107 {
108         kref_put(&uobj->ref, release_uobj);
109 }
110
111 static void put_uobj_read(struct ib_uobject *uobj)
112 {
113         up_read(&uobj->mutex);
114         put_uobj(uobj);
115 }
116
117 static void put_uobj_write(struct ib_uobject *uobj)
118 {
119         up_write(&uobj->mutex);
120         put_uobj(uobj);
121 }
122
123 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
124 {
125         int ret;
126
127 retry:
128         if (!idr_pre_get(idr, GFP_KERNEL))
129                 return -ENOMEM;
130
131         spin_lock(&ib_uverbs_idr_lock);
132         ret = idr_get_new(idr, uobj, &uobj->id);
133         spin_unlock(&ib_uverbs_idr_lock);
134
135         if (ret == -EAGAIN)
136                 goto retry;
137
138         return ret;
139 }
140
141 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
142 {
143         spin_lock(&ib_uverbs_idr_lock);
144         idr_remove(idr, uobj->id);
145         spin_unlock(&ib_uverbs_idr_lock);
146 }
147
148 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
149                                          struct ib_ucontext *context)
150 {
151         struct ib_uobject *uobj;
152
153         spin_lock(&ib_uverbs_idr_lock);
154         uobj = idr_find(idr, id);
155         if (uobj) {
156                 if (uobj->context == context)
157                         kref_get(&uobj->ref);
158                 else
159                         uobj = NULL;
160         }
161         spin_unlock(&ib_uverbs_idr_lock);
162
163         return uobj;
164 }
165
166 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
167                                         struct ib_ucontext *context, int nested)
168 {
169         struct ib_uobject *uobj;
170
171         uobj = __idr_get_uobj(idr, id, context);
172         if (!uobj)
173                 return NULL;
174
175         if (nested)
176                 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
177         else
178                 down_read(&uobj->mutex);
179         if (!uobj->live) {
180                 put_uobj_read(uobj);
181                 return NULL;
182         }
183
184         return uobj;
185 }
186
187 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
188                                          struct ib_ucontext *context)
189 {
190         struct ib_uobject *uobj;
191
192         uobj = __idr_get_uobj(idr, id, context);
193         if (!uobj)
194                 return NULL;
195
196         down_write(&uobj->mutex);
197         if (!uobj->live) {
198                 put_uobj_write(uobj);
199                 return NULL;
200         }
201
202         return uobj;
203 }
204
205 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
206                           int nested)
207 {
208         struct ib_uobject *uobj;
209
210         uobj = idr_read_uobj(idr, id, context, nested);
211         return uobj ? uobj->object : NULL;
212 }
213
214 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
215 {
216         return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
217 }
218
219 static void put_pd_read(struct ib_pd *pd)
220 {
221         put_uobj_read(pd->uobject);
222 }
223
224 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
225 {
226         return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
227 }
228
229 static void put_cq_read(struct ib_cq *cq)
230 {
231         put_uobj_read(cq->uobject);
232 }
233
234 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
235 {
236         return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
237 }
238
239 static void put_ah_read(struct ib_ah *ah)
240 {
241         put_uobj_read(ah->uobject);
242 }
243
244 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
245 {
246         return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
247 }
248
249 static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
250 {
251         struct ib_uobject *uobj;
252
253         uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
254         return uobj ? uobj->object : NULL;
255 }
256
257 static void put_qp_read(struct ib_qp *qp)
258 {
259         put_uobj_read(qp->uobject);
260 }
261
262 static void put_qp_write(struct ib_qp *qp)
263 {
264         put_uobj_write(qp->uobject);
265 }
266
267 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
268 {
269         return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
270 }
271
272 static void put_srq_read(struct ib_srq *srq)
273 {
274         put_uobj_read(srq->uobject);
275 }
276
277 static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
278                                      struct ib_uobject **uobj)
279 {
280         *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
281         return *uobj ? (*uobj)->object : NULL;
282 }
283
284 static void put_xrcd_read(struct ib_uobject *uobj)
285 {
286         put_uobj_read(uobj);
287 }
288
289 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
290                               const char __user *buf,
291                               int in_len, int out_len)
292 {
293         struct ib_uverbs_get_context      cmd;
294         struct ib_uverbs_get_context_resp resp;
295         struct ib_udata                   udata;
296         struct ib_device                 *ibdev = file->device->ib_dev;
297         struct ib_ucontext               *ucontext;
298         struct file                      *filp;
299         int ret;
300
301         if (out_len < sizeof resp)
302                 return -ENOSPC;
303
304         if (copy_from_user(&cmd, buf, sizeof cmd))
305                 return -EFAULT;
306
307         mutex_lock(&file->mutex);
308
309         if (file->ucontext) {
310                 ret = -EINVAL;
311                 goto err;
312         }
313
314         INIT_UDATA(&udata, buf + sizeof cmd,
315                    (unsigned long) cmd.response + sizeof resp,
316                    in_len - sizeof cmd, out_len - sizeof resp);
317
318         ucontext = ibdev->alloc_ucontext(ibdev, &udata);
319         if (IS_ERR(ucontext)) {
320                 ret = PTR_ERR(ucontext);
321                 goto err;
322         }
323
324         ucontext->device = ibdev;
325         INIT_LIST_HEAD(&ucontext->pd_list);
326         INIT_LIST_HEAD(&ucontext->mr_list);
327         INIT_LIST_HEAD(&ucontext->mw_list);
328         INIT_LIST_HEAD(&ucontext->cq_list);
329         INIT_LIST_HEAD(&ucontext->qp_list);
330         INIT_LIST_HEAD(&ucontext->srq_list);
331         INIT_LIST_HEAD(&ucontext->ah_list);
332         INIT_LIST_HEAD(&ucontext->xrcd_list);
333         ucontext->closing = 0;
334
335         resp.num_comp_vectors = file->device->num_comp_vectors;
336
337         ret = get_unused_fd();
338         if (ret < 0)
339                 goto err_free;
340         resp.async_fd = ret;
341
342         filp = ib_uverbs_alloc_event_file(file, 1);
343         if (IS_ERR(filp)) {
344                 ret = PTR_ERR(filp);
345                 goto err_fd;
346         }
347
348         if (copy_to_user((void __user *) (unsigned long) cmd.response,
349                          &resp, sizeof resp)) {
350                 ret = -EFAULT;
351                 goto err_file;
352         }
353
354         file->async_file = filp->private_data;
355
356         INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
357                               ib_uverbs_event_handler);
358         ret = ib_register_event_handler(&file->event_handler);
359         if (ret)
360                 goto err_file;
361
362         kref_get(&file->async_file->ref);
363         kref_get(&file->ref);
364         file->ucontext = ucontext;
365
366         fd_install(resp.async_fd, filp);
367
368         mutex_unlock(&file->mutex);
369
370         return in_len;
371
372 err_file:
373         fput(filp);
374
375 err_fd:
376         put_unused_fd(resp.async_fd);
377
378 err_free:
379         ibdev->dealloc_ucontext(ucontext);
380
381 err:
382         mutex_unlock(&file->mutex);
383         return ret;
384 }
385
386 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
387                                const char __user *buf,
388                                int in_len, int out_len)
389 {
390         struct ib_uverbs_query_device      cmd;
391         struct ib_uverbs_query_device_resp resp;
392         struct ib_device_attr              attr;
393         int                                ret;
394
395         if (out_len < sizeof resp)
396                 return -ENOSPC;
397
398         if (copy_from_user(&cmd, buf, sizeof cmd))
399                 return -EFAULT;
400
401         ret = ib_query_device(file->device->ib_dev, &attr);
402         if (ret)
403                 return ret;
404
405         memset(&resp, 0, sizeof resp);
406
407         resp.fw_ver                    = attr.fw_ver;
408         resp.node_guid                 = file->device->ib_dev->node_guid;
409         resp.sys_image_guid            = attr.sys_image_guid;
410         resp.max_mr_size               = attr.max_mr_size;
411         resp.page_size_cap             = attr.page_size_cap;
412         resp.vendor_id                 = attr.vendor_id;
413         resp.vendor_part_id            = attr.vendor_part_id;
414         resp.hw_ver                    = attr.hw_ver;
415         resp.max_qp                    = attr.max_qp;
416         resp.max_qp_wr                 = attr.max_qp_wr;
417         resp.device_cap_flags          = attr.device_cap_flags;
418         resp.max_sge                   = attr.max_sge;
419         resp.max_sge_rd                = attr.max_sge_rd;
420         resp.max_cq                    = attr.max_cq;
421         resp.max_cqe                   = attr.max_cqe;
422         resp.max_mr                    = attr.max_mr;
423         resp.max_pd                    = attr.max_pd;
424         resp.max_qp_rd_atom            = attr.max_qp_rd_atom;
425         resp.max_ee_rd_atom            = attr.max_ee_rd_atom;
426         resp.max_res_rd_atom           = attr.max_res_rd_atom;
427         resp.max_qp_init_rd_atom       = attr.max_qp_init_rd_atom;
428         resp.max_ee_init_rd_atom       = attr.max_ee_init_rd_atom;
429         resp.atomic_cap                = attr.atomic_cap;
430         resp.max_ee                    = attr.max_ee;
431         resp.max_rdd                   = attr.max_rdd;
432         resp.max_mw                    = attr.max_mw;
433         resp.max_raw_ipv6_qp           = attr.max_raw_ipv6_qp;
434         resp.max_raw_ethy_qp           = attr.max_raw_ethy_qp;
435         resp.max_mcast_grp             = attr.max_mcast_grp;
436         resp.max_mcast_qp_attach       = attr.max_mcast_qp_attach;
437         resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
438         resp.max_ah                    = attr.max_ah;
439         resp.max_fmr                   = attr.max_fmr;
440         resp.max_map_per_fmr           = attr.max_map_per_fmr;
441         resp.max_srq                   = attr.max_srq;
442         resp.max_srq_wr                = attr.max_srq_wr;
443         resp.max_srq_sge               = attr.max_srq_sge;
444         resp.max_pkeys                 = attr.max_pkeys;
445         resp.local_ca_ack_delay        = attr.local_ca_ack_delay;
446         resp.phys_port_cnt             = file->device->ib_dev->phys_port_cnt;
447
448         if (copy_to_user((void __user *) (unsigned long) cmd.response,
449                          &resp, sizeof resp))
450                 return -EFAULT;
451
452         return in_len;
453 }
454
455 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
456                              const char __user *buf,
457                              int in_len, int out_len)
458 {
459         struct ib_uverbs_query_port      cmd;
460         struct ib_uverbs_query_port_resp resp;
461         struct ib_port_attr              attr;
462         int                              ret;
463
464         if (out_len < sizeof resp)
465                 return -ENOSPC;
466
467         if (copy_from_user(&cmd, buf, sizeof cmd))
468                 return -EFAULT;
469
470         ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
471         if (ret)
472                 return ret;
473
474         memset(&resp, 0, sizeof resp);
475
476         resp.state           = attr.state;
477         resp.max_mtu         = attr.max_mtu;
478         resp.active_mtu      = attr.active_mtu;
479         resp.gid_tbl_len     = attr.gid_tbl_len;
480         resp.port_cap_flags  = attr.port_cap_flags;
481         resp.max_msg_sz      = attr.max_msg_sz;
482         resp.bad_pkey_cntr   = attr.bad_pkey_cntr;
483         resp.qkey_viol_cntr  = attr.qkey_viol_cntr;
484         resp.pkey_tbl_len    = attr.pkey_tbl_len;
485         resp.lid             = attr.lid;
486         resp.sm_lid          = attr.sm_lid;
487         resp.lmc             = attr.lmc;
488         resp.max_vl_num      = attr.max_vl_num;
489         resp.sm_sl           = attr.sm_sl;
490         resp.subnet_timeout  = attr.subnet_timeout;
491         resp.init_type_reply = attr.init_type_reply;
492         resp.active_width    = attr.active_width;
493         resp.active_speed    = attr.active_speed;
494         resp.phys_state      = attr.phys_state;
495         resp.link_layer      = rdma_port_get_link_layer(file->device->ib_dev,
496                                                         cmd.port_num);
497
498         if (copy_to_user((void __user *) (unsigned long) cmd.response,
499                          &resp, sizeof resp))
500                 return -EFAULT;
501
502         return in_len;
503 }
504
505 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
506                            const char __user *buf,
507                            int in_len, int out_len)
508 {
509         struct ib_uverbs_alloc_pd      cmd;
510         struct ib_uverbs_alloc_pd_resp resp;
511         struct ib_udata                udata;
512         struct ib_uobject             *uobj;
513         struct ib_pd                  *pd;
514         int                            ret;
515
516         if (out_len < sizeof resp)
517                 return -ENOSPC;
518
519         if (copy_from_user(&cmd, buf, sizeof cmd))
520                 return -EFAULT;
521
522         INIT_UDATA(&udata, buf + sizeof cmd,
523                    (unsigned long) cmd.response + sizeof resp,
524                    in_len - sizeof cmd, out_len - sizeof resp);
525
526         uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
527         if (!uobj)
528                 return -ENOMEM;
529
530         init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
531         down_write(&uobj->mutex);
532
533         pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
534                                             file->ucontext, &udata);
535         if (IS_ERR(pd)) {
536                 ret = PTR_ERR(pd);
537                 goto err;
538         }
539
540         pd->device  = file->device->ib_dev;
541         pd->uobject = uobj;
542         atomic_set(&pd->usecnt, 0);
543
544         uobj->object = pd;
545         ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
546         if (ret)
547                 goto err_idr;
548
549         memset(&resp, 0, sizeof resp);
550         resp.pd_handle = uobj->id;
551
552         if (copy_to_user((void __user *) (unsigned long) cmd.response,
553                          &resp, sizeof resp)) {
554                 ret = -EFAULT;
555                 goto err_copy;
556         }
557
558         mutex_lock(&file->mutex);
559         list_add_tail(&uobj->list, &file->ucontext->pd_list);
560         mutex_unlock(&file->mutex);
561
562         uobj->live = 1;
563
564         up_write(&uobj->mutex);
565
566         return in_len;
567
568 err_copy:
569         idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
570
571 err_idr:
572         ib_dealloc_pd(pd);
573
574 err:
575         put_uobj_write(uobj);
576         return ret;
577 }
578
579 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
580                              const char __user *buf,
581                              int in_len, int out_len)
582 {
583         struct ib_uverbs_dealloc_pd cmd;
584         struct ib_uobject          *uobj;
585         int                         ret;
586
587         if (copy_from_user(&cmd, buf, sizeof cmd))
588                 return -EFAULT;
589
590         uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
591         if (!uobj)
592                 return -EINVAL;
593
594         ret = ib_dealloc_pd(uobj->object);
595         if (!ret)
596                 uobj->live = 0;
597
598         put_uobj_write(uobj);
599
600         if (ret)
601                 return ret;
602
603         idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
604
605         mutex_lock(&file->mutex);
606         list_del(&uobj->list);
607         mutex_unlock(&file->mutex);
608
609         put_uobj(uobj);
610
611         return in_len;
612 }
613
614 struct xrcd_table_entry {
615         struct rb_node  node;
616         struct ib_xrcd *xrcd;
617         struct inode   *inode;
618 };
619
620 static int xrcd_table_insert(struct ib_uverbs_device *dev,
621                             struct inode *inode,
622                             struct ib_xrcd *xrcd)
623 {
624         struct xrcd_table_entry *entry, *scan;
625         struct rb_node **p = &dev->xrcd_tree.rb_node;
626         struct rb_node *parent = NULL;
627
628         entry = kmalloc(sizeof *entry, GFP_KERNEL);
629         if (!entry)
630                 return -ENOMEM;
631
632         entry->xrcd  = xrcd;
633         entry->inode = inode;
634
635         while (*p) {
636                 parent = *p;
637                 scan = rb_entry(parent, struct xrcd_table_entry, node);
638
639                 if (inode < scan->inode) {
640                         p = &(*p)->rb_left;
641                 } else if (inode > scan->inode) {
642                         p = &(*p)->rb_right;
643                 } else {
644                         kfree(entry);
645                         return -EEXIST;
646                 }
647         }
648
649         rb_link_node(&entry->node, parent, p);
650         rb_insert_color(&entry->node, &dev->xrcd_tree);
651         igrab(inode);
652         return 0;
653 }
654
655 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
656                                                   struct inode *inode)
657 {
658         struct xrcd_table_entry *entry;
659         struct rb_node *p = dev->xrcd_tree.rb_node;
660
661         while (p) {
662                 entry = rb_entry(p, struct xrcd_table_entry, node);
663
664                 if (inode < entry->inode)
665                         p = p->rb_left;
666                 else if (inode > entry->inode)
667                         p = p->rb_right;
668                 else
669                         return entry;
670         }
671
672         return NULL;
673 }
674
675 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
676 {
677         struct xrcd_table_entry *entry;
678
679         entry = xrcd_table_search(dev, inode);
680         if (!entry)
681                 return NULL;
682
683         return entry->xrcd;
684 }
685
686 static void xrcd_table_delete(struct ib_uverbs_device *dev,
687                               struct inode *inode)
688 {
689         struct xrcd_table_entry *entry;
690
691         entry = xrcd_table_search(dev, inode);
692         if (entry) {
693                 iput(inode);
694                 rb_erase(&entry->node, &dev->xrcd_tree);
695                 kfree(entry);
696         }
697 }
698
699 ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
700                             const char __user *buf, int in_len,
701                             int out_len)
702 {
703         struct ib_uverbs_open_xrcd      cmd;
704         struct ib_uverbs_open_xrcd_resp resp;
705         struct ib_udata                 udata;
706         struct ib_uxrcd_object         *obj;
707         struct ib_xrcd                 *xrcd = NULL;
708         struct file                    *f = NULL;
709         struct inode                   *inode = NULL;
710         int                             ret = 0;
711         int                             new_xrcd = 0;
712
713         if (out_len < sizeof resp)
714                 return -ENOSPC;
715
716         if (copy_from_user(&cmd, buf, sizeof cmd))
717                 return -EFAULT;
718
719         INIT_UDATA(&udata, buf + sizeof cmd,
720                    (unsigned long) cmd.response + sizeof resp,
721                    in_len - sizeof cmd, out_len - sizeof  resp);
722
723         mutex_lock(&file->device->xrcd_tree_mutex);
724
725         if (cmd.fd != -1) {
726                 /* search for file descriptor */
727                 f = fget(cmd.fd);
728                 if (!f) {
729                         ret = -EBADF;
730                         goto err_tree_mutex_unlock;
731                 }
732
733                 inode = f->f_dentry->d_inode;
734                 if (!inode) {
735                         ret = -EBADF;
736                         goto err_tree_mutex_unlock;
737                 }
738
739                 xrcd = find_xrcd(file->device, inode);
740                 if (!xrcd && !(cmd.oflags & O_CREAT)) {
741                         /* no file descriptor. Need CREATE flag */
742                         ret = -EAGAIN;
743                         goto err_tree_mutex_unlock;
744                 }
745
746                 if (xrcd && cmd.oflags & O_EXCL) {
747                         ret = -EINVAL;
748                         goto err_tree_mutex_unlock;
749                 }
750         }
751
752         obj = kmalloc(sizeof *obj, GFP_KERNEL);
753         if (!obj) {
754                 ret = -ENOMEM;
755                 goto err_tree_mutex_unlock;
756         }
757
758         init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
759
760         down_write(&obj->uobject.mutex);
761
762         if (!xrcd) {
763                 xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
764                                                         file->ucontext, &udata);
765                 if (IS_ERR(xrcd)) {
766                         ret = PTR_ERR(xrcd);
767                         goto err;
768                 }
769
770                 xrcd->inode   = inode;
771                 xrcd->device  = file->device->ib_dev;
772                 atomic_set(&xrcd->usecnt, 0);
773                 mutex_init(&xrcd->tgt_qp_mutex);
774                 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
775                 new_xrcd = 1;
776         }
777
778         atomic_set(&obj->refcnt, 0);
779         obj->uobject.object = xrcd;
780         ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
781         if (ret)
782                 goto err_idr;
783
784         memset(&resp, 0, sizeof resp);
785         resp.xrcd_handle = obj->uobject.id;
786
787         if (inode) {
788                 if (new_xrcd) {
789                         /* create new inode/xrcd table entry */
790                         ret = xrcd_table_insert(file->device, inode, xrcd);
791                         if (ret)
792                                 goto err_insert_xrcd;
793                 }
794                 atomic_inc(&xrcd->usecnt);
795         }
796
797         if (copy_to_user((void __user *) (unsigned long) cmd.response,
798                          &resp, sizeof resp)) {
799                 ret = -EFAULT;
800                 goto err_copy;
801         }
802
803         if (f)
804                 fput(f);
805
806         mutex_lock(&file->mutex);
807         list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
808         mutex_unlock(&file->mutex);
809
810         obj->uobject.live = 1;
811         up_write(&obj->uobject.mutex);
812
813         mutex_unlock(&file->device->xrcd_tree_mutex);
814         return in_len;
815
816 err_copy:
817         if (inode) {
818                 if (new_xrcd)
819                         xrcd_table_delete(file->device, inode);
820                 atomic_dec(&xrcd->usecnt);
821         }
822
823 err_insert_xrcd:
824         idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
825
826 err_idr:
827         ib_dealloc_xrcd(xrcd);
828
829 err:
830         put_uobj_write(&obj->uobject);
831
832 err_tree_mutex_unlock:
833         if (f)
834                 fput(f);
835
836         mutex_unlock(&file->device->xrcd_tree_mutex);
837
838         return ret;
839 }
840
841 ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
842                              const char __user *buf, int in_len,
843                              int out_len)
844 {
845         struct ib_uverbs_close_xrcd cmd;
846         struct ib_uobject           *uobj;
847         struct ib_xrcd              *xrcd = NULL;
848         struct inode                *inode = NULL;
849         struct ib_uxrcd_object      *obj;
850         int                         live;
851         int                         ret = 0;
852
853         if (copy_from_user(&cmd, buf, sizeof cmd))
854                 return -EFAULT;
855
856         mutex_lock(&file->device->xrcd_tree_mutex);
857         uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
858         if (!uobj) {
859                 ret = -EINVAL;
860                 goto out;
861         }
862
863         xrcd  = uobj->object;
864         inode = xrcd->inode;
865         obj   = container_of(uobj, struct ib_uxrcd_object, uobject);
866         if (atomic_read(&obj->refcnt)) {
867                 put_uobj_write(uobj);
868                 ret = -EBUSY;
869                 goto out;
870         }
871
872         if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
873                 ret = ib_dealloc_xrcd(uobj->object);
874                 if (!ret)
875                         uobj->live = 0;
876         }
877
878         live = uobj->live;
879         if (inode && ret)
880                 atomic_inc(&xrcd->usecnt);
881
882         put_uobj_write(uobj);
883
884         if (ret)
885                 goto out;
886
887         if (inode && !live)
888                 xrcd_table_delete(file->device, inode);
889
890         idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
891         mutex_lock(&file->mutex);
892         list_del(&uobj->list);
893         mutex_unlock(&file->mutex);
894
895         put_uobj(uobj);
896         ret = in_len;
897
898 out:
899         mutex_unlock(&file->device->xrcd_tree_mutex);
900         return ret;
901 }
902
903 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
904                             struct ib_xrcd *xrcd)
905 {
906         struct inode *inode;
907
908         inode = xrcd->inode;
909         if (inode && !atomic_dec_and_test(&xrcd->usecnt))
910                 return;
911
912         ib_dealloc_xrcd(xrcd);
913
914         if (inode)
915                 xrcd_table_delete(dev, inode);
916 }
917
918 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
919                          const char __user *buf, int in_len,
920                          int out_len)
921 {
922         struct ib_uverbs_reg_mr      cmd;
923         struct ib_uverbs_reg_mr_resp resp;
924         struct ib_udata              udata;
925         struct ib_uobject           *uobj;
926         struct ib_pd                *pd;
927         struct ib_mr                *mr;
928         int                          ret;
929
930         if (out_len < sizeof resp)
931                 return -ENOSPC;
932
933         if (copy_from_user(&cmd, buf, sizeof cmd))
934                 return -EFAULT;
935
936         INIT_UDATA(&udata, buf + sizeof cmd,
937                    (unsigned long) cmd.response + sizeof resp,
938                    in_len - sizeof cmd, out_len - sizeof resp);
939
940         if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
941                 return -EINVAL;
942
943         /*
944          * Local write permission is required if remote write or
945          * remote atomic permission is also requested.
946          */
947         if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
948             !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
949                 return -EINVAL;
950
951         uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
952         if (!uobj)
953                 return -ENOMEM;
954
955         init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
956         down_write(&uobj->mutex);
957
958         pd = idr_read_pd(cmd.pd_handle, file->ucontext);
959         if (!pd) {
960                 ret = -EINVAL;
961                 goto err_free;
962         }
963
964         mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
965                                      cmd.access_flags, &udata);
966         if (IS_ERR(mr)) {
967                 ret = PTR_ERR(mr);
968                 goto err_put;
969         }
970
971         mr->device  = pd->device;
972         mr->pd      = pd;
973         mr->uobject = uobj;
974         atomic_inc(&pd->usecnt);
975         atomic_set(&mr->usecnt, 0);
976
977         uobj->object = mr;
978         ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
979         if (ret)
980                 goto err_unreg;
981
982         memset(&resp, 0, sizeof resp);
983         resp.lkey      = mr->lkey;
984         resp.rkey      = mr->rkey;
985         resp.mr_handle = uobj->id;
986
987         if (copy_to_user((void __user *) (unsigned long) cmd.response,
988                          &resp, sizeof resp)) {
989                 ret = -EFAULT;
990                 goto err_copy;
991         }
992
993         put_pd_read(pd);
994
995         mutex_lock(&file->mutex);
996         list_add_tail(&uobj->list, &file->ucontext->mr_list);
997         mutex_unlock(&file->mutex);
998
999         uobj->live = 1;
1000
1001         up_write(&uobj->mutex);
1002
1003         return in_len;
1004
1005 err_copy:
1006         idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1007
1008 err_unreg:
1009         ib_dereg_mr(mr);
1010
1011 err_put:
1012         put_pd_read(pd);
1013
1014 err_free:
1015         put_uobj_write(uobj);
1016         return ret;
1017 }
1018
1019 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
1020                            const char __user *buf, int in_len,
1021                            int out_len)
1022 {
1023         struct ib_uverbs_dereg_mr cmd;
1024         struct ib_mr             *mr;
1025         struct ib_uobject        *uobj;
1026         int                       ret = -EINVAL;
1027
1028         if (copy_from_user(&cmd, buf, sizeof cmd))
1029                 return -EFAULT;
1030
1031         uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1032         if (!uobj)
1033                 return -EINVAL;
1034
1035         mr = uobj->object;
1036
1037         ret = ib_dereg_mr(mr);
1038         if (!ret)
1039                 uobj->live = 0;
1040
1041         put_uobj_write(uobj);
1042
1043         if (ret)
1044                 return ret;
1045
1046         idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
1047
1048         mutex_lock(&file->mutex);
1049         list_del(&uobj->list);
1050         mutex_unlock(&file->mutex);
1051
1052         put_uobj(uobj);
1053
1054         return in_len;
1055 }
1056
1057 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
1058                                       const char __user *buf, int in_len,
1059                                       int out_len)
1060 {
1061         struct ib_uverbs_create_comp_channel       cmd;
1062         struct ib_uverbs_create_comp_channel_resp  resp;
1063         struct file                               *filp;
1064         int ret;
1065
1066         if (out_len < sizeof resp)
1067                 return -ENOSPC;
1068
1069         if (copy_from_user(&cmd, buf, sizeof cmd))
1070                 return -EFAULT;
1071
1072         ret = get_unused_fd();
1073         if (ret < 0)
1074                 return ret;
1075         resp.fd = ret;
1076
1077         filp = ib_uverbs_alloc_event_file(file, 0);
1078         if (IS_ERR(filp)) {
1079                 put_unused_fd(resp.fd);
1080                 return PTR_ERR(filp);
1081         }
1082
1083         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1084                          &resp, sizeof resp)) {
1085                 put_unused_fd(resp.fd);
1086                 fput(filp);
1087                 return -EFAULT;
1088         }
1089
1090         fd_install(resp.fd, filp);
1091         return in_len;
1092 }
1093
1094 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
1095                             const char __user *buf, int in_len,
1096                             int out_len)
1097 {
1098         struct ib_uverbs_create_cq      cmd;
1099         struct ib_uverbs_create_cq_resp resp;
1100         struct ib_udata                 udata;
1101         struct ib_ucq_object           *obj;
1102         struct ib_uverbs_event_file    *ev_file = NULL;
1103         struct ib_cq                   *cq;
1104         int                             ret;
1105
1106         if (out_len < sizeof resp)
1107                 return -ENOSPC;
1108
1109         if (copy_from_user(&cmd, buf, sizeof cmd))
1110                 return -EFAULT;
1111
1112         INIT_UDATA(&udata, buf + sizeof cmd,
1113                    (unsigned long) cmd.response + sizeof resp,
1114                    in_len - sizeof cmd, out_len - sizeof resp);
1115
1116         if (cmd.comp_vector >= file->device->num_comp_vectors)
1117                 return -EINVAL;
1118
1119         obj = kmalloc(sizeof *obj, GFP_KERNEL);
1120         if (!obj)
1121                 return -ENOMEM;
1122
1123         init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_class);
1124         down_write(&obj->uobject.mutex);
1125
1126         if (cmd.comp_channel >= 0) {
1127                 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
1128                 if (!ev_file) {
1129                         ret = -EINVAL;
1130                         goto err;
1131                 }
1132         }
1133
1134         obj->uverbs_file           = file;
1135         obj->comp_events_reported  = 0;
1136         obj->async_events_reported = 0;
1137         INIT_LIST_HEAD(&obj->comp_list);
1138         INIT_LIST_HEAD(&obj->async_list);
1139
1140         cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
1141                                              cmd.comp_vector,
1142                                              file->ucontext, &udata);
1143         if (IS_ERR(cq)) {
1144                 ret = PTR_ERR(cq);
1145                 goto err_file;
1146         }
1147
1148         cq->device        = file->device->ib_dev;
1149         cq->uobject       = &obj->uobject;
1150         cq->comp_handler  = ib_uverbs_comp_handler;
1151         cq->event_handler = ib_uverbs_cq_event_handler;
1152         cq->cq_context    = ev_file;
1153         atomic_set(&cq->usecnt, 0);
1154
1155         obj->uobject.object = cq;
1156         ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1157         if (ret)
1158                 goto err_free;
1159
1160         memset(&resp, 0, sizeof resp);
1161         resp.cq_handle = obj->uobject.id;
1162         resp.cqe       = cq->cqe;
1163
1164         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1165                          &resp, sizeof resp)) {
1166                 ret = -EFAULT;
1167                 goto err_copy;
1168         }
1169
1170         mutex_lock(&file->mutex);
1171         list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
1172         mutex_unlock(&file->mutex);
1173
1174         obj->uobject.live = 1;
1175
1176         up_write(&obj->uobject.mutex);
1177
1178         return in_len;
1179
1180 err_copy:
1181         idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
1182
1183 err_free:
1184         ib_destroy_cq(cq);
1185
1186 err_file:
1187         if (ev_file)
1188                 ib_uverbs_release_ucq(file, ev_file, obj);
1189
1190 err:
1191         put_uobj_write(&obj->uobject);
1192         return ret;
1193 }
1194
1195 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
1196                             const char __user *buf, int in_len,
1197                             int out_len)
1198 {
1199         struct ib_uverbs_resize_cq      cmd;
1200         struct ib_uverbs_resize_cq_resp resp;
1201         struct ib_udata                 udata;
1202         struct ib_cq                    *cq;
1203         int                             ret = -EINVAL;
1204
1205         if (copy_from_user(&cmd, buf, sizeof cmd))
1206                 return -EFAULT;
1207
1208         INIT_UDATA(&udata, buf + sizeof cmd,
1209                    (unsigned long) cmd.response + sizeof resp,
1210                    in_len - sizeof cmd, out_len - sizeof resp);
1211
1212         cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1213         if (!cq)
1214                 return -EINVAL;
1215
1216         ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1217         if (ret)
1218                 goto out;
1219
1220         resp.cqe = cq->cqe;
1221
1222         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1223                          &resp, sizeof resp.cqe))
1224                 ret = -EFAULT;
1225
1226 out:
1227         put_cq_read(cq);
1228
1229         return ret ? ret : in_len;
1230 }
1231
1232 static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1233 {
1234         struct ib_uverbs_wc tmp;
1235
1236         tmp.wr_id               = wc->wr_id;
1237         tmp.status              = wc->status;
1238         tmp.opcode              = wc->opcode;
1239         tmp.vendor_err          = wc->vendor_err;
1240         tmp.byte_len            = wc->byte_len;
1241         tmp.ex.imm_data         = (__u32 __force) wc->ex.imm_data;
1242         tmp.qp_num              = wc->qp->qp_num;
1243         tmp.src_qp              = wc->src_qp;
1244         tmp.wc_flags            = wc->wc_flags;
1245         tmp.pkey_index          = wc->pkey_index;
1246         tmp.slid                = wc->slid;
1247         tmp.sl                  = wc->sl;
1248         tmp.dlid_path_bits      = wc->dlid_path_bits;
1249         tmp.port_num            = wc->port_num;
1250         tmp.reserved            = 0;
1251
1252         if (copy_to_user(dest, &tmp, sizeof tmp))
1253                 return -EFAULT;
1254
1255         return 0;
1256 }
1257
1258 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
1259                           const char __user *buf, int in_len,
1260                           int out_len)
1261 {
1262         struct ib_uverbs_poll_cq       cmd;
1263         struct ib_uverbs_poll_cq_resp  resp;
1264         u8 __user                     *header_ptr;
1265         u8 __user                     *data_ptr;
1266         struct ib_cq                  *cq;
1267         struct ib_wc                   wc;
1268         int                            ret;
1269
1270         if (copy_from_user(&cmd, buf, sizeof cmd))
1271                 return -EFAULT;
1272
1273         cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1274         if (!cq)
1275                 return -EINVAL;
1276
1277         /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1278         header_ptr = (void __user *)(unsigned long) cmd.response;
1279         data_ptr = header_ptr + sizeof resp;
1280
1281         memset(&resp, 0, sizeof resp);
1282         while (resp.count < cmd.ne) {
1283                 ret = ib_poll_cq(cq, 1, &wc);
1284                 if (ret < 0)
1285                         goto out_put;
1286                 if (!ret)
1287                         break;
1288
1289                 ret = copy_wc_to_user(data_ptr, &wc);
1290                 if (ret)
1291                         goto out_put;
1292
1293                 data_ptr += sizeof(struct ib_uverbs_wc);
1294                 ++resp.count;
1295         }
1296
1297         if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1298                 ret = -EFAULT;
1299                 goto out_put;
1300         }
1301
1302         ret = in_len;
1303
1304 out_put:
1305         put_cq_read(cq);
1306         return ret;
1307 }
1308
1309 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
1310                                 const char __user *buf, int in_len,
1311                                 int out_len)
1312 {
1313         struct ib_uverbs_req_notify_cq cmd;
1314         struct ib_cq                  *cq;
1315
1316         if (copy_from_user(&cmd, buf, sizeof cmd))
1317                 return -EFAULT;
1318
1319         cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
1320         if (!cq)
1321                 return -EINVAL;
1322
1323         ib_req_notify_cq(cq, cmd.solicited_only ?
1324                          IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1325
1326         put_cq_read(cq);
1327
1328         return in_len;
1329 }
1330
1331 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
1332                              const char __user *buf, int in_len,
1333                              int out_len)
1334 {
1335         struct ib_uverbs_destroy_cq      cmd;
1336         struct ib_uverbs_destroy_cq_resp resp;
1337         struct ib_uobject               *uobj;
1338         struct ib_cq                    *cq;
1339         struct ib_ucq_object            *obj;
1340         struct ib_uverbs_event_file     *ev_file;
1341         int                              ret = -EINVAL;
1342
1343         if (copy_from_user(&cmd, buf, sizeof cmd))
1344                 return -EFAULT;
1345
1346         uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1347         if (!uobj)
1348                 return -EINVAL;
1349         cq      = uobj->object;
1350         ev_file = cq->cq_context;
1351         obj     = container_of(cq->uobject, struct ib_ucq_object, uobject);
1352
1353         ret = ib_destroy_cq(cq);
1354         if (!ret)
1355                 uobj->live = 0;
1356
1357         put_uobj_write(uobj);
1358
1359         if (ret)
1360                 return ret;
1361
1362         idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1363
1364         mutex_lock(&file->mutex);
1365         list_del(&uobj->list);
1366         mutex_unlock(&file->mutex);
1367
1368         ib_uverbs_release_ucq(file, ev_file, obj);
1369
1370         memset(&resp, 0, sizeof resp);
1371         resp.comp_events_reported  = obj->comp_events_reported;
1372         resp.async_events_reported = obj->async_events_reported;
1373
1374         put_uobj(uobj);
1375
1376         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1377                          &resp, sizeof resp))
1378                 return -EFAULT;
1379
1380         return in_len;
1381 }
1382
1383 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1384                             const char __user *buf, int in_len,
1385                             int out_len)
1386 {
1387         struct ib_uverbs_create_qp      cmd;
1388         struct ib_uverbs_create_qp_resp resp;
1389         struct ib_udata                 udata;
1390         struct ib_uqp_object           *obj;
1391         struct ib_device               *device;
1392         struct ib_pd                   *pd = NULL;
1393         struct ib_xrcd                 *xrcd = NULL;
1394         struct ib_uobject              *uninitialized_var(xrcd_uobj);
1395         struct ib_cq                   *scq = NULL, *rcq = NULL;
1396         struct ib_srq                  *srq = NULL;
1397         struct ib_qp                   *qp;
1398         struct ib_qp_init_attr          attr;
1399         int ret;
1400
1401         if (out_len < sizeof resp)
1402                 return -ENOSPC;
1403
1404         if (copy_from_user(&cmd, buf, sizeof cmd))
1405                 return -EFAULT;
1406
1407         INIT_UDATA(&udata, buf + sizeof cmd,
1408                    (unsigned long) cmd.response + sizeof resp,
1409                    in_len - sizeof cmd, out_len - sizeof resp);
1410
1411         obj = kmalloc(sizeof *obj, GFP_KERNEL);
1412         if (!obj)
1413                 return -ENOMEM;
1414
1415         init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1416         down_write(&obj->uevent.uobject.mutex);
1417
1418         if (cmd.qp_type == IB_QPT_XRC_TGT) {
1419                 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1420                 if (!xrcd) {
1421                         ret = -EINVAL;
1422                         goto err_put;
1423                 }
1424                 device = xrcd->device;
1425         } else {
1426                 pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
1427                 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
1428                 if (!pd || !scq) {
1429                         ret = -EINVAL;
1430                         goto err_put;
1431                 }
1432
1433                 if (cmd.qp_type == IB_QPT_XRC_INI) {
1434                         cmd.max_recv_wr = cmd.max_recv_sge = 0;
1435                 } else {
1436                         if (cmd.is_srq) {
1437                                 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1438                                 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1439                                         ret = -EINVAL;
1440                                         goto err_put;
1441                                 }
1442                         }
1443                         rcq = (cmd.recv_cq_handle == cmd.send_cq_handle) ?
1444                                scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
1445                         if (!rcq) {
1446                                 ret = -EINVAL;
1447                                 goto err_put;
1448                         }
1449                 }
1450                 device = pd->device;
1451         }
1452
1453         attr.event_handler = ib_uverbs_qp_event_handler;
1454         attr.qp_context    = file;
1455         attr.send_cq       = scq;
1456         attr.recv_cq       = rcq;
1457         attr.srq           = srq;
1458         attr.xrcd          = xrcd;
1459         attr.sq_sig_type   = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1460         attr.qp_type       = cmd.qp_type;
1461         attr.create_flags  = 0;
1462
1463         attr.cap.max_send_wr     = cmd.max_send_wr;
1464         attr.cap.max_recv_wr     = cmd.max_recv_wr;
1465         attr.cap.max_send_sge    = cmd.max_send_sge;
1466         attr.cap.max_recv_sge    = cmd.max_recv_sge;
1467         attr.cap.max_inline_data = cmd.max_inline_data;
1468
1469         obj->uevent.events_reported     = 0;
1470         INIT_LIST_HEAD(&obj->uevent.event_list);
1471         INIT_LIST_HEAD(&obj->mcast_list);
1472
1473         if (cmd.qp_type == IB_QPT_XRC_TGT)
1474                 qp = ib_create_qp(pd, &attr);
1475         else
1476                 qp = device->create_qp(pd, &attr, &udata);
1477
1478         if (IS_ERR(qp)) {
1479                 ret = PTR_ERR(qp);
1480                 goto err_put;
1481         }
1482
1483         if (cmd.qp_type != IB_QPT_XRC_TGT) {
1484                 qp->real_qp       = qp;
1485                 qp->device        = device;
1486                 qp->pd            = pd;
1487                 qp->send_cq       = attr.send_cq;
1488                 qp->recv_cq       = attr.recv_cq;
1489                 qp->srq           = attr.srq;
1490                 qp->event_handler = attr.event_handler;
1491                 qp->qp_context    = attr.qp_context;
1492                 qp->qp_type       = attr.qp_type;
1493                 atomic_set(&qp->usecnt, 0);
1494                 atomic_inc(&pd->usecnt);
1495                 atomic_inc(&attr.send_cq->usecnt);
1496                 if (attr.recv_cq)
1497                         atomic_inc(&attr.recv_cq->usecnt);
1498                 if (attr.srq)
1499                         atomic_inc(&attr.srq->usecnt);
1500         }
1501         qp->uobject = &obj->uevent.uobject;
1502
1503         obj->uevent.uobject.object = qp;
1504         ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1505         if (ret)
1506                 goto err_destroy;
1507
1508         memset(&resp, 0, sizeof resp);
1509         resp.qpn             = qp->qp_num;
1510         resp.qp_handle       = obj->uevent.uobject.id;
1511         resp.max_recv_sge    = attr.cap.max_recv_sge;
1512         resp.max_send_sge    = attr.cap.max_send_sge;
1513         resp.max_recv_wr     = attr.cap.max_recv_wr;
1514         resp.max_send_wr     = attr.cap.max_send_wr;
1515         resp.max_inline_data = attr.cap.max_inline_data;
1516
1517         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1518                          &resp, sizeof resp)) {
1519                 ret = -EFAULT;
1520                 goto err_copy;
1521         }
1522
1523         if (xrcd)
1524                 put_xrcd_read(xrcd_uobj);
1525         if (pd)
1526                 put_pd_read(pd);
1527         if (scq)
1528                 put_cq_read(scq);
1529         if (rcq && rcq != scq)
1530                 put_cq_read(rcq);
1531         if (srq)
1532                 put_srq_read(srq);
1533
1534         mutex_lock(&file->mutex);
1535         list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1536         mutex_unlock(&file->mutex);
1537
1538         obj->uevent.uobject.live = 1;
1539
1540         up_write(&obj->uevent.uobject.mutex);
1541
1542         return in_len;
1543
1544 err_copy:
1545         idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1546
1547 err_destroy:
1548         ib_destroy_qp(qp);
1549
1550 err_put:
1551         if (xrcd)
1552                 put_xrcd_read(xrcd_uobj);
1553         if (pd)
1554                 put_pd_read(pd);
1555         if (scq)
1556                 put_cq_read(scq);
1557         if (rcq && rcq != scq)
1558                 put_cq_read(rcq);
1559         if (srq)
1560                 put_srq_read(srq);
1561
1562         put_uobj_write(&obj->uevent.uobject);
1563         return ret;
1564 }
1565
1566 ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
1567                           const char __user *buf, int in_len, int out_len)
1568 {
1569         struct ib_uverbs_open_qp        cmd;
1570         struct ib_uverbs_create_qp_resp resp;
1571         struct ib_udata                 udata;
1572         struct ib_uqp_object           *obj;
1573         struct ib_xrcd                 *xrcd;
1574         struct ib_uobject              *uninitialized_var(xrcd_uobj);
1575         struct ib_qp                   *qp;
1576         struct ib_qp_open_attr          attr;
1577         int ret;
1578
1579         if (out_len < sizeof resp)
1580                 return -ENOSPC;
1581
1582         if (copy_from_user(&cmd, buf, sizeof cmd))
1583                 return -EFAULT;
1584
1585         INIT_UDATA(&udata, buf + sizeof cmd,
1586                    (unsigned long) cmd.response + sizeof resp,
1587                    in_len - sizeof cmd, out_len - sizeof resp);
1588
1589         obj = kmalloc(sizeof *obj, GFP_KERNEL);
1590         if (!obj)
1591                 return -ENOMEM;
1592
1593         init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
1594         down_write(&obj->uevent.uobject.mutex);
1595
1596         xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
1597         if (!xrcd) {
1598                 ret = -EINVAL;
1599                 goto err_put;
1600         }
1601
1602         attr.event_handler = ib_uverbs_qp_event_handler;
1603         attr.qp_context    = file;
1604         attr.qp_num        = cmd.qpn;
1605         attr.qp_type       = cmd.qp_type;
1606
1607         obj->uevent.events_reported = 0;
1608         INIT_LIST_HEAD(&obj->uevent.event_list);
1609         INIT_LIST_HEAD(&obj->mcast_list);
1610
1611         qp = ib_open_qp(xrcd, &attr);
1612         if (IS_ERR(qp)) {
1613                 ret = PTR_ERR(qp);
1614                 goto err_put;
1615         }
1616
1617         qp->uobject = &obj->uevent.uobject;
1618
1619         obj->uevent.uobject.object = qp;
1620         ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1621         if (ret)
1622                 goto err_destroy;
1623
1624         memset(&resp, 0, sizeof resp);
1625         resp.qpn       = qp->qp_num;
1626         resp.qp_handle = obj->uevent.uobject.id;
1627
1628         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1629                          &resp, sizeof resp)) {
1630                 ret = -EFAULT;
1631                 goto err_remove;
1632         }
1633
1634         put_xrcd_read(xrcd_uobj);
1635
1636         mutex_lock(&file->mutex);
1637         list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1638         mutex_unlock(&file->mutex);
1639
1640         obj->uevent.uobject.live = 1;
1641
1642         up_write(&obj->uevent.uobject.mutex);
1643
1644         return in_len;
1645
1646 err_remove:
1647         idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1648
1649 err_destroy:
1650         ib_destroy_qp(qp);
1651
1652 err_put:
1653         put_xrcd_read(xrcd_uobj);
1654         put_uobj_write(&obj->uevent.uobject);
1655         return ret;
1656 }
1657
1658 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1659                            const char __user *buf, int in_len,
1660                            int out_len)
1661 {
1662         struct ib_uverbs_query_qp      cmd;
1663         struct ib_uverbs_query_qp_resp resp;
1664         struct ib_qp                   *qp;
1665         struct ib_qp_attr              *attr;
1666         struct ib_qp_init_attr         *init_attr;
1667         int                            ret;
1668
1669         if (copy_from_user(&cmd, buf, sizeof cmd))
1670                 return -EFAULT;
1671
1672         attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1673         init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1674         if (!attr || !init_attr) {
1675                 ret = -ENOMEM;
1676                 goto out;
1677         }
1678
1679         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1680         if (!qp) {
1681                 ret = -EINVAL;
1682                 goto out;
1683         }
1684
1685         ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1686
1687         put_qp_read(qp);
1688
1689         if (ret)
1690                 goto out;
1691
1692         memset(&resp, 0, sizeof resp);
1693
1694         resp.qp_state               = attr->qp_state;
1695         resp.cur_qp_state           = attr->cur_qp_state;
1696         resp.path_mtu               = attr->path_mtu;
1697         resp.path_mig_state         = attr->path_mig_state;
1698         resp.qkey                   = attr->qkey;
1699         resp.rq_psn                 = attr->rq_psn;
1700         resp.sq_psn                 = attr->sq_psn;
1701         resp.dest_qp_num            = attr->dest_qp_num;
1702         resp.qp_access_flags        = attr->qp_access_flags;
1703         resp.pkey_index             = attr->pkey_index;
1704         resp.alt_pkey_index         = attr->alt_pkey_index;
1705         resp.sq_draining            = attr->sq_draining;
1706         resp.max_rd_atomic          = attr->max_rd_atomic;
1707         resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1708         resp.min_rnr_timer          = attr->min_rnr_timer;
1709         resp.port_num               = attr->port_num;
1710         resp.timeout                = attr->timeout;
1711         resp.retry_cnt              = attr->retry_cnt;
1712         resp.rnr_retry              = attr->rnr_retry;
1713         resp.alt_port_num           = attr->alt_port_num;
1714         resp.alt_timeout            = attr->alt_timeout;
1715
1716         memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1717         resp.dest.flow_label        = attr->ah_attr.grh.flow_label;
1718         resp.dest.sgid_index        = attr->ah_attr.grh.sgid_index;
1719         resp.dest.hop_limit         = attr->ah_attr.grh.hop_limit;
1720         resp.dest.traffic_class     = attr->ah_attr.grh.traffic_class;
1721         resp.dest.dlid              = attr->ah_attr.dlid;
1722         resp.dest.sl                = attr->ah_attr.sl;
1723         resp.dest.src_path_bits     = attr->ah_attr.src_path_bits;
1724         resp.dest.static_rate       = attr->ah_attr.static_rate;
1725         resp.dest.is_global         = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1726         resp.dest.port_num          = attr->ah_attr.port_num;
1727
1728         memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1729         resp.alt_dest.flow_label    = attr->alt_ah_attr.grh.flow_label;
1730         resp.alt_dest.sgid_index    = attr->alt_ah_attr.grh.sgid_index;
1731         resp.alt_dest.hop_limit     = attr->alt_ah_attr.grh.hop_limit;
1732         resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1733         resp.alt_dest.dlid          = attr->alt_ah_attr.dlid;
1734         resp.alt_dest.sl            = attr->alt_ah_attr.sl;
1735         resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1736         resp.alt_dest.static_rate   = attr->alt_ah_attr.static_rate;
1737         resp.alt_dest.is_global     = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1738         resp.alt_dest.port_num      = attr->alt_ah_attr.port_num;
1739
1740         resp.max_send_wr            = init_attr->cap.max_send_wr;
1741         resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1742         resp.max_send_sge           = init_attr->cap.max_send_sge;
1743         resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1744         resp.max_inline_data        = init_attr->cap.max_inline_data;
1745         resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1746
1747         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1748                          &resp, sizeof resp))
1749                 ret = -EFAULT;
1750
1751 out:
1752         kfree(attr);
1753         kfree(init_attr);
1754
1755         return ret ? ret : in_len;
1756 }
1757
1758 /* Remove ignored fields set in the attribute mask */
1759 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1760 {
1761         switch (qp_type) {
1762         case IB_QPT_XRC_INI:
1763                 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1764         case IB_QPT_XRC_TGT:
1765                 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1766                                 IB_QP_RNR_RETRY);
1767         default:
1768                 return mask;
1769         }
1770 }
1771
1772 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1773                             const char __user *buf, int in_len,
1774                             int out_len)
1775 {
1776         struct ib_uverbs_modify_qp cmd;
1777         struct ib_udata            udata;
1778         struct ib_qp              *qp;
1779         struct ib_qp_attr         *attr;
1780         int                        ret;
1781
1782         if (copy_from_user(&cmd, buf, sizeof cmd))
1783                 return -EFAULT;
1784
1785         INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1786                    out_len);
1787
1788         attr = kmalloc(sizeof *attr, GFP_KERNEL);
1789         if (!attr)
1790                 return -ENOMEM;
1791
1792         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1793         if (!qp) {
1794                 ret = -EINVAL;
1795                 goto out;
1796         }
1797
1798         attr->qp_state            = cmd.qp_state;
1799         attr->cur_qp_state        = cmd.cur_qp_state;
1800         attr->path_mtu            = cmd.path_mtu;
1801         attr->path_mig_state      = cmd.path_mig_state;
1802         attr->qkey                = cmd.qkey;
1803         attr->rq_psn              = cmd.rq_psn;
1804         attr->sq_psn              = cmd.sq_psn;
1805         attr->dest_qp_num         = cmd.dest_qp_num;
1806         attr->qp_access_flags     = cmd.qp_access_flags;
1807         attr->pkey_index          = cmd.pkey_index;
1808         attr->alt_pkey_index      = cmd.alt_pkey_index;
1809         attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1810         attr->max_rd_atomic       = cmd.max_rd_atomic;
1811         attr->max_dest_rd_atomic  = cmd.max_dest_rd_atomic;
1812         attr->min_rnr_timer       = cmd.min_rnr_timer;
1813         attr->port_num            = cmd.port_num;
1814         attr->timeout             = cmd.timeout;
1815         attr->retry_cnt           = cmd.retry_cnt;
1816         attr->rnr_retry           = cmd.rnr_retry;
1817         attr->alt_port_num        = cmd.alt_port_num;
1818         attr->alt_timeout         = cmd.alt_timeout;
1819
1820         memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1821         attr->ah_attr.grh.flow_label        = cmd.dest.flow_label;
1822         attr->ah_attr.grh.sgid_index        = cmd.dest.sgid_index;
1823         attr->ah_attr.grh.hop_limit         = cmd.dest.hop_limit;
1824         attr->ah_attr.grh.traffic_class     = cmd.dest.traffic_class;
1825         attr->ah_attr.dlid                  = cmd.dest.dlid;
1826         attr->ah_attr.sl                    = cmd.dest.sl;
1827         attr->ah_attr.src_path_bits         = cmd.dest.src_path_bits;
1828         attr->ah_attr.static_rate           = cmd.dest.static_rate;
1829         attr->ah_attr.ah_flags              = cmd.dest.is_global ? IB_AH_GRH : 0;
1830         attr->ah_attr.port_num              = cmd.dest.port_num;
1831
1832         memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1833         attr->alt_ah_attr.grh.flow_label    = cmd.alt_dest.flow_label;
1834         attr->alt_ah_attr.grh.sgid_index    = cmd.alt_dest.sgid_index;
1835         attr->alt_ah_attr.grh.hop_limit     = cmd.alt_dest.hop_limit;
1836         attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1837         attr->alt_ah_attr.dlid              = cmd.alt_dest.dlid;
1838         attr->alt_ah_attr.sl                = cmd.alt_dest.sl;
1839         attr->alt_ah_attr.src_path_bits     = cmd.alt_dest.src_path_bits;
1840         attr->alt_ah_attr.static_rate       = cmd.alt_dest.static_rate;
1841         attr->alt_ah_attr.ah_flags          = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1842         attr->alt_ah_attr.port_num          = cmd.alt_dest.port_num;
1843
1844         if (qp->real_qp == qp) {
1845                 ret = qp->device->modify_qp(qp, attr,
1846                         modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
1847         } else {
1848                 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
1849         }
1850
1851         put_qp_read(qp);
1852
1853         if (ret)
1854                 goto out;
1855
1856         ret = in_len;
1857
1858 out:
1859         kfree(attr);
1860
1861         return ret;
1862 }
1863
1864 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1865                              const char __user *buf, int in_len,
1866                              int out_len)
1867 {
1868         struct ib_uverbs_destroy_qp      cmd;
1869         struct ib_uverbs_destroy_qp_resp resp;
1870         struct ib_uobject               *uobj;
1871         struct ib_qp                    *qp;
1872         struct ib_uqp_object            *obj;
1873         int                              ret = -EINVAL;
1874
1875         if (copy_from_user(&cmd, buf, sizeof cmd))
1876                 return -EFAULT;
1877
1878         memset(&resp, 0, sizeof resp);
1879
1880         uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1881         if (!uobj)
1882                 return -EINVAL;
1883         qp  = uobj->object;
1884         obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1885
1886         if (!list_empty(&obj->mcast_list)) {
1887                 put_uobj_write(uobj);
1888                 return -EBUSY;
1889         }
1890
1891         ret = ib_destroy_qp(qp);
1892         if (!ret)
1893                 uobj->live = 0;
1894
1895         put_uobj_write(uobj);
1896
1897         if (ret)
1898                 return ret;
1899
1900         idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
1901
1902         mutex_lock(&file->mutex);
1903         list_del(&uobj->list);
1904         mutex_unlock(&file->mutex);
1905
1906         ib_uverbs_release_uevent(file, &obj->uevent);
1907
1908         resp.events_reported = obj->uevent.events_reported;
1909
1910         put_uobj(uobj);
1911
1912         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1913                          &resp, sizeof resp))
1914                 return -EFAULT;
1915
1916         return in_len;
1917 }
1918
1919 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1920                             const char __user *buf, int in_len,
1921                             int out_len)
1922 {
1923         struct ib_uverbs_post_send      cmd;
1924         struct ib_uverbs_post_send_resp resp;
1925         struct ib_uverbs_send_wr       *user_wr;
1926         struct ib_send_wr              *wr = NULL, *last, *next, *bad_wr;
1927         struct ib_qp                   *qp;
1928         int                             i, sg_ind;
1929         int                             is_ud;
1930         ssize_t                         ret = -EINVAL;
1931
1932         if (copy_from_user(&cmd, buf, sizeof cmd))
1933                 return -EFAULT;
1934
1935         if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1936             cmd.sge_count * sizeof (struct ib_uverbs_sge))
1937                 return -EINVAL;
1938
1939         if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1940                 return -EINVAL;
1941
1942         user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1943         if (!user_wr)
1944                 return -ENOMEM;
1945
1946         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1947         if (!qp)
1948                 goto out;
1949
1950         is_ud = qp->qp_type == IB_QPT_UD;
1951         sg_ind = 0;
1952         last = NULL;
1953         for (i = 0; i < cmd.wr_count; ++i) {
1954                 if (copy_from_user(user_wr,
1955                                    buf + sizeof cmd + i * cmd.wqe_size,
1956                                    cmd.wqe_size)) {
1957                         ret = -EFAULT;
1958                         goto out_put;
1959                 }
1960
1961                 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1962                         ret = -EINVAL;
1963                         goto out_put;
1964                 }
1965
1966                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1967                                user_wr->num_sge * sizeof (struct ib_sge),
1968                                GFP_KERNEL);
1969                 if (!next) {
1970                         ret = -ENOMEM;
1971                         goto out_put;
1972                 }
1973
1974                 if (!last)
1975                         wr = next;
1976                 else
1977                         last->next = next;
1978                 last = next;
1979
1980                 next->next       = NULL;
1981                 next->wr_id      = user_wr->wr_id;
1982                 next->num_sge    = user_wr->num_sge;
1983                 next->opcode     = user_wr->opcode;
1984                 next->send_flags = user_wr->send_flags;
1985
1986                 if (is_ud) {
1987                         next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1988                                                      file->ucontext);
1989                         if (!next->wr.ud.ah) {
1990                                 ret = -EINVAL;
1991                                 goto out_put;
1992                         }
1993                         next->wr.ud.remote_qpn  = user_wr->wr.ud.remote_qpn;
1994                         next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1995                 } else {
1996                         switch (next->opcode) {
1997                         case IB_WR_RDMA_WRITE_WITH_IMM:
1998                                 next->ex.imm_data =
1999                                         (__be32 __force) user_wr->ex.imm_data;
2000                         case IB_WR_RDMA_WRITE:
2001                         case IB_WR_RDMA_READ:
2002                                 next->wr.rdma.remote_addr =
2003                                         user_wr->wr.rdma.remote_addr;
2004                                 next->wr.rdma.rkey        =
2005                                         user_wr->wr.rdma.rkey;
2006                                 break;
2007                         case IB_WR_SEND_WITH_IMM:
2008                                 next->ex.imm_data =
2009                                         (__be32 __force) user_wr->ex.imm_data;
2010                                 break;
2011                         case IB_WR_SEND_WITH_INV:
2012                                 next->ex.invalidate_rkey =
2013                                         user_wr->ex.invalidate_rkey;
2014                                 break;
2015                         case IB_WR_ATOMIC_CMP_AND_SWP:
2016                         case IB_WR_ATOMIC_FETCH_AND_ADD:
2017                                 next->wr.atomic.remote_addr =
2018                                         user_wr->wr.atomic.remote_addr;
2019                                 next->wr.atomic.compare_add =
2020                                         user_wr->wr.atomic.compare_add;
2021                                 next->wr.atomic.swap = user_wr->wr.atomic.swap;
2022                                 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
2023                                 break;
2024                         default:
2025                                 break;
2026                         }
2027                 }
2028
2029                 if (next->num_sge) {
2030                         next->sg_list = (void *) next +
2031                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
2032                         if (copy_from_user(next->sg_list,
2033                                            buf + sizeof cmd +
2034                                            cmd.wr_count * cmd.wqe_size +
2035                                            sg_ind * sizeof (struct ib_sge),
2036                                            next->num_sge * sizeof (struct ib_sge))) {
2037                                 ret = -EFAULT;
2038                                 goto out_put;
2039                         }
2040                         sg_ind += next->num_sge;
2041                 } else
2042                         next->sg_list = NULL;
2043         }
2044
2045         resp.bad_wr = 0;
2046         ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
2047         if (ret)
2048                 for (next = wr; next; next = next->next) {
2049                         ++resp.bad_wr;
2050                         if (next == bad_wr)
2051                                 break;
2052                 }
2053
2054         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2055                          &resp, sizeof resp))
2056                 ret = -EFAULT;
2057
2058 out_put:
2059         put_qp_read(qp);
2060
2061         while (wr) {
2062                 if (is_ud && wr->wr.ud.ah)
2063                         put_ah_read(wr->wr.ud.ah);
2064                 next = wr->next;
2065                 kfree(wr);
2066                 wr = next;
2067         }
2068
2069 out:
2070         kfree(user_wr);
2071
2072         return ret ? ret : in_len;
2073 }
2074
2075 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2076                                                     int in_len,
2077                                                     u32 wr_count,
2078                                                     u32 sge_count,
2079                                                     u32 wqe_size)
2080 {
2081         struct ib_uverbs_recv_wr *user_wr;
2082         struct ib_recv_wr        *wr = NULL, *last, *next;
2083         int                       sg_ind;
2084         int                       i;
2085         int                       ret;
2086
2087         if (in_len < wqe_size * wr_count +
2088             sge_count * sizeof (struct ib_uverbs_sge))
2089                 return ERR_PTR(-EINVAL);
2090
2091         if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2092                 return ERR_PTR(-EINVAL);
2093
2094         user_wr = kmalloc(wqe_size, GFP_KERNEL);
2095         if (!user_wr)
2096                 return ERR_PTR(-ENOMEM);
2097
2098         sg_ind = 0;
2099         last = NULL;
2100         for (i = 0; i < wr_count; ++i) {
2101                 if (copy_from_user(user_wr, buf + i * wqe_size,
2102                                    wqe_size)) {
2103                         ret = -EFAULT;
2104                         goto err;
2105                 }
2106
2107                 if (user_wr->num_sge + sg_ind > sge_count) {
2108                         ret = -EINVAL;
2109                         goto err;
2110                 }
2111
2112                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2113                                user_wr->num_sge * sizeof (struct ib_sge),
2114                                GFP_KERNEL);
2115                 if (!next) {
2116                         ret = -ENOMEM;
2117                         goto err;
2118                 }
2119
2120                 if (!last)
2121                         wr = next;
2122                 else
2123                         last->next = next;
2124                 last = next;
2125
2126                 next->next       = NULL;
2127                 next->wr_id      = user_wr->wr_id;
2128                 next->num_sge    = user_wr->num_sge;
2129
2130                 if (next->num_sge) {
2131                         next->sg_list = (void *) next +
2132                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
2133                         if (copy_from_user(next->sg_list,
2134                                            buf + wr_count * wqe_size +
2135                                            sg_ind * sizeof (struct ib_sge),
2136                                            next->num_sge * sizeof (struct ib_sge))) {
2137                                 ret = -EFAULT;
2138                                 goto err;
2139                         }
2140                         sg_ind += next->num_sge;
2141                 } else
2142                         next->sg_list = NULL;
2143         }
2144
2145         kfree(user_wr);
2146         return wr;
2147
2148 err:
2149         kfree(user_wr);
2150
2151         while (wr) {
2152                 next = wr->next;
2153                 kfree(wr);
2154                 wr = next;
2155         }
2156
2157         return ERR_PTR(ret);
2158 }
2159
2160 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
2161                             const char __user *buf, int in_len,
2162                             int out_len)
2163 {
2164         struct ib_uverbs_post_recv      cmd;
2165         struct ib_uverbs_post_recv_resp resp;
2166         struct ib_recv_wr              *wr, *next, *bad_wr;
2167         struct ib_qp                   *qp;
2168         ssize_t                         ret = -EINVAL;
2169
2170         if (copy_from_user(&cmd, buf, sizeof cmd))
2171                 return -EFAULT;
2172
2173         wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2174                                        in_len - sizeof cmd, cmd.wr_count,
2175                                        cmd.sge_count, cmd.wqe_size);
2176         if (IS_ERR(wr))
2177                 return PTR_ERR(wr);
2178
2179         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2180         if (!qp)
2181                 goto out;
2182
2183         resp.bad_wr = 0;
2184         ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
2185
2186         put_qp_read(qp);
2187
2188         if (ret)
2189                 for (next = wr; next; next = next->next) {
2190                         ++resp.bad_wr;
2191                         if (next == bad_wr)
2192                                 break;
2193                 }
2194
2195         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2196                          &resp, sizeof resp))
2197                 ret = -EFAULT;
2198
2199 out:
2200         while (wr) {
2201                 next = wr->next;
2202                 kfree(wr);
2203                 wr = next;
2204         }
2205
2206         return ret ? ret : in_len;
2207 }
2208
2209 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
2210                                 const char __user *buf, int in_len,
2211                                 int out_len)
2212 {
2213         struct ib_uverbs_post_srq_recv      cmd;
2214         struct ib_uverbs_post_srq_recv_resp resp;
2215         struct ib_recv_wr                  *wr, *next, *bad_wr;
2216         struct ib_srq                      *srq;
2217         ssize_t                             ret = -EINVAL;
2218
2219         if (copy_from_user(&cmd, buf, sizeof cmd))
2220                 return -EFAULT;
2221
2222         wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2223                                        in_len - sizeof cmd, cmd.wr_count,
2224                                        cmd.sge_count, cmd.wqe_size);
2225         if (IS_ERR(wr))
2226                 return PTR_ERR(wr);
2227
2228         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2229         if (!srq)
2230                 goto out;
2231
2232         resp.bad_wr = 0;
2233         ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
2234
2235         put_srq_read(srq);
2236
2237         if (ret)
2238                 for (next = wr; next; next = next->next) {
2239                         ++resp.bad_wr;
2240                         if (next == bad_wr)
2241                                 break;
2242                 }
2243
2244         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2245                          &resp, sizeof resp))
2246                 ret = -EFAULT;
2247
2248 out:
2249         while (wr) {
2250                 next = wr->next;
2251                 kfree(wr);
2252                 wr = next;
2253         }
2254
2255         return ret ? ret : in_len;
2256 }
2257
2258 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
2259                             const char __user *buf, int in_len,
2260                             int out_len)
2261 {
2262         struct ib_uverbs_create_ah       cmd;
2263         struct ib_uverbs_create_ah_resp  resp;
2264         struct ib_uobject               *uobj;
2265         struct ib_pd                    *pd;
2266         struct ib_ah                    *ah;
2267         struct ib_ah_attr               attr;
2268         int ret;
2269
2270         if (out_len < sizeof resp)
2271                 return -ENOSPC;
2272
2273         if (copy_from_user(&cmd, buf, sizeof cmd))
2274                 return -EFAULT;
2275
2276         uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2277         if (!uobj)
2278                 return -ENOMEM;
2279
2280         init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
2281         down_write(&uobj->mutex);
2282
2283         pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2284         if (!pd) {
2285                 ret = -EINVAL;
2286                 goto err;
2287         }
2288
2289         attr.dlid              = cmd.attr.dlid;
2290         attr.sl                = cmd.attr.sl;
2291         attr.src_path_bits     = cmd.attr.src_path_bits;
2292         attr.static_rate       = cmd.attr.static_rate;
2293         attr.ah_flags          = cmd.attr.is_global ? IB_AH_GRH : 0;
2294         attr.port_num          = cmd.attr.port_num;
2295         attr.grh.flow_label    = cmd.attr.grh.flow_label;
2296         attr.grh.sgid_index    = cmd.attr.grh.sgid_index;
2297         attr.grh.hop_limit     = cmd.attr.grh.hop_limit;
2298         attr.grh.traffic_class = cmd.attr.grh.traffic_class;
2299         memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2300
2301         ah = ib_create_ah(pd, &attr);
2302         if (IS_ERR(ah)) {
2303                 ret = PTR_ERR(ah);
2304                 goto err_put;
2305         }
2306
2307         ah->uobject  = uobj;
2308         uobj->object = ah;
2309
2310         ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
2311         if (ret)
2312                 goto err_destroy;
2313
2314         resp.ah_handle = uobj->id;
2315
2316         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2317                          &resp, sizeof resp)) {
2318                 ret = -EFAULT;
2319                 goto err_copy;
2320         }
2321
2322         put_pd_read(pd);
2323
2324         mutex_lock(&file->mutex);
2325         list_add_tail(&uobj->list, &file->ucontext->ah_list);
2326         mutex_unlock(&file->mutex);
2327
2328         uobj->live = 1;
2329
2330         up_write(&uobj->mutex);
2331
2332         return in_len;
2333
2334 err_copy:
2335         idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2336
2337 err_destroy:
2338         ib_destroy_ah(ah);
2339
2340 err_put:
2341         put_pd_read(pd);
2342
2343 err:
2344         put_uobj_write(uobj);
2345         return ret;
2346 }
2347
2348 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
2349                              const char __user *buf, int in_len, int out_len)
2350 {
2351         struct ib_uverbs_destroy_ah cmd;
2352         struct ib_ah               *ah;
2353         struct ib_uobject          *uobj;
2354         int                         ret;
2355
2356         if (copy_from_user(&cmd, buf, sizeof cmd))
2357                 return -EFAULT;
2358
2359         uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2360         if (!uobj)
2361                 return -EINVAL;
2362         ah = uobj->object;
2363
2364         ret = ib_destroy_ah(ah);
2365         if (!ret)
2366                 uobj->live = 0;
2367
2368         put_uobj_write(uobj);
2369
2370         if (ret)
2371                 return ret;
2372
2373         idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
2374
2375         mutex_lock(&file->mutex);
2376         list_del(&uobj->list);
2377         mutex_unlock(&file->mutex);
2378
2379         put_uobj(uobj);
2380
2381         return in_len;
2382 }
2383
2384 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
2385                                const char __user *buf, int in_len,
2386                                int out_len)
2387 {
2388         struct ib_uverbs_attach_mcast cmd;
2389         struct ib_qp                 *qp;
2390         struct ib_uqp_object         *obj;
2391         struct ib_uverbs_mcast_entry *mcast;
2392         int                           ret;
2393
2394         if (copy_from_user(&cmd, buf, sizeof cmd))
2395                 return -EFAULT;
2396
2397         qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2398         if (!qp)
2399                 return -EINVAL;
2400
2401         obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2402
2403         list_for_each_entry(mcast, &obj->mcast_list, list)
2404                 if (cmd.mlid == mcast->lid &&
2405                     !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2406                         ret = 0;
2407                         goto out_put;
2408                 }
2409
2410         mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2411         if (!mcast) {
2412                 ret = -ENOMEM;
2413                 goto out_put;
2414         }
2415
2416         mcast->lid = cmd.mlid;
2417         memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2418
2419         ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2420         if (!ret)
2421                 list_add_tail(&mcast->list, &obj->mcast_list);
2422         else
2423                 kfree(mcast);
2424
2425 out_put:
2426         put_qp_write(qp);
2427
2428         return ret ? ret : in_len;
2429 }
2430
2431 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
2432                                const char __user *buf, int in_len,
2433                                int out_len)
2434 {
2435         struct ib_uverbs_detach_mcast cmd;
2436         struct ib_uqp_object         *obj;
2437         struct ib_qp                 *qp;
2438         struct ib_uverbs_mcast_entry *mcast;
2439         int                           ret = -EINVAL;
2440
2441         if (copy_from_user(&cmd, buf, sizeof cmd))
2442                 return -EFAULT;
2443
2444         qp = idr_write_qp(cmd.qp_handle, file->ucontext);
2445         if (!qp)
2446                 return -EINVAL;
2447
2448         ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2449         if (ret)
2450                 goto out_put;
2451
2452         obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
2453
2454         list_for_each_entry(mcast, &obj->mcast_list, list)
2455                 if (cmd.mlid == mcast->lid &&
2456                     !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2457                         list_del(&mcast->list);
2458                         kfree(mcast);
2459                         break;
2460                 }
2461
2462 out_put:
2463         put_qp_write(qp);
2464
2465         return ret ? ret : in_len;
2466 }
2467
2468 static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
2469                                 struct ib_uverbs_create_xsrq *cmd,
2470                                 struct ib_udata *udata)
2471 {
2472         struct ib_uverbs_create_srq_resp resp;
2473         struct ib_usrq_object           *obj;
2474         struct ib_pd                    *pd;
2475         struct ib_srq                   *srq;
2476         struct ib_uobject               *uninitialized_var(xrcd_uobj);
2477         struct ib_srq_init_attr          attr;
2478         int ret;
2479
2480         obj = kmalloc(sizeof *obj, GFP_KERNEL);
2481         if (!obj)
2482                 return -ENOMEM;
2483
2484         init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
2485         down_write(&obj->uevent.uobject.mutex);
2486
2487         pd  = idr_read_pd(cmd->pd_handle, file->ucontext);
2488         if (!pd) {
2489                 ret = -EINVAL;
2490                 goto err;
2491         }
2492
2493         if (cmd->srq_type == IB_SRQT_XRC) {
2494                 attr.ext.xrc.cq  = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
2495                 if (!attr.ext.xrc.cq) {
2496                         ret = -EINVAL;
2497                         goto err_put_pd;
2498                 }
2499
2500                 attr.ext.xrc.xrcd  = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
2501                 if (!attr.ext.xrc.xrcd) {
2502                         ret = -EINVAL;
2503                         goto err_put_cq;
2504                 }
2505
2506                 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2507                 atomic_inc(&obj->uxrcd->refcnt);
2508         }
2509
2510         attr.event_handler  = ib_uverbs_srq_event_handler;
2511         attr.srq_context    = file;
2512         attr.srq_type       = cmd->srq_type;
2513         attr.attr.max_wr    = cmd->max_wr;
2514         attr.attr.max_sge   = cmd->max_sge;
2515         attr.attr.srq_limit = cmd->srq_limit;
2516
2517         obj->uevent.events_reported = 0;
2518         INIT_LIST_HEAD(&obj->uevent.event_list);
2519
2520         srq = pd->device->create_srq(pd, &attr, udata);
2521         if (IS_ERR(srq)) {
2522                 ret = PTR_ERR(srq);
2523                 goto err_put;
2524         }
2525
2526         srq->device        = pd->device;
2527         srq->pd            = pd;
2528         srq->srq_type      = cmd->srq_type;
2529         srq->uobject       = &obj->uevent.uobject;
2530         srq->event_handler = attr.event_handler;
2531         srq->srq_context   = attr.srq_context;
2532
2533         if (cmd->srq_type == IB_SRQT_XRC) {
2534                 srq->ext.xrc.cq   = attr.ext.xrc.cq;
2535                 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
2536                 atomic_inc(&attr.ext.xrc.cq->usecnt);
2537                 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
2538         }
2539
2540         atomic_inc(&pd->usecnt);
2541         atomic_set(&srq->usecnt, 0);
2542
2543         obj->uevent.uobject.object = srq;
2544         ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2545         if (ret)
2546                 goto err_destroy;
2547
2548         memset(&resp, 0, sizeof resp);
2549         resp.srq_handle = obj->uevent.uobject.id;
2550         resp.max_wr     = attr.attr.max_wr;
2551         resp.max_sge    = attr.attr.max_sge;
2552         if (cmd->srq_type == IB_SRQT_XRC)
2553                 resp.srqn = srq->ext.xrc.srq_num;
2554
2555         if (copy_to_user((void __user *) (unsigned long) cmd->response,
2556                          &resp, sizeof resp)) {
2557                 ret = -EFAULT;
2558                 goto err_copy;
2559         }
2560
2561         if (cmd->srq_type == IB_SRQT_XRC) {
2562                 put_uobj_read(xrcd_uobj);
2563                 put_cq_read(attr.ext.xrc.cq);
2564         }
2565         put_pd_read(pd);
2566
2567         mutex_lock(&file->mutex);
2568         list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
2569         mutex_unlock(&file->mutex);
2570
2571         obj->uevent.uobject.live = 1;
2572
2573         up_write(&obj->uevent.uobject.mutex);
2574
2575         return 0;
2576
2577 err_copy:
2578         idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
2579
2580 err_destroy:
2581         ib_destroy_srq(srq);
2582
2583 err_put:
2584         if (cmd->srq_type == IB_SRQT_XRC) {
2585                 atomic_dec(&obj->uxrcd->refcnt);
2586                 put_uobj_read(xrcd_uobj);
2587         }
2588
2589 err_put_cq:
2590         if (cmd->srq_type == IB_SRQT_XRC)
2591                 put_cq_read(attr.ext.xrc.cq);
2592
2593 err_put_pd:
2594         put_pd_read(pd);
2595
2596 err:
2597         put_uobj_write(&obj->uevent.uobject);
2598         return ret;
2599 }
2600
2601 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
2602                              const char __user *buf, int in_len,
2603                              int out_len)
2604 {
2605         struct ib_uverbs_create_srq      cmd;
2606         struct ib_uverbs_create_xsrq     xcmd;
2607         struct ib_uverbs_create_srq_resp resp;
2608         struct ib_udata                  udata;
2609         int ret;
2610
2611         if (out_len < sizeof resp)
2612                 return -ENOSPC;
2613
2614         if (copy_from_user(&cmd, buf, sizeof cmd))
2615                 return -EFAULT;
2616
2617         xcmd.response    = cmd.response;
2618         xcmd.user_handle = cmd.user_handle;
2619         xcmd.srq_type    = IB_SRQT_BASIC;
2620         xcmd.pd_handle   = cmd.pd_handle;
2621         xcmd.max_wr      = cmd.max_wr;
2622         xcmd.max_sge     = cmd.max_sge;
2623         xcmd.srq_limit   = cmd.srq_limit;
2624
2625         INIT_UDATA(&udata, buf + sizeof cmd,
2626                    (unsigned long) cmd.response + sizeof resp,
2627                    in_len - sizeof cmd, out_len - sizeof resp);
2628
2629         ret = __uverbs_create_xsrq(file, &xcmd, &udata);
2630         if (ret)
2631                 return ret;
2632
2633         return in_len;
2634 }
2635
2636 ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
2637                               const char __user *buf, int in_len, int out_len)
2638 {
2639         struct ib_uverbs_create_xsrq     cmd;
2640         struct ib_uverbs_create_srq_resp resp;
2641         struct ib_udata                  udata;
2642         int ret;
2643
2644         if (out_len < sizeof resp)
2645                 return -ENOSPC;
2646
2647         if (copy_from_user(&cmd, buf, sizeof cmd))
2648                 return -EFAULT;
2649
2650         INIT_UDATA(&udata, buf + sizeof cmd,
2651                    (unsigned long) cmd.response + sizeof resp,
2652                    in_len - sizeof cmd, out_len - sizeof resp);
2653
2654         ret = __uverbs_create_xsrq(file, &cmd, &udata);
2655         if (ret)
2656                 return ret;
2657
2658         return in_len;
2659 }
2660
2661 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2662                              const char __user *buf, int in_len,
2663                              int out_len)
2664 {
2665         struct ib_uverbs_modify_srq cmd;
2666         struct ib_udata             udata;
2667         struct ib_srq              *srq;
2668         struct ib_srq_attr          attr;
2669         int                         ret;
2670
2671         if (copy_from_user(&cmd, buf, sizeof cmd))
2672                 return -EFAULT;
2673
2674         INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2675                    out_len);
2676
2677         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2678         if (!srq)
2679                 return -EINVAL;
2680
2681         attr.max_wr    = cmd.max_wr;
2682         attr.srq_limit = cmd.srq_limit;
2683
2684         ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
2685
2686         put_srq_read(srq);
2687
2688         return ret ? ret : in_len;
2689 }
2690
2691 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2692                             const char __user *buf,
2693                             int in_len, int out_len)
2694 {
2695         struct ib_uverbs_query_srq      cmd;
2696         struct ib_uverbs_query_srq_resp resp;
2697         struct ib_srq_attr              attr;
2698         struct ib_srq                   *srq;
2699         int                             ret;
2700
2701         if (out_len < sizeof resp)
2702                 return -ENOSPC;
2703
2704         if (copy_from_user(&cmd, buf, sizeof cmd))
2705                 return -EFAULT;
2706
2707         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2708         if (!srq)
2709                 return -EINVAL;
2710
2711         ret = ib_query_srq(srq, &attr);
2712
2713         put_srq_read(srq);
2714
2715         if (ret)
2716                 return ret;
2717
2718         memset(&resp, 0, sizeof resp);
2719
2720         resp.max_wr    = attr.max_wr;
2721         resp.max_sge   = attr.max_sge;
2722         resp.srq_limit = attr.srq_limit;
2723
2724         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2725                          &resp, sizeof resp))
2726                 return -EFAULT;
2727
2728         return in_len;
2729 }
2730
2731 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2732                               const char __user *buf, int in_len,
2733                               int out_len)
2734 {
2735         struct ib_uverbs_destroy_srq      cmd;
2736         struct ib_uverbs_destroy_srq_resp resp;
2737         struct ib_uobject                *uobj;
2738         struct ib_srq                    *srq;
2739         struct ib_uevent_object          *obj;
2740         int                               ret = -EINVAL;
2741
2742         if (copy_from_user(&cmd, buf, sizeof cmd))
2743                 return -EFAULT;
2744
2745         uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2746         if (!uobj)
2747                 return -EINVAL;
2748         srq = uobj->object;
2749         obj = container_of(uobj, struct ib_uevent_object, uobject);
2750
2751         ret = ib_destroy_srq(srq);
2752         if (!ret)
2753                 uobj->live = 0;
2754
2755         put_uobj_write(uobj);
2756
2757         if (ret)
2758                 return ret;
2759
2760         idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2761
2762         mutex_lock(&file->mutex);
2763         list_del(&uobj->list);
2764         mutex_unlock(&file->mutex);
2765
2766         ib_uverbs_release_uevent(file, obj);
2767
2768         memset(&resp, 0, sizeof resp);
2769         resp.events_reported = obj->events_reported;
2770
2771         put_uobj(uobj);
2772
2773         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2774                          &resp, sizeof resp))
2775                 ret = -EFAULT;
2776
2777         return ret ? ret : in_len;
2778 }