blob: 6c6fbff19752ecbcf2d4822965ff6bb09d7ee656 [file] [log] [blame]
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001/*
2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08003 * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07004 * Copyright (c) 2005 PathScale, Inc. All rights reserved.
Dotan Barak8bdb0e82006-02-13 16:31:57 -08005 * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
Roland Dreierbc38a6a2005-07-07 17:57:13 -07006 *
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.
Roland Dreierbc38a6a2005-07-07 17:57:13 -070034 */
35
Roland Dreier6b73597e2005-09-26 13:53:25 -070036#include <linux/file.h>
Roland Dreier70a30e12005-10-28 15:38:26 -070037#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Shachar Raindel8ada2c12014-12-11 17:04:17 +020039#include <linux/sched.h>
Roland Dreier6b73597e2005-09-26 13:53:25 -070040
Roland Dreierbc38a6a2005-07-07 17:57:13 -070041#include <asm/uaccess.h>
42
43#include "uverbs.h"
Or Gerlitzed4c54e2013-12-12 18:03:17 +020044#include "core_priv.h"
Roland Dreierbc38a6a2005-07-07 17:57:13 -070045
Roland Dreier3bea57a2012-04-30 10:27:26 -070046struct uverbs_lock_class {
47 struct lock_class_key key;
48 char name[16];
49};
50
51static struct uverbs_lock_class pd_lock_class = { .name = "PD-uobj" };
52static struct uverbs_lock_class mr_lock_class = { .name = "MR-uobj" };
Shani Michaeli6b52a122013-02-06 16:19:13 +000053static struct uverbs_lock_class mw_lock_class = { .name = "MW-uobj" };
Roland Dreier3bea57a2012-04-30 10:27:26 -070054static struct uverbs_lock_class cq_lock_class = { .name = "CQ-uobj" };
55static struct uverbs_lock_class qp_lock_class = { .name = "QP-uobj" };
56static struct uverbs_lock_class ah_lock_class = { .name = "AH-uobj" };
57static struct uverbs_lock_class srq_lock_class = { .name = "SRQ-uobj" };
58static struct uverbs_lock_class xrcd_lock_class = { .name = "XRCD-uobj" };
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +030059static struct uverbs_lock_class rule_lock_class = { .name = "RULE-uobj" };
Roland Dreierbc38a6a2005-07-07 17:57:13 -070060
Roland Dreier9ead1902006-06-17 20:44:49 -070061/*
62 * The ib_uobject locking scheme is as follows:
63 *
64 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
Mike Marciniszynd144da82015-11-02 12:13:25 -050065 * needs to be held during all idr write operations. When an object is
Roland Dreier9ead1902006-06-17 20:44:49 -070066 * looked up, a reference must be taken on the object's kref before
Mike Marciniszynd144da82015-11-02 12:13:25 -050067 * dropping this lock. For read operations, the rcu_read_lock()
68 * and rcu_write_lock() but similarly the kref reference is grabbed
69 * before the rcu_read_unlock().
Roland Dreier9ead1902006-06-17 20:44:49 -070070 *
71 * - Each object also has an rwsem. This rwsem must be held for
72 * reading while an operation that uses the object is performed.
73 * For example, while registering an MR, the associated PD's
74 * uobject.mutex must be held for reading. The rwsem must be held
75 * for writing while initializing or destroying an object.
76 *
77 * - In addition, each object has a "live" flag. If this flag is not
78 * set, then lookups of the object will fail even if it is found in
79 * the idr. This handles a reader that blocks and does not acquire
80 * the rwsem until after the object is destroyed. The destroy
81 * operation will set the live flag to 0 and then drop the rwsem;
82 * this will allow the reader to acquire the rwsem, see that the
83 * live flag is 0, and then drop the rwsem and its reference to
84 * object. The underlying storage will not be freed until the last
85 * reference to the object is dropped.
86 */
87
88static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
Roland Dreier3bea57a2012-04-30 10:27:26 -070089 struct ib_ucontext *context, struct uverbs_lock_class *c)
Roland Dreier9ead1902006-06-17 20:44:49 -070090{
91 uobj->user_handle = user_handle;
92 uobj->context = context;
93 kref_init(&uobj->ref);
94 init_rwsem(&uobj->mutex);
Roland Dreier3bea57a2012-04-30 10:27:26 -070095 lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
Roland Dreier9ead1902006-06-17 20:44:49 -070096 uobj->live = 0;
97}
98
99static void release_uobj(struct kref *kref)
100{
Mike Marciniszynd144da82015-11-02 12:13:25 -0500101 kfree_rcu(container_of(kref, struct ib_uobject, ref), rcu);
Roland Dreier9ead1902006-06-17 20:44:49 -0700102}
103
104static void put_uobj(struct ib_uobject *uobj)
105{
106 kref_put(&uobj->ref, release_uobj);
107}
108
109static void put_uobj_read(struct ib_uobject *uobj)
110{
111 up_read(&uobj->mutex);
112 put_uobj(uobj);
113}
114
115static void put_uobj_write(struct ib_uobject *uobj)
116{
117 up_write(&uobj->mutex);
118 put_uobj(uobj);
119}
120
121static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
Roland Dreier34631752006-06-17 20:37:40 -0700122{
123 int ret;
124
Tejun Heo3b069c52013-02-27 17:04:16 -0800125 idr_preload(GFP_KERNEL);
Roland Dreier9ead1902006-06-17 20:44:49 -0700126 spin_lock(&ib_uverbs_idr_lock);
Tejun Heo3b069c52013-02-27 17:04:16 -0800127
128 ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
129 if (ret >= 0)
130 uobj->id = ret;
131
Roland Dreier9ead1902006-06-17 20:44:49 -0700132 spin_unlock(&ib_uverbs_idr_lock);
Tejun Heo3b069c52013-02-27 17:04:16 -0800133 idr_preload_end();
Roland Dreier34631752006-06-17 20:37:40 -0700134
Tejun Heo3b069c52013-02-27 17:04:16 -0800135 return ret < 0 ? ret : 0;
Roland Dreier34631752006-06-17 20:37:40 -0700136}
137
Roland Dreier9ead1902006-06-17 20:44:49 -0700138void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
139{
140 spin_lock(&ib_uverbs_idr_lock);
141 idr_remove(idr, uobj->id);
142 spin_unlock(&ib_uverbs_idr_lock);
143}
144
145static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
146 struct ib_ucontext *context)
147{
148 struct ib_uobject *uobj;
149
Mike Marciniszynd144da82015-11-02 12:13:25 -0500150 rcu_read_lock();
Roland Dreier9ead1902006-06-17 20:44:49 -0700151 uobj = idr_find(idr, id);
Roland Dreiercbfb50e2007-10-19 20:01:43 -0700152 if (uobj) {
153 if (uobj->context == context)
154 kref_get(&uobj->ref);
155 else
156 uobj = NULL;
157 }
Mike Marciniszynd144da82015-11-02 12:13:25 -0500158 rcu_read_unlock();
Roland Dreier9ead1902006-06-17 20:44:49 -0700159
160 return uobj;
161}
162
163static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700164 struct ib_ucontext *context, int nested)
Roland Dreier9ead1902006-06-17 20:44:49 -0700165{
166 struct ib_uobject *uobj;
167
168 uobj = __idr_get_uobj(idr, id, context);
169 if (!uobj)
170 return NULL;
171
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700172 if (nested)
173 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
174 else
175 down_read(&uobj->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -0700176 if (!uobj->live) {
177 put_uobj_read(uobj);
178 return NULL;
179 }
180
181 return uobj;
182}
183
184static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
185 struct ib_ucontext *context)
186{
187 struct ib_uobject *uobj;
188
189 uobj = __idr_get_uobj(idr, id, context);
190 if (!uobj)
191 return NULL;
192
193 down_write(&uobj->mutex);
194 if (!uobj->live) {
195 put_uobj_write(uobj);
196 return NULL;
197 }
198
199 return uobj;
200}
201
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700202static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
203 int nested)
Roland Dreier9ead1902006-06-17 20:44:49 -0700204{
205 struct ib_uobject *uobj;
206
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700207 uobj = idr_read_uobj(idr, id, context, nested);
Roland Dreier9ead1902006-06-17 20:44:49 -0700208 return uobj ? uobj->object : NULL;
209}
210
211static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
212{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700213 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700214}
215
216static void put_pd_read(struct ib_pd *pd)
217{
218 put_uobj_read(pd->uobject);
219}
220
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700221static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
Roland Dreier9ead1902006-06-17 20:44:49 -0700222{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700223 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
Roland Dreier9ead1902006-06-17 20:44:49 -0700224}
225
226static void put_cq_read(struct ib_cq *cq)
227{
228 put_uobj_read(cq->uobject);
229}
230
231static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
232{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700233 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700234}
235
236static void put_ah_read(struct ib_ah *ah)
237{
238 put_uobj_read(ah->uobject);
239}
240
241static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
242{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700243 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700244}
245
Eli Cohene214a0f2012-01-03 20:36:48 -0800246static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
247{
248 struct ib_uobject *uobj;
249
250 uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
251 return uobj ? uobj->object : NULL;
252}
253
Roland Dreier9ead1902006-06-17 20:44:49 -0700254static void put_qp_read(struct ib_qp *qp)
255{
256 put_uobj_read(qp->uobject);
257}
258
Eli Cohene214a0f2012-01-03 20:36:48 -0800259static void put_qp_write(struct ib_qp *qp)
260{
261 put_uobj_write(qp->uobject);
262}
263
Roland Dreier9ead1902006-06-17 20:44:49 -0700264static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
265{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700266 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700267}
268
269static void put_srq_read(struct ib_srq *srq)
270{
271 put_uobj_read(srq->uobject);
272}
273
Sean Hefty53d0bd12011-05-24 08:33:46 -0700274static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
275 struct ib_uobject **uobj)
276{
277 *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
278 return *uobj ? (*uobj)->object : NULL;
279}
280
281static void put_xrcd_read(struct ib_uobject *uobj)
282{
283 put_uobj_read(uobj);
284}
285
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700286ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300287 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700288 const char __user *buf,
289 int in_len, int out_len)
290{
291 struct ib_uverbs_get_context cmd;
292 struct ib_uverbs_get_context_resp resp;
293 struct ib_udata udata;
Roland Dreier63c47c22005-09-26 13:01:03 -0700294 struct ib_ucontext *ucontext;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700295 struct file *filp;
Roland Dreier63c47c22005-09-26 13:01:03 -0700296 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700297
298 if (out_len < sizeof resp)
299 return -ENOSPC;
300
301 if (copy_from_user(&cmd, buf, sizeof cmd))
302 return -EFAULT;
303
Ingo Molnar95ed6442006-01-13 14:51:39 -0800304 mutex_lock(&file->mutex);
Roland Dreier63c47c22005-09-26 13:01:03 -0700305
306 if (file->ucontext) {
307 ret = -EINVAL;
308 goto err;
309 }
310
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700311 INIT_UDATA(&udata, buf + sizeof cmd,
312 (unsigned long) cmd.response + sizeof resp,
313 in_len - sizeof cmd, out_len - sizeof resp);
314
Yishai Hadas057aec02015-08-13 18:32:04 +0300315 ucontext = ib_dev->alloc_ucontext(ib_dev, &udata);
Ganapathi CH77f76012006-06-17 20:37:40 -0700316 if (IS_ERR(ucontext)) {
Roel Kluindf422452009-12-09 14:30:44 -0800317 ret = PTR_ERR(ucontext);
Ganapathi CH77f76012006-06-17 20:37:40 -0700318 goto err;
319 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700320
Yishai Hadas057aec02015-08-13 18:32:04 +0300321 ucontext->device = ib_dev;
Roland Dreier63c47c22005-09-26 13:01:03 -0700322 INIT_LIST_HEAD(&ucontext->pd_list);
323 INIT_LIST_HEAD(&ucontext->mr_list);
324 INIT_LIST_HEAD(&ucontext->mw_list);
325 INIT_LIST_HEAD(&ucontext->cq_list);
326 INIT_LIST_HEAD(&ucontext->qp_list);
327 INIT_LIST_HEAD(&ucontext->srq_list);
328 INIT_LIST_HEAD(&ucontext->ah_list);
Sean Hefty53d0bd12011-05-24 08:33:46 -0700329 INIT_LIST_HEAD(&ucontext->xrcd_list);
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +0300330 INIT_LIST_HEAD(&ucontext->rule_list);
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200331 rcu_read_lock();
332 ucontext->tgid = get_task_pid(current->group_leader, PIDTYPE_PID);
333 rcu_read_unlock();
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800334 ucontext->closing = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700335
Haggai Eran882214e2014-12-11 17:04:18 +0200336#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
337 ucontext->umem_tree = RB_ROOT;
338 init_rwsem(&ucontext->umem_rwsem);
339 ucontext->odp_mrs_count = 0;
340 INIT_LIST_HEAD(&ucontext->no_private_counters);
341
Or Gerlitz86bee4c2015-12-18 10:59:45 +0200342 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
Haggai Eran882214e2014-12-11 17:04:18 +0200343 ucontext->invalidate_range = NULL;
344
345#endif
346
Roland Dreier6b73597e2005-09-26 13:53:25 -0700347 resp.num_comp_vectors = file->device->num_comp_vectors;
348
Roland Dreierda183c72013-07-08 11:15:45 -0700349 ret = get_unused_fd_flags(O_CLOEXEC);
Al Virob1e45942010-01-18 01:38:00 -0500350 if (ret < 0)
351 goto err_free;
352 resp.async_fd = ret;
353
Yishai Hadas057aec02015-08-13 18:32:04 +0300354 filp = ib_uverbs_alloc_event_file(file, ib_dev, 1);
Roland Dreier6b73597e2005-09-26 13:53:25 -0700355 if (IS_ERR(filp)) {
356 ret = PTR_ERR(filp);
Al Virob1e45942010-01-18 01:38:00 -0500357 goto err_fd;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700358 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700359
360 if (copy_to_user((void __user *) (unsigned long) cmd.response,
Roland Dreier63c47c22005-09-26 13:01:03 -0700361 &resp, sizeof resp)) {
362 ret = -EFAULT;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700363 goto err_file;
Roland Dreier63c47c22005-09-26 13:01:03 -0700364 }
365
Roland Dreier70a30e12005-10-28 15:38:26 -0700366 file->ucontext = ucontext;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700367
368 fd_install(resp.async_fd, filp);
369
Ingo Molnar95ed6442006-01-13 14:51:39 -0800370 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700371
372 return in_len;
373
Roland Dreier6b73597e2005-09-26 13:53:25 -0700374err_file:
Yishai Hadas03c40442015-08-13 18:32:02 +0300375 ib_uverbs_free_async_event_file(file);
Roland Dreier6b73597e2005-09-26 13:53:25 -0700376 fput(filp);
377
Al Virob1e45942010-01-18 01:38:00 -0500378err_fd:
379 put_unused_fd(resp.async_fd);
380
Roland Dreier63c47c22005-09-26 13:01:03 -0700381err_free:
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200382 put_pid(ucontext->tgid);
Yishai Hadas057aec02015-08-13 18:32:04 +0300383 ib_dev->dealloc_ucontext(ucontext);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700384
Roland Dreier63c47c22005-09-26 13:01:03 -0700385err:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800386 mutex_unlock(&file->mutex);
Roland Dreier63c47c22005-09-26 13:01:03 -0700387 return ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700388}
389
Eli Cohen02d1aa72015-02-08 13:28:50 +0200390static void copy_query_dev_fields(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300391 struct ib_device *ib_dev,
Eli Cohen02d1aa72015-02-08 13:28:50 +0200392 struct ib_uverbs_query_device_resp *resp,
393 struct ib_device_attr *attr)
394{
395 resp->fw_ver = attr->fw_ver;
Yishai Hadas057aec02015-08-13 18:32:04 +0300396 resp->node_guid = ib_dev->node_guid;
Eli Cohen02d1aa72015-02-08 13:28:50 +0200397 resp->sys_image_guid = attr->sys_image_guid;
398 resp->max_mr_size = attr->max_mr_size;
399 resp->page_size_cap = attr->page_size_cap;
400 resp->vendor_id = attr->vendor_id;
401 resp->vendor_part_id = attr->vendor_part_id;
402 resp->hw_ver = attr->hw_ver;
403 resp->max_qp = attr->max_qp;
404 resp->max_qp_wr = attr->max_qp_wr;
405 resp->device_cap_flags = attr->device_cap_flags;
406 resp->max_sge = attr->max_sge;
407 resp->max_sge_rd = attr->max_sge_rd;
408 resp->max_cq = attr->max_cq;
409 resp->max_cqe = attr->max_cqe;
410 resp->max_mr = attr->max_mr;
411 resp->max_pd = attr->max_pd;
412 resp->max_qp_rd_atom = attr->max_qp_rd_atom;
413 resp->max_ee_rd_atom = attr->max_ee_rd_atom;
414 resp->max_res_rd_atom = attr->max_res_rd_atom;
415 resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
416 resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
417 resp->atomic_cap = attr->atomic_cap;
418 resp->max_ee = attr->max_ee;
419 resp->max_rdd = attr->max_rdd;
420 resp->max_mw = attr->max_mw;
421 resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
422 resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
423 resp->max_mcast_grp = attr->max_mcast_grp;
424 resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
425 resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
426 resp->max_ah = attr->max_ah;
427 resp->max_fmr = attr->max_fmr;
428 resp->max_map_per_fmr = attr->max_map_per_fmr;
429 resp->max_srq = attr->max_srq;
430 resp->max_srq_wr = attr->max_srq_wr;
431 resp->max_srq_sge = attr->max_srq_sge;
432 resp->max_pkeys = attr->max_pkeys;
433 resp->local_ca_ack_delay = attr->local_ca_ack_delay;
Yishai Hadas057aec02015-08-13 18:32:04 +0300434 resp->phys_port_cnt = ib_dev->phys_port_cnt;
Eli Cohen02d1aa72015-02-08 13:28:50 +0200435}
436
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700437ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300438 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700439 const char __user *buf,
440 int in_len, int out_len)
441{
442 struct ib_uverbs_query_device cmd;
443 struct ib_uverbs_query_device_resp resp;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700444
445 if (out_len < sizeof resp)
446 return -ENOSPC;
447
448 if (copy_from_user(&cmd, buf, sizeof cmd))
449 return -EFAULT;
450
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700451 memset(&resp, 0, sizeof resp);
Or Gerlitz86bee4c2015-12-18 10:59:45 +0200452 copy_query_dev_fields(file, ib_dev, &resp, &ib_dev->attrs);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700453
454 if (copy_to_user((void __user *) (unsigned long) cmd.response,
455 &resp, sizeof resp))
456 return -EFAULT;
457
458 return in_len;
459}
460
461ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300462 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700463 const char __user *buf,
464 int in_len, int out_len)
465{
466 struct ib_uverbs_query_port cmd;
467 struct ib_uverbs_query_port_resp resp;
468 struct ib_port_attr attr;
469 int ret;
470
471 if (out_len < sizeof resp)
472 return -ENOSPC;
473
474 if (copy_from_user(&cmd, buf, sizeof cmd))
475 return -EFAULT;
476
Yishai Hadas057aec02015-08-13 18:32:04 +0300477 ret = ib_query_port(ib_dev, cmd.port_num, &attr);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700478 if (ret)
479 return ret;
480
481 memset(&resp, 0, sizeof resp);
482
483 resp.state = attr.state;
484 resp.max_mtu = attr.max_mtu;
485 resp.active_mtu = attr.active_mtu;
486 resp.gid_tbl_len = attr.gid_tbl_len;
487 resp.port_cap_flags = attr.port_cap_flags;
488 resp.max_msg_sz = attr.max_msg_sz;
489 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
490 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
491 resp.pkey_tbl_len = attr.pkey_tbl_len;
492 resp.lid = attr.lid;
493 resp.sm_lid = attr.sm_lid;
494 resp.lmc = attr.lmc;
495 resp.max_vl_num = attr.max_vl_num;
496 resp.sm_sl = attr.sm_sl;
497 resp.subnet_timeout = attr.subnet_timeout;
498 resp.init_type_reply = attr.init_type_reply;
499 resp.active_width = attr.active_width;
500 resp.active_speed = attr.active_speed;
501 resp.phys_state = attr.phys_state;
Yishai Hadas057aec02015-08-13 18:32:04 +0300502 resp.link_layer = rdma_port_get_link_layer(ib_dev,
Eli Cohen2420b602010-10-18 14:45:20 -0700503 cmd.port_num);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700504
505 if (copy_to_user((void __user *) (unsigned long) cmd.response,
506 &resp, sizeof resp))
507 return -EFAULT;
508
509 return in_len;
510}
511
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700512ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300513 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700514 const char __user *buf,
515 int in_len, int out_len)
516{
517 struct ib_uverbs_alloc_pd cmd;
518 struct ib_uverbs_alloc_pd_resp resp;
519 struct ib_udata udata;
520 struct ib_uobject *uobj;
521 struct ib_pd *pd;
522 int ret;
523
524 if (out_len < sizeof resp)
525 return -ENOSPC;
526
527 if (copy_from_user(&cmd, buf, sizeof cmd))
528 return -EFAULT;
529
530 INIT_UDATA(&udata, buf + sizeof cmd,
531 (unsigned long) cmd.response + sizeof resp,
532 in_len - sizeof cmd, out_len - sizeof resp);
533
534 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
535 if (!uobj)
536 return -ENOMEM;
537
Roland Dreier3bea57a2012-04-30 10:27:26 -0700538 init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
Roland Dreier9ead1902006-06-17 20:44:49 -0700539 down_write(&uobj->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700540
Yishai Hadas057aec02015-08-13 18:32:04 +0300541 pd = ib_dev->alloc_pd(ib_dev, file->ucontext, &udata);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700542 if (IS_ERR(pd)) {
543 ret = PTR_ERR(pd);
544 goto err;
545 }
546
Yishai Hadas057aec02015-08-13 18:32:04 +0300547 pd->device = ib_dev;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700548 pd->uobject = uobj;
Jason Gunthorpe96249d72015-08-05 14:14:45 -0600549 pd->local_mr = NULL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700550 atomic_set(&pd->usecnt, 0);
551
Roland Dreier9ead1902006-06-17 20:44:49 -0700552 uobj->object = pd;
553 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700554 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -0700555 goto err_idr;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700556
557 memset(&resp, 0, sizeof resp);
558 resp.pd_handle = uobj->id;
559
560 if (copy_to_user((void __user *) (unsigned long) cmd.response,
561 &resp, sizeof resp)) {
562 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -0700563 goto err_copy;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700564 }
565
Ingo Molnar95ed6442006-01-13 14:51:39 -0800566 mutex_lock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700567 list_add_tail(&uobj->list, &file->ucontext->pd_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800568 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700569
Roland Dreier9ead1902006-06-17 20:44:49 -0700570 uobj->live = 1;
571
572 up_write(&uobj->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700573
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700574 return in_len;
575
Roland Dreier9ead1902006-06-17 20:44:49 -0700576err_copy:
577 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700578
Roland Dreier9ead1902006-06-17 20:44:49 -0700579err_idr:
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700580 ib_dealloc_pd(pd);
581
582err:
Roland Dreier9ead1902006-06-17 20:44:49 -0700583 put_uobj_write(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700584 return ret;
585}
586
587ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300588 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700589 const char __user *buf,
590 int in_len, int out_len)
591{
592 struct ib_uverbs_dealloc_pd cmd;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700593 struct ib_uobject *uobj;
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600594 struct ib_pd *pd;
Roland Dreier9ead1902006-06-17 20:44:49 -0700595 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700596
597 if (copy_from_user(&cmd, buf, sizeof cmd))
598 return -EFAULT;
599
Roland Dreier9ead1902006-06-17 20:44:49 -0700600 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
601 if (!uobj)
602 return -EINVAL;
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600603 pd = uobj->object;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700604
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600605 if (atomic_read(&pd->usecnt)) {
606 ret = -EBUSY;
607 goto err_put;
608 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700609
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600610 ret = pd->device->dealloc_pd(uobj->object);
611 WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700612 if (ret)
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600613 goto err_put;
614
615 uobj->live = 0;
616 put_uobj_write(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700617
Roland Dreier9ead1902006-06-17 20:44:49 -0700618 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700619
Ingo Molnar95ed6442006-01-13 14:51:39 -0800620 mutex_lock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700621 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800622 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700623
Roland Dreier9ead1902006-06-17 20:44:49 -0700624 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700625
Roland Dreier9ead1902006-06-17 20:44:49 -0700626 return in_len;
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600627
628err_put:
629 put_uobj_write(uobj);
630 return ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700631}
632
Sean Hefty53d0bd12011-05-24 08:33:46 -0700633struct xrcd_table_entry {
634 struct rb_node node;
635 struct ib_xrcd *xrcd;
636 struct inode *inode;
637};
638
639static int xrcd_table_insert(struct ib_uverbs_device *dev,
640 struct inode *inode,
641 struct ib_xrcd *xrcd)
642{
643 struct xrcd_table_entry *entry, *scan;
644 struct rb_node **p = &dev->xrcd_tree.rb_node;
645 struct rb_node *parent = NULL;
646
647 entry = kmalloc(sizeof *entry, GFP_KERNEL);
648 if (!entry)
649 return -ENOMEM;
650
651 entry->xrcd = xrcd;
652 entry->inode = inode;
653
654 while (*p) {
655 parent = *p;
656 scan = rb_entry(parent, struct xrcd_table_entry, node);
657
658 if (inode < scan->inode) {
659 p = &(*p)->rb_left;
660 } else if (inode > scan->inode) {
661 p = &(*p)->rb_right;
662 } else {
663 kfree(entry);
664 return -EEXIST;
665 }
666 }
667
668 rb_link_node(&entry->node, parent, p);
669 rb_insert_color(&entry->node, &dev->xrcd_tree);
670 igrab(inode);
671 return 0;
672}
673
674static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
675 struct inode *inode)
676{
677 struct xrcd_table_entry *entry;
678 struct rb_node *p = dev->xrcd_tree.rb_node;
679
680 while (p) {
681 entry = rb_entry(p, struct xrcd_table_entry, node);
682
683 if (inode < entry->inode)
684 p = p->rb_left;
685 else if (inode > entry->inode)
686 p = p->rb_right;
687 else
688 return entry;
689 }
690
691 return NULL;
692}
693
694static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
695{
696 struct xrcd_table_entry *entry;
697
698 entry = xrcd_table_search(dev, inode);
699 if (!entry)
700 return NULL;
701
702 return entry->xrcd;
703}
704
705static void xrcd_table_delete(struct ib_uverbs_device *dev,
706 struct inode *inode)
707{
708 struct xrcd_table_entry *entry;
709
710 entry = xrcd_table_search(dev, inode);
711 if (entry) {
712 iput(inode);
713 rb_erase(&entry->node, &dev->xrcd_tree);
714 kfree(entry);
715 }
716}
717
718ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300719 struct ib_device *ib_dev,
Sean Hefty53d0bd12011-05-24 08:33:46 -0700720 const char __user *buf, int in_len,
721 int out_len)
722{
723 struct ib_uverbs_open_xrcd cmd;
724 struct ib_uverbs_open_xrcd_resp resp;
725 struct ib_udata udata;
726 struct ib_uxrcd_object *obj;
727 struct ib_xrcd *xrcd = NULL;
Al Viro2903ff02012-08-28 12:52:22 -0400728 struct fd f = {NULL, 0};
Sean Hefty53d0bd12011-05-24 08:33:46 -0700729 struct inode *inode = NULL;
Al Viro2903ff02012-08-28 12:52:22 -0400730 int ret = 0;
Sean Hefty53d0bd12011-05-24 08:33:46 -0700731 int new_xrcd = 0;
732
733 if (out_len < sizeof resp)
734 return -ENOSPC;
735
736 if (copy_from_user(&cmd, buf, sizeof cmd))
737 return -EFAULT;
738
739 INIT_UDATA(&udata, buf + sizeof cmd,
740 (unsigned long) cmd.response + sizeof resp,
741 in_len - sizeof cmd, out_len - sizeof resp);
742
743 mutex_lock(&file->device->xrcd_tree_mutex);
744
745 if (cmd.fd != -1) {
746 /* search for file descriptor */
Al Viro2903ff02012-08-28 12:52:22 -0400747 f = fdget(cmd.fd);
748 if (!f.file) {
Sean Hefty53d0bd12011-05-24 08:33:46 -0700749 ret = -EBADF;
750 goto err_tree_mutex_unlock;
751 }
752
Al Viro496ad9a2013-01-23 17:07:38 -0500753 inode = file_inode(f.file);
Sean Hefty53d0bd12011-05-24 08:33:46 -0700754 xrcd = find_xrcd(file->device, inode);
755 if (!xrcd && !(cmd.oflags & O_CREAT)) {
756 /* no file descriptor. Need CREATE flag */
757 ret = -EAGAIN;
758 goto err_tree_mutex_unlock;
759 }
760
761 if (xrcd && cmd.oflags & O_EXCL) {
762 ret = -EINVAL;
763 goto err_tree_mutex_unlock;
764 }
765 }
766
767 obj = kmalloc(sizeof *obj, GFP_KERNEL);
768 if (!obj) {
769 ret = -ENOMEM;
770 goto err_tree_mutex_unlock;
771 }
772
Roland Dreier3bea57a2012-04-30 10:27:26 -0700773 init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
Sean Hefty53d0bd12011-05-24 08:33:46 -0700774
775 down_write(&obj->uobject.mutex);
776
777 if (!xrcd) {
Yishai Hadas057aec02015-08-13 18:32:04 +0300778 xrcd = ib_dev->alloc_xrcd(ib_dev, file->ucontext, &udata);
Sean Hefty53d0bd12011-05-24 08:33:46 -0700779 if (IS_ERR(xrcd)) {
780 ret = PTR_ERR(xrcd);
781 goto err;
782 }
783
784 xrcd->inode = inode;
Yishai Hadas057aec02015-08-13 18:32:04 +0300785 xrcd->device = ib_dev;
Sean Hefty53d0bd12011-05-24 08:33:46 -0700786 atomic_set(&xrcd->usecnt, 0);
787 mutex_init(&xrcd->tgt_qp_mutex);
788 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
789 new_xrcd = 1;
790 }
791
792 atomic_set(&obj->refcnt, 0);
793 obj->uobject.object = xrcd;
794 ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
795 if (ret)
796 goto err_idr;
797
798 memset(&resp, 0, sizeof resp);
799 resp.xrcd_handle = obj->uobject.id;
800
801 if (inode) {
802 if (new_xrcd) {
803 /* create new inode/xrcd table entry */
804 ret = xrcd_table_insert(file->device, inode, xrcd);
805 if (ret)
806 goto err_insert_xrcd;
807 }
808 atomic_inc(&xrcd->usecnt);
809 }
810
811 if (copy_to_user((void __user *) (unsigned long) cmd.response,
812 &resp, sizeof resp)) {
813 ret = -EFAULT;
814 goto err_copy;
815 }
816
Al Viro2903ff02012-08-28 12:52:22 -0400817 if (f.file)
818 fdput(f);
Sean Hefty53d0bd12011-05-24 08:33:46 -0700819
820 mutex_lock(&file->mutex);
821 list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
822 mutex_unlock(&file->mutex);
823
824 obj->uobject.live = 1;
825 up_write(&obj->uobject.mutex);
826
827 mutex_unlock(&file->device->xrcd_tree_mutex);
828 return in_len;
829
830err_copy:
831 if (inode) {
832 if (new_xrcd)
833 xrcd_table_delete(file->device, inode);
834 atomic_dec(&xrcd->usecnt);
835 }
836
837err_insert_xrcd:
838 idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
839
840err_idr:
841 ib_dealloc_xrcd(xrcd);
842
843err:
844 put_uobj_write(&obj->uobject);
845
846err_tree_mutex_unlock:
Al Viro2903ff02012-08-28 12:52:22 -0400847 if (f.file)
848 fdput(f);
Sean Hefty53d0bd12011-05-24 08:33:46 -0700849
850 mutex_unlock(&file->device->xrcd_tree_mutex);
851
852 return ret;
853}
854
855ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300856 struct ib_device *ib_dev,
Sean Hefty53d0bd12011-05-24 08:33:46 -0700857 const char __user *buf, int in_len,
858 int out_len)
859{
860 struct ib_uverbs_close_xrcd cmd;
861 struct ib_uobject *uobj;
862 struct ib_xrcd *xrcd = NULL;
863 struct inode *inode = NULL;
864 struct ib_uxrcd_object *obj;
865 int live;
866 int ret = 0;
867
868 if (copy_from_user(&cmd, buf, sizeof cmd))
869 return -EFAULT;
870
871 mutex_lock(&file->device->xrcd_tree_mutex);
872 uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
873 if (!uobj) {
874 ret = -EINVAL;
875 goto out;
876 }
877
878 xrcd = uobj->object;
879 inode = xrcd->inode;
880 obj = container_of(uobj, struct ib_uxrcd_object, uobject);
881 if (atomic_read(&obj->refcnt)) {
882 put_uobj_write(uobj);
883 ret = -EBUSY;
884 goto out;
885 }
886
887 if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
888 ret = ib_dealloc_xrcd(uobj->object);
889 if (!ret)
890 uobj->live = 0;
891 }
892
893 live = uobj->live;
894 if (inode && ret)
895 atomic_inc(&xrcd->usecnt);
896
897 put_uobj_write(uobj);
898
899 if (ret)
900 goto out;
901
902 if (inode && !live)
903 xrcd_table_delete(file->device, inode);
904
905 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
906 mutex_lock(&file->mutex);
907 list_del(&uobj->list);
908 mutex_unlock(&file->mutex);
909
910 put_uobj(uobj);
911 ret = in_len;
912
913out:
914 mutex_unlock(&file->device->xrcd_tree_mutex);
915 return ret;
916}
917
918void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
919 struct ib_xrcd *xrcd)
920{
921 struct inode *inode;
922
923 inode = xrcd->inode;
924 if (inode && !atomic_dec_and_test(&xrcd->usecnt))
925 return;
926
927 ib_dealloc_xrcd(xrcd);
928
929 if (inode)
930 xrcd_table_delete(dev, inode);
931}
932
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700933ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +0300934 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700935 const char __user *buf, int in_len,
936 int out_len)
937{
938 struct ib_uverbs_reg_mr cmd;
939 struct ib_uverbs_reg_mr_resp resp;
940 struct ib_udata udata;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800941 struct ib_uobject *uobj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700942 struct ib_pd *pd;
943 struct ib_mr *mr;
944 int ret;
945
946 if (out_len < sizeof resp)
947 return -ENOSPC;
948
949 if (copy_from_user(&cmd, buf, sizeof cmd))
950 return -EFAULT;
951
952 INIT_UDATA(&udata, buf + sizeof cmd,
953 (unsigned long) cmd.response + sizeof resp,
954 in_len - sizeof cmd, out_len - sizeof resp);
955
956 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
957 return -EINVAL;
958
Eli Cohen1c636f82013-10-31 15:26:32 +0200959 ret = ib_check_mr_access(cmd.access_flags);
960 if (ret)
961 return ret;
Roland Dreierf5753942005-10-03 09:18:02 -0700962
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800963 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
964 if (!uobj)
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700965 return -ENOMEM;
966
Roland Dreier3bea57a2012-04-30 10:27:26 -0700967 init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800968 down_write(&uobj->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700969
Roland Dreier9ead1902006-06-17 20:44:49 -0700970 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
Roland Dreieraaf1aef2007-02-22 13:16:51 -0800971 if (!pd) {
972 ret = -EINVAL;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800973 goto err_free;
Roland Dreieraaf1aef2007-02-22 13:16:51 -0800974 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700975
Sagi Grimberg860f10a2014-12-11 17:04:16 +0200976 if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
Or Gerlitz86bee4c2015-12-18 10:59:45 +0200977 if (!(pd->device->attrs.device_cap_flags &
978 IB_DEVICE_ON_DEMAND_PAGING)) {
Sagi Grimberg860f10a2014-12-11 17:04:16 +0200979 pr_debug("ODP support not available\n");
980 ret = -EINVAL;
981 goto err_put;
982 }
983 }
984
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800985 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
986 cmd.access_flags, &udata);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700987 if (IS_ERR(mr)) {
988 ret = PTR_ERR(mr);
Roland Dreier9ead1902006-06-17 20:44:49 -0700989 goto err_put;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700990 }
991
992 mr->device = pd->device;
993 mr->pd = pd;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800994 mr->uobject = uobj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700995 atomic_inc(&pd->usecnt);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700996
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800997 uobj->object = mr;
998 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700999 if (ret)
1000 goto err_unreg;
1001
Roland Dreier9ead1902006-06-17 20:44:49 -07001002 memset(&resp, 0, sizeof resp);
1003 resp.lkey = mr->lkey;
1004 resp.rkey = mr->rkey;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001005 resp.mr_handle = uobj->id;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001006
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001007 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1008 &resp, sizeof resp)) {
1009 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07001010 goto err_copy;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001011 }
1012
Roland Dreier9ead1902006-06-17 20:44:49 -07001013 put_pd_read(pd);
1014
Ingo Molnar95ed6442006-01-13 14:51:39 -08001015 mutex_lock(&file->mutex);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001016 list_add_tail(&uobj->list, &file->ucontext->mr_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001017 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07001018
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001019 uobj->live = 1;
Roland Dreier9ead1902006-06-17 20:44:49 -07001020
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001021 up_write(&uobj->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001022
1023 return in_len;
1024
Roland Dreier9ead1902006-06-17 20:44:49 -07001025err_copy:
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001026 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001027
1028err_unreg:
1029 ib_dereg_mr(mr);
1030
Roland Dreier9ead1902006-06-17 20:44:49 -07001031err_put:
1032 put_pd_read(pd);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001033
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001034err_free:
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001035 put_uobj_write(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001036 return ret;
1037}
1038
Matan Barak7e6edb92014-07-31 11:01:28 +03001039ssize_t ib_uverbs_rereg_mr(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001040 struct ib_device *ib_dev,
Matan Barak7e6edb92014-07-31 11:01:28 +03001041 const char __user *buf, int in_len,
1042 int out_len)
1043{
1044 struct ib_uverbs_rereg_mr cmd;
1045 struct ib_uverbs_rereg_mr_resp resp;
1046 struct ib_udata udata;
1047 struct ib_pd *pd = NULL;
1048 struct ib_mr *mr;
1049 struct ib_pd *old_pd;
1050 int ret;
1051 struct ib_uobject *uobj;
1052
1053 if (out_len < sizeof(resp))
1054 return -ENOSPC;
1055
1056 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1057 return -EFAULT;
1058
1059 INIT_UDATA(&udata, buf + sizeof(cmd),
1060 (unsigned long) cmd.response + sizeof(resp),
1061 in_len - sizeof(cmd), out_len - sizeof(resp));
1062
1063 if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
1064 return -EINVAL;
1065
1066 if ((cmd.flags & IB_MR_REREG_TRANS) &&
1067 (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
1068 (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
1069 return -EINVAL;
1070
1071 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle,
1072 file->ucontext);
1073
1074 if (!uobj)
1075 return -EINVAL;
1076
1077 mr = uobj->object;
1078
1079 if (cmd.flags & IB_MR_REREG_ACCESS) {
1080 ret = ib_check_mr_access(cmd.access_flags);
1081 if (ret)
1082 goto put_uobjs;
1083 }
1084
1085 if (cmd.flags & IB_MR_REREG_PD) {
1086 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1087 if (!pd) {
1088 ret = -EINVAL;
1089 goto put_uobjs;
1090 }
1091 }
1092
Matan Barak7e6edb92014-07-31 11:01:28 +03001093 old_pd = mr->pd;
1094 ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start,
1095 cmd.length, cmd.hca_va,
1096 cmd.access_flags, pd, &udata);
1097 if (!ret) {
1098 if (cmd.flags & IB_MR_REREG_PD) {
1099 atomic_inc(&pd->usecnt);
1100 mr->pd = pd;
1101 atomic_dec(&old_pd->usecnt);
1102 }
1103 } else {
1104 goto put_uobj_pd;
1105 }
1106
1107 memset(&resp, 0, sizeof(resp));
1108 resp.lkey = mr->lkey;
1109 resp.rkey = mr->rkey;
1110
1111 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1112 &resp, sizeof(resp)))
1113 ret = -EFAULT;
1114 else
1115 ret = in_len;
1116
1117put_uobj_pd:
1118 if (cmd.flags & IB_MR_REREG_PD)
1119 put_pd_read(pd);
1120
1121put_uobjs:
1122
1123 put_uobj_write(mr->uobject);
1124
1125 return ret;
1126}
1127
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001128ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001129 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001130 const char __user *buf, int in_len,
1131 int out_len)
1132{
1133 struct ib_uverbs_dereg_mr cmd;
1134 struct ib_mr *mr;
Roland Dreier9ead1902006-06-17 20:44:49 -07001135 struct ib_uobject *uobj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001136 int ret = -EINVAL;
1137
1138 if (copy_from_user(&cmd, buf, sizeof cmd))
1139 return -EFAULT;
1140
Roland Dreier9ead1902006-06-17 20:44:49 -07001141 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
1142 if (!uobj)
1143 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001144
Roland Dreierf7c6a7b2007-03-04 16:15:11 -08001145 mr = uobj->object;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001146
1147 ret = ib_dereg_mr(mr);
Roland Dreier9ead1902006-06-17 20:44:49 -07001148 if (!ret)
1149 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001150
Roland Dreier9ead1902006-06-17 20:44:49 -07001151 put_uobj_write(uobj);
1152
1153 if (ret)
1154 return ret;
1155
1156 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001157
Ingo Molnar95ed6442006-01-13 14:51:39 -08001158 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001159 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001160 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001161
Roland Dreier9ead1902006-06-17 20:44:49 -07001162 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001163
Roland Dreier9ead1902006-06-17 20:44:49 -07001164 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001165}
1166
Shani Michaeli6b52a122013-02-06 16:19:13 +00001167ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001168 struct ib_device *ib_dev,
1169 const char __user *buf, int in_len,
1170 int out_len)
Shani Michaeli6b52a122013-02-06 16:19:13 +00001171{
1172 struct ib_uverbs_alloc_mw cmd;
1173 struct ib_uverbs_alloc_mw_resp resp;
1174 struct ib_uobject *uobj;
1175 struct ib_pd *pd;
1176 struct ib_mw *mw;
1177 int ret;
1178
1179 if (out_len < sizeof(resp))
1180 return -ENOSPC;
1181
1182 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1183 return -EFAULT;
1184
1185 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
1186 if (!uobj)
1187 return -ENOMEM;
1188
1189 init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
1190 down_write(&uobj->mutex);
1191
1192 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1193 if (!pd) {
1194 ret = -EINVAL;
1195 goto err_free;
1196 }
1197
1198 mw = pd->device->alloc_mw(pd, cmd.mw_type);
1199 if (IS_ERR(mw)) {
1200 ret = PTR_ERR(mw);
1201 goto err_put;
1202 }
1203
1204 mw->device = pd->device;
1205 mw->pd = pd;
1206 mw->uobject = uobj;
1207 atomic_inc(&pd->usecnt);
1208
1209 uobj->object = mw;
1210 ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
1211 if (ret)
1212 goto err_unalloc;
1213
1214 memset(&resp, 0, sizeof(resp));
1215 resp.rkey = mw->rkey;
1216 resp.mw_handle = uobj->id;
1217
1218 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1219 &resp, sizeof(resp))) {
1220 ret = -EFAULT;
1221 goto err_copy;
1222 }
1223
1224 put_pd_read(pd);
1225
1226 mutex_lock(&file->mutex);
1227 list_add_tail(&uobj->list, &file->ucontext->mw_list);
1228 mutex_unlock(&file->mutex);
1229
1230 uobj->live = 1;
1231
1232 up_write(&uobj->mutex);
1233
1234 return in_len;
1235
1236err_copy:
1237 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1238
1239err_unalloc:
Christoph Hellwigfeb7c1e2015-12-23 19:12:48 +01001240 uverbs_dealloc_mw(mw);
Shani Michaeli6b52a122013-02-06 16:19:13 +00001241
1242err_put:
1243 put_pd_read(pd);
1244
1245err_free:
1246 put_uobj_write(uobj);
1247 return ret;
1248}
1249
1250ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001251 struct ib_device *ib_dev,
1252 const char __user *buf, int in_len,
1253 int out_len)
Shani Michaeli6b52a122013-02-06 16:19:13 +00001254{
1255 struct ib_uverbs_dealloc_mw cmd;
1256 struct ib_mw *mw;
1257 struct ib_uobject *uobj;
1258 int ret = -EINVAL;
1259
1260 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1261 return -EFAULT;
1262
1263 uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
1264 if (!uobj)
1265 return -EINVAL;
1266
1267 mw = uobj->object;
1268
Christoph Hellwigfeb7c1e2015-12-23 19:12:48 +01001269 ret = uverbs_dealloc_mw(mw);
Shani Michaeli6b52a122013-02-06 16:19:13 +00001270 if (!ret)
1271 uobj->live = 0;
1272
1273 put_uobj_write(uobj);
1274
1275 if (ret)
1276 return ret;
1277
1278 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
1279
1280 mutex_lock(&file->mutex);
1281 list_del(&uobj->list);
1282 mutex_unlock(&file->mutex);
1283
1284 put_uobj(uobj);
1285
1286 return in_len;
1287}
1288
Roland Dreier6b73597e2005-09-26 13:53:25 -07001289ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001290 struct ib_device *ib_dev,
Roland Dreier6b73597e2005-09-26 13:53:25 -07001291 const char __user *buf, int in_len,
1292 int out_len)
1293{
1294 struct ib_uverbs_create_comp_channel cmd;
1295 struct ib_uverbs_create_comp_channel_resp resp;
1296 struct file *filp;
Al Virob1e45942010-01-18 01:38:00 -05001297 int ret;
Roland Dreier6b73597e2005-09-26 13:53:25 -07001298
1299 if (out_len < sizeof resp)
1300 return -ENOSPC;
1301
1302 if (copy_from_user(&cmd, buf, sizeof cmd))
1303 return -EFAULT;
1304
Roland Dreierda183c72013-07-08 11:15:45 -07001305 ret = get_unused_fd_flags(O_CLOEXEC);
Al Virob1e45942010-01-18 01:38:00 -05001306 if (ret < 0)
1307 return ret;
1308 resp.fd = ret;
1309
Yishai Hadas057aec02015-08-13 18:32:04 +03001310 filp = ib_uverbs_alloc_event_file(file, ib_dev, 0);
Al Virob1e45942010-01-18 01:38:00 -05001311 if (IS_ERR(filp)) {
1312 put_unused_fd(resp.fd);
Roland Dreier6b73597e2005-09-26 13:53:25 -07001313 return PTR_ERR(filp);
Al Virob1e45942010-01-18 01:38:00 -05001314 }
Roland Dreier6b73597e2005-09-26 13:53:25 -07001315
1316 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1317 &resp, sizeof resp)) {
1318 put_unused_fd(resp.fd);
1319 fput(filp);
1320 return -EFAULT;
1321 }
1322
1323 fd_install(resp.fd, filp);
1324 return in_len;
1325}
1326
Matan Barak565197d2015-06-11 16:35:23 +03001327static struct ib_ucq_object *create_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001328 struct ib_device *ib_dev,
Matan Barak565197d2015-06-11 16:35:23 +03001329 struct ib_udata *ucore,
1330 struct ib_udata *uhw,
1331 struct ib_uverbs_ex_create_cq *cmd,
1332 size_t cmd_sz,
1333 int (*cb)(struct ib_uverbs_file *file,
1334 struct ib_ucq_object *obj,
1335 struct ib_uverbs_ex_create_cq_resp *resp,
1336 struct ib_udata *udata,
1337 void *context),
1338 void *context)
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001339{
Roland Dreier9ead1902006-06-17 20:44:49 -07001340 struct ib_ucq_object *obj;
Roland Dreier6b73597e2005-09-26 13:53:25 -07001341 struct ib_uverbs_event_file *ev_file = NULL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001342 struct ib_cq *cq;
1343 int ret;
Matan Barak565197d2015-06-11 16:35:23 +03001344 struct ib_uverbs_ex_create_cq_resp resp;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001345 struct ib_cq_init_attr attr = {};
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001346
Matan Barak565197d2015-06-11 16:35:23 +03001347 if (cmd->comp_vector >= file->device->num_comp_vectors)
1348 return ERR_PTR(-EINVAL);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001349
Roland Dreier9ead1902006-06-17 20:44:49 -07001350 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1351 if (!obj)
Matan Barak565197d2015-06-11 16:35:23 +03001352 return ERR_PTR(-ENOMEM);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001353
Matan Barak565197d2015-06-11 16:35:23 +03001354 init_uobj(&obj->uobject, cmd->user_handle, file->ucontext, &cq_lock_class);
Roland Dreier9ead1902006-06-17 20:44:49 -07001355 down_write(&obj->uobject.mutex);
1356
Matan Barak565197d2015-06-11 16:35:23 +03001357 if (cmd->comp_channel >= 0) {
1358 ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel);
Jack Morgensteinac4e7b32006-01-06 16:43:14 -08001359 if (!ev_file) {
1360 ret = -EINVAL;
1361 goto err;
1362 }
1363 }
1364
Roland Dreier9ead1902006-06-17 20:44:49 -07001365 obj->uverbs_file = file;
1366 obj->comp_events_reported = 0;
1367 obj->async_events_reported = 0;
1368 INIT_LIST_HEAD(&obj->comp_list);
1369 INIT_LIST_HEAD(&obj->async_list);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001370
Matan Barak565197d2015-06-11 16:35:23 +03001371 attr.cqe = cmd->cqe;
1372 attr.comp_vector = cmd->comp_vector;
1373
1374 if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
1375 attr.flags = cmd->flags;
1376
Yishai Hadas057aec02015-08-13 18:32:04 +03001377 cq = ib_dev->create_cq(ib_dev, &attr,
Matan Barak565197d2015-06-11 16:35:23 +03001378 file->ucontext, uhw);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001379 if (IS_ERR(cq)) {
1380 ret = PTR_ERR(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -07001381 goto err_file;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001382 }
1383
Yishai Hadas057aec02015-08-13 18:32:04 +03001384 cq->device = ib_dev;
Roland Dreier9ead1902006-06-17 20:44:49 -07001385 cq->uobject = &obj->uobject;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001386 cq->comp_handler = ib_uverbs_comp_handler;
1387 cq->event_handler = ib_uverbs_cq_event_handler;
Roland Dreier6b73597e2005-09-26 13:53:25 -07001388 cq->cq_context = ev_file;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001389 atomic_set(&cq->usecnt, 0);
1390
Roland Dreier9ead1902006-06-17 20:44:49 -07001391 obj->uobject.object = cq;
1392 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001393 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -07001394 goto err_free;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001395
1396 memset(&resp, 0, sizeof resp);
Matan Barak565197d2015-06-11 16:35:23 +03001397 resp.base.cq_handle = obj->uobject.id;
1398 resp.base.cqe = cq->cqe;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001399
Matan Barak565197d2015-06-11 16:35:23 +03001400 resp.response_length = offsetof(typeof(resp), response_length) +
1401 sizeof(resp.response_length);
1402
1403 ret = cb(file, obj, &resp, ucore, context);
1404 if (ret)
1405 goto err_cb;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001406
Ingo Molnar95ed6442006-01-13 14:51:39 -08001407 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001408 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001409 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07001410
Roland Dreier9ead1902006-06-17 20:44:49 -07001411 obj->uobject.live = 1;
1412
1413 up_write(&obj->uobject.mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07001414
Matan Barak565197d2015-06-11 16:35:23 +03001415 return obj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001416
Matan Barak565197d2015-06-11 16:35:23 +03001417err_cb:
Roland Dreier9ead1902006-06-17 20:44:49 -07001418 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001419
Roland Dreier9ead1902006-06-17 20:44:49 -07001420err_free:
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001421 ib_destroy_cq(cq);
1422
Roland Dreier9ead1902006-06-17 20:44:49 -07001423err_file:
Jack Morgensteinac4e7b32006-01-06 16:43:14 -08001424 if (ev_file)
Roland Dreier9ead1902006-06-17 20:44:49 -07001425 ib_uverbs_release_ucq(file, ev_file, obj);
1426
1427err:
1428 put_uobj_write(&obj->uobject);
Matan Barak565197d2015-06-11 16:35:23 +03001429
1430 return ERR_PTR(ret);
1431}
1432
1433static int ib_uverbs_create_cq_cb(struct ib_uverbs_file *file,
1434 struct ib_ucq_object *obj,
1435 struct ib_uverbs_ex_create_cq_resp *resp,
1436 struct ib_udata *ucore, void *context)
1437{
1438 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1439 return -EFAULT;
1440
1441 return 0;
1442}
1443
1444ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001445 struct ib_device *ib_dev,
Matan Barak565197d2015-06-11 16:35:23 +03001446 const char __user *buf, int in_len,
1447 int out_len)
1448{
1449 struct ib_uverbs_create_cq cmd;
1450 struct ib_uverbs_ex_create_cq cmd_ex;
1451 struct ib_uverbs_create_cq_resp resp;
1452 struct ib_udata ucore;
1453 struct ib_udata uhw;
1454 struct ib_ucq_object *obj;
1455
1456 if (out_len < sizeof(resp))
1457 return -ENOSPC;
1458
1459 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1460 return -EFAULT;
1461
Arnd Bergmann5d1e6232015-10-07 14:29:51 +02001462 INIT_UDATA(&ucore, buf, (unsigned long)cmd.response, sizeof(cmd), sizeof(resp));
Matan Barak565197d2015-06-11 16:35:23 +03001463
1464 INIT_UDATA(&uhw, buf + sizeof(cmd),
1465 (unsigned long)cmd.response + sizeof(resp),
1466 in_len - sizeof(cmd), out_len - sizeof(resp));
1467
1468 memset(&cmd_ex, 0, sizeof(cmd_ex));
1469 cmd_ex.user_handle = cmd.user_handle;
1470 cmd_ex.cqe = cmd.cqe;
1471 cmd_ex.comp_vector = cmd.comp_vector;
1472 cmd_ex.comp_channel = cmd.comp_channel;
1473
Yishai Hadas057aec02015-08-13 18:32:04 +03001474 obj = create_cq(file, ib_dev, &ucore, &uhw, &cmd_ex,
Matan Barak565197d2015-06-11 16:35:23 +03001475 offsetof(typeof(cmd_ex), comp_channel) +
1476 sizeof(cmd.comp_channel), ib_uverbs_create_cq_cb,
1477 NULL);
1478
1479 if (IS_ERR(obj))
1480 return PTR_ERR(obj);
1481
1482 return in_len;
1483}
1484
1485static int ib_uverbs_ex_create_cq_cb(struct ib_uverbs_file *file,
1486 struct ib_ucq_object *obj,
1487 struct ib_uverbs_ex_create_cq_resp *resp,
1488 struct ib_udata *ucore, void *context)
1489{
1490 if (ib_copy_to_udata(ucore, resp, resp->response_length))
1491 return -EFAULT;
1492
1493 return 0;
1494}
1495
1496int ib_uverbs_ex_create_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001497 struct ib_device *ib_dev,
Matan Barak565197d2015-06-11 16:35:23 +03001498 struct ib_udata *ucore,
1499 struct ib_udata *uhw)
1500{
1501 struct ib_uverbs_ex_create_cq_resp resp;
1502 struct ib_uverbs_ex_create_cq cmd;
1503 struct ib_ucq_object *obj;
1504 int err;
1505
1506 if (ucore->inlen < sizeof(cmd))
1507 return -EINVAL;
1508
1509 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
1510 if (err)
1511 return err;
1512
1513 if (cmd.comp_mask)
1514 return -EINVAL;
1515
1516 if (cmd.reserved)
1517 return -EINVAL;
1518
1519 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
1520 sizeof(resp.response_length)))
1521 return -ENOSPC;
1522
Yishai Hadas057aec02015-08-13 18:32:04 +03001523 obj = create_cq(file, ib_dev, ucore, uhw, &cmd,
Matan Barak565197d2015-06-11 16:35:23 +03001524 min(ucore->inlen, sizeof(cmd)),
1525 ib_uverbs_ex_create_cq_cb, NULL);
1526
1527 if (IS_ERR(obj))
1528 return PTR_ERR(obj);
1529
1530 return 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001531}
1532
Roland Dreier33b9b3e2006-01-30 14:29:21 -08001533ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001534 struct ib_device *ib_dev,
Roland Dreier33b9b3e2006-01-30 14:29:21 -08001535 const char __user *buf, int in_len,
1536 int out_len)
1537{
1538 struct ib_uverbs_resize_cq cmd;
1539 struct ib_uverbs_resize_cq_resp resp;
1540 struct ib_udata udata;
1541 struct ib_cq *cq;
1542 int ret = -EINVAL;
1543
1544 if (copy_from_user(&cmd, buf, sizeof cmd))
1545 return -EFAULT;
1546
1547 INIT_UDATA(&udata, buf + sizeof cmd,
1548 (unsigned long) cmd.response + sizeof resp,
1549 in_len - sizeof cmd, out_len - sizeof resp);
1550
Roland Dreier1ccf6aa2006-09-22 15:17:20 -07001551 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -07001552 if (!cq)
1553 return -EINVAL;
Roland Dreier33b9b3e2006-01-30 14:29:21 -08001554
1555 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
1556 if (ret)
1557 goto out;
1558
Roland Dreier33b9b3e2006-01-30 14:29:21 -08001559 resp.cqe = cq->cqe;
1560
1561 if (copy_to_user((void __user *) (unsigned long) cmd.response,
Ralph Campbell64f817b2006-09-22 15:22:24 -07001562 &resp, sizeof resp.cqe))
Roland Dreier33b9b3e2006-01-30 14:29:21 -08001563 ret = -EFAULT;
1564
1565out:
Roland Dreier9ead1902006-06-17 20:44:49 -07001566 put_cq_read(cq);
Roland Dreier33b9b3e2006-01-30 14:29:21 -08001567
1568 return ret ? ret : in_len;
1569}
1570
Dan Carpenter7182afe2010-10-13 09:13:12 +00001571static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
1572{
1573 struct ib_uverbs_wc tmp;
1574
1575 tmp.wr_id = wc->wr_id;
1576 tmp.status = wc->status;
1577 tmp.opcode = wc->opcode;
1578 tmp.vendor_err = wc->vendor_err;
1579 tmp.byte_len = wc->byte_len;
1580 tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
1581 tmp.qp_num = wc->qp->qp_num;
1582 tmp.src_qp = wc->src_qp;
1583 tmp.wc_flags = wc->wc_flags;
1584 tmp.pkey_index = wc->pkey_index;
1585 tmp.slid = wc->slid;
1586 tmp.sl = wc->sl;
1587 tmp.dlid_path_bits = wc->dlid_path_bits;
1588 tmp.port_num = wc->port_num;
1589 tmp.reserved = 0;
1590
1591 if (copy_to_user(dest, &tmp, sizeof tmp))
1592 return -EFAULT;
1593
1594 return 0;
1595}
1596
Roland Dreier67cdb402005-10-14 15:26:04 -07001597ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001598 struct ib_device *ib_dev,
Roland Dreier67cdb402005-10-14 15:26:04 -07001599 const char __user *buf, int in_len,
1600 int out_len)
1601{
1602 struct ib_uverbs_poll_cq cmd;
Dan Carpenter7182afe2010-10-13 09:13:12 +00001603 struct ib_uverbs_poll_cq_resp resp;
1604 u8 __user *header_ptr;
1605 u8 __user *data_ptr;
Roland Dreier67cdb402005-10-14 15:26:04 -07001606 struct ib_cq *cq;
Dan Carpenter7182afe2010-10-13 09:13:12 +00001607 struct ib_wc wc;
1608 int ret;
Roland Dreier67cdb402005-10-14 15:26:04 -07001609
1610 if (copy_from_user(&cmd, buf, sizeof cmd))
1611 return -EFAULT;
1612
Roland Dreier1ccf6aa2006-09-22 15:17:20 -07001613 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
Dan Carpenter7182afe2010-10-13 09:13:12 +00001614 if (!cq)
1615 return -EINVAL;
1616
1617 /* we copy a struct ib_uverbs_poll_cq_resp to user space */
1618 header_ptr = (void __user *)(unsigned long) cmd.response;
1619 data_ptr = header_ptr + sizeof resp;
1620
1621 memset(&resp, 0, sizeof resp);
1622 while (resp.count < cmd.ne) {
1623 ret = ib_poll_cq(cq, 1, &wc);
1624 if (ret < 0)
1625 goto out_put;
1626 if (!ret)
1627 break;
1628
1629 ret = copy_wc_to_user(data_ptr, &wc);
1630 if (ret)
1631 goto out_put;
1632
1633 data_ptr += sizeof(struct ib_uverbs_wc);
1634 ++resp.count;
Roland Dreier67cdb402005-10-14 15:26:04 -07001635 }
1636
Dan Carpenter7182afe2010-10-13 09:13:12 +00001637 if (copy_to_user(header_ptr, &resp, sizeof resp)) {
Roland Dreier67cdb402005-10-14 15:26:04 -07001638 ret = -EFAULT;
Dan Carpenter7182afe2010-10-13 09:13:12 +00001639 goto out_put;
1640 }
Roland Dreier67cdb402005-10-14 15:26:04 -07001641
Dan Carpenter7182afe2010-10-13 09:13:12 +00001642 ret = in_len;
Roland Dreier67cdb402005-10-14 15:26:04 -07001643
Dan Carpenter7182afe2010-10-13 09:13:12 +00001644out_put:
1645 put_cq_read(cq);
1646 return ret;
Roland Dreier67cdb402005-10-14 15:26:04 -07001647}
1648
1649ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001650 struct ib_device *ib_dev,
Roland Dreier67cdb402005-10-14 15:26:04 -07001651 const char __user *buf, int in_len,
1652 int out_len)
1653{
1654 struct ib_uverbs_req_notify_cq cmd;
1655 struct ib_cq *cq;
Roland Dreier67cdb402005-10-14 15:26:04 -07001656
1657 if (copy_from_user(&cmd, buf, sizeof cmd))
1658 return -EFAULT;
1659
Roland Dreier1ccf6aa2006-09-22 15:17:20 -07001660 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
Roland Dreierab108672006-09-22 15:17:19 -07001661 if (!cq)
Roland Dreier9ead1902006-06-17 20:44:49 -07001662 return -EINVAL;
Roland Dreier67cdb402005-10-14 15:26:04 -07001663
Roland Dreier9ead1902006-06-17 20:44:49 -07001664 ib_req_notify_cq(cq, cmd.solicited_only ?
1665 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1666
Roland Dreierab108672006-09-22 15:17:19 -07001667 put_cq_read(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -07001668
1669 return in_len;
Roland Dreier67cdb402005-10-14 15:26:04 -07001670}
1671
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001672ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03001673 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001674 const char __user *buf, int in_len,
1675 int out_len)
1676{
Roland Dreier63aaf642005-09-09 15:55:08 -07001677 struct ib_uverbs_destroy_cq cmd;
1678 struct ib_uverbs_destroy_cq_resp resp;
Roland Dreier9ead1902006-06-17 20:44:49 -07001679 struct ib_uobject *uobj;
Roland Dreier63aaf642005-09-09 15:55:08 -07001680 struct ib_cq *cq;
Roland Dreier9ead1902006-06-17 20:44:49 -07001681 struct ib_ucq_object *obj;
Roland Dreier6b73597e2005-09-26 13:53:25 -07001682 struct ib_uverbs_event_file *ev_file;
Roland Dreier63aaf642005-09-09 15:55:08 -07001683 int ret = -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001684
1685 if (copy_from_user(&cmd, buf, sizeof cmd))
1686 return -EFAULT;
1687
Roland Dreier9ead1902006-06-17 20:44:49 -07001688 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1689 if (!uobj)
1690 return -EINVAL;
1691 cq = uobj->object;
1692 ev_file = cq->cq_context;
1693 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001694
1695 ret = ib_destroy_cq(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -07001696 if (!ret)
1697 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001698
Roland Dreier9ead1902006-06-17 20:44:49 -07001699 put_uobj_write(uobj);
1700
1701 if (ret)
1702 return ret;
1703
1704 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001705
Ingo Molnar95ed6442006-01-13 14:51:39 -08001706 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001707 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001708 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001709
Roland Dreier9ead1902006-06-17 20:44:49 -07001710 ib_uverbs_release_ucq(file, ev_file, obj);
Roland Dreier63aaf642005-09-09 15:55:08 -07001711
Roland Dreier9ead1902006-06-17 20:44:49 -07001712 memset(&resp, 0, sizeof resp);
1713 resp.comp_events_reported = obj->comp_events_reported;
1714 resp.async_events_reported = obj->async_events_reported;
Roland Dreier63aaf642005-09-09 15:55:08 -07001715
Roland Dreier9ead1902006-06-17 20:44:49 -07001716 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001717
Roland Dreier63aaf642005-09-09 15:55:08 -07001718 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1719 &resp, sizeof resp))
Roland Dreier9ead1902006-06-17 20:44:49 -07001720 return -EFAULT;
Roland Dreier63aaf642005-09-09 15:55:08 -07001721
Roland Dreier9ead1902006-06-17 20:44:49 -07001722 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001723}
1724
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001725static int create_qp(struct ib_uverbs_file *file,
1726 struct ib_udata *ucore,
1727 struct ib_udata *uhw,
1728 struct ib_uverbs_ex_create_qp *cmd,
1729 size_t cmd_sz,
1730 int (*cb)(struct ib_uverbs_file *file,
1731 struct ib_uverbs_ex_create_qp_resp *resp,
1732 struct ib_udata *udata),
1733 void *context)
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001734{
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001735 struct ib_uqp_object *obj;
1736 struct ib_device *device;
1737 struct ib_pd *pd = NULL;
1738 struct ib_xrcd *xrcd = NULL;
1739 struct ib_uobject *uninitialized_var(xrcd_uobj);
1740 struct ib_cq *scq = NULL, *rcq = NULL;
1741 struct ib_srq *srq = NULL;
1742 struct ib_qp *qp;
1743 char *buf;
1744 struct ib_qp_init_attr attr;
1745 struct ib_uverbs_ex_create_qp_resp resp;
1746 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001747
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001748 if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
Or Gerlitzc938a612012-03-01 12:17:51 +02001749 return -EPERM;
1750
Yishai Hadas846be902013-08-01 18:49:54 +03001751 obj = kzalloc(sizeof *obj, GFP_KERNEL);
Roland Dreier9ead1902006-06-17 20:44:49 -07001752 if (!obj)
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001753 return -ENOMEM;
1754
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001755 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext,
1756 &qp_lock_class);
Roland Dreier9ead1902006-06-17 20:44:49 -07001757 down_write(&obj->uevent.uobject.mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001758
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001759 if (cmd->qp_type == IB_QPT_XRC_TGT) {
1760 xrcd = idr_read_xrcd(cmd->pd_handle, file->ucontext,
1761 &xrcd_uobj);
Sean Heftyb93f3c12011-05-27 00:00:12 -07001762 if (!xrcd) {
1763 ret = -EINVAL;
1764 goto err_put;
1765 }
1766 device = xrcd->device;
Sean Hefty9977f4f2011-05-26 08:17:04 -07001767 } else {
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001768 if (cmd->qp_type == IB_QPT_XRC_INI) {
1769 cmd->max_recv_wr = 0;
1770 cmd->max_recv_sge = 0;
Sean Heftyb93f3c12011-05-27 00:00:12 -07001771 } else {
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001772 if (cmd->is_srq) {
1773 srq = idr_read_srq(cmd->srq_handle,
1774 file->ucontext);
Sean Heftyb93f3c12011-05-27 00:00:12 -07001775 if (!srq || srq->srq_type != IB_SRQT_BASIC) {
1776 ret = -EINVAL;
1777 goto err_put;
1778 }
1779 }
Roland Dreier5909ce52012-04-30 12:51:50 -07001780
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001781 if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1782 rcq = idr_read_cq(cmd->recv_cq_handle,
1783 file->ucontext, 0);
Roland Dreier5909ce52012-04-30 12:51:50 -07001784 if (!rcq) {
1785 ret = -EINVAL;
1786 goto err_put;
1787 }
Sean Hefty9977f4f2011-05-26 08:17:04 -07001788 }
1789 }
Roland Dreier5909ce52012-04-30 12:51:50 -07001790
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001791 scq = idr_read_cq(cmd->send_cq_handle, file->ucontext, !!rcq);
Roland Dreier5909ce52012-04-30 12:51:50 -07001792 rcq = rcq ?: scq;
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001793 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
Roland Dreier5909ce52012-04-30 12:51:50 -07001794 if (!pd || !scq) {
1795 ret = -EINVAL;
1796 goto err_put;
1797 }
1798
Sean Heftyb93f3c12011-05-27 00:00:12 -07001799 device = pd->device;
Sean Hefty9977f4f2011-05-26 08:17:04 -07001800 }
1801
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001802 attr.event_handler = ib_uverbs_qp_event_handler;
1803 attr.qp_context = file;
1804 attr.send_cq = scq;
1805 attr.recv_cq = rcq;
Roland Dreierf520ba52005-08-18 12:24:13 -07001806 attr.srq = srq;
Sean Heftyb93f3c12011-05-27 00:00:12 -07001807 attr.xrcd = xrcd;
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001808 attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1809 IB_SIGNAL_REQ_WR;
1810 attr.qp_type = cmd->qp_type;
Eli Cohenb846f252008-04-16 21:09:27 -07001811 attr.create_flags = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001812
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001813 attr.cap.max_send_wr = cmd->max_send_wr;
1814 attr.cap.max_recv_wr = cmd->max_recv_wr;
1815 attr.cap.max_send_sge = cmd->max_send_sge;
1816 attr.cap.max_recv_sge = cmd->max_recv_sge;
1817 attr.cap.max_inline_data = cmd->max_inline_data;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001818
Roland Dreier9ead1902006-06-17 20:44:49 -07001819 obj->uevent.events_reported = 0;
1820 INIT_LIST_HEAD(&obj->uevent.event_list);
1821 INIT_LIST_HEAD(&obj->mcast_list);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001822
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001823 if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
1824 sizeof(cmd->create_flags))
1825 attr.create_flags = cmd->create_flags;
1826
Leon Romanovsky8a06ce52015-12-20 12:16:10 +02001827 if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1828 IB_QP_CREATE_CROSS_CHANNEL |
1829 IB_QP_CREATE_MANAGED_SEND |
1830 IB_QP_CREATE_MANAGED_RECV)) {
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001831 ret = -EINVAL;
1832 goto err_put;
1833 }
1834
1835 buf = (void *)cmd + sizeof(*cmd);
1836 if (cmd_sz > sizeof(*cmd))
1837 if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
1838 cmd_sz - sizeof(*cmd) - 1))) {
1839 ret = -EINVAL;
1840 goto err_put;
1841 }
1842
1843 if (cmd->qp_type == IB_QPT_XRC_TGT)
Sean Heftyb93f3c12011-05-27 00:00:12 -07001844 qp = ib_create_qp(pd, &attr);
1845 else
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001846 qp = device->create_qp(pd, &attr, uhw);
Sean Heftyb93f3c12011-05-27 00:00:12 -07001847
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001848 if (IS_ERR(qp)) {
1849 ret = PTR_ERR(qp);
Roland Dreier9ead1902006-06-17 20:44:49 -07001850 goto err_put;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001851 }
1852
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001853 if (cmd->qp_type != IB_QPT_XRC_TGT) {
Sean Hefty0e0ec7e2011-08-08 15:31:51 -07001854 qp->real_qp = qp;
Sean Heftyb93f3c12011-05-27 00:00:12 -07001855 qp->device = device;
1856 qp->pd = pd;
1857 qp->send_cq = attr.send_cq;
1858 qp->recv_cq = attr.recv_cq;
1859 qp->srq = attr.srq;
1860 qp->event_handler = attr.event_handler;
1861 qp->qp_context = attr.qp_context;
1862 qp->qp_type = attr.qp_type;
Bernd Schuberte47e3212012-01-20 18:43:54 +00001863 atomic_set(&qp->usecnt, 0);
Sean Heftyb93f3c12011-05-27 00:00:12 -07001864 atomic_inc(&pd->usecnt);
1865 atomic_inc(&attr.send_cq->usecnt);
1866 if (attr.recv_cq)
1867 atomic_inc(&attr.recv_cq->usecnt);
1868 if (attr.srq)
1869 atomic_inc(&attr.srq->usecnt);
1870 }
1871 qp->uobject = &obj->uevent.uobject;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001872
Roland Dreier9ead1902006-06-17 20:44:49 -07001873 obj->uevent.uobject.object = qp;
1874 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001875 if (ret)
1876 goto err_destroy;
1877
Roland Dreier9ead1902006-06-17 20:44:49 -07001878 memset(&resp, 0, sizeof resp);
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001879 resp.base.qpn = qp->qp_num;
1880 resp.base.qp_handle = obj->uevent.uobject.id;
1881 resp.base.max_recv_sge = attr.cap.max_recv_sge;
1882 resp.base.max_send_sge = attr.cap.max_send_sge;
1883 resp.base.max_recv_wr = attr.cap.max_recv_wr;
1884 resp.base.max_send_wr = attr.cap.max_send_wr;
1885 resp.base.max_inline_data = attr.cap.max_inline_data;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001886
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001887 resp.response_length = offsetof(typeof(resp), response_length) +
1888 sizeof(resp.response_length);
1889
1890 ret = cb(file, &resp, ucore);
1891 if (ret)
1892 goto err_cb;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001893
Yishai Hadas846be902013-08-01 18:49:54 +03001894 if (xrcd) {
1895 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1896 uobject);
1897 atomic_inc(&obj->uxrcd->refcnt);
Sean Heftyb93f3c12011-05-27 00:00:12 -07001898 put_xrcd_read(xrcd_uobj);
Yishai Hadas846be902013-08-01 18:49:54 +03001899 }
1900
Sean Heftyb93f3c12011-05-27 00:00:12 -07001901 if (pd)
1902 put_pd_read(pd);
1903 if (scq)
1904 put_cq_read(scq);
Sean Hefty9977f4f2011-05-26 08:17:04 -07001905 if (rcq && rcq != scq)
Roland Dreier43db2bc2006-07-23 15:16:04 -07001906 put_cq_read(rcq);
Roland Dreier9ead1902006-06-17 20:44:49 -07001907 if (srq)
1908 put_srq_read(srq);
1909
Ingo Molnar95ed6442006-01-13 14:51:39 -08001910 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001911 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001912 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07001913
Roland Dreier9ead1902006-06-17 20:44:49 -07001914 obj->uevent.uobject.live = 1;
1915
1916 up_write(&obj->uevent.uobject.mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001917
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001918 return 0;
1919err_cb:
Roland Dreier9ead1902006-06-17 20:44:49 -07001920 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001921
1922err_destroy:
1923 ib_destroy_qp(qp);
1924
Roland Dreier9ead1902006-06-17 20:44:49 -07001925err_put:
Sean Heftyb93f3c12011-05-27 00:00:12 -07001926 if (xrcd)
1927 put_xrcd_read(xrcd_uobj);
Roland Dreier9ead1902006-06-17 20:44:49 -07001928 if (pd)
1929 put_pd_read(pd);
1930 if (scq)
1931 put_cq_read(scq);
Roland Dreier43db2bc2006-07-23 15:16:04 -07001932 if (rcq && rcq != scq)
Roland Dreier9ead1902006-06-17 20:44:49 -07001933 put_cq_read(rcq);
1934 if (srq)
1935 put_srq_read(srq);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001936
Roland Dreier9ead1902006-06-17 20:44:49 -07001937 put_uobj_write(&obj->uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001938 return ret;
1939}
1940
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001941static int ib_uverbs_create_qp_cb(struct ib_uverbs_file *file,
1942 struct ib_uverbs_ex_create_qp_resp *resp,
1943 struct ib_udata *ucore)
1944{
1945 if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
1946 return -EFAULT;
1947
1948 return 0;
1949}
1950
1951ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1952 struct ib_device *ib_dev,
1953 const char __user *buf, int in_len,
1954 int out_len)
1955{
1956 struct ib_uverbs_create_qp cmd;
1957 struct ib_uverbs_ex_create_qp cmd_ex;
1958 struct ib_udata ucore;
1959 struct ib_udata uhw;
1960 ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
1961 int err;
1962
1963 if (out_len < resp_size)
1964 return -ENOSPC;
1965
1966 if (copy_from_user(&cmd, buf, sizeof(cmd)))
1967 return -EFAULT;
1968
1969 INIT_UDATA(&ucore, buf, (unsigned long)cmd.response, sizeof(cmd),
1970 resp_size);
1971 INIT_UDATA(&uhw, buf + sizeof(cmd),
1972 (unsigned long)cmd.response + resp_size,
Majd Dibbiny3d943c92016-02-14 18:35:52 +02001973 in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
1974 out_len - resp_size);
Eran Ben Elisha6d8a7492015-10-21 17:00:42 +03001975
1976 memset(&cmd_ex, 0, sizeof(cmd_ex));
1977 cmd_ex.user_handle = cmd.user_handle;
1978 cmd_ex.pd_handle = cmd.pd_handle;
1979 cmd_ex.send_cq_handle = cmd.send_cq_handle;
1980 cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1981 cmd_ex.srq_handle = cmd.srq_handle;
1982 cmd_ex.max_send_wr = cmd.max_send_wr;
1983 cmd_ex.max_recv_wr = cmd.max_recv_wr;
1984 cmd_ex.max_send_sge = cmd.max_send_sge;
1985 cmd_ex.max_recv_sge = cmd.max_recv_sge;
1986 cmd_ex.max_inline_data = cmd.max_inline_data;
1987 cmd_ex.sq_sig_all = cmd.sq_sig_all;
1988 cmd_ex.qp_type = cmd.qp_type;
1989 cmd_ex.is_srq = cmd.is_srq;
1990
1991 err = create_qp(file, &ucore, &uhw, &cmd_ex,
1992 offsetof(typeof(cmd_ex), is_srq) +
1993 sizeof(cmd.is_srq), ib_uverbs_create_qp_cb,
1994 NULL);
1995
1996 if (err)
1997 return err;
1998
1999 return in_len;
2000}
2001
2002static int ib_uverbs_ex_create_qp_cb(struct ib_uverbs_file *file,
2003 struct ib_uverbs_ex_create_qp_resp *resp,
2004 struct ib_udata *ucore)
2005{
2006 if (ib_copy_to_udata(ucore, resp, resp->response_length))
2007 return -EFAULT;
2008
2009 return 0;
2010}
2011
2012int ib_uverbs_ex_create_qp(struct ib_uverbs_file *file,
2013 struct ib_device *ib_dev,
2014 struct ib_udata *ucore,
2015 struct ib_udata *uhw)
2016{
2017 struct ib_uverbs_ex_create_qp_resp resp;
2018 struct ib_uverbs_ex_create_qp cmd = {0};
2019 int err;
2020
2021 if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
2022 sizeof(cmd.comp_mask)))
2023 return -EINVAL;
2024
2025 err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
2026 if (err)
2027 return err;
2028
2029 if (cmd.comp_mask)
2030 return -EINVAL;
2031
2032 if (cmd.reserved)
2033 return -EINVAL;
2034
2035 if (ucore->outlen < (offsetof(typeof(resp), response_length) +
2036 sizeof(resp.response_length)))
2037 return -ENOSPC;
2038
2039 err = create_qp(file, ucore, uhw, &cmd,
2040 min(ucore->inlen, sizeof(cmd)),
2041 ib_uverbs_ex_create_qp_cb, NULL);
2042
2043 if (err)
2044 return err;
2045
2046 return 0;
2047}
2048
Sean Hefty42849b22011-08-11 13:57:43 -07002049ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002050 struct ib_device *ib_dev,
Sean Hefty42849b22011-08-11 13:57:43 -07002051 const char __user *buf, int in_len, int out_len)
2052{
2053 struct ib_uverbs_open_qp cmd;
2054 struct ib_uverbs_create_qp_resp resp;
2055 struct ib_udata udata;
2056 struct ib_uqp_object *obj;
2057 struct ib_xrcd *xrcd;
2058 struct ib_uobject *uninitialized_var(xrcd_uobj);
2059 struct ib_qp *qp;
2060 struct ib_qp_open_attr attr;
2061 int ret;
2062
2063 if (out_len < sizeof resp)
2064 return -ENOSPC;
2065
2066 if (copy_from_user(&cmd, buf, sizeof cmd))
2067 return -EFAULT;
2068
2069 INIT_UDATA(&udata, buf + sizeof cmd,
2070 (unsigned long) cmd.response + sizeof resp,
2071 in_len - sizeof cmd, out_len - sizeof resp);
2072
2073 obj = kmalloc(sizeof *obj, GFP_KERNEL);
2074 if (!obj)
2075 return -ENOMEM;
2076
Roland Dreier3bea57a2012-04-30 10:27:26 -07002077 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
Sean Hefty42849b22011-08-11 13:57:43 -07002078 down_write(&obj->uevent.uobject.mutex);
2079
2080 xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
2081 if (!xrcd) {
2082 ret = -EINVAL;
2083 goto err_put;
2084 }
2085
2086 attr.event_handler = ib_uverbs_qp_event_handler;
2087 attr.qp_context = file;
2088 attr.qp_num = cmd.qpn;
2089 attr.qp_type = cmd.qp_type;
2090
2091 obj->uevent.events_reported = 0;
2092 INIT_LIST_HEAD(&obj->uevent.event_list);
2093 INIT_LIST_HEAD(&obj->mcast_list);
2094
2095 qp = ib_open_qp(xrcd, &attr);
2096 if (IS_ERR(qp)) {
2097 ret = PTR_ERR(qp);
2098 goto err_put;
2099 }
2100
2101 qp->uobject = &obj->uevent.uobject;
2102
2103 obj->uevent.uobject.object = qp;
2104 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
2105 if (ret)
2106 goto err_destroy;
2107
2108 memset(&resp, 0, sizeof resp);
2109 resp.qpn = qp->qp_num;
2110 resp.qp_handle = obj->uevent.uobject.id;
2111
2112 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2113 &resp, sizeof resp)) {
2114 ret = -EFAULT;
2115 goto err_remove;
2116 }
2117
Yishai Hadas846be902013-08-01 18:49:54 +03002118 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
2119 atomic_inc(&obj->uxrcd->refcnt);
Sean Hefty42849b22011-08-11 13:57:43 -07002120 put_xrcd_read(xrcd_uobj);
2121
2122 mutex_lock(&file->mutex);
2123 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
2124 mutex_unlock(&file->mutex);
2125
2126 obj->uevent.uobject.live = 1;
2127
2128 up_write(&obj->uevent.uobject.mutex);
2129
2130 return in_len;
2131
2132err_remove:
2133 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
2134
2135err_destroy:
2136 ib_destroy_qp(qp);
2137
2138err_put:
2139 put_xrcd_read(xrcd_uobj);
2140 put_uobj_write(&obj->uevent.uobject);
2141 return ret;
2142}
2143
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002144ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002145 struct ib_device *ib_dev,
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002146 const char __user *buf, int in_len,
2147 int out_len)
2148{
2149 struct ib_uverbs_query_qp cmd;
2150 struct ib_uverbs_query_qp_resp resp;
2151 struct ib_qp *qp;
2152 struct ib_qp_attr *attr;
2153 struct ib_qp_init_attr *init_attr;
2154 int ret;
2155
2156 if (copy_from_user(&cmd, buf, sizeof cmd))
2157 return -EFAULT;
2158
2159 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2160 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
2161 if (!attr || !init_attr) {
2162 ret = -ENOMEM;
2163 goto out;
2164 }
2165
Roland Dreier9ead1902006-06-17 20:44:49 -07002166 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2167 if (!qp) {
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002168 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07002169 goto out;
2170 }
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002171
Roland Dreier9ead1902006-06-17 20:44:49 -07002172 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
2173
2174 put_qp_read(qp);
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002175
2176 if (ret)
2177 goto out;
2178
2179 memset(&resp, 0, sizeof resp);
2180
2181 resp.qp_state = attr->qp_state;
2182 resp.cur_qp_state = attr->cur_qp_state;
2183 resp.path_mtu = attr->path_mtu;
2184 resp.path_mig_state = attr->path_mig_state;
2185 resp.qkey = attr->qkey;
2186 resp.rq_psn = attr->rq_psn;
2187 resp.sq_psn = attr->sq_psn;
2188 resp.dest_qp_num = attr->dest_qp_num;
2189 resp.qp_access_flags = attr->qp_access_flags;
2190 resp.pkey_index = attr->pkey_index;
2191 resp.alt_pkey_index = attr->alt_pkey_index;
Jack Morgenstein0b26c882006-10-25 12:54:20 +02002192 resp.sq_draining = attr->sq_draining;
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002193 resp.max_rd_atomic = attr->max_rd_atomic;
2194 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
2195 resp.min_rnr_timer = attr->min_rnr_timer;
2196 resp.port_num = attr->port_num;
2197 resp.timeout = attr->timeout;
2198 resp.retry_cnt = attr->retry_cnt;
2199 resp.rnr_retry = attr->rnr_retry;
2200 resp.alt_port_num = attr->alt_port_num;
2201 resp.alt_timeout = attr->alt_timeout;
2202
2203 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
2204 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
2205 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
2206 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
2207 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
2208 resp.dest.dlid = attr->ah_attr.dlid;
2209 resp.dest.sl = attr->ah_attr.sl;
2210 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
2211 resp.dest.static_rate = attr->ah_attr.static_rate;
2212 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
2213 resp.dest.port_num = attr->ah_attr.port_num;
2214
2215 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
2216 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
2217 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
2218 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
2219 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
2220 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
2221 resp.alt_dest.sl = attr->alt_ah_attr.sl;
2222 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
2223 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
2224 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
2225 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
2226
2227 resp.max_send_wr = init_attr->cap.max_send_wr;
2228 resp.max_recv_wr = init_attr->cap.max_recv_wr;
2229 resp.max_send_sge = init_attr->cap.max_send_sge;
2230 resp.max_recv_sge = init_attr->cap.max_recv_sge;
2231 resp.max_inline_data = init_attr->cap.max_inline_data;
Dotan Barak27d56302006-03-02 11:25:27 -08002232 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
Dotan Barak7ccc9a22006-02-13 16:31:25 -08002233
2234 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2235 &resp, sizeof resp))
2236 ret = -EFAULT;
2237
2238out:
2239 kfree(attr);
2240 kfree(init_attr);
2241
2242 return ret ? ret : in_len;
2243}
2244
Sean Hefty9977f4f2011-05-26 08:17:04 -07002245/* Remove ignored fields set in the attribute mask */
2246static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
2247{
2248 switch (qp_type) {
2249 case IB_QPT_XRC_INI:
2250 return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
Sean Heftyb93f3c12011-05-27 00:00:12 -07002251 case IB_QPT_XRC_TGT:
2252 return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
2253 IB_QP_RNR_RETRY);
Sean Hefty9977f4f2011-05-26 08:17:04 -07002254 default:
2255 return mask;
2256 }
2257}
2258
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002259ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002260 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002261 const char __user *buf, int in_len,
2262 int out_len)
2263{
2264 struct ib_uverbs_modify_qp cmd;
Ralph Campbell9bc57e22006-08-11 14:58:09 -07002265 struct ib_udata udata;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002266 struct ib_qp *qp;
2267 struct ib_qp_attr *attr;
2268 int ret;
2269
2270 if (copy_from_user(&cmd, buf, sizeof cmd))
2271 return -EFAULT;
2272
Ralph Campbell9bc57e22006-08-11 14:58:09 -07002273 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2274 out_len);
2275
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002276 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2277 if (!attr)
2278 return -ENOMEM;
2279
Roland Dreier9ead1902006-06-17 20:44:49 -07002280 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2281 if (!qp) {
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002282 ret = -EINVAL;
2283 goto out;
2284 }
2285
2286 attr->qp_state = cmd.qp_state;
2287 attr->cur_qp_state = cmd.cur_qp_state;
2288 attr->path_mtu = cmd.path_mtu;
2289 attr->path_mig_state = cmd.path_mig_state;
2290 attr->qkey = cmd.qkey;
2291 attr->rq_psn = cmd.rq_psn;
2292 attr->sq_psn = cmd.sq_psn;
2293 attr->dest_qp_num = cmd.dest_qp_num;
2294 attr->qp_access_flags = cmd.qp_access_flags;
2295 attr->pkey_index = cmd.pkey_index;
Ami Perlmutter702b2aa2006-03-20 10:08:24 -08002296 attr->alt_pkey_index = cmd.alt_pkey_index;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002297 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
2298 attr->max_rd_atomic = cmd.max_rd_atomic;
2299 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
2300 attr->min_rnr_timer = cmd.min_rnr_timer;
2301 attr->port_num = cmd.port_num;
2302 attr->timeout = cmd.timeout;
2303 attr->retry_cnt = cmd.retry_cnt;
2304 attr->rnr_retry = cmd.rnr_retry;
2305 attr->alt_port_num = cmd.alt_port_num;
2306 attr->alt_timeout = cmd.alt_timeout;
2307
2308 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
2309 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
2310 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
2311 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
2312 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
2313 attr->ah_attr.dlid = cmd.dest.dlid;
2314 attr->ah_attr.sl = cmd.dest.sl;
2315 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
2316 attr->ah_attr.static_rate = cmd.dest.static_rate;
2317 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
2318 attr->ah_attr.port_num = cmd.dest.port_num;
2319
2320 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
2321 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
2322 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
2323 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
2324 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
2325 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
2326 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
2327 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
2328 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
2329 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
2330 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
2331
Sean Hefty0e0ec7e2011-08-08 15:31:51 -07002332 if (qp->real_qp == qp) {
Matan Barakdbf727d2015-10-15 18:38:51 +03002333 ret = ib_resolve_eth_dmac(qp, attr, &cmd.attr_mask);
Or Gerlitzed4c54e2013-12-12 18:03:17 +02002334 if (ret)
Moshe Lazer0fb8bcf2015-02-05 13:53:52 +02002335 goto release_qp;
Sean Hefty0e0ec7e2011-08-08 15:31:51 -07002336 ret = qp->device->modify_qp(qp, attr,
2337 modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
2338 } else {
2339 ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
2340 }
Roland Dreier9ead1902006-06-17 20:44:49 -07002341
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002342 if (ret)
Moshe Lazer0fb8bcf2015-02-05 13:53:52 +02002343 goto release_qp;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002344
2345 ret = in_len;
2346
Moshe Lazer0fb8bcf2015-02-05 13:53:52 +02002347release_qp:
2348 put_qp_read(qp);
2349
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002350out:
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002351 kfree(attr);
2352
2353 return ret;
2354}
2355
2356ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002357 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002358 const char __user *buf, int in_len,
2359 int out_len)
2360{
Roland Dreier63aaf642005-09-09 15:55:08 -07002361 struct ib_uverbs_destroy_qp cmd;
2362 struct ib_uverbs_destroy_qp_resp resp;
Roland Dreier9ead1902006-06-17 20:44:49 -07002363 struct ib_uobject *uobj;
Roland Dreier63aaf642005-09-09 15:55:08 -07002364 struct ib_qp *qp;
Roland Dreier9ead1902006-06-17 20:44:49 -07002365 struct ib_uqp_object *obj;
Roland Dreier63aaf642005-09-09 15:55:08 -07002366 int ret = -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002367
2368 if (copy_from_user(&cmd, buf, sizeof cmd))
2369 return -EFAULT;
2370
Roland Dreier63aaf642005-09-09 15:55:08 -07002371 memset(&resp, 0, sizeof resp);
2372
Roland Dreier9ead1902006-06-17 20:44:49 -07002373 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
2374 if (!uobj)
2375 return -EINVAL;
2376 qp = uobj->object;
2377 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002378
Roland Dreier9ead1902006-06-17 20:44:49 -07002379 if (!list_empty(&obj->mcast_list)) {
2380 put_uobj_write(uobj);
2381 return -EBUSY;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002382 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002383
2384 ret = ib_destroy_qp(qp);
Roland Dreier9ead1902006-06-17 20:44:49 -07002385 if (!ret)
2386 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002387
Roland Dreier9ead1902006-06-17 20:44:49 -07002388 put_uobj_write(uobj);
2389
2390 if (ret)
2391 return ret;
2392
Yishai Hadas846be902013-08-01 18:49:54 +03002393 if (obj->uxrcd)
2394 atomic_dec(&obj->uxrcd->refcnt);
2395
Roland Dreier9ead1902006-06-17 20:44:49 -07002396 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002397
Ingo Molnar95ed6442006-01-13 14:51:39 -08002398 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07002399 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08002400 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002401
Roland Dreier9ead1902006-06-17 20:44:49 -07002402 ib_uverbs_release_uevent(file, &obj->uevent);
Roland Dreier63aaf642005-09-09 15:55:08 -07002403
Roland Dreier9ead1902006-06-17 20:44:49 -07002404 resp.events_reported = obj->uevent.events_reported;
Roland Dreier63aaf642005-09-09 15:55:08 -07002405
Roland Dreier9ead1902006-06-17 20:44:49 -07002406 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002407
Roland Dreier63aaf642005-09-09 15:55:08 -07002408 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2409 &resp, sizeof resp))
Roland Dreier9ead1902006-06-17 20:44:49 -07002410 return -EFAULT;
Roland Dreier63aaf642005-09-09 15:55:08 -07002411
Roland Dreier9ead1902006-06-17 20:44:49 -07002412 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002413}
2414
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002415static void *alloc_wr(size_t wr_size, __u32 num_sge)
2416{
2417 return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
2418 num_sge * sizeof (struct ib_sge), GFP_KERNEL);
2419};
2420
Roland Dreier67cdb402005-10-14 15:26:04 -07002421ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002422 struct ib_device *ib_dev,
Roland Dreiera74cd4a2006-02-13 16:30:49 -08002423 const char __user *buf, int in_len,
2424 int out_len)
Roland Dreier67cdb402005-10-14 15:26:04 -07002425{
2426 struct ib_uverbs_post_send cmd;
2427 struct ib_uverbs_post_send_resp resp;
2428 struct ib_uverbs_send_wr *user_wr;
2429 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
2430 struct ib_qp *qp;
2431 int i, sg_ind;
Roland Dreier9ead1902006-06-17 20:44:49 -07002432 int is_ud;
Roland Dreier67cdb402005-10-14 15:26:04 -07002433 ssize_t ret = -EINVAL;
Mike Marciniszyn1d784b82015-12-01 10:13:51 -05002434 size_t next_size;
Roland Dreier67cdb402005-10-14 15:26:04 -07002435
2436 if (copy_from_user(&cmd, buf, sizeof cmd))
2437 return -EFAULT;
2438
2439 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
2440 cmd.sge_count * sizeof (struct ib_uverbs_sge))
2441 return -EINVAL;
2442
2443 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
2444 return -EINVAL;
2445
2446 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2447 if (!user_wr)
2448 return -ENOMEM;
2449
Roland Dreier9ead1902006-06-17 20:44:49 -07002450 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2451 if (!qp)
Roland Dreier67cdb402005-10-14 15:26:04 -07002452 goto out;
2453
Roland Dreier9ead1902006-06-17 20:44:49 -07002454 is_ud = qp->qp_type == IB_QPT_UD;
Roland Dreier67cdb402005-10-14 15:26:04 -07002455 sg_ind = 0;
2456 last = NULL;
2457 for (i = 0; i < cmd.wr_count; ++i) {
2458 if (copy_from_user(user_wr,
2459 buf + sizeof cmd + i * cmd.wqe_size,
2460 cmd.wqe_size)) {
2461 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07002462 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07002463 }
2464
2465 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2466 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07002467 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07002468 }
2469
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002470 if (is_ud) {
2471 struct ib_ud_wr *ud;
2472
2473 if (user_wr->opcode != IB_WR_SEND &&
2474 user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2475 ret = -EINVAL;
2476 goto out_put;
2477 }
2478
Mike Marciniszyn1d784b82015-12-01 10:13:51 -05002479 next_size = sizeof(*ud);
2480 ud = alloc_wr(next_size, user_wr->num_sge);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002481 if (!ud) {
2482 ret = -ENOMEM;
2483 goto out_put;
2484 }
2485
2486 ud->ah = idr_read_ah(user_wr->wr.ud.ah, file->ucontext);
2487 if (!ud->ah) {
2488 kfree(ud);
2489 ret = -EINVAL;
2490 goto out_put;
2491 }
2492 ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2493 ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2494
2495 next = &ud->wr;
2496 } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2497 user_wr->opcode == IB_WR_RDMA_WRITE ||
2498 user_wr->opcode == IB_WR_RDMA_READ) {
2499 struct ib_rdma_wr *rdma;
2500
Mike Marciniszyn1d784b82015-12-01 10:13:51 -05002501 next_size = sizeof(*rdma);
2502 rdma = alloc_wr(next_size, user_wr->num_sge);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002503 if (!rdma) {
2504 ret = -ENOMEM;
2505 goto out_put;
2506 }
2507
2508 rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2509 rdma->rkey = user_wr->wr.rdma.rkey;
2510
2511 next = &rdma->wr;
2512 } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2513 user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2514 struct ib_atomic_wr *atomic;
2515
Mike Marciniszyn1d784b82015-12-01 10:13:51 -05002516 next_size = sizeof(*atomic);
2517 atomic = alloc_wr(next_size, user_wr->num_sge);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002518 if (!atomic) {
2519 ret = -ENOMEM;
2520 goto out_put;
2521 }
2522
2523 atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2524 atomic->compare_add = user_wr->wr.atomic.compare_add;
2525 atomic->swap = user_wr->wr.atomic.swap;
2526 atomic->rkey = user_wr->wr.atomic.rkey;
2527
2528 next = &atomic->wr;
2529 } else if (user_wr->opcode == IB_WR_SEND ||
2530 user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2531 user_wr->opcode == IB_WR_SEND_WITH_INV) {
Mike Marciniszyn1d784b82015-12-01 10:13:51 -05002532 next_size = sizeof(*next);
2533 next = alloc_wr(next_size, user_wr->num_sge);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002534 if (!next) {
2535 ret = -ENOMEM;
2536 goto out_put;
2537 }
2538 } else {
2539 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07002540 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07002541 }
2542
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002543 if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2544 user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2545 next->ex.imm_data =
2546 (__be32 __force) user_wr->ex.imm_data;
2547 } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2548 next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2549 }
2550
Roland Dreier67cdb402005-10-14 15:26:04 -07002551 if (!last)
2552 wr = next;
2553 else
2554 last->next = next;
2555 last = next;
2556
2557 next->next = NULL;
2558 next->wr_id = user_wr->wr_id;
2559 next->num_sge = user_wr->num_sge;
2560 next->opcode = user_wr->opcode;
2561 next->send_flags = user_wr->send_flags;
Roland Dreier67cdb402005-10-14 15:26:04 -07002562
Roland Dreier67cdb402005-10-14 15:26:04 -07002563 if (next->num_sge) {
2564 next->sg_list = (void *) next +
Mike Marciniszyn1d784b82015-12-01 10:13:51 -05002565 ALIGN(next_size, sizeof(struct ib_sge));
Roland Dreier67cdb402005-10-14 15:26:04 -07002566 if (copy_from_user(next->sg_list,
2567 buf + sizeof cmd +
2568 cmd.wr_count * cmd.wqe_size +
2569 sg_ind * sizeof (struct ib_sge),
2570 next->num_sge * sizeof (struct ib_sge))) {
2571 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07002572 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07002573 }
2574 sg_ind += next->num_sge;
2575 } else
2576 next->sg_list = NULL;
2577 }
2578
2579 resp.bad_wr = 0;
Sean Hefty0e0ec7e2011-08-08 15:31:51 -07002580 ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
Roland Dreier67cdb402005-10-14 15:26:04 -07002581 if (ret)
2582 for (next = wr; next; next = next->next) {
2583 ++resp.bad_wr;
2584 if (next == bad_wr)
2585 break;
2586 }
2587
2588 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2589 &resp, sizeof resp))
2590 ret = -EFAULT;
2591
Roland Dreier9ead1902006-06-17 20:44:49 -07002592out_put:
2593 put_qp_read(qp);
Roland Dreier67cdb402005-10-14 15:26:04 -07002594
2595 while (wr) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002596 if (is_ud && ud_wr(wr)->ah)
2597 put_ah_read(ud_wr(wr)->ah);
Roland Dreier67cdb402005-10-14 15:26:04 -07002598 next = wr->next;
2599 kfree(wr);
2600 wr = next;
2601 }
2602
Krishna Kumar18320822006-06-22 07:47:27 -07002603out:
Roland Dreier67cdb402005-10-14 15:26:04 -07002604 kfree(user_wr);
2605
2606 return ret ? ret : in_len;
2607}
2608
2609static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
2610 int in_len,
2611 u32 wr_count,
2612 u32 sge_count,
2613 u32 wqe_size)
2614{
2615 struct ib_uverbs_recv_wr *user_wr;
2616 struct ib_recv_wr *wr = NULL, *last, *next;
2617 int sg_ind;
2618 int i;
2619 int ret;
2620
2621 if (in_len < wqe_size * wr_count +
2622 sge_count * sizeof (struct ib_uverbs_sge))
2623 return ERR_PTR(-EINVAL);
2624
2625 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
2626 return ERR_PTR(-EINVAL);
2627
2628 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2629 if (!user_wr)
2630 return ERR_PTR(-ENOMEM);
2631
2632 sg_ind = 0;
2633 last = NULL;
2634 for (i = 0; i < wr_count; ++i) {
2635 if (copy_from_user(user_wr, buf + i * wqe_size,
2636 wqe_size)) {
2637 ret = -EFAULT;
2638 goto err;
2639 }
2640
2641 if (user_wr->num_sge + sg_ind > sge_count) {
2642 ret = -EINVAL;
2643 goto err;
2644 }
2645
2646 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
2647 user_wr->num_sge * sizeof (struct ib_sge),
2648 GFP_KERNEL);
2649 if (!next) {
2650 ret = -ENOMEM;
2651 goto err;
2652 }
2653
2654 if (!last)
2655 wr = next;
2656 else
2657 last->next = next;
2658 last = next;
2659
2660 next->next = NULL;
2661 next->wr_id = user_wr->wr_id;
2662 next->num_sge = user_wr->num_sge;
2663
2664 if (next->num_sge) {
2665 next->sg_list = (void *) next +
2666 ALIGN(sizeof *next, sizeof (struct ib_sge));
2667 if (copy_from_user(next->sg_list,
2668 buf + wr_count * wqe_size +
2669 sg_ind * sizeof (struct ib_sge),
2670 next->num_sge * sizeof (struct ib_sge))) {
2671 ret = -EFAULT;
2672 goto err;
2673 }
2674 sg_ind += next->num_sge;
2675 } else
2676 next->sg_list = NULL;
2677 }
2678
2679 kfree(user_wr);
2680 return wr;
2681
2682err:
2683 kfree(user_wr);
2684
2685 while (wr) {
2686 next = wr->next;
2687 kfree(wr);
2688 wr = next;
2689 }
2690
2691 return ERR_PTR(ret);
2692}
2693
2694ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002695 struct ib_device *ib_dev,
Roland Dreiera74cd4a2006-02-13 16:30:49 -08002696 const char __user *buf, int in_len,
2697 int out_len)
Roland Dreier67cdb402005-10-14 15:26:04 -07002698{
2699 struct ib_uverbs_post_recv cmd;
2700 struct ib_uverbs_post_recv_resp resp;
2701 struct ib_recv_wr *wr, *next, *bad_wr;
2702 struct ib_qp *qp;
2703 ssize_t ret = -EINVAL;
2704
2705 if (copy_from_user(&cmd, buf, sizeof cmd))
2706 return -EFAULT;
2707
2708 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2709 in_len - sizeof cmd, cmd.wr_count,
2710 cmd.sge_count, cmd.wqe_size);
2711 if (IS_ERR(wr))
2712 return PTR_ERR(wr);
2713
Roland Dreier9ead1902006-06-17 20:44:49 -07002714 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
2715 if (!qp)
Roland Dreier67cdb402005-10-14 15:26:04 -07002716 goto out;
2717
2718 resp.bad_wr = 0;
Sean Hefty0e0ec7e2011-08-08 15:31:51 -07002719 ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
Roland Dreier9ead1902006-06-17 20:44:49 -07002720
2721 put_qp_read(qp);
2722
Roland Dreier67cdb402005-10-14 15:26:04 -07002723 if (ret)
2724 for (next = wr; next; next = next->next) {
2725 ++resp.bad_wr;
2726 if (next == bad_wr)
2727 break;
2728 }
2729
Roland Dreier67cdb402005-10-14 15:26:04 -07002730 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2731 &resp, sizeof resp))
2732 ret = -EFAULT;
2733
2734out:
Roland Dreier67cdb402005-10-14 15:26:04 -07002735 while (wr) {
2736 next = wr->next;
2737 kfree(wr);
2738 wr = next;
2739 }
2740
2741 return ret ? ret : in_len;
2742}
2743
2744ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002745 struct ib_device *ib_dev,
Roland Dreiera74cd4a2006-02-13 16:30:49 -08002746 const char __user *buf, int in_len,
2747 int out_len)
Roland Dreier67cdb402005-10-14 15:26:04 -07002748{
2749 struct ib_uverbs_post_srq_recv cmd;
2750 struct ib_uverbs_post_srq_recv_resp resp;
2751 struct ib_recv_wr *wr, *next, *bad_wr;
2752 struct ib_srq *srq;
2753 ssize_t ret = -EINVAL;
2754
2755 if (copy_from_user(&cmd, buf, sizeof cmd))
2756 return -EFAULT;
2757
2758 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
2759 in_len - sizeof cmd, cmd.wr_count,
2760 cmd.sge_count, cmd.wqe_size);
2761 if (IS_ERR(wr))
2762 return PTR_ERR(wr);
2763
Roland Dreier9ead1902006-06-17 20:44:49 -07002764 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2765 if (!srq)
Roland Dreier67cdb402005-10-14 15:26:04 -07002766 goto out;
2767
2768 resp.bad_wr = 0;
2769 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
Roland Dreier9ead1902006-06-17 20:44:49 -07002770
2771 put_srq_read(srq);
2772
Roland Dreier67cdb402005-10-14 15:26:04 -07002773 if (ret)
2774 for (next = wr; next; next = next->next) {
2775 ++resp.bad_wr;
2776 if (next == bad_wr)
2777 break;
2778 }
2779
Roland Dreier67cdb402005-10-14 15:26:04 -07002780 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2781 &resp, sizeof resp))
2782 ret = -EFAULT;
2783
2784out:
Roland Dreier67cdb402005-10-14 15:26:04 -07002785 while (wr) {
2786 next = wr->next;
2787 kfree(wr);
2788 wr = next;
2789 }
2790
2791 return ret ? ret : in_len;
2792}
2793
2794ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002795 struct ib_device *ib_dev,
Roland Dreier67cdb402005-10-14 15:26:04 -07002796 const char __user *buf, int in_len,
2797 int out_len)
2798{
2799 struct ib_uverbs_create_ah cmd;
2800 struct ib_uverbs_create_ah_resp resp;
2801 struct ib_uobject *uobj;
2802 struct ib_pd *pd;
2803 struct ib_ah *ah;
2804 struct ib_ah_attr attr;
2805 int ret;
2806
2807 if (out_len < sizeof resp)
2808 return -ENOSPC;
2809
2810 if (copy_from_user(&cmd, buf, sizeof cmd))
2811 return -EFAULT;
2812
2813 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
2814 if (!uobj)
2815 return -ENOMEM;
2816
Roland Dreier3bea57a2012-04-30 10:27:26 -07002817 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
Roland Dreier9ead1902006-06-17 20:44:49 -07002818 down_write(&uobj->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07002819
Roland Dreier9ead1902006-06-17 20:44:49 -07002820 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
2821 if (!pd) {
Roland Dreier67cdb402005-10-14 15:26:04 -07002822 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07002823 goto err;
Roland Dreier67cdb402005-10-14 15:26:04 -07002824 }
2825
Roland Dreier67cdb402005-10-14 15:26:04 -07002826 attr.dlid = cmd.attr.dlid;
2827 attr.sl = cmd.attr.sl;
2828 attr.src_path_bits = cmd.attr.src_path_bits;
2829 attr.static_rate = cmd.attr.static_rate;
Ralph Campbellea5d4a62006-01-06 16:24:45 -08002830 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
Roland Dreier67cdb402005-10-14 15:26:04 -07002831 attr.port_num = cmd.attr.port_num;
2832 attr.grh.flow_label = cmd.attr.grh.flow_label;
2833 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
2834 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
2835 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
Devesh Sharma8b0f93d2014-09-26 20:45:32 +05302836 memset(&attr.dmac, 0, sizeof(attr.dmac));
Roland Dreier67cdb402005-10-14 15:26:04 -07002837 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
2838
2839 ah = ib_create_ah(pd, &attr);
2840 if (IS_ERR(ah)) {
2841 ret = PTR_ERR(ah);
Michael S. Tsirkinec924b42006-07-17 18:20:51 +03002842 goto err_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07002843 }
2844
Roland Dreier9ead1902006-06-17 20:44:49 -07002845 ah->uobject = uobj;
2846 uobj->object = ah;
Roland Dreier67cdb402005-10-14 15:26:04 -07002847
Roland Dreier9ead1902006-06-17 20:44:49 -07002848 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07002849 if (ret)
2850 goto err_destroy;
2851
2852 resp.ah_handle = uobj->id;
2853
2854 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2855 &resp, sizeof resp)) {
2856 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07002857 goto err_copy;
Roland Dreier67cdb402005-10-14 15:26:04 -07002858 }
2859
Roland Dreier9ead1902006-06-17 20:44:49 -07002860 put_pd_read(pd);
2861
Ingo Molnar95ed6442006-01-13 14:51:39 -08002862 mutex_lock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07002863 list_add_tail(&uobj->list, &file->ucontext->ah_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08002864 mutex_unlock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07002865
Roland Dreier9ead1902006-06-17 20:44:49 -07002866 uobj->live = 1;
2867
2868 up_write(&uobj->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07002869
2870 return in_len;
2871
Roland Dreier9ead1902006-06-17 20:44:49 -07002872err_copy:
2873 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07002874
2875err_destroy:
2876 ib_destroy_ah(ah);
2877
Michael S. Tsirkinec924b42006-07-17 18:20:51 +03002878err_put:
2879 put_pd_read(pd);
2880
Roland Dreier9ead1902006-06-17 20:44:49 -07002881err:
2882 put_uobj_write(uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07002883 return ret;
2884}
2885
2886ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002887 struct ib_device *ib_dev,
Roland Dreier67cdb402005-10-14 15:26:04 -07002888 const char __user *buf, int in_len, int out_len)
2889{
2890 struct ib_uverbs_destroy_ah cmd;
2891 struct ib_ah *ah;
2892 struct ib_uobject *uobj;
Roland Dreier9ead1902006-06-17 20:44:49 -07002893 int ret;
Roland Dreier67cdb402005-10-14 15:26:04 -07002894
2895 if (copy_from_user(&cmd, buf, sizeof cmd))
2896 return -EFAULT;
2897
Roland Dreier9ead1902006-06-17 20:44:49 -07002898 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
2899 if (!uobj)
2900 return -EINVAL;
2901 ah = uobj->object;
Roland Dreier67cdb402005-10-14 15:26:04 -07002902
2903 ret = ib_destroy_ah(ah);
Roland Dreier9ead1902006-06-17 20:44:49 -07002904 if (!ret)
2905 uobj->live = 0;
Roland Dreier67cdb402005-10-14 15:26:04 -07002906
Roland Dreier9ead1902006-06-17 20:44:49 -07002907 put_uobj_write(uobj);
2908
2909 if (ret)
2910 return ret;
2911
2912 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07002913
Ingo Molnar95ed6442006-01-13 14:51:39 -08002914 mutex_lock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07002915 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08002916 mutex_unlock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07002917
Roland Dreier9ead1902006-06-17 20:44:49 -07002918 put_uobj(uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07002919
Roland Dreier9ead1902006-06-17 20:44:49 -07002920 return in_len;
Roland Dreier67cdb402005-10-14 15:26:04 -07002921}
2922
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002923ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002924 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002925 const char __user *buf, int in_len,
2926 int out_len)
2927{
2928 struct ib_uverbs_attach_mcast cmd;
2929 struct ib_qp *qp;
Roland Dreier9ead1902006-06-17 20:44:49 -07002930 struct ib_uqp_object *obj;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002931 struct ib_uverbs_mcast_entry *mcast;
Roland Dreier9ead1902006-06-17 20:44:49 -07002932 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002933
2934 if (copy_from_user(&cmd, buf, sizeof cmd))
2935 return -EFAULT;
2936
Eli Cohene214a0f2012-01-03 20:36:48 -08002937 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
Roland Dreier9ead1902006-06-17 20:44:49 -07002938 if (!qp)
2939 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002940
Roland Dreier9ead1902006-06-17 20:44:49 -07002941 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002942
Roland Dreier9ead1902006-06-17 20:44:49 -07002943 list_for_each_entry(mcast, &obj->mcast_list, list)
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002944 if (cmd.mlid == mcast->lid &&
2945 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2946 ret = 0;
Roland Dreier9ead1902006-06-17 20:44:49 -07002947 goto out_put;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002948 }
2949
2950 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2951 if (!mcast) {
2952 ret = -ENOMEM;
Roland Dreier9ead1902006-06-17 20:44:49 -07002953 goto out_put;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002954 }
2955
2956 mcast->lid = cmd.mlid;
2957 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2958
2959 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
Roland Dreier9ead1902006-06-17 20:44:49 -07002960 if (!ret)
2961 list_add_tail(&mcast->list, &obj->mcast_list);
2962 else
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002963 kfree(mcast);
2964
Roland Dreier9ead1902006-06-17 20:44:49 -07002965out_put:
Eli Cohene214a0f2012-01-03 20:36:48 -08002966 put_qp_write(qp);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002967
2968 return ret ? ret : in_len;
2969}
2970
2971ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03002972 struct ib_device *ib_dev,
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002973 const char __user *buf, int in_len,
2974 int out_len)
2975{
2976 struct ib_uverbs_detach_mcast cmd;
Roland Dreier9ead1902006-06-17 20:44:49 -07002977 struct ib_uqp_object *obj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002978 struct ib_qp *qp;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002979 struct ib_uverbs_mcast_entry *mcast;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002980 int ret = -EINVAL;
2981
2982 if (copy_from_user(&cmd, buf, sizeof cmd))
2983 return -EFAULT;
2984
Eli Cohene214a0f2012-01-03 20:36:48 -08002985 qp = idr_write_qp(cmd.qp_handle, file->ucontext);
Roland Dreier9ead1902006-06-17 20:44:49 -07002986 if (!qp)
2987 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07002988
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002989 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
2990 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -07002991 goto out_put;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002992
Roland Dreier9ead1902006-06-17 20:44:49 -07002993 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002994
Roland Dreier9ead1902006-06-17 20:44:49 -07002995 list_for_each_entry(mcast, &obj->mcast_list, list)
Jack Morgensteinf4e40152005-11-29 16:57:01 -08002996 if (cmd.mlid == mcast->lid &&
2997 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2998 list_del(&mcast->list);
2999 kfree(mcast);
3000 break;
3001 }
3002
Roland Dreier9ead1902006-06-17 20:44:49 -07003003out_put:
Eli Cohene214a0f2012-01-03 20:36:48 -08003004 put_qp_write(qp);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07003005
3006 return ret ? ret : in_len;
3007}
Roland Dreierf520ba52005-08-18 12:24:13 -07003008
Yann Droneaudb68c9562013-11-06 23:21:46 +01003009static int kern_spec_to_ib_spec(struct ib_uverbs_flow_spec *kern_spec,
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003010 union ib_flow_spec *ib_spec)
3011{
Yann Droneaudc780d822013-12-11 23:01:49 +01003012 if (kern_spec->reserved)
3013 return -EINVAL;
3014
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003015 ib_spec->type = kern_spec->type;
3016
3017 switch (ib_spec->type) {
3018 case IB_FLOW_SPEC_ETH:
3019 ib_spec->eth.size = sizeof(struct ib_flow_spec_eth);
3020 if (ib_spec->eth.size != kern_spec->eth.size)
3021 return -EINVAL;
3022 memcpy(&ib_spec->eth.val, &kern_spec->eth.val,
3023 sizeof(struct ib_flow_eth_filter));
3024 memcpy(&ib_spec->eth.mask, &kern_spec->eth.mask,
3025 sizeof(struct ib_flow_eth_filter));
3026 break;
3027 case IB_FLOW_SPEC_IPV4:
3028 ib_spec->ipv4.size = sizeof(struct ib_flow_spec_ipv4);
3029 if (ib_spec->ipv4.size != kern_spec->ipv4.size)
3030 return -EINVAL;
3031 memcpy(&ib_spec->ipv4.val, &kern_spec->ipv4.val,
3032 sizeof(struct ib_flow_ipv4_filter));
3033 memcpy(&ib_spec->ipv4.mask, &kern_spec->ipv4.mask,
3034 sizeof(struct ib_flow_ipv4_filter));
3035 break;
3036 case IB_FLOW_SPEC_TCP:
3037 case IB_FLOW_SPEC_UDP:
3038 ib_spec->tcp_udp.size = sizeof(struct ib_flow_spec_tcp_udp);
3039 if (ib_spec->tcp_udp.size != kern_spec->tcp_udp.size)
3040 return -EINVAL;
3041 memcpy(&ib_spec->tcp_udp.val, &kern_spec->tcp_udp.val,
3042 sizeof(struct ib_flow_tcp_udp_filter));
3043 memcpy(&ib_spec->tcp_udp.mask, &kern_spec->tcp_udp.mask,
3044 sizeof(struct ib_flow_tcp_udp_filter));
3045 break;
3046 default:
3047 return -EINVAL;
3048 }
3049 return 0;
3050}
3051
Yann Droneaudf21519b2013-11-06 23:21:49 +01003052int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003053 struct ib_device *ib_dev,
Yann Droneaudf21519b2013-11-06 23:21:49 +01003054 struct ib_udata *ucore,
3055 struct ib_udata *uhw)
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003056{
3057 struct ib_uverbs_create_flow cmd;
3058 struct ib_uverbs_create_flow_resp resp;
3059 struct ib_uobject *uobj;
3060 struct ib_flow *flow_id;
Yann Droneaudd82693d2013-11-06 23:21:45 +01003061 struct ib_uverbs_flow_attr *kern_flow_attr;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003062 struct ib_flow_attr *flow_attr;
3063 struct ib_qp *qp;
3064 int err = 0;
3065 void *kern_spec;
3066 void *ib_spec;
3067 int i;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003068
Yann Droneaud6bcca3d2013-12-11 23:01:52 +01003069 if (ucore->inlen < sizeof(cmd))
3070 return -EINVAL;
3071
Yann Droneaudf21519b2013-11-06 23:21:49 +01003072 if (ucore->outlen < sizeof(resp))
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003073 return -ENOSPC;
3074
Yann Droneaudf21519b2013-11-06 23:21:49 +01003075 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3076 if (err)
3077 return err;
3078
3079 ucore->inbuf += sizeof(cmd);
3080 ucore->inlen -= sizeof(cmd);
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003081
Matan Barak22878db2013-09-01 18:39:52 +03003082 if (cmd.comp_mask)
3083 return -EINVAL;
3084
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003085 if ((cmd.flow_attr.type == IB_FLOW_ATTR_SNIFFER &&
3086 !capable(CAP_NET_ADMIN)) || !capable(CAP_NET_RAW))
3087 return -EPERM;
3088
Matan Barakf8848272013-11-06 23:21:44 +01003089 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
Matan Barak22878db2013-09-01 18:39:52 +03003090 return -EINVAL;
3091
Yann Droneaudf21519b2013-11-06 23:21:49 +01003092 if (cmd.flow_attr.size > ucore->inlen ||
Matan Barakf8848272013-11-06 23:21:44 +01003093 cmd.flow_attr.size >
Yann Droneaudb68c9562013-11-06 23:21:46 +01003094 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
Matan Barak22878db2013-09-01 18:39:52 +03003095 return -EINVAL;
3096
Yann Droneaudc780d822013-12-11 23:01:49 +01003097 if (cmd.flow_attr.reserved[0] ||
3098 cmd.flow_attr.reserved[1])
3099 return -EINVAL;
3100
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003101 if (cmd.flow_attr.num_of_specs) {
Matan Barakf8848272013-11-06 23:21:44 +01003102 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3103 GFP_KERNEL);
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003104 if (!kern_flow_attr)
3105 return -ENOMEM;
3106
3107 memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr));
Yann Droneaudf21519b2013-11-06 23:21:49 +01003108 err = ib_copy_from_udata(kern_flow_attr + 1, ucore,
3109 cmd.flow_attr.size);
3110 if (err)
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003111 goto err_free_attr;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003112 } else {
3113 kern_flow_attr = &cmd.flow_attr;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003114 }
3115
3116 uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
3117 if (!uobj) {
3118 err = -ENOMEM;
3119 goto err_free_attr;
3120 }
3121 init_uobj(uobj, 0, file->ucontext, &rule_lock_class);
3122 down_write(&uobj->mutex);
3123
3124 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
3125 if (!qp) {
3126 err = -EINVAL;
3127 goto err_uobj;
3128 }
3129
Matan Barakf8848272013-11-06 23:21:44 +01003130 flow_attr = kmalloc(sizeof(*flow_attr) + cmd.flow_attr.size, GFP_KERNEL);
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003131 if (!flow_attr) {
3132 err = -ENOMEM;
3133 goto err_put;
3134 }
3135
3136 flow_attr->type = kern_flow_attr->type;
3137 flow_attr->priority = kern_flow_attr->priority;
3138 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3139 flow_attr->port = kern_flow_attr->port;
3140 flow_attr->flags = kern_flow_attr->flags;
3141 flow_attr->size = sizeof(*flow_attr);
3142
3143 kern_spec = kern_flow_attr + 1;
3144 ib_spec = flow_attr + 1;
Matan Barakf8848272013-11-06 23:21:44 +01003145 for (i = 0; i < flow_attr->num_of_specs &&
Yann Droneaudb68c9562013-11-06 23:21:46 +01003146 cmd.flow_attr.size > offsetof(struct ib_uverbs_flow_spec, reserved) &&
Matan Barakf8848272013-11-06 23:21:44 +01003147 cmd.flow_attr.size >=
Yann Droneaudb68c9562013-11-06 23:21:46 +01003148 ((struct ib_uverbs_flow_spec *)kern_spec)->size; i++) {
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003149 err = kern_spec_to_ib_spec(kern_spec, ib_spec);
3150 if (err)
3151 goto err_free;
3152 flow_attr->size +=
3153 ((union ib_flow_spec *) ib_spec)->size;
Yann Droneaudb68c9562013-11-06 23:21:46 +01003154 cmd.flow_attr.size -= ((struct ib_uverbs_flow_spec *)kern_spec)->size;
3155 kern_spec += ((struct ib_uverbs_flow_spec *) kern_spec)->size;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003156 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3157 }
Matan Barakf8848272013-11-06 23:21:44 +01003158 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3159 pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
3160 i, cmd.flow_attr.size);
Yann Droneaud98a37512013-12-11 23:01:50 +01003161 err = -EINVAL;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003162 goto err_free;
3163 }
3164 flow_id = ib_create_flow(qp, flow_attr, IB_FLOW_DOMAIN_USER);
3165 if (IS_ERR(flow_id)) {
3166 err = PTR_ERR(flow_id);
3167 goto err_free;
3168 }
3169 flow_id->qp = qp;
3170 flow_id->uobject = uobj;
3171 uobj->object = flow_id;
3172
3173 err = idr_add_uobj(&ib_uverbs_rule_idr, uobj);
3174 if (err)
3175 goto destroy_flow;
3176
3177 memset(&resp, 0, sizeof(resp));
3178 resp.flow_handle = uobj->id;
3179
Yann Droneaudf21519b2013-11-06 23:21:49 +01003180 err = ib_copy_to_udata(ucore,
3181 &resp, sizeof(resp));
3182 if (err)
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003183 goto err_copy;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003184
3185 put_qp_read(qp);
3186 mutex_lock(&file->mutex);
3187 list_add_tail(&uobj->list, &file->ucontext->rule_list);
3188 mutex_unlock(&file->mutex);
3189
3190 uobj->live = 1;
3191
3192 up_write(&uobj->mutex);
3193 kfree(flow_attr);
3194 if (cmd.flow_attr.num_of_specs)
3195 kfree(kern_flow_attr);
Yann Droneaudf21519b2013-11-06 23:21:49 +01003196 return 0;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003197err_copy:
3198 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
3199destroy_flow:
3200 ib_destroy_flow(flow_id);
3201err_free:
3202 kfree(flow_attr);
3203err_put:
3204 put_qp_read(qp);
3205err_uobj:
3206 put_uobj_write(uobj);
3207err_free_attr:
3208 if (cmd.flow_attr.num_of_specs)
3209 kfree(kern_flow_attr);
3210 return err;
3211}
3212
Yann Droneaudf21519b2013-11-06 23:21:49 +01003213int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003214 struct ib_device *ib_dev,
Yann Droneaudf21519b2013-11-06 23:21:49 +01003215 struct ib_udata *ucore,
3216 struct ib_udata *uhw)
3217{
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003218 struct ib_uverbs_destroy_flow cmd;
3219 struct ib_flow *flow_id;
3220 struct ib_uobject *uobj;
3221 int ret;
3222
Yann Droneaud6bcca3d2013-12-11 23:01:52 +01003223 if (ucore->inlen < sizeof(cmd))
3224 return -EINVAL;
3225
Yann Droneaudf21519b2013-11-06 23:21:49 +01003226 ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3227 if (ret)
3228 return ret;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003229
Yann Droneaud2782c2d2013-12-11 23:01:48 +01003230 if (cmd.comp_mask)
3231 return -EINVAL;
3232
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003233 uobj = idr_write_uobj(&ib_uverbs_rule_idr, cmd.flow_handle,
3234 file->ucontext);
3235 if (!uobj)
3236 return -EINVAL;
3237 flow_id = uobj->object;
3238
3239 ret = ib_destroy_flow(flow_id);
3240 if (!ret)
3241 uobj->live = 0;
3242
3243 put_uobj_write(uobj);
3244
3245 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
3246
3247 mutex_lock(&file->mutex);
3248 list_del(&uobj->list);
3249 mutex_unlock(&file->mutex);
3250
3251 put_uobj(uobj);
3252
Yann Droneaudf21519b2013-11-06 23:21:49 +01003253 return ret;
Hadar Hen Zion436f2ad2013-08-14 13:58:30 +03003254}
3255
Sean Heftyc89d1be2011-12-06 21:13:10 +00003256static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003257 struct ib_device *ib_dev,
Sean Heftyc89d1be2011-12-06 21:13:10 +00003258 struct ib_uverbs_create_xsrq *cmd,
3259 struct ib_udata *udata)
Sean Hefty8541f8d2011-05-25 17:08:38 -07003260{
3261 struct ib_uverbs_create_srq_resp resp;
3262 struct ib_usrq_object *obj;
3263 struct ib_pd *pd;
3264 struct ib_srq *srq;
3265 struct ib_uobject *uninitialized_var(xrcd_uobj);
3266 struct ib_srq_init_attr attr;
3267 int ret;
3268
3269 obj = kmalloc(sizeof *obj, GFP_KERNEL);
3270 if (!obj)
3271 return -ENOMEM;
3272
Roland Dreier3bea57a2012-04-30 10:27:26 -07003273 init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
Sean Hefty8541f8d2011-05-25 17:08:38 -07003274 down_write(&obj->uevent.uobject.mutex);
3275
Sean Hefty8541f8d2011-05-25 17:08:38 -07003276 if (cmd->srq_type == IB_SRQT_XRC) {
Sean Hefty8541f8d2011-05-25 17:08:38 -07003277 attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
3278 if (!attr.ext.xrc.xrcd) {
3279 ret = -EINVAL;
Roland Dreier5909ce52012-04-30 12:51:50 -07003280 goto err;
Sean Hefty8541f8d2011-05-25 17:08:38 -07003281 }
3282
3283 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3284 atomic_inc(&obj->uxrcd->refcnt);
Roland Dreier5909ce52012-04-30 12:51:50 -07003285
3286 attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
3287 if (!attr.ext.xrc.cq) {
3288 ret = -EINVAL;
3289 goto err_put_xrcd;
3290 }
3291 }
3292
3293 pd = idr_read_pd(cmd->pd_handle, file->ucontext);
3294 if (!pd) {
3295 ret = -EINVAL;
3296 goto err_put_cq;
Sean Hefty8541f8d2011-05-25 17:08:38 -07003297 }
3298
3299 attr.event_handler = ib_uverbs_srq_event_handler;
3300 attr.srq_context = file;
3301 attr.srq_type = cmd->srq_type;
3302 attr.attr.max_wr = cmd->max_wr;
3303 attr.attr.max_sge = cmd->max_sge;
3304 attr.attr.srq_limit = cmd->srq_limit;
3305
3306 obj->uevent.events_reported = 0;
3307 INIT_LIST_HEAD(&obj->uevent.event_list);
3308
3309 srq = pd->device->create_srq(pd, &attr, udata);
3310 if (IS_ERR(srq)) {
3311 ret = PTR_ERR(srq);
3312 goto err_put;
3313 }
3314
3315 srq->device = pd->device;
3316 srq->pd = pd;
3317 srq->srq_type = cmd->srq_type;
3318 srq->uobject = &obj->uevent.uobject;
3319 srq->event_handler = attr.event_handler;
3320 srq->srq_context = attr.srq_context;
3321
3322 if (cmd->srq_type == IB_SRQT_XRC) {
3323 srq->ext.xrc.cq = attr.ext.xrc.cq;
3324 srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
3325 atomic_inc(&attr.ext.xrc.cq->usecnt);
3326 atomic_inc(&attr.ext.xrc.xrcd->usecnt);
3327 }
3328
3329 atomic_inc(&pd->usecnt);
3330 atomic_set(&srq->usecnt, 0);
3331
3332 obj->uevent.uobject.object = srq;
3333 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
3334 if (ret)
3335 goto err_destroy;
3336
3337 memset(&resp, 0, sizeof resp);
3338 resp.srq_handle = obj->uevent.uobject.id;
3339 resp.max_wr = attr.attr.max_wr;
3340 resp.max_sge = attr.attr.max_sge;
3341 if (cmd->srq_type == IB_SRQT_XRC)
3342 resp.srqn = srq->ext.xrc.srq_num;
3343
3344 if (copy_to_user((void __user *) (unsigned long) cmd->response,
3345 &resp, sizeof resp)) {
3346 ret = -EFAULT;
3347 goto err_copy;
3348 }
3349
3350 if (cmd->srq_type == IB_SRQT_XRC) {
3351 put_uobj_read(xrcd_uobj);
3352 put_cq_read(attr.ext.xrc.cq);
3353 }
3354 put_pd_read(pd);
3355
3356 mutex_lock(&file->mutex);
3357 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
3358 mutex_unlock(&file->mutex);
3359
3360 obj->uevent.uobject.live = 1;
3361
3362 up_write(&obj->uevent.uobject.mutex);
3363
3364 return 0;
3365
3366err_copy:
3367 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
3368
3369err_destroy:
3370 ib_destroy_srq(srq);
3371
3372err_put:
Roland Dreier5909ce52012-04-30 12:51:50 -07003373 put_pd_read(pd);
Sean Hefty8541f8d2011-05-25 17:08:38 -07003374
3375err_put_cq:
3376 if (cmd->srq_type == IB_SRQT_XRC)
3377 put_cq_read(attr.ext.xrc.cq);
3378
Roland Dreier5909ce52012-04-30 12:51:50 -07003379err_put_xrcd:
3380 if (cmd->srq_type == IB_SRQT_XRC) {
3381 atomic_dec(&obj->uxrcd->refcnt);
3382 put_uobj_read(xrcd_uobj);
3383 }
Sean Hefty8541f8d2011-05-25 17:08:38 -07003384
3385err:
3386 put_uobj_write(&obj->uevent.uobject);
3387 return ret;
3388}
3389
Roland Dreierf520ba52005-08-18 12:24:13 -07003390ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003391 struct ib_device *ib_dev,
Roland Dreierf520ba52005-08-18 12:24:13 -07003392 const char __user *buf, int in_len,
3393 int out_len)
3394{
3395 struct ib_uverbs_create_srq cmd;
Sean Hefty8541f8d2011-05-25 17:08:38 -07003396 struct ib_uverbs_create_xsrq xcmd;
Roland Dreierf520ba52005-08-18 12:24:13 -07003397 struct ib_uverbs_create_srq_resp resp;
3398 struct ib_udata udata;
Sean Hefty8541f8d2011-05-25 17:08:38 -07003399 int ret;
3400
3401 if (out_len < sizeof resp)
3402 return -ENOSPC;
3403
3404 if (copy_from_user(&cmd, buf, sizeof cmd))
3405 return -EFAULT;
3406
3407 xcmd.response = cmd.response;
3408 xcmd.user_handle = cmd.user_handle;
3409 xcmd.srq_type = IB_SRQT_BASIC;
3410 xcmd.pd_handle = cmd.pd_handle;
3411 xcmd.max_wr = cmd.max_wr;
3412 xcmd.max_sge = cmd.max_sge;
3413 xcmd.srq_limit = cmd.srq_limit;
3414
3415 INIT_UDATA(&udata, buf + sizeof cmd,
3416 (unsigned long) cmd.response + sizeof resp,
Majd Dibbiny3d943c92016-02-14 18:35:52 +02003417 in_len - sizeof cmd - sizeof(struct ib_uverbs_cmd_hdr),
3418 out_len - sizeof resp);
Sean Hefty8541f8d2011-05-25 17:08:38 -07003419
Yishai Hadas057aec02015-08-13 18:32:04 +03003420 ret = __uverbs_create_xsrq(file, ib_dev, &xcmd, &udata);
Sean Hefty8541f8d2011-05-25 17:08:38 -07003421 if (ret)
3422 return ret;
3423
3424 return in_len;
3425}
3426
3427ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003428 struct ib_device *ib_dev,
Sean Hefty8541f8d2011-05-25 17:08:38 -07003429 const char __user *buf, int in_len, int out_len)
3430{
3431 struct ib_uverbs_create_xsrq cmd;
3432 struct ib_uverbs_create_srq_resp resp;
3433 struct ib_udata udata;
Roland Dreierf520ba52005-08-18 12:24:13 -07003434 int ret;
3435
3436 if (out_len < sizeof resp)
3437 return -ENOSPC;
3438
3439 if (copy_from_user(&cmd, buf, sizeof cmd))
3440 return -EFAULT;
3441
3442 INIT_UDATA(&udata, buf + sizeof cmd,
3443 (unsigned long) cmd.response + sizeof resp,
Majd Dibbiny3d943c92016-02-14 18:35:52 +02003444 in_len - sizeof cmd - sizeof(struct ib_uverbs_cmd_hdr),
3445 out_len - sizeof resp);
Roland Dreierf520ba52005-08-18 12:24:13 -07003446
Yishai Hadas057aec02015-08-13 18:32:04 +03003447 ret = __uverbs_create_xsrq(file, ib_dev, &cmd, &udata);
Roland Dreierf520ba52005-08-18 12:24:13 -07003448 if (ret)
Sean Hefty8541f8d2011-05-25 17:08:38 -07003449 return ret;
Roland Dreierf520ba52005-08-18 12:24:13 -07003450
3451 return in_len;
Roland Dreierf520ba52005-08-18 12:24:13 -07003452}
3453
3454ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003455 struct ib_device *ib_dev,
Roland Dreierf520ba52005-08-18 12:24:13 -07003456 const char __user *buf, int in_len,
3457 int out_len)
3458{
3459 struct ib_uverbs_modify_srq cmd;
Ralph Campbell9bc57e22006-08-11 14:58:09 -07003460 struct ib_udata udata;
Roland Dreierf520ba52005-08-18 12:24:13 -07003461 struct ib_srq *srq;
3462 struct ib_srq_attr attr;
3463 int ret;
3464
3465 if (copy_from_user(&cmd, buf, sizeof cmd))
3466 return -EFAULT;
3467
Ralph Campbell9bc57e22006-08-11 14:58:09 -07003468 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
3469 out_len);
3470
Roland Dreier9ead1902006-06-17 20:44:49 -07003471 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3472 if (!srq)
3473 return -EINVAL;
Roland Dreierf520ba52005-08-18 12:24:13 -07003474
3475 attr.max_wr = cmd.max_wr;
Roland Dreierf520ba52005-08-18 12:24:13 -07003476 attr.srq_limit = cmd.srq_limit;
3477
Ralph Campbell9bc57e22006-08-11 14:58:09 -07003478 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
Roland Dreierf520ba52005-08-18 12:24:13 -07003479
Roland Dreier9ead1902006-06-17 20:44:49 -07003480 put_srq_read(srq);
Roland Dreierf520ba52005-08-18 12:24:13 -07003481
3482 return ret ? ret : in_len;
3483}
3484
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003485ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003486 struct ib_device *ib_dev,
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003487 const char __user *buf,
3488 int in_len, int out_len)
3489{
3490 struct ib_uverbs_query_srq cmd;
3491 struct ib_uverbs_query_srq_resp resp;
3492 struct ib_srq_attr attr;
3493 struct ib_srq *srq;
3494 int ret;
3495
3496 if (out_len < sizeof resp)
3497 return -ENOSPC;
3498
3499 if (copy_from_user(&cmd, buf, sizeof cmd))
3500 return -EFAULT;
3501
Roland Dreier9ead1902006-06-17 20:44:49 -07003502 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
3503 if (!srq)
3504 return -EINVAL;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003505
Roland Dreier9ead1902006-06-17 20:44:49 -07003506 ret = ib_query_srq(srq, &attr);
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003507
Roland Dreier9ead1902006-06-17 20:44:49 -07003508 put_srq_read(srq);
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003509
3510 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -07003511 return ret;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003512
3513 memset(&resp, 0, sizeof resp);
3514
3515 resp.max_wr = attr.max_wr;
3516 resp.max_sge = attr.max_sge;
3517 resp.srq_limit = attr.srq_limit;
3518
3519 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3520 &resp, sizeof resp))
Roland Dreier9ead1902006-06-17 20:44:49 -07003521 return -EFAULT;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003522
Roland Dreier9ead1902006-06-17 20:44:49 -07003523 return in_len;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08003524}
3525
Roland Dreierf520ba52005-08-18 12:24:13 -07003526ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003527 struct ib_device *ib_dev,
Roland Dreierf520ba52005-08-18 12:24:13 -07003528 const char __user *buf, int in_len,
3529 int out_len)
3530{
Roland Dreier63aaf642005-09-09 15:55:08 -07003531 struct ib_uverbs_destroy_srq cmd;
3532 struct ib_uverbs_destroy_srq_resp resp;
Roland Dreier9ead1902006-06-17 20:44:49 -07003533 struct ib_uobject *uobj;
Roland Dreier63aaf642005-09-09 15:55:08 -07003534 struct ib_srq *srq;
Roland Dreier9ead1902006-06-17 20:44:49 -07003535 struct ib_uevent_object *obj;
Roland Dreier63aaf642005-09-09 15:55:08 -07003536 int ret = -EINVAL;
Yishai Hadas846be902013-08-01 18:49:54 +03003537 struct ib_usrq_object *us;
3538 enum ib_srq_type srq_type;
Roland Dreierf520ba52005-08-18 12:24:13 -07003539
3540 if (copy_from_user(&cmd, buf, sizeof cmd))
3541 return -EFAULT;
3542
Roland Dreier9ead1902006-06-17 20:44:49 -07003543 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
3544 if (!uobj)
3545 return -EINVAL;
3546 srq = uobj->object;
3547 obj = container_of(uobj, struct ib_uevent_object, uobject);
Yishai Hadas846be902013-08-01 18:49:54 +03003548 srq_type = srq->srq_type;
Roland Dreierf520ba52005-08-18 12:24:13 -07003549
3550 ret = ib_destroy_srq(srq);
Roland Dreier9ead1902006-06-17 20:44:49 -07003551 if (!ret)
3552 uobj->live = 0;
Roland Dreierf520ba52005-08-18 12:24:13 -07003553
Roland Dreier9ead1902006-06-17 20:44:49 -07003554 put_uobj_write(uobj);
3555
3556 if (ret)
3557 return ret;
3558
Yishai Hadas846be902013-08-01 18:49:54 +03003559 if (srq_type == IB_SRQT_XRC) {
3560 us = container_of(obj, struct ib_usrq_object, uevent);
3561 atomic_dec(&us->uxrcd->refcnt);
3562 }
3563
Roland Dreier9ead1902006-06-17 20:44:49 -07003564 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
Roland Dreierf520ba52005-08-18 12:24:13 -07003565
Ingo Molnar95ed6442006-01-13 14:51:39 -08003566 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07003567 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08003568 mutex_unlock(&file->mutex);
Roland Dreierf520ba52005-08-18 12:24:13 -07003569
Roland Dreier9ead1902006-06-17 20:44:49 -07003570 ib_uverbs_release_uevent(file, obj);
Roland Dreier63aaf642005-09-09 15:55:08 -07003571
Roland Dreier9ead1902006-06-17 20:44:49 -07003572 memset(&resp, 0, sizeof resp);
3573 resp.events_reported = obj->events_reported;
Roland Dreier63aaf642005-09-09 15:55:08 -07003574
Roland Dreier9ead1902006-06-17 20:44:49 -07003575 put_uobj(uobj);
Roland Dreierf520ba52005-08-18 12:24:13 -07003576
Roland Dreier63aaf642005-09-09 15:55:08 -07003577 if (copy_to_user((void __user *) (unsigned long) cmd.response,
3578 &resp, sizeof resp))
3579 ret = -EFAULT;
3580
Roland Dreierf520ba52005-08-18 12:24:13 -07003581 return ret ? ret : in_len;
3582}
Eli Cohen02d1aa72015-02-08 13:28:50 +02003583
3584int ib_uverbs_ex_query_device(struct ib_uverbs_file *file,
Yishai Hadas057aec02015-08-13 18:32:04 +03003585 struct ib_device *ib_dev,
Eli Cohen02d1aa72015-02-08 13:28:50 +02003586 struct ib_udata *ucore,
3587 struct ib_udata *uhw)
3588{
3589 struct ib_uverbs_ex_query_device_resp resp;
3590 struct ib_uverbs_ex_query_device cmd;
3591 struct ib_device_attr attr;
Eli Cohen02d1aa72015-02-08 13:28:50 +02003592 int err;
3593
Eli Cohen02d1aa72015-02-08 13:28:50 +02003594 if (ucore->inlen < sizeof(cmd))
3595 return -EINVAL;
3596
3597 err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
3598 if (err)
3599 return err;
3600
3601 if (cmd.comp_mask)
3602 return -EINVAL;
3603
3604 if (cmd.reserved)
3605 return -EINVAL;
3606
Haggai Eranf4056bf2015-02-08 13:28:51 +02003607 resp.response_length = offsetof(typeof(resp), odp_caps);
Eli Cohen02d1aa72015-02-08 13:28:50 +02003608
3609 if (ucore->outlen < resp.response_length)
3610 return -ENOSPC;
3611
Matan Barak24306dc2015-06-11 16:35:24 +03003612 memset(&attr, 0, sizeof(attr));
3613
Yishai Hadas057aec02015-08-13 18:32:04 +03003614 err = ib_dev->query_device(ib_dev, &attr, uhw);
Eli Cohen02d1aa72015-02-08 13:28:50 +02003615 if (err)
3616 return err;
3617
Yishai Hadas057aec02015-08-13 18:32:04 +03003618 copy_query_dev_fields(file, ib_dev, &resp.base, &attr);
Eli Cohen02d1aa72015-02-08 13:28:50 +02003619 resp.comp_mask = 0;
3620
Haggai Eranf4056bf2015-02-08 13:28:51 +02003621 if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
3622 goto end;
3623
3624#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3625 resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3626 resp.odp_caps.per_transport_caps.rc_odp_caps =
3627 attr.odp_caps.per_transport_caps.rc_odp_caps;
3628 resp.odp_caps.per_transport_caps.uc_odp_caps =
3629 attr.odp_caps.per_transport_caps.uc_odp_caps;
3630 resp.odp_caps.per_transport_caps.ud_odp_caps =
3631 attr.odp_caps.per_transport_caps.ud_odp_caps;
3632 resp.odp_caps.reserved = 0;
3633#else
3634 memset(&resp.odp_caps, 0, sizeof(resp.odp_caps));
3635#endif
3636 resp.response_length += sizeof(resp.odp_caps);
3637
Matan Barak24306dc2015-06-11 16:35:24 +03003638 if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
3639 goto end;
3640
3641 resp.timestamp_mask = attr.timestamp_mask;
3642 resp.response_length += sizeof(resp.timestamp_mask);
3643
3644 if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
3645 goto end;
3646
3647 resp.hca_core_clock = attr.hca_core_clock;
3648 resp.response_length += sizeof(resp.hca_core_clock);
3649
Haggai Eranf4056bf2015-02-08 13:28:51 +02003650end:
Eli Cohen02d1aa72015-02-08 13:28:50 +02003651 err = ib_copy_to_udata(ucore, &resp, resp.response_length);
3652 if (err)
3653 return err;
3654
3655 return 0;
3656}