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