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
34 #include <linux/completion.h>
35 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/device.h>
39 #include <linux/err.h>
40 #include <linux/poll.h>
41 #include <linux/sched.h>
42 #include <linux/file.h>
43 #include <linux/mount.h>
44 #include <linux/cdev.h>
45 #include <linux/idr.h>
46 #include <linux/mutex.h>
47 #include <linux/slab.h>
49 #include <linux/nospec.h>
51 #include <asm/uaccess.h>
54 #include <rdma/ib_cm.h>
55 #include <rdma/ib_user_cm.h>
56 #include <rdma/ib_marshall.h>
58 MODULE_AUTHOR("Libor Michalek");
59 MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
60 MODULE_LICENSE("Dual BSD/GPL");
62 struct ib_ucm_device {
66 struct ib_device *ib_dev;
70 struct mutex file_mutex;
72 struct ib_ucm_device *device;
74 struct list_head ctxs;
75 struct list_head events;
76 wait_queue_head_t poll_wait;
79 struct ib_ucm_context {
81 struct completion comp;
85 struct ib_ucm_file *file;
86 struct ib_cm_id *cm_id;
89 struct list_head events; /* list of pending events. */
90 struct list_head file_list; /* member in file ctx list */
94 struct ib_ucm_context *ctx;
95 struct list_head file_list; /* member in file event list */
96 struct list_head ctx_list; /* member in ctx event list */
98 struct ib_cm_id *cm_id;
99 struct ib_ucm_event_resp resp;
108 IB_UCM_BASE_MINOR = 224,
109 IB_UCM_MAX_DEVICES = 32
112 #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
114 static void ib_ucm_add_one(struct ib_device *device);
115 static void ib_ucm_remove_one(struct ib_device *device, void *client_data);
117 static struct ib_client ucm_client = {
119 .add = ib_ucm_add_one,
120 .remove = ib_ucm_remove_one
123 static DEFINE_MUTEX(ctx_id_mutex);
124 static DEFINE_IDR(ctx_id_table);
125 static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
127 static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
129 struct ib_ucm_context *ctx;
131 mutex_lock(&ctx_id_mutex);
132 ctx = idr_find(&ctx_id_table, id);
134 ctx = ERR_PTR(-ENOENT);
135 else if (ctx->file != file)
136 ctx = ERR_PTR(-EINVAL);
138 atomic_inc(&ctx->ref);
139 mutex_unlock(&ctx_id_mutex);
144 static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
146 if (atomic_dec_and_test(&ctx->ref))
147 complete(&ctx->comp);
150 static inline int ib_ucm_new_cm_id(int event)
152 return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
155 static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
157 struct ib_ucm_event *uevent;
159 mutex_lock(&ctx->file->file_mutex);
160 list_del(&ctx->file_list);
161 while (!list_empty(&ctx->events)) {
163 uevent = list_entry(ctx->events.next,
164 struct ib_ucm_event, ctx_list);
165 list_del(&uevent->file_list);
166 list_del(&uevent->ctx_list);
167 mutex_unlock(&ctx->file->file_mutex);
169 /* clear incoming connections. */
170 if (ib_ucm_new_cm_id(uevent->resp.event))
171 ib_destroy_cm_id(uevent->cm_id);
174 mutex_lock(&ctx->file->file_mutex);
176 mutex_unlock(&ctx->file->file_mutex);
179 static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
181 struct ib_ucm_context *ctx;
183 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
187 atomic_set(&ctx->ref, 1);
188 init_completion(&ctx->comp);
190 INIT_LIST_HEAD(&ctx->events);
192 mutex_lock(&ctx_id_mutex);
193 ctx->id = idr_alloc(&ctx_id_table, ctx, 0, 0, GFP_KERNEL);
194 mutex_unlock(&ctx_id_mutex);
198 list_add_tail(&ctx->file_list, &file->ctxs);
206 static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
207 struct ib_cm_req_event_param *kreq)
209 ureq->remote_ca_guid = kreq->remote_ca_guid;
210 ureq->remote_qkey = kreq->remote_qkey;
211 ureq->remote_qpn = kreq->remote_qpn;
212 ureq->qp_type = kreq->qp_type;
213 ureq->starting_psn = kreq->starting_psn;
214 ureq->responder_resources = kreq->responder_resources;
215 ureq->initiator_depth = kreq->initiator_depth;
216 ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
217 ureq->flow_control = kreq->flow_control;
218 ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
219 ureq->retry_count = kreq->retry_count;
220 ureq->rnr_retry_count = kreq->rnr_retry_count;
221 ureq->srq = kreq->srq;
222 ureq->port = kreq->port;
224 ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
225 if (kreq->alternate_path)
226 ib_copy_path_rec_to_user(&ureq->alternate_path,
227 kreq->alternate_path);
230 static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
231 struct ib_cm_rep_event_param *krep)
233 urep->remote_ca_guid = krep->remote_ca_guid;
234 urep->remote_qkey = krep->remote_qkey;
235 urep->remote_qpn = krep->remote_qpn;
236 urep->starting_psn = krep->starting_psn;
237 urep->responder_resources = krep->responder_resources;
238 urep->initiator_depth = krep->initiator_depth;
239 urep->target_ack_delay = krep->target_ack_delay;
240 urep->failover_accepted = krep->failover_accepted;
241 urep->flow_control = krep->flow_control;
242 urep->rnr_retry_count = krep->rnr_retry_count;
243 urep->srq = krep->srq;
246 static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
247 struct ib_cm_sidr_rep_event_param *krep)
249 urep->status = krep->status;
250 urep->qkey = krep->qkey;
251 urep->qpn = krep->qpn;
254 static int ib_ucm_event_process(struct ib_cm_event *evt,
255 struct ib_ucm_event *uvt)
259 switch (evt->event) {
260 case IB_CM_REQ_RECEIVED:
261 ib_ucm_event_req_get(&uvt->resp.u.req_resp,
262 &evt->param.req_rcvd);
263 uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
264 uvt->resp.present = IB_UCM_PRES_PRIMARY;
265 uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
266 IB_UCM_PRES_ALTERNATE : 0);
268 case IB_CM_REP_RECEIVED:
269 ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
270 &evt->param.rep_rcvd);
271 uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
273 case IB_CM_RTU_RECEIVED:
274 uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
275 uvt->resp.u.send_status = evt->param.send_status;
277 case IB_CM_DREQ_RECEIVED:
278 uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
279 uvt->resp.u.send_status = evt->param.send_status;
281 case IB_CM_DREP_RECEIVED:
282 uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
283 uvt->resp.u.send_status = evt->param.send_status;
285 case IB_CM_MRA_RECEIVED:
286 uvt->resp.u.mra_resp.timeout =
287 evt->param.mra_rcvd.service_timeout;
288 uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
290 case IB_CM_REJ_RECEIVED:
291 uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
292 uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
293 uvt->info_len = evt->param.rej_rcvd.ari_length;
294 info = evt->param.rej_rcvd.ari;
296 case IB_CM_LAP_RECEIVED:
297 ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
298 evt->param.lap_rcvd.alternate_path);
299 uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
300 uvt->resp.present = IB_UCM_PRES_ALTERNATE;
302 case IB_CM_APR_RECEIVED:
303 uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
304 uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
305 uvt->info_len = evt->param.apr_rcvd.info_len;
306 info = evt->param.apr_rcvd.apr_info;
308 case IB_CM_SIDR_REQ_RECEIVED:
309 uvt->resp.u.sidr_req_resp.pkey =
310 evt->param.sidr_req_rcvd.pkey;
311 uvt->resp.u.sidr_req_resp.port =
312 evt->param.sidr_req_rcvd.port;
313 uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
315 case IB_CM_SIDR_REP_RECEIVED:
316 ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
317 &evt->param.sidr_rep_rcvd);
318 uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
319 uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
320 info = evt->param.sidr_rep_rcvd.info;
323 uvt->resp.u.send_status = evt->param.send_status;
328 uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL);
332 uvt->resp.present |= IB_UCM_PRES_DATA;
336 uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL);
340 uvt->resp.present |= IB_UCM_PRES_INFO;
350 static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
351 struct ib_cm_event *event)
353 struct ib_ucm_event *uevent;
354 struct ib_ucm_context *ctx;
357 ctx = cm_id->context;
359 uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
364 uevent->cm_id = cm_id;
365 uevent->resp.uid = ctx->uid;
366 uevent->resp.id = ctx->id;
367 uevent->resp.event = event->event;
369 result = ib_ucm_event_process(event, uevent);
373 mutex_lock(&ctx->file->file_mutex);
374 list_add_tail(&uevent->file_list, &ctx->file->events);
375 list_add_tail(&uevent->ctx_list, &ctx->events);
376 wake_up_interruptible(&ctx->file->poll_wait);
377 mutex_unlock(&ctx->file->file_mutex);
383 /* Destroy new cm_id's */
384 return ib_ucm_new_cm_id(event->event);
387 static ssize_t ib_ucm_event(struct ib_ucm_file *file,
388 const char __user *inbuf,
389 int in_len, int out_len)
391 struct ib_ucm_context *ctx;
392 struct ib_ucm_event_get cmd;
393 struct ib_ucm_event *uevent;
396 if (out_len < sizeof(struct ib_ucm_event_resp))
399 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
402 mutex_lock(&file->file_mutex);
403 while (list_empty(&file->events)) {
404 mutex_unlock(&file->file_mutex);
406 if (file->filp->f_flags & O_NONBLOCK)
409 if (wait_event_interruptible(file->poll_wait,
410 !list_empty(&file->events)))
413 mutex_lock(&file->file_mutex);
416 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
418 if (ib_ucm_new_cm_id(uevent->resp.event)) {
419 ctx = ib_ucm_ctx_alloc(file);
425 ctx->cm_id = uevent->cm_id;
426 ctx->cm_id->context = ctx;
427 uevent->resp.id = ctx->id;
430 if (copy_to_user((void __user *)(unsigned long)cmd.response,
431 &uevent->resp, sizeof(uevent->resp))) {
437 if (cmd.data_len < uevent->data_len) {
441 if (copy_to_user((void __user *)(unsigned long)cmd.data,
442 uevent->data, uevent->data_len)) {
449 if (cmd.info_len < uevent->info_len) {
453 if (copy_to_user((void __user *)(unsigned long)cmd.info,
454 uevent->info, uevent->info_len)) {
460 list_del(&uevent->file_list);
461 list_del(&uevent->ctx_list);
462 uevent->ctx->events_reported++;
468 mutex_unlock(&file->file_mutex);
472 static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
473 const char __user *inbuf,
474 int in_len, int out_len)
476 struct ib_ucm_create_id cmd;
477 struct ib_ucm_create_id_resp resp;
478 struct ib_ucm_context *ctx;
481 if (out_len < sizeof(resp))
484 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
487 mutex_lock(&file->file_mutex);
488 ctx = ib_ucm_ctx_alloc(file);
489 mutex_unlock(&file->file_mutex);
494 ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
495 ib_ucm_event_handler, ctx);
496 if (IS_ERR(ctx->cm_id)) {
497 result = PTR_ERR(ctx->cm_id);
502 if (copy_to_user((void __user *)(unsigned long)cmd.response,
503 &resp, sizeof(resp))) {
510 ib_destroy_cm_id(ctx->cm_id);
512 mutex_lock(&ctx_id_mutex);
513 idr_remove(&ctx_id_table, ctx->id);
514 mutex_unlock(&ctx_id_mutex);
519 static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
520 const char __user *inbuf,
521 int in_len, int out_len)
523 struct ib_ucm_destroy_id cmd;
524 struct ib_ucm_destroy_id_resp resp;
525 struct ib_ucm_context *ctx;
528 if (out_len < sizeof(resp))
531 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
534 mutex_lock(&ctx_id_mutex);
535 ctx = idr_find(&ctx_id_table, cmd.id);
537 ctx = ERR_PTR(-ENOENT);
538 else if (ctx->file != file)
539 ctx = ERR_PTR(-EINVAL);
541 idr_remove(&ctx_id_table, ctx->id);
542 mutex_unlock(&ctx_id_mutex);
548 wait_for_completion(&ctx->comp);
550 /* No new events will be generated after destroying the cm_id. */
551 ib_destroy_cm_id(ctx->cm_id);
552 /* Cleanup events not yet reported to the user. */
553 ib_ucm_cleanup_events(ctx);
555 resp.events_reported = ctx->events_reported;
556 if (copy_to_user((void __user *)(unsigned long)cmd.response,
557 &resp, sizeof(resp)))
564 static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
565 const char __user *inbuf,
566 int in_len, int out_len)
568 struct ib_ucm_attr_id_resp resp;
569 struct ib_ucm_attr_id cmd;
570 struct ib_ucm_context *ctx;
573 if (out_len < sizeof(resp))
576 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
579 ctx = ib_ucm_ctx_get(file, cmd.id);
583 resp.service_id = ctx->cm_id->service_id;
584 resp.service_mask = ctx->cm_id->service_mask;
585 resp.local_id = ctx->cm_id->local_id;
586 resp.remote_id = ctx->cm_id->remote_id;
588 if (copy_to_user((void __user *)(unsigned long)cmd.response,
589 &resp, sizeof(resp)))
596 static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
597 const char __user *inbuf,
598 int in_len, int out_len)
600 struct ib_uverbs_qp_attr resp;
601 struct ib_ucm_init_qp_attr cmd;
602 struct ib_ucm_context *ctx;
603 struct ib_qp_attr qp_attr;
606 if (out_len < sizeof(resp))
609 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
612 ctx = ib_ucm_ctx_get(file, cmd.id);
616 resp.qp_attr_mask = 0;
617 memset(&qp_attr, 0, sizeof qp_attr);
618 qp_attr.qp_state = cmd.qp_state;
619 result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
623 ib_copy_qp_attr_to_user(&resp, &qp_attr);
625 if (copy_to_user((void __user *)(unsigned long)cmd.response,
626 &resp, sizeof(resp)))
634 static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
636 service_id &= service_mask;
638 if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
639 ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
645 static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
646 const char __user *inbuf,
647 int in_len, int out_len)
649 struct ib_ucm_listen cmd;
650 struct ib_ucm_context *ctx;
653 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
656 ctx = ib_ucm_ctx_get(file, cmd.id);
660 result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
664 result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
670 static ssize_t ib_ucm_notify(struct ib_ucm_file *file,
671 const char __user *inbuf,
672 int in_len, int out_len)
674 struct ib_ucm_notify cmd;
675 struct ib_ucm_context *ctx;
678 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
681 ctx = ib_ucm_ctx_get(file, cmd.id);
685 result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
690 static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
699 data = memdup_user((void __user *)(unsigned long)src, len);
701 return PTR_ERR(data);
707 static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
709 struct ib_user_path_rec upath;
710 struct ib_sa_path_rec *sa_path;
717 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
721 if (copy_from_user(&upath, (void __user *)(unsigned long)src,
728 ib_copy_path_rec_from_user(sa_path, &upath);
733 static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
734 const char __user *inbuf,
735 int in_len, int out_len)
737 struct ib_cm_req_param param;
738 struct ib_ucm_context *ctx;
739 struct ib_ucm_req cmd;
742 param.private_data = NULL;
743 param.primary_path = NULL;
744 param.alternate_path = NULL;
746 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
749 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
753 result = ib_ucm_path_get(¶m.primary_path, cmd.primary_path);
757 result = ib_ucm_path_get(¶m.alternate_path, cmd.alternate_path);
761 param.private_data_len = cmd.len;
762 param.service_id = cmd.sid;
763 param.qp_num = cmd.qpn;
764 param.qp_type = cmd.qp_type;
765 param.starting_psn = cmd.psn;
766 param.peer_to_peer = cmd.peer_to_peer;
767 param.responder_resources = cmd.responder_resources;
768 param.initiator_depth = cmd.initiator_depth;
769 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
770 param.flow_control = cmd.flow_control;
771 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
772 param.retry_count = cmd.retry_count;
773 param.rnr_retry_count = cmd.rnr_retry_count;
774 param.max_cm_retries = cmd.max_cm_retries;
777 ctx = ib_ucm_ctx_get(file, cmd.id);
779 result = ib_send_cm_req(ctx->cm_id, ¶m);
782 result = PTR_ERR(ctx);
785 kfree(param.private_data);
786 kfree(param.primary_path);
787 kfree(param.alternate_path);
791 static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
792 const char __user *inbuf,
793 int in_len, int out_len)
795 struct ib_cm_rep_param param;
796 struct ib_ucm_context *ctx;
797 struct ib_ucm_rep cmd;
800 param.private_data = NULL;
802 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
805 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
809 param.qp_num = cmd.qpn;
810 param.starting_psn = cmd.psn;
811 param.private_data_len = cmd.len;
812 param.responder_resources = cmd.responder_resources;
813 param.initiator_depth = cmd.initiator_depth;
814 param.failover_accepted = cmd.failover_accepted;
815 param.flow_control = cmd.flow_control;
816 param.rnr_retry_count = cmd.rnr_retry_count;
819 ctx = ib_ucm_ctx_get(file, cmd.id);
822 result = ib_send_cm_rep(ctx->cm_id, ¶m);
825 result = PTR_ERR(ctx);
827 kfree(param.private_data);
831 static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
832 const char __user *inbuf, int in_len,
833 int (*func)(struct ib_cm_id *cm_id,
834 const void *private_data,
835 u8 private_data_len))
837 struct ib_ucm_private_data cmd;
838 struct ib_ucm_context *ctx;
839 const void *private_data = NULL;
842 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
845 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
849 ctx = ib_ucm_ctx_get(file, cmd.id);
851 result = func(ctx->cm_id, private_data, cmd.len);
854 result = PTR_ERR(ctx);
860 static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
861 const char __user *inbuf,
862 int in_len, int out_len)
864 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
867 static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
868 const char __user *inbuf,
869 int in_len, int out_len)
871 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
874 static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
875 const char __user *inbuf,
876 int in_len, int out_len)
878 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
881 static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
882 const char __user *inbuf, int in_len,
883 int (*func)(struct ib_cm_id *cm_id,
890 struct ib_ucm_context *ctx;
891 struct ib_ucm_info cmd;
892 const void *data = NULL;
893 const void *info = NULL;
896 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
899 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
903 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
907 ctx = ib_ucm_ctx_get(file, cmd.id);
909 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
913 result = PTR_ERR(ctx);
921 static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
922 const char __user *inbuf,
923 int in_len, int out_len)
925 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
928 static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
929 const char __user *inbuf,
930 int in_len, int out_len)
932 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
935 static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
936 const char __user *inbuf,
937 int in_len, int out_len)
939 struct ib_ucm_context *ctx;
940 struct ib_ucm_mra cmd;
941 const void *data = NULL;
944 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
947 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
951 ctx = ib_ucm_ctx_get(file, cmd.id);
953 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
956 result = PTR_ERR(ctx);
962 static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
963 const char __user *inbuf,
964 int in_len, int out_len)
966 struct ib_ucm_context *ctx;
967 struct ib_sa_path_rec *path = NULL;
968 struct ib_ucm_lap cmd;
969 const void *data = NULL;
972 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
975 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
979 result = ib_ucm_path_get(&path, cmd.path);
983 ctx = ib_ucm_ctx_get(file, cmd.id);
985 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
988 result = PTR_ERR(ctx);
996 static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
997 const char __user *inbuf,
998 int in_len, int out_len)
1000 struct ib_cm_sidr_req_param param;
1001 struct ib_ucm_context *ctx;
1002 struct ib_ucm_sidr_req cmd;
1005 param.private_data = NULL;
1008 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1011 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
1015 result = ib_ucm_path_get(¶m.path, cmd.path);
1019 param.private_data_len = cmd.len;
1020 param.service_id = cmd.sid;
1021 param.timeout_ms = cmd.timeout;
1022 param.max_cm_retries = cmd.max_cm_retries;
1024 ctx = ib_ucm_ctx_get(file, cmd.id);
1026 result = ib_send_cm_sidr_req(ctx->cm_id, ¶m);
1027 ib_ucm_ctx_put(ctx);
1029 result = PTR_ERR(ctx);
1032 kfree(param.private_data);
1037 static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1038 const char __user *inbuf,
1039 int in_len, int out_len)
1041 struct ib_cm_sidr_rep_param param;
1042 struct ib_ucm_sidr_rep cmd;
1043 struct ib_ucm_context *ctx;
1048 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1051 result = ib_ucm_alloc_data(¶m.private_data,
1052 cmd.data, cmd.data_len);
1056 result = ib_ucm_alloc_data(¶m.info, cmd.info, cmd.info_len);
1060 param.qp_num = cmd.qpn;
1061 param.qkey = cmd.qkey;
1062 param.status = cmd.status;
1063 param.info_length = cmd.info_len;
1064 param.private_data_len = cmd.data_len;
1066 ctx = ib_ucm_ctx_get(file, cmd.id);
1068 result = ib_send_cm_sidr_rep(ctx->cm_id, ¶m);
1069 ib_ucm_ctx_put(ctx);
1071 result = PTR_ERR(ctx);
1074 kfree(param.private_data);
1079 static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1080 const char __user *inbuf,
1081 int in_len, int out_len) = {
1082 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1083 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1084 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1085 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
1086 [IB_USER_CM_CMD_NOTIFY] = ib_ucm_notify,
1087 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1088 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1089 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1090 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1091 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1092 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1093 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1094 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1095 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1096 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1097 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1098 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
1099 [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
1102 static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1103 size_t len, loff_t *pos)
1105 struct ib_ucm_file *file = filp->private_data;
1106 struct ib_ucm_cmd_hdr hdr;
1109 if (WARN_ON_ONCE(!ib_safe_file_access(filp)))
1112 if (len < sizeof(hdr))
1115 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1118 if (hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1120 hdr.cmd = array_index_nospec(hdr.cmd, ARRAY_SIZE(ucm_cmd_table));
1122 if (hdr.in + sizeof(hdr) > len)
1125 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1133 static unsigned int ib_ucm_poll(struct file *filp,
1134 struct poll_table_struct *wait)
1136 struct ib_ucm_file *file = filp->private_data;
1137 unsigned int mask = 0;
1139 poll_wait(filp, &file->poll_wait, wait);
1141 if (!list_empty(&file->events))
1142 mask = POLLIN | POLLRDNORM;
1148 * ib_ucm_open() does not need the BKL:
1150 * - no global state is referred to;
1151 * - there is no ioctl method to race against;
1152 * - no further module initialization is required for open to work
1153 * after the device is registered.
1155 static int ib_ucm_open(struct inode *inode, struct file *filp)
1157 struct ib_ucm_file *file;
1159 file = kmalloc(sizeof(*file), GFP_KERNEL);
1163 INIT_LIST_HEAD(&file->events);
1164 INIT_LIST_HEAD(&file->ctxs);
1165 init_waitqueue_head(&file->poll_wait);
1167 mutex_init(&file->file_mutex);
1169 filp->private_data = file;
1171 file->device = container_of(inode->i_cdev, struct ib_ucm_device, cdev);
1173 return nonseekable_open(inode, filp);
1176 static int ib_ucm_close(struct inode *inode, struct file *filp)
1178 struct ib_ucm_file *file = filp->private_data;
1179 struct ib_ucm_context *ctx;
1181 mutex_lock(&file->file_mutex);
1182 while (!list_empty(&file->ctxs)) {
1183 ctx = list_entry(file->ctxs.next,
1184 struct ib_ucm_context, file_list);
1185 mutex_unlock(&file->file_mutex);
1187 mutex_lock(&ctx_id_mutex);
1188 idr_remove(&ctx_id_table, ctx->id);
1189 mutex_unlock(&ctx_id_mutex);
1191 ib_destroy_cm_id(ctx->cm_id);
1192 ib_ucm_cleanup_events(ctx);
1195 mutex_lock(&file->file_mutex);
1197 mutex_unlock(&file->file_mutex);
1202 static DECLARE_BITMAP(overflow_map, IB_UCM_MAX_DEVICES);
1203 static void ib_ucm_release_dev(struct device *dev)
1205 struct ib_ucm_device *ucm_dev;
1207 ucm_dev = container_of(dev, struct ib_ucm_device, dev);
1208 cdev_del(&ucm_dev->cdev);
1209 if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
1210 clear_bit(ucm_dev->devnum, dev_map);
1212 clear_bit(ucm_dev->devnum - IB_UCM_MAX_DEVICES, overflow_map);
1216 static const struct file_operations ucm_fops = {
1217 .owner = THIS_MODULE,
1218 .open = ib_ucm_open,
1219 .release = ib_ucm_close,
1220 .write = ib_ucm_write,
1221 .poll = ib_ucm_poll,
1222 .llseek = no_llseek,
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 dev_t overflow_maj;
1236 static int find_overflow_devnum(void)
1240 if (!overflow_maj) {
1241 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UCM_MAX_DEVICES,
1244 pr_err("ucm: couldn't register dynamic device number\n");
1249 ret = find_first_zero_bit(overflow_map, IB_UCM_MAX_DEVICES);
1250 if (ret >= IB_UCM_MAX_DEVICES)
1256 static void ib_ucm_add_one(struct ib_device *device)
1260 struct ib_ucm_device *ucm_dev;
1262 if (!device->alloc_ucontext || !rdma_cap_ib_cm(device, 1))
1265 ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
1269 ucm_dev->ib_dev = device;
1271 devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1272 if (devnum >= IB_UCM_MAX_DEVICES) {
1273 devnum = find_overflow_devnum();
1277 ucm_dev->devnum = devnum + IB_UCM_MAX_DEVICES;
1278 base = devnum + overflow_maj;
1279 set_bit(devnum, overflow_map);
1281 ucm_dev->devnum = devnum;
1282 base = devnum + IB_UCM_BASE_DEV;
1283 set_bit(devnum, dev_map);
1286 cdev_init(&ucm_dev->cdev, &ucm_fops);
1287 ucm_dev->cdev.owner = THIS_MODULE;
1288 kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum);
1289 if (cdev_add(&ucm_dev->cdev, base, 1))
1292 ucm_dev->dev.class = &cm_class;
1293 ucm_dev->dev.parent = device->dma_device;
1294 ucm_dev->dev.devt = ucm_dev->cdev.dev;
1295 ucm_dev->dev.release = ib_ucm_release_dev;
1296 dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum);
1297 if (device_register(&ucm_dev->dev))
1300 if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev))
1303 ib_set_client_data(device, &ucm_client, ucm_dev);
1307 device_unregister(&ucm_dev->dev);
1309 cdev_del(&ucm_dev->cdev);
1310 if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
1311 clear_bit(devnum, dev_map);
1313 clear_bit(devnum, overflow_map);
1319 static void ib_ucm_remove_one(struct ib_device *device, void *client_data)
1321 struct ib_ucm_device *ucm_dev = client_data;
1326 device_unregister(&ucm_dev->dev);
1329 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1330 __stringify(IB_USER_CM_ABI_VERSION));
1332 static int __init ib_ucm_init(void)
1336 ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1339 pr_err("ucm: couldn't register device number\n");
1343 ret = class_create_file(&cm_class, &class_attr_abi_version.attr);
1345 pr_err("ucm: couldn't create abi_version attribute\n");
1349 ret = ib_register_client(&ucm_client);
1351 pr_err("ucm: couldn't register client\n");
1357 class_remove_file(&cm_class, &class_attr_abi_version.attr);
1359 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1364 static void __exit ib_ucm_cleanup(void)
1366 ib_unregister_client(&ucm_client);
1367 class_remove_file(&cm_class, &class_attr_abi_version.attr);
1368 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1370 unregister_chrdev_region(overflow_maj, IB_UCM_MAX_DEVICES);
1371 idr_destroy(&ctx_id_table);
1374 module_init(ib_ucm_init);
1375 module_exit(ib_ucm_cleanup);