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