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");
55 struct ib_ucm_device
{
58 struct class_device class_dev
;
59 struct ib_device
*ib_dev
;
63 struct semaphore mutex
;
65 struct ib_ucm_device
*device
;
67 struct list_head ctxs
;
68 struct list_head events
;
69 wait_queue_head_t poll_wait
;
72 struct ib_ucm_context
{
74 wait_queue_head_t wait
;
78 struct ib_ucm_file
*file
;
79 struct ib_cm_id
*cm_id
;
82 struct list_head events
; /* list of pending events. */
83 struct list_head file_list
; /* member in file ctx list */
87 struct ib_ucm_context
*ctx
;
88 struct list_head file_list
; /* member in file event list */
89 struct list_head ctx_list
; /* member in ctx event list */
91 struct ib_cm_id
*cm_id
;
92 struct ib_ucm_event_resp resp
;
101 IB_UCM_BASE_MINOR
= 224,
102 IB_UCM_MAX_DEVICES
= 32
105 #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
107 static void ib_ucm_add_one(struct ib_device
*device
);
108 static void ib_ucm_remove_one(struct ib_device
*device
);
110 static struct ib_client ucm_client
= {
112 .add
= ib_ucm_add_one
,
113 .remove
= ib_ucm_remove_one
116 static DECLARE_MUTEX(ctx_id_mutex
);
117 static DEFINE_IDR(ctx_id_table
);
118 static DECLARE_BITMAP(dev_map
, IB_UCM_MAX_DEVICES
);
120 static struct ib_ucm_context
*ib_ucm_ctx_get(struct ib_ucm_file
*file
, int id
)
122 struct ib_ucm_context
*ctx
;
125 ctx
= idr_find(&ctx_id_table
, id
);
127 ctx
= ERR_PTR(-ENOENT
);
128 else if (ctx
->file
!= file
)
129 ctx
= ERR_PTR(-EINVAL
);
131 atomic_inc(&ctx
->ref
);
137 static void ib_ucm_ctx_put(struct ib_ucm_context
*ctx
)
139 if (atomic_dec_and_test(&ctx
->ref
))
143 static inline int ib_ucm_new_cm_id(int event
)
145 return event
== IB_CM_REQ_RECEIVED
|| event
== IB_CM_SIDR_REQ_RECEIVED
;
148 static void ib_ucm_cleanup_events(struct ib_ucm_context
*ctx
)
150 struct ib_ucm_event
*uevent
;
152 down(&ctx
->file
->mutex
);
153 list_del(&ctx
->file_list
);
154 while (!list_empty(&ctx
->events
)) {
156 uevent
= list_entry(ctx
->events
.next
,
157 struct ib_ucm_event
, ctx_list
);
158 list_del(&uevent
->file_list
);
159 list_del(&uevent
->ctx_list
);
161 /* clear incoming connections. */
162 if (ib_ucm_new_cm_id(uevent
->resp
.event
))
163 ib_destroy_cm_id(uevent
->cm_id
);
167 up(&ctx
->file
->mutex
);
170 static struct ib_ucm_context
*ib_ucm_ctx_alloc(struct ib_ucm_file
*file
)
172 struct ib_ucm_context
*ctx
;
175 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
179 memset(ctx
, 0, sizeof *ctx
);
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
);
191 result
= idr_get_new(&ctx_id_table
, ctx
, &ctx
->id
);
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
= kmalloc(sizeof(*uevent
), GFP_KERNEL
);
393 memset(uevent
, 0, sizeof(*uevent
));
395 uevent
->cm_id
= cm_id
;
396 uevent
->resp
.uid
= ctx
->uid
;
397 uevent
->resp
.id
= ctx
->id
;
398 uevent
->resp
.event
= event
->event
;
400 result
= ib_ucm_event_process(event
, uevent
);
404 down(&ctx
->file
->mutex
);
405 list_add_tail(&uevent
->file_list
, &ctx
->file
->events
);
406 list_add_tail(&uevent
->ctx_list
, &ctx
->events
);
407 wake_up_interruptible(&ctx
->file
->poll_wait
);
408 up(&ctx
->file
->mutex
);
414 /* Destroy new cm_id's */
415 return ib_ucm_new_cm_id(event
->event
);
418 static ssize_t
ib_ucm_event(struct ib_ucm_file
*file
,
419 const char __user
*inbuf
,
420 int in_len
, int out_len
)
422 struct ib_ucm_context
*ctx
;
423 struct ib_ucm_event_get cmd
;
424 struct ib_ucm_event
*uevent
;
428 if (out_len
< sizeof(struct ib_ucm_event_resp
))
431 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
435 while (list_empty(&file
->events
)) {
437 if (file
->filp
->f_flags
& O_NONBLOCK
) {
442 if (signal_pending(current
)) {
443 result
= -ERESTARTSYS
;
447 prepare_to_wait(&file
->poll_wait
, &wait
, TASK_INTERRUPTIBLE
);
453 finish_wait(&file
->poll_wait
, &wait
);
459 uevent
= list_entry(file
->events
.next
, struct ib_ucm_event
, file_list
);
461 if (ib_ucm_new_cm_id(uevent
->resp
.event
)) {
462 ctx
= ib_ucm_ctx_alloc(file
);
468 ctx
->cm_id
= uevent
->cm_id
;
469 ctx
->cm_id
->context
= ctx
;
470 uevent
->resp
.id
= ctx
->id
;
473 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
474 &uevent
->resp
, sizeof(uevent
->resp
))) {
480 if (cmd
.data_len
< uevent
->data_len
) {
484 if (copy_to_user((void __user
*)(unsigned long)cmd
.data
,
485 uevent
->data
, uevent
->data_len
)) {
492 if (cmd
.info_len
< uevent
->info_len
) {
496 if (copy_to_user((void __user
*)(unsigned long)cmd
.info
,
497 uevent
->info
, uevent
->info_len
)) {
503 list_del(&uevent
->file_list
);
504 list_del(&uevent
->ctx_list
);
505 uevent
->ctx
->events_reported
++;
515 static ssize_t
ib_ucm_create_id(struct ib_ucm_file
*file
,
516 const char __user
*inbuf
,
517 int in_len
, int out_len
)
519 struct ib_ucm_create_id cmd
;
520 struct ib_ucm_create_id_resp resp
;
521 struct ib_ucm_context
*ctx
;
524 if (out_len
< sizeof(resp
))
527 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
531 ctx
= ib_ucm_ctx_alloc(file
);
537 ctx
->cm_id
= ib_create_cm_id(file
->device
->ib_dev
,
538 ib_ucm_event_handler
, ctx
);
539 if (IS_ERR(ctx
->cm_id
)) {
540 result
= PTR_ERR(ctx
->cm_id
);
545 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
546 &resp
, sizeof(resp
))) {
553 ib_destroy_cm_id(ctx
->cm_id
);
556 idr_remove(&ctx_id_table
, ctx
->id
);
562 static ssize_t
ib_ucm_destroy_id(struct ib_ucm_file
*file
,
563 const char __user
*inbuf
,
564 int in_len
, int out_len
)
566 struct ib_ucm_destroy_id cmd
;
567 struct ib_ucm_destroy_id_resp resp
;
568 struct ib_ucm_context
*ctx
;
571 if (out_len
< sizeof(resp
))
574 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
578 ctx
= idr_find(&ctx_id_table
, cmd
.id
);
580 ctx
= ERR_PTR(-ENOENT
);
581 else if (ctx
->file
!= file
)
582 ctx
= ERR_PTR(-EINVAL
);
584 idr_remove(&ctx_id_table
, ctx
->id
);
590 atomic_dec(&ctx
->ref
);
591 wait_event(ctx
->wait
, !atomic_read(&ctx
->ref
));
593 /* No new events will be generated after destroying the cm_id. */
594 ib_destroy_cm_id(ctx
->cm_id
);
595 /* Cleanup events not yet reported to the user. */
596 ib_ucm_cleanup_events(ctx
);
598 resp
.events_reported
= ctx
->events_reported
;
599 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
600 &resp
, sizeof(resp
)))
607 static ssize_t
ib_ucm_attr_id(struct ib_ucm_file
*file
,
608 const char __user
*inbuf
,
609 int in_len
, int out_len
)
611 struct ib_ucm_attr_id_resp resp
;
612 struct ib_ucm_attr_id cmd
;
613 struct ib_ucm_context
*ctx
;
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
.service_id
= ctx
->cm_id
->service_id
;
627 resp
.service_mask
= ctx
->cm_id
->service_mask
;
628 resp
.local_id
= ctx
->cm_id
->local_id
;
629 resp
.remote_id
= ctx
->cm_id
->remote_id
;
631 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
632 &resp
, sizeof(resp
)))
639 static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr
*dest_attr
,
640 struct ib_ah_attr
*src_attr
)
642 memcpy(dest_attr
->grh_dgid
, src_attr
->grh
.dgid
.raw
,
643 sizeof src_attr
->grh
.dgid
);
644 dest_attr
->grh_flow_label
= src_attr
->grh
.flow_label
;
645 dest_attr
->grh_sgid_index
= src_attr
->grh
.sgid_index
;
646 dest_attr
->grh_hop_limit
= src_attr
->grh
.hop_limit
;
647 dest_attr
->grh_traffic_class
= src_attr
->grh
.traffic_class
;
649 dest_attr
->dlid
= src_attr
->dlid
;
650 dest_attr
->sl
= src_attr
->sl
;
651 dest_attr
->src_path_bits
= src_attr
->src_path_bits
;
652 dest_attr
->static_rate
= src_attr
->static_rate
;
653 dest_attr
->is_global
= (src_attr
->ah_flags
& IB_AH_GRH
);
654 dest_attr
->port_num
= src_attr
->port_num
;
657 static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp
*dest_attr
,
658 struct ib_qp_attr
*src_attr
)
660 dest_attr
->cur_qp_state
= src_attr
->cur_qp_state
;
661 dest_attr
->path_mtu
= src_attr
->path_mtu
;
662 dest_attr
->path_mig_state
= src_attr
->path_mig_state
;
663 dest_attr
->qkey
= src_attr
->qkey
;
664 dest_attr
->rq_psn
= src_attr
->rq_psn
;
665 dest_attr
->sq_psn
= src_attr
->sq_psn
;
666 dest_attr
->dest_qp_num
= src_attr
->dest_qp_num
;
667 dest_attr
->qp_access_flags
= src_attr
->qp_access_flags
;
669 dest_attr
->max_send_wr
= src_attr
->cap
.max_send_wr
;
670 dest_attr
->max_recv_wr
= src_attr
->cap
.max_recv_wr
;
671 dest_attr
->max_send_sge
= src_attr
->cap
.max_send_sge
;
672 dest_attr
->max_recv_sge
= src_attr
->cap
.max_recv_sge
;
673 dest_attr
->max_inline_data
= src_attr
->cap
.max_inline_data
;
675 ib_ucm_copy_ah_attr(&dest_attr
->ah_attr
, &src_attr
->ah_attr
);
676 ib_ucm_copy_ah_attr(&dest_attr
->alt_ah_attr
, &src_attr
->alt_ah_attr
);
678 dest_attr
->pkey_index
= src_attr
->pkey_index
;
679 dest_attr
->alt_pkey_index
= src_attr
->alt_pkey_index
;
680 dest_attr
->en_sqd_async_notify
= src_attr
->en_sqd_async_notify
;
681 dest_attr
->sq_draining
= src_attr
->sq_draining
;
682 dest_attr
->max_rd_atomic
= src_attr
->max_rd_atomic
;
683 dest_attr
->max_dest_rd_atomic
= src_attr
->max_dest_rd_atomic
;
684 dest_attr
->min_rnr_timer
= src_attr
->min_rnr_timer
;
685 dest_attr
->port_num
= src_attr
->port_num
;
686 dest_attr
->timeout
= src_attr
->timeout
;
687 dest_attr
->retry_cnt
= src_attr
->retry_cnt
;
688 dest_attr
->rnr_retry
= src_attr
->rnr_retry
;
689 dest_attr
->alt_port_num
= src_attr
->alt_port_num
;
690 dest_attr
->alt_timeout
= src_attr
->alt_timeout
;
693 static ssize_t
ib_ucm_init_qp_attr(struct ib_ucm_file
*file
,
694 const char __user
*inbuf
,
695 int in_len
, int out_len
)
697 struct ib_ucm_init_qp_attr_resp resp
;
698 struct ib_ucm_init_qp_attr cmd
;
699 struct ib_ucm_context
*ctx
;
700 struct ib_qp_attr qp_attr
;
703 if (out_len
< sizeof(resp
))
706 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
709 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
713 resp
.qp_attr_mask
= 0;
714 memset(&qp_attr
, 0, sizeof qp_attr
);
715 qp_attr
.qp_state
= cmd
.qp_state
;
716 result
= ib_cm_init_qp_attr(ctx
->cm_id
, &qp_attr
, &resp
.qp_attr_mask
);
720 ib_ucm_copy_qp_attr(&resp
, &qp_attr
);
722 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
723 &resp
, sizeof(resp
)))
731 static ssize_t
ib_ucm_listen(struct ib_ucm_file
*file
,
732 const char __user
*inbuf
,
733 int in_len
, int out_len
)
735 struct ib_ucm_listen cmd
;
736 struct ib_ucm_context
*ctx
;
739 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
742 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
746 result
= ib_cm_listen(ctx
->cm_id
, cmd
.service_id
, cmd
.service_mask
);
751 static ssize_t
ib_ucm_establish(struct ib_ucm_file
*file
,
752 const char __user
*inbuf
,
753 int in_len
, int out_len
)
755 struct ib_ucm_establish cmd
;
756 struct ib_ucm_context
*ctx
;
759 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
762 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
766 result
= ib_cm_establish(ctx
->cm_id
);
771 static int ib_ucm_alloc_data(const void **dest
, u64 src
, u32 len
)
780 data
= kmalloc(len
, GFP_KERNEL
);
784 if (copy_from_user(data
, (void __user
*)(unsigned long)src
, len
)) {
793 static int ib_ucm_path_get(struct ib_sa_path_rec
**path
, u64 src
)
795 struct ib_ucm_path_rec ucm_path
;
796 struct ib_sa_path_rec
*sa_path
;
803 sa_path
= kmalloc(sizeof(*sa_path
), GFP_KERNEL
);
807 if (copy_from_user(&ucm_path
, (void __user
*)(unsigned long)src
,
814 memcpy(sa_path
->dgid
.raw
, ucm_path
.dgid
, sizeof sa_path
->dgid
);
815 memcpy(sa_path
->sgid
.raw
, ucm_path
.sgid
, sizeof sa_path
->sgid
);
817 sa_path
->dlid
= ucm_path
.dlid
;
818 sa_path
->slid
= ucm_path
.slid
;
819 sa_path
->raw_traffic
= ucm_path
.raw_traffic
;
820 sa_path
->flow_label
= ucm_path
.flow_label
;
821 sa_path
->hop_limit
= ucm_path
.hop_limit
;
822 sa_path
->traffic_class
= ucm_path
.traffic_class
;
823 sa_path
->reversible
= ucm_path
.reversible
;
824 sa_path
->numb_path
= ucm_path
.numb_path
;
825 sa_path
->pkey
= ucm_path
.pkey
;
826 sa_path
->sl
= ucm_path
.sl
;
827 sa_path
->mtu_selector
= ucm_path
.mtu_selector
;
828 sa_path
->mtu
= ucm_path
.mtu
;
829 sa_path
->rate_selector
= ucm_path
.rate_selector
;
830 sa_path
->rate
= ucm_path
.rate
;
831 sa_path
->packet_life_time
= ucm_path
.packet_life_time
;
832 sa_path
->preference
= ucm_path
.preference
;
834 sa_path
->packet_life_time_selector
=
835 ucm_path
.packet_life_time_selector
;
841 static ssize_t
ib_ucm_send_req(struct ib_ucm_file
*file
,
842 const char __user
*inbuf
,
843 int in_len
, int out_len
)
845 struct ib_cm_req_param param
;
846 struct ib_ucm_context
*ctx
;
847 struct ib_ucm_req cmd
;
850 param
.private_data
= NULL
;
851 param
.primary_path
= NULL
;
852 param
.alternate_path
= NULL
;
854 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
857 result
= ib_ucm_alloc_data(¶m
.private_data
, cmd
.data
, cmd
.len
);
861 result
= ib_ucm_path_get(¶m
.primary_path
, cmd
.primary_path
);
865 result
= ib_ucm_path_get(¶m
.alternate_path
, cmd
.alternate_path
);
869 param
.private_data_len
= cmd
.len
;
870 param
.service_id
= cmd
.sid
;
871 param
.qp_num
= cmd
.qpn
;
872 param
.qp_type
= cmd
.qp_type
;
873 param
.starting_psn
= cmd
.psn
;
874 param
.peer_to_peer
= cmd
.peer_to_peer
;
875 param
.responder_resources
= cmd
.responder_resources
;
876 param
.initiator_depth
= cmd
.initiator_depth
;
877 param
.remote_cm_response_timeout
= cmd
.remote_cm_response_timeout
;
878 param
.flow_control
= cmd
.flow_control
;
879 param
.local_cm_response_timeout
= cmd
.local_cm_response_timeout
;
880 param
.retry_count
= cmd
.retry_count
;
881 param
.rnr_retry_count
= cmd
.rnr_retry_count
;
882 param
.max_cm_retries
= cmd
.max_cm_retries
;
885 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
887 result
= ib_send_cm_req(ctx
->cm_id
, ¶m
);
890 result
= PTR_ERR(ctx
);
893 kfree(param
.private_data
);
894 kfree(param
.primary_path
);
895 kfree(param
.alternate_path
);
899 static ssize_t
ib_ucm_send_rep(struct ib_ucm_file
*file
,
900 const char __user
*inbuf
,
901 int in_len
, int out_len
)
903 struct ib_cm_rep_param param
;
904 struct ib_ucm_context
*ctx
;
905 struct ib_ucm_rep cmd
;
908 param
.private_data
= NULL
;
910 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
913 result
= ib_ucm_alloc_data(¶m
.private_data
, cmd
.data
, cmd
.len
);
917 param
.qp_num
= cmd
.qpn
;
918 param
.starting_psn
= cmd
.psn
;
919 param
.private_data_len
= cmd
.len
;
920 param
.responder_resources
= cmd
.responder_resources
;
921 param
.initiator_depth
= cmd
.initiator_depth
;
922 param
.target_ack_delay
= cmd
.target_ack_delay
;
923 param
.failover_accepted
= cmd
.failover_accepted
;
924 param
.flow_control
= cmd
.flow_control
;
925 param
.rnr_retry_count
= cmd
.rnr_retry_count
;
928 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
931 result
= ib_send_cm_rep(ctx
->cm_id
, ¶m
);
934 result
= PTR_ERR(ctx
);
936 kfree(param
.private_data
);
940 static ssize_t
ib_ucm_send_private_data(struct ib_ucm_file
*file
,
941 const char __user
*inbuf
, int in_len
,
942 int (*func
)(struct ib_cm_id
*cm_id
,
943 const void *private_data
,
944 u8 private_data_len
))
946 struct ib_ucm_private_data cmd
;
947 struct ib_ucm_context
*ctx
;
948 const void *private_data
= NULL
;
951 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
954 result
= ib_ucm_alloc_data(&private_data
, cmd
.data
, cmd
.len
);
958 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
960 result
= func(ctx
->cm_id
, private_data
, cmd
.len
);
963 result
= PTR_ERR(ctx
);
969 static ssize_t
ib_ucm_send_rtu(struct ib_ucm_file
*file
,
970 const char __user
*inbuf
,
971 int in_len
, int out_len
)
973 return ib_ucm_send_private_data(file
, inbuf
, in_len
, ib_send_cm_rtu
);
976 static ssize_t
ib_ucm_send_dreq(struct ib_ucm_file
*file
,
977 const char __user
*inbuf
,
978 int in_len
, int out_len
)
980 return ib_ucm_send_private_data(file
, inbuf
, in_len
, ib_send_cm_dreq
);
983 static ssize_t
ib_ucm_send_drep(struct ib_ucm_file
*file
,
984 const char __user
*inbuf
,
985 int in_len
, int out_len
)
987 return ib_ucm_send_private_data(file
, inbuf
, in_len
, ib_send_cm_drep
);
990 static ssize_t
ib_ucm_send_info(struct ib_ucm_file
*file
,
991 const char __user
*inbuf
, int in_len
,
992 int (*func
)(struct ib_cm_id
*cm_id
,
999 struct ib_ucm_context
*ctx
;
1000 struct ib_ucm_info cmd
;
1001 const void *data
= NULL
;
1002 const void *info
= NULL
;
1005 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1008 result
= ib_ucm_alloc_data(&data
, cmd
.data
, cmd
.data_len
);
1012 result
= ib_ucm_alloc_data(&info
, cmd
.info
, cmd
.info_len
);
1016 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1018 result
= func(ctx
->cm_id
, cmd
.status
, info
, cmd
.info_len
,
1019 data
, cmd
.data_len
);
1020 ib_ucm_ctx_put(ctx
);
1022 result
= PTR_ERR(ctx
);
1030 static ssize_t
ib_ucm_send_rej(struct ib_ucm_file
*file
,
1031 const char __user
*inbuf
,
1032 int in_len
, int out_len
)
1034 return ib_ucm_send_info(file
, inbuf
, in_len
, (void *)ib_send_cm_rej
);
1037 static ssize_t
ib_ucm_send_apr(struct ib_ucm_file
*file
,
1038 const char __user
*inbuf
,
1039 int in_len
, int out_len
)
1041 return ib_ucm_send_info(file
, inbuf
, in_len
, (void *)ib_send_cm_apr
);
1044 static ssize_t
ib_ucm_send_mra(struct ib_ucm_file
*file
,
1045 const char __user
*inbuf
,
1046 int in_len
, int out_len
)
1048 struct ib_ucm_context
*ctx
;
1049 struct ib_ucm_mra cmd
;
1050 const void *data
= NULL
;
1053 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1056 result
= ib_ucm_alloc_data(&data
, cmd
.data
, cmd
.len
);
1060 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1062 result
= ib_send_cm_mra(ctx
->cm_id
, cmd
.timeout
, data
, cmd
.len
);
1063 ib_ucm_ctx_put(ctx
);
1065 result
= PTR_ERR(ctx
);
1071 static ssize_t
ib_ucm_send_lap(struct ib_ucm_file
*file
,
1072 const char __user
*inbuf
,
1073 int in_len
, int out_len
)
1075 struct ib_ucm_context
*ctx
;
1076 struct ib_sa_path_rec
*path
= NULL
;
1077 struct ib_ucm_lap cmd
;
1078 const void *data
= NULL
;
1081 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1084 result
= ib_ucm_alloc_data(&data
, cmd
.data
, cmd
.len
);
1088 result
= ib_ucm_path_get(&path
, cmd
.path
);
1092 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1094 result
= ib_send_cm_lap(ctx
->cm_id
, path
, data
, cmd
.len
);
1095 ib_ucm_ctx_put(ctx
);
1097 result
= PTR_ERR(ctx
);
1105 static ssize_t
ib_ucm_send_sidr_req(struct ib_ucm_file
*file
,
1106 const char __user
*inbuf
,
1107 int in_len
, int out_len
)
1109 struct ib_cm_sidr_req_param param
;
1110 struct ib_ucm_context
*ctx
;
1111 struct ib_ucm_sidr_req cmd
;
1114 param
.private_data
= NULL
;
1117 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1120 result
= ib_ucm_alloc_data(¶m
.private_data
, cmd
.data
, cmd
.len
);
1124 result
= ib_ucm_path_get(¶m
.path
, cmd
.path
);
1128 param
.private_data_len
= cmd
.len
;
1129 param
.service_id
= cmd
.sid
;
1130 param
.timeout_ms
= cmd
.timeout
;
1131 param
.max_cm_retries
= cmd
.max_cm_retries
;
1132 param
.pkey
= cmd
.pkey
;
1134 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1136 result
= ib_send_cm_sidr_req(ctx
->cm_id
, ¶m
);
1137 ib_ucm_ctx_put(ctx
);
1139 result
= PTR_ERR(ctx
);
1142 kfree(param
.private_data
);
1147 static ssize_t
ib_ucm_send_sidr_rep(struct ib_ucm_file
*file
,
1148 const char __user
*inbuf
,
1149 int in_len
, int out_len
)
1151 struct ib_cm_sidr_rep_param param
;
1152 struct ib_ucm_sidr_rep cmd
;
1153 struct ib_ucm_context
*ctx
;
1158 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1161 result
= ib_ucm_alloc_data(¶m
.private_data
,
1162 cmd
.data
, cmd
.data_len
);
1166 result
= ib_ucm_alloc_data(¶m
.info
, cmd
.info
, cmd
.info_len
);
1170 param
.qp_num
= cmd
.qpn
;
1171 param
.qkey
= cmd
.qkey
;
1172 param
.status
= cmd
.status
;
1173 param
.info_length
= cmd
.info_len
;
1174 param
.private_data_len
= cmd
.data_len
;
1176 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1178 result
= ib_send_cm_sidr_rep(ctx
->cm_id
, ¶m
);
1179 ib_ucm_ctx_put(ctx
);
1181 result
= PTR_ERR(ctx
);
1184 kfree(param
.private_data
);
1189 static ssize_t (*ucm_cmd_table
[])(struct ib_ucm_file
*file
,
1190 const char __user
*inbuf
,
1191 int in_len
, int out_len
) = {
1192 [IB_USER_CM_CMD_CREATE_ID
] = ib_ucm_create_id
,
1193 [IB_USER_CM_CMD_DESTROY_ID
] = ib_ucm_destroy_id
,
1194 [IB_USER_CM_CMD_ATTR_ID
] = ib_ucm_attr_id
,
1195 [IB_USER_CM_CMD_LISTEN
] = ib_ucm_listen
,
1196 [IB_USER_CM_CMD_ESTABLISH
] = ib_ucm_establish
,
1197 [IB_USER_CM_CMD_SEND_REQ
] = ib_ucm_send_req
,
1198 [IB_USER_CM_CMD_SEND_REP
] = ib_ucm_send_rep
,
1199 [IB_USER_CM_CMD_SEND_RTU
] = ib_ucm_send_rtu
,
1200 [IB_USER_CM_CMD_SEND_DREQ
] = ib_ucm_send_dreq
,
1201 [IB_USER_CM_CMD_SEND_DREP
] = ib_ucm_send_drep
,
1202 [IB_USER_CM_CMD_SEND_REJ
] = ib_ucm_send_rej
,
1203 [IB_USER_CM_CMD_SEND_MRA
] = ib_ucm_send_mra
,
1204 [IB_USER_CM_CMD_SEND_LAP
] = ib_ucm_send_lap
,
1205 [IB_USER_CM_CMD_SEND_APR
] = ib_ucm_send_apr
,
1206 [IB_USER_CM_CMD_SEND_SIDR_REQ
] = ib_ucm_send_sidr_req
,
1207 [IB_USER_CM_CMD_SEND_SIDR_REP
] = ib_ucm_send_sidr_rep
,
1208 [IB_USER_CM_CMD_EVENT
] = ib_ucm_event
,
1209 [IB_USER_CM_CMD_INIT_QP_ATTR
] = ib_ucm_init_qp_attr
,
1212 static ssize_t
ib_ucm_write(struct file
*filp
, const char __user
*buf
,
1213 size_t len
, loff_t
*pos
)
1215 struct ib_ucm_file
*file
= filp
->private_data
;
1216 struct ib_ucm_cmd_hdr hdr
;
1219 if (len
< sizeof(hdr
))
1222 if (copy_from_user(&hdr
, buf
, sizeof(hdr
)))
1225 if (hdr
.cmd
< 0 || hdr
.cmd
>= ARRAY_SIZE(ucm_cmd_table
))
1228 if (hdr
.in
+ sizeof(hdr
) > len
)
1231 result
= ucm_cmd_table
[hdr
.cmd
](file
, buf
+ sizeof(hdr
),
1239 static unsigned int ib_ucm_poll(struct file
*filp
,
1240 struct poll_table_struct
*wait
)
1242 struct ib_ucm_file
*file
= filp
->private_data
;
1243 unsigned int mask
= 0;
1245 poll_wait(filp
, &file
->poll_wait
, wait
);
1247 if (!list_empty(&file
->events
))
1248 mask
= POLLIN
| POLLRDNORM
;
1253 static int ib_ucm_open(struct inode
*inode
, struct file
*filp
)
1255 struct ib_ucm_file
*file
;
1257 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
1261 INIT_LIST_HEAD(&file
->events
);
1262 INIT_LIST_HEAD(&file
->ctxs
);
1263 init_waitqueue_head(&file
->poll_wait
);
1265 init_MUTEX(&file
->mutex
);
1267 filp
->private_data
= file
;
1269 file
->device
= container_of(inode
->i_cdev
, struct ib_ucm_device
, dev
);
1274 static int ib_ucm_close(struct inode
*inode
, struct file
*filp
)
1276 struct ib_ucm_file
*file
= filp
->private_data
;
1277 struct ib_ucm_context
*ctx
;
1280 while (!list_empty(&file
->ctxs
)) {
1281 ctx
= list_entry(file
->ctxs
.next
,
1282 struct ib_ucm_context
, file_list
);
1285 down(&ctx_id_mutex
);
1286 idr_remove(&ctx_id_table
, ctx
->id
);
1289 ib_destroy_cm_id(ctx
->cm_id
);
1290 ib_ucm_cleanup_events(ctx
);
1300 static void ib_ucm_release_class_dev(struct class_device
*class_dev
)
1302 struct ib_ucm_device
*dev
;
1304 dev
= container_of(class_dev
, struct ib_ucm_device
, class_dev
);
1305 cdev_del(&dev
->dev
);
1306 clear_bit(dev
->devnum
, dev_map
);
1310 static struct file_operations ucm_fops
= {
1311 .owner
= THIS_MODULE
,
1312 .open
= ib_ucm_open
,
1313 .release
= ib_ucm_close
,
1314 .write
= ib_ucm_write
,
1315 .poll
= ib_ucm_poll
,
1318 static struct class ucm_class
= {
1319 .name
= "infiniband_cm",
1320 .release
= ib_ucm_release_class_dev
1323 static ssize_t
show_dev(struct class_device
*class_dev
, char *buf
)
1325 struct ib_ucm_device
*dev
;
1327 dev
= container_of(class_dev
, struct ib_ucm_device
, class_dev
);
1328 return print_dev_t(buf
, dev
->dev
.dev
);
1330 static CLASS_DEVICE_ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
1332 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1334 struct ib_ucm_device
*dev
;
1336 dev
= container_of(class_dev
, struct ib_ucm_device
, class_dev
);
1337 return sprintf(buf
, "%s\n", dev
->ib_dev
->name
);
1339 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1341 static void ib_ucm_add_one(struct ib_device
*device
)
1343 struct ib_ucm_device
*ucm_dev
;
1345 if (!device
->alloc_ucontext
)
1348 ucm_dev
= kmalloc(sizeof *ucm_dev
, GFP_KERNEL
);
1352 memset(ucm_dev
, 0, sizeof *ucm_dev
);
1353 ucm_dev
->ib_dev
= device
;
1355 ucm_dev
->devnum
= find_first_zero_bit(dev_map
, IB_UCM_MAX_DEVICES
);
1356 if (ucm_dev
->devnum
>= IB_UCM_MAX_DEVICES
)
1359 set_bit(ucm_dev
->devnum
, dev_map
);
1361 cdev_init(&ucm_dev
->dev
, &ucm_fops
);
1362 ucm_dev
->dev
.owner
= THIS_MODULE
;
1363 kobject_set_name(&ucm_dev
->dev
.kobj
, "ucm%d", ucm_dev
->devnum
);
1364 if (cdev_add(&ucm_dev
->dev
, IB_UCM_BASE_DEV
+ ucm_dev
->devnum
, 1))
1367 ucm_dev
->class_dev
.class = &ucm_class
;
1368 ucm_dev
->class_dev
.dev
= device
->dma_device
;
1369 snprintf(ucm_dev
->class_dev
.class_id
, BUS_ID_SIZE
, "ucm%d",
1371 if (class_device_register(&ucm_dev
->class_dev
))
1374 if (class_device_create_file(&ucm_dev
->class_dev
,
1375 &class_device_attr_dev
))
1377 if (class_device_create_file(&ucm_dev
->class_dev
,
1378 &class_device_attr_ibdev
))
1381 ib_set_client_data(device
, &ucm_client
, ucm_dev
);
1385 class_device_unregister(&ucm_dev
->class_dev
);
1387 cdev_del(&ucm_dev
->dev
);
1388 clear_bit(ucm_dev
->devnum
, dev_map
);
1394 static void ib_ucm_remove_one(struct ib_device
*device
)
1396 struct ib_ucm_device
*ucm_dev
= ib_get_client_data(device
, &ucm_client
);
1401 class_device_unregister(&ucm_dev
->class_dev
);
1404 static ssize_t
show_abi_version(struct class *class, char *buf
)
1406 return sprintf(buf
, "%d\n", IB_USER_CM_ABI_VERSION
);
1408 static CLASS_ATTR(abi_version
, S_IRUGO
, show_abi_version
, NULL
);
1410 static int __init
ib_ucm_init(void)
1414 ret
= register_chrdev_region(IB_UCM_BASE_DEV
, IB_UCM_MAX_DEVICES
,
1417 printk(KERN_ERR
"ucm: couldn't register device number\n");
1421 ret
= class_register(&ucm_class
);
1423 printk(KERN_ERR
"ucm: couldn't create class infiniband_cm\n");
1427 ret
= class_create_file(&ucm_class
, &class_attr_abi_version
);
1429 printk(KERN_ERR
"ucm: couldn't create abi_version attribute\n");
1433 ret
= ib_register_client(&ucm_client
);
1435 printk(KERN_ERR
"ucm: couldn't register client\n");
1441 class_unregister(&ucm_class
);
1443 unregister_chrdev_region(IB_UCM_BASE_DEV
, IB_UCM_MAX_DEVICES
);
1448 static void __exit
ib_ucm_cleanup(void)
1450 ib_unregister_client(&ucm_client
);
1451 class_unregister(&ucm_class
);
1452 unregister_chrdev_region(IB_UCM_BASE_DEV
, IB_UCM_MAX_DEVICES
);
1453 idr_destroy(&ctx_id_table
);
1456 module_init(ib_ucm_init
);
1457 module_exit(ib_ucm_cleanup
);