blob: bb96d3c4b0f4e06d3b8eeb6b8c685cddc4418eba [file] [log] [blame]
Sean Hefty75216632006-11-30 16:53:41 -08001/*
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 Hefty88314e42007-11-14 00:29:50 -080034#include <linux/file.h>
Sean Hefty75216632006-11-30 16:53:41 -080035#include <linux/mutex.h>
36#include <linux/poll.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040037#include <linux/sched.h>
Sean Hefty75216632006-11-30 16:53:41 -080038#include <linux/idr.h>
39#include <linux/in.h>
40#include <linux/in6.h>
41#include <linux/miscdevice.h>
42
43#include <rdma/rdma_user_cm.h>
44#include <rdma/ib_marshall.h>
45#include <rdma/rdma_cm.h>
46
47MODULE_AUTHOR("Sean Hefty");
48MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
49MODULE_LICENSE("Dual BSD/GPL");
50
51enum {
52 UCMA_MAX_BACKLOG = 128
53};
54
55struct ucma_file {
56 struct mutex mut;
57 struct file *filp;
58 struct list_head ctx_list;
59 struct list_head event_list;
60 wait_queue_head_t poll_wait;
61};
62
63struct ucma_context {
64 int id;
65 struct completion comp;
66 atomic_t ref;
67 int events_reported;
68 int backlog;
69
70 struct ucma_file *file;
71 struct rdma_cm_id *cm_id;
72 u64 uid;
73
74 struct list_head list;
Sean Heftyc8f6a362007-02-15 17:00:18 -080075 struct list_head mc_list;
76};
77
78struct ucma_multicast {
79 struct ucma_context *ctx;
80 int id;
81 int events_reported;
82
83 u64 uid;
84 struct list_head list;
Roland Dreier3f446752008-08-04 11:02:14 -070085 struct sockaddr_storage addr;
Sean Hefty75216632006-11-30 16:53:41 -080086};
87
88struct ucma_event {
89 struct ucma_context *ctx;
Sean Heftyc8f6a362007-02-15 17:00:18 -080090 struct ucma_multicast *mc;
Sean Hefty75216632006-11-30 16:53:41 -080091 struct list_head list;
92 struct rdma_cm_id *cm_id;
93 struct rdma_ucm_event_resp resp;
94};
95
96static DEFINE_MUTEX(mut);
97static DEFINE_IDR(ctx_idr);
Sean Heftyc8f6a362007-02-15 17:00:18 -080098static DEFINE_IDR(multicast_idr);
Sean Hefty75216632006-11-30 16:53:41 -080099
100static inline struct ucma_context *_ucma_find_context(int id,
101 struct ucma_file *file)
102{
103 struct ucma_context *ctx;
104
105 ctx = idr_find(&ctx_idr, id);
106 if (!ctx)
107 ctx = ERR_PTR(-ENOENT);
108 else if (ctx->file != file)
109 ctx = ERR_PTR(-EINVAL);
110 return ctx;
111}
112
113static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
114{
115 struct ucma_context *ctx;
116
117 mutex_lock(&mut);
118 ctx = _ucma_find_context(id, file);
119 if (!IS_ERR(ctx))
120 atomic_inc(&ctx->ref);
121 mutex_unlock(&mut);
122 return ctx;
123}
124
125static void ucma_put_ctx(struct ucma_context *ctx)
126{
127 if (atomic_dec_and_test(&ctx->ref))
128 complete(&ctx->comp);
129}
130
131static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
132{
133 struct ucma_context *ctx;
134 int ret;
135
136 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
137 if (!ctx)
138 return NULL;
139
140 atomic_set(&ctx->ref, 1);
141 init_completion(&ctx->comp);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800142 INIT_LIST_HEAD(&ctx->mc_list);
Sean Hefty75216632006-11-30 16:53:41 -0800143 ctx->file = file;
144
145 do {
146 ret = idr_pre_get(&ctx_idr, GFP_KERNEL);
147 if (!ret)
148 goto error;
149
150 mutex_lock(&mut);
151 ret = idr_get_new(&ctx_idr, ctx, &ctx->id);
152 mutex_unlock(&mut);
153 } while (ret == -EAGAIN);
154
155 if (ret)
156 goto error;
157
158 list_add_tail(&ctx->list, &file->ctx_list);
159 return ctx;
160
161error:
162 kfree(ctx);
163 return NULL;
164}
165
Sean Heftyc8f6a362007-02-15 17:00:18 -0800166static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
167{
168 struct ucma_multicast *mc;
169 int ret;
170
171 mc = kzalloc(sizeof(*mc), GFP_KERNEL);
172 if (!mc)
173 return NULL;
174
175 do {
176 ret = idr_pre_get(&multicast_idr, GFP_KERNEL);
177 if (!ret)
178 goto error;
179
180 mutex_lock(&mut);
181 ret = idr_get_new(&multicast_idr, mc, &mc->id);
182 mutex_unlock(&mut);
183 } while (ret == -EAGAIN);
184
185 if (ret)
186 goto error;
187
188 mc->ctx = ctx;
189 list_add_tail(&mc->list, &ctx->mc_list);
190 return mc;
191
192error:
193 kfree(mc);
194 return NULL;
195}
196
Sean Hefty75216632006-11-30 16:53:41 -0800197static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
198 struct rdma_conn_param *src)
199{
200 if (src->private_data_len)
201 memcpy(dst->private_data, src->private_data,
202 src->private_data_len);
203 dst->private_data_len = src->private_data_len;
204 dst->responder_resources =src->responder_resources;
205 dst->initiator_depth = src->initiator_depth;
206 dst->flow_control = src->flow_control;
207 dst->retry_count = src->retry_count;
208 dst->rnr_retry_count = src->rnr_retry_count;
209 dst->srq = src->srq;
210 dst->qp_num = src->qp_num;
211}
212
213static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
214 struct rdma_ud_param *src)
215{
216 if (src->private_data_len)
217 memcpy(dst->private_data, src->private_data,
218 src->private_data_len);
219 dst->private_data_len = src->private_data_len;
220 ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
221 dst->qp_num = src->qp_num;
222 dst->qkey = src->qkey;
223}
224
225static void ucma_set_event_context(struct ucma_context *ctx,
226 struct rdma_cm_event *event,
227 struct ucma_event *uevent)
228{
229 uevent->ctx = ctx;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800230 switch (event->event) {
231 case RDMA_CM_EVENT_MULTICAST_JOIN:
232 case RDMA_CM_EVENT_MULTICAST_ERROR:
233 uevent->mc = (struct ucma_multicast *)
234 event->param.ud.private_data;
235 uevent->resp.uid = uevent->mc->uid;
236 uevent->resp.id = uevent->mc->id;
237 break;
238 default:
239 uevent->resp.uid = ctx->uid;
240 uevent->resp.id = ctx->id;
241 break;
242 }
Sean Hefty75216632006-11-30 16:53:41 -0800243}
244
245static int ucma_event_handler(struct rdma_cm_id *cm_id,
246 struct rdma_cm_event *event)
247{
248 struct ucma_event *uevent;
249 struct ucma_context *ctx = cm_id->context;
250 int ret = 0;
251
252 uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
253 if (!uevent)
254 return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
255
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 Heftyc8f6a362007-02-15 17:00:18 -0800260 if (cm_id->ps == RDMA_PS_UDP || cm_id->ps == RDMA_PS_IPOIB)
Sean Hefty75216632006-11-30 16:53:41 -0800261 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
266 mutex_lock(&ctx->file->mut);
267 if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
268 if (!ctx->backlog) {
Sean Hefty34928562007-03-06 11:58:32 -0800269 ret = -ENOMEM;
Sean Hefty30a5ec92006-12-14 11:22:19 -0800270 kfree(uevent);
Sean Hefty75216632006-11-30 16:53:41 -0800271 goto out;
272 }
273 ctx->backlog--;
Sean Hefty0cefcf02007-01-05 12:35:15 -0800274 } else if (!ctx->uid) {
275 /*
276 * We ignore events for new connections until userspace has set
277 * their context. This can only happen if an error occurs on a
278 * new connection before the user accepts it. This is okay,
279 * since the accept will just fail later.
280 */
281 kfree(uevent);
282 goto out;
Sean Hefty75216632006-11-30 16:53:41 -0800283 }
Sean Hefty0cefcf02007-01-05 12:35:15 -0800284
Sean Hefty75216632006-11-30 16:53:41 -0800285 list_add_tail(&uevent->list, &ctx->file->event_list);
286 wake_up_interruptible(&ctx->file->poll_wait);
287out:
288 mutex_unlock(&ctx->file->mut);
289 return ret;
290}
291
292static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
293 int in_len, int out_len)
294{
295 struct ucma_context *ctx;
296 struct rdma_ucm_get_event cmd;
297 struct ucma_event *uevent;
298 int ret = 0;
299 DEFINE_WAIT(wait);
300
301 if (out_len < sizeof uevent->resp)
302 return -ENOSPC;
303
304 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
305 return -EFAULT;
306
307 mutex_lock(&file->mut);
308 while (list_empty(&file->event_list)) {
Sean Hefty75216632006-11-30 16:53:41 -0800309 mutex_unlock(&file->mut);
Sean Hefty75216632006-11-30 16:53:41 -0800310
Sean Heftyd92f7642007-04-05 10:49:51 -0700311 if (file->filp->f_flags & O_NONBLOCK)
312 return -EAGAIN;
313
314 if (wait_event_interruptible(file->poll_wait,
315 !list_empty(&file->event_list)))
316 return -ERESTARTSYS;
317
318 mutex_lock(&file->mut);
319 }
Sean Hefty75216632006-11-30 16:53:41 -0800320
321 uevent = list_entry(file->event_list.next, struct ucma_event, list);
322
323 if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
324 ctx = ucma_alloc_ctx(file);
325 if (!ctx) {
326 ret = -ENOMEM;
327 goto done;
328 }
329 uevent->ctx->backlog++;
330 ctx->cm_id = uevent->cm_id;
331 ctx->cm_id->context = ctx;
332 uevent->resp.id = ctx->id;
333 }
334
335 if (copy_to_user((void __user *)(unsigned long)cmd.response,
336 &uevent->resp, sizeof uevent->resp)) {
337 ret = -EFAULT;
338 goto done;
339 }
340
341 list_del(&uevent->list);
342 uevent->ctx->events_reported++;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800343 if (uevent->mc)
344 uevent->mc->events_reported++;
Sean Hefty75216632006-11-30 16:53:41 -0800345 kfree(uevent);
346done:
347 mutex_unlock(&file->mut);
348 return ret;
349}
350
351static ssize_t ucma_create_id(struct ucma_file *file,
352 const char __user *inbuf,
353 int in_len, int out_len)
354{
355 struct rdma_ucm_create_id cmd;
356 struct rdma_ucm_create_id_resp resp;
357 struct ucma_context *ctx;
358 int ret;
359
360 if (out_len < sizeof(resp))
361 return -ENOSPC;
362
363 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
364 return -EFAULT;
365
366 mutex_lock(&file->mut);
367 ctx = ucma_alloc_ctx(file);
368 mutex_unlock(&file->mut);
369 if (!ctx)
370 return -ENOMEM;
371
372 ctx->uid = cmd.uid;
373 ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps);
374 if (IS_ERR(ctx->cm_id)) {
375 ret = PTR_ERR(ctx->cm_id);
376 goto err1;
377 }
378
379 resp.id = ctx->id;
380 if (copy_to_user((void __user *)(unsigned long)cmd.response,
381 &resp, sizeof(resp))) {
382 ret = -EFAULT;
383 goto err2;
384 }
385 return 0;
386
387err2:
388 rdma_destroy_id(ctx->cm_id);
389err1:
390 mutex_lock(&mut);
391 idr_remove(&ctx_idr, ctx->id);
392 mutex_unlock(&mut);
393 kfree(ctx);
394 return ret;
395}
396
Sean Heftyc8f6a362007-02-15 17:00:18 -0800397static void ucma_cleanup_multicast(struct ucma_context *ctx)
398{
399 struct ucma_multicast *mc, *tmp;
400
401 mutex_lock(&mut);
402 list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
403 list_del(&mc->list);
404 idr_remove(&multicast_idr, mc->id);
405 kfree(mc);
406 }
407 mutex_unlock(&mut);
408}
409
Sean Hefty75216632006-11-30 16:53:41 -0800410static void ucma_cleanup_events(struct ucma_context *ctx)
411{
412 struct ucma_event *uevent, *tmp;
413
414 list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
415 if (uevent->ctx != ctx)
416 continue;
417
418 list_del(&uevent->list);
419
420 /* clear incoming connections. */
421 if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
422 rdma_destroy_id(uevent->cm_id);
423
424 kfree(uevent);
425 }
426}
427
Sean Heftyc8f6a362007-02-15 17:00:18 -0800428static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
429{
430 struct ucma_event *uevent, *tmp;
431
432 list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
433 if (uevent->mc != mc)
434 continue;
435
436 list_del(&uevent->list);
437 kfree(uevent);
438 }
439}
440
Sean Hefty75216632006-11-30 16:53:41 -0800441static int ucma_free_ctx(struct ucma_context *ctx)
442{
443 int events_reported;
444
445 /* No new events will be generated after destroying the id. */
446 rdma_destroy_id(ctx->cm_id);
447
Sean Heftyc8f6a362007-02-15 17:00:18 -0800448 ucma_cleanup_multicast(ctx);
449
Sean Hefty75216632006-11-30 16:53:41 -0800450 /* Cleanup events not yet reported to the user. */
451 mutex_lock(&ctx->file->mut);
452 ucma_cleanup_events(ctx);
453 list_del(&ctx->list);
454 mutex_unlock(&ctx->file->mut);
455
456 events_reported = ctx->events_reported;
457 kfree(ctx);
458 return events_reported;
459}
460
461static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
462 int in_len, int out_len)
463{
464 struct rdma_ucm_destroy_id cmd;
465 struct rdma_ucm_destroy_id_resp resp;
466 struct ucma_context *ctx;
467 int ret = 0;
468
469 if (out_len < sizeof(resp))
470 return -ENOSPC;
471
472 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
473 return -EFAULT;
474
475 mutex_lock(&mut);
476 ctx = _ucma_find_context(cmd.id, file);
477 if (!IS_ERR(ctx))
478 idr_remove(&ctx_idr, ctx->id);
479 mutex_unlock(&mut);
480
481 if (IS_ERR(ctx))
482 return PTR_ERR(ctx);
483
484 ucma_put_ctx(ctx);
485 wait_for_completion(&ctx->comp);
486 resp.events_reported = ucma_free_ctx(ctx);
487
488 if (copy_to_user((void __user *)(unsigned long)cmd.response,
489 &resp, sizeof(resp)))
490 ret = -EFAULT;
491
492 return ret;
493}
494
495static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
496 int in_len, int out_len)
497{
498 struct rdma_ucm_bind_addr cmd;
499 struct ucma_context *ctx;
500 int ret;
501
502 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
503 return -EFAULT;
504
505 ctx = ucma_get_ctx(file, cmd.id);
506 if (IS_ERR(ctx))
507 return PTR_ERR(ctx);
508
509 ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
510 ucma_put_ctx(ctx);
511 return ret;
512}
513
514static ssize_t ucma_resolve_addr(struct ucma_file *file,
515 const char __user *inbuf,
516 int in_len, int out_len)
517{
518 struct rdma_ucm_resolve_addr cmd;
519 struct ucma_context *ctx;
520 int ret;
521
522 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
523 return -EFAULT;
524
525 ctx = ucma_get_ctx(file, cmd.id);
526 if (IS_ERR(ctx))
527 return PTR_ERR(ctx);
528
529 ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
530 (struct sockaddr *) &cmd.dst_addr,
531 cmd.timeout_ms);
532 ucma_put_ctx(ctx);
533 return ret;
534}
535
536static ssize_t ucma_resolve_route(struct ucma_file *file,
537 const char __user *inbuf,
538 int in_len, int out_len)
539{
540 struct rdma_ucm_resolve_route cmd;
541 struct ucma_context *ctx;
542 int ret;
543
544 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
545 return -EFAULT;
546
547 ctx = ucma_get_ctx(file, cmd.id);
548 if (IS_ERR(ctx))
549 return PTR_ERR(ctx);
550
551 ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
552 ucma_put_ctx(ctx);
553 return ret;
554}
555
556static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
557 struct rdma_route *route)
558{
559 struct rdma_dev_addr *dev_addr;
560
561 resp->num_paths = route->num_paths;
562 switch (route->num_paths) {
563 case 0:
564 dev_addr = &route->addr.dev_addr;
565 ib_addr_get_dgid(dev_addr,
566 (union ib_gid *) &resp->ib_route[0].dgid);
567 ib_addr_get_sgid(dev_addr,
568 (union ib_gid *) &resp->ib_route[0].sgid);
569 resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
570 break;
571 case 2:
572 ib_copy_path_rec_to_user(&resp->ib_route[1],
573 &route->path_rec[1]);
574 /* fall through */
575 case 1:
576 ib_copy_path_rec_to_user(&resp->ib_route[0],
577 &route->path_rec[0]);
578 break;
579 default:
580 break;
581 }
582}
583
584static ssize_t ucma_query_route(struct ucma_file *file,
585 const char __user *inbuf,
586 int in_len, int out_len)
587{
588 struct rdma_ucm_query_route cmd;
589 struct rdma_ucm_query_route_resp resp;
590 struct ucma_context *ctx;
591 struct sockaddr *addr;
592 int ret = 0;
593
594 if (out_len < sizeof(resp))
595 return -ENOSPC;
596
597 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
598 return -EFAULT;
599
600 ctx = ucma_get_ctx(file, cmd.id);
601 if (IS_ERR(ctx))
602 return PTR_ERR(ctx);
603
604 memset(&resp, 0, sizeof resp);
Roland Dreier3f446752008-08-04 11:02:14 -0700605 addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
Sean Hefty75216632006-11-30 16:53:41 -0800606 memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
607 sizeof(struct sockaddr_in) :
608 sizeof(struct sockaddr_in6));
Roland Dreier3f446752008-08-04 11:02:14 -0700609 addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
Sean Hefty75216632006-11-30 16:53:41 -0800610 memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
611 sizeof(struct sockaddr_in) :
612 sizeof(struct sockaddr_in6));
613 if (!ctx->cm_id->device)
614 goto out;
615
Roland Dreier9cda7792008-04-16 21:01:07 -0700616 resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
Sean Hefty75216632006-11-30 16:53:41 -0800617 resp.port_num = ctx->cm_id->port_num;
618 switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
619 case RDMA_TRANSPORT_IB:
620 ucma_copy_ib_route(&resp, &ctx->cm_id->route);
621 break;
622 default:
623 break;
624 }
625
626out:
627 if (copy_to_user((void __user *)(unsigned long)cmd.response,
628 &resp, sizeof(resp)))
629 ret = -EFAULT;
630
631 ucma_put_ctx(ctx);
632 return ret;
633}
634
635static void ucma_copy_conn_param(struct rdma_conn_param *dst,
636 struct rdma_ucm_conn_param *src)
637{
638 dst->private_data = src->private_data;
639 dst->private_data_len = src->private_data_len;
640 dst->responder_resources =src->responder_resources;
641 dst->initiator_depth = src->initiator_depth;
642 dst->flow_control = src->flow_control;
643 dst->retry_count = src->retry_count;
644 dst->rnr_retry_count = src->rnr_retry_count;
645 dst->srq = src->srq;
646 dst->qp_num = src->qp_num;
647}
648
649static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
650 int in_len, int out_len)
651{
652 struct rdma_ucm_connect cmd;
653 struct rdma_conn_param conn_param;
654 struct ucma_context *ctx;
655 int ret;
656
657 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
658 return -EFAULT;
659
660 if (!cmd.conn_param.valid)
661 return -EINVAL;
662
663 ctx = ucma_get_ctx(file, cmd.id);
664 if (IS_ERR(ctx))
665 return PTR_ERR(ctx);
666
667 ucma_copy_conn_param(&conn_param, &cmd.conn_param);
668 ret = rdma_connect(ctx->cm_id, &conn_param);
669 ucma_put_ctx(ctx);
670 return ret;
671}
672
673static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
674 int in_len, int out_len)
675{
676 struct rdma_ucm_listen cmd;
677 struct ucma_context *ctx;
678 int ret;
679
680 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
681 return -EFAULT;
682
683 ctx = ucma_get_ctx(file, cmd.id);
684 if (IS_ERR(ctx))
685 return PTR_ERR(ctx);
686
687 ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
688 cmd.backlog : UCMA_MAX_BACKLOG;
689 ret = rdma_listen(ctx->cm_id, ctx->backlog);
690 ucma_put_ctx(ctx);
691 return ret;
692}
693
694static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
695 int in_len, int out_len)
696{
697 struct rdma_ucm_accept cmd;
698 struct rdma_conn_param conn_param;
699 struct ucma_context *ctx;
700 int ret;
701
702 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
703 return -EFAULT;
704
705 ctx = ucma_get_ctx(file, cmd.id);
706 if (IS_ERR(ctx))
707 return PTR_ERR(ctx);
708
709 if (cmd.conn_param.valid) {
710 ctx->uid = cmd.uid;
711 ucma_copy_conn_param(&conn_param, &cmd.conn_param);
712 ret = rdma_accept(ctx->cm_id, &conn_param);
713 } else
714 ret = rdma_accept(ctx->cm_id, NULL);
715
716 ucma_put_ctx(ctx);
717 return ret;
718}
719
720static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
721 int in_len, int out_len)
722{
723 struct rdma_ucm_reject cmd;
724 struct ucma_context *ctx;
725 int ret;
726
727 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
728 return -EFAULT;
729
730 ctx = ucma_get_ctx(file, cmd.id);
731 if (IS_ERR(ctx))
732 return PTR_ERR(ctx);
733
734 ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
735 ucma_put_ctx(ctx);
736 return ret;
737}
738
739static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
740 int in_len, int out_len)
741{
742 struct rdma_ucm_disconnect cmd;
743 struct ucma_context *ctx;
744 int ret;
745
746 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
747 return -EFAULT;
748
749 ctx = ucma_get_ctx(file, cmd.id);
750 if (IS_ERR(ctx))
751 return PTR_ERR(ctx);
752
753 ret = rdma_disconnect(ctx->cm_id);
754 ucma_put_ctx(ctx);
755 return ret;
756}
757
758static ssize_t ucma_init_qp_attr(struct ucma_file *file,
759 const char __user *inbuf,
760 int in_len, int out_len)
761{
762 struct rdma_ucm_init_qp_attr cmd;
763 struct ib_uverbs_qp_attr resp;
764 struct ucma_context *ctx;
765 struct ib_qp_attr qp_attr;
766 int ret;
767
768 if (out_len < sizeof(resp))
769 return -ENOSPC;
770
771 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
772 return -EFAULT;
773
774 ctx = ucma_get_ctx(file, cmd.id);
775 if (IS_ERR(ctx))
776 return PTR_ERR(ctx);
777
778 resp.qp_attr_mask = 0;
779 memset(&qp_attr, 0, sizeof qp_attr);
780 qp_attr.qp_state = cmd.qp_state;
781 ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
782 if (ret)
783 goto out;
784
785 ib_copy_qp_attr_to_user(&resp, &qp_attr);
786 if (copy_to_user((void __user *)(unsigned long)cmd.response,
787 &resp, sizeof(resp)))
788 ret = -EFAULT;
789
790out:
791 ucma_put_ctx(ctx);
792 return ret;
793}
794
Sean Hefty7ce86402007-08-08 15:51:13 -0700795static int ucma_set_option_id(struct ucma_context *ctx, int optname,
796 void *optval, size_t optlen)
797{
798 int ret = 0;
799
800 switch (optname) {
801 case RDMA_OPTION_ID_TOS:
802 if (optlen != sizeof(u8)) {
803 ret = -EINVAL;
804 break;
805 }
806 rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
807 break;
808 default:
809 ret = -ENOSYS;
810 }
811
812 return ret;
813}
814
815static int ucma_set_option_level(struct ucma_context *ctx, int level,
816 int optname, void *optval, size_t optlen)
817{
818 int ret;
819
820 switch (level) {
821 case RDMA_OPTION_ID:
822 ret = ucma_set_option_id(ctx, optname, optval, optlen);
823 break;
824 default:
825 ret = -ENOSYS;
826 }
827
828 return ret;
829}
830
831static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
832 int in_len, int out_len)
833{
834 struct rdma_ucm_set_option cmd;
835 struct ucma_context *ctx;
836 void *optval;
837 int ret;
838
839 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
840 return -EFAULT;
841
842 ctx = ucma_get_ctx(file, cmd.id);
843 if (IS_ERR(ctx))
844 return PTR_ERR(ctx);
845
846 optval = kmalloc(cmd.optlen, GFP_KERNEL);
847 if (!optval) {
848 ret = -ENOMEM;
849 goto out1;
850 }
851
852 if (copy_from_user(optval, (void __user *) (unsigned long) cmd.optval,
853 cmd.optlen)) {
854 ret = -EFAULT;
855 goto out2;
856 }
857
858 ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
859 cmd.optlen);
860out2:
861 kfree(optval);
862out1:
863 ucma_put_ctx(ctx);
864 return ret;
865}
866
Sean Hefty75216632006-11-30 16:53:41 -0800867static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
868 int in_len, int out_len)
869{
870 struct rdma_ucm_notify cmd;
871 struct ucma_context *ctx;
872 int ret;
873
874 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
875 return -EFAULT;
876
877 ctx = ucma_get_ctx(file, cmd.id);
878 if (IS_ERR(ctx))
879 return PTR_ERR(ctx);
880
881 ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
882 ucma_put_ctx(ctx);
883 return ret;
884}
885
Sean Heftyc8f6a362007-02-15 17:00:18 -0800886static ssize_t ucma_join_multicast(struct ucma_file *file,
887 const char __user *inbuf,
888 int in_len, int out_len)
889{
890 struct rdma_ucm_join_mcast cmd;
891 struct rdma_ucm_create_id_resp resp;
892 struct ucma_context *ctx;
893 struct ucma_multicast *mc;
894 int ret;
895
896 if (out_len < sizeof(resp))
897 return -ENOSPC;
898
899 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
900 return -EFAULT;
901
902 ctx = ucma_get_ctx(file, cmd.id);
903 if (IS_ERR(ctx))
904 return PTR_ERR(ctx);
905
906 mutex_lock(&file->mut);
907 mc = ucma_alloc_multicast(ctx);
Julien Brunel6aea9382008-10-10 12:00:19 -0700908 if (!mc) {
909 ret = -ENOMEM;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800910 goto err1;
911 }
912
913 mc->uid = cmd.uid;
914 memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
Roland Dreier3f446752008-08-04 11:02:14 -0700915 ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800916 if (ret)
917 goto err2;
918
919 resp.id = mc->id;
920 if (copy_to_user((void __user *)(unsigned long)cmd.response,
921 &resp, sizeof(resp))) {
922 ret = -EFAULT;
923 goto err3;
924 }
925
926 mutex_unlock(&file->mut);
927 ucma_put_ctx(ctx);
928 return 0;
929
930err3:
Roland Dreier3f446752008-08-04 11:02:14 -0700931 rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800932 ucma_cleanup_mc_events(mc);
933err2:
934 mutex_lock(&mut);
935 idr_remove(&multicast_idr, mc->id);
936 mutex_unlock(&mut);
937 list_del(&mc->list);
938 kfree(mc);
939err1:
940 mutex_unlock(&file->mut);
941 ucma_put_ctx(ctx);
942 return ret;
943}
944
945static ssize_t ucma_leave_multicast(struct ucma_file *file,
946 const char __user *inbuf,
947 int in_len, int out_len)
948{
949 struct rdma_ucm_destroy_id cmd;
950 struct rdma_ucm_destroy_id_resp resp;
951 struct ucma_multicast *mc;
952 int ret = 0;
953
954 if (out_len < sizeof(resp))
955 return -ENOSPC;
956
957 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
958 return -EFAULT;
959
960 mutex_lock(&mut);
961 mc = idr_find(&multicast_idr, cmd.id);
962 if (!mc)
963 mc = ERR_PTR(-ENOENT);
964 else if (mc->ctx->file != file)
965 mc = ERR_PTR(-EINVAL);
966 else {
967 idr_remove(&multicast_idr, mc->id);
968 atomic_inc(&mc->ctx->ref);
969 }
970 mutex_unlock(&mut);
971
972 if (IS_ERR(mc)) {
973 ret = PTR_ERR(mc);
974 goto out;
975 }
976
Roland Dreier3f446752008-08-04 11:02:14 -0700977 rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800978 mutex_lock(&mc->ctx->file->mut);
979 ucma_cleanup_mc_events(mc);
980 list_del(&mc->list);
981 mutex_unlock(&mc->ctx->file->mut);
982
983 ucma_put_ctx(mc->ctx);
984 resp.events_reported = mc->events_reported;
985 kfree(mc);
986
987 if (copy_to_user((void __user *)(unsigned long)cmd.response,
988 &resp, sizeof(resp)))
989 ret = -EFAULT;
990out:
991 return ret;
992}
993
Sean Hefty88314e42007-11-14 00:29:50 -0800994static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
995{
996 /* Acquire mutex's based on pointer comparison to prevent deadlock. */
997 if (file1 < file2) {
998 mutex_lock(&file1->mut);
999 mutex_lock(&file2->mut);
1000 } else {
1001 mutex_lock(&file2->mut);
1002 mutex_lock(&file1->mut);
1003 }
1004}
1005
1006static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
1007{
1008 if (file1 < file2) {
1009 mutex_unlock(&file2->mut);
1010 mutex_unlock(&file1->mut);
1011 } else {
1012 mutex_unlock(&file1->mut);
1013 mutex_unlock(&file2->mut);
1014 }
1015}
1016
1017static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
1018{
1019 struct ucma_event *uevent, *tmp;
1020
1021 list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
1022 if (uevent->ctx == ctx)
1023 list_move_tail(&uevent->list, &file->event_list);
1024}
1025
1026static ssize_t ucma_migrate_id(struct ucma_file *new_file,
1027 const char __user *inbuf,
1028 int in_len, int out_len)
1029{
1030 struct rdma_ucm_migrate_id cmd;
1031 struct rdma_ucm_migrate_resp resp;
1032 struct ucma_context *ctx;
1033 struct file *filp;
1034 struct ucma_file *cur_file;
1035 int ret = 0;
1036
1037 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1038 return -EFAULT;
1039
1040 /* Get current fd to protect against it being closed */
1041 filp = fget(cmd.fd);
1042 if (!filp)
1043 return -ENOENT;
1044
1045 /* Validate current fd and prevent destruction of id. */
1046 ctx = ucma_get_ctx(filp->private_data, cmd.id);
1047 if (IS_ERR(ctx)) {
1048 ret = PTR_ERR(ctx);
1049 goto file_put;
1050 }
1051
1052 cur_file = ctx->file;
1053 if (cur_file == new_file) {
1054 resp.events_reported = ctx->events_reported;
1055 goto response;
1056 }
1057
1058 /*
1059 * Migrate events between fd's, maintaining order, and avoiding new
1060 * events being added before existing events.
1061 */
1062 ucma_lock_files(cur_file, new_file);
1063 mutex_lock(&mut);
1064
1065 list_move_tail(&ctx->list, &new_file->ctx_list);
1066 ucma_move_events(ctx, new_file);
1067 ctx->file = new_file;
1068 resp.events_reported = ctx->events_reported;
1069
1070 mutex_unlock(&mut);
1071 ucma_unlock_files(cur_file, new_file);
1072
1073response:
1074 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1075 &resp, sizeof(resp)))
1076 ret = -EFAULT;
1077
1078 ucma_put_ctx(ctx);
1079file_put:
1080 fput(filp);
1081 return ret;
1082}
1083
Sean Hefty75216632006-11-30 16:53:41 -08001084static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
1085 const char __user *inbuf,
1086 int in_len, int out_len) = {
1087 [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
1088 [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
1089 [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
1090 [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
1091 [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
1092 [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
1093 [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
1094 [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
1095 [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
1096 [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
1097 [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
1098 [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
1099 [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
1100 [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
Sean Hefty7ce86402007-08-08 15:51:13 -07001101 [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
Sean Hefty75216632006-11-30 16:53:41 -08001102 [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
Sean Heftyc8f6a362007-02-15 17:00:18 -08001103 [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
1104 [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
Sean Hefty88314e42007-11-14 00:29:50 -08001105 [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
Sean Hefty75216632006-11-30 16:53:41 -08001106};
1107
1108static ssize_t ucma_write(struct file *filp, const char __user *buf,
1109 size_t len, loff_t *pos)
1110{
1111 struct ucma_file *file = filp->private_data;
1112 struct rdma_ucm_cmd_hdr hdr;
1113 ssize_t ret;
1114
1115 if (len < sizeof(hdr))
1116 return -EINVAL;
1117
1118 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1119 return -EFAULT;
1120
1121 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
1122 return -EINVAL;
1123
1124 if (hdr.in + sizeof(hdr) > len)
1125 return -EINVAL;
1126
1127 if (!ucma_cmd_table[hdr.cmd])
1128 return -ENOSYS;
1129
1130 ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
1131 if (!ret)
1132 ret = len;
1133
1134 return ret;
1135}
1136
1137static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
1138{
1139 struct ucma_file *file = filp->private_data;
1140 unsigned int mask = 0;
1141
1142 poll_wait(filp, &file->poll_wait, wait);
1143
1144 if (!list_empty(&file->event_list))
1145 mask = POLLIN | POLLRDNORM;
1146
1147 return mask;
1148}
1149
Roland Dreierf7a6117e2008-07-24 20:36:59 -07001150/*
1151 * ucma_open() does not need the BKL:
1152 *
1153 * - no global state is referred to;
1154 * - there is no ioctl method to race against;
1155 * - no further module initialization is required for open to work
1156 * after the device is registered.
1157 */
Sean Hefty75216632006-11-30 16:53:41 -08001158static int ucma_open(struct inode *inode, struct file *filp)
1159{
1160 struct ucma_file *file;
1161
1162 file = kmalloc(sizeof *file, GFP_KERNEL);
1163 if (!file)
1164 return -ENOMEM;
1165
1166 INIT_LIST_HEAD(&file->event_list);
1167 INIT_LIST_HEAD(&file->ctx_list);
1168 init_waitqueue_head(&file->poll_wait);
1169 mutex_init(&file->mut);
1170
1171 filp->private_data = file;
1172 file->filp = filp;
1173 return 0;
1174}
1175
1176static int ucma_close(struct inode *inode, struct file *filp)
1177{
1178 struct ucma_file *file = filp->private_data;
1179 struct ucma_context *ctx, *tmp;
1180
1181 mutex_lock(&file->mut);
1182 list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
1183 mutex_unlock(&file->mut);
1184
1185 mutex_lock(&mut);
1186 idr_remove(&ctx_idr, ctx->id);
1187 mutex_unlock(&mut);
1188
1189 ucma_free_ctx(ctx);
1190 mutex_lock(&file->mut);
1191 }
1192 mutex_unlock(&file->mut);
1193 kfree(file);
1194 return 0;
1195}
1196
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001197static const struct file_operations ucma_fops = {
Sean Hefty75216632006-11-30 16:53:41 -08001198 .owner = THIS_MODULE,
1199 .open = ucma_open,
1200 .release = ucma_close,
1201 .write = ucma_write,
1202 .poll = ucma_poll,
1203};
1204
1205static struct miscdevice ucma_misc = {
1206 .minor = MISC_DYNAMIC_MINOR,
1207 .name = "rdma_cm",
1208 .fops = &ucma_fops,
1209};
1210
1211static ssize_t show_abi_version(struct device *dev,
1212 struct device_attribute *attr,
1213 char *buf)
1214{
1215 return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
1216}
1217static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1218
1219static int __init ucma_init(void)
1220{
1221 int ret;
1222
1223 ret = misc_register(&ucma_misc);
1224 if (ret)
1225 return ret;
1226
1227 ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
1228 if (ret) {
1229 printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
1230 goto err;
1231 }
1232 return 0;
1233err:
1234 misc_deregister(&ucma_misc);
1235 return ret;
1236}
1237
1238static void __exit ucma_cleanup(void)
1239{
1240 device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1241 misc_deregister(&ucma_misc);
1242 idr_destroy(&ctx_idr);
1243}
1244
1245module_init(ucma_init);
1246module_exit(ucma_cleanup);