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