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