xref: /linux/drivers/infiniband/core/cma.c (revision e84045eab69c625bc0b0bf24d8e05bc65da1eed1)
124f52149SLeon Romanovsky // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2e51060f0SSean Hefty /*
3e51060f0SSean Hefty  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
4e51060f0SSean Hefty  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
524f52149SLeon Romanovsky  * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6e51060f0SSean Hefty  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
7e51060f0SSean Hefty  */
8e51060f0SSean Hefty 
9e51060f0SSean Hefty #include <linux/completion.h>
10e51060f0SSean Hefty #include <linux/in.h>
11e51060f0SSean Hefty #include <linux/in6.h>
12e51060f0SSean Hefty #include <linux/mutex.h>
13e51060f0SSean Hefty #include <linux/random.h>
14bee3c3c9SMoni Shoua #include <linux/igmp.h>
1563826753SMatthew Wilcox #include <linux/xarray.h>
1607ebafbaSTom Tucker #include <linux/inetdevice.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
18e4dd23d7SPaul Gortmaker #include <linux/module.h>
19366cddb4SAmir Vadai #include <net/route.h>
20e51060f0SSean Hefty 
214be74b42SHaggai Eran #include <net/net_namespace.h>
224be74b42SHaggai Eran #include <net/netns/generic.h>
23e51060f0SSean Hefty #include <net/tcp.h>
241f5175adSAleksey Senin #include <net/ipv6.h>
25f887f2acSHaggai Eran #include <net/ip_fib.h>
26f887f2acSHaggai Eran #include <net/ip6_route.h>
27e51060f0SSean Hefty 
28e51060f0SSean Hefty #include <rdma/rdma_cm.h>
29e51060f0SSean Hefty #include <rdma/rdma_cm_ib.h>
30753f618aSNir Muchtar #include <rdma/rdma_netlink.h>
312e2d190cSSean Hefty #include <rdma/ib.h>
32e51060f0SSean Hefty #include <rdma/ib_cache.h>
33e51060f0SSean Hefty #include <rdma/ib_cm.h>
34e51060f0SSean Hefty #include <rdma/ib_sa.h>
3507ebafbaSTom Tucker #include <rdma/iw_cm.h>
36e51060f0SSean Hefty 
37218a773fSMatan Barak #include "core_priv.h"
38a3b641afSSteve Wise #include "cma_priv.h"
39ed999f82SChuck Lever #include "cma_trace.h"
40218a773fSMatan Barak 
41e51060f0SSean Hefty MODULE_AUTHOR("Sean Hefty");
42e51060f0SSean Hefty MODULE_DESCRIPTION("Generic RDMA CM Agent");
43e51060f0SSean Hefty MODULE_LICENSE("Dual BSD/GPL");
44e51060f0SSean Hefty 
45e51060f0SSean Hefty #define CMA_CM_RESPONSE_TIMEOUT 20
46d5bb7599SMichael S. Tsirkin #define CMA_MAX_CM_RETRIES 15
47dcb3f974SSean Hefty #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
483c86aa70SEli Cohen #define CMA_IBOE_PACKET_LIFETIME 18
495ab2d89bSLeon Romanovsky #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
50e51060f0SSean Hefty 
512b1b5b60SSagi Grimberg static const char * const cma_events[] = {
522b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_ADDR_RESOLVED]	 = "address resolved",
532b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_ADDR_ERROR]	 = "address error",
542b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_ROUTE_RESOLVED]	 = "route resolved ",
552b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_ROUTE_ERROR]	 = "route error",
562b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_CONNECT_REQUEST]	 = "connect request",
572b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
582b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_CONNECT_ERROR]	 = "connect error",
592b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_UNREACHABLE]	 = "unreachable",
602b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_REJECTED]	 = "rejected",
612b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_ESTABLISHED]	 = "established",
622b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_DISCONNECTED]	 = "disconnected",
632b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_DEVICE_REMOVAL]	 = "device removal",
642b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_MULTICAST_JOIN]	 = "multicast join",
652b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_MULTICAST_ERROR]	 = "multicast error",
662b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_ADDR_CHANGE]	 = "address change",
672b1b5b60SSagi Grimberg 	[RDMA_CM_EVENT_TIMEWAIT_EXIT]	 = "timewait exit",
682b1b5b60SSagi Grimberg };
692b1b5b60SSagi Grimberg 
70b5de0c60SJason Gunthorpe static void cma_set_mgid(struct rdma_id_private *id_priv, struct sockaddr *addr,
71b5de0c60SJason Gunthorpe 			 union ib_gid *mgid);
72b5de0c60SJason Gunthorpe 
73db7489e0SBart Van Assche const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
742b1b5b60SSagi Grimberg {
752b1b5b60SSagi Grimberg 	size_t index = event;
762b1b5b60SSagi Grimberg 
772b1b5b60SSagi Grimberg 	return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
782b1b5b60SSagi Grimberg 			cma_events[index] : "unrecognized event";
792b1b5b60SSagi Grimberg }
802b1b5b60SSagi Grimberg EXPORT_SYMBOL(rdma_event_msg);
812b1b5b60SSagi Grimberg 
8277a5db13SSteve Wise const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
8377a5db13SSteve Wise 						int reason)
8477a5db13SSteve Wise {
8577a5db13SSteve Wise 	if (rdma_ib_or_roce(id->device, id->port_num))
8677a5db13SSteve Wise 		return ibcm_reject_msg(reason);
8777a5db13SSteve Wise 
8877a5db13SSteve Wise 	if (rdma_protocol_iwarp(id->device, id->port_num))
8977a5db13SSteve Wise 		return iwcm_reject_msg(reason);
9077a5db13SSteve Wise 
9177a5db13SSteve Wise 	WARN_ON_ONCE(1);
9277a5db13SSteve Wise 	return "unrecognized transport";
9377a5db13SSteve Wise }
9477a5db13SSteve Wise EXPORT_SYMBOL(rdma_reject_msg);
9577a5db13SSteve Wise 
96dd302ee4SLeon Romanovsky /**
97dd302ee4SLeon Romanovsky  * rdma_is_consumer_reject - return true if the consumer rejected the connect
98dd302ee4SLeon Romanovsky  *                           request.
99dd302ee4SLeon Romanovsky  * @id: Communication identifier that received the REJECT event.
100dd302ee4SLeon Romanovsky  * @reason: Value returned in the REJECT event status field.
101dd302ee4SLeon Romanovsky  */
102dd302ee4SLeon Romanovsky static bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
1035042a73dSSteve Wise {
1045042a73dSSteve Wise 	if (rdma_ib_or_roce(id->device, id->port_num))
1055042a73dSSteve Wise 		return reason == IB_CM_REJ_CONSUMER_DEFINED;
1065042a73dSSteve Wise 
1075042a73dSSteve Wise 	if (rdma_protocol_iwarp(id->device, id->port_num))
1085042a73dSSteve Wise 		return reason == -ECONNREFUSED;
1095042a73dSSteve Wise 
1105042a73dSSteve Wise 	WARN_ON_ONCE(1);
1115042a73dSSteve Wise 	return false;
1125042a73dSSteve Wise }
1135042a73dSSteve Wise 
1145f244104SSteve Wise const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
1155f244104SSteve Wise 				      struct rdma_cm_event *ev, u8 *data_len)
1165f244104SSteve Wise {
1175f244104SSteve Wise 	const void *p;
1185f244104SSteve Wise 
1195f244104SSteve Wise 	if (rdma_is_consumer_reject(id, ev->status)) {
1205f244104SSteve Wise 		*data_len = ev->param.conn.private_data_len;
1215f244104SSteve Wise 		p = ev->param.conn.private_data;
1225f244104SSteve Wise 	} else {
1235f244104SSteve Wise 		*data_len = 0;
1245f244104SSteve Wise 		p = NULL;
1255f244104SSteve Wise 	}
1265f244104SSteve Wise 	return p;
1275f244104SSteve Wise }
1285f244104SSteve Wise EXPORT_SYMBOL(rdma_consumer_reject_data);
1295f244104SSteve Wise 
130fbdb0a91SSteve Wise /**
131fbdb0a91SSteve Wise  * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
132fbdb0a91SSteve Wise  * @id: Communication Identifier
133fbdb0a91SSteve Wise  */
134fbdb0a91SSteve Wise struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
135fbdb0a91SSteve Wise {
136fbdb0a91SSteve Wise 	struct rdma_id_private *id_priv;
137fbdb0a91SSteve Wise 
138fbdb0a91SSteve Wise 	id_priv = container_of(id, struct rdma_id_private, id);
139fbdb0a91SSteve Wise 	if (id->device->node_type == RDMA_NODE_RNIC)
140fbdb0a91SSteve Wise 		return id_priv->cm_id.iw;
141fbdb0a91SSteve Wise 	return NULL;
142fbdb0a91SSteve Wise }
143fbdb0a91SSteve Wise EXPORT_SYMBOL(rdma_iw_cm_id);
144fbdb0a91SSteve Wise 
145fbdb0a91SSteve Wise /**
146fbdb0a91SSteve Wise  * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
147fbdb0a91SSteve Wise  * @res: rdma resource tracking entry pointer
148fbdb0a91SSteve Wise  */
149fbdb0a91SSteve Wise struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
150fbdb0a91SSteve Wise {
151fbdb0a91SSteve Wise 	struct rdma_id_private *id_priv =
152fbdb0a91SSteve Wise 		container_of(res, struct rdma_id_private, res);
153fbdb0a91SSteve Wise 
154fbdb0a91SSteve Wise 	return &id_priv->id;
155fbdb0a91SSteve Wise }
156fbdb0a91SSteve Wise EXPORT_SYMBOL(rdma_res_to_id);
157fbdb0a91SSteve Wise 
15811a0ae4cSJason Gunthorpe static int cma_add_one(struct ib_device *device);
1597c1eb45aSHaggai Eran static void cma_remove_one(struct ib_device *device, void *client_data);
160e51060f0SSean Hefty 
161e51060f0SSean Hefty static struct ib_client cma_client = {
162e51060f0SSean Hefty 	.name   = "cma",
163e51060f0SSean Hefty 	.add    = cma_add_one,
164e51060f0SSean Hefty 	.remove = cma_remove_one
165e51060f0SSean Hefty };
166e51060f0SSean Hefty 
167c1a0b23bSMichael S. Tsirkin static struct ib_sa_client sa_client;
168e51060f0SSean Hefty static LIST_HEAD(dev_list);
169e51060f0SSean Hefty static LIST_HEAD(listen_any_list);
170e51060f0SSean Hefty static DEFINE_MUTEX(lock);
171e51060f0SSean Hefty static struct workqueue_struct *cma_wq;
172c7d03a00SAlexey Dobriyan static unsigned int cma_pernet_id;
173e51060f0SSean Hefty 
1744be74b42SHaggai Eran struct cma_pernet {
17563826753SMatthew Wilcox 	struct xarray tcp_ps;
17663826753SMatthew Wilcox 	struct xarray udp_ps;
17763826753SMatthew Wilcox 	struct xarray ipoib_ps;
17863826753SMatthew Wilcox 	struct xarray ib_ps;
1794be74b42SHaggai Eran };
1804be74b42SHaggai Eran 
1814be74b42SHaggai Eran static struct cma_pernet *cma_pernet(struct net *net)
182aac978e1SHaggai Eran {
1834be74b42SHaggai Eran 	return net_generic(net, cma_pernet_id);
1844be74b42SHaggai Eran }
1854be74b42SHaggai Eran 
18663826753SMatthew Wilcox static
18763826753SMatthew Wilcox struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
1884be74b42SHaggai Eran {
1894be74b42SHaggai Eran 	struct cma_pernet *pernet = cma_pernet(net);
1904be74b42SHaggai Eran 
191aac978e1SHaggai Eran 	switch (ps) {
192aac978e1SHaggai Eran 	case RDMA_PS_TCP:
1934be74b42SHaggai Eran 		return &pernet->tcp_ps;
194aac978e1SHaggai Eran 	case RDMA_PS_UDP:
1954be74b42SHaggai Eran 		return &pernet->udp_ps;
196aac978e1SHaggai Eran 	case RDMA_PS_IPOIB:
1974be74b42SHaggai Eran 		return &pernet->ipoib_ps;
198aac978e1SHaggai Eran 	case RDMA_PS_IB:
1994be74b42SHaggai Eran 		return &pernet->ib_ps;
200aac978e1SHaggai Eran 	default:
201aac978e1SHaggai Eran 		return NULL;
202aac978e1SHaggai Eran 	}
203aac978e1SHaggai Eran }
204aac978e1SHaggai Eran 
205e51060f0SSean Hefty struct cma_device {
206e51060f0SSean Hefty 	struct list_head	list;
207e51060f0SSean Hefty 	struct ib_device	*device;
208e51060f0SSean Hefty 	struct completion	comp;
209be439912SParav Pandit 	refcount_t refcount;
210e51060f0SSean Hefty 	struct list_head	id_list;
211045959dbSMatan Barak 	enum ib_gid_type	*default_gid_type;
21289052d78SMajd Dibbiny 	u8			*default_roce_tos;
213e51060f0SSean Hefty };
214e51060f0SSean Hefty 
215e51060f0SSean Hefty struct rdma_bind_list {
2162253fc0cSSteve Wise 	enum rdma_ucm_port_space ps;
217e51060f0SSean Hefty 	struct hlist_head	owners;
218e51060f0SSean Hefty 	unsigned short		port;
219e51060f0SSean Hefty };
220e51060f0SSean Hefty 
2212253fc0cSSteve Wise static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
222aac978e1SHaggai Eran 			struct rdma_bind_list *bind_list, int snum)
223aac978e1SHaggai Eran {
22463826753SMatthew Wilcox 	struct xarray *xa = cma_pernet_xa(net, ps);
225aac978e1SHaggai Eran 
22663826753SMatthew Wilcox 	return xa_insert(xa, snum, bind_list, GFP_KERNEL);
227aac978e1SHaggai Eran }
228aac978e1SHaggai Eran 
2294be74b42SHaggai Eran static struct rdma_bind_list *cma_ps_find(struct net *net,
2302253fc0cSSteve Wise 					  enum rdma_ucm_port_space ps, int snum)
231aac978e1SHaggai Eran {
23263826753SMatthew Wilcox 	struct xarray *xa = cma_pernet_xa(net, ps);
233aac978e1SHaggai Eran 
23463826753SMatthew Wilcox 	return xa_load(xa, snum);
235aac978e1SHaggai Eran }
236aac978e1SHaggai Eran 
2372253fc0cSSteve Wise static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
2382253fc0cSSteve Wise 			  int snum)
239aac978e1SHaggai Eran {
24063826753SMatthew Wilcox 	struct xarray *xa = cma_pernet_xa(net, ps);
241aac978e1SHaggai Eran 
24263826753SMatthew Wilcox 	xa_erase(xa, snum);
243aac978e1SHaggai Eran }
244aac978e1SHaggai Eran 
24568602120SSean Hefty enum {
24668602120SSean Hefty 	CMA_OPTION_AFONLY,
24768602120SSean Hefty };
24868602120SSean Hefty 
2495ff8c8faSParav Pandit void cma_dev_get(struct cma_device *cma_dev)
250218a773fSMatan Barak {
251be439912SParav Pandit 	refcount_inc(&cma_dev->refcount);
252218a773fSMatan Barak }
253218a773fSMatan Barak 
2545ff8c8faSParav Pandit void cma_dev_put(struct cma_device *cma_dev)
2555ff8c8faSParav Pandit {
256be439912SParav Pandit 	if (refcount_dec_and_test(&cma_dev->refcount))
2575ff8c8faSParav Pandit 		complete(&cma_dev->comp);
2585ff8c8faSParav Pandit }
2595ff8c8faSParav Pandit 
260045959dbSMatan Barak struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter	filter,
261045959dbSMatan Barak 					     void		*cookie)
262045959dbSMatan Barak {
263045959dbSMatan Barak 	struct cma_device *cma_dev;
264045959dbSMatan Barak 	struct cma_device *found_cma_dev = NULL;
265045959dbSMatan Barak 
266045959dbSMatan Barak 	mutex_lock(&lock);
267045959dbSMatan Barak 
268045959dbSMatan Barak 	list_for_each_entry(cma_dev, &dev_list, list)
269045959dbSMatan Barak 		if (filter(cma_dev->device, cookie)) {
270045959dbSMatan Barak 			found_cma_dev = cma_dev;
271045959dbSMatan Barak 			break;
272045959dbSMatan Barak 		}
273045959dbSMatan Barak 
274045959dbSMatan Barak 	if (found_cma_dev)
2755ff8c8faSParav Pandit 		cma_dev_get(found_cma_dev);
276045959dbSMatan Barak 	mutex_unlock(&lock);
277045959dbSMatan Barak 	return found_cma_dev;
278045959dbSMatan Barak }
279045959dbSMatan Barak 
280045959dbSMatan Barak int cma_get_default_gid_type(struct cma_device *cma_dev,
2811fb7f897SMark Bloch 			     u32 port)
282045959dbSMatan Barak {
28324dc831bSYuval Shaia 	if (!rdma_is_port_valid(cma_dev->device, port))
284045959dbSMatan Barak 		return -EINVAL;
285045959dbSMatan Barak 
286045959dbSMatan Barak 	return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
287045959dbSMatan Barak }
288045959dbSMatan Barak 
289045959dbSMatan Barak int cma_set_default_gid_type(struct cma_device *cma_dev,
2901fb7f897SMark Bloch 			     u32 port,
291045959dbSMatan Barak 			     enum ib_gid_type default_gid_type)
292045959dbSMatan Barak {
293045959dbSMatan Barak 	unsigned long supported_gids;
294045959dbSMatan Barak 
29524dc831bSYuval Shaia 	if (!rdma_is_port_valid(cma_dev->device, port))
296045959dbSMatan Barak 		return -EINVAL;
297045959dbSMatan Barak 
2981c15b4f2SAvihai Horon 	if (default_gid_type == IB_GID_TYPE_IB &&
2991c15b4f2SAvihai Horon 	    rdma_protocol_roce_eth_encap(cma_dev->device, port))
3001c15b4f2SAvihai Horon 		default_gid_type = IB_GID_TYPE_ROCE;
3011c15b4f2SAvihai Horon 
302045959dbSMatan Barak 	supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
303045959dbSMatan Barak 
304045959dbSMatan Barak 	if (!(supported_gids & 1 << default_gid_type))
305045959dbSMatan Barak 		return -EINVAL;
306045959dbSMatan Barak 
307045959dbSMatan Barak 	cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
308045959dbSMatan Barak 		default_gid_type;
309045959dbSMatan Barak 
310045959dbSMatan Barak 	return 0;
311045959dbSMatan Barak }
312045959dbSMatan Barak 
3131fb7f897SMark Bloch int cma_get_default_roce_tos(struct cma_device *cma_dev, u32 port)
31489052d78SMajd Dibbiny {
31589052d78SMajd Dibbiny 	if (!rdma_is_port_valid(cma_dev->device, port))
31689052d78SMajd Dibbiny 		return -EINVAL;
31789052d78SMajd Dibbiny 
31889052d78SMajd Dibbiny 	return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
31989052d78SMajd Dibbiny }
32089052d78SMajd Dibbiny 
3211fb7f897SMark Bloch int cma_set_default_roce_tos(struct cma_device *cma_dev, u32 port,
32289052d78SMajd Dibbiny 			     u8 default_roce_tos)
32389052d78SMajd Dibbiny {
32489052d78SMajd Dibbiny 	if (!rdma_is_port_valid(cma_dev->device, port))
32589052d78SMajd Dibbiny 		return -EINVAL;
32689052d78SMajd Dibbiny 
32789052d78SMajd Dibbiny 	cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
32889052d78SMajd Dibbiny 		 default_roce_tos;
32989052d78SMajd Dibbiny 
33089052d78SMajd Dibbiny 	return 0;
33189052d78SMajd Dibbiny }
332045959dbSMatan Barak struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
333045959dbSMatan Barak {
334045959dbSMatan Barak 	return cma_dev->device;
335045959dbSMatan Barak }
336045959dbSMatan Barak 
337e51060f0SSean Hefty /*
338e51060f0SSean Hefty  * Device removal can occur at anytime, so we need extra handling to
339e51060f0SSean Hefty  * serialize notifying the user of device removal with other callbacks.
340e51060f0SSean Hefty  * We do this by disabling removal notification while a callback is in process,
341e51060f0SSean Hefty  * and reporting it after the callback completes.
342e51060f0SSean Hefty  */
343e51060f0SSean Hefty 
344c8f6a362SSean Hefty struct cma_multicast {
345c8f6a362SSean Hefty 	struct rdma_id_private *id_priv;
346fe454dc3SAvihai Horon 	union {
347b5de0c60SJason Gunthorpe 		struct ib_sa_multicast *sa_mc;
348fe454dc3SAvihai Horon 		struct {
349fe454dc3SAvihai Horon 			struct work_struct work;
350fe454dc3SAvihai Horon 			struct rdma_cm_event event;
351fe454dc3SAvihai Horon 		} iboe_join;
352fe454dc3SAvihai Horon 	};
353c8f6a362SSean Hefty 	struct list_head	list;
354c8f6a362SSean Hefty 	void			*context;
3553f446754SRoland Dreier 	struct sockaddr_storage	addr;
356ab15c95aSAlex Vesker 	u8			join_state;
357c8f6a362SSean Hefty };
358c8f6a362SSean Hefty 
359e51060f0SSean Hefty struct cma_work {
360e51060f0SSean Hefty 	struct work_struct	work;
361e51060f0SSean Hefty 	struct rdma_id_private	*id;
362550e5ca7SNir Muchtar 	enum rdma_cm_state	old_state;
363550e5ca7SNir Muchtar 	enum rdma_cm_state	new_state;
364e51060f0SSean Hefty 	struct rdma_cm_event	event;
365e51060f0SSean Hefty };
366e51060f0SSean Hefty 
367e51060f0SSean Hefty union cma_ip_addr {
368e51060f0SSean Hefty 	struct in6_addr ip6;
369e51060f0SSean Hefty 	struct {
3701b90c137SAl Viro 		__be32 pad[3];
3711b90c137SAl Viro 		__be32 addr;
372e51060f0SSean Hefty 	} ip4;
373e51060f0SSean Hefty };
374e51060f0SSean Hefty 
375e51060f0SSean Hefty struct cma_hdr {
376e51060f0SSean Hefty 	u8 cma_version;
377e51060f0SSean Hefty 	u8 ip_version;	/* IP version: 7:4 */
3781b90c137SAl Viro 	__be16 port;
379e51060f0SSean Hefty 	union cma_ip_addr src_addr;
380e51060f0SSean Hefty 	union cma_ip_addr dst_addr;
381e51060f0SSean Hefty };
382e51060f0SSean Hefty 
383e51060f0SSean Hefty #define CMA_VERSION 0x00
384e51060f0SSean Hefty 
3854c21b5bcSHaggai Eran struct cma_req_info {
3862918c1a9SParav Pandit 	struct sockaddr_storage listen_addr_storage;
3872918c1a9SParav Pandit 	struct sockaddr_storage src_addr_storage;
3884c21b5bcSHaggai Eran 	struct ib_device *device;
3894c21b5bcSHaggai Eran 	union ib_gid local_gid;
3904c21b5bcSHaggai Eran 	__be64 service_id;
39105e0b86cSParav Pandit 	int port;
39205e0b86cSParav Pandit 	bool has_gid;
3934c21b5bcSHaggai Eran 	u16 pkey;
3944c21b5bcSHaggai Eran };
3954c21b5bcSHaggai Eran 
396e51060f0SSean Hefty static int cma_comp_exch(struct rdma_id_private *id_priv,
397550e5ca7SNir Muchtar 			 enum rdma_cm_state comp, enum rdma_cm_state exch)
398e51060f0SSean Hefty {
399e51060f0SSean Hefty 	unsigned long flags;
400e51060f0SSean Hefty 	int ret;
401e51060f0SSean Hefty 
4022a7cec53SJason Gunthorpe 	/*
4032a7cec53SJason Gunthorpe 	 * The FSM uses a funny double locking where state is protected by both
4042a7cec53SJason Gunthorpe 	 * the handler_mutex and the spinlock. State is not allowed to change
405071ba4ccSJason Gunthorpe 	 * to/from a handler_mutex protected value without also holding
4062a7cec53SJason Gunthorpe 	 * handler_mutex.
4072a7cec53SJason Gunthorpe 	 */
408071ba4ccSJason Gunthorpe 	if (comp == RDMA_CM_CONNECT || exch == RDMA_CM_CONNECT)
4092a7cec53SJason Gunthorpe 		lockdep_assert_held(&id_priv->handler_mutex);
4102a7cec53SJason Gunthorpe 
411e51060f0SSean Hefty 	spin_lock_irqsave(&id_priv->lock, flags);
412e51060f0SSean Hefty 	if ((ret = (id_priv->state == comp)))
413e51060f0SSean Hefty 		id_priv->state = exch;
414e51060f0SSean Hefty 	spin_unlock_irqrestore(&id_priv->lock, flags);
415e51060f0SSean Hefty 	return ret;
416e51060f0SSean Hefty }
417e51060f0SSean Hefty 
4184c21b5bcSHaggai Eran static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
419e51060f0SSean Hefty {
420e51060f0SSean Hefty 	return hdr->ip_version >> 4;
421e51060f0SSean Hefty }
422e51060f0SSean Hefty 
423e51060f0SSean Hefty static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
424e51060f0SSean Hefty {
425e51060f0SSean Hefty 	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
426e51060f0SSean Hefty }
427e51060f0SSean Hefty 
428bee3c3c9SMoni Shoua static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
429bee3c3c9SMoni Shoua {
430bee3c3c9SMoni Shoua 	struct in_device *in_dev = NULL;
431bee3c3c9SMoni Shoua 
432bee3c3c9SMoni Shoua 	if (ndev) {
433bee3c3c9SMoni Shoua 		rtnl_lock();
434bee3c3c9SMoni Shoua 		in_dev = __in_dev_get_rtnl(ndev);
435bee3c3c9SMoni Shoua 		if (in_dev) {
436bee3c3c9SMoni Shoua 			if (join)
437bee3c3c9SMoni Shoua 				ip_mc_inc_group(in_dev,
438bee3c3c9SMoni Shoua 						*(__be32 *)(mgid->raw + 12));
439bee3c3c9SMoni Shoua 			else
440bee3c3c9SMoni Shoua 				ip_mc_dec_group(in_dev,
441bee3c3c9SMoni Shoua 						*(__be32 *)(mgid->raw + 12));
442bee3c3c9SMoni Shoua 		}
443bee3c3c9SMoni Shoua 		rtnl_unlock();
444bee3c3c9SMoni Shoua 	}
445bee3c3c9SMoni Shoua 	return (in_dev) ? 0 : -ENODEV;
446bee3c3c9SMoni Shoua }
447bee3c3c9SMoni Shoua 
448045959dbSMatan Barak static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
449e51060f0SSean Hefty 			       struct cma_device *cma_dev)
450e51060f0SSean Hefty {
4515ff8c8faSParav Pandit 	cma_dev_get(cma_dev);
452e51060f0SSean Hefty 	id_priv->cma_dev = cma_dev;
453e51060f0SSean Hefty 	id_priv->id.device = cma_dev->device;
4543c86aa70SEli Cohen 	id_priv->id.route.addr.dev_addr.transport =
4553c86aa70SEli Cohen 		rdma_node_get_transport(cma_dev->device->node_type);
456e51060f0SSean Hefty 	list_add_tail(&id_priv->list, &cma_dev->id_list);
457b09c4d70SLeon Romanovsky 
458278f74b3SChuck Lever 	trace_cm_id_attach(id_priv, cma_dev->device);
459e51060f0SSean Hefty }
460e51060f0SSean Hefty 
461045959dbSMatan Barak static void cma_attach_to_dev(struct rdma_id_private *id_priv,
462045959dbSMatan Barak 			      struct cma_device *cma_dev)
463045959dbSMatan Barak {
464045959dbSMatan Barak 	_cma_attach_to_dev(id_priv, cma_dev);
465045959dbSMatan Barak 	id_priv->gid_type =
466045959dbSMatan Barak 		cma_dev->default_gid_type[id_priv->id.port_num -
467045959dbSMatan Barak 					  rdma_start_port(cma_dev->device)];
468045959dbSMatan Barak }
469045959dbSMatan Barak 
470a396d43aSSean Hefty static void cma_release_dev(struct rdma_id_private *id_priv)
471e51060f0SSean Hefty {
472a396d43aSSean Hefty 	mutex_lock(&lock);
473e51060f0SSean Hefty 	list_del(&id_priv->list);
4745ff8c8faSParav Pandit 	cma_dev_put(id_priv->cma_dev);
475e51060f0SSean Hefty 	id_priv->cma_dev = NULL;
476889d916bSShay Drory 	id_priv->id.device = NULL;
477e246b7c0SLeon Romanovsky 	if (id_priv->id.route.addr.dev_addr.sgid_attr) {
478e246b7c0SLeon Romanovsky 		rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr);
479e246b7c0SLeon Romanovsky 		id_priv->id.route.addr.dev_addr.sgid_attr = NULL;
480e246b7c0SLeon Romanovsky 	}
481a396d43aSSean Hefty 	mutex_unlock(&lock);
482e51060f0SSean Hefty }
483e51060f0SSean Hefty 
484f4753834SSean Hefty static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
485f4753834SSean Hefty {
486f4753834SSean Hefty 	return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
487f4753834SSean Hefty }
488f4753834SSean Hefty 
489f4753834SSean Hefty static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
490f4753834SSean Hefty {
491f4753834SSean Hefty 	return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
492f4753834SSean Hefty }
493f4753834SSean Hefty 
494f4753834SSean Hefty static inline unsigned short cma_family(struct rdma_id_private *id_priv)
495f4753834SSean Hefty {
496f4753834SSean Hefty 	return id_priv->id.route.addr.src_addr.ss_family;
497f4753834SSean Hefty }
498f4753834SSean Hefty 
4995c438135SSean Hefty static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
500c8f6a362SSean Hefty {
501c8f6a362SSean Hefty 	struct ib_sa_mcmember_rec rec;
502c8f6a362SSean Hefty 	int ret = 0;
503c8f6a362SSean Hefty 
5045c438135SSean Hefty 	if (id_priv->qkey) {
5055c438135SSean Hefty 		if (qkey && id_priv->qkey != qkey)
5065c438135SSean Hefty 			return -EINVAL;
507d2ca39f2SYossi Etigin 		return 0;
5085c438135SSean Hefty 	}
5095c438135SSean Hefty 
5105c438135SSean Hefty 	if (qkey) {
5115c438135SSean Hefty 		id_priv->qkey = qkey;
5125c438135SSean Hefty 		return 0;
5135c438135SSean Hefty 	}
514d2ca39f2SYossi Etigin 
515d2ca39f2SYossi Etigin 	switch (id_priv->id.ps) {
516c8f6a362SSean Hefty 	case RDMA_PS_UDP:
5175c438135SSean Hefty 	case RDMA_PS_IB:
518d2ca39f2SYossi Etigin 		id_priv->qkey = RDMA_UDP_QKEY;
519c8f6a362SSean Hefty 		break;
520c8f6a362SSean Hefty 	case RDMA_PS_IPOIB:
521d2ca39f2SYossi Etigin 		ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
522d2ca39f2SYossi Etigin 		ret = ib_sa_get_mcmember_rec(id_priv->id.device,
523d2ca39f2SYossi Etigin 					     id_priv->id.port_num, &rec.mgid,
524d2ca39f2SYossi Etigin 					     &rec);
525d2ca39f2SYossi Etigin 		if (!ret)
526d2ca39f2SYossi Etigin 			id_priv->qkey = be32_to_cpu(rec.qkey);
527c8f6a362SSean Hefty 		break;
528c8f6a362SSean Hefty 	default:
529c8f6a362SSean Hefty 		break;
530c8f6a362SSean Hefty 	}
531c8f6a362SSean Hefty 	return ret;
532c8f6a362SSean Hefty }
533c8f6a362SSean Hefty 
534680f920aSSean Hefty static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
535680f920aSSean Hefty {
536680f920aSSean Hefty 	dev_addr->dev_type = ARPHRD_INFINIBAND;
537680f920aSSean Hefty 	rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
538680f920aSSean Hefty 	ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
539680f920aSSean Hefty }
540680f920aSSean Hefty 
541680f920aSSean Hefty static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
542680f920aSSean Hefty {
543680f920aSSean Hefty 	int ret;
544680f920aSSean Hefty 
545680f920aSSean Hefty 	if (addr->sa_family != AF_IB) {
546575c7e58SParav Pandit 		ret = rdma_translate_ip(addr, dev_addr);
547680f920aSSean Hefty 	} else {
548680f920aSSean Hefty 		cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
549680f920aSSean Hefty 		ret = 0;
550680f920aSSean Hefty 	}
551680f920aSSean Hefty 
552680f920aSSean Hefty 	return ret;
553680f920aSSean Hefty }
554680f920aSSean Hefty 
5554ed13a5fSParav Pandit static const struct ib_gid_attr *
5561fb7f897SMark Bloch cma_validate_port(struct ib_device *device, u32 port,
557045959dbSMatan Barak 		  enum ib_gid_type gid_type,
5582493a57bSParav Pandit 		  union ib_gid *gid,
5592493a57bSParav Pandit 		  struct rdma_id_private *id_priv)
5607c11147dSMichael Wang {
5612493a57bSParav Pandit 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
5622493a57bSParav Pandit 	int bound_if_index = dev_addr->bound_dev_if;
5634ed13a5fSParav Pandit 	const struct ib_gid_attr *sgid_attr;
5642493a57bSParav Pandit 	int dev_type = dev_addr->dev_type;
565abae1b71SMatan Barak 	struct net_device *ndev = NULL;
5667c11147dSMichael Wang 
56741c61401SParav Pandit 	if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
56841c61401SParav Pandit 		return ERR_PTR(-ENODEV);
56941c61401SParav Pandit 
5707c11147dSMichael Wang 	if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
5714ed13a5fSParav Pandit 		return ERR_PTR(-ENODEV);
5727c11147dSMichael Wang 
5737c11147dSMichael Wang 	if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
5744ed13a5fSParav Pandit 		return ERR_PTR(-ENODEV);
5757c11147dSMichael Wang 
57600db63c1SParav Pandit 	if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
57766c74d74SParav Pandit 		ndev = dev_get_by_index(dev_addr->net, bound_if_index);
57800db63c1SParav Pandit 		if (!ndev)
5794ed13a5fSParav Pandit 			return ERR_PTR(-ENODEV);
58000db63c1SParav Pandit 	} else {
581045959dbSMatan Barak 		gid_type = IB_GID_TYPE_IB;
58200db63c1SParav Pandit 	}
583abae1b71SMatan Barak 
5844ed13a5fSParav Pandit 	sgid_attr = rdma_find_gid_by_port(device, gid, gid_type, port, ndev);
585abae1b71SMatan Barak 	if (ndev)
586abae1b71SMatan Barak 		dev_put(ndev);
5874ed13a5fSParav Pandit 	return sgid_attr;
5884ed13a5fSParav Pandit }
5897c11147dSMichael Wang 
5904ed13a5fSParav Pandit static void cma_bind_sgid_attr(struct rdma_id_private *id_priv,
5914ed13a5fSParav Pandit 			       const struct ib_gid_attr *sgid_attr)
5924ed13a5fSParav Pandit {
5934ed13a5fSParav Pandit 	WARN_ON(id_priv->id.route.addr.dev_addr.sgid_attr);
5944ed13a5fSParav Pandit 	id_priv->id.route.addr.dev_addr.sgid_attr = sgid_attr;
5957c11147dSMichael Wang }
5967c11147dSMichael Wang 
597ff11c6cdSParav Pandit /**
598ff11c6cdSParav Pandit  * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
599ff11c6cdSParav Pandit  * based on source ip address.
600ff11c6cdSParav Pandit  * @id_priv:	cm_id which should be bound to cma device
601ff11c6cdSParav Pandit  *
602ff11c6cdSParav Pandit  * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
603ff11c6cdSParav Pandit  * based on source IP address. It returns 0 on success or error code otherwise.
604ff11c6cdSParav Pandit  * It is applicable to active and passive side cm_id.
605ff11c6cdSParav Pandit  */
606ff11c6cdSParav Pandit static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
607ff11c6cdSParav Pandit {
608ff11c6cdSParav Pandit 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
609ff11c6cdSParav Pandit 	const struct ib_gid_attr *sgid_attr;
610ff11c6cdSParav Pandit 	union ib_gid gid, iboe_gid, *gidp;
611ff11c6cdSParav Pandit 	struct cma_device *cma_dev;
612ff11c6cdSParav Pandit 	enum ib_gid_type gid_type;
613ff11c6cdSParav Pandit 	int ret = -ENODEV;
6141fb7f897SMark Bloch 	u32 port;
615ff11c6cdSParav Pandit 
616ff11c6cdSParav Pandit 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
617ff11c6cdSParav Pandit 	    id_priv->id.ps == RDMA_PS_IPOIB)
618ff11c6cdSParav Pandit 		return -EINVAL;
619ff11c6cdSParav Pandit 
620ff11c6cdSParav Pandit 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
621ff11c6cdSParav Pandit 		    &iboe_gid);
622ff11c6cdSParav Pandit 
623ff11c6cdSParav Pandit 	memcpy(&gid, dev_addr->src_dev_addr +
624ff11c6cdSParav Pandit 	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
625ff11c6cdSParav Pandit 
626ff11c6cdSParav Pandit 	mutex_lock(&lock);
627ff11c6cdSParav Pandit 	list_for_each_entry(cma_dev, &dev_list, list) {
628ea1075edSJason Gunthorpe 		rdma_for_each_port (cma_dev->device, port) {
629ff11c6cdSParav Pandit 			gidp = rdma_protocol_roce(cma_dev->device, port) ?
630ff11c6cdSParav Pandit 			       &iboe_gid : &gid;
631ff11c6cdSParav Pandit 			gid_type = cma_dev->default_gid_type[port - 1];
632ff11c6cdSParav Pandit 			sgid_attr = cma_validate_port(cma_dev->device, port,
633ff11c6cdSParav Pandit 						      gid_type, gidp, id_priv);
634ff11c6cdSParav Pandit 			if (!IS_ERR(sgid_attr)) {
635ff11c6cdSParav Pandit 				id_priv->id.port_num = port;
636ff11c6cdSParav Pandit 				cma_bind_sgid_attr(id_priv, sgid_attr);
637ff11c6cdSParav Pandit 				cma_attach_to_dev(id_priv, cma_dev);
638ff11c6cdSParav Pandit 				ret = 0;
639ff11c6cdSParav Pandit 				goto out;
640ff11c6cdSParav Pandit 			}
641ff11c6cdSParav Pandit 		}
642ff11c6cdSParav Pandit 	}
643ff11c6cdSParav Pandit out:
644ff11c6cdSParav Pandit 	mutex_unlock(&lock);
645ff11c6cdSParav Pandit 	return ret;
646ff11c6cdSParav Pandit }
647ff11c6cdSParav Pandit 
64841ab1cb7SParav Pandit /**
64941ab1cb7SParav Pandit  * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
65041ab1cb7SParav Pandit  * @id_priv:		cm id to bind to cma device
65141ab1cb7SParav Pandit  * @listen_id_priv:	listener cm id to match against
65241ab1cb7SParav Pandit  * @req:		Pointer to req structure containaining incoming
65341ab1cb7SParav Pandit  *			request information
65441ab1cb7SParav Pandit  * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
65541ab1cb7SParav Pandit  * rdma device matches for listen_id and incoming request. It also verifies
65641ab1cb7SParav Pandit  * that a GID table entry is present for the source address.
65741ab1cb7SParav Pandit  * Returns 0 on success, or returns error code otherwise.
65841ab1cb7SParav Pandit  */
65941ab1cb7SParav Pandit static int cma_ib_acquire_dev(struct rdma_id_private *id_priv,
66041ab1cb7SParav Pandit 			      const struct rdma_id_private *listen_id_priv,
66141ab1cb7SParav Pandit 			      struct cma_req_info *req)
66241ab1cb7SParav Pandit {
66341ab1cb7SParav Pandit 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
66441ab1cb7SParav Pandit 	const struct ib_gid_attr *sgid_attr;
66541ab1cb7SParav Pandit 	enum ib_gid_type gid_type;
66641ab1cb7SParav Pandit 	union ib_gid gid;
66741ab1cb7SParav Pandit 
66841ab1cb7SParav Pandit 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
66941ab1cb7SParav Pandit 	    id_priv->id.ps == RDMA_PS_IPOIB)
67041ab1cb7SParav Pandit 		return -EINVAL;
67141ab1cb7SParav Pandit 
67241ab1cb7SParav Pandit 	if (rdma_protocol_roce(req->device, req->port))
67341ab1cb7SParav Pandit 		rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
67441ab1cb7SParav Pandit 			    &gid);
67541ab1cb7SParav Pandit 	else
67641ab1cb7SParav Pandit 		memcpy(&gid, dev_addr->src_dev_addr +
67741ab1cb7SParav Pandit 		       rdma_addr_gid_offset(dev_addr), sizeof(gid));
67841ab1cb7SParav Pandit 
67941ab1cb7SParav Pandit 	gid_type = listen_id_priv->cma_dev->default_gid_type[req->port - 1];
68041ab1cb7SParav Pandit 	sgid_attr = cma_validate_port(req->device, req->port,
68141ab1cb7SParav Pandit 				      gid_type, &gid, id_priv);
68241ab1cb7SParav Pandit 	if (IS_ERR(sgid_attr))
68341ab1cb7SParav Pandit 		return PTR_ERR(sgid_attr);
68441ab1cb7SParav Pandit 
68541ab1cb7SParav Pandit 	id_priv->id.port_num = req->port;
68641ab1cb7SParav Pandit 	cma_bind_sgid_attr(id_priv, sgid_attr);
68741ab1cb7SParav Pandit 	/* Need to acquire lock to protect against reader
68841ab1cb7SParav Pandit 	 * of cma_dev->id_list such as cma_netdev_callback() and
68941ab1cb7SParav Pandit 	 * cma_process_remove().
69041ab1cb7SParav Pandit 	 */
69141ab1cb7SParav Pandit 	mutex_lock(&lock);
69241ab1cb7SParav Pandit 	cma_attach_to_dev(id_priv, listen_id_priv->cma_dev);
69341ab1cb7SParav Pandit 	mutex_unlock(&lock);
694cb5cd0eaSShay Drory 	rdma_restrack_add(&id_priv->res);
69541ab1cb7SParav Pandit 	return 0;
69641ab1cb7SParav Pandit }
69741ab1cb7SParav Pandit 
69841ab1cb7SParav Pandit static int cma_iw_acquire_dev(struct rdma_id_private *id_priv,
699e7ff98aeSParav Pandit 			      const struct rdma_id_private *listen_id_priv)
700e51060f0SSean Hefty {
701c8f6a362SSean Hefty 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
7024ed13a5fSParav Pandit 	const struct ib_gid_attr *sgid_attr;
703e51060f0SSean Hefty 	struct cma_device *cma_dev;
7044ed13a5fSParav Pandit 	enum ib_gid_type gid_type;
705e51060f0SSean Hefty 	int ret = -ENODEV;
70641ab1cb7SParav Pandit 	union ib_gid gid;
7071fb7f897SMark Bloch 	u32 port;
708e51060f0SSean Hefty 
7097c11147dSMichael Wang 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
7102efdd6a0SMoni Shoua 	    id_priv->id.ps == RDMA_PS_IPOIB)
7112efdd6a0SMoni Shoua 		return -EINVAL;
7122efdd6a0SMoni Shoua 
7133c86aa70SEli Cohen 	memcpy(&gid, dev_addr->src_dev_addr +
71441ab1cb7SParav Pandit 	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
71541ab1cb7SParav Pandit 
71641ab1cb7SParav Pandit 	mutex_lock(&lock);
7177c11147dSMichael Wang 
718be9130ccSDoug Ledford 	cma_dev = listen_id_priv->cma_dev;
719be9130ccSDoug Ledford 	port = listen_id_priv->id.port_num;
72079d684f0SParav Pandit 	gid_type = listen_id_priv->gid_type;
7214ed13a5fSParav Pandit 	sgid_attr = cma_validate_port(cma_dev->device, port,
72241ab1cb7SParav Pandit 				      gid_type, &gid, id_priv);
7234ed13a5fSParav Pandit 	if (!IS_ERR(sgid_attr)) {
7247c11147dSMichael Wang 		id_priv->id.port_num = port;
7254ed13a5fSParav Pandit 		cma_bind_sgid_attr(id_priv, sgid_attr);
7264ed13a5fSParav Pandit 		ret = 0;
727be9130ccSDoug Ledford 		goto out;
728be9130ccSDoug Ledford 	}
7297c11147dSMichael Wang 
730e51060f0SSean Hefty 	list_for_each_entry(cma_dev, &dev_list, list) {
731cc055dd3SParav Pandit 		rdma_for_each_port (cma_dev->device, port) {
732ff11c6cdSParav Pandit 			if (listen_id_priv->cma_dev == cma_dev &&
733be9130ccSDoug Ledford 			    listen_id_priv->id.port_num == port)
734be9130ccSDoug Ledford 				continue;
7353c86aa70SEli Cohen 
73679d684f0SParav Pandit 			gid_type = cma_dev->default_gid_type[port - 1];
7374ed13a5fSParav Pandit 			sgid_attr = cma_validate_port(cma_dev->device, port,
73841ab1cb7SParav Pandit 						      gid_type, &gid, id_priv);
7394ed13a5fSParav Pandit 			if (!IS_ERR(sgid_attr)) {
7407c11147dSMichael Wang 				id_priv->id.port_num = port;
7414ed13a5fSParav Pandit 				cma_bind_sgid_attr(id_priv, sgid_attr);
7424ed13a5fSParav Pandit 				ret = 0;
7433c86aa70SEli Cohen 				goto out;
74463f05be2Sshefty 			}
745e51060f0SSean Hefty 		}
746e51060f0SSean Hefty 	}
7473c86aa70SEli Cohen 
7483c86aa70SEli Cohen out:
749cb5cd0eaSShay Drory 	if (!ret) {
7503c86aa70SEli Cohen 		cma_attach_to_dev(id_priv, cma_dev);
751cb5cd0eaSShay Drory 		rdma_restrack_add(&id_priv->res);
752cb5cd0eaSShay Drory 	}
7533c86aa70SEli Cohen 
754a396d43aSSean Hefty 	mutex_unlock(&lock);
755e51060f0SSean Hefty 	return ret;
756e51060f0SSean Hefty }
757e51060f0SSean Hefty 
758f17df3b0SSean Hefty /*
759f17df3b0SSean Hefty  * Select the source IB device and address to reach the destination IB address.
760f17df3b0SSean Hefty  */
761f17df3b0SSean Hefty static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
762f17df3b0SSean Hefty {
763f17df3b0SSean Hefty 	struct cma_device *cma_dev, *cur_dev;
764f17df3b0SSean Hefty 	struct sockaddr_ib *addr;
765f17df3b0SSean Hefty 	union ib_gid gid, sgid, *dgid;
766cc055dd3SParav Pandit 	unsigned int p;
767f17df3b0SSean Hefty 	u16 pkey, index;
76893b1f29dSJack Wang 	enum ib_port_state port_state;
769f17df3b0SSean Hefty 	int i;
770f17df3b0SSean Hefty 
771f17df3b0SSean Hefty 	cma_dev = NULL;
772f17df3b0SSean Hefty 	addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
773f17df3b0SSean Hefty 	dgid = (union ib_gid *) &addr->sib_addr;
774f17df3b0SSean Hefty 	pkey = ntohs(addr->sib_pkey);
775f17df3b0SSean Hefty 
776954a8e3aSParav Pandit 	mutex_lock(&lock);
777f17df3b0SSean Hefty 	list_for_each_entry(cur_dev, &dev_list, list) {
778cc055dd3SParav Pandit 		rdma_for_each_port (cur_dev->device, p) {
77930a74ef4SMichael Wang 			if (!rdma_cap_af_ib(cur_dev->device, p))
780f17df3b0SSean Hefty 				continue;
781f17df3b0SSean Hefty 
782f17df3b0SSean Hefty 			if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
783f17df3b0SSean Hefty 				continue;
784f17df3b0SSean Hefty 
78593b1f29dSJack Wang 			if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
78693b1f29dSJack Wang 				continue;
7871dfce294SParav Pandit 			for (i = 0; !rdma_query_gid(cur_dev->device,
7881dfce294SParav Pandit 						    p, i, &gid);
78955ee3ab2SMatan Barak 			     i++) {
790f17df3b0SSean Hefty 				if (!memcmp(&gid, dgid, sizeof(gid))) {
791f17df3b0SSean Hefty 					cma_dev = cur_dev;
792f17df3b0SSean Hefty 					sgid = gid;
7938fb488d7SPaul Bolle 					id_priv->id.port_num = p;
794f17df3b0SSean Hefty 					goto found;
795f17df3b0SSean Hefty 				}
796f17df3b0SSean Hefty 
797f17df3b0SSean Hefty 				if (!cma_dev && (gid.global.subnet_prefix ==
79893b1f29dSJack Wang 				    dgid->global.subnet_prefix) &&
79993b1f29dSJack Wang 				    port_state == IB_PORT_ACTIVE) {
800f17df3b0SSean Hefty 					cma_dev = cur_dev;
801f17df3b0SSean Hefty 					sgid = gid;
8028fb488d7SPaul Bolle 					id_priv->id.port_num = p;
803954a8e3aSParav Pandit 					goto found;
804f17df3b0SSean Hefty 				}
805f17df3b0SSean Hefty 			}
806f17df3b0SSean Hefty 		}
807f17df3b0SSean Hefty 	}
808954a8e3aSParav Pandit 	mutex_unlock(&lock);
809f17df3b0SSean Hefty 	return -ENODEV;
810f17df3b0SSean Hefty 
811f17df3b0SSean Hefty found:
812f17df3b0SSean Hefty 	cma_attach_to_dev(id_priv, cma_dev);
813cb5cd0eaSShay Drory 	rdma_restrack_add(&id_priv->res);
814954a8e3aSParav Pandit 	mutex_unlock(&lock);
815f17df3b0SSean Hefty 	addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
816954a8e3aSParav Pandit 	memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
817f17df3b0SSean Hefty 	cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
818f17df3b0SSean Hefty 	return 0;
819f17df3b0SSean Hefty }
820f17df3b0SSean Hefty 
821e368d23fSParav Pandit static void cma_id_get(struct rdma_id_private *id_priv)
822e368d23fSParav Pandit {
82343fb5892SParav Pandit 	refcount_inc(&id_priv->refcount);
824e368d23fSParav Pandit }
825e368d23fSParav Pandit 
826e368d23fSParav Pandit static void cma_id_put(struct rdma_id_private *id_priv)
827e51060f0SSean Hefty {
82843fb5892SParav Pandit 	if (refcount_dec_and_test(&id_priv->refcount))
829e51060f0SSean Hefty 		complete(&id_priv->comp);
830e51060f0SSean Hefty }
831e51060f0SSean Hefty 
832b09c4d70SLeon Romanovsky static struct rdma_id_private *
833b09c4d70SLeon Romanovsky __rdma_create_id(struct net *net, rdma_cm_event_handler event_handler,
8342253fc0cSSteve Wise 		 void *context, enum rdma_ucm_port_space ps,
835b09c4d70SLeon Romanovsky 		 enum ib_qp_type qp_type, const struct rdma_id_private *parent)
836e51060f0SSean Hefty {
837e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
838e51060f0SSean Hefty 
839e51060f0SSean Hefty 	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
840e51060f0SSean Hefty 	if (!id_priv)
841e51060f0SSean Hefty 		return ERR_PTR(-ENOMEM);
842e51060f0SSean Hefty 
843550e5ca7SNir Muchtar 	id_priv->state = RDMA_CM_IDLE;
844e51060f0SSean Hefty 	id_priv->id.context = context;
845e51060f0SSean Hefty 	id_priv->id.event_handler = event_handler;
846e51060f0SSean Hefty 	id_priv->id.ps = ps;
847b26f9b99SSean Hefty 	id_priv->id.qp_type = qp_type;
84889052d78SMajd Dibbiny 	id_priv->tos_set = false;
8492c1619edSDanit Goldberg 	id_priv->timeout_set = false;
8503aeffc46SHåkon Bugge 	id_priv->min_rnr_timer_set = false;
85179d684f0SParav Pandit 	id_priv->gid_type = IB_GID_TYPE_IB;
852e51060f0SSean Hefty 	spin_lock_init(&id_priv->lock);
853c5483388SSean Hefty 	mutex_init(&id_priv->qp_mutex);
854e51060f0SSean Hefty 	init_completion(&id_priv->comp);
85543fb5892SParav Pandit 	refcount_set(&id_priv->refcount, 1);
856de910bd9SOr Gerlitz 	mutex_init(&id_priv->handler_mutex);
857e51060f0SSean Hefty 	INIT_LIST_HEAD(&id_priv->listen_list);
858c8f6a362SSean Hefty 	INIT_LIST_HEAD(&id_priv->mc_list);
859e51060f0SSean Hefty 	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
860fa20105eSGuy Shapiro 	id_priv->id.route.addr.dev_addr.net = get_net(net);
86123a9cd2aSMoni Shoua 	id_priv->seq_num &= 0x00ffffff;
862e51060f0SSean Hefty 
86313ef5539SLeon Romanovsky 	rdma_restrack_new(&id_priv->res, RDMA_RESTRACK_CM_ID);
864b09c4d70SLeon Romanovsky 	if (parent)
865b09c4d70SLeon Romanovsky 		rdma_restrack_parent_name(&id_priv->res, &parent->res);
86613ef5539SLeon Romanovsky 
867b09c4d70SLeon Romanovsky 	return id_priv;
868e51060f0SSean Hefty }
869b09c4d70SLeon Romanovsky 
870b09c4d70SLeon Romanovsky struct rdma_cm_id *
871b09c4d70SLeon Romanovsky __rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,
872b09c4d70SLeon Romanovsky 			void *context, enum rdma_ucm_port_space ps,
873b09c4d70SLeon Romanovsky 			enum ib_qp_type qp_type, const char *caller)
874b09c4d70SLeon Romanovsky {
875b09c4d70SLeon Romanovsky 	struct rdma_id_private *ret;
876b09c4d70SLeon Romanovsky 
877b09c4d70SLeon Romanovsky 	ret = __rdma_create_id(net, event_handler, context, ps, qp_type, NULL);
878b09c4d70SLeon Romanovsky 	if (IS_ERR(ret))
879b09c4d70SLeon Romanovsky 		return ERR_CAST(ret);
880b09c4d70SLeon Romanovsky 
881b09c4d70SLeon Romanovsky 	rdma_restrack_set_name(&ret->res, caller);
882b09c4d70SLeon Romanovsky 	return &ret->id;
883b09c4d70SLeon Romanovsky }
884b09c4d70SLeon Romanovsky EXPORT_SYMBOL(__rdma_create_kernel_id);
885b09c4d70SLeon Romanovsky 
886b09c4d70SLeon Romanovsky struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,
887b09c4d70SLeon Romanovsky 				       void *context,
888b09c4d70SLeon Romanovsky 				       enum rdma_ucm_port_space ps,
889b09c4d70SLeon Romanovsky 				       enum ib_qp_type qp_type)
890b09c4d70SLeon Romanovsky {
891b09c4d70SLeon Romanovsky 	struct rdma_id_private *ret;
892b09c4d70SLeon Romanovsky 
893b09c4d70SLeon Romanovsky 	ret = __rdma_create_id(current->nsproxy->net_ns, event_handler, context,
894b09c4d70SLeon Romanovsky 			       ps, qp_type, NULL);
895b09c4d70SLeon Romanovsky 	if (IS_ERR(ret))
896b09c4d70SLeon Romanovsky 		return ERR_CAST(ret);
897b09c4d70SLeon Romanovsky 
898b09c4d70SLeon Romanovsky 	rdma_restrack_set_name(&ret->res, NULL);
899b09c4d70SLeon Romanovsky 	return &ret->id;
900b09c4d70SLeon Romanovsky }
901b09c4d70SLeon Romanovsky EXPORT_SYMBOL(rdma_create_user_id);
902e51060f0SSean Hefty 
903c8f6a362SSean Hefty static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
904e51060f0SSean Hefty {
905e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
906c8f6a362SSean Hefty 	int qp_attr_mask, ret;
907e51060f0SSean Hefty 
908c8f6a362SSean Hefty 	qp_attr.qp_state = IB_QPS_INIT;
909c8f6a362SSean Hefty 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
910e51060f0SSean Hefty 	if (ret)
911e51060f0SSean Hefty 		return ret;
912e51060f0SSean Hefty 
913c8f6a362SSean Hefty 	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
914c8f6a362SSean Hefty 	if (ret)
915c8f6a362SSean Hefty 		return ret;
916c8f6a362SSean Hefty 
917c8f6a362SSean Hefty 	qp_attr.qp_state = IB_QPS_RTR;
918c8f6a362SSean Hefty 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
919c8f6a362SSean Hefty 	if (ret)
920c8f6a362SSean Hefty 		return ret;
921c8f6a362SSean Hefty 
922c8f6a362SSean Hefty 	qp_attr.qp_state = IB_QPS_RTS;
923c8f6a362SSean Hefty 	qp_attr.sq_psn = 0;
924c8f6a362SSean Hefty 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
925c8f6a362SSean Hefty 
926c8f6a362SSean Hefty 	return ret;
927e51060f0SSean Hefty }
928e51060f0SSean Hefty 
929e51060f0SSean Hefty int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
930e51060f0SSean Hefty 		   struct ib_qp_init_attr *qp_init_attr)
931e51060f0SSean Hefty {
932e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
933e51060f0SSean Hefty 	struct ib_qp *qp;
934dc70f7c3SHåkon Bugge 	int ret = 0;
935e51060f0SSean Hefty 
936e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
937ed999f82SChuck Lever 	if (id->device != pd->device) {
938ed999f82SChuck Lever 		ret = -EINVAL;
939ed999f82SChuck Lever 		goto out_err;
940ed999f82SChuck Lever 	}
941e51060f0SSean Hefty 
9420691a286SChristoph Hellwig 	qp_init_attr->port_num = id->port_num;
943e51060f0SSean Hefty 	qp = ib_create_qp(pd, qp_init_attr);
944ed999f82SChuck Lever 	if (IS_ERR(qp)) {
945ed999f82SChuck Lever 		ret = PTR_ERR(qp);
946ed999f82SChuck Lever 		goto out_err;
947ed999f82SChuck Lever 	}
948e51060f0SSean Hefty 
949b26f9b99SSean Hefty 	if (id->qp_type == IB_QPT_UD)
950c8f6a362SSean Hefty 		ret = cma_init_ud_qp(id_priv, qp);
951e51060f0SSean Hefty 	if (ret)
952ed999f82SChuck Lever 		goto out_destroy;
953e51060f0SSean Hefty 
954e51060f0SSean Hefty 	id->qp = qp;
955e51060f0SSean Hefty 	id_priv->qp_num = qp->qp_num;
956e51060f0SSean Hefty 	id_priv->srq = (qp->srq != NULL);
957ed999f82SChuck Lever 	trace_cm_qp_create(id_priv, pd, qp_init_attr, 0);
958e51060f0SSean Hefty 	return 0;
959ed999f82SChuck Lever out_destroy:
960e51060f0SSean Hefty 	ib_destroy_qp(qp);
961ed999f82SChuck Lever out_err:
962ed999f82SChuck Lever 	trace_cm_qp_create(id_priv, pd, qp_init_attr, ret);
963e51060f0SSean Hefty 	return ret;
964e51060f0SSean Hefty }
965e51060f0SSean Hefty EXPORT_SYMBOL(rdma_create_qp);
966e51060f0SSean Hefty 
967e51060f0SSean Hefty void rdma_destroy_qp(struct rdma_cm_id *id)
968e51060f0SSean Hefty {
969c5483388SSean Hefty 	struct rdma_id_private *id_priv;
970c5483388SSean Hefty 
971c5483388SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
972ed999f82SChuck Lever 	trace_cm_qp_destroy(id_priv);
973c5483388SSean Hefty 	mutex_lock(&id_priv->qp_mutex);
974c5483388SSean Hefty 	ib_destroy_qp(id_priv->id.qp);
975c5483388SSean Hefty 	id_priv->id.qp = NULL;
976c5483388SSean Hefty 	mutex_unlock(&id_priv->qp_mutex);
977e51060f0SSean Hefty }
978e51060f0SSean Hefty EXPORT_SYMBOL(rdma_destroy_qp);
979e51060f0SSean Hefty 
9805851bb89SSean Hefty static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
9815851bb89SSean Hefty 			     struct rdma_conn_param *conn_param)
982e51060f0SSean Hefty {
983e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
984e51060f0SSean Hefty 	int qp_attr_mask, ret;
985e51060f0SSean Hefty 
986c5483388SSean Hefty 	mutex_lock(&id_priv->qp_mutex);
987c5483388SSean Hefty 	if (!id_priv->id.qp) {
988c5483388SSean Hefty 		ret = 0;
989c5483388SSean Hefty 		goto out;
990c5483388SSean Hefty 	}
991e51060f0SSean Hefty 
992e51060f0SSean Hefty 	/* Need to update QP attributes from default values. */
993e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_INIT;
994c5483388SSean Hefty 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
995e51060f0SSean Hefty 	if (ret)
996c5483388SSean Hefty 		goto out;
997e51060f0SSean Hefty 
998c5483388SSean Hefty 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
999e51060f0SSean Hefty 	if (ret)
1000c5483388SSean Hefty 		goto out;
1001e51060f0SSean Hefty 
1002e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_RTR;
1003c5483388SSean Hefty 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1004e51060f0SSean Hefty 	if (ret)
1005c5483388SSean Hefty 		goto out;
1006e51060f0SSean Hefty 
1007fef60902SMichael Wang 	BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
1008fef60902SMichael Wang 
10095851bb89SSean Hefty 	if (conn_param)
10105851bb89SSean Hefty 		qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
1011c5483388SSean Hefty 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1012c5483388SSean Hefty out:
1013c5483388SSean Hefty 	mutex_unlock(&id_priv->qp_mutex);
1014c5483388SSean Hefty 	return ret;
1015e51060f0SSean Hefty }
1016e51060f0SSean Hefty 
10175851bb89SSean Hefty static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
10185851bb89SSean Hefty 			     struct rdma_conn_param *conn_param)
1019e51060f0SSean Hefty {
1020e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
1021e51060f0SSean Hefty 	int qp_attr_mask, ret;
1022e51060f0SSean Hefty 
1023c5483388SSean Hefty 	mutex_lock(&id_priv->qp_mutex);
1024c5483388SSean Hefty 	if (!id_priv->id.qp) {
1025c5483388SSean Hefty 		ret = 0;
1026c5483388SSean Hefty 		goto out;
1027e51060f0SSean Hefty 	}
1028e51060f0SSean Hefty 
1029c5483388SSean Hefty 	qp_attr.qp_state = IB_QPS_RTS;
1030c5483388SSean Hefty 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1031c5483388SSean Hefty 	if (ret)
1032c5483388SSean Hefty 		goto out;
1033c5483388SSean Hefty 
10345851bb89SSean Hefty 	if (conn_param)
10355851bb89SSean Hefty 		qp_attr.max_rd_atomic = conn_param->initiator_depth;
1036c5483388SSean Hefty 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1037c5483388SSean Hefty out:
1038c5483388SSean Hefty 	mutex_unlock(&id_priv->qp_mutex);
1039c5483388SSean Hefty 	return ret;
1040c5483388SSean Hefty }
1041c5483388SSean Hefty 
1042c5483388SSean Hefty static int cma_modify_qp_err(struct rdma_id_private *id_priv)
1043e51060f0SSean Hefty {
1044e51060f0SSean Hefty 	struct ib_qp_attr qp_attr;
1045c5483388SSean Hefty 	int ret;
1046e51060f0SSean Hefty 
1047c5483388SSean Hefty 	mutex_lock(&id_priv->qp_mutex);
1048c5483388SSean Hefty 	if (!id_priv->id.qp) {
1049c5483388SSean Hefty 		ret = 0;
1050c5483388SSean Hefty 		goto out;
1051c5483388SSean Hefty 	}
1052e51060f0SSean Hefty 
1053e51060f0SSean Hefty 	qp_attr.qp_state = IB_QPS_ERR;
1054c5483388SSean Hefty 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
1055c5483388SSean Hefty out:
1056c5483388SSean Hefty 	mutex_unlock(&id_priv->qp_mutex);
1057c5483388SSean Hefty 	return ret;
1058e51060f0SSean Hefty }
1059e51060f0SSean Hefty 
1060c8f6a362SSean Hefty static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
1061c8f6a362SSean Hefty 			       struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1062c8f6a362SSean Hefty {
1063c8f6a362SSean Hefty 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1064c8f6a362SSean Hefty 	int ret;
10653c86aa70SEli Cohen 	u16 pkey;
10663c86aa70SEli Cohen 
1067227128fcSMichael Wang 	if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
10683c86aa70SEli Cohen 		pkey = 0xffff;
1069fef60902SMichael Wang 	else
1070fef60902SMichael Wang 		pkey = ib_addr_get_pkey(dev_addr);
1071c8f6a362SSean Hefty 
1072c8f6a362SSean Hefty 	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
10733c86aa70SEli Cohen 				  pkey, &qp_attr->pkey_index);
1074c8f6a362SSean Hefty 	if (ret)
1075c8f6a362SSean Hefty 		return ret;
1076c8f6a362SSean Hefty 
1077c8f6a362SSean Hefty 	qp_attr->port_num = id_priv->id.port_num;
1078c8f6a362SSean Hefty 	*qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1079c8f6a362SSean Hefty 
1080b26f9b99SSean Hefty 	if (id_priv->id.qp_type == IB_QPT_UD) {
10815c438135SSean Hefty 		ret = cma_set_qkey(id_priv, 0);
1082d2ca39f2SYossi Etigin 		if (ret)
1083d2ca39f2SYossi Etigin 			return ret;
1084d2ca39f2SYossi Etigin 
1085c8f6a362SSean Hefty 		qp_attr->qkey = id_priv->qkey;
1086c8f6a362SSean Hefty 		*qp_attr_mask |= IB_QP_QKEY;
1087c8f6a362SSean Hefty 	} else {
1088c8f6a362SSean Hefty 		qp_attr->qp_access_flags = 0;
1089c8f6a362SSean Hefty 		*qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1090c8f6a362SSean Hefty 	}
1091c8f6a362SSean Hefty 	return 0;
1092c8f6a362SSean Hefty }
1093c8f6a362SSean Hefty 
1094e51060f0SSean Hefty int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1095e51060f0SSean Hefty 		       int *qp_attr_mask)
1096e51060f0SSean Hefty {
1097e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
1098c8f6a362SSean Hefty 	int ret = 0;
1099e51060f0SSean Hefty 
1100e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
110172219ceaSMichael Wang 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
1102b26f9b99SSean Hefty 		if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1103c8f6a362SSean Hefty 			ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1104c8f6a362SSean Hefty 		else
1105e51060f0SSean Hefty 			ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1106e51060f0SSean Hefty 						 qp_attr_mask);
1107dd5f03beSMatan Barak 
1108e51060f0SSean Hefty 		if (qp_attr->qp_state == IB_QPS_RTR)
1109e51060f0SSean Hefty 			qp_attr->rq_psn = id_priv->seq_num;
111004215330SMichael Wang 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1111c8f6a362SSean Hefty 		if (!id_priv->cm_id.iw) {
11128f076531SDotan Barak 			qp_attr->qp_access_flags = 0;
1113c8f6a362SSean Hefty 			*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1114c8f6a362SSean Hefty 		} else
111507ebafbaSTom Tucker 			ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
111607ebafbaSTom Tucker 						 qp_attr_mask);
1117a62ab66bSIsmail, Mustafa 		qp_attr->port_num = id_priv->id.port_num;
1118a62ab66bSIsmail, Mustafa 		*qp_attr_mask |= IB_QP_PORT;
1119b6eb7011SWenpeng Liang 	} else {
1120e51060f0SSean Hefty 		ret = -ENOSYS;
1121b6eb7011SWenpeng Liang 	}
1122e51060f0SSean Hefty 
11232c1619edSDanit Goldberg 	if ((*qp_attr_mask & IB_QP_TIMEOUT) && id_priv->timeout_set)
11242c1619edSDanit Goldberg 		qp_attr->timeout = id_priv->timeout;
11252c1619edSDanit Goldberg 
11263aeffc46SHåkon Bugge 	if ((*qp_attr_mask & IB_QP_MIN_RNR_TIMER) && id_priv->min_rnr_timer_set)
11273aeffc46SHåkon Bugge 		qp_attr->min_rnr_timer = id_priv->min_rnr_timer;
11283aeffc46SHåkon Bugge 
1129e51060f0SSean Hefty 	return ret;
1130e51060f0SSean Hefty }
1131e51060f0SSean Hefty EXPORT_SYMBOL(rdma_init_qp_attr);
1132e51060f0SSean Hefty 
1133ca3a8aceSParav Pandit static inline bool cma_zero_addr(const struct sockaddr *addr)
1134e51060f0SSean Hefty {
11352e2d190cSSean Hefty 	switch (addr->sa_family) {
11362e2d190cSSean Hefty 	case AF_INET:
11372e2d190cSSean Hefty 		return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
11382e2d190cSSean Hefty 	case AF_INET6:
11392e2d190cSSean Hefty 		return ipv6_addr_any(&((struct sockaddr_in6 *)addr)->sin6_addr);
11402e2d190cSSean Hefty 	case AF_IB:
11412e2d190cSSean Hefty 		return ib_addr_any(&((struct sockaddr_ib *)addr)->sib_addr);
11422e2d190cSSean Hefty 	default:
1143ca3a8aceSParav Pandit 		return false;
1144e51060f0SSean Hefty 	}
1145e51060f0SSean Hefty }
1146e51060f0SSean Hefty 
1147ca3a8aceSParav Pandit static inline bool cma_loopback_addr(const struct sockaddr *addr)
1148e51060f0SSean Hefty {
11492e2d190cSSean Hefty 	switch (addr->sa_family) {
11502e2d190cSSean Hefty 	case AF_INET:
1151ca3a8aceSParav Pandit 		return ipv4_is_loopback(
1152ca3a8aceSParav Pandit 			((struct sockaddr_in *)addr)->sin_addr.s_addr);
11532e2d190cSSean Hefty 	case AF_INET6:
1154ca3a8aceSParav Pandit 		return ipv6_addr_loopback(
1155ca3a8aceSParav Pandit 			&((struct sockaddr_in6 *)addr)->sin6_addr);
11562e2d190cSSean Hefty 	case AF_IB:
1157ca3a8aceSParav Pandit 		return ib_addr_loopback(
1158ca3a8aceSParav Pandit 			&((struct sockaddr_ib *)addr)->sib_addr);
11592e2d190cSSean Hefty 	default:
1160ca3a8aceSParav Pandit 		return false;
11612e2d190cSSean Hefty 	}
1162e51060f0SSean Hefty }
1163e51060f0SSean Hefty 
1164ca3a8aceSParav Pandit static inline bool cma_any_addr(const struct sockaddr *addr)
1165e51060f0SSean Hefty {
1166e51060f0SSean Hefty 	return cma_zero_addr(addr) || cma_loopback_addr(addr);
1167e51060f0SSean Hefty }
1168e51060f0SSean Hefty 
11695d7ed2f2SParav Pandit static int cma_addr_cmp(const struct sockaddr *src, const struct sockaddr *dst)
117043b752daSHefty, Sean {
117143b752daSHefty, Sean 	if (src->sa_family != dst->sa_family)
117243b752daSHefty, Sean 		return -1;
117343b752daSHefty, Sean 
117443b752daSHefty, Sean 	switch (src->sa_family) {
117543b752daSHefty, Sean 	case AF_INET:
117643b752daSHefty, Sean 		return ((struct sockaddr_in *)src)->sin_addr.s_addr !=
117743b752daSHefty, Sean 		       ((struct sockaddr_in *)dst)->sin_addr.s_addr;
11785d7ed2f2SParav Pandit 	case AF_INET6: {
11795d7ed2f2SParav Pandit 		struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src;
11805d7ed2f2SParav Pandit 		struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst;
11815d7ed2f2SParav Pandit 		bool link_local;
11825d7ed2f2SParav Pandit 
11835d7ed2f2SParav Pandit 		if (ipv6_addr_cmp(&src_addr6->sin6_addr,
11845d7ed2f2SParav Pandit 					  &dst_addr6->sin6_addr))
11855d7ed2f2SParav Pandit 			return 1;
11865d7ed2f2SParav Pandit 		link_local = ipv6_addr_type(&dst_addr6->sin6_addr) &
11875d7ed2f2SParav Pandit 			     IPV6_ADDR_LINKLOCAL;
11885d7ed2f2SParav Pandit 		/* Link local must match their scope_ids */
11895d7ed2f2SParav Pandit 		return link_local ? (src_addr6->sin6_scope_id !=
11905d7ed2f2SParav Pandit 				     dst_addr6->sin6_scope_id) :
11915d7ed2f2SParav Pandit 				    0;
11925d7ed2f2SParav Pandit 	}
11935d7ed2f2SParav Pandit 
11942e2d190cSSean Hefty 	default:
11952e2d190cSSean Hefty 		return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
11962e2d190cSSean Hefty 				   &((struct sockaddr_ib *) dst)->sib_addr);
119743b752daSHefty, Sean 	}
119843b752daSHefty, Sean }
119943b752daSHefty, Sean 
12002df7dba8SParav Pandit static __be16 cma_port(const struct sockaddr *addr)
1201628e5f6dSSean Hefty {
120258afdcb7SSean Hefty 	struct sockaddr_ib *sib;
120358afdcb7SSean Hefty 
120458afdcb7SSean Hefty 	switch (addr->sa_family) {
120558afdcb7SSean Hefty 	case AF_INET:
1206628e5f6dSSean Hefty 		return ((struct sockaddr_in *) addr)->sin_port;
120758afdcb7SSean Hefty 	case AF_INET6:
1208628e5f6dSSean Hefty 		return ((struct sockaddr_in6 *) addr)->sin6_port;
120958afdcb7SSean Hefty 	case AF_IB:
121058afdcb7SSean Hefty 		sib = (struct sockaddr_ib *) addr;
121158afdcb7SSean Hefty 		return htons((u16) (be64_to_cpu(sib->sib_sid) &
121258afdcb7SSean Hefty 				    be64_to_cpu(sib->sib_sid_mask)));
121358afdcb7SSean Hefty 	default:
121458afdcb7SSean Hefty 		return 0;
121558afdcb7SSean Hefty 	}
1216628e5f6dSSean Hefty }
1217628e5f6dSSean Hefty 
12182df7dba8SParav Pandit static inline int cma_any_port(const struct sockaddr *addr)
1219e51060f0SSean Hefty {
1220628e5f6dSSean Hefty 	return !cma_port(addr);
1221e51060f0SSean Hefty }
1222e51060f0SSean Hefty 
12230c505f70SHaggai Eran static void cma_save_ib_info(struct sockaddr *src_addr,
12240c505f70SHaggai Eran 			     struct sockaddr *dst_addr,
1225e7ff98aeSParav Pandit 			     const struct rdma_cm_id *listen_id,
1226e7ff98aeSParav Pandit 			     const struct sa_path_rec *path)
1227e51060f0SSean Hefty {
1228fbaa1a6dSSean Hefty 	struct sockaddr_ib *listen_ib, *ib;
1229e51060f0SSean Hefty 
1230fbaa1a6dSSean Hefty 	listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
12310c505f70SHaggai Eran 	if (src_addr) {
12320c505f70SHaggai Eran 		ib = (struct sockaddr_ib *)src_addr;
12330c505f70SHaggai Eran 		ib->sib_family = AF_IB;
1234c07678bbSMatthew Finlay 		if (path) {
1235fbaa1a6dSSean Hefty 			ib->sib_pkey = path->pkey;
1236fbaa1a6dSSean Hefty 			ib->sib_flowinfo = path->flow_label;
1237fbaa1a6dSSean Hefty 			memcpy(&ib->sib_addr, &path->sgid, 16);
1238d3957b86SMajd Dibbiny 			ib->sib_sid = path->service_id;
12390c505f70SHaggai Eran 			ib->sib_scope_id = 0;
1240c07678bbSMatthew Finlay 		} else {
1241c07678bbSMatthew Finlay 			ib->sib_pkey = listen_ib->sib_pkey;
1242c07678bbSMatthew Finlay 			ib->sib_flowinfo = listen_ib->sib_flowinfo;
1243c07678bbSMatthew Finlay 			ib->sib_addr = listen_ib->sib_addr;
1244fbaa1a6dSSean Hefty 			ib->sib_sid = listen_ib->sib_sid;
1245fbaa1a6dSSean Hefty 			ib->sib_scope_id = listen_ib->sib_scope_id;
12460c505f70SHaggai Eran 		}
12470c505f70SHaggai Eran 		ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
12480c505f70SHaggai Eran 	}
12490c505f70SHaggai Eran 	if (dst_addr) {
12500c505f70SHaggai Eran 		ib = (struct sockaddr_ib *)dst_addr;
12510c505f70SHaggai Eran 		ib->sib_family = AF_IB;
1252c07678bbSMatthew Finlay 		if (path) {
1253fbaa1a6dSSean Hefty 			ib->sib_pkey = path->pkey;
1254fbaa1a6dSSean Hefty 			ib->sib_flowinfo = path->flow_label;
1255fbaa1a6dSSean Hefty 			memcpy(&ib->sib_addr, &path->dgid, 16);
1256fbaa1a6dSSean Hefty 		}
1257c07678bbSMatthew Finlay 	}
125828521440SJason Gunthorpe }
125928521440SJason Gunthorpe 
1260c50e90d0SArnd Bergmann static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1261c50e90d0SArnd Bergmann 			      struct sockaddr_in *dst_addr,
12620c505f70SHaggai Eran 			      struct cma_hdr *hdr,
12630c505f70SHaggai Eran 			      __be16 local_port)
1264fbaa1a6dSSean Hefty {
12650c505f70SHaggai Eran 	if (src_addr) {
1266c50e90d0SArnd Bergmann 		*src_addr = (struct sockaddr_in) {
1267c50e90d0SArnd Bergmann 			.sin_family = AF_INET,
1268c50e90d0SArnd Bergmann 			.sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1269c50e90d0SArnd Bergmann 			.sin_port = local_port,
1270c50e90d0SArnd Bergmann 		};
12710c505f70SHaggai Eran 	}
1272fbaa1a6dSSean Hefty 
12730c505f70SHaggai Eran 	if (dst_addr) {
1274c50e90d0SArnd Bergmann 		*dst_addr = (struct sockaddr_in) {
1275c50e90d0SArnd Bergmann 			.sin_family = AF_INET,
1276c50e90d0SArnd Bergmann 			.sin_addr.s_addr = hdr->src_addr.ip4.addr,
1277c50e90d0SArnd Bergmann 			.sin_port = hdr->port,
1278c50e90d0SArnd Bergmann 		};
1279fbaa1a6dSSean Hefty 	}
12800c505f70SHaggai Eran }
1281fbaa1a6dSSean Hefty 
1282c50e90d0SArnd Bergmann static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1283c50e90d0SArnd Bergmann 			      struct sockaddr_in6 *dst_addr,
12840c505f70SHaggai Eran 			      struct cma_hdr *hdr,
12850c505f70SHaggai Eran 			      __be16 local_port)
1286fbaa1a6dSSean Hefty {
12870c505f70SHaggai Eran 	if (src_addr) {
1288c50e90d0SArnd Bergmann 		*src_addr = (struct sockaddr_in6) {
1289c50e90d0SArnd Bergmann 			.sin6_family = AF_INET6,
1290c50e90d0SArnd Bergmann 			.sin6_addr = hdr->dst_addr.ip6,
1291c50e90d0SArnd Bergmann 			.sin6_port = local_port,
1292c50e90d0SArnd Bergmann 		};
12930c505f70SHaggai Eran 	}
1294fbaa1a6dSSean Hefty 
12950c505f70SHaggai Eran 	if (dst_addr) {
1296c50e90d0SArnd Bergmann 		*dst_addr = (struct sockaddr_in6) {
1297c50e90d0SArnd Bergmann 			.sin6_family = AF_INET6,
1298c50e90d0SArnd Bergmann 			.sin6_addr = hdr->src_addr.ip6,
1299c50e90d0SArnd Bergmann 			.sin6_port = hdr->port,
1300c50e90d0SArnd Bergmann 		};
1301fbaa1a6dSSean Hefty 	}
13020c505f70SHaggai Eran }
1303fbaa1a6dSSean Hefty 
13040c505f70SHaggai Eran static u16 cma_port_from_service_id(__be64 service_id)
13050c505f70SHaggai Eran {
13060c505f70SHaggai Eran 	return (u16)be64_to_cpu(service_id);
13070c505f70SHaggai Eran }
13080c505f70SHaggai Eran 
13090c505f70SHaggai Eran static int cma_save_ip_info(struct sockaddr *src_addr,
13100c505f70SHaggai Eran 			    struct sockaddr *dst_addr,
1311e7ff98aeSParav Pandit 			    const struct ib_cm_event *ib_event,
13120c505f70SHaggai Eran 			    __be64 service_id)
1313fbaa1a6dSSean Hefty {
1314fbaa1a6dSSean Hefty 	struct cma_hdr *hdr;
13150c505f70SHaggai Eran 	__be16 port;
1316e51060f0SSean Hefty 
1317fbaa1a6dSSean Hefty 	hdr = ib_event->private_data;
1318fbaa1a6dSSean Hefty 	if (hdr->cma_version != CMA_VERSION)
1319fbaa1a6dSSean Hefty 		return -EINVAL;
1320e51060f0SSean Hefty 
13210c505f70SHaggai Eran 	port = htons(cma_port_from_service_id(service_id));
13220c505f70SHaggai Eran 
1323fbaa1a6dSSean Hefty 	switch (cma_get_ip_ver(hdr)) {
1324e51060f0SSean Hefty 	case 4:
1325c50e90d0SArnd Bergmann 		cma_save_ip4_info((struct sockaddr_in *)src_addr,
1326c50e90d0SArnd Bergmann 				  (struct sockaddr_in *)dst_addr, hdr, port);
1327e51060f0SSean Hefty 		break;
1328e51060f0SSean Hefty 	case 6:
1329c50e90d0SArnd Bergmann 		cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1330c50e90d0SArnd Bergmann 				  (struct sockaddr_in6 *)dst_addr, hdr, port);
1331e51060f0SSean Hefty 		break;
1332e51060f0SSean Hefty 	default:
13334c21b5bcSHaggai Eran 		return -EAFNOSUPPORT;
1334e51060f0SSean Hefty 	}
13350c505f70SHaggai Eran 
1336fbaa1a6dSSean Hefty 	return 0;
1337e51060f0SSean Hefty }
1338e51060f0SSean Hefty 
13390c505f70SHaggai Eran static int cma_save_net_info(struct sockaddr *src_addr,
13400c505f70SHaggai Eran 			     struct sockaddr *dst_addr,
1341e7ff98aeSParav Pandit 			     const struct rdma_cm_id *listen_id,
1342e7ff98aeSParav Pandit 			     const struct ib_cm_event *ib_event,
13430c505f70SHaggai Eran 			     sa_family_t sa_family, __be64 service_id)
13440c505f70SHaggai Eran {
13450c505f70SHaggai Eran 	if (sa_family == AF_IB) {
13460c505f70SHaggai Eran 		if (ib_event->event == IB_CM_REQ_RECEIVED)
13470c505f70SHaggai Eran 			cma_save_ib_info(src_addr, dst_addr, listen_id,
13480c505f70SHaggai Eran 					 ib_event->param.req_rcvd.primary_path);
13490c505f70SHaggai Eran 		else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
13500c505f70SHaggai Eran 			cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
13510c505f70SHaggai Eran 		return 0;
13520c505f70SHaggai Eran 	}
13530c505f70SHaggai Eran 
13540c505f70SHaggai Eran 	return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
13550c505f70SHaggai Eran }
13560c505f70SHaggai Eran 
13574c21b5bcSHaggai Eran static int cma_save_req_info(const struct ib_cm_event *ib_event,
13584c21b5bcSHaggai Eran 			     struct cma_req_info *req)
13594c21b5bcSHaggai Eran {
13604c21b5bcSHaggai Eran 	const struct ib_cm_req_event_param *req_param =
13614c21b5bcSHaggai Eran 		&ib_event->param.req_rcvd;
13624c21b5bcSHaggai Eran 	const struct ib_cm_sidr_req_event_param *sidr_param =
13634c21b5bcSHaggai Eran 		&ib_event->param.sidr_req_rcvd;
13644c21b5bcSHaggai Eran 
13654c21b5bcSHaggai Eran 	switch (ib_event->event) {
13664c21b5bcSHaggai Eran 	case IB_CM_REQ_RECEIVED:
13674c21b5bcSHaggai Eran 		req->device	= req_param->listen_id->device;
13684c21b5bcSHaggai Eran 		req->port	= req_param->port;
13694c21b5bcSHaggai Eran 		memcpy(&req->local_gid, &req_param->primary_path->sgid,
13704c21b5bcSHaggai Eran 		       sizeof(req->local_gid));
13714c21b5bcSHaggai Eran 		req->has_gid	= true;
1372d3957b86SMajd Dibbiny 		req->service_id = req_param->primary_path->service_id;
1373ab3964adSHaggai Eran 		req->pkey	= be16_to_cpu(req_param->primary_path->pkey);
137484424a7fSHaggai Eran 		if (req->pkey != req_param->bth_pkey)
137584424a7fSHaggai Eran 			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
137684424a7fSHaggai Eran 					    "RDMA CMA: in the future this may cause the request to be dropped\n",
137784424a7fSHaggai Eran 					    req_param->bth_pkey, req->pkey);
13784c21b5bcSHaggai Eran 		break;
13794c21b5bcSHaggai Eran 	case IB_CM_SIDR_REQ_RECEIVED:
13804c21b5bcSHaggai Eran 		req->device	= sidr_param->listen_id->device;
13814c21b5bcSHaggai Eran 		req->port	= sidr_param->port;
13824c21b5bcSHaggai Eran 		req->has_gid	= false;
13834c21b5bcSHaggai Eran 		req->service_id	= sidr_param->service_id;
1384ab3964adSHaggai Eran 		req->pkey	= sidr_param->pkey;
138584424a7fSHaggai Eran 		if (req->pkey != sidr_param->bth_pkey)
138684424a7fSHaggai Eran 			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
138784424a7fSHaggai Eran 					    "RDMA CMA: in the future this may cause the request to be dropped\n",
138884424a7fSHaggai Eran 					    sidr_param->bth_pkey, req->pkey);
13894c21b5bcSHaggai Eran 		break;
13904c21b5bcSHaggai Eran 	default:
13914c21b5bcSHaggai Eran 		return -EINVAL;
13924c21b5bcSHaggai Eran 	}
13934c21b5bcSHaggai Eran 
13944c21b5bcSHaggai Eran 	return 0;
13954c21b5bcSHaggai Eran }
13964c21b5bcSHaggai Eran 
1397f887f2acSHaggai Eran static bool validate_ipv4_net_dev(struct net_device *net_dev,
1398f887f2acSHaggai Eran 				  const struct sockaddr_in *dst_addr,
1399f887f2acSHaggai Eran 				  const struct sockaddr_in *src_addr)
1400f887f2acSHaggai Eran {
1401f887f2acSHaggai Eran 	__be32 daddr = dst_addr->sin_addr.s_addr,
1402f887f2acSHaggai Eran 	       saddr = src_addr->sin_addr.s_addr;
1403f887f2acSHaggai Eran 	struct fib_result res;
1404f887f2acSHaggai Eran 	struct flowi4 fl4;
1405f887f2acSHaggai Eran 	int err;
1406f887f2acSHaggai Eran 	bool ret;
1407f887f2acSHaggai Eran 
1408f887f2acSHaggai Eran 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1409f887f2acSHaggai Eran 	    ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1410f887f2acSHaggai Eran 	    ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1411f887f2acSHaggai Eran 	    ipv4_is_loopback(saddr))
1412f887f2acSHaggai Eran 		return false;
1413f887f2acSHaggai Eran 
1414f887f2acSHaggai Eran 	memset(&fl4, 0, sizeof(fl4));
1415f887f2acSHaggai Eran 	fl4.flowi4_iif = net_dev->ifindex;
1416f887f2acSHaggai Eran 	fl4.daddr = daddr;
1417f887f2acSHaggai Eran 	fl4.saddr = saddr;
1418f887f2acSHaggai Eran 
1419f887f2acSHaggai Eran 	rcu_read_lock();
1420f887f2acSHaggai Eran 	err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1421d3632493SBart Van Assche 	ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1422f887f2acSHaggai Eran 	rcu_read_unlock();
1423f887f2acSHaggai Eran 
1424f887f2acSHaggai Eran 	return ret;
1425f887f2acSHaggai Eran }
1426f887f2acSHaggai Eran 
1427f887f2acSHaggai Eran static bool validate_ipv6_net_dev(struct net_device *net_dev,
1428f887f2acSHaggai Eran 				  const struct sockaddr_in6 *dst_addr,
1429f887f2acSHaggai Eran 				  const struct sockaddr_in6 *src_addr)
1430f887f2acSHaggai Eran {
1431f887f2acSHaggai Eran #if IS_ENABLED(CONFIG_IPV6)
1432f887f2acSHaggai Eran 	const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1433f887f2acSHaggai Eran 			   IPV6_ADDR_LINKLOCAL;
1434f887f2acSHaggai Eran 	struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1435f887f2acSHaggai Eran 					 &src_addr->sin6_addr, net_dev->ifindex,
1436b75cc8f9SDavid Ahern 					 NULL, strict);
1437f887f2acSHaggai Eran 	bool ret;
1438f887f2acSHaggai Eran 
1439f887f2acSHaggai Eran 	if (!rt)
1440f887f2acSHaggai Eran 		return false;
1441f887f2acSHaggai Eran 
1442f887f2acSHaggai Eran 	ret = rt->rt6i_idev->dev == net_dev;
1443f887f2acSHaggai Eran 	ip6_rt_put(rt);
1444f887f2acSHaggai Eran 
1445f887f2acSHaggai Eran 	return ret;
1446f887f2acSHaggai Eran #else
1447f887f2acSHaggai Eran 	return false;
1448f887f2acSHaggai Eran #endif
1449f887f2acSHaggai Eran }
1450f887f2acSHaggai Eran 
1451f887f2acSHaggai Eran static bool validate_net_dev(struct net_device *net_dev,
1452f887f2acSHaggai Eran 			     const struct sockaddr *daddr,
1453f887f2acSHaggai Eran 			     const struct sockaddr *saddr)
1454f887f2acSHaggai Eran {
1455f887f2acSHaggai Eran 	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1456f887f2acSHaggai Eran 	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1457f887f2acSHaggai Eran 	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1458f887f2acSHaggai Eran 	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1459f887f2acSHaggai Eran 
1460f887f2acSHaggai Eran 	switch (daddr->sa_family) {
1461f887f2acSHaggai Eran 	case AF_INET:
1462f887f2acSHaggai Eran 		return saddr->sa_family == AF_INET &&
1463f887f2acSHaggai Eran 		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1464f887f2acSHaggai Eran 
1465f887f2acSHaggai Eran 	case AF_INET6:
1466f887f2acSHaggai Eran 		return saddr->sa_family == AF_INET6 &&
1467f887f2acSHaggai Eran 		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1468f887f2acSHaggai Eran 
1469f887f2acSHaggai Eran 	default:
1470f887f2acSHaggai Eran 		return false;
1471f887f2acSHaggai Eran 	}
1472f887f2acSHaggai Eran }
1473f887f2acSHaggai Eran 
1474cee10433SParav Pandit static struct net_device *
1475cee10433SParav Pandit roce_get_net_dev_by_cm_event(const struct ib_cm_event *ib_event)
1476cee10433SParav Pandit {
1477cee10433SParav Pandit 	const struct ib_gid_attr *sgid_attr = NULL;
1478adb4a57aSParav Pandit 	struct net_device *ndev;
1479cee10433SParav Pandit 
1480cee10433SParav Pandit 	if (ib_event->event == IB_CM_REQ_RECEIVED)
1481cee10433SParav Pandit 		sgid_attr = ib_event->param.req_rcvd.ppath_sgid_attr;
1482cee10433SParav Pandit 	else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1483cee10433SParav Pandit 		sgid_attr = ib_event->param.sidr_req_rcvd.sgid_attr;
1484cee10433SParav Pandit 
1485cee10433SParav Pandit 	if (!sgid_attr)
1486cee10433SParav Pandit 		return NULL;
1487adb4a57aSParav Pandit 
1488adb4a57aSParav Pandit 	rcu_read_lock();
1489adb4a57aSParav Pandit 	ndev = rdma_read_gid_attr_ndev_rcu(sgid_attr);
1490adb4a57aSParav Pandit 	if (IS_ERR(ndev))
1491adb4a57aSParav Pandit 		ndev = NULL;
1492adb4a57aSParav Pandit 	else
1493adb4a57aSParav Pandit 		dev_hold(ndev);
1494adb4a57aSParav Pandit 	rcu_read_unlock();
1495adb4a57aSParav Pandit 	return ndev;
1496cee10433SParav Pandit }
1497cee10433SParav Pandit 
1498e7ff98aeSParav Pandit static struct net_device *cma_get_net_dev(const struct ib_cm_event *ib_event,
14992918c1a9SParav Pandit 					  struct cma_req_info *req)
15004c21b5bcSHaggai Eran {
15012918c1a9SParav Pandit 	struct sockaddr *listen_addr =
15022918c1a9SParav Pandit 			(struct sockaddr *)&req->listen_addr_storage;
15032918c1a9SParav Pandit 	struct sockaddr *src_addr = (struct sockaddr *)&req->src_addr_storage;
15044c21b5bcSHaggai Eran 	struct net_device *net_dev;
15054c21b5bcSHaggai Eran 	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
15064c21b5bcSHaggai Eran 	int err;
15074c21b5bcSHaggai Eran 
1508f887f2acSHaggai Eran 	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1509f887f2acSHaggai Eran 			       req->service_id);
15104c21b5bcSHaggai Eran 	if (err)
15114c21b5bcSHaggai Eran 		return ERR_PTR(err);
15124c21b5bcSHaggai Eran 
1513cee10433SParav Pandit 	if (rdma_protocol_roce(req->device, req->port))
1514cee10433SParav Pandit 		net_dev = roce_get_net_dev_by_cm_event(ib_event);
1515cee10433SParav Pandit 	else
1516cee10433SParav Pandit 		net_dev = ib_get_net_dev_by_params(req->device, req->port,
1517cee10433SParav Pandit 						   req->pkey,
15184c21b5bcSHaggai Eran 						   gid, listen_addr);
15194c21b5bcSHaggai Eran 	if (!net_dev)
15204c21b5bcSHaggai Eran 		return ERR_PTR(-ENODEV);
15214c21b5bcSHaggai Eran 
15224c21b5bcSHaggai Eran 	return net_dev;
15234c21b5bcSHaggai Eran }
15244c21b5bcSHaggai Eran 
15252253fc0cSSteve Wise static enum rdma_ucm_port_space rdma_ps_from_service_id(__be64 service_id)
15264c21b5bcSHaggai Eran {
15274c21b5bcSHaggai Eran 	return (be64_to_cpu(service_id) >> 16) & 0xffff;
15284c21b5bcSHaggai Eran }
15294c21b5bcSHaggai Eran 
15304c21b5bcSHaggai Eran static bool cma_match_private_data(struct rdma_id_private *id_priv,
15314c21b5bcSHaggai Eran 				   const struct cma_hdr *hdr)
15324c21b5bcSHaggai Eran {
15334c21b5bcSHaggai Eran 	struct sockaddr *addr = cma_src_addr(id_priv);
15344c21b5bcSHaggai Eran 	__be32 ip4_addr;
15354c21b5bcSHaggai Eran 	struct in6_addr ip6_addr;
15364c21b5bcSHaggai Eran 
15374c21b5bcSHaggai Eran 	if (cma_any_addr(addr) && !id_priv->afonly)
15384c21b5bcSHaggai Eran 		return true;
15394c21b5bcSHaggai Eran 
15404c21b5bcSHaggai Eran 	switch (addr->sa_family) {
15414c21b5bcSHaggai Eran 	case AF_INET:
15424c21b5bcSHaggai Eran 		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
15434c21b5bcSHaggai Eran 		if (cma_get_ip_ver(hdr) != 4)
15444c21b5bcSHaggai Eran 			return false;
15454c21b5bcSHaggai Eran 		if (!cma_any_addr(addr) &&
15464c21b5bcSHaggai Eran 		    hdr->dst_addr.ip4.addr != ip4_addr)
15474c21b5bcSHaggai Eran 			return false;
15484c21b5bcSHaggai Eran 		break;
15494c21b5bcSHaggai Eran 	case AF_INET6:
15504c21b5bcSHaggai Eran 		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
15514c21b5bcSHaggai Eran 		if (cma_get_ip_ver(hdr) != 6)
15524c21b5bcSHaggai Eran 			return false;
15534c21b5bcSHaggai Eran 		if (!cma_any_addr(addr) &&
15544c21b5bcSHaggai Eran 		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
15554c21b5bcSHaggai Eran 			return false;
15564c21b5bcSHaggai Eran 		break;
15574c21b5bcSHaggai Eran 	case AF_IB:
15584c21b5bcSHaggai Eran 		return true;
15594c21b5bcSHaggai Eran 	default:
15604c21b5bcSHaggai Eran 		return false;
15614c21b5bcSHaggai Eran 	}
15624c21b5bcSHaggai Eran 
15634c21b5bcSHaggai Eran 	return true;
15644c21b5bcSHaggai Eran }
15654c21b5bcSHaggai Eran 
1566b8cab5daSHaggai Eran static bool cma_protocol_roce(const struct rdma_cm_id *id)
1567b8cab5daSHaggai Eran {
1568b8cab5daSHaggai Eran 	struct ib_device *device = id->device;
15691fb7f897SMark Bloch 	const u32 port_num = id->port_num ?: rdma_start_port(device);
1570b8cab5daSHaggai Eran 
15715ac08a34SParav Pandit 	return rdma_protocol_roce(device, port_num);
1572b8cab5daSHaggai Eran }
1573b8cab5daSHaggai Eran 
157478fb282bSParav Pandit static bool cma_is_req_ipv6_ll(const struct cma_req_info *req)
157578fb282bSParav Pandit {
157678fb282bSParav Pandit 	const struct sockaddr *daddr =
157778fb282bSParav Pandit 			(const struct sockaddr *)&req->listen_addr_storage;
157878fb282bSParav Pandit 	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
157978fb282bSParav Pandit 
158078fb282bSParav Pandit 	/* Returns true if the req is for IPv6 link local */
158178fb282bSParav Pandit 	return (daddr->sa_family == AF_INET6 &&
158278fb282bSParav Pandit 		(ipv6_addr_type(&daddr6->sin6_addr) & IPV6_ADDR_LINKLOCAL));
158378fb282bSParav Pandit }
158478fb282bSParav Pandit 
1585fac51590SMatan Barak static bool cma_match_net_dev(const struct rdma_cm_id *id,
1586fac51590SMatan Barak 			      const struct net_device *net_dev,
158778fb282bSParav Pandit 			      const struct cma_req_info *req)
15884c21b5bcSHaggai Eran {
1589fac51590SMatan Barak 	const struct rdma_addr *addr = &id->route.addr;
15904c21b5bcSHaggai Eran 
15914c21b5bcSHaggai Eran 	if (!net_dev)
1592d274e45cSParav Pandit 		/* This request is an AF_IB request */
159378fb282bSParav Pandit 		return (!id->port_num || id->port_num == req->port) &&
1594d274e45cSParav Pandit 		       (addr->src_addr.ss_family == AF_IB);
15954c21b5bcSHaggai Eran 
1596643d213aSParav Pandit 	/*
159778fb282bSParav Pandit 	 * If the request is not for IPv6 link local, allow matching
159878fb282bSParav Pandit 	 * request to any netdevice of the one or multiport rdma device.
159978fb282bSParav Pandit 	 */
160078fb282bSParav Pandit 	if (!cma_is_req_ipv6_ll(req))
160178fb282bSParav Pandit 		return true;
160278fb282bSParav Pandit 	/*
1603643d213aSParav Pandit 	 * Net namespaces must match, and if the listner is listening
1604643d213aSParav Pandit 	 * on a specific netdevice than netdevice must match as well.
1605643d213aSParav Pandit 	 */
1606643d213aSParav Pandit 	if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1607643d213aSParav Pandit 	    (!!addr->dev_addr.bound_dev_if ==
1608643d213aSParav Pandit 	     (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1609643d213aSParav Pandit 		return true;
1610643d213aSParav Pandit 	else
1611643d213aSParav Pandit 		return false;
16124c21b5bcSHaggai Eran }
16134c21b5bcSHaggai Eran 
16144c21b5bcSHaggai Eran static struct rdma_id_private *cma_find_listener(
16154c21b5bcSHaggai Eran 		const struct rdma_bind_list *bind_list,
16164c21b5bcSHaggai Eran 		const struct ib_cm_id *cm_id,
16174c21b5bcSHaggai Eran 		const struct ib_cm_event *ib_event,
16184c21b5bcSHaggai Eran 		const struct cma_req_info *req,
16194c21b5bcSHaggai Eran 		const struct net_device *net_dev)
16204c21b5bcSHaggai Eran {
16214c21b5bcSHaggai Eran 	struct rdma_id_private *id_priv, *id_priv_dev;
16224c21b5bcSHaggai Eran 
1623730c8912SMark Zhang 	lockdep_assert_held(&lock);
1624730c8912SMark Zhang 
16254c21b5bcSHaggai Eran 	if (!bind_list)
16264c21b5bcSHaggai Eran 		return ERR_PTR(-EINVAL);
16274c21b5bcSHaggai Eran 
16284c21b5bcSHaggai Eran 	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
16294c21b5bcSHaggai Eran 		if (cma_match_private_data(id_priv, ib_event->private_data)) {
16304c21b5bcSHaggai Eran 			if (id_priv->id.device == cm_id->device &&
163178fb282bSParav Pandit 			    cma_match_net_dev(&id_priv->id, net_dev, req))
16324c21b5bcSHaggai Eran 				return id_priv;
16334c21b5bcSHaggai Eran 			list_for_each_entry(id_priv_dev,
16344c21b5bcSHaggai Eran 					    &id_priv->listen_list,
16354c21b5bcSHaggai Eran 					    listen_list) {
16364c21b5bcSHaggai Eran 				if (id_priv_dev->id.device == cm_id->device &&
163778fb282bSParav Pandit 				    cma_match_net_dev(&id_priv_dev->id,
163878fb282bSParav Pandit 						      net_dev, req))
16394c21b5bcSHaggai Eran 					return id_priv_dev;
16404c21b5bcSHaggai Eran 			}
16414c21b5bcSHaggai Eran 		}
16424c21b5bcSHaggai Eran 	}
16434c21b5bcSHaggai Eran 
16444c21b5bcSHaggai Eran 	return ERR_PTR(-EINVAL);
16454c21b5bcSHaggai Eran }
16464c21b5bcSHaggai Eran 
1647e7ff98aeSParav Pandit static struct rdma_id_private *
164885463316SParav Pandit cma_ib_id_from_event(struct ib_cm_id *cm_id,
1649e7ff98aeSParav Pandit 		     const struct ib_cm_event *ib_event,
165041ab1cb7SParav Pandit 		     struct cma_req_info *req,
16510b3ca768SHaggai Eran 		     struct net_device **net_dev)
16524c21b5bcSHaggai Eran {
16534c21b5bcSHaggai Eran 	struct rdma_bind_list *bind_list;
16544c21b5bcSHaggai Eran 	struct rdma_id_private *id_priv;
16554c21b5bcSHaggai Eran 	int err;
16564c21b5bcSHaggai Eran 
165741ab1cb7SParav Pandit 	err = cma_save_req_info(ib_event, req);
16584c21b5bcSHaggai Eran 	if (err)
16594c21b5bcSHaggai Eran 		return ERR_PTR(err);
16604c21b5bcSHaggai Eran 
166141ab1cb7SParav Pandit 	*net_dev = cma_get_net_dev(ib_event, req);
16620b3ca768SHaggai Eran 	if (IS_ERR(*net_dev)) {
16630b3ca768SHaggai Eran 		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
16644c21b5bcSHaggai Eran 			/* Assuming the protocol is AF_IB */
16650b3ca768SHaggai Eran 			*net_dev = NULL;
16664c21b5bcSHaggai Eran 		} else {
16670b3ca768SHaggai Eran 			return ERR_CAST(*net_dev);
16684c21b5bcSHaggai Eran 		}
16694c21b5bcSHaggai Eran 	}
16704c21b5bcSHaggai Eran 
1671730c8912SMark Zhang 	mutex_lock(&lock);
16722918c1a9SParav Pandit 	/*
16732918c1a9SParav Pandit 	 * Net namespace might be getting deleted while route lookup,
16742918c1a9SParav Pandit 	 * cm_id lookup is in progress. Therefore, perform netdevice
16752918c1a9SParav Pandit 	 * validation, cm_id lookup under rcu lock.
16762918c1a9SParav Pandit 	 * RCU lock along with netdevice state check, synchronizes with
16772918c1a9SParav Pandit 	 * netdevice migrating to different net namespace and also avoids
16782918c1a9SParav Pandit 	 * case where net namespace doesn't get deleted while lookup is in
16792918c1a9SParav Pandit 	 * progress.
16802918c1a9SParav Pandit 	 * If the device state is not IFF_UP, its properties such as ifindex
16812918c1a9SParav Pandit 	 * and nd_net cannot be trusted to remain valid without rcu lock.
16822918c1a9SParav Pandit 	 * net/core/dev.c change_net_namespace() ensures to synchronize with
16832918c1a9SParav Pandit 	 * ongoing operations on net device after device is closed using
16842918c1a9SParav Pandit 	 * synchronize_net().
16852918c1a9SParav Pandit 	 */
16862918c1a9SParav Pandit 	rcu_read_lock();
16872918c1a9SParav Pandit 	if (*net_dev) {
16882918c1a9SParav Pandit 		/*
16892918c1a9SParav Pandit 		 * If netdevice is down, it is likely that it is administratively
16902918c1a9SParav Pandit 		 * down or it might be migrating to different namespace.
16912918c1a9SParav Pandit 		 * In that case avoid further processing, as the net namespace
16922918c1a9SParav Pandit 		 * or ifindex may change.
16932918c1a9SParav Pandit 		 */
16942918c1a9SParav Pandit 		if (((*net_dev)->flags & IFF_UP) == 0) {
16952918c1a9SParav Pandit 			id_priv = ERR_PTR(-EHOSTUNREACH);
16962918c1a9SParav Pandit 			goto err;
16972918c1a9SParav Pandit 		}
16982918c1a9SParav Pandit 
16992918c1a9SParav Pandit 		if (!validate_net_dev(*net_dev,
170041ab1cb7SParav Pandit 				 (struct sockaddr *)&req->listen_addr_storage,
170141ab1cb7SParav Pandit 				 (struct sockaddr *)&req->src_addr_storage)) {
17022918c1a9SParav Pandit 			id_priv = ERR_PTR(-EHOSTUNREACH);
17032918c1a9SParav Pandit 			goto err;
17042918c1a9SParav Pandit 		}
17052918c1a9SParav Pandit 	}
17062918c1a9SParav Pandit 
1707fa20105eSGuy Shapiro 	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
170841ab1cb7SParav Pandit 				rdma_ps_from_service_id(req->service_id),
170941ab1cb7SParav Pandit 				cma_port_from_service_id(req->service_id));
171041ab1cb7SParav Pandit 	id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev);
17112918c1a9SParav Pandit err:
17122918c1a9SParav Pandit 	rcu_read_unlock();
1713730c8912SMark Zhang 	mutex_unlock(&lock);
1714b3b51f9fSHaggai Eran 	if (IS_ERR(id_priv) && *net_dev) {
1715be688195SHaggai Eran 		dev_put(*net_dev);
1716be688195SHaggai Eran 		*net_dev = NULL;
1717be688195SHaggai Eran 	}
17184c21b5bcSHaggai Eran 	return id_priv;
17194c21b5bcSHaggai Eran }
17204c21b5bcSHaggai Eran 
1721c0b64f58SBart Van Assche static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1722e51060f0SSean Hefty {
1723e8160e15SSean Hefty 	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1724e51060f0SSean Hefty }
1725e51060f0SSean Hefty 
1726e51060f0SSean Hefty static void cma_cancel_route(struct rdma_id_private *id_priv)
1727e51060f0SSean Hefty {
1728fe53ba2fSMichael Wang 	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1729e51060f0SSean Hefty 		if (id_priv->query)
1730e51060f0SSean Hefty 			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1731e51060f0SSean Hefty 	}
1732e51060f0SSean Hefty }
1733e51060f0SSean Hefty 
1734e51060f0SSean Hefty static void cma_cancel_listens(struct rdma_id_private *id_priv)
1735e51060f0SSean Hefty {
1736e51060f0SSean Hefty 	struct rdma_id_private *dev_id_priv;
1737e51060f0SSean Hefty 
1738d02d1f53SSean Hefty 	/*
1739d02d1f53SSean Hefty 	 * Remove from listen_any_list to prevent added devices from spawning
1740d02d1f53SSean Hefty 	 * additional listen requests.
1741d02d1f53SSean Hefty 	 */
1742e51060f0SSean Hefty 	mutex_lock(&lock);
1743e51060f0SSean Hefty 	list_del(&id_priv->list);
1744e51060f0SSean Hefty 
1745e51060f0SSean Hefty 	while (!list_empty(&id_priv->listen_list)) {
1746e51060f0SSean Hefty 		dev_id_priv = list_entry(id_priv->listen_list.next,
1747e51060f0SSean Hefty 					 struct rdma_id_private, listen_list);
1748d02d1f53SSean Hefty 		/* sync with device removal to avoid duplicate destruction */
1749d02d1f53SSean Hefty 		list_del_init(&dev_id_priv->list);
1750d02d1f53SSean Hefty 		list_del(&dev_id_priv->listen_list);
1751d02d1f53SSean Hefty 		mutex_unlock(&lock);
1752d02d1f53SSean Hefty 
1753d02d1f53SSean Hefty 		rdma_destroy_id(&dev_id_priv->id);
1754d02d1f53SSean Hefty 		mutex_lock(&lock);
1755e51060f0SSean Hefty 	}
1756e51060f0SSean Hefty 	mutex_unlock(&lock);
1757e51060f0SSean Hefty }
1758e51060f0SSean Hefty 
1759e51060f0SSean Hefty static void cma_cancel_operation(struct rdma_id_private *id_priv,
1760550e5ca7SNir Muchtar 				 enum rdma_cm_state state)
1761e51060f0SSean Hefty {
1762e51060f0SSean Hefty 	switch (state) {
1763550e5ca7SNir Muchtar 	case RDMA_CM_ADDR_QUERY:
1764e51060f0SSean Hefty 		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1765e51060f0SSean Hefty 		break;
1766550e5ca7SNir Muchtar 	case RDMA_CM_ROUTE_QUERY:
1767e51060f0SSean Hefty 		cma_cancel_route(id_priv);
1768e51060f0SSean Hefty 		break;
1769550e5ca7SNir Muchtar 	case RDMA_CM_LISTEN:
1770f4753834SSean Hefty 		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1771e51060f0SSean Hefty 			cma_cancel_listens(id_priv);
1772e51060f0SSean Hefty 		break;
1773e51060f0SSean Hefty 	default:
1774e51060f0SSean Hefty 		break;
1775e51060f0SSean Hefty 	}
1776e51060f0SSean Hefty }
1777e51060f0SSean Hefty 
1778e51060f0SSean Hefty static void cma_release_port(struct rdma_id_private *id_priv)
1779e51060f0SSean Hefty {
1780e51060f0SSean Hefty 	struct rdma_bind_list *bind_list = id_priv->bind_list;
1781fa20105eSGuy Shapiro 	struct net *net = id_priv->id.route.addr.dev_addr.net;
1782e51060f0SSean Hefty 
1783e51060f0SSean Hefty 	if (!bind_list)
1784e51060f0SSean Hefty 		return;
1785e51060f0SSean Hefty 
1786e51060f0SSean Hefty 	mutex_lock(&lock);
1787e51060f0SSean Hefty 	hlist_del(&id_priv->node);
1788e51060f0SSean Hefty 	if (hlist_empty(&bind_list->owners)) {
1789fa20105eSGuy Shapiro 		cma_ps_remove(net, bind_list->ps, bind_list->port);
1790e51060f0SSean Hefty 		kfree(bind_list);
1791e51060f0SSean Hefty 	}
1792e51060f0SSean Hefty 	mutex_unlock(&lock);
1793e51060f0SSean Hefty }
1794e51060f0SSean Hefty 
17953788d299SJason Gunthorpe static void destroy_mc(struct rdma_id_private *id_priv,
179688145678SParav Pandit 		       struct cma_multicast *mc)
179788145678SParav Pandit {
1798b5de0c60SJason Gunthorpe 	if (rdma_cap_ib_mcast(id_priv->id.device, id_priv->id.port_num))
1799b5de0c60SJason Gunthorpe 		ib_sa_free_multicast(mc->sa_mc);
18003788d299SJason Gunthorpe 
1801b5de0c60SJason Gunthorpe 	if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
18023788d299SJason Gunthorpe 		struct rdma_dev_addr *dev_addr =
18033788d299SJason Gunthorpe 			&id_priv->id.route.addr.dev_addr;
180488145678SParav Pandit 		struct net_device *ndev = NULL;
180588145678SParav Pandit 
180688145678SParav Pandit 		if (dev_addr->bound_dev_if)
18073788d299SJason Gunthorpe 			ndev = dev_get_by_index(dev_addr->net,
18083788d299SJason Gunthorpe 						dev_addr->bound_dev_if);
180988145678SParav Pandit 		if (ndev) {
1810b5de0c60SJason Gunthorpe 			union ib_gid mgid;
1811b5de0c60SJason Gunthorpe 
1812b5de0c60SJason Gunthorpe 			cma_set_mgid(id_priv, (struct sockaddr *)&mc->addr,
1813b5de0c60SJason Gunthorpe 				     &mgid);
1814b5de0c60SJason Gunthorpe 			cma_igmp_send(ndev, &mgid, false);
181588145678SParav Pandit 			dev_put(ndev);
181688145678SParav Pandit 		}
1817fe454dc3SAvihai Horon 
1818fe454dc3SAvihai Horon 		cancel_work_sync(&mc->iboe_join.work);
181988145678SParav Pandit 	}
1820b5de0c60SJason Gunthorpe 	kfree(mc);
182188145678SParav Pandit }
182288145678SParav Pandit 
1823c8f6a362SSean Hefty static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1824c8f6a362SSean Hefty {
1825c8f6a362SSean Hefty 	struct cma_multicast *mc;
1826c8f6a362SSean Hefty 
1827c8f6a362SSean Hefty 	while (!list_empty(&id_priv->mc_list)) {
18283788d299SJason Gunthorpe 		mc = list_first_entry(&id_priv->mc_list, struct cma_multicast,
18293788d299SJason Gunthorpe 				      list);
1830c8f6a362SSean Hefty 		list_del(&mc->list);
18313788d299SJason Gunthorpe 		destroy_mc(id_priv, mc);
1832c8f6a362SSean Hefty 	}
1833bee3c3c9SMoni Shoua }
1834c8f6a362SSean Hefty 
1835f6a9d47aSJason Gunthorpe static void _destroy_id(struct rdma_id_private *id_priv,
1836f6a9d47aSJason Gunthorpe 			enum rdma_cm_state state)
1837e51060f0SSean Hefty {
1838e51060f0SSean Hefty 	cma_cancel_operation(id_priv, state);
1839e51060f0SSean Hefty 
1840ed7a01fdSLeon Romanovsky 	if (id_priv->cma_dev) {
184172219ceaSMichael Wang 		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
18420c9361fcSJack Morgenstein 			if (id_priv->cm_id.ib)
1843e51060f0SSean Hefty 				ib_destroy_cm_id(id_priv->cm_id.ib);
184404215330SMichael Wang 		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
18450c9361fcSJack Morgenstein 			if (id_priv->cm_id.iw)
184607ebafbaSTom Tucker 				iw_destroy_cm_id(id_priv->cm_id.iw);
1847e51060f0SSean Hefty 		}
1848c8f6a362SSean Hefty 		cma_leave_mc_groups(id_priv);
1849889d916bSShay Drory 		rdma_restrack_del(&id_priv->res);
1850a396d43aSSean Hefty 		cma_release_dev(id_priv);
1851e51060f0SSean Hefty 	}
1852e51060f0SSean Hefty 
1853e51060f0SSean Hefty 	cma_release_port(id_priv);
1854e368d23fSParav Pandit 	cma_id_put(id_priv);
1855e51060f0SSean Hefty 	wait_for_completion(&id_priv->comp);
1856e51060f0SSean Hefty 
1857d02d1f53SSean Hefty 	if (id_priv->internal_id)
1858e368d23fSParav Pandit 		cma_id_put(id_priv->id.context);
1859d02d1f53SSean Hefty 
1860e51060f0SSean Hefty 	kfree(id_priv->id.route.path_rec);
18614ed13a5fSParav Pandit 
1862fa20105eSGuy Shapiro 	put_net(id_priv->id.route.addr.dev_addr.net);
1863e51060f0SSean Hefty 	kfree(id_priv);
1864e51060f0SSean Hefty }
1865f6a9d47aSJason Gunthorpe 
1866f6a9d47aSJason Gunthorpe /*
1867f6a9d47aSJason Gunthorpe  * destroy an ID from within the handler_mutex. This ensures that no other
1868f6a9d47aSJason Gunthorpe  * handlers can start running concurrently.
1869f6a9d47aSJason Gunthorpe  */
1870f6a9d47aSJason Gunthorpe static void destroy_id_handler_unlock(struct rdma_id_private *id_priv)
1871f6a9d47aSJason Gunthorpe 	__releases(&idprv->handler_mutex)
1872f6a9d47aSJason Gunthorpe {
1873f6a9d47aSJason Gunthorpe 	enum rdma_cm_state state;
1874f6a9d47aSJason Gunthorpe 	unsigned long flags;
1875f6a9d47aSJason Gunthorpe 
1876f6a9d47aSJason Gunthorpe 	trace_cm_id_destroy(id_priv);
1877f6a9d47aSJason Gunthorpe 
1878f6a9d47aSJason Gunthorpe 	/*
1879f6a9d47aSJason Gunthorpe 	 * Setting the state to destroyed under the handler mutex provides a
1880f6a9d47aSJason Gunthorpe 	 * fence against calling handler callbacks. If this is invoked due to
1881f6a9d47aSJason Gunthorpe 	 * the failure of a handler callback then it guarentees that no future
1882f6a9d47aSJason Gunthorpe 	 * handlers will be called.
1883f6a9d47aSJason Gunthorpe 	 */
1884f6a9d47aSJason Gunthorpe 	lockdep_assert_held(&id_priv->handler_mutex);
1885f6a9d47aSJason Gunthorpe 	spin_lock_irqsave(&id_priv->lock, flags);
1886f6a9d47aSJason Gunthorpe 	state = id_priv->state;
1887f6a9d47aSJason Gunthorpe 	id_priv->state = RDMA_CM_DESTROYING;
1888f6a9d47aSJason Gunthorpe 	spin_unlock_irqrestore(&id_priv->lock, flags);
1889f6a9d47aSJason Gunthorpe 	mutex_unlock(&id_priv->handler_mutex);
1890f6a9d47aSJason Gunthorpe 	_destroy_id(id_priv, state);
1891f6a9d47aSJason Gunthorpe }
1892f6a9d47aSJason Gunthorpe 
1893f6a9d47aSJason Gunthorpe void rdma_destroy_id(struct rdma_cm_id *id)
1894f6a9d47aSJason Gunthorpe {
1895f6a9d47aSJason Gunthorpe 	struct rdma_id_private *id_priv =
1896f6a9d47aSJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
1897f6a9d47aSJason Gunthorpe 
1898f6a9d47aSJason Gunthorpe 	mutex_lock(&id_priv->handler_mutex);
1899f6a9d47aSJason Gunthorpe 	destroy_id_handler_unlock(id_priv);
1900f6a9d47aSJason Gunthorpe }
1901e51060f0SSean Hefty EXPORT_SYMBOL(rdma_destroy_id);
1902e51060f0SSean Hefty 
1903e51060f0SSean Hefty static int cma_rep_recv(struct rdma_id_private *id_priv)
1904e51060f0SSean Hefty {
1905e51060f0SSean Hefty 	int ret;
1906e51060f0SSean Hefty 
19075851bb89SSean Hefty 	ret = cma_modify_qp_rtr(id_priv, NULL);
1908e51060f0SSean Hefty 	if (ret)
1909e51060f0SSean Hefty 		goto reject;
1910e51060f0SSean Hefty 
19115851bb89SSean Hefty 	ret = cma_modify_qp_rts(id_priv, NULL);
1912e51060f0SSean Hefty 	if (ret)
1913e51060f0SSean Hefty 		goto reject;
1914e51060f0SSean Hefty 
1915ed999f82SChuck Lever 	trace_cm_send_rtu(id_priv);
1916e51060f0SSean Hefty 	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1917e51060f0SSean Hefty 	if (ret)
1918e51060f0SSean Hefty 		goto reject;
1919e51060f0SSean Hefty 
1920e51060f0SSean Hefty 	return 0;
1921e51060f0SSean Hefty reject:
1922498683c6SMoni Shoua 	pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
1923c5483388SSean Hefty 	cma_modify_qp_err(id_priv);
1924ed999f82SChuck Lever 	trace_cm_send_rej(id_priv);
1925e51060f0SSean Hefty 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1926e51060f0SSean Hefty 		       NULL, 0, NULL, 0);
1927e51060f0SSean Hefty 	return ret;
1928e51060f0SSean Hefty }
1929e51060f0SSean Hefty 
1930a1b1b61fSSean Hefty static void cma_set_rep_event_data(struct rdma_cm_event *event,
1931e7ff98aeSParav Pandit 				   const struct ib_cm_rep_event_param *rep_data,
1932a1b1b61fSSean Hefty 				   void *private_data)
1933a1b1b61fSSean Hefty {
1934a1b1b61fSSean Hefty 	event->param.conn.private_data = private_data;
1935a1b1b61fSSean Hefty 	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1936a1b1b61fSSean Hefty 	event->param.conn.responder_resources = rep_data->responder_resources;
1937a1b1b61fSSean Hefty 	event->param.conn.initiator_depth = rep_data->initiator_depth;
1938a1b1b61fSSean Hefty 	event->param.conn.flow_control = rep_data->flow_control;
1939a1b1b61fSSean Hefty 	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1940a1b1b61fSSean Hefty 	event->param.conn.srq = rep_data->srq;
1941a1b1b61fSSean Hefty 	event->param.conn.qp_num = rep_data->remote_qpn;
1942a20652e1SLeon Romanovsky 
1943a20652e1SLeon Romanovsky 	event->ece.vendor_id = rep_data->ece.vendor_id;
1944a20652e1SLeon Romanovsky 	event->ece.attr_mod = rep_data->ece.attr_mod;
1945a1b1b61fSSean Hefty }
1946a1b1b61fSSean Hefty 
1947ed999f82SChuck Lever static int cma_cm_event_handler(struct rdma_id_private *id_priv,
1948ed999f82SChuck Lever 				struct rdma_cm_event *event)
1949ed999f82SChuck Lever {
1950ed999f82SChuck Lever 	int ret;
1951ed999f82SChuck Lever 
19523647a28dSJason Gunthorpe 	lockdep_assert_held(&id_priv->handler_mutex);
19533647a28dSJason Gunthorpe 
1954ed999f82SChuck Lever 	trace_cm_event_handler(id_priv, event);
1955ed999f82SChuck Lever 	ret = id_priv->id.event_handler(&id_priv->id, event);
1956ed999f82SChuck Lever 	trace_cm_event_done(id_priv, event, ret);
1957ed999f82SChuck Lever 	return ret;
1958ed999f82SChuck Lever }
1959ed999f82SChuck Lever 
1960e7ff98aeSParav Pandit static int cma_ib_handler(struct ib_cm_id *cm_id,
1961e7ff98aeSParav Pandit 			  const struct ib_cm_event *ib_event)
1962e51060f0SSean Hefty {
1963e51060f0SSean Hefty 	struct rdma_id_private *id_priv = cm_id->context;
19647582df82SParav Pandit 	struct rdma_cm_event event = {};
19652a7cec53SJason Gunthorpe 	enum rdma_cm_state state;
1966f6a9d47aSJason Gunthorpe 	int ret;
1967e51060f0SSean Hefty 
196837e07cdaSBart Van Assche 	mutex_lock(&id_priv->handler_mutex);
19692a7cec53SJason Gunthorpe 	state = READ_ONCE(id_priv->state);
197038ca83a5SAmir Vadai 	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
19712a7cec53SJason Gunthorpe 	     state != RDMA_CM_CONNECT) ||
197238ca83a5SAmir Vadai 	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
19732a7cec53SJason Gunthorpe 	     state != RDMA_CM_DISCONNECT))
197437e07cdaSBart Van Assche 		goto out;
1975e51060f0SSean Hefty 
1976e51060f0SSean Hefty 	switch (ib_event->event) {
1977e51060f0SSean Hefty 	case IB_CM_REQ_ERROR:
1978e51060f0SSean Hefty 	case IB_CM_REP_ERROR:
1979a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_UNREACHABLE;
1980a1b1b61fSSean Hefty 		event.status = -ETIMEDOUT;
1981e51060f0SSean Hefty 		break;
1982e51060f0SSean Hefty 	case IB_CM_REP_RECEIVED:
19832a7cec53SJason Gunthorpe 		if (state == RDMA_CM_CONNECT &&
1984ed999f82SChuck Lever 		    (id_priv->id.qp_type != IB_QPT_UD)) {
1985ed999f82SChuck Lever 			trace_cm_send_mra(id_priv);
198661c0ddbeSMoni Shoua 			ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1987ed999f82SChuck Lever 		}
198801602f11SSean Hefty 		if (id_priv->id.qp) {
1989a1b1b61fSSean Hefty 			event.status = cma_rep_recv(id_priv);
1990a1b1b61fSSean Hefty 			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1991e51060f0SSean Hefty 						     RDMA_CM_EVENT_ESTABLISHED;
199201602f11SSean Hefty 		} else {
1993a1b1b61fSSean Hefty 			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
199401602f11SSean Hefty 		}
1995a1b1b61fSSean Hefty 		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1996a1b1b61fSSean Hefty 				       ib_event->private_data);
1997e51060f0SSean Hefty 		break;
1998e51060f0SSean Hefty 	case IB_CM_RTU_RECEIVED:
19990fe313b0SSean Hefty 	case IB_CM_USER_ESTABLISHED:
20000fe313b0SSean Hefty 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2001e51060f0SSean Hefty 		break;
2002e51060f0SSean Hefty 	case IB_CM_DREQ_ERROR:
2003df561f66SGustavo A. R. Silva 		event.status = -ETIMEDOUT;
2004df561f66SGustavo A. R. Silva 		fallthrough;
2005e51060f0SSean Hefty 	case IB_CM_DREQ_RECEIVED:
2006e51060f0SSean Hefty 	case IB_CM_DREP_RECEIVED:
2007550e5ca7SNir Muchtar 		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
2008550e5ca7SNir Muchtar 				   RDMA_CM_DISCONNECT))
2009e51060f0SSean Hefty 			goto out;
2010a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2011e51060f0SSean Hefty 		break;
2012e51060f0SSean Hefty 	case IB_CM_TIMEWAIT_EXIT:
201338ca83a5SAmir Vadai 		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
201438ca83a5SAmir Vadai 		break;
2015e51060f0SSean Hefty 	case IB_CM_MRA_RECEIVED:
2016e51060f0SSean Hefty 		/* ignore event */
2017e51060f0SSean Hefty 		goto out;
2018e51060f0SSean Hefty 	case IB_CM_REJ_RECEIVED:
2019498683c6SMoni Shoua 		pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
2020498683c6SMoni Shoua 										ib_event->param.rej_rcvd.reason));
2021c5483388SSean Hefty 		cma_modify_qp_err(id_priv);
2022a1b1b61fSSean Hefty 		event.status = ib_event->param.rej_rcvd.reason;
2023a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_REJECTED;
2024a1b1b61fSSean Hefty 		event.param.conn.private_data = ib_event->private_data;
2025a1b1b61fSSean Hefty 		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
2026e51060f0SSean Hefty 		break;
2027e51060f0SSean Hefty 	default:
2028aba25a3eSParav Pandit 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2029e51060f0SSean Hefty 		       ib_event->event);
2030e51060f0SSean Hefty 		goto out;
2031e51060f0SSean Hefty 	}
2032e51060f0SSean Hefty 
2033ed999f82SChuck Lever 	ret = cma_cm_event_handler(id_priv, &event);
2034e51060f0SSean Hefty 	if (ret) {
2035e51060f0SSean Hefty 		/* Destroy the CM ID by returning a non-zero value. */
2036e51060f0SSean Hefty 		id_priv->cm_id.ib = NULL;
2037f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(id_priv);
2038e51060f0SSean Hefty 		return ret;
2039e51060f0SSean Hefty 	}
2040e51060f0SSean Hefty out:
2041de910bd9SOr Gerlitz 	mutex_unlock(&id_priv->handler_mutex);
2042f6a9d47aSJason Gunthorpe 	return 0;
2043e51060f0SSean Hefty }
2044e51060f0SSean Hefty 
2045e7ff98aeSParav Pandit static struct rdma_id_private *
204685463316SParav Pandit cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2047e7ff98aeSParav Pandit 		   const struct ib_cm_event *ib_event,
20480b3ca768SHaggai Eran 		   struct net_device *net_dev)
2049e51060f0SSean Hefty {
205000313983SSteve Wise 	struct rdma_id_private *listen_id_priv;
2051e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
2052e51060f0SSean Hefty 	struct rdma_cm_id *id;
2053e51060f0SSean Hefty 	struct rdma_route *rt;
20540c505f70SHaggai Eran 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
20559fdca4daSDasaratharaman Chandramouli 	struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2056d3957b86SMajd Dibbiny 	const __be64 service_id =
2057d3957b86SMajd Dibbiny 		ib_event->param.req_rcvd.primary_path->service_id;
205864c5e613SOr Gerlitz 	int ret;
2059e51060f0SSean Hefty 
206000313983SSteve Wise 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2061b09c4d70SLeon Romanovsky 	id_priv = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2062fa20105eSGuy Shapiro 				   listen_id->event_handler, listen_id->context,
2063b09c4d70SLeon Romanovsky 				   listen_id->ps,
2064b09c4d70SLeon Romanovsky 				   ib_event->param.req_rcvd.qp_type,
2065b09c4d70SLeon Romanovsky 				   listen_id_priv);
2066b09c4d70SLeon Romanovsky 	if (IS_ERR(id_priv))
20670c9361fcSJack Morgenstein 		return NULL;
20683f168d2bSKrishna Kumar 
2069b09c4d70SLeon Romanovsky 	id = &id_priv->id;
20700c505f70SHaggai Eran 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
20710c505f70SHaggai Eran 			      (struct sockaddr *)&id->route.addr.dst_addr,
20720c505f70SHaggai Eran 			      listen_id, ib_event, ss_family, service_id))
2073fbaa1a6dSSean Hefty 		goto err;
20743f168d2bSKrishna Kumar 
20753f168d2bSKrishna Kumar 	rt = &id->route;
20763f168d2bSKrishna Kumar 	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
20776da2ec56SKees Cook 	rt->path_rec = kmalloc_array(rt->num_paths, sizeof(*rt->path_rec),
20783f168d2bSKrishna Kumar 				     GFP_KERNEL);
20793f168d2bSKrishna Kumar 	if (!rt->path_rec)
20800c9361fcSJack Morgenstein 		goto err;
20813f168d2bSKrishna Kumar 
20829fdca4daSDasaratharaman Chandramouli 	rt->path_rec[0] = *path;
2083e51060f0SSean Hefty 	if (rt->num_paths == 2)
2084e51060f0SSean Hefty 		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2085e51060f0SSean Hefty 
20860b3ca768SHaggai Eran 	if (net_dev) {
208777addc52SParav Pandit 		rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
20880b3ca768SHaggai Eran 	} else {
2089b8cab5daSHaggai Eran 		if (!cma_protocol_roce(listen_id) &&
2090b8cab5daSHaggai Eran 		    cma_any_addr(cma_src_addr(id_priv))) {
2091b8cab5daSHaggai Eran 			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2092b8cab5daSHaggai Eran 			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2093b8cab5daSHaggai Eran 			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2094b8cab5daSHaggai Eran 		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
2095b8cab5daSHaggai Eran 			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2096b8cab5daSHaggai Eran 			if (ret)
2097b8cab5daSHaggai Eran 				goto err;
2098b8cab5daSHaggai Eran 		}
20996f8372b6SSean Hefty 	}
21006f8372b6SSean Hefty 	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2101e51060f0SSean Hefty 
2102550e5ca7SNir Muchtar 	id_priv->state = RDMA_CM_CONNECT;
2103e51060f0SSean Hefty 	return id_priv;
21043f168d2bSKrishna Kumar 
21053f168d2bSKrishna Kumar err:
21060c9361fcSJack Morgenstein 	rdma_destroy_id(id);
2107e51060f0SSean Hefty 	return NULL;
2108e51060f0SSean Hefty }
2109e51060f0SSean Hefty 
2110e7ff98aeSParav Pandit static struct rdma_id_private *
211185463316SParav Pandit cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2112e7ff98aeSParav Pandit 		  const struct ib_cm_event *ib_event,
21130b3ca768SHaggai Eran 		  struct net_device *net_dev)
2114628e5f6dSSean Hefty {
2115e7ff98aeSParav Pandit 	const struct rdma_id_private *listen_id_priv;
2116628e5f6dSSean Hefty 	struct rdma_id_private *id_priv;
2117628e5f6dSSean Hefty 	struct rdma_cm_id *id;
21180c505f70SHaggai Eran 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2119fa20105eSGuy Shapiro 	struct net *net = listen_id->route.addr.dev_addr.net;
2120628e5f6dSSean Hefty 	int ret;
2121628e5f6dSSean Hefty 
212200313983SSteve Wise 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2123b09c4d70SLeon Romanovsky 	id_priv = __rdma_create_id(net, listen_id->event_handler,
2124b09c4d70SLeon Romanovsky 				   listen_id->context, listen_id->ps, IB_QPT_UD,
2125b09c4d70SLeon Romanovsky 				   listen_id_priv);
2126b09c4d70SLeon Romanovsky 	if (IS_ERR(id_priv))
2127628e5f6dSSean Hefty 		return NULL;
2128628e5f6dSSean Hefty 
2129b09c4d70SLeon Romanovsky 	id = &id_priv->id;
21300c505f70SHaggai Eran 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
21310c505f70SHaggai Eran 			      (struct sockaddr *)&id->route.addr.dst_addr,
21320c505f70SHaggai Eran 			      listen_id, ib_event, ss_family,
21330c505f70SHaggai Eran 			      ib_event->param.sidr_req_rcvd.service_id))
2134628e5f6dSSean Hefty 		goto err;
2135628e5f6dSSean Hefty 
21360b3ca768SHaggai Eran 	if (net_dev) {
213777addc52SParav Pandit 		rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
21380b3ca768SHaggai Eran 	} else {
2139b8cab5daSHaggai Eran 		if (!cma_any_addr(cma_src_addr(id_priv))) {
2140b8cab5daSHaggai Eran 			ret = cma_translate_addr(cma_src_addr(id_priv),
21410b3ca768SHaggai Eran 						 &id->route.addr.dev_addr);
2142b8cab5daSHaggai Eran 			if (ret)
2143b8cab5daSHaggai Eran 				goto err;
2144b8cab5daSHaggai Eran 		}
21456f8372b6SSean Hefty 	}
2146628e5f6dSSean Hefty 
2147550e5ca7SNir Muchtar 	id_priv->state = RDMA_CM_CONNECT;
2148628e5f6dSSean Hefty 	return id_priv;
2149628e5f6dSSean Hefty err:
2150628e5f6dSSean Hefty 	rdma_destroy_id(id);
2151628e5f6dSSean Hefty 	return NULL;
2152628e5f6dSSean Hefty }
2153628e5f6dSSean Hefty 
2154a1b1b61fSSean Hefty static void cma_set_req_event_data(struct rdma_cm_event *event,
2155e7ff98aeSParav Pandit 				   const struct ib_cm_req_event_param *req_data,
2156a1b1b61fSSean Hefty 				   void *private_data, int offset)
2157a1b1b61fSSean Hefty {
2158a1b1b61fSSean Hefty 	event->param.conn.private_data = private_data + offset;
2159a1b1b61fSSean Hefty 	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2160a1b1b61fSSean Hefty 	event->param.conn.responder_resources = req_data->responder_resources;
2161a1b1b61fSSean Hefty 	event->param.conn.initiator_depth = req_data->initiator_depth;
2162a1b1b61fSSean Hefty 	event->param.conn.flow_control = req_data->flow_control;
2163a1b1b61fSSean Hefty 	event->param.conn.retry_count = req_data->retry_count;
2164a1b1b61fSSean Hefty 	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2165a1b1b61fSSean Hefty 	event->param.conn.srq = req_data->srq;
2166a1b1b61fSSean Hefty 	event->param.conn.qp_num = req_data->remote_qpn;
2167a20652e1SLeon Romanovsky 
2168a20652e1SLeon Romanovsky 	event->ece.vendor_id = req_data->ece.vendor_id;
2169a20652e1SLeon Romanovsky 	event->ece.attr_mod = req_data->ece.attr_mod;
2170a1b1b61fSSean Hefty }
2171a1b1b61fSSean Hefty 
217285463316SParav Pandit static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2173e7ff98aeSParav Pandit 				    const struct ib_cm_event *ib_event)
21749595480cSHefty, Sean {
21754dd81e89SSean Hefty 	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
21769595480cSHefty, Sean 		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
21779595480cSHefty, Sean 		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
21789595480cSHefty, Sean 		 (id->qp_type == IB_QPT_UD)) ||
21799595480cSHefty, Sean 		(!id->qp_type));
21809595480cSHefty, Sean }
21819595480cSHefty, Sean 
218285463316SParav Pandit static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2183e7ff98aeSParav Pandit 			      const struct ib_cm_event *ib_event)
2184e51060f0SSean Hefty {
218537e07cdaSBart Van Assche 	struct rdma_id_private *listen_id, *conn_id = NULL;
21867582df82SParav Pandit 	struct rdma_cm_event event = {};
218741ab1cb7SParav Pandit 	struct cma_req_info req = {};
21880b3ca768SHaggai Eran 	struct net_device *net_dev;
2189c0b64f58SBart Van Assche 	u8 offset;
2190c0b64f58SBart Van Assche 	int ret;
2191e51060f0SSean Hefty 
219241ab1cb7SParav Pandit 	listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
21934c21b5bcSHaggai Eran 	if (IS_ERR(listen_id))
21944c21b5bcSHaggai Eran 		return PTR_ERR(listen_id);
21954c21b5bcSHaggai Eran 
2196ed999f82SChuck Lever 	trace_cm_req_handler(listen_id, ib_event->event);
219785463316SParav Pandit 	if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
21980b3ca768SHaggai Eran 		ret = -EINVAL;
21990b3ca768SHaggai Eran 		goto net_dev_put;
22000b3ca768SHaggai Eran 	}
22019595480cSHefty, Sean 
220237e07cdaSBart Van Assche 	mutex_lock(&listen_id->handler_mutex);
2203d490ee52SJason Gunthorpe 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN) {
22040b3ca768SHaggai Eran 		ret = -ECONNABORTED;
2205f6a9d47aSJason Gunthorpe 		goto err_unlock;
22060b3ca768SHaggai Eran 	}
2207e51060f0SSean Hefty 
2208e8160e15SSean Hefty 	offset = cma_user_data_offset(listen_id);
2209628e5f6dSSean Hefty 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
22109595480cSHefty, Sean 	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
221185463316SParav Pandit 		conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2212628e5f6dSSean Hefty 		event.param.ud.private_data = ib_event->private_data + offset;
2213628e5f6dSSean Hefty 		event.param.ud.private_data_len =
2214628e5f6dSSean Hefty 				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2215628e5f6dSSean Hefty 	} else {
221685463316SParav Pandit 		conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2217628e5f6dSSean Hefty 		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2218628e5f6dSSean Hefty 				       ib_event->private_data, offset);
2219628e5f6dSSean Hefty 	}
2220e51060f0SSean Hefty 	if (!conn_id) {
2221e51060f0SSean Hefty 		ret = -ENOMEM;
2222f6a9d47aSJason Gunthorpe 		goto err_unlock;
2223e51060f0SSean Hefty 	}
2224e51060f0SSean Hefty 
2225de910bd9SOr Gerlitz 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
222641ab1cb7SParav Pandit 	ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2227f6a9d47aSJason Gunthorpe 	if (ret) {
2228f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(conn_id);
2229f6a9d47aSJason Gunthorpe 		goto err_unlock;
2230f6a9d47aSJason Gunthorpe 	}
2231e51060f0SSean Hefty 
2232e51060f0SSean Hefty 	conn_id->cm_id.ib = cm_id;
2233e51060f0SSean Hefty 	cm_id->context = conn_id;
2234e51060f0SSean Hefty 	cm_id->cm_handler = cma_ib_handler;
2235e51060f0SSean Hefty 
2236ed999f82SChuck Lever 	ret = cma_cm_event_handler(conn_id, &event);
2237f6a9d47aSJason Gunthorpe 	if (ret) {
2238f6a9d47aSJason Gunthorpe 		/* Destroy the CM ID by returning a non-zero value. */
2239f6a9d47aSJason Gunthorpe 		conn_id->cm_id.ib = NULL;
2240f6a9d47aSJason Gunthorpe 		mutex_unlock(&listen_id->handler_mutex);
2241f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(conn_id);
2242f6a9d47aSJason Gunthorpe 		goto net_dev_put;
2243f6a9d47aSJason Gunthorpe 	}
2244f6a9d47aSJason Gunthorpe 
22452a7cec53SJason Gunthorpe 	if (READ_ONCE(conn_id->state) == RDMA_CM_CONNECT &&
22462a7cec53SJason Gunthorpe 	    conn_id->id.qp_type != IB_QPT_UD) {
2247ed999f82SChuck Lever 		trace_cm_send_mra(cm_id->context);
2248ead595aeSSean Hefty 		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2249ed999f82SChuck Lever 	}
2250de910bd9SOr Gerlitz 	mutex_unlock(&conn_id->handler_mutex);
2251a1a733f6SKrishna Kumar 
2252f6a9d47aSJason Gunthorpe err_unlock:
2253de910bd9SOr Gerlitz 	mutex_unlock(&listen_id->handler_mutex);
22540b3ca768SHaggai Eran 
22550b3ca768SHaggai Eran net_dev_put:
22560b3ca768SHaggai Eran 	if (net_dev)
22570b3ca768SHaggai Eran 		dev_put(net_dev);
22580b3ca768SHaggai Eran 
2259e51060f0SSean Hefty 	return ret;
2260e51060f0SSean Hefty }
2261e51060f0SSean Hefty 
2262cf53936fSSean Hefty __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2263e51060f0SSean Hefty {
2264496ce3ceSSean Hefty 	if (addr->sa_family == AF_IB)
2265496ce3ceSSean Hefty 		return ((struct sockaddr_ib *) addr)->sib_sid;
2266496ce3ceSSean Hefty 
2267cf53936fSSean Hefty 	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2268e51060f0SSean Hefty }
2269cf53936fSSean Hefty EXPORT_SYMBOL(rdma_get_service_id);
2270e51060f0SSean Hefty 
2271411460acSParav Pandit void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2272411460acSParav Pandit 		    union ib_gid *dgid)
2273411460acSParav Pandit {
2274411460acSParav Pandit 	struct rdma_addr *addr = &cm_id->route.addr;
2275411460acSParav Pandit 
2276411460acSParav Pandit 	if (!cm_id->device) {
2277411460acSParav Pandit 		if (sgid)
2278411460acSParav Pandit 			memset(sgid, 0, sizeof(*sgid));
2279411460acSParav Pandit 		if (dgid)
2280411460acSParav Pandit 			memset(dgid, 0, sizeof(*dgid));
2281411460acSParav Pandit 		return;
2282411460acSParav Pandit 	}
2283411460acSParav Pandit 
2284411460acSParav Pandit 	if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2285411460acSParav Pandit 		if (sgid)
2286411460acSParav Pandit 			rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2287411460acSParav Pandit 		if (dgid)
2288411460acSParav Pandit 			rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2289411460acSParav Pandit 	} else {
2290411460acSParav Pandit 		if (sgid)
2291411460acSParav Pandit 			rdma_addr_get_sgid(&addr->dev_addr, sgid);
2292411460acSParav Pandit 		if (dgid)
2293411460acSParav Pandit 			rdma_addr_get_dgid(&addr->dev_addr, dgid);
2294411460acSParav Pandit 	}
2295411460acSParav Pandit }
2296411460acSParav Pandit EXPORT_SYMBOL(rdma_read_gids);
2297411460acSParav Pandit 
229807ebafbaSTom Tucker static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
229907ebafbaSTom Tucker {
230007ebafbaSTom Tucker 	struct rdma_id_private *id_priv = iw_id->context;
23017582df82SParav Pandit 	struct rdma_cm_event event = {};
230207ebafbaSTom Tucker 	int ret = 0;
230324d44a39SSteve Wise 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
230424d44a39SSteve Wise 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
230507ebafbaSTom Tucker 
230637e07cdaSBart Van Assche 	mutex_lock(&id_priv->handler_mutex);
23072a7cec53SJason Gunthorpe 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
230837e07cdaSBart Van Assche 		goto out;
230907ebafbaSTom Tucker 
231007ebafbaSTom Tucker 	switch (iw_event->event) {
231107ebafbaSTom Tucker 	case IW_CM_EVENT_CLOSE:
2312a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_DISCONNECTED;
231307ebafbaSTom Tucker 		break;
231407ebafbaSTom Tucker 	case IW_CM_EVENT_CONNECT_REPLY:
231524d44a39SSteve Wise 		memcpy(cma_src_addr(id_priv), laddr,
231624d44a39SSteve Wise 		       rdma_addr_size(laddr));
231724d44a39SSteve Wise 		memcpy(cma_dst_addr(id_priv), raddr,
231824d44a39SSteve Wise 		       rdma_addr_size(raddr));
2319881a045fSSteve Wise 		switch (iw_event->status) {
2320881a045fSSteve Wise 		case 0:
2321a1b1b61fSSean Hefty 			event.event = RDMA_CM_EVENT_ESTABLISHED;
23223ebeebc3SKumar Sanghvi 			event.param.conn.initiator_depth = iw_event->ird;
23233ebeebc3SKumar Sanghvi 			event.param.conn.responder_resources = iw_event->ord;
232407ebafbaSTom Tucker 			break;
2325881a045fSSteve Wise 		case -ECONNRESET:
2326881a045fSSteve Wise 		case -ECONNREFUSED:
2327881a045fSSteve Wise 			event.event = RDMA_CM_EVENT_REJECTED;
2328881a045fSSteve Wise 			break;
2329881a045fSSteve Wise 		case -ETIMEDOUT:
2330881a045fSSteve Wise 			event.event = RDMA_CM_EVENT_UNREACHABLE;
2331881a045fSSteve Wise 			break;
2332881a045fSSteve Wise 		default:
2333881a045fSSteve Wise 			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2334881a045fSSteve Wise 			break;
2335881a045fSSteve Wise 		}
2336881a045fSSteve Wise 		break;
233707ebafbaSTom Tucker 	case IW_CM_EVENT_ESTABLISHED:
2338a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_ESTABLISHED;
23393ebeebc3SKumar Sanghvi 		event.param.conn.initiator_depth = iw_event->ird;
23403ebeebc3SKumar Sanghvi 		event.param.conn.responder_resources = iw_event->ord;
234107ebafbaSTom Tucker 		break;
234207ebafbaSTom Tucker 	default:
2343671a6cc2SLeon Romanovsky 		goto out;
234407ebafbaSTom Tucker 	}
234507ebafbaSTom Tucker 
2346a1b1b61fSSean Hefty 	event.status = iw_event->status;
2347a1b1b61fSSean Hefty 	event.param.conn.private_data = iw_event->private_data;
2348a1b1b61fSSean Hefty 	event.param.conn.private_data_len = iw_event->private_data_len;
2349ed999f82SChuck Lever 	ret = cma_cm_event_handler(id_priv, &event);
235007ebafbaSTom Tucker 	if (ret) {
235107ebafbaSTom Tucker 		/* Destroy the CM ID by returning a non-zero value. */
235207ebafbaSTom Tucker 		id_priv->cm_id.iw = NULL;
2353f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(id_priv);
235407ebafbaSTom Tucker 		return ret;
235507ebafbaSTom Tucker 	}
235607ebafbaSTom Tucker 
235737e07cdaSBart Van Assche out:
2358de910bd9SOr Gerlitz 	mutex_unlock(&id_priv->handler_mutex);
235907ebafbaSTom Tucker 	return ret;
236007ebafbaSTom Tucker }
236107ebafbaSTom Tucker 
236207ebafbaSTom Tucker static int iw_conn_req_handler(struct iw_cm_id *cm_id,
236307ebafbaSTom Tucker 			       struct iw_cm_event *iw_event)
236407ebafbaSTom Tucker {
236507ebafbaSTom Tucker 	struct rdma_id_private *listen_id, *conn_id;
23667582df82SParav Pandit 	struct rdma_cm_event event = {};
236737e07cdaSBart Van Assche 	int ret = -ECONNABORTED;
236824d44a39SSteve Wise 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
236924d44a39SSteve Wise 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
237007ebafbaSTom Tucker 
23717582df82SParav Pandit 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
23727582df82SParav Pandit 	event.param.conn.private_data = iw_event->private_data;
23737582df82SParav Pandit 	event.param.conn.private_data_len = iw_event->private_data_len;
23747582df82SParav Pandit 	event.param.conn.initiator_depth = iw_event->ird;
23757582df82SParav Pandit 	event.param.conn.responder_resources = iw_event->ord;
23767582df82SParav Pandit 
237707ebafbaSTom Tucker 	listen_id = cm_id->context;
237837e07cdaSBart Van Assche 
237937e07cdaSBart Van Assche 	mutex_lock(&listen_id->handler_mutex);
2380d490ee52SJason Gunthorpe 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN)
238137e07cdaSBart Van Assche 		goto out;
238207ebafbaSTom Tucker 
238307ebafbaSTom Tucker 	/* Create a new RDMA id for the new IW CM ID */
2384b09c4d70SLeon Romanovsky 	conn_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2385fa20105eSGuy Shapiro 				   listen_id->id.event_handler,
2386b09c4d70SLeon Romanovsky 				   listen_id->id.context, RDMA_PS_TCP,
2387b09c4d70SLeon Romanovsky 				   IB_QPT_RC, listen_id);
2388b09c4d70SLeon Romanovsky 	if (IS_ERR(conn_id)) {
238907ebafbaSTom Tucker 		ret = -ENOMEM;
239007ebafbaSTom Tucker 		goto out;
239107ebafbaSTom Tucker 	}
2392de910bd9SOr Gerlitz 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2393550e5ca7SNir Muchtar 	conn_id->state = RDMA_CM_CONNECT;
239407ebafbaSTom Tucker 
2395575c7e58SParav Pandit 	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
239607ebafbaSTom Tucker 	if (ret) {
2397f6a9d47aSJason Gunthorpe 		mutex_unlock(&listen_id->handler_mutex);
2398f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(conn_id);
2399f6a9d47aSJason Gunthorpe 		return ret;
240007ebafbaSTom Tucker 	}
240107ebafbaSTom Tucker 
240241ab1cb7SParav Pandit 	ret = cma_iw_acquire_dev(conn_id, listen_id);
240307ebafbaSTom Tucker 	if (ret) {
2404f6a9d47aSJason Gunthorpe 		mutex_unlock(&listen_id->handler_mutex);
2405f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(conn_id);
2406f6a9d47aSJason Gunthorpe 		return ret;
240707ebafbaSTom Tucker 	}
240807ebafbaSTom Tucker 
240907ebafbaSTom Tucker 	conn_id->cm_id.iw = cm_id;
241007ebafbaSTom Tucker 	cm_id->context = conn_id;
241107ebafbaSTom Tucker 	cm_id->cm_handler = cma_iw_handler;
241207ebafbaSTom Tucker 
241324d44a39SSteve Wise 	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
241424d44a39SSteve Wise 	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
241507ebafbaSTom Tucker 
2416ed999f82SChuck Lever 	ret = cma_cm_event_handler(conn_id, &event);
241707ebafbaSTom Tucker 	if (ret) {
241807ebafbaSTom Tucker 		/* User wants to destroy the CM ID */
241907ebafbaSTom Tucker 		conn_id->cm_id.iw = NULL;
2420b66f31efSBart Van Assche 		mutex_unlock(&listen_id->handler_mutex);
2421f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(conn_id);
2422b66f31efSBart Van Assche 		return ret;
242307ebafbaSTom Tucker 	}
242407ebafbaSTom Tucker 
2425de910bd9SOr Gerlitz 	mutex_unlock(&conn_id->handler_mutex);
2426de910bd9SOr Gerlitz 
242707ebafbaSTom Tucker out:
2428de910bd9SOr Gerlitz 	mutex_unlock(&listen_id->handler_mutex);
242907ebafbaSTom Tucker 	return ret;
243007ebafbaSTom Tucker }
243107ebafbaSTom Tucker 
2432e51060f0SSean Hefty static int cma_ib_listen(struct rdma_id_private *id_priv)
2433e51060f0SSean Hefty {
2434e51060f0SSean Hefty 	struct sockaddr *addr;
24350c9361fcSJack Morgenstein 	struct ib_cm_id	*id;
2436e51060f0SSean Hefty 	__be64 svc_id;
2437e51060f0SSean Hefty 
2438f4753834SSean Hefty 	addr = cma_src_addr(id_priv);
2439cf53936fSSean Hefty 	svc_id = rdma_get_service_id(&id_priv->id, addr);
244085463316SParav Pandit 	id = ib_cm_insert_listen(id_priv->id.device,
244185463316SParav Pandit 				 cma_ib_req_handler, svc_id);
244251efe394SHaggai Eran 	if (IS_ERR(id))
244351efe394SHaggai Eran 		return PTR_ERR(id);
244451efe394SHaggai Eran 	id_priv->cm_id.ib = id;
2445e51060f0SSean Hefty 
244651efe394SHaggai Eran 	return 0;
2447e51060f0SSean Hefty }
2448e51060f0SSean Hefty 
244907ebafbaSTom Tucker static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
245007ebafbaSTom Tucker {
245107ebafbaSTom Tucker 	int ret;
24520c9361fcSJack Morgenstein 	struct iw_cm_id	*id;
245307ebafbaSTom Tucker 
24540c9361fcSJack Morgenstein 	id = iw_create_cm_id(id_priv->id.device,
245507ebafbaSTom Tucker 			     iw_conn_req_handler,
245607ebafbaSTom Tucker 			     id_priv);
24570c9361fcSJack Morgenstein 	if (IS_ERR(id))
24580c9361fcSJack Morgenstein 		return PTR_ERR(id);
24590c9361fcSJack Morgenstein 
2460ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
246168cdba06SSteve Wise 	id->tos = id_priv->tos;
2462926ba19bSSteve Wise 	id->tos_set = id_priv->tos_set;
2463ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
2464e35ecb46SBernard Metzler 	id->afonly = id_priv->afonly;
24650c9361fcSJack Morgenstein 	id_priv->cm_id.iw = id;
246607ebafbaSTom Tucker 
246724d44a39SSteve Wise 	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
246824d44a39SSteve Wise 	       rdma_addr_size(cma_src_addr(id_priv)));
246907ebafbaSTom Tucker 
247007ebafbaSTom Tucker 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
247107ebafbaSTom Tucker 
247207ebafbaSTom Tucker 	if (ret) {
247307ebafbaSTom Tucker 		iw_destroy_cm_id(id_priv->cm_id.iw);
247407ebafbaSTom Tucker 		id_priv->cm_id.iw = NULL;
247507ebafbaSTom Tucker 	}
247607ebafbaSTom Tucker 
247707ebafbaSTom Tucker 	return ret;
247807ebafbaSTom Tucker }
247907ebafbaSTom Tucker 
2480e51060f0SSean Hefty static int cma_listen_handler(struct rdma_cm_id *id,
2481e51060f0SSean Hefty 			      struct rdma_cm_event *event)
2482e51060f0SSean Hefty {
2483e51060f0SSean Hefty 	struct rdma_id_private *id_priv = id->context;
2484e51060f0SSean Hefty 
2485d54f23c0SJason Gunthorpe 	/* Listening IDs are always destroyed on removal */
2486d54f23c0SJason Gunthorpe 	if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2487d54f23c0SJason Gunthorpe 		return -1;
2488d54f23c0SJason Gunthorpe 
2489e51060f0SSean Hefty 	id->context = id_priv->id.context;
2490e51060f0SSean Hefty 	id->event_handler = id_priv->id.event_handler;
2491ed999f82SChuck Lever 	trace_cm_event_handler(id_priv, event);
2492e51060f0SSean Hefty 	return id_priv->id.event_handler(id, event);
2493e51060f0SSean Hefty }
2494e51060f0SSean Hefty 
2495c80a0c52SLeon Romanovsky static int cma_listen_on_dev(struct rdma_id_private *id_priv,
2496dd37d2f5SJason Gunthorpe 			     struct cma_device *cma_dev,
2497dd37d2f5SJason Gunthorpe 			     struct rdma_id_private **to_destroy)
2498e51060f0SSean Hefty {
2499e51060f0SSean Hefty 	struct rdma_id_private *dev_id_priv;
2500fa20105eSGuy Shapiro 	struct net *net = id_priv->id.route.addr.dev_addr.net;
2501e51060f0SSean Hefty 	int ret;
2502e51060f0SSean Hefty 
2503730c8912SMark Zhang 	lockdep_assert_held(&lock);
2504730c8912SMark Zhang 
2505dd37d2f5SJason Gunthorpe 	*to_destroy = NULL;
250672219ceaSMichael Wang 	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2507c80a0c52SLeon Romanovsky 		return 0;
250894d0c939SSean Hefty 
2509b09c4d70SLeon Romanovsky 	dev_id_priv =
2510b09c4d70SLeon Romanovsky 		__rdma_create_id(net, cma_listen_handler, id_priv,
2511b09c4d70SLeon Romanovsky 				 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2512b09c4d70SLeon Romanovsky 	if (IS_ERR(dev_id_priv))
2513c80a0c52SLeon Romanovsky 		return PTR_ERR(dev_id_priv);
2514e51060f0SSean Hefty 
2515550e5ca7SNir Muchtar 	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2516f4753834SSean Hefty 	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2517f4753834SSean Hefty 	       rdma_addr_size(cma_src_addr(id_priv)));
2518e51060f0SSean Hefty 
2519045959dbSMatan Barak 	_cma_attach_to_dev(dev_id_priv, cma_dev);
2520cb5cd0eaSShay Drory 	rdma_restrack_add(&dev_id_priv->res);
2521e368d23fSParav Pandit 	cma_id_get(id_priv);
2522d02d1f53SSean Hefty 	dev_id_priv->internal_id = 1;
25235b0ec991SSean Hefty 	dev_id_priv->afonly = id_priv->afonly;
2524ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
25259491128fSSteve Wise 	dev_id_priv->tos_set = id_priv->tos_set;
25269491128fSSteve Wise 	dev_id_priv->tos = id_priv->tos;
2527ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
2528e51060f0SSean Hefty 
2529b09c4d70SLeon Romanovsky 	ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2530e51060f0SSean Hefty 	if (ret)
2531c80a0c52SLeon Romanovsky 		goto err_listen;
2532dd37d2f5SJason Gunthorpe 	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2533c80a0c52SLeon Romanovsky 	return 0;
2534c80a0c52SLeon Romanovsky err_listen:
2535dd37d2f5SJason Gunthorpe 	/* Caller must destroy this after releasing lock */
2536dd37d2f5SJason Gunthorpe 	*to_destroy = dev_id_priv;
2537c80a0c52SLeon Romanovsky 	dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret);
2538c80a0c52SLeon Romanovsky 	return ret;
2539e51060f0SSean Hefty }
2540e51060f0SSean Hefty 
2541c80a0c52SLeon Romanovsky static int cma_listen_on_all(struct rdma_id_private *id_priv)
2542e51060f0SSean Hefty {
2543dd37d2f5SJason Gunthorpe 	struct rdma_id_private *to_destroy;
2544e51060f0SSean Hefty 	struct cma_device *cma_dev;
2545c80a0c52SLeon Romanovsky 	int ret;
2546e51060f0SSean Hefty 
2547e51060f0SSean Hefty 	mutex_lock(&lock);
2548e51060f0SSean Hefty 	list_add_tail(&id_priv->list, &listen_any_list);
2549c80a0c52SLeon Romanovsky 	list_for_each_entry(cma_dev, &dev_list, list) {
2550dd37d2f5SJason Gunthorpe 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
2551dd37d2f5SJason Gunthorpe 		if (ret) {
2552dd37d2f5SJason Gunthorpe 			/* Prevent racing with cma_process_remove() */
2553dd37d2f5SJason Gunthorpe 			if (to_destroy)
2554dd37d2f5SJason Gunthorpe 				list_del_init(&to_destroy->list);
2555c80a0c52SLeon Romanovsky 			goto err_listen;
2556c80a0c52SLeon Romanovsky 		}
2557dd37d2f5SJason Gunthorpe 	}
2558e51060f0SSean Hefty 	mutex_unlock(&lock);
2559c80a0c52SLeon Romanovsky 	return 0;
2560c80a0c52SLeon Romanovsky 
2561c80a0c52SLeon Romanovsky err_listen:
2562c80a0c52SLeon Romanovsky 	list_del(&id_priv->list);
2563c80a0c52SLeon Romanovsky 	mutex_unlock(&lock);
2564dd37d2f5SJason Gunthorpe 	if (to_destroy)
2565dd37d2f5SJason Gunthorpe 		rdma_destroy_id(&to_destroy->id);
2566c80a0c52SLeon Romanovsky 	return ret;
2567e51060f0SSean Hefty }
2568e51060f0SSean Hefty 
2569a81c994dSSean Hefty void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2570a81c994dSSean Hefty {
2571a81c994dSSean Hefty 	struct rdma_id_private *id_priv;
2572a81c994dSSean Hefty 
2573a81c994dSSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
2574ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
2575a81c994dSSean Hefty 	id_priv->tos = (u8) tos;
257689052d78SMajd Dibbiny 	id_priv->tos_set = true;
2577ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
2578a81c994dSSean Hefty }
2579a81c994dSSean Hefty EXPORT_SYMBOL(rdma_set_service_type);
2580a81c994dSSean Hefty 
25812c1619edSDanit Goldberg /**
25822c1619edSDanit Goldberg  * rdma_set_ack_timeout() - Set the ack timeout of QP associated
25832c1619edSDanit Goldberg  *                          with a connection identifier.
25842c1619edSDanit Goldberg  * @id: Communication identifier to associated with service type.
25852c1619edSDanit Goldberg  * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
25862c1619edSDanit Goldberg  *
25872c1619edSDanit Goldberg  * This function should be called before rdma_connect() on active side,
25882c1619edSDanit Goldberg  * and on passive side before rdma_accept(). It is applicable to primary
25892c1619edSDanit Goldberg  * path only. The timeout will affect the local side of the QP, it is not
2590e1ee1e62SDag Moxnes  * negotiated with remote side and zero disables the timer. In case it is
2591e1ee1e62SDag Moxnes  * set before rdma_resolve_route, the value will also be used to determine
2592e1ee1e62SDag Moxnes  * PacketLifeTime for RoCE.
25932c1619edSDanit Goldberg  *
25942c1619edSDanit Goldberg  * Return: 0 for success
25952c1619edSDanit Goldberg  */
25962c1619edSDanit Goldberg int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
25972c1619edSDanit Goldberg {
25982c1619edSDanit Goldberg 	struct rdma_id_private *id_priv;
25992c1619edSDanit Goldberg 
26002c1619edSDanit Goldberg 	if (id->qp_type != IB_QPT_RC)
26012c1619edSDanit Goldberg 		return -EINVAL;
26022c1619edSDanit Goldberg 
26032c1619edSDanit Goldberg 	id_priv = container_of(id, struct rdma_id_private, id);
2604ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
26052c1619edSDanit Goldberg 	id_priv->timeout = timeout;
26062c1619edSDanit Goldberg 	id_priv->timeout_set = true;
2607ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
26082c1619edSDanit Goldberg 
26092c1619edSDanit Goldberg 	return 0;
26102c1619edSDanit Goldberg }
26112c1619edSDanit Goldberg EXPORT_SYMBOL(rdma_set_ack_timeout);
26122c1619edSDanit Goldberg 
26133aeffc46SHåkon Bugge /**
26143aeffc46SHåkon Bugge  * rdma_set_min_rnr_timer() - Set the minimum RNR Retry timer of the
26153aeffc46SHåkon Bugge  *			      QP associated with a connection identifier.
26163aeffc46SHåkon Bugge  * @id: Communication identifier to associated with service type.
26173aeffc46SHåkon Bugge  * @min_rnr_timer: 5-bit value encoded as Table 45: "Encoding for RNR NAK
26183aeffc46SHåkon Bugge  *		   Timer Field" in the IBTA specification.
26193aeffc46SHåkon Bugge  *
26203aeffc46SHåkon Bugge  * This function should be called before rdma_connect() on active
26213aeffc46SHåkon Bugge  * side, and on passive side before rdma_accept(). The timer value
26223aeffc46SHåkon Bugge  * will be associated with the local QP. When it receives a send it is
26233aeffc46SHåkon Bugge  * not read to handle, typically if the receive queue is empty, an RNR
26243aeffc46SHåkon Bugge  * Retry NAK is returned to the requester with the min_rnr_timer
26253aeffc46SHåkon Bugge  * encoded. The requester will then wait at least the time specified
26263aeffc46SHåkon Bugge  * in the NAK before retrying. The default is zero, which translates
26273aeffc46SHåkon Bugge  * to a minimum RNR Timer value of 655 ms.
26283aeffc46SHåkon Bugge  *
26293aeffc46SHåkon Bugge  * Return: 0 for success
26303aeffc46SHåkon Bugge  */
26313aeffc46SHåkon Bugge int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer)
26323aeffc46SHåkon Bugge {
26333aeffc46SHåkon Bugge 	struct rdma_id_private *id_priv;
26343aeffc46SHåkon Bugge 
26353aeffc46SHåkon Bugge 	/* It is a five-bit value */
26363aeffc46SHåkon Bugge 	if (min_rnr_timer & 0xe0)
26373aeffc46SHåkon Bugge 		return -EINVAL;
26383aeffc46SHåkon Bugge 
26393aeffc46SHåkon Bugge 	if (WARN_ON(id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_TGT))
26403aeffc46SHåkon Bugge 		return -EINVAL;
26413aeffc46SHåkon Bugge 
26423aeffc46SHåkon Bugge 	id_priv = container_of(id, struct rdma_id_private, id);
2643ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
26443aeffc46SHåkon Bugge 	id_priv->min_rnr_timer = min_rnr_timer;
26453aeffc46SHåkon Bugge 	id_priv->min_rnr_timer_set = true;
2646ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
26473aeffc46SHåkon Bugge 
26483aeffc46SHåkon Bugge 	return 0;
26493aeffc46SHåkon Bugge }
26503aeffc46SHåkon Bugge EXPORT_SYMBOL(rdma_set_min_rnr_timer);
26513aeffc46SHåkon Bugge 
2652c2f8fc4eSDasaratharaman Chandramouli static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2653e51060f0SSean Hefty 			      void *context)
2654e51060f0SSean Hefty {
2655e51060f0SSean Hefty 	struct cma_work *work = context;
2656e51060f0SSean Hefty 	struct rdma_route *route;
2657e51060f0SSean Hefty 
2658e51060f0SSean Hefty 	route = &work->id->id.route;
2659e51060f0SSean Hefty 
2660e51060f0SSean Hefty 	if (!status) {
2661e51060f0SSean Hefty 		route->num_paths = 1;
2662e51060f0SSean Hefty 		*route->path_rec = *path_rec;
2663e51060f0SSean Hefty 	} else {
2664550e5ca7SNir Muchtar 		work->old_state = RDMA_CM_ROUTE_QUERY;
2665550e5ca7SNir Muchtar 		work->new_state = RDMA_CM_ADDR_RESOLVED;
2666e51060f0SSean Hefty 		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
26678f0472d3SSean Hefty 		work->event.status = status;
2668498683c6SMoni Shoua 		pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2669498683c6SMoni Shoua 				     status);
2670e51060f0SSean Hefty 	}
2671e51060f0SSean Hefty 
2672e51060f0SSean Hefty 	queue_work(cma_wq, &work->work);
2673e51060f0SSean Hefty }
2674e51060f0SSean Hefty 
2675dbace111SLeon Romanovsky static int cma_query_ib_route(struct rdma_id_private *id_priv,
2676dbace111SLeon Romanovsky 			      unsigned long timeout_ms, struct cma_work *work)
2677e51060f0SSean Hefty {
2678f4753834SSean Hefty 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2679c2f8fc4eSDasaratharaman Chandramouli 	struct sa_path_rec path_rec;
2680a81c994dSSean Hefty 	ib_sa_comp_mask comp_mask;
2681a81c994dSSean Hefty 	struct sockaddr_in6 *sin6;
2682f68194caSSean Hefty 	struct sockaddr_ib *sib;
2683e51060f0SSean Hefty 
2684e51060f0SSean Hefty 	memset(&path_rec, 0, sizeof path_rec);
26854c33bd19SDasaratharaman Chandramouli 
26864c33bd19SDasaratharaman Chandramouli 	if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
26874c33bd19SDasaratharaman Chandramouli 		path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
26884c33bd19SDasaratharaman Chandramouli 	else
26899fdca4daSDasaratharaman Chandramouli 		path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2690f4753834SSean Hefty 	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2691f4753834SSean Hefty 	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2692f4753834SSean Hefty 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2693e51060f0SSean Hefty 	path_rec.numb_path = 1;
2694962063e6SSean Hefty 	path_rec.reversible = 1;
2695d3957b86SMajd Dibbiny 	path_rec.service_id = rdma_get_service_id(&id_priv->id,
2696d3957b86SMajd Dibbiny 						  cma_dst_addr(id_priv));
2697a81c994dSSean Hefty 
2698a81c994dSSean Hefty 	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2699a81c994dSSean Hefty 		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2700a81c994dSSean Hefty 		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2701a81c994dSSean Hefty 
2702f68194caSSean Hefty 	switch (cma_family(id_priv)) {
2703f68194caSSean Hefty 	case AF_INET:
2704a81c994dSSean Hefty 		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2705a81c994dSSean Hefty 		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2706f68194caSSean Hefty 		break;
2707f68194caSSean Hefty 	case AF_INET6:
2708f4753834SSean Hefty 		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2709a81c994dSSean Hefty 		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2710a81c994dSSean Hefty 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2711f68194caSSean Hefty 		break;
2712f68194caSSean Hefty 	case AF_IB:
2713f68194caSSean Hefty 		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2714f68194caSSean Hefty 		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2715f68194caSSean Hefty 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2716f68194caSSean Hefty 		break;
2717a81c994dSSean Hefty 	}
2718e51060f0SSean Hefty 
2719c1a0b23bSMichael S. Tsirkin 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2720e51060f0SSean Hefty 					       id_priv->id.port_num, &path_rec,
2721a81c994dSSean Hefty 					       comp_mask, timeout_ms,
2722a81c994dSSean Hefty 					       GFP_KERNEL, cma_query_handler,
2723a81c994dSSean Hefty 					       work, &id_priv->query);
2724e51060f0SSean Hefty 
2725e51060f0SSean Hefty 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2726e51060f0SSean Hefty }
2727e51060f0SSean Hefty 
2728fe454dc3SAvihai Horon static void cma_iboe_join_work_handler(struct work_struct *work)
2729fe454dc3SAvihai Horon {
2730fe454dc3SAvihai Horon 	struct cma_multicast *mc =
2731fe454dc3SAvihai Horon 		container_of(work, struct cma_multicast, iboe_join.work);
2732fe454dc3SAvihai Horon 	struct rdma_cm_event *event = &mc->iboe_join.event;
2733fe454dc3SAvihai Horon 	struct rdma_id_private *id_priv = mc->id_priv;
2734fe454dc3SAvihai Horon 	int ret;
2735fe454dc3SAvihai Horon 
2736fe454dc3SAvihai Horon 	mutex_lock(&id_priv->handler_mutex);
2737fe454dc3SAvihai Horon 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2738fe454dc3SAvihai Horon 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2739fe454dc3SAvihai Horon 		goto out_unlock;
2740fe454dc3SAvihai Horon 
2741fe454dc3SAvihai Horon 	ret = cma_cm_event_handler(id_priv, event);
2742fe454dc3SAvihai Horon 	WARN_ON(ret);
2743fe454dc3SAvihai Horon 
2744fe454dc3SAvihai Horon out_unlock:
2745fe454dc3SAvihai Horon 	mutex_unlock(&id_priv->handler_mutex);
2746fe454dc3SAvihai Horon 	if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2747fe454dc3SAvihai Horon 		rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2748fe454dc3SAvihai Horon }
2749fe454dc3SAvihai Horon 
2750c4028958SDavid Howells static void cma_work_handler(struct work_struct *_work)
2751e51060f0SSean Hefty {
2752c4028958SDavid Howells 	struct cma_work *work = container_of(_work, struct cma_work, work);
2753e51060f0SSean Hefty 	struct rdma_id_private *id_priv = work->id;
2754e51060f0SSean Hefty 
2755de910bd9SOr Gerlitz 	mutex_lock(&id_priv->handler_mutex);
27567e85bcdaSJason Gunthorpe 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
27577e85bcdaSJason Gunthorpe 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
27587e85bcdaSJason Gunthorpe 		goto out_unlock;
27597e85bcdaSJason Gunthorpe 	if (work->old_state != 0 || work->new_state != 0) {
2760e51060f0SSean Hefty 		if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2761f6a9d47aSJason Gunthorpe 			goto out_unlock;
2762e51060f0SSean Hefty 	}
2763e51060f0SSean Hefty 
2764e51060f0SSean Hefty 	if (cma_cm_event_handler(id_priv, &work->event)) {
2765e51060f0SSean Hefty 		cma_id_put(id_priv);
2766e51060f0SSean Hefty 		destroy_id_handler_unlock(id_priv);
2767e51060f0SSean Hefty 		goto out_free;
2768e51060f0SSean Hefty 	}
2769f6a9d47aSJason Gunthorpe 
2770f6a9d47aSJason Gunthorpe out_unlock:
2771de910bd9SOr Gerlitz 	mutex_unlock(&id_priv->handler_mutex);
2772e368d23fSParav Pandit 	cma_id_put(id_priv);
2773f6a9d47aSJason Gunthorpe out_free:
2774b5de0c60SJason Gunthorpe 	if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
2775b5de0c60SJason Gunthorpe 		rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
2776dd5bdff8SOr Gerlitz 	kfree(work);
2777dd5bdff8SOr Gerlitz }
2778dd5bdff8SOr Gerlitz 
2779981b5a23SParav Pandit static void cma_init_resolve_route_work(struct cma_work *work,
2780981b5a23SParav Pandit 					struct rdma_id_private *id_priv)
2781981b5a23SParav Pandit {
2782981b5a23SParav Pandit 	work->id = id_priv;
2783981b5a23SParav Pandit 	INIT_WORK(&work->work, cma_work_handler);
2784981b5a23SParav Pandit 	work->old_state = RDMA_CM_ROUTE_QUERY;
2785981b5a23SParav Pandit 	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2786981b5a23SParav Pandit 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2787981b5a23SParav Pandit }
2788981b5a23SParav Pandit 
2789081ea519SParav Pandit static void enqueue_resolve_addr_work(struct cma_work *work,
2790981b5a23SParav Pandit 				      struct rdma_id_private *id_priv)
2791981b5a23SParav Pandit {
2792e368d23fSParav Pandit 	/* Balances with cma_id_put() in cma_work_handler */
2793e368d23fSParav Pandit 	cma_id_get(id_priv);
2794081ea519SParav Pandit 
2795981b5a23SParav Pandit 	work->id = id_priv;
2796981b5a23SParav Pandit 	INIT_WORK(&work->work, cma_work_handler);
2797981b5a23SParav Pandit 	work->old_state = RDMA_CM_ADDR_QUERY;
2798981b5a23SParav Pandit 	work->new_state = RDMA_CM_ADDR_RESOLVED;
2799981b5a23SParav Pandit 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2800081ea519SParav Pandit 
2801081ea519SParav Pandit 	queue_work(cma_wq, &work->work);
2802981b5a23SParav Pandit }
2803981b5a23SParav Pandit 
2804dbace111SLeon Romanovsky static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
2805dbace111SLeon Romanovsky 				unsigned long timeout_ms)
2806e51060f0SSean Hefty {
2807e51060f0SSean Hefty 	struct rdma_route *route = &id_priv->id.route;
2808e51060f0SSean Hefty 	struct cma_work *work;
2809e51060f0SSean Hefty 	int ret;
2810e51060f0SSean Hefty 
2811e51060f0SSean Hefty 	work = kzalloc(sizeof *work, GFP_KERNEL);
2812e51060f0SSean Hefty 	if (!work)
2813e51060f0SSean Hefty 		return -ENOMEM;
2814e51060f0SSean Hefty 
2815981b5a23SParav Pandit 	cma_init_resolve_route_work(work, id_priv);
2816e51060f0SSean Hefty 
2817e51060f0SSean Hefty 	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2818e51060f0SSean Hefty 	if (!route->path_rec) {
2819e51060f0SSean Hefty 		ret = -ENOMEM;
2820e51060f0SSean Hefty 		goto err1;
2821e51060f0SSean Hefty 	}
2822e51060f0SSean Hefty 
2823e51060f0SSean Hefty 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2824e51060f0SSean Hefty 	if (ret)
2825e51060f0SSean Hefty 		goto err2;
2826e51060f0SSean Hefty 
2827e51060f0SSean Hefty 	return 0;
2828e51060f0SSean Hefty err2:
2829e51060f0SSean Hefty 	kfree(route->path_rec);
2830e51060f0SSean Hefty 	route->path_rec = NULL;
2831e51060f0SSean Hefty err1:
2832e51060f0SSean Hefty 	kfree(work);
2833e51060f0SSean Hefty 	return ret;
2834e51060f0SSean Hefty }
2835e51060f0SSean Hefty 
28369327c7afSParav Pandit static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
28379327c7afSParav Pandit 					   unsigned long supported_gids,
28389327c7afSParav Pandit 					   enum ib_gid_type default_gid)
28399327c7afSParav Pandit {
28409327c7afSParav Pandit 	if ((network_type == RDMA_NETWORK_IPV4 ||
28419327c7afSParav Pandit 	     network_type == RDMA_NETWORK_IPV6) &&
28429327c7afSParav Pandit 	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
28439327c7afSParav Pandit 		return IB_GID_TYPE_ROCE_UDP_ENCAP;
28449327c7afSParav Pandit 
28459327c7afSParav Pandit 	return default_gid;
28469327c7afSParav Pandit }
28479327c7afSParav Pandit 
28489327c7afSParav Pandit /*
28499327c7afSParav Pandit  * cma_iboe_set_path_rec_l2_fields() is helper function which sets
28509327c7afSParav Pandit  * path record type based on GID type.
28519327c7afSParav Pandit  * It also sets up other L2 fields which includes destination mac address
28529327c7afSParav Pandit  * netdev ifindex, of the path record.
28539327c7afSParav Pandit  * It returns the netdev of the bound interface for this path record entry.
28549327c7afSParav Pandit  */
28559327c7afSParav Pandit static struct net_device *
28569327c7afSParav Pandit cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
28579327c7afSParav Pandit {
28589327c7afSParav Pandit 	struct rdma_route *route = &id_priv->id.route;
28599327c7afSParav Pandit 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
28609327c7afSParav Pandit 	struct rdma_addr *addr = &route->addr;
28619327c7afSParav Pandit 	unsigned long supported_gids;
28629327c7afSParav Pandit 	struct net_device *ndev;
28639327c7afSParav Pandit 
28649327c7afSParav Pandit 	if (!addr->dev_addr.bound_dev_if)
28659327c7afSParav Pandit 		return NULL;
28669327c7afSParav Pandit 
28679327c7afSParav Pandit 	ndev = dev_get_by_index(addr->dev_addr.net,
28689327c7afSParav Pandit 				addr->dev_addr.bound_dev_if);
28699327c7afSParav Pandit 	if (!ndev)
28709327c7afSParav Pandit 		return NULL;
28719327c7afSParav Pandit 
28729327c7afSParav Pandit 	supported_gids = roce_gid_type_mask_support(id_priv->id.device,
28739327c7afSParav Pandit 						    id_priv->id.port_num);
28749327c7afSParav Pandit 	gid_type = cma_route_gid_type(addr->dev_addr.network,
28759327c7afSParav Pandit 				      supported_gids,
28769327c7afSParav Pandit 				      id_priv->gid_type);
28779327c7afSParav Pandit 	/* Use the hint from IP Stack to select GID Type */
28789327c7afSParav Pandit 	if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
28799327c7afSParav Pandit 		gid_type = ib_network_to_gid_type(addr->dev_addr.network);
28809327c7afSParav Pandit 	route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
28819327c7afSParav Pandit 
2882114cc9c4SParav Pandit 	route->path_rec->roce.route_resolved = true;
28839327c7afSParav Pandit 	sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
28849327c7afSParav Pandit 	return ndev;
28859327c7afSParav Pandit }
28869327c7afSParav Pandit 
2887fe75889fSParav Pandit int rdma_set_ib_path(struct rdma_cm_id *id,
2888fe75889fSParav Pandit 		     struct sa_path_rec *path_rec)
2889e51060f0SSean Hefty {
2890e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
28918d20a1f0SParav Pandit 	struct net_device *ndev;
2892e51060f0SSean Hefty 	int ret;
2893e51060f0SSean Hefty 
2894e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
2895550e5ca7SNir Muchtar 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2896550e5ca7SNir Muchtar 			   RDMA_CM_ROUTE_RESOLVED))
2897e51060f0SSean Hefty 		return -EINVAL;
2898e51060f0SSean Hefty 
2899fe75889fSParav Pandit 	id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
29009893e742SJulia Lawall 				     GFP_KERNEL);
2901e51060f0SSean Hefty 	if (!id->route.path_rec) {
2902e51060f0SSean Hefty 		ret = -ENOMEM;
2903e51060f0SSean Hefty 		goto err;
2904e51060f0SSean Hefty 	}
2905e51060f0SSean Hefty 
29068d20a1f0SParav Pandit 	if (rdma_protocol_roce(id->device, id->port_num)) {
29078d20a1f0SParav Pandit 		ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
29088d20a1f0SParav Pandit 		if (!ndev) {
29098d20a1f0SParav Pandit 			ret = -ENODEV;
29108d20a1f0SParav Pandit 			goto err_free;
29118d20a1f0SParav Pandit 		}
29128d20a1f0SParav Pandit 		dev_put(ndev);
29138d20a1f0SParav Pandit 	}
29148d20a1f0SParav Pandit 
2915fe75889fSParav Pandit 	id->route.num_paths = 1;
2916e51060f0SSean Hefty 	return 0;
29178d20a1f0SParav Pandit 
29188d20a1f0SParav Pandit err_free:
29198d20a1f0SParav Pandit 	kfree(id->route.path_rec);
29208d20a1f0SParav Pandit 	id->route.path_rec = NULL;
2921e51060f0SSean Hefty err:
2922550e5ca7SNir Muchtar 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2923e51060f0SSean Hefty 	return ret;
2924e51060f0SSean Hefty }
2925fe75889fSParav Pandit EXPORT_SYMBOL(rdma_set_ib_path);
2926e51060f0SSean Hefty 
2927d6f91252SLeon Romanovsky static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
292807ebafbaSTom Tucker {
292907ebafbaSTom Tucker 	struct cma_work *work;
293007ebafbaSTom Tucker 
293107ebafbaSTom Tucker 	work = kzalloc(sizeof *work, GFP_KERNEL);
293207ebafbaSTom Tucker 	if (!work)
293307ebafbaSTom Tucker 		return -ENOMEM;
293407ebafbaSTom Tucker 
2935981b5a23SParav Pandit 	cma_init_resolve_route_work(work, id_priv);
293607ebafbaSTom Tucker 	queue_work(cma_wq, &work->work);
293707ebafbaSTom Tucker 	return 0;
293807ebafbaSTom Tucker }
293907ebafbaSTom Tucker 
2940d3bd9396SParav Pandit static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
2941eb072c4bSEyal Perry {
2942eb072c4bSEyal Perry 	struct net_device *dev;
2943eb072c4bSEyal Perry 
2944d3bd9396SParav Pandit 	dev = vlan_dev_real_dev(vlan_ndev);
2945eb072c4bSEyal Perry 	if (dev->num_tc)
2946eb072c4bSEyal Perry 		return netdev_get_prio_tc_map(dev, prio);
2947eb072c4bSEyal Perry 
2948d3bd9396SParav Pandit 	return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
2949eb072c4bSEyal Perry 		VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2950d3bd9396SParav Pandit }
2951d3bd9396SParav Pandit 
2952d3bd9396SParav Pandit struct iboe_prio_tc_map {
2953d3bd9396SParav Pandit 	int input_prio;
2954d3bd9396SParav Pandit 	int output_tc;
2955d3bd9396SParav Pandit 	bool found;
2956d3bd9396SParav Pandit };
2957d3bd9396SParav Pandit 
2958eff74233STaehee Yoo static int get_lower_vlan_dev_tc(struct net_device *dev,
2959eff74233STaehee Yoo 				 struct netdev_nested_priv *priv)
2960d3bd9396SParav Pandit {
2961eff74233STaehee Yoo 	struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
2962d3bd9396SParav Pandit 
2963d3bd9396SParav Pandit 	if (is_vlan_dev(dev))
2964d3bd9396SParav Pandit 		map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
2965d3bd9396SParav Pandit 	else if (dev->num_tc)
2966d3bd9396SParav Pandit 		map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
2967d3bd9396SParav Pandit 	else
2968d3bd9396SParav Pandit 		map->output_tc = 0;
2969d3bd9396SParav Pandit 	/* We are interested only in first level VLAN device, so always
2970d3bd9396SParav Pandit 	 * return 1 to stop iterating over next level devices.
2971d3bd9396SParav Pandit 	 */
2972d3bd9396SParav Pandit 	map->found = true;
2973d3bd9396SParav Pandit 	return 1;
2974d3bd9396SParav Pandit }
2975d3bd9396SParav Pandit 
2976d3bd9396SParav Pandit static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2977d3bd9396SParav Pandit {
2978d3bd9396SParav Pandit 	struct iboe_prio_tc_map prio_tc_map = {};
2979d3bd9396SParav Pandit 	int prio = rt_tos2priority(tos);
2980eff74233STaehee Yoo 	struct netdev_nested_priv priv;
2981d3bd9396SParav Pandit 
2982d3bd9396SParav Pandit 	/* If VLAN device, get it directly from the VLAN netdev */
2983d3bd9396SParav Pandit 	if (is_vlan_dev(ndev))
2984d3bd9396SParav Pandit 		return get_vlan_ndev_tc(ndev, prio);
2985d3bd9396SParav Pandit 
2986d3bd9396SParav Pandit 	prio_tc_map.input_prio = prio;
2987eff74233STaehee Yoo 	priv.data = (void *)&prio_tc_map;
2988d3bd9396SParav Pandit 	rcu_read_lock();
2989d3bd9396SParav Pandit 	netdev_walk_all_lower_dev_rcu(ndev,
2990d3bd9396SParav Pandit 				      get_lower_vlan_dev_tc,
2991eff74233STaehee Yoo 				      &priv);
2992d3bd9396SParav Pandit 	rcu_read_unlock();
2993d3bd9396SParav Pandit 	/* If map is found from lower device, use it; Otherwise
2994d3bd9396SParav Pandit 	 * continue with the current netdevice to get priority to tc map.
2995d3bd9396SParav Pandit 	 */
2996d3bd9396SParav Pandit 	if (prio_tc_map.found)
2997d3bd9396SParav Pandit 		return prio_tc_map.output_tc;
2998d3bd9396SParav Pandit 	else if (ndev->num_tc)
2999d3bd9396SParav Pandit 		return netdev_get_prio_tc_map(ndev, prio);
3000d3bd9396SParav Pandit 	else
3001eb072c4bSEyal Perry 		return 0;
3002eb072c4bSEyal Perry }
3003eb072c4bSEyal Perry 
3004f6653405SMark Zhang static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
3005f6653405SMark Zhang {
3006f6653405SMark Zhang 	struct sockaddr_in6 *addr6;
3007f6653405SMark Zhang 	u16 dport, sport;
3008f6653405SMark Zhang 	u32 hash, fl;
3009f6653405SMark Zhang 
3010f6653405SMark Zhang 	addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3011f6653405SMark Zhang 	fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3012f6653405SMark Zhang 	if ((cma_family(id_priv) != AF_INET6) || !fl) {
3013f6653405SMark Zhang 		dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3014f6653405SMark Zhang 		sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3015f6653405SMark Zhang 		hash = (u32)sport * 31 + dport;
3016f6653405SMark Zhang 		fl = hash & IB_GRH_FLOWLABEL_MASK;
3017f6653405SMark Zhang 	}
3018f6653405SMark Zhang 
3019f6653405SMark Zhang 	return cpu_to_be32(fl);
3020f6653405SMark Zhang }
3021f6653405SMark Zhang 
30223c86aa70SEli Cohen static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
30233c86aa70SEli Cohen {
30243c86aa70SEli Cohen 	struct rdma_route *route = &id_priv->id.route;
30253c86aa70SEli Cohen 	struct rdma_addr *addr = &route->addr;
30263c86aa70SEli Cohen 	struct cma_work *work;
30273c86aa70SEli Cohen 	int ret;
30284367ec7fSParav Pandit 	struct net_device *ndev;
30294367ec7fSParav Pandit 
303089052d78SMajd Dibbiny 	u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
303189052d78SMajd Dibbiny 					rdma_start_port(id_priv->cma_dev->device)];
3032ca0c448dSHåkon Bugge 	u8 tos;
3033dd5f03beSMatan Barak 
3034ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
3035ca0c448dSHåkon Bugge 	tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3036ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
30373c86aa70SEli Cohen 
30383c86aa70SEli Cohen 	work = kzalloc(sizeof *work, GFP_KERNEL);
30393c86aa70SEli Cohen 	if (!work)
30403c86aa70SEli Cohen 		return -ENOMEM;
30413c86aa70SEli Cohen 
30423c86aa70SEli Cohen 	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
30433c86aa70SEli Cohen 	if (!route->path_rec) {
30443c86aa70SEli Cohen 		ret = -ENOMEM;
30453c86aa70SEli Cohen 		goto err1;
30463c86aa70SEli Cohen 	}
30473c86aa70SEli Cohen 
30483c86aa70SEli Cohen 	route->num_paths = 1;
30493c86aa70SEli Cohen 
30509327c7afSParav Pandit 	ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
305123d70503SWei Yongjun 	if (!ndev) {
305223d70503SWei Yongjun 		ret = -ENODEV;
305323d70503SWei Yongjun 		goto err2;
305423d70503SWei Yongjun 	}
305520029832SMatan Barak 
30567b85627bSMoni Shoua 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
30577b85627bSMoni Shoua 		    &route->path_rec->sgid);
30587b85627bSMoni Shoua 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
30597b85627bSMoni Shoua 		    &route->path_rec->dgid);
3060af7bd463SEli Cohen 
3061c3efe750SMatan Barak 	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3062c865f246SSomnath Kotur 		/* TODO: get the hoplimit from the inet/inet6 device */
3063c3efe750SMatan Barak 		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3064c3efe750SMatan Barak 	else
3065af7bd463SEli Cohen 		route->path_rec->hop_limit = 1;
3066af7bd463SEli Cohen 	route->path_rec->reversible = 1;
3067af7bd463SEli Cohen 	route->path_rec->pkey = cpu_to_be16(0xffff);
3068af7bd463SEli Cohen 	route->path_rec->mtu_selector = IB_SA_EQ;
306989052d78SMajd Dibbiny 	route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
307089052d78SMajd Dibbiny 	route->path_rec->traffic_class = tos;
30713c86aa70SEli Cohen 	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
30723c86aa70SEli Cohen 	route->path_rec->rate_selector = IB_SA_EQ;
30733c86aa70SEli Cohen 	route->path_rec->rate = iboe_get_rate(ndev);
30743c86aa70SEli Cohen 	dev_put(ndev);
30753c86aa70SEli Cohen 	route->path_rec->packet_life_time_selector = IB_SA_EQ;
3076e1ee1e62SDag Moxnes 	/* In case ACK timeout is set, use this value to calculate
3077e1ee1e62SDag Moxnes 	 * PacketLifeTime.  As per IBTA 12.7.34,
3078e1ee1e62SDag Moxnes 	 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3079e1ee1e62SDag Moxnes 	 * Assuming a negligible local ACK delay, we can use
3080e1ee1e62SDag Moxnes 	 * PacketLifeTime = local ACK timeout/2
3081e1ee1e62SDag Moxnes 	 * as a reasonable approximation for RoCE networks.
3082e1ee1e62SDag Moxnes 	 */
3083ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
3084*e84045eaSHåkon Bugge 	if (id_priv->timeout_set && id_priv->timeout)
3085*e84045eaSHåkon Bugge 		route->path_rec->packet_life_time = id_priv->timeout - 1;
3086*e84045eaSHåkon Bugge 	else
3087*e84045eaSHåkon Bugge 		route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
3088ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
3089e1ee1e62SDag Moxnes 
30903c86aa70SEli Cohen 	if (!route->path_rec->mtu) {
30913c86aa70SEli Cohen 		ret = -EINVAL;
30923c86aa70SEli Cohen 		goto err2;
30933c86aa70SEli Cohen 	}
30943c86aa70SEli Cohen 
3095f6653405SMark Zhang 	if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3096f6653405SMark Zhang 					 id_priv->id.port_num))
3097f6653405SMark Zhang 		route->path_rec->flow_label =
3098f6653405SMark Zhang 			cma_get_roce_udp_flow_label(id_priv);
3099f6653405SMark Zhang 
3100981b5a23SParav Pandit 	cma_init_resolve_route_work(work, id_priv);
31013c86aa70SEli Cohen 	queue_work(cma_wq, &work->work);
31023c86aa70SEli Cohen 
31033c86aa70SEli Cohen 	return 0;
31043c86aa70SEli Cohen 
31053c86aa70SEli Cohen err2:
31063c86aa70SEli Cohen 	kfree(route->path_rec);
31073c86aa70SEli Cohen 	route->path_rec = NULL;
3108987914abSAvihai Horon 	route->num_paths = 0;
31093c86aa70SEli Cohen err1:
31103c86aa70SEli Cohen 	kfree(work);
31113c86aa70SEli Cohen 	return ret;
31123c86aa70SEli Cohen }
31133c86aa70SEli Cohen 
3114dbace111SLeon Romanovsky int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3115e51060f0SSean Hefty {
3116e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
3117e51060f0SSean Hefty 	int ret;
3118e51060f0SSean Hefty 
3119e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
3120550e5ca7SNir Muchtar 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3121e51060f0SSean Hefty 		return -EINVAL;
3122e51060f0SSean Hefty 
3123e368d23fSParav Pandit 	cma_id_get(id_priv);
3124fe53ba2fSMichael Wang 	if (rdma_cap_ib_sa(id->device, id->port_num))
3125e51060f0SSean Hefty 		ret = cma_resolve_ib_route(id_priv, timeout_ms);
31265d9fb044SIra Weiny 	else if (rdma_protocol_roce(id->device, id->port_num))
31273c86aa70SEli Cohen 		ret = cma_resolve_iboe_route(id_priv);
3128c72f2189SMichael Wang 	else if (rdma_protocol_iwarp(id->device, id->port_num))
3129d6f91252SLeon Romanovsky 		ret = cma_resolve_iw_route(id_priv);
3130c72f2189SMichael Wang 	else
3131e51060f0SSean Hefty 		ret = -ENOSYS;
3132c72f2189SMichael Wang 
3133e51060f0SSean Hefty 	if (ret)
3134e51060f0SSean Hefty 		goto err;
3135e51060f0SSean Hefty 
3136e51060f0SSean Hefty 	return 0;
3137e51060f0SSean Hefty err:
3138550e5ca7SNir Muchtar 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3139e368d23fSParav Pandit 	cma_id_put(id_priv);
3140e51060f0SSean Hefty 	return ret;
3141e51060f0SSean Hefty }
3142e51060f0SSean Hefty EXPORT_SYMBOL(rdma_resolve_route);
3143e51060f0SSean Hefty 
31446a3e362dSSean Hefty static void cma_set_loopback(struct sockaddr *addr)
31456a3e362dSSean Hefty {
31466a3e362dSSean Hefty 	switch (addr->sa_family) {
31476a3e362dSSean Hefty 	case AF_INET:
31486a3e362dSSean Hefty 		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
31496a3e362dSSean Hefty 		break;
31506a3e362dSSean Hefty 	case AF_INET6:
31516a3e362dSSean Hefty 		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
31526a3e362dSSean Hefty 			      0, 0, 0, htonl(1));
31536a3e362dSSean Hefty 		break;
31546a3e362dSSean Hefty 	default:
31556a3e362dSSean Hefty 		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
31566a3e362dSSean Hefty 			    0, 0, 0, htonl(1));
31576a3e362dSSean Hefty 		break;
31586a3e362dSSean Hefty 	}
31596a3e362dSSean Hefty }
31606a3e362dSSean Hefty 
3161e51060f0SSean Hefty static int cma_bind_loopback(struct rdma_id_private *id_priv)
3162e51060f0SSean Hefty {
3163b0569e40SSean Hefty 	struct cma_device *cma_dev, *cur_dev;
3164f0ee3404SMichael S. Tsirkin 	union ib_gid gid;
3165102c5ce0SJack Wang 	enum ib_port_state port_state;
3166cc055dd3SParav Pandit 	unsigned int p;
3167e51060f0SSean Hefty 	u16 pkey;
3168e51060f0SSean Hefty 	int ret;
3169e51060f0SSean Hefty 
3170b0569e40SSean Hefty 	cma_dev = NULL;
3171e51060f0SSean Hefty 	mutex_lock(&lock);
3172b0569e40SSean Hefty 	list_for_each_entry(cur_dev, &dev_list, list) {
3173b0569e40SSean Hefty 		if (cma_family(id_priv) == AF_IB &&
317472219ceaSMichael Wang 		    !rdma_cap_ib_cm(cur_dev->device, 1))
3175b0569e40SSean Hefty 			continue;
3176b0569e40SSean Hefty 
3177b0569e40SSean Hefty 		if (!cma_dev)
3178b0569e40SSean Hefty 			cma_dev = cur_dev;
3179b0569e40SSean Hefty 
3180cc055dd3SParav Pandit 		rdma_for_each_port (cur_dev->device, p) {
3181102c5ce0SJack Wang 			if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3182102c5ce0SJack Wang 			    port_state == IB_PORT_ACTIVE) {
3183b0569e40SSean Hefty 				cma_dev = cur_dev;
3184b0569e40SSean Hefty 				goto port_found;
3185b0569e40SSean Hefty 			}
3186b0569e40SSean Hefty 		}
3187b0569e40SSean Hefty 	}
3188b0569e40SSean Hefty 
3189b0569e40SSean Hefty 	if (!cma_dev) {
3190e82153b5SKrishna Kumar 		ret = -ENODEV;
3191e82153b5SKrishna Kumar 		goto out;
3192e82153b5SKrishna Kumar 	}
3193e51060f0SSean Hefty 
3194e51060f0SSean Hefty 	p = 1;
3195e51060f0SSean Hefty 
3196e51060f0SSean Hefty port_found:
31971dfce294SParav Pandit 	ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3198e51060f0SSean Hefty 	if (ret)
3199e51060f0SSean Hefty 		goto out;
3200e51060f0SSean Hefty 
3201e51060f0SSean Hefty 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3202e51060f0SSean Hefty 	if (ret)
3203e51060f0SSean Hefty 		goto out;
3204e51060f0SSean Hefty 
32056f8372b6SSean Hefty 	id_priv->id.route.addr.dev_addr.dev_type =
320621655afcSMichael Wang 		(rdma_protocol_ib(cma_dev->device, p)) ?
32076f8372b6SSean Hefty 		ARPHRD_INFINIBAND : ARPHRD_ETHER;
32086f8372b6SSean Hefty 
32096f8372b6SSean Hefty 	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3210e51060f0SSean Hefty 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3211e51060f0SSean Hefty 	id_priv->id.port_num = p;
3212e51060f0SSean Hefty 	cma_attach_to_dev(id_priv, cma_dev);
3213cb5cd0eaSShay Drory 	rdma_restrack_add(&id_priv->res);
3214f4753834SSean Hefty 	cma_set_loopback(cma_src_addr(id_priv));
3215e51060f0SSean Hefty out:
3216e51060f0SSean Hefty 	mutex_unlock(&lock);
3217e51060f0SSean Hefty 	return ret;
3218e51060f0SSean Hefty }
3219e51060f0SSean Hefty 
3220e51060f0SSean Hefty static void addr_handler(int status, struct sockaddr *src_addr,
3221e51060f0SSean Hefty 			 struct rdma_dev_addr *dev_addr, void *context)
3222e51060f0SSean Hefty {
3223e51060f0SSean Hefty 	struct rdma_id_private *id_priv = context;
32247582df82SParav Pandit 	struct rdma_cm_event event = {};
32255fc01fb8SMyungho Jung 	struct sockaddr *addr;
32265fc01fb8SMyungho Jung 	struct sockaddr_storage old_addr;
3227e51060f0SSean Hefty 
3228de910bd9SOr Gerlitz 	mutex_lock(&id_priv->handler_mutex);
3229550e5ca7SNir Muchtar 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3230550e5ca7SNir Muchtar 			   RDMA_CM_ADDR_RESOLVED))
323161a73c70SSean Hefty 		goto out;
323261a73c70SSean Hefty 
32335fc01fb8SMyungho Jung 	/*
32345fc01fb8SMyungho Jung 	 * Store the previous src address, so that if we fail to acquire
32355fc01fb8SMyungho Jung 	 * matching rdma device, old address can be restored back, which helps
32365fc01fb8SMyungho Jung 	 * to cancel the cma listen operation correctly.
32375fc01fb8SMyungho Jung 	 */
32385fc01fb8SMyungho Jung 	addr = cma_src_addr(id_priv);
32395fc01fb8SMyungho Jung 	memcpy(&old_addr, addr, rdma_addr_size(addr));
32405fc01fb8SMyungho Jung 	memcpy(addr, src_addr, rdma_addr_size(src_addr));
3241498683c6SMoni Shoua 	if (!status && !id_priv->cma_dev) {
3242ff11c6cdSParav Pandit 		status = cma_acquire_dev_by_src_ip(id_priv);
3243498683c6SMoni Shoua 		if (status)
3244498683c6SMoni Shoua 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3245498683c6SMoni Shoua 					     status);
3246cb5cd0eaSShay Drory 		rdma_restrack_add(&id_priv->res);
3247a6e4d254SHåkon Bugge 	} else if (status) {
3248498683c6SMoni Shoua 		pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3249498683c6SMoni Shoua 	}
3250e51060f0SSean Hefty 
3251e51060f0SSean Hefty 	if (status) {
32525fc01fb8SMyungho Jung 		memcpy(addr, &old_addr,
32535fc01fb8SMyungho Jung 		       rdma_addr_size((struct sockaddr *)&old_addr));
3254550e5ca7SNir Muchtar 		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3255550e5ca7SNir Muchtar 				   RDMA_CM_ADDR_BOUND))
3256e51060f0SSean Hefty 			goto out;
3257a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_ADDR_ERROR;
3258a1b1b61fSSean Hefty 		event.status = status;
32597b85627bSMoni Shoua 	} else
3260a1b1b61fSSean Hefty 		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3261e51060f0SSean Hefty 
3262ed999f82SChuck Lever 	if (cma_cm_event_handler(id_priv, &event)) {
3263f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(id_priv);
3264e51060f0SSean Hefty 		return;
3265e51060f0SSean Hefty 	}
3266e51060f0SSean Hefty out:
3267de910bd9SOr Gerlitz 	mutex_unlock(&id_priv->handler_mutex);
3268e51060f0SSean Hefty }
3269e51060f0SSean Hefty 
3270e51060f0SSean Hefty static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3271e51060f0SSean Hefty {
3272e51060f0SSean Hefty 	struct cma_work *work;
3273f0ee3404SMichael S. Tsirkin 	union ib_gid gid;
3274e51060f0SSean Hefty 	int ret;
3275e51060f0SSean Hefty 
3276e51060f0SSean Hefty 	work = kzalloc(sizeof *work, GFP_KERNEL);
3277e51060f0SSean Hefty 	if (!work)
3278e51060f0SSean Hefty 		return -ENOMEM;
3279e51060f0SSean Hefty 
3280e51060f0SSean Hefty 	if (!id_priv->cma_dev) {
3281e51060f0SSean Hefty 		ret = cma_bind_loopback(id_priv);
3282e51060f0SSean Hefty 		if (ret)
3283e51060f0SSean Hefty 			goto err;
3284e51060f0SSean Hefty 	}
3285e51060f0SSean Hefty 
32866f8372b6SSean Hefty 	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
32876f8372b6SSean Hefty 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3288e51060f0SSean Hefty 
3289081ea519SParav Pandit 	enqueue_resolve_addr_work(work, id_priv);
3290e51060f0SSean Hefty 	return 0;
3291e51060f0SSean Hefty err:
3292e51060f0SSean Hefty 	kfree(work);
3293e51060f0SSean Hefty 	return ret;
3294e51060f0SSean Hefty }
3295e51060f0SSean Hefty 
3296f17df3b0SSean Hefty static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3297f17df3b0SSean Hefty {
3298f17df3b0SSean Hefty 	struct cma_work *work;
3299f17df3b0SSean Hefty 	int ret;
3300f17df3b0SSean Hefty 
3301f17df3b0SSean Hefty 	work = kzalloc(sizeof *work, GFP_KERNEL);
3302f17df3b0SSean Hefty 	if (!work)
3303f17df3b0SSean Hefty 		return -ENOMEM;
3304f17df3b0SSean Hefty 
3305f17df3b0SSean Hefty 	if (!id_priv->cma_dev) {
3306f17df3b0SSean Hefty 		ret = cma_resolve_ib_dev(id_priv);
3307f17df3b0SSean Hefty 		if (ret)
3308f17df3b0SSean Hefty 			goto err;
3309f17df3b0SSean Hefty 	}
3310f17df3b0SSean Hefty 
3311f17df3b0SSean Hefty 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3312f17df3b0SSean Hefty 		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3313f17df3b0SSean Hefty 
3314081ea519SParav Pandit 	enqueue_resolve_addr_work(work, id_priv);
3315f17df3b0SSean Hefty 	return 0;
3316f17df3b0SSean Hefty err:
3317f17df3b0SSean Hefty 	kfree(work);
3318f17df3b0SSean Hefty 	return ret;
3319f17df3b0SSean Hefty }
3320f17df3b0SSean Hefty 
3321e51060f0SSean Hefty static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
33222df7dba8SParav Pandit 			 const struct sockaddr *dst_addr)
3323e51060f0SSean Hefty {
3324d14714dfSSean Hefty 	if (!src_addr || !src_addr->sa_family) {
3325d14714dfSSean Hefty 		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
3326f17df3b0SSean Hefty 		src_addr->sa_family = dst_addr->sa_family;
3327b4cfe397SJack Morgenstein 		if (IS_ENABLED(CONFIG_IPV6) &&
3328b4cfe397SJack Morgenstein 		    dst_addr->sa_family == AF_INET6) {
33296c26a771SSpencer Baugh 			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
33306c26a771SSpencer Baugh 			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
33316c26a771SSpencer Baugh 			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
33326c26a771SSpencer Baugh 			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
33336c26a771SSpencer Baugh 				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
3334f17df3b0SSean Hefty 		} else if (dst_addr->sa_family == AF_IB) {
3335f17df3b0SSean Hefty 			((struct sockaddr_ib *) src_addr)->sib_pkey =
3336f17df3b0SSean Hefty 				((struct sockaddr_ib *) dst_addr)->sib_pkey;
3337d14714dfSSean Hefty 		}
3338d14714dfSSean Hefty 	}
3339e51060f0SSean Hefty 	return rdma_bind_addr(id, src_addr);
3340e51060f0SSean Hefty }
3341e51060f0SSean Hefty 
3342732d41c5SJason Gunthorpe /*
3343732d41c5SJason Gunthorpe  * If required, resolve the source address for bind and leave the id_priv in
3344732d41c5SJason Gunthorpe  * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3345732d41c5SJason Gunthorpe  * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3346732d41c5SJason Gunthorpe  * ignored.
3347732d41c5SJason Gunthorpe  */
3348732d41c5SJason Gunthorpe static int resolve_prepare_src(struct rdma_id_private *id_priv,
3349732d41c5SJason Gunthorpe 			       struct sockaddr *src_addr,
3350732d41c5SJason Gunthorpe 			       const struct sockaddr *dst_addr)
3351e51060f0SSean Hefty {
3352e51060f0SSean Hefty 	int ret;
3353e51060f0SSean Hefty 
3354e4103312SParav Pandit 	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3355732d41c5SJason Gunthorpe 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3356732d41c5SJason Gunthorpe 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3357732d41c5SJason Gunthorpe 		ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3358732d41c5SJason Gunthorpe 		if (ret)
3359732d41c5SJason Gunthorpe 			goto err_dst;
3360732d41c5SJason Gunthorpe 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3361732d41c5SJason Gunthorpe 					   RDMA_CM_ADDR_QUERY))) {
3362732d41c5SJason Gunthorpe 			ret = -EINVAL;
3363732d41c5SJason Gunthorpe 			goto err_dst;
3364e51060f0SSean Hefty 		}
3365e4103312SParav Pandit 	}
3366e51060f0SSean Hefty 
3367e4103312SParav Pandit 	if (cma_family(id_priv) != dst_addr->sa_family) {
3368732d41c5SJason Gunthorpe 		ret = -EINVAL;
3369732d41c5SJason Gunthorpe 		goto err_state;
3370732d41c5SJason Gunthorpe 	}
3371732d41c5SJason Gunthorpe 	return 0;
3372732d41c5SJason Gunthorpe 
3373732d41c5SJason Gunthorpe err_state:
3374732d41c5SJason Gunthorpe 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3375732d41c5SJason Gunthorpe err_dst:
3376e4103312SParav Pandit 	memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3377732d41c5SJason Gunthorpe 	return ret;
3378e4103312SParav Pandit }
33794ae7152eSSean Hefty 
3380732d41c5SJason Gunthorpe int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3381732d41c5SJason Gunthorpe 		      const struct sockaddr *dst_addr, unsigned long timeout_ms)
3382732d41c5SJason Gunthorpe {
3383732d41c5SJason Gunthorpe 	struct rdma_id_private *id_priv =
3384732d41c5SJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
3385732d41c5SJason Gunthorpe 	int ret;
3386732d41c5SJason Gunthorpe 
3387732d41c5SJason Gunthorpe 	ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3388732d41c5SJason Gunthorpe 	if (ret)
3389732d41c5SJason Gunthorpe 		return ret;
3390e51060f0SSean Hefty 
3391f17df3b0SSean Hefty 	if (cma_any_addr(dst_addr)) {
3392e51060f0SSean Hefty 		ret = cma_resolve_loopback(id_priv);
3393f17df3b0SSean Hefty 	} else {
3394f17df3b0SSean Hefty 		if (dst_addr->sa_family == AF_IB) {
3395f17df3b0SSean Hefty 			ret = cma_resolve_ib_addr(id_priv);
3396f17df3b0SSean Hefty 		} else {
33970e9d2c19SParav Pandit 			ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
33980e9d2c19SParav Pandit 					      &id->route.addr.dev_addr,
33990e9d2c19SParav Pandit 					      timeout_ms, addr_handler,
34000e9d2c19SParav Pandit 					      false, id_priv);
3401f17df3b0SSean Hefty 		}
3402f17df3b0SSean Hefty 	}
3403e51060f0SSean Hefty 	if (ret)
3404e51060f0SSean Hefty 		goto err;
3405e51060f0SSean Hefty 
3406e51060f0SSean Hefty 	return 0;
3407e51060f0SSean Hefty err:
3408550e5ca7SNir Muchtar 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3409e51060f0SSean Hefty 	return ret;
3410e51060f0SSean Hefty }
3411e51060f0SSean Hefty EXPORT_SYMBOL(rdma_resolve_addr);
3412e51060f0SSean Hefty 
3413a9bb7912SHefty, Sean int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3414a9bb7912SHefty, Sean {
3415a9bb7912SHefty, Sean 	struct rdma_id_private *id_priv;
3416a9bb7912SHefty, Sean 	unsigned long flags;
3417a9bb7912SHefty, Sean 	int ret;
3418a9bb7912SHefty, Sean 
3419a9bb7912SHefty, Sean 	id_priv = container_of(id, struct rdma_id_private, id);
3420a9bb7912SHefty, Sean 	spin_lock_irqsave(&id_priv->lock, flags);
3421d490ee52SJason Gunthorpe 	if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3422d490ee52SJason Gunthorpe 	    id_priv->state == RDMA_CM_IDLE) {
3423a9bb7912SHefty, Sean 		id_priv->reuseaddr = reuse;
3424a9bb7912SHefty, Sean 		ret = 0;
3425a9bb7912SHefty, Sean 	} else {
3426a9bb7912SHefty, Sean 		ret = -EINVAL;
3427a9bb7912SHefty, Sean 	}
3428a9bb7912SHefty, Sean 	spin_unlock_irqrestore(&id_priv->lock, flags);
3429a9bb7912SHefty, Sean 	return ret;
3430a9bb7912SHefty, Sean }
3431a9bb7912SHefty, Sean EXPORT_SYMBOL(rdma_set_reuseaddr);
3432a9bb7912SHefty, Sean 
343368602120SSean Hefty int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
343468602120SSean Hefty {
343568602120SSean Hefty 	struct rdma_id_private *id_priv;
343668602120SSean Hefty 	unsigned long flags;
343768602120SSean Hefty 	int ret;
343868602120SSean Hefty 
343968602120SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
344068602120SSean Hefty 	spin_lock_irqsave(&id_priv->lock, flags);
344168602120SSean Hefty 	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
344268602120SSean Hefty 		id_priv->options |= (1 << CMA_OPTION_AFONLY);
344368602120SSean Hefty 		id_priv->afonly = afonly;
344468602120SSean Hefty 		ret = 0;
344568602120SSean Hefty 	} else {
344668602120SSean Hefty 		ret = -EINVAL;
344768602120SSean Hefty 	}
344868602120SSean Hefty 	spin_unlock_irqrestore(&id_priv->lock, flags);
344968602120SSean Hefty 	return ret;
345068602120SSean Hefty }
345168602120SSean Hefty EXPORT_SYMBOL(rdma_set_afonly);
345268602120SSean Hefty 
3453e51060f0SSean Hefty static void cma_bind_port(struct rdma_bind_list *bind_list,
3454e51060f0SSean Hefty 			  struct rdma_id_private *id_priv)
3455e51060f0SSean Hefty {
345658afdcb7SSean Hefty 	struct sockaddr *addr;
345758afdcb7SSean Hefty 	struct sockaddr_ib *sib;
345858afdcb7SSean Hefty 	u64 sid, mask;
345958afdcb7SSean Hefty 	__be16 port;
3460e51060f0SSean Hefty 
3461730c8912SMark Zhang 	lockdep_assert_held(&lock);
3462730c8912SMark Zhang 
3463f4753834SSean Hefty 	addr = cma_src_addr(id_priv);
346458afdcb7SSean Hefty 	port = htons(bind_list->port);
346558afdcb7SSean Hefty 
346658afdcb7SSean Hefty 	switch (addr->sa_family) {
346758afdcb7SSean Hefty 	case AF_INET:
346858afdcb7SSean Hefty 		((struct sockaddr_in *) addr)->sin_port = port;
346958afdcb7SSean Hefty 		break;
347058afdcb7SSean Hefty 	case AF_INET6:
347158afdcb7SSean Hefty 		((struct sockaddr_in6 *) addr)->sin6_port = port;
347258afdcb7SSean Hefty 		break;
347358afdcb7SSean Hefty 	case AF_IB:
347458afdcb7SSean Hefty 		sib = (struct sockaddr_ib *) addr;
347558afdcb7SSean Hefty 		sid = be64_to_cpu(sib->sib_sid);
347658afdcb7SSean Hefty 		mask = be64_to_cpu(sib->sib_sid_mask);
347758afdcb7SSean Hefty 		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
347858afdcb7SSean Hefty 		sib->sib_sid_mask = cpu_to_be64(~0ULL);
347958afdcb7SSean Hefty 		break;
348058afdcb7SSean Hefty 	}
3481e51060f0SSean Hefty 	id_priv->bind_list = bind_list;
3482e51060f0SSean Hefty 	hlist_add_head(&id_priv->node, &bind_list->owners);
3483e51060f0SSean Hefty }
3484e51060f0SSean Hefty 
34852253fc0cSSteve Wise static int cma_alloc_port(enum rdma_ucm_port_space ps,
3486aac978e1SHaggai Eran 			  struct rdma_id_private *id_priv, unsigned short snum)
3487e51060f0SSean Hefty {
3488e51060f0SSean Hefty 	struct rdma_bind_list *bind_list;
34893b069c5dSTejun Heo 	int ret;
3490e51060f0SSean Hefty 
3491730c8912SMark Zhang 	lockdep_assert_held(&lock);
3492730c8912SMark Zhang 
3493cb164b8cSSean Hefty 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3494e51060f0SSean Hefty 	if (!bind_list)
3495e51060f0SSean Hefty 		return -ENOMEM;
3496e51060f0SSean Hefty 
3497fa20105eSGuy Shapiro 	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3498fa20105eSGuy Shapiro 			   snum);
34993b069c5dSTejun Heo 	if (ret < 0)
35003b069c5dSTejun Heo 		goto err;
3501e51060f0SSean Hefty 
3502e51060f0SSean Hefty 	bind_list->ps = ps;
3503061ccb52SLeon Romanovsky 	bind_list->port = snum;
3504e51060f0SSean Hefty 	cma_bind_port(bind_list, id_priv);
3505e51060f0SSean Hefty 	return 0;
35063b069c5dSTejun Heo err:
3507aedec080SSean Hefty 	kfree(bind_list);
35083b069c5dSTejun Heo 	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3509aedec080SSean Hefty }
3510aedec080SSean Hefty 
351119b752a1SMoni Shoua static int cma_port_is_unique(struct rdma_bind_list *bind_list,
351219b752a1SMoni Shoua 			      struct rdma_id_private *id_priv)
351319b752a1SMoni Shoua {
351419b752a1SMoni Shoua 	struct rdma_id_private *cur_id;
351519b752a1SMoni Shoua 	struct sockaddr  *daddr = cma_dst_addr(id_priv);
351619b752a1SMoni Shoua 	struct sockaddr  *saddr = cma_src_addr(id_priv);
351719b752a1SMoni Shoua 	__be16 dport = cma_port(daddr);
351819b752a1SMoni Shoua 
3519730c8912SMark Zhang 	lockdep_assert_held(&lock);
3520730c8912SMark Zhang 
352119b752a1SMoni Shoua 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
352219b752a1SMoni Shoua 		struct sockaddr  *cur_daddr = cma_dst_addr(cur_id);
352319b752a1SMoni Shoua 		struct sockaddr  *cur_saddr = cma_src_addr(cur_id);
352419b752a1SMoni Shoua 		__be16 cur_dport = cma_port(cur_daddr);
352519b752a1SMoni Shoua 
352619b752a1SMoni Shoua 		if (id_priv == cur_id)
352719b752a1SMoni Shoua 			continue;
352819b752a1SMoni Shoua 
352919b752a1SMoni Shoua 		/* different dest port -> unique */
35309dea9a2fSTatyana Nikolova 		if (!cma_any_port(daddr) &&
35319dea9a2fSTatyana Nikolova 		    !cma_any_port(cur_daddr) &&
353219b752a1SMoni Shoua 		    (dport != cur_dport))
353319b752a1SMoni Shoua 			continue;
353419b752a1SMoni Shoua 
353519b752a1SMoni Shoua 		/* different src address -> unique */
353619b752a1SMoni Shoua 		if (!cma_any_addr(saddr) &&
353719b752a1SMoni Shoua 		    !cma_any_addr(cur_saddr) &&
353819b752a1SMoni Shoua 		    cma_addr_cmp(saddr, cur_saddr))
353919b752a1SMoni Shoua 			continue;
354019b752a1SMoni Shoua 
354119b752a1SMoni Shoua 		/* different dst address -> unique */
35429dea9a2fSTatyana Nikolova 		if (!cma_any_addr(daddr) &&
35439dea9a2fSTatyana Nikolova 		    !cma_any_addr(cur_daddr) &&
354419b752a1SMoni Shoua 		    cma_addr_cmp(daddr, cur_daddr))
354519b752a1SMoni Shoua 			continue;
354619b752a1SMoni Shoua 
354719b752a1SMoni Shoua 		return -EADDRNOTAVAIL;
354819b752a1SMoni Shoua 	}
354919b752a1SMoni Shoua 	return 0;
355019b752a1SMoni Shoua }
355119b752a1SMoni Shoua 
35522253fc0cSSteve Wise static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3553aac978e1SHaggai Eran 			      struct rdma_id_private *id_priv)
3554aedec080SSean Hefty {
35555d7220e8STetsuo Handa 	static unsigned int last_used_port;
35565d7220e8STetsuo Handa 	int low, high, remaining;
35575d7220e8STetsuo Handa 	unsigned int rover;
3558fa20105eSGuy Shapiro 	struct net *net = id_priv->id.route.addr.dev_addr.net;
3559aedec080SSean Hefty 
3560730c8912SMark Zhang 	lockdep_assert_held(&lock);
3561730c8912SMark Zhang 
3562fa20105eSGuy Shapiro 	inet_get_local_port_range(net, &low, &high);
35635d7220e8STetsuo Handa 	remaining = (high - low) + 1;
356463862b5bSAruna-Hewapathirane 	rover = prandom_u32() % remaining + low;
35655d7220e8STetsuo Handa retry:
356619b752a1SMoni Shoua 	if (last_used_port != rover) {
356719b752a1SMoni Shoua 		struct rdma_bind_list *bind_list;
356819b752a1SMoni Shoua 		int ret;
356919b752a1SMoni Shoua 
357019b752a1SMoni Shoua 		bind_list = cma_ps_find(net, ps, (unsigned short)rover);
357119b752a1SMoni Shoua 
357219b752a1SMoni Shoua 		if (!bind_list) {
357319b752a1SMoni Shoua 			ret = cma_alloc_port(ps, id_priv, rover);
357419b752a1SMoni Shoua 		} else {
357519b752a1SMoni Shoua 			ret = cma_port_is_unique(bind_list, id_priv);
357619b752a1SMoni Shoua 			if (!ret)
357719b752a1SMoni Shoua 				cma_bind_port(bind_list, id_priv);
357819b752a1SMoni Shoua 		}
35795d7220e8STetsuo Handa 		/*
35805d7220e8STetsuo Handa 		 * Remember previously used port number in order to avoid
35815d7220e8STetsuo Handa 		 * re-using same port immediately after it is closed.
35825d7220e8STetsuo Handa 		 */
35835d7220e8STetsuo Handa 		if (!ret)
35845d7220e8STetsuo Handa 			last_used_port = rover;
35855d7220e8STetsuo Handa 		if (ret != -EADDRNOTAVAIL)
35865d7220e8STetsuo Handa 			return ret;
35875d7220e8STetsuo Handa 	}
35885d7220e8STetsuo Handa 	if (--remaining) {
35895d7220e8STetsuo Handa 		rover++;
35905d7220e8STetsuo Handa 		if ((rover < low) || (rover > high))
35915d7220e8STetsuo Handa 			rover = low;
3592aedec080SSean Hefty 		goto retry;
3593aedec080SSean Hefty 	}
35945d7220e8STetsuo Handa 	return -EADDRNOTAVAIL;
3595e51060f0SSean Hefty }
3596e51060f0SSean Hefty 
3597a9bb7912SHefty, Sean /*
3598a9bb7912SHefty, Sean  * Check that the requested port is available.  This is called when trying to
3599a9bb7912SHefty, Sean  * bind to a specific port, or when trying to listen on a bound port.  In
3600a9bb7912SHefty, Sean  * the latter case, the provided id_priv may already be on the bind_list, but
3601a9bb7912SHefty, Sean  * we still need to check that it's okay to start listening.
3602a9bb7912SHefty, Sean  */
3603a9bb7912SHefty, Sean static int cma_check_port(struct rdma_bind_list *bind_list,
3604a9bb7912SHefty, Sean 			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3605e51060f0SSean Hefty {
3606e51060f0SSean Hefty 	struct rdma_id_private *cur_id;
360743b752daSHefty, Sean 	struct sockaddr *addr, *cur_addr;
3608e51060f0SSean Hefty 
3609730c8912SMark Zhang 	lockdep_assert_held(&lock);
3610730c8912SMark Zhang 
3611f4753834SSean Hefty 	addr = cma_src_addr(id_priv);
3612b67bfe0dSSasha Levin 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3613a9bb7912SHefty, Sean 		if (id_priv == cur_id)
3614a9bb7912SHefty, Sean 			continue;
3615a9bb7912SHefty, Sean 
3616d490ee52SJason Gunthorpe 		if (reuseaddr && cur_id->reuseaddr)
36175b0ec991SSean Hefty 			continue;
36185b0ec991SSean Hefty 
3619f4753834SSean Hefty 		cur_addr = cma_src_addr(cur_id);
36205b0ec991SSean Hefty 		if (id_priv->afonly && cur_id->afonly &&
36215b0ec991SSean Hefty 		    (addr->sa_family != cur_addr->sa_family))
36225b0ec991SSean Hefty 			continue;
36235b0ec991SSean Hefty 
36245b0ec991SSean Hefty 		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3625e51060f0SSean Hefty 			return -EADDRNOTAVAIL;
3626e51060f0SSean Hefty 
362743b752daSHefty, Sean 		if (!cma_addr_cmp(addr, cur_addr))
3628e51060f0SSean Hefty 			return -EADDRINUSE;
3629e51060f0SSean Hefty 	}
3630e51060f0SSean Hefty 	return 0;
3631e51060f0SSean Hefty }
3632e51060f0SSean Hefty 
36332253fc0cSSteve Wise static int cma_use_port(enum rdma_ucm_port_space ps,
3634aac978e1SHaggai Eran 			struct rdma_id_private *id_priv)
3635a9bb7912SHefty, Sean {
3636a9bb7912SHefty, Sean 	struct rdma_bind_list *bind_list;
3637a9bb7912SHefty, Sean 	unsigned short snum;
3638a9bb7912SHefty, Sean 	int ret;
3639a9bb7912SHefty, Sean 
3640730c8912SMark Zhang 	lockdep_assert_held(&lock);
3641730c8912SMark Zhang 
3642f4753834SSean Hefty 	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3643a9bb7912SHefty, Sean 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3644a9bb7912SHefty, Sean 		return -EACCES;
3645a9bb7912SHefty, Sean 
3646fa20105eSGuy Shapiro 	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3647a9bb7912SHefty, Sean 	if (!bind_list) {
3648a9bb7912SHefty, Sean 		ret = cma_alloc_port(ps, id_priv, snum);
3649a9bb7912SHefty, Sean 	} else {
3650a9bb7912SHefty, Sean 		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3651a9bb7912SHefty, Sean 		if (!ret)
3652a9bb7912SHefty, Sean 			cma_bind_port(bind_list, id_priv);
3653a9bb7912SHefty, Sean 	}
3654a9bb7912SHefty, Sean 	return ret;
3655a9bb7912SHefty, Sean }
3656a9bb7912SHefty, Sean 
36572253fc0cSSteve Wise static enum rdma_ucm_port_space
36582253fc0cSSteve Wise cma_select_inet_ps(struct rdma_id_private *id_priv)
365958afdcb7SSean Hefty {
366058afdcb7SSean Hefty 	switch (id_priv->id.ps) {
366158afdcb7SSean Hefty 	case RDMA_PS_TCP:
366258afdcb7SSean Hefty 	case RDMA_PS_UDP:
366358afdcb7SSean Hefty 	case RDMA_PS_IPOIB:
366458afdcb7SSean Hefty 	case RDMA_PS_IB:
3665aac978e1SHaggai Eran 		return id_priv->id.ps;
366658afdcb7SSean Hefty 	default:
3667aac978e1SHaggai Eran 
3668aac978e1SHaggai Eran 		return 0;
366958afdcb7SSean Hefty 	}
367058afdcb7SSean Hefty }
367158afdcb7SSean Hefty 
36722253fc0cSSteve Wise static enum rdma_ucm_port_space
36732253fc0cSSteve Wise cma_select_ib_ps(struct rdma_id_private *id_priv)
367458afdcb7SSean Hefty {
36752253fc0cSSteve Wise 	enum rdma_ucm_port_space ps = 0;
367658afdcb7SSean Hefty 	struct sockaddr_ib *sib;
367758afdcb7SSean Hefty 	u64 sid_ps, mask, sid;
367858afdcb7SSean Hefty 
3679f4753834SSean Hefty 	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
368058afdcb7SSean Hefty 	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
368158afdcb7SSean Hefty 	sid = be64_to_cpu(sib->sib_sid) & mask;
368258afdcb7SSean Hefty 
368358afdcb7SSean Hefty 	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
368458afdcb7SSean Hefty 		sid_ps = RDMA_IB_IP_PS_IB;
3685aac978e1SHaggai Eran 		ps = RDMA_PS_IB;
368658afdcb7SSean Hefty 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
368758afdcb7SSean Hefty 		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
368858afdcb7SSean Hefty 		sid_ps = RDMA_IB_IP_PS_TCP;
3689aac978e1SHaggai Eran 		ps = RDMA_PS_TCP;
369058afdcb7SSean Hefty 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
369158afdcb7SSean Hefty 		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
369258afdcb7SSean Hefty 		sid_ps = RDMA_IB_IP_PS_UDP;
3693aac978e1SHaggai Eran 		ps = RDMA_PS_UDP;
369458afdcb7SSean Hefty 	}
369558afdcb7SSean Hefty 
369658afdcb7SSean Hefty 	if (ps) {
369758afdcb7SSean Hefty 		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
369858afdcb7SSean Hefty 		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
369958afdcb7SSean Hefty 						be64_to_cpu(sib->sib_sid_mask));
370058afdcb7SSean Hefty 	}
370158afdcb7SSean Hefty 	return ps;
370258afdcb7SSean Hefty }
370358afdcb7SSean Hefty 
3704e51060f0SSean Hefty static int cma_get_port(struct rdma_id_private *id_priv)
3705e51060f0SSean Hefty {
37062253fc0cSSteve Wise 	enum rdma_ucm_port_space ps;
3707e51060f0SSean Hefty 	int ret;
3708e51060f0SSean Hefty 
3709f4753834SSean Hefty 	if (cma_family(id_priv) != AF_IB)
371058afdcb7SSean Hefty 		ps = cma_select_inet_ps(id_priv);
371158afdcb7SSean Hefty 	else
371258afdcb7SSean Hefty 		ps = cma_select_ib_ps(id_priv);
371358afdcb7SSean Hefty 	if (!ps)
3714e51060f0SSean Hefty 		return -EPROTONOSUPPORT;
3715e51060f0SSean Hefty 
3716e51060f0SSean Hefty 	mutex_lock(&lock);
3717f4753834SSean Hefty 	if (cma_any_port(cma_src_addr(id_priv)))
3718aedec080SSean Hefty 		ret = cma_alloc_any_port(ps, id_priv);
3719e51060f0SSean Hefty 	else
3720e51060f0SSean Hefty 		ret = cma_use_port(ps, id_priv);
3721e51060f0SSean Hefty 	mutex_unlock(&lock);
3722e51060f0SSean Hefty 
3723e51060f0SSean Hefty 	return ret;
3724e51060f0SSean Hefty }
3725e51060f0SSean Hefty 
3726d14714dfSSean Hefty static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3727d14714dfSSean Hefty 			       struct sockaddr *addr)
3728d14714dfSSean Hefty {
3729d90f9b35SRoland Dreier #if IS_ENABLED(CONFIG_IPV6)
3730d14714dfSSean Hefty 	struct sockaddr_in6 *sin6;
3731d14714dfSSean Hefty 
3732d14714dfSSean Hefty 	if (addr->sa_family != AF_INET6)
3733d14714dfSSean Hefty 		return 0;
3734d14714dfSSean Hefty 
3735d14714dfSSean Hefty 	sin6 = (struct sockaddr_in6 *) addr;
37365462edddSSomnath Kotur 
37375462edddSSomnath Kotur 	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
37385462edddSSomnath Kotur 		return 0;
37395462edddSSomnath Kotur 
37405462edddSSomnath Kotur 	if (!sin6->sin6_scope_id)
3741d14714dfSSean Hefty 			return -EINVAL;
3742d14714dfSSean Hefty 
3743d14714dfSSean Hefty 	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3744d14714dfSSean Hefty #endif
3745d14714dfSSean Hefty 	return 0;
3746d14714dfSSean Hefty }
3747d14714dfSSean Hefty 
3748a9bb7912SHefty, Sean int rdma_listen(struct rdma_cm_id *id, int backlog)
3749a9bb7912SHefty, Sean {
3750732d41c5SJason Gunthorpe 	struct rdma_id_private *id_priv =
3751732d41c5SJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
3752a9bb7912SHefty, Sean 	int ret;
3753a9bb7912SHefty, Sean 
3754732d41c5SJason Gunthorpe 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3755732d41c5SJason Gunthorpe 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3756f4753834SSean Hefty 		id->route.addr.src_addr.ss_family = AF_INET;
3757f4753834SSean Hefty 		ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3758a9bb7912SHefty, Sean 		if (ret)
3759a9bb7912SHefty, Sean 			return ret;
3760732d41c5SJason Gunthorpe 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3761732d41c5SJason Gunthorpe 					   RDMA_CM_LISTEN)))
3762a9bb7912SHefty, Sean 			return -EINVAL;
3763a9bb7912SHefty, Sean 	}
3764a9bb7912SHefty, Sean 
3765d490ee52SJason Gunthorpe 	/*
3766d490ee52SJason Gunthorpe 	 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3767d490ee52SJason Gunthorpe 	 * any more, and has to be unique in the bind list.
3768d490ee52SJason Gunthorpe 	 */
3769a9bb7912SHefty, Sean 	if (id_priv->reuseaddr) {
3770d490ee52SJason Gunthorpe 		mutex_lock(&lock);
3771d490ee52SJason Gunthorpe 		ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3772d490ee52SJason Gunthorpe 		if (!ret)
3773d490ee52SJason Gunthorpe 			id_priv->reuseaddr = 0;
3774d490ee52SJason Gunthorpe 		mutex_unlock(&lock);
3775a9bb7912SHefty, Sean 		if (ret)
3776a9bb7912SHefty, Sean 			goto err;
3777a9bb7912SHefty, Sean 	}
3778a9bb7912SHefty, Sean 
3779a9bb7912SHefty, Sean 	id_priv->backlog = backlog;
3780889d916bSShay Drory 	if (id_priv->cma_dev) {
378172219ceaSMichael Wang 		if (rdma_cap_ib_cm(id->device, 1)) {
3782a9bb7912SHefty, Sean 			ret = cma_ib_listen(id_priv);
3783a9bb7912SHefty, Sean 			if (ret)
3784a9bb7912SHefty, Sean 				goto err;
378504215330SMichael Wang 		} else if (rdma_cap_iw_cm(id->device, 1)) {
3786a9bb7912SHefty, Sean 			ret = cma_iw_listen(id_priv, backlog);
3787a9bb7912SHefty, Sean 			if (ret)
3788a9bb7912SHefty, Sean 				goto err;
378921655afcSMichael Wang 		} else {
3790a9bb7912SHefty, Sean 			ret = -ENOSYS;
3791a9bb7912SHefty, Sean 			goto err;
3792a9bb7912SHefty, Sean 		}
3793c80a0c52SLeon Romanovsky 	} else {
3794c80a0c52SLeon Romanovsky 		ret = cma_listen_on_all(id_priv);
3795c80a0c52SLeon Romanovsky 		if (ret)
3796c80a0c52SLeon Romanovsky 			goto err;
3797c80a0c52SLeon Romanovsky 	}
3798a9bb7912SHefty, Sean 
3799a9bb7912SHefty, Sean 	return 0;
3800a9bb7912SHefty, Sean err:
3801a9bb7912SHefty, Sean 	id_priv->backlog = 0;
3802d490ee52SJason Gunthorpe 	/*
3803d490ee52SJason Gunthorpe 	 * All the failure paths that lead here will not allow the req_handler's
3804d490ee52SJason Gunthorpe 	 * to have run.
3805d490ee52SJason Gunthorpe 	 */
3806550e5ca7SNir Muchtar 	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3807a9bb7912SHefty, Sean 	return ret;
3808a9bb7912SHefty, Sean }
3809a9bb7912SHefty, Sean EXPORT_SYMBOL(rdma_listen);
3810a9bb7912SHefty, Sean 
3811e51060f0SSean Hefty int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3812e51060f0SSean Hefty {
3813e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
3814e51060f0SSean Hefty 	int ret;
38156df6b4a9SMoni Shoua 	struct sockaddr  *daddr;
3816e51060f0SSean Hefty 
3817680f920aSSean Hefty 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3818680f920aSSean Hefty 	    addr->sa_family != AF_IB)
3819e51060f0SSean Hefty 		return -EAFNOSUPPORT;
3820e51060f0SSean Hefty 
3821e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
3822550e5ca7SNir Muchtar 	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3823e51060f0SSean Hefty 		return -EINVAL;
3824e51060f0SSean Hefty 
3825d14714dfSSean Hefty 	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3826d14714dfSSean Hefty 	if (ret)
3827d14714dfSSean Hefty 		goto err1;
3828d14714dfSSean Hefty 
38297b85627bSMoni Shoua 	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
38308523c048SSean Hefty 	if (!cma_any_addr(addr)) {
3831680f920aSSean Hefty 		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3832255d0c14SKrishna Kumar 		if (ret)
3833255d0c14SKrishna Kumar 			goto err1;
3834255d0c14SKrishna Kumar 
3835ff11c6cdSParav Pandit 		ret = cma_acquire_dev_by_src_ip(id_priv);
3836e51060f0SSean Hefty 		if (ret)
3837255d0c14SKrishna Kumar 			goto err1;
3838e51060f0SSean Hefty 	}
3839e51060f0SSean Hefty 
384068602120SSean Hefty 	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
38415b0ec991SSean Hefty 		if (addr->sa_family == AF_INET)
38425b0ec991SSean Hefty 			id_priv->afonly = 1;
38435b0ec991SSean Hefty #if IS_ENABLED(CONFIG_IPV6)
3844fa20105eSGuy Shapiro 		else if (addr->sa_family == AF_INET6) {
3845fa20105eSGuy Shapiro 			struct net *net = id_priv->id.route.addr.dev_addr.net;
3846fa20105eSGuy Shapiro 
3847fa20105eSGuy Shapiro 			id_priv->afonly = net->ipv6.sysctl.bindv6only;
3848fa20105eSGuy Shapiro 		}
38495b0ec991SSean Hefty #endif
385068602120SSean Hefty 	}
38519dea9a2fSTatyana Nikolova 	daddr = cma_dst_addr(id_priv);
38529dea9a2fSTatyana Nikolova 	daddr->sa_family = addr->sa_family;
38539dea9a2fSTatyana Nikolova 
3854e51060f0SSean Hefty 	ret = cma_get_port(id_priv);
3855e51060f0SSean Hefty 	if (ret)
3856255d0c14SKrishna Kumar 		goto err2;
3857e51060f0SSean Hefty 
3858cb5cd0eaSShay Drory 	if (!cma_any_addr(addr))
3859cb5cd0eaSShay Drory 		rdma_restrack_add(&id_priv->res);
3860e51060f0SSean Hefty 	return 0;
3861255d0c14SKrishna Kumar err2:
3862ed7a01fdSLeon Romanovsky 	if (id_priv->cma_dev)
3863a396d43aSSean Hefty 		cma_release_dev(id_priv);
3864255d0c14SKrishna Kumar err1:
3865550e5ca7SNir Muchtar 	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3866e51060f0SSean Hefty 	return ret;
3867e51060f0SSean Hefty }
3868e51060f0SSean Hefty EXPORT_SYMBOL(rdma_bind_addr);
3869e51060f0SSean Hefty 
3870f4753834SSean Hefty static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3871e51060f0SSean Hefty {
3872e51060f0SSean Hefty 	struct cma_hdr *cma_hdr;
3873e51060f0SSean Hefty 
387401602f11SSean Hefty 	cma_hdr = hdr;
387501602f11SSean Hefty 	cma_hdr->cma_version = CMA_VERSION;
3876f4753834SSean Hefty 	if (cma_family(id_priv) == AF_INET) {
38771f5175adSAleksey Senin 		struct sockaddr_in *src4, *dst4;
38781f5175adSAleksey Senin 
3879f4753834SSean Hefty 		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3880f4753834SSean Hefty 		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3881e51060f0SSean Hefty 
3882e51060f0SSean Hefty 		cma_set_ip_ver(cma_hdr, 4);
3883e51060f0SSean Hefty 		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3884e51060f0SSean Hefty 		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3885e51060f0SSean Hefty 		cma_hdr->port = src4->sin_port;
3886e8160e15SSean Hefty 	} else if (cma_family(id_priv) == AF_INET6) {
38871f5175adSAleksey Senin 		struct sockaddr_in6 *src6, *dst6;
38881f5175adSAleksey Senin 
3889f4753834SSean Hefty 		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3890f4753834SSean Hefty 		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
38911f5175adSAleksey Senin 
38921f5175adSAleksey Senin 		cma_set_ip_ver(cma_hdr, 6);
38931f5175adSAleksey Senin 		cma_hdr->src_addr.ip6 = src6->sin6_addr;
38941f5175adSAleksey Senin 		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
38951f5175adSAleksey Senin 		cma_hdr->port = src6->sin6_port;
38961f5175adSAleksey Senin 	}
3897e51060f0SSean Hefty 	return 0;
3898e51060f0SSean Hefty }
3899e51060f0SSean Hefty 
3900628e5f6dSSean Hefty static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3901e7ff98aeSParav Pandit 				const struct ib_cm_event *ib_event)
3902628e5f6dSSean Hefty {
3903628e5f6dSSean Hefty 	struct rdma_id_private *id_priv = cm_id->context;
39047582df82SParav Pandit 	struct rdma_cm_event event = {};
3905e7ff98aeSParav Pandit 	const struct ib_cm_sidr_rep_event_param *rep =
3906e7ff98aeSParav Pandit 				&ib_event->param.sidr_rep_rcvd;
3907f6a9d47aSJason Gunthorpe 	int ret;
3908628e5f6dSSean Hefty 
390937e07cdaSBart Van Assche 	mutex_lock(&id_priv->handler_mutex);
39102a7cec53SJason Gunthorpe 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
391137e07cdaSBart Van Assche 		goto out;
3912628e5f6dSSean Hefty 
3913628e5f6dSSean Hefty 	switch (ib_event->event) {
3914628e5f6dSSean Hefty 	case IB_CM_SIDR_REQ_ERROR:
3915628e5f6dSSean Hefty 		event.event = RDMA_CM_EVENT_UNREACHABLE;
3916628e5f6dSSean Hefty 		event.status = -ETIMEDOUT;
3917628e5f6dSSean Hefty 		break;
3918628e5f6dSSean Hefty 	case IB_CM_SIDR_REP_RECEIVED:
3919628e5f6dSSean Hefty 		event.param.ud.private_data = ib_event->private_data;
3920628e5f6dSSean Hefty 		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3921628e5f6dSSean Hefty 		if (rep->status != IB_SIDR_SUCCESS) {
3922628e5f6dSSean Hefty 			event.event = RDMA_CM_EVENT_UNREACHABLE;
3923628e5f6dSSean Hefty 			event.status = ib_event->param.sidr_rep_rcvd.status;
3924498683c6SMoni Shoua 			pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3925498683c6SMoni Shoua 					     event.status);
3926628e5f6dSSean Hefty 			break;
3927628e5f6dSSean Hefty 		}
39285c438135SSean Hefty 		ret = cma_set_qkey(id_priv, rep->qkey);
3929d2ca39f2SYossi Etigin 		if (ret) {
3930498683c6SMoni Shoua 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3931d2ca39f2SYossi Etigin 			event.event = RDMA_CM_EVENT_ADDR_ERROR;
39325c438135SSean Hefty 			event.status = ret;
3933628e5f6dSSean Hefty 			break;
3934628e5f6dSSean Hefty 		}
39354ad6a024SParav Pandit 		ib_init_ah_attr_from_path(id_priv->id.device,
39364ad6a024SParav Pandit 					  id_priv->id.port_num,
3937628e5f6dSSean Hefty 					  id_priv->id.route.path_rec,
393839839107SParav Pandit 					  &event.param.ud.ah_attr,
393939839107SParav Pandit 					  rep->sgid_attr);
3940628e5f6dSSean Hefty 		event.param.ud.qp_num = rep->qpn;
3941628e5f6dSSean Hefty 		event.param.ud.qkey = rep->qkey;
3942628e5f6dSSean Hefty 		event.event = RDMA_CM_EVENT_ESTABLISHED;
3943628e5f6dSSean Hefty 		event.status = 0;
3944628e5f6dSSean Hefty 		break;
3945628e5f6dSSean Hefty 	default:
3946aba25a3eSParav Pandit 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3947628e5f6dSSean Hefty 		       ib_event->event);
3948628e5f6dSSean Hefty 		goto out;
3949628e5f6dSSean Hefty 	}
3950628e5f6dSSean Hefty 
3951ed999f82SChuck Lever 	ret = cma_cm_event_handler(id_priv, &event);
3952aa74f487SParav Pandit 
3953aa74f487SParav Pandit 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
3954628e5f6dSSean Hefty 	if (ret) {
3955628e5f6dSSean Hefty 		/* Destroy the CM ID by returning a non-zero value. */
3956628e5f6dSSean Hefty 		id_priv->cm_id.ib = NULL;
3957f6a9d47aSJason Gunthorpe 		destroy_id_handler_unlock(id_priv);
3958628e5f6dSSean Hefty 		return ret;
3959628e5f6dSSean Hefty 	}
3960628e5f6dSSean Hefty out:
3961de910bd9SOr Gerlitz 	mutex_unlock(&id_priv->handler_mutex);
3962f6a9d47aSJason Gunthorpe 	return 0;
3963628e5f6dSSean Hefty }
3964628e5f6dSSean Hefty 
3965628e5f6dSSean Hefty static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3966628e5f6dSSean Hefty 			      struct rdma_conn_param *conn_param)
3967628e5f6dSSean Hefty {
3968628e5f6dSSean Hefty 	struct ib_cm_sidr_req_param req;
39690c9361fcSJack Morgenstein 	struct ib_cm_id	*id;
3970e511d1aeSSean Hefty 	void *private_data;
3971c0b64f58SBart Van Assche 	u8 offset;
3972c0b64f58SBart Van Assche 	int ret;
3973628e5f6dSSean Hefty 
3974e511d1aeSSean Hefty 	memset(&req, 0, sizeof req);
3975e8160e15SSean Hefty 	offset = cma_user_data_offset(id_priv);
3976e8160e15SSean Hefty 	req.private_data_len = offset + conn_param->private_data_len;
397704ded167SSean Hefty 	if (req.private_data_len < conn_param->private_data_len)
397804ded167SSean Hefty 		return -EINVAL;
397904ded167SSean Hefty 
3980e8160e15SSean Hefty 	if (req.private_data_len) {
3981e511d1aeSSean Hefty 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3982e511d1aeSSean Hefty 		if (!private_data)
3983628e5f6dSSean Hefty 			return -ENOMEM;
3984e8160e15SSean Hefty 	} else {
3985e511d1aeSSean Hefty 		private_data = NULL;
3986e8160e15SSean Hefty 	}
3987628e5f6dSSean Hefty 
3988628e5f6dSSean Hefty 	if (conn_param->private_data && conn_param->private_data_len)
3989e511d1aeSSean Hefty 		memcpy(private_data + offset, conn_param->private_data,
3990e511d1aeSSean Hefty 		       conn_param->private_data_len);
3991628e5f6dSSean Hefty 
3992e511d1aeSSean Hefty 	if (private_data) {
3993e511d1aeSSean Hefty 		ret = cma_format_hdr(private_data, id_priv);
3994628e5f6dSSean Hefty 		if (ret)
3995628e5f6dSSean Hefty 			goto out;
3996e511d1aeSSean Hefty 		req.private_data = private_data;
3997e8160e15SSean Hefty 	}
3998628e5f6dSSean Hefty 
39990c9361fcSJack Morgenstein 	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
40000c9361fcSJack Morgenstein 			     id_priv);
40010c9361fcSJack Morgenstein 	if (IS_ERR(id)) {
40020c9361fcSJack Morgenstein 		ret = PTR_ERR(id);
4003628e5f6dSSean Hefty 		goto out;
4004628e5f6dSSean Hefty 	}
40050c9361fcSJack Morgenstein 	id_priv->cm_id.ib = id;
4006628e5f6dSSean Hefty 
4007f4753834SSean Hefty 	req.path = id_priv->id.route.path_rec;
4008815d456eSParav Pandit 	req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4009cf53936fSSean Hefty 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4010628e5f6dSSean Hefty 	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4011628e5f6dSSean Hefty 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4012628e5f6dSSean Hefty 
4013ed999f82SChuck Lever 	trace_cm_send_sidr_req(id_priv);
4014628e5f6dSSean Hefty 	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4015628e5f6dSSean Hefty 	if (ret) {
4016628e5f6dSSean Hefty 		ib_destroy_cm_id(id_priv->cm_id.ib);
4017628e5f6dSSean Hefty 		id_priv->cm_id.ib = NULL;
4018628e5f6dSSean Hefty 	}
4019628e5f6dSSean Hefty out:
4020e511d1aeSSean Hefty 	kfree(private_data);
4021628e5f6dSSean Hefty 	return ret;
4022628e5f6dSSean Hefty }
4023628e5f6dSSean Hefty 
4024e51060f0SSean Hefty static int cma_connect_ib(struct rdma_id_private *id_priv,
4025e51060f0SSean Hefty 			  struct rdma_conn_param *conn_param)
4026e51060f0SSean Hefty {
4027e51060f0SSean Hefty 	struct ib_cm_req_param req;
4028e51060f0SSean Hefty 	struct rdma_route *route;
4029e51060f0SSean Hefty 	void *private_data;
40300c9361fcSJack Morgenstein 	struct ib_cm_id	*id;
4031c0b64f58SBart Van Assche 	u8 offset;
4032c0b64f58SBart Van Assche 	int ret;
4033e51060f0SSean Hefty 
4034e51060f0SSean Hefty 	memset(&req, 0, sizeof req);
4035e8160e15SSean Hefty 	offset = cma_user_data_offset(id_priv);
4036e51060f0SSean Hefty 	req.private_data_len = offset + conn_param->private_data_len;
403704ded167SSean Hefty 	if (req.private_data_len < conn_param->private_data_len)
403804ded167SSean Hefty 		return -EINVAL;
403904ded167SSean Hefty 
4040e8160e15SSean Hefty 	if (req.private_data_len) {
4041e51060f0SSean Hefty 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4042e51060f0SSean Hefty 		if (!private_data)
4043e51060f0SSean Hefty 			return -ENOMEM;
4044e8160e15SSean Hefty 	} else {
4045e8160e15SSean Hefty 		private_data = NULL;
4046e8160e15SSean Hefty 	}
4047e51060f0SSean Hefty 
4048e51060f0SSean Hefty 	if (conn_param->private_data && conn_param->private_data_len)
4049e51060f0SSean Hefty 		memcpy(private_data + offset, conn_param->private_data,
4050e51060f0SSean Hefty 		       conn_param->private_data_len);
4051e51060f0SSean Hefty 
40520c9361fcSJack Morgenstein 	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
40530c9361fcSJack Morgenstein 	if (IS_ERR(id)) {
40540c9361fcSJack Morgenstein 		ret = PTR_ERR(id);
4055e51060f0SSean Hefty 		goto out;
4056e51060f0SSean Hefty 	}
40570c9361fcSJack Morgenstein 	id_priv->cm_id.ib = id;
4058e51060f0SSean Hefty 
4059e51060f0SSean Hefty 	route = &id_priv->id.route;
4060e8160e15SSean Hefty 	if (private_data) {
4061f4753834SSean Hefty 		ret = cma_format_hdr(private_data, id_priv);
4062e51060f0SSean Hefty 		if (ret)
4063e51060f0SSean Hefty 			goto out;
4064e51060f0SSean Hefty 		req.private_data = private_data;
4065e8160e15SSean Hefty 	}
4066e51060f0SSean Hefty 
4067e51060f0SSean Hefty 	req.primary_path = &route->path_rec[0];
4068e51060f0SSean Hefty 	if (route->num_paths == 2)
4069e51060f0SSean Hefty 		req.alternate_path = &route->path_rec[1];
4070e51060f0SSean Hefty 
4071815d456eSParav Pandit 	req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4072815d456eSParav Pandit 	/* Alternate path SGID attribute currently unsupported */
4073cf53936fSSean Hefty 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4074e51060f0SSean Hefty 	req.qp_num = id_priv->qp_num;
407518c441a6SSean Hefty 	req.qp_type = id_priv->id.qp_type;
4076e51060f0SSean Hefty 	req.starting_psn = id_priv->seq_num;
4077e51060f0SSean Hefty 	req.responder_resources = conn_param->responder_resources;
4078e51060f0SSean Hefty 	req.initiator_depth = conn_param->initiator_depth;
4079e51060f0SSean Hefty 	req.flow_control = conn_param->flow_control;
40804ede178aSSean Hefty 	req.retry_count = min_t(u8, 7, conn_param->retry_count);
40814ede178aSSean Hefty 	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4082e51060f0SSean Hefty 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4083e51060f0SSean Hefty 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4084e51060f0SSean Hefty 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4085e51060f0SSean Hefty 	req.srq = id_priv->srq ? 1 : 0;
4086a20652e1SLeon Romanovsky 	req.ece.vendor_id = id_priv->ece.vendor_id;
4087a20652e1SLeon Romanovsky 	req.ece.attr_mod = id_priv->ece.attr_mod;
4088e51060f0SSean Hefty 
4089ed999f82SChuck Lever 	trace_cm_send_req(id_priv);
4090e51060f0SSean Hefty 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4091e51060f0SSean Hefty out:
40920c9361fcSJack Morgenstein 	if (ret && !IS_ERR(id)) {
40930c9361fcSJack Morgenstein 		ib_destroy_cm_id(id);
4094675a027cSKrishna Kumar 		id_priv->cm_id.ib = NULL;
4095675a027cSKrishna Kumar 	}
4096675a027cSKrishna Kumar 
4097e51060f0SSean Hefty 	kfree(private_data);
4098e51060f0SSean Hefty 	return ret;
4099e51060f0SSean Hefty }
4100e51060f0SSean Hefty 
410107ebafbaSTom Tucker static int cma_connect_iw(struct rdma_id_private *id_priv,
410207ebafbaSTom Tucker 			  struct rdma_conn_param *conn_param)
410307ebafbaSTom Tucker {
410407ebafbaSTom Tucker 	struct iw_cm_id *cm_id;
410507ebafbaSTom Tucker 	int ret;
410607ebafbaSTom Tucker 	struct iw_cm_conn_param iw_param;
410707ebafbaSTom Tucker 
410807ebafbaSTom Tucker 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
41090c9361fcSJack Morgenstein 	if (IS_ERR(cm_id))
41100c9361fcSJack Morgenstein 		return PTR_ERR(cm_id);
411107ebafbaSTom Tucker 
4112ca0c448dSHåkon Bugge 	mutex_lock(&id_priv->qp_mutex);
411368cdba06SSteve Wise 	cm_id->tos = id_priv->tos;
4114926ba19bSSteve Wise 	cm_id->tos_set = id_priv->tos_set;
4115ca0c448dSHåkon Bugge 	mutex_unlock(&id_priv->qp_mutex);
4116ca0c448dSHåkon Bugge 
411707ebafbaSTom Tucker 	id_priv->cm_id.iw = cm_id;
411807ebafbaSTom Tucker 
411924d44a39SSteve Wise 	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
412024d44a39SSteve Wise 	       rdma_addr_size(cma_src_addr(id_priv)));
412124d44a39SSteve Wise 	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
412224d44a39SSteve Wise 	       rdma_addr_size(cma_dst_addr(id_priv)));
412307ebafbaSTom Tucker 
41245851bb89SSean Hefty 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4125675a027cSKrishna Kumar 	if (ret)
4126675a027cSKrishna Kumar 		goto out;
412707ebafbaSTom Tucker 
4128f45ee80eSHefty, Sean 	if (conn_param) {
412907ebafbaSTom Tucker 		iw_param.ord = conn_param->initiator_depth;
413007ebafbaSTom Tucker 		iw_param.ird = conn_param->responder_resources;
413107ebafbaSTom Tucker 		iw_param.private_data = conn_param->private_data;
413207ebafbaSTom Tucker 		iw_param.private_data_len = conn_param->private_data_len;
4133f45ee80eSHefty, Sean 		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4134f45ee80eSHefty, Sean 	} else {
4135f45ee80eSHefty, Sean 		memset(&iw_param, 0, sizeof iw_param);
413607ebafbaSTom Tucker 		iw_param.qpn = id_priv->qp_num;
4137f45ee80eSHefty, Sean 	}
413807ebafbaSTom Tucker 	ret = iw_cm_connect(cm_id, &iw_param);
413907ebafbaSTom Tucker out:
41400c9361fcSJack Morgenstein 	if (ret) {
4141675a027cSKrishna Kumar 		iw_destroy_cm_id(cm_id);
4142675a027cSKrishna Kumar 		id_priv->cm_id.iw = NULL;
4143675a027cSKrishna Kumar 	}
414407ebafbaSTom Tucker 	return ret;
414507ebafbaSTom Tucker }
414607ebafbaSTom Tucker 
4147071ba4ccSJason Gunthorpe /**
4148071ba4ccSJason Gunthorpe  * rdma_connect_locked - Initiate an active connection request.
4149071ba4ccSJason Gunthorpe  * @id: Connection identifier to connect.
4150071ba4ccSJason Gunthorpe  * @conn_param: Connection information used for connected QPs.
4151071ba4ccSJason Gunthorpe  *
4152071ba4ccSJason Gunthorpe  * Same as rdma_connect() but can only be called from the
4153071ba4ccSJason Gunthorpe  * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4154071ba4ccSJason Gunthorpe  */
4155071ba4ccSJason Gunthorpe int rdma_connect_locked(struct rdma_cm_id *id,
4156071ba4ccSJason Gunthorpe 			struct rdma_conn_param *conn_param)
4157e51060f0SSean Hefty {
41582a7cec53SJason Gunthorpe 	struct rdma_id_private *id_priv =
41592a7cec53SJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
4160e51060f0SSean Hefty 	int ret;
4161e51060f0SSean Hefty 
4162071ba4ccSJason Gunthorpe 	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4163071ba4ccSJason Gunthorpe 		return -EINVAL;
4164e51060f0SSean Hefty 
4165e51060f0SSean Hefty 	if (!id->qp) {
4166e51060f0SSean Hefty 		id_priv->qp_num = conn_param->qp_num;
4167e51060f0SSean Hefty 		id_priv->srq = conn_param->srq;
4168e51060f0SSean Hefty 	}
4169e51060f0SSean Hefty 
417072219ceaSMichael Wang 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4171b26f9b99SSean Hefty 		if (id->qp_type == IB_QPT_UD)
4172628e5f6dSSean Hefty 			ret = cma_resolve_ib_udp(id_priv, conn_param);
4173628e5f6dSSean Hefty 		else
4174e51060f0SSean Hefty 			ret = cma_connect_ib(id_priv, conn_param);
4175b6eb7011SWenpeng Liang 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
417607ebafbaSTom Tucker 		ret = cma_connect_iw(id_priv, conn_param);
4177b6eb7011SWenpeng Liang 	} else {
4178e51060f0SSean Hefty 		ret = -ENOSYS;
4179b6eb7011SWenpeng Liang 	}
4180e51060f0SSean Hefty 	if (ret)
41812a7cec53SJason Gunthorpe 		goto err_state;
4182e51060f0SSean Hefty 	return 0;
41832a7cec53SJason Gunthorpe err_state:
4184550e5ca7SNir Muchtar 	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4185071ba4ccSJason Gunthorpe 	return ret;
4186071ba4ccSJason Gunthorpe }
4187071ba4ccSJason Gunthorpe EXPORT_SYMBOL(rdma_connect_locked);
4188071ba4ccSJason Gunthorpe 
4189071ba4ccSJason Gunthorpe /**
4190071ba4ccSJason Gunthorpe  * rdma_connect - Initiate an active connection request.
4191071ba4ccSJason Gunthorpe  * @id: Connection identifier to connect.
4192071ba4ccSJason Gunthorpe  * @conn_param: Connection information used for connected QPs.
4193071ba4ccSJason Gunthorpe  *
4194071ba4ccSJason Gunthorpe  * Users must have resolved a route for the rdma_cm_id to connect with by having
4195071ba4ccSJason Gunthorpe  * called rdma_resolve_route before calling this routine.
4196071ba4ccSJason Gunthorpe  *
4197071ba4ccSJason Gunthorpe  * This call will either connect to a remote QP or obtain remote QP information
4198071ba4ccSJason Gunthorpe  * for unconnected rdma_cm_id's.  The actual operation is based on the
4199071ba4ccSJason Gunthorpe  * rdma_cm_id's port space.
4200071ba4ccSJason Gunthorpe  */
4201071ba4ccSJason Gunthorpe int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4202071ba4ccSJason Gunthorpe {
4203071ba4ccSJason Gunthorpe 	struct rdma_id_private *id_priv =
4204071ba4ccSJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
4205071ba4ccSJason Gunthorpe 	int ret;
4206071ba4ccSJason Gunthorpe 
4207071ba4ccSJason Gunthorpe 	mutex_lock(&id_priv->handler_mutex);
4208071ba4ccSJason Gunthorpe 	ret = rdma_connect_locked(id, conn_param);
42092a7cec53SJason Gunthorpe 	mutex_unlock(&id_priv->handler_mutex);
4210e51060f0SSean Hefty 	return ret;
4211e51060f0SSean Hefty }
4212e51060f0SSean Hefty EXPORT_SYMBOL(rdma_connect);
4213e51060f0SSean Hefty 
421434e2ab57SLeon Romanovsky /**
421534e2ab57SLeon Romanovsky  * rdma_connect_ece - Initiate an active connection request with ECE data.
421634e2ab57SLeon Romanovsky  * @id: Connection identifier to connect.
421734e2ab57SLeon Romanovsky  * @conn_param: Connection information used for connected QPs.
421834e2ab57SLeon Romanovsky  * @ece: ECE parameters
421934e2ab57SLeon Romanovsky  *
422034e2ab57SLeon Romanovsky  * See rdma_connect() explanation.
422134e2ab57SLeon Romanovsky  */
422234e2ab57SLeon Romanovsky int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
422334e2ab57SLeon Romanovsky 		     struct rdma_ucm_ece *ece)
422434e2ab57SLeon Romanovsky {
422534e2ab57SLeon Romanovsky 	struct rdma_id_private *id_priv =
422634e2ab57SLeon Romanovsky 		container_of(id, struct rdma_id_private, id);
422734e2ab57SLeon Romanovsky 
422834e2ab57SLeon Romanovsky 	id_priv->ece.vendor_id = ece->vendor_id;
422934e2ab57SLeon Romanovsky 	id_priv->ece.attr_mod = ece->attr_mod;
423034e2ab57SLeon Romanovsky 
423134e2ab57SLeon Romanovsky 	return rdma_connect(id, conn_param);
423234e2ab57SLeon Romanovsky }
423334e2ab57SLeon Romanovsky EXPORT_SYMBOL(rdma_connect_ece);
423434e2ab57SLeon Romanovsky 
4235e51060f0SSean Hefty static int cma_accept_ib(struct rdma_id_private *id_priv,
4236e51060f0SSean Hefty 			 struct rdma_conn_param *conn_param)
4237e51060f0SSean Hefty {
4238e51060f0SSean Hefty 	struct ib_cm_rep_param rep;
42395851bb89SSean Hefty 	int ret;
4240e51060f0SSean Hefty 
42415851bb89SSean Hefty 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4242e51060f0SSean Hefty 	if (ret)
42430fe313b0SSean Hefty 		goto out;
42440fe313b0SSean Hefty 
42455851bb89SSean Hefty 	ret = cma_modify_qp_rts(id_priv, conn_param);
42460fe313b0SSean Hefty 	if (ret)
42470fe313b0SSean Hefty 		goto out;
42480fe313b0SSean Hefty 
4249e51060f0SSean Hefty 	memset(&rep, 0, sizeof rep);
4250e51060f0SSean Hefty 	rep.qp_num = id_priv->qp_num;
4251e51060f0SSean Hefty 	rep.starting_psn = id_priv->seq_num;
4252e51060f0SSean Hefty 	rep.private_data = conn_param->private_data;
4253e51060f0SSean Hefty 	rep.private_data_len = conn_param->private_data_len;
4254e51060f0SSean Hefty 	rep.responder_resources = conn_param->responder_resources;
4255e51060f0SSean Hefty 	rep.initiator_depth = conn_param->initiator_depth;
4256e51060f0SSean Hefty 	rep.failover_accepted = 0;
4257e51060f0SSean Hefty 	rep.flow_control = conn_param->flow_control;
42584ede178aSSean Hefty 	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4259e51060f0SSean Hefty 	rep.srq = id_priv->srq ? 1 : 0;
42600cb15372SLeon Romanovsky 	rep.ece.vendor_id = id_priv->ece.vendor_id;
42610cb15372SLeon Romanovsky 	rep.ece.attr_mod = id_priv->ece.attr_mod;
4262e51060f0SSean Hefty 
4263ed999f82SChuck Lever 	trace_cm_send_rep(id_priv);
42640fe313b0SSean Hefty 	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
42650fe313b0SSean Hefty out:
42660fe313b0SSean Hefty 	return ret;
4267e51060f0SSean Hefty }
4268e51060f0SSean Hefty 
426907ebafbaSTom Tucker static int cma_accept_iw(struct rdma_id_private *id_priv,
427007ebafbaSTom Tucker 		  struct rdma_conn_param *conn_param)
427107ebafbaSTom Tucker {
427207ebafbaSTom Tucker 	struct iw_cm_conn_param iw_param;
427307ebafbaSTom Tucker 	int ret;
427407ebafbaSTom Tucker 
4275f2625f7dSSteve Wise 	if (!conn_param)
4276f2625f7dSSteve Wise 		return -EINVAL;
4277f2625f7dSSteve Wise 
42785851bb89SSean Hefty 	ret = cma_modify_qp_rtr(id_priv, conn_param);
427907ebafbaSTom Tucker 	if (ret)
428007ebafbaSTom Tucker 		return ret;
428107ebafbaSTom Tucker 
428207ebafbaSTom Tucker 	iw_param.ord = conn_param->initiator_depth;
428307ebafbaSTom Tucker 	iw_param.ird = conn_param->responder_resources;
428407ebafbaSTom Tucker 	iw_param.private_data = conn_param->private_data;
428507ebafbaSTom Tucker 	iw_param.private_data_len = conn_param->private_data_len;
4286b6eb7011SWenpeng Liang 	if (id_priv->id.qp)
428707ebafbaSTom Tucker 		iw_param.qpn = id_priv->qp_num;
4288b6eb7011SWenpeng Liang 	else
428907ebafbaSTom Tucker 		iw_param.qpn = conn_param->qp_num;
429007ebafbaSTom Tucker 
429107ebafbaSTom Tucker 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
429207ebafbaSTom Tucker }
429307ebafbaSTom Tucker 
4294628e5f6dSSean Hefty static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
42955c438135SSean Hefty 			     enum ib_cm_sidr_status status, u32 qkey,
4296628e5f6dSSean Hefty 			     const void *private_data, int private_data_len)
4297628e5f6dSSean Hefty {
4298628e5f6dSSean Hefty 	struct ib_cm_sidr_rep_param rep;
4299d2ca39f2SYossi Etigin 	int ret;
4300628e5f6dSSean Hefty 
4301628e5f6dSSean Hefty 	memset(&rep, 0, sizeof rep);
4302628e5f6dSSean Hefty 	rep.status = status;
4303628e5f6dSSean Hefty 	if (status == IB_SIDR_SUCCESS) {
43045c438135SSean Hefty 		ret = cma_set_qkey(id_priv, qkey);
4305d2ca39f2SYossi Etigin 		if (ret)
4306d2ca39f2SYossi Etigin 			return ret;
4307628e5f6dSSean Hefty 		rep.qp_num = id_priv->qp_num;
4308c8f6a362SSean Hefty 		rep.qkey = id_priv->qkey;
43090cb15372SLeon Romanovsky 
43100cb15372SLeon Romanovsky 		rep.ece.vendor_id = id_priv->ece.vendor_id;
43110cb15372SLeon Romanovsky 		rep.ece.attr_mod = id_priv->ece.attr_mod;
4312628e5f6dSSean Hefty 	}
43130cb15372SLeon Romanovsky 
4314628e5f6dSSean Hefty 	rep.private_data = private_data;
4315628e5f6dSSean Hefty 	rep.private_data_len = private_data_len;
4316628e5f6dSSean Hefty 
4317ed999f82SChuck Lever 	trace_cm_send_sidr_rep(id_priv);
4318628e5f6dSSean Hefty 	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4319628e5f6dSSean Hefty }
4320628e5f6dSSean Hefty 
4321b09c4d70SLeon Romanovsky /**
4322b09c4d70SLeon Romanovsky  * rdma_accept - Called to accept a connection request or response.
4323b09c4d70SLeon Romanovsky  * @id: Connection identifier associated with the request.
4324b09c4d70SLeon Romanovsky  * @conn_param: Information needed to establish the connection.  This must be
4325b09c4d70SLeon Romanovsky  *   provided if accepting a connection request.  If accepting a connection
4326b09c4d70SLeon Romanovsky  *   response, this parameter must be NULL.
4327b09c4d70SLeon Romanovsky  *
4328b09c4d70SLeon Romanovsky  * Typically, this routine is only called by the listener to accept a connection
4329b09c4d70SLeon Romanovsky  * request.  It must also be called on the active side of a connection if the
4330b09c4d70SLeon Romanovsky  * user is performing their own QP transitions.
4331b09c4d70SLeon Romanovsky  *
4332b09c4d70SLeon Romanovsky  * In the case of error, a reject message is sent to the remote side and the
4333b09c4d70SLeon Romanovsky  * state of the qp associated with the id is modified to error, such that any
4334b09c4d70SLeon Romanovsky  * previously posted receive buffers would be flushed.
4335b09c4d70SLeon Romanovsky  *
4336b09c4d70SLeon Romanovsky  * This function is for use by kernel ULPs and must be called from under the
4337b09c4d70SLeon Romanovsky  * handler callback.
4338b09c4d70SLeon Romanovsky  */
4339b09c4d70SLeon Romanovsky int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4340e51060f0SSean Hefty {
4341d114c6feSJason Gunthorpe 	struct rdma_id_private *id_priv =
4342d114c6feSJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
4343e51060f0SSean Hefty 	int ret;
4344e51060f0SSean Hefty 
4345d114c6feSJason Gunthorpe 	lockdep_assert_held(&id_priv->handler_mutex);
434683e9502dSNir Muchtar 
4347d114c6feSJason Gunthorpe 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4348e51060f0SSean Hefty 		return -EINVAL;
4349e51060f0SSean Hefty 
4350e51060f0SSean Hefty 	if (!id->qp && conn_param) {
4351e51060f0SSean Hefty 		id_priv->qp_num = conn_param->qp_num;
4352e51060f0SSean Hefty 		id_priv->srq = conn_param->srq;
4353e51060f0SSean Hefty 	}
4354e51060f0SSean Hefty 
435572219ceaSMichael Wang 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4356f45ee80eSHefty, Sean 		if (id->qp_type == IB_QPT_UD) {
4357f45ee80eSHefty, Sean 			if (conn_param)
4358628e5f6dSSean Hefty 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
43595c438135SSean Hefty 							conn_param->qkey,
4360628e5f6dSSean Hefty 							conn_param->private_data,
4361628e5f6dSSean Hefty 							conn_param->private_data_len);
4362f45ee80eSHefty, Sean 			else
4363f45ee80eSHefty, Sean 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
43645c438135SSean Hefty 							0, NULL, 0);
4365f45ee80eSHefty, Sean 		} else {
4366f45ee80eSHefty, Sean 			if (conn_param)
4367e51060f0SSean Hefty 				ret = cma_accept_ib(id_priv, conn_param);
4368e51060f0SSean Hefty 			else
4369e51060f0SSean Hefty 				ret = cma_rep_recv(id_priv);
4370f45ee80eSHefty, Sean 		}
4371b6eb7011SWenpeng Liang 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
437207ebafbaSTom Tucker 		ret = cma_accept_iw(id_priv, conn_param);
4373b6eb7011SWenpeng Liang 	} else {
4374e51060f0SSean Hefty 		ret = -ENOSYS;
4375b6eb7011SWenpeng Liang 	}
4376e51060f0SSean Hefty 	if (ret)
4377e51060f0SSean Hefty 		goto reject;
4378e51060f0SSean Hefty 
4379e51060f0SSean Hefty 	return 0;
4380e51060f0SSean Hefty reject:
4381c5483388SSean Hefty 	cma_modify_qp_err(id_priv);
43828094ba0aSLeon Romanovsky 	rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4383e51060f0SSean Hefty 	return ret;
4384e51060f0SSean Hefty }
4385b09c4d70SLeon Romanovsky EXPORT_SYMBOL(rdma_accept);
4386e51060f0SSean Hefty 
4387b09c4d70SLeon Romanovsky int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4388b09c4d70SLeon Romanovsky 		    struct rdma_ucm_ece *ece)
43890cb15372SLeon Romanovsky {
43900cb15372SLeon Romanovsky 	struct rdma_id_private *id_priv =
43910cb15372SLeon Romanovsky 		container_of(id, struct rdma_id_private, id);
43920cb15372SLeon Romanovsky 
43930cb15372SLeon Romanovsky 	id_priv->ece.vendor_id = ece->vendor_id;
43940cb15372SLeon Romanovsky 	id_priv->ece.attr_mod = ece->attr_mod;
43950cb15372SLeon Romanovsky 
4396b09c4d70SLeon Romanovsky 	return rdma_accept(id, conn_param);
43970cb15372SLeon Romanovsky }
4398b09c4d70SLeon Romanovsky EXPORT_SYMBOL(rdma_accept_ece);
43990cb15372SLeon Romanovsky 
4400d114c6feSJason Gunthorpe void rdma_lock_handler(struct rdma_cm_id *id)
4401d114c6feSJason Gunthorpe {
4402d114c6feSJason Gunthorpe 	struct rdma_id_private *id_priv =
4403d114c6feSJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
4404d114c6feSJason Gunthorpe 
4405d114c6feSJason Gunthorpe 	mutex_lock(&id_priv->handler_mutex);
4406d114c6feSJason Gunthorpe }
4407d114c6feSJason Gunthorpe EXPORT_SYMBOL(rdma_lock_handler);
4408d114c6feSJason Gunthorpe 
4409d114c6feSJason Gunthorpe void rdma_unlock_handler(struct rdma_cm_id *id)
4410d114c6feSJason Gunthorpe {
4411d114c6feSJason Gunthorpe 	struct rdma_id_private *id_priv =
4412d114c6feSJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
4413d114c6feSJason Gunthorpe 
4414d114c6feSJason Gunthorpe 	mutex_unlock(&id_priv->handler_mutex);
4415d114c6feSJason Gunthorpe }
4416d114c6feSJason Gunthorpe EXPORT_SYMBOL(rdma_unlock_handler);
44170fe313b0SSean Hefty 
44180fe313b0SSean Hefty int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
44190fe313b0SSean Hefty {
4420e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
4421e51060f0SSean Hefty 	int ret;
4422e51060f0SSean Hefty 
4423e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
4424e51060f0SSean Hefty 	if (!id_priv->cm_id.ib)
4425e51060f0SSean Hefty 		return -EINVAL;
4426e51060f0SSean Hefty 
44270c9361fcSJack Morgenstein 	switch (id->device->node_type) {
4428e51060f0SSean Hefty 	case RDMA_NODE_IB_CA:
4429e51060f0SSean Hefty 		ret = ib_cm_notify(id_priv->cm_id.ib, event);
443072219ceaSMichael Wang 		break;
4431ed999f82SChuck Lever 	default:
44325c438135SSean Hefty 		ret = 0;
4433e51060f0SSean Hefty 		break;
4434ed999f82SChuck Lever 	}
4435ed999f82SChuck Lever 	return ret;
4436628e5f6dSSean Hefty }
4437628e5f6dSSean Hefty EXPORT_SYMBOL(rdma_notify);
4438628e5f6dSSean Hefty 
4439ed999f82SChuck Lever int rdma_reject(struct rdma_cm_id *id, const void *private_data,
44408094ba0aSLeon Romanovsky 		u8 private_data_len, u8 reason)
4441e51060f0SSean Hefty {
4442e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
444307ebafbaSTom Tucker 	int ret;
444407ebafbaSTom Tucker 
444507ebafbaSTom Tucker 	id_priv = container_of(id, struct rdma_id_private, id);
444607ebafbaSTom Tucker 	if (!id_priv->cm_id.ib)
4447e51060f0SSean Hefty 		return -EINVAL;
4448e51060f0SSean Hefty 
4449e51060f0SSean Hefty 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4450e51060f0SSean Hefty 		if (id->qp_type == IB_QPT_UD) {
4451e51060f0SSean Hefty 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4452e51060f0SSean Hefty 						private_data, private_data_len);
4453e51060f0SSean Hefty 		} else {
4454e51060f0SSean Hefty 			trace_cm_send_rej(id_priv);
44558094ba0aSLeon Romanovsky 			ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
44568094ba0aSLeon Romanovsky 					     private_data, private_data_len);
4457e51060f0SSean Hefty 		}
445804215330SMichael Wang 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4459e51060f0SSean Hefty 		ret = iw_cm_reject(id_priv->cm_id.iw,
4460e51060f0SSean Hefty 				   private_data, private_data_len);
4461b6eb7011SWenpeng Liang 	} else {
4462e51060f0SSean Hefty 		ret = -ENOSYS;
4463b6eb7011SWenpeng Liang 	}
446421655afcSMichael Wang 
4465e51060f0SSean Hefty 	return ret;
4466e51060f0SSean Hefty }
4467e51060f0SSean Hefty EXPORT_SYMBOL(rdma_reject);
4468e51060f0SSean Hefty 
4469e51060f0SSean Hefty int rdma_disconnect(struct rdma_cm_id *id)
4470e51060f0SSean Hefty {
4471e51060f0SSean Hefty 	struct rdma_id_private *id_priv;
4472e51060f0SSean Hefty 	int ret;
4473e51060f0SSean Hefty 
4474e51060f0SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
44750c9361fcSJack Morgenstein 	if (!id_priv->cm_id.ib)
4476e51060f0SSean Hefty 		return -EINVAL;
4477e51060f0SSean Hefty 
447872219ceaSMichael Wang 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4479c5483388SSean Hefty 		ret = cma_modify_qp_err(id_priv);
4480e51060f0SSean Hefty 		if (ret)
4481e51060f0SSean Hefty 			goto out;
4482e51060f0SSean Hefty 		/* Initiate or respond to a disconnect. */
4483ed999f82SChuck Lever 		trace_cm_disconnect(id_priv);
4484ed999f82SChuck Lever 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4485ed999f82SChuck Lever 			if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4486ed999f82SChuck Lever 				trace_cm_sent_drep(id_priv);
4487ed999f82SChuck Lever 		} else {
4488ed999f82SChuck Lever 			trace_cm_sent_dreq(id_priv);
4489ed999f82SChuck Lever 		}
449004215330SMichael Wang 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
449107ebafbaSTom Tucker 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
449221655afcSMichael Wang 	} else
449307ebafbaSTom Tucker 		ret = -EINVAL;
449421655afcSMichael Wang 
4495e51060f0SSean Hefty out:
4496e51060f0SSean Hefty 	return ret;
4497e51060f0SSean Hefty }
4498e51060f0SSean Hefty EXPORT_SYMBOL(rdma_disconnect);
4499e51060f0SSean Hefty 
4500b5de0c60SJason Gunthorpe static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4501b5de0c60SJason Gunthorpe 			      struct ib_sa_multicast *multicast,
4502b5de0c60SJason Gunthorpe 			      struct rdma_cm_event *event,
4503b5de0c60SJason Gunthorpe 			      struct cma_multicast *mc)
4504c8f6a362SSean Hefty {
4505b5de0c60SJason Gunthorpe 	struct rdma_dev_addr *dev_addr;
4506b5de0c60SJason Gunthorpe 	enum ib_gid_type gid_type;
4507b5de0c60SJason Gunthorpe 	struct net_device *ndev;
4508c8f6a362SSean Hefty 
45095c438135SSean Hefty 	if (!status)
45105c438135SSean Hefty 		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4511498683c6SMoni Shoua 	else
4512498683c6SMoni Shoua 		pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4513498683c6SMoni Shoua 				     status);
4514bee3c3c9SMoni Shoua 
4515b5de0c60SJason Gunthorpe 	event->status = status;
4516b5de0c60SJason Gunthorpe 	event->param.ud.private_data = mc->context;
4517b5de0c60SJason Gunthorpe 	if (status) {
4518b5de0c60SJason Gunthorpe 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4519b5de0c60SJason Gunthorpe 		return;
452046ea5061SSean Hefty 	}
4521c8f6a362SSean Hefty 
4522b5de0c60SJason Gunthorpe 	dev_addr = &id_priv->id.route.addr.dev_addr;
4523b5de0c60SJason Gunthorpe 	ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4524b5de0c60SJason Gunthorpe 	gid_type =
4525b5de0c60SJason Gunthorpe 		id_priv->cma_dev
4526b5de0c60SJason Gunthorpe 			->default_gid_type[id_priv->id.port_num -
4527b5de0c60SJason Gunthorpe 					   rdma_start_port(
4528b5de0c60SJason Gunthorpe 						   id_priv->cma_dev->device)];
4529c8f6a362SSean Hefty 
4530b5de0c60SJason Gunthorpe 	event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4531b5de0c60SJason Gunthorpe 	if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4532b5de0c60SJason Gunthorpe 				     &multicast->rec, ndev, gid_type,
4533b5de0c60SJason Gunthorpe 				     &event->param.ud.ah_attr)) {
4534b5de0c60SJason Gunthorpe 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4535b5de0c60SJason Gunthorpe 		goto out;
4536b5de0c60SJason Gunthorpe 	}
45376d337179SParav Pandit 
4538b5de0c60SJason Gunthorpe 	event->param.ud.qp_num = 0xFFFFFF;
4539b5de0c60SJason Gunthorpe 	event->param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
4540b5de0c60SJason Gunthorpe 
4541b5de0c60SJason Gunthorpe out:
4542bee3c3c9SMoni Shoua 	if (ndev)
4543bee3c3c9SMoni Shoua 		dev_put(ndev);
4544b5de0c60SJason Gunthorpe }
4545c8f6a362SSean Hefty 
4546b5de0c60SJason Gunthorpe static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4547b5de0c60SJason Gunthorpe {
4548b5de0c60SJason Gunthorpe 	struct cma_multicast *mc = multicast->context;
4549b5de0c60SJason Gunthorpe 	struct rdma_id_private *id_priv = mc->id_priv;
4550b5de0c60SJason Gunthorpe 	struct rdma_cm_event event = {};
4551b5de0c60SJason Gunthorpe 	int ret = 0;
4552b5de0c60SJason Gunthorpe 
4553b5de0c60SJason Gunthorpe 	mutex_lock(&id_priv->handler_mutex);
4554b5de0c60SJason Gunthorpe 	if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4555b5de0c60SJason Gunthorpe 	    READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4556b5de0c60SJason Gunthorpe 		goto out;
4557b5de0c60SJason Gunthorpe 
4558b5de0c60SJason Gunthorpe 	cma_make_mc_event(status, id_priv, multicast, &event, mc);
4559ed999f82SChuck Lever 	ret = cma_cm_event_handler(id_priv, &event);
4560f685c195SParav Pandit 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4561fe454dc3SAvihai Horon 	WARN_ON(ret);
45628aa08602SSean Hefty 
456337e07cdaSBart Van Assche out:
4564de910bd9SOr Gerlitz 	mutex_unlock(&id_priv->handler_mutex);
4565c8f6a362SSean Hefty 	return 0;
4566c8f6a362SSean Hefty }
4567c8f6a362SSean Hefty 
4568c8f6a362SSean Hefty static void cma_set_mgid(struct rdma_id_private *id_priv,
4569c8f6a362SSean Hefty 			 struct sockaddr *addr, union ib_gid *mgid)
4570c8f6a362SSean Hefty {
4571c8f6a362SSean Hefty 	unsigned char mc_map[MAX_ADDR_LEN];
4572c8f6a362SSean Hefty 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4573c8f6a362SSean Hefty 	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4574c8f6a362SSean Hefty 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4575c8f6a362SSean Hefty 
4576c8f6a362SSean Hefty 	if (cma_any_addr(addr)) {
4577c8f6a362SSean Hefty 		memset(mgid, 0, sizeof *mgid);
4578c8f6a362SSean Hefty 	} else if ((addr->sa_family == AF_INET6) &&
45791c9b2819SJason Gunthorpe 		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4580c8f6a362SSean Hefty 								 0xFF10A01B)) {
4581c8f6a362SSean Hefty 		/* IPv6 address is an SA assigned MGID. */
4582c8f6a362SSean Hefty 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
45835bc2b7b3SSean Hefty 	} else if (addr->sa_family == AF_IB) {
45845bc2b7b3SSean Hefty 		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4585076dd53bSVarsha Rao 	} else if (addr->sa_family == AF_INET6) {
4586e2e62697SJason Gunthorpe 		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4587e2e62697SJason Gunthorpe 		if (id_priv->id.ps == RDMA_PS_UDP)
4588e2e62697SJason Gunthorpe 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4589e2e62697SJason Gunthorpe 		*mgid = *(union ib_gid *) (mc_map + 4);
4590c8f6a362SSean Hefty 	} else {
4591a9e527e3SRolf Manderscheid 		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4592c8f6a362SSean Hefty 		if (id_priv->id.ps == RDMA_PS_UDP)
4593c8f6a362SSean Hefty 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4594c8f6a362SSean Hefty 		*mgid = *(union ib_gid *) (mc_map + 4);
4595c8f6a362SSean Hefty 	}
4596c8f6a362SSean Hefty }
4597c8f6a362SSean Hefty 
4598c8f6a362SSean Hefty static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4599c8f6a362SSean Hefty 				 struct cma_multicast *mc)
4600c8f6a362SSean Hefty {
4601c8f6a362SSean Hefty 	struct ib_sa_mcmember_rec rec;
4602c8f6a362SSean Hefty 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4603c8f6a362SSean Hefty 	ib_sa_comp_mask comp_mask;
4604c8f6a362SSean Hefty 	int ret;
4605c8f6a362SSean Hefty 
4606c8f6a362SSean Hefty 	ib_addr_get_mgid(dev_addr, &rec.mgid);
4607c8f6a362SSean Hefty 	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4608c8f6a362SSean Hefty 				     &rec.mgid, &rec);
4609c8f6a362SSean Hefty 	if (ret)
4610c8f6a362SSean Hefty 		return ret;
4611c8f6a362SSean Hefty 
46125bc2b7b3SSean Hefty 	ret = cma_set_qkey(id_priv, 0);
46135bc2b7b3SSean Hefty 	if (ret)
46145bc2b7b3SSean Hefty 		return ret;
46155bc2b7b3SSean Hefty 
46163f446754SRoland Dreier 	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
46175bc2b7b3SSean Hefty 	rec.qkey = cpu_to_be32(id_priv->qkey);
46186f8372b6SSean Hefty 	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4619c8f6a362SSean Hefty 	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4620ab15c95aSAlex Vesker 	rec.join_state = mc->join_state;
4621ab15c95aSAlex Vesker 
4622c8f6a362SSean Hefty 	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4623c8f6a362SSean Hefty 		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4624c8f6a362SSean Hefty 		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4625c8f6a362SSean Hefty 		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
4626c8f6a362SSean Hefty 		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4627c8f6a362SSean Hefty 
462884adeee9SYossi Etigin 	if (id_priv->id.ps == RDMA_PS_IPOIB)
462984adeee9SYossi Etigin 		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
46302a22fb8cSDotan Barak 			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
46312a22fb8cSDotan Barak 			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
46322a22fb8cSDotan Barak 			     IB_SA_MCMEMBER_REC_MTU |
46332a22fb8cSDotan Barak 			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
463484adeee9SYossi Etigin 
4635b5de0c60SJason Gunthorpe 	mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4636b5de0c60SJason Gunthorpe 					 id_priv->id.port_num, &rec, comp_mask,
4637b5de0c60SJason Gunthorpe 					 GFP_KERNEL, cma_ib_mc_handler, mc);
4638b5de0c60SJason Gunthorpe 	return PTR_ERR_OR_ZERO(mc->sa_mc);
46393c86aa70SEli Cohen }
46403c86aa70SEli Cohen 
4641be1d325aSNoa Osherovich static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4642be1d325aSNoa Osherovich 			      enum ib_gid_type gid_type)
46433c86aa70SEli Cohen {
46443c86aa70SEli Cohen 	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
46453c86aa70SEli Cohen 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
46463c86aa70SEli Cohen 
46473c86aa70SEli Cohen 	if (cma_any_addr(addr)) {
46483c86aa70SEli Cohen 		memset(mgid, 0, sizeof *mgid);
46493c86aa70SEli Cohen 	} else if (addr->sa_family == AF_INET6) {
46503c86aa70SEli Cohen 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
46513c86aa70SEli Cohen 	} else {
46525c181bdaSParav Pandit 		mgid->raw[0] =
46535c181bdaSParav Pandit 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
46545c181bdaSParav Pandit 		mgid->raw[1] =
46555c181bdaSParav Pandit 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
46563c86aa70SEli Cohen 		mgid->raw[2] = 0;
46573c86aa70SEli Cohen 		mgid->raw[3] = 0;
46583c86aa70SEli Cohen 		mgid->raw[4] = 0;
46593c86aa70SEli Cohen 		mgid->raw[5] = 0;
46603c86aa70SEli Cohen 		mgid->raw[6] = 0;
46613c86aa70SEli Cohen 		mgid->raw[7] = 0;
46623c86aa70SEli Cohen 		mgid->raw[8] = 0;
46633c86aa70SEli Cohen 		mgid->raw[9] = 0;
46643c86aa70SEli Cohen 		mgid->raw[10] = 0xff;
46653c86aa70SEli Cohen 		mgid->raw[11] = 0xff;
46663c86aa70SEli Cohen 		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
46673c86aa70SEli Cohen 	}
46683c86aa70SEli Cohen }
46693c86aa70SEli Cohen 
46703c86aa70SEli Cohen static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
46713c86aa70SEli Cohen 				   struct cma_multicast *mc)
46723c86aa70SEli Cohen {
46733c86aa70SEli Cohen 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4674bee3c3c9SMoni Shoua 	int err = 0;
46753c86aa70SEli Cohen 	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
46763c86aa70SEli Cohen 	struct net_device *ndev = NULL;
4677b5de0c60SJason Gunthorpe 	struct ib_sa_multicast ib;
4678bee3c3c9SMoni Shoua 	enum ib_gid_type gid_type;
4679ab15c95aSAlex Vesker 	bool send_only;
4680ab15c95aSAlex Vesker 
4681ab15c95aSAlex Vesker 	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
46823c86aa70SEli Cohen 
4683b5de0c60SJason Gunthorpe 	if (cma_zero_addr(addr))
46843c86aa70SEli Cohen 		return -EINVAL;
46853c86aa70SEli Cohen 
4686be1d325aSNoa Osherovich 	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4687be1d325aSNoa Osherovich 		   rdma_start_port(id_priv->cma_dev->device)];
4688b5de0c60SJason Gunthorpe 	cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
46893c86aa70SEli Cohen 
4690b5de0c60SJason Gunthorpe 	ib.rec.pkey = cpu_to_be16(0xffff);
46913c86aa70SEli Cohen 	if (id_priv->id.ps == RDMA_PS_UDP)
4692b5de0c60SJason Gunthorpe 		ib.rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
46933c86aa70SEli Cohen 
46943c86aa70SEli Cohen 	if (dev_addr->bound_dev_if)
4695052eac6eSParav Pandit 		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4696fe454dc3SAvihai Horon 	if (!ndev)
4697fe454dc3SAvihai Horon 		return -ENODEV;
4698fe454dc3SAvihai Horon 
4699b5de0c60SJason Gunthorpe 	ib.rec.rate = iboe_get_rate(ndev);
4700b5de0c60SJason Gunthorpe 	ib.rec.hop_limit = 1;
4701b5de0c60SJason Gunthorpe 	ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4702bee3c3c9SMoni Shoua 
4703bee3c3c9SMoni Shoua 	if (addr->sa_family == AF_INET) {
4704c65f6c5aSAlex Vesker 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4705b5de0c60SJason Gunthorpe 			ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4706ab15c95aSAlex Vesker 			if (!send_only) {
4707b5de0c60SJason Gunthorpe 				err = cma_igmp_send(ndev, &ib.rec.mgid,
4708bee3c3c9SMoni Shoua 						    true);
4709bee3c3c9SMoni Shoua 			}
4710bee3c3c9SMoni Shoua 		}
4711bee3c3c9SMoni Shoua 	} else {
4712bee3c3c9SMoni Shoua 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4713bee3c3c9SMoni Shoua 			err = -ENOTSUPP;
4714bee3c3c9SMoni Shoua 	}
47153c86aa70SEli Cohen 	dev_put(ndev);
4716fe454dc3SAvihai Horon 	if (err || !ib.rec.mtu)
4717fe454dc3SAvihai Horon 		return err ?: -EINVAL;
4718fe454dc3SAvihai Horon 
47197b85627bSMoni Shoua 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4720b5de0c60SJason Gunthorpe 		    &ib.rec.port_gid);
4721fe454dc3SAvihai Horon 	INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
4722fe454dc3SAvihai Horon 	cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
4723fe454dc3SAvihai Horon 	queue_work(cma_wq, &mc->iboe_join.work);
47243c86aa70SEli Cohen 	return 0;
47253c86aa70SEli Cohen }
47263c86aa70SEli Cohen 
4727c8f6a362SSean Hefty int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4728ab15c95aSAlex Vesker 			u8 join_state, void *context)
4729c8f6a362SSean Hefty {
47305cfbf929SJason Gunthorpe 	struct rdma_id_private *id_priv =
47315cfbf929SJason Gunthorpe 		container_of(id, struct rdma_id_private, id);
4732c8f6a362SSean Hefty 	struct cma_multicast *mc;
4733c8f6a362SSean Hefty 	int ret;
4734c8f6a362SSean Hefty 
47351bb5091dSJason Gunthorpe 	/* Not supported for kernel QPs */
47361bb5091dSJason Gunthorpe 	if (WARN_ON(id->qp))
47377688f2c3SLeon Romanovsky 		return -EINVAL;
47387688f2c3SLeon Romanovsky 
47395cfbf929SJason Gunthorpe 	/* ULP is calling this wrong. */
47405cfbf929SJason Gunthorpe 	if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
47415cfbf929SJason Gunthorpe 			    READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
4742c8f6a362SSean Hefty 		return -EINVAL;
4743c8f6a362SSean Hefty 
4744b5de0c60SJason Gunthorpe 	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
4745c8f6a362SSean Hefty 	if (!mc)
4746c8f6a362SSean Hefty 		return -ENOMEM;
4747c8f6a362SSean Hefty 
4748ef560861SSean Hefty 	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4749c8f6a362SSean Hefty 	mc->context = context;
4750c8f6a362SSean Hefty 	mc->id_priv = id_priv;
4751ab15c95aSAlex Vesker 	mc->join_state = join_state;
4752c8f6a362SSean Hefty 
47535d9fb044SIra Weiny 	if (rdma_protocol_roce(id->device, id->port_num)) {
47543c86aa70SEli Cohen 		ret = cma_iboe_join_multicast(id_priv, mc);
4755c0126915SJason Gunthorpe 		if (ret)
4756c0126915SJason Gunthorpe 			goto out_err;
4757c0126915SJason Gunthorpe 	} else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
47585c9a5282SMichael Wang 		ret = cma_join_ib_multicast(id_priv, mc);
4759c0126915SJason Gunthorpe 		if (ret)
4760c0126915SJason Gunthorpe 			goto out_err;
4761c0126915SJason Gunthorpe 	} else {
4762c8f6a362SSean Hefty 		ret = -ENOSYS;
4763c0126915SJason Gunthorpe 		goto out_err;
4764c8f6a362SSean Hefty 	}
4765c0126915SJason Gunthorpe 
4766c0126915SJason Gunthorpe 	spin_lock(&id_priv->lock);
4767c0126915SJason Gunthorpe 	list_add(&mc->list, &id_priv->mc_list);
4768c0126915SJason Gunthorpe 	spin_unlock(&id_priv->lock);
4769c0126915SJason Gunthorpe 
4770c0126915SJason Gunthorpe 	return 0;
4771c0126915SJason Gunthorpe out_err:
4772c0126915SJason Gunthorpe 	kfree(mc);
4773c8f6a362SSean Hefty 	return ret;
4774c8f6a362SSean Hefty }
4775c8f6a362SSean Hefty EXPORT_SYMBOL(rdma_join_multicast);
4776c8f6a362SSean Hefty 
4777c8f6a362SSean Hefty void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4778c8f6a362SSean Hefty {
4779c8f6a362SSean Hefty 	struct rdma_id_private *id_priv;
4780c8f6a362SSean Hefty 	struct cma_multicast *mc;
4781c8f6a362SSean Hefty 
4782c8f6a362SSean Hefty 	id_priv = container_of(id, struct rdma_id_private, id);
4783c8f6a362SSean Hefty 	spin_lock_irq(&id_priv->lock);
4784c8f6a362SSean Hefty 	list_for_each_entry(mc, &id_priv->mc_list, list) {
47853788d299SJason Gunthorpe 		if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
47863788d299SJason Gunthorpe 			continue;
4787c8f6a362SSean Hefty 		list_del(&mc->list);
4788c8f6a362SSean Hefty 		spin_unlock_irq(&id_priv->lock);
4789c8f6a362SSean Hefty 
47903788d299SJason Gunthorpe 		WARN_ON(id_priv->cma_dev->device != id->device);
47913788d299SJason Gunthorpe 		destroy_mc(id_priv, mc);
4792c8f6a362SSean Hefty 		return;
4793c8f6a362SSean Hefty 	}
4794c8f6a362SSean Hefty 	spin_unlock_irq(&id_priv->lock);
4795c8f6a362SSean Hefty }
4796c8f6a362SSean Hefty EXPORT_SYMBOL(rdma_leave_multicast);
4797c8f6a362SSean Hefty 
4798dd5bdff8SOr Gerlitz static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4799dd5bdff8SOr Gerlitz {
4800dd5bdff8SOr Gerlitz 	struct rdma_dev_addr *dev_addr;
48017e85bcdaSJason Gunthorpe 	struct cma_work *work;
4802dd5bdff8SOr Gerlitz 
4803dd5bdff8SOr Gerlitz 	dev_addr = &id_priv->id.route.addr.dev_addr;
4804dd5bdff8SOr Gerlitz 
48056266ed6eSSean Hefty 	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4806fa20105eSGuy Shapiro 	    (net_eq(dev_net(ndev), dev_addr->net)) &&
4807dd5bdff8SOr Gerlitz 	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4808aba25a3eSParav Pandit 		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4809dd5bdff8SOr Gerlitz 			ndev->name, &id_priv->id);
4810dd5bdff8SOr Gerlitz 		work = kzalloc(sizeof *work, GFP_KERNEL);
4811dd5bdff8SOr Gerlitz 		if (!work)
4812dd5bdff8SOr Gerlitz 			return -ENOMEM;
4813dd5bdff8SOr Gerlitz 
48147e85bcdaSJason Gunthorpe 		INIT_WORK(&work->work, cma_work_handler);
4815dd5bdff8SOr Gerlitz 		work->id = id_priv;
4816dd5bdff8SOr Gerlitz 		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4817e368d23fSParav Pandit 		cma_id_get(id_priv);
4818dd5bdff8SOr Gerlitz 		queue_work(cma_wq, &work->work);
4819dd5bdff8SOr Gerlitz 	}
4820dd5bdff8SOr Gerlitz 
4821dd5bdff8SOr Gerlitz 	return 0;
4822dd5bdff8SOr Gerlitz }
4823dd5bdff8SOr Gerlitz 
4824dd5bdff8SOr Gerlitz static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4825351638e7SJiri Pirko 			       void *ptr)
4826dd5bdff8SOr Gerlitz {
4827351638e7SJiri Pirko 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4828dd5bdff8SOr Gerlitz 	struct cma_device *cma_dev;
4829dd5bdff8SOr Gerlitz 	struct rdma_id_private *id_priv;
4830dd5bdff8SOr Gerlitz 	int ret = NOTIFY_DONE;
4831dd5bdff8SOr Gerlitz 
4832dd5bdff8SOr Gerlitz 	if (event != NETDEV_BONDING_FAILOVER)
4833dd5bdff8SOr Gerlitz 		return NOTIFY_DONE;
4834dd5bdff8SOr Gerlitz 
48353cd96fddSParav Pandit 	if (!netif_is_bond_master(ndev))
4836dd5bdff8SOr Gerlitz 		return NOTIFY_DONE;
4837dd5bdff8SOr Gerlitz 
4838dd5bdff8SOr Gerlitz 	mutex_lock(&lock);
4839dd5bdff8SOr Gerlitz 	list_for_each_entry(cma_dev, &dev_list, list)
4840dd5bdff8SOr Gerlitz 		list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4841dd5bdff8SOr Gerlitz 			ret = cma_netdev_change(ndev, id_priv);
4842dd5bdff8SOr Gerlitz 			if (ret)
4843dd5bdff8SOr Gerlitz 				goto out;
4844dd5bdff8SOr Gerlitz 		}
4845dd5bdff8SOr Gerlitz 
4846dd5bdff8SOr Gerlitz out:
4847dd5bdff8SOr Gerlitz 	mutex_unlock(&lock);
4848dd5bdff8SOr Gerlitz 	return ret;
4849dd5bdff8SOr Gerlitz }
4850dd5bdff8SOr Gerlitz 
4851dd5bdff8SOr Gerlitz static struct notifier_block cma_nb = {
4852dd5bdff8SOr Gerlitz 	.notifier_call = cma_netdev_callback
4853dd5bdff8SOr Gerlitz };
4854dd5bdff8SOr Gerlitz 
48553647a28dSJason Gunthorpe static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
4856e51060f0SSean Hefty {
48573647a28dSJason Gunthorpe 	struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
4858550e5ca7SNir Muchtar 	enum rdma_cm_state state;
48593647a28dSJason Gunthorpe 	unsigned long flags;
4860e51060f0SSean Hefty 
4861de910bd9SOr Gerlitz 	mutex_lock(&id_priv->handler_mutex);
48623647a28dSJason Gunthorpe 	/* Record that we want to remove the device */
48633647a28dSJason Gunthorpe 	spin_lock_irqsave(&id_priv->lock, flags);
48643647a28dSJason Gunthorpe 	state = id_priv->state;
48653647a28dSJason Gunthorpe 	if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
48663647a28dSJason Gunthorpe 		spin_unlock_irqrestore(&id_priv->lock, flags);
4867de910bd9SOr Gerlitz 		mutex_unlock(&id_priv->handler_mutex);
48683647a28dSJason Gunthorpe 		cma_id_put(id_priv);
48693647a28dSJason Gunthorpe 		return;
48703647a28dSJason Gunthorpe 	}
48713647a28dSJason Gunthorpe 	id_priv->state = RDMA_CM_DEVICE_REMOVAL;
48723647a28dSJason Gunthorpe 	spin_unlock_irqrestore(&id_priv->lock, flags);
48733647a28dSJason Gunthorpe 
48743647a28dSJason Gunthorpe 	if (cma_cm_event_handler(id_priv, &event)) {
48753647a28dSJason Gunthorpe 		/*
48763647a28dSJason Gunthorpe 		 * At this point the ULP promises it won't call
48773647a28dSJason Gunthorpe 		 * rdma_destroy_id() concurrently
48783647a28dSJason Gunthorpe 		 */
48793647a28dSJason Gunthorpe 		cma_id_put(id_priv);
48803647a28dSJason Gunthorpe 		mutex_unlock(&id_priv->handler_mutex);
4881f6a9d47aSJason Gunthorpe 		trace_cm_id_destroy(id_priv);
4882f6a9d47aSJason Gunthorpe 		_destroy_id(id_priv, state);
48833647a28dSJason Gunthorpe 		return;
48843647a28dSJason Gunthorpe 	}
48853647a28dSJason Gunthorpe 	mutex_unlock(&id_priv->handler_mutex);
48863647a28dSJason Gunthorpe 
48873647a28dSJason Gunthorpe 	/*
48883647a28dSJason Gunthorpe 	 * If this races with destroy then the thread that first assigns state
48893647a28dSJason Gunthorpe 	 * to a destroying does the cancel.
48903647a28dSJason Gunthorpe 	 */
48913647a28dSJason Gunthorpe 	cma_cancel_operation(id_priv, state);
48923647a28dSJason Gunthorpe 	cma_id_put(id_priv);
4893e51060f0SSean Hefty }
4894e51060f0SSean Hefty 
4895e51060f0SSean Hefty static void cma_process_remove(struct cma_device *cma_dev)
4896e51060f0SSean Hefty {
4897e51060f0SSean Hefty 	mutex_lock(&lock);
4898e51060f0SSean Hefty 	while (!list_empty(&cma_dev->id_list)) {
48993647a28dSJason Gunthorpe 		struct rdma_id_private *id_priv = list_first_entry(
49003647a28dSJason Gunthorpe 			&cma_dev->id_list, struct rdma_id_private, list);
4901e51060f0SSean Hefty 
4902d02d1f53SSean Hefty 		list_del(&id_priv->listen_list);
490394de178aSKrishna Kumar 		list_del_init(&id_priv->list);
4904e368d23fSParav Pandit 		cma_id_get(id_priv);
4905e51060f0SSean Hefty 		mutex_unlock(&lock);
4906e51060f0SSean Hefty 
49073647a28dSJason Gunthorpe 		cma_send_device_removal_put(id_priv);
4908e51060f0SSean Hefty 
4909e51060f0SSean Hefty 		mutex_lock(&lock);
4910e51060f0SSean Hefty 	}
4911e51060f0SSean Hefty 	mutex_unlock(&lock);
4912e51060f0SSean Hefty 
49135ff8c8faSParav Pandit 	cma_dev_put(cma_dev);
4914e51060f0SSean Hefty 	wait_for_completion(&cma_dev->comp);
4915e51060f0SSean Hefty }
4916e51060f0SSean Hefty 
49174d51c3d9SParav Pandit static bool cma_supported(struct ib_device *device)
49184d51c3d9SParav Pandit {
49194d51c3d9SParav Pandit 	u32 i;
49204d51c3d9SParav Pandit 
49214d51c3d9SParav Pandit 	rdma_for_each_port(device, i) {
49224d51c3d9SParav Pandit 		if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
49234d51c3d9SParav Pandit 			return true;
49244d51c3d9SParav Pandit 	}
49254d51c3d9SParav Pandit 	return false;
49264d51c3d9SParav Pandit }
49274d51c3d9SParav Pandit 
4928c80a0c52SLeon Romanovsky static int cma_add_one(struct ib_device *device)
4929c80a0c52SLeon Romanovsky {
4930dd37d2f5SJason Gunthorpe 	struct rdma_id_private *to_destroy;
4931c80a0c52SLeon Romanovsky 	struct cma_device *cma_dev;
4932c80a0c52SLeon Romanovsky 	struct rdma_id_private *id_priv;
4933c80a0c52SLeon Romanovsky 	unsigned long supported_gids = 0;
4934c80a0c52SLeon Romanovsky 	int ret;
49351fb7f897SMark Bloch 	u32 i;
4936c80a0c52SLeon Romanovsky 
49374d51c3d9SParav Pandit 	if (!cma_supported(device))
49384d51c3d9SParav Pandit 		return -EOPNOTSUPP;
49394d51c3d9SParav Pandit 
4940c80a0c52SLeon Romanovsky 	cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
4941c80a0c52SLeon Romanovsky 	if (!cma_dev)
4942c80a0c52SLeon Romanovsky 		return -ENOMEM;
4943c80a0c52SLeon Romanovsky 
4944c80a0c52SLeon Romanovsky 	cma_dev->device = device;
4945c80a0c52SLeon Romanovsky 	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4946c80a0c52SLeon Romanovsky 					    sizeof(*cma_dev->default_gid_type),
4947c80a0c52SLeon Romanovsky 					    GFP_KERNEL);
4948c80a0c52SLeon Romanovsky 	if (!cma_dev->default_gid_type) {
4949c80a0c52SLeon Romanovsky 		ret = -ENOMEM;
4950c80a0c52SLeon Romanovsky 		goto free_cma_dev;
4951c80a0c52SLeon Romanovsky 	}
4952c80a0c52SLeon Romanovsky 
4953c80a0c52SLeon Romanovsky 	cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
4954c80a0c52SLeon Romanovsky 					    sizeof(*cma_dev->default_roce_tos),
4955c80a0c52SLeon Romanovsky 					    GFP_KERNEL);
4956c80a0c52SLeon Romanovsky 	if (!cma_dev->default_roce_tos) {
4957c80a0c52SLeon Romanovsky 		ret = -ENOMEM;
4958c80a0c52SLeon Romanovsky 		goto free_gid_type;
4959c80a0c52SLeon Romanovsky 	}
4960c80a0c52SLeon Romanovsky 
4961c80a0c52SLeon Romanovsky 	rdma_for_each_port (device, i) {
4962c80a0c52SLeon Romanovsky 		supported_gids = roce_gid_type_mask_support(device, i);
4963c80a0c52SLeon Romanovsky 		WARN_ON(!supported_gids);
4964c80a0c52SLeon Romanovsky 		if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
4965c80a0c52SLeon Romanovsky 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
4966c80a0c52SLeon Romanovsky 				CMA_PREFERRED_ROCE_GID_TYPE;
4967c80a0c52SLeon Romanovsky 		else
4968c80a0c52SLeon Romanovsky 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
4969c80a0c52SLeon Romanovsky 				find_first_bit(&supported_gids, BITS_PER_LONG);
4970c80a0c52SLeon Romanovsky 		cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
4971c80a0c52SLeon Romanovsky 	}
4972c80a0c52SLeon Romanovsky 
4973c80a0c52SLeon Romanovsky 	init_completion(&cma_dev->comp);
4974c80a0c52SLeon Romanovsky 	refcount_set(&cma_dev->refcount, 1);
4975c80a0c52SLeon Romanovsky 	INIT_LIST_HEAD(&cma_dev->id_list);
4976c80a0c52SLeon Romanovsky 	ib_set_client_data(device, &cma_client, cma_dev);
4977c80a0c52SLeon Romanovsky 
4978c80a0c52SLeon Romanovsky 	mutex_lock(&lock);
4979c80a0c52SLeon Romanovsky 	list_add_tail(&cma_dev->list, &dev_list);
4980c80a0c52SLeon Romanovsky 	list_for_each_entry(id_priv, &listen_any_list, list) {
4981dd37d2f5SJason Gunthorpe 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
4982c80a0c52SLeon Romanovsky 		if (ret)
4983c80a0c52SLeon Romanovsky 			goto free_listen;
4984c80a0c52SLeon Romanovsky 	}
4985c80a0c52SLeon Romanovsky 	mutex_unlock(&lock);
4986c80a0c52SLeon Romanovsky 
4987c80a0c52SLeon Romanovsky 	trace_cm_add_one(device);
4988c80a0c52SLeon Romanovsky 	return 0;
4989c80a0c52SLeon Romanovsky 
4990c80a0c52SLeon Romanovsky free_listen:
4991c80a0c52SLeon Romanovsky 	list_del(&cma_dev->list);
4992c80a0c52SLeon Romanovsky 	mutex_unlock(&lock);
4993c80a0c52SLeon Romanovsky 
4994dd37d2f5SJason Gunthorpe 	/* cma_process_remove() will delete to_destroy */
4995c80a0c52SLeon Romanovsky 	cma_process_remove(cma_dev);
4996c80a0c52SLeon Romanovsky 	kfree(cma_dev->default_roce_tos);
4997c80a0c52SLeon Romanovsky free_gid_type:
4998c80a0c52SLeon Romanovsky 	kfree(cma_dev->default_gid_type);
4999c80a0c52SLeon Romanovsky 
5000c80a0c52SLeon Romanovsky free_cma_dev:
5001c80a0c52SLeon Romanovsky 	kfree(cma_dev);
5002c80a0c52SLeon Romanovsky 	return ret;
5003c80a0c52SLeon Romanovsky }
5004c80a0c52SLeon Romanovsky 
50057c1eb45aSHaggai Eran static void cma_remove_one(struct ib_device *device, void *client_data)
5006e51060f0SSean Hefty {
50077c1eb45aSHaggai Eran 	struct cma_device *cma_dev = client_data;
5008e51060f0SSean Hefty 
5009ed999f82SChuck Lever 	trace_cm_remove_one(device);
5010ed999f82SChuck Lever 
5011e51060f0SSean Hefty 	mutex_lock(&lock);
5012e51060f0SSean Hefty 	list_del(&cma_dev->list);
5013e51060f0SSean Hefty 	mutex_unlock(&lock);
5014e51060f0SSean Hefty 
5015e51060f0SSean Hefty 	cma_process_remove(cma_dev);
501689052d78SMajd Dibbiny 	kfree(cma_dev->default_roce_tos);
5017045959dbSMatan Barak 	kfree(cma_dev->default_gid_type);
5018e51060f0SSean Hefty 	kfree(cma_dev);
5019e51060f0SSean Hefty }
5020e51060f0SSean Hefty 
50214be74b42SHaggai Eran static int cma_init_net(struct net *net)
50224be74b42SHaggai Eran {
50234be74b42SHaggai Eran 	struct cma_pernet *pernet = cma_pernet(net);
50244be74b42SHaggai Eran 
502563826753SMatthew Wilcox 	xa_init(&pernet->tcp_ps);
502663826753SMatthew Wilcox 	xa_init(&pernet->udp_ps);
502763826753SMatthew Wilcox 	xa_init(&pernet->ipoib_ps);
502863826753SMatthew Wilcox 	xa_init(&pernet->ib_ps);
50294be74b42SHaggai Eran 
50304be74b42SHaggai Eran 	return 0;
50314be74b42SHaggai Eran }
50324be74b42SHaggai Eran 
50334be74b42SHaggai Eran static void cma_exit_net(struct net *net)
50344be74b42SHaggai Eran {
50354be74b42SHaggai Eran 	struct cma_pernet *pernet = cma_pernet(net);
50364be74b42SHaggai Eran 
503763826753SMatthew Wilcox 	WARN_ON(!xa_empty(&pernet->tcp_ps));
503863826753SMatthew Wilcox 	WARN_ON(!xa_empty(&pernet->udp_ps));
503963826753SMatthew Wilcox 	WARN_ON(!xa_empty(&pernet->ipoib_ps));
504063826753SMatthew Wilcox 	WARN_ON(!xa_empty(&pernet->ib_ps));
50414be74b42SHaggai Eran }
50424be74b42SHaggai Eran 
50434be74b42SHaggai Eran static struct pernet_operations cma_pernet_operations = {
50444be74b42SHaggai Eran 	.init = cma_init_net,
50454be74b42SHaggai Eran 	.exit = cma_exit_net,
50464be74b42SHaggai Eran 	.id = &cma_pernet_id,
50474be74b42SHaggai Eran 	.size = sizeof(struct cma_pernet),
50484be74b42SHaggai Eran };
50494be74b42SHaggai Eran 
5050716abb1fSPeter Huewe static int __init cma_init(void)
5051e51060f0SSean Hefty {
50525d7220e8STetsuo Handa 	int ret;
5053227b60f5SStephen Hemminger 
505432ac9e43SJason Gunthorpe 	/*
505532ac9e43SJason Gunthorpe 	 * There is a rare lock ordering dependency in cma_netdev_callback()
505632ac9e43SJason Gunthorpe 	 * that only happens when bonding is enabled. Teach lockdep that rtnl
505732ac9e43SJason Gunthorpe 	 * must never be nested under lock so it can find these without having
505832ac9e43SJason Gunthorpe 	 * to test with bonding.
505932ac9e43SJason Gunthorpe 	 */
506032ac9e43SJason Gunthorpe 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
506132ac9e43SJason Gunthorpe 		rtnl_lock();
506232ac9e43SJason Gunthorpe 		mutex_lock(&lock);
506332ac9e43SJason Gunthorpe 		mutex_unlock(&lock);
506432ac9e43SJason Gunthorpe 		rtnl_unlock();
506532ac9e43SJason Gunthorpe 	}
506632ac9e43SJason Gunthorpe 
5067dee9acbbSBhaktipriya Shridhar 	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5068e51060f0SSean Hefty 	if (!cma_wq)
5069e51060f0SSean Hefty 		return -ENOMEM;
5070e51060f0SSean Hefty 
50714be74b42SHaggai Eran 	ret = register_pernet_subsys(&cma_pernet_operations);
50724be74b42SHaggai Eran 	if (ret)
50734be74b42SHaggai Eran 		goto err_wq;
50744be74b42SHaggai Eran 
5075c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&sa_client);
5076dd5bdff8SOr Gerlitz 	register_netdevice_notifier(&cma_nb);
5077c1a0b23bSMichael S. Tsirkin 
5078e51060f0SSean Hefty 	ret = ib_register_client(&cma_client);
5079e51060f0SSean Hefty 	if (ret)
5080e51060f0SSean Hefty 		goto err;
5081753f618aSNir Muchtar 
5082a7bfb93fSzhengbin 	ret = cma_configfs_init();
5083a7bfb93fSzhengbin 	if (ret)
5084a7bfb93fSzhengbin 		goto err_ib;
5085753f618aSNir Muchtar 
5086e51060f0SSean Hefty 	return 0;
5087e51060f0SSean Hefty 
5088a7bfb93fSzhengbin err_ib:
5089a7bfb93fSzhengbin 	ib_unregister_client(&cma_client);
5090e51060f0SSean Hefty err:
5091dd5bdff8SOr Gerlitz 	unregister_netdevice_notifier(&cma_nb);
5092c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&sa_client);
509344a7b675SChuhong Yuan 	unregister_pernet_subsys(&cma_pernet_operations);
50944be74b42SHaggai Eran err_wq:
5095e51060f0SSean Hefty 	destroy_workqueue(cma_wq);
5096e51060f0SSean Hefty 	return ret;
5097e51060f0SSean Hefty }
5098e51060f0SSean Hefty 
5099716abb1fSPeter Huewe static void __exit cma_cleanup(void)
5100e51060f0SSean Hefty {
5101045959dbSMatan Barak 	cma_configfs_exit();
5102e51060f0SSean Hefty 	ib_unregister_client(&cma_client);
5103dd5bdff8SOr Gerlitz 	unregister_netdevice_notifier(&cma_nb);
5104c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&sa_client);
51054be74b42SHaggai Eran 	unregister_pernet_subsys(&cma_pernet_operations);
5106e51060f0SSean Hefty 	destroy_workqueue(cma_wq);
5107e51060f0SSean Hefty }
5108e51060f0SSean Hefty 
5109e51060f0SSean Hefty module_init(cma_init);
5110e51060f0SSean Hefty module_exit(cma_cleanup);
5111