Merge rsync://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / infiniband / core / uverbs_cmd.c
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 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  * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
36  */
37
38 #include <linux/file.h>
39 #include <linux/fs.h>
40
41 #include <asm/uaccess.h>
42
43 #include "uverbs.h"
44
45 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen)                       \
46         do {                                                            \
47                 (udata)->inbuf  = (void __user *) (ibuf);               \
48                 (udata)->outbuf = (void __user *) (obuf);               \
49                 (udata)->inlen  = (ilen);                               \
50                 (udata)->outlen = (olen);                               \
51         } while (0)
52
53 /*
54  * The ib_uobject locking scheme is as follows:
55  *
56  * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
57  *   needs to be held during all idr operations.  When an object is
58  *   looked up, a reference must be taken on the object's kref before
59  *   dropping this lock.
60  *
61  * - Each object also has an rwsem.  This rwsem must be held for
62  *   reading while an operation that uses the object is performed.
63  *   For example, while registering an MR, the associated PD's
64  *   uobject.mutex must be held for reading.  The rwsem must be held
65  *   for writing while initializing or destroying an object.
66  *
67  * - In addition, each object has a "live" flag.  If this flag is not
68  *   set, then lookups of the object will fail even if it is found in
69  *   the idr.  This handles a reader that blocks and does not acquire
70  *   the rwsem until after the object is destroyed.  The destroy
71  *   operation will set the live flag to 0 and then drop the rwsem;
72  *   this will allow the reader to acquire the rwsem, see that the
73  *   live flag is 0, and then drop the rwsem and its reference to
74  *   object.  The underlying storage will not be freed until the last
75  *   reference to the object is dropped.
76  */
77
78 static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
79                       struct ib_ucontext *context)
80 {
81         uobj->user_handle = user_handle;
82         uobj->context     = context;
83         kref_init(&uobj->ref);
84         init_rwsem(&uobj->mutex);
85         uobj->live        = 0;
86 }
87
88 static void release_uobj(struct kref *kref)
89 {
90         kfree(container_of(kref, struct ib_uobject, ref));
91 }
92
93 static void put_uobj(struct ib_uobject *uobj)
94 {
95         kref_put(&uobj->ref, release_uobj);
96 }
97
98 static void put_uobj_read(struct ib_uobject *uobj)
99 {
100         up_read(&uobj->mutex);
101         put_uobj(uobj);
102 }
103
104 static void put_uobj_write(struct ib_uobject *uobj)
105 {
106         up_write(&uobj->mutex);
107         put_uobj(uobj);
108 }
109
110 static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
111 {
112         int ret;
113
114 retry:
115         if (!idr_pre_get(idr, GFP_KERNEL))
116                 return -ENOMEM;
117
118         spin_lock(&ib_uverbs_idr_lock);
119         ret = idr_get_new(idr, uobj, &uobj->id);
120         spin_unlock(&ib_uverbs_idr_lock);
121
122         if (ret == -EAGAIN)
123                 goto retry;
124
125         return ret;
126 }
127
128 void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
129 {
130         spin_lock(&ib_uverbs_idr_lock);
131         idr_remove(idr, uobj->id);
132         spin_unlock(&ib_uverbs_idr_lock);
133 }
134
135 static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
136                                          struct ib_ucontext *context)
137 {
138         struct ib_uobject *uobj;
139
140         spin_lock(&ib_uverbs_idr_lock);
141         uobj = idr_find(idr, id);
142         if (uobj)
143                 kref_get(&uobj->ref);
144         spin_unlock(&ib_uverbs_idr_lock);
145
146         return uobj;
147 }
148
149 static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
150                                         struct ib_ucontext *context)
151 {
152         struct ib_uobject *uobj;
153
154         uobj = __idr_get_uobj(idr, id, context);
155         if (!uobj)
156                 return NULL;
157
158         down_read(&uobj->mutex);
159         if (!uobj->live) {
160                 put_uobj_read(uobj);
161                 return NULL;
162         }
163
164         return uobj;
165 }
166
167 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
168                                          struct ib_ucontext *context)
169 {
170         struct ib_uobject *uobj;
171
172         uobj = __idr_get_uobj(idr, id, context);
173         if (!uobj)
174                 return NULL;
175
176         down_write(&uobj->mutex);
177         if (!uobj->live) {
178                 put_uobj_write(uobj);
179                 return NULL;
180         }
181
182         return uobj;
183 }
184
185 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context)
186 {
187         struct ib_uobject *uobj;
188
189         uobj = idr_read_uobj(idr, id, context);
190         return uobj ? uobj->object : NULL;
191 }
192
193 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
194 {
195         return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context);
196 }
197
198 static void put_pd_read(struct ib_pd *pd)
199 {
200         put_uobj_read(pd->uobject);
201 }
202
203 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context)
204 {
205         return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context);
206 }
207
208 static void put_cq_read(struct ib_cq *cq)
209 {
210         put_uobj_read(cq->uobject);
211 }
212
213 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
214 {
215         return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context);
216 }
217
218 static void put_ah_read(struct ib_ah *ah)
219 {
220         put_uobj_read(ah->uobject);
221 }
222
223 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
224 {
225         return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context);
226 }
227
228 static void put_qp_read(struct ib_qp *qp)
229 {
230         put_uobj_read(qp->uobject);
231 }
232
233 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
234 {
235         return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context);
236 }
237
238 static void put_srq_read(struct ib_srq *srq)
239 {
240         put_uobj_read(srq->uobject);
241 }
242
243 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
244                               const char __user *buf,
245                               int in_len, int out_len)
246 {
247         struct ib_uverbs_get_context      cmd;
248         struct ib_uverbs_get_context_resp resp;
249         struct ib_udata                   udata;
250         struct ib_device                 *ibdev = file->device->ib_dev;
251         struct ib_ucontext               *ucontext;
252         struct file                      *filp;
253         int ret;
254
255         if (out_len < sizeof resp)
256                 return -ENOSPC;
257
258         if (copy_from_user(&cmd, buf, sizeof cmd))
259                 return -EFAULT;
260
261         mutex_lock(&file->mutex);
262
263         if (file->ucontext) {
264                 ret = -EINVAL;
265                 goto err;
266         }
267
268         INIT_UDATA(&udata, buf + sizeof cmd,
269                    (unsigned long) cmd.response + sizeof resp,
270                    in_len - sizeof cmd, out_len - sizeof resp);
271
272         ucontext = ibdev->alloc_ucontext(ibdev, &udata);
273         if (IS_ERR(ucontext)) {
274                 ret = PTR_ERR(file->ucontext);
275                 goto err;
276         }
277
278         ucontext->device = ibdev;
279         INIT_LIST_HEAD(&ucontext->pd_list);
280         INIT_LIST_HEAD(&ucontext->mr_list);
281         INIT_LIST_HEAD(&ucontext->mw_list);
282         INIT_LIST_HEAD(&ucontext->cq_list);
283         INIT_LIST_HEAD(&ucontext->qp_list);
284         INIT_LIST_HEAD(&ucontext->srq_list);
285         INIT_LIST_HEAD(&ucontext->ah_list);
286
287         resp.num_comp_vectors = file->device->num_comp_vectors;
288
289         filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
290         if (IS_ERR(filp)) {
291                 ret = PTR_ERR(filp);
292                 goto err_free;
293         }
294
295         if (copy_to_user((void __user *) (unsigned long) cmd.response,
296                          &resp, sizeof resp)) {
297                 ret = -EFAULT;
298                 goto err_file;
299         }
300
301         file->async_file = filp->private_data;
302
303         INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
304                               ib_uverbs_event_handler);
305         ret = ib_register_event_handler(&file->event_handler);
306         if (ret)
307                 goto err_file;
308
309         kref_get(&file->async_file->ref);
310         kref_get(&file->ref);
311         file->ucontext = ucontext;
312
313         fd_install(resp.async_fd, filp);
314
315         mutex_unlock(&file->mutex);
316
317         return in_len;
318
319 err_file:
320         put_unused_fd(resp.async_fd);
321         fput(filp);
322
323 err_free:
324         ibdev->dealloc_ucontext(ucontext);
325
326 err:
327         mutex_unlock(&file->mutex);
328         return ret;
329 }
330
331 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
332                                const char __user *buf,
333                                int in_len, int out_len)
334 {
335         struct ib_uverbs_query_device      cmd;
336         struct ib_uverbs_query_device_resp resp;
337         struct ib_device_attr              attr;
338         int                                ret;
339
340         if (out_len < sizeof resp)
341                 return -ENOSPC;
342
343         if (copy_from_user(&cmd, buf, sizeof cmd))
344                 return -EFAULT;
345
346         ret = ib_query_device(file->device->ib_dev, &attr);
347         if (ret)
348                 return ret;
349
350         memset(&resp, 0, sizeof resp);
351
352         resp.fw_ver                    = attr.fw_ver;
353         resp.node_guid                 = file->device->ib_dev->node_guid;
354         resp.sys_image_guid            = attr.sys_image_guid;
355         resp.max_mr_size               = attr.max_mr_size;
356         resp.page_size_cap             = attr.page_size_cap;
357         resp.vendor_id                 = attr.vendor_id;
358         resp.vendor_part_id            = attr.vendor_part_id;
359         resp.hw_ver                    = attr.hw_ver;
360         resp.max_qp                    = attr.max_qp;
361         resp.max_qp_wr                 = attr.max_qp_wr;
362         resp.device_cap_flags          = attr.device_cap_flags;
363         resp.max_sge                   = attr.max_sge;
364         resp.max_sge_rd                = attr.max_sge_rd;
365         resp.max_cq                    = attr.max_cq;
366         resp.max_cqe                   = attr.max_cqe;
367         resp.max_mr                    = attr.max_mr;
368         resp.max_pd                    = attr.max_pd;
369         resp.max_qp_rd_atom            = attr.max_qp_rd_atom;
370         resp.max_ee_rd_atom            = attr.max_ee_rd_atom;
371         resp.max_res_rd_atom           = attr.max_res_rd_atom;
372         resp.max_qp_init_rd_atom       = attr.max_qp_init_rd_atom;
373         resp.max_ee_init_rd_atom       = attr.max_ee_init_rd_atom;
374         resp.atomic_cap                = attr.atomic_cap;
375         resp.max_ee                    = attr.max_ee;
376         resp.max_rdd                   = attr.max_rdd;
377         resp.max_mw                    = attr.max_mw;
378         resp.max_raw_ipv6_qp           = attr.max_raw_ipv6_qp;
379         resp.max_raw_ethy_qp           = attr.max_raw_ethy_qp;
380         resp.max_mcast_grp             = attr.max_mcast_grp;
381         resp.max_mcast_qp_attach       = attr.max_mcast_qp_attach;
382         resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
383         resp.max_ah                    = attr.max_ah;
384         resp.max_fmr                   = attr.max_fmr;
385         resp.max_map_per_fmr           = attr.max_map_per_fmr;
386         resp.max_srq                   = attr.max_srq;
387         resp.max_srq_wr                = attr.max_srq_wr;
388         resp.max_srq_sge               = attr.max_srq_sge;
389         resp.max_pkeys                 = attr.max_pkeys;
390         resp.local_ca_ack_delay        = attr.local_ca_ack_delay;
391         resp.phys_port_cnt             = file->device->ib_dev->phys_port_cnt;
392
393         if (copy_to_user((void __user *) (unsigned long) cmd.response,
394                          &resp, sizeof resp))
395                 return -EFAULT;
396
397         return in_len;
398 }
399
400 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
401                              const char __user *buf,
402                              int in_len, int out_len)
403 {
404         struct ib_uverbs_query_port      cmd;
405         struct ib_uverbs_query_port_resp resp;
406         struct ib_port_attr              attr;
407         int                              ret;
408
409         if (out_len < sizeof resp)
410                 return -ENOSPC;
411
412         if (copy_from_user(&cmd, buf, sizeof cmd))
413                 return -EFAULT;
414
415         ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
416         if (ret)
417                 return ret;
418
419         memset(&resp, 0, sizeof resp);
420
421         resp.state           = attr.state;
422         resp.max_mtu         = attr.max_mtu;
423         resp.active_mtu      = attr.active_mtu;
424         resp.gid_tbl_len     = attr.gid_tbl_len;
425         resp.port_cap_flags  = attr.port_cap_flags;
426         resp.max_msg_sz      = attr.max_msg_sz;
427         resp.bad_pkey_cntr   = attr.bad_pkey_cntr;
428         resp.qkey_viol_cntr  = attr.qkey_viol_cntr;
429         resp.pkey_tbl_len    = attr.pkey_tbl_len;
430         resp.lid             = attr.lid;
431         resp.sm_lid          = attr.sm_lid;
432         resp.lmc             = attr.lmc;
433         resp.max_vl_num      = attr.max_vl_num;
434         resp.sm_sl           = attr.sm_sl;
435         resp.subnet_timeout  = attr.subnet_timeout;
436         resp.init_type_reply = attr.init_type_reply;
437         resp.active_width    = attr.active_width;
438         resp.active_speed    = attr.active_speed;
439         resp.phys_state      = attr.phys_state;
440
441         if (copy_to_user((void __user *) (unsigned long) cmd.response,
442                          &resp, sizeof resp))
443                 return -EFAULT;
444
445         return in_len;
446 }
447
448 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
449                            const char __user *buf,
450                            int in_len, int out_len)
451 {
452         struct ib_uverbs_alloc_pd      cmd;
453         struct ib_uverbs_alloc_pd_resp resp;
454         struct ib_udata                udata;
455         struct ib_uobject             *uobj;
456         struct ib_pd                  *pd;
457         int                            ret;
458
459         if (out_len < sizeof resp)
460                 return -ENOSPC;
461
462         if (copy_from_user(&cmd, buf, sizeof cmd))
463                 return -EFAULT;
464
465         INIT_UDATA(&udata, buf + sizeof cmd,
466                    (unsigned long) cmd.response + sizeof resp,
467                    in_len - sizeof cmd, out_len - sizeof resp);
468
469         uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
470         if (!uobj)
471                 return -ENOMEM;
472
473         init_uobj(uobj, 0, file->ucontext);
474         down_write(&uobj->mutex);
475
476         pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
477                                             file->ucontext, &udata);
478         if (IS_ERR(pd)) {
479                 ret = PTR_ERR(pd);
480                 goto err;
481         }
482
483         pd->device  = file->device->ib_dev;
484         pd->uobject = uobj;
485         atomic_set(&pd->usecnt, 0);
486
487         uobj->object = pd;
488         ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
489         if (ret)
490                 goto err_idr;
491
492         memset(&resp, 0, sizeof resp);
493         resp.pd_handle = uobj->id;
494
495         if (copy_to_user((void __user *) (unsigned long) cmd.response,
496                          &resp, sizeof resp)) {
497                 ret = -EFAULT;
498                 goto err_copy;
499         }
500
501         mutex_lock(&file->mutex);
502         list_add_tail(&uobj->list, &file->ucontext->pd_list);
503         mutex_unlock(&file->mutex);
504
505         uobj->live = 1;
506
507         up_write(&uobj->mutex);
508
509         return in_len;
510
511 err_copy:
512         idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
513
514 err_idr:
515         ib_dealloc_pd(pd);
516
517 err:
518         put_uobj_write(uobj);
519         return ret;
520 }
521
522 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
523                              const char __user *buf,
524                              int in_len, int out_len)
525 {
526         struct ib_uverbs_dealloc_pd cmd;
527         struct ib_uobject          *uobj;
528         int                         ret;
529
530         if (copy_from_user(&cmd, buf, sizeof cmd))
531                 return -EFAULT;
532
533         uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
534         if (!uobj)
535                 return -EINVAL;
536
537         ret = ib_dealloc_pd(uobj->object);
538         if (!ret)
539                 uobj->live = 0;
540
541         put_uobj_write(uobj);
542
543         if (ret)
544                 return ret;
545
546         idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
547
548         mutex_lock(&file->mutex);
549         list_del(&uobj->list);
550         mutex_unlock(&file->mutex);
551
552         put_uobj(uobj);
553
554         return in_len;
555 }
556
557 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
558                          const char __user *buf, int in_len,
559                          int out_len)
560 {
561         struct ib_uverbs_reg_mr      cmd;
562         struct ib_uverbs_reg_mr_resp resp;
563         struct ib_udata              udata;
564         struct ib_umem_object       *obj;
565         struct ib_pd                *pd;
566         struct ib_mr                *mr;
567         int                          ret;
568
569         if (out_len < sizeof resp)
570                 return -ENOSPC;
571
572         if (copy_from_user(&cmd, buf, sizeof cmd))
573                 return -EFAULT;
574
575         INIT_UDATA(&udata, buf + sizeof cmd,
576                    (unsigned long) cmd.response + sizeof resp,
577                    in_len - sizeof cmd, out_len - sizeof resp);
578
579         if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
580                 return -EINVAL;
581
582         /*
583          * Local write permission is required if remote write or
584          * remote atomic permission is also requested.
585          */
586         if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
587             !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
588                 return -EINVAL;
589
590         obj = kmalloc(sizeof *obj, GFP_KERNEL);
591         if (!obj)
592                 return -ENOMEM;
593
594         init_uobj(&obj->uobject, 0, file->ucontext);
595         down_write(&obj->uobject.mutex);
596
597         /*
598          * We ask for writable memory if any access flags other than
599          * "remote read" are set.  "Local write" and "remote write"
600          * obviously require write access.  "Remote atomic" can do
601          * things like fetch and add, which will modify memory, and
602          * "MW bind" can change permissions by binding a window.
603          */
604         ret = ib_umem_get(file->device->ib_dev, &obj->umem,
605                           (void *) (unsigned long) cmd.start, cmd.length,
606                           !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
607         if (ret)
608                 goto err_free;
609
610         obj->umem.virt_base = cmd.hca_va;
611
612         pd = idr_read_pd(cmd.pd_handle, file->ucontext);
613         if (!pd)
614                 goto err_release;
615
616         mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
617         if (IS_ERR(mr)) {
618                 ret = PTR_ERR(mr);
619                 goto err_put;
620         }
621
622         mr->device  = pd->device;
623         mr->pd      = pd;
624         mr->uobject = &obj->uobject;
625         atomic_inc(&pd->usecnt);
626         atomic_set(&mr->usecnt, 0);
627
628         obj->uobject.object = mr;
629         ret = idr_add_uobj(&ib_uverbs_mr_idr, &obj->uobject);
630         if (ret)
631                 goto err_unreg;
632
633         memset(&resp, 0, sizeof resp);
634         resp.lkey      = mr->lkey;
635         resp.rkey      = mr->rkey;
636         resp.mr_handle = obj->uobject.id;
637
638         if (copy_to_user((void __user *) (unsigned long) cmd.response,
639                          &resp, sizeof resp)) {
640                 ret = -EFAULT;
641                 goto err_copy;
642         }
643
644         put_pd_read(pd);
645
646         mutex_lock(&file->mutex);
647         list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
648         mutex_unlock(&file->mutex);
649
650         obj->uobject.live = 1;
651
652         up_write(&obj->uobject.mutex);
653
654         return in_len;
655
656 err_copy:
657         idr_remove_uobj(&ib_uverbs_mr_idr, &obj->uobject);
658
659 err_unreg:
660         ib_dereg_mr(mr);
661
662 err_put:
663         put_pd_read(pd);
664
665 err_release:
666         ib_umem_release(file->device->ib_dev, &obj->umem);
667
668 err_free:
669         put_uobj_write(&obj->uobject);
670         return ret;
671 }
672
673 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
674                            const char __user *buf, int in_len,
675                            int out_len)
676 {
677         struct ib_uverbs_dereg_mr cmd;
678         struct ib_mr             *mr;
679         struct ib_uobject        *uobj;
680         struct ib_umem_object    *memobj;
681         int                       ret = -EINVAL;
682
683         if (copy_from_user(&cmd, buf, sizeof cmd))
684                 return -EFAULT;
685
686         uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
687         if (!uobj)
688                 return -EINVAL;
689
690         memobj = container_of(uobj, struct ib_umem_object, uobject);
691         mr     = uobj->object;
692
693         ret = ib_dereg_mr(mr);
694         if (!ret)
695                 uobj->live = 0;
696
697         put_uobj_write(uobj);
698
699         if (ret)
700                 return ret;
701
702         idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
703
704         mutex_lock(&file->mutex);
705         list_del(&uobj->list);
706         mutex_unlock(&file->mutex);
707
708         ib_umem_release(file->device->ib_dev, &memobj->umem);
709
710         put_uobj(uobj);
711
712         return in_len;
713 }
714
715 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
716                                       const char __user *buf, int in_len,
717                                       int out_len)
718 {
719         struct ib_uverbs_create_comp_channel       cmd;
720         struct ib_uverbs_create_comp_channel_resp  resp;
721         struct file                               *filp;
722
723         if (out_len < sizeof resp)
724                 return -ENOSPC;
725
726         if (copy_from_user(&cmd, buf, sizeof cmd))
727                 return -EFAULT;
728
729         filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
730         if (IS_ERR(filp))
731                 return PTR_ERR(filp);
732
733         if (copy_to_user((void __user *) (unsigned long) cmd.response,
734                          &resp, sizeof resp)) {
735                 put_unused_fd(resp.fd);
736                 fput(filp);
737                 return -EFAULT;
738         }
739
740         fd_install(resp.fd, filp);
741         return in_len;
742 }
743
744 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
745                             const char __user *buf, int in_len,
746                             int out_len)
747 {
748         struct ib_uverbs_create_cq      cmd;
749         struct ib_uverbs_create_cq_resp resp;
750         struct ib_udata                 udata;
751         struct ib_ucq_object           *obj;
752         struct ib_uverbs_event_file    *ev_file = NULL;
753         struct ib_cq                   *cq;
754         int                             ret;
755
756         if (out_len < sizeof resp)
757                 return -ENOSPC;
758
759         if (copy_from_user(&cmd, buf, sizeof cmd))
760                 return -EFAULT;
761
762         INIT_UDATA(&udata, buf + sizeof cmd,
763                    (unsigned long) cmd.response + sizeof resp,
764                    in_len - sizeof cmd, out_len - sizeof resp);
765
766         if (cmd.comp_vector >= file->device->num_comp_vectors)
767                 return -EINVAL;
768
769         obj = kmalloc(sizeof *obj, GFP_KERNEL);
770         if (!obj)
771                 return -ENOMEM;
772
773         init_uobj(&obj->uobject, cmd.user_handle, file->ucontext);
774         down_write(&obj->uobject.mutex);
775
776         if (cmd.comp_channel >= 0) {
777                 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
778                 if (!ev_file) {
779                         ret = -EINVAL;
780                         goto err;
781                 }
782         }
783
784         obj->uverbs_file           = file;
785         obj->comp_events_reported  = 0;
786         obj->async_events_reported = 0;
787         INIT_LIST_HEAD(&obj->comp_list);
788         INIT_LIST_HEAD(&obj->async_list);
789
790         cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
791                                              file->ucontext, &udata);
792         if (IS_ERR(cq)) {
793                 ret = PTR_ERR(cq);
794                 goto err_file;
795         }
796
797         cq->device        = file->device->ib_dev;
798         cq->uobject       = &obj->uobject;
799         cq->comp_handler  = ib_uverbs_comp_handler;
800         cq->event_handler = ib_uverbs_cq_event_handler;
801         cq->cq_context    = ev_file;
802         atomic_set(&cq->usecnt, 0);
803
804         obj->uobject.object = cq;
805         ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
806         if (ret)
807                 goto err_free;
808
809         memset(&resp, 0, sizeof resp);
810         resp.cq_handle = obj->uobject.id;
811         resp.cqe       = cq->cqe;
812
813         if (copy_to_user((void __user *) (unsigned long) cmd.response,
814                          &resp, sizeof resp)) {
815                 ret = -EFAULT;
816                 goto err_copy;
817         }
818
819         mutex_lock(&file->mutex);
820         list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
821         mutex_unlock(&file->mutex);
822
823         obj->uobject.live = 1;
824
825         up_write(&obj->uobject.mutex);
826
827         return in_len;
828
829 err_copy:
830         idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
831
832
833 err_free:
834         ib_destroy_cq(cq);
835
836 err_file:
837         if (ev_file)
838                 ib_uverbs_release_ucq(file, ev_file, obj);
839
840 err:
841         put_uobj_write(&obj->uobject);
842         return ret;
843 }
844
845 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
846                             const char __user *buf, int in_len,
847                             int out_len)
848 {
849         struct ib_uverbs_resize_cq      cmd;
850         struct ib_uverbs_resize_cq_resp resp;
851         struct ib_udata                 udata;
852         struct ib_cq                    *cq;
853         int                             ret = -EINVAL;
854
855         if (copy_from_user(&cmd, buf, sizeof cmd))
856                 return -EFAULT;
857
858         INIT_UDATA(&udata, buf + sizeof cmd,
859                    (unsigned long) cmd.response + sizeof resp,
860                    in_len - sizeof cmd, out_len - sizeof resp);
861
862         cq = idr_read_cq(cmd.cq_handle, file->ucontext);
863         if (!cq)
864                 return -EINVAL;
865
866         ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
867         if (ret)
868                 goto out;
869
870         memset(&resp, 0, sizeof resp);
871         resp.cqe = cq->cqe;
872
873         if (copy_to_user((void __user *) (unsigned long) cmd.response,
874                          &resp, sizeof resp))
875                 ret = -EFAULT;
876
877 out:
878         put_cq_read(cq);
879
880         return ret ? ret : in_len;
881 }
882
883 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
884                           const char __user *buf, int in_len,
885                           int out_len)
886 {
887         struct ib_uverbs_poll_cq       cmd;
888         struct ib_uverbs_poll_cq_resp *resp;
889         struct ib_uobject             *uobj;
890         struct ib_cq                  *cq;
891         struct ib_wc                  *wc;
892         int                            ret = 0;
893         int                            i;
894         int                            rsize;
895
896         if (copy_from_user(&cmd, buf, sizeof cmd))
897                 return -EFAULT;
898
899         wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
900         if (!wc)
901                 return -ENOMEM;
902
903         rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
904         resp = kmalloc(rsize, GFP_KERNEL);
905         if (!resp) {
906                 ret = -ENOMEM;
907                 goto out_wc;
908         }
909
910         uobj = idr_read_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
911         if (!uobj) {
912                 ret = -EINVAL;
913                 goto out;
914         }
915         cq = uobj->object;
916
917         resp->count = ib_poll_cq(cq, cmd.ne, wc);
918
919         put_uobj_read(uobj);
920
921         for (i = 0; i < resp->count; i++) {
922                 resp->wc[i].wr_id          = wc[i].wr_id;
923                 resp->wc[i].status         = wc[i].status;
924                 resp->wc[i].opcode         = wc[i].opcode;
925                 resp->wc[i].vendor_err     = wc[i].vendor_err;
926                 resp->wc[i].byte_len       = wc[i].byte_len;
927                 resp->wc[i].imm_data       = (__u32 __force) wc[i].imm_data;
928                 resp->wc[i].qp_num         = wc[i].qp_num;
929                 resp->wc[i].src_qp         = wc[i].src_qp;
930                 resp->wc[i].wc_flags       = wc[i].wc_flags;
931                 resp->wc[i].pkey_index     = wc[i].pkey_index;
932                 resp->wc[i].slid           = wc[i].slid;
933                 resp->wc[i].sl             = wc[i].sl;
934                 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
935                 resp->wc[i].port_num       = wc[i].port_num;
936         }
937
938         if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
939                 ret = -EFAULT;
940
941 out:
942         kfree(resp);
943
944 out_wc:
945         kfree(wc);
946         return ret ? ret : in_len;
947 }
948
949 ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
950                                 const char __user *buf, int in_len,
951                                 int out_len)
952 {
953         struct ib_uverbs_req_notify_cq cmd;
954         struct ib_uobject             *uobj;
955         struct ib_cq                  *cq;
956
957         if (copy_from_user(&cmd, buf, sizeof cmd))
958                 return -EFAULT;
959
960         uobj = idr_read_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
961         if (!uobj)
962                 return -EINVAL;
963         cq = uobj->object;
964
965         ib_req_notify_cq(cq, cmd.solicited_only ?
966                          IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
967
968         put_uobj_read(uobj);
969
970         return in_len;
971 }
972
973 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
974                              const char __user *buf, int in_len,
975                              int out_len)
976 {
977         struct ib_uverbs_destroy_cq      cmd;
978         struct ib_uverbs_destroy_cq_resp resp;
979         struct ib_uobject               *uobj;
980         struct ib_cq                    *cq;
981         struct ib_ucq_object            *obj;
982         struct ib_uverbs_event_file     *ev_file;
983         int                              ret = -EINVAL;
984
985         if (copy_from_user(&cmd, buf, sizeof cmd))
986                 return -EFAULT;
987
988         uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
989         if (!uobj)
990                 return -EINVAL;
991         cq      = uobj->object;
992         ev_file = cq->cq_context;
993         obj     = container_of(cq->uobject, struct ib_ucq_object, uobject);
994
995         ret = ib_destroy_cq(cq);
996         if (!ret)
997                 uobj->live = 0;
998
999         put_uobj_write(uobj);
1000
1001         if (ret)
1002                 return ret;
1003
1004         idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1005
1006         mutex_lock(&file->mutex);
1007         list_del(&uobj->list);
1008         mutex_unlock(&file->mutex);
1009
1010         ib_uverbs_release_ucq(file, ev_file, obj);
1011
1012         memset(&resp, 0, sizeof resp);
1013         resp.comp_events_reported  = obj->comp_events_reported;
1014         resp.async_events_reported = obj->async_events_reported;
1015
1016         put_uobj(uobj);
1017
1018         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1019                          &resp, sizeof resp))
1020                 return -EFAULT;
1021
1022         return in_len;
1023 }
1024
1025 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1026                             const char __user *buf, int in_len,
1027                             int out_len)
1028 {
1029         struct ib_uverbs_create_qp      cmd;
1030         struct ib_uverbs_create_qp_resp resp;
1031         struct ib_udata                 udata;
1032         struct ib_uqp_object           *obj;
1033         struct ib_pd                   *pd;
1034         struct ib_cq                   *scq, *rcq;
1035         struct ib_srq                  *srq;
1036         struct ib_qp                   *qp;
1037         struct ib_qp_init_attr          attr;
1038         int ret;
1039
1040         if (out_len < sizeof resp)
1041                 return -ENOSPC;
1042
1043         if (copy_from_user(&cmd, buf, sizeof cmd))
1044                 return -EFAULT;
1045
1046         INIT_UDATA(&udata, buf + sizeof cmd,
1047                    (unsigned long) cmd.response + sizeof resp,
1048                    in_len - sizeof cmd, out_len - sizeof resp);
1049
1050         obj = kmalloc(sizeof *obj, GFP_KERNEL);
1051         if (!obj)
1052                 return -ENOMEM;
1053
1054         init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext);
1055         down_write(&obj->uevent.uobject.mutex);
1056
1057         pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
1058         scq = idr_read_cq(cmd.send_cq_handle, file->ucontext);
1059         rcq = idr_read_cq(cmd.recv_cq_handle, file->ucontext);
1060         srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
1061
1062         if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
1063                 ret = -EINVAL;
1064                 goto err_put;
1065         }
1066
1067         attr.event_handler = ib_uverbs_qp_event_handler;
1068         attr.qp_context    = file;
1069         attr.send_cq       = scq;
1070         attr.recv_cq       = rcq;
1071         attr.srq           = srq;
1072         attr.sq_sig_type   = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1073         attr.qp_type       = cmd.qp_type;
1074
1075         attr.cap.max_send_wr     = cmd.max_send_wr;
1076         attr.cap.max_recv_wr     = cmd.max_recv_wr;
1077         attr.cap.max_send_sge    = cmd.max_send_sge;
1078         attr.cap.max_recv_sge    = cmd.max_recv_sge;
1079         attr.cap.max_inline_data = cmd.max_inline_data;
1080
1081         obj->uevent.events_reported     = 0;
1082         INIT_LIST_HEAD(&obj->uevent.event_list);
1083         INIT_LIST_HEAD(&obj->mcast_list);
1084
1085         qp = pd->device->create_qp(pd, &attr, &udata);
1086         if (IS_ERR(qp)) {
1087                 ret = PTR_ERR(qp);
1088                 goto err_put;
1089         }
1090
1091         qp->device        = pd->device;
1092         qp->pd            = pd;
1093         qp->send_cq       = attr.send_cq;
1094         qp->recv_cq       = attr.recv_cq;
1095         qp->srq           = attr.srq;
1096         qp->uobject       = &obj->uevent.uobject;
1097         qp->event_handler = attr.event_handler;
1098         qp->qp_context    = attr.qp_context;
1099         qp->qp_type       = attr.qp_type;
1100         atomic_inc(&pd->usecnt);
1101         atomic_inc(&attr.send_cq->usecnt);
1102         atomic_inc(&attr.recv_cq->usecnt);
1103         if (attr.srq)
1104                 atomic_inc(&attr.srq->usecnt);
1105
1106         obj->uevent.uobject.object = qp;
1107         ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1108         if (ret)
1109                 goto err_destroy;
1110
1111         memset(&resp, 0, sizeof resp);
1112         resp.qpn             = qp->qp_num;
1113         resp.qp_handle       = obj->uevent.uobject.id;
1114         resp.max_recv_sge    = attr.cap.max_recv_sge;
1115         resp.max_send_sge    = attr.cap.max_send_sge;
1116         resp.max_recv_wr     = attr.cap.max_recv_wr;
1117         resp.max_send_wr     = attr.cap.max_send_wr;
1118         resp.max_inline_data = attr.cap.max_inline_data;
1119
1120         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1121                          &resp, sizeof resp)) {
1122                 ret = -EFAULT;
1123                 goto err_copy;
1124         }
1125
1126         put_pd_read(pd);
1127         put_cq_read(scq);
1128         put_cq_read(rcq);
1129         if (srq)
1130                 put_srq_read(srq);
1131
1132         mutex_lock(&file->mutex);
1133         list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1134         mutex_unlock(&file->mutex);
1135
1136         obj->uevent.uobject.live = 1;
1137
1138         up_write(&obj->uevent.uobject.mutex);
1139
1140         return in_len;
1141
1142 err_copy:
1143         idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1144
1145 err_destroy:
1146         ib_destroy_qp(qp);
1147
1148 err_put:
1149         if (pd)
1150                 put_pd_read(pd);
1151         if (scq)
1152                 put_cq_read(scq);
1153         if (rcq)
1154                 put_cq_read(rcq);
1155         if (srq)
1156                 put_srq_read(srq);
1157
1158         put_uobj_write(&obj->uevent.uobject);
1159         return ret;
1160 }
1161
1162 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1163                            const char __user *buf, int in_len,
1164                            int out_len)
1165 {
1166         struct ib_uverbs_query_qp      cmd;
1167         struct ib_uverbs_query_qp_resp resp;
1168         struct ib_qp                   *qp;
1169         struct ib_qp_attr              *attr;
1170         struct ib_qp_init_attr         *init_attr;
1171         int                            ret;
1172
1173         if (copy_from_user(&cmd, buf, sizeof cmd))
1174                 return -EFAULT;
1175
1176         attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1177         init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1178         if (!attr || !init_attr) {
1179                 ret = -ENOMEM;
1180                 goto out;
1181         }
1182
1183         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1184         if (!qp) {
1185                 ret = -EINVAL;
1186                 goto out;
1187         }
1188
1189         ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1190
1191         put_qp_read(qp);
1192
1193         if (ret)
1194                 goto out;
1195
1196         memset(&resp, 0, sizeof resp);
1197
1198         resp.qp_state               = attr->qp_state;
1199         resp.cur_qp_state           = attr->cur_qp_state;
1200         resp.path_mtu               = attr->path_mtu;
1201         resp.path_mig_state         = attr->path_mig_state;
1202         resp.qkey                   = attr->qkey;
1203         resp.rq_psn                 = attr->rq_psn;
1204         resp.sq_psn                 = attr->sq_psn;
1205         resp.dest_qp_num            = attr->dest_qp_num;
1206         resp.qp_access_flags        = attr->qp_access_flags;
1207         resp.pkey_index             = attr->pkey_index;
1208         resp.alt_pkey_index         = attr->alt_pkey_index;
1209         resp.en_sqd_async_notify    = attr->en_sqd_async_notify;
1210         resp.max_rd_atomic          = attr->max_rd_atomic;
1211         resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1212         resp.min_rnr_timer          = attr->min_rnr_timer;
1213         resp.port_num               = attr->port_num;
1214         resp.timeout                = attr->timeout;
1215         resp.retry_cnt              = attr->retry_cnt;
1216         resp.rnr_retry              = attr->rnr_retry;
1217         resp.alt_port_num           = attr->alt_port_num;
1218         resp.alt_timeout            = attr->alt_timeout;
1219
1220         memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1221         resp.dest.flow_label        = attr->ah_attr.grh.flow_label;
1222         resp.dest.sgid_index        = attr->ah_attr.grh.sgid_index;
1223         resp.dest.hop_limit         = attr->ah_attr.grh.hop_limit;
1224         resp.dest.traffic_class     = attr->ah_attr.grh.traffic_class;
1225         resp.dest.dlid              = attr->ah_attr.dlid;
1226         resp.dest.sl                = attr->ah_attr.sl;
1227         resp.dest.src_path_bits     = attr->ah_attr.src_path_bits;
1228         resp.dest.static_rate       = attr->ah_attr.static_rate;
1229         resp.dest.is_global         = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1230         resp.dest.port_num          = attr->ah_attr.port_num;
1231
1232         memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1233         resp.alt_dest.flow_label    = attr->alt_ah_attr.grh.flow_label;
1234         resp.alt_dest.sgid_index    = attr->alt_ah_attr.grh.sgid_index;
1235         resp.alt_dest.hop_limit     = attr->alt_ah_attr.grh.hop_limit;
1236         resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1237         resp.alt_dest.dlid          = attr->alt_ah_attr.dlid;
1238         resp.alt_dest.sl            = attr->alt_ah_attr.sl;
1239         resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1240         resp.alt_dest.static_rate   = attr->alt_ah_attr.static_rate;
1241         resp.alt_dest.is_global     = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1242         resp.alt_dest.port_num      = attr->alt_ah_attr.port_num;
1243
1244         resp.max_send_wr            = init_attr->cap.max_send_wr;
1245         resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1246         resp.max_send_sge           = init_attr->cap.max_send_sge;
1247         resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1248         resp.max_inline_data        = init_attr->cap.max_inline_data;
1249         resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1250
1251         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1252                          &resp, sizeof resp))
1253                 ret = -EFAULT;
1254
1255 out:
1256         kfree(attr);
1257         kfree(init_attr);
1258
1259         return ret ? ret : in_len;
1260 }
1261
1262 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1263                             const char __user *buf, int in_len,
1264                             int out_len)
1265 {
1266         struct ib_uverbs_modify_qp cmd;
1267         struct ib_qp              *qp;
1268         struct ib_qp_attr         *attr;
1269         int                        ret;
1270
1271         if (copy_from_user(&cmd, buf, sizeof cmd))
1272                 return -EFAULT;
1273
1274         attr = kmalloc(sizeof *attr, GFP_KERNEL);
1275         if (!attr)
1276                 return -ENOMEM;
1277
1278         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1279         if (!qp) {
1280                 ret = -EINVAL;
1281                 goto out;
1282         }
1283
1284         attr->qp_state            = cmd.qp_state;
1285         attr->cur_qp_state        = cmd.cur_qp_state;
1286         attr->path_mtu            = cmd.path_mtu;
1287         attr->path_mig_state      = cmd.path_mig_state;
1288         attr->qkey                = cmd.qkey;
1289         attr->rq_psn              = cmd.rq_psn;
1290         attr->sq_psn              = cmd.sq_psn;
1291         attr->dest_qp_num         = cmd.dest_qp_num;
1292         attr->qp_access_flags     = cmd.qp_access_flags;
1293         attr->pkey_index          = cmd.pkey_index;
1294         attr->alt_pkey_index      = cmd.alt_pkey_index;
1295         attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1296         attr->max_rd_atomic       = cmd.max_rd_atomic;
1297         attr->max_dest_rd_atomic  = cmd.max_dest_rd_atomic;
1298         attr->min_rnr_timer       = cmd.min_rnr_timer;
1299         attr->port_num            = cmd.port_num;
1300         attr->timeout             = cmd.timeout;
1301         attr->retry_cnt           = cmd.retry_cnt;
1302         attr->rnr_retry           = cmd.rnr_retry;
1303         attr->alt_port_num        = cmd.alt_port_num;
1304         attr->alt_timeout         = cmd.alt_timeout;
1305
1306         memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1307         attr->ah_attr.grh.flow_label        = cmd.dest.flow_label;
1308         attr->ah_attr.grh.sgid_index        = cmd.dest.sgid_index;
1309         attr->ah_attr.grh.hop_limit         = cmd.dest.hop_limit;
1310         attr->ah_attr.grh.traffic_class     = cmd.dest.traffic_class;
1311         attr->ah_attr.dlid                  = cmd.dest.dlid;
1312         attr->ah_attr.sl                    = cmd.dest.sl;
1313         attr->ah_attr.src_path_bits         = cmd.dest.src_path_bits;
1314         attr->ah_attr.static_rate           = cmd.dest.static_rate;
1315         attr->ah_attr.ah_flags              = cmd.dest.is_global ? IB_AH_GRH : 0;
1316         attr->ah_attr.port_num              = cmd.dest.port_num;
1317
1318         memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1319         attr->alt_ah_attr.grh.flow_label    = cmd.alt_dest.flow_label;
1320         attr->alt_ah_attr.grh.sgid_index    = cmd.alt_dest.sgid_index;
1321         attr->alt_ah_attr.grh.hop_limit     = cmd.alt_dest.hop_limit;
1322         attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1323         attr->alt_ah_attr.dlid              = cmd.alt_dest.dlid;
1324         attr->alt_ah_attr.sl                = cmd.alt_dest.sl;
1325         attr->alt_ah_attr.src_path_bits     = cmd.alt_dest.src_path_bits;
1326         attr->alt_ah_attr.static_rate       = cmd.alt_dest.static_rate;
1327         attr->alt_ah_attr.ah_flags          = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1328         attr->alt_ah_attr.port_num          = cmd.alt_dest.port_num;
1329
1330         ret = ib_modify_qp(qp, attr, cmd.attr_mask);
1331
1332         put_qp_read(qp);
1333
1334         if (ret)
1335                 goto out;
1336
1337         ret = in_len;
1338
1339 out:
1340         kfree(attr);
1341
1342         return ret;
1343 }
1344
1345 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1346                              const char __user *buf, int in_len,
1347                              int out_len)
1348 {
1349         struct ib_uverbs_destroy_qp      cmd;
1350         struct ib_uverbs_destroy_qp_resp resp;
1351         struct ib_uobject               *uobj;
1352         struct ib_qp                    *qp;
1353         struct ib_uqp_object            *obj;
1354         int                              ret = -EINVAL;
1355
1356         if (copy_from_user(&cmd, buf, sizeof cmd))
1357                 return -EFAULT;
1358
1359         memset(&resp, 0, sizeof resp);
1360
1361         uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1362         if (!uobj)
1363                 return -EINVAL;
1364         qp  = uobj->object;
1365         obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1366
1367         if (!list_empty(&obj->mcast_list)) {
1368                 put_uobj_write(uobj);
1369                 return -EBUSY;
1370         }
1371
1372         ret = ib_destroy_qp(qp);
1373         if (!ret)
1374                 uobj->live = 0;
1375
1376         put_uobj_write(uobj);
1377
1378         if (ret)
1379                 return ret;
1380
1381         idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
1382
1383         mutex_lock(&file->mutex);
1384         list_del(&uobj->list);
1385         mutex_unlock(&file->mutex);
1386
1387         ib_uverbs_release_uevent(file, &obj->uevent);
1388
1389         resp.events_reported = obj->uevent.events_reported;
1390
1391         put_uobj(uobj);
1392
1393         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1394                          &resp, sizeof resp))
1395                 return -EFAULT;
1396
1397         return in_len;
1398 }
1399
1400 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1401                             const char __user *buf, int in_len,
1402                             int out_len)
1403 {
1404         struct ib_uverbs_post_send      cmd;
1405         struct ib_uverbs_post_send_resp resp;
1406         struct ib_uverbs_send_wr       *user_wr;
1407         struct ib_send_wr              *wr = NULL, *last, *next, *bad_wr;
1408         struct ib_qp                   *qp;
1409         int                             i, sg_ind;
1410         int                             is_ud;
1411         ssize_t                         ret = -EINVAL;
1412
1413         if (copy_from_user(&cmd, buf, sizeof cmd))
1414                 return -EFAULT;
1415
1416         if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1417             cmd.sge_count * sizeof (struct ib_uverbs_sge))
1418                 return -EINVAL;
1419
1420         if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1421                 return -EINVAL;
1422
1423         user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1424         if (!user_wr)
1425                 return -ENOMEM;
1426
1427         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1428         if (!qp)
1429                 goto out;
1430
1431         is_ud = qp->qp_type == IB_QPT_UD;
1432         sg_ind = 0;
1433         last = NULL;
1434         for (i = 0; i < cmd.wr_count; ++i) {
1435                 if (copy_from_user(user_wr,
1436                                    buf + sizeof cmd + i * cmd.wqe_size,
1437                                    cmd.wqe_size)) {
1438                         ret = -EFAULT;
1439                         goto out_put;
1440                 }
1441
1442                 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1443                         ret = -EINVAL;
1444                         goto out_put;
1445                 }
1446
1447                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1448                                user_wr->num_sge * sizeof (struct ib_sge),
1449                                GFP_KERNEL);
1450                 if (!next) {
1451                         ret = -ENOMEM;
1452                         goto out_put;
1453                 }
1454
1455                 if (!last)
1456                         wr = next;
1457                 else
1458                         last->next = next;
1459                 last = next;
1460
1461                 next->next       = NULL;
1462                 next->wr_id      = user_wr->wr_id;
1463                 next->num_sge    = user_wr->num_sge;
1464                 next->opcode     = user_wr->opcode;
1465                 next->send_flags = user_wr->send_flags;
1466                 next->imm_data   = (__be32 __force) user_wr->imm_data;
1467
1468                 if (is_ud) {
1469                         next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1470                                                      file->ucontext);
1471                         if (!next->wr.ud.ah) {
1472                                 ret = -EINVAL;
1473                                 goto out_put;
1474                         }
1475                         next->wr.ud.remote_qpn  = user_wr->wr.ud.remote_qpn;
1476                         next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1477                 } else {
1478                         switch (next->opcode) {
1479                         case IB_WR_RDMA_WRITE:
1480                         case IB_WR_RDMA_WRITE_WITH_IMM:
1481                         case IB_WR_RDMA_READ:
1482                                 next->wr.rdma.remote_addr =
1483                                         user_wr->wr.rdma.remote_addr;
1484                                 next->wr.rdma.rkey        =
1485                                         user_wr->wr.rdma.rkey;
1486                                 break;
1487                         case IB_WR_ATOMIC_CMP_AND_SWP:
1488                         case IB_WR_ATOMIC_FETCH_AND_ADD:
1489                                 next->wr.atomic.remote_addr =
1490                                         user_wr->wr.atomic.remote_addr;
1491                                 next->wr.atomic.compare_add =
1492                                         user_wr->wr.atomic.compare_add;
1493                                 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1494                                 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1495                                 break;
1496                         default:
1497                                 break;
1498                         }
1499                 }
1500
1501                 if (next->num_sge) {
1502                         next->sg_list = (void *) next +
1503                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
1504                         if (copy_from_user(next->sg_list,
1505                                            buf + sizeof cmd +
1506                                            cmd.wr_count * cmd.wqe_size +
1507                                            sg_ind * sizeof (struct ib_sge),
1508                                            next->num_sge * sizeof (struct ib_sge))) {
1509                                 ret = -EFAULT;
1510                                 goto out_put;
1511                         }
1512                         sg_ind += next->num_sge;
1513                 } else
1514                         next->sg_list = NULL;
1515         }
1516
1517         resp.bad_wr = 0;
1518         ret = qp->device->post_send(qp, wr, &bad_wr);
1519         if (ret)
1520                 for (next = wr; next; next = next->next) {
1521                         ++resp.bad_wr;
1522                         if (next == bad_wr)
1523                                 break;
1524                 }
1525
1526         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1527                          &resp, sizeof resp))
1528                 ret = -EFAULT;
1529
1530 out_put:
1531         put_qp_read(qp);
1532
1533         while (wr) {
1534                 if (is_ud && wr->wr.ud.ah)
1535                         put_ah_read(wr->wr.ud.ah);
1536                 next = wr->next;
1537                 kfree(wr);
1538                 wr = next;
1539         }
1540
1541 out:
1542         kfree(user_wr);
1543
1544         return ret ? ret : in_len;
1545 }
1546
1547 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1548                                                     int in_len,
1549                                                     u32 wr_count,
1550                                                     u32 sge_count,
1551                                                     u32 wqe_size)
1552 {
1553         struct ib_uverbs_recv_wr *user_wr;
1554         struct ib_recv_wr        *wr = NULL, *last, *next;
1555         int                       sg_ind;
1556         int                       i;
1557         int                       ret;
1558
1559         if (in_len < wqe_size * wr_count +
1560             sge_count * sizeof (struct ib_uverbs_sge))
1561                 return ERR_PTR(-EINVAL);
1562
1563         if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1564                 return ERR_PTR(-EINVAL);
1565
1566         user_wr = kmalloc(wqe_size, GFP_KERNEL);
1567         if (!user_wr)
1568                 return ERR_PTR(-ENOMEM);
1569
1570         sg_ind = 0;
1571         last = NULL;
1572         for (i = 0; i < wr_count; ++i) {
1573                 if (copy_from_user(user_wr, buf + i * wqe_size,
1574                                    wqe_size)) {
1575                         ret = -EFAULT;
1576                         goto err;
1577                 }
1578
1579                 if (user_wr->num_sge + sg_ind > sge_count) {
1580                         ret = -EINVAL;
1581                         goto err;
1582                 }
1583
1584                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1585                                user_wr->num_sge * sizeof (struct ib_sge),
1586                                GFP_KERNEL);
1587                 if (!next) {
1588                         ret = -ENOMEM;
1589                         goto err;
1590                 }
1591
1592                 if (!last)
1593                         wr = next;
1594                 else
1595                         last->next = next;
1596                 last = next;
1597
1598                 next->next       = NULL;
1599                 next->wr_id      = user_wr->wr_id;
1600                 next->num_sge    = user_wr->num_sge;
1601
1602                 if (next->num_sge) {
1603                         next->sg_list = (void *) next +
1604                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
1605                         if (copy_from_user(next->sg_list,
1606                                            buf + wr_count * wqe_size +
1607                                            sg_ind * sizeof (struct ib_sge),
1608                                            next->num_sge * sizeof (struct ib_sge))) {
1609                                 ret = -EFAULT;
1610                                 goto err;
1611                         }
1612                         sg_ind += next->num_sge;
1613                 } else
1614                         next->sg_list = NULL;
1615         }
1616
1617         kfree(user_wr);
1618         return wr;
1619
1620 err:
1621         kfree(user_wr);
1622
1623         while (wr) {
1624                 next = wr->next;
1625                 kfree(wr);
1626                 wr = next;
1627         }
1628
1629         return ERR_PTR(ret);
1630 }
1631
1632 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1633                             const char __user *buf, int in_len,
1634                             int out_len)
1635 {
1636         struct ib_uverbs_post_recv      cmd;
1637         struct ib_uverbs_post_recv_resp resp;
1638         struct ib_recv_wr              *wr, *next, *bad_wr;
1639         struct ib_qp                   *qp;
1640         ssize_t                         ret = -EINVAL;
1641
1642         if (copy_from_user(&cmd, buf, sizeof cmd))
1643                 return -EFAULT;
1644
1645         wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1646                                        in_len - sizeof cmd, cmd.wr_count,
1647                                        cmd.sge_count, cmd.wqe_size);
1648         if (IS_ERR(wr))
1649                 return PTR_ERR(wr);
1650
1651         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1652         if (!qp)
1653                 goto out;
1654
1655         resp.bad_wr = 0;
1656         ret = qp->device->post_recv(qp, wr, &bad_wr);
1657
1658         put_qp_read(qp);
1659
1660         if (ret)
1661                 for (next = wr; next; next = next->next) {
1662                         ++resp.bad_wr;
1663                         if (next == bad_wr)
1664                                 break;
1665                 }
1666
1667
1668         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1669                          &resp, sizeof resp))
1670                 ret = -EFAULT;
1671
1672 out:
1673         while (wr) {
1674                 next = wr->next;
1675                 kfree(wr);
1676                 wr = next;
1677         }
1678
1679         return ret ? ret : in_len;
1680 }
1681
1682 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1683                                 const char __user *buf, int in_len,
1684                                 int out_len)
1685 {
1686         struct ib_uverbs_post_srq_recv      cmd;
1687         struct ib_uverbs_post_srq_recv_resp resp;
1688         struct ib_recv_wr                  *wr, *next, *bad_wr;
1689         struct ib_srq                      *srq;
1690         ssize_t                             ret = -EINVAL;
1691
1692         if (copy_from_user(&cmd, buf, sizeof cmd))
1693                 return -EFAULT;
1694
1695         wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1696                                        in_len - sizeof cmd, cmd.wr_count,
1697                                        cmd.sge_count, cmd.wqe_size);
1698         if (IS_ERR(wr))
1699                 return PTR_ERR(wr);
1700
1701         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1702         if (!srq)
1703                 goto out;
1704
1705         resp.bad_wr = 0;
1706         ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1707
1708         put_srq_read(srq);
1709
1710         if (ret)
1711                 for (next = wr; next; next = next->next) {
1712                         ++resp.bad_wr;
1713                         if (next == bad_wr)
1714                                 break;
1715                 }
1716
1717
1718         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1719                          &resp, sizeof resp))
1720                 ret = -EFAULT;
1721
1722 out:
1723         while (wr) {
1724                 next = wr->next;
1725                 kfree(wr);
1726                 wr = next;
1727         }
1728
1729         return ret ? ret : in_len;
1730 }
1731
1732 ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1733                             const char __user *buf, int in_len,
1734                             int out_len)
1735 {
1736         struct ib_uverbs_create_ah       cmd;
1737         struct ib_uverbs_create_ah_resp  resp;
1738         struct ib_uobject               *uobj;
1739         struct ib_pd                    *pd;
1740         struct ib_ah                    *ah;
1741         struct ib_ah_attr               attr;
1742         int ret;
1743
1744         if (out_len < sizeof resp)
1745                 return -ENOSPC;
1746
1747         if (copy_from_user(&cmd, buf, sizeof cmd))
1748                 return -EFAULT;
1749
1750         uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1751         if (!uobj)
1752                 return -ENOMEM;
1753
1754         init_uobj(uobj, cmd.user_handle, file->ucontext);
1755         down_write(&uobj->mutex);
1756
1757         pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1758         if (!pd) {
1759                 ret = -EINVAL;
1760                 goto err;
1761         }
1762
1763         attr.dlid              = cmd.attr.dlid;
1764         attr.sl                = cmd.attr.sl;
1765         attr.src_path_bits     = cmd.attr.src_path_bits;
1766         attr.static_rate       = cmd.attr.static_rate;
1767         attr.ah_flags          = cmd.attr.is_global ? IB_AH_GRH : 0;
1768         attr.port_num          = cmd.attr.port_num;
1769         attr.grh.flow_label    = cmd.attr.grh.flow_label;
1770         attr.grh.sgid_index    = cmd.attr.grh.sgid_index;
1771         attr.grh.hop_limit     = cmd.attr.grh.hop_limit;
1772         attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1773         memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1774
1775         ah = ib_create_ah(pd, &attr);
1776         if (IS_ERR(ah)) {
1777                 ret = PTR_ERR(ah);
1778                 goto err;
1779         }
1780
1781         ah->uobject  = uobj;
1782         uobj->object = ah;
1783
1784         ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
1785         if (ret)
1786                 goto err_destroy;
1787
1788         resp.ah_handle = uobj->id;
1789
1790         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1791                          &resp, sizeof resp)) {
1792                 ret = -EFAULT;
1793                 goto err_copy;
1794         }
1795
1796         put_pd_read(pd);
1797
1798         mutex_lock(&file->mutex);
1799         list_add_tail(&uobj->list, &file->ucontext->ah_list);
1800         mutex_unlock(&file->mutex);
1801
1802         uobj->live = 1;
1803
1804         up_write(&uobj->mutex);
1805
1806         return in_len;
1807
1808 err_copy:
1809         idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1810
1811 err_destroy:
1812         ib_destroy_ah(ah);
1813
1814 err:
1815         put_uobj_write(uobj);
1816         return ret;
1817 }
1818
1819 ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1820                              const char __user *buf, int in_len, int out_len)
1821 {
1822         struct ib_uverbs_destroy_ah cmd;
1823         struct ib_ah               *ah;
1824         struct ib_uobject          *uobj;
1825         int                         ret;
1826
1827         if (copy_from_user(&cmd, buf, sizeof cmd))
1828                 return -EFAULT;
1829
1830         uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
1831         if (!uobj)
1832                 return -EINVAL;
1833         ah = uobj->object;
1834
1835         ret = ib_destroy_ah(ah);
1836         if (!ret)
1837                 uobj->live = 0;
1838
1839         put_uobj_write(uobj);
1840
1841         if (ret)
1842                 return ret;
1843
1844         idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1845
1846         mutex_lock(&file->mutex);
1847         list_del(&uobj->list);
1848         mutex_unlock(&file->mutex);
1849
1850         put_uobj(uobj);
1851
1852         return in_len;
1853 }
1854
1855 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1856                                const char __user *buf, int in_len,
1857                                int out_len)
1858 {
1859         struct ib_uverbs_attach_mcast cmd;
1860         struct ib_qp                 *qp;
1861         struct ib_uqp_object         *obj;
1862         struct ib_uverbs_mcast_entry *mcast;
1863         int                           ret;
1864
1865         if (copy_from_user(&cmd, buf, sizeof cmd))
1866                 return -EFAULT;
1867
1868         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1869         if (!qp)
1870                 return -EINVAL;
1871
1872         obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1873
1874         list_for_each_entry(mcast, &obj->mcast_list, list)
1875                 if (cmd.mlid == mcast->lid &&
1876                     !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1877                         ret = 0;
1878                         goto out_put;
1879                 }
1880
1881         mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1882         if (!mcast) {
1883                 ret = -ENOMEM;
1884                 goto out_put;
1885         }
1886
1887         mcast->lid = cmd.mlid;
1888         memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1889
1890         ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1891         if (!ret)
1892                 list_add_tail(&mcast->list, &obj->mcast_list);
1893         else
1894                 kfree(mcast);
1895
1896 out_put:
1897         put_qp_read(qp);
1898
1899         return ret ? ret : in_len;
1900 }
1901
1902 ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1903                                const char __user *buf, int in_len,
1904                                int out_len)
1905 {
1906         struct ib_uverbs_detach_mcast cmd;
1907         struct ib_uqp_object         *obj;
1908         struct ib_qp                 *qp;
1909         struct ib_uverbs_mcast_entry *mcast;
1910         int                           ret = -EINVAL;
1911
1912         if (copy_from_user(&cmd, buf, sizeof cmd))
1913                 return -EFAULT;
1914
1915         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1916         if (!qp)
1917                 return -EINVAL;
1918
1919         ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1920         if (ret)
1921                 goto out_put;
1922
1923         obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1924
1925         list_for_each_entry(mcast, &obj->mcast_list, list)
1926                 if (cmd.mlid == mcast->lid &&
1927                     !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1928                         list_del(&mcast->list);
1929                         kfree(mcast);
1930                         break;
1931                 }
1932
1933 out_put:
1934         put_qp_read(qp);
1935
1936         return ret ? ret : in_len;
1937 }
1938
1939 ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1940                              const char __user *buf, int in_len,
1941                              int out_len)
1942 {
1943         struct ib_uverbs_create_srq      cmd;
1944         struct ib_uverbs_create_srq_resp resp;
1945         struct ib_udata                  udata;
1946         struct ib_uevent_object         *obj;
1947         struct ib_pd                    *pd;
1948         struct ib_srq                   *srq;
1949         struct ib_srq_init_attr          attr;
1950         int ret;
1951
1952         if (out_len < sizeof resp)
1953                 return -ENOSPC;
1954
1955         if (copy_from_user(&cmd, buf, sizeof cmd))
1956                 return -EFAULT;
1957
1958         INIT_UDATA(&udata, buf + sizeof cmd,
1959                    (unsigned long) cmd.response + sizeof resp,
1960                    in_len - sizeof cmd, out_len - sizeof resp);
1961
1962         obj = kmalloc(sizeof *obj, GFP_KERNEL);
1963         if (!obj)
1964                 return -ENOMEM;
1965
1966         init_uobj(&obj->uobject, cmd.user_handle, file->ucontext);
1967         down_write(&obj->uobject.mutex);
1968
1969         pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
1970         if (!pd) {
1971                 ret = -EINVAL;
1972                 goto err;
1973         }
1974
1975         attr.event_handler  = ib_uverbs_srq_event_handler;
1976         attr.srq_context    = file;
1977         attr.attr.max_wr    = cmd.max_wr;
1978         attr.attr.max_sge   = cmd.max_sge;
1979         attr.attr.srq_limit = cmd.srq_limit;
1980
1981         obj->events_reported     = 0;
1982         INIT_LIST_HEAD(&obj->event_list);
1983
1984         srq = pd->device->create_srq(pd, &attr, &udata);
1985         if (IS_ERR(srq)) {
1986                 ret = PTR_ERR(srq);
1987                 goto err;
1988         }
1989
1990         srq->device        = pd->device;
1991         srq->pd            = pd;
1992         srq->uobject       = &obj->uobject;
1993         srq->event_handler = attr.event_handler;
1994         srq->srq_context   = attr.srq_context;
1995         atomic_inc(&pd->usecnt);
1996         atomic_set(&srq->usecnt, 0);
1997
1998         obj->uobject.object = srq;
1999         ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2000         if (ret)
2001                 goto err_destroy;
2002
2003         memset(&resp, 0, sizeof resp);
2004         resp.srq_handle = obj->uobject.id;
2005         resp.max_wr     = attr.attr.max_wr;
2006         resp.max_sge    = attr.attr.max_sge;
2007
2008         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2009                          &resp, sizeof resp)) {
2010                 ret = -EFAULT;
2011                 goto err_copy;
2012         }
2013
2014         put_pd_read(pd);
2015
2016         mutex_lock(&file->mutex);
2017         list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
2018         mutex_unlock(&file->mutex);
2019
2020         obj->uobject.live = 1;
2021
2022         up_write(&obj->uobject.mutex);
2023
2024         return in_len;
2025
2026 err_copy:
2027         idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2028
2029 err_destroy:
2030         ib_destroy_srq(srq);
2031
2032 err:
2033         put_uobj_write(&obj->uobject);
2034         return ret;
2035 }
2036
2037 ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2038                              const char __user *buf, int in_len,
2039                              int out_len)
2040 {
2041         struct ib_uverbs_modify_srq cmd;
2042         struct ib_srq              *srq;
2043         struct ib_srq_attr          attr;
2044         int                         ret;
2045
2046         if (copy_from_user(&cmd, buf, sizeof cmd))
2047                 return -EFAULT;
2048
2049         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2050         if (!srq)
2051                 return -EINVAL;
2052
2053         attr.max_wr    = cmd.max_wr;
2054         attr.srq_limit = cmd.srq_limit;
2055
2056         ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
2057
2058         put_srq_read(srq);
2059
2060         return ret ? ret : in_len;
2061 }
2062
2063 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2064                             const char __user *buf,
2065                             int in_len, int out_len)
2066 {
2067         struct ib_uverbs_query_srq      cmd;
2068         struct ib_uverbs_query_srq_resp resp;
2069         struct ib_srq_attr              attr;
2070         struct ib_srq                   *srq;
2071         int                             ret;
2072
2073         if (out_len < sizeof resp)
2074                 return -ENOSPC;
2075
2076         if (copy_from_user(&cmd, buf, sizeof cmd))
2077                 return -EFAULT;
2078
2079         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2080         if (!srq)
2081                 return -EINVAL;
2082
2083         ret = ib_query_srq(srq, &attr);
2084
2085         put_srq_read(srq);
2086
2087         if (ret)
2088                 return ret;
2089
2090         memset(&resp, 0, sizeof resp);
2091
2092         resp.max_wr    = attr.max_wr;
2093         resp.max_sge   = attr.max_sge;
2094         resp.srq_limit = attr.srq_limit;
2095
2096         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2097                          &resp, sizeof resp))
2098                 return -EFAULT;
2099
2100         return in_len;
2101 }
2102
2103 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2104                               const char __user *buf, int in_len,
2105                               int out_len)
2106 {
2107         struct ib_uverbs_destroy_srq      cmd;
2108         struct ib_uverbs_destroy_srq_resp resp;
2109         struct ib_uobject                *uobj;
2110         struct ib_srq                    *srq;
2111         struct ib_uevent_object          *obj;
2112         int                               ret = -EINVAL;
2113
2114         if (copy_from_user(&cmd, buf, sizeof cmd))
2115                 return -EFAULT;
2116
2117         uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2118         if (!uobj)
2119                 return -EINVAL;
2120         srq = uobj->object;
2121         obj = container_of(uobj, struct ib_uevent_object, uobject);
2122
2123         ret = ib_destroy_srq(srq);
2124         if (!ret)
2125                 uobj->live = 0;
2126
2127         put_uobj_write(uobj);
2128
2129         if (ret)
2130                 return ret;
2131
2132         idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2133
2134         mutex_lock(&file->mutex);
2135         list_del(&uobj->list);
2136         mutex_unlock(&file->mutex);
2137
2138         ib_uverbs_release_uevent(file, obj);
2139
2140         memset(&resp, 0, sizeof resp);
2141         resp.events_reported = obj->events_reported;
2142
2143         put_uobj(uobj);
2144
2145         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2146                          &resp, sizeof resp))
2147                 ret = -EFAULT;
2148
2149         return ret ? ret : in_len;
2150 }