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