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