IB/uverbs: Fix lockdep warnings
[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)
159 {
160         struct ib_uobject *uobj;
161
162         uobj = __idr_get_uobj(idr, id, context);
163         if (!uobj)
164                 return NULL;
165
166         down_read(&uobj->mutex);
167         if (!uobj->live) {
168                 put_uobj_read(uobj);
169                 return NULL;
170         }
171
172         return uobj;
173 }
174
175 static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
176                                          struct ib_ucontext *context)
177 {
178         struct ib_uobject *uobj;
179
180         uobj = __idr_get_uobj(idr, id, context);
181         if (!uobj)
182                 return NULL;
183
184         down_write(&uobj->mutex);
185         if (!uobj->live) {
186                 put_uobj_write(uobj);
187                 return NULL;
188         }
189
190         return uobj;
191 }
192
193 static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context)
194 {
195         struct ib_uobject *uobj;
196
197         uobj = idr_read_uobj(idr, id, context);
198         return uobj ? uobj->object : NULL;
199 }
200
201 static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
202 {
203         return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context);
204 }
205
206 static void put_pd_read(struct ib_pd *pd)
207 {
208         put_uobj_read(pd->uobject);
209 }
210
211 static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context)
212 {
213         return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context);
214 }
215
216 static void put_cq_read(struct ib_cq *cq)
217 {
218         put_uobj_read(cq->uobject);
219 }
220
221 static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
222 {
223         return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context);
224 }
225
226 static void put_ah_read(struct ib_ah *ah)
227 {
228         put_uobj_read(ah->uobject);
229 }
230
231 static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
232 {
233         return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context);
234 }
235
236 static void put_qp_read(struct ib_qp *qp)
237 {
238         put_uobj_read(qp->uobject);
239 }
240
241 static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
242 {
243         return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context);
244 }
245
246 static void put_srq_read(struct ib_srq *srq)
247 {
248         put_uobj_read(srq->uobject);
249 }
250
251 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
252                               const char __user *buf,
253                               int in_len, int out_len)
254 {
255         struct ib_uverbs_get_context      cmd;
256         struct ib_uverbs_get_context_resp resp;
257         struct ib_udata                   udata;
258         struct ib_device                 *ibdev = file->device->ib_dev;
259         struct ib_ucontext               *ucontext;
260         struct file                      *filp;
261         int ret;
262
263         if (out_len < sizeof resp)
264                 return -ENOSPC;
265
266         if (copy_from_user(&cmd, buf, sizeof cmd))
267                 return -EFAULT;
268
269         mutex_lock(&file->mutex);
270
271         if (file->ucontext) {
272                 ret = -EINVAL;
273                 goto err;
274         }
275
276         INIT_UDATA(&udata, buf + sizeof cmd,
277                    (unsigned long) cmd.response + sizeof resp,
278                    in_len - sizeof cmd, out_len - sizeof resp);
279
280         ucontext = ibdev->alloc_ucontext(ibdev, &udata);
281         if (IS_ERR(ucontext)) {
282                 ret = PTR_ERR(file->ucontext);
283                 goto err;
284         }
285
286         ucontext->device = ibdev;
287         INIT_LIST_HEAD(&ucontext->pd_list);
288         INIT_LIST_HEAD(&ucontext->mr_list);
289         INIT_LIST_HEAD(&ucontext->mw_list);
290         INIT_LIST_HEAD(&ucontext->cq_list);
291         INIT_LIST_HEAD(&ucontext->qp_list);
292         INIT_LIST_HEAD(&ucontext->srq_list);
293         INIT_LIST_HEAD(&ucontext->ah_list);
294
295         resp.num_comp_vectors = file->device->num_comp_vectors;
296
297         filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
298         if (IS_ERR(filp)) {
299                 ret = PTR_ERR(filp);
300                 goto err_free;
301         }
302
303         if (copy_to_user((void __user *) (unsigned long) cmd.response,
304                          &resp, sizeof resp)) {
305                 ret = -EFAULT;
306                 goto err_file;
307         }
308
309         file->async_file = filp->private_data;
310
311         INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
312                               ib_uverbs_event_handler);
313         ret = ib_register_event_handler(&file->event_handler);
314         if (ret)
315                 goto err_file;
316
317         kref_get(&file->async_file->ref);
318         kref_get(&file->ref);
319         file->ucontext = ucontext;
320
321         fd_install(resp.async_fd, filp);
322
323         mutex_unlock(&file->mutex);
324
325         return in_len;
326
327 err_file:
328         put_unused_fd(resp.async_fd);
329         fput(filp);
330
331 err_free:
332         ibdev->dealloc_ucontext(ucontext);
333
334 err:
335         mutex_unlock(&file->mutex);
336         return ret;
337 }
338
339 ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
340                                const char __user *buf,
341                                int in_len, int out_len)
342 {
343         struct ib_uverbs_query_device      cmd;
344         struct ib_uverbs_query_device_resp resp;
345         struct ib_device_attr              attr;
346         int                                ret;
347
348         if (out_len < sizeof resp)
349                 return -ENOSPC;
350
351         if (copy_from_user(&cmd, buf, sizeof cmd))
352                 return -EFAULT;
353
354         ret = ib_query_device(file->device->ib_dev, &attr);
355         if (ret)
356                 return ret;
357
358         memset(&resp, 0, sizeof resp);
359
360         resp.fw_ver                    = attr.fw_ver;
361         resp.node_guid                 = file->device->ib_dev->node_guid;
362         resp.sys_image_guid            = attr.sys_image_guid;
363         resp.max_mr_size               = attr.max_mr_size;
364         resp.page_size_cap             = attr.page_size_cap;
365         resp.vendor_id                 = attr.vendor_id;
366         resp.vendor_part_id            = attr.vendor_part_id;
367         resp.hw_ver                    = attr.hw_ver;
368         resp.max_qp                    = attr.max_qp;
369         resp.max_qp_wr                 = attr.max_qp_wr;
370         resp.device_cap_flags          = attr.device_cap_flags;
371         resp.max_sge                   = attr.max_sge;
372         resp.max_sge_rd                = attr.max_sge_rd;
373         resp.max_cq                    = attr.max_cq;
374         resp.max_cqe                   = attr.max_cqe;
375         resp.max_mr                    = attr.max_mr;
376         resp.max_pd                    = attr.max_pd;
377         resp.max_qp_rd_atom            = attr.max_qp_rd_atom;
378         resp.max_ee_rd_atom            = attr.max_ee_rd_atom;
379         resp.max_res_rd_atom           = attr.max_res_rd_atom;
380         resp.max_qp_init_rd_atom       = attr.max_qp_init_rd_atom;
381         resp.max_ee_init_rd_atom       = attr.max_ee_init_rd_atom;
382         resp.atomic_cap                = attr.atomic_cap;
383         resp.max_ee                    = attr.max_ee;
384         resp.max_rdd                   = attr.max_rdd;
385         resp.max_mw                    = attr.max_mw;
386         resp.max_raw_ipv6_qp           = attr.max_raw_ipv6_qp;
387         resp.max_raw_ethy_qp           = attr.max_raw_ethy_qp;
388         resp.max_mcast_grp             = attr.max_mcast_grp;
389         resp.max_mcast_qp_attach       = attr.max_mcast_qp_attach;
390         resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
391         resp.max_ah                    = attr.max_ah;
392         resp.max_fmr                   = attr.max_fmr;
393         resp.max_map_per_fmr           = attr.max_map_per_fmr;
394         resp.max_srq                   = attr.max_srq;
395         resp.max_srq_wr                = attr.max_srq_wr;
396         resp.max_srq_sge               = attr.max_srq_sge;
397         resp.max_pkeys                 = attr.max_pkeys;
398         resp.local_ca_ack_delay        = attr.local_ca_ack_delay;
399         resp.phys_port_cnt             = file->device->ib_dev->phys_port_cnt;
400
401         if (copy_to_user((void __user *) (unsigned long) cmd.response,
402                          &resp, sizeof resp))
403                 return -EFAULT;
404
405         return in_len;
406 }
407
408 ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
409                              const char __user *buf,
410                              int in_len, int out_len)
411 {
412         struct ib_uverbs_query_port      cmd;
413         struct ib_uverbs_query_port_resp resp;
414         struct ib_port_attr              attr;
415         int                              ret;
416
417         if (out_len < sizeof resp)
418                 return -ENOSPC;
419
420         if (copy_from_user(&cmd, buf, sizeof cmd))
421                 return -EFAULT;
422
423         ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
424         if (ret)
425                 return ret;
426
427         memset(&resp, 0, sizeof resp);
428
429         resp.state           = attr.state;
430         resp.max_mtu         = attr.max_mtu;
431         resp.active_mtu      = attr.active_mtu;
432         resp.gid_tbl_len     = attr.gid_tbl_len;
433         resp.port_cap_flags  = attr.port_cap_flags;
434         resp.max_msg_sz      = attr.max_msg_sz;
435         resp.bad_pkey_cntr   = attr.bad_pkey_cntr;
436         resp.qkey_viol_cntr  = attr.qkey_viol_cntr;
437         resp.pkey_tbl_len    = attr.pkey_tbl_len;
438         resp.lid             = attr.lid;
439         resp.sm_lid          = attr.sm_lid;
440         resp.lmc             = attr.lmc;
441         resp.max_vl_num      = attr.max_vl_num;
442         resp.sm_sl           = attr.sm_sl;
443         resp.subnet_timeout  = attr.subnet_timeout;
444         resp.init_type_reply = attr.init_type_reply;
445         resp.active_width    = attr.active_width;
446         resp.active_speed    = attr.active_speed;
447         resp.phys_state      = attr.phys_state;
448
449         if (copy_to_user((void __user *) (unsigned long) cmd.response,
450                          &resp, sizeof resp))
451                 return -EFAULT;
452
453         return in_len;
454 }
455
456 ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
457                            const char __user *buf,
458                            int in_len, int out_len)
459 {
460         struct ib_uverbs_alloc_pd      cmd;
461         struct ib_uverbs_alloc_pd_resp resp;
462         struct ib_udata                udata;
463         struct ib_uobject             *uobj;
464         struct ib_pd                  *pd;
465         int                            ret;
466
467         if (out_len < sizeof resp)
468                 return -ENOSPC;
469
470         if (copy_from_user(&cmd, buf, sizeof cmd))
471                 return -EFAULT;
472
473         INIT_UDATA(&udata, buf + sizeof cmd,
474                    (unsigned long) cmd.response + sizeof resp,
475                    in_len - sizeof cmd, out_len - sizeof resp);
476
477         uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
478         if (!uobj)
479                 return -ENOMEM;
480
481         init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
482         down_write(&uobj->mutex);
483
484         pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
485                                             file->ucontext, &udata);
486         if (IS_ERR(pd)) {
487                 ret = PTR_ERR(pd);
488                 goto err;
489         }
490
491         pd->device  = file->device->ib_dev;
492         pd->uobject = uobj;
493         atomic_set(&pd->usecnt, 0);
494
495         uobj->object = pd;
496         ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
497         if (ret)
498                 goto err_idr;
499
500         memset(&resp, 0, sizeof resp);
501         resp.pd_handle = uobj->id;
502
503         if (copy_to_user((void __user *) (unsigned long) cmd.response,
504                          &resp, sizeof resp)) {
505                 ret = -EFAULT;
506                 goto err_copy;
507         }
508
509         mutex_lock(&file->mutex);
510         list_add_tail(&uobj->list, &file->ucontext->pd_list);
511         mutex_unlock(&file->mutex);
512
513         uobj->live = 1;
514
515         up_write(&uobj->mutex);
516
517         return in_len;
518
519 err_copy:
520         idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
521
522 err_idr:
523         ib_dealloc_pd(pd);
524
525 err:
526         put_uobj_write(uobj);
527         return ret;
528 }
529
530 ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
531                              const char __user *buf,
532                              int in_len, int out_len)
533 {
534         struct ib_uverbs_dealloc_pd cmd;
535         struct ib_uobject          *uobj;
536         int                         ret;
537
538         if (copy_from_user(&cmd, buf, sizeof cmd))
539                 return -EFAULT;
540
541         uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
542         if (!uobj)
543                 return -EINVAL;
544
545         ret = ib_dealloc_pd(uobj->object);
546         if (!ret)
547                 uobj->live = 0;
548
549         put_uobj_write(uobj);
550
551         if (ret)
552                 return ret;
553
554         idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
555
556         mutex_lock(&file->mutex);
557         list_del(&uobj->list);
558         mutex_unlock(&file->mutex);
559
560         put_uobj(uobj);
561
562         return in_len;
563 }
564
565 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
566                          const char __user *buf, int in_len,
567                          int out_len)
568 {
569         struct ib_uverbs_reg_mr      cmd;
570         struct ib_uverbs_reg_mr_resp resp;
571         struct ib_udata              udata;
572         struct ib_umem_object       *obj;
573         struct ib_pd                *pd;
574         struct ib_mr                *mr;
575         int                          ret;
576
577         if (out_len < sizeof resp)
578                 return -ENOSPC;
579
580         if (copy_from_user(&cmd, buf, sizeof cmd))
581                 return -EFAULT;
582
583         INIT_UDATA(&udata, buf + sizeof cmd,
584                    (unsigned long) cmd.response + sizeof resp,
585                    in_len - sizeof cmd, out_len - sizeof resp);
586
587         if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
588                 return -EINVAL;
589
590         /*
591          * Local write permission is required if remote write or
592          * remote atomic permission is also requested.
593          */
594         if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
595             !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
596                 return -EINVAL;
597
598         obj = kmalloc(sizeof *obj, GFP_KERNEL);
599         if (!obj)
600                 return -ENOMEM;
601
602         init_uobj(&obj->uobject, 0, file->ucontext, &mr_lock_key);
603         down_write(&obj->uobject.mutex);
604
605         /*
606          * We ask for writable memory if any access flags other than
607          * "remote read" are set.  "Local write" and "remote write"
608          * obviously require write access.  "Remote atomic" can do
609          * things like fetch and add, which will modify memory, and
610          * "MW bind" can change permissions by binding a window.
611          */
612         ret = ib_umem_get(file->device->ib_dev, &obj->umem,
613                           (void *) (unsigned long) cmd.start, cmd.length,
614                           !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
615         if (ret)
616                 goto err_free;
617
618         obj->umem.virt_base = cmd.hca_va;
619
620         pd = idr_read_pd(cmd.pd_handle, file->ucontext);
621         if (!pd)
622                 goto err_release;
623
624         mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
625         if (IS_ERR(mr)) {
626                 ret = PTR_ERR(mr);
627                 goto err_put;
628         }
629
630         mr->device  = pd->device;
631         mr->pd      = pd;
632         mr->uobject = &obj->uobject;
633         atomic_inc(&pd->usecnt);
634         atomic_set(&mr->usecnt, 0);
635
636         obj->uobject.object = mr;
637         ret = idr_add_uobj(&ib_uverbs_mr_idr, &obj->uobject);
638         if (ret)
639                 goto err_unreg;
640
641         memset(&resp, 0, sizeof resp);
642         resp.lkey      = mr->lkey;
643         resp.rkey      = mr->rkey;
644         resp.mr_handle = obj->uobject.id;
645
646         if (copy_to_user((void __user *) (unsigned long) cmd.response,
647                          &resp, sizeof resp)) {
648                 ret = -EFAULT;
649                 goto err_copy;
650         }
651
652         put_pd_read(pd);
653
654         mutex_lock(&file->mutex);
655         list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
656         mutex_unlock(&file->mutex);
657
658         obj->uobject.live = 1;
659
660         up_write(&obj->uobject.mutex);
661
662         return in_len;
663
664 err_copy:
665         idr_remove_uobj(&ib_uverbs_mr_idr, &obj->uobject);
666
667 err_unreg:
668         ib_dereg_mr(mr);
669
670 err_put:
671         put_pd_read(pd);
672
673 err_release:
674         ib_umem_release(file->device->ib_dev, &obj->umem);
675
676 err_free:
677         put_uobj_write(&obj->uobject);
678         return ret;
679 }
680
681 ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
682                            const char __user *buf, int in_len,
683                            int out_len)
684 {
685         struct ib_uverbs_dereg_mr cmd;
686         struct ib_mr             *mr;
687         struct ib_uobject        *uobj;
688         struct ib_umem_object    *memobj;
689         int                       ret = -EINVAL;
690
691         if (copy_from_user(&cmd, buf, sizeof cmd))
692                 return -EFAULT;
693
694         uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
695         if (!uobj)
696                 return -EINVAL;
697
698         memobj = container_of(uobj, struct ib_umem_object, uobject);
699         mr     = uobj->object;
700
701         ret = ib_dereg_mr(mr);
702         if (!ret)
703                 uobj->live = 0;
704
705         put_uobj_write(uobj);
706
707         if (ret)
708                 return ret;
709
710         idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
711
712         mutex_lock(&file->mutex);
713         list_del(&uobj->list);
714         mutex_unlock(&file->mutex);
715
716         ib_umem_release(file->device->ib_dev, &memobj->umem);
717
718         put_uobj(uobj);
719
720         return in_len;
721 }
722
723 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
724                                       const char __user *buf, int in_len,
725                                       int out_len)
726 {
727         struct ib_uverbs_create_comp_channel       cmd;
728         struct ib_uverbs_create_comp_channel_resp  resp;
729         struct file                               *filp;
730
731         if (out_len < sizeof resp)
732                 return -ENOSPC;
733
734         if (copy_from_user(&cmd, buf, sizeof cmd))
735                 return -EFAULT;
736
737         filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
738         if (IS_ERR(filp))
739                 return PTR_ERR(filp);
740
741         if (copy_to_user((void __user *) (unsigned long) cmd.response,
742                          &resp, sizeof resp)) {
743                 put_unused_fd(resp.fd);
744                 fput(filp);
745                 return -EFAULT;
746         }
747
748         fd_install(resp.fd, filp);
749         return in_len;
750 }
751
752 ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
753                             const char __user *buf, int in_len,
754                             int out_len)
755 {
756         struct ib_uverbs_create_cq      cmd;
757         struct ib_uverbs_create_cq_resp resp;
758         struct ib_udata                 udata;
759         struct ib_ucq_object           *obj;
760         struct ib_uverbs_event_file    *ev_file = NULL;
761         struct ib_cq                   *cq;
762         int                             ret;
763
764         if (out_len < sizeof resp)
765                 return -ENOSPC;
766
767         if (copy_from_user(&cmd, buf, sizeof cmd))
768                 return -EFAULT;
769
770         INIT_UDATA(&udata, buf + sizeof cmd,
771                    (unsigned long) cmd.response + sizeof resp,
772                    in_len - sizeof cmd, out_len - sizeof resp);
773
774         if (cmd.comp_vector >= file->device->num_comp_vectors)
775                 return -EINVAL;
776
777         obj = kmalloc(sizeof *obj, GFP_KERNEL);
778         if (!obj)
779                 return -ENOMEM;
780
781         init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
782         down_write(&obj->uobject.mutex);
783
784         if (cmd.comp_channel >= 0) {
785                 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
786                 if (!ev_file) {
787                         ret = -EINVAL;
788                         goto err;
789                 }
790         }
791
792         obj->uverbs_file           = file;
793         obj->comp_events_reported  = 0;
794         obj->async_events_reported = 0;
795         INIT_LIST_HEAD(&obj->comp_list);
796         INIT_LIST_HEAD(&obj->async_list);
797
798         cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
799                                              file->ucontext, &udata);
800         if (IS_ERR(cq)) {
801                 ret = PTR_ERR(cq);
802                 goto err_file;
803         }
804
805         cq->device        = file->device->ib_dev;
806         cq->uobject       = &obj->uobject;
807         cq->comp_handler  = ib_uverbs_comp_handler;
808         cq->event_handler = ib_uverbs_cq_event_handler;
809         cq->cq_context    = ev_file;
810         atomic_set(&cq->usecnt, 0);
811
812         obj->uobject.object = cq;
813         ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
814         if (ret)
815                 goto err_free;
816
817         memset(&resp, 0, sizeof resp);
818         resp.cq_handle = obj->uobject.id;
819         resp.cqe       = cq->cqe;
820
821         if (copy_to_user((void __user *) (unsigned long) cmd.response,
822                          &resp, sizeof resp)) {
823                 ret = -EFAULT;
824                 goto err_copy;
825         }
826
827         mutex_lock(&file->mutex);
828         list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
829         mutex_unlock(&file->mutex);
830
831         obj->uobject.live = 1;
832
833         up_write(&obj->uobject.mutex);
834
835         return in_len;
836
837 err_copy:
838         idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
839
840
841 err_free:
842         ib_destroy_cq(cq);
843
844 err_file:
845         if (ev_file)
846                 ib_uverbs_release_ucq(file, ev_file, obj);
847
848 err:
849         put_uobj_write(&obj->uobject);
850         return ret;
851 }
852
853 ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
854                             const char __user *buf, int in_len,
855                             int out_len)
856 {
857         struct ib_uverbs_resize_cq      cmd;
858         struct ib_uverbs_resize_cq_resp resp;
859         struct ib_udata                 udata;
860         struct ib_cq                    *cq;
861         int                             ret = -EINVAL;
862
863         if (copy_from_user(&cmd, buf, sizeof cmd))
864                 return -EFAULT;
865
866         INIT_UDATA(&udata, buf + sizeof cmd,
867                    (unsigned long) cmd.response + sizeof resp,
868                    in_len - sizeof cmd, out_len - sizeof resp);
869
870         cq = idr_read_cq(cmd.cq_handle, file->ucontext);
871         if (!cq)
872                 return -EINVAL;
873
874         ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
875         if (ret)
876                 goto out;
877
878         memset(&resp, 0, sizeof resp);
879         resp.cqe = cq->cqe;
880
881         if (copy_to_user((void __user *) (unsigned long) cmd.response,
882                          &resp, sizeof resp))
883                 ret = -EFAULT;
884
885 out:
886         put_cq_read(cq);
887
888         return ret ? ret : in_len;
889 }
890
891 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
892                           const char __user *buf, int in_len,
893                           int out_len)
894 {
895         struct ib_uverbs_poll_cq       cmd;
896         struct ib_uverbs_poll_cq_resp *resp;
897         struct ib_uobject             *uobj;
898         struct ib_cq                  *cq;
899         struct ib_wc                  *wc;
900         int                            ret = 0;
901         int                            i;
902         int                            rsize;
903
904         if (copy_from_user(&cmd, buf, sizeof cmd))
905                 return -EFAULT;
906
907         wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
908         if (!wc)
909                 return -ENOMEM;
910
911         rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
912         resp = kmalloc(rsize, GFP_KERNEL);
913         if (!resp) {
914                 ret = -ENOMEM;
915                 goto out_wc;
916         }
917
918         uobj = idr_read_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
919         if (!uobj) {
920                 ret = -EINVAL;
921                 goto out;
922         }
923         cq = uobj->object;
924
925         resp->count = ib_poll_cq(cq, cmd.ne, wc);
926
927         put_uobj_read(uobj);
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_uobject             *uobj;
963         struct ib_cq                  *cq;
964
965         if (copy_from_user(&cmd, buf, sizeof cmd))
966                 return -EFAULT;
967
968         uobj = idr_read_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
969         if (!uobj)
970                 return -EINVAL;
971         cq = uobj->object;
972
973         ib_req_notify_cq(cq, cmd.solicited_only ?
974                          IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
975
976         put_uobj_read(uobj);
977
978         return in_len;
979 }
980
981 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
982                              const char __user *buf, int in_len,
983                              int out_len)
984 {
985         struct ib_uverbs_destroy_cq      cmd;
986         struct ib_uverbs_destroy_cq_resp resp;
987         struct ib_uobject               *uobj;
988         struct ib_cq                    *cq;
989         struct ib_ucq_object            *obj;
990         struct ib_uverbs_event_file     *ev_file;
991         int                              ret = -EINVAL;
992
993         if (copy_from_user(&cmd, buf, sizeof cmd))
994                 return -EFAULT;
995
996         uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
997         if (!uobj)
998                 return -EINVAL;
999         cq      = uobj->object;
1000         ev_file = cq->cq_context;
1001         obj     = container_of(cq->uobject, struct ib_ucq_object, uobject);
1002
1003         ret = ib_destroy_cq(cq);
1004         if (!ret)
1005                 uobj->live = 0;
1006
1007         put_uobj_write(uobj);
1008
1009         if (ret)
1010                 return ret;
1011
1012         idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
1013
1014         mutex_lock(&file->mutex);
1015         list_del(&uobj->list);
1016         mutex_unlock(&file->mutex);
1017
1018         ib_uverbs_release_ucq(file, ev_file, obj);
1019
1020         memset(&resp, 0, sizeof resp);
1021         resp.comp_events_reported  = obj->comp_events_reported;
1022         resp.async_events_reported = obj->async_events_reported;
1023
1024         put_uobj(uobj);
1025
1026         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1027                          &resp, sizeof resp))
1028                 return -EFAULT;
1029
1030         return in_len;
1031 }
1032
1033 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1034                             const char __user *buf, int in_len,
1035                             int out_len)
1036 {
1037         struct ib_uverbs_create_qp      cmd;
1038         struct ib_uverbs_create_qp_resp resp;
1039         struct ib_udata                 udata;
1040         struct ib_uqp_object           *obj;
1041         struct ib_pd                   *pd;
1042         struct ib_cq                   *scq, *rcq;
1043         struct ib_srq                  *srq;
1044         struct ib_qp                   *qp;
1045         struct ib_qp_init_attr          attr;
1046         int ret;
1047
1048         if (out_len < sizeof resp)
1049                 return -ENOSPC;
1050
1051         if (copy_from_user(&cmd, buf, sizeof cmd))
1052                 return -EFAULT;
1053
1054         INIT_UDATA(&udata, buf + sizeof cmd,
1055                    (unsigned long) cmd.response + sizeof resp,
1056                    in_len - sizeof cmd, out_len - sizeof resp);
1057
1058         obj = kmalloc(sizeof *obj, GFP_KERNEL);
1059         if (!obj)
1060                 return -ENOMEM;
1061
1062         init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
1063         down_write(&obj->uevent.uobject.mutex);
1064
1065         srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
1066         pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
1067         scq = idr_read_cq(cmd.send_cq_handle, file->ucontext);
1068         rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
1069                 scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext);
1070
1071         if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
1072                 ret = -EINVAL;
1073                 goto err_put;
1074         }
1075
1076         attr.event_handler = ib_uverbs_qp_event_handler;
1077         attr.qp_context    = file;
1078         attr.send_cq       = scq;
1079         attr.recv_cq       = rcq;
1080         attr.srq           = srq;
1081         attr.sq_sig_type   = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1082         attr.qp_type       = cmd.qp_type;
1083
1084         attr.cap.max_send_wr     = cmd.max_send_wr;
1085         attr.cap.max_recv_wr     = cmd.max_recv_wr;
1086         attr.cap.max_send_sge    = cmd.max_send_sge;
1087         attr.cap.max_recv_sge    = cmd.max_recv_sge;
1088         attr.cap.max_inline_data = cmd.max_inline_data;
1089
1090         obj->uevent.events_reported     = 0;
1091         INIT_LIST_HEAD(&obj->uevent.event_list);
1092         INIT_LIST_HEAD(&obj->mcast_list);
1093
1094         qp = pd->device->create_qp(pd, &attr, &udata);
1095         if (IS_ERR(qp)) {
1096                 ret = PTR_ERR(qp);
1097                 goto err_put;
1098         }
1099
1100         qp->device        = pd->device;
1101         qp->pd            = pd;
1102         qp->send_cq       = attr.send_cq;
1103         qp->recv_cq       = attr.recv_cq;
1104         qp->srq           = attr.srq;
1105         qp->uobject       = &obj->uevent.uobject;
1106         qp->event_handler = attr.event_handler;
1107         qp->qp_context    = attr.qp_context;
1108         qp->qp_type       = attr.qp_type;
1109         atomic_inc(&pd->usecnt);
1110         atomic_inc(&attr.send_cq->usecnt);
1111         atomic_inc(&attr.recv_cq->usecnt);
1112         if (attr.srq)
1113                 atomic_inc(&attr.srq->usecnt);
1114
1115         obj->uevent.uobject.object = qp;
1116         ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1117         if (ret)
1118                 goto err_destroy;
1119
1120         memset(&resp, 0, sizeof resp);
1121         resp.qpn             = qp->qp_num;
1122         resp.qp_handle       = obj->uevent.uobject.id;
1123         resp.max_recv_sge    = attr.cap.max_recv_sge;
1124         resp.max_send_sge    = attr.cap.max_send_sge;
1125         resp.max_recv_wr     = attr.cap.max_recv_wr;
1126         resp.max_send_wr     = attr.cap.max_send_wr;
1127         resp.max_inline_data = attr.cap.max_inline_data;
1128
1129         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1130                          &resp, sizeof resp)) {
1131                 ret = -EFAULT;
1132                 goto err_copy;
1133         }
1134
1135         put_pd_read(pd);
1136         put_cq_read(scq);
1137         if (rcq != scq)
1138                 put_cq_read(rcq);
1139         if (srq)
1140                 put_srq_read(srq);
1141
1142         mutex_lock(&file->mutex);
1143         list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1144         mutex_unlock(&file->mutex);
1145
1146         obj->uevent.uobject.live = 1;
1147
1148         up_write(&obj->uevent.uobject.mutex);
1149
1150         return in_len;
1151
1152 err_copy:
1153         idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1154
1155 err_destroy:
1156         ib_destroy_qp(qp);
1157
1158 err_put:
1159         if (pd)
1160                 put_pd_read(pd);
1161         if (scq)
1162                 put_cq_read(scq);
1163         if (rcq && rcq != scq)
1164                 put_cq_read(rcq);
1165         if (srq)
1166                 put_srq_read(srq);
1167
1168         put_uobj_write(&obj->uevent.uobject);
1169         return ret;
1170 }
1171
1172 ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1173                            const char __user *buf, int in_len,
1174                            int out_len)
1175 {
1176         struct ib_uverbs_query_qp      cmd;
1177         struct ib_uverbs_query_qp_resp resp;
1178         struct ib_qp                   *qp;
1179         struct ib_qp_attr              *attr;
1180         struct ib_qp_init_attr         *init_attr;
1181         int                            ret;
1182
1183         if (copy_from_user(&cmd, buf, sizeof cmd))
1184                 return -EFAULT;
1185
1186         attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1187         init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1188         if (!attr || !init_attr) {
1189                 ret = -ENOMEM;
1190                 goto out;
1191         }
1192
1193         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1194         if (!qp) {
1195                 ret = -EINVAL;
1196                 goto out;
1197         }
1198
1199         ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1200
1201         put_qp_read(qp);
1202
1203         if (ret)
1204                 goto out;
1205
1206         memset(&resp, 0, sizeof resp);
1207
1208         resp.qp_state               = attr->qp_state;
1209         resp.cur_qp_state           = attr->cur_qp_state;
1210         resp.path_mtu               = attr->path_mtu;
1211         resp.path_mig_state         = attr->path_mig_state;
1212         resp.qkey                   = attr->qkey;
1213         resp.rq_psn                 = attr->rq_psn;
1214         resp.sq_psn                 = attr->sq_psn;
1215         resp.dest_qp_num            = attr->dest_qp_num;
1216         resp.qp_access_flags        = attr->qp_access_flags;
1217         resp.pkey_index             = attr->pkey_index;
1218         resp.alt_pkey_index         = attr->alt_pkey_index;
1219         resp.en_sqd_async_notify    = attr->en_sqd_async_notify;
1220         resp.max_rd_atomic          = attr->max_rd_atomic;
1221         resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1222         resp.min_rnr_timer          = attr->min_rnr_timer;
1223         resp.port_num               = attr->port_num;
1224         resp.timeout                = attr->timeout;
1225         resp.retry_cnt              = attr->retry_cnt;
1226         resp.rnr_retry              = attr->rnr_retry;
1227         resp.alt_port_num           = attr->alt_port_num;
1228         resp.alt_timeout            = attr->alt_timeout;
1229
1230         memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1231         resp.dest.flow_label        = attr->ah_attr.grh.flow_label;
1232         resp.dest.sgid_index        = attr->ah_attr.grh.sgid_index;
1233         resp.dest.hop_limit         = attr->ah_attr.grh.hop_limit;
1234         resp.dest.traffic_class     = attr->ah_attr.grh.traffic_class;
1235         resp.dest.dlid              = attr->ah_attr.dlid;
1236         resp.dest.sl                = attr->ah_attr.sl;
1237         resp.dest.src_path_bits     = attr->ah_attr.src_path_bits;
1238         resp.dest.static_rate       = attr->ah_attr.static_rate;
1239         resp.dest.is_global         = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1240         resp.dest.port_num          = attr->ah_attr.port_num;
1241
1242         memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1243         resp.alt_dest.flow_label    = attr->alt_ah_attr.grh.flow_label;
1244         resp.alt_dest.sgid_index    = attr->alt_ah_attr.grh.sgid_index;
1245         resp.alt_dest.hop_limit     = attr->alt_ah_attr.grh.hop_limit;
1246         resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1247         resp.alt_dest.dlid          = attr->alt_ah_attr.dlid;
1248         resp.alt_dest.sl            = attr->alt_ah_attr.sl;
1249         resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1250         resp.alt_dest.static_rate   = attr->alt_ah_attr.static_rate;
1251         resp.alt_dest.is_global     = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1252         resp.alt_dest.port_num      = attr->alt_ah_attr.port_num;
1253
1254         resp.max_send_wr            = init_attr->cap.max_send_wr;
1255         resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1256         resp.max_send_sge           = init_attr->cap.max_send_sge;
1257         resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1258         resp.max_inline_data        = init_attr->cap.max_inline_data;
1259         resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1260
1261         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1262                          &resp, sizeof resp))
1263                 ret = -EFAULT;
1264
1265 out:
1266         kfree(attr);
1267         kfree(init_attr);
1268
1269         return ret ? ret : in_len;
1270 }
1271
1272 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1273                             const char __user *buf, int in_len,
1274                             int out_len)
1275 {
1276         struct ib_uverbs_modify_qp cmd;
1277         struct ib_qp              *qp;
1278         struct ib_qp_attr         *attr;
1279         int                        ret;
1280
1281         if (copy_from_user(&cmd, buf, sizeof cmd))
1282                 return -EFAULT;
1283
1284         attr = kmalloc(sizeof *attr, GFP_KERNEL);
1285         if (!attr)
1286                 return -ENOMEM;
1287
1288         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1289         if (!qp) {
1290                 ret = -EINVAL;
1291                 goto out;
1292         }
1293
1294         attr->qp_state            = cmd.qp_state;
1295         attr->cur_qp_state        = cmd.cur_qp_state;
1296         attr->path_mtu            = cmd.path_mtu;
1297         attr->path_mig_state      = cmd.path_mig_state;
1298         attr->qkey                = cmd.qkey;
1299         attr->rq_psn              = cmd.rq_psn;
1300         attr->sq_psn              = cmd.sq_psn;
1301         attr->dest_qp_num         = cmd.dest_qp_num;
1302         attr->qp_access_flags     = cmd.qp_access_flags;
1303         attr->pkey_index          = cmd.pkey_index;
1304         attr->alt_pkey_index      = cmd.alt_pkey_index;
1305         attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1306         attr->max_rd_atomic       = cmd.max_rd_atomic;
1307         attr->max_dest_rd_atomic  = cmd.max_dest_rd_atomic;
1308         attr->min_rnr_timer       = cmd.min_rnr_timer;
1309         attr->port_num            = cmd.port_num;
1310         attr->timeout             = cmd.timeout;
1311         attr->retry_cnt           = cmd.retry_cnt;
1312         attr->rnr_retry           = cmd.rnr_retry;
1313         attr->alt_port_num        = cmd.alt_port_num;
1314         attr->alt_timeout         = cmd.alt_timeout;
1315
1316         memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1317         attr->ah_attr.grh.flow_label        = cmd.dest.flow_label;
1318         attr->ah_attr.grh.sgid_index        = cmd.dest.sgid_index;
1319         attr->ah_attr.grh.hop_limit         = cmd.dest.hop_limit;
1320         attr->ah_attr.grh.traffic_class     = cmd.dest.traffic_class;
1321         attr->ah_attr.dlid                  = cmd.dest.dlid;
1322         attr->ah_attr.sl                    = cmd.dest.sl;
1323         attr->ah_attr.src_path_bits         = cmd.dest.src_path_bits;
1324         attr->ah_attr.static_rate           = cmd.dest.static_rate;
1325         attr->ah_attr.ah_flags              = cmd.dest.is_global ? IB_AH_GRH : 0;
1326         attr->ah_attr.port_num              = cmd.dest.port_num;
1327
1328         memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1329         attr->alt_ah_attr.grh.flow_label    = cmd.alt_dest.flow_label;
1330         attr->alt_ah_attr.grh.sgid_index    = cmd.alt_dest.sgid_index;
1331         attr->alt_ah_attr.grh.hop_limit     = cmd.alt_dest.hop_limit;
1332         attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1333         attr->alt_ah_attr.dlid              = cmd.alt_dest.dlid;
1334         attr->alt_ah_attr.sl                = cmd.alt_dest.sl;
1335         attr->alt_ah_attr.src_path_bits     = cmd.alt_dest.src_path_bits;
1336         attr->alt_ah_attr.static_rate       = cmd.alt_dest.static_rate;
1337         attr->alt_ah_attr.ah_flags          = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1338         attr->alt_ah_attr.port_num          = cmd.alt_dest.port_num;
1339
1340         ret = ib_modify_qp(qp, attr, cmd.attr_mask);
1341
1342         put_qp_read(qp);
1343
1344         if (ret)
1345                 goto out;
1346
1347         ret = in_len;
1348
1349 out:
1350         kfree(attr);
1351
1352         return ret;
1353 }
1354
1355 ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1356                              const char __user *buf, int in_len,
1357                              int out_len)
1358 {
1359         struct ib_uverbs_destroy_qp      cmd;
1360         struct ib_uverbs_destroy_qp_resp resp;
1361         struct ib_uobject               *uobj;
1362         struct ib_qp                    *qp;
1363         struct ib_uqp_object            *obj;
1364         int                              ret = -EINVAL;
1365
1366         if (copy_from_user(&cmd, buf, sizeof cmd))
1367                 return -EFAULT;
1368
1369         memset(&resp, 0, sizeof resp);
1370
1371         uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1372         if (!uobj)
1373                 return -EINVAL;
1374         qp  = uobj->object;
1375         obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1376
1377         if (!list_empty(&obj->mcast_list)) {
1378                 put_uobj_write(uobj);
1379                 return -EBUSY;
1380         }
1381
1382         ret = ib_destroy_qp(qp);
1383         if (!ret)
1384                 uobj->live = 0;
1385
1386         put_uobj_write(uobj);
1387
1388         if (ret)
1389                 return ret;
1390
1391         idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
1392
1393         mutex_lock(&file->mutex);
1394         list_del(&uobj->list);
1395         mutex_unlock(&file->mutex);
1396
1397         ib_uverbs_release_uevent(file, &obj->uevent);
1398
1399         resp.events_reported = obj->uevent.events_reported;
1400
1401         put_uobj(uobj);
1402
1403         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1404                          &resp, sizeof resp))
1405                 return -EFAULT;
1406
1407         return in_len;
1408 }
1409
1410 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1411                             const char __user *buf, int in_len,
1412                             int out_len)
1413 {
1414         struct ib_uverbs_post_send      cmd;
1415         struct ib_uverbs_post_send_resp resp;
1416         struct ib_uverbs_send_wr       *user_wr;
1417         struct ib_send_wr              *wr = NULL, *last, *next, *bad_wr;
1418         struct ib_qp                   *qp;
1419         int                             i, sg_ind;
1420         int                             is_ud;
1421         ssize_t                         ret = -EINVAL;
1422
1423         if (copy_from_user(&cmd, buf, sizeof cmd))
1424                 return -EFAULT;
1425
1426         if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1427             cmd.sge_count * sizeof (struct ib_uverbs_sge))
1428                 return -EINVAL;
1429
1430         if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1431                 return -EINVAL;
1432
1433         user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1434         if (!user_wr)
1435                 return -ENOMEM;
1436
1437         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1438         if (!qp)
1439                 goto out;
1440
1441         is_ud = qp->qp_type == IB_QPT_UD;
1442         sg_ind = 0;
1443         last = NULL;
1444         for (i = 0; i < cmd.wr_count; ++i) {
1445                 if (copy_from_user(user_wr,
1446                                    buf + sizeof cmd + i * cmd.wqe_size,
1447                                    cmd.wqe_size)) {
1448                         ret = -EFAULT;
1449                         goto out_put;
1450                 }
1451
1452                 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1453                         ret = -EINVAL;
1454                         goto out_put;
1455                 }
1456
1457                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1458                                user_wr->num_sge * sizeof (struct ib_sge),
1459                                GFP_KERNEL);
1460                 if (!next) {
1461                         ret = -ENOMEM;
1462                         goto out_put;
1463                 }
1464
1465                 if (!last)
1466                         wr = next;
1467                 else
1468                         last->next = next;
1469                 last = next;
1470
1471                 next->next       = NULL;
1472                 next->wr_id      = user_wr->wr_id;
1473                 next->num_sge    = user_wr->num_sge;
1474                 next->opcode     = user_wr->opcode;
1475                 next->send_flags = user_wr->send_flags;
1476                 next->imm_data   = (__be32 __force) user_wr->imm_data;
1477
1478                 if (is_ud) {
1479                         next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1480                                                      file->ucontext);
1481                         if (!next->wr.ud.ah) {
1482                                 ret = -EINVAL;
1483                                 goto out_put;
1484                         }
1485                         next->wr.ud.remote_qpn  = user_wr->wr.ud.remote_qpn;
1486                         next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1487                 } else {
1488                         switch (next->opcode) {
1489                         case IB_WR_RDMA_WRITE:
1490                         case IB_WR_RDMA_WRITE_WITH_IMM:
1491                         case IB_WR_RDMA_READ:
1492                                 next->wr.rdma.remote_addr =
1493                                         user_wr->wr.rdma.remote_addr;
1494                                 next->wr.rdma.rkey        =
1495                                         user_wr->wr.rdma.rkey;
1496                                 break;
1497                         case IB_WR_ATOMIC_CMP_AND_SWP:
1498                         case IB_WR_ATOMIC_FETCH_AND_ADD:
1499                                 next->wr.atomic.remote_addr =
1500                                         user_wr->wr.atomic.remote_addr;
1501                                 next->wr.atomic.compare_add =
1502                                         user_wr->wr.atomic.compare_add;
1503                                 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1504                                 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1505                                 break;
1506                         default:
1507                                 break;
1508                         }
1509                 }
1510
1511                 if (next->num_sge) {
1512                         next->sg_list = (void *) next +
1513                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
1514                         if (copy_from_user(next->sg_list,
1515                                            buf + sizeof cmd +
1516                                            cmd.wr_count * cmd.wqe_size +
1517                                            sg_ind * sizeof (struct ib_sge),
1518                                            next->num_sge * sizeof (struct ib_sge))) {
1519                                 ret = -EFAULT;
1520                                 goto out_put;
1521                         }
1522                         sg_ind += next->num_sge;
1523                 } else
1524                         next->sg_list = NULL;
1525         }
1526
1527         resp.bad_wr = 0;
1528         ret = qp->device->post_send(qp, wr, &bad_wr);
1529         if (ret)
1530                 for (next = wr; next; next = next->next) {
1531                         ++resp.bad_wr;
1532                         if (next == bad_wr)
1533                                 break;
1534                 }
1535
1536         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1537                          &resp, sizeof resp))
1538                 ret = -EFAULT;
1539
1540 out_put:
1541         put_qp_read(qp);
1542
1543         while (wr) {
1544                 if (is_ud && wr->wr.ud.ah)
1545                         put_ah_read(wr->wr.ud.ah);
1546                 next = wr->next;
1547                 kfree(wr);
1548                 wr = next;
1549         }
1550
1551 out:
1552         kfree(user_wr);
1553
1554         return ret ? ret : in_len;
1555 }
1556
1557 static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1558                                                     int in_len,
1559                                                     u32 wr_count,
1560                                                     u32 sge_count,
1561                                                     u32 wqe_size)
1562 {
1563         struct ib_uverbs_recv_wr *user_wr;
1564         struct ib_recv_wr        *wr = NULL, *last, *next;
1565         int                       sg_ind;
1566         int                       i;
1567         int                       ret;
1568
1569         if (in_len < wqe_size * wr_count +
1570             sge_count * sizeof (struct ib_uverbs_sge))
1571                 return ERR_PTR(-EINVAL);
1572
1573         if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1574                 return ERR_PTR(-EINVAL);
1575
1576         user_wr = kmalloc(wqe_size, GFP_KERNEL);
1577         if (!user_wr)
1578                 return ERR_PTR(-ENOMEM);
1579
1580         sg_ind = 0;
1581         last = NULL;
1582         for (i = 0; i < wr_count; ++i) {
1583                 if (copy_from_user(user_wr, buf + i * wqe_size,
1584                                    wqe_size)) {
1585                         ret = -EFAULT;
1586                         goto err;
1587                 }
1588
1589                 if (user_wr->num_sge + sg_ind > sge_count) {
1590                         ret = -EINVAL;
1591                         goto err;
1592                 }
1593
1594                 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1595                                user_wr->num_sge * sizeof (struct ib_sge),
1596                                GFP_KERNEL);
1597                 if (!next) {
1598                         ret = -ENOMEM;
1599                         goto err;
1600                 }
1601
1602                 if (!last)
1603                         wr = next;
1604                 else
1605                         last->next = next;
1606                 last = next;
1607
1608                 next->next       = NULL;
1609                 next->wr_id      = user_wr->wr_id;
1610                 next->num_sge    = user_wr->num_sge;
1611
1612                 if (next->num_sge) {
1613                         next->sg_list = (void *) next +
1614                                 ALIGN(sizeof *next, sizeof (struct ib_sge));
1615                         if (copy_from_user(next->sg_list,
1616                                            buf + wr_count * wqe_size +
1617                                            sg_ind * sizeof (struct ib_sge),
1618                                            next->num_sge * sizeof (struct ib_sge))) {
1619                                 ret = -EFAULT;
1620                                 goto err;
1621                         }
1622                         sg_ind += next->num_sge;
1623                 } else
1624                         next->sg_list = NULL;
1625         }
1626
1627         kfree(user_wr);
1628         return wr;
1629
1630 err:
1631         kfree(user_wr);
1632
1633         while (wr) {
1634                 next = wr->next;
1635                 kfree(wr);
1636                 wr = next;
1637         }
1638
1639         return ERR_PTR(ret);
1640 }
1641
1642 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1643                             const char __user *buf, int in_len,
1644                             int out_len)
1645 {
1646         struct ib_uverbs_post_recv      cmd;
1647         struct ib_uverbs_post_recv_resp resp;
1648         struct ib_recv_wr              *wr, *next, *bad_wr;
1649         struct ib_qp                   *qp;
1650         ssize_t                         ret = -EINVAL;
1651
1652         if (copy_from_user(&cmd, buf, sizeof cmd))
1653                 return -EFAULT;
1654
1655         wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1656                                        in_len - sizeof cmd, cmd.wr_count,
1657                                        cmd.sge_count, cmd.wqe_size);
1658         if (IS_ERR(wr))
1659                 return PTR_ERR(wr);
1660
1661         qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1662         if (!qp)
1663                 goto out;
1664
1665         resp.bad_wr = 0;
1666         ret = qp->device->post_recv(qp, wr, &bad_wr);
1667
1668         put_qp_read(qp);
1669
1670         if (ret)
1671                 for (next = wr; next; next = next->next) {
1672                         ++resp.bad_wr;
1673                         if (next == bad_wr)
1674                                 break;
1675                 }
1676
1677
1678         if (copy_to_user((void __user *) (unsigned long) cmd.response,
1679                          &resp, sizeof resp))
1680                 ret = -EFAULT;
1681
1682 out:
1683         while (wr) {
1684                 next = wr->next;
1685                 kfree(wr);
1686                 wr = next;
1687         }
1688
1689         return ret ? ret : in_len;
1690 }
1691
1692 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1693                                 const char __user *buf, int in_len,
1694                                 int out_len)
1695 {
1696         struct ib_uverbs_post_srq_recv      cmd;
1697         struct ib_uverbs_post_srq_recv_resp resp;
1698         struct ib_recv_wr                  *wr, *next, *bad_wr;
1699         struct ib_srq                      *srq;
1700         ssize_t                             ret = -EINVAL;
1701
1702         if (copy_from_user(&cmd, buf, sizeof cmd))
1703                 return -EFAULT;
1704
1705         wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1706                                        in_len - sizeof cmd, cmd.wr_count,
1707                                        cmd.sge_count, cmd.wqe_size);
1708         if (IS_ERR(wr))
1709                 return PTR_ERR(wr);
1710
1711         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1712         if (!srq)
1713                 goto out;
1714
1715         resp.bad_wr = 0;
1716         ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1717
1718         put_srq_read(srq);
1719
1720         if (ret)
1721                 for (next = wr; next; next = next->next) {
1722                         ++resp.bad_wr;
1723                         if (next == bad_wr)
1724                                 break;
1725                 }
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_srq              *srq;
2059         struct ib_srq_attr          attr;
2060         int                         ret;
2061
2062         if (copy_from_user(&cmd, buf, sizeof cmd))
2063                 return -EFAULT;
2064
2065         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2066         if (!srq)
2067                 return -EINVAL;
2068
2069         attr.max_wr    = cmd.max_wr;
2070         attr.srq_limit = cmd.srq_limit;
2071
2072         ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
2073
2074         put_srq_read(srq);
2075
2076         return ret ? ret : in_len;
2077 }
2078
2079 ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2080                             const char __user *buf,
2081                             int in_len, int out_len)
2082 {
2083         struct ib_uverbs_query_srq      cmd;
2084         struct ib_uverbs_query_srq_resp resp;
2085         struct ib_srq_attr              attr;
2086         struct ib_srq                   *srq;
2087         int                             ret;
2088
2089         if (out_len < sizeof resp)
2090                 return -ENOSPC;
2091
2092         if (copy_from_user(&cmd, buf, sizeof cmd))
2093                 return -EFAULT;
2094
2095         srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2096         if (!srq)
2097                 return -EINVAL;
2098
2099         ret = ib_query_srq(srq, &attr);
2100
2101         put_srq_read(srq);
2102
2103         if (ret)
2104                 return ret;
2105
2106         memset(&resp, 0, sizeof resp);
2107
2108         resp.max_wr    = attr.max_wr;
2109         resp.max_sge   = attr.max_sge;
2110         resp.srq_limit = attr.srq_limit;
2111
2112         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2113                          &resp, sizeof resp))
2114                 return -EFAULT;
2115
2116         return in_len;
2117 }
2118
2119 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2120                               const char __user *buf, int in_len,
2121                               int out_len)
2122 {
2123         struct ib_uverbs_destroy_srq      cmd;
2124         struct ib_uverbs_destroy_srq_resp resp;
2125         struct ib_uobject                *uobj;
2126         struct ib_srq                    *srq;
2127         struct ib_uevent_object          *obj;
2128         int                               ret = -EINVAL;
2129
2130         if (copy_from_user(&cmd, buf, sizeof cmd))
2131                 return -EFAULT;
2132
2133         uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2134         if (!uobj)
2135                 return -EINVAL;
2136         srq = uobj->object;
2137         obj = container_of(uobj, struct ib_uevent_object, uobject);
2138
2139         ret = ib_destroy_srq(srq);
2140         if (!ret)
2141                 uobj->live = 0;
2142
2143         put_uobj_write(uobj);
2144
2145         if (ret)
2146                 return ret;
2147
2148         idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
2149
2150         mutex_lock(&file->mutex);
2151         list_del(&uobj->list);
2152         mutex_unlock(&file->mutex);
2153
2154         ib_uverbs_release_uevent(file, obj);
2155
2156         memset(&resp, 0, sizeof resp);
2157         resp.events_reported = obj->events_reported;
2158
2159         put_uobj(uobj);
2160
2161         if (copy_to_user((void __user *) (unsigned long) cmd.response,
2162                          &resp, sizeof resp))
2163                 ret = -EFAULT;
2164
2165         return ret ? ret : in_len;
2166 }