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