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