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