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