xref: /openbmc/linux/drivers/infiniband/core/cma.c (revision 10756dc5)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
4  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
5  * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
7  */
8 
9 #include <linux/completion.h>
10 #include <linux/in.h>
11 #include <linux/in6.h>
12 #include <linux/mutex.h>
13 #include <linux/random.h>
14 #include <linux/igmp.h>
15 #include <linux/xarray.h>
16 #include <linux/inetdevice.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <net/route.h>
20 
21 #include <net/net_namespace.h>
22 #include <net/netns/generic.h>
23 #include <net/tcp.h>
24 #include <net/ipv6.h>
25 #include <net/ip_fib.h>
26 #include <net/ip6_route.h>
27 
28 #include <rdma/rdma_cm.h>
29 #include <rdma/rdma_cm_ib.h>
30 #include <rdma/rdma_netlink.h>
31 #include <rdma/ib.h>
32 #include <rdma/ib_cache.h>
33 #include <rdma/ib_cm.h>
34 #include <rdma/ib_sa.h>
35 #include <rdma/iw_cm.h>
36 
37 #include "core_priv.h"
38 #include "cma_priv.h"
39 #include "cma_trace.h"
40 
41 MODULE_AUTHOR("Sean Hefty");
42 MODULE_DESCRIPTION("Generic RDMA CM Agent");
43 MODULE_LICENSE("Dual BSD/GPL");
44 
45 #define CMA_CM_RESPONSE_TIMEOUT 20
46 #define CMA_MAX_CM_RETRIES 15
47 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
48 #define CMA_IBOE_PACKET_LIFETIME 18
49 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
50 
51 static const char * const cma_events[] = {
52 	[RDMA_CM_EVENT_ADDR_RESOLVED]	 = "address resolved",
53 	[RDMA_CM_EVENT_ADDR_ERROR]	 = "address error",
54 	[RDMA_CM_EVENT_ROUTE_RESOLVED]	 = "route resolved ",
55 	[RDMA_CM_EVENT_ROUTE_ERROR]	 = "route error",
56 	[RDMA_CM_EVENT_CONNECT_REQUEST]	 = "connect request",
57 	[RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
58 	[RDMA_CM_EVENT_CONNECT_ERROR]	 = "connect error",
59 	[RDMA_CM_EVENT_UNREACHABLE]	 = "unreachable",
60 	[RDMA_CM_EVENT_REJECTED]	 = "rejected",
61 	[RDMA_CM_EVENT_ESTABLISHED]	 = "established",
62 	[RDMA_CM_EVENT_DISCONNECTED]	 = "disconnected",
63 	[RDMA_CM_EVENT_DEVICE_REMOVAL]	 = "device removal",
64 	[RDMA_CM_EVENT_MULTICAST_JOIN]	 = "multicast join",
65 	[RDMA_CM_EVENT_MULTICAST_ERROR]	 = "multicast error",
66 	[RDMA_CM_EVENT_ADDR_CHANGE]	 = "address change",
67 	[RDMA_CM_EVENT_TIMEWAIT_EXIT]	 = "timewait exit",
68 };
69 
70 static void cma_set_mgid(struct rdma_id_private *id_priv, struct sockaddr *addr,
71 			 union ib_gid *mgid);
72 
73 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
74 {
75 	size_t index = event;
76 
77 	return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
78 			cma_events[index] : "unrecognized event";
79 }
80 EXPORT_SYMBOL(rdma_event_msg);
81 
82 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
83 						int reason)
84 {
85 	if (rdma_ib_or_roce(id->device, id->port_num))
86 		return ibcm_reject_msg(reason);
87 
88 	if (rdma_protocol_iwarp(id->device, id->port_num))
89 		return iwcm_reject_msg(reason);
90 
91 	WARN_ON_ONCE(1);
92 	return "unrecognized transport";
93 }
94 EXPORT_SYMBOL(rdma_reject_msg);
95 
96 /**
97  * rdma_is_consumer_reject - return true if the consumer rejected the connect
98  *                           request.
99  * @id: Communication identifier that received the REJECT event.
100  * @reason: Value returned in the REJECT event status field.
101  */
102 static bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
103 {
104 	if (rdma_ib_or_roce(id->device, id->port_num))
105 		return reason == IB_CM_REJ_CONSUMER_DEFINED;
106 
107 	if (rdma_protocol_iwarp(id->device, id->port_num))
108 		return reason == -ECONNREFUSED;
109 
110 	WARN_ON_ONCE(1);
111 	return false;
112 }
113 
114 const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
115 				      struct rdma_cm_event *ev, u8 *data_len)
116 {
117 	const void *p;
118 
119 	if (rdma_is_consumer_reject(id, ev->status)) {
120 		*data_len = ev->param.conn.private_data_len;
121 		p = ev->param.conn.private_data;
122 	} else {
123 		*data_len = 0;
124 		p = NULL;
125 	}
126 	return p;
127 }
128 EXPORT_SYMBOL(rdma_consumer_reject_data);
129 
130 /**
131  * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
132  * @id: Communication Identifier
133  */
134 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
135 {
136 	struct rdma_id_private *id_priv;
137 
138 	id_priv = container_of(id, struct rdma_id_private, id);
139 	if (id->device->node_type == RDMA_NODE_RNIC)
140 		return id_priv->cm_id.iw;
141 	return NULL;
142 }
143 EXPORT_SYMBOL(rdma_iw_cm_id);
144 
145 /**
146  * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
147  * @res: rdma resource tracking entry pointer
148  */
149 struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
150 {
151 	struct rdma_id_private *id_priv =
152 		container_of(res, struct rdma_id_private, res);
153 
154 	return &id_priv->id;
155 }
156 EXPORT_SYMBOL(rdma_res_to_id);
157 
158 static int cma_add_one(struct ib_device *device);
159 static void cma_remove_one(struct ib_device *device, void *client_data);
160 
161 static struct ib_client cma_client = {
162 	.name   = "cma",
163 	.add    = cma_add_one,
164 	.remove = cma_remove_one
165 };
166 
167 static struct ib_sa_client sa_client;
168 static LIST_HEAD(dev_list);
169 static LIST_HEAD(listen_any_list);
170 static DEFINE_MUTEX(lock);
171 static struct workqueue_struct *cma_wq;
172 static unsigned int cma_pernet_id;
173 
174 struct cma_pernet {
175 	struct xarray tcp_ps;
176 	struct xarray udp_ps;
177 	struct xarray ipoib_ps;
178 	struct xarray ib_ps;
179 };
180 
181 static struct cma_pernet *cma_pernet(struct net *net)
182 {
183 	return net_generic(net, cma_pernet_id);
184 }
185 
186 static
187 struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
188 {
189 	struct cma_pernet *pernet = cma_pernet(net);
190 
191 	switch (ps) {
192 	case RDMA_PS_TCP:
193 		return &pernet->tcp_ps;
194 	case RDMA_PS_UDP:
195 		return &pernet->udp_ps;
196 	case RDMA_PS_IPOIB:
197 		return &pernet->ipoib_ps;
198 	case RDMA_PS_IB:
199 		return &pernet->ib_ps;
200 	default:
201 		return NULL;
202 	}
203 }
204 
205 struct cma_device {
206 	struct list_head	list;
207 	struct ib_device	*device;
208 	struct completion	comp;
209 	refcount_t refcount;
210 	struct list_head	id_list;
211 	enum ib_gid_type	*default_gid_type;
212 	u8			*default_roce_tos;
213 };
214 
215 struct rdma_bind_list {
216 	enum rdma_ucm_port_space ps;
217 	struct hlist_head	owners;
218 	unsigned short		port;
219 };
220 
221 static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
222 			struct rdma_bind_list *bind_list, int snum)
223 {
224 	struct xarray *xa = cma_pernet_xa(net, ps);
225 
226 	return xa_insert(xa, snum, bind_list, GFP_KERNEL);
227 }
228 
229 static struct rdma_bind_list *cma_ps_find(struct net *net,
230 					  enum rdma_ucm_port_space ps, int snum)
231 {
232 	struct xarray *xa = cma_pernet_xa(net, ps);
233 
234 	return xa_load(xa, snum);
235 }
236 
237 static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
238 			  int snum)
239 {
240 	struct xarray *xa = cma_pernet_xa(net, ps);
241 
242 	xa_erase(xa, snum);
243 }
244 
245 enum {
246 	CMA_OPTION_AFONLY,
247 };
248 
249 void cma_dev_get(struct cma_device *cma_dev)
250 {
251 	refcount_inc(&cma_dev->refcount);
252 }
253 
254 void cma_dev_put(struct cma_device *cma_dev)
255 {
256 	if (refcount_dec_and_test(&cma_dev->refcount))
257 		complete(&cma_dev->comp);
258 }
259 
260 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter	filter,
261 					     void		*cookie)
262 {
263 	struct cma_device *cma_dev;
264 	struct cma_device *found_cma_dev = NULL;
265 
266 	mutex_lock(&lock);
267 
268 	list_for_each_entry(cma_dev, &dev_list, list)
269 		if (filter(cma_dev->device, cookie)) {
270 			found_cma_dev = cma_dev;
271 			break;
272 		}
273 
274 	if (found_cma_dev)
275 		cma_dev_get(found_cma_dev);
276 	mutex_unlock(&lock);
277 	return found_cma_dev;
278 }
279 
280 int cma_get_default_gid_type(struct cma_device *cma_dev,
281 			     u32 port)
282 {
283 	if (!rdma_is_port_valid(cma_dev->device, port))
284 		return -EINVAL;
285 
286 	return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
287 }
288 
289 int cma_set_default_gid_type(struct cma_device *cma_dev,
290 			     u32 port,
291 			     enum ib_gid_type default_gid_type)
292 {
293 	unsigned long supported_gids;
294 
295 	if (!rdma_is_port_valid(cma_dev->device, port))
296 		return -EINVAL;
297 
298 	if (default_gid_type == IB_GID_TYPE_IB &&
299 	    rdma_protocol_roce_eth_encap(cma_dev->device, port))
300 		default_gid_type = IB_GID_TYPE_ROCE;
301 
302 	supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
303 
304 	if (!(supported_gids & 1 << default_gid_type))
305 		return -EINVAL;
306 
307 	cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
308 		default_gid_type;
309 
310 	return 0;
311 }
312 
313 int cma_get_default_roce_tos(struct cma_device *cma_dev, u32 port)
314 {
315 	if (!rdma_is_port_valid(cma_dev->device, port))
316 		return -EINVAL;
317 
318 	return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
319 }
320 
321 int cma_set_default_roce_tos(struct cma_device *cma_dev, u32 port,
322 			     u8 default_roce_tos)
323 {
324 	if (!rdma_is_port_valid(cma_dev->device, port))
325 		return -EINVAL;
326 
327 	cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
328 		 default_roce_tos;
329 
330 	return 0;
331 }
332 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
333 {
334 	return cma_dev->device;
335 }
336 
337 /*
338  * Device removal can occur at anytime, so we need extra handling to
339  * serialize notifying the user of device removal with other callbacks.
340  * We do this by disabling removal notification while a callback is in process,
341  * and reporting it after the callback completes.
342  */
343 
344 struct cma_multicast {
345 	struct rdma_id_private *id_priv;
346 	union {
347 		struct ib_sa_multicast *sa_mc;
348 		struct {
349 			struct work_struct work;
350 			struct rdma_cm_event event;
351 		} iboe_join;
352 	};
353 	struct list_head	list;
354 	void			*context;
355 	struct sockaddr_storage	addr;
356 	u8			join_state;
357 };
358 
359 struct cma_work {
360 	struct work_struct	work;
361 	struct rdma_id_private	*id;
362 	enum rdma_cm_state	old_state;
363 	enum rdma_cm_state	new_state;
364 	struct rdma_cm_event	event;
365 };
366 
367 union cma_ip_addr {
368 	struct in6_addr ip6;
369 	struct {
370 		__be32 pad[3];
371 		__be32 addr;
372 	} ip4;
373 };
374 
375 struct cma_hdr {
376 	u8 cma_version;
377 	u8 ip_version;	/* IP version: 7:4 */
378 	__be16 port;
379 	union cma_ip_addr src_addr;
380 	union cma_ip_addr dst_addr;
381 };
382 
383 #define CMA_VERSION 0x00
384 
385 struct cma_req_info {
386 	struct sockaddr_storage listen_addr_storage;
387 	struct sockaddr_storage src_addr_storage;
388 	struct ib_device *device;
389 	union ib_gid local_gid;
390 	__be64 service_id;
391 	int port;
392 	bool has_gid;
393 	u16 pkey;
394 };
395 
396 static int cma_comp_exch(struct rdma_id_private *id_priv,
397 			 enum rdma_cm_state comp, enum rdma_cm_state exch)
398 {
399 	unsigned long flags;
400 	int ret;
401 
402 	/*
403 	 * The FSM uses a funny double locking where state is protected by both
404 	 * the handler_mutex and the spinlock. State is not allowed to change
405 	 * to/from a handler_mutex protected value without also holding
406 	 * handler_mutex.
407 	 */
408 	if (comp == RDMA_CM_CONNECT || exch == RDMA_CM_CONNECT)
409 		lockdep_assert_held(&id_priv->handler_mutex);
410 
411 	spin_lock_irqsave(&id_priv->lock, flags);
412 	if ((ret = (id_priv->state == comp)))
413 		id_priv->state = exch;
414 	spin_unlock_irqrestore(&id_priv->lock, flags);
415 	return ret;
416 }
417 
418 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
419 {
420 	return hdr->ip_version >> 4;
421 }
422 
423 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
424 {
425 	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
426 }
427 
428 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
429 {
430 	struct in_device *in_dev = NULL;
431 
432 	if (ndev) {
433 		rtnl_lock();
434 		in_dev = __in_dev_get_rtnl(ndev);
435 		if (in_dev) {
436 			if (join)
437 				ip_mc_inc_group(in_dev,
438 						*(__be32 *)(mgid->raw + 12));
439 			else
440 				ip_mc_dec_group(in_dev,
441 						*(__be32 *)(mgid->raw + 12));
442 		}
443 		rtnl_unlock();
444 	}
445 	return (in_dev) ? 0 : -ENODEV;
446 }
447 
448 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
449 			       struct cma_device *cma_dev)
450 {
451 	cma_dev_get(cma_dev);
452 	id_priv->cma_dev = cma_dev;
453 	id_priv->id.device = cma_dev->device;
454 	id_priv->id.route.addr.dev_addr.transport =
455 		rdma_node_get_transport(cma_dev->device->node_type);
456 	list_add_tail(&id_priv->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) {
1850 			union ib_gid mgid;
1851 
1852 			cma_set_mgid(id_priv, (struct sockaddr *)&mc->addr,
1853 				     &mgid);
1854 
1855 			if (!send_only)
1856 				cma_igmp_send(ndev, &mgid, false);
1857 
1858 			dev_put(ndev);
1859 		}
1860 
1861 		cancel_work_sync(&mc->iboe_join.work);
1862 	}
1863 	kfree(mc);
1864 }
1865 
1866 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1867 {
1868 	struct cma_multicast *mc;
1869 
1870 	while (!list_empty(&id_priv->mc_list)) {
1871 		mc = list_first_entry(&id_priv->mc_list, struct cma_multicast,
1872 				      list);
1873 		list_del(&mc->list);
1874 		destroy_mc(id_priv, mc);
1875 	}
1876 }
1877 
1878 static void _destroy_id(struct rdma_id_private *id_priv,
1879 			enum rdma_cm_state state)
1880 {
1881 	cma_cancel_operation(id_priv, state);
1882 
1883 	rdma_restrack_del(&id_priv->res);
1884 	if (id_priv->cma_dev) {
1885 		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1886 			if (id_priv->cm_id.ib)
1887 				ib_destroy_cm_id(id_priv->cm_id.ib);
1888 		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1889 			if (id_priv->cm_id.iw)
1890 				iw_destroy_cm_id(id_priv->cm_id.iw);
1891 		}
1892 		cma_leave_mc_groups(id_priv);
1893 		cma_release_dev(id_priv);
1894 	}
1895 
1896 	cma_release_port(id_priv);
1897 	cma_id_put(id_priv);
1898 	wait_for_completion(&id_priv->comp);
1899 
1900 	if (id_priv->internal_id)
1901 		cma_id_put(id_priv->id.context);
1902 
1903 	kfree(id_priv->id.route.path_rec);
1904 
1905 	put_net(id_priv->id.route.addr.dev_addr.net);
1906 	kfree(id_priv);
1907 }
1908 
1909 /*
1910  * destroy an ID from within the handler_mutex. This ensures that no other
1911  * handlers can start running concurrently.
1912  */
1913 static void destroy_id_handler_unlock(struct rdma_id_private *id_priv)
1914 	__releases(&idprv->handler_mutex)
1915 {
1916 	enum rdma_cm_state state;
1917 	unsigned long flags;
1918 
1919 	trace_cm_id_destroy(id_priv);
1920 
1921 	/*
1922 	 * Setting the state to destroyed under the handler mutex provides a
1923 	 * fence against calling handler callbacks. If this is invoked due to
1924 	 * the failure of a handler callback then it guarentees that no future
1925 	 * handlers will be called.
1926 	 */
1927 	lockdep_assert_held(&id_priv->handler_mutex);
1928 	spin_lock_irqsave(&id_priv->lock, flags);
1929 	state = id_priv->state;
1930 	id_priv->state = RDMA_CM_DESTROYING;
1931 	spin_unlock_irqrestore(&id_priv->lock, flags);
1932 	mutex_unlock(&id_priv->handler_mutex);
1933 	_destroy_id(id_priv, state);
1934 }
1935 
1936 void rdma_destroy_id(struct rdma_cm_id *id)
1937 {
1938 	struct rdma_id_private *id_priv =
1939 		container_of(id, struct rdma_id_private, id);
1940 
1941 	mutex_lock(&id_priv->handler_mutex);
1942 	destroy_id_handler_unlock(id_priv);
1943 }
1944 EXPORT_SYMBOL(rdma_destroy_id);
1945 
1946 static int cma_rep_recv(struct rdma_id_private *id_priv)
1947 {
1948 	int ret;
1949 
1950 	ret = cma_modify_qp_rtr(id_priv, NULL);
1951 	if (ret)
1952 		goto reject;
1953 
1954 	ret = cma_modify_qp_rts(id_priv, NULL);
1955 	if (ret)
1956 		goto reject;
1957 
1958 	trace_cm_send_rtu(id_priv);
1959 	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1960 	if (ret)
1961 		goto reject;
1962 
1963 	return 0;
1964 reject:
1965 	pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
1966 	cma_modify_qp_err(id_priv);
1967 	trace_cm_send_rej(id_priv);
1968 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1969 		       NULL, 0, NULL, 0);
1970 	return ret;
1971 }
1972 
1973 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1974 				   const struct ib_cm_rep_event_param *rep_data,
1975 				   void *private_data)
1976 {
1977 	event->param.conn.private_data = private_data;
1978 	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1979 	event->param.conn.responder_resources = rep_data->responder_resources;
1980 	event->param.conn.initiator_depth = rep_data->initiator_depth;
1981 	event->param.conn.flow_control = rep_data->flow_control;
1982 	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1983 	event->param.conn.srq = rep_data->srq;
1984 	event->param.conn.qp_num = rep_data->remote_qpn;
1985 
1986 	event->ece.vendor_id = rep_data->ece.vendor_id;
1987 	event->ece.attr_mod = rep_data->ece.attr_mod;
1988 }
1989 
1990 static int cma_cm_event_handler(struct rdma_id_private *id_priv,
1991 				struct rdma_cm_event *event)
1992 {
1993 	int ret;
1994 
1995 	lockdep_assert_held(&id_priv->handler_mutex);
1996 
1997 	trace_cm_event_handler(id_priv, event);
1998 	ret = id_priv->id.event_handler(&id_priv->id, event);
1999 	trace_cm_event_done(id_priv, event, ret);
2000 	return ret;
2001 }
2002 
2003 static int cma_ib_handler(struct ib_cm_id *cm_id,
2004 			  const struct ib_cm_event *ib_event)
2005 {
2006 	struct rdma_id_private *id_priv = cm_id->context;
2007 	struct rdma_cm_event event = {};
2008 	enum rdma_cm_state state;
2009 	int ret;
2010 
2011 	mutex_lock(&id_priv->handler_mutex);
2012 	state = READ_ONCE(id_priv->state);
2013 	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
2014 	     state != RDMA_CM_CONNECT) ||
2015 	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
2016 	     state != RDMA_CM_DISCONNECT))
2017 		goto out;
2018 
2019 	switch (ib_event->event) {
2020 	case IB_CM_REQ_ERROR:
2021 	case IB_CM_REP_ERROR:
2022 		event.event = RDMA_CM_EVENT_UNREACHABLE;
2023 		event.status = -ETIMEDOUT;
2024 		break;
2025 	case IB_CM_REP_RECEIVED:
2026 		if (state == RDMA_CM_CONNECT &&
2027 		    (id_priv->id.qp_type != IB_QPT_UD)) {
2028 			trace_cm_send_mra(id_priv);
2029 			ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2030 		}
2031 		if (id_priv->id.qp) {
2032 			event.status = cma_rep_recv(id_priv);
2033 			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
2034 						     RDMA_CM_EVENT_ESTABLISHED;
2035 		} else {
2036 			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
2037 		}
2038 		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
2039 				       ib_event->private_data);
2040 		break;
2041 	case IB_CM_RTU_RECEIVED:
2042 	case IB_CM_USER_ESTABLISHED:
2043 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2044 		break;
2045 	case IB_CM_DREQ_ERROR:
2046 		event.status = -ETIMEDOUT;
2047 		fallthrough;
2048 	case IB_CM_DREQ_RECEIVED:
2049 	case IB_CM_DREP_RECEIVED:
2050 		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
2051 				   RDMA_CM_DISCONNECT))
2052 			goto out;
2053 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2054 		break;
2055 	case IB_CM_TIMEWAIT_EXIT:
2056 		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
2057 		break;
2058 	case IB_CM_MRA_RECEIVED:
2059 		/* ignore event */
2060 		goto out;
2061 	case IB_CM_REJ_RECEIVED:
2062 		pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
2063 										ib_event->param.rej_rcvd.reason));
2064 		cma_modify_qp_err(id_priv);
2065 		event.status = ib_event->param.rej_rcvd.reason;
2066 		event.event = RDMA_CM_EVENT_REJECTED;
2067 		event.param.conn.private_data = ib_event->private_data;
2068 		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
2069 		break;
2070 	default:
2071 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2072 		       ib_event->event);
2073 		goto out;
2074 	}
2075 
2076 	ret = cma_cm_event_handler(id_priv, &event);
2077 	if (ret) {
2078 		/* Destroy the CM ID by returning a non-zero value. */
2079 		id_priv->cm_id.ib = NULL;
2080 		destroy_id_handler_unlock(id_priv);
2081 		return ret;
2082 	}
2083 out:
2084 	mutex_unlock(&id_priv->handler_mutex);
2085 	return 0;
2086 }
2087 
2088 static struct rdma_id_private *
2089 cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2090 		   const struct ib_cm_event *ib_event,
2091 		   struct net_device *net_dev)
2092 {
2093 	struct rdma_id_private *listen_id_priv;
2094 	struct rdma_id_private *id_priv;
2095 	struct rdma_cm_id *id;
2096 	struct rdma_route *rt;
2097 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2098 	struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2099 	const __be64 service_id =
2100 		ib_event->param.req_rcvd.primary_path->service_id;
2101 	int ret;
2102 
2103 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2104 	id_priv = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2105 				   listen_id->event_handler, listen_id->context,
2106 				   listen_id->ps,
2107 				   ib_event->param.req_rcvd.qp_type,
2108 				   listen_id_priv);
2109 	if (IS_ERR(id_priv))
2110 		return NULL;
2111 
2112 	id = &id_priv->id;
2113 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2114 			      (struct sockaddr *)&id->route.addr.dst_addr,
2115 			      listen_id, ib_event, ss_family, service_id))
2116 		goto err;
2117 
2118 	rt = &id->route;
2119 	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
2120 	rt->path_rec = kmalloc_array(rt->num_paths, sizeof(*rt->path_rec),
2121 				     GFP_KERNEL);
2122 	if (!rt->path_rec)
2123 		goto err;
2124 
2125 	rt->path_rec[0] = *path;
2126 	if (rt->num_paths == 2)
2127 		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2128 
2129 	if (net_dev) {
2130 		rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
2131 	} else {
2132 		if (!cma_protocol_roce(listen_id) &&
2133 		    cma_any_addr(cma_src_addr(id_priv))) {
2134 			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2135 			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2136 			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2137 		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
2138 			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2139 			if (ret)
2140 				goto err;
2141 		}
2142 	}
2143 	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2144 
2145 	id_priv->state = RDMA_CM_CONNECT;
2146 	return id_priv;
2147 
2148 err:
2149 	rdma_destroy_id(id);
2150 	return NULL;
2151 }
2152 
2153 static struct rdma_id_private *
2154 cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2155 		  const struct ib_cm_event *ib_event,
2156 		  struct net_device *net_dev)
2157 {
2158 	const struct rdma_id_private *listen_id_priv;
2159 	struct rdma_id_private *id_priv;
2160 	struct rdma_cm_id *id;
2161 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2162 	struct net *net = listen_id->route.addr.dev_addr.net;
2163 	int ret;
2164 
2165 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2166 	id_priv = __rdma_create_id(net, listen_id->event_handler,
2167 				   listen_id->context, listen_id->ps, IB_QPT_UD,
2168 				   listen_id_priv);
2169 	if (IS_ERR(id_priv))
2170 		return NULL;
2171 
2172 	id = &id_priv->id;
2173 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2174 			      (struct sockaddr *)&id->route.addr.dst_addr,
2175 			      listen_id, ib_event, ss_family,
2176 			      ib_event->param.sidr_req_rcvd.service_id))
2177 		goto err;
2178 
2179 	if (net_dev) {
2180 		rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
2181 	} else {
2182 		if (!cma_any_addr(cma_src_addr(id_priv))) {
2183 			ret = cma_translate_addr(cma_src_addr(id_priv),
2184 						 &id->route.addr.dev_addr);
2185 			if (ret)
2186 				goto err;
2187 		}
2188 	}
2189 
2190 	id_priv->state = RDMA_CM_CONNECT;
2191 	return id_priv;
2192 err:
2193 	rdma_destroy_id(id);
2194 	return NULL;
2195 }
2196 
2197 static void cma_set_req_event_data(struct rdma_cm_event *event,
2198 				   const struct ib_cm_req_event_param *req_data,
2199 				   void *private_data, int offset)
2200 {
2201 	event->param.conn.private_data = private_data + offset;
2202 	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2203 	event->param.conn.responder_resources = req_data->responder_resources;
2204 	event->param.conn.initiator_depth = req_data->initiator_depth;
2205 	event->param.conn.flow_control = req_data->flow_control;
2206 	event->param.conn.retry_count = req_data->retry_count;
2207 	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2208 	event->param.conn.srq = req_data->srq;
2209 	event->param.conn.qp_num = req_data->remote_qpn;
2210 
2211 	event->ece.vendor_id = req_data->ece.vendor_id;
2212 	event->ece.attr_mod = req_data->ece.attr_mod;
2213 }
2214 
2215 static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2216 				    const struct ib_cm_event *ib_event)
2217 {
2218 	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
2219 		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
2220 		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
2221 		 (id->qp_type == IB_QPT_UD)) ||
2222 		(!id->qp_type));
2223 }
2224 
2225 static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2226 			      const struct ib_cm_event *ib_event)
2227 {
2228 	struct rdma_id_private *listen_id, *conn_id = NULL;
2229 	struct rdma_cm_event event = {};
2230 	struct cma_req_info req = {};
2231 	struct net_device *net_dev;
2232 	u8 offset;
2233 	int ret;
2234 
2235 	listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
2236 	if (IS_ERR(listen_id))
2237 		return PTR_ERR(listen_id);
2238 
2239 	trace_cm_req_handler(listen_id, ib_event->event);
2240 	if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
2241 		ret = -EINVAL;
2242 		goto net_dev_put;
2243 	}
2244 
2245 	mutex_lock(&listen_id->handler_mutex);
2246 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN) {
2247 		ret = -ECONNABORTED;
2248 		goto err_unlock;
2249 	}
2250 
2251 	offset = cma_user_data_offset(listen_id);
2252 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2253 	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2254 		conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2255 		event.param.ud.private_data = ib_event->private_data + offset;
2256 		event.param.ud.private_data_len =
2257 				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2258 	} else {
2259 		conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2260 		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2261 				       ib_event->private_data, offset);
2262 	}
2263 	if (!conn_id) {
2264 		ret = -ENOMEM;
2265 		goto err_unlock;
2266 	}
2267 
2268 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2269 	ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2270 	if (ret) {
2271 		destroy_id_handler_unlock(conn_id);
2272 		goto err_unlock;
2273 	}
2274 
2275 	conn_id->cm_id.ib = cm_id;
2276 	cm_id->context = conn_id;
2277 	cm_id->cm_handler = cma_ib_handler;
2278 
2279 	ret = cma_cm_event_handler(conn_id, &event);
2280 	if (ret) {
2281 		/* Destroy the CM ID by returning a non-zero value. */
2282 		conn_id->cm_id.ib = NULL;
2283 		mutex_unlock(&listen_id->handler_mutex);
2284 		destroy_id_handler_unlock(conn_id);
2285 		goto net_dev_put;
2286 	}
2287 
2288 	if (READ_ONCE(conn_id->state) == RDMA_CM_CONNECT &&
2289 	    conn_id->id.qp_type != IB_QPT_UD) {
2290 		trace_cm_send_mra(cm_id->context);
2291 		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2292 	}
2293 	mutex_unlock(&conn_id->handler_mutex);
2294 
2295 err_unlock:
2296 	mutex_unlock(&listen_id->handler_mutex);
2297 
2298 net_dev_put:
2299 	if (net_dev)
2300 		dev_put(net_dev);
2301 
2302 	return ret;
2303 }
2304 
2305 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2306 {
2307 	if (addr->sa_family == AF_IB)
2308 		return ((struct sockaddr_ib *) addr)->sib_sid;
2309 
2310 	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2311 }
2312 EXPORT_SYMBOL(rdma_get_service_id);
2313 
2314 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2315 		    union ib_gid *dgid)
2316 {
2317 	struct rdma_addr *addr = &cm_id->route.addr;
2318 
2319 	if (!cm_id->device) {
2320 		if (sgid)
2321 			memset(sgid, 0, sizeof(*sgid));
2322 		if (dgid)
2323 			memset(dgid, 0, sizeof(*dgid));
2324 		return;
2325 	}
2326 
2327 	if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2328 		if (sgid)
2329 			rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2330 		if (dgid)
2331 			rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2332 	} else {
2333 		if (sgid)
2334 			rdma_addr_get_sgid(&addr->dev_addr, sgid);
2335 		if (dgid)
2336 			rdma_addr_get_dgid(&addr->dev_addr, dgid);
2337 	}
2338 }
2339 EXPORT_SYMBOL(rdma_read_gids);
2340 
2341 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2342 {
2343 	struct rdma_id_private *id_priv = iw_id->context;
2344 	struct rdma_cm_event event = {};
2345 	int ret = 0;
2346 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2347 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2348 
2349 	mutex_lock(&id_priv->handler_mutex);
2350 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
2351 		goto out;
2352 
2353 	switch (iw_event->event) {
2354 	case IW_CM_EVENT_CLOSE:
2355 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2356 		break;
2357 	case IW_CM_EVENT_CONNECT_REPLY:
2358 		memcpy(cma_src_addr(id_priv), laddr,
2359 		       rdma_addr_size(laddr));
2360 		memcpy(cma_dst_addr(id_priv), raddr,
2361 		       rdma_addr_size(raddr));
2362 		switch (iw_event->status) {
2363 		case 0:
2364 			event.event = RDMA_CM_EVENT_ESTABLISHED;
2365 			event.param.conn.initiator_depth = iw_event->ird;
2366 			event.param.conn.responder_resources = iw_event->ord;
2367 			break;
2368 		case -ECONNRESET:
2369 		case -ECONNREFUSED:
2370 			event.event = RDMA_CM_EVENT_REJECTED;
2371 			break;
2372 		case -ETIMEDOUT:
2373 			event.event = RDMA_CM_EVENT_UNREACHABLE;
2374 			break;
2375 		default:
2376 			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2377 			break;
2378 		}
2379 		break;
2380 	case IW_CM_EVENT_ESTABLISHED:
2381 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2382 		event.param.conn.initiator_depth = iw_event->ird;
2383 		event.param.conn.responder_resources = iw_event->ord;
2384 		break;
2385 	default:
2386 		goto out;
2387 	}
2388 
2389 	event.status = iw_event->status;
2390 	event.param.conn.private_data = iw_event->private_data;
2391 	event.param.conn.private_data_len = iw_event->private_data_len;
2392 	ret = cma_cm_event_handler(id_priv, &event);
2393 	if (ret) {
2394 		/* Destroy the CM ID by returning a non-zero value. */
2395 		id_priv->cm_id.iw = NULL;
2396 		destroy_id_handler_unlock(id_priv);
2397 		return ret;
2398 	}
2399 
2400 out:
2401 	mutex_unlock(&id_priv->handler_mutex);
2402 	return ret;
2403 }
2404 
2405 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2406 			       struct iw_cm_event *iw_event)
2407 {
2408 	struct rdma_id_private *listen_id, *conn_id;
2409 	struct rdma_cm_event event = {};
2410 	int ret = -ECONNABORTED;
2411 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2412 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2413 
2414 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2415 	event.param.conn.private_data = iw_event->private_data;
2416 	event.param.conn.private_data_len = iw_event->private_data_len;
2417 	event.param.conn.initiator_depth = iw_event->ird;
2418 	event.param.conn.responder_resources = iw_event->ord;
2419 
2420 	listen_id = cm_id->context;
2421 
2422 	mutex_lock(&listen_id->handler_mutex);
2423 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN)
2424 		goto out;
2425 
2426 	/* Create a new RDMA id for the new IW CM ID */
2427 	conn_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2428 				   listen_id->id.event_handler,
2429 				   listen_id->id.context, RDMA_PS_TCP,
2430 				   IB_QPT_RC, listen_id);
2431 	if (IS_ERR(conn_id)) {
2432 		ret = -ENOMEM;
2433 		goto out;
2434 	}
2435 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2436 	conn_id->state = RDMA_CM_CONNECT;
2437 
2438 	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2439 	if (ret) {
2440 		mutex_unlock(&listen_id->handler_mutex);
2441 		destroy_id_handler_unlock(conn_id);
2442 		return ret;
2443 	}
2444 
2445 	ret = cma_iw_acquire_dev(conn_id, listen_id);
2446 	if (ret) {
2447 		mutex_unlock(&listen_id->handler_mutex);
2448 		destroy_id_handler_unlock(conn_id);
2449 		return ret;
2450 	}
2451 
2452 	conn_id->cm_id.iw = cm_id;
2453 	cm_id->context = conn_id;
2454 	cm_id->cm_handler = cma_iw_handler;
2455 
2456 	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2457 	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2458 
2459 	ret = cma_cm_event_handler(conn_id, &event);
2460 	if (ret) {
2461 		/* User wants to destroy the CM ID */
2462 		conn_id->cm_id.iw = NULL;
2463 		mutex_unlock(&listen_id->handler_mutex);
2464 		destroy_id_handler_unlock(conn_id);
2465 		return ret;
2466 	}
2467 
2468 	mutex_unlock(&conn_id->handler_mutex);
2469 
2470 out:
2471 	mutex_unlock(&listen_id->handler_mutex);
2472 	return ret;
2473 }
2474 
2475 static int cma_ib_listen(struct rdma_id_private *id_priv)
2476 {
2477 	struct sockaddr *addr;
2478 	struct ib_cm_id	*id;
2479 	__be64 svc_id;
2480 
2481 	addr = cma_src_addr(id_priv);
2482 	svc_id = rdma_get_service_id(&id_priv->id, addr);
2483 	id = ib_cm_insert_listen(id_priv->id.device,
2484 				 cma_ib_req_handler, svc_id);
2485 	if (IS_ERR(id))
2486 		return PTR_ERR(id);
2487 	id_priv->cm_id.ib = id;
2488 
2489 	return 0;
2490 }
2491 
2492 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2493 {
2494 	int ret;
2495 	struct iw_cm_id	*id;
2496 
2497 	id = iw_create_cm_id(id_priv->id.device,
2498 			     iw_conn_req_handler,
2499 			     id_priv);
2500 	if (IS_ERR(id))
2501 		return PTR_ERR(id);
2502 
2503 	mutex_lock(&id_priv->qp_mutex);
2504 	id->tos = id_priv->tos;
2505 	id->tos_set = id_priv->tos_set;
2506 	mutex_unlock(&id_priv->qp_mutex);
2507 	id->afonly = id_priv->afonly;
2508 	id_priv->cm_id.iw = id;
2509 
2510 	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2511 	       rdma_addr_size(cma_src_addr(id_priv)));
2512 
2513 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2514 
2515 	if (ret) {
2516 		iw_destroy_cm_id(id_priv->cm_id.iw);
2517 		id_priv->cm_id.iw = NULL;
2518 	}
2519 
2520 	return ret;
2521 }
2522 
2523 static int cma_listen_handler(struct rdma_cm_id *id,
2524 			      struct rdma_cm_event *event)
2525 {
2526 	struct rdma_id_private *id_priv = id->context;
2527 
2528 	/* Listening IDs are always destroyed on removal */
2529 	if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2530 		return -1;
2531 
2532 	id->context = id_priv->id.context;
2533 	id->event_handler = id_priv->id.event_handler;
2534 	trace_cm_event_handler(id_priv, event);
2535 	return id_priv->id.event_handler(id, event);
2536 }
2537 
2538 static int cma_listen_on_dev(struct rdma_id_private *id_priv,
2539 			     struct cma_device *cma_dev,
2540 			     struct rdma_id_private **to_destroy)
2541 {
2542 	struct rdma_id_private *dev_id_priv;
2543 	struct net *net = id_priv->id.route.addr.dev_addr.net;
2544 	int ret;
2545 
2546 	lockdep_assert_held(&lock);
2547 
2548 	*to_destroy = NULL;
2549 	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2550 		return 0;
2551 
2552 	dev_id_priv =
2553 		__rdma_create_id(net, cma_listen_handler, id_priv,
2554 				 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2555 	if (IS_ERR(dev_id_priv))
2556 		return PTR_ERR(dev_id_priv);
2557 
2558 	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2559 	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2560 	       rdma_addr_size(cma_src_addr(id_priv)));
2561 
2562 	_cma_attach_to_dev(dev_id_priv, cma_dev);
2563 	rdma_restrack_add(&dev_id_priv->res);
2564 	cma_id_get(id_priv);
2565 	dev_id_priv->internal_id = 1;
2566 	dev_id_priv->afonly = id_priv->afonly;
2567 	mutex_lock(&id_priv->qp_mutex);
2568 	dev_id_priv->tos_set = id_priv->tos_set;
2569 	dev_id_priv->tos = id_priv->tos;
2570 	mutex_unlock(&id_priv->qp_mutex);
2571 
2572 	ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2573 	if (ret)
2574 		goto err_listen;
2575 	list_add_tail(&dev_id_priv->listen_item, &id_priv->listen_list);
2576 	return 0;
2577 err_listen:
2578 	/* Caller must destroy this after releasing lock */
2579 	*to_destroy = dev_id_priv;
2580 	dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret);
2581 	return ret;
2582 }
2583 
2584 static int cma_listen_on_all(struct rdma_id_private *id_priv)
2585 {
2586 	struct rdma_id_private *to_destroy;
2587 	struct cma_device *cma_dev;
2588 	int ret;
2589 
2590 	mutex_lock(&lock);
2591 	list_add_tail(&id_priv->listen_any_item, &listen_any_list);
2592 	list_for_each_entry(cma_dev, &dev_list, list) {
2593 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
2594 		if (ret) {
2595 			/* Prevent racing with cma_process_remove() */
2596 			if (to_destroy)
2597 				list_del_init(&to_destroy->device_item);
2598 			goto err_listen;
2599 		}
2600 	}
2601 	mutex_unlock(&lock);
2602 	return 0;
2603 
2604 err_listen:
2605 	_cma_cancel_listens(id_priv);
2606 	mutex_unlock(&lock);
2607 	if (to_destroy)
2608 		rdma_destroy_id(&to_destroy->id);
2609 	return ret;
2610 }
2611 
2612 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2613 {
2614 	struct rdma_id_private *id_priv;
2615 
2616 	id_priv = container_of(id, struct rdma_id_private, id);
2617 	mutex_lock(&id_priv->qp_mutex);
2618 	id_priv->tos = (u8) tos;
2619 	id_priv->tos_set = true;
2620 	mutex_unlock(&id_priv->qp_mutex);
2621 }
2622 EXPORT_SYMBOL(rdma_set_service_type);
2623 
2624 /**
2625  * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2626  *                          with a connection identifier.
2627  * @id: Communication identifier to associated with service type.
2628  * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2629  *
2630  * This function should be called before rdma_connect() on active side,
2631  * and on passive side before rdma_accept(). It is applicable to primary
2632  * path only. The timeout will affect the local side of the QP, it is not
2633  * negotiated with remote side and zero disables the timer. In case it is
2634  * set before rdma_resolve_route, the value will also be used to determine
2635  * PacketLifeTime for RoCE.
2636  *
2637  * Return: 0 for success
2638  */
2639 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2640 {
2641 	struct rdma_id_private *id_priv;
2642 
2643 	if (id->qp_type != IB_QPT_RC)
2644 		return -EINVAL;
2645 
2646 	id_priv = container_of(id, struct rdma_id_private, id);
2647 	mutex_lock(&id_priv->qp_mutex);
2648 	id_priv->timeout = timeout;
2649 	id_priv->timeout_set = true;
2650 	mutex_unlock(&id_priv->qp_mutex);
2651 
2652 	return 0;
2653 }
2654 EXPORT_SYMBOL(rdma_set_ack_timeout);
2655 
2656 /**
2657  * rdma_set_min_rnr_timer() - Set the minimum RNR Retry timer of the
2658  *			      QP associated with a connection identifier.
2659  * @id: Communication identifier to associated with service type.
2660  * @min_rnr_timer: 5-bit value encoded as Table 45: "Encoding for RNR NAK
2661  *		   Timer Field" in the IBTA specification.
2662  *
2663  * This function should be called before rdma_connect() on active
2664  * side, and on passive side before rdma_accept(). The timer value
2665  * will be associated with the local QP. When it receives a send it is
2666  * not read to handle, typically if the receive queue is empty, an RNR
2667  * Retry NAK is returned to the requester with the min_rnr_timer
2668  * encoded. The requester will then wait at least the time specified
2669  * in the NAK before retrying. The default is zero, which translates
2670  * to a minimum RNR Timer value of 655 ms.
2671  *
2672  * Return: 0 for success
2673  */
2674 int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer)
2675 {
2676 	struct rdma_id_private *id_priv;
2677 
2678 	/* It is a five-bit value */
2679 	if (min_rnr_timer & 0xe0)
2680 		return -EINVAL;
2681 
2682 	if (WARN_ON(id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_TGT))
2683 		return -EINVAL;
2684 
2685 	id_priv = container_of(id, struct rdma_id_private, id);
2686 	mutex_lock(&id_priv->qp_mutex);
2687 	id_priv->min_rnr_timer = min_rnr_timer;
2688 	id_priv->min_rnr_timer_set = true;
2689 	mutex_unlock(&id_priv->qp_mutex);
2690 
2691 	return 0;
2692 }
2693 EXPORT_SYMBOL(rdma_set_min_rnr_timer);
2694 
2695 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2696 			      void *context)
2697 {
2698 	struct cma_work *work = context;
2699 	struct rdma_route *route;
2700 
2701 	route = &work->id->id.route;
2702 
2703 	if (!status) {
2704 		route->num_paths = 1;
2705 		*route->path_rec = *path_rec;
2706 	} else {
2707 		work->old_state = RDMA_CM_ROUTE_QUERY;
2708 		work->new_state = RDMA_CM_ADDR_RESOLVED;
2709 		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2710 		work->event.status = status;
2711 		pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2712 				     status);
2713 	}
2714 
2715 	queue_work(cma_wq, &work->work);
2716 }
2717 
2718 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2719 			      unsigned long timeout_ms, struct cma_work *work)
2720 {
2721 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2722 	struct sa_path_rec path_rec;
2723 	ib_sa_comp_mask comp_mask;
2724 	struct sockaddr_in6 *sin6;
2725 	struct sockaddr_ib *sib;
2726 
2727 	memset(&path_rec, 0, sizeof path_rec);
2728 
2729 	if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2730 		path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2731 	else
2732 		path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2733 	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2734 	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2735 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2736 	path_rec.numb_path = 1;
2737 	path_rec.reversible = 1;
2738 	path_rec.service_id = rdma_get_service_id(&id_priv->id,
2739 						  cma_dst_addr(id_priv));
2740 
2741 	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2742 		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2743 		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2744 
2745 	switch (cma_family(id_priv)) {
2746 	case AF_INET:
2747 		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2748 		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2749 		break;
2750 	case AF_INET6:
2751 		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2752 		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2753 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2754 		break;
2755 	case AF_IB:
2756 		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2757 		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2758 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2759 		break;
2760 	}
2761 
2762 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2763 					       id_priv->id.port_num, &path_rec,
2764 					       comp_mask, timeout_ms,
2765 					       GFP_KERNEL, cma_query_handler,
2766 					       work, &id_priv->query);
2767 
2768 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2769 }
2770 
2771 static void cma_iboe_join_work_handler(struct work_struct *work)
2772 {
2773 	struct cma_multicast *mc =
2774 		container_of(work, struct cma_multicast, iboe_join.work);
2775 	struct rdma_cm_event *event = &mc->iboe_join.event;
2776 	struct rdma_id_private *id_priv = mc->id_priv;
2777 	int ret;
2778 
2779 	mutex_lock(&id_priv->handler_mutex);
2780 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2781 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2782 		goto out_unlock;
2783 
2784 	ret = cma_cm_event_handler(id_priv, event);
2785 	WARN_ON(ret);
2786 
2787 out_unlock:
2788 	mutex_unlock(&id_priv->handler_mutex);
2789 	if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2790 		rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2791 }
2792 
2793 static void cma_work_handler(struct work_struct *_work)
2794 {
2795 	struct cma_work *work = container_of(_work, struct cma_work, work);
2796 	struct rdma_id_private *id_priv = work->id;
2797 
2798 	mutex_lock(&id_priv->handler_mutex);
2799 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2800 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2801 		goto out_unlock;
2802 	if (work->old_state != 0 || work->new_state != 0) {
2803 		if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2804 			goto out_unlock;
2805 	}
2806 
2807 	if (cma_cm_event_handler(id_priv, &work->event)) {
2808 		cma_id_put(id_priv);
2809 		destroy_id_handler_unlock(id_priv);
2810 		goto out_free;
2811 	}
2812 
2813 out_unlock:
2814 	mutex_unlock(&id_priv->handler_mutex);
2815 	cma_id_put(id_priv);
2816 out_free:
2817 	if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
2818 		rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
2819 	kfree(work);
2820 }
2821 
2822 static void cma_init_resolve_route_work(struct cma_work *work,
2823 					struct rdma_id_private *id_priv)
2824 {
2825 	work->id = id_priv;
2826 	INIT_WORK(&work->work, cma_work_handler);
2827 	work->old_state = RDMA_CM_ROUTE_QUERY;
2828 	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2829 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2830 }
2831 
2832 static void enqueue_resolve_addr_work(struct cma_work *work,
2833 				      struct rdma_id_private *id_priv)
2834 {
2835 	/* Balances with cma_id_put() in cma_work_handler */
2836 	cma_id_get(id_priv);
2837 
2838 	work->id = id_priv;
2839 	INIT_WORK(&work->work, cma_work_handler);
2840 	work->old_state = RDMA_CM_ADDR_QUERY;
2841 	work->new_state = RDMA_CM_ADDR_RESOLVED;
2842 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2843 
2844 	queue_work(cma_wq, &work->work);
2845 }
2846 
2847 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
2848 				unsigned long timeout_ms)
2849 {
2850 	struct rdma_route *route = &id_priv->id.route;
2851 	struct cma_work *work;
2852 	int ret;
2853 
2854 	work = kzalloc(sizeof *work, GFP_KERNEL);
2855 	if (!work)
2856 		return -ENOMEM;
2857 
2858 	cma_init_resolve_route_work(work, id_priv);
2859 
2860 	if (!route->path_rec)
2861 		route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2862 	if (!route->path_rec) {
2863 		ret = -ENOMEM;
2864 		goto err1;
2865 	}
2866 
2867 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2868 	if (ret)
2869 		goto err2;
2870 
2871 	return 0;
2872 err2:
2873 	kfree(route->path_rec);
2874 	route->path_rec = NULL;
2875 err1:
2876 	kfree(work);
2877 	return ret;
2878 }
2879 
2880 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2881 					   unsigned long supported_gids,
2882 					   enum ib_gid_type default_gid)
2883 {
2884 	if ((network_type == RDMA_NETWORK_IPV4 ||
2885 	     network_type == RDMA_NETWORK_IPV6) &&
2886 	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2887 		return IB_GID_TYPE_ROCE_UDP_ENCAP;
2888 
2889 	return default_gid;
2890 }
2891 
2892 /*
2893  * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2894  * path record type based on GID type.
2895  * It also sets up other L2 fields which includes destination mac address
2896  * netdev ifindex, of the path record.
2897  * It returns the netdev of the bound interface for this path record entry.
2898  */
2899 static struct net_device *
2900 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
2901 {
2902 	struct rdma_route *route = &id_priv->id.route;
2903 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
2904 	struct rdma_addr *addr = &route->addr;
2905 	unsigned long supported_gids;
2906 	struct net_device *ndev;
2907 
2908 	if (!addr->dev_addr.bound_dev_if)
2909 		return NULL;
2910 
2911 	ndev = dev_get_by_index(addr->dev_addr.net,
2912 				addr->dev_addr.bound_dev_if);
2913 	if (!ndev)
2914 		return NULL;
2915 
2916 	supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2917 						    id_priv->id.port_num);
2918 	gid_type = cma_route_gid_type(addr->dev_addr.network,
2919 				      supported_gids,
2920 				      id_priv->gid_type);
2921 	/* Use the hint from IP Stack to select GID Type */
2922 	if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2923 		gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2924 	route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
2925 
2926 	route->path_rec->roce.route_resolved = true;
2927 	sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
2928 	return ndev;
2929 }
2930 
2931 int rdma_set_ib_path(struct rdma_cm_id *id,
2932 		     struct sa_path_rec *path_rec)
2933 {
2934 	struct rdma_id_private *id_priv;
2935 	struct net_device *ndev;
2936 	int ret;
2937 
2938 	id_priv = container_of(id, struct rdma_id_private, id);
2939 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2940 			   RDMA_CM_ROUTE_RESOLVED))
2941 		return -EINVAL;
2942 
2943 	id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
2944 				     GFP_KERNEL);
2945 	if (!id->route.path_rec) {
2946 		ret = -ENOMEM;
2947 		goto err;
2948 	}
2949 
2950 	if (rdma_protocol_roce(id->device, id->port_num)) {
2951 		ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
2952 		if (!ndev) {
2953 			ret = -ENODEV;
2954 			goto err_free;
2955 		}
2956 		dev_put(ndev);
2957 	}
2958 
2959 	id->route.num_paths = 1;
2960 	return 0;
2961 
2962 err_free:
2963 	kfree(id->route.path_rec);
2964 	id->route.path_rec = NULL;
2965 err:
2966 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2967 	return ret;
2968 }
2969 EXPORT_SYMBOL(rdma_set_ib_path);
2970 
2971 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
2972 {
2973 	struct cma_work *work;
2974 
2975 	work = kzalloc(sizeof *work, GFP_KERNEL);
2976 	if (!work)
2977 		return -ENOMEM;
2978 
2979 	cma_init_resolve_route_work(work, id_priv);
2980 	queue_work(cma_wq, &work->work);
2981 	return 0;
2982 }
2983 
2984 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
2985 {
2986 	struct net_device *dev;
2987 
2988 	dev = vlan_dev_real_dev(vlan_ndev);
2989 	if (dev->num_tc)
2990 		return netdev_get_prio_tc_map(dev, prio);
2991 
2992 	return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
2993 		VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2994 }
2995 
2996 struct iboe_prio_tc_map {
2997 	int input_prio;
2998 	int output_tc;
2999 	bool found;
3000 };
3001 
3002 static int get_lower_vlan_dev_tc(struct net_device *dev,
3003 				 struct netdev_nested_priv *priv)
3004 {
3005 	struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
3006 
3007 	if (is_vlan_dev(dev))
3008 		map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
3009 	else if (dev->num_tc)
3010 		map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
3011 	else
3012 		map->output_tc = 0;
3013 	/* We are interested only in first level VLAN device, so always
3014 	 * return 1 to stop iterating over next level devices.
3015 	 */
3016 	map->found = true;
3017 	return 1;
3018 }
3019 
3020 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
3021 {
3022 	struct iboe_prio_tc_map prio_tc_map = {};
3023 	int prio = rt_tos2priority(tos);
3024 	struct netdev_nested_priv priv;
3025 
3026 	/* If VLAN device, get it directly from the VLAN netdev */
3027 	if (is_vlan_dev(ndev))
3028 		return get_vlan_ndev_tc(ndev, prio);
3029 
3030 	prio_tc_map.input_prio = prio;
3031 	priv.data = (void *)&prio_tc_map;
3032 	rcu_read_lock();
3033 	netdev_walk_all_lower_dev_rcu(ndev,
3034 				      get_lower_vlan_dev_tc,
3035 				      &priv);
3036 	rcu_read_unlock();
3037 	/* If map is found from lower device, use it; Otherwise
3038 	 * continue with the current netdevice to get priority to tc map.
3039 	 */
3040 	if (prio_tc_map.found)
3041 		return prio_tc_map.output_tc;
3042 	else if (ndev->num_tc)
3043 		return netdev_get_prio_tc_map(ndev, prio);
3044 	else
3045 		return 0;
3046 }
3047 
3048 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
3049 {
3050 	struct sockaddr_in6 *addr6;
3051 	u16 dport, sport;
3052 	u32 hash, fl;
3053 
3054 	addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3055 	fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3056 	if ((cma_family(id_priv) != AF_INET6) || !fl) {
3057 		dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3058 		sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3059 		hash = (u32)sport * 31 + dport;
3060 		fl = hash & IB_GRH_FLOWLABEL_MASK;
3061 	}
3062 
3063 	return cpu_to_be32(fl);
3064 }
3065 
3066 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
3067 {
3068 	struct rdma_route *route = &id_priv->id.route;
3069 	struct rdma_addr *addr = &route->addr;
3070 	struct cma_work *work;
3071 	int ret;
3072 	struct net_device *ndev;
3073 
3074 	u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
3075 					rdma_start_port(id_priv->cma_dev->device)];
3076 	u8 tos;
3077 
3078 	mutex_lock(&id_priv->qp_mutex);
3079 	tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3080 	mutex_unlock(&id_priv->qp_mutex);
3081 
3082 	work = kzalloc(sizeof *work, GFP_KERNEL);
3083 	if (!work)
3084 		return -ENOMEM;
3085 
3086 	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
3087 	if (!route->path_rec) {
3088 		ret = -ENOMEM;
3089 		goto err1;
3090 	}
3091 
3092 	route->num_paths = 1;
3093 
3094 	ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3095 	if (!ndev) {
3096 		ret = -ENODEV;
3097 		goto err2;
3098 	}
3099 
3100 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3101 		    &route->path_rec->sgid);
3102 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
3103 		    &route->path_rec->dgid);
3104 
3105 	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3106 		/* TODO: get the hoplimit from the inet/inet6 device */
3107 		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3108 	else
3109 		route->path_rec->hop_limit = 1;
3110 	route->path_rec->reversible = 1;
3111 	route->path_rec->pkey = cpu_to_be16(0xffff);
3112 	route->path_rec->mtu_selector = IB_SA_EQ;
3113 	route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
3114 	route->path_rec->traffic_class = tos;
3115 	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
3116 	route->path_rec->rate_selector = IB_SA_EQ;
3117 	route->path_rec->rate = iboe_get_rate(ndev);
3118 	dev_put(ndev);
3119 	route->path_rec->packet_life_time_selector = IB_SA_EQ;
3120 	/* In case ACK timeout is set, use this value to calculate
3121 	 * PacketLifeTime.  As per IBTA 12.7.34,
3122 	 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3123 	 * Assuming a negligible local ACK delay, we can use
3124 	 * PacketLifeTime = local ACK timeout/2
3125 	 * as a reasonable approximation for RoCE networks.
3126 	 */
3127 	mutex_lock(&id_priv->qp_mutex);
3128 	if (id_priv->timeout_set && id_priv->timeout)
3129 		route->path_rec->packet_life_time = id_priv->timeout - 1;
3130 	else
3131 		route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
3132 	mutex_unlock(&id_priv->qp_mutex);
3133 
3134 	if (!route->path_rec->mtu) {
3135 		ret = -EINVAL;
3136 		goto err2;
3137 	}
3138 
3139 	if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3140 					 id_priv->id.port_num))
3141 		route->path_rec->flow_label =
3142 			cma_get_roce_udp_flow_label(id_priv);
3143 
3144 	cma_init_resolve_route_work(work, id_priv);
3145 	queue_work(cma_wq, &work->work);
3146 
3147 	return 0;
3148 
3149 err2:
3150 	kfree(route->path_rec);
3151 	route->path_rec = NULL;
3152 	route->num_paths = 0;
3153 err1:
3154 	kfree(work);
3155 	return ret;
3156 }
3157 
3158 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3159 {
3160 	struct rdma_id_private *id_priv;
3161 	int ret;
3162 
3163 	if (!timeout_ms)
3164 		return -EINVAL;
3165 
3166 	id_priv = container_of(id, struct rdma_id_private, id);
3167 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3168 		return -EINVAL;
3169 
3170 	cma_id_get(id_priv);
3171 	if (rdma_cap_ib_sa(id->device, id->port_num))
3172 		ret = cma_resolve_ib_route(id_priv, timeout_ms);
3173 	else if (rdma_protocol_roce(id->device, id->port_num))
3174 		ret = cma_resolve_iboe_route(id_priv);
3175 	else if (rdma_protocol_iwarp(id->device, id->port_num))
3176 		ret = cma_resolve_iw_route(id_priv);
3177 	else
3178 		ret = -ENOSYS;
3179 
3180 	if (ret)
3181 		goto err;
3182 
3183 	return 0;
3184 err:
3185 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3186 	cma_id_put(id_priv);
3187 	return ret;
3188 }
3189 EXPORT_SYMBOL(rdma_resolve_route);
3190 
3191 static void cma_set_loopback(struct sockaddr *addr)
3192 {
3193 	switch (addr->sa_family) {
3194 	case AF_INET:
3195 		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3196 		break;
3197 	case AF_INET6:
3198 		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3199 			      0, 0, 0, htonl(1));
3200 		break;
3201 	default:
3202 		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3203 			    0, 0, 0, htonl(1));
3204 		break;
3205 	}
3206 }
3207 
3208 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3209 {
3210 	struct cma_device *cma_dev, *cur_dev;
3211 	union ib_gid gid;
3212 	enum ib_port_state port_state;
3213 	unsigned int p;
3214 	u16 pkey;
3215 	int ret;
3216 
3217 	cma_dev = NULL;
3218 	mutex_lock(&lock);
3219 	list_for_each_entry(cur_dev, &dev_list, list) {
3220 		if (cma_family(id_priv) == AF_IB &&
3221 		    !rdma_cap_ib_cm(cur_dev->device, 1))
3222 			continue;
3223 
3224 		if (!cma_dev)
3225 			cma_dev = cur_dev;
3226 
3227 		rdma_for_each_port (cur_dev->device, p) {
3228 			if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3229 			    port_state == IB_PORT_ACTIVE) {
3230 				cma_dev = cur_dev;
3231 				goto port_found;
3232 			}
3233 		}
3234 	}
3235 
3236 	if (!cma_dev) {
3237 		ret = -ENODEV;
3238 		goto out;
3239 	}
3240 
3241 	p = 1;
3242 
3243 port_found:
3244 	ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3245 	if (ret)
3246 		goto out;
3247 
3248 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3249 	if (ret)
3250 		goto out;
3251 
3252 	id_priv->id.route.addr.dev_addr.dev_type =
3253 		(rdma_protocol_ib(cma_dev->device, p)) ?
3254 		ARPHRD_INFINIBAND : ARPHRD_ETHER;
3255 
3256 	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3257 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3258 	id_priv->id.port_num = p;
3259 	cma_attach_to_dev(id_priv, cma_dev);
3260 	rdma_restrack_add(&id_priv->res);
3261 	cma_set_loopback(cma_src_addr(id_priv));
3262 out:
3263 	mutex_unlock(&lock);
3264 	return ret;
3265 }
3266 
3267 static void addr_handler(int status, struct sockaddr *src_addr,
3268 			 struct rdma_dev_addr *dev_addr, void *context)
3269 {
3270 	struct rdma_id_private *id_priv = context;
3271 	struct rdma_cm_event event = {};
3272 	struct sockaddr *addr;
3273 	struct sockaddr_storage old_addr;
3274 
3275 	mutex_lock(&id_priv->handler_mutex);
3276 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3277 			   RDMA_CM_ADDR_RESOLVED))
3278 		goto out;
3279 
3280 	/*
3281 	 * Store the previous src address, so that if we fail to acquire
3282 	 * matching rdma device, old address can be restored back, which helps
3283 	 * to cancel the cma listen operation correctly.
3284 	 */
3285 	addr = cma_src_addr(id_priv);
3286 	memcpy(&old_addr, addr, rdma_addr_size(addr));
3287 	memcpy(addr, src_addr, rdma_addr_size(src_addr));
3288 	if (!status && !id_priv->cma_dev) {
3289 		status = cma_acquire_dev_by_src_ip(id_priv);
3290 		if (status)
3291 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3292 					     status);
3293 		rdma_restrack_add(&id_priv->res);
3294 	} else if (status) {
3295 		pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3296 	}
3297 
3298 	if (status) {
3299 		memcpy(addr, &old_addr,
3300 		       rdma_addr_size((struct sockaddr *)&old_addr));
3301 		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3302 				   RDMA_CM_ADDR_BOUND))
3303 			goto out;
3304 		event.event = RDMA_CM_EVENT_ADDR_ERROR;
3305 		event.status = status;
3306 	} else
3307 		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3308 
3309 	if (cma_cm_event_handler(id_priv, &event)) {
3310 		destroy_id_handler_unlock(id_priv);
3311 		return;
3312 	}
3313 out:
3314 	mutex_unlock(&id_priv->handler_mutex);
3315 }
3316 
3317 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3318 {
3319 	struct cma_work *work;
3320 	union ib_gid gid;
3321 	int ret;
3322 
3323 	work = kzalloc(sizeof *work, GFP_KERNEL);
3324 	if (!work)
3325 		return -ENOMEM;
3326 
3327 	if (!id_priv->cma_dev) {
3328 		ret = cma_bind_loopback(id_priv);
3329 		if (ret)
3330 			goto err;
3331 	}
3332 
3333 	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3334 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3335 
3336 	enqueue_resolve_addr_work(work, id_priv);
3337 	return 0;
3338 err:
3339 	kfree(work);
3340 	return ret;
3341 }
3342 
3343 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3344 {
3345 	struct cma_work *work;
3346 	int ret;
3347 
3348 	work = kzalloc(sizeof *work, GFP_KERNEL);
3349 	if (!work)
3350 		return -ENOMEM;
3351 
3352 	if (!id_priv->cma_dev) {
3353 		ret = cma_resolve_ib_dev(id_priv);
3354 		if (ret)
3355 			goto err;
3356 	}
3357 
3358 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3359 		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3360 
3361 	enqueue_resolve_addr_work(work, id_priv);
3362 	return 0;
3363 err:
3364 	kfree(work);
3365 	return ret;
3366 }
3367 
3368 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3369 			 const struct sockaddr *dst_addr)
3370 {
3371 	if (!src_addr || !src_addr->sa_family) {
3372 		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
3373 		src_addr->sa_family = dst_addr->sa_family;
3374 		if (IS_ENABLED(CONFIG_IPV6) &&
3375 		    dst_addr->sa_family == AF_INET6) {
3376 			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
3377 			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
3378 			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
3379 			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
3380 				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
3381 		} else if (dst_addr->sa_family == AF_IB) {
3382 			((struct sockaddr_ib *) src_addr)->sib_pkey =
3383 				((struct sockaddr_ib *) dst_addr)->sib_pkey;
3384 		}
3385 	}
3386 	return rdma_bind_addr(id, src_addr);
3387 }
3388 
3389 /*
3390  * If required, resolve the source address for bind and leave the id_priv in
3391  * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3392  * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3393  * ignored.
3394  */
3395 static int resolve_prepare_src(struct rdma_id_private *id_priv,
3396 			       struct sockaddr *src_addr,
3397 			       const struct sockaddr *dst_addr)
3398 {
3399 	int ret;
3400 
3401 	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3402 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3403 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3404 		ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3405 		if (ret)
3406 			goto err_dst;
3407 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3408 					   RDMA_CM_ADDR_QUERY))) {
3409 			ret = -EINVAL;
3410 			goto err_dst;
3411 		}
3412 	}
3413 
3414 	if (cma_family(id_priv) != dst_addr->sa_family) {
3415 		ret = -EINVAL;
3416 		goto err_state;
3417 	}
3418 	return 0;
3419 
3420 err_state:
3421 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3422 err_dst:
3423 	memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3424 	return ret;
3425 }
3426 
3427 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3428 		      const struct sockaddr *dst_addr, unsigned long timeout_ms)
3429 {
3430 	struct rdma_id_private *id_priv =
3431 		container_of(id, struct rdma_id_private, id);
3432 	int ret;
3433 
3434 	ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3435 	if (ret)
3436 		return ret;
3437 
3438 	if (cma_any_addr(dst_addr)) {
3439 		ret = cma_resolve_loopback(id_priv);
3440 	} else {
3441 		if (dst_addr->sa_family == AF_IB) {
3442 			ret = cma_resolve_ib_addr(id_priv);
3443 		} else {
3444 			/*
3445 			 * The FSM can return back to RDMA_CM_ADDR_BOUND after
3446 			 * rdma_resolve_ip() is called, eg through the error
3447 			 * path in addr_handler(). If this happens the existing
3448 			 * request must be canceled before issuing a new one.
3449 			 * Since canceling a request is a bit slow and this
3450 			 * oddball path is rare, keep track once a request has
3451 			 * been issued. The track turns out to be a permanent
3452 			 * state since this is the only cancel as it is
3453 			 * immediately before rdma_resolve_ip().
3454 			 */
3455 			if (id_priv->used_resolve_ip)
3456 				rdma_addr_cancel(&id->route.addr.dev_addr);
3457 			else
3458 				id_priv->used_resolve_ip = 1;
3459 			ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
3460 					      &id->route.addr.dev_addr,
3461 					      timeout_ms, addr_handler,
3462 					      false, id_priv);
3463 		}
3464 	}
3465 	if (ret)
3466 		goto err;
3467 
3468 	return 0;
3469 err:
3470 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3471 	return ret;
3472 }
3473 EXPORT_SYMBOL(rdma_resolve_addr);
3474 
3475 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3476 {
3477 	struct rdma_id_private *id_priv;
3478 	unsigned long flags;
3479 	int ret;
3480 
3481 	id_priv = container_of(id, struct rdma_id_private, id);
3482 	spin_lock_irqsave(&id_priv->lock, flags);
3483 	if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3484 	    id_priv->state == RDMA_CM_IDLE) {
3485 		id_priv->reuseaddr = reuse;
3486 		ret = 0;
3487 	} else {
3488 		ret = -EINVAL;
3489 	}
3490 	spin_unlock_irqrestore(&id_priv->lock, flags);
3491 	return ret;
3492 }
3493 EXPORT_SYMBOL(rdma_set_reuseaddr);
3494 
3495 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3496 {
3497 	struct rdma_id_private *id_priv;
3498 	unsigned long flags;
3499 	int ret;
3500 
3501 	id_priv = container_of(id, struct rdma_id_private, id);
3502 	spin_lock_irqsave(&id_priv->lock, flags);
3503 	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3504 		id_priv->options |= (1 << CMA_OPTION_AFONLY);
3505 		id_priv->afonly = afonly;
3506 		ret = 0;
3507 	} else {
3508 		ret = -EINVAL;
3509 	}
3510 	spin_unlock_irqrestore(&id_priv->lock, flags);
3511 	return ret;
3512 }
3513 EXPORT_SYMBOL(rdma_set_afonly);
3514 
3515 static void cma_bind_port(struct rdma_bind_list *bind_list,
3516 			  struct rdma_id_private *id_priv)
3517 {
3518 	struct sockaddr *addr;
3519 	struct sockaddr_ib *sib;
3520 	u64 sid, mask;
3521 	__be16 port;
3522 
3523 	lockdep_assert_held(&lock);
3524 
3525 	addr = cma_src_addr(id_priv);
3526 	port = htons(bind_list->port);
3527 
3528 	switch (addr->sa_family) {
3529 	case AF_INET:
3530 		((struct sockaddr_in *) addr)->sin_port = port;
3531 		break;
3532 	case AF_INET6:
3533 		((struct sockaddr_in6 *) addr)->sin6_port = port;
3534 		break;
3535 	case AF_IB:
3536 		sib = (struct sockaddr_ib *) addr;
3537 		sid = be64_to_cpu(sib->sib_sid);
3538 		mask = be64_to_cpu(sib->sib_sid_mask);
3539 		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3540 		sib->sib_sid_mask = cpu_to_be64(~0ULL);
3541 		break;
3542 	}
3543 	id_priv->bind_list = bind_list;
3544 	hlist_add_head(&id_priv->node, &bind_list->owners);
3545 }
3546 
3547 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3548 			  struct rdma_id_private *id_priv, unsigned short snum)
3549 {
3550 	struct rdma_bind_list *bind_list;
3551 	int ret;
3552 
3553 	lockdep_assert_held(&lock);
3554 
3555 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3556 	if (!bind_list)
3557 		return -ENOMEM;
3558 
3559 	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3560 			   snum);
3561 	if (ret < 0)
3562 		goto err;
3563 
3564 	bind_list->ps = ps;
3565 	bind_list->port = snum;
3566 	cma_bind_port(bind_list, id_priv);
3567 	return 0;
3568 err:
3569 	kfree(bind_list);
3570 	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3571 }
3572 
3573 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3574 			      struct rdma_id_private *id_priv)
3575 {
3576 	struct rdma_id_private *cur_id;
3577 	struct sockaddr  *daddr = cma_dst_addr(id_priv);
3578 	struct sockaddr  *saddr = cma_src_addr(id_priv);
3579 	__be16 dport = cma_port(daddr);
3580 
3581 	lockdep_assert_held(&lock);
3582 
3583 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3584 		struct sockaddr  *cur_daddr = cma_dst_addr(cur_id);
3585 		struct sockaddr  *cur_saddr = cma_src_addr(cur_id);
3586 		__be16 cur_dport = cma_port(cur_daddr);
3587 
3588 		if (id_priv == cur_id)
3589 			continue;
3590 
3591 		/* different dest port -> unique */
3592 		if (!cma_any_port(daddr) &&
3593 		    !cma_any_port(cur_daddr) &&
3594 		    (dport != cur_dport))
3595 			continue;
3596 
3597 		/* different src address -> unique */
3598 		if (!cma_any_addr(saddr) &&
3599 		    !cma_any_addr(cur_saddr) &&
3600 		    cma_addr_cmp(saddr, cur_saddr))
3601 			continue;
3602 
3603 		/* different dst address -> unique */
3604 		if (!cma_any_addr(daddr) &&
3605 		    !cma_any_addr(cur_daddr) &&
3606 		    cma_addr_cmp(daddr, cur_daddr))
3607 			continue;
3608 
3609 		return -EADDRNOTAVAIL;
3610 	}
3611 	return 0;
3612 }
3613 
3614 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3615 			      struct rdma_id_private *id_priv)
3616 {
3617 	static unsigned int last_used_port;
3618 	int low, high, remaining;
3619 	unsigned int rover;
3620 	struct net *net = id_priv->id.route.addr.dev_addr.net;
3621 
3622 	lockdep_assert_held(&lock);
3623 
3624 	inet_get_local_port_range(net, &low, &high);
3625 	remaining = (high - low) + 1;
3626 	rover = prandom_u32() % remaining + low;
3627 retry:
3628 	if (last_used_port != rover) {
3629 		struct rdma_bind_list *bind_list;
3630 		int ret;
3631 
3632 		bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3633 
3634 		if (!bind_list) {
3635 			ret = cma_alloc_port(ps, id_priv, rover);
3636 		} else {
3637 			ret = cma_port_is_unique(bind_list, id_priv);
3638 			if (!ret)
3639 				cma_bind_port(bind_list, id_priv);
3640 		}
3641 		/*
3642 		 * Remember previously used port number in order to avoid
3643 		 * re-using same port immediately after it is closed.
3644 		 */
3645 		if (!ret)
3646 			last_used_port = rover;
3647 		if (ret != -EADDRNOTAVAIL)
3648 			return ret;
3649 	}
3650 	if (--remaining) {
3651 		rover++;
3652 		if ((rover < low) || (rover > high))
3653 			rover = low;
3654 		goto retry;
3655 	}
3656 	return -EADDRNOTAVAIL;
3657 }
3658 
3659 /*
3660  * Check that the requested port is available.  This is called when trying to
3661  * bind to a specific port, or when trying to listen on a bound port.  In
3662  * the latter case, the provided id_priv may already be on the bind_list, but
3663  * we still need to check that it's okay to start listening.
3664  */
3665 static int cma_check_port(struct rdma_bind_list *bind_list,
3666 			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3667 {
3668 	struct rdma_id_private *cur_id;
3669 	struct sockaddr *addr, *cur_addr;
3670 
3671 	lockdep_assert_held(&lock);
3672 
3673 	addr = cma_src_addr(id_priv);
3674 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3675 		if (id_priv == cur_id)
3676 			continue;
3677 
3678 		if (reuseaddr && cur_id->reuseaddr)
3679 			continue;
3680 
3681 		cur_addr = cma_src_addr(cur_id);
3682 		if (id_priv->afonly && cur_id->afonly &&
3683 		    (addr->sa_family != cur_addr->sa_family))
3684 			continue;
3685 
3686 		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3687 			return -EADDRNOTAVAIL;
3688 
3689 		if (!cma_addr_cmp(addr, cur_addr))
3690 			return -EADDRINUSE;
3691 	}
3692 	return 0;
3693 }
3694 
3695 static int cma_use_port(enum rdma_ucm_port_space ps,
3696 			struct rdma_id_private *id_priv)
3697 {
3698 	struct rdma_bind_list *bind_list;
3699 	unsigned short snum;
3700 	int ret;
3701 
3702 	lockdep_assert_held(&lock);
3703 
3704 	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3705 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3706 		return -EACCES;
3707 
3708 	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3709 	if (!bind_list) {
3710 		ret = cma_alloc_port(ps, id_priv, snum);
3711 	} else {
3712 		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3713 		if (!ret)
3714 			cma_bind_port(bind_list, id_priv);
3715 	}
3716 	return ret;
3717 }
3718 
3719 static enum rdma_ucm_port_space
3720 cma_select_inet_ps(struct rdma_id_private *id_priv)
3721 {
3722 	switch (id_priv->id.ps) {
3723 	case RDMA_PS_TCP:
3724 	case RDMA_PS_UDP:
3725 	case RDMA_PS_IPOIB:
3726 	case RDMA_PS_IB:
3727 		return id_priv->id.ps;
3728 	default:
3729 
3730 		return 0;
3731 	}
3732 }
3733 
3734 static enum rdma_ucm_port_space
3735 cma_select_ib_ps(struct rdma_id_private *id_priv)
3736 {
3737 	enum rdma_ucm_port_space ps = 0;
3738 	struct sockaddr_ib *sib;
3739 	u64 sid_ps, mask, sid;
3740 
3741 	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3742 	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3743 	sid = be64_to_cpu(sib->sib_sid) & mask;
3744 
3745 	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3746 		sid_ps = RDMA_IB_IP_PS_IB;
3747 		ps = RDMA_PS_IB;
3748 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3749 		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3750 		sid_ps = RDMA_IB_IP_PS_TCP;
3751 		ps = RDMA_PS_TCP;
3752 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3753 		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3754 		sid_ps = RDMA_IB_IP_PS_UDP;
3755 		ps = RDMA_PS_UDP;
3756 	}
3757 
3758 	if (ps) {
3759 		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3760 		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3761 						be64_to_cpu(sib->sib_sid_mask));
3762 	}
3763 	return ps;
3764 }
3765 
3766 static int cma_get_port(struct rdma_id_private *id_priv)
3767 {
3768 	enum rdma_ucm_port_space ps;
3769 	int ret;
3770 
3771 	if (cma_family(id_priv) != AF_IB)
3772 		ps = cma_select_inet_ps(id_priv);
3773 	else
3774 		ps = cma_select_ib_ps(id_priv);
3775 	if (!ps)
3776 		return -EPROTONOSUPPORT;
3777 
3778 	mutex_lock(&lock);
3779 	if (cma_any_port(cma_src_addr(id_priv)))
3780 		ret = cma_alloc_any_port(ps, id_priv);
3781 	else
3782 		ret = cma_use_port(ps, id_priv);
3783 	mutex_unlock(&lock);
3784 
3785 	return ret;
3786 }
3787 
3788 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3789 			       struct sockaddr *addr)
3790 {
3791 #if IS_ENABLED(CONFIG_IPV6)
3792 	struct sockaddr_in6 *sin6;
3793 
3794 	if (addr->sa_family != AF_INET6)
3795 		return 0;
3796 
3797 	sin6 = (struct sockaddr_in6 *) addr;
3798 
3799 	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3800 		return 0;
3801 
3802 	if (!sin6->sin6_scope_id)
3803 			return -EINVAL;
3804 
3805 	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3806 #endif
3807 	return 0;
3808 }
3809 
3810 int rdma_listen(struct rdma_cm_id *id, int backlog)
3811 {
3812 	struct rdma_id_private *id_priv =
3813 		container_of(id, struct rdma_id_private, id);
3814 	int ret;
3815 
3816 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3817 		struct sockaddr_in any_in = {
3818 			.sin_family = AF_INET,
3819 			.sin_addr.s_addr = htonl(INADDR_ANY),
3820 		};
3821 
3822 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3823 		ret = rdma_bind_addr(id, (struct sockaddr *)&any_in);
3824 		if (ret)
3825 			return ret;
3826 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3827 					   RDMA_CM_LISTEN)))
3828 			return -EINVAL;
3829 	}
3830 
3831 	/*
3832 	 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3833 	 * any more, and has to be unique in the bind list.
3834 	 */
3835 	if (id_priv->reuseaddr) {
3836 		mutex_lock(&lock);
3837 		ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3838 		if (!ret)
3839 			id_priv->reuseaddr = 0;
3840 		mutex_unlock(&lock);
3841 		if (ret)
3842 			goto err;
3843 	}
3844 
3845 	id_priv->backlog = backlog;
3846 	if (id_priv->cma_dev) {
3847 		if (rdma_cap_ib_cm(id->device, 1)) {
3848 			ret = cma_ib_listen(id_priv);
3849 			if (ret)
3850 				goto err;
3851 		} else if (rdma_cap_iw_cm(id->device, 1)) {
3852 			ret = cma_iw_listen(id_priv, backlog);
3853 			if (ret)
3854 				goto err;
3855 		} else {
3856 			ret = -ENOSYS;
3857 			goto err;
3858 		}
3859 	} else {
3860 		ret = cma_listen_on_all(id_priv);
3861 		if (ret)
3862 			goto err;
3863 	}
3864 
3865 	return 0;
3866 err:
3867 	id_priv->backlog = 0;
3868 	/*
3869 	 * All the failure paths that lead here will not allow the req_handler's
3870 	 * to have run.
3871 	 */
3872 	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3873 	return ret;
3874 }
3875 EXPORT_SYMBOL(rdma_listen);
3876 
3877 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3878 {
3879 	struct rdma_id_private *id_priv;
3880 	int ret;
3881 	struct sockaddr  *daddr;
3882 
3883 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3884 	    addr->sa_family != AF_IB)
3885 		return -EAFNOSUPPORT;
3886 
3887 	id_priv = container_of(id, struct rdma_id_private, id);
3888 	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3889 		return -EINVAL;
3890 
3891 	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3892 	if (ret)
3893 		goto err1;
3894 
3895 	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3896 	if (!cma_any_addr(addr)) {
3897 		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3898 		if (ret)
3899 			goto err1;
3900 
3901 		ret = cma_acquire_dev_by_src_ip(id_priv);
3902 		if (ret)
3903 			goto err1;
3904 	}
3905 
3906 	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3907 		if (addr->sa_family == AF_INET)
3908 			id_priv->afonly = 1;
3909 #if IS_ENABLED(CONFIG_IPV6)
3910 		else if (addr->sa_family == AF_INET6) {
3911 			struct net *net = id_priv->id.route.addr.dev_addr.net;
3912 
3913 			id_priv->afonly = net->ipv6.sysctl.bindv6only;
3914 		}
3915 #endif
3916 	}
3917 	daddr = cma_dst_addr(id_priv);
3918 	daddr->sa_family = addr->sa_family;
3919 
3920 	ret = cma_get_port(id_priv);
3921 	if (ret)
3922 		goto err2;
3923 
3924 	if (!cma_any_addr(addr))
3925 		rdma_restrack_add(&id_priv->res);
3926 	return 0;
3927 err2:
3928 	if (id_priv->cma_dev)
3929 		cma_release_dev(id_priv);
3930 err1:
3931 	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3932 	return ret;
3933 }
3934 EXPORT_SYMBOL(rdma_bind_addr);
3935 
3936 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3937 {
3938 	struct cma_hdr *cma_hdr;
3939 
3940 	cma_hdr = hdr;
3941 	cma_hdr->cma_version = CMA_VERSION;
3942 	if (cma_family(id_priv) == AF_INET) {
3943 		struct sockaddr_in *src4, *dst4;
3944 
3945 		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3946 		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3947 
3948 		cma_set_ip_ver(cma_hdr, 4);
3949 		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3950 		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3951 		cma_hdr->port = src4->sin_port;
3952 	} else if (cma_family(id_priv) == AF_INET6) {
3953 		struct sockaddr_in6 *src6, *dst6;
3954 
3955 		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3956 		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3957 
3958 		cma_set_ip_ver(cma_hdr, 6);
3959 		cma_hdr->src_addr.ip6 = src6->sin6_addr;
3960 		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3961 		cma_hdr->port = src6->sin6_port;
3962 	}
3963 	return 0;
3964 }
3965 
3966 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3967 				const struct ib_cm_event *ib_event)
3968 {
3969 	struct rdma_id_private *id_priv = cm_id->context;
3970 	struct rdma_cm_event event = {};
3971 	const struct ib_cm_sidr_rep_event_param *rep =
3972 				&ib_event->param.sidr_rep_rcvd;
3973 	int ret;
3974 
3975 	mutex_lock(&id_priv->handler_mutex);
3976 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
3977 		goto out;
3978 
3979 	switch (ib_event->event) {
3980 	case IB_CM_SIDR_REQ_ERROR:
3981 		event.event = RDMA_CM_EVENT_UNREACHABLE;
3982 		event.status = -ETIMEDOUT;
3983 		break;
3984 	case IB_CM_SIDR_REP_RECEIVED:
3985 		event.param.ud.private_data = ib_event->private_data;
3986 		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3987 		if (rep->status != IB_SIDR_SUCCESS) {
3988 			event.event = RDMA_CM_EVENT_UNREACHABLE;
3989 			event.status = ib_event->param.sidr_rep_rcvd.status;
3990 			pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3991 					     event.status);
3992 			break;
3993 		}
3994 		ret = cma_set_qkey(id_priv, rep->qkey);
3995 		if (ret) {
3996 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
3997 			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3998 			event.status = ret;
3999 			break;
4000 		}
4001 		ib_init_ah_attr_from_path(id_priv->id.device,
4002 					  id_priv->id.port_num,
4003 					  id_priv->id.route.path_rec,
4004 					  &event.param.ud.ah_attr,
4005 					  rep->sgid_attr);
4006 		event.param.ud.qp_num = rep->qpn;
4007 		event.param.ud.qkey = rep->qkey;
4008 		event.event = RDMA_CM_EVENT_ESTABLISHED;
4009 		event.status = 0;
4010 		break;
4011 	default:
4012 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
4013 		       ib_event->event);
4014 		goto out;
4015 	}
4016 
4017 	ret = cma_cm_event_handler(id_priv, &event);
4018 
4019 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4020 	if (ret) {
4021 		/* Destroy the CM ID by returning a non-zero value. */
4022 		id_priv->cm_id.ib = NULL;
4023 		destroy_id_handler_unlock(id_priv);
4024 		return ret;
4025 	}
4026 out:
4027 	mutex_unlock(&id_priv->handler_mutex);
4028 	return 0;
4029 }
4030 
4031 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
4032 			      struct rdma_conn_param *conn_param)
4033 {
4034 	struct ib_cm_sidr_req_param req;
4035 	struct ib_cm_id	*id;
4036 	void *private_data;
4037 	u8 offset;
4038 	int ret;
4039 
4040 	memset(&req, 0, sizeof req);
4041 	offset = cma_user_data_offset(id_priv);
4042 	if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
4043 		return -EINVAL;
4044 
4045 	if (req.private_data_len) {
4046 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4047 		if (!private_data)
4048 			return -ENOMEM;
4049 	} else {
4050 		private_data = NULL;
4051 	}
4052 
4053 	if (conn_param->private_data && conn_param->private_data_len)
4054 		memcpy(private_data + offset, conn_param->private_data,
4055 		       conn_param->private_data_len);
4056 
4057 	if (private_data) {
4058 		ret = cma_format_hdr(private_data, id_priv);
4059 		if (ret)
4060 			goto out;
4061 		req.private_data = private_data;
4062 	}
4063 
4064 	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
4065 			     id_priv);
4066 	if (IS_ERR(id)) {
4067 		ret = PTR_ERR(id);
4068 		goto out;
4069 	}
4070 	id_priv->cm_id.ib = id;
4071 
4072 	req.path = id_priv->id.route.path_rec;
4073 	req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4074 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4075 	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4076 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4077 
4078 	trace_cm_send_sidr_req(id_priv);
4079 	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4080 	if (ret) {
4081 		ib_destroy_cm_id(id_priv->cm_id.ib);
4082 		id_priv->cm_id.ib = NULL;
4083 	}
4084 out:
4085 	kfree(private_data);
4086 	return ret;
4087 }
4088 
4089 static int cma_connect_ib(struct rdma_id_private *id_priv,
4090 			  struct rdma_conn_param *conn_param)
4091 {
4092 	struct ib_cm_req_param req;
4093 	struct rdma_route *route;
4094 	void *private_data;
4095 	struct ib_cm_id	*id;
4096 	u8 offset;
4097 	int ret;
4098 
4099 	memset(&req, 0, sizeof req);
4100 	offset = cma_user_data_offset(id_priv);
4101 	if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
4102 		return -EINVAL;
4103 
4104 	if (req.private_data_len) {
4105 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4106 		if (!private_data)
4107 			return -ENOMEM;
4108 	} else {
4109 		private_data = NULL;
4110 	}
4111 
4112 	if (conn_param->private_data && conn_param->private_data_len)
4113 		memcpy(private_data + offset, conn_param->private_data,
4114 		       conn_param->private_data_len);
4115 
4116 	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
4117 	if (IS_ERR(id)) {
4118 		ret = PTR_ERR(id);
4119 		goto out;
4120 	}
4121 	id_priv->cm_id.ib = id;
4122 
4123 	route = &id_priv->id.route;
4124 	if (private_data) {
4125 		ret = cma_format_hdr(private_data, id_priv);
4126 		if (ret)
4127 			goto out;
4128 		req.private_data = private_data;
4129 	}
4130 
4131 	req.primary_path = &route->path_rec[0];
4132 	if (route->num_paths == 2)
4133 		req.alternate_path = &route->path_rec[1];
4134 
4135 	req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4136 	/* Alternate path SGID attribute currently unsupported */
4137 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4138 	req.qp_num = id_priv->qp_num;
4139 	req.qp_type = id_priv->id.qp_type;
4140 	req.starting_psn = id_priv->seq_num;
4141 	req.responder_resources = conn_param->responder_resources;
4142 	req.initiator_depth = conn_param->initiator_depth;
4143 	req.flow_control = conn_param->flow_control;
4144 	req.retry_count = min_t(u8, 7, conn_param->retry_count);
4145 	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4146 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4147 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4148 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4149 	req.srq = id_priv->srq ? 1 : 0;
4150 	req.ece.vendor_id = id_priv->ece.vendor_id;
4151 	req.ece.attr_mod = id_priv->ece.attr_mod;
4152 
4153 	trace_cm_send_req(id_priv);
4154 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4155 out:
4156 	if (ret && !IS_ERR(id)) {
4157 		ib_destroy_cm_id(id);
4158 		id_priv->cm_id.ib = NULL;
4159 	}
4160 
4161 	kfree(private_data);
4162 	return ret;
4163 }
4164 
4165 static int cma_connect_iw(struct rdma_id_private *id_priv,
4166 			  struct rdma_conn_param *conn_param)
4167 {
4168 	struct iw_cm_id *cm_id;
4169 	int ret;
4170 	struct iw_cm_conn_param iw_param;
4171 
4172 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4173 	if (IS_ERR(cm_id))
4174 		return PTR_ERR(cm_id);
4175 
4176 	mutex_lock(&id_priv->qp_mutex);
4177 	cm_id->tos = id_priv->tos;
4178 	cm_id->tos_set = id_priv->tos_set;
4179 	mutex_unlock(&id_priv->qp_mutex);
4180 
4181 	id_priv->cm_id.iw = cm_id;
4182 
4183 	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4184 	       rdma_addr_size(cma_src_addr(id_priv)));
4185 	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4186 	       rdma_addr_size(cma_dst_addr(id_priv)));
4187 
4188 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4189 	if (ret)
4190 		goto out;
4191 
4192 	if (conn_param) {
4193 		iw_param.ord = conn_param->initiator_depth;
4194 		iw_param.ird = conn_param->responder_resources;
4195 		iw_param.private_data = conn_param->private_data;
4196 		iw_param.private_data_len = conn_param->private_data_len;
4197 		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4198 	} else {
4199 		memset(&iw_param, 0, sizeof iw_param);
4200 		iw_param.qpn = id_priv->qp_num;
4201 	}
4202 	ret = iw_cm_connect(cm_id, &iw_param);
4203 out:
4204 	if (ret) {
4205 		iw_destroy_cm_id(cm_id);
4206 		id_priv->cm_id.iw = NULL;
4207 	}
4208 	return ret;
4209 }
4210 
4211 /**
4212  * rdma_connect_locked - Initiate an active connection request.
4213  * @id: Connection identifier to connect.
4214  * @conn_param: Connection information used for connected QPs.
4215  *
4216  * Same as rdma_connect() but can only be called from the
4217  * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4218  */
4219 int rdma_connect_locked(struct rdma_cm_id *id,
4220 			struct rdma_conn_param *conn_param)
4221 {
4222 	struct rdma_id_private *id_priv =
4223 		container_of(id, struct rdma_id_private, id);
4224 	int ret;
4225 
4226 	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4227 		return -EINVAL;
4228 
4229 	if (!id->qp) {
4230 		id_priv->qp_num = conn_param->qp_num;
4231 		id_priv->srq = conn_param->srq;
4232 	}
4233 
4234 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4235 		if (id->qp_type == IB_QPT_UD)
4236 			ret = cma_resolve_ib_udp(id_priv, conn_param);
4237 		else
4238 			ret = cma_connect_ib(id_priv, conn_param);
4239 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4240 		ret = cma_connect_iw(id_priv, conn_param);
4241 	} else {
4242 		ret = -ENOSYS;
4243 	}
4244 	if (ret)
4245 		goto err_state;
4246 	return 0;
4247 err_state:
4248 	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4249 	return ret;
4250 }
4251 EXPORT_SYMBOL(rdma_connect_locked);
4252 
4253 /**
4254  * rdma_connect - Initiate an active connection request.
4255  * @id: Connection identifier to connect.
4256  * @conn_param: Connection information used for connected QPs.
4257  *
4258  * Users must have resolved a route for the rdma_cm_id to connect with by having
4259  * called rdma_resolve_route before calling this routine.
4260  *
4261  * This call will either connect to a remote QP or obtain remote QP information
4262  * for unconnected rdma_cm_id's.  The actual operation is based on the
4263  * rdma_cm_id's port space.
4264  */
4265 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4266 {
4267 	struct rdma_id_private *id_priv =
4268 		container_of(id, struct rdma_id_private, id);
4269 	int ret;
4270 
4271 	mutex_lock(&id_priv->handler_mutex);
4272 	ret = rdma_connect_locked(id, conn_param);
4273 	mutex_unlock(&id_priv->handler_mutex);
4274 	return ret;
4275 }
4276 EXPORT_SYMBOL(rdma_connect);
4277 
4278 /**
4279  * rdma_connect_ece - Initiate an active connection request with ECE data.
4280  * @id: Connection identifier to connect.
4281  * @conn_param: Connection information used for connected QPs.
4282  * @ece: ECE parameters
4283  *
4284  * See rdma_connect() explanation.
4285  */
4286 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4287 		     struct rdma_ucm_ece *ece)
4288 {
4289 	struct rdma_id_private *id_priv =
4290 		container_of(id, struct rdma_id_private, id);
4291 
4292 	id_priv->ece.vendor_id = ece->vendor_id;
4293 	id_priv->ece.attr_mod = ece->attr_mod;
4294 
4295 	return rdma_connect(id, conn_param);
4296 }
4297 EXPORT_SYMBOL(rdma_connect_ece);
4298 
4299 static int cma_accept_ib(struct rdma_id_private *id_priv,
4300 			 struct rdma_conn_param *conn_param)
4301 {
4302 	struct ib_cm_rep_param rep;
4303 	int ret;
4304 
4305 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4306 	if (ret)
4307 		goto out;
4308 
4309 	ret = cma_modify_qp_rts(id_priv, conn_param);
4310 	if (ret)
4311 		goto out;
4312 
4313 	memset(&rep, 0, sizeof rep);
4314 	rep.qp_num = id_priv->qp_num;
4315 	rep.starting_psn = id_priv->seq_num;
4316 	rep.private_data = conn_param->private_data;
4317 	rep.private_data_len = conn_param->private_data_len;
4318 	rep.responder_resources = conn_param->responder_resources;
4319 	rep.initiator_depth = conn_param->initiator_depth;
4320 	rep.failover_accepted = 0;
4321 	rep.flow_control = conn_param->flow_control;
4322 	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4323 	rep.srq = id_priv->srq ? 1 : 0;
4324 	rep.ece.vendor_id = id_priv->ece.vendor_id;
4325 	rep.ece.attr_mod = id_priv->ece.attr_mod;
4326 
4327 	trace_cm_send_rep(id_priv);
4328 	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4329 out:
4330 	return ret;
4331 }
4332 
4333 static int cma_accept_iw(struct rdma_id_private *id_priv,
4334 		  struct rdma_conn_param *conn_param)
4335 {
4336 	struct iw_cm_conn_param iw_param;
4337 	int ret;
4338 
4339 	if (!conn_param)
4340 		return -EINVAL;
4341 
4342 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4343 	if (ret)
4344 		return ret;
4345 
4346 	iw_param.ord = conn_param->initiator_depth;
4347 	iw_param.ird = conn_param->responder_resources;
4348 	iw_param.private_data = conn_param->private_data;
4349 	iw_param.private_data_len = conn_param->private_data_len;
4350 	if (id_priv->id.qp)
4351 		iw_param.qpn = id_priv->qp_num;
4352 	else
4353 		iw_param.qpn = conn_param->qp_num;
4354 
4355 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4356 }
4357 
4358 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4359 			     enum ib_cm_sidr_status status, u32 qkey,
4360 			     const void *private_data, int private_data_len)
4361 {
4362 	struct ib_cm_sidr_rep_param rep;
4363 	int ret;
4364 
4365 	memset(&rep, 0, sizeof rep);
4366 	rep.status = status;
4367 	if (status == IB_SIDR_SUCCESS) {
4368 		ret = cma_set_qkey(id_priv, qkey);
4369 		if (ret)
4370 			return ret;
4371 		rep.qp_num = id_priv->qp_num;
4372 		rep.qkey = id_priv->qkey;
4373 
4374 		rep.ece.vendor_id = id_priv->ece.vendor_id;
4375 		rep.ece.attr_mod = id_priv->ece.attr_mod;
4376 	}
4377 
4378 	rep.private_data = private_data;
4379 	rep.private_data_len = private_data_len;
4380 
4381 	trace_cm_send_sidr_rep(id_priv);
4382 	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4383 }
4384 
4385 /**
4386  * rdma_accept - Called to accept a connection request or response.
4387  * @id: Connection identifier associated with the request.
4388  * @conn_param: Information needed to establish the connection.  This must be
4389  *   provided if accepting a connection request.  If accepting a connection
4390  *   response, this parameter must be NULL.
4391  *
4392  * Typically, this routine is only called by the listener to accept a connection
4393  * request.  It must also be called on the active side of a connection if the
4394  * user is performing their own QP transitions.
4395  *
4396  * In the case of error, a reject message is sent to the remote side and the
4397  * state of the qp associated with the id is modified to error, such that any
4398  * previously posted receive buffers would be flushed.
4399  *
4400  * This function is for use by kernel ULPs and must be called from under the
4401  * handler callback.
4402  */
4403 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4404 {
4405 	struct rdma_id_private *id_priv =
4406 		container_of(id, struct rdma_id_private, id);
4407 	int ret;
4408 
4409 	lockdep_assert_held(&id_priv->handler_mutex);
4410 
4411 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4412 		return -EINVAL;
4413 
4414 	if (!id->qp && conn_param) {
4415 		id_priv->qp_num = conn_param->qp_num;
4416 		id_priv->srq = conn_param->srq;
4417 	}
4418 
4419 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4420 		if (id->qp_type == IB_QPT_UD) {
4421 			if (conn_param)
4422 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4423 							conn_param->qkey,
4424 							conn_param->private_data,
4425 							conn_param->private_data_len);
4426 			else
4427 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4428 							0, NULL, 0);
4429 		} else {
4430 			if (conn_param)
4431 				ret = cma_accept_ib(id_priv, conn_param);
4432 			else
4433 				ret = cma_rep_recv(id_priv);
4434 		}
4435 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4436 		ret = cma_accept_iw(id_priv, conn_param);
4437 	} else {
4438 		ret = -ENOSYS;
4439 	}
4440 	if (ret)
4441 		goto reject;
4442 
4443 	return 0;
4444 reject:
4445 	cma_modify_qp_err(id_priv);
4446 	rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4447 	return ret;
4448 }
4449 EXPORT_SYMBOL(rdma_accept);
4450 
4451 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4452 		    struct rdma_ucm_ece *ece)
4453 {
4454 	struct rdma_id_private *id_priv =
4455 		container_of(id, struct rdma_id_private, id);
4456 
4457 	id_priv->ece.vendor_id = ece->vendor_id;
4458 	id_priv->ece.attr_mod = ece->attr_mod;
4459 
4460 	return rdma_accept(id, conn_param);
4461 }
4462 EXPORT_SYMBOL(rdma_accept_ece);
4463 
4464 void rdma_lock_handler(struct rdma_cm_id *id)
4465 {
4466 	struct rdma_id_private *id_priv =
4467 		container_of(id, struct rdma_id_private, id);
4468 
4469 	mutex_lock(&id_priv->handler_mutex);
4470 }
4471 EXPORT_SYMBOL(rdma_lock_handler);
4472 
4473 void rdma_unlock_handler(struct rdma_cm_id *id)
4474 {
4475 	struct rdma_id_private *id_priv =
4476 		container_of(id, struct rdma_id_private, id);
4477 
4478 	mutex_unlock(&id_priv->handler_mutex);
4479 }
4480 EXPORT_SYMBOL(rdma_unlock_handler);
4481 
4482 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4483 {
4484 	struct rdma_id_private *id_priv;
4485 	int ret;
4486 
4487 	id_priv = container_of(id, struct rdma_id_private, id);
4488 	if (!id_priv->cm_id.ib)
4489 		return -EINVAL;
4490 
4491 	switch (id->device->node_type) {
4492 	case RDMA_NODE_IB_CA:
4493 		ret = ib_cm_notify(id_priv->cm_id.ib, event);
4494 		break;
4495 	default:
4496 		ret = 0;
4497 		break;
4498 	}
4499 	return ret;
4500 }
4501 EXPORT_SYMBOL(rdma_notify);
4502 
4503 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4504 		u8 private_data_len, u8 reason)
4505 {
4506 	struct rdma_id_private *id_priv;
4507 	int ret;
4508 
4509 	id_priv = container_of(id, struct rdma_id_private, id);
4510 	if (!id_priv->cm_id.ib)
4511 		return -EINVAL;
4512 
4513 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4514 		if (id->qp_type == IB_QPT_UD) {
4515 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4516 						private_data, private_data_len);
4517 		} else {
4518 			trace_cm_send_rej(id_priv);
4519 			ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4520 					     private_data, private_data_len);
4521 		}
4522 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4523 		ret = iw_cm_reject(id_priv->cm_id.iw,
4524 				   private_data, private_data_len);
4525 	} else {
4526 		ret = -ENOSYS;
4527 	}
4528 
4529 	return ret;
4530 }
4531 EXPORT_SYMBOL(rdma_reject);
4532 
4533 int rdma_disconnect(struct rdma_cm_id *id)
4534 {
4535 	struct rdma_id_private *id_priv;
4536 	int ret;
4537 
4538 	id_priv = container_of(id, struct rdma_id_private, id);
4539 	if (!id_priv->cm_id.ib)
4540 		return -EINVAL;
4541 
4542 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4543 		ret = cma_modify_qp_err(id_priv);
4544 		if (ret)
4545 			goto out;
4546 		/* Initiate or respond to a disconnect. */
4547 		trace_cm_disconnect(id_priv);
4548 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4549 			if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4550 				trace_cm_sent_drep(id_priv);
4551 		} else {
4552 			trace_cm_sent_dreq(id_priv);
4553 		}
4554 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4555 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4556 	} else
4557 		ret = -EINVAL;
4558 
4559 out:
4560 	return ret;
4561 }
4562 EXPORT_SYMBOL(rdma_disconnect);
4563 
4564 static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4565 			      struct ib_sa_multicast *multicast,
4566 			      struct rdma_cm_event *event,
4567 			      struct cma_multicast *mc)
4568 {
4569 	struct rdma_dev_addr *dev_addr;
4570 	enum ib_gid_type gid_type;
4571 	struct net_device *ndev;
4572 
4573 	if (!status)
4574 		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4575 	else
4576 		pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4577 				     status);
4578 
4579 	event->status = status;
4580 	event->param.ud.private_data = mc->context;
4581 	if (status) {
4582 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4583 		return;
4584 	}
4585 
4586 	dev_addr = &id_priv->id.route.addr.dev_addr;
4587 	ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4588 	gid_type =
4589 		id_priv->cma_dev
4590 			->default_gid_type[id_priv->id.port_num -
4591 					   rdma_start_port(
4592 						   id_priv->cma_dev->device)];
4593 
4594 	event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4595 	if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4596 				     &multicast->rec, ndev, gid_type,
4597 				     &event->param.ud.ah_attr)) {
4598 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4599 		goto out;
4600 	}
4601 
4602 	event->param.ud.qp_num = 0xFFFFFF;
4603 	event->param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
4604 
4605 out:
4606 	if (ndev)
4607 		dev_put(ndev);
4608 }
4609 
4610 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4611 {
4612 	struct cma_multicast *mc = multicast->context;
4613 	struct rdma_id_private *id_priv = mc->id_priv;
4614 	struct rdma_cm_event event = {};
4615 	int ret = 0;
4616 
4617 	mutex_lock(&id_priv->handler_mutex);
4618 	if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4619 	    READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4620 		goto out;
4621 
4622 	cma_make_mc_event(status, id_priv, multicast, &event, mc);
4623 	ret = cma_cm_event_handler(id_priv, &event);
4624 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4625 	WARN_ON(ret);
4626 
4627 out:
4628 	mutex_unlock(&id_priv->handler_mutex);
4629 	return 0;
4630 }
4631 
4632 static void cma_set_mgid(struct rdma_id_private *id_priv,
4633 			 struct sockaddr *addr, union ib_gid *mgid)
4634 {
4635 	unsigned char mc_map[MAX_ADDR_LEN];
4636 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4637 	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4638 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4639 
4640 	if (cma_any_addr(addr)) {
4641 		memset(mgid, 0, sizeof *mgid);
4642 	} else if ((addr->sa_family == AF_INET6) &&
4643 		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4644 								 0xFF10A01B)) {
4645 		/* IPv6 address is an SA assigned MGID. */
4646 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4647 	} else if (addr->sa_family == AF_IB) {
4648 		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4649 	} else if (addr->sa_family == AF_INET6) {
4650 		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4651 		if (id_priv->id.ps == RDMA_PS_UDP)
4652 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4653 		*mgid = *(union ib_gid *) (mc_map + 4);
4654 	} else {
4655 		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4656 		if (id_priv->id.ps == RDMA_PS_UDP)
4657 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4658 		*mgid = *(union ib_gid *) (mc_map + 4);
4659 	}
4660 }
4661 
4662 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4663 				 struct cma_multicast *mc)
4664 {
4665 	struct ib_sa_mcmember_rec rec;
4666 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4667 	ib_sa_comp_mask comp_mask;
4668 	int ret;
4669 
4670 	ib_addr_get_mgid(dev_addr, &rec.mgid);
4671 	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4672 				     &rec.mgid, &rec);
4673 	if (ret)
4674 		return ret;
4675 
4676 	ret = cma_set_qkey(id_priv, 0);
4677 	if (ret)
4678 		return ret;
4679 
4680 	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4681 	rec.qkey = cpu_to_be32(id_priv->qkey);
4682 	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4683 	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4684 	rec.join_state = mc->join_state;
4685 
4686 	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4687 		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4688 		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4689 		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
4690 		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4691 
4692 	if (id_priv->id.ps == RDMA_PS_IPOIB)
4693 		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4694 			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4695 			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4696 			     IB_SA_MCMEMBER_REC_MTU |
4697 			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
4698 
4699 	mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4700 					 id_priv->id.port_num, &rec, comp_mask,
4701 					 GFP_KERNEL, cma_ib_mc_handler, mc);
4702 	return PTR_ERR_OR_ZERO(mc->sa_mc);
4703 }
4704 
4705 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4706 			      enum ib_gid_type gid_type)
4707 {
4708 	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4709 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4710 
4711 	if (cma_any_addr(addr)) {
4712 		memset(mgid, 0, sizeof *mgid);
4713 	} else if (addr->sa_family == AF_INET6) {
4714 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4715 	} else {
4716 		mgid->raw[0] =
4717 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4718 		mgid->raw[1] =
4719 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4720 		mgid->raw[2] = 0;
4721 		mgid->raw[3] = 0;
4722 		mgid->raw[4] = 0;
4723 		mgid->raw[5] = 0;
4724 		mgid->raw[6] = 0;
4725 		mgid->raw[7] = 0;
4726 		mgid->raw[8] = 0;
4727 		mgid->raw[9] = 0;
4728 		mgid->raw[10] = 0xff;
4729 		mgid->raw[11] = 0xff;
4730 		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4731 	}
4732 }
4733 
4734 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4735 				   struct cma_multicast *mc)
4736 {
4737 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4738 	int err = 0;
4739 	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4740 	struct net_device *ndev = NULL;
4741 	struct ib_sa_multicast ib;
4742 	enum ib_gid_type gid_type;
4743 	bool send_only;
4744 
4745 	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4746 
4747 	if (cma_zero_addr(addr))
4748 		return -EINVAL;
4749 
4750 	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4751 		   rdma_start_port(id_priv->cma_dev->device)];
4752 	cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
4753 
4754 	ib.rec.pkey = cpu_to_be16(0xffff);
4755 	if (id_priv->id.ps == RDMA_PS_UDP)
4756 		ib.rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4757 
4758 	if (dev_addr->bound_dev_if)
4759 		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4760 	if (!ndev)
4761 		return -ENODEV;
4762 
4763 	ib.rec.rate = iboe_get_rate(ndev);
4764 	ib.rec.hop_limit = 1;
4765 	ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4766 
4767 	if (addr->sa_family == AF_INET) {
4768 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4769 			ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4770 			if (!send_only) {
4771 				err = cma_igmp_send(ndev, &ib.rec.mgid,
4772 						    true);
4773 			}
4774 		}
4775 	} else {
4776 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4777 			err = -ENOTSUPP;
4778 	}
4779 	dev_put(ndev);
4780 	if (err || !ib.rec.mtu)
4781 		return err ?: -EINVAL;
4782 
4783 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4784 		    &ib.rec.port_gid);
4785 	INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
4786 	cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
4787 	queue_work(cma_wq, &mc->iboe_join.work);
4788 	return 0;
4789 }
4790 
4791 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4792 			u8 join_state, void *context)
4793 {
4794 	struct rdma_id_private *id_priv =
4795 		container_of(id, struct rdma_id_private, id);
4796 	struct cma_multicast *mc;
4797 	int ret;
4798 
4799 	/* Not supported for kernel QPs */
4800 	if (WARN_ON(id->qp))
4801 		return -EINVAL;
4802 
4803 	/* ULP is calling this wrong. */
4804 	if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
4805 			    READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
4806 		return -EINVAL;
4807 
4808 	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
4809 	if (!mc)
4810 		return -ENOMEM;
4811 
4812 	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4813 	mc->context = context;
4814 	mc->id_priv = id_priv;
4815 	mc->join_state = join_state;
4816 
4817 	if (rdma_protocol_roce(id->device, id->port_num)) {
4818 		ret = cma_iboe_join_multicast(id_priv, mc);
4819 		if (ret)
4820 			goto out_err;
4821 	} else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4822 		ret = cma_join_ib_multicast(id_priv, mc);
4823 		if (ret)
4824 			goto out_err;
4825 	} else {
4826 		ret = -ENOSYS;
4827 		goto out_err;
4828 	}
4829 
4830 	spin_lock(&id_priv->lock);
4831 	list_add(&mc->list, &id_priv->mc_list);
4832 	spin_unlock(&id_priv->lock);
4833 
4834 	return 0;
4835 out_err:
4836 	kfree(mc);
4837 	return ret;
4838 }
4839 EXPORT_SYMBOL(rdma_join_multicast);
4840 
4841 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4842 {
4843 	struct rdma_id_private *id_priv;
4844 	struct cma_multicast *mc;
4845 
4846 	id_priv = container_of(id, struct rdma_id_private, id);
4847 	spin_lock_irq(&id_priv->lock);
4848 	list_for_each_entry(mc, &id_priv->mc_list, list) {
4849 		if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
4850 			continue;
4851 		list_del(&mc->list);
4852 		spin_unlock_irq(&id_priv->lock);
4853 
4854 		WARN_ON(id_priv->cma_dev->device != id->device);
4855 		destroy_mc(id_priv, mc);
4856 		return;
4857 	}
4858 	spin_unlock_irq(&id_priv->lock);
4859 }
4860 EXPORT_SYMBOL(rdma_leave_multicast);
4861 
4862 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4863 {
4864 	struct rdma_dev_addr *dev_addr;
4865 	struct cma_work *work;
4866 
4867 	dev_addr = &id_priv->id.route.addr.dev_addr;
4868 
4869 	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4870 	    (net_eq(dev_net(ndev), dev_addr->net)) &&
4871 	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4872 		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4873 			ndev->name, &id_priv->id);
4874 		work = kzalloc(sizeof *work, GFP_KERNEL);
4875 		if (!work)
4876 			return -ENOMEM;
4877 
4878 		INIT_WORK(&work->work, cma_work_handler);
4879 		work->id = id_priv;
4880 		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4881 		cma_id_get(id_priv);
4882 		queue_work(cma_wq, &work->work);
4883 	}
4884 
4885 	return 0;
4886 }
4887 
4888 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4889 			       void *ptr)
4890 {
4891 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4892 	struct cma_device *cma_dev;
4893 	struct rdma_id_private *id_priv;
4894 	int ret = NOTIFY_DONE;
4895 
4896 	if (event != NETDEV_BONDING_FAILOVER)
4897 		return NOTIFY_DONE;
4898 
4899 	if (!netif_is_bond_master(ndev))
4900 		return NOTIFY_DONE;
4901 
4902 	mutex_lock(&lock);
4903 	list_for_each_entry(cma_dev, &dev_list, list)
4904 		list_for_each_entry(id_priv, &cma_dev->id_list, device_item) {
4905 			ret = cma_netdev_change(ndev, id_priv);
4906 			if (ret)
4907 				goto out;
4908 		}
4909 
4910 out:
4911 	mutex_unlock(&lock);
4912 	return ret;
4913 }
4914 
4915 static struct notifier_block cma_nb = {
4916 	.notifier_call = cma_netdev_callback
4917 };
4918 
4919 static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
4920 {
4921 	struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
4922 	enum rdma_cm_state state;
4923 	unsigned long flags;
4924 
4925 	mutex_lock(&id_priv->handler_mutex);
4926 	/* Record that we want to remove the device */
4927 	spin_lock_irqsave(&id_priv->lock, flags);
4928 	state = id_priv->state;
4929 	if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
4930 		spin_unlock_irqrestore(&id_priv->lock, flags);
4931 		mutex_unlock(&id_priv->handler_mutex);
4932 		cma_id_put(id_priv);
4933 		return;
4934 	}
4935 	id_priv->state = RDMA_CM_DEVICE_REMOVAL;
4936 	spin_unlock_irqrestore(&id_priv->lock, flags);
4937 
4938 	if (cma_cm_event_handler(id_priv, &event)) {
4939 		/*
4940 		 * At this point the ULP promises it won't call
4941 		 * rdma_destroy_id() concurrently
4942 		 */
4943 		cma_id_put(id_priv);
4944 		mutex_unlock(&id_priv->handler_mutex);
4945 		trace_cm_id_destroy(id_priv);
4946 		_destroy_id(id_priv, state);
4947 		return;
4948 	}
4949 	mutex_unlock(&id_priv->handler_mutex);
4950 
4951 	/*
4952 	 * If this races with destroy then the thread that first assigns state
4953 	 * to a destroying does the cancel.
4954 	 */
4955 	cma_cancel_operation(id_priv, state);
4956 	cma_id_put(id_priv);
4957 }
4958 
4959 static void cma_process_remove(struct cma_device *cma_dev)
4960 {
4961 	mutex_lock(&lock);
4962 	while (!list_empty(&cma_dev->id_list)) {
4963 		struct rdma_id_private *id_priv = list_first_entry(
4964 			&cma_dev->id_list, struct rdma_id_private, device_item);
4965 
4966 		list_del_init(&id_priv->listen_item);
4967 		list_del_init(&id_priv->device_item);
4968 		cma_id_get(id_priv);
4969 		mutex_unlock(&lock);
4970 
4971 		cma_send_device_removal_put(id_priv);
4972 
4973 		mutex_lock(&lock);
4974 	}
4975 	mutex_unlock(&lock);
4976 
4977 	cma_dev_put(cma_dev);
4978 	wait_for_completion(&cma_dev->comp);
4979 }
4980 
4981 static bool cma_supported(struct ib_device *device)
4982 {
4983 	u32 i;
4984 
4985 	rdma_for_each_port(device, i) {
4986 		if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
4987 			return true;
4988 	}
4989 	return false;
4990 }
4991 
4992 static int cma_add_one(struct ib_device *device)
4993 {
4994 	struct rdma_id_private *to_destroy;
4995 	struct cma_device *cma_dev;
4996 	struct rdma_id_private *id_priv;
4997 	unsigned long supported_gids = 0;
4998 	int ret;
4999 	u32 i;
5000 
5001 	if (!cma_supported(device))
5002 		return -EOPNOTSUPP;
5003 
5004 	cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
5005 	if (!cma_dev)
5006 		return -ENOMEM;
5007 
5008 	cma_dev->device = device;
5009 	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
5010 					    sizeof(*cma_dev->default_gid_type),
5011 					    GFP_KERNEL);
5012 	if (!cma_dev->default_gid_type) {
5013 		ret = -ENOMEM;
5014 		goto free_cma_dev;
5015 	}
5016 
5017 	cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
5018 					    sizeof(*cma_dev->default_roce_tos),
5019 					    GFP_KERNEL);
5020 	if (!cma_dev->default_roce_tos) {
5021 		ret = -ENOMEM;
5022 		goto free_gid_type;
5023 	}
5024 
5025 	rdma_for_each_port (device, i) {
5026 		supported_gids = roce_gid_type_mask_support(device, i);
5027 		WARN_ON(!supported_gids);
5028 		if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
5029 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
5030 				CMA_PREFERRED_ROCE_GID_TYPE;
5031 		else
5032 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
5033 				find_first_bit(&supported_gids, BITS_PER_LONG);
5034 		cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
5035 	}
5036 
5037 	init_completion(&cma_dev->comp);
5038 	refcount_set(&cma_dev->refcount, 1);
5039 	INIT_LIST_HEAD(&cma_dev->id_list);
5040 	ib_set_client_data(device, &cma_client, cma_dev);
5041 
5042 	mutex_lock(&lock);
5043 	list_add_tail(&cma_dev->list, &dev_list);
5044 	list_for_each_entry(id_priv, &listen_any_list, listen_any_item) {
5045 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
5046 		if (ret)
5047 			goto free_listen;
5048 	}
5049 	mutex_unlock(&lock);
5050 
5051 	trace_cm_add_one(device);
5052 	return 0;
5053 
5054 free_listen:
5055 	list_del(&cma_dev->list);
5056 	mutex_unlock(&lock);
5057 
5058 	/* cma_process_remove() will delete to_destroy */
5059 	cma_process_remove(cma_dev);
5060 	kfree(cma_dev->default_roce_tos);
5061 free_gid_type:
5062 	kfree(cma_dev->default_gid_type);
5063 
5064 free_cma_dev:
5065 	kfree(cma_dev);
5066 	return ret;
5067 }
5068 
5069 static void cma_remove_one(struct ib_device *device, void *client_data)
5070 {
5071 	struct cma_device *cma_dev = client_data;
5072 
5073 	trace_cm_remove_one(device);
5074 
5075 	mutex_lock(&lock);
5076 	list_del(&cma_dev->list);
5077 	mutex_unlock(&lock);
5078 
5079 	cma_process_remove(cma_dev);
5080 	kfree(cma_dev->default_roce_tos);
5081 	kfree(cma_dev->default_gid_type);
5082 	kfree(cma_dev);
5083 }
5084 
5085 static int cma_init_net(struct net *net)
5086 {
5087 	struct cma_pernet *pernet = cma_pernet(net);
5088 
5089 	xa_init(&pernet->tcp_ps);
5090 	xa_init(&pernet->udp_ps);
5091 	xa_init(&pernet->ipoib_ps);
5092 	xa_init(&pernet->ib_ps);
5093 
5094 	return 0;
5095 }
5096 
5097 static void cma_exit_net(struct net *net)
5098 {
5099 	struct cma_pernet *pernet = cma_pernet(net);
5100 
5101 	WARN_ON(!xa_empty(&pernet->tcp_ps));
5102 	WARN_ON(!xa_empty(&pernet->udp_ps));
5103 	WARN_ON(!xa_empty(&pernet->ipoib_ps));
5104 	WARN_ON(!xa_empty(&pernet->ib_ps));
5105 }
5106 
5107 static struct pernet_operations cma_pernet_operations = {
5108 	.init = cma_init_net,
5109 	.exit = cma_exit_net,
5110 	.id = &cma_pernet_id,
5111 	.size = sizeof(struct cma_pernet),
5112 };
5113 
5114 static int __init cma_init(void)
5115 {
5116 	int ret;
5117 
5118 	/*
5119 	 * There is a rare lock ordering dependency in cma_netdev_callback()
5120 	 * that only happens when bonding is enabled. Teach lockdep that rtnl
5121 	 * must never be nested under lock so it can find these without having
5122 	 * to test with bonding.
5123 	 */
5124 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
5125 		rtnl_lock();
5126 		mutex_lock(&lock);
5127 		mutex_unlock(&lock);
5128 		rtnl_unlock();
5129 	}
5130 
5131 	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5132 	if (!cma_wq)
5133 		return -ENOMEM;
5134 
5135 	ret = register_pernet_subsys(&cma_pernet_operations);
5136 	if (ret)
5137 		goto err_wq;
5138 
5139 	ib_sa_register_client(&sa_client);
5140 	register_netdevice_notifier(&cma_nb);
5141 
5142 	ret = ib_register_client(&cma_client);
5143 	if (ret)
5144 		goto err;
5145 
5146 	ret = cma_configfs_init();
5147 	if (ret)
5148 		goto err_ib;
5149 
5150 	return 0;
5151 
5152 err_ib:
5153 	ib_unregister_client(&cma_client);
5154 err:
5155 	unregister_netdevice_notifier(&cma_nb);
5156 	ib_sa_unregister_client(&sa_client);
5157 	unregister_pernet_subsys(&cma_pernet_operations);
5158 err_wq:
5159 	destroy_workqueue(cma_wq);
5160 	return ret;
5161 }
5162 
5163 static void __exit cma_cleanup(void)
5164 {
5165 	cma_configfs_exit();
5166 	ib_unregister_client(&cma_client);
5167 	unregister_netdevice_notifier(&cma_nb);
5168 	ib_sa_unregister_client(&sa_client);
5169 	unregister_pernet_subsys(&cma_pernet_operations);
5170 	destroy_workqueue(cma_wq);
5171 }
5172 
5173 module_init(cma_init);
5174 module_exit(cma_cleanup);
5175