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