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