xref: /openbmc/linux/drivers/infiniband/core/cma.c (revision 710b797c)
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 	if (id_priv->cma_dev) {
1856 		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1857 			if (id_priv->cm_id.ib)
1858 				ib_destroy_cm_id(id_priv->cm_id.ib);
1859 		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1860 			if (id_priv->cm_id.iw)
1861 				iw_destroy_cm_id(id_priv->cm_id.iw);
1862 		}
1863 		cma_leave_mc_groups(id_priv);
1864 		rdma_restrack_del(&id_priv->res);
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 	id->tos = id_priv->tos;
2476 	id->tos_set = id_priv->tos_set;
2477 	id->afonly = id_priv->afonly;
2478 	id_priv->cm_id.iw = id;
2479 
2480 	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2481 	       rdma_addr_size(cma_src_addr(id_priv)));
2482 
2483 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2484 
2485 	if (ret) {
2486 		iw_destroy_cm_id(id_priv->cm_id.iw);
2487 		id_priv->cm_id.iw = NULL;
2488 	}
2489 
2490 	return ret;
2491 }
2492 
2493 static int cma_listen_handler(struct rdma_cm_id *id,
2494 			      struct rdma_cm_event *event)
2495 {
2496 	struct rdma_id_private *id_priv = id->context;
2497 
2498 	/* Listening IDs are always destroyed on removal */
2499 	if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2500 		return -1;
2501 
2502 	id->context = id_priv->id.context;
2503 	id->event_handler = id_priv->id.event_handler;
2504 	trace_cm_event_handler(id_priv, event);
2505 	return id_priv->id.event_handler(id, event);
2506 }
2507 
2508 static int cma_listen_on_dev(struct rdma_id_private *id_priv,
2509 			     struct cma_device *cma_dev,
2510 			     struct rdma_id_private **to_destroy)
2511 {
2512 	struct rdma_id_private *dev_id_priv;
2513 	struct net *net = id_priv->id.route.addr.dev_addr.net;
2514 	int ret;
2515 
2516 	lockdep_assert_held(&lock);
2517 
2518 	*to_destroy = NULL;
2519 	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2520 		return 0;
2521 
2522 	dev_id_priv =
2523 		__rdma_create_id(net, cma_listen_handler, id_priv,
2524 				 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2525 	if (IS_ERR(dev_id_priv))
2526 		return PTR_ERR(dev_id_priv);
2527 
2528 	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2529 	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2530 	       rdma_addr_size(cma_src_addr(id_priv)));
2531 
2532 	_cma_attach_to_dev(dev_id_priv, cma_dev);
2533 	rdma_restrack_add(&dev_id_priv->res);
2534 	cma_id_get(id_priv);
2535 	dev_id_priv->internal_id = 1;
2536 	dev_id_priv->afonly = id_priv->afonly;
2537 	dev_id_priv->tos_set = id_priv->tos_set;
2538 	dev_id_priv->tos = id_priv->tos;
2539 
2540 	ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2541 	if (ret)
2542 		goto err_listen;
2543 	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2544 	return 0;
2545 err_listen:
2546 	/* Caller must destroy this after releasing lock */
2547 	*to_destroy = dev_id_priv;
2548 	dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret);
2549 	return ret;
2550 }
2551 
2552 static int cma_listen_on_all(struct rdma_id_private *id_priv)
2553 {
2554 	struct rdma_id_private *to_destroy;
2555 	struct cma_device *cma_dev;
2556 	int ret;
2557 
2558 	mutex_lock(&lock);
2559 	list_add_tail(&id_priv->list, &listen_any_list);
2560 	list_for_each_entry(cma_dev, &dev_list, list) {
2561 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
2562 		if (ret) {
2563 			/* Prevent racing with cma_process_remove() */
2564 			if (to_destroy)
2565 				list_del_init(&to_destroy->list);
2566 			goto err_listen;
2567 		}
2568 	}
2569 	mutex_unlock(&lock);
2570 	return 0;
2571 
2572 err_listen:
2573 	list_del(&id_priv->list);
2574 	mutex_unlock(&lock);
2575 	if (to_destroy)
2576 		rdma_destroy_id(&to_destroy->id);
2577 	return ret;
2578 }
2579 
2580 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2581 {
2582 	struct rdma_id_private *id_priv;
2583 
2584 	id_priv = container_of(id, struct rdma_id_private, id);
2585 	id_priv->tos = (u8) tos;
2586 	id_priv->tos_set = true;
2587 }
2588 EXPORT_SYMBOL(rdma_set_service_type);
2589 
2590 /**
2591  * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2592  *                          with a connection identifier.
2593  * @id: Communication identifier to associated with service type.
2594  * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2595  *
2596  * This function should be called before rdma_connect() on active side,
2597  * and on passive side before rdma_accept(). It is applicable to primary
2598  * path only. The timeout will affect the local side of the QP, it is not
2599  * negotiated with remote side and zero disables the timer. In case it is
2600  * set before rdma_resolve_route, the value will also be used to determine
2601  * PacketLifeTime for RoCE.
2602  *
2603  * Return: 0 for success
2604  */
2605 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2606 {
2607 	struct rdma_id_private *id_priv;
2608 
2609 	if (id->qp_type != IB_QPT_RC)
2610 		return -EINVAL;
2611 
2612 	id_priv = container_of(id, struct rdma_id_private, id);
2613 	id_priv->timeout = timeout;
2614 	id_priv->timeout_set = true;
2615 
2616 	return 0;
2617 }
2618 EXPORT_SYMBOL(rdma_set_ack_timeout);
2619 
2620 /**
2621  * rdma_set_min_rnr_timer() - Set the minimum RNR Retry timer of the
2622  *			      QP associated with a connection identifier.
2623  * @id: Communication identifier to associated with service type.
2624  * @min_rnr_timer: 5-bit value encoded as Table 45: "Encoding for RNR NAK
2625  *		   Timer Field" in the IBTA specification.
2626  *
2627  * This function should be called before rdma_connect() on active
2628  * side, and on passive side before rdma_accept(). The timer value
2629  * will be associated with the local QP. When it receives a send it is
2630  * not read to handle, typically if the receive queue is empty, an RNR
2631  * Retry NAK is returned to the requester with the min_rnr_timer
2632  * encoded. The requester will then wait at least the time specified
2633  * in the NAK before retrying. The default is zero, which translates
2634  * to a minimum RNR Timer value of 655 ms.
2635  *
2636  * Return: 0 for success
2637  */
2638 int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer)
2639 {
2640 	struct rdma_id_private *id_priv;
2641 
2642 	/* It is a five-bit value */
2643 	if (min_rnr_timer & 0xe0)
2644 		return -EINVAL;
2645 
2646 	if (WARN_ON(id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_TGT))
2647 		return -EINVAL;
2648 
2649 	id_priv = container_of(id, struct rdma_id_private, id);
2650 	id_priv->min_rnr_timer = min_rnr_timer;
2651 	id_priv->min_rnr_timer_set = true;
2652 
2653 	return 0;
2654 }
2655 EXPORT_SYMBOL(rdma_set_min_rnr_timer);
2656 
2657 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2658 			      void *context)
2659 {
2660 	struct cma_work *work = context;
2661 	struct rdma_route *route;
2662 
2663 	route = &work->id->id.route;
2664 
2665 	if (!status) {
2666 		route->num_paths = 1;
2667 		*route->path_rec = *path_rec;
2668 	} else {
2669 		work->old_state = RDMA_CM_ROUTE_QUERY;
2670 		work->new_state = RDMA_CM_ADDR_RESOLVED;
2671 		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2672 		work->event.status = status;
2673 		pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2674 				     status);
2675 	}
2676 
2677 	queue_work(cma_wq, &work->work);
2678 }
2679 
2680 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2681 			      unsigned long timeout_ms, struct cma_work *work)
2682 {
2683 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2684 	struct sa_path_rec path_rec;
2685 	ib_sa_comp_mask comp_mask;
2686 	struct sockaddr_in6 *sin6;
2687 	struct sockaddr_ib *sib;
2688 
2689 	memset(&path_rec, 0, sizeof path_rec);
2690 
2691 	if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2692 		path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2693 	else
2694 		path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2695 	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2696 	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2697 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2698 	path_rec.numb_path = 1;
2699 	path_rec.reversible = 1;
2700 	path_rec.service_id = rdma_get_service_id(&id_priv->id,
2701 						  cma_dst_addr(id_priv));
2702 
2703 	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2704 		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2705 		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2706 
2707 	switch (cma_family(id_priv)) {
2708 	case AF_INET:
2709 		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2710 		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2711 		break;
2712 	case AF_INET6:
2713 		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2714 		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2715 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2716 		break;
2717 	case AF_IB:
2718 		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2719 		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2720 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2721 		break;
2722 	}
2723 
2724 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2725 					       id_priv->id.port_num, &path_rec,
2726 					       comp_mask, timeout_ms,
2727 					       GFP_KERNEL, cma_query_handler,
2728 					       work, &id_priv->query);
2729 
2730 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2731 }
2732 
2733 static void cma_iboe_join_work_handler(struct work_struct *work)
2734 {
2735 	struct cma_multicast *mc =
2736 		container_of(work, struct cma_multicast, iboe_join.work);
2737 	struct rdma_cm_event *event = &mc->iboe_join.event;
2738 	struct rdma_id_private *id_priv = mc->id_priv;
2739 	int ret;
2740 
2741 	mutex_lock(&id_priv->handler_mutex);
2742 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2743 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2744 		goto out_unlock;
2745 
2746 	ret = cma_cm_event_handler(id_priv, event);
2747 	WARN_ON(ret);
2748 
2749 out_unlock:
2750 	mutex_unlock(&id_priv->handler_mutex);
2751 	if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2752 		rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2753 }
2754 
2755 static void cma_work_handler(struct work_struct *_work)
2756 {
2757 	struct cma_work *work = container_of(_work, struct cma_work, work);
2758 	struct rdma_id_private *id_priv = work->id;
2759 
2760 	mutex_lock(&id_priv->handler_mutex);
2761 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2762 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2763 		goto out_unlock;
2764 	if (work->old_state != 0 || work->new_state != 0) {
2765 		if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2766 			goto out_unlock;
2767 	}
2768 
2769 	if (cma_cm_event_handler(id_priv, &work->event)) {
2770 		cma_id_put(id_priv);
2771 		destroy_id_handler_unlock(id_priv);
2772 		goto out_free;
2773 	}
2774 
2775 out_unlock:
2776 	mutex_unlock(&id_priv->handler_mutex);
2777 	cma_id_put(id_priv);
2778 out_free:
2779 	if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
2780 		rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
2781 	kfree(work);
2782 }
2783 
2784 static void cma_init_resolve_route_work(struct cma_work *work,
2785 					struct rdma_id_private *id_priv)
2786 {
2787 	work->id = id_priv;
2788 	INIT_WORK(&work->work, cma_work_handler);
2789 	work->old_state = RDMA_CM_ROUTE_QUERY;
2790 	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2791 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2792 }
2793 
2794 static void enqueue_resolve_addr_work(struct cma_work *work,
2795 				      struct rdma_id_private *id_priv)
2796 {
2797 	/* Balances with cma_id_put() in cma_work_handler */
2798 	cma_id_get(id_priv);
2799 
2800 	work->id = id_priv;
2801 	INIT_WORK(&work->work, cma_work_handler);
2802 	work->old_state = RDMA_CM_ADDR_QUERY;
2803 	work->new_state = RDMA_CM_ADDR_RESOLVED;
2804 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2805 
2806 	queue_work(cma_wq, &work->work);
2807 }
2808 
2809 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
2810 				unsigned long timeout_ms)
2811 {
2812 	struct rdma_route *route = &id_priv->id.route;
2813 	struct cma_work *work;
2814 	int ret;
2815 
2816 	work = kzalloc(sizeof *work, GFP_KERNEL);
2817 	if (!work)
2818 		return -ENOMEM;
2819 
2820 	cma_init_resolve_route_work(work, id_priv);
2821 
2822 	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2823 	if (!route->path_rec) {
2824 		ret = -ENOMEM;
2825 		goto err1;
2826 	}
2827 
2828 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2829 	if (ret)
2830 		goto err2;
2831 
2832 	return 0;
2833 err2:
2834 	kfree(route->path_rec);
2835 	route->path_rec = NULL;
2836 err1:
2837 	kfree(work);
2838 	return ret;
2839 }
2840 
2841 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2842 					   unsigned long supported_gids,
2843 					   enum ib_gid_type default_gid)
2844 {
2845 	if ((network_type == RDMA_NETWORK_IPV4 ||
2846 	     network_type == RDMA_NETWORK_IPV6) &&
2847 	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2848 		return IB_GID_TYPE_ROCE_UDP_ENCAP;
2849 
2850 	return default_gid;
2851 }
2852 
2853 /*
2854  * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2855  * path record type based on GID type.
2856  * It also sets up other L2 fields which includes destination mac address
2857  * netdev ifindex, of the path record.
2858  * It returns the netdev of the bound interface for this path record entry.
2859  */
2860 static struct net_device *
2861 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
2862 {
2863 	struct rdma_route *route = &id_priv->id.route;
2864 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
2865 	struct rdma_addr *addr = &route->addr;
2866 	unsigned long supported_gids;
2867 	struct net_device *ndev;
2868 
2869 	if (!addr->dev_addr.bound_dev_if)
2870 		return NULL;
2871 
2872 	ndev = dev_get_by_index(addr->dev_addr.net,
2873 				addr->dev_addr.bound_dev_if);
2874 	if (!ndev)
2875 		return NULL;
2876 
2877 	supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2878 						    id_priv->id.port_num);
2879 	gid_type = cma_route_gid_type(addr->dev_addr.network,
2880 				      supported_gids,
2881 				      id_priv->gid_type);
2882 	/* Use the hint from IP Stack to select GID Type */
2883 	if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2884 		gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2885 	route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
2886 
2887 	route->path_rec->roce.route_resolved = true;
2888 	sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
2889 	return ndev;
2890 }
2891 
2892 int rdma_set_ib_path(struct rdma_cm_id *id,
2893 		     struct sa_path_rec *path_rec)
2894 {
2895 	struct rdma_id_private *id_priv;
2896 	struct net_device *ndev;
2897 	int ret;
2898 
2899 	id_priv = container_of(id, struct rdma_id_private, id);
2900 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2901 			   RDMA_CM_ROUTE_RESOLVED))
2902 		return -EINVAL;
2903 
2904 	id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
2905 				     GFP_KERNEL);
2906 	if (!id->route.path_rec) {
2907 		ret = -ENOMEM;
2908 		goto err;
2909 	}
2910 
2911 	if (rdma_protocol_roce(id->device, id->port_num)) {
2912 		ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2913 		if (!ndev) {
2914 			ret = -ENODEV;
2915 			goto err_free;
2916 		}
2917 		dev_put(ndev);
2918 	}
2919 
2920 	id->route.num_paths = 1;
2921 	return 0;
2922 
2923 err_free:
2924 	kfree(id->route.path_rec);
2925 	id->route.path_rec = NULL;
2926 err:
2927 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2928 	return ret;
2929 }
2930 EXPORT_SYMBOL(rdma_set_ib_path);
2931 
2932 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
2933 {
2934 	struct cma_work *work;
2935 
2936 	work = kzalloc(sizeof *work, GFP_KERNEL);
2937 	if (!work)
2938 		return -ENOMEM;
2939 
2940 	cma_init_resolve_route_work(work, id_priv);
2941 	queue_work(cma_wq, &work->work);
2942 	return 0;
2943 }
2944 
2945 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
2946 {
2947 	struct net_device *dev;
2948 
2949 	dev = vlan_dev_real_dev(vlan_ndev);
2950 	if (dev->num_tc)
2951 		return netdev_get_prio_tc_map(dev, prio);
2952 
2953 	return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
2954 		VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2955 }
2956 
2957 struct iboe_prio_tc_map {
2958 	int input_prio;
2959 	int output_tc;
2960 	bool found;
2961 };
2962 
2963 static int get_lower_vlan_dev_tc(struct net_device *dev,
2964 				 struct netdev_nested_priv *priv)
2965 {
2966 	struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
2967 
2968 	if (is_vlan_dev(dev))
2969 		map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
2970 	else if (dev->num_tc)
2971 		map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
2972 	else
2973 		map->output_tc = 0;
2974 	/* We are interested only in first level VLAN device, so always
2975 	 * return 1 to stop iterating over next level devices.
2976 	 */
2977 	map->found = true;
2978 	return 1;
2979 }
2980 
2981 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2982 {
2983 	struct iboe_prio_tc_map prio_tc_map = {};
2984 	int prio = rt_tos2priority(tos);
2985 	struct netdev_nested_priv priv;
2986 
2987 	/* If VLAN device, get it directly from the VLAN netdev */
2988 	if (is_vlan_dev(ndev))
2989 		return get_vlan_ndev_tc(ndev, prio);
2990 
2991 	prio_tc_map.input_prio = prio;
2992 	priv.data = (void *)&prio_tc_map;
2993 	rcu_read_lock();
2994 	netdev_walk_all_lower_dev_rcu(ndev,
2995 				      get_lower_vlan_dev_tc,
2996 				      &priv);
2997 	rcu_read_unlock();
2998 	/* If map is found from lower device, use it; Otherwise
2999 	 * continue with the current netdevice to get priority to tc map.
3000 	 */
3001 	if (prio_tc_map.found)
3002 		return prio_tc_map.output_tc;
3003 	else if (ndev->num_tc)
3004 		return netdev_get_prio_tc_map(ndev, prio);
3005 	else
3006 		return 0;
3007 }
3008 
3009 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
3010 {
3011 	struct sockaddr_in6 *addr6;
3012 	u16 dport, sport;
3013 	u32 hash, fl;
3014 
3015 	addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3016 	fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3017 	if ((cma_family(id_priv) != AF_INET6) || !fl) {
3018 		dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3019 		sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3020 		hash = (u32)sport * 31 + dport;
3021 		fl = hash & IB_GRH_FLOWLABEL_MASK;
3022 	}
3023 
3024 	return cpu_to_be32(fl);
3025 }
3026 
3027 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
3028 {
3029 	struct rdma_route *route = &id_priv->id.route;
3030 	struct rdma_addr *addr = &route->addr;
3031 	struct cma_work *work;
3032 	int ret;
3033 	struct net_device *ndev;
3034 
3035 	u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
3036 					rdma_start_port(id_priv->cma_dev->device)];
3037 	u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3038 
3039 
3040 	work = kzalloc(sizeof *work, GFP_KERNEL);
3041 	if (!work)
3042 		return -ENOMEM;
3043 
3044 	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
3045 	if (!route->path_rec) {
3046 		ret = -ENOMEM;
3047 		goto err1;
3048 	}
3049 
3050 	route->num_paths = 1;
3051 
3052 	ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3053 	if (!ndev) {
3054 		ret = -ENODEV;
3055 		goto err2;
3056 	}
3057 
3058 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3059 		    &route->path_rec->sgid);
3060 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
3061 		    &route->path_rec->dgid);
3062 
3063 	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3064 		/* TODO: get the hoplimit from the inet/inet6 device */
3065 		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3066 	else
3067 		route->path_rec->hop_limit = 1;
3068 	route->path_rec->reversible = 1;
3069 	route->path_rec->pkey = cpu_to_be16(0xffff);
3070 	route->path_rec->mtu_selector = IB_SA_EQ;
3071 	route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
3072 	route->path_rec->traffic_class = tos;
3073 	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
3074 	route->path_rec->rate_selector = IB_SA_EQ;
3075 	route->path_rec->rate = iboe_get_rate(ndev);
3076 	dev_put(ndev);
3077 	route->path_rec->packet_life_time_selector = IB_SA_EQ;
3078 	/* In case ACK timeout is set, use this value to calculate
3079 	 * PacketLifeTime.  As per IBTA 12.7.34,
3080 	 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3081 	 * Assuming a negligible local ACK delay, we can use
3082 	 * PacketLifeTime = local ACK timeout/2
3083 	 * as a reasonable approximation for RoCE networks.
3084 	 */
3085 	route->path_rec->packet_life_time = id_priv->timeout_set ?
3086 		id_priv->timeout - 1 : CMA_IBOE_PACKET_LIFETIME;
3087 
3088 	if (!route->path_rec->mtu) {
3089 		ret = -EINVAL;
3090 		goto err2;
3091 	}
3092 
3093 	if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3094 					 id_priv->id.port_num))
3095 		route->path_rec->flow_label =
3096 			cma_get_roce_udp_flow_label(id_priv);
3097 
3098 	cma_init_resolve_route_work(work, id_priv);
3099 	queue_work(cma_wq, &work->work);
3100 
3101 	return 0;
3102 
3103 err2:
3104 	kfree(route->path_rec);
3105 	route->path_rec = NULL;
3106 	route->num_paths = 0;
3107 err1:
3108 	kfree(work);
3109 	return ret;
3110 }
3111 
3112 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3113 {
3114 	struct rdma_id_private *id_priv;
3115 	int ret;
3116 
3117 	id_priv = container_of(id, struct rdma_id_private, id);
3118 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3119 		return -EINVAL;
3120 
3121 	cma_id_get(id_priv);
3122 	if (rdma_cap_ib_sa(id->device, id->port_num))
3123 		ret = cma_resolve_ib_route(id_priv, timeout_ms);
3124 	else if (rdma_protocol_roce(id->device, id->port_num))
3125 		ret = cma_resolve_iboe_route(id_priv);
3126 	else if (rdma_protocol_iwarp(id->device, id->port_num))
3127 		ret = cma_resolve_iw_route(id_priv);
3128 	else
3129 		ret = -ENOSYS;
3130 
3131 	if (ret)
3132 		goto err;
3133 
3134 	return 0;
3135 err:
3136 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3137 	cma_id_put(id_priv);
3138 	return ret;
3139 }
3140 EXPORT_SYMBOL(rdma_resolve_route);
3141 
3142 static void cma_set_loopback(struct sockaddr *addr)
3143 {
3144 	switch (addr->sa_family) {
3145 	case AF_INET:
3146 		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3147 		break;
3148 	case AF_INET6:
3149 		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3150 			      0, 0, 0, htonl(1));
3151 		break;
3152 	default:
3153 		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3154 			    0, 0, 0, htonl(1));
3155 		break;
3156 	}
3157 }
3158 
3159 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3160 {
3161 	struct cma_device *cma_dev, *cur_dev;
3162 	union ib_gid gid;
3163 	enum ib_port_state port_state;
3164 	unsigned int p;
3165 	u16 pkey;
3166 	int ret;
3167 
3168 	cma_dev = NULL;
3169 	mutex_lock(&lock);
3170 	list_for_each_entry(cur_dev, &dev_list, list) {
3171 		if (cma_family(id_priv) == AF_IB &&
3172 		    !rdma_cap_ib_cm(cur_dev->device, 1))
3173 			continue;
3174 
3175 		if (!cma_dev)
3176 			cma_dev = cur_dev;
3177 
3178 		rdma_for_each_port (cur_dev->device, p) {
3179 			if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3180 			    port_state == IB_PORT_ACTIVE) {
3181 				cma_dev = cur_dev;
3182 				goto port_found;
3183 			}
3184 		}
3185 	}
3186 
3187 	if (!cma_dev) {
3188 		ret = -ENODEV;
3189 		goto out;
3190 	}
3191 
3192 	p = 1;
3193 
3194 port_found:
3195 	ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3196 	if (ret)
3197 		goto out;
3198 
3199 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3200 	if (ret)
3201 		goto out;
3202 
3203 	id_priv->id.route.addr.dev_addr.dev_type =
3204 		(rdma_protocol_ib(cma_dev->device, p)) ?
3205 		ARPHRD_INFINIBAND : ARPHRD_ETHER;
3206 
3207 	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3208 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3209 	id_priv->id.port_num = p;
3210 	cma_attach_to_dev(id_priv, cma_dev);
3211 	rdma_restrack_add(&id_priv->res);
3212 	cma_set_loopback(cma_src_addr(id_priv));
3213 out:
3214 	mutex_unlock(&lock);
3215 	return ret;
3216 }
3217 
3218 static void addr_handler(int status, struct sockaddr *src_addr,
3219 			 struct rdma_dev_addr *dev_addr, void *context)
3220 {
3221 	struct rdma_id_private *id_priv = context;
3222 	struct rdma_cm_event event = {};
3223 	struct sockaddr *addr;
3224 	struct sockaddr_storage old_addr;
3225 
3226 	mutex_lock(&id_priv->handler_mutex);
3227 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3228 			   RDMA_CM_ADDR_RESOLVED))
3229 		goto out;
3230 
3231 	/*
3232 	 * Store the previous src address, so that if we fail to acquire
3233 	 * matching rdma device, old address can be restored back, which helps
3234 	 * to cancel the cma listen operation correctly.
3235 	 */
3236 	addr = cma_src_addr(id_priv);
3237 	memcpy(&old_addr, addr, rdma_addr_size(addr));
3238 	memcpy(addr, src_addr, rdma_addr_size(src_addr));
3239 	if (!status && !id_priv->cma_dev) {
3240 		status = cma_acquire_dev_by_src_ip(id_priv);
3241 		if (status)
3242 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3243 					     status);
3244 		rdma_restrack_add(&id_priv->res);
3245 	} else if (status) {
3246 		pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3247 	}
3248 
3249 	if (status) {
3250 		memcpy(addr, &old_addr,
3251 		       rdma_addr_size((struct sockaddr *)&old_addr));
3252 		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3253 				   RDMA_CM_ADDR_BOUND))
3254 			goto out;
3255 		event.event = RDMA_CM_EVENT_ADDR_ERROR;
3256 		event.status = status;
3257 	} else
3258 		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3259 
3260 	if (cma_cm_event_handler(id_priv, &event)) {
3261 		destroy_id_handler_unlock(id_priv);
3262 		return;
3263 	}
3264 out:
3265 	mutex_unlock(&id_priv->handler_mutex);
3266 }
3267 
3268 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3269 {
3270 	struct cma_work *work;
3271 	union ib_gid gid;
3272 	int ret;
3273 
3274 	work = kzalloc(sizeof *work, GFP_KERNEL);
3275 	if (!work)
3276 		return -ENOMEM;
3277 
3278 	if (!id_priv->cma_dev) {
3279 		ret = cma_bind_loopback(id_priv);
3280 		if (ret)
3281 			goto err;
3282 	}
3283 
3284 	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3285 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3286 
3287 	enqueue_resolve_addr_work(work, id_priv);
3288 	return 0;
3289 err:
3290 	kfree(work);
3291 	return ret;
3292 }
3293 
3294 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3295 {
3296 	struct cma_work *work;
3297 	int ret;
3298 
3299 	work = kzalloc(sizeof *work, GFP_KERNEL);
3300 	if (!work)
3301 		return -ENOMEM;
3302 
3303 	if (!id_priv->cma_dev) {
3304 		ret = cma_resolve_ib_dev(id_priv);
3305 		if (ret)
3306 			goto err;
3307 	}
3308 
3309 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3310 		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3311 
3312 	enqueue_resolve_addr_work(work, id_priv);
3313 	return 0;
3314 err:
3315 	kfree(work);
3316 	return ret;
3317 }
3318 
3319 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3320 			 const struct sockaddr *dst_addr)
3321 {
3322 	if (!src_addr || !src_addr->sa_family) {
3323 		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
3324 		src_addr->sa_family = dst_addr->sa_family;
3325 		if (IS_ENABLED(CONFIG_IPV6) &&
3326 		    dst_addr->sa_family == AF_INET6) {
3327 			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
3328 			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
3329 			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
3330 			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
3331 				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
3332 		} else if (dst_addr->sa_family == AF_IB) {
3333 			((struct sockaddr_ib *) src_addr)->sib_pkey =
3334 				((struct sockaddr_ib *) dst_addr)->sib_pkey;
3335 		}
3336 	}
3337 	return rdma_bind_addr(id, src_addr);
3338 }
3339 
3340 /*
3341  * If required, resolve the source address for bind and leave the id_priv in
3342  * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3343  * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3344  * ignored.
3345  */
3346 static int resolve_prepare_src(struct rdma_id_private *id_priv,
3347 			       struct sockaddr *src_addr,
3348 			       const struct sockaddr *dst_addr)
3349 {
3350 	int ret;
3351 
3352 	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3353 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3354 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3355 		ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3356 		if (ret)
3357 			goto err_dst;
3358 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3359 					   RDMA_CM_ADDR_QUERY))) {
3360 			ret = -EINVAL;
3361 			goto err_dst;
3362 		}
3363 	}
3364 
3365 	if (cma_family(id_priv) != dst_addr->sa_family) {
3366 		ret = -EINVAL;
3367 		goto err_state;
3368 	}
3369 	return 0;
3370 
3371 err_state:
3372 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3373 err_dst:
3374 	memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3375 	return ret;
3376 }
3377 
3378 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3379 		      const struct sockaddr *dst_addr, unsigned long timeout_ms)
3380 {
3381 	struct rdma_id_private *id_priv =
3382 		container_of(id, struct rdma_id_private, id);
3383 	int ret;
3384 
3385 	ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3386 	if (ret)
3387 		return ret;
3388 
3389 	if (cma_any_addr(dst_addr)) {
3390 		ret = cma_resolve_loopback(id_priv);
3391 	} else {
3392 		if (dst_addr->sa_family == AF_IB) {
3393 			ret = cma_resolve_ib_addr(id_priv);
3394 		} else {
3395 			ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
3396 					      &id->route.addr.dev_addr,
3397 					      timeout_ms, addr_handler,
3398 					      false, id_priv);
3399 		}
3400 	}
3401 	if (ret)
3402 		goto err;
3403 
3404 	return 0;
3405 err:
3406 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3407 	return ret;
3408 }
3409 EXPORT_SYMBOL(rdma_resolve_addr);
3410 
3411 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3412 {
3413 	struct rdma_id_private *id_priv;
3414 	unsigned long flags;
3415 	int ret;
3416 
3417 	id_priv = container_of(id, struct rdma_id_private, id);
3418 	spin_lock_irqsave(&id_priv->lock, flags);
3419 	if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3420 	    id_priv->state == RDMA_CM_IDLE) {
3421 		id_priv->reuseaddr = reuse;
3422 		ret = 0;
3423 	} else {
3424 		ret = -EINVAL;
3425 	}
3426 	spin_unlock_irqrestore(&id_priv->lock, flags);
3427 	return ret;
3428 }
3429 EXPORT_SYMBOL(rdma_set_reuseaddr);
3430 
3431 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3432 {
3433 	struct rdma_id_private *id_priv;
3434 	unsigned long flags;
3435 	int ret;
3436 
3437 	id_priv = container_of(id, struct rdma_id_private, id);
3438 	spin_lock_irqsave(&id_priv->lock, flags);
3439 	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3440 		id_priv->options |= (1 << CMA_OPTION_AFONLY);
3441 		id_priv->afonly = afonly;
3442 		ret = 0;
3443 	} else {
3444 		ret = -EINVAL;
3445 	}
3446 	spin_unlock_irqrestore(&id_priv->lock, flags);
3447 	return ret;
3448 }
3449 EXPORT_SYMBOL(rdma_set_afonly);
3450 
3451 static void cma_bind_port(struct rdma_bind_list *bind_list,
3452 			  struct rdma_id_private *id_priv)
3453 {
3454 	struct sockaddr *addr;
3455 	struct sockaddr_ib *sib;
3456 	u64 sid, mask;
3457 	__be16 port;
3458 
3459 	lockdep_assert_held(&lock);
3460 
3461 	addr = cma_src_addr(id_priv);
3462 	port = htons(bind_list->port);
3463 
3464 	switch (addr->sa_family) {
3465 	case AF_INET:
3466 		((struct sockaddr_in *) addr)->sin_port = port;
3467 		break;
3468 	case AF_INET6:
3469 		((struct sockaddr_in6 *) addr)->sin6_port = port;
3470 		break;
3471 	case AF_IB:
3472 		sib = (struct sockaddr_ib *) addr;
3473 		sid = be64_to_cpu(sib->sib_sid);
3474 		mask = be64_to_cpu(sib->sib_sid_mask);
3475 		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3476 		sib->sib_sid_mask = cpu_to_be64(~0ULL);
3477 		break;
3478 	}
3479 	id_priv->bind_list = bind_list;
3480 	hlist_add_head(&id_priv->node, &bind_list->owners);
3481 }
3482 
3483 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3484 			  struct rdma_id_private *id_priv, unsigned short snum)
3485 {
3486 	struct rdma_bind_list *bind_list;
3487 	int ret;
3488 
3489 	lockdep_assert_held(&lock);
3490 
3491 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3492 	if (!bind_list)
3493 		return -ENOMEM;
3494 
3495 	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3496 			   snum);
3497 	if (ret < 0)
3498 		goto err;
3499 
3500 	bind_list->ps = ps;
3501 	bind_list->port = snum;
3502 	cma_bind_port(bind_list, id_priv);
3503 	return 0;
3504 err:
3505 	kfree(bind_list);
3506 	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3507 }
3508 
3509 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3510 			      struct rdma_id_private *id_priv)
3511 {
3512 	struct rdma_id_private *cur_id;
3513 	struct sockaddr  *daddr = cma_dst_addr(id_priv);
3514 	struct sockaddr  *saddr = cma_src_addr(id_priv);
3515 	__be16 dport = cma_port(daddr);
3516 
3517 	lockdep_assert_held(&lock);
3518 
3519 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3520 		struct sockaddr  *cur_daddr = cma_dst_addr(cur_id);
3521 		struct sockaddr  *cur_saddr = cma_src_addr(cur_id);
3522 		__be16 cur_dport = cma_port(cur_daddr);
3523 
3524 		if (id_priv == cur_id)
3525 			continue;
3526 
3527 		/* different dest port -> unique */
3528 		if (!cma_any_port(daddr) &&
3529 		    !cma_any_port(cur_daddr) &&
3530 		    (dport != cur_dport))
3531 			continue;
3532 
3533 		/* different src address -> unique */
3534 		if (!cma_any_addr(saddr) &&
3535 		    !cma_any_addr(cur_saddr) &&
3536 		    cma_addr_cmp(saddr, cur_saddr))
3537 			continue;
3538 
3539 		/* different dst address -> unique */
3540 		if (!cma_any_addr(daddr) &&
3541 		    !cma_any_addr(cur_daddr) &&
3542 		    cma_addr_cmp(daddr, cur_daddr))
3543 			continue;
3544 
3545 		return -EADDRNOTAVAIL;
3546 	}
3547 	return 0;
3548 }
3549 
3550 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3551 			      struct rdma_id_private *id_priv)
3552 {
3553 	static unsigned int last_used_port;
3554 	int low, high, remaining;
3555 	unsigned int rover;
3556 	struct net *net = id_priv->id.route.addr.dev_addr.net;
3557 
3558 	lockdep_assert_held(&lock);
3559 
3560 	inet_get_local_port_range(net, &low, &high);
3561 	remaining = (high - low) + 1;
3562 	rover = prandom_u32() % remaining + low;
3563 retry:
3564 	if (last_used_port != rover) {
3565 		struct rdma_bind_list *bind_list;
3566 		int ret;
3567 
3568 		bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3569 
3570 		if (!bind_list) {
3571 			ret = cma_alloc_port(ps, id_priv, rover);
3572 		} else {
3573 			ret = cma_port_is_unique(bind_list, id_priv);
3574 			if (!ret)
3575 				cma_bind_port(bind_list, id_priv);
3576 		}
3577 		/*
3578 		 * Remember previously used port number in order to avoid
3579 		 * re-using same port immediately after it is closed.
3580 		 */
3581 		if (!ret)
3582 			last_used_port = rover;
3583 		if (ret != -EADDRNOTAVAIL)
3584 			return ret;
3585 	}
3586 	if (--remaining) {
3587 		rover++;
3588 		if ((rover < low) || (rover > high))
3589 			rover = low;
3590 		goto retry;
3591 	}
3592 	return -EADDRNOTAVAIL;
3593 }
3594 
3595 /*
3596  * Check that the requested port is available.  This is called when trying to
3597  * bind to a specific port, or when trying to listen on a bound port.  In
3598  * the latter case, the provided id_priv may already be on the bind_list, but
3599  * we still need to check that it's okay to start listening.
3600  */
3601 static int cma_check_port(struct rdma_bind_list *bind_list,
3602 			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3603 {
3604 	struct rdma_id_private *cur_id;
3605 	struct sockaddr *addr, *cur_addr;
3606 
3607 	lockdep_assert_held(&lock);
3608 
3609 	addr = cma_src_addr(id_priv);
3610 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3611 		if (id_priv == cur_id)
3612 			continue;
3613 
3614 		if (reuseaddr && cur_id->reuseaddr)
3615 			continue;
3616 
3617 		cur_addr = cma_src_addr(cur_id);
3618 		if (id_priv->afonly && cur_id->afonly &&
3619 		    (addr->sa_family != cur_addr->sa_family))
3620 			continue;
3621 
3622 		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3623 			return -EADDRNOTAVAIL;
3624 
3625 		if (!cma_addr_cmp(addr, cur_addr))
3626 			return -EADDRINUSE;
3627 	}
3628 	return 0;
3629 }
3630 
3631 static int cma_use_port(enum rdma_ucm_port_space ps,
3632 			struct rdma_id_private *id_priv)
3633 {
3634 	struct rdma_bind_list *bind_list;
3635 	unsigned short snum;
3636 	int ret;
3637 
3638 	lockdep_assert_held(&lock);
3639 
3640 	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3641 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3642 		return -EACCES;
3643 
3644 	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3645 	if (!bind_list) {
3646 		ret = cma_alloc_port(ps, id_priv, snum);
3647 	} else {
3648 		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3649 		if (!ret)
3650 			cma_bind_port(bind_list, id_priv);
3651 	}
3652 	return ret;
3653 }
3654 
3655 static enum rdma_ucm_port_space
3656 cma_select_inet_ps(struct rdma_id_private *id_priv)
3657 {
3658 	switch (id_priv->id.ps) {
3659 	case RDMA_PS_TCP:
3660 	case RDMA_PS_UDP:
3661 	case RDMA_PS_IPOIB:
3662 	case RDMA_PS_IB:
3663 		return id_priv->id.ps;
3664 	default:
3665 
3666 		return 0;
3667 	}
3668 }
3669 
3670 static enum rdma_ucm_port_space
3671 cma_select_ib_ps(struct rdma_id_private *id_priv)
3672 {
3673 	enum rdma_ucm_port_space ps = 0;
3674 	struct sockaddr_ib *sib;
3675 	u64 sid_ps, mask, sid;
3676 
3677 	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3678 	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3679 	sid = be64_to_cpu(sib->sib_sid) & mask;
3680 
3681 	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3682 		sid_ps = RDMA_IB_IP_PS_IB;
3683 		ps = RDMA_PS_IB;
3684 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3685 		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3686 		sid_ps = RDMA_IB_IP_PS_TCP;
3687 		ps = RDMA_PS_TCP;
3688 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3689 		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3690 		sid_ps = RDMA_IB_IP_PS_UDP;
3691 		ps = RDMA_PS_UDP;
3692 	}
3693 
3694 	if (ps) {
3695 		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3696 		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3697 						be64_to_cpu(sib->sib_sid_mask));
3698 	}
3699 	return ps;
3700 }
3701 
3702 static int cma_get_port(struct rdma_id_private *id_priv)
3703 {
3704 	enum rdma_ucm_port_space ps;
3705 	int ret;
3706 
3707 	if (cma_family(id_priv) != AF_IB)
3708 		ps = cma_select_inet_ps(id_priv);
3709 	else
3710 		ps = cma_select_ib_ps(id_priv);
3711 	if (!ps)
3712 		return -EPROTONOSUPPORT;
3713 
3714 	mutex_lock(&lock);
3715 	if (cma_any_port(cma_src_addr(id_priv)))
3716 		ret = cma_alloc_any_port(ps, id_priv);
3717 	else
3718 		ret = cma_use_port(ps, id_priv);
3719 	mutex_unlock(&lock);
3720 
3721 	return ret;
3722 }
3723 
3724 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3725 			       struct sockaddr *addr)
3726 {
3727 #if IS_ENABLED(CONFIG_IPV6)
3728 	struct sockaddr_in6 *sin6;
3729 
3730 	if (addr->sa_family != AF_INET6)
3731 		return 0;
3732 
3733 	sin6 = (struct sockaddr_in6 *) addr;
3734 
3735 	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3736 		return 0;
3737 
3738 	if (!sin6->sin6_scope_id)
3739 			return -EINVAL;
3740 
3741 	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3742 #endif
3743 	return 0;
3744 }
3745 
3746 int rdma_listen(struct rdma_cm_id *id, int backlog)
3747 {
3748 	struct rdma_id_private *id_priv =
3749 		container_of(id, struct rdma_id_private, id);
3750 	int ret;
3751 
3752 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3753 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3754 		id->route.addr.src_addr.ss_family = AF_INET;
3755 		ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3756 		if (ret)
3757 			return ret;
3758 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3759 					   RDMA_CM_LISTEN)))
3760 			return -EINVAL;
3761 	}
3762 
3763 	/*
3764 	 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3765 	 * any more, and has to be unique in the bind list.
3766 	 */
3767 	if (id_priv->reuseaddr) {
3768 		mutex_lock(&lock);
3769 		ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3770 		if (!ret)
3771 			id_priv->reuseaddr = 0;
3772 		mutex_unlock(&lock);
3773 		if (ret)
3774 			goto err;
3775 	}
3776 
3777 	id_priv->backlog = backlog;
3778 	if (id_priv->cma_dev) {
3779 		if (rdma_cap_ib_cm(id->device, 1)) {
3780 			ret = cma_ib_listen(id_priv);
3781 			if (ret)
3782 				goto err;
3783 		} else if (rdma_cap_iw_cm(id->device, 1)) {
3784 			ret = cma_iw_listen(id_priv, backlog);
3785 			if (ret)
3786 				goto err;
3787 		} else {
3788 			ret = -ENOSYS;
3789 			goto err;
3790 		}
3791 	} else {
3792 		ret = cma_listen_on_all(id_priv);
3793 		if (ret)
3794 			goto err;
3795 	}
3796 
3797 	return 0;
3798 err:
3799 	id_priv->backlog = 0;
3800 	/*
3801 	 * All the failure paths that lead here will not allow the req_handler's
3802 	 * to have run.
3803 	 */
3804 	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3805 	return ret;
3806 }
3807 EXPORT_SYMBOL(rdma_listen);
3808 
3809 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3810 {
3811 	struct rdma_id_private *id_priv;
3812 	int ret;
3813 	struct sockaddr  *daddr;
3814 
3815 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3816 	    addr->sa_family != AF_IB)
3817 		return -EAFNOSUPPORT;
3818 
3819 	id_priv = container_of(id, struct rdma_id_private, id);
3820 	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3821 		return -EINVAL;
3822 
3823 	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3824 	if (ret)
3825 		goto err1;
3826 
3827 	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3828 	if (!cma_any_addr(addr)) {
3829 		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3830 		if (ret)
3831 			goto err1;
3832 
3833 		ret = cma_acquire_dev_by_src_ip(id_priv);
3834 		if (ret)
3835 			goto err1;
3836 	}
3837 
3838 	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3839 		if (addr->sa_family == AF_INET)
3840 			id_priv->afonly = 1;
3841 #if IS_ENABLED(CONFIG_IPV6)
3842 		else if (addr->sa_family == AF_INET6) {
3843 			struct net *net = id_priv->id.route.addr.dev_addr.net;
3844 
3845 			id_priv->afonly = net->ipv6.sysctl.bindv6only;
3846 		}
3847 #endif
3848 	}
3849 	daddr = cma_dst_addr(id_priv);
3850 	daddr->sa_family = addr->sa_family;
3851 
3852 	ret = cma_get_port(id_priv);
3853 	if (ret)
3854 		goto err2;
3855 
3856 	if (!cma_any_addr(addr))
3857 		rdma_restrack_add(&id_priv->res);
3858 	return 0;
3859 err2:
3860 	if (id_priv->cma_dev)
3861 		cma_release_dev(id_priv);
3862 err1:
3863 	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3864 	return ret;
3865 }
3866 EXPORT_SYMBOL(rdma_bind_addr);
3867 
3868 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3869 {
3870 	struct cma_hdr *cma_hdr;
3871 
3872 	cma_hdr = hdr;
3873 	cma_hdr->cma_version = CMA_VERSION;
3874 	if (cma_family(id_priv) == AF_INET) {
3875 		struct sockaddr_in *src4, *dst4;
3876 
3877 		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3878 		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3879 
3880 		cma_set_ip_ver(cma_hdr, 4);
3881 		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3882 		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3883 		cma_hdr->port = src4->sin_port;
3884 	} else if (cma_family(id_priv) == AF_INET6) {
3885 		struct sockaddr_in6 *src6, *dst6;
3886 
3887 		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3888 		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3889 
3890 		cma_set_ip_ver(cma_hdr, 6);
3891 		cma_hdr->src_addr.ip6 = src6->sin6_addr;
3892 		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3893 		cma_hdr->port = src6->sin6_port;
3894 	}
3895 	return 0;
3896 }
3897 
3898 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3899 				const struct ib_cm_event *ib_event)
3900 {
3901 	struct rdma_id_private *id_priv = cm_id->context;
3902 	struct rdma_cm_event event = {};
3903 	const struct ib_cm_sidr_rep_event_param *rep =
3904 				&ib_event->param.sidr_rep_rcvd;
3905 	int ret;
3906 
3907 	mutex_lock(&id_priv->handler_mutex);
3908 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
3909 		goto out;
3910 
3911 	switch (ib_event->event) {
3912 	case IB_CM_SIDR_REQ_ERROR:
3913 		event.event = RDMA_CM_EVENT_UNREACHABLE;
3914 		event.status = -ETIMEDOUT;
3915 		break;
3916 	case IB_CM_SIDR_REP_RECEIVED:
3917 		event.param.ud.private_data = ib_event->private_data;
3918 		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3919 		if (rep->status != IB_SIDR_SUCCESS) {
3920 			event.event = RDMA_CM_EVENT_UNREACHABLE;
3921 			event.status = ib_event->param.sidr_rep_rcvd.status;
3922 			pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3923 					     event.status);
3924 			break;
3925 		}
3926 		ret = cma_set_qkey(id_priv, rep->qkey);
3927 		if (ret) {
3928 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3929 			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3930 			event.status = ret;
3931 			break;
3932 		}
3933 		ib_init_ah_attr_from_path(id_priv->id.device,
3934 					  id_priv->id.port_num,
3935 					  id_priv->id.route.path_rec,
3936 					  &event.param.ud.ah_attr,
3937 					  rep->sgid_attr);
3938 		event.param.ud.qp_num = rep->qpn;
3939 		event.param.ud.qkey = rep->qkey;
3940 		event.event = RDMA_CM_EVENT_ESTABLISHED;
3941 		event.status = 0;
3942 		break;
3943 	default:
3944 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3945 		       ib_event->event);
3946 		goto out;
3947 	}
3948 
3949 	ret = cma_cm_event_handler(id_priv, &event);
3950 
3951 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
3952 	if (ret) {
3953 		/* Destroy the CM ID by returning a non-zero value. */
3954 		id_priv->cm_id.ib = NULL;
3955 		destroy_id_handler_unlock(id_priv);
3956 		return ret;
3957 	}
3958 out:
3959 	mutex_unlock(&id_priv->handler_mutex);
3960 	return 0;
3961 }
3962 
3963 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3964 			      struct rdma_conn_param *conn_param)
3965 {
3966 	struct ib_cm_sidr_req_param req;
3967 	struct ib_cm_id	*id;
3968 	void *private_data;
3969 	u8 offset;
3970 	int ret;
3971 
3972 	memset(&req, 0, sizeof req);
3973 	offset = cma_user_data_offset(id_priv);
3974 	req.private_data_len = offset + conn_param->private_data_len;
3975 	if (req.private_data_len < conn_param->private_data_len)
3976 		return -EINVAL;
3977 
3978 	if (req.private_data_len) {
3979 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3980 		if (!private_data)
3981 			return -ENOMEM;
3982 	} else {
3983 		private_data = NULL;
3984 	}
3985 
3986 	if (conn_param->private_data && conn_param->private_data_len)
3987 		memcpy(private_data + offset, conn_param->private_data,
3988 		       conn_param->private_data_len);
3989 
3990 	if (private_data) {
3991 		ret = cma_format_hdr(private_data, id_priv);
3992 		if (ret)
3993 			goto out;
3994 		req.private_data = private_data;
3995 	}
3996 
3997 	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3998 			     id_priv);
3999 	if (IS_ERR(id)) {
4000 		ret = PTR_ERR(id);
4001 		goto out;
4002 	}
4003 	id_priv->cm_id.ib = id;
4004 
4005 	req.path = id_priv->id.route.path_rec;
4006 	req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4007 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4008 	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4009 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4010 
4011 	trace_cm_send_sidr_req(id_priv);
4012 	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4013 	if (ret) {
4014 		ib_destroy_cm_id(id_priv->cm_id.ib);
4015 		id_priv->cm_id.ib = NULL;
4016 	}
4017 out:
4018 	kfree(private_data);
4019 	return ret;
4020 }
4021 
4022 static int cma_connect_ib(struct rdma_id_private *id_priv,
4023 			  struct rdma_conn_param *conn_param)
4024 {
4025 	struct ib_cm_req_param req;
4026 	struct rdma_route *route;
4027 	void *private_data;
4028 	struct ib_cm_id	*id;
4029 	u8 offset;
4030 	int ret;
4031 
4032 	memset(&req, 0, sizeof req);
4033 	offset = cma_user_data_offset(id_priv);
4034 	req.private_data_len = offset + conn_param->private_data_len;
4035 	if (req.private_data_len < conn_param->private_data_len)
4036 		return -EINVAL;
4037 
4038 	if (req.private_data_len) {
4039 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4040 		if (!private_data)
4041 			return -ENOMEM;
4042 	} else {
4043 		private_data = NULL;
4044 	}
4045 
4046 	if (conn_param->private_data && conn_param->private_data_len)
4047 		memcpy(private_data + offset, conn_param->private_data,
4048 		       conn_param->private_data_len);
4049 
4050 	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
4051 	if (IS_ERR(id)) {
4052 		ret = PTR_ERR(id);
4053 		goto out;
4054 	}
4055 	id_priv->cm_id.ib = id;
4056 
4057 	route = &id_priv->id.route;
4058 	if (private_data) {
4059 		ret = cma_format_hdr(private_data, id_priv);
4060 		if (ret)
4061 			goto out;
4062 		req.private_data = private_data;
4063 	}
4064 
4065 	req.primary_path = &route->path_rec[0];
4066 	if (route->num_paths == 2)
4067 		req.alternate_path = &route->path_rec[1];
4068 
4069 	req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4070 	/* Alternate path SGID attribute currently unsupported */
4071 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4072 	req.qp_num = id_priv->qp_num;
4073 	req.qp_type = id_priv->id.qp_type;
4074 	req.starting_psn = id_priv->seq_num;
4075 	req.responder_resources = conn_param->responder_resources;
4076 	req.initiator_depth = conn_param->initiator_depth;
4077 	req.flow_control = conn_param->flow_control;
4078 	req.retry_count = min_t(u8, 7, conn_param->retry_count);
4079 	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4080 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4081 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4082 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4083 	req.srq = id_priv->srq ? 1 : 0;
4084 	req.ece.vendor_id = id_priv->ece.vendor_id;
4085 	req.ece.attr_mod = id_priv->ece.attr_mod;
4086 
4087 	trace_cm_send_req(id_priv);
4088 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4089 out:
4090 	if (ret && !IS_ERR(id)) {
4091 		ib_destroy_cm_id(id);
4092 		id_priv->cm_id.ib = NULL;
4093 	}
4094 
4095 	kfree(private_data);
4096 	return ret;
4097 }
4098 
4099 static int cma_connect_iw(struct rdma_id_private *id_priv,
4100 			  struct rdma_conn_param *conn_param)
4101 {
4102 	struct iw_cm_id *cm_id;
4103 	int ret;
4104 	struct iw_cm_conn_param iw_param;
4105 
4106 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4107 	if (IS_ERR(cm_id))
4108 		return PTR_ERR(cm_id);
4109 
4110 	cm_id->tos = id_priv->tos;
4111 	cm_id->tos_set = id_priv->tos_set;
4112 	id_priv->cm_id.iw = cm_id;
4113 
4114 	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4115 	       rdma_addr_size(cma_src_addr(id_priv)));
4116 	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4117 	       rdma_addr_size(cma_dst_addr(id_priv)));
4118 
4119 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4120 	if (ret)
4121 		goto out;
4122 
4123 	if (conn_param) {
4124 		iw_param.ord = conn_param->initiator_depth;
4125 		iw_param.ird = conn_param->responder_resources;
4126 		iw_param.private_data = conn_param->private_data;
4127 		iw_param.private_data_len = conn_param->private_data_len;
4128 		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4129 	} else {
4130 		memset(&iw_param, 0, sizeof iw_param);
4131 		iw_param.qpn = id_priv->qp_num;
4132 	}
4133 	ret = iw_cm_connect(cm_id, &iw_param);
4134 out:
4135 	if (ret) {
4136 		iw_destroy_cm_id(cm_id);
4137 		id_priv->cm_id.iw = NULL;
4138 	}
4139 	return ret;
4140 }
4141 
4142 /**
4143  * rdma_connect_locked - Initiate an active connection request.
4144  * @id: Connection identifier to connect.
4145  * @conn_param: Connection information used for connected QPs.
4146  *
4147  * Same as rdma_connect() but can only be called from the
4148  * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4149  */
4150 int rdma_connect_locked(struct rdma_cm_id *id,
4151 			struct rdma_conn_param *conn_param)
4152 {
4153 	struct rdma_id_private *id_priv =
4154 		container_of(id, struct rdma_id_private, id);
4155 	int ret;
4156 
4157 	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4158 		return -EINVAL;
4159 
4160 	if (!id->qp) {
4161 		id_priv->qp_num = conn_param->qp_num;
4162 		id_priv->srq = conn_param->srq;
4163 	}
4164 
4165 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4166 		if (id->qp_type == IB_QPT_UD)
4167 			ret = cma_resolve_ib_udp(id_priv, conn_param);
4168 		else
4169 			ret = cma_connect_ib(id_priv, conn_param);
4170 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4171 		ret = cma_connect_iw(id_priv, conn_param);
4172 	} else {
4173 		ret = -ENOSYS;
4174 	}
4175 	if (ret)
4176 		goto err_state;
4177 	return 0;
4178 err_state:
4179 	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4180 	return ret;
4181 }
4182 EXPORT_SYMBOL(rdma_connect_locked);
4183 
4184 /**
4185  * rdma_connect - Initiate an active connection request.
4186  * @id: Connection identifier to connect.
4187  * @conn_param: Connection information used for connected QPs.
4188  *
4189  * Users must have resolved a route for the rdma_cm_id to connect with by having
4190  * called rdma_resolve_route before calling this routine.
4191  *
4192  * This call will either connect to a remote QP or obtain remote QP information
4193  * for unconnected rdma_cm_id's.  The actual operation is based on the
4194  * rdma_cm_id's port space.
4195  */
4196 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4197 {
4198 	struct rdma_id_private *id_priv =
4199 		container_of(id, struct rdma_id_private, id);
4200 	int ret;
4201 
4202 	mutex_lock(&id_priv->handler_mutex);
4203 	ret = rdma_connect_locked(id, conn_param);
4204 	mutex_unlock(&id_priv->handler_mutex);
4205 	return ret;
4206 }
4207 EXPORT_SYMBOL(rdma_connect);
4208 
4209 /**
4210  * rdma_connect_ece - Initiate an active connection request with ECE data.
4211  * @id: Connection identifier to connect.
4212  * @conn_param: Connection information used for connected QPs.
4213  * @ece: ECE parameters
4214  *
4215  * See rdma_connect() explanation.
4216  */
4217 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4218 		     struct rdma_ucm_ece *ece)
4219 {
4220 	struct rdma_id_private *id_priv =
4221 		container_of(id, struct rdma_id_private, id);
4222 
4223 	id_priv->ece.vendor_id = ece->vendor_id;
4224 	id_priv->ece.attr_mod = ece->attr_mod;
4225 
4226 	return rdma_connect(id, conn_param);
4227 }
4228 EXPORT_SYMBOL(rdma_connect_ece);
4229 
4230 static int cma_accept_ib(struct rdma_id_private *id_priv,
4231 			 struct rdma_conn_param *conn_param)
4232 {
4233 	struct ib_cm_rep_param rep;
4234 	int ret;
4235 
4236 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4237 	if (ret)
4238 		goto out;
4239 
4240 	ret = cma_modify_qp_rts(id_priv, conn_param);
4241 	if (ret)
4242 		goto out;
4243 
4244 	memset(&rep, 0, sizeof rep);
4245 	rep.qp_num = id_priv->qp_num;
4246 	rep.starting_psn = id_priv->seq_num;
4247 	rep.private_data = conn_param->private_data;
4248 	rep.private_data_len = conn_param->private_data_len;
4249 	rep.responder_resources = conn_param->responder_resources;
4250 	rep.initiator_depth = conn_param->initiator_depth;
4251 	rep.failover_accepted = 0;
4252 	rep.flow_control = conn_param->flow_control;
4253 	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4254 	rep.srq = id_priv->srq ? 1 : 0;
4255 	rep.ece.vendor_id = id_priv->ece.vendor_id;
4256 	rep.ece.attr_mod = id_priv->ece.attr_mod;
4257 
4258 	trace_cm_send_rep(id_priv);
4259 	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4260 out:
4261 	return ret;
4262 }
4263 
4264 static int cma_accept_iw(struct rdma_id_private *id_priv,
4265 		  struct rdma_conn_param *conn_param)
4266 {
4267 	struct iw_cm_conn_param iw_param;
4268 	int ret;
4269 
4270 	if (!conn_param)
4271 		return -EINVAL;
4272 
4273 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4274 	if (ret)
4275 		return ret;
4276 
4277 	iw_param.ord = conn_param->initiator_depth;
4278 	iw_param.ird = conn_param->responder_resources;
4279 	iw_param.private_data = conn_param->private_data;
4280 	iw_param.private_data_len = conn_param->private_data_len;
4281 	if (id_priv->id.qp)
4282 		iw_param.qpn = id_priv->qp_num;
4283 	else
4284 		iw_param.qpn = conn_param->qp_num;
4285 
4286 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4287 }
4288 
4289 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4290 			     enum ib_cm_sidr_status status, u32 qkey,
4291 			     const void *private_data, int private_data_len)
4292 {
4293 	struct ib_cm_sidr_rep_param rep;
4294 	int ret;
4295 
4296 	memset(&rep, 0, sizeof rep);
4297 	rep.status = status;
4298 	if (status == IB_SIDR_SUCCESS) {
4299 		ret = cma_set_qkey(id_priv, qkey);
4300 		if (ret)
4301 			return ret;
4302 		rep.qp_num = id_priv->qp_num;
4303 		rep.qkey = id_priv->qkey;
4304 
4305 		rep.ece.vendor_id = id_priv->ece.vendor_id;
4306 		rep.ece.attr_mod = id_priv->ece.attr_mod;
4307 	}
4308 
4309 	rep.private_data = private_data;
4310 	rep.private_data_len = private_data_len;
4311 
4312 	trace_cm_send_sidr_rep(id_priv);
4313 	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4314 }
4315 
4316 /**
4317  * rdma_accept - Called to accept a connection request or response.
4318  * @id: Connection identifier associated with the request.
4319  * @conn_param: Information needed to establish the connection.  This must be
4320  *   provided if accepting a connection request.  If accepting a connection
4321  *   response, this parameter must be NULL.
4322  *
4323  * Typically, this routine is only called by the listener to accept a connection
4324  * request.  It must also be called on the active side of a connection if the
4325  * user is performing their own QP transitions.
4326  *
4327  * In the case of error, a reject message is sent to the remote side and the
4328  * state of the qp associated with the id is modified to error, such that any
4329  * previously posted receive buffers would be flushed.
4330  *
4331  * This function is for use by kernel ULPs and must be called from under the
4332  * handler callback.
4333  */
4334 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4335 {
4336 	struct rdma_id_private *id_priv =
4337 		container_of(id, struct rdma_id_private, id);
4338 	int ret;
4339 
4340 	lockdep_assert_held(&id_priv->handler_mutex);
4341 
4342 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4343 		return -EINVAL;
4344 
4345 	if (!id->qp && conn_param) {
4346 		id_priv->qp_num = conn_param->qp_num;
4347 		id_priv->srq = conn_param->srq;
4348 	}
4349 
4350 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4351 		if (id->qp_type == IB_QPT_UD) {
4352 			if (conn_param)
4353 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4354 							conn_param->qkey,
4355 							conn_param->private_data,
4356 							conn_param->private_data_len);
4357 			else
4358 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4359 							0, NULL, 0);
4360 		} else {
4361 			if (conn_param)
4362 				ret = cma_accept_ib(id_priv, conn_param);
4363 			else
4364 				ret = cma_rep_recv(id_priv);
4365 		}
4366 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4367 		ret = cma_accept_iw(id_priv, conn_param);
4368 	} else {
4369 		ret = -ENOSYS;
4370 	}
4371 	if (ret)
4372 		goto reject;
4373 
4374 	return 0;
4375 reject:
4376 	cma_modify_qp_err(id_priv);
4377 	rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4378 	return ret;
4379 }
4380 EXPORT_SYMBOL(rdma_accept);
4381 
4382 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4383 		    struct rdma_ucm_ece *ece)
4384 {
4385 	struct rdma_id_private *id_priv =
4386 		container_of(id, struct rdma_id_private, id);
4387 
4388 	id_priv->ece.vendor_id = ece->vendor_id;
4389 	id_priv->ece.attr_mod = ece->attr_mod;
4390 
4391 	return rdma_accept(id, conn_param);
4392 }
4393 EXPORT_SYMBOL(rdma_accept_ece);
4394 
4395 void rdma_lock_handler(struct rdma_cm_id *id)
4396 {
4397 	struct rdma_id_private *id_priv =
4398 		container_of(id, struct rdma_id_private, id);
4399 
4400 	mutex_lock(&id_priv->handler_mutex);
4401 }
4402 EXPORT_SYMBOL(rdma_lock_handler);
4403 
4404 void rdma_unlock_handler(struct rdma_cm_id *id)
4405 {
4406 	struct rdma_id_private *id_priv =
4407 		container_of(id, struct rdma_id_private, id);
4408 
4409 	mutex_unlock(&id_priv->handler_mutex);
4410 }
4411 EXPORT_SYMBOL(rdma_unlock_handler);
4412 
4413 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4414 {
4415 	struct rdma_id_private *id_priv;
4416 	int ret;
4417 
4418 	id_priv = container_of(id, struct rdma_id_private, id);
4419 	if (!id_priv->cm_id.ib)
4420 		return -EINVAL;
4421 
4422 	switch (id->device->node_type) {
4423 	case RDMA_NODE_IB_CA:
4424 		ret = ib_cm_notify(id_priv->cm_id.ib, event);
4425 		break;
4426 	default:
4427 		ret = 0;
4428 		break;
4429 	}
4430 	return ret;
4431 }
4432 EXPORT_SYMBOL(rdma_notify);
4433 
4434 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4435 		u8 private_data_len, u8 reason)
4436 {
4437 	struct rdma_id_private *id_priv;
4438 	int ret;
4439 
4440 	id_priv = container_of(id, struct rdma_id_private, id);
4441 	if (!id_priv->cm_id.ib)
4442 		return -EINVAL;
4443 
4444 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4445 		if (id->qp_type == IB_QPT_UD) {
4446 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4447 						private_data, private_data_len);
4448 		} else {
4449 			trace_cm_send_rej(id_priv);
4450 			ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4451 					     private_data, private_data_len);
4452 		}
4453 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4454 		ret = iw_cm_reject(id_priv->cm_id.iw,
4455 				   private_data, private_data_len);
4456 	} else {
4457 		ret = -ENOSYS;
4458 	}
4459 
4460 	return ret;
4461 }
4462 EXPORT_SYMBOL(rdma_reject);
4463 
4464 int rdma_disconnect(struct rdma_cm_id *id)
4465 {
4466 	struct rdma_id_private *id_priv;
4467 	int ret;
4468 
4469 	id_priv = container_of(id, struct rdma_id_private, id);
4470 	if (!id_priv->cm_id.ib)
4471 		return -EINVAL;
4472 
4473 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4474 		ret = cma_modify_qp_err(id_priv);
4475 		if (ret)
4476 			goto out;
4477 		/* Initiate or respond to a disconnect. */
4478 		trace_cm_disconnect(id_priv);
4479 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4480 			if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4481 				trace_cm_sent_drep(id_priv);
4482 		} else {
4483 			trace_cm_sent_dreq(id_priv);
4484 		}
4485 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4486 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4487 	} else
4488 		ret = -EINVAL;
4489 
4490 out:
4491 	return ret;
4492 }
4493 EXPORT_SYMBOL(rdma_disconnect);
4494 
4495 static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4496 			      struct ib_sa_multicast *multicast,
4497 			      struct rdma_cm_event *event,
4498 			      struct cma_multicast *mc)
4499 {
4500 	struct rdma_dev_addr *dev_addr;
4501 	enum ib_gid_type gid_type;
4502 	struct net_device *ndev;
4503 
4504 	if (!status)
4505 		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4506 	else
4507 		pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4508 				     status);
4509 
4510 	event->status = status;
4511 	event->param.ud.private_data = mc->context;
4512 	if (status) {
4513 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4514 		return;
4515 	}
4516 
4517 	dev_addr = &id_priv->id.route.addr.dev_addr;
4518 	ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4519 	gid_type =
4520 		id_priv->cma_dev
4521 			->default_gid_type[id_priv->id.port_num -
4522 					   rdma_start_port(
4523 						   id_priv->cma_dev->device)];
4524 
4525 	event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4526 	if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4527 				     &multicast->rec, ndev, gid_type,
4528 				     &event->param.ud.ah_attr)) {
4529 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4530 		goto out;
4531 	}
4532 
4533 	event->param.ud.qp_num = 0xFFFFFF;
4534 	event->param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
4535 
4536 out:
4537 	if (ndev)
4538 		dev_put(ndev);
4539 }
4540 
4541 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4542 {
4543 	struct cma_multicast *mc = multicast->context;
4544 	struct rdma_id_private *id_priv = mc->id_priv;
4545 	struct rdma_cm_event event = {};
4546 	int ret = 0;
4547 
4548 	mutex_lock(&id_priv->handler_mutex);
4549 	if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4550 	    READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4551 		goto out;
4552 
4553 	cma_make_mc_event(status, id_priv, multicast, &event, mc);
4554 	ret = cma_cm_event_handler(id_priv, &event);
4555 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4556 	WARN_ON(ret);
4557 
4558 out:
4559 	mutex_unlock(&id_priv->handler_mutex);
4560 	return 0;
4561 }
4562 
4563 static void cma_set_mgid(struct rdma_id_private *id_priv,
4564 			 struct sockaddr *addr, union ib_gid *mgid)
4565 {
4566 	unsigned char mc_map[MAX_ADDR_LEN];
4567 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4568 	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4569 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4570 
4571 	if (cma_any_addr(addr)) {
4572 		memset(mgid, 0, sizeof *mgid);
4573 	} else if ((addr->sa_family == AF_INET6) &&
4574 		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4575 								 0xFF10A01B)) {
4576 		/* IPv6 address is an SA assigned MGID. */
4577 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4578 	} else if (addr->sa_family == AF_IB) {
4579 		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4580 	} else if (addr->sa_family == AF_INET6) {
4581 		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4582 		if (id_priv->id.ps == RDMA_PS_UDP)
4583 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4584 		*mgid = *(union ib_gid *) (mc_map + 4);
4585 	} else {
4586 		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4587 		if (id_priv->id.ps == RDMA_PS_UDP)
4588 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4589 		*mgid = *(union ib_gid *) (mc_map + 4);
4590 	}
4591 }
4592 
4593 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4594 				 struct cma_multicast *mc)
4595 {
4596 	struct ib_sa_mcmember_rec rec;
4597 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4598 	ib_sa_comp_mask comp_mask;
4599 	int ret;
4600 
4601 	ib_addr_get_mgid(dev_addr, &rec.mgid);
4602 	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4603 				     &rec.mgid, &rec);
4604 	if (ret)
4605 		return ret;
4606 
4607 	ret = cma_set_qkey(id_priv, 0);
4608 	if (ret)
4609 		return ret;
4610 
4611 	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4612 	rec.qkey = cpu_to_be32(id_priv->qkey);
4613 	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4614 	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4615 	rec.join_state = mc->join_state;
4616 
4617 	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4618 		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4619 		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4620 		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
4621 		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4622 
4623 	if (id_priv->id.ps == RDMA_PS_IPOIB)
4624 		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4625 			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4626 			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4627 			     IB_SA_MCMEMBER_REC_MTU |
4628 			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
4629 
4630 	mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4631 					 id_priv->id.port_num, &rec, comp_mask,
4632 					 GFP_KERNEL, cma_ib_mc_handler, mc);
4633 	return PTR_ERR_OR_ZERO(mc->sa_mc);
4634 }
4635 
4636 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4637 			      enum ib_gid_type gid_type)
4638 {
4639 	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4640 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4641 
4642 	if (cma_any_addr(addr)) {
4643 		memset(mgid, 0, sizeof *mgid);
4644 	} else if (addr->sa_family == AF_INET6) {
4645 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4646 	} else {
4647 		mgid->raw[0] =
4648 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4649 		mgid->raw[1] =
4650 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4651 		mgid->raw[2] = 0;
4652 		mgid->raw[3] = 0;
4653 		mgid->raw[4] = 0;
4654 		mgid->raw[5] = 0;
4655 		mgid->raw[6] = 0;
4656 		mgid->raw[7] = 0;
4657 		mgid->raw[8] = 0;
4658 		mgid->raw[9] = 0;
4659 		mgid->raw[10] = 0xff;
4660 		mgid->raw[11] = 0xff;
4661 		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4662 	}
4663 }
4664 
4665 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4666 				   struct cma_multicast *mc)
4667 {
4668 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4669 	int err = 0;
4670 	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4671 	struct net_device *ndev = NULL;
4672 	struct ib_sa_multicast ib;
4673 	enum ib_gid_type gid_type;
4674 	bool send_only;
4675 
4676 	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4677 
4678 	if (cma_zero_addr(addr))
4679 		return -EINVAL;
4680 
4681 	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4682 		   rdma_start_port(id_priv->cma_dev->device)];
4683 	cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
4684 
4685 	ib.rec.pkey = cpu_to_be16(0xffff);
4686 	if (id_priv->id.ps == RDMA_PS_UDP)
4687 		ib.rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4688 
4689 	if (dev_addr->bound_dev_if)
4690 		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4691 	if (!ndev)
4692 		return -ENODEV;
4693 
4694 	ib.rec.rate = iboe_get_rate(ndev);
4695 	ib.rec.hop_limit = 1;
4696 	ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4697 
4698 	if (addr->sa_family == AF_INET) {
4699 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4700 			ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4701 			if (!send_only) {
4702 				err = cma_igmp_send(ndev, &ib.rec.mgid,
4703 						    true);
4704 			}
4705 		}
4706 	} else {
4707 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4708 			err = -ENOTSUPP;
4709 	}
4710 	dev_put(ndev);
4711 	if (err || !ib.rec.mtu)
4712 		return err ?: -EINVAL;
4713 
4714 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4715 		    &ib.rec.port_gid);
4716 	INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
4717 	cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
4718 	queue_work(cma_wq, &mc->iboe_join.work);
4719 	return 0;
4720 }
4721 
4722 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4723 			u8 join_state, void *context)
4724 {
4725 	struct rdma_id_private *id_priv =
4726 		container_of(id, struct rdma_id_private, id);
4727 	struct cma_multicast *mc;
4728 	int ret;
4729 
4730 	/* Not supported for kernel QPs */
4731 	if (WARN_ON(id->qp))
4732 		return -EINVAL;
4733 
4734 	/* ULP is calling this wrong. */
4735 	if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
4736 			    READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
4737 		return -EINVAL;
4738 
4739 	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
4740 	if (!mc)
4741 		return -ENOMEM;
4742 
4743 	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4744 	mc->context = context;
4745 	mc->id_priv = id_priv;
4746 	mc->join_state = join_state;
4747 
4748 	if (rdma_protocol_roce(id->device, id->port_num)) {
4749 		ret = cma_iboe_join_multicast(id_priv, mc);
4750 		if (ret)
4751 			goto out_err;
4752 	} else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4753 		ret = cma_join_ib_multicast(id_priv, mc);
4754 		if (ret)
4755 			goto out_err;
4756 	} else {
4757 		ret = -ENOSYS;
4758 		goto out_err;
4759 	}
4760 
4761 	spin_lock(&id_priv->lock);
4762 	list_add(&mc->list, &id_priv->mc_list);
4763 	spin_unlock(&id_priv->lock);
4764 
4765 	return 0;
4766 out_err:
4767 	kfree(mc);
4768 	return ret;
4769 }
4770 EXPORT_SYMBOL(rdma_join_multicast);
4771 
4772 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4773 {
4774 	struct rdma_id_private *id_priv;
4775 	struct cma_multicast *mc;
4776 
4777 	id_priv = container_of(id, struct rdma_id_private, id);
4778 	spin_lock_irq(&id_priv->lock);
4779 	list_for_each_entry(mc, &id_priv->mc_list, list) {
4780 		if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
4781 			continue;
4782 		list_del(&mc->list);
4783 		spin_unlock_irq(&id_priv->lock);
4784 
4785 		WARN_ON(id_priv->cma_dev->device != id->device);
4786 		destroy_mc(id_priv, mc);
4787 		return;
4788 	}
4789 	spin_unlock_irq(&id_priv->lock);
4790 }
4791 EXPORT_SYMBOL(rdma_leave_multicast);
4792 
4793 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4794 {
4795 	struct rdma_dev_addr *dev_addr;
4796 	struct cma_work *work;
4797 
4798 	dev_addr = &id_priv->id.route.addr.dev_addr;
4799 
4800 	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4801 	    (net_eq(dev_net(ndev), dev_addr->net)) &&
4802 	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4803 		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4804 			ndev->name, &id_priv->id);
4805 		work = kzalloc(sizeof *work, GFP_KERNEL);
4806 		if (!work)
4807 			return -ENOMEM;
4808 
4809 		INIT_WORK(&work->work, cma_work_handler);
4810 		work->id = id_priv;
4811 		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4812 		cma_id_get(id_priv);
4813 		queue_work(cma_wq, &work->work);
4814 	}
4815 
4816 	return 0;
4817 }
4818 
4819 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4820 			       void *ptr)
4821 {
4822 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4823 	struct cma_device *cma_dev;
4824 	struct rdma_id_private *id_priv;
4825 	int ret = NOTIFY_DONE;
4826 
4827 	if (event != NETDEV_BONDING_FAILOVER)
4828 		return NOTIFY_DONE;
4829 
4830 	if (!netif_is_bond_master(ndev))
4831 		return NOTIFY_DONE;
4832 
4833 	mutex_lock(&lock);
4834 	list_for_each_entry(cma_dev, &dev_list, list)
4835 		list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4836 			ret = cma_netdev_change(ndev, id_priv);
4837 			if (ret)
4838 				goto out;
4839 		}
4840 
4841 out:
4842 	mutex_unlock(&lock);
4843 	return ret;
4844 }
4845 
4846 static struct notifier_block cma_nb = {
4847 	.notifier_call = cma_netdev_callback
4848 };
4849 
4850 static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
4851 {
4852 	struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
4853 	enum rdma_cm_state state;
4854 	unsigned long flags;
4855 
4856 	mutex_lock(&id_priv->handler_mutex);
4857 	/* Record that we want to remove the device */
4858 	spin_lock_irqsave(&id_priv->lock, flags);
4859 	state = id_priv->state;
4860 	if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
4861 		spin_unlock_irqrestore(&id_priv->lock, flags);
4862 		mutex_unlock(&id_priv->handler_mutex);
4863 		cma_id_put(id_priv);
4864 		return;
4865 	}
4866 	id_priv->state = RDMA_CM_DEVICE_REMOVAL;
4867 	spin_unlock_irqrestore(&id_priv->lock, flags);
4868 
4869 	if (cma_cm_event_handler(id_priv, &event)) {
4870 		/*
4871 		 * At this point the ULP promises it won't call
4872 		 * rdma_destroy_id() concurrently
4873 		 */
4874 		cma_id_put(id_priv);
4875 		mutex_unlock(&id_priv->handler_mutex);
4876 		trace_cm_id_destroy(id_priv);
4877 		_destroy_id(id_priv, state);
4878 		return;
4879 	}
4880 	mutex_unlock(&id_priv->handler_mutex);
4881 
4882 	/*
4883 	 * If this races with destroy then the thread that first assigns state
4884 	 * to a destroying does the cancel.
4885 	 */
4886 	cma_cancel_operation(id_priv, state);
4887 	cma_id_put(id_priv);
4888 }
4889 
4890 static void cma_process_remove(struct cma_device *cma_dev)
4891 {
4892 	mutex_lock(&lock);
4893 	while (!list_empty(&cma_dev->id_list)) {
4894 		struct rdma_id_private *id_priv = list_first_entry(
4895 			&cma_dev->id_list, struct rdma_id_private, list);
4896 
4897 		list_del(&id_priv->listen_list);
4898 		list_del_init(&id_priv->list);
4899 		cma_id_get(id_priv);
4900 		mutex_unlock(&lock);
4901 
4902 		cma_send_device_removal_put(id_priv);
4903 
4904 		mutex_lock(&lock);
4905 	}
4906 	mutex_unlock(&lock);
4907 
4908 	cma_dev_put(cma_dev);
4909 	wait_for_completion(&cma_dev->comp);
4910 }
4911 
4912 static bool cma_supported(struct ib_device *device)
4913 {
4914 	u32 i;
4915 
4916 	rdma_for_each_port(device, i) {
4917 		if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
4918 			return true;
4919 	}
4920 	return false;
4921 }
4922 
4923 static int cma_add_one(struct ib_device *device)
4924 {
4925 	struct rdma_id_private *to_destroy;
4926 	struct cma_device *cma_dev;
4927 	struct rdma_id_private *id_priv;
4928 	unsigned long supported_gids = 0;
4929 	int ret;
4930 	u32 i;
4931 
4932 	if (!cma_supported(device))
4933 		return -EOPNOTSUPP;
4934 
4935 	cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
4936 	if (!cma_dev)
4937 		return -ENOMEM;
4938 
4939 	cma_dev->device = device;
4940 	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4941 					    sizeof(*cma_dev->default_gid_type),
4942 					    GFP_KERNEL);
4943 	if (!cma_dev->default_gid_type) {
4944 		ret = -ENOMEM;
4945 		goto free_cma_dev;
4946 	}
4947 
4948 	cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
4949 					    sizeof(*cma_dev->default_roce_tos),
4950 					    GFP_KERNEL);
4951 	if (!cma_dev->default_roce_tos) {
4952 		ret = -ENOMEM;
4953 		goto free_gid_type;
4954 	}
4955 
4956 	rdma_for_each_port (device, i) {
4957 		supported_gids = roce_gid_type_mask_support(device, i);
4958 		WARN_ON(!supported_gids);
4959 		if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
4960 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
4961 				CMA_PREFERRED_ROCE_GID_TYPE;
4962 		else
4963 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
4964 				find_first_bit(&supported_gids, BITS_PER_LONG);
4965 		cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
4966 	}
4967 
4968 	init_completion(&cma_dev->comp);
4969 	refcount_set(&cma_dev->refcount, 1);
4970 	INIT_LIST_HEAD(&cma_dev->id_list);
4971 	ib_set_client_data(device, &cma_client, cma_dev);
4972 
4973 	mutex_lock(&lock);
4974 	list_add_tail(&cma_dev->list, &dev_list);
4975 	list_for_each_entry(id_priv, &listen_any_list, list) {
4976 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
4977 		if (ret)
4978 			goto free_listen;
4979 	}
4980 	mutex_unlock(&lock);
4981 
4982 	trace_cm_add_one(device);
4983 	return 0;
4984 
4985 free_listen:
4986 	list_del(&cma_dev->list);
4987 	mutex_unlock(&lock);
4988 
4989 	/* cma_process_remove() will delete to_destroy */
4990 	cma_process_remove(cma_dev);
4991 	kfree(cma_dev->default_roce_tos);
4992 free_gid_type:
4993 	kfree(cma_dev->default_gid_type);
4994 
4995 free_cma_dev:
4996 	kfree(cma_dev);
4997 	return ret;
4998 }
4999 
5000 static void cma_remove_one(struct ib_device *device, void *client_data)
5001 {
5002 	struct cma_device *cma_dev = client_data;
5003 
5004 	trace_cm_remove_one(device);
5005 
5006 	mutex_lock(&lock);
5007 	list_del(&cma_dev->list);
5008 	mutex_unlock(&lock);
5009 
5010 	cma_process_remove(cma_dev);
5011 	kfree(cma_dev->default_roce_tos);
5012 	kfree(cma_dev->default_gid_type);
5013 	kfree(cma_dev);
5014 }
5015 
5016 static int cma_init_net(struct net *net)
5017 {
5018 	struct cma_pernet *pernet = cma_pernet(net);
5019 
5020 	xa_init(&pernet->tcp_ps);
5021 	xa_init(&pernet->udp_ps);
5022 	xa_init(&pernet->ipoib_ps);
5023 	xa_init(&pernet->ib_ps);
5024 
5025 	return 0;
5026 }
5027 
5028 static void cma_exit_net(struct net *net)
5029 {
5030 	struct cma_pernet *pernet = cma_pernet(net);
5031 
5032 	WARN_ON(!xa_empty(&pernet->tcp_ps));
5033 	WARN_ON(!xa_empty(&pernet->udp_ps));
5034 	WARN_ON(!xa_empty(&pernet->ipoib_ps));
5035 	WARN_ON(!xa_empty(&pernet->ib_ps));
5036 }
5037 
5038 static struct pernet_operations cma_pernet_operations = {
5039 	.init = cma_init_net,
5040 	.exit = cma_exit_net,
5041 	.id = &cma_pernet_id,
5042 	.size = sizeof(struct cma_pernet),
5043 };
5044 
5045 static int __init cma_init(void)
5046 {
5047 	int ret;
5048 
5049 	/*
5050 	 * There is a rare lock ordering dependency in cma_netdev_callback()
5051 	 * that only happens when bonding is enabled. Teach lockdep that rtnl
5052 	 * must never be nested under lock so it can find these without having
5053 	 * to test with bonding.
5054 	 */
5055 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
5056 		rtnl_lock();
5057 		mutex_lock(&lock);
5058 		mutex_unlock(&lock);
5059 		rtnl_unlock();
5060 	}
5061 
5062 	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5063 	if (!cma_wq)
5064 		return -ENOMEM;
5065 
5066 	ret = register_pernet_subsys(&cma_pernet_operations);
5067 	if (ret)
5068 		goto err_wq;
5069 
5070 	ib_sa_register_client(&sa_client);
5071 	register_netdevice_notifier(&cma_nb);
5072 
5073 	ret = ib_register_client(&cma_client);
5074 	if (ret)
5075 		goto err;
5076 
5077 	ret = cma_configfs_init();
5078 	if (ret)
5079 		goto err_ib;
5080 
5081 	return 0;
5082 
5083 err_ib:
5084 	ib_unregister_client(&cma_client);
5085 err:
5086 	unregister_netdevice_notifier(&cma_nb);
5087 	ib_sa_unregister_client(&sa_client);
5088 	unregister_pernet_subsys(&cma_pernet_operations);
5089 err_wq:
5090 	destroy_workqueue(cma_wq);
5091 	return ret;
5092 }
5093 
5094 static void __exit cma_cleanup(void)
5095 {
5096 	cma_configfs_exit();
5097 	ib_unregister_client(&cma_client);
5098 	unregister_netdevice_notifier(&cma_nb);
5099 	ib_sa_unregister_client(&sa_client);
5100 	unregister_pernet_subsys(&cma_pernet_operations);
5101 	destroy_workqueue(cma_wq);
5102 }
5103 
5104 module_init(cma_init);
5105 module_exit(cma_cleanup);
5106