1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *	copyright notice, this list of conditions and the following
16219820Sjeff *	disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *	copyright notice, this list of conditions and the following
20219820Sjeff *	disclaimer in the documentation and/or other materials
21219820Sjeff *	provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#include <linux/completion.h>
34219820Sjeff#include <linux/file.h>
35219820Sjeff#include <linux/mutex.h>
36219820Sjeff#include <linux/poll.h>
37219820Sjeff#include <linux/idr.h>
38219820Sjeff#include <linux/in.h>
39219820Sjeff#include <linux/in6.h>
40219820Sjeff#include <linux/miscdevice.h>
41219820Sjeff
42304846Shselasky#include <sys/filio.h>
43304846Shselasky
44219820Sjeff#include <rdma/rdma_user_cm.h>
45219820Sjeff#include <rdma/ib_marshall.h>
46219820Sjeff#include <rdma/rdma_cm.h>
47219820Sjeff#include <rdma/rdma_cm_ib.h>
48219820Sjeff
49219820SjeffMODULE_AUTHOR("Sean Hefty");
50219820SjeffMODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
51219820SjeffMODULE_LICENSE("Dual BSD/GPL");
52219820Sjeff
53219820Sjeffenum {
54219820Sjeff	UCMA_MAX_BACKLOG	= 1024
55219820Sjeff};
56219820Sjeff
57219820Sjeffstruct ucma_file {
58219820Sjeff	struct mutex		mut;
59219820Sjeff	struct file		*filp;
60219820Sjeff	struct list_head	ctx_list;
61219820Sjeff	struct list_head	event_list;
62219820Sjeff	wait_queue_head_t	poll_wait;
63219820Sjeff};
64219820Sjeff
65219820Sjeffstruct ucma_context {
66219820Sjeff	int			id;
67219820Sjeff	struct completion	comp;
68219820Sjeff	atomic_t		ref;
69219820Sjeff	int			events_reported;
70219820Sjeff	int			backlog;
71219820Sjeff
72219820Sjeff	struct ucma_file	*file;
73219820Sjeff	struct rdma_cm_id	*cm_id;
74219820Sjeff	u64			uid;
75219820Sjeff
76219820Sjeff	struct list_head	list;
77219820Sjeff	struct list_head	mc_list;
78219820Sjeff};
79219820Sjeff
80219820Sjeffstruct ucma_multicast {
81219820Sjeff	struct ucma_context	*ctx;
82219820Sjeff	int			id;
83219820Sjeff	int			events_reported;
84219820Sjeff
85219820Sjeff	u64			uid;
86219820Sjeff	struct list_head	list;
87219820Sjeff	struct sockaddr_storage	addr;
88219820Sjeff};
89219820Sjeff
90219820Sjeffstruct ucma_event {
91219820Sjeff	struct ucma_context	*ctx;
92219820Sjeff	struct ucma_multicast	*mc;
93219820Sjeff	struct list_head	list;
94219820Sjeff	struct rdma_cm_id	*cm_id;
95219820Sjeff	struct rdma_ucm_event_resp resp;
96219820Sjeff};
97219820Sjeff
98219820Sjeffstatic DEFINE_MUTEX(mut);
99219820Sjeffstatic DEFINE_IDR(ctx_idr);
100219820Sjeffstatic DEFINE_IDR(multicast_idr);
101219820Sjeff
102219820Sjeffstatic inline struct ucma_context *_ucma_find_context(int id,
103219820Sjeff						      struct ucma_file *file)
104219820Sjeff{
105219820Sjeff	struct ucma_context *ctx;
106219820Sjeff
107219820Sjeff	ctx = idr_find(&ctx_idr, id);
108219820Sjeff	if (!ctx)
109219820Sjeff		ctx = ERR_PTR(-ENOENT);
110219820Sjeff	else if (ctx->file != file)
111219820Sjeff		ctx = ERR_PTR(-EINVAL);
112219820Sjeff	return ctx;
113219820Sjeff}
114219820Sjeff
115219820Sjeffstatic struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
116219820Sjeff{
117219820Sjeff	struct ucma_context *ctx;
118219820Sjeff
119219820Sjeff	mutex_lock(&mut);
120219820Sjeff	ctx = _ucma_find_context(id, file);
121219820Sjeff	if (!IS_ERR(ctx))
122219820Sjeff		atomic_inc(&ctx->ref);
123219820Sjeff	mutex_unlock(&mut);
124219820Sjeff	return ctx;
125219820Sjeff}
126219820Sjeff
127219820Sjeffstatic void ucma_put_ctx(struct ucma_context *ctx)
128219820Sjeff{
129219820Sjeff	if (atomic_dec_and_test(&ctx->ref))
130219820Sjeff		complete(&ctx->comp);
131219820Sjeff}
132219820Sjeff
133219820Sjeffstatic struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
134219820Sjeff{
135219820Sjeff	struct ucma_context *ctx;
136219820Sjeff	int ret;
137219820Sjeff
138219820Sjeff	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
139219820Sjeff	if (!ctx)
140219820Sjeff		return NULL;
141219820Sjeff
142219820Sjeff	atomic_set(&ctx->ref, 1);
143219820Sjeff	init_completion(&ctx->comp);
144219820Sjeff	INIT_LIST_HEAD(&ctx->mc_list);
145219820Sjeff	ctx->file = file;
146219820Sjeff
147219820Sjeff	do {
148219820Sjeff		ret = idr_pre_get(&ctx_idr, GFP_KERNEL);
149219820Sjeff		if (!ret)
150219820Sjeff			goto error;
151219820Sjeff
152219820Sjeff		mutex_lock(&mut);
153219820Sjeff		ret = idr_get_new(&ctx_idr, ctx, &ctx->id);
154219820Sjeff		mutex_unlock(&mut);
155219820Sjeff	} while (ret == -EAGAIN);
156219820Sjeff
157219820Sjeff	if (ret)
158219820Sjeff		goto error;
159219820Sjeff
160219820Sjeff	list_add_tail(&ctx->list, &file->ctx_list);
161219820Sjeff	return ctx;
162219820Sjeff
163219820Sjefferror:
164219820Sjeff	kfree(ctx);
165219820Sjeff	return NULL;
166219820Sjeff}
167219820Sjeff
168219820Sjeffstatic struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
169219820Sjeff{
170219820Sjeff	struct ucma_multicast *mc;
171219820Sjeff	int ret;
172219820Sjeff
173219820Sjeff	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
174219820Sjeff	if (!mc)
175219820Sjeff		return NULL;
176219820Sjeff
177219820Sjeff	do {
178219820Sjeff		ret = idr_pre_get(&multicast_idr, GFP_KERNEL);
179219820Sjeff		if (!ret)
180219820Sjeff			goto error;
181219820Sjeff
182219820Sjeff		mutex_lock(&mut);
183219820Sjeff		ret = idr_get_new(&multicast_idr, mc, &mc->id);
184219820Sjeff		mutex_unlock(&mut);
185219820Sjeff	} while (ret == -EAGAIN);
186219820Sjeff
187219820Sjeff	if (ret)
188219820Sjeff		goto error;
189219820Sjeff
190219820Sjeff	mc->ctx = ctx;
191219820Sjeff	list_add_tail(&mc->list, &ctx->mc_list);
192219820Sjeff	return mc;
193219820Sjeff
194219820Sjefferror:
195219820Sjeff	kfree(mc);
196219820Sjeff	return NULL;
197219820Sjeff}
198219820Sjeff
199219820Sjeffstatic void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
200219820Sjeff				 struct rdma_conn_param *src)
201219820Sjeff{
202219820Sjeff	if (src->private_data_len)
203219820Sjeff		memcpy(dst->private_data, src->private_data,
204219820Sjeff		       src->private_data_len);
205219820Sjeff	dst->private_data_len = src->private_data_len;
206219820Sjeff	dst->responder_resources =src->responder_resources;
207219820Sjeff	dst->initiator_depth = src->initiator_depth;
208219820Sjeff	dst->flow_control = src->flow_control;
209219820Sjeff	dst->retry_count = src->retry_count;
210219820Sjeff	dst->rnr_retry_count = src->rnr_retry_count;
211219820Sjeff	dst->srq = src->srq;
212219820Sjeff	dst->qp_num = src->qp_num;
213219820Sjeff}
214219820Sjeff
215219820Sjeffstatic void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
216219820Sjeff			       struct rdma_ud_param *src)
217219820Sjeff{
218219820Sjeff	if (src->private_data_len)
219219820Sjeff		memcpy(dst->private_data, src->private_data,
220219820Sjeff		       src->private_data_len);
221219820Sjeff	dst->private_data_len = src->private_data_len;
222219820Sjeff	ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
223219820Sjeff	dst->qp_num = src->qp_num;
224219820Sjeff	dst->qkey = src->qkey;
225219820Sjeff}
226219820Sjeff
227219820Sjeffstatic void ucma_set_event_context(struct ucma_context *ctx,
228219820Sjeff				   struct rdma_cm_event *event,
229219820Sjeff				   struct ucma_event *uevent)
230219820Sjeff{
231219820Sjeff	uevent->ctx = ctx;
232219820Sjeff	switch (event->event) {
233219820Sjeff	case RDMA_CM_EVENT_MULTICAST_JOIN:
234219820Sjeff	case RDMA_CM_EVENT_MULTICAST_ERROR:
235219820Sjeff		uevent->mc = (struct ucma_multicast *)
236219820Sjeff			     event->param.ud.private_data;
237219820Sjeff		uevent->resp.uid = uevent->mc->uid;
238219820Sjeff		uevent->resp.id = uevent->mc->id;
239219820Sjeff		break;
240219820Sjeff	default:
241219820Sjeff		uevent->resp.uid = ctx->uid;
242219820Sjeff		uevent->resp.id = ctx->id;
243219820Sjeff		break;
244219820Sjeff	}
245219820Sjeff}
246219820Sjeff
247219820Sjeffstatic int ucma_event_handler(struct rdma_cm_id *cm_id,
248219820Sjeff			      struct rdma_cm_event *event)
249219820Sjeff{
250219820Sjeff	struct ucma_event *uevent;
251219820Sjeff	struct ucma_context *ctx = cm_id->context;
252219820Sjeff	int ret = 0;
253219820Sjeff
254219820Sjeff	uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
255219820Sjeff	if (!uevent)
256219820Sjeff		return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
257219820Sjeff
258219820Sjeff	uevent->cm_id = cm_id;
259219820Sjeff	ucma_set_event_context(ctx, event, uevent);
260219820Sjeff	uevent->resp.event = event->event;
261219820Sjeff	uevent->resp.status = event->status;
262219820Sjeff	if (cm_id->ps == RDMA_PS_UDP || cm_id->ps == RDMA_PS_IPOIB)
263219820Sjeff		ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
264219820Sjeff	else
265219820Sjeff		ucma_copy_conn_event(&uevent->resp.param.conn,
266219820Sjeff				     &event->param.conn);
267219820Sjeff
268219820Sjeff	mutex_lock(&ctx->file->mut);
269219820Sjeff	if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
270219820Sjeff		if (!ctx->backlog) {
271219820Sjeff			ret = -ENOMEM;
272219820Sjeff			kfree(uevent);
273219820Sjeff			goto out;
274219820Sjeff		}
275219820Sjeff		ctx->backlog--;
276219820Sjeff	} else if (!ctx->uid) {
277219820Sjeff		/*
278219820Sjeff		 * We ignore events for new connections until userspace has set
279219820Sjeff		 * their context.  This can only happen if an error occurs on a
280219820Sjeff		 * new connection before the user accepts it.  This is okay,
281219820Sjeff		 * since the accept will just fail later.
282219820Sjeff		 */
283219820Sjeff		kfree(uevent);
284219820Sjeff		goto out;
285219820Sjeff	}
286219820Sjeff
287219820Sjeff	list_add_tail(&uevent->list, &ctx->file->event_list);
288219820Sjeff	wake_up_interruptible(&ctx->file->poll_wait);
289219820Sjeff	if (ctx->file->filp)
290219820Sjeff		selwakeup(&ctx->file->filp->f_selinfo);
291219820Sjeffout:
292219820Sjeff	mutex_unlock(&ctx->file->mut);
293219820Sjeff	return ret;
294219820Sjeff}
295219820Sjeff
296219820Sjeffstatic ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
297219820Sjeff			      int in_len, int out_len)
298219820Sjeff{
299219820Sjeff	struct ucma_context *ctx;
300219820Sjeff	struct rdma_ucm_get_event cmd;
301219820Sjeff	struct ucma_event *uevent;
302219820Sjeff	int ret = 0;
303219820Sjeff	DEFINE_WAIT(wait);
304219820Sjeff
305219820Sjeff	if (out_len < sizeof uevent->resp)
306219820Sjeff		return -ENOSPC;
307219820Sjeff
308219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
309219820Sjeff		return -EFAULT;
310219820Sjeff
311219820Sjeff	mutex_lock(&file->mut);
312219820Sjeff	while (list_empty(&file->event_list)) {
313219820Sjeff		mutex_unlock(&file->mut);
314219820Sjeff
315219820Sjeff		if (file->filp->f_flags & O_NONBLOCK)
316219820Sjeff			return -EAGAIN;
317219820Sjeff
318219820Sjeff		if (wait_event_interruptible(file->poll_wait,
319219820Sjeff					     !list_empty(&file->event_list)))
320219820Sjeff			return -ERESTARTSYS;
321219820Sjeff
322219820Sjeff		mutex_lock(&file->mut);
323219820Sjeff	}
324219820Sjeff
325219820Sjeff	uevent = list_entry(file->event_list.next, struct ucma_event, list);
326219820Sjeff
327219820Sjeff	if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
328219820Sjeff		ctx = ucma_alloc_ctx(file);
329219820Sjeff		if (!ctx) {
330219820Sjeff			ret = -ENOMEM;
331219820Sjeff			goto done;
332219820Sjeff		}
333219820Sjeff		uevent->ctx->backlog++;
334219820Sjeff		ctx->cm_id = uevent->cm_id;
335219820Sjeff		ctx->cm_id->context = ctx;
336219820Sjeff		uevent->resp.id = ctx->id;
337219820Sjeff	}
338219820Sjeff
339219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
340219820Sjeff			 &uevent->resp, sizeof uevent->resp)) {
341219820Sjeff		ret = -EFAULT;
342219820Sjeff		goto done;
343219820Sjeff	}
344219820Sjeff
345219820Sjeff	list_del(&uevent->list);
346219820Sjeff	uevent->ctx->events_reported++;
347219820Sjeff	if (uevent->mc)
348219820Sjeff		uevent->mc->events_reported++;
349219820Sjeff	kfree(uevent);
350219820Sjeffdone:
351219820Sjeff	mutex_unlock(&file->mut);
352219820Sjeff	return ret;
353219820Sjeff}
354219820Sjeff
355219820Sjeffstatic ssize_t ucma_create_id(struct ucma_file *file,
356219820Sjeff				const char __user *inbuf,
357219820Sjeff				int in_len, int out_len)
358219820Sjeff{
359219820Sjeff	struct rdma_ucm_create_id cmd;
360219820Sjeff	struct rdma_ucm_create_id_resp resp;
361219820Sjeff	struct ucma_context *ctx;
362219820Sjeff	int ret;
363219820Sjeff
364219820Sjeff	if (out_len < sizeof(resp))
365219820Sjeff		return -ENOSPC;
366219820Sjeff
367219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
368219820Sjeff		return -EFAULT;
369219820Sjeff
370219820Sjeff	mutex_lock(&file->mut);
371219820Sjeff	ctx = ucma_alloc_ctx(file);
372219820Sjeff	mutex_unlock(&file->mut);
373219820Sjeff	if (!ctx)
374219820Sjeff		return -ENOMEM;
375219820Sjeff
376219820Sjeff	ctx->uid = cmd.uid;
377219820Sjeff	ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps);
378219820Sjeff	if (IS_ERR(ctx->cm_id)) {
379219820Sjeff		ret = PTR_ERR(ctx->cm_id);
380219820Sjeff		goto err1;
381219820Sjeff	}
382219820Sjeff
383219820Sjeff	resp.id = ctx->id;
384219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
385219820Sjeff			 &resp, sizeof(resp))) {
386219820Sjeff		ret = -EFAULT;
387219820Sjeff		goto err2;
388219820Sjeff	}
389219820Sjeff	return 0;
390219820Sjeff
391219820Sjefferr2:
392219820Sjeff	rdma_destroy_id(ctx->cm_id);
393219820Sjefferr1:
394219820Sjeff	mutex_lock(&mut);
395219820Sjeff	idr_remove(&ctx_idr, ctx->id);
396219820Sjeff	mutex_unlock(&mut);
397219820Sjeff	kfree(ctx);
398219820Sjeff	return ret;
399219820Sjeff}
400219820Sjeff
401219820Sjeffstatic void ucma_cleanup_multicast(struct ucma_context *ctx)
402219820Sjeff{
403219820Sjeff	struct ucma_multicast *mc, *tmp;
404219820Sjeff
405219820Sjeff	mutex_lock(&mut);
406219820Sjeff	list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
407219820Sjeff		list_del(&mc->list);
408219820Sjeff		idr_remove(&multicast_idr, mc->id);
409219820Sjeff		kfree(mc);
410219820Sjeff	}
411219820Sjeff	mutex_unlock(&mut);
412219820Sjeff}
413219820Sjeff
414219820Sjeffstatic void ucma_cleanup_events(struct ucma_context *ctx)
415219820Sjeff{
416219820Sjeff	struct ucma_event *uevent, *tmp;
417219820Sjeff
418219820Sjeff	list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
419219820Sjeff		if (uevent->ctx != ctx)
420219820Sjeff			continue;
421219820Sjeff
422219820Sjeff		list_del(&uevent->list);
423219820Sjeff
424219820Sjeff		/* clear incoming connections. */
425219820Sjeff		if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
426219820Sjeff			rdma_destroy_id(uevent->cm_id);
427219820Sjeff
428219820Sjeff		kfree(uevent);
429219820Sjeff	}
430219820Sjeff}
431219820Sjeff
432219820Sjeffstatic void ucma_cleanup_mc_events(struct ucma_multicast *mc)
433219820Sjeff{
434219820Sjeff	struct ucma_event *uevent, *tmp;
435219820Sjeff
436219820Sjeff	list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
437219820Sjeff		if (uevent->mc != mc)
438219820Sjeff			continue;
439219820Sjeff
440219820Sjeff		list_del(&uevent->list);
441219820Sjeff		kfree(uevent);
442219820Sjeff	}
443219820Sjeff}
444219820Sjeff
445219820Sjeffstatic int ucma_free_ctx(struct ucma_context *ctx)
446219820Sjeff{
447219820Sjeff	int events_reported;
448219820Sjeff
449219820Sjeff	/* No new events will be generated after destroying the id. */
450219820Sjeff	rdma_destroy_id(ctx->cm_id);
451219820Sjeff
452219820Sjeff	ucma_cleanup_multicast(ctx);
453219820Sjeff
454219820Sjeff	/* Cleanup events not yet reported to the user. */
455219820Sjeff	mutex_lock(&ctx->file->mut);
456219820Sjeff	ucma_cleanup_events(ctx);
457219820Sjeff	list_del(&ctx->list);
458219820Sjeff	mutex_unlock(&ctx->file->mut);
459219820Sjeff
460219820Sjeff	events_reported = ctx->events_reported;
461219820Sjeff	kfree(ctx);
462219820Sjeff	return events_reported;
463219820Sjeff}
464219820Sjeff
465219820Sjeffstatic ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
466219820Sjeff			       int in_len, int out_len)
467219820Sjeff{
468219820Sjeff	struct rdma_ucm_destroy_id cmd;
469219820Sjeff	struct rdma_ucm_destroy_id_resp resp;
470219820Sjeff	struct ucma_context *ctx;
471219820Sjeff	int ret = 0;
472219820Sjeff
473219820Sjeff	if (out_len < sizeof(resp))
474219820Sjeff		return -ENOSPC;
475219820Sjeff
476219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
477219820Sjeff		return -EFAULT;
478219820Sjeff
479219820Sjeff	mutex_lock(&mut);
480219820Sjeff	ctx = _ucma_find_context(cmd.id, file);
481219820Sjeff	if (!IS_ERR(ctx))
482219820Sjeff		idr_remove(&ctx_idr, ctx->id);
483219820Sjeff	mutex_unlock(&mut);
484219820Sjeff
485219820Sjeff	if (IS_ERR(ctx))
486219820Sjeff		return PTR_ERR(ctx);
487219820Sjeff
488219820Sjeff	ucma_put_ctx(ctx);
489219820Sjeff	wait_for_completion(&ctx->comp);
490219820Sjeff	resp.events_reported = ucma_free_ctx(ctx);
491219820Sjeff
492219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
493219820Sjeff			 &resp, sizeof(resp)))
494219820Sjeff		ret = -EFAULT;
495219820Sjeff
496219820Sjeff	return ret;
497219820Sjeff}
498219820Sjeff
499219820Sjeffstatic ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
500219820Sjeff			      int in_len, int out_len)
501219820Sjeff{
502219820Sjeff	struct rdma_ucm_bind_addr cmd;
503219820Sjeff	struct ucma_context *ctx;
504219820Sjeff	int ret;
505219820Sjeff
506219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
507219820Sjeff		return -EFAULT;
508219820Sjeff
509219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
510219820Sjeff	if (IS_ERR(ctx))
511219820Sjeff		return PTR_ERR(ctx);
512219820Sjeff
513219820Sjeff	ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
514219820Sjeff	ucma_put_ctx(ctx);
515219820Sjeff	return ret;
516219820Sjeff}
517219820Sjeff
518219820Sjeffstatic ssize_t ucma_resolve_addr(struct ucma_file *file,
519219820Sjeff				 const char __user *inbuf,
520219820Sjeff				 int in_len, int out_len)
521219820Sjeff{
522219820Sjeff	struct rdma_ucm_resolve_addr cmd;
523219820Sjeff	struct ucma_context *ctx;
524219820Sjeff	int ret;
525219820Sjeff
526219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
527219820Sjeff		return -EFAULT;
528219820Sjeff
529219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
530219820Sjeff	if (IS_ERR(ctx))
531219820Sjeff		return PTR_ERR(ctx);
532219820Sjeff
533219820Sjeff	ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
534219820Sjeff				(struct sockaddr *) &cmd.dst_addr,
535219820Sjeff				cmd.timeout_ms);
536219820Sjeff	ucma_put_ctx(ctx);
537219820Sjeff	return ret;
538219820Sjeff}
539219820Sjeff
540219820Sjeffstatic ssize_t ucma_resolve_route(struct ucma_file *file,
541219820Sjeff				  const char __user *inbuf,
542219820Sjeff				  int in_len, int out_len)
543219820Sjeff{
544219820Sjeff	struct rdma_ucm_resolve_route cmd;
545219820Sjeff	struct ucma_context *ctx;
546219820Sjeff	int ret;
547219820Sjeff
548219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
549219820Sjeff		return -EFAULT;
550219820Sjeff
551219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
552219820Sjeff	if (IS_ERR(ctx))
553219820Sjeff		return PTR_ERR(ctx);
554219820Sjeff
555219820Sjeff	ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
556219820Sjeff	ucma_put_ctx(ctx);
557219820Sjeff	return ret;
558219820Sjeff}
559219820Sjeff
560219820Sjeffstatic void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
561219820Sjeff			       struct rdma_route *route)
562219820Sjeff{
563219820Sjeff	struct rdma_dev_addr *dev_addr;
564219820Sjeff
565219820Sjeff	resp->num_paths = route->num_paths;
566219820Sjeff	switch (route->num_paths) {
567219820Sjeff	case 0:
568219820Sjeff		dev_addr = &route->addr.dev_addr;
569219820Sjeff		rdma_addr_get_dgid(dev_addr,
570219820Sjeff				   (union ib_gid *) &resp->ib_route[0].dgid);
571219820Sjeff		rdma_addr_get_sgid(dev_addr,
572219820Sjeff				   (union ib_gid *) &resp->ib_route[0].sgid);
573219820Sjeff		resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
574219820Sjeff		break;
575219820Sjeff	case 2:
576219820Sjeff		ib_copy_path_rec_to_user(&resp->ib_route[1],
577219820Sjeff					 &route->path_rec[1]);
578219820Sjeff		/* fall through */
579219820Sjeff	case 1:
580219820Sjeff		ib_copy_path_rec_to_user(&resp->ib_route[0],
581219820Sjeff					 &route->path_rec[0]);
582219820Sjeff		break;
583219820Sjeff	default:
584219820Sjeff		break;
585219820Sjeff	}
586219820Sjeff}
587219820Sjeff
588219820Sjeffstatic void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
589219820Sjeff				 struct rdma_route *route)
590219820Sjeff{
591219820Sjeff	struct rdma_dev_addr *dev_addr;
592219820Sjeff	struct net_device *dev;
593219820Sjeff	u16 vid = 0;
594219820Sjeff
595219820Sjeff	resp->num_paths = route->num_paths;
596219820Sjeff	switch (route->num_paths) {
597219820Sjeff	case 0:
598219820Sjeff		dev_addr = &route->addr.dev_addr;
599219820Sjeff		dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
600219820Sjeff			if (dev) {
601219820Sjeff				vid = rdma_vlan_dev_vlan_id(dev);
602219820Sjeff				dev_put(dev);
603219820Sjeff			}
604219820Sjeff
605219820Sjeff		iboe_mac_vlan_to_ll((union ib_gid *) &resp->ib_route[0].dgid,
606219820Sjeff				    dev_addr->dst_dev_addr, vid);
607219820Sjeff		iboe_addr_get_sgid(dev_addr,
608219820Sjeff				   (union ib_gid *) &resp->ib_route[0].sgid);
609219820Sjeff		resp->ib_route[0].pkey = cpu_to_be16(0xffff);
610219820Sjeff		break;
611219820Sjeff	case 2:
612219820Sjeff		ib_copy_path_rec_to_user(&resp->ib_route[1],
613219820Sjeff					 &route->path_rec[1]);
614219820Sjeff		/* fall through */
615219820Sjeff	case 1:
616219820Sjeff		ib_copy_path_rec_to_user(&resp->ib_route[0],
617219820Sjeff					 &route->path_rec[0]);
618219820Sjeff		break;
619219820Sjeff	default:
620219820Sjeff		break;
621219820Sjeff	}
622219820Sjeff}
623219820Sjeff
624219820Sjeffstatic ssize_t ucma_query_route(struct ucma_file *file,
625219820Sjeff				const char __user *inbuf,
626219820Sjeff				int in_len, int out_len)
627219820Sjeff{
628219820Sjeff	struct rdma_ucm_query_route cmd;
629219820Sjeff	struct rdma_ucm_query_route_resp resp;
630219820Sjeff	struct ucma_context *ctx;
631219820Sjeff	struct sockaddr *addr;
632219820Sjeff	int ret = 0;
633219820Sjeff
634219820Sjeff	if (out_len < sizeof(resp))
635219820Sjeff		return -ENOSPC;
636219820Sjeff
637219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
638219820Sjeff		return -EFAULT;
639219820Sjeff
640219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
641219820Sjeff	if (IS_ERR(ctx))
642219820Sjeff		return PTR_ERR(ctx);
643219820Sjeff
644219820Sjeff	memset(&resp, 0, sizeof resp);
645219820Sjeff	addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
646219820Sjeff	memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
647219820Sjeff				     sizeof(struct sockaddr_in) :
648219820Sjeff				     sizeof(struct sockaddr_in6));
649219820Sjeff	addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
650219820Sjeff	memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
651219820Sjeff				     sizeof(struct sockaddr_in) :
652219820Sjeff				     sizeof(struct sockaddr_in6));
653219820Sjeff	if (!ctx->cm_id->device)
654219820Sjeff		goto out;
655219820Sjeff
656219820Sjeff	resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
657219820Sjeff	resp.port_num = ctx->cm_id->port_num;
658219820Sjeff	if (rdma_node_get_transport(ctx->cm_id->device->node_type) == RDMA_TRANSPORT_IB) {
659219820Sjeff		switch (rdma_port_get_link_layer(ctx->cm_id->device, ctx->cm_id->port_num)) {
660219820Sjeff		case IB_LINK_LAYER_INFINIBAND:
661219820Sjeff			ucma_copy_ib_route(&resp, &ctx->cm_id->route);
662219820Sjeff			break;
663219820Sjeff		case IB_LINK_LAYER_ETHERNET:
664219820Sjeff			ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
665219820Sjeff			break;
666219820Sjeff		default:
667219820Sjeff			break;
668219820Sjeff		}
669219820Sjeff	}
670219820Sjeff
671219820Sjeffout:
672219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
673219820Sjeff			 &resp, sizeof(resp)))
674219820Sjeff		ret = -EFAULT;
675219820Sjeff
676219820Sjeff	ucma_put_ctx(ctx);
677219820Sjeff	return ret;
678219820Sjeff}
679219820Sjeff
680219820Sjeffstatic void ucma_copy_conn_param(struct rdma_conn_param *dst,
681219820Sjeff				 struct rdma_ucm_conn_param *src)
682219820Sjeff{
683219820Sjeff	dst->private_data = src->private_data;
684219820Sjeff	dst->private_data_len = src->private_data_len;
685219820Sjeff	dst->responder_resources =src->responder_resources;
686219820Sjeff	dst->initiator_depth = src->initiator_depth;
687219820Sjeff	dst->flow_control = src->flow_control;
688219820Sjeff	dst->retry_count = src->retry_count;
689219820Sjeff	dst->rnr_retry_count = src->rnr_retry_count;
690219820Sjeff	dst->srq = src->srq;
691219820Sjeff	dst->qp_num = src->qp_num;
692219820Sjeff}
693219820Sjeff
694219820Sjeffstatic ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
695219820Sjeff			    int in_len, int out_len)
696219820Sjeff{
697219820Sjeff	struct rdma_ucm_connect cmd;
698219820Sjeff	struct rdma_conn_param conn_param;
699219820Sjeff	struct ucma_context *ctx;
700219820Sjeff	int ret;
701219820Sjeff
702219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
703219820Sjeff		return -EFAULT;
704219820Sjeff
705219820Sjeff	if (!cmd.conn_param.valid)
706219820Sjeff		return -EINVAL;
707219820Sjeff
708219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
709219820Sjeff	if (IS_ERR(ctx))
710219820Sjeff		return PTR_ERR(ctx);
711219820Sjeff
712219820Sjeff	ucma_copy_conn_param(&conn_param, &cmd.conn_param);
713219820Sjeff	ret = rdma_connect(ctx->cm_id, &conn_param);
714219820Sjeff	ucma_put_ctx(ctx);
715219820Sjeff	return ret;
716219820Sjeff}
717219820Sjeff
718219820Sjeffstatic ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
719219820Sjeff			   int in_len, int out_len)
720219820Sjeff{
721219820Sjeff	struct rdma_ucm_listen cmd;
722219820Sjeff	struct ucma_context *ctx;
723219820Sjeff	int ret;
724219820Sjeff
725219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
726219820Sjeff		return -EFAULT;
727219820Sjeff
728219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
729219820Sjeff	if (IS_ERR(ctx))
730219820Sjeff		return PTR_ERR(ctx);
731219820Sjeff
732219820Sjeff	ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
733219820Sjeff		       cmd.backlog : UCMA_MAX_BACKLOG;
734219820Sjeff	ret = rdma_listen(ctx->cm_id, ctx->backlog);
735219820Sjeff	ucma_put_ctx(ctx);
736219820Sjeff	return ret;
737219820Sjeff}
738219820Sjeff
739219820Sjeffstatic ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
740219820Sjeff			   int in_len, int out_len)
741219820Sjeff{
742219820Sjeff	struct rdma_ucm_accept cmd;
743219820Sjeff	struct rdma_conn_param conn_param;
744219820Sjeff	struct ucma_context *ctx;
745219820Sjeff	int ret;
746219820Sjeff
747219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
748219820Sjeff		return -EFAULT;
749219820Sjeff
750219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
751219820Sjeff	if (IS_ERR(ctx))
752219820Sjeff		return PTR_ERR(ctx);
753219820Sjeff
754219820Sjeff	if (cmd.conn_param.valid) {
755219820Sjeff		ctx->uid = cmd.uid;
756219820Sjeff		ucma_copy_conn_param(&conn_param, &cmd.conn_param);
757219820Sjeff		ret = rdma_accept(ctx->cm_id, &conn_param);
758219820Sjeff	} else
759219820Sjeff		ret = rdma_accept(ctx->cm_id, NULL);
760219820Sjeff
761219820Sjeff	ucma_put_ctx(ctx);
762219820Sjeff	return ret;
763219820Sjeff}
764219820Sjeff
765219820Sjeffstatic ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
766219820Sjeff			   int in_len, int out_len)
767219820Sjeff{
768219820Sjeff	struct rdma_ucm_reject cmd;
769219820Sjeff	struct ucma_context *ctx;
770219820Sjeff	int ret;
771219820Sjeff
772219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
773219820Sjeff		return -EFAULT;
774219820Sjeff
775219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
776219820Sjeff	if (IS_ERR(ctx))
777219820Sjeff		return PTR_ERR(ctx);
778219820Sjeff
779219820Sjeff	ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
780219820Sjeff	ucma_put_ctx(ctx);
781219820Sjeff	return ret;
782219820Sjeff}
783219820Sjeff
784219820Sjeffstatic ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
785219820Sjeff			       int in_len, int out_len)
786219820Sjeff{
787219820Sjeff	struct rdma_ucm_disconnect cmd;
788219820Sjeff	struct ucma_context *ctx;
789219820Sjeff	int ret;
790219820Sjeff
791219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
792219820Sjeff		return -EFAULT;
793219820Sjeff
794219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
795219820Sjeff	if (IS_ERR(ctx))
796219820Sjeff		return PTR_ERR(ctx);
797219820Sjeff
798219820Sjeff	ret = rdma_disconnect(ctx->cm_id);
799219820Sjeff	ucma_put_ctx(ctx);
800219820Sjeff	return ret;
801219820Sjeff}
802219820Sjeff
803219820Sjeffstatic ssize_t ucma_init_qp_attr(struct ucma_file *file,
804219820Sjeff				 const char __user *inbuf,
805219820Sjeff				 int in_len, int out_len)
806219820Sjeff{
807219820Sjeff	struct rdma_ucm_init_qp_attr cmd;
808219820Sjeff	struct ib_uverbs_qp_attr resp;
809219820Sjeff	struct ucma_context *ctx;
810219820Sjeff	struct ib_qp_attr qp_attr;
811219820Sjeff	int ret;
812219820Sjeff
813219820Sjeff	if (out_len < sizeof(resp))
814219820Sjeff		return -ENOSPC;
815219820Sjeff
816219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
817219820Sjeff		return -EFAULT;
818219820Sjeff
819219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
820219820Sjeff	if (IS_ERR(ctx))
821219820Sjeff		return PTR_ERR(ctx);
822219820Sjeff
823219820Sjeff	resp.qp_attr_mask = 0;
824219820Sjeff	memset(&qp_attr, 0, sizeof qp_attr);
825219820Sjeff	qp_attr.qp_state = cmd.qp_state;
826219820Sjeff	ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
827219820Sjeff	if (ret)
828219820Sjeff		goto out;
829219820Sjeff
830219820Sjeff	ib_copy_qp_attr_to_user(&resp, &qp_attr);
831219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
832219820Sjeff			 &resp, sizeof(resp)))
833219820Sjeff		ret = -EFAULT;
834219820Sjeff
835219820Sjeffout:
836219820Sjeff	ucma_put_ctx(ctx);
837219820Sjeff	return ret;
838219820Sjeff}
839219820Sjeff
840219820Sjeffstatic int ucma_set_option_id(struct ucma_context *ctx, int optname,
841219820Sjeff			      void *optval, size_t optlen)
842219820Sjeff{
843219820Sjeff	int ret = 0;
844219820Sjeff
845219820Sjeff	switch (optname) {
846219820Sjeff	case RDMA_OPTION_ID_TOS:
847219820Sjeff		if (optlen != sizeof(u8)) {
848219820Sjeff			ret = -EINVAL;
849219820Sjeff			break;
850219820Sjeff		}
851219820Sjeff		rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
852219820Sjeff		break;
853219820Sjeff	default:
854219820Sjeff		ret = -ENOSYS;
855219820Sjeff	}
856219820Sjeff
857219820Sjeff	return ret;
858219820Sjeff}
859219820Sjeff
860219820Sjeffstatic int ucma_set_ib_path(struct ucma_context *ctx,
861219820Sjeff			    struct ib_path_rec_data *path_data, size_t optlen)
862219820Sjeff{
863219820Sjeff	struct ib_sa_path_rec sa_path;
864219820Sjeff	struct rdma_cm_event event;
865219820Sjeff	int ret;
866219820Sjeff
867219820Sjeff	if (optlen % sizeof(*path_data))
868219820Sjeff		return -EINVAL;
869219820Sjeff
870219820Sjeff	for (; optlen; optlen -= sizeof(*path_data), path_data++) {
871219820Sjeff		if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
872219820Sjeff					 IB_PATH_BIDIRECTIONAL))
873219820Sjeff			break;
874219820Sjeff	}
875219820Sjeff
876219820Sjeff	if (!optlen)
877219820Sjeff		return -EINVAL;
878219820Sjeff
879219820Sjeff	ib_sa_unpack_path(path_data->path_rec, &sa_path);
880219820Sjeff	ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
881219820Sjeff	if (ret)
882219820Sjeff		return ret;
883219820Sjeff
884219820Sjeff	memset(&event, 0, sizeof event);
885219820Sjeff	event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
886219820Sjeff	return ucma_event_handler(ctx->cm_id, &event);
887219820Sjeff}
888219820Sjeff
889219820Sjeffstatic int ucma_set_option_ib(struct ucma_context *ctx, int optname,
890219820Sjeff			      void *optval, size_t optlen)
891219820Sjeff{
892219820Sjeff	int ret;
893219820Sjeff
894219820Sjeff	switch (optname) {
895219820Sjeff	case RDMA_OPTION_IB_PATH:
896219820Sjeff		ret = ucma_set_ib_path(ctx, optval, optlen);
897219820Sjeff		break;
898219820Sjeff	default:
899219820Sjeff		ret = -ENOSYS;
900219820Sjeff	}
901219820Sjeff
902219820Sjeff	return ret;
903219820Sjeff}
904219820Sjeff
905219820Sjeffstatic int ucma_set_option_level(struct ucma_context *ctx, int level,
906219820Sjeff				 int optname, void *optval, size_t optlen)
907219820Sjeff{
908219820Sjeff	int ret;
909219820Sjeff
910219820Sjeff	switch (level) {
911219820Sjeff	case RDMA_OPTION_ID:
912219820Sjeff		ret = ucma_set_option_id(ctx, optname, optval, optlen);
913219820Sjeff		break;
914219820Sjeff	case RDMA_OPTION_IB:
915219820Sjeff		ret = ucma_set_option_ib(ctx, optname, optval, optlen);
916219820Sjeff		break;
917219820Sjeff	default:
918219820Sjeff		ret = -ENOSYS;
919219820Sjeff	}
920219820Sjeff
921219820Sjeff	return ret;
922219820Sjeff}
923219820Sjeff
924219820Sjeffstatic ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
925219820Sjeff			       int in_len, int out_len)
926219820Sjeff{
927219820Sjeff	struct rdma_ucm_set_option cmd;
928219820Sjeff	struct ucma_context *ctx;
929219820Sjeff	void *optval;
930219820Sjeff	int ret;
931219820Sjeff
932219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
933219820Sjeff		return -EFAULT;
934219820Sjeff
935219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
936219820Sjeff	if (IS_ERR(ctx))
937219820Sjeff		return PTR_ERR(ctx);
938219820Sjeff
939219820Sjeff	optval = kmalloc(cmd.optlen, GFP_KERNEL);
940219820Sjeff	if (!optval) {
941219820Sjeff		ret = -ENOMEM;
942219820Sjeff		goto out1;
943219820Sjeff	}
944219820Sjeff
945219820Sjeff	if (copy_from_user(optval, (void __user *) (unsigned long) cmd.optval,
946219820Sjeff			   cmd.optlen)) {
947219820Sjeff		ret = -EFAULT;
948219820Sjeff		goto out2;
949219820Sjeff	}
950219820Sjeff
951219820Sjeff	ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
952219820Sjeff				    cmd.optlen);
953219820Sjeffout2:
954219820Sjeff	kfree(optval);
955219820Sjeffout1:
956219820Sjeff	ucma_put_ctx(ctx);
957219820Sjeff	return ret;
958219820Sjeff}
959219820Sjeff
960219820Sjeffstatic ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
961219820Sjeff			   int in_len, int out_len)
962219820Sjeff{
963219820Sjeff	struct rdma_ucm_notify cmd;
964219820Sjeff	struct ucma_context *ctx;
965219820Sjeff	int ret;
966219820Sjeff
967219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
968219820Sjeff		return -EFAULT;
969219820Sjeff
970219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
971219820Sjeff	if (IS_ERR(ctx))
972219820Sjeff		return PTR_ERR(ctx);
973219820Sjeff
974219820Sjeff	ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
975219820Sjeff	ucma_put_ctx(ctx);
976219820Sjeff	return ret;
977219820Sjeff}
978219820Sjeff
979219820Sjeffstatic ssize_t ucma_join_multicast(struct ucma_file *file,
980219820Sjeff				   const char __user *inbuf,
981219820Sjeff				   int in_len, int out_len)
982219820Sjeff{
983219820Sjeff	struct rdma_ucm_join_mcast cmd;
984219820Sjeff	struct rdma_ucm_create_id_resp resp;
985219820Sjeff	struct ucma_context *ctx;
986219820Sjeff	struct ucma_multicast *mc;
987219820Sjeff	int ret;
988219820Sjeff
989219820Sjeff	if (out_len < sizeof(resp))
990219820Sjeff		return -ENOSPC;
991219820Sjeff
992219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
993219820Sjeff		return -EFAULT;
994219820Sjeff
995219820Sjeff	ctx = ucma_get_ctx(file, cmd.id);
996219820Sjeff	if (IS_ERR(ctx))
997219820Sjeff		return PTR_ERR(ctx);
998219820Sjeff
999219820Sjeff	mutex_lock(&file->mut);
1000219820Sjeff	mc = ucma_alloc_multicast(ctx);
1001219820Sjeff	if (!mc) {
1002219820Sjeff		ret = -ENOMEM;
1003219820Sjeff		goto err1;
1004219820Sjeff	}
1005219820Sjeff
1006219820Sjeff	mc->uid = cmd.uid;
1007219820Sjeff	memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
1008219820Sjeff	ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
1009219820Sjeff	if (ret)
1010219820Sjeff		goto err2;
1011219820Sjeff
1012219820Sjeff	resp.id = mc->id;
1013219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
1014219820Sjeff			 &resp, sizeof(resp))) {
1015219820Sjeff		ret = -EFAULT;
1016219820Sjeff		goto err3;
1017219820Sjeff	}
1018219820Sjeff
1019219820Sjeff	mutex_unlock(&file->mut);
1020219820Sjeff	ucma_put_ctx(ctx);
1021219820Sjeff	return 0;
1022219820Sjeff
1023219820Sjefferr3:
1024219820Sjeff	rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
1025219820Sjeff	ucma_cleanup_mc_events(mc);
1026219820Sjefferr2:
1027219820Sjeff	mutex_lock(&mut);
1028219820Sjeff	idr_remove(&multicast_idr, mc->id);
1029219820Sjeff	mutex_unlock(&mut);
1030219820Sjeff	list_del(&mc->list);
1031219820Sjeff	kfree(mc);
1032219820Sjefferr1:
1033219820Sjeff	mutex_unlock(&file->mut);
1034219820Sjeff	ucma_put_ctx(ctx);
1035219820Sjeff	return ret;
1036219820Sjeff}
1037219820Sjeff
1038219820Sjeffstatic ssize_t ucma_leave_multicast(struct ucma_file *file,
1039219820Sjeff				    const char __user *inbuf,
1040219820Sjeff				    int in_len, int out_len)
1041219820Sjeff{
1042219820Sjeff	struct rdma_ucm_destroy_id cmd;
1043219820Sjeff	struct rdma_ucm_destroy_id_resp resp;
1044219820Sjeff	struct ucma_multicast *mc;
1045219820Sjeff	int ret = 0;
1046219820Sjeff
1047219820Sjeff	if (out_len < sizeof(resp))
1048219820Sjeff		return -ENOSPC;
1049219820Sjeff
1050219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1051219820Sjeff		return -EFAULT;
1052219820Sjeff
1053219820Sjeff	mutex_lock(&mut);
1054219820Sjeff	mc = idr_find(&multicast_idr, cmd.id);
1055219820Sjeff	if (!mc)
1056219820Sjeff		mc = ERR_PTR(-ENOENT);
1057219820Sjeff	else if (mc->ctx->file != file)
1058219820Sjeff		mc = ERR_PTR(-EINVAL);
1059219820Sjeff	else {
1060219820Sjeff		idr_remove(&multicast_idr, mc->id);
1061219820Sjeff		atomic_inc(&mc->ctx->ref);
1062219820Sjeff	}
1063219820Sjeff	mutex_unlock(&mut);
1064219820Sjeff
1065219820Sjeff	if (IS_ERR(mc)) {
1066219820Sjeff		ret = PTR_ERR(mc);
1067219820Sjeff		goto out;
1068219820Sjeff	}
1069219820Sjeff
1070219820Sjeff	rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
1071219820Sjeff	mutex_lock(&mc->ctx->file->mut);
1072219820Sjeff	ucma_cleanup_mc_events(mc);
1073219820Sjeff	list_del(&mc->list);
1074219820Sjeff	mutex_unlock(&mc->ctx->file->mut);
1075219820Sjeff
1076219820Sjeff	ucma_put_ctx(mc->ctx);
1077219820Sjeff	resp.events_reported = mc->events_reported;
1078219820Sjeff	kfree(mc);
1079219820Sjeff
1080219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
1081219820Sjeff			 &resp, sizeof(resp)))
1082219820Sjeff		ret = -EFAULT;
1083219820Sjeffout:
1084219820Sjeff	return ret;
1085219820Sjeff}
1086219820Sjeff
1087219820Sjeffstatic void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
1088219820Sjeff{
1089219820Sjeff	/* Acquire mutex's based on pointer comparison to prevent deadlock. */
1090219820Sjeff	if (file1 < file2) {
1091219820Sjeff		mutex_lock(&file1->mut);
1092219820Sjeff		mutex_lock(&file2->mut);
1093219820Sjeff	} else {
1094219820Sjeff		mutex_lock(&file2->mut);
1095219820Sjeff		mutex_lock(&file1->mut);
1096219820Sjeff	}
1097219820Sjeff}
1098219820Sjeff
1099219820Sjeffstatic void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
1100219820Sjeff{
1101219820Sjeff	if (file1 < file2) {
1102219820Sjeff		mutex_unlock(&file2->mut);
1103219820Sjeff		mutex_unlock(&file1->mut);
1104219820Sjeff	} else {
1105219820Sjeff		mutex_unlock(&file1->mut);
1106219820Sjeff		mutex_unlock(&file2->mut);
1107219820Sjeff	}
1108219820Sjeff}
1109219820Sjeff
1110219820Sjeffstatic void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
1111219820Sjeff{
1112219820Sjeff	struct ucma_event *uevent, *tmp;
1113219820Sjeff
1114219820Sjeff	list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
1115219820Sjeff		if (uevent->ctx == ctx)
1116219820Sjeff			list_move_tail(&uevent->list, &file->event_list);
1117219820Sjeff}
1118219820Sjeff
1119219820Sjeffstatic ssize_t ucma_migrate_id(struct ucma_file *new_file,
1120219820Sjeff			       const char __user *inbuf,
1121219820Sjeff			       int in_len, int out_len)
1122219820Sjeff{
1123219820Sjeff	struct rdma_ucm_migrate_id cmd;
1124219820Sjeff	struct rdma_ucm_migrate_resp resp;
1125219820Sjeff	struct ucma_context *ctx;
1126219820Sjeff	struct file *filp;
1127219820Sjeff	struct ucma_file *cur_file;
1128219820Sjeff	int ret = 0;
1129219820Sjeff
1130219820Sjeff	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1131219820Sjeff		return -EFAULT;
1132219820Sjeff
1133219820Sjeff	/* Get current fd to protect against it being closed */
1134219820Sjeff	filp = fget(cmd.fd);
1135219820Sjeff	if (!filp)
1136219820Sjeff		return -ENOENT;
1137219820Sjeff
1138219820Sjeff	/* Validate current fd and prevent destruction of id. */
1139219820Sjeff	ctx = ucma_get_ctx(filp->private_data, cmd.id);
1140219820Sjeff	if (IS_ERR(ctx)) {
1141219820Sjeff		ret = PTR_ERR(ctx);
1142219820Sjeff		goto file_put;
1143219820Sjeff	}
1144219820Sjeff
1145219820Sjeff	cur_file = ctx->file;
1146219820Sjeff	if (cur_file == new_file) {
1147219820Sjeff		resp.events_reported = ctx->events_reported;
1148219820Sjeff		goto response;
1149219820Sjeff	}
1150219820Sjeff
1151219820Sjeff	/*
1152219820Sjeff	 * Migrate events between fd's, maintaining order, and avoiding new
1153219820Sjeff	 * events being added before existing events.
1154219820Sjeff	 */
1155219820Sjeff	ucma_lock_files(cur_file, new_file);
1156219820Sjeff	mutex_lock(&mut);
1157219820Sjeff
1158219820Sjeff	list_move_tail(&ctx->list, &new_file->ctx_list);
1159219820Sjeff	ucma_move_events(ctx, new_file);
1160219820Sjeff	ctx->file = new_file;
1161219820Sjeff	resp.events_reported = ctx->events_reported;
1162219820Sjeff
1163219820Sjeff	mutex_unlock(&mut);
1164219820Sjeff	ucma_unlock_files(cur_file, new_file);
1165219820Sjeff
1166219820Sjeffresponse:
1167219820Sjeff	if (copy_to_user((void __user *)(unsigned long)cmd.response,
1168219820Sjeff			 &resp, sizeof(resp)))
1169219820Sjeff		ret = -EFAULT;
1170219820Sjeff
1171219820Sjeff	ucma_put_ctx(ctx);
1172219820Sjefffile_put:
1173219820Sjeff	fput(filp);
1174219820Sjeff	return ret;
1175219820Sjeff}
1176219820Sjeff
1177219820Sjeffstatic ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
1178219820Sjeff				   const char __user *inbuf,
1179219820Sjeff				   int in_len, int out_len) = {
1180219820Sjeff	[RDMA_USER_CM_CMD_CREATE_ID]	= ucma_create_id,
1181219820Sjeff	[RDMA_USER_CM_CMD_DESTROY_ID]	= ucma_destroy_id,
1182219820Sjeff	[RDMA_USER_CM_CMD_BIND_ADDR]	= ucma_bind_addr,
1183219820Sjeff	[RDMA_USER_CM_CMD_RESOLVE_ADDR]	= ucma_resolve_addr,
1184219820Sjeff	[RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
1185219820Sjeff	[RDMA_USER_CM_CMD_QUERY_ROUTE]	= ucma_query_route,
1186219820Sjeff	[RDMA_USER_CM_CMD_CONNECT]	= ucma_connect,
1187219820Sjeff	[RDMA_USER_CM_CMD_LISTEN]	= ucma_listen,
1188219820Sjeff	[RDMA_USER_CM_CMD_ACCEPT]	= ucma_accept,
1189219820Sjeff	[RDMA_USER_CM_CMD_REJECT]	= ucma_reject,
1190219820Sjeff	[RDMA_USER_CM_CMD_DISCONNECT]	= ucma_disconnect,
1191219820Sjeff	[RDMA_USER_CM_CMD_INIT_QP_ATTR]	= ucma_init_qp_attr,
1192219820Sjeff	[RDMA_USER_CM_CMD_GET_EVENT]	= ucma_get_event,
1193219820Sjeff	[RDMA_USER_CM_CMD_GET_OPTION]	= NULL,
1194219820Sjeff	[RDMA_USER_CM_CMD_SET_OPTION]	= ucma_set_option,
1195219820Sjeff	[RDMA_USER_CM_CMD_NOTIFY]	= ucma_notify,
1196219820Sjeff	[RDMA_USER_CM_CMD_JOIN_MCAST]	= ucma_join_multicast,
1197219820Sjeff	[RDMA_USER_CM_CMD_LEAVE_MCAST]	= ucma_leave_multicast,
1198219820Sjeff	[RDMA_USER_CM_CMD_MIGRATE_ID]	= ucma_migrate_id
1199219820Sjeff};
1200219820Sjeff
1201219820Sjeffstatic ssize_t ucma_write(struct file *filp, const char __user *buf,
1202219820Sjeff			  size_t len, loff_t *pos)
1203219820Sjeff{
1204219820Sjeff	struct ucma_file *file = filp->private_data;
1205219820Sjeff	struct rdma_ucm_cmd_hdr hdr;
1206219820Sjeff	ssize_t ret;
1207219820Sjeff
1208219820Sjeff	if (len < sizeof(hdr))
1209219820Sjeff		return -EINVAL;
1210219820Sjeff
1211219820Sjeff	if (copy_from_user(&hdr, buf, sizeof(hdr)))
1212219820Sjeff		return -EFAULT;
1213219820Sjeff
1214219820Sjeff	if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
1215219820Sjeff		return -EINVAL;
1216219820Sjeff
1217219820Sjeff	if (hdr.in + sizeof(hdr) > len)
1218219820Sjeff		return -EINVAL;
1219219820Sjeff
1220219820Sjeff	if (!ucma_cmd_table[hdr.cmd])
1221219820Sjeff		return -ENOSYS;
1222219820Sjeff
1223219820Sjeff	ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
1224219820Sjeff	if (!ret)
1225219820Sjeff		ret = len;
1226219820Sjeff
1227219820Sjeff	return ret;
1228219820Sjeff}
1229219820Sjeff
1230219820Sjeffstatic unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
1231219820Sjeff{
1232219820Sjeff	struct ucma_file *file = filp->private_data;
1233219820Sjeff	unsigned int mask = 0;
1234219820Sjeff
1235219820Sjeff	poll_wait(filp, &file->poll_wait, wait);
1236219820Sjeff
1237219820Sjeff	if (!list_empty(&file->event_list))
1238219820Sjeff		mask = POLLIN | POLLRDNORM;
1239219820Sjeff
1240219820Sjeff	return mask;
1241219820Sjeff}
1242219820Sjeff
1243219820Sjeff/*
1244219820Sjeff * ucma_open() does not need the BKL:
1245219820Sjeff *
1246219820Sjeff *  - no global state is referred to;
1247219820Sjeff *  - there is no ioctl method to race against;
1248219820Sjeff *  - no further module initialization is required for open to work
1249219820Sjeff *    after the device is registered.
1250219820Sjeff */
1251219820Sjeffstatic int ucma_open(struct inode *inode, struct file *filp)
1252219820Sjeff{
1253219820Sjeff	struct ucma_file *file;
1254219820Sjeff
1255219820Sjeff	file = kmalloc(sizeof *file, GFP_KERNEL);
1256219820Sjeff	if (!file)
1257219820Sjeff		return -ENOMEM;
1258219820Sjeff
1259219820Sjeff	INIT_LIST_HEAD(&file->event_list);
1260219820Sjeff	INIT_LIST_HEAD(&file->ctx_list);
1261219820Sjeff	init_waitqueue_head(&file->poll_wait);
1262219820Sjeff	mutex_init(&file->mut);
1263219820Sjeff
1264219820Sjeff	filp->private_data = file;
1265219820Sjeff	file->filp = filp;
1266219820Sjeff	return 0;
1267219820Sjeff}
1268219820Sjeff
1269219820Sjeffstatic int ucma_close(struct inode *inode, struct file *filp)
1270219820Sjeff{
1271219820Sjeff	struct ucma_file *file = filp->private_data;
1272219820Sjeff	struct ucma_context *ctx, *tmp;
1273219820Sjeff
1274219820Sjeff	mutex_lock(&file->mut);
1275219820Sjeff	list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
1276219820Sjeff		mutex_unlock(&file->mut);
1277219820Sjeff
1278219820Sjeff		mutex_lock(&mut);
1279219820Sjeff		idr_remove(&ctx_idr, ctx->id);
1280219820Sjeff		mutex_unlock(&mut);
1281219820Sjeff
1282219820Sjeff		ucma_free_ctx(ctx);
1283219820Sjeff		mutex_lock(&file->mut);
1284219820Sjeff	}
1285219820Sjeff	mutex_unlock(&file->mut);
1286219820Sjeff	kfree(file);
1287219820Sjeff	return 0;
1288219820Sjeff}
1289219820Sjeff
1290304846Shselaskystatic long
1291304846Shselaskyucma_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1292304846Shselasky{
1293304846Shselasky
1294304846Shselasky	switch (cmd) {
1295304846Shselasky	case FIONBIO:
1296304846Shselasky	case FIOASYNC:
1297304846Shselasky		return (0);
1298304846Shselasky	default:
1299304846Shselasky		return (-ENOTTY);
1300304846Shselasky	}
1301304846Shselasky}
1302304846Shselasky
1303219820Sjeffstatic const struct file_operations ucma_fops = {
1304219820Sjeff	.owner 	 = THIS_MODULE,
1305219820Sjeff	.open 	 = ucma_open,
1306219820Sjeff	.release = ucma_close,
1307219820Sjeff	.write	 = ucma_write,
1308304846Shselasky	.unlocked_ioctl = ucma_ioctl,
1309219820Sjeff	.poll    = ucma_poll,
1310219820Sjeff};
1311219820Sjeff
1312219820Sjeffstatic struct miscdevice ucma_misc = {
1313219820Sjeff	.minor	= MISC_DYNAMIC_MINOR,
1314219820Sjeff	.name	= "rdma_cm",
1315219820Sjeff	.fops	= &ucma_fops,
1316219820Sjeff};
1317219820Sjeff
1318219820Sjeffstatic ssize_t show_abi_version(struct device *dev,
1319219820Sjeff				struct device_attribute *attr,
1320219820Sjeff				char *buf)
1321219820Sjeff{
1322219820Sjeff	return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
1323219820Sjeff}
1324219820Sjeffstatic DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1325219820Sjeff
1326219820Sjeffstatic int __init ucma_init(void)
1327219820Sjeff{
1328219820Sjeff	int ret;
1329219820Sjeff
1330219820Sjeff	ret = misc_register(&ucma_misc);
1331219820Sjeff	if (ret)
1332219820Sjeff		return ret;
1333219820Sjeff
1334219820Sjeff	ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
1335219820Sjeff	if (ret) {
1336219820Sjeff		printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
1337219820Sjeff		goto err;
1338219820Sjeff	}
1339219820Sjeff	return 0;
1340219820Sjefferr:
1341219820Sjeff	misc_deregister(&ucma_misc);
1342219820Sjeff	return ret;
1343219820Sjeff}
1344219820Sjeff
1345219820Sjeffstatic void __exit ucma_cleanup(void)
1346219820Sjeff{
1347219820Sjeff	device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1348219820Sjeff	misc_deregister(&ucma_misc);
1349219820Sjeff	idr_destroy(&ctx_idr);
1350219820Sjeff}
1351219820Sjeff
1352219820Sjeffmodule_init(ucma_init);
1353219820Sjeffmodule_exit(ucma_cleanup);
1354