xref: /linux/drivers/infiniband/core/cma.c (revision 662fa3d6099374c4615bf64d06895e3573b935b2)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
4  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
5  * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
7  */
8 
9 #include <linux/completion.h>
10 #include <linux/in.h>
11 #include <linux/in6.h>
12 #include <linux/mutex.h>
13 #include <linux/random.h>
14 #include <linux/igmp.h>
15 #include <linux/xarray.h>
16 #include <linux/inetdevice.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <net/route.h>
20 
21 #include <net/net_namespace.h>
22 #include <net/netns/generic.h>
23 #include <net/tcp.h>
24 #include <net/ipv6.h>
25 #include <net/ip_fib.h>
26 #include <net/ip6_route.h>
27 
28 #include <rdma/rdma_cm.h>
29 #include <rdma/rdma_cm_ib.h>
30 #include <rdma/rdma_netlink.h>
31 #include <rdma/ib.h>
32 #include <rdma/ib_cache.h>
33 #include <rdma/ib_cm.h>
34 #include <rdma/ib_sa.h>
35 #include <rdma/iw_cm.h>
36 
37 #include "core_priv.h"
38 #include "cma_priv.h"
39 #include "cma_trace.h"
40 
41 MODULE_AUTHOR("Sean Hefty");
42 MODULE_DESCRIPTION("Generic RDMA CM Agent");
43 MODULE_LICENSE("Dual BSD/GPL");
44 
45 #define CMA_CM_RESPONSE_TIMEOUT 20
46 #define CMA_MAX_CM_RETRIES 15
47 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
48 #define CMA_IBOE_PACKET_LIFETIME 18
49 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
50 
51 static const char * const cma_events[] = {
52 	[RDMA_CM_EVENT_ADDR_RESOLVED]	 = "address resolved",
53 	[RDMA_CM_EVENT_ADDR_ERROR]	 = "address error",
54 	[RDMA_CM_EVENT_ROUTE_RESOLVED]	 = "route resolved ",
55 	[RDMA_CM_EVENT_ROUTE_ERROR]	 = "route error",
56 	[RDMA_CM_EVENT_CONNECT_REQUEST]	 = "connect request",
57 	[RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
58 	[RDMA_CM_EVENT_CONNECT_ERROR]	 = "connect error",
59 	[RDMA_CM_EVENT_UNREACHABLE]	 = "unreachable",
60 	[RDMA_CM_EVENT_REJECTED]	 = "rejected",
61 	[RDMA_CM_EVENT_ESTABLISHED]	 = "established",
62 	[RDMA_CM_EVENT_DISCONNECTED]	 = "disconnected",
63 	[RDMA_CM_EVENT_DEVICE_REMOVAL]	 = "device removal",
64 	[RDMA_CM_EVENT_MULTICAST_JOIN]	 = "multicast join",
65 	[RDMA_CM_EVENT_MULTICAST_ERROR]	 = "multicast error",
66 	[RDMA_CM_EVENT_ADDR_CHANGE]	 = "address change",
67 	[RDMA_CM_EVENT_TIMEWAIT_EXIT]	 = "timewait exit",
68 };
69 
70 static void cma_set_mgid(struct rdma_id_private *id_priv, struct sockaddr *addr,
71 			 union ib_gid *mgid);
72 
73 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
74 {
75 	size_t index = event;
76 
77 	return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
78 			cma_events[index] : "unrecognized event";
79 }
80 EXPORT_SYMBOL(rdma_event_msg);
81 
82 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
83 						int reason)
84 {
85 	if (rdma_ib_or_roce(id->device, id->port_num))
86 		return ibcm_reject_msg(reason);
87 
88 	if (rdma_protocol_iwarp(id->device, id->port_num))
89 		return iwcm_reject_msg(reason);
90 
91 	WARN_ON_ONCE(1);
92 	return "unrecognized transport";
93 }
94 EXPORT_SYMBOL(rdma_reject_msg);
95 
96 /**
97  * rdma_is_consumer_reject - return true if the consumer rejected the connect
98  *                           request.
99  * @id: Communication identifier that received the REJECT event.
100  * @reason: Value returned in the REJECT event status field.
101  */
102 static bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
103 {
104 	if (rdma_ib_or_roce(id->device, id->port_num))
105 		return reason == IB_CM_REJ_CONSUMER_DEFINED;
106 
107 	if (rdma_protocol_iwarp(id->device, id->port_num))
108 		return reason == -ECONNREFUSED;
109 
110 	WARN_ON_ONCE(1);
111 	return false;
112 }
113 
114 const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
115 				      struct rdma_cm_event *ev, u8 *data_len)
116 {
117 	const void *p;
118 
119 	if (rdma_is_consumer_reject(id, ev->status)) {
120 		*data_len = ev->param.conn.private_data_len;
121 		p = ev->param.conn.private_data;
122 	} else {
123 		*data_len = 0;
124 		p = NULL;
125 	}
126 	return p;
127 }
128 EXPORT_SYMBOL(rdma_consumer_reject_data);
129 
130 /**
131  * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
132  * @id: Communication Identifier
133  */
134 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
135 {
136 	struct rdma_id_private *id_priv;
137 
138 	id_priv = container_of(id, struct rdma_id_private, id);
139 	if (id->device->node_type == RDMA_NODE_RNIC)
140 		return id_priv->cm_id.iw;
141 	return NULL;
142 }
143 EXPORT_SYMBOL(rdma_iw_cm_id);
144 
145 /**
146  * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
147  * @res: rdma resource tracking entry pointer
148  */
149 struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
150 {
151 	struct rdma_id_private *id_priv =
152 		container_of(res, struct rdma_id_private, res);
153 
154 	return &id_priv->id;
155 }
156 EXPORT_SYMBOL(rdma_res_to_id);
157 
158 static int cma_add_one(struct ib_device *device);
159 static void cma_remove_one(struct ib_device *device, void *client_data);
160 
161 static struct ib_client cma_client = {
162 	.name   = "cma",
163 	.add    = cma_add_one,
164 	.remove = cma_remove_one
165 };
166 
167 static struct ib_sa_client sa_client;
168 static LIST_HEAD(dev_list);
169 static LIST_HEAD(listen_any_list);
170 static DEFINE_MUTEX(lock);
171 static struct workqueue_struct *cma_wq;
172 static unsigned int cma_pernet_id;
173 
174 struct cma_pernet {
175 	struct xarray tcp_ps;
176 	struct xarray udp_ps;
177 	struct xarray ipoib_ps;
178 	struct xarray ib_ps;
179 };
180 
181 static struct cma_pernet *cma_pernet(struct net *net)
182 {
183 	return net_generic(net, cma_pernet_id);
184 }
185 
186 static
187 struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
188 {
189 	struct cma_pernet *pernet = cma_pernet(net);
190 
191 	switch (ps) {
192 	case RDMA_PS_TCP:
193 		return &pernet->tcp_ps;
194 	case RDMA_PS_UDP:
195 		return &pernet->udp_ps;
196 	case RDMA_PS_IPOIB:
197 		return &pernet->ipoib_ps;
198 	case RDMA_PS_IB:
199 		return &pernet->ib_ps;
200 	default:
201 		return NULL;
202 	}
203 }
204 
205 struct cma_device {
206 	struct list_head	list;
207 	struct ib_device	*device;
208 	struct completion	comp;
209 	refcount_t refcount;
210 	struct list_head	id_list;
211 	enum ib_gid_type	*default_gid_type;
212 	u8			*default_roce_tos;
213 };
214 
215 struct rdma_bind_list {
216 	enum rdma_ucm_port_space ps;
217 	struct hlist_head	owners;
218 	unsigned short		port;
219 };
220 
221 static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
222 			struct rdma_bind_list *bind_list, int snum)
223 {
224 	struct xarray *xa = cma_pernet_xa(net, ps);
225 
226 	return xa_insert(xa, snum, bind_list, GFP_KERNEL);
227 }
228 
229 static struct rdma_bind_list *cma_ps_find(struct net *net,
230 					  enum rdma_ucm_port_space ps, int snum)
231 {
232 	struct xarray *xa = cma_pernet_xa(net, ps);
233 
234 	return xa_load(xa, snum);
235 }
236 
237 static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
238 			  int snum)
239 {
240 	struct xarray *xa = cma_pernet_xa(net, ps);
241 
242 	xa_erase(xa, snum);
243 }
244 
245 enum {
246 	CMA_OPTION_AFONLY,
247 };
248 
249 void cma_dev_get(struct cma_device *cma_dev)
250 {
251 	refcount_inc(&cma_dev->refcount);
252 }
253 
254 void cma_dev_put(struct cma_device *cma_dev)
255 {
256 	if (refcount_dec_and_test(&cma_dev->refcount))
257 		complete(&cma_dev->comp);
258 }
259 
260 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter	filter,
261 					     void		*cookie)
262 {
263 	struct cma_device *cma_dev;
264 	struct cma_device *found_cma_dev = NULL;
265 
266 	mutex_lock(&lock);
267 
268 	list_for_each_entry(cma_dev, &dev_list, list)
269 		if (filter(cma_dev->device, cookie)) {
270 			found_cma_dev = cma_dev;
271 			break;
272 		}
273 
274 	if (found_cma_dev)
275 		cma_dev_get(found_cma_dev);
276 	mutex_unlock(&lock);
277 	return found_cma_dev;
278 }
279 
280 int cma_get_default_gid_type(struct cma_device *cma_dev,
281 			     u32 port)
282 {
283 	if (!rdma_is_port_valid(cma_dev->device, port))
284 		return -EINVAL;
285 
286 	return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
287 }
288 
289 int cma_set_default_gid_type(struct cma_device *cma_dev,
290 			     u32 port,
291 			     enum ib_gid_type default_gid_type)
292 {
293 	unsigned long supported_gids;
294 
295 	if (!rdma_is_port_valid(cma_dev->device, port))
296 		return -EINVAL;
297 
298 	if (default_gid_type == IB_GID_TYPE_IB &&
299 	    rdma_protocol_roce_eth_encap(cma_dev->device, port))
300 		default_gid_type = IB_GID_TYPE_ROCE;
301 
302 	supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
303 
304 	if (!(supported_gids & 1 << default_gid_type))
305 		return -EINVAL;
306 
307 	cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
308 		default_gid_type;
309 
310 	return 0;
311 }
312 
313 int cma_get_default_roce_tos(struct cma_device *cma_dev, u32 port)
314 {
315 	if (!rdma_is_port_valid(cma_dev->device, port))
316 		return -EINVAL;
317 
318 	return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
319 }
320 
321 int cma_set_default_roce_tos(struct cma_device *cma_dev, u32 port,
322 			     u8 default_roce_tos)
323 {
324 	if (!rdma_is_port_valid(cma_dev->device, port))
325 		return -EINVAL;
326 
327 	cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
328 		 default_roce_tos;
329 
330 	return 0;
331 }
332 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
333 {
334 	return cma_dev->device;
335 }
336 
337 /*
338  * Device removal can occur at anytime, so we need extra handling to
339  * serialize notifying the user of device removal with other callbacks.
340  * We do this by disabling removal notification while a callback is in process,
341  * and reporting it after the callback completes.
342  */
343 
344 struct cma_multicast {
345 	struct rdma_id_private *id_priv;
346 	union {
347 		struct ib_sa_multicast *sa_mc;
348 		struct {
349 			struct work_struct work;
350 			struct rdma_cm_event event;
351 		} iboe_join;
352 	};
353 	struct list_head	list;
354 	void			*context;
355 	struct sockaddr_storage	addr;
356 	u8			join_state;
357 };
358 
359 struct cma_work {
360 	struct work_struct	work;
361 	struct rdma_id_private	*id;
362 	enum rdma_cm_state	old_state;
363 	enum rdma_cm_state	new_state;
364 	struct rdma_cm_event	event;
365 };
366 
367 union cma_ip_addr {
368 	struct in6_addr ip6;
369 	struct {
370 		__be32 pad[3];
371 		__be32 addr;
372 	} ip4;
373 };
374 
375 struct cma_hdr {
376 	u8 cma_version;
377 	u8 ip_version;	/* IP version: 7:4 */
378 	__be16 port;
379 	union cma_ip_addr src_addr;
380 	union cma_ip_addr dst_addr;
381 };
382 
383 #define CMA_VERSION 0x00
384 
385 struct cma_req_info {
386 	struct sockaddr_storage listen_addr_storage;
387 	struct sockaddr_storage src_addr_storage;
388 	struct ib_device *device;
389 	union ib_gid local_gid;
390 	__be64 service_id;
391 	int port;
392 	bool has_gid;
393 	u16 pkey;
394 };
395 
396 static int cma_comp_exch(struct rdma_id_private *id_priv,
397 			 enum rdma_cm_state comp, enum rdma_cm_state exch)
398 {
399 	unsigned long flags;
400 	int ret;
401 
402 	/*
403 	 * The FSM uses a funny double locking where state is protected by both
404 	 * the handler_mutex and the spinlock. State is not allowed to change
405 	 * to/from a handler_mutex protected value without also holding
406 	 * handler_mutex.
407 	 */
408 	if (comp == RDMA_CM_CONNECT || exch == RDMA_CM_CONNECT)
409 		lockdep_assert_held(&id_priv->handler_mutex);
410 
411 	spin_lock_irqsave(&id_priv->lock, flags);
412 	if ((ret = (id_priv->state == comp)))
413 		id_priv->state = exch;
414 	spin_unlock_irqrestore(&id_priv->lock, flags);
415 	return ret;
416 }
417 
418 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
419 {
420 	return hdr->ip_version >> 4;
421 }
422 
423 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
424 {
425 	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
426 }
427 
428 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
429 {
430 	struct in_device *in_dev = NULL;
431 
432 	if (ndev) {
433 		rtnl_lock();
434 		in_dev = __in_dev_get_rtnl(ndev);
435 		if (in_dev) {
436 			if (join)
437 				ip_mc_inc_group(in_dev,
438 						*(__be32 *)(mgid->raw + 12));
439 			else
440 				ip_mc_dec_group(in_dev,
441 						*(__be32 *)(mgid->raw + 12));
442 		}
443 		rtnl_unlock();
444 	}
445 	return (in_dev) ? 0 : -ENODEV;
446 }
447 
448 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
449 			       struct cma_device *cma_dev)
450 {
451 	cma_dev_get(cma_dev);
452 	id_priv->cma_dev = cma_dev;
453 	id_priv->id.device = cma_dev->device;
454 	id_priv->id.route.addr.dev_addr.transport =
455 		rdma_node_get_transport(cma_dev->device->node_type);
456 	list_add_tail(&id_priv->list, &cma_dev->id_list);
457 
458 	trace_cm_id_attach(id_priv, cma_dev->device);
459 }
460 
461 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
462 			      struct cma_device *cma_dev)
463 {
464 	_cma_attach_to_dev(id_priv, cma_dev);
465 	id_priv->gid_type =
466 		cma_dev->default_gid_type[id_priv->id.port_num -
467 					  rdma_start_port(cma_dev->device)];
468 }
469 
470 static void cma_release_dev(struct rdma_id_private *id_priv)
471 {
472 	mutex_lock(&lock);
473 	list_del(&id_priv->list);
474 	cma_dev_put(id_priv->cma_dev);
475 	id_priv->cma_dev = NULL;
476 	id_priv->id.device = NULL;
477 	if (id_priv->id.route.addr.dev_addr.sgid_attr) {
478 		rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr);
479 		id_priv->id.route.addr.dev_addr.sgid_attr = NULL;
480 	}
481 	mutex_unlock(&lock);
482 }
483 
484 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
485 {
486 	return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
487 }
488 
489 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
490 {
491 	return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
492 }
493 
494 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
495 {
496 	return id_priv->id.route.addr.src_addr.ss_family;
497 }
498 
499 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
500 {
501 	struct ib_sa_mcmember_rec rec;
502 	int ret = 0;
503 
504 	if (id_priv->qkey) {
505 		if (qkey && id_priv->qkey != qkey)
506 			return -EINVAL;
507 		return 0;
508 	}
509 
510 	if (qkey) {
511 		id_priv->qkey = qkey;
512 		return 0;
513 	}
514 
515 	switch (id_priv->id.ps) {
516 	case RDMA_PS_UDP:
517 	case RDMA_PS_IB:
518 		id_priv->qkey = RDMA_UDP_QKEY;
519 		break;
520 	case RDMA_PS_IPOIB:
521 		ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
522 		ret = ib_sa_get_mcmember_rec(id_priv->id.device,
523 					     id_priv->id.port_num, &rec.mgid,
524 					     &rec);
525 		if (!ret)
526 			id_priv->qkey = be32_to_cpu(rec.qkey);
527 		break;
528 	default:
529 		break;
530 	}
531 	return ret;
532 }
533 
534 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
535 {
536 	dev_addr->dev_type = ARPHRD_INFINIBAND;
537 	rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
538 	ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
539 }
540 
541 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
542 {
543 	int ret;
544 
545 	if (addr->sa_family != AF_IB) {
546 		ret = rdma_translate_ip(addr, dev_addr);
547 	} else {
548 		cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
549 		ret = 0;
550 	}
551 
552 	return ret;
553 }
554 
555 static const struct ib_gid_attr *
556 cma_validate_port(struct ib_device *device, u32 port,
557 		  enum ib_gid_type gid_type,
558 		  union ib_gid *gid,
559 		  struct rdma_id_private *id_priv)
560 {
561 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
562 	int bound_if_index = dev_addr->bound_dev_if;
563 	const struct ib_gid_attr *sgid_attr;
564 	int dev_type = dev_addr->dev_type;
565 	struct net_device *ndev = NULL;
566 
567 	if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
568 		return ERR_PTR(-ENODEV);
569 
570 	if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
571 		return ERR_PTR(-ENODEV);
572 
573 	if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
574 		return ERR_PTR(-ENODEV);
575 
576 	if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
577 		ndev = dev_get_by_index(dev_addr->net, bound_if_index);
578 		if (!ndev)
579 			return ERR_PTR(-ENODEV);
580 	} else {
581 		gid_type = IB_GID_TYPE_IB;
582 	}
583 
584 	sgid_attr = rdma_find_gid_by_port(device, gid, gid_type, port, ndev);
585 	if (ndev)
586 		dev_put(ndev);
587 	return sgid_attr;
588 }
589 
590 static void cma_bind_sgid_attr(struct rdma_id_private *id_priv,
591 			       const struct ib_gid_attr *sgid_attr)
592 {
593 	WARN_ON(id_priv->id.route.addr.dev_addr.sgid_attr);
594 	id_priv->id.route.addr.dev_addr.sgid_attr = sgid_attr;
595 }
596 
597 /**
598  * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
599  * based on source ip address.
600  * @id_priv:	cm_id which should be bound to cma device
601  *
602  * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
603  * based on source IP address. It returns 0 on success or error code otherwise.
604  * It is applicable to active and passive side cm_id.
605  */
606 static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
607 {
608 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
609 	const struct ib_gid_attr *sgid_attr;
610 	union ib_gid gid, iboe_gid, *gidp;
611 	struct cma_device *cma_dev;
612 	enum ib_gid_type gid_type;
613 	int ret = -ENODEV;
614 	u32 port;
615 
616 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
617 	    id_priv->id.ps == RDMA_PS_IPOIB)
618 		return -EINVAL;
619 
620 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
621 		    &iboe_gid);
622 
623 	memcpy(&gid, dev_addr->src_dev_addr +
624 	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
625 
626 	mutex_lock(&lock);
627 	list_for_each_entry(cma_dev, &dev_list, list) {
628 		rdma_for_each_port (cma_dev->device, port) {
629 			gidp = rdma_protocol_roce(cma_dev->device, port) ?
630 			       &iboe_gid : &gid;
631 			gid_type = cma_dev->default_gid_type[port - 1];
632 			sgid_attr = cma_validate_port(cma_dev->device, port,
633 						      gid_type, gidp, id_priv);
634 			if (!IS_ERR(sgid_attr)) {
635 				id_priv->id.port_num = port;
636 				cma_bind_sgid_attr(id_priv, sgid_attr);
637 				cma_attach_to_dev(id_priv, cma_dev);
638 				ret = 0;
639 				goto out;
640 			}
641 		}
642 	}
643 out:
644 	mutex_unlock(&lock);
645 	return ret;
646 }
647 
648 /**
649  * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
650  * @id_priv:		cm id to bind to cma device
651  * @listen_id_priv:	listener cm id to match against
652  * @req:		Pointer to req structure containaining incoming
653  *			request information
654  * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
655  * rdma device matches for listen_id and incoming request. It also verifies
656  * that a GID table entry is present for the source address.
657  * Returns 0 on success, or returns error code otherwise.
658  */
659 static int cma_ib_acquire_dev(struct rdma_id_private *id_priv,
660 			      const struct rdma_id_private *listen_id_priv,
661 			      struct cma_req_info *req)
662 {
663 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
664 	const struct ib_gid_attr *sgid_attr;
665 	enum ib_gid_type gid_type;
666 	union ib_gid gid;
667 
668 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
669 	    id_priv->id.ps == RDMA_PS_IPOIB)
670 		return -EINVAL;
671 
672 	if (rdma_protocol_roce(req->device, req->port))
673 		rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
674 			    &gid);
675 	else
676 		memcpy(&gid, dev_addr->src_dev_addr +
677 		       rdma_addr_gid_offset(dev_addr), sizeof(gid));
678 
679 	gid_type = listen_id_priv->cma_dev->default_gid_type[req->port - 1];
680 	sgid_attr = cma_validate_port(req->device, req->port,
681 				      gid_type, &gid, id_priv);
682 	if (IS_ERR(sgid_attr))
683 		return PTR_ERR(sgid_attr);
684 
685 	id_priv->id.port_num = req->port;
686 	cma_bind_sgid_attr(id_priv, sgid_attr);
687 	/* Need to acquire lock to protect against reader
688 	 * of cma_dev->id_list such as cma_netdev_callback() and
689 	 * cma_process_remove().
690 	 */
691 	mutex_lock(&lock);
692 	cma_attach_to_dev(id_priv, listen_id_priv->cma_dev);
693 	mutex_unlock(&lock);
694 	rdma_restrack_add(&id_priv->res);
695 	return 0;
696 }
697 
698 static int cma_iw_acquire_dev(struct rdma_id_private *id_priv,
699 			      const struct rdma_id_private *listen_id_priv)
700 {
701 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
702 	const struct ib_gid_attr *sgid_attr;
703 	struct cma_device *cma_dev;
704 	enum ib_gid_type gid_type;
705 	int ret = -ENODEV;
706 	union ib_gid gid;
707 	u32 port;
708 
709 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
710 	    id_priv->id.ps == RDMA_PS_IPOIB)
711 		return -EINVAL;
712 
713 	memcpy(&gid, dev_addr->src_dev_addr +
714 	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
715 
716 	mutex_lock(&lock);
717 
718 	cma_dev = listen_id_priv->cma_dev;
719 	port = listen_id_priv->id.port_num;
720 	gid_type = listen_id_priv->gid_type;
721 	sgid_attr = cma_validate_port(cma_dev->device, port,
722 				      gid_type, &gid, id_priv);
723 	if (!IS_ERR(sgid_attr)) {
724 		id_priv->id.port_num = port;
725 		cma_bind_sgid_attr(id_priv, sgid_attr);
726 		ret = 0;
727 		goto out;
728 	}
729 
730 	list_for_each_entry(cma_dev, &dev_list, list) {
731 		rdma_for_each_port (cma_dev->device, port) {
732 			if (listen_id_priv->cma_dev == cma_dev &&
733 			    listen_id_priv->id.port_num == port)
734 				continue;
735 
736 			gid_type = cma_dev->default_gid_type[port - 1];
737 			sgid_attr = cma_validate_port(cma_dev->device, port,
738 						      gid_type, &gid, id_priv);
739 			if (!IS_ERR(sgid_attr)) {
740 				id_priv->id.port_num = port;
741 				cma_bind_sgid_attr(id_priv, sgid_attr);
742 				ret = 0;
743 				goto out;
744 			}
745 		}
746 	}
747 
748 out:
749 	if (!ret) {
750 		cma_attach_to_dev(id_priv, cma_dev);
751 		rdma_restrack_add(&id_priv->res);
752 	}
753 
754 	mutex_unlock(&lock);
755 	return ret;
756 }
757 
758 /*
759  * Select the source IB device and address to reach the destination IB address.
760  */
761 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
762 {
763 	struct cma_device *cma_dev, *cur_dev;
764 	struct sockaddr_ib *addr;
765 	union ib_gid gid, sgid, *dgid;
766 	unsigned int p;
767 	u16 pkey, index;
768 	enum ib_port_state port_state;
769 	int i;
770 
771 	cma_dev = NULL;
772 	addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
773 	dgid = (union ib_gid *) &addr->sib_addr;
774 	pkey = ntohs(addr->sib_pkey);
775 
776 	mutex_lock(&lock);
777 	list_for_each_entry(cur_dev, &dev_list, list) {
778 		rdma_for_each_port (cur_dev->device, p) {
779 			if (!rdma_cap_af_ib(cur_dev->device, p))
780 				continue;
781 
782 			if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
783 				continue;
784 
785 			if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
786 				continue;
787 			for (i = 0; !rdma_query_gid(cur_dev->device,
788 						    p, i, &gid);
789 			     i++) {
790 				if (!memcmp(&gid, dgid, sizeof(gid))) {
791 					cma_dev = cur_dev;
792 					sgid = gid;
793 					id_priv->id.port_num = p;
794 					goto found;
795 				}
796 
797 				if (!cma_dev && (gid.global.subnet_prefix ==
798 				    dgid->global.subnet_prefix) &&
799 				    port_state == IB_PORT_ACTIVE) {
800 					cma_dev = cur_dev;
801 					sgid = gid;
802 					id_priv->id.port_num = p;
803 					goto found;
804 				}
805 			}
806 		}
807 	}
808 	mutex_unlock(&lock);
809 	return -ENODEV;
810 
811 found:
812 	cma_attach_to_dev(id_priv, cma_dev);
813 	rdma_restrack_add(&id_priv->res);
814 	mutex_unlock(&lock);
815 	addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
816 	memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
817 	cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
818 	return 0;
819 }
820 
821 static void cma_id_get(struct rdma_id_private *id_priv)
822 {
823 	refcount_inc(&id_priv->refcount);
824 }
825 
826 static void cma_id_put(struct rdma_id_private *id_priv)
827 {
828 	if (refcount_dec_and_test(&id_priv->refcount))
829 		complete(&id_priv->comp);
830 }
831 
832 static struct rdma_id_private *
833 __rdma_create_id(struct net *net, rdma_cm_event_handler event_handler,
834 		 void *context, enum rdma_ucm_port_space ps,
835 		 enum ib_qp_type qp_type, const struct rdma_id_private *parent)
836 {
837 	struct rdma_id_private *id_priv;
838 
839 	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
840 	if (!id_priv)
841 		return ERR_PTR(-ENOMEM);
842 
843 	id_priv->state = RDMA_CM_IDLE;
844 	id_priv->id.context = context;
845 	id_priv->id.event_handler = event_handler;
846 	id_priv->id.ps = ps;
847 	id_priv->id.qp_type = qp_type;
848 	id_priv->tos_set = false;
849 	id_priv->timeout_set = false;
850 	id_priv->min_rnr_timer_set = false;
851 	id_priv->gid_type = IB_GID_TYPE_IB;
852 	spin_lock_init(&id_priv->lock);
853 	mutex_init(&id_priv->qp_mutex);
854 	init_completion(&id_priv->comp);
855 	refcount_set(&id_priv->refcount, 1);
856 	mutex_init(&id_priv->handler_mutex);
857 	INIT_LIST_HEAD(&id_priv->listen_list);
858 	INIT_LIST_HEAD(&id_priv->mc_list);
859 	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
860 	id_priv->id.route.addr.dev_addr.net = get_net(net);
861 	id_priv->seq_num &= 0x00ffffff;
862 
863 	rdma_restrack_new(&id_priv->res, RDMA_RESTRACK_CM_ID);
864 	if (parent)
865 		rdma_restrack_parent_name(&id_priv->res, &parent->res);
866 
867 	return id_priv;
868 }
869 
870 struct rdma_cm_id *
871 __rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,
872 			void *context, enum rdma_ucm_port_space ps,
873 			enum ib_qp_type qp_type, const char *caller)
874 {
875 	struct rdma_id_private *ret;
876 
877 	ret = __rdma_create_id(net, event_handler, context, ps, qp_type, NULL);
878 	if (IS_ERR(ret))
879 		return ERR_CAST(ret);
880 
881 	rdma_restrack_set_name(&ret->res, caller);
882 	return &ret->id;
883 }
884 EXPORT_SYMBOL(__rdma_create_kernel_id);
885 
886 struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,
887 				       void *context,
888 				       enum rdma_ucm_port_space ps,
889 				       enum ib_qp_type qp_type)
890 {
891 	struct rdma_id_private *ret;
892 
893 	ret = __rdma_create_id(current->nsproxy->net_ns, event_handler, context,
894 			       ps, qp_type, NULL);
895 	if (IS_ERR(ret))
896 		return ERR_CAST(ret);
897 
898 	rdma_restrack_set_name(&ret->res, NULL);
899 	return &ret->id;
900 }
901 EXPORT_SYMBOL(rdma_create_user_id);
902 
903 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
904 {
905 	struct ib_qp_attr qp_attr;
906 	int qp_attr_mask, ret;
907 
908 	qp_attr.qp_state = IB_QPS_INIT;
909 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
910 	if (ret)
911 		return ret;
912 
913 	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
914 	if (ret)
915 		return ret;
916 
917 	qp_attr.qp_state = IB_QPS_RTR;
918 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
919 	if (ret)
920 		return ret;
921 
922 	qp_attr.qp_state = IB_QPS_RTS;
923 	qp_attr.sq_psn = 0;
924 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
925 
926 	return ret;
927 }
928 
929 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
930 		   struct ib_qp_init_attr *qp_init_attr)
931 {
932 	struct rdma_id_private *id_priv;
933 	struct ib_qp *qp;
934 	int ret = 0;
935 
936 	id_priv = container_of(id, struct rdma_id_private, id);
937 	if (id->device != pd->device) {
938 		ret = -EINVAL;
939 		goto out_err;
940 	}
941 
942 	qp_init_attr->port_num = id->port_num;
943 	qp = ib_create_qp(pd, qp_init_attr);
944 	if (IS_ERR(qp)) {
945 		ret = PTR_ERR(qp);
946 		goto out_err;
947 	}
948 
949 	if (id->qp_type == IB_QPT_UD)
950 		ret = cma_init_ud_qp(id_priv, qp);
951 	if (ret)
952 		goto out_destroy;
953 
954 	id->qp = qp;
955 	id_priv->qp_num = qp->qp_num;
956 	id_priv->srq = (qp->srq != NULL);
957 	trace_cm_qp_create(id_priv, pd, qp_init_attr, 0);
958 	return 0;
959 out_destroy:
960 	ib_destroy_qp(qp);
961 out_err:
962 	trace_cm_qp_create(id_priv, pd, qp_init_attr, ret);
963 	return ret;
964 }
965 EXPORT_SYMBOL(rdma_create_qp);
966 
967 void rdma_destroy_qp(struct rdma_cm_id *id)
968 {
969 	struct rdma_id_private *id_priv;
970 
971 	id_priv = container_of(id, struct rdma_id_private, id);
972 	trace_cm_qp_destroy(id_priv);
973 	mutex_lock(&id_priv->qp_mutex);
974 	ib_destroy_qp(id_priv->id.qp);
975 	id_priv->id.qp = NULL;
976 	mutex_unlock(&id_priv->qp_mutex);
977 }
978 EXPORT_SYMBOL(rdma_destroy_qp);
979 
980 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
981 			     struct rdma_conn_param *conn_param)
982 {
983 	struct ib_qp_attr qp_attr;
984 	int qp_attr_mask, ret;
985 
986 	mutex_lock(&id_priv->qp_mutex);
987 	if (!id_priv->id.qp) {
988 		ret = 0;
989 		goto out;
990 	}
991 
992 	/* Need to update QP attributes from default values. */
993 	qp_attr.qp_state = IB_QPS_INIT;
994 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
995 	if (ret)
996 		goto out;
997 
998 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
999 	if (ret)
1000 		goto out;
1001 
1002 	qp_attr.qp_state = IB_QPS_RTR;
1003 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1004 	if (ret)
1005 		goto out;
1006 
1007 	BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
1008 
1009 	if (conn_param)
1010 		qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
1011 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1012 out:
1013 	mutex_unlock(&id_priv->qp_mutex);
1014 	return ret;
1015 }
1016 
1017 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
1018 			     struct rdma_conn_param *conn_param)
1019 {
1020 	struct ib_qp_attr qp_attr;
1021 	int qp_attr_mask, ret;
1022 
1023 	mutex_lock(&id_priv->qp_mutex);
1024 	if (!id_priv->id.qp) {
1025 		ret = 0;
1026 		goto out;
1027 	}
1028 
1029 	qp_attr.qp_state = IB_QPS_RTS;
1030 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1031 	if (ret)
1032 		goto out;
1033 
1034 	if (conn_param)
1035 		qp_attr.max_rd_atomic = conn_param->initiator_depth;
1036 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1037 out:
1038 	mutex_unlock(&id_priv->qp_mutex);
1039 	return ret;
1040 }
1041 
1042 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
1043 {
1044 	struct ib_qp_attr qp_attr;
1045 	int ret;
1046 
1047 	mutex_lock(&id_priv->qp_mutex);
1048 	if (!id_priv->id.qp) {
1049 		ret = 0;
1050 		goto out;
1051 	}
1052 
1053 	qp_attr.qp_state = IB_QPS_ERR;
1054 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
1055 out:
1056 	mutex_unlock(&id_priv->qp_mutex);
1057 	return ret;
1058 }
1059 
1060 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
1061 			       struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1062 {
1063 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1064 	int ret;
1065 	u16 pkey;
1066 
1067 	if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
1068 		pkey = 0xffff;
1069 	else
1070 		pkey = ib_addr_get_pkey(dev_addr);
1071 
1072 	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
1073 				  pkey, &qp_attr->pkey_index);
1074 	if (ret)
1075 		return ret;
1076 
1077 	qp_attr->port_num = id_priv->id.port_num;
1078 	*qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1079 
1080 	if (id_priv->id.qp_type == IB_QPT_UD) {
1081 		ret = cma_set_qkey(id_priv, 0);
1082 		if (ret)
1083 			return ret;
1084 
1085 		qp_attr->qkey = id_priv->qkey;
1086 		*qp_attr_mask |= IB_QP_QKEY;
1087 	} else {
1088 		qp_attr->qp_access_flags = 0;
1089 		*qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1090 	}
1091 	return 0;
1092 }
1093 
1094 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1095 		       int *qp_attr_mask)
1096 {
1097 	struct rdma_id_private *id_priv;
1098 	int ret = 0;
1099 
1100 	id_priv = container_of(id, struct rdma_id_private, id);
1101 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
1102 		if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1103 			ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1104 		else
1105 			ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1106 						 qp_attr_mask);
1107 
1108 		if (qp_attr->qp_state == IB_QPS_RTR)
1109 			qp_attr->rq_psn = id_priv->seq_num;
1110 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1111 		if (!id_priv->cm_id.iw) {
1112 			qp_attr->qp_access_flags = 0;
1113 			*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1114 		} else
1115 			ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1116 						 qp_attr_mask);
1117 		qp_attr->port_num = id_priv->id.port_num;
1118 		*qp_attr_mask |= IB_QP_PORT;
1119 	} else {
1120 		ret = -ENOSYS;
1121 	}
1122 
1123 	if ((*qp_attr_mask & IB_QP_TIMEOUT) && id_priv->timeout_set)
1124 		qp_attr->timeout = id_priv->timeout;
1125 
1126 	if ((*qp_attr_mask & IB_QP_MIN_RNR_TIMER) && id_priv->min_rnr_timer_set)
1127 		qp_attr->min_rnr_timer = id_priv->min_rnr_timer;
1128 
1129 	return ret;
1130 }
1131 EXPORT_SYMBOL(rdma_init_qp_attr);
1132 
1133 static inline bool cma_zero_addr(const struct sockaddr *addr)
1134 {
1135 	switch (addr->sa_family) {
1136 	case AF_INET:
1137 		return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1138 	case AF_INET6:
1139 		return ipv6_addr_any(&((struct sockaddr_in6 *)addr)->sin6_addr);
1140 	case AF_IB:
1141 		return ib_addr_any(&((struct sockaddr_ib *)addr)->sib_addr);
1142 	default:
1143 		return false;
1144 	}
1145 }
1146 
1147 static inline bool cma_loopback_addr(const struct sockaddr *addr)
1148 {
1149 	switch (addr->sa_family) {
1150 	case AF_INET:
1151 		return ipv4_is_loopback(
1152 			((struct sockaddr_in *)addr)->sin_addr.s_addr);
1153 	case AF_INET6:
1154 		return ipv6_addr_loopback(
1155 			&((struct sockaddr_in6 *)addr)->sin6_addr);
1156 	case AF_IB:
1157 		return ib_addr_loopback(
1158 			&((struct sockaddr_ib *)addr)->sib_addr);
1159 	default:
1160 		return false;
1161 	}
1162 }
1163 
1164 static inline bool cma_any_addr(const struct sockaddr *addr)
1165 {
1166 	return cma_zero_addr(addr) || cma_loopback_addr(addr);
1167 }
1168 
1169 static int cma_addr_cmp(const struct sockaddr *src, const struct sockaddr *dst)
1170 {
1171 	if (src->sa_family != dst->sa_family)
1172 		return -1;
1173 
1174 	switch (src->sa_family) {
1175 	case AF_INET:
1176 		return ((struct sockaddr_in *)src)->sin_addr.s_addr !=
1177 		       ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1178 	case AF_INET6: {
1179 		struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src;
1180 		struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst;
1181 		bool link_local;
1182 
1183 		if (ipv6_addr_cmp(&src_addr6->sin6_addr,
1184 					  &dst_addr6->sin6_addr))
1185 			return 1;
1186 		link_local = ipv6_addr_type(&dst_addr6->sin6_addr) &
1187 			     IPV6_ADDR_LINKLOCAL;
1188 		/* Link local must match their scope_ids */
1189 		return link_local ? (src_addr6->sin6_scope_id !=
1190 				     dst_addr6->sin6_scope_id) :
1191 				    0;
1192 	}
1193 
1194 	default:
1195 		return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1196 				   &((struct sockaddr_ib *) dst)->sib_addr);
1197 	}
1198 }
1199 
1200 static __be16 cma_port(const struct sockaddr *addr)
1201 {
1202 	struct sockaddr_ib *sib;
1203 
1204 	switch (addr->sa_family) {
1205 	case AF_INET:
1206 		return ((struct sockaddr_in *) addr)->sin_port;
1207 	case AF_INET6:
1208 		return ((struct sockaddr_in6 *) addr)->sin6_port;
1209 	case AF_IB:
1210 		sib = (struct sockaddr_ib *) addr;
1211 		return htons((u16) (be64_to_cpu(sib->sib_sid) &
1212 				    be64_to_cpu(sib->sib_sid_mask)));
1213 	default:
1214 		return 0;
1215 	}
1216 }
1217 
1218 static inline int cma_any_port(const struct sockaddr *addr)
1219 {
1220 	return !cma_port(addr);
1221 }
1222 
1223 static void cma_save_ib_info(struct sockaddr *src_addr,
1224 			     struct sockaddr *dst_addr,
1225 			     const struct rdma_cm_id *listen_id,
1226 			     const struct sa_path_rec *path)
1227 {
1228 	struct sockaddr_ib *listen_ib, *ib;
1229 
1230 	listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1231 	if (src_addr) {
1232 		ib = (struct sockaddr_ib *)src_addr;
1233 		ib->sib_family = AF_IB;
1234 		if (path) {
1235 			ib->sib_pkey = path->pkey;
1236 			ib->sib_flowinfo = path->flow_label;
1237 			memcpy(&ib->sib_addr, &path->sgid, 16);
1238 			ib->sib_sid = path->service_id;
1239 			ib->sib_scope_id = 0;
1240 		} else {
1241 			ib->sib_pkey = listen_ib->sib_pkey;
1242 			ib->sib_flowinfo = listen_ib->sib_flowinfo;
1243 			ib->sib_addr = listen_ib->sib_addr;
1244 			ib->sib_sid = listen_ib->sib_sid;
1245 			ib->sib_scope_id = listen_ib->sib_scope_id;
1246 		}
1247 		ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1248 	}
1249 	if (dst_addr) {
1250 		ib = (struct sockaddr_ib *)dst_addr;
1251 		ib->sib_family = AF_IB;
1252 		if (path) {
1253 			ib->sib_pkey = path->pkey;
1254 			ib->sib_flowinfo = path->flow_label;
1255 			memcpy(&ib->sib_addr, &path->dgid, 16);
1256 		}
1257 	}
1258 }
1259 
1260 static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1261 			      struct sockaddr_in *dst_addr,
1262 			      struct cma_hdr *hdr,
1263 			      __be16 local_port)
1264 {
1265 	if (src_addr) {
1266 		*src_addr = (struct sockaddr_in) {
1267 			.sin_family = AF_INET,
1268 			.sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1269 			.sin_port = local_port,
1270 		};
1271 	}
1272 
1273 	if (dst_addr) {
1274 		*dst_addr = (struct sockaddr_in) {
1275 			.sin_family = AF_INET,
1276 			.sin_addr.s_addr = hdr->src_addr.ip4.addr,
1277 			.sin_port = hdr->port,
1278 		};
1279 	}
1280 }
1281 
1282 static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1283 			      struct sockaddr_in6 *dst_addr,
1284 			      struct cma_hdr *hdr,
1285 			      __be16 local_port)
1286 {
1287 	if (src_addr) {
1288 		*src_addr = (struct sockaddr_in6) {
1289 			.sin6_family = AF_INET6,
1290 			.sin6_addr = hdr->dst_addr.ip6,
1291 			.sin6_port = local_port,
1292 		};
1293 	}
1294 
1295 	if (dst_addr) {
1296 		*dst_addr = (struct sockaddr_in6) {
1297 			.sin6_family = AF_INET6,
1298 			.sin6_addr = hdr->src_addr.ip6,
1299 			.sin6_port = hdr->port,
1300 		};
1301 	}
1302 }
1303 
1304 static u16 cma_port_from_service_id(__be64 service_id)
1305 {
1306 	return (u16)be64_to_cpu(service_id);
1307 }
1308 
1309 static int cma_save_ip_info(struct sockaddr *src_addr,
1310 			    struct sockaddr *dst_addr,
1311 			    const struct ib_cm_event *ib_event,
1312 			    __be64 service_id)
1313 {
1314 	struct cma_hdr *hdr;
1315 	__be16 port;
1316 
1317 	hdr = ib_event->private_data;
1318 	if (hdr->cma_version != CMA_VERSION)
1319 		return -EINVAL;
1320 
1321 	port = htons(cma_port_from_service_id(service_id));
1322 
1323 	switch (cma_get_ip_ver(hdr)) {
1324 	case 4:
1325 		cma_save_ip4_info((struct sockaddr_in *)src_addr,
1326 				  (struct sockaddr_in *)dst_addr, hdr, port);
1327 		break;
1328 	case 6:
1329 		cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1330 				  (struct sockaddr_in6 *)dst_addr, hdr, port);
1331 		break;
1332 	default:
1333 		return -EAFNOSUPPORT;
1334 	}
1335 
1336 	return 0;
1337 }
1338 
1339 static int cma_save_net_info(struct sockaddr *src_addr,
1340 			     struct sockaddr *dst_addr,
1341 			     const struct rdma_cm_id *listen_id,
1342 			     const struct ib_cm_event *ib_event,
1343 			     sa_family_t sa_family, __be64 service_id)
1344 {
1345 	if (sa_family == AF_IB) {
1346 		if (ib_event->event == IB_CM_REQ_RECEIVED)
1347 			cma_save_ib_info(src_addr, dst_addr, listen_id,
1348 					 ib_event->param.req_rcvd.primary_path);
1349 		else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1350 			cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1351 		return 0;
1352 	}
1353 
1354 	return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1355 }
1356 
1357 static int cma_save_req_info(const struct ib_cm_event *ib_event,
1358 			     struct cma_req_info *req)
1359 {
1360 	const struct ib_cm_req_event_param *req_param =
1361 		&ib_event->param.req_rcvd;
1362 	const struct ib_cm_sidr_req_event_param *sidr_param =
1363 		&ib_event->param.sidr_req_rcvd;
1364 
1365 	switch (ib_event->event) {
1366 	case IB_CM_REQ_RECEIVED:
1367 		req->device	= req_param->listen_id->device;
1368 		req->port	= req_param->port;
1369 		memcpy(&req->local_gid, &req_param->primary_path->sgid,
1370 		       sizeof(req->local_gid));
1371 		req->has_gid	= true;
1372 		req->service_id = req_param->primary_path->service_id;
1373 		req->pkey	= be16_to_cpu(req_param->primary_path->pkey);
1374 		if (req->pkey != req_param->bth_pkey)
1375 			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1376 					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1377 					    req_param->bth_pkey, req->pkey);
1378 		break;
1379 	case IB_CM_SIDR_REQ_RECEIVED:
1380 		req->device	= sidr_param->listen_id->device;
1381 		req->port	= sidr_param->port;
1382 		req->has_gid	= false;
1383 		req->service_id	= sidr_param->service_id;
1384 		req->pkey	= sidr_param->pkey;
1385 		if (req->pkey != sidr_param->bth_pkey)
1386 			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1387 					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1388 					    sidr_param->bth_pkey, req->pkey);
1389 		break;
1390 	default:
1391 		return -EINVAL;
1392 	}
1393 
1394 	return 0;
1395 }
1396 
1397 static bool validate_ipv4_net_dev(struct net_device *net_dev,
1398 				  const struct sockaddr_in *dst_addr,
1399 				  const struct sockaddr_in *src_addr)
1400 {
1401 	__be32 daddr = dst_addr->sin_addr.s_addr,
1402 	       saddr = src_addr->sin_addr.s_addr;
1403 	struct fib_result res;
1404 	struct flowi4 fl4;
1405 	int err;
1406 	bool ret;
1407 
1408 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1409 	    ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1410 	    ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1411 	    ipv4_is_loopback(saddr))
1412 		return false;
1413 
1414 	memset(&fl4, 0, sizeof(fl4));
1415 	fl4.flowi4_iif = net_dev->ifindex;
1416 	fl4.daddr = daddr;
1417 	fl4.saddr = saddr;
1418 
1419 	rcu_read_lock();
1420 	err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1421 	ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1422 	rcu_read_unlock();
1423 
1424 	return ret;
1425 }
1426 
1427 static bool validate_ipv6_net_dev(struct net_device *net_dev,
1428 				  const struct sockaddr_in6 *dst_addr,
1429 				  const struct sockaddr_in6 *src_addr)
1430 {
1431 #if IS_ENABLED(CONFIG_IPV6)
1432 	const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1433 			   IPV6_ADDR_LINKLOCAL;
1434 	struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1435 					 &src_addr->sin6_addr, net_dev->ifindex,
1436 					 NULL, strict);
1437 	bool ret;
1438 
1439 	if (!rt)
1440 		return false;
1441 
1442 	ret = rt->rt6i_idev->dev == net_dev;
1443 	ip6_rt_put(rt);
1444 
1445 	return ret;
1446 #else
1447 	return false;
1448 #endif
1449 }
1450 
1451 static bool validate_net_dev(struct net_device *net_dev,
1452 			     const struct sockaddr *daddr,
1453 			     const struct sockaddr *saddr)
1454 {
1455 	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1456 	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1457 	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1458 	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1459 
1460 	switch (daddr->sa_family) {
1461 	case AF_INET:
1462 		return saddr->sa_family == AF_INET &&
1463 		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1464 
1465 	case AF_INET6:
1466 		return saddr->sa_family == AF_INET6 &&
1467 		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1468 
1469 	default:
1470 		return false;
1471 	}
1472 }
1473 
1474 static struct net_device *
1475 roce_get_net_dev_by_cm_event(const struct ib_cm_event *ib_event)
1476 {
1477 	const struct ib_gid_attr *sgid_attr = NULL;
1478 	struct net_device *ndev;
1479 
1480 	if (ib_event->event == IB_CM_REQ_RECEIVED)
1481 		sgid_attr = ib_event->param.req_rcvd.ppath_sgid_attr;
1482 	else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1483 		sgid_attr = ib_event->param.sidr_req_rcvd.sgid_attr;
1484 
1485 	if (!sgid_attr)
1486 		return NULL;
1487 
1488 	rcu_read_lock();
1489 	ndev = rdma_read_gid_attr_ndev_rcu(sgid_attr);
1490 	if (IS_ERR(ndev))
1491 		ndev = NULL;
1492 	else
1493 		dev_hold(ndev);
1494 	rcu_read_unlock();
1495 	return ndev;
1496 }
1497 
1498 static struct net_device *cma_get_net_dev(const struct ib_cm_event *ib_event,
1499 					  struct cma_req_info *req)
1500 {
1501 	struct sockaddr *listen_addr =
1502 			(struct sockaddr *)&req->listen_addr_storage;
1503 	struct sockaddr *src_addr = (struct sockaddr *)&req->src_addr_storage;
1504 	struct net_device *net_dev;
1505 	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1506 	int err;
1507 
1508 	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1509 			       req->service_id);
1510 	if (err)
1511 		return ERR_PTR(err);
1512 
1513 	if (rdma_protocol_roce(req->device, req->port))
1514 		net_dev = roce_get_net_dev_by_cm_event(ib_event);
1515 	else
1516 		net_dev = ib_get_net_dev_by_params(req->device, req->port,
1517 						   req->pkey,
1518 						   gid, listen_addr);
1519 	if (!net_dev)
1520 		return ERR_PTR(-ENODEV);
1521 
1522 	return net_dev;
1523 }
1524 
1525 static enum rdma_ucm_port_space rdma_ps_from_service_id(__be64 service_id)
1526 {
1527 	return (be64_to_cpu(service_id) >> 16) & 0xffff;
1528 }
1529 
1530 static bool cma_match_private_data(struct rdma_id_private *id_priv,
1531 				   const struct cma_hdr *hdr)
1532 {
1533 	struct sockaddr *addr = cma_src_addr(id_priv);
1534 	__be32 ip4_addr;
1535 	struct in6_addr ip6_addr;
1536 
1537 	if (cma_any_addr(addr) && !id_priv->afonly)
1538 		return true;
1539 
1540 	switch (addr->sa_family) {
1541 	case AF_INET:
1542 		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1543 		if (cma_get_ip_ver(hdr) != 4)
1544 			return false;
1545 		if (!cma_any_addr(addr) &&
1546 		    hdr->dst_addr.ip4.addr != ip4_addr)
1547 			return false;
1548 		break;
1549 	case AF_INET6:
1550 		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1551 		if (cma_get_ip_ver(hdr) != 6)
1552 			return false;
1553 		if (!cma_any_addr(addr) &&
1554 		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1555 			return false;
1556 		break;
1557 	case AF_IB:
1558 		return true;
1559 	default:
1560 		return false;
1561 	}
1562 
1563 	return true;
1564 }
1565 
1566 static bool cma_protocol_roce(const struct rdma_cm_id *id)
1567 {
1568 	struct ib_device *device = id->device;
1569 	const u32 port_num = id->port_num ?: rdma_start_port(device);
1570 
1571 	return rdma_protocol_roce(device, port_num);
1572 }
1573 
1574 static bool cma_is_req_ipv6_ll(const struct cma_req_info *req)
1575 {
1576 	const struct sockaddr *daddr =
1577 			(const struct sockaddr *)&req->listen_addr_storage;
1578 	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1579 
1580 	/* Returns true if the req is for IPv6 link local */
1581 	return (daddr->sa_family == AF_INET6 &&
1582 		(ipv6_addr_type(&daddr6->sin6_addr) & IPV6_ADDR_LINKLOCAL));
1583 }
1584 
1585 static bool cma_match_net_dev(const struct rdma_cm_id *id,
1586 			      const struct net_device *net_dev,
1587 			      const struct cma_req_info *req)
1588 {
1589 	const struct rdma_addr *addr = &id->route.addr;
1590 
1591 	if (!net_dev)
1592 		/* This request is an AF_IB request */
1593 		return (!id->port_num || id->port_num == req->port) &&
1594 		       (addr->src_addr.ss_family == AF_IB);
1595 
1596 	/*
1597 	 * If the request is not for IPv6 link local, allow matching
1598 	 * request to any netdevice of the one or multiport rdma device.
1599 	 */
1600 	if (!cma_is_req_ipv6_ll(req))
1601 		return true;
1602 	/*
1603 	 * Net namespaces must match, and if the listner is listening
1604 	 * on a specific netdevice than netdevice must match as well.
1605 	 */
1606 	if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1607 	    (!!addr->dev_addr.bound_dev_if ==
1608 	     (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1609 		return true;
1610 	else
1611 		return false;
1612 }
1613 
1614 static struct rdma_id_private *cma_find_listener(
1615 		const struct rdma_bind_list *bind_list,
1616 		const struct ib_cm_id *cm_id,
1617 		const struct ib_cm_event *ib_event,
1618 		const struct cma_req_info *req,
1619 		const struct net_device *net_dev)
1620 {
1621 	struct rdma_id_private *id_priv, *id_priv_dev;
1622 
1623 	lockdep_assert_held(&lock);
1624 
1625 	if (!bind_list)
1626 		return ERR_PTR(-EINVAL);
1627 
1628 	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1629 		if (cma_match_private_data(id_priv, ib_event->private_data)) {
1630 			if (id_priv->id.device == cm_id->device &&
1631 			    cma_match_net_dev(&id_priv->id, net_dev, req))
1632 				return id_priv;
1633 			list_for_each_entry(id_priv_dev,
1634 					    &id_priv->listen_list,
1635 					    listen_list) {
1636 				if (id_priv_dev->id.device == cm_id->device &&
1637 				    cma_match_net_dev(&id_priv_dev->id,
1638 						      net_dev, req))
1639 					return id_priv_dev;
1640 			}
1641 		}
1642 	}
1643 
1644 	return ERR_PTR(-EINVAL);
1645 }
1646 
1647 static struct rdma_id_private *
1648 cma_ib_id_from_event(struct ib_cm_id *cm_id,
1649 		     const struct ib_cm_event *ib_event,
1650 		     struct cma_req_info *req,
1651 		     struct net_device **net_dev)
1652 {
1653 	struct rdma_bind_list *bind_list;
1654 	struct rdma_id_private *id_priv;
1655 	int err;
1656 
1657 	err = cma_save_req_info(ib_event, req);
1658 	if (err)
1659 		return ERR_PTR(err);
1660 
1661 	*net_dev = cma_get_net_dev(ib_event, req);
1662 	if (IS_ERR(*net_dev)) {
1663 		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1664 			/* Assuming the protocol is AF_IB */
1665 			*net_dev = NULL;
1666 		} else {
1667 			return ERR_CAST(*net_dev);
1668 		}
1669 	}
1670 
1671 	mutex_lock(&lock);
1672 	/*
1673 	 * Net namespace might be getting deleted while route lookup,
1674 	 * cm_id lookup is in progress. Therefore, perform netdevice
1675 	 * validation, cm_id lookup under rcu lock.
1676 	 * RCU lock along with netdevice state check, synchronizes with
1677 	 * netdevice migrating to different net namespace and also avoids
1678 	 * case where net namespace doesn't get deleted while lookup is in
1679 	 * progress.
1680 	 * If the device state is not IFF_UP, its properties such as ifindex
1681 	 * and nd_net cannot be trusted to remain valid without rcu lock.
1682 	 * net/core/dev.c change_net_namespace() ensures to synchronize with
1683 	 * ongoing operations on net device after device is closed using
1684 	 * synchronize_net().
1685 	 */
1686 	rcu_read_lock();
1687 	if (*net_dev) {
1688 		/*
1689 		 * If netdevice is down, it is likely that it is administratively
1690 		 * down or it might be migrating to different namespace.
1691 		 * In that case avoid further processing, as the net namespace
1692 		 * or ifindex may change.
1693 		 */
1694 		if (((*net_dev)->flags & IFF_UP) == 0) {
1695 			id_priv = ERR_PTR(-EHOSTUNREACH);
1696 			goto err;
1697 		}
1698 
1699 		if (!validate_net_dev(*net_dev,
1700 				 (struct sockaddr *)&req->listen_addr_storage,
1701 				 (struct sockaddr *)&req->src_addr_storage)) {
1702 			id_priv = ERR_PTR(-EHOSTUNREACH);
1703 			goto err;
1704 		}
1705 	}
1706 
1707 	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1708 				rdma_ps_from_service_id(req->service_id),
1709 				cma_port_from_service_id(req->service_id));
1710 	id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev);
1711 err:
1712 	rcu_read_unlock();
1713 	mutex_unlock(&lock);
1714 	if (IS_ERR(id_priv) && *net_dev) {
1715 		dev_put(*net_dev);
1716 		*net_dev = NULL;
1717 	}
1718 	return id_priv;
1719 }
1720 
1721 static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1722 {
1723 	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1724 }
1725 
1726 static void cma_cancel_route(struct rdma_id_private *id_priv)
1727 {
1728 	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1729 		if (id_priv->query)
1730 			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1731 	}
1732 }
1733 
1734 static void cma_cancel_listens(struct rdma_id_private *id_priv)
1735 {
1736 	struct rdma_id_private *dev_id_priv;
1737 
1738 	/*
1739 	 * Remove from listen_any_list to prevent added devices from spawning
1740 	 * additional listen requests.
1741 	 */
1742 	mutex_lock(&lock);
1743 	list_del(&id_priv->list);
1744 
1745 	while (!list_empty(&id_priv->listen_list)) {
1746 		dev_id_priv = list_entry(id_priv->listen_list.next,
1747 					 struct rdma_id_private, listen_list);
1748 		/* sync with device removal to avoid duplicate destruction */
1749 		list_del_init(&dev_id_priv->list);
1750 		list_del(&dev_id_priv->listen_list);
1751 		mutex_unlock(&lock);
1752 
1753 		rdma_destroy_id(&dev_id_priv->id);
1754 		mutex_lock(&lock);
1755 	}
1756 	mutex_unlock(&lock);
1757 }
1758 
1759 static void cma_cancel_operation(struct rdma_id_private *id_priv,
1760 				 enum rdma_cm_state state)
1761 {
1762 	switch (state) {
1763 	case RDMA_CM_ADDR_QUERY:
1764 		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1765 		break;
1766 	case RDMA_CM_ROUTE_QUERY:
1767 		cma_cancel_route(id_priv);
1768 		break;
1769 	case RDMA_CM_LISTEN:
1770 		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1771 			cma_cancel_listens(id_priv);
1772 		break;
1773 	default:
1774 		break;
1775 	}
1776 }
1777 
1778 static void cma_release_port(struct rdma_id_private *id_priv)
1779 {
1780 	struct rdma_bind_list *bind_list = id_priv->bind_list;
1781 	struct net *net = id_priv->id.route.addr.dev_addr.net;
1782 
1783 	if (!bind_list)
1784 		return;
1785 
1786 	mutex_lock(&lock);
1787 	hlist_del(&id_priv->node);
1788 	if (hlist_empty(&bind_list->owners)) {
1789 		cma_ps_remove(net, bind_list->ps, bind_list->port);
1790 		kfree(bind_list);
1791 	}
1792 	mutex_unlock(&lock);
1793 }
1794 
1795 static void destroy_mc(struct rdma_id_private *id_priv,
1796 		       struct cma_multicast *mc)
1797 {
1798 	if (rdma_cap_ib_mcast(id_priv->id.device, id_priv->id.port_num))
1799 		ib_sa_free_multicast(mc->sa_mc);
1800 
1801 	if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
1802 		struct rdma_dev_addr *dev_addr =
1803 			&id_priv->id.route.addr.dev_addr;
1804 		struct net_device *ndev = NULL;
1805 
1806 		if (dev_addr->bound_dev_if)
1807 			ndev = dev_get_by_index(dev_addr->net,
1808 						dev_addr->bound_dev_if);
1809 		if (ndev) {
1810 			union ib_gid mgid;
1811 
1812 			cma_set_mgid(id_priv, (struct sockaddr *)&mc->addr,
1813 				     &mgid);
1814 			cma_igmp_send(ndev, &mgid, false);
1815 			dev_put(ndev);
1816 		}
1817 
1818 		cancel_work_sync(&mc->iboe_join.work);
1819 	}
1820 	kfree(mc);
1821 }
1822 
1823 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1824 {
1825 	struct cma_multicast *mc;
1826 
1827 	while (!list_empty(&id_priv->mc_list)) {
1828 		mc = list_first_entry(&id_priv->mc_list, struct cma_multicast,
1829 				      list);
1830 		list_del(&mc->list);
1831 		destroy_mc(id_priv, mc);
1832 	}
1833 }
1834 
1835 static void _destroy_id(struct rdma_id_private *id_priv,
1836 			enum rdma_cm_state state)
1837 {
1838 	cma_cancel_operation(id_priv, state);
1839 
1840 	rdma_restrack_del(&id_priv->res);
1841 	if (id_priv->cma_dev) {
1842 		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1843 			if (id_priv->cm_id.ib)
1844 				ib_destroy_cm_id(id_priv->cm_id.ib);
1845 		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1846 			if (id_priv->cm_id.iw)
1847 				iw_destroy_cm_id(id_priv->cm_id.iw);
1848 		}
1849 		cma_leave_mc_groups(id_priv);
1850 		cma_release_dev(id_priv);
1851 	}
1852 
1853 	cma_release_port(id_priv);
1854 	cma_id_put(id_priv);
1855 	wait_for_completion(&id_priv->comp);
1856 
1857 	if (id_priv->internal_id)
1858 		cma_id_put(id_priv->id.context);
1859 
1860 	kfree(id_priv->id.route.path_rec);
1861 
1862 	put_net(id_priv->id.route.addr.dev_addr.net);
1863 	kfree(id_priv);
1864 }
1865 
1866 /*
1867  * destroy an ID from within the handler_mutex. This ensures that no other
1868  * handlers can start running concurrently.
1869  */
1870 static void destroy_id_handler_unlock(struct rdma_id_private *id_priv)
1871 	__releases(&idprv->handler_mutex)
1872 {
1873 	enum rdma_cm_state state;
1874 	unsigned long flags;
1875 
1876 	trace_cm_id_destroy(id_priv);
1877 
1878 	/*
1879 	 * Setting the state to destroyed under the handler mutex provides a
1880 	 * fence against calling handler callbacks. If this is invoked due to
1881 	 * the failure of a handler callback then it guarentees that no future
1882 	 * handlers will be called.
1883 	 */
1884 	lockdep_assert_held(&id_priv->handler_mutex);
1885 	spin_lock_irqsave(&id_priv->lock, flags);
1886 	state = id_priv->state;
1887 	id_priv->state = RDMA_CM_DESTROYING;
1888 	spin_unlock_irqrestore(&id_priv->lock, flags);
1889 	mutex_unlock(&id_priv->handler_mutex);
1890 	_destroy_id(id_priv, state);
1891 }
1892 
1893 void rdma_destroy_id(struct rdma_cm_id *id)
1894 {
1895 	struct rdma_id_private *id_priv =
1896 		container_of(id, struct rdma_id_private, id);
1897 
1898 	mutex_lock(&id_priv->handler_mutex);
1899 	destroy_id_handler_unlock(id_priv);
1900 }
1901 EXPORT_SYMBOL(rdma_destroy_id);
1902 
1903 static int cma_rep_recv(struct rdma_id_private *id_priv)
1904 {
1905 	int ret;
1906 
1907 	ret = cma_modify_qp_rtr(id_priv, NULL);
1908 	if (ret)
1909 		goto reject;
1910 
1911 	ret = cma_modify_qp_rts(id_priv, NULL);
1912 	if (ret)
1913 		goto reject;
1914 
1915 	trace_cm_send_rtu(id_priv);
1916 	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1917 	if (ret)
1918 		goto reject;
1919 
1920 	return 0;
1921 reject:
1922 	pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
1923 	cma_modify_qp_err(id_priv);
1924 	trace_cm_send_rej(id_priv);
1925 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1926 		       NULL, 0, NULL, 0);
1927 	return ret;
1928 }
1929 
1930 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1931 				   const struct ib_cm_rep_event_param *rep_data,
1932 				   void *private_data)
1933 {
1934 	event->param.conn.private_data = private_data;
1935 	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1936 	event->param.conn.responder_resources = rep_data->responder_resources;
1937 	event->param.conn.initiator_depth = rep_data->initiator_depth;
1938 	event->param.conn.flow_control = rep_data->flow_control;
1939 	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1940 	event->param.conn.srq = rep_data->srq;
1941 	event->param.conn.qp_num = rep_data->remote_qpn;
1942 
1943 	event->ece.vendor_id = rep_data->ece.vendor_id;
1944 	event->ece.attr_mod = rep_data->ece.attr_mod;
1945 }
1946 
1947 static int cma_cm_event_handler(struct rdma_id_private *id_priv,
1948 				struct rdma_cm_event *event)
1949 {
1950 	int ret;
1951 
1952 	lockdep_assert_held(&id_priv->handler_mutex);
1953 
1954 	trace_cm_event_handler(id_priv, event);
1955 	ret = id_priv->id.event_handler(&id_priv->id, event);
1956 	trace_cm_event_done(id_priv, event, ret);
1957 	return ret;
1958 }
1959 
1960 static int cma_ib_handler(struct ib_cm_id *cm_id,
1961 			  const struct ib_cm_event *ib_event)
1962 {
1963 	struct rdma_id_private *id_priv = cm_id->context;
1964 	struct rdma_cm_event event = {};
1965 	enum rdma_cm_state state;
1966 	int ret;
1967 
1968 	mutex_lock(&id_priv->handler_mutex);
1969 	state = READ_ONCE(id_priv->state);
1970 	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1971 	     state != RDMA_CM_CONNECT) ||
1972 	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1973 	     state != RDMA_CM_DISCONNECT))
1974 		goto out;
1975 
1976 	switch (ib_event->event) {
1977 	case IB_CM_REQ_ERROR:
1978 	case IB_CM_REP_ERROR:
1979 		event.event = RDMA_CM_EVENT_UNREACHABLE;
1980 		event.status = -ETIMEDOUT;
1981 		break;
1982 	case IB_CM_REP_RECEIVED:
1983 		if (state == RDMA_CM_CONNECT &&
1984 		    (id_priv->id.qp_type != IB_QPT_UD)) {
1985 			trace_cm_send_mra(id_priv);
1986 			ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1987 		}
1988 		if (id_priv->id.qp) {
1989 			event.status = cma_rep_recv(id_priv);
1990 			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1991 						     RDMA_CM_EVENT_ESTABLISHED;
1992 		} else {
1993 			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1994 		}
1995 		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1996 				       ib_event->private_data);
1997 		break;
1998 	case IB_CM_RTU_RECEIVED:
1999 	case IB_CM_USER_ESTABLISHED:
2000 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2001 		break;
2002 	case IB_CM_DREQ_ERROR:
2003 		event.status = -ETIMEDOUT;
2004 		fallthrough;
2005 	case IB_CM_DREQ_RECEIVED:
2006 	case IB_CM_DREP_RECEIVED:
2007 		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
2008 				   RDMA_CM_DISCONNECT))
2009 			goto out;
2010 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2011 		break;
2012 	case IB_CM_TIMEWAIT_EXIT:
2013 		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
2014 		break;
2015 	case IB_CM_MRA_RECEIVED:
2016 		/* ignore event */
2017 		goto out;
2018 	case IB_CM_REJ_RECEIVED:
2019 		pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
2020 										ib_event->param.rej_rcvd.reason));
2021 		cma_modify_qp_err(id_priv);
2022 		event.status = ib_event->param.rej_rcvd.reason;
2023 		event.event = RDMA_CM_EVENT_REJECTED;
2024 		event.param.conn.private_data = ib_event->private_data;
2025 		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
2026 		break;
2027 	default:
2028 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2029 		       ib_event->event);
2030 		goto out;
2031 	}
2032 
2033 	ret = cma_cm_event_handler(id_priv, &event);
2034 	if (ret) {
2035 		/* Destroy the CM ID by returning a non-zero value. */
2036 		id_priv->cm_id.ib = NULL;
2037 		destroy_id_handler_unlock(id_priv);
2038 		return ret;
2039 	}
2040 out:
2041 	mutex_unlock(&id_priv->handler_mutex);
2042 	return 0;
2043 }
2044 
2045 static struct rdma_id_private *
2046 cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2047 		   const struct ib_cm_event *ib_event,
2048 		   struct net_device *net_dev)
2049 {
2050 	struct rdma_id_private *listen_id_priv;
2051 	struct rdma_id_private *id_priv;
2052 	struct rdma_cm_id *id;
2053 	struct rdma_route *rt;
2054 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2055 	struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2056 	const __be64 service_id =
2057 		ib_event->param.req_rcvd.primary_path->service_id;
2058 	int ret;
2059 
2060 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2061 	id_priv = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2062 				   listen_id->event_handler, listen_id->context,
2063 				   listen_id->ps,
2064 				   ib_event->param.req_rcvd.qp_type,
2065 				   listen_id_priv);
2066 	if (IS_ERR(id_priv))
2067 		return NULL;
2068 
2069 	id = &id_priv->id;
2070 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2071 			      (struct sockaddr *)&id->route.addr.dst_addr,
2072 			      listen_id, ib_event, ss_family, service_id))
2073 		goto err;
2074 
2075 	rt = &id->route;
2076 	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
2077 	rt->path_rec = kmalloc_array(rt->num_paths, sizeof(*rt->path_rec),
2078 				     GFP_KERNEL);
2079 	if (!rt->path_rec)
2080 		goto err;
2081 
2082 	rt->path_rec[0] = *path;
2083 	if (rt->num_paths == 2)
2084 		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2085 
2086 	if (net_dev) {
2087 		rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
2088 	} else {
2089 		if (!cma_protocol_roce(listen_id) &&
2090 		    cma_any_addr(cma_src_addr(id_priv))) {
2091 			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2092 			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2093 			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2094 		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
2095 			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2096 			if (ret)
2097 				goto err;
2098 		}
2099 	}
2100 	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2101 
2102 	id_priv->state = RDMA_CM_CONNECT;
2103 	return id_priv;
2104 
2105 err:
2106 	rdma_destroy_id(id);
2107 	return NULL;
2108 }
2109 
2110 static struct rdma_id_private *
2111 cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2112 		  const struct ib_cm_event *ib_event,
2113 		  struct net_device *net_dev)
2114 {
2115 	const struct rdma_id_private *listen_id_priv;
2116 	struct rdma_id_private *id_priv;
2117 	struct rdma_cm_id *id;
2118 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2119 	struct net *net = listen_id->route.addr.dev_addr.net;
2120 	int ret;
2121 
2122 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2123 	id_priv = __rdma_create_id(net, listen_id->event_handler,
2124 				   listen_id->context, listen_id->ps, IB_QPT_UD,
2125 				   listen_id_priv);
2126 	if (IS_ERR(id_priv))
2127 		return NULL;
2128 
2129 	id = &id_priv->id;
2130 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2131 			      (struct sockaddr *)&id->route.addr.dst_addr,
2132 			      listen_id, ib_event, ss_family,
2133 			      ib_event->param.sidr_req_rcvd.service_id))
2134 		goto err;
2135 
2136 	if (net_dev) {
2137 		rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
2138 	} else {
2139 		if (!cma_any_addr(cma_src_addr(id_priv))) {
2140 			ret = cma_translate_addr(cma_src_addr(id_priv),
2141 						 &id->route.addr.dev_addr);
2142 			if (ret)
2143 				goto err;
2144 		}
2145 	}
2146 
2147 	id_priv->state = RDMA_CM_CONNECT;
2148 	return id_priv;
2149 err:
2150 	rdma_destroy_id(id);
2151 	return NULL;
2152 }
2153 
2154 static void cma_set_req_event_data(struct rdma_cm_event *event,
2155 				   const struct ib_cm_req_event_param *req_data,
2156 				   void *private_data, int offset)
2157 {
2158 	event->param.conn.private_data = private_data + offset;
2159 	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2160 	event->param.conn.responder_resources = req_data->responder_resources;
2161 	event->param.conn.initiator_depth = req_data->initiator_depth;
2162 	event->param.conn.flow_control = req_data->flow_control;
2163 	event->param.conn.retry_count = req_data->retry_count;
2164 	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2165 	event->param.conn.srq = req_data->srq;
2166 	event->param.conn.qp_num = req_data->remote_qpn;
2167 
2168 	event->ece.vendor_id = req_data->ece.vendor_id;
2169 	event->ece.attr_mod = req_data->ece.attr_mod;
2170 }
2171 
2172 static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2173 				    const struct ib_cm_event *ib_event)
2174 {
2175 	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
2176 		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
2177 		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
2178 		 (id->qp_type == IB_QPT_UD)) ||
2179 		(!id->qp_type));
2180 }
2181 
2182 static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2183 			      const struct ib_cm_event *ib_event)
2184 {
2185 	struct rdma_id_private *listen_id, *conn_id = NULL;
2186 	struct rdma_cm_event event = {};
2187 	struct cma_req_info req = {};
2188 	struct net_device *net_dev;
2189 	u8 offset;
2190 	int ret;
2191 
2192 	listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
2193 	if (IS_ERR(listen_id))
2194 		return PTR_ERR(listen_id);
2195 
2196 	trace_cm_req_handler(listen_id, ib_event->event);
2197 	if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
2198 		ret = -EINVAL;
2199 		goto net_dev_put;
2200 	}
2201 
2202 	mutex_lock(&listen_id->handler_mutex);
2203 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN) {
2204 		ret = -ECONNABORTED;
2205 		goto err_unlock;
2206 	}
2207 
2208 	offset = cma_user_data_offset(listen_id);
2209 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2210 	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2211 		conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2212 		event.param.ud.private_data = ib_event->private_data + offset;
2213 		event.param.ud.private_data_len =
2214 				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2215 	} else {
2216 		conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2217 		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2218 				       ib_event->private_data, offset);
2219 	}
2220 	if (!conn_id) {
2221 		ret = -ENOMEM;
2222 		goto err_unlock;
2223 	}
2224 
2225 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2226 	ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2227 	if (ret) {
2228 		destroy_id_handler_unlock(conn_id);
2229 		goto err_unlock;
2230 	}
2231 
2232 	conn_id->cm_id.ib = cm_id;
2233 	cm_id->context = conn_id;
2234 	cm_id->cm_handler = cma_ib_handler;
2235 
2236 	ret = cma_cm_event_handler(conn_id, &event);
2237 	if (ret) {
2238 		/* Destroy the CM ID by returning a non-zero value. */
2239 		conn_id->cm_id.ib = NULL;
2240 		mutex_unlock(&listen_id->handler_mutex);
2241 		destroy_id_handler_unlock(conn_id);
2242 		goto net_dev_put;
2243 	}
2244 
2245 	if (READ_ONCE(conn_id->state) == RDMA_CM_CONNECT &&
2246 	    conn_id->id.qp_type != IB_QPT_UD) {
2247 		trace_cm_send_mra(cm_id->context);
2248 		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2249 	}
2250 	mutex_unlock(&conn_id->handler_mutex);
2251 
2252 err_unlock:
2253 	mutex_unlock(&listen_id->handler_mutex);
2254 
2255 net_dev_put:
2256 	if (net_dev)
2257 		dev_put(net_dev);
2258 
2259 	return ret;
2260 }
2261 
2262 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2263 {
2264 	if (addr->sa_family == AF_IB)
2265 		return ((struct sockaddr_ib *) addr)->sib_sid;
2266 
2267 	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2268 }
2269 EXPORT_SYMBOL(rdma_get_service_id);
2270 
2271 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2272 		    union ib_gid *dgid)
2273 {
2274 	struct rdma_addr *addr = &cm_id->route.addr;
2275 
2276 	if (!cm_id->device) {
2277 		if (sgid)
2278 			memset(sgid, 0, sizeof(*sgid));
2279 		if (dgid)
2280 			memset(dgid, 0, sizeof(*dgid));
2281 		return;
2282 	}
2283 
2284 	if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2285 		if (sgid)
2286 			rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2287 		if (dgid)
2288 			rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2289 	} else {
2290 		if (sgid)
2291 			rdma_addr_get_sgid(&addr->dev_addr, sgid);
2292 		if (dgid)
2293 			rdma_addr_get_dgid(&addr->dev_addr, dgid);
2294 	}
2295 }
2296 EXPORT_SYMBOL(rdma_read_gids);
2297 
2298 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2299 {
2300 	struct rdma_id_private *id_priv = iw_id->context;
2301 	struct rdma_cm_event event = {};
2302 	int ret = 0;
2303 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2304 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2305 
2306 	mutex_lock(&id_priv->handler_mutex);
2307 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
2308 		goto out;
2309 
2310 	switch (iw_event->event) {
2311 	case IW_CM_EVENT_CLOSE:
2312 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2313 		break;
2314 	case IW_CM_EVENT_CONNECT_REPLY:
2315 		memcpy(cma_src_addr(id_priv), laddr,
2316 		       rdma_addr_size(laddr));
2317 		memcpy(cma_dst_addr(id_priv), raddr,
2318 		       rdma_addr_size(raddr));
2319 		switch (iw_event->status) {
2320 		case 0:
2321 			event.event = RDMA_CM_EVENT_ESTABLISHED;
2322 			event.param.conn.initiator_depth = iw_event->ird;
2323 			event.param.conn.responder_resources = iw_event->ord;
2324 			break;
2325 		case -ECONNRESET:
2326 		case -ECONNREFUSED:
2327 			event.event = RDMA_CM_EVENT_REJECTED;
2328 			break;
2329 		case -ETIMEDOUT:
2330 			event.event = RDMA_CM_EVENT_UNREACHABLE;
2331 			break;
2332 		default:
2333 			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2334 			break;
2335 		}
2336 		break;
2337 	case IW_CM_EVENT_ESTABLISHED:
2338 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2339 		event.param.conn.initiator_depth = iw_event->ird;
2340 		event.param.conn.responder_resources = iw_event->ord;
2341 		break;
2342 	default:
2343 		goto out;
2344 	}
2345 
2346 	event.status = iw_event->status;
2347 	event.param.conn.private_data = iw_event->private_data;
2348 	event.param.conn.private_data_len = iw_event->private_data_len;
2349 	ret = cma_cm_event_handler(id_priv, &event);
2350 	if (ret) {
2351 		/* Destroy the CM ID by returning a non-zero value. */
2352 		id_priv->cm_id.iw = NULL;
2353 		destroy_id_handler_unlock(id_priv);
2354 		return ret;
2355 	}
2356 
2357 out:
2358 	mutex_unlock(&id_priv->handler_mutex);
2359 	return ret;
2360 }
2361 
2362 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2363 			       struct iw_cm_event *iw_event)
2364 {
2365 	struct rdma_id_private *listen_id, *conn_id;
2366 	struct rdma_cm_event event = {};
2367 	int ret = -ECONNABORTED;
2368 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2369 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2370 
2371 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2372 	event.param.conn.private_data = iw_event->private_data;
2373 	event.param.conn.private_data_len = iw_event->private_data_len;
2374 	event.param.conn.initiator_depth = iw_event->ird;
2375 	event.param.conn.responder_resources = iw_event->ord;
2376 
2377 	listen_id = cm_id->context;
2378 
2379 	mutex_lock(&listen_id->handler_mutex);
2380 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN)
2381 		goto out;
2382 
2383 	/* Create a new RDMA id for the new IW CM ID */
2384 	conn_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2385 				   listen_id->id.event_handler,
2386 				   listen_id->id.context, RDMA_PS_TCP,
2387 				   IB_QPT_RC, listen_id);
2388 	if (IS_ERR(conn_id)) {
2389 		ret = -ENOMEM;
2390 		goto out;
2391 	}
2392 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2393 	conn_id->state = RDMA_CM_CONNECT;
2394 
2395 	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2396 	if (ret) {
2397 		mutex_unlock(&listen_id->handler_mutex);
2398 		destroy_id_handler_unlock(conn_id);
2399 		return ret;
2400 	}
2401 
2402 	ret = cma_iw_acquire_dev(conn_id, listen_id);
2403 	if (ret) {
2404 		mutex_unlock(&listen_id->handler_mutex);
2405 		destroy_id_handler_unlock(conn_id);
2406 		return ret;
2407 	}
2408 
2409 	conn_id->cm_id.iw = cm_id;
2410 	cm_id->context = conn_id;
2411 	cm_id->cm_handler = cma_iw_handler;
2412 
2413 	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2414 	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2415 
2416 	ret = cma_cm_event_handler(conn_id, &event);
2417 	if (ret) {
2418 		/* User wants to destroy the CM ID */
2419 		conn_id->cm_id.iw = NULL;
2420 		mutex_unlock(&listen_id->handler_mutex);
2421 		destroy_id_handler_unlock(conn_id);
2422 		return ret;
2423 	}
2424 
2425 	mutex_unlock(&conn_id->handler_mutex);
2426 
2427 out:
2428 	mutex_unlock(&listen_id->handler_mutex);
2429 	return ret;
2430 }
2431 
2432 static int cma_ib_listen(struct rdma_id_private *id_priv)
2433 {
2434 	struct sockaddr *addr;
2435 	struct ib_cm_id	*id;
2436 	__be64 svc_id;
2437 
2438 	addr = cma_src_addr(id_priv);
2439 	svc_id = rdma_get_service_id(&id_priv->id, addr);
2440 	id = ib_cm_insert_listen(id_priv->id.device,
2441 				 cma_ib_req_handler, svc_id);
2442 	if (IS_ERR(id))
2443 		return PTR_ERR(id);
2444 	id_priv->cm_id.ib = id;
2445 
2446 	return 0;
2447 }
2448 
2449 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2450 {
2451 	int ret;
2452 	struct iw_cm_id	*id;
2453 
2454 	id = iw_create_cm_id(id_priv->id.device,
2455 			     iw_conn_req_handler,
2456 			     id_priv);
2457 	if (IS_ERR(id))
2458 		return PTR_ERR(id);
2459 
2460 	mutex_lock(&id_priv->qp_mutex);
2461 	id->tos = id_priv->tos;
2462 	id->tos_set = id_priv->tos_set;
2463 	mutex_unlock(&id_priv->qp_mutex);
2464 	id->afonly = id_priv->afonly;
2465 	id_priv->cm_id.iw = id;
2466 
2467 	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2468 	       rdma_addr_size(cma_src_addr(id_priv)));
2469 
2470 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2471 
2472 	if (ret) {
2473 		iw_destroy_cm_id(id_priv->cm_id.iw);
2474 		id_priv->cm_id.iw = NULL;
2475 	}
2476 
2477 	return ret;
2478 }
2479 
2480 static int cma_listen_handler(struct rdma_cm_id *id,
2481 			      struct rdma_cm_event *event)
2482 {
2483 	struct rdma_id_private *id_priv = id->context;
2484 
2485 	/* Listening IDs are always destroyed on removal */
2486 	if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2487 		return -1;
2488 
2489 	id->context = id_priv->id.context;
2490 	id->event_handler = id_priv->id.event_handler;
2491 	trace_cm_event_handler(id_priv, event);
2492 	return id_priv->id.event_handler(id, event);
2493 }
2494 
2495 static int cma_listen_on_dev(struct rdma_id_private *id_priv,
2496 			     struct cma_device *cma_dev,
2497 			     struct rdma_id_private **to_destroy)
2498 {
2499 	struct rdma_id_private *dev_id_priv;
2500 	struct net *net = id_priv->id.route.addr.dev_addr.net;
2501 	int ret;
2502 
2503 	lockdep_assert_held(&lock);
2504 
2505 	*to_destroy = NULL;
2506 	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2507 		return 0;
2508 
2509 	dev_id_priv =
2510 		__rdma_create_id(net, cma_listen_handler, id_priv,
2511 				 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2512 	if (IS_ERR(dev_id_priv))
2513 		return PTR_ERR(dev_id_priv);
2514 
2515 	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2516 	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2517 	       rdma_addr_size(cma_src_addr(id_priv)));
2518 
2519 	_cma_attach_to_dev(dev_id_priv, cma_dev);
2520 	rdma_restrack_add(&dev_id_priv->res);
2521 	cma_id_get(id_priv);
2522 	dev_id_priv->internal_id = 1;
2523 	dev_id_priv->afonly = id_priv->afonly;
2524 	mutex_lock(&id_priv->qp_mutex);
2525 	dev_id_priv->tos_set = id_priv->tos_set;
2526 	dev_id_priv->tos = id_priv->tos;
2527 	mutex_unlock(&id_priv->qp_mutex);
2528 
2529 	ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2530 	if (ret)
2531 		goto err_listen;
2532 	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2533 	return 0;
2534 err_listen:
2535 	/* Caller must destroy this after releasing lock */
2536 	*to_destroy = dev_id_priv;
2537 	dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret);
2538 	return ret;
2539 }
2540 
2541 static int cma_listen_on_all(struct rdma_id_private *id_priv)
2542 {
2543 	struct rdma_id_private *to_destroy;
2544 	struct cma_device *cma_dev;
2545 	int ret;
2546 
2547 	mutex_lock(&lock);
2548 	list_add_tail(&id_priv->list, &listen_any_list);
2549 	list_for_each_entry(cma_dev, &dev_list, list) {
2550 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
2551 		if (ret) {
2552 			/* Prevent racing with cma_process_remove() */
2553 			if (to_destroy)
2554 				list_del_init(&to_destroy->list);
2555 			goto err_listen;
2556 		}
2557 	}
2558 	mutex_unlock(&lock);
2559 	return 0;
2560 
2561 err_listen:
2562 	list_del(&id_priv->list);
2563 	mutex_unlock(&lock);
2564 	if (to_destroy)
2565 		rdma_destroy_id(&to_destroy->id);
2566 	return ret;
2567 }
2568 
2569 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2570 {
2571 	struct rdma_id_private *id_priv;
2572 
2573 	id_priv = container_of(id, struct rdma_id_private, id);
2574 	mutex_lock(&id_priv->qp_mutex);
2575 	id_priv->tos = (u8) tos;
2576 	id_priv->tos_set = true;
2577 	mutex_unlock(&id_priv->qp_mutex);
2578 }
2579 EXPORT_SYMBOL(rdma_set_service_type);
2580 
2581 /**
2582  * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2583  *                          with a connection identifier.
2584  * @id: Communication identifier to associated with service type.
2585  * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2586  *
2587  * This function should be called before rdma_connect() on active side,
2588  * and on passive side before rdma_accept(). It is applicable to primary
2589  * path only. The timeout will affect the local side of the QP, it is not
2590  * negotiated with remote side and zero disables the timer. In case it is
2591  * set before rdma_resolve_route, the value will also be used to determine
2592  * PacketLifeTime for RoCE.
2593  *
2594  * Return: 0 for success
2595  */
2596 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2597 {
2598 	struct rdma_id_private *id_priv;
2599 
2600 	if (id->qp_type != IB_QPT_RC)
2601 		return -EINVAL;
2602 
2603 	id_priv = container_of(id, struct rdma_id_private, id);
2604 	mutex_lock(&id_priv->qp_mutex);
2605 	id_priv->timeout = timeout;
2606 	id_priv->timeout_set = true;
2607 	mutex_unlock(&id_priv->qp_mutex);
2608 
2609 	return 0;
2610 }
2611 EXPORT_SYMBOL(rdma_set_ack_timeout);
2612 
2613 /**
2614  * rdma_set_min_rnr_timer() - Set the minimum RNR Retry timer of the
2615  *			      QP associated with a connection identifier.
2616  * @id: Communication identifier to associated with service type.
2617  * @min_rnr_timer: 5-bit value encoded as Table 45: "Encoding for RNR NAK
2618  *		   Timer Field" in the IBTA specification.
2619  *
2620  * This function should be called before rdma_connect() on active
2621  * side, and on passive side before rdma_accept(). The timer value
2622  * will be associated with the local QP. When it receives a send it is
2623  * not read to handle, typically if the receive queue is empty, an RNR
2624  * Retry NAK is returned to the requester with the min_rnr_timer
2625  * encoded. The requester will then wait at least the time specified
2626  * in the NAK before retrying. The default is zero, which translates
2627  * to a minimum RNR Timer value of 655 ms.
2628  *
2629  * Return: 0 for success
2630  */
2631 int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer)
2632 {
2633 	struct rdma_id_private *id_priv;
2634 
2635 	/* It is a five-bit value */
2636 	if (min_rnr_timer & 0xe0)
2637 		return -EINVAL;
2638 
2639 	if (WARN_ON(id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_TGT))
2640 		return -EINVAL;
2641 
2642 	id_priv = container_of(id, struct rdma_id_private, id);
2643 	mutex_lock(&id_priv->qp_mutex);
2644 	id_priv->min_rnr_timer = min_rnr_timer;
2645 	id_priv->min_rnr_timer_set = true;
2646 	mutex_unlock(&id_priv->qp_mutex);
2647 
2648 	return 0;
2649 }
2650 EXPORT_SYMBOL(rdma_set_min_rnr_timer);
2651 
2652 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2653 			      void *context)
2654 {
2655 	struct cma_work *work = context;
2656 	struct rdma_route *route;
2657 
2658 	route = &work->id->id.route;
2659 
2660 	if (!status) {
2661 		route->num_paths = 1;
2662 		*route->path_rec = *path_rec;
2663 	} else {
2664 		work->old_state = RDMA_CM_ROUTE_QUERY;
2665 		work->new_state = RDMA_CM_ADDR_RESOLVED;
2666 		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2667 		work->event.status = status;
2668 		pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2669 				     status);
2670 	}
2671 
2672 	queue_work(cma_wq, &work->work);
2673 }
2674 
2675 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2676 			      unsigned long timeout_ms, struct cma_work *work)
2677 {
2678 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2679 	struct sa_path_rec path_rec;
2680 	ib_sa_comp_mask comp_mask;
2681 	struct sockaddr_in6 *sin6;
2682 	struct sockaddr_ib *sib;
2683 
2684 	memset(&path_rec, 0, sizeof path_rec);
2685 
2686 	if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2687 		path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2688 	else
2689 		path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2690 	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2691 	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2692 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2693 	path_rec.numb_path = 1;
2694 	path_rec.reversible = 1;
2695 	path_rec.service_id = rdma_get_service_id(&id_priv->id,
2696 						  cma_dst_addr(id_priv));
2697 
2698 	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2699 		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2700 		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2701 
2702 	switch (cma_family(id_priv)) {
2703 	case AF_INET:
2704 		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2705 		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2706 		break;
2707 	case AF_INET6:
2708 		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2709 		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2710 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2711 		break;
2712 	case AF_IB:
2713 		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2714 		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2715 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2716 		break;
2717 	}
2718 
2719 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2720 					       id_priv->id.port_num, &path_rec,
2721 					       comp_mask, timeout_ms,
2722 					       GFP_KERNEL, cma_query_handler,
2723 					       work, &id_priv->query);
2724 
2725 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2726 }
2727 
2728 static void cma_iboe_join_work_handler(struct work_struct *work)
2729 {
2730 	struct cma_multicast *mc =
2731 		container_of(work, struct cma_multicast, iboe_join.work);
2732 	struct rdma_cm_event *event = &mc->iboe_join.event;
2733 	struct rdma_id_private *id_priv = mc->id_priv;
2734 	int ret;
2735 
2736 	mutex_lock(&id_priv->handler_mutex);
2737 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2738 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2739 		goto out_unlock;
2740 
2741 	ret = cma_cm_event_handler(id_priv, event);
2742 	WARN_ON(ret);
2743 
2744 out_unlock:
2745 	mutex_unlock(&id_priv->handler_mutex);
2746 	if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2747 		rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2748 }
2749 
2750 static void cma_work_handler(struct work_struct *_work)
2751 {
2752 	struct cma_work *work = container_of(_work, struct cma_work, work);
2753 	struct rdma_id_private *id_priv = work->id;
2754 
2755 	mutex_lock(&id_priv->handler_mutex);
2756 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2757 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2758 		goto out_unlock;
2759 	if (work->old_state != 0 || work->new_state != 0) {
2760 		if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2761 			goto out_unlock;
2762 	}
2763 
2764 	if (cma_cm_event_handler(id_priv, &work->event)) {
2765 		cma_id_put(id_priv);
2766 		destroy_id_handler_unlock(id_priv);
2767 		goto out_free;
2768 	}
2769 
2770 out_unlock:
2771 	mutex_unlock(&id_priv->handler_mutex);
2772 	cma_id_put(id_priv);
2773 out_free:
2774 	if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
2775 		rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
2776 	kfree(work);
2777 }
2778 
2779 static void cma_init_resolve_route_work(struct cma_work *work,
2780 					struct rdma_id_private *id_priv)
2781 {
2782 	work->id = id_priv;
2783 	INIT_WORK(&work->work, cma_work_handler);
2784 	work->old_state = RDMA_CM_ROUTE_QUERY;
2785 	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2786 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2787 }
2788 
2789 static void enqueue_resolve_addr_work(struct cma_work *work,
2790 				      struct rdma_id_private *id_priv)
2791 {
2792 	/* Balances with cma_id_put() in cma_work_handler */
2793 	cma_id_get(id_priv);
2794 
2795 	work->id = id_priv;
2796 	INIT_WORK(&work->work, cma_work_handler);
2797 	work->old_state = RDMA_CM_ADDR_QUERY;
2798 	work->new_state = RDMA_CM_ADDR_RESOLVED;
2799 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2800 
2801 	queue_work(cma_wq, &work->work);
2802 }
2803 
2804 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
2805 				unsigned long timeout_ms)
2806 {
2807 	struct rdma_route *route = &id_priv->id.route;
2808 	struct cma_work *work;
2809 	int ret;
2810 
2811 	work = kzalloc(sizeof *work, GFP_KERNEL);
2812 	if (!work)
2813 		return -ENOMEM;
2814 
2815 	cma_init_resolve_route_work(work, id_priv);
2816 
2817 	if (!route->path_rec)
2818 		route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2819 	if (!route->path_rec) {
2820 		ret = -ENOMEM;
2821 		goto err1;
2822 	}
2823 
2824 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2825 	if (ret)
2826 		goto err2;
2827 
2828 	return 0;
2829 err2:
2830 	kfree(route->path_rec);
2831 	route->path_rec = NULL;
2832 err1:
2833 	kfree(work);
2834 	return ret;
2835 }
2836 
2837 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2838 					   unsigned long supported_gids,
2839 					   enum ib_gid_type default_gid)
2840 {
2841 	if ((network_type == RDMA_NETWORK_IPV4 ||
2842 	     network_type == RDMA_NETWORK_IPV6) &&
2843 	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2844 		return IB_GID_TYPE_ROCE_UDP_ENCAP;
2845 
2846 	return default_gid;
2847 }
2848 
2849 /*
2850  * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2851  * path record type based on GID type.
2852  * It also sets up other L2 fields which includes destination mac address
2853  * netdev ifindex, of the path record.
2854  * It returns the netdev of the bound interface for this path record entry.
2855  */
2856 static struct net_device *
2857 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
2858 {
2859 	struct rdma_route *route = &id_priv->id.route;
2860 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
2861 	struct rdma_addr *addr = &route->addr;
2862 	unsigned long supported_gids;
2863 	struct net_device *ndev;
2864 
2865 	if (!addr->dev_addr.bound_dev_if)
2866 		return NULL;
2867 
2868 	ndev = dev_get_by_index(addr->dev_addr.net,
2869 				addr->dev_addr.bound_dev_if);
2870 	if (!ndev)
2871 		return NULL;
2872 
2873 	supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2874 						    id_priv->id.port_num);
2875 	gid_type = cma_route_gid_type(addr->dev_addr.network,
2876 				      supported_gids,
2877 				      id_priv->gid_type);
2878 	/* Use the hint from IP Stack to select GID Type */
2879 	if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2880 		gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2881 	route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
2882 
2883 	route->path_rec->roce.route_resolved = true;
2884 	sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
2885 	return ndev;
2886 }
2887 
2888 int rdma_set_ib_path(struct rdma_cm_id *id,
2889 		     struct sa_path_rec *path_rec)
2890 {
2891 	struct rdma_id_private *id_priv;
2892 	struct net_device *ndev;
2893 	int ret;
2894 
2895 	id_priv = container_of(id, struct rdma_id_private, id);
2896 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2897 			   RDMA_CM_ROUTE_RESOLVED))
2898 		return -EINVAL;
2899 
2900 	id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
2901 				     GFP_KERNEL);
2902 	if (!id->route.path_rec) {
2903 		ret = -ENOMEM;
2904 		goto err;
2905 	}
2906 
2907 	if (rdma_protocol_roce(id->device, id->port_num)) {
2908 		ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2909 		if (!ndev) {
2910 			ret = -ENODEV;
2911 			goto err_free;
2912 		}
2913 		dev_put(ndev);
2914 	}
2915 
2916 	id->route.num_paths = 1;
2917 	return 0;
2918 
2919 err_free:
2920 	kfree(id->route.path_rec);
2921 	id->route.path_rec = NULL;
2922 err:
2923 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2924 	return ret;
2925 }
2926 EXPORT_SYMBOL(rdma_set_ib_path);
2927 
2928 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
2929 {
2930 	struct cma_work *work;
2931 
2932 	work = kzalloc(sizeof *work, GFP_KERNEL);
2933 	if (!work)
2934 		return -ENOMEM;
2935 
2936 	cma_init_resolve_route_work(work, id_priv);
2937 	queue_work(cma_wq, &work->work);
2938 	return 0;
2939 }
2940 
2941 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
2942 {
2943 	struct net_device *dev;
2944 
2945 	dev = vlan_dev_real_dev(vlan_ndev);
2946 	if (dev->num_tc)
2947 		return netdev_get_prio_tc_map(dev, prio);
2948 
2949 	return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
2950 		VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2951 }
2952 
2953 struct iboe_prio_tc_map {
2954 	int input_prio;
2955 	int output_tc;
2956 	bool found;
2957 };
2958 
2959 static int get_lower_vlan_dev_tc(struct net_device *dev,
2960 				 struct netdev_nested_priv *priv)
2961 {
2962 	struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
2963 
2964 	if (is_vlan_dev(dev))
2965 		map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
2966 	else if (dev->num_tc)
2967 		map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
2968 	else
2969 		map->output_tc = 0;
2970 	/* We are interested only in first level VLAN device, so always
2971 	 * return 1 to stop iterating over next level devices.
2972 	 */
2973 	map->found = true;
2974 	return 1;
2975 }
2976 
2977 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2978 {
2979 	struct iboe_prio_tc_map prio_tc_map = {};
2980 	int prio = rt_tos2priority(tos);
2981 	struct netdev_nested_priv priv;
2982 
2983 	/* If VLAN device, get it directly from the VLAN netdev */
2984 	if (is_vlan_dev(ndev))
2985 		return get_vlan_ndev_tc(ndev, prio);
2986 
2987 	prio_tc_map.input_prio = prio;
2988 	priv.data = (void *)&prio_tc_map;
2989 	rcu_read_lock();
2990 	netdev_walk_all_lower_dev_rcu(ndev,
2991 				      get_lower_vlan_dev_tc,
2992 				      &priv);
2993 	rcu_read_unlock();
2994 	/* If map is found from lower device, use it; Otherwise
2995 	 * continue with the current netdevice to get priority to tc map.
2996 	 */
2997 	if (prio_tc_map.found)
2998 		return prio_tc_map.output_tc;
2999 	else if (ndev->num_tc)
3000 		return netdev_get_prio_tc_map(ndev, prio);
3001 	else
3002 		return 0;
3003 }
3004 
3005 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
3006 {
3007 	struct sockaddr_in6 *addr6;
3008 	u16 dport, sport;
3009 	u32 hash, fl;
3010 
3011 	addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3012 	fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3013 	if ((cma_family(id_priv) != AF_INET6) || !fl) {
3014 		dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3015 		sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3016 		hash = (u32)sport * 31 + dport;
3017 		fl = hash & IB_GRH_FLOWLABEL_MASK;
3018 	}
3019 
3020 	return cpu_to_be32(fl);
3021 }
3022 
3023 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
3024 {
3025 	struct rdma_route *route = &id_priv->id.route;
3026 	struct rdma_addr *addr = &route->addr;
3027 	struct cma_work *work;
3028 	int ret;
3029 	struct net_device *ndev;
3030 
3031 	u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
3032 					rdma_start_port(id_priv->cma_dev->device)];
3033 	u8 tos;
3034 
3035 	mutex_lock(&id_priv->qp_mutex);
3036 	tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3037 	mutex_unlock(&id_priv->qp_mutex);
3038 
3039 	work = kzalloc(sizeof *work, GFP_KERNEL);
3040 	if (!work)
3041 		return -ENOMEM;
3042 
3043 	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
3044 	if (!route->path_rec) {
3045 		ret = -ENOMEM;
3046 		goto err1;
3047 	}
3048 
3049 	route->num_paths = 1;
3050 
3051 	ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3052 	if (!ndev) {
3053 		ret = -ENODEV;
3054 		goto err2;
3055 	}
3056 
3057 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3058 		    &route->path_rec->sgid);
3059 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
3060 		    &route->path_rec->dgid);
3061 
3062 	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3063 		/* TODO: get the hoplimit from the inet/inet6 device */
3064 		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3065 	else
3066 		route->path_rec->hop_limit = 1;
3067 	route->path_rec->reversible = 1;
3068 	route->path_rec->pkey = cpu_to_be16(0xffff);
3069 	route->path_rec->mtu_selector = IB_SA_EQ;
3070 	route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
3071 	route->path_rec->traffic_class = tos;
3072 	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
3073 	route->path_rec->rate_selector = IB_SA_EQ;
3074 	route->path_rec->rate = iboe_get_rate(ndev);
3075 	dev_put(ndev);
3076 	route->path_rec->packet_life_time_selector = IB_SA_EQ;
3077 	/* In case ACK timeout is set, use this value to calculate
3078 	 * PacketLifeTime.  As per IBTA 12.7.34,
3079 	 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3080 	 * Assuming a negligible local ACK delay, we can use
3081 	 * PacketLifeTime = local ACK timeout/2
3082 	 * as a reasonable approximation for RoCE networks.
3083 	 */
3084 	mutex_lock(&id_priv->qp_mutex);
3085 	if (id_priv->timeout_set && id_priv->timeout)
3086 		route->path_rec->packet_life_time = id_priv->timeout - 1;
3087 	else
3088 		route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
3089 	mutex_unlock(&id_priv->qp_mutex);
3090 
3091 	if (!route->path_rec->mtu) {
3092 		ret = -EINVAL;
3093 		goto err2;
3094 	}
3095 
3096 	if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3097 					 id_priv->id.port_num))
3098 		route->path_rec->flow_label =
3099 			cma_get_roce_udp_flow_label(id_priv);
3100 
3101 	cma_init_resolve_route_work(work, id_priv);
3102 	queue_work(cma_wq, &work->work);
3103 
3104 	return 0;
3105 
3106 err2:
3107 	kfree(route->path_rec);
3108 	route->path_rec = NULL;
3109 	route->num_paths = 0;
3110 err1:
3111 	kfree(work);
3112 	return ret;
3113 }
3114 
3115 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3116 {
3117 	struct rdma_id_private *id_priv;
3118 	int ret;
3119 
3120 	id_priv = container_of(id, struct rdma_id_private, id);
3121 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3122 		return -EINVAL;
3123 
3124 	cma_id_get(id_priv);
3125 	if (rdma_cap_ib_sa(id->device, id->port_num))
3126 		ret = cma_resolve_ib_route(id_priv, timeout_ms);
3127 	else if (rdma_protocol_roce(id->device, id->port_num))
3128 		ret = cma_resolve_iboe_route(id_priv);
3129 	else if (rdma_protocol_iwarp(id->device, id->port_num))
3130 		ret = cma_resolve_iw_route(id_priv);
3131 	else
3132 		ret = -ENOSYS;
3133 
3134 	if (ret)
3135 		goto err;
3136 
3137 	return 0;
3138 err:
3139 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3140 	cma_id_put(id_priv);
3141 	return ret;
3142 }
3143 EXPORT_SYMBOL(rdma_resolve_route);
3144 
3145 static void cma_set_loopback(struct sockaddr *addr)
3146 {
3147 	switch (addr->sa_family) {
3148 	case AF_INET:
3149 		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3150 		break;
3151 	case AF_INET6:
3152 		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3153 			      0, 0, 0, htonl(1));
3154 		break;
3155 	default:
3156 		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3157 			    0, 0, 0, htonl(1));
3158 		break;
3159 	}
3160 }
3161 
3162 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3163 {
3164 	struct cma_device *cma_dev, *cur_dev;
3165 	union ib_gid gid;
3166 	enum ib_port_state port_state;
3167 	unsigned int p;
3168 	u16 pkey;
3169 	int ret;
3170 
3171 	cma_dev = NULL;
3172 	mutex_lock(&lock);
3173 	list_for_each_entry(cur_dev, &dev_list, list) {
3174 		if (cma_family(id_priv) == AF_IB &&
3175 		    !rdma_cap_ib_cm(cur_dev->device, 1))
3176 			continue;
3177 
3178 		if (!cma_dev)
3179 			cma_dev = cur_dev;
3180 
3181 		rdma_for_each_port (cur_dev->device, p) {
3182 			if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3183 			    port_state == IB_PORT_ACTIVE) {
3184 				cma_dev = cur_dev;
3185 				goto port_found;
3186 			}
3187 		}
3188 	}
3189 
3190 	if (!cma_dev) {
3191 		ret = -ENODEV;
3192 		goto out;
3193 	}
3194 
3195 	p = 1;
3196 
3197 port_found:
3198 	ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3199 	if (ret)
3200 		goto out;
3201 
3202 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3203 	if (ret)
3204 		goto out;
3205 
3206 	id_priv->id.route.addr.dev_addr.dev_type =
3207 		(rdma_protocol_ib(cma_dev->device, p)) ?
3208 		ARPHRD_INFINIBAND : ARPHRD_ETHER;
3209 
3210 	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3211 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3212 	id_priv->id.port_num = p;
3213 	cma_attach_to_dev(id_priv, cma_dev);
3214 	rdma_restrack_add(&id_priv->res);
3215 	cma_set_loopback(cma_src_addr(id_priv));
3216 out:
3217 	mutex_unlock(&lock);
3218 	return ret;
3219 }
3220 
3221 static void addr_handler(int status, struct sockaddr *src_addr,
3222 			 struct rdma_dev_addr *dev_addr, void *context)
3223 {
3224 	struct rdma_id_private *id_priv = context;
3225 	struct rdma_cm_event event = {};
3226 	struct sockaddr *addr;
3227 	struct sockaddr_storage old_addr;
3228 
3229 	mutex_lock(&id_priv->handler_mutex);
3230 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3231 			   RDMA_CM_ADDR_RESOLVED))
3232 		goto out;
3233 
3234 	/*
3235 	 * Store the previous src address, so that if we fail to acquire
3236 	 * matching rdma device, old address can be restored back, which helps
3237 	 * to cancel the cma listen operation correctly.
3238 	 */
3239 	addr = cma_src_addr(id_priv);
3240 	memcpy(&old_addr, addr, rdma_addr_size(addr));
3241 	memcpy(addr, src_addr, rdma_addr_size(src_addr));
3242 	if (!status && !id_priv->cma_dev) {
3243 		status = cma_acquire_dev_by_src_ip(id_priv);
3244 		if (status)
3245 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3246 					     status);
3247 		rdma_restrack_add(&id_priv->res);
3248 	} else if (status) {
3249 		pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3250 	}
3251 
3252 	if (status) {
3253 		memcpy(addr, &old_addr,
3254 		       rdma_addr_size((struct sockaddr *)&old_addr));
3255 		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3256 				   RDMA_CM_ADDR_BOUND))
3257 			goto out;
3258 		event.event = RDMA_CM_EVENT_ADDR_ERROR;
3259 		event.status = status;
3260 	} else
3261 		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3262 
3263 	if (cma_cm_event_handler(id_priv, &event)) {
3264 		destroy_id_handler_unlock(id_priv);
3265 		return;
3266 	}
3267 out:
3268 	mutex_unlock(&id_priv->handler_mutex);
3269 }
3270 
3271 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3272 {
3273 	struct cma_work *work;
3274 	union ib_gid gid;
3275 	int ret;
3276 
3277 	work = kzalloc(sizeof *work, GFP_KERNEL);
3278 	if (!work)
3279 		return -ENOMEM;
3280 
3281 	if (!id_priv->cma_dev) {
3282 		ret = cma_bind_loopback(id_priv);
3283 		if (ret)
3284 			goto err;
3285 	}
3286 
3287 	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3288 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3289 
3290 	enqueue_resolve_addr_work(work, id_priv);
3291 	return 0;
3292 err:
3293 	kfree(work);
3294 	return ret;
3295 }
3296 
3297 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3298 {
3299 	struct cma_work *work;
3300 	int ret;
3301 
3302 	work = kzalloc(sizeof *work, GFP_KERNEL);
3303 	if (!work)
3304 		return -ENOMEM;
3305 
3306 	if (!id_priv->cma_dev) {
3307 		ret = cma_resolve_ib_dev(id_priv);
3308 		if (ret)
3309 			goto err;
3310 	}
3311 
3312 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3313 		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3314 
3315 	enqueue_resolve_addr_work(work, id_priv);
3316 	return 0;
3317 err:
3318 	kfree(work);
3319 	return ret;
3320 }
3321 
3322 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3323 			 const struct sockaddr *dst_addr)
3324 {
3325 	if (!src_addr || !src_addr->sa_family) {
3326 		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
3327 		src_addr->sa_family = dst_addr->sa_family;
3328 		if (IS_ENABLED(CONFIG_IPV6) &&
3329 		    dst_addr->sa_family == AF_INET6) {
3330 			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
3331 			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
3332 			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
3333 			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
3334 				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
3335 		} else if (dst_addr->sa_family == AF_IB) {
3336 			((struct sockaddr_ib *) src_addr)->sib_pkey =
3337 				((struct sockaddr_ib *) dst_addr)->sib_pkey;
3338 		}
3339 	}
3340 	return rdma_bind_addr(id, src_addr);
3341 }
3342 
3343 /*
3344  * If required, resolve the source address for bind and leave the id_priv in
3345  * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3346  * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3347  * ignored.
3348  */
3349 static int resolve_prepare_src(struct rdma_id_private *id_priv,
3350 			       struct sockaddr *src_addr,
3351 			       const struct sockaddr *dst_addr)
3352 {
3353 	int ret;
3354 
3355 	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3356 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3357 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3358 		ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3359 		if (ret)
3360 			goto err_dst;
3361 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3362 					   RDMA_CM_ADDR_QUERY))) {
3363 			ret = -EINVAL;
3364 			goto err_dst;
3365 		}
3366 	}
3367 
3368 	if (cma_family(id_priv) != dst_addr->sa_family) {
3369 		ret = -EINVAL;
3370 		goto err_state;
3371 	}
3372 	return 0;
3373 
3374 err_state:
3375 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3376 err_dst:
3377 	memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3378 	return ret;
3379 }
3380 
3381 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3382 		      const struct sockaddr *dst_addr, unsigned long timeout_ms)
3383 {
3384 	struct rdma_id_private *id_priv =
3385 		container_of(id, struct rdma_id_private, id);
3386 	int ret;
3387 
3388 	ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3389 	if (ret)
3390 		return ret;
3391 
3392 	if (cma_any_addr(dst_addr)) {
3393 		ret = cma_resolve_loopback(id_priv);
3394 	} else {
3395 		if (dst_addr->sa_family == AF_IB) {
3396 			ret = cma_resolve_ib_addr(id_priv);
3397 		} else {
3398 			ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
3399 					      &id->route.addr.dev_addr,
3400 					      timeout_ms, addr_handler,
3401 					      false, id_priv);
3402 		}
3403 	}
3404 	if (ret)
3405 		goto err;
3406 
3407 	return 0;
3408 err:
3409 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3410 	return ret;
3411 }
3412 EXPORT_SYMBOL(rdma_resolve_addr);
3413 
3414 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3415 {
3416 	struct rdma_id_private *id_priv;
3417 	unsigned long flags;
3418 	int ret;
3419 
3420 	id_priv = container_of(id, struct rdma_id_private, id);
3421 	spin_lock_irqsave(&id_priv->lock, flags);
3422 	if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3423 	    id_priv->state == RDMA_CM_IDLE) {
3424 		id_priv->reuseaddr = reuse;
3425 		ret = 0;
3426 	} else {
3427 		ret = -EINVAL;
3428 	}
3429 	spin_unlock_irqrestore(&id_priv->lock, flags);
3430 	return ret;
3431 }
3432 EXPORT_SYMBOL(rdma_set_reuseaddr);
3433 
3434 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3435 {
3436 	struct rdma_id_private *id_priv;
3437 	unsigned long flags;
3438 	int ret;
3439 
3440 	id_priv = container_of(id, struct rdma_id_private, id);
3441 	spin_lock_irqsave(&id_priv->lock, flags);
3442 	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3443 		id_priv->options |= (1 << CMA_OPTION_AFONLY);
3444 		id_priv->afonly = afonly;
3445 		ret = 0;
3446 	} else {
3447 		ret = -EINVAL;
3448 	}
3449 	spin_unlock_irqrestore(&id_priv->lock, flags);
3450 	return ret;
3451 }
3452 EXPORT_SYMBOL(rdma_set_afonly);
3453 
3454 static void cma_bind_port(struct rdma_bind_list *bind_list,
3455 			  struct rdma_id_private *id_priv)
3456 {
3457 	struct sockaddr *addr;
3458 	struct sockaddr_ib *sib;
3459 	u64 sid, mask;
3460 	__be16 port;
3461 
3462 	lockdep_assert_held(&lock);
3463 
3464 	addr = cma_src_addr(id_priv);
3465 	port = htons(bind_list->port);
3466 
3467 	switch (addr->sa_family) {
3468 	case AF_INET:
3469 		((struct sockaddr_in *) addr)->sin_port = port;
3470 		break;
3471 	case AF_INET6:
3472 		((struct sockaddr_in6 *) addr)->sin6_port = port;
3473 		break;
3474 	case AF_IB:
3475 		sib = (struct sockaddr_ib *) addr;
3476 		sid = be64_to_cpu(sib->sib_sid);
3477 		mask = be64_to_cpu(sib->sib_sid_mask);
3478 		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3479 		sib->sib_sid_mask = cpu_to_be64(~0ULL);
3480 		break;
3481 	}
3482 	id_priv->bind_list = bind_list;
3483 	hlist_add_head(&id_priv->node, &bind_list->owners);
3484 }
3485 
3486 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3487 			  struct rdma_id_private *id_priv, unsigned short snum)
3488 {
3489 	struct rdma_bind_list *bind_list;
3490 	int ret;
3491 
3492 	lockdep_assert_held(&lock);
3493 
3494 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3495 	if (!bind_list)
3496 		return -ENOMEM;
3497 
3498 	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3499 			   snum);
3500 	if (ret < 0)
3501 		goto err;
3502 
3503 	bind_list->ps = ps;
3504 	bind_list->port = snum;
3505 	cma_bind_port(bind_list, id_priv);
3506 	return 0;
3507 err:
3508 	kfree(bind_list);
3509 	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3510 }
3511 
3512 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3513 			      struct rdma_id_private *id_priv)
3514 {
3515 	struct rdma_id_private *cur_id;
3516 	struct sockaddr  *daddr = cma_dst_addr(id_priv);
3517 	struct sockaddr  *saddr = cma_src_addr(id_priv);
3518 	__be16 dport = cma_port(daddr);
3519 
3520 	lockdep_assert_held(&lock);
3521 
3522 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3523 		struct sockaddr  *cur_daddr = cma_dst_addr(cur_id);
3524 		struct sockaddr  *cur_saddr = cma_src_addr(cur_id);
3525 		__be16 cur_dport = cma_port(cur_daddr);
3526 
3527 		if (id_priv == cur_id)
3528 			continue;
3529 
3530 		/* different dest port -> unique */
3531 		if (!cma_any_port(daddr) &&
3532 		    !cma_any_port(cur_daddr) &&
3533 		    (dport != cur_dport))
3534 			continue;
3535 
3536 		/* different src address -> unique */
3537 		if (!cma_any_addr(saddr) &&
3538 		    !cma_any_addr(cur_saddr) &&
3539 		    cma_addr_cmp(saddr, cur_saddr))
3540 			continue;
3541 
3542 		/* different dst address -> unique */
3543 		if (!cma_any_addr(daddr) &&
3544 		    !cma_any_addr(cur_daddr) &&
3545 		    cma_addr_cmp(daddr, cur_daddr))
3546 			continue;
3547 
3548 		return -EADDRNOTAVAIL;
3549 	}
3550 	return 0;
3551 }
3552 
3553 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3554 			      struct rdma_id_private *id_priv)
3555 {
3556 	static unsigned int last_used_port;
3557 	int low, high, remaining;
3558 	unsigned int rover;
3559 	struct net *net = id_priv->id.route.addr.dev_addr.net;
3560 
3561 	lockdep_assert_held(&lock);
3562 
3563 	inet_get_local_port_range(net, &low, &high);
3564 	remaining = (high - low) + 1;
3565 	rover = prandom_u32() % remaining + low;
3566 retry:
3567 	if (last_used_port != rover) {
3568 		struct rdma_bind_list *bind_list;
3569 		int ret;
3570 
3571 		bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3572 
3573 		if (!bind_list) {
3574 			ret = cma_alloc_port(ps, id_priv, rover);
3575 		} else {
3576 			ret = cma_port_is_unique(bind_list, id_priv);
3577 			if (!ret)
3578 				cma_bind_port(bind_list, id_priv);
3579 		}
3580 		/*
3581 		 * Remember previously used port number in order to avoid
3582 		 * re-using same port immediately after it is closed.
3583 		 */
3584 		if (!ret)
3585 			last_used_port = rover;
3586 		if (ret != -EADDRNOTAVAIL)
3587 			return ret;
3588 	}
3589 	if (--remaining) {
3590 		rover++;
3591 		if ((rover < low) || (rover > high))
3592 			rover = low;
3593 		goto retry;
3594 	}
3595 	return -EADDRNOTAVAIL;
3596 }
3597 
3598 /*
3599  * Check that the requested port is available.  This is called when trying to
3600  * bind to a specific port, or when trying to listen on a bound port.  In
3601  * the latter case, the provided id_priv may already be on the bind_list, but
3602  * we still need to check that it's okay to start listening.
3603  */
3604 static int cma_check_port(struct rdma_bind_list *bind_list,
3605 			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3606 {
3607 	struct rdma_id_private *cur_id;
3608 	struct sockaddr *addr, *cur_addr;
3609 
3610 	lockdep_assert_held(&lock);
3611 
3612 	addr = cma_src_addr(id_priv);
3613 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3614 		if (id_priv == cur_id)
3615 			continue;
3616 
3617 		if (reuseaddr && cur_id->reuseaddr)
3618 			continue;
3619 
3620 		cur_addr = cma_src_addr(cur_id);
3621 		if (id_priv->afonly && cur_id->afonly &&
3622 		    (addr->sa_family != cur_addr->sa_family))
3623 			continue;
3624 
3625 		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3626 			return -EADDRNOTAVAIL;
3627 
3628 		if (!cma_addr_cmp(addr, cur_addr))
3629 			return -EADDRINUSE;
3630 	}
3631 	return 0;
3632 }
3633 
3634 static int cma_use_port(enum rdma_ucm_port_space ps,
3635 			struct rdma_id_private *id_priv)
3636 {
3637 	struct rdma_bind_list *bind_list;
3638 	unsigned short snum;
3639 	int ret;
3640 
3641 	lockdep_assert_held(&lock);
3642 
3643 	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3644 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3645 		return -EACCES;
3646 
3647 	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3648 	if (!bind_list) {
3649 		ret = cma_alloc_port(ps, id_priv, snum);
3650 	} else {
3651 		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3652 		if (!ret)
3653 			cma_bind_port(bind_list, id_priv);
3654 	}
3655 	return ret;
3656 }
3657 
3658 static enum rdma_ucm_port_space
3659 cma_select_inet_ps(struct rdma_id_private *id_priv)
3660 {
3661 	switch (id_priv->id.ps) {
3662 	case RDMA_PS_TCP:
3663 	case RDMA_PS_UDP:
3664 	case RDMA_PS_IPOIB:
3665 	case RDMA_PS_IB:
3666 		return id_priv->id.ps;
3667 	default:
3668 
3669 		return 0;
3670 	}
3671 }
3672 
3673 static enum rdma_ucm_port_space
3674 cma_select_ib_ps(struct rdma_id_private *id_priv)
3675 {
3676 	enum rdma_ucm_port_space ps = 0;
3677 	struct sockaddr_ib *sib;
3678 	u64 sid_ps, mask, sid;
3679 
3680 	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3681 	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3682 	sid = be64_to_cpu(sib->sib_sid) & mask;
3683 
3684 	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3685 		sid_ps = RDMA_IB_IP_PS_IB;
3686 		ps = RDMA_PS_IB;
3687 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3688 		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3689 		sid_ps = RDMA_IB_IP_PS_TCP;
3690 		ps = RDMA_PS_TCP;
3691 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3692 		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3693 		sid_ps = RDMA_IB_IP_PS_UDP;
3694 		ps = RDMA_PS_UDP;
3695 	}
3696 
3697 	if (ps) {
3698 		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3699 		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3700 						be64_to_cpu(sib->sib_sid_mask));
3701 	}
3702 	return ps;
3703 }
3704 
3705 static int cma_get_port(struct rdma_id_private *id_priv)
3706 {
3707 	enum rdma_ucm_port_space ps;
3708 	int ret;
3709 
3710 	if (cma_family(id_priv) != AF_IB)
3711 		ps = cma_select_inet_ps(id_priv);
3712 	else
3713 		ps = cma_select_ib_ps(id_priv);
3714 	if (!ps)
3715 		return -EPROTONOSUPPORT;
3716 
3717 	mutex_lock(&lock);
3718 	if (cma_any_port(cma_src_addr(id_priv)))
3719 		ret = cma_alloc_any_port(ps, id_priv);
3720 	else
3721 		ret = cma_use_port(ps, id_priv);
3722 	mutex_unlock(&lock);
3723 
3724 	return ret;
3725 }
3726 
3727 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3728 			       struct sockaddr *addr)
3729 {
3730 #if IS_ENABLED(CONFIG_IPV6)
3731 	struct sockaddr_in6 *sin6;
3732 
3733 	if (addr->sa_family != AF_INET6)
3734 		return 0;
3735 
3736 	sin6 = (struct sockaddr_in6 *) addr;
3737 
3738 	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3739 		return 0;
3740 
3741 	if (!sin6->sin6_scope_id)
3742 			return -EINVAL;
3743 
3744 	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3745 #endif
3746 	return 0;
3747 }
3748 
3749 int rdma_listen(struct rdma_cm_id *id, int backlog)
3750 {
3751 	struct rdma_id_private *id_priv =
3752 		container_of(id, struct rdma_id_private, id);
3753 	int ret;
3754 
3755 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3756 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3757 		id->route.addr.src_addr.ss_family = AF_INET;
3758 		ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3759 		if (ret)
3760 			return ret;
3761 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3762 					   RDMA_CM_LISTEN)))
3763 			return -EINVAL;
3764 	}
3765 
3766 	/*
3767 	 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3768 	 * any more, and has to be unique in the bind list.
3769 	 */
3770 	if (id_priv->reuseaddr) {
3771 		mutex_lock(&lock);
3772 		ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3773 		if (!ret)
3774 			id_priv->reuseaddr = 0;
3775 		mutex_unlock(&lock);
3776 		if (ret)
3777 			goto err;
3778 	}
3779 
3780 	id_priv->backlog = backlog;
3781 	if (id_priv->cma_dev) {
3782 		if (rdma_cap_ib_cm(id->device, 1)) {
3783 			ret = cma_ib_listen(id_priv);
3784 			if (ret)
3785 				goto err;
3786 		} else if (rdma_cap_iw_cm(id->device, 1)) {
3787 			ret = cma_iw_listen(id_priv, backlog);
3788 			if (ret)
3789 				goto err;
3790 		} else {
3791 			ret = -ENOSYS;
3792 			goto err;
3793 		}
3794 	} else {
3795 		ret = cma_listen_on_all(id_priv);
3796 		if (ret)
3797 			goto err;
3798 	}
3799 
3800 	return 0;
3801 err:
3802 	id_priv->backlog = 0;
3803 	/*
3804 	 * All the failure paths that lead here will not allow the req_handler's
3805 	 * to have run.
3806 	 */
3807 	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3808 	return ret;
3809 }
3810 EXPORT_SYMBOL(rdma_listen);
3811 
3812 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3813 {
3814 	struct rdma_id_private *id_priv;
3815 	int ret;
3816 	struct sockaddr  *daddr;
3817 
3818 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3819 	    addr->sa_family != AF_IB)
3820 		return -EAFNOSUPPORT;
3821 
3822 	id_priv = container_of(id, struct rdma_id_private, id);
3823 	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3824 		return -EINVAL;
3825 
3826 	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3827 	if (ret)
3828 		goto err1;
3829 
3830 	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3831 	if (!cma_any_addr(addr)) {
3832 		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3833 		if (ret)
3834 			goto err1;
3835 
3836 		ret = cma_acquire_dev_by_src_ip(id_priv);
3837 		if (ret)
3838 			goto err1;
3839 	}
3840 
3841 	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3842 		if (addr->sa_family == AF_INET)
3843 			id_priv->afonly = 1;
3844 #if IS_ENABLED(CONFIG_IPV6)
3845 		else if (addr->sa_family == AF_INET6) {
3846 			struct net *net = id_priv->id.route.addr.dev_addr.net;
3847 
3848 			id_priv->afonly = net->ipv6.sysctl.bindv6only;
3849 		}
3850 #endif
3851 	}
3852 	daddr = cma_dst_addr(id_priv);
3853 	daddr->sa_family = addr->sa_family;
3854 
3855 	ret = cma_get_port(id_priv);
3856 	if (ret)
3857 		goto err2;
3858 
3859 	if (!cma_any_addr(addr))
3860 		rdma_restrack_add(&id_priv->res);
3861 	return 0;
3862 err2:
3863 	if (id_priv->cma_dev)
3864 		cma_release_dev(id_priv);
3865 err1:
3866 	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3867 	return ret;
3868 }
3869 EXPORT_SYMBOL(rdma_bind_addr);
3870 
3871 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3872 {
3873 	struct cma_hdr *cma_hdr;
3874 
3875 	cma_hdr = hdr;
3876 	cma_hdr->cma_version = CMA_VERSION;
3877 	if (cma_family(id_priv) == AF_INET) {
3878 		struct sockaddr_in *src4, *dst4;
3879 
3880 		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3881 		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3882 
3883 		cma_set_ip_ver(cma_hdr, 4);
3884 		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3885 		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3886 		cma_hdr->port = src4->sin_port;
3887 	} else if (cma_family(id_priv) == AF_INET6) {
3888 		struct sockaddr_in6 *src6, *dst6;
3889 
3890 		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3891 		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3892 
3893 		cma_set_ip_ver(cma_hdr, 6);
3894 		cma_hdr->src_addr.ip6 = src6->sin6_addr;
3895 		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3896 		cma_hdr->port = src6->sin6_port;
3897 	}
3898 	return 0;
3899 }
3900 
3901 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3902 				const struct ib_cm_event *ib_event)
3903 {
3904 	struct rdma_id_private *id_priv = cm_id->context;
3905 	struct rdma_cm_event event = {};
3906 	const struct ib_cm_sidr_rep_event_param *rep =
3907 				&ib_event->param.sidr_rep_rcvd;
3908 	int ret;
3909 
3910 	mutex_lock(&id_priv->handler_mutex);
3911 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
3912 		goto out;
3913 
3914 	switch (ib_event->event) {
3915 	case IB_CM_SIDR_REQ_ERROR:
3916 		event.event = RDMA_CM_EVENT_UNREACHABLE;
3917 		event.status = -ETIMEDOUT;
3918 		break;
3919 	case IB_CM_SIDR_REP_RECEIVED:
3920 		event.param.ud.private_data = ib_event->private_data;
3921 		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3922 		if (rep->status != IB_SIDR_SUCCESS) {
3923 			event.event = RDMA_CM_EVENT_UNREACHABLE;
3924 			event.status = ib_event->param.sidr_rep_rcvd.status;
3925 			pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3926 					     event.status);
3927 			break;
3928 		}
3929 		ret = cma_set_qkey(id_priv, rep->qkey);
3930 		if (ret) {
3931 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3932 			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3933 			event.status = ret;
3934 			break;
3935 		}
3936 		ib_init_ah_attr_from_path(id_priv->id.device,
3937 					  id_priv->id.port_num,
3938 					  id_priv->id.route.path_rec,
3939 					  &event.param.ud.ah_attr,
3940 					  rep->sgid_attr);
3941 		event.param.ud.qp_num = rep->qpn;
3942 		event.param.ud.qkey = rep->qkey;
3943 		event.event = RDMA_CM_EVENT_ESTABLISHED;
3944 		event.status = 0;
3945 		break;
3946 	default:
3947 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3948 		       ib_event->event);
3949 		goto out;
3950 	}
3951 
3952 	ret = cma_cm_event_handler(id_priv, &event);
3953 
3954 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
3955 	if (ret) {
3956 		/* Destroy the CM ID by returning a non-zero value. */
3957 		id_priv->cm_id.ib = NULL;
3958 		destroy_id_handler_unlock(id_priv);
3959 		return ret;
3960 	}
3961 out:
3962 	mutex_unlock(&id_priv->handler_mutex);
3963 	return 0;
3964 }
3965 
3966 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3967 			      struct rdma_conn_param *conn_param)
3968 {
3969 	struct ib_cm_sidr_req_param req;
3970 	struct ib_cm_id	*id;
3971 	void *private_data;
3972 	u8 offset;
3973 	int ret;
3974 
3975 	memset(&req, 0, sizeof req);
3976 	offset = cma_user_data_offset(id_priv);
3977 	req.private_data_len = offset + conn_param->private_data_len;
3978 	if (req.private_data_len < conn_param->private_data_len)
3979 		return -EINVAL;
3980 
3981 	if (req.private_data_len) {
3982 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3983 		if (!private_data)
3984 			return -ENOMEM;
3985 	} else {
3986 		private_data = NULL;
3987 	}
3988 
3989 	if (conn_param->private_data && conn_param->private_data_len)
3990 		memcpy(private_data + offset, conn_param->private_data,
3991 		       conn_param->private_data_len);
3992 
3993 	if (private_data) {
3994 		ret = cma_format_hdr(private_data, id_priv);
3995 		if (ret)
3996 			goto out;
3997 		req.private_data = private_data;
3998 	}
3999 
4000 	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
4001 			     id_priv);
4002 	if (IS_ERR(id)) {
4003 		ret = PTR_ERR(id);
4004 		goto out;
4005 	}
4006 	id_priv->cm_id.ib = id;
4007 
4008 	req.path = id_priv->id.route.path_rec;
4009 	req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4010 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4011 	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4012 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4013 
4014 	trace_cm_send_sidr_req(id_priv);
4015 	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4016 	if (ret) {
4017 		ib_destroy_cm_id(id_priv->cm_id.ib);
4018 		id_priv->cm_id.ib = NULL;
4019 	}
4020 out:
4021 	kfree(private_data);
4022 	return ret;
4023 }
4024 
4025 static int cma_connect_ib(struct rdma_id_private *id_priv,
4026 			  struct rdma_conn_param *conn_param)
4027 {
4028 	struct ib_cm_req_param req;
4029 	struct rdma_route *route;
4030 	void *private_data;
4031 	struct ib_cm_id	*id;
4032 	u8 offset;
4033 	int ret;
4034 
4035 	memset(&req, 0, sizeof req);
4036 	offset = cma_user_data_offset(id_priv);
4037 	req.private_data_len = offset + conn_param->private_data_len;
4038 	if (req.private_data_len < conn_param->private_data_len)
4039 		return -EINVAL;
4040 
4041 	if (req.private_data_len) {
4042 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4043 		if (!private_data)
4044 			return -ENOMEM;
4045 	} else {
4046 		private_data = NULL;
4047 	}
4048 
4049 	if (conn_param->private_data && conn_param->private_data_len)
4050 		memcpy(private_data + offset, conn_param->private_data,
4051 		       conn_param->private_data_len);
4052 
4053 	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
4054 	if (IS_ERR(id)) {
4055 		ret = PTR_ERR(id);
4056 		goto out;
4057 	}
4058 	id_priv->cm_id.ib = id;
4059 
4060 	route = &id_priv->id.route;
4061 	if (private_data) {
4062 		ret = cma_format_hdr(private_data, id_priv);
4063 		if (ret)
4064 			goto out;
4065 		req.private_data = private_data;
4066 	}
4067 
4068 	req.primary_path = &route->path_rec[0];
4069 	if (route->num_paths == 2)
4070 		req.alternate_path = &route->path_rec[1];
4071 
4072 	req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4073 	/* Alternate path SGID attribute currently unsupported */
4074 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4075 	req.qp_num = id_priv->qp_num;
4076 	req.qp_type = id_priv->id.qp_type;
4077 	req.starting_psn = id_priv->seq_num;
4078 	req.responder_resources = conn_param->responder_resources;
4079 	req.initiator_depth = conn_param->initiator_depth;
4080 	req.flow_control = conn_param->flow_control;
4081 	req.retry_count = min_t(u8, 7, conn_param->retry_count);
4082 	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4083 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4084 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4085 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4086 	req.srq = id_priv->srq ? 1 : 0;
4087 	req.ece.vendor_id = id_priv->ece.vendor_id;
4088 	req.ece.attr_mod = id_priv->ece.attr_mod;
4089 
4090 	trace_cm_send_req(id_priv);
4091 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4092 out:
4093 	if (ret && !IS_ERR(id)) {
4094 		ib_destroy_cm_id(id);
4095 		id_priv->cm_id.ib = NULL;
4096 	}
4097 
4098 	kfree(private_data);
4099 	return ret;
4100 }
4101 
4102 static int cma_connect_iw(struct rdma_id_private *id_priv,
4103 			  struct rdma_conn_param *conn_param)
4104 {
4105 	struct iw_cm_id *cm_id;
4106 	int ret;
4107 	struct iw_cm_conn_param iw_param;
4108 
4109 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4110 	if (IS_ERR(cm_id))
4111 		return PTR_ERR(cm_id);
4112 
4113 	mutex_lock(&id_priv->qp_mutex);
4114 	cm_id->tos = id_priv->tos;
4115 	cm_id->tos_set = id_priv->tos_set;
4116 	mutex_unlock(&id_priv->qp_mutex);
4117 
4118 	id_priv->cm_id.iw = cm_id;
4119 
4120 	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4121 	       rdma_addr_size(cma_src_addr(id_priv)));
4122 	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4123 	       rdma_addr_size(cma_dst_addr(id_priv)));
4124 
4125 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4126 	if (ret)
4127 		goto out;
4128 
4129 	if (conn_param) {
4130 		iw_param.ord = conn_param->initiator_depth;
4131 		iw_param.ird = conn_param->responder_resources;
4132 		iw_param.private_data = conn_param->private_data;
4133 		iw_param.private_data_len = conn_param->private_data_len;
4134 		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4135 	} else {
4136 		memset(&iw_param, 0, sizeof iw_param);
4137 		iw_param.qpn = id_priv->qp_num;
4138 	}
4139 	ret = iw_cm_connect(cm_id, &iw_param);
4140 out:
4141 	if (ret) {
4142 		iw_destroy_cm_id(cm_id);
4143 		id_priv->cm_id.iw = NULL;
4144 	}
4145 	return ret;
4146 }
4147 
4148 /**
4149  * rdma_connect_locked - Initiate an active connection request.
4150  * @id: Connection identifier to connect.
4151  * @conn_param: Connection information used for connected QPs.
4152  *
4153  * Same as rdma_connect() but can only be called from the
4154  * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4155  */
4156 int rdma_connect_locked(struct rdma_cm_id *id,
4157 			struct rdma_conn_param *conn_param)
4158 {
4159 	struct rdma_id_private *id_priv =
4160 		container_of(id, struct rdma_id_private, id);
4161 	int ret;
4162 
4163 	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4164 		return -EINVAL;
4165 
4166 	if (!id->qp) {
4167 		id_priv->qp_num = conn_param->qp_num;
4168 		id_priv->srq = conn_param->srq;
4169 	}
4170 
4171 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4172 		if (id->qp_type == IB_QPT_UD)
4173 			ret = cma_resolve_ib_udp(id_priv, conn_param);
4174 		else
4175 			ret = cma_connect_ib(id_priv, conn_param);
4176 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4177 		ret = cma_connect_iw(id_priv, conn_param);
4178 	} else {
4179 		ret = -ENOSYS;
4180 	}
4181 	if (ret)
4182 		goto err_state;
4183 	return 0;
4184 err_state:
4185 	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4186 	return ret;
4187 }
4188 EXPORT_SYMBOL(rdma_connect_locked);
4189 
4190 /**
4191  * rdma_connect - Initiate an active connection request.
4192  * @id: Connection identifier to connect.
4193  * @conn_param: Connection information used for connected QPs.
4194  *
4195  * Users must have resolved a route for the rdma_cm_id to connect with by having
4196  * called rdma_resolve_route before calling this routine.
4197  *
4198  * This call will either connect to a remote QP or obtain remote QP information
4199  * for unconnected rdma_cm_id's.  The actual operation is based on the
4200  * rdma_cm_id's port space.
4201  */
4202 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4203 {
4204 	struct rdma_id_private *id_priv =
4205 		container_of(id, struct rdma_id_private, id);
4206 	int ret;
4207 
4208 	mutex_lock(&id_priv->handler_mutex);
4209 	ret = rdma_connect_locked(id, conn_param);
4210 	mutex_unlock(&id_priv->handler_mutex);
4211 	return ret;
4212 }
4213 EXPORT_SYMBOL(rdma_connect);
4214 
4215 /**
4216  * rdma_connect_ece - Initiate an active connection request with ECE data.
4217  * @id: Connection identifier to connect.
4218  * @conn_param: Connection information used for connected QPs.
4219  * @ece: ECE parameters
4220  *
4221  * See rdma_connect() explanation.
4222  */
4223 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4224 		     struct rdma_ucm_ece *ece)
4225 {
4226 	struct rdma_id_private *id_priv =
4227 		container_of(id, struct rdma_id_private, id);
4228 
4229 	id_priv->ece.vendor_id = ece->vendor_id;
4230 	id_priv->ece.attr_mod = ece->attr_mod;
4231 
4232 	return rdma_connect(id, conn_param);
4233 }
4234 EXPORT_SYMBOL(rdma_connect_ece);
4235 
4236 static int cma_accept_ib(struct rdma_id_private *id_priv,
4237 			 struct rdma_conn_param *conn_param)
4238 {
4239 	struct ib_cm_rep_param rep;
4240 	int ret;
4241 
4242 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4243 	if (ret)
4244 		goto out;
4245 
4246 	ret = cma_modify_qp_rts(id_priv, conn_param);
4247 	if (ret)
4248 		goto out;
4249 
4250 	memset(&rep, 0, sizeof rep);
4251 	rep.qp_num = id_priv->qp_num;
4252 	rep.starting_psn = id_priv->seq_num;
4253 	rep.private_data = conn_param->private_data;
4254 	rep.private_data_len = conn_param->private_data_len;
4255 	rep.responder_resources = conn_param->responder_resources;
4256 	rep.initiator_depth = conn_param->initiator_depth;
4257 	rep.failover_accepted = 0;
4258 	rep.flow_control = conn_param->flow_control;
4259 	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4260 	rep.srq = id_priv->srq ? 1 : 0;
4261 	rep.ece.vendor_id = id_priv->ece.vendor_id;
4262 	rep.ece.attr_mod = id_priv->ece.attr_mod;
4263 
4264 	trace_cm_send_rep(id_priv);
4265 	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4266 out:
4267 	return ret;
4268 }
4269 
4270 static int cma_accept_iw(struct rdma_id_private *id_priv,
4271 		  struct rdma_conn_param *conn_param)
4272 {
4273 	struct iw_cm_conn_param iw_param;
4274 	int ret;
4275 
4276 	if (!conn_param)
4277 		return -EINVAL;
4278 
4279 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4280 	if (ret)
4281 		return ret;
4282 
4283 	iw_param.ord = conn_param->initiator_depth;
4284 	iw_param.ird = conn_param->responder_resources;
4285 	iw_param.private_data = conn_param->private_data;
4286 	iw_param.private_data_len = conn_param->private_data_len;
4287 	if (id_priv->id.qp)
4288 		iw_param.qpn = id_priv->qp_num;
4289 	else
4290 		iw_param.qpn = conn_param->qp_num;
4291 
4292 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4293 }
4294 
4295 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4296 			     enum ib_cm_sidr_status status, u32 qkey,
4297 			     const void *private_data, int private_data_len)
4298 {
4299 	struct ib_cm_sidr_rep_param rep;
4300 	int ret;
4301 
4302 	memset(&rep, 0, sizeof rep);
4303 	rep.status = status;
4304 	if (status == IB_SIDR_SUCCESS) {
4305 		ret = cma_set_qkey(id_priv, qkey);
4306 		if (ret)
4307 			return ret;
4308 		rep.qp_num = id_priv->qp_num;
4309 		rep.qkey = id_priv->qkey;
4310 
4311 		rep.ece.vendor_id = id_priv->ece.vendor_id;
4312 		rep.ece.attr_mod = id_priv->ece.attr_mod;
4313 	}
4314 
4315 	rep.private_data = private_data;
4316 	rep.private_data_len = private_data_len;
4317 
4318 	trace_cm_send_sidr_rep(id_priv);
4319 	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4320 }
4321 
4322 /**
4323  * rdma_accept - Called to accept a connection request or response.
4324  * @id: Connection identifier associated with the request.
4325  * @conn_param: Information needed to establish the connection.  This must be
4326  *   provided if accepting a connection request.  If accepting a connection
4327  *   response, this parameter must be NULL.
4328  *
4329  * Typically, this routine is only called by the listener to accept a connection
4330  * request.  It must also be called on the active side of a connection if the
4331  * user is performing their own QP transitions.
4332  *
4333  * In the case of error, a reject message is sent to the remote side and the
4334  * state of the qp associated with the id is modified to error, such that any
4335  * previously posted receive buffers would be flushed.
4336  *
4337  * This function is for use by kernel ULPs and must be called from under the
4338  * handler callback.
4339  */
4340 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4341 {
4342 	struct rdma_id_private *id_priv =
4343 		container_of(id, struct rdma_id_private, id);
4344 	int ret;
4345 
4346 	lockdep_assert_held(&id_priv->handler_mutex);
4347 
4348 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4349 		return -EINVAL;
4350 
4351 	if (!id->qp && conn_param) {
4352 		id_priv->qp_num = conn_param->qp_num;
4353 		id_priv->srq = conn_param->srq;
4354 	}
4355 
4356 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4357 		if (id->qp_type == IB_QPT_UD) {
4358 			if (conn_param)
4359 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4360 							conn_param->qkey,
4361 							conn_param->private_data,
4362 							conn_param->private_data_len);
4363 			else
4364 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4365 							0, NULL, 0);
4366 		} else {
4367 			if (conn_param)
4368 				ret = cma_accept_ib(id_priv, conn_param);
4369 			else
4370 				ret = cma_rep_recv(id_priv);
4371 		}
4372 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4373 		ret = cma_accept_iw(id_priv, conn_param);
4374 	} else {
4375 		ret = -ENOSYS;
4376 	}
4377 	if (ret)
4378 		goto reject;
4379 
4380 	return 0;
4381 reject:
4382 	cma_modify_qp_err(id_priv);
4383 	rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4384 	return ret;
4385 }
4386 EXPORT_SYMBOL(rdma_accept);
4387 
4388 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4389 		    struct rdma_ucm_ece *ece)
4390 {
4391 	struct rdma_id_private *id_priv =
4392 		container_of(id, struct rdma_id_private, id);
4393 
4394 	id_priv->ece.vendor_id = ece->vendor_id;
4395 	id_priv->ece.attr_mod = ece->attr_mod;
4396 
4397 	return rdma_accept(id, conn_param);
4398 }
4399 EXPORT_SYMBOL(rdma_accept_ece);
4400 
4401 void rdma_lock_handler(struct rdma_cm_id *id)
4402 {
4403 	struct rdma_id_private *id_priv =
4404 		container_of(id, struct rdma_id_private, id);
4405 
4406 	mutex_lock(&id_priv->handler_mutex);
4407 }
4408 EXPORT_SYMBOL(rdma_lock_handler);
4409 
4410 void rdma_unlock_handler(struct rdma_cm_id *id)
4411 {
4412 	struct rdma_id_private *id_priv =
4413 		container_of(id, struct rdma_id_private, id);
4414 
4415 	mutex_unlock(&id_priv->handler_mutex);
4416 }
4417 EXPORT_SYMBOL(rdma_unlock_handler);
4418 
4419 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4420 {
4421 	struct rdma_id_private *id_priv;
4422 	int ret;
4423 
4424 	id_priv = container_of(id, struct rdma_id_private, id);
4425 	if (!id_priv->cm_id.ib)
4426 		return -EINVAL;
4427 
4428 	switch (id->device->node_type) {
4429 	case RDMA_NODE_IB_CA:
4430 		ret = ib_cm_notify(id_priv->cm_id.ib, event);
4431 		break;
4432 	default:
4433 		ret = 0;
4434 		break;
4435 	}
4436 	return ret;
4437 }
4438 EXPORT_SYMBOL(rdma_notify);
4439 
4440 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4441 		u8 private_data_len, u8 reason)
4442 {
4443 	struct rdma_id_private *id_priv;
4444 	int ret;
4445 
4446 	id_priv = container_of(id, struct rdma_id_private, id);
4447 	if (!id_priv->cm_id.ib)
4448 		return -EINVAL;
4449 
4450 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4451 		if (id->qp_type == IB_QPT_UD) {
4452 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4453 						private_data, private_data_len);
4454 		} else {
4455 			trace_cm_send_rej(id_priv);
4456 			ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4457 					     private_data, private_data_len);
4458 		}
4459 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4460 		ret = iw_cm_reject(id_priv->cm_id.iw,
4461 				   private_data, private_data_len);
4462 	} else {
4463 		ret = -ENOSYS;
4464 	}
4465 
4466 	return ret;
4467 }
4468 EXPORT_SYMBOL(rdma_reject);
4469 
4470 int rdma_disconnect(struct rdma_cm_id *id)
4471 {
4472 	struct rdma_id_private *id_priv;
4473 	int ret;
4474 
4475 	id_priv = container_of(id, struct rdma_id_private, id);
4476 	if (!id_priv->cm_id.ib)
4477 		return -EINVAL;
4478 
4479 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4480 		ret = cma_modify_qp_err(id_priv);
4481 		if (ret)
4482 			goto out;
4483 		/* Initiate or respond to a disconnect. */
4484 		trace_cm_disconnect(id_priv);
4485 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4486 			if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4487 				trace_cm_sent_drep(id_priv);
4488 		} else {
4489 			trace_cm_sent_dreq(id_priv);
4490 		}
4491 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4492 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4493 	} else
4494 		ret = -EINVAL;
4495 
4496 out:
4497 	return ret;
4498 }
4499 EXPORT_SYMBOL(rdma_disconnect);
4500 
4501 static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4502 			      struct ib_sa_multicast *multicast,
4503 			      struct rdma_cm_event *event,
4504 			      struct cma_multicast *mc)
4505 {
4506 	struct rdma_dev_addr *dev_addr;
4507 	enum ib_gid_type gid_type;
4508 	struct net_device *ndev;
4509 
4510 	if (!status)
4511 		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4512 	else
4513 		pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4514 				     status);
4515 
4516 	event->status = status;
4517 	event->param.ud.private_data = mc->context;
4518 	if (status) {
4519 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4520 		return;
4521 	}
4522 
4523 	dev_addr = &id_priv->id.route.addr.dev_addr;
4524 	ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4525 	gid_type =
4526 		id_priv->cma_dev
4527 			->default_gid_type[id_priv->id.port_num -
4528 					   rdma_start_port(
4529 						   id_priv->cma_dev->device)];
4530 
4531 	event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4532 	if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4533 				     &multicast->rec, ndev, gid_type,
4534 				     &event->param.ud.ah_attr)) {
4535 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4536 		goto out;
4537 	}
4538 
4539 	event->param.ud.qp_num = 0xFFFFFF;
4540 	event->param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
4541 
4542 out:
4543 	if (ndev)
4544 		dev_put(ndev);
4545 }
4546 
4547 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4548 {
4549 	struct cma_multicast *mc = multicast->context;
4550 	struct rdma_id_private *id_priv = mc->id_priv;
4551 	struct rdma_cm_event event = {};
4552 	int ret = 0;
4553 
4554 	mutex_lock(&id_priv->handler_mutex);
4555 	if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4556 	    READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4557 		goto out;
4558 
4559 	cma_make_mc_event(status, id_priv, multicast, &event, mc);
4560 	ret = cma_cm_event_handler(id_priv, &event);
4561 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4562 	WARN_ON(ret);
4563 
4564 out:
4565 	mutex_unlock(&id_priv->handler_mutex);
4566 	return 0;
4567 }
4568 
4569 static void cma_set_mgid(struct rdma_id_private *id_priv,
4570 			 struct sockaddr *addr, union ib_gid *mgid)
4571 {
4572 	unsigned char mc_map[MAX_ADDR_LEN];
4573 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4574 	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4575 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4576 
4577 	if (cma_any_addr(addr)) {
4578 		memset(mgid, 0, sizeof *mgid);
4579 	} else if ((addr->sa_family == AF_INET6) &&
4580 		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4581 								 0xFF10A01B)) {
4582 		/* IPv6 address is an SA assigned MGID. */
4583 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4584 	} else if (addr->sa_family == AF_IB) {
4585 		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4586 	} else if (addr->sa_family == AF_INET6) {
4587 		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4588 		if (id_priv->id.ps == RDMA_PS_UDP)
4589 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4590 		*mgid = *(union ib_gid *) (mc_map + 4);
4591 	} else {
4592 		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4593 		if (id_priv->id.ps == RDMA_PS_UDP)
4594 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4595 		*mgid = *(union ib_gid *) (mc_map + 4);
4596 	}
4597 }
4598 
4599 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4600 				 struct cma_multicast *mc)
4601 {
4602 	struct ib_sa_mcmember_rec rec;
4603 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4604 	ib_sa_comp_mask comp_mask;
4605 	int ret;
4606 
4607 	ib_addr_get_mgid(dev_addr, &rec.mgid);
4608 	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4609 				     &rec.mgid, &rec);
4610 	if (ret)
4611 		return ret;
4612 
4613 	ret = cma_set_qkey(id_priv, 0);
4614 	if (ret)
4615 		return ret;
4616 
4617 	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4618 	rec.qkey = cpu_to_be32(id_priv->qkey);
4619 	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4620 	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4621 	rec.join_state = mc->join_state;
4622 
4623 	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4624 		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4625 		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4626 		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
4627 		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4628 
4629 	if (id_priv->id.ps == RDMA_PS_IPOIB)
4630 		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4631 			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4632 			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4633 			     IB_SA_MCMEMBER_REC_MTU |
4634 			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
4635 
4636 	mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4637 					 id_priv->id.port_num, &rec, comp_mask,
4638 					 GFP_KERNEL, cma_ib_mc_handler, mc);
4639 	return PTR_ERR_OR_ZERO(mc->sa_mc);
4640 }
4641 
4642 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4643 			      enum ib_gid_type gid_type)
4644 {
4645 	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4646 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4647 
4648 	if (cma_any_addr(addr)) {
4649 		memset(mgid, 0, sizeof *mgid);
4650 	} else if (addr->sa_family == AF_INET6) {
4651 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4652 	} else {
4653 		mgid->raw[0] =
4654 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4655 		mgid->raw[1] =
4656 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4657 		mgid->raw[2] = 0;
4658 		mgid->raw[3] = 0;
4659 		mgid->raw[4] = 0;
4660 		mgid->raw[5] = 0;
4661 		mgid->raw[6] = 0;
4662 		mgid->raw[7] = 0;
4663 		mgid->raw[8] = 0;
4664 		mgid->raw[9] = 0;
4665 		mgid->raw[10] = 0xff;
4666 		mgid->raw[11] = 0xff;
4667 		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4668 	}
4669 }
4670 
4671 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4672 				   struct cma_multicast *mc)
4673 {
4674 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4675 	int err = 0;
4676 	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4677 	struct net_device *ndev = NULL;
4678 	struct ib_sa_multicast ib;
4679 	enum ib_gid_type gid_type;
4680 	bool send_only;
4681 
4682 	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4683 
4684 	if (cma_zero_addr(addr))
4685 		return -EINVAL;
4686 
4687 	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4688 		   rdma_start_port(id_priv->cma_dev->device)];
4689 	cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
4690 
4691 	ib.rec.pkey = cpu_to_be16(0xffff);
4692 	if (id_priv->id.ps == RDMA_PS_UDP)
4693 		ib.rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4694 
4695 	if (dev_addr->bound_dev_if)
4696 		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4697 	if (!ndev)
4698 		return -ENODEV;
4699 
4700 	ib.rec.rate = iboe_get_rate(ndev);
4701 	ib.rec.hop_limit = 1;
4702 	ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4703 
4704 	if (addr->sa_family == AF_INET) {
4705 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4706 			ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4707 			if (!send_only) {
4708 				err = cma_igmp_send(ndev, &ib.rec.mgid,
4709 						    true);
4710 			}
4711 		}
4712 	} else {
4713 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4714 			err = -ENOTSUPP;
4715 	}
4716 	dev_put(ndev);
4717 	if (err || !ib.rec.mtu)
4718 		return err ?: -EINVAL;
4719 
4720 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4721 		    &ib.rec.port_gid);
4722 	INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
4723 	cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
4724 	queue_work(cma_wq, &mc->iboe_join.work);
4725 	return 0;
4726 }
4727 
4728 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4729 			u8 join_state, void *context)
4730 {
4731 	struct rdma_id_private *id_priv =
4732 		container_of(id, struct rdma_id_private, id);
4733 	struct cma_multicast *mc;
4734 	int ret;
4735 
4736 	/* Not supported for kernel QPs */
4737 	if (WARN_ON(id->qp))
4738 		return -EINVAL;
4739 
4740 	/* ULP is calling this wrong. */
4741 	if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
4742 			    READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
4743 		return -EINVAL;
4744 
4745 	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
4746 	if (!mc)
4747 		return -ENOMEM;
4748 
4749 	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4750 	mc->context = context;
4751 	mc->id_priv = id_priv;
4752 	mc->join_state = join_state;
4753 
4754 	if (rdma_protocol_roce(id->device, id->port_num)) {
4755 		ret = cma_iboe_join_multicast(id_priv, mc);
4756 		if (ret)
4757 			goto out_err;
4758 	} else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4759 		ret = cma_join_ib_multicast(id_priv, mc);
4760 		if (ret)
4761 			goto out_err;
4762 	} else {
4763 		ret = -ENOSYS;
4764 		goto out_err;
4765 	}
4766 
4767 	spin_lock(&id_priv->lock);
4768 	list_add(&mc->list, &id_priv->mc_list);
4769 	spin_unlock(&id_priv->lock);
4770 
4771 	return 0;
4772 out_err:
4773 	kfree(mc);
4774 	return ret;
4775 }
4776 EXPORT_SYMBOL(rdma_join_multicast);
4777 
4778 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4779 {
4780 	struct rdma_id_private *id_priv;
4781 	struct cma_multicast *mc;
4782 
4783 	id_priv = container_of(id, struct rdma_id_private, id);
4784 	spin_lock_irq(&id_priv->lock);
4785 	list_for_each_entry(mc, &id_priv->mc_list, list) {
4786 		if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
4787 			continue;
4788 		list_del(&mc->list);
4789 		spin_unlock_irq(&id_priv->lock);
4790 
4791 		WARN_ON(id_priv->cma_dev->device != id->device);
4792 		destroy_mc(id_priv, mc);
4793 		return;
4794 	}
4795 	spin_unlock_irq(&id_priv->lock);
4796 }
4797 EXPORT_SYMBOL(rdma_leave_multicast);
4798 
4799 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4800 {
4801 	struct rdma_dev_addr *dev_addr;
4802 	struct cma_work *work;
4803 
4804 	dev_addr = &id_priv->id.route.addr.dev_addr;
4805 
4806 	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4807 	    (net_eq(dev_net(ndev), dev_addr->net)) &&
4808 	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4809 		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4810 			ndev->name, &id_priv->id);
4811 		work = kzalloc(sizeof *work, GFP_KERNEL);
4812 		if (!work)
4813 			return -ENOMEM;
4814 
4815 		INIT_WORK(&work->work, cma_work_handler);
4816 		work->id = id_priv;
4817 		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4818 		cma_id_get(id_priv);
4819 		queue_work(cma_wq, &work->work);
4820 	}
4821 
4822 	return 0;
4823 }
4824 
4825 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4826 			       void *ptr)
4827 {
4828 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4829 	struct cma_device *cma_dev;
4830 	struct rdma_id_private *id_priv;
4831 	int ret = NOTIFY_DONE;
4832 
4833 	if (event != NETDEV_BONDING_FAILOVER)
4834 		return NOTIFY_DONE;
4835 
4836 	if (!netif_is_bond_master(ndev))
4837 		return NOTIFY_DONE;
4838 
4839 	mutex_lock(&lock);
4840 	list_for_each_entry(cma_dev, &dev_list, list)
4841 		list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4842 			ret = cma_netdev_change(ndev, id_priv);
4843 			if (ret)
4844 				goto out;
4845 		}
4846 
4847 out:
4848 	mutex_unlock(&lock);
4849 	return ret;
4850 }
4851 
4852 static struct notifier_block cma_nb = {
4853 	.notifier_call = cma_netdev_callback
4854 };
4855 
4856 static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
4857 {
4858 	struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
4859 	enum rdma_cm_state state;
4860 	unsigned long flags;
4861 
4862 	mutex_lock(&id_priv->handler_mutex);
4863 	/* Record that we want to remove the device */
4864 	spin_lock_irqsave(&id_priv->lock, flags);
4865 	state = id_priv->state;
4866 	if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
4867 		spin_unlock_irqrestore(&id_priv->lock, flags);
4868 		mutex_unlock(&id_priv->handler_mutex);
4869 		cma_id_put(id_priv);
4870 		return;
4871 	}
4872 	id_priv->state = RDMA_CM_DEVICE_REMOVAL;
4873 	spin_unlock_irqrestore(&id_priv->lock, flags);
4874 
4875 	if (cma_cm_event_handler(id_priv, &event)) {
4876 		/*
4877 		 * At this point the ULP promises it won't call
4878 		 * rdma_destroy_id() concurrently
4879 		 */
4880 		cma_id_put(id_priv);
4881 		mutex_unlock(&id_priv->handler_mutex);
4882 		trace_cm_id_destroy(id_priv);
4883 		_destroy_id(id_priv, state);
4884 		return;
4885 	}
4886 	mutex_unlock(&id_priv->handler_mutex);
4887 
4888 	/*
4889 	 * If this races with destroy then the thread that first assigns state
4890 	 * to a destroying does the cancel.
4891 	 */
4892 	cma_cancel_operation(id_priv, state);
4893 	cma_id_put(id_priv);
4894 }
4895 
4896 static void cma_process_remove(struct cma_device *cma_dev)
4897 {
4898 	mutex_lock(&lock);
4899 	while (!list_empty(&cma_dev->id_list)) {
4900 		struct rdma_id_private *id_priv = list_first_entry(
4901 			&cma_dev->id_list, struct rdma_id_private, list);
4902 
4903 		list_del(&id_priv->listen_list);
4904 		list_del_init(&id_priv->list);
4905 		cma_id_get(id_priv);
4906 		mutex_unlock(&lock);
4907 
4908 		cma_send_device_removal_put(id_priv);
4909 
4910 		mutex_lock(&lock);
4911 	}
4912 	mutex_unlock(&lock);
4913 
4914 	cma_dev_put(cma_dev);
4915 	wait_for_completion(&cma_dev->comp);
4916 }
4917 
4918 static bool cma_supported(struct ib_device *device)
4919 {
4920 	u32 i;
4921 
4922 	rdma_for_each_port(device, i) {
4923 		if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
4924 			return true;
4925 	}
4926 	return false;
4927 }
4928 
4929 static int cma_add_one(struct ib_device *device)
4930 {
4931 	struct rdma_id_private *to_destroy;
4932 	struct cma_device *cma_dev;
4933 	struct rdma_id_private *id_priv;
4934 	unsigned long supported_gids = 0;
4935 	int ret;
4936 	u32 i;
4937 
4938 	if (!cma_supported(device))
4939 		return -EOPNOTSUPP;
4940 
4941 	cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
4942 	if (!cma_dev)
4943 		return -ENOMEM;
4944 
4945 	cma_dev->device = device;
4946 	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4947 					    sizeof(*cma_dev->default_gid_type),
4948 					    GFP_KERNEL);
4949 	if (!cma_dev->default_gid_type) {
4950 		ret = -ENOMEM;
4951 		goto free_cma_dev;
4952 	}
4953 
4954 	cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
4955 					    sizeof(*cma_dev->default_roce_tos),
4956 					    GFP_KERNEL);
4957 	if (!cma_dev->default_roce_tos) {
4958 		ret = -ENOMEM;
4959 		goto free_gid_type;
4960 	}
4961 
4962 	rdma_for_each_port (device, i) {
4963 		supported_gids = roce_gid_type_mask_support(device, i);
4964 		WARN_ON(!supported_gids);
4965 		if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
4966 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
4967 				CMA_PREFERRED_ROCE_GID_TYPE;
4968 		else
4969 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
4970 				find_first_bit(&supported_gids, BITS_PER_LONG);
4971 		cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
4972 	}
4973 
4974 	init_completion(&cma_dev->comp);
4975 	refcount_set(&cma_dev->refcount, 1);
4976 	INIT_LIST_HEAD(&cma_dev->id_list);
4977 	ib_set_client_data(device, &cma_client, cma_dev);
4978 
4979 	mutex_lock(&lock);
4980 	list_add_tail(&cma_dev->list, &dev_list);
4981 	list_for_each_entry(id_priv, &listen_any_list, list) {
4982 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
4983 		if (ret)
4984 			goto free_listen;
4985 	}
4986 	mutex_unlock(&lock);
4987 
4988 	trace_cm_add_one(device);
4989 	return 0;
4990 
4991 free_listen:
4992 	list_del(&cma_dev->list);
4993 	mutex_unlock(&lock);
4994 
4995 	/* cma_process_remove() will delete to_destroy */
4996 	cma_process_remove(cma_dev);
4997 	kfree(cma_dev->default_roce_tos);
4998 free_gid_type:
4999 	kfree(cma_dev->default_gid_type);
5000 
5001 free_cma_dev:
5002 	kfree(cma_dev);
5003 	return ret;
5004 }
5005 
5006 static void cma_remove_one(struct ib_device *device, void *client_data)
5007 {
5008 	struct cma_device *cma_dev = client_data;
5009 
5010 	trace_cm_remove_one(device);
5011 
5012 	mutex_lock(&lock);
5013 	list_del(&cma_dev->list);
5014 	mutex_unlock(&lock);
5015 
5016 	cma_process_remove(cma_dev);
5017 	kfree(cma_dev->default_roce_tos);
5018 	kfree(cma_dev->default_gid_type);
5019 	kfree(cma_dev);
5020 }
5021 
5022 static int cma_init_net(struct net *net)
5023 {
5024 	struct cma_pernet *pernet = cma_pernet(net);
5025 
5026 	xa_init(&pernet->tcp_ps);
5027 	xa_init(&pernet->udp_ps);
5028 	xa_init(&pernet->ipoib_ps);
5029 	xa_init(&pernet->ib_ps);
5030 
5031 	return 0;
5032 }
5033 
5034 static void cma_exit_net(struct net *net)
5035 {
5036 	struct cma_pernet *pernet = cma_pernet(net);
5037 
5038 	WARN_ON(!xa_empty(&pernet->tcp_ps));
5039 	WARN_ON(!xa_empty(&pernet->udp_ps));
5040 	WARN_ON(!xa_empty(&pernet->ipoib_ps));
5041 	WARN_ON(!xa_empty(&pernet->ib_ps));
5042 }
5043 
5044 static struct pernet_operations cma_pernet_operations = {
5045 	.init = cma_init_net,
5046 	.exit = cma_exit_net,
5047 	.id = &cma_pernet_id,
5048 	.size = sizeof(struct cma_pernet),
5049 };
5050 
5051 static int __init cma_init(void)
5052 {
5053 	int ret;
5054 
5055 	/*
5056 	 * There is a rare lock ordering dependency in cma_netdev_callback()
5057 	 * that only happens when bonding is enabled. Teach lockdep that rtnl
5058 	 * must never be nested under lock so it can find these without having
5059 	 * to test with bonding.
5060 	 */
5061 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
5062 		rtnl_lock();
5063 		mutex_lock(&lock);
5064 		mutex_unlock(&lock);
5065 		rtnl_unlock();
5066 	}
5067 
5068 	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5069 	if (!cma_wq)
5070 		return -ENOMEM;
5071 
5072 	ret = register_pernet_subsys(&cma_pernet_operations);
5073 	if (ret)
5074 		goto err_wq;
5075 
5076 	ib_sa_register_client(&sa_client);
5077 	register_netdevice_notifier(&cma_nb);
5078 
5079 	ret = ib_register_client(&cma_client);
5080 	if (ret)
5081 		goto err;
5082 
5083 	ret = cma_configfs_init();
5084 	if (ret)
5085 		goto err_ib;
5086 
5087 	return 0;
5088 
5089 err_ib:
5090 	ib_unregister_client(&cma_client);
5091 err:
5092 	unregister_netdevice_notifier(&cma_nb);
5093 	ib_sa_unregister_client(&sa_client);
5094 	unregister_pernet_subsys(&cma_pernet_operations);
5095 err_wq:
5096 	destroy_workqueue(cma_wq);
5097 	return ret;
5098 }
5099 
5100 static void __exit cma_cleanup(void)
5101 {
5102 	cma_configfs_exit();
5103 	ib_unregister_client(&cma_client);
5104 	unregister_netdevice_notifier(&cma_nb);
5105 	ib_sa_unregister_client(&sa_client);
5106 	unregister_pernet_subsys(&cma_pernet_operations);
5107 	destroy_workqueue(cma_wq);
5108 }
5109 
5110 module_init(cma_init);
5111 module_exit(cma_cleanup);
5112