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/igmp.h>
42 #include <linux/idr.h>
43 #include <linux/inetdevice.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <net/route.h>
48 #include <net/net_namespace.h>
49 #include <net/netns/generic.h>
52 #include <net/ip_fib.h>
53 #include <net/ip6_route.h>
55 #include <rdma/rdma_cm.h>
56 #include <rdma/rdma_cm_ib.h>
57 #include <rdma/rdma_netlink.h>
59 #include <rdma/ib_cache.h>
60 #include <rdma/ib_cm.h>
61 #include <rdma/ib_sa.h>
62 #include <rdma/iw_cm.h>
64 #include "core_priv.h"
66 MODULE_AUTHOR("Sean Hefty");
67 MODULE_DESCRIPTION("Generic RDMA CM Agent");
68 MODULE_LICENSE("Dual BSD/GPL");
70 #define CMA_CM_RESPONSE_TIMEOUT 20
71 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
72 #define CMA_MAX_CM_RETRIES 15
73 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
74 #define CMA_IBOE_PACKET_LIFETIME 18
76 static const char * const cma_events
[] = {
77 [RDMA_CM_EVENT_ADDR_RESOLVED
] = "address resolved",
78 [RDMA_CM_EVENT_ADDR_ERROR
] = "address error",
79 [RDMA_CM_EVENT_ROUTE_RESOLVED
] = "route resolved ",
80 [RDMA_CM_EVENT_ROUTE_ERROR
] = "route error",
81 [RDMA_CM_EVENT_CONNECT_REQUEST
] = "connect request",
82 [RDMA_CM_EVENT_CONNECT_RESPONSE
] = "connect response",
83 [RDMA_CM_EVENT_CONNECT_ERROR
] = "connect error",
84 [RDMA_CM_EVENT_UNREACHABLE
] = "unreachable",
85 [RDMA_CM_EVENT_REJECTED
] = "rejected",
86 [RDMA_CM_EVENT_ESTABLISHED
] = "established",
87 [RDMA_CM_EVENT_DISCONNECTED
] = "disconnected",
88 [RDMA_CM_EVENT_DEVICE_REMOVAL
] = "device removal",
89 [RDMA_CM_EVENT_MULTICAST_JOIN
] = "multicast join",
90 [RDMA_CM_EVENT_MULTICAST_ERROR
] = "multicast error",
91 [RDMA_CM_EVENT_ADDR_CHANGE
] = "address change",
92 [RDMA_CM_EVENT_TIMEWAIT_EXIT
] = "timewait exit",
95 const char *__attribute_const__
rdma_event_msg(enum rdma_cm_event_type event
)
99 return (index
< ARRAY_SIZE(cma_events
) && cma_events
[index
]) ?
100 cma_events
[index
] : "unrecognized event";
102 EXPORT_SYMBOL(rdma_event_msg
);
104 const char *__attribute_const__
rdma_reject_msg(struct rdma_cm_id
*id
,
107 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
108 return ibcm_reject_msg(reason
);
110 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
111 return iwcm_reject_msg(reason
);
114 return "unrecognized transport";
116 EXPORT_SYMBOL(rdma_reject_msg
);
118 bool rdma_is_consumer_reject(struct rdma_cm_id
*id
, int reason
)
120 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
121 return reason
== IB_CM_REJ_CONSUMER_DEFINED
;
123 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
124 return reason
== -ECONNREFUSED
;
129 EXPORT_SYMBOL(rdma_is_consumer_reject
);
131 const void *rdma_consumer_reject_data(struct rdma_cm_id
*id
,
132 struct rdma_cm_event
*ev
, u8
*data_len
)
136 if (rdma_is_consumer_reject(id
, ev
->status
)) {
137 *data_len
= ev
->param
.conn
.private_data_len
;
138 p
= ev
->param
.conn
.private_data
;
145 EXPORT_SYMBOL(rdma_consumer_reject_data
);
147 static void cma_add_one(struct ib_device
*device
);
148 static void cma_remove_one(struct ib_device
*device
, void *client_data
);
150 static struct ib_client cma_client
= {
153 .remove
= cma_remove_one
156 static struct ib_sa_client sa_client
;
157 static struct rdma_addr_client addr_client
;
158 static LIST_HEAD(dev_list
);
159 static LIST_HEAD(listen_any_list
);
160 static DEFINE_MUTEX(lock
);
161 static struct workqueue_struct
*cma_wq
;
162 static unsigned int cma_pernet_id
;
171 static struct cma_pernet
*cma_pernet(struct net
*net
)
173 return net_generic(net
, cma_pernet_id
);
176 static struct idr
*cma_pernet_idr(struct net
*net
, enum rdma_port_space ps
)
178 struct cma_pernet
*pernet
= cma_pernet(net
);
182 return &pernet
->tcp_ps
;
184 return &pernet
->udp_ps
;
186 return &pernet
->ipoib_ps
;
188 return &pernet
->ib_ps
;
195 struct list_head list
;
196 struct ib_device
*device
;
197 struct completion comp
;
199 struct list_head id_list
;
200 enum ib_gid_type
*default_gid_type
;
203 struct rdma_bind_list
{
204 enum rdma_port_space ps
;
205 struct hlist_head owners
;
209 struct class_port_info_context
{
210 struct ib_class_port_info
*class_port_info
;
211 struct ib_device
*device
;
212 struct completion done
;
213 struct ib_sa_query
*sa_query
;
217 static int cma_ps_alloc(struct net
*net
, enum rdma_port_space ps
,
218 struct rdma_bind_list
*bind_list
, int snum
)
220 struct idr
*idr
= cma_pernet_idr(net
, ps
);
222 return idr_alloc(idr
, bind_list
, snum
, snum
+ 1, GFP_KERNEL
);
225 static struct rdma_bind_list
*cma_ps_find(struct net
*net
,
226 enum rdma_port_space ps
, int snum
)
228 struct idr
*idr
= cma_pernet_idr(net
, ps
);
230 return idr_find(idr
, snum
);
233 static void cma_ps_remove(struct net
*net
, enum rdma_port_space ps
, int snum
)
235 struct idr
*idr
= cma_pernet_idr(net
, ps
);
237 idr_remove(idr
, snum
);
244 void cma_ref_dev(struct cma_device
*cma_dev
)
246 atomic_inc(&cma_dev
->refcount
);
249 struct cma_device
*cma_enum_devices_by_ibdev(cma_device_filter filter
,
252 struct cma_device
*cma_dev
;
253 struct cma_device
*found_cma_dev
= NULL
;
257 list_for_each_entry(cma_dev
, &dev_list
, list
)
258 if (filter(cma_dev
->device
, cookie
)) {
259 found_cma_dev
= cma_dev
;
264 cma_ref_dev(found_cma_dev
);
266 return found_cma_dev
;
269 int cma_get_default_gid_type(struct cma_device
*cma_dev
,
272 if (port
< rdma_start_port(cma_dev
->device
) ||
273 port
> rdma_end_port(cma_dev
->device
))
276 return cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)];
279 int cma_set_default_gid_type(struct cma_device
*cma_dev
,
281 enum ib_gid_type default_gid_type
)
283 unsigned long supported_gids
;
285 if (port
< rdma_start_port(cma_dev
->device
) ||
286 port
> rdma_end_port(cma_dev
->device
))
289 supported_gids
= roce_gid_type_mask_support(cma_dev
->device
, port
);
291 if (!(supported_gids
& 1 << default_gid_type
))
294 cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)] =
300 struct ib_device
*cma_get_ib_dev(struct cma_device
*cma_dev
)
302 return cma_dev
->device
;
306 * Device removal can occur at anytime, so we need extra handling to
307 * serialize notifying the user of device removal with other callbacks.
308 * We do this by disabling removal notification while a callback is in process,
309 * and reporting it after the callback completes.
311 struct rdma_id_private
{
312 struct rdma_cm_id id
;
314 struct rdma_bind_list
*bind_list
;
315 struct hlist_node node
;
316 struct list_head list
; /* listen_any_list or cma_device.list */
317 struct list_head listen_list
; /* per device listens */
318 struct cma_device
*cma_dev
;
319 struct list_head mc_list
;
322 enum rdma_cm_state state
;
324 struct mutex qp_mutex
;
326 struct completion comp
;
328 struct mutex handler_mutex
;
332 struct ib_sa_query
*query
;
348 enum ib_gid_type gid_type
;
351 struct cma_multicast
{
352 struct rdma_id_private
*id_priv
;
354 struct ib_sa_multicast
*ib
;
356 struct list_head list
;
358 struct sockaddr_storage addr
;
365 struct work_struct work
;
366 struct rdma_id_private
*id
;
367 enum rdma_cm_state old_state
;
368 enum rdma_cm_state new_state
;
369 struct rdma_cm_event event
;
372 struct cma_ndev_work
{
373 struct work_struct work
;
374 struct rdma_id_private
*id
;
375 struct rdma_cm_event event
;
378 struct iboe_mcast_work
{
379 struct work_struct work
;
380 struct rdma_id_private
*id
;
381 struct cma_multicast
*mc
;
394 u8 ip_version
; /* IP version: 7:4 */
396 union cma_ip_addr src_addr
;
397 union cma_ip_addr dst_addr
;
400 #define CMA_VERSION 0x00
402 struct cma_req_info
{
403 struct ib_device
*device
;
405 union ib_gid local_gid
;
411 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
416 spin_lock_irqsave(&id_priv
->lock
, flags
);
417 ret
= (id_priv
->state
== comp
);
418 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
422 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
423 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
428 spin_lock_irqsave(&id_priv
->lock
, flags
);
429 if ((ret
= (id_priv
->state
== comp
)))
430 id_priv
->state
= exch
;
431 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
435 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
436 enum rdma_cm_state exch
)
439 enum rdma_cm_state old
;
441 spin_lock_irqsave(&id_priv
->lock
, flags
);
442 old
= id_priv
->state
;
443 id_priv
->state
= exch
;
444 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
448 static inline u8
cma_get_ip_ver(const struct cma_hdr
*hdr
)
450 return hdr
->ip_version
>> 4;
453 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
455 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
458 static int cma_igmp_send(struct net_device
*ndev
, union ib_gid
*mgid
, bool join
)
460 struct in_device
*in_dev
= NULL
;
464 in_dev
= __in_dev_get_rtnl(ndev
);
467 ip_mc_inc_group(in_dev
,
468 *(__be32
*)(mgid
->raw
+ 12));
470 ip_mc_dec_group(in_dev
,
471 *(__be32
*)(mgid
->raw
+ 12));
475 return (in_dev
) ? 0 : -ENODEV
;
478 static void _cma_attach_to_dev(struct rdma_id_private
*id_priv
,
479 struct cma_device
*cma_dev
)
481 cma_ref_dev(cma_dev
);
482 id_priv
->cma_dev
= cma_dev
;
483 id_priv
->gid_type
= 0;
484 id_priv
->id
.device
= cma_dev
->device
;
485 id_priv
->id
.route
.addr
.dev_addr
.transport
=
486 rdma_node_get_transport(cma_dev
->device
->node_type
);
487 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
490 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
491 struct cma_device
*cma_dev
)
493 _cma_attach_to_dev(id_priv
, cma_dev
);
495 cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
496 rdma_start_port(cma_dev
->device
)];
499 void cma_deref_dev(struct cma_device
*cma_dev
)
501 if (atomic_dec_and_test(&cma_dev
->refcount
))
502 complete(&cma_dev
->comp
);
505 static inline void release_mc(struct kref
*kref
)
507 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
509 kfree(mc
->multicast
.ib
);
513 static void cma_release_dev(struct rdma_id_private
*id_priv
)
516 list_del(&id_priv
->list
);
517 cma_deref_dev(id_priv
->cma_dev
);
518 id_priv
->cma_dev
= NULL
;
522 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
524 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
527 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
529 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
532 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
534 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
537 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
539 struct ib_sa_mcmember_rec rec
;
543 if (qkey
&& id_priv
->qkey
!= qkey
)
549 id_priv
->qkey
= qkey
;
553 switch (id_priv
->id
.ps
) {
556 id_priv
->qkey
= RDMA_UDP_QKEY
;
559 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
560 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
561 id_priv
->id
.port_num
, &rec
.mgid
,
564 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
572 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
574 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
575 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
576 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
579 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
583 if (addr
->sa_family
!= AF_IB
) {
584 ret
= rdma_translate_ip(addr
, dev_addr
, NULL
);
586 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
593 static inline int cma_validate_port(struct ib_device
*device
, u8 port
,
594 enum ib_gid_type gid_type
,
595 union ib_gid
*gid
, int dev_type
,
599 struct net_device
*ndev
= NULL
;
601 if ((dev_type
== ARPHRD_INFINIBAND
) && !rdma_protocol_ib(device
, port
))
604 if ((dev_type
!= ARPHRD_INFINIBAND
) && rdma_protocol_ib(device
, port
))
607 if (dev_type
== ARPHRD_ETHER
&& rdma_protocol_roce(device
, port
)) {
608 ndev
= dev_get_by_index(&init_net
, bound_if_index
);
609 if (ndev
&& ndev
->flags
& IFF_LOOPBACK
) {
610 pr_info("detected loopback device\n");
613 if (!device
->get_netdev
)
616 ndev
= device
->get_netdev(device
, port
);
621 gid_type
= IB_GID_TYPE_IB
;
624 ret
= ib_find_cached_gid_by_port(device
, gid
, gid_type
, port
,
633 static int cma_acquire_dev(struct rdma_id_private
*id_priv
,
634 struct rdma_id_private
*listen_id_priv
)
636 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
637 struct cma_device
*cma_dev
;
638 union ib_gid gid
, iboe_gid
, *gidp
;
642 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
643 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
647 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
650 memcpy(&gid
, dev_addr
->src_dev_addr
+
651 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
653 if (listen_id_priv
) {
654 cma_dev
= listen_id_priv
->cma_dev
;
655 port
= listen_id_priv
->id
.port_num
;
656 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
659 ret
= cma_validate_port(cma_dev
->device
, port
,
660 rdma_protocol_ib(cma_dev
->device
, port
) ?
662 listen_id_priv
->gid_type
, gidp
,
664 dev_addr
->bound_dev_if
);
666 id_priv
->id
.port_num
= port
;
671 list_for_each_entry(cma_dev
, &dev_list
, list
) {
672 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
673 if (listen_id_priv
&&
674 listen_id_priv
->cma_dev
== cma_dev
&&
675 listen_id_priv
->id
.port_num
== port
)
678 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
681 ret
= cma_validate_port(cma_dev
->device
, port
,
682 rdma_protocol_ib(cma_dev
->device
, port
) ?
684 cma_dev
->default_gid_type
[port
- 1],
685 gidp
, dev_addr
->dev_type
,
686 dev_addr
->bound_dev_if
);
688 id_priv
->id
.port_num
= port
;
696 cma_attach_to_dev(id_priv
, cma_dev
);
703 * Select the source IB device and address to reach the destination IB address.
705 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
707 struct cma_device
*cma_dev
, *cur_dev
;
708 struct sockaddr_ib
*addr
;
709 union ib_gid gid
, sgid
, *dgid
;
715 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
716 dgid
= (union ib_gid
*) &addr
->sib_addr
;
717 pkey
= ntohs(addr
->sib_pkey
);
719 list_for_each_entry(cur_dev
, &dev_list
, list
) {
720 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
721 if (!rdma_cap_af_ib(cur_dev
->device
, p
))
724 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
727 for (i
= 0; !ib_get_cached_gid(cur_dev
->device
, p
, i
,
730 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
733 id_priv
->id
.port_num
= p
;
737 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
738 dgid
->global
.subnet_prefix
)) {
741 id_priv
->id
.port_num
= p
;
751 cma_attach_to_dev(id_priv
, cma_dev
);
752 addr
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
753 memcpy(&addr
->sib_addr
, &sgid
, sizeof sgid
);
754 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
758 static void cma_deref_id(struct rdma_id_private
*id_priv
)
760 if (atomic_dec_and_test(&id_priv
->refcount
))
761 complete(&id_priv
->comp
);
764 struct rdma_cm_id
*rdma_create_id(struct net
*net
,
765 rdma_cm_event_handler event_handler
,
766 void *context
, enum rdma_port_space ps
,
767 enum ib_qp_type qp_type
)
769 struct rdma_id_private
*id_priv
;
771 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
773 return ERR_PTR(-ENOMEM
);
775 id_priv
->owner
= task_pid_nr(current
);
776 id_priv
->state
= RDMA_CM_IDLE
;
777 id_priv
->id
.context
= context
;
778 id_priv
->id
.event_handler
= event_handler
;
780 id_priv
->id
.qp_type
= qp_type
;
781 spin_lock_init(&id_priv
->lock
);
782 mutex_init(&id_priv
->qp_mutex
);
783 init_completion(&id_priv
->comp
);
784 atomic_set(&id_priv
->refcount
, 1);
785 mutex_init(&id_priv
->handler_mutex
);
786 INIT_LIST_HEAD(&id_priv
->listen_list
);
787 INIT_LIST_HEAD(&id_priv
->mc_list
);
788 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
789 id_priv
->id
.route
.addr
.dev_addr
.net
= get_net(net
);
793 EXPORT_SYMBOL(rdma_create_id
);
795 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
797 struct ib_qp_attr qp_attr
;
798 int qp_attr_mask
, ret
;
800 qp_attr
.qp_state
= IB_QPS_INIT
;
801 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
805 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
809 qp_attr
.qp_state
= IB_QPS_RTR
;
810 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
814 qp_attr
.qp_state
= IB_QPS_RTS
;
816 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
821 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
823 struct ib_qp_attr qp_attr
;
824 int qp_attr_mask
, ret
;
826 qp_attr
.qp_state
= IB_QPS_INIT
;
827 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
831 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
834 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
835 struct ib_qp_init_attr
*qp_init_attr
)
837 struct rdma_id_private
*id_priv
;
841 id_priv
= container_of(id
, struct rdma_id_private
, id
);
842 if (id
->device
!= pd
->device
)
845 qp_init_attr
->port_num
= id
->port_num
;
846 qp
= ib_create_qp(pd
, qp_init_attr
);
850 if (id
->qp_type
== IB_QPT_UD
)
851 ret
= cma_init_ud_qp(id_priv
, qp
);
853 ret
= cma_init_conn_qp(id_priv
, qp
);
858 id_priv
->qp_num
= qp
->qp_num
;
859 id_priv
->srq
= (qp
->srq
!= NULL
);
865 EXPORT_SYMBOL(rdma_create_qp
);
867 void rdma_destroy_qp(struct rdma_cm_id
*id
)
869 struct rdma_id_private
*id_priv
;
871 id_priv
= container_of(id
, struct rdma_id_private
, id
);
872 mutex_lock(&id_priv
->qp_mutex
);
873 ib_destroy_qp(id_priv
->id
.qp
);
874 id_priv
->id
.qp
= NULL
;
875 mutex_unlock(&id_priv
->qp_mutex
);
877 EXPORT_SYMBOL(rdma_destroy_qp
);
879 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
880 struct rdma_conn_param
*conn_param
)
882 struct ib_qp_attr qp_attr
;
883 int qp_attr_mask
, ret
;
886 mutex_lock(&id_priv
->qp_mutex
);
887 if (!id_priv
->id
.qp
) {
892 /* Need to update QP attributes from default values. */
893 qp_attr
.qp_state
= IB_QPS_INIT
;
894 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
898 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
902 qp_attr
.qp_state
= IB_QPS_RTR
;
903 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
907 ret
= ib_query_gid(id_priv
->id
.device
, id_priv
->id
.port_num
,
908 qp_attr
.ah_attr
.grh
.sgid_index
, &sgid
, NULL
);
912 BUG_ON(id_priv
->cma_dev
->device
!= id_priv
->id
.device
);
915 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
916 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
918 mutex_unlock(&id_priv
->qp_mutex
);
922 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
923 struct rdma_conn_param
*conn_param
)
925 struct ib_qp_attr qp_attr
;
926 int qp_attr_mask
, ret
;
928 mutex_lock(&id_priv
->qp_mutex
);
929 if (!id_priv
->id
.qp
) {
934 qp_attr
.qp_state
= IB_QPS_RTS
;
935 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
940 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
941 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
943 mutex_unlock(&id_priv
->qp_mutex
);
947 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
949 struct ib_qp_attr qp_attr
;
952 mutex_lock(&id_priv
->qp_mutex
);
953 if (!id_priv
->id
.qp
) {
958 qp_attr
.qp_state
= IB_QPS_ERR
;
959 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
961 mutex_unlock(&id_priv
->qp_mutex
);
965 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
966 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
968 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
972 if (rdma_cap_eth_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
975 pkey
= ib_addr_get_pkey(dev_addr
);
977 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
978 pkey
, &qp_attr
->pkey_index
);
982 qp_attr
->port_num
= id_priv
->id
.port_num
;
983 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
985 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
986 ret
= cma_set_qkey(id_priv
, 0);
990 qp_attr
->qkey
= id_priv
->qkey
;
991 *qp_attr_mask
|= IB_QP_QKEY
;
993 qp_attr
->qp_access_flags
= 0;
994 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
999 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
1002 struct rdma_id_private
*id_priv
;
1005 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1006 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
1007 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
1008 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
1010 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
1013 if (qp_attr
->qp_state
== IB_QPS_RTR
)
1014 qp_attr
->rq_psn
= id_priv
->seq_num
;
1015 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
1016 if (!id_priv
->cm_id
.iw
) {
1017 qp_attr
->qp_access_flags
= 0;
1018 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1020 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
1027 EXPORT_SYMBOL(rdma_init_qp_attr
);
1029 static inline int cma_zero_addr(struct sockaddr
*addr
)
1031 switch (addr
->sa_family
) {
1033 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1035 return ipv6_addr_any(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
1037 return ib_addr_any(&((struct sockaddr_ib
*) addr
)->sib_addr
);
1043 static inline int cma_loopback_addr(struct sockaddr
*addr
)
1045 switch (addr
->sa_family
) {
1047 return ipv4_is_loopback(((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
1049 return ipv6_addr_loopback(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
1051 return ib_addr_loopback(&((struct sockaddr_ib
*) addr
)->sib_addr
);
1057 static inline int cma_any_addr(struct sockaddr
*addr
)
1059 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
1062 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
1064 if (src
->sa_family
!= dst
->sa_family
)
1067 switch (src
->sa_family
) {
1069 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
1070 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
1072 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
1073 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
1075 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
1076 &((struct sockaddr_ib
*) dst
)->sib_addr
);
1080 static __be16
cma_port(struct sockaddr
*addr
)
1082 struct sockaddr_ib
*sib
;
1084 switch (addr
->sa_family
) {
1086 return ((struct sockaddr_in
*) addr
)->sin_port
;
1088 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
1090 sib
= (struct sockaddr_ib
*) addr
;
1091 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
1092 be64_to_cpu(sib
->sib_sid_mask
)));
1098 static inline int cma_any_port(struct sockaddr
*addr
)
1100 return !cma_port(addr
);
1103 static void cma_save_ib_info(struct sockaddr
*src_addr
,
1104 struct sockaddr
*dst_addr
,
1105 struct rdma_cm_id
*listen_id
,
1106 struct ib_sa_path_rec
*path
)
1108 struct sockaddr_ib
*listen_ib
, *ib
;
1110 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
1112 ib
= (struct sockaddr_ib
*)src_addr
;
1113 ib
->sib_family
= AF_IB
;
1115 ib
->sib_pkey
= path
->pkey
;
1116 ib
->sib_flowinfo
= path
->flow_label
;
1117 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
1118 ib
->sib_sid
= path
->service_id
;
1119 ib
->sib_scope_id
= 0;
1121 ib
->sib_pkey
= listen_ib
->sib_pkey
;
1122 ib
->sib_flowinfo
= listen_ib
->sib_flowinfo
;
1123 ib
->sib_addr
= listen_ib
->sib_addr
;
1124 ib
->sib_sid
= listen_ib
->sib_sid
;
1125 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
1127 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
1130 ib
= (struct sockaddr_ib
*)dst_addr
;
1131 ib
->sib_family
= AF_IB
;
1133 ib
->sib_pkey
= path
->pkey
;
1134 ib
->sib_flowinfo
= path
->flow_label
;
1135 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
1140 static void cma_save_ip4_info(struct sockaddr_in
*src_addr
,
1141 struct sockaddr_in
*dst_addr
,
1142 struct cma_hdr
*hdr
,
1146 *src_addr
= (struct sockaddr_in
) {
1147 .sin_family
= AF_INET
,
1148 .sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
,
1149 .sin_port
= local_port
,
1154 *dst_addr
= (struct sockaddr_in
) {
1155 .sin_family
= AF_INET
,
1156 .sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
,
1157 .sin_port
= hdr
->port
,
1162 static void cma_save_ip6_info(struct sockaddr_in6
*src_addr
,
1163 struct sockaddr_in6
*dst_addr
,
1164 struct cma_hdr
*hdr
,
1168 *src_addr
= (struct sockaddr_in6
) {
1169 .sin6_family
= AF_INET6
,
1170 .sin6_addr
= hdr
->dst_addr
.ip6
,
1171 .sin6_port
= local_port
,
1176 *dst_addr
= (struct sockaddr_in6
) {
1177 .sin6_family
= AF_INET6
,
1178 .sin6_addr
= hdr
->src_addr
.ip6
,
1179 .sin6_port
= hdr
->port
,
1184 static u16
cma_port_from_service_id(__be64 service_id
)
1186 return (u16
)be64_to_cpu(service_id
);
1189 static int cma_save_ip_info(struct sockaddr
*src_addr
,
1190 struct sockaddr
*dst_addr
,
1191 struct ib_cm_event
*ib_event
,
1194 struct cma_hdr
*hdr
;
1197 hdr
= ib_event
->private_data
;
1198 if (hdr
->cma_version
!= CMA_VERSION
)
1201 port
= htons(cma_port_from_service_id(service_id
));
1203 switch (cma_get_ip_ver(hdr
)) {
1205 cma_save_ip4_info((struct sockaddr_in
*)src_addr
,
1206 (struct sockaddr_in
*)dst_addr
, hdr
, port
);
1209 cma_save_ip6_info((struct sockaddr_in6
*)src_addr
,
1210 (struct sockaddr_in6
*)dst_addr
, hdr
, port
);
1213 return -EAFNOSUPPORT
;
1219 static int cma_save_net_info(struct sockaddr
*src_addr
,
1220 struct sockaddr
*dst_addr
,
1221 struct rdma_cm_id
*listen_id
,
1222 struct ib_cm_event
*ib_event
,
1223 sa_family_t sa_family
, __be64 service_id
)
1225 if (sa_family
== AF_IB
) {
1226 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1227 cma_save_ib_info(src_addr
, dst_addr
, listen_id
,
1228 ib_event
->param
.req_rcvd
.primary_path
);
1229 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1230 cma_save_ib_info(src_addr
, dst_addr
, listen_id
, NULL
);
1234 return cma_save_ip_info(src_addr
, dst_addr
, ib_event
, service_id
);
1237 static int cma_save_req_info(const struct ib_cm_event
*ib_event
,
1238 struct cma_req_info
*req
)
1240 const struct ib_cm_req_event_param
*req_param
=
1241 &ib_event
->param
.req_rcvd
;
1242 const struct ib_cm_sidr_req_event_param
*sidr_param
=
1243 &ib_event
->param
.sidr_req_rcvd
;
1245 switch (ib_event
->event
) {
1246 case IB_CM_REQ_RECEIVED
:
1247 req
->device
= req_param
->listen_id
->device
;
1248 req
->port
= req_param
->port
;
1249 memcpy(&req
->local_gid
, &req_param
->primary_path
->sgid
,
1250 sizeof(req
->local_gid
));
1251 req
->has_gid
= true;
1252 req
->service_id
= req_param
->primary_path
->service_id
;
1253 req
->pkey
= be16_to_cpu(req_param
->primary_path
->pkey
);
1254 if (req
->pkey
!= req_param
->bth_pkey
)
1255 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1256 "RDMA CMA: in the future this may cause the request to be dropped\n",
1257 req_param
->bth_pkey
, req
->pkey
);
1259 case IB_CM_SIDR_REQ_RECEIVED
:
1260 req
->device
= sidr_param
->listen_id
->device
;
1261 req
->port
= sidr_param
->port
;
1262 req
->has_gid
= false;
1263 req
->service_id
= sidr_param
->service_id
;
1264 req
->pkey
= sidr_param
->pkey
;
1265 if (req
->pkey
!= sidr_param
->bth_pkey
)
1266 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1267 "RDMA CMA: in the future this may cause the request to be dropped\n",
1268 sidr_param
->bth_pkey
, req
->pkey
);
1277 static bool validate_ipv4_net_dev(struct net_device
*net_dev
,
1278 const struct sockaddr_in
*dst_addr
,
1279 const struct sockaddr_in
*src_addr
)
1281 __be32 daddr
= dst_addr
->sin_addr
.s_addr
,
1282 saddr
= src_addr
->sin_addr
.s_addr
;
1283 struct fib_result res
;
1288 if (ipv4_is_multicast(saddr
) || ipv4_is_lbcast(saddr
) ||
1289 ipv4_is_lbcast(daddr
) || ipv4_is_zeronet(saddr
) ||
1290 ipv4_is_zeronet(daddr
) || ipv4_is_loopback(daddr
) ||
1291 ipv4_is_loopback(saddr
))
1294 memset(&fl4
, 0, sizeof(fl4
));
1295 fl4
.flowi4_iif
= net_dev
->ifindex
;
1300 err
= fib_lookup(dev_net(net_dev
), &fl4
, &res
, 0);
1301 ret
= err
== 0 && FIB_RES_DEV(res
) == net_dev
;
1307 static bool validate_ipv6_net_dev(struct net_device
*net_dev
,
1308 const struct sockaddr_in6
*dst_addr
,
1309 const struct sockaddr_in6
*src_addr
)
1311 #if IS_ENABLED(CONFIG_IPV6)
1312 const int strict
= ipv6_addr_type(&dst_addr
->sin6_addr
) &
1313 IPV6_ADDR_LINKLOCAL
;
1314 struct rt6_info
*rt
= rt6_lookup(dev_net(net_dev
), &dst_addr
->sin6_addr
,
1315 &src_addr
->sin6_addr
, net_dev
->ifindex
,
1322 ret
= rt
->rt6i_idev
->dev
== net_dev
;
1331 static bool validate_net_dev(struct net_device
*net_dev
,
1332 const struct sockaddr
*daddr
,
1333 const struct sockaddr
*saddr
)
1335 const struct sockaddr_in
*daddr4
= (const struct sockaddr_in
*)daddr
;
1336 const struct sockaddr_in
*saddr4
= (const struct sockaddr_in
*)saddr
;
1337 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1338 const struct sockaddr_in6
*saddr6
= (const struct sockaddr_in6
*)saddr
;
1340 switch (daddr
->sa_family
) {
1342 return saddr
->sa_family
== AF_INET
&&
1343 validate_ipv4_net_dev(net_dev
, daddr4
, saddr4
);
1346 return saddr
->sa_family
== AF_INET6
&&
1347 validate_ipv6_net_dev(net_dev
, daddr6
, saddr6
);
1354 static struct net_device
*cma_get_net_dev(struct ib_cm_event
*ib_event
,
1355 const struct cma_req_info
*req
)
1357 struct sockaddr_storage listen_addr_storage
, src_addr_storage
;
1358 struct sockaddr
*listen_addr
= (struct sockaddr
*)&listen_addr_storage
,
1359 *src_addr
= (struct sockaddr
*)&src_addr_storage
;
1360 struct net_device
*net_dev
;
1361 const union ib_gid
*gid
= req
->has_gid
? &req
->local_gid
: NULL
;
1364 err
= cma_save_ip_info(listen_addr
, src_addr
, ib_event
,
1367 return ERR_PTR(err
);
1369 net_dev
= ib_get_net_dev_by_params(req
->device
, req
->port
, req
->pkey
,
1372 return ERR_PTR(-ENODEV
);
1374 if (!validate_net_dev(net_dev
, listen_addr
, src_addr
)) {
1376 return ERR_PTR(-EHOSTUNREACH
);
1382 static enum rdma_port_space
rdma_ps_from_service_id(__be64 service_id
)
1384 return (be64_to_cpu(service_id
) >> 16) & 0xffff;
1387 static bool cma_match_private_data(struct rdma_id_private
*id_priv
,
1388 const struct cma_hdr
*hdr
)
1390 struct sockaddr
*addr
= cma_src_addr(id_priv
);
1392 struct in6_addr ip6_addr
;
1394 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1397 switch (addr
->sa_family
) {
1399 ip4_addr
= ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
;
1400 if (cma_get_ip_ver(hdr
) != 4)
1402 if (!cma_any_addr(addr
) &&
1403 hdr
->dst_addr
.ip4
.addr
!= ip4_addr
)
1407 ip6_addr
= ((struct sockaddr_in6
*)addr
)->sin6_addr
;
1408 if (cma_get_ip_ver(hdr
) != 6)
1410 if (!cma_any_addr(addr
) &&
1411 memcmp(&hdr
->dst_addr
.ip6
, &ip6_addr
, sizeof(ip6_addr
)))
1423 static bool cma_protocol_roce_dev_port(struct ib_device
*device
, int port_num
)
1425 enum rdma_link_layer ll
= rdma_port_get_link_layer(device
, port_num
);
1426 enum rdma_transport_type transport
=
1427 rdma_node_get_transport(device
->node_type
);
1429 return ll
== IB_LINK_LAYER_ETHERNET
&& transport
== RDMA_TRANSPORT_IB
;
1432 static bool cma_protocol_roce(const struct rdma_cm_id
*id
)
1434 struct ib_device
*device
= id
->device
;
1435 const int port_num
= id
->port_num
?: rdma_start_port(device
);
1437 return cma_protocol_roce_dev_port(device
, port_num
);
1440 static bool cma_match_net_dev(const struct rdma_cm_id
*id
,
1441 const struct net_device
*net_dev
,
1444 const struct rdma_addr
*addr
= &id
->route
.addr
;
1447 /* This request is an AF_IB request or a RoCE request */
1448 return (!id
->port_num
|| id
->port_num
== port_num
) &&
1449 (addr
->src_addr
.ss_family
== AF_IB
||
1450 cma_protocol_roce_dev_port(id
->device
, port_num
));
1452 return !addr
->dev_addr
.bound_dev_if
||
1453 (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1454 addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
);
1457 static struct rdma_id_private
*cma_find_listener(
1458 const struct rdma_bind_list
*bind_list
,
1459 const struct ib_cm_id
*cm_id
,
1460 const struct ib_cm_event
*ib_event
,
1461 const struct cma_req_info
*req
,
1462 const struct net_device
*net_dev
)
1464 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1467 return ERR_PTR(-EINVAL
);
1469 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1470 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1471 if (id_priv
->id
.device
== cm_id
->device
&&
1472 cma_match_net_dev(&id_priv
->id
, net_dev
, req
->port
))
1474 list_for_each_entry(id_priv_dev
,
1475 &id_priv
->listen_list
,
1477 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1478 cma_match_net_dev(&id_priv_dev
->id
, net_dev
, req
->port
))
1484 return ERR_PTR(-EINVAL
);
1487 static struct rdma_id_private
*cma_id_from_event(struct ib_cm_id
*cm_id
,
1488 struct ib_cm_event
*ib_event
,
1489 struct net_device
**net_dev
)
1491 struct cma_req_info req
;
1492 struct rdma_bind_list
*bind_list
;
1493 struct rdma_id_private
*id_priv
;
1496 err
= cma_save_req_info(ib_event
, &req
);
1498 return ERR_PTR(err
);
1500 *net_dev
= cma_get_net_dev(ib_event
, &req
);
1501 if (IS_ERR(*net_dev
)) {
1502 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1503 /* Assuming the protocol is AF_IB */
1505 } else if (cma_protocol_roce_dev_port(req
.device
, req
.port
)) {
1506 /* TODO find the net dev matching the request parameters
1507 * through the RoCE GID table */
1510 return ERR_CAST(*net_dev
);
1514 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1515 rdma_ps_from_service_id(req
.service_id
),
1516 cma_port_from_service_id(req
.service_id
));
1517 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, &req
, *net_dev
);
1518 if (IS_ERR(id_priv
) && *net_dev
) {
1526 static inline int cma_user_data_offset(struct rdma_id_private
*id_priv
)
1528 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1531 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1533 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1535 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1539 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1541 struct rdma_id_private
*dev_id_priv
;
1544 * Remove from listen_any_list to prevent added devices from spawning
1545 * additional listen requests.
1548 list_del(&id_priv
->list
);
1550 while (!list_empty(&id_priv
->listen_list
)) {
1551 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1552 struct rdma_id_private
, listen_list
);
1553 /* sync with device removal to avoid duplicate destruction */
1554 list_del_init(&dev_id_priv
->list
);
1555 list_del(&dev_id_priv
->listen_list
);
1556 mutex_unlock(&lock
);
1558 rdma_destroy_id(&dev_id_priv
->id
);
1561 mutex_unlock(&lock
);
1564 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1565 enum rdma_cm_state state
)
1568 case RDMA_CM_ADDR_QUERY
:
1569 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1571 case RDMA_CM_ROUTE_QUERY
:
1572 cma_cancel_route(id_priv
);
1574 case RDMA_CM_LISTEN
:
1575 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1576 cma_cancel_listens(id_priv
);
1583 static void cma_release_port(struct rdma_id_private
*id_priv
)
1585 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1586 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1592 hlist_del(&id_priv
->node
);
1593 if (hlist_empty(&bind_list
->owners
)) {
1594 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1597 mutex_unlock(&lock
);
1600 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1602 struct cma_multicast
*mc
;
1604 while (!list_empty(&id_priv
->mc_list
)) {
1605 mc
= container_of(id_priv
->mc_list
.next
,
1606 struct cma_multicast
, list
);
1607 list_del(&mc
->list
);
1608 if (rdma_cap_ib_mcast(id_priv
->cma_dev
->device
,
1609 id_priv
->id
.port_num
)) {
1610 ib_sa_free_multicast(mc
->multicast
.ib
);
1613 if (mc
->igmp_joined
) {
1614 struct rdma_dev_addr
*dev_addr
=
1615 &id_priv
->id
.route
.addr
.dev_addr
;
1616 struct net_device
*ndev
= NULL
;
1618 if (dev_addr
->bound_dev_if
)
1619 ndev
= dev_get_by_index(&init_net
,
1620 dev_addr
->bound_dev_if
);
1623 &mc
->multicast
.ib
->rec
.mgid
,
1628 kref_put(&mc
->mcref
, release_mc
);
1633 void rdma_destroy_id(struct rdma_cm_id
*id
)
1635 struct rdma_id_private
*id_priv
;
1636 enum rdma_cm_state state
;
1638 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1639 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1640 cma_cancel_operation(id_priv
, state
);
1643 * Wait for any active callback to finish. New callbacks will find
1644 * the id_priv state set to destroying and abort.
1646 mutex_lock(&id_priv
->handler_mutex
);
1647 mutex_unlock(&id_priv
->handler_mutex
);
1649 if (id_priv
->cma_dev
) {
1650 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1651 if (id_priv
->cm_id
.ib
)
1652 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1653 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1654 if (id_priv
->cm_id
.iw
)
1655 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1657 cma_leave_mc_groups(id_priv
);
1658 cma_release_dev(id_priv
);
1661 cma_release_port(id_priv
);
1662 cma_deref_id(id_priv
);
1663 wait_for_completion(&id_priv
->comp
);
1665 if (id_priv
->internal_id
)
1666 cma_deref_id(id_priv
->id
.context
);
1668 kfree(id_priv
->id
.route
.path_rec
);
1669 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1672 EXPORT_SYMBOL(rdma_destroy_id
);
1674 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1678 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1682 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1686 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1692 cma_modify_qp_err(id_priv
);
1693 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1698 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1699 struct ib_cm_rep_event_param
*rep_data
,
1702 event
->param
.conn
.private_data
= private_data
;
1703 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1704 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1705 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1706 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1707 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1708 event
->param
.conn
.srq
= rep_data
->srq
;
1709 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1712 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1714 struct rdma_id_private
*id_priv
= cm_id
->context
;
1715 struct rdma_cm_event event
;
1718 mutex_lock(&id_priv
->handler_mutex
);
1719 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1720 id_priv
->state
!= RDMA_CM_CONNECT
) ||
1721 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1722 id_priv
->state
!= RDMA_CM_DISCONNECT
))
1725 memset(&event
, 0, sizeof event
);
1726 switch (ib_event
->event
) {
1727 case IB_CM_REQ_ERROR
:
1728 case IB_CM_REP_ERROR
:
1729 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1730 event
.status
= -ETIMEDOUT
;
1732 case IB_CM_REP_RECEIVED
:
1733 if (id_priv
->id
.qp
) {
1734 event
.status
= cma_rep_recv(id_priv
);
1735 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1736 RDMA_CM_EVENT_ESTABLISHED
;
1738 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1740 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1741 ib_event
->private_data
);
1743 case IB_CM_RTU_RECEIVED
:
1744 case IB_CM_USER_ESTABLISHED
:
1745 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1747 case IB_CM_DREQ_ERROR
:
1748 event
.status
= -ETIMEDOUT
; /* fall through */
1749 case IB_CM_DREQ_RECEIVED
:
1750 case IB_CM_DREP_RECEIVED
:
1751 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1752 RDMA_CM_DISCONNECT
))
1754 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1756 case IB_CM_TIMEWAIT_EXIT
:
1757 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1759 case IB_CM_MRA_RECEIVED
:
1762 case IB_CM_REJ_RECEIVED
:
1763 cma_modify_qp_err(id_priv
);
1764 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1765 event
.event
= RDMA_CM_EVENT_REJECTED
;
1766 event
.param
.conn
.private_data
= ib_event
->private_data
;
1767 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1770 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1775 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1777 /* Destroy the CM ID by returning a non-zero value. */
1778 id_priv
->cm_id
.ib
= NULL
;
1779 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1780 mutex_unlock(&id_priv
->handler_mutex
);
1781 rdma_destroy_id(&id_priv
->id
);
1785 mutex_unlock(&id_priv
->handler_mutex
);
1789 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1790 struct ib_cm_event
*ib_event
,
1791 struct net_device
*net_dev
)
1793 struct rdma_id_private
*id_priv
;
1794 struct rdma_cm_id
*id
;
1795 struct rdma_route
*rt
;
1796 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1797 const __be64 service_id
=
1798 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
1801 id
= rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
1802 listen_id
->event_handler
, listen_id
->context
,
1803 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
);
1807 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1808 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1809 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1810 listen_id
, ib_event
, ss_family
, service_id
))
1814 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1815 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1820 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1821 if (rt
->num_paths
== 2)
1822 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1825 ret
= rdma_copy_addr(&rt
->addr
.dev_addr
, net_dev
, NULL
);
1829 if (!cma_protocol_roce(listen_id
) &&
1830 cma_any_addr(cma_src_addr(id_priv
))) {
1831 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1832 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1833 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1834 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
1835 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
1840 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1842 id_priv
->state
= RDMA_CM_CONNECT
;
1846 rdma_destroy_id(id
);
1850 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1851 struct ib_cm_event
*ib_event
,
1852 struct net_device
*net_dev
)
1854 struct rdma_id_private
*id_priv
;
1855 struct rdma_cm_id
*id
;
1856 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1857 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
1860 id
= rdma_create_id(net
, listen_id
->event_handler
, listen_id
->context
,
1861 listen_id
->ps
, IB_QPT_UD
);
1865 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1866 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1867 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1868 listen_id
, ib_event
, ss_family
,
1869 ib_event
->param
.sidr_req_rcvd
.service_id
))
1873 ret
= rdma_copy_addr(&id
->route
.addr
.dev_addr
, net_dev
, NULL
);
1877 if (!cma_any_addr(cma_src_addr(id_priv
))) {
1878 ret
= cma_translate_addr(cma_src_addr(id_priv
),
1879 &id
->route
.addr
.dev_addr
);
1885 id_priv
->state
= RDMA_CM_CONNECT
;
1888 rdma_destroy_id(id
);
1892 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1893 struct ib_cm_req_event_param
*req_data
,
1894 void *private_data
, int offset
)
1896 event
->param
.conn
.private_data
= private_data
+ offset
;
1897 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1898 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1899 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1900 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1901 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1902 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1903 event
->param
.conn
.srq
= req_data
->srq
;
1904 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1907 static int cma_check_req_qp_type(struct rdma_cm_id
*id
, struct ib_cm_event
*ib_event
)
1909 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
1910 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
1911 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
1912 (id
->qp_type
== IB_QPT_UD
)) ||
1916 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1918 struct rdma_id_private
*listen_id
, *conn_id
= NULL
;
1919 struct rdma_cm_event event
;
1920 struct net_device
*net_dev
;
1923 listen_id
= cma_id_from_event(cm_id
, ib_event
, &net_dev
);
1924 if (IS_ERR(listen_id
))
1925 return PTR_ERR(listen_id
);
1927 if (!cma_check_req_qp_type(&listen_id
->id
, ib_event
)) {
1932 mutex_lock(&listen_id
->handler_mutex
);
1933 if (listen_id
->state
!= RDMA_CM_LISTEN
) {
1934 ret
= -ECONNABORTED
;
1938 memset(&event
, 0, sizeof event
);
1939 offset
= cma_user_data_offset(listen_id
);
1940 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1941 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
1942 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
1943 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1944 event
.param
.ud
.private_data_len
=
1945 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1947 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
1948 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1949 ib_event
->private_data
, offset
);
1956 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1957 ret
= cma_acquire_dev(conn_id
, listen_id
);
1961 conn_id
->cm_id
.ib
= cm_id
;
1962 cm_id
->context
= conn_id
;
1963 cm_id
->cm_handler
= cma_ib_handler
;
1966 * Protect against the user destroying conn_id from another thread
1967 * until we're done accessing it.
1969 atomic_inc(&conn_id
->refcount
);
1970 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1974 * Acquire mutex to prevent user executing rdma_destroy_id()
1975 * while we're accessing the cm_id.
1978 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) &&
1979 (conn_id
->id
.qp_type
!= IB_QPT_UD
))
1980 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1981 mutex_unlock(&lock
);
1982 mutex_unlock(&conn_id
->handler_mutex
);
1983 mutex_unlock(&listen_id
->handler_mutex
);
1984 cma_deref_id(conn_id
);
1990 cma_deref_id(conn_id
);
1991 /* Destroy the CM ID by returning a non-zero value. */
1992 conn_id
->cm_id
.ib
= NULL
;
1994 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1995 mutex_unlock(&conn_id
->handler_mutex
);
1997 mutex_unlock(&listen_id
->handler_mutex
);
1999 rdma_destroy_id(&conn_id
->id
);
2008 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2010 if (addr
->sa_family
== AF_IB
)
2011 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
2013 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
2015 EXPORT_SYMBOL(rdma_get_service_id
);
2017 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
2019 struct rdma_id_private
*id_priv
= iw_id
->context
;
2020 struct rdma_cm_event event
;
2022 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2023 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2025 mutex_lock(&id_priv
->handler_mutex
);
2026 if (id_priv
->state
!= RDMA_CM_CONNECT
)
2029 memset(&event
, 0, sizeof event
);
2030 switch (iw_event
->event
) {
2031 case IW_CM_EVENT_CLOSE
:
2032 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
2034 case IW_CM_EVENT_CONNECT_REPLY
:
2035 memcpy(cma_src_addr(id_priv
), laddr
,
2036 rdma_addr_size(laddr
));
2037 memcpy(cma_dst_addr(id_priv
), raddr
,
2038 rdma_addr_size(raddr
));
2039 switch (iw_event
->status
) {
2041 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2042 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2043 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2047 event
.event
= RDMA_CM_EVENT_REJECTED
;
2050 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2053 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
2057 case IW_CM_EVENT_ESTABLISHED
:
2058 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2059 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2060 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2066 event
.status
= iw_event
->status
;
2067 event
.param
.conn
.private_data
= iw_event
->private_data
;
2068 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2069 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2071 /* Destroy the CM ID by returning a non-zero value. */
2072 id_priv
->cm_id
.iw
= NULL
;
2073 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2074 mutex_unlock(&id_priv
->handler_mutex
);
2075 rdma_destroy_id(&id_priv
->id
);
2080 mutex_unlock(&id_priv
->handler_mutex
);
2084 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
2085 struct iw_cm_event
*iw_event
)
2087 struct rdma_cm_id
*new_cm_id
;
2088 struct rdma_id_private
*listen_id
, *conn_id
;
2089 struct rdma_cm_event event
;
2090 int ret
= -ECONNABORTED
;
2091 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2092 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2094 listen_id
= cm_id
->context
;
2096 mutex_lock(&listen_id
->handler_mutex
);
2097 if (listen_id
->state
!= RDMA_CM_LISTEN
)
2100 /* Create a new RDMA id for the new IW CM ID */
2101 new_cm_id
= rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
2102 listen_id
->id
.event_handler
,
2103 listen_id
->id
.context
,
2104 RDMA_PS_TCP
, IB_QPT_RC
);
2105 if (IS_ERR(new_cm_id
)) {
2109 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
2110 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2111 conn_id
->state
= RDMA_CM_CONNECT
;
2113 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
, NULL
);
2115 mutex_unlock(&conn_id
->handler_mutex
);
2116 rdma_destroy_id(new_cm_id
);
2120 ret
= cma_acquire_dev(conn_id
, listen_id
);
2122 mutex_unlock(&conn_id
->handler_mutex
);
2123 rdma_destroy_id(new_cm_id
);
2127 conn_id
->cm_id
.iw
= cm_id
;
2128 cm_id
->context
= conn_id
;
2129 cm_id
->cm_handler
= cma_iw_handler
;
2131 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
2132 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
2134 memset(&event
, 0, sizeof event
);
2135 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2136 event
.param
.conn
.private_data
= iw_event
->private_data
;
2137 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2138 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2139 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2142 * Protect against the user destroying conn_id from another thread
2143 * until we're done accessing it.
2145 atomic_inc(&conn_id
->refcount
);
2146 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
2148 /* User wants to destroy the CM ID */
2149 conn_id
->cm_id
.iw
= NULL
;
2150 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2151 mutex_unlock(&conn_id
->handler_mutex
);
2152 cma_deref_id(conn_id
);
2153 rdma_destroy_id(&conn_id
->id
);
2157 mutex_unlock(&conn_id
->handler_mutex
);
2158 cma_deref_id(conn_id
);
2161 mutex_unlock(&listen_id
->handler_mutex
);
2165 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
2167 struct sockaddr
*addr
;
2168 struct ib_cm_id
*id
;
2171 addr
= cma_src_addr(id_priv
);
2172 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
2173 id
= ib_cm_insert_listen(id_priv
->id
.device
, cma_req_handler
, svc_id
);
2176 id_priv
->cm_id
.ib
= id
;
2181 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
2184 struct iw_cm_id
*id
;
2186 id
= iw_create_cm_id(id_priv
->id
.device
,
2187 iw_conn_req_handler
,
2192 id
->tos
= id_priv
->tos
;
2193 id_priv
->cm_id
.iw
= id
;
2195 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2196 rdma_addr_size(cma_src_addr(id_priv
)));
2198 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2201 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2202 id_priv
->cm_id
.iw
= NULL
;
2208 static int cma_listen_handler(struct rdma_cm_id
*id
,
2209 struct rdma_cm_event
*event
)
2211 struct rdma_id_private
*id_priv
= id
->context
;
2213 id
->context
= id_priv
->id
.context
;
2214 id
->event_handler
= id_priv
->id
.event_handler
;
2215 return id_priv
->id
.event_handler(id
, event
);
2218 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2219 struct cma_device
*cma_dev
)
2221 struct rdma_id_private
*dev_id_priv
;
2222 struct rdma_cm_id
*id
;
2223 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2226 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2229 id
= rdma_create_id(net
, cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
2230 id_priv
->id
.qp_type
);
2234 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
2236 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2237 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2238 rdma_addr_size(cma_src_addr(id_priv
)));
2240 _cma_attach_to_dev(dev_id_priv
, cma_dev
);
2241 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2242 atomic_inc(&id_priv
->refcount
);
2243 dev_id_priv
->internal_id
= 1;
2244 dev_id_priv
->afonly
= id_priv
->afonly
;
2246 ret
= rdma_listen(id
, id_priv
->backlog
);
2248 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2249 ret
, cma_dev
->device
->name
);
2252 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
2254 struct cma_device
*cma_dev
;
2257 list_add_tail(&id_priv
->list
, &listen_any_list
);
2258 list_for_each_entry(cma_dev
, &dev_list
, list
)
2259 cma_listen_on_dev(id_priv
, cma_dev
);
2260 mutex_unlock(&lock
);
2263 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2265 struct rdma_id_private
*id_priv
;
2267 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2268 id_priv
->tos
= (u8
) tos
;
2270 EXPORT_SYMBOL(rdma_set_service_type
);
2272 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
2275 struct cma_work
*work
= context
;
2276 struct rdma_route
*route
;
2278 route
= &work
->id
->id
.route
;
2281 route
->num_paths
= 1;
2282 *route
->path_rec
= *path_rec
;
2284 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2285 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2286 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2287 work
->event
.status
= status
;
2290 queue_work(cma_wq
, &work
->work
);
2293 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
2294 struct cma_work
*work
)
2296 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2297 struct ib_sa_path_rec path_rec
;
2298 ib_sa_comp_mask comp_mask
;
2299 struct sockaddr_in6
*sin6
;
2300 struct sockaddr_ib
*sib
;
2302 memset(&path_rec
, 0, sizeof path_rec
);
2303 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2304 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2305 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2306 path_rec
.numb_path
= 1;
2307 path_rec
.reversible
= 1;
2308 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
2310 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2311 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2312 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2314 switch (cma_family(id_priv
)) {
2316 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2317 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2320 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2321 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2322 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2325 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2326 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2327 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2331 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2332 id_priv
->id
.port_num
, &path_rec
,
2333 comp_mask
, timeout_ms
,
2334 GFP_KERNEL
, cma_query_handler
,
2335 work
, &id_priv
->query
);
2337 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2340 static void cma_work_handler(struct work_struct
*_work
)
2342 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2343 struct rdma_id_private
*id_priv
= work
->id
;
2346 mutex_lock(&id_priv
->handler_mutex
);
2347 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2350 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2351 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2355 mutex_unlock(&id_priv
->handler_mutex
);
2356 cma_deref_id(id_priv
);
2358 rdma_destroy_id(&id_priv
->id
);
2362 static void cma_ndev_work_handler(struct work_struct
*_work
)
2364 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
2365 struct rdma_id_private
*id_priv
= work
->id
;
2368 mutex_lock(&id_priv
->handler_mutex
);
2369 if (id_priv
->state
== RDMA_CM_DESTROYING
||
2370 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
2373 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2374 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2379 mutex_unlock(&id_priv
->handler_mutex
);
2380 cma_deref_id(id_priv
);
2382 rdma_destroy_id(&id_priv
->id
);
2386 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2388 struct rdma_route
*route
= &id_priv
->id
.route
;
2389 struct cma_work
*work
;
2392 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2397 INIT_WORK(&work
->work
, cma_work_handler
);
2398 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2399 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2400 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2402 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2403 if (!route
->path_rec
) {
2408 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2414 kfree(route
->path_rec
);
2415 route
->path_rec
= NULL
;
2421 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
2422 struct ib_sa_path_rec
*path_rec
, int num_paths
)
2424 struct rdma_id_private
*id_priv
;
2427 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2428 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2429 RDMA_CM_ROUTE_RESOLVED
))
2432 id
->route
.path_rec
= kmemdup(path_rec
, sizeof *path_rec
* num_paths
,
2434 if (!id
->route
.path_rec
) {
2439 id
->route
.num_paths
= num_paths
;
2442 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2445 EXPORT_SYMBOL(rdma_set_ib_paths
);
2447 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2449 struct cma_work
*work
;
2451 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2456 INIT_WORK(&work
->work
, cma_work_handler
);
2457 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2458 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2459 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2460 queue_work(cma_wq
, &work
->work
);
2464 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2467 struct net_device
*dev
;
2469 prio
= rt_tos2priority(tos
);
2470 dev
= ndev
->priv_flags
& IFF_802_1Q_VLAN
?
2471 vlan_dev_real_dev(ndev
) : ndev
;
2474 return netdev_get_prio_tc_map(dev
, prio
);
2476 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2477 if (ndev
->priv_flags
& IFF_802_1Q_VLAN
)
2478 return (vlan_dev_get_egress_qos_mask(ndev
, prio
) &
2479 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2484 static enum ib_gid_type
cma_route_gid_type(enum rdma_network_type network_type
,
2485 unsigned long supported_gids
,
2486 enum ib_gid_type default_gid
)
2488 if ((network_type
== RDMA_NETWORK_IPV4
||
2489 network_type
== RDMA_NETWORK_IPV6
) &&
2490 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP
, &supported_gids
))
2491 return IB_GID_TYPE_ROCE_UDP_ENCAP
;
2496 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2498 struct rdma_route
*route
= &id_priv
->id
.route
;
2499 struct rdma_addr
*addr
= &route
->addr
;
2500 struct cma_work
*work
;
2502 struct net_device
*ndev
= NULL
;
2505 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2510 INIT_WORK(&work
->work
, cma_work_handler
);
2512 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2513 if (!route
->path_rec
) {
2518 route
->num_paths
= 1;
2520 if (addr
->dev_addr
.bound_dev_if
) {
2521 unsigned long supported_gids
;
2523 ndev
= dev_get_by_index(&init_net
, addr
->dev_addr
.bound_dev_if
);
2529 if (ndev
->flags
& IFF_LOOPBACK
) {
2531 if (!id_priv
->id
.device
->get_netdev
) {
2536 ndev
= id_priv
->id
.device
->get_netdev(id_priv
->id
.device
,
2537 id_priv
->id
.port_num
);
2544 route
->path_rec
->net
= &init_net
;
2545 route
->path_rec
->ifindex
= ndev
->ifindex
;
2546 supported_gids
= roce_gid_type_mask_support(id_priv
->id
.device
,
2547 id_priv
->id
.port_num
);
2548 route
->path_rec
->gid_type
=
2549 cma_route_gid_type(addr
->dev_addr
.network
,
2558 memcpy(route
->path_rec
->dmac
, addr
->dev_addr
.dst_dev_addr
, ETH_ALEN
);
2560 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2561 &route
->path_rec
->sgid
);
2562 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2563 &route
->path_rec
->dgid
);
2565 /* Use the hint from IP Stack to select GID Type */
2566 if (route
->path_rec
->gid_type
< ib_network_to_gid_type(addr
->dev_addr
.network
))
2567 route
->path_rec
->gid_type
= ib_network_to_gid_type(addr
->dev_addr
.network
);
2568 if (((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
)->sa_family
!= AF_IB
)
2569 /* TODO: get the hoplimit from the inet/inet6 device */
2570 route
->path_rec
->hop_limit
= addr
->dev_addr
.hoplimit
;
2572 route
->path_rec
->hop_limit
= 1;
2573 route
->path_rec
->reversible
= 1;
2574 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
2575 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
2576 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, id_priv
->tos
);
2577 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
2578 route
->path_rec
->rate_selector
= IB_SA_EQ
;
2579 route
->path_rec
->rate
= iboe_get_rate(ndev
);
2581 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
2582 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
2583 if (!route
->path_rec
->mtu
) {
2588 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2589 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2590 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2591 work
->event
.status
= 0;
2593 queue_work(cma_wq
, &work
->work
);
2598 kfree(route
->path_rec
);
2599 route
->path_rec
= NULL
;
2605 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
2607 struct rdma_id_private
*id_priv
;
2610 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2611 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
2614 atomic_inc(&id_priv
->refcount
);
2615 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
2616 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
2617 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
2618 ret
= cma_resolve_iboe_route(id_priv
);
2619 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
2620 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
2629 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
2630 cma_deref_id(id_priv
);
2633 EXPORT_SYMBOL(rdma_resolve_route
);
2635 static void cma_set_loopback(struct sockaddr
*addr
)
2637 switch (addr
->sa_family
) {
2639 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2642 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
2646 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
2652 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
2654 struct cma_device
*cma_dev
, *cur_dev
;
2655 struct ib_port_attr port_attr
;
2663 list_for_each_entry(cur_dev
, &dev_list
, list
) {
2664 if (cma_family(id_priv
) == AF_IB
&&
2665 !rdma_cap_ib_cm(cur_dev
->device
, 1))
2671 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
2672 if (!ib_query_port(cur_dev
->device
, p
, &port_attr
) &&
2673 port_attr
.state
== IB_PORT_ACTIVE
) {
2688 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
, NULL
);
2692 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
2696 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
2697 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
2698 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
2700 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2701 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
2702 id_priv
->id
.port_num
= p
;
2703 cma_attach_to_dev(id_priv
, cma_dev
);
2704 cma_set_loopback(cma_src_addr(id_priv
));
2706 mutex_unlock(&lock
);
2710 static void addr_handler(int status
, struct sockaddr
*src_addr
,
2711 struct rdma_dev_addr
*dev_addr
, void *context
)
2713 struct rdma_id_private
*id_priv
= context
;
2714 struct rdma_cm_event event
;
2716 memset(&event
, 0, sizeof event
);
2717 mutex_lock(&id_priv
->handler_mutex
);
2718 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
2719 RDMA_CM_ADDR_RESOLVED
))
2722 memcpy(cma_src_addr(id_priv
), src_addr
, rdma_addr_size(src_addr
));
2723 if (!status
&& !id_priv
->cma_dev
)
2724 status
= cma_acquire_dev(id_priv
, NULL
);
2727 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2728 RDMA_CM_ADDR_BOUND
))
2730 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2731 event
.status
= status
;
2733 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2735 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
2736 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2737 mutex_unlock(&id_priv
->handler_mutex
);
2738 cma_deref_id(id_priv
);
2739 rdma_destroy_id(&id_priv
->id
);
2743 mutex_unlock(&id_priv
->handler_mutex
);
2744 cma_deref_id(id_priv
);
2747 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
2749 struct cma_work
*work
;
2753 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2757 if (!id_priv
->cma_dev
) {
2758 ret
= cma_bind_loopback(id_priv
);
2763 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2764 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2767 INIT_WORK(&work
->work
, cma_work_handler
);
2768 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2769 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2770 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2771 queue_work(cma_wq
, &work
->work
);
2778 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
2780 struct cma_work
*work
;
2783 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2787 if (!id_priv
->cma_dev
) {
2788 ret
= cma_resolve_ib_dev(id_priv
);
2793 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
2794 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
2797 INIT_WORK(&work
->work
, cma_work_handler
);
2798 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2799 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2800 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2801 queue_work(cma_wq
, &work
->work
);
2808 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2809 struct sockaddr
*dst_addr
)
2811 if (!src_addr
|| !src_addr
->sa_family
) {
2812 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2813 src_addr
->sa_family
= dst_addr
->sa_family
;
2814 if (dst_addr
->sa_family
== AF_INET6
) {
2815 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
2816 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
2817 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
2818 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
2819 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
2820 } else if (dst_addr
->sa_family
== AF_IB
) {
2821 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
2822 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
2825 return rdma_bind_addr(id
, src_addr
);
2828 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2829 struct sockaddr
*dst_addr
, int timeout_ms
)
2831 struct rdma_id_private
*id_priv
;
2834 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2835 if (id_priv
->state
== RDMA_CM_IDLE
) {
2836 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2841 if (cma_family(id_priv
) != dst_addr
->sa_family
)
2844 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
))
2847 atomic_inc(&id_priv
->refcount
);
2848 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
2849 if (cma_any_addr(dst_addr
)) {
2850 ret
= cma_resolve_loopback(id_priv
);
2852 if (dst_addr
->sa_family
== AF_IB
) {
2853 ret
= cma_resolve_ib_addr(id_priv
);
2855 ret
= rdma_resolve_ip(&addr_client
, cma_src_addr(id_priv
),
2856 dst_addr
, &id
->route
.addr
.dev_addr
,
2857 timeout_ms
, addr_handler
, id_priv
);
2865 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2866 cma_deref_id(id_priv
);
2869 EXPORT_SYMBOL(rdma_resolve_addr
);
2871 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2873 struct rdma_id_private
*id_priv
;
2874 unsigned long flags
;
2877 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2878 spin_lock_irqsave(&id_priv
->lock
, flags
);
2879 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
2880 id_priv
->reuseaddr
= reuse
;
2885 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2888 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2890 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
2892 struct rdma_id_private
*id_priv
;
2893 unsigned long flags
;
2896 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2897 spin_lock_irqsave(&id_priv
->lock
, flags
);
2898 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
2899 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
2900 id_priv
->afonly
= afonly
;
2905 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2908 EXPORT_SYMBOL(rdma_set_afonly
);
2910 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
2911 struct rdma_id_private
*id_priv
)
2913 struct sockaddr
*addr
;
2914 struct sockaddr_ib
*sib
;
2918 addr
= cma_src_addr(id_priv
);
2919 port
= htons(bind_list
->port
);
2921 switch (addr
->sa_family
) {
2923 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
2926 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
2929 sib
= (struct sockaddr_ib
*) addr
;
2930 sid
= be64_to_cpu(sib
->sib_sid
);
2931 mask
= be64_to_cpu(sib
->sib_sid_mask
);
2932 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
2933 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
2936 id_priv
->bind_list
= bind_list
;
2937 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
2940 static int cma_alloc_port(enum rdma_port_space ps
,
2941 struct rdma_id_private
*id_priv
, unsigned short snum
)
2943 struct rdma_bind_list
*bind_list
;
2946 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
2950 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
2956 bind_list
->port
= (unsigned short)ret
;
2957 cma_bind_port(bind_list
, id_priv
);
2961 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
2964 static int cma_alloc_any_port(enum rdma_port_space ps
,
2965 struct rdma_id_private
*id_priv
)
2967 static unsigned int last_used_port
;
2968 int low
, high
, remaining
;
2970 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2972 inet_get_local_port_range(net
, &low
, &high
);
2973 remaining
= (high
- low
) + 1;
2974 rover
= prandom_u32() % remaining
+ low
;
2976 if (last_used_port
!= rover
&&
2977 !cma_ps_find(net
, ps
, (unsigned short)rover
)) {
2978 int ret
= cma_alloc_port(ps
, id_priv
, rover
);
2980 * Remember previously used port number in order to avoid
2981 * re-using same port immediately after it is closed.
2984 last_used_port
= rover
;
2985 if (ret
!= -EADDRNOTAVAIL
)
2990 if ((rover
< low
) || (rover
> high
))
2994 return -EADDRNOTAVAIL
;
2998 * Check that the requested port is available. This is called when trying to
2999 * bind to a specific port, or when trying to listen on a bound port. In
3000 * the latter case, the provided id_priv may already be on the bind_list, but
3001 * we still need to check that it's okay to start listening.
3003 static int cma_check_port(struct rdma_bind_list
*bind_list
,
3004 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
3006 struct rdma_id_private
*cur_id
;
3007 struct sockaddr
*addr
, *cur_addr
;
3009 addr
= cma_src_addr(id_priv
);
3010 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3011 if (id_priv
== cur_id
)
3014 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
3018 cur_addr
= cma_src_addr(cur_id
);
3019 if (id_priv
->afonly
&& cur_id
->afonly
&&
3020 (addr
->sa_family
!= cur_addr
->sa_family
))
3023 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
3024 return -EADDRNOTAVAIL
;
3026 if (!cma_addr_cmp(addr
, cur_addr
))
3032 static int cma_use_port(enum rdma_port_space ps
,
3033 struct rdma_id_private
*id_priv
)
3035 struct rdma_bind_list
*bind_list
;
3036 unsigned short snum
;
3039 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
3040 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
3043 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
3045 ret
= cma_alloc_port(ps
, id_priv
, snum
);
3047 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
3049 cma_bind_port(bind_list
, id_priv
);
3054 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
3056 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
3060 if (bind_list
->owners
.first
->next
)
3061 ret
= cma_check_port(bind_list
, id_priv
, 0);
3062 mutex_unlock(&lock
);
3066 static enum rdma_port_space
cma_select_inet_ps(
3067 struct rdma_id_private
*id_priv
)
3069 switch (id_priv
->id
.ps
) {
3074 return id_priv
->id
.ps
;
3081 static enum rdma_port_space
cma_select_ib_ps(struct rdma_id_private
*id_priv
)
3083 enum rdma_port_space ps
= 0;
3084 struct sockaddr_ib
*sib
;
3085 u64 sid_ps
, mask
, sid
;
3087 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
3088 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
3089 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
3091 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
3092 sid_ps
= RDMA_IB_IP_PS_IB
;
3094 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
3095 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
3096 sid_ps
= RDMA_IB_IP_PS_TCP
;
3098 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
3099 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
3100 sid_ps
= RDMA_IB_IP_PS_UDP
;
3105 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
3106 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
3107 be64_to_cpu(sib
->sib_sid_mask
));
3112 static int cma_get_port(struct rdma_id_private
*id_priv
)
3114 enum rdma_port_space ps
;
3117 if (cma_family(id_priv
) != AF_IB
)
3118 ps
= cma_select_inet_ps(id_priv
);
3120 ps
= cma_select_ib_ps(id_priv
);
3122 return -EPROTONOSUPPORT
;
3125 if (cma_any_port(cma_src_addr(id_priv
)))
3126 ret
= cma_alloc_any_port(ps
, id_priv
);
3128 ret
= cma_use_port(ps
, id_priv
);
3129 mutex_unlock(&lock
);
3134 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
3135 struct sockaddr
*addr
)
3137 #if IS_ENABLED(CONFIG_IPV6)
3138 struct sockaddr_in6
*sin6
;
3140 if (addr
->sa_family
!= AF_INET6
)
3143 sin6
= (struct sockaddr_in6
*) addr
;
3145 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
3148 if (!sin6
->sin6_scope_id
)
3151 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
3156 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
3158 struct rdma_id_private
*id_priv
;
3161 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3162 if (id_priv
->state
== RDMA_CM_IDLE
) {
3163 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
3164 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
3169 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
3172 if (id_priv
->reuseaddr
) {
3173 ret
= cma_bind_listen(id_priv
);
3178 id_priv
->backlog
= backlog
;
3180 if (rdma_cap_ib_cm(id
->device
, 1)) {
3181 ret
= cma_ib_listen(id_priv
);
3184 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
3185 ret
= cma_iw_listen(id_priv
, backlog
);
3193 cma_listen_on_all(id_priv
);
3197 id_priv
->backlog
= 0;
3198 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
3201 EXPORT_SYMBOL(rdma_listen
);
3203 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3205 struct rdma_id_private
*id_priv
;
3208 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
3209 addr
->sa_family
!= AF_IB
)
3210 return -EAFNOSUPPORT
;
3212 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3213 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
3216 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
3220 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
3221 if (!cma_any_addr(addr
)) {
3222 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
3226 ret
= cma_acquire_dev(id_priv
, NULL
);
3231 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
3232 if (addr
->sa_family
== AF_INET
)
3233 id_priv
->afonly
= 1;
3234 #if IS_ENABLED(CONFIG_IPV6)
3235 else if (addr
->sa_family
== AF_INET6
) {
3236 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3238 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3242 ret
= cma_get_port(id_priv
);
3248 if (id_priv
->cma_dev
)
3249 cma_release_dev(id_priv
);
3251 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3254 EXPORT_SYMBOL(rdma_bind_addr
);
3256 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3258 struct cma_hdr
*cma_hdr
;
3261 cma_hdr
->cma_version
= CMA_VERSION
;
3262 if (cma_family(id_priv
) == AF_INET
) {
3263 struct sockaddr_in
*src4
, *dst4
;
3265 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3266 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3268 cma_set_ip_ver(cma_hdr
, 4);
3269 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3270 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3271 cma_hdr
->port
= src4
->sin_port
;
3272 } else if (cma_family(id_priv
) == AF_INET6
) {
3273 struct sockaddr_in6
*src6
, *dst6
;
3275 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3276 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3278 cma_set_ip_ver(cma_hdr
, 6);
3279 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3280 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3281 cma_hdr
->port
= src6
->sin6_port
;
3286 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3287 struct ib_cm_event
*ib_event
)
3289 struct rdma_id_private
*id_priv
= cm_id
->context
;
3290 struct rdma_cm_event event
;
3291 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
3294 mutex_lock(&id_priv
->handler_mutex
);
3295 if (id_priv
->state
!= RDMA_CM_CONNECT
)
3298 memset(&event
, 0, sizeof event
);
3299 switch (ib_event
->event
) {
3300 case IB_CM_SIDR_REQ_ERROR
:
3301 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3302 event
.status
= -ETIMEDOUT
;
3304 case IB_CM_SIDR_REP_RECEIVED
:
3305 event
.param
.ud
.private_data
= ib_event
->private_data
;
3306 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3307 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3308 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3309 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3312 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3314 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3318 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
3319 id_priv
->id
.route
.path_rec
,
3320 &event
.param
.ud
.ah_attr
);
3321 event
.param
.ud
.qp_num
= rep
->qpn
;
3322 event
.param
.ud
.qkey
= rep
->qkey
;
3323 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3327 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3332 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3334 /* Destroy the CM ID by returning a non-zero value. */
3335 id_priv
->cm_id
.ib
= NULL
;
3336 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3337 mutex_unlock(&id_priv
->handler_mutex
);
3338 rdma_destroy_id(&id_priv
->id
);
3342 mutex_unlock(&id_priv
->handler_mutex
);
3346 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3347 struct rdma_conn_param
*conn_param
)
3349 struct ib_cm_sidr_req_param req
;
3350 struct ib_cm_id
*id
;
3354 memset(&req
, 0, sizeof req
);
3355 offset
= cma_user_data_offset(id_priv
);
3356 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3357 if (req
.private_data_len
< conn_param
->private_data_len
)
3360 if (req
.private_data_len
) {
3361 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3365 private_data
= NULL
;
3368 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3369 memcpy(private_data
+ offset
, conn_param
->private_data
,
3370 conn_param
->private_data_len
);
3373 ret
= cma_format_hdr(private_data
, id_priv
);
3376 req
.private_data
= private_data
;
3379 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3385 id_priv
->cm_id
.ib
= id
;
3387 req
.path
= id_priv
->id
.route
.path_rec
;
3388 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3389 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3390 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3392 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3394 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3395 id_priv
->cm_id
.ib
= NULL
;
3398 kfree(private_data
);
3402 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3403 struct rdma_conn_param
*conn_param
)
3405 struct ib_cm_req_param req
;
3406 struct rdma_route
*route
;
3408 struct ib_cm_id
*id
;
3411 memset(&req
, 0, sizeof req
);
3412 offset
= cma_user_data_offset(id_priv
);
3413 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3414 if (req
.private_data_len
< conn_param
->private_data_len
)
3417 if (req
.private_data_len
) {
3418 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3422 private_data
= NULL
;
3425 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3426 memcpy(private_data
+ offset
, conn_param
->private_data
,
3427 conn_param
->private_data_len
);
3429 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3434 id_priv
->cm_id
.ib
= id
;
3436 route
= &id_priv
->id
.route
;
3438 ret
= cma_format_hdr(private_data
, id_priv
);
3441 req
.private_data
= private_data
;
3444 req
.primary_path
= &route
->path_rec
[0];
3445 if (route
->num_paths
== 2)
3446 req
.alternate_path
= &route
->path_rec
[1];
3448 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3449 req
.qp_num
= id_priv
->qp_num
;
3450 req
.qp_type
= id_priv
->id
.qp_type
;
3451 req
.starting_psn
= id_priv
->seq_num
;
3452 req
.responder_resources
= conn_param
->responder_resources
;
3453 req
.initiator_depth
= conn_param
->initiator_depth
;
3454 req
.flow_control
= conn_param
->flow_control
;
3455 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
3456 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3457 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3458 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3459 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3460 req
.srq
= id_priv
->srq
? 1 : 0;
3462 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
3464 if (ret
&& !IS_ERR(id
)) {
3465 ib_destroy_cm_id(id
);
3466 id_priv
->cm_id
.ib
= NULL
;
3469 kfree(private_data
);
3473 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
3474 struct rdma_conn_param
*conn_param
)
3476 struct iw_cm_id
*cm_id
;
3478 struct iw_cm_conn_param iw_param
;
3480 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
3482 return PTR_ERR(cm_id
);
3484 cm_id
->tos
= id_priv
->tos
;
3485 id_priv
->cm_id
.iw
= cm_id
;
3487 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
3488 rdma_addr_size(cma_src_addr(id_priv
)));
3489 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
3490 rdma_addr_size(cma_dst_addr(id_priv
)));
3492 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3497 iw_param
.ord
= conn_param
->initiator_depth
;
3498 iw_param
.ird
= conn_param
->responder_resources
;
3499 iw_param
.private_data
= conn_param
->private_data
;
3500 iw_param
.private_data_len
= conn_param
->private_data_len
;
3501 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
3503 memset(&iw_param
, 0, sizeof iw_param
);
3504 iw_param
.qpn
= id_priv
->qp_num
;
3506 ret
= iw_cm_connect(cm_id
, &iw_param
);
3509 iw_destroy_cm_id(cm_id
);
3510 id_priv
->cm_id
.iw
= NULL
;
3515 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3517 struct rdma_id_private
*id_priv
;
3520 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3521 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
3525 id_priv
->qp_num
= conn_param
->qp_num
;
3526 id_priv
->srq
= conn_param
->srq
;
3529 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3530 if (id
->qp_type
== IB_QPT_UD
)
3531 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
3533 ret
= cma_connect_ib(id_priv
, conn_param
);
3534 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3535 ret
= cma_connect_iw(id_priv
, conn_param
);
3543 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
3546 EXPORT_SYMBOL(rdma_connect
);
3548 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
3549 struct rdma_conn_param
*conn_param
)
3551 struct ib_cm_rep_param rep
;
3554 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3558 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
3562 memset(&rep
, 0, sizeof rep
);
3563 rep
.qp_num
= id_priv
->qp_num
;
3564 rep
.starting_psn
= id_priv
->seq_num
;
3565 rep
.private_data
= conn_param
->private_data
;
3566 rep
.private_data_len
= conn_param
->private_data_len
;
3567 rep
.responder_resources
= conn_param
->responder_resources
;
3568 rep
.initiator_depth
= conn_param
->initiator_depth
;
3569 rep
.failover_accepted
= 0;
3570 rep
.flow_control
= conn_param
->flow_control
;
3571 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3572 rep
.srq
= id_priv
->srq
? 1 : 0;
3574 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
3579 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
3580 struct rdma_conn_param
*conn_param
)
3582 struct iw_cm_conn_param iw_param
;
3585 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3589 iw_param
.ord
= conn_param
->initiator_depth
;
3590 iw_param
.ird
= conn_param
->responder_resources
;
3591 iw_param
.private_data
= conn_param
->private_data
;
3592 iw_param
.private_data_len
= conn_param
->private_data_len
;
3593 if (id_priv
->id
.qp
) {
3594 iw_param
.qpn
= id_priv
->qp_num
;
3596 iw_param
.qpn
= conn_param
->qp_num
;
3598 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
3601 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
3602 enum ib_cm_sidr_status status
, u32 qkey
,
3603 const void *private_data
, int private_data_len
)
3605 struct ib_cm_sidr_rep_param rep
;
3608 memset(&rep
, 0, sizeof rep
);
3609 rep
.status
= status
;
3610 if (status
== IB_SIDR_SUCCESS
) {
3611 ret
= cma_set_qkey(id_priv
, qkey
);
3614 rep
.qp_num
= id_priv
->qp_num
;
3615 rep
.qkey
= id_priv
->qkey
;
3617 rep
.private_data
= private_data
;
3618 rep
.private_data_len
= private_data_len
;
3620 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
3623 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3625 struct rdma_id_private
*id_priv
;
3628 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3630 id_priv
->owner
= task_pid_nr(current
);
3632 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
3635 if (!id
->qp
&& conn_param
) {
3636 id_priv
->qp_num
= conn_param
->qp_num
;
3637 id_priv
->srq
= conn_param
->srq
;
3640 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3641 if (id
->qp_type
== IB_QPT_UD
) {
3643 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3645 conn_param
->private_data
,
3646 conn_param
->private_data_len
);
3648 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3652 ret
= cma_accept_ib(id_priv
, conn_param
);
3654 ret
= cma_rep_recv(id_priv
);
3656 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3657 ret
= cma_accept_iw(id_priv
, conn_param
);
3666 cma_modify_qp_err(id_priv
);
3667 rdma_reject(id
, NULL
, 0);
3670 EXPORT_SYMBOL(rdma_accept
);
3672 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
3674 struct rdma_id_private
*id_priv
;
3677 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3678 if (!id_priv
->cm_id
.ib
)
3681 switch (id
->device
->node_type
) {
3682 case RDMA_NODE_IB_CA
:
3683 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
3691 EXPORT_SYMBOL(rdma_notify
);
3693 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
3694 u8 private_data_len
)
3696 struct rdma_id_private
*id_priv
;
3699 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3700 if (!id_priv
->cm_id
.ib
)
3703 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3704 if (id
->qp_type
== IB_QPT_UD
)
3705 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
3706 private_data
, private_data_len
);
3708 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
3709 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
3710 0, private_data
, private_data_len
);
3711 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3712 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
3713 private_data
, private_data_len
);
3719 EXPORT_SYMBOL(rdma_reject
);
3721 int rdma_disconnect(struct rdma_cm_id
*id
)
3723 struct rdma_id_private
*id_priv
;
3726 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3727 if (!id_priv
->cm_id
.ib
)
3730 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3731 ret
= cma_modify_qp_err(id_priv
);
3734 /* Initiate or respond to a disconnect. */
3735 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
3736 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
3737 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3738 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
3745 EXPORT_SYMBOL(rdma_disconnect
);
3747 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
3749 struct rdma_id_private
*id_priv
;
3750 struct cma_multicast
*mc
= multicast
->context
;
3751 struct rdma_cm_event event
;
3754 id_priv
= mc
->id_priv
;
3755 mutex_lock(&id_priv
->handler_mutex
);
3756 if (id_priv
->state
!= RDMA_CM_ADDR_BOUND
&&
3757 id_priv
->state
!= RDMA_CM_ADDR_RESOLVED
)
3761 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
3762 mutex_lock(&id_priv
->qp_mutex
);
3763 if (!status
&& id_priv
->id
.qp
)
3764 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
3765 be16_to_cpu(multicast
->rec
.mlid
));
3766 mutex_unlock(&id_priv
->qp_mutex
);
3768 memset(&event
, 0, sizeof event
);
3769 event
.status
= status
;
3770 event
.param
.ud
.private_data
= mc
->context
;
3772 struct rdma_dev_addr
*dev_addr
=
3773 &id_priv
->id
.route
.addr
.dev_addr
;
3774 struct net_device
*ndev
=
3775 dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
3776 enum ib_gid_type gid_type
=
3777 id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
3778 rdma_start_port(id_priv
->cma_dev
->device
)];
3780 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
3781 ib_init_ah_from_mcmember(id_priv
->id
.device
,
3782 id_priv
->id
.port_num
, &multicast
->rec
,
3784 &event
.param
.ud
.ah_attr
);
3785 event
.param
.ud
.qp_num
= 0xFFFFFF;
3786 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
3790 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3792 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3794 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3795 mutex_unlock(&id_priv
->handler_mutex
);
3796 rdma_destroy_id(&id_priv
->id
);
3801 mutex_unlock(&id_priv
->handler_mutex
);
3805 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
3806 struct sockaddr
*addr
, union ib_gid
*mgid
)
3808 unsigned char mc_map
[MAX_ADDR_LEN
];
3809 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3810 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
3811 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
3813 if (cma_any_addr(addr
)) {
3814 memset(mgid
, 0, sizeof *mgid
);
3815 } else if ((addr
->sa_family
== AF_INET6
) &&
3816 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
3818 /* IPv6 address is an SA assigned MGID. */
3819 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3820 } else if (addr
->sa_family
== AF_IB
) {
3821 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
3822 } else if ((addr
->sa_family
== AF_INET6
)) {
3823 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
3824 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3825 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3826 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3828 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
3829 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3830 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3831 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3835 static void cma_query_sa_classport_info_cb(int status
,
3836 struct ib_class_port_info
*rec
,
3839 struct class_port_info_context
*cb_ctx
= context
;
3843 if (status
|| !rec
) {
3844 pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3845 cb_ctx
->device
->name
, cb_ctx
->port_num
, status
);
3849 memcpy(cb_ctx
->class_port_info
, rec
, sizeof(struct ib_class_port_info
));
3852 complete(&cb_ctx
->done
);
3855 static int cma_query_sa_classport_info(struct ib_device
*device
, u8 port_num
,
3856 struct ib_class_port_info
*class_port_info
)
3858 struct class_port_info_context
*cb_ctx
;
3861 cb_ctx
= kmalloc(sizeof(*cb_ctx
), GFP_KERNEL
);
3865 cb_ctx
->device
= device
;
3866 cb_ctx
->class_port_info
= class_port_info
;
3867 cb_ctx
->port_num
= port_num
;
3868 init_completion(&cb_ctx
->done
);
3870 ret
= ib_sa_classport_info_rec_query(&sa_client
, device
, port_num
,
3871 CMA_QUERY_CLASSPORT_INFO_TIMEOUT
,
3872 GFP_KERNEL
, cma_query_sa_classport_info_cb
,
3873 cb_ctx
, &cb_ctx
->sa_query
);
3875 pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3876 device
->name
, port_num
, ret
);
3880 wait_for_completion(&cb_ctx
->done
);
3887 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
3888 struct cma_multicast
*mc
)
3890 struct ib_sa_mcmember_rec rec
;
3891 struct ib_class_port_info class_port_info
;
3892 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3893 ib_sa_comp_mask comp_mask
;
3896 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
3897 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
3902 ret
= cma_set_qkey(id_priv
, 0);
3906 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
3907 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
3908 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
3909 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
3910 rec
.join_state
= mc
->join_state
;
3912 if (rec
.join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
)) {
3913 ret
= cma_query_sa_classport_info(id_priv
->id
.device
,
3914 id_priv
->id
.port_num
,
3920 if (!(ib_get_cpi_capmask2(&class_port_info
) &
3921 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT
)) {
3922 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3923 "RDMA CM: SM doesn't support Send Only Full Member option\n",
3924 id_priv
->id
.device
->name
, id_priv
->id
.port_num
);
3929 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
3930 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
3931 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
3932 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
3933 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
3935 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
3936 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
3937 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
3938 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
3939 IB_SA_MCMEMBER_REC_MTU
|
3940 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
3942 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
3943 id_priv
->id
.port_num
, &rec
,
3944 comp_mask
, GFP_KERNEL
,
3945 cma_ib_mc_handler
, mc
);
3946 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
3949 static void iboe_mcast_work_handler(struct work_struct
*work
)
3951 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
3952 struct cma_multicast
*mc
= mw
->mc
;
3953 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
3955 mc
->multicast
.ib
->context
= mc
;
3956 cma_ib_mc_handler(0, m
);
3957 kref_put(&mc
->mcref
, release_mc
);
3961 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
)
3963 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
3964 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
3966 if (cma_any_addr(addr
)) {
3967 memset(mgid
, 0, sizeof *mgid
);
3968 } else if (addr
->sa_family
== AF_INET6
) {
3969 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3971 mgid
->raw
[0] = 0xff;
3972 mgid
->raw
[1] = 0x0e;
3981 mgid
->raw
[10] = 0xff;
3982 mgid
->raw
[11] = 0xff;
3983 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
3987 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
3988 struct cma_multicast
*mc
)
3990 struct iboe_mcast_work
*work
;
3991 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3993 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
3994 struct net_device
*ndev
= NULL
;
3995 enum ib_gid_type gid_type
;
3998 send_only
= mc
->join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
);
4000 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
4003 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4007 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
4008 if (!mc
->multicast
.ib
) {
4013 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
);
4015 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
4016 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4017 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
4019 if (dev_addr
->bound_dev_if
)
4020 ndev
= dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
4025 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
4026 mc
->multicast
.ib
->rec
.hop_limit
= 1;
4027 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
4029 gid_type
= id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
4030 rdma_start_port(id_priv
->cma_dev
->device
)];
4031 if (addr
->sa_family
== AF_INET
) {
4032 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) {
4033 mc
->multicast
.ib
->rec
.hop_limit
= IPV6_DEFAULT_HOPLIMIT
;
4035 err
= cma_igmp_send(ndev
, &mc
->multicast
.ib
->rec
.mgid
,
4038 mc
->igmp_joined
= true;
4042 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
)
4046 if (err
|| !mc
->multicast
.ib
->rec
.mtu
) {
4051 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
4052 &mc
->multicast
.ib
->rec
.port_gid
);
4055 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
4056 kref_get(&mc
->mcref
);
4057 queue_work(cma_wq
, &work
->work
);
4062 kfree(mc
->multicast
.ib
);
4068 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
4069 u8 join_state
, void *context
)
4071 struct rdma_id_private
*id_priv
;
4072 struct cma_multicast
*mc
;
4075 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4076 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
4077 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
4080 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
4084 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
4085 mc
->context
= context
;
4086 mc
->id_priv
= id_priv
;
4087 mc
->igmp_joined
= false;
4088 mc
->join_state
= join_state
;
4089 spin_lock(&id_priv
->lock
);
4090 list_add(&mc
->list
, &id_priv
->mc_list
);
4091 spin_unlock(&id_priv
->lock
);
4093 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4094 kref_init(&mc
->mcref
);
4095 ret
= cma_iboe_join_multicast(id_priv
, mc
);
4096 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
))
4097 ret
= cma_join_ib_multicast(id_priv
, mc
);
4102 spin_lock_irq(&id_priv
->lock
);
4103 list_del(&mc
->list
);
4104 spin_unlock_irq(&id_priv
->lock
);
4109 EXPORT_SYMBOL(rdma_join_multicast
);
4111 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
4113 struct rdma_id_private
*id_priv
;
4114 struct cma_multicast
*mc
;
4116 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4117 spin_lock_irq(&id_priv
->lock
);
4118 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
4119 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
4120 list_del(&mc
->list
);
4121 spin_unlock_irq(&id_priv
->lock
);
4124 ib_detach_mcast(id
->qp
,
4125 &mc
->multicast
.ib
->rec
.mgid
,
4126 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
4128 BUG_ON(id_priv
->cma_dev
->device
!= id
->device
);
4130 if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
4131 ib_sa_free_multicast(mc
->multicast
.ib
);
4133 } else if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4134 if (mc
->igmp_joined
) {
4135 struct rdma_dev_addr
*dev_addr
=
4136 &id
->route
.addr
.dev_addr
;
4137 struct net_device
*ndev
= NULL
;
4139 if (dev_addr
->bound_dev_if
)
4140 ndev
= dev_get_by_index(&init_net
,
4141 dev_addr
->bound_dev_if
);
4144 &mc
->multicast
.ib
->rec
.mgid
,
4148 mc
->igmp_joined
= false;
4150 kref_put(&mc
->mcref
, release_mc
);
4155 spin_unlock_irq(&id_priv
->lock
);
4157 EXPORT_SYMBOL(rdma_leave_multicast
);
4159 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
4161 struct rdma_dev_addr
*dev_addr
;
4162 struct cma_ndev_work
*work
;
4164 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4166 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
4167 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
4168 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
4169 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4170 ndev
->name
, &id_priv
->id
);
4171 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4175 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
4177 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
4178 atomic_inc(&id_priv
->refcount
);
4179 queue_work(cma_wq
, &work
->work
);
4185 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
4188 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
4189 struct cma_device
*cma_dev
;
4190 struct rdma_id_private
*id_priv
;
4191 int ret
= NOTIFY_DONE
;
4193 if (event
!= NETDEV_BONDING_FAILOVER
)
4196 if (!(ndev
->flags
& IFF_MASTER
) || !(ndev
->priv_flags
& IFF_BONDING
))
4200 list_for_each_entry(cma_dev
, &dev_list
, list
)
4201 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4202 ret
= cma_netdev_change(ndev
, id_priv
);
4208 mutex_unlock(&lock
);
4212 static struct notifier_block cma_nb
= {
4213 .notifier_call
= cma_netdev_callback
4216 static void cma_add_one(struct ib_device
*device
)
4218 struct cma_device
*cma_dev
;
4219 struct rdma_id_private
*id_priv
;
4221 unsigned long supported_gids
= 0;
4223 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
4227 cma_dev
->device
= device
;
4228 cma_dev
->default_gid_type
= kcalloc(device
->phys_port_cnt
,
4229 sizeof(*cma_dev
->default_gid_type
),
4231 if (!cma_dev
->default_gid_type
) {
4235 for (i
= rdma_start_port(device
); i
<= rdma_end_port(device
); i
++) {
4236 supported_gids
= roce_gid_type_mask_support(device
, i
);
4237 WARN_ON(!supported_gids
);
4238 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4239 find_first_bit(&supported_gids
, BITS_PER_LONG
);
4242 init_completion(&cma_dev
->comp
);
4243 atomic_set(&cma_dev
->refcount
, 1);
4244 INIT_LIST_HEAD(&cma_dev
->id_list
);
4245 ib_set_client_data(device
, &cma_client
, cma_dev
);
4248 list_add_tail(&cma_dev
->list
, &dev_list
);
4249 list_for_each_entry(id_priv
, &listen_any_list
, list
)
4250 cma_listen_on_dev(id_priv
, cma_dev
);
4251 mutex_unlock(&lock
);
4254 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
4256 struct rdma_cm_event event
;
4257 enum rdma_cm_state state
;
4260 /* Record that we want to remove the device */
4261 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
4262 if (state
== RDMA_CM_DESTROYING
)
4265 cma_cancel_operation(id_priv
, state
);
4266 mutex_lock(&id_priv
->handler_mutex
);
4268 /* Check for destruction from another callback. */
4269 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
4272 memset(&event
, 0, sizeof event
);
4273 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
4274 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
4276 mutex_unlock(&id_priv
->handler_mutex
);
4280 static void cma_process_remove(struct cma_device
*cma_dev
)
4282 struct rdma_id_private
*id_priv
;
4286 while (!list_empty(&cma_dev
->id_list
)) {
4287 id_priv
= list_entry(cma_dev
->id_list
.next
,
4288 struct rdma_id_private
, list
);
4290 list_del(&id_priv
->listen_list
);
4291 list_del_init(&id_priv
->list
);
4292 atomic_inc(&id_priv
->refcount
);
4293 mutex_unlock(&lock
);
4295 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
4296 cma_deref_id(id_priv
);
4298 rdma_destroy_id(&id_priv
->id
);
4302 mutex_unlock(&lock
);
4304 cma_deref_dev(cma_dev
);
4305 wait_for_completion(&cma_dev
->comp
);
4308 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
4310 struct cma_device
*cma_dev
= client_data
;
4316 list_del(&cma_dev
->list
);
4317 mutex_unlock(&lock
);
4319 cma_process_remove(cma_dev
);
4320 kfree(cma_dev
->default_gid_type
);
4324 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4326 struct nlmsghdr
*nlh
;
4327 struct rdma_cm_id_stats
*id_stats
;
4328 struct rdma_id_private
*id_priv
;
4329 struct rdma_cm_id
*id
= NULL
;
4330 struct cma_device
*cma_dev
;
4331 int i_dev
= 0, i_id
= 0;
4334 * We export all of the IDs as a sequence of messages. Each
4335 * ID gets its own netlink message.
4339 list_for_each_entry(cma_dev
, &dev_list
, list
) {
4340 if (i_dev
< cb
->args
[0]) {
4346 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4347 if (i_id
< cb
->args
[1]) {
4352 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
4353 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
4354 RDMA_NL_RDMA_CM_ID_STATS
,
4359 memset(id_stats
, 0, sizeof *id_stats
);
4361 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
4362 id_stats
->port_num
= id
->port_num
;
4363 id_stats
->bound_dev_if
=
4364 id
->route
.addr
.dev_addr
.bound_dev_if
;
4366 if (ibnl_put_attr(skb
, nlh
,
4367 rdma_addr_size(cma_src_addr(id_priv
)),
4368 cma_src_addr(id_priv
),
4369 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
))
4371 if (ibnl_put_attr(skb
, nlh
,
4372 rdma_addr_size(cma_src_addr(id_priv
)),
4373 cma_dst_addr(id_priv
),
4374 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
))
4377 id_stats
->pid
= id_priv
->owner
;
4378 id_stats
->port_space
= id
->ps
;
4379 id_stats
->cm_state
= id_priv
->state
;
4380 id_stats
->qp_num
= id_priv
->qp_num
;
4381 id_stats
->qp_type
= id
->qp_type
;
4391 mutex_unlock(&lock
);
4392 cb
->args
[0] = i_dev
;
4398 static const struct ibnl_client_cbs cma_cb_table
[] = {
4399 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
,
4400 .module
= THIS_MODULE
},
4403 static int cma_init_net(struct net
*net
)
4405 struct cma_pernet
*pernet
= cma_pernet(net
);
4407 idr_init(&pernet
->tcp_ps
);
4408 idr_init(&pernet
->udp_ps
);
4409 idr_init(&pernet
->ipoib_ps
);
4410 idr_init(&pernet
->ib_ps
);
4415 static void cma_exit_net(struct net
*net
)
4417 struct cma_pernet
*pernet
= cma_pernet(net
);
4419 idr_destroy(&pernet
->tcp_ps
);
4420 idr_destroy(&pernet
->udp_ps
);
4421 idr_destroy(&pernet
->ipoib_ps
);
4422 idr_destroy(&pernet
->ib_ps
);
4425 static struct pernet_operations cma_pernet_operations
= {
4426 .init
= cma_init_net
,
4427 .exit
= cma_exit_net
,
4428 .id
= &cma_pernet_id
,
4429 .size
= sizeof(struct cma_pernet
),
4432 static int __init
cma_init(void)
4436 cma_wq
= alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM
);
4440 ret
= register_pernet_subsys(&cma_pernet_operations
);
4444 ib_sa_register_client(&sa_client
);
4445 rdma_addr_register_client(&addr_client
);
4446 register_netdevice_notifier(&cma_nb
);
4448 ret
= ib_register_client(&cma_client
);
4452 if (ibnl_add_client(RDMA_NL_RDMA_CM
, ARRAY_SIZE(cma_cb_table
),
4454 pr_warn("RDMA CMA: failed to add netlink callback\n");
4455 cma_configfs_init();
4460 unregister_netdevice_notifier(&cma_nb
);
4461 rdma_addr_unregister_client(&addr_client
);
4462 ib_sa_unregister_client(&sa_client
);
4464 destroy_workqueue(cma_wq
);
4468 static void __exit
cma_cleanup(void)
4470 cma_configfs_exit();
4471 ibnl_remove_client(RDMA_NL_RDMA_CM
);
4472 ib_unregister_client(&cma_client
);
4473 unregister_netdevice_notifier(&cma_nb
);
4474 rdma_addr_unregister_client(&addr_client
);
4475 ib_sa_unregister_client(&sa_client
);
4476 unregister_pernet_subsys(&cma_pernet_operations
);
4477 destroy_workqueue(cma_wq
);
4480 module_init(cma_init
);
4481 module_exit(cma_cleanup
);