pandora: defconfig: update
[pandora-kernel.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
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 #include <linux/slab.h>
48
49 #include <asm/uaccess.h>
50
51 #include <rdma/ib.h>
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 = memdup_user((void __user *)(unsigned long)src, len);
711         if (IS_ERR(data))
712                 return PTR_ERR(data);
713
714         *dest = data;
715         return 0;
716 }
717
718 static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
719 {
720         struct ib_user_path_rec upath;
721         struct ib_sa_path_rec  *sa_path;
722
723         *path = NULL;
724
725         if (!src)
726                 return 0;
727
728         sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
729         if (!sa_path)
730                 return -ENOMEM;
731
732         if (copy_from_user(&upath, (void __user *)(unsigned long)src,
733                            sizeof(upath))) {
734
735                 kfree(sa_path);
736                 return -EFAULT;
737         }
738
739         ib_copy_path_rec_from_user(sa_path, &upath);
740         *path = sa_path;
741         return 0;
742 }
743
744 static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
745                                const char __user *inbuf,
746                                int in_len, int out_len)
747 {
748         struct ib_cm_req_param param;
749         struct ib_ucm_context *ctx;
750         struct ib_ucm_req cmd;
751         int result;
752
753         param.private_data   = NULL;
754         param.primary_path   = NULL;
755         param.alternate_path = NULL;
756
757         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
758                 return -EFAULT;
759
760         result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
761         if (result)
762                 goto done;
763
764         result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
765         if (result)
766                 goto done;
767
768         result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
769         if (result)
770                 goto done;
771
772         param.private_data_len           = cmd.len;
773         param.service_id                 = cmd.sid;
774         param.qp_num                     = cmd.qpn;
775         param.qp_type                    = cmd.qp_type;
776         param.starting_psn               = cmd.psn;
777         param.peer_to_peer               = cmd.peer_to_peer;
778         param.responder_resources        = cmd.responder_resources;
779         param.initiator_depth            = cmd.initiator_depth;
780         param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
781         param.flow_control               = cmd.flow_control;
782         param.local_cm_response_timeout  = cmd.local_cm_response_timeout;
783         param.retry_count                = cmd.retry_count;
784         param.rnr_retry_count            = cmd.rnr_retry_count;
785         param.max_cm_retries             = cmd.max_cm_retries;
786         param.srq                        = cmd.srq;
787
788         ctx = ib_ucm_ctx_get(file, cmd.id);
789         if (!IS_ERR(ctx)) {
790                 result = ib_send_cm_req(ctx->cm_id, &param);
791                 ib_ucm_ctx_put(ctx);
792         } else
793                 result = PTR_ERR(ctx);
794
795 done:
796         kfree(param.private_data);
797         kfree(param.primary_path);
798         kfree(param.alternate_path);
799         return result;
800 }
801
802 static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
803                                const char __user *inbuf,
804                                int in_len, int out_len)
805 {
806         struct ib_cm_rep_param param;
807         struct ib_ucm_context *ctx;
808         struct ib_ucm_rep cmd;
809         int result;
810
811         param.private_data = NULL;
812
813         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
814                 return -EFAULT;
815
816         result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
817         if (result)
818                 return result;
819
820         param.qp_num              = cmd.qpn;
821         param.starting_psn        = cmd.psn;
822         param.private_data_len    = cmd.len;
823         param.responder_resources = cmd.responder_resources;
824         param.initiator_depth     = cmd.initiator_depth;
825         param.failover_accepted   = cmd.failover_accepted;
826         param.flow_control        = cmd.flow_control;
827         param.rnr_retry_count     = cmd.rnr_retry_count;
828         param.srq                 = cmd.srq;
829
830         ctx = ib_ucm_ctx_get(file, cmd.id);
831         if (!IS_ERR(ctx)) {
832                 ctx->uid = cmd.uid;
833                 result = ib_send_cm_rep(ctx->cm_id, &param);
834                 ib_ucm_ctx_put(ctx);
835         } else
836                 result = PTR_ERR(ctx);
837
838         kfree(param.private_data);
839         return result;
840 }
841
842 static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
843                                         const char __user *inbuf, int in_len,
844                                         int (*func)(struct ib_cm_id *cm_id,
845                                                     const void *private_data,
846                                                     u8 private_data_len))
847 {
848         struct ib_ucm_private_data cmd;
849         struct ib_ucm_context *ctx;
850         const void *private_data = NULL;
851         int result;
852
853         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
854                 return -EFAULT;
855
856         result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
857         if (result)
858                 return result;
859
860         ctx = ib_ucm_ctx_get(file, cmd.id);
861         if (!IS_ERR(ctx)) {
862                 result = func(ctx->cm_id, private_data, cmd.len);
863                 ib_ucm_ctx_put(ctx);
864         } else
865                 result = PTR_ERR(ctx);
866
867         kfree(private_data);
868         return result;
869 }
870
871 static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
872                                const char __user *inbuf,
873                                int in_len, int out_len)
874 {
875         return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
876 }
877
878 static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
879                                 const char __user *inbuf,
880                                 int in_len, int out_len)
881 {
882         return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
883 }
884
885 static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
886                                 const char __user *inbuf,
887                                 int in_len, int out_len)
888 {
889         return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
890 }
891
892 static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
893                                 const char __user *inbuf, int in_len,
894                                 int (*func)(struct ib_cm_id *cm_id,
895                                             int status,
896                                             const void *info,
897                                             u8 info_len,
898                                             const void *data,
899                                             u8 data_len))
900 {
901         struct ib_ucm_context *ctx;
902         struct ib_ucm_info cmd;
903         const void *data = NULL;
904         const void *info = NULL;
905         int result;
906
907         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
908                 return -EFAULT;
909
910         result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
911         if (result)
912                 goto done;
913
914         result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
915         if (result)
916                 goto done;
917
918         ctx = ib_ucm_ctx_get(file, cmd.id);
919         if (!IS_ERR(ctx)) {
920                 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
921                               data, cmd.data_len);
922                 ib_ucm_ctx_put(ctx);
923         } else
924                 result = PTR_ERR(ctx);
925
926 done:
927         kfree(data);
928         kfree(info);
929         return result;
930 }
931
932 static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
933                                const char __user *inbuf,
934                                int in_len, int out_len)
935 {
936         return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
937 }
938
939 static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
940                                const char __user *inbuf,
941                                int in_len, int out_len)
942 {
943         return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
944 }
945
946 static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
947                                const char __user *inbuf,
948                                int in_len, int out_len)
949 {
950         struct ib_ucm_context *ctx;
951         struct ib_ucm_mra cmd;
952         const void *data = NULL;
953         int result;
954
955         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
956                 return -EFAULT;
957
958         result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
959         if (result)
960                 return result;
961
962         ctx = ib_ucm_ctx_get(file, cmd.id);
963         if (!IS_ERR(ctx)) {
964                 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
965                 ib_ucm_ctx_put(ctx);
966         } else
967                 result = PTR_ERR(ctx);
968
969         kfree(data);
970         return result;
971 }
972
973 static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
974                                const char __user *inbuf,
975                                int in_len, int out_len)
976 {
977         struct ib_ucm_context *ctx;
978         struct ib_sa_path_rec *path = NULL;
979         struct ib_ucm_lap cmd;
980         const void *data = NULL;
981         int result;
982
983         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
984                 return -EFAULT;
985
986         result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
987         if (result)
988                 goto done;
989
990         result = ib_ucm_path_get(&path, cmd.path);
991         if (result)
992                 goto done;
993
994         ctx = ib_ucm_ctx_get(file, cmd.id);
995         if (!IS_ERR(ctx)) {
996                 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
997                 ib_ucm_ctx_put(ctx);
998         } else
999                 result = PTR_ERR(ctx);
1000
1001 done:
1002         kfree(data);
1003         kfree(path);
1004         return result;
1005 }
1006
1007 static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1008                                     const char __user *inbuf,
1009                                     int in_len, int out_len)
1010 {
1011         struct ib_cm_sidr_req_param param;
1012         struct ib_ucm_context *ctx;
1013         struct ib_ucm_sidr_req cmd;
1014         int result;
1015
1016         param.private_data = NULL;
1017         param.path = NULL;
1018
1019         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1020                 return -EFAULT;
1021
1022         result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
1023         if (result)
1024                 goto done;
1025
1026         result = ib_ucm_path_get(&param.path, cmd.path);
1027         if (result)
1028                 goto done;
1029
1030         param.private_data_len = cmd.len;
1031         param.service_id       = cmd.sid;
1032         param.timeout_ms       = cmd.timeout;
1033         param.max_cm_retries   = cmd.max_cm_retries;
1034
1035         ctx = ib_ucm_ctx_get(file, cmd.id);
1036         if (!IS_ERR(ctx)) {
1037                 result = ib_send_cm_sidr_req(ctx->cm_id, &param);
1038                 ib_ucm_ctx_put(ctx);
1039         } else
1040                 result = PTR_ERR(ctx);
1041
1042 done:
1043         kfree(param.private_data);
1044         kfree(param.path);
1045         return result;
1046 }
1047
1048 static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1049                                     const char __user *inbuf,
1050                                     int in_len, int out_len)
1051 {
1052         struct ib_cm_sidr_rep_param param;
1053         struct ib_ucm_sidr_rep cmd;
1054         struct ib_ucm_context *ctx;
1055         int result;
1056
1057         param.info = NULL;
1058
1059         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1060                 return -EFAULT;
1061
1062         result = ib_ucm_alloc_data(&param.private_data,
1063                                    cmd.data, cmd.data_len);
1064         if (result)
1065                 goto done;
1066
1067         result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
1068         if (result)
1069                 goto done;
1070
1071         param.qp_num            = cmd.qpn;
1072         param.qkey              = cmd.qkey;
1073         param.status            = cmd.status;
1074         param.info_length       = cmd.info_len;
1075         param.private_data_len  = cmd.data_len;
1076
1077         ctx = ib_ucm_ctx_get(file, cmd.id);
1078         if (!IS_ERR(ctx)) {
1079                 result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
1080                 ib_ucm_ctx_put(ctx);
1081         } else
1082                 result = PTR_ERR(ctx);
1083
1084 done:
1085         kfree(param.private_data);
1086         kfree(param.info);
1087         return result;
1088 }
1089
1090 static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1091                                   const char __user *inbuf,
1092                                   int in_len, int out_len) = {
1093         [IB_USER_CM_CMD_CREATE_ID]     = ib_ucm_create_id,
1094         [IB_USER_CM_CMD_DESTROY_ID]    = ib_ucm_destroy_id,
1095         [IB_USER_CM_CMD_ATTR_ID]       = ib_ucm_attr_id,
1096         [IB_USER_CM_CMD_LISTEN]        = ib_ucm_listen,
1097         [IB_USER_CM_CMD_NOTIFY]        = ib_ucm_notify,
1098         [IB_USER_CM_CMD_SEND_REQ]      = ib_ucm_send_req,
1099         [IB_USER_CM_CMD_SEND_REP]      = ib_ucm_send_rep,
1100         [IB_USER_CM_CMD_SEND_RTU]      = ib_ucm_send_rtu,
1101         [IB_USER_CM_CMD_SEND_DREQ]     = ib_ucm_send_dreq,
1102         [IB_USER_CM_CMD_SEND_DREP]     = ib_ucm_send_drep,
1103         [IB_USER_CM_CMD_SEND_REJ]      = ib_ucm_send_rej,
1104         [IB_USER_CM_CMD_SEND_MRA]      = ib_ucm_send_mra,
1105         [IB_USER_CM_CMD_SEND_LAP]      = ib_ucm_send_lap,
1106         [IB_USER_CM_CMD_SEND_APR]      = ib_ucm_send_apr,
1107         [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1108         [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1109         [IB_USER_CM_CMD_EVENT]         = ib_ucm_event,
1110         [IB_USER_CM_CMD_INIT_QP_ATTR]  = ib_ucm_init_qp_attr,
1111 };
1112
1113 static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1114                             size_t len, loff_t *pos)
1115 {
1116         struct ib_ucm_file *file = filp->private_data;
1117         struct ib_ucm_cmd_hdr hdr;
1118         ssize_t result;
1119
1120         if (WARN_ON_ONCE(!ib_safe_file_access(filp)))
1121                 return -EACCES;
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 >= 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 nonseekable_open(inode, filp);
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         if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
1219                 clear_bit(ucm_dev->devnum, dev_map);
1220         else
1221                 clear_bit(ucm_dev->devnum - IB_UCM_MAX_DEVICES, dev_map);
1222         kfree(ucm_dev);
1223 }
1224
1225 static const struct file_operations ucm_fops = {
1226         .owner   = THIS_MODULE,
1227         .open    = ib_ucm_open,
1228         .release = ib_ucm_close,
1229         .write   = ib_ucm_write,
1230         .poll    = ib_ucm_poll,
1231         .llseek  = no_llseek,
1232 };
1233
1234 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1235                           char *buf)
1236 {
1237         struct ib_ucm_device *ucm_dev;
1238
1239         ucm_dev = container_of(dev, struct ib_ucm_device, dev);
1240         return sprintf(buf, "%s\n", ucm_dev->ib_dev->name);
1241 }
1242 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1243
1244 static dev_t overflow_maj;
1245 static DECLARE_BITMAP(overflow_map, IB_UCM_MAX_DEVICES);
1246 static int find_overflow_devnum(void)
1247 {
1248         int ret;
1249
1250         if (!overflow_maj) {
1251                 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UCM_MAX_DEVICES,
1252                                           "infiniband_cm");
1253                 if (ret) {
1254                         printk(KERN_ERR "ucm: couldn't register dynamic device number\n");
1255                         return ret;
1256                 }
1257         }
1258
1259         ret = find_first_zero_bit(overflow_map, IB_UCM_MAX_DEVICES);
1260         if (ret >= IB_UCM_MAX_DEVICES)
1261                 return -1;
1262
1263         return ret;
1264 }
1265
1266 static void ib_ucm_add_one(struct ib_device *device)
1267 {
1268         int devnum;
1269         dev_t base;
1270         struct ib_ucm_device *ucm_dev;
1271
1272         if (!device->alloc_ucontext ||
1273             rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1274                 return;
1275
1276         ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
1277         if (!ucm_dev)
1278                 return;
1279
1280         ucm_dev->ib_dev = device;
1281
1282         devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1283         if (devnum >= IB_UCM_MAX_DEVICES) {
1284                 devnum = find_overflow_devnum();
1285                 if (devnum < 0)
1286                         goto err;
1287
1288                 ucm_dev->devnum = devnum + IB_UCM_MAX_DEVICES;
1289                 base = devnum + overflow_maj;
1290                 set_bit(devnum, overflow_map);
1291         } else {
1292                 ucm_dev->devnum = devnum;
1293                 base = devnum + IB_UCM_BASE_DEV;
1294                 set_bit(devnum, dev_map);
1295         }
1296
1297         cdev_init(&ucm_dev->cdev, &ucm_fops);
1298         ucm_dev->cdev.owner = THIS_MODULE;
1299         kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum);
1300         if (cdev_add(&ucm_dev->cdev, base, 1))
1301                 goto err;
1302
1303         ucm_dev->dev.class = &cm_class;
1304         ucm_dev->dev.parent = device->dma_device;
1305         ucm_dev->dev.devt = ucm_dev->cdev.dev;
1306         ucm_dev->dev.release = ib_ucm_release_dev;
1307         dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum);
1308         if (device_register(&ucm_dev->dev))
1309                 goto err_cdev;
1310
1311         if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev))
1312                 goto err_dev;
1313
1314         ib_set_client_data(device, &ucm_client, ucm_dev);
1315         return;
1316
1317 err_dev:
1318         device_unregister(&ucm_dev->dev);
1319 err_cdev:
1320         cdev_del(&ucm_dev->cdev);
1321         if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
1322                 clear_bit(devnum, dev_map);
1323         else
1324                 clear_bit(devnum, overflow_map);
1325 err:
1326         kfree(ucm_dev);
1327         return;
1328 }
1329
1330 static void ib_ucm_remove_one(struct ib_device *device)
1331 {
1332         struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
1333
1334         if (!ucm_dev)
1335                 return;
1336
1337         device_unregister(&ucm_dev->dev);
1338 }
1339
1340 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1341                          __stringify(IB_USER_CM_ABI_VERSION));
1342
1343 static int __init ib_ucm_init(void)
1344 {
1345         int ret;
1346
1347         ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1348                                      "infiniband_cm");
1349         if (ret) {
1350                 printk(KERN_ERR "ucm: couldn't register device number\n");
1351                 goto error1;
1352         }
1353
1354         ret = class_create_file(&cm_class, &class_attr_abi_version.attr);
1355         if (ret) {
1356                 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
1357                 goto error2;
1358         }
1359
1360         ret = ib_register_client(&ucm_client);
1361         if (ret) {
1362                 printk(KERN_ERR "ucm: couldn't register client\n");
1363                 goto error3;
1364         }
1365         return 0;
1366
1367 error3:
1368         class_remove_file(&cm_class, &class_attr_abi_version.attr);
1369 error2:
1370         unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1371 error1:
1372         return ret;
1373 }
1374
1375 static void __exit ib_ucm_cleanup(void)
1376 {
1377         ib_unregister_client(&ucm_client);
1378         class_remove_file(&cm_class, &class_attr_abi_version.attr);
1379         unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1380         if (overflow_maj)
1381                 unregister_chrdev_region(overflow_maj, IB_UCM_MAX_DEVICES);
1382         idr_destroy(&ctx_id_table);
1383 }
1384
1385 module_init(ib_ucm_init);
1386 module_exit(ib_ucm_cleanup);