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