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>
48 #include <rdma/rdma_cm.h>
49 #include <rdma/rdma_cm_ib.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_cm.h>
52 #include <rdma/ib_sa.h>
53 #include <rdma/iw_cm.h>
55 MODULE_AUTHOR("Sean Hefty");
56 MODULE_DESCRIPTION("Generic RDMA CM Agent");
57 MODULE_LICENSE("Dual BSD/GPL");
59 #define CMA_CM_RESPONSE_TIMEOUT 20
60 #define CMA_MAX_CM_RETRIES 15
61 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
62 #define CMA_IBOE_PACKET_LIFETIME 18
64 static void cma_add_one(struct ib_device
*device
);
65 static void cma_remove_one(struct ib_device
*device
);
67 static struct ib_client cma_client
= {
70 .remove
= cma_remove_one
73 static struct ib_sa_client sa_client
;
74 static struct rdma_addr_client addr_client
;
75 static LIST_HEAD(dev_list
);
76 static LIST_HEAD(listen_any_list
);
77 static DEFINE_MUTEX(lock
);
78 static struct workqueue_struct
*cma_wq
;
79 static DEFINE_IDR(sdp_ps
);
80 static DEFINE_IDR(tcp_ps
);
81 static DEFINE_IDR(udp_ps
);
82 static DEFINE_IDR(ipoib_ps
);
85 struct list_head list
;
86 struct ib_device
*device
;
87 struct completion comp
;
89 struct list_head id_list
;
106 struct rdma_bind_list
{
108 struct hlist_head owners
;
113 * Device removal can occur at anytime, so we need extra handling to
114 * serialize notifying the user of device removal with other callbacks.
115 * We do this by disabling removal notification while a callback is in process,
116 * and reporting it after the callback completes.
118 struct rdma_id_private
{
119 struct rdma_cm_id id
;
121 struct rdma_bind_list
*bind_list
;
122 struct hlist_node node
;
123 struct list_head list
; /* listen_any_list or cma_device.list */
124 struct list_head listen_list
; /* per device listens */
125 struct cma_device
*cma_dev
;
126 struct list_head mc_list
;
129 enum cma_state state
;
131 struct mutex qp_mutex
;
133 struct completion comp
;
135 struct mutex handler_mutex
;
139 struct ib_sa_query
*query
;
154 struct cma_multicast
{
155 struct rdma_id_private
*id_priv
;
157 struct ib_sa_multicast
*ib
;
159 struct list_head list
;
161 struct sockaddr_storage addr
;
166 struct work_struct work
;
167 struct rdma_id_private
*id
;
168 enum cma_state old_state
;
169 enum cma_state new_state
;
170 struct rdma_cm_event event
;
173 struct cma_ndev_work
{
174 struct work_struct work
;
175 struct rdma_id_private
*id
;
176 struct rdma_cm_event event
;
179 struct iboe_mcast_work
{
180 struct work_struct work
;
181 struct rdma_id_private
*id
;
182 struct cma_multicast
*mc
;
195 u8 ip_version
; /* IP version: 7:4 */
197 union cma_ip_addr src_addr
;
198 union cma_ip_addr dst_addr
;
203 u8 sdp_version
; /* Major version: 7:4 */
204 u8 ip_version
; /* IP version: 7:4 */
205 u8 sdp_specific1
[10];
207 __be16 sdp_specific2
;
208 union cma_ip_addr src_addr
;
209 union cma_ip_addr dst_addr
;
217 #define CMA_VERSION 0x00
218 #define SDP_MAJ_VERSION 0x2
220 static int cma_comp(struct rdma_id_private
*id_priv
, enum cma_state comp
)
225 spin_lock_irqsave(&id_priv
->lock
, flags
);
226 ret
= (id_priv
->state
== comp
);
227 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
231 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
232 enum cma_state comp
, enum cma_state exch
)
237 spin_lock_irqsave(&id_priv
->lock
, flags
);
238 if ((ret
= (id_priv
->state
== comp
)))
239 id_priv
->state
= exch
;
240 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
244 static enum cma_state
cma_exch(struct rdma_id_private
*id_priv
,
250 spin_lock_irqsave(&id_priv
->lock
, flags
);
251 old
= id_priv
->state
;
252 id_priv
->state
= exch
;
253 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
257 static inline u8
cma_get_ip_ver(struct cma_hdr
*hdr
)
259 return hdr
->ip_version
>> 4;
262 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
264 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
267 static inline u8
sdp_get_majv(u8 sdp_version
)
269 return sdp_version
>> 4;
272 static inline u8
sdp_get_ip_ver(struct sdp_hh
*hh
)
274 return hh
->ip_version
>> 4;
277 static inline void sdp_set_ip_ver(struct sdp_hh
*hh
, u8 ip_ver
)
279 hh
->ip_version
= (ip_ver
<< 4) | (hh
->ip_version
& 0xF);
282 static inline int cma_is_ud_ps(enum rdma_port_space ps
)
284 return (ps
== RDMA_PS_UDP
|| ps
== RDMA_PS_IPOIB
);
287 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
288 struct cma_device
*cma_dev
)
290 atomic_inc(&cma_dev
->refcount
);
291 id_priv
->cma_dev
= cma_dev
;
292 id_priv
->id
.device
= cma_dev
->device
;
293 id_priv
->id
.route
.addr
.dev_addr
.transport
=
294 rdma_node_get_transport(cma_dev
->device
->node_type
);
295 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
298 static inline void cma_deref_dev(struct cma_device
*cma_dev
)
300 if (atomic_dec_and_test(&cma_dev
->refcount
))
301 complete(&cma_dev
->comp
);
304 static inline void release_mc(struct kref
*kref
)
306 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
308 kfree(mc
->multicast
.ib
);
312 static void cma_release_dev(struct rdma_id_private
*id_priv
)
315 list_del(&id_priv
->list
);
316 cma_deref_dev(id_priv
->cma_dev
);
317 id_priv
->cma_dev
= NULL
;
321 static int cma_set_qkey(struct rdma_id_private
*id_priv
)
323 struct ib_sa_mcmember_rec rec
;
329 switch (id_priv
->id
.ps
) {
331 id_priv
->qkey
= RDMA_UDP_QKEY
;
334 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
335 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
336 id_priv
->id
.port_num
, &rec
.mgid
,
339 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
347 static int find_gid_port(struct ib_device
*device
, union ib_gid
*gid
, u8 port_num
)
351 struct ib_port_attr props
;
354 err
= ib_query_port(device
, port_num
, &props
);
358 for (i
= 0; i
< props
.gid_tbl_len
; ++i
) {
359 err
= ib_query_gid(device
, port_num
, i
, &tmp
);
362 if (!memcmp(&tmp
, gid
, sizeof tmp
))
369 static int cma_acquire_dev(struct rdma_id_private
*id_priv
)
371 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
372 struct cma_device
*cma_dev
;
373 union ib_gid gid
, iboe_gid
;
376 enum rdma_link_layer dev_ll
= dev_addr
->dev_type
== ARPHRD_INFINIBAND
?
377 IB_LINK_LAYER_INFINIBAND
: IB_LINK_LAYER_ETHERNET
;
380 iboe_addr_get_sgid(dev_addr
, &iboe_gid
);
381 memcpy(&gid
, dev_addr
->src_dev_addr
+
382 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
383 list_for_each_entry(cma_dev
, &dev_list
, list
) {
384 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
385 if (rdma_port_get_link_layer(cma_dev
->device
, port
) == dev_ll
) {
386 if (rdma_node_get_transport(cma_dev
->device
->node_type
) == RDMA_TRANSPORT_IB
&&
387 rdma_port_get_link_layer(cma_dev
->device
, port
) == IB_LINK_LAYER_ETHERNET
)
388 ret
= find_gid_port(cma_dev
->device
, &iboe_gid
, port
);
390 ret
= find_gid_port(cma_dev
->device
, &gid
, port
);
393 id_priv
->id
.port_num
= port
;
403 cma_attach_to_dev(id_priv
, cma_dev
);
409 static void cma_deref_id(struct rdma_id_private
*id_priv
)
411 if (atomic_dec_and_test(&id_priv
->refcount
))
412 complete(&id_priv
->comp
);
415 static int cma_disable_callback(struct rdma_id_private
*id_priv
,
416 enum cma_state state
)
418 mutex_lock(&id_priv
->handler_mutex
);
419 if (id_priv
->state
!= state
) {
420 mutex_unlock(&id_priv
->handler_mutex
);
426 static int cma_has_cm_dev(struct rdma_id_private
*id_priv
)
428 return (id_priv
->id
.device
&& id_priv
->cm_id
.ib
);
431 struct rdma_cm_id
*rdma_create_id(rdma_cm_event_handler event_handler
,
432 void *context
, enum rdma_port_space ps
)
434 struct rdma_id_private
*id_priv
;
436 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
438 return ERR_PTR(-ENOMEM
);
440 id_priv
->state
= CMA_IDLE
;
441 id_priv
->id
.context
= context
;
442 id_priv
->id
.event_handler
= event_handler
;
444 spin_lock_init(&id_priv
->lock
);
445 mutex_init(&id_priv
->qp_mutex
);
446 init_completion(&id_priv
->comp
);
447 atomic_set(&id_priv
->refcount
, 1);
448 mutex_init(&id_priv
->handler_mutex
);
449 INIT_LIST_HEAD(&id_priv
->listen_list
);
450 INIT_LIST_HEAD(&id_priv
->mc_list
);
451 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
455 EXPORT_SYMBOL(rdma_create_id
);
457 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
459 struct ib_qp_attr qp_attr
;
460 int qp_attr_mask
, ret
;
462 qp_attr
.qp_state
= IB_QPS_INIT
;
463 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
467 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
471 qp_attr
.qp_state
= IB_QPS_RTR
;
472 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
476 qp_attr
.qp_state
= IB_QPS_RTS
;
478 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
483 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
485 struct ib_qp_attr qp_attr
;
486 int qp_attr_mask
, ret
;
488 qp_attr
.qp_state
= IB_QPS_INIT
;
489 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
493 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
496 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
497 struct ib_qp_init_attr
*qp_init_attr
)
499 struct rdma_id_private
*id_priv
;
503 id_priv
= container_of(id
, struct rdma_id_private
, id
);
504 if (id
->device
!= pd
->device
)
507 qp
= ib_create_qp(pd
, qp_init_attr
);
511 if (cma_is_ud_ps(id_priv
->id
.ps
))
512 ret
= cma_init_ud_qp(id_priv
, qp
);
514 ret
= cma_init_conn_qp(id_priv
, qp
);
519 id_priv
->qp_num
= qp
->qp_num
;
520 id_priv
->srq
= (qp
->srq
!= NULL
);
526 EXPORT_SYMBOL(rdma_create_qp
);
528 void rdma_destroy_qp(struct rdma_cm_id
*id
)
530 struct rdma_id_private
*id_priv
;
532 id_priv
= container_of(id
, struct rdma_id_private
, id
);
533 mutex_lock(&id_priv
->qp_mutex
);
534 ib_destroy_qp(id_priv
->id
.qp
);
535 id_priv
->id
.qp
= NULL
;
536 mutex_unlock(&id_priv
->qp_mutex
);
538 EXPORT_SYMBOL(rdma_destroy_qp
);
540 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
541 struct rdma_conn_param
*conn_param
)
543 struct ib_qp_attr qp_attr
;
544 int qp_attr_mask
, ret
;
546 mutex_lock(&id_priv
->qp_mutex
);
547 if (!id_priv
->id
.qp
) {
552 /* Need to update QP attributes from default values. */
553 qp_attr
.qp_state
= IB_QPS_INIT
;
554 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
558 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
562 qp_attr
.qp_state
= IB_QPS_RTR
;
563 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
568 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
569 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
571 mutex_unlock(&id_priv
->qp_mutex
);
575 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
576 struct rdma_conn_param
*conn_param
)
578 struct ib_qp_attr qp_attr
;
579 int qp_attr_mask
, ret
;
581 mutex_lock(&id_priv
->qp_mutex
);
582 if (!id_priv
->id
.qp
) {
587 qp_attr
.qp_state
= IB_QPS_RTS
;
588 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
593 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
594 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
596 mutex_unlock(&id_priv
->qp_mutex
);
600 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
602 struct ib_qp_attr qp_attr
;
605 mutex_lock(&id_priv
->qp_mutex
);
606 if (!id_priv
->id
.qp
) {
611 qp_attr
.qp_state
= IB_QPS_ERR
;
612 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
614 mutex_unlock(&id_priv
->qp_mutex
);
618 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
619 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
621 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
625 if (rdma_port_get_link_layer(id_priv
->id
.device
, id_priv
->id
.port_num
) ==
626 IB_LINK_LAYER_INFINIBAND
)
627 pkey
= ib_addr_get_pkey(dev_addr
);
631 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
632 pkey
, &qp_attr
->pkey_index
);
636 qp_attr
->port_num
= id_priv
->id
.port_num
;
637 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
639 if (cma_is_ud_ps(id_priv
->id
.ps
)) {
640 ret
= cma_set_qkey(id_priv
);
644 qp_attr
->qkey
= id_priv
->qkey
;
645 *qp_attr_mask
|= IB_QP_QKEY
;
647 qp_attr
->qp_access_flags
= 0;
648 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
653 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
656 struct rdma_id_private
*id_priv
;
659 id_priv
= container_of(id
, struct rdma_id_private
, id
);
660 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
661 case RDMA_TRANSPORT_IB
:
662 if (!id_priv
->cm_id
.ib
|| cma_is_ud_ps(id_priv
->id
.ps
))
663 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
665 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
667 if (qp_attr
->qp_state
== IB_QPS_RTR
)
668 qp_attr
->rq_psn
= id_priv
->seq_num
;
670 case RDMA_TRANSPORT_IWARP
:
671 if (!id_priv
->cm_id
.iw
) {
672 qp_attr
->qp_access_flags
= 0;
673 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
675 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
685 EXPORT_SYMBOL(rdma_init_qp_attr
);
687 static inline int cma_zero_addr(struct sockaddr
*addr
)
689 struct in6_addr
*ip6
;
691 if (addr
->sa_family
== AF_INET
)
692 return ipv4_is_zeronet(
693 ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
695 ip6
= &((struct sockaddr_in6
*) addr
)->sin6_addr
;
696 return (ip6
->s6_addr32
[0] | ip6
->s6_addr32
[1] |
697 ip6
->s6_addr32
[2] | ip6
->s6_addr32
[3]) == 0;
701 static inline int cma_loopback_addr(struct sockaddr
*addr
)
703 if (addr
->sa_family
== AF_INET
)
704 return ipv4_is_loopback(
705 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
707 return ipv6_addr_loopback(
708 &((struct sockaddr_in6
*) addr
)->sin6_addr
);
711 static inline int cma_any_addr(struct sockaddr
*addr
)
713 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
716 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
718 if (src
->sa_family
!= dst
->sa_family
)
721 switch (src
->sa_family
) {
723 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
724 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
726 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
727 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
731 static inline __be16
cma_port(struct sockaddr
*addr
)
733 if (addr
->sa_family
== AF_INET
)
734 return ((struct sockaddr_in
*) addr
)->sin_port
;
736 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
739 static inline int cma_any_port(struct sockaddr
*addr
)
741 return !cma_port(addr
);
744 static int cma_get_net_info(void *hdr
, enum rdma_port_space ps
,
745 u8
*ip_ver
, __be16
*port
,
746 union cma_ip_addr
**src
, union cma_ip_addr
**dst
)
750 if (sdp_get_majv(((struct sdp_hh
*) hdr
)->sdp_version
) !=
754 *ip_ver
= sdp_get_ip_ver(hdr
);
755 *port
= ((struct sdp_hh
*) hdr
)->port
;
756 *src
= &((struct sdp_hh
*) hdr
)->src_addr
;
757 *dst
= &((struct sdp_hh
*) hdr
)->dst_addr
;
760 if (((struct cma_hdr
*) hdr
)->cma_version
!= CMA_VERSION
)
763 *ip_ver
= cma_get_ip_ver(hdr
);
764 *port
= ((struct cma_hdr
*) hdr
)->port
;
765 *src
= &((struct cma_hdr
*) hdr
)->src_addr
;
766 *dst
= &((struct cma_hdr
*) hdr
)->dst_addr
;
770 if (*ip_ver
!= 4 && *ip_ver
!= 6)
775 static void cma_save_net_info(struct rdma_addr
*addr
,
776 struct rdma_addr
*listen_addr
,
777 u8 ip_ver
, __be16 port
,
778 union cma_ip_addr
*src
, union cma_ip_addr
*dst
)
780 struct sockaddr_in
*listen4
, *ip4
;
781 struct sockaddr_in6
*listen6
, *ip6
;
785 listen4
= (struct sockaddr_in
*) &listen_addr
->src_addr
;
786 ip4
= (struct sockaddr_in
*) &addr
->src_addr
;
787 ip4
->sin_family
= listen4
->sin_family
;
788 ip4
->sin_addr
.s_addr
= dst
->ip4
.addr
;
789 ip4
->sin_port
= listen4
->sin_port
;
791 ip4
= (struct sockaddr_in
*) &addr
->dst_addr
;
792 ip4
->sin_family
= listen4
->sin_family
;
793 ip4
->sin_addr
.s_addr
= src
->ip4
.addr
;
794 ip4
->sin_port
= port
;
797 listen6
= (struct sockaddr_in6
*) &listen_addr
->src_addr
;
798 ip6
= (struct sockaddr_in6
*) &addr
->src_addr
;
799 ip6
->sin6_family
= listen6
->sin6_family
;
800 ip6
->sin6_addr
= dst
->ip6
;
801 ip6
->sin6_port
= listen6
->sin6_port
;
803 ip6
= (struct sockaddr_in6
*) &addr
->dst_addr
;
804 ip6
->sin6_family
= listen6
->sin6_family
;
805 ip6
->sin6_addr
= src
->ip6
;
806 ip6
->sin6_port
= port
;
813 static inline int cma_user_data_offset(enum rdma_port_space ps
)
819 return sizeof(struct cma_hdr
);
823 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
825 switch (rdma_port_get_link_layer(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
826 case IB_LINK_LAYER_INFINIBAND
:
828 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
835 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
837 struct rdma_id_private
*dev_id_priv
;
840 * Remove from listen_any_list to prevent added devices from spawning
841 * additional listen requests.
844 list_del(&id_priv
->list
);
846 while (!list_empty(&id_priv
->listen_list
)) {
847 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
848 struct rdma_id_private
, listen_list
);
849 /* sync with device removal to avoid duplicate destruction */
850 list_del_init(&dev_id_priv
->list
);
851 list_del(&dev_id_priv
->listen_list
);
854 rdma_destroy_id(&dev_id_priv
->id
);
860 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
861 enum cma_state state
)
865 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
867 case CMA_ROUTE_QUERY
:
868 cma_cancel_route(id_priv
);
871 if (cma_any_addr((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
)
872 && !id_priv
->cma_dev
)
873 cma_cancel_listens(id_priv
);
880 static void cma_release_port(struct rdma_id_private
*id_priv
)
882 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
888 hlist_del(&id_priv
->node
);
889 if (hlist_empty(&bind_list
->owners
)) {
890 idr_remove(bind_list
->ps
, bind_list
->port
);
896 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
898 struct cma_multicast
*mc
;
900 while (!list_empty(&id_priv
->mc_list
)) {
901 mc
= container_of(id_priv
->mc_list
.next
,
902 struct cma_multicast
, list
);
904 switch (rdma_port_get_link_layer(id_priv
->cma_dev
->device
, id_priv
->id
.port_num
)) {
905 case IB_LINK_LAYER_INFINIBAND
:
906 ib_sa_free_multicast(mc
->multicast
.ib
);
909 case IB_LINK_LAYER_ETHERNET
:
910 kref_put(&mc
->mcref
, release_mc
);
918 void rdma_destroy_id(struct rdma_cm_id
*id
)
920 struct rdma_id_private
*id_priv
;
921 enum cma_state state
;
923 id_priv
= container_of(id
, struct rdma_id_private
, id
);
924 state
= cma_exch(id_priv
, CMA_DESTROYING
);
925 cma_cancel_operation(id_priv
, state
);
928 * Wait for any active callback to finish. New callbacks will find
929 * the id_priv state set to destroying and abort.
931 mutex_lock(&id_priv
->handler_mutex
);
932 mutex_unlock(&id_priv
->handler_mutex
);
934 if (id_priv
->cma_dev
) {
935 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
936 case RDMA_TRANSPORT_IB
:
937 if (id_priv
->cm_id
.ib
&& !IS_ERR(id_priv
->cm_id
.ib
))
938 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
940 case RDMA_TRANSPORT_IWARP
:
941 if (id_priv
->cm_id
.iw
&& !IS_ERR(id_priv
->cm_id
.iw
))
942 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
947 cma_leave_mc_groups(id_priv
);
948 cma_release_dev(id_priv
);
951 cma_release_port(id_priv
);
952 cma_deref_id(id_priv
);
953 wait_for_completion(&id_priv
->comp
);
955 if (id_priv
->internal_id
)
956 cma_deref_id(id_priv
->id
.context
);
958 kfree(id_priv
->id
.route
.path_rec
);
961 EXPORT_SYMBOL(rdma_destroy_id
);
963 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
967 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
971 ret
= cma_modify_qp_rts(id_priv
, NULL
);
975 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
981 cma_modify_qp_err(id_priv
);
982 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
987 static int cma_verify_rep(struct rdma_id_private
*id_priv
, void *data
)
989 if (id_priv
->id
.ps
== RDMA_PS_SDP
&&
990 sdp_get_majv(((struct sdp_hah
*) data
)->sdp_version
) !=
997 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
998 struct ib_cm_rep_event_param
*rep_data
,
1001 event
->param
.conn
.private_data
= private_data
;
1002 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1003 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1004 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1005 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1006 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1007 event
->param
.conn
.srq
= rep_data
->srq
;
1008 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1011 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1013 struct rdma_id_private
*id_priv
= cm_id
->context
;
1014 struct rdma_cm_event event
;
1017 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1018 cma_disable_callback(id_priv
, CMA_CONNECT
)) ||
1019 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1020 cma_disable_callback(id_priv
, CMA_DISCONNECT
)))
1023 memset(&event
, 0, sizeof event
);
1024 switch (ib_event
->event
) {
1025 case IB_CM_REQ_ERROR
:
1026 case IB_CM_REP_ERROR
:
1027 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1028 event
.status
= -ETIMEDOUT
;
1030 case IB_CM_REP_RECEIVED
:
1031 event
.status
= cma_verify_rep(id_priv
, ib_event
->private_data
);
1033 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1034 else if (id_priv
->id
.qp
&& id_priv
->id
.ps
!= RDMA_PS_SDP
) {
1035 event
.status
= cma_rep_recv(id_priv
);
1036 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1037 RDMA_CM_EVENT_ESTABLISHED
;
1039 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1040 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1041 ib_event
->private_data
);
1043 case IB_CM_RTU_RECEIVED
:
1044 case IB_CM_USER_ESTABLISHED
:
1045 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1047 case IB_CM_DREQ_ERROR
:
1048 event
.status
= -ETIMEDOUT
; /* fall through */
1049 case IB_CM_DREQ_RECEIVED
:
1050 case IB_CM_DREP_RECEIVED
:
1051 if (!cma_comp_exch(id_priv
, CMA_CONNECT
, CMA_DISCONNECT
))
1053 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1055 case IB_CM_TIMEWAIT_EXIT
:
1056 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1058 case IB_CM_MRA_RECEIVED
:
1061 case IB_CM_REJ_RECEIVED
:
1062 cma_modify_qp_err(id_priv
);
1063 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1064 event
.event
= RDMA_CM_EVENT_REJECTED
;
1065 event
.param
.conn
.private_data
= ib_event
->private_data
;
1066 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1069 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
1074 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1076 /* Destroy the CM ID by returning a non-zero value. */
1077 id_priv
->cm_id
.ib
= NULL
;
1078 cma_exch(id_priv
, CMA_DESTROYING
);
1079 mutex_unlock(&id_priv
->handler_mutex
);
1080 rdma_destroy_id(&id_priv
->id
);
1084 mutex_unlock(&id_priv
->handler_mutex
);
1088 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1089 struct ib_cm_event
*ib_event
)
1091 struct rdma_id_private
*id_priv
;
1092 struct rdma_cm_id
*id
;
1093 struct rdma_route
*rt
;
1094 union cma_ip_addr
*src
, *dst
;
1099 if (cma_get_net_info(ib_event
->private_data
, listen_id
->ps
,
1100 &ip_ver
, &port
, &src
, &dst
))
1103 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1108 cma_save_net_info(&id
->route
.addr
, &listen_id
->route
.addr
,
1109 ip_ver
, port
, src
, dst
);
1112 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1113 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1118 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1119 if (rt
->num_paths
== 2)
1120 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1122 if (cma_any_addr((struct sockaddr
*) &rt
->addr
.src_addr
)) {
1123 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1124 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1125 ib_addr_set_pkey(&rt
->addr
.dev_addr
, rt
->path_rec
[0].pkey
);
1127 ret
= rdma_translate_ip((struct sockaddr
*) &rt
->addr
.src_addr
,
1128 &rt
->addr
.dev_addr
);
1132 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1134 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1135 id_priv
->state
= CMA_CONNECT
;
1139 rdma_destroy_id(id
);
1144 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1145 struct ib_cm_event
*ib_event
)
1147 struct rdma_id_private
*id_priv
;
1148 struct rdma_cm_id
*id
;
1149 union cma_ip_addr
*src
, *dst
;
1154 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1160 if (cma_get_net_info(ib_event
->private_data
, listen_id
->ps
,
1161 &ip_ver
, &port
, &src
, &dst
))
1164 cma_save_net_info(&id
->route
.addr
, &listen_id
->route
.addr
,
1165 ip_ver
, port
, src
, dst
);
1167 if (!cma_any_addr((struct sockaddr
*) &id
->route
.addr
.src_addr
)) {
1168 ret
= rdma_translate_ip((struct sockaddr
*) &id
->route
.addr
.src_addr
,
1169 &id
->route
.addr
.dev_addr
);
1174 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1175 id_priv
->state
= CMA_CONNECT
;
1178 rdma_destroy_id(id
);
1182 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1183 struct ib_cm_req_event_param
*req_data
,
1184 void *private_data
, int offset
)
1186 event
->param
.conn
.private_data
= private_data
+ offset
;
1187 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1188 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1189 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1190 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1191 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1192 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1193 event
->param
.conn
.srq
= req_data
->srq
;
1194 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1197 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1199 struct rdma_id_private
*listen_id
, *conn_id
;
1200 struct rdma_cm_event event
;
1203 listen_id
= cm_id
->context
;
1204 if (cma_disable_callback(listen_id
, CMA_LISTEN
))
1205 return -ECONNABORTED
;
1207 memset(&event
, 0, sizeof event
);
1208 offset
= cma_user_data_offset(listen_id
->id
.ps
);
1209 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1210 if (cma_is_ud_ps(listen_id
->id
.ps
)) {
1211 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
);
1212 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1213 event
.param
.ud
.private_data_len
=
1214 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1216 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
);
1217 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1218 ib_event
->private_data
, offset
);
1225 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1226 ret
= cma_acquire_dev(conn_id
);
1228 goto release_conn_id
;
1230 conn_id
->cm_id
.ib
= cm_id
;
1231 cm_id
->context
= conn_id
;
1232 cm_id
->cm_handler
= cma_ib_handler
;
1235 * Protect against the user destroying conn_id from another thread
1236 * until we're done accessing it.
1238 atomic_inc(&conn_id
->refcount
);
1239 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1242 * Acquire mutex to prevent user executing rdma_destroy_id()
1243 * while we're accessing the cm_id.
1246 if (cma_comp(conn_id
, CMA_CONNECT
) &&
1247 !cma_is_ud_ps(conn_id
->id
.ps
))
1248 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1249 mutex_unlock(&lock
);
1250 mutex_unlock(&conn_id
->handler_mutex
);
1251 cma_deref_id(conn_id
);
1254 cma_deref_id(conn_id
);
1256 /* Destroy the CM ID by returning a non-zero value. */
1257 conn_id
->cm_id
.ib
= NULL
;
1260 cma_exch(conn_id
, CMA_DESTROYING
);
1261 mutex_unlock(&conn_id
->handler_mutex
);
1262 rdma_destroy_id(&conn_id
->id
);
1265 mutex_unlock(&listen_id
->handler_mutex
);
1269 static __be64
cma_get_service_id(enum rdma_port_space ps
, struct sockaddr
*addr
)
1271 return cpu_to_be64(((u64
)ps
<< 16) + be16_to_cpu(cma_port(addr
)));
1274 static void cma_set_compare_data(enum rdma_port_space ps
, struct sockaddr
*addr
,
1275 struct ib_cm_compare_data
*compare
)
1277 struct cma_hdr
*cma_data
, *cma_mask
;
1278 struct sdp_hh
*sdp_data
, *sdp_mask
;
1280 struct in6_addr ip6_addr
;
1282 memset(compare
, 0, sizeof *compare
);
1283 cma_data
= (void *) compare
->data
;
1284 cma_mask
= (void *) compare
->mask
;
1285 sdp_data
= (void *) compare
->data
;
1286 sdp_mask
= (void *) compare
->mask
;
1288 switch (addr
->sa_family
) {
1290 ip4_addr
= ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
;
1291 if (ps
== RDMA_PS_SDP
) {
1292 sdp_set_ip_ver(sdp_data
, 4);
1293 sdp_set_ip_ver(sdp_mask
, 0xF);
1294 sdp_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1295 sdp_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1297 cma_set_ip_ver(cma_data
, 4);
1298 cma_set_ip_ver(cma_mask
, 0xF);
1299 cma_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1300 cma_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1304 ip6_addr
= ((struct sockaddr_in6
*) addr
)->sin6_addr
;
1305 if (ps
== RDMA_PS_SDP
) {
1306 sdp_set_ip_ver(sdp_data
, 6);
1307 sdp_set_ip_ver(sdp_mask
, 0xF);
1308 sdp_data
->dst_addr
.ip6
= ip6_addr
;
1309 memset(&sdp_mask
->dst_addr
.ip6
, 0xFF,
1310 sizeof sdp_mask
->dst_addr
.ip6
);
1312 cma_set_ip_ver(cma_data
, 6);
1313 cma_set_ip_ver(cma_mask
, 0xF);
1314 cma_data
->dst_addr
.ip6
= ip6_addr
;
1315 memset(&cma_mask
->dst_addr
.ip6
, 0xFF,
1316 sizeof cma_mask
->dst_addr
.ip6
);
1324 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
1326 struct rdma_id_private
*id_priv
= iw_id
->context
;
1327 struct rdma_cm_event event
;
1328 struct sockaddr_in
*sin
;
1331 if (cma_disable_callback(id_priv
, CMA_CONNECT
))
1334 memset(&event
, 0, sizeof event
);
1335 switch (iw_event
->event
) {
1336 case IW_CM_EVENT_CLOSE
:
1337 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1339 case IW_CM_EVENT_CONNECT_REPLY
:
1340 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1341 *sin
= iw_event
->local_addr
;
1342 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.dst_addr
;
1343 *sin
= iw_event
->remote_addr
;
1344 switch (iw_event
->status
) {
1346 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1350 event
.event
= RDMA_CM_EVENT_REJECTED
;
1353 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1356 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1360 case IW_CM_EVENT_ESTABLISHED
:
1361 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1367 event
.status
= iw_event
->status
;
1368 event
.param
.conn
.private_data
= iw_event
->private_data
;
1369 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1370 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1372 /* Destroy the CM ID by returning a non-zero value. */
1373 id_priv
->cm_id
.iw
= NULL
;
1374 cma_exch(id_priv
, CMA_DESTROYING
);
1375 mutex_unlock(&id_priv
->handler_mutex
);
1376 rdma_destroy_id(&id_priv
->id
);
1380 mutex_unlock(&id_priv
->handler_mutex
);
1384 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
1385 struct iw_cm_event
*iw_event
)
1387 struct rdma_cm_id
*new_cm_id
;
1388 struct rdma_id_private
*listen_id
, *conn_id
;
1389 struct sockaddr_in
*sin
;
1390 struct net_device
*dev
= NULL
;
1391 struct rdma_cm_event event
;
1393 struct ib_device_attr attr
;
1395 listen_id
= cm_id
->context
;
1396 if (cma_disable_callback(listen_id
, CMA_LISTEN
))
1397 return -ECONNABORTED
;
1399 /* Create a new RDMA id for the new IW CM ID */
1400 new_cm_id
= rdma_create_id(listen_id
->id
.event_handler
,
1401 listen_id
->id
.context
,
1403 if (IS_ERR(new_cm_id
)) {
1407 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
1408 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1409 conn_id
->state
= CMA_CONNECT
;
1411 dev
= ip_dev_find(&init_net
, iw_event
->local_addr
.sin_addr
.s_addr
);
1413 ret
= -EADDRNOTAVAIL
;
1414 mutex_unlock(&conn_id
->handler_mutex
);
1415 rdma_destroy_id(new_cm_id
);
1418 ret
= rdma_copy_addr(&conn_id
->id
.route
.addr
.dev_addr
, dev
, NULL
);
1420 mutex_unlock(&conn_id
->handler_mutex
);
1421 rdma_destroy_id(new_cm_id
);
1425 ret
= cma_acquire_dev(conn_id
);
1427 mutex_unlock(&conn_id
->handler_mutex
);
1428 rdma_destroy_id(new_cm_id
);
1432 conn_id
->cm_id
.iw
= cm_id
;
1433 cm_id
->context
= conn_id
;
1434 cm_id
->cm_handler
= cma_iw_handler
;
1436 sin
= (struct sockaddr_in
*) &new_cm_id
->route
.addr
.src_addr
;
1437 *sin
= iw_event
->local_addr
;
1438 sin
= (struct sockaddr_in
*) &new_cm_id
->route
.addr
.dst_addr
;
1439 *sin
= iw_event
->remote_addr
;
1441 ret
= ib_query_device(conn_id
->id
.device
, &attr
);
1443 mutex_unlock(&conn_id
->handler_mutex
);
1444 rdma_destroy_id(new_cm_id
);
1448 memset(&event
, 0, sizeof event
);
1449 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1450 event
.param
.conn
.private_data
= iw_event
->private_data
;
1451 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1452 event
.param
.conn
.initiator_depth
= attr
.max_qp_init_rd_atom
;
1453 event
.param
.conn
.responder_resources
= attr
.max_qp_rd_atom
;
1456 * Protect against the user destroying conn_id from another thread
1457 * until we're done accessing it.
1459 atomic_inc(&conn_id
->refcount
);
1460 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1462 /* User wants to destroy the CM ID */
1463 conn_id
->cm_id
.iw
= NULL
;
1464 cma_exch(conn_id
, CMA_DESTROYING
);
1465 mutex_unlock(&conn_id
->handler_mutex
);
1466 cma_deref_id(conn_id
);
1467 rdma_destroy_id(&conn_id
->id
);
1471 mutex_unlock(&conn_id
->handler_mutex
);
1472 cma_deref_id(conn_id
);
1477 mutex_unlock(&listen_id
->handler_mutex
);
1481 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
1483 struct ib_cm_compare_data compare_data
;
1484 struct sockaddr
*addr
;
1488 id_priv
->cm_id
.ib
= ib_create_cm_id(id_priv
->id
.device
, cma_req_handler
,
1490 if (IS_ERR(id_priv
->cm_id
.ib
))
1491 return PTR_ERR(id_priv
->cm_id
.ib
);
1493 addr
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
1494 svc_id
= cma_get_service_id(id_priv
->id
.ps
, addr
);
1495 if (cma_any_addr(addr
))
1496 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, NULL
);
1498 cma_set_compare_data(id_priv
->id
.ps
, addr
, &compare_data
);
1499 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, &compare_data
);
1503 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1504 id_priv
->cm_id
.ib
= NULL
;
1510 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
1513 struct sockaddr_in
*sin
;
1515 id_priv
->cm_id
.iw
= iw_create_cm_id(id_priv
->id
.device
,
1516 iw_conn_req_handler
,
1518 if (IS_ERR(id_priv
->cm_id
.iw
))
1519 return PTR_ERR(id_priv
->cm_id
.iw
);
1521 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1522 id_priv
->cm_id
.iw
->local_addr
= *sin
;
1524 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
1527 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1528 id_priv
->cm_id
.iw
= NULL
;
1534 static int cma_listen_handler(struct rdma_cm_id
*id
,
1535 struct rdma_cm_event
*event
)
1537 struct rdma_id_private
*id_priv
= id
->context
;
1539 id
->context
= id_priv
->id
.context
;
1540 id
->event_handler
= id_priv
->id
.event_handler
;
1541 return id_priv
->id
.event_handler(id
, event
);
1544 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
1545 struct cma_device
*cma_dev
)
1547 struct rdma_id_private
*dev_id_priv
;
1548 struct rdma_cm_id
*id
;
1551 id
= rdma_create_id(cma_listen_handler
, id_priv
, id_priv
->id
.ps
);
1555 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
1557 dev_id_priv
->state
= CMA_ADDR_BOUND
;
1558 memcpy(&id
->route
.addr
.src_addr
, &id_priv
->id
.route
.addr
.src_addr
,
1559 ip_addr_size((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
));
1561 cma_attach_to_dev(dev_id_priv
, cma_dev
);
1562 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
1563 atomic_inc(&id_priv
->refcount
);
1564 dev_id_priv
->internal_id
= 1;
1566 ret
= rdma_listen(id
, id_priv
->backlog
);
1568 printk(KERN_WARNING
"RDMA CMA: cma_listen_on_dev, error %d, "
1569 "listening on device %s\n", ret
, cma_dev
->device
->name
);
1572 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
1574 struct cma_device
*cma_dev
;
1577 list_add_tail(&id_priv
->list
, &listen_any_list
);
1578 list_for_each_entry(cma_dev
, &dev_list
, list
)
1579 cma_listen_on_dev(id_priv
, cma_dev
);
1580 mutex_unlock(&lock
);
1583 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
1585 struct rdma_id_private
*id_priv
;
1587 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1588 id_priv
->tos
= (u8
) tos
;
1590 EXPORT_SYMBOL(rdma_set_service_type
);
1592 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
1595 struct cma_work
*work
= context
;
1596 struct rdma_route
*route
;
1598 route
= &work
->id
->id
.route
;
1601 route
->num_paths
= 1;
1602 *route
->path_rec
= *path_rec
;
1604 work
->old_state
= CMA_ROUTE_QUERY
;
1605 work
->new_state
= CMA_ADDR_RESOLVED
;
1606 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
1607 work
->event
.status
= status
;
1610 queue_work(cma_wq
, &work
->work
);
1613 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
1614 struct cma_work
*work
)
1616 struct rdma_addr
*addr
= &id_priv
->id
.route
.addr
;
1617 struct ib_sa_path_rec path_rec
;
1618 ib_sa_comp_mask comp_mask
;
1619 struct sockaddr_in6
*sin6
;
1621 memset(&path_rec
, 0, sizeof path_rec
);
1622 rdma_addr_get_sgid(&addr
->dev_addr
, &path_rec
.sgid
);
1623 rdma_addr_get_dgid(&addr
->dev_addr
, &path_rec
.dgid
);
1624 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(&addr
->dev_addr
));
1625 path_rec
.numb_path
= 1;
1626 path_rec
.reversible
= 1;
1627 path_rec
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
1628 (struct sockaddr
*) &addr
->dst_addr
);
1630 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
1631 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
1632 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
1634 if (addr
->src_addr
.ss_family
== AF_INET
) {
1635 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
1636 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
1638 sin6
= (struct sockaddr_in6
*) &addr
->src_addr
;
1639 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
1640 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
1643 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
1644 id_priv
->id
.port_num
, &path_rec
,
1645 comp_mask
, timeout_ms
,
1646 GFP_KERNEL
, cma_query_handler
,
1647 work
, &id_priv
->query
);
1649 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
1652 static void cma_work_handler(struct work_struct
*_work
)
1654 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
1655 struct rdma_id_private
*id_priv
= work
->id
;
1658 mutex_lock(&id_priv
->handler_mutex
);
1659 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
1662 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1663 cma_exch(id_priv
, CMA_DESTROYING
);
1667 mutex_unlock(&id_priv
->handler_mutex
);
1668 cma_deref_id(id_priv
);
1670 rdma_destroy_id(&id_priv
->id
);
1674 static void cma_ndev_work_handler(struct work_struct
*_work
)
1676 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
1677 struct rdma_id_private
*id_priv
= work
->id
;
1680 mutex_lock(&id_priv
->handler_mutex
);
1681 if (id_priv
->state
== CMA_DESTROYING
||
1682 id_priv
->state
== CMA_DEVICE_REMOVAL
)
1685 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1686 cma_exch(id_priv
, CMA_DESTROYING
);
1691 mutex_unlock(&id_priv
->handler_mutex
);
1692 cma_deref_id(id_priv
);
1694 rdma_destroy_id(&id_priv
->id
);
1698 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1700 struct rdma_route
*route
= &id_priv
->id
.route
;
1701 struct cma_work
*work
;
1704 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1709 INIT_WORK(&work
->work
, cma_work_handler
);
1710 work
->old_state
= CMA_ROUTE_QUERY
;
1711 work
->new_state
= CMA_ROUTE_RESOLVED
;
1712 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1714 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1715 if (!route
->path_rec
) {
1720 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
1726 kfree(route
->path_rec
);
1727 route
->path_rec
= NULL
;
1733 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
1734 struct ib_sa_path_rec
*path_rec
, int num_paths
)
1736 struct rdma_id_private
*id_priv
;
1739 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1740 if (!cma_comp_exch(id_priv
, CMA_ADDR_RESOLVED
, CMA_ROUTE_RESOLVED
))
1743 id
->route
.path_rec
= kmemdup(path_rec
, sizeof *path_rec
* num_paths
,
1745 if (!id
->route
.path_rec
) {
1750 id
->route
.num_paths
= num_paths
;
1753 cma_comp_exch(id_priv
, CMA_ROUTE_RESOLVED
, CMA_ADDR_RESOLVED
);
1756 EXPORT_SYMBOL(rdma_set_ib_paths
);
1758 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1760 struct cma_work
*work
;
1762 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1767 INIT_WORK(&work
->work
, cma_work_handler
);
1768 work
->old_state
= CMA_ROUTE_QUERY
;
1769 work
->new_state
= CMA_ROUTE_RESOLVED
;
1770 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1771 queue_work(cma_wq
, &work
->work
);
1775 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
1777 struct rdma_route
*route
= &id_priv
->id
.route
;
1778 struct rdma_addr
*addr
= &route
->addr
;
1779 struct cma_work
*work
;
1781 struct sockaddr_in
*src_addr
= (struct sockaddr_in
*)&route
->addr
.src_addr
;
1782 struct sockaddr_in
*dst_addr
= (struct sockaddr_in
*)&route
->addr
.dst_addr
;
1783 struct net_device
*ndev
= NULL
;
1786 if (src_addr
->sin_family
!= dst_addr
->sin_family
)
1789 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1794 INIT_WORK(&work
->work
, cma_work_handler
);
1796 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1797 if (!route
->path_rec
) {
1802 route
->num_paths
= 1;
1804 if (addr
->dev_addr
.bound_dev_if
)
1805 ndev
= dev_get_by_index(&init_net
, addr
->dev_addr
.bound_dev_if
);
1811 vid
= rdma_vlan_dev_vlan_id(ndev
);
1813 iboe_mac_vlan_to_ll(&route
->path_rec
->sgid
, addr
->dev_addr
.src_dev_addr
, vid
);
1814 iboe_mac_vlan_to_ll(&route
->path_rec
->dgid
, addr
->dev_addr
.dst_dev_addr
, vid
);
1816 route
->path_rec
->hop_limit
= 1;
1817 route
->path_rec
->reversible
= 1;
1818 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
1819 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
1820 route
->path_rec
->sl
= id_priv
->tos
>> 5;
1822 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
1823 route
->path_rec
->rate_selector
= IB_SA_EQ
;
1824 route
->path_rec
->rate
= iboe_get_rate(ndev
);
1826 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
1827 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
1828 if (!route
->path_rec
->mtu
) {
1833 work
->old_state
= CMA_ROUTE_QUERY
;
1834 work
->new_state
= CMA_ROUTE_RESOLVED
;
1835 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1836 work
->event
.status
= 0;
1838 queue_work(cma_wq
, &work
->work
);
1843 kfree(route
->path_rec
);
1844 route
->path_rec
= NULL
;
1850 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
1852 struct rdma_id_private
*id_priv
;
1855 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1856 if (!cma_comp_exch(id_priv
, CMA_ADDR_RESOLVED
, CMA_ROUTE_QUERY
))
1859 atomic_inc(&id_priv
->refcount
);
1860 switch (rdma_node_get_transport(id
->device
->node_type
)) {
1861 case RDMA_TRANSPORT_IB
:
1862 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
1863 case IB_LINK_LAYER_INFINIBAND
:
1864 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
1866 case IB_LINK_LAYER_ETHERNET
:
1867 ret
= cma_resolve_iboe_route(id_priv
);
1873 case RDMA_TRANSPORT_IWARP
:
1874 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
1885 cma_comp_exch(id_priv
, CMA_ROUTE_QUERY
, CMA_ADDR_RESOLVED
);
1886 cma_deref_id(id_priv
);
1889 EXPORT_SYMBOL(rdma_resolve_route
);
1891 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
1893 struct cma_device
*cma_dev
;
1894 struct ib_port_attr port_attr
;
1901 if (list_empty(&dev_list
)) {
1905 list_for_each_entry(cma_dev
, &dev_list
, list
)
1906 for (p
= 1; p
<= cma_dev
->device
->phys_port_cnt
; ++p
)
1907 if (!ib_query_port(cma_dev
->device
, p
, &port_attr
) &&
1908 port_attr
.state
== IB_PORT_ACTIVE
)
1912 cma_dev
= list_entry(dev_list
.next
, struct cma_device
, list
);
1915 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
);
1919 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
1923 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
1924 (rdma_port_get_link_layer(cma_dev
->device
, p
) == IB_LINK_LAYER_INFINIBAND
) ?
1925 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
1927 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1928 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
1929 id_priv
->id
.port_num
= p
;
1930 cma_attach_to_dev(id_priv
, cma_dev
);
1932 mutex_unlock(&lock
);
1936 static void addr_handler(int status
, struct sockaddr
*src_addr
,
1937 struct rdma_dev_addr
*dev_addr
, void *context
)
1939 struct rdma_id_private
*id_priv
= context
;
1940 struct rdma_cm_event event
;
1942 memset(&event
, 0, sizeof event
);
1943 mutex_lock(&id_priv
->handler_mutex
);
1944 if (!cma_comp_exch(id_priv
, CMA_ADDR_QUERY
, CMA_ADDR_RESOLVED
))
1947 if (!status
&& !id_priv
->cma_dev
)
1948 status
= cma_acquire_dev(id_priv
);
1951 if (!cma_comp_exch(id_priv
, CMA_ADDR_RESOLVED
, CMA_ADDR_BOUND
))
1953 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
1954 event
.status
= status
;
1956 memcpy(&id_priv
->id
.route
.addr
.src_addr
, src_addr
,
1957 ip_addr_size(src_addr
));
1958 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
1961 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
1962 cma_exch(id_priv
, CMA_DESTROYING
);
1963 mutex_unlock(&id_priv
->handler_mutex
);
1964 cma_deref_id(id_priv
);
1965 rdma_destroy_id(&id_priv
->id
);
1969 mutex_unlock(&id_priv
->handler_mutex
);
1970 cma_deref_id(id_priv
);
1973 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
1975 struct cma_work
*work
;
1976 struct sockaddr
*src
, *dst
;
1980 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1984 if (!id_priv
->cma_dev
) {
1985 ret
= cma_bind_loopback(id_priv
);
1990 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1991 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1993 src
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
1994 if (cma_zero_addr(src
)) {
1995 dst
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
1996 if ((src
->sa_family
= dst
->sa_family
) == AF_INET
) {
1997 ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
=
1998 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
2000 ipv6_addr_copy(&((struct sockaddr_in6
*) src
)->sin6_addr
,
2001 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
2006 INIT_WORK(&work
->work
, cma_work_handler
);
2007 work
->old_state
= CMA_ADDR_QUERY
;
2008 work
->new_state
= CMA_ADDR_RESOLVED
;
2009 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2010 queue_work(cma_wq
, &work
->work
);
2017 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2018 struct sockaddr
*dst_addr
)
2020 if (!src_addr
|| !src_addr
->sa_family
) {
2021 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2022 if ((src_addr
->sa_family
= dst_addr
->sa_family
) == AF_INET6
) {
2023 ((struct sockaddr_in6
*) src_addr
)->sin6_scope_id
=
2024 ((struct sockaddr_in6
*) dst_addr
)->sin6_scope_id
;
2027 return rdma_bind_addr(id
, src_addr
);
2030 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2031 struct sockaddr
*dst_addr
, int timeout_ms
)
2033 struct rdma_id_private
*id_priv
;
2036 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2037 if (id_priv
->state
== CMA_IDLE
) {
2038 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2043 if (!cma_comp_exch(id_priv
, CMA_ADDR_BOUND
, CMA_ADDR_QUERY
))
2046 atomic_inc(&id_priv
->refcount
);
2047 memcpy(&id
->route
.addr
.dst_addr
, dst_addr
, ip_addr_size(dst_addr
));
2048 if (cma_any_addr(dst_addr
))
2049 ret
= cma_resolve_loopback(id_priv
);
2051 ret
= rdma_resolve_ip(&addr_client
, (struct sockaddr
*) &id
->route
.addr
.src_addr
,
2052 dst_addr
, &id
->route
.addr
.dev_addr
,
2053 timeout_ms
, addr_handler
, id_priv
);
2059 cma_comp_exch(id_priv
, CMA_ADDR_QUERY
, CMA_ADDR_BOUND
);
2060 cma_deref_id(id_priv
);
2063 EXPORT_SYMBOL(rdma_resolve_addr
);
2065 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2067 struct rdma_id_private
*id_priv
;
2068 unsigned long flags
;
2071 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2072 spin_lock_irqsave(&id_priv
->lock
, flags
);
2073 if (id_priv
->state
== CMA_IDLE
) {
2074 id_priv
->reuseaddr
= reuse
;
2079 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2082 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2084 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
2085 struct rdma_id_private
*id_priv
)
2087 struct sockaddr_in
*sin
;
2089 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
2090 sin
->sin_port
= htons(bind_list
->port
);
2091 id_priv
->bind_list
= bind_list
;
2092 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
2095 static int cma_alloc_port(struct idr
*ps
, struct rdma_id_private
*id_priv
,
2096 unsigned short snum
)
2098 struct rdma_bind_list
*bind_list
;
2101 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
2106 ret
= idr_get_new_above(ps
, bind_list
, snum
, &port
);
2107 } while ((ret
== -EAGAIN
) && idr_pre_get(ps
, GFP_KERNEL
));
2113 ret
= -EADDRNOTAVAIL
;
2118 bind_list
->port
= (unsigned short) port
;
2119 cma_bind_port(bind_list
, id_priv
);
2122 idr_remove(ps
, port
);
2128 static int cma_alloc_any_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
2130 static unsigned int last_used_port
;
2131 int low
, high
, remaining
;
2134 inet_get_local_port_range(&low
, &high
);
2135 remaining
= (high
- low
) + 1;
2136 rover
= net_random() % remaining
+ low
;
2138 if (last_used_port
!= rover
&&
2139 !idr_find(ps
, (unsigned short) rover
)) {
2140 int ret
= cma_alloc_port(ps
, id_priv
, rover
);
2142 * Remember previously used port number in order to avoid
2143 * re-using same port immediately after it is closed.
2146 last_used_port
= rover
;
2147 if (ret
!= -EADDRNOTAVAIL
)
2152 if ((rover
< low
) || (rover
> high
))
2156 return -EADDRNOTAVAIL
;
2160 * Check that the requested port is available. This is called when trying to
2161 * bind to a specific port, or when trying to listen on a bound port. In
2162 * the latter case, the provided id_priv may already be on the bind_list, but
2163 * we still need to check that it's okay to start listening.
2165 static int cma_check_port(struct rdma_bind_list
*bind_list
,
2166 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
2168 struct rdma_id_private
*cur_id
;
2169 struct sockaddr
*addr
, *cur_addr
;
2170 struct hlist_node
*node
;
2172 addr
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
2173 if (cma_any_addr(addr
) && !reuseaddr
)
2174 return -EADDRNOTAVAIL
;
2176 hlist_for_each_entry(cur_id
, node
, &bind_list
->owners
, node
) {
2177 if (id_priv
== cur_id
)
2180 if ((cur_id
->state
== CMA_LISTEN
) ||
2181 !reuseaddr
|| !cur_id
->reuseaddr
) {
2182 cur_addr
= (struct sockaddr
*) &cur_id
->id
.route
.addr
.src_addr
;
2183 if (cma_any_addr(cur_addr
))
2184 return -EADDRNOTAVAIL
;
2186 if (!cma_addr_cmp(addr
, cur_addr
))
2193 static int cma_use_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
2195 struct rdma_bind_list
*bind_list
;
2196 unsigned short snum
;
2199 snum
= ntohs(cma_port((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
));
2200 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
2203 bind_list
= idr_find(ps
, snum
);
2205 ret
= cma_alloc_port(ps
, id_priv
, snum
);
2207 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
2209 cma_bind_port(bind_list
, id_priv
);
2214 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
2216 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
2220 if (bind_list
->owners
.first
->next
)
2221 ret
= cma_check_port(bind_list
, id_priv
, 0);
2222 mutex_unlock(&lock
);
2226 static int cma_get_port(struct rdma_id_private
*id_priv
)
2231 switch (id_priv
->id
.ps
) {
2245 return -EPROTONOSUPPORT
;
2249 if (cma_any_port((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
))
2250 ret
= cma_alloc_any_port(ps
, id_priv
);
2252 ret
= cma_use_port(ps
, id_priv
);
2253 mutex_unlock(&lock
);
2258 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
2259 struct sockaddr
*addr
)
2261 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2262 struct sockaddr_in6
*sin6
;
2264 if (addr
->sa_family
!= AF_INET6
)
2267 sin6
= (struct sockaddr_in6
*) addr
;
2268 if ((ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
) &&
2269 !sin6
->sin6_scope_id
)
2272 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
2277 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
2279 struct rdma_id_private
*id_priv
;
2282 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2283 if (id_priv
->state
== CMA_IDLE
) {
2284 ((struct sockaddr
*) &id
->route
.addr
.src_addr
)->sa_family
= AF_INET
;
2285 ret
= rdma_bind_addr(id
, (struct sockaddr
*) &id
->route
.addr
.src_addr
);
2290 if (!cma_comp_exch(id_priv
, CMA_ADDR_BOUND
, CMA_LISTEN
))
2293 if (id_priv
->reuseaddr
) {
2294 ret
= cma_bind_listen(id_priv
);
2299 id_priv
->backlog
= backlog
;
2301 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2302 case RDMA_TRANSPORT_IB
:
2303 ret
= cma_ib_listen(id_priv
);
2307 case RDMA_TRANSPORT_IWARP
:
2308 ret
= cma_iw_listen(id_priv
, backlog
);
2317 cma_listen_on_all(id_priv
);
2321 id_priv
->backlog
= 0;
2322 cma_comp_exch(id_priv
, CMA_LISTEN
, CMA_ADDR_BOUND
);
2325 EXPORT_SYMBOL(rdma_listen
);
2327 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2329 struct rdma_id_private
*id_priv
;
2332 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
)
2333 return -EAFNOSUPPORT
;
2335 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2336 if (!cma_comp_exch(id_priv
, CMA_IDLE
, CMA_ADDR_BOUND
))
2339 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
2343 if (!cma_any_addr(addr
)) {
2344 ret
= rdma_translate_ip(addr
, &id
->route
.addr
.dev_addr
);
2348 ret
= cma_acquire_dev(id_priv
);
2353 memcpy(&id
->route
.addr
.src_addr
, addr
, ip_addr_size(addr
));
2354 ret
= cma_get_port(id_priv
);
2360 if (id_priv
->cma_dev
)
2361 cma_release_dev(id_priv
);
2363 cma_comp_exch(id_priv
, CMA_ADDR_BOUND
, CMA_IDLE
);
2366 EXPORT_SYMBOL(rdma_bind_addr
);
2368 static int cma_format_hdr(void *hdr
, enum rdma_port_space ps
,
2369 struct rdma_route
*route
)
2371 struct cma_hdr
*cma_hdr
;
2372 struct sdp_hh
*sdp_hdr
;
2374 if (route
->addr
.src_addr
.ss_family
== AF_INET
) {
2375 struct sockaddr_in
*src4
, *dst4
;
2377 src4
= (struct sockaddr_in
*) &route
->addr
.src_addr
;
2378 dst4
= (struct sockaddr_in
*) &route
->addr
.dst_addr
;
2383 if (sdp_get_majv(sdp_hdr
->sdp_version
) != SDP_MAJ_VERSION
)
2385 sdp_set_ip_ver(sdp_hdr
, 4);
2386 sdp_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2387 sdp_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2388 sdp_hdr
->port
= src4
->sin_port
;
2392 cma_hdr
->cma_version
= CMA_VERSION
;
2393 cma_set_ip_ver(cma_hdr
, 4);
2394 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2395 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2396 cma_hdr
->port
= src4
->sin_port
;
2400 struct sockaddr_in6
*src6
, *dst6
;
2402 src6
= (struct sockaddr_in6
*) &route
->addr
.src_addr
;
2403 dst6
= (struct sockaddr_in6
*) &route
->addr
.dst_addr
;
2408 if (sdp_get_majv(sdp_hdr
->sdp_version
) != SDP_MAJ_VERSION
)
2410 sdp_set_ip_ver(sdp_hdr
, 6);
2411 sdp_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
2412 sdp_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
2413 sdp_hdr
->port
= src6
->sin6_port
;
2417 cma_hdr
->cma_version
= CMA_VERSION
;
2418 cma_set_ip_ver(cma_hdr
, 6);
2419 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
2420 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
2421 cma_hdr
->port
= src6
->sin6_port
;
2428 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
2429 struct ib_cm_event
*ib_event
)
2431 struct rdma_id_private
*id_priv
= cm_id
->context
;
2432 struct rdma_cm_event event
;
2433 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
2436 if (cma_disable_callback(id_priv
, CMA_CONNECT
))
2439 memset(&event
, 0, sizeof event
);
2440 switch (ib_event
->event
) {
2441 case IB_CM_SIDR_REQ_ERROR
:
2442 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2443 event
.status
= -ETIMEDOUT
;
2445 case IB_CM_SIDR_REP_RECEIVED
:
2446 event
.param
.ud
.private_data
= ib_event
->private_data
;
2447 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
2448 if (rep
->status
!= IB_SIDR_SUCCESS
) {
2449 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2450 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
2453 ret
= cma_set_qkey(id_priv
);
2455 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2456 event
.status
= -EINVAL
;
2459 if (id_priv
->qkey
!= rep
->qkey
) {
2460 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2461 event
.status
= -EINVAL
;
2464 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
2465 id_priv
->id
.route
.path_rec
,
2466 &event
.param
.ud
.ah_attr
);
2467 event
.param
.ud
.qp_num
= rep
->qpn
;
2468 event
.param
.ud
.qkey
= rep
->qkey
;
2469 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2473 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
2478 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2480 /* Destroy the CM ID by returning a non-zero value. */
2481 id_priv
->cm_id
.ib
= NULL
;
2482 cma_exch(id_priv
, CMA_DESTROYING
);
2483 mutex_unlock(&id_priv
->handler_mutex
);
2484 rdma_destroy_id(&id_priv
->id
);
2488 mutex_unlock(&id_priv
->handler_mutex
);
2492 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
2493 struct rdma_conn_param
*conn_param
)
2495 struct ib_cm_sidr_req_param req
;
2496 struct rdma_route
*route
;
2499 req
.private_data_len
= sizeof(struct cma_hdr
) +
2500 conn_param
->private_data_len
;
2501 req
.private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2502 if (!req
.private_data
)
2505 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2506 memcpy((void *) req
.private_data
+ sizeof(struct cma_hdr
),
2507 conn_param
->private_data
, conn_param
->private_data_len
);
2509 route
= &id_priv
->id
.route
;
2510 ret
= cma_format_hdr((void *) req
.private_data
, id_priv
->id
.ps
, route
);
2514 id_priv
->cm_id
.ib
= ib_create_cm_id(id_priv
->id
.device
,
2515 cma_sidr_rep_handler
, id_priv
);
2516 if (IS_ERR(id_priv
->cm_id
.ib
)) {
2517 ret
= PTR_ERR(id_priv
->cm_id
.ib
);
2521 req
.path
= route
->path_rec
;
2522 req
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
2523 (struct sockaddr
*) &route
->addr
.dst_addr
);
2524 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
2525 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2527 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
2529 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
2530 id_priv
->cm_id
.ib
= NULL
;
2533 kfree(req
.private_data
);
2537 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
2538 struct rdma_conn_param
*conn_param
)
2540 struct ib_cm_req_param req
;
2541 struct rdma_route
*route
;
2545 memset(&req
, 0, sizeof req
);
2546 offset
= cma_user_data_offset(id_priv
->id
.ps
);
2547 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
2548 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2552 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2553 memcpy(private_data
+ offset
, conn_param
->private_data
,
2554 conn_param
->private_data_len
);
2556 id_priv
->cm_id
.ib
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
,
2558 if (IS_ERR(id_priv
->cm_id
.ib
)) {
2559 ret
= PTR_ERR(id_priv
->cm_id
.ib
);
2563 route
= &id_priv
->id
.route
;
2564 ret
= cma_format_hdr(private_data
, id_priv
->id
.ps
, route
);
2567 req
.private_data
= private_data
;
2569 req
.primary_path
= &route
->path_rec
[0];
2570 if (route
->num_paths
== 2)
2571 req
.alternate_path
= &route
->path_rec
[1];
2573 req
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
2574 (struct sockaddr
*) &route
->addr
.dst_addr
);
2575 req
.qp_num
= id_priv
->qp_num
;
2576 req
.qp_type
= IB_QPT_RC
;
2577 req
.starting_psn
= id_priv
->seq_num
;
2578 req
.responder_resources
= conn_param
->responder_resources
;
2579 req
.initiator_depth
= conn_param
->initiator_depth
;
2580 req
.flow_control
= conn_param
->flow_control
;
2581 req
.retry_count
= conn_param
->retry_count
;
2582 req
.rnr_retry_count
= conn_param
->rnr_retry_count
;
2583 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2584 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2585 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2586 req
.srq
= id_priv
->srq
? 1 : 0;
2588 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
2590 if (ret
&& !IS_ERR(id_priv
->cm_id
.ib
)) {
2591 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
2592 id_priv
->cm_id
.ib
= NULL
;
2595 kfree(private_data
);
2599 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
2600 struct rdma_conn_param
*conn_param
)
2602 struct iw_cm_id
*cm_id
;
2603 struct sockaddr_in
* sin
;
2605 struct iw_cm_conn_param iw_param
;
2607 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
2608 if (IS_ERR(cm_id
)) {
2609 ret
= PTR_ERR(cm_id
);
2613 id_priv
->cm_id
.iw
= cm_id
;
2615 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
2616 cm_id
->local_addr
= *sin
;
2618 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.dst_addr
;
2619 cm_id
->remote_addr
= *sin
;
2621 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2625 iw_param
.ord
= conn_param
->initiator_depth
;
2626 iw_param
.ird
= conn_param
->responder_resources
;
2627 iw_param
.private_data
= conn_param
->private_data
;
2628 iw_param
.private_data_len
= conn_param
->private_data_len
;
2630 iw_param
.qpn
= id_priv
->qp_num
;
2632 iw_param
.qpn
= conn_param
->qp_num
;
2633 ret
= iw_cm_connect(cm_id
, &iw_param
);
2635 if (ret
&& !IS_ERR(cm_id
)) {
2636 iw_destroy_cm_id(cm_id
);
2637 id_priv
->cm_id
.iw
= NULL
;
2642 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2644 struct rdma_id_private
*id_priv
;
2647 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2648 if (!cma_comp_exch(id_priv
, CMA_ROUTE_RESOLVED
, CMA_CONNECT
))
2652 id_priv
->qp_num
= conn_param
->qp_num
;
2653 id_priv
->srq
= conn_param
->srq
;
2656 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2657 case RDMA_TRANSPORT_IB
:
2658 if (cma_is_ud_ps(id
->ps
))
2659 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
2661 ret
= cma_connect_ib(id_priv
, conn_param
);
2663 case RDMA_TRANSPORT_IWARP
:
2664 ret
= cma_connect_iw(id_priv
, conn_param
);
2675 cma_comp_exch(id_priv
, CMA_CONNECT
, CMA_ROUTE_RESOLVED
);
2678 EXPORT_SYMBOL(rdma_connect
);
2680 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
2681 struct rdma_conn_param
*conn_param
)
2683 struct ib_cm_rep_param rep
;
2686 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2690 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
2694 memset(&rep
, 0, sizeof rep
);
2695 rep
.qp_num
= id_priv
->qp_num
;
2696 rep
.starting_psn
= id_priv
->seq_num
;
2697 rep
.private_data
= conn_param
->private_data
;
2698 rep
.private_data_len
= conn_param
->private_data_len
;
2699 rep
.responder_resources
= conn_param
->responder_resources
;
2700 rep
.initiator_depth
= conn_param
->initiator_depth
;
2701 rep
.failover_accepted
= 0;
2702 rep
.flow_control
= conn_param
->flow_control
;
2703 rep
.rnr_retry_count
= conn_param
->rnr_retry_count
;
2704 rep
.srq
= id_priv
->srq
? 1 : 0;
2706 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
2711 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
2712 struct rdma_conn_param
*conn_param
)
2714 struct iw_cm_conn_param iw_param
;
2717 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2721 iw_param
.ord
= conn_param
->initiator_depth
;
2722 iw_param
.ird
= conn_param
->responder_resources
;
2723 iw_param
.private_data
= conn_param
->private_data
;
2724 iw_param
.private_data_len
= conn_param
->private_data_len
;
2725 if (id_priv
->id
.qp
) {
2726 iw_param
.qpn
= id_priv
->qp_num
;
2728 iw_param
.qpn
= conn_param
->qp_num
;
2730 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
2733 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
2734 enum ib_cm_sidr_status status
,
2735 const void *private_data
, int private_data_len
)
2737 struct ib_cm_sidr_rep_param rep
;
2740 memset(&rep
, 0, sizeof rep
);
2741 rep
.status
= status
;
2742 if (status
== IB_SIDR_SUCCESS
) {
2743 ret
= cma_set_qkey(id_priv
);
2746 rep
.qp_num
= id_priv
->qp_num
;
2747 rep
.qkey
= id_priv
->qkey
;
2749 rep
.private_data
= private_data
;
2750 rep
.private_data_len
= private_data_len
;
2752 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
2755 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2757 struct rdma_id_private
*id_priv
;
2760 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2761 if (!cma_comp(id_priv
, CMA_CONNECT
))
2764 if (!id
->qp
&& conn_param
) {
2765 id_priv
->qp_num
= conn_param
->qp_num
;
2766 id_priv
->srq
= conn_param
->srq
;
2769 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2770 case RDMA_TRANSPORT_IB
:
2771 if (cma_is_ud_ps(id
->ps
))
2772 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
2773 conn_param
->private_data
,
2774 conn_param
->private_data_len
);
2775 else if (conn_param
)
2776 ret
= cma_accept_ib(id_priv
, conn_param
);
2778 ret
= cma_rep_recv(id_priv
);
2780 case RDMA_TRANSPORT_IWARP
:
2781 ret
= cma_accept_iw(id_priv
, conn_param
);
2793 cma_modify_qp_err(id_priv
);
2794 rdma_reject(id
, NULL
, 0);
2797 EXPORT_SYMBOL(rdma_accept
);
2799 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
2801 struct rdma_id_private
*id_priv
;
2804 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2805 if (!cma_has_cm_dev(id_priv
))
2808 switch (id
->device
->node_type
) {
2809 case RDMA_NODE_IB_CA
:
2810 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
2818 EXPORT_SYMBOL(rdma_notify
);
2820 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
2821 u8 private_data_len
)
2823 struct rdma_id_private
*id_priv
;
2826 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2827 if (!cma_has_cm_dev(id_priv
))
2830 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2831 case RDMA_TRANSPORT_IB
:
2832 if (cma_is_ud_ps(id
->ps
))
2833 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
,
2834 private_data
, private_data_len
);
2836 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
2837 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
2838 0, private_data
, private_data_len
);
2840 case RDMA_TRANSPORT_IWARP
:
2841 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
2842 private_data
, private_data_len
);
2850 EXPORT_SYMBOL(rdma_reject
);
2852 int rdma_disconnect(struct rdma_cm_id
*id
)
2854 struct rdma_id_private
*id_priv
;
2857 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2858 if (!cma_has_cm_dev(id_priv
))
2861 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2862 case RDMA_TRANSPORT_IB
:
2863 ret
= cma_modify_qp_err(id_priv
);
2866 /* Initiate or respond to a disconnect. */
2867 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
2868 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
2870 case RDMA_TRANSPORT_IWARP
:
2871 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
2880 EXPORT_SYMBOL(rdma_disconnect
);
2882 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
2884 struct rdma_id_private
*id_priv
;
2885 struct cma_multicast
*mc
= multicast
->context
;
2886 struct rdma_cm_event event
;
2889 id_priv
= mc
->id_priv
;
2890 if (cma_disable_callback(id_priv
, CMA_ADDR_BOUND
) &&
2891 cma_disable_callback(id_priv
, CMA_ADDR_RESOLVED
))
2894 mutex_lock(&id_priv
->qp_mutex
);
2895 if (!status
&& id_priv
->id
.qp
)
2896 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
2897 multicast
->rec
.mlid
);
2898 mutex_unlock(&id_priv
->qp_mutex
);
2900 memset(&event
, 0, sizeof event
);
2901 event
.status
= status
;
2902 event
.param
.ud
.private_data
= mc
->context
;
2904 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
2905 ib_init_ah_from_mcmember(id_priv
->id
.device
,
2906 id_priv
->id
.port_num
, &multicast
->rec
,
2907 &event
.param
.ud
.ah_attr
);
2908 event
.param
.ud
.qp_num
= 0xFFFFFF;
2909 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
2911 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
2913 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2915 cma_exch(id_priv
, CMA_DESTROYING
);
2916 mutex_unlock(&id_priv
->handler_mutex
);
2917 rdma_destroy_id(&id_priv
->id
);
2921 mutex_unlock(&id_priv
->handler_mutex
);
2925 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
2926 struct sockaddr
*addr
, union ib_gid
*mgid
)
2928 unsigned char mc_map
[MAX_ADDR_LEN
];
2929 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2930 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
2931 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
2933 if (cma_any_addr(addr
)) {
2934 memset(mgid
, 0, sizeof *mgid
);
2935 } else if ((addr
->sa_family
== AF_INET6
) &&
2936 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
2938 /* IPv6 address is an SA assigned MGID. */
2939 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
2940 } else if ((addr
->sa_family
== AF_INET6
)) {
2941 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
2942 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2943 mc_map
[7] = 0x01; /* Use RDMA CM signature */
2944 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
2946 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
2947 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2948 mc_map
[7] = 0x01; /* Use RDMA CM signature */
2949 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
2953 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
2954 struct cma_multicast
*mc
)
2956 struct ib_sa_mcmember_rec rec
;
2957 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2958 ib_sa_comp_mask comp_mask
;
2961 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
2962 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
2967 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
2968 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2969 rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
2970 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
2971 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2974 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
2975 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
2976 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
2977 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
2978 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
2980 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
2981 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
2982 IB_SA_MCMEMBER_REC_RATE_SELECTOR
;
2984 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
2985 id_priv
->id
.port_num
, &rec
,
2986 comp_mask
, GFP_KERNEL
,
2987 cma_ib_mc_handler
, mc
);
2988 if (IS_ERR(mc
->multicast
.ib
))
2989 return PTR_ERR(mc
->multicast
.ib
);
2994 static void iboe_mcast_work_handler(struct work_struct
*work
)
2996 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
2997 struct cma_multicast
*mc
= mw
->mc
;
2998 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
3000 mc
->multicast
.ib
->context
= mc
;
3001 cma_ib_mc_handler(0, m
);
3002 kref_put(&mc
->mcref
, release_mc
);
3006 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
)
3008 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
3009 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
3011 if (cma_any_addr(addr
)) {
3012 memset(mgid
, 0, sizeof *mgid
);
3013 } else if (addr
->sa_family
== AF_INET6
) {
3014 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3016 mgid
->raw
[0] = 0xff;
3017 mgid
->raw
[1] = 0x0e;
3026 mgid
->raw
[10] = 0xff;
3027 mgid
->raw
[11] = 0xff;
3028 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
3032 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
3033 struct cma_multicast
*mc
)
3035 struct iboe_mcast_work
*work
;
3036 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3038 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
3039 struct net_device
*ndev
= NULL
;
3041 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
3044 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3048 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
3049 if (!mc
->multicast
.ib
) {
3054 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
);
3056 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
3057 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3058 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
3060 if (dev_addr
->bound_dev_if
)
3061 ndev
= dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
3066 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
3067 mc
->multicast
.ib
->rec
.hop_limit
= 1;
3068 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
3070 if (!mc
->multicast
.ib
->rec
.mtu
) {
3074 iboe_addr_get_sgid(dev_addr
, &mc
->multicast
.ib
->rec
.port_gid
);
3077 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
3078 kref_get(&mc
->mcref
);
3079 queue_work(cma_wq
, &work
->work
);
3084 kfree(mc
->multicast
.ib
);
3090 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
3093 struct rdma_id_private
*id_priv
;
3094 struct cma_multicast
*mc
;
3097 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3098 if (!cma_comp(id_priv
, CMA_ADDR_BOUND
) &&
3099 !cma_comp(id_priv
, CMA_ADDR_RESOLVED
))
3102 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
3106 memcpy(&mc
->addr
, addr
, ip_addr_size(addr
));
3107 mc
->context
= context
;
3108 mc
->id_priv
= id_priv
;
3110 spin_lock(&id_priv
->lock
);
3111 list_add(&mc
->list
, &id_priv
->mc_list
);
3112 spin_unlock(&id_priv
->lock
);
3114 switch (rdma_node_get_transport(id
->device
->node_type
)) {
3115 case RDMA_TRANSPORT_IB
:
3116 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
3117 case IB_LINK_LAYER_INFINIBAND
:
3118 ret
= cma_join_ib_multicast(id_priv
, mc
);
3120 case IB_LINK_LAYER_ETHERNET
:
3121 kref_init(&mc
->mcref
);
3122 ret
= cma_iboe_join_multicast(id_priv
, mc
);
3134 spin_lock_irq(&id_priv
->lock
);
3135 list_del(&mc
->list
);
3136 spin_unlock_irq(&id_priv
->lock
);
3141 EXPORT_SYMBOL(rdma_join_multicast
);
3143 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3145 struct rdma_id_private
*id_priv
;
3146 struct cma_multicast
*mc
;
3148 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3149 spin_lock_irq(&id_priv
->lock
);
3150 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
3151 if (!memcmp(&mc
->addr
, addr
, ip_addr_size(addr
))) {
3152 list_del(&mc
->list
);
3153 spin_unlock_irq(&id_priv
->lock
);
3156 ib_detach_mcast(id
->qp
,
3157 &mc
->multicast
.ib
->rec
.mgid
,
3158 mc
->multicast
.ib
->rec
.mlid
);
3159 if (rdma_node_get_transport(id_priv
->cma_dev
->device
->node_type
) == RDMA_TRANSPORT_IB
) {
3160 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
3161 case IB_LINK_LAYER_INFINIBAND
:
3162 ib_sa_free_multicast(mc
->multicast
.ib
);
3165 case IB_LINK_LAYER_ETHERNET
:
3166 kref_put(&mc
->mcref
, release_mc
);
3175 spin_unlock_irq(&id_priv
->lock
);
3177 EXPORT_SYMBOL(rdma_leave_multicast
);
3179 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
3181 struct rdma_dev_addr
*dev_addr
;
3182 struct cma_ndev_work
*work
;
3184 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3186 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
3187 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
3188 printk(KERN_INFO
"RDMA CM addr change for ndev %s used by id %p\n",
3189 ndev
->name
, &id_priv
->id
);
3190 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3194 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
3196 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
3197 atomic_inc(&id_priv
->refcount
);
3198 queue_work(cma_wq
, &work
->work
);
3204 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
3207 struct net_device
*ndev
= (struct net_device
*)ctx
;
3208 struct cma_device
*cma_dev
;
3209 struct rdma_id_private
*id_priv
;
3210 int ret
= NOTIFY_DONE
;
3212 if (dev_net(ndev
) != &init_net
)
3215 if (event
!= NETDEV_BONDING_FAILOVER
)
3218 if (!(ndev
->flags
& IFF_MASTER
) || !(ndev
->priv_flags
& IFF_BONDING
))
3222 list_for_each_entry(cma_dev
, &dev_list
, list
)
3223 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3224 ret
= cma_netdev_change(ndev
, id_priv
);
3230 mutex_unlock(&lock
);
3234 static struct notifier_block cma_nb
= {
3235 .notifier_call
= cma_netdev_callback
3238 static void cma_add_one(struct ib_device
*device
)
3240 struct cma_device
*cma_dev
;
3241 struct rdma_id_private
*id_priv
;
3243 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
3247 cma_dev
->device
= device
;
3249 init_completion(&cma_dev
->comp
);
3250 atomic_set(&cma_dev
->refcount
, 1);
3251 INIT_LIST_HEAD(&cma_dev
->id_list
);
3252 ib_set_client_data(device
, &cma_client
, cma_dev
);
3255 list_add_tail(&cma_dev
->list
, &dev_list
);
3256 list_for_each_entry(id_priv
, &listen_any_list
, list
)
3257 cma_listen_on_dev(id_priv
, cma_dev
);
3258 mutex_unlock(&lock
);
3261 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
3263 struct rdma_cm_event event
;
3264 enum cma_state state
;
3267 /* Record that we want to remove the device */
3268 state
= cma_exch(id_priv
, CMA_DEVICE_REMOVAL
);
3269 if (state
== CMA_DESTROYING
)
3272 cma_cancel_operation(id_priv
, state
);
3273 mutex_lock(&id_priv
->handler_mutex
);
3275 /* Check for destruction from another callback. */
3276 if (!cma_comp(id_priv
, CMA_DEVICE_REMOVAL
))
3279 memset(&event
, 0, sizeof event
);
3280 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
3281 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3283 mutex_unlock(&id_priv
->handler_mutex
);
3287 static void cma_process_remove(struct cma_device
*cma_dev
)
3289 struct rdma_id_private
*id_priv
;
3293 while (!list_empty(&cma_dev
->id_list
)) {
3294 id_priv
= list_entry(cma_dev
->id_list
.next
,
3295 struct rdma_id_private
, list
);
3297 list_del(&id_priv
->listen_list
);
3298 list_del_init(&id_priv
->list
);
3299 atomic_inc(&id_priv
->refcount
);
3300 mutex_unlock(&lock
);
3302 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
3303 cma_deref_id(id_priv
);
3305 rdma_destroy_id(&id_priv
->id
);
3309 mutex_unlock(&lock
);
3311 cma_deref_dev(cma_dev
);
3312 wait_for_completion(&cma_dev
->comp
);
3315 static void cma_remove_one(struct ib_device
*device
)
3317 struct cma_device
*cma_dev
;
3319 cma_dev
= ib_get_client_data(device
, &cma_client
);
3324 list_del(&cma_dev
->list
);
3325 mutex_unlock(&lock
);
3327 cma_process_remove(cma_dev
);
3331 static int __init
cma_init(void)
3335 cma_wq
= create_singlethread_workqueue("rdma_cm");
3339 ib_sa_register_client(&sa_client
);
3340 rdma_addr_register_client(&addr_client
);
3341 register_netdevice_notifier(&cma_nb
);
3343 ret
= ib_register_client(&cma_client
);
3349 unregister_netdevice_notifier(&cma_nb
);
3350 rdma_addr_unregister_client(&addr_client
);
3351 ib_sa_unregister_client(&sa_client
);
3352 destroy_workqueue(cma_wq
);
3356 static void __exit
cma_cleanup(void)
3358 ib_unregister_client(&cma_client
);
3359 unregister_netdevice_notifier(&cma_nb
);
3360 rdma_addr_unregister_client(&addr_client
);
3361 ib_sa_unregister_client(&sa_client
);
3362 destroy_workqueue(cma_wq
);
3363 idr_destroy(&sdp_ps
);
3364 idr_destroy(&tcp_ps
);
3365 idr_destroy(&udp_ps
);
3366 idr_destroy(&ipoib_ps
);
3369 module_init(cma_init
);
3370 module_exit(cma_cleanup
);