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 $
36 #include <linux/completion.h>
37 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/device.h>
41 #include <linux/err.h>
42 #include <linux/poll.h>
43 #include <linux/file.h>
44 #include <linux/mount.h>
45 #include <linux/cdev.h>
46 #include <linux/idr.h>
47 #include <linux/mutex.h>
49 #include <asm/uaccess.h>
51 #include <rdma/ib_cm.h>
52 #include <rdma/ib_user_cm.h>
54 MODULE_AUTHOR("Libor Michalek");
55 MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
56 MODULE_LICENSE("Dual BSD/GPL");
58 struct ib_ucm_device
{
61 struct class_device class_dev
;
62 struct ib_device
*ib_dev
;
66 struct semaphore mutex
;
68 struct ib_ucm_device
*device
;
70 struct list_head ctxs
;
71 struct list_head events
;
72 wait_queue_head_t poll_wait
;
75 struct ib_ucm_context
{
77 struct completion comp
;
81 struct ib_ucm_file
*file
;
82 struct ib_cm_id
*cm_id
;
85 struct list_head events
; /* list of pending events. */
86 struct list_head file_list
; /* member in file ctx list */
90 struct ib_ucm_context
*ctx
;
91 struct list_head file_list
; /* member in file event list */
92 struct list_head ctx_list
; /* member in ctx event list */
94 struct ib_cm_id
*cm_id
;
95 struct ib_ucm_event_resp resp
;
104 IB_UCM_BASE_MINOR
= 224,
105 IB_UCM_MAX_DEVICES
= 32
108 #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
110 static void ib_ucm_add_one(struct ib_device
*device
);
111 static void ib_ucm_remove_one(struct ib_device
*device
);
113 static struct ib_client ucm_client
= {
115 .add
= ib_ucm_add_one
,
116 .remove
= ib_ucm_remove_one
119 static DEFINE_MUTEX(ctx_id_mutex
);
120 static DEFINE_IDR(ctx_id_table
);
121 static DECLARE_BITMAP(dev_map
, IB_UCM_MAX_DEVICES
);
123 static struct ib_ucm_context
*ib_ucm_ctx_get(struct ib_ucm_file
*file
, int id
)
125 struct ib_ucm_context
*ctx
;
127 mutex_lock(&ctx_id_mutex
);
128 ctx
= idr_find(&ctx_id_table
, id
);
130 ctx
= ERR_PTR(-ENOENT
);
131 else if (ctx
->file
!= file
)
132 ctx
= ERR_PTR(-EINVAL
);
134 atomic_inc(&ctx
->ref
);
135 mutex_unlock(&ctx_id_mutex
);
140 static void ib_ucm_ctx_put(struct ib_ucm_context
*ctx
)
142 if (atomic_dec_and_test(&ctx
->ref
))
143 complete(&ctx
->comp
);
146 static inline int ib_ucm_new_cm_id(int event
)
148 return event
== IB_CM_REQ_RECEIVED
|| event
== IB_CM_SIDR_REQ_RECEIVED
;
151 static void ib_ucm_cleanup_events(struct ib_ucm_context
*ctx
)
153 struct ib_ucm_event
*uevent
;
155 down(&ctx
->file
->mutex
);
156 list_del(&ctx
->file_list
);
157 while (!list_empty(&ctx
->events
)) {
159 uevent
= list_entry(ctx
->events
.next
,
160 struct ib_ucm_event
, ctx_list
);
161 list_del(&uevent
->file_list
);
162 list_del(&uevent
->ctx_list
);
164 /* clear incoming connections. */
165 if (ib_ucm_new_cm_id(uevent
->resp
.event
))
166 ib_destroy_cm_id(uevent
->cm_id
);
170 up(&ctx
->file
->mutex
);
173 static struct ib_ucm_context
*ib_ucm_ctx_alloc(struct ib_ucm_file
*file
)
175 struct ib_ucm_context
*ctx
;
178 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
182 atomic_set(&ctx
->ref
, 1);
183 init_completion(&ctx
->comp
);
185 INIT_LIST_HEAD(&ctx
->events
);
188 result
= idr_pre_get(&ctx_id_table
, GFP_KERNEL
);
192 mutex_lock(&ctx_id_mutex
);
193 result
= idr_get_new(&ctx_id_table
, ctx
, &ctx
->id
);
194 mutex_unlock(&ctx_id_mutex
);
195 } while (result
== -EAGAIN
);
200 list_add_tail(&ctx
->file_list
, &file
->ctxs
);
208 static void ib_ucm_event_path_get(struct ib_ucm_path_rec
*upath
,
209 struct ib_sa_path_rec
*kpath
)
211 if (!kpath
|| !upath
)
214 memcpy(upath
->dgid
, kpath
->dgid
.raw
, sizeof *upath
->dgid
);
215 memcpy(upath
->sgid
, kpath
->sgid
.raw
, sizeof *upath
->sgid
);
217 upath
->dlid
= kpath
->dlid
;
218 upath
->slid
= kpath
->slid
;
219 upath
->raw_traffic
= kpath
->raw_traffic
;
220 upath
->flow_label
= kpath
->flow_label
;
221 upath
->hop_limit
= kpath
->hop_limit
;
222 upath
->traffic_class
= kpath
->traffic_class
;
223 upath
->reversible
= kpath
->reversible
;
224 upath
->numb_path
= kpath
->numb_path
;
225 upath
->pkey
= kpath
->pkey
;
226 upath
->sl
= kpath
->sl
;
227 upath
->mtu_selector
= kpath
->mtu_selector
;
228 upath
->mtu
= kpath
->mtu
;
229 upath
->rate_selector
= kpath
->rate_selector
;
230 upath
->rate
= kpath
->rate
;
231 upath
->packet_life_time
= kpath
->packet_life_time
;
232 upath
->preference
= kpath
->preference
;
234 upath
->packet_life_time_selector
=
235 kpath
->packet_life_time_selector
;
238 static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp
*ureq
,
239 struct ib_cm_req_event_param
*kreq
)
241 ureq
->remote_ca_guid
= kreq
->remote_ca_guid
;
242 ureq
->remote_qkey
= kreq
->remote_qkey
;
243 ureq
->remote_qpn
= kreq
->remote_qpn
;
244 ureq
->qp_type
= kreq
->qp_type
;
245 ureq
->starting_psn
= kreq
->starting_psn
;
246 ureq
->responder_resources
= kreq
->responder_resources
;
247 ureq
->initiator_depth
= kreq
->initiator_depth
;
248 ureq
->local_cm_response_timeout
= kreq
->local_cm_response_timeout
;
249 ureq
->flow_control
= kreq
->flow_control
;
250 ureq
->remote_cm_response_timeout
= kreq
->remote_cm_response_timeout
;
251 ureq
->retry_count
= kreq
->retry_count
;
252 ureq
->rnr_retry_count
= kreq
->rnr_retry_count
;
253 ureq
->srq
= kreq
->srq
;
254 ureq
->port
= kreq
->port
;
256 ib_ucm_event_path_get(&ureq
->primary_path
, kreq
->primary_path
);
257 ib_ucm_event_path_get(&ureq
->alternate_path
, kreq
->alternate_path
);
260 static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp
*urep
,
261 struct ib_cm_rep_event_param
*krep
)
263 urep
->remote_ca_guid
= krep
->remote_ca_guid
;
264 urep
->remote_qkey
= krep
->remote_qkey
;
265 urep
->remote_qpn
= krep
->remote_qpn
;
266 urep
->starting_psn
= krep
->starting_psn
;
267 urep
->responder_resources
= krep
->responder_resources
;
268 urep
->initiator_depth
= krep
->initiator_depth
;
269 urep
->target_ack_delay
= krep
->target_ack_delay
;
270 urep
->failover_accepted
= krep
->failover_accepted
;
271 urep
->flow_control
= krep
->flow_control
;
272 urep
->rnr_retry_count
= krep
->rnr_retry_count
;
273 urep
->srq
= krep
->srq
;
276 static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp
*urep
,
277 struct ib_cm_sidr_rep_event_param
*krep
)
279 urep
->status
= krep
->status
;
280 urep
->qkey
= krep
->qkey
;
281 urep
->qpn
= krep
->qpn
;
284 static int ib_ucm_event_process(struct ib_cm_event
*evt
,
285 struct ib_ucm_event
*uvt
)
289 switch (evt
->event
) {
290 case IB_CM_REQ_RECEIVED
:
291 ib_ucm_event_req_get(&uvt
->resp
.u
.req_resp
,
292 &evt
->param
.req_rcvd
);
293 uvt
->data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
;
294 uvt
->resp
.present
= IB_UCM_PRES_PRIMARY
;
295 uvt
->resp
.present
|= (evt
->param
.req_rcvd
.alternate_path
?
296 IB_UCM_PRES_ALTERNATE
: 0);
298 case IB_CM_REP_RECEIVED
:
299 ib_ucm_event_rep_get(&uvt
->resp
.u
.rep_resp
,
300 &evt
->param
.rep_rcvd
);
301 uvt
->data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
303 case IB_CM_RTU_RECEIVED
:
304 uvt
->data_len
= IB_CM_RTU_PRIVATE_DATA_SIZE
;
305 uvt
->resp
.u
.send_status
= evt
->param
.send_status
;
307 case IB_CM_DREQ_RECEIVED
:
308 uvt
->data_len
= IB_CM_DREQ_PRIVATE_DATA_SIZE
;
309 uvt
->resp
.u
.send_status
= evt
->param
.send_status
;
311 case IB_CM_DREP_RECEIVED
:
312 uvt
->data_len
= IB_CM_DREP_PRIVATE_DATA_SIZE
;
313 uvt
->resp
.u
.send_status
= evt
->param
.send_status
;
315 case IB_CM_MRA_RECEIVED
:
316 uvt
->resp
.u
.mra_resp
.timeout
=
317 evt
->param
.mra_rcvd
.service_timeout
;
318 uvt
->data_len
= IB_CM_MRA_PRIVATE_DATA_SIZE
;
320 case IB_CM_REJ_RECEIVED
:
321 uvt
->resp
.u
.rej_resp
.reason
= evt
->param
.rej_rcvd
.reason
;
322 uvt
->data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
323 uvt
->info_len
= evt
->param
.rej_rcvd
.ari_length
;
324 info
= evt
->param
.rej_rcvd
.ari
;
326 case IB_CM_LAP_RECEIVED
:
327 ib_ucm_event_path_get(&uvt
->resp
.u
.lap_resp
.path
,
328 evt
->param
.lap_rcvd
.alternate_path
);
329 uvt
->data_len
= IB_CM_LAP_PRIVATE_DATA_SIZE
;
330 uvt
->resp
.present
= IB_UCM_PRES_ALTERNATE
;
332 case IB_CM_APR_RECEIVED
:
333 uvt
->resp
.u
.apr_resp
.status
= evt
->param
.apr_rcvd
.ap_status
;
334 uvt
->data_len
= IB_CM_APR_PRIVATE_DATA_SIZE
;
335 uvt
->info_len
= evt
->param
.apr_rcvd
.info_len
;
336 info
= evt
->param
.apr_rcvd
.apr_info
;
338 case IB_CM_SIDR_REQ_RECEIVED
:
339 uvt
->resp
.u
.sidr_req_resp
.pkey
=
340 evt
->param
.sidr_req_rcvd
.pkey
;
341 uvt
->resp
.u
.sidr_req_resp
.port
=
342 evt
->param
.sidr_req_rcvd
.port
;
343 uvt
->data_len
= IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
;
345 case IB_CM_SIDR_REP_RECEIVED
:
346 ib_ucm_event_sidr_rep_get(&uvt
->resp
.u
.sidr_rep_resp
,
347 &evt
->param
.sidr_rep_rcvd
);
348 uvt
->data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
349 uvt
->info_len
= evt
->param
.sidr_rep_rcvd
.info_len
;
350 info
= evt
->param
.sidr_rep_rcvd
.info
;
353 uvt
->resp
.u
.send_status
= evt
->param
.send_status
;
358 uvt
->data
= kmalloc(uvt
->data_len
, GFP_KERNEL
);
362 memcpy(uvt
->data
, evt
->private_data
, uvt
->data_len
);
363 uvt
->resp
.present
|= IB_UCM_PRES_DATA
;
367 uvt
->info
= kmalloc(uvt
->info_len
, GFP_KERNEL
);
371 memcpy(uvt
->info
, info
, uvt
->info_len
);
372 uvt
->resp
.present
|= IB_UCM_PRES_INFO
;
382 static int ib_ucm_event_handler(struct ib_cm_id
*cm_id
,
383 struct ib_cm_event
*event
)
385 struct ib_ucm_event
*uevent
;
386 struct ib_ucm_context
*ctx
;
389 ctx
= cm_id
->context
;
391 uevent
= kzalloc(sizeof *uevent
, GFP_KERNEL
);
396 uevent
->cm_id
= cm_id
;
397 uevent
->resp
.uid
= ctx
->uid
;
398 uevent
->resp
.id
= ctx
->id
;
399 uevent
->resp
.event
= event
->event
;
401 result
= ib_ucm_event_process(event
, uevent
);
405 down(&ctx
->file
->mutex
);
406 list_add_tail(&uevent
->file_list
, &ctx
->file
->events
);
407 list_add_tail(&uevent
->ctx_list
, &ctx
->events
);
408 wake_up_interruptible(&ctx
->file
->poll_wait
);
409 up(&ctx
->file
->mutex
);
415 /* Destroy new cm_id's */
416 return ib_ucm_new_cm_id(event
->event
);
419 static ssize_t
ib_ucm_event(struct ib_ucm_file
*file
,
420 const char __user
*inbuf
,
421 int in_len
, int out_len
)
423 struct ib_ucm_context
*ctx
;
424 struct ib_ucm_event_get cmd
;
425 struct ib_ucm_event
*uevent
;
429 if (out_len
< sizeof(struct ib_ucm_event_resp
))
432 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
436 while (list_empty(&file
->events
)) {
438 if (file
->filp
->f_flags
& O_NONBLOCK
) {
443 if (signal_pending(current
)) {
444 result
= -ERESTARTSYS
;
448 prepare_to_wait(&file
->poll_wait
, &wait
, TASK_INTERRUPTIBLE
);
454 finish_wait(&file
->poll_wait
, &wait
);
460 uevent
= list_entry(file
->events
.next
, struct ib_ucm_event
, file_list
);
462 if (ib_ucm_new_cm_id(uevent
->resp
.event
)) {
463 ctx
= ib_ucm_ctx_alloc(file
);
469 ctx
->cm_id
= uevent
->cm_id
;
470 ctx
->cm_id
->context
= ctx
;
471 uevent
->resp
.id
= ctx
->id
;
474 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
475 &uevent
->resp
, sizeof(uevent
->resp
))) {
481 if (cmd
.data_len
< uevent
->data_len
) {
485 if (copy_to_user((void __user
*)(unsigned long)cmd
.data
,
486 uevent
->data
, uevent
->data_len
)) {
493 if (cmd
.info_len
< uevent
->info_len
) {
497 if (copy_to_user((void __user
*)(unsigned long)cmd
.info
,
498 uevent
->info
, uevent
->info_len
)) {
504 list_del(&uevent
->file_list
);
505 list_del(&uevent
->ctx_list
);
506 uevent
->ctx
->events_reported
++;
516 static ssize_t
ib_ucm_create_id(struct ib_ucm_file
*file
,
517 const char __user
*inbuf
,
518 int in_len
, int out_len
)
520 struct ib_ucm_create_id cmd
;
521 struct ib_ucm_create_id_resp resp
;
522 struct ib_ucm_context
*ctx
;
525 if (out_len
< sizeof(resp
))
528 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
532 ctx
= ib_ucm_ctx_alloc(file
);
538 ctx
->cm_id
= ib_create_cm_id(file
->device
->ib_dev
,
539 ib_ucm_event_handler
, ctx
);
540 if (IS_ERR(ctx
->cm_id
)) {
541 result
= PTR_ERR(ctx
->cm_id
);
546 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
547 &resp
, sizeof(resp
))) {
554 ib_destroy_cm_id(ctx
->cm_id
);
556 mutex_lock(&ctx_id_mutex
);
557 idr_remove(&ctx_id_table
, ctx
->id
);
558 mutex_unlock(&ctx_id_mutex
);
563 static ssize_t
ib_ucm_destroy_id(struct ib_ucm_file
*file
,
564 const char __user
*inbuf
,
565 int in_len
, int out_len
)
567 struct ib_ucm_destroy_id cmd
;
568 struct ib_ucm_destroy_id_resp resp
;
569 struct ib_ucm_context
*ctx
;
572 if (out_len
< sizeof(resp
))
575 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
578 mutex_lock(&ctx_id_mutex
);
579 ctx
= idr_find(&ctx_id_table
, cmd
.id
);
581 ctx
= ERR_PTR(-ENOENT
);
582 else if (ctx
->file
!= file
)
583 ctx
= ERR_PTR(-EINVAL
);
585 idr_remove(&ctx_id_table
, ctx
->id
);
586 mutex_unlock(&ctx_id_mutex
);
592 wait_for_completion(&ctx
->comp
);
594 /* No new events will be generated after destroying the cm_id. */
595 ib_destroy_cm_id(ctx
->cm_id
);
596 /* Cleanup events not yet reported to the user. */
597 ib_ucm_cleanup_events(ctx
);
599 resp
.events_reported
= ctx
->events_reported
;
600 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
601 &resp
, sizeof(resp
)))
608 static ssize_t
ib_ucm_attr_id(struct ib_ucm_file
*file
,
609 const char __user
*inbuf
,
610 int in_len
, int out_len
)
612 struct ib_ucm_attr_id_resp resp
;
613 struct ib_ucm_attr_id cmd
;
614 struct ib_ucm_context
*ctx
;
617 if (out_len
< sizeof(resp
))
620 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
623 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
627 resp
.service_id
= ctx
->cm_id
->service_id
;
628 resp
.service_mask
= ctx
->cm_id
->service_mask
;
629 resp
.local_id
= ctx
->cm_id
->local_id
;
630 resp
.remote_id
= ctx
->cm_id
->remote_id
;
632 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
633 &resp
, sizeof(resp
)))
640 static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr
*dest_attr
,
641 struct ib_ah_attr
*src_attr
)
643 memcpy(dest_attr
->grh_dgid
, src_attr
->grh
.dgid
.raw
,
644 sizeof src_attr
->grh
.dgid
);
645 dest_attr
->grh_flow_label
= src_attr
->grh
.flow_label
;
646 dest_attr
->grh_sgid_index
= src_attr
->grh
.sgid_index
;
647 dest_attr
->grh_hop_limit
= src_attr
->grh
.hop_limit
;
648 dest_attr
->grh_traffic_class
= src_attr
->grh
.traffic_class
;
650 dest_attr
->dlid
= src_attr
->dlid
;
651 dest_attr
->sl
= src_attr
->sl
;
652 dest_attr
->src_path_bits
= src_attr
->src_path_bits
;
653 dest_attr
->static_rate
= src_attr
->static_rate
;
654 dest_attr
->is_global
= (src_attr
->ah_flags
& IB_AH_GRH
);
655 dest_attr
->port_num
= src_attr
->port_num
;
658 static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp
*dest_attr
,
659 struct ib_qp_attr
*src_attr
)
661 dest_attr
->cur_qp_state
= src_attr
->cur_qp_state
;
662 dest_attr
->path_mtu
= src_attr
->path_mtu
;
663 dest_attr
->path_mig_state
= src_attr
->path_mig_state
;
664 dest_attr
->qkey
= src_attr
->qkey
;
665 dest_attr
->rq_psn
= src_attr
->rq_psn
;
666 dest_attr
->sq_psn
= src_attr
->sq_psn
;
667 dest_attr
->dest_qp_num
= src_attr
->dest_qp_num
;
668 dest_attr
->qp_access_flags
= src_attr
->qp_access_flags
;
670 dest_attr
->max_send_wr
= src_attr
->cap
.max_send_wr
;
671 dest_attr
->max_recv_wr
= src_attr
->cap
.max_recv_wr
;
672 dest_attr
->max_send_sge
= src_attr
->cap
.max_send_sge
;
673 dest_attr
->max_recv_sge
= src_attr
->cap
.max_recv_sge
;
674 dest_attr
->max_inline_data
= src_attr
->cap
.max_inline_data
;
676 ib_ucm_copy_ah_attr(&dest_attr
->ah_attr
, &src_attr
->ah_attr
);
677 ib_ucm_copy_ah_attr(&dest_attr
->alt_ah_attr
, &src_attr
->alt_ah_attr
);
679 dest_attr
->pkey_index
= src_attr
->pkey_index
;
680 dest_attr
->alt_pkey_index
= src_attr
->alt_pkey_index
;
681 dest_attr
->en_sqd_async_notify
= src_attr
->en_sqd_async_notify
;
682 dest_attr
->sq_draining
= src_attr
->sq_draining
;
683 dest_attr
->max_rd_atomic
= src_attr
->max_rd_atomic
;
684 dest_attr
->max_dest_rd_atomic
= src_attr
->max_dest_rd_atomic
;
685 dest_attr
->min_rnr_timer
= src_attr
->min_rnr_timer
;
686 dest_attr
->port_num
= src_attr
->port_num
;
687 dest_attr
->timeout
= src_attr
->timeout
;
688 dest_attr
->retry_cnt
= src_attr
->retry_cnt
;
689 dest_attr
->rnr_retry
= src_attr
->rnr_retry
;
690 dest_attr
->alt_port_num
= src_attr
->alt_port_num
;
691 dest_attr
->alt_timeout
= src_attr
->alt_timeout
;
694 static ssize_t
ib_ucm_init_qp_attr(struct ib_ucm_file
*file
,
695 const char __user
*inbuf
,
696 int in_len
, int out_len
)
698 struct ib_ucm_init_qp_attr_resp resp
;
699 struct ib_ucm_init_qp_attr cmd
;
700 struct ib_ucm_context
*ctx
;
701 struct ib_qp_attr qp_attr
;
704 if (out_len
< sizeof(resp
))
707 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
710 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
714 resp
.qp_attr_mask
= 0;
715 memset(&qp_attr
, 0, sizeof qp_attr
);
716 qp_attr
.qp_state
= cmd
.qp_state
;
717 result
= ib_cm_init_qp_attr(ctx
->cm_id
, &qp_attr
, &resp
.qp_attr_mask
);
721 ib_ucm_copy_qp_attr(&resp
, &qp_attr
);
723 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
724 &resp
, sizeof(resp
)))
732 static ssize_t
ib_ucm_listen(struct ib_ucm_file
*file
,
733 const char __user
*inbuf
,
734 int in_len
, int out_len
)
736 struct ib_ucm_listen cmd
;
737 struct ib_ucm_context
*ctx
;
740 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
743 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
747 result
= ib_cm_listen(ctx
->cm_id
, cmd
.service_id
, cmd
.service_mask
);
752 static ssize_t
ib_ucm_establish(struct ib_ucm_file
*file
,
753 const char __user
*inbuf
,
754 int in_len
, int out_len
)
756 struct ib_ucm_establish cmd
;
757 struct ib_ucm_context
*ctx
;
760 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
763 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
767 result
= ib_cm_establish(ctx
->cm_id
);
772 static int ib_ucm_alloc_data(const void **dest
, u64 src
, u32 len
)
781 data
= kmalloc(len
, GFP_KERNEL
);
785 if (copy_from_user(data
, (void __user
*)(unsigned long)src
, len
)) {
794 static int ib_ucm_path_get(struct ib_sa_path_rec
**path
, u64 src
)
796 struct ib_ucm_path_rec ucm_path
;
797 struct ib_sa_path_rec
*sa_path
;
804 sa_path
= kmalloc(sizeof(*sa_path
), GFP_KERNEL
);
808 if (copy_from_user(&ucm_path
, (void __user
*)(unsigned long)src
,
815 memcpy(sa_path
->dgid
.raw
, ucm_path
.dgid
, sizeof sa_path
->dgid
);
816 memcpy(sa_path
->sgid
.raw
, ucm_path
.sgid
, sizeof sa_path
->sgid
);
818 sa_path
->dlid
= ucm_path
.dlid
;
819 sa_path
->slid
= ucm_path
.slid
;
820 sa_path
->raw_traffic
= ucm_path
.raw_traffic
;
821 sa_path
->flow_label
= ucm_path
.flow_label
;
822 sa_path
->hop_limit
= ucm_path
.hop_limit
;
823 sa_path
->traffic_class
= ucm_path
.traffic_class
;
824 sa_path
->reversible
= ucm_path
.reversible
;
825 sa_path
->numb_path
= ucm_path
.numb_path
;
826 sa_path
->pkey
= ucm_path
.pkey
;
827 sa_path
->sl
= ucm_path
.sl
;
828 sa_path
->mtu_selector
= ucm_path
.mtu_selector
;
829 sa_path
->mtu
= ucm_path
.mtu
;
830 sa_path
->rate_selector
= ucm_path
.rate_selector
;
831 sa_path
->rate
= ucm_path
.rate
;
832 sa_path
->packet_life_time
= ucm_path
.packet_life_time
;
833 sa_path
->preference
= ucm_path
.preference
;
835 sa_path
->packet_life_time_selector
=
836 ucm_path
.packet_life_time_selector
;
842 static ssize_t
ib_ucm_send_req(struct ib_ucm_file
*file
,
843 const char __user
*inbuf
,
844 int in_len
, int out_len
)
846 struct ib_cm_req_param param
;
847 struct ib_ucm_context
*ctx
;
848 struct ib_ucm_req cmd
;
851 param
.private_data
= NULL
;
852 param
.primary_path
= NULL
;
853 param
.alternate_path
= NULL
;
855 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
858 result
= ib_ucm_alloc_data(¶m
.private_data
, cmd
.data
, cmd
.len
);
862 result
= ib_ucm_path_get(¶m
.primary_path
, cmd
.primary_path
);
866 result
= ib_ucm_path_get(¶m
.alternate_path
, cmd
.alternate_path
);
870 param
.private_data_len
= cmd
.len
;
871 param
.service_id
= cmd
.sid
;
872 param
.qp_num
= cmd
.qpn
;
873 param
.qp_type
= cmd
.qp_type
;
874 param
.starting_psn
= cmd
.psn
;
875 param
.peer_to_peer
= cmd
.peer_to_peer
;
876 param
.responder_resources
= cmd
.responder_resources
;
877 param
.initiator_depth
= cmd
.initiator_depth
;
878 param
.remote_cm_response_timeout
= cmd
.remote_cm_response_timeout
;
879 param
.flow_control
= cmd
.flow_control
;
880 param
.local_cm_response_timeout
= cmd
.local_cm_response_timeout
;
881 param
.retry_count
= cmd
.retry_count
;
882 param
.rnr_retry_count
= cmd
.rnr_retry_count
;
883 param
.max_cm_retries
= cmd
.max_cm_retries
;
886 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
888 result
= ib_send_cm_req(ctx
->cm_id
, ¶m
);
891 result
= PTR_ERR(ctx
);
894 kfree(param
.private_data
);
895 kfree(param
.primary_path
);
896 kfree(param
.alternate_path
);
900 static ssize_t
ib_ucm_send_rep(struct ib_ucm_file
*file
,
901 const char __user
*inbuf
,
902 int in_len
, int out_len
)
904 struct ib_cm_rep_param param
;
905 struct ib_ucm_context
*ctx
;
906 struct ib_ucm_rep cmd
;
909 param
.private_data
= NULL
;
911 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
914 result
= ib_ucm_alloc_data(¶m
.private_data
, cmd
.data
, cmd
.len
);
918 param
.qp_num
= cmd
.qpn
;
919 param
.starting_psn
= cmd
.psn
;
920 param
.private_data_len
= cmd
.len
;
921 param
.responder_resources
= cmd
.responder_resources
;
922 param
.initiator_depth
= cmd
.initiator_depth
;
923 param
.target_ack_delay
= cmd
.target_ack_delay
;
924 param
.failover_accepted
= cmd
.failover_accepted
;
925 param
.flow_control
= cmd
.flow_control
;
926 param
.rnr_retry_count
= cmd
.rnr_retry_count
;
929 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
932 result
= ib_send_cm_rep(ctx
->cm_id
, ¶m
);
935 result
= PTR_ERR(ctx
);
937 kfree(param
.private_data
);
941 static ssize_t
ib_ucm_send_private_data(struct ib_ucm_file
*file
,
942 const char __user
*inbuf
, int in_len
,
943 int (*func
)(struct ib_cm_id
*cm_id
,
944 const void *private_data
,
945 u8 private_data_len
))
947 struct ib_ucm_private_data cmd
;
948 struct ib_ucm_context
*ctx
;
949 const void *private_data
= NULL
;
952 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
955 result
= ib_ucm_alloc_data(&private_data
, cmd
.data
, cmd
.len
);
959 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
961 result
= func(ctx
->cm_id
, private_data
, cmd
.len
);
964 result
= PTR_ERR(ctx
);
970 static ssize_t
ib_ucm_send_rtu(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_rtu
);
977 static ssize_t
ib_ucm_send_dreq(struct ib_ucm_file
*file
,
978 const char __user
*inbuf
,
979 int in_len
, int out_len
)
981 return ib_ucm_send_private_data(file
, inbuf
, in_len
, ib_send_cm_dreq
);
984 static ssize_t
ib_ucm_send_drep(struct ib_ucm_file
*file
,
985 const char __user
*inbuf
,
986 int in_len
, int out_len
)
988 return ib_ucm_send_private_data(file
, inbuf
, in_len
, ib_send_cm_drep
);
991 static ssize_t
ib_ucm_send_info(struct ib_ucm_file
*file
,
992 const char __user
*inbuf
, int in_len
,
993 int (*func
)(struct ib_cm_id
*cm_id
,
1000 struct ib_ucm_context
*ctx
;
1001 struct ib_ucm_info cmd
;
1002 const void *data
= NULL
;
1003 const void *info
= NULL
;
1006 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1009 result
= ib_ucm_alloc_data(&data
, cmd
.data
, cmd
.data_len
);
1013 result
= ib_ucm_alloc_data(&info
, cmd
.info
, cmd
.info_len
);
1017 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1019 result
= func(ctx
->cm_id
, cmd
.status
, info
, cmd
.info_len
,
1020 data
, cmd
.data_len
);
1021 ib_ucm_ctx_put(ctx
);
1023 result
= PTR_ERR(ctx
);
1031 static ssize_t
ib_ucm_send_rej(struct ib_ucm_file
*file
,
1032 const char __user
*inbuf
,
1033 int in_len
, int out_len
)
1035 return ib_ucm_send_info(file
, inbuf
, in_len
, (void *)ib_send_cm_rej
);
1038 static ssize_t
ib_ucm_send_apr(struct ib_ucm_file
*file
,
1039 const char __user
*inbuf
,
1040 int in_len
, int out_len
)
1042 return ib_ucm_send_info(file
, inbuf
, in_len
, (void *)ib_send_cm_apr
);
1045 static ssize_t
ib_ucm_send_mra(struct ib_ucm_file
*file
,
1046 const char __user
*inbuf
,
1047 int in_len
, int out_len
)
1049 struct ib_ucm_context
*ctx
;
1050 struct ib_ucm_mra cmd
;
1051 const void *data
= NULL
;
1054 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1057 result
= ib_ucm_alloc_data(&data
, cmd
.data
, cmd
.len
);
1061 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1063 result
= ib_send_cm_mra(ctx
->cm_id
, cmd
.timeout
, data
, cmd
.len
);
1064 ib_ucm_ctx_put(ctx
);
1066 result
= PTR_ERR(ctx
);
1072 static ssize_t
ib_ucm_send_lap(struct ib_ucm_file
*file
,
1073 const char __user
*inbuf
,
1074 int in_len
, int out_len
)
1076 struct ib_ucm_context
*ctx
;
1077 struct ib_sa_path_rec
*path
= NULL
;
1078 struct ib_ucm_lap cmd
;
1079 const void *data
= NULL
;
1082 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1085 result
= ib_ucm_alloc_data(&data
, cmd
.data
, cmd
.len
);
1089 result
= ib_ucm_path_get(&path
, cmd
.path
);
1093 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1095 result
= ib_send_cm_lap(ctx
->cm_id
, path
, data
, cmd
.len
);
1096 ib_ucm_ctx_put(ctx
);
1098 result
= PTR_ERR(ctx
);
1106 static ssize_t
ib_ucm_send_sidr_req(struct ib_ucm_file
*file
,
1107 const char __user
*inbuf
,
1108 int in_len
, int out_len
)
1110 struct ib_cm_sidr_req_param param
;
1111 struct ib_ucm_context
*ctx
;
1112 struct ib_ucm_sidr_req cmd
;
1115 param
.private_data
= NULL
;
1118 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1121 result
= ib_ucm_alloc_data(¶m
.private_data
, cmd
.data
, cmd
.len
);
1125 result
= ib_ucm_path_get(¶m
.path
, cmd
.path
);
1129 param
.private_data_len
= cmd
.len
;
1130 param
.service_id
= cmd
.sid
;
1131 param
.timeout_ms
= cmd
.timeout
;
1132 param
.max_cm_retries
= cmd
.max_cm_retries
;
1133 param
.pkey
= cmd
.pkey
;
1135 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1137 result
= ib_send_cm_sidr_req(ctx
->cm_id
, ¶m
);
1138 ib_ucm_ctx_put(ctx
);
1140 result
= PTR_ERR(ctx
);
1143 kfree(param
.private_data
);
1148 static ssize_t
ib_ucm_send_sidr_rep(struct ib_ucm_file
*file
,
1149 const char __user
*inbuf
,
1150 int in_len
, int out_len
)
1152 struct ib_cm_sidr_rep_param param
;
1153 struct ib_ucm_sidr_rep cmd
;
1154 struct ib_ucm_context
*ctx
;
1159 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1162 result
= ib_ucm_alloc_data(¶m
.private_data
,
1163 cmd
.data
, cmd
.data_len
);
1167 result
= ib_ucm_alloc_data(¶m
.info
, cmd
.info
, cmd
.info_len
);
1171 param
.qp_num
= cmd
.qpn
;
1172 param
.qkey
= cmd
.qkey
;
1173 param
.status
= cmd
.status
;
1174 param
.info_length
= cmd
.info_len
;
1175 param
.private_data_len
= cmd
.data_len
;
1177 ctx
= ib_ucm_ctx_get(file
, cmd
.id
);
1179 result
= ib_send_cm_sidr_rep(ctx
->cm_id
, ¶m
);
1180 ib_ucm_ctx_put(ctx
);
1182 result
= PTR_ERR(ctx
);
1185 kfree(param
.private_data
);
1190 static ssize_t (*ucm_cmd_table
[])(struct ib_ucm_file
*file
,
1191 const char __user
*inbuf
,
1192 int in_len
, int out_len
) = {
1193 [IB_USER_CM_CMD_CREATE_ID
] = ib_ucm_create_id
,
1194 [IB_USER_CM_CMD_DESTROY_ID
] = ib_ucm_destroy_id
,
1195 [IB_USER_CM_CMD_ATTR_ID
] = ib_ucm_attr_id
,
1196 [IB_USER_CM_CMD_LISTEN
] = ib_ucm_listen
,
1197 [IB_USER_CM_CMD_ESTABLISH
] = ib_ucm_establish
,
1198 [IB_USER_CM_CMD_SEND_REQ
] = ib_ucm_send_req
,
1199 [IB_USER_CM_CMD_SEND_REP
] = ib_ucm_send_rep
,
1200 [IB_USER_CM_CMD_SEND_RTU
] = ib_ucm_send_rtu
,
1201 [IB_USER_CM_CMD_SEND_DREQ
] = ib_ucm_send_dreq
,
1202 [IB_USER_CM_CMD_SEND_DREP
] = ib_ucm_send_drep
,
1203 [IB_USER_CM_CMD_SEND_REJ
] = ib_ucm_send_rej
,
1204 [IB_USER_CM_CMD_SEND_MRA
] = ib_ucm_send_mra
,
1205 [IB_USER_CM_CMD_SEND_LAP
] = ib_ucm_send_lap
,
1206 [IB_USER_CM_CMD_SEND_APR
] = ib_ucm_send_apr
,
1207 [IB_USER_CM_CMD_SEND_SIDR_REQ
] = ib_ucm_send_sidr_req
,
1208 [IB_USER_CM_CMD_SEND_SIDR_REP
] = ib_ucm_send_sidr_rep
,
1209 [IB_USER_CM_CMD_EVENT
] = ib_ucm_event
,
1210 [IB_USER_CM_CMD_INIT_QP_ATTR
] = ib_ucm_init_qp_attr
,
1213 static ssize_t
ib_ucm_write(struct file
*filp
, const char __user
*buf
,
1214 size_t len
, loff_t
*pos
)
1216 struct ib_ucm_file
*file
= filp
->private_data
;
1217 struct ib_ucm_cmd_hdr hdr
;
1220 if (len
< sizeof(hdr
))
1223 if (copy_from_user(&hdr
, buf
, sizeof(hdr
)))
1226 if (hdr
.cmd
< 0 || hdr
.cmd
>= ARRAY_SIZE(ucm_cmd_table
))
1229 if (hdr
.in
+ sizeof(hdr
) > len
)
1232 result
= ucm_cmd_table
[hdr
.cmd
](file
, buf
+ sizeof(hdr
),
1240 static unsigned int ib_ucm_poll(struct file
*filp
,
1241 struct poll_table_struct
*wait
)
1243 struct ib_ucm_file
*file
= filp
->private_data
;
1244 unsigned int mask
= 0;
1246 poll_wait(filp
, &file
->poll_wait
, wait
);
1248 if (!list_empty(&file
->events
))
1249 mask
= POLLIN
| POLLRDNORM
;
1254 static int ib_ucm_open(struct inode
*inode
, struct file
*filp
)
1256 struct ib_ucm_file
*file
;
1258 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
1262 INIT_LIST_HEAD(&file
->events
);
1263 INIT_LIST_HEAD(&file
->ctxs
);
1264 init_waitqueue_head(&file
->poll_wait
);
1266 init_MUTEX(&file
->mutex
);
1268 filp
->private_data
= file
;
1270 file
->device
= container_of(inode
->i_cdev
, struct ib_ucm_device
, dev
);
1275 static int ib_ucm_close(struct inode
*inode
, struct file
*filp
)
1277 struct ib_ucm_file
*file
= filp
->private_data
;
1278 struct ib_ucm_context
*ctx
;
1281 while (!list_empty(&file
->ctxs
)) {
1282 ctx
= list_entry(file
->ctxs
.next
,
1283 struct ib_ucm_context
, file_list
);
1286 mutex_lock(&ctx_id_mutex
);
1287 idr_remove(&ctx_id_table
, ctx
->id
);
1288 mutex_unlock(&ctx_id_mutex
);
1290 ib_destroy_cm_id(ctx
->cm_id
);
1291 ib_ucm_cleanup_events(ctx
);
1301 static void ib_ucm_release_class_dev(struct class_device
*class_dev
)
1303 struct ib_ucm_device
*dev
;
1305 dev
= container_of(class_dev
, struct ib_ucm_device
, class_dev
);
1306 cdev_del(&dev
->dev
);
1307 clear_bit(dev
->devnum
, dev_map
);
1311 static struct file_operations ucm_fops
= {
1312 .owner
= THIS_MODULE
,
1313 .open
= ib_ucm_open
,
1314 .release
= ib_ucm_close
,
1315 .write
= ib_ucm_write
,
1316 .poll
= ib_ucm_poll
,
1319 static struct class ucm_class
= {
1320 .name
= "infiniband_cm",
1321 .release
= ib_ucm_release_class_dev
1324 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1326 struct ib_ucm_device
*dev
;
1328 dev
= container_of(class_dev
, struct ib_ucm_device
, class_dev
);
1329 return sprintf(buf
, "%s\n", dev
->ib_dev
->name
);
1331 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1333 static void ib_ucm_add_one(struct ib_device
*device
)
1335 struct ib_ucm_device
*ucm_dev
;
1337 if (!device
->alloc_ucontext
)
1340 ucm_dev
= kzalloc(sizeof *ucm_dev
, GFP_KERNEL
);
1344 ucm_dev
->ib_dev
= device
;
1346 ucm_dev
->devnum
= find_first_zero_bit(dev_map
, IB_UCM_MAX_DEVICES
);
1347 if (ucm_dev
->devnum
>= IB_UCM_MAX_DEVICES
)
1350 set_bit(ucm_dev
->devnum
, dev_map
);
1352 cdev_init(&ucm_dev
->dev
, &ucm_fops
);
1353 ucm_dev
->dev
.owner
= THIS_MODULE
;
1354 kobject_set_name(&ucm_dev
->dev
.kobj
, "ucm%d", ucm_dev
->devnum
);
1355 if (cdev_add(&ucm_dev
->dev
, IB_UCM_BASE_DEV
+ ucm_dev
->devnum
, 1))
1358 ucm_dev
->class_dev
.class = &ucm_class
;
1359 ucm_dev
->class_dev
.dev
= device
->dma_device
;
1360 ucm_dev
->class_dev
.devt
= ucm_dev
->dev
.dev
;
1361 snprintf(ucm_dev
->class_dev
.class_id
, BUS_ID_SIZE
, "ucm%d",
1363 if (class_device_register(&ucm_dev
->class_dev
))
1366 if (class_device_create_file(&ucm_dev
->class_dev
,
1367 &class_device_attr_ibdev
))
1370 ib_set_client_data(device
, &ucm_client
, ucm_dev
);
1374 class_device_unregister(&ucm_dev
->class_dev
);
1376 cdev_del(&ucm_dev
->dev
);
1377 clear_bit(ucm_dev
->devnum
, dev_map
);
1383 static void ib_ucm_remove_one(struct ib_device
*device
)
1385 struct ib_ucm_device
*ucm_dev
= ib_get_client_data(device
, &ucm_client
);
1390 class_device_unregister(&ucm_dev
->class_dev
);
1393 static ssize_t
show_abi_version(struct class *class, char *buf
)
1395 return sprintf(buf
, "%d\n", IB_USER_CM_ABI_VERSION
);
1397 static CLASS_ATTR(abi_version
, S_IRUGO
, show_abi_version
, NULL
);
1399 static int __init
ib_ucm_init(void)
1403 ret
= register_chrdev_region(IB_UCM_BASE_DEV
, IB_UCM_MAX_DEVICES
,
1406 printk(KERN_ERR
"ucm: couldn't register device number\n");
1410 ret
= class_register(&ucm_class
);
1412 printk(KERN_ERR
"ucm: couldn't create class infiniband_cm\n");
1416 ret
= class_create_file(&ucm_class
, &class_attr_abi_version
);
1418 printk(KERN_ERR
"ucm: couldn't create abi_version attribute\n");
1422 ret
= ib_register_client(&ucm_client
);
1424 printk(KERN_ERR
"ucm: couldn't register client\n");
1430 class_unregister(&ucm_class
);
1432 unregister_chrdev_region(IB_UCM_BASE_DEV
, IB_UCM_MAX_DEVICES
);
1437 static void __exit
ib_ucm_cleanup(void)
1439 ib_unregister_client(&ucm_client
);
1440 class_unregister(&ucm_class
);
1441 unregister_chrdev_region(IB_UCM_BASE_DEV
, IB_UCM_MAX_DEVICES
);
1442 idr_destroy(&ctx_id_table
);
1445 module_init(ib_ucm_init
);
1446 module_exit(ib_ucm_cleanup
);