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