1/*
2 * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3 * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 *     Redistribution and use in source and binary forms, with or
12 *     without modification, are permitted provided that the following
13 *     conditions are met:
14 *
15 *      - Redistributions of source code must retain the above
16 *	copyright notice, this list of conditions and the following
17 *	disclaimer.
18 *
19 *      - Redistributions in binary form must reproduce the above
20 *	copyright notice, this list of conditions and the following
21 *	disclaimer in the documentation and/or other materials
22 *	provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 * $Id: ucm.c,v 1.1.1.1 2007/10/11 23:31:50 Exp $
34 */
35
36#include <linux/completion.h>
37#include <linux/init.h>
38#include <linux/fs.h>
39#include <linux/module.h>
40#include <linux/device.h>
41#include <linux/err.h>
42#include <linux/poll.h>
43#include <linux/file.h>
44#include <linux/mount.h>
45#include <linux/cdev.h>
46#include <linux/idr.h>
47#include <linux/mutex.h>
48
49#include <asm/uaccess.h>
50
51#include <rdma/ib_cm.h>
52#include <rdma/ib_user_cm.h>
53#include <rdma/ib_marshall.h>
54
55MODULE_AUTHOR("Libor Michalek");
56MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
57MODULE_LICENSE("Dual BSD/GPL");
58
59struct ib_ucm_device {
60	int			devnum;
61	struct cdev		dev;
62	struct class_device	class_dev;
63	struct ib_device	*ib_dev;
64};
65
66struct ib_ucm_file {
67	struct mutex file_mutex;
68	struct file *filp;
69	struct ib_ucm_device *device;
70
71	struct list_head  ctxs;
72	struct list_head  events;
73	wait_queue_head_t poll_wait;
74};
75
76struct ib_ucm_context {
77	int                 id;
78	struct completion   comp;
79	atomic_t            ref;
80	int		    events_reported;
81
82	struct ib_ucm_file *file;
83	struct ib_cm_id    *cm_id;
84	__u64		   uid;
85
86	struct list_head    events;    /* list of pending events. */
87	struct list_head    file_list; /* member in file ctx list */
88};
89
90struct ib_ucm_event {
91	struct ib_ucm_context *ctx;
92	struct list_head file_list; /* member in file event list */
93	struct list_head ctx_list;  /* member in ctx event list */
94
95	struct ib_cm_id *cm_id;
96	struct ib_ucm_event_resp resp;
97	void *data;
98	void *info;
99	int data_len;
100	int info_len;
101};
102
103enum {
104	IB_UCM_MAJOR = 231,
105	IB_UCM_BASE_MINOR = 224,
106	IB_UCM_MAX_DEVICES = 32
107};
108
109#define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
110
111static void ib_ucm_add_one(struct ib_device *device);
112static void ib_ucm_remove_one(struct ib_device *device);
113
114static struct ib_client ucm_client = {
115	.name   = "ucm",
116	.add    = ib_ucm_add_one,
117	.remove = ib_ucm_remove_one
118};
119
120static DEFINE_MUTEX(ctx_id_mutex);
121static DEFINE_IDR(ctx_id_table);
122static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
123
124static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
125{
126	struct ib_ucm_context *ctx;
127
128	mutex_lock(&ctx_id_mutex);
129	ctx = idr_find(&ctx_id_table, id);
130	if (!ctx)
131		ctx = ERR_PTR(-ENOENT);
132	else if (ctx->file != file)
133		ctx = ERR_PTR(-EINVAL);
134	else
135		atomic_inc(&ctx->ref);
136	mutex_unlock(&ctx_id_mutex);
137
138	return ctx;
139}
140
141static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
142{
143	if (atomic_dec_and_test(&ctx->ref))
144		complete(&ctx->comp);
145}
146
147static inline int ib_ucm_new_cm_id(int event)
148{
149	return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
150}
151
152static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
153{
154	struct ib_ucm_event *uevent;
155
156	mutex_lock(&ctx->file->file_mutex);
157	list_del(&ctx->file_list);
158	while (!list_empty(&ctx->events)) {
159
160		uevent = list_entry(ctx->events.next,
161				    struct ib_ucm_event, ctx_list);
162		list_del(&uevent->file_list);
163		list_del(&uevent->ctx_list);
164		mutex_unlock(&ctx->file->file_mutex);
165
166		/* clear incoming connections. */
167		if (ib_ucm_new_cm_id(uevent->resp.event))
168			ib_destroy_cm_id(uevent->cm_id);
169
170		kfree(uevent);
171		mutex_lock(&ctx->file->file_mutex);
172	}
173	mutex_unlock(&ctx->file->file_mutex);
174}
175
176static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
177{
178	struct ib_ucm_context *ctx;
179	int result;
180
181	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
182	if (!ctx)
183		return NULL;
184
185	atomic_set(&ctx->ref, 1);
186	init_completion(&ctx->comp);
187	ctx->file = file;
188	INIT_LIST_HEAD(&ctx->events);
189
190	do {
191		result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
192		if (!result)
193			goto error;
194
195		mutex_lock(&ctx_id_mutex);
196		result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
197		mutex_unlock(&ctx_id_mutex);
198	} while (result == -EAGAIN);
199
200	if (result)
201		goto error;
202
203	list_add_tail(&ctx->file_list, &file->ctxs);
204	return ctx;
205
206error:
207	kfree(ctx);
208	return NULL;
209}
210
211static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
212				 struct ib_cm_req_event_param *kreq)
213{
214	ureq->remote_ca_guid             = kreq->remote_ca_guid;
215	ureq->remote_qkey                = kreq->remote_qkey;
216	ureq->remote_qpn                 = kreq->remote_qpn;
217	ureq->qp_type                    = kreq->qp_type;
218	ureq->starting_psn               = kreq->starting_psn;
219	ureq->responder_resources        = kreq->responder_resources;
220	ureq->initiator_depth            = kreq->initiator_depth;
221	ureq->local_cm_response_timeout  = kreq->local_cm_response_timeout;
222	ureq->flow_control               = kreq->flow_control;
223	ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
224	ureq->retry_count                = kreq->retry_count;
225	ureq->rnr_retry_count            = kreq->rnr_retry_count;
226	ureq->srq                        = kreq->srq;
227	ureq->port			 = kreq->port;
228
229	ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
230	if (kreq->alternate_path)
231		ib_copy_path_rec_to_user(&ureq->alternate_path,
232					 kreq->alternate_path);
233}
234
235static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
236				 struct ib_cm_rep_event_param *krep)
237{
238	urep->remote_ca_guid      = krep->remote_ca_guid;
239	urep->remote_qkey         = krep->remote_qkey;
240	urep->remote_qpn          = krep->remote_qpn;
241	urep->starting_psn        = krep->starting_psn;
242	urep->responder_resources = krep->responder_resources;
243	urep->initiator_depth     = krep->initiator_depth;
244	urep->target_ack_delay    = krep->target_ack_delay;
245	urep->failover_accepted   = krep->failover_accepted;
246	urep->flow_control        = krep->flow_control;
247	urep->rnr_retry_count     = krep->rnr_retry_count;
248	urep->srq                 = krep->srq;
249}
250
251static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
252				      struct ib_cm_sidr_rep_event_param *krep)
253{
254	urep->status = krep->status;
255	urep->qkey   = krep->qkey;
256	urep->qpn    = krep->qpn;
257};
258
259static int ib_ucm_event_process(struct ib_cm_event *evt,
260				struct ib_ucm_event *uvt)
261{
262	void *info = NULL;
263
264	switch (evt->event) {
265	case IB_CM_REQ_RECEIVED:
266		ib_ucm_event_req_get(&uvt->resp.u.req_resp,
267				     &evt->param.req_rcvd);
268		uvt->data_len      = IB_CM_REQ_PRIVATE_DATA_SIZE;
269		uvt->resp.present  = IB_UCM_PRES_PRIMARY;
270		uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
271				      IB_UCM_PRES_ALTERNATE : 0);
272		break;
273	case IB_CM_REP_RECEIVED:
274		ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
275				     &evt->param.rep_rcvd);
276		uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
277		break;
278	case IB_CM_RTU_RECEIVED:
279		uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
280		uvt->resp.u.send_status = evt->param.send_status;
281		break;
282	case IB_CM_DREQ_RECEIVED:
283		uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
284		uvt->resp.u.send_status = evt->param.send_status;
285		break;
286	case IB_CM_DREP_RECEIVED:
287		uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
288		uvt->resp.u.send_status = evt->param.send_status;
289		break;
290	case IB_CM_MRA_RECEIVED:
291		uvt->resp.u.mra_resp.timeout =
292					evt->param.mra_rcvd.service_timeout;
293		uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
294		break;
295	case IB_CM_REJ_RECEIVED:
296		uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
297		uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
298		uvt->info_len = evt->param.rej_rcvd.ari_length;
299		info	      = evt->param.rej_rcvd.ari;
300		break;
301	case IB_CM_LAP_RECEIVED:
302		ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
303					 evt->param.lap_rcvd.alternate_path);
304		uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
305		uvt->resp.present = IB_UCM_PRES_ALTERNATE;
306		break;
307	case IB_CM_APR_RECEIVED:
308		uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
309		uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
310		uvt->info_len = evt->param.apr_rcvd.info_len;
311		info	      = evt->param.apr_rcvd.apr_info;
312		break;
313	case IB_CM_SIDR_REQ_RECEIVED:
314		uvt->resp.u.sidr_req_resp.pkey =
315					evt->param.sidr_req_rcvd.pkey;
316		uvt->resp.u.sidr_req_resp.port =
317					evt->param.sidr_req_rcvd.port;
318		uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
319		break;
320	case IB_CM_SIDR_REP_RECEIVED:
321		ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
322					  &evt->param.sidr_rep_rcvd);
323		uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
324		uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
325		info	      = evt->param.sidr_rep_rcvd.info;
326		break;
327	default:
328		uvt->resp.u.send_status = evt->param.send_status;
329		break;
330	}
331
332	if (uvt->data_len) {
333		uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL);
334		if (!uvt->data)
335			goto err1;
336
337		uvt->resp.present |= IB_UCM_PRES_DATA;
338	}
339
340	if (uvt->info_len) {
341		uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL);
342		if (!uvt->info)
343			goto err2;
344
345		uvt->resp.present |= IB_UCM_PRES_INFO;
346	}
347	return 0;
348
349err2:
350	kfree(uvt->data);
351err1:
352	return -ENOMEM;
353}
354
355static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
356				struct ib_cm_event *event)
357{
358	struct ib_ucm_event *uevent;
359	struct ib_ucm_context *ctx;
360	int result = 0;
361
362	ctx = cm_id->context;
363
364	uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
365	if (!uevent)
366		goto err1;
367
368	uevent->ctx = ctx;
369	uevent->cm_id = cm_id;
370	uevent->resp.uid = ctx->uid;
371	uevent->resp.id = ctx->id;
372	uevent->resp.event = event->event;
373
374	result = ib_ucm_event_process(event, uevent);
375	if (result)
376		goto err2;
377
378	mutex_lock(&ctx->file->file_mutex);
379	list_add_tail(&uevent->file_list, &ctx->file->events);
380	list_add_tail(&uevent->ctx_list, &ctx->events);
381	wake_up_interruptible(&ctx->file->poll_wait);
382	mutex_unlock(&ctx->file->file_mutex);
383	return 0;
384
385err2:
386	kfree(uevent);
387err1:
388	/* Destroy new cm_id's */
389	return ib_ucm_new_cm_id(event->event);
390}
391
392static ssize_t ib_ucm_event(struct ib_ucm_file *file,
393			    const char __user *inbuf,
394			    int in_len, int out_len)
395{
396	struct ib_ucm_context *ctx;
397	struct ib_ucm_event_get cmd;
398	struct ib_ucm_event *uevent;
399	int result = 0;
400	DEFINE_WAIT(wait);
401
402	if (out_len < sizeof(struct ib_ucm_event_resp))
403		return -ENOSPC;
404
405	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
406		return -EFAULT;
407
408	mutex_lock(&file->file_mutex);
409	while (list_empty(&file->events)) {
410		mutex_unlock(&file->file_mutex);
411
412		if (file->filp->f_flags & O_NONBLOCK)
413			return -EAGAIN;
414
415		if (wait_event_interruptible(file->poll_wait,
416					     !list_empty(&file->events)))
417			return -ERESTARTSYS;
418
419		mutex_lock(&file->file_mutex);
420	}
421
422	uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
423
424	if (ib_ucm_new_cm_id(uevent->resp.event)) {
425		ctx = ib_ucm_ctx_alloc(file);
426		if (!ctx) {
427			result = -ENOMEM;
428			goto done;
429		}
430
431		ctx->cm_id = uevent->cm_id;
432		ctx->cm_id->context = ctx;
433		uevent->resp.id = ctx->id;
434	}
435
436	if (copy_to_user((void __user *)(unsigned long)cmd.response,
437			 &uevent->resp, sizeof(uevent->resp))) {
438		result = -EFAULT;
439		goto done;
440	}
441
442	if (uevent->data) {
443		if (cmd.data_len < uevent->data_len) {
444			result = -ENOMEM;
445			goto done;
446		}
447		if (copy_to_user((void __user *)(unsigned long)cmd.data,
448				 uevent->data, uevent->data_len)) {
449			result = -EFAULT;
450			goto done;
451		}
452	}
453
454	if (uevent->info) {
455		if (cmd.info_len < uevent->info_len) {
456			result = -ENOMEM;
457			goto done;
458		}
459		if (copy_to_user((void __user *)(unsigned long)cmd.info,
460				 uevent->info, uevent->info_len)) {
461			result = -EFAULT;
462			goto done;
463		}
464	}
465
466	list_del(&uevent->file_list);
467	list_del(&uevent->ctx_list);
468	uevent->ctx->events_reported++;
469
470	kfree(uevent->data);
471	kfree(uevent->info);
472	kfree(uevent);
473done:
474	mutex_unlock(&file->file_mutex);
475	return result;
476}
477
478static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
479				const char __user *inbuf,
480				int in_len, int out_len)
481{
482	struct ib_ucm_create_id cmd;
483	struct ib_ucm_create_id_resp resp;
484	struct ib_ucm_context *ctx;
485	int result;
486
487	if (out_len < sizeof(resp))
488		return -ENOSPC;
489
490	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
491		return -EFAULT;
492
493	mutex_lock(&file->file_mutex);
494	ctx = ib_ucm_ctx_alloc(file);
495	mutex_unlock(&file->file_mutex);
496	if (!ctx)
497		return -ENOMEM;
498
499	ctx->uid = cmd.uid;
500	ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
501				     ib_ucm_event_handler, ctx);
502	if (IS_ERR(ctx->cm_id)) {
503		result = PTR_ERR(ctx->cm_id);
504		goto err1;
505	}
506
507	resp.id = ctx->id;
508	if (copy_to_user((void __user *)(unsigned long)cmd.response,
509			 &resp, sizeof(resp))) {
510		result = -EFAULT;
511		goto err2;
512	}
513	return 0;
514
515err2:
516	ib_destroy_cm_id(ctx->cm_id);
517err1:
518	mutex_lock(&ctx_id_mutex);
519	idr_remove(&ctx_id_table, ctx->id);
520	mutex_unlock(&ctx_id_mutex);
521	kfree(ctx);
522	return result;
523}
524
525static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
526				 const char __user *inbuf,
527				 int in_len, int out_len)
528{
529	struct ib_ucm_destroy_id cmd;
530	struct ib_ucm_destroy_id_resp resp;
531	struct ib_ucm_context *ctx;
532	int result = 0;
533
534	if (out_len < sizeof(resp))
535		return -ENOSPC;
536
537	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
538		return -EFAULT;
539
540	mutex_lock(&ctx_id_mutex);
541	ctx = idr_find(&ctx_id_table, cmd.id);
542	if (!ctx)
543		ctx = ERR_PTR(-ENOENT);
544	else if (ctx->file != file)
545		ctx = ERR_PTR(-EINVAL);
546	else
547		idr_remove(&ctx_id_table, ctx->id);
548	mutex_unlock(&ctx_id_mutex);
549
550	if (IS_ERR(ctx))
551		return PTR_ERR(ctx);
552
553	ib_ucm_ctx_put(ctx);
554	wait_for_completion(&ctx->comp);
555
556	/* No new events will be generated after destroying the cm_id. */
557	ib_destroy_cm_id(ctx->cm_id);
558	/* Cleanup events not yet reported to the user. */
559	ib_ucm_cleanup_events(ctx);
560
561	resp.events_reported = ctx->events_reported;
562	if (copy_to_user((void __user *)(unsigned long)cmd.response,
563			 &resp, sizeof(resp)))
564		result = -EFAULT;
565
566	kfree(ctx);
567	return result;
568}
569
570static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
571			      const char __user *inbuf,
572			      int in_len, int out_len)
573{
574	struct ib_ucm_attr_id_resp resp;
575	struct ib_ucm_attr_id cmd;
576	struct ib_ucm_context *ctx;
577	int result = 0;
578
579	if (out_len < sizeof(resp))
580		return -ENOSPC;
581
582	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
583		return -EFAULT;
584
585	ctx = ib_ucm_ctx_get(file, cmd.id);
586	if (IS_ERR(ctx))
587		return PTR_ERR(ctx);
588
589	resp.service_id   = ctx->cm_id->service_id;
590	resp.service_mask = ctx->cm_id->service_mask;
591	resp.local_id     = ctx->cm_id->local_id;
592	resp.remote_id    = ctx->cm_id->remote_id;
593
594	if (copy_to_user((void __user *)(unsigned long)cmd.response,
595			 &resp, sizeof(resp)))
596		result = -EFAULT;
597
598	ib_ucm_ctx_put(ctx);
599	return result;
600}
601
602static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
603				   const char __user *inbuf,
604				   int in_len, int out_len)
605{
606	struct ib_uverbs_qp_attr resp;
607	struct ib_ucm_init_qp_attr cmd;
608	struct ib_ucm_context *ctx;
609	struct ib_qp_attr qp_attr;
610	int result = 0;
611
612	if (out_len < sizeof(resp))
613		return -ENOSPC;
614
615	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
616		return -EFAULT;
617
618	ctx = ib_ucm_ctx_get(file, cmd.id);
619	if (IS_ERR(ctx))
620		return PTR_ERR(ctx);
621
622	resp.qp_attr_mask = 0;
623	memset(&qp_attr, 0, sizeof qp_attr);
624	qp_attr.qp_state = cmd.qp_state;
625	result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
626	if (result)
627		goto out;
628
629	ib_copy_qp_attr_to_user(&resp, &qp_attr);
630
631	if (copy_to_user((void __user *)(unsigned long)cmd.response,
632			 &resp, sizeof(resp)))
633		result = -EFAULT;
634
635out:
636	ib_ucm_ctx_put(ctx);
637	return result;
638}
639
640static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
641{
642	service_id &= service_mask;
643
644	if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
645	    ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
646		return -EINVAL;
647
648	return 0;
649}
650
651static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
652			     const char __user *inbuf,
653			     int in_len, int out_len)
654{
655	struct ib_ucm_listen cmd;
656	struct ib_ucm_context *ctx;
657	int result;
658
659	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
660		return -EFAULT;
661
662	ctx = ib_ucm_ctx_get(file, cmd.id);
663	if (IS_ERR(ctx))
664		return PTR_ERR(ctx);
665
666	result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
667	if (result)
668		goto out;
669
670	result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask,
671			      NULL);
672out:
673	ib_ucm_ctx_put(ctx);
674	return result;
675}
676
677static ssize_t ib_ucm_notify(struct ib_ucm_file *file,
678			     const char __user *inbuf,
679			     int in_len, int out_len)
680{
681	struct ib_ucm_notify cmd;
682	struct ib_ucm_context *ctx;
683	int result;
684
685	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
686		return -EFAULT;
687
688	ctx = ib_ucm_ctx_get(file, cmd.id);
689	if (IS_ERR(ctx))
690		return PTR_ERR(ctx);
691
692	result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
693	ib_ucm_ctx_put(ctx);
694	return result;
695}
696
697static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
698{
699	void *data;
700
701	*dest = NULL;
702
703	if (!len)
704		return 0;
705
706	data = kmalloc(len, GFP_KERNEL);
707	if (!data)
708		return -ENOMEM;
709
710	if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
711		kfree(data);
712		return -EFAULT;
713	}
714
715	*dest = data;
716	return 0;
717}
718
719static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
720{
721	struct ib_user_path_rec upath;
722	struct ib_sa_path_rec  *sa_path;
723
724	*path = NULL;
725
726	if (!src)
727		return 0;
728
729	sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
730	if (!sa_path)
731		return -ENOMEM;
732
733	if (copy_from_user(&upath, (void __user *)(unsigned long)src,
734			   sizeof(upath))) {
735
736		kfree(sa_path);
737		return -EFAULT;
738	}
739
740	ib_copy_path_rec_from_user(sa_path, &upath);
741	*path = sa_path;
742	return 0;
743}
744
745static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
746			       const char __user *inbuf,
747			       int in_len, int out_len)
748{
749	struct ib_cm_req_param param;
750	struct ib_ucm_context *ctx;
751	struct ib_ucm_req cmd;
752	int result;
753
754	param.private_data   = NULL;
755	param.primary_path   = NULL;
756	param.alternate_path = NULL;
757
758	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
759		return -EFAULT;
760
761	result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
762	if (result)
763		goto done;
764
765	result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
766	if (result)
767		goto done;
768
769	result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
770	if (result)
771		goto done;
772
773	param.private_data_len           = cmd.len;
774	param.service_id                 = cmd.sid;
775	param.qp_num                     = cmd.qpn;
776	param.qp_type                    = cmd.qp_type;
777	param.starting_psn               = cmd.psn;
778	param.peer_to_peer               = cmd.peer_to_peer;
779	param.responder_resources        = cmd.responder_resources;
780	param.initiator_depth            = cmd.initiator_depth;
781	param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
782	param.flow_control               = cmd.flow_control;
783	param.local_cm_response_timeout  = cmd.local_cm_response_timeout;
784	param.retry_count                = cmd.retry_count;
785	param.rnr_retry_count            = cmd.rnr_retry_count;
786	param.max_cm_retries             = cmd.max_cm_retries;
787	param.srq                        = cmd.srq;
788
789	ctx = ib_ucm_ctx_get(file, cmd.id);
790	if (!IS_ERR(ctx)) {
791		result = ib_send_cm_req(ctx->cm_id, &param);
792		ib_ucm_ctx_put(ctx);
793	} else
794		result = PTR_ERR(ctx);
795
796done:
797	kfree(param.private_data);
798	kfree(param.primary_path);
799	kfree(param.alternate_path);
800	return result;
801}
802
803static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
804			       const char __user *inbuf,
805			       int in_len, int out_len)
806{
807	struct ib_cm_rep_param param;
808	struct ib_ucm_context *ctx;
809	struct ib_ucm_rep cmd;
810	int result;
811
812	param.private_data = NULL;
813
814	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
815		return -EFAULT;
816
817	result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
818	if (result)
819		return result;
820
821	param.qp_num              = cmd.qpn;
822	param.starting_psn        = cmd.psn;
823	param.private_data_len    = cmd.len;
824	param.responder_resources = cmd.responder_resources;
825	param.initiator_depth     = cmd.initiator_depth;
826	param.target_ack_delay    = cmd.target_ack_delay;
827	param.failover_accepted   = cmd.failover_accepted;
828	param.flow_control        = cmd.flow_control;
829	param.rnr_retry_count     = cmd.rnr_retry_count;
830	param.srq                 = cmd.srq;
831
832	ctx = ib_ucm_ctx_get(file, cmd.id);
833	if (!IS_ERR(ctx)) {
834		ctx->uid = cmd.uid;
835		result = ib_send_cm_rep(ctx->cm_id, &param);
836		ib_ucm_ctx_put(ctx);
837	} else
838		result = PTR_ERR(ctx);
839
840	kfree(param.private_data);
841	return result;
842}
843
844static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
845					const char __user *inbuf, int in_len,
846					int (*func)(struct ib_cm_id *cm_id,
847						    const void *private_data,
848						    u8 private_data_len))
849{
850	struct ib_ucm_private_data cmd;
851	struct ib_ucm_context *ctx;
852	const void *private_data = NULL;
853	int result;
854
855	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
856		return -EFAULT;
857
858	result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
859	if (result)
860		return result;
861
862	ctx = ib_ucm_ctx_get(file, cmd.id);
863	if (!IS_ERR(ctx)) {
864		result = func(ctx->cm_id, private_data, cmd.len);
865		ib_ucm_ctx_put(ctx);
866	} else
867		result = PTR_ERR(ctx);
868
869	kfree(private_data);
870	return result;
871}
872
873static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
874			       const char __user *inbuf,
875			       int in_len, int out_len)
876{
877	return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
878}
879
880static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
881				const char __user *inbuf,
882				int in_len, int out_len)
883{
884	return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
885}
886
887static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
888				const char __user *inbuf,
889				int in_len, int out_len)
890{
891	return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
892}
893
894static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
895				const char __user *inbuf, int in_len,
896				int (*func)(struct ib_cm_id *cm_id,
897					    int status,
898					    const void *info,
899					    u8 info_len,
900					    const void *data,
901					    u8 data_len))
902{
903	struct ib_ucm_context *ctx;
904	struct ib_ucm_info cmd;
905	const void *data = NULL;
906	const void *info = NULL;
907	int result;
908
909	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
910		return -EFAULT;
911
912	result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
913	if (result)
914		goto done;
915
916	result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
917	if (result)
918		goto done;
919
920	ctx = ib_ucm_ctx_get(file, cmd.id);
921	if (!IS_ERR(ctx)) {
922		result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
923			      data, cmd.data_len);
924		ib_ucm_ctx_put(ctx);
925	} else
926		result = PTR_ERR(ctx);
927
928done:
929	kfree(data);
930	kfree(info);
931	return result;
932}
933
934static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
935			       const char __user *inbuf,
936			       int in_len, int out_len)
937{
938	return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
939}
940
941static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
942			       const char __user *inbuf,
943			       int in_len, int out_len)
944{
945	return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
946}
947
948static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
949			       const char __user *inbuf,
950			       int in_len, int out_len)
951{
952	struct ib_ucm_context *ctx;
953	struct ib_ucm_mra cmd;
954	const void *data = NULL;
955	int result;
956
957	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
958		return -EFAULT;
959
960	result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
961	if (result)
962		return result;
963
964	ctx = ib_ucm_ctx_get(file, cmd.id);
965	if (!IS_ERR(ctx)) {
966		result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
967		ib_ucm_ctx_put(ctx);
968	} else
969		result = PTR_ERR(ctx);
970
971	kfree(data);
972	return result;
973}
974
975static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
976			       const char __user *inbuf,
977			       int in_len, int out_len)
978{
979	struct ib_ucm_context *ctx;
980	struct ib_sa_path_rec *path = NULL;
981	struct ib_ucm_lap cmd;
982	const void *data = NULL;
983	int result;
984
985	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
986		return -EFAULT;
987
988	result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
989	if (result)
990		goto done;
991
992	result = ib_ucm_path_get(&path, cmd.path);
993	if (result)
994		goto done;
995
996	ctx = ib_ucm_ctx_get(file, cmd.id);
997	if (!IS_ERR(ctx)) {
998		result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
999		ib_ucm_ctx_put(ctx);
1000	} else
1001		result = PTR_ERR(ctx);
1002
1003done:
1004	kfree(data);
1005	kfree(path);
1006	return result;
1007}
1008
1009static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1010				    const char __user *inbuf,
1011				    int in_len, int out_len)
1012{
1013	struct ib_cm_sidr_req_param param;
1014	struct ib_ucm_context *ctx;
1015	struct ib_ucm_sidr_req cmd;
1016	int result;
1017
1018	param.private_data = NULL;
1019	param.path = NULL;
1020
1021	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1022		return -EFAULT;
1023
1024	result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
1025	if (result)
1026		goto done;
1027
1028	result = ib_ucm_path_get(&param.path, cmd.path);
1029	if (result)
1030		goto done;
1031
1032	param.private_data_len = cmd.len;
1033	param.service_id       = cmd.sid;
1034	param.timeout_ms       = cmd.timeout;
1035	param.max_cm_retries   = cmd.max_cm_retries;
1036
1037	ctx = ib_ucm_ctx_get(file, cmd.id);
1038	if (!IS_ERR(ctx)) {
1039		result = ib_send_cm_sidr_req(ctx->cm_id, &param);
1040		ib_ucm_ctx_put(ctx);
1041	} else
1042		result = PTR_ERR(ctx);
1043
1044done:
1045	kfree(param.private_data);
1046	kfree(param.path);
1047	return result;
1048}
1049
1050static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1051				    const char __user *inbuf,
1052				    int in_len, int out_len)
1053{
1054	struct ib_cm_sidr_rep_param param;
1055	struct ib_ucm_sidr_rep cmd;
1056	struct ib_ucm_context *ctx;
1057	int result;
1058
1059	param.info = NULL;
1060
1061	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1062		return -EFAULT;
1063
1064	result = ib_ucm_alloc_data(&param.private_data,
1065				   cmd.data, cmd.data_len);
1066	if (result)
1067		goto done;
1068
1069	result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
1070	if (result)
1071		goto done;
1072
1073	param.qp_num		= cmd.qpn;
1074	param.qkey		= cmd.qkey;
1075	param.status		= cmd.status;
1076	param.info_length	= cmd.info_len;
1077	param.private_data_len	= cmd.data_len;
1078
1079	ctx = ib_ucm_ctx_get(file, cmd.id);
1080	if (!IS_ERR(ctx)) {
1081		result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
1082		ib_ucm_ctx_put(ctx);
1083	} else
1084		result = PTR_ERR(ctx);
1085
1086done:
1087	kfree(param.private_data);
1088	kfree(param.info);
1089	return result;
1090}
1091
1092static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1093				  const char __user *inbuf,
1094				  int in_len, int out_len) = {
1095	[IB_USER_CM_CMD_CREATE_ID]     = ib_ucm_create_id,
1096	[IB_USER_CM_CMD_DESTROY_ID]    = ib_ucm_destroy_id,
1097	[IB_USER_CM_CMD_ATTR_ID]       = ib_ucm_attr_id,
1098	[IB_USER_CM_CMD_LISTEN]        = ib_ucm_listen,
1099	[IB_USER_CM_CMD_NOTIFY]        = ib_ucm_notify,
1100	[IB_USER_CM_CMD_SEND_REQ]      = ib_ucm_send_req,
1101	[IB_USER_CM_CMD_SEND_REP]      = ib_ucm_send_rep,
1102	[IB_USER_CM_CMD_SEND_RTU]      = ib_ucm_send_rtu,
1103	[IB_USER_CM_CMD_SEND_DREQ]     = ib_ucm_send_dreq,
1104	[IB_USER_CM_CMD_SEND_DREP]     = ib_ucm_send_drep,
1105	[IB_USER_CM_CMD_SEND_REJ]      = ib_ucm_send_rej,
1106	[IB_USER_CM_CMD_SEND_MRA]      = ib_ucm_send_mra,
1107	[IB_USER_CM_CMD_SEND_LAP]      = ib_ucm_send_lap,
1108	[IB_USER_CM_CMD_SEND_APR]      = ib_ucm_send_apr,
1109	[IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1110	[IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1111	[IB_USER_CM_CMD_EVENT]	       = ib_ucm_event,
1112	[IB_USER_CM_CMD_INIT_QP_ATTR]  = ib_ucm_init_qp_attr,
1113};
1114
1115static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1116			    size_t len, loff_t *pos)
1117{
1118	struct ib_ucm_file *file = filp->private_data;
1119	struct ib_ucm_cmd_hdr hdr;
1120	ssize_t result;
1121
1122	if (len < sizeof(hdr))
1123		return -EINVAL;
1124
1125	if (copy_from_user(&hdr, buf, sizeof(hdr)))
1126		return -EFAULT;
1127
1128	if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1129		return -EINVAL;
1130
1131	if (hdr.in + sizeof(hdr) > len)
1132		return -EINVAL;
1133
1134	result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1135					hdr.in, hdr.out);
1136	if (!result)
1137		result = len;
1138
1139	return result;
1140}
1141
1142static unsigned int ib_ucm_poll(struct file *filp,
1143				struct poll_table_struct *wait)
1144{
1145	struct ib_ucm_file *file = filp->private_data;
1146	unsigned int mask = 0;
1147
1148	poll_wait(filp, &file->poll_wait, wait);
1149
1150	if (!list_empty(&file->events))
1151		mask = POLLIN | POLLRDNORM;
1152
1153	return mask;
1154}
1155
1156static int ib_ucm_open(struct inode *inode, struct file *filp)
1157{
1158	struct ib_ucm_file *file;
1159
1160	file = kmalloc(sizeof(*file), GFP_KERNEL);
1161	if (!file)
1162		return -ENOMEM;
1163
1164	INIT_LIST_HEAD(&file->events);
1165	INIT_LIST_HEAD(&file->ctxs);
1166	init_waitqueue_head(&file->poll_wait);
1167
1168	mutex_init(&file->file_mutex);
1169
1170	filp->private_data = file;
1171	file->filp = filp;
1172	file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev);
1173
1174	return 0;
1175}
1176
1177static int ib_ucm_close(struct inode *inode, struct file *filp)
1178{
1179	struct ib_ucm_file *file = filp->private_data;
1180	struct ib_ucm_context *ctx;
1181
1182	mutex_lock(&file->file_mutex);
1183	while (!list_empty(&file->ctxs)) {
1184		ctx = list_entry(file->ctxs.next,
1185				 struct ib_ucm_context, file_list);
1186		mutex_unlock(&file->file_mutex);
1187
1188		mutex_lock(&ctx_id_mutex);
1189		idr_remove(&ctx_id_table, ctx->id);
1190		mutex_unlock(&ctx_id_mutex);
1191
1192		ib_destroy_cm_id(ctx->cm_id);
1193		ib_ucm_cleanup_events(ctx);
1194		kfree(ctx);
1195
1196		mutex_lock(&file->file_mutex);
1197	}
1198	mutex_unlock(&file->file_mutex);
1199	kfree(file);
1200	return 0;
1201}
1202
1203static void ib_ucm_release_class_dev(struct class_device *class_dev)
1204{
1205	struct ib_ucm_device *dev;
1206
1207	dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1208	cdev_del(&dev->dev);
1209	clear_bit(dev->devnum, dev_map);
1210	kfree(dev);
1211}
1212
1213static const struct file_operations ucm_fops = {
1214	.owner 	 = THIS_MODULE,
1215	.open 	 = ib_ucm_open,
1216	.release = ib_ucm_close,
1217	.write 	 = ib_ucm_write,
1218	.poll    = ib_ucm_poll,
1219};
1220
1221static struct class ucm_class = {
1222	.name    = "infiniband_cm",
1223	.release = ib_ucm_release_class_dev
1224};
1225
1226static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1227{
1228	struct ib_ucm_device *dev;
1229
1230	dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1231	return sprintf(buf, "%s\n", dev->ib_dev->name);
1232}
1233static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1234
1235static void ib_ucm_add_one(struct ib_device *device)
1236{
1237	struct ib_ucm_device *ucm_dev;
1238
1239	if (!device->alloc_ucontext ||
1240	    rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1241		return;
1242
1243	ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
1244	if (!ucm_dev)
1245		return;
1246
1247	ucm_dev->ib_dev = device;
1248
1249	ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1250	if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
1251		goto err;
1252
1253	set_bit(ucm_dev->devnum, dev_map);
1254
1255	cdev_init(&ucm_dev->dev, &ucm_fops);
1256	ucm_dev->dev.owner = THIS_MODULE;
1257	kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
1258	if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
1259		goto err;
1260
1261	ucm_dev->class_dev.class = &ucm_class;
1262	ucm_dev->class_dev.dev = device->dma_device;
1263	ucm_dev->class_dev.devt = ucm_dev->dev.dev;
1264	snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
1265		 ucm_dev->devnum);
1266	if (class_device_register(&ucm_dev->class_dev))
1267		goto err_cdev;
1268
1269	if (class_device_create_file(&ucm_dev->class_dev,
1270				     &class_device_attr_ibdev))
1271		goto err_class;
1272
1273	ib_set_client_data(device, &ucm_client, ucm_dev);
1274	return;
1275
1276err_class:
1277	class_device_unregister(&ucm_dev->class_dev);
1278err_cdev:
1279	cdev_del(&ucm_dev->dev);
1280	clear_bit(ucm_dev->devnum, dev_map);
1281err:
1282	kfree(ucm_dev);
1283	return;
1284}
1285
1286static void ib_ucm_remove_one(struct ib_device *device)
1287{
1288	struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
1289
1290	if (!ucm_dev)
1291		return;
1292
1293	class_device_unregister(&ucm_dev->class_dev);
1294}
1295
1296static ssize_t show_abi_version(struct class *class, char *buf)
1297{
1298	return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
1299}
1300static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1301
1302static int __init ib_ucm_init(void)
1303{
1304	int ret;
1305
1306	ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1307				     "infiniband_cm");
1308	if (ret) {
1309		printk(KERN_ERR "ucm: couldn't register device number\n");
1310		goto err;
1311	}
1312
1313	ret = class_register(&ucm_class);
1314	if (ret) {
1315		printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
1316		goto err_chrdev;
1317	}
1318
1319	ret = class_create_file(&ucm_class, &class_attr_abi_version);
1320	if (ret) {
1321		printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
1322		goto err_class;
1323	}
1324
1325	ret = ib_register_client(&ucm_client);
1326	if (ret) {
1327		printk(KERN_ERR "ucm: couldn't register client\n");
1328		goto err_class;
1329	}
1330	return 0;
1331
1332err_class:
1333	class_unregister(&ucm_class);
1334err_chrdev:
1335	unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1336err:
1337	return ret;
1338}
1339
1340static void __exit ib_ucm_cleanup(void)
1341{
1342	ib_unregister_client(&ucm_client);
1343	class_unregister(&ucm_class);
1344	unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1345	idr_destroy(&ctx_id_table);
1346}
1347
1348module_init(ib_ucm_init);
1349module_exit(ib_ucm_cleanup);
1350