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