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>
46 #include <asm/uaccess.h>
48 #include <rdma/ib_cm.h>
49 #include <rdma/ib_user_cm.h>
51 MODULE_AUTHOR("Libor Michalek");
52 MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
53 MODULE_LICENSE("Dual BSD/GPL");
56 struct semaphore mutex;
59 struct list_head ctxs; /* list of active connections */
60 struct list_head events; /* list of pending events */
61 wait_queue_head_t poll_wait;
64 struct ib_ucm_context {
66 wait_queue_head_t wait;
70 struct ib_ucm_file *file;
71 struct ib_cm_id *cm_id;
74 struct list_head events; /* list of pending events. */
75 struct list_head file_list; /* member in file ctx list */
79 struct ib_ucm_context *ctx;
80 struct list_head file_list; /* member in file event list */
81 struct list_head ctx_list; /* member in ctx event list */
83 struct ib_cm_id *cm_id;
84 struct ib_ucm_event_resp resp;
96 #define IB_UCM_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_MINOR)
98 static struct semaphore ctx_id_mutex;
99 static struct idr ctx_id_table;
102 static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
104 struct ib_ucm_context *ctx;
107 ctx = idr_find(&ctx_id_table, id);
109 ctx = ERR_PTR(-ENOENT);
110 else if (ctx->file != file)
111 ctx = ERR_PTR(-EINVAL);
113 atomic_inc(&ctx->ref);
119 static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
121 if (atomic_dec_and_test(&ctx->ref))
125 static inline int ib_ucm_new_cm_id(int event)
127 return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
130 static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
132 struct ib_ucm_event *uevent;
134 down(&ctx->file->mutex);
135 list_del(&ctx->file_list);
136 while (!list_empty(&ctx->events)) {
138 uevent = list_entry(ctx->events.next,
139 struct ib_ucm_event, ctx_list);
140 list_del(&uevent->file_list);
141 list_del(&uevent->ctx_list);
143 /* clear incoming connections. */
144 if (ib_ucm_new_cm_id(uevent->resp.event))
145 ib_destroy_cm_id(uevent->cm_id);
149 up(&ctx->file->mutex);
152 static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
154 struct ib_ucm_context *ctx;
157 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
161 memset(ctx, 0, sizeof *ctx);
162 atomic_set(&ctx->ref, 1);
163 init_waitqueue_head(&ctx->wait);
165 INIT_LIST_HEAD(&ctx->events);
168 result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
173 result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
175 } while (result == -EAGAIN);
180 list_add_tail(&ctx->file_list, &file->ctxs);
188 * Event portion of the API, handle CM events
189 * and allow event polling.
191 static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
192 struct ib_sa_path_rec *kpath)
194 if (!kpath || !upath)
197 memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid);
198 memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid);
200 upath->dlid = kpath->dlid;
201 upath->slid = kpath->slid;
202 upath->raw_traffic = kpath->raw_traffic;
203 upath->flow_label = kpath->flow_label;
204 upath->hop_limit = kpath->hop_limit;
205 upath->traffic_class = kpath->traffic_class;
206 upath->reversible = kpath->reversible;
207 upath->numb_path = kpath->numb_path;
208 upath->pkey = kpath->pkey;
209 upath->sl = kpath->sl;
210 upath->mtu_selector = kpath->mtu_selector;
211 upath->mtu = kpath->mtu;
212 upath->rate_selector = kpath->rate_selector;
213 upath->rate = kpath->rate;
214 upath->packet_life_time = kpath->packet_life_time;
215 upath->preference = kpath->preference;
217 upath->packet_life_time_selector =
218 kpath->packet_life_time_selector;
221 static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
222 struct ib_cm_req_event_param *kreq)
224 ureq->remote_ca_guid = kreq->remote_ca_guid;
225 ureq->remote_qkey = kreq->remote_qkey;
226 ureq->remote_qpn = kreq->remote_qpn;
227 ureq->qp_type = kreq->qp_type;
228 ureq->starting_psn = kreq->starting_psn;
229 ureq->responder_resources = kreq->responder_resources;
230 ureq->initiator_depth = kreq->initiator_depth;
231 ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
232 ureq->flow_control = kreq->flow_control;
233 ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
234 ureq->retry_count = kreq->retry_count;
235 ureq->rnr_retry_count = kreq->rnr_retry_count;
236 ureq->srq = kreq->srq;
238 ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
239 ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
242 static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
243 struct ib_cm_rep_event_param *krep)
245 urep->remote_ca_guid = krep->remote_ca_guid;
246 urep->remote_qkey = krep->remote_qkey;
247 urep->remote_qpn = krep->remote_qpn;
248 urep->starting_psn = krep->starting_psn;
249 urep->responder_resources = krep->responder_resources;
250 urep->initiator_depth = krep->initiator_depth;
251 urep->target_ack_delay = krep->target_ack_delay;
252 urep->failover_accepted = krep->failover_accepted;
253 urep->flow_control = krep->flow_control;
254 urep->rnr_retry_count = krep->rnr_retry_count;
255 urep->srq = krep->srq;
258 static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
259 struct ib_cm_sidr_rep_event_param *krep)
261 urep->status = krep->status;
262 urep->qkey = krep->qkey;
263 urep->qpn = krep->qpn;
266 static int ib_ucm_event_process(struct ib_cm_event *evt,
267 struct ib_ucm_event *uvt)
271 switch (evt->event) {
272 case IB_CM_REQ_RECEIVED:
273 ib_ucm_event_req_get(&uvt->resp.u.req_resp,
274 &evt->param.req_rcvd);
275 uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
276 uvt->resp.present = IB_UCM_PRES_PRIMARY;
277 uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
278 IB_UCM_PRES_ALTERNATE : 0);
280 case IB_CM_REP_RECEIVED:
281 ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
282 &evt->param.rep_rcvd);
283 uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
285 case IB_CM_RTU_RECEIVED:
286 uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
287 uvt->resp.u.send_status = evt->param.send_status;
289 case IB_CM_DREQ_RECEIVED:
290 uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
291 uvt->resp.u.send_status = evt->param.send_status;
293 case IB_CM_DREP_RECEIVED:
294 uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
295 uvt->resp.u.send_status = evt->param.send_status;
297 case IB_CM_MRA_RECEIVED:
298 uvt->resp.u.mra_resp.timeout =
299 evt->param.mra_rcvd.service_timeout;
300 uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
302 case IB_CM_REJ_RECEIVED:
303 uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
304 uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
305 uvt->info_len = evt->param.rej_rcvd.ari_length;
306 info = evt->param.rej_rcvd.ari;
308 case IB_CM_LAP_RECEIVED:
309 ib_ucm_event_path_get(&uvt->resp.u.lap_resp.path,
310 evt->param.lap_rcvd.alternate_path);
311 uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
312 uvt->resp.present = IB_UCM_PRES_ALTERNATE;
314 case IB_CM_APR_RECEIVED:
315 uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
316 uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
317 uvt->info_len = evt->param.apr_rcvd.info_len;
318 info = evt->param.apr_rcvd.apr_info;
320 case IB_CM_SIDR_REQ_RECEIVED:
321 uvt->resp.u.sidr_req_resp.pkey =
322 evt->param.sidr_req_rcvd.pkey;
323 uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
325 case IB_CM_SIDR_REP_RECEIVED:
326 ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
327 &evt->param.sidr_rep_rcvd);
328 uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
329 uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
330 info = evt->param.sidr_rep_rcvd.info;
333 uvt->resp.u.send_status = evt->param.send_status;
338 uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
342 memcpy(uvt->data, evt->private_data, uvt->data_len);
343 uvt->resp.present |= IB_UCM_PRES_DATA;
347 uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
351 memcpy(uvt->info, info, uvt->info_len);
352 uvt->resp.present |= IB_UCM_PRES_INFO;
362 static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
363 struct ib_cm_event *event)
365 struct ib_ucm_event *uevent;
366 struct ib_ucm_context *ctx;
369 ctx = cm_id->context;
371 uevent = kmalloc(sizeof(*uevent), GFP_KERNEL);
375 memset(uevent, 0, sizeof(*uevent));
377 uevent->cm_id = cm_id;
378 uevent->resp.uid = ctx->uid;
379 uevent->resp.id = ctx->id;
380 uevent->resp.event = event->event;
382 result = ib_ucm_event_process(event, uevent);
386 down(&ctx->file->mutex);
387 list_add_tail(&uevent->file_list, &ctx->file->events);
388 list_add_tail(&uevent->ctx_list, &ctx->events);
389 wake_up_interruptible(&ctx->file->poll_wait);
390 up(&ctx->file->mutex);
396 /* Destroy new cm_id's */
397 return ib_ucm_new_cm_id(event->event);
400 static ssize_t ib_ucm_event(struct ib_ucm_file *file,
401 const char __user *inbuf,
402 int in_len, int out_len)
404 struct ib_ucm_context *ctx;
405 struct ib_ucm_event_get cmd;
406 struct ib_ucm_event *uevent;
410 if (out_len < sizeof(struct ib_ucm_event_resp))
413 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
419 while (list_empty(&file->events)) {
421 if (file->filp->f_flags & O_NONBLOCK) {
426 if (signal_pending(current)) {
427 result = -ERESTARTSYS;
431 prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
437 finish_wait(&file->poll_wait, &wait);
443 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
445 if (ib_ucm_new_cm_id(uevent->resp.event)) {
446 ctx = ib_ucm_ctx_alloc(file);
452 ctx->cm_id = uevent->cm_id;
453 ctx->cm_id->context = ctx;
454 uevent->resp.id = ctx->id;
457 if (copy_to_user((void __user *)(unsigned long)cmd.response,
458 &uevent->resp, sizeof(uevent->resp))) {
464 if (cmd.data_len < uevent->data_len) {
468 if (copy_to_user((void __user *)(unsigned long)cmd.data,
469 uevent->data, uevent->data_len)) {
476 if (cmd.info_len < uevent->info_len) {
480 if (copy_to_user((void __user *)(unsigned long)cmd.info,
481 uevent->info, uevent->info_len)) {
487 list_del(&uevent->file_list);
488 list_del(&uevent->ctx_list);
489 uevent->ctx->events_reported++;
500 static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
501 const char __user *inbuf,
502 int in_len, int out_len)
504 struct ib_ucm_create_id cmd;
505 struct ib_ucm_create_id_resp resp;
506 struct ib_ucm_context *ctx;
509 if (out_len < sizeof(resp))
512 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
516 ctx = ib_ucm_ctx_alloc(file);
522 ctx->cm_id = ib_create_cm_id(ib_ucm_event_handler, ctx);
523 if (IS_ERR(ctx->cm_id)) {
524 result = PTR_ERR(ctx->cm_id);
529 if (copy_to_user((void __user *)(unsigned long)cmd.response,
530 &resp, sizeof(resp))) {
539 idr_remove(&ctx_id_table, ctx->id);
542 if (!IS_ERR(ctx->cm_id))
543 ib_destroy_cm_id(ctx->cm_id);
549 static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
550 const char __user *inbuf,
551 int in_len, int out_len)
553 struct ib_ucm_destroy_id cmd;
554 struct ib_ucm_destroy_id_resp resp;
555 struct ib_ucm_context *ctx;
558 if (out_len < sizeof(resp))
561 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
565 ctx = idr_find(&ctx_id_table, cmd.id);
567 ctx = ERR_PTR(-ENOENT);
568 else if (ctx->file != file)
569 ctx = ERR_PTR(-EINVAL);
571 idr_remove(&ctx_id_table, ctx->id);
577 atomic_dec(&ctx->ref);
578 wait_event(ctx->wait, !atomic_read(&ctx->ref));
580 /* No new events will be generated after destroying the cm_id. */
581 ib_destroy_cm_id(ctx->cm_id);
582 /* Cleanup events not yet reported to the user. */
583 ib_ucm_cleanup_events(ctx);
585 resp.events_reported = ctx->events_reported;
586 if (copy_to_user((void __user *)(unsigned long)cmd.response,
587 &resp, sizeof(resp)))
594 static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
595 const char __user *inbuf,
596 int in_len, int out_len)
598 struct ib_ucm_attr_id_resp resp;
599 struct ib_ucm_attr_id cmd;
600 struct ib_ucm_context *ctx;
603 if (out_len < sizeof(resp))
606 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
609 ctx = ib_ucm_ctx_get(file, cmd.id);
613 resp.service_id = ctx->cm_id->service_id;
614 resp.service_mask = ctx->cm_id->service_mask;
615 resp.local_id = ctx->cm_id->local_id;
616 resp.remote_id = ctx->cm_id->remote_id;
618 if (copy_to_user((void __user *)(unsigned long)cmd.response,
619 &resp, sizeof(resp)))
626 static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr *dest_attr,
627 struct ib_ah_attr *src_attr)
629 memcpy(dest_attr->grh_dgid, src_attr->grh.dgid.raw,
630 sizeof src_attr->grh.dgid);
631 dest_attr->grh_flow_label = src_attr->grh.flow_label;
632 dest_attr->grh_sgid_index = src_attr->grh.sgid_index;
633 dest_attr->grh_hop_limit = src_attr->grh.hop_limit;
634 dest_attr->grh_traffic_class = src_attr->grh.traffic_class;
636 dest_attr->dlid = src_attr->dlid;
637 dest_attr->sl = src_attr->sl;
638 dest_attr->src_path_bits = src_attr->src_path_bits;
639 dest_attr->static_rate = src_attr->static_rate;
640 dest_attr->is_global = (src_attr->ah_flags & IB_AH_GRH);
641 dest_attr->port_num = src_attr->port_num;
644 static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp *dest_attr,
645 struct ib_qp_attr *src_attr)
647 dest_attr->cur_qp_state = src_attr->cur_qp_state;
648 dest_attr->path_mtu = src_attr->path_mtu;
649 dest_attr->path_mig_state = src_attr->path_mig_state;
650 dest_attr->qkey = src_attr->qkey;
651 dest_attr->rq_psn = src_attr->rq_psn;
652 dest_attr->sq_psn = src_attr->sq_psn;
653 dest_attr->dest_qp_num = src_attr->dest_qp_num;
654 dest_attr->qp_access_flags = src_attr->qp_access_flags;
656 dest_attr->max_send_wr = src_attr->cap.max_send_wr;
657 dest_attr->max_recv_wr = src_attr->cap.max_recv_wr;
658 dest_attr->max_send_sge = src_attr->cap.max_send_sge;
659 dest_attr->max_recv_sge = src_attr->cap.max_recv_sge;
660 dest_attr->max_inline_data = src_attr->cap.max_inline_data;
662 ib_ucm_copy_ah_attr(&dest_attr->ah_attr, &src_attr->ah_attr);
663 ib_ucm_copy_ah_attr(&dest_attr->alt_ah_attr, &src_attr->alt_ah_attr);
665 dest_attr->pkey_index = src_attr->pkey_index;
666 dest_attr->alt_pkey_index = src_attr->alt_pkey_index;
667 dest_attr->en_sqd_async_notify = src_attr->en_sqd_async_notify;
668 dest_attr->sq_draining = src_attr->sq_draining;
669 dest_attr->max_rd_atomic = src_attr->max_rd_atomic;
670 dest_attr->max_dest_rd_atomic = src_attr->max_dest_rd_atomic;
671 dest_attr->min_rnr_timer = src_attr->min_rnr_timer;
672 dest_attr->port_num = src_attr->port_num;
673 dest_attr->timeout = src_attr->timeout;
674 dest_attr->retry_cnt = src_attr->retry_cnt;
675 dest_attr->rnr_retry = src_attr->rnr_retry;
676 dest_attr->alt_port_num = src_attr->alt_port_num;
677 dest_attr->alt_timeout = src_attr->alt_timeout;
680 static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
681 const char __user *inbuf,
682 int in_len, int out_len)
684 struct ib_ucm_init_qp_attr_resp resp;
685 struct ib_ucm_init_qp_attr cmd;
686 struct ib_ucm_context *ctx;
687 struct ib_qp_attr qp_attr;
690 if (out_len < sizeof(resp))
693 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
696 ctx = ib_ucm_ctx_get(file, cmd.id);
700 resp.qp_attr_mask = 0;
701 memset(&qp_attr, 0, sizeof qp_attr);
702 qp_attr.qp_state = cmd.qp_state;
703 result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
707 ib_ucm_copy_qp_attr(&resp, &qp_attr);
709 if (copy_to_user((void __user *)(unsigned long)cmd.response,
710 &resp, sizeof(resp)))
718 static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
719 const char __user *inbuf,
720 int in_len, int out_len)
722 struct ib_ucm_listen cmd;
723 struct ib_ucm_context *ctx;
726 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
729 ctx = ib_ucm_ctx_get(file, cmd.id);
733 result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
738 static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
739 const char __user *inbuf,
740 int in_len, int out_len)
742 struct ib_ucm_establish cmd;
743 struct ib_ucm_context *ctx;
746 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
749 ctx = ib_ucm_ctx_get(file, cmd.id);
753 result = ib_cm_establish(ctx->cm_id);
758 static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
767 data = kmalloc(len, GFP_KERNEL);
771 if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
780 static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
782 struct ib_ucm_path_rec ucm_path;
783 struct ib_sa_path_rec *sa_path;
790 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
794 if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
801 memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid);
802 memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid);
804 sa_path->dlid = ucm_path.dlid;
805 sa_path->slid = ucm_path.slid;
806 sa_path->raw_traffic = ucm_path.raw_traffic;
807 sa_path->flow_label = ucm_path.flow_label;
808 sa_path->hop_limit = ucm_path.hop_limit;
809 sa_path->traffic_class = ucm_path.traffic_class;
810 sa_path->reversible = ucm_path.reversible;
811 sa_path->numb_path = ucm_path.numb_path;
812 sa_path->pkey = ucm_path.pkey;
813 sa_path->sl = ucm_path.sl;
814 sa_path->mtu_selector = ucm_path.mtu_selector;
815 sa_path->mtu = ucm_path.mtu;
816 sa_path->rate_selector = ucm_path.rate_selector;
817 sa_path->rate = ucm_path.rate;
818 sa_path->packet_life_time = ucm_path.packet_life_time;
819 sa_path->preference = ucm_path.preference;
821 sa_path->packet_life_time_selector =
822 ucm_path.packet_life_time_selector;
828 static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
829 const char __user *inbuf,
830 int in_len, int out_len)
832 struct ib_cm_req_param param;
833 struct ib_ucm_context *ctx;
834 struct ib_ucm_req cmd;
837 param.private_data = NULL;
838 param.primary_path = NULL;
839 param.alternate_path = NULL;
841 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
844 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
848 result = ib_ucm_path_get(¶m.primary_path, cmd.primary_path);
852 result = ib_ucm_path_get(¶m.alternate_path, cmd.alternate_path);
856 param.private_data_len = cmd.len;
857 param.service_id = cmd.sid;
858 param.qp_num = cmd.qpn;
859 param.qp_type = cmd.qp_type;
860 param.starting_psn = cmd.psn;
861 param.peer_to_peer = cmd.peer_to_peer;
862 param.responder_resources = cmd.responder_resources;
863 param.initiator_depth = cmd.initiator_depth;
864 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
865 param.flow_control = cmd.flow_control;
866 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
867 param.retry_count = cmd.retry_count;
868 param.rnr_retry_count = cmd.rnr_retry_count;
869 param.max_cm_retries = cmd.max_cm_retries;
872 ctx = ib_ucm_ctx_get(file, cmd.id);
874 result = ib_send_cm_req(ctx->cm_id, ¶m);
877 result = PTR_ERR(ctx);
880 kfree(param.private_data);
881 kfree(param.primary_path);
882 kfree(param.alternate_path);
886 static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
887 const char __user *inbuf,
888 int in_len, int out_len)
890 struct ib_cm_rep_param param;
891 struct ib_ucm_context *ctx;
892 struct ib_ucm_rep cmd;
895 param.private_data = NULL;
897 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
900 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
904 param.qp_num = cmd.qpn;
905 param.starting_psn = cmd.psn;
906 param.private_data_len = cmd.len;
907 param.responder_resources = cmd.responder_resources;
908 param.initiator_depth = cmd.initiator_depth;
909 param.target_ack_delay = cmd.target_ack_delay;
910 param.failover_accepted = cmd.failover_accepted;
911 param.flow_control = cmd.flow_control;
912 param.rnr_retry_count = cmd.rnr_retry_count;
915 ctx = ib_ucm_ctx_get(file, cmd.id);
918 result = ib_send_cm_rep(ctx->cm_id, ¶m);
921 result = PTR_ERR(ctx);
923 kfree(param.private_data);
927 static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
928 const char __user *inbuf, int in_len,
929 int (*func)(struct ib_cm_id *cm_id,
930 const void *private_data,
931 u8 private_data_len))
933 struct ib_ucm_private_data cmd;
934 struct ib_ucm_context *ctx;
935 const void *private_data = NULL;
938 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
941 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
945 ctx = ib_ucm_ctx_get(file, cmd.id);
947 result = func(ctx->cm_id, private_data, cmd.len);
950 result = PTR_ERR(ctx);
956 static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
957 const char __user *inbuf,
958 int in_len, int out_len)
960 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
963 static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
964 const char __user *inbuf,
965 int in_len, int out_len)
967 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
970 static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
971 const char __user *inbuf,
972 int in_len, int out_len)
974 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
977 static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
978 const char __user *inbuf, int in_len,
979 int (*func)(struct ib_cm_id *cm_id,
986 struct ib_ucm_context *ctx;
987 struct ib_ucm_info cmd;
988 const void *data = NULL;
989 const void *info = NULL;
992 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
995 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
999 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
1003 ctx = ib_ucm_ctx_get(file, cmd.id);
1005 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
1006 data, cmd.data_len);
1007 ib_ucm_ctx_put(ctx);
1009 result = PTR_ERR(ctx);
1017 static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
1018 const char __user *inbuf,
1019 int in_len, int out_len)
1021 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
1024 static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
1025 const char __user *inbuf,
1026 int in_len, int out_len)
1028 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
1031 static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
1032 const char __user *inbuf,
1033 int in_len, int out_len)
1035 struct ib_ucm_context *ctx;
1036 struct ib_ucm_mra cmd;
1037 const void *data = NULL;
1040 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1043 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1047 ctx = ib_ucm_ctx_get(file, cmd.id);
1049 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
1050 ib_ucm_ctx_put(ctx);
1052 result = PTR_ERR(ctx);
1058 static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
1059 const char __user *inbuf,
1060 int in_len, int out_len)
1062 struct ib_ucm_context *ctx;
1063 struct ib_sa_path_rec *path = NULL;
1064 struct ib_ucm_lap cmd;
1065 const void *data = NULL;
1068 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1071 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1075 result = ib_ucm_path_get(&path, cmd.path);
1079 ctx = ib_ucm_ctx_get(file, cmd.id);
1081 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
1082 ib_ucm_ctx_put(ctx);
1084 result = PTR_ERR(ctx);
1092 static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1093 const char __user *inbuf,
1094 int in_len, int out_len)
1096 struct ib_cm_sidr_req_param param;
1097 struct ib_ucm_context *ctx;
1098 struct ib_ucm_sidr_req cmd;
1101 param.private_data = NULL;
1104 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1107 result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len);
1111 result = ib_ucm_path_get(¶m.path, cmd.path);
1115 param.private_data_len = cmd.len;
1116 param.service_id = cmd.sid;
1117 param.timeout_ms = cmd.timeout;
1118 param.max_cm_retries = cmd.max_cm_retries;
1119 param.pkey = cmd.pkey;
1121 ctx = ib_ucm_ctx_get(file, cmd.id);
1123 result = ib_send_cm_sidr_req(ctx->cm_id, ¶m);
1124 ib_ucm_ctx_put(ctx);
1126 result = PTR_ERR(ctx);
1129 kfree(param.private_data);
1134 static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1135 const char __user *inbuf,
1136 int in_len, int out_len)
1138 struct ib_cm_sidr_rep_param param;
1139 struct ib_ucm_sidr_rep cmd;
1140 struct ib_ucm_context *ctx;
1145 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1148 result = ib_ucm_alloc_data(¶m.private_data,
1149 cmd.data, cmd.data_len);
1153 result = ib_ucm_alloc_data(¶m.info, cmd.info, cmd.info_len);
1157 param.qp_num = cmd.qpn;
1158 param.qkey = cmd.qkey;
1159 param.status = cmd.status;
1160 param.info_length = cmd.info_len;
1161 param.private_data_len = cmd.data_len;
1163 ctx = ib_ucm_ctx_get(file, cmd.id);
1165 result = ib_send_cm_sidr_rep(ctx->cm_id, ¶m);
1166 ib_ucm_ctx_put(ctx);
1168 result = PTR_ERR(ctx);
1171 kfree(param.private_data);
1176 static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1177 const char __user *inbuf,
1178 int in_len, int out_len) = {
1179 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1180 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1181 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1182 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
1183 [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
1184 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1185 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1186 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1187 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1188 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1189 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1190 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1191 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1192 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1193 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1194 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1195 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
1196 [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
1199 static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1200 size_t len, loff_t *pos)
1202 struct ib_ucm_file *file = filp->private_data;
1203 struct ib_ucm_cmd_hdr hdr;
1206 if (len < sizeof(hdr))
1209 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1212 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1215 if (hdr.in + sizeof(hdr) > len)
1218 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1226 static unsigned int ib_ucm_poll(struct file *filp,
1227 struct poll_table_struct *wait)
1229 struct ib_ucm_file *file = filp->private_data;
1230 unsigned int mask = 0;
1232 poll_wait(filp, &file->poll_wait, wait);
1234 if (!list_empty(&file->events))
1235 mask = POLLIN | POLLRDNORM;
1240 static int ib_ucm_open(struct inode *inode, struct file *filp)
1242 struct ib_ucm_file *file;
1244 file = kmalloc(sizeof(*file), GFP_KERNEL);
1248 INIT_LIST_HEAD(&file->events);
1249 INIT_LIST_HEAD(&file->ctxs);
1250 init_waitqueue_head(&file->poll_wait);
1252 init_MUTEX(&file->mutex);
1254 filp->private_data = file;
1260 static int ib_ucm_close(struct inode *inode, struct file *filp)
1262 struct ib_ucm_file *file = filp->private_data;
1263 struct ib_ucm_context *ctx;
1266 while (!list_empty(&file->ctxs)) {
1267 ctx = list_entry(file->ctxs.next,
1268 struct ib_ucm_context, file_list);
1271 down(&ctx_id_mutex);
1272 idr_remove(&ctx_id_table, ctx->id);
1275 ib_destroy_cm_id(ctx->cm_id);
1276 ib_ucm_cleanup_events(ctx);
1286 static struct file_operations ib_ucm_fops = {
1287 .owner = THIS_MODULE,
1288 .open = ib_ucm_open,
1289 .release = ib_ucm_close,
1290 .write = ib_ucm_write,
1291 .poll = ib_ucm_poll,
1295 static struct class *ib_ucm_class;
1296 static struct cdev ib_ucm_cdev;
1298 static int __init ib_ucm_init(void)
1302 result = register_chrdev_region(IB_UCM_DEV, 1, "infiniband_cm");
1304 printk(KERN_ERR "ucm: Error <%d> registering dev\n", result);
1308 cdev_init(&ib_ucm_cdev, &ib_ucm_fops);
1310 result = cdev_add(&ib_ucm_cdev, IB_UCM_DEV, 1);
1312 printk(KERN_ERR "ucm: Error <%d> adding cdev\n", result);
1316 ib_ucm_class = class_create(THIS_MODULE, "infiniband_cm");
1317 if (IS_ERR(ib_ucm_class)) {
1318 result = PTR_ERR(ib_ucm_class);
1319 printk(KERN_ERR "Error <%d> creating class\n", result);
1323 class_device_create(ib_ucm_class, IB_UCM_DEV, NULL, "ucm");
1325 idr_init(&ctx_id_table);
1326 init_MUTEX(&ctx_id_mutex);
1330 cdev_del(&ib_ucm_cdev);
1332 unregister_chrdev_region(IB_UCM_DEV, 1);
1337 static void __exit ib_ucm_cleanup(void)
1339 class_device_destroy(ib_ucm_class, IB_UCM_DEV);
1340 class_destroy(ib_ucm_class);
1341 cdev_del(&ib_ucm_cdev);
1342 unregister_chrdev_region(IB_UCM_DEV, 1);
1345 module_init(ib_ucm_init);
1346 module_exit(ib_ucm_cleanup);