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