xref: /openbmc/linux/drivers/infiniband/core/ucma.c (revision 64c70b1c)
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>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/idr.h>
37 #include <linux/in.h>
38 #include <linux/in6.h>
39 #include <linux/miscdevice.h>
40 
41 #include <rdma/rdma_user_cm.h>
42 #include <rdma/ib_marshall.h>
43 #include <rdma/rdma_cm.h>
44 
45 MODULE_AUTHOR("Sean Hefty");
46 MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
47 MODULE_LICENSE("Dual BSD/GPL");
48 
49 enum {
50 	UCMA_MAX_BACKLOG	= 128
51 };
52 
53 struct ucma_file {
54 	struct mutex		mut;
55 	struct file		*filp;
56 	struct list_head	ctx_list;
57 	struct list_head	event_list;
58 	wait_queue_head_t	poll_wait;
59 };
60 
61 struct ucma_context {
62 	int			id;
63 	struct completion	comp;
64 	atomic_t		ref;
65 	int			events_reported;
66 	int			backlog;
67 
68 	struct ucma_file	*file;
69 	struct rdma_cm_id	*cm_id;
70 	u64			uid;
71 
72 	struct list_head	list;
73 	struct list_head	mc_list;
74 };
75 
76 struct ucma_multicast {
77 	struct ucma_context	*ctx;
78 	int			id;
79 	int			events_reported;
80 
81 	u64			uid;
82 	struct list_head	list;
83 	struct sockaddr		addr;
84 	u8			pad[sizeof(struct sockaddr_in6) -
85 				    sizeof(struct sockaddr)];
86 };
87 
88 struct ucma_event {
89 	struct ucma_context	*ctx;
90 	struct ucma_multicast	*mc;
91 	struct list_head	list;
92 	struct rdma_cm_id	*cm_id;
93 	struct rdma_ucm_event_resp resp;
94 };
95 
96 static DEFINE_MUTEX(mut);
97 static DEFINE_IDR(ctx_idr);
98 static DEFINE_IDR(multicast_idr);
99 
100 static 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 
113 static 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 
125 static void ucma_put_ctx(struct ucma_context *ctx)
126 {
127 	if (atomic_dec_and_test(&ctx->ref))
128 		complete(&ctx->comp);
129 }
130 
131 static 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);
142 	INIT_LIST_HEAD(&ctx->mc_list);
143 	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 
161 error:
162 	kfree(ctx);
163 	return NULL;
164 }
165 
166 static 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 
192 error:
193 	kfree(mc);
194 	return NULL;
195 }
196 
197 static 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 
213 static 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 
225 static 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;
230 	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 	}
243 }
244 
245 static 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;
260 	if (cm_id->ps == RDMA_PS_UDP || cm_id->ps == RDMA_PS_IPOIB)
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 
266 	mutex_lock(&ctx->file->mut);
267 	if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
268 		if (!ctx->backlog) {
269 			ret = -ENOMEM;
270 			kfree(uevent);
271 			goto out;
272 		}
273 		ctx->backlog--;
274 	} 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;
283 	}
284 
285 	list_add_tail(&uevent->list, &ctx->file->event_list);
286 	wake_up_interruptible(&ctx->file->poll_wait);
287 out:
288 	mutex_unlock(&ctx->file->mut);
289 	return ret;
290 }
291 
292 static 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)) {
309 		mutex_unlock(&file->mut);
310 
311 		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 	}
320 
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++;
343 	if (uevent->mc)
344 		uevent->mc->events_reported++;
345 	kfree(uevent);
346 done:
347 	mutex_unlock(&file->mut);
348 	return ret;
349 }
350 
351 static 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 
387 err2:
388 	rdma_destroy_id(ctx->cm_id);
389 err1:
390 	mutex_lock(&mut);
391 	idr_remove(&ctx_idr, ctx->id);
392 	mutex_unlock(&mut);
393 	kfree(ctx);
394 	return ret;
395 }
396 
397 static 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 
410 static 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 
428 static 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 
441 static 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 
448 	ucma_cleanup_multicast(ctx);
449 
450 	/* 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 
461 static 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 
495 static 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 
514 static 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 
536 static 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 
556 static 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 
584 static 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);
605 	addr = &ctx->cm_id->route.addr.src_addr;
606 	memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
607 				     sizeof(struct sockaddr_in) :
608 				     sizeof(struct sockaddr_in6));
609 	addr = &ctx->cm_id->route.addr.dst_addr;
610 	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 
616 	resp.node_guid = ctx->cm_id->device->node_guid;
617 	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 
626 out:
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 
635 static 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 
649 static 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 
673 static 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 
694 static 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 
720 static 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 
739 static 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 
758 static 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 
790 out:
791 	ucma_put_ctx(ctx);
792 	return ret;
793 }
794 
795 static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
796 			   int in_len, int out_len)
797 {
798 	struct rdma_ucm_notify cmd;
799 	struct ucma_context *ctx;
800 	int ret;
801 
802 	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
803 		return -EFAULT;
804 
805 	ctx = ucma_get_ctx(file, cmd.id);
806 	if (IS_ERR(ctx))
807 		return PTR_ERR(ctx);
808 
809 	ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
810 	ucma_put_ctx(ctx);
811 	return ret;
812 }
813 
814 static ssize_t ucma_join_multicast(struct ucma_file *file,
815 				   const char __user *inbuf,
816 				   int in_len, int out_len)
817 {
818 	struct rdma_ucm_join_mcast cmd;
819 	struct rdma_ucm_create_id_resp resp;
820 	struct ucma_context *ctx;
821 	struct ucma_multicast *mc;
822 	int ret;
823 
824 	if (out_len < sizeof(resp))
825 		return -ENOSPC;
826 
827 	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
828 		return -EFAULT;
829 
830 	ctx = ucma_get_ctx(file, cmd.id);
831 	if (IS_ERR(ctx))
832 		return PTR_ERR(ctx);
833 
834 	mutex_lock(&file->mut);
835 	mc = ucma_alloc_multicast(ctx);
836 	if (IS_ERR(mc)) {
837 		ret = PTR_ERR(mc);
838 		goto err1;
839 	}
840 
841 	mc->uid = cmd.uid;
842 	memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
843 	ret = rdma_join_multicast(ctx->cm_id, &mc->addr, mc);
844 	if (ret)
845 		goto err2;
846 
847 	resp.id = mc->id;
848 	if (copy_to_user((void __user *)(unsigned long)cmd.response,
849 			 &resp, sizeof(resp))) {
850 		ret = -EFAULT;
851 		goto err3;
852 	}
853 
854 	mutex_unlock(&file->mut);
855 	ucma_put_ctx(ctx);
856 	return 0;
857 
858 err3:
859 	rdma_leave_multicast(ctx->cm_id, &mc->addr);
860 	ucma_cleanup_mc_events(mc);
861 err2:
862 	mutex_lock(&mut);
863 	idr_remove(&multicast_idr, mc->id);
864 	mutex_unlock(&mut);
865 	list_del(&mc->list);
866 	kfree(mc);
867 err1:
868 	mutex_unlock(&file->mut);
869 	ucma_put_ctx(ctx);
870 	return ret;
871 }
872 
873 static ssize_t ucma_leave_multicast(struct ucma_file *file,
874 				    const char __user *inbuf,
875 				    int in_len, int out_len)
876 {
877 	struct rdma_ucm_destroy_id cmd;
878 	struct rdma_ucm_destroy_id_resp resp;
879 	struct ucma_multicast *mc;
880 	int ret = 0;
881 
882 	if (out_len < sizeof(resp))
883 		return -ENOSPC;
884 
885 	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
886 		return -EFAULT;
887 
888 	mutex_lock(&mut);
889 	mc = idr_find(&multicast_idr, cmd.id);
890 	if (!mc)
891 		mc = ERR_PTR(-ENOENT);
892 	else if (mc->ctx->file != file)
893 		mc = ERR_PTR(-EINVAL);
894 	else {
895 		idr_remove(&multicast_idr, mc->id);
896 		atomic_inc(&mc->ctx->ref);
897 	}
898 	mutex_unlock(&mut);
899 
900 	if (IS_ERR(mc)) {
901 		ret = PTR_ERR(mc);
902 		goto out;
903 	}
904 
905 	rdma_leave_multicast(mc->ctx->cm_id, &mc->addr);
906 	mutex_lock(&mc->ctx->file->mut);
907 	ucma_cleanup_mc_events(mc);
908 	list_del(&mc->list);
909 	mutex_unlock(&mc->ctx->file->mut);
910 
911 	ucma_put_ctx(mc->ctx);
912 	resp.events_reported = mc->events_reported;
913 	kfree(mc);
914 
915 	if (copy_to_user((void __user *)(unsigned long)cmd.response,
916 			 &resp, sizeof(resp)))
917 		ret = -EFAULT;
918 out:
919 	return ret;
920 }
921 
922 static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
923 				   const char __user *inbuf,
924 				   int in_len, int out_len) = {
925 	[RDMA_USER_CM_CMD_CREATE_ID]	= ucma_create_id,
926 	[RDMA_USER_CM_CMD_DESTROY_ID]	= ucma_destroy_id,
927 	[RDMA_USER_CM_CMD_BIND_ADDR]	= ucma_bind_addr,
928 	[RDMA_USER_CM_CMD_RESOLVE_ADDR]	= ucma_resolve_addr,
929 	[RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
930 	[RDMA_USER_CM_CMD_QUERY_ROUTE]	= ucma_query_route,
931 	[RDMA_USER_CM_CMD_CONNECT]	= ucma_connect,
932 	[RDMA_USER_CM_CMD_LISTEN]	= ucma_listen,
933 	[RDMA_USER_CM_CMD_ACCEPT]	= ucma_accept,
934 	[RDMA_USER_CM_CMD_REJECT]	= ucma_reject,
935 	[RDMA_USER_CM_CMD_DISCONNECT]	= ucma_disconnect,
936 	[RDMA_USER_CM_CMD_INIT_QP_ATTR]	= ucma_init_qp_attr,
937 	[RDMA_USER_CM_CMD_GET_EVENT]	= ucma_get_event,
938 	[RDMA_USER_CM_CMD_GET_OPTION]	= NULL,
939 	[RDMA_USER_CM_CMD_SET_OPTION]	= NULL,
940 	[RDMA_USER_CM_CMD_NOTIFY]	= ucma_notify,
941 	[RDMA_USER_CM_CMD_JOIN_MCAST]	= ucma_join_multicast,
942 	[RDMA_USER_CM_CMD_LEAVE_MCAST]	= ucma_leave_multicast,
943 };
944 
945 static ssize_t ucma_write(struct file *filp, const char __user *buf,
946 			  size_t len, loff_t *pos)
947 {
948 	struct ucma_file *file = filp->private_data;
949 	struct rdma_ucm_cmd_hdr hdr;
950 	ssize_t ret;
951 
952 	if (len < sizeof(hdr))
953 		return -EINVAL;
954 
955 	if (copy_from_user(&hdr, buf, sizeof(hdr)))
956 		return -EFAULT;
957 
958 	if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
959 		return -EINVAL;
960 
961 	if (hdr.in + sizeof(hdr) > len)
962 		return -EINVAL;
963 
964 	if (!ucma_cmd_table[hdr.cmd])
965 		return -ENOSYS;
966 
967 	ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
968 	if (!ret)
969 		ret = len;
970 
971 	return ret;
972 }
973 
974 static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
975 {
976 	struct ucma_file *file = filp->private_data;
977 	unsigned int mask = 0;
978 
979 	poll_wait(filp, &file->poll_wait, wait);
980 
981 	if (!list_empty(&file->event_list))
982 		mask = POLLIN | POLLRDNORM;
983 
984 	return mask;
985 }
986 
987 static int ucma_open(struct inode *inode, struct file *filp)
988 {
989 	struct ucma_file *file;
990 
991 	file = kmalloc(sizeof *file, GFP_KERNEL);
992 	if (!file)
993 		return -ENOMEM;
994 
995 	INIT_LIST_HEAD(&file->event_list);
996 	INIT_LIST_HEAD(&file->ctx_list);
997 	init_waitqueue_head(&file->poll_wait);
998 	mutex_init(&file->mut);
999 
1000 	filp->private_data = file;
1001 	file->filp = filp;
1002 	return 0;
1003 }
1004 
1005 static int ucma_close(struct inode *inode, struct file *filp)
1006 {
1007 	struct ucma_file *file = filp->private_data;
1008 	struct ucma_context *ctx, *tmp;
1009 
1010 	mutex_lock(&file->mut);
1011 	list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
1012 		mutex_unlock(&file->mut);
1013 
1014 		mutex_lock(&mut);
1015 		idr_remove(&ctx_idr, ctx->id);
1016 		mutex_unlock(&mut);
1017 
1018 		ucma_free_ctx(ctx);
1019 		mutex_lock(&file->mut);
1020 	}
1021 	mutex_unlock(&file->mut);
1022 	kfree(file);
1023 	return 0;
1024 }
1025 
1026 static const struct file_operations ucma_fops = {
1027 	.owner 	 = THIS_MODULE,
1028 	.open 	 = ucma_open,
1029 	.release = ucma_close,
1030 	.write	 = ucma_write,
1031 	.poll    = ucma_poll,
1032 };
1033 
1034 static struct miscdevice ucma_misc = {
1035 	.minor	= MISC_DYNAMIC_MINOR,
1036 	.name	= "rdma_cm",
1037 	.fops	= &ucma_fops,
1038 };
1039 
1040 static ssize_t show_abi_version(struct device *dev,
1041 				struct device_attribute *attr,
1042 				char *buf)
1043 {
1044 	return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
1045 }
1046 static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1047 
1048 static int __init ucma_init(void)
1049 {
1050 	int ret;
1051 
1052 	ret = misc_register(&ucma_misc);
1053 	if (ret)
1054 		return ret;
1055 
1056 	ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
1057 	if (ret) {
1058 		printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
1059 		goto err;
1060 	}
1061 	return 0;
1062 err:
1063 	misc_deregister(&ucma_misc);
1064 	return ret;
1065 }
1066 
1067 static void __exit ucma_cleanup(void)
1068 {
1069 	device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1070 	misc_deregister(&ucma_misc);
1071 	idr_destroy(&ctx_idr);
1072 }
1073 
1074 module_init(ucma_init);
1075 module_exit(ucma_cleanup);
1076