2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
33 * $Id: ucm.c 4311 2005-12-05 18:42:01Z sean.hefty $
36 #include <linux/completion.h>
37 #include <linux/init.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 #include <linux/smp_lock.h>
50 #include <asm/uaccess.h>
52 #include <rdma/ib_cm.h>
53 #include <rdma/ib_user_cm.h>
54 #include <rdma/ib_marshall.h>
56 MODULE_AUTHOR("Libor Michalek");
57 MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
58 MODULE_LICENSE("Dual BSD/GPL");
60 struct ib_ucm_device {
64 struct ib_device *ib_dev;
68 struct mutex file_mutex;
70 struct ib_ucm_device *device;
72 struct list_head ctxs;
73 struct list_head events;
74 wait_queue_head_t poll_wait;
77 struct ib_ucm_context {
79 struct completion comp;
83 struct ib_ucm_file *file;
84 struct ib_cm_id *cm_id;
87 struct list_head events; /* list of pending events. */
88 struct list_head file_list; /* member in file ctx list */
92 struct ib_ucm_context *ctx;
93 struct list_head file_list; /* member in file event list */
94 struct list_head ctx_list; /* member in ctx event list */
96 struct ib_cm_id *cm_id;
97 struct ib_ucm_event_resp resp;
106 IB_UCM_BASE_MINOR = 224,
107 IB_UCM_MAX_DEVICES = 32
110 /* ib_cm and ib_user_cm modules share /sys/class/infiniband_cm */
111 extern struct class cm_class;
113 #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
115 static void ib_ucm_add_one(struct ib_device *device);
116 static void ib_ucm_remove_one(struct ib_device *device);
118 static struct ib_client ucm_client = {
120 .add = ib_ucm_add_one,
121 .remove = ib_ucm_remove_one
124 static DEFINE_MUTEX(ctx_id_mutex);
125 static DEFINE_IDR(ctx_id_table);
126 static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
128 static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
130 struct ib_ucm_context *ctx;
132 mutex_lock(&ctx_id_mutex);
133 ctx = idr_find(&ctx_id_table, id);
135 ctx = ERR_PTR(-ENOENT);
136 else if (ctx->file != file)
137 ctx = ERR_PTR(-EINVAL);
139 atomic_inc(&ctx->ref);
140 mutex_unlock(&ctx_id_mutex);
145 static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
147 if (atomic_dec_and_test(&ctx->ref))
148 complete(&ctx->comp);
151 static inline int ib_ucm_new_cm_id(int event)
153 return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
156 static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
158 struct ib_ucm_event *uevent;
160 mutex_lock(&ctx->file->file_mutex);
161 list_del(&ctx->file_list);
162 while (!list_empty(&ctx->events)) {
164 uevent = list_entry(ctx->events.next,
165 struct ib_ucm_event, ctx_list);
166 list_del(&uevent->file_list);
167 list_del(&uevent->ctx_list);
168 mutex_unlock(&ctx->file->file_mutex);
170 /* clear incoming connections. */
171 if (ib_ucm_new_cm_id(uevent->resp.event))
172 ib_destroy_cm_id(uevent->cm_id);
175 mutex_lock(&ctx->file->file_mutex);
177 mutex_unlock(&ctx->file->file_mutex);
180 static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
182 struct ib_ucm_context *ctx;
185 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
189 atomic_set(&ctx->ref, 1);
190 init_completion(&ctx->comp);
192 INIT_LIST_HEAD(&ctx->events);
195 result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
199 mutex_lock(&ctx_id_mutex);
200 result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
201 mutex_unlock(&ctx_id_mutex);
202 } while (result == -EAGAIN);
207 list_add_tail(&ctx->file_list, &file->ctxs);
215 static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
216 struct ib_cm_req_event_param *kreq)
218 ureq->remote_ca_guid = kreq->remote_ca_guid;
219 ureq->remote_qkey = kreq->remote_qkey;
220 ureq->remote_qpn = kreq->remote_qpn;
221 ureq->qp_type = kreq->qp_type;
222 ureq->starting_psn = kreq->starting_psn;
223 ureq->responder_resources = kreq->responder_resources;
224 ureq->initiator_depth = kreq->initiator_depth;
225 ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
226 ureq->flow_control = kreq->flow_control;
227 ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
228 ureq->retry_count = kreq->retry_count;
229 ureq->rnr_retry_count = kreq->rnr_retry_count;
230 ureq->srq = kreq->srq;
231 ureq->port = kreq->port;
233 ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
234 if (kreq->alternate_path)
235 ib_copy_path_rec_to_user(&ureq->alternate_path,
236 kreq->alternate_path);
239 static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
240 struct ib_cm_rep_event_param *krep)
242 urep->remote_ca_guid = krep->remote_ca_guid;
243 urep->remote_qkey = krep->remote_qkey;
244 urep->remote_qpn = krep->remote_qpn;
245 urep->starting_psn = krep->starting_psn;
246 urep->responder_resources = krep->responder_resources;
247 urep->initiator_depth = krep->initiator_depth;
248 urep->target_ack_delay = krep->target_ack_delay;
249 urep->failover_accepted = krep->failover_accepted;
250 urep->flow_control = krep->flow_control;
251 urep->rnr_retry_count = krep->rnr_retry_count;
252 urep->srq = krep->srq;
255 static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
256 struct ib_cm_sidr_rep_event_param *krep)
258 urep->status = krep->status;
259 urep->qkey = krep->qkey;
260 urep->qpn = krep->qpn;
263 static int ib_ucm_event_process(struct ib_cm_event *evt,
264 struct ib_ucm_event *uvt)
268 switch (evt->event) {
269 case IB_CM_REQ_RECEIVED:
270 ib_ucm_event_req_get(&uvt->resp.u.req_resp,
271 &evt->param.req_rcvd);
272 uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
273 uvt->resp.present = IB_UCM_PRES_PRIMARY;
274 uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
275 IB_UCM_PRES_ALTERNATE : 0);
277 case IB_CM_REP_RECEIVED:
278 ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
279 &evt->param.rep_rcvd);
280 uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
282 case IB_CM_RTU_RECEIVED:
283 uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
284 uvt->resp.u.send_status = evt->param.send_status;
286 case IB_CM_DREQ_RECEIVED:
287 uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
288 uvt->resp.u.send_status = evt->param.send_status;
290 case IB_CM_DREP_RECEIVED:
291 uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
292 uvt->resp.u.send_status = evt->param.send_status;
294 case IB_CM_MRA_RECEIVED:
295 uvt->resp.u.mra_resp.timeout =
296 evt->param.mra_rcvd.service_timeout;
297 uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
299 case IB_CM_REJ_RECEIVED:
300 uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
301 uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
302 uvt->info_len = evt->param.rej_rcvd.ari_length;
303 info = evt->param.rej_rcvd.ari;
305 case IB_CM_LAP_RECEIVED:
306 ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
307 evt->param.lap_rcvd.alternate_path);
308 uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
309 uvt->resp.present = IB_UCM_PRES_ALTERNATE;
311 case IB_CM_APR_RECEIVED:
312 uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
313 uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
314 uvt->info_len = evt->param.apr_rcvd.info_len;
315 info = evt->param.apr_rcvd.apr_info;
317 case IB_CM_SIDR_REQ_RECEIVED:
318 uvt->resp.u.sidr_req_resp.pkey =
319 evt->param.sidr_req_rcvd.pkey;
320 uvt->resp.u.sidr_req_resp.port =
321 evt->param.sidr_req_rcvd.port;
322 uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
324 case IB_CM_SIDR_REP_RECEIVED:
325 ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
326 &evt->param.sidr_rep_rcvd);
327 uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
328 uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
329 info = evt->param.sidr_rep_rcvd.info;
332 uvt->resp.u.send_status = evt->param.send_status;
337 uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL);
341 uvt->resp.present |= IB_UCM_PRES_DATA;
345 uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL);
349 uvt->resp.present |= IB_UCM_PRES_INFO;
359 static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
360 struct ib_cm_event *event)
362 struct ib_ucm_event *uevent;
363 struct ib_ucm_context *ctx;
366 ctx = cm_id->context;
368 uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
373 uevent->cm_id = cm_id;
374 uevent->resp.uid = ctx->uid;
375 uevent->resp.id = ctx->id;
376 uevent->resp.event = event->event;
378 result = ib_ucm_event_process(event, uevent);
382 mutex_lock(&ctx->file->file_mutex);
383 list_add_tail(&uevent->file_list, &ctx->file->events);
384 list_add_tail(&uevent->ctx_list, &ctx->events);
385 wake_up_interruptible(&ctx->file->poll_wait);
386 mutex_unlock(&ctx->file->file_mutex);
392 /* Destroy new cm_id's */
393 return ib_ucm_new_cm_id(event->event);
396 static ssize_t ib_ucm_event(struct ib_ucm_file *file,
397 const char __user *inbuf,
398 int in_len, int out_len)
400 struct ib_ucm_context *ctx;
401 struct ib_ucm_event_get cmd;
402 struct ib_ucm_event *uevent;
406 if (out_len < sizeof(struct ib_ucm_event_resp))
409 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
412 mutex_lock(&file->file_mutex);
413 while (list_empty(&file->events)) {
414 mutex_unlock(&file->file_mutex);
416 if (file->filp->f_flags & O_NONBLOCK)
419 if (wait_event_interruptible(file->poll_wait,
420 !list_empty(&file->events)))
423 mutex_lock(&file->file_mutex);
426 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
428 if (ib_ucm_new_cm_id(uevent->resp.event)) {
429 ctx = ib_ucm_ctx_alloc(file);
435 ctx->cm_id = uevent->cm_id;
436 ctx->cm_id->context = ctx;
437 uevent->resp.id = ctx->id;
440 if (copy_to_user((void __user *)(unsigned long)cmd.response,
441 &uevent->resp, sizeof(uevent->resp))) {
447 if (cmd.data_len < uevent->data_len) {
451 if (copy_to_user((void __user *)(unsigned long)cmd.data,
452 uevent->data, uevent->data_len)) {
459 if (cmd.info_len < uevent->info_len) {
463 if (copy_to_user((void __user *)(unsigned long)cmd.info,
464 uevent->info, uevent->info_len)) {
470 list_del(&uevent->file_list);
471 list_del(&uevent->ctx_list);
472 uevent->ctx->events_reported++;
478 mutex_unlock(&file->file_mutex);
482 static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
483 const char __user *inbuf,
484 int in_len, int out_len)
486 struct ib_ucm_create_id cmd;
487 struct ib_ucm_create_id_resp resp;
488 struct ib_ucm_context *ctx;
491 if (out_len < sizeof(resp))
494 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
497 mutex_lock(&file->file_mutex);
498 ctx = ib_ucm_ctx_alloc(file);
499 mutex_unlock(&file->file_mutex);
504 ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
505 ib_ucm_event_handler, ctx);
506 if (IS_ERR(ctx->cm_id)) {
507 result = PTR_ERR(ctx->cm_id);
512 if (copy_to_user((void __user *)(unsigned long)cmd.response,
513 &resp, sizeof(resp))) {
520 ib_destroy_cm_id(ctx->cm_id);
522 mutex_lock(&ctx_id_mutex);
523 idr_remove(&ctx_id_table, ctx->id);
524 mutex_unlock(&ctx_id_mutex);
529 static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
530 const char __user *inbuf,
531 int in_len, int out_len)
533 struct ib_ucm_destroy_id cmd;
534 struct ib_ucm_destroy_id_resp resp;
535 struct ib_ucm_context *ctx;
538 if (out_len < sizeof(resp))
541 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
544 mutex_lock(&ctx_id_mutex);
545 ctx = idr_find(&ctx_id_table, cmd.id);
547 ctx = ERR_PTR(-ENOENT);
548 else if (ctx->file != file)
549 ctx = ERR_PTR(-EINVAL);
551 idr_remove(&ctx_id_table, ctx->id);
552 mutex_unlock(&ctx_id_mutex);
558 wait_for_completion(&ctx->comp);
560 /* No new events will be generated after destroying the cm_id. */
561 ib_destroy_cm_id(ctx->cm_id);
562 /* Cleanup events not yet reported to the user. */
563 ib_ucm_cleanup_events(ctx);
565 resp.events_reported = ctx->events_reported;
566 if (copy_to_user((void __user *)(unsigned long)cmd.response,
567 &resp, sizeof(resp)))
574 static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
575 const char __user *inbuf,
576 int in_len, int out_len)
578 struct ib_ucm_attr_id_resp resp;
579 struct ib_ucm_attr_id cmd;
580 struct ib_ucm_context *ctx;
583 if (out_len < sizeof(resp))
586 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
589 ctx = ib_ucm_ctx_get(file, cmd.id);
593 resp.service_id = ctx->cm_id->service_id;
594 resp.service_mask = ctx->cm_id->service_mask;
595 resp.local_id = ctx->cm_id->local_id;
596 resp.remote_id = ctx->cm_id->remote_id;
598 if (copy_to_user((void __user *)(unsigned long)cmd.response,
599 &resp, sizeof(resp)))
606 static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
607 const char __user *inbuf,
608 int in_len, int out_len)
610 struct ib_uverbs_qp_attr resp;
611 struct ib_ucm_init_qp_attr cmd;
612 struct ib_ucm_context *ctx;
613 struct ib_qp_attr qp_attr;
616 if (out_len < sizeof(resp))
619 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
622 ctx = ib_ucm_ctx_get(file, cmd.id);
626 resp.qp_attr_mask = 0;
627 memset(&qp_attr, 0, sizeof qp_attr);
628 qp_attr.qp_state = cmd.qp_state;
629 result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
633 ib_copy_qp_attr_to_user(&resp, &qp_attr);
635 if (copy_to_user((void __user *)(unsigned long)cmd.response,
636 &resp, sizeof(resp)))
644 static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
646 service_id &= service_mask;
648 if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
649 ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
655 static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
656 const char __user *inbuf,
657 int in_len, int out_len)
659 struct ib_ucm_listen cmd;
660 struct ib_ucm_context *ctx;
663 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
666 ctx = ib_ucm_ctx_get(file, cmd.id);
670 result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
674 result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask,
681 static ssize_t ib_ucm_notify(struct ib_ucm_file *file,
682 const char __user *inbuf,
683 int in_len, int out_len)
685 struct ib_ucm_notify cmd;
686 struct ib_ucm_context *ctx;
689 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
692 ctx = ib_ucm_ctx_get(file, cmd.id);
696 result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
701 static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
710 data = kmalloc(len, GFP_KERNEL);
714 if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
723 static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
725 struct ib_user_path_rec upath;
726 struct ib_sa_path_rec *sa_path;
733 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
737 if (copy_from_user(&upath, (void __user *)(unsigned long)src,
744 ib_copy_path_rec_from_user(sa_path, &upath);
749 static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
750 const char __user *inbuf,
751 int in_len, int out_len)
753 struct ib_cm_req_param param;
754 struct ib_ucm_context *ctx;
755 struct ib_ucm_req cmd;
758 param.private_data = NULL;
759 param.primary_path = NULL;
760 param.alternate_path = NULL;
762 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
765 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
769 result = ib_ucm_path_get(¶m.primary_path, cmd.primary_path);
773 result = ib_ucm_path_get(¶m.alternate_path, cmd.alternate_path);
777 param.private_data_len = cmd.len;
778 param.service_id = cmd.sid;
779 param.qp_num = cmd.qpn;
780 param.qp_type = cmd.qp_type;
781 param.starting_psn = cmd.psn;
782 param.peer_to_peer = cmd.peer_to_peer;
783 param.responder_resources = cmd.responder_resources;
784 param.initiator_depth = cmd.initiator_depth;
785 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
786 param.flow_control = cmd.flow_control;
787 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
788 param.retry_count = cmd.retry_count;
789 param.rnr_retry_count = cmd.rnr_retry_count;
790 param.max_cm_retries = cmd.max_cm_retries;
793 ctx = ib_ucm_ctx_get(file, cmd.id);
795 result = ib_send_cm_req(ctx->cm_id, ¶m);
798 result = PTR_ERR(ctx);
801 kfree(param.private_data);
802 kfree(param.primary_path);
803 kfree(param.alternate_path);
807 static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
808 const char __user *inbuf,
809 int in_len, int out_len)
811 struct ib_cm_rep_param param;
812 struct ib_ucm_context *ctx;
813 struct ib_ucm_rep cmd;
816 param.private_data = NULL;
818 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
821 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
825 param.qp_num = cmd.qpn;
826 param.starting_psn = cmd.psn;
827 param.private_data_len = cmd.len;
828 param.responder_resources = cmd.responder_resources;
829 param.initiator_depth = cmd.initiator_depth;
830 param.failover_accepted = cmd.failover_accepted;
831 param.flow_control = cmd.flow_control;
832 param.rnr_retry_count = cmd.rnr_retry_count;
835 ctx = ib_ucm_ctx_get(file, cmd.id);
838 result = ib_send_cm_rep(ctx->cm_id, ¶m);
841 result = PTR_ERR(ctx);
843 kfree(param.private_data);
847 static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
848 const char __user *inbuf, int in_len,
849 int (*func)(struct ib_cm_id *cm_id,
850 const void *private_data,
851 u8 private_data_len))
853 struct ib_ucm_private_data cmd;
854 struct ib_ucm_context *ctx;
855 const void *private_data = NULL;
858 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
861 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
865 ctx = ib_ucm_ctx_get(file, cmd.id);
867 result = func(ctx->cm_id, private_data, cmd.len);
870 result = PTR_ERR(ctx);
876 static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
877 const char __user *inbuf,
878 int in_len, int out_len)
880 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
883 static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
884 const char __user *inbuf,
885 int in_len, int out_len)
887 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
890 static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
891 const char __user *inbuf,
892 int in_len, int out_len)
894 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
897 static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
898 const char __user *inbuf, int in_len,
899 int (*func)(struct ib_cm_id *cm_id,
906 struct ib_ucm_context *ctx;
907 struct ib_ucm_info cmd;
908 const void *data = NULL;
909 const void *info = NULL;
912 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
915 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
919 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
923 ctx = ib_ucm_ctx_get(file, cmd.id);
925 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
929 result = PTR_ERR(ctx);
937 static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
938 const char __user *inbuf,
939 int in_len, int out_len)
941 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
944 static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
945 const char __user *inbuf,
946 int in_len, int out_len)
948 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
951 static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
952 const char __user *inbuf,
953 int in_len, int out_len)
955 struct ib_ucm_context *ctx;
956 struct ib_ucm_mra cmd;
957 const void *data = NULL;
960 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
963 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
967 ctx = ib_ucm_ctx_get(file, cmd.id);
969 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
972 result = PTR_ERR(ctx);
978 static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
979 const char __user *inbuf,
980 int in_len, int out_len)
982 struct ib_ucm_context *ctx;
983 struct ib_sa_path_rec *path = NULL;
984 struct ib_ucm_lap cmd;
985 const void *data = NULL;
988 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
991 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
995 result = ib_ucm_path_get(&path, cmd.path);
999 ctx = ib_ucm_ctx_get(file, cmd.id);
1001 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
1002 ib_ucm_ctx_put(ctx);
1004 result = PTR_ERR(ctx);
1012 static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1013 const char __user *inbuf,
1014 int in_len, int out_len)
1016 struct ib_cm_sidr_req_param param;
1017 struct ib_ucm_context *ctx;
1018 struct ib_ucm_sidr_req cmd;
1021 param.private_data = NULL;
1024 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1027 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
1031 result = ib_ucm_path_get(¶m.path, cmd.path);
1035 param.private_data_len = cmd.len;
1036 param.service_id = cmd.sid;
1037 param.timeout_ms = cmd.timeout;
1038 param.max_cm_retries = cmd.max_cm_retries;
1040 ctx = ib_ucm_ctx_get(file, cmd.id);
1042 result = ib_send_cm_sidr_req(ctx->cm_id, ¶m);
1043 ib_ucm_ctx_put(ctx);
1045 result = PTR_ERR(ctx);
1048 kfree(param.private_data);
1053 static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1054 const char __user *inbuf,
1055 int in_len, int out_len)
1057 struct ib_cm_sidr_rep_param param;
1058 struct ib_ucm_sidr_rep cmd;
1059 struct ib_ucm_context *ctx;
1064 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1067 result = ib_ucm_alloc_data(¶m.private_data,
1068 cmd.data, cmd.data_len);
1072 result = ib_ucm_alloc_data(¶m.info, cmd.info, cmd.info_len);
1076 param.qp_num = cmd.qpn;
1077 param.qkey = cmd.qkey;
1078 param.status = cmd.status;
1079 param.info_length = cmd.info_len;
1080 param.private_data_len = cmd.data_len;
1082 ctx = ib_ucm_ctx_get(file, cmd.id);
1084 result = ib_send_cm_sidr_rep(ctx->cm_id, ¶m);
1085 ib_ucm_ctx_put(ctx);
1087 result = PTR_ERR(ctx);
1090 kfree(param.private_data);
1095 static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1096 const char __user *inbuf,
1097 int in_len, int out_len) = {
1098 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1099 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1100 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1101 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
1102 [IB_USER_CM_CMD_NOTIFY] = ib_ucm_notify,
1103 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1104 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1105 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1106 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1107 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1108 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1109 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1110 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1111 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1112 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1113 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1114 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
1115 [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
1118 static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1119 size_t len, loff_t *pos)
1121 struct ib_ucm_file *file = filp->private_data;
1122 struct ib_ucm_cmd_hdr hdr;
1125 if (len < sizeof(hdr))
1128 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1131 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1134 if (hdr.in + sizeof(hdr) > len)
1137 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1145 static unsigned int ib_ucm_poll(struct file *filp,
1146 struct poll_table_struct *wait)
1148 struct ib_ucm_file *file = filp->private_data;
1149 unsigned int mask = 0;
1151 poll_wait(filp, &file->poll_wait, wait);
1153 if (!list_empty(&file->events))
1154 mask = POLLIN | POLLRDNORM;
1159 static int ib_ucm_open(struct inode *inode, struct file *filp)
1161 struct ib_ucm_file *file;
1163 cycle_kernel_lock();
1164 file = kmalloc(sizeof(*file), GFP_KERNEL);
1168 INIT_LIST_HEAD(&file->events);
1169 INIT_LIST_HEAD(&file->ctxs);
1170 init_waitqueue_head(&file->poll_wait);
1172 mutex_init(&file->file_mutex);
1174 filp->private_data = file;
1176 file->device = container_of(inode->i_cdev, struct ib_ucm_device, cdev);
1181 static int ib_ucm_close(struct inode *inode, struct file *filp)
1183 struct ib_ucm_file *file = filp->private_data;
1184 struct ib_ucm_context *ctx;
1186 mutex_lock(&file->file_mutex);
1187 while (!list_empty(&file->ctxs)) {
1188 ctx = list_entry(file->ctxs.next,
1189 struct ib_ucm_context, file_list);
1190 mutex_unlock(&file->file_mutex);
1192 mutex_lock(&ctx_id_mutex);
1193 idr_remove(&ctx_id_table, ctx->id);
1194 mutex_unlock(&ctx_id_mutex);
1196 ib_destroy_cm_id(ctx->cm_id);
1197 ib_ucm_cleanup_events(ctx);
1200 mutex_lock(&file->file_mutex);
1202 mutex_unlock(&file->file_mutex);
1207 static void ib_ucm_release_dev(struct device *dev)
1209 struct ib_ucm_device *ucm_dev;
1211 ucm_dev = container_of(dev, struct ib_ucm_device, dev);
1212 cdev_del(&ucm_dev->cdev);
1213 clear_bit(ucm_dev->devnum, dev_map);
1217 static const struct file_operations ucm_fops = {
1218 .owner = THIS_MODULE,
1219 .open = ib_ucm_open,
1220 .release = ib_ucm_close,
1221 .write = ib_ucm_write,
1222 .poll = ib_ucm_poll,
1225 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1228 struct ib_ucm_device *ucm_dev;
1230 ucm_dev = container_of(dev, struct ib_ucm_device, dev);
1231 return sprintf(buf, "%s\n", ucm_dev->ib_dev->name);
1233 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1235 static void ib_ucm_add_one(struct ib_device *device)
1237 struct ib_ucm_device *ucm_dev;
1239 if (!device->alloc_ucontext ||
1240 rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1243 ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
1247 ucm_dev->ib_dev = device;
1249 ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1250 if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
1253 set_bit(ucm_dev->devnum, dev_map);
1255 cdev_init(&ucm_dev->cdev, &ucm_fops);
1256 ucm_dev->cdev.owner = THIS_MODULE;
1257 kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum);
1258 if (cdev_add(&ucm_dev->cdev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
1261 ucm_dev->dev.class = &cm_class;
1262 ucm_dev->dev.parent = device->dma_device;
1263 ucm_dev->dev.devt = ucm_dev->cdev.dev;
1264 ucm_dev->dev.release = ib_ucm_release_dev;
1265 snprintf(ucm_dev->dev.bus_id, BUS_ID_SIZE, "ucm%d",
1267 if (device_register(&ucm_dev->dev))
1270 if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev))
1273 ib_set_client_data(device, &ucm_client, ucm_dev);
1277 device_unregister(&ucm_dev->dev);
1279 cdev_del(&ucm_dev->cdev);
1280 clear_bit(ucm_dev->devnum, dev_map);
1286 static void ib_ucm_remove_one(struct ib_device *device)
1288 struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
1293 device_unregister(&ucm_dev->dev);
1296 static ssize_t show_abi_version(struct class *class, char *buf)
1298 return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
1300 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1302 static int __init ib_ucm_init(void)
1306 ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1309 printk(KERN_ERR "ucm: couldn't register device number\n");
1313 ret = class_create_file(&cm_class, &class_attr_abi_version);
1315 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
1319 ret = ib_register_client(&ucm_client);
1321 printk(KERN_ERR "ucm: couldn't register client\n");
1327 class_remove_file(&cm_class, &class_attr_abi_version);
1329 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1334 static void __exit ib_ucm_cleanup(void)
1336 ib_unregister_client(&ucm_client);
1337 class_remove_file(&cm_class, &class_attr_abi_version);
1338 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1339 idr_destroy(&ctx_id_table);
1342 module_init(ib_ucm_init);
1343 module_exit(ib_ucm_cleanup);