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 licensed under one of the following licenses:
9 * 1) under the terms of the "Common Public License 1.0" a copy of which is
10 * available from the Open Source Initiative, see
11 * http://www.opensource.org/licenses/cpl.php.
13 * 2) under the terms of the "The BSD License" a copy of which is
14 * available from the Open Source Initiative, see
15 * http://www.opensource.org/licenses/bsd-license.php.
17 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
18 * copy of which is available from the Open Source Initiative, see
19 * http://www.opensource.org/licenses/gpl-license.php.
21 * Licensee has the right to choose one of the above licenses.
23 * Redistributions of source code must retain the above copyright
24 * notice and one of the license notices.
26 * Redistributions in binary form must reproduce both the above copyright
27 * notice, one of the license notices in the documentation
28 * and/or other materials provided with the distribution.
32 #include <linux/completion.h>
34 #include <linux/in6.h>
35 #include <linux/mutex.h>
36 #include <linux/random.h>
37 #include <linux/idr.h>
38 #include <linux/inetdevice.h>
42 #include <rdma/rdma_cm.h>
43 #include <rdma/rdma_cm_ib.h>
44 #include <rdma/ib_cache.h>
45 #include <rdma/ib_cm.h>
46 #include <rdma/ib_sa.h>
47 #include <rdma/iw_cm.h>
49 MODULE_AUTHOR("Sean Hefty");
50 MODULE_DESCRIPTION("Generic RDMA CM Agent");
51 MODULE_LICENSE("Dual BSD/GPL");
53 #define CMA_CM_RESPONSE_TIMEOUT 20
54 #define CMA_MAX_CM_RETRIES 15
55 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
57 static void cma_add_one(struct ib_device
*device
);
58 static void cma_remove_one(struct ib_device
*device
);
60 static struct ib_client cma_client
= {
63 .remove
= cma_remove_one
66 static struct ib_sa_client sa_client
;
67 static struct rdma_addr_client addr_client
;
68 static LIST_HEAD(dev_list
);
69 static LIST_HEAD(listen_any_list
);
70 static DEFINE_MUTEX(lock
);
71 static struct workqueue_struct
*cma_wq
;
72 static DEFINE_IDR(sdp_ps
);
73 static DEFINE_IDR(tcp_ps
);
74 static DEFINE_IDR(udp_ps
);
75 static DEFINE_IDR(ipoib_ps
);
79 struct list_head list
;
80 struct ib_device
*device
;
81 struct completion comp
;
83 struct list_head id_list
;
100 struct rdma_bind_list
{
102 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 cma_state state
;
125 struct mutex qp_mutex
;
127 struct completion comp
;
129 wait_queue_head_t wait_remove
;
134 struct ib_sa_query
*query
;
148 struct cma_multicast
{
149 struct rdma_id_private
*id_priv
;
151 struct ib_sa_multicast
*ib
;
153 struct list_head list
;
155 struct sockaddr addr
;
156 u8 pad
[sizeof(struct sockaddr_in6
) -
157 sizeof(struct sockaddr
)];
161 struct work_struct work
;
162 struct rdma_id_private
*id
;
163 enum cma_state old_state
;
164 enum cma_state new_state
;
165 struct rdma_cm_event event
;
178 u8 ip_version
; /* IP version: 7:4 */
180 union cma_ip_addr src_addr
;
181 union cma_ip_addr dst_addr
;
186 u8 sdp_version
; /* Major version: 7:4 */
187 u8 ip_version
; /* IP version: 7:4 */
188 u8 sdp_specific1
[10];
190 __be16 sdp_specific2
;
191 union cma_ip_addr src_addr
;
192 union cma_ip_addr dst_addr
;
200 #define CMA_VERSION 0x00
201 #define SDP_MAJ_VERSION 0x2
203 static int cma_comp(struct rdma_id_private
*id_priv
, enum cma_state comp
)
208 spin_lock_irqsave(&id_priv
->lock
, flags
);
209 ret
= (id_priv
->state
== comp
);
210 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
214 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
215 enum cma_state comp
, enum cma_state exch
)
220 spin_lock_irqsave(&id_priv
->lock
, flags
);
221 if ((ret
= (id_priv
->state
== comp
)))
222 id_priv
->state
= exch
;
223 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
227 static enum cma_state
cma_exch(struct rdma_id_private
*id_priv
,
233 spin_lock_irqsave(&id_priv
->lock
, flags
);
234 old
= id_priv
->state
;
235 id_priv
->state
= exch
;
236 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
240 static inline u8
cma_get_ip_ver(struct cma_hdr
*hdr
)
242 return hdr
->ip_version
>> 4;
245 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
247 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
250 static inline u8
sdp_get_majv(u8 sdp_version
)
252 return sdp_version
>> 4;
255 static inline u8
sdp_get_ip_ver(struct sdp_hh
*hh
)
257 return hh
->ip_version
>> 4;
260 static inline void sdp_set_ip_ver(struct sdp_hh
*hh
, u8 ip_ver
)
262 hh
->ip_version
= (ip_ver
<< 4) | (hh
->ip_version
& 0xF);
265 static inline int cma_is_ud_ps(enum rdma_port_space ps
)
267 return (ps
== RDMA_PS_UDP
|| ps
== RDMA_PS_IPOIB
);
270 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
271 struct cma_device
*cma_dev
)
273 atomic_inc(&cma_dev
->refcount
);
274 id_priv
->cma_dev
= cma_dev
;
275 id_priv
->id
.device
= cma_dev
->device
;
276 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
279 static inline void cma_deref_dev(struct cma_device
*cma_dev
)
281 if (atomic_dec_and_test(&cma_dev
->refcount
))
282 complete(&cma_dev
->comp
);
285 static void cma_detach_from_dev(struct rdma_id_private
*id_priv
)
287 list_del(&id_priv
->list
);
288 cma_deref_dev(id_priv
->cma_dev
);
289 id_priv
->cma_dev
= NULL
;
292 static int cma_set_qkey(struct ib_device
*device
, u8 port_num
,
293 enum rdma_port_space ps
,
294 struct rdma_dev_addr
*dev_addr
, u32
*qkey
)
296 struct ib_sa_mcmember_rec rec
;
301 *qkey
= RDMA_UDP_QKEY
;
304 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
305 ret
= ib_sa_get_mcmember_rec(device
, port_num
, &rec
.mgid
, &rec
);
306 *qkey
= be32_to_cpu(rec
.qkey
);
314 static int cma_acquire_dev(struct rdma_id_private
*id_priv
)
316 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
317 struct cma_device
*cma_dev
;
321 switch (rdma_node_get_transport(dev_addr
->dev_type
)) {
322 case RDMA_TRANSPORT_IB
:
323 ib_addr_get_sgid(dev_addr
, &gid
);
325 case RDMA_TRANSPORT_IWARP
:
326 iw_addr_get_sgid(dev_addr
, &gid
);
332 list_for_each_entry(cma_dev
, &dev_list
, list
) {
333 ret
= ib_find_cached_gid(cma_dev
->device
, &gid
,
334 &id_priv
->id
.port_num
, NULL
);
336 ret
= cma_set_qkey(cma_dev
->device
,
337 id_priv
->id
.port_num
,
338 id_priv
->id
.ps
, dev_addr
,
341 cma_attach_to_dev(id_priv
, cma_dev
);
348 static void cma_deref_id(struct rdma_id_private
*id_priv
)
350 if (atomic_dec_and_test(&id_priv
->refcount
))
351 complete(&id_priv
->comp
);
354 static int cma_disable_remove(struct rdma_id_private
*id_priv
,
355 enum cma_state state
)
360 spin_lock_irqsave(&id_priv
->lock
, flags
);
361 if (id_priv
->state
== state
) {
362 atomic_inc(&id_priv
->dev_remove
);
366 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
370 static void cma_enable_remove(struct rdma_id_private
*id_priv
)
372 if (atomic_dec_and_test(&id_priv
->dev_remove
))
373 wake_up(&id_priv
->wait_remove
);
376 static int cma_has_cm_dev(struct rdma_id_private
*id_priv
)
378 return (id_priv
->id
.device
&& id_priv
->cm_id
.ib
);
381 struct rdma_cm_id
*rdma_create_id(rdma_cm_event_handler event_handler
,
382 void *context
, enum rdma_port_space ps
)
384 struct rdma_id_private
*id_priv
;
386 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
388 return ERR_PTR(-ENOMEM
);
390 id_priv
->state
= CMA_IDLE
;
391 id_priv
->id
.context
= context
;
392 id_priv
->id
.event_handler
= event_handler
;
394 spin_lock_init(&id_priv
->lock
);
395 mutex_init(&id_priv
->qp_mutex
);
396 init_completion(&id_priv
->comp
);
397 atomic_set(&id_priv
->refcount
, 1);
398 init_waitqueue_head(&id_priv
->wait_remove
);
399 atomic_set(&id_priv
->dev_remove
, 0);
400 INIT_LIST_HEAD(&id_priv
->listen_list
);
401 INIT_LIST_HEAD(&id_priv
->mc_list
);
402 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
406 EXPORT_SYMBOL(rdma_create_id
);
408 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
410 struct ib_qp_attr qp_attr
;
411 int qp_attr_mask
, ret
;
413 qp_attr
.qp_state
= IB_QPS_INIT
;
414 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
418 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
422 qp_attr
.qp_state
= IB_QPS_RTR
;
423 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
427 qp_attr
.qp_state
= IB_QPS_RTS
;
429 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
434 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
436 struct ib_qp_attr qp_attr
;
437 int qp_attr_mask
, ret
;
439 qp_attr
.qp_state
= IB_QPS_INIT
;
440 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
444 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
447 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
448 struct ib_qp_init_attr
*qp_init_attr
)
450 struct rdma_id_private
*id_priv
;
454 id_priv
= container_of(id
, struct rdma_id_private
, id
);
455 if (id
->device
!= pd
->device
)
458 qp
= ib_create_qp(pd
, qp_init_attr
);
462 if (cma_is_ud_ps(id_priv
->id
.ps
))
463 ret
= cma_init_ud_qp(id_priv
, qp
);
465 ret
= cma_init_conn_qp(id_priv
, qp
);
470 id_priv
->qp_num
= qp
->qp_num
;
471 id_priv
->srq
= (qp
->srq
!= NULL
);
477 EXPORT_SYMBOL(rdma_create_qp
);
479 void rdma_destroy_qp(struct rdma_cm_id
*id
)
481 struct rdma_id_private
*id_priv
;
483 id_priv
= container_of(id
, struct rdma_id_private
, id
);
484 mutex_lock(&id_priv
->qp_mutex
);
485 ib_destroy_qp(id_priv
->id
.qp
);
486 id_priv
->id
.qp
= NULL
;
487 mutex_unlock(&id_priv
->qp_mutex
);
489 EXPORT_SYMBOL(rdma_destroy_qp
);
491 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
492 struct rdma_conn_param
*conn_param
)
494 struct ib_qp_attr qp_attr
;
495 int qp_attr_mask
, ret
;
497 mutex_lock(&id_priv
->qp_mutex
);
498 if (!id_priv
->id
.qp
) {
503 /* Need to update QP attributes from default values. */
504 qp_attr
.qp_state
= IB_QPS_INIT
;
505 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
509 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
513 qp_attr
.qp_state
= IB_QPS_RTR
;
514 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
519 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
520 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
522 mutex_unlock(&id_priv
->qp_mutex
);
526 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
527 struct rdma_conn_param
*conn_param
)
529 struct ib_qp_attr qp_attr
;
530 int qp_attr_mask
, ret
;
532 mutex_lock(&id_priv
->qp_mutex
);
533 if (!id_priv
->id
.qp
) {
538 qp_attr
.qp_state
= IB_QPS_RTS
;
539 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
544 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
545 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
547 mutex_unlock(&id_priv
->qp_mutex
);
551 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
553 struct ib_qp_attr qp_attr
;
556 mutex_lock(&id_priv
->qp_mutex
);
557 if (!id_priv
->id
.qp
) {
562 qp_attr
.qp_state
= IB_QPS_ERR
;
563 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
565 mutex_unlock(&id_priv
->qp_mutex
);
569 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
570 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
572 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
575 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
576 ib_addr_get_pkey(dev_addr
),
577 &qp_attr
->pkey_index
);
581 qp_attr
->port_num
= id_priv
->id
.port_num
;
582 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
584 if (cma_is_ud_ps(id_priv
->id
.ps
)) {
585 qp_attr
->qkey
= id_priv
->qkey
;
586 *qp_attr_mask
|= IB_QP_QKEY
;
588 qp_attr
->qp_access_flags
= 0;
589 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
594 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
597 struct rdma_id_private
*id_priv
;
600 id_priv
= container_of(id
, struct rdma_id_private
, id
);
601 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
602 case RDMA_TRANSPORT_IB
:
603 if (!id_priv
->cm_id
.ib
|| cma_is_ud_ps(id_priv
->id
.ps
))
604 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
606 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
608 if (qp_attr
->qp_state
== IB_QPS_RTR
)
609 qp_attr
->rq_psn
= id_priv
->seq_num
;
611 case RDMA_TRANSPORT_IWARP
:
612 if (!id_priv
->cm_id
.iw
) {
613 qp_attr
->qp_access_flags
= 0;
614 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
616 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
626 EXPORT_SYMBOL(rdma_init_qp_attr
);
628 static inline int cma_zero_addr(struct sockaddr
*addr
)
630 struct in6_addr
*ip6
;
632 if (addr
->sa_family
== AF_INET
)
633 return ipv4_is_zeronet(
634 ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
636 ip6
= &((struct sockaddr_in6
*) addr
)->sin6_addr
;
637 return (ip6
->s6_addr32
[0] | ip6
->s6_addr32
[1] |
638 ip6
->s6_addr32
[2] | ip6
->s6_addr32
[3]) == 0;
642 static inline int cma_loopback_addr(struct sockaddr
*addr
)
644 return ipv4_is_loopback(((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
647 static inline int cma_any_addr(struct sockaddr
*addr
)
649 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
652 static inline __be16
cma_port(struct sockaddr
*addr
)
654 if (addr
->sa_family
== AF_INET
)
655 return ((struct sockaddr_in
*) addr
)->sin_port
;
657 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
660 static inline int cma_any_port(struct sockaddr
*addr
)
662 return !cma_port(addr
);
665 static int cma_get_net_info(void *hdr
, enum rdma_port_space ps
,
666 u8
*ip_ver
, __be16
*port
,
667 union cma_ip_addr
**src
, union cma_ip_addr
**dst
)
671 if (sdp_get_majv(((struct sdp_hh
*) hdr
)->sdp_version
) !=
675 *ip_ver
= sdp_get_ip_ver(hdr
);
676 *port
= ((struct sdp_hh
*) hdr
)->port
;
677 *src
= &((struct sdp_hh
*) hdr
)->src_addr
;
678 *dst
= &((struct sdp_hh
*) hdr
)->dst_addr
;
681 if (((struct cma_hdr
*) hdr
)->cma_version
!= CMA_VERSION
)
684 *ip_ver
= cma_get_ip_ver(hdr
);
685 *port
= ((struct cma_hdr
*) hdr
)->port
;
686 *src
= &((struct cma_hdr
*) hdr
)->src_addr
;
687 *dst
= &((struct cma_hdr
*) hdr
)->dst_addr
;
691 if (*ip_ver
!= 4 && *ip_ver
!= 6)
696 static void cma_save_net_info(struct rdma_addr
*addr
,
697 struct rdma_addr
*listen_addr
,
698 u8 ip_ver
, __be16 port
,
699 union cma_ip_addr
*src
, union cma_ip_addr
*dst
)
701 struct sockaddr_in
*listen4
, *ip4
;
702 struct sockaddr_in6
*listen6
, *ip6
;
706 listen4
= (struct sockaddr_in
*) &listen_addr
->src_addr
;
707 ip4
= (struct sockaddr_in
*) &addr
->src_addr
;
708 ip4
->sin_family
= listen4
->sin_family
;
709 ip4
->sin_addr
.s_addr
= dst
->ip4
.addr
;
710 ip4
->sin_port
= listen4
->sin_port
;
712 ip4
= (struct sockaddr_in
*) &addr
->dst_addr
;
713 ip4
->sin_family
= listen4
->sin_family
;
714 ip4
->sin_addr
.s_addr
= src
->ip4
.addr
;
715 ip4
->sin_port
= port
;
718 listen6
= (struct sockaddr_in6
*) &listen_addr
->src_addr
;
719 ip6
= (struct sockaddr_in6
*) &addr
->src_addr
;
720 ip6
->sin6_family
= listen6
->sin6_family
;
721 ip6
->sin6_addr
= dst
->ip6
;
722 ip6
->sin6_port
= listen6
->sin6_port
;
724 ip6
= (struct sockaddr_in6
*) &addr
->dst_addr
;
725 ip6
->sin6_family
= listen6
->sin6_family
;
726 ip6
->sin6_addr
= src
->ip6
;
727 ip6
->sin6_port
= port
;
734 static inline int cma_user_data_offset(enum rdma_port_space ps
)
740 return sizeof(struct cma_hdr
);
744 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
746 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
747 case RDMA_TRANSPORT_IB
:
749 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
756 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
758 struct rdma_id_private
*dev_id_priv
;
761 * Remove from listen_any_list to prevent added devices from spawning
762 * additional listen requests.
765 list_del(&id_priv
->list
);
767 while (!list_empty(&id_priv
->listen_list
)) {
768 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
769 struct rdma_id_private
, listen_list
);
770 /* sync with device removal to avoid duplicate destruction */
771 list_del_init(&dev_id_priv
->list
);
772 list_del(&dev_id_priv
->listen_list
);
775 rdma_destroy_id(&dev_id_priv
->id
);
781 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
782 enum cma_state state
)
786 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
788 case CMA_ROUTE_QUERY
:
789 cma_cancel_route(id_priv
);
792 if (cma_any_addr(&id_priv
->id
.route
.addr
.src_addr
) &&
794 cma_cancel_listens(id_priv
);
801 static void cma_release_port(struct rdma_id_private
*id_priv
)
803 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
809 hlist_del(&id_priv
->node
);
810 if (hlist_empty(&bind_list
->owners
)) {
811 idr_remove(bind_list
->ps
, bind_list
->port
);
817 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
819 struct cma_multicast
*mc
;
821 while (!list_empty(&id_priv
->mc_list
)) {
822 mc
= container_of(id_priv
->mc_list
.next
,
823 struct cma_multicast
, list
);
825 ib_sa_free_multicast(mc
->multicast
.ib
);
830 void rdma_destroy_id(struct rdma_cm_id
*id
)
832 struct rdma_id_private
*id_priv
;
833 enum cma_state state
;
835 id_priv
= container_of(id
, struct rdma_id_private
, id
);
836 state
= cma_exch(id_priv
, CMA_DESTROYING
);
837 cma_cancel_operation(id_priv
, state
);
840 if (id_priv
->cma_dev
) {
842 switch (rdma_node_get_transport(id
->device
->node_type
)) {
843 case RDMA_TRANSPORT_IB
:
844 if (id_priv
->cm_id
.ib
&& !IS_ERR(id_priv
->cm_id
.ib
))
845 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
847 case RDMA_TRANSPORT_IWARP
:
848 if (id_priv
->cm_id
.iw
&& !IS_ERR(id_priv
->cm_id
.iw
))
849 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
854 cma_leave_mc_groups(id_priv
);
856 cma_detach_from_dev(id_priv
);
860 cma_release_port(id_priv
);
861 cma_deref_id(id_priv
);
862 wait_for_completion(&id_priv
->comp
);
864 if (id_priv
->internal_id
)
865 cma_deref_id(id_priv
->id
.context
);
867 kfree(id_priv
->id
.route
.path_rec
);
870 EXPORT_SYMBOL(rdma_destroy_id
);
872 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
876 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
880 ret
= cma_modify_qp_rts(id_priv
, NULL
);
884 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
890 cma_modify_qp_err(id_priv
);
891 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
896 static int cma_verify_rep(struct rdma_id_private
*id_priv
, void *data
)
898 if (id_priv
->id
.ps
== RDMA_PS_SDP
&&
899 sdp_get_majv(((struct sdp_hah
*) data
)->sdp_version
) !=
906 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
907 struct ib_cm_rep_event_param
*rep_data
,
910 event
->param
.conn
.private_data
= private_data
;
911 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
912 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
913 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
914 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
915 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
916 event
->param
.conn
.srq
= rep_data
->srq
;
917 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
920 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
922 struct rdma_id_private
*id_priv
= cm_id
->context
;
923 struct rdma_cm_event event
;
926 if (cma_disable_remove(id_priv
, CMA_CONNECT
))
929 memset(&event
, 0, sizeof event
);
930 switch (ib_event
->event
) {
931 case IB_CM_REQ_ERROR
:
932 case IB_CM_REP_ERROR
:
933 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
934 event
.status
= -ETIMEDOUT
;
936 case IB_CM_REP_RECEIVED
:
937 event
.status
= cma_verify_rep(id_priv
, ib_event
->private_data
);
939 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
940 else if (id_priv
->id
.qp
&& id_priv
->id
.ps
!= RDMA_PS_SDP
) {
941 event
.status
= cma_rep_recv(id_priv
);
942 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
943 RDMA_CM_EVENT_ESTABLISHED
;
945 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
946 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
947 ib_event
->private_data
);
949 case IB_CM_RTU_RECEIVED
:
950 case IB_CM_USER_ESTABLISHED
:
951 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
953 case IB_CM_DREQ_ERROR
:
954 event
.status
= -ETIMEDOUT
; /* fall through */
955 case IB_CM_DREQ_RECEIVED
:
956 case IB_CM_DREP_RECEIVED
:
957 if (!cma_comp_exch(id_priv
, CMA_CONNECT
, CMA_DISCONNECT
))
959 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
961 case IB_CM_TIMEWAIT_EXIT
:
962 case IB_CM_MRA_RECEIVED
:
965 case IB_CM_REJ_RECEIVED
:
966 cma_modify_qp_err(id_priv
);
967 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
968 event
.event
= RDMA_CM_EVENT_REJECTED
;
969 event
.param
.conn
.private_data
= ib_event
->private_data
;
970 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
973 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d",
978 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
980 /* Destroy the CM ID by returning a non-zero value. */
981 id_priv
->cm_id
.ib
= NULL
;
982 cma_exch(id_priv
, CMA_DESTROYING
);
983 cma_enable_remove(id_priv
);
984 rdma_destroy_id(&id_priv
->id
);
988 cma_enable_remove(id_priv
);
992 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
993 struct ib_cm_event
*ib_event
)
995 struct rdma_id_private
*id_priv
;
996 struct rdma_cm_id
*id
;
997 struct rdma_route
*rt
;
998 union cma_ip_addr
*src
, *dst
;
1002 if (cma_get_net_info(ib_event
->private_data
, listen_id
->ps
,
1003 &ip_ver
, &port
, &src
, &dst
))
1006 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1011 cma_save_net_info(&id
->route
.addr
, &listen_id
->route
.addr
,
1012 ip_ver
, port
, src
, dst
);
1015 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1016 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1021 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1022 if (rt
->num_paths
== 2)
1023 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1025 ib_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1026 ib_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1027 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1028 rt
->addr
.dev_addr
.dev_type
= RDMA_NODE_IB_CA
;
1030 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1031 id_priv
->state
= CMA_CONNECT
;
1035 rdma_destroy_id(id
);
1040 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1041 struct ib_cm_event
*ib_event
)
1043 struct rdma_id_private
*id_priv
;
1044 struct rdma_cm_id
*id
;
1045 union cma_ip_addr
*src
, *dst
;
1050 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1056 if (cma_get_net_info(ib_event
->private_data
, listen_id
->ps
,
1057 &ip_ver
, &port
, &src
, &dst
))
1060 cma_save_net_info(&id
->route
.addr
, &listen_id
->route
.addr
,
1061 ip_ver
, port
, src
, dst
);
1063 ret
= rdma_translate_ip(&id
->route
.addr
.src_addr
,
1064 &id
->route
.addr
.dev_addr
);
1068 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1069 id_priv
->state
= CMA_CONNECT
;
1072 rdma_destroy_id(id
);
1076 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1077 struct ib_cm_req_event_param
*req_data
,
1078 void *private_data
, int offset
)
1080 event
->param
.conn
.private_data
= private_data
+ offset
;
1081 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1082 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1083 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1084 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1085 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1086 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1087 event
->param
.conn
.srq
= req_data
->srq
;
1088 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1091 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1093 struct rdma_id_private
*listen_id
, *conn_id
;
1094 struct rdma_cm_event event
;
1097 listen_id
= cm_id
->context
;
1098 if (cma_disable_remove(listen_id
, CMA_LISTEN
))
1099 return -ECONNABORTED
;
1101 memset(&event
, 0, sizeof event
);
1102 offset
= cma_user_data_offset(listen_id
->id
.ps
);
1103 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1104 if (cma_is_ud_ps(listen_id
->id
.ps
)) {
1105 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
);
1106 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1107 event
.param
.ud
.private_data_len
=
1108 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1110 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
);
1111 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1112 ib_event
->private_data
, offset
);
1119 atomic_inc(&conn_id
->dev_remove
);
1121 ret
= cma_acquire_dev(conn_id
);
1122 mutex_unlock(&lock
);
1124 goto release_conn_id
;
1126 conn_id
->cm_id
.ib
= cm_id
;
1127 cm_id
->context
= conn_id
;
1128 cm_id
->cm_handler
= cma_ib_handler
;
1130 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1133 * Acquire mutex to prevent user executing rdma_destroy_id()
1134 * while we're accessing the cm_id.
1137 if (cma_comp(conn_id
, CMA_CONNECT
) &&
1138 !cma_is_ud_ps(conn_id
->id
.ps
))
1139 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1140 mutex_unlock(&lock
);
1141 cma_enable_remove(conn_id
);
1145 /* Destroy the CM ID by returning a non-zero value. */
1146 conn_id
->cm_id
.ib
= NULL
;
1149 cma_exch(conn_id
, CMA_DESTROYING
);
1150 cma_enable_remove(conn_id
);
1151 rdma_destroy_id(&conn_id
->id
);
1154 cma_enable_remove(listen_id
);
1158 static __be64
cma_get_service_id(enum rdma_port_space ps
, struct sockaddr
*addr
)
1160 return cpu_to_be64(((u64
)ps
<< 16) + be16_to_cpu(cma_port(addr
)));
1163 static void cma_set_compare_data(enum rdma_port_space ps
, struct sockaddr
*addr
,
1164 struct ib_cm_compare_data
*compare
)
1166 struct cma_hdr
*cma_data
, *cma_mask
;
1167 struct sdp_hh
*sdp_data
, *sdp_mask
;
1169 struct in6_addr ip6_addr
;
1171 memset(compare
, 0, sizeof *compare
);
1172 cma_data
= (void *) compare
->data
;
1173 cma_mask
= (void *) compare
->mask
;
1174 sdp_data
= (void *) compare
->data
;
1175 sdp_mask
= (void *) compare
->mask
;
1177 switch (addr
->sa_family
) {
1179 ip4_addr
= ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
;
1180 if (ps
== RDMA_PS_SDP
) {
1181 sdp_set_ip_ver(sdp_data
, 4);
1182 sdp_set_ip_ver(sdp_mask
, 0xF);
1183 sdp_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1184 sdp_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1186 cma_set_ip_ver(cma_data
, 4);
1187 cma_set_ip_ver(cma_mask
, 0xF);
1188 cma_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1189 cma_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1193 ip6_addr
= ((struct sockaddr_in6
*) addr
)->sin6_addr
;
1194 if (ps
== RDMA_PS_SDP
) {
1195 sdp_set_ip_ver(sdp_data
, 6);
1196 sdp_set_ip_ver(sdp_mask
, 0xF);
1197 sdp_data
->dst_addr
.ip6
= ip6_addr
;
1198 memset(&sdp_mask
->dst_addr
.ip6
, 0xFF,
1199 sizeof sdp_mask
->dst_addr
.ip6
);
1201 cma_set_ip_ver(cma_data
, 6);
1202 cma_set_ip_ver(cma_mask
, 0xF);
1203 cma_data
->dst_addr
.ip6
= ip6_addr
;
1204 memset(&cma_mask
->dst_addr
.ip6
, 0xFF,
1205 sizeof cma_mask
->dst_addr
.ip6
);
1213 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
1215 struct rdma_id_private
*id_priv
= iw_id
->context
;
1216 struct rdma_cm_event event
;
1217 struct sockaddr_in
*sin
;
1220 if (cma_disable_remove(id_priv
, CMA_CONNECT
))
1223 memset(&event
, 0, sizeof event
);
1224 switch (iw_event
->event
) {
1225 case IW_CM_EVENT_CLOSE
:
1226 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1228 case IW_CM_EVENT_CONNECT_REPLY
:
1229 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1230 *sin
= iw_event
->local_addr
;
1231 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.dst_addr
;
1232 *sin
= iw_event
->remote_addr
;
1233 switch (iw_event
->status
) {
1235 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1239 event
.event
= RDMA_CM_EVENT_REJECTED
;
1242 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1245 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1249 case IW_CM_EVENT_ESTABLISHED
:
1250 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1256 event
.status
= iw_event
->status
;
1257 event
.param
.conn
.private_data
= iw_event
->private_data
;
1258 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1259 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1261 /* Destroy the CM ID by returning a non-zero value. */
1262 id_priv
->cm_id
.iw
= NULL
;
1263 cma_exch(id_priv
, CMA_DESTROYING
);
1264 cma_enable_remove(id_priv
);
1265 rdma_destroy_id(&id_priv
->id
);
1269 cma_enable_remove(id_priv
);
1273 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
1274 struct iw_cm_event
*iw_event
)
1276 struct rdma_cm_id
*new_cm_id
;
1277 struct rdma_id_private
*listen_id
, *conn_id
;
1278 struct sockaddr_in
*sin
;
1279 struct net_device
*dev
= NULL
;
1280 struct rdma_cm_event event
;
1282 struct ib_device_attr attr
;
1284 listen_id
= cm_id
->context
;
1285 if (cma_disable_remove(listen_id
, CMA_LISTEN
))
1286 return -ECONNABORTED
;
1288 /* Create a new RDMA id for the new IW CM ID */
1289 new_cm_id
= rdma_create_id(listen_id
->id
.event_handler
,
1290 listen_id
->id
.context
,
1292 if (IS_ERR(new_cm_id
)) {
1296 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
1297 atomic_inc(&conn_id
->dev_remove
);
1298 conn_id
->state
= CMA_CONNECT
;
1300 dev
= ip_dev_find(&init_net
, iw_event
->local_addr
.sin_addr
.s_addr
);
1302 ret
= -EADDRNOTAVAIL
;
1303 cma_enable_remove(conn_id
);
1304 rdma_destroy_id(new_cm_id
);
1307 ret
= rdma_copy_addr(&conn_id
->id
.route
.addr
.dev_addr
, dev
, NULL
);
1309 cma_enable_remove(conn_id
);
1310 rdma_destroy_id(new_cm_id
);
1315 ret
= cma_acquire_dev(conn_id
);
1316 mutex_unlock(&lock
);
1318 cma_enable_remove(conn_id
);
1319 rdma_destroy_id(new_cm_id
);
1323 conn_id
->cm_id
.iw
= cm_id
;
1324 cm_id
->context
= conn_id
;
1325 cm_id
->cm_handler
= cma_iw_handler
;
1327 sin
= (struct sockaddr_in
*) &new_cm_id
->route
.addr
.src_addr
;
1328 *sin
= iw_event
->local_addr
;
1329 sin
= (struct sockaddr_in
*) &new_cm_id
->route
.addr
.dst_addr
;
1330 *sin
= iw_event
->remote_addr
;
1332 ret
= ib_query_device(conn_id
->id
.device
, &attr
);
1334 cma_enable_remove(conn_id
);
1335 rdma_destroy_id(new_cm_id
);
1339 memset(&event
, 0, sizeof event
);
1340 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1341 event
.param
.conn
.private_data
= iw_event
->private_data
;
1342 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1343 event
.param
.conn
.initiator_depth
= attr
.max_qp_init_rd_atom
;
1344 event
.param
.conn
.responder_resources
= attr
.max_qp_rd_atom
;
1345 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1347 /* User wants to destroy the CM ID */
1348 conn_id
->cm_id
.iw
= NULL
;
1349 cma_exch(conn_id
, CMA_DESTROYING
);
1350 cma_enable_remove(conn_id
);
1351 rdma_destroy_id(&conn_id
->id
);
1357 cma_enable_remove(listen_id
);
1361 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
1363 struct ib_cm_compare_data compare_data
;
1364 struct sockaddr
*addr
;
1368 id_priv
->cm_id
.ib
= ib_create_cm_id(id_priv
->id
.device
, cma_req_handler
,
1370 if (IS_ERR(id_priv
->cm_id
.ib
))
1371 return PTR_ERR(id_priv
->cm_id
.ib
);
1373 addr
= &id_priv
->id
.route
.addr
.src_addr
;
1374 svc_id
= cma_get_service_id(id_priv
->id
.ps
, addr
);
1375 if (cma_any_addr(addr
))
1376 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, NULL
);
1378 cma_set_compare_data(id_priv
->id
.ps
, addr
, &compare_data
);
1379 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, &compare_data
);
1383 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1384 id_priv
->cm_id
.ib
= NULL
;
1390 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
1393 struct sockaddr_in
*sin
;
1395 id_priv
->cm_id
.iw
= iw_create_cm_id(id_priv
->id
.device
,
1396 iw_conn_req_handler
,
1398 if (IS_ERR(id_priv
->cm_id
.iw
))
1399 return PTR_ERR(id_priv
->cm_id
.iw
);
1401 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1402 id_priv
->cm_id
.iw
->local_addr
= *sin
;
1404 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
1407 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1408 id_priv
->cm_id
.iw
= NULL
;
1414 static int cma_listen_handler(struct rdma_cm_id
*id
,
1415 struct rdma_cm_event
*event
)
1417 struct rdma_id_private
*id_priv
= id
->context
;
1419 id
->context
= id_priv
->id
.context
;
1420 id
->event_handler
= id_priv
->id
.event_handler
;
1421 return id_priv
->id
.event_handler(id
, event
);
1424 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
1425 struct cma_device
*cma_dev
)
1427 struct rdma_id_private
*dev_id_priv
;
1428 struct rdma_cm_id
*id
;
1431 id
= rdma_create_id(cma_listen_handler
, id_priv
, id_priv
->id
.ps
);
1435 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
1437 dev_id_priv
->state
= CMA_ADDR_BOUND
;
1438 memcpy(&id
->route
.addr
.src_addr
, &id_priv
->id
.route
.addr
.src_addr
,
1439 ip_addr_size(&id_priv
->id
.route
.addr
.src_addr
));
1441 cma_attach_to_dev(dev_id_priv
, cma_dev
);
1442 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
1443 atomic_inc(&id_priv
->refcount
);
1444 dev_id_priv
->internal_id
= 1;
1446 ret
= rdma_listen(id
, id_priv
->backlog
);
1448 printk(KERN_WARNING
"RDMA CMA: cma_listen_on_dev, error %d, "
1449 "listening on device %s", ret
, cma_dev
->device
->name
);
1452 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
1454 struct cma_device
*cma_dev
;
1457 list_add_tail(&id_priv
->list
, &listen_any_list
);
1458 list_for_each_entry(cma_dev
, &dev_list
, list
)
1459 cma_listen_on_dev(id_priv
, cma_dev
);
1460 mutex_unlock(&lock
);
1463 static int cma_bind_any(struct rdma_cm_id
*id
, sa_family_t af
)
1465 struct sockaddr_in addr_in
;
1467 memset(&addr_in
, 0, sizeof addr_in
);
1468 addr_in
.sin_family
= af
;
1469 return rdma_bind_addr(id
, (struct sockaddr
*) &addr_in
);
1472 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
1474 struct rdma_id_private
*id_priv
;
1477 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1478 if (id_priv
->state
== CMA_IDLE
) {
1479 ret
= cma_bind_any(id
, AF_INET
);
1484 if (!cma_comp_exch(id_priv
, CMA_ADDR_BOUND
, CMA_LISTEN
))
1487 id_priv
->backlog
= backlog
;
1489 switch (rdma_node_get_transport(id
->device
->node_type
)) {
1490 case RDMA_TRANSPORT_IB
:
1491 ret
= cma_ib_listen(id_priv
);
1495 case RDMA_TRANSPORT_IWARP
:
1496 ret
= cma_iw_listen(id_priv
, backlog
);
1505 cma_listen_on_all(id_priv
);
1509 id_priv
->backlog
= 0;
1510 cma_comp_exch(id_priv
, CMA_LISTEN
, CMA_ADDR_BOUND
);
1513 EXPORT_SYMBOL(rdma_listen
);
1515 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
1517 struct rdma_id_private
*id_priv
;
1519 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1520 id_priv
->tos
= (u8
) tos
;
1522 EXPORT_SYMBOL(rdma_set_service_type
);
1524 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
1527 struct cma_work
*work
= context
;
1528 struct rdma_route
*route
;
1530 route
= &work
->id
->id
.route
;
1533 route
->num_paths
= 1;
1534 *route
->path_rec
= *path_rec
;
1536 work
->old_state
= CMA_ROUTE_QUERY
;
1537 work
->new_state
= CMA_ADDR_RESOLVED
;
1538 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
1539 work
->event
.status
= status
;
1542 queue_work(cma_wq
, &work
->work
);
1545 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
1546 struct cma_work
*work
)
1548 struct rdma_addr
*addr
= &id_priv
->id
.route
.addr
;
1549 struct ib_sa_path_rec path_rec
;
1550 ib_sa_comp_mask comp_mask
;
1551 struct sockaddr_in6
*sin6
;
1553 memset(&path_rec
, 0, sizeof path_rec
);
1554 ib_addr_get_sgid(&addr
->dev_addr
, &path_rec
.sgid
);
1555 ib_addr_get_dgid(&addr
->dev_addr
, &path_rec
.dgid
);
1556 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(&addr
->dev_addr
));
1557 path_rec
.numb_path
= 1;
1558 path_rec
.reversible
= 1;
1559 path_rec
.service_id
= cma_get_service_id(id_priv
->id
.ps
, &addr
->dst_addr
);
1561 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
1562 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
1563 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
1565 if (addr
->src_addr
.sa_family
== AF_INET
) {
1566 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
1567 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
1569 sin6
= (struct sockaddr_in6
*) &addr
->src_addr
;
1570 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
1571 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
1574 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
1575 id_priv
->id
.port_num
, &path_rec
,
1576 comp_mask
, timeout_ms
,
1577 GFP_KERNEL
, cma_query_handler
,
1578 work
, &id_priv
->query
);
1580 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
1583 static void cma_work_handler(struct work_struct
*_work
)
1585 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
1586 struct rdma_id_private
*id_priv
= work
->id
;
1589 atomic_inc(&id_priv
->dev_remove
);
1590 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
1593 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1594 cma_exch(id_priv
, CMA_DESTROYING
);
1598 cma_enable_remove(id_priv
);
1599 cma_deref_id(id_priv
);
1601 rdma_destroy_id(&id_priv
->id
);
1605 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1607 struct rdma_route
*route
= &id_priv
->id
.route
;
1608 struct cma_work
*work
;
1611 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1616 INIT_WORK(&work
->work
, cma_work_handler
);
1617 work
->old_state
= CMA_ROUTE_QUERY
;
1618 work
->new_state
= CMA_ROUTE_RESOLVED
;
1619 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1621 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1622 if (!route
->path_rec
) {
1627 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
1633 kfree(route
->path_rec
);
1634 route
->path_rec
= NULL
;
1640 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
1641 struct ib_sa_path_rec
*path_rec
, int num_paths
)
1643 struct rdma_id_private
*id_priv
;
1646 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1647 if (!cma_comp_exch(id_priv
, CMA_ADDR_RESOLVED
, CMA_ROUTE_RESOLVED
))
1650 id
->route
.path_rec
= kmalloc(sizeof *path_rec
* num_paths
, GFP_KERNEL
);
1651 if (!id
->route
.path_rec
) {
1656 memcpy(id
->route
.path_rec
, path_rec
, sizeof *path_rec
* num_paths
);
1659 cma_comp_exch(id_priv
, CMA_ROUTE_RESOLVED
, CMA_ADDR_RESOLVED
);
1662 EXPORT_SYMBOL(rdma_set_ib_paths
);
1664 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1666 struct cma_work
*work
;
1668 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1673 INIT_WORK(&work
->work
, cma_work_handler
);
1674 work
->old_state
= CMA_ROUTE_QUERY
;
1675 work
->new_state
= CMA_ROUTE_RESOLVED
;
1676 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1677 queue_work(cma_wq
, &work
->work
);
1681 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
1683 struct rdma_id_private
*id_priv
;
1686 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1687 if (!cma_comp_exch(id_priv
, CMA_ADDR_RESOLVED
, CMA_ROUTE_QUERY
))
1690 atomic_inc(&id_priv
->refcount
);
1691 switch (rdma_node_get_transport(id
->device
->node_type
)) {
1692 case RDMA_TRANSPORT_IB
:
1693 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
1695 case RDMA_TRANSPORT_IWARP
:
1696 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
1707 cma_comp_exch(id_priv
, CMA_ROUTE_QUERY
, CMA_ADDR_RESOLVED
);
1708 cma_deref_id(id_priv
);
1711 EXPORT_SYMBOL(rdma_resolve_route
);
1713 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
1715 struct cma_device
*cma_dev
;
1716 struct ib_port_attr port_attr
;
1723 if (list_empty(&dev_list
)) {
1727 list_for_each_entry(cma_dev
, &dev_list
, list
)
1728 for (p
= 1; p
<= cma_dev
->device
->phys_port_cnt
; ++p
)
1729 if (!ib_query_port(cma_dev
->device
, p
, &port_attr
) &&
1730 port_attr
.state
== IB_PORT_ACTIVE
)
1734 cma_dev
= list_entry(dev_list
.next
, struct cma_device
, list
);
1737 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
);
1741 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
1745 ib_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1746 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
1747 id_priv
->id
.port_num
= p
;
1748 cma_attach_to_dev(id_priv
, cma_dev
);
1750 mutex_unlock(&lock
);
1754 static void addr_handler(int status
, struct sockaddr
*src_addr
,
1755 struct rdma_dev_addr
*dev_addr
, void *context
)
1757 struct rdma_id_private
*id_priv
= context
;
1758 struct rdma_cm_event event
;
1760 memset(&event
, 0, sizeof event
);
1761 atomic_inc(&id_priv
->dev_remove
);
1764 * Grab mutex to block rdma_destroy_id() from removing the device while
1765 * we're trying to acquire it.
1768 if (!cma_comp_exch(id_priv
, CMA_ADDR_QUERY
, CMA_ADDR_RESOLVED
)) {
1769 mutex_unlock(&lock
);
1773 if (!status
&& !id_priv
->cma_dev
)
1774 status
= cma_acquire_dev(id_priv
);
1775 mutex_unlock(&lock
);
1778 if (!cma_comp_exch(id_priv
, CMA_ADDR_RESOLVED
, CMA_ADDR_BOUND
))
1780 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
1781 event
.status
= status
;
1783 memcpy(&id_priv
->id
.route
.addr
.src_addr
, src_addr
,
1784 ip_addr_size(src_addr
));
1785 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
1788 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
1789 cma_exch(id_priv
, CMA_DESTROYING
);
1790 cma_enable_remove(id_priv
);
1791 cma_deref_id(id_priv
);
1792 rdma_destroy_id(&id_priv
->id
);
1796 cma_enable_remove(id_priv
);
1797 cma_deref_id(id_priv
);
1800 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
1802 struct cma_work
*work
;
1803 struct sockaddr_in
*src_in
, *dst_in
;
1807 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1811 if (!id_priv
->cma_dev
) {
1812 ret
= cma_bind_loopback(id_priv
);
1817 ib_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1818 ib_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1820 if (cma_zero_addr(&id_priv
->id
.route
.addr
.src_addr
)) {
1821 src_in
= (struct sockaddr_in
*)&id_priv
->id
.route
.addr
.src_addr
;
1822 dst_in
= (struct sockaddr_in
*)&id_priv
->id
.route
.addr
.dst_addr
;
1823 src_in
->sin_family
= dst_in
->sin_family
;
1824 src_in
->sin_addr
.s_addr
= dst_in
->sin_addr
.s_addr
;
1828 INIT_WORK(&work
->work
, cma_work_handler
);
1829 work
->old_state
= CMA_ADDR_QUERY
;
1830 work
->new_state
= CMA_ADDR_RESOLVED
;
1831 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
1832 queue_work(cma_wq
, &work
->work
);
1839 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
1840 struct sockaddr
*dst_addr
)
1842 if (src_addr
&& src_addr
->sa_family
)
1843 return rdma_bind_addr(id
, src_addr
);
1845 return cma_bind_any(id
, dst_addr
->sa_family
);
1848 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
1849 struct sockaddr
*dst_addr
, int timeout_ms
)
1851 struct rdma_id_private
*id_priv
;
1854 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1855 if (id_priv
->state
== CMA_IDLE
) {
1856 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
1861 if (!cma_comp_exch(id_priv
, CMA_ADDR_BOUND
, CMA_ADDR_QUERY
))
1864 atomic_inc(&id_priv
->refcount
);
1865 memcpy(&id
->route
.addr
.dst_addr
, dst_addr
, ip_addr_size(dst_addr
));
1866 if (cma_any_addr(dst_addr
))
1867 ret
= cma_resolve_loopback(id_priv
);
1869 ret
= rdma_resolve_ip(&addr_client
, &id
->route
.addr
.src_addr
,
1870 dst_addr
, &id
->route
.addr
.dev_addr
,
1871 timeout_ms
, addr_handler
, id_priv
);
1877 cma_comp_exch(id_priv
, CMA_ADDR_QUERY
, CMA_ADDR_BOUND
);
1878 cma_deref_id(id_priv
);
1881 EXPORT_SYMBOL(rdma_resolve_addr
);
1883 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
1884 struct rdma_id_private
*id_priv
)
1886 struct sockaddr_in
*sin
;
1888 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1889 sin
->sin_port
= htons(bind_list
->port
);
1890 id_priv
->bind_list
= bind_list
;
1891 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
1894 static int cma_alloc_port(struct idr
*ps
, struct rdma_id_private
*id_priv
,
1895 unsigned short snum
)
1897 struct rdma_bind_list
*bind_list
;
1900 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
1905 ret
= idr_get_new_above(ps
, bind_list
, snum
, &port
);
1906 } while ((ret
== -EAGAIN
) && idr_pre_get(ps
, GFP_KERNEL
));
1912 ret
= -EADDRNOTAVAIL
;
1917 bind_list
->port
= (unsigned short) port
;
1918 cma_bind_port(bind_list
, id_priv
);
1921 idr_remove(ps
, port
);
1927 static int cma_alloc_any_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
1929 struct rdma_bind_list
*bind_list
;
1930 int port
, ret
, low
, high
;
1932 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
1937 /* FIXME: add proper port randomization per like inet_csk_get_port */
1939 ret
= idr_get_new_above(ps
, bind_list
, next_port
, &port
);
1940 } while ((ret
== -EAGAIN
) && idr_pre_get(ps
, GFP_KERNEL
));
1945 inet_get_local_port_range(&low
, &high
);
1947 if (next_port
!= low
) {
1948 idr_remove(ps
, port
);
1952 ret
= -EADDRNOTAVAIL
;
1959 next_port
= port
+ 1;
1962 bind_list
->port
= (unsigned short) port
;
1963 cma_bind_port(bind_list
, id_priv
);
1966 idr_remove(ps
, port
);
1972 static int cma_use_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
1974 struct rdma_id_private
*cur_id
;
1975 struct sockaddr_in
*sin
, *cur_sin
;
1976 struct rdma_bind_list
*bind_list
;
1977 struct hlist_node
*node
;
1978 unsigned short snum
;
1980 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1981 snum
= ntohs(sin
->sin_port
);
1982 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
1985 bind_list
= idr_find(ps
, snum
);
1987 return cma_alloc_port(ps
, id_priv
, snum
);
1990 * We don't support binding to any address if anyone is bound to
1991 * a specific address on the same port.
1993 if (cma_any_addr(&id_priv
->id
.route
.addr
.src_addr
))
1994 return -EADDRNOTAVAIL
;
1996 hlist_for_each_entry(cur_id
, node
, &bind_list
->owners
, node
) {
1997 if (cma_any_addr(&cur_id
->id
.route
.addr
.src_addr
))
1998 return -EADDRNOTAVAIL
;
2000 cur_sin
= (struct sockaddr_in
*) &cur_id
->id
.route
.addr
.src_addr
;
2001 if (sin
->sin_addr
.s_addr
== cur_sin
->sin_addr
.s_addr
)
2005 cma_bind_port(bind_list
, id_priv
);
2009 static int cma_get_port(struct rdma_id_private
*id_priv
)
2014 switch (id_priv
->id
.ps
) {
2028 return -EPROTONOSUPPORT
;
2032 if (cma_any_port(&id_priv
->id
.route
.addr
.src_addr
))
2033 ret
= cma_alloc_any_port(ps
, id_priv
);
2035 ret
= cma_use_port(ps
, id_priv
);
2036 mutex_unlock(&lock
);
2041 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2043 struct rdma_id_private
*id_priv
;
2046 if (addr
->sa_family
!= AF_INET
)
2047 return -EAFNOSUPPORT
;
2049 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2050 if (!cma_comp_exch(id_priv
, CMA_IDLE
, CMA_ADDR_BOUND
))
2053 if (!cma_any_addr(addr
)) {
2054 ret
= rdma_translate_ip(addr
, &id
->route
.addr
.dev_addr
);
2059 ret
= cma_acquire_dev(id_priv
);
2060 mutex_unlock(&lock
);
2065 memcpy(&id
->route
.addr
.src_addr
, addr
, ip_addr_size(addr
));
2066 ret
= cma_get_port(id_priv
);
2072 if (!cma_any_addr(addr
)) {
2074 cma_detach_from_dev(id_priv
);
2075 mutex_unlock(&lock
);
2078 cma_comp_exch(id_priv
, CMA_ADDR_BOUND
, CMA_IDLE
);
2081 EXPORT_SYMBOL(rdma_bind_addr
);
2083 static int cma_format_hdr(void *hdr
, enum rdma_port_space ps
,
2084 struct rdma_route
*route
)
2086 struct sockaddr_in
*src4
, *dst4
;
2087 struct cma_hdr
*cma_hdr
;
2088 struct sdp_hh
*sdp_hdr
;
2090 src4
= (struct sockaddr_in
*) &route
->addr
.src_addr
;
2091 dst4
= (struct sockaddr_in
*) &route
->addr
.dst_addr
;
2096 if (sdp_get_majv(sdp_hdr
->sdp_version
) != SDP_MAJ_VERSION
)
2098 sdp_set_ip_ver(sdp_hdr
, 4);
2099 sdp_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2100 sdp_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2101 sdp_hdr
->port
= src4
->sin_port
;
2105 cma_hdr
->cma_version
= CMA_VERSION
;
2106 cma_set_ip_ver(cma_hdr
, 4);
2107 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2108 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2109 cma_hdr
->port
= src4
->sin_port
;
2115 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
2116 struct ib_cm_event
*ib_event
)
2118 struct rdma_id_private
*id_priv
= cm_id
->context
;
2119 struct rdma_cm_event event
;
2120 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
2123 if (cma_disable_remove(id_priv
, CMA_CONNECT
))
2126 memset(&event
, 0, sizeof event
);
2127 switch (ib_event
->event
) {
2128 case IB_CM_SIDR_REQ_ERROR
:
2129 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2130 event
.status
= -ETIMEDOUT
;
2132 case IB_CM_SIDR_REP_RECEIVED
:
2133 event
.param
.ud
.private_data
= ib_event
->private_data
;
2134 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
2135 if (rep
->status
!= IB_SIDR_SUCCESS
) {
2136 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2137 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
2140 if (id_priv
->qkey
!= rep
->qkey
) {
2141 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2142 event
.status
= -EINVAL
;
2145 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
2146 id_priv
->id
.route
.path_rec
,
2147 &event
.param
.ud
.ah_attr
);
2148 event
.param
.ud
.qp_num
= rep
->qpn
;
2149 event
.param
.ud
.qkey
= rep
->qkey
;
2150 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2154 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d",
2159 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2161 /* Destroy the CM ID by returning a non-zero value. */
2162 id_priv
->cm_id
.ib
= NULL
;
2163 cma_exch(id_priv
, CMA_DESTROYING
);
2164 cma_enable_remove(id_priv
);
2165 rdma_destroy_id(&id_priv
->id
);
2169 cma_enable_remove(id_priv
);
2173 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
2174 struct rdma_conn_param
*conn_param
)
2176 struct ib_cm_sidr_req_param req
;
2177 struct rdma_route
*route
;
2180 req
.private_data_len
= sizeof(struct cma_hdr
) +
2181 conn_param
->private_data_len
;
2182 req
.private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2183 if (!req
.private_data
)
2186 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2187 memcpy((void *) req
.private_data
+ sizeof(struct cma_hdr
),
2188 conn_param
->private_data
, conn_param
->private_data_len
);
2190 route
= &id_priv
->id
.route
;
2191 ret
= cma_format_hdr((void *) req
.private_data
, id_priv
->id
.ps
, route
);
2195 id_priv
->cm_id
.ib
= ib_create_cm_id(id_priv
->id
.device
,
2196 cma_sidr_rep_handler
, id_priv
);
2197 if (IS_ERR(id_priv
->cm_id
.ib
)) {
2198 ret
= PTR_ERR(id_priv
->cm_id
.ib
);
2202 req
.path
= route
->path_rec
;
2203 req
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
2204 &route
->addr
.dst_addr
);
2205 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
2206 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2208 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
2210 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
2211 id_priv
->cm_id
.ib
= NULL
;
2214 kfree(req
.private_data
);
2218 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
2219 struct rdma_conn_param
*conn_param
)
2221 struct ib_cm_req_param req
;
2222 struct rdma_route
*route
;
2226 memset(&req
, 0, sizeof req
);
2227 offset
= cma_user_data_offset(id_priv
->id
.ps
);
2228 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
2229 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2233 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2234 memcpy(private_data
+ offset
, conn_param
->private_data
,
2235 conn_param
->private_data_len
);
2237 id_priv
->cm_id
.ib
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
,
2239 if (IS_ERR(id_priv
->cm_id
.ib
)) {
2240 ret
= PTR_ERR(id_priv
->cm_id
.ib
);
2244 route
= &id_priv
->id
.route
;
2245 ret
= cma_format_hdr(private_data
, id_priv
->id
.ps
, route
);
2248 req
.private_data
= private_data
;
2250 req
.primary_path
= &route
->path_rec
[0];
2251 if (route
->num_paths
== 2)
2252 req
.alternate_path
= &route
->path_rec
[1];
2254 req
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
2255 &route
->addr
.dst_addr
);
2256 req
.qp_num
= id_priv
->qp_num
;
2257 req
.qp_type
= IB_QPT_RC
;
2258 req
.starting_psn
= id_priv
->seq_num
;
2259 req
.responder_resources
= conn_param
->responder_resources
;
2260 req
.initiator_depth
= conn_param
->initiator_depth
;
2261 req
.flow_control
= conn_param
->flow_control
;
2262 req
.retry_count
= conn_param
->retry_count
;
2263 req
.rnr_retry_count
= conn_param
->rnr_retry_count
;
2264 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2265 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2266 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2267 req
.srq
= id_priv
->srq
? 1 : 0;
2269 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
2271 if (ret
&& !IS_ERR(id_priv
->cm_id
.ib
)) {
2272 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
2273 id_priv
->cm_id
.ib
= NULL
;
2276 kfree(private_data
);
2280 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
2281 struct rdma_conn_param
*conn_param
)
2283 struct iw_cm_id
*cm_id
;
2284 struct sockaddr_in
* sin
;
2286 struct iw_cm_conn_param iw_param
;
2288 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
2289 if (IS_ERR(cm_id
)) {
2290 ret
= PTR_ERR(cm_id
);
2294 id_priv
->cm_id
.iw
= cm_id
;
2296 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
2297 cm_id
->local_addr
= *sin
;
2299 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.dst_addr
;
2300 cm_id
->remote_addr
= *sin
;
2302 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2306 iw_param
.ord
= conn_param
->initiator_depth
;
2307 iw_param
.ird
= conn_param
->responder_resources
;
2308 iw_param
.private_data
= conn_param
->private_data
;
2309 iw_param
.private_data_len
= conn_param
->private_data_len
;
2311 iw_param
.qpn
= id_priv
->qp_num
;
2313 iw_param
.qpn
= conn_param
->qp_num
;
2314 ret
= iw_cm_connect(cm_id
, &iw_param
);
2316 if (ret
&& !IS_ERR(cm_id
)) {
2317 iw_destroy_cm_id(cm_id
);
2318 id_priv
->cm_id
.iw
= NULL
;
2323 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2325 struct rdma_id_private
*id_priv
;
2328 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2329 if (!cma_comp_exch(id_priv
, CMA_ROUTE_RESOLVED
, CMA_CONNECT
))
2333 id_priv
->qp_num
= conn_param
->qp_num
;
2334 id_priv
->srq
= conn_param
->srq
;
2337 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2338 case RDMA_TRANSPORT_IB
:
2339 if (cma_is_ud_ps(id
->ps
))
2340 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
2342 ret
= cma_connect_ib(id_priv
, conn_param
);
2344 case RDMA_TRANSPORT_IWARP
:
2345 ret
= cma_connect_iw(id_priv
, conn_param
);
2356 cma_comp_exch(id_priv
, CMA_CONNECT
, CMA_ROUTE_RESOLVED
);
2359 EXPORT_SYMBOL(rdma_connect
);
2361 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
2362 struct rdma_conn_param
*conn_param
)
2364 struct ib_cm_rep_param rep
;
2367 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2371 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
2375 memset(&rep
, 0, sizeof rep
);
2376 rep
.qp_num
= id_priv
->qp_num
;
2377 rep
.starting_psn
= id_priv
->seq_num
;
2378 rep
.private_data
= conn_param
->private_data
;
2379 rep
.private_data_len
= conn_param
->private_data_len
;
2380 rep
.responder_resources
= conn_param
->responder_resources
;
2381 rep
.initiator_depth
= conn_param
->initiator_depth
;
2382 rep
.failover_accepted
= 0;
2383 rep
.flow_control
= conn_param
->flow_control
;
2384 rep
.rnr_retry_count
= conn_param
->rnr_retry_count
;
2385 rep
.srq
= id_priv
->srq
? 1 : 0;
2387 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
2392 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
2393 struct rdma_conn_param
*conn_param
)
2395 struct iw_cm_conn_param iw_param
;
2398 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2402 iw_param
.ord
= conn_param
->initiator_depth
;
2403 iw_param
.ird
= conn_param
->responder_resources
;
2404 iw_param
.private_data
= conn_param
->private_data
;
2405 iw_param
.private_data_len
= conn_param
->private_data_len
;
2406 if (id_priv
->id
.qp
) {
2407 iw_param
.qpn
= id_priv
->qp_num
;
2409 iw_param
.qpn
= conn_param
->qp_num
;
2411 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
2414 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
2415 enum ib_cm_sidr_status status
,
2416 const void *private_data
, int private_data_len
)
2418 struct ib_cm_sidr_rep_param rep
;
2420 memset(&rep
, 0, sizeof rep
);
2421 rep
.status
= status
;
2422 if (status
== IB_SIDR_SUCCESS
) {
2423 rep
.qp_num
= id_priv
->qp_num
;
2424 rep
.qkey
= id_priv
->qkey
;
2426 rep
.private_data
= private_data
;
2427 rep
.private_data_len
= private_data_len
;
2429 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
2432 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2434 struct rdma_id_private
*id_priv
;
2437 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2438 if (!cma_comp(id_priv
, CMA_CONNECT
))
2441 if (!id
->qp
&& conn_param
) {
2442 id_priv
->qp_num
= conn_param
->qp_num
;
2443 id_priv
->srq
= conn_param
->srq
;
2446 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2447 case RDMA_TRANSPORT_IB
:
2448 if (cma_is_ud_ps(id
->ps
))
2449 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
2450 conn_param
->private_data
,
2451 conn_param
->private_data_len
);
2452 else if (conn_param
)
2453 ret
= cma_accept_ib(id_priv
, conn_param
);
2455 ret
= cma_rep_recv(id_priv
);
2457 case RDMA_TRANSPORT_IWARP
:
2458 ret
= cma_accept_iw(id_priv
, conn_param
);
2470 cma_modify_qp_err(id_priv
);
2471 rdma_reject(id
, NULL
, 0);
2474 EXPORT_SYMBOL(rdma_accept
);
2476 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
2478 struct rdma_id_private
*id_priv
;
2481 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2482 if (!cma_has_cm_dev(id_priv
))
2485 switch (id
->device
->node_type
) {
2486 case RDMA_NODE_IB_CA
:
2487 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
2495 EXPORT_SYMBOL(rdma_notify
);
2497 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
2498 u8 private_data_len
)
2500 struct rdma_id_private
*id_priv
;
2503 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2504 if (!cma_has_cm_dev(id_priv
))
2507 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2508 case RDMA_TRANSPORT_IB
:
2509 if (cma_is_ud_ps(id
->ps
))
2510 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
,
2511 private_data
, private_data_len
);
2513 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
2514 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
2515 0, private_data
, private_data_len
);
2517 case RDMA_TRANSPORT_IWARP
:
2518 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
2519 private_data
, private_data_len
);
2527 EXPORT_SYMBOL(rdma_reject
);
2529 int rdma_disconnect(struct rdma_cm_id
*id
)
2531 struct rdma_id_private
*id_priv
;
2534 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2535 if (!cma_has_cm_dev(id_priv
))
2538 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2539 case RDMA_TRANSPORT_IB
:
2540 ret
= cma_modify_qp_err(id_priv
);
2543 /* Initiate or respond to a disconnect. */
2544 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
2545 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
2547 case RDMA_TRANSPORT_IWARP
:
2548 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
2557 EXPORT_SYMBOL(rdma_disconnect
);
2559 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
2561 struct rdma_id_private
*id_priv
;
2562 struct cma_multicast
*mc
= multicast
->context
;
2563 struct rdma_cm_event event
;
2566 id_priv
= mc
->id_priv
;
2567 if (cma_disable_remove(id_priv
, CMA_ADDR_BOUND
) &&
2568 cma_disable_remove(id_priv
, CMA_ADDR_RESOLVED
))
2571 mutex_lock(&id_priv
->qp_mutex
);
2572 if (!status
&& id_priv
->id
.qp
)
2573 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
2574 multicast
->rec
.mlid
);
2575 mutex_unlock(&id_priv
->qp_mutex
);
2577 memset(&event
, 0, sizeof event
);
2578 event
.status
= status
;
2579 event
.param
.ud
.private_data
= mc
->context
;
2581 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
2582 ib_init_ah_from_mcmember(id_priv
->id
.device
,
2583 id_priv
->id
.port_num
, &multicast
->rec
,
2584 &event
.param
.ud
.ah_attr
);
2585 event
.param
.ud
.qp_num
= 0xFFFFFF;
2586 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
2588 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
2590 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2592 cma_exch(id_priv
, CMA_DESTROYING
);
2593 cma_enable_remove(id_priv
);
2594 rdma_destroy_id(&id_priv
->id
);
2598 cma_enable_remove(id_priv
);
2602 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
2603 struct sockaddr
*addr
, union ib_gid
*mgid
)
2605 unsigned char mc_map
[MAX_ADDR_LEN
];
2606 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2607 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
2608 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
2610 if (cma_any_addr(addr
)) {
2611 memset(mgid
, 0, sizeof *mgid
);
2612 } else if ((addr
->sa_family
== AF_INET6
) &&
2613 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFF10A01B) ==
2615 /* IPv6 address is an SA assigned MGID. */
2616 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
2618 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
2619 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2620 mc_map
[7] = 0x01; /* Use RDMA CM signature */
2621 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
2625 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
2626 struct cma_multicast
*mc
)
2628 struct ib_sa_mcmember_rec rec
;
2629 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2630 ib_sa_comp_mask comp_mask
;
2633 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
2634 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
2639 cma_set_mgid(id_priv
, &mc
->addr
, &rec
.mgid
);
2640 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2641 rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
2642 ib_addr_get_sgid(dev_addr
, &rec
.port_gid
);
2643 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2646 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
2647 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
2648 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
2649 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
2650 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
2652 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
2653 id_priv
->id
.port_num
, &rec
,
2654 comp_mask
, GFP_KERNEL
,
2655 cma_ib_mc_handler
, mc
);
2656 if (IS_ERR(mc
->multicast
.ib
))
2657 return PTR_ERR(mc
->multicast
.ib
);
2662 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
2665 struct rdma_id_private
*id_priv
;
2666 struct cma_multicast
*mc
;
2669 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2670 if (!cma_comp(id_priv
, CMA_ADDR_BOUND
) &&
2671 !cma_comp(id_priv
, CMA_ADDR_RESOLVED
))
2674 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
2678 memcpy(&mc
->addr
, addr
, ip_addr_size(addr
));
2679 mc
->context
= context
;
2680 mc
->id_priv
= id_priv
;
2682 spin_lock(&id_priv
->lock
);
2683 list_add(&mc
->list
, &id_priv
->mc_list
);
2684 spin_unlock(&id_priv
->lock
);
2686 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2687 case RDMA_TRANSPORT_IB
:
2688 ret
= cma_join_ib_multicast(id_priv
, mc
);
2696 spin_lock_irq(&id_priv
->lock
);
2697 list_del(&mc
->list
);
2698 spin_unlock_irq(&id_priv
->lock
);
2703 EXPORT_SYMBOL(rdma_join_multicast
);
2705 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2707 struct rdma_id_private
*id_priv
;
2708 struct cma_multicast
*mc
;
2710 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2711 spin_lock_irq(&id_priv
->lock
);
2712 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
2713 if (!memcmp(&mc
->addr
, addr
, ip_addr_size(addr
))) {
2714 list_del(&mc
->list
);
2715 spin_unlock_irq(&id_priv
->lock
);
2718 ib_detach_mcast(id
->qp
,
2719 &mc
->multicast
.ib
->rec
.mgid
,
2720 mc
->multicast
.ib
->rec
.mlid
);
2721 ib_sa_free_multicast(mc
->multicast
.ib
);
2726 spin_unlock_irq(&id_priv
->lock
);
2728 EXPORT_SYMBOL(rdma_leave_multicast
);
2730 static void cma_add_one(struct ib_device
*device
)
2732 struct cma_device
*cma_dev
;
2733 struct rdma_id_private
*id_priv
;
2735 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
2739 cma_dev
->device
= device
;
2741 init_completion(&cma_dev
->comp
);
2742 atomic_set(&cma_dev
->refcount
, 1);
2743 INIT_LIST_HEAD(&cma_dev
->id_list
);
2744 ib_set_client_data(device
, &cma_client
, cma_dev
);
2747 list_add_tail(&cma_dev
->list
, &dev_list
);
2748 list_for_each_entry(id_priv
, &listen_any_list
, list
)
2749 cma_listen_on_dev(id_priv
, cma_dev
);
2750 mutex_unlock(&lock
);
2753 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
2755 struct rdma_cm_event event
;
2756 enum cma_state state
;
2758 /* Record that we want to remove the device */
2759 state
= cma_exch(id_priv
, CMA_DEVICE_REMOVAL
);
2760 if (state
== CMA_DESTROYING
)
2763 cma_cancel_operation(id_priv
, state
);
2764 wait_event(id_priv
->wait_remove
, !atomic_read(&id_priv
->dev_remove
));
2766 /* Check for destruction from another callback. */
2767 if (!cma_comp(id_priv
, CMA_DEVICE_REMOVAL
))
2770 memset(&event
, 0, sizeof event
);
2771 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
2772 return id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2775 static void cma_process_remove(struct cma_device
*cma_dev
)
2777 struct rdma_id_private
*id_priv
;
2781 while (!list_empty(&cma_dev
->id_list
)) {
2782 id_priv
= list_entry(cma_dev
->id_list
.next
,
2783 struct rdma_id_private
, list
);
2785 list_del(&id_priv
->listen_list
);
2786 list_del_init(&id_priv
->list
);
2787 atomic_inc(&id_priv
->refcount
);
2788 mutex_unlock(&lock
);
2790 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
2791 cma_deref_id(id_priv
);
2793 rdma_destroy_id(&id_priv
->id
);
2797 mutex_unlock(&lock
);
2799 cma_deref_dev(cma_dev
);
2800 wait_for_completion(&cma_dev
->comp
);
2803 static void cma_remove_one(struct ib_device
*device
)
2805 struct cma_device
*cma_dev
;
2807 cma_dev
= ib_get_client_data(device
, &cma_client
);
2812 list_del(&cma_dev
->list
);
2813 mutex_unlock(&lock
);
2815 cma_process_remove(cma_dev
);
2819 static int cma_init(void)
2821 int ret
, low
, high
, remaining
;
2823 get_random_bytes(&next_port
, sizeof next_port
);
2824 inet_get_local_port_range(&low
, &high
);
2825 remaining
= (high
- low
) + 1;
2826 next_port
= ((unsigned int) next_port
% remaining
) + low
;
2828 cma_wq
= create_singlethread_workqueue("rdma_cm");
2832 ib_sa_register_client(&sa_client
);
2833 rdma_addr_register_client(&addr_client
);
2835 ret
= ib_register_client(&cma_client
);
2841 rdma_addr_unregister_client(&addr_client
);
2842 ib_sa_unregister_client(&sa_client
);
2843 destroy_workqueue(cma_wq
);
2847 static void cma_cleanup(void)
2849 ib_unregister_client(&cma_client
);
2850 rdma_addr_unregister_client(&addr_client
);
2851 ib_sa_unregister_client(&sa_client
);
2852 destroy_workqueue(cma_wq
);
2853 idr_destroy(&sdp_ps
);
2854 idr_destroy(&tcp_ps
);
2855 idr_destroy(&udp_ps
);
2856 idr_destroy(&ipoib_ps
);
2859 module_init(cma_init
);
2860 module_exit(cma_cleanup
);