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