blob: 9e98cec6230fd8398f7c0c4ddc7071e3f22b5366 [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.
34 *
35 * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
36 */
37
Roland Dreier6b73597e2005-09-26 13:53:25 -070038#include <linux/file.h>
Roland Dreier70a30e12005-10-28 15:38:26 -070039#include <linux/fs.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"
44
Roland Dreier43db2bc2006-07-23 15:16:04 -070045static struct lock_class_key pd_lock_key;
46static struct lock_class_key mr_lock_key;
47static struct lock_class_key cq_lock_key;
48static struct lock_class_key qp_lock_key;
49static struct lock_class_key ah_lock_key;
50static struct lock_class_key srq_lock_key;
51
Roland Dreierbc38a6a2005-07-07 17:57:13 -070052#define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
53 do { \
54 (udata)->inbuf = (void __user *) (ibuf); \
55 (udata)->outbuf = (void __user *) (obuf); \
56 (udata)->inlen = (ilen); \
57 (udata)->outlen = (olen); \
58 } while (0)
59
Roland Dreier9ead1902006-06-17 20:44:49 -070060/*
61 * The ib_uobject locking scheme is as follows:
62 *
63 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
64 * needs to be held during all idr operations. When an object is
65 * looked up, a reference must be taken on the object's kref before
66 * dropping this lock.
67 *
68 * - Each object also has an rwsem. This rwsem must be held for
69 * reading while an operation that uses the object is performed.
70 * For example, while registering an MR, the associated PD's
71 * uobject.mutex must be held for reading. The rwsem must be held
72 * for writing while initializing or destroying an object.
73 *
74 * - In addition, each object has a "live" flag. If this flag is not
75 * set, then lookups of the object will fail even if it is found in
76 * the idr. This handles a reader that blocks and does not acquire
77 * the rwsem until after the object is destroyed. The destroy
78 * operation will set the live flag to 0 and then drop the rwsem;
79 * this will allow the reader to acquire the rwsem, see that the
80 * live flag is 0, and then drop the rwsem and its reference to
81 * object. The underlying storage will not be freed until the last
82 * reference to the object is dropped.
83 */
84
85static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
Roland Dreier43db2bc2006-07-23 15:16:04 -070086 struct ib_ucontext *context, struct lock_class_key *key)
Roland Dreier9ead1902006-06-17 20:44:49 -070087{
88 uobj->user_handle = user_handle;
89 uobj->context = context;
90 kref_init(&uobj->ref);
91 init_rwsem(&uobj->mutex);
Roland Dreier43db2bc2006-07-23 15:16:04 -070092 lockdep_set_class(&uobj->mutex, key);
Roland Dreier9ead1902006-06-17 20:44:49 -070093 uobj->live = 0;
94}
95
96static void release_uobj(struct kref *kref)
97{
98 kfree(container_of(kref, struct ib_uobject, ref));
99}
100
101static void put_uobj(struct ib_uobject *uobj)
102{
103 kref_put(&uobj->ref, release_uobj);
104}
105
106static void put_uobj_read(struct ib_uobject *uobj)
107{
108 up_read(&uobj->mutex);
109 put_uobj(uobj);
110}
111
112static void put_uobj_write(struct ib_uobject *uobj)
113{
114 up_write(&uobj->mutex);
115 put_uobj(uobj);
116}
117
118static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
Roland Dreier34631752006-06-17 20:37:40 -0700119{
120 int ret;
121
122retry:
123 if (!idr_pre_get(idr, GFP_KERNEL))
124 return -ENOMEM;
125
Roland Dreier9ead1902006-06-17 20:44:49 -0700126 spin_lock(&ib_uverbs_idr_lock);
Roland Dreier34631752006-06-17 20:37:40 -0700127 ret = idr_get_new(idr, uobj, &uobj->id);
Roland Dreier9ead1902006-06-17 20:44:49 -0700128 spin_unlock(&ib_uverbs_idr_lock);
Roland Dreier34631752006-06-17 20:37:40 -0700129
130 if (ret == -EAGAIN)
131 goto retry;
132
133 return ret;
134}
135
Roland Dreier9ead1902006-06-17 20:44:49 -0700136void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
137{
138 spin_lock(&ib_uverbs_idr_lock);
139 idr_remove(idr, uobj->id);
140 spin_unlock(&ib_uverbs_idr_lock);
141}
142
143static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
144 struct ib_ucontext *context)
145{
146 struct ib_uobject *uobj;
147
148 spin_lock(&ib_uverbs_idr_lock);
149 uobj = idr_find(idr, id);
Roland Dreiercbfb50e2007-10-19 20:01:43 -0700150 if (uobj) {
151 if (uobj->context == context)
152 kref_get(&uobj->ref);
153 else
154 uobj = NULL;
155 }
Roland Dreier9ead1902006-06-17 20:44:49 -0700156 spin_unlock(&ib_uverbs_idr_lock);
157
158 return uobj;
159}
160
161static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700162 struct ib_ucontext *context, int nested)
Roland Dreier9ead1902006-06-17 20:44:49 -0700163{
164 struct ib_uobject *uobj;
165
166 uobj = __idr_get_uobj(idr, id, context);
167 if (!uobj)
168 return NULL;
169
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700170 if (nested)
171 down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
172 else
173 down_read(&uobj->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -0700174 if (!uobj->live) {
175 put_uobj_read(uobj);
176 return NULL;
177 }
178
179 return uobj;
180}
181
182static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
183 struct ib_ucontext *context)
184{
185 struct ib_uobject *uobj;
186
187 uobj = __idr_get_uobj(idr, id, context);
188 if (!uobj)
189 return NULL;
190
191 down_write(&uobj->mutex);
192 if (!uobj->live) {
193 put_uobj_write(uobj);
194 return NULL;
195 }
196
197 return uobj;
198}
199
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700200static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
201 int nested)
Roland Dreier9ead1902006-06-17 20:44:49 -0700202{
203 struct ib_uobject *uobj;
204
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700205 uobj = idr_read_uobj(idr, id, context, nested);
Roland Dreier9ead1902006-06-17 20:44:49 -0700206 return uobj ? uobj->object : NULL;
207}
208
209static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
210{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700211 return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700212}
213
214static void put_pd_read(struct ib_pd *pd)
215{
216 put_uobj_read(pd->uobject);
217}
218
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700219static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
Roland Dreier9ead1902006-06-17 20:44:49 -0700220{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700221 return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
Roland Dreier9ead1902006-06-17 20:44:49 -0700222}
223
224static void put_cq_read(struct ib_cq *cq)
225{
226 put_uobj_read(cq->uobject);
227}
228
229static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
230{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700231 return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700232}
233
234static void put_ah_read(struct ib_ah *ah)
235{
236 put_uobj_read(ah->uobject);
237}
238
239static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
240{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700241 return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700242}
243
244static void put_qp_read(struct ib_qp *qp)
245{
246 put_uobj_read(qp->uobject);
247}
248
249static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
250{
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700251 return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700252}
253
254static void put_srq_read(struct ib_srq *srq)
255{
256 put_uobj_read(srq->uobject);
257}
258
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700259ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
260 const char __user *buf,
261 int in_len, int out_len)
262{
263 struct ib_uverbs_get_context cmd;
264 struct ib_uverbs_get_context_resp resp;
265 struct ib_udata udata;
266 struct ib_device *ibdev = file->device->ib_dev;
Roland Dreier63c47c22005-09-26 13:01:03 -0700267 struct ib_ucontext *ucontext;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700268 struct file *filp;
Roland Dreier63c47c22005-09-26 13:01:03 -0700269 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700270
271 if (out_len < sizeof resp)
272 return -ENOSPC;
273
274 if (copy_from_user(&cmd, buf, sizeof cmd))
275 return -EFAULT;
276
Ingo Molnar95ed6442006-01-13 14:51:39 -0800277 mutex_lock(&file->mutex);
Roland Dreier63c47c22005-09-26 13:01:03 -0700278
279 if (file->ucontext) {
280 ret = -EINVAL;
281 goto err;
282 }
283
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700284 INIT_UDATA(&udata, buf + sizeof cmd,
285 (unsigned long) cmd.response + sizeof resp,
286 in_len - sizeof cmd, out_len - sizeof resp);
287
Roland Dreier63c47c22005-09-26 13:01:03 -0700288 ucontext = ibdev->alloc_ucontext(ibdev, &udata);
Ganapathi CH77f76012006-06-17 20:37:40 -0700289 if (IS_ERR(ucontext)) {
290 ret = PTR_ERR(file->ucontext);
291 goto err;
292 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700293
Roland Dreier63c47c22005-09-26 13:01:03 -0700294 ucontext->device = ibdev;
295 INIT_LIST_HEAD(&ucontext->pd_list);
296 INIT_LIST_HEAD(&ucontext->mr_list);
297 INIT_LIST_HEAD(&ucontext->mw_list);
298 INIT_LIST_HEAD(&ucontext->cq_list);
299 INIT_LIST_HEAD(&ucontext->qp_list);
300 INIT_LIST_HEAD(&ucontext->srq_list);
301 INIT_LIST_HEAD(&ucontext->ah_list);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800302 ucontext->closing = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700303
Roland Dreier6b73597e2005-09-26 13:53:25 -0700304 resp.num_comp_vectors = file->device->num_comp_vectors;
305
306 filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
307 if (IS_ERR(filp)) {
308 ret = PTR_ERR(filp);
309 goto err_free;
310 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700311
312 if (copy_to_user((void __user *) (unsigned long) cmd.response,
Roland Dreier63c47c22005-09-26 13:01:03 -0700313 &resp, sizeof resp)) {
314 ret = -EFAULT;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700315 goto err_file;
Roland Dreier63c47c22005-09-26 13:01:03 -0700316 }
317
Roland Dreier6b73597e2005-09-26 13:53:25 -0700318 file->async_file = filp->private_data;
319
320 INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
321 ib_uverbs_event_handler);
322 ret = ib_register_event_handler(&file->event_handler);
323 if (ret)
324 goto err_file;
325
326 kref_get(&file->async_file->ref);
327 kref_get(&file->ref);
Roland Dreier70a30e12005-10-28 15:38:26 -0700328 file->ucontext = ucontext;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700329
330 fd_install(resp.async_fd, filp);
331
Ingo Molnar95ed6442006-01-13 14:51:39 -0800332 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700333
334 return in_len;
335
Roland Dreier6b73597e2005-09-26 13:53:25 -0700336err_file:
337 put_unused_fd(resp.async_fd);
338 fput(filp);
339
Roland Dreier63c47c22005-09-26 13:01:03 -0700340err_free:
341 ibdev->dealloc_ucontext(ucontext);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700342
Roland Dreier63c47c22005-09-26 13:01:03 -0700343err:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800344 mutex_unlock(&file->mutex);
Roland Dreier63c47c22005-09-26 13:01:03 -0700345 return ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700346}
347
348ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
349 const char __user *buf,
350 int in_len, int out_len)
351{
352 struct ib_uverbs_query_device cmd;
353 struct ib_uverbs_query_device_resp resp;
354 struct ib_device_attr attr;
355 int ret;
356
357 if (out_len < sizeof resp)
358 return -ENOSPC;
359
360 if (copy_from_user(&cmd, buf, sizeof cmd))
361 return -EFAULT;
362
363 ret = ib_query_device(file->device->ib_dev, &attr);
364 if (ret)
365 return ret;
366
367 memset(&resp, 0, sizeof resp);
368
369 resp.fw_ver = attr.fw_ver;
Sean Heftycf311cd2006-01-10 07:39:34 -0800370 resp.node_guid = file->device->ib_dev->node_guid;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700371 resp.sys_image_guid = attr.sys_image_guid;
372 resp.max_mr_size = attr.max_mr_size;
373 resp.page_size_cap = attr.page_size_cap;
374 resp.vendor_id = attr.vendor_id;
375 resp.vendor_part_id = attr.vendor_part_id;
376 resp.hw_ver = attr.hw_ver;
377 resp.max_qp = attr.max_qp;
378 resp.max_qp_wr = attr.max_qp_wr;
379 resp.device_cap_flags = attr.device_cap_flags;
380 resp.max_sge = attr.max_sge;
381 resp.max_sge_rd = attr.max_sge_rd;
382 resp.max_cq = attr.max_cq;
383 resp.max_cqe = attr.max_cqe;
384 resp.max_mr = attr.max_mr;
385 resp.max_pd = attr.max_pd;
386 resp.max_qp_rd_atom = attr.max_qp_rd_atom;
387 resp.max_ee_rd_atom = attr.max_ee_rd_atom;
388 resp.max_res_rd_atom = attr.max_res_rd_atom;
389 resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
390 resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
391 resp.atomic_cap = attr.atomic_cap;
392 resp.max_ee = attr.max_ee;
393 resp.max_rdd = attr.max_rdd;
394 resp.max_mw = attr.max_mw;
395 resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
396 resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
397 resp.max_mcast_grp = attr.max_mcast_grp;
398 resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
399 resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
400 resp.max_ah = attr.max_ah;
401 resp.max_fmr = attr.max_fmr;
402 resp.max_map_per_fmr = attr.max_map_per_fmr;
403 resp.max_srq = attr.max_srq;
404 resp.max_srq_wr = attr.max_srq_wr;
405 resp.max_srq_sge = attr.max_srq_sge;
406 resp.max_pkeys = attr.max_pkeys;
407 resp.local_ca_ack_delay = attr.local_ca_ack_delay;
408 resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
409
410 if (copy_to_user((void __user *) (unsigned long) cmd.response,
411 &resp, sizeof resp))
412 return -EFAULT;
413
414 return in_len;
415}
416
417ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
418 const char __user *buf,
419 int in_len, int out_len)
420{
421 struct ib_uverbs_query_port cmd;
422 struct ib_uverbs_query_port_resp resp;
423 struct ib_port_attr attr;
424 int ret;
425
426 if (out_len < sizeof resp)
427 return -ENOSPC;
428
429 if (copy_from_user(&cmd, buf, sizeof cmd))
430 return -EFAULT;
431
432 ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
433 if (ret)
434 return ret;
435
436 memset(&resp, 0, sizeof resp);
437
438 resp.state = attr.state;
439 resp.max_mtu = attr.max_mtu;
440 resp.active_mtu = attr.active_mtu;
441 resp.gid_tbl_len = attr.gid_tbl_len;
442 resp.port_cap_flags = attr.port_cap_flags;
443 resp.max_msg_sz = attr.max_msg_sz;
444 resp.bad_pkey_cntr = attr.bad_pkey_cntr;
445 resp.qkey_viol_cntr = attr.qkey_viol_cntr;
446 resp.pkey_tbl_len = attr.pkey_tbl_len;
447 resp.lid = attr.lid;
448 resp.sm_lid = attr.sm_lid;
449 resp.lmc = attr.lmc;
450 resp.max_vl_num = attr.max_vl_num;
451 resp.sm_sl = attr.sm_sl;
452 resp.subnet_timeout = attr.subnet_timeout;
453 resp.init_type_reply = attr.init_type_reply;
454 resp.active_width = attr.active_width;
455 resp.active_speed = attr.active_speed;
456 resp.phys_state = attr.phys_state;
457
458 if (copy_to_user((void __user *) (unsigned long) cmd.response,
459 &resp, sizeof resp))
460 return -EFAULT;
461
462 return in_len;
463}
464
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700465ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
466 const char __user *buf,
467 int in_len, int out_len)
468{
469 struct ib_uverbs_alloc_pd cmd;
470 struct ib_uverbs_alloc_pd_resp resp;
471 struct ib_udata udata;
472 struct ib_uobject *uobj;
473 struct ib_pd *pd;
474 int ret;
475
476 if (out_len < sizeof resp)
477 return -ENOSPC;
478
479 if (copy_from_user(&cmd, buf, sizeof cmd))
480 return -EFAULT;
481
482 INIT_UDATA(&udata, buf + sizeof cmd,
483 (unsigned long) cmd.response + sizeof resp,
484 in_len - sizeof cmd, out_len - sizeof resp);
485
486 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
487 if (!uobj)
488 return -ENOMEM;
489
Roland Dreier43db2bc2006-07-23 15:16:04 -0700490 init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
Roland Dreier9ead1902006-06-17 20:44:49 -0700491 down_write(&uobj->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700492
493 pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
494 file->ucontext, &udata);
495 if (IS_ERR(pd)) {
496 ret = PTR_ERR(pd);
497 goto err;
498 }
499
500 pd->device = file->device->ib_dev;
501 pd->uobject = uobj;
502 atomic_set(&pd->usecnt, 0);
503
Roland Dreier9ead1902006-06-17 20:44:49 -0700504 uobj->object = pd;
505 ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700506 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -0700507 goto err_idr;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700508
509 memset(&resp, 0, sizeof resp);
510 resp.pd_handle = uobj->id;
511
512 if (copy_to_user((void __user *) (unsigned long) cmd.response,
513 &resp, sizeof resp)) {
514 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -0700515 goto err_copy;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700516 }
517
Ingo Molnar95ed6442006-01-13 14:51:39 -0800518 mutex_lock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700519 list_add_tail(&uobj->list, &file->ucontext->pd_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800520 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700521
Roland Dreier9ead1902006-06-17 20:44:49 -0700522 uobj->live = 1;
523
524 up_write(&uobj->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700525
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700526 return in_len;
527
Roland Dreier9ead1902006-06-17 20:44:49 -0700528err_copy:
529 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700530
Roland Dreier9ead1902006-06-17 20:44:49 -0700531err_idr:
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700532 ib_dealloc_pd(pd);
533
534err:
Roland Dreier9ead1902006-06-17 20:44:49 -0700535 put_uobj_write(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700536 return ret;
537}
538
539ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
540 const char __user *buf,
541 int in_len, int out_len)
542{
543 struct ib_uverbs_dealloc_pd cmd;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700544 struct ib_uobject *uobj;
Roland Dreier9ead1902006-06-17 20:44:49 -0700545 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700546
547 if (copy_from_user(&cmd, buf, sizeof cmd))
548 return -EFAULT;
549
Roland Dreier9ead1902006-06-17 20:44:49 -0700550 uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
551 if (!uobj)
552 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700553
Roland Dreier9ead1902006-06-17 20:44:49 -0700554 ret = ib_dealloc_pd(uobj->object);
555 if (!ret)
556 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700557
Roland Dreier9ead1902006-06-17 20:44:49 -0700558 put_uobj_write(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700559
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700560 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -0700561 return ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700562
Roland Dreier9ead1902006-06-17 20:44:49 -0700563 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700564
Ingo Molnar95ed6442006-01-13 14:51:39 -0800565 mutex_lock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700566 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800567 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700568
Roland Dreier9ead1902006-06-17 20:44:49 -0700569 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700570
Roland Dreier9ead1902006-06-17 20:44:49 -0700571 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700572}
573
574ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
575 const char __user *buf, int in_len,
576 int out_len)
577{
578 struct ib_uverbs_reg_mr cmd;
579 struct ib_uverbs_reg_mr_resp resp;
580 struct ib_udata udata;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800581 struct ib_uobject *uobj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700582 struct ib_pd *pd;
583 struct ib_mr *mr;
584 int ret;
585
586 if (out_len < sizeof resp)
587 return -ENOSPC;
588
589 if (copy_from_user(&cmd, buf, sizeof cmd))
590 return -EFAULT;
591
592 INIT_UDATA(&udata, buf + sizeof cmd,
593 (unsigned long) cmd.response + sizeof resp,
594 in_len - sizeof cmd, out_len - sizeof resp);
595
596 if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
597 return -EINVAL;
598
Roland Dreierf5753942005-10-03 09:18:02 -0700599 /*
600 * Local write permission is required if remote write or
601 * remote atomic permission is also requested.
602 */
603 if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
604 !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
605 return -EINVAL;
606
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800607 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
608 if (!uobj)
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700609 return -ENOMEM;
610
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800611 init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
612 down_write(&uobj->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700613
Roland Dreier9ead1902006-06-17 20:44:49 -0700614 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
Roland Dreieraaf1aef2007-02-22 13:16:51 -0800615 if (!pd) {
616 ret = -EINVAL;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800617 goto err_free;
Roland Dreieraaf1aef2007-02-22 13:16:51 -0800618 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700619
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800620 mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
621 cmd.access_flags, &udata);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700622 if (IS_ERR(mr)) {
623 ret = PTR_ERR(mr);
Roland Dreier9ead1902006-06-17 20:44:49 -0700624 goto err_put;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700625 }
626
627 mr->device = pd->device;
628 mr->pd = pd;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800629 mr->uobject = uobj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700630 atomic_inc(&pd->usecnt);
631 atomic_set(&mr->usecnt, 0);
632
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800633 uobj->object = mr;
634 ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700635 if (ret)
636 goto err_unreg;
637
Roland Dreier9ead1902006-06-17 20:44:49 -0700638 memset(&resp, 0, sizeof resp);
639 resp.lkey = mr->lkey;
640 resp.rkey = mr->rkey;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800641 resp.mr_handle = uobj->id;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700642
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700643 if (copy_to_user((void __user *) (unsigned long) cmd.response,
644 &resp, sizeof resp)) {
645 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -0700646 goto err_copy;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700647 }
648
Roland Dreier9ead1902006-06-17 20:44:49 -0700649 put_pd_read(pd);
650
Ingo Molnar95ed6442006-01-13 14:51:39 -0800651 mutex_lock(&file->mutex);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800652 list_add_tail(&uobj->list, &file->ucontext->mr_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800653 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700654
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800655 uobj->live = 1;
Roland Dreier9ead1902006-06-17 20:44:49 -0700656
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800657 up_write(&uobj->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700658
659 return in_len;
660
Roland Dreier9ead1902006-06-17 20:44:49 -0700661err_copy:
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800662 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700663
664err_unreg:
665 ib_dereg_mr(mr);
666
Roland Dreier9ead1902006-06-17 20:44:49 -0700667err_put:
668 put_pd_read(pd);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700669
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700670err_free:
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800671 put_uobj_write(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700672 return ret;
673}
674
675ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
676 const char __user *buf, int in_len,
677 int out_len)
678{
679 struct ib_uverbs_dereg_mr cmd;
680 struct ib_mr *mr;
Roland Dreier9ead1902006-06-17 20:44:49 -0700681 struct ib_uobject *uobj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700682 int ret = -EINVAL;
683
684 if (copy_from_user(&cmd, buf, sizeof cmd))
685 return -EFAULT;
686
Roland Dreier9ead1902006-06-17 20:44:49 -0700687 uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
688 if (!uobj)
689 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700690
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800691 mr = uobj->object;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700692
693 ret = ib_dereg_mr(mr);
Roland Dreier9ead1902006-06-17 20:44:49 -0700694 if (!ret)
695 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700696
Roland Dreier9ead1902006-06-17 20:44:49 -0700697 put_uobj_write(uobj);
698
699 if (ret)
700 return ret;
701
702 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700703
Ingo Molnar95ed6442006-01-13 14:51:39 -0800704 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -0700705 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800706 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700707
Roland Dreier9ead1902006-06-17 20:44:49 -0700708 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700709
Roland Dreier9ead1902006-06-17 20:44:49 -0700710 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700711}
712
Roland Dreier6b73597e2005-09-26 13:53:25 -0700713ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
714 const char __user *buf, int in_len,
715 int out_len)
716{
717 struct ib_uverbs_create_comp_channel cmd;
718 struct ib_uverbs_create_comp_channel_resp resp;
719 struct file *filp;
720
721 if (out_len < sizeof resp)
722 return -ENOSPC;
723
724 if (copy_from_user(&cmd, buf, sizeof cmd))
725 return -EFAULT;
726
727 filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
728 if (IS_ERR(filp))
729 return PTR_ERR(filp);
730
731 if (copy_to_user((void __user *) (unsigned long) cmd.response,
732 &resp, sizeof resp)) {
733 put_unused_fd(resp.fd);
734 fput(filp);
735 return -EFAULT;
736 }
737
738 fd_install(resp.fd, filp);
739 return in_len;
740}
741
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700742ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
743 const char __user *buf, int in_len,
744 int out_len)
745{
746 struct ib_uverbs_create_cq cmd;
747 struct ib_uverbs_create_cq_resp resp;
748 struct ib_udata udata;
Roland Dreier9ead1902006-06-17 20:44:49 -0700749 struct ib_ucq_object *obj;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700750 struct ib_uverbs_event_file *ev_file = NULL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700751 struct ib_cq *cq;
752 int ret;
753
754 if (out_len < sizeof resp)
755 return -ENOSPC;
756
757 if (copy_from_user(&cmd, buf, sizeof cmd))
758 return -EFAULT;
759
760 INIT_UDATA(&udata, buf + sizeof cmd,
761 (unsigned long) cmd.response + sizeof resp,
762 in_len - sizeof cmd, out_len - sizeof resp);
763
Roland Dreier6b73597e2005-09-26 13:53:25 -0700764 if (cmd.comp_vector >= file->device->num_comp_vectors)
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700765 return -EINVAL;
766
Roland Dreier9ead1902006-06-17 20:44:49 -0700767 obj = kmalloc(sizeof *obj, GFP_KERNEL);
768 if (!obj)
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700769 return -ENOMEM;
770
Roland Dreier43db2bc2006-07-23 15:16:04 -0700771 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
Roland Dreier9ead1902006-06-17 20:44:49 -0700772 down_write(&obj->uobject.mutex);
773
Jack Morgensteinac4e7b32006-01-06 16:43:14 -0800774 if (cmd.comp_channel >= 0) {
775 ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
776 if (!ev_file) {
777 ret = -EINVAL;
778 goto err;
779 }
780 }
781
Roland Dreier9ead1902006-06-17 20:44:49 -0700782 obj->uverbs_file = file;
783 obj->comp_events_reported = 0;
784 obj->async_events_reported = 0;
785 INIT_LIST_HEAD(&obj->comp_list);
786 INIT_LIST_HEAD(&obj->async_list);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700787
788 cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300789 cmd.comp_vector,
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700790 file->ucontext, &udata);
791 if (IS_ERR(cq)) {
792 ret = PTR_ERR(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -0700793 goto err_file;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700794 }
795
796 cq->device = file->device->ib_dev;
Roland Dreier9ead1902006-06-17 20:44:49 -0700797 cq->uobject = &obj->uobject;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700798 cq->comp_handler = ib_uverbs_comp_handler;
799 cq->event_handler = ib_uverbs_cq_event_handler;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700800 cq->cq_context = ev_file;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700801 atomic_set(&cq->usecnt, 0);
802
Roland Dreier9ead1902006-06-17 20:44:49 -0700803 obj->uobject.object = cq;
804 ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700805 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -0700806 goto err_free;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700807
808 memset(&resp, 0, sizeof resp);
Roland Dreier9ead1902006-06-17 20:44:49 -0700809 resp.cq_handle = obj->uobject.id;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700810 resp.cqe = cq->cqe;
811
812 if (copy_to_user((void __user *) (unsigned long) cmd.response,
813 &resp, sizeof resp)) {
814 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -0700815 goto err_copy;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700816 }
817
Ingo Molnar95ed6442006-01-13 14:51:39 -0800818 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -0700819 list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800820 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700821
Roland Dreier9ead1902006-06-17 20:44:49 -0700822 obj->uobject.live = 1;
823
824 up_write(&obj->uobject.mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -0700825
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700826 return in_len;
827
Roland Dreier9ead1902006-06-17 20:44:49 -0700828err_copy:
829 idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700830
Roland Dreier9ead1902006-06-17 20:44:49 -0700831err_free:
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700832 ib_destroy_cq(cq);
833
Roland Dreier9ead1902006-06-17 20:44:49 -0700834err_file:
Jack Morgensteinac4e7b32006-01-06 16:43:14 -0800835 if (ev_file)
Roland Dreier9ead1902006-06-17 20:44:49 -0700836 ib_uverbs_release_ucq(file, ev_file, obj);
837
838err:
839 put_uobj_write(&obj->uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700840 return ret;
841}
842
Roland Dreier33b9b3e2006-01-30 14:29:21 -0800843ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
844 const char __user *buf, int in_len,
845 int out_len)
846{
847 struct ib_uverbs_resize_cq cmd;
848 struct ib_uverbs_resize_cq_resp resp;
849 struct ib_udata udata;
850 struct ib_cq *cq;
851 int ret = -EINVAL;
852
853 if (copy_from_user(&cmd, buf, sizeof cmd))
854 return -EFAULT;
855
856 INIT_UDATA(&udata, buf + sizeof cmd,
857 (unsigned long) cmd.response + sizeof resp,
858 in_len - sizeof cmd, out_len - sizeof resp);
859
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700860 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
Roland Dreier9ead1902006-06-17 20:44:49 -0700861 if (!cq)
862 return -EINVAL;
Roland Dreier33b9b3e2006-01-30 14:29:21 -0800863
864 ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
865 if (ret)
866 goto out;
867
Roland Dreier33b9b3e2006-01-30 14:29:21 -0800868 resp.cqe = cq->cqe;
869
870 if (copy_to_user((void __user *) (unsigned long) cmd.response,
Ralph Campbell64f817b2006-09-22 15:22:24 -0700871 &resp, sizeof resp.cqe))
Roland Dreier33b9b3e2006-01-30 14:29:21 -0800872 ret = -EFAULT;
873
874out:
Roland Dreier9ead1902006-06-17 20:44:49 -0700875 put_cq_read(cq);
Roland Dreier33b9b3e2006-01-30 14:29:21 -0800876
877 return ret ? ret : in_len;
878}
879
Roland Dreier67cdb402005-10-14 15:26:04 -0700880ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
881 const char __user *buf, int in_len,
882 int out_len)
883{
884 struct ib_uverbs_poll_cq cmd;
885 struct ib_uverbs_poll_cq_resp *resp;
886 struct ib_cq *cq;
887 struct ib_wc *wc;
888 int ret = 0;
889 int i;
890 int rsize;
891
892 if (copy_from_user(&cmd, buf, sizeof cmd))
893 return -EFAULT;
894
895 wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);
896 if (!wc)
897 return -ENOMEM;
898
899 rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);
900 resp = kmalloc(rsize, GFP_KERNEL);
901 if (!resp) {
902 ret = -ENOMEM;
903 goto out_wc;
904 }
905
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700906 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
Roland Dreierab108672006-09-22 15:17:19 -0700907 if (!cq) {
Roland Dreier67cdb402005-10-14 15:26:04 -0700908 ret = -EINVAL;
909 goto out;
910 }
911
912 resp->count = ib_poll_cq(cq, cmd.ne, wc);
913
Roland Dreierab108672006-09-22 15:17:19 -0700914 put_cq_read(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -0700915
Roland Dreier67cdb402005-10-14 15:26:04 -0700916 for (i = 0; i < resp->count; i++) {
917 resp->wc[i].wr_id = wc[i].wr_id;
918 resp->wc[i].status = wc[i].status;
919 resp->wc[i].opcode = wc[i].opcode;
920 resp->wc[i].vendor_err = wc[i].vendor_err;
921 resp->wc[i].byte_len = wc[i].byte_len;
Jack Morgenstein77369ed2005-11-09 11:26:07 -0800922 resp->wc[i].imm_data = (__u32 __force) wc[i].imm_data;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200923 resp->wc[i].qp_num = wc[i].qp->qp_num;
Roland Dreier67cdb402005-10-14 15:26:04 -0700924 resp->wc[i].src_qp = wc[i].src_qp;
925 resp->wc[i].wc_flags = wc[i].wc_flags;
926 resp->wc[i].pkey_index = wc[i].pkey_index;
927 resp->wc[i].slid = wc[i].slid;
928 resp->wc[i].sl = wc[i].sl;
929 resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;
930 resp->wc[i].port_num = wc[i].port_num;
931 }
932
933 if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))
934 ret = -EFAULT;
935
936out:
Roland Dreier67cdb402005-10-14 15:26:04 -0700937 kfree(resp);
938
939out_wc:
940 kfree(wc);
941 return ret ? ret : in_len;
942}
943
944ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
945 const char __user *buf, int in_len,
946 int out_len)
947{
948 struct ib_uverbs_req_notify_cq cmd;
949 struct ib_cq *cq;
Roland Dreier67cdb402005-10-14 15:26:04 -0700950
951 if (copy_from_user(&cmd, buf, sizeof cmd))
952 return -EFAULT;
953
Roland Dreier1ccf6aa2006-09-22 15:17:20 -0700954 cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
Roland Dreierab108672006-09-22 15:17:19 -0700955 if (!cq)
Roland Dreier9ead1902006-06-17 20:44:49 -0700956 return -EINVAL;
Roland Dreier67cdb402005-10-14 15:26:04 -0700957
Roland Dreier9ead1902006-06-17 20:44:49 -0700958 ib_req_notify_cq(cq, cmd.solicited_only ?
959 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
960
Roland Dreierab108672006-09-22 15:17:19 -0700961 put_cq_read(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -0700962
963 return in_len;
Roland Dreier67cdb402005-10-14 15:26:04 -0700964}
965
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700966ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
967 const char __user *buf, int in_len,
968 int out_len)
969{
Roland Dreier63aaf642005-09-09 15:55:08 -0700970 struct ib_uverbs_destroy_cq cmd;
971 struct ib_uverbs_destroy_cq_resp resp;
Roland Dreier9ead1902006-06-17 20:44:49 -0700972 struct ib_uobject *uobj;
Roland Dreier63aaf642005-09-09 15:55:08 -0700973 struct ib_cq *cq;
Roland Dreier9ead1902006-06-17 20:44:49 -0700974 struct ib_ucq_object *obj;
Roland Dreier6b73597e2005-09-26 13:53:25 -0700975 struct ib_uverbs_event_file *ev_file;
Roland Dreier63aaf642005-09-09 15:55:08 -0700976 int ret = -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700977
978 if (copy_from_user(&cmd, buf, sizeof cmd))
979 return -EFAULT;
980
Roland Dreier9ead1902006-06-17 20:44:49 -0700981 uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
982 if (!uobj)
983 return -EINVAL;
984 cq = uobj->object;
985 ev_file = cq->cq_context;
986 obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700987
988 ret = ib_destroy_cq(cq);
Roland Dreier9ead1902006-06-17 20:44:49 -0700989 if (!ret)
990 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700991
Roland Dreier9ead1902006-06-17 20:44:49 -0700992 put_uobj_write(uobj);
993
994 if (ret)
995 return ret;
996
997 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -0700998
Ingo Molnar95ed6442006-01-13 14:51:39 -0800999 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001000 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001001 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001002
Roland Dreier9ead1902006-06-17 20:44:49 -07001003 ib_uverbs_release_ucq(file, ev_file, obj);
Roland Dreier63aaf642005-09-09 15:55:08 -07001004
Roland Dreier9ead1902006-06-17 20:44:49 -07001005 memset(&resp, 0, sizeof resp);
1006 resp.comp_events_reported = obj->comp_events_reported;
1007 resp.async_events_reported = obj->async_events_reported;
Roland Dreier63aaf642005-09-09 15:55:08 -07001008
Roland Dreier9ead1902006-06-17 20:44:49 -07001009 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001010
Roland Dreier63aaf642005-09-09 15:55:08 -07001011 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1012 &resp, sizeof resp))
Roland Dreier9ead1902006-06-17 20:44:49 -07001013 return -EFAULT;
Roland Dreier63aaf642005-09-09 15:55:08 -07001014
Roland Dreier9ead1902006-06-17 20:44:49 -07001015 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001016}
1017
1018ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1019 const char __user *buf, int in_len,
1020 int out_len)
1021{
1022 struct ib_uverbs_create_qp cmd;
1023 struct ib_uverbs_create_qp_resp resp;
1024 struct ib_udata udata;
Roland Dreier9ead1902006-06-17 20:44:49 -07001025 struct ib_uqp_object *obj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001026 struct ib_pd *pd;
1027 struct ib_cq *scq, *rcq;
Roland Dreierf520ba52005-08-18 12:24:13 -07001028 struct ib_srq *srq;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001029 struct ib_qp *qp;
1030 struct ib_qp_init_attr attr;
1031 int ret;
1032
1033 if (out_len < sizeof resp)
1034 return -ENOSPC;
1035
1036 if (copy_from_user(&cmd, buf, sizeof cmd))
1037 return -EFAULT;
1038
1039 INIT_UDATA(&udata, buf + sizeof cmd,
1040 (unsigned long) cmd.response + sizeof resp,
1041 in_len - sizeof cmd, out_len - sizeof resp);
1042
Roland Dreier9ead1902006-06-17 20:44:49 -07001043 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1044 if (!obj)
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001045 return -ENOMEM;
1046
Roland Dreier43db2bc2006-07-23 15:16:04 -07001047 init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
Roland Dreier9ead1902006-06-17 20:44:49 -07001048 down_write(&obj->uevent.uobject.mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001049
Roland Dreier43db2bc2006-07-23 15:16:04 -07001050 srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001051 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
Roland Dreier1ccf6aa2006-09-22 15:17:20 -07001052 scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
Roland Dreier43db2bc2006-07-23 15:16:04 -07001053 rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
Roland Dreier1ccf6aa2006-09-22 15:17:20 -07001054 scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001055
Roland Dreier9ead1902006-06-17 20:44:49 -07001056 if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001057 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001058 goto err_put;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001059 }
1060
1061 attr.event_handler = ib_uverbs_qp_event_handler;
1062 attr.qp_context = file;
1063 attr.send_cq = scq;
1064 attr.recv_cq = rcq;
Roland Dreierf520ba52005-08-18 12:24:13 -07001065 attr.srq = srq;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001066 attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1067 attr.qp_type = cmd.qp_type;
Eli Cohenb846f252008-04-16 21:09:27 -07001068 attr.create_flags = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001069
1070 attr.cap.max_send_wr = cmd.max_send_wr;
1071 attr.cap.max_recv_wr = cmd.max_recv_wr;
1072 attr.cap.max_send_sge = cmd.max_send_sge;
1073 attr.cap.max_recv_sge = cmd.max_recv_sge;
1074 attr.cap.max_inline_data = cmd.max_inline_data;
1075
Roland Dreier9ead1902006-06-17 20:44:49 -07001076 obj->uevent.events_reported = 0;
1077 INIT_LIST_HEAD(&obj->uevent.event_list);
1078 INIT_LIST_HEAD(&obj->mcast_list);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001079
1080 qp = pd->device->create_qp(pd, &attr, &udata);
1081 if (IS_ERR(qp)) {
1082 ret = PTR_ERR(qp);
Roland Dreier9ead1902006-06-17 20:44:49 -07001083 goto err_put;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001084 }
1085
1086 qp->device = pd->device;
1087 qp->pd = pd;
1088 qp->send_cq = attr.send_cq;
1089 qp->recv_cq = attr.recv_cq;
1090 qp->srq = attr.srq;
Roland Dreier9ead1902006-06-17 20:44:49 -07001091 qp->uobject = &obj->uevent.uobject;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001092 qp->event_handler = attr.event_handler;
1093 qp->qp_context = attr.qp_context;
1094 qp->qp_type = attr.qp_type;
1095 atomic_inc(&pd->usecnt);
1096 atomic_inc(&attr.send_cq->usecnt);
1097 atomic_inc(&attr.recv_cq->usecnt);
1098 if (attr.srq)
1099 atomic_inc(&attr.srq->usecnt);
1100
Roland Dreier9ead1902006-06-17 20:44:49 -07001101 obj->uevent.uobject.object = qp;
1102 ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001103 if (ret)
1104 goto err_destroy;
1105
Roland Dreier9ead1902006-06-17 20:44:49 -07001106 memset(&resp, 0, sizeof resp);
1107 resp.qpn = qp->qp_num;
1108 resp.qp_handle = obj->uevent.uobject.id;
Jack Morgenstein77369ed2005-11-09 11:26:07 -08001109 resp.max_recv_sge = attr.cap.max_recv_sge;
1110 resp.max_send_sge = attr.cap.max_send_sge;
1111 resp.max_recv_wr = attr.cap.max_recv_wr;
1112 resp.max_send_wr = attr.cap.max_send_wr;
1113 resp.max_inline_data = attr.cap.max_inline_data;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001114
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001115 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1116 &resp, sizeof resp)) {
1117 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07001118 goto err_copy;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001119 }
1120
Roland Dreier9ead1902006-06-17 20:44:49 -07001121 put_pd_read(pd);
1122 put_cq_read(scq);
Roland Dreier43db2bc2006-07-23 15:16:04 -07001123 if (rcq != scq)
1124 put_cq_read(rcq);
Roland Dreier9ead1902006-06-17 20:44:49 -07001125 if (srq)
1126 put_srq_read(srq);
1127
Ingo Molnar95ed6442006-01-13 14:51:39 -08001128 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001129 list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001130 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07001131
Roland Dreier9ead1902006-06-17 20:44:49 -07001132 obj->uevent.uobject.live = 1;
1133
1134 up_write(&obj->uevent.uobject.mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001135
1136 return in_len;
1137
Roland Dreier9ead1902006-06-17 20:44:49 -07001138err_copy:
1139 idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001140
1141err_destroy:
1142 ib_destroy_qp(qp);
1143
Roland Dreier9ead1902006-06-17 20:44:49 -07001144err_put:
1145 if (pd)
1146 put_pd_read(pd);
1147 if (scq)
1148 put_cq_read(scq);
Roland Dreier43db2bc2006-07-23 15:16:04 -07001149 if (rcq && rcq != scq)
Roland Dreier9ead1902006-06-17 20:44:49 -07001150 put_cq_read(rcq);
1151 if (srq)
1152 put_srq_read(srq);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001153
Roland Dreier9ead1902006-06-17 20:44:49 -07001154 put_uobj_write(&obj->uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001155 return ret;
1156}
1157
Dotan Barak7ccc9a22006-02-13 16:31:25 -08001158ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1159 const char __user *buf, int in_len,
1160 int out_len)
1161{
1162 struct ib_uverbs_query_qp cmd;
1163 struct ib_uverbs_query_qp_resp resp;
1164 struct ib_qp *qp;
1165 struct ib_qp_attr *attr;
1166 struct ib_qp_init_attr *init_attr;
1167 int ret;
1168
1169 if (copy_from_user(&cmd, buf, sizeof cmd))
1170 return -EFAULT;
1171
1172 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1173 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1174 if (!attr || !init_attr) {
1175 ret = -ENOMEM;
1176 goto out;
1177 }
1178
Roland Dreier9ead1902006-06-17 20:44:49 -07001179 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1180 if (!qp) {
Dotan Barak7ccc9a22006-02-13 16:31:25 -08001181 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001182 goto out;
1183 }
Dotan Barak7ccc9a22006-02-13 16:31:25 -08001184
Roland Dreier9ead1902006-06-17 20:44:49 -07001185 ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1186
1187 put_qp_read(qp);
Dotan Barak7ccc9a22006-02-13 16:31:25 -08001188
1189 if (ret)
1190 goto out;
1191
1192 memset(&resp, 0, sizeof resp);
1193
1194 resp.qp_state = attr->qp_state;
1195 resp.cur_qp_state = attr->cur_qp_state;
1196 resp.path_mtu = attr->path_mtu;
1197 resp.path_mig_state = attr->path_mig_state;
1198 resp.qkey = attr->qkey;
1199 resp.rq_psn = attr->rq_psn;
1200 resp.sq_psn = attr->sq_psn;
1201 resp.dest_qp_num = attr->dest_qp_num;
1202 resp.qp_access_flags = attr->qp_access_flags;
1203 resp.pkey_index = attr->pkey_index;
1204 resp.alt_pkey_index = attr->alt_pkey_index;
Jack Morgenstein0b26c882006-10-25 12:54:20 +02001205 resp.sq_draining = attr->sq_draining;
Dotan Barak7ccc9a22006-02-13 16:31:25 -08001206 resp.max_rd_atomic = attr->max_rd_atomic;
1207 resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
1208 resp.min_rnr_timer = attr->min_rnr_timer;
1209 resp.port_num = attr->port_num;
1210 resp.timeout = attr->timeout;
1211 resp.retry_cnt = attr->retry_cnt;
1212 resp.rnr_retry = attr->rnr_retry;
1213 resp.alt_port_num = attr->alt_port_num;
1214 resp.alt_timeout = attr->alt_timeout;
1215
1216 memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1217 resp.dest.flow_label = attr->ah_attr.grh.flow_label;
1218 resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
1219 resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
1220 resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
1221 resp.dest.dlid = attr->ah_attr.dlid;
1222 resp.dest.sl = attr->ah_attr.sl;
1223 resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
1224 resp.dest.static_rate = attr->ah_attr.static_rate;
1225 resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1226 resp.dest.port_num = attr->ah_attr.port_num;
1227
1228 memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1229 resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
1230 resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
1231 resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
1232 resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1233 resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
1234 resp.alt_dest.sl = attr->alt_ah_attr.sl;
1235 resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1236 resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
1237 resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1238 resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
1239
1240 resp.max_send_wr = init_attr->cap.max_send_wr;
1241 resp.max_recv_wr = init_attr->cap.max_recv_wr;
1242 resp.max_send_sge = init_attr->cap.max_send_sge;
1243 resp.max_recv_sge = init_attr->cap.max_recv_sge;
1244 resp.max_inline_data = init_attr->cap.max_inline_data;
Dotan Barak27d56302006-03-02 11:25:27 -08001245 resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
Dotan Barak7ccc9a22006-02-13 16:31:25 -08001246
1247 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1248 &resp, sizeof resp))
1249 ret = -EFAULT;
1250
1251out:
1252 kfree(attr);
1253 kfree(init_attr);
1254
1255 return ret ? ret : in_len;
1256}
1257
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001258ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1259 const char __user *buf, int in_len,
1260 int out_len)
1261{
1262 struct ib_uverbs_modify_qp cmd;
Ralph Campbell9bc57e22006-08-11 14:58:09 -07001263 struct ib_udata udata;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001264 struct ib_qp *qp;
1265 struct ib_qp_attr *attr;
1266 int ret;
1267
1268 if (copy_from_user(&cmd, buf, sizeof cmd))
1269 return -EFAULT;
1270
Ralph Campbell9bc57e22006-08-11 14:58:09 -07001271 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1272 out_len);
1273
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001274 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1275 if (!attr)
1276 return -ENOMEM;
1277
Roland Dreier9ead1902006-06-17 20:44:49 -07001278 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1279 if (!qp) {
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001280 ret = -EINVAL;
1281 goto out;
1282 }
1283
1284 attr->qp_state = cmd.qp_state;
1285 attr->cur_qp_state = cmd.cur_qp_state;
1286 attr->path_mtu = cmd.path_mtu;
1287 attr->path_mig_state = cmd.path_mig_state;
1288 attr->qkey = cmd.qkey;
1289 attr->rq_psn = cmd.rq_psn;
1290 attr->sq_psn = cmd.sq_psn;
1291 attr->dest_qp_num = cmd.dest_qp_num;
1292 attr->qp_access_flags = cmd.qp_access_flags;
1293 attr->pkey_index = cmd.pkey_index;
Ami Perlmutter702b2aa2006-03-20 10:08:24 -08001294 attr->alt_pkey_index = cmd.alt_pkey_index;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001295 attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1296 attr->max_rd_atomic = cmd.max_rd_atomic;
1297 attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
1298 attr->min_rnr_timer = cmd.min_rnr_timer;
1299 attr->port_num = cmd.port_num;
1300 attr->timeout = cmd.timeout;
1301 attr->retry_cnt = cmd.retry_cnt;
1302 attr->rnr_retry = cmd.rnr_retry;
1303 attr->alt_port_num = cmd.alt_port_num;
1304 attr->alt_timeout = cmd.alt_timeout;
1305
1306 memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1307 attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
1308 attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
1309 attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
1310 attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
1311 attr->ah_attr.dlid = cmd.dest.dlid;
1312 attr->ah_attr.sl = cmd.dest.sl;
1313 attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
1314 attr->ah_attr.static_rate = cmd.dest.static_rate;
1315 attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
1316 attr->ah_attr.port_num = cmd.dest.port_num;
1317
1318 memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1319 attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
1320 attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
1321 attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
1322 attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1323 attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
1324 attr->alt_ah_attr.sl = cmd.alt_dest.sl;
1325 attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
1326 attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
1327 attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1328 attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
1329
Ralph Campbell9bc57e22006-08-11 14:58:09 -07001330 ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
Roland Dreier9ead1902006-06-17 20:44:49 -07001331
1332 put_qp_read(qp);
1333
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001334 if (ret)
1335 goto out;
1336
1337 ret = in_len;
1338
1339out:
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001340 kfree(attr);
1341
1342 return ret;
1343}
1344
1345ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1346 const char __user *buf, int in_len,
1347 int out_len)
1348{
Roland Dreier63aaf642005-09-09 15:55:08 -07001349 struct ib_uverbs_destroy_qp cmd;
1350 struct ib_uverbs_destroy_qp_resp resp;
Roland Dreier9ead1902006-06-17 20:44:49 -07001351 struct ib_uobject *uobj;
Roland Dreier63aaf642005-09-09 15:55:08 -07001352 struct ib_qp *qp;
Roland Dreier9ead1902006-06-17 20:44:49 -07001353 struct ib_uqp_object *obj;
Roland Dreier63aaf642005-09-09 15:55:08 -07001354 int ret = -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001355
1356 if (copy_from_user(&cmd, buf, sizeof cmd))
1357 return -EFAULT;
1358
Roland Dreier63aaf642005-09-09 15:55:08 -07001359 memset(&resp, 0, sizeof resp);
1360
Roland Dreier9ead1902006-06-17 20:44:49 -07001361 uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1362 if (!uobj)
1363 return -EINVAL;
1364 qp = uobj->object;
1365 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001366
Roland Dreier9ead1902006-06-17 20:44:49 -07001367 if (!list_empty(&obj->mcast_list)) {
1368 put_uobj_write(uobj);
1369 return -EBUSY;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001370 }
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001371
1372 ret = ib_destroy_qp(qp);
Roland Dreier9ead1902006-06-17 20:44:49 -07001373 if (!ret)
1374 uobj->live = 0;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001375
Roland Dreier9ead1902006-06-17 20:44:49 -07001376 put_uobj_write(uobj);
1377
1378 if (ret)
1379 return ret;
1380
1381 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001382
Ingo Molnar95ed6442006-01-13 14:51:39 -08001383 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07001384 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001385 mutex_unlock(&file->mutex);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001386
Roland Dreier9ead1902006-06-17 20:44:49 -07001387 ib_uverbs_release_uevent(file, &obj->uevent);
Roland Dreier63aaf642005-09-09 15:55:08 -07001388
Roland Dreier9ead1902006-06-17 20:44:49 -07001389 resp.events_reported = obj->uevent.events_reported;
Roland Dreier63aaf642005-09-09 15:55:08 -07001390
Roland Dreier9ead1902006-06-17 20:44:49 -07001391 put_uobj(uobj);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001392
Roland Dreier63aaf642005-09-09 15:55:08 -07001393 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1394 &resp, sizeof resp))
Roland Dreier9ead1902006-06-17 20:44:49 -07001395 return -EFAULT;
Roland Dreier63aaf642005-09-09 15:55:08 -07001396
Roland Dreier9ead1902006-06-17 20:44:49 -07001397 return in_len;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001398}
1399
Roland Dreier67cdb402005-10-14 15:26:04 -07001400ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
Roland Dreiera74cd4a2006-02-13 16:30:49 -08001401 const char __user *buf, int in_len,
1402 int out_len)
Roland Dreier67cdb402005-10-14 15:26:04 -07001403{
1404 struct ib_uverbs_post_send cmd;
1405 struct ib_uverbs_post_send_resp resp;
1406 struct ib_uverbs_send_wr *user_wr;
1407 struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
1408 struct ib_qp *qp;
1409 int i, sg_ind;
Roland Dreier9ead1902006-06-17 20:44:49 -07001410 int is_ud;
Roland Dreier67cdb402005-10-14 15:26:04 -07001411 ssize_t ret = -EINVAL;
1412
1413 if (copy_from_user(&cmd, buf, sizeof cmd))
1414 return -EFAULT;
1415
1416 if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1417 cmd.sge_count * sizeof (struct ib_uverbs_sge))
1418 return -EINVAL;
1419
1420 if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1421 return -EINVAL;
1422
1423 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1424 if (!user_wr)
1425 return -ENOMEM;
1426
Roland Dreier9ead1902006-06-17 20:44:49 -07001427 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1428 if (!qp)
Roland Dreier67cdb402005-10-14 15:26:04 -07001429 goto out;
1430
Roland Dreier9ead1902006-06-17 20:44:49 -07001431 is_ud = qp->qp_type == IB_QPT_UD;
Roland Dreier67cdb402005-10-14 15:26:04 -07001432 sg_ind = 0;
1433 last = NULL;
1434 for (i = 0; i < cmd.wr_count; ++i) {
1435 if (copy_from_user(user_wr,
1436 buf + sizeof cmd + i * cmd.wqe_size,
1437 cmd.wqe_size)) {
1438 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07001439 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07001440 }
1441
1442 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1443 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001444 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07001445 }
1446
1447 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1448 user_wr->num_sge * sizeof (struct ib_sge),
1449 GFP_KERNEL);
1450 if (!next) {
1451 ret = -ENOMEM;
Roland Dreier9ead1902006-06-17 20:44:49 -07001452 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07001453 }
1454
1455 if (!last)
1456 wr = next;
1457 else
1458 last->next = next;
1459 last = next;
1460
1461 next->next = NULL;
1462 next->wr_id = user_wr->wr_id;
1463 next->num_sge = user_wr->num_sge;
1464 next->opcode = user_wr->opcode;
1465 next->send_flags = user_wr->send_flags;
Jack Morgenstein77369ed2005-11-09 11:26:07 -08001466 next->imm_data = (__be32 __force) user_wr->imm_data;
Roland Dreier67cdb402005-10-14 15:26:04 -07001467
Roland Dreier9ead1902006-06-17 20:44:49 -07001468 if (is_ud) {
1469 next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1470 file->ucontext);
Roland Dreier67cdb402005-10-14 15:26:04 -07001471 if (!next->wr.ud.ah) {
1472 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001473 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07001474 }
1475 next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
1476 next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1477 } else {
1478 switch (next->opcode) {
1479 case IB_WR_RDMA_WRITE:
1480 case IB_WR_RDMA_WRITE_WITH_IMM:
1481 case IB_WR_RDMA_READ:
1482 next->wr.rdma.remote_addr =
1483 user_wr->wr.rdma.remote_addr;
1484 next->wr.rdma.rkey =
1485 user_wr->wr.rdma.rkey;
1486 break;
1487 case IB_WR_ATOMIC_CMP_AND_SWP:
1488 case IB_WR_ATOMIC_FETCH_AND_ADD:
1489 next->wr.atomic.remote_addr =
1490 user_wr->wr.atomic.remote_addr;
1491 next->wr.atomic.compare_add =
1492 user_wr->wr.atomic.compare_add;
1493 next->wr.atomic.swap = user_wr->wr.atomic.swap;
1494 next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1495 break;
1496 default:
1497 break;
1498 }
1499 }
1500
1501 if (next->num_sge) {
1502 next->sg_list = (void *) next +
1503 ALIGN(sizeof *next, sizeof (struct ib_sge));
1504 if (copy_from_user(next->sg_list,
1505 buf + sizeof cmd +
1506 cmd.wr_count * cmd.wqe_size +
1507 sg_ind * sizeof (struct ib_sge),
1508 next->num_sge * sizeof (struct ib_sge))) {
1509 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07001510 goto out_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07001511 }
1512 sg_ind += next->num_sge;
1513 } else
1514 next->sg_list = NULL;
1515 }
1516
1517 resp.bad_wr = 0;
1518 ret = qp->device->post_send(qp, wr, &bad_wr);
1519 if (ret)
1520 for (next = wr; next; next = next->next) {
1521 ++resp.bad_wr;
1522 if (next == bad_wr)
1523 break;
1524 }
1525
1526 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1527 &resp, sizeof resp))
1528 ret = -EFAULT;
1529
Roland Dreier9ead1902006-06-17 20:44:49 -07001530out_put:
1531 put_qp_read(qp);
Roland Dreier67cdb402005-10-14 15:26:04 -07001532
1533 while (wr) {
Roland Dreier9ead1902006-06-17 20:44:49 -07001534 if (is_ud && wr->wr.ud.ah)
1535 put_ah_read(wr->wr.ud.ah);
Roland Dreier67cdb402005-10-14 15:26:04 -07001536 next = wr->next;
1537 kfree(wr);
1538 wr = next;
1539 }
1540
Krishna Kumar18320822006-06-22 07:47:27 -07001541out:
Roland Dreier67cdb402005-10-14 15:26:04 -07001542 kfree(user_wr);
1543
1544 return ret ? ret : in_len;
1545}
1546
1547static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1548 int in_len,
1549 u32 wr_count,
1550 u32 sge_count,
1551 u32 wqe_size)
1552{
1553 struct ib_uverbs_recv_wr *user_wr;
1554 struct ib_recv_wr *wr = NULL, *last, *next;
1555 int sg_ind;
1556 int i;
1557 int ret;
1558
1559 if (in_len < wqe_size * wr_count +
1560 sge_count * sizeof (struct ib_uverbs_sge))
1561 return ERR_PTR(-EINVAL);
1562
1563 if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1564 return ERR_PTR(-EINVAL);
1565
1566 user_wr = kmalloc(wqe_size, GFP_KERNEL);
1567 if (!user_wr)
1568 return ERR_PTR(-ENOMEM);
1569
1570 sg_ind = 0;
1571 last = NULL;
1572 for (i = 0; i < wr_count; ++i) {
1573 if (copy_from_user(user_wr, buf + i * wqe_size,
1574 wqe_size)) {
1575 ret = -EFAULT;
1576 goto err;
1577 }
1578
1579 if (user_wr->num_sge + sg_ind > sge_count) {
1580 ret = -EINVAL;
1581 goto err;
1582 }
1583
1584 next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1585 user_wr->num_sge * sizeof (struct ib_sge),
1586 GFP_KERNEL);
1587 if (!next) {
1588 ret = -ENOMEM;
1589 goto err;
1590 }
1591
1592 if (!last)
1593 wr = next;
1594 else
1595 last->next = next;
1596 last = next;
1597
1598 next->next = NULL;
1599 next->wr_id = user_wr->wr_id;
1600 next->num_sge = user_wr->num_sge;
1601
1602 if (next->num_sge) {
1603 next->sg_list = (void *) next +
1604 ALIGN(sizeof *next, sizeof (struct ib_sge));
1605 if (copy_from_user(next->sg_list,
1606 buf + wr_count * wqe_size +
1607 sg_ind * sizeof (struct ib_sge),
1608 next->num_sge * sizeof (struct ib_sge))) {
1609 ret = -EFAULT;
1610 goto err;
1611 }
1612 sg_ind += next->num_sge;
1613 } else
1614 next->sg_list = NULL;
1615 }
1616
1617 kfree(user_wr);
1618 return wr;
1619
1620err:
1621 kfree(user_wr);
1622
1623 while (wr) {
1624 next = wr->next;
1625 kfree(wr);
1626 wr = next;
1627 }
1628
1629 return ERR_PTR(ret);
1630}
1631
1632ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
Roland Dreiera74cd4a2006-02-13 16:30:49 -08001633 const char __user *buf, int in_len,
1634 int out_len)
Roland Dreier67cdb402005-10-14 15:26:04 -07001635{
1636 struct ib_uverbs_post_recv cmd;
1637 struct ib_uverbs_post_recv_resp resp;
1638 struct ib_recv_wr *wr, *next, *bad_wr;
1639 struct ib_qp *qp;
1640 ssize_t ret = -EINVAL;
1641
1642 if (copy_from_user(&cmd, buf, sizeof cmd))
1643 return -EFAULT;
1644
1645 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1646 in_len - sizeof cmd, cmd.wr_count,
1647 cmd.sge_count, cmd.wqe_size);
1648 if (IS_ERR(wr))
1649 return PTR_ERR(wr);
1650
Roland Dreier9ead1902006-06-17 20:44:49 -07001651 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1652 if (!qp)
Roland Dreier67cdb402005-10-14 15:26:04 -07001653 goto out;
1654
1655 resp.bad_wr = 0;
1656 ret = qp->device->post_recv(qp, wr, &bad_wr);
Roland Dreier9ead1902006-06-17 20:44:49 -07001657
1658 put_qp_read(qp);
1659
Roland Dreier67cdb402005-10-14 15:26:04 -07001660 if (ret)
1661 for (next = wr; next; next = next->next) {
1662 ++resp.bad_wr;
1663 if (next == bad_wr)
1664 break;
1665 }
1666
Roland Dreier67cdb402005-10-14 15:26:04 -07001667 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1668 &resp, sizeof resp))
1669 ret = -EFAULT;
1670
1671out:
Roland Dreier67cdb402005-10-14 15:26:04 -07001672 while (wr) {
1673 next = wr->next;
1674 kfree(wr);
1675 wr = next;
1676 }
1677
1678 return ret ? ret : in_len;
1679}
1680
1681ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
Roland Dreiera74cd4a2006-02-13 16:30:49 -08001682 const char __user *buf, int in_len,
1683 int out_len)
Roland Dreier67cdb402005-10-14 15:26:04 -07001684{
1685 struct ib_uverbs_post_srq_recv cmd;
1686 struct ib_uverbs_post_srq_recv_resp resp;
1687 struct ib_recv_wr *wr, *next, *bad_wr;
1688 struct ib_srq *srq;
1689 ssize_t ret = -EINVAL;
1690
1691 if (copy_from_user(&cmd, buf, sizeof cmd))
1692 return -EFAULT;
1693
1694 wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1695 in_len - sizeof cmd, cmd.wr_count,
1696 cmd.sge_count, cmd.wqe_size);
1697 if (IS_ERR(wr))
1698 return PTR_ERR(wr);
1699
Roland Dreier9ead1902006-06-17 20:44:49 -07001700 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1701 if (!srq)
Roland Dreier67cdb402005-10-14 15:26:04 -07001702 goto out;
1703
1704 resp.bad_wr = 0;
1705 ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
Roland Dreier9ead1902006-06-17 20:44:49 -07001706
1707 put_srq_read(srq);
1708
Roland Dreier67cdb402005-10-14 15:26:04 -07001709 if (ret)
1710 for (next = wr; next; next = next->next) {
1711 ++resp.bad_wr;
1712 if (next == bad_wr)
1713 break;
1714 }
1715
Roland Dreier67cdb402005-10-14 15:26:04 -07001716 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1717 &resp, sizeof resp))
1718 ret = -EFAULT;
1719
1720out:
Roland Dreier67cdb402005-10-14 15:26:04 -07001721 while (wr) {
1722 next = wr->next;
1723 kfree(wr);
1724 wr = next;
1725 }
1726
1727 return ret ? ret : in_len;
1728}
1729
1730ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1731 const char __user *buf, int in_len,
1732 int out_len)
1733{
1734 struct ib_uverbs_create_ah cmd;
1735 struct ib_uverbs_create_ah_resp resp;
1736 struct ib_uobject *uobj;
1737 struct ib_pd *pd;
1738 struct ib_ah *ah;
1739 struct ib_ah_attr attr;
1740 int ret;
1741
1742 if (out_len < sizeof resp)
1743 return -ENOSPC;
1744
1745 if (copy_from_user(&cmd, buf, sizeof cmd))
1746 return -EFAULT;
1747
1748 uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1749 if (!uobj)
1750 return -ENOMEM;
1751
Roland Dreier43db2bc2006-07-23 15:16:04 -07001752 init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
Roland Dreier9ead1902006-06-17 20:44:49 -07001753 down_write(&uobj->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07001754
Roland Dreier9ead1902006-06-17 20:44:49 -07001755 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1756 if (!pd) {
Roland Dreier67cdb402005-10-14 15:26:04 -07001757 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001758 goto err;
Roland Dreier67cdb402005-10-14 15:26:04 -07001759 }
1760
Roland Dreier67cdb402005-10-14 15:26:04 -07001761 attr.dlid = cmd.attr.dlid;
1762 attr.sl = cmd.attr.sl;
1763 attr.src_path_bits = cmd.attr.src_path_bits;
1764 attr.static_rate = cmd.attr.static_rate;
Ralph Campbellea5d4a62006-01-06 16:24:45 -08001765 attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
Roland Dreier67cdb402005-10-14 15:26:04 -07001766 attr.port_num = cmd.attr.port_num;
1767 attr.grh.flow_label = cmd.attr.grh.flow_label;
1768 attr.grh.sgid_index = cmd.attr.grh.sgid_index;
1769 attr.grh.hop_limit = cmd.attr.grh.hop_limit;
1770 attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1771 memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
1772
1773 ah = ib_create_ah(pd, &attr);
1774 if (IS_ERR(ah)) {
1775 ret = PTR_ERR(ah);
Michael S. Tsirkinec924b42006-07-17 18:20:51 +03001776 goto err_put;
Roland Dreier67cdb402005-10-14 15:26:04 -07001777 }
1778
Roland Dreier9ead1902006-06-17 20:44:49 -07001779 ah->uobject = uobj;
1780 uobj->object = ah;
Roland Dreier67cdb402005-10-14 15:26:04 -07001781
Roland Dreier9ead1902006-06-17 20:44:49 -07001782 ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07001783 if (ret)
1784 goto err_destroy;
1785
1786 resp.ah_handle = uobj->id;
1787
1788 if (copy_to_user((void __user *) (unsigned long) cmd.response,
1789 &resp, sizeof resp)) {
1790 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07001791 goto err_copy;
Roland Dreier67cdb402005-10-14 15:26:04 -07001792 }
1793
Roland Dreier9ead1902006-06-17 20:44:49 -07001794 put_pd_read(pd);
1795
Ingo Molnar95ed6442006-01-13 14:51:39 -08001796 mutex_lock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07001797 list_add_tail(&uobj->list, &file->ucontext->ah_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001798 mutex_unlock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07001799
Roland Dreier9ead1902006-06-17 20:44:49 -07001800 uobj->live = 1;
1801
1802 up_write(&uobj->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07001803
1804 return in_len;
1805
Roland Dreier9ead1902006-06-17 20:44:49 -07001806err_copy:
1807 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07001808
1809err_destroy:
1810 ib_destroy_ah(ah);
1811
Michael S. Tsirkinec924b42006-07-17 18:20:51 +03001812err_put:
1813 put_pd_read(pd);
1814
Roland Dreier9ead1902006-06-17 20:44:49 -07001815err:
1816 put_uobj_write(uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07001817 return ret;
1818}
1819
1820ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1821 const char __user *buf, int in_len, int out_len)
1822{
1823 struct ib_uverbs_destroy_ah cmd;
1824 struct ib_ah *ah;
1825 struct ib_uobject *uobj;
Roland Dreier9ead1902006-06-17 20:44:49 -07001826 int ret;
Roland Dreier67cdb402005-10-14 15:26:04 -07001827
1828 if (copy_from_user(&cmd, buf, sizeof cmd))
1829 return -EFAULT;
1830
Roland Dreier9ead1902006-06-17 20:44:49 -07001831 uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
1832 if (!uobj)
1833 return -EINVAL;
1834 ah = uobj->object;
Roland Dreier67cdb402005-10-14 15:26:04 -07001835
1836 ret = ib_destroy_ah(ah);
Roland Dreier9ead1902006-06-17 20:44:49 -07001837 if (!ret)
1838 uobj->live = 0;
Roland Dreier67cdb402005-10-14 15:26:04 -07001839
Roland Dreier9ead1902006-06-17 20:44:49 -07001840 put_uobj_write(uobj);
1841
1842 if (ret)
1843 return ret;
1844
1845 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07001846
Ingo Molnar95ed6442006-01-13 14:51:39 -08001847 mutex_lock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07001848 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001849 mutex_unlock(&file->mutex);
Roland Dreier67cdb402005-10-14 15:26:04 -07001850
Roland Dreier9ead1902006-06-17 20:44:49 -07001851 put_uobj(uobj);
Roland Dreier67cdb402005-10-14 15:26:04 -07001852
Roland Dreier9ead1902006-06-17 20:44:49 -07001853 return in_len;
Roland Dreier67cdb402005-10-14 15:26:04 -07001854}
1855
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001856ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1857 const char __user *buf, int in_len,
1858 int out_len)
1859{
1860 struct ib_uverbs_attach_mcast cmd;
1861 struct ib_qp *qp;
Roland Dreier9ead1902006-06-17 20:44:49 -07001862 struct ib_uqp_object *obj;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001863 struct ib_uverbs_mcast_entry *mcast;
Roland Dreier9ead1902006-06-17 20:44:49 -07001864 int ret;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001865
1866 if (copy_from_user(&cmd, buf, sizeof cmd))
1867 return -EFAULT;
1868
Roland Dreier9ead1902006-06-17 20:44:49 -07001869 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1870 if (!qp)
1871 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001872
Roland Dreier9ead1902006-06-17 20:44:49 -07001873 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001874
Roland Dreier9ead1902006-06-17 20:44:49 -07001875 list_for_each_entry(mcast, &obj->mcast_list, list)
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001876 if (cmd.mlid == mcast->lid &&
1877 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1878 ret = 0;
Roland Dreier9ead1902006-06-17 20:44:49 -07001879 goto out_put;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001880 }
1881
1882 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1883 if (!mcast) {
1884 ret = -ENOMEM;
Roland Dreier9ead1902006-06-17 20:44:49 -07001885 goto out_put;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001886 }
1887
1888 mcast->lid = cmd.mlid;
1889 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1890
1891 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
Roland Dreier9ead1902006-06-17 20:44:49 -07001892 if (!ret)
1893 list_add_tail(&mcast->list, &obj->mcast_list);
1894 else
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001895 kfree(mcast);
1896
Roland Dreier9ead1902006-06-17 20:44:49 -07001897out_put:
1898 put_qp_read(qp);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001899
1900 return ret ? ret : in_len;
1901}
1902
1903ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1904 const char __user *buf, int in_len,
1905 int out_len)
1906{
1907 struct ib_uverbs_detach_mcast cmd;
Roland Dreier9ead1902006-06-17 20:44:49 -07001908 struct ib_uqp_object *obj;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001909 struct ib_qp *qp;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001910 struct ib_uverbs_mcast_entry *mcast;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001911 int ret = -EINVAL;
1912
1913 if (copy_from_user(&cmd, buf, sizeof cmd))
1914 return -EFAULT;
1915
Roland Dreier9ead1902006-06-17 20:44:49 -07001916 qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1917 if (!qp)
1918 return -EINVAL;
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001919
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001920 ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1921 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -07001922 goto out_put;
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001923
Roland Dreier9ead1902006-06-17 20:44:49 -07001924 obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001925
Roland Dreier9ead1902006-06-17 20:44:49 -07001926 list_for_each_entry(mcast, &obj->mcast_list, list)
Jack Morgensteinf4e40152005-11-29 16:57:01 -08001927 if (cmd.mlid == mcast->lid &&
1928 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1929 list_del(&mcast->list);
1930 kfree(mcast);
1931 break;
1932 }
1933
Roland Dreier9ead1902006-06-17 20:44:49 -07001934out_put:
1935 put_qp_read(qp);
Roland Dreierbc38a6a2005-07-07 17:57:13 -07001936
1937 return ret ? ret : in_len;
1938}
Roland Dreierf520ba52005-08-18 12:24:13 -07001939
1940ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1941 const char __user *buf, int in_len,
1942 int out_len)
1943{
1944 struct ib_uverbs_create_srq cmd;
1945 struct ib_uverbs_create_srq_resp resp;
1946 struct ib_udata udata;
Roland Dreier9ead1902006-06-17 20:44:49 -07001947 struct ib_uevent_object *obj;
Roland Dreierf520ba52005-08-18 12:24:13 -07001948 struct ib_pd *pd;
1949 struct ib_srq *srq;
1950 struct ib_srq_init_attr attr;
1951 int ret;
1952
1953 if (out_len < sizeof resp)
1954 return -ENOSPC;
1955
1956 if (copy_from_user(&cmd, buf, sizeof cmd))
1957 return -EFAULT;
1958
1959 INIT_UDATA(&udata, buf + sizeof cmd,
1960 (unsigned long) cmd.response + sizeof resp,
1961 in_len - sizeof cmd, out_len - sizeof resp);
1962
Roland Dreier9ead1902006-06-17 20:44:49 -07001963 obj = kmalloc(sizeof *obj, GFP_KERNEL);
1964 if (!obj)
Roland Dreierf520ba52005-08-18 12:24:13 -07001965 return -ENOMEM;
1966
Roland Dreier43db2bc2006-07-23 15:16:04 -07001967 init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
Roland Dreier9ead1902006-06-17 20:44:49 -07001968 down_write(&obj->uobject.mutex);
Roland Dreierf520ba52005-08-18 12:24:13 -07001969
Roland Dreier9ead1902006-06-17 20:44:49 -07001970 pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1971 if (!pd) {
Roland Dreierf520ba52005-08-18 12:24:13 -07001972 ret = -EINVAL;
Roland Dreier9ead1902006-06-17 20:44:49 -07001973 goto err;
Roland Dreierf520ba52005-08-18 12:24:13 -07001974 }
1975
1976 attr.event_handler = ib_uverbs_srq_event_handler;
1977 attr.srq_context = file;
1978 attr.attr.max_wr = cmd.max_wr;
1979 attr.attr.max_sge = cmd.max_sge;
1980 attr.attr.srq_limit = cmd.srq_limit;
1981
Roland Dreier9ead1902006-06-17 20:44:49 -07001982 obj->events_reported = 0;
1983 INIT_LIST_HEAD(&obj->event_list);
Roland Dreierf520ba52005-08-18 12:24:13 -07001984
1985 srq = pd->device->create_srq(pd, &attr, &udata);
1986 if (IS_ERR(srq)) {
1987 ret = PTR_ERR(srq);
Michael S. Tsirkinec924b42006-07-17 18:20:51 +03001988 goto err_put;
Roland Dreierf520ba52005-08-18 12:24:13 -07001989 }
1990
1991 srq->device = pd->device;
1992 srq->pd = pd;
Roland Dreier9ead1902006-06-17 20:44:49 -07001993 srq->uobject = &obj->uobject;
Roland Dreierf520ba52005-08-18 12:24:13 -07001994 srq->event_handler = attr.event_handler;
1995 srq->srq_context = attr.srq_context;
1996 atomic_inc(&pd->usecnt);
1997 atomic_set(&srq->usecnt, 0);
1998
Roland Dreier9ead1902006-06-17 20:44:49 -07001999 obj->uobject.object = srq;
2000 ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
Roland Dreierf520ba52005-08-18 12:24:13 -07002001 if (ret)
2002 goto err_destroy;
2003
Roland Dreier9ead1902006-06-17 20:44:49 -07002004 memset(&resp, 0, sizeof resp);
2005 resp.srq_handle = obj->uobject.id;
Dotan Barakea88fd12006-02-23 12:36:18 -08002006 resp.max_wr = attr.attr.max_wr;
2007 resp.max_sge = attr.attr.max_sge;
Roland Dreierf520ba52005-08-18 12:24:13 -07002008
Roland Dreierf520ba52005-08-18 12:24:13 -07002009 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2010 &resp, sizeof resp)) {
2011 ret = -EFAULT;
Roland Dreier9ead1902006-06-17 20:44:49 -07002012 goto err_copy;
Roland Dreierf520ba52005-08-18 12:24:13 -07002013 }
2014
Roland Dreier9ead1902006-06-17 20:44:49 -07002015 put_pd_read(pd);
2016
Ingo Molnar95ed6442006-01-13 14:51:39 -08002017 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07002018 list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08002019 mutex_unlock(&file->mutex);
Roland Dreiereb9d3cd2005-09-27 15:07:25 -07002020
Roland Dreier9ead1902006-06-17 20:44:49 -07002021 obj->uobject.live = 1;
2022
2023 up_write(&obj->uobject.mutex);
Roland Dreierf520ba52005-08-18 12:24:13 -07002024
2025 return in_len;
2026
Roland Dreier9ead1902006-06-17 20:44:49 -07002027err_copy:
2028 idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
Roland Dreierf520ba52005-08-18 12:24:13 -07002029
2030err_destroy:
2031 ib_destroy_srq(srq);
2032
Michael S. Tsirkinec924b42006-07-17 18:20:51 +03002033err_put:
2034 put_pd_read(pd);
2035
Roland Dreier9ead1902006-06-17 20:44:49 -07002036err:
2037 put_uobj_write(&obj->uobject);
Roland Dreierf520ba52005-08-18 12:24:13 -07002038 return ret;
2039}
2040
2041ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2042 const char __user *buf, int in_len,
2043 int out_len)
2044{
2045 struct ib_uverbs_modify_srq cmd;
Ralph Campbell9bc57e22006-08-11 14:58:09 -07002046 struct ib_udata udata;
Roland Dreierf520ba52005-08-18 12:24:13 -07002047 struct ib_srq *srq;
2048 struct ib_srq_attr attr;
2049 int ret;
2050
2051 if (copy_from_user(&cmd, buf, sizeof cmd))
2052 return -EFAULT;
2053
Ralph Campbell9bc57e22006-08-11 14:58:09 -07002054 INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2055 out_len);
2056
Roland Dreier9ead1902006-06-17 20:44:49 -07002057 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2058 if (!srq)
2059 return -EINVAL;
Roland Dreierf520ba52005-08-18 12:24:13 -07002060
2061 attr.max_wr = cmd.max_wr;
Roland Dreierf520ba52005-08-18 12:24:13 -07002062 attr.srq_limit = cmd.srq_limit;
2063
Ralph Campbell9bc57e22006-08-11 14:58:09 -07002064 ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
Roland Dreierf520ba52005-08-18 12:24:13 -07002065
Roland Dreier9ead1902006-06-17 20:44:49 -07002066 put_srq_read(srq);
Roland Dreierf520ba52005-08-18 12:24:13 -07002067
2068 return ret ? ret : in_len;
2069}
2070
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002071ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2072 const char __user *buf,
2073 int in_len, int out_len)
2074{
2075 struct ib_uverbs_query_srq cmd;
2076 struct ib_uverbs_query_srq_resp resp;
2077 struct ib_srq_attr attr;
2078 struct ib_srq *srq;
2079 int ret;
2080
2081 if (out_len < sizeof resp)
2082 return -ENOSPC;
2083
2084 if (copy_from_user(&cmd, buf, sizeof cmd))
2085 return -EFAULT;
2086
Roland Dreier9ead1902006-06-17 20:44:49 -07002087 srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2088 if (!srq)
2089 return -EINVAL;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002090
Roland Dreier9ead1902006-06-17 20:44:49 -07002091 ret = ib_query_srq(srq, &attr);
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002092
Roland Dreier9ead1902006-06-17 20:44:49 -07002093 put_srq_read(srq);
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002094
2095 if (ret)
Roland Dreier9ead1902006-06-17 20:44:49 -07002096 return ret;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002097
2098 memset(&resp, 0, sizeof resp);
2099
2100 resp.max_wr = attr.max_wr;
2101 resp.max_sge = attr.max_sge;
2102 resp.srq_limit = attr.srq_limit;
2103
2104 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2105 &resp, sizeof resp))
Roland Dreier9ead1902006-06-17 20:44:49 -07002106 return -EFAULT;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002107
Roland Dreier9ead1902006-06-17 20:44:49 -07002108 return in_len;
Dotan Barak8bdb0e82006-02-13 16:31:57 -08002109}
2110
Roland Dreierf520ba52005-08-18 12:24:13 -07002111ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2112 const char __user *buf, int in_len,
2113 int out_len)
2114{
Roland Dreier63aaf642005-09-09 15:55:08 -07002115 struct ib_uverbs_destroy_srq cmd;
2116 struct ib_uverbs_destroy_srq_resp resp;
Roland Dreier9ead1902006-06-17 20:44:49 -07002117 struct ib_uobject *uobj;
Roland Dreier63aaf642005-09-09 15:55:08 -07002118 struct ib_srq *srq;
Roland Dreier9ead1902006-06-17 20:44:49 -07002119 struct ib_uevent_object *obj;
Roland Dreier63aaf642005-09-09 15:55:08 -07002120 int ret = -EINVAL;
Roland Dreierf520ba52005-08-18 12:24:13 -07002121
2122 if (copy_from_user(&cmd, buf, sizeof cmd))
2123 return -EFAULT;
2124
Roland Dreier9ead1902006-06-17 20:44:49 -07002125 uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2126 if (!uobj)
2127 return -EINVAL;
2128 srq = uobj->object;
2129 obj = container_of(uobj, struct ib_uevent_object, uobject);
Roland Dreierf520ba52005-08-18 12:24:13 -07002130
2131 ret = ib_destroy_srq(srq);
Roland Dreier9ead1902006-06-17 20:44:49 -07002132 if (!ret)
2133 uobj->live = 0;
Roland Dreierf520ba52005-08-18 12:24:13 -07002134
Roland Dreier9ead1902006-06-17 20:44:49 -07002135 put_uobj_write(uobj);
2136
2137 if (ret)
2138 return ret;
2139
2140 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
Roland Dreierf520ba52005-08-18 12:24:13 -07002141
Ingo Molnar95ed6442006-01-13 14:51:39 -08002142 mutex_lock(&file->mutex);
Roland Dreier9ead1902006-06-17 20:44:49 -07002143 list_del(&uobj->list);
Ingo Molnar95ed6442006-01-13 14:51:39 -08002144 mutex_unlock(&file->mutex);
Roland Dreierf520ba52005-08-18 12:24:13 -07002145
Roland Dreier9ead1902006-06-17 20:44:49 -07002146 ib_uverbs_release_uevent(file, obj);
Roland Dreier63aaf642005-09-09 15:55:08 -07002147
Roland Dreier9ead1902006-06-17 20:44:49 -07002148 memset(&resp, 0, sizeof resp);
2149 resp.events_reported = obj->events_reported;
Roland Dreier63aaf642005-09-09 15:55:08 -07002150
Roland Dreier9ead1902006-06-17 20:44:49 -07002151 put_uobj(uobj);
Roland Dreierf520ba52005-08-18 12:24:13 -07002152
Roland Dreier63aaf642005-09-09 15:55:08 -07002153 if (copy_to_user((void __user *) (unsigned long) cmd.response,
2154 &resp, sizeof resp))
2155 ret = -EFAULT;
2156
Roland Dreierf520ba52005-08-18 12:24:13 -07002157 return ret ? ret : in_len;
2158}