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 2594 2005-06-13 19:46:02Z libor $
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/file.h>
42 #include <linux/mount.h>
43 #include <linux/cdev.h>
44 #include <linux/idr.h>
45 #include <linux/mutex.h>
47 #include <asm/uaccess.h>
49 #include <rdma/ib_cm.h>
50 #include <rdma/ib_user_cm.h>
52 MODULE_AUTHOR("Libor Michalek");
53 MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
54 MODULE_LICENSE("Dual BSD/GPL");
56 struct ib_ucm_device {
59 struct class_device class_dev;
60 struct ib_device *ib_dev;
64 struct semaphore mutex;
66 struct ib_ucm_device *device;
68 struct list_head ctxs;
69 struct list_head events;
70 wait_queue_head_t poll_wait;
73 struct ib_ucm_context {
75 wait_queue_head_t wait;
79 struct ib_ucm_file *file;
80 struct ib_cm_id *cm_id;
83 struct list_head events; /* list of pending events. */
84 struct list_head file_list; /* member in file ctx list */
88 struct ib_ucm_context *ctx;
89 struct list_head file_list; /* member in file event list */
90 struct list_head ctx_list; /* member in ctx event list */
92 struct ib_cm_id *cm_id;
93 struct ib_ucm_event_resp resp;
102 IB_UCM_BASE_MINOR = 224,
103 IB_UCM_MAX_DEVICES = 32
106 #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
108 static void ib_ucm_add_one(struct ib_device *device);
109 static void ib_ucm_remove_one(struct ib_device *device);
111 static struct ib_client ucm_client = {
113 .add = ib_ucm_add_one,
114 .remove = ib_ucm_remove_one
117 static DEFINE_MUTEX(ctx_id_mutex);
118 static DEFINE_IDR(ctx_id_table);
119 static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
121 static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
123 struct ib_ucm_context *ctx;
125 mutex_lock(&ctx_id_mutex);
126 ctx = idr_find(&ctx_id_table, id);
128 ctx = ERR_PTR(-ENOENT);
129 else if (ctx->file != file)
130 ctx = ERR_PTR(-EINVAL);
132 atomic_inc(&ctx->ref);
133 mutex_unlock(&ctx_id_mutex);
138 static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
140 if (atomic_dec_and_test(&ctx->ref))
144 static inline int ib_ucm_new_cm_id(int event)
146 return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
149 static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
151 struct ib_ucm_event *uevent;
153 down(&ctx->file->mutex);
154 list_del(&ctx->file_list);
155 while (!list_empty(&ctx->events)) {
157 uevent = list_entry(ctx->events.next,
158 struct ib_ucm_event, ctx_list);
159 list_del(&uevent->file_list);
160 list_del(&uevent->ctx_list);
162 /* clear incoming connections. */
163 if (ib_ucm_new_cm_id(uevent->resp.event))
164 ib_destroy_cm_id(uevent->cm_id);
168 up(&ctx->file->mutex);
171 static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
173 struct ib_ucm_context *ctx;
176 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
180 atomic_set(&ctx->ref, 1);
181 init_waitqueue_head(&ctx->wait);
183 INIT_LIST_HEAD(&ctx->events);
186 result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
190 mutex_lock(&ctx_id_mutex);
191 result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
192 mutex_unlock(&ctx_id_mutex);
193 } while (result == -EAGAIN);
198 list_add_tail(&ctx->file_list, &file->ctxs);
206 static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
207 struct ib_sa_path_rec *kpath)
209 if (!kpath || !upath)
212 memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid);
213 memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid);
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;
232 upath->packet_life_time_selector =
233 kpath->packet_life_time_selector;
236 static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
237 struct ib_cm_req_event_param *kreq)
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;
254 ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
255 ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
258 static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
259 struct ib_cm_rep_event_param *krep)
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;
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)
277 urep->status = krep->status;
278 urep->qkey = krep->qkey;
279 urep->qpn = krep->qpn;
282 static int ib_ucm_event_process(struct ib_cm_event *evt,
283 struct ib_ucm_event *uvt)
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);
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
351 uvt->resp.u.send_status = evt->param.send_status;
356 uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
360 memcpy(uvt->data, evt->private_data, uvt->data_len);
361 uvt->resp.present |= IB_UCM_PRES_DATA;
365 uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
369 memcpy(uvt->info, info, uvt->info_len);
370 uvt->resp.present |= IB_UCM_PRES_INFO;
380 static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
381 struct ib_cm_event *event)
383 struct ib_ucm_event *uevent;
384 struct ib_ucm_context *ctx;
387 ctx = cm_id->context;
389 uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
394 uevent->cm_id = cm_id;
395 uevent->resp.uid = ctx->uid;
396 uevent->resp.id = ctx->id;
397 uevent->resp.event = event->event;
399 result = ib_ucm_event_process(event, uevent);
403 down(&ctx->file->mutex);
404 list_add_tail(&uevent->file_list, &ctx->file->events);
405 list_add_tail(&uevent->ctx_list, &ctx->events);
406 wake_up_interruptible(&ctx->file->poll_wait);
407 up(&ctx->file->mutex);
413 /* Destroy new cm_id's */
414 return ib_ucm_new_cm_id(event->event);
417 static ssize_t ib_ucm_event(struct ib_ucm_file *file,
418 const char __user *inbuf,
419 int in_len, int out_len)
421 struct ib_ucm_context *ctx;
422 struct ib_ucm_event_get cmd;
423 struct ib_ucm_event *uevent;
427 if (out_len < sizeof(struct ib_ucm_event_resp))
430 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
434 while (list_empty(&file->events)) {
436 if (file->filp->f_flags & O_NONBLOCK) {
441 if (signal_pending(current)) {
442 result = -ERESTARTSYS;
446 prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
452 finish_wait(&file->poll_wait, &wait);
458 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
460 if (ib_ucm_new_cm_id(uevent->resp.event)) {
461 ctx = ib_ucm_ctx_alloc(file);
467 ctx->cm_id = uevent->cm_id;
468 ctx->cm_id->context = ctx;
469 uevent->resp.id = ctx->id;
472 if (copy_to_user((void __user *)(unsigned long)cmd.response,
473 &uevent->resp, sizeof(uevent->resp))) {
479 if (cmd.data_len < uevent->data_len) {
483 if (copy_to_user((void __user *)(unsigned long)cmd.data,
484 uevent->data, uevent->data_len)) {
491 if (cmd.info_len < uevent->info_len) {
495 if (copy_to_user((void __user *)(unsigned long)cmd.info,
496 uevent->info, uevent->info_len)) {
502 list_del(&uevent->file_list);
503 list_del(&uevent->ctx_list);
504 uevent->ctx->events_reported++;
514 static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
515 const char __user *inbuf,
516 int in_len, int out_len)
518 struct ib_ucm_create_id cmd;
519 struct ib_ucm_create_id_resp resp;
520 struct ib_ucm_context *ctx;
523 if (out_len < sizeof(resp))
526 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
530 ctx = ib_ucm_ctx_alloc(file);
536 ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
537 ib_ucm_event_handler, ctx);
538 if (IS_ERR(ctx->cm_id)) {
539 result = PTR_ERR(ctx->cm_id);
544 if (copy_to_user((void __user *)(unsigned long)cmd.response,
545 &resp, sizeof(resp))) {
552 ib_destroy_cm_id(ctx->cm_id);
554 mutex_lock(&ctx_id_mutex);
555 idr_remove(&ctx_id_table, ctx->id);
556 mutex_unlock(&ctx_id_mutex);
561 static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
562 const char __user *inbuf,
563 int in_len, int out_len)
565 struct ib_ucm_destroy_id cmd;
566 struct ib_ucm_destroy_id_resp resp;
567 struct ib_ucm_context *ctx;
570 if (out_len < sizeof(resp))
573 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
576 mutex_lock(&ctx_id_mutex);
577 ctx = idr_find(&ctx_id_table, cmd.id);
579 ctx = ERR_PTR(-ENOENT);
580 else if (ctx->file != file)
581 ctx = ERR_PTR(-EINVAL);
583 idr_remove(&ctx_id_table, ctx->id);
584 mutex_unlock(&ctx_id_mutex);
589 atomic_dec(&ctx->ref);
590 wait_event(ctx->wait, !atomic_read(&ctx->ref));
592 /* No new events will be generated after destroying the cm_id. */
593 ib_destroy_cm_id(ctx->cm_id);
594 /* Cleanup events not yet reported to the user. */
595 ib_ucm_cleanup_events(ctx);
597 resp.events_reported = ctx->events_reported;
598 if (copy_to_user((void __user *)(unsigned long)cmd.response,
599 &resp, sizeof(resp)))
606 static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
607 const char __user *inbuf,
608 int in_len, int out_len)
610 struct ib_ucm_attr_id_resp resp;
611 struct ib_ucm_attr_id cmd;
612 struct ib_ucm_context *ctx;
615 if (out_len < sizeof(resp))
618 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
621 ctx = ib_ucm_ctx_get(file, cmd.id);
625 resp.service_id = ctx->cm_id->service_id;
626 resp.service_mask = ctx->cm_id->service_mask;
627 resp.local_id = ctx->cm_id->local_id;
628 resp.remote_id = ctx->cm_id->remote_id;
630 if (copy_to_user((void __user *)(unsigned long)cmd.response,
631 &resp, sizeof(resp)))
638 static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr *dest_attr,
639 struct ib_ah_attr *src_attr)
641 memcpy(dest_attr->grh_dgid, src_attr->grh.dgid.raw,
642 sizeof src_attr->grh.dgid);
643 dest_attr->grh_flow_label = src_attr->grh.flow_label;
644 dest_attr->grh_sgid_index = src_attr->grh.sgid_index;
645 dest_attr->grh_hop_limit = src_attr->grh.hop_limit;
646 dest_attr->grh_traffic_class = src_attr->grh.traffic_class;
648 dest_attr->dlid = src_attr->dlid;
649 dest_attr->sl = src_attr->sl;
650 dest_attr->src_path_bits = src_attr->src_path_bits;
651 dest_attr->static_rate = src_attr->static_rate;
652 dest_attr->is_global = (src_attr->ah_flags & IB_AH_GRH);
653 dest_attr->port_num = src_attr->port_num;
656 static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp *dest_attr,
657 struct ib_qp_attr *src_attr)
659 dest_attr->cur_qp_state = src_attr->cur_qp_state;
660 dest_attr->path_mtu = src_attr->path_mtu;
661 dest_attr->path_mig_state = src_attr->path_mig_state;
662 dest_attr->qkey = src_attr->qkey;
663 dest_attr->rq_psn = src_attr->rq_psn;
664 dest_attr->sq_psn = src_attr->sq_psn;
665 dest_attr->dest_qp_num = src_attr->dest_qp_num;
666 dest_attr->qp_access_flags = src_attr->qp_access_flags;
668 dest_attr->max_send_wr = src_attr->cap.max_send_wr;
669 dest_attr->max_recv_wr = src_attr->cap.max_recv_wr;
670 dest_attr->max_send_sge = src_attr->cap.max_send_sge;
671 dest_attr->max_recv_sge = src_attr->cap.max_recv_sge;
672 dest_attr->max_inline_data = src_attr->cap.max_inline_data;
674 ib_ucm_copy_ah_attr(&dest_attr->ah_attr, &src_attr->ah_attr);
675 ib_ucm_copy_ah_attr(&dest_attr->alt_ah_attr, &src_attr->alt_ah_attr);
677 dest_attr->pkey_index = src_attr->pkey_index;
678 dest_attr->alt_pkey_index = src_attr->alt_pkey_index;
679 dest_attr->en_sqd_async_notify = src_attr->en_sqd_async_notify;
680 dest_attr->sq_draining = src_attr->sq_draining;
681 dest_attr->max_rd_atomic = src_attr->max_rd_atomic;
682 dest_attr->max_dest_rd_atomic = src_attr->max_dest_rd_atomic;
683 dest_attr->min_rnr_timer = src_attr->min_rnr_timer;
684 dest_attr->port_num = src_attr->port_num;
685 dest_attr->timeout = src_attr->timeout;
686 dest_attr->retry_cnt = src_attr->retry_cnt;
687 dest_attr->rnr_retry = src_attr->rnr_retry;
688 dest_attr->alt_port_num = src_attr->alt_port_num;
689 dest_attr->alt_timeout = src_attr->alt_timeout;
692 static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
693 const char __user *inbuf,
694 int in_len, int out_len)
696 struct ib_ucm_init_qp_attr_resp resp;
697 struct ib_ucm_init_qp_attr cmd;
698 struct ib_ucm_context *ctx;
699 struct ib_qp_attr qp_attr;
702 if (out_len < sizeof(resp))
705 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
708 ctx = ib_ucm_ctx_get(file, cmd.id);
712 resp.qp_attr_mask = 0;
713 memset(&qp_attr, 0, sizeof qp_attr);
714 qp_attr.qp_state = cmd.qp_state;
715 result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
719 ib_ucm_copy_qp_attr(&resp, &qp_attr);
721 if (copy_to_user((void __user *)(unsigned long)cmd.response,
722 &resp, sizeof(resp)))
730 static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
731 const char __user *inbuf,
732 int in_len, int out_len)
734 struct ib_ucm_listen cmd;
735 struct ib_ucm_context *ctx;
738 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
741 ctx = ib_ucm_ctx_get(file, cmd.id);
745 result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
750 static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
751 const char __user *inbuf,
752 int in_len, int out_len)
754 struct ib_ucm_establish cmd;
755 struct ib_ucm_context *ctx;
758 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
761 ctx = ib_ucm_ctx_get(file, cmd.id);
765 result = ib_cm_establish(ctx->cm_id);
770 static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
779 data = kmalloc(len, GFP_KERNEL);
783 if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
792 static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
794 struct ib_ucm_path_rec ucm_path;
795 struct ib_sa_path_rec *sa_path;
802 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
806 if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
813 memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid);
814 memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid);
816 sa_path->dlid = ucm_path.dlid;
817 sa_path->slid = ucm_path.slid;
818 sa_path->raw_traffic = ucm_path.raw_traffic;
819 sa_path->flow_label = ucm_path.flow_label;
820 sa_path->hop_limit = ucm_path.hop_limit;
821 sa_path->traffic_class = ucm_path.traffic_class;
822 sa_path->reversible = ucm_path.reversible;
823 sa_path->numb_path = ucm_path.numb_path;
824 sa_path->pkey = ucm_path.pkey;
825 sa_path->sl = ucm_path.sl;
826 sa_path->mtu_selector = ucm_path.mtu_selector;
827 sa_path->mtu = ucm_path.mtu;
828 sa_path->rate_selector = ucm_path.rate_selector;
829 sa_path->rate = ucm_path.rate;
830 sa_path->packet_life_time = ucm_path.packet_life_time;
831 sa_path->preference = ucm_path.preference;
833 sa_path->packet_life_time_selector =
834 ucm_path.packet_life_time_selector;
840 static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
841 const char __user *inbuf,
842 int in_len, int out_len)
844 struct ib_cm_req_param param;
845 struct ib_ucm_context *ctx;
846 struct ib_ucm_req cmd;
849 param.private_data = NULL;
850 param.primary_path = NULL;
851 param.alternate_path = NULL;
853 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
856 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
860 result = ib_ucm_path_get(¶m.primary_path, cmd.primary_path);
864 result = ib_ucm_path_get(¶m.alternate_path, cmd.alternate_path);
868 param.private_data_len = cmd.len;
869 param.service_id = cmd.sid;
870 param.qp_num = cmd.qpn;
871 param.qp_type = cmd.qp_type;
872 param.starting_psn = cmd.psn;
873 param.peer_to_peer = cmd.peer_to_peer;
874 param.responder_resources = cmd.responder_resources;
875 param.initiator_depth = cmd.initiator_depth;
876 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
877 param.flow_control = cmd.flow_control;
878 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
879 param.retry_count = cmd.retry_count;
880 param.rnr_retry_count = cmd.rnr_retry_count;
881 param.max_cm_retries = cmd.max_cm_retries;
884 ctx = ib_ucm_ctx_get(file, cmd.id);
886 result = ib_send_cm_req(ctx->cm_id, ¶m);
889 result = PTR_ERR(ctx);
892 kfree(param.private_data);
893 kfree(param.primary_path);
894 kfree(param.alternate_path);
898 static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
899 const char __user *inbuf,
900 int in_len, int out_len)
902 struct ib_cm_rep_param param;
903 struct ib_ucm_context *ctx;
904 struct ib_ucm_rep cmd;
907 param.private_data = NULL;
909 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
912 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
916 param.qp_num = cmd.qpn;
917 param.starting_psn = cmd.psn;
918 param.private_data_len = cmd.len;
919 param.responder_resources = cmd.responder_resources;
920 param.initiator_depth = cmd.initiator_depth;
921 param.target_ack_delay = cmd.target_ack_delay;
922 param.failover_accepted = cmd.failover_accepted;
923 param.flow_control = cmd.flow_control;
924 param.rnr_retry_count = cmd.rnr_retry_count;
927 ctx = ib_ucm_ctx_get(file, cmd.id);
930 result = ib_send_cm_rep(ctx->cm_id, ¶m);
933 result = PTR_ERR(ctx);
935 kfree(param.private_data);
939 static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
940 const char __user *inbuf, int in_len,
941 int (*func)(struct ib_cm_id *cm_id,
942 const void *private_data,
943 u8 private_data_len))
945 struct ib_ucm_private_data cmd;
946 struct ib_ucm_context *ctx;
947 const void *private_data = NULL;
950 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
953 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
957 ctx = ib_ucm_ctx_get(file, cmd.id);
959 result = func(ctx->cm_id, private_data, cmd.len);
962 result = PTR_ERR(ctx);
968 static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
969 const char __user *inbuf,
970 int in_len, int out_len)
972 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
975 static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
976 const char __user *inbuf,
977 int in_len, int out_len)
979 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
982 static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
983 const char __user *inbuf,
984 int in_len, int out_len)
986 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
989 static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
990 const char __user *inbuf, int in_len,
991 int (*func)(struct ib_cm_id *cm_id,
998 struct ib_ucm_context *ctx;
999 struct ib_ucm_info cmd;
1000 const void *data = NULL;
1001 const void *info = NULL;
1004 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1007 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
1011 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
1015 ctx = ib_ucm_ctx_get(file, cmd.id);
1017 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
1018 data, cmd.data_len);
1019 ib_ucm_ctx_put(ctx);
1021 result = PTR_ERR(ctx);
1029 static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
1030 const char __user *inbuf,
1031 int in_len, int out_len)
1033 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
1036 static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
1037 const char __user *inbuf,
1038 int in_len, int out_len)
1040 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
1043 static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
1044 const char __user *inbuf,
1045 int in_len, int out_len)
1047 struct ib_ucm_context *ctx;
1048 struct ib_ucm_mra cmd;
1049 const void *data = NULL;
1052 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1055 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1059 ctx = ib_ucm_ctx_get(file, cmd.id);
1061 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
1062 ib_ucm_ctx_put(ctx);
1064 result = PTR_ERR(ctx);
1070 static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
1071 const char __user *inbuf,
1072 int in_len, int out_len)
1074 struct ib_ucm_context *ctx;
1075 struct ib_sa_path_rec *path = NULL;
1076 struct ib_ucm_lap cmd;
1077 const void *data = NULL;
1080 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1083 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1087 result = ib_ucm_path_get(&path, cmd.path);
1091 ctx = ib_ucm_ctx_get(file, cmd.id);
1093 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
1094 ib_ucm_ctx_put(ctx);
1096 result = PTR_ERR(ctx);
1104 static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1105 const char __user *inbuf,
1106 int in_len, int out_len)
1108 struct ib_cm_sidr_req_param param;
1109 struct ib_ucm_context *ctx;
1110 struct ib_ucm_sidr_req cmd;
1113 param.private_data = NULL;
1116 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1119 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
1123 result = ib_ucm_path_get(¶m.path, cmd.path);
1127 param.private_data_len = cmd.len;
1128 param.service_id = cmd.sid;
1129 param.timeout_ms = cmd.timeout;
1130 param.max_cm_retries = cmd.max_cm_retries;
1131 param.pkey = cmd.pkey;
1133 ctx = ib_ucm_ctx_get(file, cmd.id);
1135 result = ib_send_cm_sidr_req(ctx->cm_id, ¶m);
1136 ib_ucm_ctx_put(ctx);
1138 result = PTR_ERR(ctx);
1141 kfree(param.private_data);
1146 static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1147 const char __user *inbuf,
1148 int in_len, int out_len)
1150 struct ib_cm_sidr_rep_param param;
1151 struct ib_ucm_sidr_rep cmd;
1152 struct ib_ucm_context *ctx;
1157 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1160 result = ib_ucm_alloc_data(¶m.private_data,
1161 cmd.data, cmd.data_len);
1165 result = ib_ucm_alloc_data(¶m.info, cmd.info, cmd.info_len);
1169 param.qp_num = cmd.qpn;
1170 param.qkey = cmd.qkey;
1171 param.status = cmd.status;
1172 param.info_length = cmd.info_len;
1173 param.private_data_len = cmd.data_len;
1175 ctx = ib_ucm_ctx_get(file, cmd.id);
1177 result = ib_send_cm_sidr_rep(ctx->cm_id, ¶m);
1178 ib_ucm_ctx_put(ctx);
1180 result = PTR_ERR(ctx);
1183 kfree(param.private_data);
1188 static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1189 const char __user *inbuf,
1190 int in_len, int out_len) = {
1191 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1192 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1193 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1194 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
1195 [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
1196 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1197 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1198 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1199 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1200 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1201 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1202 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1203 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1204 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1205 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1206 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1207 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
1208 [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
1211 static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1212 size_t len, loff_t *pos)
1214 struct ib_ucm_file *file = filp->private_data;
1215 struct ib_ucm_cmd_hdr hdr;
1218 if (len < sizeof(hdr))
1221 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1224 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1227 if (hdr.in + sizeof(hdr) > len)
1230 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1238 static unsigned int ib_ucm_poll(struct file *filp,
1239 struct poll_table_struct *wait)
1241 struct ib_ucm_file *file = filp->private_data;
1242 unsigned int mask = 0;
1244 poll_wait(filp, &file->poll_wait, wait);
1246 if (!list_empty(&file->events))
1247 mask = POLLIN | POLLRDNORM;
1252 static int ib_ucm_open(struct inode *inode, struct file *filp)
1254 struct ib_ucm_file *file;
1256 file = kmalloc(sizeof(*file), GFP_KERNEL);
1260 INIT_LIST_HEAD(&file->events);
1261 INIT_LIST_HEAD(&file->ctxs);
1262 init_waitqueue_head(&file->poll_wait);
1264 init_MUTEX(&file->mutex);
1266 filp->private_data = file;
1268 file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev);
1273 static int ib_ucm_close(struct inode *inode, struct file *filp)
1275 struct ib_ucm_file *file = filp->private_data;
1276 struct ib_ucm_context *ctx;
1279 while (!list_empty(&file->ctxs)) {
1280 ctx = list_entry(file->ctxs.next,
1281 struct ib_ucm_context, file_list);
1284 mutex_lock(&ctx_id_mutex);
1285 idr_remove(&ctx_id_table, ctx->id);
1286 mutex_unlock(&ctx_id_mutex);
1288 ib_destroy_cm_id(ctx->cm_id);
1289 ib_ucm_cleanup_events(ctx);
1299 static void ib_ucm_release_class_dev(struct class_device *class_dev)
1301 struct ib_ucm_device *dev;
1303 dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1304 cdev_del(&dev->dev);
1305 clear_bit(dev->devnum, dev_map);
1309 static struct file_operations ucm_fops = {
1310 .owner = THIS_MODULE,
1311 .open = ib_ucm_open,
1312 .release = ib_ucm_close,
1313 .write = ib_ucm_write,
1314 .poll = ib_ucm_poll,
1317 static struct class ucm_class = {
1318 .name = "infiniband_cm",
1319 .release = ib_ucm_release_class_dev
1322 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1324 struct ib_ucm_device *dev;
1326 dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1327 return sprintf(buf, "%s\n", dev->ib_dev->name);
1329 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1331 static void ib_ucm_add_one(struct ib_device *device)
1333 struct ib_ucm_device *ucm_dev;
1335 if (!device->alloc_ucontext)
1338 ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
1342 ucm_dev->ib_dev = device;
1344 ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1345 if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
1348 set_bit(ucm_dev->devnum, dev_map);
1350 cdev_init(&ucm_dev->dev, &ucm_fops);
1351 ucm_dev->dev.owner = THIS_MODULE;
1352 kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
1353 if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
1356 ucm_dev->class_dev.class = &ucm_class;
1357 ucm_dev->class_dev.dev = device->dma_device;
1358 ucm_dev->class_dev.devt = ucm_dev->dev.dev;
1359 snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
1361 if (class_device_register(&ucm_dev->class_dev))
1364 if (class_device_create_file(&ucm_dev->class_dev,
1365 &class_device_attr_ibdev))
1368 ib_set_client_data(device, &ucm_client, ucm_dev);
1372 class_device_unregister(&ucm_dev->class_dev);
1374 cdev_del(&ucm_dev->dev);
1375 clear_bit(ucm_dev->devnum, dev_map);
1381 static void ib_ucm_remove_one(struct ib_device *device)
1383 struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
1388 class_device_unregister(&ucm_dev->class_dev);
1391 static ssize_t show_abi_version(struct class *class, char *buf)
1393 return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
1395 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1397 static int __init ib_ucm_init(void)
1401 ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1404 printk(KERN_ERR "ucm: couldn't register device number\n");
1408 ret = class_register(&ucm_class);
1410 printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
1414 ret = class_create_file(&ucm_class, &class_attr_abi_version);
1416 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
1420 ret = ib_register_client(&ucm_client);
1422 printk(KERN_ERR "ucm: couldn't register client\n");
1428 class_unregister(&ucm_class);
1430 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1435 static void __exit ib_ucm_cleanup(void)
1437 ib_unregister_client(&ucm_client);
1438 class_unregister(&ucm_class);
1439 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1440 idr_destroy(&ctx_id_table);
1443 module_init(ib_ucm_init);
1444 module_exit(ib_ucm_cleanup);