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