xref: /openbmc/linux/drivers/infiniband/core/ucma.c (revision 96de0e252cedffad61b3cb5e05662c591898e69a)
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 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 
815 static 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 
831 static 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);
860 out2:
861 	kfree(optval);
862 out1:
863 	ucma_put_ctx(ctx);
864 	return ret;
865 }
866 
867 static 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 
886 static 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);
908 	if (IS_ERR(mc)) {
909 		ret = PTR_ERR(mc);
910 		goto err1;
911 	}
912 
913 	mc->uid = cmd.uid;
914 	memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
915 	ret = rdma_join_multicast(ctx->cm_id, &mc->addr, mc);
916 	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 
930 err3:
931 	rdma_leave_multicast(ctx->cm_id, &mc->addr);
932 	ucma_cleanup_mc_events(mc);
933 err2:
934 	mutex_lock(&mut);
935 	idr_remove(&multicast_idr, mc->id);
936 	mutex_unlock(&mut);
937 	list_del(&mc->list);
938 	kfree(mc);
939 err1:
940 	mutex_unlock(&file->mut);
941 	ucma_put_ctx(ctx);
942 	return ret;
943 }
944 
945 static 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 
977 	rdma_leave_multicast(mc->ctx->cm_id, &mc->addr);
978 	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;
990 out:
991 	return ret;
992 }
993 
994 static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
995 				   const char __user *inbuf,
996 				   int in_len, int out_len) = {
997 	[RDMA_USER_CM_CMD_CREATE_ID]	= ucma_create_id,
998 	[RDMA_USER_CM_CMD_DESTROY_ID]	= ucma_destroy_id,
999 	[RDMA_USER_CM_CMD_BIND_ADDR]	= ucma_bind_addr,
1000 	[RDMA_USER_CM_CMD_RESOLVE_ADDR]	= ucma_resolve_addr,
1001 	[RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
1002 	[RDMA_USER_CM_CMD_QUERY_ROUTE]	= ucma_query_route,
1003 	[RDMA_USER_CM_CMD_CONNECT]	= ucma_connect,
1004 	[RDMA_USER_CM_CMD_LISTEN]	= ucma_listen,
1005 	[RDMA_USER_CM_CMD_ACCEPT]	= ucma_accept,
1006 	[RDMA_USER_CM_CMD_REJECT]	= ucma_reject,
1007 	[RDMA_USER_CM_CMD_DISCONNECT]	= ucma_disconnect,
1008 	[RDMA_USER_CM_CMD_INIT_QP_ATTR]	= ucma_init_qp_attr,
1009 	[RDMA_USER_CM_CMD_GET_EVENT]	= ucma_get_event,
1010 	[RDMA_USER_CM_CMD_GET_OPTION]	= NULL,
1011 	[RDMA_USER_CM_CMD_SET_OPTION]	= ucma_set_option,
1012 	[RDMA_USER_CM_CMD_NOTIFY]	= ucma_notify,
1013 	[RDMA_USER_CM_CMD_JOIN_MCAST]	= ucma_join_multicast,
1014 	[RDMA_USER_CM_CMD_LEAVE_MCAST]	= ucma_leave_multicast,
1015 };
1016 
1017 static ssize_t ucma_write(struct file *filp, const char __user *buf,
1018 			  size_t len, loff_t *pos)
1019 {
1020 	struct ucma_file *file = filp->private_data;
1021 	struct rdma_ucm_cmd_hdr hdr;
1022 	ssize_t ret;
1023 
1024 	if (len < sizeof(hdr))
1025 		return -EINVAL;
1026 
1027 	if (copy_from_user(&hdr, buf, sizeof(hdr)))
1028 		return -EFAULT;
1029 
1030 	if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
1031 		return -EINVAL;
1032 
1033 	if (hdr.in + sizeof(hdr) > len)
1034 		return -EINVAL;
1035 
1036 	if (!ucma_cmd_table[hdr.cmd])
1037 		return -ENOSYS;
1038 
1039 	ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
1040 	if (!ret)
1041 		ret = len;
1042 
1043 	return ret;
1044 }
1045 
1046 static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
1047 {
1048 	struct ucma_file *file = filp->private_data;
1049 	unsigned int mask = 0;
1050 
1051 	poll_wait(filp, &file->poll_wait, wait);
1052 
1053 	if (!list_empty(&file->event_list))
1054 		mask = POLLIN | POLLRDNORM;
1055 
1056 	return mask;
1057 }
1058 
1059 static int ucma_open(struct inode *inode, struct file *filp)
1060 {
1061 	struct ucma_file *file;
1062 
1063 	file = kmalloc(sizeof *file, GFP_KERNEL);
1064 	if (!file)
1065 		return -ENOMEM;
1066 
1067 	INIT_LIST_HEAD(&file->event_list);
1068 	INIT_LIST_HEAD(&file->ctx_list);
1069 	init_waitqueue_head(&file->poll_wait);
1070 	mutex_init(&file->mut);
1071 
1072 	filp->private_data = file;
1073 	file->filp = filp;
1074 	return 0;
1075 }
1076 
1077 static int ucma_close(struct inode *inode, struct file *filp)
1078 {
1079 	struct ucma_file *file = filp->private_data;
1080 	struct ucma_context *ctx, *tmp;
1081 
1082 	mutex_lock(&file->mut);
1083 	list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
1084 		mutex_unlock(&file->mut);
1085 
1086 		mutex_lock(&mut);
1087 		idr_remove(&ctx_idr, ctx->id);
1088 		mutex_unlock(&mut);
1089 
1090 		ucma_free_ctx(ctx);
1091 		mutex_lock(&file->mut);
1092 	}
1093 	mutex_unlock(&file->mut);
1094 	kfree(file);
1095 	return 0;
1096 }
1097 
1098 static const struct file_operations ucma_fops = {
1099 	.owner 	 = THIS_MODULE,
1100 	.open 	 = ucma_open,
1101 	.release = ucma_close,
1102 	.write	 = ucma_write,
1103 	.poll    = ucma_poll,
1104 };
1105 
1106 static struct miscdevice ucma_misc = {
1107 	.minor	= MISC_DYNAMIC_MINOR,
1108 	.name	= "rdma_cm",
1109 	.fops	= &ucma_fops,
1110 };
1111 
1112 static ssize_t show_abi_version(struct device *dev,
1113 				struct device_attribute *attr,
1114 				char *buf)
1115 {
1116 	return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
1117 }
1118 static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1119 
1120 static int __init ucma_init(void)
1121 {
1122 	int ret;
1123 
1124 	ret = misc_register(&ucma_misc);
1125 	if (ret)
1126 		return ret;
1127 
1128 	ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
1129 	if (ret) {
1130 		printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
1131 		goto err;
1132 	}
1133 	return 0;
1134 err:
1135 	misc_deregister(&ucma_misc);
1136 	return ret;
1137 }
1138 
1139 static void __exit ucma_cleanup(void)
1140 {
1141 	device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1142 	misc_deregister(&ucma_misc);
1143 	idr_destroy(&ctx_idr);
1144 }
1145 
1146 module_init(ucma_init);
1147 module_exit(ucma_cleanup);
1148