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