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