Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005-2006 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #include <linux/completion.h> |
Sean Hefty | 88314e4 | 2007-11-14 00:29:50 -0800 | [diff] [blame] | 34 | #include <linux/file.h> |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 35 | #include <linux/mutex.h> |
| 36 | #include <linux/poll.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 37 | #include <linux/sched.h> |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 38 | #include <linux/idr.h> |
| 39 | #include <linux/in.h> |
| 40 | #include <linux/in6.h> |
| 41 | #include <linux/miscdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Steve Wise | 97cb7e4 | 2010-08-13 20:56:34 +0000 | [diff] [blame] | 43 | #include <linux/sysctl.h> |
Paul Gortmaker | e4dd23d | 2011-05-27 15:35:46 -0400 | [diff] [blame] | 44 | #include <linux/module.h> |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 45 | |
| 46 | #include <rdma/rdma_user_cm.h> |
| 47 | #include <rdma/ib_marshall.h> |
| 48 | #include <rdma/rdma_cm.h> |
Sean Hefty | a7ca1f0 | 2009-11-16 09:30:33 -0800 | [diff] [blame] | 49 | #include <rdma/rdma_cm_ib.h> |
Sean Hefty | ee7aed4 | 2013-05-29 10:09:25 -0700 | [diff] [blame] | 50 | #include <rdma/ib_addr.h> |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 51 | |
| 52 | MODULE_AUTHOR("Sean Hefty"); |
| 53 | MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access"); |
| 54 | MODULE_LICENSE("Dual BSD/GPL"); |
| 55 | |
Steve Wise | 97cb7e4 | 2010-08-13 20:56:34 +0000 | [diff] [blame] | 56 | static unsigned int max_backlog = 1024; |
| 57 | |
| 58 | static struct ctl_table_header *ucma_ctl_table_hdr; |
| 59 | static ctl_table ucma_ctl_table[] = { |
| 60 | { |
| 61 | .procname = "max_backlog", |
| 62 | .data = &max_backlog, |
| 63 | .maxlen = sizeof max_backlog, |
| 64 | .mode = 0644, |
| 65 | .proc_handler = proc_dointvec, |
| 66 | }, |
| 67 | { } |
| 68 | }; |
| 69 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 70 | struct ucma_file { |
| 71 | struct mutex mut; |
| 72 | struct file *filp; |
| 73 | struct list_head ctx_list; |
| 74 | struct list_head event_list; |
| 75 | wait_queue_head_t poll_wait; |
| 76 | }; |
| 77 | |
| 78 | struct ucma_context { |
| 79 | int id; |
| 80 | struct completion comp; |
| 81 | atomic_t ref; |
| 82 | int events_reported; |
| 83 | int backlog; |
| 84 | |
| 85 | struct ucma_file *file; |
| 86 | struct rdma_cm_id *cm_id; |
| 87 | u64 uid; |
| 88 | |
| 89 | struct list_head list; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 90 | struct list_head mc_list; |
| 91 | }; |
| 92 | |
| 93 | struct ucma_multicast { |
| 94 | struct ucma_context *ctx; |
| 95 | int id; |
| 96 | int events_reported; |
| 97 | |
| 98 | u64 uid; |
| 99 | struct list_head list; |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 100 | struct sockaddr_storage addr; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct ucma_event { |
| 104 | struct ucma_context *ctx; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 105 | struct ucma_multicast *mc; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 106 | struct list_head list; |
| 107 | struct rdma_cm_id *cm_id; |
| 108 | struct rdma_ucm_event_resp resp; |
| 109 | }; |
| 110 | |
| 111 | static DEFINE_MUTEX(mut); |
| 112 | static DEFINE_IDR(ctx_idr); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 113 | static DEFINE_IDR(multicast_idr); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 114 | |
| 115 | static inline struct ucma_context *_ucma_find_context(int id, |
| 116 | struct ucma_file *file) |
| 117 | { |
| 118 | struct ucma_context *ctx; |
| 119 | |
| 120 | ctx = idr_find(&ctx_idr, id); |
| 121 | if (!ctx) |
| 122 | ctx = ERR_PTR(-ENOENT); |
| 123 | else if (ctx->file != file) |
| 124 | ctx = ERR_PTR(-EINVAL); |
| 125 | return ctx; |
| 126 | } |
| 127 | |
| 128 | static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id) |
| 129 | { |
| 130 | struct ucma_context *ctx; |
| 131 | |
| 132 | mutex_lock(&mut); |
| 133 | ctx = _ucma_find_context(id, file); |
| 134 | if (!IS_ERR(ctx)) |
| 135 | atomic_inc(&ctx->ref); |
| 136 | mutex_unlock(&mut); |
| 137 | return ctx; |
| 138 | } |
| 139 | |
| 140 | static void ucma_put_ctx(struct ucma_context *ctx) |
| 141 | { |
| 142 | if (atomic_dec_and_test(&ctx->ref)) |
| 143 | complete(&ctx->comp); |
| 144 | } |
| 145 | |
| 146 | static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file) |
| 147 | { |
| 148 | struct ucma_context *ctx; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 149 | |
| 150 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 151 | if (!ctx) |
| 152 | return NULL; |
| 153 | |
| 154 | atomic_set(&ctx->ref, 1); |
| 155 | init_completion(&ctx->comp); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 156 | INIT_LIST_HEAD(&ctx->mc_list); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 157 | ctx->file = file; |
| 158 | |
Tejun Heo | 3b069c5 | 2013-02-27 17:04:16 -0800 | [diff] [blame] | 159 | mutex_lock(&mut); |
| 160 | ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL); |
| 161 | mutex_unlock(&mut); |
| 162 | if (ctx->id < 0) |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 163 | goto error; |
| 164 | |
| 165 | list_add_tail(&ctx->list, &file->ctx_list); |
| 166 | return ctx; |
| 167 | |
| 168 | error: |
| 169 | kfree(ctx); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 173 | static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx) |
| 174 | { |
| 175 | struct ucma_multicast *mc; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 176 | |
| 177 | mc = kzalloc(sizeof(*mc), GFP_KERNEL); |
| 178 | if (!mc) |
| 179 | return NULL; |
| 180 | |
Tejun Heo | 3b069c5 | 2013-02-27 17:04:16 -0800 | [diff] [blame] | 181 | mutex_lock(&mut); |
| 182 | mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL); |
| 183 | mutex_unlock(&mut); |
| 184 | if (mc->id < 0) |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 185 | goto error; |
| 186 | |
| 187 | mc->ctx = ctx; |
| 188 | list_add_tail(&mc->list, &ctx->mc_list); |
| 189 | return mc; |
| 190 | |
| 191 | error: |
| 192 | kfree(mc); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 196 | static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst, |
| 197 | struct rdma_conn_param *src) |
| 198 | { |
| 199 | if (src->private_data_len) |
| 200 | memcpy(dst->private_data, src->private_data, |
| 201 | src->private_data_len); |
| 202 | dst->private_data_len = src->private_data_len; |
| 203 | dst->responder_resources =src->responder_resources; |
| 204 | dst->initiator_depth = src->initiator_depth; |
| 205 | dst->flow_control = src->flow_control; |
| 206 | dst->retry_count = src->retry_count; |
| 207 | dst->rnr_retry_count = src->rnr_retry_count; |
| 208 | dst->srq = src->srq; |
| 209 | dst->qp_num = src->qp_num; |
| 210 | } |
| 211 | |
| 212 | static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst, |
| 213 | struct rdma_ud_param *src) |
| 214 | { |
| 215 | if (src->private_data_len) |
| 216 | memcpy(dst->private_data, src->private_data, |
| 217 | src->private_data_len); |
| 218 | dst->private_data_len = src->private_data_len; |
| 219 | ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr); |
| 220 | dst->qp_num = src->qp_num; |
| 221 | dst->qkey = src->qkey; |
| 222 | } |
| 223 | |
| 224 | static void ucma_set_event_context(struct ucma_context *ctx, |
| 225 | struct rdma_cm_event *event, |
| 226 | struct ucma_event *uevent) |
| 227 | { |
| 228 | uevent->ctx = ctx; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 229 | switch (event->event) { |
| 230 | case RDMA_CM_EVENT_MULTICAST_JOIN: |
| 231 | case RDMA_CM_EVENT_MULTICAST_ERROR: |
| 232 | uevent->mc = (struct ucma_multicast *) |
| 233 | event->param.ud.private_data; |
| 234 | uevent->resp.uid = uevent->mc->uid; |
| 235 | uevent->resp.id = uevent->mc->id; |
| 236 | break; |
| 237 | default: |
| 238 | uevent->resp.uid = ctx->uid; |
| 239 | uevent->resp.id = ctx->id; |
| 240 | break; |
| 241 | } |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static int ucma_event_handler(struct rdma_cm_id *cm_id, |
| 245 | struct rdma_cm_event *event) |
| 246 | { |
| 247 | struct ucma_event *uevent; |
| 248 | struct ucma_context *ctx = cm_id->context; |
| 249 | int ret = 0; |
| 250 | |
| 251 | uevent = kzalloc(sizeof(*uevent), GFP_KERNEL); |
| 252 | if (!uevent) |
| 253 | return event->event == RDMA_CM_EVENT_CONNECT_REQUEST; |
| 254 | |
Tatyana Nikolova | 418edaa | 2012-08-03 23:59:41 +0000 | [diff] [blame] | 255 | mutex_lock(&ctx->file->mut); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 256 | uevent->cm_id = cm_id; |
| 257 | ucma_set_event_context(ctx, event, uevent); |
| 258 | uevent->resp.event = event->event; |
| 259 | uevent->resp.status = event->status; |
Sean Hefty | 638ef7a | 2011-06-14 16:31:53 -0700 | [diff] [blame] | 260 | if (cm_id->qp_type == IB_QPT_UD) |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 261 | ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud); |
| 262 | else |
| 263 | ucma_copy_conn_event(&uevent->resp.param.conn, |
| 264 | &event->param.conn); |
| 265 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 266 | if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) { |
| 267 | if (!ctx->backlog) { |
Sean Hefty | 3492856 | 2007-03-06 11:58:32 -0800 | [diff] [blame] | 268 | ret = -ENOMEM; |
Sean Hefty | 30a5ec9 | 2006-12-14 11:22:19 -0800 | [diff] [blame] | 269 | kfree(uevent); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 270 | goto out; |
| 271 | } |
| 272 | ctx->backlog--; |
Sean Hefty | 0cefcf0 | 2007-01-05 12:35:15 -0800 | [diff] [blame] | 273 | } else if (!ctx->uid) { |
| 274 | /* |
| 275 | * We ignore events for new connections until userspace has set |
| 276 | * their context. This can only happen if an error occurs on a |
| 277 | * new connection before the user accepts it. This is okay, |
| 278 | * since the accept will just fail later. |
| 279 | */ |
| 280 | kfree(uevent); |
| 281 | goto out; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 282 | } |
Sean Hefty | 0cefcf0 | 2007-01-05 12:35:15 -0800 | [diff] [blame] | 283 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 284 | list_add_tail(&uevent->list, &ctx->file->event_list); |
| 285 | wake_up_interruptible(&ctx->file->poll_wait); |
| 286 | out: |
| 287 | mutex_unlock(&ctx->file->mut); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf, |
| 292 | int in_len, int out_len) |
| 293 | { |
| 294 | struct ucma_context *ctx; |
| 295 | struct rdma_ucm_get_event cmd; |
| 296 | struct ucma_event *uevent; |
| 297 | int ret = 0; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 298 | |
| 299 | if (out_len < sizeof uevent->resp) |
| 300 | return -ENOSPC; |
| 301 | |
| 302 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 303 | return -EFAULT; |
| 304 | |
| 305 | mutex_lock(&file->mut); |
| 306 | while (list_empty(&file->event_list)) { |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 307 | mutex_unlock(&file->mut); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 308 | |
Sean Hefty | d92f764 | 2007-04-05 10:49:51 -0700 | [diff] [blame] | 309 | if (file->filp->f_flags & O_NONBLOCK) |
| 310 | return -EAGAIN; |
| 311 | |
| 312 | if (wait_event_interruptible(file->poll_wait, |
| 313 | !list_empty(&file->event_list))) |
| 314 | return -ERESTARTSYS; |
| 315 | |
| 316 | mutex_lock(&file->mut); |
| 317 | } |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 318 | |
| 319 | uevent = list_entry(file->event_list.next, struct ucma_event, list); |
| 320 | |
| 321 | if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) { |
| 322 | ctx = ucma_alloc_ctx(file); |
| 323 | if (!ctx) { |
| 324 | ret = -ENOMEM; |
| 325 | goto done; |
| 326 | } |
| 327 | uevent->ctx->backlog++; |
| 328 | ctx->cm_id = uevent->cm_id; |
| 329 | ctx->cm_id->context = ctx; |
| 330 | uevent->resp.id = ctx->id; |
| 331 | } |
| 332 | |
| 333 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 334 | &uevent->resp, sizeof uevent->resp)) { |
| 335 | ret = -EFAULT; |
| 336 | goto done; |
| 337 | } |
| 338 | |
| 339 | list_del(&uevent->list); |
| 340 | uevent->ctx->events_reported++; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 341 | if (uevent->mc) |
| 342 | uevent->mc->events_reported++; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 343 | kfree(uevent); |
| 344 | done: |
| 345 | mutex_unlock(&file->mut); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 349 | static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type) |
| 350 | { |
| 351 | switch (cmd->ps) { |
| 352 | case RDMA_PS_TCP: |
| 353 | *qp_type = IB_QPT_RC; |
| 354 | return 0; |
| 355 | case RDMA_PS_UDP: |
| 356 | case RDMA_PS_IPOIB: |
| 357 | *qp_type = IB_QPT_UD; |
| 358 | return 0; |
Sean Hefty | 638ef7a | 2011-06-14 16:31:53 -0700 | [diff] [blame] | 359 | case RDMA_PS_IB: |
| 360 | *qp_type = cmd->qp_type; |
| 361 | return 0; |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 362 | default: |
| 363 | return -EINVAL; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf, |
| 368 | int in_len, int out_len) |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 369 | { |
| 370 | struct rdma_ucm_create_id cmd; |
| 371 | struct rdma_ucm_create_id_resp resp; |
| 372 | struct ucma_context *ctx; |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 373 | enum ib_qp_type qp_type; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 374 | int ret; |
| 375 | |
| 376 | if (out_len < sizeof(resp)) |
| 377 | return -ENOSPC; |
| 378 | |
| 379 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 380 | return -EFAULT; |
| 381 | |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 382 | ret = ucma_get_qp_type(&cmd, &qp_type); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 386 | mutex_lock(&file->mut); |
| 387 | ctx = ucma_alloc_ctx(file); |
| 388 | mutex_unlock(&file->mut); |
| 389 | if (!ctx) |
| 390 | return -ENOMEM; |
| 391 | |
| 392 | ctx->uid = cmd.uid; |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 393 | ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 394 | if (IS_ERR(ctx->cm_id)) { |
| 395 | ret = PTR_ERR(ctx->cm_id); |
| 396 | goto err1; |
| 397 | } |
| 398 | |
| 399 | resp.id = ctx->id; |
| 400 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 401 | &resp, sizeof(resp))) { |
| 402 | ret = -EFAULT; |
| 403 | goto err2; |
| 404 | } |
| 405 | return 0; |
| 406 | |
| 407 | err2: |
| 408 | rdma_destroy_id(ctx->cm_id); |
| 409 | err1: |
| 410 | mutex_lock(&mut); |
| 411 | idr_remove(&ctx_idr, ctx->id); |
| 412 | mutex_unlock(&mut); |
| 413 | kfree(ctx); |
| 414 | return ret; |
| 415 | } |
| 416 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 417 | static void ucma_cleanup_multicast(struct ucma_context *ctx) |
| 418 | { |
| 419 | struct ucma_multicast *mc, *tmp; |
| 420 | |
| 421 | mutex_lock(&mut); |
| 422 | list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) { |
| 423 | list_del(&mc->list); |
| 424 | idr_remove(&multicast_idr, mc->id); |
| 425 | kfree(mc); |
| 426 | } |
| 427 | mutex_unlock(&mut); |
| 428 | } |
| 429 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 430 | static void ucma_cleanup_mc_events(struct ucma_multicast *mc) |
| 431 | { |
| 432 | struct ucma_event *uevent, *tmp; |
| 433 | |
| 434 | list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) { |
| 435 | if (uevent->mc != mc) |
| 436 | continue; |
| 437 | |
| 438 | list_del(&uevent->list); |
| 439 | kfree(uevent); |
| 440 | } |
| 441 | } |
| 442 | |
Hefty, Sean | 186834b | 2012-03-02 00:01:19 +0000 | [diff] [blame] | 443 | /* |
| 444 | * We cannot hold file->mut when calling rdma_destroy_id() or we can |
| 445 | * deadlock. We also acquire file->mut in ucma_event_handler(), and |
| 446 | * rdma_destroy_id() will wait until all callbacks have completed. |
| 447 | */ |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 448 | static int ucma_free_ctx(struct ucma_context *ctx) |
| 449 | { |
| 450 | int events_reported; |
Hefty, Sean | 186834b | 2012-03-02 00:01:19 +0000 | [diff] [blame] | 451 | struct ucma_event *uevent, *tmp; |
| 452 | LIST_HEAD(list); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 453 | |
| 454 | /* No new events will be generated after destroying the id. */ |
| 455 | rdma_destroy_id(ctx->cm_id); |
| 456 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 457 | ucma_cleanup_multicast(ctx); |
| 458 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 459 | /* Cleanup events not yet reported to the user. */ |
| 460 | mutex_lock(&ctx->file->mut); |
Hefty, Sean | 186834b | 2012-03-02 00:01:19 +0000 | [diff] [blame] | 461 | list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) { |
| 462 | if (uevent->ctx == ctx) |
| 463 | list_move_tail(&uevent->list, &list); |
| 464 | } |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 465 | list_del(&ctx->list); |
| 466 | mutex_unlock(&ctx->file->mut); |
| 467 | |
Hefty, Sean | 186834b | 2012-03-02 00:01:19 +0000 | [diff] [blame] | 468 | list_for_each_entry_safe(uevent, tmp, &list, list) { |
| 469 | list_del(&uevent->list); |
| 470 | if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) |
| 471 | rdma_destroy_id(uevent->cm_id); |
| 472 | kfree(uevent); |
| 473 | } |
| 474 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 475 | events_reported = ctx->events_reported; |
| 476 | kfree(ctx); |
| 477 | return events_reported; |
| 478 | } |
| 479 | |
| 480 | static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf, |
| 481 | int in_len, int out_len) |
| 482 | { |
| 483 | struct rdma_ucm_destroy_id cmd; |
| 484 | struct rdma_ucm_destroy_id_resp resp; |
| 485 | struct ucma_context *ctx; |
| 486 | int ret = 0; |
| 487 | |
| 488 | if (out_len < sizeof(resp)) |
| 489 | return -ENOSPC; |
| 490 | |
| 491 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 492 | return -EFAULT; |
| 493 | |
| 494 | mutex_lock(&mut); |
| 495 | ctx = _ucma_find_context(cmd.id, file); |
| 496 | if (!IS_ERR(ctx)) |
| 497 | idr_remove(&ctx_idr, ctx->id); |
| 498 | mutex_unlock(&mut); |
| 499 | |
| 500 | if (IS_ERR(ctx)) |
| 501 | return PTR_ERR(ctx); |
| 502 | |
| 503 | ucma_put_ctx(ctx); |
| 504 | wait_for_completion(&ctx->comp); |
| 505 | resp.events_reported = ucma_free_ctx(ctx); |
| 506 | |
| 507 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 508 | &resp, sizeof(resp))) |
| 509 | ret = -EFAULT; |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf, |
| 515 | int in_len, int out_len) |
| 516 | { |
| 517 | struct rdma_ucm_bind_addr cmd; |
| 518 | struct ucma_context *ctx; |
| 519 | int ret; |
| 520 | |
| 521 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 522 | return -EFAULT; |
| 523 | |
| 524 | ctx = ucma_get_ctx(file, cmd.id); |
| 525 | if (IS_ERR(ctx)) |
| 526 | return PTR_ERR(ctx); |
| 527 | |
| 528 | ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr); |
| 529 | ucma_put_ctx(ctx); |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | static ssize_t ucma_resolve_addr(struct ucma_file *file, |
| 534 | const char __user *inbuf, |
| 535 | int in_len, int out_len) |
| 536 | { |
| 537 | struct rdma_ucm_resolve_addr cmd; |
| 538 | struct ucma_context *ctx; |
| 539 | int ret; |
| 540 | |
| 541 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 542 | return -EFAULT; |
| 543 | |
| 544 | ctx = ucma_get_ctx(file, cmd.id); |
| 545 | if (IS_ERR(ctx)) |
| 546 | return PTR_ERR(ctx); |
| 547 | |
| 548 | ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr, |
| 549 | (struct sockaddr *) &cmd.dst_addr, |
| 550 | cmd.timeout_ms); |
| 551 | ucma_put_ctx(ctx); |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | static ssize_t ucma_resolve_route(struct ucma_file *file, |
| 556 | const char __user *inbuf, |
| 557 | int in_len, int out_len) |
| 558 | { |
| 559 | struct rdma_ucm_resolve_route cmd; |
| 560 | struct ucma_context *ctx; |
| 561 | int ret; |
| 562 | |
| 563 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 564 | return -EFAULT; |
| 565 | |
| 566 | ctx = ucma_get_ctx(file, cmd.id); |
| 567 | if (IS_ERR(ctx)) |
| 568 | return PTR_ERR(ctx); |
| 569 | |
| 570 | ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms); |
| 571 | ucma_put_ctx(ctx); |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp, |
| 576 | struct rdma_route *route) |
| 577 | { |
| 578 | struct rdma_dev_addr *dev_addr; |
| 579 | |
| 580 | resp->num_paths = route->num_paths; |
| 581 | switch (route->num_paths) { |
| 582 | case 0: |
| 583 | dev_addr = &route->addr.dev_addr; |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 584 | rdma_addr_get_dgid(dev_addr, |
| 585 | (union ib_gid *) &resp->ib_route[0].dgid); |
| 586 | rdma_addr_get_sgid(dev_addr, |
| 587 | (union ib_gid *) &resp->ib_route[0].sgid); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 588 | resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); |
| 589 | break; |
| 590 | case 2: |
| 591 | ib_copy_path_rec_to_user(&resp->ib_route[1], |
| 592 | &route->path_rec[1]); |
| 593 | /* fall through */ |
| 594 | case 1: |
| 595 | ib_copy_path_rec_to_user(&resp->ib_route[0], |
| 596 | &route->path_rec[0]); |
| 597 | break; |
| 598 | default: |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 603 | static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp, |
| 604 | struct rdma_route *route) |
| 605 | { |
| 606 | struct rdma_dev_addr *dev_addr; |
Eli Cohen | af7bd46 | 2010-08-26 17:18:59 +0300 | [diff] [blame] | 607 | struct net_device *dev; |
| 608 | u16 vid = 0; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 609 | |
| 610 | resp->num_paths = route->num_paths; |
| 611 | switch (route->num_paths) { |
| 612 | case 0: |
| 613 | dev_addr = &route->addr.dev_addr; |
Eli Cohen | af7bd46 | 2010-08-26 17:18:59 +0300 | [diff] [blame] | 614 | dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); |
| 615 | if (dev) { |
| 616 | vid = rdma_vlan_dev_vlan_id(dev); |
| 617 | dev_put(dev); |
| 618 | } |
| 619 | |
| 620 | iboe_mac_vlan_to_ll((union ib_gid *) &resp->ib_route[0].dgid, |
| 621 | dev_addr->dst_dev_addr, vid); |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 622 | iboe_addr_get_sgid(dev_addr, |
| 623 | (union ib_gid *) &resp->ib_route[0].sgid); |
| 624 | resp->ib_route[0].pkey = cpu_to_be16(0xffff); |
| 625 | break; |
| 626 | case 2: |
| 627 | ib_copy_path_rec_to_user(&resp->ib_route[1], |
| 628 | &route->path_rec[1]); |
| 629 | /* fall through */ |
| 630 | case 1: |
| 631 | ib_copy_path_rec_to_user(&resp->ib_route[0], |
| 632 | &route->path_rec[0]); |
| 633 | break; |
| 634 | default: |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | |
Steve Wise | e86f8b0 | 2011-01-21 03:40:46 +0000 | [diff] [blame] | 639 | static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp, |
| 640 | struct rdma_route *route) |
| 641 | { |
| 642 | struct rdma_dev_addr *dev_addr; |
| 643 | |
| 644 | dev_addr = &route->addr.dev_addr; |
| 645 | rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid); |
| 646 | rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid); |
| 647 | } |
| 648 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 649 | static ssize_t ucma_query_route(struct ucma_file *file, |
| 650 | const char __user *inbuf, |
| 651 | int in_len, int out_len) |
| 652 | { |
Sean Hefty | ee7aed4 | 2013-05-29 10:09:25 -0700 | [diff] [blame] | 653 | struct rdma_ucm_query cmd; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 654 | struct rdma_ucm_query_route_resp resp; |
| 655 | struct ucma_context *ctx; |
| 656 | struct sockaddr *addr; |
| 657 | int ret = 0; |
| 658 | |
| 659 | if (out_len < sizeof(resp)) |
| 660 | return -ENOSPC; |
| 661 | |
| 662 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 663 | return -EFAULT; |
| 664 | |
| 665 | ctx = ucma_get_ctx(file, cmd.id); |
| 666 | if (IS_ERR(ctx)) |
| 667 | return PTR_ERR(ctx); |
| 668 | |
| 669 | memset(&resp, 0, sizeof resp); |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 670 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 671 | memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ? |
| 672 | sizeof(struct sockaddr_in) : |
| 673 | sizeof(struct sockaddr_in6)); |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 674 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 675 | memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ? |
| 676 | sizeof(struct sockaddr_in) : |
| 677 | sizeof(struct sockaddr_in6)); |
| 678 | if (!ctx->cm_id->device) |
| 679 | goto out; |
| 680 | |
Roland Dreier | 9cda779 | 2008-04-16 21:01:07 -0700 | [diff] [blame] | 681 | resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 682 | resp.port_num = ctx->cm_id->port_num; |
Steve Wise | e86f8b0 | 2011-01-21 03:40:46 +0000 | [diff] [blame] | 683 | switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) { |
| 684 | case RDMA_TRANSPORT_IB: |
| 685 | switch (rdma_port_get_link_layer(ctx->cm_id->device, |
| 686 | ctx->cm_id->port_num)) { |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 687 | case IB_LINK_LAYER_INFINIBAND: |
| 688 | ucma_copy_ib_route(&resp, &ctx->cm_id->route); |
| 689 | break; |
| 690 | case IB_LINK_LAYER_ETHERNET: |
| 691 | ucma_copy_iboe_route(&resp, &ctx->cm_id->route); |
| 692 | break; |
| 693 | default: |
| 694 | break; |
| 695 | } |
Steve Wise | e86f8b0 | 2011-01-21 03:40:46 +0000 | [diff] [blame] | 696 | break; |
| 697 | case RDMA_TRANSPORT_IWARP: |
| 698 | ucma_copy_iw_route(&resp, &ctx->cm_id->route); |
| 699 | break; |
| 700 | default: |
| 701 | break; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | out: |
| 705 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 706 | &resp, sizeof(resp))) |
| 707 | ret = -EFAULT; |
| 708 | |
| 709 | ucma_put_ctx(ctx); |
| 710 | return ret; |
| 711 | } |
| 712 | |
Sean Hefty | ee7aed4 | 2013-05-29 10:09:25 -0700 | [diff] [blame] | 713 | static void ucma_query_device_addr(struct rdma_cm_id *cm_id, |
| 714 | struct rdma_ucm_query_addr_resp *resp) |
| 715 | { |
| 716 | if (!cm_id->device) |
| 717 | return; |
| 718 | |
| 719 | resp->node_guid = (__force __u64) cm_id->device->node_guid; |
| 720 | resp->port_num = cm_id->port_num; |
| 721 | resp->pkey = (__force __u16) cpu_to_be16( |
| 722 | ib_addr_get_pkey(&cm_id->route.addr.dev_addr)); |
| 723 | } |
| 724 | |
| 725 | static ssize_t ucma_query_addr(struct ucma_context *ctx, |
| 726 | void __user *response, int out_len) |
| 727 | { |
| 728 | struct rdma_ucm_query_addr_resp resp; |
| 729 | struct sockaddr *addr; |
| 730 | int ret = 0; |
| 731 | |
| 732 | if (out_len < sizeof(resp)) |
| 733 | return -ENOSPC; |
| 734 | |
| 735 | memset(&resp, 0, sizeof resp); |
| 736 | |
| 737 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr; |
| 738 | resp.src_size = rdma_addr_size(addr); |
| 739 | memcpy(&resp.src_addr, addr, resp.src_size); |
| 740 | |
| 741 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr; |
| 742 | resp.dst_size = rdma_addr_size(addr); |
| 743 | memcpy(&resp.dst_addr, addr, resp.dst_size); |
| 744 | |
| 745 | ucma_query_device_addr(ctx->cm_id, &resp); |
| 746 | |
| 747 | if (copy_to_user(response, &resp, sizeof(resp))) |
| 748 | ret = -EFAULT; |
| 749 | |
| 750 | return ret; |
| 751 | } |
| 752 | |
Sean Hefty | ac53b26 | 2013-05-29 10:09:27 -0700 | [diff] [blame^] | 753 | static ssize_t ucma_query_path(struct ucma_context *ctx, |
| 754 | void __user *response, int out_len) |
| 755 | { |
| 756 | struct rdma_ucm_query_path_resp *resp; |
| 757 | int i, ret = 0; |
| 758 | |
| 759 | if (out_len < sizeof(*resp)) |
| 760 | return -ENOSPC; |
| 761 | |
| 762 | resp = kzalloc(out_len, GFP_KERNEL); |
| 763 | if (!resp) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | resp->num_paths = ctx->cm_id->route.num_paths; |
| 767 | for (i = 0, out_len -= sizeof(*resp); |
| 768 | i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data); |
| 769 | i++, out_len -= sizeof(struct ib_path_rec_data)) { |
| 770 | |
| 771 | resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY | |
| 772 | IB_PATH_BIDIRECTIONAL; |
| 773 | ib_sa_pack_path(&ctx->cm_id->route.path_rec[i], |
| 774 | &resp->path_data[i].path_rec); |
| 775 | } |
| 776 | |
| 777 | if (copy_to_user(response, resp, |
| 778 | sizeof(*resp) + (i * sizeof(struct ib_path_rec_data)))) |
| 779 | ret = -EFAULT; |
| 780 | |
| 781 | kfree(resp); |
| 782 | return ret; |
| 783 | } |
| 784 | |
Sean Hefty | ee7aed4 | 2013-05-29 10:09:25 -0700 | [diff] [blame] | 785 | static ssize_t ucma_query(struct ucma_file *file, |
| 786 | const char __user *inbuf, |
| 787 | int in_len, int out_len) |
| 788 | { |
| 789 | struct rdma_ucm_query cmd; |
| 790 | struct ucma_context *ctx; |
| 791 | void __user *response; |
| 792 | int ret; |
| 793 | |
| 794 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 795 | return -EFAULT; |
| 796 | |
| 797 | response = (void __user *)(unsigned long) cmd.response; |
| 798 | ctx = ucma_get_ctx(file, cmd.id); |
| 799 | if (IS_ERR(ctx)) |
| 800 | return PTR_ERR(ctx); |
| 801 | |
| 802 | switch (cmd.option) { |
| 803 | case RDMA_USER_CM_QUERY_ADDR: |
| 804 | ret = ucma_query_addr(ctx, response, out_len); |
| 805 | break; |
Sean Hefty | ac53b26 | 2013-05-29 10:09:27 -0700 | [diff] [blame^] | 806 | case RDMA_USER_CM_QUERY_PATH: |
| 807 | ret = ucma_query_path(ctx, response, out_len); |
| 808 | break; |
Sean Hefty | ee7aed4 | 2013-05-29 10:09:25 -0700 | [diff] [blame] | 809 | default: |
| 810 | ret = -ENOSYS; |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | ucma_put_ctx(ctx); |
| 815 | return ret; |
| 816 | } |
| 817 | |
Sean Hefty | 5c43813 | 2013-05-29 10:09:23 -0700 | [diff] [blame] | 818 | static void ucma_copy_conn_param(struct rdma_cm_id *id, |
| 819 | struct rdma_conn_param *dst, |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 820 | struct rdma_ucm_conn_param *src) |
| 821 | { |
| 822 | dst->private_data = src->private_data; |
| 823 | dst->private_data_len = src->private_data_len; |
| 824 | dst->responder_resources =src->responder_resources; |
| 825 | dst->initiator_depth = src->initiator_depth; |
| 826 | dst->flow_control = src->flow_control; |
| 827 | dst->retry_count = src->retry_count; |
| 828 | dst->rnr_retry_count = src->rnr_retry_count; |
| 829 | dst->srq = src->srq; |
| 830 | dst->qp_num = src->qp_num; |
Sean Hefty | 5c43813 | 2013-05-29 10:09:23 -0700 | [diff] [blame] | 831 | dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf, |
| 835 | int in_len, int out_len) |
| 836 | { |
| 837 | struct rdma_ucm_connect cmd; |
| 838 | struct rdma_conn_param conn_param; |
| 839 | struct ucma_context *ctx; |
| 840 | int ret; |
| 841 | |
| 842 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 843 | return -EFAULT; |
| 844 | |
| 845 | if (!cmd.conn_param.valid) |
| 846 | return -EINVAL; |
| 847 | |
| 848 | ctx = ucma_get_ctx(file, cmd.id); |
| 849 | if (IS_ERR(ctx)) |
| 850 | return PTR_ERR(ctx); |
| 851 | |
Sean Hefty | 5c43813 | 2013-05-29 10:09:23 -0700 | [diff] [blame] | 852 | ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 853 | ret = rdma_connect(ctx->cm_id, &conn_param); |
| 854 | ucma_put_ctx(ctx); |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf, |
| 859 | int in_len, int out_len) |
| 860 | { |
| 861 | struct rdma_ucm_listen cmd; |
| 862 | struct ucma_context *ctx; |
| 863 | int ret; |
| 864 | |
| 865 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 866 | return -EFAULT; |
| 867 | |
| 868 | ctx = ucma_get_ctx(file, cmd.id); |
| 869 | if (IS_ERR(ctx)) |
| 870 | return PTR_ERR(ctx); |
| 871 | |
Steve Wise | 97cb7e4 | 2010-08-13 20:56:34 +0000 | [diff] [blame] | 872 | ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ? |
| 873 | cmd.backlog : max_backlog; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 874 | ret = rdma_listen(ctx->cm_id, ctx->backlog); |
| 875 | ucma_put_ctx(ctx); |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf, |
| 880 | int in_len, int out_len) |
| 881 | { |
| 882 | struct rdma_ucm_accept cmd; |
| 883 | struct rdma_conn_param conn_param; |
| 884 | struct ucma_context *ctx; |
| 885 | int ret; |
| 886 | |
| 887 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 888 | return -EFAULT; |
| 889 | |
| 890 | ctx = ucma_get_ctx(file, cmd.id); |
| 891 | if (IS_ERR(ctx)) |
| 892 | return PTR_ERR(ctx); |
| 893 | |
| 894 | if (cmd.conn_param.valid) { |
Sean Hefty | 5c43813 | 2013-05-29 10:09:23 -0700 | [diff] [blame] | 895 | ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param); |
Sean Hefty | 9ced69c | 2012-01-10 23:53:41 +0000 | [diff] [blame] | 896 | mutex_lock(&file->mut); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 897 | ret = rdma_accept(ctx->cm_id, &conn_param); |
Sean Hefty | 9ced69c | 2012-01-10 23:53:41 +0000 | [diff] [blame] | 898 | if (!ret) |
| 899 | ctx->uid = cmd.uid; |
| 900 | mutex_unlock(&file->mut); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 901 | } else |
| 902 | ret = rdma_accept(ctx->cm_id, NULL); |
| 903 | |
| 904 | ucma_put_ctx(ctx); |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf, |
| 909 | int in_len, int out_len) |
| 910 | { |
| 911 | struct rdma_ucm_reject cmd; |
| 912 | struct ucma_context *ctx; |
| 913 | int ret; |
| 914 | |
| 915 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 916 | return -EFAULT; |
| 917 | |
| 918 | ctx = ucma_get_ctx(file, cmd.id); |
| 919 | if (IS_ERR(ctx)) |
| 920 | return PTR_ERR(ctx); |
| 921 | |
| 922 | ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len); |
| 923 | ucma_put_ctx(ctx); |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf, |
| 928 | int in_len, int out_len) |
| 929 | { |
| 930 | struct rdma_ucm_disconnect cmd; |
| 931 | struct ucma_context *ctx; |
| 932 | int ret; |
| 933 | |
| 934 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 935 | return -EFAULT; |
| 936 | |
| 937 | ctx = ucma_get_ctx(file, cmd.id); |
| 938 | if (IS_ERR(ctx)) |
| 939 | return PTR_ERR(ctx); |
| 940 | |
| 941 | ret = rdma_disconnect(ctx->cm_id); |
| 942 | ucma_put_ctx(ctx); |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | static ssize_t ucma_init_qp_attr(struct ucma_file *file, |
| 947 | const char __user *inbuf, |
| 948 | int in_len, int out_len) |
| 949 | { |
| 950 | struct rdma_ucm_init_qp_attr cmd; |
| 951 | struct ib_uverbs_qp_attr resp; |
| 952 | struct ucma_context *ctx; |
| 953 | struct ib_qp_attr qp_attr; |
| 954 | int ret; |
| 955 | |
| 956 | if (out_len < sizeof(resp)) |
| 957 | return -ENOSPC; |
| 958 | |
| 959 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 960 | return -EFAULT; |
| 961 | |
| 962 | ctx = ucma_get_ctx(file, cmd.id); |
| 963 | if (IS_ERR(ctx)) |
| 964 | return PTR_ERR(ctx); |
| 965 | |
| 966 | resp.qp_attr_mask = 0; |
| 967 | memset(&qp_attr, 0, sizeof qp_attr); |
| 968 | qp_attr.qp_state = cmd.qp_state; |
| 969 | ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask); |
| 970 | if (ret) |
| 971 | goto out; |
| 972 | |
| 973 | ib_copy_qp_attr_to_user(&resp, &qp_attr); |
| 974 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 975 | &resp, sizeof(resp))) |
| 976 | ret = -EFAULT; |
| 977 | |
| 978 | out: |
| 979 | ucma_put_ctx(ctx); |
| 980 | return ret; |
| 981 | } |
| 982 | |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 983 | static int ucma_set_option_id(struct ucma_context *ctx, int optname, |
| 984 | void *optval, size_t optlen) |
| 985 | { |
| 986 | int ret = 0; |
| 987 | |
| 988 | switch (optname) { |
| 989 | case RDMA_OPTION_ID_TOS: |
| 990 | if (optlen != sizeof(u8)) { |
| 991 | ret = -EINVAL; |
| 992 | break; |
| 993 | } |
| 994 | rdma_set_service_type(ctx->cm_id, *((u8 *) optval)); |
| 995 | break; |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 996 | case RDMA_OPTION_ID_REUSEADDR: |
| 997 | if (optlen != sizeof(int)) { |
| 998 | ret = -EINVAL; |
| 999 | break; |
| 1000 | } |
| 1001 | ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0); |
| 1002 | break; |
Sean Hefty | 6860212 | 2012-06-14 20:31:39 +0000 | [diff] [blame] | 1003 | case RDMA_OPTION_ID_AFONLY: |
| 1004 | if (optlen != sizeof(int)) { |
| 1005 | ret = -EINVAL; |
| 1006 | break; |
| 1007 | } |
| 1008 | ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0); |
| 1009 | break; |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1010 | default: |
| 1011 | ret = -ENOSYS; |
| 1012 | } |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Sean Hefty | a7ca1f0 | 2009-11-16 09:30:33 -0800 | [diff] [blame] | 1017 | static int ucma_set_ib_path(struct ucma_context *ctx, |
| 1018 | struct ib_path_rec_data *path_data, size_t optlen) |
| 1019 | { |
| 1020 | struct ib_sa_path_rec sa_path; |
| 1021 | struct rdma_cm_event event; |
| 1022 | int ret; |
| 1023 | |
| 1024 | if (optlen % sizeof(*path_data)) |
| 1025 | return -EINVAL; |
| 1026 | |
| 1027 | for (; optlen; optlen -= sizeof(*path_data), path_data++) { |
| 1028 | if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY | |
| 1029 | IB_PATH_BIDIRECTIONAL)) |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | if (!optlen) |
| 1034 | return -EINVAL; |
| 1035 | |
| 1036 | ib_sa_unpack_path(path_data->path_rec, &sa_path); |
| 1037 | ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1); |
| 1038 | if (ret) |
| 1039 | return ret; |
| 1040 | |
| 1041 | memset(&event, 0, sizeof event); |
| 1042 | event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
| 1043 | return ucma_event_handler(ctx->cm_id, &event); |
| 1044 | } |
| 1045 | |
| 1046 | static int ucma_set_option_ib(struct ucma_context *ctx, int optname, |
| 1047 | void *optval, size_t optlen) |
| 1048 | { |
| 1049 | int ret; |
| 1050 | |
| 1051 | switch (optname) { |
| 1052 | case RDMA_OPTION_IB_PATH: |
| 1053 | ret = ucma_set_ib_path(ctx, optval, optlen); |
| 1054 | break; |
| 1055 | default: |
| 1056 | ret = -ENOSYS; |
| 1057 | } |
| 1058 | |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1062 | static int ucma_set_option_level(struct ucma_context *ctx, int level, |
| 1063 | int optname, void *optval, size_t optlen) |
| 1064 | { |
| 1065 | int ret; |
| 1066 | |
| 1067 | switch (level) { |
| 1068 | case RDMA_OPTION_ID: |
| 1069 | ret = ucma_set_option_id(ctx, optname, optval, optlen); |
| 1070 | break; |
Sean Hefty | a7ca1f0 | 2009-11-16 09:30:33 -0800 | [diff] [blame] | 1071 | case RDMA_OPTION_IB: |
| 1072 | ret = ucma_set_option_ib(ctx, optname, optval, optlen); |
| 1073 | break; |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1074 | default: |
| 1075 | ret = -ENOSYS; |
| 1076 | } |
| 1077 | |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf, |
| 1082 | int in_len, int out_len) |
| 1083 | { |
| 1084 | struct rdma_ucm_set_option cmd; |
| 1085 | struct ucma_context *ctx; |
| 1086 | void *optval; |
| 1087 | int ret; |
| 1088 | |
| 1089 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 1090 | return -EFAULT; |
| 1091 | |
| 1092 | ctx = ucma_get_ctx(file, cmd.id); |
| 1093 | if (IS_ERR(ctx)) |
| 1094 | return PTR_ERR(ctx); |
| 1095 | |
Roland Dreier | 0764c76 | 2012-07-27 13:27:45 -0700 | [diff] [blame] | 1096 | optval = memdup_user((void __user *) (unsigned long) cmd.optval, |
| 1097 | cmd.optlen); |
| 1098 | if (IS_ERR(optval)) { |
| 1099 | ret = PTR_ERR(optval); |
| 1100 | goto out; |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval, |
| 1104 | cmd.optlen); |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1105 | kfree(optval); |
Roland Dreier | 0764c76 | 2012-07-27 13:27:45 -0700 | [diff] [blame] | 1106 | |
| 1107 | out: |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1108 | ucma_put_ctx(ctx); |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1112 | static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf, |
| 1113 | int in_len, int out_len) |
| 1114 | { |
| 1115 | struct rdma_ucm_notify cmd; |
| 1116 | struct ucma_context *ctx; |
| 1117 | int ret; |
| 1118 | |
| 1119 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 1120 | return -EFAULT; |
| 1121 | |
| 1122 | ctx = ucma_get_ctx(file, cmd.id); |
| 1123 | if (IS_ERR(ctx)) |
| 1124 | return PTR_ERR(ctx); |
| 1125 | |
| 1126 | ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event); |
| 1127 | ucma_put_ctx(ctx); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 1131 | static ssize_t ucma_join_multicast(struct ucma_file *file, |
| 1132 | const char __user *inbuf, |
| 1133 | int in_len, int out_len) |
| 1134 | { |
| 1135 | struct rdma_ucm_join_mcast cmd; |
| 1136 | struct rdma_ucm_create_id_resp resp; |
| 1137 | struct ucma_context *ctx; |
| 1138 | struct ucma_multicast *mc; |
| 1139 | int ret; |
| 1140 | |
| 1141 | if (out_len < sizeof(resp)) |
| 1142 | return -ENOSPC; |
| 1143 | |
| 1144 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 1145 | return -EFAULT; |
| 1146 | |
| 1147 | ctx = ucma_get_ctx(file, cmd.id); |
| 1148 | if (IS_ERR(ctx)) |
| 1149 | return PTR_ERR(ctx); |
| 1150 | |
| 1151 | mutex_lock(&file->mut); |
| 1152 | mc = ucma_alloc_multicast(ctx); |
Julien Brunel | 6aea938 | 2008-10-10 12:00:19 -0700 | [diff] [blame] | 1153 | if (!mc) { |
| 1154 | ret = -ENOMEM; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 1155 | goto err1; |
| 1156 | } |
| 1157 | |
| 1158 | mc->uid = cmd.uid; |
| 1159 | memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr); |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1160 | ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 1161 | if (ret) |
| 1162 | goto err2; |
| 1163 | |
| 1164 | resp.id = mc->id; |
| 1165 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 1166 | &resp, sizeof(resp))) { |
| 1167 | ret = -EFAULT; |
| 1168 | goto err3; |
| 1169 | } |
| 1170 | |
| 1171 | mutex_unlock(&file->mut); |
| 1172 | ucma_put_ctx(ctx); |
| 1173 | return 0; |
| 1174 | |
| 1175 | err3: |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1176 | rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 1177 | ucma_cleanup_mc_events(mc); |
| 1178 | err2: |
| 1179 | mutex_lock(&mut); |
| 1180 | idr_remove(&multicast_idr, mc->id); |
| 1181 | mutex_unlock(&mut); |
| 1182 | list_del(&mc->list); |
| 1183 | kfree(mc); |
| 1184 | err1: |
| 1185 | mutex_unlock(&file->mut); |
| 1186 | ucma_put_ctx(ctx); |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | static ssize_t ucma_leave_multicast(struct ucma_file *file, |
| 1191 | const char __user *inbuf, |
| 1192 | int in_len, int out_len) |
| 1193 | { |
| 1194 | struct rdma_ucm_destroy_id cmd; |
| 1195 | struct rdma_ucm_destroy_id_resp resp; |
| 1196 | struct ucma_multicast *mc; |
| 1197 | int ret = 0; |
| 1198 | |
| 1199 | if (out_len < sizeof(resp)) |
| 1200 | return -ENOSPC; |
| 1201 | |
| 1202 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 1203 | return -EFAULT; |
| 1204 | |
| 1205 | mutex_lock(&mut); |
| 1206 | mc = idr_find(&multicast_idr, cmd.id); |
| 1207 | if (!mc) |
| 1208 | mc = ERR_PTR(-ENOENT); |
| 1209 | else if (mc->ctx->file != file) |
| 1210 | mc = ERR_PTR(-EINVAL); |
| 1211 | else { |
| 1212 | idr_remove(&multicast_idr, mc->id); |
| 1213 | atomic_inc(&mc->ctx->ref); |
| 1214 | } |
| 1215 | mutex_unlock(&mut); |
| 1216 | |
| 1217 | if (IS_ERR(mc)) { |
| 1218 | ret = PTR_ERR(mc); |
| 1219 | goto out; |
| 1220 | } |
| 1221 | |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1222 | rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 1223 | mutex_lock(&mc->ctx->file->mut); |
| 1224 | ucma_cleanup_mc_events(mc); |
| 1225 | list_del(&mc->list); |
| 1226 | mutex_unlock(&mc->ctx->file->mut); |
| 1227 | |
| 1228 | ucma_put_ctx(mc->ctx); |
| 1229 | resp.events_reported = mc->events_reported; |
| 1230 | kfree(mc); |
| 1231 | |
| 1232 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 1233 | &resp, sizeof(resp))) |
| 1234 | ret = -EFAULT; |
| 1235 | out: |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
Sean Hefty | 88314e4 | 2007-11-14 00:29:50 -0800 | [diff] [blame] | 1239 | static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2) |
| 1240 | { |
| 1241 | /* Acquire mutex's based on pointer comparison to prevent deadlock. */ |
| 1242 | if (file1 < file2) { |
| 1243 | mutex_lock(&file1->mut); |
| 1244 | mutex_lock(&file2->mut); |
| 1245 | } else { |
| 1246 | mutex_lock(&file2->mut); |
| 1247 | mutex_lock(&file1->mut); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2) |
| 1252 | { |
| 1253 | if (file1 < file2) { |
| 1254 | mutex_unlock(&file2->mut); |
| 1255 | mutex_unlock(&file1->mut); |
| 1256 | } else { |
| 1257 | mutex_unlock(&file1->mut); |
| 1258 | mutex_unlock(&file2->mut); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file) |
| 1263 | { |
| 1264 | struct ucma_event *uevent, *tmp; |
| 1265 | |
| 1266 | list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) |
| 1267 | if (uevent->ctx == ctx) |
| 1268 | list_move_tail(&uevent->list, &file->event_list); |
| 1269 | } |
| 1270 | |
| 1271 | static ssize_t ucma_migrate_id(struct ucma_file *new_file, |
| 1272 | const char __user *inbuf, |
| 1273 | int in_len, int out_len) |
| 1274 | { |
| 1275 | struct rdma_ucm_migrate_id cmd; |
| 1276 | struct rdma_ucm_migrate_resp resp; |
| 1277 | struct ucma_context *ctx; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1278 | struct fd f; |
Sean Hefty | 88314e4 | 2007-11-14 00:29:50 -0800 | [diff] [blame] | 1279 | struct ucma_file *cur_file; |
| 1280 | int ret = 0; |
| 1281 | |
| 1282 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) |
| 1283 | return -EFAULT; |
| 1284 | |
| 1285 | /* Get current fd to protect against it being closed */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1286 | f = fdget(cmd.fd); |
| 1287 | if (!f.file) |
Sean Hefty | 88314e4 | 2007-11-14 00:29:50 -0800 | [diff] [blame] | 1288 | return -ENOENT; |
| 1289 | |
| 1290 | /* Validate current fd and prevent destruction of id. */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1291 | ctx = ucma_get_ctx(f.file->private_data, cmd.id); |
Sean Hefty | 88314e4 | 2007-11-14 00:29:50 -0800 | [diff] [blame] | 1292 | if (IS_ERR(ctx)) { |
| 1293 | ret = PTR_ERR(ctx); |
| 1294 | goto file_put; |
| 1295 | } |
| 1296 | |
| 1297 | cur_file = ctx->file; |
| 1298 | if (cur_file == new_file) { |
| 1299 | resp.events_reported = ctx->events_reported; |
| 1300 | goto response; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | * Migrate events between fd's, maintaining order, and avoiding new |
| 1305 | * events being added before existing events. |
| 1306 | */ |
| 1307 | ucma_lock_files(cur_file, new_file); |
| 1308 | mutex_lock(&mut); |
| 1309 | |
| 1310 | list_move_tail(&ctx->list, &new_file->ctx_list); |
| 1311 | ucma_move_events(ctx, new_file); |
| 1312 | ctx->file = new_file; |
| 1313 | resp.events_reported = ctx->events_reported; |
| 1314 | |
| 1315 | mutex_unlock(&mut); |
| 1316 | ucma_unlock_files(cur_file, new_file); |
| 1317 | |
| 1318 | response: |
| 1319 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
| 1320 | &resp, sizeof(resp))) |
| 1321 | ret = -EFAULT; |
| 1322 | |
| 1323 | ucma_put_ctx(ctx); |
| 1324 | file_put: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1325 | fdput(f); |
Sean Hefty | 88314e4 | 2007-11-14 00:29:50 -0800 | [diff] [blame] | 1326 | return ret; |
| 1327 | } |
| 1328 | |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1329 | static ssize_t (*ucma_cmd_table[])(struct ucma_file *file, |
| 1330 | const char __user *inbuf, |
| 1331 | int in_len, int out_len) = { |
| 1332 | [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id, |
| 1333 | [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id, |
| 1334 | [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr, |
| 1335 | [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr, |
| 1336 | [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route, |
| 1337 | [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route, |
| 1338 | [RDMA_USER_CM_CMD_CONNECT] = ucma_connect, |
| 1339 | [RDMA_USER_CM_CMD_LISTEN] = ucma_listen, |
| 1340 | [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept, |
| 1341 | [RDMA_USER_CM_CMD_REJECT] = ucma_reject, |
| 1342 | [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect, |
| 1343 | [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr, |
| 1344 | [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event, |
| 1345 | [RDMA_USER_CM_CMD_GET_OPTION] = NULL, |
Sean Hefty | 7ce8640 | 2007-08-08 15:51:13 -0700 | [diff] [blame] | 1346 | [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option, |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1347 | [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify, |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 1348 | [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast, |
| 1349 | [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast, |
Sean Hefty | ee7aed4 | 2013-05-29 10:09:25 -0700 | [diff] [blame] | 1350 | [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id, |
| 1351 | [RDMA_USER_CM_CMD_QUERY] = ucma_query |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1352 | }; |
| 1353 | |
| 1354 | static ssize_t ucma_write(struct file *filp, const char __user *buf, |
| 1355 | size_t len, loff_t *pos) |
| 1356 | { |
| 1357 | struct ucma_file *file = filp->private_data; |
| 1358 | struct rdma_ucm_cmd_hdr hdr; |
| 1359 | ssize_t ret; |
| 1360 | |
| 1361 | if (len < sizeof(hdr)) |
| 1362 | return -EINVAL; |
| 1363 | |
| 1364 | if (copy_from_user(&hdr, buf, sizeof(hdr))) |
| 1365 | return -EFAULT; |
| 1366 | |
Hefty, Sean | caf6e3f | 2011-10-06 09:33:05 -0700 | [diff] [blame] | 1367 | if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table)) |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1368 | return -EINVAL; |
| 1369 | |
| 1370 | if (hdr.in + sizeof(hdr) > len) |
| 1371 | return -EINVAL; |
| 1372 | |
| 1373 | if (!ucma_cmd_table[hdr.cmd]) |
| 1374 | return -ENOSYS; |
| 1375 | |
| 1376 | ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out); |
| 1377 | if (!ret) |
| 1378 | ret = len; |
| 1379 | |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
| 1383 | static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait) |
| 1384 | { |
| 1385 | struct ucma_file *file = filp->private_data; |
| 1386 | unsigned int mask = 0; |
| 1387 | |
| 1388 | poll_wait(filp, &file->poll_wait, wait); |
| 1389 | |
| 1390 | if (!list_empty(&file->event_list)) |
| 1391 | mask = POLLIN | POLLRDNORM; |
| 1392 | |
| 1393 | return mask; |
| 1394 | } |
| 1395 | |
Roland Dreier | f7a6117e | 2008-07-24 20:36:59 -0700 | [diff] [blame] | 1396 | /* |
| 1397 | * ucma_open() does not need the BKL: |
| 1398 | * |
| 1399 | * - no global state is referred to; |
| 1400 | * - there is no ioctl method to race against; |
| 1401 | * - no further module initialization is required for open to work |
| 1402 | * after the device is registered. |
| 1403 | */ |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1404 | static int ucma_open(struct inode *inode, struct file *filp) |
| 1405 | { |
| 1406 | struct ucma_file *file; |
| 1407 | |
| 1408 | file = kmalloc(sizeof *file, GFP_KERNEL); |
| 1409 | if (!file) |
| 1410 | return -ENOMEM; |
| 1411 | |
| 1412 | INIT_LIST_HEAD(&file->event_list); |
| 1413 | INIT_LIST_HEAD(&file->ctx_list); |
| 1414 | init_waitqueue_head(&file->poll_wait); |
| 1415 | mutex_init(&file->mut); |
| 1416 | |
| 1417 | filp->private_data = file; |
| 1418 | file->filp = filp; |
Roland Dreier | bc1db9a | 2010-04-09 17:13:50 -0700 | [diff] [blame] | 1419 | |
| 1420 | return nonseekable_open(inode, filp); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | static int ucma_close(struct inode *inode, struct file *filp) |
| 1424 | { |
| 1425 | struct ucma_file *file = filp->private_data; |
| 1426 | struct ucma_context *ctx, *tmp; |
| 1427 | |
| 1428 | mutex_lock(&file->mut); |
| 1429 | list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) { |
| 1430 | mutex_unlock(&file->mut); |
| 1431 | |
| 1432 | mutex_lock(&mut); |
| 1433 | idr_remove(&ctx_idr, ctx->id); |
| 1434 | mutex_unlock(&mut); |
| 1435 | |
| 1436 | ucma_free_ctx(ctx); |
| 1437 | mutex_lock(&file->mut); |
| 1438 | } |
| 1439 | mutex_unlock(&file->mut); |
| 1440 | kfree(file); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 1444 | static const struct file_operations ucma_fops = { |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1445 | .owner = THIS_MODULE, |
| 1446 | .open = ucma_open, |
| 1447 | .release = ucma_close, |
| 1448 | .write = ucma_write, |
| 1449 | .poll = ucma_poll, |
Roland Dreier | bc1db9a | 2010-04-09 17:13:50 -0700 | [diff] [blame] | 1450 | .llseek = no_llseek, |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1451 | }; |
| 1452 | |
| 1453 | static struct miscdevice ucma_misc = { |
Roland Dreier | 04ea2f8 | 2011-05-23 10:48:43 -0700 | [diff] [blame] | 1454 | .minor = MISC_DYNAMIC_MINOR, |
| 1455 | .name = "rdma_cm", |
| 1456 | .nodename = "infiniband/rdma_cm", |
| 1457 | .mode = 0666, |
| 1458 | .fops = &ucma_fops, |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1459 | }; |
| 1460 | |
| 1461 | static ssize_t show_abi_version(struct device *dev, |
| 1462 | struct device_attribute *attr, |
| 1463 | char *buf) |
| 1464 | { |
| 1465 | return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION); |
| 1466 | } |
| 1467 | static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); |
| 1468 | |
| 1469 | static int __init ucma_init(void) |
| 1470 | { |
| 1471 | int ret; |
| 1472 | |
| 1473 | ret = misc_register(&ucma_misc); |
| 1474 | if (ret) |
| 1475 | return ret; |
| 1476 | |
| 1477 | ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version); |
| 1478 | if (ret) { |
| 1479 | printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n"); |
Steve Wise | 97cb7e4 | 2010-08-13 20:56:34 +0000 | [diff] [blame] | 1480 | goto err1; |
| 1481 | } |
| 1482 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 1483 | ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table); |
Steve Wise | 97cb7e4 | 2010-08-13 20:56:34 +0000 | [diff] [blame] | 1484 | if (!ucma_ctl_table_hdr) { |
| 1485 | printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n"); |
| 1486 | ret = -ENOMEM; |
| 1487 | goto err2; |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1488 | } |
| 1489 | return 0; |
Steve Wise | 97cb7e4 | 2010-08-13 20:56:34 +0000 | [diff] [blame] | 1490 | err2: |
| 1491 | device_remove_file(ucma_misc.this_device, &dev_attr_abi_version); |
| 1492 | err1: |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1493 | misc_deregister(&ucma_misc); |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
| 1497 | static void __exit ucma_cleanup(void) |
| 1498 | { |
Eric W. Biederman | 5dd3df1 | 2012-04-19 13:24:33 +0000 | [diff] [blame] | 1499 | unregister_net_sysctl_table(ucma_ctl_table_hdr); |
Sean Hefty | 7521663 | 2006-11-30 16:53:41 -0800 | [diff] [blame] | 1500 | device_remove_file(ucma_misc.this_device, &dev_attr_abi_version); |
| 1501 | misc_deregister(&ucma_misc); |
| 1502 | idr_destroy(&ctx_idr); |
| 1503 | } |
| 1504 | |
| 1505 | module_init(ucma_init); |
| 1506 | module_exit(ucma_cleanup); |