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