2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/completion.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/idr.h>
42 #include <linux/inetdevice.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <net/route.h>
50 #include <rdma/rdma_cm.h>
51 #include <rdma/rdma_cm_ib.h>
52 #include <rdma/rdma_netlink.h>
54 #include <rdma/ib_cache.h>
55 #include <rdma/ib_cm.h>
56 #include <rdma/ib_sa.h>
57 #include <rdma/iw_cm.h>
59 MODULE_AUTHOR("Sean Hefty");
60 MODULE_DESCRIPTION("Generic RDMA CM Agent");
61 MODULE_LICENSE("Dual BSD/GPL");
63 #define CMA_CM_RESPONSE_TIMEOUT 20
64 #define CMA_MAX_CM_RETRIES 15
65 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
66 #define CMA_IBOE_PACKET_LIFETIME 18
68 static void cma_add_one(struct ib_device
*device
);
69 static void cma_remove_one(struct ib_device
*device
);
71 static struct ib_client cma_client
= {
74 .remove
= cma_remove_one
77 static struct ib_sa_client sa_client
;
78 static struct rdma_addr_client addr_client
;
79 static LIST_HEAD(dev_list
);
80 static LIST_HEAD(listen_any_list
);
81 static DEFINE_MUTEX(lock
);
82 static struct workqueue_struct
*cma_wq
;
83 static DEFINE_IDR(tcp_ps
);
84 static DEFINE_IDR(udp_ps
);
85 static DEFINE_IDR(ipoib_ps
);
86 static DEFINE_IDR(ib_ps
);
89 struct list_head list
;
90 struct ib_device
*device
;
91 struct completion comp
;
93 struct list_head id_list
;
96 struct rdma_bind_list
{
98 struct hlist_head owners
;
107 * Device removal can occur at anytime, so we need extra handling to
108 * serialize notifying the user of device removal with other callbacks.
109 * We do this by disabling removal notification while a callback is in process,
110 * and reporting it after the callback completes.
112 struct rdma_id_private
{
113 struct rdma_cm_id id
;
115 struct rdma_bind_list
*bind_list
;
116 struct hlist_node node
;
117 struct list_head list
; /* listen_any_list or cma_device.list */
118 struct list_head listen_list
; /* per device listens */
119 struct cma_device
*cma_dev
;
120 struct list_head mc_list
;
123 enum rdma_cm_state state
;
125 struct mutex qp_mutex
;
127 struct completion comp
;
129 struct mutex handler_mutex
;
133 struct ib_sa_query
*query
;
151 struct cma_multicast
{
152 struct rdma_id_private
*id_priv
;
154 struct ib_sa_multicast
*ib
;
156 struct list_head list
;
158 struct sockaddr_storage addr
;
163 struct work_struct work
;
164 struct rdma_id_private
*id
;
165 enum rdma_cm_state old_state
;
166 enum rdma_cm_state new_state
;
167 struct rdma_cm_event event
;
170 struct cma_ndev_work
{
171 struct work_struct work
;
172 struct rdma_id_private
*id
;
173 struct rdma_cm_event event
;
176 struct iboe_mcast_work
{
177 struct work_struct work
;
178 struct rdma_id_private
*id
;
179 struct cma_multicast
*mc
;
192 u8 ip_version
; /* IP version: 7:4 */
194 union cma_ip_addr src_addr
;
195 union cma_ip_addr dst_addr
;
198 #define CMA_VERSION 0x00
200 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
205 spin_lock_irqsave(&id_priv
->lock
, flags
);
206 ret
= (id_priv
->state
== comp
);
207 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
211 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
212 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
217 spin_lock_irqsave(&id_priv
->lock
, flags
);
218 if ((ret
= (id_priv
->state
== comp
)))
219 id_priv
->state
= exch
;
220 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
224 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
225 enum rdma_cm_state exch
)
228 enum rdma_cm_state old
;
230 spin_lock_irqsave(&id_priv
->lock
, flags
);
231 old
= id_priv
->state
;
232 id_priv
->state
= exch
;
233 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
237 static inline u8
cma_get_ip_ver(struct cma_hdr
*hdr
)
239 return hdr
->ip_version
>> 4;
242 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
244 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
247 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
248 struct cma_device
*cma_dev
)
250 atomic_inc(&cma_dev
->refcount
);
251 id_priv
->cma_dev
= cma_dev
;
252 id_priv
->id
.device
= cma_dev
->device
;
253 id_priv
->id
.route
.addr
.dev_addr
.transport
=
254 rdma_node_get_transport(cma_dev
->device
->node_type
);
255 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
258 static inline void cma_deref_dev(struct cma_device
*cma_dev
)
260 if (atomic_dec_and_test(&cma_dev
->refcount
))
261 complete(&cma_dev
->comp
);
264 static inline void release_mc(struct kref
*kref
)
266 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
268 kfree(mc
->multicast
.ib
);
272 static void cma_release_dev(struct rdma_id_private
*id_priv
)
275 list_del(&id_priv
->list
);
276 cma_deref_dev(id_priv
->cma_dev
);
277 id_priv
->cma_dev
= NULL
;
281 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
283 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
286 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
288 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
291 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
293 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
296 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
298 struct ib_sa_mcmember_rec rec
;
302 if (qkey
&& id_priv
->qkey
!= qkey
)
308 id_priv
->qkey
= qkey
;
312 switch (id_priv
->id
.ps
) {
315 id_priv
->qkey
= RDMA_UDP_QKEY
;
318 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
319 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
320 id_priv
->id
.port_num
, &rec
.mgid
,
323 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
331 static int find_gid_port(struct ib_device
*device
, union ib_gid
*gid
, u8 port_num
)
335 struct ib_port_attr props
;
338 err
= ib_query_port(device
, port_num
, &props
);
342 for (i
= 0; i
< props
.gid_tbl_len
; ++i
) {
343 err
= ib_query_gid(device
, port_num
, i
, &tmp
);
346 if (!memcmp(&tmp
, gid
, sizeof tmp
))
350 return -EADDRNOTAVAIL
;
353 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
355 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
356 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
357 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
360 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
364 if (addr
->sa_family
!= AF_IB
) {
365 ret
= rdma_translate_ip(addr
, dev_addr
);
367 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
374 static int cma_acquire_dev(struct rdma_id_private
*id_priv
)
376 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
377 struct cma_device
*cma_dev
;
378 union ib_gid gid
, iboe_gid
;
381 enum rdma_link_layer dev_ll
= dev_addr
->dev_type
== ARPHRD_INFINIBAND
?
382 IB_LINK_LAYER_INFINIBAND
: IB_LINK_LAYER_ETHERNET
;
384 if (dev_ll
!= IB_LINK_LAYER_INFINIBAND
&&
385 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
389 iboe_addr_get_sgid(dev_addr
, &iboe_gid
);
390 memcpy(&gid
, dev_addr
->src_dev_addr
+
391 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
392 list_for_each_entry(cma_dev
, &dev_list
, list
) {
393 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
394 if (rdma_port_get_link_layer(cma_dev
->device
, port
) == dev_ll
) {
395 if (rdma_node_get_transport(cma_dev
->device
->node_type
) == RDMA_TRANSPORT_IB
&&
396 rdma_port_get_link_layer(cma_dev
->device
, port
) == IB_LINK_LAYER_ETHERNET
)
397 ret
= find_gid_port(cma_dev
->device
, &iboe_gid
, port
);
399 ret
= find_gid_port(cma_dev
->device
, &gid
, port
);
402 id_priv
->id
.port_num
= port
;
411 cma_attach_to_dev(id_priv
, cma_dev
);
418 * Select the source IB device and address to reach the destination IB address.
420 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
422 struct cma_device
*cma_dev
, *cur_dev
;
423 struct sockaddr_ib
*addr
;
424 union ib_gid gid
, sgid
, *dgid
;
430 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
431 dgid
= (union ib_gid
*) &addr
->sib_addr
;
432 pkey
= ntohs(addr
->sib_pkey
);
434 list_for_each_entry(cur_dev
, &dev_list
, list
) {
435 if (rdma_node_get_transport(cur_dev
->device
->node_type
) != RDMA_TRANSPORT_IB
)
438 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
439 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
442 for (i
= 0; !ib_get_cached_gid(cur_dev
->device
, p
, i
, &gid
); i
++) {
443 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
446 id_priv
->id
.port_num
= p
;
450 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
451 dgid
->global
.subnet_prefix
)) {
454 id_priv
->id
.port_num
= p
;
464 cma_attach_to_dev(id_priv
, cma_dev
);
465 addr
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
466 memcpy(&addr
->sib_addr
, &sgid
, sizeof sgid
);
467 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
471 static void cma_deref_id(struct rdma_id_private
*id_priv
)
473 if (atomic_dec_and_test(&id_priv
->refcount
))
474 complete(&id_priv
->comp
);
477 static int cma_disable_callback(struct rdma_id_private
*id_priv
,
478 enum rdma_cm_state state
)
480 mutex_lock(&id_priv
->handler_mutex
);
481 if (id_priv
->state
!= state
) {
482 mutex_unlock(&id_priv
->handler_mutex
);
488 struct rdma_cm_id
*rdma_create_id(rdma_cm_event_handler event_handler
,
489 void *context
, enum rdma_port_space ps
,
490 enum ib_qp_type qp_type
)
492 struct rdma_id_private
*id_priv
;
494 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
496 return ERR_PTR(-ENOMEM
);
498 id_priv
->owner
= task_pid_nr(current
);
499 id_priv
->state
= RDMA_CM_IDLE
;
500 id_priv
->id
.context
= context
;
501 id_priv
->id
.event_handler
= event_handler
;
503 id_priv
->id
.qp_type
= qp_type
;
504 spin_lock_init(&id_priv
->lock
);
505 mutex_init(&id_priv
->qp_mutex
);
506 init_completion(&id_priv
->comp
);
507 atomic_set(&id_priv
->refcount
, 1);
508 mutex_init(&id_priv
->handler_mutex
);
509 INIT_LIST_HEAD(&id_priv
->listen_list
);
510 INIT_LIST_HEAD(&id_priv
->mc_list
);
511 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
515 EXPORT_SYMBOL(rdma_create_id
);
517 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
519 struct ib_qp_attr qp_attr
;
520 int qp_attr_mask
, ret
;
522 qp_attr
.qp_state
= IB_QPS_INIT
;
523 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
527 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
531 qp_attr
.qp_state
= IB_QPS_RTR
;
532 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
536 qp_attr
.qp_state
= IB_QPS_RTS
;
538 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
543 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
545 struct ib_qp_attr qp_attr
;
546 int qp_attr_mask
, ret
;
548 qp_attr
.qp_state
= IB_QPS_INIT
;
549 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
553 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
556 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
557 struct ib_qp_init_attr
*qp_init_attr
)
559 struct rdma_id_private
*id_priv
;
563 id_priv
= container_of(id
, struct rdma_id_private
, id
);
564 if (id
->device
!= pd
->device
)
567 qp
= ib_create_qp(pd
, qp_init_attr
);
571 if (id
->qp_type
== IB_QPT_UD
)
572 ret
= cma_init_ud_qp(id_priv
, qp
);
574 ret
= cma_init_conn_qp(id_priv
, qp
);
579 id_priv
->qp_num
= qp
->qp_num
;
580 id_priv
->srq
= (qp
->srq
!= NULL
);
586 EXPORT_SYMBOL(rdma_create_qp
);
588 void rdma_destroy_qp(struct rdma_cm_id
*id
)
590 struct rdma_id_private
*id_priv
;
592 id_priv
= container_of(id
, struct rdma_id_private
, id
);
593 mutex_lock(&id_priv
->qp_mutex
);
594 ib_destroy_qp(id_priv
->id
.qp
);
595 id_priv
->id
.qp
= NULL
;
596 mutex_unlock(&id_priv
->qp_mutex
);
598 EXPORT_SYMBOL(rdma_destroy_qp
);
600 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
601 struct rdma_conn_param
*conn_param
)
603 struct ib_qp_attr qp_attr
;
604 int qp_attr_mask
, ret
;
606 mutex_lock(&id_priv
->qp_mutex
);
607 if (!id_priv
->id
.qp
) {
612 /* Need to update QP attributes from default values. */
613 qp_attr
.qp_state
= IB_QPS_INIT
;
614 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
618 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
622 qp_attr
.qp_state
= IB_QPS_RTR
;
623 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
628 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
629 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
631 mutex_unlock(&id_priv
->qp_mutex
);
635 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
636 struct rdma_conn_param
*conn_param
)
638 struct ib_qp_attr qp_attr
;
639 int qp_attr_mask
, ret
;
641 mutex_lock(&id_priv
->qp_mutex
);
642 if (!id_priv
->id
.qp
) {
647 qp_attr
.qp_state
= IB_QPS_RTS
;
648 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
653 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
654 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
656 mutex_unlock(&id_priv
->qp_mutex
);
660 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
662 struct ib_qp_attr qp_attr
;
665 mutex_lock(&id_priv
->qp_mutex
);
666 if (!id_priv
->id
.qp
) {
671 qp_attr
.qp_state
= IB_QPS_ERR
;
672 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
674 mutex_unlock(&id_priv
->qp_mutex
);
678 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
679 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
681 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
685 if (rdma_port_get_link_layer(id_priv
->id
.device
, id_priv
->id
.port_num
) ==
686 IB_LINK_LAYER_INFINIBAND
)
687 pkey
= ib_addr_get_pkey(dev_addr
);
691 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
692 pkey
, &qp_attr
->pkey_index
);
696 qp_attr
->port_num
= id_priv
->id
.port_num
;
697 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
699 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
700 ret
= cma_set_qkey(id_priv
, 0);
704 qp_attr
->qkey
= id_priv
->qkey
;
705 *qp_attr_mask
|= IB_QP_QKEY
;
707 qp_attr
->qp_access_flags
= 0;
708 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
713 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
716 struct rdma_id_private
*id_priv
;
719 id_priv
= container_of(id
, struct rdma_id_private
, id
);
720 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
721 case RDMA_TRANSPORT_IB
:
722 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
723 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
725 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
727 if (qp_attr
->qp_state
== IB_QPS_RTR
)
728 qp_attr
->rq_psn
= id_priv
->seq_num
;
730 case RDMA_TRANSPORT_IWARP
:
731 if (!id_priv
->cm_id
.iw
) {
732 qp_attr
->qp_access_flags
= 0;
733 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
735 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
745 EXPORT_SYMBOL(rdma_init_qp_attr
);
747 static inline int cma_zero_addr(struct sockaddr
*addr
)
749 switch (addr
->sa_family
) {
751 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
753 return ipv6_addr_any(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
755 return ib_addr_any(&((struct sockaddr_ib
*) addr
)->sib_addr
);
761 static inline int cma_loopback_addr(struct sockaddr
*addr
)
763 switch (addr
->sa_family
) {
765 return ipv4_is_loopback(((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
767 return ipv6_addr_loopback(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
769 return ib_addr_loopback(&((struct sockaddr_ib
*) addr
)->sib_addr
);
775 static inline int cma_any_addr(struct sockaddr
*addr
)
777 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
780 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
782 if (src
->sa_family
!= dst
->sa_family
)
785 switch (src
->sa_family
) {
787 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
788 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
790 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
791 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
793 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
794 &((struct sockaddr_ib
*) dst
)->sib_addr
);
798 static __be16
cma_port(struct sockaddr
*addr
)
800 struct sockaddr_ib
*sib
;
802 switch (addr
->sa_family
) {
804 return ((struct sockaddr_in
*) addr
)->sin_port
;
806 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
808 sib
= (struct sockaddr_ib
*) addr
;
809 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
810 be64_to_cpu(sib
->sib_sid_mask
)));
816 static inline int cma_any_port(struct sockaddr
*addr
)
818 return !cma_port(addr
);
821 static void cma_save_ib_info(struct rdma_cm_id
*id
, struct rdma_cm_id
*listen_id
,
822 struct ib_sa_path_rec
*path
)
824 struct sockaddr_ib
*listen_ib
, *ib
;
826 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
827 ib
= (struct sockaddr_ib
*) &id
->route
.addr
.src_addr
;
828 ib
->sib_family
= listen_ib
->sib_family
;
829 ib
->sib_pkey
= path
->pkey
;
830 ib
->sib_flowinfo
= path
->flow_label
;
831 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
832 ib
->sib_sid
= listen_ib
->sib_sid
;
833 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
834 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
836 ib
= (struct sockaddr_ib
*) &id
->route
.addr
.dst_addr
;
837 ib
->sib_family
= listen_ib
->sib_family
;
838 ib
->sib_pkey
= path
->pkey
;
839 ib
->sib_flowinfo
= path
->flow_label
;
840 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
843 static void cma_save_ip4_info(struct rdma_cm_id
*id
, struct rdma_cm_id
*listen_id
,
846 struct sockaddr_in
*listen4
, *ip4
;
848 listen4
= (struct sockaddr_in
*) &listen_id
->route
.addr
.src_addr
;
849 ip4
= (struct sockaddr_in
*) &id
->route
.addr
.src_addr
;
850 ip4
->sin_family
= listen4
->sin_family
;
851 ip4
->sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
;
852 ip4
->sin_port
= listen4
->sin_port
;
854 ip4
= (struct sockaddr_in
*) &id
->route
.addr
.dst_addr
;
855 ip4
->sin_family
= listen4
->sin_family
;
856 ip4
->sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
;
857 ip4
->sin_port
= hdr
->port
;
860 static void cma_save_ip6_info(struct rdma_cm_id
*id
, struct rdma_cm_id
*listen_id
,
863 struct sockaddr_in6
*listen6
, *ip6
;
865 listen6
= (struct sockaddr_in6
*) &listen_id
->route
.addr
.src_addr
;
866 ip6
= (struct sockaddr_in6
*) &id
->route
.addr
.src_addr
;
867 ip6
->sin6_family
= listen6
->sin6_family
;
868 ip6
->sin6_addr
= hdr
->dst_addr
.ip6
;
869 ip6
->sin6_port
= listen6
->sin6_port
;
871 ip6
= (struct sockaddr_in6
*) &id
->route
.addr
.dst_addr
;
872 ip6
->sin6_family
= listen6
->sin6_family
;
873 ip6
->sin6_addr
= hdr
->src_addr
.ip6
;
874 ip6
->sin6_port
= hdr
->port
;
877 static int cma_save_net_info(struct rdma_cm_id
*id
, struct rdma_cm_id
*listen_id
,
878 struct ib_cm_event
*ib_event
)
882 if ((listen_id
->route
.addr
.src_addr
.ss_family
== AF_IB
) &&
883 (ib_event
->event
== IB_CM_REQ_RECEIVED
)) {
884 cma_save_ib_info(id
, listen_id
, ib_event
->param
.req_rcvd
.primary_path
);
888 hdr
= ib_event
->private_data
;
889 if (hdr
->cma_version
!= CMA_VERSION
)
892 switch (cma_get_ip_ver(hdr
)) {
894 cma_save_ip4_info(id
, listen_id
, hdr
);
897 cma_save_ip6_info(id
, listen_id
, hdr
);
905 static inline int cma_user_data_offset(struct rdma_id_private
*id_priv
)
907 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
910 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
912 switch (rdma_port_get_link_layer(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
913 case IB_LINK_LAYER_INFINIBAND
:
915 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
922 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
924 struct rdma_id_private
*dev_id_priv
;
927 * Remove from listen_any_list to prevent added devices from spawning
928 * additional listen requests.
931 list_del(&id_priv
->list
);
933 while (!list_empty(&id_priv
->listen_list
)) {
934 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
935 struct rdma_id_private
, listen_list
);
936 /* sync with device removal to avoid duplicate destruction */
937 list_del_init(&dev_id_priv
->list
);
938 list_del(&dev_id_priv
->listen_list
);
941 rdma_destroy_id(&dev_id_priv
->id
);
947 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
948 enum rdma_cm_state state
)
951 case RDMA_CM_ADDR_QUERY
:
952 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
954 case RDMA_CM_ROUTE_QUERY
:
955 cma_cancel_route(id_priv
);
958 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
959 cma_cancel_listens(id_priv
);
966 static void cma_release_port(struct rdma_id_private
*id_priv
)
968 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
974 hlist_del(&id_priv
->node
);
975 if (hlist_empty(&bind_list
->owners
)) {
976 idr_remove(bind_list
->ps
, bind_list
->port
);
982 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
984 struct cma_multicast
*mc
;
986 while (!list_empty(&id_priv
->mc_list
)) {
987 mc
= container_of(id_priv
->mc_list
.next
,
988 struct cma_multicast
, list
);
990 switch (rdma_port_get_link_layer(id_priv
->cma_dev
->device
, id_priv
->id
.port_num
)) {
991 case IB_LINK_LAYER_INFINIBAND
:
992 ib_sa_free_multicast(mc
->multicast
.ib
);
995 case IB_LINK_LAYER_ETHERNET
:
996 kref_put(&mc
->mcref
, release_mc
);
1004 void rdma_destroy_id(struct rdma_cm_id
*id
)
1006 struct rdma_id_private
*id_priv
;
1007 enum rdma_cm_state state
;
1009 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1010 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1011 cma_cancel_operation(id_priv
, state
);
1014 * Wait for any active callback to finish. New callbacks will find
1015 * the id_priv state set to destroying and abort.
1017 mutex_lock(&id_priv
->handler_mutex
);
1018 mutex_unlock(&id_priv
->handler_mutex
);
1020 if (id_priv
->cma_dev
) {
1021 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
1022 case RDMA_TRANSPORT_IB
:
1023 if (id_priv
->cm_id
.ib
)
1024 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1026 case RDMA_TRANSPORT_IWARP
:
1027 if (id_priv
->cm_id
.iw
)
1028 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1033 cma_leave_mc_groups(id_priv
);
1034 cma_release_dev(id_priv
);
1037 cma_release_port(id_priv
);
1038 cma_deref_id(id_priv
);
1039 wait_for_completion(&id_priv
->comp
);
1041 if (id_priv
->internal_id
)
1042 cma_deref_id(id_priv
->id
.context
);
1044 kfree(id_priv
->id
.route
.path_rec
);
1047 EXPORT_SYMBOL(rdma_destroy_id
);
1049 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1053 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1057 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1061 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1067 cma_modify_qp_err(id_priv
);
1068 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1073 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1074 struct ib_cm_rep_event_param
*rep_data
,
1077 event
->param
.conn
.private_data
= private_data
;
1078 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1079 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1080 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1081 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1082 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1083 event
->param
.conn
.srq
= rep_data
->srq
;
1084 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1087 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1089 struct rdma_id_private
*id_priv
= cm_id
->context
;
1090 struct rdma_cm_event event
;
1093 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1094 cma_disable_callback(id_priv
, RDMA_CM_CONNECT
)) ||
1095 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1096 cma_disable_callback(id_priv
, RDMA_CM_DISCONNECT
)))
1099 memset(&event
, 0, sizeof event
);
1100 switch (ib_event
->event
) {
1101 case IB_CM_REQ_ERROR
:
1102 case IB_CM_REP_ERROR
:
1103 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1104 event
.status
= -ETIMEDOUT
;
1106 case IB_CM_REP_RECEIVED
:
1107 if (id_priv
->id
.qp
) {
1108 event
.status
= cma_rep_recv(id_priv
);
1109 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1110 RDMA_CM_EVENT_ESTABLISHED
;
1112 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1114 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1115 ib_event
->private_data
);
1117 case IB_CM_RTU_RECEIVED
:
1118 case IB_CM_USER_ESTABLISHED
:
1119 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1121 case IB_CM_DREQ_ERROR
:
1122 event
.status
= -ETIMEDOUT
; /* fall through */
1123 case IB_CM_DREQ_RECEIVED
:
1124 case IB_CM_DREP_RECEIVED
:
1125 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1126 RDMA_CM_DISCONNECT
))
1128 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1130 case IB_CM_TIMEWAIT_EXIT
:
1131 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1133 case IB_CM_MRA_RECEIVED
:
1136 case IB_CM_REJ_RECEIVED
:
1137 cma_modify_qp_err(id_priv
);
1138 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1139 event
.event
= RDMA_CM_EVENT_REJECTED
;
1140 event
.param
.conn
.private_data
= ib_event
->private_data
;
1141 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1144 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
1149 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1151 /* Destroy the CM ID by returning a non-zero value. */
1152 id_priv
->cm_id
.ib
= NULL
;
1153 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1154 mutex_unlock(&id_priv
->handler_mutex
);
1155 rdma_destroy_id(&id_priv
->id
);
1159 mutex_unlock(&id_priv
->handler_mutex
);
1163 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1164 struct ib_cm_event
*ib_event
)
1166 struct rdma_id_private
*id_priv
;
1167 struct rdma_cm_id
*id
;
1168 struct rdma_route
*rt
;
1171 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1172 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
);
1176 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1177 if (cma_save_net_info(id
, listen_id
, ib_event
))
1181 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1182 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1187 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1188 if (rt
->num_paths
== 2)
1189 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1191 if (cma_any_addr(cma_src_addr(id_priv
))) {
1192 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1193 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1194 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1196 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
1200 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1202 id_priv
->state
= RDMA_CM_CONNECT
;
1206 rdma_destroy_id(id
);
1210 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1211 struct ib_cm_event
*ib_event
)
1213 struct rdma_id_private
*id_priv
;
1214 struct rdma_cm_id
*id
;
1217 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1218 listen_id
->ps
, IB_QPT_UD
);
1222 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1223 if (cma_save_net_info(id
, listen_id
, ib_event
))
1226 if (!cma_any_addr((struct sockaddr
*) &id
->route
.addr
.src_addr
)) {
1227 ret
= cma_translate_addr(cma_src_addr(id_priv
), &id
->route
.addr
.dev_addr
);
1232 id_priv
->state
= RDMA_CM_CONNECT
;
1235 rdma_destroy_id(id
);
1239 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1240 struct ib_cm_req_event_param
*req_data
,
1241 void *private_data
, int offset
)
1243 event
->param
.conn
.private_data
= private_data
+ offset
;
1244 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1245 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1246 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1247 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1248 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1249 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1250 event
->param
.conn
.srq
= req_data
->srq
;
1251 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1254 static int cma_check_req_qp_type(struct rdma_cm_id
*id
, struct ib_cm_event
*ib_event
)
1256 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
1257 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
1258 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
1259 (id
->qp_type
== IB_QPT_UD
)) ||
1263 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1265 struct rdma_id_private
*listen_id
, *conn_id
;
1266 struct rdma_cm_event event
;
1269 listen_id
= cm_id
->context
;
1270 if (!cma_check_req_qp_type(&listen_id
->id
, ib_event
))
1273 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
))
1274 return -ECONNABORTED
;
1276 memset(&event
, 0, sizeof event
);
1277 offset
= cma_user_data_offset(listen_id
);
1278 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1279 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
1280 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
);
1281 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1282 event
.param
.ud
.private_data_len
=
1283 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1285 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
);
1286 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1287 ib_event
->private_data
, offset
);
1294 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1295 ret
= cma_acquire_dev(conn_id
);
1299 conn_id
->cm_id
.ib
= cm_id
;
1300 cm_id
->context
= conn_id
;
1301 cm_id
->cm_handler
= cma_ib_handler
;
1304 * Protect against the user destroying conn_id from another thread
1305 * until we're done accessing it.
1307 atomic_inc(&conn_id
->refcount
);
1308 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1313 * Acquire mutex to prevent user executing rdma_destroy_id()
1314 * while we're accessing the cm_id.
1317 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) && (conn_id
->id
.qp_type
!= IB_QPT_UD
))
1318 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1319 mutex_unlock(&lock
);
1320 mutex_unlock(&conn_id
->handler_mutex
);
1321 mutex_unlock(&listen_id
->handler_mutex
);
1322 cma_deref_id(conn_id
);
1326 cma_deref_id(conn_id
);
1327 /* Destroy the CM ID by returning a non-zero value. */
1328 conn_id
->cm_id
.ib
= NULL
;
1330 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1331 mutex_unlock(&conn_id
->handler_mutex
);
1333 mutex_unlock(&listen_id
->handler_mutex
);
1335 rdma_destroy_id(&conn_id
->id
);
1339 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
1341 if (addr
->sa_family
== AF_IB
)
1342 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
1344 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
1346 EXPORT_SYMBOL(rdma_get_service_id
);
1348 static void cma_set_compare_data(enum rdma_port_space ps
, struct sockaddr
*addr
,
1349 struct ib_cm_compare_data
*compare
)
1351 struct cma_hdr
*cma_data
, *cma_mask
;
1353 struct in6_addr ip6_addr
;
1355 memset(compare
, 0, sizeof *compare
);
1356 cma_data
= (void *) compare
->data
;
1357 cma_mask
= (void *) compare
->mask
;
1359 switch (addr
->sa_family
) {
1361 ip4_addr
= ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
;
1362 cma_set_ip_ver(cma_data
, 4);
1363 cma_set_ip_ver(cma_mask
, 0xF);
1364 if (!cma_any_addr(addr
)) {
1365 cma_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1366 cma_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1370 ip6_addr
= ((struct sockaddr_in6
*) addr
)->sin6_addr
;
1371 cma_set_ip_ver(cma_data
, 6);
1372 cma_set_ip_ver(cma_mask
, 0xF);
1373 if (!cma_any_addr(addr
)) {
1374 cma_data
->dst_addr
.ip6
= ip6_addr
;
1375 memset(&cma_mask
->dst_addr
.ip6
, 0xFF,
1376 sizeof cma_mask
->dst_addr
.ip6
);
1384 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
1386 struct rdma_id_private
*id_priv
= iw_id
->context
;
1387 struct rdma_cm_event event
;
1389 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
1390 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
1392 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
1395 memset(&event
, 0, sizeof event
);
1396 switch (iw_event
->event
) {
1397 case IW_CM_EVENT_CLOSE
:
1398 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1400 case IW_CM_EVENT_CONNECT_REPLY
:
1401 memcpy(cma_src_addr(id_priv
), laddr
,
1402 rdma_addr_size(laddr
));
1403 memcpy(cma_dst_addr(id_priv
), raddr
,
1404 rdma_addr_size(raddr
));
1405 switch (iw_event
->status
) {
1407 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1408 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1409 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1413 event
.event
= RDMA_CM_EVENT_REJECTED
;
1416 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1419 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1423 case IW_CM_EVENT_ESTABLISHED
:
1424 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1425 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1426 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1432 event
.status
= iw_event
->status
;
1433 event
.param
.conn
.private_data
= iw_event
->private_data
;
1434 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1435 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1437 /* Destroy the CM ID by returning a non-zero value. */
1438 id_priv
->cm_id
.iw
= NULL
;
1439 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1440 mutex_unlock(&id_priv
->handler_mutex
);
1441 rdma_destroy_id(&id_priv
->id
);
1445 mutex_unlock(&id_priv
->handler_mutex
);
1449 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
1450 struct iw_cm_event
*iw_event
)
1452 struct rdma_cm_id
*new_cm_id
;
1453 struct rdma_id_private
*listen_id
, *conn_id
;
1454 struct net_device
*dev
= NULL
;
1455 struct rdma_cm_event event
;
1457 struct ib_device_attr attr
;
1458 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
1459 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
1461 listen_id
= cm_id
->context
;
1462 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
))
1463 return -ECONNABORTED
;
1465 /* Create a new RDMA id for the new IW CM ID */
1466 new_cm_id
= rdma_create_id(listen_id
->id
.event_handler
,
1467 listen_id
->id
.context
,
1468 RDMA_PS_TCP
, IB_QPT_RC
);
1469 if (IS_ERR(new_cm_id
)) {
1473 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
1474 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1475 conn_id
->state
= RDMA_CM_CONNECT
;
1477 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
);
1479 mutex_unlock(&conn_id
->handler_mutex
);
1480 rdma_destroy_id(new_cm_id
);
1484 ret
= cma_acquire_dev(conn_id
);
1486 mutex_unlock(&conn_id
->handler_mutex
);
1487 rdma_destroy_id(new_cm_id
);
1491 conn_id
->cm_id
.iw
= cm_id
;
1492 cm_id
->context
= conn_id
;
1493 cm_id
->cm_handler
= cma_iw_handler
;
1495 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
1496 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
1498 ret
= ib_query_device(conn_id
->id
.device
, &attr
);
1500 mutex_unlock(&conn_id
->handler_mutex
);
1501 rdma_destroy_id(new_cm_id
);
1505 memset(&event
, 0, sizeof event
);
1506 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1507 event
.param
.conn
.private_data
= iw_event
->private_data
;
1508 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1509 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1510 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1513 * Protect against the user destroying conn_id from another thread
1514 * until we're done accessing it.
1516 atomic_inc(&conn_id
->refcount
);
1517 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1519 /* User wants to destroy the CM ID */
1520 conn_id
->cm_id
.iw
= NULL
;
1521 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1522 mutex_unlock(&conn_id
->handler_mutex
);
1523 cma_deref_id(conn_id
);
1524 rdma_destroy_id(&conn_id
->id
);
1528 mutex_unlock(&conn_id
->handler_mutex
);
1529 cma_deref_id(conn_id
);
1534 mutex_unlock(&listen_id
->handler_mutex
);
1538 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
1540 struct ib_cm_compare_data compare_data
;
1541 struct sockaddr
*addr
;
1542 struct ib_cm_id
*id
;
1546 id
= ib_create_cm_id(id_priv
->id
.device
, cma_req_handler
, id_priv
);
1550 id_priv
->cm_id
.ib
= id
;
1552 addr
= cma_src_addr(id_priv
);
1553 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
1554 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1555 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, NULL
);
1557 cma_set_compare_data(id_priv
->id
.ps
, addr
, &compare_data
);
1558 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, &compare_data
);
1562 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1563 id_priv
->cm_id
.ib
= NULL
;
1569 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
1572 struct iw_cm_id
*id
;
1574 id
= iw_create_cm_id(id_priv
->id
.device
,
1575 iw_conn_req_handler
,
1580 id_priv
->cm_id
.iw
= id
;
1582 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
1583 rdma_addr_size(cma_src_addr(id_priv
)));
1585 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
1588 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1589 id_priv
->cm_id
.iw
= NULL
;
1595 static int cma_listen_handler(struct rdma_cm_id
*id
,
1596 struct rdma_cm_event
*event
)
1598 struct rdma_id_private
*id_priv
= id
->context
;
1600 id
->context
= id_priv
->id
.context
;
1601 id
->event_handler
= id_priv
->id
.event_handler
;
1602 return id_priv
->id
.event_handler(id
, event
);
1605 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
1606 struct cma_device
*cma_dev
)
1608 struct rdma_id_private
*dev_id_priv
;
1609 struct rdma_cm_id
*id
;
1612 if (cma_family(id_priv
) == AF_IB
&&
1613 rdma_node_get_transport(cma_dev
->device
->node_type
) != RDMA_TRANSPORT_IB
)
1616 id
= rdma_create_id(cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
1617 id_priv
->id
.qp_type
);
1621 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
1623 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
1624 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
1625 rdma_addr_size(cma_src_addr(id_priv
)));
1627 cma_attach_to_dev(dev_id_priv
, cma_dev
);
1628 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
1629 atomic_inc(&id_priv
->refcount
);
1630 dev_id_priv
->internal_id
= 1;
1631 dev_id_priv
->afonly
= id_priv
->afonly
;
1633 ret
= rdma_listen(id
, id_priv
->backlog
);
1635 printk(KERN_WARNING
"RDMA CMA: cma_listen_on_dev, error %d, "
1636 "listening on device %s\n", ret
, cma_dev
->device
->name
);
1639 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
1641 struct cma_device
*cma_dev
;
1644 list_add_tail(&id_priv
->list
, &listen_any_list
);
1645 list_for_each_entry(cma_dev
, &dev_list
, list
)
1646 cma_listen_on_dev(id_priv
, cma_dev
);
1647 mutex_unlock(&lock
);
1650 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
1652 struct rdma_id_private
*id_priv
;
1654 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1655 id_priv
->tos
= (u8
) tos
;
1657 EXPORT_SYMBOL(rdma_set_service_type
);
1659 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
1662 struct cma_work
*work
= context
;
1663 struct rdma_route
*route
;
1665 route
= &work
->id
->id
.route
;
1668 route
->num_paths
= 1;
1669 *route
->path_rec
= *path_rec
;
1671 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1672 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
1673 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
1674 work
->event
.status
= status
;
1677 queue_work(cma_wq
, &work
->work
);
1680 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
1681 struct cma_work
*work
)
1683 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
1684 struct ib_sa_path_rec path_rec
;
1685 ib_sa_comp_mask comp_mask
;
1686 struct sockaddr_in6
*sin6
;
1687 struct sockaddr_ib
*sib
;
1689 memset(&path_rec
, 0, sizeof path_rec
);
1690 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
1691 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
1692 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
1693 path_rec
.numb_path
= 1;
1694 path_rec
.reversible
= 1;
1695 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
1697 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
1698 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
1699 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
1701 switch (cma_family(id_priv
)) {
1703 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
1704 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
1707 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
1708 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
1709 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
1712 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
1713 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
1714 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
1718 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
1719 id_priv
->id
.port_num
, &path_rec
,
1720 comp_mask
, timeout_ms
,
1721 GFP_KERNEL
, cma_query_handler
,
1722 work
, &id_priv
->query
);
1724 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
1727 static void cma_work_handler(struct work_struct
*_work
)
1729 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
1730 struct rdma_id_private
*id_priv
= work
->id
;
1733 mutex_lock(&id_priv
->handler_mutex
);
1734 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
1737 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1738 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1742 mutex_unlock(&id_priv
->handler_mutex
);
1743 cma_deref_id(id_priv
);
1745 rdma_destroy_id(&id_priv
->id
);
1749 static void cma_ndev_work_handler(struct work_struct
*_work
)
1751 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
1752 struct rdma_id_private
*id_priv
= work
->id
;
1755 mutex_lock(&id_priv
->handler_mutex
);
1756 if (id_priv
->state
== RDMA_CM_DESTROYING
||
1757 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
1760 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1761 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1766 mutex_unlock(&id_priv
->handler_mutex
);
1767 cma_deref_id(id_priv
);
1769 rdma_destroy_id(&id_priv
->id
);
1773 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1775 struct rdma_route
*route
= &id_priv
->id
.route
;
1776 struct cma_work
*work
;
1779 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1784 INIT_WORK(&work
->work
, cma_work_handler
);
1785 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1786 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
1787 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1789 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1790 if (!route
->path_rec
) {
1795 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
1801 kfree(route
->path_rec
);
1802 route
->path_rec
= NULL
;
1808 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
1809 struct ib_sa_path_rec
*path_rec
, int num_paths
)
1811 struct rdma_id_private
*id_priv
;
1814 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1815 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
1816 RDMA_CM_ROUTE_RESOLVED
))
1819 id
->route
.path_rec
= kmemdup(path_rec
, sizeof *path_rec
* num_paths
,
1821 if (!id
->route
.path_rec
) {
1826 id
->route
.num_paths
= num_paths
;
1829 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
1832 EXPORT_SYMBOL(rdma_set_ib_paths
);
1834 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1836 struct cma_work
*work
;
1838 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1843 INIT_WORK(&work
->work
, cma_work_handler
);
1844 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1845 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
1846 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1847 queue_work(cma_wq
, &work
->work
);
1851 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
1853 struct rdma_route
*route
= &id_priv
->id
.route
;
1854 struct rdma_addr
*addr
= &route
->addr
;
1855 struct cma_work
*work
;
1857 struct net_device
*ndev
= NULL
;
1860 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1865 INIT_WORK(&work
->work
, cma_work_handler
);
1867 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1868 if (!route
->path_rec
) {
1873 route
->num_paths
= 1;
1875 if (addr
->dev_addr
.bound_dev_if
)
1876 ndev
= dev_get_by_index(&init_net
, addr
->dev_addr
.bound_dev_if
);
1882 vid
= rdma_vlan_dev_vlan_id(ndev
);
1884 iboe_mac_vlan_to_ll(&route
->path_rec
->sgid
, addr
->dev_addr
.src_dev_addr
, vid
);
1885 iboe_mac_vlan_to_ll(&route
->path_rec
->dgid
, addr
->dev_addr
.dst_dev_addr
, vid
);
1887 route
->path_rec
->hop_limit
= 1;
1888 route
->path_rec
->reversible
= 1;
1889 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
1890 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
1891 route
->path_rec
->sl
= netdev_get_prio_tc_map(
1892 ndev
->priv_flags
& IFF_802_1Q_VLAN
?
1893 vlan_dev_real_dev(ndev
) : ndev
,
1894 rt_tos2priority(id_priv
->tos
));
1896 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
1897 route
->path_rec
->rate_selector
= IB_SA_EQ
;
1898 route
->path_rec
->rate
= iboe_get_rate(ndev
);
1900 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
1901 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
1902 if (!route
->path_rec
->mtu
) {
1907 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1908 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
1909 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1910 work
->event
.status
= 0;
1912 queue_work(cma_wq
, &work
->work
);
1917 kfree(route
->path_rec
);
1918 route
->path_rec
= NULL
;
1924 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
1926 struct rdma_id_private
*id_priv
;
1929 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1930 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
1933 atomic_inc(&id_priv
->refcount
);
1934 switch (rdma_node_get_transport(id
->device
->node_type
)) {
1935 case RDMA_TRANSPORT_IB
:
1936 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
1937 case IB_LINK_LAYER_INFINIBAND
:
1938 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
1940 case IB_LINK_LAYER_ETHERNET
:
1941 ret
= cma_resolve_iboe_route(id_priv
);
1947 case RDMA_TRANSPORT_IWARP
:
1948 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
1959 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
1960 cma_deref_id(id_priv
);
1963 EXPORT_SYMBOL(rdma_resolve_route
);
1965 static void cma_set_loopback(struct sockaddr
*addr
)
1967 switch (addr
->sa_family
) {
1969 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1972 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
1976 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
1982 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
1984 struct cma_device
*cma_dev
, *cur_dev
;
1985 struct ib_port_attr port_attr
;
1993 list_for_each_entry(cur_dev
, &dev_list
, list
) {
1994 if (cma_family(id_priv
) == AF_IB
&&
1995 rdma_node_get_transport(cur_dev
->device
->node_type
) != RDMA_TRANSPORT_IB
)
2001 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
2002 if (!ib_query_port(cur_dev
->device
, p
, &port_attr
) &&
2003 port_attr
.state
== IB_PORT_ACTIVE
) {
2018 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
);
2022 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
2026 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
2027 (rdma_port_get_link_layer(cma_dev
->device
, p
) == IB_LINK_LAYER_INFINIBAND
) ?
2028 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
2030 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2031 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
2032 id_priv
->id
.port_num
= p
;
2033 cma_attach_to_dev(id_priv
, cma_dev
);
2034 cma_set_loopback(cma_src_addr(id_priv
));
2036 mutex_unlock(&lock
);
2040 static void addr_handler(int status
, struct sockaddr
*src_addr
,
2041 struct rdma_dev_addr
*dev_addr
, void *context
)
2043 struct rdma_id_private
*id_priv
= context
;
2044 struct rdma_cm_event event
;
2046 memset(&event
, 0, sizeof event
);
2047 mutex_lock(&id_priv
->handler_mutex
);
2048 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
2049 RDMA_CM_ADDR_RESOLVED
))
2052 if (!status
&& !id_priv
->cma_dev
)
2053 status
= cma_acquire_dev(id_priv
);
2056 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2057 RDMA_CM_ADDR_BOUND
))
2059 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2060 event
.status
= status
;
2062 memcpy(cma_src_addr(id_priv
), src_addr
, rdma_addr_size(src_addr
));
2063 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2066 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
2067 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2068 mutex_unlock(&id_priv
->handler_mutex
);
2069 cma_deref_id(id_priv
);
2070 rdma_destroy_id(&id_priv
->id
);
2074 mutex_unlock(&id_priv
->handler_mutex
);
2075 cma_deref_id(id_priv
);
2078 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
2080 struct cma_work
*work
;
2084 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2088 if (!id_priv
->cma_dev
) {
2089 ret
= cma_bind_loopback(id_priv
);
2094 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2095 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2098 INIT_WORK(&work
->work
, cma_work_handler
);
2099 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2100 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2101 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2102 queue_work(cma_wq
, &work
->work
);
2109 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
2111 struct cma_work
*work
;
2114 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2118 if (!id_priv
->cma_dev
) {
2119 ret
= cma_resolve_ib_dev(id_priv
);
2124 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
2125 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
2128 INIT_WORK(&work
->work
, cma_work_handler
);
2129 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2130 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2131 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2132 queue_work(cma_wq
, &work
->work
);
2139 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2140 struct sockaddr
*dst_addr
)
2142 if (!src_addr
|| !src_addr
->sa_family
) {
2143 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2144 src_addr
->sa_family
= dst_addr
->sa_family
;
2145 if (dst_addr
->sa_family
== AF_INET6
) {
2146 ((struct sockaddr_in6
*) src_addr
)->sin6_scope_id
=
2147 ((struct sockaddr_in6
*) dst_addr
)->sin6_scope_id
;
2148 } else if (dst_addr
->sa_family
== AF_IB
) {
2149 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
2150 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
2153 return rdma_bind_addr(id
, src_addr
);
2156 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2157 struct sockaddr
*dst_addr
, int timeout_ms
)
2159 struct rdma_id_private
*id_priv
;
2162 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2163 if (id_priv
->state
== RDMA_CM_IDLE
) {
2164 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2169 if (cma_family(id_priv
) != dst_addr
->sa_family
)
2172 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
))
2175 atomic_inc(&id_priv
->refcount
);
2176 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
2177 if (cma_any_addr(dst_addr
)) {
2178 ret
= cma_resolve_loopback(id_priv
);
2180 if (dst_addr
->sa_family
== AF_IB
) {
2181 ret
= cma_resolve_ib_addr(id_priv
);
2183 ret
= rdma_resolve_ip(&addr_client
, cma_src_addr(id_priv
),
2184 dst_addr
, &id
->route
.addr
.dev_addr
,
2185 timeout_ms
, addr_handler
, id_priv
);
2193 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2194 cma_deref_id(id_priv
);
2197 EXPORT_SYMBOL(rdma_resolve_addr
);
2199 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2201 struct rdma_id_private
*id_priv
;
2202 unsigned long flags
;
2205 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2206 spin_lock_irqsave(&id_priv
->lock
, flags
);
2207 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
2208 id_priv
->reuseaddr
= reuse
;
2213 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2216 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2218 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
2220 struct rdma_id_private
*id_priv
;
2221 unsigned long flags
;
2224 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2225 spin_lock_irqsave(&id_priv
->lock
, flags
);
2226 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
2227 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
2228 id_priv
->afonly
= afonly
;
2233 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2236 EXPORT_SYMBOL(rdma_set_afonly
);
2238 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
2239 struct rdma_id_private
*id_priv
)
2241 struct sockaddr
*addr
;
2242 struct sockaddr_ib
*sib
;
2246 addr
= cma_src_addr(id_priv
);
2247 port
= htons(bind_list
->port
);
2249 switch (addr
->sa_family
) {
2251 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
2254 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
2257 sib
= (struct sockaddr_ib
*) addr
;
2258 sid
= be64_to_cpu(sib
->sib_sid
);
2259 mask
= be64_to_cpu(sib
->sib_sid_mask
);
2260 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
2261 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
2264 id_priv
->bind_list
= bind_list
;
2265 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
2268 static int cma_alloc_port(struct idr
*ps
, struct rdma_id_private
*id_priv
,
2269 unsigned short snum
)
2271 struct rdma_bind_list
*bind_list
;
2274 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
2278 ret
= idr_alloc(ps
, bind_list
, snum
, snum
+ 1, GFP_KERNEL
);
2283 bind_list
->port
= (unsigned short)ret
;
2284 cma_bind_port(bind_list
, id_priv
);
2288 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
2291 static int cma_alloc_any_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
2293 static unsigned int last_used_port
;
2294 int low
, high
, remaining
;
2297 inet_get_local_port_range(&low
, &high
);
2298 remaining
= (high
- low
) + 1;
2299 rover
= net_random() % remaining
+ low
;
2301 if (last_used_port
!= rover
&&
2302 !idr_find(ps
, (unsigned short) rover
)) {
2303 int ret
= cma_alloc_port(ps
, id_priv
, rover
);
2305 * Remember previously used port number in order to avoid
2306 * re-using same port immediately after it is closed.
2309 last_used_port
= rover
;
2310 if (ret
!= -EADDRNOTAVAIL
)
2315 if ((rover
< low
) || (rover
> high
))
2319 return -EADDRNOTAVAIL
;
2323 * Check that the requested port is available. This is called when trying to
2324 * bind to a specific port, or when trying to listen on a bound port. In
2325 * the latter case, the provided id_priv may already be on the bind_list, but
2326 * we still need to check that it's okay to start listening.
2328 static int cma_check_port(struct rdma_bind_list
*bind_list
,
2329 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
2331 struct rdma_id_private
*cur_id
;
2332 struct sockaddr
*addr
, *cur_addr
;
2334 addr
= cma_src_addr(id_priv
);
2335 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
2336 if (id_priv
== cur_id
)
2339 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
2343 cur_addr
= cma_src_addr(cur_id
);
2344 if (id_priv
->afonly
&& cur_id
->afonly
&&
2345 (addr
->sa_family
!= cur_addr
->sa_family
))
2348 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
2349 return -EADDRNOTAVAIL
;
2351 if (!cma_addr_cmp(addr
, cur_addr
))
2357 static int cma_use_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
2359 struct rdma_bind_list
*bind_list
;
2360 unsigned short snum
;
2363 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
2364 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
2367 bind_list
= idr_find(ps
, snum
);
2369 ret
= cma_alloc_port(ps
, id_priv
, snum
);
2371 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
2373 cma_bind_port(bind_list
, id_priv
);
2378 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
2380 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
2384 if (bind_list
->owners
.first
->next
)
2385 ret
= cma_check_port(bind_list
, id_priv
, 0);
2386 mutex_unlock(&lock
);
2390 static struct idr
*cma_select_inet_ps(struct rdma_id_private
*id_priv
)
2392 switch (id_priv
->id
.ps
) {
2406 static struct idr
*cma_select_ib_ps(struct rdma_id_private
*id_priv
)
2408 struct idr
*ps
= NULL
;
2409 struct sockaddr_ib
*sib
;
2410 u64 sid_ps
, mask
, sid
;
2412 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2413 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
2414 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
2416 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
2417 sid_ps
= RDMA_IB_IP_PS_IB
;
2419 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
2420 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
2421 sid_ps
= RDMA_IB_IP_PS_TCP
;
2423 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
2424 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
2425 sid_ps
= RDMA_IB_IP_PS_UDP
;
2430 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
2431 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
2432 be64_to_cpu(sib
->sib_sid_mask
));
2437 static int cma_get_port(struct rdma_id_private
*id_priv
)
2442 if (cma_family(id_priv
) != AF_IB
)
2443 ps
= cma_select_inet_ps(id_priv
);
2445 ps
= cma_select_ib_ps(id_priv
);
2447 return -EPROTONOSUPPORT
;
2450 if (cma_any_port(cma_src_addr(id_priv
)))
2451 ret
= cma_alloc_any_port(ps
, id_priv
);
2453 ret
= cma_use_port(ps
, id_priv
);
2454 mutex_unlock(&lock
);
2459 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
2460 struct sockaddr
*addr
)
2462 #if IS_ENABLED(CONFIG_IPV6)
2463 struct sockaddr_in6
*sin6
;
2465 if (addr
->sa_family
!= AF_INET6
)
2468 sin6
= (struct sockaddr_in6
*) addr
;
2469 if ((ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
) &&
2470 !sin6
->sin6_scope_id
)
2473 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
2478 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
2480 struct rdma_id_private
*id_priv
;
2483 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2484 if (id_priv
->state
== RDMA_CM_IDLE
) {
2485 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
2486 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
2491 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
2494 if (id_priv
->reuseaddr
) {
2495 ret
= cma_bind_listen(id_priv
);
2500 id_priv
->backlog
= backlog
;
2502 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2503 case RDMA_TRANSPORT_IB
:
2504 ret
= cma_ib_listen(id_priv
);
2508 case RDMA_TRANSPORT_IWARP
:
2509 ret
= cma_iw_listen(id_priv
, backlog
);
2518 cma_listen_on_all(id_priv
);
2522 id_priv
->backlog
= 0;
2523 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
2526 EXPORT_SYMBOL(rdma_listen
);
2528 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2530 struct rdma_id_private
*id_priv
;
2533 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
2534 addr
->sa_family
!= AF_IB
)
2535 return -EAFNOSUPPORT
;
2537 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2538 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
2541 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
2545 if (!cma_any_addr(addr
)) {
2546 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
2550 ret
= cma_acquire_dev(id_priv
);
2555 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
2556 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
2557 if (addr
->sa_family
== AF_INET
)
2558 id_priv
->afonly
= 1;
2559 #if IS_ENABLED(CONFIG_IPV6)
2560 else if (addr
->sa_family
== AF_INET6
)
2561 id_priv
->afonly
= init_net
.ipv6
.sysctl
.bindv6only
;
2564 ret
= cma_get_port(id_priv
);
2570 if (id_priv
->cma_dev
)
2571 cma_release_dev(id_priv
);
2573 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
2576 EXPORT_SYMBOL(rdma_bind_addr
);
2578 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
2580 struct cma_hdr
*cma_hdr
;
2583 cma_hdr
->cma_version
= CMA_VERSION
;
2584 if (cma_family(id_priv
) == AF_INET
) {
2585 struct sockaddr_in
*src4
, *dst4
;
2587 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
2588 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
2590 cma_set_ip_ver(cma_hdr
, 4);
2591 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2592 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2593 cma_hdr
->port
= src4
->sin_port
;
2594 } else if (cma_family(id_priv
) == AF_INET6
) {
2595 struct sockaddr_in6
*src6
, *dst6
;
2597 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2598 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
2600 cma_set_ip_ver(cma_hdr
, 6);
2601 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
2602 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
2603 cma_hdr
->port
= src6
->sin6_port
;
2608 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
2609 struct ib_cm_event
*ib_event
)
2611 struct rdma_id_private
*id_priv
= cm_id
->context
;
2612 struct rdma_cm_event event
;
2613 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
2616 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
2619 memset(&event
, 0, sizeof event
);
2620 switch (ib_event
->event
) {
2621 case IB_CM_SIDR_REQ_ERROR
:
2622 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2623 event
.status
= -ETIMEDOUT
;
2625 case IB_CM_SIDR_REP_RECEIVED
:
2626 event
.param
.ud
.private_data
= ib_event
->private_data
;
2627 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
2628 if (rep
->status
!= IB_SIDR_SUCCESS
) {
2629 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2630 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
2633 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
2635 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2639 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
2640 id_priv
->id
.route
.path_rec
,
2641 &event
.param
.ud
.ah_attr
);
2642 event
.param
.ud
.qp_num
= rep
->qpn
;
2643 event
.param
.ud
.qkey
= rep
->qkey
;
2644 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2648 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
2653 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2655 /* Destroy the CM ID by returning a non-zero value. */
2656 id_priv
->cm_id
.ib
= NULL
;
2657 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2658 mutex_unlock(&id_priv
->handler_mutex
);
2659 rdma_destroy_id(&id_priv
->id
);
2663 mutex_unlock(&id_priv
->handler_mutex
);
2667 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
2668 struct rdma_conn_param
*conn_param
)
2670 struct ib_cm_sidr_req_param req
;
2671 struct ib_cm_id
*id
;
2675 memset(&req
, 0, sizeof req
);
2676 offset
= cma_user_data_offset(id_priv
);
2677 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
2678 if (req
.private_data_len
< conn_param
->private_data_len
)
2681 if (req
.private_data_len
) {
2682 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2686 private_data
= NULL
;
2689 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2690 memcpy(private_data
+ offset
, conn_param
->private_data
,
2691 conn_param
->private_data_len
);
2694 ret
= cma_format_hdr(private_data
, id_priv
);
2697 req
.private_data
= private_data
;
2700 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
2706 id_priv
->cm_id
.ib
= id
;
2708 req
.path
= id_priv
->id
.route
.path_rec
;
2709 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
2710 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
2711 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2713 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
2715 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
2716 id_priv
->cm_id
.ib
= NULL
;
2719 kfree(private_data
);
2723 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
2724 struct rdma_conn_param
*conn_param
)
2726 struct ib_cm_req_param req
;
2727 struct rdma_route
*route
;
2729 struct ib_cm_id
*id
;
2732 memset(&req
, 0, sizeof req
);
2733 offset
= cma_user_data_offset(id_priv
);
2734 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
2735 if (req
.private_data_len
< conn_param
->private_data_len
)
2738 if (req
.private_data_len
) {
2739 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2743 private_data
= NULL
;
2746 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2747 memcpy(private_data
+ offset
, conn_param
->private_data
,
2748 conn_param
->private_data_len
);
2750 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
2755 id_priv
->cm_id
.ib
= id
;
2757 route
= &id_priv
->id
.route
;
2759 ret
= cma_format_hdr(private_data
, id_priv
);
2762 req
.private_data
= private_data
;
2765 req
.primary_path
= &route
->path_rec
[0];
2766 if (route
->num_paths
== 2)
2767 req
.alternate_path
= &route
->path_rec
[1];
2769 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
2770 req
.qp_num
= id_priv
->qp_num
;
2771 req
.qp_type
= id_priv
->id
.qp_type
;
2772 req
.starting_psn
= id_priv
->seq_num
;
2773 req
.responder_resources
= conn_param
->responder_resources
;
2774 req
.initiator_depth
= conn_param
->initiator_depth
;
2775 req
.flow_control
= conn_param
->flow_control
;
2776 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
2777 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
2778 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2779 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2780 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2781 req
.srq
= id_priv
->srq
? 1 : 0;
2783 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
2785 if (ret
&& !IS_ERR(id
)) {
2786 ib_destroy_cm_id(id
);
2787 id_priv
->cm_id
.ib
= NULL
;
2790 kfree(private_data
);
2794 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
2795 struct rdma_conn_param
*conn_param
)
2797 struct iw_cm_id
*cm_id
;
2799 struct iw_cm_conn_param iw_param
;
2801 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
2803 return PTR_ERR(cm_id
);
2805 id_priv
->cm_id
.iw
= cm_id
;
2807 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
2808 rdma_addr_size(cma_src_addr(id_priv
)));
2809 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
2810 rdma_addr_size(cma_dst_addr(id_priv
)));
2812 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2817 iw_param
.ord
= conn_param
->initiator_depth
;
2818 iw_param
.ird
= conn_param
->responder_resources
;
2819 iw_param
.private_data
= conn_param
->private_data
;
2820 iw_param
.private_data_len
= conn_param
->private_data_len
;
2821 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
2823 memset(&iw_param
, 0, sizeof iw_param
);
2824 iw_param
.qpn
= id_priv
->qp_num
;
2826 ret
= iw_cm_connect(cm_id
, &iw_param
);
2829 iw_destroy_cm_id(cm_id
);
2830 id_priv
->cm_id
.iw
= NULL
;
2835 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2837 struct rdma_id_private
*id_priv
;
2840 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2841 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
2845 id_priv
->qp_num
= conn_param
->qp_num
;
2846 id_priv
->srq
= conn_param
->srq
;
2849 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2850 case RDMA_TRANSPORT_IB
:
2851 if (id
->qp_type
== IB_QPT_UD
)
2852 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
2854 ret
= cma_connect_ib(id_priv
, conn_param
);
2856 case RDMA_TRANSPORT_IWARP
:
2857 ret
= cma_connect_iw(id_priv
, conn_param
);
2868 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
2871 EXPORT_SYMBOL(rdma_connect
);
2873 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
2874 struct rdma_conn_param
*conn_param
)
2876 struct ib_cm_rep_param rep
;
2879 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2883 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
2887 memset(&rep
, 0, sizeof rep
);
2888 rep
.qp_num
= id_priv
->qp_num
;
2889 rep
.starting_psn
= id_priv
->seq_num
;
2890 rep
.private_data
= conn_param
->private_data
;
2891 rep
.private_data_len
= conn_param
->private_data_len
;
2892 rep
.responder_resources
= conn_param
->responder_resources
;
2893 rep
.initiator_depth
= conn_param
->initiator_depth
;
2894 rep
.failover_accepted
= 0;
2895 rep
.flow_control
= conn_param
->flow_control
;
2896 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
2897 rep
.srq
= id_priv
->srq
? 1 : 0;
2899 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
2904 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
2905 struct rdma_conn_param
*conn_param
)
2907 struct iw_cm_conn_param iw_param
;
2910 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2914 iw_param
.ord
= conn_param
->initiator_depth
;
2915 iw_param
.ird
= conn_param
->responder_resources
;
2916 iw_param
.private_data
= conn_param
->private_data
;
2917 iw_param
.private_data_len
= conn_param
->private_data_len
;
2918 if (id_priv
->id
.qp
) {
2919 iw_param
.qpn
= id_priv
->qp_num
;
2921 iw_param
.qpn
= conn_param
->qp_num
;
2923 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
2926 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
2927 enum ib_cm_sidr_status status
, u32 qkey
,
2928 const void *private_data
, int private_data_len
)
2930 struct ib_cm_sidr_rep_param rep
;
2933 memset(&rep
, 0, sizeof rep
);
2934 rep
.status
= status
;
2935 if (status
== IB_SIDR_SUCCESS
) {
2936 ret
= cma_set_qkey(id_priv
, qkey
);
2939 rep
.qp_num
= id_priv
->qp_num
;
2940 rep
.qkey
= id_priv
->qkey
;
2942 rep
.private_data
= private_data
;
2943 rep
.private_data_len
= private_data_len
;
2945 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
2948 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2950 struct rdma_id_private
*id_priv
;
2953 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2955 id_priv
->owner
= task_pid_nr(current
);
2957 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
2960 if (!id
->qp
&& conn_param
) {
2961 id_priv
->qp_num
= conn_param
->qp_num
;
2962 id_priv
->srq
= conn_param
->srq
;
2965 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2966 case RDMA_TRANSPORT_IB
:
2967 if (id
->qp_type
== IB_QPT_UD
) {
2969 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
2971 conn_param
->private_data
,
2972 conn_param
->private_data_len
);
2974 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
2978 ret
= cma_accept_ib(id_priv
, conn_param
);
2980 ret
= cma_rep_recv(id_priv
);
2983 case RDMA_TRANSPORT_IWARP
:
2984 ret
= cma_accept_iw(id_priv
, conn_param
);
2996 cma_modify_qp_err(id_priv
);
2997 rdma_reject(id
, NULL
, 0);
3000 EXPORT_SYMBOL(rdma_accept
);
3002 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
3004 struct rdma_id_private
*id_priv
;
3007 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3008 if (!id_priv
->cm_id
.ib
)
3011 switch (id
->device
->node_type
) {
3012 case RDMA_NODE_IB_CA
:
3013 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
3021 EXPORT_SYMBOL(rdma_notify
);
3023 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
3024 u8 private_data_len
)
3026 struct rdma_id_private
*id_priv
;
3029 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3030 if (!id_priv
->cm_id
.ib
)
3033 switch (rdma_node_get_transport(id
->device
->node_type
)) {
3034 case RDMA_TRANSPORT_IB
:
3035 if (id
->qp_type
== IB_QPT_UD
)
3036 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
3037 private_data
, private_data_len
);
3039 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
3040 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
3041 0, private_data
, private_data_len
);
3043 case RDMA_TRANSPORT_IWARP
:
3044 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
3045 private_data
, private_data_len
);
3053 EXPORT_SYMBOL(rdma_reject
);
3055 int rdma_disconnect(struct rdma_cm_id
*id
)
3057 struct rdma_id_private
*id_priv
;
3060 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3061 if (!id_priv
->cm_id
.ib
)
3064 switch (rdma_node_get_transport(id
->device
->node_type
)) {
3065 case RDMA_TRANSPORT_IB
:
3066 ret
= cma_modify_qp_err(id_priv
);
3069 /* Initiate or respond to a disconnect. */
3070 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
3071 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
3073 case RDMA_TRANSPORT_IWARP
:
3074 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
3083 EXPORT_SYMBOL(rdma_disconnect
);
3085 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
3087 struct rdma_id_private
*id_priv
;
3088 struct cma_multicast
*mc
= multicast
->context
;
3089 struct rdma_cm_event event
;
3092 id_priv
= mc
->id_priv
;
3093 if (cma_disable_callback(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3094 cma_disable_callback(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3098 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
3099 mutex_lock(&id_priv
->qp_mutex
);
3100 if (!status
&& id_priv
->id
.qp
)
3101 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
3102 be16_to_cpu(multicast
->rec
.mlid
));
3103 mutex_unlock(&id_priv
->qp_mutex
);
3105 memset(&event
, 0, sizeof event
);
3106 event
.status
= status
;
3107 event
.param
.ud
.private_data
= mc
->context
;
3109 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
3110 ib_init_ah_from_mcmember(id_priv
->id
.device
,
3111 id_priv
->id
.port_num
, &multicast
->rec
,
3112 &event
.param
.ud
.ah_attr
);
3113 event
.param
.ud
.qp_num
= 0xFFFFFF;
3114 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
3116 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3118 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3120 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3121 mutex_unlock(&id_priv
->handler_mutex
);
3122 rdma_destroy_id(&id_priv
->id
);
3126 mutex_unlock(&id_priv
->handler_mutex
);
3130 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
3131 struct sockaddr
*addr
, union ib_gid
*mgid
)
3133 unsigned char mc_map
[MAX_ADDR_LEN
];
3134 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3135 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
3136 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
3138 if (cma_any_addr(addr
)) {
3139 memset(mgid
, 0, sizeof *mgid
);
3140 } else if ((addr
->sa_family
== AF_INET6
) &&
3141 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
3143 /* IPv6 address is an SA assigned MGID. */
3144 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3145 } else if (addr
->sa_family
== AF_IB
) {
3146 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
3147 } else if ((addr
->sa_family
== AF_INET6
)) {
3148 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
3149 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3150 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3151 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3153 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
3154 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3155 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3156 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3160 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
3161 struct cma_multicast
*mc
)
3163 struct ib_sa_mcmember_rec rec
;
3164 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3165 ib_sa_comp_mask comp_mask
;
3168 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
3169 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
3174 ret
= cma_set_qkey(id_priv
, 0);
3178 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
3179 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
3180 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
3181 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
3184 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
3185 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
3186 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
3187 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
3188 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
3190 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
3191 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
3192 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
3193 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
3194 IB_SA_MCMEMBER_REC_MTU
|
3195 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
3197 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
3198 id_priv
->id
.port_num
, &rec
,
3199 comp_mask
, GFP_KERNEL
,
3200 cma_ib_mc_handler
, mc
);
3201 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
3204 static void iboe_mcast_work_handler(struct work_struct
*work
)
3206 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
3207 struct cma_multicast
*mc
= mw
->mc
;
3208 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
3210 mc
->multicast
.ib
->context
= mc
;
3211 cma_ib_mc_handler(0, m
);
3212 kref_put(&mc
->mcref
, release_mc
);
3216 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
)
3218 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
3219 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
3221 if (cma_any_addr(addr
)) {
3222 memset(mgid
, 0, sizeof *mgid
);
3223 } else if (addr
->sa_family
== AF_INET6
) {
3224 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3226 mgid
->raw
[0] = 0xff;
3227 mgid
->raw
[1] = 0x0e;
3236 mgid
->raw
[10] = 0xff;
3237 mgid
->raw
[11] = 0xff;
3238 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
3242 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
3243 struct cma_multicast
*mc
)
3245 struct iboe_mcast_work
*work
;
3246 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3248 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
3249 struct net_device
*ndev
= NULL
;
3251 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
3254 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3258 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
3259 if (!mc
->multicast
.ib
) {
3264 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
);
3266 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
3267 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3268 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
3270 if (dev_addr
->bound_dev_if
)
3271 ndev
= dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
3276 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
3277 mc
->multicast
.ib
->rec
.hop_limit
= 1;
3278 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
3280 if (!mc
->multicast
.ib
->rec
.mtu
) {
3284 iboe_addr_get_sgid(dev_addr
, &mc
->multicast
.ib
->rec
.port_gid
);
3287 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
3288 kref_get(&mc
->mcref
);
3289 queue_work(cma_wq
, &work
->work
);
3294 kfree(mc
->multicast
.ib
);
3300 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
3303 struct rdma_id_private
*id_priv
;
3304 struct cma_multicast
*mc
;
3307 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3308 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3309 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3312 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
3316 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
3317 mc
->context
= context
;
3318 mc
->id_priv
= id_priv
;
3320 spin_lock(&id_priv
->lock
);
3321 list_add(&mc
->list
, &id_priv
->mc_list
);
3322 spin_unlock(&id_priv
->lock
);
3324 switch (rdma_node_get_transport(id
->device
->node_type
)) {
3325 case RDMA_TRANSPORT_IB
:
3326 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
3327 case IB_LINK_LAYER_INFINIBAND
:
3328 ret
= cma_join_ib_multicast(id_priv
, mc
);
3330 case IB_LINK_LAYER_ETHERNET
:
3331 kref_init(&mc
->mcref
);
3332 ret
= cma_iboe_join_multicast(id_priv
, mc
);
3344 spin_lock_irq(&id_priv
->lock
);
3345 list_del(&mc
->list
);
3346 spin_unlock_irq(&id_priv
->lock
);
3351 EXPORT_SYMBOL(rdma_join_multicast
);
3353 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3355 struct rdma_id_private
*id_priv
;
3356 struct cma_multicast
*mc
;
3358 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3359 spin_lock_irq(&id_priv
->lock
);
3360 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
3361 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
3362 list_del(&mc
->list
);
3363 spin_unlock_irq(&id_priv
->lock
);
3366 ib_detach_mcast(id
->qp
,
3367 &mc
->multicast
.ib
->rec
.mgid
,
3368 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
3369 if (rdma_node_get_transport(id_priv
->cma_dev
->device
->node_type
) == RDMA_TRANSPORT_IB
) {
3370 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
3371 case IB_LINK_LAYER_INFINIBAND
:
3372 ib_sa_free_multicast(mc
->multicast
.ib
);
3375 case IB_LINK_LAYER_ETHERNET
:
3376 kref_put(&mc
->mcref
, release_mc
);
3385 spin_unlock_irq(&id_priv
->lock
);
3387 EXPORT_SYMBOL(rdma_leave_multicast
);
3389 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
3391 struct rdma_dev_addr
*dev_addr
;
3392 struct cma_ndev_work
*work
;
3394 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3396 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
3397 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
3398 printk(KERN_INFO
"RDMA CM addr change for ndev %s used by id %p\n",
3399 ndev
->name
, &id_priv
->id
);
3400 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3404 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
3406 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
3407 atomic_inc(&id_priv
->refcount
);
3408 queue_work(cma_wq
, &work
->work
);
3414 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
3417 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
3418 struct cma_device
*cma_dev
;
3419 struct rdma_id_private
*id_priv
;
3420 int ret
= NOTIFY_DONE
;
3422 if (dev_net(ndev
) != &init_net
)
3425 if (event
!= NETDEV_BONDING_FAILOVER
)
3428 if (!(ndev
->flags
& IFF_MASTER
) || !(ndev
->priv_flags
& IFF_BONDING
))
3432 list_for_each_entry(cma_dev
, &dev_list
, list
)
3433 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3434 ret
= cma_netdev_change(ndev
, id_priv
);
3440 mutex_unlock(&lock
);
3444 static struct notifier_block cma_nb
= {
3445 .notifier_call
= cma_netdev_callback
3448 static void cma_add_one(struct ib_device
*device
)
3450 struct cma_device
*cma_dev
;
3451 struct rdma_id_private
*id_priv
;
3453 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
3457 cma_dev
->device
= device
;
3459 init_completion(&cma_dev
->comp
);
3460 atomic_set(&cma_dev
->refcount
, 1);
3461 INIT_LIST_HEAD(&cma_dev
->id_list
);
3462 ib_set_client_data(device
, &cma_client
, cma_dev
);
3465 list_add_tail(&cma_dev
->list
, &dev_list
);
3466 list_for_each_entry(id_priv
, &listen_any_list
, list
)
3467 cma_listen_on_dev(id_priv
, cma_dev
);
3468 mutex_unlock(&lock
);
3471 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
3473 struct rdma_cm_event event
;
3474 enum rdma_cm_state state
;
3477 /* Record that we want to remove the device */
3478 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
3479 if (state
== RDMA_CM_DESTROYING
)
3482 cma_cancel_operation(id_priv
, state
);
3483 mutex_lock(&id_priv
->handler_mutex
);
3485 /* Check for destruction from another callback. */
3486 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
3489 memset(&event
, 0, sizeof event
);
3490 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
3491 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3493 mutex_unlock(&id_priv
->handler_mutex
);
3497 static void cma_process_remove(struct cma_device
*cma_dev
)
3499 struct rdma_id_private
*id_priv
;
3503 while (!list_empty(&cma_dev
->id_list
)) {
3504 id_priv
= list_entry(cma_dev
->id_list
.next
,
3505 struct rdma_id_private
, list
);
3507 list_del(&id_priv
->listen_list
);
3508 list_del_init(&id_priv
->list
);
3509 atomic_inc(&id_priv
->refcount
);
3510 mutex_unlock(&lock
);
3512 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
3513 cma_deref_id(id_priv
);
3515 rdma_destroy_id(&id_priv
->id
);
3519 mutex_unlock(&lock
);
3521 cma_deref_dev(cma_dev
);
3522 wait_for_completion(&cma_dev
->comp
);
3525 static void cma_remove_one(struct ib_device
*device
)
3527 struct cma_device
*cma_dev
;
3529 cma_dev
= ib_get_client_data(device
, &cma_client
);
3534 list_del(&cma_dev
->list
);
3535 mutex_unlock(&lock
);
3537 cma_process_remove(cma_dev
);
3541 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3543 struct nlmsghdr
*nlh
;
3544 struct rdma_cm_id_stats
*id_stats
;
3545 struct rdma_id_private
*id_priv
;
3546 struct rdma_cm_id
*id
= NULL
;
3547 struct cma_device
*cma_dev
;
3548 int i_dev
= 0, i_id
= 0;
3551 * We export all of the IDs as a sequence of messages. Each
3552 * ID gets its own netlink message.
3556 list_for_each_entry(cma_dev
, &dev_list
, list
) {
3557 if (i_dev
< cb
->args
[0]) {
3563 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3564 if (i_id
< cb
->args
[1]) {
3569 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
3570 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
3571 RDMA_NL_RDMA_CM_ID_STATS
);
3575 memset(id_stats
, 0, sizeof *id_stats
);
3577 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
3578 id_stats
->port_num
= id
->port_num
;
3579 id_stats
->bound_dev_if
=
3580 id
->route
.addr
.dev_addr
.bound_dev_if
;
3582 if (ibnl_put_attr(skb
, nlh
,
3583 rdma_addr_size(cma_src_addr(id_priv
)),
3584 cma_src_addr(id_priv
),
3585 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
))
3587 if (ibnl_put_attr(skb
, nlh
,
3588 rdma_addr_size(cma_src_addr(id_priv
)),
3589 cma_dst_addr(id_priv
),
3590 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
))
3593 id_stats
->pid
= id_priv
->owner
;
3594 id_stats
->port_space
= id
->ps
;
3595 id_stats
->cm_state
= id_priv
->state
;
3596 id_stats
->qp_num
= id_priv
->qp_num
;
3597 id_stats
->qp_type
= id
->qp_type
;
3607 mutex_unlock(&lock
);
3608 cb
->args
[0] = i_dev
;
3614 static const struct ibnl_client_cbs cma_cb_table
[] = {
3615 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
,
3616 .module
= THIS_MODULE
},
3619 static int __init
cma_init(void)
3623 cma_wq
= create_singlethread_workqueue("rdma_cm");
3627 ib_sa_register_client(&sa_client
);
3628 rdma_addr_register_client(&addr_client
);
3629 register_netdevice_notifier(&cma_nb
);
3631 ret
= ib_register_client(&cma_client
);
3635 if (ibnl_add_client(RDMA_NL_RDMA_CM
, RDMA_NL_RDMA_CM_NUM_OPS
, cma_cb_table
))
3636 printk(KERN_WARNING
"RDMA CMA: failed to add netlink callback\n");
3641 unregister_netdevice_notifier(&cma_nb
);
3642 rdma_addr_unregister_client(&addr_client
);
3643 ib_sa_unregister_client(&sa_client
);
3644 destroy_workqueue(cma_wq
);
3648 static void __exit
cma_cleanup(void)
3650 ibnl_remove_client(RDMA_NL_RDMA_CM
);
3651 ib_unregister_client(&cma_client
);
3652 unregister_netdevice_notifier(&cma_nb
);
3653 rdma_addr_unregister_client(&addr_client
);
3654 ib_sa_unregister_client(&sa_client
);
3655 destroy_workqueue(cma_wq
);
3656 idr_destroy(&tcp_ps
);
3657 idr_destroy(&udp_ps
);
3658 idr_destroy(&ipoib_ps
);
3659 idr_destroy(&ib_ps
);
3662 module_init(cma_init
);
3663 module_exit(cma_cleanup
);