ib_cma.c revision cc6a289dfb2c2da42b990e8eed0f5dfc7f2a3f3f
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	dev_put(dst_dev);
1339
1340	CURVNET_SET(net_dev->if_vnet);
1341
1342	/*
1343	 * Make sure the socket address length field
1344	 * is set, else rtalloc1() will fail.
1345	 */
1346	src_tmp.sin6_len = sizeof(src_tmp);
1347
1348	/*
1349	 * Make sure the scope ID gets embedded, else rtalloc1() will
1350	 * resolve to the loopback interface.
1351	 */
1352	src_tmp.sin6_scope_id = net_dev->if_index;
1353	sa6_embedscope(&src_tmp, 0);
1354
1355	dst_tmp.sin6_scope_id = net_dev->if_index;
1356	sa6_embedscope(&dst_tmp, 0);
1357
1358	/*
1359	 * Check for loopback after scope ID
1360	 * has been embedded:
1361	 */
1362	if (memcmp(&src_tmp.sin6_addr, &dst_tmp.sin6_addr,
1363	    sizeof(dst_tmp.sin6_addr)) == 0) {
1364		ret = true;
1365	} else {
1366		/* non-loopback case */
1367		rte = rtalloc1((struct sockaddr *)&src_tmp, 1, 0);
1368		if (rte != NULL) {
1369			ret = (rte->rt_ifp == net_dev);
1370			RTFREE_LOCKED(rte);
1371		} else {
1372			ret = false;
1373		}
1374	}
1375	CURVNET_RESTORE();
1376	return ret;
1377#else
1378	return false;
1379#endif
1380}
1381
1382static bool validate_net_dev(struct net_device *net_dev,
1383			     const struct sockaddr *daddr,
1384			     const struct sockaddr *saddr)
1385{
1386	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1387	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1388	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1389	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1390
1391	switch (daddr->sa_family) {
1392	case AF_INET:
1393		return saddr->sa_family == AF_INET &&
1394		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1395
1396	case AF_INET6:
1397		return saddr->sa_family == AF_INET6 &&
1398		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1399
1400	default:
1401		return false;
1402	}
1403}
1404
1405static struct net_device *
1406roce_get_net_dev_by_cm_event(struct ib_device *device, u8 port_num,
1407    const struct ib_cm_event *ib_event)
1408{
1409	struct ib_gid_attr sgid_attr;
1410	union ib_gid sgid;
1411	int err = -EINVAL;
1412
1413	if (ib_event->event == IB_CM_REQ_RECEIVED) {
1414		err = ib_get_cached_gid(device, port_num,
1415		    ib_event->param.req_rcvd.ppath_sgid_index, &sgid, &sgid_attr);
1416	} else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
1417		err = ib_get_cached_gid(device, port_num,
1418		    ib_event->param.sidr_req_rcvd.sgid_index, &sgid, &sgid_attr);
1419	}
1420	if (err)
1421		return (NULL);
1422	return (sgid_attr.ndev);
1423}
1424
1425static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1426					  const struct cma_req_info *req)
1427{
1428	struct sockaddr_storage listen_addr_storage, src_addr_storage;
1429	struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1430			*src_addr = (struct sockaddr *)&src_addr_storage;
1431	struct net_device *net_dev;
1432	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1433	int err;
1434
1435	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1436			       req->service_id);
1437	if (err)
1438		return ERR_PTR(err);
1439
1440	if (rdma_protocol_roce(req->device, req->port)) {
1441		net_dev = roce_get_net_dev_by_cm_event(req->device, req->port,
1442						       ib_event);
1443	} else {
1444		net_dev = ib_get_net_dev_by_params(req->device, req->port,
1445						   req->pkey,
1446						   gid, listen_addr);
1447	}
1448	if (!net_dev)
1449		return ERR_PTR(-ENODEV);
1450
1451	if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1452		dev_put(net_dev);
1453		return ERR_PTR(-EHOSTUNREACH);
1454	}
1455
1456	return net_dev;
1457}
1458
1459static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1460{
1461	return (be64_to_cpu(service_id) >> 16) & 0xffff;
1462}
1463
1464static bool cma_match_private_data(struct rdma_id_private *id_priv,
1465				   const struct cma_hdr *hdr)
1466{
1467	struct sockaddr *addr = cma_src_addr(id_priv);
1468	__be32 ip4_addr;
1469	struct in6_addr ip6_addr;
1470
1471	if (cma_any_addr(addr) && !id_priv->afonly)
1472		return true;
1473
1474	switch (addr->sa_family) {
1475	case AF_INET:
1476		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1477		if (cma_get_ip_ver(hdr) != 4)
1478			return false;
1479		if (!cma_any_addr(addr) &&
1480		    hdr->dst_addr.ip4.addr != ip4_addr)
1481			return false;
1482		break;
1483	case AF_INET6:
1484		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1485		if (cma_get_ip_ver(hdr) != 6)
1486			return false;
1487		cma_ip6_clear_scope_id(&ip6_addr);
1488		if (!cma_any_addr(addr) &&
1489		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1490			return false;
1491		break;
1492	case AF_IB:
1493		return true;
1494	default:
1495		return false;
1496	}
1497
1498	return true;
1499}
1500
1501static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1502{
1503	enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1504	enum rdma_transport_type transport =
1505		rdma_node_get_transport(device->node_type);
1506
1507	return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1508}
1509
1510static bool cma_protocol_roce(const struct rdma_cm_id *id)
1511{
1512	struct ib_device *device = id->device;
1513	const int port_num = id->port_num ?: rdma_start_port(device);
1514
1515	return cma_protocol_roce_dev_port(device, port_num);
1516}
1517
1518static bool cma_match_net_dev(const struct rdma_cm_id *id,
1519			      const struct net_device *net_dev,
1520			      u8 port_num)
1521{
1522	const struct rdma_addr *addr = &id->route.addr;
1523
1524	if (!net_dev)
1525		/* This request is an AF_IB request or a RoCE request */
1526		return (!id->port_num || id->port_num == port_num) &&
1527		       (addr->src_addr.ss_family == AF_IB ||
1528			cma_protocol_roce_dev_port(id->device, port_num));
1529
1530	return !addr->dev_addr.bound_dev_if ||
1531	       (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1532		addr->dev_addr.bound_dev_if == net_dev->if_index);
1533}
1534
1535static struct rdma_id_private *cma_find_listener(
1536		const struct rdma_bind_list *bind_list,
1537		const struct ib_cm_id *cm_id,
1538		const struct ib_cm_event *ib_event,
1539		const struct cma_req_info *req,
1540		const struct net_device *net_dev)
1541{
1542	struct rdma_id_private *id_priv, *id_priv_dev;
1543
1544	if (!bind_list)
1545		return ERR_PTR(-EINVAL);
1546
1547	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1548		if (cma_match_private_data(id_priv, ib_event->private_data)) {
1549			if (id_priv->id.device == cm_id->device &&
1550			    cma_match_net_dev(&id_priv->id, net_dev, req->port))
1551				return id_priv;
1552			list_for_each_entry(id_priv_dev,
1553					    &id_priv->listen_list,
1554					    listen_list) {
1555				if (id_priv_dev->id.device == cm_id->device &&
1556				    cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
1557					return id_priv_dev;
1558			}
1559		}
1560	}
1561
1562	return ERR_PTR(-EINVAL);
1563}
1564
1565static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
1566						 struct ib_cm_event *ib_event,
1567						 struct net_device **net_dev)
1568{
1569	struct cma_req_info req;
1570	struct rdma_bind_list *bind_list;
1571	struct rdma_id_private *id_priv;
1572	int err;
1573
1574	err = cma_save_req_info(ib_event, &req);
1575	if (err)
1576		return ERR_PTR(err);
1577
1578	*net_dev = cma_get_net_dev(ib_event, &req);
1579	if (IS_ERR(*net_dev)) {
1580		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1581			/* Assuming the protocol is AF_IB */
1582			*net_dev = NULL;
1583		} else {
1584			return ERR_CAST(*net_dev);
1585		}
1586	}
1587
1588	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1589				rdma_ps_from_service_id(req.service_id),
1590				cma_port_from_service_id(req.service_id));
1591	id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
1592	if (IS_ERR(id_priv) && *net_dev) {
1593		dev_put(*net_dev);
1594		*net_dev = NULL;
1595	}
1596
1597	return id_priv;
1598}
1599
1600static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
1601{
1602	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1603}
1604
1605static void cma_cancel_route(struct rdma_id_private *id_priv)
1606{
1607	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1608		if (id_priv->query)
1609			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1610	}
1611}
1612
1613static void cma_cancel_listens(struct rdma_id_private *id_priv)
1614{
1615	struct rdma_id_private *dev_id_priv;
1616
1617	/*
1618	 * Remove from listen_any_list to prevent added devices from spawning
1619	 * additional listen requests.
1620	 */
1621	mutex_lock(&lock);
1622	list_del(&id_priv->list);
1623
1624	while (!list_empty(&id_priv->listen_list)) {
1625		dev_id_priv = list_entry(id_priv->listen_list.next,
1626					 struct rdma_id_private, listen_list);
1627		/* sync with device removal to avoid duplicate destruction */
1628		list_del_init(&dev_id_priv->list);
1629		list_del(&dev_id_priv->listen_list);
1630		mutex_unlock(&lock);
1631
1632		rdma_destroy_id(&dev_id_priv->id);
1633		mutex_lock(&lock);
1634	}
1635	mutex_unlock(&lock);
1636}
1637
1638static void cma_cancel_operation(struct rdma_id_private *id_priv,
1639				 enum rdma_cm_state state)
1640{
1641	switch (state) {
1642	case RDMA_CM_ADDR_QUERY:
1643		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1644		break;
1645	case RDMA_CM_ROUTE_QUERY:
1646		cma_cancel_route(id_priv);
1647		break;
1648	case RDMA_CM_LISTEN:
1649		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1650			cma_cancel_listens(id_priv);
1651		break;
1652	default:
1653		break;
1654	}
1655}
1656
1657static void cma_release_port(struct rdma_id_private *id_priv)
1658{
1659	struct rdma_bind_list *bind_list = id_priv->bind_list;
1660	struct vnet *net = id_priv->id.route.addr.dev_addr.net;
1661
1662	if (!bind_list)
1663		return;
1664
1665	mutex_lock(&lock);
1666	hlist_del(&id_priv->node);
1667	if (hlist_empty(&bind_list->owners)) {
1668		cma_ps_remove(net, bind_list->ps, bind_list->port);
1669		kfree(bind_list);
1670	}
1671	mutex_unlock(&lock);
1672}
1673
1674static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1675{
1676	struct cma_multicast *mc;
1677
1678	while (!list_empty(&id_priv->mc_list)) {
1679		mc = container_of(id_priv->mc_list.next,
1680				  struct cma_multicast, list);
1681		list_del(&mc->list);
1682		if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
1683				      id_priv->id.port_num)) {
1684			ib_sa_free_multicast(mc->multicast.ib);
1685			kfree(mc);
1686		} else {
1687			if (mc->igmp_joined) {
1688				struct rdma_dev_addr *dev_addr =
1689					&id_priv->id.route.addr.dev_addr;
1690				struct net_device *ndev = NULL;
1691
1692				if (dev_addr->bound_dev_if)
1693					ndev = dev_get_by_index(dev_addr->net,
1694								dev_addr->bound_dev_if);
1695				if (ndev) {
1696					cma_igmp_send(ndev,
1697						      &mc->multicast.ib->rec.mgid,
1698						      false);
1699					dev_put(ndev);
1700				}
1701			}
1702			kref_put(&mc->mcref, release_mc);
1703		}
1704	}
1705}
1706
1707void rdma_destroy_id(struct rdma_cm_id *id)
1708{
1709	struct rdma_id_private *id_priv;
1710	enum rdma_cm_state state;
1711
1712	id_priv = container_of(id, struct rdma_id_private, id);
1713	state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1714	cma_cancel_operation(id_priv, state);
1715
1716	/*
1717	 * Wait for any active callback to finish.  New callbacks will find
1718	 * the id_priv state set to destroying and abort.
1719	 */
1720	mutex_lock(&id_priv->handler_mutex);
1721	mutex_unlock(&id_priv->handler_mutex);
1722
1723	if (id_priv->cma_dev) {
1724		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1725			if (id_priv->cm_id.ib)
1726				ib_destroy_cm_id(id_priv->cm_id.ib);
1727		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1728			if (id_priv->cm_id.iw)
1729				iw_destroy_cm_id(id_priv->cm_id.iw);
1730		}
1731		cma_leave_mc_groups(id_priv);
1732		cma_release_dev(id_priv);
1733	}
1734
1735	cma_release_port(id_priv);
1736	cma_deref_id(id_priv);
1737	wait_for_completion(&id_priv->comp);
1738
1739	if (id_priv->internal_id)
1740		cma_deref_id(id_priv->id.context);
1741
1742	kfree(id_priv->id.route.path_rec);
1743	kfree(id_priv);
1744}
1745EXPORT_SYMBOL(rdma_destroy_id);
1746
1747static int cma_rep_recv(struct rdma_id_private *id_priv)
1748{
1749	int ret;
1750
1751	ret = cma_modify_qp_rtr(id_priv, NULL);
1752	if (ret)
1753		goto reject;
1754
1755	ret = cma_modify_qp_rts(id_priv, NULL);
1756	if (ret)
1757		goto reject;
1758
1759	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1760	if (ret)
1761		goto reject;
1762
1763	return 0;
1764reject:
1765	cma_modify_qp_err(id_priv);
1766	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1767		       NULL, 0, NULL, 0);
1768	return ret;
1769}
1770
1771static void cma_set_rep_event_data(struct rdma_cm_event *event,
1772				   struct ib_cm_rep_event_param *rep_data,
1773				   void *private_data)
1774{
1775	event->param.conn.private_data = private_data;
1776	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1777	event->param.conn.responder_resources = rep_data->responder_resources;
1778	event->param.conn.initiator_depth = rep_data->initiator_depth;
1779	event->param.conn.flow_control = rep_data->flow_control;
1780	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1781	event->param.conn.srq = rep_data->srq;
1782	event->param.conn.qp_num = rep_data->remote_qpn;
1783}
1784
1785static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1786{
1787	struct rdma_id_private *id_priv = cm_id->context;
1788	struct rdma_cm_event event;
1789	int ret = 0;
1790
1791	mutex_lock(&id_priv->handler_mutex);
1792	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1793	     id_priv->state != RDMA_CM_CONNECT) ||
1794	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1795	     id_priv->state != RDMA_CM_DISCONNECT))
1796		goto out;
1797
1798	memset(&event, 0, sizeof event);
1799	switch (ib_event->event) {
1800	case IB_CM_REQ_ERROR:
1801	case IB_CM_REP_ERROR:
1802		event.event = RDMA_CM_EVENT_UNREACHABLE;
1803		event.status = -ETIMEDOUT;
1804		break;
1805	case IB_CM_REP_RECEIVED:
1806		if (id_priv->id.qp) {
1807			event.status = cma_rep_recv(id_priv);
1808			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1809						     RDMA_CM_EVENT_ESTABLISHED;
1810		} else {
1811			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1812		}
1813		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1814				       ib_event->private_data);
1815		break;
1816	case IB_CM_RTU_RECEIVED:
1817	case IB_CM_USER_ESTABLISHED:
1818		event.event = RDMA_CM_EVENT_ESTABLISHED;
1819		break;
1820	case IB_CM_DREQ_ERROR:
1821		event.status = -ETIMEDOUT; /* fall through */
1822	case IB_CM_DREQ_RECEIVED:
1823	case IB_CM_DREP_RECEIVED:
1824		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1825				   RDMA_CM_DISCONNECT))
1826			goto out;
1827		event.event = RDMA_CM_EVENT_DISCONNECTED;
1828		break;
1829	case IB_CM_TIMEWAIT_EXIT:
1830		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1831		break;
1832	case IB_CM_MRA_RECEIVED:
1833		/* ignore event */
1834		goto out;
1835	case IB_CM_REJ_RECEIVED:
1836		cma_modify_qp_err(id_priv);
1837		event.status = ib_event->param.rej_rcvd.reason;
1838		event.event = RDMA_CM_EVENT_REJECTED;
1839		event.param.conn.private_data = ib_event->private_data;
1840		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1841		break;
1842	default:
1843		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1844		       ib_event->event);
1845		goto out;
1846	}
1847
1848	ret = id_priv->id.event_handler(&id_priv->id, &event);
1849	if (ret) {
1850		/* Destroy the CM ID by returning a non-zero value. */
1851		id_priv->cm_id.ib = NULL;
1852		cma_exch(id_priv, RDMA_CM_DESTROYING);
1853		mutex_unlock(&id_priv->handler_mutex);
1854		rdma_destroy_id(&id_priv->id);
1855		return ret;
1856	}
1857out:
1858	mutex_unlock(&id_priv->handler_mutex);
1859	return ret;
1860}
1861
1862static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1863					       struct ib_cm_event *ib_event,
1864					       struct net_device *net_dev)
1865{
1866	struct rdma_id_private *id_priv;
1867	struct rdma_cm_id *id;
1868	struct rdma_route *rt;
1869	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1870	const __be64 service_id =
1871		      ib_event->param.req_rcvd.primary_path->service_id;
1872	int ret;
1873
1874	id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1875			    listen_id->event_handler, listen_id->context,
1876			    listen_id->ps, ib_event->param.req_rcvd.qp_type);
1877	if (IS_ERR(id))
1878		return NULL;
1879
1880	id_priv = container_of(id, struct rdma_id_private, id);
1881	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1882			      (struct sockaddr *)&id->route.addr.dst_addr,
1883			      listen_id, ib_event, ss_family, service_id))
1884		goto err;
1885
1886	rt = &id->route;
1887	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1888	rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1889			       GFP_KERNEL);
1890	if (!rt->path_rec)
1891		goto err;
1892
1893	rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1894	if (rt->num_paths == 2)
1895		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1896
1897	if (net_dev) {
1898		ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
1899		if (ret)
1900			goto err;
1901	} else {
1902		if (!cma_protocol_roce(listen_id) &&
1903		    cma_any_addr(cma_src_addr(id_priv))) {
1904			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1905			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1906			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1907		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
1908			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1909			if (ret)
1910				goto err;
1911		}
1912	}
1913	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1914
1915	id_priv->state = RDMA_CM_CONNECT;
1916	return id_priv;
1917
1918err:
1919	rdma_destroy_id(id);
1920	return NULL;
1921}
1922
1923static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1924					      struct ib_cm_event *ib_event,
1925					      struct net_device *net_dev)
1926{
1927	struct rdma_id_private *id_priv;
1928	struct rdma_cm_id *id;
1929	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1930	struct vnet *net = listen_id->route.addr.dev_addr.net;
1931	int ret;
1932
1933	id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
1934			    listen_id->ps, IB_QPT_UD);
1935	if (IS_ERR(id))
1936		return NULL;
1937
1938	id_priv = container_of(id, struct rdma_id_private, id);
1939	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1940			      (struct sockaddr *)&id->route.addr.dst_addr,
1941			      listen_id, ib_event, ss_family,
1942			      ib_event->param.sidr_req_rcvd.service_id))
1943		goto err;
1944
1945	if (net_dev) {
1946		ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
1947		if (ret)
1948			goto err;
1949	} else {
1950		if (!cma_any_addr(cma_src_addr(id_priv))) {
1951			ret = cma_translate_addr(cma_src_addr(id_priv),
1952						 &id->route.addr.dev_addr);
1953			if (ret)
1954				goto err;
1955		}
1956	}
1957
1958	id_priv->state = RDMA_CM_CONNECT;
1959	return id_priv;
1960err:
1961	rdma_destroy_id(id);
1962	return NULL;
1963}
1964
1965static void cma_set_req_event_data(struct rdma_cm_event *event,
1966				   struct ib_cm_req_event_param *req_data,
1967				   void *private_data, int offset)
1968{
1969	event->param.conn.private_data = (char *)private_data + offset;
1970	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1971	event->param.conn.responder_resources = req_data->responder_resources;
1972	event->param.conn.initiator_depth = req_data->initiator_depth;
1973	event->param.conn.flow_control = req_data->flow_control;
1974	event->param.conn.retry_count = req_data->retry_count;
1975	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1976	event->param.conn.srq = req_data->srq;
1977	event->param.conn.qp_num = req_data->remote_qpn;
1978}
1979
1980static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1981{
1982	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
1983		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1984		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1985		 (id->qp_type == IB_QPT_UD)) ||
1986		(!id->qp_type));
1987}
1988
1989static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1990{
1991	struct rdma_id_private *listen_id, *conn_id = NULL;
1992	struct rdma_cm_event event;
1993	struct net_device *net_dev;
1994	int offset, ret;
1995
1996	listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
1997	if (IS_ERR(listen_id))
1998		return PTR_ERR(listen_id);
1999
2000	if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
2001		ret = -EINVAL;
2002		goto net_dev_put;
2003	}
2004
2005	mutex_lock(&listen_id->handler_mutex);
2006	if (listen_id->state != RDMA_CM_LISTEN) {
2007		ret = -ECONNABORTED;
2008		goto err1;
2009	}
2010
2011	memset(&event, 0, sizeof event);
2012	offset = cma_user_data_offset(listen_id);
2013	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2014	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2015		conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
2016		event.param.ud.private_data = (char *)ib_event->private_data + offset;
2017		event.param.ud.private_data_len =
2018				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2019	} else {
2020		conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
2021		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2022				       ib_event->private_data, offset);
2023	}
2024	if (!conn_id) {
2025		ret = -ENOMEM;
2026		goto err1;
2027	}
2028
2029	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2030	ret = cma_acquire_dev(conn_id, listen_id);
2031	if (ret)
2032		goto err2;
2033
2034	conn_id->cm_id.ib = cm_id;
2035	cm_id->context = conn_id;
2036	cm_id->cm_handler = cma_ib_handler;
2037
2038	/*
2039	 * Protect against the user destroying conn_id from another thread
2040	 * until we're done accessing it.
2041	 */
2042	atomic_inc(&conn_id->refcount);
2043	ret = conn_id->id.event_handler(&conn_id->id, &event);
2044	if (ret)
2045		goto err3;
2046	/*
2047	 * Acquire mutex to prevent user executing rdma_destroy_id()
2048	 * while we're accessing the cm_id.
2049	 */
2050	mutex_lock(&lock);
2051	if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
2052	    (conn_id->id.qp_type != IB_QPT_UD))
2053		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2054	mutex_unlock(&lock);
2055	mutex_unlock(&conn_id->handler_mutex);
2056	mutex_unlock(&listen_id->handler_mutex);
2057	cma_deref_id(conn_id);
2058	if (net_dev)
2059		dev_put(net_dev);
2060	return 0;
2061
2062err3:
2063	cma_deref_id(conn_id);
2064	/* Destroy the CM ID by returning a non-zero value. */
2065	conn_id->cm_id.ib = NULL;
2066err2:
2067	cma_exch(conn_id, RDMA_CM_DESTROYING);
2068	mutex_unlock(&conn_id->handler_mutex);
2069err1:
2070	mutex_unlock(&listen_id->handler_mutex);
2071	if (conn_id)
2072		rdma_destroy_id(&conn_id->id);
2073
2074net_dev_put:
2075	if (net_dev)
2076		dev_put(net_dev);
2077
2078	return ret;
2079}
2080
2081__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2082{
2083	if (addr->sa_family == AF_IB)
2084		return ((struct sockaddr_ib *) addr)->sib_sid;
2085
2086	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2087}
2088EXPORT_SYMBOL(rdma_get_service_id);
2089
2090static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2091{
2092	struct rdma_id_private *id_priv = iw_id->context;
2093	struct rdma_cm_event event;
2094	int ret = 0;
2095	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2096	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2097
2098	mutex_lock(&id_priv->handler_mutex);
2099	if (id_priv->state != RDMA_CM_CONNECT)
2100		goto out;
2101
2102	memset(&event, 0, sizeof event);
2103	switch (iw_event->event) {
2104	case IW_CM_EVENT_CLOSE:
2105		event.event = RDMA_CM_EVENT_DISCONNECTED;
2106		break;
2107	case IW_CM_EVENT_CONNECT_REPLY:
2108		memcpy(cma_src_addr(id_priv), laddr,
2109		       rdma_addr_size(laddr));
2110		memcpy(cma_dst_addr(id_priv), raddr,
2111		       rdma_addr_size(raddr));
2112		switch (iw_event->status) {
2113		case 0:
2114			event.event = RDMA_CM_EVENT_ESTABLISHED;
2115			event.param.conn.initiator_depth = iw_event->ird;
2116			event.param.conn.responder_resources = iw_event->ord;
2117			break;
2118		case -ECONNRESET:
2119		case -ECONNREFUSED:
2120			event.event = RDMA_CM_EVENT_REJECTED;
2121			break;
2122		case -ETIMEDOUT:
2123			event.event = RDMA_CM_EVENT_UNREACHABLE;
2124			break;
2125		default:
2126			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2127			break;
2128		}
2129		break;
2130	case IW_CM_EVENT_ESTABLISHED:
2131		event.event = RDMA_CM_EVENT_ESTABLISHED;
2132		event.param.conn.initiator_depth = iw_event->ird;
2133		event.param.conn.responder_resources = iw_event->ord;
2134		break;
2135	default:
2136		BUG_ON(1);
2137	}
2138
2139	event.status = iw_event->status;
2140	event.param.conn.private_data = iw_event->private_data;
2141	event.param.conn.private_data_len = iw_event->private_data_len;
2142	ret = id_priv->id.event_handler(&id_priv->id, &event);
2143	if (ret) {
2144		/* Destroy the CM ID by returning a non-zero value. */
2145		id_priv->cm_id.iw = NULL;
2146		cma_exch(id_priv, RDMA_CM_DESTROYING);
2147		mutex_unlock(&id_priv->handler_mutex);
2148		rdma_destroy_id(&id_priv->id);
2149		return ret;
2150	}
2151
2152out:
2153	mutex_unlock(&id_priv->handler_mutex);
2154	return ret;
2155}
2156
2157static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2158			       struct iw_cm_event *iw_event)
2159{
2160	struct rdma_cm_id *new_cm_id;
2161	struct rdma_id_private *listen_id, *conn_id;
2162	struct rdma_cm_event event;
2163	int ret = -ECONNABORTED;
2164	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2165	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2166
2167	listen_id = cm_id->context;
2168
2169	mutex_lock(&listen_id->handler_mutex);
2170	if (listen_id->state != RDMA_CM_LISTEN)
2171		goto out;
2172
2173	/* Create a new RDMA id for the new IW CM ID */
2174	new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2175				   listen_id->id.event_handler,
2176				   listen_id->id.context,
2177				   RDMA_PS_TCP, IB_QPT_RC);
2178	if (IS_ERR(new_cm_id)) {
2179		ret = -ENOMEM;
2180		goto out;
2181	}
2182	conn_id = container_of(new_cm_id, struct rdma_id_private, id);
2183	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2184	conn_id->state = RDMA_CM_CONNECT;
2185
2186	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2187	if (ret) {
2188		mutex_unlock(&conn_id->handler_mutex);
2189		rdma_destroy_id(new_cm_id);
2190		goto out;
2191	}
2192
2193	ret = cma_acquire_dev(conn_id, listen_id);
2194	if (ret) {
2195		mutex_unlock(&conn_id->handler_mutex);
2196		rdma_destroy_id(new_cm_id);
2197		goto out;
2198	}
2199
2200	conn_id->cm_id.iw = cm_id;
2201	cm_id->context = conn_id;
2202	cm_id->cm_handler = cma_iw_handler;
2203
2204	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2205	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2206
2207	memset(&event, 0, sizeof event);
2208	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2209	event.param.conn.private_data = iw_event->private_data;
2210	event.param.conn.private_data_len = iw_event->private_data_len;
2211	event.param.conn.initiator_depth = iw_event->ird;
2212	event.param.conn.responder_resources = iw_event->ord;
2213
2214	/*
2215	 * Protect against the user destroying conn_id from another thread
2216	 * until we're done accessing it.
2217	 */
2218	atomic_inc(&conn_id->refcount);
2219	ret = conn_id->id.event_handler(&conn_id->id, &event);
2220	if (ret) {
2221		/* User wants to destroy the CM ID */
2222		conn_id->cm_id.iw = NULL;
2223		cma_exch(conn_id, RDMA_CM_DESTROYING);
2224		mutex_unlock(&conn_id->handler_mutex);
2225		cma_deref_id(conn_id);
2226		rdma_destroy_id(&conn_id->id);
2227		goto out;
2228	}
2229
2230	mutex_unlock(&conn_id->handler_mutex);
2231	cma_deref_id(conn_id);
2232
2233out:
2234	mutex_unlock(&listen_id->handler_mutex);
2235	return ret;
2236}
2237
2238static int cma_ib_listen(struct rdma_id_private *id_priv)
2239{
2240	struct sockaddr *addr;
2241	struct ib_cm_id	*id;
2242	__be64 svc_id;
2243
2244	addr = cma_src_addr(id_priv);
2245	svc_id = rdma_get_service_id(&id_priv->id, addr);
2246	id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2247	if (IS_ERR(id))
2248		return PTR_ERR(id);
2249	id_priv->cm_id.ib = id;
2250
2251	return 0;
2252}
2253
2254static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2255{
2256	int ret;
2257	struct iw_cm_id	*id;
2258
2259	id = iw_create_cm_id(id_priv->id.device,
2260			     iw_conn_req_handler,
2261			     id_priv);
2262	if (IS_ERR(id))
2263		return PTR_ERR(id);
2264
2265	id->tos = id_priv->tos;
2266	id_priv->cm_id.iw = id;
2267
2268	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2269	       rdma_addr_size(cma_src_addr(id_priv)));
2270
2271	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2272
2273	if (ret) {
2274		iw_destroy_cm_id(id_priv->cm_id.iw);
2275		id_priv->cm_id.iw = NULL;
2276	}
2277
2278	return ret;
2279}
2280
2281static int cma_listen_handler(struct rdma_cm_id *id,
2282			      struct rdma_cm_event *event)
2283{
2284	struct rdma_id_private *id_priv = id->context;
2285
2286	id->context = id_priv->id.context;
2287	id->event_handler = id_priv->id.event_handler;
2288	return id_priv->id.event_handler(id, event);
2289}
2290
2291static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2292			      struct cma_device *cma_dev)
2293{
2294	struct rdma_id_private *dev_id_priv;
2295	struct rdma_cm_id *id;
2296	struct vnet *net = id_priv->id.route.addr.dev_addr.net;
2297	int ret;
2298
2299	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2300		return;
2301
2302	id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
2303			    id_priv->id.qp_type);
2304	if (IS_ERR(id))
2305		return;
2306
2307	dev_id_priv = container_of(id, struct rdma_id_private, id);
2308
2309	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2310	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2311	       rdma_addr_size(cma_src_addr(id_priv)));
2312
2313	_cma_attach_to_dev(dev_id_priv, cma_dev);
2314	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2315	atomic_inc(&id_priv->refcount);
2316	dev_id_priv->internal_id = 1;
2317	dev_id_priv->afonly = id_priv->afonly;
2318
2319	ret = rdma_listen(id, id_priv->backlog);
2320	if (ret)
2321		pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2322			ret, cma_dev->device->name);
2323}
2324
2325static void cma_listen_on_all(struct rdma_id_private *id_priv)
2326{
2327	struct cma_device *cma_dev;
2328
2329	mutex_lock(&lock);
2330	list_add_tail(&id_priv->list, &listen_any_list);
2331	list_for_each_entry(cma_dev, &dev_list, list)
2332		cma_listen_on_dev(id_priv, cma_dev);
2333	mutex_unlock(&lock);
2334}
2335
2336void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2337{
2338	struct rdma_id_private *id_priv;
2339
2340	id_priv = container_of(id, struct rdma_id_private, id);
2341	id_priv->tos = (u8) tos;
2342}
2343EXPORT_SYMBOL(rdma_set_service_type);
2344
2345static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2346			      void *context)
2347{
2348	struct cma_work *work = context;
2349	struct rdma_route *route;
2350
2351	route = &work->id->id.route;
2352
2353	if (!status) {
2354		route->num_paths = 1;
2355		*route->path_rec = *path_rec;
2356	} else {
2357		work->old_state = RDMA_CM_ROUTE_QUERY;
2358		work->new_state = RDMA_CM_ADDR_RESOLVED;
2359		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2360		work->event.status = status;
2361	}
2362
2363	queue_work(cma_wq, &work->work);
2364}
2365
2366static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2367			      struct cma_work *work)
2368{
2369	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2370	struct ib_sa_path_rec path_rec;
2371	ib_sa_comp_mask comp_mask;
2372	struct sockaddr_in6 *sin6;
2373	struct sockaddr_ib *sib;
2374
2375	memset(&path_rec, 0, sizeof path_rec);
2376	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2377	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2378	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2379	path_rec.numb_path = 1;
2380	path_rec.reversible = 1;
2381	path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
2382
2383	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2384		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2385		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2386
2387	switch (cma_family(id_priv)) {
2388	case AF_INET:
2389		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2390		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2391		break;
2392	case AF_INET6:
2393		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2394		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2395		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2396		break;
2397	case AF_IB:
2398		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2399		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2400		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2401		break;
2402	}
2403
2404	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2405					       id_priv->id.port_num, &path_rec,
2406					       comp_mask, timeout_ms,
2407					       GFP_KERNEL, cma_query_handler,
2408					       work, &id_priv->query);
2409
2410	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2411}
2412
2413static void cma_work_handler(struct work_struct *_work)
2414{
2415	struct cma_work *work = container_of(_work, struct cma_work, work);
2416	struct rdma_id_private *id_priv = work->id;
2417	int destroy = 0;
2418
2419	mutex_lock(&id_priv->handler_mutex);
2420	if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2421		goto out;
2422
2423	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2424		cma_exch(id_priv, RDMA_CM_DESTROYING);
2425		destroy = 1;
2426	}
2427out:
2428	mutex_unlock(&id_priv->handler_mutex);
2429	cma_deref_id(id_priv);
2430	if (destroy)
2431		rdma_destroy_id(&id_priv->id);
2432	kfree(work);
2433}
2434
2435static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2436{
2437	struct rdma_route *route = &id_priv->id.route;
2438	struct cma_work *work;
2439	int ret;
2440
2441	work = kzalloc(sizeof *work, GFP_KERNEL);
2442	if (!work)
2443		return -ENOMEM;
2444
2445	work->id = id_priv;
2446	INIT_WORK(&work->work, cma_work_handler);
2447	work->old_state = RDMA_CM_ROUTE_QUERY;
2448	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2449	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2450
2451	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2452	if (!route->path_rec) {
2453		ret = -ENOMEM;
2454		goto err1;
2455	}
2456
2457	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2458	if (ret)
2459		goto err2;
2460
2461	return 0;
2462err2:
2463	kfree(route->path_rec);
2464	route->path_rec = NULL;
2465err1:
2466	kfree(work);
2467	return ret;
2468}
2469
2470int rdma_set_ib_paths(struct rdma_cm_id *id,
2471		      struct ib_sa_path_rec *path_rec, int num_paths)
2472{
2473	struct rdma_id_private *id_priv;
2474	int ret;
2475
2476	id_priv = container_of(id, struct rdma_id_private, id);
2477	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2478			   RDMA_CM_ROUTE_RESOLVED))
2479		return -EINVAL;
2480
2481	id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2482				     GFP_KERNEL);
2483	if (!id->route.path_rec) {
2484		ret = -ENOMEM;
2485		goto err;
2486	}
2487
2488	id->route.num_paths = num_paths;
2489	return 0;
2490err:
2491	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2492	return ret;
2493}
2494EXPORT_SYMBOL(rdma_set_ib_paths);
2495
2496static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2497{
2498	struct cma_work *work;
2499
2500	work = kzalloc(sizeof *work, GFP_KERNEL);
2501	if (!work)
2502		return -ENOMEM;
2503
2504	work->id = id_priv;
2505	INIT_WORK(&work->work, cma_work_handler);
2506	work->old_state = RDMA_CM_ROUTE_QUERY;
2507	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2508	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2509	queue_work(cma_wq, &work->work);
2510	return 0;
2511}
2512
2513static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2514{
2515	/* get service level, SL, from IPv4 type of service, TOS */
2516	int sl = (tos >> 5) & 0x7;
2517
2518	/* final mappings are done by the vendor specific drivers */
2519	return sl;
2520}
2521
2522static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2523					   unsigned long supported_gids,
2524					   enum ib_gid_type default_gid)
2525{
2526	if ((network_type == RDMA_NETWORK_IPV4 ||
2527	     network_type == RDMA_NETWORK_IPV6) &&
2528	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2529		return IB_GID_TYPE_ROCE_UDP_ENCAP;
2530
2531	return default_gid;
2532}
2533
2534static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2535{
2536	struct rdma_route *route = &id_priv->id.route;
2537	struct rdma_addr *addr = &route->addr;
2538	struct cma_work *work;
2539	int ret;
2540	struct net_device *ndev = NULL;
2541
2542
2543	work = kzalloc(sizeof *work, GFP_KERNEL);
2544	if (!work)
2545		return -ENOMEM;
2546
2547	work->id = id_priv;
2548	INIT_WORK(&work->work, cma_work_handler);
2549
2550	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2551	if (!route->path_rec) {
2552		ret = -ENOMEM;
2553		goto err1;
2554	}
2555
2556	route->num_paths = 1;
2557
2558	if (addr->dev_addr.bound_dev_if) {
2559		unsigned long supported_gids;
2560
2561		ndev = dev_get_by_index(addr->dev_addr.net,
2562					addr->dev_addr.bound_dev_if);
2563		if (!ndev) {
2564			ret = -ENODEV;
2565			goto err2;
2566		}
2567
2568		route->path_rec->net = ndev->if_vnet;
2569		route->path_rec->ifindex = ndev->if_index;
2570		supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2571							    id_priv->id.port_num);
2572		route->path_rec->gid_type =
2573			cma_route_gid_type(addr->dev_addr.network,
2574					   supported_gids,
2575					   id_priv->gid_type);
2576	}
2577	if (!ndev) {
2578		ret = -ENODEV;
2579		goto err2;
2580	}
2581
2582	memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
2583
2584	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2585		    &route->path_rec->sgid);
2586	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2587		    &route->path_rec->dgid);
2588
2589	/* Use the hint from IP Stack to select GID Type */
2590	if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2591		route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2592	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2593		/* TODO: get the hoplimit from the inet/inet6 device */
2594		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2595	else
2596		route->path_rec->hop_limit = 1;
2597	route->path_rec->reversible = 1;
2598	route->path_rec->pkey = cpu_to_be16(0xffff);
2599	route->path_rec->mtu_selector = IB_SA_EQ;
2600	route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
2601	route->path_rec->traffic_class = id_priv->tos;
2602	route->path_rec->mtu = iboe_get_mtu(ndev->if_mtu);
2603	route->path_rec->rate_selector = IB_SA_EQ;
2604	route->path_rec->rate = iboe_get_rate(ndev);
2605	dev_put(ndev);
2606	route->path_rec->packet_life_time_selector = IB_SA_EQ;
2607	route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2608	if (!route->path_rec->mtu) {
2609		ret = -EINVAL;
2610		goto err2;
2611	}
2612
2613	work->old_state = RDMA_CM_ROUTE_QUERY;
2614	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2615	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2616	work->event.status = 0;
2617
2618	queue_work(cma_wq, &work->work);
2619
2620	return 0;
2621
2622err2:
2623	kfree(route->path_rec);
2624	route->path_rec = NULL;
2625err1:
2626	kfree(work);
2627	return ret;
2628}
2629
2630int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2631{
2632	struct rdma_id_private *id_priv;
2633	int ret;
2634
2635	id_priv = container_of(id, struct rdma_id_private, id);
2636	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
2637		return -EINVAL;
2638
2639	atomic_inc(&id_priv->refcount);
2640	if (rdma_cap_ib_sa(id->device, id->port_num))
2641		ret = cma_resolve_ib_route(id_priv, timeout_ms);
2642	else if (rdma_protocol_roce(id->device, id->port_num))
2643		ret = cma_resolve_iboe_route(id_priv);
2644	else if (rdma_protocol_iwarp(id->device, id->port_num))
2645		ret = cma_resolve_iw_route(id_priv, timeout_ms);
2646	else
2647		ret = -ENOSYS;
2648
2649	if (ret)
2650		goto err;
2651
2652	return 0;
2653err:
2654	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
2655	cma_deref_id(id_priv);
2656	return ret;
2657}
2658EXPORT_SYMBOL(rdma_resolve_route);
2659
2660static void cma_set_loopback(struct sockaddr *addr)
2661{
2662	switch (addr->sa_family) {
2663	case AF_INET:
2664		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2665		break;
2666	case AF_INET6:
2667		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2668			      0, 0, 0, htonl(1));
2669		break;
2670	default:
2671		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2672			    0, 0, 0, htonl(1));
2673		break;
2674	}
2675}
2676
2677static int cma_bind_loopback(struct rdma_id_private *id_priv)
2678{
2679	struct cma_device *cma_dev, *cur_dev;
2680	struct ib_port_attr port_attr;
2681	union ib_gid gid;
2682	u16 pkey;
2683	int ret;
2684	u8 p;
2685
2686	cma_dev = NULL;
2687	mutex_lock(&lock);
2688	list_for_each_entry(cur_dev, &dev_list, list) {
2689		if (cma_family(id_priv) == AF_IB &&
2690		    !rdma_cap_ib_cm(cur_dev->device, 1))
2691			continue;
2692
2693		if (!cma_dev)
2694			cma_dev = cur_dev;
2695
2696		for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2697			if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2698			    port_attr.state == IB_PORT_ACTIVE) {
2699				cma_dev = cur_dev;
2700				goto port_found;
2701			}
2702		}
2703	}
2704
2705	if (!cma_dev) {
2706		ret = -ENODEV;
2707		goto out;
2708	}
2709
2710	p = 1;
2711
2712port_found:
2713	ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
2714	if (ret)
2715		goto out;
2716
2717	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2718	if (ret)
2719		goto out;
2720
2721	id_priv->id.route.addr.dev_addr.dev_type =
2722		(rdma_protocol_ib(cma_dev->device, p)) ?
2723		ARPHRD_INFINIBAND : ARPHRD_ETHER;
2724
2725	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2726	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2727	id_priv->id.port_num = p;
2728	cma_attach_to_dev(id_priv, cma_dev);
2729	cma_set_loopback(cma_src_addr(id_priv));
2730out:
2731	mutex_unlock(&lock);
2732	return ret;
2733}
2734
2735static void addr_handler(int status, struct sockaddr *src_addr,
2736			 struct rdma_dev_addr *dev_addr, void *context)
2737{
2738	struct rdma_id_private *id_priv = context;
2739	struct rdma_cm_event event;
2740
2741	memset(&event, 0, sizeof event);
2742	mutex_lock(&id_priv->handler_mutex);
2743	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2744			   RDMA_CM_ADDR_RESOLVED))
2745		goto out;
2746
2747	memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
2748	if (!status && !id_priv->cma_dev)
2749		status = cma_acquire_dev(id_priv, NULL);
2750
2751	if (status) {
2752		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2753				   RDMA_CM_ADDR_BOUND))
2754			goto out;
2755		event.event = RDMA_CM_EVENT_ADDR_ERROR;
2756		event.status = status;
2757	} else
2758		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2759
2760	if (id_priv->id.event_handler(&id_priv->id, &event)) {
2761		cma_exch(id_priv, RDMA_CM_DESTROYING);
2762		mutex_unlock(&id_priv->handler_mutex);
2763		cma_deref_id(id_priv);
2764		rdma_destroy_id(&id_priv->id);
2765		return;
2766	}
2767out:
2768	mutex_unlock(&id_priv->handler_mutex);
2769	cma_deref_id(id_priv);
2770}
2771
2772static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2773{
2774	struct cma_work *work;
2775	union ib_gid gid;
2776	int ret;
2777
2778	work = kzalloc(sizeof *work, GFP_KERNEL);
2779	if (!work)
2780		return -ENOMEM;
2781
2782	if (!id_priv->cma_dev) {
2783		ret = cma_bind_loopback(id_priv);
2784		if (ret)
2785			goto err;
2786	}
2787
2788	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2789	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2790
2791	work->id = id_priv;
2792	INIT_WORK(&work->work, cma_work_handler);
2793	work->old_state = RDMA_CM_ADDR_QUERY;
2794	work->new_state = RDMA_CM_ADDR_RESOLVED;
2795	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2796	queue_work(cma_wq, &work->work);
2797	return 0;
2798err:
2799	kfree(work);
2800	return ret;
2801}
2802
2803static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2804{
2805	struct cma_work *work;
2806	int ret;
2807
2808	work = kzalloc(sizeof *work, GFP_KERNEL);
2809	if (!work)
2810		return -ENOMEM;
2811
2812	if (!id_priv->cma_dev) {
2813		ret = cma_resolve_ib_dev(id_priv);
2814		if (ret)
2815			goto err;
2816	}
2817
2818	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2819		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2820
2821	work->id = id_priv;
2822	INIT_WORK(&work->work, cma_work_handler);
2823	work->old_state = RDMA_CM_ADDR_QUERY;
2824	work->new_state = RDMA_CM_ADDR_RESOLVED;
2825	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2826	queue_work(cma_wq, &work->work);
2827	return 0;
2828err:
2829	kfree(work);
2830	return ret;
2831}
2832
2833static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2834			 struct sockaddr *dst_addr)
2835{
2836	if (!src_addr || !src_addr->sa_family) {
2837		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2838		src_addr->sa_family = dst_addr->sa_family;
2839		if (dst_addr->sa_family == AF_INET6) {
2840			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2841			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2842			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2843			if (IN6_IS_SCOPE_LINKLOCAL(&dst_addr6->sin6_addr) ||
2844			    IN6_IS_ADDR_MC_INTFACELOCAL(&dst_addr6->sin6_addr))
2845				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
2846		} else if (dst_addr->sa_family == AF_IB) {
2847			((struct sockaddr_ib *) src_addr)->sib_pkey =
2848				((struct sockaddr_ib *) dst_addr)->sib_pkey;
2849		}
2850	}
2851	return rdma_bind_addr(id, src_addr);
2852}
2853
2854int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2855		      struct sockaddr *dst_addr, int timeout_ms)
2856{
2857	struct rdma_id_private *id_priv;
2858	int ret;
2859
2860	id_priv = container_of(id, struct rdma_id_private, id);
2861	if (id_priv->state == RDMA_CM_IDLE) {
2862		ret = cma_bind_addr(id, src_addr, dst_addr);
2863		if (ret)
2864			return ret;
2865	}
2866
2867	if (cma_family(id_priv) != dst_addr->sa_family)
2868		return -EINVAL;
2869
2870	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
2871		return -EINVAL;
2872
2873	atomic_inc(&id_priv->refcount);
2874	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
2875	if (cma_any_addr(dst_addr)) {
2876		ret = cma_resolve_loopback(id_priv);
2877	} else {
2878		if (dst_addr->sa_family == AF_IB) {
2879			ret = cma_resolve_ib_addr(id_priv);
2880		} else {
2881			ret = cma_check_linklocal(&id->route.addr.dev_addr, dst_addr);
2882			if (ret)
2883				goto err;
2884
2885			ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2886					      dst_addr, &id->route.addr.dev_addr,
2887					      timeout_ms, addr_handler, id_priv);
2888		}
2889	}
2890	if (ret)
2891		goto err;
2892
2893	return 0;
2894err:
2895	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2896	cma_deref_id(id_priv);
2897	return ret;
2898}
2899EXPORT_SYMBOL(rdma_resolve_addr);
2900
2901int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2902{
2903	struct rdma_id_private *id_priv;
2904	unsigned long flags;
2905	int ret;
2906
2907	id_priv = container_of(id, struct rdma_id_private, id);
2908	spin_lock_irqsave(&id_priv->lock, flags);
2909	if (reuse || id_priv->state == RDMA_CM_IDLE) {
2910		id_priv->reuseaddr = reuse;
2911		ret = 0;
2912	} else {
2913		ret = -EINVAL;
2914	}
2915	spin_unlock_irqrestore(&id_priv->lock, flags);
2916	return ret;
2917}
2918EXPORT_SYMBOL(rdma_set_reuseaddr);
2919
2920int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2921{
2922	struct rdma_id_private *id_priv;
2923	unsigned long flags;
2924	int ret;
2925
2926	id_priv = container_of(id, struct rdma_id_private, id);
2927	spin_lock_irqsave(&id_priv->lock, flags);
2928	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2929		id_priv->options |= (1 << CMA_OPTION_AFONLY);
2930		id_priv->afonly = afonly;
2931		ret = 0;
2932	} else {
2933		ret = -EINVAL;
2934	}
2935	spin_unlock_irqrestore(&id_priv->lock, flags);
2936	return ret;
2937}
2938EXPORT_SYMBOL(rdma_set_afonly);
2939
2940static void cma_bind_port(struct rdma_bind_list *bind_list,
2941			  struct rdma_id_private *id_priv)
2942{
2943	struct sockaddr *addr;
2944	struct sockaddr_ib *sib;
2945	u64 sid, mask;
2946	__be16 port;
2947
2948	addr = cma_src_addr(id_priv);
2949	port = htons(bind_list->port);
2950
2951	switch (addr->sa_family) {
2952	case AF_INET:
2953		((struct sockaddr_in *) addr)->sin_port = port;
2954		break;
2955	case AF_INET6:
2956		((struct sockaddr_in6 *) addr)->sin6_port = port;
2957		break;
2958	case AF_IB:
2959		sib = (struct sockaddr_ib *) addr;
2960		sid = be64_to_cpu(sib->sib_sid);
2961		mask = be64_to_cpu(sib->sib_sid_mask);
2962		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2963		sib->sib_sid_mask = cpu_to_be64(~0ULL);
2964		break;
2965	}
2966	id_priv->bind_list = bind_list;
2967	hlist_add_head(&id_priv->node, &bind_list->owners);
2968}
2969
2970static int cma_alloc_port(enum rdma_port_space ps,
2971			  struct rdma_id_private *id_priv, unsigned short snum)
2972{
2973	struct rdma_bind_list *bind_list;
2974	int ret;
2975
2976	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2977	if (!bind_list)
2978		return -ENOMEM;
2979
2980	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2981			   snum);
2982	if (ret < 0)
2983		goto err;
2984
2985	bind_list->ps = ps;
2986	bind_list->port = (unsigned short)ret;
2987	cma_bind_port(bind_list, id_priv);
2988	return 0;
2989err:
2990	kfree(bind_list);
2991	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
2992}
2993
2994static int cma_alloc_any_port(enum rdma_port_space ps,
2995			      struct rdma_id_private *id_priv)
2996{
2997	static unsigned int last_used_port;
2998	int low, high, remaining;
2999	unsigned int rover;
3000	struct vnet *net = id_priv->id.route.addr.dev_addr.net;
3001	u32 rand;
3002
3003	inet_get_local_port_range(net, &low, &high);
3004	remaining = (high - low) + 1;
3005	get_random_bytes(&rand, sizeof(rand));
3006	rover = rand % remaining + low;
3007retry:
3008	if (last_used_port != rover &&
3009	    !cma_ps_find(net, ps, (unsigned short)rover)) {
3010		int ret = cma_alloc_port(ps, id_priv, rover);
3011		/*
3012		 * Remember previously used port number in order to avoid
3013		 * re-using same port immediately after it is closed.
3014		 */
3015		if (!ret)
3016			last_used_port = rover;
3017		if (ret != -EADDRNOTAVAIL)
3018			return ret;
3019	}
3020	if (--remaining) {
3021		rover++;
3022		if ((rover < low) || (rover > high))
3023			rover = low;
3024		goto retry;
3025	}
3026	return -EADDRNOTAVAIL;
3027}
3028
3029/*
3030 * Check that the requested port is available.  This is called when trying to
3031 * bind to a specific port, or when trying to listen on a bound port.  In
3032 * the latter case, the provided id_priv may already be on the bind_list, but
3033 * we still need to check that it's okay to start listening.
3034 */
3035static int cma_check_port(struct rdma_bind_list *bind_list,
3036			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3037{
3038	struct rdma_id_private *cur_id;
3039	struct sockaddr *addr, *cur_addr;
3040
3041	addr = cma_src_addr(id_priv);
3042	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3043		if (id_priv == cur_id)
3044			continue;
3045
3046		if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
3047		    cur_id->reuseaddr)
3048			continue;
3049
3050		cur_addr = cma_src_addr(cur_id);
3051		if (id_priv->afonly && cur_id->afonly &&
3052		    (addr->sa_family != cur_addr->sa_family))
3053			continue;
3054
3055		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3056			return -EADDRNOTAVAIL;
3057
3058		if (!cma_addr_cmp(addr, cur_addr))
3059			return -EADDRINUSE;
3060	}
3061	return 0;
3062}
3063
3064static int cma_use_port(enum rdma_port_space ps,
3065			struct rdma_id_private *id_priv)
3066{
3067	struct rdma_bind_list *bind_list;
3068	unsigned short snum;
3069	int ret;
3070
3071	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3072	if (snum < IPPORT_RESERVED &&
3073	    priv_check(curthread, PRIV_NETINET_BINDANY) != 0)
3074		return -EACCES;
3075
3076	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3077	if (!bind_list) {
3078		ret = cma_alloc_port(ps, id_priv, snum);
3079	} else {
3080		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3081		if (!ret)
3082			cma_bind_port(bind_list, id_priv);
3083	}
3084	return ret;
3085}
3086
3087static int cma_bind_listen(struct rdma_id_private *id_priv)
3088{
3089	struct rdma_bind_list *bind_list = id_priv->bind_list;
3090	int ret = 0;
3091
3092	mutex_lock(&lock);
3093	if (bind_list->owners.first->next)
3094		ret = cma_check_port(bind_list, id_priv, 0);
3095	mutex_unlock(&lock);
3096	return ret;
3097}
3098
3099static enum rdma_port_space cma_select_inet_ps(
3100		struct rdma_id_private *id_priv)
3101{
3102	switch (id_priv->id.ps) {
3103	case RDMA_PS_TCP:
3104	case RDMA_PS_UDP:
3105	case RDMA_PS_IPOIB:
3106	case RDMA_PS_IB:
3107		return id_priv->id.ps;
3108	default:
3109
3110		return 0;
3111	}
3112}
3113
3114static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
3115{
3116	enum rdma_port_space ps = 0;
3117	struct sockaddr_ib *sib;
3118	u64 sid_ps, mask, sid;
3119
3120	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3121	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3122	sid = be64_to_cpu(sib->sib_sid) & mask;
3123
3124	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3125		sid_ps = RDMA_IB_IP_PS_IB;
3126		ps = RDMA_PS_IB;
3127	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3128		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3129		sid_ps = RDMA_IB_IP_PS_TCP;
3130		ps = RDMA_PS_TCP;
3131	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3132		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3133		sid_ps = RDMA_IB_IP_PS_UDP;
3134		ps = RDMA_PS_UDP;
3135	}
3136
3137	if (ps) {
3138		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3139		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3140						be64_to_cpu(sib->sib_sid_mask));
3141	}
3142	return ps;
3143}
3144
3145static int cma_get_port(struct rdma_id_private *id_priv)
3146{
3147	enum rdma_port_space ps;
3148	int ret;
3149
3150	if (cma_family(id_priv) != AF_IB)
3151		ps = cma_select_inet_ps(id_priv);
3152	else
3153		ps = cma_select_ib_ps(id_priv);
3154	if (!ps)
3155		return -EPROTONOSUPPORT;
3156
3157	mutex_lock(&lock);
3158	if (cma_any_port(cma_src_addr(id_priv)))
3159		ret = cma_alloc_any_port(ps, id_priv);
3160	else
3161		ret = cma_use_port(ps, id_priv);
3162	mutex_unlock(&lock);
3163
3164	return ret;
3165}
3166
3167static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3168			       struct sockaddr *addr)
3169{
3170#ifdef INET6
3171	struct sockaddr_in6 sin6;
3172
3173	if (addr->sa_family != AF_INET6)
3174		return 0;
3175
3176	sin6 = *(struct sockaddr_in6 *)addr;
3177
3178	if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr) ||
3179	    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) {
3180		bool failure;
3181
3182		CURVNET_SET_QUIET(dev_addr->net);
3183		failure = sa6_recoverscope(&sin6) || sin6.sin6_scope_id == 0;
3184		CURVNET_RESTORE();
3185
3186		/* check if IPv6 scope ID is not set */
3187		if (failure)
3188			return -EINVAL;
3189		dev_addr->bound_dev_if = sin6.sin6_scope_id;
3190	}
3191#endif
3192	return 0;
3193}
3194
3195int rdma_listen(struct rdma_cm_id *id, int backlog)
3196{
3197	struct rdma_id_private *id_priv;
3198	int ret;
3199
3200	id_priv = container_of(id, struct rdma_id_private, id);
3201	if (id_priv->state == RDMA_CM_IDLE) {
3202		id->route.addr.src_addr.ss_family = AF_INET;
3203		ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3204		if (ret)
3205			return ret;
3206	}
3207
3208	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3209		return -EINVAL;
3210
3211	if (id_priv->reuseaddr) {
3212		ret = cma_bind_listen(id_priv);
3213		if (ret)
3214			goto err;
3215	}
3216
3217	id_priv->backlog = backlog;
3218	if (id->device) {
3219		if (rdma_cap_ib_cm(id->device, 1)) {
3220			ret = cma_ib_listen(id_priv);
3221			if (ret)
3222				goto err;
3223		} else if (rdma_cap_iw_cm(id->device, 1)) {
3224			ret = cma_iw_listen(id_priv, backlog);
3225			if (ret)
3226				goto err;
3227		} else {
3228			ret = -ENOSYS;
3229			goto err;
3230		}
3231	} else
3232		cma_listen_on_all(id_priv);
3233
3234	return 0;
3235err:
3236	id_priv->backlog = 0;
3237	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3238	return ret;
3239}
3240EXPORT_SYMBOL(rdma_listen);
3241
3242int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3243{
3244	struct rdma_id_private *id_priv;
3245	int ret;
3246
3247	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3248	    addr->sa_family != AF_IB)
3249		return -EAFNOSUPPORT;
3250
3251	id_priv = container_of(id, struct rdma_id_private, id);
3252	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3253		return -EINVAL;
3254
3255	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3256	if (ret)
3257		goto err1;
3258
3259	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3260	if (!cma_any_addr(addr)) {
3261		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3262		if (ret)
3263			goto err1;
3264
3265		ret = cma_acquire_dev(id_priv, NULL);
3266		if (ret)
3267			goto err1;
3268	}
3269
3270	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3271		if (addr->sa_family == AF_INET)
3272			id_priv->afonly = 1;
3273#ifdef INET6
3274		else if (addr->sa_family == AF_INET6) {
3275			CURVNET_SET_QUIET(id_priv->id.route.addr.dev_addr.net);
3276			id_priv->afonly = V_ip6_v6only;
3277			CURVNET_RESTORE();
3278		}
3279#endif
3280	}
3281	ret = cma_get_port(id_priv);
3282	if (ret)
3283		goto err2;
3284
3285	return 0;
3286err2:
3287	if (id_priv->cma_dev)
3288		cma_release_dev(id_priv);
3289err1:
3290	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3291	return ret;
3292}
3293EXPORT_SYMBOL(rdma_bind_addr);
3294
3295static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3296{
3297	struct cma_hdr *cma_hdr;
3298
3299	cma_hdr = hdr;
3300	cma_hdr->cma_version = CMA_VERSION;
3301	if (cma_family(id_priv) == AF_INET) {
3302		struct sockaddr_in *src4, *dst4;
3303
3304		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3305		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3306
3307		cma_set_ip_ver(cma_hdr, 4);
3308		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3309		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3310		cma_hdr->port = src4->sin_port;
3311	} else if (cma_family(id_priv) == AF_INET6) {
3312		struct sockaddr_in6 *src6, *dst6;
3313
3314		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3315		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3316
3317		cma_set_ip_ver(cma_hdr, 6);
3318		cma_hdr->src_addr.ip6 = src6->sin6_addr;
3319		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3320		cma_hdr->port = src6->sin6_port;
3321		cma_ip6_clear_scope_id(&cma_hdr->src_addr.ip6);
3322		cma_ip6_clear_scope_id(&cma_hdr->dst_addr.ip6);
3323	}
3324	return 0;
3325}
3326
3327static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3328				struct ib_cm_event *ib_event)
3329{
3330	struct rdma_id_private *id_priv = cm_id->context;
3331	struct rdma_cm_event event;
3332	struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3333	int ret = 0;
3334
3335	mutex_lock(&id_priv->handler_mutex);
3336	if (id_priv->state != RDMA_CM_CONNECT)
3337		goto out;
3338
3339	memset(&event, 0, sizeof event);
3340	switch (ib_event->event) {
3341	case IB_CM_SIDR_REQ_ERROR:
3342		event.event = RDMA_CM_EVENT_UNREACHABLE;
3343		event.status = -ETIMEDOUT;
3344		break;
3345	case IB_CM_SIDR_REP_RECEIVED:
3346		event.param.ud.private_data = ib_event->private_data;
3347		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3348		if (rep->status != IB_SIDR_SUCCESS) {
3349			event.event = RDMA_CM_EVENT_UNREACHABLE;
3350			event.status = ib_event->param.sidr_rep_rcvd.status;
3351			break;
3352		}
3353		ret = cma_set_qkey(id_priv, rep->qkey);
3354		if (ret) {
3355			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3356			event.status = ret;
3357			break;
3358		}
3359		ret = ib_init_ah_from_path(id_priv->id.device,
3360					   id_priv->id.port_num,
3361					   id_priv->id.route.path_rec,
3362					   &event.param.ud.ah_attr);
3363		if (ret) {
3364			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3365			event.status = ret;
3366			break;
3367		}
3368		event.param.ud.qp_num = rep->qpn;
3369		event.param.ud.qkey = rep->qkey;
3370		event.event = RDMA_CM_EVENT_ESTABLISHED;
3371		event.status = 0;
3372		break;
3373	default:
3374		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3375		       ib_event->event);
3376		goto out;
3377	}
3378
3379	ret = id_priv->id.event_handler(&id_priv->id, &event);
3380	if (ret) {
3381		/* Destroy the CM ID by returning a non-zero value. */
3382		id_priv->cm_id.ib = NULL;
3383		cma_exch(id_priv, RDMA_CM_DESTROYING);
3384		mutex_unlock(&id_priv->handler_mutex);
3385		rdma_destroy_id(&id_priv->id);
3386		return ret;
3387	}
3388out:
3389	mutex_unlock(&id_priv->handler_mutex);
3390	return ret;
3391}
3392
3393static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3394			      struct rdma_conn_param *conn_param)
3395{
3396	struct ib_cm_sidr_req_param req;
3397	struct ib_cm_id	*id;
3398	void *private_data;
3399	int offset, ret;
3400
3401	memset(&req, 0, sizeof req);
3402	offset = cma_user_data_offset(id_priv);
3403	req.private_data_len = offset + conn_param->private_data_len;
3404	if (req.private_data_len < conn_param->private_data_len)
3405		return -EINVAL;
3406
3407	if (req.private_data_len) {
3408		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3409		if (!private_data)
3410			return -ENOMEM;
3411	} else {
3412		private_data = NULL;
3413	}
3414
3415	if (conn_param->private_data && conn_param->private_data_len)
3416		memcpy((char *)private_data + offset, conn_param->private_data,
3417		       conn_param->private_data_len);
3418
3419	if (private_data) {
3420		ret = cma_format_hdr(private_data, id_priv);
3421		if (ret)
3422			goto out;
3423		req.private_data = private_data;
3424	}
3425
3426	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3427			     id_priv);
3428	if (IS_ERR(id)) {
3429		ret = PTR_ERR(id);
3430		goto out;
3431	}
3432	id_priv->cm_id.ib = id;
3433
3434	req.path = id_priv->id.route.path_rec;
3435	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3436	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3437	req.max_cm_retries = CMA_MAX_CM_RETRIES;
3438
3439	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3440	if (ret) {
3441		ib_destroy_cm_id(id_priv->cm_id.ib);
3442		id_priv->cm_id.ib = NULL;
3443	}
3444out:
3445	kfree(private_data);
3446	return ret;
3447}
3448
3449static int cma_connect_ib(struct rdma_id_private *id_priv,
3450			  struct rdma_conn_param *conn_param)
3451{
3452	struct ib_cm_req_param req;
3453	struct rdma_route *route;
3454	void *private_data;
3455	struct ib_cm_id	*id;
3456	int offset, ret;
3457
3458	memset(&req, 0, sizeof req);
3459	offset = cma_user_data_offset(id_priv);
3460	req.private_data_len = offset + conn_param->private_data_len;
3461	if (req.private_data_len < conn_param->private_data_len)
3462		return -EINVAL;
3463
3464	if (req.private_data_len) {
3465		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3466		if (!private_data)
3467			return -ENOMEM;
3468	} else {
3469		private_data = NULL;
3470	}
3471
3472	if (conn_param->private_data && conn_param->private_data_len)
3473		memcpy((char *)private_data + offset, conn_param->private_data,
3474		       conn_param->private_data_len);
3475
3476	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3477	if (IS_ERR(id)) {
3478		ret = PTR_ERR(id);
3479		goto out;
3480	}
3481	id_priv->cm_id.ib = id;
3482
3483	route = &id_priv->id.route;
3484	if (private_data) {
3485		ret = cma_format_hdr(private_data, id_priv);
3486		if (ret)
3487			goto out;
3488		req.private_data = private_data;
3489	}
3490
3491	req.primary_path = &route->path_rec[0];
3492	if (route->num_paths == 2)
3493		req.alternate_path = &route->path_rec[1];
3494
3495	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3496	req.qp_num = id_priv->qp_num;
3497	req.qp_type = id_priv->id.qp_type;
3498	req.starting_psn = id_priv->seq_num;
3499	req.responder_resources = conn_param->responder_resources;
3500	req.initiator_depth = conn_param->initiator_depth;
3501	req.flow_control = conn_param->flow_control;
3502	req.retry_count = min_t(u8, 7, conn_param->retry_count);
3503	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3504	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3505	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3506	req.max_cm_retries = CMA_MAX_CM_RETRIES;
3507	req.srq = id_priv->srq ? 1 : 0;
3508
3509	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3510out:
3511	if (ret && !IS_ERR(id)) {
3512		ib_destroy_cm_id(id);
3513		id_priv->cm_id.ib = NULL;
3514	}
3515
3516	kfree(private_data);
3517	return ret;
3518}
3519
3520static int cma_connect_iw(struct rdma_id_private *id_priv,
3521			  struct rdma_conn_param *conn_param)
3522{
3523	struct iw_cm_id *cm_id;
3524	int ret;
3525	struct iw_cm_conn_param iw_param;
3526
3527	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
3528	if (IS_ERR(cm_id))
3529		return PTR_ERR(cm_id);
3530
3531	cm_id->tos = id_priv->tos;
3532	id_priv->cm_id.iw = cm_id;
3533
3534	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3535	       rdma_addr_size(cma_src_addr(id_priv)));
3536	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3537	       rdma_addr_size(cma_dst_addr(id_priv)));
3538
3539	ret = cma_modify_qp_rtr(id_priv, conn_param);
3540	if (ret)
3541		goto out;
3542
3543	if (conn_param) {
3544		iw_param.ord = conn_param->initiator_depth;
3545		iw_param.ird = conn_param->responder_resources;
3546		iw_param.private_data = conn_param->private_data;
3547		iw_param.private_data_len = conn_param->private_data_len;
3548		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3549	} else {
3550		memset(&iw_param, 0, sizeof iw_param);
3551		iw_param.qpn = id_priv->qp_num;
3552	}
3553	ret = iw_cm_connect(cm_id, &iw_param);
3554out:
3555	if (ret) {
3556		iw_destroy_cm_id(cm_id);
3557		id_priv->cm_id.iw = NULL;
3558	}
3559	return ret;
3560}
3561
3562int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3563{
3564	struct rdma_id_private *id_priv;
3565	int ret;
3566
3567	id_priv = container_of(id, struct rdma_id_private, id);
3568	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
3569		return -EINVAL;
3570
3571	if (!id->qp) {
3572		id_priv->qp_num = conn_param->qp_num;
3573		id_priv->srq = conn_param->srq;
3574	}
3575
3576	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3577		if (id->qp_type == IB_QPT_UD)
3578			ret = cma_resolve_ib_udp(id_priv, conn_param);
3579		else
3580			ret = cma_connect_ib(id_priv, conn_param);
3581	} else if (rdma_cap_iw_cm(id->device, id->port_num))
3582		ret = cma_connect_iw(id_priv, conn_param);
3583	else
3584		ret = -ENOSYS;
3585	if (ret)
3586		goto err;
3587
3588	return 0;
3589err:
3590	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
3591	return ret;
3592}
3593EXPORT_SYMBOL(rdma_connect);
3594
3595static int cma_accept_ib(struct rdma_id_private *id_priv,
3596			 struct rdma_conn_param *conn_param)
3597{
3598	struct ib_cm_rep_param rep;
3599	int ret;
3600
3601	ret = cma_modify_qp_rtr(id_priv, conn_param);
3602	if (ret)
3603		goto out;
3604
3605	ret = cma_modify_qp_rts(id_priv, conn_param);
3606	if (ret)
3607		goto out;
3608
3609	memset(&rep, 0, sizeof rep);
3610	rep.qp_num = id_priv->qp_num;
3611	rep.starting_psn = id_priv->seq_num;
3612	rep.private_data = conn_param->private_data;
3613	rep.private_data_len = conn_param->private_data_len;
3614	rep.responder_resources = conn_param->responder_resources;
3615	rep.initiator_depth = conn_param->initiator_depth;
3616	rep.failover_accepted = 0;
3617	rep.flow_control = conn_param->flow_control;
3618	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3619	rep.srq = id_priv->srq ? 1 : 0;
3620
3621	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3622out:
3623	return ret;
3624}
3625
3626static int cma_accept_iw(struct rdma_id_private *id_priv,
3627		  struct rdma_conn_param *conn_param)
3628{
3629	struct iw_cm_conn_param iw_param;
3630	int ret;
3631
3632	ret = cma_modify_qp_rtr(id_priv, conn_param);
3633	if (ret)
3634		return ret;
3635
3636	iw_param.ord = conn_param->initiator_depth;
3637	iw_param.ird = conn_param->responder_resources;
3638	iw_param.private_data = conn_param->private_data;
3639	iw_param.private_data_len = conn_param->private_data_len;
3640	if (id_priv->id.qp) {
3641		iw_param.qpn = id_priv->qp_num;
3642	} else
3643		iw_param.qpn = conn_param->qp_num;
3644
3645	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3646}
3647
3648static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
3649			     enum ib_cm_sidr_status status, u32 qkey,
3650			     const void *private_data, int private_data_len)
3651{
3652	struct ib_cm_sidr_rep_param rep;
3653	int ret;
3654
3655	memset(&rep, 0, sizeof rep);
3656	rep.status = status;
3657	if (status == IB_SIDR_SUCCESS) {
3658		ret = cma_set_qkey(id_priv, qkey);
3659		if (ret)
3660			return ret;
3661		rep.qp_num = id_priv->qp_num;
3662		rep.qkey = id_priv->qkey;
3663	}
3664	rep.private_data = private_data;
3665	rep.private_data_len = private_data_len;
3666
3667	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3668}
3669
3670int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3671{
3672	struct rdma_id_private *id_priv;
3673	int ret;
3674
3675	id_priv = container_of(id, struct rdma_id_private, id);
3676
3677	id_priv->owner = task_pid_nr(current);
3678
3679	if (!cma_comp(id_priv, RDMA_CM_CONNECT))
3680		return -EINVAL;
3681
3682	if (!id->qp && conn_param) {
3683		id_priv->qp_num = conn_param->qp_num;
3684		id_priv->srq = conn_param->srq;
3685	}
3686
3687	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3688		if (id->qp_type == IB_QPT_UD) {
3689			if (conn_param)
3690				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3691							conn_param->qkey,
3692							conn_param->private_data,
3693							conn_param->private_data_len);
3694			else
3695				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3696							0, NULL, 0);
3697		} else {
3698			if (conn_param)
3699				ret = cma_accept_ib(id_priv, conn_param);
3700			else
3701				ret = cma_rep_recv(id_priv);
3702		}
3703	} else if (rdma_cap_iw_cm(id->device, id->port_num))
3704		ret = cma_accept_iw(id_priv, conn_param);
3705	else
3706		ret = -ENOSYS;
3707
3708	if (ret)
3709		goto reject;
3710
3711	return 0;
3712reject:
3713	cma_modify_qp_err(id_priv);
3714	rdma_reject(id, NULL, 0);
3715	return ret;
3716}
3717EXPORT_SYMBOL(rdma_accept);
3718
3719int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3720{
3721	struct rdma_id_private *id_priv;
3722	int ret;
3723
3724	id_priv = container_of(id, struct rdma_id_private, id);
3725	if (!id_priv->cm_id.ib)
3726		return -EINVAL;
3727
3728	switch (id->device->node_type) {
3729	case RDMA_NODE_IB_CA:
3730		ret = ib_cm_notify(id_priv->cm_id.ib, event);
3731		break;
3732	default:
3733		ret = 0;
3734		break;
3735	}
3736	return ret;
3737}
3738EXPORT_SYMBOL(rdma_notify);
3739
3740int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3741		u8 private_data_len)
3742{
3743	struct rdma_id_private *id_priv;
3744	int ret;
3745
3746	id_priv = container_of(id, struct rdma_id_private, id);
3747	if (!id_priv->cm_id.ib)
3748		return -EINVAL;
3749
3750	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3751		if (id->qp_type == IB_QPT_UD)
3752			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
3753						private_data, private_data_len);
3754		else
3755			ret = ib_send_cm_rej(id_priv->cm_id.ib,
3756					     IB_CM_REJ_CONSUMER_DEFINED, NULL,
3757					     0, private_data, private_data_len);
3758	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3759		ret = iw_cm_reject(id_priv->cm_id.iw,
3760				   private_data, private_data_len);
3761	} else
3762		ret = -ENOSYS;
3763
3764	return ret;
3765}
3766EXPORT_SYMBOL(rdma_reject);
3767
3768int rdma_disconnect(struct rdma_cm_id *id)
3769{
3770	struct rdma_id_private *id_priv;
3771	int ret;
3772
3773	id_priv = container_of(id, struct rdma_id_private, id);
3774	if (!id_priv->cm_id.ib)
3775		return -EINVAL;
3776
3777	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3778		ret = cma_modify_qp_err(id_priv);
3779		if (ret)
3780			goto out;
3781		/* Initiate or respond to a disconnect. */
3782		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3783			ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
3784	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3785		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3786	} else
3787		ret = -EINVAL;
3788
3789out:
3790	return ret;
3791}
3792EXPORT_SYMBOL(rdma_disconnect);
3793
3794static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3795{
3796	struct rdma_id_private *id_priv;
3797	struct cma_multicast *mc = multicast->context;
3798	struct rdma_cm_event event;
3799	int ret = 0;
3800
3801	id_priv = mc->id_priv;
3802	mutex_lock(&id_priv->handler_mutex);
3803	if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3804	    id_priv->state != RDMA_CM_ADDR_RESOLVED)
3805		goto out;
3806
3807	if (!status)
3808		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
3809	mutex_lock(&id_priv->qp_mutex);
3810	if (!status && id_priv->id.qp)
3811		status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
3812					 be16_to_cpu(multicast->rec.mlid));
3813	mutex_unlock(&id_priv->qp_mutex);
3814
3815	memset(&event, 0, sizeof event);
3816	event.status = status;
3817	event.param.ud.private_data = mc->context;
3818	if (!status) {
3819		struct rdma_dev_addr *dev_addr =
3820			&id_priv->id.route.addr.dev_addr;
3821		struct net_device *ndev =
3822			dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
3823		enum ib_gid_type gid_type =
3824			id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3825			rdma_start_port(id_priv->cma_dev->device)];
3826
3827		event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3828		ret = ib_init_ah_from_mcmember(id_priv->id.device,
3829					       id_priv->id.port_num,
3830					       &multicast->rec,
3831					       ndev, gid_type,
3832					       &event.param.ud.ah_attr);
3833		if (ret)
3834			event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3835
3836		event.param.ud.qp_num = 0xFFFFFF;
3837		event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3838		if (ndev)
3839			dev_put(ndev);
3840	} else
3841		event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3842
3843	ret = id_priv->id.event_handler(&id_priv->id, &event);
3844	if (ret) {
3845		cma_exch(id_priv, RDMA_CM_DESTROYING);
3846		mutex_unlock(&id_priv->handler_mutex);
3847		rdma_destroy_id(&id_priv->id);
3848		return 0;
3849	}
3850
3851out:
3852	mutex_unlock(&id_priv->handler_mutex);
3853	return 0;
3854}
3855
3856static void cma_set_mgid(struct rdma_id_private *id_priv,
3857			 struct sockaddr *addr, union ib_gid *mgid)
3858{
3859	unsigned char mc_map[MAX_ADDR_LEN];
3860	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3861	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3862	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3863
3864	if (cma_any_addr(addr)) {
3865		memset(mgid, 0, sizeof *mgid);
3866	} else if ((addr->sa_family == AF_INET6) &&
3867		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
3868								 0xFF10A01B)) {
3869		/* IPv6 address is an SA assigned MGID. */
3870		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3871	} else if (addr->sa_family == AF_IB) {
3872		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
3873	} else if (addr->sa_family == AF_INET6) {
3874		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3875		if (id_priv->id.ps == RDMA_PS_UDP)
3876			mc_map[7] = 0x01;	/* Use RDMA CM signature */
3877		*mgid = *(union ib_gid *) (mc_map + 4);
3878	} else {
3879		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
3880		if (id_priv->id.ps == RDMA_PS_UDP)
3881			mc_map[7] = 0x01;	/* Use RDMA CM signature */
3882		*mgid = *(union ib_gid *) (mc_map + 4);
3883	}
3884}
3885
3886static void cma_query_sa_classport_info_cb(int status,
3887					   struct ib_class_port_info *rec,
3888					   void *context)
3889{
3890	struct class_port_info_context *cb_ctx = context;
3891
3892	WARN_ON(!context);
3893
3894	if (status || !rec) {
3895		pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3896			 cb_ctx->device->name, cb_ctx->port_num, status);
3897		goto out;
3898	}
3899
3900	memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
3901
3902out:
3903	complete(&cb_ctx->done);
3904}
3905
3906static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
3907				       struct ib_class_port_info *class_port_info)
3908{
3909	struct class_port_info_context *cb_ctx;
3910	int ret;
3911
3912	cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
3913	if (!cb_ctx)
3914		return -ENOMEM;
3915
3916	cb_ctx->device = device;
3917	cb_ctx->class_port_info = class_port_info;
3918	cb_ctx->port_num = port_num;
3919	init_completion(&cb_ctx->done);
3920
3921	ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
3922					     CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
3923					     GFP_KERNEL, cma_query_sa_classport_info_cb,
3924					     cb_ctx, &cb_ctx->sa_query);
3925	if (ret < 0) {
3926		pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3927		       device->name, port_num, ret);
3928		goto out;
3929	}
3930
3931	wait_for_completion(&cb_ctx->done);
3932
3933out:
3934	kfree(cb_ctx);
3935	return ret;
3936}
3937
3938static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3939				 struct cma_multicast *mc)
3940{
3941	struct ib_sa_mcmember_rec rec;
3942	struct ib_class_port_info class_port_info;
3943	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3944	ib_sa_comp_mask comp_mask;
3945	int ret;
3946
3947	ib_addr_get_mgid(dev_addr, &rec.mgid);
3948	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3949				     &rec.mgid, &rec);
3950	if (ret)
3951		return ret;
3952
3953	ret = cma_set_qkey(id_priv, 0);
3954	if (ret)
3955		return ret;
3956
3957	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
3958	rec.qkey = cpu_to_be32(id_priv->qkey);
3959	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
3960	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3961	rec.join_state = mc->join_state;
3962
3963	if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
3964		ret = cma_query_sa_classport_info(id_priv->id.device,
3965						  id_priv->id.port_num,
3966						  &class_port_info);
3967
3968		if (ret)
3969			return ret;
3970
3971		if (!(ib_get_cpi_capmask2(&class_port_info) &
3972		      IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
3973			pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3974				"RDMA CM: SM doesn't support Send Only Full Member option\n",
3975				id_priv->id.device->name, id_priv->id.port_num);
3976			return -EOPNOTSUPP;
3977		}
3978	}
3979
3980	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3981		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3982		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3983		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
3984		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3985
3986	if (id_priv->id.ps == RDMA_PS_IPOIB)
3987		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
3988			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3989			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3990			     IB_SA_MCMEMBER_REC_MTU |
3991			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
3992
3993	mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3994						id_priv->id.port_num, &rec,
3995						comp_mask, GFP_KERNEL,
3996						cma_ib_mc_handler, mc);
3997	return PTR_ERR_OR_ZERO(mc->multicast.ib);
3998}
3999
4000static void iboe_mcast_work_handler(struct work_struct *work)
4001{
4002	struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
4003	struct cma_multicast *mc = mw->mc;
4004	struct ib_sa_multicast *m = mc->multicast.ib;
4005
4006	mc->multicast.ib->context = mc;
4007	cma_ib_mc_handler(0, m);
4008	kref_put(&mc->mcref, release_mc);
4009	kfree(mw);
4010}
4011
4012static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4013			      enum ib_gid_type gid_type)
4014{
4015	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4016	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4017
4018	if (cma_any_addr(addr)) {
4019		memset(mgid, 0, sizeof *mgid);
4020	} else if (addr->sa_family == AF_INET6) {
4021		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4022	} else {
4023		mgid->raw[0] =
4024			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4025		mgid->raw[1] =
4026			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4027		mgid->raw[2] = 0;
4028		mgid->raw[3] = 0;
4029		mgid->raw[4] = 0;
4030		mgid->raw[5] = 0;
4031		mgid->raw[6] = 0;
4032		mgid->raw[7] = 0;
4033		mgid->raw[8] = 0;
4034		mgid->raw[9] = 0;
4035		mgid->raw[10] = 0xff;
4036		mgid->raw[11] = 0xff;
4037		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4038	}
4039}
4040
4041static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4042				   struct cma_multicast *mc)
4043{
4044	struct iboe_mcast_work *work;
4045	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4046	int err = 0;
4047	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4048	struct net_device *ndev = NULL;
4049	enum ib_gid_type gid_type;
4050	bool send_only;
4051
4052	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4053
4054	if (cma_zero_addr((struct sockaddr *)&mc->addr))
4055		return -EINVAL;
4056
4057	work = kzalloc(sizeof *work, GFP_KERNEL);
4058	if (!work)
4059		return -ENOMEM;
4060
4061	mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
4062	if (!mc->multicast.ib) {
4063		err = -ENOMEM;
4064		goto out1;
4065	}
4066
4067	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4068		   rdma_start_port(id_priv->cma_dev->device)];
4069	cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid, gid_type);
4070
4071	mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
4072	if (id_priv->id.ps == RDMA_PS_UDP)
4073		mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
4074
4075	if (dev_addr->bound_dev_if)
4076		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4077	if (!ndev) {
4078		err = -ENODEV;
4079		goto out2;
4080	}
4081	mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
4082	mc->multicast.ib->rec.hop_limit = 1;
4083	mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->if_mtu);
4084
4085	if (addr->sa_family == AF_INET || addr->sa_family == AF_INET6) {
4086		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4087			mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4088			if (!send_only) {
4089				err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
4090						    true);
4091				if (!err)
4092					mc->igmp_joined = true;
4093			}
4094		}
4095	} else {
4096		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4097			err = -ENOTSUPP;
4098	}
4099	dev_put(ndev);
4100	if (err || !mc->multicast.ib->rec.mtu) {
4101		if (!err)
4102			err = -EINVAL;
4103		goto out2;
4104	}
4105	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4106		    &mc->multicast.ib->rec.port_gid);
4107	work->id = id_priv;
4108	work->mc = mc;
4109	INIT_WORK(&work->work, iboe_mcast_work_handler);
4110	kref_get(&mc->mcref);
4111	queue_work(cma_wq, &work->work);
4112
4113	return 0;
4114
4115out2:
4116	kfree(mc->multicast.ib);
4117out1:
4118	kfree(work);
4119	return err;
4120}
4121
4122int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4123			u8 join_state, void *context)
4124{
4125	struct rdma_id_private *id_priv;
4126	struct cma_multicast *mc;
4127	int ret;
4128
4129	if (!id->device)
4130		return -EINVAL;
4131
4132	id_priv = container_of(id, struct rdma_id_private, id);
4133	if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4134	    !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
4135		return -EINVAL;
4136
4137	mc = kmalloc(sizeof *mc, GFP_KERNEL);
4138	if (!mc)
4139		return -ENOMEM;
4140
4141	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4142	mc->context = context;
4143	mc->id_priv = id_priv;
4144	mc->igmp_joined = false;
4145	mc->join_state = join_state;
4146	spin_lock(&id_priv->lock);
4147	list_add(&mc->list, &id_priv->mc_list);
4148	spin_unlock(&id_priv->lock);
4149
4150	if (rdma_protocol_roce(id->device, id->port_num)) {
4151		kref_init(&mc->mcref);
4152		ret = cma_iboe_join_multicast(id_priv, mc);
4153	} else if (rdma_cap_ib_mcast(id->device, id->port_num))
4154		ret = cma_join_ib_multicast(id_priv, mc);
4155	else
4156		ret = -ENOSYS;
4157
4158	if (ret) {
4159		spin_lock_irq(&id_priv->lock);
4160		list_del(&mc->list);
4161		spin_unlock_irq(&id_priv->lock);
4162		kfree(mc);
4163	}
4164	return ret;
4165}
4166EXPORT_SYMBOL(rdma_join_multicast);
4167
4168void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4169{
4170	struct rdma_id_private *id_priv;
4171	struct cma_multicast *mc;
4172
4173	id_priv = container_of(id, struct rdma_id_private, id);
4174	spin_lock_irq(&id_priv->lock);
4175	list_for_each_entry(mc, &id_priv->mc_list, list) {
4176		if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
4177			list_del(&mc->list);
4178			spin_unlock_irq(&id_priv->lock);
4179
4180			if (id->qp)
4181				ib_detach_mcast(id->qp,
4182						&mc->multicast.ib->rec.mgid,
4183						be16_to_cpu(mc->multicast.ib->rec.mlid));
4184
4185			BUG_ON(id_priv->cma_dev->device != id->device);
4186
4187			if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4188				ib_sa_free_multicast(mc->multicast.ib);
4189				kfree(mc);
4190			} else if (rdma_protocol_roce(id->device, id->port_num)) {
4191				if (mc->igmp_joined) {
4192					struct rdma_dev_addr *dev_addr =
4193						&id->route.addr.dev_addr;
4194					struct net_device *ndev = NULL;
4195
4196					if (dev_addr->bound_dev_if)
4197						ndev = dev_get_by_index(dev_addr->net,
4198									dev_addr->bound_dev_if);
4199					if (ndev) {
4200						cma_igmp_send(ndev,
4201							      &mc->multicast.ib->rec.mgid,
4202							      false);
4203						dev_put(ndev);
4204					}
4205					mc->igmp_joined = false;
4206				}
4207				kref_put(&mc->mcref, release_mc);
4208			}
4209			return;
4210		}
4211	}
4212	spin_unlock_irq(&id_priv->lock);
4213}
4214EXPORT_SYMBOL(rdma_leave_multicast);
4215
4216static int
4217sysctl_cma_default_roce_mode(SYSCTL_HANDLER_ARGS)
4218{
4219	struct cma_device *cma_dev = arg1;
4220	const int port = arg2;
4221	char buf[64];
4222	int error;
4223
4224	strlcpy(buf, ib_cache_gid_type_str(
4225	    cma_get_default_gid_type(cma_dev, port)), sizeof(buf));
4226
4227	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
4228	if (error != 0 || req->newptr == NULL)
4229		goto done;
4230
4231	error = ib_cache_gid_parse_type_str(buf);
4232	if (error < 0) {
4233		error = EINVAL;
4234		goto done;
4235	}
4236
4237	cma_set_default_gid_type(cma_dev, port, error);
4238	error = 0;
4239done:
4240	return (error);
4241}
4242
4243static void cma_add_one(struct ib_device *device)
4244{
4245	struct cma_device *cma_dev;
4246	struct rdma_id_private *id_priv;
4247	unsigned int i;
4248
4249	cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4250	if (!cma_dev)
4251		return;
4252
4253	sysctl_ctx_init(&cma_dev->sysctl_ctx);
4254
4255	cma_dev->device = device;
4256	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4257					    sizeof(*cma_dev->default_gid_type),
4258					    GFP_KERNEL);
4259	if (!cma_dev->default_gid_type) {
4260		kfree(cma_dev);
4261		return;
4262	}
4263	for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4264		unsigned long supported_gids;
4265		unsigned int default_gid_type;
4266
4267		supported_gids = roce_gid_type_mask_support(device, i);
4268
4269		if (WARN_ON(!supported_gids)) {
4270			/* set something valid */
4271			default_gid_type = 0;
4272		} else if (test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids)) {
4273			/* prefer RoCEv2, if supported */
4274			default_gid_type = IB_GID_TYPE_ROCE_UDP_ENCAP;
4275		} else {
4276			default_gid_type = find_first_bit(&supported_gids,
4277			    BITS_PER_LONG);
4278		}
4279		cma_dev->default_gid_type[i - rdma_start_port(device)] =
4280		    default_gid_type;
4281	}
4282
4283	init_completion(&cma_dev->comp);
4284	atomic_set(&cma_dev->refcount, 1);
4285	INIT_LIST_HEAD(&cma_dev->id_list);
4286	ib_set_client_data(device, &cma_client, cma_dev);
4287
4288	mutex_lock(&lock);
4289	list_add_tail(&cma_dev->list, &dev_list);
4290	list_for_each_entry(id_priv, &listen_any_list, list)
4291		cma_listen_on_dev(id_priv, cma_dev);
4292	mutex_unlock(&lock);
4293
4294	for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4295		char buf[64];
4296
4297		snprintf(buf, sizeof(buf), "default_roce_mode_port%d", i);
4298
4299		(void) SYSCTL_ADD_PROC(&cma_dev->sysctl_ctx,
4300		    SYSCTL_CHILDREN(device->ports_parent->parent->oidp),
4301		    OID_AUTO, buf, CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
4302		    cma_dev, i, &sysctl_cma_default_roce_mode, "A",
4303		    "Default RoCE mode. Valid values: IB/RoCE v1 and RoCE v2");
4304	}
4305}
4306
4307static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4308{
4309	struct rdma_cm_event event;
4310	enum rdma_cm_state state;
4311	int ret = 0;
4312
4313	/* Record that we want to remove the device */
4314	state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4315	if (state == RDMA_CM_DESTROYING)
4316		return 0;
4317
4318	cma_cancel_operation(id_priv, state);
4319	mutex_lock(&id_priv->handler_mutex);
4320
4321	/* Check for destruction from another callback. */
4322	if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
4323		goto out;
4324
4325	memset(&event, 0, sizeof event);
4326	event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
4327	ret = id_priv->id.event_handler(&id_priv->id, &event);
4328out:
4329	mutex_unlock(&id_priv->handler_mutex);
4330	return ret;
4331}
4332
4333static void cma_process_remove(struct cma_device *cma_dev)
4334{
4335	struct rdma_id_private *id_priv;
4336	int ret;
4337
4338	mutex_lock(&lock);
4339	while (!list_empty(&cma_dev->id_list)) {
4340		id_priv = list_entry(cma_dev->id_list.next,
4341				     struct rdma_id_private, list);
4342
4343		list_del(&id_priv->listen_list);
4344		list_del_init(&id_priv->list);
4345		atomic_inc(&id_priv->refcount);
4346		mutex_unlock(&lock);
4347
4348		ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
4349		cma_deref_id(id_priv);
4350		if (ret)
4351			rdma_destroy_id(&id_priv->id);
4352
4353		mutex_lock(&lock);
4354	}
4355	mutex_unlock(&lock);
4356
4357	cma_deref_dev(cma_dev);
4358	wait_for_completion(&cma_dev->comp);
4359}
4360
4361static void cma_remove_one(struct ib_device *device, void *client_data)
4362{
4363	struct cma_device *cma_dev = client_data;
4364
4365	if (!cma_dev)
4366		return;
4367
4368	mutex_lock(&lock);
4369	list_del(&cma_dev->list);
4370	mutex_unlock(&lock);
4371
4372	cma_process_remove(cma_dev);
4373	sysctl_ctx_free(&cma_dev->sysctl_ctx);
4374	kfree(cma_dev->default_gid_type);
4375	kfree(cma_dev);
4376}
4377
4378static void cma_init_vnet(void *arg)
4379{
4380	struct cma_pernet *pernet = &VNET(cma_pernet);
4381
4382	idr_init(&pernet->tcp_ps);
4383	idr_init(&pernet->udp_ps);
4384	idr_init(&pernet->ipoib_ps);
4385	idr_init(&pernet->ib_ps);
4386}
4387VNET_SYSINIT(cma_init_vnet, SI_SUB_OFED_MODINIT - 1, SI_ORDER_FIRST, cma_init_vnet, NULL);
4388
4389static void cma_destroy_vnet(void *arg)
4390{
4391	struct cma_pernet *pernet = &VNET(cma_pernet);
4392
4393	idr_destroy(&pernet->tcp_ps);
4394	idr_destroy(&pernet->udp_ps);
4395	idr_destroy(&pernet->ipoib_ps);
4396	idr_destroy(&pernet->ib_ps);
4397}
4398VNET_SYSUNINIT(cma_destroy_vnet, SI_SUB_OFED_MODINIT - 1, SI_ORDER_SECOND, cma_destroy_vnet, NULL);
4399
4400static int __init cma_init(void)
4401{
4402	int ret;
4403
4404	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4405	if (!cma_wq)
4406		return -ENOMEM;
4407
4408	ib_sa_register_client(&sa_client);
4409	rdma_addr_register_client(&addr_client);
4410
4411	ret = ib_register_client(&cma_client);
4412	if (ret)
4413		goto err;
4414
4415	cma_configfs_init();
4416
4417	return 0;
4418
4419err:
4420	rdma_addr_unregister_client(&addr_client);
4421	ib_sa_unregister_client(&sa_client);
4422	destroy_workqueue(cma_wq);
4423	return ret;
4424}
4425
4426static void __exit cma_cleanup(void)
4427{
4428	cma_configfs_exit();
4429	ib_unregister_client(&cma_client);
4430	rdma_addr_unregister_client(&addr_client);
4431	ib_sa_unregister_client(&sa_client);
4432	destroy_workqueue(cma_wq);
4433}
4434
4435module_init(cma_init);
4436module_exit(cma_cleanup);
4437