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