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